3 * Sylvie Gohl, AMCC/IBM, gohl.sylvie@fr.ibm.com
4 * Jacqueline Pira-Ferriol, AMCC/IBM, jpira-ferriol@fr.ibm.com
5 * Thierry Roman, AMCC/IBM, thierry_roman@fr.ibm.com
6 * Alain Saurel, AMCC/IBM, alain.saurel@fr.ibm.com
7 * Robert Snyder, AMCC/IBM, rob.snyder@fr.ibm.com
9 * (C) Copyright 2006-2007
10 * Stefan Roese, DENX Software Engineering, sr@denx.de.
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 /* define DEBUG for debug output */
32 #include <asm/processor.h>
38 #if !defined(CONFIG_NAND_U_BOOT) || defined(CONFIG_NAND_SPL) || \
39 defined(CONFIG_DDR_DATA_EYE)
40 /*-----------------------------------------------------------------------------+
42 +----------------------------------------------------------------------------*/
43 static int wait_for_dlllock(void)
48 /* -----------------------------------------------------------+
49 * Wait for the DCC master delay line to finish calibration
50 * ----------------------------------------------------------*/
51 mtdcr(ddrcfga, DDR0_17);
52 val = DDR0_17_DLLLOCKREG_UNLOCKED;
54 while (wait != 0xffff) {
56 if ((val & DDR0_17_DLLLOCKREG_MASK) == DDR0_17_DLLLOCKREG_LOCKED)
57 /* dlllockreg bit on */
62 debug("0x%04x: DDR0_17 Value (dlllockreg bit): 0x%08x\n", wait, val);
63 debug("Waiting for dlllockreg bit to raise\n");
69 #if defined(CONFIG_DDR_DATA_EYE)
70 /*-----------------------------------------------------------------------------+
71 * wait_for_dram_init_complete.
72 +----------------------------------------------------------------------------*/
73 int wait_for_dram_init_complete(void)
78 /* --------------------------------------------------------------+
79 * Wait for 'DRAM initialization complete' bit in status register
80 * -------------------------------------------------------------*/
81 mtdcr(ddrcfga, DDR0_00);
83 while (wait != 0xffff) {
85 if ((val & DDR0_00_INT_STATUS_BIT6) == DDR0_00_INT_STATUS_BIT6)
86 /* 'DRAM initialization complete' bit */
92 debug("DRAM initialization complete bit in status register did not rise\n");
100 /*-----------------------------------------------------------------------------+
101 * denali_core_search_data_eye.
102 +----------------------------------------------------------------------------*/
103 void denali_core_search_data_eye(unsigned long memory_size)
107 u32 wr_dqs_shift, dqs_out_shift, dll_dqs_delay_X;
108 u32 max_passing_cases = 0, wr_dqs_shift_with_max_passing_cases = 0;
109 u32 passing_cases = 0, dll_dqs_delay_X_sw_val = 0;
110 u32 dll_dqs_delay_X_start_window = 0, dll_dqs_delay_X_end_window = 0;
111 volatile u32 *ram_pointer;
112 u32 test[NUM_TRIES] = {
113 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
114 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
115 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
116 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
117 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
118 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
119 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
120 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
121 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
122 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
123 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
124 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
125 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
126 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
127 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55,
128 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55 };
130 ram_pointer = (volatile u32 *)(CFG_SDRAM_BASE);
132 for (wr_dqs_shift = 64; wr_dqs_shift < 96; wr_dqs_shift++) {
133 /*for (wr_dqs_shift=1; wr_dqs_shift<96; wr_dqs_shift++) {*/
135 /* -----------------------------------------------------------+
136 * De-assert 'start' parameter.
137 * ----------------------------------------------------------*/
138 mtdcr(ddrcfga, DDR0_02);
139 val = (mfdcr(ddrcfgd) & ~DDR0_02_START_MASK) | DDR0_02_START_OFF;
142 /* -----------------------------------------------------------+
144 * ----------------------------------------------------------*/
145 mtdcr(ddrcfga, DDR0_09);
146 val = (mfdcr(ddrcfgd) & ~DDR0_09_WR_DQS_SHIFT_MASK)
147 | DDR0_09_WR_DQS_SHIFT_ENCODE(wr_dqs_shift);
150 /* -----------------------------------------------------------+
151 * Set 'dqs_out_shift' = wr_dqs_shift + 32
152 * ----------------------------------------------------------*/
153 dqs_out_shift = wr_dqs_shift + 32;
154 mtdcr(ddrcfga, DDR0_22);
155 val = (mfdcr(ddrcfgd) & ~DDR0_22_DQS_OUT_SHIFT_MASK)
156 | DDR0_22_DQS_OUT_SHIFT_ENCODE(dqs_out_shift);
161 for (dll_dqs_delay_X = 1; dll_dqs_delay_X < 64; dll_dqs_delay_X++) {
162 /*for (dll_dqs_delay_X=1; dll_dqs_delay_X<128; dll_dqs_delay_X++) {*/
163 /* -----------------------------------------------------------+
164 * Set 'dll_dqs_delay_X'.
165 * ----------------------------------------------------------*/
166 /* dll_dqs_delay_0 */
167 mtdcr(ddrcfga, DDR0_17);
168 val = (mfdcr(ddrcfgd) & ~DDR0_17_DLL_DQS_DELAY_0_MASK)
169 | DDR0_17_DLL_DQS_DELAY_0_ENCODE(dll_dqs_delay_X);
171 /* dll_dqs_delay_1 to dll_dqs_delay_4 */
172 mtdcr(ddrcfga, DDR0_18);
173 val = (mfdcr(ddrcfgd) & ~DDR0_18_DLL_DQS_DELAY_X_MASK)
174 | DDR0_18_DLL_DQS_DELAY_4_ENCODE(dll_dqs_delay_X)
175 | DDR0_18_DLL_DQS_DELAY_3_ENCODE(dll_dqs_delay_X)
176 | DDR0_18_DLL_DQS_DELAY_2_ENCODE(dll_dqs_delay_X)
177 | DDR0_18_DLL_DQS_DELAY_1_ENCODE(dll_dqs_delay_X);
179 /* dll_dqs_delay_5 to dll_dqs_delay_8 */
180 mtdcr(ddrcfga, DDR0_19);
181 val = (mfdcr(ddrcfgd) & ~DDR0_19_DLL_DQS_DELAY_X_MASK)
182 | DDR0_19_DLL_DQS_DELAY_8_ENCODE(dll_dqs_delay_X)
183 | DDR0_19_DLL_DQS_DELAY_7_ENCODE(dll_dqs_delay_X)
184 | DDR0_19_DLL_DQS_DELAY_6_ENCODE(dll_dqs_delay_X)
185 | DDR0_19_DLL_DQS_DELAY_5_ENCODE(dll_dqs_delay_X);
191 /* -----------------------------------------------------------+
192 * Assert 'start' parameter.
193 * ----------------------------------------------------------*/
194 mtdcr(ddrcfga, DDR0_02);
195 val = (mfdcr(ddrcfgd) & ~DDR0_02_START_MASK) | DDR0_02_START_ON;
201 /* -----------------------------------------------------------+
202 * Wait for the DCC master delay line to finish calibration
203 * ----------------------------------------------------------*/
204 if (wait_for_dlllock() != 0) {
205 printf("dlllock did not occur !!!\n");
206 printf("denali_core_search_data_eye!!!\n");
207 printf("wr_dqs_shift = %d - dll_dqs_delay_X = %d\n",
208 wr_dqs_shift, dll_dqs_delay_X);
214 if (wait_for_dram_init_complete() != 0) {
215 printf("dram init complete did not occur !!!\n");
216 printf("denali_core_search_data_eye!!!\n");
217 printf("wr_dqs_shift = %d - dll_dqs_delay_X = %d\n",
218 wr_dqs_shift, dll_dqs_delay_X);
221 udelay(100); /* wait 100us to ensure init is really completed !!! */
224 for (j=0; j<NUM_TRIES; j++) {
225 ram_pointer[j] = test[j];
227 /* clear any cache at ram location */
228 __asm__("dcbf 0,%0": :"r" (&ram_pointer[j]));
231 /* read values back */
232 for (j=0; j<NUM_TRIES; j++) {
233 for (k=0; k<NUM_READS; k++) {
234 /* clear any cache at ram location */
235 __asm__("dcbf 0,%0": :"r" (&ram_pointer[j]));
237 if (ram_pointer[j] != test[j])
246 /* See if the dll_dqs_delay_X value passed.*/
253 if (passing_cases == 0)
254 dll_dqs_delay_X_sw_val = dll_dqs_delay_X;
256 if (passing_cases >= max_passing_cases) {
257 max_passing_cases = passing_cases;
258 wr_dqs_shift_with_max_passing_cases = wr_dqs_shift;
259 dll_dqs_delay_X_start_window = dll_dqs_delay_X_sw_val;
260 dll_dqs_delay_X_end_window = dll_dqs_delay_X;
264 /* -----------------------------------------------------------+
265 * De-assert 'start' parameter.
266 * ----------------------------------------------------------*/
267 mtdcr(ddrcfga, DDR0_02);
268 val = (mfdcr(ddrcfgd) & ~DDR0_02_START_MASK) | DDR0_02_START_OFF;
271 } /* for (dll_dqs_delay_X=0; dll_dqs_delay_X<128; dll_dqs_delay_X++) */
273 } /* for (wr_dqs_shift=0; wr_dqs_shift<96; wr_dqs_shift++) */
275 /* -----------------------------------------------------------+
276 * Largest passing window is now detected.
277 * ----------------------------------------------------------*/
279 /* Compute dll_dqs_delay_X value */
280 dll_dqs_delay_X = (dll_dqs_delay_X_end_window + dll_dqs_delay_X_start_window) / 2;
281 wr_dqs_shift = wr_dqs_shift_with_max_passing_cases;
283 debug("DQS calibration - Window detected:\n");
284 debug("max_passing_cases = %d\n", max_passing_cases);
285 debug("wr_dqs_shift = %d\n", wr_dqs_shift);
286 debug("dll_dqs_delay_X = %d\n", dll_dqs_delay_X);
287 debug("dll_dqs_delay_X window = %d - %d\n",
288 dll_dqs_delay_X_start_window, dll_dqs_delay_X_end_window);
290 /* -----------------------------------------------------------+
291 * De-assert 'start' parameter.
292 * ----------------------------------------------------------*/
293 mtdcr(ddrcfga, DDR0_02);
294 val = (mfdcr(ddrcfgd) & ~DDR0_02_START_MASK) | DDR0_02_START_OFF;
297 /* -----------------------------------------------------------+
299 * ----------------------------------------------------------*/
300 mtdcr(ddrcfga, DDR0_09);
301 val = (mfdcr(ddrcfgd) & ~DDR0_09_WR_DQS_SHIFT_MASK)
302 | DDR0_09_WR_DQS_SHIFT_ENCODE(wr_dqs_shift);
304 debug("DDR0_09=0x%08lx\n", val);
306 /* -----------------------------------------------------------+
307 * Set 'dqs_out_shift' = wr_dqs_shift + 32
308 * ----------------------------------------------------------*/
309 dqs_out_shift = wr_dqs_shift + 32;
310 mtdcr(ddrcfga, DDR0_22);
311 val = (mfdcr(ddrcfgd) & ~DDR0_22_DQS_OUT_SHIFT_MASK)
312 | DDR0_22_DQS_OUT_SHIFT_ENCODE(dqs_out_shift);
314 debug("DDR0_22=0x%08lx\n", val);
316 /* -----------------------------------------------------------+
317 * Set 'dll_dqs_delay_X'.
318 * ----------------------------------------------------------*/
319 /* dll_dqs_delay_0 */
320 mtdcr(ddrcfga, DDR0_17);
321 val = (mfdcr(ddrcfgd) & ~DDR0_17_DLL_DQS_DELAY_0_MASK)
322 | DDR0_17_DLL_DQS_DELAY_0_ENCODE(dll_dqs_delay_X);
324 debug("DDR0_17=0x%08lx\n", val);
326 /* dll_dqs_delay_1 to dll_dqs_delay_4 */
327 mtdcr(ddrcfga, DDR0_18);
328 val = (mfdcr(ddrcfgd) & ~DDR0_18_DLL_DQS_DELAY_X_MASK)
329 | DDR0_18_DLL_DQS_DELAY_4_ENCODE(dll_dqs_delay_X)
330 | DDR0_18_DLL_DQS_DELAY_3_ENCODE(dll_dqs_delay_X)
331 | DDR0_18_DLL_DQS_DELAY_2_ENCODE(dll_dqs_delay_X)
332 | DDR0_18_DLL_DQS_DELAY_1_ENCODE(dll_dqs_delay_X);
334 debug("DDR0_18=0x%08lx\n", val);
336 /* dll_dqs_delay_5 to dll_dqs_delay_8 */
337 mtdcr(ddrcfga, DDR0_19);
338 val = (mfdcr(ddrcfgd) & ~DDR0_19_DLL_DQS_DELAY_X_MASK)
339 | DDR0_19_DLL_DQS_DELAY_8_ENCODE(dll_dqs_delay_X)
340 | DDR0_19_DLL_DQS_DELAY_7_ENCODE(dll_dqs_delay_X)
341 | DDR0_19_DLL_DQS_DELAY_6_ENCODE(dll_dqs_delay_X)
342 | DDR0_19_DLL_DQS_DELAY_5_ENCODE(dll_dqs_delay_X);
344 debug("DDR0_19=0x%08lx\n", val);
346 /* -----------------------------------------------------------+
347 * Assert 'start' parameter.
348 * ----------------------------------------------------------*/
349 mtdcr(ddrcfga, DDR0_02);
350 val = (mfdcr(ddrcfgd) & ~DDR0_02_START_MASK) | DDR0_02_START_ON;
356 /* -----------------------------------------------------------+
357 * Wait for the DCC master delay line to finish calibration
358 * ----------------------------------------------------------*/
359 if (wait_for_dlllock() != 0) {
360 printf("dlllock did not occur !!!\n");
366 if (wait_for_dram_init_complete() != 0) {
367 printf("dram init complete did not occur !!!\n");
370 udelay(100); /* wait 100us to ensure init is really completed !!! */
372 #endif /* CONFIG_DDR_DATA_EYE */
374 #if defined(CONFIG_NAND_SPL)
375 /* Using cpu/ppc4xx/speed.c to calculate the bus frequency is too big
376 * for the 4k NAND boot image so define bus_frequency to 133MHz here
377 * which is save for the refresh counter setup.
379 #define get_bus_freq(val) 133000000
382 /*************************************************************************
384 * initdram -- 440EPx's DDR controller is a DENALI Core
386 ************************************************************************/
387 long int initdram (int board_type)
389 #if !defined(CONFIG_NAND_U_BOOT) || defined(CONFIG_NAND_SPL)
390 #if !defined(CONFIG_NAND_SPL)
391 ulong speed = get_bus_freq(0);
393 ulong speed = 133333333; /* 133MHz is on the safe side */
396 mtsdram(DDR0_02, 0x00000000);
398 mtsdram(DDR0_00, 0x0000190A);
399 mtsdram(DDR0_01, 0x01000000);
400 mtsdram(DDR0_03, 0x02030602);
401 mtsdram(DDR0_04, 0x0A020200);
402 mtsdram(DDR0_05, 0x02020308);
403 mtsdram(DDR0_06, 0x0102C812);
404 mtsdram(DDR0_07, 0x000D0100);
405 mtsdram(DDR0_08, 0x02430001);
406 mtsdram(DDR0_09, 0x00011D5F);
407 mtsdram(DDR0_10, 0x00000300);
408 mtsdram(DDR0_11, 0x0027C800);
409 mtsdram(DDR0_12, 0x00000003);
410 mtsdram(DDR0_14, 0x00000000);
411 mtsdram(DDR0_17, 0x19000000);
412 mtsdram(DDR0_18, 0x19191919);
413 mtsdram(DDR0_19, 0x19191919);
414 mtsdram(DDR0_20, 0x0B0B0B0B);
415 mtsdram(DDR0_21, 0x0B0B0B0B);
416 mtsdram(DDR0_22, 0x00267F0B);
417 mtsdram(DDR0_23, 0x00000000);
418 mtsdram(DDR0_24, 0x01010002);
419 if (speed > 133333334)
420 mtsdram(DDR0_26, 0x5B26050C);
422 mtsdram(DDR0_26, 0x5B260408);
423 mtsdram(DDR0_27, 0x0000682B);
424 mtsdram(DDR0_28, 0x00000000);
425 mtsdram(DDR0_31, 0x00000000);
426 mtsdram(DDR0_42, 0x01000006);
427 mtsdram(DDR0_43, 0x030A0200);
428 mtsdram(DDR0_44, 0x00000003);
429 mtsdram(DDR0_02, 0x00000001);
432 #endif /* #ifndef CONFIG_NAND_U_BOOT */
434 #ifdef CONFIG_DDR_DATA_EYE
435 /* -----------------------------------------------------------+
436 * Perform data eye search if requested.
437 * ----------------------------------------------------------*/
438 denali_core_search_data_eye(CFG_MBYTES_SDRAM << 20);
441 return (CFG_MBYTES_SDRAM << 20);