2 * arch/powerpc/cpu/ppc4xx/44x_spd_ddr.c
3 * This SPD DDR detection code supports IBM/AMCC PPC44x cpu with a
4 * DDR controller. Those are 440GP/GX/EP/GR.
7 * Bill Hunter, Wave 7 Optics, williamhunter@attbi.com
11 * Kenneth Johansson ,Ericsson AB.
12 * kenneth.johansson@etx.ericsson.se
14 * hacked up by bill hunter. fixed so we could run before
15 * serial_init and console_init. previous version avoided this by
16 * running out of cache memory during serial/console init, then running
20 * Jun Gu, Artesyn Technology, jung@artesyncp.com
21 * Support for AMCC 440 based on OpenBIOS draminit.c from IBM.
23 * (C) Copyright 2005-2007
24 * Stefan Roese, DENX Software Engineering, sr@denx.de.
26 * See file CREDITS for list of people who contributed to this
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License as
31 * published by the Free Software Foundation; either version 2 of
32 * the License, or (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
45 /* define DEBUG for debugging output (obviously ;-)) */
51 #include <asm/processor.h>
53 #include <asm/ppc4xx.h>
58 #if defined(CONFIG_SPD_EEPROM) && \
59 (defined(CONFIG_440GP) || defined(CONFIG_440GX) || \
60 defined(CONFIG_440EP) || defined(CONFIG_440GR))
65 #define ONE_BILLION 1000000000
68 * Board-specific Platform code can reimplement spd_ddr_init_hang () if needed
70 void __spd_ddr_init_hang (void)
74 void spd_ddr_init_hang (void) __attribute__((weak, alias("__spd_ddr_init_hang")));
76 /*-----------------------------------------------------------------------------+
78 +-----------------------------------------------------------------------------*/
79 #define DEFAULT_SPD_ADDR1 0x53
80 #define DEFAULT_SPD_ADDR2 0x52
81 #define MAXBANKS 4 /* at most 4 dimm banks */
82 #define MAX_SPD_BYTES 256
83 #define NUMHALFCYCLES 4
89 * This DDR2 setup code can dynamically setup the TLB entries for the DDR2 memory
90 * region. Right now the cache should still be disabled in U-Boot because of the
91 * EMAC driver, that need it's buffer descriptor to be located in non cached
94 * If at some time this restriction doesn't apply anymore, just define
95 * CONFIG_4xx_DCACHE in the board config file and this code should setup
96 * everything correctly.
98 #ifdef CONFIG_4xx_DCACHE
99 #define MY_TLB_WORD2_I_ENABLE 0 /* enable caching on SDRAM */
101 #define MY_TLB_WORD2_I_ENABLE TLB_WORD2_I_ENABLE /* disable caching on SDRAM */
104 /* bank_parms is used to sort the bank sizes by descending order */
107 unsigned long bank_size_bytes;
110 typedef struct bank_param BANKPARMS;
112 #ifdef CONFIG_SYS_SIMULATE_SPD_EEPROM
113 extern const unsigned char cfg_simulate_spd_eeprom[128];
116 static unsigned char spd_read(uchar chip, uint addr);
117 static void get_spd_info(unsigned long *dimm_populated,
118 unsigned char *iic0_dimm_addr,
119 unsigned long num_dimm_banks);
120 static void check_mem_type(unsigned long *dimm_populated,
121 unsigned char *iic0_dimm_addr,
122 unsigned long num_dimm_banks);
123 static void check_volt_type(unsigned long *dimm_populated,
124 unsigned char *iic0_dimm_addr,
125 unsigned long num_dimm_banks);
126 static void program_cfg0(unsigned long *dimm_populated,
127 unsigned char *iic0_dimm_addr,
128 unsigned long num_dimm_banks);
129 static void program_cfg1(unsigned long *dimm_populated,
130 unsigned char *iic0_dimm_addr,
131 unsigned long num_dimm_banks);
132 static void program_rtr(unsigned long *dimm_populated,
133 unsigned char *iic0_dimm_addr,
134 unsigned long num_dimm_banks);
135 static void program_tr0(unsigned long *dimm_populated,
136 unsigned char *iic0_dimm_addr,
137 unsigned long num_dimm_banks);
138 static void program_tr1(void);
140 static unsigned long program_bxcr(unsigned long *dimm_populated,
141 unsigned char *iic0_dimm_addr,
142 unsigned long num_dimm_banks);
145 * This function is reading data from the DIMM module EEPROM over the SPD bus
146 * and uses that to program the sdram controller.
148 * This works on boards that has the same schematics that the AMCC walnut has.
150 * BUG: Don't handle ECC memory
151 * BUG: A few values in the TR register is currently hardcoded
153 long int spd_sdram(void) {
154 unsigned char iic0_dimm_addr[] = SPD_EEPROM_ADDRESS;
155 unsigned long dimm_populated[sizeof(iic0_dimm_addr)];
156 unsigned long total_size;
159 unsigned long num_dimm_banks; /* on board dimm banks */
161 num_dimm_banks = sizeof(iic0_dimm_addr);
164 * Make sure I2C controller is initialized
167 i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
170 * Read the SPD information using I2C interface. Check to see if the
171 * DIMM slots are populated.
173 get_spd_info(dimm_populated, iic0_dimm_addr, num_dimm_banks);
176 * Check the memory type for the dimms plugged.
178 check_mem_type(dimm_populated, iic0_dimm_addr, num_dimm_banks);
181 * Check the voltage type for the dimms plugged.
183 check_volt_type(dimm_populated, iic0_dimm_addr, num_dimm_banks);
185 #if defined(CONFIG_440GX) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
187 * Soft-reset SDRAM controller.
189 mtsdr(SDR0_SRST, SDR0_SRST_DMC);
190 mtsdr(SDR0_SRST, 0x00000000);
194 * program 440GP SDRAM controller options (SDRAM0_CFG0)
196 program_cfg0(dimm_populated, iic0_dimm_addr, num_dimm_banks);
199 * program 440GP SDRAM controller options (SDRAM0_CFG1)
201 program_cfg1(dimm_populated, iic0_dimm_addr, num_dimm_banks);
204 * program SDRAM refresh register (SDRAM0_RTR)
206 program_rtr(dimm_populated, iic0_dimm_addr, num_dimm_banks);
209 * program SDRAM Timing Register 0 (SDRAM0_TR0)
211 program_tr0(dimm_populated, iic0_dimm_addr, num_dimm_banks);
214 * program the BxCR registers to find out total sdram installed
216 total_size = program_bxcr(dimm_populated, iic0_dimm_addr,
219 #ifdef CONFIG_PROG_SDRAM_TLB /* this define should eventually be removed */
220 /* and program tlb entries for this size (dynamic) */
221 program_tlb(0, 0, total_size, MY_TLB_WORD2_I_ENABLE);
225 * program SDRAM Clock Timing Register (SDRAM0_CLKTR)
227 mtsdram(SDRAM0_CLKTR, 0x40000000);
230 * delay to ensure 200 usec has elapsed
235 * enable the memory controller
237 mfsdram(SDRAM0_CFG0, cfg0);
238 mtsdram(SDRAM0_CFG0, cfg0 | SDRAM_CFG0_DCEN);
241 * wait for SDRAM_CFG0_DC_EN to complete
244 mfsdram(SDRAM0_MCSTS, mcsts);
245 if ((mcsts & SDRAM_MCSTS_MRSC) != 0)
250 * program SDRAM Timing Register 1, adding some delays
254 #ifdef CONFIG_DDR_ECC
256 * If ecc is enabled, initialize the parity bits.
258 ecc_init(CONFIG_SYS_SDRAM_BASE, total_size);
264 static unsigned char spd_read(uchar chip, uint addr)
266 unsigned char data[2];
268 #ifdef CONFIG_SYS_SIMULATE_SPD_EEPROM
269 if (chip == CONFIG_SYS_SIMULATE_SPD_EEPROM) {
271 * Onboard spd eeprom requested -> simulate values
273 return cfg_simulate_spd_eeprom[addr];
275 #endif /* CONFIG_SYS_SIMULATE_SPD_EEPROM */
277 if (i2c_probe(chip) == 0) {
278 if (i2c_read(chip, addr, 1, data, 1) == 0) {
286 static void get_spd_info(unsigned long *dimm_populated,
287 unsigned char *iic0_dimm_addr,
288 unsigned long num_dimm_banks)
290 unsigned long dimm_num;
291 unsigned long dimm_found;
292 unsigned char num_of_bytes;
293 unsigned char total_size;
296 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
300 num_of_bytes = spd_read(iic0_dimm_addr[dimm_num], 0);
301 total_size = spd_read(iic0_dimm_addr[dimm_num], 1);
303 if ((num_of_bytes != 0) && (total_size != 0)) {
304 dimm_populated[dimm_num] = true;
306 debug("DIMM slot %lu: populated\n", dimm_num);
308 dimm_populated[dimm_num] = false;
309 debug("DIMM slot %lu: Not populated\n", dimm_num);
313 if (dimm_found == false) {
314 printf("ERROR - No memory installed. Install a DDR-SDRAM DIMM.\n\n");
315 spd_ddr_init_hang ();
319 static void check_mem_type(unsigned long *dimm_populated,
320 unsigned char *iic0_dimm_addr,
321 unsigned long num_dimm_banks)
323 unsigned long dimm_num;
324 unsigned char dimm_type;
326 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
327 if (dimm_populated[dimm_num] == true) {
328 dimm_type = spd_read(iic0_dimm_addr[dimm_num], 2);
331 debug("DIMM slot %lu: DDR SDRAM detected\n", dimm_num);
334 printf("ERROR: Unsupported DIMM detected in slot %lu.\n",
336 printf("Only DDR SDRAM DIMMs are supported.\n");
337 printf("Replace the DIMM module with a supported DIMM.\n\n");
338 spd_ddr_init_hang ();
345 static void check_volt_type(unsigned long *dimm_populated,
346 unsigned char *iic0_dimm_addr,
347 unsigned long num_dimm_banks)
349 unsigned long dimm_num;
350 unsigned long voltage_type;
352 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
353 if (dimm_populated[dimm_num] == true) {
354 voltage_type = spd_read(iic0_dimm_addr[dimm_num], 8);
355 if (voltage_type != 0x04) {
356 printf("ERROR: DIMM %lu with unsupported voltage level.\n",
358 spd_ddr_init_hang ();
360 debug("DIMM %lu voltage level supported.\n", dimm_num);
367 static void program_cfg0(unsigned long *dimm_populated,
368 unsigned char *iic0_dimm_addr,
369 unsigned long num_dimm_banks)
371 unsigned long dimm_num;
373 unsigned long ecc_enabled;
375 unsigned char attributes;
376 unsigned long data_width;
379 * get Memory Controller Options 0 data
381 mfsdram(SDRAM0_CFG0, cfg0);
386 cfg0 &= ~(SDRAM_CFG0_DCEN | SDRAM_CFG0_MCHK_MASK |
387 SDRAM_CFG0_RDEN | SDRAM_CFG0_PMUD |
388 SDRAM_CFG0_DMWD_MASK |
389 SDRAM_CFG0_UIOS_MASK | SDRAM_CFG0_PDP);
393 * FIXME: assume the DDR SDRAMs in both banks are the same
396 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
397 if (dimm_populated[dimm_num] == true) {
398 ecc = spd_read(iic0_dimm_addr[dimm_num], 11);
404 * program Registered DIMM Enable
406 attributes = spd_read(iic0_dimm_addr[dimm_num], 21);
407 if ((attributes & 0x02) != 0x00) {
408 cfg0 |= SDRAM_CFG0_RDEN;
412 * program DDR SDRAM Data Width
415 (unsigned long)spd_read(iic0_dimm_addr[dimm_num],6) +
416 (((unsigned long)spd_read(iic0_dimm_addr[dimm_num],7)) << 8);
417 if (data_width == 64 || data_width == 72) {
418 cfg0 |= SDRAM_CFG0_DMWD_64;
419 } else if (data_width == 32 || data_width == 40) {
420 cfg0 |= SDRAM_CFG0_DMWD_32;
422 printf("WARNING: DIMM with datawidth of %lu bits.\n",
424 printf("Only DIMMs with 32 or 64 bit datawidths supported.\n");
425 spd_ddr_init_hang ();
432 * program Memory Data Error Checking
434 if (ecc_enabled == true) {
435 cfg0 |= SDRAM_CFG0_MCHK_GEN;
437 cfg0 |= SDRAM_CFG0_MCHK_NON;
441 * program Page Management Unit (0 == enabled)
443 cfg0 &= ~SDRAM_CFG0_PMUD;
446 * program Memory Controller Options 0
447 * Note: DCEN must be enabled after all DDR SDRAM controller
448 * configuration registers get initialized.
450 mtsdram(SDRAM0_CFG0, cfg0);
453 static void program_cfg1(unsigned long *dimm_populated,
454 unsigned char *iic0_dimm_addr,
455 unsigned long num_dimm_banks)
458 mfsdram(SDRAM0_CFG1, cfg1);
461 * Self-refresh exit, disable PM
463 cfg1 &= ~(SDRAM_CFG1_SRE | SDRAM_CFG1_PMEN);
466 * program Memory Controller Options 1
468 mtsdram(SDRAM0_CFG1, cfg1);
471 static void program_rtr(unsigned long *dimm_populated,
472 unsigned char *iic0_dimm_addr,
473 unsigned long num_dimm_banks)
475 unsigned long dimm_num;
476 unsigned long bus_period_x_10;
477 unsigned long refresh_rate = 0;
478 unsigned char refresh_rate_type;
479 unsigned long refresh_interval;
480 unsigned long sdram_rtr;
481 PPC4xx_SYS_INFO sys_info;
486 get_sys_info(&sys_info);
487 bus_period_x_10 = ONE_BILLION / (sys_info.freqPLB / 10);
489 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
490 if (dimm_populated[dimm_num] == true) {
491 refresh_rate_type = 0x7F & spd_read(iic0_dimm_addr[dimm_num], 12);
492 switch (refresh_rate_type) {
494 refresh_rate = 15625;
497 refresh_rate = 15625/4;
500 refresh_rate = 15625/2;
503 refresh_rate = 15626*2;
506 refresh_rate = 15625*4;
509 refresh_rate = 15625*8;
512 printf("ERROR: DIMM %lu, unsupported refresh rate/type.\n",
514 printf("Replace the DIMM module with a supported DIMM.\n");
522 refresh_interval = refresh_rate * 10 / bus_period_x_10;
523 sdram_rtr = (refresh_interval & 0x3ff8) << 16;
526 * program Refresh Timer Register (SDRAM0_RTR)
528 mtsdram(SDRAM0_RTR, sdram_rtr);
531 static void program_tr0(unsigned long *dimm_populated,
532 unsigned char *iic0_dimm_addr,
533 unsigned long num_dimm_banks)
535 unsigned long dimm_num;
538 unsigned char t_rp_ns;
539 unsigned char t_rcd_ns;
540 unsigned char t_ras_ns;
541 unsigned long t_rp_clk;
542 unsigned long t_ras_rcd_clk;
543 unsigned long t_rcd_clk;
544 unsigned long t_rfc_clk;
545 unsigned long plb_check;
546 unsigned char cas_bit;
547 unsigned long cas_index;
548 unsigned char cas_2_0_available;
549 unsigned char cas_2_5_available;
550 unsigned char cas_3_0_available;
551 unsigned long cycle_time_ns_x_10[3];
552 unsigned long tcyc_3_0_ns_x_10;
553 unsigned long tcyc_2_5_ns_x_10;
554 unsigned long tcyc_2_0_ns_x_10;
555 unsigned long tcyc_reg;
556 unsigned long bus_period_x_10;
557 PPC4xx_SYS_INFO sys_info;
558 unsigned long residue;
563 get_sys_info(&sys_info);
564 bus_period_x_10 = ONE_BILLION / (sys_info.freqPLB / 10);
567 * get SDRAM Timing Register 0 (SDRAM_TR0) and clear bits
569 mfsdram(SDRAM0_TR0, tr0);
570 tr0 &= ~(SDRAM_TR0_SDWR_MASK | SDRAM_TR0_SDWD_MASK |
571 SDRAM_TR0_SDCL_MASK | SDRAM_TR0_SDPA_MASK |
572 SDRAM_TR0_SDCP_MASK | SDRAM_TR0_SDLD_MASK |
573 SDRAM_TR0_SDRA_MASK | SDRAM_TR0_SDRD_MASK);
582 cas_2_0_available = true;
583 cas_2_5_available = true;
584 cas_3_0_available = true;
585 tcyc_2_0_ns_x_10 = 0;
586 tcyc_2_5_ns_x_10 = 0;
587 tcyc_3_0_ns_x_10 = 0;
589 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
590 if (dimm_populated[dimm_num] == true) {
591 wcsbc = spd_read(iic0_dimm_addr[dimm_num], 15);
592 t_rp_ns = spd_read(iic0_dimm_addr[dimm_num], 27) >> 2;
593 t_rcd_ns = spd_read(iic0_dimm_addr[dimm_num], 29) >> 2;
594 t_ras_ns = spd_read(iic0_dimm_addr[dimm_num], 30);
595 cas_bit = spd_read(iic0_dimm_addr[dimm_num], 18);
597 for (cas_index = 0; cas_index < 3; cas_index++) {
600 tcyc_reg = spd_read(iic0_dimm_addr[dimm_num], 9);
603 tcyc_reg = spd_read(iic0_dimm_addr[dimm_num], 23);
606 tcyc_reg = spd_read(iic0_dimm_addr[dimm_num], 25);
610 if ((tcyc_reg & 0x0F) >= 10) {
611 printf("ERROR: Tcyc incorrect for DIMM in slot %lu\n",
613 spd_ddr_init_hang ();
616 cycle_time_ns_x_10[cas_index] =
617 (((tcyc_reg & 0xF0) >> 4) * 10) + (tcyc_reg & 0x0F);
622 if ((cas_bit & 0x80) != 0) {
624 } else if ((cas_bit & 0x40) != 0) {
626 } else if ((cas_bit & 0x20) != 0) {
630 if (((cas_bit & 0x10) != 0) && (cas_index < 3)) {
631 tcyc_3_0_ns_x_10 = cycle_time_ns_x_10[cas_index];
634 if (cas_index != 0) {
637 cas_3_0_available = false;
640 if (((cas_bit & 0x08) != 0) || (cas_index < 3)) {
641 tcyc_2_5_ns_x_10 = cycle_time_ns_x_10[cas_index];
644 if (cas_index != 0) {
647 cas_2_5_available = false;
650 if (((cas_bit & 0x04) != 0) || (cas_index < 3)) {
651 tcyc_2_0_ns_x_10 = cycle_time_ns_x_10[cas_index];
654 if (cas_index != 0) {
657 cas_2_0_available = false;
665 * Program SD_WR and SD_WCSBC fields
667 tr0 |= SDRAM_TR0_SDWR_2_CLK; /* Write Recovery: 2 CLK */
670 tr0 |= SDRAM_TR0_SDWD_0_CLK;
673 tr0 |= SDRAM_TR0_SDWD_1_CLK;
678 * Program SD_CASL field
680 if ((cas_2_0_available == true) &&
681 (bus_period_x_10 >= tcyc_2_0_ns_x_10)) {
682 tr0 |= SDRAM_TR0_SDCL_2_0_CLK;
683 } else if ((cas_2_5_available == true) &&
684 (bus_period_x_10 >= tcyc_2_5_ns_x_10)) {
685 tr0 |= SDRAM_TR0_SDCL_2_5_CLK;
686 } else if ((cas_3_0_available == true) &&
687 (bus_period_x_10 >= tcyc_3_0_ns_x_10)) {
688 tr0 |= SDRAM_TR0_SDCL_3_0_CLK;
690 printf("ERROR: No supported CAS latency with the installed DIMMs.\n");
691 printf("Only CAS latencies of 2.0, 2.5, and 3.0 are supported.\n");
692 printf("Make sure the PLB speed is within the supported range.\n");
693 spd_ddr_init_hang ();
697 * Calculate Trp in clock cycles and round up if necessary
698 * Program SD_PTA field
700 t_rp_clk = sys_info.freqPLB * t_rp_ns / ONE_BILLION;
701 plb_check = ONE_BILLION * t_rp_clk / t_rp_ns;
702 if (sys_info.freqPLB != plb_check) {
705 switch ((unsigned long)t_rp_clk) {
709 tr0 |= SDRAM_TR0_SDPA_2_CLK;
712 tr0 |= SDRAM_TR0_SDPA_3_CLK;
715 tr0 |= SDRAM_TR0_SDPA_4_CLK;
720 * Program SD_CTP field
722 t_ras_rcd_clk = sys_info.freqPLB * (t_ras_ns - t_rcd_ns) / ONE_BILLION;
723 plb_check = ONE_BILLION * t_ras_rcd_clk / (t_ras_ns - t_rcd_ns);
724 if (sys_info.freqPLB != plb_check) {
727 switch (t_ras_rcd_clk) {
731 tr0 |= SDRAM_TR0_SDCP_2_CLK;
734 tr0 |= SDRAM_TR0_SDCP_3_CLK;
737 tr0 |= SDRAM_TR0_SDCP_4_CLK;
740 tr0 |= SDRAM_TR0_SDCP_5_CLK;
745 * Program SD_LDF field
747 tr0 |= SDRAM_TR0_SDLD_2_CLK;
750 * Program SD_RFTA field
751 * FIXME tRFC hardcoded as 75 nanoseconds
753 t_rfc_clk = sys_info.freqPLB / (ONE_BILLION / 75);
754 residue = sys_info.freqPLB % (ONE_BILLION / 75);
755 if (residue >= (ONE_BILLION / 150)) {
766 tr0 |= SDRAM_TR0_SDRA_6_CLK;
769 tr0 |= SDRAM_TR0_SDRA_7_CLK;
772 tr0 |= SDRAM_TR0_SDRA_8_CLK;
775 tr0 |= SDRAM_TR0_SDRA_9_CLK;
778 tr0 |= SDRAM_TR0_SDRA_10_CLK;
781 tr0 |= SDRAM_TR0_SDRA_11_CLK;
784 tr0 |= SDRAM_TR0_SDRA_12_CLK;
787 tr0 |= SDRAM_TR0_SDRA_13_CLK;
792 * Program SD_RCD field
794 t_rcd_clk = sys_info.freqPLB * t_rcd_ns / ONE_BILLION;
795 plb_check = ONE_BILLION * t_rcd_clk / t_rcd_ns;
796 if (sys_info.freqPLB != plb_check) {
803 tr0 |= SDRAM_TR0_SDRD_2_CLK;
806 tr0 |= SDRAM_TR0_SDRD_3_CLK;
809 tr0 |= SDRAM_TR0_SDRD_4_CLK;
813 debug("tr0: %lx\n", tr0);
814 mtsdram(SDRAM0_TR0, tr0);
817 static int short_mem_test(void)
820 unsigned long bxcr_num;
821 unsigned long *membase;
822 const unsigned long test[NUMMEMTESTS][NUMMEMWORDS] = {
823 {0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
824 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF},
825 {0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
826 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000},
827 {0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
828 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555},
829 {0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
830 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA},
831 {0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
832 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A},
833 {0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
834 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5},
835 {0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
836 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA},
837 {0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55,
838 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55}};
840 for (bxcr_num = 0; bxcr_num < MAXBXCR; bxcr_num++) {
841 mtdcr(SDRAM0_CFGADDR, SDRAM0_B0CR + (bxcr_num << 2));
842 if ((mfdcr(SDRAM0_CFGDATA) & SDRAM_BXCR_SDBE) == SDRAM_BXCR_SDBE) {
843 /* Bank is enabled */
844 membase = (unsigned long*)
845 (mfdcr(SDRAM0_CFGDATA) & SDRAM_BXCR_SDBA_MASK);
848 * Run the short memory test
850 for (i = 0; i < NUMMEMTESTS; i++) {
851 for (j = 0; j < NUMMEMWORDS; j++) {
852 /* printf("bank enabled base:%x\n", &membase[j]); */
853 membase[j] = test[i][j];
854 ppcDcbf((unsigned long)&(membase[j]));
857 for (j = 0; j < NUMMEMWORDS; j++) {
858 if (membase[j] != test[i][j]) {
859 ppcDcbf((unsigned long)&(membase[j]));
862 ppcDcbf((unsigned long)&(membase[j]));
870 * see if the rdclt value passed
880 static void program_tr1(void)
885 unsigned long ecc_temp;
886 unsigned long dlycal;
887 unsigned long dly_val;
889 unsigned long max_pass_length;
890 unsigned long current_pass_length;
891 unsigned long current_fail_length;
892 unsigned long current_start;
894 unsigned long rdclt_offset;
898 unsigned char window_found;
899 unsigned char fail_found;
900 unsigned char pass_found;
901 PPC4xx_SYS_INFO sys_info;
906 get_sys_info(&sys_info);
909 * get SDRAM Timing Register 0 (SDRAM_TR0) and clear bits
911 mfsdram(SDRAM0_TR1, tr1);
912 tr1 &= ~(SDRAM_TR1_RDSS_MASK | SDRAM_TR1_RDSL_MASK |
913 SDRAM_TR1_RDCD_MASK | SDRAM_TR1_RDCT_MASK);
915 mfsdram(SDRAM0_TR0, tr0);
916 if (((tr0 & SDRAM_TR0_SDCL_MASK) == SDRAM_TR0_SDCL_2_5_CLK) &&
917 (sys_info.freqPLB > 100000000)) {
918 tr1 |= SDRAM_TR1_RDSS_TR2;
919 tr1 |= SDRAM_TR1_RDSL_STAGE3;
920 tr1 |= SDRAM_TR1_RDCD_RCD_1_2;
922 tr1 |= SDRAM_TR1_RDSS_TR1;
923 tr1 |= SDRAM_TR1_RDSL_STAGE2;
924 tr1 |= SDRAM_TR1_RDCD_RCD_0_0;
928 * save CFG0 ECC setting to a temporary variable and turn ECC off
930 mfsdram(SDRAM0_CFG0, cfg0);
931 ecc_temp = cfg0 & SDRAM_CFG0_MCHK_MASK;
932 mtsdram(SDRAM0_CFG0, (cfg0 & ~SDRAM_CFG0_MCHK_MASK) | SDRAM_CFG0_MCHK_NON);
935 * get the delay line calibration register value
937 mfsdram(SDRAM0_DLYCAL, dlycal);
938 dly_val = SDRAM_DLYCAL_DLCV_DECODE(dlycal) << 2;
943 current_pass_length = 0;
944 current_fail_length = 0;
947 window_found = false;
950 debug("Starting memory test ");
952 for (k = 0; k < NUMHALFCYCLES; k++) {
953 for (rdclt = 0; rdclt < dly_val; rdclt++) {
955 * Set the timing reg for the test.
957 mtsdram(SDRAM0_TR1, (tr1 | SDRAM_TR1_RDCT_ENCODE(rdclt)));
959 if (short_mem_test()) {
960 if (fail_found == true) {
962 if (current_pass_length == 0) {
963 current_start = rdclt_offset + rdclt;
966 current_fail_length = 0;
967 current_pass_length++;
969 if (current_pass_length > max_pass_length) {
970 max_pass_length = current_pass_length;
971 max_start = current_start;
972 max_end = rdclt_offset + rdclt;
976 current_pass_length = 0;
977 current_fail_length++;
979 if (current_fail_length >= (dly_val>>2)) {
980 if (fail_found == false) {
982 } else if (pass_found == true) {
991 if (window_found == true)
994 tr1 = tr1 ^ SDRAM_TR1_RDCD_MASK;
995 rdclt_offset += dly_val;
1000 * make sure we find the window
1002 if (window_found == false) {
1003 printf("ERROR: Cannot determine a common read delay.\n");
1004 spd_ddr_init_hang ();
1008 * restore the orignal ECC setting
1010 mtsdram(SDRAM0_CFG0, (cfg0 & ~SDRAM_CFG0_MCHK_MASK) | ecc_temp);
1013 * set the SDRAM TR1 RDCD value
1015 tr1 &= ~SDRAM_TR1_RDCD_MASK;
1016 if ((tr0 & SDRAM_TR0_SDCL_MASK) == SDRAM_TR0_SDCL_2_5_CLK) {
1017 tr1 |= SDRAM_TR1_RDCD_RCD_1_2;
1019 tr1 |= SDRAM_TR1_RDCD_RCD_0_0;
1023 * set the SDRAM TR1 RDCLT value
1025 tr1 &= ~SDRAM_TR1_RDCT_MASK;
1026 while (max_end >= (dly_val << 1)) {
1027 max_end -= (dly_val << 1);
1028 max_start -= (dly_val << 1);
1031 rdclt_average = ((max_start + max_end) >> 1);
1033 if (rdclt_average < 0) {
1037 if (rdclt_average >= dly_val) {
1038 rdclt_average -= dly_val;
1039 tr1 = tr1 ^ SDRAM_TR1_RDCD_MASK;
1041 tr1 |= SDRAM_TR1_RDCT_ENCODE(rdclt_average);
1043 debug("tr1: %lx\n", tr1);
1046 * program SDRAM Timing Register 1 TR1
1048 mtsdram(SDRAM0_TR1, tr1);
1051 static unsigned long program_bxcr(unsigned long *dimm_populated,
1052 unsigned char *iic0_dimm_addr,
1053 unsigned long num_dimm_banks)
1055 unsigned long dimm_num;
1056 unsigned long bank_base_addr;
1061 unsigned char num_row_addr;
1062 unsigned char num_col_addr;
1063 unsigned char num_banks;
1064 unsigned char bank_size_id;
1065 unsigned long ctrl_bank_num[MAXBANKS];
1066 unsigned long bx_cr_num;
1067 unsigned long largest_size_index;
1068 unsigned long largest_size;
1069 unsigned long current_size_index;
1070 BANKPARMS bank_parms[MAXBXCR];
1071 unsigned long sorted_bank_num[MAXBXCR]; /* DDR Controller bank number table (sorted by size) */
1072 unsigned long sorted_bank_size[MAXBXCR]; /* DDR Controller bank size table (sorted by size)*/
1075 * Set the BxCR regs. First, wipe out the bank config registers.
1077 for (bx_cr_num = 0; bx_cr_num < MAXBXCR; bx_cr_num++) {
1078 mtdcr(SDRAM0_CFGADDR, SDRAM0_B0CR + (bx_cr_num << 2));
1079 mtdcr(SDRAM0_CFGDATA, 0x00000000);
1080 bank_parms[bx_cr_num].bank_size_bytes = 0;
1083 #ifdef CONFIG_BAMBOO
1085 * This next section is hardware dependent and must be programmed
1086 * to match the hardware. For bamboo, the following holds...
1087 * 1. SDRAM0_B0CR: Bank 0 of dimm 0 ctrl_bank_num : 0 (soldered onboard)
1088 * 2. SDRAM0_B1CR: Bank 0 of dimm 1 ctrl_bank_num : 1
1089 * 3. SDRAM0_B2CR: Bank 1 of dimm 1 ctrl_bank_num : 1
1090 * 4. SDRAM0_B3CR: Bank 0 of dimm 2 ctrl_bank_num : 3
1091 * ctrl_bank_num corresponds to the first usable DDR controller bank number by DIMM
1093 ctrl_bank_num[0] = 0;
1094 ctrl_bank_num[1] = 1;
1095 ctrl_bank_num[2] = 3;
1098 * Ocotea, Ebony and the other IBM/AMCC eval boards have
1099 * 2 DIMM slots with each max 2 banks
1101 ctrl_bank_num[0] = 0;
1102 ctrl_bank_num[1] = 2;
1106 * reset the bank_base address
1108 bank_base_addr = CONFIG_SYS_SDRAM_BASE;
1110 for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
1111 if (dimm_populated[dimm_num] == true) {
1112 num_row_addr = spd_read(iic0_dimm_addr[dimm_num], 3);
1113 num_col_addr = spd_read(iic0_dimm_addr[dimm_num], 4);
1114 num_banks = spd_read(iic0_dimm_addr[dimm_num], 5);
1115 bank_size_id = spd_read(iic0_dimm_addr[dimm_num], 31);
1116 debug("DIMM%ld: row=%d col=%d banks=%d\n", dimm_num,
1117 num_row_addr, num_col_addr, num_banks);
1120 * Set the SDRAM0_BxCR regs
1123 switch (bank_size_id) {
1125 cr |= SDRAM_BXCR_SDSZ_8;
1128 cr |= SDRAM_BXCR_SDSZ_16;
1131 cr |= SDRAM_BXCR_SDSZ_32;
1134 cr |= SDRAM_BXCR_SDSZ_64;
1137 cr |= SDRAM_BXCR_SDSZ_128;
1140 cr |= SDRAM_BXCR_SDSZ_256;
1143 cr |= SDRAM_BXCR_SDSZ_512;
1146 printf("DDR-SDRAM: DIMM %lu BxCR configuration.\n",
1148 printf("ERROR: Unsupported value for the banksize: %d.\n",
1150 printf("Replace the DIMM module with a supported DIMM.\n\n");
1151 spd_ddr_init_hang ();
1154 switch (num_col_addr) {
1156 cr |= SDRAM_BXCR_SDAM_1;
1159 cr |= SDRAM_BXCR_SDAM_2;
1162 cr |= SDRAM_BXCR_SDAM_3;
1165 cr |= SDRAM_BXCR_SDAM_4;
1168 printf("DDR-SDRAM: DIMM %lu BxCR configuration.\n",
1170 printf("ERROR: Unsupported value for number of "
1171 "column addresses: %d.\n", num_col_addr);
1172 printf("Replace the DIMM module with a supported DIMM.\n\n");
1173 spd_ddr_init_hang ();
1179 cr |= SDRAM_BXCR_SDBE;
1181 for (i = 0; i < num_banks; i++) {
1182 bank_parms[ctrl_bank_num[dimm_num]+i].bank_size_bytes =
1183 (4 << 20) * bank_size_id;
1184 bank_parms[ctrl_bank_num[dimm_num]+i].cr = cr;
1185 debug("DIMM%ld-bank %ld (SDRAM0_B%ldCR): "
1186 "bank_size_bytes=%ld\n",
1188 ctrl_bank_num[dimm_num] + i,
1189 bank_parms[ctrl_bank_num[dimm_num] + i].bank_size_bytes);
1194 /* Initialize sort tables */
1195 for (i = 0; i < MAXBXCR; i++) {
1196 sorted_bank_num[i] = i;
1197 sorted_bank_size[i] = bank_parms[i].bank_size_bytes;
1200 for (i = 0; i < MAXBXCR-1; i++) {
1201 largest_size = sorted_bank_size[i];
1202 largest_size_index = 255;
1204 /* Find the largest remaining value */
1205 for (j = i + 1; j < MAXBXCR; j++) {
1206 if (sorted_bank_size[j] > largest_size) {
1207 /* Save largest remaining value and its index */
1208 largest_size = sorted_bank_size[j];
1209 largest_size_index = j;
1213 if (largest_size_index != 255) {
1214 /* Swap the current and largest values */
1215 current_size_index = sorted_bank_num[largest_size_index];
1216 sorted_bank_size[largest_size_index] = sorted_bank_size[i];
1217 sorted_bank_size[i] = largest_size;
1218 sorted_bank_num[largest_size_index] = sorted_bank_num[i];
1219 sorted_bank_num[i] = current_size_index;
1223 /* Set the SDRAM0_BxCR regs thanks to sort tables */
1224 for (bx_cr_num = 0, bank_base_addr = 0; bx_cr_num < MAXBXCR; bx_cr_num++) {
1225 if (bank_parms[sorted_bank_num[bx_cr_num]].bank_size_bytes) {
1226 mtdcr(SDRAM0_CFGADDR, SDRAM0_B0CR + (sorted_bank_num[bx_cr_num] << 2));
1227 temp = mfdcr(SDRAM0_CFGDATA) & ~(SDRAM_BXCR_SDBA_MASK | SDRAM_BXCR_SDSZ_MASK |
1228 SDRAM_BXCR_SDAM_MASK | SDRAM_BXCR_SDBE);
1229 temp = temp | (bank_base_addr & SDRAM_BXCR_SDBA_MASK) |
1230 bank_parms[sorted_bank_num[bx_cr_num]].cr;
1231 mtdcr(SDRAM0_CFGDATA, temp);
1232 bank_base_addr += bank_parms[sorted_bank_num[bx_cr_num]].bank_size_bytes;
1233 debug("SDRAM0_B%ldCR=0x%08lx\n",
1234 sorted_bank_num[bx_cr_num], temp);
1238 return(bank_base_addr);
1240 #endif /* CONFIG_SPD_EEPROM */