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)
257 * Check DIMM configuration, return 2 if quad-rank or two dual-rank
258 * Return 1 if other two slots configuration. Return 0 if single slot.
260 static inline int avoid_odt_overlap(const dimm_params_t *dimm_params)
262 #if CONFIG_DIMM_SLOTS_PER_CTLR == 1
263 if (dimm_params[0].n_ranks == 4)
267 #if CONFIG_DIMM_SLOTS_PER_CTLR == 2
268 if ((dimm_params[0].n_ranks == 2) &&
269 (dimm_params[1].n_ranks == 2))
272 #ifdef CONFIG_FSL_DDR_FIRST_SLOT_QUAD_CAPABLE
273 if (dimm_params[0].n_ranks == 4)
277 if ((dimm_params[0].n_ranks != 0) &&
278 (dimm_params[2].n_ranks != 0))
285 * DDR SDRAM Timing Configuration 0 (TIMING_CFG_0)
287 * Avoid writing for DDR I. The new PQ38 DDR controller
288 * dreams up non-zero default values to be backwards compatible.
290 static void set_timing_cfg_0(fsl_ddr_cfg_regs_t *ddr,
291 const memctl_options_t *popts,
292 const dimm_params_t *dimm_params)
294 unsigned char trwt_mclk = 0; /* Read-to-write turnaround */
295 unsigned char twrt_mclk = 0; /* Write-to-read turnaround */
296 /* 7.5 ns on -3E; 0 means WL - CL + BL/2 + 1 */
297 unsigned char trrt_mclk = 0; /* Read-to-read turnaround */
298 unsigned char twwt_mclk = 0; /* Write-to-write turnaround */
300 /* Active powerdown exit timing (tXARD and tXARDS). */
301 unsigned char act_pd_exit_mclk;
302 /* Precharge powerdown exit timing (tXP). */
303 unsigned char pre_pd_exit_mclk;
304 /* ODT powerdown exit timing (tAXPD). */
305 unsigned char taxpd_mclk = 0;
306 /* Mode register set cycle time (tMRD). */
307 unsigned char tmrd_mclk;
308 #if defined(CONFIG_SYS_FSL_DDR4) || defined(CONFIG_SYS_FSL_DDR3)
309 const unsigned int mclk_ps = get_memory_clk_period_ps();
312 #ifdef CONFIG_SYS_FSL_DDR4
313 /* tXP=max(4nCK, 6ns) */
314 int txp = max((int)mclk_ps * 4, 6000); /* unit=ps */
317 act_pd_exit_mclk = picos_to_mclk(txp);
318 pre_pd_exit_mclk = act_pd_exit_mclk;
320 * MRS_CYC = max(tMRD, tMOD)
321 * tMRD = 8nCK, tMOD = max(24nCK, 15ns)
323 tmrd_mclk = max(24U, picos_to_mclk(15000));
324 #elif defined(CONFIG_SYS_FSL_DDR3)
325 unsigned int data_rate = get_ddr_freq(0);
330 * (tXARD and tXARDS). Empirical?
331 * The DDR3 spec has not tXARD,
332 * we use the tXP instead of it.
333 * tXP=max(3nCK, 7.5ns) for DDR3-800, 1066
334 * max(3nCK, 6ns) for DDR3-1333, 1600, 1866, 2133
335 * spec has not the tAXPD, we use
336 * tAXPD=1, need design to confirm.
338 txp = max((int)mclk_ps * 3, (mclk_ps > 1540 ? 7500 : 6000));
340 ip_rev = fsl_ddr_get_version();
341 if (ip_rev >= 0x40700) {
343 * MRS_CYC = max(tMRD, tMOD)
344 * tMRD = 4nCK (8nCK for RDIMM)
345 * tMOD = max(12nCK, 15ns)
347 tmrd_mclk = max((unsigned int)12, picos_to_mclk(15000));
351 * tMRD = 4nCK (8nCK for RDIMM)
353 if (popts->registered_dimm_en)
359 /* set the turnaround time */
362 * for single quad-rank DIMM and two-slot DIMMs
363 * to avoid ODT overlap
365 odt_overlap = avoid_odt_overlap(dimm_params);
366 switch (odt_overlap) {
379 /* for faster clock, need more time for data setup */
380 trwt_mclk = (data_rate/1000000 > 1800) ? 2 : 1;
382 if ((data_rate/1000000 > 1150) || (popts->memctl_interleaving))
385 if (popts->dynamic_power == 0) { /* powerdown is not used */
386 act_pd_exit_mclk = 1;
387 pre_pd_exit_mclk = 1;
390 /* act_pd_exit_mclk = tXARD, see above */
391 act_pd_exit_mclk = picos_to_mclk(txp);
392 /* Mode register MR0[A12] is '1' - fast exit */
393 pre_pd_exit_mclk = act_pd_exit_mclk;
396 #else /* CONFIG_SYS_FSL_DDR2 */
398 * (tXARD and tXARDS). Empirical?
403 act_pd_exit_mclk = 2;
404 pre_pd_exit_mclk = 2;
409 if (popts->trwt_override)
410 trwt_mclk = popts->trwt;
412 ddr->timing_cfg_0 = (0
413 | ((trwt_mclk & 0x3) << 30) /* RWT */
414 | ((twrt_mclk & 0x3) << 28) /* WRT */
415 | ((trrt_mclk & 0x3) << 26) /* RRT */
416 | ((twwt_mclk & 0x3) << 24) /* WWT */
417 | ((act_pd_exit_mclk & 0xf) << 20) /* ACT_PD_EXIT */
418 | ((pre_pd_exit_mclk & 0xF) << 16) /* PRE_PD_EXIT */
419 | ((taxpd_mclk & 0xf) << 8) /* ODT_PD_EXIT */
420 | ((tmrd_mclk & 0x1f) << 0) /* MRS_CYC */
422 debug("FSLDDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
424 #endif /* !defined(CONFIG_SYS_FSL_DDR1) */
426 /* DDR SDRAM Timing Configuration 3 (TIMING_CFG_3) */
427 static void set_timing_cfg_3(fsl_ddr_cfg_regs_t *ddr,
428 const memctl_options_t *popts,
429 const common_timing_params_t *common_dimm,
430 unsigned int cas_latency,
431 unsigned int additive_latency)
433 /* Extended precharge to activate interval (tRP) */
434 unsigned int ext_pretoact = 0;
435 /* Extended Activate to precharge interval (tRAS) */
436 unsigned int ext_acttopre = 0;
437 /* Extended activate to read/write interval (tRCD) */
438 unsigned int ext_acttorw = 0;
439 /* Extended refresh recovery time (tRFC) */
440 unsigned int ext_refrec;
441 /* Extended MCAS latency from READ cmd */
442 unsigned int ext_caslat = 0;
443 /* Extended additive latency */
444 unsigned int ext_add_lat = 0;
445 /* Extended last data to precharge interval (tWR) */
446 unsigned int ext_wrrec = 0;
448 unsigned int cntl_adj = 0;
450 ext_pretoact = picos_to_mclk(common_dimm->trp_ps) >> 4;
451 ext_acttopre = picos_to_mclk(common_dimm->tras_ps) >> 4;
452 ext_acttorw = picos_to_mclk(common_dimm->trcd_ps) >> 4;
453 ext_caslat = (2 * cas_latency - 1) >> 4;
454 ext_add_lat = additive_latency >> 4;
455 #ifdef CONFIG_SYS_FSL_DDR4
456 ext_refrec = (picos_to_mclk(common_dimm->trfc1_ps) - 8) >> 4;
458 ext_refrec = (picos_to_mclk(common_dimm->trfc_ps) - 8) >> 4;
459 /* ext_wrrec only deals with 16 clock and above, or 14 with OTF */
461 ext_wrrec = (picos_to_mclk(common_dimm->twr_ps) +
462 (popts->otf_burst_chop_en ? 2 : 0)) >> 4;
464 ddr->timing_cfg_3 = (0
465 | ((ext_pretoact & 0x1) << 28)
466 | ((ext_acttopre & 0x3) << 24)
467 | ((ext_acttorw & 0x1) << 22)
468 | ((ext_refrec & 0x1F) << 16)
469 | ((ext_caslat & 0x3) << 12)
470 | ((ext_add_lat & 0x1) << 10)
471 | ((ext_wrrec & 0x1) << 8)
472 | ((cntl_adj & 0x7) << 0)
474 debug("FSLDDR: timing_cfg_3 = 0x%08x\n", ddr->timing_cfg_3);
477 /* DDR SDRAM Timing Configuration 1 (TIMING_CFG_1) */
478 static void set_timing_cfg_1(fsl_ddr_cfg_regs_t *ddr,
479 const memctl_options_t *popts,
480 const common_timing_params_t *common_dimm,
481 unsigned int cas_latency)
483 /* Precharge-to-activate interval (tRP) */
484 unsigned char pretoact_mclk;
485 /* Activate to precharge interval (tRAS) */
486 unsigned char acttopre_mclk;
487 /* Activate to read/write interval (tRCD) */
488 unsigned char acttorw_mclk;
490 unsigned char caslat_ctrl;
491 /* Refresh recovery time (tRFC) ; trfc_low */
492 unsigned char refrec_ctrl;
493 /* Last data to precharge minimum interval (tWR) */
494 unsigned char wrrec_mclk;
495 /* Activate-to-activate interval (tRRD) */
496 unsigned char acttoact_mclk;
497 /* Last write data pair to read command issue interval (tWTR) */
498 unsigned char wrtord_mclk;
499 #ifdef CONFIG_SYS_FSL_DDR4
500 /* DDR4 supports 10, 12, 14, 16, 18, 20, 24 */
501 static const u8 wrrec_table[] = {
508 /* DDR_SDRAM_MODE doesn't support 9,11,13,15 */
509 static const u8 wrrec_table[] = {
510 1, 2, 3, 4, 5, 6, 7, 8, 10, 10, 12, 12, 14, 14, 0, 0};
513 pretoact_mclk = picos_to_mclk(common_dimm->trp_ps);
514 acttopre_mclk = picos_to_mclk(common_dimm->tras_ps);
515 acttorw_mclk = picos_to_mclk(common_dimm->trcd_ps);
518 * Translate CAS Latency to a DDR controller field value:
520 * CAS Lat DDR I DDR II Ctrl
521 * Clocks SPD Bit SPD Bit Value
522 * ------- ------- ------- -----
533 #if defined(CONFIG_SYS_FSL_DDR1)
534 caslat_ctrl = (cas_latency + 1) & 0x07;
535 #elif defined(CONFIG_SYS_FSL_DDR2)
536 caslat_ctrl = 2 * cas_latency - 1;
539 * if the CAS latency more than 8 cycle,
540 * we need set extend bit for it at
541 * TIMING_CFG_3[EXT_CASLAT]
543 if (fsl_ddr_get_version() <= 0x40400)
544 caslat_ctrl = 2 * cas_latency - 1;
546 caslat_ctrl = (cas_latency - 1) << 1;
549 #ifdef CONFIG_SYS_FSL_DDR4
550 refrec_ctrl = picos_to_mclk(common_dimm->trfc1_ps) - 8;
551 wrrec_mclk = picos_to_mclk(common_dimm->twr_ps);
552 acttoact_mclk = max(picos_to_mclk(common_dimm->trrds_ps), 4U);
553 wrtord_mclk = max(2U, picos_to_mclk(2500));
554 if ((wrrec_mclk < 1) || (wrrec_mclk > 24))
555 printf("Error: WRREC doesn't support %d clocks\n", wrrec_mclk);
557 wrrec_mclk = wrrec_table[wrrec_mclk - 1];
559 refrec_ctrl = picos_to_mclk(common_dimm->trfc_ps) - 8;
560 wrrec_mclk = picos_to_mclk(common_dimm->twr_ps);
561 acttoact_mclk = picos_to_mclk(common_dimm->trrd_ps);
562 wrtord_mclk = picos_to_mclk(common_dimm->twtr_ps);
563 if ((wrrec_mclk < 1) || (wrrec_mclk > 16))
564 printf("Error: WRREC doesn't support %d clocks\n", wrrec_mclk);
566 wrrec_mclk = wrrec_table[wrrec_mclk - 1];
568 if (popts->otf_burst_chop_en)
572 * JEDEC has min requirement for tRRD
574 #if defined(CONFIG_SYS_FSL_DDR3)
575 if (acttoact_mclk < 4)
579 * JEDEC has some min requirements for tWTR
581 #if defined(CONFIG_SYS_FSL_DDR2)
584 #elif defined(CONFIG_SYS_FSL_DDR3)
588 if (popts->otf_burst_chop_en)
591 ddr->timing_cfg_1 = (0
592 | ((pretoact_mclk & 0x0F) << 28)
593 | ((acttopre_mclk & 0x0F) << 24)
594 | ((acttorw_mclk & 0xF) << 20)
595 | ((caslat_ctrl & 0xF) << 16)
596 | ((refrec_ctrl & 0xF) << 12)
597 | ((wrrec_mclk & 0x0F) << 8)
598 | ((acttoact_mclk & 0x0F) << 4)
599 | ((wrtord_mclk & 0x0F) << 0)
601 debug("FSLDDR: timing_cfg_1 = 0x%08x\n", ddr->timing_cfg_1);
604 /* DDR SDRAM Timing Configuration 2 (TIMING_CFG_2) */
605 static void set_timing_cfg_2(fsl_ddr_cfg_regs_t *ddr,
606 const memctl_options_t *popts,
607 const common_timing_params_t *common_dimm,
608 unsigned int cas_latency,
609 unsigned int additive_latency)
611 /* Additive latency */
612 unsigned char add_lat_mclk;
613 /* CAS-to-preamble override */
616 unsigned char wr_lat;
617 /* Read to precharge (tRTP) */
618 unsigned char rd_to_pre;
619 /* Write command to write data strobe timing adjustment */
620 unsigned char wr_data_delay;
621 /* Minimum CKE pulse width (tCKE) */
622 unsigned char cke_pls;
623 /* Window for four activates (tFAW) */
624 unsigned short four_act;
625 #ifdef CONFIG_SYS_FSL_DDR3
626 const unsigned int mclk_ps = get_memory_clk_period_ps();
629 /* FIXME add check that this must be less than acttorw_mclk */
630 add_lat_mclk = additive_latency;
631 cpo = popts->cpo_override;
633 #if defined(CONFIG_SYS_FSL_DDR1)
635 * This is a lie. It should really be 1, but if it is
636 * set to 1, bits overlap into the old controller's
637 * otherwise unused ACSM field. If we leave it 0, then
638 * the HW will magically treat it as 1 for DDR 1. Oh Yea.
641 #elif defined(CONFIG_SYS_FSL_DDR2)
642 wr_lat = cas_latency - 1;
644 wr_lat = compute_cas_write_latency();
647 #ifdef CONFIG_SYS_FSL_DDR4
648 rd_to_pre = picos_to_mclk(7500);
650 rd_to_pre = picos_to_mclk(common_dimm->trtp_ps);
653 * JEDEC has some min requirements for tRTP
655 #if defined(CONFIG_SYS_FSL_DDR2)
658 #elif defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
662 if (popts->otf_burst_chop_en)
663 rd_to_pre += 2; /* according to UM */
665 wr_data_delay = popts->write_data_delay;
666 #ifdef CONFIG_SYS_FSL_DDR4
668 cke_pls = max(3U, picos_to_mclk(5000));
669 #elif defined(CONFIG_SYS_FSL_DDR3)
671 * cke pulse = max(3nCK, 7.5ns) for DDR3-800
672 * max(3nCK, 5.625ns) for DDR3-1066, 1333
673 * max(3nCK, 5ns) for DDR3-1600, 1866, 2133
675 cke_pls = max(3U, picos_to_mclk(mclk_ps > 1870 ? 7500 :
676 (mclk_ps > 1245 ? 5625 : 5000)));
678 cke_pls = FSL_DDR_MIN_TCKE_PULSE_WIDTH_DDR;
680 four_act = picos_to_mclk(popts->tfaw_window_four_activates_ps);
682 ddr->timing_cfg_2 = (0
683 | ((add_lat_mclk & 0xf) << 28)
684 | ((cpo & 0x1f) << 23)
685 | ((wr_lat & 0xf) << 19)
686 | ((wr_lat & 0x10) << 14)
687 | ((rd_to_pre & RD_TO_PRE_MASK) << RD_TO_PRE_SHIFT)
688 | ((wr_data_delay & WR_DATA_DELAY_MASK) << WR_DATA_DELAY_SHIFT)
689 | ((cke_pls & 0x7) << 6)
690 | ((four_act & 0x3f) << 0)
692 debug("FSLDDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2);
695 /* DDR SDRAM Register Control Word */
696 static void set_ddr_sdram_rcw(fsl_ddr_cfg_regs_t *ddr,
697 const memctl_options_t *popts,
698 const common_timing_params_t *common_dimm)
700 if (common_dimm->all_dimms_registered &&
701 !common_dimm->all_dimms_unbuffered) {
702 if (popts->rcw_override) {
703 ddr->ddr_sdram_rcw_1 = popts->rcw_1;
704 ddr->ddr_sdram_rcw_2 = popts->rcw_2;
706 ddr->ddr_sdram_rcw_1 =
707 common_dimm->rcw[0] << 28 | \
708 common_dimm->rcw[1] << 24 | \
709 common_dimm->rcw[2] << 20 | \
710 common_dimm->rcw[3] << 16 | \
711 common_dimm->rcw[4] << 12 | \
712 common_dimm->rcw[5] << 8 | \
713 common_dimm->rcw[6] << 4 | \
715 ddr->ddr_sdram_rcw_2 =
716 common_dimm->rcw[8] << 28 | \
717 common_dimm->rcw[9] << 24 | \
718 common_dimm->rcw[10] << 20 | \
719 common_dimm->rcw[11] << 16 | \
720 common_dimm->rcw[12] << 12 | \
721 common_dimm->rcw[13] << 8 | \
722 common_dimm->rcw[14] << 4 | \
723 common_dimm->rcw[15];
725 debug("FSLDDR: ddr_sdram_rcw_1 = 0x%08x\n", ddr->ddr_sdram_rcw_1);
726 debug("FSLDDR: ddr_sdram_rcw_2 = 0x%08x\n", ddr->ddr_sdram_rcw_2);
730 /* DDR SDRAM control configuration (DDR_SDRAM_CFG) */
731 static void set_ddr_sdram_cfg(fsl_ddr_cfg_regs_t *ddr,
732 const memctl_options_t *popts,
733 const common_timing_params_t *common_dimm)
735 unsigned int mem_en; /* DDR SDRAM interface logic enable */
736 unsigned int sren; /* Self refresh enable (during sleep) */
737 unsigned int ecc_en; /* ECC enable. */
738 unsigned int rd_en; /* Registered DIMM enable */
739 unsigned int sdram_type; /* Type of SDRAM */
740 unsigned int dyn_pwr; /* Dynamic power management mode */
741 unsigned int dbw; /* DRAM dta bus width */
742 unsigned int eight_be = 0; /* 8-beat burst enable, DDR2 is zero */
743 unsigned int ncap = 0; /* Non-concurrent auto-precharge */
744 unsigned int threet_en; /* Enable 3T timing */
745 unsigned int twot_en; /* Enable 2T timing */
746 unsigned int ba_intlv_ctl; /* Bank (CS) interleaving control */
747 unsigned int x32_en = 0; /* x32 enable */
748 unsigned int pchb8 = 0; /* precharge bit 8 enable */
749 unsigned int hse; /* Global half strength override */
750 unsigned int acc_ecc_en = 0; /* Accumulated ECC enable */
751 unsigned int mem_halt = 0; /* memory controller halt */
752 unsigned int bi = 0; /* Bypass initialization */
755 sren = popts->self_refresh_in_sleep;
756 if (common_dimm->all_dimms_ecc_capable) {
757 /* Allow setting of ECC only if all DIMMs are ECC. */
758 ecc_en = popts->ecc_mode;
763 if (common_dimm->all_dimms_registered &&
764 !common_dimm->all_dimms_unbuffered) {
769 twot_en = popts->twot_en;
772 sdram_type = CONFIG_FSL_SDRAM_TYPE;
774 dyn_pwr = popts->dynamic_power;
775 dbw = popts->data_bus_width;
776 /* 8-beat burst enable DDR-III case
777 * we must clear it when use the on-the-fly mode,
778 * must set it when use the 32-bits bus mode.
780 if ((sdram_type == SDRAM_TYPE_DDR3) ||
781 (sdram_type == SDRAM_TYPE_DDR4)) {
782 if (popts->burst_length == DDR_BL8)
784 if (popts->burst_length == DDR_OTF)
790 threet_en = popts->threet_en;
791 ba_intlv_ctl = popts->ba_intlv_ctl;
792 hse = popts->half_strength_driver_enable;
794 /* set when ddr bus width < 64 */
795 acc_ecc_en = (dbw != 0 && ecc_en == 1) ? 1 : 0;
797 ddr->ddr_sdram_cfg = (0
798 | ((mem_en & 0x1) << 31)
799 | ((sren & 0x1) << 30)
800 | ((ecc_en & 0x1) << 29)
801 | ((rd_en & 0x1) << 28)
802 | ((sdram_type & 0x7) << 24)
803 | ((dyn_pwr & 0x1) << 21)
804 | ((dbw & 0x3) << 19)
805 | ((eight_be & 0x1) << 18)
806 | ((ncap & 0x1) << 17)
807 | ((threet_en & 0x1) << 16)
808 | ((twot_en & 0x1) << 15)
809 | ((ba_intlv_ctl & 0x7F) << 8)
810 | ((x32_en & 0x1) << 5)
811 | ((pchb8 & 0x1) << 4)
813 | ((acc_ecc_en & 0x1) << 2)
814 | ((mem_halt & 0x1) << 1)
817 debug("FSLDDR: ddr_sdram_cfg = 0x%08x\n", ddr->ddr_sdram_cfg);
820 /* DDR SDRAM control configuration 2 (DDR_SDRAM_CFG_2) */
821 static void set_ddr_sdram_cfg_2(fsl_ddr_cfg_regs_t *ddr,
822 const memctl_options_t *popts,
823 const unsigned int unq_mrs_en)
825 unsigned int frc_sr = 0; /* Force self refresh */
826 unsigned int sr_ie = 0; /* Self-refresh interrupt enable */
827 unsigned int odt_cfg = 0; /* ODT configuration */
828 unsigned int num_pr; /* Number of posted refreshes */
829 unsigned int slow = 0; /* DDR will be run less than 1250 */
830 unsigned int x4_en = 0; /* x4 DRAM enable */
831 unsigned int obc_cfg; /* On-The-Fly Burst Chop Cfg */
832 unsigned int ap_en; /* Address Parity Enable */
833 unsigned int d_init; /* DRAM data initialization */
834 unsigned int rcw_en = 0; /* Register Control Word Enable */
835 unsigned int md_en = 0; /* Mirrored DIMM Enable */
836 unsigned int qd_en = 0; /* quad-rank DIMM Enable */
838 #ifndef CONFIG_SYS_FSL_DDR4
839 unsigned int dll_rst_dis = 1; /* DLL reset disable */
840 unsigned int dqs_cfg; /* DQS configuration */
842 dqs_cfg = popts->dqs_config;
844 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
845 if (popts->cs_local_opts[i].odt_rd_cfg
846 || popts->cs_local_opts[i].odt_wr_cfg) {
847 odt_cfg = SDRAM_CFG2_ODT_ONLY_READ;
852 num_pr = 1; /* Make this configurable */
856 * {TIMING_CFG_1[PRETOACT]
857 * + [DDR_SDRAM_CFG_2[NUM_PR]
858 * * ({EXT_REFREC || REFREC} + 8 + 2)]}
859 * << DDR_SDRAM_INTERVAL[REFINT]
861 #if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
862 obc_cfg = popts->otf_burst_chop_en;
867 #if (CONFIG_SYS_FSL_DDR_VER >= FSL_DDR_VER_4_7)
868 slow = get_ddr_freq(0) < 1249000000;
871 if (popts->registered_dimm_en) {
873 ap_en = popts->ap_en;
878 x4_en = popts->x4_en ? 1 : 0;
880 #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
881 /* Use the DDR controller to auto initialize memory. */
882 d_init = popts->ecc_init_using_memctl;
883 ddr->ddr_data_init = CONFIG_MEM_INIT_VALUE;
884 debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init);
886 /* Memory will be initialized via DMA, or not at all. */
890 #if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
891 md_en = popts->mirrored_dimm;
893 qd_en = popts->quad_rank_present ? 1 : 0;
894 ddr->ddr_sdram_cfg_2 = (0
895 | ((frc_sr & 0x1) << 31)
896 | ((sr_ie & 0x1) << 30)
897 #ifndef CONFIG_SYS_FSL_DDR4
898 | ((dll_rst_dis & 0x1) << 29)
899 | ((dqs_cfg & 0x3) << 26)
901 | ((odt_cfg & 0x3) << 21)
902 | ((num_pr & 0xf) << 12)
907 | ((obc_cfg & 0x1) << 6)
908 | ((ap_en & 0x1) << 5)
909 | ((d_init & 0x1) << 4)
910 | ((rcw_en & 0x1) << 2)
911 | ((md_en & 0x1) << 0)
913 debug("FSLDDR: ddr_sdram_cfg_2 = 0x%08x\n", ddr->ddr_sdram_cfg_2);
916 #ifdef CONFIG_SYS_FSL_DDR4
917 /* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
918 static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr,
919 const memctl_options_t *popts,
920 const common_timing_params_t *common_dimm,
921 const unsigned int unq_mrs_en)
923 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
924 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
926 unsigned int wr_crc = 0; /* Disable */
927 unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */
928 unsigned int srt = 0; /* self-refresh temerature, normal range */
929 unsigned int cwl = compute_cas_write_latency() - 9;
930 unsigned int mpr = 0; /* serial */
932 const unsigned int mclk_ps = get_memory_clk_period_ps();
934 if (popts->rtt_override)
935 rtt_wr = popts->rtt_wr_override_value;
937 rtt_wr = popts->cs_local_opts[0].odt_rtt_wr;
939 if (common_dimm->extended_op_srt)
940 srt = common_dimm->extended_op_srt;
943 | ((wr_crc & 0x1) << 12)
944 | ((rtt_wr & 0x3) << 9)
946 | ((cwl & 0x7) << 3));
950 else if (mclk_ps >= 833)
956 | ((mpr & 0x3) << 11)
957 | ((wc_lat & 0x3) << 9));
959 ddr->ddr_sdram_mode_2 = (0
960 | ((esdmode2 & 0xFFFF) << 16)
961 | ((esdmode3 & 0xFFFF) << 0)
963 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
965 if (unq_mrs_en) { /* unique mode registers are supported */
966 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
967 if (popts->rtt_override)
968 rtt_wr = popts->rtt_wr_override_value;
970 rtt_wr = popts->cs_local_opts[i].odt_rtt_wr;
972 esdmode2 &= 0xF9FF; /* clear bit 10, 9 */
973 esdmode2 |= (rtt_wr & 0x3) << 9;
976 ddr->ddr_sdram_mode_4 = (0
977 | ((esdmode2 & 0xFFFF) << 16)
978 | ((esdmode3 & 0xFFFF) << 0)
982 ddr->ddr_sdram_mode_6 = (0
983 | ((esdmode2 & 0xFFFF) << 16)
984 | ((esdmode3 & 0xFFFF) << 0)
988 ddr->ddr_sdram_mode_8 = (0
989 | ((esdmode2 & 0xFFFF) << 16)
990 | ((esdmode3 & 0xFFFF) << 0)
995 debug("FSLDDR: ddr_sdram_mode_4 = 0x%08x\n",
996 ddr->ddr_sdram_mode_4);
997 debug("FSLDDR: ddr_sdram_mode_6 = 0x%08x\n",
998 ddr->ddr_sdram_mode_6);
999 debug("FSLDDR: ddr_sdram_mode_8 = 0x%08x\n",
1000 ddr->ddr_sdram_mode_8);
1003 #elif defined(CONFIG_SYS_FSL_DDR3)
1004 /* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
1005 static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr,
1006 const memctl_options_t *popts,
1007 const common_timing_params_t *common_dimm,
1008 const unsigned int unq_mrs_en)
1010 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
1011 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
1013 unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */
1014 unsigned int srt = 0; /* self-refresh temerature, normal range */
1015 unsigned int asr = 0; /* auto self-refresh disable */
1016 unsigned int cwl = compute_cas_write_latency() - 5;
1017 unsigned int pasr = 0; /* partial array self refresh disable */
1019 if (popts->rtt_override)
1020 rtt_wr = popts->rtt_wr_override_value;
1022 rtt_wr = popts->cs_local_opts[0].odt_rtt_wr;
1024 if (common_dimm->extended_op_srt)
1025 srt = common_dimm->extended_op_srt;
1028 | ((rtt_wr & 0x3) << 9)
1029 | ((srt & 0x1) << 7)
1030 | ((asr & 0x1) << 6)
1031 | ((cwl & 0x7) << 3)
1032 | ((pasr & 0x7) << 0));
1033 ddr->ddr_sdram_mode_2 = (0
1034 | ((esdmode2 & 0xFFFF) << 16)
1035 | ((esdmode3 & 0xFFFF) << 0)
1037 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
1039 if (unq_mrs_en) { /* unique mode registers are supported */
1040 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1041 if (popts->rtt_override)
1042 rtt_wr = popts->rtt_wr_override_value;
1044 rtt_wr = popts->cs_local_opts[i].odt_rtt_wr;
1046 esdmode2 &= 0xF9FF; /* clear bit 10, 9 */
1047 esdmode2 |= (rtt_wr & 0x3) << 9;
1050 ddr->ddr_sdram_mode_4 = (0
1051 | ((esdmode2 & 0xFFFF) << 16)
1052 | ((esdmode3 & 0xFFFF) << 0)
1056 ddr->ddr_sdram_mode_6 = (0
1057 | ((esdmode2 & 0xFFFF) << 16)
1058 | ((esdmode3 & 0xFFFF) << 0)
1062 ddr->ddr_sdram_mode_8 = (0
1063 | ((esdmode2 & 0xFFFF) << 16)
1064 | ((esdmode3 & 0xFFFF) << 0)
1069 debug("FSLDDR: ddr_sdram_mode_4 = 0x%08x\n",
1070 ddr->ddr_sdram_mode_4);
1071 debug("FSLDDR: ddr_sdram_mode_6 = 0x%08x\n",
1072 ddr->ddr_sdram_mode_6);
1073 debug("FSLDDR: ddr_sdram_mode_8 = 0x%08x\n",
1074 ddr->ddr_sdram_mode_8);
1078 #else /* for DDR2 and DDR1 */
1079 /* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
1080 static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr,
1081 const memctl_options_t *popts,
1082 const common_timing_params_t *common_dimm,
1083 const unsigned int unq_mrs_en)
1085 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
1086 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
1088 ddr->ddr_sdram_mode_2 = (0
1089 | ((esdmode2 & 0xFFFF) << 16)
1090 | ((esdmode3 & 0xFFFF) << 0)
1092 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
1096 #ifdef CONFIG_SYS_FSL_DDR4
1097 /* DDR SDRAM Mode configuration 9 (DDR_SDRAM_MODE_9) */
1098 static void set_ddr_sdram_mode_9(fsl_ddr_cfg_regs_t *ddr,
1099 const memctl_options_t *popts,
1100 const common_timing_params_t *common_dimm,
1101 const unsigned int unq_mrs_en)
1104 unsigned short esdmode4 = 0; /* Extended SDRAM mode 4 */
1105 unsigned short esdmode5; /* Extended SDRAM mode 5 */
1107 esdmode5 = 0x00000400; /* Data mask enabled */
1109 ddr->ddr_sdram_mode_9 = (0
1110 | ((esdmode4 & 0xffff) << 16)
1111 | ((esdmode5 & 0xffff) << 0)
1113 debug("FSLDDR: ddr_sdram_mode_9) = 0x%08x\n", ddr->ddr_sdram_mode_9);
1114 if (unq_mrs_en) { /* unique mode registers are supported */
1115 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1118 ddr->ddr_sdram_mode_11 = (0
1119 | ((esdmode4 & 0xFFFF) << 16)
1120 | ((esdmode5 & 0xFFFF) << 0)
1124 ddr->ddr_sdram_mode_13 = (0
1125 | ((esdmode4 & 0xFFFF) << 16)
1126 | ((esdmode5 & 0xFFFF) << 0)
1130 ddr->ddr_sdram_mode_15 = (0
1131 | ((esdmode4 & 0xFFFF) << 16)
1132 | ((esdmode5 & 0xFFFF) << 0)
1137 debug("FSLDDR: ddr_sdram_mode_11 = 0x%08x\n",
1138 ddr->ddr_sdram_mode_11);
1139 debug("FSLDDR: ddr_sdram_mode_13 = 0x%08x\n",
1140 ddr->ddr_sdram_mode_13);
1141 debug("FSLDDR: ddr_sdram_mode_15 = 0x%08x\n",
1142 ddr->ddr_sdram_mode_15);
1146 /* DDR SDRAM Mode configuration 10 (DDR_SDRAM_MODE_10) */
1147 static void set_ddr_sdram_mode_10(fsl_ddr_cfg_regs_t *ddr,
1148 const memctl_options_t *popts,
1149 const common_timing_params_t *common_dimm,
1150 const unsigned int unq_mrs_en)
1153 unsigned short esdmode6 = 0; /* Extended SDRAM mode 6 */
1154 unsigned short esdmode7 = 0; /* Extended SDRAM mode 7 */
1155 unsigned int tccdl_min = picos_to_mclk(common_dimm->tccdl_ps);
1157 esdmode6 = ((tccdl_min - 4) & 0x7) << 10;
1159 ddr->ddr_sdram_mode_10 = (0
1160 | ((esdmode6 & 0xffff) << 16)
1161 | ((esdmode7 & 0xffff) << 0)
1163 debug("FSLDDR: ddr_sdram_mode_10) = 0x%08x\n", ddr->ddr_sdram_mode_10);
1164 if (unq_mrs_en) { /* unique mode registers are supported */
1165 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1168 ddr->ddr_sdram_mode_12 = (0
1169 | ((esdmode6 & 0xFFFF) << 16)
1170 | ((esdmode7 & 0xFFFF) << 0)
1174 ddr->ddr_sdram_mode_14 = (0
1175 | ((esdmode6 & 0xFFFF) << 16)
1176 | ((esdmode7 & 0xFFFF) << 0)
1180 ddr->ddr_sdram_mode_16 = (0
1181 | ((esdmode6 & 0xFFFF) << 16)
1182 | ((esdmode7 & 0xFFFF) << 0)
1187 debug("FSLDDR: ddr_sdram_mode_12 = 0x%08x\n",
1188 ddr->ddr_sdram_mode_12);
1189 debug("FSLDDR: ddr_sdram_mode_14 = 0x%08x\n",
1190 ddr->ddr_sdram_mode_14);
1191 debug("FSLDDR: ddr_sdram_mode_16 = 0x%08x\n",
1192 ddr->ddr_sdram_mode_16);
1198 /* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */
1199 static void set_ddr_sdram_interval(fsl_ddr_cfg_regs_t *ddr,
1200 const memctl_options_t *popts,
1201 const common_timing_params_t *common_dimm)
1203 unsigned int refint; /* Refresh interval */
1204 unsigned int bstopre; /* Precharge interval */
1206 refint = picos_to_mclk(common_dimm->refresh_rate_ps);
1208 bstopre = popts->bstopre;
1210 /* refint field used 0x3FFF in earlier controllers */
1211 ddr->ddr_sdram_interval = (0
1212 | ((refint & 0xFFFF) << 16)
1213 | ((bstopre & 0x3FFF) << 0)
1215 debug("FSLDDR: ddr_sdram_interval = 0x%08x\n", ddr->ddr_sdram_interval);
1218 #ifdef CONFIG_SYS_FSL_DDR4
1219 /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
1220 static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
1221 const memctl_options_t *popts,
1222 const common_timing_params_t *common_dimm,
1223 unsigned int cas_latency,
1224 unsigned int additive_latency,
1225 const unsigned int unq_mrs_en)
1228 unsigned short esdmode; /* Extended SDRAM mode */
1229 unsigned short sdmode; /* SDRAM mode */
1231 /* Mode Register - MR1 */
1232 unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */
1233 unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */
1235 unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */
1236 unsigned int al = 0; /* Posted CAS# additive latency (AL) */
1237 unsigned int dic = 0; /* Output driver impedance, 40ohm */
1238 unsigned int dll_en = 1; /* DLL Enable 1=Enable (Normal),
1239 0=Disable (Test/Debug) */
1241 /* Mode Register - MR0 */
1242 unsigned int wr = 0; /* Write Recovery */
1243 unsigned int dll_rst; /* DLL Reset */
1244 unsigned int mode; /* Normal=0 or Test=1 */
1245 unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
1246 /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
1248 unsigned int bl; /* BL: Burst Length */
1250 unsigned int wr_mclk;
1251 /* DDR4 support WR 10, 12, 14, 16, 18, 20, 24 */
1252 static const u8 wr_table[] = {
1253 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6};
1254 /* DDR4 support CAS 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 22, 24 */
1255 static const u8 cas_latency_table[] = {
1256 0, 1, 2, 3, 4, 5, 6, 7, 8, 8,
1257 9, 9, 10, 10, 11, 11};
1259 if (popts->rtt_override)
1260 rtt = popts->rtt_override_value;
1262 rtt = popts->cs_local_opts[0].odt_rtt_norm;
1264 if (additive_latency == (cas_latency - 1))
1266 if (additive_latency == (cas_latency - 2))
1269 if (popts->quad_rank_present)
1270 dic = 1; /* output driver impedance 240/7 ohm */
1273 * The esdmode value will also be used for writing
1274 * MR1 during write leveling for DDR3, although the
1275 * bits specifically related to the write leveling
1276 * scheme will be handled automatically by the DDR
1277 * controller. so we set the wrlvl_en = 0 here.
1280 | ((qoff & 0x1) << 12)
1281 | ((tdqs_en & 0x1) << 11)
1282 | ((rtt & 0x7) << 8)
1283 | ((wrlvl_en & 0x1) << 7)
1285 | ((dic & 0x3) << 1) /* DIC field is split */
1286 | ((dll_en & 0x1) << 0)
1290 * DLL control for precharge PD
1291 * 0=slow exit DLL off (tXPDLL)
1292 * 1=fast exit DLL on (tXP)
1295 wr_mclk = picos_to_mclk(common_dimm->twr_ps);
1296 if (wr_mclk <= 24) {
1297 wr = wr_table[wr_mclk - 10];
1299 printf("Error: unsupported write recovery for mode register wr_mclk = %d\n",
1303 dll_rst = 0; /* dll no reset */
1304 mode = 0; /* normal mode */
1306 /* look up table to get the cas latency bits */
1307 if (cas_latency >= 9 && cas_latency <= 24)
1308 caslat = cas_latency_table[cas_latency - 9];
1310 printf("Error: unsupported cas latency for mode register\n");
1312 bt = 0; /* Nibble sequential */
1314 switch (popts->burst_length) {
1325 printf("Error: invalid burst length of %u specified. ",
1326 popts->burst_length);
1327 puts("Defaulting to on-the-fly BC4 or BL8 beats.\n");
1334 | ((dll_rst & 0x1) << 8)
1335 | ((mode & 0x1) << 7)
1336 | (((caslat >> 1) & 0x7) << 4)
1338 | ((caslat & 1) << 2)
1342 ddr->ddr_sdram_mode = (0
1343 | ((esdmode & 0xFFFF) << 16)
1344 | ((sdmode & 0xFFFF) << 0)
1347 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
1349 if (unq_mrs_en) { /* unique mode registers are supported */
1350 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1351 if (popts->rtt_override)
1352 rtt = popts->rtt_override_value;
1354 rtt = popts->cs_local_opts[i].odt_rtt_norm;
1356 esdmode &= 0xF8FF; /* clear bit 10,9,8 for rtt */
1357 esdmode |= (rtt & 0x7) << 8;
1360 ddr->ddr_sdram_mode_3 = (0
1361 | ((esdmode & 0xFFFF) << 16)
1362 | ((sdmode & 0xFFFF) << 0)
1366 ddr->ddr_sdram_mode_5 = (0
1367 | ((esdmode & 0xFFFF) << 16)
1368 | ((sdmode & 0xFFFF) << 0)
1372 ddr->ddr_sdram_mode_7 = (0
1373 | ((esdmode & 0xFFFF) << 16)
1374 | ((sdmode & 0xFFFF) << 0)
1379 debug("FSLDDR: ddr_sdram_mode_3 = 0x%08x\n",
1380 ddr->ddr_sdram_mode_3);
1381 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1382 ddr->ddr_sdram_mode_5);
1383 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1384 ddr->ddr_sdram_mode_5);
1388 #elif defined(CONFIG_SYS_FSL_DDR3)
1389 /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
1390 static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
1391 const memctl_options_t *popts,
1392 const common_timing_params_t *common_dimm,
1393 unsigned int cas_latency,
1394 unsigned int additive_latency,
1395 const unsigned int unq_mrs_en)
1398 unsigned short esdmode; /* Extended SDRAM mode */
1399 unsigned short sdmode; /* SDRAM mode */
1401 /* Mode Register - MR1 */
1402 unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */
1403 unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */
1405 unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */
1406 unsigned int al = 0; /* Posted CAS# additive latency (AL) */
1407 unsigned int dic = 0; /* Output driver impedance, 40ohm */
1408 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
1409 1=Disable (Test/Debug) */
1411 /* Mode Register - MR0 */
1412 unsigned int dll_on; /* DLL control for precharge PD, 0=off, 1=on */
1413 unsigned int wr = 0; /* Write Recovery */
1414 unsigned int dll_rst; /* DLL Reset */
1415 unsigned int mode; /* Normal=0 or Test=1 */
1416 unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
1417 /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
1419 unsigned int bl; /* BL: Burst Length */
1421 unsigned int wr_mclk;
1423 * DDR_SDRAM_MODE doesn't support 9,11,13,15
1424 * Please refer JEDEC Standard No. 79-3E for Mode Register MR0
1427 static const u8 wr_table[] = {1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 0, 0};
1429 if (popts->rtt_override)
1430 rtt = popts->rtt_override_value;
1432 rtt = popts->cs_local_opts[0].odt_rtt_norm;
1434 if (additive_latency == (cas_latency - 1))
1436 if (additive_latency == (cas_latency - 2))
1439 if (popts->quad_rank_present)
1440 dic = 1; /* output driver impedance 240/7 ohm */
1443 * The esdmode value will also be used for writing
1444 * MR1 during write leveling for DDR3, although the
1445 * bits specifically related to the write leveling
1446 * scheme will be handled automatically by the DDR
1447 * controller. so we set the wrlvl_en = 0 here.
1450 | ((qoff & 0x1) << 12)
1451 | ((tdqs_en & 0x1) << 11)
1452 | ((rtt & 0x4) << 7) /* rtt field is split */
1453 | ((wrlvl_en & 0x1) << 7)
1454 | ((rtt & 0x2) << 5) /* rtt field is split */
1455 | ((dic & 0x2) << 4) /* DIC field is split */
1457 | ((rtt & 0x1) << 2) /* rtt field is split */
1458 | ((dic & 0x1) << 1) /* DIC field is split */
1459 | ((dll_en & 0x1) << 0)
1463 * DLL control for precharge PD
1464 * 0=slow exit DLL off (tXPDLL)
1465 * 1=fast exit DLL on (tXP)
1469 wr_mclk = picos_to_mclk(common_dimm->twr_ps);
1470 if (wr_mclk <= 16) {
1471 wr = wr_table[wr_mclk - 5];
1473 printf("Error: unsupported write recovery for mode register "
1474 "wr_mclk = %d\n", wr_mclk);
1477 dll_rst = 0; /* dll no reset */
1478 mode = 0; /* normal mode */
1480 /* look up table to get the cas latency bits */
1481 if (cas_latency >= 5 && cas_latency <= 16) {
1482 unsigned char cas_latency_table[] = {
1488 0xc, /* 10 clocks */
1489 0xe, /* 11 clocks */
1490 0x1, /* 12 clocks */
1491 0x3, /* 13 clocks */
1492 0x5, /* 14 clocks */
1493 0x7, /* 15 clocks */
1494 0x9, /* 16 clocks */
1496 caslat = cas_latency_table[cas_latency - 5];
1498 printf("Error: unsupported cas latency for mode register\n");
1501 bt = 0; /* Nibble sequential */
1503 switch (popts->burst_length) {
1514 printf("Error: invalid burst length of %u specified. "
1515 " Defaulting to on-the-fly BC4 or BL8 beats.\n",
1516 popts->burst_length);
1522 | ((dll_on & 0x1) << 12)
1524 | ((dll_rst & 0x1) << 8)
1525 | ((mode & 0x1) << 7)
1526 | (((caslat >> 1) & 0x7) << 4)
1528 | ((caslat & 1) << 2)
1532 ddr->ddr_sdram_mode = (0
1533 | ((esdmode & 0xFFFF) << 16)
1534 | ((sdmode & 0xFFFF) << 0)
1537 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
1539 if (unq_mrs_en) { /* unique mode registers are supported */
1540 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1541 if (popts->rtt_override)
1542 rtt = popts->rtt_override_value;
1544 rtt = popts->cs_local_opts[i].odt_rtt_norm;
1546 esdmode &= 0xFDBB; /* clear bit 9,6,2 */
1548 | ((rtt & 0x4) << 7) /* rtt field is split */
1549 | ((rtt & 0x2) << 5) /* rtt field is split */
1550 | ((rtt & 0x1) << 2) /* rtt field is split */
1554 ddr->ddr_sdram_mode_3 = (0
1555 | ((esdmode & 0xFFFF) << 16)
1556 | ((sdmode & 0xFFFF) << 0)
1560 ddr->ddr_sdram_mode_5 = (0
1561 | ((esdmode & 0xFFFF) << 16)
1562 | ((sdmode & 0xFFFF) << 0)
1566 ddr->ddr_sdram_mode_7 = (0
1567 | ((esdmode & 0xFFFF) << 16)
1568 | ((sdmode & 0xFFFF) << 0)
1573 debug("FSLDDR: ddr_sdram_mode_3 = 0x%08x\n",
1574 ddr->ddr_sdram_mode_3);
1575 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1576 ddr->ddr_sdram_mode_5);
1577 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1578 ddr->ddr_sdram_mode_5);
1582 #else /* !CONFIG_SYS_FSL_DDR3 */
1584 /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
1585 static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
1586 const memctl_options_t *popts,
1587 const common_timing_params_t *common_dimm,
1588 unsigned int cas_latency,
1589 unsigned int additive_latency,
1590 const unsigned int unq_mrs_en)
1592 unsigned short esdmode; /* Extended SDRAM mode */
1593 unsigned short sdmode; /* SDRAM mode */
1596 * FIXME: This ought to be pre-calculated in a
1597 * technology-specific routine,
1598 * e.g. compute_DDR2_mode_register(), and then the
1599 * sdmode and esdmode passed in as part of common_dimm.
1602 /* Extended Mode Register */
1603 unsigned int mrs = 0; /* Mode Register Set */
1604 unsigned int outputs = 0; /* 0=Enabled, 1=Disabled */
1605 unsigned int rdqs_en = 0; /* RDQS Enable: 0=no, 1=yes */
1606 unsigned int dqs_en = 0; /* DQS# Enable: 0=enable, 1=disable */
1607 unsigned int ocd = 0; /* 0x0=OCD not supported,
1608 0x7=OCD default state */
1610 unsigned int al; /* Posted CAS# additive latency (AL) */
1611 unsigned int ods = 0; /* Output Drive Strength:
1612 0 = Full strength (18ohm)
1613 1 = Reduced strength (4ohm) */
1614 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
1615 1=Disable (Test/Debug) */
1617 /* Mode Register (MR) */
1618 unsigned int mr; /* Mode Register Definition */
1619 unsigned int pd; /* Power-Down Mode */
1620 unsigned int wr; /* Write Recovery */
1621 unsigned int dll_res; /* DLL Reset */
1622 unsigned int mode; /* Normal=0 or Test=1 */
1623 unsigned int caslat = 0;/* CAS# latency */
1624 /* BT: Burst Type (0=Sequential, 1=Interleaved) */
1626 unsigned int bl; /* BL: Burst Length */
1628 dqs_en = !popts->dqs_config;
1629 rtt = fsl_ddr_get_rtt();
1631 al = additive_latency;
1634 | ((mrs & 0x3) << 14)
1635 | ((outputs & 0x1) << 12)
1636 | ((rdqs_en & 0x1) << 11)
1637 | ((dqs_en & 0x1) << 10)
1638 | ((ocd & 0x7) << 7)
1639 | ((rtt & 0x2) << 5) /* rtt field is split */
1641 | ((rtt & 0x1) << 2) /* rtt field is split */
1642 | ((ods & 0x1) << 1)
1643 | ((dll_en & 0x1) << 0)
1646 mr = 0; /* FIXME: CHECKME */
1649 * 0 = Fast Exit (Normal)
1650 * 1 = Slow Exit (Low Power)
1654 #if defined(CONFIG_SYS_FSL_DDR1)
1655 wr = 0; /* Historical */
1656 #elif defined(CONFIG_SYS_FSL_DDR2)
1657 wr = picos_to_mclk(common_dimm->twr_ps);
1662 #if defined(CONFIG_SYS_FSL_DDR1)
1663 if (1 <= cas_latency && cas_latency <= 4) {
1664 unsigned char mode_caslat_table[4] = {
1665 0x5, /* 1.5 clocks */
1666 0x2, /* 2.0 clocks */
1667 0x6, /* 2.5 clocks */
1668 0x3 /* 3.0 clocks */
1670 caslat = mode_caslat_table[cas_latency - 1];
1672 printf("Warning: unknown cas_latency %d\n", cas_latency);
1674 #elif defined(CONFIG_SYS_FSL_DDR2)
1675 caslat = cas_latency;
1679 switch (popts->burst_length) {
1687 printf("Error: invalid burst length of %u specified. "
1688 " Defaulting to 4 beats.\n",
1689 popts->burst_length);
1695 | ((mr & 0x3) << 14)
1696 | ((pd & 0x1) << 12)
1698 | ((dll_res & 0x1) << 8)
1699 | ((mode & 0x1) << 7)
1700 | ((caslat & 0x7) << 4)
1705 ddr->ddr_sdram_mode = (0
1706 | ((esdmode & 0xFFFF) << 16)
1707 | ((sdmode & 0xFFFF) << 0)
1709 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
1713 /* DDR SDRAM Data Initialization (DDR_DATA_INIT) */
1714 static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr)
1716 unsigned int init_value; /* Initialization value */
1718 #ifdef CONFIG_MEM_INIT_VALUE
1719 init_value = CONFIG_MEM_INIT_VALUE;
1721 init_value = 0xDEADBEEF;
1723 ddr->ddr_data_init = init_value;
1727 * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL)
1728 * The old controller on the 8540/60 doesn't have this register.
1729 * Hope it's OK to set it (to 0) anyway.
1731 static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
1732 const memctl_options_t *popts)
1734 unsigned int clk_adjust; /* Clock adjust */
1736 clk_adjust = popts->clk_adjust;
1737 ddr->ddr_sdram_clk_cntl = (clk_adjust & 0xF) << 23;
1738 debug("FSLDDR: clk_cntl = 0x%08x\n", ddr->ddr_sdram_clk_cntl);
1741 /* DDR Initialization Address (DDR_INIT_ADDR) */
1742 static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
1744 unsigned int init_addr = 0; /* Initialization address */
1746 ddr->ddr_init_addr = init_addr;
1749 /* DDR Initialization Address (DDR_INIT_EXT_ADDR) */
1750 static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
1752 unsigned int uia = 0; /* Use initialization address */
1753 unsigned int init_ext_addr = 0; /* Initialization address */
1755 ddr->ddr_init_ext_addr = (0
1756 | ((uia & 0x1) << 31)
1757 | (init_ext_addr & 0xF)
1761 /* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */
1762 static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr,
1763 const memctl_options_t *popts)
1765 unsigned int rwt = 0; /* Read-to-write turnaround for same CS */
1766 unsigned int wrt = 0; /* Write-to-read turnaround for same CS */
1767 unsigned int rrt = 0; /* Read-to-read turnaround for same CS */
1768 unsigned int wwt = 0; /* Write-to-write turnaround for same CS */
1769 unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */
1771 #if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
1772 if (popts->burst_length == DDR_BL8) {
1773 /* We set BL/2 for fixed BL8 */
1774 rrt = 0; /* BL/2 clocks */
1775 wwt = 0; /* BL/2 clocks */
1777 /* We need to set BL/2 + 2 to BC4 and OTF */
1778 rrt = 2; /* BL/2 + 2 clocks */
1779 wwt = 2; /* BL/2 + 2 clocks */
1783 #ifdef CONFIG_SYS_FSL_DDR4
1784 dll_lock = 2; /* tDLLK = 1024 clocks */
1785 #elif defined(CONFIG_SYS_FSL_DDR3)
1786 dll_lock = 1; /* tDLLK = 512 clocks from spec */
1788 ddr->timing_cfg_4 = (0
1789 | ((rwt & 0xf) << 28)
1790 | ((wrt & 0xf) << 24)
1791 | ((rrt & 0xf) << 20)
1792 | ((wwt & 0xf) << 16)
1795 debug("FSLDDR: timing_cfg_4 = 0x%08x\n", ddr->timing_cfg_4);
1798 /* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */
1799 static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr, unsigned int cas_latency)
1801 unsigned int rodt_on = 0; /* Read to ODT on */
1802 unsigned int rodt_off = 0; /* Read to ODT off */
1803 unsigned int wodt_on = 0; /* Write to ODT on */
1804 unsigned int wodt_off = 0; /* Write to ODT off */
1806 #if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
1807 unsigned int wr_lat = ((ddr->timing_cfg_2 & 0x00780000) >> 19) +
1808 ((ddr->timing_cfg_2 & 0x00040000) >> 14);
1809 /* rodt_on = timing_cfg_1[caslat] - timing_cfg_2[wrlat] + 1 */
1810 if (cas_latency >= wr_lat)
1811 rodt_on = cas_latency - wr_lat + 1;
1812 rodt_off = 4; /* 4 clocks */
1813 wodt_on = 1; /* 1 clocks */
1814 wodt_off = 4; /* 4 clocks */
1817 ddr->timing_cfg_5 = (0
1818 | ((rodt_on & 0x1f) << 24)
1819 | ((rodt_off & 0x7) << 20)
1820 | ((wodt_on & 0x1f) << 12)
1821 | ((wodt_off & 0x7) << 8)
1823 debug("FSLDDR: timing_cfg_5 = 0x%08x\n", ddr->timing_cfg_5);
1826 #ifdef CONFIG_SYS_FSL_DDR4
1827 static void set_timing_cfg_6(fsl_ddr_cfg_regs_t *ddr)
1829 unsigned int hs_caslat = 0;
1830 unsigned int hs_wrlat = 0;
1831 unsigned int hs_wrrec = 0;
1832 unsigned int hs_clkadj = 0;
1833 unsigned int hs_wrlvl_start = 0;
1835 ddr->timing_cfg_6 = (0
1836 | ((hs_caslat & 0x1f) << 24)
1837 | ((hs_wrlat & 0x1f) << 19)
1838 | ((hs_wrrec & 0x1f) << 12)
1839 | ((hs_clkadj & 0x1f) << 6)
1840 | ((hs_wrlvl_start & 0x1f) << 0)
1842 debug("FSLDDR: timing_cfg_6 = 0x%08x\n", ddr->timing_cfg_6);
1845 static void set_timing_cfg_7(fsl_ddr_cfg_regs_t *ddr,
1846 const common_timing_params_t *common_dimm)
1848 unsigned int txpr, tcksre, tcksrx;
1849 unsigned int cke_rst, cksre, cksrx, par_lat, cs_to_cmd;
1851 txpr = max(5U, picos_to_mclk(common_dimm->trfc1_ps + 10000));
1852 tcksre = max(5U, picos_to_mclk(10000));
1853 tcksrx = max(5U, picos_to_mclk(10000));
1859 else if (txpr <= 256)
1861 else if (txpr <= 512)
1876 ddr->timing_cfg_7 = (0
1877 | ((cke_rst & 0x3) << 28)
1878 | ((cksre & 0xf) << 24)
1879 | ((cksrx & 0xf) << 20)
1880 | ((par_lat & 0xf) << 16)
1881 | ((cs_to_cmd & 0xf) << 4)
1883 debug("FSLDDR: timing_cfg_7 = 0x%08x\n", ddr->timing_cfg_7);
1886 static void set_timing_cfg_8(fsl_ddr_cfg_regs_t *ddr,
1887 const memctl_options_t *popts,
1888 const common_timing_params_t *common_dimm,
1889 unsigned int cas_latency)
1891 unsigned int rwt_bg, wrt_bg, rrt_bg, wwt_bg;
1892 unsigned int acttoact_bg, wrtord_bg, pre_all_rec;
1893 unsigned int tccdl = picos_to_mclk(common_dimm->tccdl_ps);
1894 unsigned int wr_lat = ((ddr->timing_cfg_2 & 0x00780000) >> 19) +
1895 ((ddr->timing_cfg_2 & 0x00040000) >> 14);
1897 rwt_bg = cas_latency + 2 + 4 - wr_lat;
1899 rwt_bg = tccdl - rwt_bg;
1903 wrt_bg = wr_lat + 4 + 1 - cas_latency;
1905 wrt_bg = tccdl - wrt_bg;
1909 if (popts->burst_length == DDR_BL8) {
1917 acttoact_bg = picos_to_mclk(common_dimm->trrdl_ps);
1918 wrtord_bg = max(4U, picos_to_mclk(7500));
1919 if (popts->otf_burst_chop_en)
1924 ddr->timing_cfg_8 = (0
1925 | ((rwt_bg & 0xf) << 28)
1926 | ((wrt_bg & 0xf) << 24)
1927 | ((rrt_bg & 0xf) << 20)
1928 | ((wwt_bg & 0xf) << 16)
1929 | ((acttoact_bg & 0xf) << 12)
1930 | ((wrtord_bg & 0xf) << 8)
1931 | ((pre_all_rec & 0x1f) << 0)
1934 debug("FSLDDR: timing_cfg_8 = 0x%08x\n", ddr->timing_cfg_8);
1937 static void set_timing_cfg_9(fsl_ddr_cfg_regs_t *ddr)
1939 ddr->timing_cfg_9 = 0;
1940 debug("FSLDDR: timing_cfg_9 = 0x%08x\n", ddr->timing_cfg_9);
1943 /* This function needs to be called after set_ddr_sdram_cfg() is called */
1944 static void set_ddr_dq_mapping(fsl_ddr_cfg_regs_t *ddr,
1945 const dimm_params_t *dimm_params)
1947 unsigned int acc_ecc_en = (ddr->ddr_sdram_cfg >> 2) & 0x1;
1949 ddr->dq_map_0 = ((dimm_params->dq_mapping[0] & 0x3F) << 26) |
1950 ((dimm_params->dq_mapping[1] & 0x3F) << 20) |
1951 ((dimm_params->dq_mapping[2] & 0x3F) << 14) |
1952 ((dimm_params->dq_mapping[3] & 0x3F) << 8) |
1953 ((dimm_params->dq_mapping[4] & 0x3F) << 2);
1955 ddr->dq_map_1 = ((dimm_params->dq_mapping[5] & 0x3F) << 26) |
1956 ((dimm_params->dq_mapping[6] & 0x3F) << 20) |
1957 ((dimm_params->dq_mapping[7] & 0x3F) << 14) |
1958 ((dimm_params->dq_mapping[10] & 0x3F) << 8) |
1959 ((dimm_params->dq_mapping[11] & 0x3F) << 2);
1961 ddr->dq_map_2 = ((dimm_params->dq_mapping[12] & 0x3F) << 26) |
1962 ((dimm_params->dq_mapping[13] & 0x3F) << 20) |
1963 ((dimm_params->dq_mapping[14] & 0x3F) << 14) |
1964 ((dimm_params->dq_mapping[15] & 0x3F) << 8) |
1965 ((dimm_params->dq_mapping[16] & 0x3F) << 2);
1967 /* dq_map for ECC[4:7] is set to 0 if accumulated ECC is enabled */
1968 ddr->dq_map_3 = ((dimm_params->dq_mapping[17] & 0x3F) << 26) |
1969 ((dimm_params->dq_mapping[8] & 0x3F) << 20) |
1971 (dimm_params->dq_mapping[9] & 0x3F) << 14) |
1972 dimm_params->dq_mapping_ors;
1974 debug("FSLDDR: dq_map_0 = 0x%08x\n", ddr->dq_map_0);
1975 debug("FSLDDR: dq_map_1 = 0x%08x\n", ddr->dq_map_1);
1976 debug("FSLDDR: dq_map_2 = 0x%08x\n", ddr->dq_map_2);
1977 debug("FSLDDR: dq_map_3 = 0x%08x\n", ddr->dq_map_3);
1979 static void set_ddr_sdram_cfg_3(fsl_ddr_cfg_regs_t *ddr,
1980 const memctl_options_t *popts)
1984 rd_pre = popts->quad_rank_present ? 1 : 0;
1986 ddr->ddr_sdram_cfg_3 = (rd_pre & 0x1) << 16;
1988 debug("FSLDDR: ddr_sdram_cfg_3 = 0x%08x\n", ddr->ddr_sdram_cfg_3);
1990 #endif /* CONFIG_SYS_FSL_DDR4 */
1992 /* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */
1993 static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int zq_en)
1995 unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */
1996 /* Normal Operation Full Calibration Time (tZQoper) */
1997 unsigned int zqoper = 0;
1998 /* Normal Operation Short Calibration Time (tZQCS) */
1999 unsigned int zqcs = 0;
2000 #ifdef CONFIG_SYS_FSL_DDR4
2001 unsigned int zqcs_init;
2005 #ifdef CONFIG_SYS_FSL_DDR4
2006 zqinit = 10; /* 1024 clocks */
2007 zqoper = 9; /* 512 clocks */
2008 zqcs = 7; /* 128 clocks */
2009 zqcs_init = 5; /* 1024 refresh sequences */
2011 zqinit = 9; /* 512 clocks */
2012 zqoper = 8; /* 256 clocks */
2013 zqcs = 6; /* 64 clocks */
2017 ddr->ddr_zq_cntl = (0
2018 | ((zq_en & 0x1) << 31)
2019 | ((zqinit & 0xF) << 24)
2020 | ((zqoper & 0xF) << 16)
2021 | ((zqcs & 0xF) << 8)
2022 #ifdef CONFIG_SYS_FSL_DDR4
2023 | ((zqcs_init & 0xF) << 0)
2026 debug("FSLDDR: zq_cntl = 0x%08x\n", ddr->ddr_zq_cntl);
2029 /* DDR Write Leveling Control (DDR_WRLVL_CNTL) */
2030 static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int wrlvl_en,
2031 const memctl_options_t *popts)
2034 * First DQS pulse rising edge after margining mode
2035 * is programmed (tWL_MRD)
2037 unsigned int wrlvl_mrd = 0;
2038 /* ODT delay after margining mode is programmed (tWL_ODTEN) */
2039 unsigned int wrlvl_odten = 0;
2040 /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */
2041 unsigned int wrlvl_dqsen = 0;
2042 /* WRLVL_SMPL: Write leveling sample time */
2043 unsigned int wrlvl_smpl = 0;
2044 /* WRLVL_WLR: Write leveling repeition time */
2045 unsigned int wrlvl_wlr = 0;
2046 /* WRLVL_START: Write leveling start time */
2047 unsigned int wrlvl_start = 0;
2049 /* suggest enable write leveling for DDR3 due to fly-by topology */
2051 /* tWL_MRD min = 40 nCK, we set it 64 */
2055 /* tWL_DQSEN min = 25 nCK, we set it 32 */
2058 * Write leveling sample time at least need 6 clocks
2059 * higher than tWLO to allow enough time for progagation
2060 * delay and sampling the prime data bits.
2064 * Write leveling repetition time
2065 * at least tWLO + 6 clocks clocks
2070 * Write leveling start time
2071 * The value use for the DQS_ADJUST for the first sample
2072 * when write leveling is enabled. It probably needs to be
2073 * overriden per platform.
2077 * Override the write leveling sample and start time
2078 * according to specific board
2080 if (popts->wrlvl_override) {
2081 wrlvl_smpl = popts->wrlvl_sample;
2082 wrlvl_start = popts->wrlvl_start;
2086 ddr->ddr_wrlvl_cntl = (0
2087 | ((wrlvl_en & 0x1) << 31)
2088 | ((wrlvl_mrd & 0x7) << 24)
2089 | ((wrlvl_odten & 0x7) << 20)
2090 | ((wrlvl_dqsen & 0x7) << 16)
2091 | ((wrlvl_smpl & 0xf) << 12)
2092 | ((wrlvl_wlr & 0x7) << 8)
2093 | ((wrlvl_start & 0x1F) << 0)
2095 debug("FSLDDR: wrlvl_cntl = 0x%08x\n", ddr->ddr_wrlvl_cntl);
2096 ddr->ddr_wrlvl_cntl_2 = popts->wrlvl_ctl_2;
2097 debug("FSLDDR: wrlvl_cntl_2 = 0x%08x\n", ddr->ddr_wrlvl_cntl_2);
2098 ddr->ddr_wrlvl_cntl_3 = popts->wrlvl_ctl_3;
2099 debug("FSLDDR: wrlvl_cntl_3 = 0x%08x\n", ddr->ddr_wrlvl_cntl_3);
2103 /* DDR Self Refresh Counter (DDR_SR_CNTR) */
2104 static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr, unsigned int sr_it)
2106 /* Self Refresh Idle Threshold */
2107 ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
2110 static void set_ddr_eor(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
2112 if (popts->addr_hash) {
2113 ddr->ddr_eor = 0x40000000; /* address hash enable */
2114 puts("Address hashing enabled.\n");
2118 static void set_ddr_cdr1(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
2120 ddr->ddr_cdr1 = popts->ddr_cdr1;
2121 debug("FSLDDR: ddr_cdr1 = 0x%08x\n", ddr->ddr_cdr1);
2124 static void set_ddr_cdr2(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
2126 ddr->ddr_cdr2 = popts->ddr_cdr2;
2127 debug("FSLDDR: ddr_cdr2 = 0x%08x\n", ddr->ddr_cdr2);
2131 check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
2133 unsigned int res = 0;
2136 * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are
2137 * not set at the same time.
2139 if (ddr->ddr_sdram_cfg & 0x10000000
2140 && ddr->ddr_sdram_cfg & 0x00008000) {
2141 printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
2142 " should not be set at the same time.\n");
2150 compute_fsl_memctl_config_regs(const memctl_options_t *popts,
2151 fsl_ddr_cfg_regs_t *ddr,
2152 const common_timing_params_t *common_dimm,
2153 const dimm_params_t *dimm_params,
2154 unsigned int dbw_cap_adj,
2155 unsigned int size_only)
2158 unsigned int cas_latency;
2159 unsigned int additive_latency;
2162 unsigned int wrlvl_en;
2163 unsigned int ip_rev = 0;
2164 unsigned int unq_mrs_en = 0;
2167 memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
2169 if (common_dimm == NULL) {
2170 printf("Error: subset DIMM params struct null pointer\n");
2175 * Process overrides first.
2177 * FIXME: somehow add dereated caslat to this
2179 cas_latency = (popts->cas_latency_override)
2180 ? popts->cas_latency_override_value
2181 : common_dimm->lowest_common_spd_caslat;
2183 additive_latency = (popts->additive_latency_override)
2184 ? popts->additive_latency_override_value
2185 : common_dimm->additive_latency;
2187 sr_it = (popts->auto_self_refresh_en)
2190 /* ZQ calibration */
2191 zq_en = (popts->zq_en) ? 1 : 0;
2192 /* write leveling */
2193 wrlvl_en = (popts->wrlvl_en) ? 1 : 0;
2195 /* Chip Select Memory Bounds (CSn_BNDS) */
2196 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
2197 unsigned long long ea, sa;
2198 unsigned int cs_per_dimm
2199 = CONFIG_CHIP_SELECTS_PER_CTRL / CONFIG_DIMM_SLOTS_PER_CTLR;
2200 unsigned int dimm_number
2202 unsigned long long rank_density
2203 = dimm_params[dimm_number].rank_density >> dbw_cap_adj;
2205 if (dimm_params[dimm_number].n_ranks == 0) {
2206 debug("Skipping setup of CS%u "
2207 "because n_ranks on DIMM %u is 0\n", i, dimm_number);
2210 if (popts->memctl_interleaving) {
2211 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
2212 case FSL_DDR_CS0_CS1_CS2_CS3:
2214 case FSL_DDR_CS0_CS1:
2215 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
2219 case FSL_DDR_CS2_CS3:
2225 sa = common_dimm->base_address;
2226 ea = sa + common_dimm->total_mem - 1;
2227 } else if (!popts->memctl_interleaving) {
2229 * If memory interleaving between controllers is NOT
2230 * enabled, the starting address for each memory
2231 * controller is distinct. However, because rank
2232 * interleaving is enabled, the starting and ending
2233 * addresses of the total memory on that memory
2234 * controller needs to be programmed into its
2235 * respective CS0_BNDS.
2237 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
2238 case FSL_DDR_CS0_CS1_CS2_CS3:
2239 sa = common_dimm->base_address;
2240 ea = sa + common_dimm->total_mem - 1;
2242 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
2243 if ((i >= 2) && (dimm_number == 0)) {
2244 sa = dimm_params[dimm_number].base_address +
2246 ea = sa + 2 * rank_density - 1;
2248 sa = dimm_params[dimm_number].base_address;
2249 ea = sa + 2 * rank_density - 1;
2252 case FSL_DDR_CS0_CS1:
2253 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
2254 sa = dimm_params[dimm_number].base_address;
2255 ea = sa + rank_density - 1;
2257 sa += (i % cs_per_dimm) * rank_density;
2258 ea += (i % cs_per_dimm) * rank_density;
2266 case FSL_DDR_CS2_CS3:
2267 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
2268 sa = dimm_params[dimm_number].base_address;
2269 ea = sa + rank_density - 1;
2271 sa += (i % cs_per_dimm) * rank_density;
2272 ea += (i % cs_per_dimm) * rank_density;
2278 ea += (rank_density >> dbw_cap_adj);
2280 default: /* No bank(chip-select) interleaving */
2281 sa = dimm_params[dimm_number].base_address;
2282 ea = sa + rank_density - 1;
2283 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
2284 sa += (i % cs_per_dimm) * rank_density;
2285 ea += (i % cs_per_dimm) * rank_density;
2298 ddr->cs[i].bnds = (0
2299 | ((sa & 0xffff) << 16) /* starting address */
2300 | ((ea & 0xffff) << 0) /* ending address */
2303 /* setting bnds to 0xffffffff for inactive CS */
2304 ddr->cs[i].bnds = 0xffffffff;
2307 debug("FSLDDR: cs[%d]_bnds = 0x%08x\n", i, ddr->cs[i].bnds);
2308 set_csn_config(dimm_number, i, ddr, popts, dimm_params);
2309 set_csn_config_2(i, ddr);
2313 * In the case we only need to compute the ddr sdram size, we only need
2314 * to set csn registers, so return from here.
2319 set_ddr_eor(ddr, popts);
2321 #if !defined(CONFIG_SYS_FSL_DDR1)
2322 set_timing_cfg_0(ddr, popts, dimm_params);
2325 set_timing_cfg_3(ddr, popts, common_dimm, cas_latency,
2327 set_timing_cfg_1(ddr, popts, common_dimm, cas_latency);
2328 set_timing_cfg_2(ddr, popts, common_dimm,
2329 cas_latency, additive_latency);
2331 set_ddr_cdr1(ddr, popts);
2332 set_ddr_cdr2(ddr, popts);
2333 set_ddr_sdram_cfg(ddr, popts, common_dimm);
2334 ip_rev = fsl_ddr_get_version();
2335 if (ip_rev > 0x40400)
2338 if ((ip_rev > 0x40700) && (popts->cswl_override != 0))
2339 ddr->debug[18] = popts->cswl_override;
2341 set_ddr_sdram_cfg_2(ddr, popts, unq_mrs_en);
2342 set_ddr_sdram_mode(ddr, popts, common_dimm,
2343 cas_latency, additive_latency, unq_mrs_en);
2344 set_ddr_sdram_mode_2(ddr, popts, common_dimm, unq_mrs_en);
2345 #ifdef CONFIG_SYS_FSL_DDR4
2346 set_ddr_sdram_mode_9(ddr, popts, common_dimm, unq_mrs_en);
2347 set_ddr_sdram_mode_10(ddr, popts, common_dimm, unq_mrs_en);
2349 set_ddr_sdram_interval(ddr, popts, common_dimm);
2350 set_ddr_data_init(ddr);
2351 set_ddr_sdram_clk_cntl(ddr, popts);
2352 set_ddr_init_addr(ddr);
2353 set_ddr_init_ext_addr(ddr);
2354 set_timing_cfg_4(ddr, popts);
2355 set_timing_cfg_5(ddr, cas_latency);
2356 #ifdef CONFIG_SYS_FSL_DDR4
2357 set_ddr_sdram_cfg_3(ddr, popts);
2358 set_timing_cfg_6(ddr);
2359 set_timing_cfg_7(ddr, common_dimm);
2360 set_timing_cfg_8(ddr, popts, common_dimm, cas_latency);
2361 set_timing_cfg_9(ddr);
2362 set_ddr_dq_mapping(ddr, dimm_params);
2365 set_ddr_zq_cntl(ddr, zq_en);
2366 set_ddr_wrlvl_cntl(ddr, wrlvl_en, popts);
2368 set_ddr_sr_cntr(ddr, sr_it);
2370 set_ddr_sdram_rcw(ddr, popts, common_dimm);
2372 #ifdef CONFIG_SYS_FSL_DDR_EMU
2373 /* disble DDR training for emulator */
2374 ddr->debug[2] = 0x00000400;
2375 ddr->debug[4] = 0xff800000;
2377 #ifdef CONFIG_SYS_FSL_ERRATUM_A004508
2378 if ((ip_rev >= 0x40000) && (ip_rev < 0x40400))
2379 ddr->debug[2] |= 0x00000200; /* set bit 22 */
2382 return check_fsl_memctl_config_regs(ddr);