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
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 debugging output (obviously ;-)) */
34 #include <asm/processor.h>
42 * This DDR2 setup code can dynamically setup the TLB entries for the DDR2 memory
43 * region. Right now the cache should still be disabled in U-Boot because of the
44 * EMAC driver, that need it's buffer descriptor to be located in non cached
47 * If at some time this restriction doesn't apply anymore, just define
48 * CFG_ENABLE_SDRAM_CACHE in the board config file and this code should setup
49 * everything correctly.
51 #ifdef CFG_ENABLE_SDRAM_CACHE
52 #define MY_TLB_WORD2_I_ENABLE 0 /* enable caching on SDRAM */
54 #define MY_TLB_WORD2_I_ENABLE TLB_WORD2_I_ENABLE /* disable caching on SDRAM */
57 void dcbz_area(u32 start_address, u32 num_bytes);
60 static u32 is_ecc_enabled(void)
64 mfsdram(DDR0_22, val);
65 val &= DDR0_22_CTRL_RAW_MASK;
72 void board_add_ram_info(int use_default)
74 PPC440_SYS_INFO board_cfg;
82 get_sys_info(&board_cfg);
83 printf(" enabled, %d MHz", (board_cfg.freqPLB * 2) / 1000000);
85 mfsdram(DDR0_03, val);
86 val = DDR0_03_CASLAT_DECODE(val);
87 printf(", CL%d)", val);
90 static int wait_for_dlllock(void)
96 * Wait for the DCC master delay line to finish calibration
98 mtdcr(ddrcfga, DDR0_17);
99 val = DDR0_17_DLLLOCKREG_UNLOCKED;
101 while (wait != 0xffff) {
102 val = mfdcr(ddrcfgd);
103 if ((val & DDR0_17_DLLLOCKREG_MASK) == DDR0_17_DLLLOCKREG_LOCKED)
104 /* dlllockreg bit on */
109 debug("0x%04x: DDR0_17 Value (dlllockreg bit): 0x%08x\n", wait, val);
110 debug("Waiting for dlllockreg bit to raise\n");
115 #if defined(CONFIG_DDR_DATA_EYE)
116 int wait_for_dram_init_complete(void)
122 * Wait for 'DRAM initialization complete' bit in status register
124 mtdcr(ddrcfga, DDR0_00);
126 while (wait != 0xffff) {
127 val = mfdcr(ddrcfgd);
128 if ((val & DDR0_00_INT_STATUS_BIT6) == DDR0_00_INT_STATUS_BIT6)
129 /* 'DRAM initialization complete' bit */
135 debug("DRAM initialization complete bit in status register did not rise\n");
143 void denali_core_search_data_eye(u32 start_addr, u32 memory_size)
147 u32 wr_dqs_shift, dqs_out_shift, dll_dqs_delay_X;
148 u32 max_passing_cases = 0, wr_dqs_shift_with_max_passing_cases = 0;
149 u32 passing_cases = 0, dll_dqs_delay_X_sw_val = 0;
150 u32 dll_dqs_delay_X_start_window = 0, dll_dqs_delay_X_end_window = 0;
151 volatile u32 *ram_pointer;
152 u32 test[NUM_TRIES] = {
153 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
154 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
155 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
156 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
157 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
158 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
159 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
160 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
161 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
162 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
163 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
164 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
165 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
166 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
167 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55,
168 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55 };
170 ram_pointer = (volatile u32 *)start_addr;
172 for (wr_dqs_shift = 64; wr_dqs_shift < 96; wr_dqs_shift++) {
173 /*for (wr_dqs_shift=1; wr_dqs_shift<96; wr_dqs_shift++) {*/
176 * De-assert 'start' parameter.
178 mtdcr(ddrcfga, DDR0_02);
179 val = (mfdcr(ddrcfgd) & ~DDR0_02_START_MASK) | DDR0_02_START_OFF;
185 mtdcr(ddrcfga, DDR0_09);
186 val = (mfdcr(ddrcfgd) & ~DDR0_09_WR_DQS_SHIFT_MASK)
187 | DDR0_09_WR_DQS_SHIFT_ENCODE(wr_dqs_shift);
191 * Set 'dqs_out_shift' = wr_dqs_shift + 32
193 dqs_out_shift = wr_dqs_shift + 32;
194 mtdcr(ddrcfga, DDR0_22);
195 val = (mfdcr(ddrcfgd) & ~DDR0_22_DQS_OUT_SHIFT_MASK)
196 | DDR0_22_DQS_OUT_SHIFT_ENCODE(dqs_out_shift);
201 for (dll_dqs_delay_X = 1; dll_dqs_delay_X < 64; dll_dqs_delay_X++) {
202 /*for (dll_dqs_delay_X=1; dll_dqs_delay_X<128; dll_dqs_delay_X++) {*/
204 * Set 'dll_dqs_delay_X'.
206 /* dll_dqs_delay_0 */
207 mtdcr(ddrcfga, DDR0_17);
208 val = (mfdcr(ddrcfgd) & ~DDR0_17_DLL_DQS_DELAY_0_MASK)
209 | DDR0_17_DLL_DQS_DELAY_0_ENCODE(dll_dqs_delay_X);
211 /* dll_dqs_delay_1 to dll_dqs_delay_4 */
212 mtdcr(ddrcfga, DDR0_18);
213 val = (mfdcr(ddrcfgd) & ~DDR0_18_DLL_DQS_DELAY_X_MASK)
214 | DDR0_18_DLL_DQS_DELAY_4_ENCODE(dll_dqs_delay_X)
215 | DDR0_18_DLL_DQS_DELAY_3_ENCODE(dll_dqs_delay_X)
216 | DDR0_18_DLL_DQS_DELAY_2_ENCODE(dll_dqs_delay_X)
217 | DDR0_18_DLL_DQS_DELAY_1_ENCODE(dll_dqs_delay_X);
219 /* dll_dqs_delay_5 to dll_dqs_delay_8 */
220 mtdcr(ddrcfga, DDR0_19);
221 val = (mfdcr(ddrcfgd) & ~DDR0_19_DLL_DQS_DELAY_X_MASK)
222 | DDR0_19_DLL_DQS_DELAY_8_ENCODE(dll_dqs_delay_X)
223 | DDR0_19_DLL_DQS_DELAY_7_ENCODE(dll_dqs_delay_X)
224 | DDR0_19_DLL_DQS_DELAY_6_ENCODE(dll_dqs_delay_X)
225 | DDR0_19_DLL_DQS_DELAY_5_ENCODE(dll_dqs_delay_X);
232 * Assert 'start' parameter.
234 mtdcr(ddrcfga, DDR0_02);
235 val = (mfdcr(ddrcfgd) & ~DDR0_02_START_MASK) | DDR0_02_START_ON;
242 * Wait for the DCC master delay line to finish calibration
244 if (wait_for_dlllock() != 0) {
245 printf("dlllock did not occur !!!\n");
246 printf("denali_core_search_data_eye!!!\n");
247 printf("wr_dqs_shift = %d - dll_dqs_delay_X = %d\n",
248 wr_dqs_shift, dll_dqs_delay_X);
254 if (wait_for_dram_init_complete() != 0) {
255 printf("dram init complete did not occur !!!\n");
256 printf("denali_core_search_data_eye!!!\n");
257 printf("wr_dqs_shift = %d - dll_dqs_delay_X = %d\n",
258 wr_dqs_shift, dll_dqs_delay_X);
261 udelay(100); /* wait 100us to ensure init is really completed !!! */
264 for (j=0; j<NUM_TRIES; j++) {
265 ram_pointer[j] = test[j];
267 /* clear any cache at ram location */
268 __asm__("dcbf 0,%0": :"r" (&ram_pointer[j]));
271 /* read values back */
272 for (j=0; j<NUM_TRIES; j++) {
273 for (k=0; k<NUM_READS; k++) {
274 /* clear any cache at ram location */
275 __asm__("dcbf 0,%0": :"r" (&ram_pointer[j]));
277 if (ram_pointer[j] != test[j])
286 /* See if the dll_dqs_delay_X value passed.*/
293 if (passing_cases == 0)
294 dll_dqs_delay_X_sw_val = dll_dqs_delay_X;
296 if (passing_cases >= max_passing_cases) {
297 max_passing_cases = passing_cases;
298 wr_dqs_shift_with_max_passing_cases = wr_dqs_shift;
299 dll_dqs_delay_X_start_window = dll_dqs_delay_X_sw_val;
300 dll_dqs_delay_X_end_window = dll_dqs_delay_X;
305 * De-assert 'start' parameter.
307 mtdcr(ddrcfga, DDR0_02);
308 val = (mfdcr(ddrcfgd) & ~DDR0_02_START_MASK) | DDR0_02_START_OFF;
311 } /* for (dll_dqs_delay_X=0; dll_dqs_delay_X<128; dll_dqs_delay_X++) */
313 } /* for (wr_dqs_shift=0; wr_dqs_shift<96; wr_dqs_shift++) */
316 * Largest passing window is now detected.
319 /* Compute dll_dqs_delay_X value */
320 dll_dqs_delay_X = (dll_dqs_delay_X_end_window + dll_dqs_delay_X_start_window) / 2;
321 wr_dqs_shift = wr_dqs_shift_with_max_passing_cases;
323 debug("DQS calibration - Window detected:\n");
324 debug("max_passing_cases = %d\n", max_passing_cases);
325 debug("wr_dqs_shift = %d\n", wr_dqs_shift);
326 debug("dll_dqs_delay_X = %d\n", dll_dqs_delay_X);
327 debug("dll_dqs_delay_X window = %d - %d\n",
328 dll_dqs_delay_X_start_window, dll_dqs_delay_X_end_window);
331 * De-assert 'start' parameter.
333 mtdcr(ddrcfga, DDR0_02);
334 val = (mfdcr(ddrcfgd) & ~DDR0_02_START_MASK) | DDR0_02_START_OFF;
340 mtdcr(ddrcfga, DDR0_09);
341 val = (mfdcr(ddrcfgd) & ~DDR0_09_WR_DQS_SHIFT_MASK)
342 | DDR0_09_WR_DQS_SHIFT_ENCODE(wr_dqs_shift);
344 debug("DDR0_09=0x%08lx\n", val);
347 * Set 'dqs_out_shift' = wr_dqs_shift + 32
349 dqs_out_shift = wr_dqs_shift + 32;
350 mtdcr(ddrcfga, DDR0_22);
351 val = (mfdcr(ddrcfgd) & ~DDR0_22_DQS_OUT_SHIFT_MASK)
352 | DDR0_22_DQS_OUT_SHIFT_ENCODE(dqs_out_shift);
354 debug("DDR0_22=0x%08lx\n", val);
357 * Set 'dll_dqs_delay_X'.
359 /* dll_dqs_delay_0 */
360 mtdcr(ddrcfga, DDR0_17);
361 val = (mfdcr(ddrcfgd) & ~DDR0_17_DLL_DQS_DELAY_0_MASK)
362 | DDR0_17_DLL_DQS_DELAY_0_ENCODE(dll_dqs_delay_X);
364 debug("DDR0_17=0x%08lx\n", val);
366 /* dll_dqs_delay_1 to dll_dqs_delay_4 */
367 mtdcr(ddrcfga, DDR0_18);
368 val = (mfdcr(ddrcfgd) & ~DDR0_18_DLL_DQS_DELAY_X_MASK)
369 | DDR0_18_DLL_DQS_DELAY_4_ENCODE(dll_dqs_delay_X)
370 | DDR0_18_DLL_DQS_DELAY_3_ENCODE(dll_dqs_delay_X)
371 | DDR0_18_DLL_DQS_DELAY_2_ENCODE(dll_dqs_delay_X)
372 | DDR0_18_DLL_DQS_DELAY_1_ENCODE(dll_dqs_delay_X);
374 debug("DDR0_18=0x%08lx\n", val);
376 /* dll_dqs_delay_5 to dll_dqs_delay_8 */
377 mtdcr(ddrcfga, DDR0_19);
378 val = (mfdcr(ddrcfgd) & ~DDR0_19_DLL_DQS_DELAY_X_MASK)
379 | DDR0_19_DLL_DQS_DELAY_8_ENCODE(dll_dqs_delay_X)
380 | DDR0_19_DLL_DQS_DELAY_7_ENCODE(dll_dqs_delay_X)
381 | DDR0_19_DLL_DQS_DELAY_6_ENCODE(dll_dqs_delay_X)
382 | DDR0_19_DLL_DQS_DELAY_5_ENCODE(dll_dqs_delay_X);
384 debug("DDR0_19=0x%08lx\n", val);
387 * Assert 'start' parameter.
389 mtdcr(ddrcfga, DDR0_02);
390 val = (mfdcr(ddrcfgd) & ~DDR0_02_START_MASK) | DDR0_02_START_ON;
397 * Wait for the DCC master delay line to finish calibration
399 if (wait_for_dlllock() != 0) {
400 printf("dlllock did not occur !!!\n");
406 if (wait_for_dram_init_complete() != 0) {
407 printf("dram init complete did not occur !!!\n");
410 udelay(100); /* wait 100us to ensure init is really completed !!! */
412 #endif /* CONFIG_DDR_DATA_EYE */
414 #ifdef CONFIG_DDR_ECC
415 static void wait_ddr_idle(void)
418 * Controller idle status cannot be determined for Denali
419 * DDR2 code. Just return here.
423 static void blank_string(int size)
427 for (i=0; i<size; i++)
429 for (i=0; i<size; i++)
431 for (i=0; i<size; i++)
435 static void program_ecc(u32 start_address,
437 u32 tlb_word2_i_value)
441 u32 address_increment;
443 char str[] = "ECC generation -";
444 char slash[] = "\\|/-\\|/-";
448 current_address = start_address;
454 if (tlb_word2_i_value == TLB_WORD2_I_ENABLE) {
455 /* ECC bit set method for non-cached memory */
456 address_increment = 4;
457 end_address = current_address + num_bytes;
461 while (current_address < end_address) {
462 *((u32 *)current_address) = 0x00000000;
463 current_address += address_increment;
465 if ((loop++ % (2 << 20)) == 0) {
467 putc(slash[loopi++ % 8]);
471 blank_string(strlen(str));
473 /* ECC bit set method for cached memory */
474 #if 0 /* test-only: will remove this define later, when ECC problems are solved! */
476 * Some boards (like lwmon5) need to preserve the memory
477 * content upon ECC generation (for the log-buffer).
478 * Therefore we don't fill the memory with a pattern or
479 * just zero it, but write the same values back that are
480 * already in the memory cells.
482 address_increment = CFG_CACHELINE_SIZE;
483 end_address = current_address + num_bytes;
485 current_address = start_address;
486 while (current_address < end_address) {
488 * TODO: Th following sequence doesn't work correctly.
489 * Just invalidating and flushing the cache doesn't
490 * seem to trigger the re-write of the memory.
492 ppcDcbi(current_address);
493 ppcDcbf(current_address);
494 current_address += CFG_CACHELINE_SIZE;
497 dcbz_area(start_address, num_bytes);
506 /* Clear error status */
507 mfsdram(DDR0_00, val);
508 mtsdram(DDR0_00, val | DDR0_00_INT_ACK_ALL);
510 /* Set 'int_mask' parameter to functionnal value */
511 mfsdram(DDR0_01, val);
512 mtsdram(DDR0_01, ((val &~ DDR0_01_INT_MASK_MASK) | DDR0_01_INT_MASK_ALL_OFF));
520 /*************************************************************************
522 * initdram -- 440EPx's DDR controller is a DENALI Core
524 ************************************************************************/
525 long int initdram (int board_type)
527 #if 0 /* test-only: will remove this define later, when ECC problems are solved! */
529 mtsdram(DDR0_02, 0x00000000);
531 mtsdram(DDR0_00, 0x0000190A);
532 mtsdram(DDR0_01, 0x01000000);
533 mtsdram(DDR0_03, 0x02030603); /* A suitable burst length was taken. CAS is right for our board */
535 mtsdram(DDR0_04, 0x0A030300);
536 mtsdram(DDR0_05, 0x02020308);
537 mtsdram(DDR0_06, 0x0103C812);
538 mtsdram(DDR0_07, 0x00090100);
539 mtsdram(DDR0_08, 0x02c80001);
540 mtsdram(DDR0_09, 0x00011D5F);
541 mtsdram(DDR0_10, 0x00000300);
542 mtsdram(DDR0_11, 0x000CC800);
543 mtsdram(DDR0_12, 0x00000003);
544 mtsdram(DDR0_14, 0x00000000);
545 mtsdram(DDR0_17, 0x1e000000);
546 mtsdram(DDR0_18, 0x1e1e1e1e);
547 mtsdram(DDR0_19, 0x1e1e1e1e);
548 mtsdram(DDR0_20, 0x0B0B0B0B);
549 mtsdram(DDR0_21, 0x0B0B0B0B);
550 #ifdef CONFIG_DDR_ECC
551 mtsdram(DDR0_22, 0x00267F0B | DDR0_22_CTRL_RAW_ECC_ENABLE); /* enable ECC */
553 mtsdram(DDR0_22, 0x00267F0B);
556 mtsdram(DDR0_23, 0x01000000);
557 mtsdram(DDR0_24, 0x01010001);
559 mtsdram(DDR0_26, 0x2D93028A);
560 mtsdram(DDR0_27, 0x0784682B);
562 mtsdram(DDR0_28, 0x00000080);
563 mtsdram(DDR0_31, 0x00000000);
564 mtsdram(DDR0_42, 0x01000006);
566 mtsdram(DDR0_43, 0x030A0200);
567 mtsdram(DDR0_44, 0x00000003);
568 mtsdram(DDR0_02, 0x00000001); /* Activate the denali core */
571 mtsdram(DDR0_02, 0x00000000);
573 mtsdram(DDR0_00, 0x0000190A);
574 mtsdram(DDR0_01, 0x01000000);
575 mtsdram(DDR0_03, 0x02040803); /* A suitable burst length was taken. CAS is right for our board */
577 mtsdram(DDR0_04, 0x0B030300);
578 mtsdram(DDR0_05, 0x02020308);
579 mtsdram(DDR0_06, 0x0003C812);
580 mtsdram(DDR0_07, 0x00090100);
581 mtsdram(DDR0_08, 0x03c80001);
582 mtsdram(DDR0_09, 0x00011D5F);
583 mtsdram(DDR0_10, 0x00000300);
584 mtsdram(DDR0_11, 0x000CC800);
585 mtsdram(DDR0_12, 0x00000003);
586 mtsdram(DDR0_14, 0x00000000);
587 mtsdram(DDR0_17, 0x1e000000);
588 mtsdram(DDR0_18, 0x1e1e1e1e);
589 mtsdram(DDR0_19, 0x1e1e1e1e);
590 mtsdram(DDR0_20, 0x0B0B0B0B);
591 mtsdram(DDR0_21, 0x0B0B0B0B);
592 #ifdef CONFIG_DDR_ECC
593 mtsdram(DDR0_22, 0x00267F0B | DDR0_22_CTRL_RAW_ECC_ENABLE); /* enable ECC */
595 mtsdram(DDR0_22, 0x00267F0B);
598 mtsdram(DDR0_23, 0x01000000);
599 mtsdram(DDR0_24, 0x01010001);
601 mtsdram(DDR0_26, 0x2D93028A);
602 mtsdram(DDR0_27, 0x0784682B);
604 mtsdram(DDR0_28, 0x00000080);
605 mtsdram(DDR0_31, 0x00000000);
606 mtsdram(DDR0_42, 0x01000008);
608 mtsdram(DDR0_43, 0x050A0200);
609 mtsdram(DDR0_44, 0x00000005);
610 mtsdram(DDR0_02, 0x00000001); /* Activate the denali core */
616 * Program tlb entries for this size (dynamic)
618 program_tlb(0, 0, CFG_MBYTES_SDRAM << 20, MY_TLB_WORD2_I_ENABLE);
621 * Setup 2nd TLB with same physical address but different virtual address
622 * with cache enabled. This is done for fast ECC generation.
624 program_tlb(0, CFG_DDR_CACHED_ADDR, CFG_MBYTES_SDRAM << 20, 0);
626 #ifdef CONFIG_DDR_DATA_EYE
628 * Perform data eye search if requested.
630 denali_core_search_data_eye(CFG_DDR_CACHED_ADDR, CFG_MBYTES_SDRAM << 20);
633 #ifdef CONFIG_DDR_ECC
635 * If ECC is enabled, initialize the parity bits.
637 program_ecc(CFG_DDR_CACHED_ADDR, CFG_MBYTES_SDRAM << 20, 0);
641 * Clear possible errors resulting from data-eye-search.
642 * If not done, then we could get an interrupt later on when
643 * exceptions are enabled.
645 set_mcsr(get_mcsr());
647 return (CFG_MBYTES_SDRAM << 20);