blob: e6c2a5ce7cf03bc77d659c904c894cbad048eba9 [file] [log] [blame]
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001/*
2 * Copyright 2008 Freescale Semiconductor, Inc.
3 *
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.
7 */
8
9/*
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]
13 */
14
15#include <common.h>
16#include <asm/fsl_ddr_sdram.h>
17
18#include "ddr.h"
19
20extern unsigned int picos_to_mclk(unsigned int picos);
21/*
22 * Determine Rtt value.
23 *
24 * This should likely be either board or controller specific.
25 *
26 * Rtt(nominal):
27 * 0 = Rtt disabled
28 * 1 = 75 ohm
29 * 2 = 150 ohm
30 * 3 = 50 ohm
31 *
32 * FIXME: Apparently 8641 needs a value of 2
33 * FIXME: Old code seys if 667 MHz or higher, use 3 on 8572
34 *
35 * FIXME: There was some effort down this line earlier:
36 *
37 * unsigned int i;
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)) {
42 * rtt = 2;
43 * break;
44 * }
45 * }
46 */
47static inline int fsl_ddr_get_rtt(void)
48{
49 int rtt;
50
51#if defined(CONFIG_FSL_DDR1)
52 rtt = 0;
53#elif defined(CONFIG_FSL_DDR2)
54 rtt = 3;
55#else
56#error "Need Rtt value for DDR3"
57#endif
58
59 return rtt;
60}
61
62/* Chip Select Configuration (CSn_CONFIG) */
63static 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)
66{
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 */
76
77 /* Compute CS_CONFIG only for existing ranks of each DIMM. */
78 if ((((i&1) == 0)
79 && (dimm_params[i/2].n_ranks == 1))
80 || (dimm_params[i/2].n_ranks == 2)) {
81 unsigned int n_banks_per_sdram_device;
82 cs_n_en = 1;
83 if (i == 0) {
84 /* These fields only available in CS0_CONFIG */
85 intlv_en = popts->memctl_interleaving;
86 intlv_ctl = popts->memctl_interleaving_mode;
87 }
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;
96 }
97
98 /* FIXME: intlv_en, intlv_ctl only on CS0_CONFIG */
99 if (i != 0) {
100 intlv_en = 0;
101 intlv_ctl = 0;
102 }
103
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)
109
110 /* XXX: some implementation only have 1 bit starting at left */
111 | ((odt_rd_cfg & 0x7) << 20)
112
113 /* XXX: Some implementation only have 1 bit starting at left */
114 | ((odt_wr_cfg & 0x7) << 16)
115
116 | ((ba_bits_cs_n & 0x3) << 14)
117 | ((row_bits_cs_n & 0x7) << 8)
118 | ((col_bits_cs_n & 0x7) << 0)
119 );
120}
121
122/* Chip Select Configuration 2 (CSn_CONFIG_2) */
123/* FIXME: 8572 */
124static void set_csn_config_2(int i, fsl_ddr_cfg_regs_t *ddr)
125{
126 unsigned int pasr_cfg = 0; /* Partial array self refresh config */
127
128 ddr->cs[i].config_2 = ((pasr_cfg & 7) << 24);
129}
130
131/* -3E = 667 CL5, -25 = CL6 800, -25E = CL5 800 */
132
133#if defined(CONFIG_FSL_DDR2)
134/*
135 * DDR SDRAM Timing Configuration 0 (TIMING_CFG_0)
136 *
137 * Avoid writing for DDR I. The new PQ38 DDR controller
138 * dreams up non-zero default values to be backwards compatible.
139 */
140static void set_timing_cfg_0(fsl_ddr_cfg_regs_t *ddr)
141{
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 */
147
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;
156
157 /* (tXARD and tXARDS). Empirical? */
158 act_pd_exit_mclk = 2;
159
160 /* XXX: tXARD = 2, tXARDS = 7 - AL. * Empirical? */
161 pre_pd_exit_mclk = 6;
162
163 /* FIXME: tXP = 2 on Micron 667 MHz DIMM */
164 taxpd_mclk = 8;
165
166 tmrd_mclk = 2;
167
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 */
177 );
178 debug("FSLDDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
179}
180#endif /* defined(CONFIG_FSL_DDR2) */
181
182/* DDR SDRAM Timing Configuration 3 (TIMING_CFG_3) */
183static void set_timing_cfg_3(fsl_ddr_cfg_regs_t *ddr,
184 const common_timing_params_t *common_dimm)
185{
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 */
191
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)
198 );
199}
200
201/* DDR SDRAM Timing Configuration 1 (TIMING_CFG_1) */
202static void set_timing_cfg_1(fsl_ddr_cfg_regs_t *ddr,
203 const common_timing_params_t *common_dimm,
204 unsigned int cas_latency)
205{
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;
212 /* CASLAT */
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;
222
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);
226
227 /*
228 * Translate CAS Latency to a DDR controller field value:
229 *
230 * CAS Lat DDR I DDR II Ctrl
231 * Clocks SPD Bit SPD Bit Value
232 * ------- ------- ------- -----
233 * 1.0 0 0001
234 * 1.5 1 0010
235 * 2.0 2 2 0011
236 * 2.5 3 0100
237 * 3.0 4 3 0101
238 * 3.5 5 0110
239 * 4.0 4 0111
240 * 4.5 1000
241 * 5.0 5 1001
242 */
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;
247#else
248#error "Need CAS Latency help for DDR3 in fsl_ddr_sdram.c"
249#endif
250
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);
255
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)
265 );
266}
267
268/* DDR SDRAM Timing Configuration 2 (TIMING_CFG_2) */
269static 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)
274{
275 /* Additive latency */
276 unsigned char add_lat_mclk;
277 /* CAS-to-preamble override */
278 unsigned short cpo;
279 /* Write latency */
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;
289
290 /* FIXME add check that this must be less than acttorw_mclk */
291 add_lat_mclk = additive_latency;
292 cpo = popts->cpo_override;
293
294#if defined(CONFIG_FSL_DDR1)
295 /*
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.
300 */
301 wr_lat = 0;
302#elif defined(CONFIG_FSL_DDR2)
303 wr_lat = cas_latency + additive_latency - 1;
304#else
305#error "Fix WR_LAT for DDR3"
306#endif
307
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);
312
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)
321 );
322}
323
324/* DDR SDRAM control configuration (DDR_SDRAM_CFG) */
325static 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)
328{
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 */
346
347 mem_en = 1;
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;
352 } else {
353 ecc_en = 0;
354 }
355
356 rd_en = (common_dimm->all_DIMMs_registered
357 && !common_dimm->all_DIMMs_unbuffered);
358
359 sdram_type = CONFIG_FSL_SDRAM_TYPE;
360
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;
368
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)
384 | ((hse & 0x1) << 3)
385 | ((mem_halt & 0x1) << 1)
386 | ((bi & 0x1) << 0)
387 );
388}
389
390/* DDR SDRAM control configuration 2 (DDR_SDRAM_CFG_2) */
391static void set_ddr_sdram_cfg_2(fsl_ddr_cfg_regs_t *ddr,
392 const memctl_options_t *popts)
393{
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 */
405
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) {
410 /* FIXME */
411 odt_cfg = 2;
412 } else {
413 odt_cfg = 0;
414 }
415
416 num_pr = 1; /* Make this configurable */
417
418 /*
419 * 8572 manual says
420 * {TIMING_CFG_1[PRETOACT]
421 * + [DDR_SDRAM_CFG_2[NUM_PR]
422 * * ({EXT_REFREC || REFREC} + 8 + 2)]}
423 * << DDR_SDRAM_INTERVAL[REFINT]
424 */
425
426 obc_cfg = 0; /* Make this configurable? */
427 ap_en = 0; /* Make this configurable? */
428
429#if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
430 /* Use the DDR controller to auto initialize memory. */
431 d_init = 1;
432 ddr->ddr_data_init = CONFIG_MEM_INIT_VALUE;
433 debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init);
434#else
435 /* Memory will be initialized via DMA, or not at all. */
436 d_init = 0;
437#endif
438
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)
451 );
452}
453
454/* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
455static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr)
456{
457 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
458 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
459
460 ddr->ddr_sdram_mode_2 = (0
461 | ((esdmode2 & 0xFFFF) << 16)
462 | ((esdmode3 & 0xFFFF) << 0)
463 );
464}
465
466/* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */
467static 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)
470{
471 unsigned int refint; /* Refresh interval */
472 unsigned int bstopre; /* Precharge interval */
473
474 refint = picos_to_mclk(common_dimm->refresh_rate_ps);
475
476 bstopre = popts->bstopre;
477
478 /* refint field used 0x3FFF in earlier controllers */
479 ddr->ddr_sdram_interval = (0
480 | ((refint & 0xFFFF) << 16)
481 | ((bstopre & 0x3FFF) << 0)
482 );
483}
484
485/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
486static 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)
491{
492 unsigned short esdmode; /* Extended SDRAM mode */
493 unsigned short sdmode; /* SDRAM mode */
494
495 /*
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.
500 */
501
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 */
509 unsigned int rtt;
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) */
516
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 */
Kumar Gala302e52e2008-09-05 14:40:29 -0500523 unsigned int caslat = 0;/* CAS# latency */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500524 /* BT: Burst Type (0=Sequential, 1=Interleaved) */
525 unsigned int bt;
526 unsigned int bl; /* BL: Burst Length */
527
528#if defined(CONFIG_FSL_DDR2)
529 const unsigned int mclk_ps = get_memory_clk_period_ps();
530#endif
531
532 rtt = fsl_ddr_get_rtt();
533
534 al = additive_latency;
535
536 esdmode = (0
537 | ((mrs & 0x3) << 14)
538 | ((outputs & 0x1) << 12)
539 | ((rdqs_en & 0x1) << 11)
540 | ((dqs_en & 0x1) << 10)
541 | ((ocd & 0x7) << 7)
542 | ((rtt & 0x2) << 5) /* rtt field is split */
543 | ((al & 0x7) << 3)
544 | ((rtt & 0x1) << 2) /* rtt field is split */
545 | ((ods & 0x1) << 1)
546 | ((dll_en & 0x1) << 0)
547 );
548
549 mr = 0; /* FIXME: CHECKME */
550
551 /*
552 * 0 = Fast Exit (Normal)
553 * 1 = Slow Exit (Low Power)
554 */
555 pd = 0;
556
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;
561#else
562#error "Write tWR_auto for DDR3"
563#endif
564 dll_res = 0;
565 mode = 0;
566
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 */
573 0x3 /* 3.0 clocks */
574 };
Kumar Gala302e52e2008-09-05 14:40:29 -0500575 caslat = mode_caslat_table[cas_latency - 1];
576 } else {
577 printf("Warning: unknown cas_latency %d\n", cas_latency);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500578 }
579#elif defined(CONFIG_FSL_DDR2)
580 caslat = cas_latency;
581#else
582#error "Fix the mode CAS Latency for DDR3"
583#endif
584 bt = 0;
585
586 switch (popts->burst_length) {
587 case 4:
588 bl = 2;
589 break;
590 case 8:
591 bl = 3;
592 break;
593 default:
594 printf("Error: invalid burst length of %u specified. "
595 " Defaulting to 4 beats.\n",
596 popts->burst_length);
597 bl = 2;
598 break;
599 }
600
601 sdmode = (0
602 | ((mr & 0x3) << 14)
603 | ((pd & 0x1) << 12)
604 | ((wr & 0x7) << 9)
605 | ((dll_res & 0x1) << 8)
606 | ((mode & 0x1) << 7)
607 | ((caslat & 0x7) << 4)
608 | ((bt & 0x1) << 3)
609 | ((bl & 0x7) << 0)
610 );
611
612 ddr->ddr_sdram_mode = (0
613 | ((esdmode & 0xFFFF) << 16)
614 | ((sdmode & 0xFFFF) << 0)
615 );
616}
617
618
619/* DDR SDRAM Data Initialization (DDR_DATA_INIT) */
620static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr)
621{
622 unsigned int init_value; /* Initialization value */
623
624 init_value = 0xDEADBEEF;
625 ddr->ddr_data_init = init_value;
626}
627
628/*
629 * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL)
630 * The old controller on the 8540/60 doesn't have this register.
631 * Hope it's OK to set it (to 0) anyway.
632 */
633static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
634 const memctl_options_t *popts)
635{
636 unsigned int clk_adjust; /* Clock adjust */
637
638 clk_adjust = popts->clk_adjust;
639 ddr->ddr_sdram_clk_cntl = (clk_adjust & 0xF) << 23;
640}
641
642/* DDR Initialization Address (DDR_INIT_ADDR) */
643static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
644{
645 unsigned int init_addr = 0; /* Initialization address */
646
647 ddr->ddr_init_addr = init_addr;
648}
649
650/* DDR Initialization Address (DDR_INIT_EXT_ADDR) */
651static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
652{
653 unsigned int uia = 0; /* Use initialization address */
654 unsigned int init_ext_addr = 0; /* Initialization address */
655
656 ddr->ddr_init_ext_addr = (0
657 | ((uia & 0x1) << 31)
658 | (init_ext_addr & 0xF)
659 );
660}
661
662/* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */
663static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr)
664{
665 unsigned int rwt = 0; /* Read-to-write turnaround for same CS */
666 unsigned int wrt = 0; /* Write-to-read turnaround for same CS */
667 unsigned int rrt = 0; /* Read-to-read turnaround for same CS */
668 unsigned int wwt = 0; /* Write-to-write turnaround for same CS */
669 unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */
670
671 ddr->timing_cfg_4 = (0
672 | ((rwt & 0xf) << 28)
673 | ((wrt & 0xf) << 24)
674 | ((rrt & 0xf) << 20)
675 | ((wwt & 0xf) << 16)
676 | (dll_lock & 0x3)
677 );
678}
679
680/* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */
681static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr)
682{
683 unsigned int rodt_on = 0; /* Read to ODT on */
684 unsigned int rodt_off = 0; /* Read to ODT off */
685 unsigned int wodt_on = 0; /* Write to ODT on */
686 unsigned int wodt_off = 0; /* Write to ODT off */
687
688 ddr->timing_cfg_5 = (0
689 | ((rodt_on & 0xf) << 24)
690 | ((rodt_off & 0xf) << 20)
691 | ((wodt_on & 0xf) << 12)
692 | ((wodt_off & 0xf) << 8)
693 );
694}
695
696/* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */
697static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr)
698{
699 unsigned int zq_en = 0; /* ZQ Calibration Enable */
700 unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */
701 /* Normal Operation Full Calibration Time (tZQoper) */
702 unsigned int zqoper = 0;
703 /* Normal Operation Short Calibration Time (tZQCS) */
704 unsigned int zqcs = 0;
705
706 ddr->ddr_zq_cntl = (0
707 | ((zq_en & 0x1) << 31)
708 | ((zqinit & 0xF) << 24)
709 | ((zqoper & 0xF) << 16)
710 | ((zqcs & 0xF) << 8)
711 );
712}
713
714/* DDR Write Leveling Control (DDR_WRLVL_CNTL) */
715static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr)
716{
717 unsigned int wrlvl_en = 0; /* Write Leveling Enable */
718 /*
719 * First DQS pulse rising edge after margining mode
720 * is programmed (tWL_MRD)
721 */
722 unsigned int wrlvl_mrd = 0;
723 /* ODT delay after margining mode is programmed (tWL_ODTEN) */
724 unsigned int wrlvl_odten = 0;
725 /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */
726 unsigned int wrlvl_dqsen = 0;
727 /* WRLVL_SMPL: Write leveling sample time */
728 unsigned int wrlvl_smpl = 0;
729 /* WRLVL_WLR: Write leveling repeition time */
730 unsigned int wrlvl_wlr = 0;
731 /* WRLVL_START: Write leveling start time */
732 unsigned int wrlvl_start = 0;
733
734 ddr->ddr_wrlvl_cntl = (0
735 | ((wrlvl_en & 0x1) << 31)
736 | ((wrlvl_mrd & 0x7) << 24)
737 | ((wrlvl_odten & 0x7) << 20)
738 | ((wrlvl_dqsen & 0x7) << 16)
739 | ((wrlvl_smpl & 0xf) << 12)
740 | ((wrlvl_wlr & 0x7) << 8)
741 | ((wrlvl_start & 0xF) << 0)
742 );
743}
744
745/* DDR Self Refresh Counter (DDR_SR_CNTR) */
746static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr)
747{
748 unsigned int sr_it = 0; /* Self Refresh Idle Threshold */
749
750 ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
751}
752
753/* DDR Pre-Drive Conditioning Control (DDR_PD_CNTL) */
754static void set_ddr_pd_cntl(fsl_ddr_cfg_regs_t *ddr)
755{
756 /* Termination value during pre-drive conditioning */
757 unsigned int tvpd = 0;
758 unsigned int pd_en = 0; /* Pre-Drive Conditioning Enable */
759 unsigned int pdar = 0; /* Pre-Drive After Read */
760 unsigned int pdaw = 0; /* Pre-Drive After Write */
761 unsigned int pd_on = 0; /* Pre-Drive Conditioning On */
762 unsigned int pd_off = 0; /* Pre-Drive Conditioning Off */
763
764 ddr->ddr_pd_cntl = (0
765 | ((pd_en & 0x1) << 31)
766 | ((tvpd & 0x7) << 28)
767 | ((pdar & 0x7F) << 20)
768 | ((pdaw & 0x7F) << 12)
769 | ((pd_on & 0x1F) << 6)
770 | ((pd_off & 0x1F) << 0)
771 );
772}
773
774
775/* DDR SDRAM Register Control Word 1 (DDR_SDRAM_RCW_1) */
776static void set_ddr_sdram_rcw_1(fsl_ddr_cfg_regs_t *ddr)
777{
778 unsigned int rcw0 = 0; /* RCW0: Register Control Word 0 */
779 unsigned int rcw1 = 0; /* RCW1: Register Control Word 1 */
780 unsigned int rcw2 = 0; /* RCW2: Register Control Word 2 */
781 unsigned int rcw3 = 0; /* RCW3: Register Control Word 3 */
782 unsigned int rcw4 = 0; /* RCW4: Register Control Word 4 */
783 unsigned int rcw5 = 0; /* RCW5: Register Control Word 5 */
784 unsigned int rcw6 = 0; /* RCW6: Register Control Word 6 */
785 unsigned int rcw7 = 0; /* RCW7: Register Control Word 7 */
786
787 ddr->ddr_sdram_rcw_1 = (0
788 | ((rcw0 & 0xF) << 28)
789 | ((rcw1 & 0xF) << 24)
790 | ((rcw2 & 0xF) << 20)
791 | ((rcw3 & 0xF) << 16)
792 | ((rcw4 & 0xF) << 12)
793 | ((rcw5 & 0xF) << 8)
794 | ((rcw6 & 0xF) << 4)
795 | ((rcw7 & 0xF) << 0)
796 );
797}
798
799/* DDR SDRAM Register Control Word 2 (DDR_SDRAM_RCW_2) */
800static void set_ddr_sdram_rcw_2(fsl_ddr_cfg_regs_t *ddr)
801{
802 unsigned int rcw8 = 0; /* RCW0: Register Control Word 8 */
803 unsigned int rcw9 = 0; /* RCW1: Register Control Word 9 */
804 unsigned int rcw10 = 0; /* RCW2: Register Control Word 10 */
805 unsigned int rcw11 = 0; /* RCW3: Register Control Word 11 */
806 unsigned int rcw12 = 0; /* RCW4: Register Control Word 12 */
807 unsigned int rcw13 = 0; /* RCW5: Register Control Word 13 */
808 unsigned int rcw14 = 0; /* RCW6: Register Control Word 14 */
809 unsigned int rcw15 = 0; /* RCW7: Register Control Word 15 */
810
811 ddr->ddr_sdram_rcw_2 = (0
812 | ((rcw8 & 0xF) << 28)
813 | ((rcw9 & 0xF) << 24)
814 | ((rcw10 & 0xF) << 20)
815 | ((rcw11 & 0xF) << 16)
816 | ((rcw12 & 0xF) << 12)
817 | ((rcw13 & 0xF) << 8)
818 | ((rcw14 & 0xF) << 4)
819 | ((rcw15 & 0xF) << 0)
820 );
821}
822
823unsigned int
824check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
825{
826 unsigned int res = 0;
827
828 /*
829 * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are
830 * not set at the same time.
831 */
832 if (ddr->ddr_sdram_cfg & 0x10000000
833 && ddr->ddr_sdram_cfg & 0x00008000) {
834 printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
835 " should not be set at the same time.\n");
836 res++;
837 }
838
839 return res;
840}
841
842unsigned int
843compute_fsl_memctl_config_regs(const memctl_options_t *popts,
844 fsl_ddr_cfg_regs_t *ddr,
845 const common_timing_params_t *common_dimm,
846 const dimm_params_t *dimm_params,
847 unsigned int dbw_cap_adj)
848{
849 unsigned int i;
850 unsigned int cas_latency;
851 unsigned int additive_latency;
852
853 memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
854
855 if (common_dimm == NULL) {
856 printf("Error: subset DIMM params struct null pointer\n");
857 return 1;
858 }
859
860 /*
861 * Process overrides first.
862 *
863 * FIXME: somehow add dereated caslat to this
864 */
865 cas_latency = (popts->cas_latency_override)
866 ? popts->cas_latency_override_value
867 : common_dimm->lowest_common_SPD_caslat;
868
869 additive_latency = (popts->additive_latency_override)
870 ? popts->additive_latency_override_value
871 : common_dimm->additive_latency;
872
873 /* Chip Select Memory Bounds (CSn_BNDS) */
874 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
875 phys_size_t sa = 0;
876 phys_size_t ea = 0;
877 if (popts->ba_intlv_ctl && i > 0) {
878 /* Don't set up boundaries if bank interleaving */
879 break;
880 }
881
882 if (dimm_params[i/2].n_ranks == 0) {
883 debug("Skipping setup of CS%u "
884 "because n_ranks on DIMM %u is 0\n", i, i/2);
885 continue;
886 }
887 if (popts->memctl_interleaving && popts->ba_intlv_ctl) {
888 /*
889 * This works superbank 2CS
890 * There are 2 memory controllers configured
891 * identically, memory is interleaved between them,
892 * and each controller uses rank interleaving within
893 * itself. Therefore the starting and ending address
894 * on each controller is twice the amount present on
895 * each controller.
896 */
897 ea = (2 * common_dimm->total_mem >> dbw_cap_adj) - 1;
898 }
899 else if (!popts->memctl_interleaving && popts->ba_intlv_ctl) {
900 /*
901 * If memory interleaving between controllers is NOT
902 * enabled, the starting address for each memory
903 * controller is distinct. However, because rank
904 * interleaving is enabled, the starting and ending
905 * addresses of the total memory on that memory
906 * controller needs to be programmed into its
907 * respective CS0_BNDS.
908 */
909 sa = common_dimm->base_address;
910 ea = sa + (common_dimm->total_mem >> dbw_cap_adj) - 1;
911 }
912 else if (popts->memctl_interleaving && !popts->ba_intlv_ctl) {
913 /*
914 * Only the rank on CS0 of each memory controller may
915 * be used if memory controller interleaving is used
916 * without rank interleaving within each memory
917 * controller. However, the ending address programmed
918 * into each CS0 must be the sum of the amount of
919 * memory in the two CS0 ranks.
920 */
921 if (i == 0) {
922 unsigned long long rank_density
923 = dimm_params[0].rank_density;
924 ea = (2 * (rank_density >> dbw_cap_adj)) - 1;
925 }
926
927 }
928 else if (!popts->memctl_interleaving && !popts->ba_intlv_ctl) {
929 /*
930 * No rank interleaving and no memory controller
931 * interleaving.
932 */
933 unsigned long long rank_density
934 = dimm_params[i/2].rank_density;
935 sa = dimm_params[i/2].base_address;
936 ea = sa + (rank_density >> dbw_cap_adj) - 1;
937 if (i&1) {
938 if ((dimm_params[i/2].n_ranks == 1)) {
939 /* Odd chip select, single-rank dimm */
940 sa = 0;
941 ea = 0;
942 } else {
943 /* Odd chip select, dual-rank DIMM */
944 sa += rank_density >> dbw_cap_adj;
945 ea += rank_density >> dbw_cap_adj;
946 }
947 }
948 }
949
950 sa >>= 24;
951 ea >>= 24;
952
953 ddr->cs[i].bnds = (0
954 | ((sa & 0xFFF) << 16) /* starting address MSB */
955 | ((ea & 0xFFF) << 0) /* ending address MSB */
956 );
957
958 set_csn_config(i, ddr, popts, dimm_params);
959 set_csn_config_2(i, ddr);
960 }
961
962#if defined(CONFIG_FSL_DDR2)
963 set_timing_cfg_0(ddr);
964#endif
965
966 set_timing_cfg_3(ddr, common_dimm);
967 set_timing_cfg_1(ddr, common_dimm, cas_latency);
968 set_timing_cfg_2(ddr, popts, common_dimm,
969 cas_latency, additive_latency);
970
971 set_ddr_sdram_cfg(ddr, popts, common_dimm);
972
973 set_ddr_sdram_cfg_2(ddr, popts);
974 set_ddr_sdram_mode(ddr, popts, common_dimm,
975 cas_latency, additive_latency);
976 set_ddr_sdram_mode_2(ddr);
977 set_ddr_sdram_interval(ddr, popts, common_dimm);
978 set_ddr_data_init(ddr);
979 set_ddr_sdram_clk_cntl(ddr, popts);
980 set_ddr_init_addr(ddr);
981 set_ddr_init_ext_addr(ddr);
982 set_timing_cfg_4(ddr);
983 set_timing_cfg_5(ddr);
984
985 set_ddr_zq_cntl(ddr);
986 set_ddr_wrlvl_cntl(ddr);
987
988 set_ddr_pd_cntl(ddr);
989 set_ddr_sr_cntr(ddr);
990
991 set_ddr_sdram_rcw_1(ddr);
992 set_ddr_sdram_rcw_2(ddr);
993
994 return check_fsl_memctl_config_regs(ddr);
995}