blob: fe8aa98e8e363e8696072101efff08fe472944c7 [file] [log] [blame]
Kumar Gala124b0822008-08-26 15:01:29 -05001/*
York Sun2896cb72014-03-27 17:54:47 -07002 * Copyright 2008-2014 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>
York Sunf0626592013-09-30 09:22:09 -070014#include <fsl_ddr_sdram.h>
Kumar Gala124b0822008-08-26 15:01:29 -050015
York Sunf0626592013-09-30 09:22:09 -070016#include <fsl_ddr.h>
York Suna21803d2013-11-18 10:29:32 -080017#include <fsl_immap.h>
York Sunf0626592013-09-30 09:22:09 -070018#include <asm/io.h>
Kumar Gala124b0822008-08-26 15:01:29 -050019
York Sunba0c2eb2011-01-10 12:03:00 +000020unsigned int picos_to_mclk(unsigned int picos);
21
Kumar Gala124b0822008-08-26 15:01:29 -050022/*
23 * Determine Rtt value.
24 *
25 * This should likely be either board or controller specific.
26 *
Dave Liu4be87b22009-03-14 12:48:30 +080027 * Rtt(nominal) - DDR2:
Kumar Gala124b0822008-08-26 15:01:29 -050028 * 0 = Rtt disabled
29 * 1 = 75 ohm
30 * 2 = 150 ohm
31 * 3 = 50 ohm
Dave Liu4be87b22009-03-14 12:48:30 +080032 * Rtt(nominal) - DDR3:
33 * 0 = Rtt disabled
34 * 1 = 60 ohm
35 * 2 = 120 ohm
36 * 3 = 40 ohm
37 * 4 = 20 ohm
38 * 5 = 30 ohm
Kumar Gala124b0822008-08-26 15:01:29 -050039 *
40 * FIXME: Apparently 8641 needs a value of 2
41 * FIXME: Old code seys if 667 MHz or higher, use 3 on 8572
42 *
43 * FIXME: There was some effort down this line earlier:
44 *
45 * unsigned int i;
46 * for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL/2; i++) {
47 * if (popts->dimmslot[i].num_valid_cs
48 * && (popts->cs_local_opts[2*i].odt_rd_cfg
49 * || popts->cs_local_opts[2*i].odt_wr_cfg)) {
50 * rtt = 2;
51 * break;
52 * }
53 * }
54 */
55static inline int fsl_ddr_get_rtt(void)
56{
57 int rtt;
58
York Sunf0626592013-09-30 09:22:09 -070059#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala124b0822008-08-26 15:01:29 -050060 rtt = 0;
York Sunf0626592013-09-30 09:22:09 -070061#elif defined(CONFIG_SYS_FSL_DDR2)
Kumar Gala124b0822008-08-26 15:01:29 -050062 rtt = 3;
63#else
Dave Liu4be87b22009-03-14 12:48:30 +080064 rtt = 0;
Kumar Gala124b0822008-08-26 15:01:29 -050065#endif
66
67 return rtt;
68}
69
York Sun2896cb72014-03-27 17:54:47 -070070#ifdef CONFIG_SYS_FSL_DDR4
71/*
72 * compute CAS write latency according to DDR4 spec
73 * CWL = 9 for <= 1600MT/s
74 * 10 for <= 1866MT/s
75 * 11 for <= 2133MT/s
76 * 12 for <= 2400MT/s
77 * 14 for <= 2667MT/s
78 * 16 for <= 2933MT/s
79 * 18 for higher
80 */
81static inline unsigned int compute_cas_write_latency(void)
82{
83 unsigned int cwl;
84 const unsigned int mclk_ps = get_memory_clk_period_ps();
85 if (mclk_ps >= 1250)
86 cwl = 9;
87 else if (mclk_ps >= 1070)
88 cwl = 10;
89 else if (mclk_ps >= 935)
90 cwl = 11;
91 else if (mclk_ps >= 833)
92 cwl = 12;
93 else if (mclk_ps >= 750)
94 cwl = 14;
95 else if (mclk_ps >= 681)
96 cwl = 16;
97 else
98 cwl = 18;
99
100 return cwl;
101}
102#else
Dave Liu4be87b22009-03-14 12:48:30 +0800103/*
104 * compute the CAS write latency according to DDR3 spec
105 * CWL = 5 if tCK >= 2.5ns
106 * 6 if 2.5ns > tCK >= 1.875ns
107 * 7 if 1.875ns > tCK >= 1.5ns
108 * 8 if 1.5ns > tCK >= 1.25ns
York Sun7a16d642011-08-24 09:40:25 -0700109 * 9 if 1.25ns > tCK >= 1.07ns
110 * 10 if 1.07ns > tCK >= 0.935ns
111 * 11 if 0.935ns > tCK >= 0.833ns
112 * 12 if 0.833ns > tCK >= 0.75ns
Dave Liu4be87b22009-03-14 12:48:30 +0800113 */
114static inline unsigned int compute_cas_write_latency(void)
115{
116 unsigned int cwl;
117 const unsigned int mclk_ps = get_memory_clk_period_ps();
118
119 if (mclk_ps >= 2500)
120 cwl = 5;
121 else if (mclk_ps >= 1875)
122 cwl = 6;
123 else if (mclk_ps >= 1500)
124 cwl = 7;
125 else if (mclk_ps >= 1250)
126 cwl = 8;
York Sun7a16d642011-08-24 09:40:25 -0700127 else if (mclk_ps >= 1070)
128 cwl = 9;
129 else if (mclk_ps >= 935)
130 cwl = 10;
131 else if (mclk_ps >= 833)
132 cwl = 11;
133 else if (mclk_ps >= 750)
134 cwl = 12;
135 else {
136 cwl = 12;
137 printf("Warning: CWL is out of range\n");
138 }
Dave Liu4be87b22009-03-14 12:48:30 +0800139 return cwl;
140}
York Sun2896cb72014-03-27 17:54:47 -0700141#endif
Dave Liu4be87b22009-03-14 12:48:30 +0800142
Kumar Gala124b0822008-08-26 15:01:29 -0500143/* Chip Select Configuration (CSn_CONFIG) */
yorkf4f93c62010-07-02 22:25:53 +0000144static void set_csn_config(int dimm_number, int i, fsl_ddr_cfg_regs_t *ddr,
Kumar Gala124b0822008-08-26 15:01:29 -0500145 const memctl_options_t *popts,
146 const dimm_params_t *dimm_params)
147{
148 unsigned int cs_n_en = 0; /* Chip Select enable */
149 unsigned int intlv_en = 0; /* Memory controller interleave enable */
150 unsigned int intlv_ctl = 0; /* Interleaving control */
151 unsigned int ap_n_en = 0; /* Chip select n auto-precharge enable */
152 unsigned int odt_rd_cfg = 0; /* ODT for reads configuration */
153 unsigned int odt_wr_cfg = 0; /* ODT for writes configuration */
154 unsigned int ba_bits_cs_n = 0; /* Num of bank bits for SDRAM on CSn */
155 unsigned int row_bits_cs_n = 0; /* Num of row bits for SDRAM on CSn */
156 unsigned int col_bits_cs_n = 0; /* Num of ocl bits for SDRAM on CSn */
yorkf4f93c62010-07-02 22:25:53 +0000157 int go_config = 0;
York Sun2896cb72014-03-27 17:54:47 -0700158#ifdef CONFIG_SYS_FSL_DDR4
159 unsigned int bg_bits_cs_n = 0; /* Num of bank group bits */
160#else
161 unsigned int n_banks_per_sdram_device;
162#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500163
164 /* Compute CS_CONFIG only for existing ranks of each DIMM. */
yorkf4f93c62010-07-02 22:25:53 +0000165 switch (i) {
166 case 0:
167 if (dimm_params[dimm_number].n_ranks > 0) {
168 go_config = 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500169 /* These fields only available in CS0_CONFIG */
York Sune8dc17b2012-08-17 08:22:39 +0000170 if (!popts->memctl_interleaving)
171 break;
172 switch (popts->memctl_interleaving_mode) {
York Sunc459ae62014-02-10 13:59:44 -0800173 case FSL_DDR_256B_INTERLEAVING:
York Sune8dc17b2012-08-17 08:22:39 +0000174 case FSL_DDR_CACHE_LINE_INTERLEAVING:
175 case FSL_DDR_PAGE_INTERLEAVING:
176 case FSL_DDR_BANK_INTERLEAVING:
177 case FSL_DDR_SUPERBANK_INTERLEAVING:
178 intlv_en = popts->memctl_interleaving;
179 intlv_ctl = popts->memctl_interleaving_mode;
180 break;
181 default:
182 break;
183 }
Kumar Gala124b0822008-08-26 15:01:29 -0500184 }
yorkf4f93c62010-07-02 22:25:53 +0000185 break;
186 case 1:
187 if ((dimm_number == 0 && dimm_params[0].n_ranks > 1) || \
188 (dimm_number == 1 && dimm_params[1].n_ranks > 0))
189 go_config = 1;
190 break;
191 case 2:
192 if ((dimm_number == 0 && dimm_params[0].n_ranks > 2) || \
York Sun15f874a2011-08-26 11:32:40 -0700193 (dimm_number >= 1 && dimm_params[dimm_number].n_ranks > 0))
yorkf4f93c62010-07-02 22:25:53 +0000194 go_config = 1;
195 break;
196 case 3:
197 if ((dimm_number == 0 && dimm_params[0].n_ranks > 3) || \
198 (dimm_number == 1 && dimm_params[1].n_ranks > 1) || \
199 (dimm_number == 3 && dimm_params[3].n_ranks > 0))
200 go_config = 1;
201 break;
202 default:
203 break;
204 }
205 if (go_config) {
yorkf4f93c62010-07-02 22:25:53 +0000206 cs_n_en = 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500207 ap_n_en = popts->cs_local_opts[i].auto_precharge;
208 odt_rd_cfg = popts->cs_local_opts[i].odt_rd_cfg;
209 odt_wr_cfg = popts->cs_local_opts[i].odt_wr_cfg;
York Sun2896cb72014-03-27 17:54:47 -0700210#ifdef CONFIG_SYS_FSL_DDR4
211 ba_bits_cs_n = dimm_params[dimm_number].bank_addr_bits;
212 bg_bits_cs_n = dimm_params[dimm_number].bank_group_bits;
213#else
Kumar Gala124b0822008-08-26 15:01:29 -0500214 n_banks_per_sdram_device
yorkf4f93c62010-07-02 22:25:53 +0000215 = dimm_params[dimm_number].n_banks_per_sdram_device;
Kumar Gala124b0822008-08-26 15:01:29 -0500216 ba_bits_cs_n = __ilog2(n_banks_per_sdram_device) - 2;
York Sun2896cb72014-03-27 17:54:47 -0700217#endif
yorkf4f93c62010-07-02 22:25:53 +0000218 row_bits_cs_n = dimm_params[dimm_number].n_row_addr - 12;
219 col_bits_cs_n = dimm_params[dimm_number].n_col_addr - 8;
Kumar Gala124b0822008-08-26 15:01:29 -0500220 }
Kumar Gala124b0822008-08-26 15:01:29 -0500221 ddr->cs[i].config = (0
222 | ((cs_n_en & 0x1) << 31)
223 | ((intlv_en & 0x3) << 29)
Haiying Wang272b5962008-10-03 12:36:39 -0400224 | ((intlv_ctl & 0xf) << 24)
Kumar Gala124b0822008-08-26 15:01:29 -0500225 | ((ap_n_en & 0x1) << 23)
226
227 /* XXX: some implementation only have 1 bit starting at left */
228 | ((odt_rd_cfg & 0x7) << 20)
229
230 /* XXX: Some implementation only have 1 bit starting at left */
231 | ((odt_wr_cfg & 0x7) << 16)
232
233 | ((ba_bits_cs_n & 0x3) << 14)
234 | ((row_bits_cs_n & 0x7) << 8)
York Sun2896cb72014-03-27 17:54:47 -0700235#ifdef CONFIG_SYS_FSL_DDR4
236 | ((bg_bits_cs_n & 0x3) << 4)
237#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500238 | ((col_bits_cs_n & 0x7) << 0)
239 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400240 debug("FSLDDR: cs[%d]_config = 0x%08x\n", i,ddr->cs[i].config);
Kumar Gala124b0822008-08-26 15:01:29 -0500241}
242
243/* Chip Select Configuration 2 (CSn_CONFIG_2) */
244/* FIXME: 8572 */
245static void set_csn_config_2(int i, fsl_ddr_cfg_regs_t *ddr)
246{
247 unsigned int pasr_cfg = 0; /* Partial array self refresh config */
248
249 ddr->cs[i].config_2 = ((pasr_cfg & 7) << 24);
Haiying Wangd90e0402008-10-03 12:37:26 -0400250 debug("FSLDDR: cs[%d]_config_2 = 0x%08x\n", i, ddr->cs[i].config_2);
Kumar Gala124b0822008-08-26 15:01:29 -0500251}
252
253/* -3E = 667 CL5, -25 = CL6 800, -25E = CL5 800 */
254
York Sunf0626592013-09-30 09:22:09 -0700255#if !defined(CONFIG_SYS_FSL_DDR1)
York Sunfbce88c2014-11-07 12:14:36 -0800256/*
257 * Check DIMM configuration, return 2 if quad-rank or two dual-rank
258 * Return 1 if other two slots configuration. Return 0 if single slot.
259 */
York Sun98df4d12012-10-08 07:44:23 +0000260static inline int avoid_odt_overlap(const dimm_params_t *dimm_params)
261{
262#if CONFIG_DIMM_SLOTS_PER_CTLR == 1
263 if (dimm_params[0].n_ranks == 4)
York Sunfbce88c2014-11-07 12:14:36 -0800264 return 2;
York Sun98df4d12012-10-08 07:44:23 +0000265#endif
266
267#if CONFIG_DIMM_SLOTS_PER_CTLR == 2
268 if ((dimm_params[0].n_ranks == 2) &&
269 (dimm_params[1].n_ranks == 2))
York Sunfbce88c2014-11-07 12:14:36 -0800270 return 2;
York Sun98df4d12012-10-08 07:44:23 +0000271
272#ifdef CONFIG_FSL_DDR_FIRST_SLOT_QUAD_CAPABLE
273 if (dimm_params[0].n_ranks == 4)
York Sunfbce88c2014-11-07 12:14:36 -0800274 return 2;
York Sun98df4d12012-10-08 07:44:23 +0000275#endif
York Sunfbce88c2014-11-07 12:14:36 -0800276
277 if ((dimm_params[0].n_ranks != 0) &&
278 (dimm_params[2].n_ranks != 0))
279 return 1;
York Sun98df4d12012-10-08 07:44:23 +0000280#endif
281 return 0;
282}
283
Kumar Gala124b0822008-08-26 15:01:29 -0500284/*
285 * DDR SDRAM Timing Configuration 0 (TIMING_CFG_0)
286 *
287 * Avoid writing for DDR I. The new PQ38 DDR controller
288 * dreams up non-zero default values to be backwards compatible.
289 */
York Sunba0c2eb2011-01-10 12:03:00 +0000290static void set_timing_cfg_0(fsl_ddr_cfg_regs_t *ddr,
York Sun98df4d12012-10-08 07:44:23 +0000291 const memctl_options_t *popts,
292 const dimm_params_t *dimm_params)
Kumar Gala124b0822008-08-26 15:01:29 -0500293{
294 unsigned char trwt_mclk = 0; /* Read-to-write turnaround */
295 unsigned char twrt_mclk = 0; /* Write-to-read turnaround */
296 /* 7.5 ns on -3E; 0 means WL - CL + BL/2 + 1 */
297 unsigned char trrt_mclk = 0; /* Read-to-read turnaround */
298 unsigned char twwt_mclk = 0; /* Write-to-write turnaround */
299
300 /* Active powerdown exit timing (tXARD and tXARDS). */
301 unsigned char act_pd_exit_mclk;
302 /* Precharge powerdown exit timing (tXP). */
303 unsigned char pre_pd_exit_mclk;
york1714e492010-07-02 22:25:56 +0000304 /* ODT powerdown exit timing (tAXPD). */
York Sun2896cb72014-03-27 17:54:47 -0700305 unsigned char taxpd_mclk = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500306 /* Mode register set cycle time (tMRD). */
307 unsigned char tmrd_mclk;
York Sunc1bf24f2014-08-21 16:13:22 -0700308#if defined(CONFIG_SYS_FSL_DDR4) || defined(CONFIG_SYS_FSL_DDR3)
309 const unsigned int mclk_ps = get_memory_clk_period_ps();
310#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500311
York Sun2896cb72014-03-27 17:54:47 -0700312#ifdef CONFIG_SYS_FSL_DDR4
313 /* tXP=max(4nCK, 6ns) */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900314 int txp = max((int)mclk_ps * 4, 6000); /* unit=ps */
York Sun2896cb72014-03-27 17:54:47 -0700315 trwt_mclk = 2;
316 twrt_mclk = 1;
317 act_pd_exit_mclk = picos_to_mclk(txp);
318 pre_pd_exit_mclk = act_pd_exit_mclk;
319 /*
320 * MRS_CYC = max(tMRD, tMOD)
321 * tMRD = 8nCK, tMOD = max(24nCK, 15ns)
322 */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900323 tmrd_mclk = max(24U, picos_to_mclk(15000));
York Sun2896cb72014-03-27 17:54:47 -0700324#elif defined(CONFIG_SYS_FSL_DDR3)
York Sunc1bf24f2014-08-21 16:13:22 -0700325 unsigned int data_rate = get_ddr_freq(0);
326 int txp;
York Sunfbce88c2014-11-07 12:14:36 -0800327 int odt_overlap;
Dave Liu4be87b22009-03-14 12:48:30 +0800328 /*
329 * (tXARD and tXARDS). Empirical?
330 * The DDR3 spec has not tXARD,
331 * we use the tXP instead of it.
York Sunc1bf24f2014-08-21 16:13:22 -0700332 * tXP=max(3nCK, 7.5ns) for DDR3-800, 1066
333 * max(3nCK, 6ns) for DDR3-1333, 1600, 1866, 2133
Dave Liu4be87b22009-03-14 12:48:30 +0800334 * spec has not the tAXPD, we use
york1714e492010-07-02 22:25:56 +0000335 * tAXPD=1, need design to confirm.
Dave Liu4be87b22009-03-14 12:48:30 +0800336 */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900337 txp = max((int)mclk_ps * 3, (mclk_ps > 1540 ? 7500 : 6000));
York Sunc1bf24f2014-08-21 16:13:22 -0700338
Dave Liu4be87b22009-03-14 12:48:30 +0800339 tmrd_mclk = 4;
Dave Liu81079262009-12-08 11:56:48 +0800340 /* set the turnaround time */
York Sun98df4d12012-10-08 07:44:23 +0000341
342 /*
York Sunfbce88c2014-11-07 12:14:36 -0800343 * for single quad-rank DIMM and two-slot DIMMs
York Sun98df4d12012-10-08 07:44:23 +0000344 * to avoid ODT overlap
345 */
York Sunfbce88c2014-11-07 12:14:36 -0800346 odt_overlap = avoid_odt_overlap(dimm_params);
347 switch (odt_overlap) {
348 case 2:
York Sun98df4d12012-10-08 07:44:23 +0000349 twwt_mclk = 2;
350 trrt_mclk = 1;
York Sunfbce88c2014-11-07 12:14:36 -0800351 break;
352 case 1:
353 twwt_mclk = 1;
354 trrt_mclk = 0;
355 break;
356 default:
357 break;
York Sun98df4d12012-10-08 07:44:23 +0000358 }
York Sunfbce88c2014-11-07 12:14:36 -0800359
York Sun98df4d12012-10-08 07:44:23 +0000360 /* for faster clock, need more time for data setup */
361 trwt_mclk = (data_rate/1000000 > 1800) ? 2 : 1;
362
York Sun27f83be2011-02-10 10:13:10 -0800363 if ((data_rate/1000000 > 1150) || (popts->memctl_interleaving))
364 twrt_mclk = 1;
York Sunba0c2eb2011-01-10 12:03:00 +0000365
366 if (popts->dynamic_power == 0) { /* powerdown is not used */
367 act_pd_exit_mclk = 1;
368 pre_pd_exit_mclk = 1;
369 taxpd_mclk = 1;
370 } else {
371 /* act_pd_exit_mclk = tXARD, see above */
York Sun2896cb72014-03-27 17:54:47 -0700372 act_pd_exit_mclk = picos_to_mclk(txp);
York Sunba0c2eb2011-01-10 12:03:00 +0000373 /* Mode register MR0[A12] is '1' - fast exit */
374 pre_pd_exit_mclk = act_pd_exit_mclk;
375 taxpd_mclk = 1;
376 }
York Sunf0626592013-09-30 09:22:09 -0700377#else /* CONFIG_SYS_FSL_DDR2 */
Dave Liu4be87b22009-03-14 12:48:30 +0800378 /*
379 * (tXARD and tXARDS). Empirical?
380 * tXARD = 2 for DDR2
381 * tXP=2
382 * tAXPD=8
383 */
384 act_pd_exit_mclk = 2;
385 pre_pd_exit_mclk = 2;
386 taxpd_mclk = 8;
Kumar Gala124b0822008-08-26 15:01:29 -0500387 tmrd_mclk = 2;
Dave Liu4be87b22009-03-14 12:48:30 +0800388#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500389
York Sunf8691fc2011-05-27 13:44:28 +0800390 if (popts->trwt_override)
391 trwt_mclk = popts->trwt;
392
Kumar Gala124b0822008-08-26 15:01:29 -0500393 ddr->timing_cfg_0 = (0
394 | ((trwt_mclk & 0x3) << 30) /* RWT */
395 | ((twrt_mclk & 0x3) << 28) /* WRT */
396 | ((trrt_mclk & 0x3) << 26) /* RRT */
397 | ((twwt_mclk & 0x3) << 24) /* WWT */
York Sun63c91cd2013-06-03 12:39:06 -0700398 | ((act_pd_exit_mclk & 0xf) << 20) /* ACT_PD_EXIT */
Dave Liu4758d532008-11-21 16:31:29 +0800399 | ((pre_pd_exit_mclk & 0xF) << 16) /* PRE_PD_EXIT */
Kumar Gala124b0822008-08-26 15:01:29 -0500400 | ((taxpd_mclk & 0xf) << 8) /* ODT_PD_EXIT */
York Sun63c91cd2013-06-03 12:39:06 -0700401 | ((tmrd_mclk & 0x1f) << 0) /* MRS_CYC */
Kumar Gala124b0822008-08-26 15:01:29 -0500402 );
403 debug("FSLDDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
404}
York Sunfbce88c2014-11-07 12:14:36 -0800405#endif /* !defined(CONFIG_SYS_FSL_DDR1) */
Kumar Gala124b0822008-08-26 15:01:29 -0500406
407/* DDR SDRAM Timing Configuration 3 (TIMING_CFG_3) */
408static void set_timing_cfg_3(fsl_ddr_cfg_regs_t *ddr,
York Suncd077cf2012-08-17 08:22:40 +0000409 const memctl_options_t *popts,
Dave Liu4be87b22009-03-14 12:48:30 +0800410 const common_timing_params_t *common_dimm,
York Sun63c91cd2013-06-03 12:39:06 -0700411 unsigned int cas_latency,
412 unsigned int additive_latency)
Kumar Gala124b0822008-08-26 15:01:29 -0500413{
York Suncd077cf2012-08-17 08:22:40 +0000414 /* Extended precharge to activate interval (tRP) */
415 unsigned int ext_pretoact = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500416 /* Extended Activate to precharge interval (tRAS) */
417 unsigned int ext_acttopre = 0;
York Suncd077cf2012-08-17 08:22:40 +0000418 /* Extended activate to read/write interval (tRCD) */
419 unsigned int ext_acttorw = 0;
420 /* Extended refresh recovery time (tRFC) */
421 unsigned int ext_refrec;
422 /* Extended MCAS latency from READ cmd */
423 unsigned int ext_caslat = 0;
York Sun63c91cd2013-06-03 12:39:06 -0700424 /* Extended additive latency */
425 unsigned int ext_add_lat = 0;
York Suncd077cf2012-08-17 08:22:40 +0000426 /* Extended last data to precharge interval (tWR) */
427 unsigned int ext_wrrec = 0;
428 /* Control Adjust */
429 unsigned int cntl_adj = 0;
Dave Liu5c1bb512008-11-21 16:31:22 +0800430
Priyanka Jain4a717412013-09-25 10:41:19 +0530431 ext_pretoact = picos_to_mclk(common_dimm->trp_ps) >> 4;
432 ext_acttopre = picos_to_mclk(common_dimm->tras_ps) >> 4;
433 ext_acttorw = picos_to_mclk(common_dimm->trcd_ps) >> 4;
York Suncd077cf2012-08-17 08:22:40 +0000434 ext_caslat = (2 * cas_latency - 1) >> 4;
York Sun63c91cd2013-06-03 12:39:06 -0700435 ext_add_lat = additive_latency >> 4;
York Sun2896cb72014-03-27 17:54:47 -0700436#ifdef CONFIG_SYS_FSL_DDR4
437 ext_refrec = (picos_to_mclk(common_dimm->trfc1_ps) - 8) >> 4;
438#else
Priyanka Jain4a717412013-09-25 10:41:19 +0530439 ext_refrec = (picos_to_mclk(common_dimm->trfc_ps) - 8) >> 4;
York Suncd077cf2012-08-17 08:22:40 +0000440 /* ext_wrrec only deals with 16 clock and above, or 14 with OTF */
York Sun2896cb72014-03-27 17:54:47 -0700441#endif
Priyanka Jain4a717412013-09-25 10:41:19 +0530442 ext_wrrec = (picos_to_mclk(common_dimm->twr_ps) +
443 (popts->otf_burst_chop_en ? 2 : 0)) >> 4;
Dave Liu4be87b22009-03-14 12:48:30 +0800444
Kumar Gala124b0822008-08-26 15:01:29 -0500445 ddr->timing_cfg_3 = (0
York Suncd077cf2012-08-17 08:22:40 +0000446 | ((ext_pretoact & 0x1) << 28)
James Yang26681f52013-07-22 09:35:26 -0700447 | ((ext_acttopre & 0x3) << 24)
York Suncd077cf2012-08-17 08:22:40 +0000448 | ((ext_acttorw & 0x1) << 22)
449 | ((ext_refrec & 0x1F) << 16)
450 | ((ext_caslat & 0x3) << 12)
York Sun63c91cd2013-06-03 12:39:06 -0700451 | ((ext_add_lat & 0x1) << 10)
York Suncd077cf2012-08-17 08:22:40 +0000452 | ((ext_wrrec & 0x1) << 8)
Kumar Gala124b0822008-08-26 15:01:29 -0500453 | ((cntl_adj & 0x7) << 0)
454 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400455 debug("FSLDDR: timing_cfg_3 = 0x%08x\n", ddr->timing_cfg_3);
Kumar Gala124b0822008-08-26 15:01:29 -0500456}
457
458/* DDR SDRAM Timing Configuration 1 (TIMING_CFG_1) */
459static void set_timing_cfg_1(fsl_ddr_cfg_regs_t *ddr,
Dave Liu4be87b22009-03-14 12:48:30 +0800460 const memctl_options_t *popts,
Kumar Gala124b0822008-08-26 15:01:29 -0500461 const common_timing_params_t *common_dimm,
462 unsigned int cas_latency)
463{
464 /* Precharge-to-activate interval (tRP) */
465 unsigned char pretoact_mclk;
466 /* Activate to precharge interval (tRAS) */
467 unsigned char acttopre_mclk;
468 /* Activate to read/write interval (tRCD) */
469 unsigned char acttorw_mclk;
470 /* CASLAT */
471 unsigned char caslat_ctrl;
472 /* Refresh recovery time (tRFC) ; trfc_low */
473 unsigned char refrec_ctrl;
474 /* Last data to precharge minimum interval (tWR) */
475 unsigned char wrrec_mclk;
476 /* Activate-to-activate interval (tRRD) */
477 unsigned char acttoact_mclk;
478 /* Last write data pair to read command issue interval (tWTR) */
479 unsigned char wrtord_mclk;
York Sun2896cb72014-03-27 17:54:47 -0700480#ifdef CONFIG_SYS_FSL_DDR4
481 /* DDR4 supports 10, 12, 14, 16, 18, 20, 24 */
482 static const u8 wrrec_table[] = {
483 10, 10, 10, 10, 10,
484 10, 10, 10, 10, 10,
485 12, 12, 14, 14, 16,
486 16, 18, 18, 20, 20,
487 24, 24, 24, 24};
488#else
York Sun3673f2c2011-03-02 14:24:11 -0800489 /* DDR_SDRAM_MODE doesn't support 9,11,13,15 */
490 static const u8 wrrec_table[] = {
491 1, 2, 3, 4, 5, 6, 7, 8, 10, 10, 12, 12, 14, 14, 0, 0};
York Sun2896cb72014-03-27 17:54:47 -0700492#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500493
Priyanka Jain4a717412013-09-25 10:41:19 +0530494 pretoact_mclk = picos_to_mclk(common_dimm->trp_ps);
495 acttopre_mclk = picos_to_mclk(common_dimm->tras_ps);
496 acttorw_mclk = picos_to_mclk(common_dimm->trcd_ps);
Kumar Gala124b0822008-08-26 15:01:29 -0500497
498 /*
499 * Translate CAS Latency to a DDR controller field value:
500 *
501 * CAS Lat DDR I DDR II Ctrl
502 * Clocks SPD Bit SPD Bit Value
503 * ------- ------- ------- -----
504 * 1.0 0 0001
505 * 1.5 1 0010
506 * 2.0 2 2 0011
507 * 2.5 3 0100
508 * 3.0 4 3 0101
509 * 3.5 5 0110
510 * 4.0 4 0111
511 * 4.5 1000
512 * 5.0 5 1001
513 */
York Sunf0626592013-09-30 09:22:09 -0700514#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala124b0822008-08-26 15:01:29 -0500515 caslat_ctrl = (cas_latency + 1) & 0x07;
York Sunf0626592013-09-30 09:22:09 -0700516#elif defined(CONFIG_SYS_FSL_DDR2)
Kumar Gala124b0822008-08-26 15:01:29 -0500517 caslat_ctrl = 2 * cas_latency - 1;
518#else
Dave Liu4be87b22009-03-14 12:48:30 +0800519 /*
520 * if the CAS latency more than 8 cycle,
521 * we need set extend bit for it at
522 * TIMING_CFG_3[EXT_CASLAT]
523 */
York Sun2896cb72014-03-27 17:54:47 -0700524 if (fsl_ddr_get_version() <= 0x40400)
525 caslat_ctrl = 2 * cas_latency - 1;
526 else
527 caslat_ctrl = (cas_latency - 1) << 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500528#endif
529
York Sun2896cb72014-03-27 17:54:47 -0700530#ifdef CONFIG_SYS_FSL_DDR4
531 refrec_ctrl = picos_to_mclk(common_dimm->trfc1_ps) - 8;
532 wrrec_mclk = picos_to_mclk(common_dimm->twr_ps);
Masahiro Yamadadb204642014-11-07 03:03:31 +0900533 acttoact_mclk = max(picos_to_mclk(common_dimm->trrds_ps), 4U);
534 wrtord_mclk = max(2U, picos_to_mclk(2500));
York Sunedbeee12014-04-01 14:20:49 -0700535 if ((wrrec_mclk < 1) || (wrrec_mclk > 24))
536 printf("Error: WRREC doesn't support %d clocks\n", wrrec_mclk);
York Sun2896cb72014-03-27 17:54:47 -0700537 else
538 wrrec_mclk = wrrec_table[wrrec_mclk - 1];
539#else
Priyanka Jain4a717412013-09-25 10:41:19 +0530540 refrec_ctrl = picos_to_mclk(common_dimm->trfc_ps) - 8;
541 wrrec_mclk = picos_to_mclk(common_dimm->twr_ps);
York Sun2896cb72014-03-27 17:54:47 -0700542 acttoact_mclk = picos_to_mclk(common_dimm->trrd_ps);
543 wrtord_mclk = picos_to_mclk(common_dimm->twtr_ps);
York Sunedbeee12014-04-01 14:20:49 -0700544 if ((wrrec_mclk < 1) || (wrrec_mclk > 16))
545 printf("Error: WRREC doesn't support %d clocks\n", wrrec_mclk);
York Suncd077cf2012-08-17 08:22:40 +0000546 else
547 wrrec_mclk = wrrec_table[wrrec_mclk - 1];
York Sun2896cb72014-03-27 17:54:47 -0700548#endif
Priyanka Jain4a717412013-09-25 10:41:19 +0530549 if (popts->otf_burst_chop_en)
Dave Liu4be87b22009-03-14 12:48:30 +0800550 wrrec_mclk += 2;
551
Dave Liu4be87b22009-03-14 12:48:30 +0800552 /*
553 * JEDEC has min requirement for tRRD
554 */
York Sunf0626592013-09-30 09:22:09 -0700555#if defined(CONFIG_SYS_FSL_DDR3)
Dave Liu4be87b22009-03-14 12:48:30 +0800556 if (acttoact_mclk < 4)
557 acttoact_mclk = 4;
558#endif
Dave Liu4be87b22009-03-14 12:48:30 +0800559 /*
560 * JEDEC has some min requirements for tWTR
561 */
York Sunf0626592013-09-30 09:22:09 -0700562#if defined(CONFIG_SYS_FSL_DDR2)
Dave Liu4be87b22009-03-14 12:48:30 +0800563 if (wrtord_mclk < 2)
564 wrtord_mclk = 2;
York Sunf0626592013-09-30 09:22:09 -0700565#elif defined(CONFIG_SYS_FSL_DDR3)
Dave Liu4be87b22009-03-14 12:48:30 +0800566 if (wrtord_mclk < 4)
567 wrtord_mclk = 4;
568#endif
Priyanka Jain4a717412013-09-25 10:41:19 +0530569 if (popts->otf_burst_chop_en)
Dave Liu4be87b22009-03-14 12:48:30 +0800570 wrtord_mclk += 2;
Kumar Gala124b0822008-08-26 15:01:29 -0500571
572 ddr->timing_cfg_1 = (0
Dave Liu5c1bb512008-11-21 16:31:22 +0800573 | ((pretoact_mclk & 0x0F) << 28)
Kumar Gala124b0822008-08-26 15:01:29 -0500574 | ((acttopre_mclk & 0x0F) << 24)
Dave Liu5c1bb512008-11-21 16:31:22 +0800575 | ((acttorw_mclk & 0xF) << 20)
Kumar Gala124b0822008-08-26 15:01:29 -0500576 | ((caslat_ctrl & 0xF) << 16)
577 | ((refrec_ctrl & 0xF) << 12)
Dave Liu5c1bb512008-11-21 16:31:22 +0800578 | ((wrrec_mclk & 0x0F) << 8)
York Sun7d69ea32012-10-08 07:44:22 +0000579 | ((acttoact_mclk & 0x0F) << 4)
580 | ((wrtord_mclk & 0x0F) << 0)
Kumar Gala124b0822008-08-26 15:01:29 -0500581 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400582 debug("FSLDDR: timing_cfg_1 = 0x%08x\n", ddr->timing_cfg_1);
Kumar Gala124b0822008-08-26 15:01:29 -0500583}
584
585/* DDR SDRAM Timing Configuration 2 (TIMING_CFG_2) */
586static void set_timing_cfg_2(fsl_ddr_cfg_regs_t *ddr,
587 const memctl_options_t *popts,
588 const common_timing_params_t *common_dimm,
589 unsigned int cas_latency,
590 unsigned int additive_latency)
591{
592 /* Additive latency */
593 unsigned char add_lat_mclk;
594 /* CAS-to-preamble override */
595 unsigned short cpo;
596 /* Write latency */
597 unsigned char wr_lat;
598 /* Read to precharge (tRTP) */
599 unsigned char rd_to_pre;
600 /* Write command to write data strobe timing adjustment */
601 unsigned char wr_data_delay;
602 /* Minimum CKE pulse width (tCKE) */
603 unsigned char cke_pls;
604 /* Window for four activates (tFAW) */
605 unsigned short four_act;
York Sunc1bf24f2014-08-21 16:13:22 -0700606#ifdef CONFIG_SYS_FSL_DDR3
607 const unsigned int mclk_ps = get_memory_clk_period_ps();
608#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500609
610 /* FIXME add check that this must be less than acttorw_mclk */
611 add_lat_mclk = additive_latency;
612 cpo = popts->cpo_override;
613
York Sunf0626592013-09-30 09:22:09 -0700614#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala124b0822008-08-26 15:01:29 -0500615 /*
616 * This is a lie. It should really be 1, but if it is
617 * set to 1, bits overlap into the old controller's
618 * otherwise unused ACSM field. If we leave it 0, then
619 * the HW will magically treat it as 1 for DDR 1. Oh Yea.
620 */
621 wr_lat = 0;
York Sunf0626592013-09-30 09:22:09 -0700622#elif defined(CONFIG_SYS_FSL_DDR2)
Dave Liu82aa9532009-03-14 12:48:19 +0800623 wr_lat = cas_latency - 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500624#else
Dave Liu4be87b22009-03-14 12:48:30 +0800625 wr_lat = compute_cas_write_latency();
Kumar Gala124b0822008-08-26 15:01:29 -0500626#endif
627
York Sun2896cb72014-03-27 17:54:47 -0700628#ifdef CONFIG_SYS_FSL_DDR4
629 rd_to_pre = picos_to_mclk(7500);
630#else
Priyanka Jain4a717412013-09-25 10:41:19 +0530631 rd_to_pre = picos_to_mclk(common_dimm->trtp_ps);
York Sun2896cb72014-03-27 17:54:47 -0700632#endif
Dave Liu4be87b22009-03-14 12:48:30 +0800633 /*
634 * JEDEC has some min requirements for tRTP
635 */
York Sunf0626592013-09-30 09:22:09 -0700636#if defined(CONFIG_SYS_FSL_DDR2)
Dave Liu4be87b22009-03-14 12:48:30 +0800637 if (rd_to_pre < 2)
638 rd_to_pre = 2;
York Sun2896cb72014-03-27 17:54:47 -0700639#elif defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
Dave Liu4be87b22009-03-14 12:48:30 +0800640 if (rd_to_pre < 4)
641 rd_to_pre = 4;
Dave Liu82aa9532009-03-14 12:48:19 +0800642#endif
Priyanka Jain4a717412013-09-25 10:41:19 +0530643 if (popts->otf_burst_chop_en)
Dave Liu4be87b22009-03-14 12:48:30 +0800644 rd_to_pre += 2; /* according to UM */
645
Kumar Gala124b0822008-08-26 15:01:29 -0500646 wr_data_delay = popts->write_data_delay;
York Sun2896cb72014-03-27 17:54:47 -0700647#ifdef CONFIG_SYS_FSL_DDR4
648 cpo = 0;
Masahiro Yamadadb204642014-11-07 03:03:31 +0900649 cke_pls = max(3U, picos_to_mclk(5000));
York Sunc1bf24f2014-08-21 16:13:22 -0700650#elif defined(CONFIG_SYS_FSL_DDR3)
651 /*
652 * cke pulse = max(3nCK, 7.5ns) for DDR3-800
653 * max(3nCK, 5.625ns) for DDR3-1066, 1333
654 * max(3nCK, 5ns) for DDR3-1600, 1866, 2133
655 */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900656 cke_pls = max(3U, picos_to_mclk(mclk_ps > 1870 ? 7500 :
York Sunc1bf24f2014-08-21 16:13:22 -0700657 (mclk_ps > 1245 ? 5625 : 5000)));
York Sun2896cb72014-03-27 17:54:47 -0700658#else
York Sunc1bf24f2014-08-21 16:13:22 -0700659 cke_pls = FSL_DDR_MIN_TCKE_PULSE_WIDTH_DDR;
York Sun2896cb72014-03-27 17:54:47 -0700660#endif
Priyanka Jain4a717412013-09-25 10:41:19 +0530661 four_act = picos_to_mclk(popts->tfaw_window_four_activates_ps);
Kumar Gala124b0822008-08-26 15:01:29 -0500662
663 ddr->timing_cfg_2 = (0
Dave Liu4758d532008-11-21 16:31:29 +0800664 | ((add_lat_mclk & 0xf) << 28)
Kumar Gala124b0822008-08-26 15:01:29 -0500665 | ((cpo & 0x1f) << 23)
Dave Liu4758d532008-11-21 16:31:29 +0800666 | ((wr_lat & 0xf) << 19)
York Sun2896cb72014-03-27 17:54:47 -0700667 | ((wr_lat & 0x10) << 14)
Dave Liu4be87b22009-03-14 12:48:30 +0800668 | ((rd_to_pre & RD_TO_PRE_MASK) << RD_TO_PRE_SHIFT)
669 | ((wr_data_delay & WR_DATA_DELAY_MASK) << WR_DATA_DELAY_SHIFT)
Kumar Gala124b0822008-08-26 15:01:29 -0500670 | ((cke_pls & 0x7) << 6)
Dave Liu4758d532008-11-21 16:31:29 +0800671 | ((four_act & 0x3f) << 0)
Kumar Gala124b0822008-08-26 15:01:29 -0500672 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400673 debug("FSLDDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2);
Kumar Gala124b0822008-08-26 15:01:29 -0500674}
675
yorkde879322010-07-02 22:25:55 +0000676/* DDR SDRAM Register Control Word */
677static void set_ddr_sdram_rcw(fsl_ddr_cfg_regs_t *ddr,
York Sunba0c2eb2011-01-10 12:03:00 +0000678 const memctl_options_t *popts,
yorkde879322010-07-02 22:25:55 +0000679 const common_timing_params_t *common_dimm)
680{
Priyanka Jain4a717412013-09-25 10:41:19 +0530681 if (common_dimm->all_dimms_registered &&
682 !common_dimm->all_dimms_unbuffered) {
York Sunba0c2eb2011-01-10 12:03:00 +0000683 if (popts->rcw_override) {
684 ddr->ddr_sdram_rcw_1 = popts->rcw_1;
685 ddr->ddr_sdram_rcw_2 = popts->rcw_2;
686 } else {
687 ddr->ddr_sdram_rcw_1 =
688 common_dimm->rcw[0] << 28 | \
689 common_dimm->rcw[1] << 24 | \
690 common_dimm->rcw[2] << 20 | \
691 common_dimm->rcw[3] << 16 | \
692 common_dimm->rcw[4] << 12 | \
693 common_dimm->rcw[5] << 8 | \
694 common_dimm->rcw[6] << 4 | \
695 common_dimm->rcw[7];
696 ddr->ddr_sdram_rcw_2 =
697 common_dimm->rcw[8] << 28 | \
698 common_dimm->rcw[9] << 24 | \
699 common_dimm->rcw[10] << 20 | \
700 common_dimm->rcw[11] << 16 | \
701 common_dimm->rcw[12] << 12 | \
702 common_dimm->rcw[13] << 8 | \
703 common_dimm->rcw[14] << 4 | \
704 common_dimm->rcw[15];
705 }
yorkde879322010-07-02 22:25:55 +0000706 debug("FSLDDR: ddr_sdram_rcw_1 = 0x%08x\n", ddr->ddr_sdram_rcw_1);
707 debug("FSLDDR: ddr_sdram_rcw_2 = 0x%08x\n", ddr->ddr_sdram_rcw_2);
708 }
709}
710
Kumar Gala124b0822008-08-26 15:01:29 -0500711/* DDR SDRAM control configuration (DDR_SDRAM_CFG) */
712static void set_ddr_sdram_cfg(fsl_ddr_cfg_regs_t *ddr,
713 const memctl_options_t *popts,
714 const common_timing_params_t *common_dimm)
715{
716 unsigned int mem_en; /* DDR SDRAM interface logic enable */
717 unsigned int sren; /* Self refresh enable (during sleep) */
718 unsigned int ecc_en; /* ECC enable. */
719 unsigned int rd_en; /* Registered DIMM enable */
720 unsigned int sdram_type; /* Type of SDRAM */
721 unsigned int dyn_pwr; /* Dynamic power management mode */
722 unsigned int dbw; /* DRAM dta bus width */
Dave Liu4758d532008-11-21 16:31:29 +0800723 unsigned int eight_be = 0; /* 8-beat burst enable, DDR2 is zero */
Kumar Gala124b0822008-08-26 15:01:29 -0500724 unsigned int ncap = 0; /* Non-concurrent auto-precharge */
Priyanka Jain4a717412013-09-25 10:41:19 +0530725 unsigned int threet_en; /* Enable 3T timing */
726 unsigned int twot_en; /* Enable 2T timing */
Kumar Gala124b0822008-08-26 15:01:29 -0500727 unsigned int ba_intlv_ctl; /* Bank (CS) interleaving control */
728 unsigned int x32_en = 0; /* x32 enable */
729 unsigned int pchb8 = 0; /* precharge bit 8 enable */
730 unsigned int hse; /* Global half strength override */
York Sun5d6c6262014-09-05 13:52:41 +0800731 unsigned int acc_ecc_en = 0; /* Accumulated ECC enable */
Kumar Gala124b0822008-08-26 15:01:29 -0500732 unsigned int mem_halt = 0; /* memory controller halt */
733 unsigned int bi = 0; /* Bypass initialization */
734
735 mem_en = 1;
736 sren = popts->self_refresh_in_sleep;
Priyanka Jain4a717412013-09-25 10:41:19 +0530737 if (common_dimm->all_dimms_ecc_capable) {
Kumar Gala124b0822008-08-26 15:01:29 -0500738 /* Allow setting of ECC only if all DIMMs are ECC. */
Priyanka Jain4a717412013-09-25 10:41:19 +0530739 ecc_en = popts->ecc_mode;
Kumar Gala124b0822008-08-26 15:01:29 -0500740 } else {
741 ecc_en = 0;
742 }
743
Priyanka Jain4a717412013-09-25 10:41:19 +0530744 if (common_dimm->all_dimms_registered &&
745 !common_dimm->all_dimms_unbuffered) {
York Sunba0c2eb2011-01-10 12:03:00 +0000746 rd_en = 1;
Priyanka Jain4a717412013-09-25 10:41:19 +0530747 twot_en = 0;
York Sunba0c2eb2011-01-10 12:03:00 +0000748 } else {
749 rd_en = 0;
Priyanka Jain4a717412013-09-25 10:41:19 +0530750 twot_en = popts->twot_en;
York Sunba0c2eb2011-01-10 12:03:00 +0000751 }
Kumar Gala124b0822008-08-26 15:01:29 -0500752
753 sdram_type = CONFIG_FSL_SDRAM_TYPE;
754
755 dyn_pwr = popts->dynamic_power;
756 dbw = popts->data_bus_width;
Dave Liu4be87b22009-03-14 12:48:30 +0800757 /* 8-beat burst enable DDR-III case
758 * we must clear it when use the on-the-fly mode,
759 * must set it when use the 32-bits bus mode.
760 */
York Sun2896cb72014-03-27 17:54:47 -0700761 if ((sdram_type == SDRAM_TYPE_DDR3) ||
762 (sdram_type == SDRAM_TYPE_DDR4)) {
Dave Liu4be87b22009-03-14 12:48:30 +0800763 if (popts->burst_length == DDR_BL8)
764 eight_be = 1;
765 if (popts->burst_length == DDR_OTF)
766 eight_be = 0;
767 if (dbw == 0x1)
768 eight_be = 1;
769 }
770
Priyanka Jain4a717412013-09-25 10:41:19 +0530771 threet_en = popts->threet_en;
Kumar Gala124b0822008-08-26 15:01:29 -0500772 ba_intlv_ctl = popts->ba_intlv_ctl;
773 hse = popts->half_strength_driver_enable;
774
York Sun5d6c6262014-09-05 13:52:41 +0800775 /* set when ddr bus width < 64 */
776 acc_ecc_en = (dbw != 0 && ecc_en == 1) ? 1 : 0;
777
Kumar Gala124b0822008-08-26 15:01:29 -0500778 ddr->ddr_sdram_cfg = (0
779 | ((mem_en & 0x1) << 31)
780 | ((sren & 0x1) << 30)
781 | ((ecc_en & 0x1) << 29)
782 | ((rd_en & 0x1) << 28)
783 | ((sdram_type & 0x7) << 24)
784 | ((dyn_pwr & 0x1) << 21)
785 | ((dbw & 0x3) << 19)
786 | ((eight_be & 0x1) << 18)
787 | ((ncap & 0x1) << 17)
Priyanka Jain4a717412013-09-25 10:41:19 +0530788 | ((threet_en & 0x1) << 16)
789 | ((twot_en & 0x1) << 15)
Kumar Gala124b0822008-08-26 15:01:29 -0500790 | ((ba_intlv_ctl & 0x7F) << 8)
791 | ((x32_en & 0x1) << 5)
792 | ((pchb8 & 0x1) << 4)
793 | ((hse & 0x1) << 3)
York Sun5d6c6262014-09-05 13:52:41 +0800794 | ((acc_ecc_en & 0x1) << 2)
Kumar Gala124b0822008-08-26 15:01:29 -0500795 | ((mem_halt & 0x1) << 1)
796 | ((bi & 0x1) << 0)
797 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400798 debug("FSLDDR: ddr_sdram_cfg = 0x%08x\n", ddr->ddr_sdram_cfg);
Kumar Gala124b0822008-08-26 15:01:29 -0500799}
800
801/* DDR SDRAM control configuration 2 (DDR_SDRAM_CFG_2) */
802static void set_ddr_sdram_cfg_2(fsl_ddr_cfg_regs_t *ddr,
York Sunba0c2eb2011-01-10 12:03:00 +0000803 const memctl_options_t *popts,
804 const unsigned int unq_mrs_en)
Kumar Gala124b0822008-08-26 15:01:29 -0500805{
806 unsigned int frc_sr = 0; /* Force self refresh */
807 unsigned int sr_ie = 0; /* Self-refresh interrupt enable */
York Sun15f874a2011-08-26 11:32:40 -0700808 unsigned int odt_cfg = 0; /* ODT configuration */
Kumar Gala124b0822008-08-26 15:01:29 -0500809 unsigned int num_pr; /* Number of posted refreshes */
York Sun7d69ea32012-10-08 07:44:22 +0000810 unsigned int slow = 0; /* DDR will be run less than 1250 */
York Sun4889c982013-06-25 11:37:47 -0700811 unsigned int x4_en = 0; /* x4 DRAM enable */
Kumar Gala124b0822008-08-26 15:01:29 -0500812 unsigned int obc_cfg; /* On-The-Fly Burst Chop Cfg */
813 unsigned int ap_en; /* Address Parity Enable */
814 unsigned int d_init; /* DRAM data initialization */
815 unsigned int rcw_en = 0; /* Register Control Word Enable */
816 unsigned int md_en = 0; /* Mirrored DIMM Enable */
yorkf4f93c62010-07-02 22:25:53 +0000817 unsigned int qd_en = 0; /* quad-rank DIMM Enable */
York Sun15f874a2011-08-26 11:32:40 -0700818 int i;
York Sun2896cb72014-03-27 17:54:47 -0700819#ifndef CONFIG_SYS_FSL_DDR4
820 unsigned int dll_rst_dis = 1; /* DLL reset disable */
821 unsigned int dqs_cfg; /* DQS configuration */
Kumar Gala124b0822008-08-26 15:01:29 -0500822
Priyanka Jain4a717412013-09-25 10:41:19 +0530823 dqs_cfg = popts->dqs_config;
York Sun2896cb72014-03-27 17:54:47 -0700824#endif
York Sun15f874a2011-08-26 11:32:40 -0700825 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
826 if (popts->cs_local_opts[i].odt_rd_cfg
827 || popts->cs_local_opts[i].odt_wr_cfg) {
828 odt_cfg = SDRAM_CFG2_ODT_ONLY_READ;
829 break;
830 }
Kumar Gala124b0822008-08-26 15:01:29 -0500831 }
832
833 num_pr = 1; /* Make this configurable */
834
835 /*
836 * 8572 manual says
837 * {TIMING_CFG_1[PRETOACT]
838 * + [DDR_SDRAM_CFG_2[NUM_PR]
839 * * ({EXT_REFREC || REFREC} + 8 + 2)]}
840 * << DDR_SDRAM_INTERVAL[REFINT]
841 */
York Sun2896cb72014-03-27 17:54:47 -0700842#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
Priyanka Jain4a717412013-09-25 10:41:19 +0530843 obc_cfg = popts->otf_burst_chop_en;
Dave Liu4be87b22009-03-14 12:48:30 +0800844#else
845 obc_cfg = 0;
846#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500847
York Sun7d69ea32012-10-08 07:44:22 +0000848#if (CONFIG_SYS_FSL_DDR_VER >= FSL_DDR_VER_4_7)
849 slow = get_ddr_freq(0) < 1249000000;
850#endif
851
York Sunba0c2eb2011-01-10 12:03:00 +0000852 if (popts->registered_dimm_en) {
853 rcw_en = 1;
854 ap_en = popts->ap_en;
855 } else {
York Sunba0c2eb2011-01-10 12:03:00 +0000856 ap_en = 0;
857 }
Kumar Gala124b0822008-08-26 15:01:29 -0500858
York Sun4889c982013-06-25 11:37:47 -0700859 x4_en = popts->x4_en ? 1 : 0;
860
Kumar Gala124b0822008-08-26 15:01:29 -0500861#if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
862 /* Use the DDR controller to auto initialize memory. */
Priyanka Jain4a717412013-09-25 10:41:19 +0530863 d_init = popts->ecc_init_using_memctl;
Kumar Gala124b0822008-08-26 15:01:29 -0500864 ddr->ddr_data_init = CONFIG_MEM_INIT_VALUE;
865 debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init);
866#else
867 /* Memory will be initialized via DMA, or not at all. */
868 d_init = 0;
869#endif
870
York Sun2896cb72014-03-27 17:54:47 -0700871#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
Dave Liu4be87b22009-03-14 12:48:30 +0800872 md_en = popts->mirrored_dimm;
873#endif
yorkf4f93c62010-07-02 22:25:53 +0000874 qd_en = popts->quad_rank_present ? 1 : 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500875 ddr->ddr_sdram_cfg_2 = (0
876 | ((frc_sr & 0x1) << 31)
877 | ((sr_ie & 0x1) << 30)
York Sun2896cb72014-03-27 17:54:47 -0700878#ifndef CONFIG_SYS_FSL_DDR4
Kumar Gala124b0822008-08-26 15:01:29 -0500879 | ((dll_rst_dis & 0x1) << 29)
880 | ((dqs_cfg & 0x3) << 26)
York Sun2896cb72014-03-27 17:54:47 -0700881#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500882 | ((odt_cfg & 0x3) << 21)
883 | ((num_pr & 0xf) << 12)
York Sun7d69ea32012-10-08 07:44:22 +0000884 | ((slow & 1) << 11)
York Sun4889c982013-06-25 11:37:47 -0700885 | (x4_en << 10)
yorkf4f93c62010-07-02 22:25:53 +0000886 | (qd_en << 9)
York Sunba0c2eb2011-01-10 12:03:00 +0000887 | (unq_mrs_en << 8)
Kumar Gala124b0822008-08-26 15:01:29 -0500888 | ((obc_cfg & 0x1) << 6)
889 | ((ap_en & 0x1) << 5)
890 | ((d_init & 0x1) << 4)
891 | ((rcw_en & 0x1) << 2)
892 | ((md_en & 0x1) << 0)
893 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400894 debug("FSLDDR: ddr_sdram_cfg_2 = 0x%08x\n", ddr->ddr_sdram_cfg_2);
Kumar Gala124b0822008-08-26 15:01:29 -0500895}
896
York Sun2896cb72014-03-27 17:54:47 -0700897#ifdef CONFIG_SYS_FSL_DDR4
Kumar Gala124b0822008-08-26 15:01:29 -0500898/* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
Dave Liu2d0f1252009-12-16 10:24:38 -0600899static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr,
York Sunba0c2eb2011-01-10 12:03:00 +0000900 const memctl_options_t *popts,
Valentin Longchamp0b810932013-10-18 11:47:20 +0200901 const common_timing_params_t *common_dimm,
York Sunba0c2eb2011-01-10 12:03:00 +0000902 const unsigned int unq_mrs_en)
Kumar Gala124b0822008-08-26 15:01:29 -0500903{
904 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
905 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
York Sun2896cb72014-03-27 17:54:47 -0700906 int i;
907 unsigned int wr_crc = 0; /* Disable */
908 unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */
909 unsigned int srt = 0; /* self-refresh temerature, normal range */
910 unsigned int cwl = compute_cas_write_latency() - 9;
911 unsigned int mpr = 0; /* serial */
912 unsigned int wc_lat;
913 const unsigned int mclk_ps = get_memory_clk_period_ps();
Kumar Gala124b0822008-08-26 15:01:29 -0500914
York Sun2896cb72014-03-27 17:54:47 -0700915 if (popts->rtt_override)
916 rtt_wr = popts->rtt_wr_override_value;
917 else
918 rtt_wr = popts->cs_local_opts[0].odt_rtt_wr;
919
920 if (common_dimm->extended_op_srt)
921 srt = common_dimm->extended_op_srt;
922
923 esdmode2 = (0
924 | ((wr_crc & 0x1) << 12)
925 | ((rtt_wr & 0x3) << 9)
926 | ((srt & 0x3) << 6)
927 | ((cwl & 0x7) << 3));
928
929 if (mclk_ps >= 1250)
930 wc_lat = 0;
931 else if (mclk_ps >= 833)
932 wc_lat = 1;
933 else
934 wc_lat = 2;
935
936 esdmode3 = (0
937 | ((mpr & 0x3) << 11)
938 | ((wc_lat & 0x3) << 9));
939
940 ddr->ddr_sdram_mode_2 = (0
941 | ((esdmode2 & 0xFFFF) << 16)
942 | ((esdmode3 & 0xFFFF) << 0)
943 );
944 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
945
946 if (unq_mrs_en) { /* unique mode registers are supported */
947 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
948 if (popts->rtt_override)
949 rtt_wr = popts->rtt_wr_override_value;
950 else
951 rtt_wr = popts->cs_local_opts[i].odt_rtt_wr;
952
953 esdmode2 &= 0xF9FF; /* clear bit 10, 9 */
954 esdmode2 |= (rtt_wr & 0x3) << 9;
955 switch (i) {
956 case 1:
957 ddr->ddr_sdram_mode_4 = (0
958 | ((esdmode2 & 0xFFFF) << 16)
959 | ((esdmode3 & 0xFFFF) << 0)
960 );
961 break;
962 case 2:
963 ddr->ddr_sdram_mode_6 = (0
964 | ((esdmode2 & 0xFFFF) << 16)
965 | ((esdmode3 & 0xFFFF) << 0)
966 );
967 break;
968 case 3:
969 ddr->ddr_sdram_mode_8 = (0
970 | ((esdmode2 & 0xFFFF) << 16)
971 | ((esdmode3 & 0xFFFF) << 0)
972 );
973 break;
974 }
975 }
976 debug("FSLDDR: ddr_sdram_mode_4 = 0x%08x\n",
977 ddr->ddr_sdram_mode_4);
978 debug("FSLDDR: ddr_sdram_mode_6 = 0x%08x\n",
979 ddr->ddr_sdram_mode_6);
980 debug("FSLDDR: ddr_sdram_mode_8 = 0x%08x\n",
981 ddr->ddr_sdram_mode_8);
982 }
983}
984#elif defined(CONFIG_SYS_FSL_DDR3)
985/* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
986static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr,
987 const memctl_options_t *popts,
988 const common_timing_params_t *common_dimm,
989 const unsigned int unq_mrs_en)
990{
991 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
992 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
Kumar Gala65b5be22011-01-20 01:53:15 -0600993 int i;
Dave Liu2d0f1252009-12-16 10:24:38 -0600994 unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */
Dave Liu4be87b22009-03-14 12:48:30 +0800995 unsigned int srt = 0; /* self-refresh temerature, normal range */
996 unsigned int asr = 0; /* auto self-refresh disable */
997 unsigned int cwl = compute_cas_write_latency() - 5;
998 unsigned int pasr = 0; /* partial array self refresh disable */
999
Dave Liu2d0f1252009-12-16 10:24:38 -06001000 if (popts->rtt_override)
1001 rtt_wr = popts->rtt_wr_override_value;
York Sunba0c2eb2011-01-10 12:03:00 +00001002 else
1003 rtt_wr = popts->cs_local_opts[0].odt_rtt_wr;
Valentin Longchamp0b810932013-10-18 11:47:20 +02001004
1005 if (common_dimm->extended_op_srt)
1006 srt = common_dimm->extended_op_srt;
1007
Dave Liu4be87b22009-03-14 12:48:30 +08001008 esdmode2 = (0
1009 | ((rtt_wr & 0x3) << 9)
1010 | ((srt & 0x1) << 7)
1011 | ((asr & 0x1) << 6)
1012 | ((cwl & 0x7) << 3)
1013 | ((pasr & 0x7) << 0));
Kumar Gala124b0822008-08-26 15:01:29 -05001014 ddr->ddr_sdram_mode_2 = (0
1015 | ((esdmode2 & 0xFFFF) << 16)
1016 | ((esdmode3 & 0xFFFF) << 0)
1017 );
Haiying Wangd90e0402008-10-03 12:37:26 -04001018 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
York Sunba0c2eb2011-01-10 12:03:00 +00001019
York Sunba0c2eb2011-01-10 12:03:00 +00001020 if (unq_mrs_en) { /* unique mode registers are supported */
Kumar Galad5bbe662011-11-09 10:05:10 -06001021 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Sunba0c2eb2011-01-10 12:03:00 +00001022 if (popts->rtt_override)
1023 rtt_wr = popts->rtt_wr_override_value;
1024 else
1025 rtt_wr = popts->cs_local_opts[i].odt_rtt_wr;
1026
1027 esdmode2 &= 0xF9FF; /* clear bit 10, 9 */
1028 esdmode2 |= (rtt_wr & 0x3) << 9;
1029 switch (i) {
1030 case 1:
1031 ddr->ddr_sdram_mode_4 = (0
1032 | ((esdmode2 & 0xFFFF) << 16)
1033 | ((esdmode3 & 0xFFFF) << 0)
1034 );
1035 break;
1036 case 2:
1037 ddr->ddr_sdram_mode_6 = (0
1038 | ((esdmode2 & 0xFFFF) << 16)
1039 | ((esdmode3 & 0xFFFF) << 0)
1040 );
1041 break;
1042 case 3:
1043 ddr->ddr_sdram_mode_8 = (0
1044 | ((esdmode2 & 0xFFFF) << 16)
1045 | ((esdmode3 & 0xFFFF) << 0)
1046 );
1047 break;
1048 }
1049 }
1050 debug("FSLDDR: ddr_sdram_mode_4 = 0x%08x\n",
1051 ddr->ddr_sdram_mode_4);
1052 debug("FSLDDR: ddr_sdram_mode_6 = 0x%08x\n",
1053 ddr->ddr_sdram_mode_6);
1054 debug("FSLDDR: ddr_sdram_mode_8 = 0x%08x\n",
1055 ddr->ddr_sdram_mode_8);
1056 }
York Sun2896cb72014-03-27 17:54:47 -07001057}
1058
1059#else /* for DDR2 and DDR1 */
1060/* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
1061static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr,
1062 const memctl_options_t *popts,
1063 const common_timing_params_t *common_dimm,
1064 const unsigned int unq_mrs_en)
1065{
1066 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
1067 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
1068
1069 ddr->ddr_sdram_mode_2 = (0
1070 | ((esdmode2 & 0xFFFF) << 16)
1071 | ((esdmode3 & 0xFFFF) << 0)
1072 );
1073 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
1074}
York Sunba0c2eb2011-01-10 12:03:00 +00001075#endif
York Sun2896cb72014-03-27 17:54:47 -07001076
1077#ifdef CONFIG_SYS_FSL_DDR4
1078/* DDR SDRAM Mode configuration 9 (DDR_SDRAM_MODE_9) */
1079static void set_ddr_sdram_mode_9(fsl_ddr_cfg_regs_t *ddr,
1080 const memctl_options_t *popts,
1081 const common_timing_params_t *common_dimm,
1082 const unsigned int unq_mrs_en)
1083{
1084 int i;
1085 unsigned short esdmode4 = 0; /* Extended SDRAM mode 4 */
1086 unsigned short esdmode5; /* Extended SDRAM mode 5 */
1087
1088 esdmode5 = 0x00000400; /* Data mask enabled */
1089
1090 ddr->ddr_sdram_mode_9 = (0
1091 | ((esdmode4 & 0xffff) << 16)
1092 | ((esdmode5 & 0xffff) << 0)
1093 );
1094 debug("FSLDDR: ddr_sdram_mode_9) = 0x%08x\n", ddr->ddr_sdram_mode_9);
1095 if (unq_mrs_en) { /* unique mode registers are supported */
1096 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1097 switch (i) {
1098 case 1:
1099 ddr->ddr_sdram_mode_11 = (0
1100 | ((esdmode4 & 0xFFFF) << 16)
1101 | ((esdmode5 & 0xFFFF) << 0)
1102 );
1103 break;
1104 case 2:
1105 ddr->ddr_sdram_mode_13 = (0
1106 | ((esdmode4 & 0xFFFF) << 16)
1107 | ((esdmode5 & 0xFFFF) << 0)
1108 );
1109 break;
1110 case 3:
1111 ddr->ddr_sdram_mode_15 = (0
1112 | ((esdmode4 & 0xFFFF) << 16)
1113 | ((esdmode5 & 0xFFFF) << 0)
1114 );
1115 break;
1116 }
1117 }
1118 debug("FSLDDR: ddr_sdram_mode_11 = 0x%08x\n",
1119 ddr->ddr_sdram_mode_11);
1120 debug("FSLDDR: ddr_sdram_mode_13 = 0x%08x\n",
1121 ddr->ddr_sdram_mode_13);
1122 debug("FSLDDR: ddr_sdram_mode_15 = 0x%08x\n",
1123 ddr->ddr_sdram_mode_15);
1124 }
Kumar Gala124b0822008-08-26 15:01:29 -05001125}
1126
York Sun2896cb72014-03-27 17:54:47 -07001127/* DDR SDRAM Mode configuration 10 (DDR_SDRAM_MODE_10) */
1128static void set_ddr_sdram_mode_10(fsl_ddr_cfg_regs_t *ddr,
1129 const memctl_options_t *popts,
1130 const common_timing_params_t *common_dimm,
1131 const unsigned int unq_mrs_en)
1132{
1133 int i;
1134 unsigned short esdmode6 = 0; /* Extended SDRAM mode 6 */
1135 unsigned short esdmode7 = 0; /* Extended SDRAM mode 7 */
1136 unsigned int tccdl_min = picos_to_mclk(common_dimm->tccdl_ps);
1137
1138 esdmode6 = ((tccdl_min - 4) & 0x7) << 10;
1139
1140 ddr->ddr_sdram_mode_10 = (0
1141 | ((esdmode6 & 0xffff) << 16)
1142 | ((esdmode7 & 0xffff) << 0)
1143 );
1144 debug("FSLDDR: ddr_sdram_mode_10) = 0x%08x\n", ddr->ddr_sdram_mode_10);
1145 if (unq_mrs_en) { /* unique mode registers are supported */
1146 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1147 switch (i) {
1148 case 1:
1149 ddr->ddr_sdram_mode_12 = (0
1150 | ((esdmode6 & 0xFFFF) << 16)
1151 | ((esdmode7 & 0xFFFF) << 0)
1152 );
1153 break;
1154 case 2:
1155 ddr->ddr_sdram_mode_14 = (0
1156 | ((esdmode6 & 0xFFFF) << 16)
1157 | ((esdmode7 & 0xFFFF) << 0)
1158 );
1159 break;
1160 case 3:
1161 ddr->ddr_sdram_mode_16 = (0
1162 | ((esdmode6 & 0xFFFF) << 16)
1163 | ((esdmode7 & 0xFFFF) << 0)
1164 );
1165 break;
1166 }
1167 }
1168 debug("FSLDDR: ddr_sdram_mode_12 = 0x%08x\n",
1169 ddr->ddr_sdram_mode_12);
1170 debug("FSLDDR: ddr_sdram_mode_14 = 0x%08x\n",
1171 ddr->ddr_sdram_mode_14);
1172 debug("FSLDDR: ddr_sdram_mode_16 = 0x%08x\n",
1173 ddr->ddr_sdram_mode_16);
1174 }
1175}
1176
1177#endif
1178
Kumar Gala124b0822008-08-26 15:01:29 -05001179/* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */
1180static void set_ddr_sdram_interval(fsl_ddr_cfg_regs_t *ddr,
1181 const memctl_options_t *popts,
1182 const common_timing_params_t *common_dimm)
1183{
1184 unsigned int refint; /* Refresh interval */
1185 unsigned int bstopre; /* Precharge interval */
1186
1187 refint = picos_to_mclk(common_dimm->refresh_rate_ps);
1188
1189 bstopre = popts->bstopre;
1190
1191 /* refint field used 0x3FFF in earlier controllers */
1192 ddr->ddr_sdram_interval = (0
1193 | ((refint & 0xFFFF) << 16)
1194 | ((bstopre & 0x3FFF) << 0)
1195 );
Haiying Wangd90e0402008-10-03 12:37:26 -04001196 debug("FSLDDR: ddr_sdram_interval = 0x%08x\n", ddr->ddr_sdram_interval);
Kumar Gala124b0822008-08-26 15:01:29 -05001197}
1198
York Sun2896cb72014-03-27 17:54:47 -07001199#ifdef CONFIG_SYS_FSL_DDR4
Kumar Gala124b0822008-08-26 15:01:29 -05001200/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
1201static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
1202 const memctl_options_t *popts,
1203 const common_timing_params_t *common_dimm,
1204 unsigned int cas_latency,
York Sunba0c2eb2011-01-10 12:03:00 +00001205 unsigned int additive_latency,
1206 const unsigned int unq_mrs_en)
Kumar Gala124b0822008-08-26 15:01:29 -05001207{
York Sun2896cb72014-03-27 17:54:47 -07001208 int i;
Kumar Gala124b0822008-08-26 15:01:29 -05001209 unsigned short esdmode; /* Extended SDRAM mode */
1210 unsigned short sdmode; /* SDRAM mode */
1211
Dave Liu4be87b22009-03-14 12:48:30 +08001212 /* Mode Register - MR1 */
1213 unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */
1214 unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */
1215 unsigned int rtt;
1216 unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */
1217 unsigned int al = 0; /* Posted CAS# additive latency (AL) */
York Sunba0c2eb2011-01-10 12:03:00 +00001218 unsigned int dic = 0; /* Output driver impedance, 40ohm */
York Sun2896cb72014-03-27 17:54:47 -07001219 unsigned int dll_en = 1; /* DLL Enable 1=Enable (Normal),
1220 0=Disable (Test/Debug) */
1221
1222 /* Mode Register - MR0 */
1223 unsigned int wr = 0; /* Write Recovery */
1224 unsigned int dll_rst; /* DLL Reset */
1225 unsigned int mode; /* Normal=0 or Test=1 */
1226 unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
1227 /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
1228 unsigned int bt;
1229 unsigned int bl; /* BL: Burst Length */
1230
1231 unsigned int wr_mclk;
1232 /* DDR4 support WR 10, 12, 14, 16, 18, 20, 24 */
1233 static const u8 wr_table[] = {
1234 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6};
1235 /* DDR4 support CAS 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 22, 24 */
1236 static const u8 cas_latency_table[] = {
1237 0, 1, 2, 3, 4, 5, 6, 7, 8, 8,
1238 9, 9, 10, 10, 11, 11};
1239
1240 if (popts->rtt_override)
1241 rtt = popts->rtt_override_value;
1242 else
1243 rtt = popts->cs_local_opts[0].odt_rtt_norm;
1244
1245 if (additive_latency == (cas_latency - 1))
1246 al = 1;
1247 if (additive_latency == (cas_latency - 2))
1248 al = 2;
1249
1250 if (popts->quad_rank_present)
1251 dic = 1; /* output driver impedance 240/7 ohm */
1252
1253 /*
1254 * The esdmode value will also be used for writing
1255 * MR1 during write leveling for DDR3, although the
1256 * bits specifically related to the write leveling
1257 * scheme will be handled automatically by the DDR
1258 * controller. so we set the wrlvl_en = 0 here.
1259 */
1260 esdmode = (0
1261 | ((qoff & 0x1) << 12)
1262 | ((tdqs_en & 0x1) << 11)
1263 | ((rtt & 0x7) << 8)
1264 | ((wrlvl_en & 0x1) << 7)
1265 | ((al & 0x3) << 3)
1266 | ((dic & 0x3) << 1) /* DIC field is split */
1267 | ((dll_en & 0x1) << 0)
1268 );
1269
1270 /*
1271 * DLL control for precharge PD
1272 * 0=slow exit DLL off (tXPDLL)
1273 * 1=fast exit DLL on (tXP)
1274 */
1275
1276 wr_mclk = picos_to_mclk(common_dimm->twr_ps);
1277 if (wr_mclk <= 24) {
1278 wr = wr_table[wr_mclk - 10];
1279 } else {
1280 printf("Error: unsupported write recovery for mode register wr_mclk = %d\n",
1281 wr_mclk);
1282 }
1283
1284 dll_rst = 0; /* dll no reset */
1285 mode = 0; /* normal mode */
1286
1287 /* look up table to get the cas latency bits */
1288 if (cas_latency >= 9 && cas_latency <= 24)
1289 caslat = cas_latency_table[cas_latency - 9];
1290 else
1291 printf("Error: unsupported cas latency for mode register\n");
1292
1293 bt = 0; /* Nibble sequential */
1294
1295 switch (popts->burst_length) {
1296 case DDR_BL8:
1297 bl = 0;
1298 break;
1299 case DDR_OTF:
1300 bl = 1;
1301 break;
1302 case DDR_BC4:
1303 bl = 2;
1304 break;
1305 default:
1306 printf("Error: invalid burst length of %u specified. ",
1307 popts->burst_length);
1308 puts("Defaulting to on-the-fly BC4 or BL8 beats.\n");
1309 bl = 1;
1310 break;
1311 }
1312
1313 sdmode = (0
1314 | ((wr & 0x7) << 9)
1315 | ((dll_rst & 0x1) << 8)
1316 | ((mode & 0x1) << 7)
1317 | (((caslat >> 1) & 0x7) << 4)
1318 | ((bt & 0x1) << 3)
1319 | ((caslat & 1) << 2)
1320 | ((bl & 0x3) << 0)
1321 );
1322
1323 ddr->ddr_sdram_mode = (0
1324 | ((esdmode & 0xFFFF) << 16)
1325 | ((sdmode & 0xFFFF) << 0)
1326 );
1327
1328 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
1329
1330 if (unq_mrs_en) { /* unique mode registers are supported */
1331 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1332 if (popts->rtt_override)
1333 rtt = popts->rtt_override_value;
1334 else
1335 rtt = popts->cs_local_opts[i].odt_rtt_norm;
1336
1337 esdmode &= 0xF8FF; /* clear bit 10,9,8 for rtt */
1338 esdmode |= (rtt & 0x7) << 8;
1339 switch (i) {
1340 case 1:
1341 ddr->ddr_sdram_mode_3 = (0
1342 | ((esdmode & 0xFFFF) << 16)
1343 | ((sdmode & 0xFFFF) << 0)
1344 );
1345 break;
1346 case 2:
1347 ddr->ddr_sdram_mode_5 = (0
1348 | ((esdmode & 0xFFFF) << 16)
1349 | ((sdmode & 0xFFFF) << 0)
1350 );
1351 break;
1352 case 3:
1353 ddr->ddr_sdram_mode_7 = (0
1354 | ((esdmode & 0xFFFF) << 16)
1355 | ((sdmode & 0xFFFF) << 0)
1356 );
1357 break;
1358 }
1359 }
1360 debug("FSLDDR: ddr_sdram_mode_3 = 0x%08x\n",
1361 ddr->ddr_sdram_mode_3);
1362 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1363 ddr->ddr_sdram_mode_5);
1364 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1365 ddr->ddr_sdram_mode_5);
1366 }
1367}
1368
1369#elif defined(CONFIG_SYS_FSL_DDR3)
1370/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
1371static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
1372 const memctl_options_t *popts,
1373 const common_timing_params_t *common_dimm,
1374 unsigned int cas_latency,
1375 unsigned int additive_latency,
1376 const unsigned int unq_mrs_en)
1377{
1378 int i;
1379 unsigned short esdmode; /* Extended SDRAM mode */
1380 unsigned short sdmode; /* SDRAM mode */
1381
1382 /* Mode Register - MR1 */
1383 unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */
1384 unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */
1385 unsigned int rtt;
1386 unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */
1387 unsigned int al = 0; /* Posted CAS# additive latency (AL) */
1388 unsigned int dic = 0; /* Output driver impedance, 40ohm */
Dave Liu4be87b22009-03-14 12:48:30 +08001389 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
1390 1=Disable (Test/Debug) */
1391
1392 /* Mode Register - MR0 */
1393 unsigned int dll_on; /* DLL control for precharge PD, 0=off, 1=on */
York Sunbad82092012-08-17 08:22:38 +00001394 unsigned int wr = 0; /* Write Recovery */
Dave Liu4be87b22009-03-14 12:48:30 +08001395 unsigned int dll_rst; /* DLL Reset */
1396 unsigned int mode; /* Normal=0 or Test=1 */
1397 unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
1398 /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
1399 unsigned int bt;
1400 unsigned int bl; /* BL: Burst Length */
1401
1402 unsigned int wr_mclk;
York Sun3673f2c2011-03-02 14:24:11 -08001403 /*
1404 * DDR_SDRAM_MODE doesn't support 9,11,13,15
1405 * Please refer JEDEC Standard No. 79-3E for Mode Register MR0
1406 * for this table
1407 */
1408 static const u8 wr_table[] = {1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 0, 0};
Dave Liu4be87b22009-03-14 12:48:30 +08001409
Dave Liu4be87b22009-03-14 12:48:30 +08001410 if (popts->rtt_override)
1411 rtt = popts->rtt_override_value;
York Sunba0c2eb2011-01-10 12:03:00 +00001412 else
1413 rtt = popts->cs_local_opts[0].odt_rtt_norm;
Dave Liu4be87b22009-03-14 12:48:30 +08001414
1415 if (additive_latency == (cas_latency - 1))
1416 al = 1;
1417 if (additive_latency == (cas_latency - 2))
1418 al = 2;
1419
York Sunba0c2eb2011-01-10 12:03:00 +00001420 if (popts->quad_rank_present)
1421 dic = 1; /* output driver impedance 240/7 ohm */
1422
Dave Liu4be87b22009-03-14 12:48:30 +08001423 /*
1424 * The esdmode value will also be used for writing
1425 * MR1 during write leveling for DDR3, although the
1426 * bits specifically related to the write leveling
1427 * scheme will be handled automatically by the DDR
1428 * controller. so we set the wrlvl_en = 0 here.
1429 */
1430 esdmode = (0
1431 | ((qoff & 0x1) << 12)
1432 | ((tdqs_en & 0x1) << 11)
Kumar Gala14f2eb12009-09-10 14:54:55 -05001433 | ((rtt & 0x4) << 7) /* rtt field is split */
Dave Liu4be87b22009-03-14 12:48:30 +08001434 | ((wrlvl_en & 0x1) << 7)
Kumar Gala14f2eb12009-09-10 14:54:55 -05001435 | ((rtt & 0x2) << 5) /* rtt field is split */
1436 | ((dic & 0x2) << 4) /* DIC field is split */
Dave Liu4be87b22009-03-14 12:48:30 +08001437 | ((al & 0x3) << 3)
Kumar Gala14f2eb12009-09-10 14:54:55 -05001438 | ((rtt & 0x1) << 2) /* rtt field is split */
Dave Liu4be87b22009-03-14 12:48:30 +08001439 | ((dic & 0x1) << 1) /* DIC field is split */
1440 | ((dll_en & 0x1) << 0)
1441 );
1442
1443 /*
1444 * DLL control for precharge PD
1445 * 0=slow exit DLL off (tXPDLL)
1446 * 1=fast exit DLL on (tXP)
1447 */
1448 dll_on = 1;
York Sun3673f2c2011-03-02 14:24:11 -08001449
York Sun2896cb72014-03-27 17:54:47 -07001450 wr_mclk = picos_to_mclk(common_dimm->twr_ps);
York Sunbad82092012-08-17 08:22:38 +00001451 if (wr_mclk <= 16) {
1452 wr = wr_table[wr_mclk - 5];
1453 } else {
1454 printf("Error: unsupported write recovery for mode register "
1455 "wr_mclk = %d\n", wr_mclk);
1456 }
York Sun3673f2c2011-03-02 14:24:11 -08001457
Dave Liu4be87b22009-03-14 12:48:30 +08001458 dll_rst = 0; /* dll no reset */
1459 mode = 0; /* normal mode */
1460
1461 /* look up table to get the cas latency bits */
York Sunbad82092012-08-17 08:22:38 +00001462 if (cas_latency >= 5 && cas_latency <= 16) {
1463 unsigned char cas_latency_table[] = {
Dave Liu4be87b22009-03-14 12:48:30 +08001464 0x2, /* 5 clocks */
1465 0x4, /* 6 clocks */
1466 0x6, /* 7 clocks */
1467 0x8, /* 8 clocks */
1468 0xa, /* 9 clocks */
1469 0xc, /* 10 clocks */
York Sunbad82092012-08-17 08:22:38 +00001470 0xe, /* 11 clocks */
1471 0x1, /* 12 clocks */
1472 0x3, /* 13 clocks */
1473 0x5, /* 14 clocks */
1474 0x7, /* 15 clocks */
1475 0x9, /* 16 clocks */
Dave Liu4be87b22009-03-14 12:48:30 +08001476 };
1477 caslat = cas_latency_table[cas_latency - 5];
York Sunbad82092012-08-17 08:22:38 +00001478 } else {
1479 printf("Error: unsupported cas latency for mode register\n");
Dave Liu4be87b22009-03-14 12:48:30 +08001480 }
York Sunbad82092012-08-17 08:22:38 +00001481
Dave Liu4be87b22009-03-14 12:48:30 +08001482 bt = 0; /* Nibble sequential */
1483
1484 switch (popts->burst_length) {
1485 case DDR_BL8:
1486 bl = 0;
1487 break;
1488 case DDR_OTF:
1489 bl = 1;
1490 break;
1491 case DDR_BC4:
1492 bl = 2;
1493 break;
1494 default:
1495 printf("Error: invalid burst length of %u specified. "
1496 " Defaulting to on-the-fly BC4 or BL8 beats.\n",
1497 popts->burst_length);
1498 bl = 1;
1499 break;
1500 }
1501
1502 sdmode = (0
1503 | ((dll_on & 0x1) << 12)
1504 | ((wr & 0x7) << 9)
1505 | ((dll_rst & 0x1) << 8)
1506 | ((mode & 0x1) << 7)
1507 | (((caslat >> 1) & 0x7) << 4)
1508 | ((bt & 0x1) << 3)
York Sunbad82092012-08-17 08:22:38 +00001509 | ((caslat & 1) << 2)
Dave Liu4be87b22009-03-14 12:48:30 +08001510 | ((bl & 0x3) << 0)
1511 );
1512
1513 ddr->ddr_sdram_mode = (0
1514 | ((esdmode & 0xFFFF) << 16)
1515 | ((sdmode & 0xFFFF) << 0)
1516 );
1517
1518 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
York Sunba0c2eb2011-01-10 12:03:00 +00001519
1520 if (unq_mrs_en) { /* unique mode registers are supported */
Kumar Galad5bbe662011-11-09 10:05:10 -06001521 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Sunba0c2eb2011-01-10 12:03:00 +00001522 if (popts->rtt_override)
1523 rtt = popts->rtt_override_value;
1524 else
1525 rtt = popts->cs_local_opts[i].odt_rtt_norm;
1526
1527 esdmode &= 0xFDBB; /* clear bit 9,6,2 */
1528 esdmode |= (0
1529 | ((rtt & 0x4) << 7) /* rtt field is split */
1530 | ((rtt & 0x2) << 5) /* rtt field is split */
1531 | ((rtt & 0x1) << 2) /* rtt field is split */
1532 );
1533 switch (i) {
1534 case 1:
1535 ddr->ddr_sdram_mode_3 = (0
1536 | ((esdmode & 0xFFFF) << 16)
1537 | ((sdmode & 0xFFFF) << 0)
1538 );
1539 break;
1540 case 2:
1541 ddr->ddr_sdram_mode_5 = (0
1542 | ((esdmode & 0xFFFF) << 16)
1543 | ((sdmode & 0xFFFF) << 0)
1544 );
1545 break;
1546 case 3:
1547 ddr->ddr_sdram_mode_7 = (0
1548 | ((esdmode & 0xFFFF) << 16)
1549 | ((sdmode & 0xFFFF) << 0)
1550 );
1551 break;
1552 }
1553 }
1554 debug("FSLDDR: ddr_sdram_mode_3 = 0x%08x\n",
1555 ddr->ddr_sdram_mode_3);
1556 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1557 ddr->ddr_sdram_mode_5);
1558 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1559 ddr->ddr_sdram_mode_5);
1560 }
Dave Liu4be87b22009-03-14 12:48:30 +08001561}
1562
York Sunf0626592013-09-30 09:22:09 -07001563#else /* !CONFIG_SYS_FSL_DDR3 */
Dave Liu4be87b22009-03-14 12:48:30 +08001564
1565/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
1566static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
1567 const memctl_options_t *popts,
1568 const common_timing_params_t *common_dimm,
1569 unsigned int cas_latency,
York Sunba0c2eb2011-01-10 12:03:00 +00001570 unsigned int additive_latency,
1571 const unsigned int unq_mrs_en)
Dave Liu4be87b22009-03-14 12:48:30 +08001572{
1573 unsigned short esdmode; /* Extended SDRAM mode */
1574 unsigned short sdmode; /* SDRAM mode */
1575
Kumar Gala124b0822008-08-26 15:01:29 -05001576 /*
1577 * FIXME: This ought to be pre-calculated in a
1578 * technology-specific routine,
1579 * e.g. compute_DDR2_mode_register(), and then the
1580 * sdmode and esdmode passed in as part of common_dimm.
1581 */
1582
1583 /* Extended Mode Register */
1584 unsigned int mrs = 0; /* Mode Register Set */
1585 unsigned int outputs = 0; /* 0=Enabled, 1=Disabled */
1586 unsigned int rdqs_en = 0; /* RDQS Enable: 0=no, 1=yes */
1587 unsigned int dqs_en = 0; /* DQS# Enable: 0=enable, 1=disable */
1588 unsigned int ocd = 0; /* 0x0=OCD not supported,
1589 0x7=OCD default state */
1590 unsigned int rtt;
1591 unsigned int al; /* Posted CAS# additive latency (AL) */
1592 unsigned int ods = 0; /* Output Drive Strength:
1593 0 = Full strength (18ohm)
1594 1 = Reduced strength (4ohm) */
1595 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
1596 1=Disable (Test/Debug) */
1597
1598 /* Mode Register (MR) */
1599 unsigned int mr; /* Mode Register Definition */
1600 unsigned int pd; /* Power-Down Mode */
1601 unsigned int wr; /* Write Recovery */
1602 unsigned int dll_res; /* DLL Reset */
1603 unsigned int mode; /* Normal=0 or Test=1 */
Kumar Gala35ad58d2008-09-05 14:40:29 -05001604 unsigned int caslat = 0;/* CAS# latency */
Kumar Gala124b0822008-08-26 15:01:29 -05001605 /* BT: Burst Type (0=Sequential, 1=Interleaved) */
1606 unsigned int bt;
1607 unsigned int bl; /* BL: Burst Length */
1608
Priyanka Jain4a717412013-09-25 10:41:19 +05301609 dqs_en = !popts->dqs_config;
Kumar Gala124b0822008-08-26 15:01:29 -05001610 rtt = fsl_ddr_get_rtt();
1611
1612 al = additive_latency;
1613
1614 esdmode = (0
1615 | ((mrs & 0x3) << 14)
1616 | ((outputs & 0x1) << 12)
1617 | ((rdqs_en & 0x1) << 11)
1618 | ((dqs_en & 0x1) << 10)
1619 | ((ocd & 0x7) << 7)
1620 | ((rtt & 0x2) << 5) /* rtt field is split */
1621 | ((al & 0x7) << 3)
1622 | ((rtt & 0x1) << 2) /* rtt field is split */
1623 | ((ods & 0x1) << 1)
1624 | ((dll_en & 0x1) << 0)
1625 );
1626
1627 mr = 0; /* FIXME: CHECKME */
1628
1629 /*
1630 * 0 = Fast Exit (Normal)
1631 * 1 = Slow Exit (Low Power)
1632 */
1633 pd = 0;
1634
York Sunf0626592013-09-30 09:22:09 -07001635#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala124b0822008-08-26 15:01:29 -05001636 wr = 0; /* Historical */
York Sunf0626592013-09-30 09:22:09 -07001637#elif defined(CONFIG_SYS_FSL_DDR2)
York Sun2896cb72014-03-27 17:54:47 -07001638 wr = picos_to_mclk(common_dimm->twr_ps);
Kumar Gala124b0822008-08-26 15:01:29 -05001639#endif
1640 dll_res = 0;
1641 mode = 0;
1642
York Sunf0626592013-09-30 09:22:09 -07001643#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala124b0822008-08-26 15:01:29 -05001644 if (1 <= cas_latency && cas_latency <= 4) {
1645 unsigned char mode_caslat_table[4] = {
1646 0x5, /* 1.5 clocks */
1647 0x2, /* 2.0 clocks */
1648 0x6, /* 2.5 clocks */
1649 0x3 /* 3.0 clocks */
1650 };
Kumar Gala35ad58d2008-09-05 14:40:29 -05001651 caslat = mode_caslat_table[cas_latency - 1];
1652 } else {
1653 printf("Warning: unknown cas_latency %d\n", cas_latency);
Kumar Gala124b0822008-08-26 15:01:29 -05001654 }
York Sunf0626592013-09-30 09:22:09 -07001655#elif defined(CONFIG_SYS_FSL_DDR2)
Kumar Gala124b0822008-08-26 15:01:29 -05001656 caslat = cas_latency;
Kumar Gala124b0822008-08-26 15:01:29 -05001657#endif
1658 bt = 0;
1659
1660 switch (popts->burst_length) {
Dave Liu4be87b22009-03-14 12:48:30 +08001661 case DDR_BL4:
Kumar Gala124b0822008-08-26 15:01:29 -05001662 bl = 2;
1663 break;
Dave Liu4be87b22009-03-14 12:48:30 +08001664 case DDR_BL8:
Kumar Gala124b0822008-08-26 15:01:29 -05001665 bl = 3;
1666 break;
1667 default:
1668 printf("Error: invalid burst length of %u specified. "
1669 " Defaulting to 4 beats.\n",
1670 popts->burst_length);
1671 bl = 2;
1672 break;
1673 }
1674
1675 sdmode = (0
1676 | ((mr & 0x3) << 14)
1677 | ((pd & 0x1) << 12)
1678 | ((wr & 0x7) << 9)
1679 | ((dll_res & 0x1) << 8)
1680 | ((mode & 0x1) << 7)
1681 | ((caslat & 0x7) << 4)
1682 | ((bt & 0x1) << 3)
1683 | ((bl & 0x7) << 0)
1684 );
1685
1686 ddr->ddr_sdram_mode = (0
1687 | ((esdmode & 0xFFFF) << 16)
1688 | ((sdmode & 0xFFFF) << 0)
1689 );
Haiying Wangd90e0402008-10-03 12:37:26 -04001690 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
Kumar Gala124b0822008-08-26 15:01:29 -05001691}
Dave Liu4be87b22009-03-14 12:48:30 +08001692#endif
Kumar Gala124b0822008-08-26 15:01:29 -05001693
1694/* DDR SDRAM Data Initialization (DDR_DATA_INIT) */
1695static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr)
1696{
1697 unsigned int init_value; /* Initialization value */
1698
Anatolij Gustschin021b7ae2013-01-21 23:50:27 +00001699#ifdef CONFIG_MEM_INIT_VALUE
1700 init_value = CONFIG_MEM_INIT_VALUE;
1701#else
Kumar Gala124b0822008-08-26 15:01:29 -05001702 init_value = 0xDEADBEEF;
Anatolij Gustschin021b7ae2013-01-21 23:50:27 +00001703#endif
Kumar Gala124b0822008-08-26 15:01:29 -05001704 ddr->ddr_data_init = init_value;
1705}
1706
1707/*
1708 * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL)
1709 * The old controller on the 8540/60 doesn't have this register.
1710 * Hope it's OK to set it (to 0) anyway.
1711 */
1712static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
1713 const memctl_options_t *popts)
1714{
1715 unsigned int clk_adjust; /* Clock adjust */
1716
1717 clk_adjust = popts->clk_adjust;
1718 ddr->ddr_sdram_clk_cntl = (clk_adjust & 0xF) << 23;
yorkde879322010-07-02 22:25:55 +00001719 debug("FSLDDR: clk_cntl = 0x%08x\n", ddr->ddr_sdram_clk_cntl);
Kumar Gala124b0822008-08-26 15:01:29 -05001720}
1721
1722/* DDR Initialization Address (DDR_INIT_ADDR) */
1723static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
1724{
1725 unsigned int init_addr = 0; /* Initialization address */
1726
1727 ddr->ddr_init_addr = init_addr;
1728}
1729
1730/* DDR Initialization Address (DDR_INIT_EXT_ADDR) */
1731static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
1732{
1733 unsigned int uia = 0; /* Use initialization address */
1734 unsigned int init_ext_addr = 0; /* Initialization address */
1735
1736 ddr->ddr_init_ext_addr = (0
1737 | ((uia & 0x1) << 31)
1738 | (init_ext_addr & 0xF)
1739 );
1740}
1741
1742/* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */
Dave Liu3525e1a2010-03-05 12:22:00 +08001743static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr,
1744 const memctl_options_t *popts)
Kumar Gala124b0822008-08-26 15:01:29 -05001745{
1746 unsigned int rwt = 0; /* Read-to-write turnaround for same CS */
1747 unsigned int wrt = 0; /* Write-to-read turnaround for same CS */
1748 unsigned int rrt = 0; /* Read-to-read turnaround for same CS */
1749 unsigned int wwt = 0; /* Write-to-write turnaround for same CS */
1750 unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */
1751
York Sun2896cb72014-03-27 17:54:47 -07001752#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
Dave Liu3525e1a2010-03-05 12:22:00 +08001753 if (popts->burst_length == DDR_BL8) {
1754 /* We set BL/2 for fixed BL8 */
1755 rrt = 0; /* BL/2 clocks */
1756 wwt = 0; /* BL/2 clocks */
1757 } else {
1758 /* We need to set BL/2 + 2 to BC4 and OTF */
1759 rrt = 2; /* BL/2 + 2 clocks */
1760 wwt = 2; /* BL/2 + 2 clocks */
1761 }
York Sun2896cb72014-03-27 17:54:47 -07001762#endif
1763
1764#ifdef CONFIG_SYS_FSL_DDR4
1765 dll_lock = 2; /* tDLLK = 1024 clocks */
1766#elif defined(CONFIG_SYS_FSL_DDR3)
Dave Liu4be87b22009-03-14 12:48:30 +08001767 dll_lock = 1; /* tDLLK = 512 clocks from spec */
1768#endif
Kumar Gala124b0822008-08-26 15:01:29 -05001769 ddr->timing_cfg_4 = (0
1770 | ((rwt & 0xf) << 28)
1771 | ((wrt & 0xf) << 24)
1772 | ((rrt & 0xf) << 20)
1773 | ((wwt & 0xf) << 16)
1774 | (dll_lock & 0x3)
1775 );
Haiying Wangd90e0402008-10-03 12:37:26 -04001776 debug("FSLDDR: timing_cfg_4 = 0x%08x\n", ddr->timing_cfg_4);
Kumar Gala124b0822008-08-26 15:01:29 -05001777}
1778
1779/* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */
York Sunba0c2eb2011-01-10 12:03:00 +00001780static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr, unsigned int cas_latency)
Kumar Gala124b0822008-08-26 15:01:29 -05001781{
1782 unsigned int rodt_on = 0; /* Read to ODT on */
1783 unsigned int rodt_off = 0; /* Read to ODT off */
1784 unsigned int wodt_on = 0; /* Write to ODT on */
1785 unsigned int wodt_off = 0; /* Write to ODT off */
1786
York Sun2896cb72014-03-27 17:54:47 -07001787#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
1788 unsigned int wr_lat = ((ddr->timing_cfg_2 & 0x00780000) >> 19) +
1789 ((ddr->timing_cfg_2 & 0x00040000) >> 14);
York Sunba0c2eb2011-01-10 12:03:00 +00001790 /* rodt_on = timing_cfg_1[caslat] - timing_cfg_2[wrlat] + 1 */
York Sun2896cb72014-03-27 17:54:47 -07001791 if (cas_latency >= wr_lat)
1792 rodt_on = cas_latency - wr_lat + 1;
Dave Liu4be87b22009-03-14 12:48:30 +08001793 rodt_off = 4; /* 4 clocks */
york1714e492010-07-02 22:25:56 +00001794 wodt_on = 1; /* 1 clocks */
Dave Liu4be87b22009-03-14 12:48:30 +08001795 wodt_off = 4; /* 4 clocks */
1796#endif
1797
Kumar Gala124b0822008-08-26 15:01:29 -05001798 ddr->timing_cfg_5 = (0
Dave Liu4758d532008-11-21 16:31:29 +08001799 | ((rodt_on & 0x1f) << 24)
1800 | ((rodt_off & 0x7) << 20)
1801 | ((wodt_on & 0x1f) << 12)
1802 | ((wodt_off & 0x7) << 8)
Kumar Gala124b0822008-08-26 15:01:29 -05001803 );
Haiying Wangd90e0402008-10-03 12:37:26 -04001804 debug("FSLDDR: timing_cfg_5 = 0x%08x\n", ddr->timing_cfg_5);
Kumar Gala124b0822008-08-26 15:01:29 -05001805}
1806
York Sun2896cb72014-03-27 17:54:47 -07001807#ifdef CONFIG_SYS_FSL_DDR4
1808static void set_timing_cfg_6(fsl_ddr_cfg_regs_t *ddr)
1809{
1810 unsigned int hs_caslat = 0;
1811 unsigned int hs_wrlat = 0;
1812 unsigned int hs_wrrec = 0;
1813 unsigned int hs_clkadj = 0;
1814 unsigned int hs_wrlvl_start = 0;
1815
1816 ddr->timing_cfg_6 = (0
1817 | ((hs_caslat & 0x1f) << 24)
1818 | ((hs_wrlat & 0x1f) << 19)
1819 | ((hs_wrrec & 0x1f) << 12)
1820 | ((hs_clkadj & 0x1f) << 6)
1821 | ((hs_wrlvl_start & 0x1f) << 0)
1822 );
1823 debug("FSLDDR: timing_cfg_6 = 0x%08x\n", ddr->timing_cfg_6);
1824}
1825
1826static void set_timing_cfg_7(fsl_ddr_cfg_regs_t *ddr,
1827 const common_timing_params_t *common_dimm)
1828{
1829 unsigned int txpr, tcksre, tcksrx;
1830 unsigned int cke_rst, cksre, cksrx, par_lat, cs_to_cmd;
1831
Masahiro Yamadadb204642014-11-07 03:03:31 +09001832 txpr = max(5U, picos_to_mclk(common_dimm->trfc1_ps + 10000));
1833 tcksre = max(5U, picos_to_mclk(10000));
1834 tcksrx = max(5U, picos_to_mclk(10000));
York Sun2896cb72014-03-27 17:54:47 -07001835 par_lat = 0;
1836 cs_to_cmd = 0;
1837
1838 if (txpr <= 200)
1839 cke_rst = 0;
1840 else if (txpr <= 256)
1841 cke_rst = 1;
1842 else if (txpr <= 512)
1843 cke_rst = 2;
1844 else
1845 cke_rst = 3;
1846
1847 if (tcksre <= 19)
1848 cksre = tcksre - 5;
1849 else
1850 cksre = 15;
1851
1852 if (tcksrx <= 19)
1853 cksrx = tcksrx - 5;
1854 else
1855 cksrx = 15;
1856
1857 ddr->timing_cfg_7 = (0
1858 | ((cke_rst & 0x3) << 28)
1859 | ((cksre & 0xf) << 24)
1860 | ((cksrx & 0xf) << 20)
1861 | ((par_lat & 0xf) << 16)
1862 | ((cs_to_cmd & 0xf) << 4)
1863 );
1864 debug("FSLDDR: timing_cfg_7 = 0x%08x\n", ddr->timing_cfg_7);
1865}
1866
1867static void set_timing_cfg_8(fsl_ddr_cfg_regs_t *ddr,
1868 const memctl_options_t *popts,
1869 const common_timing_params_t *common_dimm,
1870 unsigned int cas_latency)
1871{
1872 unsigned int rwt_bg, wrt_bg, rrt_bg, wwt_bg;
1873 unsigned int acttoact_bg, wrtord_bg, pre_all_rec;
1874 unsigned int tccdl = picos_to_mclk(common_dimm->tccdl_ps);
1875 unsigned int wr_lat = ((ddr->timing_cfg_2 & 0x00780000) >> 19) +
1876 ((ddr->timing_cfg_2 & 0x00040000) >> 14);
1877
1878 rwt_bg = cas_latency + 2 + 4 - wr_lat;
1879 if (rwt_bg < tccdl)
1880 rwt_bg = tccdl - rwt_bg;
1881 else
1882 rwt_bg = 0;
1883
1884 wrt_bg = wr_lat + 4 + 1 - cas_latency;
1885 if (wrt_bg < tccdl)
1886 wrt_bg = tccdl - wrt_bg;
1887 else
1888 wrt_bg = 0;
1889
1890 if (popts->burst_length == DDR_BL8) {
1891 rrt_bg = tccdl - 4;
1892 wwt_bg = tccdl - 4;
1893 } else {
1894 rrt_bg = tccdl - 2;
1895 wwt_bg = tccdl - 4;
1896 }
1897
1898 acttoact_bg = picos_to_mclk(common_dimm->trrdl_ps);
Masahiro Yamadadb204642014-11-07 03:03:31 +09001899 wrtord_bg = max(4U, picos_to_mclk(7500));
York Sunf0e4f6d2014-06-26 11:14:44 -07001900 if (popts->otf_burst_chop_en)
1901 wrtord_bg += 2;
1902
York Sun2896cb72014-03-27 17:54:47 -07001903 pre_all_rec = 0;
1904
1905 ddr->timing_cfg_8 = (0
1906 | ((rwt_bg & 0xf) << 28)
1907 | ((wrt_bg & 0xf) << 24)
1908 | ((rrt_bg & 0xf) << 20)
1909 | ((wwt_bg & 0xf) << 16)
1910 | ((acttoact_bg & 0xf) << 12)
1911 | ((wrtord_bg & 0xf) << 8)
1912 | ((pre_all_rec & 0x1f) << 0)
1913 );
1914
1915 debug("FSLDDR: timing_cfg_8 = 0x%08x\n", ddr->timing_cfg_8);
1916}
1917
1918static void set_timing_cfg_9(fsl_ddr_cfg_regs_t *ddr)
1919{
1920 ddr->timing_cfg_9 = 0;
1921 debug("FSLDDR: timing_cfg_9 = 0x%08x\n", ddr->timing_cfg_9);
1922}
1923
York Suna8b3d522014-09-11 13:32:06 -07001924/* This function needs to be called after set_ddr_sdram_cfg() is called */
York Sun2896cb72014-03-27 17:54:47 -07001925static void set_ddr_dq_mapping(fsl_ddr_cfg_regs_t *ddr,
1926 const dimm_params_t *dimm_params)
1927{
York Suna8b3d522014-09-11 13:32:06 -07001928 unsigned int acc_ecc_en = (ddr->ddr_sdram_cfg >> 2) & 0x1;
1929
York Sun2896cb72014-03-27 17:54:47 -07001930 ddr->dq_map_0 = ((dimm_params->dq_mapping[0] & 0x3F) << 26) |
1931 ((dimm_params->dq_mapping[1] & 0x3F) << 20) |
1932 ((dimm_params->dq_mapping[2] & 0x3F) << 14) |
1933 ((dimm_params->dq_mapping[3] & 0x3F) << 8) |
1934 ((dimm_params->dq_mapping[4] & 0x3F) << 2);
1935
1936 ddr->dq_map_1 = ((dimm_params->dq_mapping[5] & 0x3F) << 26) |
1937 ((dimm_params->dq_mapping[6] & 0x3F) << 20) |
1938 ((dimm_params->dq_mapping[7] & 0x3F) << 14) |
1939 ((dimm_params->dq_mapping[10] & 0x3F) << 8) |
1940 ((dimm_params->dq_mapping[11] & 0x3F) << 2);
1941
1942 ddr->dq_map_2 = ((dimm_params->dq_mapping[12] & 0x3F) << 26) |
1943 ((dimm_params->dq_mapping[13] & 0x3F) << 20) |
1944 ((dimm_params->dq_mapping[14] & 0x3F) << 14) |
1945 ((dimm_params->dq_mapping[15] & 0x3F) << 8) |
1946 ((dimm_params->dq_mapping[16] & 0x3F) << 2);
1947
York Suna8b3d522014-09-11 13:32:06 -07001948 /* dq_map for ECC[4:7] is set to 0 if accumulated ECC is enabled */
York Sun2896cb72014-03-27 17:54:47 -07001949 ddr->dq_map_3 = ((dimm_params->dq_mapping[17] & 0x3F) << 26) |
1950 ((dimm_params->dq_mapping[8] & 0x3F) << 20) |
York Suna8b3d522014-09-11 13:32:06 -07001951 (acc_ecc_en ? 0 :
1952 (dimm_params->dq_mapping[9] & 0x3F) << 14) |
York Sun2896cb72014-03-27 17:54:47 -07001953 dimm_params->dq_mapping_ors;
1954
1955 debug("FSLDDR: dq_map_0 = 0x%08x\n", ddr->dq_map_0);
1956 debug("FSLDDR: dq_map_1 = 0x%08x\n", ddr->dq_map_1);
1957 debug("FSLDDR: dq_map_2 = 0x%08x\n", ddr->dq_map_2);
1958 debug("FSLDDR: dq_map_3 = 0x%08x\n", ddr->dq_map_3);
1959}
1960static void set_ddr_sdram_cfg_3(fsl_ddr_cfg_regs_t *ddr,
1961 const memctl_options_t *popts)
1962{
1963 int rd_pre;
1964
1965 rd_pre = popts->quad_rank_present ? 1 : 0;
1966
1967 ddr->ddr_sdram_cfg_3 = (rd_pre & 0x1) << 16;
1968
1969 debug("FSLDDR: ddr_sdram_cfg_3 = 0x%08x\n", ddr->ddr_sdram_cfg_3);
1970}
1971#endif /* CONFIG_SYS_FSL_DDR4 */
1972
Kumar Gala124b0822008-08-26 15:01:29 -05001973/* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */
Dave Liu4be87b22009-03-14 12:48:30 +08001974static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int zq_en)
Kumar Gala124b0822008-08-26 15:01:29 -05001975{
Kumar Gala124b0822008-08-26 15:01:29 -05001976 unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */
1977 /* Normal Operation Full Calibration Time (tZQoper) */
1978 unsigned int zqoper = 0;
1979 /* Normal Operation Short Calibration Time (tZQCS) */
1980 unsigned int zqcs = 0;
York Sun2896cb72014-03-27 17:54:47 -07001981#ifdef CONFIG_SYS_FSL_DDR4
1982 unsigned int zqcs_init;
1983#endif
Kumar Gala124b0822008-08-26 15:01:29 -05001984
Dave Liu4be87b22009-03-14 12:48:30 +08001985 if (zq_en) {
York Sun2896cb72014-03-27 17:54:47 -07001986#ifdef CONFIG_SYS_FSL_DDR4
1987 zqinit = 10; /* 1024 clocks */
1988 zqoper = 9; /* 512 clocks */
1989 zqcs = 7; /* 128 clocks */
1990 zqcs_init = 5; /* 1024 refresh sequences */
1991#else
Dave Liu4be87b22009-03-14 12:48:30 +08001992 zqinit = 9; /* 512 clocks */
1993 zqoper = 8; /* 256 clocks */
1994 zqcs = 6; /* 64 clocks */
York Sun2896cb72014-03-27 17:54:47 -07001995#endif
Dave Liu4be87b22009-03-14 12:48:30 +08001996 }
1997
Kumar Gala124b0822008-08-26 15:01:29 -05001998 ddr->ddr_zq_cntl = (0
1999 | ((zq_en & 0x1) << 31)
2000 | ((zqinit & 0xF) << 24)
2001 | ((zqoper & 0xF) << 16)
2002 | ((zqcs & 0xF) << 8)
York Sun2896cb72014-03-27 17:54:47 -07002003#ifdef CONFIG_SYS_FSL_DDR4
2004 | ((zqcs_init & 0xF) << 0)
2005#endif
Kumar Gala124b0822008-08-26 15:01:29 -05002006 );
York Sunba0c2eb2011-01-10 12:03:00 +00002007 debug("FSLDDR: zq_cntl = 0x%08x\n", ddr->ddr_zq_cntl);
Kumar Gala124b0822008-08-26 15:01:29 -05002008}
2009
2010/* DDR Write Leveling Control (DDR_WRLVL_CNTL) */
Dave Liu64ee7df2009-12-16 10:24:37 -06002011static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int wrlvl_en,
2012 const memctl_options_t *popts)
Kumar Gala124b0822008-08-26 15:01:29 -05002013{
Kumar Gala124b0822008-08-26 15:01:29 -05002014 /*
2015 * First DQS pulse rising edge after margining mode
2016 * is programmed (tWL_MRD)
2017 */
2018 unsigned int wrlvl_mrd = 0;
2019 /* ODT delay after margining mode is programmed (tWL_ODTEN) */
2020 unsigned int wrlvl_odten = 0;
2021 /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */
2022 unsigned int wrlvl_dqsen = 0;
2023 /* WRLVL_SMPL: Write leveling sample time */
2024 unsigned int wrlvl_smpl = 0;
2025 /* WRLVL_WLR: Write leveling repeition time */
2026 unsigned int wrlvl_wlr = 0;
2027 /* WRLVL_START: Write leveling start time */
2028 unsigned int wrlvl_start = 0;
2029
Dave Liu4be87b22009-03-14 12:48:30 +08002030 /* suggest enable write leveling for DDR3 due to fly-by topology */
2031 if (wrlvl_en) {
2032 /* tWL_MRD min = 40 nCK, we set it 64 */
2033 wrlvl_mrd = 0x6;
2034 /* tWL_ODTEN 128 */
2035 wrlvl_odten = 0x7;
2036 /* tWL_DQSEN min = 25 nCK, we set it 32 */
2037 wrlvl_dqsen = 0x5;
2038 /*
Dave Liu64ee7df2009-12-16 10:24:37 -06002039 * Write leveling sample time at least need 6 clocks
2040 * higher than tWLO to allow enough time for progagation
2041 * delay and sampling the prime data bits.
Dave Liu4be87b22009-03-14 12:48:30 +08002042 */
2043 wrlvl_smpl = 0xf;
2044 /*
2045 * Write leveling repetition time
2046 * at least tWLO + 6 clocks clocks
york1714e492010-07-02 22:25:56 +00002047 * we set it 64
Dave Liu4be87b22009-03-14 12:48:30 +08002048 */
york1714e492010-07-02 22:25:56 +00002049 wrlvl_wlr = 0x6;
Dave Liu4be87b22009-03-14 12:48:30 +08002050 /*
2051 * Write leveling start time
2052 * The value use for the DQS_ADJUST for the first sample
York Sunba0c2eb2011-01-10 12:03:00 +00002053 * when write leveling is enabled. It probably needs to be
2054 * overriden per platform.
Dave Liu4be87b22009-03-14 12:48:30 +08002055 */
2056 wrlvl_start = 0x8;
Dave Liu64ee7df2009-12-16 10:24:37 -06002057 /*
2058 * Override the write leveling sample and start time
2059 * according to specific board
2060 */
2061 if (popts->wrlvl_override) {
2062 wrlvl_smpl = popts->wrlvl_sample;
2063 wrlvl_start = popts->wrlvl_start;
2064 }
Dave Liu4be87b22009-03-14 12:48:30 +08002065 }
2066
Kumar Gala124b0822008-08-26 15:01:29 -05002067 ddr->ddr_wrlvl_cntl = (0
2068 | ((wrlvl_en & 0x1) << 31)
2069 | ((wrlvl_mrd & 0x7) << 24)
2070 | ((wrlvl_odten & 0x7) << 20)
2071 | ((wrlvl_dqsen & 0x7) << 16)
2072 | ((wrlvl_smpl & 0xf) << 12)
2073 | ((wrlvl_wlr & 0x7) << 8)
Dave Liu4758d532008-11-21 16:31:29 +08002074 | ((wrlvl_start & 0x1F) << 0)
Kumar Gala124b0822008-08-26 15:01:29 -05002075 );
York Sunba0c2eb2011-01-10 12:03:00 +00002076 debug("FSLDDR: wrlvl_cntl = 0x%08x\n", ddr->ddr_wrlvl_cntl);
York Sun7d69ea32012-10-08 07:44:22 +00002077 ddr->ddr_wrlvl_cntl_2 = popts->wrlvl_ctl_2;
2078 debug("FSLDDR: wrlvl_cntl_2 = 0x%08x\n", ddr->ddr_wrlvl_cntl_2);
2079 ddr->ddr_wrlvl_cntl_3 = popts->wrlvl_ctl_3;
2080 debug("FSLDDR: wrlvl_cntl_3 = 0x%08x\n", ddr->ddr_wrlvl_cntl_3);
2081
Kumar Gala124b0822008-08-26 15:01:29 -05002082}
2083
2084/* DDR Self Refresh Counter (DDR_SR_CNTR) */
Dave Liu2aad0ae2008-11-21 16:31:35 +08002085static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr, unsigned int sr_it)
Kumar Gala124b0822008-08-26 15:01:29 -05002086{
Dave Liu2aad0ae2008-11-21 16:31:35 +08002087 /* Self Refresh Idle Threshold */
Kumar Gala124b0822008-08-26 15:01:29 -05002088 ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
2089}
2090
york42603722010-07-02 22:25:54 +00002091static void set_ddr_eor(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
2092{
2093 if (popts->addr_hash) {
2094 ddr->ddr_eor = 0x40000000; /* address hash enable */
Kumar Gala4513d762011-03-18 11:53:06 -05002095 puts("Address hashing enabled.\n");
york42603722010-07-02 22:25:54 +00002096 }
2097}
2098
York Sunba0c2eb2011-01-10 12:03:00 +00002099static void set_ddr_cdr1(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
2100{
2101 ddr->ddr_cdr1 = popts->ddr_cdr1;
2102 debug("FSLDDR: ddr_cdr1 = 0x%08x\n", ddr->ddr_cdr1);
2103}
2104
York Sun7d69ea32012-10-08 07:44:22 +00002105static void set_ddr_cdr2(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
2106{
2107 ddr->ddr_cdr2 = popts->ddr_cdr2;
2108 debug("FSLDDR: ddr_cdr2 = 0x%08x\n", ddr->ddr_cdr2);
2109}
2110
Kumar Gala124b0822008-08-26 15:01:29 -05002111unsigned int
2112check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
2113{
2114 unsigned int res = 0;
2115
2116 /*
2117 * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are
2118 * not set at the same time.
2119 */
2120 if (ddr->ddr_sdram_cfg & 0x10000000
2121 && ddr->ddr_sdram_cfg & 0x00008000) {
2122 printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
2123 " should not be set at the same time.\n");
2124 res++;
2125 }
2126
2127 return res;
2128}
2129
2130unsigned int
2131compute_fsl_memctl_config_regs(const memctl_options_t *popts,
2132 fsl_ddr_cfg_regs_t *ddr,
2133 const common_timing_params_t *common_dimm,
2134 const dimm_params_t *dimm_params,
Haiying Wang80ad4012010-12-01 10:35:31 -05002135 unsigned int dbw_cap_adj,
2136 unsigned int size_only)
Kumar Gala124b0822008-08-26 15:01:29 -05002137{
2138 unsigned int i;
2139 unsigned int cas_latency;
2140 unsigned int additive_latency;
Dave Liu2aad0ae2008-11-21 16:31:35 +08002141 unsigned int sr_it;
Dave Liu4be87b22009-03-14 12:48:30 +08002142 unsigned int zq_en;
2143 unsigned int wrlvl_en;
York Sunba0c2eb2011-01-10 12:03:00 +00002144 unsigned int ip_rev = 0;
2145 unsigned int unq_mrs_en = 0;
York Sun2927c5e2010-10-18 13:46:50 -07002146 int cs_en = 1;
Kumar Gala124b0822008-08-26 15:01:29 -05002147
2148 memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
2149
2150 if (common_dimm == NULL) {
2151 printf("Error: subset DIMM params struct null pointer\n");
2152 return 1;
2153 }
2154
2155 /*
2156 * Process overrides first.
2157 *
2158 * FIXME: somehow add dereated caslat to this
2159 */
2160 cas_latency = (popts->cas_latency_override)
2161 ? popts->cas_latency_override_value
York Sun2896cb72014-03-27 17:54:47 -07002162 : common_dimm->lowest_common_spd_caslat;
Kumar Gala124b0822008-08-26 15:01:29 -05002163
2164 additive_latency = (popts->additive_latency_override)
2165 ? popts->additive_latency_override_value
2166 : common_dimm->additive_latency;
2167
Dave Liu2aad0ae2008-11-21 16:31:35 +08002168 sr_it = (popts->auto_self_refresh_en)
2169 ? popts->sr_it
2170 : 0;
Dave Liu4be87b22009-03-14 12:48:30 +08002171 /* ZQ calibration */
2172 zq_en = (popts->zq_en) ? 1 : 0;
2173 /* write leveling */
2174 wrlvl_en = (popts->wrlvl_en) ? 1 : 0;
Dave Liu2aad0ae2008-11-21 16:31:35 +08002175
Kumar Gala124b0822008-08-26 15:01:29 -05002176 /* Chip Select Memory Bounds (CSn_BNDS) */
2177 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Sune8dc17b2012-08-17 08:22:39 +00002178 unsigned long long ea, sa;
york93799ca2010-07-02 22:25:52 +00002179 unsigned int cs_per_dimm
2180 = CONFIG_CHIP_SELECTS_PER_CTRL / CONFIG_DIMM_SLOTS_PER_CTLR;
2181 unsigned int dimm_number
2182 = i / cs_per_dimm;
2183 unsigned long long rank_density
York Sune8dc17b2012-08-17 08:22:39 +00002184 = dimm_params[dimm_number].rank_density >> dbw_cap_adj;
Haiying Wang272b5962008-10-03 12:36:39 -04002185
york93799ca2010-07-02 22:25:52 +00002186 if (dimm_params[dimm_number].n_ranks == 0) {
Kumar Gala124b0822008-08-26 15:01:29 -05002187 debug("Skipping setup of CS%u "
yorkf4f93c62010-07-02 22:25:53 +00002188 "because n_ranks on DIMM %u is 0\n", i, dimm_number);
Kumar Gala124b0822008-08-26 15:01:29 -05002189 continue;
2190 }
York Sune8dc17b2012-08-17 08:22:39 +00002191 if (popts->memctl_interleaving) {
york93799ca2010-07-02 22:25:52 +00002192 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
York Sune8dc17b2012-08-17 08:22:39 +00002193 case FSL_DDR_CS0_CS1_CS2_CS3:
2194 break;
york93799ca2010-07-02 22:25:52 +00002195 case FSL_DDR_CS0_CS1:
2196 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
York Sun2927c5e2010-10-18 13:46:50 -07002197 if (i > 1)
2198 cs_en = 0;
york93799ca2010-07-02 22:25:52 +00002199 break;
2200 case FSL_DDR_CS2_CS3:
York Sune8dc17b2012-08-17 08:22:39 +00002201 default:
York Sun2927c5e2010-10-18 13:46:50 -07002202 if (i > 0)
2203 cs_en = 0;
york93799ca2010-07-02 22:25:52 +00002204 break;
york93799ca2010-07-02 22:25:52 +00002205 }
York Sune8dc17b2012-08-17 08:22:39 +00002206 sa = common_dimm->base_address;
York Sun98df4d12012-10-08 07:44:23 +00002207 ea = sa + common_dimm->total_mem - 1;
York Sune8dc17b2012-08-17 08:22:39 +00002208 } else if (!popts->memctl_interleaving) {
Kumar Gala124b0822008-08-26 15:01:29 -05002209 /*
2210 * If memory interleaving between controllers is NOT
2211 * enabled, the starting address for each memory
2212 * controller is distinct. However, because rank
2213 * interleaving is enabled, the starting and ending
2214 * addresses of the total memory on that memory
2215 * controller needs to be programmed into its
2216 * respective CS0_BNDS.
2217 */
Haiying Wang272b5962008-10-03 12:36:39 -04002218 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
2219 case FSL_DDR_CS0_CS1_CS2_CS3:
Haiying Wang272b5962008-10-03 12:36:39 -04002220 sa = common_dimm->base_address;
York Sun98df4d12012-10-08 07:44:23 +00002221 ea = sa + common_dimm->total_mem - 1;
Haiying Wang272b5962008-10-03 12:36:39 -04002222 break;
2223 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
York Sune8dc17b2012-08-17 08:22:39 +00002224 if ((i >= 2) && (dimm_number == 0)) {
york93799ca2010-07-02 22:25:52 +00002225 sa = dimm_params[dimm_number].base_address +
York Sune8dc17b2012-08-17 08:22:39 +00002226 2 * rank_density;
2227 ea = sa + 2 * rank_density - 1;
york93799ca2010-07-02 22:25:52 +00002228 } else {
2229 sa = dimm_params[dimm_number].base_address;
York Sune8dc17b2012-08-17 08:22:39 +00002230 ea = sa + 2 * rank_density - 1;
Haiying Wang272b5962008-10-03 12:36:39 -04002231 }
2232 break;
2233 case FSL_DDR_CS0_CS1:
york93799ca2010-07-02 22:25:52 +00002234 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
2235 sa = dimm_params[dimm_number].base_address;
York Sune8dc17b2012-08-17 08:22:39 +00002236 ea = sa + rank_density - 1;
2237 if (i != 1)
2238 sa += (i % cs_per_dimm) * rank_density;
2239 ea += (i % cs_per_dimm) * rank_density;
york93799ca2010-07-02 22:25:52 +00002240 } else {
2241 sa = 0;
2242 ea = 0;
2243 }
2244 if (i == 0)
York Sune8dc17b2012-08-17 08:22:39 +00002245 ea += rank_density;
Haiying Wang272b5962008-10-03 12:36:39 -04002246 break;
2247 case FSL_DDR_CS2_CS3:
york93799ca2010-07-02 22:25:52 +00002248 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
2249 sa = dimm_params[dimm_number].base_address;
York Sune8dc17b2012-08-17 08:22:39 +00002250 ea = sa + rank_density - 1;
2251 if (i != 3)
2252 sa += (i % cs_per_dimm) * rank_density;
2253 ea += (i % cs_per_dimm) * rank_density;
york93799ca2010-07-02 22:25:52 +00002254 } else {
2255 sa = 0;
2256 ea = 0;
Haiying Wang272b5962008-10-03 12:36:39 -04002257 }
york93799ca2010-07-02 22:25:52 +00002258 if (i == 2)
2259 ea += (rank_density >> dbw_cap_adj);
Haiying Wang272b5962008-10-03 12:36:39 -04002260 break;
2261 default: /* No bank(chip-select) interleaving */
York Sune8dc17b2012-08-17 08:22:39 +00002262 sa = dimm_params[dimm_number].base_address;
2263 ea = sa + rank_density - 1;
2264 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
2265 sa += (i % cs_per_dimm) * rank_density;
2266 ea += (i % cs_per_dimm) * rank_density;
2267 } else {
2268 sa = 0;
2269 ea = 0;
2270 }
Haiying Wang272b5962008-10-03 12:36:39 -04002271 break;
2272 }
Kumar Gala124b0822008-08-26 15:01:29 -05002273 }
Kumar Gala124b0822008-08-26 15:01:29 -05002274
2275 sa >>= 24;
2276 ea >>= 24;
2277
York Sun98df4d12012-10-08 07:44:23 +00002278 if (cs_en) {
2279 ddr->cs[i].bnds = (0
York Sun63c91cd2013-06-03 12:39:06 -07002280 | ((sa & 0xffff) << 16) /* starting address */
2281 | ((ea & 0xffff) << 0) /* ending address */
York Sun98df4d12012-10-08 07:44:23 +00002282 );
2283 } else {
York Sunc21a7392013-06-25 11:37:45 -07002284 /* setting bnds to 0xffffffff for inactive CS */
2285 ddr->cs[i].bnds = 0xffffffff;
York Sun98df4d12012-10-08 07:44:23 +00002286 }
Kumar Gala124b0822008-08-26 15:01:29 -05002287
Haiying Wangd90e0402008-10-03 12:37:26 -04002288 debug("FSLDDR: cs[%d]_bnds = 0x%08x\n", i, ddr->cs[i].bnds);
York Sun98df4d12012-10-08 07:44:23 +00002289 set_csn_config(dimm_number, i, ddr, popts, dimm_params);
2290 set_csn_config_2(i, ddr);
Kumar Gala124b0822008-08-26 15:01:29 -05002291 }
2292
Haiying Wang80ad4012010-12-01 10:35:31 -05002293 /*
2294 * In the case we only need to compute the ddr sdram size, we only need
2295 * to set csn registers, so return from here.
2296 */
2297 if (size_only)
2298 return 0;
2299
york42603722010-07-02 22:25:54 +00002300 set_ddr_eor(ddr, popts);
2301
York Sunf0626592013-09-30 09:22:09 -07002302#if !defined(CONFIG_SYS_FSL_DDR1)
York Sun98df4d12012-10-08 07:44:23 +00002303 set_timing_cfg_0(ddr, popts, dimm_params);
Kumar Gala124b0822008-08-26 15:01:29 -05002304#endif
2305
York Sun63c91cd2013-06-03 12:39:06 -07002306 set_timing_cfg_3(ddr, popts, common_dimm, cas_latency,
2307 additive_latency);
Dave Liu4be87b22009-03-14 12:48:30 +08002308 set_timing_cfg_1(ddr, popts, common_dimm, cas_latency);
Kumar Gala124b0822008-08-26 15:01:29 -05002309 set_timing_cfg_2(ddr, popts, common_dimm,
2310 cas_latency, additive_latency);
2311
York Sunba0c2eb2011-01-10 12:03:00 +00002312 set_ddr_cdr1(ddr, popts);
York Sun7d69ea32012-10-08 07:44:22 +00002313 set_ddr_cdr2(ddr, popts);
Kumar Gala124b0822008-08-26 15:01:29 -05002314 set_ddr_sdram_cfg(ddr, popts, common_dimm);
York Sunba0c2eb2011-01-10 12:03:00 +00002315 ip_rev = fsl_ddr_get_version();
2316 if (ip_rev > 0x40400)
2317 unq_mrs_en = 1;
Kumar Gala124b0822008-08-26 15:01:29 -05002318
York Suna8b3d522014-09-11 13:32:06 -07002319 if ((ip_rev > 0x40700) && (popts->cswl_override != 0))
York Sune0f60462014-09-05 13:52:43 +08002320 ddr->debug[18] = popts->cswl_override;
2321
York Sunba0c2eb2011-01-10 12:03:00 +00002322 set_ddr_sdram_cfg_2(ddr, popts, unq_mrs_en);
Kumar Gala124b0822008-08-26 15:01:29 -05002323 set_ddr_sdram_mode(ddr, popts, common_dimm,
York Sunba0c2eb2011-01-10 12:03:00 +00002324 cas_latency, additive_latency, unq_mrs_en);
Valentin Longchamp0b810932013-10-18 11:47:20 +02002325 set_ddr_sdram_mode_2(ddr, popts, common_dimm, unq_mrs_en);
York Sun2896cb72014-03-27 17:54:47 -07002326#ifdef CONFIG_SYS_FSL_DDR4
2327 set_ddr_sdram_mode_9(ddr, popts, common_dimm, unq_mrs_en);
2328 set_ddr_sdram_mode_10(ddr, popts, common_dimm, unq_mrs_en);
2329#endif
Kumar Gala124b0822008-08-26 15:01:29 -05002330 set_ddr_sdram_interval(ddr, popts, common_dimm);
2331 set_ddr_data_init(ddr);
2332 set_ddr_sdram_clk_cntl(ddr, popts);
2333 set_ddr_init_addr(ddr);
2334 set_ddr_init_ext_addr(ddr);
Dave Liu3525e1a2010-03-05 12:22:00 +08002335 set_timing_cfg_4(ddr, popts);
York Sunba0c2eb2011-01-10 12:03:00 +00002336 set_timing_cfg_5(ddr, cas_latency);
York Sun2896cb72014-03-27 17:54:47 -07002337#ifdef CONFIG_SYS_FSL_DDR4
2338 set_ddr_sdram_cfg_3(ddr, popts);
2339 set_timing_cfg_6(ddr);
2340 set_timing_cfg_7(ddr, common_dimm);
2341 set_timing_cfg_8(ddr, popts, common_dimm, cas_latency);
2342 set_timing_cfg_9(ddr);
2343 set_ddr_dq_mapping(ddr, dimm_params);
2344#endif
Kumar Gala124b0822008-08-26 15:01:29 -05002345
Dave Liu4be87b22009-03-14 12:48:30 +08002346 set_ddr_zq_cntl(ddr, zq_en);
Dave Liu64ee7df2009-12-16 10:24:37 -06002347 set_ddr_wrlvl_cntl(ddr, wrlvl_en, popts);
Kumar Gala124b0822008-08-26 15:01:29 -05002348
Dave Liu2aad0ae2008-11-21 16:31:35 +08002349 set_ddr_sr_cntr(ddr, sr_it);
Kumar Gala124b0822008-08-26 15:01:29 -05002350
York Sunba0c2eb2011-01-10 12:03:00 +00002351 set_ddr_sdram_rcw(ddr, popts, common_dimm);
Kumar Gala124b0822008-08-26 15:01:29 -05002352
York Sun972cc402013-06-25 11:37:41 -07002353#ifdef CONFIG_SYS_FSL_DDR_EMU
2354 /* disble DDR training for emulator */
2355 ddr->debug[2] = 0x00000400;
2356 ddr->debug[4] = 0xff800000;
2357#endif
York Sun99825792014-05-23 13:15:00 -07002358#ifdef CONFIG_SYS_FSL_ERRATUM_A004508
2359 if ((ip_rev >= 0x40000) && (ip_rev < 0x40400))
2360 ddr->debug[2] |= 0x00000200; /* set bit 22 */
2361#endif
2362
Kumar Gala124b0822008-08-26 15:01:29 -05002363 return check_fsl_memctl_config_regs(ddr);
2364}