blob: 5e37ca6b80643f79ba863192d712b2bfa00aaa40 [file] [log] [blame]
Kumar Gala124b0822008-08-26 15:01:29 -05001/*
Kumar Gala14f2eb12009-09-10 14:54:55 -05002 * Copyright 2008-2009 Freescale Semiconductor, Inc.
Kumar Gala124b0822008-08-26 15:01:29 -05003 *
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 *
Dave Liu4be87b22009-03-14 12:48:30 +080026 * Rtt(nominal) - DDR2:
Kumar Gala124b0822008-08-26 15:01:29 -050027 * 0 = Rtt disabled
28 * 1 = 75 ohm
29 * 2 = 150 ohm
30 * 3 = 50 ohm
Dave Liu4be87b22009-03-14 12:48:30 +080031 * Rtt(nominal) - DDR3:
32 * 0 = Rtt disabled
33 * 1 = 60 ohm
34 * 2 = 120 ohm
35 * 3 = 40 ohm
36 * 4 = 20 ohm
37 * 5 = 30 ohm
Kumar Gala124b0822008-08-26 15:01:29 -050038 *
39 * FIXME: Apparently 8641 needs a value of 2
40 * FIXME: Old code seys if 667 MHz or higher, use 3 on 8572
41 *
42 * FIXME: There was some effort down this line earlier:
43 *
44 * unsigned int i;
45 * for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL/2; i++) {
46 * if (popts->dimmslot[i].num_valid_cs
47 * && (popts->cs_local_opts[2*i].odt_rd_cfg
48 * || popts->cs_local_opts[2*i].odt_wr_cfg)) {
49 * rtt = 2;
50 * break;
51 * }
52 * }
53 */
54static inline int fsl_ddr_get_rtt(void)
55{
56 int rtt;
57
58#if defined(CONFIG_FSL_DDR1)
59 rtt = 0;
60#elif defined(CONFIG_FSL_DDR2)
61 rtt = 3;
62#else
Dave Liu4be87b22009-03-14 12:48:30 +080063 rtt = 0;
Kumar Gala124b0822008-08-26 15:01:29 -050064#endif
65
66 return rtt;
67}
68
Dave Liu4be87b22009-03-14 12:48:30 +080069/*
70 * compute the CAS write latency according to DDR3 spec
71 * CWL = 5 if tCK >= 2.5ns
72 * 6 if 2.5ns > tCK >= 1.875ns
73 * 7 if 1.875ns > tCK >= 1.5ns
74 * 8 if 1.5ns > tCK >= 1.25ns
75 */
76static inline unsigned int compute_cas_write_latency(void)
77{
78 unsigned int cwl;
79 const unsigned int mclk_ps = get_memory_clk_period_ps();
80
81 if (mclk_ps >= 2500)
82 cwl = 5;
83 else if (mclk_ps >= 1875)
84 cwl = 6;
85 else if (mclk_ps >= 1500)
86 cwl = 7;
87 else if (mclk_ps >= 1250)
88 cwl = 8;
89 else
90 cwl = 8;
91 return cwl;
92}
93
Kumar Gala124b0822008-08-26 15:01:29 -050094/* Chip Select Configuration (CSn_CONFIG) */
95static void set_csn_config(int i, fsl_ddr_cfg_regs_t *ddr,
96 const memctl_options_t *popts,
97 const dimm_params_t *dimm_params)
98{
99 unsigned int cs_n_en = 0; /* Chip Select enable */
100 unsigned int intlv_en = 0; /* Memory controller interleave enable */
101 unsigned int intlv_ctl = 0; /* Interleaving control */
102 unsigned int ap_n_en = 0; /* Chip select n auto-precharge enable */
103 unsigned int odt_rd_cfg = 0; /* ODT for reads configuration */
104 unsigned int odt_wr_cfg = 0; /* ODT for writes configuration */
105 unsigned int ba_bits_cs_n = 0; /* Num of bank bits for SDRAM on CSn */
106 unsigned int row_bits_cs_n = 0; /* Num of row bits for SDRAM on CSn */
107 unsigned int col_bits_cs_n = 0; /* Num of ocl bits for SDRAM on CSn */
108
109 /* Compute CS_CONFIG only for existing ranks of each DIMM. */
110 if ((((i&1) == 0)
111 && (dimm_params[i/2].n_ranks == 1))
112 || (dimm_params[i/2].n_ranks == 2)) {
113 unsigned int n_banks_per_sdram_device;
114 cs_n_en = 1;
115 if (i == 0) {
116 /* These fields only available in CS0_CONFIG */
117 intlv_en = popts->memctl_interleaving;
118 intlv_ctl = popts->memctl_interleaving_mode;
119 }
120 ap_n_en = popts->cs_local_opts[i].auto_precharge;
121 odt_rd_cfg = popts->cs_local_opts[i].odt_rd_cfg;
122 odt_wr_cfg = popts->cs_local_opts[i].odt_wr_cfg;
123 n_banks_per_sdram_device
124 = dimm_params[i/2].n_banks_per_sdram_device;
125 ba_bits_cs_n = __ilog2(n_banks_per_sdram_device) - 2;
126 row_bits_cs_n = dimm_params[i/2].n_row_addr - 12;
127 col_bits_cs_n = dimm_params[i/2].n_col_addr - 8;
128 }
129
Kumar Gala124b0822008-08-26 15:01:29 -0500130 ddr->cs[i].config = (0
131 | ((cs_n_en & 0x1) << 31)
132 | ((intlv_en & 0x3) << 29)
Haiying Wang272b5962008-10-03 12:36:39 -0400133 | ((intlv_ctl & 0xf) << 24)
Kumar Gala124b0822008-08-26 15:01:29 -0500134 | ((ap_n_en & 0x1) << 23)
135
136 /* XXX: some implementation only have 1 bit starting at left */
137 | ((odt_rd_cfg & 0x7) << 20)
138
139 /* XXX: Some implementation only have 1 bit starting at left */
140 | ((odt_wr_cfg & 0x7) << 16)
141
142 | ((ba_bits_cs_n & 0x3) << 14)
143 | ((row_bits_cs_n & 0x7) << 8)
144 | ((col_bits_cs_n & 0x7) << 0)
145 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400146 debug("FSLDDR: cs[%d]_config = 0x%08x\n", i,ddr->cs[i].config);
Kumar Gala124b0822008-08-26 15:01:29 -0500147}
148
149/* Chip Select Configuration 2 (CSn_CONFIG_2) */
150/* FIXME: 8572 */
151static void set_csn_config_2(int i, fsl_ddr_cfg_regs_t *ddr)
152{
153 unsigned int pasr_cfg = 0; /* Partial array self refresh config */
154
155 ddr->cs[i].config_2 = ((pasr_cfg & 7) << 24);
Haiying Wangd90e0402008-10-03 12:37:26 -0400156 debug("FSLDDR: cs[%d]_config_2 = 0x%08x\n", i, ddr->cs[i].config_2);
Kumar Gala124b0822008-08-26 15:01:29 -0500157}
158
159/* -3E = 667 CL5, -25 = CL6 800, -25E = CL5 800 */
160
Dave Liu4be87b22009-03-14 12:48:30 +0800161#if !defined(CONFIG_FSL_DDR1)
Kumar Gala124b0822008-08-26 15:01:29 -0500162/*
163 * DDR SDRAM Timing Configuration 0 (TIMING_CFG_0)
164 *
165 * Avoid writing for DDR I. The new PQ38 DDR controller
166 * dreams up non-zero default values to be backwards compatible.
167 */
168static void set_timing_cfg_0(fsl_ddr_cfg_regs_t *ddr)
169{
170 unsigned char trwt_mclk = 0; /* Read-to-write turnaround */
171 unsigned char twrt_mclk = 0; /* Write-to-read turnaround */
172 /* 7.5 ns on -3E; 0 means WL - CL + BL/2 + 1 */
173 unsigned char trrt_mclk = 0; /* Read-to-read turnaround */
174 unsigned char twwt_mclk = 0; /* Write-to-write turnaround */
175
176 /* Active powerdown exit timing (tXARD and tXARDS). */
177 unsigned char act_pd_exit_mclk;
178 /* Precharge powerdown exit timing (tXP). */
179 unsigned char pre_pd_exit_mclk;
180 /* Precharge powerdown exit timing (tAXPD). */
181 unsigned char taxpd_mclk;
182 /* Mode register set cycle time (tMRD). */
183 unsigned char tmrd_mclk;
184
Dave Liu4be87b22009-03-14 12:48:30 +0800185#if defined(CONFIG_FSL_DDR3)
186 /*
187 * (tXARD and tXARDS). Empirical?
188 * The DDR3 spec has not tXARD,
189 * we use the tXP instead of it.
190 * tXP=max(3nCK, 7.5ns) for DDR3.
Dave Liu4be87b22009-03-14 12:48:30 +0800191 * spec has not the tAXPD, we use
192 * tAXPD=8, need design to confirm.
193 */
Dave Liuc7d983a2009-12-16 10:24:36 -0600194 int tXP = max((get_memory_clk_period_ps() * 3), 7500); /* unit=ps */
195 act_pd_exit_mclk = picos_to_mclk(tXP);
196 /* Mode register MR0[A12] is '1' - fast exit */
197 pre_pd_exit_mclk = act_pd_exit_mclk;
Kumar Gala124b0822008-08-26 15:01:29 -0500198 taxpd_mclk = 8;
Dave Liu4be87b22009-03-14 12:48:30 +0800199 tmrd_mclk = 4;
200#else /* CONFIG_FSL_DDR2 */
201 /*
202 * (tXARD and tXARDS). Empirical?
203 * tXARD = 2 for DDR2
204 * tXP=2
205 * tAXPD=8
206 */
207 act_pd_exit_mclk = 2;
208 pre_pd_exit_mclk = 2;
209 taxpd_mclk = 8;
Kumar Gala124b0822008-08-26 15:01:29 -0500210 tmrd_mclk = 2;
Dave Liu4be87b22009-03-14 12:48:30 +0800211#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500212
213 ddr->timing_cfg_0 = (0
214 | ((trwt_mclk & 0x3) << 30) /* RWT */
215 | ((twrt_mclk & 0x3) << 28) /* WRT */
216 | ((trrt_mclk & 0x3) << 26) /* RRT */
217 | ((twwt_mclk & 0x3) << 24) /* WWT */
218 | ((act_pd_exit_mclk & 0x7) << 20) /* ACT_PD_EXIT */
Dave Liu4758d532008-11-21 16:31:29 +0800219 | ((pre_pd_exit_mclk & 0xF) << 16) /* PRE_PD_EXIT */
Kumar Gala124b0822008-08-26 15:01:29 -0500220 | ((taxpd_mclk & 0xf) << 8) /* ODT_PD_EXIT */
221 | ((tmrd_mclk & 0xf) << 0) /* MRS_CYC */
222 );
223 debug("FSLDDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
224}
225#endif /* defined(CONFIG_FSL_DDR2) */
226
227/* DDR SDRAM Timing Configuration 3 (TIMING_CFG_3) */
228static void set_timing_cfg_3(fsl_ddr_cfg_regs_t *ddr,
Dave Liu4be87b22009-03-14 12:48:30 +0800229 const common_timing_params_t *common_dimm,
230 unsigned int cas_latency)
Kumar Gala124b0822008-08-26 15:01:29 -0500231{
232 /* Extended Activate to precharge interval (tRAS) */
233 unsigned int ext_acttopre = 0;
234 unsigned int ext_refrec; /* Extended refresh recovery time (tRFC) */
235 unsigned int ext_caslat = 0; /* Extended MCAS latency from READ cmd */
236 unsigned int cntl_adj = 0; /* Control Adjust */
237
Dave Liu5c1bb512008-11-21 16:31:22 +0800238 /* If the tRAS > 19 MCLK, we use the ext mode */
239 if (picos_to_mclk(common_dimm->tRAS_ps) > 0x13)
240 ext_acttopre = 1;
241
Kumar Gala124b0822008-08-26 15:01:29 -0500242 ext_refrec = (picos_to_mclk(common_dimm->tRFC_ps) - 8) >> 4;
Dave Liu4be87b22009-03-14 12:48:30 +0800243
244 /* If the CAS latency more than 8, use the ext mode */
245 if (cas_latency > 8)
246 ext_caslat = 1;
247
Kumar Gala124b0822008-08-26 15:01:29 -0500248 ddr->timing_cfg_3 = (0
249 | ((ext_acttopre & 0x1) << 24)
Dave Liu5c1bb512008-11-21 16:31:22 +0800250 | ((ext_refrec & 0xF) << 16)
Kumar Gala124b0822008-08-26 15:01:29 -0500251 | ((ext_caslat & 0x1) << 12)
252 | ((cntl_adj & 0x7) << 0)
253 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400254 debug("FSLDDR: timing_cfg_3 = 0x%08x\n", ddr->timing_cfg_3);
Kumar Gala124b0822008-08-26 15:01:29 -0500255}
256
257/* DDR SDRAM Timing Configuration 1 (TIMING_CFG_1) */
258static void set_timing_cfg_1(fsl_ddr_cfg_regs_t *ddr,
Dave Liu4be87b22009-03-14 12:48:30 +0800259 const memctl_options_t *popts,
Kumar Gala124b0822008-08-26 15:01:29 -0500260 const common_timing_params_t *common_dimm,
261 unsigned int cas_latency)
262{
263 /* Precharge-to-activate interval (tRP) */
264 unsigned char pretoact_mclk;
265 /* Activate to precharge interval (tRAS) */
266 unsigned char acttopre_mclk;
267 /* Activate to read/write interval (tRCD) */
268 unsigned char acttorw_mclk;
269 /* CASLAT */
270 unsigned char caslat_ctrl;
271 /* Refresh recovery time (tRFC) ; trfc_low */
272 unsigned char refrec_ctrl;
273 /* Last data to precharge minimum interval (tWR) */
274 unsigned char wrrec_mclk;
275 /* Activate-to-activate interval (tRRD) */
276 unsigned char acttoact_mclk;
277 /* Last write data pair to read command issue interval (tWTR) */
278 unsigned char wrtord_mclk;
279
280 pretoact_mclk = picos_to_mclk(common_dimm->tRP_ps);
281 acttopre_mclk = picos_to_mclk(common_dimm->tRAS_ps);
282 acttorw_mclk = picos_to_mclk(common_dimm->tRCD_ps);
283
284 /*
285 * Translate CAS Latency to a DDR controller field value:
286 *
287 * CAS Lat DDR I DDR II Ctrl
288 * Clocks SPD Bit SPD Bit Value
289 * ------- ------- ------- -----
290 * 1.0 0 0001
291 * 1.5 1 0010
292 * 2.0 2 2 0011
293 * 2.5 3 0100
294 * 3.0 4 3 0101
295 * 3.5 5 0110
296 * 4.0 4 0111
297 * 4.5 1000
298 * 5.0 5 1001
299 */
300#if defined(CONFIG_FSL_DDR1)
301 caslat_ctrl = (cas_latency + 1) & 0x07;
302#elif defined(CONFIG_FSL_DDR2)
303 caslat_ctrl = 2 * cas_latency - 1;
304#else
Dave Liu4be87b22009-03-14 12:48:30 +0800305 /*
306 * if the CAS latency more than 8 cycle,
307 * we need set extend bit for it at
308 * TIMING_CFG_3[EXT_CASLAT]
309 */
310 if (cas_latency > 8)
311 cas_latency -= 8;
312 caslat_ctrl = 2 * cas_latency - 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500313#endif
314
315 refrec_ctrl = picos_to_mclk(common_dimm->tRFC_ps) - 8;
316 wrrec_mclk = picos_to_mclk(common_dimm->tWR_ps);
Dave Liu4be87b22009-03-14 12:48:30 +0800317 if (popts->OTF_burst_chop_en)
318 wrrec_mclk += 2;
319
Kumar Gala124b0822008-08-26 15:01:29 -0500320 acttoact_mclk = picos_to_mclk(common_dimm->tRRD_ps);
Dave Liu4be87b22009-03-14 12:48:30 +0800321 /*
322 * JEDEC has min requirement for tRRD
323 */
324#if defined(CONFIG_FSL_DDR3)
325 if (acttoact_mclk < 4)
326 acttoact_mclk = 4;
327#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500328 wrtord_mclk = picos_to_mclk(common_dimm->tWTR_ps);
Dave Liu4be87b22009-03-14 12:48:30 +0800329 /*
330 * JEDEC has some min requirements for tWTR
331 */
332#if defined(CONFIG_FSL_DDR2)
333 if (wrtord_mclk < 2)
334 wrtord_mclk = 2;
335#elif defined(CONFIG_FSL_DDR3)
336 if (wrtord_mclk < 4)
337 wrtord_mclk = 4;
338#endif
339 if (popts->OTF_burst_chop_en)
340 wrtord_mclk += 2;
Kumar Gala124b0822008-08-26 15:01:29 -0500341
342 ddr->timing_cfg_1 = (0
Dave Liu5c1bb512008-11-21 16:31:22 +0800343 | ((pretoact_mclk & 0x0F) << 28)
Kumar Gala124b0822008-08-26 15:01:29 -0500344 | ((acttopre_mclk & 0x0F) << 24)
Dave Liu5c1bb512008-11-21 16:31:22 +0800345 | ((acttorw_mclk & 0xF) << 20)
Kumar Gala124b0822008-08-26 15:01:29 -0500346 | ((caslat_ctrl & 0xF) << 16)
347 | ((refrec_ctrl & 0xF) << 12)
Dave Liu5c1bb512008-11-21 16:31:22 +0800348 | ((wrrec_mclk & 0x0F) << 8)
Kumar Gala124b0822008-08-26 15:01:29 -0500349 | ((acttoact_mclk & 0x07) << 4)
350 | ((wrtord_mclk & 0x07) << 0)
351 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400352 debug("FSLDDR: timing_cfg_1 = 0x%08x\n", ddr->timing_cfg_1);
Kumar Gala124b0822008-08-26 15:01:29 -0500353}
354
355/* DDR SDRAM Timing Configuration 2 (TIMING_CFG_2) */
356static void set_timing_cfg_2(fsl_ddr_cfg_regs_t *ddr,
357 const memctl_options_t *popts,
358 const common_timing_params_t *common_dimm,
359 unsigned int cas_latency,
360 unsigned int additive_latency)
361{
362 /* Additive latency */
363 unsigned char add_lat_mclk;
364 /* CAS-to-preamble override */
365 unsigned short cpo;
366 /* Write latency */
367 unsigned char wr_lat;
368 /* Read to precharge (tRTP) */
369 unsigned char rd_to_pre;
370 /* Write command to write data strobe timing adjustment */
371 unsigned char wr_data_delay;
372 /* Minimum CKE pulse width (tCKE) */
373 unsigned char cke_pls;
374 /* Window for four activates (tFAW) */
375 unsigned short four_act;
376
377 /* FIXME add check that this must be less than acttorw_mclk */
378 add_lat_mclk = additive_latency;
379 cpo = popts->cpo_override;
380
381#if defined(CONFIG_FSL_DDR1)
382 /*
383 * This is a lie. It should really be 1, but if it is
384 * set to 1, bits overlap into the old controller's
385 * otherwise unused ACSM field. If we leave it 0, then
386 * the HW will magically treat it as 1 for DDR 1. Oh Yea.
387 */
388 wr_lat = 0;
389#elif defined(CONFIG_FSL_DDR2)
Dave Liu82aa9532009-03-14 12:48:19 +0800390 wr_lat = cas_latency - 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500391#else
Dave Liu4be87b22009-03-14 12:48:30 +0800392 wr_lat = compute_cas_write_latency();
Kumar Gala124b0822008-08-26 15:01:29 -0500393#endif
394
395 rd_to_pre = picos_to_mclk(common_dimm->tRTP_ps);
Dave Liu4be87b22009-03-14 12:48:30 +0800396 /*
397 * JEDEC has some min requirements for tRTP
398 */
Dave Liu82aa9532009-03-14 12:48:19 +0800399#if defined(CONFIG_FSL_DDR2)
Dave Liu4be87b22009-03-14 12:48:30 +0800400 if (rd_to_pre < 2)
401 rd_to_pre = 2;
402#elif defined(CONFIG_FSL_DDR3)
403 if (rd_to_pre < 4)
404 rd_to_pre = 4;
Dave Liu82aa9532009-03-14 12:48:19 +0800405#endif
Dave Liu4be87b22009-03-14 12:48:30 +0800406 if (additive_latency)
407 rd_to_pre += additive_latency;
408 if (popts->OTF_burst_chop_en)
409 rd_to_pre += 2; /* according to UM */
410
Kumar Gala124b0822008-08-26 15:01:29 -0500411 wr_data_delay = popts->write_data_delay;
412 cke_pls = picos_to_mclk(popts->tCKE_clock_pulse_width_ps);
413 four_act = picos_to_mclk(popts->tFAW_window_four_activates_ps);
414
415 ddr->timing_cfg_2 = (0
Dave Liu4758d532008-11-21 16:31:29 +0800416 | ((add_lat_mclk & 0xf) << 28)
Kumar Gala124b0822008-08-26 15:01:29 -0500417 | ((cpo & 0x1f) << 23)
Dave Liu4758d532008-11-21 16:31:29 +0800418 | ((wr_lat & 0xf) << 19)
Dave Liu4be87b22009-03-14 12:48:30 +0800419 | ((rd_to_pre & RD_TO_PRE_MASK) << RD_TO_PRE_SHIFT)
420 | ((wr_data_delay & WR_DATA_DELAY_MASK) << WR_DATA_DELAY_SHIFT)
Kumar Gala124b0822008-08-26 15:01:29 -0500421 | ((cke_pls & 0x7) << 6)
Dave Liu4758d532008-11-21 16:31:29 +0800422 | ((four_act & 0x3f) << 0)
Kumar Gala124b0822008-08-26 15:01:29 -0500423 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400424 debug("FSLDDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2);
Kumar Gala124b0822008-08-26 15:01:29 -0500425}
426
427/* DDR SDRAM control configuration (DDR_SDRAM_CFG) */
428static void set_ddr_sdram_cfg(fsl_ddr_cfg_regs_t *ddr,
429 const memctl_options_t *popts,
430 const common_timing_params_t *common_dimm)
431{
432 unsigned int mem_en; /* DDR SDRAM interface logic enable */
433 unsigned int sren; /* Self refresh enable (during sleep) */
434 unsigned int ecc_en; /* ECC enable. */
435 unsigned int rd_en; /* Registered DIMM enable */
436 unsigned int sdram_type; /* Type of SDRAM */
437 unsigned int dyn_pwr; /* Dynamic power management mode */
438 unsigned int dbw; /* DRAM dta bus width */
Dave Liu4758d532008-11-21 16:31:29 +0800439 unsigned int eight_be = 0; /* 8-beat burst enable, DDR2 is zero */
Kumar Gala124b0822008-08-26 15:01:29 -0500440 unsigned int ncap = 0; /* Non-concurrent auto-precharge */
441 unsigned int threeT_en; /* Enable 3T timing */
442 unsigned int twoT_en; /* Enable 2T timing */
443 unsigned int ba_intlv_ctl; /* Bank (CS) interleaving control */
444 unsigned int x32_en = 0; /* x32 enable */
445 unsigned int pchb8 = 0; /* precharge bit 8 enable */
446 unsigned int hse; /* Global half strength override */
447 unsigned int mem_halt = 0; /* memory controller halt */
448 unsigned int bi = 0; /* Bypass initialization */
449
450 mem_en = 1;
451 sren = popts->self_refresh_in_sleep;
452 if (common_dimm->all_DIMMs_ECC_capable) {
453 /* Allow setting of ECC only if all DIMMs are ECC. */
454 ecc_en = popts->ECC_mode;
455 } else {
456 ecc_en = 0;
457 }
458
459 rd_en = (common_dimm->all_DIMMs_registered
460 && !common_dimm->all_DIMMs_unbuffered);
461
462 sdram_type = CONFIG_FSL_SDRAM_TYPE;
463
464 dyn_pwr = popts->dynamic_power;
465 dbw = popts->data_bus_width;
Dave Liu4be87b22009-03-14 12:48:30 +0800466 /* 8-beat burst enable DDR-III case
467 * we must clear it when use the on-the-fly mode,
468 * must set it when use the 32-bits bus mode.
469 */
470 if (sdram_type == SDRAM_TYPE_DDR3) {
471 if (popts->burst_length == DDR_BL8)
472 eight_be = 1;
473 if (popts->burst_length == DDR_OTF)
474 eight_be = 0;
475 if (dbw == 0x1)
476 eight_be = 1;
477 }
478
Kumar Gala124b0822008-08-26 15:01:29 -0500479 threeT_en = popts->threeT_en;
480 twoT_en = popts->twoT_en;
481 ba_intlv_ctl = popts->ba_intlv_ctl;
482 hse = popts->half_strength_driver_enable;
483
484 ddr->ddr_sdram_cfg = (0
485 | ((mem_en & 0x1) << 31)
486 | ((sren & 0x1) << 30)
487 | ((ecc_en & 0x1) << 29)
488 | ((rd_en & 0x1) << 28)
489 | ((sdram_type & 0x7) << 24)
490 | ((dyn_pwr & 0x1) << 21)
491 | ((dbw & 0x3) << 19)
492 | ((eight_be & 0x1) << 18)
493 | ((ncap & 0x1) << 17)
494 | ((threeT_en & 0x1) << 16)
495 | ((twoT_en & 0x1) << 15)
496 | ((ba_intlv_ctl & 0x7F) << 8)
497 | ((x32_en & 0x1) << 5)
498 | ((pchb8 & 0x1) << 4)
499 | ((hse & 0x1) << 3)
500 | ((mem_halt & 0x1) << 1)
501 | ((bi & 0x1) << 0)
502 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400503 debug("FSLDDR: ddr_sdram_cfg = 0x%08x\n", ddr->ddr_sdram_cfg);
Kumar Gala124b0822008-08-26 15:01:29 -0500504}
505
506/* DDR SDRAM control configuration 2 (DDR_SDRAM_CFG_2) */
507static void set_ddr_sdram_cfg_2(fsl_ddr_cfg_regs_t *ddr,
508 const memctl_options_t *popts)
509{
510 unsigned int frc_sr = 0; /* Force self refresh */
511 unsigned int sr_ie = 0; /* Self-refresh interrupt enable */
512 unsigned int dll_rst_dis; /* DLL reset disable */
513 unsigned int dqs_cfg; /* DQS configuration */
514 unsigned int odt_cfg; /* ODT configuration */
515 unsigned int num_pr; /* Number of posted refreshes */
516 unsigned int obc_cfg; /* On-The-Fly Burst Chop Cfg */
517 unsigned int ap_en; /* Address Parity Enable */
518 unsigned int d_init; /* DRAM data initialization */
519 unsigned int rcw_en = 0; /* Register Control Word Enable */
520 unsigned int md_en = 0; /* Mirrored DIMM Enable */
521
522 dll_rst_dis = 1; /* Make this configurable */
523 dqs_cfg = popts->DQS_config;
524 if (popts->cs_local_opts[0].odt_rd_cfg
525 || popts->cs_local_opts[0].odt_wr_cfg) {
526 /* FIXME */
527 odt_cfg = 2;
528 } else {
529 odt_cfg = 0;
530 }
531
532 num_pr = 1; /* Make this configurable */
533
534 /*
535 * 8572 manual says
536 * {TIMING_CFG_1[PRETOACT]
537 * + [DDR_SDRAM_CFG_2[NUM_PR]
538 * * ({EXT_REFREC || REFREC} + 8 + 2)]}
539 * << DDR_SDRAM_INTERVAL[REFINT]
540 */
Dave Liu4be87b22009-03-14 12:48:30 +0800541#if defined(CONFIG_FSL_DDR3)
542 obc_cfg = popts->OTF_burst_chop_en;
543#else
544 obc_cfg = 0;
545#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500546
Kumar Gala124b0822008-08-26 15:01:29 -0500547 ap_en = 0; /* Make this configurable? */
548
549#if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
550 /* Use the DDR controller to auto initialize memory. */
551 d_init = 1;
552 ddr->ddr_data_init = CONFIG_MEM_INIT_VALUE;
553 debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init);
554#else
555 /* Memory will be initialized via DMA, or not at all. */
556 d_init = 0;
557#endif
558
Dave Liu4be87b22009-03-14 12:48:30 +0800559#if defined(CONFIG_FSL_DDR3)
560 md_en = popts->mirrored_dimm;
561#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500562 ddr->ddr_sdram_cfg_2 = (0
563 | ((frc_sr & 0x1) << 31)
564 | ((sr_ie & 0x1) << 30)
565 | ((dll_rst_dis & 0x1) << 29)
566 | ((dqs_cfg & 0x3) << 26)
567 | ((odt_cfg & 0x3) << 21)
568 | ((num_pr & 0xf) << 12)
569 | ((obc_cfg & 0x1) << 6)
570 | ((ap_en & 0x1) << 5)
571 | ((d_init & 0x1) << 4)
572 | ((rcw_en & 0x1) << 2)
573 | ((md_en & 0x1) << 0)
574 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400575 debug("FSLDDR: ddr_sdram_cfg_2 = 0x%08x\n", ddr->ddr_sdram_cfg_2);
Kumar Gala124b0822008-08-26 15:01:29 -0500576}
577
578/* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
579static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr)
580{
581 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
582 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
583
Dave Liu4be87b22009-03-14 12:48:30 +0800584#if defined(CONFIG_FSL_DDR3)
585 unsigned int rtt_wr = 2; /* 120 ohm Rtt_WR */
586 unsigned int srt = 0; /* self-refresh temerature, normal range */
587 unsigned int asr = 0; /* auto self-refresh disable */
588 unsigned int cwl = compute_cas_write_latency() - 5;
589 unsigned int pasr = 0; /* partial array self refresh disable */
590
591 esdmode2 = (0
592 | ((rtt_wr & 0x3) << 9)
593 | ((srt & 0x1) << 7)
594 | ((asr & 0x1) << 6)
595 | ((cwl & 0x7) << 3)
596 | ((pasr & 0x7) << 0));
597#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500598 ddr->ddr_sdram_mode_2 = (0
599 | ((esdmode2 & 0xFFFF) << 16)
600 | ((esdmode3 & 0xFFFF) << 0)
601 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400602 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
Kumar Gala124b0822008-08-26 15:01:29 -0500603}
604
605/* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */
606static void set_ddr_sdram_interval(fsl_ddr_cfg_regs_t *ddr,
607 const memctl_options_t *popts,
608 const common_timing_params_t *common_dimm)
609{
610 unsigned int refint; /* Refresh interval */
611 unsigned int bstopre; /* Precharge interval */
612
613 refint = picos_to_mclk(common_dimm->refresh_rate_ps);
614
615 bstopre = popts->bstopre;
616
617 /* refint field used 0x3FFF in earlier controllers */
618 ddr->ddr_sdram_interval = (0
619 | ((refint & 0xFFFF) << 16)
620 | ((bstopre & 0x3FFF) << 0)
621 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400622 debug("FSLDDR: ddr_sdram_interval = 0x%08x\n", ddr->ddr_sdram_interval);
Kumar Gala124b0822008-08-26 15:01:29 -0500623}
624
Dave Liu4be87b22009-03-14 12:48:30 +0800625#if defined(CONFIG_FSL_DDR3)
Kumar Gala124b0822008-08-26 15:01:29 -0500626/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
627static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
628 const memctl_options_t *popts,
629 const common_timing_params_t *common_dimm,
630 unsigned int cas_latency,
631 unsigned int additive_latency)
632{
633 unsigned short esdmode; /* Extended SDRAM mode */
634 unsigned short sdmode; /* SDRAM mode */
635
Dave Liu4be87b22009-03-14 12:48:30 +0800636 /* Mode Register - MR1 */
637 unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */
638 unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */
639 unsigned int rtt;
640 unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */
641 unsigned int al = 0; /* Posted CAS# additive latency (AL) */
642 unsigned int dic = 1; /* Output driver impedance, 34ohm */
643 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
644 1=Disable (Test/Debug) */
645
646 /* Mode Register - MR0 */
647 unsigned int dll_on; /* DLL control for precharge PD, 0=off, 1=on */
648 unsigned int wr; /* Write Recovery */
649 unsigned int dll_rst; /* DLL Reset */
650 unsigned int mode; /* Normal=0 or Test=1 */
651 unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
652 /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
653 unsigned int bt;
654 unsigned int bl; /* BL: Burst Length */
655
656 unsigned int wr_mclk;
657
658 const unsigned int mclk_ps = get_memory_clk_period_ps();
659
660 rtt = fsl_ddr_get_rtt();
661 if (popts->rtt_override)
662 rtt = popts->rtt_override_value;
663
664 if (additive_latency == (cas_latency - 1))
665 al = 1;
666 if (additive_latency == (cas_latency - 2))
667 al = 2;
668
669 /*
670 * The esdmode value will also be used for writing
671 * MR1 during write leveling for DDR3, although the
672 * bits specifically related to the write leveling
673 * scheme will be handled automatically by the DDR
674 * controller. so we set the wrlvl_en = 0 here.
675 */
676 esdmode = (0
677 | ((qoff & 0x1) << 12)
678 | ((tdqs_en & 0x1) << 11)
Kumar Gala14f2eb12009-09-10 14:54:55 -0500679 | ((rtt & 0x4) << 7) /* rtt field is split */
Dave Liu4be87b22009-03-14 12:48:30 +0800680 | ((wrlvl_en & 0x1) << 7)
Kumar Gala14f2eb12009-09-10 14:54:55 -0500681 | ((rtt & 0x2) << 5) /* rtt field is split */
682 | ((dic & 0x2) << 4) /* DIC field is split */
Dave Liu4be87b22009-03-14 12:48:30 +0800683 | ((al & 0x3) << 3)
Kumar Gala14f2eb12009-09-10 14:54:55 -0500684 | ((rtt & 0x1) << 2) /* rtt field is split */
Dave Liu4be87b22009-03-14 12:48:30 +0800685 | ((dic & 0x1) << 1) /* DIC field is split */
686 | ((dll_en & 0x1) << 0)
687 );
688
689 /*
690 * DLL control for precharge PD
691 * 0=slow exit DLL off (tXPDLL)
692 * 1=fast exit DLL on (tXP)
693 */
694 dll_on = 1;
695 wr_mclk = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps;
696 if (wr_mclk >= 12)
697 wr = 6;
698 else if (wr_mclk >= 9)
699 wr = 5;
700 else
701 wr = wr_mclk - 4;
702 dll_rst = 0; /* dll no reset */
703 mode = 0; /* normal mode */
704
705 /* look up table to get the cas latency bits */
706 if (cas_latency >= 5 && cas_latency <= 11) {
707 unsigned char cas_latency_table[7] = {
708 0x2, /* 5 clocks */
709 0x4, /* 6 clocks */
710 0x6, /* 7 clocks */
711 0x8, /* 8 clocks */
712 0xa, /* 9 clocks */
713 0xc, /* 10 clocks */
714 0xe /* 11 clocks */
715 };
716 caslat = cas_latency_table[cas_latency - 5];
717 }
718 bt = 0; /* Nibble sequential */
719
720 switch (popts->burst_length) {
721 case DDR_BL8:
722 bl = 0;
723 break;
724 case DDR_OTF:
725 bl = 1;
726 break;
727 case DDR_BC4:
728 bl = 2;
729 break;
730 default:
731 printf("Error: invalid burst length of %u specified. "
732 " Defaulting to on-the-fly BC4 or BL8 beats.\n",
733 popts->burst_length);
734 bl = 1;
735 break;
736 }
737
738 sdmode = (0
739 | ((dll_on & 0x1) << 12)
740 | ((wr & 0x7) << 9)
741 | ((dll_rst & 0x1) << 8)
742 | ((mode & 0x1) << 7)
743 | (((caslat >> 1) & 0x7) << 4)
744 | ((bt & 0x1) << 3)
745 | ((bl & 0x3) << 0)
746 );
747
748 ddr->ddr_sdram_mode = (0
749 | ((esdmode & 0xFFFF) << 16)
750 | ((sdmode & 0xFFFF) << 0)
751 );
752
753 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
754}
755
756#else /* !CONFIG_FSL_DDR3 */
757
758/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
759static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
760 const memctl_options_t *popts,
761 const common_timing_params_t *common_dimm,
762 unsigned int cas_latency,
763 unsigned int additive_latency)
764{
765 unsigned short esdmode; /* Extended SDRAM mode */
766 unsigned short sdmode; /* SDRAM mode */
767
Kumar Gala124b0822008-08-26 15:01:29 -0500768 /*
769 * FIXME: This ought to be pre-calculated in a
770 * technology-specific routine,
771 * e.g. compute_DDR2_mode_register(), and then the
772 * sdmode and esdmode passed in as part of common_dimm.
773 */
774
775 /* Extended Mode Register */
776 unsigned int mrs = 0; /* Mode Register Set */
777 unsigned int outputs = 0; /* 0=Enabled, 1=Disabled */
778 unsigned int rdqs_en = 0; /* RDQS Enable: 0=no, 1=yes */
779 unsigned int dqs_en = 0; /* DQS# Enable: 0=enable, 1=disable */
780 unsigned int ocd = 0; /* 0x0=OCD not supported,
781 0x7=OCD default state */
782 unsigned int rtt;
783 unsigned int al; /* Posted CAS# additive latency (AL) */
784 unsigned int ods = 0; /* Output Drive Strength:
785 0 = Full strength (18ohm)
786 1 = Reduced strength (4ohm) */
787 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
788 1=Disable (Test/Debug) */
789
790 /* Mode Register (MR) */
791 unsigned int mr; /* Mode Register Definition */
792 unsigned int pd; /* Power-Down Mode */
793 unsigned int wr; /* Write Recovery */
794 unsigned int dll_res; /* DLL Reset */
795 unsigned int mode; /* Normal=0 or Test=1 */
Kumar Gala35ad58d2008-09-05 14:40:29 -0500796 unsigned int caslat = 0;/* CAS# latency */
Kumar Gala124b0822008-08-26 15:01:29 -0500797 /* BT: Burst Type (0=Sequential, 1=Interleaved) */
798 unsigned int bt;
799 unsigned int bl; /* BL: Burst Length */
800
801#if defined(CONFIG_FSL_DDR2)
802 const unsigned int mclk_ps = get_memory_clk_period_ps();
803#endif
804
805 rtt = fsl_ddr_get_rtt();
806
807 al = additive_latency;
808
809 esdmode = (0
810 | ((mrs & 0x3) << 14)
811 | ((outputs & 0x1) << 12)
812 | ((rdqs_en & 0x1) << 11)
813 | ((dqs_en & 0x1) << 10)
814 | ((ocd & 0x7) << 7)
815 | ((rtt & 0x2) << 5) /* rtt field is split */
816 | ((al & 0x7) << 3)
817 | ((rtt & 0x1) << 2) /* rtt field is split */
818 | ((ods & 0x1) << 1)
819 | ((dll_en & 0x1) << 0)
820 );
821
822 mr = 0; /* FIXME: CHECKME */
823
824 /*
825 * 0 = Fast Exit (Normal)
826 * 1 = Slow Exit (Low Power)
827 */
828 pd = 0;
829
830#if defined(CONFIG_FSL_DDR1)
831 wr = 0; /* Historical */
832#elif defined(CONFIG_FSL_DDR2)
833 wr = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps - 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500834#endif
835 dll_res = 0;
836 mode = 0;
837
838#if defined(CONFIG_FSL_DDR1)
839 if (1 <= cas_latency && cas_latency <= 4) {
840 unsigned char mode_caslat_table[4] = {
841 0x5, /* 1.5 clocks */
842 0x2, /* 2.0 clocks */
843 0x6, /* 2.5 clocks */
844 0x3 /* 3.0 clocks */
845 };
Kumar Gala35ad58d2008-09-05 14:40:29 -0500846 caslat = mode_caslat_table[cas_latency - 1];
847 } else {
848 printf("Warning: unknown cas_latency %d\n", cas_latency);
Kumar Gala124b0822008-08-26 15:01:29 -0500849 }
850#elif defined(CONFIG_FSL_DDR2)
851 caslat = cas_latency;
Kumar Gala124b0822008-08-26 15:01:29 -0500852#endif
853 bt = 0;
854
855 switch (popts->burst_length) {
Dave Liu4be87b22009-03-14 12:48:30 +0800856 case DDR_BL4:
Kumar Gala124b0822008-08-26 15:01:29 -0500857 bl = 2;
858 break;
Dave Liu4be87b22009-03-14 12:48:30 +0800859 case DDR_BL8:
Kumar Gala124b0822008-08-26 15:01:29 -0500860 bl = 3;
861 break;
862 default:
863 printf("Error: invalid burst length of %u specified. "
864 " Defaulting to 4 beats.\n",
865 popts->burst_length);
866 bl = 2;
867 break;
868 }
869
870 sdmode = (0
871 | ((mr & 0x3) << 14)
872 | ((pd & 0x1) << 12)
873 | ((wr & 0x7) << 9)
874 | ((dll_res & 0x1) << 8)
875 | ((mode & 0x1) << 7)
876 | ((caslat & 0x7) << 4)
877 | ((bt & 0x1) << 3)
878 | ((bl & 0x7) << 0)
879 );
880
881 ddr->ddr_sdram_mode = (0
882 | ((esdmode & 0xFFFF) << 16)
883 | ((sdmode & 0xFFFF) << 0)
884 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400885 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
Kumar Gala124b0822008-08-26 15:01:29 -0500886}
Dave Liu4be87b22009-03-14 12:48:30 +0800887#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500888
889/* DDR SDRAM Data Initialization (DDR_DATA_INIT) */
890static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr)
891{
892 unsigned int init_value; /* Initialization value */
893
894 init_value = 0xDEADBEEF;
895 ddr->ddr_data_init = init_value;
896}
897
898/*
899 * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL)
900 * The old controller on the 8540/60 doesn't have this register.
901 * Hope it's OK to set it (to 0) anyway.
902 */
903static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
904 const memctl_options_t *popts)
905{
906 unsigned int clk_adjust; /* Clock adjust */
907
908 clk_adjust = popts->clk_adjust;
909 ddr->ddr_sdram_clk_cntl = (clk_adjust & 0xF) << 23;
910}
911
912/* DDR Initialization Address (DDR_INIT_ADDR) */
913static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
914{
915 unsigned int init_addr = 0; /* Initialization address */
916
917 ddr->ddr_init_addr = init_addr;
918}
919
920/* DDR Initialization Address (DDR_INIT_EXT_ADDR) */
921static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
922{
923 unsigned int uia = 0; /* Use initialization address */
924 unsigned int init_ext_addr = 0; /* Initialization address */
925
926 ddr->ddr_init_ext_addr = (0
927 | ((uia & 0x1) << 31)
928 | (init_ext_addr & 0xF)
929 );
930}
931
932/* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */
933static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr)
934{
935 unsigned int rwt = 0; /* Read-to-write turnaround for same CS */
936 unsigned int wrt = 0; /* Write-to-read turnaround for same CS */
937 unsigned int rrt = 0; /* Read-to-read turnaround for same CS */
938 unsigned int wwt = 0; /* Write-to-write turnaround for same CS */
939 unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */
940
Dave Liu4be87b22009-03-14 12:48:30 +0800941#if defined(CONFIG_FSL_DDR3)
942 /* We need set BL/2 + 4 for BC4 or OTF */
943 rrt = 4; /* BL/2 + 4 clocks */
944 wwt = 4; /* BL/2 + 4 clocks */
945 dll_lock = 1; /* tDLLK = 512 clocks from spec */
946#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500947 ddr->timing_cfg_4 = (0
948 | ((rwt & 0xf) << 28)
949 | ((wrt & 0xf) << 24)
950 | ((rrt & 0xf) << 20)
951 | ((wwt & 0xf) << 16)
952 | (dll_lock & 0x3)
953 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400954 debug("FSLDDR: timing_cfg_4 = 0x%08x\n", ddr->timing_cfg_4);
Kumar Gala124b0822008-08-26 15:01:29 -0500955}
956
957/* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */
958static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr)
959{
960 unsigned int rodt_on = 0; /* Read to ODT on */
961 unsigned int rodt_off = 0; /* Read to ODT off */
962 unsigned int wodt_on = 0; /* Write to ODT on */
963 unsigned int wodt_off = 0; /* Write to ODT off */
964
Dave Liu4be87b22009-03-14 12:48:30 +0800965#if defined(CONFIG_FSL_DDR3)
966 rodt_on = 3; /* 2 clocks */
967 rodt_off = 4; /* 4 clocks */
968 wodt_on = 2; /* 1 clocks */
969 wodt_off = 4; /* 4 clocks */
970#endif
971
Kumar Gala124b0822008-08-26 15:01:29 -0500972 ddr->timing_cfg_5 = (0
Dave Liu4758d532008-11-21 16:31:29 +0800973 | ((rodt_on & 0x1f) << 24)
974 | ((rodt_off & 0x7) << 20)
975 | ((wodt_on & 0x1f) << 12)
976 | ((wodt_off & 0x7) << 8)
Kumar Gala124b0822008-08-26 15:01:29 -0500977 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400978 debug("FSLDDR: timing_cfg_5 = 0x%08x\n", ddr->timing_cfg_5);
Kumar Gala124b0822008-08-26 15:01:29 -0500979}
980
981/* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */
Dave Liu4be87b22009-03-14 12:48:30 +0800982static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int zq_en)
Kumar Gala124b0822008-08-26 15:01:29 -0500983{
Kumar Gala124b0822008-08-26 15:01:29 -0500984 unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */
985 /* Normal Operation Full Calibration Time (tZQoper) */
986 unsigned int zqoper = 0;
987 /* Normal Operation Short Calibration Time (tZQCS) */
988 unsigned int zqcs = 0;
989
Dave Liu4be87b22009-03-14 12:48:30 +0800990 if (zq_en) {
991 zqinit = 9; /* 512 clocks */
992 zqoper = 8; /* 256 clocks */
993 zqcs = 6; /* 64 clocks */
994 }
995
Kumar Gala124b0822008-08-26 15:01:29 -0500996 ddr->ddr_zq_cntl = (0
997 | ((zq_en & 0x1) << 31)
998 | ((zqinit & 0xF) << 24)
999 | ((zqoper & 0xF) << 16)
1000 | ((zqcs & 0xF) << 8)
1001 );
1002}
1003
1004/* DDR Write Leveling Control (DDR_WRLVL_CNTL) */
Dave Liu64ee7df2009-12-16 10:24:37 -06001005static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int wrlvl_en,
1006 const memctl_options_t *popts)
Kumar Gala124b0822008-08-26 15:01:29 -05001007{
Kumar Gala124b0822008-08-26 15:01:29 -05001008 /*
1009 * First DQS pulse rising edge after margining mode
1010 * is programmed (tWL_MRD)
1011 */
1012 unsigned int wrlvl_mrd = 0;
1013 /* ODT delay after margining mode is programmed (tWL_ODTEN) */
1014 unsigned int wrlvl_odten = 0;
1015 /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */
1016 unsigned int wrlvl_dqsen = 0;
1017 /* WRLVL_SMPL: Write leveling sample time */
1018 unsigned int wrlvl_smpl = 0;
1019 /* WRLVL_WLR: Write leveling repeition time */
1020 unsigned int wrlvl_wlr = 0;
1021 /* WRLVL_START: Write leveling start time */
1022 unsigned int wrlvl_start = 0;
1023
Dave Liu4be87b22009-03-14 12:48:30 +08001024 /* suggest enable write leveling for DDR3 due to fly-by topology */
1025 if (wrlvl_en) {
1026 /* tWL_MRD min = 40 nCK, we set it 64 */
1027 wrlvl_mrd = 0x6;
1028 /* tWL_ODTEN 128 */
1029 wrlvl_odten = 0x7;
1030 /* tWL_DQSEN min = 25 nCK, we set it 32 */
1031 wrlvl_dqsen = 0x5;
1032 /*
Dave Liu64ee7df2009-12-16 10:24:37 -06001033 * Write leveling sample time at least need 6 clocks
1034 * higher than tWLO to allow enough time for progagation
1035 * delay and sampling the prime data bits.
Dave Liu4be87b22009-03-14 12:48:30 +08001036 */
1037 wrlvl_smpl = 0xf;
1038 /*
1039 * Write leveling repetition time
1040 * at least tWLO + 6 clocks clocks
1041 * we set it 32
1042 */
1043 wrlvl_wlr = 0x5;
1044 /*
1045 * Write leveling start time
1046 * The value use for the DQS_ADJUST for the first sample
1047 * when write leveling is enabled.
Dave Liu4be87b22009-03-14 12:48:30 +08001048 */
1049 wrlvl_start = 0x8;
Dave Liu64ee7df2009-12-16 10:24:37 -06001050 /*
1051 * Override the write leveling sample and start time
1052 * according to specific board
1053 */
1054 if (popts->wrlvl_override) {
1055 wrlvl_smpl = popts->wrlvl_sample;
1056 wrlvl_start = popts->wrlvl_start;
1057 }
Dave Liu4be87b22009-03-14 12:48:30 +08001058 }
1059
Kumar Gala124b0822008-08-26 15:01:29 -05001060 ddr->ddr_wrlvl_cntl = (0
1061 | ((wrlvl_en & 0x1) << 31)
1062 | ((wrlvl_mrd & 0x7) << 24)
1063 | ((wrlvl_odten & 0x7) << 20)
1064 | ((wrlvl_dqsen & 0x7) << 16)
1065 | ((wrlvl_smpl & 0xf) << 12)
1066 | ((wrlvl_wlr & 0x7) << 8)
Dave Liu4758d532008-11-21 16:31:29 +08001067 | ((wrlvl_start & 0x1F) << 0)
Kumar Gala124b0822008-08-26 15:01:29 -05001068 );
1069}
1070
1071/* DDR Self Refresh Counter (DDR_SR_CNTR) */
Dave Liu2aad0ae2008-11-21 16:31:35 +08001072static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr, unsigned int sr_it)
Kumar Gala124b0822008-08-26 15:01:29 -05001073{
Dave Liu2aad0ae2008-11-21 16:31:35 +08001074 /* Self Refresh Idle Threshold */
Kumar Gala124b0822008-08-26 15:01:29 -05001075 ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
1076}
1077
Kumar Gala124b0822008-08-26 15:01:29 -05001078/* DDR SDRAM Register Control Word 1 (DDR_SDRAM_RCW_1) */
1079static void set_ddr_sdram_rcw_1(fsl_ddr_cfg_regs_t *ddr)
1080{
1081 unsigned int rcw0 = 0; /* RCW0: Register Control Word 0 */
1082 unsigned int rcw1 = 0; /* RCW1: Register Control Word 1 */
1083 unsigned int rcw2 = 0; /* RCW2: Register Control Word 2 */
1084 unsigned int rcw3 = 0; /* RCW3: Register Control Word 3 */
1085 unsigned int rcw4 = 0; /* RCW4: Register Control Word 4 */
1086 unsigned int rcw5 = 0; /* RCW5: Register Control Word 5 */
1087 unsigned int rcw6 = 0; /* RCW6: Register Control Word 6 */
1088 unsigned int rcw7 = 0; /* RCW7: Register Control Word 7 */
1089
1090 ddr->ddr_sdram_rcw_1 = (0
1091 | ((rcw0 & 0xF) << 28)
1092 | ((rcw1 & 0xF) << 24)
1093 | ((rcw2 & 0xF) << 20)
1094 | ((rcw3 & 0xF) << 16)
1095 | ((rcw4 & 0xF) << 12)
1096 | ((rcw5 & 0xF) << 8)
1097 | ((rcw6 & 0xF) << 4)
1098 | ((rcw7 & 0xF) << 0)
1099 );
1100}
1101
1102/* DDR SDRAM Register Control Word 2 (DDR_SDRAM_RCW_2) */
1103static void set_ddr_sdram_rcw_2(fsl_ddr_cfg_regs_t *ddr)
1104{
1105 unsigned int rcw8 = 0; /* RCW0: Register Control Word 8 */
1106 unsigned int rcw9 = 0; /* RCW1: Register Control Word 9 */
1107 unsigned int rcw10 = 0; /* RCW2: Register Control Word 10 */
1108 unsigned int rcw11 = 0; /* RCW3: Register Control Word 11 */
1109 unsigned int rcw12 = 0; /* RCW4: Register Control Word 12 */
1110 unsigned int rcw13 = 0; /* RCW5: Register Control Word 13 */
1111 unsigned int rcw14 = 0; /* RCW6: Register Control Word 14 */
1112 unsigned int rcw15 = 0; /* RCW7: Register Control Word 15 */
1113
1114 ddr->ddr_sdram_rcw_2 = (0
1115 | ((rcw8 & 0xF) << 28)
1116 | ((rcw9 & 0xF) << 24)
1117 | ((rcw10 & 0xF) << 20)
1118 | ((rcw11 & 0xF) << 16)
1119 | ((rcw12 & 0xF) << 12)
1120 | ((rcw13 & 0xF) << 8)
1121 | ((rcw14 & 0xF) << 4)
1122 | ((rcw15 & 0xF) << 0)
1123 );
1124}
1125
1126unsigned int
1127check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
1128{
1129 unsigned int res = 0;
1130
1131 /*
1132 * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are
1133 * not set at the same time.
1134 */
1135 if (ddr->ddr_sdram_cfg & 0x10000000
1136 && ddr->ddr_sdram_cfg & 0x00008000) {
1137 printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
1138 " should not be set at the same time.\n");
1139 res++;
1140 }
1141
1142 return res;
1143}
1144
1145unsigned int
1146compute_fsl_memctl_config_regs(const memctl_options_t *popts,
1147 fsl_ddr_cfg_regs_t *ddr,
1148 const common_timing_params_t *common_dimm,
1149 const dimm_params_t *dimm_params,
1150 unsigned int dbw_cap_adj)
1151{
1152 unsigned int i;
1153 unsigned int cas_latency;
1154 unsigned int additive_latency;
Dave Liu2aad0ae2008-11-21 16:31:35 +08001155 unsigned int sr_it;
Dave Liu4be87b22009-03-14 12:48:30 +08001156 unsigned int zq_en;
1157 unsigned int wrlvl_en;
Kumar Gala124b0822008-08-26 15:01:29 -05001158
1159 memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
1160
1161 if (common_dimm == NULL) {
1162 printf("Error: subset DIMM params struct null pointer\n");
1163 return 1;
1164 }
1165
1166 /*
1167 * Process overrides first.
1168 *
1169 * FIXME: somehow add dereated caslat to this
1170 */
1171 cas_latency = (popts->cas_latency_override)
1172 ? popts->cas_latency_override_value
1173 : common_dimm->lowest_common_SPD_caslat;
1174
1175 additive_latency = (popts->additive_latency_override)
1176 ? popts->additive_latency_override_value
1177 : common_dimm->additive_latency;
1178
Dave Liu2aad0ae2008-11-21 16:31:35 +08001179 sr_it = (popts->auto_self_refresh_en)
1180 ? popts->sr_it
1181 : 0;
Dave Liu4be87b22009-03-14 12:48:30 +08001182 /* ZQ calibration */
1183 zq_en = (popts->zq_en) ? 1 : 0;
1184 /* write leveling */
1185 wrlvl_en = (popts->wrlvl_en) ? 1 : 0;
Dave Liu2aad0ae2008-11-21 16:31:35 +08001186
Kumar Gala124b0822008-08-26 15:01:29 -05001187 /* Chip Select Memory Bounds (CSn_BNDS) */
1188 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
Kumar Gala68ef4bd2009-06-11 23:42:35 -05001189 unsigned long long ea = 0, sa = 0;
Haiying Wang272b5962008-10-03 12:36:39 -04001190
1191 if (popts->ba_intlv_ctl && (i > 0) &&
1192 ((popts->ba_intlv_ctl & 0x60) != FSL_DDR_CS2_CS3 )) {
1193 /* Don't set up boundaries for other CS
1194 * other than CS0, if bank interleaving
1195 * is enabled and not CS2+CS3 interleaved.
1196 */
Kumar Gala124b0822008-08-26 15:01:29 -05001197 break;
1198 }
1199
1200 if (dimm_params[i/2].n_ranks == 0) {
1201 debug("Skipping setup of CS%u "
1202 "because n_ranks on DIMM %u is 0\n", i, i/2);
1203 continue;
1204 }
1205 if (popts->memctl_interleaving && popts->ba_intlv_ctl) {
1206 /*
1207 * This works superbank 2CS
1208 * There are 2 memory controllers configured
1209 * identically, memory is interleaved between them,
1210 * and each controller uses rank interleaving within
1211 * itself. Therefore the starting and ending address
1212 * on each controller is twice the amount present on
1213 * each controller.
1214 */
Haiying Wang272b5962008-10-03 12:36:39 -04001215 unsigned long long rank_density
1216 = dimm_params[0].capacity;
1217 ea = (2 * (rank_density >> dbw_cap_adj)) - 1;
Kumar Gala124b0822008-08-26 15:01:29 -05001218 }
1219 else if (!popts->memctl_interleaving && popts->ba_intlv_ctl) {
1220 /*
1221 * If memory interleaving between controllers is NOT
1222 * enabled, the starting address for each memory
1223 * controller is distinct. However, because rank
1224 * interleaving is enabled, the starting and ending
1225 * addresses of the total memory on that memory
1226 * controller needs to be programmed into its
1227 * respective CS0_BNDS.
1228 */
Haiying Wang272b5962008-10-03 12:36:39 -04001229 unsigned long long rank_density
1230 = dimm_params[i/2].rank_density;
1231 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
1232 case FSL_DDR_CS0_CS1_CS2_CS3:
1233 /* CS0+CS1+CS2+CS3 interleaving, only CS0_CNDS
1234 * needs to be set.
1235 */
1236 sa = common_dimm->base_address;
1237 ea = sa + (4 * (rank_density >> dbw_cap_adj))-1;
1238 break;
1239 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
1240 /* CS0+CS1 and CS2+CS3 interleaving, CS0_CNDS
1241 * and CS2_CNDS need to be set.
1242 */
1243 if (!(i&1)) {
1244 sa = dimm_params[i/2].base_address;
1245 ea = sa + (i * (rank_density >>
1246 dbw_cap_adj)) - 1;
1247 }
1248 break;
1249 case FSL_DDR_CS0_CS1:
1250 /* CS0+CS1 interleaving, CS0_CNDS needs
1251 * to be set
1252 */
1253 sa = common_dimm->base_address;
1254 ea = sa + (2 * (rank_density >> dbw_cap_adj))-1;
1255 break;
1256 case FSL_DDR_CS2_CS3:
1257 /* CS2+CS3 interleaving*/
1258 if (i == 2) {
1259 sa = dimm_params[i/2].base_address;
1260 ea = sa + (2 * (rank_density >>
1261 dbw_cap_adj)) - 1;
1262 }
1263 break;
1264 default: /* No bank(chip-select) interleaving */
1265 break;
1266 }
Kumar Gala124b0822008-08-26 15:01:29 -05001267 }
1268 else if (popts->memctl_interleaving && !popts->ba_intlv_ctl) {
1269 /*
1270 * Only the rank on CS0 of each memory controller may
1271 * be used if memory controller interleaving is used
1272 * without rank interleaving within each memory
1273 * controller. However, the ending address programmed
1274 * into each CS0 must be the sum of the amount of
1275 * memory in the two CS0 ranks.
1276 */
1277 if (i == 0) {
1278 unsigned long long rank_density
1279 = dimm_params[0].rank_density;
1280 ea = (2 * (rank_density >> dbw_cap_adj)) - 1;
1281 }
1282
1283 }
1284 else if (!popts->memctl_interleaving && !popts->ba_intlv_ctl) {
1285 /*
1286 * No rank interleaving and no memory controller
1287 * interleaving.
1288 */
1289 unsigned long long rank_density
1290 = dimm_params[i/2].rank_density;
1291 sa = dimm_params[i/2].base_address;
1292 ea = sa + (rank_density >> dbw_cap_adj) - 1;
1293 if (i&1) {
1294 if ((dimm_params[i/2].n_ranks == 1)) {
1295 /* Odd chip select, single-rank dimm */
1296 sa = 0;
1297 ea = 0;
1298 } else {
1299 /* Odd chip select, dual-rank DIMM */
1300 sa += rank_density >> dbw_cap_adj;
1301 ea += rank_density >> dbw_cap_adj;
1302 }
1303 }
1304 }
1305
1306 sa >>= 24;
1307 ea >>= 24;
1308
1309 ddr->cs[i].bnds = (0
1310 | ((sa & 0xFFF) << 16) /* starting address MSB */
1311 | ((ea & 0xFFF) << 0) /* ending address MSB */
1312 );
1313
Haiying Wangd90e0402008-10-03 12:37:26 -04001314 debug("FSLDDR: cs[%d]_bnds = 0x%08x\n", i, ddr->cs[i].bnds);
Kumar Gala124b0822008-08-26 15:01:29 -05001315 set_csn_config(i, ddr, popts, dimm_params);
1316 set_csn_config_2(i, ddr);
1317 }
1318
Dave Liu4be87b22009-03-14 12:48:30 +08001319#if !defined(CONFIG_FSL_DDR1)
Kumar Gala124b0822008-08-26 15:01:29 -05001320 set_timing_cfg_0(ddr);
1321#endif
1322
Dave Liu4be87b22009-03-14 12:48:30 +08001323 set_timing_cfg_3(ddr, common_dimm, cas_latency);
1324 set_timing_cfg_1(ddr, popts, common_dimm, cas_latency);
Kumar Gala124b0822008-08-26 15:01:29 -05001325 set_timing_cfg_2(ddr, popts, common_dimm,
1326 cas_latency, additive_latency);
1327
1328 set_ddr_sdram_cfg(ddr, popts, common_dimm);
1329
1330 set_ddr_sdram_cfg_2(ddr, popts);
1331 set_ddr_sdram_mode(ddr, popts, common_dimm,
1332 cas_latency, additive_latency);
1333 set_ddr_sdram_mode_2(ddr);
1334 set_ddr_sdram_interval(ddr, popts, common_dimm);
1335 set_ddr_data_init(ddr);
1336 set_ddr_sdram_clk_cntl(ddr, popts);
1337 set_ddr_init_addr(ddr);
1338 set_ddr_init_ext_addr(ddr);
1339 set_timing_cfg_4(ddr);
1340 set_timing_cfg_5(ddr);
1341
Dave Liu4be87b22009-03-14 12:48:30 +08001342 set_ddr_zq_cntl(ddr, zq_en);
Dave Liu64ee7df2009-12-16 10:24:37 -06001343 set_ddr_wrlvl_cntl(ddr, wrlvl_en, popts);
Kumar Gala124b0822008-08-26 15:01:29 -05001344
Dave Liu2aad0ae2008-11-21 16:31:35 +08001345 set_ddr_sr_cntr(ddr, sr_it);
Kumar Gala124b0822008-08-26 15:01:29 -05001346
1347 set_ddr_sdram_rcw_1(ddr);
1348 set_ddr_sdram_rcw_2(ddr);
1349
1350 return check_fsl_memctl_config_regs(ddr);
1351}