wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1 | /* |
Stefan Roese | 88fbf93 | 2010-04-15 16:07:28 +0200 | [diff] [blame] | 2 | * arch/powerpc/cpu/ppc4xx/44x_spd_ddr.c |
Stefan Roese | c229533 | 2007-02-20 10:35:42 +0100 | [diff] [blame] | 3 | * This SPD DDR detection code supports IBM/AMCC PPC44x cpu with a |
| 4 | * DDR controller. Those are 440GP/GX/EP/GR. |
| 5 | * |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 6 | * (C) Copyright 2001 |
| 7 | * Bill Hunter, Wave 7 Optics, williamhunter@attbi.com |
| 8 | * |
| 9 | * Based on code by: |
| 10 | * |
wdenk | b666c8f | 2003-03-06 00:58:30 +0000 | [diff] [blame] | 11 | * Kenneth Johansson ,Ericsson AB. |
| 12 | * kenneth.johansson@etx.ericsson.se |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 13 | * |
| 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 |
| 17 | * this code later. |
| 18 | * |
| 19 | * (C) Copyright 2002 |
| 20 | * Jun Gu, Artesyn Technology, jung@artesyncp.com |
Wolfgang Denk | 0ee7077 | 2005-09-23 11:05:55 +0200 | [diff] [blame] | 21 | * Support for AMCC 440 based on OpenBIOS draminit.c from IBM. |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 22 | * |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 23 | * (C) Copyright 2005-2007 |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 24 | * Stefan Roese, DENX Software Engineering, sr@denx.de. |
| 25 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame^] | 26 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 27 | */ |
| 28 | |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 29 | /* define DEBUG for debugging output (obviously ;-)) */ |
| 30 | #if 0 |
| 31 | #define DEBUG |
| 32 | #endif |
| 33 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 34 | #include <common.h> |
| 35 | #include <asm/processor.h> |
| 36 | #include <i2c.h> |
Stefan Roese | 247e9d7 | 2010-09-09 19:18:00 +0200 | [diff] [blame] | 37 | #include <asm/ppc4xx.h> |
Stefan Roese | 5b89fc1 | 2007-03-07 16:39:36 +0100 | [diff] [blame] | 38 | #include <asm/mmu.h> |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 39 | |
Grant Erickson | b693341 | 2008-05-22 14:44:14 -0700 | [diff] [blame] | 40 | #include "ecc.h" |
| 41 | |
Stefan Roese | c229533 | 2007-02-20 10:35:42 +0100 | [diff] [blame] | 42 | #if defined(CONFIG_SPD_EEPROM) && \ |
| 43 | (defined(CONFIG_440GP) || defined(CONFIG_440GX) || \ |
| 44 | defined(CONFIG_440EP) || defined(CONFIG_440GR)) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * Set default values |
| 48 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 49 | #ifndef CONFIG_SYS_I2C_SPEED |
| 50 | #define CONFIG_SYS_I2C_SPEED 50000 |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 51 | #endif |
| 52 | |
Wolfgang Denk | f6a692b | 2005-12-04 00:40:34 +0100 | [diff] [blame] | 53 | #define ONE_BILLION 1000000000 |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 54 | |
Heiko Schocher | 68310b0 | 2007-06-25 19:11:37 +0200 | [diff] [blame] | 55 | /* |
| 56 | * Board-specific Platform code can reimplement spd_ddr_init_hang () if needed |
| 57 | */ |
| 58 | void __spd_ddr_init_hang (void) |
| 59 | { |
| 60 | hang (); |
| 61 | } |
| 62 | void spd_ddr_init_hang (void) __attribute__((weak, alias("__spd_ddr_init_hang"))); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 63 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 64 | /*-----------------------------------------------------------------------------+ |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 65 | | General Definition |
| 66 | +-----------------------------------------------------------------------------*/ |
Wolfgang Denk | f6a692b | 2005-12-04 00:40:34 +0100 | [diff] [blame] | 67 | #define DEFAULT_SPD_ADDR1 0x53 |
| 68 | #define DEFAULT_SPD_ADDR2 0x52 |
| 69 | #define MAXBANKS 4 /* at most 4 dimm banks */ |
| 70 | #define MAX_SPD_BYTES 256 |
| 71 | #define NUMHALFCYCLES 4 |
| 72 | #define NUMMEMTESTS 8 |
| 73 | #define NUMMEMWORDS 8 |
| 74 | #define MAXBXCR 4 |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 75 | |
Stefan Roese | 5b89fc1 | 2007-03-07 16:39:36 +0100 | [diff] [blame] | 76 | /* |
| 77 | * This DDR2 setup code can dynamically setup the TLB entries for the DDR2 memory |
| 78 | * region. Right now the cache should still be disabled in U-Boot because of the |
| 79 | * EMAC driver, that need it's buffer descriptor to be located in non cached |
| 80 | * memory. |
| 81 | * |
| 82 | * If at some time this restriction doesn't apply anymore, just define |
Stefan Roese | 68681607 | 2007-10-31 20:57:11 +0100 | [diff] [blame] | 83 | * CONFIG_4xx_DCACHE in the board config file and this code should setup |
Stefan Roese | 5b89fc1 | 2007-03-07 16:39:36 +0100 | [diff] [blame] | 84 | * everything correctly. |
| 85 | */ |
Stefan Roese | 68681607 | 2007-10-31 20:57:11 +0100 | [diff] [blame] | 86 | #ifdef CONFIG_4xx_DCACHE |
Stefan Roese | 5b89fc1 | 2007-03-07 16:39:36 +0100 | [diff] [blame] | 87 | #define MY_TLB_WORD2_I_ENABLE 0 /* enable caching on SDRAM */ |
| 88 | #else |
| 89 | #define MY_TLB_WORD2_I_ENABLE TLB_WORD2_I_ENABLE /* disable caching on SDRAM */ |
| 90 | #endif |
| 91 | |
Stefan Roese | c45d1e3 | 2005-11-15 16:04:58 +0100 | [diff] [blame] | 92 | /* bank_parms is used to sort the bank sizes by descending order */ |
| 93 | struct bank_param { |
| 94 | unsigned long cr; |
| 95 | unsigned long bank_size_bytes; |
| 96 | }; |
| 97 | |
| 98 | typedef struct bank_param BANKPARMS; |
| 99 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 100 | #ifdef CONFIG_SYS_SIMULATE_SPD_EEPROM |
Eugene OBrien | c59d1a0 | 2007-07-31 10:24:56 +0200 | [diff] [blame] | 101 | extern const unsigned char cfg_simulate_spd_eeprom[128]; |
Stefan Roese | c45d1e3 | 2005-11-15 16:04:58 +0100 | [diff] [blame] | 102 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 103 | |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 104 | static unsigned char spd_read(uchar chip, uint addr); |
| 105 | static void get_spd_info(unsigned long *dimm_populated, |
| 106 | unsigned char *iic0_dimm_addr, |
| 107 | unsigned long num_dimm_banks); |
| 108 | static void check_mem_type(unsigned long *dimm_populated, |
| 109 | unsigned char *iic0_dimm_addr, |
| 110 | unsigned long num_dimm_banks); |
| 111 | static void check_volt_type(unsigned long *dimm_populated, |
| 112 | unsigned char *iic0_dimm_addr, |
| 113 | unsigned long num_dimm_banks); |
| 114 | static void program_cfg0(unsigned long *dimm_populated, |
| 115 | unsigned char *iic0_dimm_addr, |
| 116 | unsigned long num_dimm_banks); |
| 117 | static void program_cfg1(unsigned long *dimm_populated, |
| 118 | unsigned char *iic0_dimm_addr, |
| 119 | unsigned long num_dimm_banks); |
| 120 | static void program_rtr(unsigned long *dimm_populated, |
| 121 | unsigned char *iic0_dimm_addr, |
| 122 | unsigned long num_dimm_banks); |
| 123 | static void program_tr0(unsigned long *dimm_populated, |
| 124 | unsigned char *iic0_dimm_addr, |
| 125 | unsigned long num_dimm_banks); |
| 126 | static void program_tr1(void); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 127 | |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 128 | static unsigned long program_bxcr(unsigned long *dimm_populated, |
| 129 | unsigned char *iic0_dimm_addr, |
| 130 | unsigned long num_dimm_banks); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * This function is reading data from the DIMM module EEPROM over the SPD bus |
| 134 | * and uses that to program the sdram controller. |
| 135 | * |
Wolfgang Denk | 0ee7077 | 2005-09-23 11:05:55 +0200 | [diff] [blame] | 136 | * This works on boards that has the same schematics that the AMCC walnut has. |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 137 | * |
| 138 | * BUG: Don't handle ECC memory |
| 139 | * BUG: A few values in the TR register is currently hardcoded |
| 140 | */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 141 | long int spd_sdram(void) { |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 142 | unsigned char iic0_dimm_addr[] = SPD_EEPROM_ADDRESS; |
| 143 | unsigned long dimm_populated[sizeof(iic0_dimm_addr)]; |
| 144 | unsigned long total_size; |
| 145 | unsigned long cfg0; |
| 146 | unsigned long mcsts; |
Wolfgang Denk | f6a692b | 2005-12-04 00:40:34 +0100 | [diff] [blame] | 147 | unsigned long num_dimm_banks; /* on board dimm banks */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 148 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 149 | num_dimm_banks = sizeof(iic0_dimm_addr); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 150 | |
| 151 | /* |
| 152 | * Make sure I2C controller is initialized |
| 153 | * before continuing. |
| 154 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 155 | i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 156 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 157 | /* |
| 158 | * Read the SPD information using I2C interface. Check to see if the |
| 159 | * DIMM slots are populated. |
| 160 | */ |
| 161 | get_spd_info(dimm_populated, iic0_dimm_addr, num_dimm_banks); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 162 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 163 | /* |
| 164 | * Check the memory type for the dimms plugged. |
| 165 | */ |
| 166 | check_mem_type(dimm_populated, iic0_dimm_addr, num_dimm_banks); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 167 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 168 | /* |
| 169 | * Check the voltage type for the dimms plugged. |
| 170 | */ |
| 171 | check_volt_type(dimm_populated, iic0_dimm_addr, num_dimm_banks); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 172 | |
Stefan Roese | 68681607 | 2007-10-31 20:57:11 +0100 | [diff] [blame] | 173 | #if defined(CONFIG_440GX) || defined(CONFIG_440EP) || defined(CONFIG_440GR) |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 174 | /* |
| 175 | * Soft-reset SDRAM controller. |
| 176 | */ |
Stefan Roese | 918010a | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 177 | mtsdr(SDR0_SRST, SDR0_SRST_DMC); |
| 178 | mtsdr(SDR0_SRST, 0x00000000); |
wdenk | 6148e74 | 2005-04-03 20:55:38 +0000 | [diff] [blame] | 179 | #endif |
| 180 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 181 | /* |
| 182 | * program 440GP SDRAM controller options (SDRAM0_CFG0) |
| 183 | */ |
| 184 | program_cfg0(dimm_populated, iic0_dimm_addr, num_dimm_banks); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 185 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 186 | /* |
| 187 | * program 440GP SDRAM controller options (SDRAM0_CFG1) |
| 188 | */ |
| 189 | program_cfg1(dimm_populated, iic0_dimm_addr, num_dimm_banks); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 190 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 191 | /* |
| 192 | * program SDRAM refresh register (SDRAM0_RTR) |
| 193 | */ |
| 194 | program_rtr(dimm_populated, iic0_dimm_addr, num_dimm_banks); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 195 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 196 | /* |
| 197 | * program SDRAM Timing Register 0 (SDRAM0_TR0) |
| 198 | */ |
| 199 | program_tr0(dimm_populated, iic0_dimm_addr, num_dimm_banks); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 200 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 201 | /* |
| 202 | * program the BxCR registers to find out total sdram installed |
| 203 | */ |
| 204 | total_size = program_bxcr(dimm_populated, iic0_dimm_addr, |
| 205 | num_dimm_banks); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 206 | |
Stefan Roese | 5b89fc1 | 2007-03-07 16:39:36 +0100 | [diff] [blame] | 207 | #ifdef CONFIG_PROG_SDRAM_TLB /* this define should eventually be removed */ |
| 208 | /* and program tlb entries for this size (dynamic) */ |
Stefan Roese | fb33c76 | 2007-06-14 11:14:32 +0200 | [diff] [blame] | 209 | program_tlb(0, 0, total_size, MY_TLB_WORD2_I_ENABLE); |
Stefan Roese | 5b89fc1 | 2007-03-07 16:39:36 +0100 | [diff] [blame] | 210 | #endif |
| 211 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 212 | /* |
| 213 | * program SDRAM Clock Timing Register (SDRAM0_CLKTR) |
| 214 | */ |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 215 | mtsdram(SDRAM0_CLKTR, 0x40000000); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 216 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 217 | /* |
| 218 | * delay to ensure 200 usec has elapsed |
| 219 | */ |
| 220 | udelay(400); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 221 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 222 | /* |
| 223 | * enable the memory controller |
| 224 | */ |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 225 | mfsdram(SDRAM0_CFG0, cfg0); |
| 226 | mtsdram(SDRAM0_CFG0, cfg0 | SDRAM_CFG0_DCEN); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 227 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 228 | /* |
| 229 | * wait for SDRAM_CFG0_DC_EN to complete |
| 230 | */ |
| 231 | while (1) { |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 232 | mfsdram(SDRAM0_MCSTS, mcsts); |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 233 | if ((mcsts & SDRAM_MCSTS_MRSC) != 0) |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 234 | break; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 235 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 236 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 237 | /* |
| 238 | * program SDRAM Timing Register 1, adding some delays |
| 239 | */ |
| 240 | program_tr1(); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 241 | |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 242 | #ifdef CONFIG_DDR_ECC |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 243 | /* |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 244 | * If ecc is enabled, initialize the parity bits. |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 245 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 246 | ecc_init(CONFIG_SYS_SDRAM_BASE, total_size); |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 247 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 248 | |
| 249 | return total_size; |
| 250 | } |
| 251 | |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 252 | static unsigned char spd_read(uchar chip, uint addr) |
Stefan Roese | c45d1e3 | 2005-11-15 16:04:58 +0100 | [diff] [blame] | 253 | { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 254 | unsigned char data[2]; |
| 255 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 256 | #ifdef CONFIG_SYS_SIMULATE_SPD_EEPROM |
| 257 | if (chip == CONFIG_SYS_SIMULATE_SPD_EEPROM) { |
Stefan Roese | c45d1e3 | 2005-11-15 16:04:58 +0100 | [diff] [blame] | 258 | /* |
| 259 | * Onboard spd eeprom requested -> simulate values |
| 260 | */ |
| 261 | return cfg_simulate_spd_eeprom[addr]; |
| 262 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 263 | #endif /* CONFIG_SYS_SIMULATE_SPD_EEPROM */ |
Stefan Roese | c45d1e3 | 2005-11-15 16:04:58 +0100 | [diff] [blame] | 264 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 265 | if (i2c_probe(chip) == 0) { |
| 266 | if (i2c_read(chip, addr, 1, data, 1) == 0) { |
| 267 | return data[0]; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | return 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 274 | static void get_spd_info(unsigned long *dimm_populated, |
| 275 | unsigned char *iic0_dimm_addr, |
| 276 | unsigned long num_dimm_banks) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 277 | { |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 278 | unsigned long dimm_num; |
| 279 | unsigned long dimm_found; |
| 280 | unsigned char num_of_bytes; |
| 281 | unsigned char total_size; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 282 | |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 283 | dimm_found = false; |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 284 | for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) { |
| 285 | num_of_bytes = 0; |
| 286 | total_size = 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 287 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 288 | num_of_bytes = spd_read(iic0_dimm_addr[dimm_num], 0); |
| 289 | total_size = spd_read(iic0_dimm_addr[dimm_num], 1); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 290 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 291 | if ((num_of_bytes != 0) && (total_size != 0)) { |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 292 | dimm_populated[dimm_num] = true; |
| 293 | dimm_found = true; |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 294 | debug("DIMM slot %lu: populated\n", dimm_num); |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 295 | } else { |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 296 | dimm_populated[dimm_num] = false; |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 297 | debug("DIMM slot %lu: Not populated\n", dimm_num); |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 298 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 299 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 300 | |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 301 | if (dimm_found == false) { |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 302 | printf("ERROR - No memory installed. Install a DDR-SDRAM DIMM.\n\n"); |
Heiko Schocher | 68310b0 | 2007-06-25 19:11:37 +0200 | [diff] [blame] | 303 | spd_ddr_init_hang (); |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 304 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 307 | static void check_mem_type(unsigned long *dimm_populated, |
| 308 | unsigned char *iic0_dimm_addr, |
| 309 | unsigned long num_dimm_banks) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 310 | { |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 311 | unsigned long dimm_num; |
| 312 | unsigned char dimm_type; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 313 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 314 | for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) { |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 315 | if (dimm_populated[dimm_num] == true) { |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 316 | dimm_type = spd_read(iic0_dimm_addr[dimm_num], 2); |
| 317 | switch (dimm_type) { |
| 318 | case 7: |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 319 | debug("DIMM slot %lu: DDR SDRAM detected\n", dimm_num); |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 320 | break; |
| 321 | default: |
| 322 | printf("ERROR: Unsupported DIMM detected in slot %lu.\n", |
| 323 | dimm_num); |
| 324 | printf("Only DDR SDRAM DIMMs are supported.\n"); |
| 325 | printf("Replace the DIMM module with a supported DIMM.\n\n"); |
Heiko Schocher | 68310b0 | 2007-06-25 19:11:37 +0200 | [diff] [blame] | 326 | spd_ddr_init_hang (); |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 327 | break; |
| 328 | } |
| 329 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 330 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 333 | static void check_volt_type(unsigned long *dimm_populated, |
| 334 | unsigned char *iic0_dimm_addr, |
| 335 | unsigned long num_dimm_banks) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 336 | { |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 337 | unsigned long dimm_num; |
| 338 | unsigned long voltage_type; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 339 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 340 | for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) { |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 341 | if (dimm_populated[dimm_num] == true) { |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 342 | voltage_type = spd_read(iic0_dimm_addr[dimm_num], 8); |
| 343 | if (voltage_type != 0x04) { |
| 344 | printf("ERROR: DIMM %lu with unsupported voltage level.\n", |
| 345 | dimm_num); |
Heiko Schocher | 68310b0 | 2007-06-25 19:11:37 +0200 | [diff] [blame] | 346 | spd_ddr_init_hang (); |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 347 | } else { |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 348 | debug("DIMM %lu voltage level supported.\n", dimm_num); |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 349 | } |
| 350 | break; |
| 351 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 352 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 355 | static void program_cfg0(unsigned long *dimm_populated, |
| 356 | unsigned char *iic0_dimm_addr, |
| 357 | unsigned long num_dimm_banks) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 358 | { |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 359 | unsigned long dimm_num; |
| 360 | unsigned long cfg0; |
| 361 | unsigned long ecc_enabled; |
| 362 | unsigned char ecc; |
| 363 | unsigned char attributes; |
| 364 | unsigned long data_width; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 365 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 366 | /* |
| 367 | * get Memory Controller Options 0 data |
| 368 | */ |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 369 | mfsdram(SDRAM0_CFG0, cfg0); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 370 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 371 | /* |
| 372 | * clear bits |
| 373 | */ |
| 374 | cfg0 &= ~(SDRAM_CFG0_DCEN | SDRAM_CFG0_MCHK_MASK | |
| 375 | SDRAM_CFG0_RDEN | SDRAM_CFG0_PMUD | |
| 376 | SDRAM_CFG0_DMWD_MASK | |
| 377 | SDRAM_CFG0_UIOS_MASK | SDRAM_CFG0_PDP); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 378 | |
| 379 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 380 | /* |
| 381 | * FIXME: assume the DDR SDRAMs in both banks are the same |
| 382 | */ |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 383 | ecc_enabled = true; |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 384 | for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) { |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 385 | if (dimm_populated[dimm_num] == true) { |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 386 | ecc = spd_read(iic0_dimm_addr[dimm_num], 11); |
| 387 | if (ecc != 0x02) { |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 388 | ecc_enabled = false; |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 389 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 390 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 391 | /* |
| 392 | * program Registered DIMM Enable |
| 393 | */ |
| 394 | attributes = spd_read(iic0_dimm_addr[dimm_num], 21); |
| 395 | if ((attributes & 0x02) != 0x00) { |
| 396 | cfg0 |= SDRAM_CFG0_RDEN; |
| 397 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 398 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 399 | /* |
| 400 | * program DDR SDRAM Data Width |
| 401 | */ |
| 402 | data_width = |
| 403 | (unsigned long)spd_read(iic0_dimm_addr[dimm_num],6) + |
| 404 | (((unsigned long)spd_read(iic0_dimm_addr[dimm_num],7)) << 8); |
| 405 | if (data_width == 64 || data_width == 72) { |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 406 | cfg0 |= SDRAM_CFG0_DMWD_64; |
| 407 | } else if (data_width == 32 || data_width == 40) { |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 408 | cfg0 |= SDRAM_CFG0_DMWD_32; |
| 409 | } else { |
| 410 | printf("WARNING: DIMM with datawidth of %lu bits.\n", |
| 411 | data_width); |
| 412 | printf("Only DIMMs with 32 or 64 bit datawidths supported.\n"); |
Heiko Schocher | 68310b0 | 2007-06-25 19:11:37 +0200 | [diff] [blame] | 413 | spd_ddr_init_hang (); |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 414 | } |
| 415 | break; |
| 416 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 417 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 418 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 419 | /* |
| 420 | * program Memory Data Error Checking |
| 421 | */ |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 422 | if (ecc_enabled == true) { |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 423 | cfg0 |= SDRAM_CFG0_MCHK_GEN; |
| 424 | } else { |
| 425 | cfg0 |= SDRAM_CFG0_MCHK_NON; |
| 426 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 427 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 428 | /* |
Stefan Roese | e335ee0 | 2006-07-28 18:34:58 +0200 | [diff] [blame] | 429 | * program Page Management Unit (0 == enabled) |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 430 | */ |
Stefan Roese | e335ee0 | 2006-07-28 18:34:58 +0200 | [diff] [blame] | 431 | cfg0 &= ~SDRAM_CFG0_PMUD; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 432 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 433 | /* |
| 434 | * program Memory Controller Options 0 |
| 435 | * Note: DCEN must be enabled after all DDR SDRAM controller |
| 436 | * configuration registers get initialized. |
| 437 | */ |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 438 | mtsdram(SDRAM0_CFG0, cfg0); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 441 | static void program_cfg1(unsigned long *dimm_populated, |
| 442 | unsigned char *iic0_dimm_addr, |
| 443 | unsigned long num_dimm_banks) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 444 | { |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 445 | unsigned long cfg1; |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 446 | mfsdram(SDRAM0_CFG1, cfg1); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 447 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 448 | /* |
| 449 | * Self-refresh exit, disable PM |
| 450 | */ |
| 451 | cfg1 &= ~(SDRAM_CFG1_SRE | SDRAM_CFG1_PMEN); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 452 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 453 | /* |
| 454 | * program Memory Controller Options 1 |
| 455 | */ |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 456 | mtsdram(SDRAM0_CFG1, cfg1); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 459 | static void program_rtr(unsigned long *dimm_populated, |
| 460 | unsigned char *iic0_dimm_addr, |
| 461 | unsigned long num_dimm_banks) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 462 | { |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 463 | unsigned long dimm_num; |
| 464 | unsigned long bus_period_x_10; |
| 465 | unsigned long refresh_rate = 0; |
| 466 | unsigned char refresh_rate_type; |
| 467 | unsigned long refresh_interval; |
| 468 | unsigned long sdram_rtr; |
Stefan Roese | edd73f2 | 2007-10-21 08:12:41 +0200 | [diff] [blame] | 469 | PPC4xx_SYS_INFO sys_info; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 470 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 471 | /* |
| 472 | * get the board info |
| 473 | */ |
| 474 | get_sys_info(&sys_info); |
| 475 | bus_period_x_10 = ONE_BILLION / (sys_info.freqPLB / 10); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 476 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 477 | for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) { |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 478 | if (dimm_populated[dimm_num] == true) { |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 479 | refresh_rate_type = 0x7F & spd_read(iic0_dimm_addr[dimm_num], 12); |
| 480 | switch (refresh_rate_type) { |
| 481 | case 0x00: |
| 482 | refresh_rate = 15625; |
| 483 | break; |
| 484 | case 0x01: |
| 485 | refresh_rate = 15625/4; |
| 486 | break; |
| 487 | case 0x02: |
| 488 | refresh_rate = 15625/2; |
| 489 | break; |
| 490 | case 0x03: |
| 491 | refresh_rate = 15626*2; |
| 492 | break; |
| 493 | case 0x04: |
| 494 | refresh_rate = 15625*4; |
| 495 | break; |
| 496 | case 0x05: |
| 497 | refresh_rate = 15625*8; |
| 498 | break; |
| 499 | default: |
| 500 | printf("ERROR: DIMM %lu, unsupported refresh rate/type.\n", |
| 501 | dimm_num); |
| 502 | printf("Replace the DIMM module with a supported DIMM.\n"); |
| 503 | break; |
| 504 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 505 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 506 | break; |
| 507 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 508 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 509 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 510 | refresh_interval = refresh_rate * 10 / bus_period_x_10; |
| 511 | sdram_rtr = (refresh_interval & 0x3ff8) << 16; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 512 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 513 | /* |
| 514 | * program Refresh Timer Register (SDRAM0_RTR) |
| 515 | */ |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 516 | mtsdram(SDRAM0_RTR, sdram_rtr); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 519 | static void program_tr0(unsigned long *dimm_populated, |
| 520 | unsigned char *iic0_dimm_addr, |
| 521 | unsigned long num_dimm_banks) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 522 | { |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 523 | unsigned long dimm_num; |
| 524 | unsigned long tr0; |
| 525 | unsigned char wcsbc; |
| 526 | unsigned char t_rp_ns; |
| 527 | unsigned char t_rcd_ns; |
| 528 | unsigned char t_ras_ns; |
| 529 | unsigned long t_rp_clk; |
| 530 | unsigned long t_ras_rcd_clk; |
| 531 | unsigned long t_rcd_clk; |
| 532 | unsigned long t_rfc_clk; |
| 533 | unsigned long plb_check; |
| 534 | unsigned char cas_bit; |
| 535 | unsigned long cas_index; |
| 536 | unsigned char cas_2_0_available; |
| 537 | unsigned char cas_2_5_available; |
| 538 | unsigned char cas_3_0_available; |
| 539 | unsigned long cycle_time_ns_x_10[3]; |
| 540 | unsigned long tcyc_3_0_ns_x_10; |
| 541 | unsigned long tcyc_2_5_ns_x_10; |
| 542 | unsigned long tcyc_2_0_ns_x_10; |
| 543 | unsigned long tcyc_reg; |
| 544 | unsigned long bus_period_x_10; |
Stefan Roese | edd73f2 | 2007-10-21 08:12:41 +0200 | [diff] [blame] | 545 | PPC4xx_SYS_INFO sys_info; |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 546 | unsigned long residue; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 547 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 548 | /* |
| 549 | * get the board info |
| 550 | */ |
| 551 | get_sys_info(&sys_info); |
| 552 | bus_period_x_10 = ONE_BILLION / (sys_info.freqPLB / 10); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 553 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 554 | /* |
| 555 | * get SDRAM Timing Register 0 (SDRAM_TR0) and clear bits |
| 556 | */ |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 557 | mfsdram(SDRAM0_TR0, tr0); |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 558 | tr0 &= ~(SDRAM_TR0_SDWR_MASK | SDRAM_TR0_SDWD_MASK | |
| 559 | SDRAM_TR0_SDCL_MASK | SDRAM_TR0_SDPA_MASK | |
| 560 | SDRAM_TR0_SDCP_MASK | SDRAM_TR0_SDLD_MASK | |
| 561 | SDRAM_TR0_SDRA_MASK | SDRAM_TR0_SDRD_MASK); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 562 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 563 | /* |
| 564 | * initialization |
| 565 | */ |
| 566 | wcsbc = 0; |
| 567 | t_rp_ns = 0; |
| 568 | t_rcd_ns = 0; |
| 569 | t_ras_ns = 0; |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 570 | cas_2_0_available = true; |
| 571 | cas_2_5_available = true; |
| 572 | cas_3_0_available = true; |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 573 | tcyc_2_0_ns_x_10 = 0; |
| 574 | tcyc_2_5_ns_x_10 = 0; |
| 575 | tcyc_3_0_ns_x_10 = 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 576 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 577 | for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) { |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 578 | if (dimm_populated[dimm_num] == true) { |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 579 | wcsbc = spd_read(iic0_dimm_addr[dimm_num], 15); |
Wolfgang Denk | f6a692b | 2005-12-04 00:40:34 +0100 | [diff] [blame] | 580 | t_rp_ns = spd_read(iic0_dimm_addr[dimm_num], 27) >> 2; |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 581 | t_rcd_ns = spd_read(iic0_dimm_addr[dimm_num], 29) >> 2; |
| 582 | t_ras_ns = spd_read(iic0_dimm_addr[dimm_num], 30); |
| 583 | cas_bit = spd_read(iic0_dimm_addr[dimm_num], 18); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 584 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 585 | for (cas_index = 0; cas_index < 3; cas_index++) { |
| 586 | switch (cas_index) { |
| 587 | case 0: |
| 588 | tcyc_reg = spd_read(iic0_dimm_addr[dimm_num], 9); |
| 589 | break; |
| 590 | case 1: |
| 591 | tcyc_reg = spd_read(iic0_dimm_addr[dimm_num], 23); |
| 592 | break; |
| 593 | default: |
| 594 | tcyc_reg = spd_read(iic0_dimm_addr[dimm_num], 25); |
| 595 | break; |
| 596 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 597 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 598 | if ((tcyc_reg & 0x0F) >= 10) { |
| 599 | printf("ERROR: Tcyc incorrect for DIMM in slot %lu\n", |
| 600 | dimm_num); |
Heiko Schocher | 68310b0 | 2007-06-25 19:11:37 +0200 | [diff] [blame] | 601 | spd_ddr_init_hang (); |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 602 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 603 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 604 | cycle_time_ns_x_10[cas_index] = |
| 605 | (((tcyc_reg & 0xF0) >> 4) * 10) + (tcyc_reg & 0x0F); |
| 606 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 607 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 608 | cas_index = 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 609 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 610 | if ((cas_bit & 0x80) != 0) { |
| 611 | cas_index += 3; |
| 612 | } else if ((cas_bit & 0x40) != 0) { |
| 613 | cas_index += 2; |
| 614 | } else if ((cas_bit & 0x20) != 0) { |
| 615 | cas_index += 1; |
| 616 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 617 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 618 | if (((cas_bit & 0x10) != 0) && (cas_index < 3)) { |
| 619 | tcyc_3_0_ns_x_10 = cycle_time_ns_x_10[cas_index]; |
| 620 | cas_index++; |
| 621 | } else { |
| 622 | if (cas_index != 0) { |
| 623 | cas_index++; |
| 624 | } |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 625 | cas_3_0_available = false; |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 626 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 627 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 628 | if (((cas_bit & 0x08) != 0) || (cas_index < 3)) { |
| 629 | tcyc_2_5_ns_x_10 = cycle_time_ns_x_10[cas_index]; |
| 630 | cas_index++; |
| 631 | } else { |
| 632 | if (cas_index != 0) { |
| 633 | cas_index++; |
| 634 | } |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 635 | cas_2_5_available = false; |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 636 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 637 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 638 | if (((cas_bit & 0x04) != 0) || (cas_index < 3)) { |
| 639 | tcyc_2_0_ns_x_10 = cycle_time_ns_x_10[cas_index]; |
| 640 | cas_index++; |
| 641 | } else { |
| 642 | if (cas_index != 0) { |
| 643 | cas_index++; |
| 644 | } |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 645 | cas_2_0_available = false; |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 646 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 647 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 648 | break; |
| 649 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 650 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 651 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 652 | /* |
| 653 | * Program SD_WR and SD_WCSBC fields |
| 654 | */ |
Wolfgang Denk | f6a692b | 2005-12-04 00:40:34 +0100 | [diff] [blame] | 655 | tr0 |= SDRAM_TR0_SDWR_2_CLK; /* Write Recovery: 2 CLK */ |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 656 | switch (wcsbc) { |
| 657 | case 0: |
| 658 | tr0 |= SDRAM_TR0_SDWD_0_CLK; |
| 659 | break; |
| 660 | default: |
| 661 | tr0 |= SDRAM_TR0_SDWD_1_CLK; |
| 662 | break; |
| 663 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 664 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 665 | /* |
| 666 | * Program SD_CASL field |
| 667 | */ |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 668 | if ((cas_2_0_available == true) && |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 669 | (bus_period_x_10 >= tcyc_2_0_ns_x_10)) { |
| 670 | tr0 |= SDRAM_TR0_SDCL_2_0_CLK; |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 671 | } else if ((cas_2_5_available == true) && |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 672 | (bus_period_x_10 >= tcyc_2_5_ns_x_10)) { |
| 673 | tr0 |= SDRAM_TR0_SDCL_2_5_CLK; |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 674 | } else if ((cas_3_0_available == true) && |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 675 | (bus_period_x_10 >= tcyc_3_0_ns_x_10)) { |
| 676 | tr0 |= SDRAM_TR0_SDCL_3_0_CLK; |
| 677 | } else { |
| 678 | printf("ERROR: No supported CAS latency with the installed DIMMs.\n"); |
| 679 | printf("Only CAS latencies of 2.0, 2.5, and 3.0 are supported.\n"); |
| 680 | printf("Make sure the PLB speed is within the supported range.\n"); |
Heiko Schocher | 68310b0 | 2007-06-25 19:11:37 +0200 | [diff] [blame] | 681 | spd_ddr_init_hang (); |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 682 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 683 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 684 | /* |
| 685 | * Calculate Trp in clock cycles and round up if necessary |
| 686 | * Program SD_PTA field |
| 687 | */ |
| 688 | t_rp_clk = sys_info.freqPLB * t_rp_ns / ONE_BILLION; |
| 689 | plb_check = ONE_BILLION * t_rp_clk / t_rp_ns; |
| 690 | if (sys_info.freqPLB != plb_check) { |
| 691 | t_rp_clk++; |
| 692 | } |
| 693 | switch ((unsigned long)t_rp_clk) { |
| 694 | case 0: |
| 695 | case 1: |
| 696 | case 2: |
| 697 | tr0 |= SDRAM_TR0_SDPA_2_CLK; |
| 698 | break; |
| 699 | case 3: |
| 700 | tr0 |= SDRAM_TR0_SDPA_3_CLK; |
| 701 | break; |
| 702 | default: |
| 703 | tr0 |= SDRAM_TR0_SDPA_4_CLK; |
| 704 | break; |
| 705 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 706 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 707 | /* |
| 708 | * Program SD_CTP field |
| 709 | */ |
| 710 | t_ras_rcd_clk = sys_info.freqPLB * (t_ras_ns - t_rcd_ns) / ONE_BILLION; |
| 711 | plb_check = ONE_BILLION * t_ras_rcd_clk / (t_ras_ns - t_rcd_ns); |
| 712 | if (sys_info.freqPLB != plb_check) { |
| 713 | t_ras_rcd_clk++; |
| 714 | } |
| 715 | switch (t_ras_rcd_clk) { |
| 716 | case 0: |
| 717 | case 1: |
| 718 | case 2: |
| 719 | tr0 |= SDRAM_TR0_SDCP_2_CLK; |
| 720 | break; |
| 721 | case 3: |
| 722 | tr0 |= SDRAM_TR0_SDCP_3_CLK; |
| 723 | break; |
| 724 | case 4: |
| 725 | tr0 |= SDRAM_TR0_SDCP_4_CLK; |
| 726 | break; |
| 727 | default: |
| 728 | tr0 |= SDRAM_TR0_SDCP_5_CLK; |
| 729 | break; |
| 730 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 731 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 732 | /* |
| 733 | * Program SD_LDF field |
| 734 | */ |
| 735 | tr0 |= SDRAM_TR0_SDLD_2_CLK; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 736 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 737 | /* |
| 738 | * Program SD_RFTA field |
| 739 | * FIXME tRFC hardcoded as 75 nanoseconds |
| 740 | */ |
| 741 | t_rfc_clk = sys_info.freqPLB / (ONE_BILLION / 75); |
| 742 | residue = sys_info.freqPLB % (ONE_BILLION / 75); |
| 743 | if (residue >= (ONE_BILLION / 150)) { |
| 744 | t_rfc_clk++; |
| 745 | } |
| 746 | switch (t_rfc_clk) { |
| 747 | case 0: |
| 748 | case 1: |
| 749 | case 2: |
| 750 | case 3: |
| 751 | case 4: |
| 752 | case 5: |
| 753 | case 6: |
| 754 | tr0 |= SDRAM_TR0_SDRA_6_CLK; |
| 755 | break; |
| 756 | case 7: |
| 757 | tr0 |= SDRAM_TR0_SDRA_7_CLK; |
| 758 | break; |
| 759 | case 8: |
| 760 | tr0 |= SDRAM_TR0_SDRA_8_CLK; |
| 761 | break; |
| 762 | case 9: |
| 763 | tr0 |= SDRAM_TR0_SDRA_9_CLK; |
| 764 | break; |
| 765 | case 10: |
| 766 | tr0 |= SDRAM_TR0_SDRA_10_CLK; |
| 767 | break; |
| 768 | case 11: |
| 769 | tr0 |= SDRAM_TR0_SDRA_11_CLK; |
| 770 | break; |
| 771 | case 12: |
| 772 | tr0 |= SDRAM_TR0_SDRA_12_CLK; |
| 773 | break; |
| 774 | default: |
| 775 | tr0 |= SDRAM_TR0_SDRA_13_CLK; |
| 776 | break; |
| 777 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 778 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 779 | /* |
| 780 | * Program SD_RCD field |
| 781 | */ |
| 782 | t_rcd_clk = sys_info.freqPLB * t_rcd_ns / ONE_BILLION; |
| 783 | plb_check = ONE_BILLION * t_rcd_clk / t_rcd_ns; |
| 784 | if (sys_info.freqPLB != plb_check) { |
| 785 | t_rcd_clk++; |
| 786 | } |
| 787 | switch (t_rcd_clk) { |
| 788 | case 0: |
| 789 | case 1: |
| 790 | case 2: |
| 791 | tr0 |= SDRAM_TR0_SDRD_2_CLK; |
| 792 | break; |
| 793 | case 3: |
| 794 | tr0 |= SDRAM_TR0_SDRD_3_CLK; |
| 795 | break; |
| 796 | default: |
| 797 | tr0 |= SDRAM_TR0_SDRD_4_CLK; |
| 798 | break; |
| 799 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 800 | |
Marek Vasut | 99b00d9 | 2011-10-21 14:17:12 +0000 | [diff] [blame] | 801 | debug("tr0: %lx\n", tr0); |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 802 | mtsdram(SDRAM0_TR0, tr0); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 805 | static int short_mem_test(void) |
| 806 | { |
| 807 | unsigned long i, j; |
| 808 | unsigned long bxcr_num; |
| 809 | unsigned long *membase; |
| 810 | const unsigned long test[NUMMEMTESTS][NUMMEMWORDS] = { |
| 811 | {0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF, |
| 812 | 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF}, |
| 813 | {0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000, |
| 814 | 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000}, |
| 815 | {0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555, |
| 816 | 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555}, |
| 817 | {0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA, |
| 818 | 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA}, |
| 819 | {0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A, |
| 820 | 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A}, |
| 821 | {0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5, |
| 822 | 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5}, |
| 823 | {0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA, |
| 824 | 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA}, |
| 825 | {0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55, |
| 826 | 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55}}; |
| 827 | |
| 828 | for (bxcr_num = 0; bxcr_num < MAXBXCR; bxcr_num++) { |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 829 | mtdcr(SDRAM0_CFGADDR, SDRAM0_B0CR + (bxcr_num << 2)); |
Stefan Roese | 918010a | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 830 | if ((mfdcr(SDRAM0_CFGDATA) & SDRAM_BXCR_SDBE) == SDRAM_BXCR_SDBE) { |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 831 | /* Bank is enabled */ |
| 832 | membase = (unsigned long*) |
Stefan Roese | 918010a | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 833 | (mfdcr(SDRAM0_CFGDATA) & SDRAM_BXCR_SDBA_MASK); |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 834 | |
| 835 | /* |
| 836 | * Run the short memory test |
| 837 | */ |
| 838 | for (i = 0; i < NUMMEMTESTS; i++) { |
| 839 | for (j = 0; j < NUMMEMWORDS; j++) { |
Wolfgang Denk | e877984 | 2007-07-12 01:45:34 +0200 | [diff] [blame] | 840 | /* printf("bank enabled base:%x\n", &membase[j]); */ |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 841 | membase[j] = test[i][j]; |
| 842 | ppcDcbf((unsigned long)&(membase[j])); |
| 843 | } |
| 844 | |
| 845 | for (j = 0; j < NUMMEMWORDS; j++) { |
| 846 | if (membase[j] != test[i][j]) { |
| 847 | ppcDcbf((unsigned long)&(membase[j])); |
| 848 | return 0; |
| 849 | } |
| 850 | ppcDcbf((unsigned long)&(membase[j])); |
| 851 | } |
| 852 | |
| 853 | if (j < NUMMEMWORDS) |
| 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | /* |
| 858 | * see if the rdclt value passed |
| 859 | */ |
| 860 | if (i < NUMMEMTESTS) |
| 861 | return 0; |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | return 1; |
| 866 | } |
| 867 | |
| 868 | static void program_tr1(void) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 869 | { |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 870 | unsigned long tr0; |
| 871 | unsigned long tr1; |
| 872 | unsigned long cfg0; |
| 873 | unsigned long ecc_temp; |
| 874 | unsigned long dlycal; |
| 875 | unsigned long dly_val; |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 876 | unsigned long k; |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 877 | unsigned long max_pass_length; |
| 878 | unsigned long current_pass_length; |
| 879 | unsigned long current_fail_length; |
| 880 | unsigned long current_start; |
| 881 | unsigned long rdclt; |
| 882 | unsigned long rdclt_offset; |
| 883 | long max_start; |
| 884 | long max_end; |
| 885 | long rdclt_average; |
| 886 | unsigned char window_found; |
| 887 | unsigned char fail_found; |
| 888 | unsigned char pass_found; |
Stefan Roese | edd73f2 | 2007-10-21 08:12:41 +0200 | [diff] [blame] | 889 | PPC4xx_SYS_INFO sys_info; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 890 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 891 | /* |
| 892 | * get the board info |
| 893 | */ |
| 894 | get_sys_info(&sys_info); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 895 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 896 | /* |
| 897 | * get SDRAM Timing Register 0 (SDRAM_TR0) and clear bits |
| 898 | */ |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 899 | mfsdram(SDRAM0_TR1, tr1); |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 900 | tr1 &= ~(SDRAM_TR1_RDSS_MASK | SDRAM_TR1_RDSL_MASK | |
| 901 | SDRAM_TR1_RDCD_MASK | SDRAM_TR1_RDCT_MASK); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 902 | |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 903 | mfsdram(SDRAM0_TR0, tr0); |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 904 | if (((tr0 & SDRAM_TR0_SDCL_MASK) == SDRAM_TR0_SDCL_2_5_CLK) && |
| 905 | (sys_info.freqPLB > 100000000)) { |
| 906 | tr1 |= SDRAM_TR1_RDSS_TR2; |
| 907 | tr1 |= SDRAM_TR1_RDSL_STAGE3; |
| 908 | tr1 |= SDRAM_TR1_RDCD_RCD_1_2; |
| 909 | } else { |
| 910 | tr1 |= SDRAM_TR1_RDSS_TR1; |
| 911 | tr1 |= SDRAM_TR1_RDSL_STAGE2; |
| 912 | tr1 |= SDRAM_TR1_RDCD_RCD_0_0; |
| 913 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 914 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 915 | /* |
| 916 | * save CFG0 ECC setting to a temporary variable and turn ECC off |
| 917 | */ |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 918 | mfsdram(SDRAM0_CFG0, cfg0); |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 919 | ecc_temp = cfg0 & SDRAM_CFG0_MCHK_MASK; |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 920 | mtsdram(SDRAM0_CFG0, (cfg0 & ~SDRAM_CFG0_MCHK_MASK) | SDRAM_CFG0_MCHK_NON); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 921 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 922 | /* |
| 923 | * get the delay line calibration register value |
| 924 | */ |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 925 | mfsdram(SDRAM0_DLYCAL, dlycal); |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 926 | dly_val = SDRAM_DLYCAL_DLCV_DECODE(dlycal) << 2; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 927 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 928 | max_pass_length = 0; |
| 929 | max_start = 0; |
| 930 | max_end = 0; |
| 931 | current_pass_length = 0; |
| 932 | current_fail_length = 0; |
| 933 | current_start = 0; |
| 934 | rdclt_offset = 0; |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 935 | window_found = false; |
| 936 | fail_found = false; |
| 937 | pass_found = false; |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 938 | debug("Starting memory test "); |
| 939 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 940 | for (k = 0; k < NUMHALFCYCLES; k++) { |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 941 | for (rdclt = 0; rdclt < dly_val; rdclt++) { |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 942 | /* |
| 943 | * Set the timing reg for the test. |
| 944 | */ |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 945 | mtsdram(SDRAM0_TR1, (tr1 | SDRAM_TR1_RDCT_ENCODE(rdclt))); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 946 | |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 947 | if (short_mem_test()) { |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 948 | if (fail_found == true) { |
| 949 | pass_found = true; |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 950 | if (current_pass_length == 0) { |
| 951 | current_start = rdclt_offset + rdclt; |
| 952 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 953 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 954 | current_fail_length = 0; |
| 955 | current_pass_length++; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 956 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 957 | if (current_pass_length > max_pass_length) { |
| 958 | max_pass_length = current_pass_length; |
| 959 | max_start = current_start; |
| 960 | max_end = rdclt_offset + rdclt; |
| 961 | } |
| 962 | } |
| 963 | } else { |
| 964 | current_pass_length = 0; |
| 965 | current_fail_length++; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 966 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 967 | if (current_fail_length >= (dly_val>>2)) { |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 968 | if (fail_found == false) { |
| 969 | fail_found = true; |
| 970 | } else if (pass_found == true) { |
| 971 | window_found = true; |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 972 | break; |
| 973 | } |
| 974 | } |
| 975 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 976 | } |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 977 | debug("."); |
| 978 | |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 979 | if (window_found == true) |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 980 | break; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 981 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 982 | tr1 = tr1 ^ SDRAM_TR1_RDCD_MASK; |
| 983 | rdclt_offset += dly_val; |
| 984 | } |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 985 | debug("\n"); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 986 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 987 | /* |
| 988 | * make sure we find the window |
| 989 | */ |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 990 | if (window_found == false) { |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 991 | printf("ERROR: Cannot determine a common read delay.\n"); |
Heiko Schocher | 68310b0 | 2007-06-25 19:11:37 +0200 | [diff] [blame] | 992 | spd_ddr_init_hang (); |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 993 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 994 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 995 | /* |
| 996 | * restore the orignal ECC setting |
| 997 | */ |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 998 | mtsdram(SDRAM0_CFG0, (cfg0 & ~SDRAM_CFG0_MCHK_MASK) | ecc_temp); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 999 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 1000 | /* |
| 1001 | * set the SDRAM TR1 RDCD value |
| 1002 | */ |
| 1003 | tr1 &= ~SDRAM_TR1_RDCD_MASK; |
| 1004 | if ((tr0 & SDRAM_TR0_SDCL_MASK) == SDRAM_TR0_SDCL_2_5_CLK) { |
| 1005 | tr1 |= SDRAM_TR1_RDCD_RCD_1_2; |
| 1006 | } else { |
| 1007 | tr1 |= SDRAM_TR1_RDCD_RCD_0_0; |
| 1008 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1009 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 1010 | /* |
| 1011 | * set the SDRAM TR1 RDCLT value |
| 1012 | */ |
| 1013 | tr1 &= ~SDRAM_TR1_RDCT_MASK; |
| 1014 | while (max_end >= (dly_val << 1)) { |
| 1015 | max_end -= (dly_val << 1); |
| 1016 | max_start -= (dly_val << 1); |
| 1017 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1018 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 1019 | rdclt_average = ((max_start + max_end) >> 1); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1020 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 1021 | if (rdclt_average < 0) { |
| 1022 | rdclt_average = 0; |
| 1023 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1024 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 1025 | if (rdclt_average >= dly_val) { |
| 1026 | rdclt_average -= dly_val; |
| 1027 | tr1 = tr1 ^ SDRAM_TR1_RDCD_MASK; |
| 1028 | } |
| 1029 | tr1 |= SDRAM_TR1_RDCT_ENCODE(rdclt_average); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1030 | |
Marek Vasut | 99b00d9 | 2011-10-21 14:17:12 +0000 | [diff] [blame] | 1031 | debug("tr1: %lx\n", tr1); |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 1032 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 1033 | /* |
| 1034 | * program SDRAM Timing Register 1 TR1 |
| 1035 | */ |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 1036 | mtsdram(SDRAM0_TR1, tr1); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 1039 | static unsigned long program_bxcr(unsigned long *dimm_populated, |
| 1040 | unsigned char *iic0_dimm_addr, |
| 1041 | unsigned long num_dimm_banks) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1042 | { |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 1043 | unsigned long dimm_num; |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 1044 | unsigned long bank_base_addr; |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 1045 | unsigned long cr; |
| 1046 | unsigned long i; |
Stefan Roese | c45d1e3 | 2005-11-15 16:04:58 +0100 | [diff] [blame] | 1047 | unsigned long j; |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 1048 | unsigned long temp; |
| 1049 | unsigned char num_row_addr; |
| 1050 | unsigned char num_col_addr; |
| 1051 | unsigned char num_banks; |
| 1052 | unsigned char bank_size_id; |
Stefan Roese | c45d1e3 | 2005-11-15 16:04:58 +0100 | [diff] [blame] | 1053 | unsigned long ctrl_bank_num[MAXBANKS]; |
| 1054 | unsigned long bx_cr_num; |
| 1055 | unsigned long largest_size_index; |
Wolfgang Denk | f6a692b | 2005-12-04 00:40:34 +0100 | [diff] [blame] | 1056 | unsigned long largest_size; |
| 1057 | unsigned long current_size_index; |
Stefan Roese | c45d1e3 | 2005-11-15 16:04:58 +0100 | [diff] [blame] | 1058 | BANKPARMS bank_parms[MAXBXCR]; |
| 1059 | unsigned long sorted_bank_num[MAXBXCR]; /* DDR Controller bank number table (sorted by size) */ |
| 1060 | unsigned long sorted_bank_size[MAXBXCR]; /* DDR Controller bank size table (sorted by size)*/ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1061 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 1062 | /* |
| 1063 | * Set the BxCR regs. First, wipe out the bank config registers. |
| 1064 | */ |
Stefan Roese | c45d1e3 | 2005-11-15 16:04:58 +0100 | [diff] [blame] | 1065 | for (bx_cr_num = 0; bx_cr_num < MAXBXCR; bx_cr_num++) { |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 1066 | mtdcr(SDRAM0_CFGADDR, SDRAM0_B0CR + (bx_cr_num << 2)); |
Stefan Roese | 918010a | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 1067 | mtdcr(SDRAM0_CFGDATA, 0x00000000); |
Stefan Roese | c45d1e3 | 2005-11-15 16:04:58 +0100 | [diff] [blame] | 1068 | bank_parms[bx_cr_num].bank_size_bytes = 0; |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 1069 | } |
Stefan Roese | c45d1e3 | 2005-11-15 16:04:58 +0100 | [diff] [blame] | 1070 | |
| 1071 | #ifdef CONFIG_BAMBOO |
| 1072 | /* |
| 1073 | * This next section is hardware dependent and must be programmed |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 1074 | * to match the hardware. For bamboo, the following holds... |
| 1075 | * 1. SDRAM0_B0CR: Bank 0 of dimm 0 ctrl_bank_num : 0 (soldered onboard) |
Stefan Roese | c45d1e3 | 2005-11-15 16:04:58 +0100 | [diff] [blame] | 1076 | * 2. SDRAM0_B1CR: Bank 0 of dimm 1 ctrl_bank_num : 1 |
| 1077 | * 3. SDRAM0_B2CR: Bank 1 of dimm 1 ctrl_bank_num : 1 |
| 1078 | * 4. SDRAM0_B3CR: Bank 0 of dimm 2 ctrl_bank_num : 3 |
| 1079 | * ctrl_bank_num corresponds to the first usable DDR controller bank number by DIMM |
| 1080 | */ |
| 1081 | ctrl_bank_num[0] = 0; |
| 1082 | ctrl_bank_num[1] = 1; |
| 1083 | ctrl_bank_num[2] = 3; |
| 1084 | #else |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 1085 | /* |
| 1086 | * Ocotea, Ebony and the other IBM/AMCC eval boards have |
| 1087 | * 2 DIMM slots with each max 2 banks |
| 1088 | */ |
Stefan Roese | c45d1e3 | 2005-11-15 16:04:58 +0100 | [diff] [blame] | 1089 | ctrl_bank_num[0] = 0; |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 1090 | ctrl_bank_num[1] = 2; |
Stefan Roese | 363330b | 2005-08-04 17:09:16 +0200 | [diff] [blame] | 1091 | #endif |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1092 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 1093 | /* |
| 1094 | * reset the bank_base address |
| 1095 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 1096 | bank_base_addr = CONFIG_SYS_SDRAM_BASE; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1097 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 1098 | for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) { |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 1099 | if (dimm_populated[dimm_num] == true) { |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 1100 | num_row_addr = spd_read(iic0_dimm_addr[dimm_num], 3); |
| 1101 | num_col_addr = spd_read(iic0_dimm_addr[dimm_num], 4); |
| 1102 | num_banks = spd_read(iic0_dimm_addr[dimm_num], 5); |
| 1103 | bank_size_id = spd_read(iic0_dimm_addr[dimm_num], 31); |
Marek Vasut | 99b00d9 | 2011-10-21 14:17:12 +0000 | [diff] [blame] | 1104 | debug("DIMM%ld: row=%d col=%d banks=%d\n", dimm_num, |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 1105 | num_row_addr, num_col_addr, num_banks); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1106 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 1107 | /* |
| 1108 | * Set the SDRAM0_BxCR regs |
| 1109 | */ |
| 1110 | cr = 0; |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 1111 | switch (bank_size_id) { |
| 1112 | case 0x02: |
| 1113 | cr |= SDRAM_BXCR_SDSZ_8; |
| 1114 | break; |
| 1115 | case 0x04: |
| 1116 | cr |= SDRAM_BXCR_SDSZ_16; |
| 1117 | break; |
| 1118 | case 0x08: |
| 1119 | cr |= SDRAM_BXCR_SDSZ_32; |
| 1120 | break; |
| 1121 | case 0x10: |
| 1122 | cr |= SDRAM_BXCR_SDSZ_64; |
| 1123 | break; |
| 1124 | case 0x20: |
| 1125 | cr |= SDRAM_BXCR_SDSZ_128; |
| 1126 | break; |
| 1127 | case 0x40: |
| 1128 | cr |= SDRAM_BXCR_SDSZ_256; |
| 1129 | break; |
| 1130 | case 0x80: |
| 1131 | cr |= SDRAM_BXCR_SDSZ_512; |
| 1132 | break; |
| 1133 | default: |
| 1134 | printf("DDR-SDRAM: DIMM %lu BxCR configuration.\n", |
| 1135 | dimm_num); |
| 1136 | printf("ERROR: Unsupported value for the banksize: %d.\n", |
| 1137 | bank_size_id); |
| 1138 | printf("Replace the DIMM module with a supported DIMM.\n\n"); |
Heiko Schocher | 68310b0 | 2007-06-25 19:11:37 +0200 | [diff] [blame] | 1139 | spd_ddr_init_hang (); |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 1140 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1141 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 1142 | switch (num_col_addr) { |
| 1143 | case 0x08: |
| 1144 | cr |= SDRAM_BXCR_SDAM_1; |
| 1145 | break; |
| 1146 | case 0x09: |
| 1147 | cr |= SDRAM_BXCR_SDAM_2; |
| 1148 | break; |
| 1149 | case 0x0A: |
| 1150 | cr |= SDRAM_BXCR_SDAM_3; |
| 1151 | break; |
| 1152 | case 0x0B: |
| 1153 | cr |= SDRAM_BXCR_SDAM_4; |
| 1154 | break; |
| 1155 | default: |
| 1156 | printf("DDR-SDRAM: DIMM %lu BxCR configuration.\n", |
| 1157 | dimm_num); |
| 1158 | printf("ERROR: Unsupported value for number of " |
| 1159 | "column addresses: %d.\n", num_col_addr); |
| 1160 | printf("Replace the DIMM module with a supported DIMM.\n\n"); |
Heiko Schocher | 68310b0 | 2007-06-25 19:11:37 +0200 | [diff] [blame] | 1161 | spd_ddr_init_hang (); |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 1162 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1163 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 1164 | /* |
| 1165 | * enable the bank |
| 1166 | */ |
| 1167 | cr |= SDRAM_BXCR_SDBE; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1168 | |
Wolfgang Denk | 70df7bc | 2007-06-22 23:59:00 +0200 | [diff] [blame] | 1169 | for (i = 0; i < num_banks; i++) { |
Stefan Roese | 63dc63c | 2007-06-01 15:09:50 +0200 | [diff] [blame] | 1170 | bank_parms[ctrl_bank_num[dimm_num]+i].bank_size_bytes = |
| 1171 | (4 << 20) * bank_size_id; |
| 1172 | bank_parms[ctrl_bank_num[dimm_num]+i].cr = cr; |
Marek Vasut | 99b00d9 | 2011-10-21 14:17:12 +0000 | [diff] [blame] | 1173 | debug("DIMM%ld-bank %ld (SDRAM0_B%ldCR): " |
| 1174 | "bank_size_bytes=%ld\n", |
| 1175 | dimm_num, i, |
| 1176 | ctrl_bank_num[dimm_num] + i, |
| 1177 | bank_parms[ctrl_bank_num[dimm_num] + i].bank_size_bytes); |
Wolfgang Denk | 70df7bc | 2007-06-22 23:59:00 +0200 | [diff] [blame] | 1178 | } |
Stefan Roese | c45d1e3 | 2005-11-15 16:04:58 +0100 | [diff] [blame] | 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | /* Initialize sort tables */ |
| 1183 | for (i = 0; i < MAXBXCR; i++) { |
| 1184 | sorted_bank_num[i] = i; |
| 1185 | sorted_bank_size[i] = bank_parms[i].bank_size_bytes; |
| 1186 | } |
| 1187 | |
| 1188 | for (i = 0; i < MAXBXCR-1; i++) { |
| 1189 | largest_size = sorted_bank_size[i]; |
| 1190 | largest_size_index = 255; |
| 1191 | |
| 1192 | /* Find the largest remaining value */ |
| 1193 | for (j = i + 1; j < MAXBXCR; j++) { |
| 1194 | if (sorted_bank_size[j] > largest_size) { |
| 1195 | /* Save largest remaining value and its index */ |
| 1196 | largest_size = sorted_bank_size[j]; |
| 1197 | largest_size_index = j; |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 1198 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1199 | } |
Stefan Roese | c45d1e3 | 2005-11-15 16:04:58 +0100 | [diff] [blame] | 1200 | |
| 1201 | if (largest_size_index != 255) { |
| 1202 | /* Swap the current and largest values */ |
| 1203 | current_size_index = sorted_bank_num[largest_size_index]; |
| 1204 | sorted_bank_size[largest_size_index] = sorted_bank_size[i]; |
| 1205 | sorted_bank_size[i] = largest_size; |
| 1206 | sorted_bank_num[largest_size_index] = sorted_bank_num[i]; |
| 1207 | sorted_bank_num[i] = current_size_index; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | /* Set the SDRAM0_BxCR regs thanks to sort tables */ |
| 1212 | for (bx_cr_num = 0, bank_base_addr = 0; bx_cr_num < MAXBXCR; bx_cr_num++) { |
| 1213 | if (bank_parms[sorted_bank_num[bx_cr_num]].bank_size_bytes) { |
Stefan Roese | 6987e65 | 2009-09-24 13:59:57 +0200 | [diff] [blame] | 1214 | mtdcr(SDRAM0_CFGADDR, SDRAM0_B0CR + (sorted_bank_num[bx_cr_num] << 2)); |
Stefan Roese | 918010a | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 1215 | temp = mfdcr(SDRAM0_CFGDATA) & ~(SDRAM_BXCR_SDBA_MASK | SDRAM_BXCR_SDSZ_MASK | |
Stefan Roese | c45d1e3 | 2005-11-15 16:04:58 +0100 | [diff] [blame] | 1216 | SDRAM_BXCR_SDAM_MASK | SDRAM_BXCR_SDBE); |
| 1217 | temp = temp | (bank_base_addr & SDRAM_BXCR_SDBA_MASK) | |
| 1218 | bank_parms[sorted_bank_num[bx_cr_num]].cr; |
Stefan Roese | 918010a | 2009-09-09 16:25:29 +0200 | [diff] [blame] | 1219 | mtdcr(SDRAM0_CFGDATA, temp); |
Stefan Roese | c45d1e3 | 2005-11-15 16:04:58 +0100 | [diff] [blame] | 1220 | bank_base_addr += bank_parms[sorted_bank_num[bx_cr_num]].bank_size_bytes; |
Marek Vasut | 99b00d9 | 2011-10-21 14:17:12 +0000 | [diff] [blame] | 1221 | debug("SDRAM0_B%ldCR=0x%08lx\n", |
| 1222 | sorted_bank_num[bx_cr_num], temp); |
Stefan Roese | c45d1e3 | 2005-11-15 16:04:58 +0100 | [diff] [blame] | 1223 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 1224 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1225 | |
Stefan Roese | 326c971 | 2005-08-01 16:41:48 +0200 | [diff] [blame] | 1226 | return(bank_base_addr); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1227 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1228 | #endif /* CONFIG_SPD_EEPROM */ |