blob: a9b085db8c2e3e237755e6c6b661eaf4424af319 [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/*
York Sunb513d9d2012-08-17 08:22:36 +00003 * Copyright 2008-2012 Freescale Semiconductor, Inc.
Kumar Galad5a1fb92008-08-26 21:34:55 -05004 */
5
6#include <common.h>
7#include <asm/io.h>
York Sunf0626592013-09-30 09:22:09 -07008#include <fsl_ddr_sdram.h>
York Sun7dda8472011-01-10 12:02:59 +00009#include <asm/processor.h>
Kumar Galad5a1fb92008-08-26 21:34:55 -050010
11#if (CONFIG_CHIP_SELECTS_PER_CTRL > 4)
12#error Invalid setting for CONFIG_CHIP_SELECTS_PER_CTRL
13#endif
14
York Sun5e155552013-06-25 11:37:48 -070015/*
16 * regs has the to-be-set values for DDR controller registers
17 * ctrl_num is the DDR controller number
18 * step: 0 goes through the initialization in one pass
19 * 1 sets registers and returns before enabling controller
20 * 2 resumes from step 1 and continues to initialize
21 * Dividing the initialization to two steps to deassert DDR reset signal
22 * to comply with JEDEC specs for RDIMMs.
23 */
Kumar Galad5a1fb92008-08-26 21:34:55 -050024void fsl_ddr_set_memctl_regs(const fsl_ddr_cfg_regs_t *regs,
York Sun5e155552013-06-25 11:37:48 -070025 unsigned int ctrl_num, int step)
Kumar Galad5a1fb92008-08-26 21:34:55 -050026{
York Sun016095d2012-10-08 07:44:24 +000027 unsigned int i, bus_width;
York Suna21803d2013-11-18 10:29:32 -080028 struct ccsr_ddr __iomem *ddr;
Poonam_Aggrwal-b1081230cb1452009-01-04 08:46:38 +053029 u32 temp_sdram_cfg;
York Sun016095d2012-10-08 07:44:24 +000030 u32 total_gb_size_per_controller;
Andy Fleming1bc8b042012-10-22 17:28:18 -050031 int timeout;
York Sunc8fc9592011-01-25 22:05:49 -080032#ifdef CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134
Andy Fleming1bc8b042012-10-22 17:28:18 -050033 int timeout_save;
York Sunc8fc9592011-01-25 22:05:49 -080034 volatile ccsr_local_ecm_t *ecm = (void *)CONFIG_SYS_MPC85xx_ECM_ADDR;
York Sun7d9781b2011-03-17 11:18:13 -070035 unsigned int csn_bnds_backup = 0, cs_sa, cs_ea, *csn_bnds_t;
36 int csn = -1;
York Sunc8fc9592011-01-25 22:05:49 -080037#endif
York Sun0c88c812014-01-08 13:00:42 -080038#ifdef CONFIG_SYS_FSL_ERRATUM_DDR_A003
39 u32 save1, save2;
40#endif
Kumar Galad5a1fb92008-08-26 21:34:55 -050041
42 switch (ctrl_num) {
43 case 0:
York Sunf0626592013-09-30 09:22:09 -070044 ddr = (void *)CONFIG_SYS_FSL_DDR_ADDR;
Kumar Galad5a1fb92008-08-26 21:34:55 -050045 break;
York Sunfe845072016-12-28 08:43:45 -080046#if defined(CONFIG_SYS_FSL_DDR2_ADDR) && (CONFIG_SYS_NUM_DDR_CTLRS > 1)
Kumar Galad5a1fb92008-08-26 21:34:55 -050047 case 1:
York Sunf0626592013-09-30 09:22:09 -070048 ddr = (void *)CONFIG_SYS_FSL_DDR2_ADDR;
Kumar Galad5a1fb92008-08-26 21:34:55 -050049 break;
York Sune8dc17b2012-08-17 08:22:39 +000050#endif
York Sunfe845072016-12-28 08:43:45 -080051#if defined(CONFIG_SYS_FSL_DDR3_ADDR) && (CONFIG_SYS_NUM_DDR_CTLRS > 2)
York Sune8dc17b2012-08-17 08:22:39 +000052 case 2:
York Sunf0626592013-09-30 09:22:09 -070053 ddr = (void *)CONFIG_SYS_FSL_DDR3_ADDR;
York Sune8dc17b2012-08-17 08:22:39 +000054 break;
55#endif
York Sunfe845072016-12-28 08:43:45 -080056#if defined(CONFIG_SYS_FSL_DDR4_ADDR) && (CONFIG_SYS_NUM_DDR_CTLRS > 3)
York Sune8dc17b2012-08-17 08:22:39 +000057 case 3:
York Sunf0626592013-09-30 09:22:09 -070058 ddr = (void *)CONFIG_SYS_FSL_DDR4_ADDR;
York Sune8dc17b2012-08-17 08:22:39 +000059 break;
60#endif
Kumar Galad5a1fb92008-08-26 21:34:55 -050061 default:
62 printf("%s unexpected ctrl_num = %u\n", __FUNCTION__, ctrl_num);
63 return;
64 }
65
York Sun5e155552013-06-25 11:37:48 -070066 if (step == 2)
67 goto step2;
68
York Sun016095d2012-10-08 07:44:24 +000069 if (regs->ddr_eor)
70 out_be32(&ddr->eor, regs->ddr_eor);
York Sun7d9781b2011-03-17 11:18:13 -070071#ifdef CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134
York Sunb513d9d2012-08-17 08:22:36 +000072 debug("Workaround for ERRATUM_DDR111_DDR134\n");
York Sun7d9781b2011-03-17 11:18:13 -070073 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
74 cs_sa = (regs->cs[i].bnds >> 16) & 0xfff;
75 cs_ea = regs->cs[i].bnds & 0xfff;
76 if ((cs_sa <= 0xff) && (cs_ea >= 0xff)) {
77 csn = i;
78 csn_bnds_backup = regs->cs[i].bnds;
79 csn_bnds_t = (unsigned int *) &regs->cs[i].bnds;
York Sun4b736b82012-05-21 08:43:11 +000080 if (cs_ea > 0xeff)
81 *csn_bnds_t = regs->cs[i].bnds + 0x01000000;
82 else
83 *csn_bnds_t = regs->cs[i].bnds + 0x01000100;
York Sun7d9781b2011-03-17 11:18:13 -070084 debug("Found cs%d_bns (0x%08x) covering 0xff000000, "
85 "change it to 0x%x\n",
86 csn, csn_bnds_backup, regs->cs[i].bnds);
87 break;
88 }
89 }
90#endif
Kumar Galad5a1fb92008-08-26 21:34:55 -050091 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
92 if (i == 0) {
93 out_be32(&ddr->cs0_bnds, regs->cs[i].bnds);
94 out_be32(&ddr->cs0_config, regs->cs[i].config);
95 out_be32(&ddr->cs0_config_2, regs->cs[i].config_2);
96
97 } else if (i == 1) {
98 out_be32(&ddr->cs1_bnds, regs->cs[i].bnds);
99 out_be32(&ddr->cs1_config, regs->cs[i].config);
100 out_be32(&ddr->cs1_config_2, regs->cs[i].config_2);
101
102 } else if (i == 2) {
103 out_be32(&ddr->cs2_bnds, regs->cs[i].bnds);
104 out_be32(&ddr->cs2_config, regs->cs[i].config);
105 out_be32(&ddr->cs2_config_2, regs->cs[i].config_2);
106
107 } else if (i == 3) {
108 out_be32(&ddr->cs3_bnds, regs->cs[i].bnds);
109 out_be32(&ddr->cs3_config, regs->cs[i].config);
110 out_be32(&ddr->cs3_config_2, regs->cs[i].config_2);
111 }
112 }
113
114 out_be32(&ddr->timing_cfg_3, regs->timing_cfg_3);
115 out_be32(&ddr->timing_cfg_0, regs->timing_cfg_0);
116 out_be32(&ddr->timing_cfg_1, regs->timing_cfg_1);
117 out_be32(&ddr->timing_cfg_2, regs->timing_cfg_2);
Kumar Galad5a1fb92008-08-26 21:34:55 -0500118 out_be32(&ddr->sdram_mode, regs->ddr_sdram_mode);
119 out_be32(&ddr->sdram_mode_2, regs->ddr_sdram_mode_2);
York Sunba0c2eb2011-01-10 12:03:00 +0000120 out_be32(&ddr->sdram_mode_3, regs->ddr_sdram_mode_3);
121 out_be32(&ddr->sdram_mode_4, regs->ddr_sdram_mode_4);
122 out_be32(&ddr->sdram_mode_5, regs->ddr_sdram_mode_5);
123 out_be32(&ddr->sdram_mode_6, regs->ddr_sdram_mode_6);
124 out_be32(&ddr->sdram_mode_7, regs->ddr_sdram_mode_7);
125 out_be32(&ddr->sdram_mode_8, regs->ddr_sdram_mode_8);
Kumar Galad5a1fb92008-08-26 21:34:55 -0500126 out_be32(&ddr->sdram_md_cntl, regs->ddr_sdram_md_cntl);
127 out_be32(&ddr->sdram_interval, regs->ddr_sdram_interval);
128 out_be32(&ddr->sdram_data_init, regs->ddr_data_init);
129 out_be32(&ddr->sdram_clk_cntl, regs->ddr_sdram_clk_cntl);
Kumar Galad5a1fb92008-08-26 21:34:55 -0500130 out_be32(&ddr->timing_cfg_4, regs->timing_cfg_4);
131 out_be32(&ddr->timing_cfg_5, regs->timing_cfg_5);
132 out_be32(&ddr->ddr_zq_cntl, regs->ddr_zq_cntl);
133 out_be32(&ddr->ddr_wrlvl_cntl, regs->ddr_wrlvl_cntl);
York Sun972cc402013-06-25 11:37:41 -0700134#ifndef CONFIG_SYS_FSL_DDR_EMU
135 /*
136 * Skip these two registers if running on emulator
137 * because emulator doesn't have skew between bytes.
138 */
139
York Sun7d69ea32012-10-08 07:44:22 +0000140 if (regs->ddr_wrlvl_cntl_2)
141 out_be32(&ddr->ddr_wrlvl_cntl_2, regs->ddr_wrlvl_cntl_2);
142 if (regs->ddr_wrlvl_cntl_3)
143 out_be32(&ddr->ddr_wrlvl_cntl_3, regs->ddr_wrlvl_cntl_3);
York Sun972cc402013-06-25 11:37:41 -0700144#endif
York Sun7d69ea32012-10-08 07:44:22 +0000145
Kumar Galad5a1fb92008-08-26 21:34:55 -0500146 out_be32(&ddr->ddr_sr_cntr, regs->ddr_sr_cntr);
147 out_be32(&ddr->ddr_sdram_rcw_1, regs->ddr_sdram_rcw_1);
148 out_be32(&ddr->ddr_sdram_rcw_2, regs->ddr_sdram_rcw_2);
York Sun7dda8472011-01-10 12:02:59 +0000149 out_be32(&ddr->ddr_cdr1, regs->ddr_cdr1);
Tang Yuantian064f1262014-11-21 11:17:15 +0800150#ifdef CONFIG_DEEP_SLEEP
151 if (is_warm_boot()) {
152 out_be32(&ddr->sdram_cfg_2,
153 regs->ddr_sdram_cfg_2 & ~SDRAM_CFG2_D_INIT);
154 out_be32(&ddr->init_addr, CONFIG_SYS_SDRAM_BASE);
155 out_be32(&ddr->init_ext_addr, DDR_INIT_ADDR_EXT_UIA);
156
157 /* DRAM VRef will not be trained */
158 out_be32(&ddr->ddr_cdr2,
159 regs->ddr_cdr2 & ~DDR_CDR2_VREF_TRAIN_EN);
160 } else
161#endif
162 {
163 out_be32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2);
164 out_be32(&ddr->init_addr, regs->ddr_init_addr);
165 out_be32(&ddr->init_ext_addr, regs->ddr_init_ext_addr);
166 out_be32(&ddr->ddr_cdr2, regs->ddr_cdr2);
167 }
York Sun7dda8472011-01-10 12:02:59 +0000168 out_be32(&ddr->err_disable, regs->err_disable);
169 out_be32(&ddr->err_int_en, regs->err_int_en);
York Sunb513d9d2012-08-17 08:22:36 +0000170 for (i = 0; i < 32; i++) {
171 if (regs->debug[i]) {
172 debug("Write to debug_%d as %08x\n", i+1, regs->debug[i]);
173 out_be32(&ddr->debug[i], regs->debug[i]);
174 }
175 }
Kumar Galad5a1fb92008-08-26 21:34:55 -0500176
York Sundf2be192011-11-20 10:01:35 -0800177#ifdef CONFIG_SYS_FSL_ERRATUM_DDR_A003474
178 out_be32(&ddr->debug[12], 0x00000015);
179 out_be32(&ddr->debug[21], 0x24000000);
180#endif /* CONFIG_SYS_FSL_ERRATUM_DDR_A003474 */
181
York Sun5e155552013-06-25 11:37:48 -0700182 /*
183 * For RDIMMs, JEDEC spec requires clocks to be stable before reset is
184 * deasserted. Clocks start when any chip select is enabled and clock
185 * control register is set. Because all DDR components are connected to
186 * one reset signal, this needs to be done in two steps. Step 1 is to
187 * get the clocks started. Step 2 resumes after reset signal is
188 * deasserted.
189 */
190 if (step == 1) {
191 udelay(200);
192 return;
193 }
194
195step2:
Ed Swarthoute674b832009-02-24 02:37:59 -0600196 /* Set, but do not enable the memory */
197 temp_sdram_cfg = regs->ddr_sdram_cfg;
Poonam_Aggrwal-b1081230cb1452009-01-04 08:46:38 +0530198 temp_sdram_cfg &= ~(SDRAM_CFG_MEM_EN);
199 out_be32(&ddr->sdram_cfg, temp_sdram_cfg);
York Sun922f40f2011-01-10 12:03:01 +0000200#ifdef CONFIG_SYS_FSL_ERRATUM_DDR_A003
York Sunb513d9d2012-08-17 08:22:36 +0000201 debug("Workaround for ERRATUM_DDR_A003\n");
York Sun922f40f2011-01-10 12:03:01 +0000202 if (regs->ddr_sdram_rcw_2 & 0x00f00000) {
203 out_be32(&ddr->timing_cfg_2, regs->timing_cfg_2 & 0xf07fffff);
204 out_be32(&ddr->debug[2], 0x00000400);
205 out_be32(&ddr->ddr_zq_cntl, regs->ddr_zq_cntl & 0x7fffffff);
206 out_be32(&ddr->ddr_wrlvl_cntl, regs->ddr_wrlvl_cntl & 0x7fffffff);
207 out_be32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2 & 0xffffffeb);
208 out_be32(&ddr->mtcr, 0);
York Sun0c88c812014-01-08 13:00:42 -0800209 save1 = in_be32(&ddr->debug[12]);
210 save2 = in_be32(&ddr->debug[21]);
York Sun922f40f2011-01-10 12:03:01 +0000211 out_be32(&ddr->debug[12], 0x00000015);
212 out_be32(&ddr->debug[21], 0x24000000);
213 out_be32(&ddr->sdram_interval, regs->ddr_sdram_interval & 0xffff);
214 out_be32(&ddr->sdram_cfg, temp_sdram_cfg | SDRAM_CFG_BI | SDRAM_CFG_MEM_EN);
215
216 asm volatile("sync;isync");
217 while (!(in_be32(&ddr->debug[1]) & 0x2))
218 ;
219
220 switch (regs->ddr_sdram_rcw_2 & 0x00f00000) {
221 case 0x00000000:
222 out_be32(&ddr->sdram_md_cntl,
223 MD_CNTL_MD_EN |
224 MD_CNTL_CS_SEL_CS0_CS1 |
225 0x04000000 |
226 MD_CNTL_WRCW |
227 MD_CNTL_MD_VALUE(0x02));
York Sun0c88c812014-01-08 13:00:42 -0800228#if (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
229 if (!(regs->cs[2].config & SDRAM_CS_CONFIG_EN))
230 break;
231 while (in_be32(&ddr->sdram_md_cntl) & MD_CNTL_MD_EN)
232 ;
233 out_be32(&ddr->sdram_md_cntl,
234 MD_CNTL_MD_EN |
235 MD_CNTL_CS_SEL_CS2_CS3 |
236 0x04000000 |
237 MD_CNTL_WRCW |
238 MD_CNTL_MD_VALUE(0x02));
239#endif
York Sun922f40f2011-01-10 12:03:01 +0000240 break;
241 case 0x00100000:
242 out_be32(&ddr->sdram_md_cntl,
243 MD_CNTL_MD_EN |
244 MD_CNTL_CS_SEL_CS0_CS1 |
245 0x04000000 |
246 MD_CNTL_WRCW |
247 MD_CNTL_MD_VALUE(0x0a));
York Sun0c88c812014-01-08 13:00:42 -0800248#if (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
249 if (!(regs->cs[2].config & SDRAM_CS_CONFIG_EN))
250 break;
251 while (in_be32(&ddr->sdram_md_cntl) & MD_CNTL_MD_EN)
252 ;
253 out_be32(&ddr->sdram_md_cntl,
254 MD_CNTL_MD_EN |
255 MD_CNTL_CS_SEL_CS2_CS3 |
256 0x04000000 |
257 MD_CNTL_WRCW |
258 MD_CNTL_MD_VALUE(0x0a));
259#endif
York Sun922f40f2011-01-10 12:03:01 +0000260 break;
261 case 0x00200000:
262 out_be32(&ddr->sdram_md_cntl,
263 MD_CNTL_MD_EN |
264 MD_CNTL_CS_SEL_CS0_CS1 |
265 0x04000000 |
266 MD_CNTL_WRCW |
267 MD_CNTL_MD_VALUE(0x12));
York Sun0c88c812014-01-08 13:00:42 -0800268#if (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
269 if (!(regs->cs[2].config & SDRAM_CS_CONFIG_EN))
270 break;
271 while (in_be32(&ddr->sdram_md_cntl) & MD_CNTL_MD_EN)
272 ;
273 out_be32(&ddr->sdram_md_cntl,
274 MD_CNTL_MD_EN |
275 MD_CNTL_CS_SEL_CS2_CS3 |
276 0x04000000 |
277 MD_CNTL_WRCW |
278 MD_CNTL_MD_VALUE(0x12));
279#endif
York Sun922f40f2011-01-10 12:03:01 +0000280 break;
281 case 0x00300000:
282 out_be32(&ddr->sdram_md_cntl,
283 MD_CNTL_MD_EN |
284 MD_CNTL_CS_SEL_CS0_CS1 |
285 0x04000000 |
286 MD_CNTL_WRCW |
287 MD_CNTL_MD_VALUE(0x1a));
York Sun0c88c812014-01-08 13:00:42 -0800288#if (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
289 if (!(regs->cs[2].config & SDRAM_CS_CONFIG_EN))
290 break;
291 while (in_be32(&ddr->sdram_md_cntl) & MD_CNTL_MD_EN)
292 ;
293 out_be32(&ddr->sdram_md_cntl,
294 MD_CNTL_MD_EN |
295 MD_CNTL_CS_SEL_CS2_CS3 |
296 0x04000000 |
297 MD_CNTL_WRCW |
298 MD_CNTL_MD_VALUE(0x1a));
299#endif
York Sun922f40f2011-01-10 12:03:01 +0000300 break;
301 default:
302 out_be32(&ddr->sdram_md_cntl,
303 MD_CNTL_MD_EN |
304 MD_CNTL_CS_SEL_CS0_CS1 |
305 0x04000000 |
306 MD_CNTL_WRCW |
307 MD_CNTL_MD_VALUE(0x02));
York Sun0c88c812014-01-08 13:00:42 -0800308#if (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
309 if (!(regs->cs[2].config & SDRAM_CS_CONFIG_EN))
310 break;
311 while (in_be32(&ddr->sdram_md_cntl) & MD_CNTL_MD_EN)
312 ;
313 out_be32(&ddr->sdram_md_cntl,
314 MD_CNTL_MD_EN |
315 MD_CNTL_CS_SEL_CS2_CS3 |
316 0x04000000 |
317 MD_CNTL_WRCW |
318 MD_CNTL_MD_VALUE(0x02));
319#endif
York Sun922f40f2011-01-10 12:03:01 +0000320 printf("Unsupported RC10\n");
321 break;
322 }
323
324 while (in_be32(&ddr->sdram_md_cntl) & 0x80000000)
325 ;
326 udelay(6);
327 out_be32(&ddr->sdram_cfg, temp_sdram_cfg);
328 out_be32(&ddr->timing_cfg_2, regs->timing_cfg_2);
329 out_be32(&ddr->debug[2], 0x0);
330 out_be32(&ddr->ddr_zq_cntl, regs->ddr_zq_cntl);
331 out_be32(&ddr->ddr_wrlvl_cntl, regs->ddr_wrlvl_cntl);
332 out_be32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2);
York Sun0c88c812014-01-08 13:00:42 -0800333 out_be32(&ddr->debug[12], save1);
334 out_be32(&ddr->debug[21], save2);
York Sun922f40f2011-01-10 12:03:01 +0000335 out_be32(&ddr->sdram_interval, regs->ddr_sdram_interval);
336
337 }
338#endif
Kumar Galad5a1fb92008-08-26 21:34:55 -0500339 /*
Dave Liu7dc79f72008-10-23 21:18:53 +0800340 * For 8572 DDR1 erratum - DDR controller may enter illegal state
341 * when operatiing in 32-bit bus mode with 4-beat bursts,
342 * This erratum does not affect DDR3 mode, only for DDR2 mode.
Kumar Galad5a1fb92008-08-26 21:34:55 -0500343 */
York Sun9aa857b2011-01-25 21:51:27 -0800344#ifdef CONFIG_SYS_FSL_ERRATUM_DDR_115
York Sunb513d9d2012-08-17 08:22:36 +0000345 debug("Workaround for ERRATUM_DDR_115\n");
Kumar Galad5a1fb92008-08-26 21:34:55 -0500346 if ((((in_be32(&ddr->sdram_cfg) >> 24) & 0x7) == SDRAM_TYPE_DDR2)
Dave Liu7dc79f72008-10-23 21:18:53 +0800347 && in_be32(&ddr->sdram_cfg) & 0x80000) {
Kumar Galad5a1fb92008-08-26 21:34:55 -0500348 /* set DEBUG_1[31] */
York Sun7dda8472011-01-10 12:02:59 +0000349 setbits_be32(&ddr->debug[0], 1);
Kumar Galad5a1fb92008-08-26 21:34:55 -0500350 }
Dave Liu7dc79f72008-10-23 21:18:53 +0800351#endif
York Sunc8fc9592011-01-25 22:05:49 -0800352#ifdef CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134
York Sunb513d9d2012-08-17 08:22:36 +0000353 debug("Workaround for ERRATUM_DDR111_DDR134\n");
York Sunc8fc9592011-01-25 22:05:49 -0800354 /*
355 * This is the combined workaround for DDR111 and DDR134
356 * following the published errata for MPC8572
357 */
358
359 /* 1. Set EEBACR[3] */
360 setbits_be32(&ecm->eebacr, 0x10000000);
361 debug("Setting EEBACR[3] to 0x%08x\n", in_be32(&ecm->eebacr));
362
363 /* 2. Set DINIT in SDRAM_CFG_2*/
364 setbits_be32(&ddr->sdram_cfg_2, SDRAM_CFG2_D_INIT);
365 debug("Setting sdram_cfg_2[D_INIT] to 0x%08x\n",
366 in_be32(&ddr->sdram_cfg_2));
367
368 /* 3. Set DEBUG_3[21] */
369 setbits_be32(&ddr->debug[2], 0x400);
370 debug("Setting DEBUG_3[21] to 0x%08x\n", in_be32(&ddr->debug[2]));
371
372#endif /* part 1 of the workaound */
Kumar Galad5a1fb92008-08-26 21:34:55 -0500373
374 /*
Dave Liu4be87b22009-03-14 12:48:30 +0800375 * 500 painful micro-seconds must elapse between
Kumar Galad5a1fb92008-08-26 21:34:55 -0500376 * the DDR clock setup and the DDR config enable.
Dave Liu4be87b22009-03-14 12:48:30 +0800377 * DDR2 need 200 us, and DDR3 need 500 us from spec,
378 * we choose the max, that is 500 us for all of case.
Kumar Galad5a1fb92008-08-26 21:34:55 -0500379 */
Dave Liu4be87b22009-03-14 12:48:30 +0800380 udelay(500);
Kumar Galad5a1fb92008-08-26 21:34:55 -0500381 asm volatile("sync;isync");
382
Tang Yuantiana7364af2014-04-17 15:33:46 +0800383#ifdef CONFIG_DEEP_SLEEP
Tang Yuantian064f1262014-11-21 11:17:15 +0800384 if (is_warm_boot()) {
Tang Yuantiana7364af2014-04-17 15:33:46 +0800385 /* enter self-refresh */
Tang Yuantian064f1262014-11-21 11:17:15 +0800386 setbits_be32(&ddr->sdram_cfg_2, SDRAM_CFG2_FRC_SR);
Tang Yuantiana7364af2014-04-17 15:33:46 +0800387 /* do board specific memory setup */
388 board_mem_sleep_setup();
Tang Yuantiana7364af2014-04-17 15:33:46 +0800389 temp_sdram_cfg = (in_be32(&ddr->sdram_cfg) | SDRAM_CFG_BI);
Tang Yuantian064f1262014-11-21 11:17:15 +0800390 } else
Tang Yuantiana7364af2014-04-17 15:33:46 +0800391#endif
392 temp_sdram_cfg = (in_be32(&ddr->sdram_cfg) & ~SDRAM_CFG_BI);
Tang Yuantian064f1262014-11-21 11:17:15 +0800393
394 /* Let the controller go */
Poonam_Aggrwal-b1081230cb1452009-01-04 08:46:38 +0530395 out_be32(&ddr->sdram_cfg, temp_sdram_cfg | SDRAM_CFG_MEM_EN);
York Sun922f40f2011-01-10 12:03:01 +0000396 asm volatile("sync;isync");
Kumar Galad5a1fb92008-08-26 21:34:55 -0500397
York Sun016095d2012-10-08 07:44:24 +0000398 total_gb_size_per_controller = 0;
399 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
400 if (!(regs->cs[i].config & 0x80000000))
401 continue;
402 total_gb_size_per_controller += 1 << (
403 ((regs->cs[i].config >> 14) & 0x3) + 2 +
404 ((regs->cs[i].config >> 8) & 0x7) + 12 +
405 ((regs->cs[i].config >> 0) & 0x7) + 8 +
406 3 - ((regs->ddr_sdram_cfg >> 19) & 0x3) -
407 26); /* minus 26 (count of 64M) */
408 }
409 if (fsl_ddr_get_intl3r() & 0x80000000) /* 3-way interleaving */
410 total_gb_size_per_controller *= 3;
411 else if (regs->cs[0].config & 0x20000000) /* 2-way interleaving */
412 total_gb_size_per_controller <<= 1;
413 /*
414 * total memory / bus width = transactions needed
415 * transactions needed / data rate = seconds
416 * to add plenty of buffer, double the time
417 * For example, 2GB on 666MT/s 64-bit bus takes about 402ms
418 * Let's wait for 800ms
419 */
420 bus_width = 3 - ((ddr->sdram_cfg & SDRAM_CFG_DBW_MASK)
421 >> SDRAM_CFG_DBW_SHIFT);
422 timeout = ((total_gb_size_per_controller << (6 - bus_width)) * 100 /
York Sun2c0b62d2015-01-06 13:18:50 -0800423 (get_ddr_freq(ctrl_num) >> 20)) << 1;
Andy Fleming1bc8b042012-10-22 17:28:18 -0500424#ifdef CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134
York Sun016095d2012-10-08 07:44:24 +0000425 timeout_save = timeout;
Andy Fleming1bc8b042012-10-22 17:28:18 -0500426#endif
York Sun016095d2012-10-08 07:44:24 +0000427 total_gb_size_per_controller >>= 4; /* shift down to gb size */
428 debug("total %d GB\n", total_gb_size_per_controller);
429 debug("Need to wait up to %d * 10ms\n", timeout);
430
Kumar Galad5a1fb92008-08-26 21:34:55 -0500431 /* Poll DDR_SDRAM_CFG_2[D_INIT] bit until auto-data init is done. */
York Sun016095d2012-10-08 07:44:24 +0000432 while ((in_be32(&ddr->sdram_cfg_2) & SDRAM_CFG2_D_INIT) &&
433 (timeout >= 0)) {
Kumar Galad5a1fb92008-08-26 21:34:55 -0500434 udelay(10000); /* throttle polling rate */
York Sun016095d2012-10-08 07:44:24 +0000435 timeout--;
436 }
437
438 if (timeout <= 0)
439 printf("Waiting for D_INIT timeout. Memory may not work.\n");
York Sunc8fc9592011-01-25 22:05:49 -0800440
441#ifdef CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134
442 /* continue this workaround */
443
444 /* 4. Clear DEBUG3[21] */
445 clrbits_be32(&ddr->debug[2], 0x400);
446 debug("Clearing D3[21] to 0x%08x\n", in_be32(&ddr->debug[2]));
447
448 /* DDR134 workaround starts */
449 /* A: Clear sdram_cfg_2[odt_cfg] */
450 clrbits_be32(&ddr->sdram_cfg_2, SDRAM_CFG2_ODT_CFG_MASK);
451 debug("Clearing SDRAM_CFG2[ODT_CFG] to 0x%08x\n",
452 in_be32(&ddr->sdram_cfg_2));
453
454 /* B: Set DEBUG1[15] */
455 setbits_be32(&ddr->debug[0], 0x10000);
456 debug("Setting D1[15] to 0x%08x\n", in_be32(&ddr->debug[0]));
457
458 /* C: Set timing_cfg_2[cpo] to 0b11111 */
459 setbits_be32(&ddr->timing_cfg_2, TIMING_CFG_2_CPO_MASK);
460 debug("Setting TMING_CFG_2[CPO] to 0x%08x\n",
461 in_be32(&ddr->timing_cfg_2));
462
463 /* D: Set D6 to 0x9f9f9f9f */
464 out_be32(&ddr->debug[5], 0x9f9f9f9f);
465 debug("Setting D6 to 0x%08x\n", in_be32(&ddr->debug[5]));
466
467 /* E: Set D7 to 0x9f9f9f9f */
468 out_be32(&ddr->debug[6], 0x9f9f9f9f);
469 debug("Setting D7 to 0x%08x\n", in_be32(&ddr->debug[6]));
470
471 /* F: Set D2[20] */
472 setbits_be32(&ddr->debug[1], 0x800);
473 debug("Setting D2[20] to 0x%08x\n", in_be32(&ddr->debug[1]));
474
475 /* G: Poll on D2[20] until cleared */
476 while (in_be32(&ddr->debug[1]) & 0x800)
477 udelay(10000); /* throttle polling rate */
478
479 /* H: Clear D1[15] */
480 clrbits_be32(&ddr->debug[0], 0x10000);
481 debug("Setting D1[15] to 0x%08x\n", in_be32(&ddr->debug[0]));
482
483 /* I: Set sdram_cfg_2[odt_cfg] */
484 setbits_be32(&ddr->sdram_cfg_2,
485 regs->ddr_sdram_cfg_2 & SDRAM_CFG2_ODT_CFG_MASK);
486 debug("Setting sdram_cfg_2 to 0x%08x\n", in_be32(&ddr->sdram_cfg_2));
487
488 /* Continuing with the DDR111 workaround */
489 /* 5. Set D2[21] */
490 setbits_be32(&ddr->debug[1], 0x400);
491 debug("Setting D2[21] to 0x%08x\n", in_be32(&ddr->debug[1]));
492
493 /* 6. Poll D2[21] until its cleared */
494 while (in_be32(&ddr->debug[1]) & 0x400)
495 udelay(10000); /* throttle polling rate */
496
York Sun016095d2012-10-08 07:44:24 +0000497 /* 7. Wait for state machine 2nd run, roughly 400ms/GB */
498 debug("Wait for %d * 10ms\n", timeout_save);
499 udelay(timeout_save * 10000);
York Sunc8fc9592011-01-25 22:05:49 -0800500
501 /* 8. Set sdram_cfg_2[dinit] if options requires */
502 setbits_be32(&ddr->sdram_cfg_2,
503 regs->ddr_sdram_cfg_2 & SDRAM_CFG2_D_INIT);
504 debug("Setting sdram_cfg_2 to 0x%08x\n", in_be32(&ddr->sdram_cfg_2));
505
506 /* 9. Poll until dinit is cleared */
York Sun016095d2012-10-08 07:44:24 +0000507 timeout = timeout_save;
508 debug("Need to wait up to %d * 10ms\n", timeout);
509 while ((in_be32(&ddr->sdram_cfg_2) & SDRAM_CFG2_D_INIT) &&
510 (timeout >= 0)) {
511 udelay(10000); /* throttle polling rate */
512 timeout--;
513 }
514
515 if (timeout <= 0)
516 printf("Waiting for D_INIT timeout. Memory may not work.\n");
York Sunc8fc9592011-01-25 22:05:49 -0800517
518 /* 10. Clear EEBACR[3] */
519 clrbits_be32(&ecm->eebacr, 10000000);
520 debug("Clearing EEBACR[3] to 0x%08x\n", in_be32(&ecm->eebacr));
York Sun7d9781b2011-03-17 11:18:13 -0700521
522 if (csn != -1) {
523 csn_bnds_t = (unsigned int *) &regs->cs[csn].bnds;
524 *csn_bnds_t = csn_bnds_backup;
525 debug("Change cs%d_bnds back to 0x%08x\n",
526 csn, regs->cs[csn].bnds);
527 setbits_be32(&ddr->sdram_cfg, 0x2); /* MEM_HALT */
528 switch (csn) {
529 case 0:
530 out_be32(&ddr->cs0_bnds, regs->cs[csn].bnds);
531 break;
532 case 1:
533 out_be32(&ddr->cs1_bnds, regs->cs[csn].bnds);
534 break;
York Sun2c0b62d2015-01-06 13:18:50 -0800535#if CONFIG_CHIP_SELECTS_PER_CTRL > 2
York Sun7d9781b2011-03-17 11:18:13 -0700536 case 2:
537 out_be32(&ddr->cs2_bnds, regs->cs[csn].bnds);
538 break;
539 case 3:
540 out_be32(&ddr->cs3_bnds, regs->cs[csn].bnds);
541 break;
York Sun2c0b62d2015-01-06 13:18:50 -0800542#endif
York Sun7d9781b2011-03-17 11:18:13 -0700543 }
544 clrbits_be32(&ddr->sdram_cfg, 0x2);
545 }
York Sunc8fc9592011-01-25 22:05:49 -0800546#endif /* CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134 */
Tang Yuantiana7364af2014-04-17 15:33:46 +0800547#ifdef CONFIG_DEEP_SLEEP
Tang Yuantian064f1262014-11-21 11:17:15 +0800548 if (is_warm_boot())
Tang Yuantiana7364af2014-04-17 15:33:46 +0800549 /* exit self-refresh */
Tang Yuantian064f1262014-11-21 11:17:15 +0800550 clrbits_be32(&ddr->sdram_cfg_2, SDRAM_CFG2_FRC_SR);
Tang Yuantiana7364af2014-04-17 15:33:46 +0800551#endif
Kumar Galad5a1fb92008-08-26 21:34:55 -0500552}