blob: 3fec100377b4ef1ed107d3b4d9ebf72a2453bc79 [file] [log] [blame]
Kumar Gala124b0822008-08-26 15:01:29 -05001/*
Dave Liu3525e1a2010-03-05 12:22:00 +08002 * Copyright 2008-2010 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
21extern unsigned int picos_to_mclk(unsigned int picos);
22/*
23 * Determine Rtt value.
24 *
25 * This should likely be either board or controller specific.
26 *
Dave Liu4be87b22009-03-14 12:48:30 +080027 * Rtt(nominal) - DDR2:
Kumar Gala124b0822008-08-26 15:01:29 -050028 * 0 = Rtt disabled
29 * 1 = 75 ohm
30 * 2 = 150 ohm
31 * 3 = 50 ohm
Dave Liu4be87b22009-03-14 12:48:30 +080032 * Rtt(nominal) - DDR3:
33 * 0 = Rtt disabled
34 * 1 = 60 ohm
35 * 2 = 120 ohm
36 * 3 = 40 ohm
37 * 4 = 20 ohm
38 * 5 = 30 ohm
Kumar Gala124b0822008-08-26 15:01:29 -050039 *
40 * FIXME: Apparently 8641 needs a value of 2
41 * FIXME: Old code seys if 667 MHz or higher, use 3 on 8572
42 *
43 * FIXME: There was some effort down this line earlier:
44 *
45 * unsigned int i;
46 * for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL/2; i++) {
47 * if (popts->dimmslot[i].num_valid_cs
48 * && (popts->cs_local_opts[2*i].odt_rd_cfg
49 * || popts->cs_local_opts[2*i].odt_wr_cfg)) {
50 * rtt = 2;
51 * break;
52 * }
53 * }
54 */
55static inline int fsl_ddr_get_rtt(void)
56{
57 int rtt;
58
59#if defined(CONFIG_FSL_DDR1)
60 rtt = 0;
61#elif defined(CONFIG_FSL_DDR2)
62 rtt = 3;
63#else
Dave Liu4be87b22009-03-14 12:48:30 +080064 rtt = 0;
Kumar Gala124b0822008-08-26 15:01:29 -050065#endif
66
67 return rtt;
68}
69
Dave Liu4be87b22009-03-14 12:48:30 +080070/*
71 * compute the CAS write latency according to DDR3 spec
72 * CWL = 5 if tCK >= 2.5ns
73 * 6 if 2.5ns > tCK >= 1.875ns
74 * 7 if 1.875ns > tCK >= 1.5ns
75 * 8 if 1.5ns > tCK >= 1.25ns
76 */
77static inline unsigned int compute_cas_write_latency(void)
78{
79 unsigned int cwl;
80 const unsigned int mclk_ps = get_memory_clk_period_ps();
81
82 if (mclk_ps >= 2500)
83 cwl = 5;
84 else if (mclk_ps >= 1875)
85 cwl = 6;
86 else if (mclk_ps >= 1500)
87 cwl = 7;
88 else if (mclk_ps >= 1250)
89 cwl = 8;
90 else
91 cwl = 8;
92 return cwl;
93}
94
Kumar Gala124b0822008-08-26 15:01:29 -050095/* Chip Select Configuration (CSn_CONFIG) */
yorkf4f93c62010-07-02 22:25:53 +000096static void set_csn_config(int dimm_number, int i, fsl_ddr_cfg_regs_t *ddr,
Kumar Gala124b0822008-08-26 15:01:29 -050097 const memctl_options_t *popts,
98 const dimm_params_t *dimm_params)
99{
100 unsigned int cs_n_en = 0; /* Chip Select enable */
101 unsigned int intlv_en = 0; /* Memory controller interleave enable */
102 unsigned int intlv_ctl = 0; /* Interleaving control */
103 unsigned int ap_n_en = 0; /* Chip select n auto-precharge enable */
104 unsigned int odt_rd_cfg = 0; /* ODT for reads configuration */
105 unsigned int odt_wr_cfg = 0; /* ODT for writes configuration */
106 unsigned int ba_bits_cs_n = 0; /* Num of bank bits for SDRAM on CSn */
107 unsigned int row_bits_cs_n = 0; /* Num of row bits for SDRAM on CSn */
108 unsigned int col_bits_cs_n = 0; /* Num of ocl bits for SDRAM on CSn */
yorkf4f93c62010-07-02 22:25:53 +0000109 int go_config = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500110
111 /* Compute CS_CONFIG only for existing ranks of each DIMM. */
yorkf4f93c62010-07-02 22:25:53 +0000112 switch (i) {
113 case 0:
114 if (dimm_params[dimm_number].n_ranks > 0) {
115 go_config = 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500116 /* These fields only available in CS0_CONFIG */
117 intlv_en = popts->memctl_interleaving;
118 intlv_ctl = popts->memctl_interleaving_mode;
119 }
yorkf4f93c62010-07-02 22:25:53 +0000120 break;
121 case 1:
122 if ((dimm_number == 0 && dimm_params[0].n_ranks > 1) || \
123 (dimm_number == 1 && dimm_params[1].n_ranks > 0))
124 go_config = 1;
125 break;
126 case 2:
127 if ((dimm_number == 0 && dimm_params[0].n_ranks > 2) || \
128 (dimm_number > 1 && dimm_params[dimm_number].n_ranks > 0))
129 go_config = 1;
130 break;
131 case 3:
132 if ((dimm_number == 0 && dimm_params[0].n_ranks > 3) || \
133 (dimm_number == 1 && dimm_params[1].n_ranks > 1) || \
134 (dimm_number == 3 && dimm_params[3].n_ranks > 0))
135 go_config = 1;
136 break;
137 default:
138 break;
139 }
140 if (go_config) {
141 unsigned int n_banks_per_sdram_device;
142 cs_n_en = 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500143 ap_n_en = popts->cs_local_opts[i].auto_precharge;
144 odt_rd_cfg = popts->cs_local_opts[i].odt_rd_cfg;
145 odt_wr_cfg = popts->cs_local_opts[i].odt_wr_cfg;
146 n_banks_per_sdram_device
yorkf4f93c62010-07-02 22:25:53 +0000147 = dimm_params[dimm_number].n_banks_per_sdram_device;
Kumar Gala124b0822008-08-26 15:01:29 -0500148 ba_bits_cs_n = __ilog2(n_banks_per_sdram_device) - 2;
yorkf4f93c62010-07-02 22:25:53 +0000149 row_bits_cs_n = dimm_params[dimm_number].n_row_addr - 12;
150 col_bits_cs_n = dimm_params[dimm_number].n_col_addr - 8;
Kumar Gala124b0822008-08-26 15:01:29 -0500151 }
Kumar Gala124b0822008-08-26 15:01:29 -0500152 ddr->cs[i].config = (0
153 | ((cs_n_en & 0x1) << 31)
154 | ((intlv_en & 0x3) << 29)
Haiying Wang272b5962008-10-03 12:36:39 -0400155 | ((intlv_ctl & 0xf) << 24)
Kumar Gala124b0822008-08-26 15:01:29 -0500156 | ((ap_n_en & 0x1) << 23)
157
158 /* XXX: some implementation only have 1 bit starting at left */
159 | ((odt_rd_cfg & 0x7) << 20)
160
161 /* XXX: Some implementation only have 1 bit starting at left */
162 | ((odt_wr_cfg & 0x7) << 16)
163
164 | ((ba_bits_cs_n & 0x3) << 14)
165 | ((row_bits_cs_n & 0x7) << 8)
166 | ((col_bits_cs_n & 0x7) << 0)
167 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400168 debug("FSLDDR: cs[%d]_config = 0x%08x\n", i,ddr->cs[i].config);
Kumar Gala124b0822008-08-26 15:01:29 -0500169}
170
171/* Chip Select Configuration 2 (CSn_CONFIG_2) */
172/* FIXME: 8572 */
173static void set_csn_config_2(int i, fsl_ddr_cfg_regs_t *ddr)
174{
175 unsigned int pasr_cfg = 0; /* Partial array self refresh config */
176
177 ddr->cs[i].config_2 = ((pasr_cfg & 7) << 24);
Haiying Wangd90e0402008-10-03 12:37:26 -0400178 debug("FSLDDR: cs[%d]_config_2 = 0x%08x\n", i, ddr->cs[i].config_2);
Kumar Gala124b0822008-08-26 15:01:29 -0500179}
180
181/* -3E = 667 CL5, -25 = CL6 800, -25E = CL5 800 */
182
Dave Liu4be87b22009-03-14 12:48:30 +0800183#if !defined(CONFIG_FSL_DDR1)
Kumar Gala124b0822008-08-26 15:01:29 -0500184/*
185 * DDR SDRAM Timing Configuration 0 (TIMING_CFG_0)
186 *
187 * Avoid writing for DDR I. The new PQ38 DDR controller
188 * dreams up non-zero default values to be backwards compatible.
189 */
190static void set_timing_cfg_0(fsl_ddr_cfg_regs_t *ddr)
191{
192 unsigned char trwt_mclk = 0; /* Read-to-write turnaround */
193 unsigned char twrt_mclk = 0; /* Write-to-read turnaround */
194 /* 7.5 ns on -3E; 0 means WL - CL + BL/2 + 1 */
195 unsigned char trrt_mclk = 0; /* Read-to-read turnaround */
196 unsigned char twwt_mclk = 0; /* Write-to-write turnaround */
197
198 /* Active powerdown exit timing (tXARD and tXARDS). */
199 unsigned char act_pd_exit_mclk;
200 /* Precharge powerdown exit timing (tXP). */
201 unsigned char pre_pd_exit_mclk;
york1714e492010-07-02 22:25:56 +0000202 /* ODT powerdown exit timing (tAXPD). */
Kumar Gala124b0822008-08-26 15:01:29 -0500203 unsigned char taxpd_mclk;
204 /* Mode register set cycle time (tMRD). */
205 unsigned char tmrd_mclk;
206
Dave Liu4be87b22009-03-14 12:48:30 +0800207#if defined(CONFIG_FSL_DDR3)
208 /*
209 * (tXARD and tXARDS). Empirical?
210 * The DDR3 spec has not tXARD,
211 * we use the tXP instead of it.
212 * tXP=max(3nCK, 7.5ns) for DDR3.
Dave Liu4be87b22009-03-14 12:48:30 +0800213 * spec has not the tAXPD, we use
york1714e492010-07-02 22:25:56 +0000214 * tAXPD=1, need design to confirm.
Dave Liu4be87b22009-03-14 12:48:30 +0800215 */
Dave Liuc7d983a2009-12-16 10:24:36 -0600216 int tXP = max((get_memory_clk_period_ps() * 3), 7500); /* unit=ps */
217 act_pd_exit_mclk = picos_to_mclk(tXP);
218 /* Mode register MR0[A12] is '1' - fast exit */
219 pre_pd_exit_mclk = act_pd_exit_mclk;
york1714e492010-07-02 22:25:56 +0000220 taxpd_mclk = 1;
Dave Liu4be87b22009-03-14 12:48:30 +0800221 tmrd_mclk = 4;
Dave Liu81079262009-12-08 11:56:48 +0800222 /* set the turnaround time */
223 trwt_mclk = 1;
Dave Liu4be87b22009-03-14 12:48:30 +0800224#else /* CONFIG_FSL_DDR2 */
225 /*
226 * (tXARD and tXARDS). Empirical?
227 * tXARD = 2 for DDR2
228 * tXP=2
229 * tAXPD=8
230 */
231 act_pd_exit_mclk = 2;
232 pre_pd_exit_mclk = 2;
233 taxpd_mclk = 8;
Kumar Gala124b0822008-08-26 15:01:29 -0500234 tmrd_mclk = 2;
Dave Liu4be87b22009-03-14 12:48:30 +0800235#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500236
237 ddr->timing_cfg_0 = (0
238 | ((trwt_mclk & 0x3) << 30) /* RWT */
239 | ((twrt_mclk & 0x3) << 28) /* WRT */
240 | ((trrt_mclk & 0x3) << 26) /* RRT */
241 | ((twwt_mclk & 0x3) << 24) /* WWT */
242 | ((act_pd_exit_mclk & 0x7) << 20) /* ACT_PD_EXIT */
Dave Liu4758d532008-11-21 16:31:29 +0800243 | ((pre_pd_exit_mclk & 0xF) << 16) /* PRE_PD_EXIT */
Kumar Gala124b0822008-08-26 15:01:29 -0500244 | ((taxpd_mclk & 0xf) << 8) /* ODT_PD_EXIT */
245 | ((tmrd_mclk & 0xf) << 0) /* MRS_CYC */
246 );
247 debug("FSLDDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
248}
249#endif /* defined(CONFIG_FSL_DDR2) */
250
251/* DDR SDRAM Timing Configuration 3 (TIMING_CFG_3) */
252static void set_timing_cfg_3(fsl_ddr_cfg_regs_t *ddr,
Dave Liu4be87b22009-03-14 12:48:30 +0800253 const common_timing_params_t *common_dimm,
254 unsigned int cas_latency)
Kumar Gala124b0822008-08-26 15:01:29 -0500255{
256 /* Extended Activate to precharge interval (tRAS) */
257 unsigned int ext_acttopre = 0;
258 unsigned int ext_refrec; /* Extended refresh recovery time (tRFC) */
259 unsigned int ext_caslat = 0; /* Extended MCAS latency from READ cmd */
260 unsigned int cntl_adj = 0; /* Control Adjust */
261
Dave Liu5c1bb512008-11-21 16:31:22 +0800262 /* If the tRAS > 19 MCLK, we use the ext mode */
263 if (picos_to_mclk(common_dimm->tRAS_ps) > 0x13)
264 ext_acttopre = 1;
265
Kumar Gala124b0822008-08-26 15:01:29 -0500266 ext_refrec = (picos_to_mclk(common_dimm->tRFC_ps) - 8) >> 4;
Dave Liu4be87b22009-03-14 12:48:30 +0800267
268 /* If the CAS latency more than 8, use the ext mode */
269 if (cas_latency > 8)
270 ext_caslat = 1;
271
Kumar Gala124b0822008-08-26 15:01:29 -0500272 ddr->timing_cfg_3 = (0
273 | ((ext_acttopre & 0x1) << 24)
Dave Liu5c1bb512008-11-21 16:31:22 +0800274 | ((ext_refrec & 0xF) << 16)
Kumar Gala124b0822008-08-26 15:01:29 -0500275 | ((ext_caslat & 0x1) << 12)
276 | ((cntl_adj & 0x7) << 0)
277 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400278 debug("FSLDDR: timing_cfg_3 = 0x%08x\n", ddr->timing_cfg_3);
Kumar Gala124b0822008-08-26 15:01:29 -0500279}
280
281/* DDR SDRAM Timing Configuration 1 (TIMING_CFG_1) */
282static void set_timing_cfg_1(fsl_ddr_cfg_regs_t *ddr,
Dave Liu4be87b22009-03-14 12:48:30 +0800283 const memctl_options_t *popts,
Kumar Gala124b0822008-08-26 15:01:29 -0500284 const common_timing_params_t *common_dimm,
285 unsigned int cas_latency)
286{
287 /* Precharge-to-activate interval (tRP) */
288 unsigned char pretoact_mclk;
289 /* Activate to precharge interval (tRAS) */
290 unsigned char acttopre_mclk;
291 /* Activate to read/write interval (tRCD) */
292 unsigned char acttorw_mclk;
293 /* CASLAT */
294 unsigned char caslat_ctrl;
295 /* Refresh recovery time (tRFC) ; trfc_low */
296 unsigned char refrec_ctrl;
297 /* Last data to precharge minimum interval (tWR) */
298 unsigned char wrrec_mclk;
299 /* Activate-to-activate interval (tRRD) */
300 unsigned char acttoact_mclk;
301 /* Last write data pair to read command issue interval (tWTR) */
302 unsigned char wrtord_mclk;
303
304 pretoact_mclk = picos_to_mclk(common_dimm->tRP_ps);
305 acttopre_mclk = picos_to_mclk(common_dimm->tRAS_ps);
306 acttorw_mclk = picos_to_mclk(common_dimm->tRCD_ps);
307
308 /*
309 * Translate CAS Latency to a DDR controller field value:
310 *
311 * CAS Lat DDR I DDR II Ctrl
312 * Clocks SPD Bit SPD Bit Value
313 * ------- ------- ------- -----
314 * 1.0 0 0001
315 * 1.5 1 0010
316 * 2.0 2 2 0011
317 * 2.5 3 0100
318 * 3.0 4 3 0101
319 * 3.5 5 0110
320 * 4.0 4 0111
321 * 4.5 1000
322 * 5.0 5 1001
323 */
324#if defined(CONFIG_FSL_DDR1)
325 caslat_ctrl = (cas_latency + 1) & 0x07;
326#elif defined(CONFIG_FSL_DDR2)
327 caslat_ctrl = 2 * cas_latency - 1;
328#else
Dave Liu4be87b22009-03-14 12:48:30 +0800329 /*
330 * if the CAS latency more than 8 cycle,
331 * we need set extend bit for it at
332 * TIMING_CFG_3[EXT_CASLAT]
333 */
334 if (cas_latency > 8)
335 cas_latency -= 8;
336 caslat_ctrl = 2 * cas_latency - 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500337#endif
338
339 refrec_ctrl = picos_to_mclk(common_dimm->tRFC_ps) - 8;
340 wrrec_mclk = picos_to_mclk(common_dimm->tWR_ps);
Dave Liu4be87b22009-03-14 12:48:30 +0800341 if (popts->OTF_burst_chop_en)
342 wrrec_mclk += 2;
343
Kumar Gala124b0822008-08-26 15:01:29 -0500344 acttoact_mclk = picos_to_mclk(common_dimm->tRRD_ps);
Dave Liu4be87b22009-03-14 12:48:30 +0800345 /*
346 * JEDEC has min requirement for tRRD
347 */
348#if defined(CONFIG_FSL_DDR3)
349 if (acttoact_mclk < 4)
350 acttoact_mclk = 4;
351#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500352 wrtord_mclk = picos_to_mclk(common_dimm->tWTR_ps);
Dave Liu4be87b22009-03-14 12:48:30 +0800353 /*
354 * JEDEC has some min requirements for tWTR
355 */
356#if defined(CONFIG_FSL_DDR2)
357 if (wrtord_mclk < 2)
358 wrtord_mclk = 2;
359#elif defined(CONFIG_FSL_DDR3)
360 if (wrtord_mclk < 4)
361 wrtord_mclk = 4;
362#endif
363 if (popts->OTF_burst_chop_en)
364 wrtord_mclk += 2;
Kumar Gala124b0822008-08-26 15:01:29 -0500365
366 ddr->timing_cfg_1 = (0
Dave Liu5c1bb512008-11-21 16:31:22 +0800367 | ((pretoact_mclk & 0x0F) << 28)
Kumar Gala124b0822008-08-26 15:01:29 -0500368 | ((acttopre_mclk & 0x0F) << 24)
Dave Liu5c1bb512008-11-21 16:31:22 +0800369 | ((acttorw_mclk & 0xF) << 20)
Kumar Gala124b0822008-08-26 15:01:29 -0500370 | ((caslat_ctrl & 0xF) << 16)
371 | ((refrec_ctrl & 0xF) << 12)
Dave Liu5c1bb512008-11-21 16:31:22 +0800372 | ((wrrec_mclk & 0x0F) << 8)
Kumar Gala124b0822008-08-26 15:01:29 -0500373 | ((acttoact_mclk & 0x07) << 4)
374 | ((wrtord_mclk & 0x07) << 0)
375 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400376 debug("FSLDDR: timing_cfg_1 = 0x%08x\n", ddr->timing_cfg_1);
Kumar Gala124b0822008-08-26 15:01:29 -0500377}
378
379/* DDR SDRAM Timing Configuration 2 (TIMING_CFG_2) */
380static void set_timing_cfg_2(fsl_ddr_cfg_regs_t *ddr,
381 const memctl_options_t *popts,
382 const common_timing_params_t *common_dimm,
383 unsigned int cas_latency,
384 unsigned int additive_latency)
385{
386 /* Additive latency */
387 unsigned char add_lat_mclk;
388 /* CAS-to-preamble override */
389 unsigned short cpo;
390 /* Write latency */
391 unsigned char wr_lat;
392 /* Read to precharge (tRTP) */
393 unsigned char rd_to_pre;
394 /* Write command to write data strobe timing adjustment */
395 unsigned char wr_data_delay;
396 /* Minimum CKE pulse width (tCKE) */
397 unsigned char cke_pls;
398 /* Window for four activates (tFAW) */
399 unsigned short four_act;
400
401 /* FIXME add check that this must be less than acttorw_mclk */
402 add_lat_mclk = additive_latency;
403 cpo = popts->cpo_override;
404
405#if defined(CONFIG_FSL_DDR1)
406 /*
407 * This is a lie. It should really be 1, but if it is
408 * set to 1, bits overlap into the old controller's
409 * otherwise unused ACSM field. If we leave it 0, then
410 * the HW will magically treat it as 1 for DDR 1. Oh Yea.
411 */
412 wr_lat = 0;
413#elif defined(CONFIG_FSL_DDR2)
Dave Liu82aa9532009-03-14 12:48:19 +0800414 wr_lat = cas_latency - 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500415#else
Dave Liu4be87b22009-03-14 12:48:30 +0800416 wr_lat = compute_cas_write_latency();
Kumar Gala124b0822008-08-26 15:01:29 -0500417#endif
418
419 rd_to_pre = picos_to_mclk(common_dimm->tRTP_ps);
Dave Liu4be87b22009-03-14 12:48:30 +0800420 /*
421 * JEDEC has some min requirements for tRTP
422 */
Dave Liu82aa9532009-03-14 12:48:19 +0800423#if defined(CONFIG_FSL_DDR2)
Dave Liu4be87b22009-03-14 12:48:30 +0800424 if (rd_to_pre < 2)
425 rd_to_pre = 2;
426#elif defined(CONFIG_FSL_DDR3)
427 if (rd_to_pre < 4)
428 rd_to_pre = 4;
Dave Liu82aa9532009-03-14 12:48:19 +0800429#endif
Dave Liu4be87b22009-03-14 12:48:30 +0800430 if (additive_latency)
431 rd_to_pre += additive_latency;
432 if (popts->OTF_burst_chop_en)
433 rd_to_pre += 2; /* according to UM */
434
Kumar Gala124b0822008-08-26 15:01:29 -0500435 wr_data_delay = popts->write_data_delay;
436 cke_pls = picos_to_mclk(popts->tCKE_clock_pulse_width_ps);
437 four_act = picos_to_mclk(popts->tFAW_window_four_activates_ps);
438
439 ddr->timing_cfg_2 = (0
Dave Liu4758d532008-11-21 16:31:29 +0800440 | ((add_lat_mclk & 0xf) << 28)
Kumar Gala124b0822008-08-26 15:01:29 -0500441 | ((cpo & 0x1f) << 23)
Dave Liu4758d532008-11-21 16:31:29 +0800442 | ((wr_lat & 0xf) << 19)
Dave Liu4be87b22009-03-14 12:48:30 +0800443 | ((rd_to_pre & RD_TO_PRE_MASK) << RD_TO_PRE_SHIFT)
444 | ((wr_data_delay & WR_DATA_DELAY_MASK) << WR_DATA_DELAY_SHIFT)
Kumar Gala124b0822008-08-26 15:01:29 -0500445 | ((cke_pls & 0x7) << 6)
Dave Liu4758d532008-11-21 16:31:29 +0800446 | ((four_act & 0x3f) << 0)
Kumar Gala124b0822008-08-26 15:01:29 -0500447 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400448 debug("FSLDDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2);
Kumar Gala124b0822008-08-26 15:01:29 -0500449}
450
yorkde879322010-07-02 22:25:55 +0000451/* DDR SDRAM Register Control Word */
452static void set_ddr_sdram_rcw(fsl_ddr_cfg_regs_t *ddr,
453 const common_timing_params_t *common_dimm)
454{
455 if (common_dimm->all_DIMMs_registered
456 && !common_dimm->all_DIMMs_unbuffered) {
457 ddr->ddr_sdram_rcw_1 =
458 common_dimm->rcw[0] << 28 | \
459 common_dimm->rcw[1] << 24 | \
460 common_dimm->rcw[2] << 20 | \
461 common_dimm->rcw[3] << 16 | \
462 common_dimm->rcw[4] << 12 | \
463 common_dimm->rcw[5] << 8 | \
464 common_dimm->rcw[6] << 4 | \
465 common_dimm->rcw[7];
466 ddr->ddr_sdram_rcw_2 =
467 common_dimm->rcw[8] << 28 | \
468 common_dimm->rcw[9] << 24 | \
469 common_dimm->rcw[10] << 20 | \
470 common_dimm->rcw[11] << 16 | \
471 common_dimm->rcw[12] << 12 | \
472 common_dimm->rcw[13] << 8 | \
473 common_dimm->rcw[14] << 4 | \
474 common_dimm->rcw[15];
475 debug("FSLDDR: ddr_sdram_rcw_1 = 0x%08x\n", ddr->ddr_sdram_rcw_1);
476 debug("FSLDDR: ddr_sdram_rcw_2 = 0x%08x\n", ddr->ddr_sdram_rcw_2);
477 }
478}
479
Kumar Gala124b0822008-08-26 15:01:29 -0500480/* DDR SDRAM control configuration (DDR_SDRAM_CFG) */
481static void set_ddr_sdram_cfg(fsl_ddr_cfg_regs_t *ddr,
482 const memctl_options_t *popts,
483 const common_timing_params_t *common_dimm)
484{
485 unsigned int mem_en; /* DDR SDRAM interface logic enable */
486 unsigned int sren; /* Self refresh enable (during sleep) */
487 unsigned int ecc_en; /* ECC enable. */
488 unsigned int rd_en; /* Registered DIMM enable */
489 unsigned int sdram_type; /* Type of SDRAM */
490 unsigned int dyn_pwr; /* Dynamic power management mode */
491 unsigned int dbw; /* DRAM dta bus width */
Dave Liu4758d532008-11-21 16:31:29 +0800492 unsigned int eight_be = 0; /* 8-beat burst enable, DDR2 is zero */
Kumar Gala124b0822008-08-26 15:01:29 -0500493 unsigned int ncap = 0; /* Non-concurrent auto-precharge */
494 unsigned int threeT_en; /* Enable 3T timing */
495 unsigned int twoT_en; /* Enable 2T timing */
496 unsigned int ba_intlv_ctl; /* Bank (CS) interleaving control */
497 unsigned int x32_en = 0; /* x32 enable */
498 unsigned int pchb8 = 0; /* precharge bit 8 enable */
499 unsigned int hse; /* Global half strength override */
500 unsigned int mem_halt = 0; /* memory controller halt */
501 unsigned int bi = 0; /* Bypass initialization */
502
503 mem_en = 1;
504 sren = popts->self_refresh_in_sleep;
505 if (common_dimm->all_DIMMs_ECC_capable) {
506 /* Allow setting of ECC only if all DIMMs are ECC. */
507 ecc_en = popts->ECC_mode;
508 } else {
509 ecc_en = 0;
510 }
511
512 rd_en = (common_dimm->all_DIMMs_registered
513 && !common_dimm->all_DIMMs_unbuffered);
514
515 sdram_type = CONFIG_FSL_SDRAM_TYPE;
516
517 dyn_pwr = popts->dynamic_power;
518 dbw = popts->data_bus_width;
Dave Liu4be87b22009-03-14 12:48:30 +0800519 /* 8-beat burst enable DDR-III case
520 * we must clear it when use the on-the-fly mode,
521 * must set it when use the 32-bits bus mode.
522 */
523 if (sdram_type == SDRAM_TYPE_DDR3) {
524 if (popts->burst_length == DDR_BL8)
525 eight_be = 1;
526 if (popts->burst_length == DDR_OTF)
527 eight_be = 0;
528 if (dbw == 0x1)
529 eight_be = 1;
530 }
531
Kumar Gala124b0822008-08-26 15:01:29 -0500532 threeT_en = popts->threeT_en;
533 twoT_en = popts->twoT_en;
534 ba_intlv_ctl = popts->ba_intlv_ctl;
535 hse = popts->half_strength_driver_enable;
536
537 ddr->ddr_sdram_cfg = (0
538 | ((mem_en & 0x1) << 31)
539 | ((sren & 0x1) << 30)
540 | ((ecc_en & 0x1) << 29)
541 | ((rd_en & 0x1) << 28)
542 | ((sdram_type & 0x7) << 24)
543 | ((dyn_pwr & 0x1) << 21)
544 | ((dbw & 0x3) << 19)
545 | ((eight_be & 0x1) << 18)
546 | ((ncap & 0x1) << 17)
547 | ((threeT_en & 0x1) << 16)
548 | ((twoT_en & 0x1) << 15)
549 | ((ba_intlv_ctl & 0x7F) << 8)
550 | ((x32_en & 0x1) << 5)
551 | ((pchb8 & 0x1) << 4)
552 | ((hse & 0x1) << 3)
553 | ((mem_halt & 0x1) << 1)
554 | ((bi & 0x1) << 0)
555 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400556 debug("FSLDDR: ddr_sdram_cfg = 0x%08x\n", ddr->ddr_sdram_cfg);
Kumar Gala124b0822008-08-26 15:01:29 -0500557}
558
559/* DDR SDRAM control configuration 2 (DDR_SDRAM_CFG_2) */
560static void set_ddr_sdram_cfg_2(fsl_ddr_cfg_regs_t *ddr,
561 const memctl_options_t *popts)
562{
563 unsigned int frc_sr = 0; /* Force self refresh */
564 unsigned int sr_ie = 0; /* Self-refresh interrupt enable */
565 unsigned int dll_rst_dis; /* DLL reset disable */
566 unsigned int dqs_cfg; /* DQS configuration */
567 unsigned int odt_cfg; /* ODT configuration */
568 unsigned int num_pr; /* Number of posted refreshes */
569 unsigned int obc_cfg; /* On-The-Fly Burst Chop Cfg */
570 unsigned int ap_en; /* Address Parity Enable */
571 unsigned int d_init; /* DRAM data initialization */
572 unsigned int rcw_en = 0; /* Register Control Word Enable */
573 unsigned int md_en = 0; /* Mirrored DIMM Enable */
yorkf4f93c62010-07-02 22:25:53 +0000574 unsigned int qd_en = 0; /* quad-rank DIMM Enable */
Kumar Gala124b0822008-08-26 15:01:29 -0500575
576 dll_rst_dis = 1; /* Make this configurable */
577 dqs_cfg = popts->DQS_config;
578 if (popts->cs_local_opts[0].odt_rd_cfg
579 || popts->cs_local_opts[0].odt_wr_cfg) {
580 /* FIXME */
581 odt_cfg = 2;
582 } else {
583 odt_cfg = 0;
584 }
585
586 num_pr = 1; /* Make this configurable */
587
588 /*
589 * 8572 manual says
590 * {TIMING_CFG_1[PRETOACT]
591 * + [DDR_SDRAM_CFG_2[NUM_PR]
592 * * ({EXT_REFREC || REFREC} + 8 + 2)]}
593 * << DDR_SDRAM_INTERVAL[REFINT]
594 */
Dave Liu4be87b22009-03-14 12:48:30 +0800595#if defined(CONFIG_FSL_DDR3)
596 obc_cfg = popts->OTF_burst_chop_en;
597#else
598 obc_cfg = 0;
599#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500600
Kumar Gala124b0822008-08-26 15:01:29 -0500601 ap_en = 0; /* Make this configurable? */
602
603#if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
604 /* Use the DDR controller to auto initialize memory. */
605 d_init = 1;
606 ddr->ddr_data_init = CONFIG_MEM_INIT_VALUE;
607 debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init);
608#else
609 /* Memory will be initialized via DMA, or not at all. */
610 d_init = 0;
611#endif
612
Dave Liu4be87b22009-03-14 12:48:30 +0800613#if defined(CONFIG_FSL_DDR3)
614 md_en = popts->mirrored_dimm;
615#endif
York Sun5207e772010-08-27 16:25:56 -0500616 rcw_en = popts->registered_dimm_en;
yorkf4f93c62010-07-02 22:25:53 +0000617 qd_en = popts->quad_rank_present ? 1 : 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500618 ddr->ddr_sdram_cfg_2 = (0
619 | ((frc_sr & 0x1) << 31)
620 | ((sr_ie & 0x1) << 30)
621 | ((dll_rst_dis & 0x1) << 29)
622 | ((dqs_cfg & 0x3) << 26)
623 | ((odt_cfg & 0x3) << 21)
624 | ((num_pr & 0xf) << 12)
yorkf4f93c62010-07-02 22:25:53 +0000625 | (qd_en << 9)
Kumar Gala124b0822008-08-26 15:01:29 -0500626 | ((obc_cfg & 0x1) << 6)
627 | ((ap_en & 0x1) << 5)
628 | ((d_init & 0x1) << 4)
629 | ((rcw_en & 0x1) << 2)
630 | ((md_en & 0x1) << 0)
631 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400632 debug("FSLDDR: ddr_sdram_cfg_2 = 0x%08x\n", ddr->ddr_sdram_cfg_2);
Kumar Gala124b0822008-08-26 15:01:29 -0500633}
634
635/* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
Dave Liu2d0f1252009-12-16 10:24:38 -0600636static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr,
637 const memctl_options_t *popts)
Kumar Gala124b0822008-08-26 15:01:29 -0500638{
639 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
640 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
641
Dave Liu4be87b22009-03-14 12:48:30 +0800642#if defined(CONFIG_FSL_DDR3)
Dave Liu2d0f1252009-12-16 10:24:38 -0600643 unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */
Dave Liu4be87b22009-03-14 12:48:30 +0800644 unsigned int srt = 0; /* self-refresh temerature, normal range */
645 unsigned int asr = 0; /* auto self-refresh disable */
646 unsigned int cwl = compute_cas_write_latency() - 5;
647 unsigned int pasr = 0; /* partial array self refresh disable */
648
Dave Liu2d0f1252009-12-16 10:24:38 -0600649 if (popts->rtt_override)
650 rtt_wr = popts->rtt_wr_override_value;
651
Dave Liu4be87b22009-03-14 12:48:30 +0800652 esdmode2 = (0
653 | ((rtt_wr & 0x3) << 9)
654 | ((srt & 0x1) << 7)
655 | ((asr & 0x1) << 6)
656 | ((cwl & 0x7) << 3)
657 | ((pasr & 0x7) << 0));
658#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500659 ddr->ddr_sdram_mode_2 = (0
660 | ((esdmode2 & 0xFFFF) << 16)
661 | ((esdmode3 & 0xFFFF) << 0)
662 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400663 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
Kumar Gala124b0822008-08-26 15:01:29 -0500664}
665
666/* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */
667static void set_ddr_sdram_interval(fsl_ddr_cfg_regs_t *ddr,
668 const memctl_options_t *popts,
669 const common_timing_params_t *common_dimm)
670{
671 unsigned int refint; /* Refresh interval */
672 unsigned int bstopre; /* Precharge interval */
673
674 refint = picos_to_mclk(common_dimm->refresh_rate_ps);
675
676 bstopre = popts->bstopre;
677
678 /* refint field used 0x3FFF in earlier controllers */
679 ddr->ddr_sdram_interval = (0
680 | ((refint & 0xFFFF) << 16)
681 | ((bstopre & 0x3FFF) << 0)
682 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400683 debug("FSLDDR: ddr_sdram_interval = 0x%08x\n", ddr->ddr_sdram_interval);
Kumar Gala124b0822008-08-26 15:01:29 -0500684}
685
Dave Liu4be87b22009-03-14 12:48:30 +0800686#if defined(CONFIG_FSL_DDR3)
Kumar Gala124b0822008-08-26 15:01:29 -0500687/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
688static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
689 const memctl_options_t *popts,
690 const common_timing_params_t *common_dimm,
691 unsigned int cas_latency,
692 unsigned int additive_latency)
693{
694 unsigned short esdmode; /* Extended SDRAM mode */
695 unsigned short sdmode; /* SDRAM mode */
696
Dave Liu4be87b22009-03-14 12:48:30 +0800697 /* Mode Register - MR1 */
698 unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */
699 unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */
700 unsigned int rtt;
701 unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */
702 unsigned int al = 0; /* Posted CAS# additive latency (AL) */
703 unsigned int dic = 1; /* Output driver impedance, 34ohm */
704 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
705 1=Disable (Test/Debug) */
706
707 /* Mode Register - MR0 */
708 unsigned int dll_on; /* DLL control for precharge PD, 0=off, 1=on */
709 unsigned int wr; /* Write Recovery */
710 unsigned int dll_rst; /* DLL Reset */
711 unsigned int mode; /* Normal=0 or Test=1 */
712 unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
713 /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
714 unsigned int bt;
715 unsigned int bl; /* BL: Burst Length */
716
717 unsigned int wr_mclk;
718
719 const unsigned int mclk_ps = get_memory_clk_period_ps();
720
721 rtt = fsl_ddr_get_rtt();
722 if (popts->rtt_override)
723 rtt = popts->rtt_override_value;
724
725 if (additive_latency == (cas_latency - 1))
726 al = 1;
727 if (additive_latency == (cas_latency - 2))
728 al = 2;
729
730 /*
731 * The esdmode value will also be used for writing
732 * MR1 during write leveling for DDR3, although the
733 * bits specifically related to the write leveling
734 * scheme will be handled automatically by the DDR
735 * controller. so we set the wrlvl_en = 0 here.
736 */
737 esdmode = (0
738 | ((qoff & 0x1) << 12)
739 | ((tdqs_en & 0x1) << 11)
Kumar Gala14f2eb12009-09-10 14:54:55 -0500740 | ((rtt & 0x4) << 7) /* rtt field is split */
Dave Liu4be87b22009-03-14 12:48:30 +0800741 | ((wrlvl_en & 0x1) << 7)
Kumar Gala14f2eb12009-09-10 14:54:55 -0500742 | ((rtt & 0x2) << 5) /* rtt field is split */
743 | ((dic & 0x2) << 4) /* DIC field is split */
Dave Liu4be87b22009-03-14 12:48:30 +0800744 | ((al & 0x3) << 3)
Kumar Gala14f2eb12009-09-10 14:54:55 -0500745 | ((rtt & 0x1) << 2) /* rtt field is split */
Dave Liu4be87b22009-03-14 12:48:30 +0800746 | ((dic & 0x1) << 1) /* DIC field is split */
747 | ((dll_en & 0x1) << 0)
748 );
749
750 /*
751 * DLL control for precharge PD
752 * 0=slow exit DLL off (tXPDLL)
753 * 1=fast exit DLL on (tXP)
754 */
755 dll_on = 1;
756 wr_mclk = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps;
757 if (wr_mclk >= 12)
758 wr = 6;
759 else if (wr_mclk >= 9)
760 wr = 5;
761 else
762 wr = wr_mclk - 4;
763 dll_rst = 0; /* dll no reset */
764 mode = 0; /* normal mode */
765
766 /* look up table to get the cas latency bits */
767 if (cas_latency >= 5 && cas_latency <= 11) {
768 unsigned char cas_latency_table[7] = {
769 0x2, /* 5 clocks */
770 0x4, /* 6 clocks */
771 0x6, /* 7 clocks */
772 0x8, /* 8 clocks */
773 0xa, /* 9 clocks */
774 0xc, /* 10 clocks */
775 0xe /* 11 clocks */
776 };
777 caslat = cas_latency_table[cas_latency - 5];
778 }
779 bt = 0; /* Nibble sequential */
780
781 switch (popts->burst_length) {
782 case DDR_BL8:
783 bl = 0;
784 break;
785 case DDR_OTF:
786 bl = 1;
787 break;
788 case DDR_BC4:
789 bl = 2;
790 break;
791 default:
792 printf("Error: invalid burst length of %u specified. "
793 " Defaulting to on-the-fly BC4 or BL8 beats.\n",
794 popts->burst_length);
795 bl = 1;
796 break;
797 }
798
799 sdmode = (0
800 | ((dll_on & 0x1) << 12)
801 | ((wr & 0x7) << 9)
802 | ((dll_rst & 0x1) << 8)
803 | ((mode & 0x1) << 7)
804 | (((caslat >> 1) & 0x7) << 4)
805 | ((bt & 0x1) << 3)
806 | ((bl & 0x3) << 0)
807 );
808
809 ddr->ddr_sdram_mode = (0
810 | ((esdmode & 0xFFFF) << 16)
811 | ((sdmode & 0xFFFF) << 0)
812 );
813
814 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
815}
816
817#else /* !CONFIG_FSL_DDR3 */
818
819/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
820static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
821 const memctl_options_t *popts,
822 const common_timing_params_t *common_dimm,
823 unsigned int cas_latency,
824 unsigned int additive_latency)
825{
826 unsigned short esdmode; /* Extended SDRAM mode */
827 unsigned short sdmode; /* SDRAM mode */
828
Kumar Gala124b0822008-08-26 15:01:29 -0500829 /*
830 * FIXME: This ought to be pre-calculated in a
831 * technology-specific routine,
832 * e.g. compute_DDR2_mode_register(), and then the
833 * sdmode and esdmode passed in as part of common_dimm.
834 */
835
836 /* Extended Mode Register */
837 unsigned int mrs = 0; /* Mode Register Set */
838 unsigned int outputs = 0; /* 0=Enabled, 1=Disabled */
839 unsigned int rdqs_en = 0; /* RDQS Enable: 0=no, 1=yes */
840 unsigned int dqs_en = 0; /* DQS# Enable: 0=enable, 1=disable */
841 unsigned int ocd = 0; /* 0x0=OCD not supported,
842 0x7=OCD default state */
843 unsigned int rtt;
844 unsigned int al; /* Posted CAS# additive latency (AL) */
845 unsigned int ods = 0; /* Output Drive Strength:
846 0 = Full strength (18ohm)
847 1 = Reduced strength (4ohm) */
848 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
849 1=Disable (Test/Debug) */
850
851 /* Mode Register (MR) */
852 unsigned int mr; /* Mode Register Definition */
853 unsigned int pd; /* Power-Down Mode */
854 unsigned int wr; /* Write Recovery */
855 unsigned int dll_res; /* DLL Reset */
856 unsigned int mode; /* Normal=0 or Test=1 */
Kumar Gala35ad58d2008-09-05 14:40:29 -0500857 unsigned int caslat = 0;/* CAS# latency */
Kumar Gala124b0822008-08-26 15:01:29 -0500858 /* BT: Burst Type (0=Sequential, 1=Interleaved) */
859 unsigned int bt;
860 unsigned int bl; /* BL: Burst Length */
861
862#if defined(CONFIG_FSL_DDR2)
863 const unsigned int mclk_ps = get_memory_clk_period_ps();
864#endif
865
866 rtt = fsl_ddr_get_rtt();
867
868 al = additive_latency;
869
870 esdmode = (0
871 | ((mrs & 0x3) << 14)
872 | ((outputs & 0x1) << 12)
873 | ((rdqs_en & 0x1) << 11)
874 | ((dqs_en & 0x1) << 10)
875 | ((ocd & 0x7) << 7)
876 | ((rtt & 0x2) << 5) /* rtt field is split */
877 | ((al & 0x7) << 3)
878 | ((rtt & 0x1) << 2) /* rtt field is split */
879 | ((ods & 0x1) << 1)
880 | ((dll_en & 0x1) << 0)
881 );
882
883 mr = 0; /* FIXME: CHECKME */
884
885 /*
886 * 0 = Fast Exit (Normal)
887 * 1 = Slow Exit (Low Power)
888 */
889 pd = 0;
890
891#if defined(CONFIG_FSL_DDR1)
892 wr = 0; /* Historical */
893#elif defined(CONFIG_FSL_DDR2)
894 wr = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps - 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500895#endif
896 dll_res = 0;
897 mode = 0;
898
899#if defined(CONFIG_FSL_DDR1)
900 if (1 <= cas_latency && cas_latency <= 4) {
901 unsigned char mode_caslat_table[4] = {
902 0x5, /* 1.5 clocks */
903 0x2, /* 2.0 clocks */
904 0x6, /* 2.5 clocks */
905 0x3 /* 3.0 clocks */
906 };
Kumar Gala35ad58d2008-09-05 14:40:29 -0500907 caslat = mode_caslat_table[cas_latency - 1];
908 } else {
909 printf("Warning: unknown cas_latency %d\n", cas_latency);
Kumar Gala124b0822008-08-26 15:01:29 -0500910 }
911#elif defined(CONFIG_FSL_DDR2)
912 caslat = cas_latency;
Kumar Gala124b0822008-08-26 15:01:29 -0500913#endif
914 bt = 0;
915
916 switch (popts->burst_length) {
Dave Liu4be87b22009-03-14 12:48:30 +0800917 case DDR_BL4:
Kumar Gala124b0822008-08-26 15:01:29 -0500918 bl = 2;
919 break;
Dave Liu4be87b22009-03-14 12:48:30 +0800920 case DDR_BL8:
Kumar Gala124b0822008-08-26 15:01:29 -0500921 bl = 3;
922 break;
923 default:
924 printf("Error: invalid burst length of %u specified. "
925 " Defaulting to 4 beats.\n",
926 popts->burst_length);
927 bl = 2;
928 break;
929 }
930
931 sdmode = (0
932 | ((mr & 0x3) << 14)
933 | ((pd & 0x1) << 12)
934 | ((wr & 0x7) << 9)
935 | ((dll_res & 0x1) << 8)
936 | ((mode & 0x1) << 7)
937 | ((caslat & 0x7) << 4)
938 | ((bt & 0x1) << 3)
939 | ((bl & 0x7) << 0)
940 );
941
942 ddr->ddr_sdram_mode = (0
943 | ((esdmode & 0xFFFF) << 16)
944 | ((sdmode & 0xFFFF) << 0)
945 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400946 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
Kumar Gala124b0822008-08-26 15:01:29 -0500947}
Dave Liu4be87b22009-03-14 12:48:30 +0800948#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500949
950/* DDR SDRAM Data Initialization (DDR_DATA_INIT) */
951static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr)
952{
953 unsigned int init_value; /* Initialization value */
954
955 init_value = 0xDEADBEEF;
956 ddr->ddr_data_init = init_value;
957}
958
959/*
960 * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL)
961 * The old controller on the 8540/60 doesn't have this register.
962 * Hope it's OK to set it (to 0) anyway.
963 */
964static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
965 const memctl_options_t *popts)
966{
967 unsigned int clk_adjust; /* Clock adjust */
968
969 clk_adjust = popts->clk_adjust;
970 ddr->ddr_sdram_clk_cntl = (clk_adjust & 0xF) << 23;
yorkde879322010-07-02 22:25:55 +0000971 debug("FSLDDR: clk_cntl = 0x%08x\n", ddr->ddr_sdram_clk_cntl);
Kumar Gala124b0822008-08-26 15:01:29 -0500972}
973
974/* DDR Initialization Address (DDR_INIT_ADDR) */
975static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
976{
977 unsigned int init_addr = 0; /* Initialization address */
978
979 ddr->ddr_init_addr = init_addr;
980}
981
982/* DDR Initialization Address (DDR_INIT_EXT_ADDR) */
983static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
984{
985 unsigned int uia = 0; /* Use initialization address */
986 unsigned int init_ext_addr = 0; /* Initialization address */
987
988 ddr->ddr_init_ext_addr = (0
989 | ((uia & 0x1) << 31)
990 | (init_ext_addr & 0xF)
991 );
992}
993
994/* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */
Dave Liu3525e1a2010-03-05 12:22:00 +0800995static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr,
996 const memctl_options_t *popts)
Kumar Gala124b0822008-08-26 15:01:29 -0500997{
998 unsigned int rwt = 0; /* Read-to-write turnaround for same CS */
999 unsigned int wrt = 0; /* Write-to-read turnaround for same CS */
1000 unsigned int rrt = 0; /* Read-to-read turnaround for same CS */
1001 unsigned int wwt = 0; /* Write-to-write turnaround for same CS */
1002 unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */
1003
Dave Liu4be87b22009-03-14 12:48:30 +08001004#if defined(CONFIG_FSL_DDR3)
Dave Liu3525e1a2010-03-05 12:22:00 +08001005 if (popts->burst_length == DDR_BL8) {
1006 /* We set BL/2 for fixed BL8 */
1007 rrt = 0; /* BL/2 clocks */
1008 wwt = 0; /* BL/2 clocks */
1009 } else {
1010 /* We need to set BL/2 + 2 to BC4 and OTF */
1011 rrt = 2; /* BL/2 + 2 clocks */
1012 wwt = 2; /* BL/2 + 2 clocks */
1013 }
Dave Liu4be87b22009-03-14 12:48:30 +08001014 dll_lock = 1; /* tDLLK = 512 clocks from spec */
1015#endif
Kumar Gala124b0822008-08-26 15:01:29 -05001016 ddr->timing_cfg_4 = (0
1017 | ((rwt & 0xf) << 28)
1018 | ((wrt & 0xf) << 24)
1019 | ((rrt & 0xf) << 20)
1020 | ((wwt & 0xf) << 16)
1021 | (dll_lock & 0x3)
1022 );
Haiying Wangd90e0402008-10-03 12:37:26 -04001023 debug("FSLDDR: timing_cfg_4 = 0x%08x\n", ddr->timing_cfg_4);
Kumar Gala124b0822008-08-26 15:01:29 -05001024}
1025
1026/* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */
1027static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr)
1028{
1029 unsigned int rodt_on = 0; /* Read to ODT on */
1030 unsigned int rodt_off = 0; /* Read to ODT off */
1031 unsigned int wodt_on = 0; /* Write to ODT on */
1032 unsigned int wodt_off = 0; /* Write to ODT off */
1033
Dave Liu4be87b22009-03-14 12:48:30 +08001034#if defined(CONFIG_FSL_DDR3)
york1714e492010-07-02 22:25:56 +00001035 rodt_on = 2; /* 2 clocks */
Dave Liu4be87b22009-03-14 12:48:30 +08001036 rodt_off = 4; /* 4 clocks */
york1714e492010-07-02 22:25:56 +00001037 wodt_on = 1; /* 1 clocks */
Dave Liu4be87b22009-03-14 12:48:30 +08001038 wodt_off = 4; /* 4 clocks */
1039#endif
1040
Kumar Gala124b0822008-08-26 15:01:29 -05001041 ddr->timing_cfg_5 = (0
Dave Liu4758d532008-11-21 16:31:29 +08001042 | ((rodt_on & 0x1f) << 24)
1043 | ((rodt_off & 0x7) << 20)
1044 | ((wodt_on & 0x1f) << 12)
1045 | ((wodt_off & 0x7) << 8)
Kumar Gala124b0822008-08-26 15:01:29 -05001046 );
Haiying Wangd90e0402008-10-03 12:37:26 -04001047 debug("FSLDDR: timing_cfg_5 = 0x%08x\n", ddr->timing_cfg_5);
Kumar Gala124b0822008-08-26 15:01:29 -05001048}
1049
1050/* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */
Dave Liu4be87b22009-03-14 12:48:30 +08001051static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int zq_en)
Kumar Gala124b0822008-08-26 15:01:29 -05001052{
Kumar Gala124b0822008-08-26 15:01:29 -05001053 unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */
1054 /* Normal Operation Full Calibration Time (tZQoper) */
1055 unsigned int zqoper = 0;
1056 /* Normal Operation Short Calibration Time (tZQCS) */
1057 unsigned int zqcs = 0;
1058
Dave Liu4be87b22009-03-14 12:48:30 +08001059 if (zq_en) {
1060 zqinit = 9; /* 512 clocks */
1061 zqoper = 8; /* 256 clocks */
1062 zqcs = 6; /* 64 clocks */
1063 }
1064
Kumar Gala124b0822008-08-26 15:01:29 -05001065 ddr->ddr_zq_cntl = (0
1066 | ((zq_en & 0x1) << 31)
1067 | ((zqinit & 0xF) << 24)
1068 | ((zqoper & 0xF) << 16)
1069 | ((zqcs & 0xF) << 8)
1070 );
1071}
1072
1073/* DDR Write Leveling Control (DDR_WRLVL_CNTL) */
Dave Liu64ee7df2009-12-16 10:24:37 -06001074static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int wrlvl_en,
1075 const memctl_options_t *popts)
Kumar Gala124b0822008-08-26 15:01:29 -05001076{
Kumar Gala124b0822008-08-26 15:01:29 -05001077 /*
1078 * First DQS pulse rising edge after margining mode
1079 * is programmed (tWL_MRD)
1080 */
1081 unsigned int wrlvl_mrd = 0;
1082 /* ODT delay after margining mode is programmed (tWL_ODTEN) */
1083 unsigned int wrlvl_odten = 0;
1084 /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */
1085 unsigned int wrlvl_dqsen = 0;
1086 /* WRLVL_SMPL: Write leveling sample time */
1087 unsigned int wrlvl_smpl = 0;
1088 /* WRLVL_WLR: Write leveling repeition time */
1089 unsigned int wrlvl_wlr = 0;
1090 /* WRLVL_START: Write leveling start time */
1091 unsigned int wrlvl_start = 0;
1092
Dave Liu4be87b22009-03-14 12:48:30 +08001093 /* suggest enable write leveling for DDR3 due to fly-by topology */
1094 if (wrlvl_en) {
1095 /* tWL_MRD min = 40 nCK, we set it 64 */
1096 wrlvl_mrd = 0x6;
1097 /* tWL_ODTEN 128 */
1098 wrlvl_odten = 0x7;
1099 /* tWL_DQSEN min = 25 nCK, we set it 32 */
1100 wrlvl_dqsen = 0x5;
1101 /*
Dave Liu64ee7df2009-12-16 10:24:37 -06001102 * Write leveling sample time at least need 6 clocks
1103 * higher than tWLO to allow enough time for progagation
1104 * delay and sampling the prime data bits.
Dave Liu4be87b22009-03-14 12:48:30 +08001105 */
1106 wrlvl_smpl = 0xf;
1107 /*
1108 * Write leveling repetition time
1109 * at least tWLO + 6 clocks clocks
york1714e492010-07-02 22:25:56 +00001110 * we set it 64
Dave Liu4be87b22009-03-14 12:48:30 +08001111 */
york1714e492010-07-02 22:25:56 +00001112 wrlvl_wlr = 0x6;
Dave Liu4be87b22009-03-14 12:48:30 +08001113 /*
1114 * Write leveling start time
1115 * The value use for the DQS_ADJUST for the first sample
1116 * when write leveling is enabled.
Dave Liu4be87b22009-03-14 12:48:30 +08001117 */
1118 wrlvl_start = 0x8;
Dave Liu64ee7df2009-12-16 10:24:37 -06001119 /*
1120 * Override the write leveling sample and start time
1121 * according to specific board
1122 */
1123 if (popts->wrlvl_override) {
1124 wrlvl_smpl = popts->wrlvl_sample;
1125 wrlvl_start = popts->wrlvl_start;
1126 }
Dave Liu4be87b22009-03-14 12:48:30 +08001127 }
1128
Kumar Gala124b0822008-08-26 15:01:29 -05001129 ddr->ddr_wrlvl_cntl = (0
1130 | ((wrlvl_en & 0x1) << 31)
1131 | ((wrlvl_mrd & 0x7) << 24)
1132 | ((wrlvl_odten & 0x7) << 20)
1133 | ((wrlvl_dqsen & 0x7) << 16)
1134 | ((wrlvl_smpl & 0xf) << 12)
1135 | ((wrlvl_wlr & 0x7) << 8)
Dave Liu4758d532008-11-21 16:31:29 +08001136 | ((wrlvl_start & 0x1F) << 0)
Kumar Gala124b0822008-08-26 15:01:29 -05001137 );
1138}
1139
1140/* DDR Self Refresh Counter (DDR_SR_CNTR) */
Dave Liu2aad0ae2008-11-21 16:31:35 +08001141static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr, unsigned int sr_it)
Kumar Gala124b0822008-08-26 15:01:29 -05001142{
Dave Liu2aad0ae2008-11-21 16:31:35 +08001143 /* Self Refresh Idle Threshold */
Kumar Gala124b0822008-08-26 15:01:29 -05001144 ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
1145}
1146
york42603722010-07-02 22:25:54 +00001147static void set_ddr_eor(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
1148{
1149 if (popts->addr_hash) {
1150 ddr->ddr_eor = 0x40000000; /* address hash enable */
1151 puts("Addess hashing enabled.\n");
1152 }
1153}
1154
Kumar Gala124b0822008-08-26 15:01:29 -05001155unsigned int
1156check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
1157{
1158 unsigned int res = 0;
1159
1160 /*
1161 * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are
1162 * not set at the same time.
1163 */
1164 if (ddr->ddr_sdram_cfg & 0x10000000
1165 && ddr->ddr_sdram_cfg & 0x00008000) {
1166 printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
1167 " should not be set at the same time.\n");
1168 res++;
1169 }
1170
1171 return res;
1172}
1173
1174unsigned int
1175compute_fsl_memctl_config_regs(const memctl_options_t *popts,
1176 fsl_ddr_cfg_regs_t *ddr,
1177 const common_timing_params_t *common_dimm,
1178 const dimm_params_t *dimm_params,
1179 unsigned int dbw_cap_adj)
1180{
1181 unsigned int i;
1182 unsigned int cas_latency;
1183 unsigned int additive_latency;
Dave Liu2aad0ae2008-11-21 16:31:35 +08001184 unsigned int sr_it;
Dave Liu4be87b22009-03-14 12:48:30 +08001185 unsigned int zq_en;
1186 unsigned int wrlvl_en;
York Sun2927c5e2010-10-18 13:46:50 -07001187 int cs_en = 1;
Kumar Gala124b0822008-08-26 15:01:29 -05001188
1189 memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
1190
1191 if (common_dimm == NULL) {
1192 printf("Error: subset DIMM params struct null pointer\n");
1193 return 1;
1194 }
1195
1196 /*
1197 * Process overrides first.
1198 *
1199 * FIXME: somehow add dereated caslat to this
1200 */
1201 cas_latency = (popts->cas_latency_override)
1202 ? popts->cas_latency_override_value
1203 : common_dimm->lowest_common_SPD_caslat;
1204
1205 additive_latency = (popts->additive_latency_override)
1206 ? popts->additive_latency_override_value
1207 : common_dimm->additive_latency;
1208
Dave Liu2aad0ae2008-11-21 16:31:35 +08001209 sr_it = (popts->auto_self_refresh_en)
1210 ? popts->sr_it
1211 : 0;
Dave Liu4be87b22009-03-14 12:48:30 +08001212 /* ZQ calibration */
1213 zq_en = (popts->zq_en) ? 1 : 0;
1214 /* write leveling */
1215 wrlvl_en = (popts->wrlvl_en) ? 1 : 0;
Dave Liu2aad0ae2008-11-21 16:31:35 +08001216
Kumar Gala124b0822008-08-26 15:01:29 -05001217 /* Chip Select Memory Bounds (CSn_BNDS) */
1218 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
Kumar Gala68ef4bd2009-06-11 23:42:35 -05001219 unsigned long long ea = 0, sa = 0;
york93799ca2010-07-02 22:25:52 +00001220 unsigned int cs_per_dimm
1221 = CONFIG_CHIP_SELECTS_PER_CTRL / CONFIG_DIMM_SLOTS_PER_CTLR;
1222 unsigned int dimm_number
1223 = i / cs_per_dimm;
1224 unsigned long long rank_density
1225 = dimm_params[dimm_number].rank_density;
Haiying Wang272b5962008-10-03 12:36:39 -04001226
york93799ca2010-07-02 22:25:52 +00001227 if (((i == 1) && (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1)) ||
1228 ((i == 2) && (popts->ba_intlv_ctl & 0x04)) ||
1229 ((i == 3) && (popts->ba_intlv_ctl & FSL_DDR_CS2_CS3))) {
1230 /*
1231 * Don't set up boundaries for unused CS
1232 * cs1 for cs0_cs1, cs0_cs1_and_cs2_cs3, cs0_cs1_cs2_cs3
1233 * cs2 for cs0_cs1_cs2_cs3
1234 * cs3 for cs2_cs3, cs0_cs1_and_cs2_cs3, cs0_cs1_cs2_cs3
Dave Liu625b2682009-12-16 10:24:39 -06001235 * But we need to set the ODT_RD_CFG and
1236 * ODT_WR_CFG for CS1_CONFIG here.
Haiying Wang272b5962008-10-03 12:36:39 -04001237 */
yorkf4f93c62010-07-02 22:25:53 +00001238 set_csn_config(dimm_number, i, ddr, popts, dimm_params);
york93799ca2010-07-02 22:25:52 +00001239 continue;
Kumar Gala124b0822008-08-26 15:01:29 -05001240 }
york93799ca2010-07-02 22:25:52 +00001241 if (dimm_params[dimm_number].n_ranks == 0) {
Kumar Gala124b0822008-08-26 15:01:29 -05001242 debug("Skipping setup of CS%u "
yorkf4f93c62010-07-02 22:25:53 +00001243 "because n_ranks on DIMM %u is 0\n", i, dimm_number);
Kumar Gala124b0822008-08-26 15:01:29 -05001244 continue;
1245 }
1246 if (popts->memctl_interleaving && popts->ba_intlv_ctl) {
1247 /*
1248 * This works superbank 2CS
york93799ca2010-07-02 22:25:52 +00001249 * There are 2 or more memory controllers configured
Kumar Gala124b0822008-08-26 15:01:29 -05001250 * identically, memory is interleaved between them,
1251 * and each controller uses rank interleaving within
1252 * itself. Therefore the starting and ending address
1253 * on each controller is twice the amount present on
York Sun2927c5e2010-10-18 13:46:50 -07001254 * each controller. If any CS is not included in the
1255 * interleaving, the memory on that CS is not accssible
1256 * and the total memory size is reduced. The CS is also
1257 * disabled.
Kumar Gala124b0822008-08-26 15:01:29 -05001258 */
york93799ca2010-07-02 22:25:52 +00001259 unsigned long long ctlr_density = 0;
1260 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
1261 case FSL_DDR_CS0_CS1:
1262 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
1263 ctlr_density = dimm_params[0].rank_density * 2;
York Sun2927c5e2010-10-18 13:46:50 -07001264 if (i > 1)
1265 cs_en = 0;
york93799ca2010-07-02 22:25:52 +00001266 break;
1267 case FSL_DDR_CS2_CS3:
1268 ctlr_density = dimm_params[0].rank_density;
York Sun2927c5e2010-10-18 13:46:50 -07001269 if (i > 0)
1270 cs_en = 0;
york93799ca2010-07-02 22:25:52 +00001271 break;
1272 case FSL_DDR_CS0_CS1_CS2_CS3:
1273 /*
1274 * The four CS interleaving should have been verified by
1275 * populate_memctl_options()
1276 */
1277 ctlr_density = dimm_params[0].rank_density * 4;
1278 break;
1279 default:
1280 break;
1281 }
1282 ea = (CONFIG_NUM_DDR_CONTROLLERS *
1283 (ctlr_density >> dbw_cap_adj)) - 1;
Kumar Gala124b0822008-08-26 15:01:29 -05001284 }
1285 else if (!popts->memctl_interleaving && popts->ba_intlv_ctl) {
1286 /*
1287 * If memory interleaving between controllers is NOT
1288 * enabled, the starting address for each memory
1289 * controller is distinct. However, because rank
1290 * interleaving is enabled, the starting and ending
1291 * addresses of the total memory on that memory
1292 * controller needs to be programmed into its
1293 * respective CS0_BNDS.
1294 */
Haiying Wang272b5962008-10-03 12:36:39 -04001295 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
1296 case FSL_DDR_CS0_CS1_CS2_CS3:
1297 /* CS0+CS1+CS2+CS3 interleaving, only CS0_CNDS
1298 * needs to be set.
1299 */
1300 sa = common_dimm->base_address;
1301 ea = sa + (4 * (rank_density >> dbw_cap_adj))-1;
1302 break;
1303 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
1304 /* CS0+CS1 and CS2+CS3 interleaving, CS0_CNDS
1305 * and CS2_CNDS need to be set.
1306 */
york93799ca2010-07-02 22:25:52 +00001307 if ((i == 2) && (dimm_number == 0)) {
1308 sa = dimm_params[dimm_number].base_address +
1309 2 * (rank_density >> dbw_cap_adj);
1310 ea = sa + 2 * (rank_density >> dbw_cap_adj) - 1;
1311 } else {
1312 sa = dimm_params[dimm_number].base_address;
1313 ea = sa + (2 * (rank_density >>
Haiying Wang272b5962008-10-03 12:36:39 -04001314 dbw_cap_adj)) - 1;
1315 }
1316 break;
1317 case FSL_DDR_CS0_CS1:
1318 /* CS0+CS1 interleaving, CS0_CNDS needs
1319 * to be set
1320 */
york93799ca2010-07-02 22:25:52 +00001321 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
1322 sa = dimm_params[dimm_number].base_address;
1323 ea = sa + (rank_density >> dbw_cap_adj) - 1;
1324 sa += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
1325 ea += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
1326 } else {
1327 sa = 0;
1328 ea = 0;
1329 }
1330 if (i == 0)
1331 ea += (rank_density >> dbw_cap_adj);
Haiying Wang272b5962008-10-03 12:36:39 -04001332 break;
1333 case FSL_DDR_CS2_CS3:
1334 /* CS2+CS3 interleaving*/
york93799ca2010-07-02 22:25:52 +00001335 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
1336 sa = dimm_params[dimm_number].base_address;
1337 ea = sa + (rank_density >> dbw_cap_adj) - 1;
1338 sa += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
1339 ea += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
1340 } else {
1341 sa = 0;
1342 ea = 0;
Haiying Wang272b5962008-10-03 12:36:39 -04001343 }
york93799ca2010-07-02 22:25:52 +00001344 if (i == 2)
1345 ea += (rank_density >> dbw_cap_adj);
Haiying Wang272b5962008-10-03 12:36:39 -04001346 break;
1347 default: /* No bank(chip-select) interleaving */
1348 break;
1349 }
Kumar Gala124b0822008-08-26 15:01:29 -05001350 }
1351 else if (popts->memctl_interleaving && !popts->ba_intlv_ctl) {
1352 /*
1353 * Only the rank on CS0 of each memory controller may
1354 * be used if memory controller interleaving is used
1355 * without rank interleaving within each memory
1356 * controller. However, the ending address programmed
1357 * into each CS0 must be the sum of the amount of
1358 * memory in the two CS0 ranks.
1359 */
1360 if (i == 0) {
Kumar Gala124b0822008-08-26 15:01:29 -05001361 ea = (2 * (rank_density >> dbw_cap_adj)) - 1;
1362 }
1363
1364 }
1365 else if (!popts->memctl_interleaving && !popts->ba_intlv_ctl) {
1366 /*
1367 * No rank interleaving and no memory controller
1368 * interleaving.
1369 */
york93799ca2010-07-02 22:25:52 +00001370 sa = dimm_params[dimm_number].base_address;
Kumar Gala124b0822008-08-26 15:01:29 -05001371 ea = sa + (rank_density >> dbw_cap_adj) - 1;
york93799ca2010-07-02 22:25:52 +00001372 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
1373 sa += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
1374 ea += (i % cs_per_dimm) * (rank_density >> dbw_cap_adj);
1375 } else {
1376 sa = 0;
1377 ea = 0;
Kumar Gala124b0822008-08-26 15:01:29 -05001378 }
1379 }
1380
1381 sa >>= 24;
1382 ea >>= 24;
1383
1384 ddr->cs[i].bnds = (0
1385 | ((sa & 0xFFF) << 16) /* starting address MSB */
1386 | ((ea & 0xFFF) << 0) /* ending address MSB */
1387 );
1388
Haiying Wangd90e0402008-10-03 12:37:26 -04001389 debug("FSLDDR: cs[%d]_bnds = 0x%08x\n", i, ddr->cs[i].bnds);
York Sun2927c5e2010-10-18 13:46:50 -07001390 if (cs_en) {
1391 set_csn_config(dimm_number, i, ddr, popts, dimm_params);
1392 set_csn_config_2(i, ddr);
1393 } else
1394 printf("CS%d is disabled.\n", i);
Kumar Gala124b0822008-08-26 15:01:29 -05001395 }
1396
york42603722010-07-02 22:25:54 +00001397 set_ddr_eor(ddr, popts);
1398
Dave Liu4be87b22009-03-14 12:48:30 +08001399#if !defined(CONFIG_FSL_DDR1)
Kumar Gala124b0822008-08-26 15:01:29 -05001400 set_timing_cfg_0(ddr);
1401#endif
1402
Dave Liu4be87b22009-03-14 12:48:30 +08001403 set_timing_cfg_3(ddr, common_dimm, cas_latency);
1404 set_timing_cfg_1(ddr, popts, common_dimm, cas_latency);
Kumar Gala124b0822008-08-26 15:01:29 -05001405 set_timing_cfg_2(ddr, popts, common_dimm,
1406 cas_latency, additive_latency);
1407
1408 set_ddr_sdram_cfg(ddr, popts, common_dimm);
1409
1410 set_ddr_sdram_cfg_2(ddr, popts);
1411 set_ddr_sdram_mode(ddr, popts, common_dimm,
1412 cas_latency, additive_latency);
Dave Liu2d0f1252009-12-16 10:24:38 -06001413 set_ddr_sdram_mode_2(ddr, popts);
Kumar Gala124b0822008-08-26 15:01:29 -05001414 set_ddr_sdram_interval(ddr, popts, common_dimm);
1415 set_ddr_data_init(ddr);
1416 set_ddr_sdram_clk_cntl(ddr, popts);
1417 set_ddr_init_addr(ddr);
1418 set_ddr_init_ext_addr(ddr);
Dave Liu3525e1a2010-03-05 12:22:00 +08001419 set_timing_cfg_4(ddr, popts);
Kumar Gala124b0822008-08-26 15:01:29 -05001420 set_timing_cfg_5(ddr);
1421
Dave Liu4be87b22009-03-14 12:48:30 +08001422 set_ddr_zq_cntl(ddr, zq_en);
Dave Liu64ee7df2009-12-16 10:24:37 -06001423 set_ddr_wrlvl_cntl(ddr, wrlvl_en, popts);
Kumar Gala124b0822008-08-26 15:01:29 -05001424
Dave Liu2aad0ae2008-11-21 16:31:35 +08001425 set_ddr_sr_cntr(ddr, sr_it);
Kumar Gala124b0822008-08-26 15:01:29 -05001426
yorkde879322010-07-02 22:25:55 +00001427 set_ddr_sdram_rcw(ddr, common_dimm);
Kumar Gala124b0822008-08-26 15:01:29 -05001428
1429 return check_fsl_memctl_config_regs(ddr);
1430}