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 /* FIXME: intlv_en, intlv_ctl only on CS0_CONFIG */
104 ddr->cs[i].config = (0
105 | ((cs_n_en & 0x1) << 31)
106 | ((intlv_en & 0x3) << 29)
107 | ((intlv_en & 0xf) << 24)
108 | ((ap_n_en & 0x1) << 23)
110 /* XXX: some implementation only have 1 bit starting at left */
111 | ((odt_rd_cfg & 0x7) << 20)
113 /* XXX: Some implementation only have 1 bit starting at left */
114 | ((odt_wr_cfg & 0x7) << 16)
116 | ((ba_bits_cs_n & 0x3) << 14)
117 | ((row_bits_cs_n & 0x7) << 8)
118 | ((col_bits_cs_n & 0x7) << 0)
122 /* Chip Select Configuration 2 (CSn_CONFIG_2) */
124 static void set_csn_config_2(int i, fsl_ddr_cfg_regs_t *ddr)
126 unsigned int pasr_cfg = 0; /* Partial array self refresh config */
128 ddr->cs[i].config_2 = ((pasr_cfg & 7) << 24);
131 /* -3E = 667 CL5, -25 = CL6 800, -25E = CL5 800 */
133 #if defined(CONFIG_FSL_DDR2)
135 * DDR SDRAM Timing Configuration 0 (TIMING_CFG_0)
137 * Avoid writing for DDR I. The new PQ38 DDR controller
138 * dreams up non-zero default values to be backwards compatible.
140 static void set_timing_cfg_0(fsl_ddr_cfg_regs_t *ddr)
142 unsigned char trwt_mclk = 0; /* Read-to-write turnaround */
143 unsigned char twrt_mclk = 0; /* Write-to-read turnaround */
144 /* 7.5 ns on -3E; 0 means WL - CL + BL/2 + 1 */
145 unsigned char trrt_mclk = 0; /* Read-to-read turnaround */
146 unsigned char twwt_mclk = 0; /* Write-to-write turnaround */
148 /* Active powerdown exit timing (tXARD and tXARDS). */
149 unsigned char act_pd_exit_mclk;
150 /* Precharge powerdown exit timing (tXP). */
151 unsigned char pre_pd_exit_mclk;
152 /* Precharge powerdown exit timing (tAXPD). */
153 unsigned char taxpd_mclk;
154 /* Mode register set cycle time (tMRD). */
155 unsigned char tmrd_mclk;
157 /* (tXARD and tXARDS). Empirical? */
158 act_pd_exit_mclk = 2;
160 /* XXX: tXARD = 2, tXARDS = 7 - AL. * Empirical? */
161 pre_pd_exit_mclk = 6;
163 /* FIXME: tXP = 2 on Micron 667 MHz DIMM */
168 ddr->timing_cfg_0 = (0
169 | ((trwt_mclk & 0x3) << 30) /* RWT */
170 | ((twrt_mclk & 0x3) << 28) /* WRT */
171 | ((trrt_mclk & 0x3) << 26) /* RRT */
172 | ((twwt_mclk & 0x3) << 24) /* WWT */
173 | ((act_pd_exit_mclk & 0x7) << 20) /* ACT_PD_EXIT */
174 | ((pre_pd_exit_mclk & 0x7) << 16) /* PRE_PD_EXIT */
175 | ((taxpd_mclk & 0xf) << 8) /* ODT_PD_EXIT */
176 | ((tmrd_mclk & 0xf) << 0) /* MRS_CYC */
178 debug("FSLDDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
180 #endif /* defined(CONFIG_FSL_DDR2) */
182 /* DDR SDRAM Timing Configuration 3 (TIMING_CFG_3) */
183 static void set_timing_cfg_3(fsl_ddr_cfg_regs_t *ddr,
184 const common_timing_params_t *common_dimm)
186 /* Extended Activate to precharge interval (tRAS) */
187 unsigned int ext_acttopre = 0;
188 unsigned int ext_refrec; /* Extended refresh recovery time (tRFC) */
189 unsigned int ext_caslat = 0; /* Extended MCAS latency from READ cmd */
190 unsigned int cntl_adj = 0; /* Control Adjust */
192 ext_refrec = (picos_to_mclk(common_dimm->tRFC_ps) - 8) >> 4;
193 ddr->timing_cfg_3 = (0
194 | ((ext_acttopre & 0x1) << 24)
195 | ((ext_refrec & 0x7) << 16)
196 | ((ext_caslat & 0x1) << 12)
197 | ((cntl_adj & 0x7) << 0)
201 /* DDR SDRAM Timing Configuration 1 (TIMING_CFG_1) */
202 static void set_timing_cfg_1(fsl_ddr_cfg_regs_t *ddr,
203 const common_timing_params_t *common_dimm,
204 unsigned int cas_latency)
206 /* Precharge-to-activate interval (tRP) */
207 unsigned char pretoact_mclk;
208 /* Activate to precharge interval (tRAS) */
209 unsigned char acttopre_mclk;
210 /* Activate to read/write interval (tRCD) */
211 unsigned char acttorw_mclk;
213 unsigned char caslat_ctrl;
214 /* Refresh recovery time (tRFC) ; trfc_low */
215 unsigned char refrec_ctrl;
216 /* Last data to precharge minimum interval (tWR) */
217 unsigned char wrrec_mclk;
218 /* Activate-to-activate interval (tRRD) */
219 unsigned char acttoact_mclk;
220 /* Last write data pair to read command issue interval (tWTR) */
221 unsigned char wrtord_mclk;
223 pretoact_mclk = picos_to_mclk(common_dimm->tRP_ps);
224 acttopre_mclk = picos_to_mclk(common_dimm->tRAS_ps);
225 acttorw_mclk = picos_to_mclk(common_dimm->tRCD_ps);
228 * Translate CAS Latency to a DDR controller field value:
230 * CAS Lat DDR I DDR II Ctrl
231 * Clocks SPD Bit SPD Bit Value
232 * ------- ------- ------- -----
243 #if defined(CONFIG_FSL_DDR1)
244 caslat_ctrl = (cas_latency + 1) & 0x07;
245 #elif defined(CONFIG_FSL_DDR2)
246 caslat_ctrl = 2 * cas_latency - 1;
248 #error "Need CAS Latency help for DDR3 in fsl_ddr_sdram.c"
251 refrec_ctrl = picos_to_mclk(common_dimm->tRFC_ps) - 8;
252 wrrec_mclk = picos_to_mclk(common_dimm->tWR_ps);
253 acttoact_mclk = picos_to_mclk(common_dimm->tRRD_ps);
254 wrtord_mclk = picos_to_mclk(common_dimm->tWTR_ps);
256 ddr->timing_cfg_1 = (0
257 | ((pretoact_mclk & 0x07) << 28)
258 | ((acttopre_mclk & 0x0F) << 24)
259 | ((acttorw_mclk & 0x7) << 20)
260 | ((caslat_ctrl & 0xF) << 16)
261 | ((refrec_ctrl & 0xF) << 12)
262 | ((wrrec_mclk & 0x07) << 8)
263 | ((acttoact_mclk & 0x07) << 4)
264 | ((wrtord_mclk & 0x07) << 0)
268 /* DDR SDRAM Timing Configuration 2 (TIMING_CFG_2) */
269 static void set_timing_cfg_2(fsl_ddr_cfg_regs_t *ddr,
270 const memctl_options_t *popts,
271 const common_timing_params_t *common_dimm,
272 unsigned int cas_latency,
273 unsigned int additive_latency)
275 /* Additive latency */
276 unsigned char add_lat_mclk;
277 /* CAS-to-preamble override */
280 unsigned char wr_lat;
281 /* Read to precharge (tRTP) */
282 unsigned char rd_to_pre;
283 /* Write command to write data strobe timing adjustment */
284 unsigned char wr_data_delay;
285 /* Minimum CKE pulse width (tCKE) */
286 unsigned char cke_pls;
287 /* Window for four activates (tFAW) */
288 unsigned short four_act;
290 /* FIXME add check that this must be less than acttorw_mclk */
291 add_lat_mclk = additive_latency;
292 cpo = popts->cpo_override;
294 #if defined(CONFIG_FSL_DDR1)
296 * This is a lie. It should really be 1, but if it is
297 * set to 1, bits overlap into the old controller's
298 * otherwise unused ACSM field. If we leave it 0, then
299 * the HW will magically treat it as 1 for DDR 1. Oh Yea.
302 #elif defined(CONFIG_FSL_DDR2)
303 wr_lat = cas_latency + additive_latency - 1;
305 #error "Fix WR_LAT for DDR3"
308 rd_to_pre = picos_to_mclk(common_dimm->tRTP_ps);
309 wr_data_delay = popts->write_data_delay;
310 cke_pls = picos_to_mclk(popts->tCKE_clock_pulse_width_ps);
311 four_act = picos_to_mclk(popts->tFAW_window_four_activates_ps);
313 ddr->timing_cfg_2 = (0
314 | ((add_lat_mclk & 0x7) << 28)
315 | ((cpo & 0x1f) << 23)
316 | ((wr_lat & 0x7) << 19)
317 | ((rd_to_pre & 0x7) << 13)
318 | ((wr_data_delay & 0x7) << 10)
319 | ((cke_pls & 0x7) << 6)
320 | ((four_act & 0x1f) << 0)
324 /* DDR SDRAM control configuration (DDR_SDRAM_CFG) */
325 static void set_ddr_sdram_cfg(fsl_ddr_cfg_regs_t *ddr,
326 const memctl_options_t *popts,
327 const common_timing_params_t *common_dimm)
329 unsigned int mem_en; /* DDR SDRAM interface logic enable */
330 unsigned int sren; /* Self refresh enable (during sleep) */
331 unsigned int ecc_en; /* ECC enable. */
332 unsigned int rd_en; /* Registered DIMM enable */
333 unsigned int sdram_type; /* Type of SDRAM */
334 unsigned int dyn_pwr; /* Dynamic power management mode */
335 unsigned int dbw; /* DRAM dta bus width */
336 unsigned int eight_be; /* 8-beat burst enable */
337 unsigned int ncap = 0; /* Non-concurrent auto-precharge */
338 unsigned int threeT_en; /* Enable 3T timing */
339 unsigned int twoT_en; /* Enable 2T timing */
340 unsigned int ba_intlv_ctl; /* Bank (CS) interleaving control */
341 unsigned int x32_en = 0; /* x32 enable */
342 unsigned int pchb8 = 0; /* precharge bit 8 enable */
343 unsigned int hse; /* Global half strength override */
344 unsigned int mem_halt = 0; /* memory controller halt */
345 unsigned int bi = 0; /* Bypass initialization */
348 sren = popts->self_refresh_in_sleep;
349 if (common_dimm->all_DIMMs_ECC_capable) {
350 /* Allow setting of ECC only if all DIMMs are ECC. */
351 ecc_en = popts->ECC_mode;
356 rd_en = (common_dimm->all_DIMMs_registered
357 && !common_dimm->all_DIMMs_unbuffered);
359 sdram_type = CONFIG_FSL_SDRAM_TYPE;
361 dyn_pwr = popts->dynamic_power;
362 dbw = popts->data_bus_width;
363 eight_be = 0; /* always 0 for DDR2 */
364 threeT_en = popts->threeT_en;
365 twoT_en = popts->twoT_en;
366 ba_intlv_ctl = popts->ba_intlv_ctl;
367 hse = popts->half_strength_driver_enable;
369 ddr->ddr_sdram_cfg = (0
370 | ((mem_en & 0x1) << 31)
371 | ((sren & 0x1) << 30)
372 | ((ecc_en & 0x1) << 29)
373 | ((rd_en & 0x1) << 28)
374 | ((sdram_type & 0x7) << 24)
375 | ((dyn_pwr & 0x1) << 21)
376 | ((dbw & 0x3) << 19)
377 | ((eight_be & 0x1) << 18)
378 | ((ncap & 0x1) << 17)
379 | ((threeT_en & 0x1) << 16)
380 | ((twoT_en & 0x1) << 15)
381 | ((ba_intlv_ctl & 0x7F) << 8)
382 | ((x32_en & 0x1) << 5)
383 | ((pchb8 & 0x1) << 4)
385 | ((mem_halt & 0x1) << 1)
390 /* DDR SDRAM control configuration 2 (DDR_SDRAM_CFG_2) */
391 static void set_ddr_sdram_cfg_2(fsl_ddr_cfg_regs_t *ddr,
392 const memctl_options_t *popts)
394 unsigned int frc_sr = 0; /* Force self refresh */
395 unsigned int sr_ie = 0; /* Self-refresh interrupt enable */
396 unsigned int dll_rst_dis; /* DLL reset disable */
397 unsigned int dqs_cfg; /* DQS configuration */
398 unsigned int odt_cfg; /* ODT configuration */
399 unsigned int num_pr; /* Number of posted refreshes */
400 unsigned int obc_cfg; /* On-The-Fly Burst Chop Cfg */
401 unsigned int ap_en; /* Address Parity Enable */
402 unsigned int d_init; /* DRAM data initialization */
403 unsigned int rcw_en = 0; /* Register Control Word Enable */
404 unsigned int md_en = 0; /* Mirrored DIMM Enable */
406 dll_rst_dis = 1; /* Make this configurable */
407 dqs_cfg = popts->DQS_config;
408 if (popts->cs_local_opts[0].odt_rd_cfg
409 || popts->cs_local_opts[0].odt_wr_cfg) {
416 num_pr = 1; /* Make this configurable */
420 * {TIMING_CFG_1[PRETOACT]
421 * + [DDR_SDRAM_CFG_2[NUM_PR]
422 * * ({EXT_REFREC || REFREC} + 8 + 2)]}
423 * << DDR_SDRAM_INTERVAL[REFINT]
426 obc_cfg = 0; /* Make this configurable? */
427 ap_en = 0; /* Make this configurable? */
429 #if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
430 /* Use the DDR controller to auto initialize memory. */
432 ddr->ddr_data_init = CONFIG_MEM_INIT_VALUE;
433 debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init);
435 /* Memory will be initialized via DMA, or not at all. */
439 ddr->ddr_sdram_cfg_2 = (0
440 | ((frc_sr & 0x1) << 31)
441 | ((sr_ie & 0x1) << 30)
442 | ((dll_rst_dis & 0x1) << 29)
443 | ((dqs_cfg & 0x3) << 26)
444 | ((odt_cfg & 0x3) << 21)
445 | ((num_pr & 0xf) << 12)
446 | ((obc_cfg & 0x1) << 6)
447 | ((ap_en & 0x1) << 5)
448 | ((d_init & 0x1) << 4)
449 | ((rcw_en & 0x1) << 2)
450 | ((md_en & 0x1) << 0)
454 /* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
455 static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr)
457 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
458 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
460 ddr->ddr_sdram_mode_2 = (0
461 | ((esdmode2 & 0xFFFF) << 16)
462 | ((esdmode3 & 0xFFFF) << 0)
466 /* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */
467 static void set_ddr_sdram_interval(fsl_ddr_cfg_regs_t *ddr,
468 const memctl_options_t *popts,
469 const common_timing_params_t *common_dimm)
471 unsigned int refint; /* Refresh interval */
472 unsigned int bstopre; /* Precharge interval */
474 refint = picos_to_mclk(common_dimm->refresh_rate_ps);
476 bstopre = popts->bstopre;
478 /* refint field used 0x3FFF in earlier controllers */
479 ddr->ddr_sdram_interval = (0
480 | ((refint & 0xFFFF) << 16)
481 | ((bstopre & 0x3FFF) << 0)
485 /* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
486 static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
487 const memctl_options_t *popts,
488 const common_timing_params_t *common_dimm,
489 unsigned int cas_latency,
490 unsigned int additive_latency)
492 unsigned short esdmode; /* Extended SDRAM mode */
493 unsigned short sdmode; /* SDRAM mode */
496 * FIXME: This ought to be pre-calculated in a
497 * technology-specific routine,
498 * e.g. compute_DDR2_mode_register(), and then the
499 * sdmode and esdmode passed in as part of common_dimm.
502 /* Extended Mode Register */
503 unsigned int mrs = 0; /* Mode Register Set */
504 unsigned int outputs = 0; /* 0=Enabled, 1=Disabled */
505 unsigned int rdqs_en = 0; /* RDQS Enable: 0=no, 1=yes */
506 unsigned int dqs_en = 0; /* DQS# Enable: 0=enable, 1=disable */
507 unsigned int ocd = 0; /* 0x0=OCD not supported,
508 0x7=OCD default state */
510 unsigned int al; /* Posted CAS# additive latency (AL) */
511 unsigned int ods = 0; /* Output Drive Strength:
512 0 = Full strength (18ohm)
513 1 = Reduced strength (4ohm) */
514 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
515 1=Disable (Test/Debug) */
517 /* Mode Register (MR) */
518 unsigned int mr; /* Mode Register Definition */
519 unsigned int pd; /* Power-Down Mode */
520 unsigned int wr; /* Write Recovery */
521 unsigned int dll_res; /* DLL Reset */
522 unsigned int mode; /* Normal=0 or Test=1 */
523 unsigned int caslat; /* CAS# latency */
524 /* BT: Burst Type (0=Sequential, 1=Interleaved) */
526 unsigned int bl; /* BL: Burst Length */
528 #if defined(CONFIG_FSL_DDR2)
529 const unsigned int mclk_ps = get_memory_clk_period_ps();
532 rtt = fsl_ddr_get_rtt();
534 al = additive_latency;
537 | ((mrs & 0x3) << 14)
538 | ((outputs & 0x1) << 12)
539 | ((rdqs_en & 0x1) << 11)
540 | ((dqs_en & 0x1) << 10)
542 | ((rtt & 0x2) << 5) /* rtt field is split */
544 | ((rtt & 0x1) << 2) /* rtt field is split */
546 | ((dll_en & 0x1) << 0)
549 mr = 0; /* FIXME: CHECKME */
552 * 0 = Fast Exit (Normal)
553 * 1 = Slow Exit (Low Power)
557 #if defined(CONFIG_FSL_DDR1)
558 wr = 0; /* Historical */
559 #elif defined(CONFIG_FSL_DDR2)
560 wr = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps - 1;
562 #error "Write tWR_auto for DDR3"
567 #if defined(CONFIG_FSL_DDR1)
568 if (1 <= cas_latency && cas_latency <= 4) {
569 unsigned char mode_caslat_table[4] = {
570 0x5, /* 1.5 clocks */
571 0x2, /* 2.0 clocks */
572 0x6, /* 2.5 clocks */
575 caslat = mode_caslat_table[cas_latency - 1];
577 #elif defined(CONFIG_FSL_DDR2)
578 caslat = cas_latency;
580 #error "Fix the mode CAS Latency for DDR3"
584 switch (popts->burst_length) {
592 printf("Error: invalid burst length of %u specified. "
593 " Defaulting to 4 beats.\n",
594 popts->burst_length);
603 | ((dll_res & 0x1) << 8)
604 | ((mode & 0x1) << 7)
605 | ((caslat & 0x7) << 4)
610 ddr->ddr_sdram_mode = (0
611 | ((esdmode & 0xFFFF) << 16)
612 | ((sdmode & 0xFFFF) << 0)
617 /* DDR SDRAM Data Initialization (DDR_DATA_INIT) */
618 static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr)
620 unsigned int init_value; /* Initialization value */
622 init_value = 0xDEADBEEF;
623 ddr->ddr_data_init = init_value;
627 * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL)
628 * The old controller on the 8540/60 doesn't have this register.
629 * Hope it's OK to set it (to 0) anyway.
631 static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
632 const memctl_options_t *popts)
634 unsigned int clk_adjust; /* Clock adjust */
636 clk_adjust = popts->clk_adjust;
637 ddr->ddr_sdram_clk_cntl = (clk_adjust & 0xF) << 23;
640 /* DDR Initialization Address (DDR_INIT_ADDR) */
641 static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
643 unsigned int init_addr = 0; /* Initialization address */
645 ddr->ddr_init_addr = init_addr;
648 /* DDR Initialization Address (DDR_INIT_EXT_ADDR) */
649 static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
651 unsigned int uia = 0; /* Use initialization address */
652 unsigned int init_ext_addr = 0; /* Initialization address */
654 ddr->ddr_init_ext_addr = (0
655 | ((uia & 0x1) << 31)
656 | (init_ext_addr & 0xF)
660 /* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */
661 static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr)
663 unsigned int rwt = 0; /* Read-to-write turnaround for same CS */
664 unsigned int wrt = 0; /* Write-to-read turnaround for same CS */
665 unsigned int rrt = 0; /* Read-to-read turnaround for same CS */
666 unsigned int wwt = 0; /* Write-to-write turnaround for same CS */
667 unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */
669 ddr->timing_cfg_4 = (0
670 | ((rwt & 0xf) << 28)
671 | ((wrt & 0xf) << 24)
672 | ((rrt & 0xf) << 20)
673 | ((wwt & 0xf) << 16)
678 /* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */
679 static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr)
681 unsigned int rodt_on = 0; /* Read to ODT on */
682 unsigned int rodt_off = 0; /* Read to ODT off */
683 unsigned int wodt_on = 0; /* Write to ODT on */
684 unsigned int wodt_off = 0; /* Write to ODT off */
686 ddr->timing_cfg_5 = (0
687 | ((rodt_on & 0xf) << 24)
688 | ((rodt_off & 0xf) << 20)
689 | ((wodt_on & 0xf) << 12)
690 | ((wodt_off & 0xf) << 8)
694 /* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */
695 static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr)
697 unsigned int zq_en = 0; /* ZQ Calibration Enable */
698 unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */
699 /* Normal Operation Full Calibration Time (tZQoper) */
700 unsigned int zqoper = 0;
701 /* Normal Operation Short Calibration Time (tZQCS) */
702 unsigned int zqcs = 0;
704 ddr->ddr_zq_cntl = (0
705 | ((zq_en & 0x1) << 31)
706 | ((zqinit & 0xF) << 24)
707 | ((zqoper & 0xF) << 16)
708 | ((zqcs & 0xF) << 8)
712 /* DDR Write Leveling Control (DDR_WRLVL_CNTL) */
713 static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr)
715 unsigned int wrlvl_en = 0; /* Write Leveling Enable */
717 * First DQS pulse rising edge after margining mode
718 * is programmed (tWL_MRD)
720 unsigned int wrlvl_mrd = 0;
721 /* ODT delay after margining mode is programmed (tWL_ODTEN) */
722 unsigned int wrlvl_odten = 0;
723 /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */
724 unsigned int wrlvl_dqsen = 0;
725 /* WRLVL_SMPL: Write leveling sample time */
726 unsigned int wrlvl_smpl = 0;
727 /* WRLVL_WLR: Write leveling repeition time */
728 unsigned int wrlvl_wlr = 0;
729 /* WRLVL_START: Write leveling start time */
730 unsigned int wrlvl_start = 0;
732 ddr->ddr_wrlvl_cntl = (0
733 | ((wrlvl_en & 0x1) << 31)
734 | ((wrlvl_mrd & 0x7) << 24)
735 | ((wrlvl_odten & 0x7) << 20)
736 | ((wrlvl_dqsen & 0x7) << 16)
737 | ((wrlvl_smpl & 0xf) << 12)
738 | ((wrlvl_wlr & 0x7) << 8)
739 | ((wrlvl_start & 0xF) << 0)
743 /* DDR Self Refresh Counter (DDR_SR_CNTR) */
744 static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr)
746 unsigned int sr_it = 0; /* Self Refresh Idle Threshold */
748 ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
751 /* DDR Pre-Drive Conditioning Control (DDR_PD_CNTL) */
752 static void set_ddr_pd_cntl(fsl_ddr_cfg_regs_t *ddr)
754 /* Termination value during pre-drive conditioning */
755 unsigned int tvpd = 0;
756 unsigned int pd_en = 0; /* Pre-Drive Conditioning Enable */
757 unsigned int pdar = 0; /* Pre-Drive After Read */
758 unsigned int pdaw = 0; /* Pre-Drive After Write */
759 unsigned int pd_on = 0; /* Pre-Drive Conditioning On */
760 unsigned int pd_off = 0; /* Pre-Drive Conditioning Off */
762 ddr->ddr_pd_cntl = (0
763 | ((pd_en & 0x1) << 31)
764 | ((tvpd & 0x7) << 28)
765 | ((pdar & 0x7F) << 20)
766 | ((pdaw & 0x7F) << 12)
767 | ((pd_on & 0x1F) << 6)
768 | ((pd_off & 0x1F) << 0)
773 /* DDR SDRAM Register Control Word 1 (DDR_SDRAM_RCW_1) */
774 static void set_ddr_sdram_rcw_1(fsl_ddr_cfg_regs_t *ddr)
776 unsigned int rcw0 = 0; /* RCW0: Register Control Word 0 */
777 unsigned int rcw1 = 0; /* RCW1: Register Control Word 1 */
778 unsigned int rcw2 = 0; /* RCW2: Register Control Word 2 */
779 unsigned int rcw3 = 0; /* RCW3: Register Control Word 3 */
780 unsigned int rcw4 = 0; /* RCW4: Register Control Word 4 */
781 unsigned int rcw5 = 0; /* RCW5: Register Control Word 5 */
782 unsigned int rcw6 = 0; /* RCW6: Register Control Word 6 */
783 unsigned int rcw7 = 0; /* RCW7: Register Control Word 7 */
785 ddr->ddr_sdram_rcw_1 = (0
786 | ((rcw0 & 0xF) << 28)
787 | ((rcw1 & 0xF) << 24)
788 | ((rcw2 & 0xF) << 20)
789 | ((rcw3 & 0xF) << 16)
790 | ((rcw4 & 0xF) << 12)
791 | ((rcw5 & 0xF) << 8)
792 | ((rcw6 & 0xF) << 4)
793 | ((rcw7 & 0xF) << 0)
797 /* DDR SDRAM Register Control Word 2 (DDR_SDRAM_RCW_2) */
798 static void set_ddr_sdram_rcw_2(fsl_ddr_cfg_regs_t *ddr)
800 unsigned int rcw8 = 0; /* RCW0: Register Control Word 8 */
801 unsigned int rcw9 = 0; /* RCW1: Register Control Word 9 */
802 unsigned int rcw10 = 0; /* RCW2: Register Control Word 10 */
803 unsigned int rcw11 = 0; /* RCW3: Register Control Word 11 */
804 unsigned int rcw12 = 0; /* RCW4: Register Control Word 12 */
805 unsigned int rcw13 = 0; /* RCW5: Register Control Word 13 */
806 unsigned int rcw14 = 0; /* RCW6: Register Control Word 14 */
807 unsigned int rcw15 = 0; /* RCW7: Register Control Word 15 */
809 ddr->ddr_sdram_rcw_2 = (0
810 | ((rcw8 & 0xF) << 28)
811 | ((rcw9 & 0xF) << 24)
812 | ((rcw10 & 0xF) << 20)
813 | ((rcw11 & 0xF) << 16)
814 | ((rcw12 & 0xF) << 12)
815 | ((rcw13 & 0xF) << 8)
816 | ((rcw14 & 0xF) << 4)
817 | ((rcw15 & 0xF) << 0)
822 check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
824 unsigned int res = 0;
827 * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are
828 * not set at the same time.
830 if (ddr->ddr_sdram_cfg & 0x10000000
831 && ddr->ddr_sdram_cfg & 0x00008000) {
832 printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
833 " should not be set at the same time.\n");
841 compute_fsl_memctl_config_regs(const memctl_options_t *popts,
842 fsl_ddr_cfg_regs_t *ddr,
843 const common_timing_params_t *common_dimm,
844 const dimm_params_t *dimm_params,
845 unsigned int dbw_cap_adj)
848 unsigned int cas_latency;
849 unsigned int additive_latency;
851 memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
853 if (common_dimm == NULL) {
854 printf("Error: subset DIMM params struct null pointer\n");
859 * Process overrides first.
861 * FIXME: somehow add dereated caslat to this
863 cas_latency = (popts->cas_latency_override)
864 ? popts->cas_latency_override_value
865 : common_dimm->lowest_common_SPD_caslat;
867 additive_latency = (popts->additive_latency_override)
868 ? popts->additive_latency_override_value
869 : common_dimm->additive_latency;
871 /* Chip Select Memory Bounds (CSn_BNDS) */
872 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
875 if (popts->ba_intlv_ctl && i > 0) {
876 /* Don't set up boundaries if bank interleaving */
880 if (dimm_params[i/2].n_ranks == 0) {
881 debug("Skipping setup of CS%u "
882 "because n_ranks on DIMM %u is 0\n", i, i/2);
885 if (popts->memctl_interleaving && popts->ba_intlv_ctl) {
887 * This works superbank 2CS
888 * There are 2 memory controllers configured
889 * identically, memory is interleaved between them,
890 * and each controller uses rank interleaving within
891 * itself. Therefore the starting and ending address
892 * on each controller is twice the amount present on
895 ea = (2 * common_dimm->total_mem >> dbw_cap_adj) - 1;
897 else if (!popts->memctl_interleaving && popts->ba_intlv_ctl) {
899 * If memory interleaving between controllers is NOT
900 * enabled, the starting address for each memory
901 * controller is distinct. However, because rank
902 * interleaving is enabled, the starting and ending
903 * addresses of the total memory on that memory
904 * controller needs to be programmed into its
905 * respective CS0_BNDS.
907 sa = common_dimm->base_address;
908 ea = sa + (common_dimm->total_mem >> dbw_cap_adj) - 1;
910 else if (popts->memctl_interleaving && !popts->ba_intlv_ctl) {
912 * Only the rank on CS0 of each memory controller may
913 * be used if memory controller interleaving is used
914 * without rank interleaving within each memory
915 * controller. However, the ending address programmed
916 * into each CS0 must be the sum of the amount of
917 * memory in the two CS0 ranks.
920 unsigned long long rank_density
921 = dimm_params[0].rank_density;
922 ea = (2 * (rank_density >> dbw_cap_adj)) - 1;
926 else if (!popts->memctl_interleaving && !popts->ba_intlv_ctl) {
928 * No rank interleaving and no memory controller
931 unsigned long long rank_density
932 = dimm_params[i/2].rank_density;
933 sa = dimm_params[i/2].base_address;
934 ea = sa + (rank_density >> dbw_cap_adj) - 1;
936 if ((dimm_params[i/2].n_ranks == 1)) {
937 /* Odd chip select, single-rank dimm */
941 /* Odd chip select, dual-rank DIMM */
942 sa += rank_density >> dbw_cap_adj;
943 ea += rank_density >> dbw_cap_adj;
952 | ((sa & 0xFFF) << 16) /* starting address MSB */
953 | ((ea & 0xFFF) << 0) /* ending address MSB */
956 set_csn_config(i, ddr, popts, dimm_params);
957 set_csn_config_2(i, ddr);
960 #if defined(CONFIG_FSL_DDR2)
961 set_timing_cfg_0(ddr);
964 set_timing_cfg_3(ddr, common_dimm);
965 set_timing_cfg_1(ddr, common_dimm, cas_latency);
966 set_timing_cfg_2(ddr, popts, common_dimm,
967 cas_latency, additive_latency);
969 set_ddr_sdram_cfg(ddr, popts, common_dimm);
971 set_ddr_sdram_cfg_2(ddr, popts);
972 set_ddr_sdram_mode(ddr, popts, common_dimm,
973 cas_latency, additive_latency);
974 set_ddr_sdram_mode_2(ddr);
975 set_ddr_sdram_interval(ddr, popts, common_dimm);
976 set_ddr_data_init(ddr);
977 set_ddr_sdram_clk_cntl(ddr, popts);
978 set_ddr_init_addr(ddr);
979 set_ddr_init_ext_addr(ddr);
980 set_timing_cfg_4(ddr);
981 set_timing_cfg_5(ddr);
983 set_ddr_zq_cntl(ddr);
984 set_ddr_wrlvl_cntl(ddr);
986 set_ddr_pd_cntl(ddr);
987 set_ddr_sr_cntr(ddr);
989 set_ddr_sdram_rcw_1(ddr);
990 set_ddr_sdram_rcw_2(ddr);
992 return check_fsl_memctl_config_regs(ddr);