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 program_tlb(u32 phys_addr, u32 virt_addr, u32 size, u32 tlb_word2_i_value);
58 void dcbz_area(u32 start_address, u32 num_bytes);
61 #ifdef CONFIG_ADD_RAM_INFO
62 static u32 is_ecc_enabled(void)
66 mfsdram(DDR0_22, val);
67 val &= DDR0_22_CTRL_RAW_MASK;
74 void board_add_ram_info(int use_default)
76 PPC440_SYS_INFO board_cfg;
84 get_sys_info(&board_cfg);
85 printf(" enabled, %d MHz", (board_cfg.freqPLB * 2) / 1000000);
87 mfsdram(DDR0_03, val);
88 val = DDR0_03_CASLAT_DECODE(val);
89 printf(", CL%d)", val);
93 static int wait_for_dlllock(void)
99 * Wait for the DCC master delay line to finish calibration
101 mtdcr(ddrcfga, DDR0_17);
102 val = DDR0_17_DLLLOCKREG_UNLOCKED;
104 while (wait != 0xffff) {
105 val = mfdcr(ddrcfgd);
106 if ((val & DDR0_17_DLLLOCKREG_MASK) == DDR0_17_DLLLOCKREG_LOCKED)
107 /* dlllockreg bit on */
112 debug("0x%04x: DDR0_17 Value (dlllockreg bit): 0x%08x\n", wait, val);
113 debug("Waiting for dlllockreg bit to raise\n");
118 #if defined(CONFIG_DDR_DATA_EYE)
119 int wait_for_dram_init_complete(void)
125 * Wait for 'DRAM initialization complete' bit in status register
127 mtdcr(ddrcfga, DDR0_00);
129 while (wait != 0xffff) {
130 val = mfdcr(ddrcfgd);
131 if ((val & DDR0_00_INT_STATUS_BIT6) == DDR0_00_INT_STATUS_BIT6)
132 /* 'DRAM initialization complete' bit */
138 debug("DRAM initialization complete bit in status register did not rise\n");
146 void denali_core_search_data_eye(u32 start_addr, u32 memory_size)
150 u32 wr_dqs_shift, dqs_out_shift, dll_dqs_delay_X;
151 u32 max_passing_cases = 0, wr_dqs_shift_with_max_passing_cases = 0;
152 u32 passing_cases = 0, dll_dqs_delay_X_sw_val = 0;
153 u32 dll_dqs_delay_X_start_window = 0, dll_dqs_delay_X_end_window = 0;
154 volatile u32 *ram_pointer;
155 u32 test[NUM_TRIES] = {
156 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
157 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
158 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
159 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
160 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
161 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
162 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
163 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
164 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
165 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
166 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
167 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
168 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
169 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
170 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55,
171 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55 };
173 ram_pointer = (volatile u32 *)start_addr;
175 for (wr_dqs_shift = 64; wr_dqs_shift < 96; wr_dqs_shift++) {
176 /*for (wr_dqs_shift=1; wr_dqs_shift<96; wr_dqs_shift++) {*/
179 * De-assert 'start' parameter.
181 mtdcr(ddrcfga, DDR0_02);
182 val = (mfdcr(ddrcfgd) & ~DDR0_02_START_MASK) | DDR0_02_START_OFF;
188 mtdcr(ddrcfga, DDR0_09);
189 val = (mfdcr(ddrcfgd) & ~DDR0_09_WR_DQS_SHIFT_MASK)
190 | DDR0_09_WR_DQS_SHIFT_ENCODE(wr_dqs_shift);
194 * Set 'dqs_out_shift' = wr_dqs_shift + 32
196 dqs_out_shift = wr_dqs_shift + 32;
197 mtdcr(ddrcfga, DDR0_22);
198 val = (mfdcr(ddrcfgd) & ~DDR0_22_DQS_OUT_SHIFT_MASK)
199 | DDR0_22_DQS_OUT_SHIFT_ENCODE(dqs_out_shift);
204 for (dll_dqs_delay_X = 1; dll_dqs_delay_X < 64; dll_dqs_delay_X++) {
205 /*for (dll_dqs_delay_X=1; dll_dqs_delay_X<128; dll_dqs_delay_X++) {*/
207 * Set 'dll_dqs_delay_X'.
209 /* dll_dqs_delay_0 */
210 mtdcr(ddrcfga, DDR0_17);
211 val = (mfdcr(ddrcfgd) & ~DDR0_17_DLL_DQS_DELAY_0_MASK)
212 | DDR0_17_DLL_DQS_DELAY_0_ENCODE(dll_dqs_delay_X);
214 /* dll_dqs_delay_1 to dll_dqs_delay_4 */
215 mtdcr(ddrcfga, DDR0_18);
216 val = (mfdcr(ddrcfgd) & ~DDR0_18_DLL_DQS_DELAY_X_MASK)
217 | DDR0_18_DLL_DQS_DELAY_4_ENCODE(dll_dqs_delay_X)
218 | DDR0_18_DLL_DQS_DELAY_3_ENCODE(dll_dqs_delay_X)
219 | DDR0_18_DLL_DQS_DELAY_2_ENCODE(dll_dqs_delay_X)
220 | DDR0_18_DLL_DQS_DELAY_1_ENCODE(dll_dqs_delay_X);
222 /* dll_dqs_delay_5 to dll_dqs_delay_8 */
223 mtdcr(ddrcfga, DDR0_19);
224 val = (mfdcr(ddrcfgd) & ~DDR0_19_DLL_DQS_DELAY_X_MASK)
225 | DDR0_19_DLL_DQS_DELAY_8_ENCODE(dll_dqs_delay_X)
226 | DDR0_19_DLL_DQS_DELAY_7_ENCODE(dll_dqs_delay_X)
227 | DDR0_19_DLL_DQS_DELAY_6_ENCODE(dll_dqs_delay_X)
228 | DDR0_19_DLL_DQS_DELAY_5_ENCODE(dll_dqs_delay_X);
235 * Assert 'start' parameter.
237 mtdcr(ddrcfga, DDR0_02);
238 val = (mfdcr(ddrcfgd) & ~DDR0_02_START_MASK) | DDR0_02_START_ON;
245 * Wait for the DCC master delay line to finish calibration
247 if (wait_for_dlllock() != 0) {
248 printf("dlllock did not occur !!!\n");
249 printf("denali_core_search_data_eye!!!\n");
250 printf("wr_dqs_shift = %d - dll_dqs_delay_X = %d\n",
251 wr_dqs_shift, dll_dqs_delay_X);
257 if (wait_for_dram_init_complete() != 0) {
258 printf("dram init complete did not occur !!!\n");
259 printf("denali_core_search_data_eye!!!\n");
260 printf("wr_dqs_shift = %d - dll_dqs_delay_X = %d\n",
261 wr_dqs_shift, dll_dqs_delay_X);
264 udelay(100); /* wait 100us to ensure init is really completed !!! */
267 for (j=0; j<NUM_TRIES; j++) {
268 ram_pointer[j] = test[j];
270 /* clear any cache at ram location */
271 __asm__("dcbf 0,%0": :"r" (&ram_pointer[j]));
274 /* read values back */
275 for (j=0; j<NUM_TRIES; j++) {
276 for (k=0; k<NUM_READS; k++) {
277 /* clear any cache at ram location */
278 __asm__("dcbf 0,%0": :"r" (&ram_pointer[j]));
280 if (ram_pointer[j] != test[j])
289 /* See if the dll_dqs_delay_X value passed.*/
296 if (passing_cases == 0)
297 dll_dqs_delay_X_sw_val = dll_dqs_delay_X;
299 if (passing_cases >= max_passing_cases) {
300 max_passing_cases = passing_cases;
301 wr_dqs_shift_with_max_passing_cases = wr_dqs_shift;
302 dll_dqs_delay_X_start_window = dll_dqs_delay_X_sw_val;
303 dll_dqs_delay_X_end_window = dll_dqs_delay_X;
308 * De-assert 'start' parameter.
310 mtdcr(ddrcfga, DDR0_02);
311 val = (mfdcr(ddrcfgd) & ~DDR0_02_START_MASK) | DDR0_02_START_OFF;
314 } /* for (dll_dqs_delay_X=0; dll_dqs_delay_X<128; dll_dqs_delay_X++) */
316 } /* for (wr_dqs_shift=0; wr_dqs_shift<96; wr_dqs_shift++) */
319 * Largest passing window is now detected.
322 /* Compute dll_dqs_delay_X value */
323 dll_dqs_delay_X = (dll_dqs_delay_X_end_window + dll_dqs_delay_X_start_window) / 2;
324 wr_dqs_shift = wr_dqs_shift_with_max_passing_cases;
326 debug("DQS calibration - Window detected:\n");
327 debug("max_passing_cases = %d\n", max_passing_cases);
328 debug("wr_dqs_shift = %d\n", wr_dqs_shift);
329 debug("dll_dqs_delay_X = %d\n", dll_dqs_delay_X);
330 debug("dll_dqs_delay_X window = %d - %d\n",
331 dll_dqs_delay_X_start_window, dll_dqs_delay_X_end_window);
334 * De-assert 'start' parameter.
336 mtdcr(ddrcfga, DDR0_02);
337 val = (mfdcr(ddrcfgd) & ~DDR0_02_START_MASK) | DDR0_02_START_OFF;
343 mtdcr(ddrcfga, DDR0_09);
344 val = (mfdcr(ddrcfgd) & ~DDR0_09_WR_DQS_SHIFT_MASK)
345 | DDR0_09_WR_DQS_SHIFT_ENCODE(wr_dqs_shift);
347 debug("DDR0_09=0x%08lx\n", val);
350 * Set 'dqs_out_shift' = wr_dqs_shift + 32
352 dqs_out_shift = wr_dqs_shift + 32;
353 mtdcr(ddrcfga, DDR0_22);
354 val = (mfdcr(ddrcfgd) & ~DDR0_22_DQS_OUT_SHIFT_MASK)
355 | DDR0_22_DQS_OUT_SHIFT_ENCODE(dqs_out_shift);
357 debug("DDR0_22=0x%08lx\n", val);
360 * Set 'dll_dqs_delay_X'.
362 /* dll_dqs_delay_0 */
363 mtdcr(ddrcfga, DDR0_17);
364 val = (mfdcr(ddrcfgd) & ~DDR0_17_DLL_DQS_DELAY_0_MASK)
365 | DDR0_17_DLL_DQS_DELAY_0_ENCODE(dll_dqs_delay_X);
367 debug("DDR0_17=0x%08lx\n", val);
369 /* dll_dqs_delay_1 to dll_dqs_delay_4 */
370 mtdcr(ddrcfga, DDR0_18);
371 val = (mfdcr(ddrcfgd) & ~DDR0_18_DLL_DQS_DELAY_X_MASK)
372 | DDR0_18_DLL_DQS_DELAY_4_ENCODE(dll_dqs_delay_X)
373 | DDR0_18_DLL_DQS_DELAY_3_ENCODE(dll_dqs_delay_X)
374 | DDR0_18_DLL_DQS_DELAY_2_ENCODE(dll_dqs_delay_X)
375 | DDR0_18_DLL_DQS_DELAY_1_ENCODE(dll_dqs_delay_X);
377 debug("DDR0_18=0x%08lx\n", val);
379 /* dll_dqs_delay_5 to dll_dqs_delay_8 */
380 mtdcr(ddrcfga, DDR0_19);
381 val = (mfdcr(ddrcfgd) & ~DDR0_19_DLL_DQS_DELAY_X_MASK)
382 | DDR0_19_DLL_DQS_DELAY_8_ENCODE(dll_dqs_delay_X)
383 | DDR0_19_DLL_DQS_DELAY_7_ENCODE(dll_dqs_delay_X)
384 | DDR0_19_DLL_DQS_DELAY_6_ENCODE(dll_dqs_delay_X)
385 | DDR0_19_DLL_DQS_DELAY_5_ENCODE(dll_dqs_delay_X);
387 debug("DDR0_19=0x%08lx\n", val);
390 * Assert 'start' parameter.
392 mtdcr(ddrcfga, DDR0_02);
393 val = (mfdcr(ddrcfgd) & ~DDR0_02_START_MASK) | DDR0_02_START_ON;
400 * Wait for the DCC master delay line to finish calibration
402 if (wait_for_dlllock() != 0) {
403 printf("dlllock did not occur !!!\n");
409 if (wait_for_dram_init_complete() != 0) {
410 printf("dram init complete did not occur !!!\n");
413 udelay(100); /* wait 100us to ensure init is really completed !!! */
415 #endif /* CONFIG_DDR_DATA_EYE */
417 #ifdef CONFIG_DDR_ECC
418 static void wait_ddr_idle(void)
421 * Controller idle status cannot be determined for Denali
422 * DDR2 code. Just return here.
426 static void blank_string(int size)
430 for (i=0; i<size; i++)
432 for (i=0; i<size; i++)
434 for (i=0; i<size; i++)
438 static void program_ecc(u32 start_address,
440 u32 tlb_word2_i_value)
444 u32 address_increment;
446 char str[] = "ECC generation -";
447 char slash[] = "\\|/-\\|/-";
451 current_address = start_address;
457 if (tlb_word2_i_value == TLB_WORD2_I_ENABLE) {
458 /* ECC bit set method for non-cached memory */
459 address_increment = 4;
460 end_address = current_address + num_bytes;
464 while (current_address < end_address) {
465 *((u32 *)current_address) = 0x00000000;
466 current_address += address_increment;
468 if ((loop++ % (2 << 20)) == 0) {
470 putc(slash[loopi++ % 8]);
474 blank_string(strlen(str));
476 /* ECC bit set method for cached memory */
477 #if 1 /* test-only: will remove this define later, when ECC problems are solved! */
479 * Some boards (like lwmon5) need to preserve the memory
480 * content upon ECC generation (for the log-buffer).
481 * Therefore we don't fill the memory with a pattern or
482 * just zero it, but write the same values back that are
483 * already in the memory cells.
485 address_increment = CFG_CACHELINE_SIZE;
486 end_address = current_address + num_bytes;
488 current_address = start_address;
489 while (current_address < end_address) {
490 ppcDcbi(current_address);
491 ppcDcbf(current_address);
492 current_address += CFG_CACHELINE_SIZE;
495 dcbz_area(start_address, num_bytes);
504 /* Clear error status */
505 mfsdram(DDR0_00, val);
506 mtsdram(DDR0_00, val | DDR0_00_INT_ACK_ALL);
508 /* Set 'int_mask' parameter to functionnal value */
509 mfsdram(DDR0_01, val);
510 mtsdram(DDR0_01, ((val &~ DDR0_01_INT_MASK_MASK) | DDR0_01_INT_MASK_ALL_OFF));
518 static __inline__ u32 get_mcsr(void)
522 asm volatile("mfspr %0, 0x23c" : "=r" (val) :);
526 static __inline__ void set_mcsr(u32 val)
528 asm volatile("mtspr 0x23c, %0" : "=r" (val) :);
531 /*************************************************************************
533 * initdram -- 440EPx's DDR controller is a DENALI Core
535 ************************************************************************/
536 long int initdram (int board_type)
540 #if 0 /* test-only: will remove this define later, when ECC problems are solved! */
542 mtsdram(DDR0_02, 0x00000000);
544 mtsdram(DDR0_00, 0x0000190A);
545 mtsdram(DDR0_01, 0x01000000);
546 mtsdram(DDR0_03, 0x02030603); /* A suitable burst length was taken. CAS is right for our board */
548 mtsdram(DDR0_04, 0x0A030300);
549 mtsdram(DDR0_05, 0x02020308);
550 mtsdram(DDR0_06, 0x0103C812);
551 mtsdram(DDR0_07, 0x00090100);
552 mtsdram(DDR0_08, 0x02c80001);
553 mtsdram(DDR0_09, 0x00011D5F);
554 mtsdram(DDR0_10, 0x00000300);
555 mtsdram(DDR0_11, 0x000CC800);
556 mtsdram(DDR0_12, 0x00000003);
557 mtsdram(DDR0_14, 0x00000000);
558 mtsdram(DDR0_17, 0x1e000000);
559 mtsdram(DDR0_18, 0x1e1e1e1e);
560 mtsdram(DDR0_19, 0x1e1e1e1e);
561 mtsdram(DDR0_20, 0x0B0B0B0B);
562 mtsdram(DDR0_21, 0x0B0B0B0B);
563 #ifdef CONFIG_DDR_ECC
564 mtsdram(DDR0_22, 0x00267F0B | DDR0_22_CTRL_RAW_ECC_ENABLE); /* enable ECC */
566 mtsdram(DDR0_22, 0x00267F0B);
569 mtsdram(DDR0_23, 0x01000000);
570 mtsdram(DDR0_24, 0x01010001);
572 mtsdram(DDR0_26, 0x2D93028A);
573 mtsdram(DDR0_27, 0x0784682B);
575 mtsdram(DDR0_28, 0x00000080);
576 mtsdram(DDR0_31, 0x00000000);
577 mtsdram(DDR0_42, 0x01000006);
579 mtsdram(DDR0_43, 0x030A0200);
580 mtsdram(DDR0_44, 0x00000003);
581 mtsdram(DDR0_02, 0x00000001); /* Activate the denali core */
584 mtsdram(DDR0_02, 0x00000000);
586 mtsdram(DDR0_00, 0x0000190A);
587 mtsdram(DDR0_01, 0x01000000);
588 mtsdram(DDR0_03, 0x02040803); /* A suitable burst length was taken. CAS is right for our board */
590 mtsdram(DDR0_04, 0x0B030300);
591 mtsdram(DDR0_05, 0x02020308);
592 mtsdram(DDR0_06, 0x0003C812);
593 mtsdram(DDR0_07, 0x00090100);
594 mtsdram(DDR0_08, 0x03c80001);
595 mtsdram(DDR0_09, 0x00011D5F);
596 mtsdram(DDR0_10, 0x00000300);
597 mtsdram(DDR0_11, 0x000CC800);
598 mtsdram(DDR0_12, 0x00000003);
599 mtsdram(DDR0_14, 0x00000000);
600 mtsdram(DDR0_17, 0x1e000000);
601 mtsdram(DDR0_18, 0x1e1e1e1e);
602 mtsdram(DDR0_19, 0x1e1e1e1e);
603 mtsdram(DDR0_20, 0x0B0B0B0B);
604 mtsdram(DDR0_21, 0x0B0B0B0B);
605 #ifdef CONFIG_DDR_ECC
606 mtsdram(DDR0_22, 0x00267F0B | DDR0_22_CTRL_RAW_ECC_ENABLE); /* enable ECC */
608 mtsdram(DDR0_22, 0x00267F0B);
611 mtsdram(DDR0_23, 0x01000000);
612 mtsdram(DDR0_24, 0x01010001);
614 mtsdram(DDR0_26, 0x2D93028A);
615 mtsdram(DDR0_27, 0x0784682B);
617 mtsdram(DDR0_28, 0x00000080);
618 mtsdram(DDR0_31, 0x00000000);
619 mtsdram(DDR0_42, 0x01000008);
621 mtsdram(DDR0_43, 0x050A0200);
622 mtsdram(DDR0_44, 0x00000005);
623 mtsdram(DDR0_02, 0x00000001); /* Activate the denali core */
629 * Program tlb entries for this size (dynamic)
631 program_tlb(0, 0, CFG_MBYTES_SDRAM << 20, MY_TLB_WORD2_I_ENABLE);
634 * Setup 2nd TLB with same physical address but different virtual address
635 * with cache enabled. This is done for fast ECC generation.
637 program_tlb(0, CFG_DDR_CACHED_ADDR, CFG_MBYTES_SDRAM << 20, 0);
639 #ifdef CONFIG_DDR_DATA_EYE
641 * Perform data eye search if requested.
643 denali_core_search_data_eye(CFG_DDR_CACHED_ADDR, CFG_MBYTES_SDRAM << 20);
646 * Clear possible errors resulting from data-eye-search.
647 * If not done, then we could get an interrupt later on when
648 * exceptions are enabled.
654 #ifdef CONFIG_DDR_ECC
656 * If ECC is enabled, initialize the parity bits.
658 program_ecc(CFG_DDR_CACHED_ADDR, CFG_MBYTES_SDRAM << 20, 0);
661 return (CFG_MBYTES_SDRAM << 20);