blob: 9a25192c079803dcbf9fc684ce339f57537baf74 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kumar Gala124b0822008-08-26 15:01:29 -05002/*
York Sun6db4fdd2018-01-29 09:44:35 -08003 * Copyright 2008-2016 Freescale Semiconductor, Inc.
Jaiprakash Singhe230a922020-06-02 12:44:02 +05304 * Copyright 2017-2020 NXP Semiconductor
Kumar Gala124b0822008-08-26 15:01:29 -05005 */
6
7/*
Shengzhou Liu15875a52016-11-21 11:36:48 +08008 * Generic driver for Freescale DDR/DDR2/DDR3/DDR4 memory controller.
Kumar Gala124b0822008-08-26 15:01:29 -05009 * Based on code from spd_sdram.c
10 * Author: James Yang [at freescale.com]
11 */
12
Tom Rinidec7ea02024-05-20 13:35:03 -060013#include <config.h>
York Sunf0626592013-09-30 09:22:09 -070014#include <fsl_ddr_sdram.h>
Shengzhou Liu15875a52016-11-21 11:36:48 +080015#include <fsl_errata.h>
York Sunf0626592013-09-30 09:22:09 -070016#include <fsl_ddr.h>
York Suna21803d2013-11-18 10:29:32 -080017#include <fsl_immap.h>
Simon Glass0f2af882020-05-10 11:40:05 -060018#include <log.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060019#include <linux/string.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060020#include <asm/bitops.h>
York Sunf0626592013-09-30 09:22:09 -070021#include <asm/io.h>
Simon Glass89e0a3a2017-05-17 08:23:10 -060022#if defined(CONFIG_FSL_LSCH2) || defined(CONFIG_FSL_LSCH3) || \
23 defined(CONFIG_ARM)
Simon Glass243182c2017-05-17 08:23:06 -060024#include <asm/arch/clock.h>
25#endif
Kumar Gala124b0822008-08-26 15:01:29 -050026
Kumar Gala124b0822008-08-26 15:01:29 -050027/*
28 * Determine Rtt value.
29 *
30 * This should likely be either board or controller specific.
31 *
Dave Liu4be87b22009-03-14 12:48:30 +080032 * Rtt(nominal) - DDR2:
Kumar Gala124b0822008-08-26 15:01:29 -050033 * 0 = Rtt disabled
34 * 1 = 75 ohm
35 * 2 = 150 ohm
36 * 3 = 50 ohm
Dave Liu4be87b22009-03-14 12:48:30 +080037 * Rtt(nominal) - DDR3:
38 * 0 = Rtt disabled
39 * 1 = 60 ohm
40 * 2 = 120 ohm
41 * 3 = 40 ohm
42 * 4 = 20 ohm
43 * 5 = 30 ohm
Kumar Gala124b0822008-08-26 15:01:29 -050044 *
45 * FIXME: Apparently 8641 needs a value of 2
46 * FIXME: Old code seys if 667 MHz or higher, use 3 on 8572
47 *
48 * FIXME: There was some effort down this line earlier:
49 *
50 * unsigned int i;
51 * for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL/2; i++) {
52 * if (popts->dimmslot[i].num_valid_cs
53 * && (popts->cs_local_opts[2*i].odt_rd_cfg
54 * || popts->cs_local_opts[2*i].odt_wr_cfg)) {
55 * rtt = 2;
56 * break;
57 * }
58 * }
59 */
60static inline int fsl_ddr_get_rtt(void)
61{
62 int rtt;
63
York Sunf0626592013-09-30 09:22:09 -070064#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala124b0822008-08-26 15:01:29 -050065 rtt = 0;
York Sunf0626592013-09-30 09:22:09 -070066#elif defined(CONFIG_SYS_FSL_DDR2)
Kumar Gala124b0822008-08-26 15:01:29 -050067 rtt = 3;
68#else
Dave Liu4be87b22009-03-14 12:48:30 +080069 rtt = 0;
Kumar Gala124b0822008-08-26 15:01:29 -050070#endif
71
72 return rtt;
73}
74
York Sun2896cb72014-03-27 17:54:47 -070075#ifdef CONFIG_SYS_FSL_DDR4
76/*
77 * compute CAS write latency according to DDR4 spec
78 * CWL = 9 for <= 1600MT/s
79 * 10 for <= 1866MT/s
80 * 11 for <= 2133MT/s
81 * 12 for <= 2400MT/s
82 * 14 for <= 2667MT/s
83 * 16 for <= 2933MT/s
84 * 18 for higher
85 */
York Sun2c0b62d2015-01-06 13:18:50 -080086static inline unsigned int compute_cas_write_latency(
87 const unsigned int ctrl_num)
York Sun2896cb72014-03-27 17:54:47 -070088{
89 unsigned int cwl;
York Sun2c0b62d2015-01-06 13:18:50 -080090 const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
York Sun2896cb72014-03-27 17:54:47 -070091 if (mclk_ps >= 1250)
92 cwl = 9;
93 else if (mclk_ps >= 1070)
94 cwl = 10;
95 else if (mclk_ps >= 935)
96 cwl = 11;
97 else if (mclk_ps >= 833)
98 cwl = 12;
99 else if (mclk_ps >= 750)
100 cwl = 14;
101 else if (mclk_ps >= 681)
102 cwl = 16;
103 else
104 cwl = 18;
105
106 return cwl;
107}
108#else
Dave Liu4be87b22009-03-14 12:48:30 +0800109/*
110 * compute the CAS write latency according to DDR3 spec
111 * CWL = 5 if tCK >= 2.5ns
112 * 6 if 2.5ns > tCK >= 1.875ns
113 * 7 if 1.875ns > tCK >= 1.5ns
114 * 8 if 1.5ns > tCK >= 1.25ns
York Sun7a16d642011-08-24 09:40:25 -0700115 * 9 if 1.25ns > tCK >= 1.07ns
116 * 10 if 1.07ns > tCK >= 0.935ns
117 * 11 if 0.935ns > tCK >= 0.833ns
118 * 12 if 0.833ns > tCK >= 0.75ns
Dave Liu4be87b22009-03-14 12:48:30 +0800119 */
York Sun2c0b62d2015-01-06 13:18:50 -0800120static inline unsigned int compute_cas_write_latency(
121 const unsigned int ctrl_num)
Dave Liu4be87b22009-03-14 12:48:30 +0800122{
123 unsigned int cwl;
York Sun2c0b62d2015-01-06 13:18:50 -0800124 const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
Dave Liu4be87b22009-03-14 12:48:30 +0800125
126 if (mclk_ps >= 2500)
127 cwl = 5;
128 else if (mclk_ps >= 1875)
129 cwl = 6;
130 else if (mclk_ps >= 1500)
131 cwl = 7;
132 else if (mclk_ps >= 1250)
133 cwl = 8;
York Sun7a16d642011-08-24 09:40:25 -0700134 else if (mclk_ps >= 1070)
135 cwl = 9;
136 else if (mclk_ps >= 935)
137 cwl = 10;
138 else if (mclk_ps >= 833)
139 cwl = 11;
140 else if (mclk_ps >= 750)
141 cwl = 12;
142 else {
143 cwl = 12;
144 printf("Warning: CWL is out of range\n");
145 }
Dave Liu4be87b22009-03-14 12:48:30 +0800146 return cwl;
147}
York Sun2896cb72014-03-27 17:54:47 -0700148#endif
Dave Liu4be87b22009-03-14 12:48:30 +0800149
Kumar Gala124b0822008-08-26 15:01:29 -0500150/* Chip Select Configuration (CSn_CONFIG) */
yorkf4f93c62010-07-02 22:25:53 +0000151static void set_csn_config(int dimm_number, int i, fsl_ddr_cfg_regs_t *ddr,
Kumar Gala124b0822008-08-26 15:01:29 -0500152 const memctl_options_t *popts,
153 const dimm_params_t *dimm_params)
154{
155 unsigned int cs_n_en = 0; /* Chip Select enable */
156 unsigned int intlv_en = 0; /* Memory controller interleave enable */
157 unsigned int intlv_ctl = 0; /* Interleaving control */
158 unsigned int ap_n_en = 0; /* Chip select n auto-precharge enable */
159 unsigned int odt_rd_cfg = 0; /* ODT for reads configuration */
160 unsigned int odt_wr_cfg = 0; /* ODT for writes configuration */
161 unsigned int ba_bits_cs_n = 0; /* Num of bank bits for SDRAM on CSn */
162 unsigned int row_bits_cs_n = 0; /* Num of row bits for SDRAM on CSn */
163 unsigned int col_bits_cs_n = 0; /* Num of ocl bits for SDRAM on CSn */
yorkf4f93c62010-07-02 22:25:53 +0000164 int go_config = 0;
York Sun2896cb72014-03-27 17:54:47 -0700165#ifdef CONFIG_SYS_FSL_DDR4
166 unsigned int bg_bits_cs_n = 0; /* Num of bank group bits */
167#else
168 unsigned int n_banks_per_sdram_device;
169#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500170
171 /* Compute CS_CONFIG only for existing ranks of each DIMM. */
yorkf4f93c62010-07-02 22:25:53 +0000172 switch (i) {
173 case 0:
174 if (dimm_params[dimm_number].n_ranks > 0) {
175 go_config = 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500176 /* These fields only available in CS0_CONFIG */
York Sune8dc17b2012-08-17 08:22:39 +0000177 if (!popts->memctl_interleaving)
178 break;
179 switch (popts->memctl_interleaving_mode) {
York Sunc459ae62014-02-10 13:59:44 -0800180 case FSL_DDR_256B_INTERLEAVING:
York Sune8dc17b2012-08-17 08:22:39 +0000181 case FSL_DDR_CACHE_LINE_INTERLEAVING:
182 case FSL_DDR_PAGE_INTERLEAVING:
183 case FSL_DDR_BANK_INTERLEAVING:
184 case FSL_DDR_SUPERBANK_INTERLEAVING:
185 intlv_en = popts->memctl_interleaving;
186 intlv_ctl = popts->memctl_interleaving_mode;
187 break;
188 default:
189 break;
190 }
Kumar Gala124b0822008-08-26 15:01:29 -0500191 }
yorkf4f93c62010-07-02 22:25:53 +0000192 break;
193 case 1:
194 if ((dimm_number == 0 && dimm_params[0].n_ranks > 1) || \
195 (dimm_number == 1 && dimm_params[1].n_ranks > 0))
196 go_config = 1;
197 break;
198 case 2:
199 if ((dimm_number == 0 && dimm_params[0].n_ranks > 2) || \
York Sun15f874a2011-08-26 11:32:40 -0700200 (dimm_number >= 1 && dimm_params[dimm_number].n_ranks > 0))
yorkf4f93c62010-07-02 22:25:53 +0000201 go_config = 1;
202 break;
203 case 3:
204 if ((dimm_number == 0 && dimm_params[0].n_ranks > 3) || \
205 (dimm_number == 1 && dimm_params[1].n_ranks > 1) || \
206 (dimm_number == 3 && dimm_params[3].n_ranks > 0))
207 go_config = 1;
208 break;
209 default:
210 break;
211 }
212 if (go_config) {
yorkf4f93c62010-07-02 22:25:53 +0000213 cs_n_en = 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500214 ap_n_en = popts->cs_local_opts[i].auto_precharge;
215 odt_rd_cfg = popts->cs_local_opts[i].odt_rd_cfg;
216 odt_wr_cfg = popts->cs_local_opts[i].odt_wr_cfg;
York Sun2896cb72014-03-27 17:54:47 -0700217#ifdef CONFIG_SYS_FSL_DDR4
Sean Andersona072a6c2022-08-30 17:01:07 -0400218 ba_bits_cs_n = dimm_params[dimm_number].bank_addr_bits - 2;
York Sun2896cb72014-03-27 17:54:47 -0700219 bg_bits_cs_n = dimm_params[dimm_number].bank_group_bits;
220#else
Kumar Gala124b0822008-08-26 15:01:29 -0500221 n_banks_per_sdram_device
yorkf4f93c62010-07-02 22:25:53 +0000222 = dimm_params[dimm_number].n_banks_per_sdram_device;
Kumar Gala124b0822008-08-26 15:01:29 -0500223 ba_bits_cs_n = __ilog2(n_banks_per_sdram_device) - 2;
York Sun2896cb72014-03-27 17:54:47 -0700224#endif
yorkf4f93c62010-07-02 22:25:53 +0000225 row_bits_cs_n = dimm_params[dimm_number].n_row_addr - 12;
226 col_bits_cs_n = dimm_params[dimm_number].n_col_addr - 8;
Kumar Gala124b0822008-08-26 15:01:29 -0500227 }
Kumar Gala124b0822008-08-26 15:01:29 -0500228 ddr->cs[i].config = (0
229 | ((cs_n_en & 0x1) << 31)
230 | ((intlv_en & 0x3) << 29)
Haiying Wang272b5962008-10-03 12:36:39 -0400231 | ((intlv_ctl & 0xf) << 24)
Kumar Gala124b0822008-08-26 15:01:29 -0500232 | ((ap_n_en & 0x1) << 23)
233
234 /* XXX: some implementation only have 1 bit starting at left */
235 | ((odt_rd_cfg & 0x7) << 20)
236
237 /* XXX: Some implementation only have 1 bit starting at left */
238 | ((odt_wr_cfg & 0x7) << 16)
239
240 | ((ba_bits_cs_n & 0x3) << 14)
241 | ((row_bits_cs_n & 0x7) << 8)
York Sun2896cb72014-03-27 17:54:47 -0700242#ifdef CONFIG_SYS_FSL_DDR4
243 | ((bg_bits_cs_n & 0x3) << 4)
244#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500245 | ((col_bits_cs_n & 0x7) << 0)
246 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400247 debug("FSLDDR: cs[%d]_config = 0x%08x\n", i,ddr->cs[i].config);
Kumar Gala124b0822008-08-26 15:01:29 -0500248}
249
250/* Chip Select Configuration 2 (CSn_CONFIG_2) */
251/* FIXME: 8572 */
252static void set_csn_config_2(int i, fsl_ddr_cfg_regs_t *ddr)
253{
254 unsigned int pasr_cfg = 0; /* Partial array self refresh config */
255
256 ddr->cs[i].config_2 = ((pasr_cfg & 7) << 24);
Haiying Wangd90e0402008-10-03 12:37:26 -0400257 debug("FSLDDR: cs[%d]_config_2 = 0x%08x\n", i, ddr->cs[i].config_2);
Kumar Gala124b0822008-08-26 15:01:29 -0500258}
259
260/* -3E = 667 CL5, -25 = CL6 800, -25E = CL5 800 */
261
York Sunf0626592013-09-30 09:22:09 -0700262#if !defined(CONFIG_SYS_FSL_DDR1)
York Sunfbce88c2014-11-07 12:14:36 -0800263/*
264 * Check DIMM configuration, return 2 if quad-rank or two dual-rank
265 * Return 1 if other two slots configuration. Return 0 if single slot.
266 */
York Sun98df4d12012-10-08 07:44:23 +0000267static inline int avoid_odt_overlap(const dimm_params_t *dimm_params)
268{
269#if CONFIG_DIMM_SLOTS_PER_CTLR == 1
270 if (dimm_params[0].n_ranks == 4)
York Sunfbce88c2014-11-07 12:14:36 -0800271 return 2;
York Sun98df4d12012-10-08 07:44:23 +0000272#endif
273
274#if CONFIG_DIMM_SLOTS_PER_CTLR == 2
275 if ((dimm_params[0].n_ranks == 2) &&
276 (dimm_params[1].n_ranks == 2))
York Sunfbce88c2014-11-07 12:14:36 -0800277 return 2;
York Sun98df4d12012-10-08 07:44:23 +0000278
279#ifdef CONFIG_FSL_DDR_FIRST_SLOT_QUAD_CAPABLE
280 if (dimm_params[0].n_ranks == 4)
York Sunfbce88c2014-11-07 12:14:36 -0800281 return 2;
York Sun98df4d12012-10-08 07:44:23 +0000282#endif
York Sunfbce88c2014-11-07 12:14:36 -0800283
284 if ((dimm_params[0].n_ranks != 0) &&
285 (dimm_params[2].n_ranks != 0))
286 return 1;
York Sun98df4d12012-10-08 07:44:23 +0000287#endif
288 return 0;
289}
290
Kumar Gala124b0822008-08-26 15:01:29 -0500291/*
292 * DDR SDRAM Timing Configuration 0 (TIMING_CFG_0)
293 *
294 * Avoid writing for DDR I. The new PQ38 DDR controller
295 * dreams up non-zero default values to be backwards compatible.
296 */
York Sun2c0b62d2015-01-06 13:18:50 -0800297static void set_timing_cfg_0(const unsigned int ctrl_num,
298 fsl_ddr_cfg_regs_t *ddr,
York Sun98df4d12012-10-08 07:44:23 +0000299 const memctl_options_t *popts,
300 const dimm_params_t *dimm_params)
Kumar Gala124b0822008-08-26 15:01:29 -0500301{
302 unsigned char trwt_mclk = 0; /* Read-to-write turnaround */
303 unsigned char twrt_mclk = 0; /* Write-to-read turnaround */
304 /* 7.5 ns on -3E; 0 means WL - CL + BL/2 + 1 */
305 unsigned char trrt_mclk = 0; /* Read-to-read turnaround */
306 unsigned char twwt_mclk = 0; /* Write-to-write turnaround */
307
308 /* Active powerdown exit timing (tXARD and tXARDS). */
309 unsigned char act_pd_exit_mclk;
310 /* Precharge powerdown exit timing (tXP). */
311 unsigned char pre_pd_exit_mclk;
york1714e492010-07-02 22:25:56 +0000312 /* ODT powerdown exit timing (tAXPD). */
York Sun2896cb72014-03-27 17:54:47 -0700313 unsigned char taxpd_mclk = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500314 /* Mode register set cycle time (tMRD). */
315 unsigned char tmrd_mclk;
York Sunc1bf24f2014-08-21 16:13:22 -0700316#if defined(CONFIG_SYS_FSL_DDR4) || defined(CONFIG_SYS_FSL_DDR3)
York Sun2c0b62d2015-01-06 13:18:50 -0800317 const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
York Sunc1bf24f2014-08-21 16:13:22 -0700318#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500319
York Sun2896cb72014-03-27 17:54:47 -0700320#ifdef CONFIG_SYS_FSL_DDR4
321 /* tXP=max(4nCK, 6ns) */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900322 int txp = max((int)mclk_ps * 4, 6000); /* unit=ps */
York Sun55eb5fa2015-03-19 09:30:26 -0700323 unsigned int data_rate = get_ddr_freq(ctrl_num);
324
325 /* for faster clock, need more time for data setup */
326 trwt_mclk = (data_rate/1000000 > 1900) ? 3 : 2;
York Sun77594b32015-11-04 10:03:21 -0800327
328 /*
329 * for single quad-rank DIMM and two-slot DIMMs
330 * to avoid ODT overlap
331 */
332 switch (avoid_odt_overlap(dimm_params)) {
333 case 2:
334 twrt_mclk = 2;
335 twwt_mclk = 2;
336 trrt_mclk = 2;
337 break;
338 default:
339 twrt_mclk = 1;
340 twwt_mclk = 1;
341 trrt_mclk = 0;
342 break;
343 }
344
York Sun2c0b62d2015-01-06 13:18:50 -0800345 act_pd_exit_mclk = picos_to_mclk(ctrl_num, txp);
York Sun2896cb72014-03-27 17:54:47 -0700346 pre_pd_exit_mclk = act_pd_exit_mclk;
347 /*
348 * MRS_CYC = max(tMRD, tMOD)
349 * tMRD = 8nCK, tMOD = max(24nCK, 15ns)
350 */
York Sun2c0b62d2015-01-06 13:18:50 -0800351 tmrd_mclk = max(24U, picos_to_mclk(ctrl_num, 15000));
York Sun2896cb72014-03-27 17:54:47 -0700352#elif defined(CONFIG_SYS_FSL_DDR3)
York Sun2c0b62d2015-01-06 13:18:50 -0800353 unsigned int data_rate = get_ddr_freq(ctrl_num);
York Sunc1bf24f2014-08-21 16:13:22 -0700354 int txp;
York Sun1b07ef12014-12-02 11:18:09 -0800355 unsigned int ip_rev;
York Sunfbce88c2014-11-07 12:14:36 -0800356 int odt_overlap;
Dave Liu4be87b22009-03-14 12:48:30 +0800357 /*
358 * (tXARD and tXARDS). Empirical?
359 * The DDR3 spec has not tXARD,
360 * we use the tXP instead of it.
York Sunc1bf24f2014-08-21 16:13:22 -0700361 * tXP=max(3nCK, 7.5ns) for DDR3-800, 1066
362 * max(3nCK, 6ns) for DDR3-1333, 1600, 1866, 2133
Dave Liu4be87b22009-03-14 12:48:30 +0800363 * spec has not the tAXPD, we use
york1714e492010-07-02 22:25:56 +0000364 * tAXPD=1, need design to confirm.
Dave Liu4be87b22009-03-14 12:48:30 +0800365 */
Masahiro Yamadadb204642014-11-07 03:03:31 +0900366 txp = max((int)mclk_ps * 3, (mclk_ps > 1540 ? 7500 : 6000));
York Sunc1bf24f2014-08-21 16:13:22 -0700367
York Sun55eb5fa2015-03-19 09:30:26 -0700368 ip_rev = fsl_ddr_get_version(ctrl_num);
York Sun1b07ef12014-12-02 11:18:09 -0800369 if (ip_rev >= 0x40700) {
370 /*
371 * MRS_CYC = max(tMRD, tMOD)
372 * tMRD = 4nCK (8nCK for RDIMM)
373 * tMOD = max(12nCK, 15ns)
374 */
York Sun2c0b62d2015-01-06 13:18:50 -0800375 tmrd_mclk = max((unsigned int)12,
376 picos_to_mclk(ctrl_num, 15000));
York Sun1b07ef12014-12-02 11:18:09 -0800377 } else {
378 /*
379 * MRS_CYC = tMRD
380 * tMRD = 4nCK (8nCK for RDIMM)
381 */
382 if (popts->registered_dimm_en)
383 tmrd_mclk = 8;
384 else
385 tmrd_mclk = 4;
386 }
387
Dave Liu81079262009-12-08 11:56:48 +0800388 /* set the turnaround time */
York Sun98df4d12012-10-08 07:44:23 +0000389
390 /*
York Sunfbce88c2014-11-07 12:14:36 -0800391 * for single quad-rank DIMM and two-slot DIMMs
York Sun98df4d12012-10-08 07:44:23 +0000392 * to avoid ODT overlap
393 */
York Sunfbce88c2014-11-07 12:14:36 -0800394 odt_overlap = avoid_odt_overlap(dimm_params);
395 switch (odt_overlap) {
396 case 2:
York Sun98df4d12012-10-08 07:44:23 +0000397 twwt_mclk = 2;
398 trrt_mclk = 1;
York Sunfbce88c2014-11-07 12:14:36 -0800399 break;
400 case 1:
401 twwt_mclk = 1;
402 trrt_mclk = 0;
403 break;
404 default:
405 break;
York Sun98df4d12012-10-08 07:44:23 +0000406 }
York Sunfbce88c2014-11-07 12:14:36 -0800407
York Sun98df4d12012-10-08 07:44:23 +0000408 /* for faster clock, need more time for data setup */
409 trwt_mclk = (data_rate/1000000 > 1800) ? 2 : 1;
410
York Sun27f83be2011-02-10 10:13:10 -0800411 if ((data_rate/1000000 > 1150) || (popts->memctl_interleaving))
412 twrt_mclk = 1;
York Sunba0c2eb2011-01-10 12:03:00 +0000413
414 if (popts->dynamic_power == 0) { /* powerdown is not used */
415 act_pd_exit_mclk = 1;
416 pre_pd_exit_mclk = 1;
417 taxpd_mclk = 1;
418 } else {
419 /* act_pd_exit_mclk = tXARD, see above */
York Sun2c0b62d2015-01-06 13:18:50 -0800420 act_pd_exit_mclk = picos_to_mclk(ctrl_num, txp);
York Sunba0c2eb2011-01-10 12:03:00 +0000421 /* Mode register MR0[A12] is '1' - fast exit */
422 pre_pd_exit_mclk = act_pd_exit_mclk;
423 taxpd_mclk = 1;
424 }
York Sunf0626592013-09-30 09:22:09 -0700425#else /* CONFIG_SYS_FSL_DDR2 */
Dave Liu4be87b22009-03-14 12:48:30 +0800426 /*
427 * (tXARD and tXARDS). Empirical?
428 * tXARD = 2 for DDR2
429 * tXP=2
430 * tAXPD=8
431 */
432 act_pd_exit_mclk = 2;
433 pre_pd_exit_mclk = 2;
434 taxpd_mclk = 8;
Kumar Gala124b0822008-08-26 15:01:29 -0500435 tmrd_mclk = 2;
Dave Liu4be87b22009-03-14 12:48:30 +0800436#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500437
York Sunf8691fc2011-05-27 13:44:28 +0800438 if (popts->trwt_override)
439 trwt_mclk = popts->trwt;
440
Kumar Gala124b0822008-08-26 15:01:29 -0500441 ddr->timing_cfg_0 = (0
442 | ((trwt_mclk & 0x3) << 30) /* RWT */
443 | ((twrt_mclk & 0x3) << 28) /* WRT */
444 | ((trrt_mclk & 0x3) << 26) /* RRT */
445 | ((twwt_mclk & 0x3) << 24) /* WWT */
York Sun63c91cd2013-06-03 12:39:06 -0700446 | ((act_pd_exit_mclk & 0xf) << 20) /* ACT_PD_EXIT */
Dave Liu4758d532008-11-21 16:31:29 +0800447 | ((pre_pd_exit_mclk & 0xF) << 16) /* PRE_PD_EXIT */
Kumar Gala124b0822008-08-26 15:01:29 -0500448 | ((taxpd_mclk & 0xf) << 8) /* ODT_PD_EXIT */
York Sun63c91cd2013-06-03 12:39:06 -0700449 | ((tmrd_mclk & 0x1f) << 0) /* MRS_CYC */
Kumar Gala124b0822008-08-26 15:01:29 -0500450 );
451 debug("FSLDDR: timing_cfg_0 = 0x%08x\n", ddr->timing_cfg_0);
452}
York Sunfbce88c2014-11-07 12:14:36 -0800453#endif /* !defined(CONFIG_SYS_FSL_DDR1) */
Kumar Gala124b0822008-08-26 15:01:29 -0500454
455/* DDR SDRAM Timing Configuration 3 (TIMING_CFG_3) */
York Sun2c0b62d2015-01-06 13:18:50 -0800456static void set_timing_cfg_3(const unsigned int ctrl_num,
457 fsl_ddr_cfg_regs_t *ddr,
458 const memctl_options_t *popts,
459 const common_timing_params_t *common_dimm,
460 unsigned int cas_latency,
461 unsigned int additive_latency)
Kumar Gala124b0822008-08-26 15:01:29 -0500462{
York Suncd077cf2012-08-17 08:22:40 +0000463 /* Extended precharge to activate interval (tRP) */
464 unsigned int ext_pretoact = 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500465 /* Extended Activate to precharge interval (tRAS) */
466 unsigned int ext_acttopre = 0;
York Suncd077cf2012-08-17 08:22:40 +0000467 /* Extended activate to read/write interval (tRCD) */
468 unsigned int ext_acttorw = 0;
469 /* Extended refresh recovery time (tRFC) */
470 unsigned int ext_refrec;
471 /* Extended MCAS latency from READ cmd */
472 unsigned int ext_caslat = 0;
York Sun63c91cd2013-06-03 12:39:06 -0700473 /* Extended additive latency */
474 unsigned int ext_add_lat = 0;
York Suncd077cf2012-08-17 08:22:40 +0000475 /* Extended last data to precharge interval (tWR) */
476 unsigned int ext_wrrec = 0;
477 /* Control Adjust */
478 unsigned int cntl_adj = 0;
Dave Liu5c1bb512008-11-21 16:31:22 +0800479
York Sun2c0b62d2015-01-06 13:18:50 -0800480 ext_pretoact = picos_to_mclk(ctrl_num, common_dimm->trp_ps) >> 4;
481 ext_acttopre = picos_to_mclk(ctrl_num, common_dimm->tras_ps) >> 4;
482 ext_acttorw = picos_to_mclk(ctrl_num, common_dimm->trcd_ps) >> 4;
York Suncd077cf2012-08-17 08:22:40 +0000483 ext_caslat = (2 * cas_latency - 1) >> 4;
York Sun63c91cd2013-06-03 12:39:06 -0700484 ext_add_lat = additive_latency >> 4;
York Sun2896cb72014-03-27 17:54:47 -0700485#ifdef CONFIG_SYS_FSL_DDR4
York Sun2c0b62d2015-01-06 13:18:50 -0800486 ext_refrec = (picos_to_mclk(ctrl_num, common_dimm->trfc1_ps) - 8) >> 4;
York Sun2896cb72014-03-27 17:54:47 -0700487#else
York Sun2c0b62d2015-01-06 13:18:50 -0800488 ext_refrec = (picos_to_mclk(ctrl_num, common_dimm->trfc_ps) - 8) >> 4;
York Suncd077cf2012-08-17 08:22:40 +0000489 /* ext_wrrec only deals with 16 clock and above, or 14 with OTF */
York Sun2896cb72014-03-27 17:54:47 -0700490#endif
York Sun2c0b62d2015-01-06 13:18:50 -0800491 ext_wrrec = (picos_to_mclk(ctrl_num, common_dimm->twr_ps) +
Priyanka Jain4a717412013-09-25 10:41:19 +0530492 (popts->otf_burst_chop_en ? 2 : 0)) >> 4;
Dave Liu4be87b22009-03-14 12:48:30 +0800493
Kumar Gala124b0822008-08-26 15:01:29 -0500494 ddr->timing_cfg_3 = (0
York Suncd077cf2012-08-17 08:22:40 +0000495 | ((ext_pretoact & 0x1) << 28)
James Yang26681f52013-07-22 09:35:26 -0700496 | ((ext_acttopre & 0x3) << 24)
York Suncd077cf2012-08-17 08:22:40 +0000497 | ((ext_acttorw & 0x1) << 22)
York Sun6db4fdd2018-01-29 09:44:35 -0800498 | ((ext_refrec & 0x3F) << 16)
York Suncd077cf2012-08-17 08:22:40 +0000499 | ((ext_caslat & 0x3) << 12)
York Sun63c91cd2013-06-03 12:39:06 -0700500 | ((ext_add_lat & 0x1) << 10)
York Suncd077cf2012-08-17 08:22:40 +0000501 | ((ext_wrrec & 0x1) << 8)
Kumar Gala124b0822008-08-26 15:01:29 -0500502 | ((cntl_adj & 0x7) << 0)
503 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400504 debug("FSLDDR: timing_cfg_3 = 0x%08x\n", ddr->timing_cfg_3);
Kumar Gala124b0822008-08-26 15:01:29 -0500505}
506
507/* DDR SDRAM Timing Configuration 1 (TIMING_CFG_1) */
York Sun2c0b62d2015-01-06 13:18:50 -0800508static void set_timing_cfg_1(const unsigned int ctrl_num,
509 fsl_ddr_cfg_regs_t *ddr,
510 const memctl_options_t *popts,
511 const common_timing_params_t *common_dimm,
512 unsigned int cas_latency)
Kumar Gala124b0822008-08-26 15:01:29 -0500513{
514 /* Precharge-to-activate interval (tRP) */
515 unsigned char pretoact_mclk;
516 /* Activate to precharge interval (tRAS) */
517 unsigned char acttopre_mclk;
518 /* Activate to read/write interval (tRCD) */
519 unsigned char acttorw_mclk;
520 /* CASLAT */
521 unsigned char caslat_ctrl;
522 /* Refresh recovery time (tRFC) ; trfc_low */
523 unsigned char refrec_ctrl;
524 /* Last data to precharge minimum interval (tWR) */
525 unsigned char wrrec_mclk;
526 /* Activate-to-activate interval (tRRD) */
527 unsigned char acttoact_mclk;
528 /* Last write data pair to read command issue interval (tWTR) */
529 unsigned char wrtord_mclk;
York Sun2896cb72014-03-27 17:54:47 -0700530#ifdef CONFIG_SYS_FSL_DDR4
531 /* DDR4 supports 10, 12, 14, 16, 18, 20, 24 */
532 static const u8 wrrec_table[] = {
533 10, 10, 10, 10, 10,
534 10, 10, 10, 10, 10,
535 12, 12, 14, 14, 16,
536 16, 18, 18, 20, 20,
537 24, 24, 24, 24};
538#else
York Sun3673f2c2011-03-02 14:24:11 -0800539 /* DDR_SDRAM_MODE doesn't support 9,11,13,15 */
540 static const u8 wrrec_table[] = {
541 1, 2, 3, 4, 5, 6, 7, 8, 10, 10, 12, 12, 14, 14, 0, 0};
York Sun2896cb72014-03-27 17:54:47 -0700542#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500543
York Sun2c0b62d2015-01-06 13:18:50 -0800544 pretoact_mclk = picos_to_mclk(ctrl_num, common_dimm->trp_ps);
545 acttopre_mclk = picos_to_mclk(ctrl_num, common_dimm->tras_ps);
546 acttorw_mclk = picos_to_mclk(ctrl_num, common_dimm->trcd_ps);
Kumar Gala124b0822008-08-26 15:01:29 -0500547
548 /*
549 * Translate CAS Latency to a DDR controller field value:
550 *
551 * CAS Lat DDR I DDR II Ctrl
552 * Clocks SPD Bit SPD Bit Value
553 * ------- ------- ------- -----
554 * 1.0 0 0001
555 * 1.5 1 0010
556 * 2.0 2 2 0011
557 * 2.5 3 0100
558 * 3.0 4 3 0101
559 * 3.5 5 0110
560 * 4.0 4 0111
561 * 4.5 1000
562 * 5.0 5 1001
563 */
York Sunf0626592013-09-30 09:22:09 -0700564#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala124b0822008-08-26 15:01:29 -0500565 caslat_ctrl = (cas_latency + 1) & 0x07;
York Sunf0626592013-09-30 09:22:09 -0700566#elif defined(CONFIG_SYS_FSL_DDR2)
Kumar Gala124b0822008-08-26 15:01:29 -0500567 caslat_ctrl = 2 * cas_latency - 1;
568#else
Dave Liu4be87b22009-03-14 12:48:30 +0800569 /*
570 * if the CAS latency more than 8 cycle,
571 * we need set extend bit for it at
572 * TIMING_CFG_3[EXT_CASLAT]
573 */
York Sun55eb5fa2015-03-19 09:30:26 -0700574 if (fsl_ddr_get_version(ctrl_num) <= 0x40400)
York Sun2896cb72014-03-27 17:54:47 -0700575 caslat_ctrl = 2 * cas_latency - 1;
576 else
577 caslat_ctrl = (cas_latency - 1) << 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500578#endif
579
York Sun2896cb72014-03-27 17:54:47 -0700580#ifdef CONFIG_SYS_FSL_DDR4
York Sun2c0b62d2015-01-06 13:18:50 -0800581 refrec_ctrl = picos_to_mclk(ctrl_num, common_dimm->trfc1_ps) - 8;
582 wrrec_mclk = picos_to_mclk(ctrl_num, common_dimm->twr_ps);
583 acttoact_mclk = max(picos_to_mclk(ctrl_num, common_dimm->trrds_ps), 4U);
584 wrtord_mclk = max(2U, picos_to_mclk(ctrl_num, 2500));
York Sunedbeee12014-04-01 14:20:49 -0700585 if ((wrrec_mclk < 1) || (wrrec_mclk > 24))
586 printf("Error: WRREC doesn't support %d clocks\n", wrrec_mclk);
York Sun2896cb72014-03-27 17:54:47 -0700587 else
588 wrrec_mclk = wrrec_table[wrrec_mclk - 1];
589#else
York Sun2c0b62d2015-01-06 13:18:50 -0800590 refrec_ctrl = picos_to_mclk(ctrl_num, common_dimm->trfc_ps) - 8;
591 wrrec_mclk = picos_to_mclk(ctrl_num, common_dimm->twr_ps);
592 acttoact_mclk = picos_to_mclk(ctrl_num, common_dimm->trrd_ps);
593 wrtord_mclk = picos_to_mclk(ctrl_num, common_dimm->twtr_ps);
York Sunedbeee12014-04-01 14:20:49 -0700594 if ((wrrec_mclk < 1) || (wrrec_mclk > 16))
595 printf("Error: WRREC doesn't support %d clocks\n", wrrec_mclk);
York Suncd077cf2012-08-17 08:22:40 +0000596 else
597 wrrec_mclk = wrrec_table[wrrec_mclk - 1];
York Sun2896cb72014-03-27 17:54:47 -0700598#endif
Priyanka Jain4a717412013-09-25 10:41:19 +0530599 if (popts->otf_burst_chop_en)
Dave Liu4be87b22009-03-14 12:48:30 +0800600 wrrec_mclk += 2;
601
Dave Liu4be87b22009-03-14 12:48:30 +0800602 /*
603 * JEDEC has min requirement for tRRD
604 */
York Sunf0626592013-09-30 09:22:09 -0700605#if defined(CONFIG_SYS_FSL_DDR3)
Dave Liu4be87b22009-03-14 12:48:30 +0800606 if (acttoact_mclk < 4)
607 acttoact_mclk = 4;
608#endif
Dave Liu4be87b22009-03-14 12:48:30 +0800609 /*
610 * JEDEC has some min requirements for tWTR
611 */
York Sunf0626592013-09-30 09:22:09 -0700612#if defined(CONFIG_SYS_FSL_DDR2)
Dave Liu4be87b22009-03-14 12:48:30 +0800613 if (wrtord_mclk < 2)
614 wrtord_mclk = 2;
York Sunf0626592013-09-30 09:22:09 -0700615#elif defined(CONFIG_SYS_FSL_DDR3)
Dave Liu4be87b22009-03-14 12:48:30 +0800616 if (wrtord_mclk < 4)
617 wrtord_mclk = 4;
618#endif
Priyanka Jain4a717412013-09-25 10:41:19 +0530619 if (popts->otf_burst_chop_en)
Dave Liu4be87b22009-03-14 12:48:30 +0800620 wrtord_mclk += 2;
Kumar Gala124b0822008-08-26 15:01:29 -0500621
622 ddr->timing_cfg_1 = (0
Dave Liu5c1bb512008-11-21 16:31:22 +0800623 | ((pretoact_mclk & 0x0F) << 28)
Kumar Gala124b0822008-08-26 15:01:29 -0500624 | ((acttopre_mclk & 0x0F) << 24)
Dave Liu5c1bb512008-11-21 16:31:22 +0800625 | ((acttorw_mclk & 0xF) << 20)
Kumar Gala124b0822008-08-26 15:01:29 -0500626 | ((caslat_ctrl & 0xF) << 16)
627 | ((refrec_ctrl & 0xF) << 12)
Dave Liu5c1bb512008-11-21 16:31:22 +0800628 | ((wrrec_mclk & 0x0F) << 8)
York Sun7d69ea32012-10-08 07:44:22 +0000629 | ((acttoact_mclk & 0x0F) << 4)
630 | ((wrtord_mclk & 0x0F) << 0)
Kumar Gala124b0822008-08-26 15:01:29 -0500631 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400632 debug("FSLDDR: timing_cfg_1 = 0x%08x\n", ddr->timing_cfg_1);
Kumar Gala124b0822008-08-26 15:01:29 -0500633}
634
635/* DDR SDRAM Timing Configuration 2 (TIMING_CFG_2) */
York Sun2c0b62d2015-01-06 13:18:50 -0800636static void set_timing_cfg_2(const unsigned int ctrl_num,
637 fsl_ddr_cfg_regs_t *ddr,
638 const memctl_options_t *popts,
639 const common_timing_params_t *common_dimm,
640 unsigned int cas_latency,
641 unsigned int additive_latency)
Kumar Gala124b0822008-08-26 15:01:29 -0500642{
643 /* Additive latency */
644 unsigned char add_lat_mclk;
645 /* CAS-to-preamble override */
646 unsigned short cpo;
647 /* Write latency */
648 unsigned char wr_lat;
649 /* Read to precharge (tRTP) */
650 unsigned char rd_to_pre;
651 /* Write command to write data strobe timing adjustment */
652 unsigned char wr_data_delay;
653 /* Minimum CKE pulse width (tCKE) */
654 unsigned char cke_pls;
655 /* Window for four activates (tFAW) */
656 unsigned short four_act;
York Sunc1bf24f2014-08-21 16:13:22 -0700657#ifdef CONFIG_SYS_FSL_DDR3
York Sun2c0b62d2015-01-06 13:18:50 -0800658 const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
York Sunc1bf24f2014-08-21 16:13:22 -0700659#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500660
661 /* FIXME add check that this must be less than acttorw_mclk */
662 add_lat_mclk = additive_latency;
663 cpo = popts->cpo_override;
664
York Sunf0626592013-09-30 09:22:09 -0700665#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala124b0822008-08-26 15:01:29 -0500666 /*
667 * This is a lie. It should really be 1, but if it is
668 * set to 1, bits overlap into the old controller's
669 * otherwise unused ACSM field. If we leave it 0, then
670 * the HW will magically treat it as 1 for DDR 1. Oh Yea.
671 */
672 wr_lat = 0;
York Sunf0626592013-09-30 09:22:09 -0700673#elif defined(CONFIG_SYS_FSL_DDR2)
Dave Liu82aa9532009-03-14 12:48:19 +0800674 wr_lat = cas_latency - 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500675#else
York Sun2c0b62d2015-01-06 13:18:50 -0800676 wr_lat = compute_cas_write_latency(ctrl_num);
Kumar Gala124b0822008-08-26 15:01:29 -0500677#endif
678
York Sun2896cb72014-03-27 17:54:47 -0700679#ifdef CONFIG_SYS_FSL_DDR4
York Sun2c0b62d2015-01-06 13:18:50 -0800680 rd_to_pre = picos_to_mclk(ctrl_num, 7500);
York Sun2896cb72014-03-27 17:54:47 -0700681#else
York Sun2c0b62d2015-01-06 13:18:50 -0800682 rd_to_pre = picos_to_mclk(ctrl_num, common_dimm->trtp_ps);
York Sun2896cb72014-03-27 17:54:47 -0700683#endif
Dave Liu4be87b22009-03-14 12:48:30 +0800684 /*
685 * JEDEC has some min requirements for tRTP
686 */
York Sunf0626592013-09-30 09:22:09 -0700687#if defined(CONFIG_SYS_FSL_DDR2)
Dave Liu4be87b22009-03-14 12:48:30 +0800688 if (rd_to_pre < 2)
689 rd_to_pre = 2;
York Sun2896cb72014-03-27 17:54:47 -0700690#elif defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
Dave Liu4be87b22009-03-14 12:48:30 +0800691 if (rd_to_pre < 4)
692 rd_to_pre = 4;
Dave Liu82aa9532009-03-14 12:48:19 +0800693#endif
Priyanka Jain4a717412013-09-25 10:41:19 +0530694 if (popts->otf_burst_chop_en)
Dave Liu4be87b22009-03-14 12:48:30 +0800695 rd_to_pre += 2; /* according to UM */
696
Kumar Gala124b0822008-08-26 15:01:29 -0500697 wr_data_delay = popts->write_data_delay;
York Sun2896cb72014-03-27 17:54:47 -0700698#ifdef CONFIG_SYS_FSL_DDR4
699 cpo = 0;
York Sun2c0b62d2015-01-06 13:18:50 -0800700 cke_pls = max(3U, picos_to_mclk(ctrl_num, 5000));
York Sunc1bf24f2014-08-21 16:13:22 -0700701#elif defined(CONFIG_SYS_FSL_DDR3)
702 /*
703 * cke pulse = max(3nCK, 7.5ns) for DDR3-800
704 * max(3nCK, 5.625ns) for DDR3-1066, 1333
705 * max(3nCK, 5ns) for DDR3-1600, 1866, 2133
706 */
York Sun2c0b62d2015-01-06 13:18:50 -0800707 cke_pls = max(3U, picos_to_mclk(ctrl_num, mclk_ps > 1870 ? 7500 :
708 (mclk_ps > 1245 ? 5625 : 5000)));
York Sun2896cb72014-03-27 17:54:47 -0700709#else
York Sunc1bf24f2014-08-21 16:13:22 -0700710 cke_pls = FSL_DDR_MIN_TCKE_PULSE_WIDTH_DDR;
York Sun2896cb72014-03-27 17:54:47 -0700711#endif
York Sun2c0b62d2015-01-06 13:18:50 -0800712 four_act = picos_to_mclk(ctrl_num,
713 popts->tfaw_window_four_activates_ps);
Kumar Gala124b0822008-08-26 15:01:29 -0500714
715 ddr->timing_cfg_2 = (0
Dave Liu4758d532008-11-21 16:31:29 +0800716 | ((add_lat_mclk & 0xf) << 28)
Kumar Gala124b0822008-08-26 15:01:29 -0500717 | ((cpo & 0x1f) << 23)
Dave Liu4758d532008-11-21 16:31:29 +0800718 | ((wr_lat & 0xf) << 19)
York Sune3cef9f2016-07-29 09:02:29 -0700719 | (((wr_lat & 0x10) >> 4) << 18)
Dave Liu4be87b22009-03-14 12:48:30 +0800720 | ((rd_to_pre & RD_TO_PRE_MASK) << RD_TO_PRE_SHIFT)
721 | ((wr_data_delay & WR_DATA_DELAY_MASK) << WR_DATA_DELAY_SHIFT)
Kumar Gala124b0822008-08-26 15:01:29 -0500722 | ((cke_pls & 0x7) << 6)
Dave Liu4758d532008-11-21 16:31:29 +0800723 | ((four_act & 0x3f) << 0)
Kumar Gala124b0822008-08-26 15:01:29 -0500724 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400725 debug("FSLDDR: timing_cfg_2 = 0x%08x\n", ddr->timing_cfg_2);
Kumar Gala124b0822008-08-26 15:01:29 -0500726}
727
yorkde879322010-07-02 22:25:55 +0000728/* DDR SDRAM Register Control Word */
York Sunbc2f32a2018-01-29 10:24:08 -0800729static void set_ddr_sdram_rcw(const unsigned int ctrl_num,
730 fsl_ddr_cfg_regs_t *ddr,
731 const memctl_options_t *popts,
732 const common_timing_params_t *common_dimm)
yorkde879322010-07-02 22:25:55 +0000733{
York Sunbc2f32a2018-01-29 10:24:08 -0800734 unsigned int ddr_freq = get_ddr_freq(ctrl_num) / 1000000;
735 unsigned int rc0a, rc0f;
736
Priyanka Jain4a717412013-09-25 10:41:19 +0530737 if (common_dimm->all_dimms_registered &&
738 !common_dimm->all_dimms_unbuffered) {
York Sunba0c2eb2011-01-10 12:03:00 +0000739 if (popts->rcw_override) {
740 ddr->ddr_sdram_rcw_1 = popts->rcw_1;
741 ddr->ddr_sdram_rcw_2 = popts->rcw_2;
York Sund9f7fa02018-01-29 09:44:33 -0800742 ddr->ddr_sdram_rcw_3 = popts->rcw_3;
York Sunba0c2eb2011-01-10 12:03:00 +0000743 } else {
York Sunbc2f32a2018-01-29 10:24:08 -0800744 rc0a = ddr_freq > 3200 ? 0x7 :
745 (ddr_freq > 2933 ? 0x6 :
746 (ddr_freq > 2666 ? 0x5 :
747 (ddr_freq > 2400 ? 0x4 :
748 (ddr_freq > 2133 ? 0x3 :
749 (ddr_freq > 1866 ? 0x2 :
750 (ddr_freq > 1600 ? 1 : 0))))));
751 rc0f = ddr_freq > 3200 ? 0x3 :
752 (ddr_freq > 2400 ? 0x2 :
753 (ddr_freq > 2133 ? 0x1 : 0));
York Sunba0c2eb2011-01-10 12:03:00 +0000754 ddr->ddr_sdram_rcw_1 =
755 common_dimm->rcw[0] << 28 | \
756 common_dimm->rcw[1] << 24 | \
757 common_dimm->rcw[2] << 20 | \
758 common_dimm->rcw[3] << 16 | \
759 common_dimm->rcw[4] << 12 | \
760 common_dimm->rcw[5] << 8 | \
761 common_dimm->rcw[6] << 4 | \
762 common_dimm->rcw[7];
763 ddr->ddr_sdram_rcw_2 =
764 common_dimm->rcw[8] << 28 | \
765 common_dimm->rcw[9] << 24 | \
York Sunbc2f32a2018-01-29 10:24:08 -0800766 rc0a << 20 | \
York Sunba0c2eb2011-01-10 12:03:00 +0000767 common_dimm->rcw[11] << 16 | \
768 common_dimm->rcw[12] << 12 | \
769 common_dimm->rcw[13] << 8 | \
770 common_dimm->rcw[14] << 4 | \
York Sunbc2f32a2018-01-29 10:24:08 -0800771 rc0f;
772 ddr->ddr_sdram_rcw_3 =
773 ((ddr_freq - 1260 + 19) / 20) << 8;
York Sunba0c2eb2011-01-10 12:03:00 +0000774 }
York Sund9f7fa02018-01-29 09:44:33 -0800775 debug("FSLDDR: ddr_sdram_rcw_1 = 0x%08x\n",
776 ddr->ddr_sdram_rcw_1);
777 debug("FSLDDR: ddr_sdram_rcw_2 = 0x%08x\n",
778 ddr->ddr_sdram_rcw_2);
779 debug("FSLDDR: ddr_sdram_rcw_3 = 0x%08x\n",
780 ddr->ddr_sdram_rcw_3);
yorkde879322010-07-02 22:25:55 +0000781 }
782}
783
Kumar Gala124b0822008-08-26 15:01:29 -0500784/* DDR SDRAM control configuration (DDR_SDRAM_CFG) */
785static void set_ddr_sdram_cfg(fsl_ddr_cfg_regs_t *ddr,
786 const memctl_options_t *popts,
787 const common_timing_params_t *common_dimm)
788{
789 unsigned int mem_en; /* DDR SDRAM interface logic enable */
790 unsigned int sren; /* Self refresh enable (during sleep) */
791 unsigned int ecc_en; /* ECC enable. */
792 unsigned int rd_en; /* Registered DIMM enable */
793 unsigned int sdram_type; /* Type of SDRAM */
794 unsigned int dyn_pwr; /* Dynamic power management mode */
795 unsigned int dbw; /* DRAM dta bus width */
Dave Liu4758d532008-11-21 16:31:29 +0800796 unsigned int eight_be = 0; /* 8-beat burst enable, DDR2 is zero */
Kumar Gala124b0822008-08-26 15:01:29 -0500797 unsigned int ncap = 0; /* Non-concurrent auto-precharge */
Priyanka Jain4a717412013-09-25 10:41:19 +0530798 unsigned int threet_en; /* Enable 3T timing */
799 unsigned int twot_en; /* Enable 2T timing */
Kumar Gala124b0822008-08-26 15:01:29 -0500800 unsigned int ba_intlv_ctl; /* Bank (CS) interleaving control */
801 unsigned int x32_en = 0; /* x32 enable */
802 unsigned int pchb8 = 0; /* precharge bit 8 enable */
803 unsigned int hse; /* Global half strength override */
York Sun5d6c6262014-09-05 13:52:41 +0800804 unsigned int acc_ecc_en = 0; /* Accumulated ECC enable */
Kumar Gala124b0822008-08-26 15:01:29 -0500805 unsigned int mem_halt = 0; /* memory controller halt */
806 unsigned int bi = 0; /* Bypass initialization */
807
808 mem_en = 1;
809 sren = popts->self_refresh_in_sleep;
Priyanka Jain4a717412013-09-25 10:41:19 +0530810 if (common_dimm->all_dimms_ecc_capable) {
Kumar Gala124b0822008-08-26 15:01:29 -0500811 /* Allow setting of ECC only if all DIMMs are ECC. */
Priyanka Jain4a717412013-09-25 10:41:19 +0530812 ecc_en = popts->ecc_mode;
Kumar Gala124b0822008-08-26 15:01:29 -0500813 } else {
814 ecc_en = 0;
815 }
816
Priyanka Jain4a717412013-09-25 10:41:19 +0530817 if (common_dimm->all_dimms_registered &&
818 !common_dimm->all_dimms_unbuffered) {
York Sunba0c2eb2011-01-10 12:03:00 +0000819 rd_en = 1;
Priyanka Jain4a717412013-09-25 10:41:19 +0530820 twot_en = 0;
York Sunba0c2eb2011-01-10 12:03:00 +0000821 } else {
822 rd_en = 0;
Priyanka Jain4a717412013-09-25 10:41:19 +0530823 twot_en = popts->twot_en;
York Sunba0c2eb2011-01-10 12:03:00 +0000824 }
Kumar Gala124b0822008-08-26 15:01:29 -0500825
Tom Rini364d0022023-01-10 11:19:45 -0500826 sdram_type = CFG_FSL_SDRAM_TYPE;
Kumar Gala124b0822008-08-26 15:01:29 -0500827
828 dyn_pwr = popts->dynamic_power;
829 dbw = popts->data_bus_width;
Dave Liu4be87b22009-03-14 12:48:30 +0800830 /* 8-beat burst enable DDR-III case
831 * we must clear it when use the on-the-fly mode,
832 * must set it when use the 32-bits bus mode.
833 */
York Sun2896cb72014-03-27 17:54:47 -0700834 if ((sdram_type == SDRAM_TYPE_DDR3) ||
835 (sdram_type == SDRAM_TYPE_DDR4)) {
Dave Liu4be87b22009-03-14 12:48:30 +0800836 if (popts->burst_length == DDR_BL8)
837 eight_be = 1;
838 if (popts->burst_length == DDR_OTF)
839 eight_be = 0;
840 if (dbw == 0x1)
841 eight_be = 1;
842 }
843
Priyanka Jain4a717412013-09-25 10:41:19 +0530844 threet_en = popts->threet_en;
Kumar Gala124b0822008-08-26 15:01:29 -0500845 ba_intlv_ctl = popts->ba_intlv_ctl;
846 hse = popts->half_strength_driver_enable;
847
York Sun5d6c6262014-09-05 13:52:41 +0800848 /* set when ddr bus width < 64 */
849 acc_ecc_en = (dbw != 0 && ecc_en == 1) ? 1 : 0;
850
Kumar Gala124b0822008-08-26 15:01:29 -0500851 ddr->ddr_sdram_cfg = (0
852 | ((mem_en & 0x1) << 31)
853 | ((sren & 0x1) << 30)
854 | ((ecc_en & 0x1) << 29)
855 | ((rd_en & 0x1) << 28)
856 | ((sdram_type & 0x7) << 24)
857 | ((dyn_pwr & 0x1) << 21)
858 | ((dbw & 0x3) << 19)
859 | ((eight_be & 0x1) << 18)
860 | ((ncap & 0x1) << 17)
Priyanka Jain4a717412013-09-25 10:41:19 +0530861 | ((threet_en & 0x1) << 16)
862 | ((twot_en & 0x1) << 15)
Kumar Gala124b0822008-08-26 15:01:29 -0500863 | ((ba_intlv_ctl & 0x7F) << 8)
864 | ((x32_en & 0x1) << 5)
865 | ((pchb8 & 0x1) << 4)
866 | ((hse & 0x1) << 3)
York Sun5d6c6262014-09-05 13:52:41 +0800867 | ((acc_ecc_en & 0x1) << 2)
Kumar Gala124b0822008-08-26 15:01:29 -0500868 | ((mem_halt & 0x1) << 1)
869 | ((bi & 0x1) << 0)
870 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400871 debug("FSLDDR: ddr_sdram_cfg = 0x%08x\n", ddr->ddr_sdram_cfg);
Kumar Gala124b0822008-08-26 15:01:29 -0500872}
873
874/* DDR SDRAM control configuration 2 (DDR_SDRAM_CFG_2) */
York Sun2c0b62d2015-01-06 13:18:50 -0800875static void set_ddr_sdram_cfg_2(const unsigned int ctrl_num,
876 fsl_ddr_cfg_regs_t *ddr,
York Sunba0c2eb2011-01-10 12:03:00 +0000877 const memctl_options_t *popts,
878 const unsigned int unq_mrs_en)
Kumar Gala124b0822008-08-26 15:01:29 -0500879{
880 unsigned int frc_sr = 0; /* Force self refresh */
881 unsigned int sr_ie = 0; /* Self-refresh interrupt enable */
York Sun15f874a2011-08-26 11:32:40 -0700882 unsigned int odt_cfg = 0; /* ODT configuration */
Kumar Gala124b0822008-08-26 15:01:29 -0500883 unsigned int num_pr; /* Number of posted refreshes */
York Sun7d69ea32012-10-08 07:44:22 +0000884 unsigned int slow = 0; /* DDR will be run less than 1250 */
York Sun4889c982013-06-25 11:37:47 -0700885 unsigned int x4_en = 0; /* x4 DRAM enable */
Kumar Gala124b0822008-08-26 15:01:29 -0500886 unsigned int obc_cfg; /* On-The-Fly Burst Chop Cfg */
887 unsigned int ap_en; /* Address Parity Enable */
888 unsigned int d_init; /* DRAM data initialization */
889 unsigned int rcw_en = 0; /* Register Control Word Enable */
890 unsigned int md_en = 0; /* Mirrored DIMM Enable */
yorkf4f93c62010-07-02 22:25:53 +0000891 unsigned int qd_en = 0; /* quad-rank DIMM Enable */
York Sun15f874a2011-08-26 11:32:40 -0700892 int i;
York Sun2896cb72014-03-27 17:54:47 -0700893#ifndef CONFIG_SYS_FSL_DDR4
894 unsigned int dll_rst_dis = 1; /* DLL reset disable */
895 unsigned int dqs_cfg; /* DQS configuration */
Kumar Gala124b0822008-08-26 15:01:29 -0500896
Priyanka Jain4a717412013-09-25 10:41:19 +0530897 dqs_cfg = popts->dqs_config;
York Sun2896cb72014-03-27 17:54:47 -0700898#endif
York Sun15f874a2011-08-26 11:32:40 -0700899 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
900 if (popts->cs_local_opts[i].odt_rd_cfg
901 || popts->cs_local_opts[i].odt_wr_cfg) {
902 odt_cfg = SDRAM_CFG2_ODT_ONLY_READ;
903 break;
904 }
Kumar Gala124b0822008-08-26 15:01:29 -0500905 }
Joakim Tjernlund6dc192d2015-10-14 16:32:00 +0200906 sr_ie = popts->self_refresh_interrupt_en;
York Sun6db4fdd2018-01-29 09:44:35 -0800907 num_pr = popts->package_3ds + 1;
Kumar Gala124b0822008-08-26 15:01:29 -0500908
909 /*
910 * 8572 manual says
911 * {TIMING_CFG_1[PRETOACT]
912 * + [DDR_SDRAM_CFG_2[NUM_PR]
913 * * ({EXT_REFREC || REFREC} + 8 + 2)]}
914 * << DDR_SDRAM_INTERVAL[REFINT]
915 */
York Sun2896cb72014-03-27 17:54:47 -0700916#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
Priyanka Jain4a717412013-09-25 10:41:19 +0530917 obc_cfg = popts->otf_burst_chop_en;
Dave Liu4be87b22009-03-14 12:48:30 +0800918#else
919 obc_cfg = 0;
920#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500921
York Sun7d69ea32012-10-08 07:44:22 +0000922#if (CONFIG_SYS_FSL_DDR_VER >= FSL_DDR_VER_4_7)
York Sun2c0b62d2015-01-06 13:18:50 -0800923 slow = get_ddr_freq(ctrl_num) < 1249000000;
York Sun7d69ea32012-10-08 07:44:22 +0000924#endif
925
Shengzhou Liu52199442016-03-10 17:36:56 +0800926 if (popts->registered_dimm_en)
York Sunba0c2eb2011-01-10 12:03:00 +0000927 rcw_en = 1;
Shengzhou Liu52199442016-03-10 17:36:56 +0800928
929 /* DDR4 can have address parity for UDIMM and discrete */
Tom Rini364d0022023-01-10 11:19:45 -0500930 if ((CFG_FSL_SDRAM_TYPE != SDRAM_TYPE_DDR4) &&
Shengzhou Liu52199442016-03-10 17:36:56 +0800931 (!popts->registered_dimm_en)) {
York Sunba0c2eb2011-01-10 12:03:00 +0000932 ap_en = 0;
Shengzhou Liu52199442016-03-10 17:36:56 +0800933 } else {
934 ap_en = popts->ap_en;
York Sunba0c2eb2011-01-10 12:03:00 +0000935 }
Kumar Gala124b0822008-08-26 15:01:29 -0500936
York Sun4889c982013-06-25 11:37:47 -0700937 x4_en = popts->x4_en ? 1 : 0;
938
Kumar Gala124b0822008-08-26 15:01:29 -0500939#if defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)
940 /* Use the DDR controller to auto initialize memory. */
Priyanka Jain4a717412013-09-25 10:41:19 +0530941 d_init = popts->ecc_init_using_memctl;
Tom Rinid73175a2022-12-02 16:42:35 -0500942 ddr->ddr_data_init = 0xDEADBEEF;
Kumar Gala124b0822008-08-26 15:01:29 -0500943 debug("DDR: ddr_data_init = 0x%08x\n", ddr->ddr_data_init);
944#else
945 /* Memory will be initialized via DMA, or not at all. */
946 d_init = 0;
947#endif
948
York Sun2896cb72014-03-27 17:54:47 -0700949#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
Dave Liu4be87b22009-03-14 12:48:30 +0800950 md_en = popts->mirrored_dimm;
951#endif
yorkf4f93c62010-07-02 22:25:53 +0000952 qd_en = popts->quad_rank_present ? 1 : 0;
Kumar Gala124b0822008-08-26 15:01:29 -0500953 ddr->ddr_sdram_cfg_2 = (0
954 | ((frc_sr & 0x1) << 31)
955 | ((sr_ie & 0x1) << 30)
York Sun2896cb72014-03-27 17:54:47 -0700956#ifndef CONFIG_SYS_FSL_DDR4
Kumar Gala124b0822008-08-26 15:01:29 -0500957 | ((dll_rst_dis & 0x1) << 29)
958 | ((dqs_cfg & 0x3) << 26)
York Sun2896cb72014-03-27 17:54:47 -0700959#endif
Kumar Gala124b0822008-08-26 15:01:29 -0500960 | ((odt_cfg & 0x3) << 21)
961 | ((num_pr & 0xf) << 12)
York Sun7d69ea32012-10-08 07:44:22 +0000962 | ((slow & 1) << 11)
York Sun4889c982013-06-25 11:37:47 -0700963 | (x4_en << 10)
yorkf4f93c62010-07-02 22:25:53 +0000964 | (qd_en << 9)
York Sunba0c2eb2011-01-10 12:03:00 +0000965 | (unq_mrs_en << 8)
Kumar Gala124b0822008-08-26 15:01:29 -0500966 | ((obc_cfg & 0x1) << 6)
967 | ((ap_en & 0x1) << 5)
968 | ((d_init & 0x1) << 4)
969 | ((rcw_en & 0x1) << 2)
970 | ((md_en & 0x1) << 0)
971 );
Haiying Wangd90e0402008-10-03 12:37:26 -0400972 debug("FSLDDR: ddr_sdram_cfg_2 = 0x%08x\n", ddr->ddr_sdram_cfg_2);
Kumar Gala124b0822008-08-26 15:01:29 -0500973}
974
York Sun2896cb72014-03-27 17:54:47 -0700975#ifdef CONFIG_SYS_FSL_DDR4
Kumar Gala124b0822008-08-26 15:01:29 -0500976/* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
York Sun2c0b62d2015-01-06 13:18:50 -0800977static void set_ddr_sdram_mode_2(const unsigned int ctrl_num,
978 fsl_ddr_cfg_regs_t *ddr,
York Sunba0c2eb2011-01-10 12:03:00 +0000979 const memctl_options_t *popts,
Valentin Longchamp0b810932013-10-18 11:47:20 +0200980 const common_timing_params_t *common_dimm,
York Sunba0c2eb2011-01-10 12:03:00 +0000981 const unsigned int unq_mrs_en)
Kumar Gala124b0822008-08-26 15:01:29 -0500982{
983 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
984 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
York Sun2896cb72014-03-27 17:54:47 -0700985 int i;
986 unsigned int wr_crc = 0; /* Disable */
987 unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */
988 unsigned int srt = 0; /* self-refresh temerature, normal range */
York Sun2c0b62d2015-01-06 13:18:50 -0800989 unsigned int cwl = compute_cas_write_latency(ctrl_num) - 9;
York Sun2896cb72014-03-27 17:54:47 -0700990 unsigned int mpr = 0; /* serial */
991 unsigned int wc_lat;
York Sun2c0b62d2015-01-06 13:18:50 -0800992 const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
Kumar Gala124b0822008-08-26 15:01:29 -0500993
York Sun2896cb72014-03-27 17:54:47 -0700994 if (popts->rtt_override)
995 rtt_wr = popts->rtt_wr_override_value;
996 else
997 rtt_wr = popts->cs_local_opts[0].odt_rtt_wr;
998
999 if (common_dimm->extended_op_srt)
1000 srt = common_dimm->extended_op_srt;
1001
1002 esdmode2 = (0
1003 | ((wr_crc & 0x1) << 12)
1004 | ((rtt_wr & 0x3) << 9)
1005 | ((srt & 0x3) << 6)
1006 | ((cwl & 0x7) << 3));
1007
1008 if (mclk_ps >= 1250)
1009 wc_lat = 0;
1010 else if (mclk_ps >= 833)
1011 wc_lat = 1;
1012 else
1013 wc_lat = 2;
1014
1015 esdmode3 = (0
1016 | ((mpr & 0x3) << 11)
1017 | ((wc_lat & 0x3) << 9));
1018
1019 ddr->ddr_sdram_mode_2 = (0
1020 | ((esdmode2 & 0xFFFF) << 16)
1021 | ((esdmode3 & 0xFFFF) << 0)
1022 );
1023 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
1024
1025 if (unq_mrs_en) { /* unique mode registers are supported */
1026 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1027 if (popts->rtt_override)
1028 rtt_wr = popts->rtt_wr_override_value;
1029 else
1030 rtt_wr = popts->cs_local_opts[i].odt_rtt_wr;
1031
1032 esdmode2 &= 0xF9FF; /* clear bit 10, 9 */
1033 esdmode2 |= (rtt_wr & 0x3) << 9;
1034 switch (i) {
1035 case 1:
1036 ddr->ddr_sdram_mode_4 = (0
1037 | ((esdmode2 & 0xFFFF) << 16)
1038 | ((esdmode3 & 0xFFFF) << 0)
1039 );
1040 break;
1041 case 2:
1042 ddr->ddr_sdram_mode_6 = (0
1043 | ((esdmode2 & 0xFFFF) << 16)
1044 | ((esdmode3 & 0xFFFF) << 0)
1045 );
1046 break;
1047 case 3:
1048 ddr->ddr_sdram_mode_8 = (0
1049 | ((esdmode2 & 0xFFFF) << 16)
1050 | ((esdmode3 & 0xFFFF) << 0)
1051 );
1052 break;
1053 }
1054 }
1055 debug("FSLDDR: ddr_sdram_mode_4 = 0x%08x\n",
1056 ddr->ddr_sdram_mode_4);
1057 debug("FSLDDR: ddr_sdram_mode_6 = 0x%08x\n",
1058 ddr->ddr_sdram_mode_6);
1059 debug("FSLDDR: ddr_sdram_mode_8 = 0x%08x\n",
1060 ddr->ddr_sdram_mode_8);
1061 }
1062}
1063#elif defined(CONFIG_SYS_FSL_DDR3)
1064/* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
York Sun2c0b62d2015-01-06 13:18:50 -08001065static void set_ddr_sdram_mode_2(const unsigned int ctrl_num,
1066 fsl_ddr_cfg_regs_t *ddr,
York Sun2896cb72014-03-27 17:54:47 -07001067 const memctl_options_t *popts,
1068 const common_timing_params_t *common_dimm,
1069 const unsigned int unq_mrs_en)
1070{
1071 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
1072 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
Kumar Gala65b5be22011-01-20 01:53:15 -06001073 int i;
Dave Liu2d0f1252009-12-16 10:24:38 -06001074 unsigned int rtt_wr = 0; /* Rtt_WR - dynamic ODT off */
Dave Liu4be87b22009-03-14 12:48:30 +08001075 unsigned int srt = 0; /* self-refresh temerature, normal range */
1076 unsigned int asr = 0; /* auto self-refresh disable */
York Sun2c0b62d2015-01-06 13:18:50 -08001077 unsigned int cwl = compute_cas_write_latency(ctrl_num) - 5;
Dave Liu4be87b22009-03-14 12:48:30 +08001078 unsigned int pasr = 0; /* partial array self refresh disable */
1079
Dave Liu2d0f1252009-12-16 10:24:38 -06001080 if (popts->rtt_override)
1081 rtt_wr = popts->rtt_wr_override_value;
York Sunba0c2eb2011-01-10 12:03:00 +00001082 else
1083 rtt_wr = popts->cs_local_opts[0].odt_rtt_wr;
Valentin Longchamp0b810932013-10-18 11:47:20 +02001084
1085 if (common_dimm->extended_op_srt)
1086 srt = common_dimm->extended_op_srt;
1087
Dave Liu4be87b22009-03-14 12:48:30 +08001088 esdmode2 = (0
1089 | ((rtt_wr & 0x3) << 9)
1090 | ((srt & 0x1) << 7)
1091 | ((asr & 0x1) << 6)
1092 | ((cwl & 0x7) << 3)
1093 | ((pasr & 0x7) << 0));
Kumar Gala124b0822008-08-26 15:01:29 -05001094 ddr->ddr_sdram_mode_2 = (0
1095 | ((esdmode2 & 0xFFFF) << 16)
1096 | ((esdmode3 & 0xFFFF) << 0)
1097 );
Haiying Wangd90e0402008-10-03 12:37:26 -04001098 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
York Sunba0c2eb2011-01-10 12:03:00 +00001099
York Sunba0c2eb2011-01-10 12:03:00 +00001100 if (unq_mrs_en) { /* unique mode registers are supported */
Kumar Galad5bbe662011-11-09 10:05:10 -06001101 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Sunba0c2eb2011-01-10 12:03:00 +00001102 if (popts->rtt_override)
1103 rtt_wr = popts->rtt_wr_override_value;
1104 else
1105 rtt_wr = popts->cs_local_opts[i].odt_rtt_wr;
1106
1107 esdmode2 &= 0xF9FF; /* clear bit 10, 9 */
1108 esdmode2 |= (rtt_wr & 0x3) << 9;
1109 switch (i) {
1110 case 1:
1111 ddr->ddr_sdram_mode_4 = (0
1112 | ((esdmode2 & 0xFFFF) << 16)
1113 | ((esdmode3 & 0xFFFF) << 0)
1114 );
1115 break;
1116 case 2:
1117 ddr->ddr_sdram_mode_6 = (0
1118 | ((esdmode2 & 0xFFFF) << 16)
1119 | ((esdmode3 & 0xFFFF) << 0)
1120 );
1121 break;
1122 case 3:
1123 ddr->ddr_sdram_mode_8 = (0
1124 | ((esdmode2 & 0xFFFF) << 16)
1125 | ((esdmode3 & 0xFFFF) << 0)
1126 );
1127 break;
1128 }
1129 }
1130 debug("FSLDDR: ddr_sdram_mode_4 = 0x%08x\n",
1131 ddr->ddr_sdram_mode_4);
1132 debug("FSLDDR: ddr_sdram_mode_6 = 0x%08x\n",
1133 ddr->ddr_sdram_mode_6);
1134 debug("FSLDDR: ddr_sdram_mode_8 = 0x%08x\n",
1135 ddr->ddr_sdram_mode_8);
1136 }
York Sun2896cb72014-03-27 17:54:47 -07001137}
1138
1139#else /* for DDR2 and DDR1 */
1140/* DDR SDRAM Mode configuration 2 (DDR_SDRAM_MODE_2) */
York Sun2c0b62d2015-01-06 13:18:50 -08001141static void set_ddr_sdram_mode_2(const unsigned int ctrl_num,
1142 fsl_ddr_cfg_regs_t *ddr,
York Sun2896cb72014-03-27 17:54:47 -07001143 const memctl_options_t *popts,
1144 const common_timing_params_t *common_dimm,
1145 const unsigned int unq_mrs_en)
1146{
1147 unsigned short esdmode2 = 0; /* Extended SDRAM mode 2 */
1148 unsigned short esdmode3 = 0; /* Extended SDRAM mode 3 */
1149
1150 ddr->ddr_sdram_mode_2 = (0
1151 | ((esdmode2 & 0xFFFF) << 16)
1152 | ((esdmode3 & 0xFFFF) << 0)
1153 );
1154 debug("FSLDDR: ddr_sdram_mode_2 = 0x%08x\n", ddr->ddr_sdram_mode_2);
1155}
York Sunba0c2eb2011-01-10 12:03:00 +00001156#endif
York Sun2896cb72014-03-27 17:54:47 -07001157
1158#ifdef CONFIG_SYS_FSL_DDR4
1159/* DDR SDRAM Mode configuration 9 (DDR_SDRAM_MODE_9) */
1160static void set_ddr_sdram_mode_9(fsl_ddr_cfg_regs_t *ddr,
1161 const memctl_options_t *popts,
1162 const common_timing_params_t *common_dimm,
1163 const unsigned int unq_mrs_en)
1164{
1165 int i;
1166 unsigned short esdmode4 = 0; /* Extended SDRAM mode 4 */
1167 unsigned short esdmode5; /* Extended SDRAM mode 5 */
York Sunfc63b282015-03-19 09:30:27 -07001168 int rtt_park = 0;
York Sund1921262015-11-04 10:03:19 -08001169 bool four_cs = false;
Shengzhou Liu52199442016-03-10 17:36:56 +08001170 const unsigned int mclk_ps = get_memory_clk_period_ps(0);
York Sun2896cb72014-03-27 17:54:47 -07001171
York Sund1921262015-11-04 10:03:19 -08001172#if CONFIG_CHIP_SELECTS_PER_CTRL == 4
1173 if ((ddr->cs[0].config & SDRAM_CS_CONFIG_EN) &&
1174 (ddr->cs[1].config & SDRAM_CS_CONFIG_EN) &&
1175 (ddr->cs[2].config & SDRAM_CS_CONFIG_EN) &&
1176 (ddr->cs[3].config & SDRAM_CS_CONFIG_EN))
1177 four_cs = true;
1178#endif
York Sunfc63b282015-03-19 09:30:27 -07001179 if (ddr->cs[0].config & SDRAM_CS_CONFIG_EN) {
1180 esdmode5 = 0x00000500; /* Data mask enable, RTT_PARK CS0 */
York Sund1921262015-11-04 10:03:19 -08001181 rtt_park = four_cs ? 0 : 1;
York Sunfc63b282015-03-19 09:30:27 -07001182 } else {
1183 esdmode5 = 0x00000400; /* Data mask enabled */
1184 }
York Sun2896cb72014-03-27 17:54:47 -07001185
York Sund9f7fa02018-01-29 09:44:33 -08001186 /*
1187 * For DDR3, set C/A latency if address parity is enabled.
1188 * For DDR4, set C/A latency for UDIMM only. For RDIMM the delay is
1189 * handled by register chip and RCW settings.
1190 */
1191 if ((ddr->ddr_sdram_cfg_2 & SDRAM_CFG2_AP_EN) &&
Tom Rini364d0022023-01-10 11:19:45 -05001192 ((CFG_FSL_SDRAM_TYPE != SDRAM_TYPE_DDR4) ||
York Sund9f7fa02018-01-29 09:44:33 -08001193 !popts->registered_dimm_en)) {
Shengzhou Liu52199442016-03-10 17:36:56 +08001194 if (mclk_ps >= 935) {
1195 /* for DDR4-1600/1866/2133 */
1196 esdmode5 |= DDR_MR5_CA_PARITY_LAT_4_CLK;
1197 } else if (mclk_ps >= 833) {
1198 /* for DDR4-2400 */
1199 esdmode5 |= DDR_MR5_CA_PARITY_LAT_5_CLK;
1200 } else {
1201 printf("parity: mclk_ps = %d not supported\n", mclk_ps);
1202 }
1203 }
1204
York Sun2896cb72014-03-27 17:54:47 -07001205 ddr->ddr_sdram_mode_9 = (0
1206 | ((esdmode4 & 0xffff) << 16)
1207 | ((esdmode5 & 0xffff) << 0)
1208 );
York Sun55eb5fa2015-03-19 09:30:26 -07001209
York Sund1921262015-11-04 10:03:19 -08001210 /* Normally only the first enabled CS use 0x500, others use 0x400
1211 * But when four chip-selects are all enabled, all mode registers
1212 * need 0x500 to park.
1213 */
York Sun55eb5fa2015-03-19 09:30:26 -07001214
York Sun6db4fdd2018-01-29 09:44:35 -08001215 debug("FSLDDR: ddr_sdram_mode_9 = 0x%08x\n", ddr->ddr_sdram_mode_9);
York Sun2896cb72014-03-27 17:54:47 -07001216 if (unq_mrs_en) { /* unique mode registers are supported */
1217 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Sunfc63b282015-03-19 09:30:27 -07001218 if (!rtt_park &&
1219 (ddr->cs[i].config & SDRAM_CS_CONFIG_EN)) {
1220 esdmode5 |= 0x00000500; /* RTT_PARK */
York Sund1921262015-11-04 10:03:19 -08001221 rtt_park = four_cs ? 0 : 1;
York Sunfc63b282015-03-19 09:30:27 -07001222 } else {
1223 esdmode5 = 0x00000400;
1224 }
Shengzhou Liu52199442016-03-10 17:36:56 +08001225
York Sund9f7fa02018-01-29 09:44:33 -08001226 if ((ddr->ddr_sdram_cfg_2 & SDRAM_CFG2_AP_EN) &&
Tom Rini364d0022023-01-10 11:19:45 -05001227 ((CFG_FSL_SDRAM_TYPE != SDRAM_TYPE_DDR4) ||
York Sund9f7fa02018-01-29 09:44:33 -08001228 !popts->registered_dimm_en)) {
Shengzhou Liu52199442016-03-10 17:36:56 +08001229 if (mclk_ps >= 935) {
1230 /* for DDR4-1600/1866/2133 */
1231 esdmode5 |= DDR_MR5_CA_PARITY_LAT_4_CLK;
1232 } else if (mclk_ps >= 833) {
1233 /* for DDR4-2400 */
1234 esdmode5 |= DDR_MR5_CA_PARITY_LAT_5_CLK;
1235 } else {
1236 printf("parity: mclk_ps = %d not supported\n",
1237 mclk_ps);
1238 }
1239 }
1240
York Sun2896cb72014-03-27 17:54:47 -07001241 switch (i) {
1242 case 1:
1243 ddr->ddr_sdram_mode_11 = (0
1244 | ((esdmode4 & 0xFFFF) << 16)
1245 | ((esdmode5 & 0xFFFF) << 0)
1246 );
1247 break;
1248 case 2:
1249 ddr->ddr_sdram_mode_13 = (0
1250 | ((esdmode4 & 0xFFFF) << 16)
1251 | ((esdmode5 & 0xFFFF) << 0)
1252 );
1253 break;
1254 case 3:
1255 ddr->ddr_sdram_mode_15 = (0
1256 | ((esdmode4 & 0xFFFF) << 16)
1257 | ((esdmode5 & 0xFFFF) << 0)
1258 );
1259 break;
1260 }
1261 }
1262 debug("FSLDDR: ddr_sdram_mode_11 = 0x%08x\n",
1263 ddr->ddr_sdram_mode_11);
1264 debug("FSLDDR: ddr_sdram_mode_13 = 0x%08x\n",
1265 ddr->ddr_sdram_mode_13);
1266 debug("FSLDDR: ddr_sdram_mode_15 = 0x%08x\n",
1267 ddr->ddr_sdram_mode_15);
1268 }
Kumar Gala124b0822008-08-26 15:01:29 -05001269}
1270
York Sun2896cb72014-03-27 17:54:47 -07001271/* DDR SDRAM Mode configuration 10 (DDR_SDRAM_MODE_10) */
York Sun2c0b62d2015-01-06 13:18:50 -08001272static void set_ddr_sdram_mode_10(const unsigned int ctrl_num,
1273 fsl_ddr_cfg_regs_t *ddr,
York Sun2896cb72014-03-27 17:54:47 -07001274 const memctl_options_t *popts,
1275 const common_timing_params_t *common_dimm,
1276 const unsigned int unq_mrs_en)
1277{
1278 int i;
1279 unsigned short esdmode6 = 0; /* Extended SDRAM mode 6 */
1280 unsigned short esdmode7 = 0; /* Extended SDRAM mode 7 */
York Sun2c0b62d2015-01-06 13:18:50 -08001281 unsigned int tccdl_min = picos_to_mclk(ctrl_num, common_dimm->tccdl_ps);
York Sun2896cb72014-03-27 17:54:47 -07001282
1283 esdmode6 = ((tccdl_min - 4) & 0x7) << 10;
1284
York Sund4d97ef2015-11-04 10:03:18 -08001285 if (popts->ddr_cdr2 & DDR_CDR2_VREF_RANGE_2)
1286 esdmode6 |= 1 << 6; /* Range 2 */
1287
York Sun2896cb72014-03-27 17:54:47 -07001288 ddr->ddr_sdram_mode_10 = (0
1289 | ((esdmode6 & 0xffff) << 16)
1290 | ((esdmode7 & 0xffff) << 0)
1291 );
York Sun6db4fdd2018-01-29 09:44:35 -08001292 debug("FSLDDR: ddr_sdram_mode_10 = 0x%08x\n", ddr->ddr_sdram_mode_10);
York Sun2896cb72014-03-27 17:54:47 -07001293 if (unq_mrs_en) { /* unique mode registers are supported */
1294 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1295 switch (i) {
1296 case 1:
1297 ddr->ddr_sdram_mode_12 = (0
1298 | ((esdmode6 & 0xFFFF) << 16)
1299 | ((esdmode7 & 0xFFFF) << 0)
1300 );
1301 break;
1302 case 2:
1303 ddr->ddr_sdram_mode_14 = (0
1304 | ((esdmode6 & 0xFFFF) << 16)
1305 | ((esdmode7 & 0xFFFF) << 0)
1306 );
1307 break;
1308 case 3:
1309 ddr->ddr_sdram_mode_16 = (0
1310 | ((esdmode6 & 0xFFFF) << 16)
1311 | ((esdmode7 & 0xFFFF) << 0)
1312 );
1313 break;
1314 }
1315 }
1316 debug("FSLDDR: ddr_sdram_mode_12 = 0x%08x\n",
1317 ddr->ddr_sdram_mode_12);
1318 debug("FSLDDR: ddr_sdram_mode_14 = 0x%08x\n",
1319 ddr->ddr_sdram_mode_14);
1320 debug("FSLDDR: ddr_sdram_mode_16 = 0x%08x\n",
1321 ddr->ddr_sdram_mode_16);
1322 }
1323}
1324
1325#endif
1326
Kumar Gala124b0822008-08-26 15:01:29 -05001327/* DDR SDRAM Interval Configuration (DDR_SDRAM_INTERVAL) */
York Sun2c0b62d2015-01-06 13:18:50 -08001328static void set_ddr_sdram_interval(const unsigned int ctrl_num,
1329 fsl_ddr_cfg_regs_t *ddr,
1330 const memctl_options_t *popts,
1331 const common_timing_params_t *common_dimm)
Kumar Gala124b0822008-08-26 15:01:29 -05001332{
1333 unsigned int refint; /* Refresh interval */
1334 unsigned int bstopre; /* Precharge interval */
1335
York Sun2c0b62d2015-01-06 13:18:50 -08001336 refint = picos_to_mclk(ctrl_num, common_dimm->refresh_rate_ps);
Kumar Gala124b0822008-08-26 15:01:29 -05001337
1338 bstopre = popts->bstopre;
1339
1340 /* refint field used 0x3FFF in earlier controllers */
1341 ddr->ddr_sdram_interval = (0
1342 | ((refint & 0xFFFF) << 16)
1343 | ((bstopre & 0x3FFF) << 0)
1344 );
Haiying Wangd90e0402008-10-03 12:37:26 -04001345 debug("FSLDDR: ddr_sdram_interval = 0x%08x\n", ddr->ddr_sdram_interval);
Kumar Gala124b0822008-08-26 15:01:29 -05001346}
1347
York Sun2896cb72014-03-27 17:54:47 -07001348#ifdef CONFIG_SYS_FSL_DDR4
Kumar Gala124b0822008-08-26 15:01:29 -05001349/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
York Sun2c0b62d2015-01-06 13:18:50 -08001350static void set_ddr_sdram_mode(const unsigned int ctrl_num,
1351 fsl_ddr_cfg_regs_t *ddr,
Kumar Gala124b0822008-08-26 15:01:29 -05001352 const memctl_options_t *popts,
1353 const common_timing_params_t *common_dimm,
1354 unsigned int cas_latency,
York Sunba0c2eb2011-01-10 12:03:00 +00001355 unsigned int additive_latency,
1356 const unsigned int unq_mrs_en)
Kumar Gala124b0822008-08-26 15:01:29 -05001357{
York Sun2896cb72014-03-27 17:54:47 -07001358 int i;
Kumar Gala124b0822008-08-26 15:01:29 -05001359 unsigned short esdmode; /* Extended SDRAM mode */
1360 unsigned short sdmode; /* SDRAM mode */
1361
Dave Liu4be87b22009-03-14 12:48:30 +08001362 /* Mode Register - MR1 */
1363 unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */
1364 unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */
1365 unsigned int rtt;
1366 unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */
1367 unsigned int al = 0; /* Posted CAS# additive latency (AL) */
York Sunba0c2eb2011-01-10 12:03:00 +00001368 unsigned int dic = 0; /* Output driver impedance, 40ohm */
York Sun2896cb72014-03-27 17:54:47 -07001369 unsigned int dll_en = 1; /* DLL Enable 1=Enable (Normal),
1370 0=Disable (Test/Debug) */
1371
1372 /* Mode Register - MR0 */
1373 unsigned int wr = 0; /* Write Recovery */
1374 unsigned int dll_rst; /* DLL Reset */
1375 unsigned int mode; /* Normal=0 or Test=1 */
1376 unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
1377 /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
1378 unsigned int bt;
1379 unsigned int bl; /* BL: Burst Length */
1380
1381 unsigned int wr_mclk;
1382 /* DDR4 support WR 10, 12, 14, 16, 18, 20, 24 */
1383 static const u8 wr_table[] = {
1384 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6};
1385 /* DDR4 support CAS 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 22, 24 */
1386 static const u8 cas_latency_table[] = {
1387 0, 1, 2, 3, 4, 5, 6, 7, 8, 8,
1388 9, 9, 10, 10, 11, 11};
1389
1390 if (popts->rtt_override)
1391 rtt = popts->rtt_override_value;
1392 else
1393 rtt = popts->cs_local_opts[0].odt_rtt_norm;
1394
1395 if (additive_latency == (cas_latency - 1))
1396 al = 1;
1397 if (additive_latency == (cas_latency - 2))
1398 al = 2;
1399
1400 if (popts->quad_rank_present)
1401 dic = 1; /* output driver impedance 240/7 ohm */
1402
1403 /*
1404 * The esdmode value will also be used for writing
1405 * MR1 during write leveling for DDR3, although the
1406 * bits specifically related to the write leveling
1407 * scheme will be handled automatically by the DDR
1408 * controller. so we set the wrlvl_en = 0 here.
1409 */
1410 esdmode = (0
1411 | ((qoff & 0x1) << 12)
1412 | ((tdqs_en & 0x1) << 11)
1413 | ((rtt & 0x7) << 8)
1414 | ((wrlvl_en & 0x1) << 7)
1415 | ((al & 0x3) << 3)
1416 | ((dic & 0x3) << 1) /* DIC field is split */
1417 | ((dll_en & 0x1) << 0)
1418 );
1419
1420 /*
1421 * DLL control for precharge PD
1422 * 0=slow exit DLL off (tXPDLL)
1423 * 1=fast exit DLL on (tXP)
1424 */
1425
York Sun2c0b62d2015-01-06 13:18:50 -08001426 wr_mclk = picos_to_mclk(ctrl_num, common_dimm->twr_ps);
York Sun2896cb72014-03-27 17:54:47 -07001427 if (wr_mclk <= 24) {
1428 wr = wr_table[wr_mclk - 10];
1429 } else {
1430 printf("Error: unsupported write recovery for mode register wr_mclk = %d\n",
1431 wr_mclk);
1432 }
1433
1434 dll_rst = 0; /* dll no reset */
1435 mode = 0; /* normal mode */
1436
1437 /* look up table to get the cas latency bits */
1438 if (cas_latency >= 9 && cas_latency <= 24)
1439 caslat = cas_latency_table[cas_latency - 9];
1440 else
1441 printf("Error: unsupported cas latency for mode register\n");
1442
1443 bt = 0; /* Nibble sequential */
1444
1445 switch (popts->burst_length) {
1446 case DDR_BL8:
1447 bl = 0;
1448 break;
1449 case DDR_OTF:
1450 bl = 1;
1451 break;
1452 case DDR_BC4:
1453 bl = 2;
1454 break;
1455 default:
1456 printf("Error: invalid burst length of %u specified. ",
1457 popts->burst_length);
1458 puts("Defaulting to on-the-fly BC4 or BL8 beats.\n");
1459 bl = 1;
1460 break;
1461 }
1462
1463 sdmode = (0
1464 | ((wr & 0x7) << 9)
1465 | ((dll_rst & 0x1) << 8)
1466 | ((mode & 0x1) << 7)
1467 | (((caslat >> 1) & 0x7) << 4)
1468 | ((bt & 0x1) << 3)
1469 | ((caslat & 1) << 2)
1470 | ((bl & 0x3) << 0)
1471 );
1472
1473 ddr->ddr_sdram_mode = (0
1474 | ((esdmode & 0xFFFF) << 16)
1475 | ((sdmode & 0xFFFF) << 0)
1476 );
1477
1478 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
1479
1480 if (unq_mrs_en) { /* unique mode registers are supported */
1481 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
1482 if (popts->rtt_override)
1483 rtt = popts->rtt_override_value;
1484 else
1485 rtt = popts->cs_local_opts[i].odt_rtt_norm;
1486
1487 esdmode &= 0xF8FF; /* clear bit 10,9,8 for rtt */
1488 esdmode |= (rtt & 0x7) << 8;
1489 switch (i) {
1490 case 1:
1491 ddr->ddr_sdram_mode_3 = (0
1492 | ((esdmode & 0xFFFF) << 16)
1493 | ((sdmode & 0xFFFF) << 0)
1494 );
1495 break;
1496 case 2:
1497 ddr->ddr_sdram_mode_5 = (0
1498 | ((esdmode & 0xFFFF) << 16)
1499 | ((sdmode & 0xFFFF) << 0)
1500 );
1501 break;
1502 case 3:
1503 ddr->ddr_sdram_mode_7 = (0
1504 | ((esdmode & 0xFFFF) << 16)
1505 | ((sdmode & 0xFFFF) << 0)
1506 );
1507 break;
1508 }
1509 }
1510 debug("FSLDDR: ddr_sdram_mode_3 = 0x%08x\n",
1511 ddr->ddr_sdram_mode_3);
1512 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1513 ddr->ddr_sdram_mode_5);
1514 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1515 ddr->ddr_sdram_mode_5);
1516 }
1517}
1518
1519#elif defined(CONFIG_SYS_FSL_DDR3)
1520/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
York Sun2c0b62d2015-01-06 13:18:50 -08001521static void set_ddr_sdram_mode(const unsigned int ctrl_num,
1522 fsl_ddr_cfg_regs_t *ddr,
York Sun2896cb72014-03-27 17:54:47 -07001523 const memctl_options_t *popts,
1524 const common_timing_params_t *common_dimm,
1525 unsigned int cas_latency,
1526 unsigned int additive_latency,
1527 const unsigned int unq_mrs_en)
1528{
1529 int i;
1530 unsigned short esdmode; /* Extended SDRAM mode */
1531 unsigned short sdmode; /* SDRAM mode */
1532
1533 /* Mode Register - MR1 */
1534 unsigned int qoff = 0; /* Output buffer enable 0=yes, 1=no */
1535 unsigned int tdqs_en = 0; /* TDQS Enable: 0=no, 1=yes */
1536 unsigned int rtt;
1537 unsigned int wrlvl_en = 0; /* Write level enable: 0=no, 1=yes */
1538 unsigned int al = 0; /* Posted CAS# additive latency (AL) */
1539 unsigned int dic = 0; /* Output driver impedance, 40ohm */
Dave Liu4be87b22009-03-14 12:48:30 +08001540 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
1541 1=Disable (Test/Debug) */
1542
1543 /* Mode Register - MR0 */
1544 unsigned int dll_on; /* DLL control for precharge PD, 0=off, 1=on */
York Sunbad82092012-08-17 08:22:38 +00001545 unsigned int wr = 0; /* Write Recovery */
Dave Liu4be87b22009-03-14 12:48:30 +08001546 unsigned int dll_rst; /* DLL Reset */
1547 unsigned int mode; /* Normal=0 or Test=1 */
1548 unsigned int caslat = 4;/* CAS# latency, default set as 6 cycles */
1549 /* BT: Burst Type (0=Nibble Sequential, 1=Interleaved) */
1550 unsigned int bt;
1551 unsigned int bl; /* BL: Burst Length */
1552
1553 unsigned int wr_mclk;
York Sun3673f2c2011-03-02 14:24:11 -08001554 /*
1555 * DDR_SDRAM_MODE doesn't support 9,11,13,15
1556 * Please refer JEDEC Standard No. 79-3E for Mode Register MR0
1557 * for this table
1558 */
1559 static const u8 wr_table[] = {1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 0, 0};
Dave Liu4be87b22009-03-14 12:48:30 +08001560
Dave Liu4be87b22009-03-14 12:48:30 +08001561 if (popts->rtt_override)
1562 rtt = popts->rtt_override_value;
York Sunba0c2eb2011-01-10 12:03:00 +00001563 else
1564 rtt = popts->cs_local_opts[0].odt_rtt_norm;
Dave Liu4be87b22009-03-14 12:48:30 +08001565
1566 if (additive_latency == (cas_latency - 1))
1567 al = 1;
1568 if (additive_latency == (cas_latency - 2))
1569 al = 2;
1570
York Sunba0c2eb2011-01-10 12:03:00 +00001571 if (popts->quad_rank_present)
1572 dic = 1; /* output driver impedance 240/7 ohm */
1573
Dave Liu4be87b22009-03-14 12:48:30 +08001574 /*
1575 * The esdmode value will also be used for writing
1576 * MR1 during write leveling for DDR3, although the
1577 * bits specifically related to the write leveling
1578 * scheme will be handled automatically by the DDR
1579 * controller. so we set the wrlvl_en = 0 here.
1580 */
1581 esdmode = (0
1582 | ((qoff & 0x1) << 12)
1583 | ((tdqs_en & 0x1) << 11)
Kumar Gala14f2eb12009-09-10 14:54:55 -05001584 | ((rtt & 0x4) << 7) /* rtt field is split */
Dave Liu4be87b22009-03-14 12:48:30 +08001585 | ((wrlvl_en & 0x1) << 7)
Kumar Gala14f2eb12009-09-10 14:54:55 -05001586 | ((rtt & 0x2) << 5) /* rtt field is split */
1587 | ((dic & 0x2) << 4) /* DIC field is split */
Dave Liu4be87b22009-03-14 12:48:30 +08001588 | ((al & 0x3) << 3)
Kumar Gala14f2eb12009-09-10 14:54:55 -05001589 | ((rtt & 0x1) << 2) /* rtt field is split */
Dave Liu4be87b22009-03-14 12:48:30 +08001590 | ((dic & 0x1) << 1) /* DIC field is split */
1591 | ((dll_en & 0x1) << 0)
1592 );
1593
1594 /*
1595 * DLL control for precharge PD
1596 * 0=slow exit DLL off (tXPDLL)
1597 * 1=fast exit DLL on (tXP)
1598 */
1599 dll_on = 1;
York Sun3673f2c2011-03-02 14:24:11 -08001600
York Sun2c0b62d2015-01-06 13:18:50 -08001601 wr_mclk = picos_to_mclk(ctrl_num, common_dimm->twr_ps);
York Sunbad82092012-08-17 08:22:38 +00001602 if (wr_mclk <= 16) {
1603 wr = wr_table[wr_mclk - 5];
1604 } else {
1605 printf("Error: unsupported write recovery for mode register "
1606 "wr_mclk = %d\n", wr_mclk);
1607 }
York Sun3673f2c2011-03-02 14:24:11 -08001608
Dave Liu4be87b22009-03-14 12:48:30 +08001609 dll_rst = 0; /* dll no reset */
1610 mode = 0; /* normal mode */
1611
1612 /* look up table to get the cas latency bits */
York Sunbad82092012-08-17 08:22:38 +00001613 if (cas_latency >= 5 && cas_latency <= 16) {
1614 unsigned char cas_latency_table[] = {
Dave Liu4be87b22009-03-14 12:48:30 +08001615 0x2, /* 5 clocks */
1616 0x4, /* 6 clocks */
1617 0x6, /* 7 clocks */
1618 0x8, /* 8 clocks */
1619 0xa, /* 9 clocks */
1620 0xc, /* 10 clocks */
York Sunbad82092012-08-17 08:22:38 +00001621 0xe, /* 11 clocks */
1622 0x1, /* 12 clocks */
1623 0x3, /* 13 clocks */
1624 0x5, /* 14 clocks */
1625 0x7, /* 15 clocks */
1626 0x9, /* 16 clocks */
Dave Liu4be87b22009-03-14 12:48:30 +08001627 };
1628 caslat = cas_latency_table[cas_latency - 5];
York Sunbad82092012-08-17 08:22:38 +00001629 } else {
1630 printf("Error: unsupported cas latency for mode register\n");
Dave Liu4be87b22009-03-14 12:48:30 +08001631 }
York Sunbad82092012-08-17 08:22:38 +00001632
Dave Liu4be87b22009-03-14 12:48:30 +08001633 bt = 0; /* Nibble sequential */
1634
1635 switch (popts->burst_length) {
1636 case DDR_BL8:
1637 bl = 0;
1638 break;
1639 case DDR_OTF:
1640 bl = 1;
1641 break;
1642 case DDR_BC4:
1643 bl = 2;
1644 break;
1645 default:
1646 printf("Error: invalid burst length of %u specified. "
1647 " Defaulting to on-the-fly BC4 or BL8 beats.\n",
1648 popts->burst_length);
1649 bl = 1;
1650 break;
1651 }
1652
1653 sdmode = (0
1654 | ((dll_on & 0x1) << 12)
1655 | ((wr & 0x7) << 9)
1656 | ((dll_rst & 0x1) << 8)
1657 | ((mode & 0x1) << 7)
1658 | (((caslat >> 1) & 0x7) << 4)
1659 | ((bt & 0x1) << 3)
York Sunbad82092012-08-17 08:22:38 +00001660 | ((caslat & 1) << 2)
Dave Liu4be87b22009-03-14 12:48:30 +08001661 | ((bl & 0x3) << 0)
1662 );
1663
1664 ddr->ddr_sdram_mode = (0
1665 | ((esdmode & 0xFFFF) << 16)
1666 | ((sdmode & 0xFFFF) << 0)
1667 );
1668
1669 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
York Sunba0c2eb2011-01-10 12:03:00 +00001670
1671 if (unq_mrs_en) { /* unique mode registers are supported */
Kumar Galad5bbe662011-11-09 10:05:10 -06001672 for (i = 1; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Sunba0c2eb2011-01-10 12:03:00 +00001673 if (popts->rtt_override)
1674 rtt = popts->rtt_override_value;
1675 else
1676 rtt = popts->cs_local_opts[i].odt_rtt_norm;
1677
1678 esdmode &= 0xFDBB; /* clear bit 9,6,2 */
1679 esdmode |= (0
1680 | ((rtt & 0x4) << 7) /* rtt field is split */
1681 | ((rtt & 0x2) << 5) /* rtt field is split */
1682 | ((rtt & 0x1) << 2) /* rtt field is split */
1683 );
1684 switch (i) {
1685 case 1:
1686 ddr->ddr_sdram_mode_3 = (0
1687 | ((esdmode & 0xFFFF) << 16)
1688 | ((sdmode & 0xFFFF) << 0)
1689 );
1690 break;
1691 case 2:
1692 ddr->ddr_sdram_mode_5 = (0
1693 | ((esdmode & 0xFFFF) << 16)
1694 | ((sdmode & 0xFFFF) << 0)
1695 );
1696 break;
1697 case 3:
1698 ddr->ddr_sdram_mode_7 = (0
1699 | ((esdmode & 0xFFFF) << 16)
1700 | ((sdmode & 0xFFFF) << 0)
1701 );
1702 break;
1703 }
1704 }
1705 debug("FSLDDR: ddr_sdram_mode_3 = 0x%08x\n",
1706 ddr->ddr_sdram_mode_3);
1707 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1708 ddr->ddr_sdram_mode_5);
1709 debug("FSLDDR: ddr_sdram_mode_5 = 0x%08x\n",
1710 ddr->ddr_sdram_mode_5);
1711 }
Dave Liu4be87b22009-03-14 12:48:30 +08001712}
1713
York Sunf0626592013-09-30 09:22:09 -07001714#else /* !CONFIG_SYS_FSL_DDR3 */
Dave Liu4be87b22009-03-14 12:48:30 +08001715
1716/* DDR SDRAM Mode configuration set (DDR_SDRAM_MODE) */
York Sun2c0b62d2015-01-06 13:18:50 -08001717static void set_ddr_sdram_mode(const unsigned int ctrl_num,
1718 fsl_ddr_cfg_regs_t *ddr,
Dave Liu4be87b22009-03-14 12:48:30 +08001719 const memctl_options_t *popts,
1720 const common_timing_params_t *common_dimm,
1721 unsigned int cas_latency,
York Sunba0c2eb2011-01-10 12:03:00 +00001722 unsigned int additive_latency,
1723 const unsigned int unq_mrs_en)
Dave Liu4be87b22009-03-14 12:48:30 +08001724{
1725 unsigned short esdmode; /* Extended SDRAM mode */
1726 unsigned short sdmode; /* SDRAM mode */
1727
Kumar Gala124b0822008-08-26 15:01:29 -05001728 /*
1729 * FIXME: This ought to be pre-calculated in a
1730 * technology-specific routine,
1731 * e.g. compute_DDR2_mode_register(), and then the
1732 * sdmode and esdmode passed in as part of common_dimm.
1733 */
1734
1735 /* Extended Mode Register */
1736 unsigned int mrs = 0; /* Mode Register Set */
1737 unsigned int outputs = 0; /* 0=Enabled, 1=Disabled */
1738 unsigned int rdqs_en = 0; /* RDQS Enable: 0=no, 1=yes */
1739 unsigned int dqs_en = 0; /* DQS# Enable: 0=enable, 1=disable */
1740 unsigned int ocd = 0; /* 0x0=OCD not supported,
1741 0x7=OCD default state */
1742 unsigned int rtt;
1743 unsigned int al; /* Posted CAS# additive latency (AL) */
1744 unsigned int ods = 0; /* Output Drive Strength:
1745 0 = Full strength (18ohm)
1746 1 = Reduced strength (4ohm) */
1747 unsigned int dll_en = 0; /* DLL Enable 0=Enable (Normal),
1748 1=Disable (Test/Debug) */
1749
1750 /* Mode Register (MR) */
1751 unsigned int mr; /* Mode Register Definition */
1752 unsigned int pd; /* Power-Down Mode */
1753 unsigned int wr; /* Write Recovery */
1754 unsigned int dll_res; /* DLL Reset */
1755 unsigned int mode; /* Normal=0 or Test=1 */
Kumar Gala35ad58d2008-09-05 14:40:29 -05001756 unsigned int caslat = 0;/* CAS# latency */
Kumar Gala124b0822008-08-26 15:01:29 -05001757 /* BT: Burst Type (0=Sequential, 1=Interleaved) */
1758 unsigned int bt;
1759 unsigned int bl; /* BL: Burst Length */
1760
Priyanka Jain4a717412013-09-25 10:41:19 +05301761 dqs_en = !popts->dqs_config;
Kumar Gala124b0822008-08-26 15:01:29 -05001762 rtt = fsl_ddr_get_rtt();
1763
1764 al = additive_latency;
1765
1766 esdmode = (0
1767 | ((mrs & 0x3) << 14)
1768 | ((outputs & 0x1) << 12)
1769 | ((rdqs_en & 0x1) << 11)
1770 | ((dqs_en & 0x1) << 10)
1771 | ((ocd & 0x7) << 7)
1772 | ((rtt & 0x2) << 5) /* rtt field is split */
1773 | ((al & 0x7) << 3)
1774 | ((rtt & 0x1) << 2) /* rtt field is split */
1775 | ((ods & 0x1) << 1)
1776 | ((dll_en & 0x1) << 0)
1777 );
1778
1779 mr = 0; /* FIXME: CHECKME */
1780
1781 /*
1782 * 0 = Fast Exit (Normal)
1783 * 1 = Slow Exit (Low Power)
1784 */
1785 pd = 0;
1786
York Sunf0626592013-09-30 09:22:09 -07001787#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala124b0822008-08-26 15:01:29 -05001788 wr = 0; /* Historical */
York Sunf0626592013-09-30 09:22:09 -07001789#elif defined(CONFIG_SYS_FSL_DDR2)
York Sun2c0b62d2015-01-06 13:18:50 -08001790 wr = picos_to_mclk(ctrl_num, common_dimm->twr_ps);
Kumar Gala124b0822008-08-26 15:01:29 -05001791#endif
1792 dll_res = 0;
1793 mode = 0;
1794
York Sunf0626592013-09-30 09:22:09 -07001795#if defined(CONFIG_SYS_FSL_DDR1)
Kumar Gala124b0822008-08-26 15:01:29 -05001796 if (1 <= cas_latency && cas_latency <= 4) {
1797 unsigned char mode_caslat_table[4] = {
1798 0x5, /* 1.5 clocks */
1799 0x2, /* 2.0 clocks */
1800 0x6, /* 2.5 clocks */
1801 0x3 /* 3.0 clocks */
1802 };
Kumar Gala35ad58d2008-09-05 14:40:29 -05001803 caslat = mode_caslat_table[cas_latency - 1];
1804 } else {
1805 printf("Warning: unknown cas_latency %d\n", cas_latency);
Kumar Gala124b0822008-08-26 15:01:29 -05001806 }
York Sunf0626592013-09-30 09:22:09 -07001807#elif defined(CONFIG_SYS_FSL_DDR2)
Kumar Gala124b0822008-08-26 15:01:29 -05001808 caslat = cas_latency;
Kumar Gala124b0822008-08-26 15:01:29 -05001809#endif
1810 bt = 0;
1811
1812 switch (popts->burst_length) {
Dave Liu4be87b22009-03-14 12:48:30 +08001813 case DDR_BL4:
Kumar Gala124b0822008-08-26 15:01:29 -05001814 bl = 2;
1815 break;
Dave Liu4be87b22009-03-14 12:48:30 +08001816 case DDR_BL8:
Kumar Gala124b0822008-08-26 15:01:29 -05001817 bl = 3;
1818 break;
1819 default:
1820 printf("Error: invalid burst length of %u specified. "
1821 " Defaulting to 4 beats.\n",
1822 popts->burst_length);
1823 bl = 2;
1824 break;
1825 }
1826
1827 sdmode = (0
1828 | ((mr & 0x3) << 14)
1829 | ((pd & 0x1) << 12)
1830 | ((wr & 0x7) << 9)
1831 | ((dll_res & 0x1) << 8)
1832 | ((mode & 0x1) << 7)
1833 | ((caslat & 0x7) << 4)
1834 | ((bt & 0x1) << 3)
1835 | ((bl & 0x7) << 0)
1836 );
1837
1838 ddr->ddr_sdram_mode = (0
1839 | ((esdmode & 0xFFFF) << 16)
1840 | ((sdmode & 0xFFFF) << 0)
1841 );
Haiying Wangd90e0402008-10-03 12:37:26 -04001842 debug("FSLDDR: ddr_sdram_mode = 0x%08x\n", ddr->ddr_sdram_mode);
Kumar Gala124b0822008-08-26 15:01:29 -05001843}
Dave Liu4be87b22009-03-14 12:48:30 +08001844#endif
Kumar Gala124b0822008-08-26 15:01:29 -05001845
Kumar Gala124b0822008-08-26 15:01:29 -05001846/*
1847 * DDR SDRAM Clock Control (DDR_SDRAM_CLK_CNTL)
1848 * The old controller on the 8540/60 doesn't have this register.
1849 * Hope it's OK to set it (to 0) anyway.
1850 */
1851static void set_ddr_sdram_clk_cntl(fsl_ddr_cfg_regs_t *ddr,
1852 const memctl_options_t *popts)
1853{
Tom Rini0b730a02021-05-14 21:34:21 -04001854 if (fsl_ddr_get_version(0) >= 0x40701)
Shengzhou Liu3b33dd22016-05-04 10:20:21 +08001855 /* clk_adjust in 5-bits on T-series and LS-series */
Tom Rini0b730a02021-05-14 21:34:21 -04001856 ddr->ddr_sdram_clk_cntl = (popts->clk_adjust & 0x1F) << 22;
1857 else
Shengzhou Liu3b33dd22016-05-04 10:20:21 +08001858 /* clk_adjust in 4-bits on earlier MPC85xx and P-series */
Tom Rini0b730a02021-05-14 21:34:21 -04001859 ddr->ddr_sdram_clk_cntl = (popts->clk_adjust & 0xF) << 23;
Shengzhou Liu3b33dd22016-05-04 10:20:21 +08001860
yorkde879322010-07-02 22:25:55 +00001861 debug("FSLDDR: clk_cntl = 0x%08x\n", ddr->ddr_sdram_clk_cntl);
Kumar Gala124b0822008-08-26 15:01:29 -05001862}
1863
1864/* DDR Initialization Address (DDR_INIT_ADDR) */
1865static void set_ddr_init_addr(fsl_ddr_cfg_regs_t *ddr)
1866{
1867 unsigned int init_addr = 0; /* Initialization address */
1868
1869 ddr->ddr_init_addr = init_addr;
1870}
1871
1872/* DDR Initialization Address (DDR_INIT_EXT_ADDR) */
1873static void set_ddr_init_ext_addr(fsl_ddr_cfg_regs_t *ddr)
1874{
1875 unsigned int uia = 0; /* Use initialization address */
1876 unsigned int init_ext_addr = 0; /* Initialization address */
1877
1878 ddr->ddr_init_ext_addr = (0
1879 | ((uia & 0x1) << 31)
1880 | (init_ext_addr & 0xF)
1881 );
1882}
1883
1884/* DDR SDRAM Timing Configuration 4 (TIMING_CFG_4) */
Dave Liu3525e1a2010-03-05 12:22:00 +08001885static void set_timing_cfg_4(fsl_ddr_cfg_regs_t *ddr,
1886 const memctl_options_t *popts)
Kumar Gala124b0822008-08-26 15:01:29 -05001887{
1888 unsigned int rwt = 0; /* Read-to-write turnaround for same CS */
1889 unsigned int wrt = 0; /* Write-to-read turnaround for same CS */
1890 unsigned int rrt = 0; /* Read-to-read turnaround for same CS */
1891 unsigned int wwt = 0; /* Write-to-write turnaround for same CS */
York Sun77594b32015-11-04 10:03:21 -08001892 unsigned int trwt_mclk = 0; /* ext_rwt */
Kumar Gala124b0822008-08-26 15:01:29 -05001893 unsigned int dll_lock = 0; /* DDR SDRAM DLL Lock Time */
1894
York Sun2896cb72014-03-27 17:54:47 -07001895#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
Dave Liu3525e1a2010-03-05 12:22:00 +08001896 if (popts->burst_length == DDR_BL8) {
1897 /* We set BL/2 for fixed BL8 */
1898 rrt = 0; /* BL/2 clocks */
1899 wwt = 0; /* BL/2 clocks */
1900 } else {
1901 /* We need to set BL/2 + 2 to BC4 and OTF */
1902 rrt = 2; /* BL/2 + 2 clocks */
1903 wwt = 2; /* BL/2 + 2 clocks */
1904 }
York Sun2896cb72014-03-27 17:54:47 -07001905#endif
York Sun2896cb72014-03-27 17:54:47 -07001906#ifdef CONFIG_SYS_FSL_DDR4
1907 dll_lock = 2; /* tDLLK = 1024 clocks */
1908#elif defined(CONFIG_SYS_FSL_DDR3)
Dave Liu4be87b22009-03-14 12:48:30 +08001909 dll_lock = 1; /* tDLLK = 512 clocks from spec */
1910#endif
York Sun77594b32015-11-04 10:03:21 -08001911
1912 if (popts->trwt_override)
1913 trwt_mclk = popts->trwt;
1914
Kumar Gala124b0822008-08-26 15:01:29 -05001915 ddr->timing_cfg_4 = (0
1916 | ((rwt & 0xf) << 28)
1917 | ((wrt & 0xf) << 24)
1918 | ((rrt & 0xf) << 20)
1919 | ((wwt & 0xf) << 16)
York Sun77594b32015-11-04 10:03:21 -08001920 | ((trwt_mclk & 0xc) << 12)
Kumar Gala124b0822008-08-26 15:01:29 -05001921 | (dll_lock & 0x3)
1922 );
Haiying Wangd90e0402008-10-03 12:37:26 -04001923 debug("FSLDDR: timing_cfg_4 = 0x%08x\n", ddr->timing_cfg_4);
Kumar Gala124b0822008-08-26 15:01:29 -05001924}
1925
1926/* DDR SDRAM Timing Configuration 5 (TIMING_CFG_5) */
York Sunba0c2eb2011-01-10 12:03:00 +00001927static void set_timing_cfg_5(fsl_ddr_cfg_regs_t *ddr, unsigned int cas_latency)
Kumar Gala124b0822008-08-26 15:01:29 -05001928{
1929 unsigned int rodt_on = 0; /* Read to ODT on */
1930 unsigned int rodt_off = 0; /* Read to ODT off */
1931 unsigned int wodt_on = 0; /* Write to ODT on */
1932 unsigned int wodt_off = 0; /* Write to ODT off */
1933
York Sun2896cb72014-03-27 17:54:47 -07001934#if defined(CONFIG_SYS_FSL_DDR3) || defined(CONFIG_SYS_FSL_DDR4)
1935 unsigned int wr_lat = ((ddr->timing_cfg_2 & 0x00780000) >> 19) +
1936 ((ddr->timing_cfg_2 & 0x00040000) >> 14);
York Sunba0c2eb2011-01-10 12:03:00 +00001937 /* rodt_on = timing_cfg_1[caslat] - timing_cfg_2[wrlat] + 1 */
York Sun2896cb72014-03-27 17:54:47 -07001938 if (cas_latency >= wr_lat)
1939 rodt_on = cas_latency - wr_lat + 1;
Dave Liu4be87b22009-03-14 12:48:30 +08001940 rodt_off = 4; /* 4 clocks */
york1714e492010-07-02 22:25:56 +00001941 wodt_on = 1; /* 1 clocks */
Dave Liu4be87b22009-03-14 12:48:30 +08001942 wodt_off = 4; /* 4 clocks */
1943#endif
1944
Kumar Gala124b0822008-08-26 15:01:29 -05001945 ddr->timing_cfg_5 = (0
Dave Liu4758d532008-11-21 16:31:29 +08001946 | ((rodt_on & 0x1f) << 24)
1947 | ((rodt_off & 0x7) << 20)
1948 | ((wodt_on & 0x1f) << 12)
1949 | ((wodt_off & 0x7) << 8)
Kumar Gala124b0822008-08-26 15:01:29 -05001950 );
Haiying Wangd90e0402008-10-03 12:37:26 -04001951 debug("FSLDDR: timing_cfg_5 = 0x%08x\n", ddr->timing_cfg_5);
Kumar Gala124b0822008-08-26 15:01:29 -05001952}
1953
York Sun2896cb72014-03-27 17:54:47 -07001954#ifdef CONFIG_SYS_FSL_DDR4
1955static void set_timing_cfg_6(fsl_ddr_cfg_regs_t *ddr)
1956{
1957 unsigned int hs_caslat = 0;
1958 unsigned int hs_wrlat = 0;
1959 unsigned int hs_wrrec = 0;
1960 unsigned int hs_clkadj = 0;
1961 unsigned int hs_wrlvl_start = 0;
1962
1963 ddr->timing_cfg_6 = (0
1964 | ((hs_caslat & 0x1f) << 24)
1965 | ((hs_wrlat & 0x1f) << 19)
1966 | ((hs_wrrec & 0x1f) << 12)
1967 | ((hs_clkadj & 0x1f) << 6)
1968 | ((hs_wrlvl_start & 0x1f) << 0)
1969 );
1970 debug("FSLDDR: timing_cfg_6 = 0x%08x\n", ddr->timing_cfg_6);
1971}
1972
York Sun2c0b62d2015-01-06 13:18:50 -08001973static void set_timing_cfg_7(const unsigned int ctrl_num,
1974 fsl_ddr_cfg_regs_t *ddr,
York Sund9f7fa02018-01-29 09:44:33 -08001975 const memctl_options_t *popts,
York Sun2c0b62d2015-01-06 13:18:50 -08001976 const common_timing_params_t *common_dimm)
York Sun2896cb72014-03-27 17:54:47 -07001977{
1978 unsigned int txpr, tcksre, tcksrx;
Shengzhou Liu52199442016-03-10 17:36:56 +08001979 unsigned int cke_rst, cksre, cksrx, par_lat = 0, cs_to_cmd;
1980 const unsigned int mclk_ps = get_memory_clk_period_ps(ctrl_num);
York Sun2896cb72014-03-27 17:54:47 -07001981
York Sun2c0b62d2015-01-06 13:18:50 -08001982 txpr = max(5U, picos_to_mclk(ctrl_num, common_dimm->trfc1_ps + 10000));
1983 tcksre = max(5U, picos_to_mclk(ctrl_num, 10000));
1984 tcksrx = max(5U, picos_to_mclk(ctrl_num, 10000));
Shengzhou Liu52199442016-03-10 17:36:56 +08001985
York Sund9f7fa02018-01-29 09:44:33 -08001986 if (ddr->ddr_sdram_cfg_2 & SDRAM_CFG2_AP_EN &&
Tom Rini364d0022023-01-10 11:19:45 -05001987 CFG_FSL_SDRAM_TYPE == SDRAM_TYPE_DDR4) {
York Sund9f7fa02018-01-29 09:44:33 -08001988 /* for DDR4 only */
York Sun6db4fdd2018-01-29 09:44:35 -08001989 par_lat = (ddr->ddr_sdram_rcw_2 & 0xf) + 1;
York Sund9f7fa02018-01-29 09:44:33 -08001990 debug("PAR_LAT = %u for mclk_ps = %d\n", par_lat, mclk_ps);
Shengzhou Liu52199442016-03-10 17:36:56 +08001991 }
1992
York Sun2896cb72014-03-27 17:54:47 -07001993 cs_to_cmd = 0;
1994
1995 if (txpr <= 200)
1996 cke_rst = 0;
1997 else if (txpr <= 256)
1998 cke_rst = 1;
1999 else if (txpr <= 512)
2000 cke_rst = 2;
2001 else
2002 cke_rst = 3;
2003
2004 if (tcksre <= 19)
2005 cksre = tcksre - 5;
2006 else
2007 cksre = 15;
2008
2009 if (tcksrx <= 19)
2010 cksrx = tcksrx - 5;
2011 else
2012 cksrx = 15;
2013
2014 ddr->timing_cfg_7 = (0
2015 | ((cke_rst & 0x3) << 28)
2016 | ((cksre & 0xf) << 24)
2017 | ((cksrx & 0xf) << 20)
2018 | ((par_lat & 0xf) << 16)
2019 | ((cs_to_cmd & 0xf) << 4)
2020 );
2021 debug("FSLDDR: timing_cfg_7 = 0x%08x\n", ddr->timing_cfg_7);
2022}
2023
York Sun2c0b62d2015-01-06 13:18:50 -08002024static void set_timing_cfg_8(const unsigned int ctrl_num,
2025 fsl_ddr_cfg_regs_t *ddr,
York Sun2896cb72014-03-27 17:54:47 -07002026 const memctl_options_t *popts,
2027 const common_timing_params_t *common_dimm,
2028 unsigned int cas_latency)
2029{
York Sund9f7fa02018-01-29 09:44:33 -08002030 int rwt_bg, wrt_bg, rrt_bg, wwt_bg;
York Sun2896cb72014-03-27 17:54:47 -07002031 unsigned int acttoact_bg, wrtord_bg, pre_all_rec;
York Sund9f7fa02018-01-29 09:44:33 -08002032 int tccdl = picos_to_mclk(ctrl_num, common_dimm->tccdl_ps);
2033 int wr_lat = ((ddr->timing_cfg_2 & 0x00780000) >> 19) +
2034 ((ddr->timing_cfg_2 & 0x00040000) >> 14);
York Sun2896cb72014-03-27 17:54:47 -07002035
2036 rwt_bg = cas_latency + 2 + 4 - wr_lat;
2037 if (rwt_bg < tccdl)
2038 rwt_bg = tccdl - rwt_bg;
2039 else
2040 rwt_bg = 0;
2041
2042 wrt_bg = wr_lat + 4 + 1 - cas_latency;
2043 if (wrt_bg < tccdl)
2044 wrt_bg = tccdl - wrt_bg;
2045 else
2046 wrt_bg = 0;
2047
2048 if (popts->burst_length == DDR_BL8) {
2049 rrt_bg = tccdl - 4;
2050 wwt_bg = tccdl - 4;
2051 } else {
2052 rrt_bg = tccdl - 2;
York Sun5e526472015-01-06 13:18:52 -08002053 wwt_bg = tccdl - 2;
York Sun2896cb72014-03-27 17:54:47 -07002054 }
2055
York Sun2c0b62d2015-01-06 13:18:50 -08002056 acttoact_bg = picos_to_mclk(ctrl_num, common_dimm->trrdl_ps);
2057 wrtord_bg = max(4U, picos_to_mclk(ctrl_num, 7500));
York Sunf0e4f6d2014-06-26 11:14:44 -07002058 if (popts->otf_burst_chop_en)
2059 wrtord_bg += 2;
2060
York Sun2896cb72014-03-27 17:54:47 -07002061 pre_all_rec = 0;
2062
2063 ddr->timing_cfg_8 = (0
2064 | ((rwt_bg & 0xf) << 28)
2065 | ((wrt_bg & 0xf) << 24)
2066 | ((rrt_bg & 0xf) << 20)
2067 | ((wwt_bg & 0xf) << 16)
2068 | ((acttoact_bg & 0xf) << 12)
2069 | ((wrtord_bg & 0xf) << 8)
2070 | ((pre_all_rec & 0x1f) << 0)
2071 );
2072
2073 debug("FSLDDR: timing_cfg_8 = 0x%08x\n", ddr->timing_cfg_8);
2074}
2075
York Sun6db4fdd2018-01-29 09:44:35 -08002076static void set_timing_cfg_9(const unsigned int ctrl_num,
2077 fsl_ddr_cfg_regs_t *ddr,
2078 const memctl_options_t *popts,
2079 const common_timing_params_t *common_dimm)
York Sun2896cb72014-03-27 17:54:47 -07002080{
York Sun6db4fdd2018-01-29 09:44:35 -08002081 unsigned int refrec_cid_mclk = 0;
2082 unsigned int acttoact_cid_mclk = 0;
2083
2084 if (popts->package_3ds) {
2085 refrec_cid_mclk =
2086 picos_to_mclk(ctrl_num, common_dimm->trfc_slr_ps);
2087 acttoact_cid_mclk = 4U; /* tRRDS_slr */
2088 }
2089
2090 ddr->timing_cfg_9 = (refrec_cid_mclk & 0x3ff) << 16 |
2091 (acttoact_cid_mclk & 0xf) << 8;
2092
York Sun2896cb72014-03-27 17:54:47 -07002093 debug("FSLDDR: timing_cfg_9 = 0x%08x\n", ddr->timing_cfg_9);
2094}
2095
York Suna8b3d522014-09-11 13:32:06 -07002096/* This function needs to be called after set_ddr_sdram_cfg() is called */
York Sun2896cb72014-03-27 17:54:47 -07002097static void set_ddr_dq_mapping(fsl_ddr_cfg_regs_t *ddr,
2098 const dimm_params_t *dimm_params)
2099{
York Suna8b3d522014-09-11 13:32:06 -07002100 unsigned int acc_ecc_en = (ddr->ddr_sdram_cfg >> 2) & 0x1;
York Sunfc63b282015-03-19 09:30:27 -07002101 int i;
York Suna8b3d522014-09-11 13:32:06 -07002102
York Sunfc63b282015-03-19 09:30:27 -07002103 for (i = 0; i < CONFIG_DIMM_SLOTS_PER_CTLR; i++) {
2104 if (dimm_params[i].n_ranks)
2105 break;
2106 }
2107 if (i >= CONFIG_DIMM_SLOTS_PER_CTLR) {
2108 puts("DDR error: no DIMM found!\n");
2109 return;
2110 }
2111
2112 ddr->dq_map_0 = ((dimm_params[i].dq_mapping[0] & 0x3F) << 26) |
2113 ((dimm_params[i].dq_mapping[1] & 0x3F) << 20) |
2114 ((dimm_params[i].dq_mapping[2] & 0x3F) << 14) |
2115 ((dimm_params[i].dq_mapping[3] & 0x3F) << 8) |
2116 ((dimm_params[i].dq_mapping[4] & 0x3F) << 2);
York Sun2896cb72014-03-27 17:54:47 -07002117
York Sunfc63b282015-03-19 09:30:27 -07002118 ddr->dq_map_1 = ((dimm_params[i].dq_mapping[5] & 0x3F) << 26) |
2119 ((dimm_params[i].dq_mapping[6] & 0x3F) << 20) |
2120 ((dimm_params[i].dq_mapping[7] & 0x3F) << 14) |
2121 ((dimm_params[i].dq_mapping[10] & 0x3F) << 8) |
2122 ((dimm_params[i].dq_mapping[11] & 0x3F) << 2);
York Sun2896cb72014-03-27 17:54:47 -07002123
York Sunfc63b282015-03-19 09:30:27 -07002124 ddr->dq_map_2 = ((dimm_params[i].dq_mapping[12] & 0x3F) << 26) |
2125 ((dimm_params[i].dq_mapping[13] & 0x3F) << 20) |
2126 ((dimm_params[i].dq_mapping[14] & 0x3F) << 14) |
2127 ((dimm_params[i].dq_mapping[15] & 0x3F) << 8) |
2128 ((dimm_params[i].dq_mapping[16] & 0x3F) << 2);
York Sun2896cb72014-03-27 17:54:47 -07002129
York Suna8b3d522014-09-11 13:32:06 -07002130 /* dq_map for ECC[4:7] is set to 0 if accumulated ECC is enabled */
York Sunfc63b282015-03-19 09:30:27 -07002131 ddr->dq_map_3 = ((dimm_params[i].dq_mapping[17] & 0x3F) << 26) |
2132 ((dimm_params[i].dq_mapping[8] & 0x3F) << 20) |
York Suna8b3d522014-09-11 13:32:06 -07002133 (acc_ecc_en ? 0 :
York Sunfc63b282015-03-19 09:30:27 -07002134 (dimm_params[i].dq_mapping[9] & 0x3F) << 14) |
2135 dimm_params[i].dq_mapping_ors;
York Sun2896cb72014-03-27 17:54:47 -07002136
2137 debug("FSLDDR: dq_map_0 = 0x%08x\n", ddr->dq_map_0);
2138 debug("FSLDDR: dq_map_1 = 0x%08x\n", ddr->dq_map_1);
2139 debug("FSLDDR: dq_map_2 = 0x%08x\n", ddr->dq_map_2);
2140 debug("FSLDDR: dq_map_3 = 0x%08x\n", ddr->dq_map_3);
2141}
2142static void set_ddr_sdram_cfg_3(fsl_ddr_cfg_regs_t *ddr,
2143 const memctl_options_t *popts)
2144{
2145 int rd_pre;
2146
2147 rd_pre = popts->quad_rank_present ? 1 : 0;
2148
2149 ddr->ddr_sdram_cfg_3 = (rd_pre & 0x1) << 16;
York Sund9f7fa02018-01-29 09:44:33 -08002150 /* Disable MRS on parity error for RDIMMs */
2151 ddr->ddr_sdram_cfg_3 |= popts->registered_dimm_en ? 1 : 0;
York Sun2896cb72014-03-27 17:54:47 -07002152
York Sun6db4fdd2018-01-29 09:44:35 -08002153 if (popts->package_3ds) { /* only 2,4,8 are supported */
2154 if ((popts->package_3ds + 1) & 0x1) {
2155 printf("Error: Unsupported 3DS DIMM with %d die\n",
2156 popts->package_3ds + 1);
2157 } else {
2158 ddr->ddr_sdram_cfg_3 |= ((popts->package_3ds + 1) >> 1)
2159 << 4;
2160 }
2161 }
2162
York Sun2896cb72014-03-27 17:54:47 -07002163 debug("FSLDDR: ddr_sdram_cfg_3 = 0x%08x\n", ddr->ddr_sdram_cfg_3);
2164}
2165#endif /* CONFIG_SYS_FSL_DDR4 */
2166
Kumar Gala124b0822008-08-26 15:01:29 -05002167/* DDR ZQ Calibration Control (DDR_ZQ_CNTL) */
Dave Liu4be87b22009-03-14 12:48:30 +08002168static void set_ddr_zq_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int zq_en)
Kumar Gala124b0822008-08-26 15:01:29 -05002169{
Kumar Gala124b0822008-08-26 15:01:29 -05002170 unsigned int zqinit = 0;/* POR ZQ Calibration Time (tZQinit) */
2171 /* Normal Operation Full Calibration Time (tZQoper) */
2172 unsigned int zqoper = 0;
2173 /* Normal Operation Short Calibration Time (tZQCS) */
2174 unsigned int zqcs = 0;
York Sun2896cb72014-03-27 17:54:47 -07002175#ifdef CONFIG_SYS_FSL_DDR4
2176 unsigned int zqcs_init;
2177#endif
Kumar Gala124b0822008-08-26 15:01:29 -05002178
Dave Liu4be87b22009-03-14 12:48:30 +08002179 if (zq_en) {
York Sun2896cb72014-03-27 17:54:47 -07002180#ifdef CONFIG_SYS_FSL_DDR4
2181 zqinit = 10; /* 1024 clocks */
2182 zqoper = 9; /* 512 clocks */
2183 zqcs = 7; /* 128 clocks */
2184 zqcs_init = 5; /* 1024 refresh sequences */
2185#else
Dave Liu4be87b22009-03-14 12:48:30 +08002186 zqinit = 9; /* 512 clocks */
2187 zqoper = 8; /* 256 clocks */
2188 zqcs = 6; /* 64 clocks */
York Sun2896cb72014-03-27 17:54:47 -07002189#endif
Dave Liu4be87b22009-03-14 12:48:30 +08002190 }
2191
Kumar Gala124b0822008-08-26 15:01:29 -05002192 ddr->ddr_zq_cntl = (0
2193 | ((zq_en & 0x1) << 31)
2194 | ((zqinit & 0xF) << 24)
2195 | ((zqoper & 0xF) << 16)
2196 | ((zqcs & 0xF) << 8)
York Sun2896cb72014-03-27 17:54:47 -07002197#ifdef CONFIG_SYS_FSL_DDR4
2198 | ((zqcs_init & 0xF) << 0)
2199#endif
Kumar Gala124b0822008-08-26 15:01:29 -05002200 );
York Sunba0c2eb2011-01-10 12:03:00 +00002201 debug("FSLDDR: zq_cntl = 0x%08x\n", ddr->ddr_zq_cntl);
Kumar Gala124b0822008-08-26 15:01:29 -05002202}
2203
2204/* DDR Write Leveling Control (DDR_WRLVL_CNTL) */
Dave Liu64ee7df2009-12-16 10:24:37 -06002205static void set_ddr_wrlvl_cntl(fsl_ddr_cfg_regs_t *ddr, unsigned int wrlvl_en,
2206 const memctl_options_t *popts)
Kumar Gala124b0822008-08-26 15:01:29 -05002207{
Kumar Gala124b0822008-08-26 15:01:29 -05002208 /*
2209 * First DQS pulse rising edge after margining mode
2210 * is programmed (tWL_MRD)
2211 */
2212 unsigned int wrlvl_mrd = 0;
2213 /* ODT delay after margining mode is programmed (tWL_ODTEN) */
2214 unsigned int wrlvl_odten = 0;
2215 /* DQS/DQS_ delay after margining mode is programmed (tWL_DQSEN) */
2216 unsigned int wrlvl_dqsen = 0;
2217 /* WRLVL_SMPL: Write leveling sample time */
2218 unsigned int wrlvl_smpl = 0;
2219 /* WRLVL_WLR: Write leveling repeition time */
2220 unsigned int wrlvl_wlr = 0;
2221 /* WRLVL_START: Write leveling start time */
2222 unsigned int wrlvl_start = 0;
2223
Dave Liu4be87b22009-03-14 12:48:30 +08002224 /* suggest enable write leveling for DDR3 due to fly-by topology */
2225 if (wrlvl_en) {
2226 /* tWL_MRD min = 40 nCK, we set it 64 */
2227 wrlvl_mrd = 0x6;
2228 /* tWL_ODTEN 128 */
2229 wrlvl_odten = 0x7;
2230 /* tWL_DQSEN min = 25 nCK, we set it 32 */
2231 wrlvl_dqsen = 0x5;
2232 /*
Dave Liu64ee7df2009-12-16 10:24:37 -06002233 * Write leveling sample time at least need 6 clocks
2234 * higher than tWLO to allow enough time for progagation
2235 * delay and sampling the prime data bits.
Dave Liu4be87b22009-03-14 12:48:30 +08002236 */
2237 wrlvl_smpl = 0xf;
2238 /*
2239 * Write leveling repetition time
2240 * at least tWLO + 6 clocks clocks
york1714e492010-07-02 22:25:56 +00002241 * we set it 64
Dave Liu4be87b22009-03-14 12:48:30 +08002242 */
york1714e492010-07-02 22:25:56 +00002243 wrlvl_wlr = 0x6;
Dave Liu4be87b22009-03-14 12:48:30 +08002244 /*
2245 * Write leveling start time
2246 * The value use for the DQS_ADJUST for the first sample
York Sunba0c2eb2011-01-10 12:03:00 +00002247 * when write leveling is enabled. It probably needs to be
Robert P. J. Day8d56db92016-07-15 13:44:45 -04002248 * overridden per platform.
Dave Liu4be87b22009-03-14 12:48:30 +08002249 */
2250 wrlvl_start = 0x8;
Dave Liu64ee7df2009-12-16 10:24:37 -06002251 /*
2252 * Override the write leveling sample and start time
2253 * according to specific board
2254 */
2255 if (popts->wrlvl_override) {
2256 wrlvl_smpl = popts->wrlvl_sample;
2257 wrlvl_start = popts->wrlvl_start;
2258 }
Dave Liu4be87b22009-03-14 12:48:30 +08002259 }
2260
Kumar Gala124b0822008-08-26 15:01:29 -05002261 ddr->ddr_wrlvl_cntl = (0
2262 | ((wrlvl_en & 0x1) << 31)
2263 | ((wrlvl_mrd & 0x7) << 24)
2264 | ((wrlvl_odten & 0x7) << 20)
2265 | ((wrlvl_dqsen & 0x7) << 16)
2266 | ((wrlvl_smpl & 0xf) << 12)
2267 | ((wrlvl_wlr & 0x7) << 8)
Dave Liu4758d532008-11-21 16:31:29 +08002268 | ((wrlvl_start & 0x1F) << 0)
Kumar Gala124b0822008-08-26 15:01:29 -05002269 );
York Sunba0c2eb2011-01-10 12:03:00 +00002270 debug("FSLDDR: wrlvl_cntl = 0x%08x\n", ddr->ddr_wrlvl_cntl);
York Sun7d69ea32012-10-08 07:44:22 +00002271 ddr->ddr_wrlvl_cntl_2 = popts->wrlvl_ctl_2;
2272 debug("FSLDDR: wrlvl_cntl_2 = 0x%08x\n", ddr->ddr_wrlvl_cntl_2);
2273 ddr->ddr_wrlvl_cntl_3 = popts->wrlvl_ctl_3;
2274 debug("FSLDDR: wrlvl_cntl_3 = 0x%08x\n", ddr->ddr_wrlvl_cntl_3);
2275
Kumar Gala124b0822008-08-26 15:01:29 -05002276}
2277
2278/* DDR Self Refresh Counter (DDR_SR_CNTR) */
Dave Liu2aad0ae2008-11-21 16:31:35 +08002279static void set_ddr_sr_cntr(fsl_ddr_cfg_regs_t *ddr, unsigned int sr_it)
Kumar Gala124b0822008-08-26 15:01:29 -05002280{
Dave Liu2aad0ae2008-11-21 16:31:35 +08002281 /* Self Refresh Idle Threshold */
Kumar Gala124b0822008-08-26 15:01:29 -05002282 ddr->ddr_sr_cntr = (sr_it & 0xF) << 16;
2283}
2284
york42603722010-07-02 22:25:54 +00002285static void set_ddr_eor(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
2286{
2287 if (popts->addr_hash) {
2288 ddr->ddr_eor = 0x40000000; /* address hash enable */
Kumar Gala4513d762011-03-18 11:53:06 -05002289 puts("Address hashing enabled.\n");
york42603722010-07-02 22:25:54 +00002290 }
2291}
2292
York Sunba0c2eb2011-01-10 12:03:00 +00002293static void set_ddr_cdr1(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
2294{
2295 ddr->ddr_cdr1 = popts->ddr_cdr1;
2296 debug("FSLDDR: ddr_cdr1 = 0x%08x\n", ddr->ddr_cdr1);
2297}
2298
York Sun7d69ea32012-10-08 07:44:22 +00002299static void set_ddr_cdr2(fsl_ddr_cfg_regs_t *ddr, const memctl_options_t *popts)
2300{
2301 ddr->ddr_cdr2 = popts->ddr_cdr2;
2302 debug("FSLDDR: ddr_cdr2 = 0x%08x\n", ddr->ddr_cdr2);
2303}
2304
Kumar Gala124b0822008-08-26 15:01:29 -05002305unsigned int
2306check_fsl_memctl_config_regs(const fsl_ddr_cfg_regs_t *ddr)
2307{
2308 unsigned int res = 0;
2309
2310 /*
2311 * Check that DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] are
2312 * not set at the same time.
2313 */
2314 if (ddr->ddr_sdram_cfg & 0x10000000
2315 && ddr->ddr_sdram_cfg & 0x00008000) {
2316 printf("Error: DDR_SDRAM_CFG[RD_EN] and DDR_SDRAM_CFG[2T_EN] "
2317 " should not be set at the same time.\n");
2318 res++;
2319 }
2320
2321 return res;
2322}
2323
2324unsigned int
York Sun2c0b62d2015-01-06 13:18:50 -08002325compute_fsl_memctl_config_regs(const unsigned int ctrl_num,
2326 const memctl_options_t *popts,
Kumar Gala124b0822008-08-26 15:01:29 -05002327 fsl_ddr_cfg_regs_t *ddr,
2328 const common_timing_params_t *common_dimm,
2329 const dimm_params_t *dimm_params,
Haiying Wang80ad4012010-12-01 10:35:31 -05002330 unsigned int dbw_cap_adj,
2331 unsigned int size_only)
Kumar Gala124b0822008-08-26 15:01:29 -05002332{
2333 unsigned int i;
2334 unsigned int cas_latency;
2335 unsigned int additive_latency;
Dave Liu2aad0ae2008-11-21 16:31:35 +08002336 unsigned int sr_it;
Dave Liu4be87b22009-03-14 12:48:30 +08002337 unsigned int zq_en;
2338 unsigned int wrlvl_en;
York Sunba0c2eb2011-01-10 12:03:00 +00002339 unsigned int ip_rev = 0;
2340 unsigned int unq_mrs_en = 0;
York Sun2927c5e2010-10-18 13:46:50 -07002341 int cs_en = 1;
Kumar Gala124b0822008-08-26 15:01:29 -05002342
2343 memset(ddr, 0, sizeof(fsl_ddr_cfg_regs_t));
2344
2345 if (common_dimm == NULL) {
2346 printf("Error: subset DIMM params struct null pointer\n");
2347 return 1;
2348 }
2349
2350 /*
2351 * Process overrides first.
2352 *
2353 * FIXME: somehow add dereated caslat to this
2354 */
2355 cas_latency = (popts->cas_latency_override)
2356 ? popts->cas_latency_override_value
York Sun2896cb72014-03-27 17:54:47 -07002357 : common_dimm->lowest_common_spd_caslat;
Kumar Gala124b0822008-08-26 15:01:29 -05002358
2359 additive_latency = (popts->additive_latency_override)
2360 ? popts->additive_latency_override_value
2361 : common_dimm->additive_latency;
2362
Dave Liu2aad0ae2008-11-21 16:31:35 +08002363 sr_it = (popts->auto_self_refresh_en)
2364 ? popts->sr_it
2365 : 0;
Dave Liu4be87b22009-03-14 12:48:30 +08002366 /* ZQ calibration */
2367 zq_en = (popts->zq_en) ? 1 : 0;
2368 /* write leveling */
2369 wrlvl_en = (popts->wrlvl_en) ? 1 : 0;
Dave Liu2aad0ae2008-11-21 16:31:35 +08002370
Kumar Gala124b0822008-08-26 15:01:29 -05002371 /* Chip Select Memory Bounds (CSn_BNDS) */
2372 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
York Sune8dc17b2012-08-17 08:22:39 +00002373 unsigned long long ea, sa;
york93799ca2010-07-02 22:25:52 +00002374 unsigned int cs_per_dimm
2375 = CONFIG_CHIP_SELECTS_PER_CTRL / CONFIG_DIMM_SLOTS_PER_CTLR;
2376 unsigned int dimm_number
2377 = i / cs_per_dimm;
2378 unsigned long long rank_density
York Sune8dc17b2012-08-17 08:22:39 +00002379 = dimm_params[dimm_number].rank_density >> dbw_cap_adj;
Haiying Wang272b5962008-10-03 12:36:39 -04002380
york93799ca2010-07-02 22:25:52 +00002381 if (dimm_params[dimm_number].n_ranks == 0) {
Kumar Gala124b0822008-08-26 15:01:29 -05002382 debug("Skipping setup of CS%u "
yorkf4f93c62010-07-02 22:25:53 +00002383 "because n_ranks on DIMM %u is 0\n", i, dimm_number);
Kumar Gala124b0822008-08-26 15:01:29 -05002384 continue;
2385 }
York Sune8dc17b2012-08-17 08:22:39 +00002386 if (popts->memctl_interleaving) {
york93799ca2010-07-02 22:25:52 +00002387 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
York Sune8dc17b2012-08-17 08:22:39 +00002388 case FSL_DDR_CS0_CS1_CS2_CS3:
2389 break;
york93799ca2010-07-02 22:25:52 +00002390 case FSL_DDR_CS0_CS1:
2391 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
York Sun2927c5e2010-10-18 13:46:50 -07002392 if (i > 1)
2393 cs_en = 0;
york93799ca2010-07-02 22:25:52 +00002394 break;
2395 case FSL_DDR_CS2_CS3:
York Sune8dc17b2012-08-17 08:22:39 +00002396 default:
York Sun2927c5e2010-10-18 13:46:50 -07002397 if (i > 0)
2398 cs_en = 0;
york93799ca2010-07-02 22:25:52 +00002399 break;
york93799ca2010-07-02 22:25:52 +00002400 }
York Sune8dc17b2012-08-17 08:22:39 +00002401 sa = common_dimm->base_address;
York Sun98df4d12012-10-08 07:44:23 +00002402 ea = sa + common_dimm->total_mem - 1;
York Sune8dc17b2012-08-17 08:22:39 +00002403 } else if (!popts->memctl_interleaving) {
Kumar Gala124b0822008-08-26 15:01:29 -05002404 /*
2405 * If memory interleaving between controllers is NOT
2406 * enabled, the starting address for each memory
2407 * controller is distinct. However, because rank
2408 * interleaving is enabled, the starting and ending
2409 * addresses of the total memory on that memory
2410 * controller needs to be programmed into its
2411 * respective CS0_BNDS.
2412 */
Haiying Wang272b5962008-10-03 12:36:39 -04002413 switch (popts->ba_intlv_ctl & FSL_DDR_CS0_CS1_CS2_CS3) {
2414 case FSL_DDR_CS0_CS1_CS2_CS3:
Haiying Wang272b5962008-10-03 12:36:39 -04002415 sa = common_dimm->base_address;
York Sun98df4d12012-10-08 07:44:23 +00002416 ea = sa + common_dimm->total_mem - 1;
Haiying Wang272b5962008-10-03 12:36:39 -04002417 break;
2418 case FSL_DDR_CS0_CS1_AND_CS2_CS3:
York Sune8dc17b2012-08-17 08:22:39 +00002419 if ((i >= 2) && (dimm_number == 0)) {
york93799ca2010-07-02 22:25:52 +00002420 sa = dimm_params[dimm_number].base_address +
York Sune8dc17b2012-08-17 08:22:39 +00002421 2 * rank_density;
2422 ea = sa + 2 * rank_density - 1;
york93799ca2010-07-02 22:25:52 +00002423 } else {
2424 sa = dimm_params[dimm_number].base_address;
York Sune8dc17b2012-08-17 08:22:39 +00002425 ea = sa + 2 * rank_density - 1;
Haiying Wang272b5962008-10-03 12:36:39 -04002426 }
2427 break;
2428 case FSL_DDR_CS0_CS1:
york93799ca2010-07-02 22:25:52 +00002429 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
2430 sa = dimm_params[dimm_number].base_address;
York Sune8dc17b2012-08-17 08:22:39 +00002431 ea = sa + rank_density - 1;
2432 if (i != 1)
2433 sa += (i % cs_per_dimm) * rank_density;
2434 ea += (i % cs_per_dimm) * rank_density;
york93799ca2010-07-02 22:25:52 +00002435 } else {
2436 sa = 0;
2437 ea = 0;
2438 }
2439 if (i == 0)
York Sune8dc17b2012-08-17 08:22:39 +00002440 ea += rank_density;
Haiying Wang272b5962008-10-03 12:36:39 -04002441 break;
2442 case FSL_DDR_CS2_CS3:
york93799ca2010-07-02 22:25:52 +00002443 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
2444 sa = dimm_params[dimm_number].base_address;
York Sune8dc17b2012-08-17 08:22:39 +00002445 ea = sa + rank_density - 1;
2446 if (i != 3)
2447 sa += (i % cs_per_dimm) * rank_density;
2448 ea += (i % cs_per_dimm) * rank_density;
york93799ca2010-07-02 22:25:52 +00002449 } else {
2450 sa = 0;
2451 ea = 0;
Haiying Wang272b5962008-10-03 12:36:39 -04002452 }
york93799ca2010-07-02 22:25:52 +00002453 if (i == 2)
2454 ea += (rank_density >> dbw_cap_adj);
Haiying Wang272b5962008-10-03 12:36:39 -04002455 break;
2456 default: /* No bank(chip-select) interleaving */
York Sune8dc17b2012-08-17 08:22:39 +00002457 sa = dimm_params[dimm_number].base_address;
2458 ea = sa + rank_density - 1;
2459 if (dimm_params[dimm_number].n_ranks > (i % cs_per_dimm)) {
2460 sa += (i % cs_per_dimm) * rank_density;
2461 ea += (i % cs_per_dimm) * rank_density;
2462 } else {
2463 sa = 0;
2464 ea = 0;
2465 }
Haiying Wang272b5962008-10-03 12:36:39 -04002466 break;
2467 }
Kumar Gala124b0822008-08-26 15:01:29 -05002468 }
Kumar Gala124b0822008-08-26 15:01:29 -05002469
2470 sa >>= 24;
2471 ea >>= 24;
2472
York Sun98df4d12012-10-08 07:44:23 +00002473 if (cs_en) {
2474 ddr->cs[i].bnds = (0
York Sun63c91cd2013-06-03 12:39:06 -07002475 | ((sa & 0xffff) << 16) /* starting address */
2476 | ((ea & 0xffff) << 0) /* ending address */
York Sun98df4d12012-10-08 07:44:23 +00002477 );
2478 } else {
York Sunc21a7392013-06-25 11:37:45 -07002479 /* setting bnds to 0xffffffff for inactive CS */
2480 ddr->cs[i].bnds = 0xffffffff;
York Sun98df4d12012-10-08 07:44:23 +00002481 }
Kumar Gala124b0822008-08-26 15:01:29 -05002482
Haiying Wangd90e0402008-10-03 12:37:26 -04002483 debug("FSLDDR: cs[%d]_bnds = 0x%08x\n", i, ddr->cs[i].bnds);
York Sun98df4d12012-10-08 07:44:23 +00002484 set_csn_config(dimm_number, i, ddr, popts, dimm_params);
2485 set_csn_config_2(i, ddr);
Kumar Gala124b0822008-08-26 15:01:29 -05002486 }
2487
Haiying Wang80ad4012010-12-01 10:35:31 -05002488 /*
2489 * In the case we only need to compute the ddr sdram size, we only need
2490 * to set csn registers, so return from here.
2491 */
2492 if (size_only)
2493 return 0;
2494
york42603722010-07-02 22:25:54 +00002495 set_ddr_eor(ddr, popts);
2496
York Sunf0626592013-09-30 09:22:09 -07002497#if !defined(CONFIG_SYS_FSL_DDR1)
York Sun2c0b62d2015-01-06 13:18:50 -08002498 set_timing_cfg_0(ctrl_num, ddr, popts, dimm_params);
Kumar Gala124b0822008-08-26 15:01:29 -05002499#endif
2500
York Sun2c0b62d2015-01-06 13:18:50 -08002501 set_timing_cfg_3(ctrl_num, ddr, popts, common_dimm, cas_latency,
York Sun63c91cd2013-06-03 12:39:06 -07002502 additive_latency);
York Sun2c0b62d2015-01-06 13:18:50 -08002503 set_timing_cfg_1(ctrl_num, ddr, popts, common_dimm, cas_latency);
2504 set_timing_cfg_2(ctrl_num, ddr, popts, common_dimm,
2505 cas_latency, additive_latency);
Kumar Gala124b0822008-08-26 15:01:29 -05002506
York Sunba0c2eb2011-01-10 12:03:00 +00002507 set_ddr_cdr1(ddr, popts);
York Sun7d69ea32012-10-08 07:44:22 +00002508 set_ddr_cdr2(ddr, popts);
Kumar Gala124b0822008-08-26 15:01:29 -05002509 set_ddr_sdram_cfg(ddr, popts, common_dimm);
York Sun55eb5fa2015-03-19 09:30:26 -07002510 ip_rev = fsl_ddr_get_version(ctrl_num);
York Sunba0c2eb2011-01-10 12:03:00 +00002511 if (ip_rev > 0x40400)
2512 unq_mrs_en = 1;
Kumar Gala124b0822008-08-26 15:01:29 -05002513
York Suna8b3d522014-09-11 13:32:06 -07002514 if ((ip_rev > 0x40700) && (popts->cswl_override != 0))
York Sune0f60462014-09-05 13:52:43 +08002515 ddr->debug[18] = popts->cswl_override;
2516
York Sun2c0b62d2015-01-06 13:18:50 -08002517 set_ddr_sdram_cfg_2(ctrl_num, ddr, popts, unq_mrs_en);
2518 set_ddr_sdram_mode(ctrl_num, ddr, popts, common_dimm,
2519 cas_latency, additive_latency, unq_mrs_en);
2520 set_ddr_sdram_mode_2(ctrl_num, ddr, popts, common_dimm, unq_mrs_en);
York Sun2896cb72014-03-27 17:54:47 -07002521#ifdef CONFIG_SYS_FSL_DDR4
2522 set_ddr_sdram_mode_9(ddr, popts, common_dimm, unq_mrs_en);
York Sun2c0b62d2015-01-06 13:18:50 -08002523 set_ddr_sdram_mode_10(ctrl_num, ddr, popts, common_dimm, unq_mrs_en);
York Sun2896cb72014-03-27 17:54:47 -07002524#endif
York Sunbc2f32a2018-01-29 10:24:08 -08002525 set_ddr_sdram_rcw(ctrl_num, ddr, popts, common_dimm);
2526
York Sun2c0b62d2015-01-06 13:18:50 -08002527 set_ddr_sdram_interval(ctrl_num, ddr, popts, common_dimm);
Tom Rinid73175a2022-12-02 16:42:35 -05002528 ddr->ddr_data_init = 0xDEADBEEF;
Kumar Gala124b0822008-08-26 15:01:29 -05002529 set_ddr_sdram_clk_cntl(ddr, popts);
2530 set_ddr_init_addr(ddr);
2531 set_ddr_init_ext_addr(ddr);
Dave Liu3525e1a2010-03-05 12:22:00 +08002532 set_timing_cfg_4(ddr, popts);
York Sunba0c2eb2011-01-10 12:03:00 +00002533 set_timing_cfg_5(ddr, cas_latency);
York Sun2896cb72014-03-27 17:54:47 -07002534#ifdef CONFIG_SYS_FSL_DDR4
2535 set_ddr_sdram_cfg_3(ddr, popts);
2536 set_timing_cfg_6(ddr);
York Sund9f7fa02018-01-29 09:44:33 -08002537 set_timing_cfg_7(ctrl_num, ddr, popts, common_dimm);
York Sun2c0b62d2015-01-06 13:18:50 -08002538 set_timing_cfg_8(ctrl_num, ddr, popts, common_dimm, cas_latency);
York Sun6db4fdd2018-01-29 09:44:35 -08002539 set_timing_cfg_9(ctrl_num, ddr, popts, common_dimm);
York Sun2896cb72014-03-27 17:54:47 -07002540 set_ddr_dq_mapping(ddr, dimm_params);
2541#endif
Kumar Gala124b0822008-08-26 15:01:29 -05002542
Dave Liu4be87b22009-03-14 12:48:30 +08002543 set_ddr_zq_cntl(ddr, zq_en);
Dave Liu64ee7df2009-12-16 10:24:37 -06002544 set_ddr_wrlvl_cntl(ddr, wrlvl_en, popts);
Kumar Gala124b0822008-08-26 15:01:29 -05002545
Dave Liu2aad0ae2008-11-21 16:31:35 +08002546 set_ddr_sr_cntr(ddr, sr_it);
Kumar Gala124b0822008-08-26 15:01:29 -05002547
York Sun972cc402013-06-25 11:37:41 -07002548#ifdef CONFIG_SYS_FSL_DDR_EMU
2549 /* disble DDR training for emulator */
2550 ddr->debug[2] = 0x00000400;
York Sun63f57712015-01-06 13:18:45 -08002551 ddr->debug[4] = 0xff800800;
2552 ddr->debug[5] = 0x08000800;
2553 ddr->debug[6] = 0x08000800;
2554 ddr->debug[7] = 0x08000800;
2555 ddr->debug[8] = 0x08000800;
York Sun972cc402013-06-25 11:37:41 -07002556#endif
York Sun99825792014-05-23 13:15:00 -07002557#ifdef CONFIG_SYS_FSL_ERRATUM_A004508
2558 if ((ip_rev >= 0x40000) && (ip_rev < 0x40400))
2559 ddr->debug[2] |= 0x00000200; /* set bit 22 */
2560#endif
2561
Shengzhou Liu15875a52016-11-21 11:36:48 +08002562#ifdef CONFIG_SYS_FSL_ERRATUM_A009942
Shengzhou Liu15875a52016-11-21 11:36:48 +08002563 if (popts->cpo_sample)
2564 ddr->debug[28] = (ddr->debug[28] & 0xffffff00) |
2565 popts->cpo_sample;
2566#endif
2567
Kumar Gala124b0822008-08-26 15:01:29 -05002568 return check_fsl_memctl_config_regs(ddr);
2569}
Shengzhou Liu15875a52016-11-21 11:36:48 +08002570
2571#ifdef CONFIG_SYS_FSL_ERRATUM_A009942
2572/*
2573 * This additional workaround of A009942 checks the condition to determine if
2574 * the CPO value set by the existing A009942 workaround needs to be updated.
2575 * If need, print a warning to prompt user reconfigure DDR debug_29[24:31] with
2576 * expected optimal value, the optimal value is highly board dependent.
2577 */
2578void erratum_a009942_check_cpo(void)
2579{
2580 struct ccsr_ddr __iomem *ddr =
Tom Rini376b88a2022-10-28 20:27:13 -04002581 (struct ccsr_ddr __iomem *)(CFG_SYS_FSL_DDR_ADDR);
Shengzhou Liu15875a52016-11-21 11:36:48 +08002582 u32 cpo, cpo_e, cpo_o, cpo_target, cpo_optimal;
2583 u32 cpo_min = ddr_in32(&ddr->debug[9]) >> 24;
2584 u32 cpo_max = cpo_min;
2585 u32 sdram_cfg, i, tmp, lanes, ddr_type;
2586 bool update_cpo = false, has_ecc = false;
2587
2588 sdram_cfg = ddr_in32(&ddr->sdram_cfg);
2589 if (sdram_cfg & SDRAM_CFG_32_BE)
2590 lanes = 4;
2591 else if (sdram_cfg & SDRAM_CFG_16_BE)
2592 lanes = 2;
2593 else
2594 lanes = 8;
2595
2596 if (sdram_cfg & SDRAM_CFG_ECC_EN)
2597 has_ecc = true;
2598
2599 /* determine the maximum and minimum CPO values */
2600 for (i = 9; i < 9 + lanes / 2; i++) {
2601 cpo = ddr_in32(&ddr->debug[i]);
2602 cpo_e = cpo >> 24;
2603 cpo_o = (cpo >> 8) & 0xff;
2604 tmp = min(cpo_e, cpo_o);
2605 if (tmp < cpo_min)
2606 cpo_min = tmp;
2607 tmp = max(cpo_e, cpo_o);
2608 if (tmp > cpo_max)
2609 cpo_max = tmp;
2610 }
2611
2612 if (has_ecc) {
2613 cpo = ddr_in32(&ddr->debug[13]);
2614 cpo = cpo >> 24;
2615 if (cpo < cpo_min)
2616 cpo_min = cpo;
2617 if (cpo > cpo_max)
2618 cpo_max = cpo;
2619 }
2620
2621 cpo_target = ddr_in32(&ddr->debug[28]) & 0xff;
2622 cpo_optimal = ((cpo_max + cpo_min) >> 1) + 0x27;
2623 debug("cpo_optimal = 0x%x, cpo_target = 0x%x\n", cpo_optimal,
2624 cpo_target);
2625 debug("cpo_max = 0x%x, cpo_min = 0x%x\n", cpo_max, cpo_min);
2626
2627 ddr_type = (sdram_cfg & SDRAM_CFG_SDRAM_TYPE_MASK) >>
2628 SDRAM_CFG_SDRAM_TYPE_SHIFT;
2629 if (ddr_type == SDRAM_TYPE_DDR4)
2630 update_cpo = (cpo_min + 0x3b) < cpo_target ? true : false;
2631 else if (ddr_type == SDRAM_TYPE_DDR3)
2632 update_cpo = (cpo_min + 0x3f) < cpo_target ? true : false;
2633
2634 if (update_cpo) {
2635 printf("WARN: pls set popts->cpo_sample = 0x%x ", cpo_optimal);
2636 printf("in <board>/ddr.c to optimize cpo\n");
2637 }
2638}
2639#endif