blob: d7333ba3dd41e7116cc8cae104817ea69c3cc781 [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
Priyanka Jain4a717412013-09-25 10:41:19 +0530356 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;
York Suncd077cf2012-08-17 08:22:40 +0000359 ext_caslat = (2 * cas_latency - 1) >> 4;
Priyanka Jain4a717412013-09-25 10:41:19 +0530360 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 */
Priyanka Jain4a717412013-09-25 10:41:19 +0530362 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)
James Yang26681f52013-07-22 09:35:26 -0700367 | ((ext_acttopre & 0x3) << 24)
York Suncd077cf2012-08-17 08:22:40 +0000368 | ((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
Priyanka Jain4a717412013-09-25 10:41:19 +0530403 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);
Kumar Gala124b0822008-08-26 15:01:29 -0500406
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
Priyanka Jain4a717412013-09-25 10:41:19 +0530436 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];
Priyanka Jain4a717412013-09-25 10:41:19 +0530443 if (popts->otf_burst_chop_en)
Dave Liu4be87b22009-03-14 12:48:30 +0800444 wrrec_mclk += 2;
445
Priyanka Jain4a717412013-09-25 10:41:19 +0530446 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
Priyanka Jain4a717412013-09-25 10:41:19 +0530454 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
Priyanka Jain4a717412013-09-25 10:41:19 +0530465 if (popts->otf_burst_chop_en)
Dave Liu4be87b22009-03-14 12:48:30 +0800466 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
Priyanka Jain4a717412013-09-25 10:41:19 +0530521 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;
Priyanka Jain4a717412013-09-25 10:41:19 +0530534 if (popts->otf_burst_chop_en)
Dave Liu4be87b22009-03-14 12:48:30 +0800535 rd_to_pre += 2; /* according to UM */
536
Kumar Gala124b0822008-08-26 15:01:29 -0500537 wr_data_delay = popts->write_data_delay;
Priyanka Jain4a717412013-09-25 10:41:19 +0530538 cke_pls = picos_to_mclk(popts->tcke_clock_pulse_width_ps);
539 four_act = picos_to_mclk(popts->tfaw_window_four_activates_ps);
Kumar Gala124b0822008-08-26 15:01:29 -0500540
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{
Priyanka Jain4a717412013-09-25 10:41:19 +0530558 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 */
Priyanka Jain4a717412013-09-25 10:41:19 +0530602 unsigned int threet_en; /* Enable 3T timing */
603 unsigned int twot_en; /* Enable 2T timing */
Kumar Gala124b0822008-08-26 15:01:29 -0500604 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;
Priyanka Jain4a717412013-09-25 10:41:19 +0530613 if (common_dimm->all_dimms_ecc_capable) {
Kumar Gala124b0822008-08-26 15:01:29 -0500614 /* Allow setting of ECC only if all DIMMs are ECC. */
Priyanka Jain4a717412013-09-25 10:41:19 +0530615 ecc_en = popts->ecc_mode;
Kumar Gala124b0822008-08-26 15:01:29 -0500616 } else {
617 ecc_en = 0;
618 }
619
Priyanka Jain4a717412013-09-25 10:41:19 +0530620 if (common_dimm->all_dimms_registered &&
621 !common_dimm->all_dimms_unbuffered) {
York Sunba0c2eb2011-01-10 12:03:00 +0000622 rd_en = 1;
Priyanka Jain4a717412013-09-25 10:41:19 +0530623 twot_en = 0;
York Sunba0c2eb2011-01-10 12:03:00 +0000624 } else {
625 rd_en = 0;
Priyanka Jain4a717412013-09-25 10:41:19 +0530626 twot_en = popts->twot_en;
York Sunba0c2eb2011-01-10 12:03:00 +0000627 }
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
Priyanka Jain4a717412013-09-25 10:41:19 +0530646 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)
Priyanka Jain4a717412013-09-25 10:41:19 +0530660 | ((threet_en & 0x1) << 16)
661 | ((twot_en & 0x1) << 15)
Kumar Gala124b0822008-08-26 15:01:29 -0500662 | ((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 */
York Sun4889c982013-06-25 11:37:47 -0700684 unsigned int x4_en = 0; /* x4 DRAM enable */
Kumar Gala124b0822008-08-26 15:01:29 -0500685 unsigned int obc_cfg; /* On-The-Fly Burst Chop Cfg */
686 unsigned int ap_en; /* Address Parity Enable */
687 unsigned int d_init; /* DRAM data initialization */
688 unsigned int rcw_en = 0; /* Register Control Word Enable */
689 unsigned int md_en = 0; /* Mirrored DIMM Enable */
yorkf4f93c62010-07-02 22:25:53 +0000690 unsigned int qd_en = 0; /* quad-rank DIMM Enable */
York Sun15f874a2011-08-26 11:32:40 -0700691 int i;
Kumar Gala124b0822008-08-26 15:01:29 -0500692
693 dll_rst_dis = 1; /* Make this configurable */
Priyanka Jain4a717412013-09-25 10:41:19 +0530694 dqs_cfg = popts->dqs_config;
York Sun15f874a2011-08-26 11:32:40 -0700695 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
696 if (popts->cs_local_opts[i].odt_rd_cfg
697 || popts->cs_local_opts[i].odt_wr_cfg) {
698 odt_cfg = SDRAM_CFG2_ODT_ONLY_READ;
699 break;
700 }
Kumar Gala124b0822008-08-26 15:01:29 -0500701 }
702
703 num_pr = 1; /* Make this configurable */
704
705 /*
706 * 8572 manual says
707 * {TIMING_CFG_1[PRETOACT]
708 * + [DDR_SDRAM_CFG_2[NUM_PR]
709 * * ({EXT_REFREC || REFREC} + 8 + 2)]}
710 * << DDR_SDRAM_INTERVAL[REFINT]
711 */
Dave Liu4be87b22009-03-14 12:48:30 +0800712#if defined(CONFIG_FSL_DDR3)
Priyanka Jain4a717412013-09-25 10:41:19 +0530713 obc_cfg = popts->otf_burst_chop_en;
Dave Liu4be87b22009-03-14 12:48:30 +0800714#else
715 obc_cfg = 0;
716#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500717
York Sun7d69ea32012-10-08 07:44:22 +0000718#if (CONFIG_SYS_FSL_DDR_VER >= FSL_DDR_VER_4_7)
719 slow = get_ddr_freq(0) < 1249000000;
720#endif
721
York Sunba0c2eb2011-01-10 12:03:00 +0000722 if (popts->registered_dimm_en) {
723 rcw_en = 1;
724 ap_en = popts->ap_en;
725 } else {
York Sunba0c2eb2011-01-10 12:03:00 +0000726 ap_en = 0;
727 }
Kumar Gala124b0822008-08-26 15:01:29 -0500728
York Sun4889c982013-06-25 11:37:47 -0700729 x4_en = popts->x4_en ? 1 : 0;
730
Kumar Gala124b0822008-08-26 15:01:29 -0500731#if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
732 /* Use the DDR controller to auto initialize memory. */
Priyanka Jain4a717412013-09-25 10:41:19 +0530733 d_init = popts->ecc_init_using_memctl;
Kumar Gala124b0822008-08-26 15:01:29 -0500734 ddr->ddr_data_init = CONFIG_MEM_INIT_VALUE;
735 debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init);
736#else
737 /* Memory will be initialized via DMA, or not at all. */
738 d_init = 0;
739#endif
740
Dave Liu4be87b22009-03-14 12:48:30 +0800741#if defined(CONFIG_FSL_DDR3)
742 md_en = popts->mirrored_dimm;
743#endif
yorkf4f93c62010-07-02 22:25:53 +0000744 qd_en = popts->quad_rank_present ? 1 : 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500745 ddr->ddr_sdram_cfg_2 = (0
746 | ((frc_sr & 0x1) << 31)
747 | ((sr_ie & 0x1) << 30)
748 | ((dll_rst_dis & 0x1) << 29)
749 | ((dqs_cfg & 0x3) << 26)
750 | ((odt_cfg & 0x3) << 21)
751 | ((num_pr & 0xf) << 12)
York Sun7d69ea32012-10-08 07:44:22 +0000752 | ((slow & 1) << 11)
York Sun4889c982013-06-25 11:37:47 -0700753 | (x4_en << 10)
yorkf4f93c62010-07-02 22:25:53 +0000754 | (qd_en << 9)
York Sunba0c2eb2011-01-10 12:03:00 +0000755 | (unq_mrs_en << 8)
Kumar Gala124b0822008-08-26 15:01:29 -0500756 | ((obc_cfg & 0x1) << 6)
757 | ((ap_en & 0x1) << 5)
758 | ((d_init & 0x1) << 4)
759 | ((rcw_en & 0x1) << 2)
760 | ((md_en & 0x1) << 0)
761 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400762 debug("FSLDDR: ddr_sdram_cfg_2 = 0x%08x\n", ddr->ddr_sdram_cfg_2);
Kumar Gala124b0822008-08-26 15:01:29 -0500763}
764
765/* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
Dave Liu2d0f1252009-12-16 10:24:38 -0600766static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr,
York Sunba0c2eb2011-01-10 12:03:00 +0000767 const memctl_options_t *popts,
768 const unsigned int unq_mrs_en)
Kumar Gala124b0822008-08-26 15:01:29 -0500769{
770 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
771 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
772
Dave Liu4be87b22009-03-14 12:48:30 +0800773#if defined(CONFIG_FSL_DDR3)
Kumar Gala65b5be22011-01-20 01:53:15 -0600774 int i;
Dave Liu2d0f1252009-12-16 10:24:38 -0600775 unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */
Dave Liu4be87b22009-03-14 12:48:30 +0800776 unsigned int srt = 0; /* self-refresh temerature, normal range */
777 unsigned int asr = 0; /* auto self-refresh disable */
778 unsigned int cwl = compute_cas_write_latency() - 5;
779 unsigned int pasr = 0; /* partial array self refresh disable */
780
Dave Liu2d0f1252009-12-16 10:24:38 -0600781 if (popts->rtt_override)
782 rtt_wr = popts->rtt_wr_override_value;
York Sunba0c2eb2011-01-10 12:03:00 +0000783 else
784 rtt_wr = popts->cs_local_opts[0].odt_rtt_wr;
Dave Liu4be87b22009-03-14 12:48:30 +0800785 esdmode2 = (0
786 | ((rtt_wr & 0x3) << 9)
787 | ((srt & 0x1) << 7)
788 | ((asr & 0x1) << 6)
789 | ((cwl & 0x7) << 3)
790 | ((pasr & 0x7) << 0));
791#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500792 ddr->ddr_sdram_mode_2 = (0
793 | ((esdmode2 & 0xFFFF) << 16)
794 | ((esdmode3 & 0xFFFF) << 0)
795 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400796 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
York Sunba0c2eb2011-01-10 12:03:00 +0000797
798#ifdef CONFIG_FSL_DDR3
799 if (unq_mrs_en) { /* unique mode registers are supported */
Kumar Galad5bbe662011-11-09 10:05:10 -0600800 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Sunba0c2eb2011-01-10 12:03:00 +0000801 if (popts->rtt_override)
802 rtt_wr = popts->rtt_wr_override_value;
803 else
804 rtt_wr = popts->cs_local_opts[i].odt_rtt_wr;
805
806 esdmode2 &= 0xF9FF; /* clear bit 10, 9 */
807 esdmode2 |= (rtt_wr & 0x3) << 9;
808 switch (i) {
809 case 1:
810 ddr->ddr_sdram_mode_4 = (0
811 | ((esdmode2 & 0xFFFF) << 16)
812 | ((esdmode3 & 0xFFFF) << 0)
813 );
814 break;
815 case 2:
816 ddr->ddr_sdram_mode_6 = (0
817 | ((esdmode2 & 0xFFFF) << 16)
818 | ((esdmode3 & 0xFFFF) << 0)
819 );
820 break;
821 case 3:
822 ddr->ddr_sdram_mode_8 = (0
823 | ((esdmode2 & 0xFFFF) << 16)
824 | ((esdmode3 & 0xFFFF) << 0)
825 );
826 break;
827 }
828 }
829 debug("FSLDDR: ddr_sdram_mode_4 = 0x%08x\n",
830 ddr->ddr_sdram_mode_4);
831 debug("FSLDDR: ddr_sdram_mode_6 = 0x%08x\n",
832 ddr->ddr_sdram_mode_6);
833 debug("FSLDDR: ddr_sdram_mode_8 = 0x%08x\n",
834 ddr->ddr_sdram_mode_8);
835 }
836#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500837}
838
839/* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */
840static void set_ddr_sdram_interval(fsl_ddr_cfg_regs_t *ddr,
841 const memctl_options_t *popts,
842 const common_timing_params_t *common_dimm)
843{
844 unsigned int refint; /* Refresh interval */
845 unsigned int bstopre; /* Precharge interval */
846
847 refint = picos_to_mclk(common_dimm->refresh_rate_ps);
848
849 bstopre = popts->bstopre;
850
851 /* refint field used 0x3FFF in earlier controllers */
852 ddr->ddr_sdram_interval = (0
853 | ((refint & 0xFFFF) << 16)
854 | ((bstopre & 0x3FFF) << 0)
855 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400856 debug("FSLDDR: ddr_sdram_interval = 0x%08x\n", ddr->ddr_sdram_interval);
Kumar Gala124b0822008-08-26 15:01:29 -0500857}
858
Dave Liu4be87b22009-03-14 12:48:30 +0800859#if defined(CONFIG_FSL_DDR3)
Kumar Gala124b0822008-08-26 15:01:29 -0500860/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
861static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
862 const memctl_options_t *popts,
863 const common_timing_params_t *common_dimm,
864 unsigned int cas_latency,
York Sunba0c2eb2011-01-10 12:03:00 +0000865 unsigned int additive_latency,
866 const unsigned int unq_mrs_en)
Kumar Gala124b0822008-08-26 15:01:29 -0500867{
868 unsigned short esdmode; /* Extended SDRAM mode */
869 unsigned short sdmode; /* SDRAM mode */
870
Dave Liu4be87b22009-03-14 12:48:30 +0800871 /* Mode Register - MR1 */
872 unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */
873 unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */
874 unsigned int rtt;
875 unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */
876 unsigned int al = 0; /* Posted CAS# additive latency (AL) */
York Sunba0c2eb2011-01-10 12:03:00 +0000877 unsigned int dic = 0; /* Output driver impedance, 40ohm */
Dave Liu4be87b22009-03-14 12:48:30 +0800878 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
879 1=Disable (Test/Debug) */
880
881 /* Mode Register - MR0 */
882 unsigned int dll_on; /* DLL control for precharge PD, 0=off, 1=on */
York Sunbad82092012-08-17 08:22:38 +0000883 unsigned int wr = 0; /* Write Recovery */
Dave Liu4be87b22009-03-14 12:48:30 +0800884 unsigned int dll_rst; /* DLL Reset */
885 unsigned int mode; /* Normal=0 or Test=1 */
886 unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
887 /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
888 unsigned int bt;
889 unsigned int bl; /* BL: Burst Length */
890
891 unsigned int wr_mclk;
York Sun3673f2c2011-03-02 14:24:11 -0800892 /*
893 * DDR_SDRAM_MODE doesn't support 9,11,13,15
894 * Please refer JEDEC Standard No. 79-3E for Mode Register MR0
895 * for this table
896 */
897 static const u8 wr_table[] = {1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 0, 0};
Dave Liu4be87b22009-03-14 12:48:30 +0800898
899 const unsigned int mclk_ps = get_memory_clk_period_ps();
York Sunba0c2eb2011-01-10 12:03:00 +0000900 int i;
Dave Liu4be87b22009-03-14 12:48:30 +0800901
Dave Liu4be87b22009-03-14 12:48:30 +0800902 if (popts->rtt_override)
903 rtt = popts->rtt_override_value;
York Sunba0c2eb2011-01-10 12:03:00 +0000904 else
905 rtt = popts->cs_local_opts[0].odt_rtt_norm;
Dave Liu4be87b22009-03-14 12:48:30 +0800906
907 if (additive_latency == (cas_latency - 1))
908 al = 1;
909 if (additive_latency == (cas_latency - 2))
910 al = 2;
911
York Sunba0c2eb2011-01-10 12:03:00 +0000912 if (popts->quad_rank_present)
913 dic = 1; /* output driver impedance 240/7 ohm */
914
Dave Liu4be87b22009-03-14 12:48:30 +0800915 /*
916 * The esdmode value will also be used for writing
917 * MR1 during write leveling for DDR3, although the
918 * bits specifically related to the write leveling
919 * scheme will be handled automatically by the DDR
920 * controller. so we set the wrlvl_en = 0 here.
921 */
922 esdmode = (0
923 | ((qoff & 0x1) << 12)
924 | ((tdqs_en & 0x1) << 11)
Kumar Gala14f2eb12009-09-10 14:54:55 -0500925 | ((rtt & 0x4) << 7) /* rtt field is split */
Dave Liu4be87b22009-03-14 12:48:30 +0800926 | ((wrlvl_en & 0x1) << 7)
Kumar Gala14f2eb12009-09-10 14:54:55 -0500927 | ((rtt & 0x2) << 5) /* rtt field is split */
928 | ((dic & 0x2) << 4) /* DIC field is split */
Dave Liu4be87b22009-03-14 12:48:30 +0800929 | ((al & 0x3) << 3)
Kumar Gala14f2eb12009-09-10 14:54:55 -0500930 | ((rtt & 0x1) << 2) /* rtt field is split */
Dave Liu4be87b22009-03-14 12:48:30 +0800931 | ((dic & 0x1) << 1) /* DIC field is split */
932 | ((dll_en & 0x1) << 0)
933 );
934
935 /*
936 * DLL control for precharge PD
937 * 0=slow exit DLL off (tXPDLL)
938 * 1=fast exit DLL on (tXP)
939 */
940 dll_on = 1;
York Sun3673f2c2011-03-02 14:24:11 -0800941
Priyanka Jain4a717412013-09-25 10:41:19 +0530942 wr_mclk = (common_dimm->twr_ps + mclk_ps - 1) / mclk_ps;
York Sunbad82092012-08-17 08:22:38 +0000943 if (wr_mclk <= 16) {
944 wr = wr_table[wr_mclk - 5];
945 } else {
946 printf("Error: unsupported write recovery for mode register "
947 "wr_mclk = %d\n", wr_mclk);
948 }
York Sun3673f2c2011-03-02 14:24:11 -0800949
Dave Liu4be87b22009-03-14 12:48:30 +0800950 dll_rst = 0; /* dll no reset */
951 mode = 0; /* normal mode */
952
953 /* look up table to get the cas latency bits */
York Sunbad82092012-08-17 08:22:38 +0000954 if (cas_latency >= 5 && cas_latency <= 16) {
955 unsigned char cas_latency_table[] = {
Dave Liu4be87b22009-03-14 12:48:30 +0800956 0x2, /* 5 clocks */
957 0x4, /* 6 clocks */
958 0x6, /* 7 clocks */
959 0x8, /* 8 clocks */
960 0xa, /* 9 clocks */
961 0xc, /* 10 clocks */
York Sunbad82092012-08-17 08:22:38 +0000962 0xe, /* 11 clocks */
963 0x1, /* 12 clocks */
964 0x3, /* 13 clocks */
965 0x5, /* 14 clocks */
966 0x7, /* 15 clocks */
967 0x9, /* 16 clocks */
Dave Liu4be87b22009-03-14 12:48:30 +0800968 };
969 caslat = cas_latency_table[cas_latency - 5];
York Sunbad82092012-08-17 08:22:38 +0000970 } else {
971 printf("Error: unsupported cas latency for mode register\n");
Dave Liu4be87b22009-03-14 12:48:30 +0800972 }
York Sunbad82092012-08-17 08:22:38 +0000973
Dave Liu4be87b22009-03-14 12:48:30 +0800974 bt = 0; /* Nibble sequential */
975
976 switch (popts->burst_length) {
977 case DDR_BL8:
978 bl = 0;
979 break;
980 case DDR_OTF:
981 bl = 1;
982 break;
983 case DDR_BC4:
984 bl = 2;
985 break;
986 default:
987 printf("Error: invalid burst length of %u specified. "
988 " Defaulting to on-the-fly BC4 or BL8 beats.\n",
989 popts->burst_length);
990 bl = 1;
991 break;
992 }
993
994 sdmode = (0
995 | ((dll_on & 0x1) << 12)
996 | ((wr & 0x7) << 9)
997 | ((dll_rst & 0x1) << 8)
998 | ((mode & 0x1) << 7)
999 | (((caslat >> 1) & 0x7) << 4)
1000 | ((bt & 0x1) << 3)
York Sunbad82092012-08-17 08:22:38 +00001001 | ((caslat & 1) << 2)
Dave Liu4be87b22009-03-14 12:48:30 +08001002 | ((bl & 0x3) << 0)
1003 );
1004
1005 ddr->ddr_sdram_mode = (0
1006 | ((esdmode & 0xFFFF) << 16)
1007 | ((sdmode & 0xFFFF) << 0)
1008 );
1009
1010 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
York Sunba0c2eb2011-01-10 12:03:00 +00001011
1012 if (unq_mrs_en) { /* unique mode registers are supported */
Kumar Galad5bbe662011-11-09 10:05:10 -06001013 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Sunba0c2eb2011-01-10 12:03:00 +00001014 if (popts->rtt_override)
1015 rtt = popts->rtt_override_value;
1016 else
1017 rtt = popts->cs_local_opts[i].odt_rtt_norm;
1018
1019 esdmode &= 0xFDBB; /* clear bit 9,6,2 */
1020 esdmode |= (0
1021 | ((rtt & 0x4) << 7) /* rtt field is split */
1022 | ((rtt & 0x2) << 5) /* rtt field is split */
1023 | ((rtt & 0x1) << 2) /* rtt field is split */
1024 );
1025 switch (i) {
1026 case 1:
1027 ddr->ddr_sdram_mode_3 = (0
1028 | ((esdmode & 0xFFFF) << 16)
1029 | ((sdmode & 0xFFFF) << 0)
1030 );
1031 break;
1032 case 2:
1033 ddr->ddr_sdram_mode_5 = (0
1034 | ((esdmode & 0xFFFF) << 16)
1035 | ((sdmode & 0xFFFF) << 0)
1036 );
1037 break;
1038 case 3:
1039 ddr->ddr_sdram_mode_7 = (0
1040 | ((esdmode & 0xFFFF) << 16)
1041 | ((sdmode & 0xFFFF) << 0)
1042 );
1043 break;
1044 }
1045 }
1046 debug("FSLDDR: ddr_sdram_mode_3 = 0x%08x\n",
1047 ddr->ddr_sdram_mode_3);
1048 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1049 ddr->ddr_sdram_mode_5);
1050 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1051 ddr->ddr_sdram_mode_5);
1052 }
Dave Liu4be87b22009-03-14 12:48:30 +08001053}
1054
1055#else /* !CONFIG_FSL_DDR3 */
1056
1057/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
1058static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
1059 const memctl_options_t *popts,
1060 const common_timing_params_t *common_dimm,
1061 unsigned int cas_latency,
York Sunba0c2eb2011-01-10 12:03:00 +00001062 unsigned int additive_latency,
1063 const unsigned int unq_mrs_en)
Dave Liu4be87b22009-03-14 12:48:30 +08001064{
1065 unsigned short esdmode; /* Extended SDRAM mode */
1066 unsigned short sdmode; /* SDRAM mode */
1067
Kumar Gala124b0822008-08-26 15:01:29 -05001068 /*
1069 * FIXME: This ought to be pre-calculated in a
1070 * technology-specific routine,
1071 * e.g. compute_DDR2_mode_register(), and then the
1072 * sdmode and esdmode passed in as part of common_dimm.
1073 */
1074
1075 /* Extended Mode Register */
1076 unsigned int mrs = 0; /* Mode Register Set */
1077 unsigned int outputs = 0; /* 0=Enabled, 1=Disabled */
1078 unsigned int rdqs_en = 0; /* RDQS Enable: 0=no, 1=yes */
1079 unsigned int dqs_en = 0; /* DQS# Enable: 0=enable, 1=disable */
1080 unsigned int ocd = 0; /* 0x0=OCD not supported,
1081 0x7=OCD default state */
1082 unsigned int rtt;
1083 unsigned int al; /* Posted CAS# additive latency (AL) */
1084 unsigned int ods = 0; /* Output Drive Strength:
1085 0 = Full strength (18ohm)
1086 1 = Reduced strength (4ohm) */
1087 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
1088 1=Disable (Test/Debug) */
1089
1090 /* Mode Register (MR) */
1091 unsigned int mr; /* Mode Register Definition */
1092 unsigned int pd; /* Power-Down Mode */
1093 unsigned int wr; /* Write Recovery */
1094 unsigned int dll_res; /* DLL Reset */
1095 unsigned int mode; /* Normal=0 or Test=1 */
Kumar Gala35ad58d2008-09-05 14:40:29 -05001096 unsigned int caslat = 0;/* CAS# latency */
Kumar Gala124b0822008-08-26 15:01:29 -05001097 /* BT: Burst Type (0=Sequential, 1=Interleaved) */
1098 unsigned int bt;
1099 unsigned int bl; /* BL: Burst Length */
1100
1101#if defined(CONFIG_FSL_DDR2)
1102 const unsigned int mclk_ps = get_memory_clk_period_ps();
1103#endif
Priyanka Jain4a717412013-09-25 10:41:19 +05301104 dqs_en = !popts->dqs_config;
Kumar Gala124b0822008-08-26 15:01:29 -05001105 rtt = fsl_ddr_get_rtt();
1106
1107 al = additive_latency;
1108
1109 esdmode = (0
1110 | ((mrs & 0x3) << 14)
1111 | ((outputs & 0x1) << 12)
1112 | ((rdqs_en & 0x1) << 11)
1113 | ((dqs_en & 0x1) << 10)
1114 | ((ocd & 0x7) << 7)
1115 | ((rtt & 0x2) << 5) /* rtt field is split */
1116 | ((al & 0x7) << 3)
1117 | ((rtt & 0x1) << 2) /* rtt field is split */
1118 | ((ods & 0x1) << 1)
1119 | ((dll_en & 0x1) << 0)
1120 );
1121
1122 mr = 0; /* FIXME: CHECKME */
1123
1124 /*
1125 * 0 = Fast Exit (Normal)
1126 * 1 = Slow Exit (Low Power)
1127 */
1128 pd = 0;
1129
1130#if defined(CONFIG_FSL_DDR1)
1131 wr = 0; /* Historical */
1132#elif defined(CONFIG_FSL_DDR2)
Priyanka Jain4a717412013-09-25 10:41:19 +05301133 wr = (common_dimm->twr_ps + mclk_ps - 1) / mclk_ps - 1;
Kumar Gala124b0822008-08-26 15:01:29 -05001134#endif
1135 dll_res = 0;
1136 mode = 0;
1137
1138#if defined(CONFIG_FSL_DDR1)
1139 if (1 <= cas_latency && cas_latency <= 4) {
1140 unsigned char mode_caslat_table[4] = {
1141 0x5, /* 1.5 clocks */
1142 0x2, /* 2.0 clocks */
1143 0x6, /* 2.5 clocks */
1144 0x3 /* 3.0 clocks */
1145 };
Kumar Gala35ad58d2008-09-05 14:40:29 -05001146 caslat = mode_caslat_table[cas_latency - 1];
1147 } else {
1148 printf("Warning: unknown cas_latency %d\n", cas_latency);
Kumar Gala124b0822008-08-26 15:01:29 -05001149 }
1150#elif defined(CONFIG_FSL_DDR2)
1151 caslat = cas_latency;
Kumar Gala124b0822008-08-26 15:01:29 -05001152#endif
1153 bt = 0;
1154
1155 switch (popts->burst_length) {
Dave Liu4be87b22009-03-14 12:48:30 +08001156 case DDR_BL4:
Kumar Gala124b0822008-08-26 15:01:29 -05001157 bl = 2;
1158 break;
Dave Liu4be87b22009-03-14 12:48:30 +08001159 case DDR_BL8:
Kumar Gala124b0822008-08-26 15:01:29 -05001160 bl = 3;
1161 break;
1162 default:
1163 printf("Error: invalid burst length of %u specified. "
1164 " Defaulting to 4 beats.\n",
1165 popts->burst_length);
1166 bl = 2;
1167 break;
1168 }
1169
1170 sdmode = (0
1171 | ((mr & 0x3) << 14)
1172 | ((pd & 0x1) << 12)
1173 | ((wr & 0x7) << 9)
1174 | ((dll_res & 0x1) << 8)
1175 | ((mode & 0x1) << 7)
1176 | ((caslat & 0x7) << 4)
1177 | ((bt & 0x1) << 3)
1178 | ((bl & 0x7) << 0)
1179 );
1180
1181 ddr->ddr_sdram_mode = (0
1182 | ((esdmode & 0xFFFF) << 16)
1183 | ((sdmode & 0xFFFF) << 0)
1184 );
Haiying Wangd90e0402008-10-03 12:37:26 -04001185 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
Kumar Gala124b0822008-08-26 15:01:29 -05001186}
Dave Liu4be87b22009-03-14 12:48:30 +08001187#endif
Kumar Gala124b0822008-08-26 15:01:29 -05001188
1189/* DDR SDRAM Data Initialization (DDR_DATA_INIT) */
1190static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr)
1191{
1192 unsigned int init_value; /* Initialization value */
1193
Anatolij Gustschin021b7ae2013-01-21 23:50:27 +00001194#ifdef CONFIG_MEM_INIT_VALUE
1195 init_value = CONFIG_MEM_INIT_VALUE;
1196#else
Kumar Gala124b0822008-08-26 15:01:29 -05001197 init_value = 0xDEADBEEF;
Anatolij Gustschin021b7ae2013-01-21 23:50:27 +00001198#endif
Kumar Gala124b0822008-08-26 15:01:29 -05001199 ddr->ddr_data_init = init_value;
1200}
1201
1202/*
1203 * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL)
1204 * The old controller on the 8540/60 doesn't have this register.
1205 * Hope it's OK to set it (to 0) anyway.
1206 */
1207static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
1208 const memctl_options_t *popts)
1209{
1210 unsigned int clk_adjust; /* Clock adjust */
1211
1212 clk_adjust = popts->clk_adjust;
1213 ddr->ddr_sdram_clk_cntl = (clk_adjust & 0xF) << 23;
yorkde879322010-07-02 22:25:55 +00001214 debug("FSLDDR: clk_cntl = 0x%08x\n", ddr->ddr_sdram_clk_cntl);
Kumar Gala124b0822008-08-26 15:01:29 -05001215}
1216
1217/* DDR Initialization Address (DDR_INIT_ADDR) */
1218static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
1219{
1220 unsigned int init_addr = 0; /* Initialization address */
1221
1222 ddr->ddr_init_addr = init_addr;
1223}
1224
1225/* DDR Initialization Address (DDR_INIT_EXT_ADDR) */
1226static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
1227{
1228 unsigned int uia = 0; /* Use initialization address */
1229 unsigned int init_ext_addr = 0; /* Initialization address */
1230
1231 ddr->ddr_init_ext_addr = (0
1232 | ((uia & 0x1) << 31)
1233 | (init_ext_addr & 0xF)
1234 );
1235}
1236
1237/* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */
Dave Liu3525e1a2010-03-05 12:22:00 +08001238static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr,
1239 const memctl_options_t *popts)
Kumar Gala124b0822008-08-26 15:01:29 -05001240{
1241 unsigned int rwt = 0; /* Read-to-write turnaround for same CS */
1242 unsigned int wrt = 0; /* Write-to-read turnaround for same CS */
1243 unsigned int rrt = 0; /* Read-to-read turnaround for same CS */
1244 unsigned int wwt = 0; /* Write-to-write turnaround for same CS */
1245 unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */
1246
Dave Liu4be87b22009-03-14 12:48:30 +08001247#if defined(CONFIG_FSL_DDR3)
Dave Liu3525e1a2010-03-05 12:22:00 +08001248 if (popts->burst_length == DDR_BL8) {
1249 /* We set BL/2 for fixed BL8 */
1250 rrt = 0; /* BL/2 clocks */
1251 wwt = 0; /* BL/2 clocks */
1252 } else {
1253 /* We need to set BL/2 + 2 to BC4 and OTF */
1254 rrt = 2; /* BL/2 + 2 clocks */
1255 wwt = 2; /* BL/2 + 2 clocks */
1256 }
Dave Liu4be87b22009-03-14 12:48:30 +08001257 dll_lock = 1; /* tDLLK = 512 clocks from spec */
1258#endif
Kumar Gala124b0822008-08-26 15:01:29 -05001259 ddr->timing_cfg_4 = (0
1260 | ((rwt & 0xf) << 28)
1261 | ((wrt & 0xf) << 24)
1262 | ((rrt & 0xf) << 20)
1263 | ((wwt & 0xf) << 16)
1264 | (dll_lock & 0x3)
1265 );
Haiying Wangd90e0402008-10-03 12:37:26 -04001266 debug("FSLDDR: timing_cfg_4 = 0x%08x\n", ddr->timing_cfg_4);
Kumar Gala124b0822008-08-26 15:01:29 -05001267}
1268
1269/* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */
York Sunba0c2eb2011-01-10 12:03:00 +00001270static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr, unsigned int cas_latency)
Kumar Gala124b0822008-08-26 15:01:29 -05001271{
1272 unsigned int rodt_on = 0; /* Read to ODT on */
1273 unsigned int rodt_off = 0; /* Read to ODT off */
1274 unsigned int wodt_on = 0; /* Write to ODT on */
1275 unsigned int wodt_off = 0; /* Write to ODT off */
1276
Dave Liu4be87b22009-03-14 12:48:30 +08001277#if defined(CONFIG_FSL_DDR3)
York Sunba0c2eb2011-01-10 12:03:00 +00001278 /* rodt_on = timing_cfg_1[caslat] - timing_cfg_2[wrlat] + 1 */
1279 rodt_on = cas_latency - ((ddr->timing_cfg_2 & 0x00780000) >> 19) + 1;
Dave Liu4be87b22009-03-14 12:48:30 +08001280 rodt_off = 4; /* 4 clocks */
york1714e492010-07-02 22:25:56 +00001281 wodt_on = 1; /* 1 clocks */
Dave Liu4be87b22009-03-14 12:48:30 +08001282 wodt_off = 4; /* 4 clocks */
1283#endif
1284
Kumar Gala124b0822008-08-26 15:01:29 -05001285 ddr->timing_cfg_5 = (0
Dave Liu4758d532008-11-21 16:31:29 +08001286 | ((rodt_on & 0x1f) << 24)
1287 | ((rodt_off & 0x7) << 20)
1288 | ((wodt_on & 0x1f) << 12)
1289 | ((wodt_off & 0x7) << 8)
Kumar Gala124b0822008-08-26 15:01:29 -05001290 );
Haiying Wangd90e0402008-10-03 12:37:26 -04001291 debug("FSLDDR: timing_cfg_5 = 0x%08x\n", ddr->timing_cfg_5);
Kumar Gala124b0822008-08-26 15:01:29 -05001292}
1293
1294/* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */
Dave Liu4be87b22009-03-14 12:48:30 +08001295static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int zq_en)
Kumar Gala124b0822008-08-26 15:01:29 -05001296{
Kumar Gala124b0822008-08-26 15:01:29 -05001297 unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */
1298 /* Normal Operation Full Calibration Time (tZQoper) */
1299 unsigned int zqoper = 0;
1300 /* Normal Operation Short Calibration Time (tZQCS) */
1301 unsigned int zqcs = 0;
1302
Dave Liu4be87b22009-03-14 12:48:30 +08001303 if (zq_en) {
1304 zqinit = 9; /* 512 clocks */
1305 zqoper = 8; /* 256 clocks */
1306 zqcs = 6; /* 64 clocks */
1307 }
1308
Kumar Gala124b0822008-08-26 15:01:29 -05001309 ddr->ddr_zq_cntl = (0
1310 | ((zq_en & 0x1) << 31)
1311 | ((zqinit & 0xF) << 24)
1312 | ((zqoper & 0xF) << 16)
1313 | ((zqcs & 0xF) << 8)
1314 );
York Sunba0c2eb2011-01-10 12:03:00 +00001315 debug("FSLDDR: zq_cntl = 0x%08x\n", ddr->ddr_zq_cntl);
Kumar Gala124b0822008-08-26 15:01:29 -05001316}
1317
1318/* DDR Write Leveling Control (DDR_WRLVL_CNTL) */
Dave Liu64ee7df2009-12-16 10:24:37 -06001319static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int wrlvl_en,
1320 const memctl_options_t *popts)
Kumar Gala124b0822008-08-26 15:01:29 -05001321{
Kumar Gala124b0822008-08-26 15:01:29 -05001322 /*
1323 * First DQS pulse rising edge after margining mode
1324 * is programmed (tWL_MRD)
1325 */
1326 unsigned int wrlvl_mrd = 0;
1327 /* ODT delay after margining mode is programmed (tWL_ODTEN) */
1328 unsigned int wrlvl_odten = 0;
1329 /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */
1330 unsigned int wrlvl_dqsen = 0;
1331 /* WRLVL_SMPL: Write leveling sample time */
1332 unsigned int wrlvl_smpl = 0;
1333 /* WRLVL_WLR: Write leveling repeition time */
1334 unsigned int wrlvl_wlr = 0;
1335 /* WRLVL_START: Write leveling start time */
1336 unsigned int wrlvl_start = 0;
1337
Dave Liu4be87b22009-03-14 12:48:30 +08001338 /* suggest enable write leveling for DDR3 due to fly-by topology */
1339 if (wrlvl_en) {
1340 /* tWL_MRD min = 40 nCK, we set it 64 */
1341 wrlvl_mrd = 0x6;
1342 /* tWL_ODTEN 128 */
1343 wrlvl_odten = 0x7;
1344 /* tWL_DQSEN min = 25 nCK, we set it 32 */
1345 wrlvl_dqsen = 0x5;
1346 /*
Dave Liu64ee7df2009-12-16 10:24:37 -06001347 * Write leveling sample time at least need 6 clocks
1348 * higher than tWLO to allow enough time for progagation
1349 * delay and sampling the prime data bits.
Dave Liu4be87b22009-03-14 12:48:30 +08001350 */
1351 wrlvl_smpl = 0xf;
1352 /*
1353 * Write leveling repetition time
1354 * at least tWLO + 6 clocks clocks
york1714e492010-07-02 22:25:56 +00001355 * we set it 64
Dave Liu4be87b22009-03-14 12:48:30 +08001356 */
york1714e492010-07-02 22:25:56 +00001357 wrlvl_wlr = 0x6;
Dave Liu4be87b22009-03-14 12:48:30 +08001358 /*
1359 * Write leveling start time
1360 * The value use for the DQS_ADJUST for the first sample
York Sunba0c2eb2011-01-10 12:03:00 +00001361 * when write leveling is enabled. It probably needs to be
1362 * overriden per platform.
Dave Liu4be87b22009-03-14 12:48:30 +08001363 */
1364 wrlvl_start = 0x8;
Dave Liu64ee7df2009-12-16 10:24:37 -06001365 /*
1366 * Override the write leveling sample and start time
1367 * according to specific board
1368 */
1369 if (popts->wrlvl_override) {
1370 wrlvl_smpl = popts->wrlvl_sample;
1371 wrlvl_start = popts->wrlvl_start;
1372 }
Dave Liu4be87b22009-03-14 12:48:30 +08001373 }
1374
Kumar Gala124b0822008-08-26 15:01:29 -05001375 ddr->ddr_wrlvl_cntl = (0
1376 | ((wrlvl_en & 0x1) << 31)
1377 | ((wrlvl_mrd & 0x7) << 24)
1378 | ((wrlvl_odten & 0x7) << 20)
1379 | ((wrlvl_dqsen & 0x7) << 16)
1380 | ((wrlvl_smpl & 0xf) << 12)
1381 | ((wrlvl_wlr & 0x7) << 8)
Dave Liu4758d532008-11-21 16:31:29 +08001382 | ((wrlvl_start & 0x1F) << 0)
Kumar Gala124b0822008-08-26 15:01:29 -05001383 );
York Sunba0c2eb2011-01-10 12:03:00 +00001384 debug("FSLDDR: wrlvl_cntl = 0x%08x\n", ddr->ddr_wrlvl_cntl);
York Sun7d69ea32012-10-08 07:44:22 +00001385 ddr->ddr_wrlvl_cntl_2 = popts->wrlvl_ctl_2;
1386 debug("FSLDDR: wrlvl_cntl_2 = 0x%08x\n", ddr->ddr_wrlvl_cntl_2);
1387 ddr->ddr_wrlvl_cntl_3 = popts->wrlvl_ctl_3;
1388 debug("FSLDDR: wrlvl_cntl_3 = 0x%08x\n", ddr->ddr_wrlvl_cntl_3);
1389
Kumar Gala124b0822008-08-26 15:01:29 -05001390}
1391
1392/* DDR Self Refresh Counter (DDR_SR_CNTR) */
Dave Liu2aad0ae2008-11-21 16:31:35 +08001393static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr, unsigned int sr_it)
Kumar Gala124b0822008-08-26 15:01:29 -05001394{
Dave Liu2aad0ae2008-11-21 16:31:35 +08001395 /* Self Refresh Idle Threshold */
Kumar Gala124b0822008-08-26 15:01:29 -05001396 ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
1397}
1398
york42603722010-07-02 22:25:54 +00001399static void set_ddr_eor(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
1400{
1401 if (popts->addr_hash) {
1402 ddr->ddr_eor = 0x40000000; /* address hash enable */
Kumar Gala4513d762011-03-18 11:53:06 -05001403 puts("Address hashing enabled.\n");
york42603722010-07-02 22:25:54 +00001404 }
1405}
1406
York Sunba0c2eb2011-01-10 12:03:00 +00001407static void set_ddr_cdr1(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
1408{
1409 ddr->ddr_cdr1 = popts->ddr_cdr1;
1410 debug("FSLDDR: ddr_cdr1 = 0x%08x\n", ddr->ddr_cdr1);
1411}
1412
York Sun7d69ea32012-10-08 07:44:22 +00001413static void set_ddr_cdr2(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
1414{
1415 ddr->ddr_cdr2 = popts->ddr_cdr2;
1416 debug("FSLDDR: ddr_cdr2 = 0x%08x\n", ddr->ddr_cdr2);
1417}
1418
Kumar Gala124b0822008-08-26 15:01:29 -05001419unsigned int
1420check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
1421{
1422 unsigned int res = 0;
1423
1424 /*
1425 * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are
1426 * not set at the same time.
1427 */
1428 if (ddr->ddr_sdram_cfg & 0x10000000
1429 && ddr->ddr_sdram_cfg & 0x00008000) {
1430 printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
1431 " should not be set at the same time.\n");
1432 res++;
1433 }
1434
1435 return res;
1436}
1437
1438unsigned int
1439compute_fsl_memctl_config_regs(const memctl_options_t *popts,
1440 fsl_ddr_cfg_regs_t *ddr,
1441 const common_timing_params_t *common_dimm,
1442 const dimm_params_t *dimm_params,
Haiying Wang80ad4012010-12-01 10:35:31 -05001443 unsigned int dbw_cap_adj,
1444 unsigned int size_only)
Kumar Gala124b0822008-08-26 15:01:29 -05001445{
1446 unsigned int i;
1447 unsigned int cas_latency;
1448 unsigned int additive_latency;
Dave Liu2aad0ae2008-11-21 16:31:35 +08001449 unsigned int sr_it;
Dave Liu4be87b22009-03-14 12:48:30 +08001450 unsigned int zq_en;
1451 unsigned int wrlvl_en;
York Sunba0c2eb2011-01-10 12:03:00 +00001452 unsigned int ip_rev = 0;
1453 unsigned int unq_mrs_en = 0;
York Sun2927c5e2010-10-18 13:46:50 -07001454 int cs_en = 1;
Kumar Gala124b0822008-08-26 15:01:29 -05001455
1456 memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
1457
1458 if (common_dimm == NULL) {
1459 printf("Error: subset DIMM params struct null pointer\n");
1460 return 1;
1461 }
1462
1463 /*
1464 * Process overrides first.
1465 *
1466 * FIXME: somehow add dereated caslat to this
1467 */
1468 cas_latency = (popts->cas_latency_override)
1469 ? popts->cas_latency_override_value
1470 : common_dimm->lowest_common_SPD_caslat;
1471
1472 additive_latency = (popts->additive_latency_override)
1473 ? popts->additive_latency_override_value
1474 : common_dimm->additive_latency;
1475
Dave Liu2aad0ae2008-11-21 16:31:35 +08001476 sr_it = (popts->auto_self_refresh_en)
1477 ? popts->sr_it
1478 : 0;
Dave Liu4be87b22009-03-14 12:48:30 +08001479 /* ZQ calibration */
1480 zq_en = (popts->zq_en) ? 1 : 0;
1481 /* write leveling */
1482 wrlvl_en = (popts->wrlvl_en) ? 1 : 0;
Dave Liu2aad0ae2008-11-21 16:31:35 +08001483
Kumar Gala124b0822008-08-26 15:01:29 -05001484 /* Chip Select Memory Bounds (CSn_BNDS) */
1485 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Sune8dc17b2012-08-17 08:22:39 +00001486 unsigned long long ea, sa;
york93799ca2010-07-02 22:25:52 +00001487 unsigned int cs_per_dimm
1488 = CONFIG_CHIP_SELECTS_PER_CTRL / CONFIG_DIMM_SLOTS_PER_CTLR;
1489 unsigned int dimm_number
1490 = i / cs_per_dimm;
1491 unsigned long long rank_density
York Sune8dc17b2012-08-17 08:22:39 +00001492 = dimm_params[dimm_number].rank_density >> dbw_cap_adj;
Haiying Wang272b5962008-10-03 12:36:39 -04001493
york93799ca2010-07-02 22:25:52 +00001494 if (dimm_params[dimm_number].n_ranks == 0) {
Kumar Gala124b0822008-08-26 15:01:29 -05001495 debug("Skipping setup of CS%u "
yorkf4f93c62010-07-02 22:25:53 +00001496 "because n_ranks on DIMM %u is 0\n", i, dimm_number);
Kumar Gala124b0822008-08-26 15:01:29 -05001497 continue;
1498 }
York Sune8dc17b2012-08-17 08:22:39 +00001499 if (popts->memctl_interleaving) {
york93799ca2010-07-02 22:25:52 +00001500 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
York Sune8dc17b2012-08-17 08:22:39 +00001501 case FSL_DDR_CS0_CS1_CS2_CS3:
1502 break;
york93799ca2010-07-02 22:25:52 +00001503 case FSL_DDR_CS0_CS1:
1504 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
York Sun2927c5e2010-10-18 13:46:50 -07001505 if (i > 1)
1506 cs_en = 0;
york93799ca2010-07-02 22:25:52 +00001507 break;
1508 case FSL_DDR_CS2_CS3:
York Sune8dc17b2012-08-17 08:22:39 +00001509 default:
York Sun2927c5e2010-10-18 13:46:50 -07001510 if (i > 0)
1511 cs_en = 0;
york93799ca2010-07-02 22:25:52 +00001512 break;
york93799ca2010-07-02 22:25:52 +00001513 }
York Sune8dc17b2012-08-17 08:22:39 +00001514 sa = common_dimm->base_address;
York Sun98df4d12012-10-08 07:44:23 +00001515 ea = sa + common_dimm->total_mem - 1;
York Sune8dc17b2012-08-17 08:22:39 +00001516 } else if (!popts->memctl_interleaving) {
Kumar Gala124b0822008-08-26 15:01:29 -05001517 /*
1518 * If memory interleaving between controllers is NOT
1519 * enabled, the starting address for each memory
1520 * controller is distinct. However, because rank
1521 * interleaving is enabled, the starting and ending
1522 * addresses of the total memory on that memory
1523 * controller needs to be programmed into its
1524 * respective CS0_BNDS.
1525 */
Haiying Wang272b5962008-10-03 12:36:39 -04001526 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
1527 case FSL_DDR_CS0_CS1_CS2_CS3:
Haiying Wang272b5962008-10-03 12:36:39 -04001528 sa = common_dimm->base_address;
York Sun98df4d12012-10-08 07:44:23 +00001529 ea = sa + common_dimm->total_mem - 1;
Haiying Wang272b5962008-10-03 12:36:39 -04001530 break;
1531 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
York Sune8dc17b2012-08-17 08:22:39 +00001532 if ((i >= 2) && (dimm_number == 0)) {
york93799ca2010-07-02 22:25:52 +00001533 sa = dimm_params[dimm_number].base_address +
York Sune8dc17b2012-08-17 08:22:39 +00001534 2 * rank_density;
1535 ea = sa + 2 * rank_density - 1;
york93799ca2010-07-02 22:25:52 +00001536 } else {
1537 sa = dimm_params[dimm_number].base_address;
York Sune8dc17b2012-08-17 08:22:39 +00001538 ea = sa + 2 * rank_density - 1;
Haiying Wang272b5962008-10-03 12:36:39 -04001539 }
1540 break;
1541 case FSL_DDR_CS0_CS1:
york93799ca2010-07-02 22:25:52 +00001542 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
1543 sa = dimm_params[dimm_number].base_address;
York Sune8dc17b2012-08-17 08:22:39 +00001544 ea = sa + rank_density - 1;
1545 if (i != 1)
1546 sa += (i % cs_per_dimm) * rank_density;
1547 ea += (i % cs_per_dimm) * rank_density;
york93799ca2010-07-02 22:25:52 +00001548 } else {
1549 sa = 0;
1550 ea = 0;
1551 }
1552 if (i == 0)
York Sune8dc17b2012-08-17 08:22:39 +00001553 ea += rank_density;
Haiying Wang272b5962008-10-03 12:36:39 -04001554 break;
1555 case FSL_DDR_CS2_CS3:
york93799ca2010-07-02 22:25:52 +00001556 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
1557 sa = dimm_params[dimm_number].base_address;
York Sune8dc17b2012-08-17 08:22:39 +00001558 ea = sa + rank_density - 1;
1559 if (i != 3)
1560 sa += (i % cs_per_dimm) * rank_density;
1561 ea += (i % cs_per_dimm) * rank_density;
york93799ca2010-07-02 22:25:52 +00001562 } else {
1563 sa = 0;
1564 ea = 0;
Haiying Wang272b5962008-10-03 12:36:39 -04001565 }
york93799ca2010-07-02 22:25:52 +00001566 if (i == 2)
1567 ea += (rank_density >> dbw_cap_adj);
Haiying Wang272b5962008-10-03 12:36:39 -04001568 break;
1569 default: /* No bank(chip-select) interleaving */
York Sune8dc17b2012-08-17 08:22:39 +00001570 sa = dimm_params[dimm_number].base_address;
1571 ea = sa + rank_density - 1;
1572 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
1573 sa += (i % cs_per_dimm) * rank_density;
1574 ea += (i % cs_per_dimm) * rank_density;
1575 } else {
1576 sa = 0;
1577 ea = 0;
1578 }
Haiying Wang272b5962008-10-03 12:36:39 -04001579 break;
1580 }
Kumar Gala124b0822008-08-26 15:01:29 -05001581 }
Kumar Gala124b0822008-08-26 15:01:29 -05001582
1583 sa >>= 24;
1584 ea >>= 24;
1585
York Sun98df4d12012-10-08 07:44:23 +00001586 if (cs_en) {
1587 ddr->cs[i].bnds = (0
1588 | ((sa & 0xFFF) << 16)/* starting address MSB */
1589 | ((ea & 0xFFF) << 0) /* ending address MSB */
1590 );
1591 } else {
York Sunc21a7392013-06-25 11:37:45 -07001592 /* setting bnds to 0xffffffff for inactive CS */
1593 ddr->cs[i].bnds = 0xffffffff;
York Sun98df4d12012-10-08 07:44:23 +00001594 }
Kumar Gala124b0822008-08-26 15:01:29 -05001595
Haiying Wangd90e0402008-10-03 12:37:26 -04001596 debug("FSLDDR: cs[%d]_bnds = 0x%08x\n", i, ddr->cs[i].bnds);
York Sun98df4d12012-10-08 07:44:23 +00001597 set_csn_config(dimm_number, i, ddr, popts, dimm_params);
1598 set_csn_config_2(i, ddr);
Kumar Gala124b0822008-08-26 15:01:29 -05001599 }
1600
Haiying Wang80ad4012010-12-01 10:35:31 -05001601 /*
1602 * In the case we only need to compute the ddr sdram size, we only need
1603 * to set csn registers, so return from here.
1604 */
1605 if (size_only)
1606 return 0;
1607
york42603722010-07-02 22:25:54 +00001608 set_ddr_eor(ddr, popts);
1609
Dave Liu4be87b22009-03-14 12:48:30 +08001610#if !defined(CONFIG_FSL_DDR1)
York Sun98df4d12012-10-08 07:44:23 +00001611 set_timing_cfg_0(ddr, popts, dimm_params);
Kumar Gala124b0822008-08-26 15:01:29 -05001612#endif
1613
York Suncd077cf2012-08-17 08:22:40 +00001614 set_timing_cfg_3(ddr, popts, common_dimm, cas_latency);
Dave Liu4be87b22009-03-14 12:48:30 +08001615 set_timing_cfg_1(ddr, popts, common_dimm, cas_latency);
Kumar Gala124b0822008-08-26 15:01:29 -05001616 set_timing_cfg_2(ddr, popts, common_dimm,
1617 cas_latency, additive_latency);
1618
York Sunba0c2eb2011-01-10 12:03:00 +00001619 set_ddr_cdr1(ddr, popts);
York Sun7d69ea32012-10-08 07:44:22 +00001620 set_ddr_cdr2(ddr, popts);
Kumar Gala124b0822008-08-26 15:01:29 -05001621 set_ddr_sdram_cfg(ddr, popts, common_dimm);
York Sunba0c2eb2011-01-10 12:03:00 +00001622 ip_rev = fsl_ddr_get_version();
1623 if (ip_rev > 0x40400)
1624 unq_mrs_en = 1;
Kumar Gala124b0822008-08-26 15:01:29 -05001625
York Sunba0c2eb2011-01-10 12:03:00 +00001626 set_ddr_sdram_cfg_2(ddr, popts, unq_mrs_en);
Kumar Gala124b0822008-08-26 15:01:29 -05001627 set_ddr_sdram_mode(ddr, popts, common_dimm,
York Sunba0c2eb2011-01-10 12:03:00 +00001628 cas_latency, additive_latency, unq_mrs_en);
1629 set_ddr_sdram_mode_2(ddr, popts, unq_mrs_en);
Kumar Gala124b0822008-08-26 15:01:29 -05001630 set_ddr_sdram_interval(ddr, popts, common_dimm);
1631 set_ddr_data_init(ddr);
1632 set_ddr_sdram_clk_cntl(ddr, popts);
1633 set_ddr_init_addr(ddr);
1634 set_ddr_init_ext_addr(ddr);
Dave Liu3525e1a2010-03-05 12:22:00 +08001635 set_timing_cfg_4(ddr, popts);
York Sunba0c2eb2011-01-10 12:03:00 +00001636 set_timing_cfg_5(ddr, cas_latency);
Kumar Gala124b0822008-08-26 15:01:29 -05001637
Dave Liu4be87b22009-03-14 12:48:30 +08001638 set_ddr_zq_cntl(ddr, zq_en);
Dave Liu64ee7df2009-12-16 10:24:37 -06001639 set_ddr_wrlvl_cntl(ddr, wrlvl_en, popts);
Kumar Gala124b0822008-08-26 15:01:29 -05001640
Dave Liu2aad0ae2008-11-21 16:31:35 +08001641 set_ddr_sr_cntr(ddr, sr_it);
Kumar Gala124b0822008-08-26 15:01:29 -05001642
York Sunba0c2eb2011-01-10 12:03:00 +00001643 set_ddr_sdram_rcw(ddr, popts, common_dimm);
Kumar Gala124b0822008-08-26 15:01:29 -05001644
York Sun972cc402013-06-25 11:37:41 -07001645#ifdef CONFIG_SYS_FSL_DDR_EMU
1646 /* disble DDR training for emulator */
1647 ddr->debug[2] = 0x00000400;
1648 ddr->debug[4] = 0xff800000;
1649#endif
Kumar Gala124b0822008-08-26 15:01:29 -05001650 return check_fsl_memctl_config_regs(ddr);
1651}