blob: 6752d4d29e0861140fac00b946765898e86858d1 [file] [log] [blame]
Kumar Galad5a1fb92008-08-26 21:34:55 -05001/*
York Sunb513d9d2012-08-17 08:22:36 +00002 * Copyright 2008-2012 Freescale Semiconductor, Inc.
Kumar Galad5a1fb92008-08-26 21:34:55 -05003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * Version 2 as published by the Free Software Foundation.
7 */
8
9#include <common.h>
10#include <asm/io.h>
York Sunf0626592013-09-30 09:22:09 -070011#include <fsl_ddr_sdram.h>
York Sun7dda8472011-01-10 12:02:59 +000012#include <asm/processor.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;
York Sunc8fc9592011-01-25 22:05:49 -080037 volatile ccsr_local_ecm_t *ecm = (void *)CONFIG_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
Kumar Galad5a1fb92008-08-26 21:34:55 -050044
45 switch (ctrl_num) {
46 case 0:
York Sunf0626592013-09-30 09:22:09 -070047 ddr = (void *)CONFIG_SYS_FSL_DDR_ADDR;
Kumar Galad5a1fb92008-08-26 21:34:55 -050048 break;
York Sunf0626592013-09-30 09:22:09 -070049#if defined(CONFIG_SYS_FSL_DDR2_ADDR) && (CONFIG_NUM_DDR_CONTROLLERS > 1)
Kumar Galad5a1fb92008-08-26 21:34:55 -050050 case 1:
York Sunf0626592013-09-30 09:22:09 -070051 ddr = (void *)CONFIG_SYS_FSL_DDR2_ADDR;
Kumar Galad5a1fb92008-08-26 21:34:55 -050052 break;
York Sune8dc17b2012-08-17 08:22:39 +000053#endif
York Sunf0626592013-09-30 09:22:09 -070054#if defined(CONFIG_SYS_FSL_DDR3_ADDR) && (CONFIG_NUM_DDR_CONTROLLERS > 2)
York Sune8dc17b2012-08-17 08:22:39 +000055 case 2:
York Sunf0626592013-09-30 09:22:09 -070056 ddr = (void *)CONFIG_SYS_FSL_DDR3_ADDR;
York Sune8dc17b2012-08-17 08:22:39 +000057 break;
58#endif
York Sunf0626592013-09-30 09:22:09 -070059#if defined(CONFIG_SYS_FSL_DDR4_ADDR) && (CONFIG_NUM_DDR_CONTROLLERS > 3)
York Sune8dc17b2012-08-17 08:22:39 +000060 case 3:
York Sunf0626592013-09-30 09:22:09 -070061 ddr = (void *)CONFIG_SYS_FSL_DDR4_ADDR;
York Sune8dc17b2012-08-17 08:22:39 +000062 break;
63#endif
Kumar Galad5a1fb92008-08-26 21:34:55 -050064 default:
65 printf("%s unexpected ctrl_num = %u\n", __FUNCTION__, ctrl_num);
66 return;
67 }
68
York Sun5e155552013-06-25 11:37:48 -070069 if (step == 2)
70 goto step2;
71
York Sun016095d2012-10-08 07:44:24 +000072 if (regs->ddr_eor)
73 out_be32(&ddr->eor, regs->ddr_eor);
York Sun7d9781b2011-03-17 11:18:13 -070074#ifdef CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134
York Sunb513d9d2012-08-17 08:22:36 +000075 debug("Workaround for ERRATUM_DDR111_DDR134\n");
York Sun7d9781b2011-03-17 11:18:13 -070076 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
77 cs_sa = (regs->cs[i].bnds >> 16) & 0xfff;
78 cs_ea = regs->cs[i].bnds & 0xfff;
79 if ((cs_sa <= 0xff) && (cs_ea >= 0xff)) {
80 csn = i;
81 csn_bnds_backup = regs->cs[i].bnds;
82 csn_bnds_t = (unsigned int *) &regs->cs[i].bnds;
York Sun4b736b82012-05-21 08:43:11 +000083 if (cs_ea > 0xeff)
84 *csn_bnds_t = regs->cs[i].bnds + 0x01000000;
85 else
86 *csn_bnds_t = regs->cs[i].bnds + 0x01000100;
York Sun7d9781b2011-03-17 11:18:13 -070087 debug("Found cs%d_bns (0x%08x) covering 0xff000000, "
88 "change it to 0x%x\n",
89 csn, csn_bnds_backup, regs->cs[i].bnds);
90 break;
91 }
92 }
93#endif
Kumar Galad5a1fb92008-08-26 21:34:55 -050094 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
95 if (i == 0) {
96 out_be32(&ddr->cs0_bnds, regs->cs[i].bnds);
97 out_be32(&ddr->cs0_config, regs->cs[i].config);
98 out_be32(&ddr->cs0_config_2, regs->cs[i].config_2);
99
100 } else if (i == 1) {
101 out_be32(&ddr->cs1_bnds, regs->cs[i].bnds);
102 out_be32(&ddr->cs1_config, regs->cs[i].config);
103 out_be32(&ddr->cs1_config_2, regs->cs[i].config_2);
104
105 } else if (i == 2) {
106 out_be32(&ddr->cs2_bnds, regs->cs[i].bnds);
107 out_be32(&ddr->cs2_config, regs->cs[i].config);
108 out_be32(&ddr->cs2_config_2, regs->cs[i].config_2);
109
110 } else if (i == 3) {
111 out_be32(&ddr->cs3_bnds, regs->cs[i].bnds);
112 out_be32(&ddr->cs3_config, regs->cs[i].config);
113 out_be32(&ddr->cs3_config_2, regs->cs[i].config_2);
114 }
115 }
116
117 out_be32(&ddr->timing_cfg_3, regs->timing_cfg_3);
118 out_be32(&ddr->timing_cfg_0, regs->timing_cfg_0);
119 out_be32(&ddr->timing_cfg_1, regs->timing_cfg_1);
120 out_be32(&ddr->timing_cfg_2, regs->timing_cfg_2);
Kumar Galad5a1fb92008-08-26 21:34:55 -0500121 out_be32(&ddr->sdram_mode, regs->ddr_sdram_mode);
122 out_be32(&ddr->sdram_mode_2, regs->ddr_sdram_mode_2);
York Sunba0c2eb2011-01-10 12:03:00 +0000123 out_be32(&ddr->sdram_mode_3, regs->ddr_sdram_mode_3);
124 out_be32(&ddr->sdram_mode_4, regs->ddr_sdram_mode_4);
125 out_be32(&ddr->sdram_mode_5, regs->ddr_sdram_mode_5);
126 out_be32(&ddr->sdram_mode_6, regs->ddr_sdram_mode_6);
127 out_be32(&ddr->sdram_mode_7, regs->ddr_sdram_mode_7);
128 out_be32(&ddr->sdram_mode_8, regs->ddr_sdram_mode_8);
Kumar Galad5a1fb92008-08-26 21:34:55 -0500129 out_be32(&ddr->sdram_md_cntl, regs->ddr_sdram_md_cntl);
130 out_be32(&ddr->sdram_interval, regs->ddr_sdram_interval);
131 out_be32(&ddr->sdram_data_init, regs->ddr_data_init);
132 out_be32(&ddr->sdram_clk_cntl, regs->ddr_sdram_clk_cntl);
Kumar Galad5a1fb92008-08-26 21:34:55 -0500133 out_be32(&ddr->timing_cfg_4, regs->timing_cfg_4);
134 out_be32(&ddr->timing_cfg_5, regs->timing_cfg_5);
135 out_be32(&ddr->ddr_zq_cntl, regs->ddr_zq_cntl);
136 out_be32(&ddr->ddr_wrlvl_cntl, regs->ddr_wrlvl_cntl);
York Sun972cc402013-06-25 11:37:41 -0700137#ifndef CONFIG_SYS_FSL_DDR_EMU
138 /*
139 * Skip these two registers if running on emulator
140 * because emulator doesn't have skew between bytes.
141 */
142
York Sun7d69ea32012-10-08 07:44:22 +0000143 if (regs->ddr_wrlvl_cntl_2)
144 out_be32(&ddr->ddr_wrlvl_cntl_2, regs->ddr_wrlvl_cntl_2);
145 if (regs->ddr_wrlvl_cntl_3)
146 out_be32(&ddr->ddr_wrlvl_cntl_3, regs->ddr_wrlvl_cntl_3);
York Sun972cc402013-06-25 11:37:41 -0700147#endif
York Sun7d69ea32012-10-08 07:44:22 +0000148
Kumar Galad5a1fb92008-08-26 21:34:55 -0500149 out_be32(&ddr->ddr_sr_cntr, regs->ddr_sr_cntr);
150 out_be32(&ddr->ddr_sdram_rcw_1, regs->ddr_sdram_rcw_1);
151 out_be32(&ddr->ddr_sdram_rcw_2, regs->ddr_sdram_rcw_2);
York Sun7dda8472011-01-10 12:02:59 +0000152 out_be32(&ddr->ddr_cdr1, regs->ddr_cdr1);
Tang Yuantian064f1262014-11-21 11:17:15 +0800153#ifdef CONFIG_DEEP_SLEEP
154 if (is_warm_boot()) {
155 out_be32(&ddr->sdram_cfg_2,
156 regs->ddr_sdram_cfg_2 & ~SDRAM_CFG2_D_INIT);
157 out_be32(&ddr->init_addr, CONFIG_SYS_SDRAM_BASE);
158 out_be32(&ddr->init_ext_addr, DDR_INIT_ADDR_EXT_UIA);
159
160 /* DRAM VRef will not be trained */
161 out_be32(&ddr->ddr_cdr2,
162 regs->ddr_cdr2 & ~DDR_CDR2_VREF_TRAIN_EN);
163 } else
164#endif
165 {
166 out_be32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2);
167 out_be32(&ddr->init_addr, regs->ddr_init_addr);
168 out_be32(&ddr->init_ext_addr, regs->ddr_init_ext_addr);
169 out_be32(&ddr->ddr_cdr2, regs->ddr_cdr2);
170 }
York Sun7dda8472011-01-10 12:02:59 +0000171 out_be32(&ddr->err_disable, regs->err_disable);
172 out_be32(&ddr->err_int_en, regs->err_int_en);
York Sunb513d9d2012-08-17 08:22:36 +0000173 for (i = 0; i < 32; i++) {
174 if (regs->debug[i]) {
175 debug("Write to debug_%d as %08x\n", i+1, regs->debug[i]);
176 out_be32(&ddr->debug[i], regs->debug[i]);
177 }
178 }
York Sun6995a022012-10-08 07:44:26 +0000179#ifdef CONFIG_SYS_FSL_ERRATUM_A_004934
York Suna2e8e0a2013-03-25 07:39:34 +0000180 out_be32(&ddr->debug[28], 0x30003000);
York Sun6995a022012-10-08 07:44:26 +0000181#endif
Kumar Galad5a1fb92008-08-26 21:34:55 -0500182
York Sundf2be192011-11-20 10:01:35 -0800183#ifdef CONFIG_SYS_FSL_ERRATUM_DDR_A003474
184 out_be32(&ddr->debug[12], 0x00000015);
185 out_be32(&ddr->debug[21], 0x24000000);
186#endif /* CONFIG_SYS_FSL_ERRATUM_DDR_A003474 */
187
York Sun5e155552013-06-25 11:37:48 -0700188 /*
189 * For RDIMMs, JEDEC spec requires clocks to be stable before reset is
190 * deasserted. Clocks start when any chip select is enabled and clock
191 * control register is set. Because all DDR components are connected to
192 * one reset signal, this needs to be done in two steps. Step 1 is to
193 * get the clocks started. Step 2 resumes after reset signal is
194 * deasserted.
195 */
196 if (step == 1) {
197 udelay(200);
198 return;
199 }
200
201step2:
Ed Swarthoute674b832009-02-24 02:37:59 -0600202 /* Set, but do not enable the memory */
203 temp_sdram_cfg = regs->ddr_sdram_cfg;
Poonam_Aggrwal-b1081230cb1452009-01-04 08:46:38 +0530204 temp_sdram_cfg &= ~(SDRAM_CFG_MEM_EN);
205 out_be32(&ddr->sdram_cfg, temp_sdram_cfg);
York Sun922f40f2011-01-10 12:03:01 +0000206#ifdef CONFIG_SYS_FSL_ERRATUM_DDR_A003
York Sunb513d9d2012-08-17 08:22:36 +0000207 debug("Workaround for ERRATUM_DDR_A003\n");
York Sun922f40f2011-01-10 12:03:01 +0000208 if (regs->ddr_sdram_rcw_2 & 0x00f00000) {
209 out_be32(&ddr->timing_cfg_2, regs->timing_cfg_2 & 0xf07fffff);
210 out_be32(&ddr->debug[2], 0x00000400);
211 out_be32(&ddr->ddr_zq_cntl, regs->ddr_zq_cntl & 0x7fffffff);
212 out_be32(&ddr->ddr_wrlvl_cntl, regs->ddr_wrlvl_cntl & 0x7fffffff);
213 out_be32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2 & 0xffffffeb);
214 out_be32(&ddr->mtcr, 0);
York Sun0c88c812014-01-08 13:00:42 -0800215 save1 = in_be32(&ddr->debug[12]);
216 save2 = in_be32(&ddr->debug[21]);
York Sun922f40f2011-01-10 12:03:01 +0000217 out_be32(&ddr->debug[12], 0x00000015);
218 out_be32(&ddr->debug[21], 0x24000000);
219 out_be32(&ddr->sdram_interval, regs->ddr_sdram_interval & 0xffff);
220 out_be32(&ddr->sdram_cfg, temp_sdram_cfg | SDRAM_CFG_BI | SDRAM_CFG_MEM_EN);
221
222 asm volatile("sync;isync");
223 while (!(in_be32(&ddr->debug[1]) & 0x2))
224 ;
225
226 switch (regs->ddr_sdram_rcw_2 & 0x00f00000) {
227 case 0x00000000:
228 out_be32(&ddr->sdram_md_cntl,
229 MD_CNTL_MD_EN |
230 MD_CNTL_CS_SEL_CS0_CS1 |
231 0x04000000 |
232 MD_CNTL_WRCW |
233 MD_CNTL_MD_VALUE(0x02));
York Sun0c88c812014-01-08 13:00:42 -0800234#if (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
235 if (!(regs->cs[2].config & SDRAM_CS_CONFIG_EN))
236 break;
237 while (in_be32(&ddr->sdram_md_cntl) & MD_CNTL_MD_EN)
238 ;
239 out_be32(&ddr->sdram_md_cntl,
240 MD_CNTL_MD_EN |
241 MD_CNTL_CS_SEL_CS2_CS3 |
242 0x04000000 |
243 MD_CNTL_WRCW |
244 MD_CNTL_MD_VALUE(0x02));
245#endif
York Sun922f40f2011-01-10 12:03:01 +0000246 break;
247 case 0x00100000:
248 out_be32(&ddr->sdram_md_cntl,
249 MD_CNTL_MD_EN |
250 MD_CNTL_CS_SEL_CS0_CS1 |
251 0x04000000 |
252 MD_CNTL_WRCW |
253 MD_CNTL_MD_VALUE(0x0a));
York Sun0c88c812014-01-08 13:00:42 -0800254#if (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
255 if (!(regs->cs[2].config & SDRAM_CS_CONFIG_EN))
256 break;
257 while (in_be32(&ddr->sdram_md_cntl) & MD_CNTL_MD_EN)
258 ;
259 out_be32(&ddr->sdram_md_cntl,
260 MD_CNTL_MD_EN |
261 MD_CNTL_CS_SEL_CS2_CS3 |
262 0x04000000 |
263 MD_CNTL_WRCW |
264 MD_CNTL_MD_VALUE(0x0a));
265#endif
York Sun922f40f2011-01-10 12:03:01 +0000266 break;
267 case 0x00200000:
268 out_be32(&ddr->sdram_md_cntl,
269 MD_CNTL_MD_EN |
270 MD_CNTL_CS_SEL_CS0_CS1 |
271 0x04000000 |
272 MD_CNTL_WRCW |
273 MD_CNTL_MD_VALUE(0x12));
York Sun0c88c812014-01-08 13:00:42 -0800274#if (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
275 if (!(regs->cs[2].config & SDRAM_CS_CONFIG_EN))
276 break;
277 while (in_be32(&ddr->sdram_md_cntl) & MD_CNTL_MD_EN)
278 ;
279 out_be32(&ddr->sdram_md_cntl,
280 MD_CNTL_MD_EN |
281 MD_CNTL_CS_SEL_CS2_CS3 |
282 0x04000000 |
283 MD_CNTL_WRCW |
284 MD_CNTL_MD_VALUE(0x12));
285#endif
York Sun922f40f2011-01-10 12:03:01 +0000286 break;
287 case 0x00300000:
288 out_be32(&ddr->sdram_md_cntl,
289 MD_CNTL_MD_EN |
290 MD_CNTL_CS_SEL_CS0_CS1 |
291 0x04000000 |
292 MD_CNTL_WRCW |
293 MD_CNTL_MD_VALUE(0x1a));
York Sun0c88c812014-01-08 13:00:42 -0800294#if (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
295 if (!(regs->cs[2].config & SDRAM_CS_CONFIG_EN))
296 break;
297 while (in_be32(&ddr->sdram_md_cntl) & MD_CNTL_MD_EN)
298 ;
299 out_be32(&ddr->sdram_md_cntl,
300 MD_CNTL_MD_EN |
301 MD_CNTL_CS_SEL_CS2_CS3 |
302 0x04000000 |
303 MD_CNTL_WRCW |
304 MD_CNTL_MD_VALUE(0x1a));
305#endif
York Sun922f40f2011-01-10 12:03:01 +0000306 break;
307 default:
308 out_be32(&ddr->sdram_md_cntl,
309 MD_CNTL_MD_EN |
310 MD_CNTL_CS_SEL_CS0_CS1 |
311 0x04000000 |
312 MD_CNTL_WRCW |
313 MD_CNTL_MD_VALUE(0x02));
York Sun0c88c812014-01-08 13:00:42 -0800314#if (CONFIG_DIMM_SLOTS_PER_CTLR == 2)
315 if (!(regs->cs[2].config & SDRAM_CS_CONFIG_EN))
316 break;
317 while (in_be32(&ddr->sdram_md_cntl) & MD_CNTL_MD_EN)
318 ;
319 out_be32(&ddr->sdram_md_cntl,
320 MD_CNTL_MD_EN |
321 MD_CNTL_CS_SEL_CS2_CS3 |
322 0x04000000 |
323 MD_CNTL_WRCW |
324 MD_CNTL_MD_VALUE(0x02));
325#endif
York Sun922f40f2011-01-10 12:03:01 +0000326 printf("Unsupported RC10\n");
327 break;
328 }
329
330 while (in_be32(&ddr->sdram_md_cntl) & 0x80000000)
331 ;
332 udelay(6);
333 out_be32(&ddr->sdram_cfg, temp_sdram_cfg);
334 out_be32(&ddr->timing_cfg_2, regs->timing_cfg_2);
335 out_be32(&ddr->debug[2], 0x0);
336 out_be32(&ddr->ddr_zq_cntl, regs->ddr_zq_cntl);
337 out_be32(&ddr->ddr_wrlvl_cntl, regs->ddr_wrlvl_cntl);
338 out_be32(&ddr->sdram_cfg_2, regs->ddr_sdram_cfg_2);
York Sun0c88c812014-01-08 13:00:42 -0800339 out_be32(&ddr->debug[12], save1);
340 out_be32(&ddr->debug[21], save2);
York Sun922f40f2011-01-10 12:03:01 +0000341 out_be32(&ddr->sdram_interval, regs->ddr_sdram_interval);
342
343 }
344#endif
Kumar Galad5a1fb92008-08-26 21:34:55 -0500345 /*
Dave Liu7dc79f72008-10-23 21:18:53 +0800346 * For 8572 DDR1 erratum - DDR controller may enter illegal state
347 * when operatiing in 32-bit bus mode with 4-beat bursts,
348 * This erratum does not affect DDR3 mode, only for DDR2 mode.
Kumar Galad5a1fb92008-08-26 21:34:55 -0500349 */
York Sun9aa857b2011-01-25 21:51:27 -0800350#ifdef CONFIG_SYS_FSL_ERRATUM_DDR_115
York Sunb513d9d2012-08-17 08:22:36 +0000351 debug("Workaround for ERRATUM_DDR_115\n");
Kumar Galad5a1fb92008-08-26 21:34:55 -0500352 if ((((in_be32(&ddr->sdram_cfg) >> 24) & 0x7) == SDRAM_TYPE_DDR2)
Dave Liu7dc79f72008-10-23 21:18:53 +0800353 && in_be32(&ddr->sdram_cfg) & 0x80000) {
Kumar Galad5a1fb92008-08-26 21:34:55 -0500354 /* set DEBUG_1[31] */
York Sun7dda8472011-01-10 12:02:59 +0000355 setbits_be32(&ddr->debug[0], 1);
Kumar Galad5a1fb92008-08-26 21:34:55 -0500356 }
Dave Liu7dc79f72008-10-23 21:18:53 +0800357#endif
York Sunc8fc9592011-01-25 22:05:49 -0800358#ifdef CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134
York Sunb513d9d2012-08-17 08:22:36 +0000359 debug("Workaround for ERRATUM_DDR111_DDR134\n");
York Sunc8fc9592011-01-25 22:05:49 -0800360 /*
361 * This is the combined workaround for DDR111 and DDR134
362 * following the published errata for MPC8572
363 */
364
365 /* 1. Set EEBACR[3] */
366 setbits_be32(&ecm->eebacr, 0x10000000);
367 debug("Setting EEBACR[3] to 0x%08x\n", in_be32(&ecm->eebacr));
368
369 /* 2. Set DINIT in SDRAM_CFG_2*/
370 setbits_be32(&ddr->sdram_cfg_2, SDRAM_CFG2_D_INIT);
371 debug("Setting sdram_cfg_2[D_INIT] to 0x%08x\n",
372 in_be32(&ddr->sdram_cfg_2));
373
374 /* 3. Set DEBUG_3[21] */
375 setbits_be32(&ddr->debug[2], 0x400);
376 debug("Setting DEBUG_3[21] to 0x%08x\n", in_be32(&ddr->debug[2]));
377
378#endif /* part 1 of the workaound */
Kumar Galad5a1fb92008-08-26 21:34:55 -0500379
380 /*
Dave Liu4be87b22009-03-14 12:48:30 +0800381 * 500 painful micro-seconds must elapse between
Kumar Galad5a1fb92008-08-26 21:34:55 -0500382 * the DDR clock setup and the DDR config enable.
Dave Liu4be87b22009-03-14 12:48:30 +0800383 * DDR2 need 200 us, and DDR3 need 500 us from spec,
384 * we choose the max, that is 500 us for all of case.
Kumar Galad5a1fb92008-08-26 21:34:55 -0500385 */
Dave Liu4be87b22009-03-14 12:48:30 +0800386 udelay(500);
Kumar Galad5a1fb92008-08-26 21:34:55 -0500387 asm volatile("sync;isync");
388
Tang Yuantiana7364af2014-04-17 15:33:46 +0800389#ifdef CONFIG_DEEP_SLEEP
Tang Yuantian064f1262014-11-21 11:17:15 +0800390 if (is_warm_boot()) {
Tang Yuantiana7364af2014-04-17 15:33:46 +0800391 /* enter self-refresh */
Tang Yuantian064f1262014-11-21 11:17:15 +0800392 setbits_be32(&ddr->sdram_cfg_2, SDRAM_CFG2_FRC_SR);
Tang Yuantiana7364af2014-04-17 15:33:46 +0800393 /* do board specific memory setup */
394 board_mem_sleep_setup();
Tang Yuantiana7364af2014-04-17 15:33:46 +0800395 temp_sdram_cfg = (in_be32(&ddr->sdram_cfg) | SDRAM_CFG_BI);
Tang Yuantian064f1262014-11-21 11:17:15 +0800396 } else
Tang Yuantiana7364af2014-04-17 15:33:46 +0800397#endif
398 temp_sdram_cfg = (in_be32(&ddr->sdram_cfg) & ~SDRAM_CFG_BI);
Tang Yuantian064f1262014-11-21 11:17:15 +0800399
400 /* Let the controller go */
Poonam_Aggrwal-b1081230cb1452009-01-04 08:46:38 +0530401 out_be32(&ddr->sdram_cfg, temp_sdram_cfg | SDRAM_CFG_MEM_EN);
York Sun922f40f2011-01-10 12:03:01 +0000402 asm volatile("sync;isync");
Kumar Galad5a1fb92008-08-26 21:34:55 -0500403
York Sun016095d2012-10-08 07:44:24 +0000404 total_gb_size_per_controller = 0;
405 for (i = 0; i < CONFIG_CHIP_SELECTS_PER_CTRL; i++) {
406 if (!(regs->cs[i].config & 0x80000000))
407 continue;
408 total_gb_size_per_controller += 1 << (
409 ((regs->cs[i].config >> 14) & 0x3) + 2 +
410 ((regs->cs[i].config >> 8) & 0x7) + 12 +
411 ((regs->cs[i].config >> 0) & 0x7) + 8 +
412 3 - ((regs->ddr_sdram_cfg >> 19) & 0x3) -
413 26); /* minus 26 (count of 64M) */
414 }
415 if (fsl_ddr_get_intl3r() & 0x80000000) /* 3-way interleaving */
416 total_gb_size_per_controller *= 3;
417 else if (regs->cs[0].config & 0x20000000) /* 2-way interleaving */
418 total_gb_size_per_controller <<= 1;
419 /*
420 * total memory / bus width = transactions needed
421 * transactions needed / data rate = seconds
422 * to add plenty of buffer, double the time
423 * For example, 2GB on 666MT/s 64-bit bus takes about 402ms
424 * Let's wait for 800ms
425 */
426 bus_width = 3 - ((ddr->sdram_cfg & SDRAM_CFG_DBW_MASK)
427 >> SDRAM_CFG_DBW_SHIFT);
428 timeout = ((total_gb_size_per_controller << (6 - bus_width)) * 100 /
York Sun2c0b62d2015-01-06 13:18:50 -0800429 (get_ddr_freq(ctrl_num) >> 20)) << 1;
Andy Fleming1bc8b042012-10-22 17:28:18 -0500430#ifdef CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134
York Sun016095d2012-10-08 07:44:24 +0000431 timeout_save = timeout;
Andy Fleming1bc8b042012-10-22 17:28:18 -0500432#endif
York Sun016095d2012-10-08 07:44:24 +0000433 total_gb_size_per_controller >>= 4; /* shift down to gb size */
434 debug("total %d GB\n", total_gb_size_per_controller);
435 debug("Need to wait up to %d * 10ms\n", timeout);
436
Kumar Galad5a1fb92008-08-26 21:34:55 -0500437 /* Poll DDR_SDRAM_CFG_2[D_INIT] bit until auto-data init is done. */
York Sun016095d2012-10-08 07:44:24 +0000438 while ((in_be32(&ddr->sdram_cfg_2) & SDRAM_CFG2_D_INIT) &&
439 (timeout >= 0)) {
Kumar Galad5a1fb92008-08-26 21:34:55 -0500440 udelay(10000); /* throttle polling rate */
York Sun016095d2012-10-08 07:44:24 +0000441 timeout--;
442 }
443
444 if (timeout <= 0)
445 printf("Waiting for D_INIT timeout. Memory may not work.\n");
York Sunc8fc9592011-01-25 22:05:49 -0800446
447#ifdef CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134
448 /* continue this workaround */
449
450 /* 4. Clear DEBUG3[21] */
451 clrbits_be32(&ddr->debug[2], 0x400);
452 debug("Clearing D3[21] to 0x%08x\n", in_be32(&ddr->debug[2]));
453
454 /* DDR134 workaround starts */
455 /* A: Clear sdram_cfg_2[odt_cfg] */
456 clrbits_be32(&ddr->sdram_cfg_2, SDRAM_CFG2_ODT_CFG_MASK);
457 debug("Clearing SDRAM_CFG2[ODT_CFG] to 0x%08x\n",
458 in_be32(&ddr->sdram_cfg_2));
459
460 /* B: Set DEBUG1[15] */
461 setbits_be32(&ddr->debug[0], 0x10000);
462 debug("Setting D1[15] to 0x%08x\n", in_be32(&ddr->debug[0]));
463
464 /* C: Set timing_cfg_2[cpo] to 0b11111 */
465 setbits_be32(&ddr->timing_cfg_2, TIMING_CFG_2_CPO_MASK);
466 debug("Setting TMING_CFG_2[CPO] to 0x%08x\n",
467 in_be32(&ddr->timing_cfg_2));
468
469 /* D: Set D6 to 0x9f9f9f9f */
470 out_be32(&ddr->debug[5], 0x9f9f9f9f);
471 debug("Setting D6 to 0x%08x\n", in_be32(&ddr->debug[5]));
472
473 /* E: Set D7 to 0x9f9f9f9f */
474 out_be32(&ddr->debug[6], 0x9f9f9f9f);
475 debug("Setting D7 to 0x%08x\n", in_be32(&ddr->debug[6]));
476
477 /* F: Set D2[20] */
478 setbits_be32(&ddr->debug[1], 0x800);
479 debug("Setting D2[20] to 0x%08x\n", in_be32(&ddr->debug[1]));
480
481 /* G: Poll on D2[20] until cleared */
482 while (in_be32(&ddr->debug[1]) & 0x800)
483 udelay(10000); /* throttle polling rate */
484
485 /* H: Clear D1[15] */
486 clrbits_be32(&ddr->debug[0], 0x10000);
487 debug("Setting D1[15] to 0x%08x\n", in_be32(&ddr->debug[0]));
488
489 /* I: Set sdram_cfg_2[odt_cfg] */
490 setbits_be32(&ddr->sdram_cfg_2,
491 regs->ddr_sdram_cfg_2 & SDRAM_CFG2_ODT_CFG_MASK);
492 debug("Setting sdram_cfg_2 to 0x%08x\n", in_be32(&ddr->sdram_cfg_2));
493
494 /* Continuing with the DDR111 workaround */
495 /* 5. Set D2[21] */
496 setbits_be32(&ddr->debug[1], 0x400);
497 debug("Setting D2[21] to 0x%08x\n", in_be32(&ddr->debug[1]));
498
499 /* 6. Poll D2[21] until its cleared */
500 while (in_be32(&ddr->debug[1]) & 0x400)
501 udelay(10000); /* throttle polling rate */
502
York Sun016095d2012-10-08 07:44:24 +0000503 /* 7. Wait for state machine 2nd run, roughly 400ms/GB */
504 debug("Wait for %d * 10ms\n", timeout_save);
505 udelay(timeout_save * 10000);
York Sunc8fc9592011-01-25 22:05:49 -0800506
507 /* 8. Set sdram_cfg_2[dinit] if options requires */
508 setbits_be32(&ddr->sdram_cfg_2,
509 regs->ddr_sdram_cfg_2 & SDRAM_CFG2_D_INIT);
510 debug("Setting sdram_cfg_2 to 0x%08x\n", in_be32(&ddr->sdram_cfg_2));
511
512 /* 9. Poll until dinit is cleared */
York Sun016095d2012-10-08 07:44:24 +0000513 timeout = timeout_save;
514 debug("Need to wait up to %d * 10ms\n", timeout);
515 while ((in_be32(&ddr->sdram_cfg_2) & SDRAM_CFG2_D_INIT) &&
516 (timeout >= 0)) {
517 udelay(10000); /* throttle polling rate */
518 timeout--;
519 }
520
521 if (timeout <= 0)
522 printf("Waiting for D_INIT timeout. Memory may not work.\n");
York Sunc8fc9592011-01-25 22:05:49 -0800523
524 /* 10. Clear EEBACR[3] */
525 clrbits_be32(&ecm->eebacr, 10000000);
526 debug("Clearing EEBACR[3] to 0x%08x\n", in_be32(&ecm->eebacr));
York Sun7d9781b2011-03-17 11:18:13 -0700527
528 if (csn != -1) {
529 csn_bnds_t = (unsigned int *) &regs->cs[csn].bnds;
530 *csn_bnds_t = csn_bnds_backup;
531 debug("Change cs%d_bnds back to 0x%08x\n",
532 csn, regs->cs[csn].bnds);
533 setbits_be32(&ddr->sdram_cfg, 0x2); /* MEM_HALT */
534 switch (csn) {
535 case 0:
536 out_be32(&ddr->cs0_bnds, regs->cs[csn].bnds);
537 break;
538 case 1:
539 out_be32(&ddr->cs1_bnds, regs->cs[csn].bnds);
540 break;
York Sun2c0b62d2015-01-06 13:18:50 -0800541#if CONFIG_CHIP_SELECTS_PER_CTRL > 2
York Sun7d9781b2011-03-17 11:18:13 -0700542 case 2:
543 out_be32(&ddr->cs2_bnds, regs->cs[csn].bnds);
544 break;
545 case 3:
546 out_be32(&ddr->cs3_bnds, regs->cs[csn].bnds);
547 break;
York Sun2c0b62d2015-01-06 13:18:50 -0800548#endif
York Sun7d9781b2011-03-17 11:18:13 -0700549 }
550 clrbits_be32(&ddr->sdram_cfg, 0x2);
551 }
York Sunc8fc9592011-01-25 22:05:49 -0800552#endif /* CONFIG_SYS_FSL_ERRATUM_DDR111_DDR134 */
Tang Yuantiana7364af2014-04-17 15:33:46 +0800553#ifdef CONFIG_DEEP_SLEEP
Tang Yuantian064f1262014-11-21 11:17:15 +0800554 if (is_warm_boot())
Tang Yuantiana7364af2014-04-17 15:33:46 +0800555 /* exit self-refresh */
Tang Yuantian064f1262014-11-21 11:17:15 +0800556 clrbits_be32(&ddr->sdram_cfg_2, SDRAM_CFG2_FRC_SR);
Tang Yuantiana7364af2014-04-17 15:33:46 +0800557#endif
Kumar Galad5a1fb92008-08-26 21:34:55 -0500558}