2 * Copyright 2008 Freescale Semiconductor, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * Version 2 as published by the Free Software Foundation.
10 * Generic driver for Freescale DDR/DDR2/DDR3 memory controller.
11 * Based on code from spd_sdram.c
12 * Author: James Yang [at freescale.com]
16 #include <asm/fsl_ddr_sdram.h>
20 extern unsigned int picos_to_mclk(unsigned int picos);
22 * Determine Rtt value.
24 * This should likely be either board or controller specific.
32 * FIXME: Apparently 8641 needs a value of 2
33 * FIXME: Old code seys if 667 MHz or higher, use 3 on 8572
35 * FIXME: There was some effort down this line earlier:
38 * for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL/2; i++) {
39 * if (popts->dimmslot[i].num_valid_cs
40 * && (popts->cs_local_opts[2*i].odt_rd_cfg
41 * || popts->cs_local_opts[2*i].odt_wr_cfg)) {
47 static inline int fsl_ddr_get_rtt(void)
51 #if defined(CONFIG_FSL_DDR1)
53 #elif defined(CONFIG_FSL_DDR2)
56 #error "Need Rtt value for DDR3"
62 /* Chip Select Configuration (CSn_CONFIG) */
63 static void set_csn_config(int i, fsl_ddr_cfg_regs_t *ddr,
64 const memctl_options_t *popts,
65 const dimm_params_t *dimm_params)
67 unsigned int cs_n_en = 0; /* Chip Select enable */
68 unsigned int intlv_en = 0; /* Memory controller interleave enable */
69 unsigned int intlv_ctl = 0; /* Interleaving control */
70 unsigned int ap_n_en = 0; /* Chip select n auto-precharge enable */
71 unsigned int odt_rd_cfg = 0; /* ODT for reads configuration */
72 unsigned int odt_wr_cfg = 0; /* ODT for writes configuration */
73 unsigned int ba_bits_cs_n = 0; /* Num of bank bits for SDRAM on CSn */
74 unsigned int row_bits_cs_n = 0; /* Num of row bits for SDRAM on CSn */
75 unsigned int col_bits_cs_n = 0; /* Num of ocl bits for SDRAM on CSn */
77 /* Compute CS_CONFIG only for existing ranks of each DIMM. */
79 && (dimm_params[i/2].n_ranks == 1))
80 || (dimm_params[i/2].n_ranks == 2)) {
81 unsigned int n_banks_per_sdram_device;
84 /* These fields only available in CS0_CONFIG */
85 intlv_en = popts->memctl_interleaving;
86 intlv_ctl = popts->memctl_interleaving_mode;
88 ap_n_en = popts->cs_local_opts[i].auto_precharge;
89 odt_rd_cfg = popts->cs_local_opts[i].odt_rd_cfg;
90 odt_wr_cfg = popts->cs_local_opts[i].odt_wr_cfg;
91 n_banks_per_sdram_device
92 = dimm_params[i/2].n_banks_per_sdram_device;
93 ba_bits_cs_n = __ilog2(n_banks_per_sdram_device) - 2;
94 row_bits_cs_n = dimm_params[i/2].n_row_addr - 12;
95 col_bits_cs_n = dimm_params[i/2].n_col_addr - 8;
98 ddr->cs[i].config = (0
99 | ((cs_n_en & 0x1) << 31)
100 | ((intlv_en & 0x3) << 29)
101 | ((intlv_ctl & 0xf) << 24)
102 | ((ap_n_en & 0x1) << 23)
104 /* XXX: some implementation only have 1 bit starting at left */
105 | ((odt_rd_cfg & 0x7) << 20)
107 /* XXX: Some implementation only have 1 bit starting at left */
108 | ((odt_wr_cfg & 0x7) << 16)
110 | ((ba_bits_cs_n & 0x3) << 14)
111 | ((row_bits_cs_n & 0x7) << 8)
112 | ((col_bits_cs_n & 0x7) << 0)
116 /* Chip Select Configuration 2 (CSn_CONFIG_2) */
118 static void set_csn_config_2(int i, fsl_ddr_cfg_regs_t *ddr)
120 unsigned int pasr_cfg = 0; /* Partial array self refresh config */
122 ddr->cs[i].config_2 = ((pasr_cfg & 7) << 24);
125 /* -3E = 667 CL5, -25 = CL6 800, -25E = CL5 800 */
127 #if defined(CONFIG_FSL_DDR2)
129 * DDR SDRAM Timing Configuration 0 (TIMING_CFG_0)
131 * Avoid writing for DDR I. The new PQ38 DDR controller
132 * dreams up non-zero default values to be backwards compatible.
134 static void set_timing_cfg_0(fsl_ddr_cfg_regs_t *ddr)
136 unsigned char trwt_mclk = 0; /* Read-to-write turnaround */
137 unsigned char twrt_mclk = 0; /* Write-to-read turnaround */
138 /* 7.5 ns on -3E; 0 means WL - CL + BL/2 + 1 */
139 unsigned char trrt_mclk = 0; /* Read-to-read turnaround */
140 unsigned char twwt_mclk = 0; /* Write-to-write turnaround */
142 /* Active powerdown exit timing (tXARD and tXARDS). */
143 unsigned char act_pd_exit_mclk;
144 /* Precharge powerdown exit timing (tXP). */
145 unsigned char pre_pd_exit_mclk;
146 /* Precharge powerdown exit timing (tAXPD). */
147 unsigned char taxpd_mclk;
148 /* Mode register set cycle time (tMRD). */
149 unsigned char tmrd_mclk;
151 /* (tXARD and tXARDS). Empirical? */
152 act_pd_exit_mclk = 2;
154 /* XXX: tXARD = 2, tXARDS = 7 - AL. * Empirical? */
155 pre_pd_exit_mclk = 6;
157 /* FIXME: tXP = 2 on Micron 667 MHz DIMM */
162 ddr->timing_cfg_0 = (0
163 | ((trwt_mclk & 0x3) << 30) /* RWT */
164 | ((twrt_mclk & 0x3) << 28) /* WRT */
165 | ((trrt_mclk & 0x3) << 26) /* RRT */
166 | ((twwt_mclk & 0x3) << 24) /* WWT */
167 | ((act_pd_exit_mclk & 0x7) << 20) /* ACT_PD_EXIT */
168 | ((pre_pd_exit_mclk & 0x7) << 16) /* PRE_PD_EXIT */
169 | ((taxpd_mclk & 0xf) << 8) /* ODT_PD_EXIT */
170 | ((tmrd_mclk & 0xf) << 0) /* MRS_CYC */
172 debug("FSLDDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
174 #endif /* defined(CONFIG_FSL_DDR2) */
176 /* DDR SDRAM Timing Configuration 3 (TIMING_CFG_3) */
177 static void set_timing_cfg_3(fsl_ddr_cfg_regs_t *ddr,
178 const common_timing_params_t *common_dimm)
180 /* Extended Activate to precharge interval (tRAS) */
181 unsigned int ext_acttopre = 0;
182 unsigned int ext_refrec; /* Extended refresh recovery time (tRFC) */
183 unsigned int ext_caslat = 0; /* Extended MCAS latency from READ cmd */
184 unsigned int cntl_adj = 0; /* Control Adjust */
186 ext_refrec = (picos_to_mclk(common_dimm->tRFC_ps) - 8) >> 4;
187 ddr->timing_cfg_3 = (0
188 | ((ext_acttopre & 0x1) << 24)
189 | ((ext_refrec & 0x7) << 16)
190 | ((ext_caslat & 0x1) << 12)
191 | ((cntl_adj & 0x7) << 0)
195 /* DDR SDRAM Timing Configuration 1 (TIMING_CFG_1) */
196 static void set_timing_cfg_1(fsl_ddr_cfg_regs_t *ddr,
197 const common_timing_params_t *common_dimm,
198 unsigned int cas_latency)
200 /* Precharge-to-activate interval (tRP) */
201 unsigned char pretoact_mclk;
202 /* Activate to precharge interval (tRAS) */
203 unsigned char acttopre_mclk;
204 /* Activate to read/write interval (tRCD) */
205 unsigned char acttorw_mclk;
207 unsigned char caslat_ctrl;
208 /* Refresh recovery time (tRFC) ; trfc_low */
209 unsigned char refrec_ctrl;
210 /* Last data to precharge minimum interval (tWR) */
211 unsigned char wrrec_mclk;
212 /* Activate-to-activate interval (tRRD) */
213 unsigned char acttoact_mclk;
214 /* Last write data pair to read command issue interval (tWTR) */
215 unsigned char wrtord_mclk;
217 pretoact_mclk = picos_to_mclk(common_dimm->tRP_ps);
218 acttopre_mclk = picos_to_mclk(common_dimm->tRAS_ps);
219 acttorw_mclk = picos_to_mclk(common_dimm->tRCD_ps);
222 * Translate CAS Latency to a DDR controller field value:
224 * CAS Lat DDR I DDR II Ctrl
225 * Clocks SPD Bit SPD Bit Value
226 * ------- ------- ------- -----
237 #if defined(CONFIG_FSL_DDR1)
238 caslat_ctrl = (cas_latency + 1) & 0x07;
239 #elif defined(CONFIG_FSL_DDR2)
240 caslat_ctrl = 2 * cas_latency - 1;
242 #error "Need CAS Latency help for DDR3 in fsl_ddr_sdram.c"
245 refrec_ctrl = picos_to_mclk(common_dimm->tRFC_ps) - 8;
246 wrrec_mclk = picos_to_mclk(common_dimm->tWR_ps);
247 acttoact_mclk = picos_to_mclk(common_dimm->tRRD_ps);
248 wrtord_mclk = picos_to_mclk(common_dimm->tWTR_ps);
250 ddr->timing_cfg_1 = (0
251 | ((pretoact_mclk & 0x07) << 28)
252 | ((acttopre_mclk & 0x0F) << 24)
253 | ((acttorw_mclk & 0x7) << 20)
254 | ((caslat_ctrl & 0xF) << 16)
255 | ((refrec_ctrl & 0xF) << 12)
256 | ((wrrec_mclk & 0x07) << 8)
257 | ((acttoact_mclk & 0x07) << 4)
258 | ((wrtord_mclk & 0x07) << 0)
262 /* DDR SDRAM Timing Configuration 2 (TIMING_CFG_2) */
263 static void set_timing_cfg_2(fsl_ddr_cfg_regs_t *ddr,
264 const memctl_options_t *popts,
265 const common_timing_params_t *common_dimm,
266 unsigned int cas_latency,
267 unsigned int additive_latency)
269 /* Additive latency */
270 unsigned char add_lat_mclk;
271 /* CAS-to-preamble override */
274 unsigned char wr_lat;
275 /* Read to precharge (tRTP) */
276 unsigned char rd_to_pre;
277 /* Write command to write data strobe timing adjustment */
278 unsigned char wr_data_delay;
279 /* Minimum CKE pulse width (tCKE) */
280 unsigned char cke_pls;
281 /* Window for four activates (tFAW) */
282 unsigned short four_act;
284 /* FIXME add check that this must be less than acttorw_mclk */
285 add_lat_mclk = additive_latency;
286 cpo = popts->cpo_override;
288 #if defined(CONFIG_FSL_DDR1)
290 * This is a lie. It should really be 1, but if it is
291 * set to 1, bits overlap into the old controller's
292 * otherwise unused ACSM field. If we leave it 0, then
293 * the HW will magically treat it as 1 for DDR 1. Oh Yea.
296 #elif defined(CONFIG_FSL_DDR2)
297 wr_lat = cas_latency + additive_latency - 1;
299 #error "Fix WR_LAT for DDR3"
302 rd_to_pre = picos_to_mclk(common_dimm->tRTP_ps);
303 wr_data_delay = popts->write_data_delay;
304 cke_pls = picos_to_mclk(popts->tCKE_clock_pulse_width_ps);
305 four_act = picos_to_mclk(popts->tFAW_window_four_activates_ps);
307 ddr->timing_cfg_2 = (0
308 | ((add_lat_mclk & 0x7) << 28)
309 | ((cpo & 0x1f) << 23)
310 | ((wr_lat & 0x7) << 19)
311 | ((rd_to_pre & 0x7) << 13)
312 | ((wr_data_delay & 0x7) << 10)
313 | ((cke_pls & 0x7) << 6)
314 | ((four_act & 0x1f) << 0)
318 /* DDR SDRAM control configuration (DDR_SDRAM_CFG) */
319 static void set_ddr_sdram_cfg(fsl_ddr_cfg_regs_t *ddr,
320 const memctl_options_t *popts,
321 const common_timing_params_t *common_dimm)
323 unsigned int mem_en; /* DDR SDRAM interface logic enable */
324 unsigned int sren; /* Self refresh enable (during sleep) */
325 unsigned int ecc_en; /* ECC enable. */
326 unsigned int rd_en; /* Registered DIMM enable */
327 unsigned int sdram_type; /* Type of SDRAM */
328 unsigned int dyn_pwr; /* Dynamic power management mode */
329 unsigned int dbw; /* DRAM dta bus width */
330 unsigned int eight_be; /* 8-beat burst enable */
331 unsigned int ncap = 0; /* Non-concurrent auto-precharge */
332 unsigned int threeT_en; /* Enable 3T timing */
333 unsigned int twoT_en; /* Enable 2T timing */
334 unsigned int ba_intlv_ctl; /* Bank (CS) interleaving control */
335 unsigned int x32_en = 0; /* x32 enable */
336 unsigned int pchb8 = 0; /* precharge bit 8 enable */
337 unsigned int hse; /* Global half strength override */
338 unsigned int mem_halt = 0; /* memory controller halt */
339 unsigned int bi = 0; /* Bypass initialization */
342 sren = popts->self_refresh_in_sleep;
343 if (common_dimm->all_DIMMs_ECC_capable) {
344 /* Allow setting of ECC only if all DIMMs are ECC. */
345 ecc_en = popts->ECC_mode;
350 rd_en = (common_dimm->all_DIMMs_registered
351 && !common_dimm->all_DIMMs_unbuffered);
353 sdram_type = CONFIG_FSL_SDRAM_TYPE;
355 dyn_pwr = popts->dynamic_power;
356 dbw = popts->data_bus_width;
357 eight_be = 0; /* always 0 for DDR2 */
358 threeT_en = popts->threeT_en;
359 twoT_en = popts->twoT_en;
360 ba_intlv_ctl = popts->ba_intlv_ctl;
361 hse = popts->half_strength_driver_enable;
363 ddr->ddr_sdram_cfg = (0
364 | ((mem_en & 0x1) << 31)
365 | ((sren & 0x1) << 30)
366 | ((ecc_en & 0x1) << 29)
367 | ((rd_en & 0x1) << 28)
368 | ((sdram_type & 0x7) << 24)
369 | ((dyn_pwr & 0x1) << 21)
370 | ((dbw & 0x3) << 19)
371 | ((eight_be & 0x1) << 18)
372 | ((ncap & 0x1) << 17)
373 | ((threeT_en & 0x1) << 16)
374 | ((twoT_en & 0x1) << 15)
375 | ((ba_intlv_ctl & 0x7F) << 8)
376 | ((x32_en & 0x1) << 5)
377 | ((pchb8 & 0x1) << 4)
379 | ((mem_halt & 0x1) << 1)
384 /* DDR SDRAM control configuration 2 (DDR_SDRAM_CFG_2) */
385 static void set_ddr_sdram_cfg_2(fsl_ddr_cfg_regs_t *ddr,
386 const memctl_options_t *popts)
388 unsigned int frc_sr = 0; /* Force self refresh */
389 unsigned int sr_ie = 0; /* Self-refresh interrupt enable */
390 unsigned int dll_rst_dis; /* DLL reset disable */
391 unsigned int dqs_cfg; /* DQS configuration */
392 unsigned int odt_cfg; /* ODT configuration */
393 unsigned int num_pr; /* Number of posted refreshes */
394 unsigned int obc_cfg; /* On-The-Fly Burst Chop Cfg */
395 unsigned int ap_en; /* Address Parity Enable */
396 unsigned int d_init; /* DRAM data initialization */
397 unsigned int rcw_en = 0; /* Register Control Word Enable */
398 unsigned int md_en = 0; /* Mirrored DIMM Enable */
400 dll_rst_dis = 1; /* Make this configurable */
401 dqs_cfg = popts->DQS_config;
402 if (popts->cs_local_opts[0].odt_rd_cfg
403 || popts->cs_local_opts[0].odt_wr_cfg) {
410 num_pr = 1; /* Make this configurable */
414 * {TIMING_CFG_1[PRETOACT]
415 * + [DDR_SDRAM_CFG_2[NUM_PR]
416 * * ({EXT_REFREC || REFREC} + 8 + 2)]}
417 * << DDR_SDRAM_INTERVAL[REFINT]
420 obc_cfg = 0; /* Make this configurable? */
421 ap_en = 0; /* Make this configurable? */
423 #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
424 /* Use the DDR controller to auto initialize memory. */
426 ddr->ddr_data_init = CONFIG_MEM_INIT_VALUE;
427 debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init);
429 /* Memory will be initialized via DMA, or not at all. */
433 ddr->ddr_sdram_cfg_2 = (0
434 | ((frc_sr & 0x1) << 31)
435 | ((sr_ie & 0x1) << 30)
436 | ((dll_rst_dis & 0x1) << 29)
437 | ((dqs_cfg & 0x3) << 26)
438 | ((odt_cfg & 0x3) << 21)
439 | ((num_pr & 0xf) << 12)
440 | ((obc_cfg & 0x1) << 6)
441 | ((ap_en & 0x1) << 5)
442 | ((d_init & 0x1) << 4)
443 | ((rcw_en & 0x1) << 2)
444 | ((md_en & 0x1) << 0)
448 /* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
449 static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr)
451 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
452 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
454 ddr->ddr_sdram_mode_2 = (0
455 | ((esdmode2 & 0xFFFF) << 16)
456 | ((esdmode3 & 0xFFFF) << 0)
460 /* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */
461 static void set_ddr_sdram_interval(fsl_ddr_cfg_regs_t *ddr,
462 const memctl_options_t *popts,
463 const common_timing_params_t *common_dimm)
465 unsigned int refint; /* Refresh interval */
466 unsigned int bstopre; /* Precharge interval */
468 refint = picos_to_mclk(common_dimm->refresh_rate_ps);
470 bstopre = popts->bstopre;
472 /* refint field used 0x3FFF in earlier controllers */
473 ddr->ddr_sdram_interval = (0
474 | ((refint & 0xFFFF) << 16)
475 | ((bstopre & 0x3FFF) << 0)
479 /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
480 static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
481 const memctl_options_t *popts,
482 const common_timing_params_t *common_dimm,
483 unsigned int cas_latency,
484 unsigned int additive_latency)
486 unsigned short esdmode; /* Extended SDRAM mode */
487 unsigned short sdmode; /* SDRAM mode */
490 * FIXME: This ought to be pre-calculated in a
491 * technology-specific routine,
492 * e.g. compute_DDR2_mode_register(), and then the
493 * sdmode and esdmode passed in as part of common_dimm.
496 /* Extended Mode Register */
497 unsigned int mrs = 0; /* Mode Register Set */
498 unsigned int outputs = 0; /* 0=Enabled, 1=Disabled */
499 unsigned int rdqs_en = 0; /* RDQS Enable: 0=no, 1=yes */
500 unsigned int dqs_en = 0; /* DQS# Enable: 0=enable, 1=disable */
501 unsigned int ocd = 0; /* 0x0=OCD not supported,
502 0x7=OCD default state */
504 unsigned int al; /* Posted CAS# additive latency (AL) */
505 unsigned int ods = 0; /* Output Drive Strength:
506 0 = Full strength (18ohm)
507 1 = Reduced strength (4ohm) */
508 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
509 1=Disable (Test/Debug) */
511 /* Mode Register (MR) */
512 unsigned int mr; /* Mode Register Definition */
513 unsigned int pd; /* Power-Down Mode */
514 unsigned int wr; /* Write Recovery */
515 unsigned int dll_res; /* DLL Reset */
516 unsigned int mode; /* Normal=0 or Test=1 */
517 unsigned int caslat = 0;/* CAS# latency */
518 /* BT: Burst Type (0=Sequential, 1=Interleaved) */
520 unsigned int bl; /* BL: Burst Length */
522 #if defined(CONFIG_FSL_DDR2)
523 const unsigned int mclk_ps = get_memory_clk_period_ps();
526 rtt = fsl_ddr_get_rtt();
528 al = additive_latency;
531 | ((mrs & 0x3) << 14)
532 | ((outputs & 0x1) << 12)
533 | ((rdqs_en & 0x1) << 11)
534 | ((dqs_en & 0x1) << 10)
536 | ((rtt & 0x2) << 5) /* rtt field is split */
538 | ((rtt & 0x1) << 2) /* rtt field is split */
540 | ((dll_en & 0x1) << 0)
543 mr = 0; /* FIXME: CHECKME */
546 * 0 = Fast Exit (Normal)
547 * 1 = Slow Exit (Low Power)
551 #if defined(CONFIG_FSL_DDR1)
552 wr = 0; /* Historical */
553 #elif defined(CONFIG_FSL_DDR2)
554 wr = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps - 1;
556 #error "Write tWR_auto for DDR3"
561 #if defined(CONFIG_FSL_DDR1)
562 if (1 <= cas_latency && cas_latency <= 4) {
563 unsigned char mode_caslat_table[4] = {
564 0x5, /* 1.5 clocks */
565 0x2, /* 2.0 clocks */
566 0x6, /* 2.5 clocks */
569 caslat = mode_caslat_table[cas_latency - 1];
571 printf("Warning: unknown cas_latency %d\n", cas_latency);
573 #elif defined(CONFIG_FSL_DDR2)
574 caslat = cas_latency;
576 #error "Fix the mode CAS Latency for DDR3"
580 switch (popts->burst_length) {
588 printf("Error: invalid burst length of %u specified. "
589 " Defaulting to 4 beats.\n",
590 popts->burst_length);
599 | ((dll_res & 0x1) << 8)
600 | ((mode & 0x1) << 7)
601 | ((caslat & 0x7) << 4)
606 ddr->ddr_sdram_mode = (0
607 | ((esdmode & 0xFFFF) << 16)
608 | ((sdmode & 0xFFFF) << 0)
613 /* DDR SDRAM Data Initialization (DDR_DATA_INIT) */
614 static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr)
616 unsigned int init_value; /* Initialization value */
618 init_value = 0xDEADBEEF;
619 ddr->ddr_data_init = init_value;
623 * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL)
624 * The old controller on the 8540/60 doesn't have this register.
625 * Hope it's OK to set it (to 0) anyway.
627 static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
628 const memctl_options_t *popts)
630 unsigned int clk_adjust; /* Clock adjust */
632 clk_adjust = popts->clk_adjust;
633 ddr->ddr_sdram_clk_cntl = (clk_adjust & 0xF) << 23;
636 /* DDR Initialization Address (DDR_INIT_ADDR) */
637 static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
639 unsigned int init_addr = 0; /* Initialization address */
641 ddr->ddr_init_addr = init_addr;
644 /* DDR Initialization Address (DDR_INIT_EXT_ADDR) */
645 static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
647 unsigned int uia = 0; /* Use initialization address */
648 unsigned int init_ext_addr = 0; /* Initialization address */
650 ddr->ddr_init_ext_addr = (0
651 | ((uia & 0x1) << 31)
652 | (init_ext_addr & 0xF)
656 /* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */
657 static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr)
659 unsigned int rwt = 0; /* Read-to-write turnaround for same CS */
660 unsigned int wrt = 0; /* Write-to-read turnaround for same CS */
661 unsigned int rrt = 0; /* Read-to-read turnaround for same CS */
662 unsigned int wwt = 0; /* Write-to-write turnaround for same CS */
663 unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */
665 ddr->timing_cfg_4 = (0
666 | ((rwt & 0xf) << 28)
667 | ((wrt & 0xf) << 24)
668 | ((rrt & 0xf) << 20)
669 | ((wwt & 0xf) << 16)
674 /* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */
675 static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr)
677 unsigned int rodt_on = 0; /* Read to ODT on */
678 unsigned int rodt_off = 0; /* Read to ODT off */
679 unsigned int wodt_on = 0; /* Write to ODT on */
680 unsigned int wodt_off = 0; /* Write to ODT off */
682 ddr->timing_cfg_5 = (0
683 | ((rodt_on & 0xf) << 24)
684 | ((rodt_off & 0xf) << 20)
685 | ((wodt_on & 0xf) << 12)
686 | ((wodt_off & 0xf) << 8)
690 /* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */
691 static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr)
693 unsigned int zq_en = 0; /* ZQ Calibration Enable */
694 unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */
695 /* Normal Operation Full Calibration Time (tZQoper) */
696 unsigned int zqoper = 0;
697 /* Normal Operation Short Calibration Time (tZQCS) */
698 unsigned int zqcs = 0;
700 ddr->ddr_zq_cntl = (0
701 | ((zq_en & 0x1) << 31)
702 | ((zqinit & 0xF) << 24)
703 | ((zqoper & 0xF) << 16)
704 | ((zqcs & 0xF) << 8)
708 /* DDR Write Leveling Control (DDR_WRLVL_CNTL) */
709 static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr)
711 unsigned int wrlvl_en = 0; /* Write Leveling Enable */
713 * First DQS pulse rising edge after margining mode
714 * is programmed (tWL_MRD)
716 unsigned int wrlvl_mrd = 0;
717 /* ODT delay after margining mode is programmed (tWL_ODTEN) */
718 unsigned int wrlvl_odten = 0;
719 /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */
720 unsigned int wrlvl_dqsen = 0;
721 /* WRLVL_SMPL: Write leveling sample time */
722 unsigned int wrlvl_smpl = 0;
723 /* WRLVL_WLR: Write leveling repeition time */
724 unsigned int wrlvl_wlr = 0;
725 /* WRLVL_START: Write leveling start time */
726 unsigned int wrlvl_start = 0;
728 ddr->ddr_wrlvl_cntl = (0
729 | ((wrlvl_en & 0x1) << 31)
730 | ((wrlvl_mrd & 0x7) << 24)
731 | ((wrlvl_odten & 0x7) << 20)
732 | ((wrlvl_dqsen & 0x7) << 16)
733 | ((wrlvl_smpl & 0xf) << 12)
734 | ((wrlvl_wlr & 0x7) << 8)
735 | ((wrlvl_start & 0xF) << 0)
739 /* DDR Self Refresh Counter (DDR_SR_CNTR) */
740 static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr)
742 unsigned int sr_it = 0; /* Self Refresh Idle Threshold */
744 ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
747 /* DDR Pre-Drive Conditioning Control (DDR_PD_CNTL) */
748 static void set_ddr_pd_cntl(fsl_ddr_cfg_regs_t *ddr)
750 /* Termination value during pre-drive conditioning */
751 unsigned int tvpd = 0;
752 unsigned int pd_en = 0; /* Pre-Drive Conditioning Enable */
753 unsigned int pdar = 0; /* Pre-Drive After Read */
754 unsigned int pdaw = 0; /* Pre-Drive After Write */
755 unsigned int pd_on = 0; /* Pre-Drive Conditioning On */
756 unsigned int pd_off = 0; /* Pre-Drive Conditioning Off */
758 ddr->ddr_pd_cntl = (0
759 | ((pd_en & 0x1) << 31)
760 | ((tvpd & 0x7) << 28)
761 | ((pdar & 0x7F) << 20)
762 | ((pdaw & 0x7F) << 12)
763 | ((pd_on & 0x1F) << 6)
764 | ((pd_off & 0x1F) << 0)
769 /* DDR SDRAM Register Control Word 1 (DDR_SDRAM_RCW_1) */
770 static void set_ddr_sdram_rcw_1(fsl_ddr_cfg_regs_t *ddr)
772 unsigned int rcw0 = 0; /* RCW0: Register Control Word 0 */
773 unsigned int rcw1 = 0; /* RCW1: Register Control Word 1 */
774 unsigned int rcw2 = 0; /* RCW2: Register Control Word 2 */
775 unsigned int rcw3 = 0; /* RCW3: Register Control Word 3 */
776 unsigned int rcw4 = 0; /* RCW4: Register Control Word 4 */
777 unsigned int rcw5 = 0; /* RCW5: Register Control Word 5 */
778 unsigned int rcw6 = 0; /* RCW6: Register Control Word 6 */
779 unsigned int rcw7 = 0; /* RCW7: Register Control Word 7 */
781 ddr->ddr_sdram_rcw_1 = (0
782 | ((rcw0 & 0xF) << 28)
783 | ((rcw1 & 0xF) << 24)
784 | ((rcw2 & 0xF) << 20)
785 | ((rcw3 & 0xF) << 16)
786 | ((rcw4 & 0xF) << 12)
787 | ((rcw5 & 0xF) << 8)
788 | ((rcw6 & 0xF) << 4)
789 | ((rcw7 & 0xF) << 0)
793 /* DDR SDRAM Register Control Word 2 (DDR_SDRAM_RCW_2) */
794 static void set_ddr_sdram_rcw_2(fsl_ddr_cfg_regs_t *ddr)
796 unsigned int rcw8 = 0; /* RCW0: Register Control Word 8 */
797 unsigned int rcw9 = 0; /* RCW1: Register Control Word 9 */
798 unsigned int rcw10 = 0; /* RCW2: Register Control Word 10 */
799 unsigned int rcw11 = 0; /* RCW3: Register Control Word 11 */
800 unsigned int rcw12 = 0; /* RCW4: Register Control Word 12 */
801 unsigned int rcw13 = 0; /* RCW5: Register Control Word 13 */
802 unsigned int rcw14 = 0; /* RCW6: Register Control Word 14 */
803 unsigned int rcw15 = 0; /* RCW7: Register Control Word 15 */
805 ddr->ddr_sdram_rcw_2 = (0
806 | ((rcw8 & 0xF) << 28)
807 | ((rcw9 & 0xF) << 24)
808 | ((rcw10 & 0xF) << 20)
809 | ((rcw11 & 0xF) << 16)
810 | ((rcw12 & 0xF) << 12)
811 | ((rcw13 & 0xF) << 8)
812 | ((rcw14 & 0xF) << 4)
813 | ((rcw15 & 0xF) << 0)
818 check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
820 unsigned int res = 0;
823 * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are
824 * not set at the same time.
826 if (ddr->ddr_sdram_cfg & 0x10000000
827 && ddr->ddr_sdram_cfg & 0x00008000) {
828 printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
829 " should not be set at the same time.\n");
837 compute_fsl_memctl_config_regs(const memctl_options_t *popts,
838 fsl_ddr_cfg_regs_t *ddr,
839 const common_timing_params_t *common_dimm,
840 const dimm_params_t *dimm_params,
841 unsigned int dbw_cap_adj)
844 unsigned int cas_latency;
845 unsigned int additive_latency;
847 memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
849 if (common_dimm == NULL) {
850 printf("Error: subset DIMM params struct null pointer\n");
855 * Process overrides first.
857 * FIXME: somehow add dereated caslat to this
859 cas_latency = (popts->cas_latency_override)
860 ? popts->cas_latency_override_value
861 : common_dimm->lowest_common_SPD_caslat;
863 additive_latency = (popts->additive_latency_override)
864 ? popts->additive_latency_override_value
865 : common_dimm->additive_latency;
867 /* Chip Select Memory Bounds (CSn_BNDS) */
868 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
872 if (popts->ba_intlv_ctl && (i > 0) &&
873 ((popts->ba_intlv_ctl & 0x60) != FSL_DDR_CS2_CS3 )) {
874 /* Don't set up boundaries for other CS
875 * other than CS0, if bank interleaving
876 * is enabled and not CS2+CS3 interleaved.
881 if (dimm_params[i/2].n_ranks == 0) {
882 debug("Skipping setup of CS%u "
883 "because n_ranks on DIMM %u is 0\n", i, i/2);
886 if (popts->memctl_interleaving && popts->ba_intlv_ctl) {
888 * This works superbank 2CS
889 * There are 2 memory controllers configured
890 * identically, memory is interleaved between them,
891 * and each controller uses rank interleaving within
892 * itself. Therefore the starting and ending address
893 * on each controller is twice the amount present on
896 unsigned long long rank_density
897 = dimm_params[0].capacity;
898 ea = (2 * (rank_density >> dbw_cap_adj)) - 1;
900 else if (!popts->memctl_interleaving && popts->ba_intlv_ctl) {
902 * If memory interleaving between controllers is NOT
903 * enabled, the starting address for each memory
904 * controller is distinct. However, because rank
905 * interleaving is enabled, the starting and ending
906 * addresses of the total memory on that memory
907 * controller needs to be programmed into its
908 * respective CS0_BNDS.
910 unsigned long long rank_density
911 = dimm_params[i/2].rank_density;
912 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
913 case FSL_DDR_CS0_CS1_CS2_CS3:
914 /* CS0+CS1+CS2+CS3 interleaving, only CS0_CNDS
917 sa = common_dimm->base_address;
918 ea = sa + (4 * (rank_density >> dbw_cap_adj))-1;
920 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
921 /* CS0+CS1 and CS2+CS3 interleaving, CS0_CNDS
922 * and CS2_CNDS need to be set.
925 sa = dimm_params[i/2].base_address;
926 ea = sa + (i * (rank_density >>
930 case FSL_DDR_CS0_CS1:
931 /* CS0+CS1 interleaving, CS0_CNDS needs
934 sa = common_dimm->base_address;
935 ea = sa + (2 * (rank_density >> dbw_cap_adj))-1;
937 case FSL_DDR_CS2_CS3:
938 /* CS2+CS3 interleaving*/
940 sa = dimm_params[i/2].base_address;
941 ea = sa + (2 * (rank_density >>
945 default: /* No bank(chip-select) interleaving */
949 else if (popts->memctl_interleaving && !popts->ba_intlv_ctl) {
951 * Only the rank on CS0 of each memory controller may
952 * be used if memory controller interleaving is used
953 * without rank interleaving within each memory
954 * controller. However, the ending address programmed
955 * into each CS0 must be the sum of the amount of
956 * memory in the two CS0 ranks.
959 unsigned long long rank_density
960 = dimm_params[0].rank_density;
961 ea = (2 * (rank_density >> dbw_cap_adj)) - 1;
965 else if (!popts->memctl_interleaving && !popts->ba_intlv_ctl) {
967 * No rank interleaving and no memory controller
970 unsigned long long rank_density
971 = dimm_params[i/2].rank_density;
972 sa = dimm_params[i/2].base_address;
973 ea = sa + (rank_density >> dbw_cap_adj) - 1;
975 if ((dimm_params[i/2].n_ranks == 1)) {
976 /* Odd chip select, single-rank dimm */
980 /* Odd chip select, dual-rank DIMM */
981 sa += rank_density >> dbw_cap_adj;
982 ea += rank_density >> dbw_cap_adj;
991 | ((sa & 0xFFF) << 16) /* starting address MSB */
992 | ((ea & 0xFFF) << 0) /* ending address MSB */
995 set_csn_config(i, ddr, popts, dimm_params);
996 set_csn_config_2(i, ddr);
999 #if defined(CONFIG_FSL_DDR2)
1000 set_timing_cfg_0(ddr);
1003 set_timing_cfg_3(ddr, common_dimm);
1004 set_timing_cfg_1(ddr, common_dimm, cas_latency);
1005 set_timing_cfg_2(ddr, popts, common_dimm,
1006 cas_latency, additive_latency);
1008 set_ddr_sdram_cfg(ddr, popts, common_dimm);
1010 set_ddr_sdram_cfg_2(ddr, popts);
1011 set_ddr_sdram_mode(ddr, popts, common_dimm,
1012 cas_latency, additive_latency);
1013 set_ddr_sdram_mode_2(ddr);
1014 set_ddr_sdram_interval(ddr, popts, common_dimm);
1015 set_ddr_data_init(ddr);
1016 set_ddr_sdram_clk_cntl(ddr, popts);
1017 set_ddr_init_addr(ddr);
1018 set_ddr_init_ext_addr(ddr);
1019 set_timing_cfg_4(ddr);
1020 set_timing_cfg_5(ddr);
1022 set_ddr_zq_cntl(ddr);
1023 set_ddr_wrlvl_cntl(ddr);
1025 set_ddr_pd_cntl(ddr);
1026 set_ddr_sr_cntr(ddr);
1028 set_ddr_sdram_rcw_1(ddr);
1029 set_ddr_sdram_rcw_2(ddr);
1031 return check_fsl_memctl_config_regs(ddr);