blob: 03d7ff17dd196563b987f398e939d0d58fd6530d [file] [log] [blame]
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001/*
York Sun34e026f2014-03-27 17:54:47 -07002 * Copyright 2008-2014 Freescale Semiconductor, Inc.
Kumar Gala58e5e9a2008-08-26 15:01:29 -05003 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Kumar Gala58e5e9a2008-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 Sun5614e712013-09-30 09:22:09 -070014#include <fsl_ddr_sdram.h>
Kumar Gala58e5e9a2008-08-26 15:01:29 -050015
York Sun5614e712013-09-30 09:22:09 -070016#include <fsl_ddr.h>
York Sun9a17eb52013-11-18 10:29:32 -080017#include <fsl_immap.h>
York Sun5614e712013-09-30 09:22:09 -070018#include <asm/io.h>
Kumar Gala58e5e9a2008-08-26 15:01:29 -050019
York Sune1fd16b2011-01-10 12:03:00 +000020unsigned int picos_to_mclk(unsigned int picos);
21
Kumar Gala58e5e9a2008-08-26 15:01:29 -050022/*
23 * Determine Rtt value.
24 *
25 * This should likely be either board or controller specific.
26 *
Dave Liuc360cea2009-03-14 12:48:30 +080027 * Rtt(nominal) - DDR2:
Kumar Gala58e5e9a2008-08-26 15:01:29 -050028 * 0 = Rtt disabled
29 * 1 = 75 ohm
30 * 2 = 150 ohm
31 * 3 = 50 ohm
Dave Liuc360cea2009-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 Gala58e5e9a2008-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 Sun5614e712013-09-30 09:22:09 -070059#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala58e5e9a2008-08-26 15:01:29 -050060 rtt = 0;
York Sun5614e712013-09-30 09:22:09 -070061#elif defined(CONFIG_SYS_FSL_DDR2)
Kumar Gala58e5e9a2008-08-26 15:01:29 -050062 rtt = 3;
63#else
Dave Liuc360cea2009-03-14 12:48:30 +080064 rtt = 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -050065#endif
66
67 return rtt;
68}
69
York Sun34e026f2014-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 Liuc360cea2009-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 Sun2bba85f2011-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 Liuc360cea2009-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 Sun2bba85f2011-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 Liuc360cea2009-03-14 12:48:30 +0800139 return cwl;
140}
York Sun34e026f2014-03-27 17:54:47 -0700141#endif
Dave Liuc360cea2009-03-14 12:48:30 +0800142
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500143/* Chip Select Configuration (CSn_CONFIG) */
york5800e7a2010-07-02 22:25:53 +0000144static void set_csn_config(int dimm_number, int i, fsl_ddr_cfg_regs_t *ddr,
Kumar Gala58e5e9a2008-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 */
york5800e7a2010-07-02 22:25:53 +0000157 int go_config = 0;
York Sun34e026f2014-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 Gala58e5e9a2008-08-26 15:01:29 -0500163
164 /* Compute CS_CONFIG only for existing ranks of each DIMM. */
york5800e7a2010-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 Gala58e5e9a2008-08-26 15:01:29 -0500169 /* These fields only available in CS0_CONFIG */
York Suna4c66502012-08-17 08:22:39 +0000170 if (!popts->memctl_interleaving)
171 break;
172 switch (popts->memctl_interleaving_mode) {
York Sun6b1e1252014-02-10 13:59:44 -0800173 case FSL_DDR_256B_INTERLEAVING:
York Suna4c66502012-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 Gala58e5e9a2008-08-26 15:01:29 -0500184 }
york5800e7a2010-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 Suncae7c1b2011-08-26 11:32:40 -0700193 (dimm_number >= 1 && dimm_params[dimm_number].n_ranks > 0))
york5800e7a2010-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) {
york5800e7a2010-07-02 22:25:53 +0000206 cs_n_en = 1;
Kumar Gala58e5e9a2008-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 Sun34e026f2014-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 Gala58e5e9a2008-08-26 15:01:29 -0500214 n_banks_per_sdram_device
york5800e7a2010-07-02 22:25:53 +0000215 = dimm_params[dimm_number].n_banks_per_sdram_device;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500216 ba_bits_cs_n = __ilog2(n_banks_per_sdram_device) - 2;
York Sun34e026f2014-03-27 17:54:47 -0700217#endif
york5800e7a2010-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 Gala58e5e9a2008-08-26 15:01:29 -0500220 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500221 ddr->cs[i].config = (0
222 | ((cs_n_en & 0x1) << 31)
223 | ((intlv_en & 0x3) << 29)
Haiying Wangdbbbb3a2008-10-03 12:36:39 -0400224 | ((intlv_ctl & 0xf) << 24)
Kumar Gala58e5e9a2008-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 Sun34e026f2014-03-27 17:54:47 -0700235#ifdef CONFIG_SYS_FSL_DDR4
236 | ((bg_bits_cs_n & 0x3) << 4)
237#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500238 | ((col_bits_cs_n & 0x7) << 0)
239 );
Haiying Wang1f293b42008-10-03 12:37:26 -0400240 debug("FSLDDR: cs[%d]_config = 0x%08x\n", i,ddr->cs[i].config);
Kumar Gala58e5e9a2008-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 Wang1f293b42008-10-03 12:37:26 -0400250 debug("FSLDDR: cs[%d]_config_2 = 0x%08x\n", i, ddr->cs[i].config_2);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500251}
252
253/* -3E = 667 CL5, -25 = CL6 800, -25E = CL5 800 */
254
York Sun5614e712013-09-30 09:22:09 -0700255#if !defined(CONFIG_SYS_FSL_DDR1)
York Sun84baed22014-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 Sun123922b2012-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 Sun84baed22014-11-07 12:14:36 -0800264 return 2;
York Sun123922b2012-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 Sun84baed22014-11-07 12:14:36 -0800270 return 2;
York Sun123922b2012-10-08 07:44:23 +0000271
272#ifdef CONFIG_FSL_DDR_FIRST_SLOT_QUAD_CAPABLE
273 if (dimm_params[0].n_ranks == 4)
York Sun84baed22014-11-07 12:14:36 -0800274 return 2;
York Sun123922b2012-10-08 07:44:23 +0000275#endif
York Sun84baed22014-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 Sun123922b2012-10-08 07:44:23 +0000280#endif
281 return 0;
282}
283
Kumar Gala58e5e9a2008-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 Sune1fd16b2011-01-10 12:03:00 +0000290static void set_timing_cfg_0(fsl_ddr_cfg_regs_t *ddr,
York Sun123922b2012-10-08 07:44:23 +0000291 const memctl_options_t *popts,
292 const dimm_params_t *dimm_params)
Kumar Gala58e5e9a2008-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;
york5fb8a8a2010-07-02 22:25:56 +0000304 /* ODT powerdown exit timing (tAXPD). */
York Sun34e026f2014-03-27 17:54:47 -0700305 unsigned char taxpd_mclk = 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500306 /* Mode register set cycle time (tMRD). */
307 unsigned char tmrd_mclk;
York Sunbb578322014-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 Gala58e5e9a2008-08-26 15:01:29 -0500311
York Sun34e026f2014-03-27 17:54:47 -0700312#ifdef CONFIG_SYS_FSL_DDR4
313 /* tXP=max(4nCK, 6ns) */
Masahiro Yamadab4141192014-11-07 03:03:31 +0900314 int txp = max((int)mclk_ps * 4, 6000); /* unit=ps */
York Sun34e026f2014-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 Yamadab4141192014-11-07 03:03:31 +0900323 tmrd_mclk = max(24U, picos_to_mclk(15000));
York Sun34e026f2014-03-27 17:54:47 -0700324#elif defined(CONFIG_SYS_FSL_DDR3)
York Sunbb578322014-08-21 16:13:22 -0700325 unsigned int data_rate = get_ddr_freq(0);
326 int txp;
York Sun938bbb62014-12-02 11:18:09 -0800327 unsigned int ip_rev;
York Sun84baed22014-11-07 12:14:36 -0800328 int odt_overlap;
Dave Liuc360cea2009-03-14 12:48:30 +0800329 /*
330 * (tXARD and tXARDS). Empirical?
331 * The DDR3 spec has not tXARD,
332 * we use the tXP instead of it.
York Sunbb578322014-08-21 16:13:22 -0700333 * tXP=max(3nCK, 7.5ns) for DDR3-800, 1066
334 * max(3nCK, 6ns) for DDR3-1333, 1600, 1866, 2133
Dave Liuc360cea2009-03-14 12:48:30 +0800335 * spec has not the tAXPD, we use
york5fb8a8a2010-07-02 22:25:56 +0000336 * tAXPD=1, need design to confirm.
Dave Liuc360cea2009-03-14 12:48:30 +0800337 */
Masahiro Yamadab4141192014-11-07 03:03:31 +0900338 txp = max((int)mclk_ps * 3, (mclk_ps > 1540 ? 7500 : 6000));
York Sunbb578322014-08-21 16:13:22 -0700339
York Sun938bbb62014-12-02 11:18:09 -0800340 ip_rev = fsl_ddr_get_version();
341 if (ip_rev >= 0x40700) {
342 /*
343 * MRS_CYC = max(tMRD, tMOD)
344 * tMRD = 4nCK (8nCK for RDIMM)
345 * tMOD = max(12nCK, 15ns)
346 */
347 tmrd_mclk = max((unsigned int)12, picos_to_mclk(15000));
348 } else {
349 /*
350 * MRS_CYC = tMRD
351 * tMRD = 4nCK (8nCK for RDIMM)
352 */
353 if (popts->registered_dimm_en)
354 tmrd_mclk = 8;
355 else
356 tmrd_mclk = 4;
357 }
358
Dave Liu99bac472009-12-08 11:56:48 +0800359 /* set the turnaround time */
York Sun123922b2012-10-08 07:44:23 +0000360
361 /*
York Sun84baed22014-11-07 12:14:36 -0800362 * for single quad-rank DIMM and two-slot DIMMs
York Sun123922b2012-10-08 07:44:23 +0000363 * to avoid ODT overlap
364 */
York Sun84baed22014-11-07 12:14:36 -0800365 odt_overlap = avoid_odt_overlap(dimm_params);
366 switch (odt_overlap) {
367 case 2:
York Sun123922b2012-10-08 07:44:23 +0000368 twwt_mclk = 2;
369 trrt_mclk = 1;
York Sun84baed22014-11-07 12:14:36 -0800370 break;
371 case 1:
372 twwt_mclk = 1;
373 trrt_mclk = 0;
374 break;
375 default:
376 break;
York Sun123922b2012-10-08 07:44:23 +0000377 }
York Sun84baed22014-11-07 12:14:36 -0800378
York Sun123922b2012-10-08 07:44:23 +0000379 /* for faster clock, need more time for data setup */
380 trwt_mclk = (data_rate/1000000 > 1800) ? 2 : 1;
381
York Sun856e4b02011-02-10 10:13:10 -0800382 if ((data_rate/1000000 > 1150) || (popts->memctl_interleaving))
383 twrt_mclk = 1;
York Sune1fd16b2011-01-10 12:03:00 +0000384
385 if (popts->dynamic_power == 0) { /* powerdown is not used */
386 act_pd_exit_mclk = 1;
387 pre_pd_exit_mclk = 1;
388 taxpd_mclk = 1;
389 } else {
390 /* act_pd_exit_mclk = tXARD, see above */
York Sun34e026f2014-03-27 17:54:47 -0700391 act_pd_exit_mclk = picos_to_mclk(txp);
York Sune1fd16b2011-01-10 12:03:00 +0000392 /* Mode register MR0[A12] is '1' - fast exit */
393 pre_pd_exit_mclk = act_pd_exit_mclk;
394 taxpd_mclk = 1;
395 }
York Sun5614e712013-09-30 09:22:09 -0700396#else /* CONFIG_SYS_FSL_DDR2 */
Dave Liuc360cea2009-03-14 12:48:30 +0800397 /*
398 * (tXARD and tXARDS). Empirical?
399 * tXARD = 2 for DDR2
400 * tXP=2
401 * tAXPD=8
402 */
403 act_pd_exit_mclk = 2;
404 pre_pd_exit_mclk = 2;
405 taxpd_mclk = 8;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500406 tmrd_mclk = 2;
Dave Liuc360cea2009-03-14 12:48:30 +0800407#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500408
York Sun23f96702011-05-27 13:44:28 +0800409 if (popts->trwt_override)
410 trwt_mclk = popts->trwt;
411
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500412 ddr->timing_cfg_0 = (0
413 | ((trwt_mclk & 0x3) << 30) /* RWT */
414 | ((twrt_mclk & 0x3) << 28) /* WRT */
415 | ((trrt_mclk & 0x3) << 26) /* RRT */
416 | ((twwt_mclk & 0x3) << 24) /* WWT */
York Sund4263b82013-06-03 12:39:06 -0700417 | ((act_pd_exit_mclk & 0xf) << 20) /* ACT_PD_EXIT */
Dave Liu22ff3d02008-11-21 16:31:29 +0800418 | ((pre_pd_exit_mclk & 0xF) << 16) /* PRE_PD_EXIT */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500419 | ((taxpd_mclk & 0xf) << 8) /* ODT_PD_EXIT */
York Sund4263b82013-06-03 12:39:06 -0700420 | ((tmrd_mclk & 0x1f) << 0) /* MRS_CYC */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500421 );
422 debug("FSLDDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
423}
York Sun84baed22014-11-07 12:14:36 -0800424#endif /* !defined(CONFIG_SYS_FSL_DDR1) */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500425
426/* DDR SDRAM Timing Configuration 3 (TIMING_CFG_3) */
427static void set_timing_cfg_3(fsl_ddr_cfg_regs_t *ddr,
York Sun45064ad2012-08-17 08:22:40 +0000428 const memctl_options_t *popts,
Dave Liuc360cea2009-03-14 12:48:30 +0800429 const common_timing_params_t *common_dimm,
York Sund4263b82013-06-03 12:39:06 -0700430 unsigned int cas_latency,
431 unsigned int additive_latency)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500432{
York Sun45064ad2012-08-17 08:22:40 +0000433 /* Extended precharge to activate interval (tRP) */
434 unsigned int ext_pretoact = 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500435 /* Extended Activate to precharge interval (tRAS) */
436 unsigned int ext_acttopre = 0;
York Sun45064ad2012-08-17 08:22:40 +0000437 /* Extended activate to read/write interval (tRCD) */
438 unsigned int ext_acttorw = 0;
439 /* Extended refresh recovery time (tRFC) */
440 unsigned int ext_refrec;
441 /* Extended MCAS latency from READ cmd */
442 unsigned int ext_caslat = 0;
York Sund4263b82013-06-03 12:39:06 -0700443 /* Extended additive latency */
444 unsigned int ext_add_lat = 0;
York Sun45064ad2012-08-17 08:22:40 +0000445 /* Extended last data to precharge interval (tWR) */
446 unsigned int ext_wrrec = 0;
447 /* Control Adjust */
448 unsigned int cntl_adj = 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500449
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530450 ext_pretoact = picos_to_mclk(common_dimm->trp_ps) >> 4;
451 ext_acttopre = picos_to_mclk(common_dimm->tras_ps) >> 4;
452 ext_acttorw = picos_to_mclk(common_dimm->trcd_ps) >> 4;
York Sun45064ad2012-08-17 08:22:40 +0000453 ext_caslat = (2 * cas_latency - 1) >> 4;
York Sund4263b82013-06-03 12:39:06 -0700454 ext_add_lat = additive_latency >> 4;
York Sun34e026f2014-03-27 17:54:47 -0700455#ifdef CONFIG_SYS_FSL_DDR4
456 ext_refrec = (picos_to_mclk(common_dimm->trfc1_ps) - 8) >> 4;
457#else
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530458 ext_refrec = (picos_to_mclk(common_dimm->trfc_ps) - 8) >> 4;
York Sun45064ad2012-08-17 08:22:40 +0000459 /* ext_wrrec only deals with 16 clock and above, or 14 with OTF */
York Sun34e026f2014-03-27 17:54:47 -0700460#endif
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530461 ext_wrrec = (picos_to_mclk(common_dimm->twr_ps) +
462 (popts->otf_burst_chop_en ? 2 : 0)) >> 4;
Dave Liuc360cea2009-03-14 12:48:30 +0800463
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500464 ddr->timing_cfg_3 = (0
York Sun45064ad2012-08-17 08:22:40 +0000465 | ((ext_pretoact & 0x1) << 28)
James Yangc45f5c02013-07-22 09:35:26 -0700466 | ((ext_acttopre & 0x3) << 24)
York Sun45064ad2012-08-17 08:22:40 +0000467 | ((ext_acttorw & 0x1) << 22)
468 | ((ext_refrec & 0x1F) << 16)
469 | ((ext_caslat & 0x3) << 12)
York Sund4263b82013-06-03 12:39:06 -0700470 | ((ext_add_lat & 0x1) << 10)
York Sun45064ad2012-08-17 08:22:40 +0000471 | ((ext_wrrec & 0x1) << 8)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500472 | ((cntl_adj & 0x7) << 0)
473 );
Haiying Wang1f293b42008-10-03 12:37:26 -0400474 debug("FSLDDR: timing_cfg_3 = 0x%08x\n", ddr->timing_cfg_3);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500475}
476
477/* DDR SDRAM Timing Configuration 1 (TIMING_CFG_1) */
478static void set_timing_cfg_1(fsl_ddr_cfg_regs_t *ddr,
Dave Liuc360cea2009-03-14 12:48:30 +0800479 const memctl_options_t *popts,
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500480 const common_timing_params_t *common_dimm,
481 unsigned int cas_latency)
482{
483 /* Precharge-to-activate interval (tRP) */
484 unsigned char pretoact_mclk;
485 /* Activate to precharge interval (tRAS) */
486 unsigned char acttopre_mclk;
487 /* Activate to read/write interval (tRCD) */
488 unsigned char acttorw_mclk;
489 /* CASLAT */
490 unsigned char caslat_ctrl;
491 /* Refresh recovery time (tRFC) ; trfc_low */
492 unsigned char refrec_ctrl;
493 /* Last data to precharge minimum interval (tWR) */
494 unsigned char wrrec_mclk;
495 /* Activate-to-activate interval (tRRD) */
496 unsigned char acttoact_mclk;
497 /* Last write data pair to read command issue interval (tWTR) */
498 unsigned char wrtord_mclk;
York Sun34e026f2014-03-27 17:54:47 -0700499#ifdef CONFIG_SYS_FSL_DDR4
500 /* DDR4 supports 10, 12, 14, 16, 18, 20, 24 */
501 static const u8 wrrec_table[] = {
502 10, 10, 10, 10, 10,
503 10, 10, 10, 10, 10,
504 12, 12, 14, 14, 16,
505 16, 18, 18, 20, 20,
506 24, 24, 24, 24};
507#else
York Sunf5b6fb72011-03-02 14:24:11 -0800508 /* DDR_SDRAM_MODE doesn't support 9,11,13,15 */
509 static const u8 wrrec_table[] = {
510 1, 2, 3, 4, 5, 6, 7, 8, 10, 10, 12, 12, 14, 14, 0, 0};
York Sun34e026f2014-03-27 17:54:47 -0700511#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500512
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530513 pretoact_mclk = picos_to_mclk(common_dimm->trp_ps);
514 acttopre_mclk = picos_to_mclk(common_dimm->tras_ps);
515 acttorw_mclk = picos_to_mclk(common_dimm->trcd_ps);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500516
517 /*
518 * Translate CAS Latency to a DDR controller field value:
519 *
520 * CAS Lat DDR I DDR II Ctrl
521 * Clocks SPD Bit SPD Bit Value
522 * ------- ------- ------- -----
523 * 1.0 0 0001
524 * 1.5 1 0010
525 * 2.0 2 2 0011
526 * 2.5 3 0100
527 * 3.0 4 3 0101
528 * 3.5 5 0110
529 * 4.0 4 0111
530 * 4.5 1000
531 * 5.0 5 1001
532 */
York Sun5614e712013-09-30 09:22:09 -0700533#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500534 caslat_ctrl = (cas_latency + 1) & 0x07;
York Sun5614e712013-09-30 09:22:09 -0700535#elif defined(CONFIG_SYS_FSL_DDR2)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500536 caslat_ctrl = 2 * cas_latency - 1;
537#else
Dave Liuc360cea2009-03-14 12:48:30 +0800538 /*
539 * if the CAS latency more than 8 cycle,
540 * we need set extend bit for it at
541 * TIMING_CFG_3[EXT_CASLAT]
542 */
York Sun34e026f2014-03-27 17:54:47 -0700543 if (fsl_ddr_get_version() <= 0x40400)
544 caslat_ctrl = 2 * cas_latency - 1;
545 else
546 caslat_ctrl = (cas_latency - 1) << 1;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500547#endif
548
York Sun34e026f2014-03-27 17:54:47 -0700549#ifdef CONFIG_SYS_FSL_DDR4
550 refrec_ctrl = picos_to_mclk(common_dimm->trfc1_ps) - 8;
551 wrrec_mclk = picos_to_mclk(common_dimm->twr_ps);
Masahiro Yamadab4141192014-11-07 03:03:31 +0900552 acttoact_mclk = max(picos_to_mclk(common_dimm->trrds_ps), 4U);
553 wrtord_mclk = max(2U, picos_to_mclk(2500));
York Sun349689b2014-04-01 14:20:49 -0700554 if ((wrrec_mclk < 1) || (wrrec_mclk > 24))
555 printf("Error: WRREC doesn't support %d clocks\n", wrrec_mclk);
York Sun34e026f2014-03-27 17:54:47 -0700556 else
557 wrrec_mclk = wrrec_table[wrrec_mclk - 1];
558#else
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530559 refrec_ctrl = picos_to_mclk(common_dimm->trfc_ps) - 8;
560 wrrec_mclk = picos_to_mclk(common_dimm->twr_ps);
York Sun34e026f2014-03-27 17:54:47 -0700561 acttoact_mclk = picos_to_mclk(common_dimm->trrd_ps);
562 wrtord_mclk = picos_to_mclk(common_dimm->twtr_ps);
York Sun349689b2014-04-01 14:20:49 -0700563 if ((wrrec_mclk < 1) || (wrrec_mclk > 16))
564 printf("Error: WRREC doesn't support %d clocks\n", wrrec_mclk);
York Sun45064ad2012-08-17 08:22:40 +0000565 else
566 wrrec_mclk = wrrec_table[wrrec_mclk - 1];
York Sun34e026f2014-03-27 17:54:47 -0700567#endif
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530568 if (popts->otf_burst_chop_en)
Dave Liuc360cea2009-03-14 12:48:30 +0800569 wrrec_mclk += 2;
570
Dave Liuc360cea2009-03-14 12:48:30 +0800571 /*
572 * JEDEC has min requirement for tRRD
573 */
York Sun5614e712013-09-30 09:22:09 -0700574#if defined(CONFIG_SYS_FSL_DDR3)
Dave Liuc360cea2009-03-14 12:48:30 +0800575 if (acttoact_mclk < 4)
576 acttoact_mclk = 4;
577#endif
Dave Liuc360cea2009-03-14 12:48:30 +0800578 /*
579 * JEDEC has some min requirements for tWTR
580 */
York Sun5614e712013-09-30 09:22:09 -0700581#if defined(CONFIG_SYS_FSL_DDR2)
Dave Liuc360cea2009-03-14 12:48:30 +0800582 if (wrtord_mclk < 2)
583 wrtord_mclk = 2;
York Sun5614e712013-09-30 09:22:09 -0700584#elif defined(CONFIG_SYS_FSL_DDR3)
Dave Liuc360cea2009-03-14 12:48:30 +0800585 if (wrtord_mclk < 4)
586 wrtord_mclk = 4;
587#endif
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530588 if (popts->otf_burst_chop_en)
Dave Liuc360cea2009-03-14 12:48:30 +0800589 wrtord_mclk += 2;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500590
591 ddr->timing_cfg_1 = (0
Dave Liu80ee3ce2008-11-21 16:31:22 +0800592 | ((pretoact_mclk & 0x0F) << 28)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500593 | ((acttopre_mclk & 0x0F) << 24)
Dave Liu80ee3ce2008-11-21 16:31:22 +0800594 | ((acttorw_mclk & 0xF) << 20)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500595 | ((caslat_ctrl & 0xF) << 16)
596 | ((refrec_ctrl & 0xF) << 12)
Dave Liu80ee3ce2008-11-21 16:31:22 +0800597 | ((wrrec_mclk & 0x0F) << 8)
York Sun57495e42012-10-08 07:44:22 +0000598 | ((acttoact_mclk & 0x0F) << 4)
599 | ((wrtord_mclk & 0x0F) << 0)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500600 );
Haiying Wang1f293b42008-10-03 12:37:26 -0400601 debug("FSLDDR: timing_cfg_1 = 0x%08x\n", ddr->timing_cfg_1);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500602}
603
604/* DDR SDRAM Timing Configuration 2 (TIMING_CFG_2) */
605static void set_timing_cfg_2(fsl_ddr_cfg_regs_t *ddr,
606 const memctl_options_t *popts,
607 const common_timing_params_t *common_dimm,
608 unsigned int cas_latency,
609 unsigned int additive_latency)
610{
611 /* Additive latency */
612 unsigned char add_lat_mclk;
613 /* CAS-to-preamble override */
614 unsigned short cpo;
615 /* Write latency */
616 unsigned char wr_lat;
617 /* Read to precharge (tRTP) */
618 unsigned char rd_to_pre;
619 /* Write command to write data strobe timing adjustment */
620 unsigned char wr_data_delay;
621 /* Minimum CKE pulse width (tCKE) */
622 unsigned char cke_pls;
623 /* Window for four activates (tFAW) */
624 unsigned short four_act;
York Sunbb578322014-08-21 16:13:22 -0700625#ifdef CONFIG_SYS_FSL_DDR3
626 const unsigned int mclk_ps = get_memory_clk_period_ps();
627#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500628
629 /* FIXME add check that this must be less than acttorw_mclk */
630 add_lat_mclk = additive_latency;
631 cpo = popts->cpo_override;
632
York Sun5614e712013-09-30 09:22:09 -0700633#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500634 /*
635 * This is a lie. It should really be 1, but if it is
636 * set to 1, bits overlap into the old controller's
637 * otherwise unused ACSM field. If we leave it 0, then
638 * the HW will magically treat it as 1 for DDR 1. Oh Yea.
639 */
640 wr_lat = 0;
York Sun5614e712013-09-30 09:22:09 -0700641#elif defined(CONFIG_SYS_FSL_DDR2)
Dave Liu6a819782009-03-14 12:48:19 +0800642 wr_lat = cas_latency - 1;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500643#else
Dave Liuc360cea2009-03-14 12:48:30 +0800644 wr_lat = compute_cas_write_latency();
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500645#endif
646
York Sun34e026f2014-03-27 17:54:47 -0700647#ifdef CONFIG_SYS_FSL_DDR4
648 rd_to_pre = picos_to_mclk(7500);
649#else
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530650 rd_to_pre = picos_to_mclk(common_dimm->trtp_ps);
York Sun34e026f2014-03-27 17:54:47 -0700651#endif
Dave Liuc360cea2009-03-14 12:48:30 +0800652 /*
653 * JEDEC has some min requirements for tRTP
654 */
York Sun5614e712013-09-30 09:22:09 -0700655#if defined(CONFIG_SYS_FSL_DDR2)
Dave Liuc360cea2009-03-14 12:48:30 +0800656 if (rd_to_pre < 2)
657 rd_to_pre = 2;
York Sun34e026f2014-03-27 17:54:47 -0700658#elif defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
Dave Liuc360cea2009-03-14 12:48:30 +0800659 if (rd_to_pre < 4)
660 rd_to_pre = 4;
Dave Liu6a819782009-03-14 12:48:19 +0800661#endif
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530662 if (popts->otf_burst_chop_en)
Dave Liuc360cea2009-03-14 12:48:30 +0800663 rd_to_pre += 2; /* according to UM */
664
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500665 wr_data_delay = popts->write_data_delay;
York Sun34e026f2014-03-27 17:54:47 -0700666#ifdef CONFIG_SYS_FSL_DDR4
667 cpo = 0;
Masahiro Yamadab4141192014-11-07 03:03:31 +0900668 cke_pls = max(3U, picos_to_mclk(5000));
York Sunbb578322014-08-21 16:13:22 -0700669#elif defined(CONFIG_SYS_FSL_DDR3)
670 /*
671 * cke pulse = max(3nCK, 7.5ns) for DDR3-800
672 * max(3nCK, 5.625ns) for DDR3-1066, 1333
673 * max(3nCK, 5ns) for DDR3-1600, 1866, 2133
674 */
Masahiro Yamadab4141192014-11-07 03:03:31 +0900675 cke_pls = max(3U, picos_to_mclk(mclk_ps > 1870 ? 7500 :
York Sunbb578322014-08-21 16:13:22 -0700676 (mclk_ps > 1245 ? 5625 : 5000)));
York Sun34e026f2014-03-27 17:54:47 -0700677#else
York Sunbb578322014-08-21 16:13:22 -0700678 cke_pls = FSL_DDR_MIN_TCKE_PULSE_WIDTH_DDR;
York Sun34e026f2014-03-27 17:54:47 -0700679#endif
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530680 four_act = picos_to_mclk(popts->tfaw_window_four_activates_ps);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500681
682 ddr->timing_cfg_2 = (0
Dave Liu22ff3d02008-11-21 16:31:29 +0800683 | ((add_lat_mclk & 0xf) << 28)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500684 | ((cpo & 0x1f) << 23)
Dave Liu22ff3d02008-11-21 16:31:29 +0800685 | ((wr_lat & 0xf) << 19)
York Sun34e026f2014-03-27 17:54:47 -0700686 | ((wr_lat & 0x10) << 14)
Dave Liuc360cea2009-03-14 12:48:30 +0800687 | ((rd_to_pre & RD_TO_PRE_MASK) << RD_TO_PRE_SHIFT)
688 | ((wr_data_delay & WR_DATA_DELAY_MASK) << WR_DATA_DELAY_SHIFT)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500689 | ((cke_pls & 0x7) << 6)
Dave Liu22ff3d02008-11-21 16:31:29 +0800690 | ((four_act & 0x3f) << 0)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500691 );
Haiying Wang1f293b42008-10-03 12:37:26 -0400692 debug("FSLDDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500693}
694
york9490ff42010-07-02 22:25:55 +0000695/* DDR SDRAM Register Control Word */
696static void set_ddr_sdram_rcw(fsl_ddr_cfg_regs_t *ddr,
York Sune1fd16b2011-01-10 12:03:00 +0000697 const memctl_options_t *popts,
york9490ff42010-07-02 22:25:55 +0000698 const common_timing_params_t *common_dimm)
699{
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530700 if (common_dimm->all_dimms_registered &&
701 !common_dimm->all_dimms_unbuffered) {
York Sune1fd16b2011-01-10 12:03:00 +0000702 if (popts->rcw_override) {
703 ddr->ddr_sdram_rcw_1 = popts->rcw_1;
704 ddr->ddr_sdram_rcw_2 = popts->rcw_2;
705 } else {
706 ddr->ddr_sdram_rcw_1 =
707 common_dimm->rcw[0] << 28 | \
708 common_dimm->rcw[1] << 24 | \
709 common_dimm->rcw[2] << 20 | \
710 common_dimm->rcw[3] << 16 | \
711 common_dimm->rcw[4] << 12 | \
712 common_dimm->rcw[5] << 8 | \
713 common_dimm->rcw[6] << 4 | \
714 common_dimm->rcw[7];
715 ddr->ddr_sdram_rcw_2 =
716 common_dimm->rcw[8] << 28 | \
717 common_dimm->rcw[9] << 24 | \
718 common_dimm->rcw[10] << 20 | \
719 common_dimm->rcw[11] << 16 | \
720 common_dimm->rcw[12] << 12 | \
721 common_dimm->rcw[13] << 8 | \
722 common_dimm->rcw[14] << 4 | \
723 common_dimm->rcw[15];
724 }
york9490ff42010-07-02 22:25:55 +0000725 debug("FSLDDR: ddr_sdram_rcw_1 = 0x%08x\n", ddr->ddr_sdram_rcw_1);
726 debug("FSLDDR: ddr_sdram_rcw_2 = 0x%08x\n", ddr->ddr_sdram_rcw_2);
727 }
728}
729
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500730/* DDR SDRAM control configuration (DDR_SDRAM_CFG) */
731static void set_ddr_sdram_cfg(fsl_ddr_cfg_regs_t *ddr,
732 const memctl_options_t *popts,
733 const common_timing_params_t *common_dimm)
734{
735 unsigned int mem_en; /* DDR SDRAM interface logic enable */
736 unsigned int sren; /* Self refresh enable (during sleep) */
737 unsigned int ecc_en; /* ECC enable. */
738 unsigned int rd_en; /* Registered DIMM enable */
739 unsigned int sdram_type; /* Type of SDRAM */
740 unsigned int dyn_pwr; /* Dynamic power management mode */
741 unsigned int dbw; /* DRAM dta bus width */
Dave Liu22ff3d02008-11-21 16:31:29 +0800742 unsigned int eight_be = 0; /* 8-beat burst enable, DDR2 is zero */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500743 unsigned int ncap = 0; /* Non-concurrent auto-precharge */
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530744 unsigned int threet_en; /* Enable 3T timing */
745 unsigned int twot_en; /* Enable 2T timing */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500746 unsigned int ba_intlv_ctl; /* Bank (CS) interleaving control */
747 unsigned int x32_en = 0; /* x32 enable */
748 unsigned int pchb8 = 0; /* precharge bit 8 enable */
749 unsigned int hse; /* Global half strength override */
York Sund28cb672014-09-05 13:52:41 +0800750 unsigned int acc_ecc_en = 0; /* Accumulated ECC enable */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500751 unsigned int mem_halt = 0; /* memory controller halt */
752 unsigned int bi = 0; /* Bypass initialization */
753
754 mem_en = 1;
755 sren = popts->self_refresh_in_sleep;
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530756 if (common_dimm->all_dimms_ecc_capable) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500757 /* Allow setting of ECC only if all DIMMs are ECC. */
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530758 ecc_en = popts->ecc_mode;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500759 } else {
760 ecc_en = 0;
761 }
762
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530763 if (common_dimm->all_dimms_registered &&
764 !common_dimm->all_dimms_unbuffered) {
York Sune1fd16b2011-01-10 12:03:00 +0000765 rd_en = 1;
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530766 twot_en = 0;
York Sune1fd16b2011-01-10 12:03:00 +0000767 } else {
768 rd_en = 0;
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530769 twot_en = popts->twot_en;
York Sune1fd16b2011-01-10 12:03:00 +0000770 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500771
772 sdram_type = CONFIG_FSL_SDRAM_TYPE;
773
774 dyn_pwr = popts->dynamic_power;
775 dbw = popts->data_bus_width;
Dave Liuc360cea2009-03-14 12:48:30 +0800776 /* 8-beat burst enable DDR-III case
777 * we must clear it when use the on-the-fly mode,
778 * must set it when use the 32-bits bus mode.
779 */
York Sun34e026f2014-03-27 17:54:47 -0700780 if ((sdram_type == SDRAM_TYPE_DDR3) ||
781 (sdram_type == SDRAM_TYPE_DDR4)) {
Dave Liuc360cea2009-03-14 12:48:30 +0800782 if (popts->burst_length == DDR_BL8)
783 eight_be = 1;
784 if (popts->burst_length == DDR_OTF)
785 eight_be = 0;
786 if (dbw == 0x1)
787 eight_be = 1;
788 }
789
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530790 threet_en = popts->threet_en;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500791 ba_intlv_ctl = popts->ba_intlv_ctl;
792 hse = popts->half_strength_driver_enable;
793
York Sund28cb672014-09-05 13:52:41 +0800794 /* set when ddr bus width < 64 */
795 acc_ecc_en = (dbw != 0 && ecc_en == 1) ? 1 : 0;
796
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500797 ddr->ddr_sdram_cfg = (0
798 | ((mem_en & 0x1) << 31)
799 | ((sren & 0x1) << 30)
800 | ((ecc_en & 0x1) << 29)
801 | ((rd_en & 0x1) << 28)
802 | ((sdram_type & 0x7) << 24)
803 | ((dyn_pwr & 0x1) << 21)
804 | ((dbw & 0x3) << 19)
805 | ((eight_be & 0x1) << 18)
806 | ((ncap & 0x1) << 17)
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530807 | ((threet_en & 0x1) << 16)
808 | ((twot_en & 0x1) << 15)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500809 | ((ba_intlv_ctl & 0x7F) << 8)
810 | ((x32_en & 0x1) << 5)
811 | ((pchb8 & 0x1) << 4)
812 | ((hse & 0x1) << 3)
York Sund28cb672014-09-05 13:52:41 +0800813 | ((acc_ecc_en & 0x1) << 2)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500814 | ((mem_halt & 0x1) << 1)
815 | ((bi & 0x1) << 0)
816 );
Haiying Wang1f293b42008-10-03 12:37:26 -0400817 debug("FSLDDR: ddr_sdram_cfg = 0x%08x\n", ddr->ddr_sdram_cfg);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500818}
819
820/* DDR SDRAM control configuration 2 (DDR_SDRAM_CFG_2) */
821static void set_ddr_sdram_cfg_2(fsl_ddr_cfg_regs_t *ddr,
York Sune1fd16b2011-01-10 12:03:00 +0000822 const memctl_options_t *popts,
823 const unsigned int unq_mrs_en)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500824{
825 unsigned int frc_sr = 0; /* Force self refresh */
826 unsigned int sr_ie = 0; /* Self-refresh interrupt enable */
York Suncae7c1b2011-08-26 11:32:40 -0700827 unsigned int odt_cfg = 0; /* ODT configuration */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500828 unsigned int num_pr; /* Number of posted refreshes */
York Sun57495e42012-10-08 07:44:22 +0000829 unsigned int slow = 0; /* DDR will be run less than 1250 */
York Sunb61e0612013-06-25 11:37:47 -0700830 unsigned int x4_en = 0; /* x4 DRAM enable */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500831 unsigned int obc_cfg; /* On-The-Fly Burst Chop Cfg */
832 unsigned int ap_en; /* Address Parity Enable */
833 unsigned int d_init; /* DRAM data initialization */
834 unsigned int rcw_en = 0; /* Register Control Word Enable */
835 unsigned int md_en = 0; /* Mirrored DIMM Enable */
york5800e7a2010-07-02 22:25:53 +0000836 unsigned int qd_en = 0; /* quad-rank DIMM Enable */
York Suncae7c1b2011-08-26 11:32:40 -0700837 int i;
York Sun34e026f2014-03-27 17:54:47 -0700838#ifndef CONFIG_SYS_FSL_DDR4
839 unsigned int dll_rst_dis = 1; /* DLL reset disable */
840 unsigned int dqs_cfg; /* DQS configuration */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500841
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530842 dqs_cfg = popts->dqs_config;
York Sun34e026f2014-03-27 17:54:47 -0700843#endif
York Suncae7c1b2011-08-26 11:32:40 -0700844 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
845 if (popts->cs_local_opts[i].odt_rd_cfg
846 || popts->cs_local_opts[i].odt_wr_cfg) {
847 odt_cfg = SDRAM_CFG2_ODT_ONLY_READ;
848 break;
849 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500850 }
851
852 num_pr = 1; /* Make this configurable */
853
854 /*
855 * 8572 manual says
856 * {TIMING_CFG_1[PRETOACT]
857 * + [DDR_SDRAM_CFG_2[NUM_PR]
858 * * ({EXT_REFREC || REFREC} + 8 + 2)]}
859 * << DDR_SDRAM_INTERVAL[REFINT]
860 */
York Sun34e026f2014-03-27 17:54:47 -0700861#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530862 obc_cfg = popts->otf_burst_chop_en;
Dave Liuc360cea2009-03-14 12:48:30 +0800863#else
864 obc_cfg = 0;
865#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500866
York Sun57495e42012-10-08 07:44:22 +0000867#if (CONFIG_SYS_FSL_DDR_VER >= FSL_DDR_VER_4_7)
868 slow = get_ddr_freq(0) < 1249000000;
869#endif
870
York Sune1fd16b2011-01-10 12:03:00 +0000871 if (popts->registered_dimm_en) {
872 rcw_en = 1;
873 ap_en = popts->ap_en;
874 } else {
York Sune1fd16b2011-01-10 12:03:00 +0000875 ap_en = 0;
876 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500877
York Sunb61e0612013-06-25 11:37:47 -0700878 x4_en = popts->x4_en ? 1 : 0;
879
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500880#if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
881 /* Use the DDR controller to auto initialize memory. */
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530882 d_init = popts->ecc_init_using_memctl;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500883 ddr->ddr_data_init = CONFIG_MEM_INIT_VALUE;
884 debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init);
885#else
886 /* Memory will be initialized via DMA, or not at all. */
887 d_init = 0;
888#endif
889
York Sun34e026f2014-03-27 17:54:47 -0700890#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
Dave Liuc360cea2009-03-14 12:48:30 +0800891 md_en = popts->mirrored_dimm;
892#endif
york5800e7a2010-07-02 22:25:53 +0000893 qd_en = popts->quad_rank_present ? 1 : 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500894 ddr->ddr_sdram_cfg_2 = (0
895 | ((frc_sr & 0x1) << 31)
896 | ((sr_ie & 0x1) << 30)
York Sun34e026f2014-03-27 17:54:47 -0700897#ifndef CONFIG_SYS_FSL_DDR4
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500898 | ((dll_rst_dis & 0x1) << 29)
899 | ((dqs_cfg & 0x3) << 26)
York Sun34e026f2014-03-27 17:54:47 -0700900#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500901 | ((odt_cfg & 0x3) << 21)
902 | ((num_pr & 0xf) << 12)
York Sun57495e42012-10-08 07:44:22 +0000903 | ((slow & 1) << 11)
York Sunb61e0612013-06-25 11:37:47 -0700904 | (x4_en << 10)
york5800e7a2010-07-02 22:25:53 +0000905 | (qd_en << 9)
York Sune1fd16b2011-01-10 12:03:00 +0000906 | (unq_mrs_en << 8)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500907 | ((obc_cfg & 0x1) << 6)
908 | ((ap_en & 0x1) << 5)
909 | ((d_init & 0x1) << 4)
910 | ((rcw_en & 0x1) << 2)
911 | ((md_en & 0x1) << 0)
912 );
Haiying Wang1f293b42008-10-03 12:37:26 -0400913 debug("FSLDDR: ddr_sdram_cfg_2 = 0x%08x\n", ddr->ddr_sdram_cfg_2);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500914}
915
York Sun34e026f2014-03-27 17:54:47 -0700916#ifdef CONFIG_SYS_FSL_DDR4
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500917/* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
Dave Liu1aa3d082009-12-16 10:24:38 -0600918static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr,
York Sune1fd16b2011-01-10 12:03:00 +0000919 const memctl_options_t *popts,
Valentin Longchamp7e157b02013-10-18 11:47:20 +0200920 const common_timing_params_t *common_dimm,
York Sune1fd16b2011-01-10 12:03:00 +0000921 const unsigned int unq_mrs_en)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500922{
923 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
924 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
York Sun34e026f2014-03-27 17:54:47 -0700925 int i;
926 unsigned int wr_crc = 0; /* Disable */
927 unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */
928 unsigned int srt = 0; /* self-refresh temerature, normal range */
929 unsigned int cwl = compute_cas_write_latency() - 9;
930 unsigned int mpr = 0; /* serial */
931 unsigned int wc_lat;
932 const unsigned int mclk_ps = get_memory_clk_period_ps();
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500933
York Sun34e026f2014-03-27 17:54:47 -0700934 if (popts->rtt_override)
935 rtt_wr = popts->rtt_wr_override_value;
936 else
937 rtt_wr = popts->cs_local_opts[0].odt_rtt_wr;
938
939 if (common_dimm->extended_op_srt)
940 srt = common_dimm->extended_op_srt;
941
942 esdmode2 = (0
943 | ((wr_crc & 0x1) << 12)
944 | ((rtt_wr & 0x3) << 9)
945 | ((srt & 0x3) << 6)
946 | ((cwl & 0x7) << 3));
947
948 if (mclk_ps >= 1250)
949 wc_lat = 0;
950 else if (mclk_ps >= 833)
951 wc_lat = 1;
952 else
953 wc_lat = 2;
954
955 esdmode3 = (0
956 | ((mpr & 0x3) << 11)
957 | ((wc_lat & 0x3) << 9));
958
959 ddr->ddr_sdram_mode_2 = (0
960 | ((esdmode2 & 0xFFFF) << 16)
961 | ((esdmode3 & 0xFFFF) << 0)
962 );
963 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
964
965 if (unq_mrs_en) { /* unique mode registers are supported */
966 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
967 if (popts->rtt_override)
968 rtt_wr = popts->rtt_wr_override_value;
969 else
970 rtt_wr = popts->cs_local_opts[i].odt_rtt_wr;
971
972 esdmode2 &= 0xF9FF; /* clear bit 10, 9 */
973 esdmode2 |= (rtt_wr & 0x3) << 9;
974 switch (i) {
975 case 1:
976 ddr->ddr_sdram_mode_4 = (0
977 | ((esdmode2 & 0xFFFF) << 16)
978 | ((esdmode3 & 0xFFFF) << 0)
979 );
980 break;
981 case 2:
982 ddr->ddr_sdram_mode_6 = (0
983 | ((esdmode2 & 0xFFFF) << 16)
984 | ((esdmode3 & 0xFFFF) << 0)
985 );
986 break;
987 case 3:
988 ddr->ddr_sdram_mode_8 = (0
989 | ((esdmode2 & 0xFFFF) << 16)
990 | ((esdmode3 & 0xFFFF) << 0)
991 );
992 break;
993 }
994 }
995 debug("FSLDDR: ddr_sdram_mode_4 = 0x%08x\n",
996 ddr->ddr_sdram_mode_4);
997 debug("FSLDDR: ddr_sdram_mode_6 = 0x%08x\n",
998 ddr->ddr_sdram_mode_6);
999 debug("FSLDDR: ddr_sdram_mode_8 = 0x%08x\n",
1000 ddr->ddr_sdram_mode_8);
1001 }
1002}
1003#elif defined(CONFIG_SYS_FSL_DDR3)
1004/* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
1005static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr,
1006 const memctl_options_t *popts,
1007 const common_timing_params_t *common_dimm,
1008 const unsigned int unq_mrs_en)
1009{
1010 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
1011 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
Kumar Gala92966832011-01-20 01:53:15 -06001012 int i;
Dave Liu1aa3d082009-12-16 10:24:38 -06001013 unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */
Dave Liuc360cea2009-03-14 12:48:30 +08001014 unsigned int srt = 0; /* self-refresh temerature, normal range */
1015 unsigned int asr = 0; /* auto self-refresh disable */
1016 unsigned int cwl = compute_cas_write_latency() - 5;
1017 unsigned int pasr = 0; /* partial array self refresh disable */
1018
Dave Liu1aa3d082009-12-16 10:24:38 -06001019 if (popts->rtt_override)
1020 rtt_wr = popts->rtt_wr_override_value;
York Sune1fd16b2011-01-10 12:03:00 +00001021 else
1022 rtt_wr = popts->cs_local_opts[0].odt_rtt_wr;
Valentin Longchamp7e157b02013-10-18 11:47:20 +02001023
1024 if (common_dimm->extended_op_srt)
1025 srt = common_dimm->extended_op_srt;
1026
Dave Liuc360cea2009-03-14 12:48:30 +08001027 esdmode2 = (0
1028 | ((rtt_wr & 0x3) << 9)
1029 | ((srt & 0x1) << 7)
1030 | ((asr & 0x1) << 6)
1031 | ((cwl & 0x7) << 3)
1032 | ((pasr & 0x7) << 0));
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001033 ddr->ddr_sdram_mode_2 = (0
1034 | ((esdmode2 & 0xFFFF) << 16)
1035 | ((esdmode3 & 0xFFFF) << 0)
1036 );
Haiying Wang1f293b42008-10-03 12:37:26 -04001037 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
York Sune1fd16b2011-01-10 12:03:00 +00001038
York Sune1fd16b2011-01-10 12:03:00 +00001039 if (unq_mrs_en) { /* unique mode registers are supported */
Kumar Galadea7f882011-11-09 10:05:10 -06001040 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Sune1fd16b2011-01-10 12:03:00 +00001041 if (popts->rtt_override)
1042 rtt_wr = popts->rtt_wr_override_value;
1043 else
1044 rtt_wr = popts->cs_local_opts[i].odt_rtt_wr;
1045
1046 esdmode2 &= 0xF9FF; /* clear bit 10, 9 */
1047 esdmode2 |= (rtt_wr & 0x3) << 9;
1048 switch (i) {
1049 case 1:
1050 ddr->ddr_sdram_mode_4 = (0
1051 | ((esdmode2 & 0xFFFF) << 16)
1052 | ((esdmode3 & 0xFFFF) << 0)
1053 );
1054 break;
1055 case 2:
1056 ddr->ddr_sdram_mode_6 = (0
1057 | ((esdmode2 & 0xFFFF) << 16)
1058 | ((esdmode3 & 0xFFFF) << 0)
1059 );
1060 break;
1061 case 3:
1062 ddr->ddr_sdram_mode_8 = (0
1063 | ((esdmode2 & 0xFFFF) << 16)
1064 | ((esdmode3 & 0xFFFF) << 0)
1065 );
1066 break;
1067 }
1068 }
1069 debug("FSLDDR: ddr_sdram_mode_4 = 0x%08x\n",
1070 ddr->ddr_sdram_mode_4);
1071 debug("FSLDDR: ddr_sdram_mode_6 = 0x%08x\n",
1072 ddr->ddr_sdram_mode_6);
1073 debug("FSLDDR: ddr_sdram_mode_8 = 0x%08x\n",
1074 ddr->ddr_sdram_mode_8);
1075 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001076}
1077
York Sun34e026f2014-03-27 17:54:47 -07001078#else /* for DDR2 and DDR1 */
1079/* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
1080static void set_ddr_sdram_mode_2(fsl_ddr_cfg_regs_t *ddr,
1081 const memctl_options_t *popts,
1082 const common_timing_params_t *common_dimm,
1083 const unsigned int unq_mrs_en)
1084{
1085 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
1086 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
1087
1088 ddr->ddr_sdram_mode_2 = (0
1089 | ((esdmode2 & 0xFFFF) << 16)
1090 | ((esdmode3 & 0xFFFF) << 0)
1091 );
1092 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
1093}
1094#endif
1095
1096#ifdef CONFIG_SYS_FSL_DDR4
1097/* DDR SDRAM Mode configuration 9 (DDR_SDRAM_MODE_9) */
1098static void set_ddr_sdram_mode_9(fsl_ddr_cfg_regs_t *ddr,
1099 const memctl_options_t *popts,
1100 const common_timing_params_t *common_dimm,
1101 const unsigned int unq_mrs_en)
1102{
1103 int i;
1104 unsigned short esdmode4 = 0; /* Extended SDRAM mode 4 */
1105 unsigned short esdmode5; /* Extended SDRAM mode 5 */
1106
1107 esdmode5 = 0x00000400; /* Data mask enabled */
1108
1109 ddr->ddr_sdram_mode_9 = (0
1110 | ((esdmode4 & 0xffff) << 16)
1111 | ((esdmode5 & 0xffff) << 0)
1112 );
1113 debug("FSLDDR: ddr_sdram_mode_9) = 0x%08x\n", ddr->ddr_sdram_mode_9);
1114 if (unq_mrs_en) { /* unique mode registers are supported */
1115 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1116 switch (i) {
1117 case 1:
1118 ddr->ddr_sdram_mode_11 = (0
1119 | ((esdmode4 & 0xFFFF) << 16)
1120 | ((esdmode5 & 0xFFFF) << 0)
1121 );
1122 break;
1123 case 2:
1124 ddr->ddr_sdram_mode_13 = (0
1125 | ((esdmode4 & 0xFFFF) << 16)
1126 | ((esdmode5 & 0xFFFF) << 0)
1127 );
1128 break;
1129 case 3:
1130 ddr->ddr_sdram_mode_15 = (0
1131 | ((esdmode4 & 0xFFFF) << 16)
1132 | ((esdmode5 & 0xFFFF) << 0)
1133 );
1134 break;
1135 }
1136 }
1137 debug("FSLDDR: ddr_sdram_mode_11 = 0x%08x\n",
1138 ddr->ddr_sdram_mode_11);
1139 debug("FSLDDR: ddr_sdram_mode_13 = 0x%08x\n",
1140 ddr->ddr_sdram_mode_13);
1141 debug("FSLDDR: ddr_sdram_mode_15 = 0x%08x\n",
1142 ddr->ddr_sdram_mode_15);
1143 }
1144}
1145
1146/* DDR SDRAM Mode configuration 10 (DDR_SDRAM_MODE_10) */
1147static void set_ddr_sdram_mode_10(fsl_ddr_cfg_regs_t *ddr,
1148 const memctl_options_t *popts,
1149 const common_timing_params_t *common_dimm,
1150 const unsigned int unq_mrs_en)
1151{
1152 int i;
1153 unsigned short esdmode6 = 0; /* Extended SDRAM mode 6 */
1154 unsigned short esdmode7 = 0; /* Extended SDRAM mode 7 */
1155 unsigned int tccdl_min = picos_to_mclk(common_dimm->tccdl_ps);
1156
1157 esdmode6 = ((tccdl_min - 4) & 0x7) << 10;
1158
1159 ddr->ddr_sdram_mode_10 = (0
1160 | ((esdmode6 & 0xffff) << 16)
1161 | ((esdmode7 & 0xffff) << 0)
1162 );
1163 debug("FSLDDR: ddr_sdram_mode_10) = 0x%08x\n", ddr->ddr_sdram_mode_10);
1164 if (unq_mrs_en) { /* unique mode registers are supported */
1165 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1166 switch (i) {
1167 case 1:
1168 ddr->ddr_sdram_mode_12 = (0
1169 | ((esdmode6 & 0xFFFF) << 16)
1170 | ((esdmode7 & 0xFFFF) << 0)
1171 );
1172 break;
1173 case 2:
1174 ddr->ddr_sdram_mode_14 = (0
1175 | ((esdmode6 & 0xFFFF) << 16)
1176 | ((esdmode7 & 0xFFFF) << 0)
1177 );
1178 break;
1179 case 3:
1180 ddr->ddr_sdram_mode_16 = (0
1181 | ((esdmode6 & 0xFFFF) << 16)
1182 | ((esdmode7 & 0xFFFF) << 0)
1183 );
1184 break;
1185 }
1186 }
1187 debug("FSLDDR: ddr_sdram_mode_12 = 0x%08x\n",
1188 ddr->ddr_sdram_mode_12);
1189 debug("FSLDDR: ddr_sdram_mode_14 = 0x%08x\n",
1190 ddr->ddr_sdram_mode_14);
1191 debug("FSLDDR: ddr_sdram_mode_16 = 0x%08x\n",
1192 ddr->ddr_sdram_mode_16);
1193 }
1194}
1195
1196#endif
1197
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001198/* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */
1199static void set_ddr_sdram_interval(fsl_ddr_cfg_regs_t *ddr,
1200 const memctl_options_t *popts,
1201 const common_timing_params_t *common_dimm)
1202{
1203 unsigned int refint; /* Refresh interval */
1204 unsigned int bstopre; /* Precharge interval */
1205
1206 refint = picos_to_mclk(common_dimm->refresh_rate_ps);
1207
1208 bstopre = popts->bstopre;
1209
1210 /* refint field used 0x3FFF in earlier controllers */
1211 ddr->ddr_sdram_interval = (0
1212 | ((refint & 0xFFFF) << 16)
1213 | ((bstopre & 0x3FFF) << 0)
1214 );
Haiying Wang1f293b42008-10-03 12:37:26 -04001215 debug("FSLDDR: ddr_sdram_interval = 0x%08x\n", ddr->ddr_sdram_interval);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001216}
1217
York Sun34e026f2014-03-27 17:54:47 -07001218#ifdef CONFIG_SYS_FSL_DDR4
Dave Liuc360cea2009-03-14 12:48:30 +08001219/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
1220static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
1221 const memctl_options_t *popts,
1222 const common_timing_params_t *common_dimm,
1223 unsigned int cas_latency,
York Sune1fd16b2011-01-10 12:03:00 +00001224 unsigned int additive_latency,
1225 const unsigned int unq_mrs_en)
Dave Liuc360cea2009-03-14 12:48:30 +08001226{
York Sun34e026f2014-03-27 17:54:47 -07001227 int i;
1228 unsigned short esdmode; /* Extended SDRAM mode */
1229 unsigned short sdmode; /* SDRAM mode */
1230
1231 /* Mode Register - MR1 */
1232 unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */
1233 unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */
1234 unsigned int rtt;
1235 unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */
1236 unsigned int al = 0; /* Posted CAS# additive latency (AL) */
1237 unsigned int dic = 0; /* Output driver impedance, 40ohm */
1238 unsigned int dll_en = 1; /* DLL Enable 1=Enable (Normal),
1239 0=Disable (Test/Debug) */
1240
1241 /* Mode Register - MR0 */
1242 unsigned int wr = 0; /* Write Recovery */
1243 unsigned int dll_rst; /* DLL Reset */
1244 unsigned int mode; /* Normal=0 or Test=1 */
1245 unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
1246 /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
1247 unsigned int bt;
1248 unsigned int bl; /* BL: Burst Length */
1249
1250 unsigned int wr_mclk;
1251 /* DDR4 support WR 10, 12, 14, 16, 18, 20, 24 */
1252 static const u8 wr_table[] = {
1253 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6};
1254 /* DDR4 support CAS 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 22, 24 */
1255 static const u8 cas_latency_table[] = {
1256 0, 1, 2, 3, 4, 5, 6, 7, 8, 8,
1257 9, 9, 10, 10, 11, 11};
1258
1259 if (popts->rtt_override)
1260 rtt = popts->rtt_override_value;
1261 else
1262 rtt = popts->cs_local_opts[0].odt_rtt_norm;
1263
1264 if (additive_latency == (cas_latency - 1))
1265 al = 1;
1266 if (additive_latency == (cas_latency - 2))
1267 al = 2;
1268
1269 if (popts->quad_rank_present)
1270 dic = 1; /* output driver impedance 240/7 ohm */
1271
1272 /*
1273 * The esdmode value will also be used for writing
1274 * MR1 during write leveling for DDR3, although the
1275 * bits specifically related to the write leveling
1276 * scheme will be handled automatically by the DDR
1277 * controller. so we set the wrlvl_en = 0 here.
1278 */
1279 esdmode = (0
1280 | ((qoff & 0x1) << 12)
1281 | ((tdqs_en & 0x1) << 11)
1282 | ((rtt & 0x7) << 8)
1283 | ((wrlvl_en & 0x1) << 7)
1284 | ((al & 0x3) << 3)
1285 | ((dic & 0x3) << 1) /* DIC field is split */
1286 | ((dll_en & 0x1) << 0)
1287 );
1288
1289 /*
1290 * DLL control for precharge PD
1291 * 0=slow exit DLL off (tXPDLL)
1292 * 1=fast exit DLL on (tXP)
1293 */
1294
1295 wr_mclk = picos_to_mclk(common_dimm->twr_ps);
1296 if (wr_mclk <= 24) {
1297 wr = wr_table[wr_mclk - 10];
1298 } else {
1299 printf("Error: unsupported write recovery for mode register wr_mclk = %d\n",
1300 wr_mclk);
1301 }
1302
1303 dll_rst = 0; /* dll no reset */
1304 mode = 0; /* normal mode */
1305
1306 /* look up table to get the cas latency bits */
1307 if (cas_latency >= 9 && cas_latency <= 24)
1308 caslat = cas_latency_table[cas_latency - 9];
1309 else
1310 printf("Error: unsupported cas latency for mode register\n");
1311
1312 bt = 0; /* Nibble sequential */
1313
1314 switch (popts->burst_length) {
1315 case DDR_BL8:
1316 bl = 0;
1317 break;
1318 case DDR_OTF:
1319 bl = 1;
1320 break;
1321 case DDR_BC4:
1322 bl = 2;
1323 break;
1324 default:
1325 printf("Error: invalid burst length of %u specified. ",
1326 popts->burst_length);
1327 puts("Defaulting to on-the-fly BC4 or BL8 beats.\n");
1328 bl = 1;
1329 break;
1330 }
1331
1332 sdmode = (0
1333 | ((wr & 0x7) << 9)
1334 | ((dll_rst & 0x1) << 8)
1335 | ((mode & 0x1) << 7)
1336 | (((caslat >> 1) & 0x7) << 4)
1337 | ((bt & 0x1) << 3)
1338 | ((caslat & 1) << 2)
1339 | ((bl & 0x3) << 0)
1340 );
1341
1342 ddr->ddr_sdram_mode = (0
1343 | ((esdmode & 0xFFFF) << 16)
1344 | ((sdmode & 0xFFFF) << 0)
1345 );
1346
1347 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
1348
1349 if (unq_mrs_en) { /* unique mode registers are supported */
1350 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1351 if (popts->rtt_override)
1352 rtt = popts->rtt_override_value;
1353 else
1354 rtt = popts->cs_local_opts[i].odt_rtt_norm;
1355
1356 esdmode &= 0xF8FF; /* clear bit 10,9,8 for rtt */
1357 esdmode |= (rtt & 0x7) << 8;
1358 switch (i) {
1359 case 1:
1360 ddr->ddr_sdram_mode_3 = (0
1361 | ((esdmode & 0xFFFF) << 16)
1362 | ((sdmode & 0xFFFF) << 0)
1363 );
1364 break;
1365 case 2:
1366 ddr->ddr_sdram_mode_5 = (0
1367 | ((esdmode & 0xFFFF) << 16)
1368 | ((sdmode & 0xFFFF) << 0)
1369 );
1370 break;
1371 case 3:
1372 ddr->ddr_sdram_mode_7 = (0
1373 | ((esdmode & 0xFFFF) << 16)
1374 | ((sdmode & 0xFFFF) << 0)
1375 );
1376 break;
1377 }
1378 }
1379 debug("FSLDDR: ddr_sdram_mode_3 = 0x%08x\n",
1380 ddr->ddr_sdram_mode_3);
1381 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1382 ddr->ddr_sdram_mode_5);
1383 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1384 ddr->ddr_sdram_mode_5);
1385 }
1386}
1387
1388#elif defined(CONFIG_SYS_FSL_DDR3)
1389/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
1390static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
1391 const memctl_options_t *popts,
1392 const common_timing_params_t *common_dimm,
1393 unsigned int cas_latency,
1394 unsigned int additive_latency,
1395 const unsigned int unq_mrs_en)
1396{
1397 int i;
Dave Liuc360cea2009-03-14 12:48:30 +08001398 unsigned short esdmode; /* Extended SDRAM mode */
1399 unsigned short sdmode; /* SDRAM mode */
1400
1401 /* Mode Register - MR1 */
1402 unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */
1403 unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */
1404 unsigned int rtt;
1405 unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */
1406 unsigned int al = 0; /* Posted CAS# additive latency (AL) */
York Sune1fd16b2011-01-10 12:03:00 +00001407 unsigned int dic = 0; /* Output driver impedance, 40ohm */
Dave Liuc360cea2009-03-14 12:48:30 +08001408 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
1409 1=Disable (Test/Debug) */
1410
1411 /* Mode Register - MR0 */
1412 unsigned int dll_on; /* DLL control for precharge PD, 0=off, 1=on */
York Sunfcea3062012-08-17 08:22:38 +00001413 unsigned int wr = 0; /* Write Recovery */
Dave Liuc360cea2009-03-14 12:48:30 +08001414 unsigned int dll_rst; /* DLL Reset */
1415 unsigned int mode; /* Normal=0 or Test=1 */
1416 unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
1417 /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
1418 unsigned int bt;
1419 unsigned int bl; /* BL: Burst Length */
1420
1421 unsigned int wr_mclk;
York Sunf5b6fb72011-03-02 14:24:11 -08001422 /*
1423 * DDR_SDRAM_MODE doesn't support 9,11,13,15
1424 * Please refer JEDEC Standard No. 79-3E for Mode Register MR0
1425 * for this table
1426 */
1427 static const u8 wr_table[] = {1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 0, 0};
Dave Liuc360cea2009-03-14 12:48:30 +08001428
Dave Liuc360cea2009-03-14 12:48:30 +08001429 if (popts->rtt_override)
1430 rtt = popts->rtt_override_value;
York Sune1fd16b2011-01-10 12:03:00 +00001431 else
1432 rtt = popts->cs_local_opts[0].odt_rtt_norm;
Dave Liuc360cea2009-03-14 12:48:30 +08001433
1434 if (additive_latency == (cas_latency - 1))
1435 al = 1;
1436 if (additive_latency == (cas_latency - 2))
1437 al = 2;
1438
York Sune1fd16b2011-01-10 12:03:00 +00001439 if (popts->quad_rank_present)
1440 dic = 1; /* output driver impedance 240/7 ohm */
1441
Dave Liuc360cea2009-03-14 12:48:30 +08001442 /*
1443 * The esdmode value will also be used for writing
1444 * MR1 during write leveling for DDR3, although the
1445 * bits specifically related to the write leveling
1446 * scheme will be handled automatically by the DDR
1447 * controller. so we set the wrlvl_en = 0 here.
1448 */
1449 esdmode = (0
1450 | ((qoff & 0x1) << 12)
1451 | ((tdqs_en & 0x1) << 11)
Kumar Gala6d8565a2009-09-10 14:54:55 -05001452 | ((rtt & 0x4) << 7) /* rtt field is split */
Dave Liuc360cea2009-03-14 12:48:30 +08001453 | ((wrlvl_en & 0x1) << 7)
Kumar Gala6d8565a2009-09-10 14:54:55 -05001454 | ((rtt & 0x2) << 5) /* rtt field is split */
1455 | ((dic & 0x2) << 4) /* DIC field is split */
Dave Liuc360cea2009-03-14 12:48:30 +08001456 | ((al & 0x3) << 3)
Kumar Gala6d8565a2009-09-10 14:54:55 -05001457 | ((rtt & 0x1) << 2) /* rtt field is split */
Dave Liuc360cea2009-03-14 12:48:30 +08001458 | ((dic & 0x1) << 1) /* DIC field is split */
1459 | ((dll_en & 0x1) << 0)
1460 );
1461
1462 /*
1463 * DLL control for precharge PD
1464 * 0=slow exit DLL off (tXPDLL)
1465 * 1=fast exit DLL on (tXP)
1466 */
1467 dll_on = 1;
York Sunf5b6fb72011-03-02 14:24:11 -08001468
York Sun34e026f2014-03-27 17:54:47 -07001469 wr_mclk = picos_to_mclk(common_dimm->twr_ps);
York Sunfcea3062012-08-17 08:22:38 +00001470 if (wr_mclk <= 16) {
1471 wr = wr_table[wr_mclk - 5];
1472 } else {
1473 printf("Error: unsupported write recovery for mode register "
1474 "wr_mclk = %d\n", wr_mclk);
1475 }
York Sunf5b6fb72011-03-02 14:24:11 -08001476
Dave Liuc360cea2009-03-14 12:48:30 +08001477 dll_rst = 0; /* dll no reset */
1478 mode = 0; /* normal mode */
1479
1480 /* look up table to get the cas latency bits */
York Sunfcea3062012-08-17 08:22:38 +00001481 if (cas_latency >= 5 && cas_latency <= 16) {
1482 unsigned char cas_latency_table[] = {
Dave Liuc360cea2009-03-14 12:48:30 +08001483 0x2, /* 5 clocks */
1484 0x4, /* 6 clocks */
1485 0x6, /* 7 clocks */
1486 0x8, /* 8 clocks */
1487 0xa, /* 9 clocks */
1488 0xc, /* 10 clocks */
York Sunfcea3062012-08-17 08:22:38 +00001489 0xe, /* 11 clocks */
1490 0x1, /* 12 clocks */
1491 0x3, /* 13 clocks */
1492 0x5, /* 14 clocks */
1493 0x7, /* 15 clocks */
1494 0x9, /* 16 clocks */
Dave Liuc360cea2009-03-14 12:48:30 +08001495 };
1496 caslat = cas_latency_table[cas_latency - 5];
York Sunfcea3062012-08-17 08:22:38 +00001497 } else {
1498 printf("Error: unsupported cas latency for mode register\n");
Dave Liuc360cea2009-03-14 12:48:30 +08001499 }
York Sunfcea3062012-08-17 08:22:38 +00001500
Dave Liuc360cea2009-03-14 12:48:30 +08001501 bt = 0; /* Nibble sequential */
1502
1503 switch (popts->burst_length) {
1504 case DDR_BL8:
1505 bl = 0;
1506 break;
1507 case DDR_OTF:
1508 bl = 1;
1509 break;
1510 case DDR_BC4:
1511 bl = 2;
1512 break;
1513 default:
1514 printf("Error: invalid burst length of %u specified. "
1515 " Defaulting to on-the-fly BC4 or BL8 beats.\n",
1516 popts->burst_length);
1517 bl = 1;
1518 break;
1519 }
1520
1521 sdmode = (0
1522 | ((dll_on & 0x1) << 12)
1523 | ((wr & 0x7) << 9)
1524 | ((dll_rst & 0x1) << 8)
1525 | ((mode & 0x1) << 7)
1526 | (((caslat >> 1) & 0x7) << 4)
1527 | ((bt & 0x1) << 3)
York Sunfcea3062012-08-17 08:22:38 +00001528 | ((caslat & 1) << 2)
Dave Liuc360cea2009-03-14 12:48:30 +08001529 | ((bl & 0x3) << 0)
1530 );
1531
1532 ddr->ddr_sdram_mode = (0
1533 | ((esdmode & 0xFFFF) << 16)
1534 | ((sdmode & 0xFFFF) << 0)
1535 );
1536
1537 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
York Sune1fd16b2011-01-10 12:03:00 +00001538
1539 if (unq_mrs_en) { /* unique mode registers are supported */
Kumar Galadea7f882011-11-09 10:05:10 -06001540 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Sune1fd16b2011-01-10 12:03:00 +00001541 if (popts->rtt_override)
1542 rtt = popts->rtt_override_value;
1543 else
1544 rtt = popts->cs_local_opts[i].odt_rtt_norm;
1545
1546 esdmode &= 0xFDBB; /* clear bit 9,6,2 */
1547 esdmode |= (0
1548 | ((rtt & 0x4) << 7) /* rtt field is split */
1549 | ((rtt & 0x2) << 5) /* rtt field is split */
1550 | ((rtt & 0x1) << 2) /* rtt field is split */
1551 );
1552 switch (i) {
1553 case 1:
1554 ddr->ddr_sdram_mode_3 = (0
1555 | ((esdmode & 0xFFFF) << 16)
1556 | ((sdmode & 0xFFFF) << 0)
1557 );
1558 break;
1559 case 2:
1560 ddr->ddr_sdram_mode_5 = (0
1561 | ((esdmode & 0xFFFF) << 16)
1562 | ((sdmode & 0xFFFF) << 0)
1563 );
1564 break;
1565 case 3:
1566 ddr->ddr_sdram_mode_7 = (0
1567 | ((esdmode & 0xFFFF) << 16)
1568 | ((sdmode & 0xFFFF) << 0)
1569 );
1570 break;
1571 }
1572 }
1573 debug("FSLDDR: ddr_sdram_mode_3 = 0x%08x\n",
1574 ddr->ddr_sdram_mode_3);
1575 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1576 ddr->ddr_sdram_mode_5);
1577 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1578 ddr->ddr_sdram_mode_5);
1579 }
Dave Liuc360cea2009-03-14 12:48:30 +08001580}
1581
York Sun5614e712013-09-30 09:22:09 -07001582#else /* !CONFIG_SYS_FSL_DDR3 */
Dave Liuc360cea2009-03-14 12:48:30 +08001583
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001584/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
1585static void set_ddr_sdram_mode(fsl_ddr_cfg_regs_t *ddr,
1586 const memctl_options_t *popts,
1587 const common_timing_params_t *common_dimm,
1588 unsigned int cas_latency,
York Sune1fd16b2011-01-10 12:03:00 +00001589 unsigned int additive_latency,
1590 const unsigned int unq_mrs_en)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001591{
1592 unsigned short esdmode; /* Extended SDRAM mode */
1593 unsigned short sdmode; /* SDRAM mode */
1594
1595 /*
1596 * FIXME: This ought to be pre-calculated in a
1597 * technology-specific routine,
1598 * e.g. compute_DDR2_mode_register(), and then the
1599 * sdmode and esdmode passed in as part of common_dimm.
1600 */
1601
1602 /* Extended Mode Register */
1603 unsigned int mrs = 0; /* Mode Register Set */
1604 unsigned int outputs = 0; /* 0=Enabled, 1=Disabled */
1605 unsigned int rdqs_en = 0; /* RDQS Enable: 0=no, 1=yes */
1606 unsigned int dqs_en = 0; /* DQS# Enable: 0=enable, 1=disable */
1607 unsigned int ocd = 0; /* 0x0=OCD not supported,
1608 0x7=OCD default state */
1609 unsigned int rtt;
1610 unsigned int al; /* Posted CAS# additive latency (AL) */
1611 unsigned int ods = 0; /* Output Drive Strength:
1612 0 = Full strength (18ohm)
1613 1 = Reduced strength (4ohm) */
1614 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
1615 1=Disable (Test/Debug) */
1616
1617 /* Mode Register (MR) */
1618 unsigned int mr; /* Mode Register Definition */
1619 unsigned int pd; /* Power-Down Mode */
1620 unsigned int wr; /* Write Recovery */
1621 unsigned int dll_res; /* DLL Reset */
1622 unsigned int mode; /* Normal=0 or Test=1 */
Kumar Gala302e52e2008-09-05 14:40:29 -05001623 unsigned int caslat = 0;/* CAS# latency */
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001624 /* BT: Burst Type (0=Sequential, 1=Interleaved) */
1625 unsigned int bt;
1626 unsigned int bl; /* BL: Burst Length */
1627
Priyanka Jain0dd38a32013-09-25 10:41:19 +05301628 dqs_en = !popts->dqs_config;
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001629 rtt = fsl_ddr_get_rtt();
1630
1631 al = additive_latency;
1632
1633 esdmode = (0
1634 | ((mrs & 0x3) << 14)
1635 | ((outputs & 0x1) << 12)
1636 | ((rdqs_en & 0x1) << 11)
1637 | ((dqs_en & 0x1) << 10)
1638 | ((ocd & 0x7) << 7)
1639 | ((rtt & 0x2) << 5) /* rtt field is split */
1640 | ((al & 0x7) << 3)
1641 | ((rtt & 0x1) << 2) /* rtt field is split */
1642 | ((ods & 0x1) << 1)
1643 | ((dll_en & 0x1) << 0)
1644 );
1645
1646 mr = 0; /* FIXME: CHECKME */
1647
1648 /*
1649 * 0 = Fast Exit (Normal)
1650 * 1 = Slow Exit (Low Power)
1651 */
1652 pd = 0;
1653
York Sun5614e712013-09-30 09:22:09 -07001654#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001655 wr = 0; /* Historical */
York Sun5614e712013-09-30 09:22:09 -07001656#elif defined(CONFIG_SYS_FSL_DDR2)
York Sun34e026f2014-03-27 17:54:47 -07001657 wr = picos_to_mclk(common_dimm->twr_ps);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001658#endif
1659 dll_res = 0;
1660 mode = 0;
1661
York Sun5614e712013-09-30 09:22:09 -07001662#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001663 if (1 <= cas_latency && cas_latency <= 4) {
1664 unsigned char mode_caslat_table[4] = {
1665 0x5, /* 1.5 clocks */
1666 0x2, /* 2.0 clocks */
1667 0x6, /* 2.5 clocks */
1668 0x3 /* 3.0 clocks */
1669 };
Kumar Gala302e52e2008-09-05 14:40:29 -05001670 caslat = mode_caslat_table[cas_latency - 1];
1671 } else {
1672 printf("Warning: unknown cas_latency %d\n", cas_latency);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001673 }
York Sun5614e712013-09-30 09:22:09 -07001674#elif defined(CONFIG_SYS_FSL_DDR2)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001675 caslat = cas_latency;
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001676#endif
1677 bt = 0;
1678
1679 switch (popts->burst_length) {
Dave Liuc360cea2009-03-14 12:48:30 +08001680 case DDR_BL4:
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001681 bl = 2;
1682 break;
Dave Liuc360cea2009-03-14 12:48:30 +08001683 case DDR_BL8:
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001684 bl = 3;
1685 break;
1686 default:
1687 printf("Error: invalid burst length of %u specified. "
1688 " Defaulting to 4 beats.\n",
1689 popts->burst_length);
1690 bl = 2;
1691 break;
1692 }
1693
1694 sdmode = (0
1695 | ((mr & 0x3) << 14)
1696 | ((pd & 0x1) << 12)
1697 | ((wr & 0x7) << 9)
1698 | ((dll_res & 0x1) << 8)
1699 | ((mode & 0x1) << 7)
1700 | ((caslat & 0x7) << 4)
1701 | ((bt & 0x1) << 3)
1702 | ((bl & 0x7) << 0)
1703 );
1704
1705 ddr->ddr_sdram_mode = (0
1706 | ((esdmode & 0xFFFF) << 16)
1707 | ((sdmode & 0xFFFF) << 0)
1708 );
Haiying Wang1f293b42008-10-03 12:37:26 -04001709 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001710}
Dave Liuc360cea2009-03-14 12:48:30 +08001711#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001712
1713/* DDR SDRAM Data Initialization (DDR_DATA_INIT) */
1714static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr)
1715{
1716 unsigned int init_value; /* Initialization value */
1717
Anatolij Gustschin5b933942013-01-21 23:50:27 +00001718#ifdef CONFIG_MEM_INIT_VALUE
1719 init_value = CONFIG_MEM_INIT_VALUE;
1720#else
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001721 init_value = 0xDEADBEEF;
Anatolij Gustschin5b933942013-01-21 23:50:27 +00001722#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001723 ddr->ddr_data_init = init_value;
1724}
1725
1726/*
1727 * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL)
1728 * The old controller on the 8540/60 doesn't have this register.
1729 * Hope it's OK to set it (to 0) anyway.
1730 */
1731static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
1732 const memctl_options_t *popts)
1733{
1734 unsigned int clk_adjust; /* Clock adjust */
1735
1736 clk_adjust = popts->clk_adjust;
1737 ddr->ddr_sdram_clk_cntl = (clk_adjust & 0xF) << 23;
york9490ff42010-07-02 22:25:55 +00001738 debug("FSLDDR: clk_cntl = 0x%08x\n", ddr->ddr_sdram_clk_cntl);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001739}
1740
1741/* DDR Initialization Address (DDR_INIT_ADDR) */
1742static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
1743{
1744 unsigned int init_addr = 0; /* Initialization address */
1745
1746 ddr->ddr_init_addr = init_addr;
1747}
1748
1749/* DDR Initialization Address (DDR_INIT_EXT_ADDR) */
1750static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
1751{
1752 unsigned int uia = 0; /* Use initialization address */
1753 unsigned int init_ext_addr = 0; /* Initialization address */
1754
1755 ddr->ddr_init_ext_addr = (0
1756 | ((uia & 0x1) << 31)
1757 | (init_ext_addr & 0xF)
1758 );
1759}
1760
1761/* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */
Dave Liuec145e82010-03-05 12:22:00 +08001762static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr,
1763 const memctl_options_t *popts)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001764{
1765 unsigned int rwt = 0; /* Read-to-write turnaround for same CS */
1766 unsigned int wrt = 0; /* Write-to-read turnaround for same CS */
1767 unsigned int rrt = 0; /* Read-to-read turnaround for same CS */
1768 unsigned int wwt = 0; /* Write-to-write turnaround for same CS */
1769 unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */
1770
York Sun34e026f2014-03-27 17:54:47 -07001771#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
Dave Liuec145e82010-03-05 12:22:00 +08001772 if (popts->burst_length == DDR_BL8) {
1773 /* We set BL/2 for fixed BL8 */
1774 rrt = 0; /* BL/2 clocks */
1775 wwt = 0; /* BL/2 clocks */
1776 } else {
1777 /* We need to set BL/2 + 2 to BC4 and OTF */
1778 rrt = 2; /* BL/2 + 2 clocks */
1779 wwt = 2; /* BL/2 + 2 clocks */
1780 }
York Sun34e026f2014-03-27 17:54:47 -07001781#endif
1782
1783#ifdef CONFIG_SYS_FSL_DDR4
1784 dll_lock = 2; /* tDLLK = 1024 clocks */
1785#elif defined(CONFIG_SYS_FSL_DDR3)
Dave Liuc360cea2009-03-14 12:48:30 +08001786 dll_lock = 1; /* tDLLK = 512 clocks from spec */
1787#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001788 ddr->timing_cfg_4 = (0
1789 | ((rwt & 0xf) << 28)
1790 | ((wrt & 0xf) << 24)
1791 | ((rrt & 0xf) << 20)
1792 | ((wwt & 0xf) << 16)
1793 | (dll_lock & 0x3)
1794 );
Haiying Wang1f293b42008-10-03 12:37:26 -04001795 debug("FSLDDR: timing_cfg_4 = 0x%08x\n", ddr->timing_cfg_4);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001796}
1797
1798/* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */
York Sune1fd16b2011-01-10 12:03:00 +00001799static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr, unsigned int cas_latency)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001800{
1801 unsigned int rodt_on = 0; /* Read to ODT on */
1802 unsigned int rodt_off = 0; /* Read to ODT off */
1803 unsigned int wodt_on = 0; /* Write to ODT on */
1804 unsigned int wodt_off = 0; /* Write to ODT off */
1805
York Sun34e026f2014-03-27 17:54:47 -07001806#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
1807 unsigned int wr_lat = ((ddr->timing_cfg_2 & 0x00780000) >> 19) +
1808 ((ddr->timing_cfg_2 & 0x00040000) >> 14);
York Sune1fd16b2011-01-10 12:03:00 +00001809 /* rodt_on = timing_cfg_1[caslat] - timing_cfg_2[wrlat] + 1 */
York Sun34e026f2014-03-27 17:54:47 -07001810 if (cas_latency >= wr_lat)
1811 rodt_on = cas_latency - wr_lat + 1;
Dave Liuc360cea2009-03-14 12:48:30 +08001812 rodt_off = 4; /* 4 clocks */
york5fb8a8a2010-07-02 22:25:56 +00001813 wodt_on = 1; /* 1 clocks */
Dave Liuc360cea2009-03-14 12:48:30 +08001814 wodt_off = 4; /* 4 clocks */
1815#endif
1816
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001817 ddr->timing_cfg_5 = (0
Dave Liu22ff3d02008-11-21 16:31:29 +08001818 | ((rodt_on & 0x1f) << 24)
1819 | ((rodt_off & 0x7) << 20)
1820 | ((wodt_on & 0x1f) << 12)
1821 | ((wodt_off & 0x7) << 8)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001822 );
Haiying Wang1f293b42008-10-03 12:37:26 -04001823 debug("FSLDDR: timing_cfg_5 = 0x%08x\n", ddr->timing_cfg_5);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001824}
1825
York Sun34e026f2014-03-27 17:54:47 -07001826#ifdef CONFIG_SYS_FSL_DDR4
1827static void set_timing_cfg_6(fsl_ddr_cfg_regs_t *ddr)
1828{
1829 unsigned int hs_caslat = 0;
1830 unsigned int hs_wrlat = 0;
1831 unsigned int hs_wrrec = 0;
1832 unsigned int hs_clkadj = 0;
1833 unsigned int hs_wrlvl_start = 0;
1834
1835 ddr->timing_cfg_6 = (0
1836 | ((hs_caslat & 0x1f) << 24)
1837 | ((hs_wrlat & 0x1f) << 19)
1838 | ((hs_wrrec & 0x1f) << 12)
1839 | ((hs_clkadj & 0x1f) << 6)
1840 | ((hs_wrlvl_start & 0x1f) << 0)
1841 );
1842 debug("FSLDDR: timing_cfg_6 = 0x%08x\n", ddr->timing_cfg_6);
1843}
1844
1845static void set_timing_cfg_7(fsl_ddr_cfg_regs_t *ddr,
1846 const common_timing_params_t *common_dimm)
1847{
1848 unsigned int txpr, tcksre, tcksrx;
1849 unsigned int cke_rst, cksre, cksrx, par_lat, cs_to_cmd;
1850
Masahiro Yamadab4141192014-11-07 03:03:31 +09001851 txpr = max(5U, picos_to_mclk(common_dimm->trfc1_ps + 10000));
1852 tcksre = max(5U, picos_to_mclk(10000));
1853 tcksrx = max(5U, picos_to_mclk(10000));
York Sun34e026f2014-03-27 17:54:47 -07001854 par_lat = 0;
1855 cs_to_cmd = 0;
1856
1857 if (txpr <= 200)
1858 cke_rst = 0;
1859 else if (txpr <= 256)
1860 cke_rst = 1;
1861 else if (txpr <= 512)
1862 cke_rst = 2;
1863 else
1864 cke_rst = 3;
1865
1866 if (tcksre <= 19)
1867 cksre = tcksre - 5;
1868 else
1869 cksre = 15;
1870
1871 if (tcksrx <= 19)
1872 cksrx = tcksrx - 5;
1873 else
1874 cksrx = 15;
1875
1876 ddr->timing_cfg_7 = (0
1877 | ((cke_rst & 0x3) << 28)
1878 | ((cksre & 0xf) << 24)
1879 | ((cksrx & 0xf) << 20)
1880 | ((par_lat & 0xf) << 16)
1881 | ((cs_to_cmd & 0xf) << 4)
1882 );
1883 debug("FSLDDR: timing_cfg_7 = 0x%08x\n", ddr->timing_cfg_7);
1884}
1885
1886static void set_timing_cfg_8(fsl_ddr_cfg_regs_t *ddr,
1887 const memctl_options_t *popts,
1888 const common_timing_params_t *common_dimm,
1889 unsigned int cas_latency)
1890{
1891 unsigned int rwt_bg, wrt_bg, rrt_bg, wwt_bg;
1892 unsigned int acttoact_bg, wrtord_bg, pre_all_rec;
1893 unsigned int tccdl = picos_to_mclk(common_dimm->tccdl_ps);
1894 unsigned int wr_lat = ((ddr->timing_cfg_2 & 0x00780000) >> 19) +
1895 ((ddr->timing_cfg_2 & 0x00040000) >> 14);
1896
1897 rwt_bg = cas_latency + 2 + 4 - wr_lat;
1898 if (rwt_bg < tccdl)
1899 rwt_bg = tccdl - rwt_bg;
1900 else
1901 rwt_bg = 0;
1902
1903 wrt_bg = wr_lat + 4 + 1 - cas_latency;
1904 if (wrt_bg < tccdl)
1905 wrt_bg = tccdl - wrt_bg;
1906 else
1907 wrt_bg = 0;
1908
1909 if (popts->burst_length == DDR_BL8) {
1910 rrt_bg = tccdl - 4;
1911 wwt_bg = tccdl - 4;
1912 } else {
1913 rrt_bg = tccdl - 2;
1914 wwt_bg = tccdl - 4;
1915 }
1916
1917 acttoact_bg = picos_to_mclk(common_dimm->trrdl_ps);
Masahiro Yamadab4141192014-11-07 03:03:31 +09001918 wrtord_bg = max(4U, picos_to_mclk(7500));
York Sun3d75ec92014-06-26 11:14:44 -07001919 if (popts->otf_burst_chop_en)
1920 wrtord_bg += 2;
1921
York Sun34e026f2014-03-27 17:54:47 -07001922 pre_all_rec = 0;
1923
1924 ddr->timing_cfg_8 = (0
1925 | ((rwt_bg & 0xf) << 28)
1926 | ((wrt_bg & 0xf) << 24)
1927 | ((rrt_bg & 0xf) << 20)
1928 | ((wwt_bg & 0xf) << 16)
1929 | ((acttoact_bg & 0xf) << 12)
1930 | ((wrtord_bg & 0xf) << 8)
1931 | ((pre_all_rec & 0x1f) << 0)
1932 );
1933
1934 debug("FSLDDR: timing_cfg_8 = 0x%08x\n", ddr->timing_cfg_8);
1935}
1936
1937static void set_timing_cfg_9(fsl_ddr_cfg_regs_t *ddr)
1938{
1939 ddr->timing_cfg_9 = 0;
1940 debug("FSLDDR: timing_cfg_9 = 0x%08x\n", ddr->timing_cfg_9);
1941}
1942
York Sunf80d6472014-09-11 13:32:06 -07001943/* This function needs to be called after set_ddr_sdram_cfg() is called */
York Sun34e026f2014-03-27 17:54:47 -07001944static void set_ddr_dq_mapping(fsl_ddr_cfg_regs_t *ddr,
1945 const dimm_params_t *dimm_params)
1946{
York Sunf80d6472014-09-11 13:32:06 -07001947 unsigned int acc_ecc_en = (ddr->ddr_sdram_cfg >> 2) & 0x1;
1948
York Sun34e026f2014-03-27 17:54:47 -07001949 ddr->dq_map_0 = ((dimm_params->dq_mapping[0] & 0x3F) << 26) |
1950 ((dimm_params->dq_mapping[1] & 0x3F) << 20) |
1951 ((dimm_params->dq_mapping[2] & 0x3F) << 14) |
1952 ((dimm_params->dq_mapping[3] & 0x3F) << 8) |
1953 ((dimm_params->dq_mapping[4] & 0x3F) << 2);
1954
1955 ddr->dq_map_1 = ((dimm_params->dq_mapping[5] & 0x3F) << 26) |
1956 ((dimm_params->dq_mapping[6] & 0x3F) << 20) |
1957 ((dimm_params->dq_mapping[7] & 0x3F) << 14) |
1958 ((dimm_params->dq_mapping[10] & 0x3F) << 8) |
1959 ((dimm_params->dq_mapping[11] & 0x3F) << 2);
1960
1961 ddr->dq_map_2 = ((dimm_params->dq_mapping[12] & 0x3F) << 26) |
1962 ((dimm_params->dq_mapping[13] & 0x3F) << 20) |
1963 ((dimm_params->dq_mapping[14] & 0x3F) << 14) |
1964 ((dimm_params->dq_mapping[15] & 0x3F) << 8) |
1965 ((dimm_params->dq_mapping[16] & 0x3F) << 2);
1966
York Sunf80d6472014-09-11 13:32:06 -07001967 /* dq_map for ECC[4:7] is set to 0 if accumulated ECC is enabled */
York Sun34e026f2014-03-27 17:54:47 -07001968 ddr->dq_map_3 = ((dimm_params->dq_mapping[17] & 0x3F) << 26) |
1969 ((dimm_params->dq_mapping[8] & 0x3F) << 20) |
York Sunf80d6472014-09-11 13:32:06 -07001970 (acc_ecc_en ? 0 :
1971 (dimm_params->dq_mapping[9] & 0x3F) << 14) |
York Sun34e026f2014-03-27 17:54:47 -07001972 dimm_params->dq_mapping_ors;
1973
1974 debug("FSLDDR: dq_map_0 = 0x%08x\n", ddr->dq_map_0);
1975 debug("FSLDDR: dq_map_1 = 0x%08x\n", ddr->dq_map_1);
1976 debug("FSLDDR: dq_map_2 = 0x%08x\n", ddr->dq_map_2);
1977 debug("FSLDDR: dq_map_3 = 0x%08x\n", ddr->dq_map_3);
1978}
1979static void set_ddr_sdram_cfg_3(fsl_ddr_cfg_regs_t *ddr,
1980 const memctl_options_t *popts)
1981{
1982 int rd_pre;
1983
1984 rd_pre = popts->quad_rank_present ? 1 : 0;
1985
1986 ddr->ddr_sdram_cfg_3 = (rd_pre & 0x1) << 16;
1987
1988 debug("FSLDDR: ddr_sdram_cfg_3 = 0x%08x\n", ddr->ddr_sdram_cfg_3);
1989}
1990#endif /* CONFIG_SYS_FSL_DDR4 */
1991
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001992/* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */
Dave Liuc360cea2009-03-14 12:48:30 +08001993static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int zq_en)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001994{
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001995 unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */
1996 /* Normal Operation Full Calibration Time (tZQoper) */
1997 unsigned int zqoper = 0;
1998 /* Normal Operation Short Calibration Time (tZQCS) */
1999 unsigned int zqcs = 0;
York Sun34e026f2014-03-27 17:54:47 -07002000#ifdef CONFIG_SYS_FSL_DDR4
2001 unsigned int zqcs_init;
2002#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002003
Dave Liuc360cea2009-03-14 12:48:30 +08002004 if (zq_en) {
York Sun34e026f2014-03-27 17:54:47 -07002005#ifdef CONFIG_SYS_FSL_DDR4
2006 zqinit = 10; /* 1024 clocks */
2007 zqoper = 9; /* 512 clocks */
2008 zqcs = 7; /* 128 clocks */
2009 zqcs_init = 5; /* 1024 refresh sequences */
2010#else
Dave Liuc360cea2009-03-14 12:48:30 +08002011 zqinit = 9; /* 512 clocks */
2012 zqoper = 8; /* 256 clocks */
2013 zqcs = 6; /* 64 clocks */
York Sun34e026f2014-03-27 17:54:47 -07002014#endif
Dave Liuc360cea2009-03-14 12:48:30 +08002015 }
2016
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002017 ddr->ddr_zq_cntl = (0
2018 | ((zq_en & 0x1) << 31)
2019 | ((zqinit & 0xF) << 24)
2020 | ((zqoper & 0xF) << 16)
2021 | ((zqcs & 0xF) << 8)
York Sun34e026f2014-03-27 17:54:47 -07002022#ifdef CONFIG_SYS_FSL_DDR4
2023 | ((zqcs_init & 0xF) << 0)
2024#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002025 );
York Sune1fd16b2011-01-10 12:03:00 +00002026 debug("FSLDDR: zq_cntl = 0x%08x\n", ddr->ddr_zq_cntl);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002027}
2028
2029/* DDR Write Leveling Control (DDR_WRLVL_CNTL) */
Dave Liubdc9f7b2009-12-16 10:24:37 -06002030static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int wrlvl_en,
2031 const memctl_options_t *popts)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002032{
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002033 /*
2034 * First DQS pulse rising edge after margining mode
2035 * is programmed (tWL_MRD)
2036 */
2037 unsigned int wrlvl_mrd = 0;
2038 /* ODT delay after margining mode is programmed (tWL_ODTEN) */
2039 unsigned int wrlvl_odten = 0;
2040 /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */
2041 unsigned int wrlvl_dqsen = 0;
2042 /* WRLVL_SMPL: Write leveling sample time */
2043 unsigned int wrlvl_smpl = 0;
2044 /* WRLVL_WLR: Write leveling repeition time */
2045 unsigned int wrlvl_wlr = 0;
2046 /* WRLVL_START: Write leveling start time */
2047 unsigned int wrlvl_start = 0;
2048
Dave Liuc360cea2009-03-14 12:48:30 +08002049 /* suggest enable write leveling for DDR3 due to fly-by topology */
2050 if (wrlvl_en) {
2051 /* tWL_MRD min = 40 nCK, we set it 64 */
2052 wrlvl_mrd = 0x6;
2053 /* tWL_ODTEN 128 */
2054 wrlvl_odten = 0x7;
2055 /* tWL_DQSEN min = 25 nCK, we set it 32 */
2056 wrlvl_dqsen = 0x5;
2057 /*
Dave Liubdc9f7b2009-12-16 10:24:37 -06002058 * Write leveling sample time at least need 6 clocks
2059 * higher than tWLO to allow enough time for progagation
2060 * delay and sampling the prime data bits.
Dave Liuc360cea2009-03-14 12:48:30 +08002061 */
2062 wrlvl_smpl = 0xf;
2063 /*
2064 * Write leveling repetition time
2065 * at least tWLO + 6 clocks clocks
york5fb8a8a2010-07-02 22:25:56 +00002066 * we set it 64
Dave Liuc360cea2009-03-14 12:48:30 +08002067 */
york5fb8a8a2010-07-02 22:25:56 +00002068 wrlvl_wlr = 0x6;
Dave Liuc360cea2009-03-14 12:48:30 +08002069 /*
2070 * Write leveling start time
2071 * The value use for the DQS_ADJUST for the first sample
York Sune1fd16b2011-01-10 12:03:00 +00002072 * when write leveling is enabled. It probably needs to be
2073 * overriden per platform.
Dave Liuc360cea2009-03-14 12:48:30 +08002074 */
2075 wrlvl_start = 0x8;
Dave Liubdc9f7b2009-12-16 10:24:37 -06002076 /*
2077 * Override the write leveling sample and start time
2078 * according to specific board
2079 */
2080 if (popts->wrlvl_override) {
2081 wrlvl_smpl = popts->wrlvl_sample;
2082 wrlvl_start = popts->wrlvl_start;
2083 }
Dave Liuc360cea2009-03-14 12:48:30 +08002084 }
2085
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002086 ddr->ddr_wrlvl_cntl = (0
2087 | ((wrlvl_en & 0x1) << 31)
2088 | ((wrlvl_mrd & 0x7) << 24)
2089 | ((wrlvl_odten & 0x7) << 20)
2090 | ((wrlvl_dqsen & 0x7) << 16)
2091 | ((wrlvl_smpl & 0xf) << 12)
2092 | ((wrlvl_wlr & 0x7) << 8)
Dave Liu22ff3d02008-11-21 16:31:29 +08002093 | ((wrlvl_start & 0x1F) << 0)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002094 );
York Sune1fd16b2011-01-10 12:03:00 +00002095 debug("FSLDDR: wrlvl_cntl = 0x%08x\n", ddr->ddr_wrlvl_cntl);
York Sun57495e42012-10-08 07:44:22 +00002096 ddr->ddr_wrlvl_cntl_2 = popts->wrlvl_ctl_2;
2097 debug("FSLDDR: wrlvl_cntl_2 = 0x%08x\n", ddr->ddr_wrlvl_cntl_2);
2098 ddr->ddr_wrlvl_cntl_3 = popts->wrlvl_ctl_3;
2099 debug("FSLDDR: wrlvl_cntl_3 = 0x%08x\n", ddr->ddr_wrlvl_cntl_3);
2100
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002101}
2102
2103/* DDR Self Refresh Counter (DDR_SR_CNTR) */
Dave Liu22cca7e2008-11-21 16:31:35 +08002104static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr, unsigned int sr_it)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002105{
Dave Liu22cca7e2008-11-21 16:31:35 +08002106 /* Self Refresh Idle Threshold */
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002107 ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
2108}
2109
york7fd101c2010-07-02 22:25:54 +00002110static void set_ddr_eor(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
2111{
2112 if (popts->addr_hash) {
2113 ddr->ddr_eor = 0x40000000; /* address hash enable */
Kumar Galac2a63f42011-03-18 11:53:06 -05002114 puts("Address hashing enabled.\n");
york7fd101c2010-07-02 22:25:54 +00002115 }
2116}
2117
York Sune1fd16b2011-01-10 12:03:00 +00002118static void set_ddr_cdr1(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
2119{
2120 ddr->ddr_cdr1 = popts->ddr_cdr1;
2121 debug("FSLDDR: ddr_cdr1 = 0x%08x\n", ddr->ddr_cdr1);
2122}
2123
York Sun57495e42012-10-08 07:44:22 +00002124static void set_ddr_cdr2(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
2125{
2126 ddr->ddr_cdr2 = popts->ddr_cdr2;
2127 debug("FSLDDR: ddr_cdr2 = 0x%08x\n", ddr->ddr_cdr2);
2128}
2129
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002130unsigned int
2131check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
2132{
2133 unsigned int res = 0;
2134
2135 /*
2136 * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are
2137 * not set at the same time.
2138 */
2139 if (ddr->ddr_sdram_cfg & 0x10000000
2140 && ddr->ddr_sdram_cfg & 0x00008000) {
2141 printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
2142 " should not be set at the same time.\n");
2143 res++;
2144 }
2145
2146 return res;
2147}
2148
2149unsigned int
2150compute_fsl_memctl_config_regs(const memctl_options_t *popts,
2151 fsl_ddr_cfg_regs_t *ddr,
2152 const common_timing_params_t *common_dimm,
2153 const dimm_params_t *dimm_params,
Haiying Wangfc0c2b62010-12-01 10:35:31 -05002154 unsigned int dbw_cap_adj,
2155 unsigned int size_only)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002156{
2157 unsigned int i;
2158 unsigned int cas_latency;
2159 unsigned int additive_latency;
Dave Liu22cca7e2008-11-21 16:31:35 +08002160 unsigned int sr_it;
Dave Liuc360cea2009-03-14 12:48:30 +08002161 unsigned int zq_en;
2162 unsigned int wrlvl_en;
York Sune1fd16b2011-01-10 12:03:00 +00002163 unsigned int ip_rev = 0;
2164 unsigned int unq_mrs_en = 0;
York Sun58edbc92010-10-18 13:46:50 -07002165 int cs_en = 1;
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002166
2167 memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
2168
2169 if (common_dimm == NULL) {
2170 printf("Error: subset DIMM params struct null pointer\n");
2171 return 1;
2172 }
2173
2174 /*
2175 * Process overrides first.
2176 *
2177 * FIXME: somehow add dereated caslat to this
2178 */
2179 cas_latency = (popts->cas_latency_override)
2180 ? popts->cas_latency_override_value
York Sun34e026f2014-03-27 17:54:47 -07002181 : common_dimm->lowest_common_spd_caslat;
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002182
2183 additive_latency = (popts->additive_latency_override)
2184 ? popts->additive_latency_override_value
2185 : common_dimm->additive_latency;
2186
Dave Liu22cca7e2008-11-21 16:31:35 +08002187 sr_it = (popts->auto_self_refresh_en)
2188 ? popts->sr_it
2189 : 0;
Dave Liuc360cea2009-03-14 12:48:30 +08002190 /* ZQ calibration */
2191 zq_en = (popts->zq_en) ? 1 : 0;
2192 /* write leveling */
2193 wrlvl_en = (popts->wrlvl_en) ? 1 : 0;
Dave Liu22cca7e2008-11-21 16:31:35 +08002194
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002195 /* Chip Select Memory Bounds (CSn_BNDS) */
2196 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Suna4c66502012-08-17 08:22:39 +00002197 unsigned long long ea, sa;
york076bff82010-07-02 22:25:52 +00002198 unsigned int cs_per_dimm
2199 = CONFIG_CHIP_SELECTS_PER_CTRL / CONFIG_DIMM_SLOTS_PER_CTLR;
2200 unsigned int dimm_number
2201 = i / cs_per_dimm;
2202 unsigned long long rank_density
York Suna4c66502012-08-17 08:22:39 +00002203 = dimm_params[dimm_number].rank_density >> dbw_cap_adj;
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002204
york076bff82010-07-02 22:25:52 +00002205 if (dimm_params[dimm_number].n_ranks == 0) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002206 debug("Skipping setup of CS%u "
york5800e7a2010-07-02 22:25:53 +00002207 "because n_ranks on DIMM %u is 0\n", i, dimm_number);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002208 continue;
2209 }
York Suna4c66502012-08-17 08:22:39 +00002210 if (popts->memctl_interleaving) {
york076bff82010-07-02 22:25:52 +00002211 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
York Suna4c66502012-08-17 08:22:39 +00002212 case FSL_DDR_CS0_CS1_CS2_CS3:
2213 break;
york076bff82010-07-02 22:25:52 +00002214 case FSL_DDR_CS0_CS1:
2215 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
York Sun58edbc92010-10-18 13:46:50 -07002216 if (i > 1)
2217 cs_en = 0;
york076bff82010-07-02 22:25:52 +00002218 break;
2219 case FSL_DDR_CS2_CS3:
York Suna4c66502012-08-17 08:22:39 +00002220 default:
York Sun58edbc92010-10-18 13:46:50 -07002221 if (i > 0)
2222 cs_en = 0;
york076bff82010-07-02 22:25:52 +00002223 break;
york076bff82010-07-02 22:25:52 +00002224 }
York Suna4c66502012-08-17 08:22:39 +00002225 sa = common_dimm->base_address;
York Sun123922b2012-10-08 07:44:23 +00002226 ea = sa + common_dimm->total_mem - 1;
York Suna4c66502012-08-17 08:22:39 +00002227 } else if (!popts->memctl_interleaving) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002228 /*
2229 * If memory interleaving between controllers is NOT
2230 * enabled, the starting address for each memory
2231 * controller is distinct. However, because rank
2232 * interleaving is enabled, the starting and ending
2233 * addresses of the total memory on that memory
2234 * controller needs to be programmed into its
2235 * respective CS0_BNDS.
2236 */
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002237 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
2238 case FSL_DDR_CS0_CS1_CS2_CS3:
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002239 sa = common_dimm->base_address;
York Sun123922b2012-10-08 07:44:23 +00002240 ea = sa + common_dimm->total_mem - 1;
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002241 break;
2242 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
York Suna4c66502012-08-17 08:22:39 +00002243 if ((i >= 2) && (dimm_number == 0)) {
york076bff82010-07-02 22:25:52 +00002244 sa = dimm_params[dimm_number].base_address +
York Suna4c66502012-08-17 08:22:39 +00002245 2 * rank_density;
2246 ea = sa + 2 * rank_density - 1;
york076bff82010-07-02 22:25:52 +00002247 } else {
2248 sa = dimm_params[dimm_number].base_address;
York Suna4c66502012-08-17 08:22:39 +00002249 ea = sa + 2 * rank_density - 1;
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002250 }
2251 break;
2252 case FSL_DDR_CS0_CS1:
york076bff82010-07-02 22:25:52 +00002253 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
2254 sa = dimm_params[dimm_number].base_address;
York Suna4c66502012-08-17 08:22:39 +00002255 ea = sa + rank_density - 1;
2256 if (i != 1)
2257 sa += (i % cs_per_dimm) * rank_density;
2258 ea += (i % cs_per_dimm) * rank_density;
york076bff82010-07-02 22:25:52 +00002259 } else {
2260 sa = 0;
2261 ea = 0;
2262 }
2263 if (i == 0)
York Suna4c66502012-08-17 08:22:39 +00002264 ea += rank_density;
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002265 break;
2266 case FSL_DDR_CS2_CS3:
york076bff82010-07-02 22:25:52 +00002267 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
2268 sa = dimm_params[dimm_number].base_address;
York Suna4c66502012-08-17 08:22:39 +00002269 ea = sa + rank_density - 1;
2270 if (i != 3)
2271 sa += (i % cs_per_dimm) * rank_density;
2272 ea += (i % cs_per_dimm) * rank_density;
york076bff82010-07-02 22:25:52 +00002273 } else {
2274 sa = 0;
2275 ea = 0;
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002276 }
york076bff82010-07-02 22:25:52 +00002277 if (i == 2)
2278 ea += (rank_density >> dbw_cap_adj);
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002279 break;
2280 default: /* No bank(chip-select) interleaving */
York Suna4c66502012-08-17 08:22:39 +00002281 sa = dimm_params[dimm_number].base_address;
2282 ea = sa + rank_density - 1;
2283 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
2284 sa += (i % cs_per_dimm) * rank_density;
2285 ea += (i % cs_per_dimm) * rank_density;
2286 } else {
2287 sa = 0;
2288 ea = 0;
2289 }
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002290 break;
2291 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002292 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002293
2294 sa >>= 24;
2295 ea >>= 24;
2296
York Sun123922b2012-10-08 07:44:23 +00002297 if (cs_en) {
2298 ddr->cs[i].bnds = (0
York Sund4263b82013-06-03 12:39:06 -07002299 | ((sa & 0xffff) << 16) /* starting address */
2300 | ((ea & 0xffff) << 0) /* ending address */
York Sun123922b2012-10-08 07:44:23 +00002301 );
2302 } else {
York Sund8556db2013-06-25 11:37:45 -07002303 /* setting bnds to 0xffffffff for inactive CS */
2304 ddr->cs[i].bnds = 0xffffffff;
York Sun123922b2012-10-08 07:44:23 +00002305 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002306
Haiying Wang1f293b42008-10-03 12:37:26 -04002307 debug("FSLDDR: cs[%d]_bnds = 0x%08x\n", i, ddr->cs[i].bnds);
York Sun123922b2012-10-08 07:44:23 +00002308 set_csn_config(dimm_number, i, ddr, popts, dimm_params);
2309 set_csn_config_2(i, ddr);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002310 }
2311
Haiying Wangfc0c2b62010-12-01 10:35:31 -05002312 /*
2313 * In the case we only need to compute the ddr sdram size, we only need
2314 * to set csn registers, so return from here.
2315 */
2316 if (size_only)
2317 return 0;
2318
york7fd101c2010-07-02 22:25:54 +00002319 set_ddr_eor(ddr, popts);
2320
York Sun5614e712013-09-30 09:22:09 -07002321#if !defined(CONFIG_SYS_FSL_DDR1)
York Sun123922b2012-10-08 07:44:23 +00002322 set_timing_cfg_0(ddr, popts, dimm_params);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002323#endif
2324
York Sund4263b82013-06-03 12:39:06 -07002325 set_timing_cfg_3(ddr, popts, common_dimm, cas_latency,
2326 additive_latency);
Dave Liuc360cea2009-03-14 12:48:30 +08002327 set_timing_cfg_1(ddr, popts, common_dimm, cas_latency);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002328 set_timing_cfg_2(ddr, popts, common_dimm,
2329 cas_latency, additive_latency);
2330
York Sune1fd16b2011-01-10 12:03:00 +00002331 set_ddr_cdr1(ddr, popts);
York Sun57495e42012-10-08 07:44:22 +00002332 set_ddr_cdr2(ddr, popts);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002333 set_ddr_sdram_cfg(ddr, popts, common_dimm);
York Sune1fd16b2011-01-10 12:03:00 +00002334 ip_rev = fsl_ddr_get_version();
2335 if (ip_rev > 0x40400)
2336 unq_mrs_en = 1;
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002337
York Sunf80d6472014-09-11 13:32:06 -07002338 if ((ip_rev > 0x40700) && (popts->cswl_override != 0))
York Sunef87cab2014-09-05 13:52:43 +08002339 ddr->debug[18] = popts->cswl_override;
2340
York Sune1fd16b2011-01-10 12:03:00 +00002341 set_ddr_sdram_cfg_2(ddr, popts, unq_mrs_en);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002342 set_ddr_sdram_mode(ddr, popts, common_dimm,
York Sune1fd16b2011-01-10 12:03:00 +00002343 cas_latency, additive_latency, unq_mrs_en);
Valentin Longchamp7e157b02013-10-18 11:47:20 +02002344 set_ddr_sdram_mode_2(ddr, popts, common_dimm, unq_mrs_en);
York Sun34e026f2014-03-27 17:54:47 -07002345#ifdef CONFIG_SYS_FSL_DDR4
2346 set_ddr_sdram_mode_9(ddr, popts, common_dimm, unq_mrs_en);
2347 set_ddr_sdram_mode_10(ddr, popts, common_dimm, unq_mrs_en);
2348#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002349 set_ddr_sdram_interval(ddr, popts, common_dimm);
2350 set_ddr_data_init(ddr);
2351 set_ddr_sdram_clk_cntl(ddr, popts);
2352 set_ddr_init_addr(ddr);
2353 set_ddr_init_ext_addr(ddr);
Dave Liuec145e82010-03-05 12:22:00 +08002354 set_timing_cfg_4(ddr, popts);
York Sune1fd16b2011-01-10 12:03:00 +00002355 set_timing_cfg_5(ddr, cas_latency);
York Sun34e026f2014-03-27 17:54:47 -07002356#ifdef CONFIG_SYS_FSL_DDR4
2357 set_ddr_sdram_cfg_3(ddr, popts);
2358 set_timing_cfg_6(ddr);
2359 set_timing_cfg_7(ddr, common_dimm);
2360 set_timing_cfg_8(ddr, popts, common_dimm, cas_latency);
2361 set_timing_cfg_9(ddr);
2362 set_ddr_dq_mapping(ddr, dimm_params);
2363#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002364
Dave Liuc360cea2009-03-14 12:48:30 +08002365 set_ddr_zq_cntl(ddr, zq_en);
Dave Liubdc9f7b2009-12-16 10:24:37 -06002366 set_ddr_wrlvl_cntl(ddr, wrlvl_en, popts);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002367
Dave Liu22cca7e2008-11-21 16:31:35 +08002368 set_ddr_sr_cntr(ddr, sr_it);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002369
York Sune1fd16b2011-01-10 12:03:00 +00002370 set_ddr_sdram_rcw(ddr, popts, common_dimm);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002371
York Suncb930712013-06-25 11:37:41 -07002372#ifdef CONFIG_SYS_FSL_DDR_EMU
2373 /* disble DDR training for emulator */
2374 ddr->debug[2] = 0x00000400;
2375 ddr->debug[4] = 0xff800000;
2376#endif
York Sun9855b3b2014-05-23 13:15:00 -07002377#ifdef CONFIG_SYS_FSL_ERRATUM_A004508
2378 if ((ip_rev >= 0x40000) && (ip_rev < 0x40400))
2379 ddr->debug[2] |= 0x00000200; /* set bit 22 */
2380#endif
2381
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002382 return check_fsl_memctl_config_regs(ddr);
2383}