2 * Copyright 2008-2014 Freescale Semiconductor, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
8 * Generic driver for Freescale DDR/DDR2/DDR3 memory controller.
9 * Based on code from spd_sdram.c
10 * Author: James Yang [at freescale.com]
14 #include <fsl_ddr_sdram.h>
17 #include <fsl_immap.h>
20 unsigned int picos_to_mclk(unsigned int picos);
23 * Determine Rtt value.
25 * This should likely be either board or controller specific.
27 * Rtt(nominal) - DDR2:
32 * Rtt(nominal) - DDR3:
40 * FIXME: Apparently 8641 needs a value of 2
41 * FIXME: Old code seys if 667 MHz or higher, use 3 on 8572
43 * FIXME: There was some effort down this line earlier:
46 * for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL/2; i++) {
47 * if (popts->dimmslot[i].num_valid_cs
48 * && (popts->cs_local_opts[2*i].odt_rd_cfg
49 * || popts->cs_local_opts[2*i].odt_wr_cfg)) {
55 static inline int fsl_ddr_get_rtt(void)
59 #if defined(CONFIG_SYS_FSL_DDR1)
61 #elif defined(CONFIG_SYS_FSL_DDR2)
70 #ifdef CONFIG_SYS_FSL_DDR4
72 * compute CAS write latency according to DDR4 spec
73 * CWL = 9 for <= 1600MT/s
81 static inline unsigned int compute_cas_write_latency(void)
84 const unsigned int mclk_ps = get_memory_clk_period_ps();
87 else if (mclk_ps >= 1070)
89 else if (mclk_ps >= 935)
91 else if (mclk_ps >= 833)
93 else if (mclk_ps >= 750)
95 else if (mclk_ps >= 681)
104 * compute the CAS write latency according to DDR3 spec
105 * CWL = 5 if tCK >= 2.5ns
106 * 6 if 2.5ns > tCK >= 1.875ns
107 * 7 if 1.875ns > tCK >= 1.5ns
108 * 8 if 1.5ns > tCK >= 1.25ns
109 * 9 if 1.25ns > tCK >= 1.07ns
110 * 10 if 1.07ns > tCK >= 0.935ns
111 * 11 if 0.935ns > tCK >= 0.833ns
112 * 12 if 0.833ns > tCK >= 0.75ns
114 static inline unsigned int compute_cas_write_latency(void)
117 const unsigned int mclk_ps = get_memory_clk_period_ps();
121 else if (mclk_ps >= 1875)
123 else if (mclk_ps >= 1500)
125 else if (mclk_ps >= 1250)
127 else if (mclk_ps >= 1070)
129 else if (mclk_ps >= 935)
131 else if (mclk_ps >= 833)
133 else if (mclk_ps >= 750)
137 printf("Warning: CWL is out of range\n");
143 /* Chip Select Configuration (CSn_CONFIG) */
144 static void set_csn_config(int dimm_number, int i, fsl_ddr_cfg_regs_t *ddr,
145 const memctl_options_t *popts,
146 const dimm_params_t *dimm_params)
148 unsigned int cs_n_en = 0; /* Chip Select enable */
149 unsigned int intlv_en = 0; /* Memory controller interleave enable */
150 unsigned int intlv_ctl = 0; /* Interleaving control */
151 unsigned int ap_n_en = 0; /* Chip select n auto-precharge enable */
152 unsigned int odt_rd_cfg = 0; /* ODT for reads configuration */
153 unsigned int odt_wr_cfg = 0; /* ODT for writes configuration */
154 unsigned int ba_bits_cs_n = 0; /* Num of bank bits for SDRAM on CSn */
155 unsigned int row_bits_cs_n = 0; /* Num of row bits for SDRAM on CSn */
156 unsigned int col_bits_cs_n = 0; /* Num of ocl bits for SDRAM on CSn */
158 #ifdef CONFIG_SYS_FSL_DDR4
159 unsigned int bg_bits_cs_n = 0; /* Num of bank group bits */
161 unsigned int n_banks_per_sdram_device;
164 /* Compute CS_CONFIG only for existing ranks of each DIMM. */
167 if (dimm_params[dimm_number].n_ranks > 0) {
169 /* These fields only available in CS0_CONFIG */
170 if (!popts->memctl_interleaving)
172 switch (popts->memctl_interleaving_mode) {
173 case FSL_DDR_256B_INTERLEAVING:
174 case FSL_DDR_CACHE_LINE_INTERLEAVING:
175 case FSL_DDR_PAGE_INTERLEAVING:
176 case FSL_DDR_BANK_INTERLEAVING:
177 case FSL_DDR_SUPERBANK_INTERLEAVING:
178 intlv_en = popts->memctl_interleaving;
179 intlv_ctl = popts->memctl_interleaving_mode;
187 if ((dimm_number == 0 && dimm_params[0].n_ranks > 1) || \
188 (dimm_number == 1 && dimm_params[1].n_ranks > 0))
192 if ((dimm_number == 0 && dimm_params[0].n_ranks > 2) || \
193 (dimm_number >= 1 && dimm_params[dimm_number].n_ranks > 0))
197 if ((dimm_number == 0 && dimm_params[0].n_ranks > 3) || \
198 (dimm_number == 1 && dimm_params[1].n_ranks > 1) || \
199 (dimm_number == 3 && dimm_params[3].n_ranks > 0))
207 ap_n_en = popts->cs_local_opts[i].auto_precharge;
208 odt_rd_cfg = popts->cs_local_opts[i].odt_rd_cfg;
209 odt_wr_cfg = popts->cs_local_opts[i].odt_wr_cfg;
210 #ifdef CONFIG_SYS_FSL_DDR4
211 ba_bits_cs_n = dimm_params[dimm_number].bank_addr_bits;
212 bg_bits_cs_n = dimm_params[dimm_number].bank_group_bits;
214 n_banks_per_sdram_device
215 = dimm_params[dimm_number].n_banks_per_sdram_device;
216 ba_bits_cs_n = __ilog2(n_banks_per_sdram_device) - 2;
218 row_bits_cs_n = dimm_params[dimm_number].n_row_addr - 12;
219 col_bits_cs_n = dimm_params[dimm_number].n_col_addr - 8;
221 ddr->cs[i].config = (0
222 | ((cs_n_en & 0x1) << 31)
223 | ((intlv_en & 0x3) << 29)
224 | ((intlv_ctl & 0xf) << 24)
225 | ((ap_n_en & 0x1) << 23)
227 /* XXX: some implementation only have 1 bit starting at left */
228 | ((odt_rd_cfg & 0x7) << 20)
230 /* XXX: Some implementation only have 1 bit starting at left */
231 | ((odt_wr_cfg & 0x7) << 16)
233 | ((ba_bits_cs_n & 0x3) << 14)
234 | ((row_bits_cs_n & 0x7) << 8)
235 #ifdef CONFIG_SYS_FSL_DDR4
236 | ((bg_bits_cs_n & 0x3) << 4)
238 | ((col_bits_cs_n & 0x7) << 0)
240 debug("FSLDDR: cs[%d]_config = 0x%08x\n", i,ddr->cs[i].config);
243 /* Chip Select Configuration 2 (CSn_CONFIG_2) */
245 static void set_csn_config_2(int i, fsl_ddr_cfg_regs_t *ddr)
247 unsigned int pasr_cfg = 0; /* Partial array self refresh config */
249 ddr->cs[i].config_2 = ((pasr_cfg & 7) << 24);
250 debug("FSLDDR: cs[%d]_config_2 = 0x%08x\n", i, ddr->cs[i].config_2);
253 /* -3E = 667 CL5, -25 = CL6 800, -25E = CL5 800 */
255 #if !defined(CONFIG_SYS_FSL_DDR1)
256 static inline int avoid_odt_overlap(const dimm_params_t *dimm_params)
258 #if CONFIG_DIMM_SLOTS_PER_CTLR == 1
259 if (dimm_params[0].n_ranks == 4)
263 #if CONFIG_DIMM_SLOTS_PER_CTLR == 2
264 if ((dimm_params[0].n_ranks == 2) &&
265 (dimm_params[1].n_ranks == 2))
268 #ifdef CONFIG_FSL_DDR_FIRST_SLOT_QUAD_CAPABLE
269 if (dimm_params[0].n_ranks == 4)
277 * DDR SDRAM Timing Configuration 0 (TIMING_CFG_0)
279 * Avoid writing for DDR I. The new PQ38 DDR controller
280 * dreams up non-zero default values to be backwards compatible.
282 static void set_timing_cfg_0(fsl_ddr_cfg_regs_t *ddr,
283 const memctl_options_t *popts,
284 const dimm_params_t *dimm_params)
286 unsigned char trwt_mclk = 0; /* Read-to-write turnaround */
287 unsigned char twrt_mclk = 0; /* Write-to-read turnaround */
288 /* 7.5 ns on -3E; 0 means WL - CL + BL/2 + 1 */
289 unsigned char trrt_mclk = 0; /* Read-to-read turnaround */
290 unsigned char twwt_mclk = 0; /* Write-to-write turnaround */
292 /* Active powerdown exit timing (tXARD and tXARDS). */
293 unsigned char act_pd_exit_mclk;
294 /* Precharge powerdown exit timing (tXP). */
295 unsigned char pre_pd_exit_mclk;
296 /* ODT powerdown exit timing (tAXPD). */
297 unsigned char taxpd_mclk = 0;
298 /* Mode register set cycle time (tMRD). */
299 unsigned char tmrd_mclk;
300 #if defined(CONFIG_SYS_FSL_DDR4) || defined(CONFIG_SYS_FSL_DDR3)
301 const unsigned int mclk_ps = get_memory_clk_period_ps();
304 #ifdef CONFIG_SYS_FSL_DDR4
305 /* tXP=max(4nCK, 6ns) */
306 int txp = max((int)mclk_ps * 4, 6000); /* unit=ps */
309 act_pd_exit_mclk = picos_to_mclk(txp);
310 pre_pd_exit_mclk = act_pd_exit_mclk;
312 * MRS_CYC = max(tMRD, tMOD)
313 * tMRD = 8nCK, tMOD = max(24nCK, 15ns)
315 tmrd_mclk = max(24U, picos_to_mclk(15000));
316 #elif defined(CONFIG_SYS_FSL_DDR3)
317 unsigned int data_rate = get_ddr_freq(0);
320 * (tXARD and tXARDS). Empirical?
321 * The DDR3 spec has not tXARD,
322 * we use the tXP instead of it.
323 * tXP=max(3nCK, 7.5ns) for DDR3-800, 1066
324 * max(3nCK, 6ns) for DDR3-1333, 1600, 1866, 2133
325 * spec has not the tAXPD, we use
326 * tAXPD=1, need design to confirm.
328 txp = max((int)mclk_ps * 3, (mclk_ps > 1540 ? 7500 : 6000));
331 /* set the turnaround time */
334 * for single quad-rank DIMM and two dual-rank DIMMs
335 * to avoid ODT overlap
337 if (avoid_odt_overlap(dimm_params)) {
341 /* for faster clock, need more time for data setup */
342 trwt_mclk = (data_rate/1000000 > 1800) ? 2 : 1;
344 if ((data_rate/1000000 > 1150) || (popts->memctl_interleaving))
347 if (popts->dynamic_power == 0) { /* powerdown is not used */
348 act_pd_exit_mclk = 1;
349 pre_pd_exit_mclk = 1;
352 /* act_pd_exit_mclk = tXARD, see above */
353 act_pd_exit_mclk = picos_to_mclk(txp);
354 /* Mode register MR0[A12] is '1' - fast exit */
355 pre_pd_exit_mclk = act_pd_exit_mclk;
358 #else /* CONFIG_SYS_FSL_DDR2 */
360 * (tXARD and tXARDS). Empirical?
365 act_pd_exit_mclk = 2;
366 pre_pd_exit_mclk = 2;
371 if (popts->trwt_override)
372 trwt_mclk = popts->trwt;
374 ddr->timing_cfg_0 = (0
375 | ((trwt_mclk & 0x3) << 30) /* RWT */
376 | ((twrt_mclk & 0x3) << 28) /* WRT */
377 | ((trrt_mclk & 0x3) << 26) /* RRT */
378 | ((twwt_mclk & 0x3) << 24) /* WWT */
379 | ((act_pd_exit_mclk & 0xf) << 20) /* ACT_PD_EXIT */
380 | ((pre_pd_exit_mclk & 0xF) << 16) /* PRE_PD_EXIT */
381 | ((taxpd_mclk & 0xf) << 8) /* ODT_PD_EXIT */
382 | ((tmrd_mclk & 0x1f) << 0) /* MRS_CYC */
384 debug("FSLDDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
386 #endif /* defined(CONFIG_SYS_FSL_DDR2) */
388 /* DDR SDRAM Timing Configuration 3 (TIMING_CFG_3) */
389 static void set_timing_cfg_3(fsl_ddr_cfg_regs_t *ddr,
390 const memctl_options_t *popts,
391 const common_timing_params_t *common_dimm,
392 unsigned int cas_latency,
393 unsigned int additive_latency)
395 /* Extended precharge to activate interval (tRP) */
396 unsigned int ext_pretoact = 0;
397 /* Extended Activate to precharge interval (tRAS) */
398 unsigned int ext_acttopre = 0;
399 /* Extended activate to read/write interval (tRCD) */
400 unsigned int ext_acttorw = 0;
401 /* Extended refresh recovery time (tRFC) */
402 unsigned int ext_refrec;
403 /* Extended MCAS latency from READ cmd */
404 unsigned int ext_caslat = 0;
405 /* Extended additive latency */
406 unsigned int ext_add_lat = 0;
407 /* Extended last data to precharge interval (tWR) */
408 unsigned int ext_wrrec = 0;
410 unsigned int cntl_adj = 0;
412 ext_pretoact = picos_to_mclk(common_dimm->trp_ps) >> 4;
413 ext_acttopre = picos_to_mclk(common_dimm->tras_ps) >> 4;
414 ext_acttorw = picos_to_mclk(common_dimm->trcd_ps) >> 4;
415 ext_caslat = (2 * cas_latency - 1) >> 4;
416 ext_add_lat = additive_latency >> 4;
417 #ifdef CONFIG_SYS_FSL_DDR4
418 ext_refrec = (picos_to_mclk(common_dimm->trfc1_ps) - 8) >> 4;
420 ext_refrec = (picos_to_mclk(common_dimm->trfc_ps) - 8) >> 4;
421 /* ext_wrrec only deals with 16 clock and above, or 14 with OTF */
423 ext_wrrec = (picos_to_mclk(common_dimm->twr_ps) +
424 (popts->otf_burst_chop_en ? 2 : 0)) >> 4;
426 ddr->timing_cfg_3 = (0
427 | ((ext_pretoact & 0x1) << 28)
428 | ((ext_acttopre & 0x3) << 24)
429 | ((ext_acttorw & 0x1) << 22)
430 | ((ext_refrec & 0x1F) << 16)
431 | ((ext_caslat & 0x3) << 12)
432 | ((ext_add_lat & 0x1) << 10)
433 | ((ext_wrrec & 0x1) << 8)
434 | ((cntl_adj & 0x7) << 0)
436 debug("FSLDDR: timing_cfg_3 = 0x%08x\n", ddr->timing_cfg_3);
439 /* DDR SDRAM Timing Configuration 1 (TIMING_CFG_1) */
440 static void set_timing_cfg_1(fsl_ddr_cfg_regs_t *ddr,
441 const memctl_options_t *popts,
442 const common_timing_params_t *common_dimm,
443 unsigned int cas_latency)
445 /* Precharge-to-activate interval (tRP) */
446 unsigned char pretoact_mclk;
447 /* Activate to precharge interval (tRAS) */
448 unsigned char acttopre_mclk;
449 /* Activate to read/write interval (tRCD) */
450 unsigned char acttorw_mclk;
452 unsigned char caslat_ctrl;
453 /* Refresh recovery time (tRFC) ; trfc_low */
454 unsigned char refrec_ctrl;
455 /* Last data to precharge minimum interval (tWR) */
456 unsigned char wrrec_mclk;
457 /* Activate-to-activate interval (tRRD) */
458 unsigned char acttoact_mclk;
459 /* Last write data pair to read command issue interval (tWTR) */
460 unsigned char wrtord_mclk;
461 #ifdef CONFIG_SYS_FSL_DDR4
462 /* DDR4 supports 10, 12, 14, 16, 18, 20, 24 */
463 static const u8 wrrec_table[] = {
470 /* DDR_SDRAM_MODE doesn't support 9,11,13,15 */
471 static const u8 wrrec_table[] = {
472 1, 2, 3, 4, 5, 6, 7, 8, 10, 10, 12, 12, 14, 14, 0, 0};
475 pretoact_mclk = picos_to_mclk(common_dimm->trp_ps);
476 acttopre_mclk = picos_to_mclk(common_dimm->tras_ps);
477 acttorw_mclk = picos_to_mclk(common_dimm->trcd_ps);
480 * Translate CAS Latency to a DDR controller field value:
482 * CAS Lat DDR I DDR II Ctrl
483 * Clocks SPD Bit SPD Bit Value
484 * ------- ------- ------- -----
495 #if defined(CONFIG_SYS_FSL_DDR1)
496 caslat_ctrl = (cas_latency + 1) & 0x07;
497 #elif defined(CONFIG_SYS_FSL_DDR2)
498 caslat_ctrl = 2 * cas_latency - 1;
501 * if the CAS latency more than 8 cycle,
502 * we need set extend bit for it at
503 * TIMING_CFG_3[EXT_CASLAT]
505 if (fsl_ddr_get_version() <= 0x40400)
506 caslat_ctrl = 2 * cas_latency - 1;
508 caslat_ctrl = (cas_latency - 1) << 1;
511 #ifdef CONFIG_SYS_FSL_DDR4
512 refrec_ctrl = picos_to_mclk(common_dimm->trfc1_ps) - 8;
513 wrrec_mclk = picos_to_mclk(common_dimm->twr_ps);
514 acttoact_mclk = max(picos_to_mclk(common_dimm->trrds_ps), 4U);
515 wrtord_mclk = max(2U, picos_to_mclk(2500));
516 if ((wrrec_mclk < 1) || (wrrec_mclk > 24))
517 printf("Error: WRREC doesn't support %d clocks\n", wrrec_mclk);
519 wrrec_mclk = wrrec_table[wrrec_mclk - 1];
521 refrec_ctrl = picos_to_mclk(common_dimm->trfc_ps) - 8;
522 wrrec_mclk = picos_to_mclk(common_dimm->twr_ps);
523 acttoact_mclk = picos_to_mclk(common_dimm->trrd_ps);
524 wrtord_mclk = picos_to_mclk(common_dimm->twtr_ps);
525 if ((wrrec_mclk < 1) || (wrrec_mclk > 16))
526 printf("Error: WRREC doesn't support %d clocks\n", wrrec_mclk);
528 wrrec_mclk = wrrec_table[wrrec_mclk - 1];
530 if (popts->otf_burst_chop_en)
534 * JEDEC has min requirement for tRRD
536 #if defined(CONFIG_SYS_FSL_DDR3)
537 if (acttoact_mclk < 4)
541 * JEDEC has some min requirements for tWTR
543 #if defined(CONFIG_SYS_FSL_DDR2)
546 #elif defined(CONFIG_SYS_FSL_DDR3)
550 if (popts->otf_burst_chop_en)
553 ddr->timing_cfg_1 = (0
554 | ((pretoact_mclk & 0x0F) << 28)
555 | ((acttopre_mclk & 0x0F) << 24)
556 | ((acttorw_mclk & 0xF) << 20)
557 | ((caslat_ctrl & 0xF) << 16)
558 | ((refrec_ctrl & 0xF) << 12)
559 | ((wrrec_mclk & 0x0F) << 8)
560 | ((acttoact_mclk & 0x0F) << 4)
561 | ((wrtord_mclk & 0x0F) << 0)
563 debug("FSLDDR: timing_cfg_1 = 0x%08x\n", ddr->timing_cfg_1);
566 /* DDR SDRAM Timing Configuration 2 (TIMING_CFG_2) */
567 static void set_timing_cfg_2(fsl_ddr_cfg_regs_t *ddr,
568 const memctl_options_t *popts,
569 const common_timing_params_t *common_dimm,
570 unsigned int cas_latency,
571 unsigned int additive_latency)
573 /* Additive latency */
574 unsigned char add_lat_mclk;
575 /* CAS-to-preamble override */
578 unsigned char wr_lat;
579 /* Read to precharge (tRTP) */
580 unsigned char rd_to_pre;
581 /* Write command to write data strobe timing adjustment */
582 unsigned char wr_data_delay;
583 /* Minimum CKE pulse width (tCKE) */
584 unsigned char cke_pls;
585 /* Window for four activates (tFAW) */
586 unsigned short four_act;
587 #ifdef CONFIG_SYS_FSL_DDR3
588 const unsigned int mclk_ps = get_memory_clk_period_ps();
591 /* FIXME add check that this must be less than acttorw_mclk */
592 add_lat_mclk = additive_latency;
593 cpo = popts->cpo_override;
595 #if defined(CONFIG_SYS_FSL_DDR1)
597 * This is a lie. It should really be 1, but if it is
598 * set to 1, bits overlap into the old controller's
599 * otherwise unused ACSM field. If we leave it 0, then
600 * the HW will magically treat it as 1 for DDR 1. Oh Yea.
603 #elif defined(CONFIG_SYS_FSL_DDR2)
604 wr_lat = cas_latency - 1;
606 wr_lat = compute_cas_write_latency();
609 #ifdef CONFIG_SYS_FSL_DDR4
610 rd_to_pre = picos_to_mclk(7500);
612 rd_to_pre = picos_to_mclk(common_dimm->trtp_ps);
615 * JEDEC has some min requirements for tRTP
617 #if defined(CONFIG_SYS_FSL_DDR2)
620 #elif defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
624 if (popts->otf_burst_chop_en)
625 rd_to_pre += 2; /* according to UM */
627 wr_data_delay = popts->write_data_delay;
628 #ifdef CONFIG_SYS_FSL_DDR4
630 cke_pls = max(3U, picos_to_mclk(5000));
631 #elif defined(CONFIG_SYS_FSL_DDR3)
633 * cke pulse = max(3nCK, 7.5ns) for DDR3-800
634 * max(3nCK, 5.625ns) for DDR3-1066, 1333
635 * max(3nCK, 5ns) for DDR3-1600, 1866, 2133
637 cke_pls = max(3U, picos_to_mclk(mclk_ps > 1870 ? 7500 :
638 (mclk_ps > 1245 ? 5625 : 5000)));
640 cke_pls = FSL_DDR_MIN_TCKE_PULSE_WIDTH_DDR;
642 four_act = picos_to_mclk(popts->tfaw_window_four_activates_ps);
644 ddr->timing_cfg_2 = (0
645 | ((add_lat_mclk & 0xf) << 28)
646 | ((cpo & 0x1f) << 23)
647 | ((wr_lat & 0xf) << 19)
648 | ((wr_lat & 0x10) << 14)
649 | ((rd_to_pre & RD_TO_PRE_MASK) << RD_TO_PRE_SHIFT)
650 | ((wr_data_delay & WR_DATA_DELAY_MASK) << WR_DATA_DELAY_SHIFT)
651 | ((cke_pls & 0x7) << 6)
652 | ((four_act & 0x3f) << 0)
654 debug("FSLDDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2);
657 /* DDR SDRAM Register Control Word */
658 static void set_ddr_sdram_rcw(fsl_ddr_cfg_regs_t *ddr,
659 const memctl_options_t *popts,
660 const common_timing_params_t *common_dimm)
662 if (common_dimm->all_dimms_registered &&
663 !common_dimm->all_dimms_unbuffered) {
664 if (popts->rcw_override) {
665 ddr->ddr_sdram_rcw_1 = popts->rcw_1;
666 ddr->ddr_sdram_rcw_2 = popts->rcw_2;
668 ddr->ddr_sdram_rcw_1 =
669 common_dimm->rcw[0] << 28 | \
670 common_dimm->rcw[1] << 24 | \
671 common_dimm->rcw[2] << 20 | \
672 common_dimm->rcw[3] << 16 | \
673 common_dimm->rcw[4] << 12 | \
674 common_dimm->rcw[5] << 8 | \
675 common_dimm->rcw[6] << 4 | \
677 ddr->ddr_sdram_rcw_2 =
678 common_dimm->rcw[8] << 28 | \
679 common_dimm->rcw[9] << 24 | \
680 common_dimm->rcw[10] << 20 | \
681 common_dimm->rcw[11] << 16 | \
682 common_dimm->rcw[12] << 12 | \
683 common_dimm->rcw[13] << 8 | \
684 common_dimm->rcw[14] << 4 | \
685 common_dimm->rcw[15];
687 debug("FSLDDR: ddr_sdram_rcw_1 = 0x%08x\n", ddr->ddr_sdram_rcw_1);
688 debug("FSLDDR: ddr_sdram_rcw_2 = 0x%08x\n", ddr->ddr_sdram_rcw_2);
692 /* DDR SDRAM control configuration (DDR_SDRAM_CFG) */
693 static void set_ddr_sdram_cfg(fsl_ddr_cfg_regs_t *ddr,
694 const memctl_options_t *popts,
695 const common_timing_params_t *common_dimm)
697 unsigned int mem_en; /* DDR SDRAM interface logic enable */
698 unsigned int sren; /* Self refresh enable (during sleep) */
699 unsigned int ecc_en; /* ECC enable. */
700 unsigned int rd_en; /* Registered DIMM enable */
701 unsigned int sdram_type; /* Type of SDRAM */
702 unsigned int dyn_pwr; /* Dynamic power management mode */
703 unsigned int dbw; /* DRAM dta bus width */
704 unsigned int eight_be = 0; /* 8-beat burst enable, DDR2 is zero */
705 unsigned int ncap = 0; /* Non-concurrent auto-precharge */
706 unsigned int threet_en; /* Enable 3T timing */
707 unsigned int twot_en; /* Enable 2T timing */
708 unsigned int ba_intlv_ctl; /* Bank (CS) interleaving control */
709 unsigned int x32_en = 0; /* x32 enable */
710 unsigned int pchb8 = 0; /* precharge bit 8 enable */
711 unsigned int hse; /* Global half strength override */
712 unsigned int acc_ecc_en = 0; /* Accumulated ECC enable */
713 unsigned int mem_halt = 0; /* memory controller halt */
714 unsigned int bi = 0; /* Bypass initialization */
717 sren = popts->self_refresh_in_sleep;
718 if (common_dimm->all_dimms_ecc_capable) {
719 /* Allow setting of ECC only if all DIMMs are ECC. */
720 ecc_en = popts->ecc_mode;
725 if (common_dimm->all_dimms_registered &&
726 !common_dimm->all_dimms_unbuffered) {
731 twot_en = popts->twot_en;
734 sdram_type = CONFIG_FSL_SDRAM_TYPE;
736 dyn_pwr = popts->dynamic_power;
737 dbw = popts->data_bus_width;
738 /* 8-beat burst enable DDR-III case
739 * we must clear it when use the on-the-fly mode,
740 * must set it when use the 32-bits bus mode.
742 if ((sdram_type == SDRAM_TYPE_DDR3) ||
743 (sdram_type == SDRAM_TYPE_DDR4)) {
744 if (popts->burst_length == DDR_BL8)
746 if (popts->burst_length == DDR_OTF)
752 threet_en = popts->threet_en;
753 ba_intlv_ctl = popts->ba_intlv_ctl;
754 hse = popts->half_strength_driver_enable;
756 /* set when ddr bus width < 64 */
757 acc_ecc_en = (dbw != 0 && ecc_en == 1) ? 1 : 0;
759 ddr->ddr_sdram_cfg = (0
760 | ((mem_en & 0x1) << 31)
761 | ((sren & 0x1) << 30)
762 | ((ecc_en & 0x1) << 29)
763 | ((rd_en & 0x1) << 28)
764 | ((sdram_type & 0x7) << 24)
765 | ((dyn_pwr & 0x1) << 21)
766 | ((dbw & 0x3) << 19)
767 | ((eight_be & 0x1) << 18)
768 | ((ncap & 0x1) << 17)
769 | ((threet_en & 0x1) << 16)
770 | ((twot_en & 0x1) << 15)
771 | ((ba_intlv_ctl & 0x7F) << 8)
772 | ((x32_en & 0x1) << 5)
773 | ((pchb8 & 0x1) << 4)
775 | ((acc_ecc_en & 0x1) << 2)
776 | ((mem_halt & 0x1) << 1)
779 debug("FSLDDR: ddr_sdram_cfg = 0x%08x\n", ddr->ddr_sdram_cfg);
782 /* DDR SDRAM control configuration 2 (DDR_SDRAM_CFG_2) */
783 static void set_ddr_sdram_cfg_2(fsl_ddr_cfg_regs_t *ddr,
784 const memctl_options_t *popts,
785 const unsigned int unq_mrs_en)
787 unsigned int frc_sr = 0; /* Force self refresh */
788 unsigned int sr_ie = 0; /* Self-refresh interrupt enable */
789 unsigned int odt_cfg = 0; /* ODT configuration */
790 unsigned int num_pr; /* Number of posted refreshes */
791 unsigned int slow = 0; /* DDR will be run less than 1250 */
792 unsigned int x4_en = 0; /* x4 DRAM enable */
793 unsigned int obc_cfg; /* On-The-Fly Burst Chop Cfg */
794 unsigned int ap_en; /* Address Parity Enable */
795 unsigned int d_init; /* DRAM data initialization */
796 unsigned int rcw_en = 0; /* Register Control Word Enable */
797 unsigned int md_en = 0; /* Mirrored DIMM Enable */
798 unsigned int qd_en = 0; /* quad-rank DIMM Enable */
800 #ifndef CONFIG_SYS_FSL_DDR4
801 unsigned int dll_rst_dis = 1; /* DLL reset disable */
802 unsigned int dqs_cfg; /* DQS configuration */
804 dqs_cfg = popts->dqs_config;
806 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
807 if (popts->cs_local_opts[i].odt_rd_cfg
808 || popts->cs_local_opts[i].odt_wr_cfg) {
809 odt_cfg = SDRAM_CFG2_ODT_ONLY_READ;
814 num_pr = 1; /* Make this configurable */
818 * {TIMING_CFG_1[PRETOACT]
819 * + [DDR_SDRAM_CFG_2[NUM_PR]
820 * * ({EXT_REFREC || REFREC} + 8 + 2)]}
821 * << DDR_SDRAM_INTERVAL[REFINT]
823 #if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
824 obc_cfg = popts->otf_burst_chop_en;
829 #if (CONFIG_SYS_FSL_DDR_VER >= FSL_DDR_VER_4_7)
830 slow = get_ddr_freq(0) < 1249000000;
833 if (popts->registered_dimm_en) {
835 ap_en = popts->ap_en;
840 x4_en = popts->x4_en ? 1 : 0;
842 #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
843 /* Use the DDR controller to auto initialize memory. */
844 d_init = popts->ecc_init_using_memctl;
845 ddr->ddr_data_init = CONFIG_MEM_INIT_VALUE;
846 debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init);
848 /* Memory will be initialized via DMA, or not at all. */
852 #if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
853 md_en = popts->mirrored_dimm;
855 qd_en = popts->quad_rank_present ? 1 : 0;
856 ddr->ddr_sdram_cfg_2 = (0
857 | ((frc_sr & 0x1) << 31)
858 | ((sr_ie & 0x1) << 30)
859 #ifndef CONFIG_SYS_FSL_DDR4
860 | ((dll_rst_dis & 0x1) << 29)
861 | ((dqs_cfg & 0x3) << 26)
863 | ((odt_cfg & 0x3) << 21)
864 | ((num_pr & 0xf) << 12)
869 | ((obc_cfg & 0x1) << 6)
870 | ((ap_en & 0x1) << 5)
871 | ((d_init & 0x1) << 4)
872 | ((rcw_en & 0x1) << 2)
873 | ((md_en & 0x1) << 0)
875 debug("FSLDDR: ddr_sdram_cfg_2 = 0x%08x\n", ddr->ddr_sdram_cfg_2);
878 #ifdef CONFIG_SYS_FSL_DDR4
879 /* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
880 static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr,
881 const memctl_options_t *popts,
882 const common_timing_params_t *common_dimm,
883 const unsigned int unq_mrs_en)
885 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
886 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
888 unsigned int wr_crc = 0; /* Disable */
889 unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */
890 unsigned int srt = 0; /* self-refresh temerature, normal range */
891 unsigned int cwl = compute_cas_write_latency() - 9;
892 unsigned int mpr = 0; /* serial */
894 const unsigned int mclk_ps = get_memory_clk_period_ps();
896 if (popts->rtt_override)
897 rtt_wr = popts->rtt_wr_override_value;
899 rtt_wr = popts->cs_local_opts[0].odt_rtt_wr;
901 if (common_dimm->extended_op_srt)
902 srt = common_dimm->extended_op_srt;
905 | ((wr_crc & 0x1) << 12)
906 | ((rtt_wr & 0x3) << 9)
908 | ((cwl & 0x7) << 3));
912 else if (mclk_ps >= 833)
918 | ((mpr & 0x3) << 11)
919 | ((wc_lat & 0x3) << 9));
921 ddr->ddr_sdram_mode_2 = (0
922 | ((esdmode2 & 0xFFFF) << 16)
923 | ((esdmode3 & 0xFFFF) << 0)
925 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
927 if (unq_mrs_en) { /* unique mode registers are supported */
928 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
929 if (popts->rtt_override)
930 rtt_wr = popts->rtt_wr_override_value;
932 rtt_wr = popts->cs_local_opts[i].odt_rtt_wr;
934 esdmode2 &= 0xF9FF; /* clear bit 10, 9 */
935 esdmode2 |= (rtt_wr & 0x3) << 9;
938 ddr->ddr_sdram_mode_4 = (0
939 | ((esdmode2 & 0xFFFF) << 16)
940 | ((esdmode3 & 0xFFFF) << 0)
944 ddr->ddr_sdram_mode_6 = (0
945 | ((esdmode2 & 0xFFFF) << 16)
946 | ((esdmode3 & 0xFFFF) << 0)
950 ddr->ddr_sdram_mode_8 = (0
951 | ((esdmode2 & 0xFFFF) << 16)
952 | ((esdmode3 & 0xFFFF) << 0)
957 debug("FSLDDR: ddr_sdram_mode_4 = 0x%08x\n",
958 ddr->ddr_sdram_mode_4);
959 debug("FSLDDR: ddr_sdram_mode_6 = 0x%08x\n",
960 ddr->ddr_sdram_mode_6);
961 debug("FSLDDR: ddr_sdram_mode_8 = 0x%08x\n",
962 ddr->ddr_sdram_mode_8);
965 #elif defined(CONFIG_SYS_FSL_DDR3)
966 /* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
967 static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr,
968 const memctl_options_t *popts,
969 const common_timing_params_t *common_dimm,
970 const unsigned int unq_mrs_en)
972 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
973 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
975 unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */
976 unsigned int srt = 0; /* self-refresh temerature, normal range */
977 unsigned int asr = 0; /* auto self-refresh disable */
978 unsigned int cwl = compute_cas_write_latency() - 5;
979 unsigned int pasr = 0; /* partial array self refresh disable */
981 if (popts->rtt_override)
982 rtt_wr = popts->rtt_wr_override_value;
984 rtt_wr = popts->cs_local_opts[0].odt_rtt_wr;
986 if (common_dimm->extended_op_srt)
987 srt = common_dimm->extended_op_srt;
990 | ((rtt_wr & 0x3) << 9)
994 | ((pasr & 0x7) << 0));
995 ddr->ddr_sdram_mode_2 = (0
996 | ((esdmode2 & 0xFFFF) << 16)
997 | ((esdmode3 & 0xFFFF) << 0)
999 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
1001 if (unq_mrs_en) { /* unique mode registers are supported */
1002 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1003 if (popts->rtt_override)
1004 rtt_wr = popts->rtt_wr_override_value;
1006 rtt_wr = popts->cs_local_opts[i].odt_rtt_wr;
1008 esdmode2 &= 0xF9FF; /* clear bit 10, 9 */
1009 esdmode2 |= (rtt_wr & 0x3) << 9;
1012 ddr->ddr_sdram_mode_4 = (0
1013 | ((esdmode2 & 0xFFFF) << 16)
1014 | ((esdmode3 & 0xFFFF) << 0)
1018 ddr->ddr_sdram_mode_6 = (0
1019 | ((esdmode2 & 0xFFFF) << 16)
1020 | ((esdmode3 & 0xFFFF) << 0)
1024 ddr->ddr_sdram_mode_8 = (0
1025 | ((esdmode2 & 0xFFFF) << 16)
1026 | ((esdmode3 & 0xFFFF) << 0)
1031 debug("FSLDDR: ddr_sdram_mode_4 = 0x%08x\n",
1032 ddr->ddr_sdram_mode_4);
1033 debug("FSLDDR: ddr_sdram_mode_6 = 0x%08x\n",
1034 ddr->ddr_sdram_mode_6);
1035 debug("FSLDDR: ddr_sdram_mode_8 = 0x%08x\n",
1036 ddr->ddr_sdram_mode_8);
1040 #else /* for DDR2 and DDR1 */
1041 /* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
1042 static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr,
1043 const memctl_options_t *popts,
1044 const common_timing_params_t *common_dimm,
1045 const unsigned int unq_mrs_en)
1047 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
1048 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
1050 ddr->ddr_sdram_mode_2 = (0
1051 | ((esdmode2 & 0xFFFF) << 16)
1052 | ((esdmode3 & 0xFFFF) << 0)
1054 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
1058 #ifdef CONFIG_SYS_FSL_DDR4
1059 /* DDR SDRAM Mode configuration 9 (DDR_SDRAM_MODE_9) */
1060 static void set_ddr_sdram_mode_9(fsl_ddr_cfg_regs_t *ddr,
1061 const memctl_options_t *popts,
1062 const common_timing_params_t *common_dimm,
1063 const unsigned int unq_mrs_en)
1066 unsigned short esdmode4 = 0; /* Extended SDRAM mode 4 */
1067 unsigned short esdmode5; /* Extended SDRAM mode 5 */
1069 esdmode5 = 0x00000400; /* Data mask enabled */
1071 ddr->ddr_sdram_mode_9 = (0
1072 | ((esdmode4 & 0xffff) << 16)
1073 | ((esdmode5 & 0xffff) << 0)
1075 debug("FSLDDR: ddr_sdram_mode_9) = 0x%08x\n", ddr->ddr_sdram_mode_9);
1076 if (unq_mrs_en) { /* unique mode registers are supported */
1077 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1080 ddr->ddr_sdram_mode_11 = (0
1081 | ((esdmode4 & 0xFFFF) << 16)
1082 | ((esdmode5 & 0xFFFF) << 0)
1086 ddr->ddr_sdram_mode_13 = (0
1087 | ((esdmode4 & 0xFFFF) << 16)
1088 | ((esdmode5 & 0xFFFF) << 0)
1092 ddr->ddr_sdram_mode_15 = (0
1093 | ((esdmode4 & 0xFFFF) << 16)
1094 | ((esdmode5 & 0xFFFF) << 0)
1099 debug("FSLDDR: ddr_sdram_mode_11 = 0x%08x\n",
1100 ddr->ddr_sdram_mode_11);
1101 debug("FSLDDR: ddr_sdram_mode_13 = 0x%08x\n",
1102 ddr->ddr_sdram_mode_13);
1103 debug("FSLDDR: ddr_sdram_mode_15 = 0x%08x\n",
1104 ddr->ddr_sdram_mode_15);
1108 /* DDR SDRAM Mode configuration 10 (DDR_SDRAM_MODE_10) */
1109 static void set_ddr_sdram_mode_10(fsl_ddr_cfg_regs_t *ddr,
1110 const memctl_options_t *popts,
1111 const common_timing_params_t *common_dimm,
1112 const unsigned int unq_mrs_en)
1115 unsigned short esdmode6 = 0; /* Extended SDRAM mode 6 */
1116 unsigned short esdmode7 = 0; /* Extended SDRAM mode 7 */
1117 unsigned int tccdl_min = picos_to_mclk(common_dimm->tccdl_ps);
1119 esdmode6 = ((tccdl_min - 4) & 0x7) << 10;
1121 ddr->ddr_sdram_mode_10 = (0
1122 | ((esdmode6 & 0xffff) << 16)
1123 | ((esdmode7 & 0xffff) << 0)
1125 debug("FSLDDR: ddr_sdram_mode_10) = 0x%08x\n", ddr->ddr_sdram_mode_10);
1126 if (unq_mrs_en) { /* unique mode registers are supported */
1127 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1130 ddr->ddr_sdram_mode_12 = (0
1131 | ((esdmode6 & 0xFFFF) << 16)
1132 | ((esdmode7 & 0xFFFF) << 0)
1136 ddr->ddr_sdram_mode_14 = (0
1137 | ((esdmode6 & 0xFFFF) << 16)
1138 | ((esdmode7 & 0xFFFF) << 0)
1142 ddr->ddr_sdram_mode_16 = (0
1143 | ((esdmode6 & 0xFFFF) << 16)
1144 | ((esdmode7 & 0xFFFF) << 0)
1149 debug("FSLDDR: ddr_sdram_mode_12 = 0x%08x\n",
1150 ddr->ddr_sdram_mode_12);
1151 debug("FSLDDR: ddr_sdram_mode_14 = 0x%08x\n",
1152 ddr->ddr_sdram_mode_14);
1153 debug("FSLDDR: ddr_sdram_mode_16 = 0x%08x\n",
1154 ddr->ddr_sdram_mode_16);
1160 /* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */
1161 static void set_ddr_sdram_interval(fsl_ddr_cfg_regs_t *ddr,
1162 const memctl_options_t *popts,
1163 const common_timing_params_t *common_dimm)
1165 unsigned int refint; /* Refresh interval */
1166 unsigned int bstopre; /* Precharge interval */
1168 refint = picos_to_mclk(common_dimm->refresh_rate_ps);
1170 bstopre = popts->bstopre;
1172 /* refint field used 0x3FFF in earlier controllers */
1173 ddr->ddr_sdram_interval = (0
1174 | ((refint & 0xFFFF) << 16)
1175 | ((bstopre & 0x3FFF) << 0)
1177 debug("FSLDDR: ddr_sdram_interval = 0x%08x\n", ddr->ddr_sdram_interval);
1180 #ifdef CONFIG_SYS_FSL_DDR4
1181 /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
1182 static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
1183 const memctl_options_t *popts,
1184 const common_timing_params_t *common_dimm,
1185 unsigned int cas_latency,
1186 unsigned int additive_latency,
1187 const unsigned int unq_mrs_en)
1190 unsigned short esdmode; /* Extended SDRAM mode */
1191 unsigned short sdmode; /* SDRAM mode */
1193 /* Mode Register - MR1 */
1194 unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */
1195 unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */
1197 unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */
1198 unsigned int al = 0; /* Posted CAS# additive latency (AL) */
1199 unsigned int dic = 0; /* Output driver impedance, 40ohm */
1200 unsigned int dll_en = 1; /* DLL Enable 1=Enable (Normal),
1201 0=Disable (Test/Debug) */
1203 /* Mode Register - MR0 */
1204 unsigned int wr = 0; /* Write Recovery */
1205 unsigned int dll_rst; /* DLL Reset */
1206 unsigned int mode; /* Normal=0 or Test=1 */
1207 unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
1208 /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
1210 unsigned int bl; /* BL: Burst Length */
1212 unsigned int wr_mclk;
1213 /* DDR4 support WR 10, 12, 14, 16, 18, 20, 24 */
1214 static const u8 wr_table[] = {
1215 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6};
1216 /* DDR4 support CAS 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 22, 24 */
1217 static const u8 cas_latency_table[] = {
1218 0, 1, 2, 3, 4, 5, 6, 7, 8, 8,
1219 9, 9, 10, 10, 11, 11};
1221 if (popts->rtt_override)
1222 rtt = popts->rtt_override_value;
1224 rtt = popts->cs_local_opts[0].odt_rtt_norm;
1226 if (additive_latency == (cas_latency - 1))
1228 if (additive_latency == (cas_latency - 2))
1231 if (popts->quad_rank_present)
1232 dic = 1; /* output driver impedance 240/7 ohm */
1235 * The esdmode value will also be used for writing
1236 * MR1 during write leveling for DDR3, although the
1237 * bits specifically related to the write leveling
1238 * scheme will be handled automatically by the DDR
1239 * controller. so we set the wrlvl_en = 0 here.
1242 | ((qoff & 0x1) << 12)
1243 | ((tdqs_en & 0x1) << 11)
1244 | ((rtt & 0x7) << 8)
1245 | ((wrlvl_en & 0x1) << 7)
1247 | ((dic & 0x3) << 1) /* DIC field is split */
1248 | ((dll_en & 0x1) << 0)
1252 * DLL control for precharge PD
1253 * 0=slow exit DLL off (tXPDLL)
1254 * 1=fast exit DLL on (tXP)
1257 wr_mclk = picos_to_mclk(common_dimm->twr_ps);
1258 if (wr_mclk <= 24) {
1259 wr = wr_table[wr_mclk - 10];
1261 printf("Error: unsupported write recovery for mode register wr_mclk = %d\n",
1265 dll_rst = 0; /* dll no reset */
1266 mode = 0; /* normal mode */
1268 /* look up table to get the cas latency bits */
1269 if (cas_latency >= 9 && cas_latency <= 24)
1270 caslat = cas_latency_table[cas_latency - 9];
1272 printf("Error: unsupported cas latency for mode register\n");
1274 bt = 0; /* Nibble sequential */
1276 switch (popts->burst_length) {
1287 printf("Error: invalid burst length of %u specified. ",
1288 popts->burst_length);
1289 puts("Defaulting to on-the-fly BC4 or BL8 beats.\n");
1296 | ((dll_rst & 0x1) << 8)
1297 | ((mode & 0x1) << 7)
1298 | (((caslat >> 1) & 0x7) << 4)
1300 | ((caslat & 1) << 2)
1304 ddr->ddr_sdram_mode = (0
1305 | ((esdmode & 0xFFFF) << 16)
1306 | ((sdmode & 0xFFFF) << 0)
1309 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
1311 if (unq_mrs_en) { /* unique mode registers are supported */
1312 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1313 if (popts->rtt_override)
1314 rtt = popts->rtt_override_value;
1316 rtt = popts->cs_local_opts[i].odt_rtt_norm;
1318 esdmode &= 0xF8FF; /* clear bit 10,9,8 for rtt */
1319 esdmode |= (rtt & 0x7) << 8;
1322 ddr->ddr_sdram_mode_3 = (0
1323 | ((esdmode & 0xFFFF) << 16)
1324 | ((sdmode & 0xFFFF) << 0)
1328 ddr->ddr_sdram_mode_5 = (0
1329 | ((esdmode & 0xFFFF) << 16)
1330 | ((sdmode & 0xFFFF) << 0)
1334 ddr->ddr_sdram_mode_7 = (0
1335 | ((esdmode & 0xFFFF) << 16)
1336 | ((sdmode & 0xFFFF) << 0)
1341 debug("FSLDDR: ddr_sdram_mode_3 = 0x%08x\n",
1342 ddr->ddr_sdram_mode_3);
1343 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1344 ddr->ddr_sdram_mode_5);
1345 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1346 ddr->ddr_sdram_mode_5);
1350 #elif defined(CONFIG_SYS_FSL_DDR3)
1351 /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
1352 static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
1353 const memctl_options_t *popts,
1354 const common_timing_params_t *common_dimm,
1355 unsigned int cas_latency,
1356 unsigned int additive_latency,
1357 const unsigned int unq_mrs_en)
1360 unsigned short esdmode; /* Extended SDRAM mode */
1361 unsigned short sdmode; /* SDRAM mode */
1363 /* Mode Register - MR1 */
1364 unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */
1365 unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */
1367 unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */
1368 unsigned int al = 0; /* Posted CAS# additive latency (AL) */
1369 unsigned int dic = 0; /* Output driver impedance, 40ohm */
1370 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
1371 1=Disable (Test/Debug) */
1373 /* Mode Register - MR0 */
1374 unsigned int dll_on; /* DLL control for precharge PD, 0=off, 1=on */
1375 unsigned int wr = 0; /* Write Recovery */
1376 unsigned int dll_rst; /* DLL Reset */
1377 unsigned int mode; /* Normal=0 or Test=1 */
1378 unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
1379 /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
1381 unsigned int bl; /* BL: Burst Length */
1383 unsigned int wr_mclk;
1385 * DDR_SDRAM_MODE doesn't support 9,11,13,15
1386 * Please refer JEDEC Standard No. 79-3E for Mode Register MR0
1389 static const u8 wr_table[] = {1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 0, 0};
1391 if (popts->rtt_override)
1392 rtt = popts->rtt_override_value;
1394 rtt = popts->cs_local_opts[0].odt_rtt_norm;
1396 if (additive_latency == (cas_latency - 1))
1398 if (additive_latency == (cas_latency - 2))
1401 if (popts->quad_rank_present)
1402 dic = 1; /* output driver impedance 240/7 ohm */
1405 * The esdmode value will also be used for writing
1406 * MR1 during write leveling for DDR3, although the
1407 * bits specifically related to the write leveling
1408 * scheme will be handled automatically by the DDR
1409 * controller. so we set the wrlvl_en = 0 here.
1412 | ((qoff & 0x1) << 12)
1413 | ((tdqs_en & 0x1) << 11)
1414 | ((rtt & 0x4) << 7) /* rtt field is split */
1415 | ((wrlvl_en & 0x1) << 7)
1416 | ((rtt & 0x2) << 5) /* rtt field is split */
1417 | ((dic & 0x2) << 4) /* DIC field is split */
1419 | ((rtt & 0x1) << 2) /* rtt field is split */
1420 | ((dic & 0x1) << 1) /* DIC field is split */
1421 | ((dll_en & 0x1) << 0)
1425 * DLL control for precharge PD
1426 * 0=slow exit DLL off (tXPDLL)
1427 * 1=fast exit DLL on (tXP)
1431 wr_mclk = picos_to_mclk(common_dimm->twr_ps);
1432 if (wr_mclk <= 16) {
1433 wr = wr_table[wr_mclk - 5];
1435 printf("Error: unsupported write recovery for mode register "
1436 "wr_mclk = %d\n", wr_mclk);
1439 dll_rst = 0; /* dll no reset */
1440 mode = 0; /* normal mode */
1442 /* look up table to get the cas latency bits */
1443 if (cas_latency >= 5 && cas_latency <= 16) {
1444 unsigned char cas_latency_table[] = {
1450 0xc, /* 10 clocks */
1451 0xe, /* 11 clocks */
1452 0x1, /* 12 clocks */
1453 0x3, /* 13 clocks */
1454 0x5, /* 14 clocks */
1455 0x7, /* 15 clocks */
1456 0x9, /* 16 clocks */
1458 caslat = cas_latency_table[cas_latency - 5];
1460 printf("Error: unsupported cas latency for mode register\n");
1463 bt = 0; /* Nibble sequential */
1465 switch (popts->burst_length) {
1476 printf("Error: invalid burst length of %u specified. "
1477 " Defaulting to on-the-fly BC4 or BL8 beats.\n",
1478 popts->burst_length);
1484 | ((dll_on & 0x1) << 12)
1486 | ((dll_rst & 0x1) << 8)
1487 | ((mode & 0x1) << 7)
1488 | (((caslat >> 1) & 0x7) << 4)
1490 | ((caslat & 1) << 2)
1494 ddr->ddr_sdram_mode = (0
1495 | ((esdmode & 0xFFFF) << 16)
1496 | ((sdmode & 0xFFFF) << 0)
1499 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
1501 if (unq_mrs_en) { /* unique mode registers are supported */
1502 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1503 if (popts->rtt_override)
1504 rtt = popts->rtt_override_value;
1506 rtt = popts->cs_local_opts[i].odt_rtt_norm;
1508 esdmode &= 0xFDBB; /* clear bit 9,6,2 */
1510 | ((rtt & 0x4) << 7) /* rtt field is split */
1511 | ((rtt & 0x2) << 5) /* rtt field is split */
1512 | ((rtt & 0x1) << 2) /* rtt field is split */
1516 ddr->ddr_sdram_mode_3 = (0
1517 | ((esdmode & 0xFFFF) << 16)
1518 | ((sdmode & 0xFFFF) << 0)
1522 ddr->ddr_sdram_mode_5 = (0
1523 | ((esdmode & 0xFFFF) << 16)
1524 | ((sdmode & 0xFFFF) << 0)
1528 ddr->ddr_sdram_mode_7 = (0
1529 | ((esdmode & 0xFFFF) << 16)
1530 | ((sdmode & 0xFFFF) << 0)
1535 debug("FSLDDR: ddr_sdram_mode_3 = 0x%08x\n",
1536 ddr->ddr_sdram_mode_3);
1537 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1538 ddr->ddr_sdram_mode_5);
1539 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1540 ddr->ddr_sdram_mode_5);
1544 #else /* !CONFIG_SYS_FSL_DDR3 */
1546 /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
1547 static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
1548 const memctl_options_t *popts,
1549 const common_timing_params_t *common_dimm,
1550 unsigned int cas_latency,
1551 unsigned int additive_latency,
1552 const unsigned int unq_mrs_en)
1554 unsigned short esdmode; /* Extended SDRAM mode */
1555 unsigned short sdmode; /* SDRAM mode */
1558 * FIXME: This ought to be pre-calculated in a
1559 * technology-specific routine,
1560 * e.g. compute_DDR2_mode_register(), and then the
1561 * sdmode and esdmode passed in as part of common_dimm.
1564 /* Extended Mode Register */
1565 unsigned int mrs = 0; /* Mode Register Set */
1566 unsigned int outputs = 0; /* 0=Enabled, 1=Disabled */
1567 unsigned int rdqs_en = 0; /* RDQS Enable: 0=no, 1=yes */
1568 unsigned int dqs_en = 0; /* DQS# Enable: 0=enable, 1=disable */
1569 unsigned int ocd = 0; /* 0x0=OCD not supported,
1570 0x7=OCD default state */
1572 unsigned int al; /* Posted CAS# additive latency (AL) */
1573 unsigned int ods = 0; /* Output Drive Strength:
1574 0 = Full strength (18ohm)
1575 1 = Reduced strength (4ohm) */
1576 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
1577 1=Disable (Test/Debug) */
1579 /* Mode Register (MR) */
1580 unsigned int mr; /* Mode Register Definition */
1581 unsigned int pd; /* Power-Down Mode */
1582 unsigned int wr; /* Write Recovery */
1583 unsigned int dll_res; /* DLL Reset */
1584 unsigned int mode; /* Normal=0 or Test=1 */
1585 unsigned int caslat = 0;/* CAS# latency */
1586 /* BT: Burst Type (0=Sequential, 1=Interleaved) */
1588 unsigned int bl; /* BL: Burst Length */
1590 dqs_en = !popts->dqs_config;
1591 rtt = fsl_ddr_get_rtt();
1593 al = additive_latency;
1596 | ((mrs & 0x3) << 14)
1597 | ((outputs & 0x1) << 12)
1598 | ((rdqs_en & 0x1) << 11)
1599 | ((dqs_en & 0x1) << 10)
1600 | ((ocd & 0x7) << 7)
1601 | ((rtt & 0x2) << 5) /* rtt field is split */
1603 | ((rtt & 0x1) << 2) /* rtt field is split */
1604 | ((ods & 0x1) << 1)
1605 | ((dll_en & 0x1) << 0)
1608 mr = 0; /* FIXME: CHECKME */
1611 * 0 = Fast Exit (Normal)
1612 * 1 = Slow Exit (Low Power)
1616 #if defined(CONFIG_SYS_FSL_DDR1)
1617 wr = 0; /* Historical */
1618 #elif defined(CONFIG_SYS_FSL_DDR2)
1619 wr = picos_to_mclk(common_dimm->twr_ps);
1624 #if defined(CONFIG_SYS_FSL_DDR1)
1625 if (1 <= cas_latency && cas_latency <= 4) {
1626 unsigned char mode_caslat_table[4] = {
1627 0x5, /* 1.5 clocks */
1628 0x2, /* 2.0 clocks */
1629 0x6, /* 2.5 clocks */
1630 0x3 /* 3.0 clocks */
1632 caslat = mode_caslat_table[cas_latency - 1];
1634 printf("Warning: unknown cas_latency %d\n", cas_latency);
1636 #elif defined(CONFIG_SYS_FSL_DDR2)
1637 caslat = cas_latency;
1641 switch (popts->burst_length) {
1649 printf("Error: invalid burst length of %u specified. "
1650 " Defaulting to 4 beats.\n",
1651 popts->burst_length);
1657 | ((mr & 0x3) << 14)
1658 | ((pd & 0x1) << 12)
1660 | ((dll_res & 0x1) << 8)
1661 | ((mode & 0x1) << 7)
1662 | ((caslat & 0x7) << 4)
1667 ddr->ddr_sdram_mode = (0
1668 | ((esdmode & 0xFFFF) << 16)
1669 | ((sdmode & 0xFFFF) << 0)
1671 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
1675 /* DDR SDRAM Data Initialization (DDR_DATA_INIT) */
1676 static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr)
1678 unsigned int init_value; /* Initialization value */
1680 #ifdef CONFIG_MEM_INIT_VALUE
1681 init_value = CONFIG_MEM_INIT_VALUE;
1683 init_value = 0xDEADBEEF;
1685 ddr->ddr_data_init = init_value;
1689 * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL)
1690 * The old controller on the 8540/60 doesn't have this register.
1691 * Hope it's OK to set it (to 0) anyway.
1693 static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
1694 const memctl_options_t *popts)
1696 unsigned int clk_adjust; /* Clock adjust */
1698 clk_adjust = popts->clk_adjust;
1699 ddr->ddr_sdram_clk_cntl = (clk_adjust & 0xF) << 23;
1700 debug("FSLDDR: clk_cntl = 0x%08x\n", ddr->ddr_sdram_clk_cntl);
1703 /* DDR Initialization Address (DDR_INIT_ADDR) */
1704 static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
1706 unsigned int init_addr = 0; /* Initialization address */
1708 ddr->ddr_init_addr = init_addr;
1711 /* DDR Initialization Address (DDR_INIT_EXT_ADDR) */
1712 static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
1714 unsigned int uia = 0; /* Use initialization address */
1715 unsigned int init_ext_addr = 0; /* Initialization address */
1717 ddr->ddr_init_ext_addr = (0
1718 | ((uia & 0x1) << 31)
1719 | (init_ext_addr & 0xF)
1723 /* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */
1724 static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr,
1725 const memctl_options_t *popts)
1727 unsigned int rwt = 0; /* Read-to-write turnaround for same CS */
1728 unsigned int wrt = 0; /* Write-to-read turnaround for same CS */
1729 unsigned int rrt = 0; /* Read-to-read turnaround for same CS */
1730 unsigned int wwt = 0; /* Write-to-write turnaround for same CS */
1731 unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */
1733 #if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
1734 if (popts->burst_length == DDR_BL8) {
1735 /* We set BL/2 for fixed BL8 */
1736 rrt = 0; /* BL/2 clocks */
1737 wwt = 0; /* BL/2 clocks */
1739 /* We need to set BL/2 + 2 to BC4 and OTF */
1740 rrt = 2; /* BL/2 + 2 clocks */
1741 wwt = 2; /* BL/2 + 2 clocks */
1745 #ifdef CONFIG_SYS_FSL_DDR4
1746 dll_lock = 2; /* tDLLK = 1024 clocks */
1747 #elif defined(CONFIG_SYS_FSL_DDR3)
1748 dll_lock = 1; /* tDLLK = 512 clocks from spec */
1750 ddr->timing_cfg_4 = (0
1751 | ((rwt & 0xf) << 28)
1752 | ((wrt & 0xf) << 24)
1753 | ((rrt & 0xf) << 20)
1754 | ((wwt & 0xf) << 16)
1757 debug("FSLDDR: timing_cfg_4 = 0x%08x\n", ddr->timing_cfg_4);
1760 /* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */
1761 static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr, unsigned int cas_latency)
1763 unsigned int rodt_on = 0; /* Read to ODT on */
1764 unsigned int rodt_off = 0; /* Read to ODT off */
1765 unsigned int wodt_on = 0; /* Write to ODT on */
1766 unsigned int wodt_off = 0; /* Write to ODT off */
1768 #if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
1769 unsigned int wr_lat = ((ddr->timing_cfg_2 & 0x00780000) >> 19) +
1770 ((ddr->timing_cfg_2 & 0x00040000) >> 14);
1771 /* rodt_on = timing_cfg_1[caslat] - timing_cfg_2[wrlat] + 1 */
1772 if (cas_latency >= wr_lat)
1773 rodt_on = cas_latency - wr_lat + 1;
1774 rodt_off = 4; /* 4 clocks */
1775 wodt_on = 1; /* 1 clocks */
1776 wodt_off = 4; /* 4 clocks */
1779 ddr->timing_cfg_5 = (0
1780 | ((rodt_on & 0x1f) << 24)
1781 | ((rodt_off & 0x7) << 20)
1782 | ((wodt_on & 0x1f) << 12)
1783 | ((wodt_off & 0x7) << 8)
1785 debug("FSLDDR: timing_cfg_5 = 0x%08x\n", ddr->timing_cfg_5);
1788 #ifdef CONFIG_SYS_FSL_DDR4
1789 static void set_timing_cfg_6(fsl_ddr_cfg_regs_t *ddr)
1791 unsigned int hs_caslat = 0;
1792 unsigned int hs_wrlat = 0;
1793 unsigned int hs_wrrec = 0;
1794 unsigned int hs_clkadj = 0;
1795 unsigned int hs_wrlvl_start = 0;
1797 ddr->timing_cfg_6 = (0
1798 | ((hs_caslat & 0x1f) << 24)
1799 | ((hs_wrlat & 0x1f) << 19)
1800 | ((hs_wrrec & 0x1f) << 12)
1801 | ((hs_clkadj & 0x1f) << 6)
1802 | ((hs_wrlvl_start & 0x1f) << 0)
1804 debug("FSLDDR: timing_cfg_6 = 0x%08x\n", ddr->timing_cfg_6);
1807 static void set_timing_cfg_7(fsl_ddr_cfg_regs_t *ddr,
1808 const common_timing_params_t *common_dimm)
1810 unsigned int txpr, tcksre, tcksrx;
1811 unsigned int cke_rst, cksre, cksrx, par_lat, cs_to_cmd;
1813 txpr = max(5U, picos_to_mclk(common_dimm->trfc1_ps + 10000));
1814 tcksre = max(5U, picos_to_mclk(10000));
1815 tcksrx = max(5U, picos_to_mclk(10000));
1821 else if (txpr <= 256)
1823 else if (txpr <= 512)
1838 ddr->timing_cfg_7 = (0
1839 | ((cke_rst & 0x3) << 28)
1840 | ((cksre & 0xf) << 24)
1841 | ((cksrx & 0xf) << 20)
1842 | ((par_lat & 0xf) << 16)
1843 | ((cs_to_cmd & 0xf) << 4)
1845 debug("FSLDDR: timing_cfg_7 = 0x%08x\n", ddr->timing_cfg_7);
1848 static void set_timing_cfg_8(fsl_ddr_cfg_regs_t *ddr,
1849 const memctl_options_t *popts,
1850 const common_timing_params_t *common_dimm,
1851 unsigned int cas_latency)
1853 unsigned int rwt_bg, wrt_bg, rrt_bg, wwt_bg;
1854 unsigned int acttoact_bg, wrtord_bg, pre_all_rec;
1855 unsigned int tccdl = picos_to_mclk(common_dimm->tccdl_ps);
1856 unsigned int wr_lat = ((ddr->timing_cfg_2 & 0x00780000) >> 19) +
1857 ((ddr->timing_cfg_2 & 0x00040000) >> 14);
1859 rwt_bg = cas_latency + 2 + 4 - wr_lat;
1861 rwt_bg = tccdl - rwt_bg;
1865 wrt_bg = wr_lat + 4 + 1 - cas_latency;
1867 wrt_bg = tccdl - wrt_bg;
1871 if (popts->burst_length == DDR_BL8) {
1879 acttoact_bg = picos_to_mclk(common_dimm->trrdl_ps);
1880 wrtord_bg = max(4U, picos_to_mclk(7500));
1881 if (popts->otf_burst_chop_en)
1886 ddr->timing_cfg_8 = (0
1887 | ((rwt_bg & 0xf) << 28)
1888 | ((wrt_bg & 0xf) << 24)
1889 | ((rrt_bg & 0xf) << 20)
1890 | ((wwt_bg & 0xf) << 16)
1891 | ((acttoact_bg & 0xf) << 12)
1892 | ((wrtord_bg & 0xf) << 8)
1893 | ((pre_all_rec & 0x1f) << 0)
1896 debug("FSLDDR: timing_cfg_8 = 0x%08x\n", ddr->timing_cfg_8);
1899 static void set_timing_cfg_9(fsl_ddr_cfg_regs_t *ddr)
1901 ddr->timing_cfg_9 = 0;
1902 debug("FSLDDR: timing_cfg_9 = 0x%08x\n", ddr->timing_cfg_9);
1905 /* This function needs to be called after set_ddr_sdram_cfg() is called */
1906 static void set_ddr_dq_mapping(fsl_ddr_cfg_regs_t *ddr,
1907 const dimm_params_t *dimm_params)
1909 unsigned int acc_ecc_en = (ddr->ddr_sdram_cfg >> 2) & 0x1;
1911 ddr->dq_map_0 = ((dimm_params->dq_mapping[0] & 0x3F) << 26) |
1912 ((dimm_params->dq_mapping[1] & 0x3F) << 20) |
1913 ((dimm_params->dq_mapping[2] & 0x3F) << 14) |
1914 ((dimm_params->dq_mapping[3] & 0x3F) << 8) |
1915 ((dimm_params->dq_mapping[4] & 0x3F) << 2);
1917 ddr->dq_map_1 = ((dimm_params->dq_mapping[5] & 0x3F) << 26) |
1918 ((dimm_params->dq_mapping[6] & 0x3F) << 20) |
1919 ((dimm_params->dq_mapping[7] & 0x3F) << 14) |
1920 ((dimm_params->dq_mapping[10] & 0x3F) << 8) |
1921 ((dimm_params->dq_mapping[11] & 0x3F) << 2);
1923 ddr->dq_map_2 = ((dimm_params->dq_mapping[12] & 0x3F) << 26) |
1924 ((dimm_params->dq_mapping[13] & 0x3F) << 20) |
1925 ((dimm_params->dq_mapping[14] & 0x3F) << 14) |
1926 ((dimm_params->dq_mapping[15] & 0x3F) << 8) |
1927 ((dimm_params->dq_mapping[16] & 0x3F) << 2);
1929 /* dq_map for ECC[4:7] is set to 0 if accumulated ECC is enabled */
1930 ddr->dq_map_3 = ((dimm_params->dq_mapping[17] & 0x3F) << 26) |
1931 ((dimm_params->dq_mapping[8] & 0x3F) << 20) |
1933 (dimm_params->dq_mapping[9] & 0x3F) << 14) |
1934 dimm_params->dq_mapping_ors;
1936 debug("FSLDDR: dq_map_0 = 0x%08x\n", ddr->dq_map_0);
1937 debug("FSLDDR: dq_map_1 = 0x%08x\n", ddr->dq_map_1);
1938 debug("FSLDDR: dq_map_2 = 0x%08x\n", ddr->dq_map_2);
1939 debug("FSLDDR: dq_map_3 = 0x%08x\n", ddr->dq_map_3);
1941 static void set_ddr_sdram_cfg_3(fsl_ddr_cfg_regs_t *ddr,
1942 const memctl_options_t *popts)
1946 rd_pre = popts->quad_rank_present ? 1 : 0;
1948 ddr->ddr_sdram_cfg_3 = (rd_pre & 0x1) << 16;
1950 debug("FSLDDR: ddr_sdram_cfg_3 = 0x%08x\n", ddr->ddr_sdram_cfg_3);
1952 #endif /* CONFIG_SYS_FSL_DDR4 */
1954 /* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */
1955 static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int zq_en)
1957 unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */
1958 /* Normal Operation Full Calibration Time (tZQoper) */
1959 unsigned int zqoper = 0;
1960 /* Normal Operation Short Calibration Time (tZQCS) */
1961 unsigned int zqcs = 0;
1962 #ifdef CONFIG_SYS_FSL_DDR4
1963 unsigned int zqcs_init;
1967 #ifdef CONFIG_SYS_FSL_DDR4
1968 zqinit = 10; /* 1024 clocks */
1969 zqoper = 9; /* 512 clocks */
1970 zqcs = 7; /* 128 clocks */
1971 zqcs_init = 5; /* 1024 refresh sequences */
1973 zqinit = 9; /* 512 clocks */
1974 zqoper = 8; /* 256 clocks */
1975 zqcs = 6; /* 64 clocks */
1979 ddr->ddr_zq_cntl = (0
1980 | ((zq_en & 0x1) << 31)
1981 | ((zqinit & 0xF) << 24)
1982 | ((zqoper & 0xF) << 16)
1983 | ((zqcs & 0xF) << 8)
1984 #ifdef CONFIG_SYS_FSL_DDR4
1985 | ((zqcs_init & 0xF) << 0)
1988 debug("FSLDDR: zq_cntl = 0x%08x\n", ddr->ddr_zq_cntl);
1991 /* DDR Write Leveling Control (DDR_WRLVL_CNTL) */
1992 static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int wrlvl_en,
1993 const memctl_options_t *popts)
1996 * First DQS pulse rising edge after margining mode
1997 * is programmed (tWL_MRD)
1999 unsigned int wrlvl_mrd = 0;
2000 /* ODT delay after margining mode is programmed (tWL_ODTEN) */
2001 unsigned int wrlvl_odten = 0;
2002 /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */
2003 unsigned int wrlvl_dqsen = 0;
2004 /* WRLVL_SMPL: Write leveling sample time */
2005 unsigned int wrlvl_smpl = 0;
2006 /* WRLVL_WLR: Write leveling repeition time */
2007 unsigned int wrlvl_wlr = 0;
2008 /* WRLVL_START: Write leveling start time */
2009 unsigned int wrlvl_start = 0;
2011 /* suggest enable write leveling for DDR3 due to fly-by topology */
2013 /* tWL_MRD min = 40 nCK, we set it 64 */
2017 /* tWL_DQSEN min = 25 nCK, we set it 32 */
2020 * Write leveling sample time at least need 6 clocks
2021 * higher than tWLO to allow enough time for progagation
2022 * delay and sampling the prime data bits.
2026 * Write leveling repetition time
2027 * at least tWLO + 6 clocks clocks
2032 * Write leveling start time
2033 * The value use for the DQS_ADJUST for the first sample
2034 * when write leveling is enabled. It probably needs to be
2035 * overriden per platform.
2039 * Override the write leveling sample and start time
2040 * according to specific board
2042 if (popts->wrlvl_override) {
2043 wrlvl_smpl = popts->wrlvl_sample;
2044 wrlvl_start = popts->wrlvl_start;
2048 ddr->ddr_wrlvl_cntl = (0
2049 | ((wrlvl_en & 0x1) << 31)
2050 | ((wrlvl_mrd & 0x7) << 24)
2051 | ((wrlvl_odten & 0x7) << 20)
2052 | ((wrlvl_dqsen & 0x7) << 16)
2053 | ((wrlvl_smpl & 0xf) << 12)
2054 | ((wrlvl_wlr & 0x7) << 8)
2055 | ((wrlvl_start & 0x1F) << 0)
2057 debug("FSLDDR: wrlvl_cntl = 0x%08x\n", ddr->ddr_wrlvl_cntl);
2058 ddr->ddr_wrlvl_cntl_2 = popts->wrlvl_ctl_2;
2059 debug("FSLDDR: wrlvl_cntl_2 = 0x%08x\n", ddr->ddr_wrlvl_cntl_2);
2060 ddr->ddr_wrlvl_cntl_3 = popts->wrlvl_ctl_3;
2061 debug("FSLDDR: wrlvl_cntl_3 = 0x%08x\n", ddr->ddr_wrlvl_cntl_3);
2065 /* DDR Self Refresh Counter (DDR_SR_CNTR) */
2066 static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr, unsigned int sr_it)
2068 /* Self Refresh Idle Threshold */
2069 ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
2072 static void set_ddr_eor(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
2074 if (popts->addr_hash) {
2075 ddr->ddr_eor = 0x40000000; /* address hash enable */
2076 puts("Address hashing enabled.\n");
2080 static void set_ddr_cdr1(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
2082 ddr->ddr_cdr1 = popts->ddr_cdr1;
2083 debug("FSLDDR: ddr_cdr1 = 0x%08x\n", ddr->ddr_cdr1);
2086 static void set_ddr_cdr2(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
2088 ddr->ddr_cdr2 = popts->ddr_cdr2;
2089 debug("FSLDDR: ddr_cdr2 = 0x%08x\n", ddr->ddr_cdr2);
2093 check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
2095 unsigned int res = 0;
2098 * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are
2099 * not set at the same time.
2101 if (ddr->ddr_sdram_cfg & 0x10000000
2102 && ddr->ddr_sdram_cfg & 0x00008000) {
2103 printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
2104 " should not be set at the same time.\n");
2112 compute_fsl_memctl_config_regs(const memctl_options_t *popts,
2113 fsl_ddr_cfg_regs_t *ddr,
2114 const common_timing_params_t *common_dimm,
2115 const dimm_params_t *dimm_params,
2116 unsigned int dbw_cap_adj,
2117 unsigned int size_only)
2120 unsigned int cas_latency;
2121 unsigned int additive_latency;
2124 unsigned int wrlvl_en;
2125 unsigned int ip_rev = 0;
2126 unsigned int unq_mrs_en = 0;
2129 memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
2131 if (common_dimm == NULL) {
2132 printf("Error: subset DIMM params struct null pointer\n");
2137 * Process overrides first.
2139 * FIXME: somehow add dereated caslat to this
2141 cas_latency = (popts->cas_latency_override)
2142 ? popts->cas_latency_override_value
2143 : common_dimm->lowest_common_spd_caslat;
2145 additive_latency = (popts->additive_latency_override)
2146 ? popts->additive_latency_override_value
2147 : common_dimm->additive_latency;
2149 sr_it = (popts->auto_self_refresh_en)
2152 /* ZQ calibration */
2153 zq_en = (popts->zq_en) ? 1 : 0;
2154 /* write leveling */
2155 wrlvl_en = (popts->wrlvl_en) ? 1 : 0;
2157 /* Chip Select Memory Bounds (CSn_BNDS) */
2158 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
2159 unsigned long long ea, sa;
2160 unsigned int cs_per_dimm
2161 = CONFIG_CHIP_SELECTS_PER_CTRL / CONFIG_DIMM_SLOTS_PER_CTLR;
2162 unsigned int dimm_number
2164 unsigned long long rank_density
2165 = dimm_params[dimm_number].rank_density >> dbw_cap_adj;
2167 if (dimm_params[dimm_number].n_ranks == 0) {
2168 debug("Skipping setup of CS%u "
2169 "because n_ranks on DIMM %u is 0\n", i, dimm_number);
2172 if (popts->memctl_interleaving) {
2173 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
2174 case FSL_DDR_CS0_CS1_CS2_CS3:
2176 case FSL_DDR_CS0_CS1:
2177 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
2181 case FSL_DDR_CS2_CS3:
2187 sa = common_dimm->base_address;
2188 ea = sa + common_dimm->total_mem - 1;
2189 } else if (!popts->memctl_interleaving) {
2191 * If memory interleaving between controllers is NOT
2192 * enabled, the starting address for each memory
2193 * controller is distinct. However, because rank
2194 * interleaving is enabled, the starting and ending
2195 * addresses of the total memory on that memory
2196 * controller needs to be programmed into its
2197 * respective CS0_BNDS.
2199 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
2200 case FSL_DDR_CS0_CS1_CS2_CS3:
2201 sa = common_dimm->base_address;
2202 ea = sa + common_dimm->total_mem - 1;
2204 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
2205 if ((i >= 2) && (dimm_number == 0)) {
2206 sa = dimm_params[dimm_number].base_address +
2208 ea = sa + 2 * rank_density - 1;
2210 sa = dimm_params[dimm_number].base_address;
2211 ea = sa + 2 * rank_density - 1;
2214 case FSL_DDR_CS0_CS1:
2215 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
2216 sa = dimm_params[dimm_number].base_address;
2217 ea = sa + rank_density - 1;
2219 sa += (i % cs_per_dimm) * rank_density;
2220 ea += (i % cs_per_dimm) * rank_density;
2228 case FSL_DDR_CS2_CS3:
2229 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
2230 sa = dimm_params[dimm_number].base_address;
2231 ea = sa + rank_density - 1;
2233 sa += (i % cs_per_dimm) * rank_density;
2234 ea += (i % cs_per_dimm) * rank_density;
2240 ea += (rank_density >> dbw_cap_adj);
2242 default: /* No bank(chip-select) interleaving */
2243 sa = dimm_params[dimm_number].base_address;
2244 ea = sa + rank_density - 1;
2245 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
2246 sa += (i % cs_per_dimm) * rank_density;
2247 ea += (i % cs_per_dimm) * rank_density;
2260 ddr->cs[i].bnds = (0
2261 | ((sa & 0xffff) << 16) /* starting address */
2262 | ((ea & 0xffff) << 0) /* ending address */
2265 /* setting bnds to 0xffffffff for inactive CS */
2266 ddr->cs[i].bnds = 0xffffffff;
2269 debug("FSLDDR: cs[%d]_bnds = 0x%08x\n", i, ddr->cs[i].bnds);
2270 set_csn_config(dimm_number, i, ddr, popts, dimm_params);
2271 set_csn_config_2(i, ddr);
2275 * In the case we only need to compute the ddr sdram size, we only need
2276 * to set csn registers, so return from here.
2281 set_ddr_eor(ddr, popts);
2283 #if !defined(CONFIG_SYS_FSL_DDR1)
2284 set_timing_cfg_0(ddr, popts, dimm_params);
2287 set_timing_cfg_3(ddr, popts, common_dimm, cas_latency,
2289 set_timing_cfg_1(ddr, popts, common_dimm, cas_latency);
2290 set_timing_cfg_2(ddr, popts, common_dimm,
2291 cas_latency, additive_latency);
2293 set_ddr_cdr1(ddr, popts);
2294 set_ddr_cdr2(ddr, popts);
2295 set_ddr_sdram_cfg(ddr, popts, common_dimm);
2296 ip_rev = fsl_ddr_get_version();
2297 if (ip_rev > 0x40400)
2300 if ((ip_rev > 0x40700) && (popts->cswl_override != 0))
2301 ddr->debug[18] = popts->cswl_override;
2303 set_ddr_sdram_cfg_2(ddr, popts, unq_mrs_en);
2304 set_ddr_sdram_mode(ddr, popts, common_dimm,
2305 cas_latency, additive_latency, unq_mrs_en);
2306 set_ddr_sdram_mode_2(ddr, popts, common_dimm, unq_mrs_en);
2307 #ifdef CONFIG_SYS_FSL_DDR4
2308 set_ddr_sdram_mode_9(ddr, popts, common_dimm, unq_mrs_en);
2309 set_ddr_sdram_mode_10(ddr, popts, common_dimm, unq_mrs_en);
2311 set_ddr_sdram_interval(ddr, popts, common_dimm);
2312 set_ddr_data_init(ddr);
2313 set_ddr_sdram_clk_cntl(ddr, popts);
2314 set_ddr_init_addr(ddr);
2315 set_ddr_init_ext_addr(ddr);
2316 set_timing_cfg_4(ddr, popts);
2317 set_timing_cfg_5(ddr, cas_latency);
2318 #ifdef CONFIG_SYS_FSL_DDR4
2319 set_ddr_sdram_cfg_3(ddr, popts);
2320 set_timing_cfg_6(ddr);
2321 set_timing_cfg_7(ddr, common_dimm);
2322 set_timing_cfg_8(ddr, popts, common_dimm, cas_latency);
2323 set_timing_cfg_9(ddr);
2324 set_ddr_dq_mapping(ddr, dimm_params);
2327 set_ddr_zq_cntl(ddr, zq_en);
2328 set_ddr_wrlvl_cntl(ddr, wrlvl_en, popts);
2330 set_ddr_sr_cntr(ddr, sr_it);
2332 set_ddr_sdram_rcw(ddr, popts, common_dimm);
2334 #ifdef CONFIG_SYS_FSL_DDR_EMU
2335 /* disble DDR training for emulator */
2336 ddr->debug[2] = 0x00000400;
2337 ddr->debug[4] = 0xff800000;
2339 #ifdef CONFIG_SYS_FSL_ERRATUM_A004508
2340 if ((ip_rev >= 0x40000) && (ip_rev < 0x40400))
2341 ddr->debug[2] |= 0x00000200; /* set bit 22 */
2344 return check_fsl_memctl_config_regs(ddr);