2 * Copyright 2008-2011 Freescale Semiconductor, Inc.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
11 * Generic driver for Freescale DDR/DDR2/DDR3 memory controller.
12 * Based on code from spd_sdram.c
13 * Author: James Yang [at freescale.com]
17 #include <asm/fsl_ddr_sdram.h>
22 #define _DDR_ADDR CONFIG_SYS_MPC85xx_DDR_ADDR
23 #elif defined(CONFIG_MPC86xx)
24 #define _DDR_ADDR CONFIG_SYS_MPC86xx_DDR_ADDR
26 #error "Undefined _DDR_ADDR"
29 u32 fsl_ddr_get_version(void)
32 u32 ver_major_minor_errata;
34 ddr = (void *)_DDR_ADDR;
35 ver_major_minor_errata = (in_be32(&ddr->ip_rev1) & 0xFFFF) << 8;
36 ver_major_minor_errata |= (in_be32(&ddr->ip_rev2) & 0xFF00) >> 8;
38 return ver_major_minor_errata;
41 unsigned int picos_to_mclk(unsigned int picos);
44 * Determine Rtt value.
46 * This should likely be either board or controller specific.
48 * Rtt(nominal) - DDR2:
53 * Rtt(nominal) - DDR3:
61 * FIXME: Apparently 8641 needs a value of 2
62 * FIXME: Old code seys if 667 MHz or higher, use 3 on 8572
64 * FIXME: There was some effort down this line earlier:
67 * for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL/2; i++) {
68 * if (popts->dimmslot[i].num_valid_cs
69 * && (popts->cs_local_opts[2*i].odt_rd_cfg
70 * || popts->cs_local_opts[2*i].odt_wr_cfg)) {
76 static inline int fsl_ddr_get_rtt(void)
80 #if defined(CONFIG_FSL_DDR1)
82 #elif defined(CONFIG_FSL_DDR2)
92 * compute the CAS write latency according to DDR3 spec
93 * CWL = 5 if tCK >= 2.5ns
94 * 6 if 2.5ns > tCK >= 1.875ns
95 * 7 if 1.875ns > tCK >= 1.5ns
96 * 8 if 1.5ns > tCK >= 1.25ns
98 static inline unsigned int compute_cas_write_latency(void)
101 const unsigned int mclk_ps = get_memory_clk_period_ps();
105 else if (mclk_ps >= 1875)
107 else if (mclk_ps >= 1500)
109 else if (mclk_ps >= 1250)
116 /* Chip Select Configuration (CSn_CONFIG) */
117 static void set_csn_config(int dimm_number, int i, fsl_ddr_cfg_regs_t *ddr,
118 const memctl_options_t *popts,
119 const dimm_params_t *dimm_params)
121 unsigned int cs_n_en = 0; /* Chip Select enable */
122 unsigned int intlv_en = 0; /* Memory controller interleave enable */
123 unsigned int intlv_ctl = 0; /* Interleaving control */
124 unsigned int ap_n_en = 0; /* Chip select n auto-precharge enable */
125 unsigned int odt_rd_cfg = 0; /* ODT for reads configuration */
126 unsigned int odt_wr_cfg = 0; /* ODT for writes configuration */
127 unsigned int ba_bits_cs_n = 0; /* Num of bank bits for SDRAM on CSn */
128 unsigned int row_bits_cs_n = 0; /* Num of row bits for SDRAM on CSn */
129 unsigned int col_bits_cs_n = 0; /* Num of ocl bits for SDRAM on CSn */
132 /* Compute CS_CONFIG only for existing ranks of each DIMM. */
135 if (dimm_params[dimm_number].n_ranks > 0) {
137 /* These fields only available in CS0_CONFIG */
138 intlv_en = popts->memctl_interleaving;
139 intlv_ctl = popts->memctl_interleaving_mode;
143 if ((dimm_number == 0 && dimm_params[0].n_ranks > 1) || \
144 (dimm_number == 1 && dimm_params[1].n_ranks > 0))
148 if ((dimm_number == 0 && dimm_params[0].n_ranks > 2) || \
149 (dimm_number > 1 && dimm_params[dimm_number].n_ranks > 0))
153 if ((dimm_number == 0 && dimm_params[0].n_ranks > 3) || \
154 (dimm_number == 1 && dimm_params[1].n_ranks > 1) || \
155 (dimm_number == 3 && dimm_params[3].n_ranks > 0))
162 unsigned int n_banks_per_sdram_device;
164 ap_n_en = popts->cs_local_opts[i].auto_precharge;
165 odt_rd_cfg = popts->cs_local_opts[i].odt_rd_cfg;
166 odt_wr_cfg = popts->cs_local_opts[i].odt_wr_cfg;
167 n_banks_per_sdram_device
168 = dimm_params[dimm_number].n_banks_per_sdram_device;
169 ba_bits_cs_n = __ilog2(n_banks_per_sdram_device) - 2;
170 row_bits_cs_n = dimm_params[dimm_number].n_row_addr - 12;
171 col_bits_cs_n = dimm_params[dimm_number].n_col_addr - 8;
173 ddr->cs[i].config = (0
174 | ((cs_n_en & 0x1) << 31)
175 | ((intlv_en & 0x3) << 29)
176 | ((intlv_ctl & 0xf) << 24)
177 | ((ap_n_en & 0x1) << 23)
179 /* XXX: some implementation only have 1 bit starting at left */
180 | ((odt_rd_cfg & 0x7) << 20)
182 /* XXX: Some implementation only have 1 bit starting at left */
183 | ((odt_wr_cfg & 0x7) << 16)
185 | ((ba_bits_cs_n & 0x3) << 14)
186 | ((row_bits_cs_n & 0x7) << 8)
187 | ((col_bits_cs_n & 0x7) << 0)
189 debug("FSLDDR: cs[%d]_config = 0x%08x\n", i,ddr->cs[i].config);
192 /* Chip Select Configuration 2 (CSn_CONFIG_2) */
194 static void set_csn_config_2(int i, fsl_ddr_cfg_regs_t *ddr)
196 unsigned int pasr_cfg = 0; /* Partial array self refresh config */
198 ddr->cs[i].config_2 = ((pasr_cfg & 7) << 24);
199 debug("FSLDDR: cs[%d]_config_2 = 0x%08x\n", i, ddr->cs[i].config_2);
202 /* -3E = 667 CL5, -25 = CL6 800, -25E = CL5 800 */
204 #if !defined(CONFIG_FSL_DDR1)
206 * DDR SDRAM Timing Configuration 0 (TIMING_CFG_0)
208 * Avoid writing for DDR I. The new PQ38 DDR controller
209 * dreams up non-zero default values to be backwards compatible.
211 static void set_timing_cfg_0(fsl_ddr_cfg_regs_t *ddr,
212 const memctl_options_t *popts)
214 unsigned char trwt_mclk = 0; /* Read-to-write turnaround */
215 unsigned char twrt_mclk = 0; /* Write-to-read turnaround */
216 /* 7.5 ns on -3E; 0 means WL - CL + BL/2 + 1 */
217 unsigned char trrt_mclk = 0; /* Read-to-read turnaround */
218 unsigned char twwt_mclk = 0; /* Write-to-write turnaround */
220 /* Active powerdown exit timing (tXARD and tXARDS). */
221 unsigned char act_pd_exit_mclk;
222 /* Precharge powerdown exit timing (tXP). */
223 unsigned char pre_pd_exit_mclk;
224 /* ODT powerdown exit timing (tAXPD). */
225 unsigned char taxpd_mclk;
226 /* Mode register set cycle time (tMRD). */
227 unsigned char tmrd_mclk;
229 #ifdef CONFIG_FSL_DDR3
231 * (tXARD and tXARDS). Empirical?
232 * The DDR3 spec has not tXARD,
233 * we use the tXP instead of it.
234 * tXP=max(3nCK, 7.5ns) for DDR3.
235 * spec has not the tAXPD, we use
236 * tAXPD=1, need design to confirm.
238 int tXP = max((get_memory_clk_period_ps() * 3), 7500); /* unit=ps */
239 unsigned int data_rate = fsl_ddr_get_mem_data_rate();
241 /* set the turnaround time */
243 if ((data_rate/1000000 > 1150) || (popts->memctl_interleaving))
246 if (popts->dynamic_power == 0) { /* powerdown is not used */
247 act_pd_exit_mclk = 1;
248 pre_pd_exit_mclk = 1;
251 /* act_pd_exit_mclk = tXARD, see above */
252 act_pd_exit_mclk = picos_to_mclk(tXP);
253 /* Mode register MR0[A12] is '1' - fast exit */
254 pre_pd_exit_mclk = act_pd_exit_mclk;
257 #else /* CONFIG_FSL_DDR2 */
259 * (tXARD and tXARDS). Empirical?
264 act_pd_exit_mclk = 2;
265 pre_pd_exit_mclk = 2;
270 ddr->timing_cfg_0 = (0
271 | ((trwt_mclk & 0x3) << 30) /* RWT */
272 | ((twrt_mclk & 0x3) << 28) /* WRT */
273 | ((trrt_mclk & 0x3) << 26) /* RRT */
274 | ((twwt_mclk & 0x3) << 24) /* WWT */
275 | ((act_pd_exit_mclk & 0x7) << 20) /* ACT_PD_EXIT */
276 | ((pre_pd_exit_mclk & 0xF) << 16) /* PRE_PD_EXIT */
277 | ((taxpd_mclk & 0xf) << 8) /* ODT_PD_EXIT */
278 | ((tmrd_mclk & 0xf) << 0) /* MRS_CYC */
280 debug("FSLDDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
282 #endif /* defined(CONFIG_FSL_DDR2) */
284 /* DDR SDRAM Timing Configuration 3 (TIMING_CFG_3) */
285 static void set_timing_cfg_3(fsl_ddr_cfg_regs_t *ddr,
286 const common_timing_params_t *common_dimm,
287 unsigned int cas_latency)
289 /* Extended Activate to precharge interval (tRAS) */
290 unsigned int ext_acttopre = 0;
291 unsigned int ext_refrec; /* Extended refresh recovery time (tRFC) */
292 unsigned int ext_caslat = 0; /* Extended MCAS latency from READ cmd */
293 unsigned int cntl_adj = 0; /* Control Adjust */
295 /* If the tRAS > 19 MCLK, we use the ext mode */
296 if (picos_to_mclk(common_dimm->tRAS_ps) > 0x13)
299 ext_refrec = (picos_to_mclk(common_dimm->tRFC_ps) - 8) >> 4;
301 /* If the CAS latency more than 8, use the ext mode */
305 ddr->timing_cfg_3 = (0
306 | ((ext_acttopre & 0x1) << 24)
307 | ((ext_refrec & 0xF) << 16)
308 | ((ext_caslat & 0x1) << 12)
309 | ((cntl_adj & 0x7) << 0)
311 debug("FSLDDR: timing_cfg_3 = 0x%08x\n", ddr->timing_cfg_3);
314 /* DDR SDRAM Timing Configuration 1 (TIMING_CFG_1) */
315 static void set_timing_cfg_1(fsl_ddr_cfg_regs_t *ddr,
316 const memctl_options_t *popts,
317 const common_timing_params_t *common_dimm,
318 unsigned int cas_latency)
320 /* Precharge-to-activate interval (tRP) */
321 unsigned char pretoact_mclk;
322 /* Activate to precharge interval (tRAS) */
323 unsigned char acttopre_mclk;
324 /* Activate to read/write interval (tRCD) */
325 unsigned char acttorw_mclk;
327 unsigned char caslat_ctrl;
328 /* Refresh recovery time (tRFC) ; trfc_low */
329 unsigned char refrec_ctrl;
330 /* Last data to precharge minimum interval (tWR) */
331 unsigned char wrrec_mclk;
332 /* Activate-to-activate interval (tRRD) */
333 unsigned char acttoact_mclk;
334 /* Last write data pair to read command issue interval (tWTR) */
335 unsigned char wrtord_mclk;
336 /* DDR_SDRAM_MODE doesn't support 9,11,13,15 */
337 static const u8 wrrec_table[] = {
338 1, 2, 3, 4, 5, 6, 7, 8, 10, 10, 12, 12, 14, 14, 0, 0};
340 pretoact_mclk = picos_to_mclk(common_dimm->tRP_ps);
341 acttopre_mclk = picos_to_mclk(common_dimm->tRAS_ps);
342 acttorw_mclk = picos_to_mclk(common_dimm->tRCD_ps);
345 * Translate CAS Latency to a DDR controller field value:
347 * CAS Lat DDR I DDR II Ctrl
348 * Clocks SPD Bit SPD Bit Value
349 * ------- ------- ------- -----
360 #if defined(CONFIG_FSL_DDR1)
361 caslat_ctrl = (cas_latency + 1) & 0x07;
362 #elif defined(CONFIG_FSL_DDR2)
363 caslat_ctrl = 2 * cas_latency - 1;
366 * if the CAS latency more than 8 cycle,
367 * we need set extend bit for it at
368 * TIMING_CFG_3[EXT_CASLAT]
372 caslat_ctrl = 2 * cas_latency - 1;
375 refrec_ctrl = picos_to_mclk(common_dimm->tRFC_ps) - 8;
376 wrrec_mclk = picos_to_mclk(common_dimm->tWR_ps);
378 wrrec_mclk = wrrec_table[wrrec_mclk - 1];
379 if (popts->OTF_burst_chop_en)
382 acttoact_mclk = picos_to_mclk(common_dimm->tRRD_ps);
384 * JEDEC has min requirement for tRRD
386 #if defined(CONFIG_FSL_DDR3)
387 if (acttoact_mclk < 4)
390 wrtord_mclk = picos_to_mclk(common_dimm->tWTR_ps);
392 * JEDEC has some min requirements for tWTR
394 #if defined(CONFIG_FSL_DDR2)
397 #elif defined(CONFIG_FSL_DDR3)
401 if (popts->OTF_burst_chop_en)
404 ddr->timing_cfg_1 = (0
405 | ((pretoact_mclk & 0x0F) << 28)
406 | ((acttopre_mclk & 0x0F) << 24)
407 | ((acttorw_mclk & 0xF) << 20)
408 | ((caslat_ctrl & 0xF) << 16)
409 | ((refrec_ctrl & 0xF) << 12)
410 | ((wrrec_mclk & 0x0F) << 8)
411 | ((acttoact_mclk & 0x07) << 4)
412 | ((wrtord_mclk & 0x07) << 0)
414 debug("FSLDDR: timing_cfg_1 = 0x%08x\n", ddr->timing_cfg_1);
417 /* DDR SDRAM Timing Configuration 2 (TIMING_CFG_2) */
418 static void set_timing_cfg_2(fsl_ddr_cfg_regs_t *ddr,
419 const memctl_options_t *popts,
420 const common_timing_params_t *common_dimm,
421 unsigned int cas_latency,
422 unsigned int additive_latency)
424 /* Additive latency */
425 unsigned char add_lat_mclk;
426 /* CAS-to-preamble override */
429 unsigned char wr_lat;
430 /* Read to precharge (tRTP) */
431 unsigned char rd_to_pre;
432 /* Write command to write data strobe timing adjustment */
433 unsigned char wr_data_delay;
434 /* Minimum CKE pulse width (tCKE) */
435 unsigned char cke_pls;
436 /* Window for four activates (tFAW) */
437 unsigned short four_act;
439 /* FIXME add check that this must be less than acttorw_mclk */
440 add_lat_mclk = additive_latency;
441 cpo = popts->cpo_override;
443 #if defined(CONFIG_FSL_DDR1)
445 * This is a lie. It should really be 1, but if it is
446 * set to 1, bits overlap into the old controller's
447 * otherwise unused ACSM field. If we leave it 0, then
448 * the HW will magically treat it as 1 for DDR 1. Oh Yea.
451 #elif defined(CONFIG_FSL_DDR2)
452 wr_lat = cas_latency - 1;
454 wr_lat = compute_cas_write_latency();
457 rd_to_pre = picos_to_mclk(common_dimm->tRTP_ps);
459 * JEDEC has some min requirements for tRTP
461 #if defined(CONFIG_FSL_DDR2)
464 #elif defined(CONFIG_FSL_DDR3)
468 if (additive_latency)
469 rd_to_pre += additive_latency;
470 if (popts->OTF_burst_chop_en)
471 rd_to_pre += 2; /* according to UM */
473 wr_data_delay = popts->write_data_delay;
474 cke_pls = picos_to_mclk(popts->tCKE_clock_pulse_width_ps);
475 four_act = picos_to_mclk(popts->tFAW_window_four_activates_ps);
477 ddr->timing_cfg_2 = (0
478 | ((add_lat_mclk & 0xf) << 28)
479 | ((cpo & 0x1f) << 23)
480 | ((wr_lat & 0xf) << 19)
481 | ((rd_to_pre & RD_TO_PRE_MASK) << RD_TO_PRE_SHIFT)
482 | ((wr_data_delay & WR_DATA_DELAY_MASK) << WR_DATA_DELAY_SHIFT)
483 | ((cke_pls & 0x7) << 6)
484 | ((four_act & 0x3f) << 0)
486 debug("FSLDDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2);
489 /* DDR SDRAM Register Control Word */
490 static void set_ddr_sdram_rcw(fsl_ddr_cfg_regs_t *ddr,
491 const memctl_options_t *popts,
492 const common_timing_params_t *common_dimm)
494 if (common_dimm->all_DIMMs_registered
495 && !common_dimm->all_DIMMs_unbuffered) {
496 if (popts->rcw_override) {
497 ddr->ddr_sdram_rcw_1 = popts->rcw_1;
498 ddr->ddr_sdram_rcw_2 = popts->rcw_2;
500 ddr->ddr_sdram_rcw_1 =
501 common_dimm->rcw[0] << 28 | \
502 common_dimm->rcw[1] << 24 | \
503 common_dimm->rcw[2] << 20 | \
504 common_dimm->rcw[3] << 16 | \
505 common_dimm->rcw[4] << 12 | \
506 common_dimm->rcw[5] << 8 | \
507 common_dimm->rcw[6] << 4 | \
509 ddr->ddr_sdram_rcw_2 =
510 common_dimm->rcw[8] << 28 | \
511 common_dimm->rcw[9] << 24 | \
512 common_dimm->rcw[10] << 20 | \
513 common_dimm->rcw[11] << 16 | \
514 common_dimm->rcw[12] << 12 | \
515 common_dimm->rcw[13] << 8 | \
516 common_dimm->rcw[14] << 4 | \
517 common_dimm->rcw[15];
519 debug("FSLDDR: ddr_sdram_rcw_1 = 0x%08x\n", ddr->ddr_sdram_rcw_1);
520 debug("FSLDDR: ddr_sdram_rcw_2 = 0x%08x\n", ddr->ddr_sdram_rcw_2);
524 /* DDR SDRAM control configuration (DDR_SDRAM_CFG) */
525 static void set_ddr_sdram_cfg(fsl_ddr_cfg_regs_t *ddr,
526 const memctl_options_t *popts,
527 const common_timing_params_t *common_dimm)
529 unsigned int mem_en; /* DDR SDRAM interface logic enable */
530 unsigned int sren; /* Self refresh enable (during sleep) */
531 unsigned int ecc_en; /* ECC enable. */
532 unsigned int rd_en; /* Registered DIMM enable */
533 unsigned int sdram_type; /* Type of SDRAM */
534 unsigned int dyn_pwr; /* Dynamic power management mode */
535 unsigned int dbw; /* DRAM dta bus width */
536 unsigned int eight_be = 0; /* 8-beat burst enable, DDR2 is zero */
537 unsigned int ncap = 0; /* Non-concurrent auto-precharge */
538 unsigned int threeT_en; /* Enable 3T timing */
539 unsigned int twoT_en; /* Enable 2T timing */
540 unsigned int ba_intlv_ctl; /* Bank (CS) interleaving control */
541 unsigned int x32_en = 0; /* x32 enable */
542 unsigned int pchb8 = 0; /* precharge bit 8 enable */
543 unsigned int hse; /* Global half strength override */
544 unsigned int mem_halt = 0; /* memory controller halt */
545 unsigned int bi = 0; /* Bypass initialization */
548 sren = popts->self_refresh_in_sleep;
549 if (common_dimm->all_DIMMs_ECC_capable) {
550 /* Allow setting of ECC only if all DIMMs are ECC. */
551 ecc_en = popts->ECC_mode;
556 if (common_dimm->all_DIMMs_registered
557 && !common_dimm->all_DIMMs_unbuffered) {
562 twoT_en = popts->twoT_en;
565 sdram_type = CONFIG_FSL_SDRAM_TYPE;
567 dyn_pwr = popts->dynamic_power;
568 dbw = popts->data_bus_width;
569 /* 8-beat burst enable DDR-III case
570 * we must clear it when use the on-the-fly mode,
571 * must set it when use the 32-bits bus mode.
573 if (sdram_type == SDRAM_TYPE_DDR3) {
574 if (popts->burst_length == DDR_BL8)
576 if (popts->burst_length == DDR_OTF)
582 threeT_en = popts->threeT_en;
583 ba_intlv_ctl = popts->ba_intlv_ctl;
584 hse = popts->half_strength_driver_enable;
586 ddr->ddr_sdram_cfg = (0
587 | ((mem_en & 0x1) << 31)
588 | ((sren & 0x1) << 30)
589 | ((ecc_en & 0x1) << 29)
590 | ((rd_en & 0x1) << 28)
591 | ((sdram_type & 0x7) << 24)
592 | ((dyn_pwr & 0x1) << 21)
593 | ((dbw & 0x3) << 19)
594 | ((eight_be & 0x1) << 18)
595 | ((ncap & 0x1) << 17)
596 | ((threeT_en & 0x1) << 16)
597 | ((twoT_en & 0x1) << 15)
598 | ((ba_intlv_ctl & 0x7F) << 8)
599 | ((x32_en & 0x1) << 5)
600 | ((pchb8 & 0x1) << 4)
602 | ((mem_halt & 0x1) << 1)
605 debug("FSLDDR: ddr_sdram_cfg = 0x%08x\n", ddr->ddr_sdram_cfg);
608 /* DDR SDRAM control configuration 2 (DDR_SDRAM_CFG_2) */
609 static void set_ddr_sdram_cfg_2(fsl_ddr_cfg_regs_t *ddr,
610 const memctl_options_t *popts,
611 const unsigned int unq_mrs_en)
613 unsigned int frc_sr = 0; /* Force self refresh */
614 unsigned int sr_ie = 0; /* Self-refresh interrupt enable */
615 unsigned int dll_rst_dis; /* DLL reset disable */
616 unsigned int dqs_cfg; /* DQS configuration */
617 unsigned int odt_cfg; /* ODT configuration */
618 unsigned int num_pr; /* Number of posted refreshes */
619 unsigned int obc_cfg; /* On-The-Fly Burst Chop Cfg */
620 unsigned int ap_en; /* Address Parity Enable */
621 unsigned int d_init; /* DRAM data initialization */
622 unsigned int rcw_en = 0; /* Register Control Word Enable */
623 unsigned int md_en = 0; /* Mirrored DIMM Enable */
624 unsigned int qd_en = 0; /* quad-rank DIMM Enable */
626 dll_rst_dis = 1; /* Make this configurable */
627 dqs_cfg = popts->DQS_config;
628 if (popts->cs_local_opts[0].odt_rd_cfg
629 || popts->cs_local_opts[0].odt_wr_cfg) {
636 num_pr = 1; /* Make this configurable */
640 * {TIMING_CFG_1[PRETOACT]
641 * + [DDR_SDRAM_CFG_2[NUM_PR]
642 * * ({EXT_REFREC || REFREC} + 8 + 2)]}
643 * << DDR_SDRAM_INTERVAL[REFINT]
645 #if defined(CONFIG_FSL_DDR3)
646 obc_cfg = popts->OTF_burst_chop_en;
651 if (popts->registered_dimm_en) {
653 ap_en = popts->ap_en;
659 #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
660 /* Use the DDR controller to auto initialize memory. */
661 d_init = popts->ECC_init_using_memctl;
662 ddr->ddr_data_init = CONFIG_MEM_INIT_VALUE;
663 debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init);
665 /* Memory will be initialized via DMA, or not at all. */
669 #if defined(CONFIG_FSL_DDR3)
670 md_en = popts->mirrored_dimm;
672 qd_en = popts->quad_rank_present ? 1 : 0;
673 ddr->ddr_sdram_cfg_2 = (0
674 | ((frc_sr & 0x1) << 31)
675 | ((sr_ie & 0x1) << 30)
676 | ((dll_rst_dis & 0x1) << 29)
677 | ((dqs_cfg & 0x3) << 26)
678 | ((odt_cfg & 0x3) << 21)
679 | ((num_pr & 0xf) << 12)
682 | ((obc_cfg & 0x1) << 6)
683 | ((ap_en & 0x1) << 5)
684 | ((d_init & 0x1) << 4)
685 | ((rcw_en & 0x1) << 2)
686 | ((md_en & 0x1) << 0)
688 debug("FSLDDR: ddr_sdram_cfg_2 = 0x%08x\n", ddr->ddr_sdram_cfg_2);
691 /* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
692 static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr,
693 const memctl_options_t *popts,
694 const unsigned int unq_mrs_en)
696 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
697 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
699 #if defined(CONFIG_FSL_DDR3)
701 unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */
702 unsigned int srt = 0; /* self-refresh temerature, normal range */
703 unsigned int asr = 0; /* auto self-refresh disable */
704 unsigned int cwl = compute_cas_write_latency() - 5;
705 unsigned int pasr = 0; /* partial array self refresh disable */
707 if (popts->rtt_override)
708 rtt_wr = popts->rtt_wr_override_value;
710 rtt_wr = popts->cs_local_opts[0].odt_rtt_wr;
712 | ((rtt_wr & 0x3) << 9)
716 | ((pasr & 0x7) << 0));
718 ddr->ddr_sdram_mode_2 = (0
719 | ((esdmode2 & 0xFFFF) << 16)
720 | ((esdmode3 & 0xFFFF) << 0)
722 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
724 #ifdef CONFIG_FSL_DDR3
725 if (unq_mrs_en) { /* unique mode registers are supported */
726 for (i = 1; i < 4; i++) {
727 if (popts->rtt_override)
728 rtt_wr = popts->rtt_wr_override_value;
730 rtt_wr = popts->cs_local_opts[i].odt_rtt_wr;
732 esdmode2 &= 0xF9FF; /* clear bit 10, 9 */
733 esdmode2 |= (rtt_wr & 0x3) << 9;
736 ddr->ddr_sdram_mode_4 = (0
737 | ((esdmode2 & 0xFFFF) << 16)
738 | ((esdmode3 & 0xFFFF) << 0)
742 ddr->ddr_sdram_mode_6 = (0
743 | ((esdmode2 & 0xFFFF) << 16)
744 | ((esdmode3 & 0xFFFF) << 0)
748 ddr->ddr_sdram_mode_8 = (0
749 | ((esdmode2 & 0xFFFF) << 16)
750 | ((esdmode3 & 0xFFFF) << 0)
755 debug("FSLDDR: ddr_sdram_mode_4 = 0x%08x\n",
756 ddr->ddr_sdram_mode_4);
757 debug("FSLDDR: ddr_sdram_mode_6 = 0x%08x\n",
758 ddr->ddr_sdram_mode_6);
759 debug("FSLDDR: ddr_sdram_mode_8 = 0x%08x\n",
760 ddr->ddr_sdram_mode_8);
765 /* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */
766 static void set_ddr_sdram_interval(fsl_ddr_cfg_regs_t *ddr,
767 const memctl_options_t *popts,
768 const common_timing_params_t *common_dimm)
770 unsigned int refint; /* Refresh interval */
771 unsigned int bstopre; /* Precharge interval */
773 refint = picos_to_mclk(common_dimm->refresh_rate_ps);
775 bstopre = popts->bstopre;
777 /* refint field used 0x3FFF in earlier controllers */
778 ddr->ddr_sdram_interval = (0
779 | ((refint & 0xFFFF) << 16)
780 | ((bstopre & 0x3FFF) << 0)
782 debug("FSLDDR: ddr_sdram_interval = 0x%08x\n", ddr->ddr_sdram_interval);
785 #if defined(CONFIG_FSL_DDR3)
786 /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
787 static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
788 const memctl_options_t *popts,
789 const common_timing_params_t *common_dimm,
790 unsigned int cas_latency,
791 unsigned int additive_latency,
792 const unsigned int unq_mrs_en)
794 unsigned short esdmode; /* Extended SDRAM mode */
795 unsigned short sdmode; /* SDRAM mode */
797 /* Mode Register - MR1 */
798 unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */
799 unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */
801 unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */
802 unsigned int al = 0; /* Posted CAS# additive latency (AL) */
803 unsigned int dic = 0; /* Output driver impedance, 40ohm */
804 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
805 1=Disable (Test/Debug) */
807 /* Mode Register - MR0 */
808 unsigned int dll_on; /* DLL control for precharge PD, 0=off, 1=on */
809 unsigned int wr; /* Write Recovery */
810 unsigned int dll_rst; /* DLL Reset */
811 unsigned int mode; /* Normal=0 or Test=1 */
812 unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
813 /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
815 unsigned int bl; /* BL: Burst Length */
817 unsigned int wr_mclk;
819 * DDR_SDRAM_MODE doesn't support 9,11,13,15
820 * Please refer JEDEC Standard No. 79-3E for Mode Register MR0
823 static const u8 wr_table[] = {1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 0, 0};
825 const unsigned int mclk_ps = get_memory_clk_period_ps();
828 if (popts->rtt_override)
829 rtt = popts->rtt_override_value;
831 rtt = popts->cs_local_opts[0].odt_rtt_norm;
833 if (additive_latency == (cas_latency - 1))
835 if (additive_latency == (cas_latency - 2))
838 if (popts->quad_rank_present)
839 dic = 1; /* output driver impedance 240/7 ohm */
842 * The esdmode value will also be used for writing
843 * MR1 during write leveling for DDR3, although the
844 * bits specifically related to the write leveling
845 * scheme will be handled automatically by the DDR
846 * controller. so we set the wrlvl_en = 0 here.
849 | ((qoff & 0x1) << 12)
850 | ((tdqs_en & 0x1) << 11)
851 | ((rtt & 0x4) << 7) /* rtt field is split */
852 | ((wrlvl_en & 0x1) << 7)
853 | ((rtt & 0x2) << 5) /* rtt field is split */
854 | ((dic & 0x2) << 4) /* DIC field is split */
856 | ((rtt & 0x1) << 2) /* rtt field is split */
857 | ((dic & 0x1) << 1) /* DIC field is split */
858 | ((dll_en & 0x1) << 0)
862 * DLL control for precharge PD
863 * 0=slow exit DLL off (tXPDLL)
864 * 1=fast exit DLL on (tXP)
868 wr_mclk = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps;
869 wr = wr_table[wr_mclk - 5];
871 dll_rst = 0; /* dll no reset */
872 mode = 0; /* normal mode */
874 /* look up table to get the cas latency bits */
875 if (cas_latency >= 5 && cas_latency <= 11) {
876 unsigned char cas_latency_table[7] = {
885 caslat = cas_latency_table[cas_latency - 5];
887 bt = 0; /* Nibble sequential */
889 switch (popts->burst_length) {
900 printf("Error: invalid burst length of %u specified. "
901 " Defaulting to on-the-fly BC4 or BL8 beats.\n",
902 popts->burst_length);
908 | ((dll_on & 0x1) << 12)
910 | ((dll_rst & 0x1) << 8)
911 | ((mode & 0x1) << 7)
912 | (((caslat >> 1) & 0x7) << 4)
917 ddr->ddr_sdram_mode = (0
918 | ((esdmode & 0xFFFF) << 16)
919 | ((sdmode & 0xFFFF) << 0)
922 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
924 if (unq_mrs_en) { /* unique mode registers are supported */
925 for (i = 1; i < 4; i++) {
926 if (popts->rtt_override)
927 rtt = popts->rtt_override_value;
929 rtt = popts->cs_local_opts[i].odt_rtt_norm;
931 esdmode &= 0xFDBB; /* clear bit 9,6,2 */
933 | ((rtt & 0x4) << 7) /* rtt field is split */
934 | ((rtt & 0x2) << 5) /* rtt field is split */
935 | ((rtt & 0x1) << 2) /* rtt field is split */
939 ddr->ddr_sdram_mode_3 = (0
940 | ((esdmode & 0xFFFF) << 16)
941 | ((sdmode & 0xFFFF) << 0)
945 ddr->ddr_sdram_mode_5 = (0
946 | ((esdmode & 0xFFFF) << 16)
947 | ((sdmode & 0xFFFF) << 0)
951 ddr->ddr_sdram_mode_7 = (0
952 | ((esdmode & 0xFFFF) << 16)
953 | ((sdmode & 0xFFFF) << 0)
958 debug("FSLDDR: ddr_sdram_mode_3 = 0x%08x\n",
959 ddr->ddr_sdram_mode_3);
960 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
961 ddr->ddr_sdram_mode_5);
962 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
963 ddr->ddr_sdram_mode_5);
967 #else /* !CONFIG_FSL_DDR3 */
969 /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
970 static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
971 const memctl_options_t *popts,
972 const common_timing_params_t *common_dimm,
973 unsigned int cas_latency,
974 unsigned int additive_latency,
975 const unsigned int unq_mrs_en)
977 unsigned short esdmode; /* Extended SDRAM mode */
978 unsigned short sdmode; /* SDRAM mode */
981 * FIXME: This ought to be pre-calculated in a
982 * technology-specific routine,
983 * e.g. compute_DDR2_mode_register(), and then the
984 * sdmode and esdmode passed in as part of common_dimm.
987 /* Extended Mode Register */
988 unsigned int mrs = 0; /* Mode Register Set */
989 unsigned int outputs = 0; /* 0=Enabled, 1=Disabled */
990 unsigned int rdqs_en = 0; /* RDQS Enable: 0=no, 1=yes */
991 unsigned int dqs_en = 0; /* DQS# Enable: 0=enable, 1=disable */
992 unsigned int ocd = 0; /* 0x0=OCD not supported,
993 0x7=OCD default state */
995 unsigned int al; /* Posted CAS# additive latency (AL) */
996 unsigned int ods = 0; /* Output Drive Strength:
997 0 = Full strength (18ohm)
998 1 = Reduced strength (4ohm) */
999 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
1000 1=Disable (Test/Debug) */
1002 /* Mode Register (MR) */
1003 unsigned int mr; /* Mode Register Definition */
1004 unsigned int pd; /* Power-Down Mode */
1005 unsigned int wr; /* Write Recovery */
1006 unsigned int dll_res; /* DLL Reset */
1007 unsigned int mode; /* Normal=0 or Test=1 */
1008 unsigned int caslat = 0;/* CAS# latency */
1009 /* BT: Burst Type (0=Sequential, 1=Interleaved) */
1011 unsigned int bl; /* BL: Burst Length */
1013 #if defined(CONFIG_FSL_DDR2)
1014 const unsigned int mclk_ps = get_memory_clk_period_ps();
1017 rtt = fsl_ddr_get_rtt();
1019 al = additive_latency;
1022 | ((mrs & 0x3) << 14)
1023 | ((outputs & 0x1) << 12)
1024 | ((rdqs_en & 0x1) << 11)
1025 | ((dqs_en & 0x1) << 10)
1026 | ((ocd & 0x7) << 7)
1027 | ((rtt & 0x2) << 5) /* rtt field is split */
1029 | ((rtt & 0x1) << 2) /* rtt field is split */
1030 | ((ods & 0x1) << 1)
1031 | ((dll_en & 0x1) << 0)
1034 mr = 0; /* FIXME: CHECKME */
1037 * 0 = Fast Exit (Normal)
1038 * 1 = Slow Exit (Low Power)
1042 #if defined(CONFIG_FSL_DDR1)
1043 wr = 0; /* Historical */
1044 #elif defined(CONFIG_FSL_DDR2)
1045 wr = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps - 1;
1050 #if defined(CONFIG_FSL_DDR1)
1051 if (1 <= cas_latency && cas_latency <= 4) {
1052 unsigned char mode_caslat_table[4] = {
1053 0x5, /* 1.5 clocks */
1054 0x2, /* 2.0 clocks */
1055 0x6, /* 2.5 clocks */
1056 0x3 /* 3.0 clocks */
1058 caslat = mode_caslat_table[cas_latency - 1];
1060 printf("Warning: unknown cas_latency %d\n", cas_latency);
1062 #elif defined(CONFIG_FSL_DDR2)
1063 caslat = cas_latency;
1067 switch (popts->burst_length) {
1075 printf("Error: invalid burst length of %u specified. "
1076 " Defaulting to 4 beats.\n",
1077 popts->burst_length);
1083 | ((mr & 0x3) << 14)
1084 | ((pd & 0x1) << 12)
1086 | ((dll_res & 0x1) << 8)
1087 | ((mode & 0x1) << 7)
1088 | ((caslat & 0x7) << 4)
1093 ddr->ddr_sdram_mode = (0
1094 | ((esdmode & 0xFFFF) << 16)
1095 | ((sdmode & 0xFFFF) << 0)
1097 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
1101 /* DDR SDRAM Data Initialization (DDR_DATA_INIT) */
1102 static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr)
1104 unsigned int init_value; /* Initialization value */
1106 init_value = 0xDEADBEEF;
1107 ddr->ddr_data_init = init_value;
1111 * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL)
1112 * The old controller on the 8540/60 doesn't have this register.
1113 * Hope it's OK to set it (to 0) anyway.
1115 static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
1116 const memctl_options_t *popts)
1118 unsigned int clk_adjust; /* Clock adjust */
1120 clk_adjust = popts->clk_adjust;
1121 ddr->ddr_sdram_clk_cntl = (clk_adjust & 0xF) << 23;
1122 debug("FSLDDR: clk_cntl = 0x%08x\n", ddr->ddr_sdram_clk_cntl);
1125 /* DDR Initialization Address (DDR_INIT_ADDR) */
1126 static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
1128 unsigned int init_addr = 0; /* Initialization address */
1130 ddr->ddr_init_addr = init_addr;
1133 /* DDR Initialization Address (DDR_INIT_EXT_ADDR) */
1134 static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
1136 unsigned int uia = 0; /* Use initialization address */
1137 unsigned int init_ext_addr = 0; /* Initialization address */
1139 ddr->ddr_init_ext_addr = (0
1140 | ((uia & 0x1) << 31)
1141 | (init_ext_addr & 0xF)
1145 /* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */
1146 static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr,
1147 const memctl_options_t *popts)
1149 unsigned int rwt = 0; /* Read-to-write turnaround for same CS */
1150 unsigned int wrt = 0; /* Write-to-read turnaround for same CS */
1151 unsigned int rrt = 0; /* Read-to-read turnaround for same CS */
1152 unsigned int wwt = 0; /* Write-to-write turnaround for same CS */
1153 unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */
1155 #if defined(CONFIG_FSL_DDR3)
1156 if (popts->burst_length == DDR_BL8) {
1157 /* We set BL/2 for fixed BL8 */
1158 rrt = 0; /* BL/2 clocks */
1159 wwt = 0; /* BL/2 clocks */
1161 /* We need to set BL/2 + 2 to BC4 and OTF */
1162 rrt = 2; /* BL/2 + 2 clocks */
1163 wwt = 2; /* BL/2 + 2 clocks */
1165 dll_lock = 1; /* tDLLK = 512 clocks from spec */
1167 ddr->timing_cfg_4 = (0
1168 | ((rwt & 0xf) << 28)
1169 | ((wrt & 0xf) << 24)
1170 | ((rrt & 0xf) << 20)
1171 | ((wwt & 0xf) << 16)
1174 debug("FSLDDR: timing_cfg_4 = 0x%08x\n", ddr->timing_cfg_4);
1177 /* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */
1178 static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr, unsigned int cas_latency)
1180 unsigned int rodt_on = 0; /* Read to ODT on */
1181 unsigned int rodt_off = 0; /* Read to ODT off */
1182 unsigned int wodt_on = 0; /* Write to ODT on */
1183 unsigned int wodt_off = 0; /* Write to ODT off */
1185 #if defined(CONFIG_FSL_DDR3)
1186 /* rodt_on = timing_cfg_1[caslat] - timing_cfg_2[wrlat] + 1 */
1187 rodt_on = cas_latency - ((ddr->timing_cfg_2 & 0x00780000) >> 19) + 1;
1188 rodt_off = 4; /* 4 clocks */
1189 wodt_on = 1; /* 1 clocks */
1190 wodt_off = 4; /* 4 clocks */
1193 ddr->timing_cfg_5 = (0
1194 | ((rodt_on & 0x1f) << 24)
1195 | ((rodt_off & 0x7) << 20)
1196 | ((wodt_on & 0x1f) << 12)
1197 | ((wodt_off & 0x7) << 8)
1199 debug("FSLDDR: timing_cfg_5 = 0x%08x\n", ddr->timing_cfg_5);
1202 /* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */
1203 static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int zq_en)
1205 unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */
1206 /* Normal Operation Full Calibration Time (tZQoper) */
1207 unsigned int zqoper = 0;
1208 /* Normal Operation Short Calibration Time (tZQCS) */
1209 unsigned int zqcs = 0;
1212 zqinit = 9; /* 512 clocks */
1213 zqoper = 8; /* 256 clocks */
1214 zqcs = 6; /* 64 clocks */
1217 ddr->ddr_zq_cntl = (0
1218 | ((zq_en & 0x1) << 31)
1219 | ((zqinit & 0xF) << 24)
1220 | ((zqoper & 0xF) << 16)
1221 | ((zqcs & 0xF) << 8)
1223 debug("FSLDDR: zq_cntl = 0x%08x\n", ddr->ddr_zq_cntl);
1226 /* DDR Write Leveling Control (DDR_WRLVL_CNTL) */
1227 static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int wrlvl_en,
1228 const memctl_options_t *popts)
1231 * First DQS pulse rising edge after margining mode
1232 * is programmed (tWL_MRD)
1234 unsigned int wrlvl_mrd = 0;
1235 /* ODT delay after margining mode is programmed (tWL_ODTEN) */
1236 unsigned int wrlvl_odten = 0;
1237 /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */
1238 unsigned int wrlvl_dqsen = 0;
1239 /* WRLVL_SMPL: Write leveling sample time */
1240 unsigned int wrlvl_smpl = 0;
1241 /* WRLVL_WLR: Write leveling repeition time */
1242 unsigned int wrlvl_wlr = 0;
1243 /* WRLVL_START: Write leveling start time */
1244 unsigned int wrlvl_start = 0;
1246 /* suggest enable write leveling for DDR3 due to fly-by topology */
1248 /* tWL_MRD min = 40 nCK, we set it 64 */
1252 /* tWL_DQSEN min = 25 nCK, we set it 32 */
1255 * Write leveling sample time at least need 6 clocks
1256 * higher than tWLO to allow enough time for progagation
1257 * delay and sampling the prime data bits.
1261 * Write leveling repetition time
1262 * at least tWLO + 6 clocks clocks
1267 * Write leveling start time
1268 * The value use for the DQS_ADJUST for the first sample
1269 * when write leveling is enabled. It probably needs to be
1270 * overriden per platform.
1274 * Override the write leveling sample and start time
1275 * according to specific board
1277 if (popts->wrlvl_override) {
1278 wrlvl_smpl = popts->wrlvl_sample;
1279 wrlvl_start = popts->wrlvl_start;
1283 ddr->ddr_wrlvl_cntl = (0
1284 | ((wrlvl_en & 0x1) << 31)
1285 | ((wrlvl_mrd & 0x7) << 24)
1286 | ((wrlvl_odten & 0x7) << 20)
1287 | ((wrlvl_dqsen & 0x7) << 16)
1288 | ((wrlvl_smpl & 0xf) << 12)
1289 | ((wrlvl_wlr & 0x7) << 8)
1290 | ((wrlvl_start & 0x1F) << 0)
1292 debug("FSLDDR: wrlvl_cntl = 0x%08x\n", ddr->ddr_wrlvl_cntl);
1295 /* DDR Self Refresh Counter (DDR_SR_CNTR) */
1296 static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr, unsigned int sr_it)
1298 /* Self Refresh Idle Threshold */
1299 ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
1302 static void set_ddr_eor(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
1304 if (popts->addr_hash) {
1305 ddr->ddr_eor = 0x40000000; /* address hash enable */
1306 puts("Addess hashing enabled.\n");
1310 static void set_ddr_cdr1(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
1312 ddr->ddr_cdr1 = popts->ddr_cdr1;
1313 debug("FSLDDR: ddr_cdr1 = 0x%08x\n", ddr->ddr_cdr1);
1317 check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
1319 unsigned int res = 0;
1322 * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are
1323 * not set at the same time.
1325 if (ddr->ddr_sdram_cfg & 0x10000000
1326 && ddr->ddr_sdram_cfg & 0x00008000) {
1327 printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
1328 " should not be set at the same time.\n");
1336 compute_fsl_memctl_config_regs(const memctl_options_t *popts,
1337 fsl_ddr_cfg_regs_t *ddr,
1338 const common_timing_params_t *common_dimm,
1339 const dimm_params_t *dimm_params,
1340 unsigned int dbw_cap_adj,
1341 unsigned int size_only)
1344 unsigned int cas_latency;
1345 unsigned int additive_latency;
1348 unsigned int wrlvl_en;
1349 unsigned int ip_rev = 0;
1350 unsigned int unq_mrs_en = 0;
1353 memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
1355 if (common_dimm == NULL) {
1356 printf("Error: subset DIMM params struct null pointer\n");
1361 * Process overrides first.
1363 * FIXME: somehow add dereated caslat to this
1365 cas_latency = (popts->cas_latency_override)
1366 ? popts->cas_latency_override_value
1367 : common_dimm->lowest_common_SPD_caslat;
1369 additive_latency = (popts->additive_latency_override)
1370 ? popts->additive_latency_override_value
1371 : common_dimm->additive_latency;
1373 sr_it = (popts->auto_self_refresh_en)
1376 /* ZQ calibration */
1377 zq_en = (popts->zq_en) ? 1 : 0;
1378 /* write leveling */
1379 wrlvl_en = (popts->wrlvl_en) ? 1 : 0;
1381 /* Chip Select Memory Bounds (CSn_BNDS) */
1382 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1383 unsigned long long ea = 0, sa = 0;
1384 unsigned int cs_per_dimm
1385 = CONFIG_CHIP_SELECTS_PER_CTRL / CONFIG_DIMM_SLOTS_PER_CTLR;
1386 unsigned int dimm_number
1388 unsigned long long rank_density
1389 = dimm_params[dimm_number].rank_density;
1391 if (((i == 1) && (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1)) ||
1392 ((i == 2) && (popts->ba_intlv_ctl & 0x04)) ||
1393 ((i == 3) && (popts->ba_intlv_ctl & FSL_DDR_CS2_CS3))) {
1395 * Don't set up boundaries for unused CS
1396 * cs1 for cs0_cs1, cs0_cs1_and_cs2_cs3, cs0_cs1_cs2_cs3
1397 * cs2 for cs0_cs1_cs2_cs3
1398 * cs3 for cs2_cs3, cs0_cs1_and_cs2_cs3, cs0_cs1_cs2_cs3
1399 * But we need to set the ODT_RD_CFG and
1400 * ODT_WR_CFG for CS1_CONFIG here.
1402 set_csn_config(dimm_number, i, ddr, popts, dimm_params);
1405 if (dimm_params[dimm_number].n_ranks == 0) {
1406 debug("Skipping setup of CS%u "
1407 "because n_ranks on DIMM %u is 0\n", i, dimm_number);
1410 if (popts->memctl_interleaving && popts->ba_intlv_ctl) {
1412 * This works superbank 2CS
1413 * There are 2 or more memory controllers configured
1414 * identically, memory is interleaved between them,
1415 * and each controller uses rank interleaving within
1416 * itself. Therefore the starting and ending address
1417 * on each controller is twice the amount present on
1418 * each controller. If any CS is not included in the
1419 * interleaving, the memory on that CS is not accssible
1420 * and the total memory size is reduced. The CS is also
1423 unsigned long long ctlr_density = 0;
1424 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
1425 case FSL_DDR_CS0_CS1:
1426 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
1427 ctlr_density = dimm_params[0].rank_density * 2;
1431 case FSL_DDR_CS2_CS3:
1432 ctlr_density = dimm_params[0].rank_density;
1436 case FSL_DDR_CS0_CS1_CS2_CS3:
1438 * The four CS interleaving should have been verified by
1439 * populate_memctl_options()
1441 ctlr_density = dimm_params[0].rank_density * 4;
1446 ea = (CONFIG_NUM_DDR_CONTROLLERS *
1447 (ctlr_density >> dbw_cap_adj)) - 1;
1449 else if (!popts->memctl_interleaving && popts->ba_intlv_ctl) {
1451 * If memory interleaving between controllers is NOT
1452 * enabled, the starting address for each memory
1453 * controller is distinct. However, because rank
1454 * interleaving is enabled, the starting and ending
1455 * addresses of the total memory on that memory
1456 * controller needs to be programmed into its
1457 * respective CS0_BNDS.
1459 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
1460 case FSL_DDR_CS0_CS1_CS2_CS3:
1461 /* CS0+CS1+CS2+CS3 interleaving, only CS0_CNDS
1464 sa = common_dimm->base_address;
1465 ea = sa + (4 * (rank_density >> dbw_cap_adj))-1;
1467 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
1468 /* CS0+CS1 and CS2+CS3 interleaving, CS0_CNDS
1469 * and CS2_CNDS need to be set.
1471 if ((i == 2) && (dimm_number == 0)) {
1472 sa = dimm_params[dimm_number].base_address +
1473 2 * (rank_density >> dbw_cap_adj);
1474 ea = sa + 2 * (rank_density >> dbw_cap_adj) - 1;
1476 sa = dimm_params[dimm_number].base_address;
1477 ea = sa + (2 * (rank_density >>
1481 case FSL_DDR_CS0_CS1:
1482 /* CS0+CS1 interleaving, CS0_CNDS needs
1485 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
1486 sa = dimm_params[dimm_number].base_address;
1487 ea = sa + (rank_density >> dbw_cap_adj) - 1;
1488 sa += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
1489 ea += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
1495 ea += (rank_density >> dbw_cap_adj);
1497 case FSL_DDR_CS2_CS3:
1498 /* CS2+CS3 interleaving*/
1499 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
1500 sa = dimm_params[dimm_number].base_address;
1501 ea = sa + (rank_density >> dbw_cap_adj) - 1;
1502 sa += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
1503 ea += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
1509 ea += (rank_density >> dbw_cap_adj);
1511 default: /* No bank(chip-select) interleaving */
1515 else if (popts->memctl_interleaving && !popts->ba_intlv_ctl) {
1517 * Only the rank on CS0 of each memory controller may
1518 * be used if memory controller interleaving is used
1519 * without rank interleaving within each memory
1520 * controller. However, the ending address programmed
1521 * into each CS0 must be the sum of the amount of
1522 * memory in the two CS0 ranks.
1525 ea = (2 * (rank_density >> dbw_cap_adj)) - 1;
1529 else if (!popts->memctl_interleaving && !popts->ba_intlv_ctl) {
1531 * No rank interleaving and no memory controller
1534 sa = dimm_params[dimm_number].base_address;
1535 ea = sa + (rank_density >> dbw_cap_adj) - 1;
1536 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
1537 sa += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
1538 ea += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
1548 ddr->cs[i].bnds = (0
1549 | ((sa & 0xFFF) << 16) /* starting address MSB */
1550 | ((ea & 0xFFF) << 0) /* ending address MSB */
1553 debug("FSLDDR: cs[%d]_bnds = 0x%08x\n", i, ddr->cs[i].bnds);
1555 set_csn_config(dimm_number, i, ddr, popts, dimm_params);
1556 set_csn_config_2(i, ddr);
1558 printf("CS%d is disabled.\n", i);
1562 * In the case we only need to compute the ddr sdram size, we only need
1563 * to set csn registers, so return from here.
1568 set_ddr_eor(ddr, popts);
1570 #if !defined(CONFIG_FSL_DDR1)
1571 set_timing_cfg_0(ddr, popts);
1574 set_timing_cfg_3(ddr, common_dimm, cas_latency);
1575 set_timing_cfg_1(ddr, popts, common_dimm, cas_latency);
1576 set_timing_cfg_2(ddr, popts, common_dimm,
1577 cas_latency, additive_latency);
1579 set_ddr_cdr1(ddr, popts);
1580 set_ddr_sdram_cfg(ddr, popts, common_dimm);
1581 ip_rev = fsl_ddr_get_version();
1582 if (ip_rev > 0x40400)
1585 set_ddr_sdram_cfg_2(ddr, popts, unq_mrs_en);
1586 set_ddr_sdram_mode(ddr, popts, common_dimm,
1587 cas_latency, additive_latency, unq_mrs_en);
1588 set_ddr_sdram_mode_2(ddr, popts, unq_mrs_en);
1589 set_ddr_sdram_interval(ddr, popts, common_dimm);
1590 set_ddr_data_init(ddr);
1591 set_ddr_sdram_clk_cntl(ddr, popts);
1592 set_ddr_init_addr(ddr);
1593 set_ddr_init_ext_addr(ddr);
1594 set_timing_cfg_4(ddr, popts);
1595 set_timing_cfg_5(ddr, cas_latency);
1597 set_ddr_zq_cntl(ddr, zq_en);
1598 set_ddr_wrlvl_cntl(ddr, wrlvl_en, popts);
1600 set_ddr_sr_cntr(ddr, sr_it);
1602 set_ddr_sdram_rcw(ddr, popts, common_dimm);
1604 return check_fsl_memctl_config_regs(ddr);