blob: bcab9046adcaacc0c7f7aa440306bfeff8ca3b2a [file] [log] [blame]
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001/*
York Sunc0c32af2018-01-29 09:44:35 -08002 * Copyright 2008-2016 Freescale Semiconductor, Inc.
3 * Copyright 2017-2018 NXP Semiconductor
Kumar Gala58e5e9a2008-08-26 15:01:29 -05004 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Kumar Gala58e5e9a2008-08-26 15:01:29 -05006 */
7
8/*
Shengzhou Liu02fb2762016-11-21 11:36:48 +08009 * Generic driver for Freescale DDR/DDR2/DDR3/DDR4 memory controller.
Kumar Gala58e5e9a2008-08-26 15:01:29 -050010 * Based on code from spd_sdram.c
11 * Author: James Yang [at freescale.com]
12 */
13
14#include <common.h>
York Sun5614e712013-09-30 09:22:09 -070015#include <fsl_ddr_sdram.h>
Shengzhou Liu02fb2762016-11-21 11:36:48 +080016#include <fsl_errata.h>
York Sun5614e712013-09-30 09:22:09 -070017#include <fsl_ddr.h>
York Sun9a17eb52013-11-18 10:29:32 -080018#include <fsl_immap.h>
York Sun5614e712013-09-30 09:22:09 -070019#include <asm/io.h>
Simon Glass457e51c2017-05-17 08:23:10 -060020#if defined(CONFIG_FSL_LSCH2) || defined(CONFIG_FSL_LSCH3) || \
21 defined(CONFIG_ARM)
Simon Glass6e2941d2017-05-17 08:23:06 -060022#include <asm/arch/clock.h>
23#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -050024
Kumar Gala58e5e9a2008-08-26 15:01:29 -050025/*
26 * Determine Rtt value.
27 *
28 * This should likely be either board or controller specific.
29 *
Dave Liuc360cea2009-03-14 12:48:30 +080030 * Rtt(nominal) - DDR2:
Kumar Gala58e5e9a2008-08-26 15:01:29 -050031 * 0 = Rtt disabled
32 * 1 = 75 ohm
33 * 2 = 150 ohm
34 * 3 = 50 ohm
Dave Liuc360cea2009-03-14 12:48:30 +080035 * Rtt(nominal) - DDR3:
36 * 0 = Rtt disabled
37 * 1 = 60 ohm
38 * 2 = 120 ohm
39 * 3 = 40 ohm
40 * 4 = 20 ohm
41 * 5 = 30 ohm
Kumar Gala58e5e9a2008-08-26 15:01:29 -050042 *
43 * FIXME: Apparently 8641 needs a value of 2
44 * FIXME: Old code seys if 667 MHz or higher, use 3 on 8572
45 *
46 * FIXME: There was some effort down this line earlier:
47 *
48 * unsigned int i;
49 * for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL/2; i++) {
50 * if (popts->dimmslot[i].num_valid_cs
51 * && (popts->cs_local_opts[2*i].odt_rd_cfg
52 * || popts->cs_local_opts[2*i].odt_wr_cfg)) {
53 * rtt = 2;
54 * break;
55 * }
56 * }
57 */
58static inline int fsl_ddr_get_rtt(void)
59{
60 int rtt;
61
York Sun5614e712013-09-30 09:22:09 -070062#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala58e5e9a2008-08-26 15:01:29 -050063 rtt = 0;
York Sun5614e712013-09-30 09:22:09 -070064#elif defined(CONFIG_SYS_FSL_DDR2)
Kumar Gala58e5e9a2008-08-26 15:01:29 -050065 rtt = 3;
66#else
Dave Liuc360cea2009-03-14 12:48:30 +080067 rtt = 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -050068#endif
69
70 return rtt;
71}
72
York Sun34e026f2014-03-27 17:54:47 -070073#ifdef CONFIG_SYS_FSL_DDR4
74/*
75 * compute CAS write latency according to DDR4 spec
76 * CWL = 9 for <= 1600MT/s
77 * 10 for <= 1866MT/s
78 * 11 for <= 2133MT/s
79 * 12 for <= 2400MT/s
80 * 14 for <= 2667MT/s
81 * 16 for <= 2933MT/s
82 * 18 for higher
83 */
York Sun03e664d2015-01-06 13:18:50 -080084static inline unsigned int compute_cas_write_latency(
85 const unsigned int ctrl_num)
York Sun34e026f2014-03-27 17:54:47 -070086{
87 unsigned int cwl;
York Sun03e664d2015-01-06 13:18:50 -080088 const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
York Sun34e026f2014-03-27 17:54:47 -070089 if (mclk_ps >= 1250)
90 cwl = 9;
91 else if (mclk_ps >= 1070)
92 cwl = 10;
93 else if (mclk_ps >= 935)
94 cwl = 11;
95 else if (mclk_ps >= 833)
96 cwl = 12;
97 else if (mclk_ps >= 750)
98 cwl = 14;
99 else if (mclk_ps >= 681)
100 cwl = 16;
101 else
102 cwl = 18;
103
104 return cwl;
105}
106#else
Dave Liuc360cea2009-03-14 12:48:30 +0800107/*
108 * compute the CAS write latency according to DDR3 spec
109 * CWL = 5 if tCK >= 2.5ns
110 * 6 if 2.5ns > tCK >= 1.875ns
111 * 7 if 1.875ns > tCK >= 1.5ns
112 * 8 if 1.5ns > tCK >= 1.25ns
York Sun2bba85f2011-08-24 09:40:25 -0700113 * 9 if 1.25ns > tCK >= 1.07ns
114 * 10 if 1.07ns > tCK >= 0.935ns
115 * 11 if 0.935ns > tCK >= 0.833ns
116 * 12 if 0.833ns > tCK >= 0.75ns
Dave Liuc360cea2009-03-14 12:48:30 +0800117 */
York Sun03e664d2015-01-06 13:18:50 -0800118static inline unsigned int compute_cas_write_latency(
119 const unsigned int ctrl_num)
Dave Liuc360cea2009-03-14 12:48:30 +0800120{
121 unsigned int cwl;
York Sun03e664d2015-01-06 13:18:50 -0800122 const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
Dave Liuc360cea2009-03-14 12:48:30 +0800123
124 if (mclk_ps >= 2500)
125 cwl = 5;
126 else if (mclk_ps >= 1875)
127 cwl = 6;
128 else if (mclk_ps >= 1500)
129 cwl = 7;
130 else if (mclk_ps >= 1250)
131 cwl = 8;
York Sun2bba85f2011-08-24 09:40:25 -0700132 else if (mclk_ps >= 1070)
133 cwl = 9;
134 else if (mclk_ps >= 935)
135 cwl = 10;
136 else if (mclk_ps >= 833)
137 cwl = 11;
138 else if (mclk_ps >= 750)
139 cwl = 12;
140 else {
141 cwl = 12;
142 printf("Warning: CWL is out of range\n");
143 }
Dave Liuc360cea2009-03-14 12:48:30 +0800144 return cwl;
145}
York Sun34e026f2014-03-27 17:54:47 -0700146#endif
Dave Liuc360cea2009-03-14 12:48:30 +0800147
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500148/* Chip Select Configuration (CSn_CONFIG) */
york5800e7a2010-07-02 22:25:53 +0000149static void set_csn_config(int dimm_number, int i, fsl_ddr_cfg_regs_t *ddr,
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500150 const memctl_options_t *popts,
151 const dimm_params_t *dimm_params)
152{
153 unsigned int cs_n_en = 0; /* Chip Select enable */
154 unsigned int intlv_en = 0; /* Memory controller interleave enable */
155 unsigned int intlv_ctl = 0; /* Interleaving control */
156 unsigned int ap_n_en = 0; /* Chip select n auto-precharge enable */
157 unsigned int odt_rd_cfg = 0; /* ODT for reads configuration */
158 unsigned int odt_wr_cfg = 0; /* ODT for writes configuration */
159 unsigned int ba_bits_cs_n = 0; /* Num of bank bits for SDRAM on CSn */
160 unsigned int row_bits_cs_n = 0; /* Num of row bits for SDRAM on CSn */
161 unsigned int col_bits_cs_n = 0; /* Num of ocl bits for SDRAM on CSn */
york5800e7a2010-07-02 22:25:53 +0000162 int go_config = 0;
York Sun34e026f2014-03-27 17:54:47 -0700163#ifdef CONFIG_SYS_FSL_DDR4
164 unsigned int bg_bits_cs_n = 0; /* Num of bank group bits */
165#else
166 unsigned int n_banks_per_sdram_device;
167#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500168
169 /* Compute CS_CONFIG only for existing ranks of each DIMM. */
york5800e7a2010-07-02 22:25:53 +0000170 switch (i) {
171 case 0:
172 if (dimm_params[dimm_number].n_ranks > 0) {
173 go_config = 1;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500174 /* These fields only available in CS0_CONFIG */
York Suna4c66502012-08-17 08:22:39 +0000175 if (!popts->memctl_interleaving)
176 break;
177 switch (popts->memctl_interleaving_mode) {
York Sun6b1e1252014-02-10 13:59:44 -0800178 case FSL_DDR_256B_INTERLEAVING:
York Suna4c66502012-08-17 08:22:39 +0000179 case FSL_DDR_CACHE_LINE_INTERLEAVING:
180 case FSL_DDR_PAGE_INTERLEAVING:
181 case FSL_DDR_BANK_INTERLEAVING:
182 case FSL_DDR_SUPERBANK_INTERLEAVING:
183 intlv_en = popts->memctl_interleaving;
184 intlv_ctl = popts->memctl_interleaving_mode;
185 break;
186 default:
187 break;
188 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500189 }
york5800e7a2010-07-02 22:25:53 +0000190 break;
191 case 1:
192 if ((dimm_number == 0 && dimm_params[0].n_ranks > 1) || \
193 (dimm_number == 1 && dimm_params[1].n_ranks > 0))
194 go_config = 1;
195 break;
196 case 2:
197 if ((dimm_number == 0 && dimm_params[0].n_ranks > 2) || \
York Suncae7c1b2011-08-26 11:32:40 -0700198 (dimm_number >= 1 && dimm_params[dimm_number].n_ranks > 0))
york5800e7a2010-07-02 22:25:53 +0000199 go_config = 1;
200 break;
201 case 3:
202 if ((dimm_number == 0 && dimm_params[0].n_ranks > 3) || \
203 (dimm_number == 1 && dimm_params[1].n_ranks > 1) || \
204 (dimm_number == 3 && dimm_params[3].n_ranks > 0))
205 go_config = 1;
206 break;
207 default:
208 break;
209 }
210 if (go_config) {
york5800e7a2010-07-02 22:25:53 +0000211 cs_n_en = 1;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500212 ap_n_en = popts->cs_local_opts[i].auto_precharge;
213 odt_rd_cfg = popts->cs_local_opts[i].odt_rd_cfg;
214 odt_wr_cfg = popts->cs_local_opts[i].odt_wr_cfg;
York Sun34e026f2014-03-27 17:54:47 -0700215#ifdef CONFIG_SYS_FSL_DDR4
216 ba_bits_cs_n = dimm_params[dimm_number].bank_addr_bits;
217 bg_bits_cs_n = dimm_params[dimm_number].bank_group_bits;
218#else
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500219 n_banks_per_sdram_device
york5800e7a2010-07-02 22:25:53 +0000220 = dimm_params[dimm_number].n_banks_per_sdram_device;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500221 ba_bits_cs_n = __ilog2(n_banks_per_sdram_device) - 2;
York Sun34e026f2014-03-27 17:54:47 -0700222#endif
york5800e7a2010-07-02 22:25:53 +0000223 row_bits_cs_n = dimm_params[dimm_number].n_row_addr - 12;
224 col_bits_cs_n = dimm_params[dimm_number].n_col_addr - 8;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500225 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500226 ddr->cs[i].config = (0
227 | ((cs_n_en & 0x1) << 31)
228 | ((intlv_en & 0x3) << 29)
Haiying Wangdbbbb3a2008-10-03 12:36:39 -0400229 | ((intlv_ctl & 0xf) << 24)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500230 | ((ap_n_en & 0x1) << 23)
231
232 /* XXX: some implementation only have 1 bit starting at left */
233 | ((odt_rd_cfg & 0x7) << 20)
234
235 /* XXX: Some implementation only have 1 bit starting at left */
236 | ((odt_wr_cfg & 0x7) << 16)
237
238 | ((ba_bits_cs_n & 0x3) << 14)
239 | ((row_bits_cs_n & 0x7) << 8)
York Sun34e026f2014-03-27 17:54:47 -0700240#ifdef CONFIG_SYS_FSL_DDR4
241 | ((bg_bits_cs_n & 0x3) << 4)
242#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500243 | ((col_bits_cs_n & 0x7) << 0)
244 );
Haiying Wang1f293b42008-10-03 12:37:26 -0400245 debug("FSLDDR: cs[%d]_config = 0x%08x\n", i,ddr->cs[i].config);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500246}
247
248/* Chip Select Configuration 2 (CSn_CONFIG_2) */
249/* FIXME: 8572 */
250static void set_csn_config_2(int i, fsl_ddr_cfg_regs_t *ddr)
251{
252 unsigned int pasr_cfg = 0; /* Partial array self refresh config */
253
254 ddr->cs[i].config_2 = ((pasr_cfg & 7) << 24);
Haiying Wang1f293b42008-10-03 12:37:26 -0400255 debug("FSLDDR: cs[%d]_config_2 = 0x%08x\n", i, ddr->cs[i].config_2);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500256}
257
258/* -3E = 667 CL5, -25 = CL6 800, -25E = CL5 800 */
259
York Sun5614e712013-09-30 09:22:09 -0700260#if !defined(CONFIG_SYS_FSL_DDR1)
York Sun84baed22014-11-07 12:14:36 -0800261/*
262 * Check DIMM configuration, return 2 if quad-rank or two dual-rank
263 * Return 1 if other two slots configuration. Return 0 if single slot.
264 */
York Sun123922b2012-10-08 07:44:23 +0000265static inline int avoid_odt_overlap(const dimm_params_t *dimm_params)
266{
267#if CONFIG_DIMM_SLOTS_PER_CTLR == 1
268 if (dimm_params[0].n_ranks == 4)
York Sun84baed22014-11-07 12:14:36 -0800269 return 2;
York Sun123922b2012-10-08 07:44:23 +0000270#endif
271
272#if CONFIG_DIMM_SLOTS_PER_CTLR == 2
273 if ((dimm_params[0].n_ranks == 2) &&
274 (dimm_params[1].n_ranks == 2))
York Sun84baed22014-11-07 12:14:36 -0800275 return 2;
York Sun123922b2012-10-08 07:44:23 +0000276
277#ifdef CONFIG_FSL_DDR_FIRST_SLOT_QUAD_CAPABLE
278 if (dimm_params[0].n_ranks == 4)
York Sun84baed22014-11-07 12:14:36 -0800279 return 2;
York Sun123922b2012-10-08 07:44:23 +0000280#endif
York Sun84baed22014-11-07 12:14:36 -0800281
282 if ((dimm_params[0].n_ranks != 0) &&
283 (dimm_params[2].n_ranks != 0))
284 return 1;
York Sun123922b2012-10-08 07:44:23 +0000285#endif
286 return 0;
287}
288
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500289/*
290 * DDR SDRAM Timing Configuration 0 (TIMING_CFG_0)
291 *
292 * Avoid writing for DDR I. The new PQ38 DDR controller
293 * dreams up non-zero default values to be backwards compatible.
294 */
York Sun03e664d2015-01-06 13:18:50 -0800295static void set_timing_cfg_0(const unsigned int ctrl_num,
296 fsl_ddr_cfg_regs_t *ddr,
York Sun123922b2012-10-08 07:44:23 +0000297 const memctl_options_t *popts,
298 const dimm_params_t *dimm_params)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500299{
300 unsigned char trwt_mclk = 0; /* Read-to-write turnaround */
301 unsigned char twrt_mclk = 0; /* Write-to-read turnaround */
302 /* 7.5 ns on -3E; 0 means WL - CL + BL/2 + 1 */
303 unsigned char trrt_mclk = 0; /* Read-to-read turnaround */
304 unsigned char twwt_mclk = 0; /* Write-to-write turnaround */
305
306 /* Active powerdown exit timing (tXARD and tXARDS). */
307 unsigned char act_pd_exit_mclk;
308 /* Precharge powerdown exit timing (tXP). */
309 unsigned char pre_pd_exit_mclk;
york5fb8a8a2010-07-02 22:25:56 +0000310 /* ODT powerdown exit timing (tAXPD). */
York Sun34e026f2014-03-27 17:54:47 -0700311 unsigned char taxpd_mclk = 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500312 /* Mode register set cycle time (tMRD). */
313 unsigned char tmrd_mclk;
York Sunbb578322014-08-21 16:13:22 -0700314#if defined(CONFIG_SYS_FSL_DDR4) || defined(CONFIG_SYS_FSL_DDR3)
York Sun03e664d2015-01-06 13:18:50 -0800315 const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
York Sunbb578322014-08-21 16:13:22 -0700316#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500317
York Sun34e026f2014-03-27 17:54:47 -0700318#ifdef CONFIG_SYS_FSL_DDR4
319 /* tXP=max(4nCK, 6ns) */
Masahiro Yamadab4141192014-11-07 03:03:31 +0900320 int txp = max((int)mclk_ps * 4, 6000); /* unit=ps */
York Sun66869f92015-03-19 09:30:26 -0700321 unsigned int data_rate = get_ddr_freq(ctrl_num);
322
323 /* for faster clock, need more time for data setup */
324 trwt_mclk = (data_rate/1000000 > 1900) ? 3 : 2;
York Sun6c6e0062015-11-04 10:03:21 -0800325
326 /*
327 * for single quad-rank DIMM and two-slot DIMMs
328 * to avoid ODT overlap
329 */
330 switch (avoid_odt_overlap(dimm_params)) {
331 case 2:
332 twrt_mclk = 2;
333 twwt_mclk = 2;
334 trrt_mclk = 2;
335 break;
336 default:
337 twrt_mclk = 1;
338 twwt_mclk = 1;
339 trrt_mclk = 0;
340 break;
341 }
342
York Sun03e664d2015-01-06 13:18:50 -0800343 act_pd_exit_mclk = picos_to_mclk(ctrl_num, txp);
York Sun34e026f2014-03-27 17:54:47 -0700344 pre_pd_exit_mclk = act_pd_exit_mclk;
345 /*
346 * MRS_CYC = max(tMRD, tMOD)
347 * tMRD = 8nCK, tMOD = max(24nCK, 15ns)
348 */
York Sun03e664d2015-01-06 13:18:50 -0800349 tmrd_mclk = max(24U, picos_to_mclk(ctrl_num, 15000));
York Sun34e026f2014-03-27 17:54:47 -0700350#elif defined(CONFIG_SYS_FSL_DDR3)
York Sun03e664d2015-01-06 13:18:50 -0800351 unsigned int data_rate = get_ddr_freq(ctrl_num);
York Sunbb578322014-08-21 16:13:22 -0700352 int txp;
York Sun938bbb62014-12-02 11:18:09 -0800353 unsigned int ip_rev;
York Sun84baed22014-11-07 12:14:36 -0800354 int odt_overlap;
Dave Liuc360cea2009-03-14 12:48:30 +0800355 /*
356 * (tXARD and tXARDS). Empirical?
357 * The DDR3 spec has not tXARD,
358 * we use the tXP instead of it.
York Sunbb578322014-08-21 16:13:22 -0700359 * tXP=max(3nCK, 7.5ns) for DDR3-800, 1066
360 * max(3nCK, 6ns) for DDR3-1333, 1600, 1866, 2133
Dave Liuc360cea2009-03-14 12:48:30 +0800361 * spec has not the tAXPD, we use
york5fb8a8a2010-07-02 22:25:56 +0000362 * tAXPD=1, need design to confirm.
Dave Liuc360cea2009-03-14 12:48:30 +0800363 */
Masahiro Yamadab4141192014-11-07 03:03:31 +0900364 txp = max((int)mclk_ps * 3, (mclk_ps > 1540 ? 7500 : 6000));
York Sunbb578322014-08-21 16:13:22 -0700365
York Sun66869f92015-03-19 09:30:26 -0700366 ip_rev = fsl_ddr_get_version(ctrl_num);
York Sun938bbb62014-12-02 11:18:09 -0800367 if (ip_rev >= 0x40700) {
368 /*
369 * MRS_CYC = max(tMRD, tMOD)
370 * tMRD = 4nCK (8nCK for RDIMM)
371 * tMOD = max(12nCK, 15ns)
372 */
York Sun03e664d2015-01-06 13:18:50 -0800373 tmrd_mclk = max((unsigned int)12,
374 picos_to_mclk(ctrl_num, 15000));
York Sun938bbb62014-12-02 11:18:09 -0800375 } else {
376 /*
377 * MRS_CYC = tMRD
378 * tMRD = 4nCK (8nCK for RDIMM)
379 */
380 if (popts->registered_dimm_en)
381 tmrd_mclk = 8;
382 else
383 tmrd_mclk = 4;
384 }
385
Dave Liu99bac472009-12-08 11:56:48 +0800386 /* set the turnaround time */
York Sun123922b2012-10-08 07:44:23 +0000387
388 /*
York Sun84baed22014-11-07 12:14:36 -0800389 * for single quad-rank DIMM and two-slot DIMMs
York Sun123922b2012-10-08 07:44:23 +0000390 * to avoid ODT overlap
391 */
York Sun84baed22014-11-07 12:14:36 -0800392 odt_overlap = avoid_odt_overlap(dimm_params);
393 switch (odt_overlap) {
394 case 2:
York Sun123922b2012-10-08 07:44:23 +0000395 twwt_mclk = 2;
396 trrt_mclk = 1;
York Sun84baed22014-11-07 12:14:36 -0800397 break;
398 case 1:
399 twwt_mclk = 1;
400 trrt_mclk = 0;
401 break;
402 default:
403 break;
York Sun123922b2012-10-08 07:44:23 +0000404 }
York Sun84baed22014-11-07 12:14:36 -0800405
York Sun123922b2012-10-08 07:44:23 +0000406 /* for faster clock, need more time for data setup */
407 trwt_mclk = (data_rate/1000000 > 1800) ? 2 : 1;
408
York Sun856e4b02011-02-10 10:13:10 -0800409 if ((data_rate/1000000 > 1150) || (popts->memctl_interleaving))
410 twrt_mclk = 1;
York Sune1fd16b2011-01-10 12:03:00 +0000411
412 if (popts->dynamic_power == 0) { /* powerdown is not used */
413 act_pd_exit_mclk = 1;
414 pre_pd_exit_mclk = 1;
415 taxpd_mclk = 1;
416 } else {
417 /* act_pd_exit_mclk = tXARD, see above */
York Sun03e664d2015-01-06 13:18:50 -0800418 act_pd_exit_mclk = picos_to_mclk(ctrl_num, txp);
York Sune1fd16b2011-01-10 12:03:00 +0000419 /* Mode register MR0[A12] is '1' - fast exit */
420 pre_pd_exit_mclk = act_pd_exit_mclk;
421 taxpd_mclk = 1;
422 }
York Sun5614e712013-09-30 09:22:09 -0700423#else /* CONFIG_SYS_FSL_DDR2 */
Dave Liuc360cea2009-03-14 12:48:30 +0800424 /*
425 * (tXARD and tXARDS). Empirical?
426 * tXARD = 2 for DDR2
427 * tXP=2
428 * tAXPD=8
429 */
430 act_pd_exit_mclk = 2;
431 pre_pd_exit_mclk = 2;
432 taxpd_mclk = 8;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500433 tmrd_mclk = 2;
Dave Liuc360cea2009-03-14 12:48:30 +0800434#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500435
York Sun23f96702011-05-27 13:44:28 +0800436 if (popts->trwt_override)
437 trwt_mclk = popts->trwt;
438
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500439 ddr->timing_cfg_0 = (0
440 | ((trwt_mclk & 0x3) << 30) /* RWT */
441 | ((twrt_mclk & 0x3) << 28) /* WRT */
442 | ((trrt_mclk & 0x3) << 26) /* RRT */
443 | ((twwt_mclk & 0x3) << 24) /* WWT */
York Sund4263b82013-06-03 12:39:06 -0700444 | ((act_pd_exit_mclk & 0xf) << 20) /* ACT_PD_EXIT */
Dave Liu22ff3d02008-11-21 16:31:29 +0800445 | ((pre_pd_exit_mclk & 0xF) << 16) /* PRE_PD_EXIT */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500446 | ((taxpd_mclk & 0xf) << 8) /* ODT_PD_EXIT */
York Sund4263b82013-06-03 12:39:06 -0700447 | ((tmrd_mclk & 0x1f) << 0) /* MRS_CYC */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500448 );
449 debug("FSLDDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
450}
York Sun84baed22014-11-07 12:14:36 -0800451#endif /* !defined(CONFIG_SYS_FSL_DDR1) */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500452
453/* DDR SDRAM Timing Configuration 3 (TIMING_CFG_3) */
York Sun03e664d2015-01-06 13:18:50 -0800454static void set_timing_cfg_3(const unsigned int ctrl_num,
455 fsl_ddr_cfg_regs_t *ddr,
456 const memctl_options_t *popts,
457 const common_timing_params_t *common_dimm,
458 unsigned int cas_latency,
459 unsigned int additive_latency)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500460{
York Sun45064ad2012-08-17 08:22:40 +0000461 /* Extended precharge to activate interval (tRP) */
462 unsigned int ext_pretoact = 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500463 /* Extended Activate to precharge interval (tRAS) */
464 unsigned int ext_acttopre = 0;
York Sun45064ad2012-08-17 08:22:40 +0000465 /* Extended activate to read/write interval (tRCD) */
466 unsigned int ext_acttorw = 0;
467 /* Extended refresh recovery time (tRFC) */
468 unsigned int ext_refrec;
469 /* Extended MCAS latency from READ cmd */
470 unsigned int ext_caslat = 0;
York Sund4263b82013-06-03 12:39:06 -0700471 /* Extended additive latency */
472 unsigned int ext_add_lat = 0;
York Sun45064ad2012-08-17 08:22:40 +0000473 /* Extended last data to precharge interval (tWR) */
474 unsigned int ext_wrrec = 0;
475 /* Control Adjust */
476 unsigned int cntl_adj = 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500477
York Sun03e664d2015-01-06 13:18:50 -0800478 ext_pretoact = picos_to_mclk(ctrl_num, common_dimm->trp_ps) >> 4;
479 ext_acttopre = picos_to_mclk(ctrl_num, common_dimm->tras_ps) >> 4;
480 ext_acttorw = picos_to_mclk(ctrl_num, common_dimm->trcd_ps) >> 4;
York Sun45064ad2012-08-17 08:22:40 +0000481 ext_caslat = (2 * cas_latency - 1) >> 4;
York Sund4263b82013-06-03 12:39:06 -0700482 ext_add_lat = additive_latency >> 4;
York Sun34e026f2014-03-27 17:54:47 -0700483#ifdef CONFIG_SYS_FSL_DDR4
York Sun03e664d2015-01-06 13:18:50 -0800484 ext_refrec = (picos_to_mclk(ctrl_num, common_dimm->trfc1_ps) - 8) >> 4;
York Sun34e026f2014-03-27 17:54:47 -0700485#else
York Sun03e664d2015-01-06 13:18:50 -0800486 ext_refrec = (picos_to_mclk(ctrl_num, common_dimm->trfc_ps) - 8) >> 4;
York Sun45064ad2012-08-17 08:22:40 +0000487 /* ext_wrrec only deals with 16 clock and above, or 14 with OTF */
York Sun34e026f2014-03-27 17:54:47 -0700488#endif
York Sun03e664d2015-01-06 13:18:50 -0800489 ext_wrrec = (picos_to_mclk(ctrl_num, common_dimm->twr_ps) +
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530490 (popts->otf_burst_chop_en ? 2 : 0)) >> 4;
Dave Liuc360cea2009-03-14 12:48:30 +0800491
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500492 ddr->timing_cfg_3 = (0
York Sun45064ad2012-08-17 08:22:40 +0000493 | ((ext_pretoact & 0x1) << 28)
James Yangc45f5c02013-07-22 09:35:26 -0700494 | ((ext_acttopre & 0x3) << 24)
York Sun45064ad2012-08-17 08:22:40 +0000495 | ((ext_acttorw & 0x1) << 22)
York Sunc0c32af2018-01-29 09:44:35 -0800496 | ((ext_refrec & 0x3F) << 16)
York Sun45064ad2012-08-17 08:22:40 +0000497 | ((ext_caslat & 0x3) << 12)
York Sund4263b82013-06-03 12:39:06 -0700498 | ((ext_add_lat & 0x1) << 10)
York Sun45064ad2012-08-17 08:22:40 +0000499 | ((ext_wrrec & 0x1) << 8)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500500 | ((cntl_adj & 0x7) << 0)
501 );
Haiying Wang1f293b42008-10-03 12:37:26 -0400502 debug("FSLDDR: timing_cfg_3 = 0x%08x\n", ddr->timing_cfg_3);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500503}
504
505/* DDR SDRAM Timing Configuration 1 (TIMING_CFG_1) */
York Sun03e664d2015-01-06 13:18:50 -0800506static void set_timing_cfg_1(const unsigned int ctrl_num,
507 fsl_ddr_cfg_regs_t *ddr,
508 const memctl_options_t *popts,
509 const common_timing_params_t *common_dimm,
510 unsigned int cas_latency)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500511{
512 /* Precharge-to-activate interval (tRP) */
513 unsigned char pretoact_mclk;
514 /* Activate to precharge interval (tRAS) */
515 unsigned char acttopre_mclk;
516 /* Activate to read/write interval (tRCD) */
517 unsigned char acttorw_mclk;
518 /* CASLAT */
519 unsigned char caslat_ctrl;
520 /* Refresh recovery time (tRFC) ; trfc_low */
521 unsigned char refrec_ctrl;
522 /* Last data to precharge minimum interval (tWR) */
523 unsigned char wrrec_mclk;
524 /* Activate-to-activate interval (tRRD) */
525 unsigned char acttoact_mclk;
526 /* Last write data pair to read command issue interval (tWTR) */
527 unsigned char wrtord_mclk;
York Sun34e026f2014-03-27 17:54:47 -0700528#ifdef CONFIG_SYS_FSL_DDR4
529 /* DDR4 supports 10, 12, 14, 16, 18, 20, 24 */
530 static const u8 wrrec_table[] = {
531 10, 10, 10, 10, 10,
532 10, 10, 10, 10, 10,
533 12, 12, 14, 14, 16,
534 16, 18, 18, 20, 20,
535 24, 24, 24, 24};
536#else
York Sunf5b6fb72011-03-02 14:24:11 -0800537 /* DDR_SDRAM_MODE doesn't support 9,11,13,15 */
538 static const u8 wrrec_table[] = {
539 1, 2, 3, 4, 5, 6, 7, 8, 10, 10, 12, 12, 14, 14, 0, 0};
York Sun34e026f2014-03-27 17:54:47 -0700540#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500541
York Sun03e664d2015-01-06 13:18:50 -0800542 pretoact_mclk = picos_to_mclk(ctrl_num, common_dimm->trp_ps);
543 acttopre_mclk = picos_to_mclk(ctrl_num, common_dimm->tras_ps);
544 acttorw_mclk = picos_to_mclk(ctrl_num, common_dimm->trcd_ps);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500545
546 /*
547 * Translate CAS Latency to a DDR controller field value:
548 *
549 * CAS Lat DDR I DDR II Ctrl
550 * Clocks SPD Bit SPD Bit Value
551 * ------- ------- ------- -----
552 * 1.0 0 0001
553 * 1.5 1 0010
554 * 2.0 2 2 0011
555 * 2.5 3 0100
556 * 3.0 4 3 0101
557 * 3.5 5 0110
558 * 4.0 4 0111
559 * 4.5 1000
560 * 5.0 5 1001
561 */
York Sun5614e712013-09-30 09:22:09 -0700562#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500563 caslat_ctrl = (cas_latency + 1) & 0x07;
York Sun5614e712013-09-30 09:22:09 -0700564#elif defined(CONFIG_SYS_FSL_DDR2)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500565 caslat_ctrl = 2 * cas_latency - 1;
566#else
Dave Liuc360cea2009-03-14 12:48:30 +0800567 /*
568 * if the CAS latency more than 8 cycle,
569 * we need set extend bit for it at
570 * TIMING_CFG_3[EXT_CASLAT]
571 */
York Sun66869f92015-03-19 09:30:26 -0700572 if (fsl_ddr_get_version(ctrl_num) <= 0x40400)
York Sun34e026f2014-03-27 17:54:47 -0700573 caslat_ctrl = 2 * cas_latency - 1;
574 else
575 caslat_ctrl = (cas_latency - 1) << 1;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500576#endif
577
York Sun34e026f2014-03-27 17:54:47 -0700578#ifdef CONFIG_SYS_FSL_DDR4
York Sun03e664d2015-01-06 13:18:50 -0800579 refrec_ctrl = picos_to_mclk(ctrl_num, common_dimm->trfc1_ps) - 8;
580 wrrec_mclk = picos_to_mclk(ctrl_num, common_dimm->twr_ps);
581 acttoact_mclk = max(picos_to_mclk(ctrl_num, common_dimm->trrds_ps), 4U);
582 wrtord_mclk = max(2U, picos_to_mclk(ctrl_num, 2500));
York Sun349689b2014-04-01 14:20:49 -0700583 if ((wrrec_mclk < 1) || (wrrec_mclk > 24))
584 printf("Error: WRREC doesn't support %d clocks\n", wrrec_mclk);
York Sun34e026f2014-03-27 17:54:47 -0700585 else
586 wrrec_mclk = wrrec_table[wrrec_mclk - 1];
587#else
York Sun03e664d2015-01-06 13:18:50 -0800588 refrec_ctrl = picos_to_mclk(ctrl_num, common_dimm->trfc_ps) - 8;
589 wrrec_mclk = picos_to_mclk(ctrl_num, common_dimm->twr_ps);
590 acttoact_mclk = picos_to_mclk(ctrl_num, common_dimm->trrd_ps);
591 wrtord_mclk = picos_to_mclk(ctrl_num, common_dimm->twtr_ps);
York Sun349689b2014-04-01 14:20:49 -0700592 if ((wrrec_mclk < 1) || (wrrec_mclk > 16))
593 printf("Error: WRREC doesn't support %d clocks\n", wrrec_mclk);
York Sun45064ad2012-08-17 08:22:40 +0000594 else
595 wrrec_mclk = wrrec_table[wrrec_mclk - 1];
York Sun34e026f2014-03-27 17:54:47 -0700596#endif
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530597 if (popts->otf_burst_chop_en)
Dave Liuc360cea2009-03-14 12:48:30 +0800598 wrrec_mclk += 2;
599
Dave Liuc360cea2009-03-14 12:48:30 +0800600 /*
601 * JEDEC has min requirement for tRRD
602 */
York Sun5614e712013-09-30 09:22:09 -0700603#if defined(CONFIG_SYS_FSL_DDR3)
Dave Liuc360cea2009-03-14 12:48:30 +0800604 if (acttoact_mclk < 4)
605 acttoact_mclk = 4;
606#endif
Dave Liuc360cea2009-03-14 12:48:30 +0800607 /*
608 * JEDEC has some min requirements for tWTR
609 */
York Sun5614e712013-09-30 09:22:09 -0700610#if defined(CONFIG_SYS_FSL_DDR2)
Dave Liuc360cea2009-03-14 12:48:30 +0800611 if (wrtord_mclk < 2)
612 wrtord_mclk = 2;
York Sun5614e712013-09-30 09:22:09 -0700613#elif defined(CONFIG_SYS_FSL_DDR3)
Dave Liuc360cea2009-03-14 12:48:30 +0800614 if (wrtord_mclk < 4)
615 wrtord_mclk = 4;
616#endif
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530617 if (popts->otf_burst_chop_en)
Dave Liuc360cea2009-03-14 12:48:30 +0800618 wrtord_mclk += 2;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500619
620 ddr->timing_cfg_1 = (0
Dave Liu80ee3ce2008-11-21 16:31:22 +0800621 | ((pretoact_mclk & 0x0F) << 28)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500622 | ((acttopre_mclk & 0x0F) << 24)
Dave Liu80ee3ce2008-11-21 16:31:22 +0800623 | ((acttorw_mclk & 0xF) << 20)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500624 | ((caslat_ctrl & 0xF) << 16)
625 | ((refrec_ctrl & 0xF) << 12)
Dave Liu80ee3ce2008-11-21 16:31:22 +0800626 | ((wrrec_mclk & 0x0F) << 8)
York Sun57495e42012-10-08 07:44:22 +0000627 | ((acttoact_mclk & 0x0F) << 4)
628 | ((wrtord_mclk & 0x0F) << 0)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500629 );
Haiying Wang1f293b42008-10-03 12:37:26 -0400630 debug("FSLDDR: timing_cfg_1 = 0x%08x\n", ddr->timing_cfg_1);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500631}
632
633/* DDR SDRAM Timing Configuration 2 (TIMING_CFG_2) */
York Sun03e664d2015-01-06 13:18:50 -0800634static void set_timing_cfg_2(const unsigned int ctrl_num,
635 fsl_ddr_cfg_regs_t *ddr,
636 const memctl_options_t *popts,
637 const common_timing_params_t *common_dimm,
638 unsigned int cas_latency,
639 unsigned int additive_latency)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500640{
641 /* Additive latency */
642 unsigned char add_lat_mclk;
643 /* CAS-to-preamble override */
644 unsigned short cpo;
645 /* Write latency */
646 unsigned char wr_lat;
647 /* Read to precharge (tRTP) */
648 unsigned char rd_to_pre;
649 /* Write command to write data strobe timing adjustment */
650 unsigned char wr_data_delay;
651 /* Minimum CKE pulse width (tCKE) */
652 unsigned char cke_pls;
653 /* Window for four activates (tFAW) */
654 unsigned short four_act;
York Sunbb578322014-08-21 16:13:22 -0700655#ifdef CONFIG_SYS_FSL_DDR3
York Sun03e664d2015-01-06 13:18:50 -0800656 const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
York Sunbb578322014-08-21 16:13:22 -0700657#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500658
659 /* FIXME add check that this must be less than acttorw_mclk */
660 add_lat_mclk = additive_latency;
661 cpo = popts->cpo_override;
662
York Sun5614e712013-09-30 09:22:09 -0700663#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500664 /*
665 * This is a lie. It should really be 1, but if it is
666 * set to 1, bits overlap into the old controller's
667 * otherwise unused ACSM field. If we leave it 0, then
668 * the HW will magically treat it as 1 for DDR 1. Oh Yea.
669 */
670 wr_lat = 0;
York Sun5614e712013-09-30 09:22:09 -0700671#elif defined(CONFIG_SYS_FSL_DDR2)
Dave Liu6a819782009-03-14 12:48:19 +0800672 wr_lat = cas_latency - 1;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500673#else
York Sun03e664d2015-01-06 13:18:50 -0800674 wr_lat = compute_cas_write_latency(ctrl_num);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500675#endif
676
York Sun34e026f2014-03-27 17:54:47 -0700677#ifdef CONFIG_SYS_FSL_DDR4
York Sun03e664d2015-01-06 13:18:50 -0800678 rd_to_pre = picos_to_mclk(ctrl_num, 7500);
York Sun34e026f2014-03-27 17:54:47 -0700679#else
York Sun03e664d2015-01-06 13:18:50 -0800680 rd_to_pre = picos_to_mclk(ctrl_num, common_dimm->trtp_ps);
York Sun34e026f2014-03-27 17:54:47 -0700681#endif
Dave Liuc360cea2009-03-14 12:48:30 +0800682 /*
683 * JEDEC has some min requirements for tRTP
684 */
York Sun5614e712013-09-30 09:22:09 -0700685#if defined(CONFIG_SYS_FSL_DDR2)
Dave Liuc360cea2009-03-14 12:48:30 +0800686 if (rd_to_pre < 2)
687 rd_to_pre = 2;
York Sun34e026f2014-03-27 17:54:47 -0700688#elif defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
Dave Liuc360cea2009-03-14 12:48:30 +0800689 if (rd_to_pre < 4)
690 rd_to_pre = 4;
Dave Liu6a819782009-03-14 12:48:19 +0800691#endif
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530692 if (popts->otf_burst_chop_en)
Dave Liuc360cea2009-03-14 12:48:30 +0800693 rd_to_pre += 2; /* according to UM */
694
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500695 wr_data_delay = popts->write_data_delay;
York Sun34e026f2014-03-27 17:54:47 -0700696#ifdef CONFIG_SYS_FSL_DDR4
697 cpo = 0;
York Sun03e664d2015-01-06 13:18:50 -0800698 cke_pls = max(3U, picos_to_mclk(ctrl_num, 5000));
York Sunbb578322014-08-21 16:13:22 -0700699#elif defined(CONFIG_SYS_FSL_DDR3)
700 /*
701 * cke pulse = max(3nCK, 7.5ns) for DDR3-800
702 * max(3nCK, 5.625ns) for DDR3-1066, 1333
703 * max(3nCK, 5ns) for DDR3-1600, 1866, 2133
704 */
York Sun03e664d2015-01-06 13:18:50 -0800705 cke_pls = max(3U, picos_to_mclk(ctrl_num, mclk_ps > 1870 ? 7500 :
706 (mclk_ps > 1245 ? 5625 : 5000)));
York Sun34e026f2014-03-27 17:54:47 -0700707#else
York Sunbb578322014-08-21 16:13:22 -0700708 cke_pls = FSL_DDR_MIN_TCKE_PULSE_WIDTH_DDR;
York Sun34e026f2014-03-27 17:54:47 -0700709#endif
York Sun03e664d2015-01-06 13:18:50 -0800710 four_act = picos_to_mclk(ctrl_num,
711 popts->tfaw_window_four_activates_ps);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500712
713 ddr->timing_cfg_2 = (0
Dave Liu22ff3d02008-11-21 16:31:29 +0800714 | ((add_lat_mclk & 0xf) << 28)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500715 | ((cpo & 0x1f) << 23)
Dave Liu22ff3d02008-11-21 16:31:29 +0800716 | ((wr_lat & 0xf) << 19)
York Sun89366912016-07-29 09:02:29 -0700717 | (((wr_lat & 0x10) >> 4) << 18)
Dave Liuc360cea2009-03-14 12:48:30 +0800718 | ((rd_to_pre & RD_TO_PRE_MASK) << RD_TO_PRE_SHIFT)
719 | ((wr_data_delay & WR_DATA_DELAY_MASK) << WR_DATA_DELAY_SHIFT)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500720 | ((cke_pls & 0x7) << 6)
Dave Liu22ff3d02008-11-21 16:31:29 +0800721 | ((four_act & 0x3f) << 0)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500722 );
Haiying Wang1f293b42008-10-03 12:37:26 -0400723 debug("FSLDDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500724}
725
york9490ff42010-07-02 22:25:55 +0000726/* DDR SDRAM Register Control Word */
727static void set_ddr_sdram_rcw(fsl_ddr_cfg_regs_t *ddr,
York Sune1fd16b2011-01-10 12:03:00 +0000728 const memctl_options_t *popts,
york9490ff42010-07-02 22:25:55 +0000729 const common_timing_params_t *common_dimm)
730{
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530731 if (common_dimm->all_dimms_registered &&
732 !common_dimm->all_dimms_unbuffered) {
York Sune1fd16b2011-01-10 12:03:00 +0000733 if (popts->rcw_override) {
734 ddr->ddr_sdram_rcw_1 = popts->rcw_1;
735 ddr->ddr_sdram_rcw_2 = popts->rcw_2;
York Sun426230a2018-01-29 09:44:33 -0800736 ddr->ddr_sdram_rcw_3 = popts->rcw_3;
York Sune1fd16b2011-01-10 12:03:00 +0000737 } else {
738 ddr->ddr_sdram_rcw_1 =
739 common_dimm->rcw[0] << 28 | \
740 common_dimm->rcw[1] << 24 | \
741 common_dimm->rcw[2] << 20 | \
742 common_dimm->rcw[3] << 16 | \
743 common_dimm->rcw[4] << 12 | \
744 common_dimm->rcw[5] << 8 | \
745 common_dimm->rcw[6] << 4 | \
746 common_dimm->rcw[7];
747 ddr->ddr_sdram_rcw_2 =
748 common_dimm->rcw[8] << 28 | \
749 common_dimm->rcw[9] << 24 | \
750 common_dimm->rcw[10] << 20 | \
751 common_dimm->rcw[11] << 16 | \
752 common_dimm->rcw[12] << 12 | \
753 common_dimm->rcw[13] << 8 | \
754 common_dimm->rcw[14] << 4 | \
755 common_dimm->rcw[15];
756 }
York Sun426230a2018-01-29 09:44:33 -0800757 debug("FSLDDR: ddr_sdram_rcw_1 = 0x%08x\n",
758 ddr->ddr_sdram_rcw_1);
759 debug("FSLDDR: ddr_sdram_rcw_2 = 0x%08x\n",
760 ddr->ddr_sdram_rcw_2);
761 debug("FSLDDR: ddr_sdram_rcw_3 = 0x%08x\n",
762 ddr->ddr_sdram_rcw_3);
york9490ff42010-07-02 22:25:55 +0000763 }
764}
765
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500766/* DDR SDRAM control configuration (DDR_SDRAM_CFG) */
767static void set_ddr_sdram_cfg(fsl_ddr_cfg_regs_t *ddr,
768 const memctl_options_t *popts,
769 const common_timing_params_t *common_dimm)
770{
771 unsigned int mem_en; /* DDR SDRAM interface logic enable */
772 unsigned int sren; /* Self refresh enable (during sleep) */
773 unsigned int ecc_en; /* ECC enable. */
774 unsigned int rd_en; /* Registered DIMM enable */
775 unsigned int sdram_type; /* Type of SDRAM */
776 unsigned int dyn_pwr; /* Dynamic power management mode */
777 unsigned int dbw; /* DRAM dta bus width */
Dave Liu22ff3d02008-11-21 16:31:29 +0800778 unsigned int eight_be = 0; /* 8-beat burst enable, DDR2 is zero */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500779 unsigned int ncap = 0; /* Non-concurrent auto-precharge */
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530780 unsigned int threet_en; /* Enable 3T timing */
781 unsigned int twot_en; /* Enable 2T timing */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500782 unsigned int ba_intlv_ctl; /* Bank (CS) interleaving control */
783 unsigned int x32_en = 0; /* x32 enable */
784 unsigned int pchb8 = 0; /* precharge bit 8 enable */
785 unsigned int hse; /* Global half strength override */
York Sund28cb672014-09-05 13:52:41 +0800786 unsigned int acc_ecc_en = 0; /* Accumulated ECC enable */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500787 unsigned int mem_halt = 0; /* memory controller halt */
788 unsigned int bi = 0; /* Bypass initialization */
789
790 mem_en = 1;
791 sren = popts->self_refresh_in_sleep;
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530792 if (common_dimm->all_dimms_ecc_capable) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500793 /* Allow setting of ECC only if all DIMMs are ECC. */
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530794 ecc_en = popts->ecc_mode;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500795 } else {
796 ecc_en = 0;
797 }
798
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530799 if (common_dimm->all_dimms_registered &&
800 !common_dimm->all_dimms_unbuffered) {
York Sune1fd16b2011-01-10 12:03:00 +0000801 rd_en = 1;
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530802 twot_en = 0;
York Sune1fd16b2011-01-10 12:03:00 +0000803 } else {
804 rd_en = 0;
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530805 twot_en = popts->twot_en;
York Sune1fd16b2011-01-10 12:03:00 +0000806 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500807
808 sdram_type = CONFIG_FSL_SDRAM_TYPE;
809
810 dyn_pwr = popts->dynamic_power;
811 dbw = popts->data_bus_width;
Dave Liuc360cea2009-03-14 12:48:30 +0800812 /* 8-beat burst enable DDR-III case
813 * we must clear it when use the on-the-fly mode,
814 * must set it when use the 32-bits bus mode.
815 */
York Sun34e026f2014-03-27 17:54:47 -0700816 if ((sdram_type == SDRAM_TYPE_DDR3) ||
817 (sdram_type == SDRAM_TYPE_DDR4)) {
Dave Liuc360cea2009-03-14 12:48:30 +0800818 if (popts->burst_length == DDR_BL8)
819 eight_be = 1;
820 if (popts->burst_length == DDR_OTF)
821 eight_be = 0;
822 if (dbw == 0x1)
823 eight_be = 1;
824 }
825
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530826 threet_en = popts->threet_en;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500827 ba_intlv_ctl = popts->ba_intlv_ctl;
828 hse = popts->half_strength_driver_enable;
829
York Sund28cb672014-09-05 13:52:41 +0800830 /* set when ddr bus width < 64 */
831 acc_ecc_en = (dbw != 0 && ecc_en == 1) ? 1 : 0;
832
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500833 ddr->ddr_sdram_cfg = (0
834 | ((mem_en & 0x1) << 31)
835 | ((sren & 0x1) << 30)
836 | ((ecc_en & 0x1) << 29)
837 | ((rd_en & 0x1) << 28)
838 | ((sdram_type & 0x7) << 24)
839 | ((dyn_pwr & 0x1) << 21)
840 | ((dbw & 0x3) << 19)
841 | ((eight_be & 0x1) << 18)
842 | ((ncap & 0x1) << 17)
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530843 | ((threet_en & 0x1) << 16)
844 | ((twot_en & 0x1) << 15)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500845 | ((ba_intlv_ctl & 0x7F) << 8)
846 | ((x32_en & 0x1) << 5)
847 | ((pchb8 & 0x1) << 4)
848 | ((hse & 0x1) << 3)
York Sund28cb672014-09-05 13:52:41 +0800849 | ((acc_ecc_en & 0x1) << 2)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500850 | ((mem_halt & 0x1) << 1)
851 | ((bi & 0x1) << 0)
852 );
Haiying Wang1f293b42008-10-03 12:37:26 -0400853 debug("FSLDDR: ddr_sdram_cfg = 0x%08x\n", ddr->ddr_sdram_cfg);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500854}
855
856/* DDR SDRAM control configuration 2 (DDR_SDRAM_CFG_2) */
York Sun03e664d2015-01-06 13:18:50 -0800857static void set_ddr_sdram_cfg_2(const unsigned int ctrl_num,
858 fsl_ddr_cfg_regs_t *ddr,
York Sune1fd16b2011-01-10 12:03:00 +0000859 const memctl_options_t *popts,
860 const unsigned int unq_mrs_en)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500861{
862 unsigned int frc_sr = 0; /* Force self refresh */
863 unsigned int sr_ie = 0; /* Self-refresh interrupt enable */
York Suncae7c1b2011-08-26 11:32:40 -0700864 unsigned int odt_cfg = 0; /* ODT configuration */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500865 unsigned int num_pr; /* Number of posted refreshes */
York Sun57495e42012-10-08 07:44:22 +0000866 unsigned int slow = 0; /* DDR will be run less than 1250 */
York Sunb61e0612013-06-25 11:37:47 -0700867 unsigned int x4_en = 0; /* x4 DRAM enable */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500868 unsigned int obc_cfg; /* On-The-Fly Burst Chop Cfg */
869 unsigned int ap_en; /* Address Parity Enable */
870 unsigned int d_init; /* DRAM data initialization */
871 unsigned int rcw_en = 0; /* Register Control Word Enable */
872 unsigned int md_en = 0; /* Mirrored DIMM Enable */
york5800e7a2010-07-02 22:25:53 +0000873 unsigned int qd_en = 0; /* quad-rank DIMM Enable */
York Suncae7c1b2011-08-26 11:32:40 -0700874 int i;
York Sun34e026f2014-03-27 17:54:47 -0700875#ifndef CONFIG_SYS_FSL_DDR4
876 unsigned int dll_rst_dis = 1; /* DLL reset disable */
877 unsigned int dqs_cfg; /* DQS configuration */
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500878
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530879 dqs_cfg = popts->dqs_config;
York Sun34e026f2014-03-27 17:54:47 -0700880#endif
York Suncae7c1b2011-08-26 11:32:40 -0700881 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
882 if (popts->cs_local_opts[i].odt_rd_cfg
883 || popts->cs_local_opts[i].odt_wr_cfg) {
884 odt_cfg = SDRAM_CFG2_ODT_ONLY_READ;
885 break;
886 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500887 }
Joakim Tjernlunde368c202015-10-14 16:32:00 +0200888 sr_ie = popts->self_refresh_interrupt_en;
York Sunc0c32af2018-01-29 09:44:35 -0800889 num_pr = popts->package_3ds + 1;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500890
891 /*
892 * 8572 manual says
893 * {TIMING_CFG_1[PRETOACT]
894 * + [DDR_SDRAM_CFG_2[NUM_PR]
895 * * ({EXT_REFREC || REFREC} + 8 + 2)]}
896 * << DDR_SDRAM_INTERVAL[REFINT]
897 */
York Sun34e026f2014-03-27 17:54:47 -0700898#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530899 obc_cfg = popts->otf_burst_chop_en;
Dave Liuc360cea2009-03-14 12:48:30 +0800900#else
901 obc_cfg = 0;
902#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500903
York Sun57495e42012-10-08 07:44:22 +0000904#if (CONFIG_SYS_FSL_DDR_VER >= FSL_DDR_VER_4_7)
York Sun03e664d2015-01-06 13:18:50 -0800905 slow = get_ddr_freq(ctrl_num) < 1249000000;
York Sun57495e42012-10-08 07:44:22 +0000906#endif
907
Shengzhou Liueb118802016-03-10 17:36:56 +0800908 if (popts->registered_dimm_en)
York Sune1fd16b2011-01-10 12:03:00 +0000909 rcw_en = 1;
Shengzhou Liueb118802016-03-10 17:36:56 +0800910
911 /* DDR4 can have address parity for UDIMM and discrete */
912 if ((CONFIG_FSL_SDRAM_TYPE != SDRAM_TYPE_DDR4) &&
913 (!popts->registered_dimm_en)) {
York Sune1fd16b2011-01-10 12:03:00 +0000914 ap_en = 0;
Shengzhou Liueb118802016-03-10 17:36:56 +0800915 } else {
916 ap_en = popts->ap_en;
York Sune1fd16b2011-01-10 12:03:00 +0000917 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500918
York Sunb61e0612013-06-25 11:37:47 -0700919 x4_en = popts->x4_en ? 1 : 0;
920
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500921#if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
922 /* Use the DDR controller to auto initialize memory. */
Priyanka Jain0dd38a32013-09-25 10:41:19 +0530923 d_init = popts->ecc_init_using_memctl;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500924 ddr->ddr_data_init = CONFIG_MEM_INIT_VALUE;
925 debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init);
926#else
927 /* Memory will be initialized via DMA, or not at all. */
928 d_init = 0;
929#endif
930
York Sun34e026f2014-03-27 17:54:47 -0700931#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
Dave Liuc360cea2009-03-14 12:48:30 +0800932 md_en = popts->mirrored_dimm;
933#endif
york5800e7a2010-07-02 22:25:53 +0000934 qd_en = popts->quad_rank_present ? 1 : 0;
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500935 ddr->ddr_sdram_cfg_2 = (0
936 | ((frc_sr & 0x1) << 31)
937 | ((sr_ie & 0x1) << 30)
York Sun34e026f2014-03-27 17:54:47 -0700938#ifndef CONFIG_SYS_FSL_DDR4
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500939 | ((dll_rst_dis & 0x1) << 29)
940 | ((dqs_cfg & 0x3) << 26)
York Sun34e026f2014-03-27 17:54:47 -0700941#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500942 | ((odt_cfg & 0x3) << 21)
943 | ((num_pr & 0xf) << 12)
York Sun57495e42012-10-08 07:44:22 +0000944 | ((slow & 1) << 11)
York Sunb61e0612013-06-25 11:37:47 -0700945 | (x4_en << 10)
york5800e7a2010-07-02 22:25:53 +0000946 | (qd_en << 9)
York Sune1fd16b2011-01-10 12:03:00 +0000947 | (unq_mrs_en << 8)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500948 | ((obc_cfg & 0x1) << 6)
949 | ((ap_en & 0x1) << 5)
950 | ((d_init & 0x1) << 4)
951 | ((rcw_en & 0x1) << 2)
952 | ((md_en & 0x1) << 0)
953 );
Haiying Wang1f293b42008-10-03 12:37:26 -0400954 debug("FSLDDR: ddr_sdram_cfg_2 = 0x%08x\n", ddr->ddr_sdram_cfg_2);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500955}
956
York Sun34e026f2014-03-27 17:54:47 -0700957#ifdef CONFIG_SYS_FSL_DDR4
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500958/* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
York Sun03e664d2015-01-06 13:18:50 -0800959static void set_ddr_sdram_mode_2(const unsigned int ctrl_num,
960 fsl_ddr_cfg_regs_t *ddr,
York Sune1fd16b2011-01-10 12:03:00 +0000961 const memctl_options_t *popts,
Valentin Longchamp7e157b02013-10-18 11:47:20 +0200962 const common_timing_params_t *common_dimm,
York Sune1fd16b2011-01-10 12:03:00 +0000963 const unsigned int unq_mrs_en)
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500964{
965 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
966 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
York Sun34e026f2014-03-27 17:54:47 -0700967 int i;
968 unsigned int wr_crc = 0; /* Disable */
969 unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */
970 unsigned int srt = 0; /* self-refresh temerature, normal range */
York Sun03e664d2015-01-06 13:18:50 -0800971 unsigned int cwl = compute_cas_write_latency(ctrl_num) - 9;
York Sun34e026f2014-03-27 17:54:47 -0700972 unsigned int mpr = 0; /* serial */
973 unsigned int wc_lat;
York Sun03e664d2015-01-06 13:18:50 -0800974 const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
Kumar Gala58e5e9a2008-08-26 15:01:29 -0500975
York Sun34e026f2014-03-27 17:54:47 -0700976 if (popts->rtt_override)
977 rtt_wr = popts->rtt_wr_override_value;
978 else
979 rtt_wr = popts->cs_local_opts[0].odt_rtt_wr;
980
981 if (common_dimm->extended_op_srt)
982 srt = common_dimm->extended_op_srt;
983
984 esdmode2 = (0
985 | ((wr_crc & 0x1) << 12)
986 | ((rtt_wr & 0x3) << 9)
987 | ((srt & 0x3) << 6)
988 | ((cwl & 0x7) << 3));
989
990 if (mclk_ps >= 1250)
991 wc_lat = 0;
992 else if (mclk_ps >= 833)
993 wc_lat = 1;
994 else
995 wc_lat = 2;
996
997 esdmode3 = (0
998 | ((mpr & 0x3) << 11)
999 | ((wc_lat & 0x3) << 9));
1000
1001 ddr->ddr_sdram_mode_2 = (0
1002 | ((esdmode2 & 0xFFFF) << 16)
1003 | ((esdmode3 & 0xFFFF) << 0)
1004 );
1005 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
1006
1007 if (unq_mrs_en) { /* unique mode registers are supported */
1008 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1009 if (popts->rtt_override)
1010 rtt_wr = popts->rtt_wr_override_value;
1011 else
1012 rtt_wr = popts->cs_local_opts[i].odt_rtt_wr;
1013
1014 esdmode2 &= 0xF9FF; /* clear bit 10, 9 */
1015 esdmode2 |= (rtt_wr & 0x3) << 9;
1016 switch (i) {
1017 case 1:
1018 ddr->ddr_sdram_mode_4 = (0
1019 | ((esdmode2 & 0xFFFF) << 16)
1020 | ((esdmode3 & 0xFFFF) << 0)
1021 );
1022 break;
1023 case 2:
1024 ddr->ddr_sdram_mode_6 = (0
1025 | ((esdmode2 & 0xFFFF) << 16)
1026 | ((esdmode3 & 0xFFFF) << 0)
1027 );
1028 break;
1029 case 3:
1030 ddr->ddr_sdram_mode_8 = (0
1031 | ((esdmode2 & 0xFFFF) << 16)
1032 | ((esdmode3 & 0xFFFF) << 0)
1033 );
1034 break;
1035 }
1036 }
1037 debug("FSLDDR: ddr_sdram_mode_4 = 0x%08x\n",
1038 ddr->ddr_sdram_mode_4);
1039 debug("FSLDDR: ddr_sdram_mode_6 = 0x%08x\n",
1040 ddr->ddr_sdram_mode_6);
1041 debug("FSLDDR: ddr_sdram_mode_8 = 0x%08x\n",
1042 ddr->ddr_sdram_mode_8);
1043 }
1044}
1045#elif defined(CONFIG_SYS_FSL_DDR3)
1046/* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
York Sun03e664d2015-01-06 13:18:50 -08001047static void set_ddr_sdram_mode_2(const unsigned int ctrl_num,
1048 fsl_ddr_cfg_regs_t *ddr,
York Sun34e026f2014-03-27 17:54:47 -07001049 const memctl_options_t *popts,
1050 const common_timing_params_t *common_dimm,
1051 const unsigned int unq_mrs_en)
1052{
1053 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
1054 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
Kumar Gala92966832011-01-20 01:53:15 -06001055 int i;
Dave Liu1aa3d082009-12-16 10:24:38 -06001056 unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */
Dave Liuc360cea2009-03-14 12:48:30 +08001057 unsigned int srt = 0; /* self-refresh temerature, normal range */
1058 unsigned int asr = 0; /* auto self-refresh disable */
York Sun03e664d2015-01-06 13:18:50 -08001059 unsigned int cwl = compute_cas_write_latency(ctrl_num) - 5;
Dave Liuc360cea2009-03-14 12:48:30 +08001060 unsigned int pasr = 0; /* partial array self refresh disable */
1061
Dave Liu1aa3d082009-12-16 10:24:38 -06001062 if (popts->rtt_override)
1063 rtt_wr = popts->rtt_wr_override_value;
York Sune1fd16b2011-01-10 12:03:00 +00001064 else
1065 rtt_wr = popts->cs_local_opts[0].odt_rtt_wr;
Valentin Longchamp7e157b02013-10-18 11:47:20 +02001066
1067 if (common_dimm->extended_op_srt)
1068 srt = common_dimm->extended_op_srt;
1069
Dave Liuc360cea2009-03-14 12:48:30 +08001070 esdmode2 = (0
1071 | ((rtt_wr & 0x3) << 9)
1072 | ((srt & 0x1) << 7)
1073 | ((asr & 0x1) << 6)
1074 | ((cwl & 0x7) << 3)
1075 | ((pasr & 0x7) << 0));
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001076 ddr->ddr_sdram_mode_2 = (0
1077 | ((esdmode2 & 0xFFFF) << 16)
1078 | ((esdmode3 & 0xFFFF) << 0)
1079 );
Haiying Wang1f293b42008-10-03 12:37:26 -04001080 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
York Sune1fd16b2011-01-10 12:03:00 +00001081
York Sune1fd16b2011-01-10 12:03:00 +00001082 if (unq_mrs_en) { /* unique mode registers are supported */
Kumar Galadea7f882011-11-09 10:05:10 -06001083 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Sune1fd16b2011-01-10 12:03:00 +00001084 if (popts->rtt_override)
1085 rtt_wr = popts->rtt_wr_override_value;
1086 else
1087 rtt_wr = popts->cs_local_opts[i].odt_rtt_wr;
1088
1089 esdmode2 &= 0xF9FF; /* clear bit 10, 9 */
1090 esdmode2 |= (rtt_wr & 0x3) << 9;
1091 switch (i) {
1092 case 1:
1093 ddr->ddr_sdram_mode_4 = (0
1094 | ((esdmode2 & 0xFFFF) << 16)
1095 | ((esdmode3 & 0xFFFF) << 0)
1096 );
1097 break;
1098 case 2:
1099 ddr->ddr_sdram_mode_6 = (0
1100 | ((esdmode2 & 0xFFFF) << 16)
1101 | ((esdmode3 & 0xFFFF) << 0)
1102 );
1103 break;
1104 case 3:
1105 ddr->ddr_sdram_mode_8 = (0
1106 | ((esdmode2 & 0xFFFF) << 16)
1107 | ((esdmode3 & 0xFFFF) << 0)
1108 );
1109 break;
1110 }
1111 }
1112 debug("FSLDDR: ddr_sdram_mode_4 = 0x%08x\n",
1113 ddr->ddr_sdram_mode_4);
1114 debug("FSLDDR: ddr_sdram_mode_6 = 0x%08x\n",
1115 ddr->ddr_sdram_mode_6);
1116 debug("FSLDDR: ddr_sdram_mode_8 = 0x%08x\n",
1117 ddr->ddr_sdram_mode_8);
1118 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001119}
1120
York Sun34e026f2014-03-27 17:54:47 -07001121#else /* for DDR2 and DDR1 */
1122/* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
York Sun03e664d2015-01-06 13:18:50 -08001123static void set_ddr_sdram_mode_2(const unsigned int ctrl_num,
1124 fsl_ddr_cfg_regs_t *ddr,
York Sun34e026f2014-03-27 17:54:47 -07001125 const memctl_options_t *popts,
1126 const common_timing_params_t *common_dimm,
1127 const unsigned int unq_mrs_en)
1128{
1129 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
1130 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
1131
1132 ddr->ddr_sdram_mode_2 = (0
1133 | ((esdmode2 & 0xFFFF) << 16)
1134 | ((esdmode3 & 0xFFFF) << 0)
1135 );
1136 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
1137}
1138#endif
1139
1140#ifdef CONFIG_SYS_FSL_DDR4
1141/* DDR SDRAM Mode configuration 9 (DDR_SDRAM_MODE_9) */
1142static void set_ddr_sdram_mode_9(fsl_ddr_cfg_regs_t *ddr,
1143 const memctl_options_t *popts,
1144 const common_timing_params_t *common_dimm,
1145 const unsigned int unq_mrs_en)
1146{
1147 int i;
1148 unsigned short esdmode4 = 0; /* Extended SDRAM mode 4 */
1149 unsigned short esdmode5; /* Extended SDRAM mode 5 */
York Sun6b95be22015-03-19 09:30:27 -07001150 int rtt_park = 0;
York Sun8a514292015-11-04 10:03:19 -08001151 bool four_cs = false;
Shengzhou Liueb118802016-03-10 17:36:56 +08001152 const unsigned int mclk_ps = get_memory_clk_period_ps(0);
York Sun34e026f2014-03-27 17:54:47 -07001153
York Sun8a514292015-11-04 10:03:19 -08001154#if CONFIG_CHIP_SELECTS_PER_CTRL == 4
1155 if ((ddr->cs[0].config & SDRAM_CS_CONFIG_EN) &&
1156 (ddr->cs[1].config & SDRAM_CS_CONFIG_EN) &&
1157 (ddr->cs[2].config & SDRAM_CS_CONFIG_EN) &&
1158 (ddr->cs[3].config & SDRAM_CS_CONFIG_EN))
1159 four_cs = true;
1160#endif
York Sun6b95be22015-03-19 09:30:27 -07001161 if (ddr->cs[0].config & SDRAM_CS_CONFIG_EN) {
1162 esdmode5 = 0x00000500; /* Data mask enable, RTT_PARK CS0 */
York Sun8a514292015-11-04 10:03:19 -08001163 rtt_park = four_cs ? 0 : 1;
York Sun6b95be22015-03-19 09:30:27 -07001164 } else {
1165 esdmode5 = 0x00000400; /* Data mask enabled */
1166 }
York Sun34e026f2014-03-27 17:54:47 -07001167
York Sun426230a2018-01-29 09:44:33 -08001168 /*
1169 * For DDR3, set C/A latency if address parity is enabled.
1170 * For DDR4, set C/A latency for UDIMM only. For RDIMM the delay is
1171 * handled by register chip and RCW settings.
1172 */
1173 if ((ddr->ddr_sdram_cfg_2 & SDRAM_CFG2_AP_EN) &&
1174 ((CONFIG_FSL_SDRAM_TYPE != SDRAM_TYPE_DDR4) ||
1175 !popts->registered_dimm_en)) {
Shengzhou Liueb118802016-03-10 17:36:56 +08001176 if (mclk_ps >= 935) {
1177 /* for DDR4-1600/1866/2133 */
1178 esdmode5 |= DDR_MR5_CA_PARITY_LAT_4_CLK;
1179 } else if (mclk_ps >= 833) {
1180 /* for DDR4-2400 */
1181 esdmode5 |= DDR_MR5_CA_PARITY_LAT_5_CLK;
1182 } else {
1183 printf("parity: mclk_ps = %d not supported\n", mclk_ps);
1184 }
1185 }
1186
York Sun34e026f2014-03-27 17:54:47 -07001187 ddr->ddr_sdram_mode_9 = (0
1188 | ((esdmode4 & 0xffff) << 16)
1189 | ((esdmode5 & 0xffff) << 0)
1190 );
York Sun66869f92015-03-19 09:30:26 -07001191
York Sun8a514292015-11-04 10:03:19 -08001192 /* Normally only the first enabled CS use 0x500, others use 0x400
1193 * But when four chip-selects are all enabled, all mode registers
1194 * need 0x500 to park.
1195 */
York Sun66869f92015-03-19 09:30:26 -07001196
York Sunc0c32af2018-01-29 09:44:35 -08001197 debug("FSLDDR: ddr_sdram_mode_9 = 0x%08x\n", ddr->ddr_sdram_mode_9);
York Sun34e026f2014-03-27 17:54:47 -07001198 if (unq_mrs_en) { /* unique mode registers are supported */
1199 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Sun6b95be22015-03-19 09:30:27 -07001200 if (!rtt_park &&
1201 (ddr->cs[i].config & SDRAM_CS_CONFIG_EN)) {
1202 esdmode5 |= 0x00000500; /* RTT_PARK */
York Sun8a514292015-11-04 10:03:19 -08001203 rtt_park = four_cs ? 0 : 1;
York Sun6b95be22015-03-19 09:30:27 -07001204 } else {
1205 esdmode5 = 0x00000400;
1206 }
Shengzhou Liueb118802016-03-10 17:36:56 +08001207
York Sun426230a2018-01-29 09:44:33 -08001208 if ((ddr->ddr_sdram_cfg_2 & SDRAM_CFG2_AP_EN) &&
1209 ((CONFIG_FSL_SDRAM_TYPE != SDRAM_TYPE_DDR4) ||
1210 !popts->registered_dimm_en)) {
Shengzhou Liueb118802016-03-10 17:36:56 +08001211 if (mclk_ps >= 935) {
1212 /* for DDR4-1600/1866/2133 */
1213 esdmode5 |= DDR_MR5_CA_PARITY_LAT_4_CLK;
1214 } else if (mclk_ps >= 833) {
1215 /* for DDR4-2400 */
1216 esdmode5 |= DDR_MR5_CA_PARITY_LAT_5_CLK;
1217 } else {
1218 printf("parity: mclk_ps = %d not supported\n",
1219 mclk_ps);
1220 }
1221 }
1222
York Sun34e026f2014-03-27 17:54:47 -07001223 switch (i) {
1224 case 1:
1225 ddr->ddr_sdram_mode_11 = (0
1226 | ((esdmode4 & 0xFFFF) << 16)
1227 | ((esdmode5 & 0xFFFF) << 0)
1228 );
1229 break;
1230 case 2:
1231 ddr->ddr_sdram_mode_13 = (0
1232 | ((esdmode4 & 0xFFFF) << 16)
1233 | ((esdmode5 & 0xFFFF) << 0)
1234 );
1235 break;
1236 case 3:
1237 ddr->ddr_sdram_mode_15 = (0
1238 | ((esdmode4 & 0xFFFF) << 16)
1239 | ((esdmode5 & 0xFFFF) << 0)
1240 );
1241 break;
1242 }
1243 }
1244 debug("FSLDDR: ddr_sdram_mode_11 = 0x%08x\n",
1245 ddr->ddr_sdram_mode_11);
1246 debug("FSLDDR: ddr_sdram_mode_13 = 0x%08x\n",
1247 ddr->ddr_sdram_mode_13);
1248 debug("FSLDDR: ddr_sdram_mode_15 = 0x%08x\n",
1249 ddr->ddr_sdram_mode_15);
1250 }
1251}
1252
1253/* DDR SDRAM Mode configuration 10 (DDR_SDRAM_MODE_10) */
York Sun03e664d2015-01-06 13:18:50 -08001254static void set_ddr_sdram_mode_10(const unsigned int ctrl_num,
1255 fsl_ddr_cfg_regs_t *ddr,
York Sun34e026f2014-03-27 17:54:47 -07001256 const memctl_options_t *popts,
1257 const common_timing_params_t *common_dimm,
1258 const unsigned int unq_mrs_en)
1259{
1260 int i;
1261 unsigned short esdmode6 = 0; /* Extended SDRAM mode 6 */
1262 unsigned short esdmode7 = 0; /* Extended SDRAM mode 7 */
York Sun03e664d2015-01-06 13:18:50 -08001263 unsigned int tccdl_min = picos_to_mclk(ctrl_num, common_dimm->tccdl_ps);
York Sun34e026f2014-03-27 17:54:47 -07001264
1265 esdmode6 = ((tccdl_min - 4) & 0x7) << 10;
1266
York Sun0fb71972015-11-04 10:03:18 -08001267 if (popts->ddr_cdr2 & DDR_CDR2_VREF_RANGE_2)
1268 esdmode6 |= 1 << 6; /* Range 2 */
1269
York Sun34e026f2014-03-27 17:54:47 -07001270 ddr->ddr_sdram_mode_10 = (0
1271 | ((esdmode6 & 0xffff) << 16)
1272 | ((esdmode7 & 0xffff) << 0)
1273 );
York Sunc0c32af2018-01-29 09:44:35 -08001274 debug("FSLDDR: ddr_sdram_mode_10 = 0x%08x\n", ddr->ddr_sdram_mode_10);
York Sun34e026f2014-03-27 17:54:47 -07001275 if (unq_mrs_en) { /* unique mode registers are supported */
1276 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1277 switch (i) {
1278 case 1:
1279 ddr->ddr_sdram_mode_12 = (0
1280 | ((esdmode6 & 0xFFFF) << 16)
1281 | ((esdmode7 & 0xFFFF) << 0)
1282 );
1283 break;
1284 case 2:
1285 ddr->ddr_sdram_mode_14 = (0
1286 | ((esdmode6 & 0xFFFF) << 16)
1287 | ((esdmode7 & 0xFFFF) << 0)
1288 );
1289 break;
1290 case 3:
1291 ddr->ddr_sdram_mode_16 = (0
1292 | ((esdmode6 & 0xFFFF) << 16)
1293 | ((esdmode7 & 0xFFFF) << 0)
1294 );
1295 break;
1296 }
1297 }
1298 debug("FSLDDR: ddr_sdram_mode_12 = 0x%08x\n",
1299 ddr->ddr_sdram_mode_12);
1300 debug("FSLDDR: ddr_sdram_mode_14 = 0x%08x\n",
1301 ddr->ddr_sdram_mode_14);
1302 debug("FSLDDR: ddr_sdram_mode_16 = 0x%08x\n",
1303 ddr->ddr_sdram_mode_16);
1304 }
1305}
1306
1307#endif
1308
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001309/* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */
York Sun03e664d2015-01-06 13:18:50 -08001310static void set_ddr_sdram_interval(const unsigned int ctrl_num,
1311 fsl_ddr_cfg_regs_t *ddr,
1312 const memctl_options_t *popts,
1313 const common_timing_params_t *common_dimm)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001314{
1315 unsigned int refint; /* Refresh interval */
1316 unsigned int bstopre; /* Precharge interval */
1317
York Sun03e664d2015-01-06 13:18:50 -08001318 refint = picos_to_mclk(ctrl_num, common_dimm->refresh_rate_ps);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001319
1320 bstopre = popts->bstopre;
1321
1322 /* refint field used 0x3FFF in earlier controllers */
1323 ddr->ddr_sdram_interval = (0
1324 | ((refint & 0xFFFF) << 16)
1325 | ((bstopre & 0x3FFF) << 0)
1326 );
Haiying Wang1f293b42008-10-03 12:37:26 -04001327 debug("FSLDDR: ddr_sdram_interval = 0x%08x\n", ddr->ddr_sdram_interval);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001328}
1329
York Sun34e026f2014-03-27 17:54:47 -07001330#ifdef CONFIG_SYS_FSL_DDR4
Dave Liuc360cea2009-03-14 12:48:30 +08001331/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
York Sun03e664d2015-01-06 13:18:50 -08001332static void set_ddr_sdram_mode(const unsigned int ctrl_num,
1333 fsl_ddr_cfg_regs_t *ddr,
Dave Liuc360cea2009-03-14 12:48:30 +08001334 const memctl_options_t *popts,
1335 const common_timing_params_t *common_dimm,
1336 unsigned int cas_latency,
York Sune1fd16b2011-01-10 12:03:00 +00001337 unsigned int additive_latency,
1338 const unsigned int unq_mrs_en)
Dave Liuc360cea2009-03-14 12:48:30 +08001339{
York Sun34e026f2014-03-27 17:54:47 -07001340 int i;
1341 unsigned short esdmode; /* Extended SDRAM mode */
1342 unsigned short sdmode; /* SDRAM mode */
1343
1344 /* Mode Register - MR1 */
1345 unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */
1346 unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */
1347 unsigned int rtt;
1348 unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */
1349 unsigned int al = 0; /* Posted CAS# additive latency (AL) */
1350 unsigned int dic = 0; /* Output driver impedance, 40ohm */
1351 unsigned int dll_en = 1; /* DLL Enable 1=Enable (Normal),
1352 0=Disable (Test/Debug) */
1353
1354 /* Mode Register - MR0 */
1355 unsigned int wr = 0; /* Write Recovery */
1356 unsigned int dll_rst; /* DLL Reset */
1357 unsigned int mode; /* Normal=0 or Test=1 */
1358 unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
1359 /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
1360 unsigned int bt;
1361 unsigned int bl; /* BL: Burst Length */
1362
1363 unsigned int wr_mclk;
1364 /* DDR4 support WR 10, 12, 14, 16, 18, 20, 24 */
1365 static const u8 wr_table[] = {
1366 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6};
1367 /* DDR4 support CAS 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 22, 24 */
1368 static const u8 cas_latency_table[] = {
1369 0, 1, 2, 3, 4, 5, 6, 7, 8, 8,
1370 9, 9, 10, 10, 11, 11};
1371
1372 if (popts->rtt_override)
1373 rtt = popts->rtt_override_value;
1374 else
1375 rtt = popts->cs_local_opts[0].odt_rtt_norm;
1376
1377 if (additive_latency == (cas_latency - 1))
1378 al = 1;
1379 if (additive_latency == (cas_latency - 2))
1380 al = 2;
1381
1382 if (popts->quad_rank_present)
1383 dic = 1; /* output driver impedance 240/7 ohm */
1384
1385 /*
1386 * The esdmode value will also be used for writing
1387 * MR1 during write leveling for DDR3, although the
1388 * bits specifically related to the write leveling
1389 * scheme will be handled automatically by the DDR
1390 * controller. so we set the wrlvl_en = 0 here.
1391 */
1392 esdmode = (0
1393 | ((qoff & 0x1) << 12)
1394 | ((tdqs_en & 0x1) << 11)
1395 | ((rtt & 0x7) << 8)
1396 | ((wrlvl_en & 0x1) << 7)
1397 | ((al & 0x3) << 3)
1398 | ((dic & 0x3) << 1) /* DIC field is split */
1399 | ((dll_en & 0x1) << 0)
1400 );
1401
1402 /*
1403 * DLL control for precharge PD
1404 * 0=slow exit DLL off (tXPDLL)
1405 * 1=fast exit DLL on (tXP)
1406 */
1407
York Sun03e664d2015-01-06 13:18:50 -08001408 wr_mclk = picos_to_mclk(ctrl_num, common_dimm->twr_ps);
York Sun34e026f2014-03-27 17:54:47 -07001409 if (wr_mclk <= 24) {
1410 wr = wr_table[wr_mclk - 10];
1411 } else {
1412 printf("Error: unsupported write recovery for mode register wr_mclk = %d\n",
1413 wr_mclk);
1414 }
1415
1416 dll_rst = 0; /* dll no reset */
1417 mode = 0; /* normal mode */
1418
1419 /* look up table to get the cas latency bits */
1420 if (cas_latency >= 9 && cas_latency <= 24)
1421 caslat = cas_latency_table[cas_latency - 9];
1422 else
1423 printf("Error: unsupported cas latency for mode register\n");
1424
1425 bt = 0; /* Nibble sequential */
1426
1427 switch (popts->burst_length) {
1428 case DDR_BL8:
1429 bl = 0;
1430 break;
1431 case DDR_OTF:
1432 bl = 1;
1433 break;
1434 case DDR_BC4:
1435 bl = 2;
1436 break;
1437 default:
1438 printf("Error: invalid burst length of %u specified. ",
1439 popts->burst_length);
1440 puts("Defaulting to on-the-fly BC4 or BL8 beats.\n");
1441 bl = 1;
1442 break;
1443 }
1444
1445 sdmode = (0
1446 | ((wr & 0x7) << 9)
1447 | ((dll_rst & 0x1) << 8)
1448 | ((mode & 0x1) << 7)
1449 | (((caslat >> 1) & 0x7) << 4)
1450 | ((bt & 0x1) << 3)
1451 | ((caslat & 1) << 2)
1452 | ((bl & 0x3) << 0)
1453 );
1454
1455 ddr->ddr_sdram_mode = (0
1456 | ((esdmode & 0xFFFF) << 16)
1457 | ((sdmode & 0xFFFF) << 0)
1458 );
1459
1460 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
1461
1462 if (unq_mrs_en) { /* unique mode registers are supported */
1463 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1464 if (popts->rtt_override)
1465 rtt = popts->rtt_override_value;
1466 else
1467 rtt = popts->cs_local_opts[i].odt_rtt_norm;
1468
1469 esdmode &= 0xF8FF; /* clear bit 10,9,8 for rtt */
1470 esdmode |= (rtt & 0x7) << 8;
1471 switch (i) {
1472 case 1:
1473 ddr->ddr_sdram_mode_3 = (0
1474 | ((esdmode & 0xFFFF) << 16)
1475 | ((sdmode & 0xFFFF) << 0)
1476 );
1477 break;
1478 case 2:
1479 ddr->ddr_sdram_mode_5 = (0
1480 | ((esdmode & 0xFFFF) << 16)
1481 | ((sdmode & 0xFFFF) << 0)
1482 );
1483 break;
1484 case 3:
1485 ddr->ddr_sdram_mode_7 = (0
1486 | ((esdmode & 0xFFFF) << 16)
1487 | ((sdmode & 0xFFFF) << 0)
1488 );
1489 break;
1490 }
1491 }
1492 debug("FSLDDR: ddr_sdram_mode_3 = 0x%08x\n",
1493 ddr->ddr_sdram_mode_3);
1494 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1495 ddr->ddr_sdram_mode_5);
1496 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1497 ddr->ddr_sdram_mode_5);
1498 }
1499}
1500
1501#elif defined(CONFIG_SYS_FSL_DDR3)
1502/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
York Sun03e664d2015-01-06 13:18:50 -08001503static void set_ddr_sdram_mode(const unsigned int ctrl_num,
1504 fsl_ddr_cfg_regs_t *ddr,
York Sun34e026f2014-03-27 17:54:47 -07001505 const memctl_options_t *popts,
1506 const common_timing_params_t *common_dimm,
1507 unsigned int cas_latency,
1508 unsigned int additive_latency,
1509 const unsigned int unq_mrs_en)
1510{
1511 int i;
Dave Liuc360cea2009-03-14 12:48:30 +08001512 unsigned short esdmode; /* Extended SDRAM mode */
1513 unsigned short sdmode; /* SDRAM mode */
1514
1515 /* Mode Register - MR1 */
1516 unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */
1517 unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */
1518 unsigned int rtt;
1519 unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */
1520 unsigned int al = 0; /* Posted CAS# additive latency (AL) */
York Sune1fd16b2011-01-10 12:03:00 +00001521 unsigned int dic = 0; /* Output driver impedance, 40ohm */
Dave Liuc360cea2009-03-14 12:48:30 +08001522 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
1523 1=Disable (Test/Debug) */
1524
1525 /* Mode Register - MR0 */
1526 unsigned int dll_on; /* DLL control for precharge PD, 0=off, 1=on */
York Sunfcea3062012-08-17 08:22:38 +00001527 unsigned int wr = 0; /* Write Recovery */
Dave Liuc360cea2009-03-14 12:48:30 +08001528 unsigned int dll_rst; /* DLL Reset */
1529 unsigned int mode; /* Normal=0 or Test=1 */
1530 unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
1531 /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
1532 unsigned int bt;
1533 unsigned int bl; /* BL: Burst Length */
1534
1535 unsigned int wr_mclk;
York Sunf5b6fb72011-03-02 14:24:11 -08001536 /*
1537 * DDR_SDRAM_MODE doesn't support 9,11,13,15
1538 * Please refer JEDEC Standard No. 79-3E for Mode Register MR0
1539 * for this table
1540 */
1541 static const u8 wr_table[] = {1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 0, 0};
Dave Liuc360cea2009-03-14 12:48:30 +08001542
Dave Liuc360cea2009-03-14 12:48:30 +08001543 if (popts->rtt_override)
1544 rtt = popts->rtt_override_value;
York Sune1fd16b2011-01-10 12:03:00 +00001545 else
1546 rtt = popts->cs_local_opts[0].odt_rtt_norm;
Dave Liuc360cea2009-03-14 12:48:30 +08001547
1548 if (additive_latency == (cas_latency - 1))
1549 al = 1;
1550 if (additive_latency == (cas_latency - 2))
1551 al = 2;
1552
York Sune1fd16b2011-01-10 12:03:00 +00001553 if (popts->quad_rank_present)
1554 dic = 1; /* output driver impedance 240/7 ohm */
1555
Dave Liuc360cea2009-03-14 12:48:30 +08001556 /*
1557 * The esdmode value will also be used for writing
1558 * MR1 during write leveling for DDR3, although the
1559 * bits specifically related to the write leveling
1560 * scheme will be handled automatically by the DDR
1561 * controller. so we set the wrlvl_en = 0 here.
1562 */
1563 esdmode = (0
1564 | ((qoff & 0x1) << 12)
1565 | ((tdqs_en & 0x1) << 11)
Kumar Gala6d8565a2009-09-10 14:54:55 -05001566 | ((rtt & 0x4) << 7) /* rtt field is split */
Dave Liuc360cea2009-03-14 12:48:30 +08001567 | ((wrlvl_en & 0x1) << 7)
Kumar Gala6d8565a2009-09-10 14:54:55 -05001568 | ((rtt & 0x2) << 5) /* rtt field is split */
1569 | ((dic & 0x2) << 4) /* DIC field is split */
Dave Liuc360cea2009-03-14 12:48:30 +08001570 | ((al & 0x3) << 3)
Kumar Gala6d8565a2009-09-10 14:54:55 -05001571 | ((rtt & 0x1) << 2) /* rtt field is split */
Dave Liuc360cea2009-03-14 12:48:30 +08001572 | ((dic & 0x1) << 1) /* DIC field is split */
1573 | ((dll_en & 0x1) << 0)
1574 );
1575
1576 /*
1577 * DLL control for precharge PD
1578 * 0=slow exit DLL off (tXPDLL)
1579 * 1=fast exit DLL on (tXP)
1580 */
1581 dll_on = 1;
York Sunf5b6fb72011-03-02 14:24:11 -08001582
York Sun03e664d2015-01-06 13:18:50 -08001583 wr_mclk = picos_to_mclk(ctrl_num, common_dimm->twr_ps);
York Sunfcea3062012-08-17 08:22:38 +00001584 if (wr_mclk <= 16) {
1585 wr = wr_table[wr_mclk - 5];
1586 } else {
1587 printf("Error: unsupported write recovery for mode register "
1588 "wr_mclk = %d\n", wr_mclk);
1589 }
York Sunf5b6fb72011-03-02 14:24:11 -08001590
Dave Liuc360cea2009-03-14 12:48:30 +08001591 dll_rst = 0; /* dll no reset */
1592 mode = 0; /* normal mode */
1593
1594 /* look up table to get the cas latency bits */
York Sunfcea3062012-08-17 08:22:38 +00001595 if (cas_latency >= 5 && cas_latency <= 16) {
1596 unsigned char cas_latency_table[] = {
Dave Liuc360cea2009-03-14 12:48:30 +08001597 0x2, /* 5 clocks */
1598 0x4, /* 6 clocks */
1599 0x6, /* 7 clocks */
1600 0x8, /* 8 clocks */
1601 0xa, /* 9 clocks */
1602 0xc, /* 10 clocks */
York Sunfcea3062012-08-17 08:22:38 +00001603 0xe, /* 11 clocks */
1604 0x1, /* 12 clocks */
1605 0x3, /* 13 clocks */
1606 0x5, /* 14 clocks */
1607 0x7, /* 15 clocks */
1608 0x9, /* 16 clocks */
Dave Liuc360cea2009-03-14 12:48:30 +08001609 };
1610 caslat = cas_latency_table[cas_latency - 5];
York Sunfcea3062012-08-17 08:22:38 +00001611 } else {
1612 printf("Error: unsupported cas latency for mode register\n");
Dave Liuc360cea2009-03-14 12:48:30 +08001613 }
York Sunfcea3062012-08-17 08:22:38 +00001614
Dave Liuc360cea2009-03-14 12:48:30 +08001615 bt = 0; /* Nibble sequential */
1616
1617 switch (popts->burst_length) {
1618 case DDR_BL8:
1619 bl = 0;
1620 break;
1621 case DDR_OTF:
1622 bl = 1;
1623 break;
1624 case DDR_BC4:
1625 bl = 2;
1626 break;
1627 default:
1628 printf("Error: invalid burst length of %u specified. "
1629 " Defaulting to on-the-fly BC4 or BL8 beats.\n",
1630 popts->burst_length);
1631 bl = 1;
1632 break;
1633 }
1634
1635 sdmode = (0
1636 | ((dll_on & 0x1) << 12)
1637 | ((wr & 0x7) << 9)
1638 | ((dll_rst & 0x1) << 8)
1639 | ((mode & 0x1) << 7)
1640 | (((caslat >> 1) & 0x7) << 4)
1641 | ((bt & 0x1) << 3)
York Sunfcea3062012-08-17 08:22:38 +00001642 | ((caslat & 1) << 2)
Dave Liuc360cea2009-03-14 12:48:30 +08001643 | ((bl & 0x3) << 0)
1644 );
1645
1646 ddr->ddr_sdram_mode = (0
1647 | ((esdmode & 0xFFFF) << 16)
1648 | ((sdmode & 0xFFFF) << 0)
1649 );
1650
1651 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
York Sune1fd16b2011-01-10 12:03:00 +00001652
1653 if (unq_mrs_en) { /* unique mode registers are supported */
Kumar Galadea7f882011-11-09 10:05:10 -06001654 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Sune1fd16b2011-01-10 12:03:00 +00001655 if (popts->rtt_override)
1656 rtt = popts->rtt_override_value;
1657 else
1658 rtt = popts->cs_local_opts[i].odt_rtt_norm;
1659
1660 esdmode &= 0xFDBB; /* clear bit 9,6,2 */
1661 esdmode |= (0
1662 | ((rtt & 0x4) << 7) /* rtt field is split */
1663 | ((rtt & 0x2) << 5) /* rtt field is split */
1664 | ((rtt & 0x1) << 2) /* rtt field is split */
1665 );
1666 switch (i) {
1667 case 1:
1668 ddr->ddr_sdram_mode_3 = (0
1669 | ((esdmode & 0xFFFF) << 16)
1670 | ((sdmode & 0xFFFF) << 0)
1671 );
1672 break;
1673 case 2:
1674 ddr->ddr_sdram_mode_5 = (0
1675 | ((esdmode & 0xFFFF) << 16)
1676 | ((sdmode & 0xFFFF) << 0)
1677 );
1678 break;
1679 case 3:
1680 ddr->ddr_sdram_mode_7 = (0
1681 | ((esdmode & 0xFFFF) << 16)
1682 | ((sdmode & 0xFFFF) << 0)
1683 );
1684 break;
1685 }
1686 }
1687 debug("FSLDDR: ddr_sdram_mode_3 = 0x%08x\n",
1688 ddr->ddr_sdram_mode_3);
1689 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1690 ddr->ddr_sdram_mode_5);
1691 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1692 ddr->ddr_sdram_mode_5);
1693 }
Dave Liuc360cea2009-03-14 12:48:30 +08001694}
1695
York Sun5614e712013-09-30 09:22:09 -07001696#else /* !CONFIG_SYS_FSL_DDR3 */
Dave Liuc360cea2009-03-14 12:48:30 +08001697
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001698/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
York Sun03e664d2015-01-06 13:18:50 -08001699static void set_ddr_sdram_mode(const unsigned int ctrl_num,
1700 fsl_ddr_cfg_regs_t *ddr,
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001701 const memctl_options_t *popts,
1702 const common_timing_params_t *common_dimm,
1703 unsigned int cas_latency,
York Sune1fd16b2011-01-10 12:03:00 +00001704 unsigned int additive_latency,
1705 const unsigned int unq_mrs_en)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001706{
1707 unsigned short esdmode; /* Extended SDRAM mode */
1708 unsigned short sdmode; /* SDRAM mode */
1709
1710 /*
1711 * FIXME: This ought to be pre-calculated in a
1712 * technology-specific routine,
1713 * e.g. compute_DDR2_mode_register(), and then the
1714 * sdmode and esdmode passed in as part of common_dimm.
1715 */
1716
1717 /* Extended Mode Register */
1718 unsigned int mrs = 0; /* Mode Register Set */
1719 unsigned int outputs = 0; /* 0=Enabled, 1=Disabled */
1720 unsigned int rdqs_en = 0; /* RDQS Enable: 0=no, 1=yes */
1721 unsigned int dqs_en = 0; /* DQS# Enable: 0=enable, 1=disable */
1722 unsigned int ocd = 0; /* 0x0=OCD not supported,
1723 0x7=OCD default state */
1724 unsigned int rtt;
1725 unsigned int al; /* Posted CAS# additive latency (AL) */
1726 unsigned int ods = 0; /* Output Drive Strength:
1727 0 = Full strength (18ohm)
1728 1 = Reduced strength (4ohm) */
1729 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
1730 1=Disable (Test/Debug) */
1731
1732 /* Mode Register (MR) */
1733 unsigned int mr; /* Mode Register Definition */
1734 unsigned int pd; /* Power-Down Mode */
1735 unsigned int wr; /* Write Recovery */
1736 unsigned int dll_res; /* DLL Reset */
1737 unsigned int mode; /* Normal=0 or Test=1 */
Kumar Gala302e52e2008-09-05 14:40:29 -05001738 unsigned int caslat = 0;/* CAS# latency */
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001739 /* BT: Burst Type (0=Sequential, 1=Interleaved) */
1740 unsigned int bt;
1741 unsigned int bl; /* BL: Burst Length */
1742
Priyanka Jain0dd38a32013-09-25 10:41:19 +05301743 dqs_en = !popts->dqs_config;
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001744 rtt = fsl_ddr_get_rtt();
1745
1746 al = additive_latency;
1747
1748 esdmode = (0
1749 | ((mrs & 0x3) << 14)
1750 | ((outputs & 0x1) << 12)
1751 | ((rdqs_en & 0x1) << 11)
1752 | ((dqs_en & 0x1) << 10)
1753 | ((ocd & 0x7) << 7)
1754 | ((rtt & 0x2) << 5) /* rtt field is split */
1755 | ((al & 0x7) << 3)
1756 | ((rtt & 0x1) << 2) /* rtt field is split */
1757 | ((ods & 0x1) << 1)
1758 | ((dll_en & 0x1) << 0)
1759 );
1760
1761 mr = 0; /* FIXME: CHECKME */
1762
1763 /*
1764 * 0 = Fast Exit (Normal)
1765 * 1 = Slow Exit (Low Power)
1766 */
1767 pd = 0;
1768
York Sun5614e712013-09-30 09:22:09 -07001769#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001770 wr = 0; /* Historical */
York Sun5614e712013-09-30 09:22:09 -07001771#elif defined(CONFIG_SYS_FSL_DDR2)
York Sun03e664d2015-01-06 13:18:50 -08001772 wr = picos_to_mclk(ctrl_num, common_dimm->twr_ps);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001773#endif
1774 dll_res = 0;
1775 mode = 0;
1776
York Sun5614e712013-09-30 09:22:09 -07001777#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001778 if (1 <= cas_latency && cas_latency <= 4) {
1779 unsigned char mode_caslat_table[4] = {
1780 0x5, /* 1.5 clocks */
1781 0x2, /* 2.0 clocks */
1782 0x6, /* 2.5 clocks */
1783 0x3 /* 3.0 clocks */
1784 };
Kumar Gala302e52e2008-09-05 14:40:29 -05001785 caslat = mode_caslat_table[cas_latency - 1];
1786 } else {
1787 printf("Warning: unknown cas_latency %d\n", cas_latency);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001788 }
York Sun5614e712013-09-30 09:22:09 -07001789#elif defined(CONFIG_SYS_FSL_DDR2)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001790 caslat = cas_latency;
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001791#endif
1792 bt = 0;
1793
1794 switch (popts->burst_length) {
Dave Liuc360cea2009-03-14 12:48:30 +08001795 case DDR_BL4:
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001796 bl = 2;
1797 break;
Dave Liuc360cea2009-03-14 12:48:30 +08001798 case DDR_BL8:
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001799 bl = 3;
1800 break;
1801 default:
1802 printf("Error: invalid burst length of %u specified. "
1803 " Defaulting to 4 beats.\n",
1804 popts->burst_length);
1805 bl = 2;
1806 break;
1807 }
1808
1809 sdmode = (0
1810 | ((mr & 0x3) << 14)
1811 | ((pd & 0x1) << 12)
1812 | ((wr & 0x7) << 9)
1813 | ((dll_res & 0x1) << 8)
1814 | ((mode & 0x1) << 7)
1815 | ((caslat & 0x7) << 4)
1816 | ((bt & 0x1) << 3)
1817 | ((bl & 0x7) << 0)
1818 );
1819
1820 ddr->ddr_sdram_mode = (0
1821 | ((esdmode & 0xFFFF) << 16)
1822 | ((sdmode & 0xFFFF) << 0)
1823 );
Haiying Wang1f293b42008-10-03 12:37:26 -04001824 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001825}
Dave Liuc360cea2009-03-14 12:48:30 +08001826#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001827
1828/* DDR SDRAM Data Initialization (DDR_DATA_INIT) */
1829static void set_ddr_data_init(fsl_ddr_cfg_regs_t *ddr)
1830{
1831 unsigned int init_value; /* Initialization value */
1832
Anatolij Gustschin5b933942013-01-21 23:50:27 +00001833#ifdef CONFIG_MEM_INIT_VALUE
1834 init_value = CONFIG_MEM_INIT_VALUE;
1835#else
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001836 init_value = 0xDEADBEEF;
Anatolij Gustschin5b933942013-01-21 23:50:27 +00001837#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001838 ddr->ddr_data_init = init_value;
1839}
1840
1841/*
1842 * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL)
1843 * The old controller on the 8540/60 doesn't have this register.
1844 * Hope it's OK to set it (to 0) anyway.
1845 */
1846static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
1847 const memctl_options_t *popts)
1848{
1849 unsigned int clk_adjust; /* Clock adjust */
Curt Bruned7c865b2015-02-13 10:57:11 -08001850 unsigned int ss_en = 0; /* Source synchronous enable */
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001851
York Sun3c3d8ab2016-11-16 11:23:23 -08001852#if defined(CONFIG_ARCH_MPC8541) || defined(CONFIG_ARCH_MPC8555)
Curt Bruned7c865b2015-02-13 10:57:11 -08001853 /* Per FSL Application Note: AN2805 */
1854 ss_en = 1;
1855#endif
Shengzhou Liud8e51632016-05-04 10:20:21 +08001856 if (fsl_ddr_get_version(0) >= 0x40701) {
1857 /* clk_adjust in 5-bits on T-series and LS-series */
1858 clk_adjust = (popts->clk_adjust & 0x1F) << 22;
1859 } else {
1860 /* clk_adjust in 4-bits on earlier MPC85xx and P-series */
1861 clk_adjust = (popts->clk_adjust & 0xF) << 23;
1862 }
1863
Curt Bruned7c865b2015-02-13 10:57:11 -08001864 ddr->ddr_sdram_clk_cntl = (0
1865 | ((ss_en & 0x1) << 31)
Shengzhou Liud8e51632016-05-04 10:20:21 +08001866 | clk_adjust
Curt Bruned7c865b2015-02-13 10:57:11 -08001867 );
york9490ff42010-07-02 22:25:55 +00001868 debug("FSLDDR: clk_cntl = 0x%08x\n", ddr->ddr_sdram_clk_cntl);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001869}
1870
1871/* DDR Initialization Address (DDR_INIT_ADDR) */
1872static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
1873{
1874 unsigned int init_addr = 0; /* Initialization address */
1875
1876 ddr->ddr_init_addr = init_addr;
1877}
1878
1879/* DDR Initialization Address (DDR_INIT_EXT_ADDR) */
1880static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
1881{
1882 unsigned int uia = 0; /* Use initialization address */
1883 unsigned int init_ext_addr = 0; /* Initialization address */
1884
1885 ddr->ddr_init_ext_addr = (0
1886 | ((uia & 0x1) << 31)
1887 | (init_ext_addr & 0xF)
1888 );
1889}
1890
1891/* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */
Dave Liuec145e82010-03-05 12:22:00 +08001892static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr,
1893 const memctl_options_t *popts)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001894{
1895 unsigned int rwt = 0; /* Read-to-write turnaround for same CS */
1896 unsigned int wrt = 0; /* Write-to-read turnaround for same CS */
1897 unsigned int rrt = 0; /* Read-to-read turnaround for same CS */
1898 unsigned int wwt = 0; /* Write-to-write turnaround for same CS */
York Sun6c6e0062015-11-04 10:03:21 -08001899 unsigned int trwt_mclk = 0; /* ext_rwt */
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001900 unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */
1901
York Sun34e026f2014-03-27 17:54:47 -07001902#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
Dave Liuec145e82010-03-05 12:22:00 +08001903 if (popts->burst_length == DDR_BL8) {
1904 /* We set BL/2 for fixed BL8 */
1905 rrt = 0; /* BL/2 clocks */
1906 wwt = 0; /* BL/2 clocks */
1907 } else {
1908 /* We need to set BL/2 + 2 to BC4 and OTF */
1909 rrt = 2; /* BL/2 + 2 clocks */
1910 wwt = 2; /* BL/2 + 2 clocks */
1911 }
York Sun34e026f2014-03-27 17:54:47 -07001912#endif
York Sun34e026f2014-03-27 17:54:47 -07001913#ifdef CONFIG_SYS_FSL_DDR4
1914 dll_lock = 2; /* tDLLK = 1024 clocks */
1915#elif defined(CONFIG_SYS_FSL_DDR3)
Dave Liuc360cea2009-03-14 12:48:30 +08001916 dll_lock = 1; /* tDLLK = 512 clocks from spec */
1917#endif
York Sun6c6e0062015-11-04 10:03:21 -08001918
1919 if (popts->trwt_override)
1920 trwt_mclk = popts->trwt;
1921
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001922 ddr->timing_cfg_4 = (0
1923 | ((rwt & 0xf) << 28)
1924 | ((wrt & 0xf) << 24)
1925 | ((rrt & 0xf) << 20)
1926 | ((wwt & 0xf) << 16)
York Sun6c6e0062015-11-04 10:03:21 -08001927 | ((trwt_mclk & 0xc) << 12)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001928 | (dll_lock & 0x3)
1929 );
Haiying Wang1f293b42008-10-03 12:37:26 -04001930 debug("FSLDDR: timing_cfg_4 = 0x%08x\n", ddr->timing_cfg_4);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001931}
1932
1933/* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */
York Sune1fd16b2011-01-10 12:03:00 +00001934static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr, unsigned int cas_latency)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001935{
1936 unsigned int rodt_on = 0; /* Read to ODT on */
1937 unsigned int rodt_off = 0; /* Read to ODT off */
1938 unsigned int wodt_on = 0; /* Write to ODT on */
1939 unsigned int wodt_off = 0; /* Write to ODT off */
1940
York Sun34e026f2014-03-27 17:54:47 -07001941#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
1942 unsigned int wr_lat = ((ddr->timing_cfg_2 & 0x00780000) >> 19) +
1943 ((ddr->timing_cfg_2 & 0x00040000) >> 14);
York Sune1fd16b2011-01-10 12:03:00 +00001944 /* rodt_on = timing_cfg_1[caslat] - timing_cfg_2[wrlat] + 1 */
York Sun34e026f2014-03-27 17:54:47 -07001945 if (cas_latency >= wr_lat)
1946 rodt_on = cas_latency - wr_lat + 1;
Dave Liuc360cea2009-03-14 12:48:30 +08001947 rodt_off = 4; /* 4 clocks */
york5fb8a8a2010-07-02 22:25:56 +00001948 wodt_on = 1; /* 1 clocks */
Dave Liuc360cea2009-03-14 12:48:30 +08001949 wodt_off = 4; /* 4 clocks */
1950#endif
1951
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001952 ddr->timing_cfg_5 = (0
Dave Liu22ff3d02008-11-21 16:31:29 +08001953 | ((rodt_on & 0x1f) << 24)
1954 | ((rodt_off & 0x7) << 20)
1955 | ((wodt_on & 0x1f) << 12)
1956 | ((wodt_off & 0x7) << 8)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001957 );
Haiying Wang1f293b42008-10-03 12:37:26 -04001958 debug("FSLDDR: timing_cfg_5 = 0x%08x\n", ddr->timing_cfg_5);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05001959}
1960
York Sun34e026f2014-03-27 17:54:47 -07001961#ifdef CONFIG_SYS_FSL_DDR4
1962static void set_timing_cfg_6(fsl_ddr_cfg_regs_t *ddr)
1963{
1964 unsigned int hs_caslat = 0;
1965 unsigned int hs_wrlat = 0;
1966 unsigned int hs_wrrec = 0;
1967 unsigned int hs_clkadj = 0;
1968 unsigned int hs_wrlvl_start = 0;
1969
1970 ddr->timing_cfg_6 = (0
1971 | ((hs_caslat & 0x1f) << 24)
1972 | ((hs_wrlat & 0x1f) << 19)
1973 | ((hs_wrrec & 0x1f) << 12)
1974 | ((hs_clkadj & 0x1f) << 6)
1975 | ((hs_wrlvl_start & 0x1f) << 0)
1976 );
1977 debug("FSLDDR: timing_cfg_6 = 0x%08x\n", ddr->timing_cfg_6);
1978}
1979
York Sun03e664d2015-01-06 13:18:50 -08001980static void set_timing_cfg_7(const unsigned int ctrl_num,
1981 fsl_ddr_cfg_regs_t *ddr,
York Sun426230a2018-01-29 09:44:33 -08001982 const memctl_options_t *popts,
York Sun03e664d2015-01-06 13:18:50 -08001983 const common_timing_params_t *common_dimm)
York Sun34e026f2014-03-27 17:54:47 -07001984{
1985 unsigned int txpr, tcksre, tcksrx;
Shengzhou Liueb118802016-03-10 17:36:56 +08001986 unsigned int cke_rst, cksre, cksrx, par_lat = 0, cs_to_cmd;
1987 const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
York Sun34e026f2014-03-27 17:54:47 -07001988
York Sun03e664d2015-01-06 13:18:50 -08001989 txpr = max(5U, picos_to_mclk(ctrl_num, common_dimm->trfc1_ps + 10000));
1990 tcksre = max(5U, picos_to_mclk(ctrl_num, 10000));
1991 tcksrx = max(5U, picos_to_mclk(ctrl_num, 10000));
Shengzhou Liueb118802016-03-10 17:36:56 +08001992
York Sun426230a2018-01-29 09:44:33 -08001993 if (ddr->ddr_sdram_cfg_2 & SDRAM_CFG2_AP_EN &&
1994 CONFIG_FSL_SDRAM_TYPE == SDRAM_TYPE_DDR4) {
1995 /* for DDR4 only */
York Sunc0c32af2018-01-29 09:44:35 -08001996 par_lat = (ddr->ddr_sdram_rcw_2 & 0xf) + 1;
York Sun426230a2018-01-29 09:44:33 -08001997 debug("PAR_LAT = %u for mclk_ps = %d\n", par_lat, mclk_ps);
Shengzhou Liueb118802016-03-10 17:36:56 +08001998 }
1999
York Sun34e026f2014-03-27 17:54:47 -07002000 cs_to_cmd = 0;
2001
2002 if (txpr <= 200)
2003 cke_rst = 0;
2004 else if (txpr <= 256)
2005 cke_rst = 1;
2006 else if (txpr <= 512)
2007 cke_rst = 2;
2008 else
2009 cke_rst = 3;
2010
2011 if (tcksre <= 19)
2012 cksre = tcksre - 5;
2013 else
2014 cksre = 15;
2015
2016 if (tcksrx <= 19)
2017 cksrx = tcksrx - 5;
2018 else
2019 cksrx = 15;
2020
2021 ddr->timing_cfg_7 = (0
2022 | ((cke_rst & 0x3) << 28)
2023 | ((cksre & 0xf) << 24)
2024 | ((cksrx & 0xf) << 20)
2025 | ((par_lat & 0xf) << 16)
2026 | ((cs_to_cmd & 0xf) << 4)
2027 );
2028 debug("FSLDDR: timing_cfg_7 = 0x%08x\n", ddr->timing_cfg_7);
2029}
2030
York Sun03e664d2015-01-06 13:18:50 -08002031static void set_timing_cfg_8(const unsigned int ctrl_num,
2032 fsl_ddr_cfg_regs_t *ddr,
York Sun34e026f2014-03-27 17:54:47 -07002033 const memctl_options_t *popts,
2034 const common_timing_params_t *common_dimm,
2035 unsigned int cas_latency)
2036{
York Sun426230a2018-01-29 09:44:33 -08002037 int rwt_bg, wrt_bg, rrt_bg, wwt_bg;
York Sun34e026f2014-03-27 17:54:47 -07002038 unsigned int acttoact_bg, wrtord_bg, pre_all_rec;
York Sun426230a2018-01-29 09:44:33 -08002039 int tccdl = picos_to_mclk(ctrl_num, common_dimm->tccdl_ps);
2040 int wr_lat = ((ddr->timing_cfg_2 & 0x00780000) >> 19) +
2041 ((ddr->timing_cfg_2 & 0x00040000) >> 14);
York Sun34e026f2014-03-27 17:54:47 -07002042
2043 rwt_bg = cas_latency + 2 + 4 - wr_lat;
2044 if (rwt_bg < tccdl)
2045 rwt_bg = tccdl - rwt_bg;
2046 else
2047 rwt_bg = 0;
2048
2049 wrt_bg = wr_lat + 4 + 1 - cas_latency;
2050 if (wrt_bg < tccdl)
2051 wrt_bg = tccdl - wrt_bg;
2052 else
2053 wrt_bg = 0;
2054
2055 if (popts->burst_length == DDR_BL8) {
2056 rrt_bg = tccdl - 4;
2057 wwt_bg = tccdl - 4;
2058 } else {
2059 rrt_bg = tccdl - 2;
York Sundc1437a2015-01-06 13:18:52 -08002060 wwt_bg = tccdl - 2;
York Sun34e026f2014-03-27 17:54:47 -07002061 }
2062
York Sun03e664d2015-01-06 13:18:50 -08002063 acttoact_bg = picos_to_mclk(ctrl_num, common_dimm->trrdl_ps);
2064 wrtord_bg = max(4U, picos_to_mclk(ctrl_num, 7500));
York Sun3d75ec92014-06-26 11:14:44 -07002065 if (popts->otf_burst_chop_en)
2066 wrtord_bg += 2;
2067
York Sun34e026f2014-03-27 17:54:47 -07002068 pre_all_rec = 0;
2069
2070 ddr->timing_cfg_8 = (0
2071 | ((rwt_bg & 0xf) << 28)
2072 | ((wrt_bg & 0xf) << 24)
2073 | ((rrt_bg & 0xf) << 20)
2074 | ((wwt_bg & 0xf) << 16)
2075 | ((acttoact_bg & 0xf) << 12)
2076 | ((wrtord_bg & 0xf) << 8)
2077 | ((pre_all_rec & 0x1f) << 0)
2078 );
2079
2080 debug("FSLDDR: timing_cfg_8 = 0x%08x\n", ddr->timing_cfg_8);
2081}
2082
York Sunc0c32af2018-01-29 09:44:35 -08002083static void set_timing_cfg_9(const unsigned int ctrl_num,
2084 fsl_ddr_cfg_regs_t *ddr,
2085 const memctl_options_t *popts,
2086 const common_timing_params_t *common_dimm)
York Sun34e026f2014-03-27 17:54:47 -07002087{
York Sunc0c32af2018-01-29 09:44:35 -08002088 unsigned int refrec_cid_mclk = 0;
2089 unsigned int acttoact_cid_mclk = 0;
2090
2091 if (popts->package_3ds) {
2092 refrec_cid_mclk =
2093 picos_to_mclk(ctrl_num, common_dimm->trfc_slr_ps);
2094 acttoact_cid_mclk = 4U; /* tRRDS_slr */
2095 }
2096
2097 ddr->timing_cfg_9 = (refrec_cid_mclk & 0x3ff) << 16 |
2098 (acttoact_cid_mclk & 0xf) << 8;
2099
York Sun34e026f2014-03-27 17:54:47 -07002100 debug("FSLDDR: timing_cfg_9 = 0x%08x\n", ddr->timing_cfg_9);
2101}
2102
York Sunf80d6472014-09-11 13:32:06 -07002103/* This function needs to be called after set_ddr_sdram_cfg() is called */
York Sun34e026f2014-03-27 17:54:47 -07002104static void set_ddr_dq_mapping(fsl_ddr_cfg_regs_t *ddr,
2105 const dimm_params_t *dimm_params)
2106{
York Sunf80d6472014-09-11 13:32:06 -07002107 unsigned int acc_ecc_en = (ddr->ddr_sdram_cfg >> 2) & 0x1;
York Sun6b95be22015-03-19 09:30:27 -07002108 int i;
York Sunf80d6472014-09-11 13:32:06 -07002109
York Sun6b95be22015-03-19 09:30:27 -07002110 for (i = 0; i < CONFIG_DIMM_SLOTS_PER_CTLR; i++) {
2111 if (dimm_params[i].n_ranks)
2112 break;
2113 }
2114 if (i >= CONFIG_DIMM_SLOTS_PER_CTLR) {
2115 puts("DDR error: no DIMM found!\n");
2116 return;
2117 }
York Sun34e026f2014-03-27 17:54:47 -07002118
York Sun6b95be22015-03-19 09:30:27 -07002119 ddr->dq_map_0 = ((dimm_params[i].dq_mapping[0] & 0x3F) << 26) |
2120 ((dimm_params[i].dq_mapping[1] & 0x3F) << 20) |
2121 ((dimm_params[i].dq_mapping[2] & 0x3F) << 14) |
2122 ((dimm_params[i].dq_mapping[3] & 0x3F) << 8) |
2123 ((dimm_params[i].dq_mapping[4] & 0x3F) << 2);
York Sun34e026f2014-03-27 17:54:47 -07002124
York Sun6b95be22015-03-19 09:30:27 -07002125 ddr->dq_map_1 = ((dimm_params[i].dq_mapping[5] & 0x3F) << 26) |
2126 ((dimm_params[i].dq_mapping[6] & 0x3F) << 20) |
2127 ((dimm_params[i].dq_mapping[7] & 0x3F) << 14) |
2128 ((dimm_params[i].dq_mapping[10] & 0x3F) << 8) |
2129 ((dimm_params[i].dq_mapping[11] & 0x3F) << 2);
2130
2131 ddr->dq_map_2 = ((dimm_params[i].dq_mapping[12] & 0x3F) << 26) |
2132 ((dimm_params[i].dq_mapping[13] & 0x3F) << 20) |
2133 ((dimm_params[i].dq_mapping[14] & 0x3F) << 14) |
2134 ((dimm_params[i].dq_mapping[15] & 0x3F) << 8) |
2135 ((dimm_params[i].dq_mapping[16] & 0x3F) << 2);
York Sun34e026f2014-03-27 17:54:47 -07002136
York Sunf80d6472014-09-11 13:32:06 -07002137 /* dq_map for ECC[4:7] is set to 0 if accumulated ECC is enabled */
York Sun6b95be22015-03-19 09:30:27 -07002138 ddr->dq_map_3 = ((dimm_params[i].dq_mapping[17] & 0x3F) << 26) |
2139 ((dimm_params[i].dq_mapping[8] & 0x3F) << 20) |
York Sunf80d6472014-09-11 13:32:06 -07002140 (acc_ecc_en ? 0 :
York Sun6b95be22015-03-19 09:30:27 -07002141 (dimm_params[i].dq_mapping[9] & 0x3F) << 14) |
2142 dimm_params[i].dq_mapping_ors;
York Sun34e026f2014-03-27 17:54:47 -07002143
2144 debug("FSLDDR: dq_map_0 = 0x%08x\n", ddr->dq_map_0);
2145 debug("FSLDDR: dq_map_1 = 0x%08x\n", ddr->dq_map_1);
2146 debug("FSLDDR: dq_map_2 = 0x%08x\n", ddr->dq_map_2);
2147 debug("FSLDDR: dq_map_3 = 0x%08x\n", ddr->dq_map_3);
2148}
2149static void set_ddr_sdram_cfg_3(fsl_ddr_cfg_regs_t *ddr,
2150 const memctl_options_t *popts)
2151{
2152 int rd_pre;
2153
2154 rd_pre = popts->quad_rank_present ? 1 : 0;
2155
2156 ddr->ddr_sdram_cfg_3 = (rd_pre & 0x1) << 16;
York Sun426230a2018-01-29 09:44:33 -08002157 /* Disable MRS on parity error for RDIMMs */
2158 ddr->ddr_sdram_cfg_3 |= popts->registered_dimm_en ? 1 : 0;
York Sun34e026f2014-03-27 17:54:47 -07002159
York Sunc0c32af2018-01-29 09:44:35 -08002160 if (popts->package_3ds) { /* only 2,4,8 are supported */
2161 if ((popts->package_3ds + 1) & 0x1) {
2162 printf("Error: Unsupported 3DS DIMM with %d die\n",
2163 popts->package_3ds + 1);
2164 } else {
2165 ddr->ddr_sdram_cfg_3 |= ((popts->package_3ds + 1) >> 1)
2166 << 4;
2167 }
2168 }
2169
York Sun34e026f2014-03-27 17:54:47 -07002170 debug("FSLDDR: ddr_sdram_cfg_3 = 0x%08x\n", ddr->ddr_sdram_cfg_3);
2171}
2172#endif /* CONFIG_SYS_FSL_DDR4 */
2173
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002174/* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */
Dave Liuc360cea2009-03-14 12:48:30 +08002175static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int zq_en)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002176{
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002177 unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */
2178 /* Normal Operation Full Calibration Time (tZQoper) */
2179 unsigned int zqoper = 0;
2180 /* Normal Operation Short Calibration Time (tZQCS) */
2181 unsigned int zqcs = 0;
York Sun34e026f2014-03-27 17:54:47 -07002182#ifdef CONFIG_SYS_FSL_DDR4
2183 unsigned int zqcs_init;
2184#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002185
Dave Liuc360cea2009-03-14 12:48:30 +08002186 if (zq_en) {
York Sun34e026f2014-03-27 17:54:47 -07002187#ifdef CONFIG_SYS_FSL_DDR4
2188 zqinit = 10; /* 1024 clocks */
2189 zqoper = 9; /* 512 clocks */
2190 zqcs = 7; /* 128 clocks */
2191 zqcs_init = 5; /* 1024 refresh sequences */
2192#else
Dave Liuc360cea2009-03-14 12:48:30 +08002193 zqinit = 9; /* 512 clocks */
2194 zqoper = 8; /* 256 clocks */
2195 zqcs = 6; /* 64 clocks */
York Sun34e026f2014-03-27 17:54:47 -07002196#endif
Dave Liuc360cea2009-03-14 12:48:30 +08002197 }
2198
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002199 ddr->ddr_zq_cntl = (0
2200 | ((zq_en & 0x1) << 31)
2201 | ((zqinit & 0xF) << 24)
2202 | ((zqoper & 0xF) << 16)
2203 | ((zqcs & 0xF) << 8)
York Sun34e026f2014-03-27 17:54:47 -07002204#ifdef CONFIG_SYS_FSL_DDR4
2205 | ((zqcs_init & 0xF) << 0)
2206#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002207 );
York Sune1fd16b2011-01-10 12:03:00 +00002208 debug("FSLDDR: zq_cntl = 0x%08x\n", ddr->ddr_zq_cntl);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002209}
2210
2211/* DDR Write Leveling Control (DDR_WRLVL_CNTL) */
Dave Liubdc9f7b2009-12-16 10:24:37 -06002212static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int wrlvl_en,
2213 const memctl_options_t *popts)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002214{
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002215 /*
2216 * First DQS pulse rising edge after margining mode
2217 * is programmed (tWL_MRD)
2218 */
2219 unsigned int wrlvl_mrd = 0;
2220 /* ODT delay after margining mode is programmed (tWL_ODTEN) */
2221 unsigned int wrlvl_odten = 0;
2222 /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */
2223 unsigned int wrlvl_dqsen = 0;
2224 /* WRLVL_SMPL: Write leveling sample time */
2225 unsigned int wrlvl_smpl = 0;
2226 /* WRLVL_WLR: Write leveling repeition time */
2227 unsigned int wrlvl_wlr = 0;
2228 /* WRLVL_START: Write leveling start time */
2229 unsigned int wrlvl_start = 0;
2230
Dave Liuc360cea2009-03-14 12:48:30 +08002231 /* suggest enable write leveling for DDR3 due to fly-by topology */
2232 if (wrlvl_en) {
2233 /* tWL_MRD min = 40 nCK, we set it 64 */
2234 wrlvl_mrd = 0x6;
2235 /* tWL_ODTEN 128 */
2236 wrlvl_odten = 0x7;
2237 /* tWL_DQSEN min = 25 nCK, we set it 32 */
2238 wrlvl_dqsen = 0x5;
2239 /*
Dave Liubdc9f7b2009-12-16 10:24:37 -06002240 * Write leveling sample time at least need 6 clocks
2241 * higher than tWLO to allow enough time for progagation
2242 * delay and sampling the prime data bits.
Dave Liuc360cea2009-03-14 12:48:30 +08002243 */
2244 wrlvl_smpl = 0xf;
2245 /*
2246 * Write leveling repetition time
2247 * at least tWLO + 6 clocks clocks
york5fb8a8a2010-07-02 22:25:56 +00002248 * we set it 64
Dave Liuc360cea2009-03-14 12:48:30 +08002249 */
york5fb8a8a2010-07-02 22:25:56 +00002250 wrlvl_wlr = 0x6;
Dave Liuc360cea2009-03-14 12:48:30 +08002251 /*
2252 * Write leveling start time
2253 * The value use for the DQS_ADJUST for the first sample
York Sune1fd16b2011-01-10 12:03:00 +00002254 * when write leveling is enabled. It probably needs to be
Robert P. J. Day62a3b7d2016-07-15 13:44:45 -04002255 * overridden per platform.
Dave Liuc360cea2009-03-14 12:48:30 +08002256 */
2257 wrlvl_start = 0x8;
Dave Liubdc9f7b2009-12-16 10:24:37 -06002258 /*
2259 * Override the write leveling sample and start time
2260 * according to specific board
2261 */
2262 if (popts->wrlvl_override) {
2263 wrlvl_smpl = popts->wrlvl_sample;
2264 wrlvl_start = popts->wrlvl_start;
2265 }
Dave Liuc360cea2009-03-14 12:48:30 +08002266 }
2267
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002268 ddr->ddr_wrlvl_cntl = (0
2269 | ((wrlvl_en & 0x1) << 31)
2270 | ((wrlvl_mrd & 0x7) << 24)
2271 | ((wrlvl_odten & 0x7) << 20)
2272 | ((wrlvl_dqsen & 0x7) << 16)
2273 | ((wrlvl_smpl & 0xf) << 12)
2274 | ((wrlvl_wlr & 0x7) << 8)
Dave Liu22ff3d02008-11-21 16:31:29 +08002275 | ((wrlvl_start & 0x1F) << 0)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002276 );
York Sune1fd16b2011-01-10 12:03:00 +00002277 debug("FSLDDR: wrlvl_cntl = 0x%08x\n", ddr->ddr_wrlvl_cntl);
York Sun57495e42012-10-08 07:44:22 +00002278 ddr->ddr_wrlvl_cntl_2 = popts->wrlvl_ctl_2;
2279 debug("FSLDDR: wrlvl_cntl_2 = 0x%08x\n", ddr->ddr_wrlvl_cntl_2);
2280 ddr->ddr_wrlvl_cntl_3 = popts->wrlvl_ctl_3;
2281 debug("FSLDDR: wrlvl_cntl_3 = 0x%08x\n", ddr->ddr_wrlvl_cntl_3);
2282
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002283}
2284
2285/* DDR Self Refresh Counter (DDR_SR_CNTR) */
Dave Liu22cca7e2008-11-21 16:31:35 +08002286static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr, unsigned int sr_it)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002287{
Dave Liu22cca7e2008-11-21 16:31:35 +08002288 /* Self Refresh Idle Threshold */
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002289 ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
2290}
2291
york7fd101c2010-07-02 22:25:54 +00002292static void set_ddr_eor(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
2293{
2294 if (popts->addr_hash) {
2295 ddr->ddr_eor = 0x40000000; /* address hash enable */
Kumar Galac2a63f42011-03-18 11:53:06 -05002296 puts("Address hashing enabled.\n");
york7fd101c2010-07-02 22:25:54 +00002297 }
2298}
2299
York Sune1fd16b2011-01-10 12:03:00 +00002300static void set_ddr_cdr1(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
2301{
2302 ddr->ddr_cdr1 = popts->ddr_cdr1;
2303 debug("FSLDDR: ddr_cdr1 = 0x%08x\n", ddr->ddr_cdr1);
2304}
2305
York Sun57495e42012-10-08 07:44:22 +00002306static void set_ddr_cdr2(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
2307{
2308 ddr->ddr_cdr2 = popts->ddr_cdr2;
2309 debug("FSLDDR: ddr_cdr2 = 0x%08x\n", ddr->ddr_cdr2);
2310}
2311
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002312unsigned int
2313check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
2314{
2315 unsigned int res = 0;
2316
2317 /*
2318 * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are
2319 * not set at the same time.
2320 */
2321 if (ddr->ddr_sdram_cfg & 0x10000000
2322 && ddr->ddr_sdram_cfg & 0x00008000) {
2323 printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
2324 " should not be set at the same time.\n");
2325 res++;
2326 }
2327
2328 return res;
2329}
2330
2331unsigned int
York Sun03e664d2015-01-06 13:18:50 -08002332compute_fsl_memctl_config_regs(const unsigned int ctrl_num,
2333 const memctl_options_t *popts,
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002334 fsl_ddr_cfg_regs_t *ddr,
2335 const common_timing_params_t *common_dimm,
2336 const dimm_params_t *dimm_params,
Haiying Wangfc0c2b62010-12-01 10:35:31 -05002337 unsigned int dbw_cap_adj,
2338 unsigned int size_only)
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002339{
2340 unsigned int i;
2341 unsigned int cas_latency;
2342 unsigned int additive_latency;
Dave Liu22cca7e2008-11-21 16:31:35 +08002343 unsigned int sr_it;
Dave Liuc360cea2009-03-14 12:48:30 +08002344 unsigned int zq_en;
2345 unsigned int wrlvl_en;
York Sune1fd16b2011-01-10 12:03:00 +00002346 unsigned int ip_rev = 0;
2347 unsigned int unq_mrs_en = 0;
York Sun58edbc92010-10-18 13:46:50 -07002348 int cs_en = 1;
Shengzhou Liu02fb2762016-11-21 11:36:48 +08002349#ifdef CONFIG_SYS_FSL_ERRATUM_A009942
2350 unsigned int ddr_freq;
2351#endif
2352#if (defined(CONFIG_SYS_FSL_ERRATUM_A008378) && \
2353 defined(CONFIG_SYS_FSL_DDRC_GEN4)) || \
2354 defined(CONFIG_SYS_FSL_ERRATUM_A009942)
2355 struct ccsr_ddr __iomem *ddrc;
2356
2357 switch (ctrl_num) {
2358 case 0:
2359 ddrc = (void *)CONFIG_SYS_FSL_DDR_ADDR;
2360 break;
York Sun51370d52016-12-28 08:43:45 -08002361#if defined(CONFIG_SYS_FSL_DDR2_ADDR) && (CONFIG_SYS_NUM_DDR_CTLRS > 1)
Shengzhou Liu02fb2762016-11-21 11:36:48 +08002362 case 1:
2363 ddrc = (void *)CONFIG_SYS_FSL_DDR2_ADDR;
2364 break;
2365#endif
York Sun51370d52016-12-28 08:43:45 -08002366#if defined(CONFIG_SYS_FSL_DDR3_ADDR) && (CONFIG_SYS_NUM_DDR_CTLRS > 2)
Shengzhou Liu02fb2762016-11-21 11:36:48 +08002367 case 2:
2368 ddrc = (void *)CONFIG_SYS_FSL_DDR3_ADDR;
2369 break;
2370#endif
York Sun51370d52016-12-28 08:43:45 -08002371#if defined(CONFIG_SYS_FSL_DDR4_ADDR) && (CONFIG_SYS_NUM_DDR_CTLRS > 3)
Shengzhou Liu02fb2762016-11-21 11:36:48 +08002372 case 3:
2373 ddrc = (void *)CONFIG_SYS_FSL_DDR4_ADDR;
2374 break;
2375#endif
2376 default:
2377 printf("%s unexpected ctrl_num = %u\n", __func__, ctrl_num);
2378 return 1;
2379 }
2380#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002381
2382 memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
2383
2384 if (common_dimm == NULL) {
2385 printf("Error: subset DIMM params struct null pointer\n");
2386 return 1;
2387 }
2388
2389 /*
2390 * Process overrides first.
2391 *
2392 * FIXME: somehow add dereated caslat to this
2393 */
2394 cas_latency = (popts->cas_latency_override)
2395 ? popts->cas_latency_override_value
York Sun34e026f2014-03-27 17:54:47 -07002396 : common_dimm->lowest_common_spd_caslat;
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002397
2398 additive_latency = (popts->additive_latency_override)
2399 ? popts->additive_latency_override_value
2400 : common_dimm->additive_latency;
2401
Dave Liu22cca7e2008-11-21 16:31:35 +08002402 sr_it = (popts->auto_self_refresh_en)
2403 ? popts->sr_it
2404 : 0;
Dave Liuc360cea2009-03-14 12:48:30 +08002405 /* ZQ calibration */
2406 zq_en = (popts->zq_en) ? 1 : 0;
2407 /* write leveling */
2408 wrlvl_en = (popts->wrlvl_en) ? 1 : 0;
Dave Liu22cca7e2008-11-21 16:31:35 +08002409
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002410 /* Chip Select Memory Bounds (CSn_BNDS) */
2411 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Suna4c66502012-08-17 08:22:39 +00002412 unsigned long long ea, sa;
york076bff82010-07-02 22:25:52 +00002413 unsigned int cs_per_dimm
2414 = CONFIG_CHIP_SELECTS_PER_CTRL / CONFIG_DIMM_SLOTS_PER_CTLR;
2415 unsigned int dimm_number
2416 = i / cs_per_dimm;
2417 unsigned long long rank_density
York Suna4c66502012-08-17 08:22:39 +00002418 = dimm_params[dimm_number].rank_density >> dbw_cap_adj;
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002419
york076bff82010-07-02 22:25:52 +00002420 if (dimm_params[dimm_number].n_ranks == 0) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002421 debug("Skipping setup of CS%u "
york5800e7a2010-07-02 22:25:53 +00002422 "because n_ranks on DIMM %u is 0\n", i, dimm_number);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002423 continue;
2424 }
York Suna4c66502012-08-17 08:22:39 +00002425 if (popts->memctl_interleaving) {
york076bff82010-07-02 22:25:52 +00002426 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
York Suna4c66502012-08-17 08:22:39 +00002427 case FSL_DDR_CS0_CS1_CS2_CS3:
2428 break;
york076bff82010-07-02 22:25:52 +00002429 case FSL_DDR_CS0_CS1:
2430 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
York Sun58edbc92010-10-18 13:46:50 -07002431 if (i > 1)
2432 cs_en = 0;
york076bff82010-07-02 22:25:52 +00002433 break;
2434 case FSL_DDR_CS2_CS3:
York Suna4c66502012-08-17 08:22:39 +00002435 default:
York Sun58edbc92010-10-18 13:46:50 -07002436 if (i > 0)
2437 cs_en = 0;
york076bff82010-07-02 22:25:52 +00002438 break;
york076bff82010-07-02 22:25:52 +00002439 }
York Suna4c66502012-08-17 08:22:39 +00002440 sa = common_dimm->base_address;
York Sun123922b2012-10-08 07:44:23 +00002441 ea = sa + common_dimm->total_mem - 1;
York Suna4c66502012-08-17 08:22:39 +00002442 } else if (!popts->memctl_interleaving) {
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002443 /*
2444 * If memory interleaving between controllers is NOT
2445 * enabled, the starting address for each memory
2446 * controller is distinct. However, because rank
2447 * interleaving is enabled, the starting and ending
2448 * addresses of the total memory on that memory
2449 * controller needs to be programmed into its
2450 * respective CS0_BNDS.
2451 */
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002452 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
2453 case FSL_DDR_CS0_CS1_CS2_CS3:
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002454 sa = common_dimm->base_address;
York Sun123922b2012-10-08 07:44:23 +00002455 ea = sa + common_dimm->total_mem - 1;
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002456 break;
2457 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
York Suna4c66502012-08-17 08:22:39 +00002458 if ((i >= 2) && (dimm_number == 0)) {
york076bff82010-07-02 22:25:52 +00002459 sa = dimm_params[dimm_number].base_address +
York Suna4c66502012-08-17 08:22:39 +00002460 2 * rank_density;
2461 ea = sa + 2 * rank_density - 1;
york076bff82010-07-02 22:25:52 +00002462 } else {
2463 sa = dimm_params[dimm_number].base_address;
York Suna4c66502012-08-17 08:22:39 +00002464 ea = sa + 2 * rank_density - 1;
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002465 }
2466 break;
2467 case FSL_DDR_CS0_CS1:
york076bff82010-07-02 22:25:52 +00002468 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
2469 sa = dimm_params[dimm_number].base_address;
York Suna4c66502012-08-17 08:22:39 +00002470 ea = sa + rank_density - 1;
2471 if (i != 1)
2472 sa += (i % cs_per_dimm) * rank_density;
2473 ea += (i % cs_per_dimm) * rank_density;
york076bff82010-07-02 22:25:52 +00002474 } else {
2475 sa = 0;
2476 ea = 0;
2477 }
2478 if (i == 0)
York Suna4c66502012-08-17 08:22:39 +00002479 ea += rank_density;
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002480 break;
2481 case FSL_DDR_CS2_CS3:
york076bff82010-07-02 22:25:52 +00002482 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
2483 sa = dimm_params[dimm_number].base_address;
York Suna4c66502012-08-17 08:22:39 +00002484 ea = sa + rank_density - 1;
2485 if (i != 3)
2486 sa += (i % cs_per_dimm) * rank_density;
2487 ea += (i % cs_per_dimm) * rank_density;
york076bff82010-07-02 22:25:52 +00002488 } else {
2489 sa = 0;
2490 ea = 0;
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002491 }
york076bff82010-07-02 22:25:52 +00002492 if (i == 2)
2493 ea += (rank_density >> dbw_cap_adj);
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002494 break;
2495 default: /* No bank(chip-select) interleaving */
York Suna4c66502012-08-17 08:22:39 +00002496 sa = dimm_params[dimm_number].base_address;
2497 ea = sa + rank_density - 1;
2498 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
2499 sa += (i % cs_per_dimm) * rank_density;
2500 ea += (i % cs_per_dimm) * rank_density;
2501 } else {
2502 sa = 0;
2503 ea = 0;
2504 }
Haiying Wangdbbbb3a2008-10-03 12:36:39 -04002505 break;
2506 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002507 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002508
2509 sa >>= 24;
2510 ea >>= 24;
2511
York Sun123922b2012-10-08 07:44:23 +00002512 if (cs_en) {
2513 ddr->cs[i].bnds = (0
York Sund4263b82013-06-03 12:39:06 -07002514 | ((sa & 0xffff) << 16) /* starting address */
2515 | ((ea & 0xffff) << 0) /* ending address */
York Sun123922b2012-10-08 07:44:23 +00002516 );
2517 } else {
York Sund8556db2013-06-25 11:37:45 -07002518 /* setting bnds to 0xffffffff for inactive CS */
2519 ddr->cs[i].bnds = 0xffffffff;
York Sun123922b2012-10-08 07:44:23 +00002520 }
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002521
Haiying Wang1f293b42008-10-03 12:37:26 -04002522 debug("FSLDDR: cs[%d]_bnds = 0x%08x\n", i, ddr->cs[i].bnds);
York Sun123922b2012-10-08 07:44:23 +00002523 set_csn_config(dimm_number, i, ddr, popts, dimm_params);
2524 set_csn_config_2(i, ddr);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002525 }
2526
Haiying Wangfc0c2b62010-12-01 10:35:31 -05002527 /*
2528 * In the case we only need to compute the ddr sdram size, we only need
2529 * to set csn registers, so return from here.
2530 */
2531 if (size_only)
2532 return 0;
2533
york7fd101c2010-07-02 22:25:54 +00002534 set_ddr_eor(ddr, popts);
2535
York Sun5614e712013-09-30 09:22:09 -07002536#if !defined(CONFIG_SYS_FSL_DDR1)
York Sun03e664d2015-01-06 13:18:50 -08002537 set_timing_cfg_0(ctrl_num, ddr, popts, dimm_params);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002538#endif
2539
York Sun03e664d2015-01-06 13:18:50 -08002540 set_timing_cfg_3(ctrl_num, ddr, popts, common_dimm, cas_latency,
York Sund4263b82013-06-03 12:39:06 -07002541 additive_latency);
York Sun03e664d2015-01-06 13:18:50 -08002542 set_timing_cfg_1(ctrl_num, ddr, popts, common_dimm, cas_latency);
2543 set_timing_cfg_2(ctrl_num, ddr, popts, common_dimm,
2544 cas_latency, additive_latency);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002545
York Sune1fd16b2011-01-10 12:03:00 +00002546 set_ddr_cdr1(ddr, popts);
York Sun57495e42012-10-08 07:44:22 +00002547 set_ddr_cdr2(ddr, popts);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002548 set_ddr_sdram_cfg(ddr, popts, common_dimm);
York Sun66869f92015-03-19 09:30:26 -07002549 ip_rev = fsl_ddr_get_version(ctrl_num);
York Sune1fd16b2011-01-10 12:03:00 +00002550 if (ip_rev > 0x40400)
2551 unq_mrs_en = 1;
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002552
York Sunf80d6472014-09-11 13:32:06 -07002553 if ((ip_rev > 0x40700) && (popts->cswl_override != 0))
York Sunef87cab2014-09-05 13:52:43 +08002554 ddr->debug[18] = popts->cswl_override;
2555
York Sun03e664d2015-01-06 13:18:50 -08002556 set_ddr_sdram_cfg_2(ctrl_num, ddr, popts, unq_mrs_en);
2557 set_ddr_sdram_mode(ctrl_num, ddr, popts, common_dimm,
2558 cas_latency, additive_latency, unq_mrs_en);
2559 set_ddr_sdram_mode_2(ctrl_num, ddr, popts, common_dimm, unq_mrs_en);
York Sun34e026f2014-03-27 17:54:47 -07002560#ifdef CONFIG_SYS_FSL_DDR4
2561 set_ddr_sdram_mode_9(ddr, popts, common_dimm, unq_mrs_en);
York Sun03e664d2015-01-06 13:18:50 -08002562 set_ddr_sdram_mode_10(ctrl_num, ddr, popts, common_dimm, unq_mrs_en);
York Sun34e026f2014-03-27 17:54:47 -07002563#endif
York Sun03e664d2015-01-06 13:18:50 -08002564 set_ddr_sdram_interval(ctrl_num, ddr, popts, common_dimm);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002565 set_ddr_data_init(ddr);
2566 set_ddr_sdram_clk_cntl(ddr, popts);
2567 set_ddr_init_addr(ddr);
2568 set_ddr_init_ext_addr(ddr);
Dave Liuec145e82010-03-05 12:22:00 +08002569 set_timing_cfg_4(ddr, popts);
York Sune1fd16b2011-01-10 12:03:00 +00002570 set_timing_cfg_5(ddr, cas_latency);
York Sun34e026f2014-03-27 17:54:47 -07002571#ifdef CONFIG_SYS_FSL_DDR4
2572 set_ddr_sdram_cfg_3(ddr, popts);
2573 set_timing_cfg_6(ddr);
York Sun426230a2018-01-29 09:44:33 -08002574 set_timing_cfg_7(ctrl_num, ddr, popts, common_dimm);
York Sun03e664d2015-01-06 13:18:50 -08002575 set_timing_cfg_8(ctrl_num, ddr, popts, common_dimm, cas_latency);
York Sunc0c32af2018-01-29 09:44:35 -08002576 set_timing_cfg_9(ctrl_num, ddr, popts, common_dimm);
York Sun34e026f2014-03-27 17:54:47 -07002577 set_ddr_dq_mapping(ddr, dimm_params);
2578#endif
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002579
Dave Liuc360cea2009-03-14 12:48:30 +08002580 set_ddr_zq_cntl(ddr, zq_en);
Dave Liubdc9f7b2009-12-16 10:24:37 -06002581 set_ddr_wrlvl_cntl(ddr, wrlvl_en, popts);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002582
Dave Liu22cca7e2008-11-21 16:31:35 +08002583 set_ddr_sr_cntr(ddr, sr_it);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002584
York Sune1fd16b2011-01-10 12:03:00 +00002585 set_ddr_sdram_rcw(ddr, popts, common_dimm);
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002586
York Suncb930712013-06-25 11:37:41 -07002587#ifdef CONFIG_SYS_FSL_DDR_EMU
2588 /* disble DDR training for emulator */
2589 ddr->debug[2] = 0x00000400;
York Sun1f3402e2015-01-06 13:18:45 -08002590 ddr->debug[4] = 0xff800800;
2591 ddr->debug[5] = 0x08000800;
2592 ddr->debug[6] = 0x08000800;
2593 ddr->debug[7] = 0x08000800;
2594 ddr->debug[8] = 0x08000800;
York Suncb930712013-06-25 11:37:41 -07002595#endif
York Sun9855b3b2014-05-23 13:15:00 -07002596#ifdef CONFIG_SYS_FSL_ERRATUM_A004508
2597 if ((ip_rev >= 0x40000) && (ip_rev < 0x40400))
2598 ddr->debug[2] |= 0x00000200; /* set bit 22 */
2599#endif
2600
Shengzhou Liu02fb2762016-11-21 11:36:48 +08002601#if defined(CONFIG_SYS_FSL_ERRATUM_A008378) && defined(CONFIG_SYS_FSL_DDRC_GEN4)
2602 /* Erratum applies when accumulated ECC is used, or DBI is enabled */
2603#define IS_ACC_ECC_EN(v) ((v) & 0x4)
2604#define IS_DBI(v) ((((v) >> 12) & 0x3) == 0x2)
2605 if (has_erratum_a008378()) {
2606 if (IS_ACC_ECC_EN(ddr->ddr_sdram_cfg) ||
2607 IS_DBI(ddr->ddr_sdram_cfg_3)) {
2608 ddr->debug[28] = ddr_in32(&ddrc->debug[28]);
2609 ddr->debug[28] |= (0x9 << 20);
2610 }
2611 }
2612#endif
2613
2614#ifdef CONFIG_SYS_FSL_ERRATUM_A009942
2615 ddr_freq = get_ddr_freq(ctrl_num) / 1000000;
2616 ddr->debug[28] |= ddr_in32(&ddrc->debug[28]);
2617 ddr->debug[28] &= 0xff0fff00;
2618 if (ddr_freq <= 1333)
2619 ddr->debug[28] |= 0x0080006a;
2620 else if (ddr_freq <= 1600)
2621 ddr->debug[28] |= 0x0070006f;
2622 else if (ddr_freq <= 1867)
2623 ddr->debug[28] |= 0x00700076;
2624 else if (ddr_freq <= 2133)
2625 ddr->debug[28] |= 0x0060007b;
2626 if (popts->cpo_sample)
2627 ddr->debug[28] = (ddr->debug[28] & 0xffffff00) |
2628 popts->cpo_sample;
2629#endif
2630
Kumar Gala58e5e9a2008-08-26 15:01:29 -05002631 return check_fsl_memctl_config_regs(ddr);
2632}
Shengzhou Liu02fb2762016-11-21 11:36:48 +08002633
2634#ifdef CONFIG_SYS_FSL_ERRATUM_A009942
2635/*
2636 * This additional workaround of A009942 checks the condition to determine if
2637 * the CPO value set by the existing A009942 workaround needs to be updated.
2638 * If need, print a warning to prompt user reconfigure DDR debug_29[24:31] with
2639 * expected optimal value, the optimal value is highly board dependent.
2640 */
2641void erratum_a009942_check_cpo(void)
2642{
2643 struct ccsr_ddr __iomem *ddr =
2644 (struct ccsr_ddr __iomem *)(CONFIG_SYS_FSL_DDR_ADDR);
2645 u32 cpo, cpo_e, cpo_o, cpo_target, cpo_optimal;
2646 u32 cpo_min = ddr_in32(&ddr->debug[9]) >> 24;
2647 u32 cpo_max = cpo_min;
2648 u32 sdram_cfg, i, tmp, lanes, ddr_type;
2649 bool update_cpo = false, has_ecc = false;
2650
2651 sdram_cfg = ddr_in32(&ddr->sdram_cfg);
2652 if (sdram_cfg & SDRAM_CFG_32_BE)
2653 lanes = 4;
2654 else if (sdram_cfg & SDRAM_CFG_16_BE)
2655 lanes = 2;
2656 else
2657 lanes = 8;
2658
2659 if (sdram_cfg & SDRAM_CFG_ECC_EN)
2660 has_ecc = true;
2661
2662 /* determine the maximum and minimum CPO values */
2663 for (i = 9; i < 9 + lanes / 2; i++) {
2664 cpo = ddr_in32(&ddr->debug[i]);
2665 cpo_e = cpo >> 24;
2666 cpo_o = (cpo >> 8) & 0xff;
2667 tmp = min(cpo_e, cpo_o);
2668 if (tmp < cpo_min)
2669 cpo_min = tmp;
2670 tmp = max(cpo_e, cpo_o);
2671 if (tmp > cpo_max)
2672 cpo_max = tmp;
2673 }
2674
2675 if (has_ecc) {
2676 cpo = ddr_in32(&ddr->debug[13]);
2677 cpo = cpo >> 24;
2678 if (cpo < cpo_min)
2679 cpo_min = cpo;
2680 if (cpo > cpo_max)
2681 cpo_max = cpo;
2682 }
2683
2684 cpo_target = ddr_in32(&ddr->debug[28]) & 0xff;
2685 cpo_optimal = ((cpo_max + cpo_min) >> 1) + 0x27;
2686 debug("cpo_optimal = 0x%x, cpo_target = 0x%x\n", cpo_optimal,
2687 cpo_target);
2688 debug("cpo_max = 0x%x, cpo_min = 0x%x\n", cpo_max, cpo_min);
2689
2690 ddr_type = (sdram_cfg & SDRAM_CFG_SDRAM_TYPE_MASK) >>
2691 SDRAM_CFG_SDRAM_TYPE_SHIFT;
2692 if (ddr_type == SDRAM_TYPE_DDR4)
2693 update_cpo = (cpo_min + 0x3b) < cpo_target ? true : false;
2694 else if (ddr_type == SDRAM_TYPE_DDR3)
2695 update_cpo = (cpo_min + 0x3f) < cpo_target ? true : false;
2696
2697 if (update_cpo) {
2698 printf("WARN: pls set popts->cpo_sample = 0x%x ", cpo_optimal);
2699 printf("in <board>/ddr.c to optimize cpo\n");
2700 }
2701}
2702#endif