blob: b0a61fa2b4162d7644a88aa84ca7ba38e8db9c29 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Kumar Galad5a1fb92008-08-26 21:34:55 -05002/*
Jaiprakash Singhe230a922020-06-02 12:44:02 +05303 * Copyright 2008-2020 Freescale Semiconductor, Inc.
Kumar Galad5a1fb92008-08-26 21:34:55 -05004 */
5
Tom Rini200ba442024-05-01 19:30:40 -06006#include <config.h>
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Kumar Galad5a1fb92008-08-26 21:34:55 -05008#include <asm/io.h>
Tom Rini200ba442024-05-01 19:30:40 -06009#include <asm/ppc.h>
York Sunf0626592013-09-30 09:22:09 -070010#include <fsl_ddr_sdram.h>
York Sun7dda8472011-01-10 12:02:59 +000011#include <asm/processor.h>
Simon Glassdbd79542020-05-10 11:40:11 -060012#include <linux/delay.h>
Kumar Galad5a1fb92008-08-26 21:34:55 -050013
14#if (CONFIG_CHIP_SELECTS_PER_CTRL > 4)
15#error Invalid setting for CONFIG_CHIP_SELECTS_PER_CTRL
16#endif
17
York Sun5e155552013-06-25 11:37:48 -070018/*
19 * regs has the to-be-set values for DDR controller registers
20 * ctrl_num is the DDR controller number
21 * step: 0 goes through the initialization in one pass
22 * 1 sets registers and returns before enabling controller
23 * 2 resumes from step 1 and continues to initialize
24 * Dividing the initialization to two steps to deassert DDR reset signal
25 * to comply with JEDEC specs for RDIMMs.
26 */
Kumar Galad5a1fb92008-08-26 21:34:55 -050027void fsl_ddr_set_memctl_regs(const fsl_ddr_cfg_regs_t *regs,
York Sun5e155552013-06-25 11:37:48 -070028 unsigned int ctrl_num, int step)
Kumar Galad5a1fb92008-08-26 21:34:55 -050029{
York Sun016095d2012-10-08 07:44:24 +000030 unsigned int i, bus_width;
York Suna21803d2013-11-18 10:29:32 -080031 struct ccsr_ddr __iomem *ddr;
Poonam_Aggrwal-b1081230cb1452009-01-04 08:46:38 +053032 u32 temp_sdram_cfg;
York Sun016095d2012-10-08 07:44:24 +000033 u32 total_gb_size_per_controller;
Andy Fleming1bc8b042012-10-22 17:28:18 -050034 int timeout;
York Sunc8fc9592011-01-25 22:05:49 -080035#ifdef CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134
Andy Fleming1bc8b042012-10-22 17:28:18 -050036 int timeout_save;
Tom Rinid5c3bf22022-10-28 20:27:12 -040037 volatile ccsr_local_ecm_t *ecm = (void *)CFG_SYS_MPC85xx_ECM_ADDR;
York Sun7d9781b2011-03-17 11:18:13 -070038 unsigned int csn_bnds_backup = 0, cs_sa, cs_ea, *csn_bnds_t;
39 int csn = -1;
York Sunc8fc9592011-01-25 22:05:49 -080040#endif
York Sun0c88c812014-01-08 13:00:42 -080041#ifdef CONFIG_SYS_FSL_ERRATUM_DDR_A003
42 u32 save1, save2;
43#endif
Jaiprakash Singhe230a922020-06-02 12:44:02 +053044#if defined(CONFIG_SYS_FSL_ERRATUM_A009942) || \
45 (defined(CONFIG_SYS_FSL_ERRATUM_A008378) && \
46 defined(CONFIG_SYS_FSL_DDRC_GEN4)) || \
47 defined(CONFIG_SYS_FSL_ERRATUM_A008109)
48 u32 val32;
49#endif
50#ifdef CONFIG_SYS_FSL_ERRATUM_A009942
51 unsigned int ddr_freq;
52#endif
Kumar Galad5a1fb92008-08-26 21:34:55 -050053
54 switch (ctrl_num) {
55 case 0:
Tom Rini376b88a2022-10-28 20:27:13 -040056 ddr = (void *)CFG_SYS_FSL_DDR_ADDR;
Kumar Galad5a1fb92008-08-26 21:34:55 -050057 break;
Tom Rini376b88a2022-10-28 20:27:13 -040058#if defined(CFG_SYS_FSL_DDR2_ADDR) && (CONFIG_SYS_NUM_DDR_CTLRS > 1)
Kumar Galad5a1fb92008-08-26 21:34:55 -050059 case 1:
Tom Rini376b88a2022-10-28 20:27:13 -040060 ddr = (void *)CFG_SYS_FSL_DDR2_ADDR;
Kumar Galad5a1fb92008-08-26 21:34:55 -050061 break;
York Sune8dc17b2012-08-17 08:22:39 +000062#endif
Tom Rini376b88a2022-10-28 20:27:13 -040063#if defined(CFG_SYS_FSL_DDR3_ADDR) && (CONFIG_SYS_NUM_DDR_CTLRS > 2)
York Sune8dc17b2012-08-17 08:22:39 +000064 case 2:
Tom Rini376b88a2022-10-28 20:27:13 -040065 ddr = (void *)CFG_SYS_FSL_DDR3_ADDR;
York Sune8dc17b2012-08-17 08:22:39 +000066 break;
67#endif
York Sunfe845072016-12-28 08:43:45 -080068#if defined(CONFIG_SYS_FSL_DDR4_ADDR) && (CONFIG_SYS_NUM_DDR_CTLRS > 3)
York Sune8dc17b2012-08-17 08:22:39 +000069 case 3:
York Sunf0626592013-09-30 09:22:09 -070070 ddr = (void *)CONFIG_SYS_FSL_DDR4_ADDR;
York Sune8dc17b2012-08-17 08:22:39 +000071 break;
72#endif
Kumar Galad5a1fb92008-08-26 21:34:55 -050073 default:
74 printf("%s unexpected ctrl_num = %u\n", __FUNCTION__, ctrl_num);
75 return;
76 }
77
York Sun5e155552013-06-25 11:37:48 -070078 if (step == 2)
79 goto step2;
80
York Sun016095d2012-10-08 07:44:24 +000081 if (regs->ddr_eor)
82 out_be32(&ddr->eor, regs->ddr_eor);
York Sun7d9781b2011-03-17 11:18:13 -070083#ifdef CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134
York Sunb513d9d2012-08-17 08:22:36 +000084 debug("Workaround for ERRATUM_DDR111_DDR134\n");
York Sun7d9781b2011-03-17 11:18:13 -070085 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
86 cs_sa = (regs->cs[i].bnds >> 16) & 0xfff;
87 cs_ea = regs->cs[i].bnds & 0xfff;
88 if ((cs_sa <= 0xff) && (cs_ea >= 0xff)) {
89 csn = i;
90 csn_bnds_backup = regs->cs[i].bnds;
91 csn_bnds_t = (unsigned int *) &regs->cs[i].bnds;
York Sun4b736b82012-05-21 08:43:11 +000092 if (cs_ea > 0xeff)
93 *csn_bnds_t = regs->cs[i].bnds + 0x01000000;
94 else
95 *csn_bnds_t = regs->cs[i].bnds + 0x01000100;
York Sun7d9781b2011-03-17 11:18:13 -070096 debug("Found cs%d_bns (0x%08x) covering 0xff000000, "
97 "change it to 0x%x\n",
98 csn, csn_bnds_backup, regs->cs[i].bnds);
99 break;
100 }
101 }
102#endif
Kumar Galad5a1fb92008-08-26 21:34:55 -0500103 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
104 if (i == 0) {
105 out_be32(&ddr->cs0_bnds, regs->cs[i].bnds);
106 out_be32(&ddr->cs0_config, regs->cs[i].config);
107 out_be32(&ddr->cs0_config_2, regs->cs[i].config_2);
108
109 } else if (i == 1) {
110 out_be32(&ddr->cs1_bnds, regs->cs[i].bnds);
111 out_be32(&ddr->cs1_config, regs->cs[i].config);
112 out_be32(&ddr->cs1_config_2, regs->cs[i].config_2);
113
114 } else if (i == 2) {
115 out_be32(&ddr->cs2_bnds, regs->cs[i].bnds);
116 out_be32(&ddr->cs2_config, regs->cs[i].config);
117 out_be32(&ddr->cs2_config_2, regs->cs[i].config_2);
118
119 } else if (i == 3) {
120 out_be32(&ddr->cs3_bnds, regs->cs[i].bnds);
121 out_be32(&ddr->cs3_config, regs->cs[i].config);
122 out_be32(&ddr->cs3_config_2, regs->cs[i].config_2);
123 }
124 }
125
126 out_be32(&ddr->timing_cfg_3, regs->timing_cfg_3);
127 out_be32(&ddr->timing_cfg_0, regs->timing_cfg_0);
128 out_be32(&ddr->timing_cfg_1, regs->timing_cfg_1);
129 out_be32(&ddr->timing_cfg_2, regs->timing_cfg_2);
Kumar Galad5a1fb92008-08-26 21:34:55 -0500130 out_be32(&ddr->sdram_mode, regs->ddr_sdram_mode);
131 out_be32(&ddr->sdram_mode_2, regs->ddr_sdram_mode_2);
York Sunba0c2eb2011-01-10 12:03:00 +0000132 out_be32(&ddr->sdram_mode_3, regs->ddr_sdram_mode_3);
133 out_be32(&ddr->sdram_mode_4, regs->ddr_sdram_mode_4);
134 out_be32(&ddr->sdram_mode_5, regs->ddr_sdram_mode_5);
135 out_be32(&ddr->sdram_mode_6, regs->ddr_sdram_mode_6);
136 out_be32(&ddr->sdram_mode_7, regs->ddr_sdram_mode_7);
137 out_be32(&ddr->sdram_mode_8, regs->ddr_sdram_mode_8);
Kumar Galad5a1fb92008-08-26 21:34:55 -0500138 out_be32(&ddr->sdram_md_cntl, regs->ddr_sdram_md_cntl);
139 out_be32(&ddr->sdram_interval, regs->ddr_sdram_interval);
140 out_be32(&ddr->sdram_data_init, regs->ddr_data_init);
141 out_be32(&ddr->sdram_clk_cntl, regs->ddr_sdram_clk_cntl);
Kumar Galad5a1fb92008-08-26 21:34:55 -0500142 out_be32(&ddr->timing_cfg_4, regs->timing_cfg_4);
143 out_be32(&ddr->timing_cfg_5, regs->timing_cfg_5);
144 out_be32(&ddr->ddr_zq_cntl, regs->ddr_zq_cntl);
145 out_be32(&ddr->ddr_wrlvl_cntl, regs->ddr_wrlvl_cntl);
York Sun972cc402013-06-25 11:37:41 -0700146#ifndef CONFIG_SYS_FSL_DDR_EMU
147 /*
148 * Skip these two registers if running on emulator
149 * because emulator doesn't have skew between bytes.
150 */
151
York Sun7d69ea32012-10-08 07:44:22 +0000152 if (regs->ddr_wrlvl_cntl_2)
153 out_be32(&ddr->ddr_wrlvl_cntl_2, regs->ddr_wrlvl_cntl_2);
154 if (regs->ddr_wrlvl_cntl_3)
155 out_be32(&ddr->ddr_wrlvl_cntl_3, regs->ddr_wrlvl_cntl_3);
York Sun972cc402013-06-25 11:37:41 -0700156#endif
York Sun7d69ea32012-10-08 07:44:22 +0000157
Kumar Galad5a1fb92008-08-26 21:34:55 -0500158 out_be32(&ddr->ddr_sr_cntr, regs->ddr_sr_cntr);
159 out_be32(&ddr->ddr_sdram_rcw_1, regs->ddr_sdram_rcw_1);
160 out_be32(&ddr->ddr_sdram_rcw_2, regs->ddr_sdram_rcw_2);
York Sun7dda8472011-01-10 12:02:59 +0000161 out_be32(&ddr->ddr_cdr1, regs->ddr_cdr1);
Tang Yuantian064f1262014-11-21 11:17:15 +0800162#ifdef CONFIG_DEEP_SLEEP
163 if (is_warm_boot()) {
164 out_be32(&ddr->sdram_cfg_2,
165 regs->ddr_sdram_cfg_2 & ~SDRAM_CFG2_D_INIT);
Tom Rinibb4dd962022-11-16 13:10:37 -0500166 out_be32(&ddr->init_addr, CFG_SYS_SDRAM_BASE);
Tang Yuantian064f1262014-11-21 11:17:15 +0800167 out_be32(&ddr->init_ext_addr, DDR_INIT_ADDR_EXT_UIA);
168
169 /* DRAM VRef will not be trained */
170 out_be32(&ddr->ddr_cdr2,
171 regs->ddr_cdr2 & ~DDR_CDR2_VREF_TRAIN_EN);
172 } else
173#endif
174 {
175 out_be32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2);
176 out_be32(&ddr->init_addr, regs->ddr_init_addr);
177 out_be32(&ddr->init_ext_addr, regs->ddr_init_ext_addr);
178 out_be32(&ddr->ddr_cdr2, regs->ddr_cdr2);
179 }
York Sun7dda8472011-01-10 12:02:59 +0000180 out_be32(&ddr->err_disable, regs->err_disable);
181 out_be32(&ddr->err_int_en, regs->err_int_en);
York Sunb513d9d2012-08-17 08:22:36 +0000182 for (i = 0; i < 32; i++) {
183 if (regs->debug[i]) {
184 debug("Write to debug_%d as %08x\n", i+1, regs->debug[i]);
185 out_be32(&ddr->debug[i], regs->debug[i]);
186 }
187 }
Kumar Galad5a1fb92008-08-26 21:34:55 -0500188
York Sundf2be192011-11-20 10:01:35 -0800189#ifdef CONFIG_SYS_FSL_ERRATUM_DDR_A003474
190 out_be32(&ddr->debug[12], 0x00000015);
191 out_be32(&ddr->debug[21], 0x24000000);
192#endif /* CONFIG_SYS_FSL_ERRATUM_DDR_A003474 */
193
York Sun5e155552013-06-25 11:37:48 -0700194 /*
195 * For RDIMMs, JEDEC spec requires clocks to be stable before reset is
196 * deasserted. Clocks start when any chip select is enabled and clock
197 * control register is set. Because all DDR components are connected to
198 * one reset signal, this needs to be done in two steps. Step 1 is to
199 * get the clocks started. Step 2 resumes after reset signal is
200 * deasserted.
201 */
202 if (step == 1) {
203 udelay(200);
204 return;
205 }
206
207step2:
Ed Swarthoute674b832009-02-24 02:37:59 -0600208 /* Set, but do not enable the memory */
209 temp_sdram_cfg = regs->ddr_sdram_cfg;
Poonam_Aggrwal-b1081230cb1452009-01-04 08:46:38 +0530210 temp_sdram_cfg &= ~(SDRAM_CFG_MEM_EN);
211 out_be32(&ddr->sdram_cfg, temp_sdram_cfg);
York Sun922f40f2011-01-10 12:03:01 +0000212#ifdef CONFIG_SYS_FSL_ERRATUM_DDR_A003
York Sunb513d9d2012-08-17 08:22:36 +0000213 debug("Workaround for ERRATUM_DDR_A003\n");
York Sun922f40f2011-01-10 12:03:01 +0000214 if (regs->ddr_sdram_rcw_2 & 0x00f00000) {
215 out_be32(&ddr->timing_cfg_2, regs->timing_cfg_2 & 0xf07fffff);
216 out_be32(&ddr->debug[2], 0x00000400);
217 out_be32(&ddr->ddr_zq_cntl, regs->ddr_zq_cntl & 0x7fffffff);
218 out_be32(&ddr->ddr_wrlvl_cntl, regs->ddr_wrlvl_cntl & 0x7fffffff);
219 out_be32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2 & 0xffffffeb);
220 out_be32(&ddr->mtcr, 0);
York Sun0c88c812014-01-08 13:00:42 -0800221 save1 = in_be32(&ddr->debug[12]);
222 save2 = in_be32(&ddr->debug[21]);
York Sun922f40f2011-01-10 12:03:01 +0000223 out_be32(&ddr->debug[12], 0x00000015);
224 out_be32(&ddr->debug[21], 0x24000000);
225 out_be32(&ddr->sdram_interval, regs->ddr_sdram_interval & 0xffff);
226 out_be32(&ddr->sdram_cfg, temp_sdram_cfg | SDRAM_CFG_BI | SDRAM_CFG_MEM_EN);
227
228 asm volatile("sync;isync");
229 while (!(in_be32(&ddr->debug[1]) & 0x2))
230 ;
231
232 switch (regs->ddr_sdram_rcw_2 & 0x00f00000) {
233 case 0x00000000:
234 out_be32(&ddr->sdram_md_cntl,
235 MD_CNTL_MD_EN |
236 MD_CNTL_CS_SEL_CS0_CS1 |
237 0x04000000 |
238 MD_CNTL_WRCW |
239 MD_CNTL_MD_VALUE(0x02));
York Sun0c88c812014-01-08 13:00:42 -0800240#if (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
241 if (!(regs->cs[2].config & SDRAM_CS_CONFIG_EN))
242 break;
243 while (in_be32(&ddr->sdram_md_cntl) & MD_CNTL_MD_EN)
244 ;
245 out_be32(&ddr->sdram_md_cntl,
246 MD_CNTL_MD_EN |
247 MD_CNTL_CS_SEL_CS2_CS3 |
248 0x04000000 |
249 MD_CNTL_WRCW |
250 MD_CNTL_MD_VALUE(0x02));
251#endif
York Sun922f40f2011-01-10 12:03:01 +0000252 break;
253 case 0x00100000:
254 out_be32(&ddr->sdram_md_cntl,
255 MD_CNTL_MD_EN |
256 MD_CNTL_CS_SEL_CS0_CS1 |
257 0x04000000 |
258 MD_CNTL_WRCW |
259 MD_CNTL_MD_VALUE(0x0a));
York Sun0c88c812014-01-08 13:00:42 -0800260#if (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
261 if (!(regs->cs[2].config & SDRAM_CS_CONFIG_EN))
262 break;
263 while (in_be32(&ddr->sdram_md_cntl) & MD_CNTL_MD_EN)
264 ;
265 out_be32(&ddr->sdram_md_cntl,
266 MD_CNTL_MD_EN |
267 MD_CNTL_CS_SEL_CS2_CS3 |
268 0x04000000 |
269 MD_CNTL_WRCW |
270 MD_CNTL_MD_VALUE(0x0a));
271#endif
York Sun922f40f2011-01-10 12:03:01 +0000272 break;
273 case 0x00200000:
274 out_be32(&ddr->sdram_md_cntl,
275 MD_CNTL_MD_EN |
276 MD_CNTL_CS_SEL_CS0_CS1 |
277 0x04000000 |
278 MD_CNTL_WRCW |
279 MD_CNTL_MD_VALUE(0x12));
York Sun0c88c812014-01-08 13:00:42 -0800280#if (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
281 if (!(regs->cs[2].config & SDRAM_CS_CONFIG_EN))
282 break;
283 while (in_be32(&ddr->sdram_md_cntl) & MD_CNTL_MD_EN)
284 ;
285 out_be32(&ddr->sdram_md_cntl,
286 MD_CNTL_MD_EN |
287 MD_CNTL_CS_SEL_CS2_CS3 |
288 0x04000000 |
289 MD_CNTL_WRCW |
290 MD_CNTL_MD_VALUE(0x12));
291#endif
York Sun922f40f2011-01-10 12:03:01 +0000292 break;
293 case 0x00300000:
294 out_be32(&ddr->sdram_md_cntl,
295 MD_CNTL_MD_EN |
296 MD_CNTL_CS_SEL_CS0_CS1 |
297 0x04000000 |
298 MD_CNTL_WRCW |
299 MD_CNTL_MD_VALUE(0x1a));
York Sun0c88c812014-01-08 13:00:42 -0800300#if (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
301 if (!(regs->cs[2].config & SDRAM_CS_CONFIG_EN))
302 break;
303 while (in_be32(&ddr->sdram_md_cntl) & MD_CNTL_MD_EN)
304 ;
305 out_be32(&ddr->sdram_md_cntl,
306 MD_CNTL_MD_EN |
307 MD_CNTL_CS_SEL_CS2_CS3 |
308 0x04000000 |
309 MD_CNTL_WRCW |
310 MD_CNTL_MD_VALUE(0x1a));
311#endif
York Sun922f40f2011-01-10 12:03:01 +0000312 break;
313 default:
314 out_be32(&ddr->sdram_md_cntl,
315 MD_CNTL_MD_EN |
316 MD_CNTL_CS_SEL_CS0_CS1 |
317 0x04000000 |
318 MD_CNTL_WRCW |
319 MD_CNTL_MD_VALUE(0x02));
York Sun0c88c812014-01-08 13:00:42 -0800320#if (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
321 if (!(regs->cs[2].config & SDRAM_CS_CONFIG_EN))
322 break;
323 while (in_be32(&ddr->sdram_md_cntl) & MD_CNTL_MD_EN)
324 ;
325 out_be32(&ddr->sdram_md_cntl,
326 MD_CNTL_MD_EN |
327 MD_CNTL_CS_SEL_CS2_CS3 |
328 0x04000000 |
329 MD_CNTL_WRCW |
330 MD_CNTL_MD_VALUE(0x02));
331#endif
York Sun922f40f2011-01-10 12:03:01 +0000332 printf("Unsupported RC10\n");
333 break;
334 }
335
336 while (in_be32(&ddr->sdram_md_cntl) & 0x80000000)
337 ;
338 udelay(6);
339 out_be32(&ddr->sdram_cfg, temp_sdram_cfg);
340 out_be32(&ddr->timing_cfg_2, regs->timing_cfg_2);
341 out_be32(&ddr->debug[2], 0x0);
342 out_be32(&ddr->ddr_zq_cntl, regs->ddr_zq_cntl);
343 out_be32(&ddr->ddr_wrlvl_cntl, regs->ddr_wrlvl_cntl);
344 out_be32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2);
York Sun0c88c812014-01-08 13:00:42 -0800345 out_be32(&ddr->debug[12], save1);
346 out_be32(&ddr->debug[21], save2);
York Sun922f40f2011-01-10 12:03:01 +0000347 out_be32(&ddr->sdram_interval, regs->ddr_sdram_interval);
348
349 }
350#endif
Jaiprakash Singhe230a922020-06-02 12:44:02 +0530351
352#if defined(CONFIG_SYS_FSL_ERRATUM_A008378) && defined(CONFIG_SYS_FSL_DDRC_GEN4)
353 /* Erratum applies when accumulated ECC is used, or DBI is enabled */
354#define IS_ACC_ECC_EN(v) ((v) & 0x4)
355#define IS_DBI(v) ((((v) >> 12) & 0x3) == 0x2)
356 if (has_erratum_a008378()) {
357 if (IS_ACC_ECC_EN(regs->ddr_sdram_cfg) ||
358 IS_DBI(regs->ddr_sdram_cfg_3)) {
359 val32 = ddr_in32(&ddr->debug[28]);
360 val32 |= (0x9 << 20);
361 ddr_out32(&ddr->debug[28], val32);
362 }
363 debug("Applied errata CONFIG_SYS_FSL_ERRATUM_A008378\n");
364 }
365#endif
366
367#if defined(CONFIG_SYS_FSL_ERRATUM_A008109)
368 val32 = in_be32(&ddr->sdram_cfg_2) | 0x800; /* DDR_SLOW */
369 out_be32(&ddr->sdram_cfg_2, val32);
370
371 val32 = in_be32(&ddr->debug[18]) | 0x2;
372 out_be32(&ddr->debug[18], val32);
373
374 out_be32(&ddr->debug[28], 0x30000000);
375 debug("Applied errta CONFIG_SYS_FSL_ERRATUM_A008109\n");
376#endif
377
378#ifdef CONFIG_SYS_FSL_ERRATUM_A009942
379 ddr_freq = get_ddr_freq(ctrl_num) / 1000000;
380 val32 = in_be32(&ddr->debug[28]);
381 val32 &= 0xff0fff00;
382 if (ddr_freq <= 1333)
383 val32 |= 0x0080006a;
384 else if (ddr_freq <= 1600)
385 val32 |= 0x0070006f;
386 else if (ddr_freq <= 1867)
387 val32 |= 0x00700076;
388 else if (ddr_freq <= 2133)
389 val32 |= 0x0060007b;
390
391 out_be32(&ddr->debug[28], val32);
392 debug("Applied errata CONFIG_SYS_FSL_ERRATUM_A009942\n");
393#endif
Kumar Galad5a1fb92008-08-26 21:34:55 -0500394 /*
Dave Liu7dc79f72008-10-23 21:18:53 +0800395 * For 8572 DDR1 erratum - DDR controller may enter illegal state
396 * when operatiing in 32-bit bus mode with 4-beat bursts,
397 * This erratum does not affect DDR3 mode, only for DDR2 mode.
Kumar Galad5a1fb92008-08-26 21:34:55 -0500398 */
York Sun9aa857b2011-01-25 21:51:27 -0800399#ifdef CONFIG_SYS_FSL_ERRATUM_DDR_115
York Sunb513d9d2012-08-17 08:22:36 +0000400 debug("Workaround for ERRATUM_DDR_115\n");
Kumar Galad5a1fb92008-08-26 21:34:55 -0500401 if ((((in_be32(&ddr->sdram_cfg) >> 24) & 0x7) == SDRAM_TYPE_DDR2)
Dave Liu7dc79f72008-10-23 21:18:53 +0800402 && in_be32(&ddr->sdram_cfg) & 0x80000) {
Kumar Galad5a1fb92008-08-26 21:34:55 -0500403 /* set DEBUG_1[31] */
York Sun7dda8472011-01-10 12:02:59 +0000404 setbits_be32(&ddr->debug[0], 1);
Kumar Galad5a1fb92008-08-26 21:34:55 -0500405 }
Dave Liu7dc79f72008-10-23 21:18:53 +0800406#endif
York Sunc8fc9592011-01-25 22:05:49 -0800407#ifdef CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134
York Sunb513d9d2012-08-17 08:22:36 +0000408 debug("Workaround for ERRATUM_DDR111_DDR134\n");
York Sunc8fc9592011-01-25 22:05:49 -0800409 /*
410 * This is the combined workaround for DDR111 and DDR134
411 * following the published errata for MPC8572
412 */
413
414 /* 1. Set EEBACR[3] */
415 setbits_be32(&ecm->eebacr, 0x10000000);
416 debug("Setting EEBACR[3] to 0x%08x\n", in_be32(&ecm->eebacr));
417
418 /* 2. Set DINIT in SDRAM_CFG_2*/
419 setbits_be32(&ddr->sdram_cfg_2, SDRAM_CFG2_D_INIT);
420 debug("Setting sdram_cfg_2[D_INIT] to 0x%08x\n",
421 in_be32(&ddr->sdram_cfg_2));
422
423 /* 3. Set DEBUG_3[21] */
424 setbits_be32(&ddr->debug[2], 0x400);
425 debug("Setting DEBUG_3[21] to 0x%08x\n", in_be32(&ddr->debug[2]));
426
427#endif /* part 1 of the workaound */
Kumar Galad5a1fb92008-08-26 21:34:55 -0500428
429 /*
Dave Liu4be87b22009-03-14 12:48:30 +0800430 * 500 painful micro-seconds must elapse between
Kumar Galad5a1fb92008-08-26 21:34:55 -0500431 * the DDR clock setup and the DDR config enable.
Dave Liu4be87b22009-03-14 12:48:30 +0800432 * DDR2 need 200 us, and DDR3 need 500 us from spec,
433 * we choose the max, that is 500 us for all of case.
Kumar Galad5a1fb92008-08-26 21:34:55 -0500434 */
Dave Liu4be87b22009-03-14 12:48:30 +0800435 udelay(500);
Kumar Galad5a1fb92008-08-26 21:34:55 -0500436 asm volatile("sync;isync");
437
Tang Yuantiana7364af2014-04-17 15:33:46 +0800438#ifdef CONFIG_DEEP_SLEEP
Tang Yuantian064f1262014-11-21 11:17:15 +0800439 if (is_warm_boot()) {
Biwen Li4d5cda62020-04-09 20:44:48 +0800440 /* enter self-refresh */
441 setbits_be32(&ddr->sdram_cfg_2, SDRAM_CFG2_FRC_SR);
Tang Yuantiana7364af2014-04-17 15:33:46 +0800442 /* do board specific memory setup */
443 board_mem_sleep_setup();
Tang Yuantiana7364af2014-04-17 15:33:46 +0800444 temp_sdram_cfg = (in_be32(&ddr->sdram_cfg) | SDRAM_CFG_BI);
Tang Yuantian064f1262014-11-21 11:17:15 +0800445 } else
Tang Yuantiana7364af2014-04-17 15:33:46 +0800446#endif
447 temp_sdram_cfg = (in_be32(&ddr->sdram_cfg) & ~SDRAM_CFG_BI);
Tang Yuantian064f1262014-11-21 11:17:15 +0800448
449 /* Let the controller go */
Poonam_Aggrwal-b1081230cb1452009-01-04 08:46:38 +0530450 out_be32(&ddr->sdram_cfg, temp_sdram_cfg | SDRAM_CFG_MEM_EN);
York Sun922f40f2011-01-10 12:03:01 +0000451 asm volatile("sync;isync");
Kumar Galad5a1fb92008-08-26 21:34:55 -0500452
York Sun016095d2012-10-08 07:44:24 +0000453 total_gb_size_per_controller = 0;
454 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
455 if (!(regs->cs[i].config & 0x80000000))
456 continue;
457 total_gb_size_per_controller += 1 << (
458 ((regs->cs[i].config >> 14) & 0x3) + 2 +
459 ((regs->cs[i].config >> 8) & 0x7) + 12 +
460 ((regs->cs[i].config >> 0) & 0x7) + 8 +
461 3 - ((regs->ddr_sdram_cfg >> 19) & 0x3) -
462 26); /* minus 26 (count of 64M) */
463 }
464 if (fsl_ddr_get_intl3r() & 0x80000000) /* 3-way interleaving */
465 total_gb_size_per_controller *= 3;
466 else if (regs->cs[0].config & 0x20000000) /* 2-way interleaving */
467 total_gb_size_per_controller <<= 1;
468 /*
469 * total memory / bus width = transactions needed
470 * transactions needed / data rate = seconds
471 * to add plenty of buffer, double the time
472 * For example, 2GB on 666MT/s 64-bit bus takes about 402ms
473 * Let's wait for 800ms
474 */
475 bus_width = 3 - ((ddr->sdram_cfg & SDRAM_CFG_DBW_MASK)
476 >> SDRAM_CFG_DBW_SHIFT);
477 timeout = ((total_gb_size_per_controller << (6 - bus_width)) * 100 /
York Sun2c0b62d2015-01-06 13:18:50 -0800478 (get_ddr_freq(ctrl_num) >> 20)) << 1;
Andy Fleming1bc8b042012-10-22 17:28:18 -0500479#ifdef CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134
York Sun016095d2012-10-08 07:44:24 +0000480 timeout_save = timeout;
Andy Fleming1bc8b042012-10-22 17:28:18 -0500481#endif
York Sun016095d2012-10-08 07:44:24 +0000482 total_gb_size_per_controller >>= 4; /* shift down to gb size */
483 debug("total %d GB\n", total_gb_size_per_controller);
484 debug("Need to wait up to %d * 10ms\n", timeout);
485
Kumar Galad5a1fb92008-08-26 21:34:55 -0500486 /* Poll DDR_SDRAM_CFG_2[D_INIT] bit until auto-data init is done. */
York Sun016095d2012-10-08 07:44:24 +0000487 while ((in_be32(&ddr->sdram_cfg_2) & SDRAM_CFG2_D_INIT) &&
488 (timeout >= 0)) {
Kumar Galad5a1fb92008-08-26 21:34:55 -0500489 udelay(10000); /* throttle polling rate */
York Sun016095d2012-10-08 07:44:24 +0000490 timeout--;
491 }
492
493 if (timeout <= 0)
494 printf("Waiting for D_INIT timeout. Memory may not work.\n");
York Sunc8fc9592011-01-25 22:05:49 -0800495
496#ifdef CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134
497 /* continue this workaround */
498
499 /* 4. Clear DEBUG3[21] */
500 clrbits_be32(&ddr->debug[2], 0x400);
501 debug("Clearing D3[21] to 0x%08x\n", in_be32(&ddr->debug[2]));
502
503 /* DDR134 workaround starts */
504 /* A: Clear sdram_cfg_2[odt_cfg] */
505 clrbits_be32(&ddr->sdram_cfg_2, SDRAM_CFG2_ODT_CFG_MASK);
506 debug("Clearing SDRAM_CFG2[ODT_CFG] to 0x%08x\n",
507 in_be32(&ddr->sdram_cfg_2));
508
509 /* B: Set DEBUG1[15] */
510 setbits_be32(&ddr->debug[0], 0x10000);
511 debug("Setting D1[15] to 0x%08x\n", in_be32(&ddr->debug[0]));
512
513 /* C: Set timing_cfg_2[cpo] to 0b11111 */
514 setbits_be32(&ddr->timing_cfg_2, TIMING_CFG_2_CPO_MASK);
515 debug("Setting TMING_CFG_2[CPO] to 0x%08x\n",
516 in_be32(&ddr->timing_cfg_2));
517
518 /* D: Set D6 to 0x9f9f9f9f */
519 out_be32(&ddr->debug[5], 0x9f9f9f9f);
520 debug("Setting D6 to 0x%08x\n", in_be32(&ddr->debug[5]));
521
522 /* E: Set D7 to 0x9f9f9f9f */
523 out_be32(&ddr->debug[6], 0x9f9f9f9f);
524 debug("Setting D7 to 0x%08x\n", in_be32(&ddr->debug[6]));
525
526 /* F: Set D2[20] */
527 setbits_be32(&ddr->debug[1], 0x800);
528 debug("Setting D2[20] to 0x%08x\n", in_be32(&ddr->debug[1]));
529
530 /* G: Poll on D2[20] until cleared */
531 while (in_be32(&ddr->debug[1]) & 0x800)
532 udelay(10000); /* throttle polling rate */
533
534 /* H: Clear D1[15] */
535 clrbits_be32(&ddr->debug[0], 0x10000);
536 debug("Setting D1[15] to 0x%08x\n", in_be32(&ddr->debug[0]));
537
538 /* I: Set sdram_cfg_2[odt_cfg] */
539 setbits_be32(&ddr->sdram_cfg_2,
540 regs->ddr_sdram_cfg_2 & SDRAM_CFG2_ODT_CFG_MASK);
541 debug("Setting sdram_cfg_2 to 0x%08x\n", in_be32(&ddr->sdram_cfg_2));
542
543 /* Continuing with the DDR111 workaround */
544 /* 5. Set D2[21] */
545 setbits_be32(&ddr->debug[1], 0x400);
546 debug("Setting D2[21] to 0x%08x\n", in_be32(&ddr->debug[1]));
547
548 /* 6. Poll D2[21] until its cleared */
549 while (in_be32(&ddr->debug[1]) & 0x400)
550 udelay(10000); /* throttle polling rate */
551
York Sun016095d2012-10-08 07:44:24 +0000552 /* 7. Wait for state machine 2nd run, roughly 400ms/GB */
553 debug("Wait for %d * 10ms\n", timeout_save);
554 udelay(timeout_save * 10000);
York Sunc8fc9592011-01-25 22:05:49 -0800555
556 /* 8. Set sdram_cfg_2[dinit] if options requires */
557 setbits_be32(&ddr->sdram_cfg_2,
558 regs->ddr_sdram_cfg_2 & SDRAM_CFG2_D_INIT);
559 debug("Setting sdram_cfg_2 to 0x%08x\n", in_be32(&ddr->sdram_cfg_2));
560
561 /* 9. Poll until dinit is cleared */
York Sun016095d2012-10-08 07:44:24 +0000562 timeout = timeout_save;
563 debug("Need to wait up to %d * 10ms\n", timeout);
564 while ((in_be32(&ddr->sdram_cfg_2) & SDRAM_CFG2_D_INIT) &&
565 (timeout >= 0)) {
566 udelay(10000); /* throttle polling rate */
567 timeout--;
568 }
569
570 if (timeout <= 0)
571 printf("Waiting for D_INIT timeout. Memory may not work.\n");
York Sunc8fc9592011-01-25 22:05:49 -0800572
573 /* 10. Clear EEBACR[3] */
574 clrbits_be32(&ecm->eebacr, 10000000);
575 debug("Clearing EEBACR[3] to 0x%08x\n", in_be32(&ecm->eebacr));
York Sun7d9781b2011-03-17 11:18:13 -0700576
577 if (csn != -1) {
578 csn_bnds_t = (unsigned int *) &regs->cs[csn].bnds;
579 *csn_bnds_t = csn_bnds_backup;
580 debug("Change cs%d_bnds back to 0x%08x\n",
581 csn, regs->cs[csn].bnds);
582 setbits_be32(&ddr->sdram_cfg, 0x2); /* MEM_HALT */
583 switch (csn) {
584 case 0:
585 out_be32(&ddr->cs0_bnds, regs->cs[csn].bnds);
586 break;
587 case 1:
588 out_be32(&ddr->cs1_bnds, regs->cs[csn].bnds);
589 break;
York Sun2c0b62d2015-01-06 13:18:50 -0800590#if CONFIG_CHIP_SELECTS_PER_CTRL > 2
York Sun7d9781b2011-03-17 11:18:13 -0700591 case 2:
592 out_be32(&ddr->cs2_bnds, regs->cs[csn].bnds);
593 break;
594 case 3:
595 out_be32(&ddr->cs3_bnds, regs->cs[csn].bnds);
596 break;
York Sun2c0b62d2015-01-06 13:18:50 -0800597#endif
York Sun7d9781b2011-03-17 11:18:13 -0700598 }
599 clrbits_be32(&ddr->sdram_cfg, 0x2);
600 }
York Sunc8fc9592011-01-25 22:05:49 -0800601#endif /* CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134 */
Biwen Li4d5cda62020-04-09 20:44:48 +0800602#ifdef CONFIG_DEEP_SLEEP
603 if (is_warm_boot())
604 /* exit self-refresh */
605 clrbits_be32(&ddr->sdram_cfg_2, SDRAM_CFG2_FRC_SR);
606#endif
Kumar Galad5a1fb92008-08-26 21:34:55 -0500607}