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