blob: 9073917914e9630a77d4df47870c248357b0f53d [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
Kumar Gala58e5e9a2008-08-26 15:01:29 -050020/*
21 * Determine Rtt value.
22 *
23 * This should likely be either board or controller specific.
24 *
Dave Liuc360cea2009-03-14 12:48:30 +080025 * Rtt(nominal) - DDR2:
Kumar Gala58e5e9a2008-08-26 15:01:29 -050026 * 0 = Rtt disabled
27 * 1 = 75 ohm
28 * 2 = 150 ohm
29 * 3 = 50 ohm
Dave Liuc360cea2009-03-14 12:48:30 +080030 * Rtt(nominal) - DDR3:
31 * 0 = Rtt disabled
32 * 1 = 60 ohm
33 * 2 = 120 ohm
34 * 3 = 40 ohm
35 * 4 = 20 ohm
36 * 5 = 30 ohm
Kumar Gala58e5e9a2008-08-26 15:01:29 -050037 *
38 * FIXME: Apparently 8641 needs a value of 2
39 * FIXME: Old code seys if 667 MHz or higher, use 3 on 8572
40 *
41 * FIXME: There was some effort down this line earlier:
42 *
43 * unsigned int i;
44 * for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL/2; i++) {
45 * if (popts->dimmslot[i].num_valid_cs
46 * && (popts->cs_local_opts[2*i].odt_rd_cfg
47 * || popts->cs_local_opts[2*i].odt_wr_cfg)) {
48 * rtt = 2;
49 * break;
50 * }
51 * }
52 */
53static inline int fsl_ddr_get_rtt(void)
54{
55 int rtt;
56
York Sun5614e712013-09-30 09:22:09 -070057#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala58e5e9a2008-08-26 15:01:29 -050058 rtt = 0;
York Sun5614e712013-09-30 09:22:09 -070059#elif defined(CONFIG_SYS_FSL_DDR2)
Kumar Gala58e5e9a2008-08-26 15:01:29 -050060 rtt = 3;
61#else
Dave Liuc360cea2009-03-14 12:48:30 +080062 rtt = 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -050063#endif
64
65 return rtt;
66}
67
York Sun34e026f2014-03-27 17:54:47 -070068#ifdef CONFIG_SYS_FSL_DDR4
69/*
70 * compute CAS write latency according to DDR4 spec
71 * CWL = 9 for <= 1600MT/s
72 * 10 for <= 1866MT/s
73 * 11 for <= 2133MT/s
74 * 12 for <= 2400MT/s
75 * 14 for <= 2667MT/s
76 * 16 for <= 2933MT/s
77 * 18 for higher
78 */
York Sun03e664d2015-01-06 13:18:50 -080079static inline unsigned int compute_cas_write_latency(
80 const unsigned int ctrl_num)
York Sun34e026f2014-03-27 17:54:47 -070081{
82 unsigned int cwl;
York Sun03e664d2015-01-06 13:18:50 -080083 const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
York Sun34e026f2014-03-27 17:54:47 -070084 if (mclk_ps >= 1250)
85 cwl = 9;
86 else if (mclk_ps >= 1070)
87 cwl = 10;
88 else if (mclk_ps >= 935)
89 cwl = 11;
90 else if (mclk_ps >= 833)
91 cwl = 12;
92 else if (mclk_ps >= 750)
93 cwl = 14;
94 else if (mclk_ps >= 681)
95 cwl = 16;
96 else
97 cwl = 18;
98
99 return cwl;
100}
101#else
Dave Liuc360cea2009-03-14 12:48:30 +0800102/*
103 * compute the CAS write latency according to DDR3 spec
104 * CWL = 5 if tCK >= 2.5ns
105 * 6 if 2.5ns > tCK >= 1.875ns
106 * 7 if 1.875ns > tCK >= 1.5ns
107 * 8 if 1.5ns > tCK >= 1.25ns
York Sun2bba85f2011-08-24 09:40:25 -0700108 * 9 if 1.25ns > tCK >= 1.07ns
109 * 10 if 1.07ns > tCK >= 0.935ns
110 * 11 if 0.935ns > tCK >= 0.833ns
111 * 12 if 0.833ns > tCK >= 0.75ns
Dave Liuc360cea2009-03-14 12:48:30 +0800112 */
York Sun03e664d2015-01-06 13:18:50 -0800113static inline unsigned int compute_cas_write_latency(
114 const unsigned int ctrl_num)
Dave Liuc360cea2009-03-14 12:48:30 +0800115{
116 unsigned int cwl;
York Sun03e664d2015-01-06 13:18:50 -0800117 const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
Dave Liuc360cea2009-03-14 12:48:30 +0800118
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 Sun03e664d2015-01-06 13:18:50 -0800290static void set_timing_cfg_0(const unsigned int ctrl_num,
291 fsl_ddr_cfg_regs_t *ddr,
York Sun123922b2012-10-08 07:44:23 +0000292 const memctl_options_t *popts,
293 const dimm_params_t *dimm_params)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500294{
295 unsigned char trwt_mclk = 0; /* Read-to-write turnaround */
296 unsigned char twrt_mclk = 0; /* Write-to-read turnaround */
297 /* 7.5 ns on -3E; 0 means WL - CL + BL/2 + 1 */
298 unsigned char trrt_mclk = 0; /* Read-to-read turnaround */
299 unsigned char twwt_mclk = 0; /* Write-to-write turnaround */
300
301 /* Active powerdown exit timing (tXARD and tXARDS). */
302 unsigned char act_pd_exit_mclk;
303 /* Precharge powerdown exit timing (tXP). */
304 unsigned char pre_pd_exit_mclk;
york5fb8a8a2010-07-02 22:25:56 +0000305 /* ODT powerdown exit timing (tAXPD). */
York Sun34e026f2014-03-27 17:54:47 -0700306 unsigned char taxpd_mclk = 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500307 /* Mode register set cycle time (tMRD). */
308 unsigned char tmrd_mclk;
York Sunbb578322014-08-21 16:13:22 -0700309#if defined(CONFIG_SYS_FSL_DDR4) || defined(CONFIG_SYS_FSL_DDR3)
York Sun03e664d2015-01-06 13:18:50 -0800310 const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
York Sunbb578322014-08-21 16:13:22 -0700311#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500312
York Sun34e026f2014-03-27 17:54:47 -0700313#ifdef CONFIG_SYS_FSL_DDR4
314 /* tXP=max(4nCK, 6ns) */
Masahiro Yamadab4141192014-11-07 03:03:31 +0900315 int txp = max((int)mclk_ps * 4, 6000); /* unit=ps */
York Sun66869f92015-03-19 09:30:26 -0700316 unsigned int data_rate = get_ddr_freq(ctrl_num);
317
318 /* for faster clock, need more time for data setup */
319 trwt_mclk = (data_rate/1000000 > 1900) ? 3 : 2;
York Sun6c6e0062015-11-04 10:03:21 -0800320
321 /*
322 * for single quad-rank DIMM and two-slot DIMMs
323 * to avoid ODT overlap
324 */
325 switch (avoid_odt_overlap(dimm_params)) {
326 case 2:
327 twrt_mclk = 2;
328 twwt_mclk = 2;
329 trrt_mclk = 2;
330 break;
331 default:
332 twrt_mclk = 1;
333 twwt_mclk = 1;
334 trrt_mclk = 0;
335 break;
336 }
337
York Sun03e664d2015-01-06 13:18:50 -0800338 act_pd_exit_mclk = picos_to_mclk(ctrl_num, txp);
York Sun34e026f2014-03-27 17:54:47 -0700339 pre_pd_exit_mclk = act_pd_exit_mclk;
340 /*
341 * MRS_CYC = max(tMRD, tMOD)
342 * tMRD = 8nCK, tMOD = max(24nCK, 15ns)
343 */
York Sun03e664d2015-01-06 13:18:50 -0800344 tmrd_mclk = max(24U, picos_to_mclk(ctrl_num, 15000));
York Sun34e026f2014-03-27 17:54:47 -0700345#elif defined(CONFIG_SYS_FSL_DDR3)
York Sun03e664d2015-01-06 13:18:50 -0800346 unsigned int data_rate = get_ddr_freq(ctrl_num);
York Sunbb578322014-08-21 16:13:22 -0700347 int txp;
York Sun938bbb62014-12-02 11:18:09 -0800348 unsigned int ip_rev;
York Sun84baed22014-11-07 12:14:36 -0800349 int odt_overlap;
Dave Liuc360cea2009-03-14 12:48:30 +0800350 /*
351 * (tXARD and tXARDS). Empirical?
352 * The DDR3 spec has not tXARD,
353 * we use the tXP instead of it.
York Sunbb578322014-08-21 16:13:22 -0700354 * tXP=max(3nCK, 7.5ns) for DDR3-800, 1066
355 * max(3nCK, 6ns) for DDR3-1333, 1600, 1866, 2133
Dave Liuc360cea2009-03-14 12:48:30 +0800356 * spec has not the tAXPD, we use
york5fb8a8a2010-07-02 22:25:56 +0000357 * tAXPD=1, need design to confirm.
Dave Liuc360cea2009-03-14 12:48:30 +0800358 */
Masahiro Yamadab4141192014-11-07 03:03:31 +0900359 txp = max((int)mclk_ps * 3, (mclk_ps > 1540 ? 7500 : 6000));
York Sunbb578322014-08-21 16:13:22 -0700360
York Sun66869f92015-03-19 09:30:26 -0700361 ip_rev = fsl_ddr_get_version(ctrl_num);
York Sun938bbb62014-12-02 11:18:09 -0800362 if (ip_rev >= 0x40700) {
363 /*
364 * MRS_CYC = max(tMRD, tMOD)
365 * tMRD = 4nCK (8nCK for RDIMM)
366 * tMOD = max(12nCK, 15ns)
367 */
York Sun03e664d2015-01-06 13:18:50 -0800368 tmrd_mclk = max((unsigned int)12,
369 picos_to_mclk(ctrl_num, 15000));
York Sun938bbb62014-12-02 11:18:09 -0800370 } else {
371 /*
372 * MRS_CYC = tMRD
373 * tMRD = 4nCK (8nCK for RDIMM)
374 */
375 if (popts->registered_dimm_en)
376 tmrd_mclk = 8;
377 else
378 tmrd_mclk = 4;
379 }
380
Dave Liu99bac472009-12-08 11:56:48 +0800381 /* set the turnaround time */
York Sun123922b2012-10-08 07:44:23 +0000382
383 /*
York Sun84baed22014-11-07 12:14:36 -0800384 * for single quad-rank DIMM and two-slot DIMMs
York Sun123922b2012-10-08 07:44:23 +0000385 * to avoid ODT overlap
386 */
York Sun84baed22014-11-07 12:14:36 -0800387 odt_overlap = avoid_odt_overlap(dimm_params);
388 switch (odt_overlap) {
389 case 2:
York Sun123922b2012-10-08 07:44:23 +0000390 twwt_mclk = 2;
391 trrt_mclk = 1;
York Sun84baed22014-11-07 12:14:36 -0800392 break;
393 case 1:
394 twwt_mclk = 1;
395 trrt_mclk = 0;
396 break;
397 default:
398 break;
York Sun123922b2012-10-08 07:44:23 +0000399 }
York Sun84baed22014-11-07 12:14:36 -0800400
York Sun123922b2012-10-08 07:44:23 +0000401 /* for faster clock, need more time for data setup */
402 trwt_mclk = (data_rate/1000000 > 1800) ? 2 : 1;
403
York Sun856e4b02011-02-10 10:13:10 -0800404 if ((data_rate/1000000 > 1150) || (popts->memctl_interleaving))
405 twrt_mclk = 1;
York Sune1fd16b2011-01-10 12:03:00 +0000406
407 if (popts->dynamic_power == 0) { /* powerdown is not used */
408 act_pd_exit_mclk = 1;
409 pre_pd_exit_mclk = 1;
410 taxpd_mclk = 1;
411 } else {
412 /* act_pd_exit_mclk = tXARD, see above */
York Sun03e664d2015-01-06 13:18:50 -0800413 act_pd_exit_mclk = picos_to_mclk(ctrl_num, txp);
York Sune1fd16b2011-01-10 12:03:00 +0000414 /* Mode register MR0[A12] is '1' - fast exit */
415 pre_pd_exit_mclk = act_pd_exit_mclk;
416 taxpd_mclk = 1;
417 }
York Sun5614e712013-09-30 09:22:09 -0700418#else /* CONFIG_SYS_FSL_DDR2 */
Dave Liuc360cea2009-03-14 12:48:30 +0800419 /*
420 * (tXARD and tXARDS). Empirical?
421 * tXARD = 2 for DDR2
422 * tXP=2
423 * tAXPD=8
424 */
425 act_pd_exit_mclk = 2;
426 pre_pd_exit_mclk = 2;
427 taxpd_mclk = 8;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500428 tmrd_mclk = 2;
Dave Liuc360cea2009-03-14 12:48:30 +0800429#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500430
York Sun23f96702011-05-27 13:44:28 +0800431 if (popts->trwt_override)
432 trwt_mclk = popts->trwt;
433
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500434 ddr->timing_cfg_0 = (0
435 | ((trwt_mclk & 0x3) << 30) /* RWT */
436 | ((twrt_mclk & 0x3) << 28) /* WRT */
437 | ((trrt_mclk & 0x3) << 26) /* RRT */
438 | ((twwt_mclk & 0x3) << 24) /* WWT */
York Sund4263b82013-06-03 12:39:06 -0700439 | ((act_pd_exit_mclk & 0xf) << 20) /* ACT_PD_EXIT */
Dave Liu22ff3d02008-11-21 16:31:29 +0800440 | ((pre_pd_exit_mclk & 0xF) << 16) /* PRE_PD_EXIT */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500441 | ((taxpd_mclk & 0xf) << 8) /* ODT_PD_EXIT */
York Sund4263b82013-06-03 12:39:06 -0700442 | ((tmrd_mclk & 0x1f) << 0) /* MRS_CYC */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500443 );
444 debug("FSLDDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
445}
York Sun84baed22014-11-07 12:14:36 -0800446#endif /* !defined(CONFIG_SYS_FSL_DDR1) */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500447
448/* DDR SDRAM Timing Configuration 3 (TIMING_CFG_3) */
York Sun03e664d2015-01-06 13:18:50 -0800449static void set_timing_cfg_3(const unsigned int ctrl_num,
450 fsl_ddr_cfg_regs_t *ddr,
451 const memctl_options_t *popts,
452 const common_timing_params_t *common_dimm,
453 unsigned int cas_latency,
454 unsigned int additive_latency)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500455{
York Sun45064ad2012-08-17 08:22:40 +0000456 /* Extended precharge to activate interval (tRP) */
457 unsigned int ext_pretoact = 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500458 /* Extended Activate to precharge interval (tRAS) */
459 unsigned int ext_acttopre = 0;
York Sun45064ad2012-08-17 08:22:40 +0000460 /* Extended activate to read/write interval (tRCD) */
461 unsigned int ext_acttorw = 0;
462 /* Extended refresh recovery time (tRFC) */
463 unsigned int ext_refrec;
464 /* Extended MCAS latency from READ cmd */
465 unsigned int ext_caslat = 0;
York Sund4263b82013-06-03 12:39:06 -0700466 /* Extended additive latency */
467 unsigned int ext_add_lat = 0;
York Sun45064ad2012-08-17 08:22:40 +0000468 /* Extended last data to precharge interval (tWR) */
469 unsigned int ext_wrrec = 0;
470 /* Control Adjust */
471 unsigned int cntl_adj = 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500472
York Sun03e664d2015-01-06 13:18:50 -0800473 ext_pretoact = picos_to_mclk(ctrl_num, common_dimm->trp_ps) >> 4;
474 ext_acttopre = picos_to_mclk(ctrl_num, common_dimm->tras_ps) >> 4;
475 ext_acttorw = picos_to_mclk(ctrl_num, common_dimm->trcd_ps) >> 4;
York Sun45064ad2012-08-17 08:22:40 +0000476 ext_caslat = (2 * cas_latency - 1) >> 4;
York Sund4263b82013-06-03 12:39:06 -0700477 ext_add_lat = additive_latency >> 4;
York Sun34e026f2014-03-27 17:54:47 -0700478#ifdef CONFIG_SYS_FSL_DDR4
York Sun03e664d2015-01-06 13:18:50 -0800479 ext_refrec = (picos_to_mclk(ctrl_num, common_dimm->trfc1_ps) - 8) >> 4;
York Sun34e026f2014-03-27 17:54:47 -0700480#else
York Sun03e664d2015-01-06 13:18:50 -0800481 ext_refrec = (picos_to_mclk(ctrl_num, common_dimm->trfc_ps) - 8) >> 4;
York Sun45064ad2012-08-17 08:22:40 +0000482 /* ext_wrrec only deals with 16 clock and above, or 14 with OTF */
York Sun34e026f2014-03-27 17:54:47 -0700483#endif
York Sun03e664d2015-01-06 13:18:50 -0800484 ext_wrrec = (picos_to_mclk(ctrl_num, common_dimm->twr_ps) +
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530485 (popts->otf_burst_chop_en ? 2 : 0)) >> 4;
Dave Liuc360cea2009-03-14 12:48:30 +0800486
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500487 ddr->timing_cfg_3 = (0
York Sun45064ad2012-08-17 08:22:40 +0000488 | ((ext_pretoact & 0x1) << 28)
James Yangc45f5c02013-07-22 09:35:26 -0700489 | ((ext_acttopre & 0x3) << 24)
York Sun45064ad2012-08-17 08:22:40 +0000490 | ((ext_acttorw & 0x1) << 22)
491 | ((ext_refrec & 0x1F) << 16)
492 | ((ext_caslat & 0x3) << 12)
York Sund4263b82013-06-03 12:39:06 -0700493 | ((ext_add_lat & 0x1) << 10)
York Sun45064ad2012-08-17 08:22:40 +0000494 | ((ext_wrrec & 0x1) << 8)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500495 | ((cntl_adj & 0x7) << 0)
496 );
Haiying Wang1f293b42008-10-03 12:37:26 -0400497 debug("FSLDDR: timing_cfg_3 = 0x%08x\n", ddr->timing_cfg_3);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500498}
499
500/* DDR SDRAM Timing Configuration 1 (TIMING_CFG_1) */
York Sun03e664d2015-01-06 13:18:50 -0800501static void set_timing_cfg_1(const unsigned int ctrl_num,
502 fsl_ddr_cfg_regs_t *ddr,
503 const memctl_options_t *popts,
504 const common_timing_params_t *common_dimm,
505 unsigned int cas_latency)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500506{
507 /* Precharge-to-activate interval (tRP) */
508 unsigned char pretoact_mclk;
509 /* Activate to precharge interval (tRAS) */
510 unsigned char acttopre_mclk;
511 /* Activate to read/write interval (tRCD) */
512 unsigned char acttorw_mclk;
513 /* CASLAT */
514 unsigned char caslat_ctrl;
515 /* Refresh recovery time (tRFC) ; trfc_low */
516 unsigned char refrec_ctrl;
517 /* Last data to precharge minimum interval (tWR) */
518 unsigned char wrrec_mclk;
519 /* Activate-to-activate interval (tRRD) */
520 unsigned char acttoact_mclk;
521 /* Last write data pair to read command issue interval (tWTR) */
522 unsigned char wrtord_mclk;
York Sun34e026f2014-03-27 17:54:47 -0700523#ifdef CONFIG_SYS_FSL_DDR4
524 /* DDR4 supports 10, 12, 14, 16, 18, 20, 24 */
525 static const u8 wrrec_table[] = {
526 10, 10, 10, 10, 10,
527 10, 10, 10, 10, 10,
528 12, 12, 14, 14, 16,
529 16, 18, 18, 20, 20,
530 24, 24, 24, 24};
531#else
York Sunf5b6fb72011-03-02 14:24:11 -0800532 /* DDR_SDRAM_MODE doesn't support 9,11,13,15 */
533 static const u8 wrrec_table[] = {
534 1, 2, 3, 4, 5, 6, 7, 8, 10, 10, 12, 12, 14, 14, 0, 0};
York Sun34e026f2014-03-27 17:54:47 -0700535#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500536
York Sun03e664d2015-01-06 13:18:50 -0800537 pretoact_mclk = picos_to_mclk(ctrl_num, common_dimm->trp_ps);
538 acttopre_mclk = picos_to_mclk(ctrl_num, common_dimm->tras_ps);
539 acttorw_mclk = picos_to_mclk(ctrl_num, common_dimm->trcd_ps);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500540
541 /*
542 * Translate CAS Latency to a DDR controller field value:
543 *
544 * CAS Lat DDR I DDR II Ctrl
545 * Clocks SPD Bit SPD Bit Value
546 * ------- ------- ------- -----
547 * 1.0 0 0001
548 * 1.5 1 0010
549 * 2.0 2 2 0011
550 * 2.5 3 0100
551 * 3.0 4 3 0101
552 * 3.5 5 0110
553 * 4.0 4 0111
554 * 4.5 1000
555 * 5.0 5 1001
556 */
York Sun5614e712013-09-30 09:22:09 -0700557#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500558 caslat_ctrl = (cas_latency + 1) & 0x07;
York Sun5614e712013-09-30 09:22:09 -0700559#elif defined(CONFIG_SYS_FSL_DDR2)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500560 caslat_ctrl = 2 * cas_latency - 1;
561#else
Dave Liuc360cea2009-03-14 12:48:30 +0800562 /*
563 * if the CAS latency more than 8 cycle,
564 * we need set extend bit for it at
565 * TIMING_CFG_3[EXT_CASLAT]
566 */
York Sun66869f92015-03-19 09:30:26 -0700567 if (fsl_ddr_get_version(ctrl_num) <= 0x40400)
York Sun34e026f2014-03-27 17:54:47 -0700568 caslat_ctrl = 2 * cas_latency - 1;
569 else
570 caslat_ctrl = (cas_latency - 1) << 1;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500571#endif
572
York Sun34e026f2014-03-27 17:54:47 -0700573#ifdef CONFIG_SYS_FSL_DDR4
York Sun03e664d2015-01-06 13:18:50 -0800574 refrec_ctrl = picos_to_mclk(ctrl_num, common_dimm->trfc1_ps) - 8;
575 wrrec_mclk = picos_to_mclk(ctrl_num, common_dimm->twr_ps);
576 acttoact_mclk = max(picos_to_mclk(ctrl_num, common_dimm->trrds_ps), 4U);
577 wrtord_mclk = max(2U, picos_to_mclk(ctrl_num, 2500));
York Sun349689b2014-04-01 14:20:49 -0700578 if ((wrrec_mclk < 1) || (wrrec_mclk > 24))
579 printf("Error: WRREC doesn't support %d clocks\n", wrrec_mclk);
York Sun34e026f2014-03-27 17:54:47 -0700580 else
581 wrrec_mclk = wrrec_table[wrrec_mclk - 1];
582#else
York Sun03e664d2015-01-06 13:18:50 -0800583 refrec_ctrl = picos_to_mclk(ctrl_num, common_dimm->trfc_ps) - 8;
584 wrrec_mclk = picos_to_mclk(ctrl_num, common_dimm->twr_ps);
585 acttoact_mclk = picos_to_mclk(ctrl_num, common_dimm->trrd_ps);
586 wrtord_mclk = picos_to_mclk(ctrl_num, common_dimm->twtr_ps);
York Sun349689b2014-04-01 14:20:49 -0700587 if ((wrrec_mclk < 1) || (wrrec_mclk > 16))
588 printf("Error: WRREC doesn't support %d clocks\n", wrrec_mclk);
York Sun45064ad2012-08-17 08:22:40 +0000589 else
590 wrrec_mclk = wrrec_table[wrrec_mclk - 1];
York Sun34e026f2014-03-27 17:54:47 -0700591#endif
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530592 if (popts->otf_burst_chop_en)
Dave Liuc360cea2009-03-14 12:48:30 +0800593 wrrec_mclk += 2;
594
Dave Liuc360cea2009-03-14 12:48:30 +0800595 /*
596 * JEDEC has min requirement for tRRD
597 */
York Sun5614e712013-09-30 09:22:09 -0700598#if defined(CONFIG_SYS_FSL_DDR3)
Dave Liuc360cea2009-03-14 12:48:30 +0800599 if (acttoact_mclk < 4)
600 acttoact_mclk = 4;
601#endif
Dave Liuc360cea2009-03-14 12:48:30 +0800602 /*
603 * JEDEC has some min requirements for tWTR
604 */
York Sun5614e712013-09-30 09:22:09 -0700605#if defined(CONFIG_SYS_FSL_DDR2)
Dave Liuc360cea2009-03-14 12:48:30 +0800606 if (wrtord_mclk < 2)
607 wrtord_mclk = 2;
York Sun5614e712013-09-30 09:22:09 -0700608#elif defined(CONFIG_SYS_FSL_DDR3)
Dave Liuc360cea2009-03-14 12:48:30 +0800609 if (wrtord_mclk < 4)
610 wrtord_mclk = 4;
611#endif
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530612 if (popts->otf_burst_chop_en)
Dave Liuc360cea2009-03-14 12:48:30 +0800613 wrtord_mclk += 2;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500614
615 ddr->timing_cfg_1 = (0
Dave Liu80ee3ce2008-11-21 16:31:22 +0800616 | ((pretoact_mclk & 0x0F) << 28)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500617 | ((acttopre_mclk & 0x0F) << 24)
Dave Liu80ee3ce2008-11-21 16:31:22 +0800618 | ((acttorw_mclk & 0xF) << 20)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500619 | ((caslat_ctrl & 0xF) << 16)
620 | ((refrec_ctrl & 0xF) << 12)
Dave Liu80ee3ce2008-11-21 16:31:22 +0800621 | ((wrrec_mclk & 0x0F) << 8)
York Sun57495e42012-10-08 07:44:22 +0000622 | ((acttoact_mclk & 0x0F) << 4)
623 | ((wrtord_mclk & 0x0F) << 0)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500624 );
Haiying Wang1f293b42008-10-03 12:37:26 -0400625 debug("FSLDDR: timing_cfg_1 = 0x%08x\n", ddr->timing_cfg_1);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500626}
627
628/* DDR SDRAM Timing Configuration 2 (TIMING_CFG_2) */
York Sun03e664d2015-01-06 13:18:50 -0800629static void set_timing_cfg_2(const unsigned int ctrl_num,
630 fsl_ddr_cfg_regs_t *ddr,
631 const memctl_options_t *popts,
632 const common_timing_params_t *common_dimm,
633 unsigned int cas_latency,
634 unsigned int additive_latency)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500635{
636 /* Additive latency */
637 unsigned char add_lat_mclk;
638 /* CAS-to-preamble override */
639 unsigned short cpo;
640 /* Write latency */
641 unsigned char wr_lat;
642 /* Read to precharge (tRTP) */
643 unsigned char rd_to_pre;
644 /* Write command to write data strobe timing adjustment */
645 unsigned char wr_data_delay;
646 /* Minimum CKE pulse width (tCKE) */
647 unsigned char cke_pls;
648 /* Window for four activates (tFAW) */
649 unsigned short four_act;
York Sunbb578322014-08-21 16:13:22 -0700650#ifdef CONFIG_SYS_FSL_DDR3
York Sun03e664d2015-01-06 13:18:50 -0800651 const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
York Sunbb578322014-08-21 16:13:22 -0700652#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500653
654 /* FIXME add check that this must be less than acttorw_mclk */
655 add_lat_mclk = additive_latency;
656 cpo = popts->cpo_override;
657
York Sun5614e712013-09-30 09:22:09 -0700658#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500659 /*
660 * This is a lie. It should really be 1, but if it is
661 * set to 1, bits overlap into the old controller's
662 * otherwise unused ACSM field. If we leave it 0, then
663 * the HW will magically treat it as 1 for DDR 1. Oh Yea.
664 */
665 wr_lat = 0;
York Sun5614e712013-09-30 09:22:09 -0700666#elif defined(CONFIG_SYS_FSL_DDR2)
Dave Liu6a819782009-03-14 12:48:19 +0800667 wr_lat = cas_latency - 1;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500668#else
York Sun03e664d2015-01-06 13:18:50 -0800669 wr_lat = compute_cas_write_latency(ctrl_num);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500670#endif
671
York Sun34e026f2014-03-27 17:54:47 -0700672#ifdef CONFIG_SYS_FSL_DDR4
York Sun03e664d2015-01-06 13:18:50 -0800673 rd_to_pre = picos_to_mclk(ctrl_num, 7500);
York Sun34e026f2014-03-27 17:54:47 -0700674#else
York Sun03e664d2015-01-06 13:18:50 -0800675 rd_to_pre = picos_to_mclk(ctrl_num, common_dimm->trtp_ps);
York Sun34e026f2014-03-27 17:54:47 -0700676#endif
Dave Liuc360cea2009-03-14 12:48:30 +0800677 /*
678 * JEDEC has some min requirements for tRTP
679 */
York Sun5614e712013-09-30 09:22:09 -0700680#if defined(CONFIG_SYS_FSL_DDR2)
Dave Liuc360cea2009-03-14 12:48:30 +0800681 if (rd_to_pre < 2)
682 rd_to_pre = 2;
York Sun34e026f2014-03-27 17:54:47 -0700683#elif defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
Dave Liuc360cea2009-03-14 12:48:30 +0800684 if (rd_to_pre < 4)
685 rd_to_pre = 4;
Dave Liu6a819782009-03-14 12:48:19 +0800686#endif
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530687 if (popts->otf_burst_chop_en)
Dave Liuc360cea2009-03-14 12:48:30 +0800688 rd_to_pre += 2; /* according to UM */
689
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500690 wr_data_delay = popts->write_data_delay;
York Sun34e026f2014-03-27 17:54:47 -0700691#ifdef CONFIG_SYS_FSL_DDR4
692 cpo = 0;
York Sun03e664d2015-01-06 13:18:50 -0800693 cke_pls = max(3U, picos_to_mclk(ctrl_num, 5000));
York Sunbb578322014-08-21 16:13:22 -0700694#elif defined(CONFIG_SYS_FSL_DDR3)
695 /*
696 * cke pulse = max(3nCK, 7.5ns) for DDR3-800
697 * max(3nCK, 5.625ns) for DDR3-1066, 1333
698 * max(3nCK, 5ns) for DDR3-1600, 1866, 2133
699 */
York Sun03e664d2015-01-06 13:18:50 -0800700 cke_pls = max(3U, picos_to_mclk(ctrl_num, mclk_ps > 1870 ? 7500 :
701 (mclk_ps > 1245 ? 5625 : 5000)));
York Sun34e026f2014-03-27 17:54:47 -0700702#else
York Sunbb578322014-08-21 16:13:22 -0700703 cke_pls = FSL_DDR_MIN_TCKE_PULSE_WIDTH_DDR;
York Sun34e026f2014-03-27 17:54:47 -0700704#endif
York Sun03e664d2015-01-06 13:18:50 -0800705 four_act = picos_to_mclk(ctrl_num,
706 popts->tfaw_window_four_activates_ps);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500707
708 ddr->timing_cfg_2 = (0
Dave Liu22ff3d02008-11-21 16:31:29 +0800709 | ((add_lat_mclk & 0xf) << 28)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500710 | ((cpo & 0x1f) << 23)
Dave Liu22ff3d02008-11-21 16:31:29 +0800711 | ((wr_lat & 0xf) << 19)
York Sun34e026f2014-03-27 17:54:47 -0700712 | ((wr_lat & 0x10) << 14)
Dave Liuc360cea2009-03-14 12:48:30 +0800713 | ((rd_to_pre & RD_TO_PRE_MASK) << RD_TO_PRE_SHIFT)
714 | ((wr_data_delay & WR_DATA_DELAY_MASK) << WR_DATA_DELAY_SHIFT)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500715 | ((cke_pls & 0x7) << 6)
Dave Liu22ff3d02008-11-21 16:31:29 +0800716 | ((four_act & 0x3f) << 0)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500717 );
Haiying Wang1f293b42008-10-03 12:37:26 -0400718 debug("FSLDDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500719}
720
york9490ff42010-07-02 22:25:55 +0000721/* DDR SDRAM Register Control Word */
722static void set_ddr_sdram_rcw(fsl_ddr_cfg_regs_t *ddr,
York Sune1fd16b2011-01-10 12:03:00 +0000723 const memctl_options_t *popts,
york9490ff42010-07-02 22:25:55 +0000724 const common_timing_params_t *common_dimm)
725{
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530726 if (common_dimm->all_dimms_registered &&
727 !common_dimm->all_dimms_unbuffered) {
York Sune1fd16b2011-01-10 12:03:00 +0000728 if (popts->rcw_override) {
729 ddr->ddr_sdram_rcw_1 = popts->rcw_1;
730 ddr->ddr_sdram_rcw_2 = popts->rcw_2;
731 } else {
732 ddr->ddr_sdram_rcw_1 =
733 common_dimm->rcw[0] << 28 | \
734 common_dimm->rcw[1] << 24 | \
735 common_dimm->rcw[2] << 20 | \
736 common_dimm->rcw[3] << 16 | \
737 common_dimm->rcw[4] << 12 | \
738 common_dimm->rcw[5] << 8 | \
739 common_dimm->rcw[6] << 4 | \
740 common_dimm->rcw[7];
741 ddr->ddr_sdram_rcw_2 =
742 common_dimm->rcw[8] << 28 | \
743 common_dimm->rcw[9] << 24 | \
744 common_dimm->rcw[10] << 20 | \
745 common_dimm->rcw[11] << 16 | \
746 common_dimm->rcw[12] << 12 | \
747 common_dimm->rcw[13] << 8 | \
748 common_dimm->rcw[14] << 4 | \
749 common_dimm->rcw[15];
750 }
york9490ff42010-07-02 22:25:55 +0000751 debug("FSLDDR: ddr_sdram_rcw_1 = 0x%08x\n", ddr->ddr_sdram_rcw_1);
752 debug("FSLDDR: ddr_sdram_rcw_2 = 0x%08x\n", ddr->ddr_sdram_rcw_2);
753 }
754}
755
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500756/* DDR SDRAM control configuration (DDR_SDRAM_CFG) */
757static void set_ddr_sdram_cfg(fsl_ddr_cfg_regs_t *ddr,
758 const memctl_options_t *popts,
759 const common_timing_params_t *common_dimm)
760{
761 unsigned int mem_en; /* DDR SDRAM interface logic enable */
762 unsigned int sren; /* Self refresh enable (during sleep) */
763 unsigned int ecc_en; /* ECC enable. */
764 unsigned int rd_en; /* Registered DIMM enable */
765 unsigned int sdram_type; /* Type of SDRAM */
766 unsigned int dyn_pwr; /* Dynamic power management mode */
767 unsigned int dbw; /* DRAM dta bus width */
Dave Liu22ff3d02008-11-21 16:31:29 +0800768 unsigned int eight_be = 0; /* 8-beat burst enable, DDR2 is zero */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500769 unsigned int ncap = 0; /* Non-concurrent auto-precharge */
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530770 unsigned int threet_en; /* Enable 3T timing */
771 unsigned int twot_en; /* Enable 2T timing */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500772 unsigned int ba_intlv_ctl; /* Bank (CS) interleaving control */
773 unsigned int x32_en = 0; /* x32 enable */
774 unsigned int pchb8 = 0; /* precharge bit 8 enable */
775 unsigned int hse; /* Global half strength override */
York Sund28cb672014-09-05 13:52:41 +0800776 unsigned int acc_ecc_en = 0; /* Accumulated ECC enable */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500777 unsigned int mem_halt = 0; /* memory controller halt */
778 unsigned int bi = 0; /* Bypass initialization */
779
780 mem_en = 1;
781 sren = popts->self_refresh_in_sleep;
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530782 if (common_dimm->all_dimms_ecc_capable) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500783 /* Allow setting of ECC only if all DIMMs are ECC. */
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530784 ecc_en = popts->ecc_mode;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500785 } else {
786 ecc_en = 0;
787 }
788
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530789 if (common_dimm->all_dimms_registered &&
790 !common_dimm->all_dimms_unbuffered) {
York Sune1fd16b2011-01-10 12:03:00 +0000791 rd_en = 1;
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530792 twot_en = 0;
York Sune1fd16b2011-01-10 12:03:00 +0000793 } else {
794 rd_en = 0;
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530795 twot_en = popts->twot_en;
York Sune1fd16b2011-01-10 12:03:00 +0000796 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500797
798 sdram_type = CONFIG_FSL_SDRAM_TYPE;
799
800 dyn_pwr = popts->dynamic_power;
801 dbw = popts->data_bus_width;
Dave Liuc360cea2009-03-14 12:48:30 +0800802 /* 8-beat burst enable DDR-III case
803 * we must clear it when use the on-the-fly mode,
804 * must set it when use the 32-bits bus mode.
805 */
York Sun34e026f2014-03-27 17:54:47 -0700806 if ((sdram_type == SDRAM_TYPE_DDR3) ||
807 (sdram_type == SDRAM_TYPE_DDR4)) {
Dave Liuc360cea2009-03-14 12:48:30 +0800808 if (popts->burst_length == DDR_BL8)
809 eight_be = 1;
810 if (popts->burst_length == DDR_OTF)
811 eight_be = 0;
812 if (dbw == 0x1)
813 eight_be = 1;
814 }
815
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530816 threet_en = popts->threet_en;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500817 ba_intlv_ctl = popts->ba_intlv_ctl;
818 hse = popts->half_strength_driver_enable;
819
York Sund28cb672014-09-05 13:52:41 +0800820 /* set when ddr bus width < 64 */
821 acc_ecc_en = (dbw != 0 && ecc_en == 1) ? 1 : 0;
822
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500823 ddr->ddr_sdram_cfg = (0
824 | ((mem_en & 0x1) << 31)
825 | ((sren & 0x1) << 30)
826 | ((ecc_en & 0x1) << 29)
827 | ((rd_en & 0x1) << 28)
828 | ((sdram_type & 0x7) << 24)
829 | ((dyn_pwr & 0x1) << 21)
830 | ((dbw & 0x3) << 19)
831 | ((eight_be & 0x1) << 18)
832 | ((ncap & 0x1) << 17)
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530833 | ((threet_en & 0x1) << 16)
834 | ((twot_en & 0x1) << 15)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500835 | ((ba_intlv_ctl & 0x7F) << 8)
836 | ((x32_en & 0x1) << 5)
837 | ((pchb8 & 0x1) << 4)
838 | ((hse & 0x1) << 3)
York Sund28cb672014-09-05 13:52:41 +0800839 | ((acc_ecc_en & 0x1) << 2)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500840 | ((mem_halt & 0x1) << 1)
841 | ((bi & 0x1) << 0)
842 );
Haiying Wang1f293b42008-10-03 12:37:26 -0400843 debug("FSLDDR: ddr_sdram_cfg = 0x%08x\n", ddr->ddr_sdram_cfg);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500844}
845
846/* DDR SDRAM control configuration 2 (DDR_SDRAM_CFG_2) */
York Sun03e664d2015-01-06 13:18:50 -0800847static void set_ddr_sdram_cfg_2(const unsigned int ctrl_num,
848 fsl_ddr_cfg_regs_t *ddr,
York Sune1fd16b2011-01-10 12:03:00 +0000849 const memctl_options_t *popts,
850 const unsigned int unq_mrs_en)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500851{
852 unsigned int frc_sr = 0; /* Force self refresh */
853 unsigned int sr_ie = 0; /* Self-refresh interrupt enable */
York Suncae7c1b2011-08-26 11:32:40 -0700854 unsigned int odt_cfg = 0; /* ODT configuration */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500855 unsigned int num_pr; /* Number of posted refreshes */
York Sun57495e42012-10-08 07:44:22 +0000856 unsigned int slow = 0; /* DDR will be run less than 1250 */
York Sunb61e0612013-06-25 11:37:47 -0700857 unsigned int x4_en = 0; /* x4 DRAM enable */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500858 unsigned int obc_cfg; /* On-The-Fly Burst Chop Cfg */
859 unsigned int ap_en; /* Address Parity Enable */
860 unsigned int d_init; /* DRAM data initialization */
861 unsigned int rcw_en = 0; /* Register Control Word Enable */
862 unsigned int md_en = 0; /* Mirrored DIMM Enable */
york5800e7a2010-07-02 22:25:53 +0000863 unsigned int qd_en = 0; /* quad-rank DIMM Enable */
York Suncae7c1b2011-08-26 11:32:40 -0700864 int i;
York Sun34e026f2014-03-27 17:54:47 -0700865#ifndef CONFIG_SYS_FSL_DDR4
866 unsigned int dll_rst_dis = 1; /* DLL reset disable */
867 unsigned int dqs_cfg; /* DQS configuration */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500868
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530869 dqs_cfg = popts->dqs_config;
York Sun34e026f2014-03-27 17:54:47 -0700870#endif
York Suncae7c1b2011-08-26 11:32:40 -0700871 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
872 if (popts->cs_local_opts[i].odt_rd_cfg
873 || popts->cs_local_opts[i].odt_wr_cfg) {
874 odt_cfg = SDRAM_CFG2_ODT_ONLY_READ;
875 break;
876 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500877 }
Joakim Tjernlunde368c202015-10-14 16:32:00 +0200878 sr_ie = popts->self_refresh_interrupt_en;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500879 num_pr = 1; /* Make this configurable */
880
881 /*
882 * 8572 manual says
883 * {TIMING_CFG_1[PRETOACT]
884 * + [DDR_SDRAM_CFG_2[NUM_PR]
885 * * ({EXT_REFREC || REFREC} + 8 + 2)]}
886 * << DDR_SDRAM_INTERVAL[REFINT]
887 */
York Sun34e026f2014-03-27 17:54:47 -0700888#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530889 obc_cfg = popts->otf_burst_chop_en;
Dave Liuc360cea2009-03-14 12:48:30 +0800890#else
891 obc_cfg = 0;
892#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500893
York Sun57495e42012-10-08 07:44:22 +0000894#if (CONFIG_SYS_FSL_DDR_VER >= FSL_DDR_VER_4_7)
York Sun03e664d2015-01-06 13:18:50 -0800895 slow = get_ddr_freq(ctrl_num) < 1249000000;
York Sun57495e42012-10-08 07:44:22 +0000896#endif
897
Shengzhou Liueb118802016-03-10 17:36:56 +0800898 if (popts->registered_dimm_en)
York Sune1fd16b2011-01-10 12:03:00 +0000899 rcw_en = 1;
Shengzhou Liueb118802016-03-10 17:36:56 +0800900
901 /* DDR4 can have address parity for UDIMM and discrete */
902 if ((CONFIG_FSL_SDRAM_TYPE != SDRAM_TYPE_DDR4) &&
903 (!popts->registered_dimm_en)) {
York Sune1fd16b2011-01-10 12:03:00 +0000904 ap_en = 0;
Shengzhou Liueb118802016-03-10 17:36:56 +0800905 } else {
906 ap_en = popts->ap_en;
York Sune1fd16b2011-01-10 12:03:00 +0000907 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500908
York Sunb61e0612013-06-25 11:37:47 -0700909 x4_en = popts->x4_en ? 1 : 0;
910
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500911#if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
912 /* Use the DDR controller to auto initialize memory. */
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530913 d_init = popts->ecc_init_using_memctl;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500914 ddr->ddr_data_init = CONFIG_MEM_INIT_VALUE;
915 debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init);
916#else
917 /* Memory will be initialized via DMA, or not at all. */
918 d_init = 0;
919#endif
920
York Sun34e026f2014-03-27 17:54:47 -0700921#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
Dave Liuc360cea2009-03-14 12:48:30 +0800922 md_en = popts->mirrored_dimm;
923#endif
york5800e7a2010-07-02 22:25:53 +0000924 qd_en = popts->quad_rank_present ? 1 : 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500925 ddr->ddr_sdram_cfg_2 = (0
926 | ((frc_sr & 0x1) << 31)
927 | ((sr_ie & 0x1) << 30)
York Sun34e026f2014-03-27 17:54:47 -0700928#ifndef CONFIG_SYS_FSL_DDR4
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500929 | ((dll_rst_dis & 0x1) << 29)
930 | ((dqs_cfg & 0x3) << 26)
York Sun34e026f2014-03-27 17:54:47 -0700931#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500932 | ((odt_cfg & 0x3) << 21)
933 | ((num_pr & 0xf) << 12)
York Sun57495e42012-10-08 07:44:22 +0000934 | ((slow & 1) << 11)
York Sunb61e0612013-06-25 11:37:47 -0700935 | (x4_en << 10)
york5800e7a2010-07-02 22:25:53 +0000936 | (qd_en << 9)
York Sune1fd16b2011-01-10 12:03:00 +0000937 | (unq_mrs_en << 8)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500938 | ((obc_cfg & 0x1) << 6)
939 | ((ap_en & 0x1) << 5)
940 | ((d_init & 0x1) << 4)
941 | ((rcw_en & 0x1) << 2)
942 | ((md_en & 0x1) << 0)
943 );
Haiying Wang1f293b42008-10-03 12:37:26 -0400944 debug("FSLDDR: ddr_sdram_cfg_2 = 0x%08x\n", ddr->ddr_sdram_cfg_2);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500945}
946
York Sun34e026f2014-03-27 17:54:47 -0700947#ifdef CONFIG_SYS_FSL_DDR4
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500948/* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
York Sun03e664d2015-01-06 13:18:50 -0800949static void set_ddr_sdram_mode_2(const unsigned int ctrl_num,
950 fsl_ddr_cfg_regs_t *ddr,
York Sune1fd16b2011-01-10 12:03:00 +0000951 const memctl_options_t *popts,
Valentin Longchamp7e157b02013-10-18 11:47:20 +0200952 const common_timing_params_t *common_dimm,
York Sune1fd16b2011-01-10 12:03:00 +0000953 const unsigned int unq_mrs_en)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500954{
955 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
956 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
York Sun34e026f2014-03-27 17:54:47 -0700957 int i;
958 unsigned int wr_crc = 0; /* Disable */
959 unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */
960 unsigned int srt = 0; /* self-refresh temerature, normal range */
York Sun03e664d2015-01-06 13:18:50 -0800961 unsigned int cwl = compute_cas_write_latency(ctrl_num) - 9;
York Sun34e026f2014-03-27 17:54:47 -0700962 unsigned int mpr = 0; /* serial */
963 unsigned int wc_lat;
York Sun03e664d2015-01-06 13:18:50 -0800964 const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500965
York Sun34e026f2014-03-27 17:54:47 -0700966 if (popts->rtt_override)
967 rtt_wr = popts->rtt_wr_override_value;
968 else
969 rtt_wr = popts->cs_local_opts[0].odt_rtt_wr;
970
971 if (common_dimm->extended_op_srt)
972 srt = common_dimm->extended_op_srt;
973
974 esdmode2 = (0
975 | ((wr_crc & 0x1) << 12)
976 | ((rtt_wr & 0x3) << 9)
977 | ((srt & 0x3) << 6)
978 | ((cwl & 0x7) << 3));
979
980 if (mclk_ps >= 1250)
981 wc_lat = 0;
982 else if (mclk_ps >= 833)
983 wc_lat = 1;
984 else
985 wc_lat = 2;
986
987 esdmode3 = (0
988 | ((mpr & 0x3) << 11)
989 | ((wc_lat & 0x3) << 9));
990
991 ddr->ddr_sdram_mode_2 = (0
992 | ((esdmode2 & 0xFFFF) << 16)
993 | ((esdmode3 & 0xFFFF) << 0)
994 );
995 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
996
997 if (unq_mrs_en) { /* unique mode registers are supported */
998 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
999 if (popts->rtt_override)
1000 rtt_wr = popts->rtt_wr_override_value;
1001 else
1002 rtt_wr = popts->cs_local_opts[i].odt_rtt_wr;
1003
1004 esdmode2 &= 0xF9FF; /* clear bit 10, 9 */
1005 esdmode2 |= (rtt_wr & 0x3) << 9;
1006 switch (i) {
1007 case 1:
1008 ddr->ddr_sdram_mode_4 = (0
1009 | ((esdmode2 & 0xFFFF) << 16)
1010 | ((esdmode3 & 0xFFFF) << 0)
1011 );
1012 break;
1013 case 2:
1014 ddr->ddr_sdram_mode_6 = (0
1015 | ((esdmode2 & 0xFFFF) << 16)
1016 | ((esdmode3 & 0xFFFF) << 0)
1017 );
1018 break;
1019 case 3:
1020 ddr->ddr_sdram_mode_8 = (0
1021 | ((esdmode2 & 0xFFFF) << 16)
1022 | ((esdmode3 & 0xFFFF) << 0)
1023 );
1024 break;
1025 }
1026 }
1027 debug("FSLDDR: ddr_sdram_mode_4 = 0x%08x\n",
1028 ddr->ddr_sdram_mode_4);
1029 debug("FSLDDR: ddr_sdram_mode_6 = 0x%08x\n",
1030 ddr->ddr_sdram_mode_6);
1031 debug("FSLDDR: ddr_sdram_mode_8 = 0x%08x\n",
1032 ddr->ddr_sdram_mode_8);
1033 }
1034}
1035#elif defined(CONFIG_SYS_FSL_DDR3)
1036/* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
York Sun03e664d2015-01-06 13:18:50 -08001037static void set_ddr_sdram_mode_2(const unsigned int ctrl_num,
1038 fsl_ddr_cfg_regs_t *ddr,
York Sun34e026f2014-03-27 17:54:47 -07001039 const memctl_options_t *popts,
1040 const common_timing_params_t *common_dimm,
1041 const unsigned int unq_mrs_en)
1042{
1043 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
1044 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
Kumar Gala92966832011-01-20 01:53:15 -06001045 int i;
Dave Liu1aa3d082009-12-16 10:24:38 -06001046 unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */
Dave Liuc360cea2009-03-14 12:48:30 +08001047 unsigned int srt = 0; /* self-refresh temerature, normal range */
1048 unsigned int asr = 0; /* auto self-refresh disable */
York Sun03e664d2015-01-06 13:18:50 -08001049 unsigned int cwl = compute_cas_write_latency(ctrl_num) - 5;
Dave Liuc360cea2009-03-14 12:48:30 +08001050 unsigned int pasr = 0; /* partial array self refresh disable */
1051
Dave Liu1aa3d082009-12-16 10:24:38 -06001052 if (popts->rtt_override)
1053 rtt_wr = popts->rtt_wr_override_value;
York Sune1fd16b2011-01-10 12:03:00 +00001054 else
1055 rtt_wr = popts->cs_local_opts[0].odt_rtt_wr;
Valentin Longchamp7e157b02013-10-18 11:47:20 +02001056
1057 if (common_dimm->extended_op_srt)
1058 srt = common_dimm->extended_op_srt;
1059
Dave Liuc360cea2009-03-14 12:48:30 +08001060 esdmode2 = (0
1061 | ((rtt_wr & 0x3) << 9)
1062 | ((srt & 0x1) << 7)
1063 | ((asr & 0x1) << 6)
1064 | ((cwl & 0x7) << 3)
1065 | ((pasr & 0x7) << 0));
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001066 ddr->ddr_sdram_mode_2 = (0
1067 | ((esdmode2 & 0xFFFF) << 16)
1068 | ((esdmode3 & 0xFFFF) << 0)
1069 );
Haiying Wang1f293b42008-10-03 12:37:26 -04001070 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
York Sune1fd16b2011-01-10 12:03:00 +00001071
York Sune1fd16b2011-01-10 12:03:00 +00001072 if (unq_mrs_en) { /* unique mode registers are supported */
Kumar Galadea7f882011-11-09 10:05:10 -06001073 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Sune1fd16b2011-01-10 12:03:00 +00001074 if (popts->rtt_override)
1075 rtt_wr = popts->rtt_wr_override_value;
1076 else
1077 rtt_wr = popts->cs_local_opts[i].odt_rtt_wr;
1078
1079 esdmode2 &= 0xF9FF; /* clear bit 10, 9 */
1080 esdmode2 |= (rtt_wr & 0x3) << 9;
1081 switch (i) {
1082 case 1:
1083 ddr->ddr_sdram_mode_4 = (0
1084 | ((esdmode2 & 0xFFFF) << 16)
1085 | ((esdmode3 & 0xFFFF) << 0)
1086 );
1087 break;
1088 case 2:
1089 ddr->ddr_sdram_mode_6 = (0
1090 | ((esdmode2 & 0xFFFF) << 16)
1091 | ((esdmode3 & 0xFFFF) << 0)
1092 );
1093 break;
1094 case 3:
1095 ddr->ddr_sdram_mode_8 = (0
1096 | ((esdmode2 & 0xFFFF) << 16)
1097 | ((esdmode3 & 0xFFFF) << 0)
1098 );
1099 break;
1100 }
1101 }
1102 debug("FSLDDR: ddr_sdram_mode_4 = 0x%08x\n",
1103 ddr->ddr_sdram_mode_4);
1104 debug("FSLDDR: ddr_sdram_mode_6 = 0x%08x\n",
1105 ddr->ddr_sdram_mode_6);
1106 debug("FSLDDR: ddr_sdram_mode_8 = 0x%08x\n",
1107 ddr->ddr_sdram_mode_8);
1108 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001109}
1110
York Sun34e026f2014-03-27 17:54:47 -07001111#else /* for DDR2 and DDR1 */
1112/* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
York Sun03e664d2015-01-06 13:18:50 -08001113static void set_ddr_sdram_mode_2(const unsigned int ctrl_num,
1114 fsl_ddr_cfg_regs_t *ddr,
York Sun34e026f2014-03-27 17:54:47 -07001115 const memctl_options_t *popts,
1116 const common_timing_params_t *common_dimm,
1117 const unsigned int unq_mrs_en)
1118{
1119 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
1120 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
1121
1122 ddr->ddr_sdram_mode_2 = (0
1123 | ((esdmode2 & 0xFFFF) << 16)
1124 | ((esdmode3 & 0xFFFF) << 0)
1125 );
1126 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
1127}
1128#endif
1129
1130#ifdef CONFIG_SYS_FSL_DDR4
1131/* DDR SDRAM Mode configuration 9 (DDR_SDRAM_MODE_9) */
1132static void set_ddr_sdram_mode_9(fsl_ddr_cfg_regs_t *ddr,
1133 const memctl_options_t *popts,
1134 const common_timing_params_t *common_dimm,
1135 const unsigned int unq_mrs_en)
1136{
1137 int i;
1138 unsigned short esdmode4 = 0; /* Extended SDRAM mode 4 */
1139 unsigned short esdmode5; /* Extended SDRAM mode 5 */
York Sun6b95be22015-03-19 09:30:27 -07001140 int rtt_park = 0;
York Sun8a514292015-11-04 10:03:19 -08001141 bool four_cs = false;
Shengzhou Liueb118802016-03-10 17:36:56 +08001142 const unsigned int mclk_ps = get_memory_clk_period_ps(0);
York Sun34e026f2014-03-27 17:54:47 -07001143
York Sun8a514292015-11-04 10:03:19 -08001144#if CONFIG_CHIP_SELECTS_PER_CTRL == 4
1145 if ((ddr->cs[0].config & SDRAM_CS_CONFIG_EN) &&
1146 (ddr->cs[1].config & SDRAM_CS_CONFIG_EN) &&
1147 (ddr->cs[2].config & SDRAM_CS_CONFIG_EN) &&
1148 (ddr->cs[3].config & SDRAM_CS_CONFIG_EN))
1149 four_cs = true;
1150#endif
York Sun6b95be22015-03-19 09:30:27 -07001151 if (ddr->cs[0].config & SDRAM_CS_CONFIG_EN) {
1152 esdmode5 = 0x00000500; /* Data mask enable, RTT_PARK CS0 */
York Sun8a514292015-11-04 10:03:19 -08001153 rtt_park = four_cs ? 0 : 1;
York Sun6b95be22015-03-19 09:30:27 -07001154 } else {
1155 esdmode5 = 0x00000400; /* Data mask enabled */
1156 }
York Sun34e026f2014-03-27 17:54:47 -07001157
Shengzhou Liueb118802016-03-10 17:36:56 +08001158 /* set command/address parity latency */
1159 if (ddr->ddr_sdram_cfg_2 & SDRAM_CFG2_AP_EN) {
1160 if (mclk_ps >= 935) {
1161 /* for DDR4-1600/1866/2133 */
1162 esdmode5 |= DDR_MR5_CA_PARITY_LAT_4_CLK;
1163 } else if (mclk_ps >= 833) {
1164 /* for DDR4-2400 */
1165 esdmode5 |= DDR_MR5_CA_PARITY_LAT_5_CLK;
1166 } else {
1167 printf("parity: mclk_ps = %d not supported\n", mclk_ps);
1168 }
1169 }
1170
York Sun34e026f2014-03-27 17:54:47 -07001171 ddr->ddr_sdram_mode_9 = (0
1172 | ((esdmode4 & 0xffff) << 16)
1173 | ((esdmode5 & 0xffff) << 0)
1174 );
York Sun66869f92015-03-19 09:30:26 -07001175
York Sun8a514292015-11-04 10:03:19 -08001176 /* Normally only the first enabled CS use 0x500, others use 0x400
1177 * But when four chip-selects are all enabled, all mode registers
1178 * need 0x500 to park.
1179 */
York Sun66869f92015-03-19 09:30:26 -07001180
York Sun34e026f2014-03-27 17:54:47 -07001181 debug("FSLDDR: ddr_sdram_mode_9) = 0x%08x\n", ddr->ddr_sdram_mode_9);
1182 if (unq_mrs_en) { /* unique mode registers are supported */
1183 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Sun6b95be22015-03-19 09:30:27 -07001184 if (!rtt_park &&
1185 (ddr->cs[i].config & SDRAM_CS_CONFIG_EN)) {
1186 esdmode5 |= 0x00000500; /* RTT_PARK */
York Sun8a514292015-11-04 10:03:19 -08001187 rtt_park = four_cs ? 0 : 1;
York Sun6b95be22015-03-19 09:30:27 -07001188 } else {
1189 esdmode5 = 0x00000400;
1190 }
Shengzhou Liueb118802016-03-10 17:36:56 +08001191
1192 if (ddr->ddr_sdram_cfg_2 & SDRAM_CFG2_AP_EN) {
1193 if (mclk_ps >= 935) {
1194 /* for DDR4-1600/1866/2133 */
1195 esdmode5 |= DDR_MR5_CA_PARITY_LAT_4_CLK;
1196 } else if (mclk_ps >= 833) {
1197 /* for DDR4-2400 */
1198 esdmode5 |= DDR_MR5_CA_PARITY_LAT_5_CLK;
1199 } else {
1200 printf("parity: mclk_ps = %d not supported\n",
1201 mclk_ps);
1202 }
1203 }
1204
York Sun34e026f2014-03-27 17:54:47 -07001205 switch (i) {
1206 case 1:
1207 ddr->ddr_sdram_mode_11 = (0
1208 | ((esdmode4 & 0xFFFF) << 16)
1209 | ((esdmode5 & 0xFFFF) << 0)
1210 );
1211 break;
1212 case 2:
1213 ddr->ddr_sdram_mode_13 = (0
1214 | ((esdmode4 & 0xFFFF) << 16)
1215 | ((esdmode5 & 0xFFFF) << 0)
1216 );
1217 break;
1218 case 3:
1219 ddr->ddr_sdram_mode_15 = (0
1220 | ((esdmode4 & 0xFFFF) << 16)
1221 | ((esdmode5 & 0xFFFF) << 0)
1222 );
1223 break;
1224 }
1225 }
1226 debug("FSLDDR: ddr_sdram_mode_11 = 0x%08x\n",
1227 ddr->ddr_sdram_mode_11);
1228 debug("FSLDDR: ddr_sdram_mode_13 = 0x%08x\n",
1229 ddr->ddr_sdram_mode_13);
1230 debug("FSLDDR: ddr_sdram_mode_15 = 0x%08x\n",
1231 ddr->ddr_sdram_mode_15);
1232 }
1233}
1234
1235/* DDR SDRAM Mode configuration 10 (DDR_SDRAM_MODE_10) */
York Sun03e664d2015-01-06 13:18:50 -08001236static void set_ddr_sdram_mode_10(const unsigned int ctrl_num,
1237 fsl_ddr_cfg_regs_t *ddr,
York Sun34e026f2014-03-27 17:54:47 -07001238 const memctl_options_t *popts,
1239 const common_timing_params_t *common_dimm,
1240 const unsigned int unq_mrs_en)
1241{
1242 int i;
1243 unsigned short esdmode6 = 0; /* Extended SDRAM mode 6 */
1244 unsigned short esdmode7 = 0; /* Extended SDRAM mode 7 */
York Sun03e664d2015-01-06 13:18:50 -08001245 unsigned int tccdl_min = picos_to_mclk(ctrl_num, common_dimm->tccdl_ps);
York Sun34e026f2014-03-27 17:54:47 -07001246
1247 esdmode6 = ((tccdl_min - 4) & 0x7) << 10;
1248
York Sun0fb71972015-11-04 10:03:18 -08001249 if (popts->ddr_cdr2 & DDR_CDR2_VREF_RANGE_2)
1250 esdmode6 |= 1 << 6; /* Range 2 */
1251
York Sun34e026f2014-03-27 17:54:47 -07001252 ddr->ddr_sdram_mode_10 = (0
1253 | ((esdmode6 & 0xffff) << 16)
1254 | ((esdmode7 & 0xffff) << 0)
1255 );
1256 debug("FSLDDR: ddr_sdram_mode_10) = 0x%08x\n", ddr->ddr_sdram_mode_10);
1257 if (unq_mrs_en) { /* unique mode registers are supported */
1258 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1259 switch (i) {
1260 case 1:
1261 ddr->ddr_sdram_mode_12 = (0
1262 | ((esdmode6 & 0xFFFF) << 16)
1263 | ((esdmode7 & 0xFFFF) << 0)
1264 );
1265 break;
1266 case 2:
1267 ddr->ddr_sdram_mode_14 = (0
1268 | ((esdmode6 & 0xFFFF) << 16)
1269 | ((esdmode7 & 0xFFFF) << 0)
1270 );
1271 break;
1272 case 3:
1273 ddr->ddr_sdram_mode_16 = (0
1274 | ((esdmode6 & 0xFFFF) << 16)
1275 | ((esdmode7 & 0xFFFF) << 0)
1276 );
1277 break;
1278 }
1279 }
1280 debug("FSLDDR: ddr_sdram_mode_12 = 0x%08x\n",
1281 ddr->ddr_sdram_mode_12);
1282 debug("FSLDDR: ddr_sdram_mode_14 = 0x%08x\n",
1283 ddr->ddr_sdram_mode_14);
1284 debug("FSLDDR: ddr_sdram_mode_16 = 0x%08x\n",
1285 ddr->ddr_sdram_mode_16);
1286 }
1287}
1288
1289#endif
1290
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001291/* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */
York Sun03e664d2015-01-06 13:18:50 -08001292static void set_ddr_sdram_interval(const unsigned int ctrl_num,
1293 fsl_ddr_cfg_regs_t *ddr,
1294 const memctl_options_t *popts,
1295 const common_timing_params_t *common_dimm)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001296{
1297 unsigned int refint; /* Refresh interval */
1298 unsigned int bstopre; /* Precharge interval */
1299
York Sun03e664d2015-01-06 13:18:50 -08001300 refint = picos_to_mclk(ctrl_num, common_dimm->refresh_rate_ps);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001301
1302 bstopre = popts->bstopre;
1303
1304 /* refint field used 0x3FFF in earlier controllers */
1305 ddr->ddr_sdram_interval = (0
1306 | ((refint & 0xFFFF) << 16)
1307 | ((bstopre & 0x3FFF) << 0)
1308 );
Haiying Wang1f293b42008-10-03 12:37:26 -04001309 debug("FSLDDR: ddr_sdram_interval = 0x%08x\n", ddr->ddr_sdram_interval);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001310}
1311
York Sun34e026f2014-03-27 17:54:47 -07001312#ifdef CONFIG_SYS_FSL_DDR4
Dave Liuc360cea2009-03-14 12:48:30 +08001313/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
York Sun03e664d2015-01-06 13:18:50 -08001314static void set_ddr_sdram_mode(const unsigned int ctrl_num,
1315 fsl_ddr_cfg_regs_t *ddr,
Dave Liuc360cea2009-03-14 12:48:30 +08001316 const memctl_options_t *popts,
1317 const common_timing_params_t *common_dimm,
1318 unsigned int cas_latency,
York Sune1fd16b2011-01-10 12:03:00 +00001319 unsigned int additive_latency,
1320 const unsigned int unq_mrs_en)
Dave Liuc360cea2009-03-14 12:48:30 +08001321{
York Sun34e026f2014-03-27 17:54:47 -07001322 int i;
1323 unsigned short esdmode; /* Extended SDRAM mode */
1324 unsigned short sdmode; /* SDRAM mode */
1325
1326 /* Mode Register - MR1 */
1327 unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */
1328 unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */
1329 unsigned int rtt;
1330 unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */
1331 unsigned int al = 0; /* Posted CAS# additive latency (AL) */
1332 unsigned int dic = 0; /* Output driver impedance, 40ohm */
1333 unsigned int dll_en = 1; /* DLL Enable 1=Enable (Normal),
1334 0=Disable (Test/Debug) */
1335
1336 /* Mode Register - MR0 */
1337 unsigned int wr = 0; /* Write Recovery */
1338 unsigned int dll_rst; /* DLL Reset */
1339 unsigned int mode; /* Normal=0 or Test=1 */
1340 unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
1341 /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
1342 unsigned int bt;
1343 unsigned int bl; /* BL: Burst Length */
1344
1345 unsigned int wr_mclk;
1346 /* DDR4 support WR 10, 12, 14, 16, 18, 20, 24 */
1347 static const u8 wr_table[] = {
1348 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6};
1349 /* DDR4 support CAS 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 22, 24 */
1350 static const u8 cas_latency_table[] = {
1351 0, 1, 2, 3, 4, 5, 6, 7, 8, 8,
1352 9, 9, 10, 10, 11, 11};
1353
1354 if (popts->rtt_override)
1355 rtt = popts->rtt_override_value;
1356 else
1357 rtt = popts->cs_local_opts[0].odt_rtt_norm;
1358
1359 if (additive_latency == (cas_latency - 1))
1360 al = 1;
1361 if (additive_latency == (cas_latency - 2))
1362 al = 2;
1363
1364 if (popts->quad_rank_present)
1365 dic = 1; /* output driver impedance 240/7 ohm */
1366
1367 /*
1368 * The esdmode value will also be used for writing
1369 * MR1 during write leveling for DDR3, although the
1370 * bits specifically related to the write leveling
1371 * scheme will be handled automatically by the DDR
1372 * controller. so we set the wrlvl_en = 0 here.
1373 */
1374 esdmode = (0
1375 | ((qoff & 0x1) << 12)
1376 | ((tdqs_en & 0x1) << 11)
1377 | ((rtt & 0x7) << 8)
1378 | ((wrlvl_en & 0x1) << 7)
1379 | ((al & 0x3) << 3)
1380 | ((dic & 0x3) << 1) /* DIC field is split */
1381 | ((dll_en & 0x1) << 0)
1382 );
1383
1384 /*
1385 * DLL control for precharge PD
1386 * 0=slow exit DLL off (tXPDLL)
1387 * 1=fast exit DLL on (tXP)
1388 */
1389
York Sun03e664d2015-01-06 13:18:50 -08001390 wr_mclk = picos_to_mclk(ctrl_num, common_dimm->twr_ps);
York Sun34e026f2014-03-27 17:54:47 -07001391 if (wr_mclk <= 24) {
1392 wr = wr_table[wr_mclk - 10];
1393 } else {
1394 printf("Error: unsupported write recovery for mode register wr_mclk = %d\n",
1395 wr_mclk);
1396 }
1397
1398 dll_rst = 0; /* dll no reset */
1399 mode = 0; /* normal mode */
1400
1401 /* look up table to get the cas latency bits */
1402 if (cas_latency >= 9 && cas_latency <= 24)
1403 caslat = cas_latency_table[cas_latency - 9];
1404 else
1405 printf("Error: unsupported cas latency for mode register\n");
1406
1407 bt = 0; /* Nibble sequential */
1408
1409 switch (popts->burst_length) {
1410 case DDR_BL8:
1411 bl = 0;
1412 break;
1413 case DDR_OTF:
1414 bl = 1;
1415 break;
1416 case DDR_BC4:
1417 bl = 2;
1418 break;
1419 default:
1420 printf("Error: invalid burst length of %u specified. ",
1421 popts->burst_length);
1422 puts("Defaulting to on-the-fly BC4 or BL8 beats.\n");
1423 bl = 1;
1424 break;
1425 }
1426
1427 sdmode = (0
1428 | ((wr & 0x7) << 9)
1429 | ((dll_rst & 0x1) << 8)
1430 | ((mode & 0x1) << 7)
1431 | (((caslat >> 1) & 0x7) << 4)
1432 | ((bt & 0x1) << 3)
1433 | ((caslat & 1) << 2)
1434 | ((bl & 0x3) << 0)
1435 );
1436
1437 ddr->ddr_sdram_mode = (0
1438 | ((esdmode & 0xFFFF) << 16)
1439 | ((sdmode & 0xFFFF) << 0)
1440 );
1441
1442 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
1443
1444 if (unq_mrs_en) { /* unique mode registers are supported */
1445 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1446 if (popts->rtt_override)
1447 rtt = popts->rtt_override_value;
1448 else
1449 rtt = popts->cs_local_opts[i].odt_rtt_norm;
1450
1451 esdmode &= 0xF8FF; /* clear bit 10,9,8 for rtt */
1452 esdmode |= (rtt & 0x7) << 8;
1453 switch (i) {
1454 case 1:
1455 ddr->ddr_sdram_mode_3 = (0
1456 | ((esdmode & 0xFFFF) << 16)
1457 | ((sdmode & 0xFFFF) << 0)
1458 );
1459 break;
1460 case 2:
1461 ddr->ddr_sdram_mode_5 = (0
1462 | ((esdmode & 0xFFFF) << 16)
1463 | ((sdmode & 0xFFFF) << 0)
1464 );
1465 break;
1466 case 3:
1467 ddr->ddr_sdram_mode_7 = (0
1468 | ((esdmode & 0xFFFF) << 16)
1469 | ((sdmode & 0xFFFF) << 0)
1470 );
1471 break;
1472 }
1473 }
1474 debug("FSLDDR: ddr_sdram_mode_3 = 0x%08x\n",
1475 ddr->ddr_sdram_mode_3);
1476 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1477 ddr->ddr_sdram_mode_5);
1478 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1479 ddr->ddr_sdram_mode_5);
1480 }
1481}
1482
1483#elif defined(CONFIG_SYS_FSL_DDR3)
1484/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
York Sun03e664d2015-01-06 13:18:50 -08001485static void set_ddr_sdram_mode(const unsigned int ctrl_num,
1486 fsl_ddr_cfg_regs_t *ddr,
York Sun34e026f2014-03-27 17:54:47 -07001487 const memctl_options_t *popts,
1488 const common_timing_params_t *common_dimm,
1489 unsigned int cas_latency,
1490 unsigned int additive_latency,
1491 const unsigned int unq_mrs_en)
1492{
1493 int i;
Dave Liuc360cea2009-03-14 12:48:30 +08001494 unsigned short esdmode; /* Extended SDRAM mode */
1495 unsigned short sdmode; /* SDRAM mode */
1496
1497 /* Mode Register - MR1 */
1498 unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */
1499 unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */
1500 unsigned int rtt;
1501 unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */
1502 unsigned int al = 0; /* Posted CAS# additive latency (AL) */
York Sune1fd16b2011-01-10 12:03:00 +00001503 unsigned int dic = 0; /* Output driver impedance, 40ohm */
Dave Liuc360cea2009-03-14 12:48:30 +08001504 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
1505 1=Disable (Test/Debug) */
1506
1507 /* Mode Register - MR0 */
1508 unsigned int dll_on; /* DLL control for precharge PD, 0=off, 1=on */
York Sunfcea3062012-08-17 08:22:38 +00001509 unsigned int wr = 0; /* Write Recovery */
Dave Liuc360cea2009-03-14 12:48:30 +08001510 unsigned int dll_rst; /* DLL Reset */
1511 unsigned int mode; /* Normal=0 or Test=1 */
1512 unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
1513 /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
1514 unsigned int bt;
1515 unsigned int bl; /* BL: Burst Length */
1516
1517 unsigned int wr_mclk;
York Sunf5b6fb72011-03-02 14:24:11 -08001518 /*
1519 * DDR_SDRAM_MODE doesn't support 9,11,13,15
1520 * Please refer JEDEC Standard No. 79-3E for Mode Register MR0
1521 * for this table
1522 */
1523 static const u8 wr_table[] = {1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 0, 0};
Dave Liuc360cea2009-03-14 12:48:30 +08001524
Dave Liuc360cea2009-03-14 12:48:30 +08001525 if (popts->rtt_override)
1526 rtt = popts->rtt_override_value;
York Sune1fd16b2011-01-10 12:03:00 +00001527 else
1528 rtt = popts->cs_local_opts[0].odt_rtt_norm;
Dave Liuc360cea2009-03-14 12:48:30 +08001529
1530 if (additive_latency == (cas_latency - 1))
1531 al = 1;
1532 if (additive_latency == (cas_latency - 2))
1533 al = 2;
1534
York Sune1fd16b2011-01-10 12:03:00 +00001535 if (popts->quad_rank_present)
1536 dic = 1; /* output driver impedance 240/7 ohm */
1537
Dave Liuc360cea2009-03-14 12:48:30 +08001538 /*
1539 * The esdmode value will also be used for writing
1540 * MR1 during write leveling for DDR3, although the
1541 * bits specifically related to the write leveling
1542 * scheme will be handled automatically by the DDR
1543 * controller. so we set the wrlvl_en = 0 here.
1544 */
1545 esdmode = (0
1546 | ((qoff & 0x1) << 12)
1547 | ((tdqs_en & 0x1) << 11)
Kumar Gala6d8565a2009-09-10 14:54:55 -05001548 | ((rtt & 0x4) << 7) /* rtt field is split */
Dave Liuc360cea2009-03-14 12:48:30 +08001549 | ((wrlvl_en & 0x1) << 7)
Kumar Gala6d8565a2009-09-10 14:54:55 -05001550 | ((rtt & 0x2) << 5) /* rtt field is split */
1551 | ((dic & 0x2) << 4) /* DIC field is split */
Dave Liuc360cea2009-03-14 12:48:30 +08001552 | ((al & 0x3) << 3)
Kumar Gala6d8565a2009-09-10 14:54:55 -05001553 | ((rtt & 0x1) << 2) /* rtt field is split */
Dave Liuc360cea2009-03-14 12:48:30 +08001554 | ((dic & 0x1) << 1) /* DIC field is split */
1555 | ((dll_en & 0x1) << 0)
1556 );
1557
1558 /*
1559 * DLL control for precharge PD
1560 * 0=slow exit DLL off (tXPDLL)
1561 * 1=fast exit DLL on (tXP)
1562 */
1563 dll_on = 1;
York Sunf5b6fb72011-03-02 14:24:11 -08001564
York Sun03e664d2015-01-06 13:18:50 -08001565 wr_mclk = picos_to_mclk(ctrl_num, common_dimm->twr_ps);
York Sunfcea3062012-08-17 08:22:38 +00001566 if (wr_mclk <= 16) {
1567 wr = wr_table[wr_mclk - 5];
1568 } else {
1569 printf("Error: unsupported write recovery for mode register "
1570 "wr_mclk = %d\n", wr_mclk);
1571 }
York Sunf5b6fb72011-03-02 14:24:11 -08001572
Dave Liuc360cea2009-03-14 12:48:30 +08001573 dll_rst = 0; /* dll no reset */
1574 mode = 0; /* normal mode */
1575
1576 /* look up table to get the cas latency bits */
York Sunfcea3062012-08-17 08:22:38 +00001577 if (cas_latency >= 5 && cas_latency <= 16) {
1578 unsigned char cas_latency_table[] = {
Dave Liuc360cea2009-03-14 12:48:30 +08001579 0x2, /* 5 clocks */
1580 0x4, /* 6 clocks */
1581 0x6, /* 7 clocks */
1582 0x8, /* 8 clocks */
1583 0xa, /* 9 clocks */
1584 0xc, /* 10 clocks */
York Sunfcea3062012-08-17 08:22:38 +00001585 0xe, /* 11 clocks */
1586 0x1, /* 12 clocks */
1587 0x3, /* 13 clocks */
1588 0x5, /* 14 clocks */
1589 0x7, /* 15 clocks */
1590 0x9, /* 16 clocks */
Dave Liuc360cea2009-03-14 12:48:30 +08001591 };
1592 caslat = cas_latency_table[cas_latency - 5];
York Sunfcea3062012-08-17 08:22:38 +00001593 } else {
1594 printf("Error: unsupported cas latency for mode register\n");
Dave Liuc360cea2009-03-14 12:48:30 +08001595 }
York Sunfcea3062012-08-17 08:22:38 +00001596
Dave Liuc360cea2009-03-14 12:48:30 +08001597 bt = 0; /* Nibble sequential */
1598
1599 switch (popts->burst_length) {
1600 case DDR_BL8:
1601 bl = 0;
1602 break;
1603 case DDR_OTF:
1604 bl = 1;
1605 break;
1606 case DDR_BC4:
1607 bl = 2;
1608 break;
1609 default:
1610 printf("Error: invalid burst length of %u specified. "
1611 " Defaulting to on-the-fly BC4 or BL8 beats.\n",
1612 popts->burst_length);
1613 bl = 1;
1614 break;
1615 }
1616
1617 sdmode = (0
1618 | ((dll_on & 0x1) << 12)
1619 | ((wr & 0x7) << 9)
1620 | ((dll_rst & 0x1) << 8)
1621 | ((mode & 0x1) << 7)
1622 | (((caslat >> 1) & 0x7) << 4)
1623 | ((bt & 0x1) << 3)
York Sunfcea3062012-08-17 08:22:38 +00001624 | ((caslat & 1) << 2)
Dave Liuc360cea2009-03-14 12:48:30 +08001625 | ((bl & 0x3) << 0)
1626 );
1627
1628 ddr->ddr_sdram_mode = (0
1629 | ((esdmode & 0xFFFF) << 16)
1630 | ((sdmode & 0xFFFF) << 0)
1631 );
1632
1633 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
York Sune1fd16b2011-01-10 12:03:00 +00001634
1635 if (unq_mrs_en) { /* unique mode registers are supported */
Kumar Galadea7f882011-11-09 10:05:10 -06001636 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Sune1fd16b2011-01-10 12:03:00 +00001637 if (popts->rtt_override)
1638 rtt = popts->rtt_override_value;
1639 else
1640 rtt = popts->cs_local_opts[i].odt_rtt_norm;
1641
1642 esdmode &= 0xFDBB; /* clear bit 9,6,2 */
1643 esdmode |= (0
1644 | ((rtt & 0x4) << 7) /* rtt field is split */
1645 | ((rtt & 0x2) << 5) /* rtt field is split */
1646 | ((rtt & 0x1) << 2) /* rtt field is split */
1647 );
1648 switch (i) {
1649 case 1:
1650 ddr->ddr_sdram_mode_3 = (0
1651 | ((esdmode & 0xFFFF) << 16)
1652 | ((sdmode & 0xFFFF) << 0)
1653 );
1654 break;
1655 case 2:
1656 ddr->ddr_sdram_mode_5 = (0
1657 | ((esdmode & 0xFFFF) << 16)
1658 | ((sdmode & 0xFFFF) << 0)
1659 );
1660 break;
1661 case 3:
1662 ddr->ddr_sdram_mode_7 = (0
1663 | ((esdmode & 0xFFFF) << 16)
1664 | ((sdmode & 0xFFFF) << 0)
1665 );
1666 break;
1667 }
1668 }
1669 debug("FSLDDR: ddr_sdram_mode_3 = 0x%08x\n",
1670 ddr->ddr_sdram_mode_3);
1671 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1672 ddr->ddr_sdram_mode_5);
1673 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1674 ddr->ddr_sdram_mode_5);
1675 }
Dave Liuc360cea2009-03-14 12:48:30 +08001676}
1677
York Sun5614e712013-09-30 09:22:09 -07001678#else /* !CONFIG_SYS_FSL_DDR3 */
Dave Liuc360cea2009-03-14 12:48:30 +08001679
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001680/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
York Sun03e664d2015-01-06 13:18:50 -08001681static void set_ddr_sdram_mode(const unsigned int ctrl_num,
1682 fsl_ddr_cfg_regs_t *ddr,
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001683 const memctl_options_t *popts,
1684 const common_timing_params_t *common_dimm,
1685 unsigned int cas_latency,
York Sune1fd16b2011-01-10 12:03:00 +00001686 unsigned int additive_latency,
1687 const unsigned int unq_mrs_en)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001688{
1689 unsigned short esdmode; /* Extended SDRAM mode */
1690 unsigned short sdmode; /* SDRAM mode */
1691
1692 /*
1693 * FIXME: This ought to be pre-calculated in a
1694 * technology-specific routine,
1695 * e.g. compute_DDR2_mode_register(), and then the
1696 * sdmode and esdmode passed in as part of common_dimm.
1697 */
1698
1699 /* Extended Mode Register */
1700 unsigned int mrs = 0; /* Mode Register Set */
1701 unsigned int outputs = 0; /* 0=Enabled, 1=Disabled */
1702 unsigned int rdqs_en = 0; /* RDQS Enable: 0=no, 1=yes */
1703 unsigned int dqs_en = 0; /* DQS# Enable: 0=enable, 1=disable */
1704 unsigned int ocd = 0; /* 0x0=OCD not supported,
1705 0x7=OCD default state */
1706 unsigned int rtt;
1707 unsigned int al; /* Posted CAS# additive latency (AL) */
1708 unsigned int ods = 0; /* Output Drive Strength:
1709 0 = Full strength (18ohm)
1710 1 = Reduced strength (4ohm) */
1711 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
1712 1=Disable (Test/Debug) */
1713
1714 /* Mode Register (MR) */
1715 unsigned int mr; /* Mode Register Definition */
1716 unsigned int pd; /* Power-Down Mode */
1717 unsigned int wr; /* Write Recovery */
1718 unsigned int dll_res; /* DLL Reset */
1719 unsigned int mode; /* Normal=0 or Test=1 */
Kumar Gala302e52e2008-09-05 14:40:29 -05001720 unsigned int caslat = 0;/* CAS# latency */
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001721 /* BT: Burst Type (0=Sequential, 1=Interleaved) */
1722 unsigned int bt;
1723 unsigned int bl; /* BL: Burst Length */
1724
Priyanka Jain0dd38a32013-09-25 10:41:19 +05301725 dqs_en = !popts->dqs_config;
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001726 rtt = fsl_ddr_get_rtt();
1727
1728 al = additive_latency;
1729
1730 esdmode = (0
1731 | ((mrs & 0x3) << 14)
1732 | ((outputs & 0x1) << 12)
1733 | ((rdqs_en & 0x1) << 11)
1734 | ((dqs_en & 0x1) << 10)
1735 | ((ocd & 0x7) << 7)
1736 | ((rtt & 0x2) << 5) /* rtt field is split */
1737 | ((al & 0x7) << 3)
1738 | ((rtt & 0x1) << 2) /* rtt field is split */
1739 | ((ods & 0x1) << 1)
1740 | ((dll_en & 0x1) << 0)
1741 );
1742
1743 mr = 0; /* FIXME: CHECKME */
1744
1745 /*
1746 * 0 = Fast Exit (Normal)
1747 * 1 = Slow Exit (Low Power)
1748 */
1749 pd = 0;
1750
York Sun5614e712013-09-30 09:22:09 -07001751#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001752 wr = 0; /* Historical */
York Sun5614e712013-09-30 09:22:09 -07001753#elif defined(CONFIG_SYS_FSL_DDR2)
York Sun03e664d2015-01-06 13:18:50 -08001754 wr = picos_to_mclk(ctrl_num, common_dimm->twr_ps);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001755#endif
1756 dll_res = 0;
1757 mode = 0;
1758
York Sun5614e712013-09-30 09:22:09 -07001759#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001760 if (1 <= cas_latency && cas_latency <= 4) {
1761 unsigned char mode_caslat_table[4] = {
1762 0x5, /* 1.5 clocks */
1763 0x2, /* 2.0 clocks */
1764 0x6, /* 2.5 clocks */
1765 0x3 /* 3.0 clocks */
1766 };
Kumar Gala302e52e2008-09-05 14:40:29 -05001767 caslat = mode_caslat_table[cas_latency - 1];
1768 } else {
1769 printf("Warning: unknown cas_latency %d\n", cas_latency);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001770 }
York Sun5614e712013-09-30 09:22:09 -07001771#elif defined(CONFIG_SYS_FSL_DDR2)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001772 caslat = cas_latency;
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001773#endif
1774 bt = 0;
1775
1776 switch (popts->burst_length) {
Dave Liuc360cea2009-03-14 12:48:30 +08001777 case DDR_BL4:
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001778 bl = 2;
1779 break;
Dave Liuc360cea2009-03-14 12:48:30 +08001780 case DDR_BL8:
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001781 bl = 3;
1782 break;
1783 default:
1784 printf("Error: invalid burst length of %u specified. "
1785 " Defaulting to 4 beats.\n",
1786 popts->burst_length);
1787 bl = 2;
1788 break;
1789 }
1790
1791 sdmode = (0
1792 | ((mr & 0x3) << 14)
1793 | ((pd & 0x1) << 12)
1794 | ((wr & 0x7) << 9)
1795 | ((dll_res & 0x1) << 8)
1796 | ((mode & 0x1) << 7)
1797 | ((caslat & 0x7) << 4)
1798 | ((bt & 0x1) << 3)
1799 | ((bl & 0x7) << 0)
1800 );
1801
1802 ddr->ddr_sdram_mode = (0
1803 | ((esdmode & 0xFFFF) << 16)
1804 | ((sdmode & 0xFFFF) << 0)
1805 );
Haiying Wang1f293b42008-10-03 12:37:26 -04001806 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001807}
Dave Liuc360cea2009-03-14 12:48:30 +08001808#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001809
1810/* DDR SDRAM Data Initialization (DDR_DATA_INIT) */
1811static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr)
1812{
1813 unsigned int init_value; /* Initialization value */
1814
Anatolij Gustschin5b933942013-01-21 23:50:27 +00001815#ifdef CONFIG_MEM_INIT_VALUE
1816 init_value = CONFIG_MEM_INIT_VALUE;
1817#else
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001818 init_value = 0xDEADBEEF;
Anatolij Gustschin5b933942013-01-21 23:50:27 +00001819#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001820 ddr->ddr_data_init = init_value;
1821}
1822
1823/*
1824 * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL)
1825 * The old controller on the 8540/60 doesn't have this register.
1826 * Hope it's OK to set it (to 0) anyway.
1827 */
1828static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
1829 const memctl_options_t *popts)
1830{
1831 unsigned int clk_adjust; /* Clock adjust */
Curt Bruned7c865b2015-02-13 10:57:11 -08001832 unsigned int ss_en = 0; /* Source synchronous enable */
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001833
Curt Bruned7c865b2015-02-13 10:57:11 -08001834#if defined(CONFIG_MPC8541) || defined(CONFIG_MPC8555)
1835 /* Per FSL Application Note: AN2805 */
1836 ss_en = 1;
1837#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001838 clk_adjust = popts->clk_adjust;
Curt Bruned7c865b2015-02-13 10:57:11 -08001839 ddr->ddr_sdram_clk_cntl = (0
1840 | ((ss_en & 0x1) << 31)
1841 | ((clk_adjust & 0xF) << 23)
1842 );
york9490ff42010-07-02 22:25:55 +00001843 debug("FSLDDR: clk_cntl = 0x%08x\n", ddr->ddr_sdram_clk_cntl);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001844}
1845
1846/* DDR Initialization Address (DDR_INIT_ADDR) */
1847static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
1848{
1849 unsigned int init_addr = 0; /* Initialization address */
1850
1851 ddr->ddr_init_addr = init_addr;
1852}
1853
1854/* DDR Initialization Address (DDR_INIT_EXT_ADDR) */
1855static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
1856{
1857 unsigned int uia = 0; /* Use initialization address */
1858 unsigned int init_ext_addr = 0; /* Initialization address */
1859
1860 ddr->ddr_init_ext_addr = (0
1861 | ((uia & 0x1) << 31)
1862 | (init_ext_addr & 0xF)
1863 );
1864}
1865
1866/* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */
Dave Liuec145e82010-03-05 12:22:00 +08001867static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr,
1868 const memctl_options_t *popts)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001869{
1870 unsigned int rwt = 0; /* Read-to-write turnaround for same CS */
1871 unsigned int wrt = 0; /* Write-to-read turnaround for same CS */
1872 unsigned int rrt = 0; /* Read-to-read turnaround for same CS */
1873 unsigned int wwt = 0; /* Write-to-write turnaround for same CS */
York Sun6c6e0062015-11-04 10:03:21 -08001874 unsigned int trwt_mclk = 0; /* ext_rwt */
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001875 unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */
1876
York Sun34e026f2014-03-27 17:54:47 -07001877#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
Dave Liuec145e82010-03-05 12:22:00 +08001878 if (popts->burst_length == DDR_BL8) {
1879 /* We set BL/2 for fixed BL8 */
1880 rrt = 0; /* BL/2 clocks */
1881 wwt = 0; /* BL/2 clocks */
1882 } else {
1883 /* We need to set BL/2 + 2 to BC4 and OTF */
1884 rrt = 2; /* BL/2 + 2 clocks */
1885 wwt = 2; /* BL/2 + 2 clocks */
1886 }
York Sun34e026f2014-03-27 17:54:47 -07001887#endif
York Sun34e026f2014-03-27 17:54:47 -07001888#ifdef CONFIG_SYS_FSL_DDR4
1889 dll_lock = 2; /* tDLLK = 1024 clocks */
1890#elif defined(CONFIG_SYS_FSL_DDR3)
Dave Liuc360cea2009-03-14 12:48:30 +08001891 dll_lock = 1; /* tDLLK = 512 clocks from spec */
1892#endif
York Sun6c6e0062015-11-04 10:03:21 -08001893
1894 if (popts->trwt_override)
1895 trwt_mclk = popts->trwt;
1896
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001897 ddr->timing_cfg_4 = (0
1898 | ((rwt & 0xf) << 28)
1899 | ((wrt & 0xf) << 24)
1900 | ((rrt & 0xf) << 20)
1901 | ((wwt & 0xf) << 16)
York Sun6c6e0062015-11-04 10:03:21 -08001902 | ((trwt_mclk & 0xc) << 12)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001903 | (dll_lock & 0x3)
1904 );
Haiying Wang1f293b42008-10-03 12:37:26 -04001905 debug("FSLDDR: timing_cfg_4 = 0x%08x\n", ddr->timing_cfg_4);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001906}
1907
1908/* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */
York Sune1fd16b2011-01-10 12:03:00 +00001909static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr, unsigned int cas_latency)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001910{
1911 unsigned int rodt_on = 0; /* Read to ODT on */
1912 unsigned int rodt_off = 0; /* Read to ODT off */
1913 unsigned int wodt_on = 0; /* Write to ODT on */
1914 unsigned int wodt_off = 0; /* Write to ODT off */
1915
York Sun34e026f2014-03-27 17:54:47 -07001916#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
1917 unsigned int wr_lat = ((ddr->timing_cfg_2 & 0x00780000) >> 19) +
1918 ((ddr->timing_cfg_2 & 0x00040000) >> 14);
York Sune1fd16b2011-01-10 12:03:00 +00001919 /* rodt_on = timing_cfg_1[caslat] - timing_cfg_2[wrlat] + 1 */
York Sun34e026f2014-03-27 17:54:47 -07001920 if (cas_latency >= wr_lat)
1921 rodt_on = cas_latency - wr_lat + 1;
Dave Liuc360cea2009-03-14 12:48:30 +08001922 rodt_off = 4; /* 4 clocks */
york5fb8a8a2010-07-02 22:25:56 +00001923 wodt_on = 1; /* 1 clocks */
Dave Liuc360cea2009-03-14 12:48:30 +08001924 wodt_off = 4; /* 4 clocks */
1925#endif
1926
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001927 ddr->timing_cfg_5 = (0
Dave Liu22ff3d02008-11-21 16:31:29 +08001928 | ((rodt_on & 0x1f) << 24)
1929 | ((rodt_off & 0x7) << 20)
1930 | ((wodt_on & 0x1f) << 12)
1931 | ((wodt_off & 0x7) << 8)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001932 );
Haiying Wang1f293b42008-10-03 12:37:26 -04001933 debug("FSLDDR: timing_cfg_5 = 0x%08x\n", ddr->timing_cfg_5);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001934}
1935
York Sun34e026f2014-03-27 17:54:47 -07001936#ifdef CONFIG_SYS_FSL_DDR4
1937static void set_timing_cfg_6(fsl_ddr_cfg_regs_t *ddr)
1938{
1939 unsigned int hs_caslat = 0;
1940 unsigned int hs_wrlat = 0;
1941 unsigned int hs_wrrec = 0;
1942 unsigned int hs_clkadj = 0;
1943 unsigned int hs_wrlvl_start = 0;
1944
1945 ddr->timing_cfg_6 = (0
1946 | ((hs_caslat & 0x1f) << 24)
1947 | ((hs_wrlat & 0x1f) << 19)
1948 | ((hs_wrrec & 0x1f) << 12)
1949 | ((hs_clkadj & 0x1f) << 6)
1950 | ((hs_wrlvl_start & 0x1f) << 0)
1951 );
1952 debug("FSLDDR: timing_cfg_6 = 0x%08x\n", ddr->timing_cfg_6);
1953}
1954
York Sun03e664d2015-01-06 13:18:50 -08001955static void set_timing_cfg_7(const unsigned int ctrl_num,
1956 fsl_ddr_cfg_regs_t *ddr,
1957 const common_timing_params_t *common_dimm)
York Sun34e026f2014-03-27 17:54:47 -07001958{
1959 unsigned int txpr, tcksre, tcksrx;
Shengzhou Liueb118802016-03-10 17:36:56 +08001960 unsigned int cke_rst, cksre, cksrx, par_lat = 0, cs_to_cmd;
1961 const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
York Sun34e026f2014-03-27 17:54:47 -07001962
York Sun03e664d2015-01-06 13:18:50 -08001963 txpr = max(5U, picos_to_mclk(ctrl_num, common_dimm->trfc1_ps + 10000));
1964 tcksre = max(5U, picos_to_mclk(ctrl_num, 10000));
1965 tcksrx = max(5U, picos_to_mclk(ctrl_num, 10000));
Shengzhou Liueb118802016-03-10 17:36:56 +08001966
1967 if (ddr->ddr_sdram_cfg_2 & SDRAM_CFG2_AP_EN) {
1968 if (mclk_ps >= 935) {
1969 /* parity latency 4 clocks in case of 1600/1866/2133 */
1970 par_lat = 4;
1971 } else if (mclk_ps >= 833) {
1972 /* parity latency 5 clocks for DDR4-2400 */
1973 par_lat = 5;
1974 } else {
1975 printf("parity: mclk_ps = %d not supported\n", mclk_ps);
1976 }
1977 }
1978
York Sun34e026f2014-03-27 17:54:47 -07001979 cs_to_cmd = 0;
1980
1981 if (txpr <= 200)
1982 cke_rst = 0;
1983 else if (txpr <= 256)
1984 cke_rst = 1;
1985 else if (txpr <= 512)
1986 cke_rst = 2;
1987 else
1988 cke_rst = 3;
1989
1990 if (tcksre <= 19)
1991 cksre = tcksre - 5;
1992 else
1993 cksre = 15;
1994
1995 if (tcksrx <= 19)
1996 cksrx = tcksrx - 5;
1997 else
1998 cksrx = 15;
1999
2000 ddr->timing_cfg_7 = (0
2001 | ((cke_rst & 0x3) << 28)
2002 | ((cksre & 0xf) << 24)
2003 | ((cksrx & 0xf) << 20)
2004 | ((par_lat & 0xf) << 16)
2005 | ((cs_to_cmd & 0xf) << 4)
2006 );
2007 debug("FSLDDR: timing_cfg_7 = 0x%08x\n", ddr->timing_cfg_7);
2008}
2009
York Sun03e664d2015-01-06 13:18:50 -08002010static void set_timing_cfg_8(const unsigned int ctrl_num,
2011 fsl_ddr_cfg_regs_t *ddr,
York Sun34e026f2014-03-27 17:54:47 -07002012 const memctl_options_t *popts,
2013 const common_timing_params_t *common_dimm,
2014 unsigned int cas_latency)
2015{
2016 unsigned int rwt_bg, wrt_bg, rrt_bg, wwt_bg;
2017 unsigned int acttoact_bg, wrtord_bg, pre_all_rec;
York Sun03e664d2015-01-06 13:18:50 -08002018 unsigned int tccdl = picos_to_mclk(ctrl_num, common_dimm->tccdl_ps);
York Sun34e026f2014-03-27 17:54:47 -07002019 unsigned int wr_lat = ((ddr->timing_cfg_2 & 0x00780000) >> 19) +
2020 ((ddr->timing_cfg_2 & 0x00040000) >> 14);
2021
2022 rwt_bg = cas_latency + 2 + 4 - wr_lat;
2023 if (rwt_bg < tccdl)
2024 rwt_bg = tccdl - rwt_bg;
2025 else
2026 rwt_bg = 0;
2027
2028 wrt_bg = wr_lat + 4 + 1 - cas_latency;
2029 if (wrt_bg < tccdl)
2030 wrt_bg = tccdl - wrt_bg;
2031 else
2032 wrt_bg = 0;
2033
2034 if (popts->burst_length == DDR_BL8) {
2035 rrt_bg = tccdl - 4;
2036 wwt_bg = tccdl - 4;
2037 } else {
2038 rrt_bg = tccdl - 2;
York Sundc1437a2015-01-06 13:18:52 -08002039 wwt_bg = tccdl - 2;
York Sun34e026f2014-03-27 17:54:47 -07002040 }
2041
York Sun03e664d2015-01-06 13:18:50 -08002042 acttoact_bg = picos_to_mclk(ctrl_num, common_dimm->trrdl_ps);
2043 wrtord_bg = max(4U, picos_to_mclk(ctrl_num, 7500));
York Sun3d75ec92014-06-26 11:14:44 -07002044 if (popts->otf_burst_chop_en)
2045 wrtord_bg += 2;
2046
York Sun34e026f2014-03-27 17:54:47 -07002047 pre_all_rec = 0;
2048
2049 ddr->timing_cfg_8 = (0
2050 | ((rwt_bg & 0xf) << 28)
2051 | ((wrt_bg & 0xf) << 24)
2052 | ((rrt_bg & 0xf) << 20)
2053 | ((wwt_bg & 0xf) << 16)
2054 | ((acttoact_bg & 0xf) << 12)
2055 | ((wrtord_bg & 0xf) << 8)
2056 | ((pre_all_rec & 0x1f) << 0)
2057 );
2058
2059 debug("FSLDDR: timing_cfg_8 = 0x%08x\n", ddr->timing_cfg_8);
2060}
2061
2062static void set_timing_cfg_9(fsl_ddr_cfg_regs_t *ddr)
2063{
2064 ddr->timing_cfg_9 = 0;
2065 debug("FSLDDR: timing_cfg_9 = 0x%08x\n", ddr->timing_cfg_9);
2066}
2067
York Sunf80d6472014-09-11 13:32:06 -07002068/* This function needs to be called after set_ddr_sdram_cfg() is called */
York Sun34e026f2014-03-27 17:54:47 -07002069static void set_ddr_dq_mapping(fsl_ddr_cfg_regs_t *ddr,
2070 const dimm_params_t *dimm_params)
2071{
York Sunf80d6472014-09-11 13:32:06 -07002072 unsigned int acc_ecc_en = (ddr->ddr_sdram_cfg >> 2) & 0x1;
York Sun6b95be22015-03-19 09:30:27 -07002073 int i;
York Sunf80d6472014-09-11 13:32:06 -07002074
York Sun6b95be22015-03-19 09:30:27 -07002075 for (i = 0; i < CONFIG_DIMM_SLOTS_PER_CTLR; i++) {
2076 if (dimm_params[i].n_ranks)
2077 break;
2078 }
2079 if (i >= CONFIG_DIMM_SLOTS_PER_CTLR) {
2080 puts("DDR error: no DIMM found!\n");
2081 return;
2082 }
York Sun34e026f2014-03-27 17:54:47 -07002083
York Sun6b95be22015-03-19 09:30:27 -07002084 ddr->dq_map_0 = ((dimm_params[i].dq_mapping[0] & 0x3F) << 26) |
2085 ((dimm_params[i].dq_mapping[1] & 0x3F) << 20) |
2086 ((dimm_params[i].dq_mapping[2] & 0x3F) << 14) |
2087 ((dimm_params[i].dq_mapping[3] & 0x3F) << 8) |
2088 ((dimm_params[i].dq_mapping[4] & 0x3F) << 2);
York Sun34e026f2014-03-27 17:54:47 -07002089
York Sun6b95be22015-03-19 09:30:27 -07002090 ddr->dq_map_1 = ((dimm_params[i].dq_mapping[5] & 0x3F) << 26) |
2091 ((dimm_params[i].dq_mapping[6] & 0x3F) << 20) |
2092 ((dimm_params[i].dq_mapping[7] & 0x3F) << 14) |
2093 ((dimm_params[i].dq_mapping[10] & 0x3F) << 8) |
2094 ((dimm_params[i].dq_mapping[11] & 0x3F) << 2);
2095
2096 ddr->dq_map_2 = ((dimm_params[i].dq_mapping[12] & 0x3F) << 26) |
2097 ((dimm_params[i].dq_mapping[13] & 0x3F) << 20) |
2098 ((dimm_params[i].dq_mapping[14] & 0x3F) << 14) |
2099 ((dimm_params[i].dq_mapping[15] & 0x3F) << 8) |
2100 ((dimm_params[i].dq_mapping[16] & 0x3F) << 2);
York Sun34e026f2014-03-27 17:54:47 -07002101
York Sunf80d6472014-09-11 13:32:06 -07002102 /* dq_map for ECC[4:7] is set to 0 if accumulated ECC is enabled */
York Sun6b95be22015-03-19 09:30:27 -07002103 ddr->dq_map_3 = ((dimm_params[i].dq_mapping[17] & 0x3F) << 26) |
2104 ((dimm_params[i].dq_mapping[8] & 0x3F) << 20) |
York Sunf80d6472014-09-11 13:32:06 -07002105 (acc_ecc_en ? 0 :
York Sun6b95be22015-03-19 09:30:27 -07002106 (dimm_params[i].dq_mapping[9] & 0x3F) << 14) |
2107 dimm_params[i].dq_mapping_ors;
York Sun34e026f2014-03-27 17:54:47 -07002108
2109 debug("FSLDDR: dq_map_0 = 0x%08x\n", ddr->dq_map_0);
2110 debug("FSLDDR: dq_map_1 = 0x%08x\n", ddr->dq_map_1);
2111 debug("FSLDDR: dq_map_2 = 0x%08x\n", ddr->dq_map_2);
2112 debug("FSLDDR: dq_map_3 = 0x%08x\n", ddr->dq_map_3);
2113}
2114static void set_ddr_sdram_cfg_3(fsl_ddr_cfg_regs_t *ddr,
2115 const memctl_options_t *popts)
2116{
2117 int rd_pre;
2118
2119 rd_pre = popts->quad_rank_present ? 1 : 0;
2120
2121 ddr->ddr_sdram_cfg_3 = (rd_pre & 0x1) << 16;
2122
2123 debug("FSLDDR: ddr_sdram_cfg_3 = 0x%08x\n", ddr->ddr_sdram_cfg_3);
2124}
2125#endif /* CONFIG_SYS_FSL_DDR4 */
2126
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002127/* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */
Dave Liuc360cea2009-03-14 12:48:30 +08002128static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int zq_en)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002129{
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002130 unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */
2131 /* Normal Operation Full Calibration Time (tZQoper) */
2132 unsigned int zqoper = 0;
2133 /* Normal Operation Short Calibration Time (tZQCS) */
2134 unsigned int zqcs = 0;
York Sun34e026f2014-03-27 17:54:47 -07002135#ifdef CONFIG_SYS_FSL_DDR4
2136 unsigned int zqcs_init;
2137#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002138
Dave Liuc360cea2009-03-14 12:48:30 +08002139 if (zq_en) {
York Sun34e026f2014-03-27 17:54:47 -07002140#ifdef CONFIG_SYS_FSL_DDR4
2141 zqinit = 10; /* 1024 clocks */
2142 zqoper = 9; /* 512 clocks */
2143 zqcs = 7; /* 128 clocks */
2144 zqcs_init = 5; /* 1024 refresh sequences */
2145#else
Dave Liuc360cea2009-03-14 12:48:30 +08002146 zqinit = 9; /* 512 clocks */
2147 zqoper = 8; /* 256 clocks */
2148 zqcs = 6; /* 64 clocks */
York Sun34e026f2014-03-27 17:54:47 -07002149#endif
Dave Liuc360cea2009-03-14 12:48:30 +08002150 }
2151
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002152 ddr->ddr_zq_cntl = (0
2153 | ((zq_en & 0x1) << 31)
2154 | ((zqinit & 0xF) << 24)
2155 | ((zqoper & 0xF) << 16)
2156 | ((zqcs & 0xF) << 8)
York Sun34e026f2014-03-27 17:54:47 -07002157#ifdef CONFIG_SYS_FSL_DDR4
2158 | ((zqcs_init & 0xF) << 0)
2159#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002160 );
York Sune1fd16b2011-01-10 12:03:00 +00002161 debug("FSLDDR: zq_cntl = 0x%08x\n", ddr->ddr_zq_cntl);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002162}
2163
2164/* DDR Write Leveling Control (DDR_WRLVL_CNTL) */
Dave Liubdc9f7b2009-12-16 10:24:37 -06002165static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int wrlvl_en,
2166 const memctl_options_t *popts)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002167{
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002168 /*
2169 * First DQS pulse rising edge after margining mode
2170 * is programmed (tWL_MRD)
2171 */
2172 unsigned int wrlvl_mrd = 0;
2173 /* ODT delay after margining mode is programmed (tWL_ODTEN) */
2174 unsigned int wrlvl_odten = 0;
2175 /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */
2176 unsigned int wrlvl_dqsen = 0;
2177 /* WRLVL_SMPL: Write leveling sample time */
2178 unsigned int wrlvl_smpl = 0;
2179 /* WRLVL_WLR: Write leveling repeition time */
2180 unsigned int wrlvl_wlr = 0;
2181 /* WRLVL_START: Write leveling start time */
2182 unsigned int wrlvl_start = 0;
2183
Dave Liuc360cea2009-03-14 12:48:30 +08002184 /* suggest enable write leveling for DDR3 due to fly-by topology */
2185 if (wrlvl_en) {
2186 /* tWL_MRD min = 40 nCK, we set it 64 */
2187 wrlvl_mrd = 0x6;
2188 /* tWL_ODTEN 128 */
2189 wrlvl_odten = 0x7;
2190 /* tWL_DQSEN min = 25 nCK, we set it 32 */
2191 wrlvl_dqsen = 0x5;
2192 /*
Dave Liubdc9f7b2009-12-16 10:24:37 -06002193 * Write leveling sample time at least need 6 clocks
2194 * higher than tWLO to allow enough time for progagation
2195 * delay and sampling the prime data bits.
Dave Liuc360cea2009-03-14 12:48:30 +08002196 */
2197 wrlvl_smpl = 0xf;
2198 /*
2199 * Write leveling repetition time
2200 * at least tWLO + 6 clocks clocks
york5fb8a8a2010-07-02 22:25:56 +00002201 * we set it 64
Dave Liuc360cea2009-03-14 12:48:30 +08002202 */
york5fb8a8a2010-07-02 22:25:56 +00002203 wrlvl_wlr = 0x6;
Dave Liuc360cea2009-03-14 12:48:30 +08002204 /*
2205 * Write leveling start time
2206 * The value use for the DQS_ADJUST for the first sample
York Sune1fd16b2011-01-10 12:03:00 +00002207 * when write leveling is enabled. It probably needs to be
2208 * overriden per platform.
Dave Liuc360cea2009-03-14 12:48:30 +08002209 */
2210 wrlvl_start = 0x8;
Dave Liubdc9f7b2009-12-16 10:24:37 -06002211 /*
2212 * Override the write leveling sample and start time
2213 * according to specific board
2214 */
2215 if (popts->wrlvl_override) {
2216 wrlvl_smpl = popts->wrlvl_sample;
2217 wrlvl_start = popts->wrlvl_start;
2218 }
Dave Liuc360cea2009-03-14 12:48:30 +08002219 }
2220
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002221 ddr->ddr_wrlvl_cntl = (0
2222 | ((wrlvl_en & 0x1) << 31)
2223 | ((wrlvl_mrd & 0x7) << 24)
2224 | ((wrlvl_odten & 0x7) << 20)
2225 | ((wrlvl_dqsen & 0x7) << 16)
2226 | ((wrlvl_smpl & 0xf) << 12)
2227 | ((wrlvl_wlr & 0x7) << 8)
Dave Liu22ff3d02008-11-21 16:31:29 +08002228 | ((wrlvl_start & 0x1F) << 0)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002229 );
York Sune1fd16b2011-01-10 12:03:00 +00002230 debug("FSLDDR: wrlvl_cntl = 0x%08x\n", ddr->ddr_wrlvl_cntl);
York Sun57495e42012-10-08 07:44:22 +00002231 ddr->ddr_wrlvl_cntl_2 = popts->wrlvl_ctl_2;
2232 debug("FSLDDR: wrlvl_cntl_2 = 0x%08x\n", ddr->ddr_wrlvl_cntl_2);
2233 ddr->ddr_wrlvl_cntl_3 = popts->wrlvl_ctl_3;
2234 debug("FSLDDR: wrlvl_cntl_3 = 0x%08x\n", ddr->ddr_wrlvl_cntl_3);
2235
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002236}
2237
2238/* DDR Self Refresh Counter (DDR_SR_CNTR) */
Dave Liu22cca7e2008-11-21 16:31:35 +08002239static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr, unsigned int sr_it)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002240{
Dave Liu22cca7e2008-11-21 16:31:35 +08002241 /* Self Refresh Idle Threshold */
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002242 ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
2243}
2244
york7fd101c2010-07-02 22:25:54 +00002245static void set_ddr_eor(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
2246{
2247 if (popts->addr_hash) {
2248 ddr->ddr_eor = 0x40000000; /* address hash enable */
Kumar Galac2a63f42011-03-18 11:53:06 -05002249 puts("Address hashing enabled.\n");
york7fd101c2010-07-02 22:25:54 +00002250 }
2251}
2252
York Sune1fd16b2011-01-10 12:03:00 +00002253static void set_ddr_cdr1(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
2254{
2255 ddr->ddr_cdr1 = popts->ddr_cdr1;
2256 debug("FSLDDR: ddr_cdr1 = 0x%08x\n", ddr->ddr_cdr1);
2257}
2258
York Sun57495e42012-10-08 07:44:22 +00002259static void set_ddr_cdr2(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
2260{
2261 ddr->ddr_cdr2 = popts->ddr_cdr2;
2262 debug("FSLDDR: ddr_cdr2 = 0x%08x\n", ddr->ddr_cdr2);
2263}
2264
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002265unsigned int
2266check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
2267{
2268 unsigned int res = 0;
2269
2270 /*
2271 * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are
2272 * not set at the same time.
2273 */
2274 if (ddr->ddr_sdram_cfg & 0x10000000
2275 && ddr->ddr_sdram_cfg & 0x00008000) {
2276 printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
2277 " should not be set at the same time.\n");
2278 res++;
2279 }
2280
2281 return res;
2282}
2283
2284unsigned int
York Sun03e664d2015-01-06 13:18:50 -08002285compute_fsl_memctl_config_regs(const unsigned int ctrl_num,
2286 const memctl_options_t *popts,
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002287 fsl_ddr_cfg_regs_t *ddr,
2288 const common_timing_params_t *common_dimm,
2289 const dimm_params_t *dimm_params,
Haiying Wangfc0c2b62010-12-01 10:35:31 -05002290 unsigned int dbw_cap_adj,
2291 unsigned int size_only)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002292{
2293 unsigned int i;
2294 unsigned int cas_latency;
2295 unsigned int additive_latency;
Dave Liu22cca7e2008-11-21 16:31:35 +08002296 unsigned int sr_it;
Dave Liuc360cea2009-03-14 12:48:30 +08002297 unsigned int zq_en;
2298 unsigned int wrlvl_en;
York Sune1fd16b2011-01-10 12:03:00 +00002299 unsigned int ip_rev = 0;
2300 unsigned int unq_mrs_en = 0;
York Sun58edbc92010-10-18 13:46:50 -07002301 int cs_en = 1;
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002302
2303 memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
2304
2305 if (common_dimm == NULL) {
2306 printf("Error: subset DIMM params struct null pointer\n");
2307 return 1;
2308 }
2309
2310 /*
2311 * Process overrides first.
2312 *
2313 * FIXME: somehow add dereated caslat to this
2314 */
2315 cas_latency = (popts->cas_latency_override)
2316 ? popts->cas_latency_override_value
York Sun34e026f2014-03-27 17:54:47 -07002317 : common_dimm->lowest_common_spd_caslat;
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002318
2319 additive_latency = (popts->additive_latency_override)
2320 ? popts->additive_latency_override_value
2321 : common_dimm->additive_latency;
2322
Dave Liu22cca7e2008-11-21 16:31:35 +08002323 sr_it = (popts->auto_self_refresh_en)
2324 ? popts->sr_it
2325 : 0;
Dave Liuc360cea2009-03-14 12:48:30 +08002326 /* ZQ calibration */
2327 zq_en = (popts->zq_en) ? 1 : 0;
2328 /* write leveling */
2329 wrlvl_en = (popts->wrlvl_en) ? 1 : 0;
Dave Liu22cca7e2008-11-21 16:31:35 +08002330
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002331 /* Chip Select Memory Bounds (CSn_BNDS) */
2332 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Suna4c66502012-08-17 08:22:39 +00002333 unsigned long long ea, sa;
york076bff82010-07-02 22:25:52 +00002334 unsigned int cs_per_dimm
2335 = CONFIG_CHIP_SELECTS_PER_CTRL / CONFIG_DIMM_SLOTS_PER_CTLR;
2336 unsigned int dimm_number
2337 = i / cs_per_dimm;
2338 unsigned long long rank_density
York Suna4c66502012-08-17 08:22:39 +00002339 = dimm_params[dimm_number].rank_density >> dbw_cap_adj;
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002340
york076bff82010-07-02 22:25:52 +00002341 if (dimm_params[dimm_number].n_ranks == 0) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002342 debug("Skipping setup of CS%u "
york5800e7a2010-07-02 22:25:53 +00002343 "because n_ranks on DIMM %u is 0\n", i, dimm_number);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002344 continue;
2345 }
York Suna4c66502012-08-17 08:22:39 +00002346 if (popts->memctl_interleaving) {
york076bff82010-07-02 22:25:52 +00002347 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
York Suna4c66502012-08-17 08:22:39 +00002348 case FSL_DDR_CS0_CS1_CS2_CS3:
2349 break;
york076bff82010-07-02 22:25:52 +00002350 case FSL_DDR_CS0_CS1:
2351 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
York Sun58edbc92010-10-18 13:46:50 -07002352 if (i > 1)
2353 cs_en = 0;
york076bff82010-07-02 22:25:52 +00002354 break;
2355 case FSL_DDR_CS2_CS3:
York Suna4c66502012-08-17 08:22:39 +00002356 default:
York Sun58edbc92010-10-18 13:46:50 -07002357 if (i > 0)
2358 cs_en = 0;
york076bff82010-07-02 22:25:52 +00002359 break;
york076bff82010-07-02 22:25:52 +00002360 }
York Suna4c66502012-08-17 08:22:39 +00002361 sa = common_dimm->base_address;
York Sun123922b2012-10-08 07:44:23 +00002362 ea = sa + common_dimm->total_mem - 1;
York Suna4c66502012-08-17 08:22:39 +00002363 } else if (!popts->memctl_interleaving) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002364 /*
2365 * If memory interleaving between controllers is NOT
2366 * enabled, the starting address for each memory
2367 * controller is distinct. However, because rank
2368 * interleaving is enabled, the starting and ending
2369 * addresses of the total memory on that memory
2370 * controller needs to be programmed into its
2371 * respective CS0_BNDS.
2372 */
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002373 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
2374 case FSL_DDR_CS0_CS1_CS2_CS3:
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002375 sa = common_dimm->base_address;
York Sun123922b2012-10-08 07:44:23 +00002376 ea = sa + common_dimm->total_mem - 1;
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002377 break;
2378 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
York Suna4c66502012-08-17 08:22:39 +00002379 if ((i >= 2) && (dimm_number == 0)) {
york076bff82010-07-02 22:25:52 +00002380 sa = dimm_params[dimm_number].base_address +
York Suna4c66502012-08-17 08:22:39 +00002381 2 * rank_density;
2382 ea = sa + 2 * rank_density - 1;
york076bff82010-07-02 22:25:52 +00002383 } else {
2384 sa = dimm_params[dimm_number].base_address;
York Suna4c66502012-08-17 08:22:39 +00002385 ea = sa + 2 * rank_density - 1;
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002386 }
2387 break;
2388 case FSL_DDR_CS0_CS1:
york076bff82010-07-02 22:25:52 +00002389 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
2390 sa = dimm_params[dimm_number].base_address;
York Suna4c66502012-08-17 08:22:39 +00002391 ea = sa + rank_density - 1;
2392 if (i != 1)
2393 sa += (i % cs_per_dimm) * rank_density;
2394 ea += (i % cs_per_dimm) * rank_density;
york076bff82010-07-02 22:25:52 +00002395 } else {
2396 sa = 0;
2397 ea = 0;
2398 }
2399 if (i == 0)
York Suna4c66502012-08-17 08:22:39 +00002400 ea += rank_density;
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002401 break;
2402 case FSL_DDR_CS2_CS3:
york076bff82010-07-02 22:25:52 +00002403 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
2404 sa = dimm_params[dimm_number].base_address;
York Suna4c66502012-08-17 08:22:39 +00002405 ea = sa + rank_density - 1;
2406 if (i != 3)
2407 sa += (i % cs_per_dimm) * rank_density;
2408 ea += (i % cs_per_dimm) * rank_density;
york076bff82010-07-02 22:25:52 +00002409 } else {
2410 sa = 0;
2411 ea = 0;
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002412 }
york076bff82010-07-02 22:25:52 +00002413 if (i == 2)
2414 ea += (rank_density >> dbw_cap_adj);
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002415 break;
2416 default: /* No bank(chip-select) interleaving */
York Suna4c66502012-08-17 08:22:39 +00002417 sa = dimm_params[dimm_number].base_address;
2418 ea = sa + rank_density - 1;
2419 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
2420 sa += (i % cs_per_dimm) * rank_density;
2421 ea += (i % cs_per_dimm) * rank_density;
2422 } else {
2423 sa = 0;
2424 ea = 0;
2425 }
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002426 break;
2427 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002428 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002429
2430 sa >>= 24;
2431 ea >>= 24;
2432
York Sun123922b2012-10-08 07:44:23 +00002433 if (cs_en) {
2434 ddr->cs[i].bnds = (0
York Sund4263b82013-06-03 12:39:06 -07002435 | ((sa & 0xffff) << 16) /* starting address */
2436 | ((ea & 0xffff) << 0) /* ending address */
York Sun123922b2012-10-08 07:44:23 +00002437 );
2438 } else {
York Sund8556db2013-06-25 11:37:45 -07002439 /* setting bnds to 0xffffffff for inactive CS */
2440 ddr->cs[i].bnds = 0xffffffff;
York Sun123922b2012-10-08 07:44:23 +00002441 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002442
Haiying Wang1f293b42008-10-03 12:37:26 -04002443 debug("FSLDDR: cs[%d]_bnds = 0x%08x\n", i, ddr->cs[i].bnds);
York Sun123922b2012-10-08 07:44:23 +00002444 set_csn_config(dimm_number, i, ddr, popts, dimm_params);
2445 set_csn_config_2(i, ddr);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002446 }
2447
Haiying Wangfc0c2b62010-12-01 10:35:31 -05002448 /*
2449 * In the case we only need to compute the ddr sdram size, we only need
2450 * to set csn registers, so return from here.
2451 */
2452 if (size_only)
2453 return 0;
2454
york7fd101c2010-07-02 22:25:54 +00002455 set_ddr_eor(ddr, popts);
2456
York Sun5614e712013-09-30 09:22:09 -07002457#if !defined(CONFIG_SYS_FSL_DDR1)
York Sun03e664d2015-01-06 13:18:50 -08002458 set_timing_cfg_0(ctrl_num, ddr, popts, dimm_params);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002459#endif
2460
York Sun03e664d2015-01-06 13:18:50 -08002461 set_timing_cfg_3(ctrl_num, ddr, popts, common_dimm, cas_latency,
York Sund4263b82013-06-03 12:39:06 -07002462 additive_latency);
York Sun03e664d2015-01-06 13:18:50 -08002463 set_timing_cfg_1(ctrl_num, ddr, popts, common_dimm, cas_latency);
2464 set_timing_cfg_2(ctrl_num, ddr, popts, common_dimm,
2465 cas_latency, additive_latency);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002466
York Sune1fd16b2011-01-10 12:03:00 +00002467 set_ddr_cdr1(ddr, popts);
York Sun57495e42012-10-08 07:44:22 +00002468 set_ddr_cdr2(ddr, popts);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002469 set_ddr_sdram_cfg(ddr, popts, common_dimm);
York Sun66869f92015-03-19 09:30:26 -07002470 ip_rev = fsl_ddr_get_version(ctrl_num);
York Sune1fd16b2011-01-10 12:03:00 +00002471 if (ip_rev > 0x40400)
2472 unq_mrs_en = 1;
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002473
York Sunf80d6472014-09-11 13:32:06 -07002474 if ((ip_rev > 0x40700) && (popts->cswl_override != 0))
York Sunef87cab2014-09-05 13:52:43 +08002475 ddr->debug[18] = popts->cswl_override;
2476
York Sun03e664d2015-01-06 13:18:50 -08002477 set_ddr_sdram_cfg_2(ctrl_num, ddr, popts, unq_mrs_en);
2478 set_ddr_sdram_mode(ctrl_num, ddr, popts, common_dimm,
2479 cas_latency, additive_latency, unq_mrs_en);
2480 set_ddr_sdram_mode_2(ctrl_num, ddr, popts, common_dimm, unq_mrs_en);
York Sun34e026f2014-03-27 17:54:47 -07002481#ifdef CONFIG_SYS_FSL_DDR4
2482 set_ddr_sdram_mode_9(ddr, popts, common_dimm, unq_mrs_en);
York Sun03e664d2015-01-06 13:18:50 -08002483 set_ddr_sdram_mode_10(ctrl_num, ddr, popts, common_dimm, unq_mrs_en);
York Sun34e026f2014-03-27 17:54:47 -07002484#endif
York Sun03e664d2015-01-06 13:18:50 -08002485 set_ddr_sdram_interval(ctrl_num, ddr, popts, common_dimm);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002486 set_ddr_data_init(ddr);
2487 set_ddr_sdram_clk_cntl(ddr, popts);
2488 set_ddr_init_addr(ddr);
2489 set_ddr_init_ext_addr(ddr);
Dave Liuec145e82010-03-05 12:22:00 +08002490 set_timing_cfg_4(ddr, popts);
York Sune1fd16b2011-01-10 12:03:00 +00002491 set_timing_cfg_5(ddr, cas_latency);
York Sun34e026f2014-03-27 17:54:47 -07002492#ifdef CONFIG_SYS_FSL_DDR4
2493 set_ddr_sdram_cfg_3(ddr, popts);
2494 set_timing_cfg_6(ddr);
York Sun03e664d2015-01-06 13:18:50 -08002495 set_timing_cfg_7(ctrl_num, ddr, common_dimm);
2496 set_timing_cfg_8(ctrl_num, ddr, popts, common_dimm, cas_latency);
York Sun34e026f2014-03-27 17:54:47 -07002497 set_timing_cfg_9(ddr);
2498 set_ddr_dq_mapping(ddr, dimm_params);
2499#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002500
Dave Liuc360cea2009-03-14 12:48:30 +08002501 set_ddr_zq_cntl(ddr, zq_en);
Dave Liubdc9f7b2009-12-16 10:24:37 -06002502 set_ddr_wrlvl_cntl(ddr, wrlvl_en, popts);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002503
Dave Liu22cca7e2008-11-21 16:31:35 +08002504 set_ddr_sr_cntr(ddr, sr_it);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002505
York Sune1fd16b2011-01-10 12:03:00 +00002506 set_ddr_sdram_rcw(ddr, popts, common_dimm);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002507
York Suncb930712013-06-25 11:37:41 -07002508#ifdef CONFIG_SYS_FSL_DDR_EMU
2509 /* disble DDR training for emulator */
2510 ddr->debug[2] = 0x00000400;
York Sun1f3402e2015-01-06 13:18:45 -08002511 ddr->debug[4] = 0xff800800;
2512 ddr->debug[5] = 0x08000800;
2513 ddr->debug[6] = 0x08000800;
2514 ddr->debug[7] = 0x08000800;
2515 ddr->debug[8] = 0x08000800;
York Suncb930712013-06-25 11:37:41 -07002516#endif
York Sun9855b3b2014-05-23 13:15:00 -07002517#ifdef CONFIG_SYS_FSL_ERRATUM_A004508
2518 if ((ip_rev >= 0x40000) && (ip_rev < 0x40400))
2519 ddr->debug[2] |= 0x00000200; /* set bit 22 */
2520#endif
2521
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002522 return check_fsl_memctl_config_regs(ddr);
2523}