1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <common.h>
17#include <asm/fsl_ddr_sdram.h>
18
19#include "ddr.h"
20
21extern unsigned int picos_to_mclk(unsigned int picos);
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55static inline int fsl_ddr_get_rtt(void)
56{
57 int rtt;
58
59#if defined(CONFIG_FSL_DDR1)
60 rtt = 0;
61#elif defined(CONFIG_FSL_DDR2)
62 rtt = 3;
63#else
64 rtt = 0;
65#endif
66
67 return rtt;
68}
69
70
71
72
73
74
75
76
77static inline unsigned int compute_cas_write_latency(void)
78{
79 unsigned int cwl;
80 const unsigned int mclk_ps = get_memory_clk_period_ps();
81
82 if (mclk_ps >= 2500)
83 cwl = 5;
84 else if (mclk_ps >= 1875)
85 cwl = 6;
86 else if (mclk_ps >= 1500)
87 cwl = 7;
88 else if (mclk_ps >= 1250)
89 cwl = 8;
90 else
91 cwl = 8;
92 return cwl;
93}
94
95
96static void set_csn_config(int i, fsl_ddr_cfg_regs_t *ddr,
97 const memctl_options_t *popts,
98 const dimm_params_t *dimm_params)
99{
100 unsigned int cs_n_en = 0;
101 unsigned int intlv_en = 0;
102 unsigned int intlv_ctl = 0;
103 unsigned int ap_n_en = 0;
104 unsigned int odt_rd_cfg = 0;
105 unsigned int odt_wr_cfg = 0;
106 unsigned int ba_bits_cs_n = 0;
107 unsigned int row_bits_cs_n = 0;
108 unsigned int col_bits_cs_n = 0;
109
110
111 if ((((i&1) == 0)
112 && (dimm_params[i/2].n_ranks == 1))
113 || (dimm_params[i/2].n_ranks == 2)) {
114 unsigned int n_banks_per_sdram_device;
115 cs_n_en = 1;
116 if (i == 0) {
117
118 intlv_en = popts->memctl_interleaving;
119 intlv_ctl = popts->memctl_interleaving_mode;
120 }
121 ap_n_en = popts->cs_local_opts[i].auto_precharge;
122 odt_rd_cfg = popts->cs_local_opts[i].odt_rd_cfg;
123 odt_wr_cfg = popts->cs_local_opts[i].odt_wr_cfg;
124 n_banks_per_sdram_device
125 = dimm_params[i/2].n_banks_per_sdram_device;
126 ba_bits_cs_n = __ilog2(n_banks_per_sdram_device) - 2;
127 row_bits_cs_n = dimm_params[i/2].n_row_addr - 12;
128 col_bits_cs_n = dimm_params[i/2].n_col_addr - 8;
129 }
130
131 ddr->cs[i].config = (0
132 | ((cs_n_en & 0x1) << 31)
133 | ((intlv_en & 0x3) << 29)
134 | ((intlv_ctl & 0xf) << 24)
135 | ((ap_n_en & 0x1) << 23)
136
137
138 | ((odt_rd_cfg & 0x7) << 20)
139
140
141 | ((odt_wr_cfg & 0x7) << 16)
142
143 | ((ba_bits_cs_n & 0x3) << 14)
144 | ((row_bits_cs_n & 0x7) << 8)
145 | ((col_bits_cs_n & 0x7) << 0)
146 );
147 debug("FSLDDR: cs[%d]_config = 0x%08x\n", i,ddr->cs[i].config);
148}
149
150
151
152static void set_csn_config_2(int i, fsl_ddr_cfg_regs_t *ddr)
153{
154 unsigned int pasr_cfg = 0;
155
156 ddr->cs[i].config_2 = ((pasr_cfg & 7) << 24);
157 debug("FSLDDR: cs[%d]_config_2 = 0x%08x\n", i, ddr->cs[i].config_2);
158}
159
160
161
162#if !defined(CONFIG_FSL_DDR1)
163
164
165
166
167
168
169static void set_timing_cfg_0(fsl_ddr_cfg_regs_t *ddr)
170{
171 unsigned char trwt_mclk = 0;
172 unsigned char twrt_mclk = 0;
173
174 unsigned char trrt_mclk = 0;
175 unsigned char twwt_mclk = 0;
176
177
178 unsigned char act_pd_exit_mclk;
179
180 unsigned char pre_pd_exit_mclk;
181
182 unsigned char taxpd_mclk;
183
184 unsigned char tmrd_mclk;
185
186#if defined(CONFIG_FSL_DDR3)
187
188
189
190
191
192
193
194
195 int tXP = max((get_memory_clk_period_ps() * 3), 7500);
196 act_pd_exit_mclk = picos_to_mclk(tXP);
197
198 pre_pd_exit_mclk = act_pd_exit_mclk;
199 taxpd_mclk = 8;
200 tmrd_mclk = 4;
201
202 trwt_mclk = 1;
203#else
204
205
206
207
208
209
210 act_pd_exit_mclk = 2;
211 pre_pd_exit_mclk = 2;
212 taxpd_mclk = 8;
213 tmrd_mclk = 2;
214#endif
215
216 ddr->timing_cfg_0 = (0
217 | ((trwt_mclk & 0x3) << 30)
218 | ((twrt_mclk & 0x3) << 28)
219 | ((trrt_mclk & 0x3) << 26)
220 | ((twwt_mclk & 0x3) << 24)
221 | ((act_pd_exit_mclk & 0x7) << 20)
222 | ((pre_pd_exit_mclk & 0xF) << 16)
223 | ((taxpd_mclk & 0xf) << 8)
224 | ((tmrd_mclk & 0xf) << 0)
225 );
226 debug("FSLDDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
227}
228#endif
229
230
231static void set_timing_cfg_3(fsl_ddr_cfg_regs_t *ddr,
232 const common_timing_params_t *common_dimm,
233 unsigned int cas_latency)
234{
235
236 unsigned int ext_acttopre = 0;
237 unsigned int ext_refrec;
238 unsigned int ext_caslat = 0;
239 unsigned int cntl_adj = 0;
240
241
242 if (picos_to_mclk(common_dimm->tRAS_ps) > 0x13)
243 ext_acttopre = 1;
244
245 ext_refrec = (picos_to_mclk(common_dimm->tRFC_ps) - 8) >> 4;
246
247
248 if (cas_latency > 8)
249 ext_caslat = 1;
250
251 ddr->timing_cfg_3 = (0
252 | ((ext_acttopre & 0x1) << 24)
253 | ((ext_refrec & 0xF) << 16)
254 | ((ext_caslat & 0x1) << 12)
255 | ((cntl_adj & 0x7) << 0)
256 );
257 debug("FSLDDR: timing_cfg_3 = 0x%08x\n", ddr->timing_cfg_3);
258}
259
260
261static void set_timing_cfg_1(fsl_ddr_cfg_regs_t *ddr,
262 const memctl_options_t *popts,
263 const common_timing_params_t *common_dimm,
264 unsigned int cas_latency)
265{
266
267 unsigned char pretoact_mclk;
268
269 unsigned char acttopre_mclk;
270
271 unsigned char acttorw_mclk;
272
273 unsigned char caslat_ctrl;
274
275 unsigned char refrec_ctrl;
276
277 unsigned char wrrec_mclk;
278
279 unsigned char acttoact_mclk;
280
281 unsigned char wrtord_mclk;
282
283 pretoact_mclk = picos_to_mclk(common_dimm->tRP_ps);
284 acttopre_mclk = picos_to_mclk(common_dimm->tRAS_ps);
285 acttorw_mclk = picos_to_mclk(common_dimm->tRCD_ps);
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303#if defined(CONFIG_FSL_DDR1)
304 caslat_ctrl = (cas_latency + 1) & 0x07;
305#elif defined(CONFIG_FSL_DDR2)
306 caslat_ctrl = 2 * cas_latency - 1;
307#else
308
309
310
311
312
313 if (cas_latency > 8)
314 cas_latency -= 8;
315 caslat_ctrl = 2 * cas_latency - 1;
316#endif
317
318 refrec_ctrl = picos_to_mclk(common_dimm->tRFC_ps) - 8;
319 wrrec_mclk = picos_to_mclk(common_dimm->tWR_ps);
320 if (popts->OTF_burst_chop_en)
321 wrrec_mclk += 2;
322
323 acttoact_mclk = picos_to_mclk(common_dimm->tRRD_ps);
324
325
326
327#if defined(CONFIG_FSL_DDR3)
328 if (acttoact_mclk < 4)
329 acttoact_mclk = 4;
330#endif
331 wrtord_mclk = picos_to_mclk(common_dimm->tWTR_ps);
332
333
334
335#if defined(CONFIG_FSL_DDR2)
336 if (wrtord_mclk < 2)
337 wrtord_mclk = 2;
338#elif defined(CONFIG_FSL_DDR3)
339 if (wrtord_mclk < 4)
340 wrtord_mclk = 4;
341#endif
342 if (popts->OTF_burst_chop_en)
343 wrtord_mclk += 2;
344
345 ddr->timing_cfg_1 = (0
346 | ((pretoact_mclk & 0x0F) << 28)
347 | ((acttopre_mclk & 0x0F) << 24)
348 | ((acttorw_mclk & 0xF) << 20)
349 | ((caslat_ctrl & 0xF) << 16)
350 | ((refrec_ctrl & 0xF) << 12)
351 | ((wrrec_mclk & 0x0F) << 8)
352 | ((acttoact_mclk & 0x07) << 4)
353 | ((wrtord_mclk & 0x07) << 0)
354 );
355 debug("FSLDDR: timing_cfg_1 = 0x%08x\n", ddr->timing_cfg_1);
356}
357
358
359static void set_timing_cfg_2(fsl_ddr_cfg_regs_t *ddr,
360 const memctl_options_t *popts,
361 const common_timing_params_t *common_dimm,
362 unsigned int cas_latency,
363 unsigned int additive_latency)
364{
365
366 unsigned char add_lat_mclk;
367
368 unsigned short cpo;
369
370 unsigned char wr_lat;
371
372 unsigned char rd_to_pre;
373
374 unsigned char wr_data_delay;
375
376 unsigned char cke_pls;
377
378 unsigned short four_act;
379
380
381 add_lat_mclk = additive_latency;
382 cpo = popts->cpo_override;
383
384#if defined(CONFIG_FSL_DDR1)
385
386
387
388
389
390
391 wr_lat = 0;
392#elif defined(CONFIG_FSL_DDR2)
393 wr_lat = cas_latency - 1;
394#else
395 wr_lat = compute_cas_write_latency();
396#endif
397
398 rd_to_pre = picos_to_mclk(common_dimm->tRTP_ps);
399
400
401
402#if defined(CONFIG_FSL_DDR2)
403 if (rd_to_pre < 2)
404 rd_to_pre = 2;
405#elif defined(CONFIG_FSL_DDR3)
406 if (rd_to_pre < 4)
407 rd_to_pre = 4;
408#endif
409 if (additive_latency)
410 rd_to_pre += additive_latency;
411 if (popts->OTF_burst_chop_en)
412 rd_to_pre += 2;
413
414 wr_data_delay = popts->write_data_delay;
415 cke_pls = picos_to_mclk(popts->tCKE_clock_pulse_width_ps);
416 four_act = picos_to_mclk(popts->tFAW_window_four_activates_ps);
417
418 ddr->timing_cfg_2 = (0
419 | ((add_lat_mclk & 0xf) << 28)
420 | ((cpo & 0x1f) << 23)
421 | ((wr_lat & 0xf) << 19)
422 | ((rd_to_pre & RD_TO_PRE_MASK) << RD_TO_PRE_SHIFT)
423 | ((wr_data_delay & WR_DATA_DELAY_MASK) << WR_DATA_DELAY_SHIFT)
424 | ((cke_pls & 0x7) << 6)
425 | ((four_act & 0x3f) << 0)
426 );
427 debug("FSLDDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2);
428}
429
430
431static void set_ddr_sdram_cfg(fsl_ddr_cfg_regs_t *ddr,
432 const memctl_options_t *popts,
433 const common_timing_params_t *common_dimm)
434{
435 unsigned int mem_en;
436 unsigned int sren;
437 unsigned int ecc_en;
438 unsigned int rd_en;
439 unsigned int sdram_type;
440 unsigned int dyn_pwr;
441 unsigned int dbw;
442 unsigned int eight_be = 0;
443 unsigned int ncap = 0;
444 unsigned int threeT_en;
445 unsigned int twoT_en;
446 unsigned int ba_intlv_ctl;
447 unsigned int x32_en = 0;
448 unsigned int pchb8 = 0;
449 unsigned int hse;
450 unsigned int mem_halt = 0;
451 unsigned int bi = 0;
452
453 mem_en = 1;
454 sren = popts->self_refresh_in_sleep;
455 if (common_dimm->all_DIMMs_ECC_capable) {
456
457 ecc_en = popts->ECC_mode;
458 } else {
459 ecc_en = 0;
460 }
461
462 rd_en = (common_dimm->all_DIMMs_registered
463 && !common_dimm->all_DIMMs_unbuffered);
464
465 sdram_type = CONFIG_FSL_SDRAM_TYPE;
466
467 dyn_pwr = popts->dynamic_power;
468 dbw = popts->data_bus_width;
469
470
471
472
473 if (sdram_type == SDRAM_TYPE_DDR3) {
474 if (popts->burst_length == DDR_BL8)
475 eight_be = 1;
476 if (popts->burst_length == DDR_OTF)
477 eight_be = 0;
478 if (dbw == 0x1)
479 eight_be = 1;
480 }
481
482 threeT_en = popts->threeT_en;
483 twoT_en = popts->twoT_en;
484 ba_intlv_ctl = popts->ba_intlv_ctl;
485 hse = popts->half_strength_driver_enable;
486
487 ddr->ddr_sdram_cfg = (0
488 | ((mem_en & 0x1) << 31)
489 | ((sren & 0x1) << 30)
490 | ((ecc_en & 0x1) << 29)
491 | ((rd_en & 0x1) << 28)
492 | ((sdram_type & 0x7) << 24)
493 | ((dyn_pwr & 0x1) << 21)
494 | ((dbw & 0x3) << 19)
495 | ((eight_be & 0x1) << 18)
496 | ((ncap & 0x1) << 17)
497 | ((threeT_en & 0x1) << 16)
498 | ((twoT_en & 0x1) << 15)
499 | ((ba_intlv_ctl & 0x7F) << 8)
500 | ((x32_en & 0x1) << 5)
501 | ((pchb8 & 0x1) << 4)
502 | ((hse & 0x1) << 3)
503 | ((mem_halt & 0x1) << 1)
504 | ((bi & 0x1) << 0)
505 );
506 debug("FSLDDR: ddr_sdram_cfg = 0x%08x\n", ddr->ddr_sdram_cfg);
507}
508
509
510static void set_ddr_sdram_cfg_2(fsl_ddr_cfg_regs_t *ddr,
511 const memctl_options_t *popts)
512{
513 unsigned int frc_sr = 0;
514 unsigned int sr_ie = 0;
515 unsigned int dll_rst_dis;
516 unsigned int dqs_cfg;
517 unsigned int odt_cfg;
518 unsigned int num_pr;
519 unsigned int obc_cfg;
520 unsigned int ap_en;
521 unsigned int d_init;
522 unsigned int rcw_en = 0;
523 unsigned int md_en = 0;
524
525 dll_rst_dis = 1;
526 dqs_cfg = popts->DQS_config;
527 if (popts->cs_local_opts[0].odt_rd_cfg
528 || popts->cs_local_opts[0].odt_wr_cfg) {
529
530 odt_cfg = 2;
531 } else {
532 odt_cfg = 0;
533 }
534
535 num_pr = 1;
536
537
538
539
540
541
542
543
544#if defined(CONFIG_FSL_DDR3)
545 obc_cfg = popts->OTF_burst_chop_en;
546#else
547 obc_cfg = 0;
548#endif
549
550 ap_en = 0;
551
552#if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
553
554 d_init = 1;
555 ddr->ddr_data_init = CONFIG_MEM_INIT_VALUE;
556 debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init);
557#else
558
559 d_init = 0;
560#endif
561
562#if defined(CONFIG_FSL_DDR3)
563 md_en = popts->mirrored_dimm;
564#endif
565 ddr->ddr_sdram_cfg_2 = (0
566 | ((frc_sr & 0x1) << 31)
567 | ((sr_ie & 0x1) << 30)
568 | ((dll_rst_dis & 0x1) << 29)
569 | ((dqs_cfg & 0x3) << 26)
570 | ((odt_cfg & 0x3) << 21)
571 | ((num_pr & 0xf) << 12)
572 | ((obc_cfg & 0x1) << 6)
573 | ((ap_en & 0x1) << 5)
574 | ((d_init & 0x1) << 4)
575 | ((rcw_en & 0x1) << 2)
576 | ((md_en & 0x1) << 0)
577 );
578 debug("FSLDDR: ddr_sdram_cfg_2 = 0x%08x\n", ddr->ddr_sdram_cfg_2);
579}
580
581
582static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr,
583 const memctl_options_t *popts)
584{
585 unsigned short esdmode2 = 0;
586 unsigned short esdmode3 = 0;
587
588#if defined(CONFIG_FSL_DDR3)
589 unsigned int rtt_wr = 0;
590 unsigned int srt = 0;
591 unsigned int asr = 0;
592 unsigned int cwl = compute_cas_write_latency() - 5;
593 unsigned int pasr = 0;
594
595 if (popts->rtt_override)
596 rtt_wr = popts->rtt_wr_override_value;
597
598 esdmode2 = (0
599 | ((rtt_wr & 0x3) << 9)
600 | ((srt & 0x1) << 7)
601 | ((asr & 0x1) << 6)
602 | ((cwl & 0x7) << 3)
603 | ((pasr & 0x7) << 0));
604#endif
605 ddr->ddr_sdram_mode_2 = (0
606 | ((esdmode2 & 0xFFFF) << 16)
607 | ((esdmode3 & 0xFFFF) << 0)
608 );
609 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
610}
611
612
613static void set_ddr_sdram_interval(fsl_ddr_cfg_regs_t *ddr,
614 const memctl_options_t *popts,
615 const common_timing_params_t *common_dimm)
616{
617 unsigned int refint;
618 unsigned int bstopre;
619
620 refint = picos_to_mclk(common_dimm->refresh_rate_ps);
621
622 bstopre = popts->bstopre;
623
624
625 ddr->ddr_sdram_interval = (0
626 | ((refint & 0xFFFF) << 16)
627 | ((bstopre & 0x3FFF) << 0)
628 );
629 debug("FSLDDR: ddr_sdram_interval = 0x%08x\n", ddr->ddr_sdram_interval);
630}
631
632#if defined(CONFIG_FSL_DDR3)
633
634static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
635 const memctl_options_t *popts,
636 const common_timing_params_t *common_dimm,
637 unsigned int cas_latency,
638 unsigned int additive_latency)
639{
640 unsigned short esdmode;
641 unsigned short sdmode;
642
643
644 unsigned int qoff = 0;
645 unsigned int tdqs_en = 0;
646 unsigned int rtt;
647 unsigned int wrlvl_en = 0;
648 unsigned int al = 0;
649 unsigned int dic = 1;
650 unsigned int dll_en = 0;
651
652
653
654 unsigned int dll_on;
655 unsigned int wr;
656 unsigned int dll_rst;
657 unsigned int mode;
658 unsigned int caslat = 4;
659
660 unsigned int bt;
661 unsigned int bl;
662
663 unsigned int wr_mclk;
664
665 const unsigned int mclk_ps = get_memory_clk_period_ps();
666
667 rtt = fsl_ddr_get_rtt();
668 if (popts->rtt_override)
669 rtt = popts->rtt_override_value;
670
671 if (additive_latency == (cas_latency - 1))
672 al = 1;
673 if (additive_latency == (cas_latency - 2))
674 al = 2;
675
676
677
678
679
680
681
682
683 esdmode = (0
684 | ((qoff & 0x1) << 12)
685 | ((tdqs_en & 0x1) << 11)
686 | ((rtt & 0x4) << 7)
687 | ((wrlvl_en & 0x1) << 7)
688 | ((rtt & 0x2) << 5)
689 | ((dic & 0x2) << 4)
690 | ((al & 0x3) << 3)
691 | ((rtt & 0x1) << 2)
692 | ((dic & 0x1) << 1)
693 | ((dll_en & 0x1) << 0)
694 );
695
696
697
698
699
700
701 dll_on = 1;
702 wr_mclk = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps;
703 if (wr_mclk >= 12)
704 wr = 6;
705 else if (wr_mclk >= 9)
706 wr = 5;
707 else
708 wr = wr_mclk - 4;
709 dll_rst = 0;
710 mode = 0;
711
712
713 if (cas_latency >= 5 && cas_latency <= 11) {
714 unsigned char cas_latency_table[7] = {
715 0x2,
716 0x4,
717 0x6,
718 0x8,
719 0xa,
720 0xc,
721 0xe
722 };
723 caslat = cas_latency_table[cas_latency - 5];
724 }
725 bt = 0;
726
727 switch (popts->burst_length) {
728 case DDR_BL8:
729 bl = 0;
730 break;
731 case DDR_OTF:
732 bl = 1;
733 break;
734 case DDR_BC4:
735 bl = 2;
736 break;
737 default:
738 printf("Error: invalid burst length of %u specified. "
739 " Defaulting to on-the-fly BC4 or BL8 beats.\n",
740 popts->burst_length);
741 bl = 1;
742 break;
743 }
744
745 sdmode = (0
746 | ((dll_on & 0x1) << 12)
747 | ((wr & 0x7) << 9)
748 | ((dll_rst & 0x1) << 8)
749 | ((mode & 0x1) << 7)
750 | (((caslat >> 1) & 0x7) << 4)
751 | ((bt & 0x1) << 3)
752 | ((bl & 0x3) << 0)
753 );
754
755 ddr->ddr_sdram_mode = (0
756 | ((esdmode & 0xFFFF) << 16)
757 | ((sdmode & 0xFFFF) << 0)
758 );
759
760 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
761}
762
763#else
764
765
766static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
767 const memctl_options_t *popts,
768 const common_timing_params_t *common_dimm,
769 unsigned int cas_latency,
770 unsigned int additive_latency)
771{
772 unsigned short esdmode;
773 unsigned short sdmode;
774
775
776
777
778
779
780
781
782
783 unsigned int mrs = 0;
784 unsigned int outputs = 0;
785 unsigned int rdqs_en = 0;
786 unsigned int dqs_en = 0;
787 unsigned int ocd = 0;
788
789 unsigned int rtt;
790 unsigned int al;
791 unsigned int ods = 0;
792
793
794 unsigned int dll_en = 0;
795
796
797
798 unsigned int mr;
799 unsigned int pd;
800 unsigned int wr;
801 unsigned int dll_res;
802 unsigned int mode;
803 unsigned int caslat = 0;
804
805 unsigned int bt;
806 unsigned int bl;
807
808#if defined(CONFIG_FSL_DDR2)
809 const unsigned int mclk_ps = get_memory_clk_period_ps();
810#endif
811
812 rtt = fsl_ddr_get_rtt();
813
814 al = additive_latency;
815
816 esdmode = (0
817 | ((mrs & 0x3) << 14)
818 | ((outputs & 0x1) << 12)
819 | ((rdqs_en & 0x1) << 11)
820 | ((dqs_en & 0x1) << 10)
821 | ((ocd & 0x7) << 7)
822 | ((rtt & 0x2) << 5)
823 | ((al & 0x7) << 3)
824 | ((rtt & 0x1) << 2)
825 | ((ods & 0x1) << 1)
826 | ((dll_en & 0x1) << 0)
827 );
828
829 mr = 0;
830
831
832
833
834
835 pd = 0;
836
837#if defined(CONFIG_FSL_DDR1)
838 wr = 0;
839#elif defined(CONFIG_FSL_DDR2)
840 wr = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps - 1;
841#endif
842 dll_res = 0;
843 mode = 0;
844
845#if defined(CONFIG_FSL_DDR1)
846 if (1 <= cas_latency && cas_latency <= 4) {
847 unsigned char mode_caslat_table[4] = {
848 0x5,
849 0x2,
850 0x6,
851 0x3
852 };
853 caslat = mode_caslat_table[cas_latency - 1];
854 } else {
855 printf("Warning: unknown cas_latency %d\n", cas_latency);
856 }
857#elif defined(CONFIG_FSL_DDR2)
858 caslat = cas_latency;
859#endif
860 bt = 0;
861
862 switch (popts->burst_length) {
863 case DDR_BL4:
864 bl = 2;
865 break;
866 case DDR_BL8:
867 bl = 3;
868 break;
869 default:
870 printf("Error: invalid burst length of %u specified. "
871 " Defaulting to 4 beats.\n",
872 popts->burst_length);
873 bl = 2;
874 break;
875 }
876
877 sdmode = (0
878 | ((mr & 0x3) << 14)
879 | ((pd & 0x1) << 12)
880 | ((wr & 0x7) << 9)
881 | ((dll_res & 0x1) << 8)
882 | ((mode & 0x1) << 7)
883 | ((caslat & 0x7) << 4)
884 | ((bt & 0x1) << 3)
885 | ((bl & 0x7) << 0)
886 );
887
888 ddr->ddr_sdram_mode = (0
889 | ((esdmode & 0xFFFF) << 16)
890 | ((sdmode & 0xFFFF) << 0)
891 );
892 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
893}
894#endif
895
896
897static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr)
898{
899 unsigned int init_value;
900
901 init_value = 0xDEADBEEF;
902 ddr->ddr_data_init = init_value;
903}
904
905
906
907
908
909
910static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
911 const memctl_options_t *popts)
912{
913 unsigned int clk_adjust;
914
915 clk_adjust = popts->clk_adjust;
916 ddr->ddr_sdram_clk_cntl = (clk_adjust & 0xF) << 23;
917}
918
919
920static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
921{
922 unsigned int init_addr = 0;
923
924 ddr->ddr_init_addr = init_addr;
925}
926
927
928static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
929{
930 unsigned int uia = 0;
931 unsigned int init_ext_addr = 0;
932
933 ddr->ddr_init_ext_addr = (0
934 | ((uia & 0x1) << 31)
935 | (init_ext_addr & 0xF)
936 );
937}
938
939
940static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr,
941 const memctl_options_t *popts)
942{
943 unsigned int rwt = 0;
944 unsigned int wrt = 0;
945 unsigned int rrt = 0;
946 unsigned int wwt = 0;
947 unsigned int dll_lock = 0;
948
949#if defined(CONFIG_FSL_DDR3)
950 if (popts->burst_length == DDR_BL8) {
951
952 rrt = 0;
953 wwt = 0;
954 } else {
955
956 rrt = 2;
957 wwt = 2;
958 }
959 dll_lock = 1;
960#endif
961 ddr->timing_cfg_4 = (0
962 | ((rwt & 0xf) << 28)
963 | ((wrt & 0xf) << 24)
964 | ((rrt & 0xf) << 20)
965 | ((wwt & 0xf) << 16)
966 | (dll_lock & 0x3)
967 );
968 debug("FSLDDR: timing_cfg_4 = 0x%08x\n", ddr->timing_cfg_4);
969}
970
971
972static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr)
973{
974 unsigned int rodt_on = 0;
975 unsigned int rodt_off = 0;
976 unsigned int wodt_on = 0;
977 unsigned int wodt_off = 0;
978
979#if defined(CONFIG_FSL_DDR3)
980 rodt_on = 3;
981 rodt_off = 4;
982 wodt_on = 2;
983 wodt_off = 4;
984#endif
985
986 ddr->timing_cfg_5 = (0
987 | ((rodt_on & 0x1f) << 24)
988 | ((rodt_off & 0x7) << 20)
989 | ((wodt_on & 0x1f) << 12)
990 | ((wodt_off & 0x7) << 8)
991 );
992 debug("FSLDDR: timing_cfg_5 = 0x%08x\n", ddr->timing_cfg_5);
993}
994
995
996static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int zq_en)
997{
998 unsigned int zqinit = 0;
999
1000 unsigned int zqoper = 0;
1001
1002 unsigned int zqcs = 0;
1003
1004 if (zq_en) {
1005 zqinit = 9;
1006 zqoper = 8;
1007 zqcs = 6;
1008 }
1009
1010 ddr->ddr_zq_cntl = (0
1011 | ((zq_en & 0x1) << 31)
1012 | ((zqinit & 0xF) << 24)
1013 | ((zqoper & 0xF) << 16)
1014 | ((zqcs & 0xF) << 8)
1015 );
1016}
1017
1018
1019static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int wrlvl_en,
1020 const memctl_options_t *popts)
1021{
1022
1023
1024
1025
1026 unsigned int wrlvl_mrd = 0;
1027
1028 unsigned int wrlvl_odten = 0;
1029
1030 unsigned int wrlvl_dqsen = 0;
1031
1032 unsigned int wrlvl_smpl = 0;
1033
1034 unsigned int wrlvl_wlr = 0;
1035
1036 unsigned int wrlvl_start = 0;
1037
1038
1039 if (wrlvl_en) {
1040
1041 wrlvl_mrd = 0x6;
1042
1043 wrlvl_odten = 0x7;
1044
1045 wrlvl_dqsen = 0x5;
1046
1047
1048
1049
1050
1051 wrlvl_smpl = 0xf;
1052
1053
1054
1055
1056
1057 wrlvl_wlr = 0x5;
1058
1059
1060
1061
1062
1063 wrlvl_start = 0x8;
1064
1065
1066
1067
1068 if (popts->wrlvl_override) {
1069 wrlvl_smpl = popts->wrlvl_sample;
1070 wrlvl_start = popts->wrlvl_start;
1071 }
1072 }
1073
1074 ddr->ddr_wrlvl_cntl = (0
1075 | ((wrlvl_en & 0x1) << 31)
1076 | ((wrlvl_mrd & 0x7) << 24)
1077 | ((wrlvl_odten & 0x7) << 20)
1078 | ((wrlvl_dqsen & 0x7) << 16)
1079 | ((wrlvl_smpl & 0xf) << 12)
1080 | ((wrlvl_wlr & 0x7) << 8)
1081 | ((wrlvl_start & 0x1F) << 0)
1082 );
1083}
1084
1085
1086static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr, unsigned int sr_it)
1087{
1088
1089 ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
1090}
1091
1092
1093static void set_ddr_sdram_rcw_1(fsl_ddr_cfg_regs_t *ddr)
1094{
1095 unsigned int rcw0 = 0;
1096 unsigned int rcw1 = 0;
1097 unsigned int rcw2 = 0;
1098 unsigned int rcw3 = 0;
1099 unsigned int rcw4 = 0;
1100 unsigned int rcw5 = 0;
1101 unsigned int rcw6 = 0;
1102 unsigned int rcw7 = 0;
1103
1104 ddr->ddr_sdram_rcw_1 = (0
1105 | ((rcw0 & 0xF) << 28)
1106 | ((rcw1 & 0xF) << 24)
1107 | ((rcw2 & 0xF) << 20)
1108 | ((rcw3 & 0xF) << 16)
1109 | ((rcw4 & 0xF) << 12)
1110 | ((rcw5 & 0xF) << 8)
1111 | ((rcw6 & 0xF) << 4)
1112 | ((rcw7 & 0xF) << 0)
1113 );
1114}
1115
1116
1117static void set_ddr_sdram_rcw_2(fsl_ddr_cfg_regs_t *ddr)
1118{
1119 unsigned int rcw8 = 0;
1120 unsigned int rcw9 = 0;
1121 unsigned int rcw10 = 0;
1122 unsigned int rcw11 = 0;
1123 unsigned int rcw12 = 0;
1124 unsigned int rcw13 = 0;
1125 unsigned int rcw14 = 0;
1126 unsigned int rcw15 = 0;
1127
1128 ddr->ddr_sdram_rcw_2 = (0
1129 | ((rcw8 & 0xF) << 28)
1130 | ((rcw9 & 0xF) << 24)
1131 | ((rcw10 & 0xF) << 20)
1132 | ((rcw11 & 0xF) << 16)
1133 | ((rcw12 & 0xF) << 12)
1134 | ((rcw13 & 0xF) << 8)
1135 | ((rcw14 & 0xF) << 4)
1136 | ((rcw15 & 0xF) << 0)
1137 );
1138}
1139
1140unsigned int
1141check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
1142{
1143 unsigned int res = 0;
1144
1145
1146
1147
1148
1149 if (ddr->ddr_sdram_cfg & 0x10000000
1150 && ddr->ddr_sdram_cfg & 0x00008000) {
1151 printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
1152 " should not be set at the same time.\n");
1153 res++;
1154 }
1155
1156 return res;
1157}
1158
1159unsigned int
1160compute_fsl_memctl_config_regs(const memctl_options_t *popts,
1161 fsl_ddr_cfg_regs_t *ddr,
1162 const common_timing_params_t *common_dimm,
1163 const dimm_params_t *dimm_params,
1164 unsigned int dbw_cap_adj)
1165{
1166 unsigned int i;
1167 unsigned int cas_latency;
1168 unsigned int additive_latency;
1169 unsigned int sr_it;
1170 unsigned int zq_en;
1171 unsigned int wrlvl_en;
1172
1173 memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
1174
1175 if (common_dimm == NULL) {
1176 printf("Error: subset DIMM params struct null pointer\n");
1177 return 1;
1178 }
1179
1180
1181
1182
1183
1184
1185 cas_latency = (popts->cas_latency_override)
1186 ? popts->cas_latency_override_value
1187 : common_dimm->lowest_common_SPD_caslat;
1188
1189 additive_latency = (popts->additive_latency_override)
1190 ? popts->additive_latency_override_value
1191 : common_dimm->additive_latency;
1192
1193 sr_it = (popts->auto_self_refresh_en)
1194 ? popts->sr_it
1195 : 0;
1196
1197 zq_en = (popts->zq_en) ? 1 : 0;
1198
1199 wrlvl_en = (popts->wrlvl_en) ? 1 : 0;
1200
1201
1202 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1203 unsigned long long ea = 0, sa = 0;
1204
1205 if (popts->ba_intlv_ctl && (i > 0) &&
1206 ((popts->ba_intlv_ctl & 0x60) != FSL_DDR_CS2_CS3 )) {
1207
1208
1209
1210
1211
1212
1213 set_csn_config(i, ddr, popts, dimm_params);
1214 break;
1215 }
1216
1217 if (dimm_params[i/2].n_ranks == 0) {
1218 debug("Skipping setup of CS%u "
1219 "because n_ranks on DIMM %u is 0\n", i, i/2);
1220 continue;
1221 }
1222 if (popts->memctl_interleaving && popts->ba_intlv_ctl) {
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232 unsigned long long rank_density
1233 = dimm_params[0].capacity;
1234 ea = (2 * (rank_density >> dbw_cap_adj)) - 1;
1235 }
1236 else if (!popts->memctl_interleaving && popts->ba_intlv_ctl) {
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246 unsigned long long rank_density
1247 = dimm_params[i/2].rank_density;
1248 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
1249 case FSL_DDR_CS0_CS1_CS2_CS3:
1250
1251
1252
1253 sa = common_dimm->base_address;
1254 ea = sa + (4 * (rank_density >> dbw_cap_adj))-1;
1255 break;
1256 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
1257
1258
1259
1260 if (!(i&1)) {
1261 sa = dimm_params[i/2].base_address;
1262 ea = sa + (i * (rank_density >>
1263 dbw_cap_adj)) - 1;
1264 }
1265 break;
1266 case FSL_DDR_CS0_CS1:
1267
1268
1269
1270 sa = common_dimm->base_address;
1271 ea = sa + (2 * (rank_density >> dbw_cap_adj))-1;
1272 break;
1273 case FSL_DDR_CS2_CS3:
1274
1275 if (i == 2) {
1276 sa = dimm_params[i/2].base_address;
1277 ea = sa + (2 * (rank_density >>
1278 dbw_cap_adj)) - 1;
1279 }
1280 break;
1281 default:
1282 break;
1283 }
1284 }
1285 else if (popts->memctl_interleaving && !popts->ba_intlv_ctl) {
1286
1287
1288
1289
1290
1291
1292
1293
1294 if (i == 0) {
1295 unsigned long long rank_density
1296 = dimm_params[0].rank_density;
1297 ea = (2 * (rank_density >> dbw_cap_adj)) - 1;
1298 }
1299
1300 }
1301 else if (!popts->memctl_interleaving && !popts->ba_intlv_ctl) {
1302
1303
1304
1305
1306 unsigned long long rank_density
1307 = dimm_params[i/2].rank_density;
1308 sa = dimm_params[i/2].base_address;
1309 ea = sa + (rank_density >> dbw_cap_adj) - 1;
1310 if (i&1) {
1311 if ((dimm_params[i/2].n_ranks == 1)) {
1312
1313 sa = 0;
1314 ea = 0;
1315 } else {
1316
1317 sa += rank_density >> dbw_cap_adj;
1318 ea += rank_density >> dbw_cap_adj;
1319 }
1320 }
1321 }
1322
1323 sa >>= 24;
1324 ea >>= 24;
1325
1326 ddr->cs[i].bnds = (0
1327 | ((sa & 0xFFF) << 16)
1328 | ((ea & 0xFFF) << 0)
1329 );
1330
1331 debug("FSLDDR: cs[%d]_bnds = 0x%08x\n", i, ddr->cs[i].bnds);
1332 set_csn_config(i, ddr, popts, dimm_params);
1333 set_csn_config_2(i, ddr);
1334 }
1335
1336#if !defined(CONFIG_FSL_DDR1)
1337 set_timing_cfg_0(ddr);
1338#endif
1339
1340 set_timing_cfg_3(ddr, common_dimm, cas_latency);
1341 set_timing_cfg_1(ddr, popts, common_dimm, cas_latency);
1342 set_timing_cfg_2(ddr, popts, common_dimm,
1343 cas_latency, additive_latency);
1344
1345 set_ddr_sdram_cfg(ddr, popts, common_dimm);
1346
1347 set_ddr_sdram_cfg_2(ddr, popts);
1348 set_ddr_sdram_mode(ddr, popts, common_dimm,
1349 cas_latency, additive_latency);
1350 set_ddr_sdram_mode_2(ddr, popts);
1351 set_ddr_sdram_interval(ddr, popts, common_dimm);
1352 set_ddr_data_init(ddr);
1353 set_ddr_sdram_clk_cntl(ddr, popts);
1354 set_ddr_init_addr(ddr);
1355 set_ddr_init_ext_addr(ddr);
1356 set_timing_cfg_4(ddr, popts);
1357 set_timing_cfg_5(ddr);
1358
1359 set_ddr_zq_cntl(ddr, zq_en);
1360 set_ddr_wrlvl_cntl(ddr, wrlvl_en, popts);
1361
1362 set_ddr_sr_cntr(ddr, sr_it);
1363
1364 set_ddr_sdram_rcw_1(ddr);
1365 set_ddr_sdram_rcw_2(ddr);
1366
1367 return check_fsl_memctl_config_regs(ddr);
1368}
1369