blob: dd4fceaf88ff0160b12d67b4883ae7685b8e8933 [file] [log] [blame]
Kumar Gala124b0822008-08-26 15:01:29 -05001/*
York Sunba0c2eb2011-01-10 12:03:00 +00002 * Copyright 2008-2011 Freescale Semiconductor, Inc.
Kumar Gala124b0822008-08-26 15:01:29 -05003 *
Dave Liu3525e1a2010-03-05 12:22:00 +08004 * 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)
7 * any later version.
Kumar Gala124b0822008-08-26 15:01:29 -05008 */
9
10/*
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]
14 */
15
16#include <common.h>
17#include <asm/fsl_ddr_sdram.h>
18
19#include "ddr.h"
20
York Sunba0c2eb2011-01-10 12:03:00 +000021#ifdef CONFIG_MPC85xx
22 #define _DDR_ADDR CONFIG_SYS_MPC85xx_DDR_ADDR
23#elif defined(CONFIG_MPC86xx)
24 #define _DDR_ADDR CONFIG_SYS_MPC86xx_DDR_ADDR
25#else
26 #error "Undefined _DDR_ADDR"
27#endif
28
29u32 fsl_ddr_get_version(void)
30{
31 ccsr_ddr_t *ddr;
32 u32 ver_major_minor_errata;
33
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;
37
38 return ver_major_minor_errata;
39}
40
41unsigned int picos_to_mclk(unsigned int picos);
42
Kumar Gala124b0822008-08-26 15:01:29 -050043/*
44 * Determine Rtt value.
45 *
46 * This should likely be either board or controller specific.
47 *
Dave Liu4be87b22009-03-14 12:48:30 +080048 * Rtt(nominal) - DDR2:
Kumar Gala124b0822008-08-26 15:01:29 -050049 * 0 = Rtt disabled
50 * 1 = 75 ohm
51 * 2 = 150 ohm
52 * 3 = 50 ohm
Dave Liu4be87b22009-03-14 12:48:30 +080053 * Rtt(nominal) - DDR3:
54 * 0 = Rtt disabled
55 * 1 = 60 ohm
56 * 2 = 120 ohm
57 * 3 = 40 ohm
58 * 4 = 20 ohm
59 * 5 = 30 ohm
Kumar Gala124b0822008-08-26 15:01:29 -050060 *
61 * FIXME: Apparently 8641 needs a value of 2
62 * FIXME: Old code seys if 667 MHz or higher, use 3 on 8572
63 *
64 * FIXME: There was some effort down this line earlier:
65 *
66 * unsigned int i;
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)) {
71 * rtt = 2;
72 * break;
73 * }
74 * }
75 */
76static inline int fsl_ddr_get_rtt(void)
77{
78 int rtt;
79
80#if defined(CONFIG_FSL_DDR1)
81 rtt = 0;
82#elif defined(CONFIG_FSL_DDR2)
83 rtt = 3;
84#else
Dave Liu4be87b22009-03-14 12:48:30 +080085 rtt = 0;
Kumar Gala124b0822008-08-26 15:01:29 -050086#endif
87
88 return rtt;
89}
90
Dave Liu4be87b22009-03-14 12:48:30 +080091/*
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
York Sun7a16d642011-08-24 09:40:25 -070097 * 9 if 1.25ns > tCK >= 1.07ns
98 * 10 if 1.07ns > tCK >= 0.935ns
99 * 11 if 0.935ns > tCK >= 0.833ns
100 * 12 if 0.833ns > tCK >= 0.75ns
Dave Liu4be87b22009-03-14 12:48:30 +0800101 */
102static inline unsigned int compute_cas_write_latency(void)
103{
104 unsigned int cwl;
105 const unsigned int mclk_ps = get_memory_clk_period_ps();
106
107 if (mclk_ps >= 2500)
108 cwl = 5;
109 else if (mclk_ps >= 1875)
110 cwl = 6;
111 else if (mclk_ps >= 1500)
112 cwl = 7;
113 else if (mclk_ps >= 1250)
114 cwl = 8;
York Sun7a16d642011-08-24 09:40:25 -0700115 else if (mclk_ps >= 1070)
116 cwl = 9;
117 else if (mclk_ps >= 935)
118 cwl = 10;
119 else if (mclk_ps >= 833)
120 cwl = 11;
121 else if (mclk_ps >= 750)
122 cwl = 12;
123 else {
124 cwl = 12;
125 printf("Warning: CWL is out of range\n");
126 }
Dave Liu4be87b22009-03-14 12:48:30 +0800127 return cwl;
128}
129
Kumar Gala124b0822008-08-26 15:01:29 -0500130/* Chip Select Configuration (CSn_CONFIG) */
yorkf4f93c62010-07-02 22:25:53 +0000131static void set_csn_config(int dimm_number, int i, fsl_ddr_cfg_regs_t *ddr,
Kumar Gala124b0822008-08-26 15:01:29 -0500132 const memctl_options_t *popts,
133 const dimm_params_t *dimm_params)
134{
135 unsigned int cs_n_en = 0; /* Chip Select enable */
136 unsigned int intlv_en = 0; /* Memory controller interleave enable */
137 unsigned int intlv_ctl = 0; /* Interleaving control */
138 unsigned int ap_n_en = 0; /* Chip select n auto-precharge enable */
139 unsigned int odt_rd_cfg = 0; /* ODT for reads configuration */
140 unsigned int odt_wr_cfg = 0; /* ODT for writes configuration */
141 unsigned int ba_bits_cs_n = 0; /* Num of bank bits for SDRAM on CSn */
142 unsigned int row_bits_cs_n = 0; /* Num of row bits for SDRAM on CSn */
143 unsigned int col_bits_cs_n = 0; /* Num of ocl bits for SDRAM on CSn */
yorkf4f93c62010-07-02 22:25:53 +0000144 int go_config = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500145
146 /* Compute CS_CONFIG only for existing ranks of each DIMM. */
yorkf4f93c62010-07-02 22:25:53 +0000147 switch (i) {
148 case 0:
149 if (dimm_params[dimm_number].n_ranks > 0) {
150 go_config = 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500151 /* These fields only available in CS0_CONFIG */
152 intlv_en = popts->memctl_interleaving;
153 intlv_ctl = popts->memctl_interleaving_mode;
154 }
yorkf4f93c62010-07-02 22:25:53 +0000155 break;
156 case 1:
157 if ((dimm_number == 0 && dimm_params[0].n_ranks > 1) || \
158 (dimm_number == 1 && dimm_params[1].n_ranks > 0))
159 go_config = 1;
160 break;
161 case 2:
162 if ((dimm_number == 0 && dimm_params[0].n_ranks > 2) || \
York Sun15f874a2011-08-26 11:32:40 -0700163 (dimm_number >= 1 && dimm_params[dimm_number].n_ranks > 0))
yorkf4f93c62010-07-02 22:25:53 +0000164 go_config = 1;
165 break;
166 case 3:
167 if ((dimm_number == 0 && dimm_params[0].n_ranks > 3) || \
168 (dimm_number == 1 && dimm_params[1].n_ranks > 1) || \
169 (dimm_number == 3 && dimm_params[3].n_ranks > 0))
170 go_config = 1;
171 break;
172 default:
173 break;
174 }
175 if (go_config) {
176 unsigned int n_banks_per_sdram_device;
177 cs_n_en = 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500178 ap_n_en = popts->cs_local_opts[i].auto_precharge;
179 odt_rd_cfg = popts->cs_local_opts[i].odt_rd_cfg;
180 odt_wr_cfg = popts->cs_local_opts[i].odt_wr_cfg;
181 n_banks_per_sdram_device
yorkf4f93c62010-07-02 22:25:53 +0000182 = dimm_params[dimm_number].n_banks_per_sdram_device;
Kumar Gala124b0822008-08-26 15:01:29 -0500183 ba_bits_cs_n = __ilog2(n_banks_per_sdram_device) - 2;
yorkf4f93c62010-07-02 22:25:53 +0000184 row_bits_cs_n = dimm_params[dimm_number].n_row_addr - 12;
185 col_bits_cs_n = dimm_params[dimm_number].n_col_addr - 8;
Kumar Gala124b0822008-08-26 15:01:29 -0500186 }
Kumar Gala124b0822008-08-26 15:01:29 -0500187 ddr->cs[i].config = (0
188 | ((cs_n_en & 0x1) << 31)
189 | ((intlv_en & 0x3) << 29)
Haiying Wang272b5962008-10-03 12:36:39 -0400190 | ((intlv_ctl & 0xf) << 24)
Kumar Gala124b0822008-08-26 15:01:29 -0500191 | ((ap_n_en & 0x1) << 23)
192
193 /* XXX: some implementation only have 1 bit starting at left */
194 | ((odt_rd_cfg & 0x7) << 20)
195
196 /* XXX: Some implementation only have 1 bit starting at left */
197 | ((odt_wr_cfg & 0x7) << 16)
198
199 | ((ba_bits_cs_n & 0x3) << 14)
200 | ((row_bits_cs_n & 0x7) << 8)
201 | ((col_bits_cs_n & 0x7) << 0)
202 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400203 debug("FSLDDR: cs[%d]_config = 0x%08x\n", i,ddr->cs[i].config);
Kumar Gala124b0822008-08-26 15:01:29 -0500204}
205
206/* Chip Select Configuration 2 (CSn_CONFIG_2) */
207/* FIXME: 8572 */
208static void set_csn_config_2(int i, fsl_ddr_cfg_regs_t *ddr)
209{
210 unsigned int pasr_cfg = 0; /* Partial array self refresh config */
211
212 ddr->cs[i].config_2 = ((pasr_cfg & 7) << 24);
Haiying Wangd90e0402008-10-03 12:37:26 -0400213 debug("FSLDDR: cs[%d]_config_2 = 0x%08x\n", i, ddr->cs[i].config_2);
Kumar Gala124b0822008-08-26 15:01:29 -0500214}
215
216/* -3E = 667 CL5, -25 = CL6 800, -25E = CL5 800 */
217
Dave Liu4be87b22009-03-14 12:48:30 +0800218#if !defined(CONFIG_FSL_DDR1)
Kumar Gala124b0822008-08-26 15:01:29 -0500219/*
220 * DDR SDRAM Timing Configuration 0 (TIMING_CFG_0)
221 *
222 * Avoid writing for DDR I. The new PQ38 DDR controller
223 * dreams up non-zero default values to be backwards compatible.
224 */
York Sunba0c2eb2011-01-10 12:03:00 +0000225static void set_timing_cfg_0(fsl_ddr_cfg_regs_t *ddr,
226 const memctl_options_t *popts)
Kumar Gala124b0822008-08-26 15:01:29 -0500227{
228 unsigned char trwt_mclk = 0; /* Read-to-write turnaround */
229 unsigned char twrt_mclk = 0; /* Write-to-read turnaround */
230 /* 7.5 ns on -3E; 0 means WL - CL + BL/2 + 1 */
231 unsigned char trrt_mclk = 0; /* Read-to-read turnaround */
232 unsigned char twwt_mclk = 0; /* Write-to-write turnaround */
233
234 /* Active powerdown exit timing (tXARD and tXARDS). */
235 unsigned char act_pd_exit_mclk;
236 /* Precharge powerdown exit timing (tXP). */
237 unsigned char pre_pd_exit_mclk;
york1714e492010-07-02 22:25:56 +0000238 /* ODT powerdown exit timing (tAXPD). */
Kumar Gala124b0822008-08-26 15:01:29 -0500239 unsigned char taxpd_mclk;
240 /* Mode register set cycle time (tMRD). */
241 unsigned char tmrd_mclk;
242
York Sunba0c2eb2011-01-10 12:03:00 +0000243#ifdef CONFIG_FSL_DDR3
Dave Liu4be87b22009-03-14 12:48:30 +0800244 /*
245 * (tXARD and tXARDS). Empirical?
246 * The DDR3 spec has not tXARD,
247 * we use the tXP instead of it.
248 * tXP=max(3nCK, 7.5ns) for DDR3.
Dave Liu4be87b22009-03-14 12:48:30 +0800249 * spec has not the tAXPD, we use
york1714e492010-07-02 22:25:56 +0000250 * tAXPD=1, need design to confirm.
Dave Liu4be87b22009-03-14 12:48:30 +0800251 */
Dave Liuc7d983a2009-12-16 10:24:36 -0600252 int tXP = max((get_memory_clk_period_ps() * 3), 7500); /* unit=ps */
Kumar Galab78c7bf2011-01-31 20:36:02 -0600253 unsigned int data_rate = get_ddr_freq(0);
Dave Liu4be87b22009-03-14 12:48:30 +0800254 tmrd_mclk = 4;
Dave Liu81079262009-12-08 11:56:48 +0800255 /* set the turnaround time */
256 trwt_mclk = 1;
York Sun27f83be2011-02-10 10:13:10 -0800257 if ((data_rate/1000000 > 1150) || (popts->memctl_interleaving))
258 twrt_mclk = 1;
York Sunba0c2eb2011-01-10 12:03:00 +0000259
260 if (popts->dynamic_power == 0) { /* powerdown is not used */
261 act_pd_exit_mclk = 1;
262 pre_pd_exit_mclk = 1;
263 taxpd_mclk = 1;
264 } else {
265 /* act_pd_exit_mclk = tXARD, see above */
266 act_pd_exit_mclk = picos_to_mclk(tXP);
267 /* Mode register MR0[A12] is '1' - fast exit */
268 pre_pd_exit_mclk = act_pd_exit_mclk;
269 taxpd_mclk = 1;
270 }
Dave Liu4be87b22009-03-14 12:48:30 +0800271#else /* CONFIG_FSL_DDR2 */
272 /*
273 * (tXARD and tXARDS). Empirical?
274 * tXARD = 2 for DDR2
275 * tXP=2
276 * tAXPD=8
277 */
278 act_pd_exit_mclk = 2;
279 pre_pd_exit_mclk = 2;
280 taxpd_mclk = 8;
Kumar Gala124b0822008-08-26 15:01:29 -0500281 tmrd_mclk = 2;
Dave Liu4be87b22009-03-14 12:48:30 +0800282#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500283
York Sunf8691fc2011-05-27 13:44:28 +0800284 if (popts->trwt_override)
285 trwt_mclk = popts->trwt;
286
Kumar Gala124b0822008-08-26 15:01:29 -0500287 ddr->timing_cfg_0 = (0
288 | ((trwt_mclk & 0x3) << 30) /* RWT */
289 | ((twrt_mclk & 0x3) << 28) /* WRT */
290 | ((trrt_mclk & 0x3) << 26) /* RRT */
291 | ((twwt_mclk & 0x3) << 24) /* WWT */
292 | ((act_pd_exit_mclk & 0x7) << 20) /* ACT_PD_EXIT */
Dave Liu4758d532008-11-21 16:31:29 +0800293 | ((pre_pd_exit_mclk & 0xF) << 16) /* PRE_PD_EXIT */
Kumar Gala124b0822008-08-26 15:01:29 -0500294 | ((taxpd_mclk & 0xf) << 8) /* ODT_PD_EXIT */
295 | ((tmrd_mclk & 0xf) << 0) /* MRS_CYC */
296 );
297 debug("FSLDDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
298}
299#endif /* defined(CONFIG_FSL_DDR2) */
300
301/* DDR SDRAM Timing Configuration 3 (TIMING_CFG_3) */
302static void set_timing_cfg_3(fsl_ddr_cfg_regs_t *ddr,
Dave Liu4be87b22009-03-14 12:48:30 +0800303 const common_timing_params_t *common_dimm,
304 unsigned int cas_latency)
Kumar Gala124b0822008-08-26 15:01:29 -0500305{
306 /* Extended Activate to precharge interval (tRAS) */
307 unsigned int ext_acttopre = 0;
308 unsigned int ext_refrec; /* Extended refresh recovery time (tRFC) */
309 unsigned int ext_caslat = 0; /* Extended MCAS latency from READ cmd */
310 unsigned int cntl_adj = 0; /* Control Adjust */
311
Dave Liu5c1bb512008-11-21 16:31:22 +0800312 /* If the tRAS > 19 MCLK, we use the ext mode */
313 if (picos_to_mclk(common_dimm->tRAS_ps) > 0x13)
314 ext_acttopre = 1;
315
Kumar Gala124b0822008-08-26 15:01:29 -0500316 ext_refrec = (picos_to_mclk(common_dimm->tRFC_ps) - 8) >> 4;
Dave Liu4be87b22009-03-14 12:48:30 +0800317
318 /* If the CAS latency more than 8, use the ext mode */
319 if (cas_latency > 8)
320 ext_caslat = 1;
321
Kumar Gala124b0822008-08-26 15:01:29 -0500322 ddr->timing_cfg_3 = (0
323 | ((ext_acttopre & 0x1) << 24)
Dave Liu5c1bb512008-11-21 16:31:22 +0800324 | ((ext_refrec & 0xF) << 16)
Kumar Gala124b0822008-08-26 15:01:29 -0500325 | ((ext_caslat & 0x1) << 12)
326 | ((cntl_adj & 0x7) << 0)
327 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400328 debug("FSLDDR: timing_cfg_3 = 0x%08x\n", ddr->timing_cfg_3);
Kumar Gala124b0822008-08-26 15:01:29 -0500329}
330
331/* DDR SDRAM Timing Configuration 1 (TIMING_CFG_1) */
332static void set_timing_cfg_1(fsl_ddr_cfg_regs_t *ddr,
Dave Liu4be87b22009-03-14 12:48:30 +0800333 const memctl_options_t *popts,
Kumar Gala124b0822008-08-26 15:01:29 -0500334 const common_timing_params_t *common_dimm,
335 unsigned int cas_latency)
336{
337 /* Precharge-to-activate interval (tRP) */
338 unsigned char pretoact_mclk;
339 /* Activate to precharge interval (tRAS) */
340 unsigned char acttopre_mclk;
341 /* Activate to read/write interval (tRCD) */
342 unsigned char acttorw_mclk;
343 /* CASLAT */
344 unsigned char caslat_ctrl;
345 /* Refresh recovery time (tRFC) ; trfc_low */
346 unsigned char refrec_ctrl;
347 /* Last data to precharge minimum interval (tWR) */
348 unsigned char wrrec_mclk;
349 /* Activate-to-activate interval (tRRD) */
350 unsigned char acttoact_mclk;
351 /* Last write data pair to read command issue interval (tWTR) */
352 unsigned char wrtord_mclk;
York Sun3673f2c2011-03-02 14:24:11 -0800353 /* DDR_SDRAM_MODE doesn't support 9,11,13,15 */
354 static const u8 wrrec_table[] = {
355 1, 2, 3, 4, 5, 6, 7, 8, 10, 10, 12, 12, 14, 14, 0, 0};
Kumar Gala124b0822008-08-26 15:01:29 -0500356
357 pretoact_mclk = picos_to_mclk(common_dimm->tRP_ps);
358 acttopre_mclk = picos_to_mclk(common_dimm->tRAS_ps);
359 acttorw_mclk = picos_to_mclk(common_dimm->tRCD_ps);
360
361 /*
362 * Translate CAS Latency to a DDR controller field value:
363 *
364 * CAS Lat DDR I DDR II Ctrl
365 * Clocks SPD Bit SPD Bit Value
366 * ------- ------- ------- -----
367 * 1.0 0 0001
368 * 1.5 1 0010
369 * 2.0 2 2 0011
370 * 2.5 3 0100
371 * 3.0 4 3 0101
372 * 3.5 5 0110
373 * 4.0 4 0111
374 * 4.5 1000
375 * 5.0 5 1001
376 */
377#if defined(CONFIG_FSL_DDR1)
378 caslat_ctrl = (cas_latency + 1) & 0x07;
379#elif defined(CONFIG_FSL_DDR2)
380 caslat_ctrl = 2 * cas_latency - 1;
381#else
Dave Liu4be87b22009-03-14 12:48:30 +0800382 /*
383 * if the CAS latency more than 8 cycle,
384 * we need set extend bit for it at
385 * TIMING_CFG_3[EXT_CASLAT]
386 */
387 if (cas_latency > 8)
388 cas_latency -= 8;
389 caslat_ctrl = 2 * cas_latency - 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500390#endif
391
392 refrec_ctrl = picos_to_mclk(common_dimm->tRFC_ps) - 8;
393 wrrec_mclk = picos_to_mclk(common_dimm->tWR_ps);
York Sun3673f2c2011-03-02 14:24:11 -0800394
395 wrrec_mclk = wrrec_table[wrrec_mclk - 1];
Dave Liu4be87b22009-03-14 12:48:30 +0800396 if (popts->OTF_burst_chop_en)
397 wrrec_mclk += 2;
398
Kumar Gala124b0822008-08-26 15:01:29 -0500399 acttoact_mclk = picos_to_mclk(common_dimm->tRRD_ps);
Dave Liu4be87b22009-03-14 12:48:30 +0800400 /*
401 * JEDEC has min requirement for tRRD
402 */
403#if defined(CONFIG_FSL_DDR3)
404 if (acttoact_mclk < 4)
405 acttoact_mclk = 4;
406#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500407 wrtord_mclk = picos_to_mclk(common_dimm->tWTR_ps);
Dave Liu4be87b22009-03-14 12:48:30 +0800408 /*
409 * JEDEC has some min requirements for tWTR
410 */
411#if defined(CONFIG_FSL_DDR2)
412 if (wrtord_mclk < 2)
413 wrtord_mclk = 2;
414#elif defined(CONFIG_FSL_DDR3)
415 if (wrtord_mclk < 4)
416 wrtord_mclk = 4;
417#endif
418 if (popts->OTF_burst_chop_en)
419 wrtord_mclk += 2;
Kumar Gala124b0822008-08-26 15:01:29 -0500420
421 ddr->timing_cfg_1 = (0
Dave Liu5c1bb512008-11-21 16:31:22 +0800422 | ((pretoact_mclk & 0x0F) << 28)
Kumar Gala124b0822008-08-26 15:01:29 -0500423 | ((acttopre_mclk & 0x0F) << 24)
Dave Liu5c1bb512008-11-21 16:31:22 +0800424 | ((acttorw_mclk & 0xF) << 20)
Kumar Gala124b0822008-08-26 15:01:29 -0500425 | ((caslat_ctrl & 0xF) << 16)
426 | ((refrec_ctrl & 0xF) << 12)
Dave Liu5c1bb512008-11-21 16:31:22 +0800427 | ((wrrec_mclk & 0x0F) << 8)
Kumar Gala124b0822008-08-26 15:01:29 -0500428 | ((acttoact_mclk & 0x07) << 4)
429 | ((wrtord_mclk & 0x07) << 0)
430 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400431 debug("FSLDDR: timing_cfg_1 = 0x%08x\n", ddr->timing_cfg_1);
Kumar Gala124b0822008-08-26 15:01:29 -0500432}
433
434/* DDR SDRAM Timing Configuration 2 (TIMING_CFG_2) */
435static void set_timing_cfg_2(fsl_ddr_cfg_regs_t *ddr,
436 const memctl_options_t *popts,
437 const common_timing_params_t *common_dimm,
438 unsigned int cas_latency,
439 unsigned int additive_latency)
440{
441 /* Additive latency */
442 unsigned char add_lat_mclk;
443 /* CAS-to-preamble override */
444 unsigned short cpo;
445 /* Write latency */
446 unsigned char wr_lat;
447 /* Read to precharge (tRTP) */
448 unsigned char rd_to_pre;
449 /* Write command to write data strobe timing adjustment */
450 unsigned char wr_data_delay;
451 /* Minimum CKE pulse width (tCKE) */
452 unsigned char cke_pls;
453 /* Window for four activates (tFAW) */
454 unsigned short four_act;
455
456 /* FIXME add check that this must be less than acttorw_mclk */
457 add_lat_mclk = additive_latency;
458 cpo = popts->cpo_override;
459
460#if defined(CONFIG_FSL_DDR1)
461 /*
462 * This is a lie. It should really be 1, but if it is
463 * set to 1, bits overlap into the old controller's
464 * otherwise unused ACSM field. If we leave it 0, then
465 * the HW will magically treat it as 1 for DDR 1. Oh Yea.
466 */
467 wr_lat = 0;
468#elif defined(CONFIG_FSL_DDR2)
Dave Liu82aa9532009-03-14 12:48:19 +0800469 wr_lat = cas_latency - 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500470#else
Dave Liu4be87b22009-03-14 12:48:30 +0800471 wr_lat = compute_cas_write_latency();
Kumar Gala124b0822008-08-26 15:01:29 -0500472#endif
473
474 rd_to_pre = picos_to_mclk(common_dimm->tRTP_ps);
Dave Liu4be87b22009-03-14 12:48:30 +0800475 /*
476 * JEDEC has some min requirements for tRTP
477 */
Dave Liu82aa9532009-03-14 12:48:19 +0800478#if defined(CONFIG_FSL_DDR2)
Dave Liu4be87b22009-03-14 12:48:30 +0800479 if (rd_to_pre < 2)
480 rd_to_pre = 2;
481#elif defined(CONFIG_FSL_DDR3)
482 if (rd_to_pre < 4)
483 rd_to_pre = 4;
Dave Liu82aa9532009-03-14 12:48:19 +0800484#endif
Dave Liu4be87b22009-03-14 12:48:30 +0800485 if (additive_latency)
486 rd_to_pre += additive_latency;
487 if (popts->OTF_burst_chop_en)
488 rd_to_pre += 2; /* according to UM */
489
Kumar Gala124b0822008-08-26 15:01:29 -0500490 wr_data_delay = popts->write_data_delay;
491 cke_pls = picos_to_mclk(popts->tCKE_clock_pulse_width_ps);
492 four_act = picos_to_mclk(popts->tFAW_window_four_activates_ps);
493
494 ddr->timing_cfg_2 = (0
Dave Liu4758d532008-11-21 16:31:29 +0800495 | ((add_lat_mclk & 0xf) << 28)
Kumar Gala124b0822008-08-26 15:01:29 -0500496 | ((cpo & 0x1f) << 23)
Dave Liu4758d532008-11-21 16:31:29 +0800497 | ((wr_lat & 0xf) << 19)
Dave Liu4be87b22009-03-14 12:48:30 +0800498 | ((rd_to_pre & RD_TO_PRE_MASK) << RD_TO_PRE_SHIFT)
499 | ((wr_data_delay & WR_DATA_DELAY_MASK) << WR_DATA_DELAY_SHIFT)
Kumar Gala124b0822008-08-26 15:01:29 -0500500 | ((cke_pls & 0x7) << 6)
Dave Liu4758d532008-11-21 16:31:29 +0800501 | ((four_act & 0x3f) << 0)
Kumar Gala124b0822008-08-26 15:01:29 -0500502 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400503 debug("FSLDDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2);
Kumar Gala124b0822008-08-26 15:01:29 -0500504}
505
yorkde879322010-07-02 22:25:55 +0000506/* DDR SDRAM Register Control Word */
507static void set_ddr_sdram_rcw(fsl_ddr_cfg_regs_t *ddr,
York Sunba0c2eb2011-01-10 12:03:00 +0000508 const memctl_options_t *popts,
yorkde879322010-07-02 22:25:55 +0000509 const common_timing_params_t *common_dimm)
510{
511 if (common_dimm->all_DIMMs_registered
512 && !common_dimm->all_DIMMs_unbuffered) {
York Sunba0c2eb2011-01-10 12:03:00 +0000513 if (popts->rcw_override) {
514 ddr->ddr_sdram_rcw_1 = popts->rcw_1;
515 ddr->ddr_sdram_rcw_2 = popts->rcw_2;
516 } else {
517 ddr->ddr_sdram_rcw_1 =
518 common_dimm->rcw[0] << 28 | \
519 common_dimm->rcw[1] << 24 | \
520 common_dimm->rcw[2] << 20 | \
521 common_dimm->rcw[3] << 16 | \
522 common_dimm->rcw[4] << 12 | \
523 common_dimm->rcw[5] << 8 | \
524 common_dimm->rcw[6] << 4 | \
525 common_dimm->rcw[7];
526 ddr->ddr_sdram_rcw_2 =
527 common_dimm->rcw[8] << 28 | \
528 common_dimm->rcw[9] << 24 | \
529 common_dimm->rcw[10] << 20 | \
530 common_dimm->rcw[11] << 16 | \
531 common_dimm->rcw[12] << 12 | \
532 common_dimm->rcw[13] << 8 | \
533 common_dimm->rcw[14] << 4 | \
534 common_dimm->rcw[15];
535 }
yorkde879322010-07-02 22:25:55 +0000536 debug("FSLDDR: ddr_sdram_rcw_1 = 0x%08x\n", ddr->ddr_sdram_rcw_1);
537 debug("FSLDDR: ddr_sdram_rcw_2 = 0x%08x\n", ddr->ddr_sdram_rcw_2);
538 }
539}
540
Kumar Gala124b0822008-08-26 15:01:29 -0500541/* DDR SDRAM control configuration (DDR_SDRAM_CFG) */
542static void set_ddr_sdram_cfg(fsl_ddr_cfg_regs_t *ddr,
543 const memctl_options_t *popts,
544 const common_timing_params_t *common_dimm)
545{
546 unsigned int mem_en; /* DDR SDRAM interface logic enable */
547 unsigned int sren; /* Self refresh enable (during sleep) */
548 unsigned int ecc_en; /* ECC enable. */
549 unsigned int rd_en; /* Registered DIMM enable */
550 unsigned int sdram_type; /* Type of SDRAM */
551 unsigned int dyn_pwr; /* Dynamic power management mode */
552 unsigned int dbw; /* DRAM dta bus width */
Dave Liu4758d532008-11-21 16:31:29 +0800553 unsigned int eight_be = 0; /* 8-beat burst enable, DDR2 is zero */
Kumar Gala124b0822008-08-26 15:01:29 -0500554 unsigned int ncap = 0; /* Non-concurrent auto-precharge */
555 unsigned int threeT_en; /* Enable 3T timing */
556 unsigned int twoT_en; /* Enable 2T timing */
557 unsigned int ba_intlv_ctl; /* Bank (CS) interleaving control */
558 unsigned int x32_en = 0; /* x32 enable */
559 unsigned int pchb8 = 0; /* precharge bit 8 enable */
560 unsigned int hse; /* Global half strength override */
561 unsigned int mem_halt = 0; /* memory controller halt */
562 unsigned int bi = 0; /* Bypass initialization */
563
564 mem_en = 1;
565 sren = popts->self_refresh_in_sleep;
566 if (common_dimm->all_DIMMs_ECC_capable) {
567 /* Allow setting of ECC only if all DIMMs are ECC. */
568 ecc_en = popts->ECC_mode;
569 } else {
570 ecc_en = 0;
571 }
572
York Sunba0c2eb2011-01-10 12:03:00 +0000573 if (common_dimm->all_DIMMs_registered
574 && !common_dimm->all_DIMMs_unbuffered) {
575 rd_en = 1;
576 twoT_en = 0;
577 } else {
578 rd_en = 0;
579 twoT_en = popts->twoT_en;
580 }
Kumar Gala124b0822008-08-26 15:01:29 -0500581
582 sdram_type = CONFIG_FSL_SDRAM_TYPE;
583
584 dyn_pwr = popts->dynamic_power;
585 dbw = popts->data_bus_width;
Dave Liu4be87b22009-03-14 12:48:30 +0800586 /* 8-beat burst enable DDR-III case
587 * we must clear it when use the on-the-fly mode,
588 * must set it when use the 32-bits bus mode.
589 */
590 if (sdram_type == SDRAM_TYPE_DDR3) {
591 if (popts->burst_length == DDR_BL8)
592 eight_be = 1;
593 if (popts->burst_length == DDR_OTF)
594 eight_be = 0;
595 if (dbw == 0x1)
596 eight_be = 1;
597 }
598
Kumar Gala124b0822008-08-26 15:01:29 -0500599 threeT_en = popts->threeT_en;
Kumar Gala124b0822008-08-26 15:01:29 -0500600 ba_intlv_ctl = popts->ba_intlv_ctl;
601 hse = popts->half_strength_driver_enable;
602
603 ddr->ddr_sdram_cfg = (0
604 | ((mem_en & 0x1) << 31)
605 | ((sren & 0x1) << 30)
606 | ((ecc_en & 0x1) << 29)
607 | ((rd_en & 0x1) << 28)
608 | ((sdram_type & 0x7) << 24)
609 | ((dyn_pwr & 0x1) << 21)
610 | ((dbw & 0x3) << 19)
611 | ((eight_be & 0x1) << 18)
612 | ((ncap & 0x1) << 17)
613 | ((threeT_en & 0x1) << 16)
614 | ((twoT_en & 0x1) << 15)
615 | ((ba_intlv_ctl & 0x7F) << 8)
616 | ((x32_en & 0x1) << 5)
617 | ((pchb8 & 0x1) << 4)
618 | ((hse & 0x1) << 3)
619 | ((mem_halt & 0x1) << 1)
620 | ((bi & 0x1) << 0)
621 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400622 debug("FSLDDR: ddr_sdram_cfg = 0x%08x\n", ddr->ddr_sdram_cfg);
Kumar Gala124b0822008-08-26 15:01:29 -0500623}
624
625/* DDR SDRAM control configuration 2 (DDR_SDRAM_CFG_2) */
626static void set_ddr_sdram_cfg_2(fsl_ddr_cfg_regs_t *ddr,
York Sunba0c2eb2011-01-10 12:03:00 +0000627 const memctl_options_t *popts,
628 const unsigned int unq_mrs_en)
Kumar Gala124b0822008-08-26 15:01:29 -0500629{
630 unsigned int frc_sr = 0; /* Force self refresh */
631 unsigned int sr_ie = 0; /* Self-refresh interrupt enable */
632 unsigned int dll_rst_dis; /* DLL reset disable */
633 unsigned int dqs_cfg; /* DQS configuration */
York Sun15f874a2011-08-26 11:32:40 -0700634 unsigned int odt_cfg = 0; /* ODT configuration */
Kumar Gala124b0822008-08-26 15:01:29 -0500635 unsigned int num_pr; /* Number of posted refreshes */
636 unsigned int obc_cfg; /* On-The-Fly Burst Chop Cfg */
637 unsigned int ap_en; /* Address Parity Enable */
638 unsigned int d_init; /* DRAM data initialization */
639 unsigned int rcw_en = 0; /* Register Control Word Enable */
640 unsigned int md_en = 0; /* Mirrored DIMM Enable */
yorkf4f93c62010-07-02 22:25:53 +0000641 unsigned int qd_en = 0; /* quad-rank DIMM Enable */
York Sun15f874a2011-08-26 11:32:40 -0700642 int i;
Kumar Gala124b0822008-08-26 15:01:29 -0500643
644 dll_rst_dis = 1; /* Make this configurable */
645 dqs_cfg = popts->DQS_config;
York Sun15f874a2011-08-26 11:32:40 -0700646 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
647 if (popts->cs_local_opts[i].odt_rd_cfg
648 || popts->cs_local_opts[i].odt_wr_cfg) {
649 odt_cfg = SDRAM_CFG2_ODT_ONLY_READ;
650 break;
651 }
Kumar Gala124b0822008-08-26 15:01:29 -0500652 }
653
654 num_pr = 1; /* Make this configurable */
655
656 /*
657 * 8572 manual says
658 * {TIMING_CFG_1[PRETOACT]
659 * + [DDR_SDRAM_CFG_2[NUM_PR]
660 * * ({EXT_REFREC || REFREC} + 8 + 2)]}
661 * << DDR_SDRAM_INTERVAL[REFINT]
662 */
Dave Liu4be87b22009-03-14 12:48:30 +0800663#if defined(CONFIG_FSL_DDR3)
664 obc_cfg = popts->OTF_burst_chop_en;
665#else
666 obc_cfg = 0;
667#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500668
York Sunba0c2eb2011-01-10 12:03:00 +0000669 if (popts->registered_dimm_en) {
670 rcw_en = 1;
671 ap_en = popts->ap_en;
672 } else {
673 rcw_en = 0;
674 ap_en = 0;
675 }
Kumar Gala124b0822008-08-26 15:01:29 -0500676
677#if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
678 /* Use the DDR controller to auto initialize memory. */
York Sunba0c2eb2011-01-10 12:03:00 +0000679 d_init = popts->ECC_init_using_memctl;
Kumar Gala124b0822008-08-26 15:01:29 -0500680 ddr->ddr_data_init = CONFIG_MEM_INIT_VALUE;
681 debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init);
682#else
683 /* Memory will be initialized via DMA, or not at all. */
684 d_init = 0;
685#endif
686
Dave Liu4be87b22009-03-14 12:48:30 +0800687#if defined(CONFIG_FSL_DDR3)
688 md_en = popts->mirrored_dimm;
689#endif
yorkf4f93c62010-07-02 22:25:53 +0000690 qd_en = popts->quad_rank_present ? 1 : 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500691 ddr->ddr_sdram_cfg_2 = (0
692 | ((frc_sr & 0x1) << 31)
693 | ((sr_ie & 0x1) << 30)
694 | ((dll_rst_dis & 0x1) << 29)
695 | ((dqs_cfg & 0x3) << 26)
696 | ((odt_cfg & 0x3) << 21)
697 | ((num_pr & 0xf) << 12)
yorkf4f93c62010-07-02 22:25:53 +0000698 | (qd_en << 9)
York Sunba0c2eb2011-01-10 12:03:00 +0000699 | (unq_mrs_en << 8)
Kumar Gala124b0822008-08-26 15:01:29 -0500700 | ((obc_cfg & 0x1) << 6)
701 | ((ap_en & 0x1) << 5)
702 | ((d_init & 0x1) << 4)
York Sun501b70d2011-03-17 11:18:12 -0700703#ifdef CONFIG_FSL_DDR3
Kumar Gala124b0822008-08-26 15:01:29 -0500704 | ((rcw_en & 0x1) << 2)
York Sun501b70d2011-03-17 11:18:12 -0700705#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500706 | ((md_en & 0x1) << 0)
707 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400708 debug("FSLDDR: ddr_sdram_cfg_2 = 0x%08x\n", ddr->ddr_sdram_cfg_2);
Kumar Gala124b0822008-08-26 15:01:29 -0500709}
710
711/* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
Dave Liu2d0f1252009-12-16 10:24:38 -0600712static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr,
York Sunba0c2eb2011-01-10 12:03:00 +0000713 const memctl_options_t *popts,
714 const unsigned int unq_mrs_en)
Kumar Gala124b0822008-08-26 15:01:29 -0500715{
716 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
717 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
718
Dave Liu4be87b22009-03-14 12:48:30 +0800719#if defined(CONFIG_FSL_DDR3)
Kumar Gala65b5be22011-01-20 01:53:15 -0600720 int i;
Dave Liu2d0f1252009-12-16 10:24:38 -0600721 unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */
Dave Liu4be87b22009-03-14 12:48:30 +0800722 unsigned int srt = 0; /* self-refresh temerature, normal range */
723 unsigned int asr = 0; /* auto self-refresh disable */
724 unsigned int cwl = compute_cas_write_latency() - 5;
725 unsigned int pasr = 0; /* partial array self refresh disable */
726
Dave Liu2d0f1252009-12-16 10:24:38 -0600727 if (popts->rtt_override)
728 rtt_wr = popts->rtt_wr_override_value;
York Sunba0c2eb2011-01-10 12:03:00 +0000729 else
730 rtt_wr = popts->cs_local_opts[0].odt_rtt_wr;
Dave Liu4be87b22009-03-14 12:48:30 +0800731 esdmode2 = (0
732 | ((rtt_wr & 0x3) << 9)
733 | ((srt & 0x1) << 7)
734 | ((asr & 0x1) << 6)
735 | ((cwl & 0x7) << 3)
736 | ((pasr & 0x7) << 0));
737#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500738 ddr->ddr_sdram_mode_2 = (0
739 | ((esdmode2 & 0xFFFF) << 16)
740 | ((esdmode3 & 0xFFFF) << 0)
741 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400742 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
York Sunba0c2eb2011-01-10 12:03:00 +0000743
744#ifdef CONFIG_FSL_DDR3
745 if (unq_mrs_en) { /* unique mode registers are supported */
746 for (i = 1; i < 4; i++) {
747 if (popts->rtt_override)
748 rtt_wr = popts->rtt_wr_override_value;
749 else
750 rtt_wr = popts->cs_local_opts[i].odt_rtt_wr;
751
752 esdmode2 &= 0xF9FF; /* clear bit 10, 9 */
753 esdmode2 |= (rtt_wr & 0x3) << 9;
754 switch (i) {
755 case 1:
756 ddr->ddr_sdram_mode_4 = (0
757 | ((esdmode2 & 0xFFFF) << 16)
758 | ((esdmode3 & 0xFFFF) << 0)
759 );
760 break;
761 case 2:
762 ddr->ddr_sdram_mode_6 = (0
763 | ((esdmode2 & 0xFFFF) << 16)
764 | ((esdmode3 & 0xFFFF) << 0)
765 );
766 break;
767 case 3:
768 ddr->ddr_sdram_mode_8 = (0
769 | ((esdmode2 & 0xFFFF) << 16)
770 | ((esdmode3 & 0xFFFF) << 0)
771 );
772 break;
773 }
774 }
775 debug("FSLDDR: ddr_sdram_mode_4 = 0x%08x\n",
776 ddr->ddr_sdram_mode_4);
777 debug("FSLDDR: ddr_sdram_mode_6 = 0x%08x\n",
778 ddr->ddr_sdram_mode_6);
779 debug("FSLDDR: ddr_sdram_mode_8 = 0x%08x\n",
780 ddr->ddr_sdram_mode_8);
781 }
782#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500783}
784
785/* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */
786static void set_ddr_sdram_interval(fsl_ddr_cfg_regs_t *ddr,
787 const memctl_options_t *popts,
788 const common_timing_params_t *common_dimm)
789{
790 unsigned int refint; /* Refresh interval */
791 unsigned int bstopre; /* Precharge interval */
792
793 refint = picos_to_mclk(common_dimm->refresh_rate_ps);
794
795 bstopre = popts->bstopre;
796
797 /* refint field used 0x3FFF in earlier controllers */
798 ddr->ddr_sdram_interval = (0
799 | ((refint & 0xFFFF) << 16)
800 | ((bstopre & 0x3FFF) << 0)
801 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400802 debug("FSLDDR: ddr_sdram_interval = 0x%08x\n", ddr->ddr_sdram_interval);
Kumar Gala124b0822008-08-26 15:01:29 -0500803}
804
Dave Liu4be87b22009-03-14 12:48:30 +0800805#if defined(CONFIG_FSL_DDR3)
Kumar Gala124b0822008-08-26 15:01:29 -0500806/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
807static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
808 const memctl_options_t *popts,
809 const common_timing_params_t *common_dimm,
810 unsigned int cas_latency,
York Sunba0c2eb2011-01-10 12:03:00 +0000811 unsigned int additive_latency,
812 const unsigned int unq_mrs_en)
Kumar Gala124b0822008-08-26 15:01:29 -0500813{
814 unsigned short esdmode; /* Extended SDRAM mode */
815 unsigned short sdmode; /* SDRAM mode */
816
Dave Liu4be87b22009-03-14 12:48:30 +0800817 /* Mode Register - MR1 */
818 unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */
819 unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */
820 unsigned int rtt;
821 unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */
822 unsigned int al = 0; /* Posted CAS# additive latency (AL) */
York Sunba0c2eb2011-01-10 12:03:00 +0000823 unsigned int dic = 0; /* Output driver impedance, 40ohm */
Dave Liu4be87b22009-03-14 12:48:30 +0800824 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
825 1=Disable (Test/Debug) */
826
827 /* Mode Register - MR0 */
828 unsigned int dll_on; /* DLL control for precharge PD, 0=off, 1=on */
829 unsigned int wr; /* Write Recovery */
830 unsigned int dll_rst; /* DLL Reset */
831 unsigned int mode; /* Normal=0 or Test=1 */
832 unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
833 /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
834 unsigned int bt;
835 unsigned int bl; /* BL: Burst Length */
836
837 unsigned int wr_mclk;
York Sun3673f2c2011-03-02 14:24:11 -0800838 /*
839 * DDR_SDRAM_MODE doesn't support 9,11,13,15
840 * Please refer JEDEC Standard No. 79-3E for Mode Register MR0
841 * for this table
842 */
843 static const u8 wr_table[] = {1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 0, 0};
Dave Liu4be87b22009-03-14 12:48:30 +0800844
845 const unsigned int mclk_ps = get_memory_clk_period_ps();
York Sunba0c2eb2011-01-10 12:03:00 +0000846 int i;
Dave Liu4be87b22009-03-14 12:48:30 +0800847
Dave Liu4be87b22009-03-14 12:48:30 +0800848 if (popts->rtt_override)
849 rtt = popts->rtt_override_value;
York Sunba0c2eb2011-01-10 12:03:00 +0000850 else
851 rtt = popts->cs_local_opts[0].odt_rtt_norm;
Dave Liu4be87b22009-03-14 12:48:30 +0800852
853 if (additive_latency == (cas_latency - 1))
854 al = 1;
855 if (additive_latency == (cas_latency - 2))
856 al = 2;
857
York Sunba0c2eb2011-01-10 12:03:00 +0000858 if (popts->quad_rank_present)
859 dic = 1; /* output driver impedance 240/7 ohm */
860
Dave Liu4be87b22009-03-14 12:48:30 +0800861 /*
862 * The esdmode value will also be used for writing
863 * MR1 during write leveling for DDR3, although the
864 * bits specifically related to the write leveling
865 * scheme will be handled automatically by the DDR
866 * controller. so we set the wrlvl_en = 0 here.
867 */
868 esdmode = (0
869 | ((qoff & 0x1) << 12)
870 | ((tdqs_en & 0x1) << 11)
Kumar Gala14f2eb12009-09-10 14:54:55 -0500871 | ((rtt & 0x4) << 7) /* rtt field is split */
Dave Liu4be87b22009-03-14 12:48:30 +0800872 | ((wrlvl_en & 0x1) << 7)
Kumar Gala14f2eb12009-09-10 14:54:55 -0500873 | ((rtt & 0x2) << 5) /* rtt field is split */
874 | ((dic & 0x2) << 4) /* DIC field is split */
Dave Liu4be87b22009-03-14 12:48:30 +0800875 | ((al & 0x3) << 3)
Kumar Gala14f2eb12009-09-10 14:54:55 -0500876 | ((rtt & 0x1) << 2) /* rtt field is split */
Dave Liu4be87b22009-03-14 12:48:30 +0800877 | ((dic & 0x1) << 1) /* DIC field is split */
878 | ((dll_en & 0x1) << 0)
879 );
880
881 /*
882 * DLL control for precharge PD
883 * 0=slow exit DLL off (tXPDLL)
884 * 1=fast exit DLL on (tXP)
885 */
886 dll_on = 1;
York Sun3673f2c2011-03-02 14:24:11 -0800887
Dave Liu4be87b22009-03-14 12:48:30 +0800888 wr_mclk = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps;
York Sun3673f2c2011-03-02 14:24:11 -0800889 wr = wr_table[wr_mclk - 5];
890
Dave Liu4be87b22009-03-14 12:48:30 +0800891 dll_rst = 0; /* dll no reset */
892 mode = 0; /* normal mode */
893
894 /* look up table to get the cas latency bits */
895 if (cas_latency >= 5 && cas_latency <= 11) {
896 unsigned char cas_latency_table[7] = {
897 0x2, /* 5 clocks */
898 0x4, /* 6 clocks */
899 0x6, /* 7 clocks */
900 0x8, /* 8 clocks */
901 0xa, /* 9 clocks */
902 0xc, /* 10 clocks */
903 0xe /* 11 clocks */
904 };
905 caslat = cas_latency_table[cas_latency - 5];
906 }
907 bt = 0; /* Nibble sequential */
908
909 switch (popts->burst_length) {
910 case DDR_BL8:
911 bl = 0;
912 break;
913 case DDR_OTF:
914 bl = 1;
915 break;
916 case DDR_BC4:
917 bl = 2;
918 break;
919 default:
920 printf("Error: invalid burst length of %u specified. "
921 " Defaulting to on-the-fly BC4 or BL8 beats.\n",
922 popts->burst_length);
923 bl = 1;
924 break;
925 }
926
927 sdmode = (0
928 | ((dll_on & 0x1) << 12)
929 | ((wr & 0x7) << 9)
930 | ((dll_rst & 0x1) << 8)
931 | ((mode & 0x1) << 7)
932 | (((caslat >> 1) & 0x7) << 4)
933 | ((bt & 0x1) << 3)
934 | ((bl & 0x3) << 0)
935 );
936
937 ddr->ddr_sdram_mode = (0
938 | ((esdmode & 0xFFFF) << 16)
939 | ((sdmode & 0xFFFF) << 0)
940 );
941
942 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
York Sunba0c2eb2011-01-10 12:03:00 +0000943
944 if (unq_mrs_en) { /* unique mode registers are supported */
945 for (i = 1; i < 4; i++) {
946 if (popts->rtt_override)
947 rtt = popts->rtt_override_value;
948 else
949 rtt = popts->cs_local_opts[i].odt_rtt_norm;
950
951 esdmode &= 0xFDBB; /* clear bit 9,6,2 */
952 esdmode |= (0
953 | ((rtt & 0x4) << 7) /* rtt field is split */
954 | ((rtt & 0x2) << 5) /* rtt field is split */
955 | ((rtt & 0x1) << 2) /* rtt field is split */
956 );
957 switch (i) {
958 case 1:
959 ddr->ddr_sdram_mode_3 = (0
960 | ((esdmode & 0xFFFF) << 16)
961 | ((sdmode & 0xFFFF) << 0)
962 );
963 break;
964 case 2:
965 ddr->ddr_sdram_mode_5 = (0
966 | ((esdmode & 0xFFFF) << 16)
967 | ((sdmode & 0xFFFF) << 0)
968 );
969 break;
970 case 3:
971 ddr->ddr_sdram_mode_7 = (0
972 | ((esdmode & 0xFFFF) << 16)
973 | ((sdmode & 0xFFFF) << 0)
974 );
975 break;
976 }
977 }
978 debug("FSLDDR: ddr_sdram_mode_3 = 0x%08x\n",
979 ddr->ddr_sdram_mode_3);
980 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
981 ddr->ddr_sdram_mode_5);
982 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
983 ddr->ddr_sdram_mode_5);
984 }
Dave Liu4be87b22009-03-14 12:48:30 +0800985}
986
987#else /* !CONFIG_FSL_DDR3 */
988
989/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
990static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
991 const memctl_options_t *popts,
992 const common_timing_params_t *common_dimm,
993 unsigned int cas_latency,
York Sunba0c2eb2011-01-10 12:03:00 +0000994 unsigned int additive_latency,
995 const unsigned int unq_mrs_en)
Dave Liu4be87b22009-03-14 12:48:30 +0800996{
997 unsigned short esdmode; /* Extended SDRAM mode */
998 unsigned short sdmode; /* SDRAM mode */
999
Kumar Gala124b0822008-08-26 15:01:29 -05001000 /*
1001 * FIXME: This ought to be pre-calculated in a
1002 * technology-specific routine,
1003 * e.g. compute_DDR2_mode_register(), and then the
1004 * sdmode and esdmode passed in as part of common_dimm.
1005 */
1006
1007 /* Extended Mode Register */
1008 unsigned int mrs = 0; /* Mode Register Set */
1009 unsigned int outputs = 0; /* 0=Enabled, 1=Disabled */
1010 unsigned int rdqs_en = 0; /* RDQS Enable: 0=no, 1=yes */
1011 unsigned int dqs_en = 0; /* DQS# Enable: 0=enable, 1=disable */
1012 unsigned int ocd = 0; /* 0x0=OCD not supported,
1013 0x7=OCD default state */
1014 unsigned int rtt;
1015 unsigned int al; /* Posted CAS# additive latency (AL) */
1016 unsigned int ods = 0; /* Output Drive Strength:
1017 0 = Full strength (18ohm)
1018 1 = Reduced strength (4ohm) */
1019 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
1020 1=Disable (Test/Debug) */
1021
1022 /* Mode Register (MR) */
1023 unsigned int mr; /* Mode Register Definition */
1024 unsigned int pd; /* Power-Down Mode */
1025 unsigned int wr; /* Write Recovery */
1026 unsigned int dll_res; /* DLL Reset */
1027 unsigned int mode; /* Normal=0 or Test=1 */
Kumar Gala35ad58d2008-09-05 14:40:29 -05001028 unsigned int caslat = 0;/* CAS# latency */
Kumar Gala124b0822008-08-26 15:01:29 -05001029 /* BT: Burst Type (0=Sequential, 1=Interleaved) */
1030 unsigned int bt;
1031 unsigned int bl; /* BL: Burst Length */
1032
1033#if defined(CONFIG_FSL_DDR2)
1034 const unsigned int mclk_ps = get_memory_clk_period_ps();
1035#endif
York Sun15f874a2011-08-26 11:32:40 -07001036 dqs_en = !popts->DQS_config;
Kumar Gala124b0822008-08-26 15:01:29 -05001037 rtt = fsl_ddr_get_rtt();
1038
1039 al = additive_latency;
1040
1041 esdmode = (0
1042 | ((mrs & 0x3) << 14)
1043 | ((outputs & 0x1) << 12)
1044 | ((rdqs_en & 0x1) << 11)
1045 | ((dqs_en & 0x1) << 10)
1046 | ((ocd & 0x7) << 7)
1047 | ((rtt & 0x2) << 5) /* rtt field is split */
1048 | ((al & 0x7) << 3)
1049 | ((rtt & 0x1) << 2) /* rtt field is split */
1050 | ((ods & 0x1) << 1)
1051 | ((dll_en & 0x1) << 0)
1052 );
1053
1054 mr = 0; /* FIXME: CHECKME */
1055
1056 /*
1057 * 0 = Fast Exit (Normal)
1058 * 1 = Slow Exit (Low Power)
1059 */
1060 pd = 0;
1061
1062#if defined(CONFIG_FSL_DDR1)
1063 wr = 0; /* Historical */
1064#elif defined(CONFIG_FSL_DDR2)
1065 wr = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps - 1;
Kumar Gala124b0822008-08-26 15:01:29 -05001066#endif
1067 dll_res = 0;
1068 mode = 0;
1069
1070#if defined(CONFIG_FSL_DDR1)
1071 if (1 <= cas_latency && cas_latency <= 4) {
1072 unsigned char mode_caslat_table[4] = {
1073 0x5, /* 1.5 clocks */
1074 0x2, /* 2.0 clocks */
1075 0x6, /* 2.5 clocks */
1076 0x3 /* 3.0 clocks */
1077 };
Kumar Gala35ad58d2008-09-05 14:40:29 -05001078 caslat = mode_caslat_table[cas_latency - 1];
1079 } else {
1080 printf("Warning: unknown cas_latency %d\n", cas_latency);
Kumar Gala124b0822008-08-26 15:01:29 -05001081 }
1082#elif defined(CONFIG_FSL_DDR2)
1083 caslat = cas_latency;
Kumar Gala124b0822008-08-26 15:01:29 -05001084#endif
1085 bt = 0;
1086
1087 switch (popts->burst_length) {
Dave Liu4be87b22009-03-14 12:48:30 +08001088 case DDR_BL4:
Kumar Gala124b0822008-08-26 15:01:29 -05001089 bl = 2;
1090 break;
Dave Liu4be87b22009-03-14 12:48:30 +08001091 case DDR_BL8:
Kumar Gala124b0822008-08-26 15:01:29 -05001092 bl = 3;
1093 break;
1094 default:
1095 printf("Error: invalid burst length of %u specified. "
1096 " Defaulting to 4 beats.\n",
1097 popts->burst_length);
1098 bl = 2;
1099 break;
1100 }
1101
1102 sdmode = (0
1103 | ((mr & 0x3) << 14)
1104 | ((pd & 0x1) << 12)
1105 | ((wr & 0x7) << 9)
1106 | ((dll_res & 0x1) << 8)
1107 | ((mode & 0x1) << 7)
1108 | ((caslat & 0x7) << 4)
1109 | ((bt & 0x1) << 3)
1110 | ((bl & 0x7) << 0)
1111 );
1112
1113 ddr->ddr_sdram_mode = (0
1114 | ((esdmode & 0xFFFF) << 16)
1115 | ((sdmode & 0xFFFF) << 0)
1116 );
Haiying Wangd90e0402008-10-03 12:37:26 -04001117 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
Kumar Gala124b0822008-08-26 15:01:29 -05001118}
Dave Liu4be87b22009-03-14 12:48:30 +08001119#endif
Kumar Gala124b0822008-08-26 15:01:29 -05001120
1121/* DDR SDRAM Data Initialization (DDR_DATA_INIT) */
1122static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr)
1123{
1124 unsigned int init_value; /* Initialization value */
1125
1126 init_value = 0xDEADBEEF;
1127 ddr->ddr_data_init = init_value;
1128}
1129
1130/*
1131 * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL)
1132 * The old controller on the 8540/60 doesn't have this register.
1133 * Hope it's OK to set it (to 0) anyway.
1134 */
1135static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
1136 const memctl_options_t *popts)
1137{
1138 unsigned int clk_adjust; /* Clock adjust */
1139
1140 clk_adjust = popts->clk_adjust;
1141 ddr->ddr_sdram_clk_cntl = (clk_adjust & 0xF) << 23;
yorkde879322010-07-02 22:25:55 +00001142 debug("FSLDDR: clk_cntl = 0x%08x\n", ddr->ddr_sdram_clk_cntl);
Kumar Gala124b0822008-08-26 15:01:29 -05001143}
1144
1145/* DDR Initialization Address (DDR_INIT_ADDR) */
1146static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
1147{
1148 unsigned int init_addr = 0; /* Initialization address */
1149
1150 ddr->ddr_init_addr = init_addr;
1151}
1152
1153/* DDR Initialization Address (DDR_INIT_EXT_ADDR) */
1154static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
1155{
1156 unsigned int uia = 0; /* Use initialization address */
1157 unsigned int init_ext_addr = 0; /* Initialization address */
1158
1159 ddr->ddr_init_ext_addr = (0
1160 | ((uia & 0x1) << 31)
1161 | (init_ext_addr & 0xF)
1162 );
1163}
1164
1165/* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */
Dave Liu3525e1a2010-03-05 12:22:00 +08001166static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr,
1167 const memctl_options_t *popts)
Kumar Gala124b0822008-08-26 15:01:29 -05001168{
1169 unsigned int rwt = 0; /* Read-to-write turnaround for same CS */
1170 unsigned int wrt = 0; /* Write-to-read turnaround for same CS */
1171 unsigned int rrt = 0; /* Read-to-read turnaround for same CS */
1172 unsigned int wwt = 0; /* Write-to-write turnaround for same CS */
1173 unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */
1174
Dave Liu4be87b22009-03-14 12:48:30 +08001175#if defined(CONFIG_FSL_DDR3)
Dave Liu3525e1a2010-03-05 12:22:00 +08001176 if (popts->burst_length == DDR_BL8) {
1177 /* We set BL/2 for fixed BL8 */
1178 rrt = 0; /* BL/2 clocks */
1179 wwt = 0; /* BL/2 clocks */
1180 } else {
1181 /* We need to set BL/2 + 2 to BC4 and OTF */
1182 rrt = 2; /* BL/2 + 2 clocks */
1183 wwt = 2; /* BL/2 + 2 clocks */
1184 }
Dave Liu4be87b22009-03-14 12:48:30 +08001185 dll_lock = 1; /* tDLLK = 512 clocks from spec */
1186#endif
Kumar Gala124b0822008-08-26 15:01:29 -05001187 ddr->timing_cfg_4 = (0
1188 | ((rwt & 0xf) << 28)
1189 | ((wrt & 0xf) << 24)
1190 | ((rrt & 0xf) << 20)
1191 | ((wwt & 0xf) << 16)
1192 | (dll_lock & 0x3)
1193 );
Haiying Wangd90e0402008-10-03 12:37:26 -04001194 debug("FSLDDR: timing_cfg_4 = 0x%08x\n", ddr->timing_cfg_4);
Kumar Gala124b0822008-08-26 15:01:29 -05001195}
1196
1197/* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */
York Sunba0c2eb2011-01-10 12:03:00 +00001198static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr, unsigned int cas_latency)
Kumar Gala124b0822008-08-26 15:01:29 -05001199{
1200 unsigned int rodt_on = 0; /* Read to ODT on */
1201 unsigned int rodt_off = 0; /* Read to ODT off */
1202 unsigned int wodt_on = 0; /* Write to ODT on */
1203 unsigned int wodt_off = 0; /* Write to ODT off */
1204
Dave Liu4be87b22009-03-14 12:48:30 +08001205#if defined(CONFIG_FSL_DDR3)
York Sunba0c2eb2011-01-10 12:03:00 +00001206 /* rodt_on = timing_cfg_1[caslat] - timing_cfg_2[wrlat] + 1 */
1207 rodt_on = cas_latency - ((ddr->timing_cfg_2 & 0x00780000) >> 19) + 1;
Dave Liu4be87b22009-03-14 12:48:30 +08001208 rodt_off = 4; /* 4 clocks */
york1714e492010-07-02 22:25:56 +00001209 wodt_on = 1; /* 1 clocks */
Dave Liu4be87b22009-03-14 12:48:30 +08001210 wodt_off = 4; /* 4 clocks */
1211#endif
1212
Kumar Gala124b0822008-08-26 15:01:29 -05001213 ddr->timing_cfg_5 = (0
Dave Liu4758d532008-11-21 16:31:29 +08001214 | ((rodt_on & 0x1f) << 24)
1215 | ((rodt_off & 0x7) << 20)
1216 | ((wodt_on & 0x1f) << 12)
1217 | ((wodt_off & 0x7) << 8)
Kumar Gala124b0822008-08-26 15:01:29 -05001218 );
Haiying Wangd90e0402008-10-03 12:37:26 -04001219 debug("FSLDDR: timing_cfg_5 = 0x%08x\n", ddr->timing_cfg_5);
Kumar Gala124b0822008-08-26 15:01:29 -05001220}
1221
1222/* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */
Dave Liu4be87b22009-03-14 12:48:30 +08001223static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int zq_en)
Kumar Gala124b0822008-08-26 15:01:29 -05001224{
Kumar Gala124b0822008-08-26 15:01:29 -05001225 unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */
1226 /* Normal Operation Full Calibration Time (tZQoper) */
1227 unsigned int zqoper = 0;
1228 /* Normal Operation Short Calibration Time (tZQCS) */
1229 unsigned int zqcs = 0;
1230
Dave Liu4be87b22009-03-14 12:48:30 +08001231 if (zq_en) {
1232 zqinit = 9; /* 512 clocks */
1233 zqoper = 8; /* 256 clocks */
1234 zqcs = 6; /* 64 clocks */
1235 }
1236
Kumar Gala124b0822008-08-26 15:01:29 -05001237 ddr->ddr_zq_cntl = (0
1238 | ((zq_en & 0x1) << 31)
1239 | ((zqinit & 0xF) << 24)
1240 | ((zqoper & 0xF) << 16)
1241 | ((zqcs & 0xF) << 8)
1242 );
York Sunba0c2eb2011-01-10 12:03:00 +00001243 debug("FSLDDR: zq_cntl = 0x%08x\n", ddr->ddr_zq_cntl);
Kumar Gala124b0822008-08-26 15:01:29 -05001244}
1245
1246/* DDR Write Leveling Control (DDR_WRLVL_CNTL) */
Dave Liu64ee7df2009-12-16 10:24:37 -06001247static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int wrlvl_en,
1248 const memctl_options_t *popts)
Kumar Gala124b0822008-08-26 15:01:29 -05001249{
Kumar Gala124b0822008-08-26 15:01:29 -05001250 /*
1251 * First DQS pulse rising edge after margining mode
1252 * is programmed (tWL_MRD)
1253 */
1254 unsigned int wrlvl_mrd = 0;
1255 /* ODT delay after margining mode is programmed (tWL_ODTEN) */
1256 unsigned int wrlvl_odten = 0;
1257 /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */
1258 unsigned int wrlvl_dqsen = 0;
1259 /* WRLVL_SMPL: Write leveling sample time */
1260 unsigned int wrlvl_smpl = 0;
1261 /* WRLVL_WLR: Write leveling repeition time */
1262 unsigned int wrlvl_wlr = 0;
1263 /* WRLVL_START: Write leveling start time */
1264 unsigned int wrlvl_start = 0;
1265
Dave Liu4be87b22009-03-14 12:48:30 +08001266 /* suggest enable write leveling for DDR3 due to fly-by topology */
1267 if (wrlvl_en) {
1268 /* tWL_MRD min = 40 nCK, we set it 64 */
1269 wrlvl_mrd = 0x6;
1270 /* tWL_ODTEN 128 */
1271 wrlvl_odten = 0x7;
1272 /* tWL_DQSEN min = 25 nCK, we set it 32 */
1273 wrlvl_dqsen = 0x5;
1274 /*
Dave Liu64ee7df2009-12-16 10:24:37 -06001275 * Write leveling sample time at least need 6 clocks
1276 * higher than tWLO to allow enough time for progagation
1277 * delay and sampling the prime data bits.
Dave Liu4be87b22009-03-14 12:48:30 +08001278 */
1279 wrlvl_smpl = 0xf;
1280 /*
1281 * Write leveling repetition time
1282 * at least tWLO + 6 clocks clocks
york1714e492010-07-02 22:25:56 +00001283 * we set it 64
Dave Liu4be87b22009-03-14 12:48:30 +08001284 */
york1714e492010-07-02 22:25:56 +00001285 wrlvl_wlr = 0x6;
Dave Liu4be87b22009-03-14 12:48:30 +08001286 /*
1287 * Write leveling start time
1288 * The value use for the DQS_ADJUST for the first sample
York Sunba0c2eb2011-01-10 12:03:00 +00001289 * when write leveling is enabled. It probably needs to be
1290 * overriden per platform.
Dave Liu4be87b22009-03-14 12:48:30 +08001291 */
1292 wrlvl_start = 0x8;
Dave Liu64ee7df2009-12-16 10:24:37 -06001293 /*
1294 * Override the write leveling sample and start time
1295 * according to specific board
1296 */
1297 if (popts->wrlvl_override) {
1298 wrlvl_smpl = popts->wrlvl_sample;
1299 wrlvl_start = popts->wrlvl_start;
1300 }
Dave Liu4be87b22009-03-14 12:48:30 +08001301 }
1302
Kumar Gala124b0822008-08-26 15:01:29 -05001303 ddr->ddr_wrlvl_cntl = (0
1304 | ((wrlvl_en & 0x1) << 31)
1305 | ((wrlvl_mrd & 0x7) << 24)
1306 | ((wrlvl_odten & 0x7) << 20)
1307 | ((wrlvl_dqsen & 0x7) << 16)
1308 | ((wrlvl_smpl & 0xf) << 12)
1309 | ((wrlvl_wlr & 0x7) << 8)
Dave Liu4758d532008-11-21 16:31:29 +08001310 | ((wrlvl_start & 0x1F) << 0)
Kumar Gala124b0822008-08-26 15:01:29 -05001311 );
York Sunba0c2eb2011-01-10 12:03:00 +00001312 debug("FSLDDR: wrlvl_cntl = 0x%08x\n", ddr->ddr_wrlvl_cntl);
Kumar Gala124b0822008-08-26 15:01:29 -05001313}
1314
1315/* DDR Self Refresh Counter (DDR_SR_CNTR) */
Dave Liu2aad0ae2008-11-21 16:31:35 +08001316static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr, unsigned int sr_it)
Kumar Gala124b0822008-08-26 15:01:29 -05001317{
Dave Liu2aad0ae2008-11-21 16:31:35 +08001318 /* Self Refresh Idle Threshold */
Kumar Gala124b0822008-08-26 15:01:29 -05001319 ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
1320}
1321
york42603722010-07-02 22:25:54 +00001322static void set_ddr_eor(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
1323{
1324 if (popts->addr_hash) {
1325 ddr->ddr_eor = 0x40000000; /* address hash enable */
Kumar Gala4513d762011-03-18 11:53:06 -05001326 puts("Address hashing enabled.\n");
york42603722010-07-02 22:25:54 +00001327 }
1328}
1329
York Sunba0c2eb2011-01-10 12:03:00 +00001330static void set_ddr_cdr1(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
1331{
1332 ddr->ddr_cdr1 = popts->ddr_cdr1;
1333 debug("FSLDDR: ddr_cdr1 = 0x%08x\n", ddr->ddr_cdr1);
1334}
1335
Kumar Gala124b0822008-08-26 15:01:29 -05001336unsigned int
1337check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
1338{
1339 unsigned int res = 0;
1340
1341 /*
1342 * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are
1343 * not set at the same time.
1344 */
1345 if (ddr->ddr_sdram_cfg & 0x10000000
1346 && ddr->ddr_sdram_cfg & 0x00008000) {
1347 printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
1348 " should not be set at the same time.\n");
1349 res++;
1350 }
1351
1352 return res;
1353}
1354
1355unsigned int
1356compute_fsl_memctl_config_regs(const memctl_options_t *popts,
1357 fsl_ddr_cfg_regs_t *ddr,
1358 const common_timing_params_t *common_dimm,
1359 const dimm_params_t *dimm_params,
Haiying Wang80ad4012010-12-01 10:35:31 -05001360 unsigned int dbw_cap_adj,
1361 unsigned int size_only)
Kumar Gala124b0822008-08-26 15:01:29 -05001362{
1363 unsigned int i;
1364 unsigned int cas_latency;
1365 unsigned int additive_latency;
Dave Liu2aad0ae2008-11-21 16:31:35 +08001366 unsigned int sr_it;
Dave Liu4be87b22009-03-14 12:48:30 +08001367 unsigned int zq_en;
1368 unsigned int wrlvl_en;
York Sunba0c2eb2011-01-10 12:03:00 +00001369 unsigned int ip_rev = 0;
1370 unsigned int unq_mrs_en = 0;
York Sun2927c5e2010-10-18 13:46:50 -07001371 int cs_en = 1;
Kumar Gala124b0822008-08-26 15:01:29 -05001372
1373 memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
1374
1375 if (common_dimm == NULL) {
1376 printf("Error: subset DIMM params struct null pointer\n");
1377 return 1;
1378 }
1379
1380 /*
1381 * Process overrides first.
1382 *
1383 * FIXME: somehow add dereated caslat to this
1384 */
1385 cas_latency = (popts->cas_latency_override)
1386 ? popts->cas_latency_override_value
1387 : common_dimm->lowest_common_SPD_caslat;
1388
1389 additive_latency = (popts->additive_latency_override)
1390 ? popts->additive_latency_override_value
1391 : common_dimm->additive_latency;
1392
Dave Liu2aad0ae2008-11-21 16:31:35 +08001393 sr_it = (popts->auto_self_refresh_en)
1394 ? popts->sr_it
1395 : 0;
Dave Liu4be87b22009-03-14 12:48:30 +08001396 /* ZQ calibration */
1397 zq_en = (popts->zq_en) ? 1 : 0;
1398 /* write leveling */
1399 wrlvl_en = (popts->wrlvl_en) ? 1 : 0;
Dave Liu2aad0ae2008-11-21 16:31:35 +08001400
Kumar Gala124b0822008-08-26 15:01:29 -05001401 /* Chip Select Memory Bounds (CSn_BNDS) */
1402 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
Kumar Gala68ef4bd2009-06-11 23:42:35 -05001403 unsigned long long ea = 0, sa = 0;
york93799ca2010-07-02 22:25:52 +00001404 unsigned int cs_per_dimm
1405 = CONFIG_CHIP_SELECTS_PER_CTRL / CONFIG_DIMM_SLOTS_PER_CTLR;
1406 unsigned int dimm_number
1407 = i / cs_per_dimm;
1408 unsigned long long rank_density
1409 = dimm_params[dimm_number].rank_density;
Haiying Wang272b5962008-10-03 12:36:39 -04001410
york93799ca2010-07-02 22:25:52 +00001411 if (((i == 1) && (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1)) ||
1412 ((i == 2) && (popts->ba_intlv_ctl & 0x04)) ||
1413 ((i == 3) && (popts->ba_intlv_ctl & FSL_DDR_CS2_CS3))) {
1414 /*
1415 * Don't set up boundaries for unused CS
1416 * cs1 for cs0_cs1, cs0_cs1_and_cs2_cs3, cs0_cs1_cs2_cs3
1417 * cs2 for cs0_cs1_cs2_cs3
1418 * cs3 for cs2_cs3, cs0_cs1_and_cs2_cs3, cs0_cs1_cs2_cs3
Dave Liu625b2682009-12-16 10:24:39 -06001419 * But we need to set the ODT_RD_CFG and
1420 * ODT_WR_CFG for CS1_CONFIG here.
Haiying Wang272b5962008-10-03 12:36:39 -04001421 */
yorkf4f93c62010-07-02 22:25:53 +00001422 set_csn_config(dimm_number, i, ddr, popts, dimm_params);
york93799ca2010-07-02 22:25:52 +00001423 continue;
Kumar Gala124b0822008-08-26 15:01:29 -05001424 }
york93799ca2010-07-02 22:25:52 +00001425 if (dimm_params[dimm_number].n_ranks == 0) {
Kumar Gala124b0822008-08-26 15:01:29 -05001426 debug("Skipping setup of CS%u "
yorkf4f93c62010-07-02 22:25:53 +00001427 "because n_ranks on DIMM %u is 0\n", i, dimm_number);
Kumar Gala124b0822008-08-26 15:01:29 -05001428 continue;
1429 }
1430 if (popts->memctl_interleaving && popts->ba_intlv_ctl) {
1431 /*
1432 * This works superbank 2CS
york93799ca2010-07-02 22:25:52 +00001433 * There are 2 or more memory controllers configured
Kumar Gala124b0822008-08-26 15:01:29 -05001434 * identically, memory is interleaved between them,
1435 * and each controller uses rank interleaving within
1436 * itself. Therefore the starting and ending address
1437 * on each controller is twice the amount present on
York Sun2927c5e2010-10-18 13:46:50 -07001438 * each controller. If any CS is not included in the
1439 * interleaving, the memory on that CS is not accssible
1440 * and the total memory size is reduced. The CS is also
1441 * disabled.
Kumar Gala124b0822008-08-26 15:01:29 -05001442 */
york93799ca2010-07-02 22:25:52 +00001443 unsigned long long ctlr_density = 0;
1444 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
1445 case FSL_DDR_CS0_CS1:
1446 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
1447 ctlr_density = dimm_params[0].rank_density * 2;
York Sun2927c5e2010-10-18 13:46:50 -07001448 if (i > 1)
1449 cs_en = 0;
york93799ca2010-07-02 22:25:52 +00001450 break;
1451 case FSL_DDR_CS2_CS3:
1452 ctlr_density = dimm_params[0].rank_density;
York Sun2927c5e2010-10-18 13:46:50 -07001453 if (i > 0)
1454 cs_en = 0;
york93799ca2010-07-02 22:25:52 +00001455 break;
1456 case FSL_DDR_CS0_CS1_CS2_CS3:
1457 /*
1458 * The four CS interleaving should have been verified by
1459 * populate_memctl_options()
1460 */
1461 ctlr_density = dimm_params[0].rank_density * 4;
1462 break;
1463 default:
1464 break;
1465 }
1466 ea = (CONFIG_NUM_DDR_CONTROLLERS *
1467 (ctlr_density >> dbw_cap_adj)) - 1;
Kumar Gala124b0822008-08-26 15:01:29 -05001468 }
1469 else if (!popts->memctl_interleaving && popts->ba_intlv_ctl) {
1470 /*
1471 * If memory interleaving between controllers is NOT
1472 * enabled, the starting address for each memory
1473 * controller is distinct. However, because rank
1474 * interleaving is enabled, the starting and ending
1475 * addresses of the total memory on that memory
1476 * controller needs to be programmed into its
1477 * respective CS0_BNDS.
1478 */
Haiying Wang272b5962008-10-03 12:36:39 -04001479 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
1480 case FSL_DDR_CS0_CS1_CS2_CS3:
1481 /* CS0+CS1+CS2+CS3 interleaving, only CS0_CNDS
1482 * needs to be set.
1483 */
1484 sa = common_dimm->base_address;
1485 ea = sa + (4 * (rank_density >> dbw_cap_adj))-1;
1486 break;
1487 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
1488 /* CS0+CS1 and CS2+CS3 interleaving, CS0_CNDS
1489 * and CS2_CNDS need to be set.
1490 */
york93799ca2010-07-02 22:25:52 +00001491 if ((i == 2) && (dimm_number == 0)) {
1492 sa = dimm_params[dimm_number].base_address +
1493 2 * (rank_density >> dbw_cap_adj);
1494 ea = sa + 2 * (rank_density >> dbw_cap_adj) - 1;
1495 } else {
1496 sa = dimm_params[dimm_number].base_address;
1497 ea = sa + (2 * (rank_density >>
Haiying Wang272b5962008-10-03 12:36:39 -04001498 dbw_cap_adj)) - 1;
1499 }
1500 break;
1501 case FSL_DDR_CS0_CS1:
1502 /* CS0+CS1 interleaving, CS0_CNDS needs
1503 * to be set
1504 */
york93799ca2010-07-02 22:25:52 +00001505 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
1506 sa = dimm_params[dimm_number].base_address;
1507 ea = sa + (rank_density >> dbw_cap_adj) - 1;
1508 sa += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
1509 ea += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
1510 } else {
1511 sa = 0;
1512 ea = 0;
1513 }
1514 if (i == 0)
1515 ea += (rank_density >> dbw_cap_adj);
Haiying Wang272b5962008-10-03 12:36:39 -04001516 break;
1517 case FSL_DDR_CS2_CS3:
1518 /* CS2+CS3 interleaving*/
york93799ca2010-07-02 22:25:52 +00001519 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
1520 sa = dimm_params[dimm_number].base_address;
1521 ea = sa + (rank_density >> dbw_cap_adj) - 1;
1522 sa += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
1523 ea += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
1524 } else {
1525 sa = 0;
1526 ea = 0;
Haiying Wang272b5962008-10-03 12:36:39 -04001527 }
york93799ca2010-07-02 22:25:52 +00001528 if (i == 2)
1529 ea += (rank_density >> dbw_cap_adj);
Haiying Wang272b5962008-10-03 12:36:39 -04001530 break;
1531 default: /* No bank(chip-select) interleaving */
1532 break;
1533 }
Kumar Gala124b0822008-08-26 15:01:29 -05001534 }
1535 else if (popts->memctl_interleaving && !popts->ba_intlv_ctl) {
1536 /*
1537 * Only the rank on CS0 of each memory controller may
1538 * be used if memory controller interleaving is used
1539 * without rank interleaving within each memory
1540 * controller. However, the ending address programmed
1541 * into each CS0 must be the sum of the amount of
1542 * memory in the two CS0 ranks.
1543 */
1544 if (i == 0) {
Kumar Gala124b0822008-08-26 15:01:29 -05001545 ea = (2 * (rank_density >> dbw_cap_adj)) - 1;
1546 }
1547
1548 }
1549 else if (!popts->memctl_interleaving && !popts->ba_intlv_ctl) {
1550 /*
1551 * No rank interleaving and no memory controller
1552 * interleaving.
1553 */
york93799ca2010-07-02 22:25:52 +00001554 sa = dimm_params[dimm_number].base_address;
Kumar Gala124b0822008-08-26 15:01:29 -05001555 ea = sa + (rank_density >> dbw_cap_adj) - 1;
york93799ca2010-07-02 22:25:52 +00001556 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
1557 sa += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
1558 ea += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
1559 } else {
1560 sa = 0;
1561 ea = 0;
Kumar Gala124b0822008-08-26 15:01:29 -05001562 }
1563 }
1564
1565 sa >>= 24;
1566 ea >>= 24;
1567
1568 ddr->cs[i].bnds = (0
1569 | ((sa & 0xFFF) << 16) /* starting address MSB */
1570 | ((ea & 0xFFF) << 0) /* ending address MSB */
1571 );
1572
Haiying Wangd90e0402008-10-03 12:37:26 -04001573 debug("FSLDDR: cs[%d]_bnds = 0x%08x\n", i, ddr->cs[i].bnds);
York Sun2927c5e2010-10-18 13:46:50 -07001574 if (cs_en) {
1575 set_csn_config(dimm_number, i, ddr, popts, dimm_params);
1576 set_csn_config_2(i, ddr);
1577 } else
1578 printf("CS%d is disabled.\n", i);
Kumar Gala124b0822008-08-26 15:01:29 -05001579 }
1580
Haiying Wang80ad4012010-12-01 10:35:31 -05001581 /*
1582 * In the case we only need to compute the ddr sdram size, we only need
1583 * to set csn registers, so return from here.
1584 */
1585 if (size_only)
1586 return 0;
1587
york42603722010-07-02 22:25:54 +00001588 set_ddr_eor(ddr, popts);
1589
Dave Liu4be87b22009-03-14 12:48:30 +08001590#if !defined(CONFIG_FSL_DDR1)
York Sunba0c2eb2011-01-10 12:03:00 +00001591 set_timing_cfg_0(ddr, popts);
Kumar Gala124b0822008-08-26 15:01:29 -05001592#endif
1593
Dave Liu4be87b22009-03-14 12:48:30 +08001594 set_timing_cfg_3(ddr, common_dimm, cas_latency);
1595 set_timing_cfg_1(ddr, popts, common_dimm, cas_latency);
Kumar Gala124b0822008-08-26 15:01:29 -05001596 set_timing_cfg_2(ddr, popts, common_dimm,
1597 cas_latency, additive_latency);
1598
York Sunba0c2eb2011-01-10 12:03:00 +00001599 set_ddr_cdr1(ddr, popts);
Kumar Gala124b0822008-08-26 15:01:29 -05001600 set_ddr_sdram_cfg(ddr, popts, common_dimm);
York Sunba0c2eb2011-01-10 12:03:00 +00001601 ip_rev = fsl_ddr_get_version();
1602 if (ip_rev > 0x40400)
1603 unq_mrs_en = 1;
Kumar Gala124b0822008-08-26 15:01:29 -05001604
York Sunba0c2eb2011-01-10 12:03:00 +00001605 set_ddr_sdram_cfg_2(ddr, popts, unq_mrs_en);
Kumar Gala124b0822008-08-26 15:01:29 -05001606 set_ddr_sdram_mode(ddr, popts, common_dimm,
York Sunba0c2eb2011-01-10 12:03:00 +00001607 cas_latency, additive_latency, unq_mrs_en);
1608 set_ddr_sdram_mode_2(ddr, popts, unq_mrs_en);
Kumar Gala124b0822008-08-26 15:01:29 -05001609 set_ddr_sdram_interval(ddr, popts, common_dimm);
1610 set_ddr_data_init(ddr);
1611 set_ddr_sdram_clk_cntl(ddr, popts);
1612 set_ddr_init_addr(ddr);
1613 set_ddr_init_ext_addr(ddr);
Dave Liu3525e1a2010-03-05 12:22:00 +08001614 set_timing_cfg_4(ddr, popts);
York Sunba0c2eb2011-01-10 12:03:00 +00001615 set_timing_cfg_5(ddr, cas_latency);
Kumar Gala124b0822008-08-26 15:01:29 -05001616
Dave Liu4be87b22009-03-14 12:48:30 +08001617 set_ddr_zq_cntl(ddr, zq_en);
Dave Liu64ee7df2009-12-16 10:24:37 -06001618 set_ddr_wrlvl_cntl(ddr, wrlvl_en, popts);
Kumar Gala124b0822008-08-26 15:01:29 -05001619
Dave Liu2aad0ae2008-11-21 16:31:35 +08001620 set_ddr_sr_cntr(ddr, sr_it);
Kumar Gala124b0822008-08-26 15:01:29 -05001621
York Sunba0c2eb2011-01-10 12:03:00 +00001622 set_ddr_sdram_rcw(ddr, popts, common_dimm);
Kumar Gala124b0822008-08-26 15:01:29 -05001623
1624 return check_fsl_memctl_config_regs(ddr);
1625}