blob: b5e4070414a243bab25c8f6538b174112f1fea13 [file] [log] [blame]
Kumar Gala124b0822008-08-26 15:01:29 -05001/*
York Sunbad82092012-08-17 08:22:38 +00002 * Copyright 2008-2012 Freescale Semiconductor, Inc.
Kumar Gala124b0822008-08-26 15:01:29 -05003 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Kumar Gala124b0822008-08-26 15:01:29 -05005 */
6
7/*
8 * Generic driver for Freescale DDR/DDR2/DDR3 memory controller.
9 * Based on code from spd_sdram.c
10 * Author: James Yang [at freescale.com]
11 */
12
13#include <common.h>
14#include <asm/fsl_ddr_sdram.h>
15
16#include "ddr.h"
17
Andy Fleming992562c2012-10-23 19:03:46 -050018#define _DDR_ADDR CONFIG_SYS_MPC8xxx_DDR_ADDR
York Sunba0c2eb2011-01-10 12:03:00 +000019
Kim Phillips82f576f2012-10-29 13:34:37 +000020static u32 fsl_ddr_get_version(void)
York Sunba0c2eb2011-01-10 12:03:00 +000021{
22 ccsr_ddr_t *ddr;
23 u32 ver_major_minor_errata;
24
25 ddr = (void *)_DDR_ADDR;
26 ver_major_minor_errata = (in_be32(&ddr->ip_rev1) & 0xFFFF) << 8;
27 ver_major_minor_errata |= (in_be32(&ddr->ip_rev2) & 0xFF00) >> 8;
28
29 return ver_major_minor_errata;
30}
31
32unsigned int picos_to_mclk(unsigned int picos);
33
Kumar Gala124b0822008-08-26 15:01:29 -050034/*
35 * Determine Rtt value.
36 *
37 * This should likely be either board or controller specific.
38 *
Dave Liu4be87b22009-03-14 12:48:30 +080039 * Rtt(nominal) - DDR2:
Kumar Gala124b0822008-08-26 15:01:29 -050040 * 0 = Rtt disabled
41 * 1 = 75 ohm
42 * 2 = 150 ohm
43 * 3 = 50 ohm
Dave Liu4be87b22009-03-14 12:48:30 +080044 * Rtt(nominal) - DDR3:
45 * 0 = Rtt disabled
46 * 1 = 60 ohm
47 * 2 = 120 ohm
48 * 3 = 40 ohm
49 * 4 = 20 ohm
50 * 5 = 30 ohm
Kumar Gala124b0822008-08-26 15:01:29 -050051 *
52 * FIXME: Apparently 8641 needs a value of 2
53 * FIXME: Old code seys if 667 MHz or higher, use 3 on 8572
54 *
55 * FIXME: There was some effort down this line earlier:
56 *
57 * unsigned int i;
58 * for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL/2; i++) {
59 * if (popts->dimmslot[i].num_valid_cs
60 * && (popts->cs_local_opts[2*i].odt_rd_cfg
61 * || popts->cs_local_opts[2*i].odt_wr_cfg)) {
62 * rtt = 2;
63 * break;
64 * }
65 * }
66 */
67static inline int fsl_ddr_get_rtt(void)
68{
69 int rtt;
70
71#if defined(CONFIG_FSL_DDR1)
72 rtt = 0;
73#elif defined(CONFIG_FSL_DDR2)
74 rtt = 3;
75#else
Dave Liu4be87b22009-03-14 12:48:30 +080076 rtt = 0;
Kumar Gala124b0822008-08-26 15:01:29 -050077#endif
78
79 return rtt;
80}
81
Dave Liu4be87b22009-03-14 12:48:30 +080082/*
83 * compute the CAS write latency according to DDR3 spec
84 * CWL = 5 if tCK >= 2.5ns
85 * 6 if 2.5ns > tCK >= 1.875ns
86 * 7 if 1.875ns > tCK >= 1.5ns
87 * 8 if 1.5ns > tCK >= 1.25ns
York Sun7a16d642011-08-24 09:40:25 -070088 * 9 if 1.25ns > tCK >= 1.07ns
89 * 10 if 1.07ns > tCK >= 0.935ns
90 * 11 if 0.935ns > tCK >= 0.833ns
91 * 12 if 0.833ns > tCK >= 0.75ns
Dave Liu4be87b22009-03-14 12:48:30 +080092 */
93static inline unsigned int compute_cas_write_latency(void)
94{
95 unsigned int cwl;
96 const unsigned int mclk_ps = get_memory_clk_period_ps();
97
98 if (mclk_ps >= 2500)
99 cwl = 5;
100 else if (mclk_ps >= 1875)
101 cwl = 6;
102 else if (mclk_ps >= 1500)
103 cwl = 7;
104 else if (mclk_ps >= 1250)
105 cwl = 8;
York Sun7a16d642011-08-24 09:40:25 -0700106 else if (mclk_ps >= 1070)
107 cwl = 9;
108 else if (mclk_ps >= 935)
109 cwl = 10;
110 else if (mclk_ps >= 833)
111 cwl = 11;
112 else if (mclk_ps >= 750)
113 cwl = 12;
114 else {
115 cwl = 12;
116 printf("Warning: CWL is out of range\n");
117 }
Dave Liu4be87b22009-03-14 12:48:30 +0800118 return cwl;
119}
120
Kumar Gala124b0822008-08-26 15:01:29 -0500121/* Chip Select Configuration (CSn_CONFIG) */
yorkf4f93c62010-07-02 22:25:53 +0000122static void set_csn_config(int dimm_number, int i, fsl_ddr_cfg_regs_t *ddr,
Kumar Gala124b0822008-08-26 15:01:29 -0500123 const memctl_options_t *popts,
124 const dimm_params_t *dimm_params)
125{
126 unsigned int cs_n_en = 0; /* Chip Select enable */
127 unsigned int intlv_en = 0; /* Memory controller interleave enable */
128 unsigned int intlv_ctl = 0; /* Interleaving control */
129 unsigned int ap_n_en = 0; /* Chip select n auto-precharge enable */
130 unsigned int odt_rd_cfg = 0; /* ODT for reads configuration */
131 unsigned int odt_wr_cfg = 0; /* ODT for writes configuration */
132 unsigned int ba_bits_cs_n = 0; /* Num of bank bits for SDRAM on CSn */
133 unsigned int row_bits_cs_n = 0; /* Num of row bits for SDRAM on CSn */
134 unsigned int col_bits_cs_n = 0; /* Num of ocl bits for SDRAM on CSn */
yorkf4f93c62010-07-02 22:25:53 +0000135 int go_config = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500136
137 /* Compute CS_CONFIG only for existing ranks of each DIMM. */
yorkf4f93c62010-07-02 22:25:53 +0000138 switch (i) {
139 case 0:
140 if (dimm_params[dimm_number].n_ranks > 0) {
141 go_config = 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500142 /* These fields only available in CS0_CONFIG */
York Sune8dc17b2012-08-17 08:22:39 +0000143 if (!popts->memctl_interleaving)
144 break;
145 switch (popts->memctl_interleaving_mode) {
146 case FSL_DDR_CACHE_LINE_INTERLEAVING:
147 case FSL_DDR_PAGE_INTERLEAVING:
148 case FSL_DDR_BANK_INTERLEAVING:
149 case FSL_DDR_SUPERBANK_INTERLEAVING:
150 intlv_en = popts->memctl_interleaving;
151 intlv_ctl = popts->memctl_interleaving_mode;
152 break;
153 default:
154 break;
155 }
Kumar Gala124b0822008-08-26 15:01:29 -0500156 }
yorkf4f93c62010-07-02 22:25:53 +0000157 break;
158 case 1:
159 if ((dimm_number == 0 && dimm_params[0].n_ranks > 1) || \
160 (dimm_number == 1 && dimm_params[1].n_ranks > 0))
161 go_config = 1;
162 break;
163 case 2:
164 if ((dimm_number == 0 && dimm_params[0].n_ranks > 2) || \
York Sun15f874a2011-08-26 11:32:40 -0700165 (dimm_number >= 1 && dimm_params[dimm_number].n_ranks > 0))
yorkf4f93c62010-07-02 22:25:53 +0000166 go_config = 1;
167 break;
168 case 3:
169 if ((dimm_number == 0 && dimm_params[0].n_ranks > 3) || \
170 (dimm_number == 1 && dimm_params[1].n_ranks > 1) || \
171 (dimm_number == 3 && dimm_params[3].n_ranks > 0))
172 go_config = 1;
173 break;
174 default:
175 break;
176 }
177 if (go_config) {
178 unsigned int n_banks_per_sdram_device;
179 cs_n_en = 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500180 ap_n_en = popts->cs_local_opts[i].auto_precharge;
181 odt_rd_cfg = popts->cs_local_opts[i].odt_rd_cfg;
182 odt_wr_cfg = popts->cs_local_opts[i].odt_wr_cfg;
183 n_banks_per_sdram_device
yorkf4f93c62010-07-02 22:25:53 +0000184 = dimm_params[dimm_number].n_banks_per_sdram_device;
Kumar Gala124b0822008-08-26 15:01:29 -0500185 ba_bits_cs_n = __ilog2(n_banks_per_sdram_device) - 2;
yorkf4f93c62010-07-02 22:25:53 +0000186 row_bits_cs_n = dimm_params[dimm_number].n_row_addr - 12;
187 col_bits_cs_n = dimm_params[dimm_number].n_col_addr - 8;
Kumar Gala124b0822008-08-26 15:01:29 -0500188 }
Kumar Gala124b0822008-08-26 15:01:29 -0500189 ddr->cs[i].config = (0
190 | ((cs_n_en & 0x1) << 31)
191 | ((intlv_en & 0x3) << 29)
Haiying Wang272b5962008-10-03 12:36:39 -0400192 | ((intlv_ctl & 0xf) << 24)
Kumar Gala124b0822008-08-26 15:01:29 -0500193 | ((ap_n_en & 0x1) << 23)
194
195 /* XXX: some implementation only have 1 bit starting at left */
196 | ((odt_rd_cfg & 0x7) << 20)
197
198 /* XXX: Some implementation only have 1 bit starting at left */
199 | ((odt_wr_cfg & 0x7) << 16)
200
201 | ((ba_bits_cs_n & 0x3) << 14)
202 | ((row_bits_cs_n & 0x7) << 8)
203 | ((col_bits_cs_n & 0x7) << 0)
204 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400205 debug("FSLDDR: cs[%d]_config = 0x%08x\n", i,ddr->cs[i].config);
Kumar Gala124b0822008-08-26 15:01:29 -0500206}
207
208/* Chip Select Configuration 2 (CSn_CONFIG_2) */
209/* FIXME: 8572 */
210static void set_csn_config_2(int i, fsl_ddr_cfg_regs_t *ddr)
211{
212 unsigned int pasr_cfg = 0; /* Partial array self refresh config */
213
214 ddr->cs[i].config_2 = ((pasr_cfg & 7) << 24);
Haiying Wangd90e0402008-10-03 12:37:26 -0400215 debug("FSLDDR: cs[%d]_config_2 = 0x%08x\n", i, ddr->cs[i].config_2);
Kumar Gala124b0822008-08-26 15:01:29 -0500216}
217
218/* -3E = 667 CL5, -25 = CL6 800, -25E = CL5 800 */
219
Dave Liu4be87b22009-03-14 12:48:30 +0800220#if !defined(CONFIG_FSL_DDR1)
York Sun98df4d12012-10-08 07:44:23 +0000221static inline int avoid_odt_overlap(const dimm_params_t *dimm_params)
222{
223#if CONFIG_DIMM_SLOTS_PER_CTLR == 1
224 if (dimm_params[0].n_ranks == 4)
225 return 1;
226#endif
227
228#if CONFIG_DIMM_SLOTS_PER_CTLR == 2
229 if ((dimm_params[0].n_ranks == 2) &&
230 (dimm_params[1].n_ranks == 2))
231 return 1;
232
233#ifdef CONFIG_FSL_DDR_FIRST_SLOT_QUAD_CAPABLE
234 if (dimm_params[0].n_ranks == 4)
235 return 1;
236#endif
237#endif
238 return 0;
239}
240
Kumar Gala124b0822008-08-26 15:01:29 -0500241/*
242 * DDR SDRAM Timing Configuration 0 (TIMING_CFG_0)
243 *
244 * Avoid writing for DDR I. The new PQ38 DDR controller
245 * dreams up non-zero default values to be backwards compatible.
246 */
York Sunba0c2eb2011-01-10 12:03:00 +0000247static void set_timing_cfg_0(fsl_ddr_cfg_regs_t *ddr,
York Sun98df4d12012-10-08 07:44:23 +0000248 const memctl_options_t *popts,
249 const dimm_params_t *dimm_params)
Kumar Gala124b0822008-08-26 15:01:29 -0500250{
251 unsigned char trwt_mclk = 0; /* Read-to-write turnaround */
252 unsigned char twrt_mclk = 0; /* Write-to-read turnaround */
253 /* 7.5 ns on -3E; 0 means WL - CL + BL/2 + 1 */
254 unsigned char trrt_mclk = 0; /* Read-to-read turnaround */
255 unsigned char twwt_mclk = 0; /* Write-to-write turnaround */
256
257 /* Active powerdown exit timing (tXARD and tXARDS). */
258 unsigned char act_pd_exit_mclk;
259 /* Precharge powerdown exit timing (tXP). */
260 unsigned char pre_pd_exit_mclk;
york1714e492010-07-02 22:25:56 +0000261 /* ODT powerdown exit timing (tAXPD). */
Kumar Gala124b0822008-08-26 15:01:29 -0500262 unsigned char taxpd_mclk;
263 /* Mode register set cycle time (tMRD). */
264 unsigned char tmrd_mclk;
265
York Sunba0c2eb2011-01-10 12:03:00 +0000266#ifdef CONFIG_FSL_DDR3
Dave Liu4be87b22009-03-14 12:48:30 +0800267 /*
268 * (tXARD and tXARDS). Empirical?
269 * The DDR3 spec has not tXARD,
270 * we use the tXP instead of it.
271 * tXP=max(3nCK, 7.5ns) for DDR3.
Dave Liu4be87b22009-03-14 12:48:30 +0800272 * spec has not the tAXPD, we use
york1714e492010-07-02 22:25:56 +0000273 * tAXPD=1, need design to confirm.
Dave Liu4be87b22009-03-14 12:48:30 +0800274 */
Dave Liuc7d983a2009-12-16 10:24:36 -0600275 int tXP = max((get_memory_clk_period_ps() * 3), 7500); /* unit=ps */
Kumar Galab78c7bf2011-01-31 20:36:02 -0600276 unsigned int data_rate = get_ddr_freq(0);
Dave Liu4be87b22009-03-14 12:48:30 +0800277 tmrd_mclk = 4;
Dave Liu81079262009-12-08 11:56:48 +0800278 /* set the turnaround time */
York Sun98df4d12012-10-08 07:44:23 +0000279
280 /*
281 * for single quad-rank DIMM and two dual-rank DIMMs
282 * to avoid ODT overlap
283 */
284 if (avoid_odt_overlap(dimm_params)) {
285 twwt_mclk = 2;
286 trrt_mclk = 1;
287 }
288 /* for faster clock, need more time for data setup */
289 trwt_mclk = (data_rate/1000000 > 1800) ? 2 : 1;
290
York Sun27f83be2011-02-10 10:13:10 -0800291 if ((data_rate/1000000 > 1150) || (popts->memctl_interleaving))
292 twrt_mclk = 1;
York Sunba0c2eb2011-01-10 12:03:00 +0000293
294 if (popts->dynamic_power == 0) { /* powerdown is not used */
295 act_pd_exit_mclk = 1;
296 pre_pd_exit_mclk = 1;
297 taxpd_mclk = 1;
298 } else {
299 /* act_pd_exit_mclk = tXARD, see above */
300 act_pd_exit_mclk = picos_to_mclk(tXP);
301 /* Mode register MR0[A12] is '1' - fast exit */
302 pre_pd_exit_mclk = act_pd_exit_mclk;
303 taxpd_mclk = 1;
304 }
Dave Liu4be87b22009-03-14 12:48:30 +0800305#else /* CONFIG_FSL_DDR2 */
306 /*
307 * (tXARD and tXARDS). Empirical?
308 * tXARD = 2 for DDR2
309 * tXP=2
310 * tAXPD=8
311 */
312 act_pd_exit_mclk = 2;
313 pre_pd_exit_mclk = 2;
314 taxpd_mclk = 8;
Kumar Gala124b0822008-08-26 15:01:29 -0500315 tmrd_mclk = 2;
Dave Liu4be87b22009-03-14 12:48:30 +0800316#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500317
York Sunf8691fc2011-05-27 13:44:28 +0800318 if (popts->trwt_override)
319 trwt_mclk = popts->trwt;
320
Kumar Gala124b0822008-08-26 15:01:29 -0500321 ddr->timing_cfg_0 = (0
322 | ((trwt_mclk & 0x3) << 30) /* RWT */
323 | ((twrt_mclk & 0x3) << 28) /* WRT */
324 | ((trrt_mclk & 0x3) << 26) /* RRT */
325 | ((twwt_mclk & 0x3) << 24) /* WWT */
326 | ((act_pd_exit_mclk & 0x7) << 20) /* ACT_PD_EXIT */
Dave Liu4758d532008-11-21 16:31:29 +0800327 | ((pre_pd_exit_mclk & 0xF) << 16) /* PRE_PD_EXIT */
Kumar Gala124b0822008-08-26 15:01:29 -0500328 | ((taxpd_mclk & 0xf) << 8) /* ODT_PD_EXIT */
329 | ((tmrd_mclk & 0xf) << 0) /* MRS_CYC */
330 );
331 debug("FSLDDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
332}
333#endif /* defined(CONFIG_FSL_DDR2) */
334
335/* DDR SDRAM Timing Configuration 3 (TIMING_CFG_3) */
336static void set_timing_cfg_3(fsl_ddr_cfg_regs_t *ddr,
York Suncd077cf2012-08-17 08:22:40 +0000337 const memctl_options_t *popts,
Dave Liu4be87b22009-03-14 12:48:30 +0800338 const common_timing_params_t *common_dimm,
339 unsigned int cas_latency)
Kumar Gala124b0822008-08-26 15:01:29 -0500340{
York Suncd077cf2012-08-17 08:22:40 +0000341 /* Extended precharge to activate interval (tRP) */
342 unsigned int ext_pretoact = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500343 /* Extended Activate to precharge interval (tRAS) */
344 unsigned int ext_acttopre = 0;
York Suncd077cf2012-08-17 08:22:40 +0000345 /* Extended activate to read/write interval (tRCD) */
346 unsigned int ext_acttorw = 0;
347 /* Extended refresh recovery time (tRFC) */
348 unsigned int ext_refrec;
349 /* Extended MCAS latency from READ cmd */
350 unsigned int ext_caslat = 0;
351 /* Extended last data to precharge interval (tWR) */
352 unsigned int ext_wrrec = 0;
353 /* Control Adjust */
354 unsigned int cntl_adj = 0;
Dave Liu5c1bb512008-11-21 16:31:22 +0800355
York Suncd077cf2012-08-17 08:22:40 +0000356 ext_pretoact = picos_to_mclk(common_dimm->tRP_ps) >> 4;
357 ext_acttopre = picos_to_mclk(common_dimm->tRAS_ps) >> 4;
358 ext_acttorw = picos_to_mclk(common_dimm->tRCD_ps) >> 4;
359 ext_caslat = (2 * cas_latency - 1) >> 4;
Kumar Gala124b0822008-08-26 15:01:29 -0500360 ext_refrec = (picos_to_mclk(common_dimm->tRFC_ps) - 8) >> 4;
York Suncd077cf2012-08-17 08:22:40 +0000361 /* ext_wrrec only deals with 16 clock and above, or 14 with OTF */
362 ext_wrrec = (picos_to_mclk(common_dimm->tWR_ps) +
363 (popts->OTF_burst_chop_en ? 2 : 0)) >> 4;
Dave Liu4be87b22009-03-14 12:48:30 +0800364
Kumar Gala124b0822008-08-26 15:01:29 -0500365 ddr->timing_cfg_3 = (0
York Suncd077cf2012-08-17 08:22:40 +0000366 | ((ext_pretoact & 0x1) << 28)
367 | ((ext_acttopre & 0x2) << 24)
368 | ((ext_acttorw & 0x1) << 22)
369 | ((ext_refrec & 0x1F) << 16)
370 | ((ext_caslat & 0x3) << 12)
371 | ((ext_wrrec & 0x1) << 8)
Kumar Gala124b0822008-08-26 15:01:29 -0500372 | ((cntl_adj & 0x7) << 0)
373 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400374 debug("FSLDDR: timing_cfg_3 = 0x%08x\n", ddr->timing_cfg_3);
Kumar Gala124b0822008-08-26 15:01:29 -0500375}
376
377/* DDR SDRAM Timing Configuration 1 (TIMING_CFG_1) */
378static void set_timing_cfg_1(fsl_ddr_cfg_regs_t *ddr,
Dave Liu4be87b22009-03-14 12:48:30 +0800379 const memctl_options_t *popts,
Kumar Gala124b0822008-08-26 15:01:29 -0500380 const common_timing_params_t *common_dimm,
381 unsigned int cas_latency)
382{
383 /* Precharge-to-activate interval (tRP) */
384 unsigned char pretoact_mclk;
385 /* Activate to precharge interval (tRAS) */
386 unsigned char acttopre_mclk;
387 /* Activate to read/write interval (tRCD) */
388 unsigned char acttorw_mclk;
389 /* CASLAT */
390 unsigned char caslat_ctrl;
391 /* Refresh recovery time (tRFC) ; trfc_low */
392 unsigned char refrec_ctrl;
393 /* Last data to precharge minimum interval (tWR) */
394 unsigned char wrrec_mclk;
395 /* Activate-to-activate interval (tRRD) */
396 unsigned char acttoact_mclk;
397 /* Last write data pair to read command issue interval (tWTR) */
398 unsigned char wrtord_mclk;
York Sun3673f2c2011-03-02 14:24:11 -0800399 /* DDR_SDRAM_MODE doesn't support 9,11,13,15 */
400 static const u8 wrrec_table[] = {
401 1, 2, 3, 4, 5, 6, 7, 8, 10, 10, 12, 12, 14, 14, 0, 0};
Kumar Gala124b0822008-08-26 15:01:29 -0500402
403 pretoact_mclk = picos_to_mclk(common_dimm->tRP_ps);
404 acttopre_mclk = picos_to_mclk(common_dimm->tRAS_ps);
405 acttorw_mclk = picos_to_mclk(common_dimm->tRCD_ps);
406
407 /*
408 * Translate CAS Latency to a DDR controller field value:
409 *
410 * CAS Lat DDR I DDR II Ctrl
411 * Clocks SPD Bit SPD Bit Value
412 * ------- ------- ------- -----
413 * 1.0 0 0001
414 * 1.5 1 0010
415 * 2.0 2 2 0011
416 * 2.5 3 0100
417 * 3.0 4 3 0101
418 * 3.5 5 0110
419 * 4.0 4 0111
420 * 4.5 1000
421 * 5.0 5 1001
422 */
423#if defined(CONFIG_FSL_DDR1)
424 caslat_ctrl = (cas_latency + 1) & 0x07;
425#elif defined(CONFIG_FSL_DDR2)
426 caslat_ctrl = 2 * cas_latency - 1;
427#else
Dave Liu4be87b22009-03-14 12:48:30 +0800428 /*
429 * if the CAS latency more than 8 cycle,
430 * we need set extend bit for it at
431 * TIMING_CFG_3[EXT_CASLAT]
432 */
Dave Liu4be87b22009-03-14 12:48:30 +0800433 caslat_ctrl = 2 * cas_latency - 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500434#endif
435
436 refrec_ctrl = picos_to_mclk(common_dimm->tRFC_ps) - 8;
437 wrrec_mclk = picos_to_mclk(common_dimm->tWR_ps);
York Sun3673f2c2011-03-02 14:24:11 -0800438
York Suncd077cf2012-08-17 08:22:40 +0000439 if (wrrec_mclk > 16)
440 printf("Error: WRREC doesn't support more than 16 clocks\n");
441 else
442 wrrec_mclk = wrrec_table[wrrec_mclk - 1];
Dave Liu4be87b22009-03-14 12:48:30 +0800443 if (popts->OTF_burst_chop_en)
444 wrrec_mclk += 2;
445
Kumar Gala124b0822008-08-26 15:01:29 -0500446 acttoact_mclk = picos_to_mclk(common_dimm->tRRD_ps);
Dave Liu4be87b22009-03-14 12:48:30 +0800447 /*
448 * JEDEC has min requirement for tRRD
449 */
450#if defined(CONFIG_FSL_DDR3)
451 if (acttoact_mclk < 4)
452 acttoact_mclk = 4;
453#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500454 wrtord_mclk = picos_to_mclk(common_dimm->tWTR_ps);
Dave Liu4be87b22009-03-14 12:48:30 +0800455 /*
456 * JEDEC has some min requirements for tWTR
457 */
458#if defined(CONFIG_FSL_DDR2)
459 if (wrtord_mclk < 2)
460 wrtord_mclk = 2;
461#elif defined(CONFIG_FSL_DDR3)
462 if (wrtord_mclk < 4)
463 wrtord_mclk = 4;
464#endif
465 if (popts->OTF_burst_chop_en)
466 wrtord_mclk += 2;
Kumar Gala124b0822008-08-26 15:01:29 -0500467
468 ddr->timing_cfg_1 = (0
Dave Liu5c1bb512008-11-21 16:31:22 +0800469 | ((pretoact_mclk & 0x0F) << 28)
Kumar Gala124b0822008-08-26 15:01:29 -0500470 | ((acttopre_mclk & 0x0F) << 24)
Dave Liu5c1bb512008-11-21 16:31:22 +0800471 | ((acttorw_mclk & 0xF) << 20)
Kumar Gala124b0822008-08-26 15:01:29 -0500472 | ((caslat_ctrl & 0xF) << 16)
473 | ((refrec_ctrl & 0xF) << 12)
Dave Liu5c1bb512008-11-21 16:31:22 +0800474 | ((wrrec_mclk & 0x0F) << 8)
York Sun7d69ea32012-10-08 07:44:22 +0000475 | ((acttoact_mclk & 0x0F) << 4)
476 | ((wrtord_mclk & 0x0F) << 0)
Kumar Gala124b0822008-08-26 15:01:29 -0500477 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400478 debug("FSLDDR: timing_cfg_1 = 0x%08x\n", ddr->timing_cfg_1);
Kumar Gala124b0822008-08-26 15:01:29 -0500479}
480
481/* DDR SDRAM Timing Configuration 2 (TIMING_CFG_2) */
482static void set_timing_cfg_2(fsl_ddr_cfg_regs_t *ddr,
483 const memctl_options_t *popts,
484 const common_timing_params_t *common_dimm,
485 unsigned int cas_latency,
486 unsigned int additive_latency)
487{
488 /* Additive latency */
489 unsigned char add_lat_mclk;
490 /* CAS-to-preamble override */
491 unsigned short cpo;
492 /* Write latency */
493 unsigned char wr_lat;
494 /* Read to precharge (tRTP) */
495 unsigned char rd_to_pre;
496 /* Write command to write data strobe timing adjustment */
497 unsigned char wr_data_delay;
498 /* Minimum CKE pulse width (tCKE) */
499 unsigned char cke_pls;
500 /* Window for four activates (tFAW) */
501 unsigned short four_act;
502
503 /* FIXME add check that this must be less than acttorw_mclk */
504 add_lat_mclk = additive_latency;
505 cpo = popts->cpo_override;
506
507#if defined(CONFIG_FSL_DDR1)
508 /*
509 * This is a lie. It should really be 1, but if it is
510 * set to 1, bits overlap into the old controller's
511 * otherwise unused ACSM field. If we leave it 0, then
512 * the HW will magically treat it as 1 for DDR 1. Oh Yea.
513 */
514 wr_lat = 0;
515#elif defined(CONFIG_FSL_DDR2)
Dave Liu82aa9532009-03-14 12:48:19 +0800516 wr_lat = cas_latency - 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500517#else
Dave Liu4be87b22009-03-14 12:48:30 +0800518 wr_lat = compute_cas_write_latency();
Kumar Gala124b0822008-08-26 15:01:29 -0500519#endif
520
521 rd_to_pre = picos_to_mclk(common_dimm->tRTP_ps);
Dave Liu4be87b22009-03-14 12:48:30 +0800522 /*
523 * JEDEC has some min requirements for tRTP
524 */
Dave Liu82aa9532009-03-14 12:48:19 +0800525#if defined(CONFIG_FSL_DDR2)
Dave Liu4be87b22009-03-14 12:48:30 +0800526 if (rd_to_pre < 2)
527 rd_to_pre = 2;
528#elif defined(CONFIG_FSL_DDR3)
529 if (rd_to_pre < 4)
530 rd_to_pre = 4;
Dave Liu82aa9532009-03-14 12:48:19 +0800531#endif
Dave Liu4be87b22009-03-14 12:48:30 +0800532 if (additive_latency)
533 rd_to_pre += additive_latency;
534 if (popts->OTF_burst_chop_en)
535 rd_to_pre += 2; /* according to UM */
536
Kumar Gala124b0822008-08-26 15:01:29 -0500537 wr_data_delay = popts->write_data_delay;
538 cke_pls = picos_to_mclk(popts->tCKE_clock_pulse_width_ps);
539 four_act = picos_to_mclk(popts->tFAW_window_four_activates_ps);
540
541 ddr->timing_cfg_2 = (0
Dave Liu4758d532008-11-21 16:31:29 +0800542 | ((add_lat_mclk & 0xf) << 28)
Kumar Gala124b0822008-08-26 15:01:29 -0500543 | ((cpo & 0x1f) << 23)
Dave Liu4758d532008-11-21 16:31:29 +0800544 | ((wr_lat & 0xf) << 19)
Dave Liu4be87b22009-03-14 12:48:30 +0800545 | ((rd_to_pre & RD_TO_PRE_MASK) << RD_TO_PRE_SHIFT)
546 | ((wr_data_delay & WR_DATA_DELAY_MASK) << WR_DATA_DELAY_SHIFT)
Kumar Gala124b0822008-08-26 15:01:29 -0500547 | ((cke_pls & 0x7) << 6)
Dave Liu4758d532008-11-21 16:31:29 +0800548 | ((four_act & 0x3f) << 0)
Kumar Gala124b0822008-08-26 15:01:29 -0500549 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400550 debug("FSLDDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2);
Kumar Gala124b0822008-08-26 15:01:29 -0500551}
552
yorkde879322010-07-02 22:25:55 +0000553/* DDR SDRAM Register Control Word */
554static void set_ddr_sdram_rcw(fsl_ddr_cfg_regs_t *ddr,
York Sunba0c2eb2011-01-10 12:03:00 +0000555 const memctl_options_t *popts,
yorkde879322010-07-02 22:25:55 +0000556 const common_timing_params_t *common_dimm)
557{
558 if (common_dimm->all_DIMMs_registered
559 && !common_dimm->all_DIMMs_unbuffered) {
York Sunba0c2eb2011-01-10 12:03:00 +0000560 if (popts->rcw_override) {
561 ddr->ddr_sdram_rcw_1 = popts->rcw_1;
562 ddr->ddr_sdram_rcw_2 = popts->rcw_2;
563 } else {
564 ddr->ddr_sdram_rcw_1 =
565 common_dimm->rcw[0] << 28 | \
566 common_dimm->rcw[1] << 24 | \
567 common_dimm->rcw[2] << 20 | \
568 common_dimm->rcw[3] << 16 | \
569 common_dimm->rcw[4] << 12 | \
570 common_dimm->rcw[5] << 8 | \
571 common_dimm->rcw[6] << 4 | \
572 common_dimm->rcw[7];
573 ddr->ddr_sdram_rcw_2 =
574 common_dimm->rcw[8] << 28 | \
575 common_dimm->rcw[9] << 24 | \
576 common_dimm->rcw[10] << 20 | \
577 common_dimm->rcw[11] << 16 | \
578 common_dimm->rcw[12] << 12 | \
579 common_dimm->rcw[13] << 8 | \
580 common_dimm->rcw[14] << 4 | \
581 common_dimm->rcw[15];
582 }
yorkde879322010-07-02 22:25:55 +0000583 debug("FSLDDR: ddr_sdram_rcw_1 = 0x%08x\n", ddr->ddr_sdram_rcw_1);
584 debug("FSLDDR: ddr_sdram_rcw_2 = 0x%08x\n", ddr->ddr_sdram_rcw_2);
585 }
586}
587
Kumar Gala124b0822008-08-26 15:01:29 -0500588/* DDR SDRAM control configuration (DDR_SDRAM_CFG) */
589static void set_ddr_sdram_cfg(fsl_ddr_cfg_regs_t *ddr,
590 const memctl_options_t *popts,
591 const common_timing_params_t *common_dimm)
592{
593 unsigned int mem_en; /* DDR SDRAM interface logic enable */
594 unsigned int sren; /* Self refresh enable (during sleep) */
595 unsigned int ecc_en; /* ECC enable. */
596 unsigned int rd_en; /* Registered DIMM enable */
597 unsigned int sdram_type; /* Type of SDRAM */
598 unsigned int dyn_pwr; /* Dynamic power management mode */
599 unsigned int dbw; /* DRAM dta bus width */
Dave Liu4758d532008-11-21 16:31:29 +0800600 unsigned int eight_be = 0; /* 8-beat burst enable, DDR2 is zero */
Kumar Gala124b0822008-08-26 15:01:29 -0500601 unsigned int ncap = 0; /* Non-concurrent auto-precharge */
602 unsigned int threeT_en; /* Enable 3T timing */
603 unsigned int twoT_en; /* Enable 2T timing */
604 unsigned int ba_intlv_ctl; /* Bank (CS) interleaving control */
605 unsigned int x32_en = 0; /* x32 enable */
606 unsigned int pchb8 = 0; /* precharge bit 8 enable */
607 unsigned int hse; /* Global half strength override */
608 unsigned int mem_halt = 0; /* memory controller halt */
609 unsigned int bi = 0; /* Bypass initialization */
610
611 mem_en = 1;
612 sren = popts->self_refresh_in_sleep;
613 if (common_dimm->all_DIMMs_ECC_capable) {
614 /* Allow setting of ECC only if all DIMMs are ECC. */
615 ecc_en = popts->ECC_mode;
616 } else {
617 ecc_en = 0;
618 }
619
York Sunba0c2eb2011-01-10 12:03:00 +0000620 if (common_dimm->all_DIMMs_registered
621 && !common_dimm->all_DIMMs_unbuffered) {
622 rd_en = 1;
623 twoT_en = 0;
624 } else {
625 rd_en = 0;
626 twoT_en = popts->twoT_en;
627 }
Kumar Gala124b0822008-08-26 15:01:29 -0500628
629 sdram_type = CONFIG_FSL_SDRAM_TYPE;
630
631 dyn_pwr = popts->dynamic_power;
632 dbw = popts->data_bus_width;
Dave Liu4be87b22009-03-14 12:48:30 +0800633 /* 8-beat burst enable DDR-III case
634 * we must clear it when use the on-the-fly mode,
635 * must set it when use the 32-bits bus mode.
636 */
637 if (sdram_type == SDRAM_TYPE_DDR3) {
638 if (popts->burst_length == DDR_BL8)
639 eight_be = 1;
640 if (popts->burst_length == DDR_OTF)
641 eight_be = 0;
642 if (dbw == 0x1)
643 eight_be = 1;
644 }
645
Kumar Gala124b0822008-08-26 15:01:29 -0500646 threeT_en = popts->threeT_en;
Kumar Gala124b0822008-08-26 15:01:29 -0500647 ba_intlv_ctl = popts->ba_intlv_ctl;
648 hse = popts->half_strength_driver_enable;
649
650 ddr->ddr_sdram_cfg = (0
651 | ((mem_en & 0x1) << 31)
652 | ((sren & 0x1) << 30)
653 | ((ecc_en & 0x1) << 29)
654 | ((rd_en & 0x1) << 28)
655 | ((sdram_type & 0x7) << 24)
656 | ((dyn_pwr & 0x1) << 21)
657 | ((dbw & 0x3) << 19)
658 | ((eight_be & 0x1) << 18)
659 | ((ncap & 0x1) << 17)
660 | ((threeT_en & 0x1) << 16)
661 | ((twoT_en & 0x1) << 15)
662 | ((ba_intlv_ctl & 0x7F) << 8)
663 | ((x32_en & 0x1) << 5)
664 | ((pchb8 & 0x1) << 4)
665 | ((hse & 0x1) << 3)
666 | ((mem_halt & 0x1) << 1)
667 | ((bi & 0x1) << 0)
668 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400669 debug("FSLDDR: ddr_sdram_cfg = 0x%08x\n", ddr->ddr_sdram_cfg);
Kumar Gala124b0822008-08-26 15:01:29 -0500670}
671
672/* DDR SDRAM control configuration 2 (DDR_SDRAM_CFG_2) */
673static void set_ddr_sdram_cfg_2(fsl_ddr_cfg_regs_t *ddr,
York Sunba0c2eb2011-01-10 12:03:00 +0000674 const memctl_options_t *popts,
675 const unsigned int unq_mrs_en)
Kumar Gala124b0822008-08-26 15:01:29 -0500676{
677 unsigned int frc_sr = 0; /* Force self refresh */
678 unsigned int sr_ie = 0; /* Self-refresh interrupt enable */
679 unsigned int dll_rst_dis; /* DLL reset disable */
680 unsigned int dqs_cfg; /* DQS configuration */
York Sun15f874a2011-08-26 11:32:40 -0700681 unsigned int odt_cfg = 0; /* ODT configuration */
Kumar Gala124b0822008-08-26 15:01:29 -0500682 unsigned int num_pr; /* Number of posted refreshes */
York Sun7d69ea32012-10-08 07:44:22 +0000683 unsigned int slow = 0; /* DDR will be run less than 1250 */
Kumar Gala124b0822008-08-26 15:01:29 -0500684 unsigned int obc_cfg; /* On-The-Fly Burst Chop Cfg */
685 unsigned int ap_en; /* Address Parity Enable */
686 unsigned int d_init; /* DRAM data initialization */
687 unsigned int rcw_en = 0; /* Register Control Word Enable */
688 unsigned int md_en = 0; /* Mirrored DIMM Enable */
yorkf4f93c62010-07-02 22:25:53 +0000689 unsigned int qd_en = 0; /* quad-rank DIMM Enable */
York Sun15f874a2011-08-26 11:32:40 -0700690 int i;
Kumar Gala124b0822008-08-26 15:01:29 -0500691
692 dll_rst_dis = 1; /* Make this configurable */
693 dqs_cfg = popts->DQS_config;
York Sun15f874a2011-08-26 11:32:40 -0700694 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
695 if (popts->cs_local_opts[i].odt_rd_cfg
696 || popts->cs_local_opts[i].odt_wr_cfg) {
697 odt_cfg = SDRAM_CFG2_ODT_ONLY_READ;
698 break;
699 }
Kumar Gala124b0822008-08-26 15:01:29 -0500700 }
701
702 num_pr = 1; /* Make this configurable */
703
704 /*
705 * 8572 manual says
706 * {TIMING_CFG_1[PRETOACT]
707 * + [DDR_SDRAM_CFG_2[NUM_PR]
708 * * ({EXT_REFREC || REFREC} + 8 + 2)]}
709 * << DDR_SDRAM_INTERVAL[REFINT]
710 */
Dave Liu4be87b22009-03-14 12:48:30 +0800711#if defined(CONFIG_FSL_DDR3)
712 obc_cfg = popts->OTF_burst_chop_en;
713#else
714 obc_cfg = 0;
715#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500716
York Sun7d69ea32012-10-08 07:44:22 +0000717#if (CONFIG_SYS_FSL_DDR_VER >= FSL_DDR_VER_4_7)
718 slow = get_ddr_freq(0) < 1249000000;
719#endif
720
York Sunba0c2eb2011-01-10 12:03:00 +0000721 if (popts->registered_dimm_en) {
722 rcw_en = 1;
723 ap_en = popts->ap_en;
724 } else {
York Sunba0c2eb2011-01-10 12:03:00 +0000725 ap_en = 0;
726 }
Kumar Gala124b0822008-08-26 15:01:29 -0500727
728#if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
729 /* Use the DDR controller to auto initialize memory. */
York Sunba0c2eb2011-01-10 12:03:00 +0000730 d_init = popts->ECC_init_using_memctl;
Kumar Gala124b0822008-08-26 15:01:29 -0500731 ddr->ddr_data_init = CONFIG_MEM_INIT_VALUE;
732 debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init);
733#else
734 /* Memory will be initialized via DMA, or not at all. */
735 d_init = 0;
736#endif
737
Dave Liu4be87b22009-03-14 12:48:30 +0800738#if defined(CONFIG_FSL_DDR3)
739 md_en = popts->mirrored_dimm;
740#endif
yorkf4f93c62010-07-02 22:25:53 +0000741 qd_en = popts->quad_rank_present ? 1 : 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500742 ddr->ddr_sdram_cfg_2 = (0
743 | ((frc_sr & 0x1) << 31)
744 | ((sr_ie & 0x1) << 30)
745 | ((dll_rst_dis & 0x1) << 29)
746 | ((dqs_cfg & 0x3) << 26)
747 | ((odt_cfg & 0x3) << 21)
748 | ((num_pr & 0xf) << 12)
York Sun7d69ea32012-10-08 07:44:22 +0000749 | ((slow & 1) << 11)
yorkf4f93c62010-07-02 22:25:53 +0000750 | (qd_en << 9)
York Sunba0c2eb2011-01-10 12:03:00 +0000751 | (unq_mrs_en << 8)
Kumar Gala124b0822008-08-26 15:01:29 -0500752 | ((obc_cfg & 0x1) << 6)
753 | ((ap_en & 0x1) << 5)
754 | ((d_init & 0x1) << 4)
755 | ((rcw_en & 0x1) << 2)
756 | ((md_en & 0x1) << 0)
757 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400758 debug("FSLDDR: ddr_sdram_cfg_2 = 0x%08x\n", ddr->ddr_sdram_cfg_2);
Kumar Gala124b0822008-08-26 15:01:29 -0500759}
760
761/* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
Dave Liu2d0f1252009-12-16 10:24:38 -0600762static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr,
York Sunba0c2eb2011-01-10 12:03:00 +0000763 const memctl_options_t *popts,
764 const unsigned int unq_mrs_en)
Kumar Gala124b0822008-08-26 15:01:29 -0500765{
766 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
767 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
768
Dave Liu4be87b22009-03-14 12:48:30 +0800769#if defined(CONFIG_FSL_DDR3)
Kumar Gala65b5be22011-01-20 01:53:15 -0600770 int i;
Dave Liu2d0f1252009-12-16 10:24:38 -0600771 unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */
Dave Liu4be87b22009-03-14 12:48:30 +0800772 unsigned int srt = 0; /* self-refresh temerature, normal range */
773 unsigned int asr = 0; /* auto self-refresh disable */
774 unsigned int cwl = compute_cas_write_latency() - 5;
775 unsigned int pasr = 0; /* partial array self refresh disable */
776
Dave Liu2d0f1252009-12-16 10:24:38 -0600777 if (popts->rtt_override)
778 rtt_wr = popts->rtt_wr_override_value;
York Sunba0c2eb2011-01-10 12:03:00 +0000779 else
780 rtt_wr = popts->cs_local_opts[0].odt_rtt_wr;
Dave Liu4be87b22009-03-14 12:48:30 +0800781 esdmode2 = (0
782 | ((rtt_wr & 0x3) << 9)
783 | ((srt & 0x1) << 7)
784 | ((asr & 0x1) << 6)
785 | ((cwl & 0x7) << 3)
786 | ((pasr & 0x7) << 0));
787#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500788 ddr->ddr_sdram_mode_2 = (0
789 | ((esdmode2 & 0xFFFF) << 16)
790 | ((esdmode3 & 0xFFFF) << 0)
791 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400792 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
York Sunba0c2eb2011-01-10 12:03:00 +0000793
794#ifdef CONFIG_FSL_DDR3
795 if (unq_mrs_en) { /* unique mode registers are supported */
Kumar Galad5bbe662011-11-09 10:05:10 -0600796 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Sunba0c2eb2011-01-10 12:03:00 +0000797 if (popts->rtt_override)
798 rtt_wr = popts->rtt_wr_override_value;
799 else
800 rtt_wr = popts->cs_local_opts[i].odt_rtt_wr;
801
802 esdmode2 &= 0xF9FF; /* clear bit 10, 9 */
803 esdmode2 |= (rtt_wr & 0x3) << 9;
804 switch (i) {
805 case 1:
806 ddr->ddr_sdram_mode_4 = (0
807 | ((esdmode2 & 0xFFFF) << 16)
808 | ((esdmode3 & 0xFFFF) << 0)
809 );
810 break;
811 case 2:
812 ddr->ddr_sdram_mode_6 = (0
813 | ((esdmode2 & 0xFFFF) << 16)
814 | ((esdmode3 & 0xFFFF) << 0)
815 );
816 break;
817 case 3:
818 ddr->ddr_sdram_mode_8 = (0
819 | ((esdmode2 & 0xFFFF) << 16)
820 | ((esdmode3 & 0xFFFF) << 0)
821 );
822 break;
823 }
824 }
825 debug("FSLDDR: ddr_sdram_mode_4 = 0x%08x\n",
826 ddr->ddr_sdram_mode_4);
827 debug("FSLDDR: ddr_sdram_mode_6 = 0x%08x\n",
828 ddr->ddr_sdram_mode_6);
829 debug("FSLDDR: ddr_sdram_mode_8 = 0x%08x\n",
830 ddr->ddr_sdram_mode_8);
831 }
832#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500833}
834
835/* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */
836static void set_ddr_sdram_interval(fsl_ddr_cfg_regs_t *ddr,
837 const memctl_options_t *popts,
838 const common_timing_params_t *common_dimm)
839{
840 unsigned int refint; /* Refresh interval */
841 unsigned int bstopre; /* Precharge interval */
842
843 refint = picos_to_mclk(common_dimm->refresh_rate_ps);
844
845 bstopre = popts->bstopre;
846
847 /* refint field used 0x3FFF in earlier controllers */
848 ddr->ddr_sdram_interval = (0
849 | ((refint & 0xFFFF) << 16)
850 | ((bstopre & 0x3FFF) << 0)
851 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400852 debug("FSLDDR: ddr_sdram_interval = 0x%08x\n", ddr->ddr_sdram_interval);
Kumar Gala124b0822008-08-26 15:01:29 -0500853}
854
Dave Liu4be87b22009-03-14 12:48:30 +0800855#if defined(CONFIG_FSL_DDR3)
Kumar Gala124b0822008-08-26 15:01:29 -0500856/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
857static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
858 const memctl_options_t *popts,
859 const common_timing_params_t *common_dimm,
860 unsigned int cas_latency,
York Sunba0c2eb2011-01-10 12:03:00 +0000861 unsigned int additive_latency,
862 const unsigned int unq_mrs_en)
Kumar Gala124b0822008-08-26 15:01:29 -0500863{
864 unsigned short esdmode; /* Extended SDRAM mode */
865 unsigned short sdmode; /* SDRAM mode */
866
Dave Liu4be87b22009-03-14 12:48:30 +0800867 /* Mode Register - MR1 */
868 unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */
869 unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */
870 unsigned int rtt;
871 unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */
872 unsigned int al = 0; /* Posted CAS# additive latency (AL) */
York Sunba0c2eb2011-01-10 12:03:00 +0000873 unsigned int dic = 0; /* Output driver impedance, 40ohm */
Dave Liu4be87b22009-03-14 12:48:30 +0800874 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
875 1=Disable (Test/Debug) */
876
877 /* Mode Register - MR0 */
878 unsigned int dll_on; /* DLL control for precharge PD, 0=off, 1=on */
York Sunbad82092012-08-17 08:22:38 +0000879 unsigned int wr = 0; /* Write Recovery */
Dave Liu4be87b22009-03-14 12:48:30 +0800880 unsigned int dll_rst; /* DLL Reset */
881 unsigned int mode; /* Normal=0 or Test=1 */
882 unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
883 /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
884 unsigned int bt;
885 unsigned int bl; /* BL: Burst Length */
886
887 unsigned int wr_mclk;
York Sun3673f2c2011-03-02 14:24:11 -0800888 /*
889 * DDR_SDRAM_MODE doesn't support 9,11,13,15
890 * Please refer JEDEC Standard No. 79-3E for Mode Register MR0
891 * for this table
892 */
893 static const u8 wr_table[] = {1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 0, 0};
Dave Liu4be87b22009-03-14 12:48:30 +0800894
895 const unsigned int mclk_ps = get_memory_clk_period_ps();
York Sunba0c2eb2011-01-10 12:03:00 +0000896 int i;
Dave Liu4be87b22009-03-14 12:48:30 +0800897
Dave Liu4be87b22009-03-14 12:48:30 +0800898 if (popts->rtt_override)
899 rtt = popts->rtt_override_value;
York Sunba0c2eb2011-01-10 12:03:00 +0000900 else
901 rtt = popts->cs_local_opts[0].odt_rtt_norm;
Dave Liu4be87b22009-03-14 12:48:30 +0800902
903 if (additive_latency == (cas_latency - 1))
904 al = 1;
905 if (additive_latency == (cas_latency - 2))
906 al = 2;
907
York Sunba0c2eb2011-01-10 12:03:00 +0000908 if (popts->quad_rank_present)
909 dic = 1; /* output driver impedance 240/7 ohm */
910
Dave Liu4be87b22009-03-14 12:48:30 +0800911 /*
912 * The esdmode value will also be used for writing
913 * MR1 during write leveling for DDR3, although the
914 * bits specifically related to the write leveling
915 * scheme will be handled automatically by the DDR
916 * controller. so we set the wrlvl_en = 0 here.
917 */
918 esdmode = (0
919 | ((qoff & 0x1) << 12)
920 | ((tdqs_en & 0x1) << 11)
Kumar Gala14f2eb12009-09-10 14:54:55 -0500921 | ((rtt & 0x4) << 7) /* rtt field is split */
Dave Liu4be87b22009-03-14 12:48:30 +0800922 | ((wrlvl_en & 0x1) << 7)
Kumar Gala14f2eb12009-09-10 14:54:55 -0500923 | ((rtt & 0x2) << 5) /* rtt field is split */
924 | ((dic & 0x2) << 4) /* DIC field is split */
Dave Liu4be87b22009-03-14 12:48:30 +0800925 | ((al & 0x3) << 3)
Kumar Gala14f2eb12009-09-10 14:54:55 -0500926 | ((rtt & 0x1) << 2) /* rtt field is split */
Dave Liu4be87b22009-03-14 12:48:30 +0800927 | ((dic & 0x1) << 1) /* DIC field is split */
928 | ((dll_en & 0x1) << 0)
929 );
930
931 /*
932 * DLL control for precharge PD
933 * 0=slow exit DLL off (tXPDLL)
934 * 1=fast exit DLL on (tXP)
935 */
936 dll_on = 1;
York Sun3673f2c2011-03-02 14:24:11 -0800937
Dave Liu4be87b22009-03-14 12:48:30 +0800938 wr_mclk = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps;
York Sunbad82092012-08-17 08:22:38 +0000939 if (wr_mclk <= 16) {
940 wr = wr_table[wr_mclk - 5];
941 } else {
942 printf("Error: unsupported write recovery for mode register "
943 "wr_mclk = %d\n", wr_mclk);
944 }
York Sun3673f2c2011-03-02 14:24:11 -0800945
Dave Liu4be87b22009-03-14 12:48:30 +0800946 dll_rst = 0; /* dll no reset */
947 mode = 0; /* normal mode */
948
949 /* look up table to get the cas latency bits */
York Sunbad82092012-08-17 08:22:38 +0000950 if (cas_latency >= 5 && cas_latency <= 16) {
951 unsigned char cas_latency_table[] = {
Dave Liu4be87b22009-03-14 12:48:30 +0800952 0x2, /* 5 clocks */
953 0x4, /* 6 clocks */
954 0x6, /* 7 clocks */
955 0x8, /* 8 clocks */
956 0xa, /* 9 clocks */
957 0xc, /* 10 clocks */
York Sunbad82092012-08-17 08:22:38 +0000958 0xe, /* 11 clocks */
959 0x1, /* 12 clocks */
960 0x3, /* 13 clocks */
961 0x5, /* 14 clocks */
962 0x7, /* 15 clocks */
963 0x9, /* 16 clocks */
Dave Liu4be87b22009-03-14 12:48:30 +0800964 };
965 caslat = cas_latency_table[cas_latency - 5];
York Sunbad82092012-08-17 08:22:38 +0000966 } else {
967 printf("Error: unsupported cas latency for mode register\n");
Dave Liu4be87b22009-03-14 12:48:30 +0800968 }
York Sunbad82092012-08-17 08:22:38 +0000969
Dave Liu4be87b22009-03-14 12:48:30 +0800970 bt = 0; /* Nibble sequential */
971
972 switch (popts->burst_length) {
973 case DDR_BL8:
974 bl = 0;
975 break;
976 case DDR_OTF:
977 bl = 1;
978 break;
979 case DDR_BC4:
980 bl = 2;
981 break;
982 default:
983 printf("Error: invalid burst length of %u specified. "
984 " Defaulting to on-the-fly BC4 or BL8 beats.\n",
985 popts->burst_length);
986 bl = 1;
987 break;
988 }
989
990 sdmode = (0
991 | ((dll_on & 0x1) << 12)
992 | ((wr & 0x7) << 9)
993 | ((dll_rst & 0x1) << 8)
994 | ((mode & 0x1) << 7)
995 | (((caslat >> 1) & 0x7) << 4)
996 | ((bt & 0x1) << 3)
York Sunbad82092012-08-17 08:22:38 +0000997 | ((caslat & 1) << 2)
Dave Liu4be87b22009-03-14 12:48:30 +0800998 | ((bl & 0x3) << 0)
999 );
1000
1001 ddr->ddr_sdram_mode = (0
1002 | ((esdmode & 0xFFFF) << 16)
1003 | ((sdmode & 0xFFFF) << 0)
1004 );
1005
1006 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
York Sunba0c2eb2011-01-10 12:03:00 +00001007
1008 if (unq_mrs_en) { /* unique mode registers are supported */
Kumar Galad5bbe662011-11-09 10:05:10 -06001009 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Sunba0c2eb2011-01-10 12:03:00 +00001010 if (popts->rtt_override)
1011 rtt = popts->rtt_override_value;
1012 else
1013 rtt = popts->cs_local_opts[i].odt_rtt_norm;
1014
1015 esdmode &= 0xFDBB; /* clear bit 9,6,2 */
1016 esdmode |= (0
1017 | ((rtt & 0x4) << 7) /* rtt field is split */
1018 | ((rtt & 0x2) << 5) /* rtt field is split */
1019 | ((rtt & 0x1) << 2) /* rtt field is split */
1020 );
1021 switch (i) {
1022 case 1:
1023 ddr->ddr_sdram_mode_3 = (0
1024 | ((esdmode & 0xFFFF) << 16)
1025 | ((sdmode & 0xFFFF) << 0)
1026 );
1027 break;
1028 case 2:
1029 ddr->ddr_sdram_mode_5 = (0
1030 | ((esdmode & 0xFFFF) << 16)
1031 | ((sdmode & 0xFFFF) << 0)
1032 );
1033 break;
1034 case 3:
1035 ddr->ddr_sdram_mode_7 = (0
1036 | ((esdmode & 0xFFFF) << 16)
1037 | ((sdmode & 0xFFFF) << 0)
1038 );
1039 break;
1040 }
1041 }
1042 debug("FSLDDR: ddr_sdram_mode_3 = 0x%08x\n",
1043 ddr->ddr_sdram_mode_3);
1044 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1045 ddr->ddr_sdram_mode_5);
1046 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1047 ddr->ddr_sdram_mode_5);
1048 }
Dave Liu4be87b22009-03-14 12:48:30 +08001049}
1050
1051#else /* !CONFIG_FSL_DDR3 */
1052
1053/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
1054static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
1055 const memctl_options_t *popts,
1056 const common_timing_params_t *common_dimm,
1057 unsigned int cas_latency,
York Sunba0c2eb2011-01-10 12:03:00 +00001058 unsigned int additive_latency,
1059 const unsigned int unq_mrs_en)
Dave Liu4be87b22009-03-14 12:48:30 +08001060{
1061 unsigned short esdmode; /* Extended SDRAM mode */
1062 unsigned short sdmode; /* SDRAM mode */
1063
Kumar Gala124b0822008-08-26 15:01:29 -05001064 /*
1065 * FIXME: This ought to be pre-calculated in a
1066 * technology-specific routine,
1067 * e.g. compute_DDR2_mode_register(), and then the
1068 * sdmode and esdmode passed in as part of common_dimm.
1069 */
1070
1071 /* Extended Mode Register */
1072 unsigned int mrs = 0; /* Mode Register Set */
1073 unsigned int outputs = 0; /* 0=Enabled, 1=Disabled */
1074 unsigned int rdqs_en = 0; /* RDQS Enable: 0=no, 1=yes */
1075 unsigned int dqs_en = 0; /* DQS# Enable: 0=enable, 1=disable */
1076 unsigned int ocd = 0; /* 0x0=OCD not supported,
1077 0x7=OCD default state */
1078 unsigned int rtt;
1079 unsigned int al; /* Posted CAS# additive latency (AL) */
1080 unsigned int ods = 0; /* Output Drive Strength:
1081 0 = Full strength (18ohm)
1082 1 = Reduced strength (4ohm) */
1083 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
1084 1=Disable (Test/Debug) */
1085
1086 /* Mode Register (MR) */
1087 unsigned int mr; /* Mode Register Definition */
1088 unsigned int pd; /* Power-Down Mode */
1089 unsigned int wr; /* Write Recovery */
1090 unsigned int dll_res; /* DLL Reset */
1091 unsigned int mode; /* Normal=0 or Test=1 */
Kumar Gala35ad58d2008-09-05 14:40:29 -05001092 unsigned int caslat = 0;/* CAS# latency */
Kumar Gala124b0822008-08-26 15:01:29 -05001093 /* BT: Burst Type (0=Sequential, 1=Interleaved) */
1094 unsigned int bt;
1095 unsigned int bl; /* BL: Burst Length */
1096
1097#if defined(CONFIG_FSL_DDR2)
1098 const unsigned int mclk_ps = get_memory_clk_period_ps();
1099#endif
York Sun15f874a2011-08-26 11:32:40 -07001100 dqs_en = !popts->DQS_config;
Kumar Gala124b0822008-08-26 15:01:29 -05001101 rtt = fsl_ddr_get_rtt();
1102
1103 al = additive_latency;
1104
1105 esdmode = (0
1106 | ((mrs & 0x3) << 14)
1107 | ((outputs & 0x1) << 12)
1108 | ((rdqs_en & 0x1) << 11)
1109 | ((dqs_en & 0x1) << 10)
1110 | ((ocd & 0x7) << 7)
1111 | ((rtt & 0x2) << 5) /* rtt field is split */
1112 | ((al & 0x7) << 3)
1113 | ((rtt & 0x1) << 2) /* rtt field is split */
1114 | ((ods & 0x1) << 1)
1115 | ((dll_en & 0x1) << 0)
1116 );
1117
1118 mr = 0; /* FIXME: CHECKME */
1119
1120 /*
1121 * 0 = Fast Exit (Normal)
1122 * 1 = Slow Exit (Low Power)
1123 */
1124 pd = 0;
1125
1126#if defined(CONFIG_FSL_DDR1)
1127 wr = 0; /* Historical */
1128#elif defined(CONFIG_FSL_DDR2)
1129 wr = (common_dimm->tWR_ps + mclk_ps - 1) / mclk_ps - 1;
Kumar Gala124b0822008-08-26 15:01:29 -05001130#endif
1131 dll_res = 0;
1132 mode = 0;
1133
1134#if defined(CONFIG_FSL_DDR1)
1135 if (1 <= cas_latency && cas_latency <= 4) {
1136 unsigned char mode_caslat_table[4] = {
1137 0x5, /* 1.5 clocks */
1138 0x2, /* 2.0 clocks */
1139 0x6, /* 2.5 clocks */
1140 0x3 /* 3.0 clocks */
1141 };
Kumar Gala35ad58d2008-09-05 14:40:29 -05001142 caslat = mode_caslat_table[cas_latency - 1];
1143 } else {
1144 printf("Warning: unknown cas_latency %d\n", cas_latency);
Kumar Gala124b0822008-08-26 15:01:29 -05001145 }
1146#elif defined(CONFIG_FSL_DDR2)
1147 caslat = cas_latency;
Kumar Gala124b0822008-08-26 15:01:29 -05001148#endif
1149 bt = 0;
1150
1151 switch (popts->burst_length) {
Dave Liu4be87b22009-03-14 12:48:30 +08001152 case DDR_BL4:
Kumar Gala124b0822008-08-26 15:01:29 -05001153 bl = 2;
1154 break;
Dave Liu4be87b22009-03-14 12:48:30 +08001155 case DDR_BL8:
Kumar Gala124b0822008-08-26 15:01:29 -05001156 bl = 3;
1157 break;
1158 default:
1159 printf("Error: invalid burst length of %u specified. "
1160 " Defaulting to 4 beats.\n",
1161 popts->burst_length);
1162 bl = 2;
1163 break;
1164 }
1165
1166 sdmode = (0
1167 | ((mr & 0x3) << 14)
1168 | ((pd & 0x1) << 12)
1169 | ((wr & 0x7) << 9)
1170 | ((dll_res & 0x1) << 8)
1171 | ((mode & 0x1) << 7)
1172 | ((caslat & 0x7) << 4)
1173 | ((bt & 0x1) << 3)
1174 | ((bl & 0x7) << 0)
1175 );
1176
1177 ddr->ddr_sdram_mode = (0
1178 | ((esdmode & 0xFFFF) << 16)
1179 | ((sdmode & 0xFFFF) << 0)
1180 );
Haiying Wangd90e0402008-10-03 12:37:26 -04001181 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
Kumar Gala124b0822008-08-26 15:01:29 -05001182}
Dave Liu4be87b22009-03-14 12:48:30 +08001183#endif
Kumar Gala124b0822008-08-26 15:01:29 -05001184
1185/* DDR SDRAM Data Initialization (DDR_DATA_INIT) */
1186static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr)
1187{
1188 unsigned int init_value; /* Initialization value */
1189
Anatolij Gustschin021b7ae2013-01-21 23:50:27 +00001190#ifdef CONFIG_MEM_INIT_VALUE
1191 init_value = CONFIG_MEM_INIT_VALUE;
1192#else
Kumar Gala124b0822008-08-26 15:01:29 -05001193 init_value = 0xDEADBEEF;
Anatolij Gustschin021b7ae2013-01-21 23:50:27 +00001194#endif
Kumar Gala124b0822008-08-26 15:01:29 -05001195 ddr->ddr_data_init = init_value;
1196}
1197
1198/*
1199 * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL)
1200 * The old controller on the 8540/60 doesn't have this register.
1201 * Hope it's OK to set it (to 0) anyway.
1202 */
1203static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
1204 const memctl_options_t *popts)
1205{
1206 unsigned int clk_adjust; /* Clock adjust */
1207
1208 clk_adjust = popts->clk_adjust;
1209 ddr->ddr_sdram_clk_cntl = (clk_adjust & 0xF) << 23;
yorkde879322010-07-02 22:25:55 +00001210 debug("FSLDDR: clk_cntl = 0x%08x\n", ddr->ddr_sdram_clk_cntl);
Kumar Gala124b0822008-08-26 15:01:29 -05001211}
1212
1213/* DDR Initialization Address (DDR_INIT_ADDR) */
1214static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
1215{
1216 unsigned int init_addr = 0; /* Initialization address */
1217
1218 ddr->ddr_init_addr = init_addr;
1219}
1220
1221/* DDR Initialization Address (DDR_INIT_EXT_ADDR) */
1222static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
1223{
1224 unsigned int uia = 0; /* Use initialization address */
1225 unsigned int init_ext_addr = 0; /* Initialization address */
1226
1227 ddr->ddr_init_ext_addr = (0
1228 | ((uia & 0x1) << 31)
1229 | (init_ext_addr & 0xF)
1230 );
1231}
1232
1233/* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */
Dave Liu3525e1a2010-03-05 12:22:00 +08001234static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr,
1235 const memctl_options_t *popts)
Kumar Gala124b0822008-08-26 15:01:29 -05001236{
1237 unsigned int rwt = 0; /* Read-to-write turnaround for same CS */
1238 unsigned int wrt = 0; /* Write-to-read turnaround for same CS */
1239 unsigned int rrt = 0; /* Read-to-read turnaround for same CS */
1240 unsigned int wwt = 0; /* Write-to-write turnaround for same CS */
1241 unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */
1242
Dave Liu4be87b22009-03-14 12:48:30 +08001243#if defined(CONFIG_FSL_DDR3)
Dave Liu3525e1a2010-03-05 12:22:00 +08001244 if (popts->burst_length == DDR_BL8) {
1245 /* We set BL/2 for fixed BL8 */
1246 rrt = 0; /* BL/2 clocks */
1247 wwt = 0; /* BL/2 clocks */
1248 } else {
1249 /* We need to set BL/2 + 2 to BC4 and OTF */
1250 rrt = 2; /* BL/2 + 2 clocks */
1251 wwt = 2; /* BL/2 + 2 clocks */
1252 }
Dave Liu4be87b22009-03-14 12:48:30 +08001253 dll_lock = 1; /* tDLLK = 512 clocks from spec */
1254#endif
Kumar Gala124b0822008-08-26 15:01:29 -05001255 ddr->timing_cfg_4 = (0
1256 | ((rwt & 0xf) << 28)
1257 | ((wrt & 0xf) << 24)
1258 | ((rrt & 0xf) << 20)
1259 | ((wwt & 0xf) << 16)
1260 | (dll_lock & 0x3)
1261 );
Haiying Wangd90e0402008-10-03 12:37:26 -04001262 debug("FSLDDR: timing_cfg_4 = 0x%08x\n", ddr->timing_cfg_4);
Kumar Gala124b0822008-08-26 15:01:29 -05001263}
1264
1265/* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */
York Sunba0c2eb2011-01-10 12:03:00 +00001266static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr, unsigned int cas_latency)
Kumar Gala124b0822008-08-26 15:01:29 -05001267{
1268 unsigned int rodt_on = 0; /* Read to ODT on */
1269 unsigned int rodt_off = 0; /* Read to ODT off */
1270 unsigned int wodt_on = 0; /* Write to ODT on */
1271 unsigned int wodt_off = 0; /* Write to ODT off */
1272
Dave Liu4be87b22009-03-14 12:48:30 +08001273#if defined(CONFIG_FSL_DDR3)
York Sunba0c2eb2011-01-10 12:03:00 +00001274 /* rodt_on = timing_cfg_1[caslat] - timing_cfg_2[wrlat] + 1 */
1275 rodt_on = cas_latency - ((ddr->timing_cfg_2 & 0x00780000) >> 19) + 1;
Dave Liu4be87b22009-03-14 12:48:30 +08001276 rodt_off = 4; /* 4 clocks */
york1714e492010-07-02 22:25:56 +00001277 wodt_on = 1; /* 1 clocks */
Dave Liu4be87b22009-03-14 12:48:30 +08001278 wodt_off = 4; /* 4 clocks */
1279#endif
1280
Kumar Gala124b0822008-08-26 15:01:29 -05001281 ddr->timing_cfg_5 = (0
Dave Liu4758d532008-11-21 16:31:29 +08001282 | ((rodt_on & 0x1f) << 24)
1283 | ((rodt_off & 0x7) << 20)
1284 | ((wodt_on & 0x1f) << 12)
1285 | ((wodt_off & 0x7) << 8)
Kumar Gala124b0822008-08-26 15:01:29 -05001286 );
Haiying Wangd90e0402008-10-03 12:37:26 -04001287 debug("FSLDDR: timing_cfg_5 = 0x%08x\n", ddr->timing_cfg_5);
Kumar Gala124b0822008-08-26 15:01:29 -05001288}
1289
1290/* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */
Dave Liu4be87b22009-03-14 12:48:30 +08001291static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int zq_en)
Kumar Gala124b0822008-08-26 15:01:29 -05001292{
Kumar Gala124b0822008-08-26 15:01:29 -05001293 unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */
1294 /* Normal Operation Full Calibration Time (tZQoper) */
1295 unsigned int zqoper = 0;
1296 /* Normal Operation Short Calibration Time (tZQCS) */
1297 unsigned int zqcs = 0;
1298
Dave Liu4be87b22009-03-14 12:48:30 +08001299 if (zq_en) {
1300 zqinit = 9; /* 512 clocks */
1301 zqoper = 8; /* 256 clocks */
1302 zqcs = 6; /* 64 clocks */
1303 }
1304
Kumar Gala124b0822008-08-26 15:01:29 -05001305 ddr->ddr_zq_cntl = (0
1306 | ((zq_en & 0x1) << 31)
1307 | ((zqinit & 0xF) << 24)
1308 | ((zqoper & 0xF) << 16)
1309 | ((zqcs & 0xF) << 8)
1310 );
York Sunba0c2eb2011-01-10 12:03:00 +00001311 debug("FSLDDR: zq_cntl = 0x%08x\n", ddr->ddr_zq_cntl);
Kumar Gala124b0822008-08-26 15:01:29 -05001312}
1313
1314/* DDR Write Leveling Control (DDR_WRLVL_CNTL) */
Dave Liu64ee7df2009-12-16 10:24:37 -06001315static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int wrlvl_en,
1316 const memctl_options_t *popts)
Kumar Gala124b0822008-08-26 15:01:29 -05001317{
Kumar Gala124b0822008-08-26 15:01:29 -05001318 /*
1319 * First DQS pulse rising edge after margining mode
1320 * is programmed (tWL_MRD)
1321 */
1322 unsigned int wrlvl_mrd = 0;
1323 /* ODT delay after margining mode is programmed (tWL_ODTEN) */
1324 unsigned int wrlvl_odten = 0;
1325 /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */
1326 unsigned int wrlvl_dqsen = 0;
1327 /* WRLVL_SMPL: Write leveling sample time */
1328 unsigned int wrlvl_smpl = 0;
1329 /* WRLVL_WLR: Write leveling repeition time */
1330 unsigned int wrlvl_wlr = 0;
1331 /* WRLVL_START: Write leveling start time */
1332 unsigned int wrlvl_start = 0;
1333
Dave Liu4be87b22009-03-14 12:48:30 +08001334 /* suggest enable write leveling for DDR3 due to fly-by topology */
1335 if (wrlvl_en) {
1336 /* tWL_MRD min = 40 nCK, we set it 64 */
1337 wrlvl_mrd = 0x6;
1338 /* tWL_ODTEN 128 */
1339 wrlvl_odten = 0x7;
1340 /* tWL_DQSEN min = 25 nCK, we set it 32 */
1341 wrlvl_dqsen = 0x5;
1342 /*
Dave Liu64ee7df2009-12-16 10:24:37 -06001343 * Write leveling sample time at least need 6 clocks
1344 * higher than tWLO to allow enough time for progagation
1345 * delay and sampling the prime data bits.
Dave Liu4be87b22009-03-14 12:48:30 +08001346 */
1347 wrlvl_smpl = 0xf;
1348 /*
1349 * Write leveling repetition time
1350 * at least tWLO + 6 clocks clocks
york1714e492010-07-02 22:25:56 +00001351 * we set it 64
Dave Liu4be87b22009-03-14 12:48:30 +08001352 */
york1714e492010-07-02 22:25:56 +00001353 wrlvl_wlr = 0x6;
Dave Liu4be87b22009-03-14 12:48:30 +08001354 /*
1355 * Write leveling start time
1356 * The value use for the DQS_ADJUST for the first sample
York Sunba0c2eb2011-01-10 12:03:00 +00001357 * when write leveling is enabled. It probably needs to be
1358 * overriden per platform.
Dave Liu4be87b22009-03-14 12:48:30 +08001359 */
1360 wrlvl_start = 0x8;
Dave Liu64ee7df2009-12-16 10:24:37 -06001361 /*
1362 * Override the write leveling sample and start time
1363 * according to specific board
1364 */
1365 if (popts->wrlvl_override) {
1366 wrlvl_smpl = popts->wrlvl_sample;
1367 wrlvl_start = popts->wrlvl_start;
1368 }
Dave Liu4be87b22009-03-14 12:48:30 +08001369 }
1370
Kumar Gala124b0822008-08-26 15:01:29 -05001371 ddr->ddr_wrlvl_cntl = (0
1372 | ((wrlvl_en & 0x1) << 31)
1373 | ((wrlvl_mrd & 0x7) << 24)
1374 | ((wrlvl_odten & 0x7) << 20)
1375 | ((wrlvl_dqsen & 0x7) << 16)
1376 | ((wrlvl_smpl & 0xf) << 12)
1377 | ((wrlvl_wlr & 0x7) << 8)
Dave Liu4758d532008-11-21 16:31:29 +08001378 | ((wrlvl_start & 0x1F) << 0)
Kumar Gala124b0822008-08-26 15:01:29 -05001379 );
York Sunba0c2eb2011-01-10 12:03:00 +00001380 debug("FSLDDR: wrlvl_cntl = 0x%08x\n", ddr->ddr_wrlvl_cntl);
York Sun7d69ea32012-10-08 07:44:22 +00001381 ddr->ddr_wrlvl_cntl_2 = popts->wrlvl_ctl_2;
1382 debug("FSLDDR: wrlvl_cntl_2 = 0x%08x\n", ddr->ddr_wrlvl_cntl_2);
1383 ddr->ddr_wrlvl_cntl_3 = popts->wrlvl_ctl_3;
1384 debug("FSLDDR: wrlvl_cntl_3 = 0x%08x\n", ddr->ddr_wrlvl_cntl_3);
1385
Kumar Gala124b0822008-08-26 15:01:29 -05001386}
1387
1388/* DDR Self Refresh Counter (DDR_SR_CNTR) */
Dave Liu2aad0ae2008-11-21 16:31:35 +08001389static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr, unsigned int sr_it)
Kumar Gala124b0822008-08-26 15:01:29 -05001390{
Dave Liu2aad0ae2008-11-21 16:31:35 +08001391 /* Self Refresh Idle Threshold */
Kumar Gala124b0822008-08-26 15:01:29 -05001392 ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
1393}
1394
york42603722010-07-02 22:25:54 +00001395static void set_ddr_eor(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
1396{
1397 if (popts->addr_hash) {
1398 ddr->ddr_eor = 0x40000000; /* address hash enable */
Kumar Gala4513d762011-03-18 11:53:06 -05001399 puts("Address hashing enabled.\n");
york42603722010-07-02 22:25:54 +00001400 }
1401}
1402
York Sunba0c2eb2011-01-10 12:03:00 +00001403static void set_ddr_cdr1(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
1404{
1405 ddr->ddr_cdr1 = popts->ddr_cdr1;
1406 debug("FSLDDR: ddr_cdr1 = 0x%08x\n", ddr->ddr_cdr1);
1407}
1408
York Sun7d69ea32012-10-08 07:44:22 +00001409static void set_ddr_cdr2(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
1410{
1411 ddr->ddr_cdr2 = popts->ddr_cdr2;
1412 debug("FSLDDR: ddr_cdr2 = 0x%08x\n", ddr->ddr_cdr2);
1413}
1414
Kumar Gala124b0822008-08-26 15:01:29 -05001415unsigned int
1416check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
1417{
1418 unsigned int res = 0;
1419
1420 /*
1421 * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are
1422 * not set at the same time.
1423 */
1424 if (ddr->ddr_sdram_cfg & 0x10000000
1425 && ddr->ddr_sdram_cfg & 0x00008000) {
1426 printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
1427 " should not be set at the same time.\n");
1428 res++;
1429 }
1430
1431 return res;
1432}
1433
1434unsigned int
1435compute_fsl_memctl_config_regs(const memctl_options_t *popts,
1436 fsl_ddr_cfg_regs_t *ddr,
1437 const common_timing_params_t *common_dimm,
1438 const dimm_params_t *dimm_params,
Haiying Wang80ad4012010-12-01 10:35:31 -05001439 unsigned int dbw_cap_adj,
1440 unsigned int size_only)
Kumar Gala124b0822008-08-26 15:01:29 -05001441{
1442 unsigned int i;
1443 unsigned int cas_latency;
1444 unsigned int additive_latency;
Dave Liu2aad0ae2008-11-21 16:31:35 +08001445 unsigned int sr_it;
Dave Liu4be87b22009-03-14 12:48:30 +08001446 unsigned int zq_en;
1447 unsigned int wrlvl_en;
York Sunba0c2eb2011-01-10 12:03:00 +00001448 unsigned int ip_rev = 0;
1449 unsigned int unq_mrs_en = 0;
York Sun2927c5e2010-10-18 13:46:50 -07001450 int cs_en = 1;
Kumar Gala124b0822008-08-26 15:01:29 -05001451
1452 memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
1453
1454 if (common_dimm == NULL) {
1455 printf("Error: subset DIMM params struct null pointer\n");
1456 return 1;
1457 }
1458
1459 /*
1460 * Process overrides first.
1461 *
1462 * FIXME: somehow add dereated caslat to this
1463 */
1464 cas_latency = (popts->cas_latency_override)
1465 ? popts->cas_latency_override_value
1466 : common_dimm->lowest_common_SPD_caslat;
1467
1468 additive_latency = (popts->additive_latency_override)
1469 ? popts->additive_latency_override_value
1470 : common_dimm->additive_latency;
1471
Dave Liu2aad0ae2008-11-21 16:31:35 +08001472 sr_it = (popts->auto_self_refresh_en)
1473 ? popts->sr_it
1474 : 0;
Dave Liu4be87b22009-03-14 12:48:30 +08001475 /* ZQ calibration */
1476 zq_en = (popts->zq_en) ? 1 : 0;
1477 /* write leveling */
1478 wrlvl_en = (popts->wrlvl_en) ? 1 : 0;
Dave Liu2aad0ae2008-11-21 16:31:35 +08001479
Kumar Gala124b0822008-08-26 15:01:29 -05001480 /* Chip Select Memory Bounds (CSn_BNDS) */
1481 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Sune8dc17b2012-08-17 08:22:39 +00001482 unsigned long long ea, sa;
york93799ca2010-07-02 22:25:52 +00001483 unsigned int cs_per_dimm
1484 = CONFIG_CHIP_SELECTS_PER_CTRL / CONFIG_DIMM_SLOTS_PER_CTLR;
1485 unsigned int dimm_number
1486 = i / cs_per_dimm;
1487 unsigned long long rank_density
York Sune8dc17b2012-08-17 08:22:39 +00001488 = dimm_params[dimm_number].rank_density >> dbw_cap_adj;
Haiying Wang272b5962008-10-03 12:36:39 -04001489
york93799ca2010-07-02 22:25:52 +00001490 if (dimm_params[dimm_number].n_ranks == 0) {
Kumar Gala124b0822008-08-26 15:01:29 -05001491 debug("Skipping setup of CS%u "
yorkf4f93c62010-07-02 22:25:53 +00001492 "because n_ranks on DIMM %u is 0\n", i, dimm_number);
Kumar Gala124b0822008-08-26 15:01:29 -05001493 continue;
1494 }
York Sune8dc17b2012-08-17 08:22:39 +00001495 if (popts->memctl_interleaving) {
york93799ca2010-07-02 22:25:52 +00001496 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
York Sune8dc17b2012-08-17 08:22:39 +00001497 case FSL_DDR_CS0_CS1_CS2_CS3:
1498 break;
york93799ca2010-07-02 22:25:52 +00001499 case FSL_DDR_CS0_CS1:
1500 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
York Sun2927c5e2010-10-18 13:46:50 -07001501 if (i > 1)
1502 cs_en = 0;
york93799ca2010-07-02 22:25:52 +00001503 break;
1504 case FSL_DDR_CS2_CS3:
York Sune8dc17b2012-08-17 08:22:39 +00001505 default:
York Sun2927c5e2010-10-18 13:46:50 -07001506 if (i > 0)
1507 cs_en = 0;
york93799ca2010-07-02 22:25:52 +00001508 break;
york93799ca2010-07-02 22:25:52 +00001509 }
York Sune8dc17b2012-08-17 08:22:39 +00001510 sa = common_dimm->base_address;
York Sun98df4d12012-10-08 07:44:23 +00001511 ea = sa + common_dimm->total_mem - 1;
York Sune8dc17b2012-08-17 08:22:39 +00001512 } else if (!popts->memctl_interleaving) {
Kumar Gala124b0822008-08-26 15:01:29 -05001513 /*
1514 * If memory interleaving between controllers is NOT
1515 * enabled, the starting address for each memory
1516 * controller is distinct. However, because rank
1517 * interleaving is enabled, the starting and ending
1518 * addresses of the total memory on that memory
1519 * controller needs to be programmed into its
1520 * respective CS0_BNDS.
1521 */
Haiying Wang272b5962008-10-03 12:36:39 -04001522 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
1523 case FSL_DDR_CS0_CS1_CS2_CS3:
Haiying Wang272b5962008-10-03 12:36:39 -04001524 sa = common_dimm->base_address;
York Sun98df4d12012-10-08 07:44:23 +00001525 ea = sa + common_dimm->total_mem - 1;
Haiying Wang272b5962008-10-03 12:36:39 -04001526 break;
1527 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
York Sune8dc17b2012-08-17 08:22:39 +00001528 if ((i >= 2) && (dimm_number == 0)) {
york93799ca2010-07-02 22:25:52 +00001529 sa = dimm_params[dimm_number].base_address +
York Sune8dc17b2012-08-17 08:22:39 +00001530 2 * rank_density;
1531 ea = sa + 2 * rank_density - 1;
york93799ca2010-07-02 22:25:52 +00001532 } else {
1533 sa = dimm_params[dimm_number].base_address;
York Sune8dc17b2012-08-17 08:22:39 +00001534 ea = sa + 2 * rank_density - 1;
Haiying Wang272b5962008-10-03 12:36:39 -04001535 }
1536 break;
1537 case FSL_DDR_CS0_CS1:
york93799ca2010-07-02 22:25:52 +00001538 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
1539 sa = dimm_params[dimm_number].base_address;
York Sune8dc17b2012-08-17 08:22:39 +00001540 ea = sa + rank_density - 1;
1541 if (i != 1)
1542 sa += (i % cs_per_dimm) * rank_density;
1543 ea += (i % cs_per_dimm) * rank_density;
york93799ca2010-07-02 22:25:52 +00001544 } else {
1545 sa = 0;
1546 ea = 0;
1547 }
1548 if (i == 0)
York Sune8dc17b2012-08-17 08:22:39 +00001549 ea += rank_density;
Haiying Wang272b5962008-10-03 12:36:39 -04001550 break;
1551 case FSL_DDR_CS2_CS3:
york93799ca2010-07-02 22:25:52 +00001552 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
1553 sa = dimm_params[dimm_number].base_address;
York Sune8dc17b2012-08-17 08:22:39 +00001554 ea = sa + rank_density - 1;
1555 if (i != 3)
1556 sa += (i % cs_per_dimm) * rank_density;
1557 ea += (i % cs_per_dimm) * rank_density;
york93799ca2010-07-02 22:25:52 +00001558 } else {
1559 sa = 0;
1560 ea = 0;
Haiying Wang272b5962008-10-03 12:36:39 -04001561 }
york93799ca2010-07-02 22:25:52 +00001562 if (i == 2)
1563 ea += (rank_density >> dbw_cap_adj);
Haiying Wang272b5962008-10-03 12:36:39 -04001564 break;
1565 default: /* No bank(chip-select) interleaving */
York Sune8dc17b2012-08-17 08:22:39 +00001566 sa = dimm_params[dimm_number].base_address;
1567 ea = sa + rank_density - 1;
1568 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
1569 sa += (i % cs_per_dimm) * rank_density;
1570 ea += (i % cs_per_dimm) * rank_density;
1571 } else {
1572 sa = 0;
1573 ea = 0;
1574 }
Haiying Wang272b5962008-10-03 12:36:39 -04001575 break;
1576 }
Kumar Gala124b0822008-08-26 15:01:29 -05001577 }
Kumar Gala124b0822008-08-26 15:01:29 -05001578
1579 sa >>= 24;
1580 ea >>= 24;
1581
York Sun98df4d12012-10-08 07:44:23 +00001582 if (cs_en) {
1583 ddr->cs[i].bnds = (0
1584 | ((sa & 0xFFF) << 16)/* starting address MSB */
1585 | ((ea & 0xFFF) << 0) /* ending address MSB */
1586 );
1587 } else {
York Sunc21a7392013-06-25 11:37:45 -07001588 /* setting bnds to 0xffffffff for inactive CS */
1589 ddr->cs[i].bnds = 0xffffffff;
York Sun98df4d12012-10-08 07:44:23 +00001590 }
Kumar Gala124b0822008-08-26 15:01:29 -05001591
Haiying Wangd90e0402008-10-03 12:37:26 -04001592 debug("FSLDDR: cs[%d]_bnds = 0x%08x\n", i, ddr->cs[i].bnds);
York Sun98df4d12012-10-08 07:44:23 +00001593 set_csn_config(dimm_number, i, ddr, popts, dimm_params);
1594 set_csn_config_2(i, ddr);
Kumar Gala124b0822008-08-26 15:01:29 -05001595 }
1596
Haiying Wang80ad4012010-12-01 10:35:31 -05001597 /*
1598 * In the case we only need to compute the ddr sdram size, we only need
1599 * to set csn registers, so return from here.
1600 */
1601 if (size_only)
1602 return 0;
1603
york42603722010-07-02 22:25:54 +00001604 set_ddr_eor(ddr, popts);
1605
Dave Liu4be87b22009-03-14 12:48:30 +08001606#if !defined(CONFIG_FSL_DDR1)
York Sun98df4d12012-10-08 07:44:23 +00001607 set_timing_cfg_0(ddr, popts, dimm_params);
Kumar Gala124b0822008-08-26 15:01:29 -05001608#endif
1609
York Suncd077cf2012-08-17 08:22:40 +00001610 set_timing_cfg_3(ddr, popts, common_dimm, cas_latency);
Dave Liu4be87b22009-03-14 12:48:30 +08001611 set_timing_cfg_1(ddr, popts, common_dimm, cas_latency);
Kumar Gala124b0822008-08-26 15:01:29 -05001612 set_timing_cfg_2(ddr, popts, common_dimm,
1613 cas_latency, additive_latency);
1614
York Sunba0c2eb2011-01-10 12:03:00 +00001615 set_ddr_cdr1(ddr, popts);
York Sun7d69ea32012-10-08 07:44:22 +00001616 set_ddr_cdr2(ddr, popts);
Kumar Gala124b0822008-08-26 15:01:29 -05001617 set_ddr_sdram_cfg(ddr, popts, common_dimm);
York Sunba0c2eb2011-01-10 12:03:00 +00001618 ip_rev = fsl_ddr_get_version();
1619 if (ip_rev > 0x40400)
1620 unq_mrs_en = 1;
Kumar Gala124b0822008-08-26 15:01:29 -05001621
York Sunba0c2eb2011-01-10 12:03:00 +00001622 set_ddr_sdram_cfg_2(ddr, popts, unq_mrs_en);
Kumar Gala124b0822008-08-26 15:01:29 -05001623 set_ddr_sdram_mode(ddr, popts, common_dimm,
York Sunba0c2eb2011-01-10 12:03:00 +00001624 cas_latency, additive_latency, unq_mrs_en);
1625 set_ddr_sdram_mode_2(ddr, popts, unq_mrs_en);
Kumar Gala124b0822008-08-26 15:01:29 -05001626 set_ddr_sdram_interval(ddr, popts, common_dimm);
1627 set_ddr_data_init(ddr);
1628 set_ddr_sdram_clk_cntl(ddr, popts);
1629 set_ddr_init_addr(ddr);
1630 set_ddr_init_ext_addr(ddr);
Dave Liu3525e1a2010-03-05 12:22:00 +08001631 set_timing_cfg_4(ddr, popts);
York Sunba0c2eb2011-01-10 12:03:00 +00001632 set_timing_cfg_5(ddr, cas_latency);
Kumar Gala124b0822008-08-26 15:01:29 -05001633
Dave Liu4be87b22009-03-14 12:48:30 +08001634 set_ddr_zq_cntl(ddr, zq_en);
Dave Liu64ee7df2009-12-16 10:24:37 -06001635 set_ddr_wrlvl_cntl(ddr, wrlvl_en, popts);
Kumar Gala124b0822008-08-26 15:01:29 -05001636
Dave Liu2aad0ae2008-11-21 16:31:35 +08001637 set_ddr_sr_cntr(ddr, sr_it);
Kumar Gala124b0822008-08-26 15:01:29 -05001638
York Sunba0c2eb2011-01-10 12:03:00 +00001639 set_ddr_sdram_rcw(ddr, popts, common_dimm);
Kumar Gala124b0822008-08-26 15:01:29 -05001640
York Sun972cc402013-06-25 11:37:41 -07001641#ifdef CONFIG_SYS_FSL_DDR_EMU
1642 /* disble DDR training for emulator */
1643 ddr->debug[2] = 0x00000400;
1644 ddr->debug[4] = 0xff800000;
1645#endif
Kumar Gala124b0822008-08-26 15:01:29 -05001646 return check_fsl_memctl_config_regs(ddr);
1647}