wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
Stefan Roese | 8d98230 | 2007-01-18 10:25:34 +0100 | [diff] [blame] | 2 | * (C) Copyright 2005-2007 |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 3 | * Stefan Roese, DENX Software Engineering, sr@denx.de. |
| 4 | * |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 5 | * (C) Copyright 2006 |
| 6 | * DAVE Srl <www.dave-tech.it> |
| 7 | * |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 8 | * (C) Copyright 2002-2004 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 9 | * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com |
| 10 | * |
| 11 | * See file CREDITS for list of people who contributed to this |
| 12 | * project. |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of |
| 17 | * the License, or (at your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, write to the Free Software |
| 26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 27 | * MA 02111-1307 USA |
| 28 | */ |
| 29 | |
| 30 | #include <common.h> |
| 31 | #include <ppc4xx.h> |
| 32 | #include <asm/processor.h> |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 33 | #include "sdram.h" |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 34 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 35 | #ifdef CONFIG_SDRAM_BANK0 |
| 36 | |
Stefan Roese | 8d98230 | 2007-01-18 10:25:34 +0100 | [diff] [blame] | 37 | #ifndef CONFIG_440 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 38 | |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 39 | #ifndef CFG_SDRAM_TABLE |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 40 | sdram_conf_t mb0cf[] = { |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 41 | {(128 << 20), 13, 0x000A4001}, /* (0-128MB) Address Mode 3, 13x10(4) */ |
| 42 | {(64 << 20), 13, 0x00084001}, /* (0-64MB) Address Mode 3, 13x9(4) */ |
| 43 | {(32 << 20), 12, 0x00062001}, /* (0-32MB) Address Mode 2, 12x9(4) */ |
| 44 | {(16 << 20), 12, 0x00046001}, /* (0-16MB) Address Mode 4, 12x8(4) */ |
| 45 | {(4 << 20), 11, 0x00008001}, /* (0-4MB) Address Mode 5, 11x8(2) */ |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 46 | }; |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 47 | #else |
| 48 | sdram_conf_t mb0cf[] = CFG_SDRAM_TABLE; |
| 49 | #endif |
| 50 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 51 | #define N_MB0CF (sizeof(mb0cf) / sizeof(mb0cf[0])) |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 52 | |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 53 | #ifdef CFG_SDRAM_CASL |
| 54 | static ulong ns2clks(ulong ns) |
| 55 | { |
| 56 | ulong bus_period_x_10 = ONE_BILLION / (get_bus_freq(0) / 10); |
| 57 | |
| 58 | return ((ns * 10) + bus_period_x_10) / bus_period_x_10; |
| 59 | } |
| 60 | #endif /* CFG_SDRAM_CASL */ |
| 61 | |
| 62 | static ulong compute_sdtr1(ulong speed) |
| 63 | { |
| 64 | #ifdef CFG_SDRAM_CASL |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 65 | ulong tmp; |
| 66 | ulong sdtr1 = 0; |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 67 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 68 | /* CASL */ |
| 69 | if (CFG_SDRAM_CASL < 2) |
| 70 | sdtr1 |= (1 << SDRAM0_TR_CASL); |
| 71 | else |
| 72 | if (CFG_SDRAM_CASL > 4) |
| 73 | sdtr1 |= (3 << SDRAM0_TR_CASL); |
| 74 | else |
| 75 | sdtr1 |= ((CFG_SDRAM_CASL-1) << SDRAM0_TR_CASL); |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 76 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 77 | /* PTA */ |
| 78 | tmp = ns2clks(CFG_SDRAM_PTA); |
| 79 | if ((tmp >= 2) && (tmp <= 4)) |
| 80 | sdtr1 |= ((tmp-1) << SDRAM0_TR_PTA); |
| 81 | else |
| 82 | sdtr1 |= ((4-1) << SDRAM0_TR_PTA); |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 83 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 84 | /* CTP */ |
| 85 | tmp = ns2clks(CFG_SDRAM_CTP); |
| 86 | if ((tmp >= 2) && (tmp <= 4)) |
| 87 | sdtr1 |= ((tmp-1) << SDRAM0_TR_CTP); |
| 88 | else |
| 89 | sdtr1 |= ((4-1) << SDRAM0_TR_CTP); |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 90 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 91 | /* LDF */ |
| 92 | tmp = ns2clks(CFG_SDRAM_LDF); |
| 93 | if ((tmp >= 2) && (tmp <= 4)) |
| 94 | sdtr1 |= ((tmp-1) << SDRAM0_TR_LDF); |
| 95 | else |
| 96 | sdtr1 |= ((2-1) << SDRAM0_TR_LDF); |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 97 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 98 | /* RFTA */ |
| 99 | tmp = ns2clks(CFG_SDRAM_RFTA); |
| 100 | if ((tmp >= 4) && (tmp <= 10)) |
| 101 | sdtr1 |= ((tmp-4) << SDRAM0_TR_RFTA); |
| 102 | else |
| 103 | sdtr1 |= ((10-4) << SDRAM0_TR_RFTA); |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 104 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 105 | /* RCD */ |
| 106 | tmp = ns2clks(CFG_SDRAM_RCD); |
| 107 | if ((tmp >= 2) && (tmp <= 4)) |
| 108 | sdtr1 |= ((tmp-1) << SDRAM0_TR_RCD); |
| 109 | else |
| 110 | sdtr1 |= ((4-1) << SDRAM0_TR_RCD); |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 111 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 112 | return sdtr1; |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 113 | #else /* CFG_SDRAM_CASL */ |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 114 | /* |
| 115 | * If no values are configured in the board config file |
| 116 | * use the default values, which seem to be ok for most |
| 117 | * boards. |
| 118 | * |
| 119 | * REMARK: |
| 120 | * For new board ports we strongly recommend to define the |
| 121 | * correct values for the used SDRAM chips in your board |
| 122 | * config file (see PPChameleonEVB.h) |
| 123 | */ |
| 124 | if (speed > 100000000) { |
| 125 | /* |
| 126 | * 133 MHz SDRAM |
| 127 | */ |
| 128 | return 0x01074015; |
| 129 | } else { |
| 130 | /* |
| 131 | * default: 100 MHz SDRAM |
| 132 | */ |
| 133 | return 0x0086400d; |
| 134 | } |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 135 | #endif /* CFG_SDRAM_CASL */ |
| 136 | } |
| 137 | |
| 138 | /* refresh is expressed in ms */ |
| 139 | static ulong compute_rtr(ulong speed, ulong rows, ulong refresh) |
| 140 | { |
| 141 | #ifdef CFG_SDRAM_CASL |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 142 | ulong tmp; |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 143 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 144 | tmp = ((refresh*1000*1000) / (1 << rows)) * (speed / 1000); |
| 145 | tmp /= 1000000; |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 146 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 147 | return ((tmp & 0x00003FF8) << 16); |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 148 | #else /* CFG_SDRAM_CASL */ |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 149 | if (speed > 100000000) { |
| 150 | /* |
| 151 | * 133 MHz SDRAM |
| 152 | */ |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 153 | return 0x07f00000; |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 154 | } else { |
| 155 | /* |
| 156 | * default: 100 MHz SDRAM |
| 157 | */ |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 158 | return 0x05f00000; |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 159 | } |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 160 | #endif /* CFG_SDRAM_CASL */ |
| 161 | } |
| 162 | |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 163 | /* |
| 164 | * Autodetect onboard SDRAM on 405 platforms |
| 165 | */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 166 | void sdram_init(void) |
| 167 | { |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 168 | ulong speed; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 169 | ulong sdtr1; |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 170 | int i; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 171 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 172 | /* |
| 173 | * Determine SDRAM speed |
| 174 | */ |
| 175 | speed = get_bus_freq(0); /* parameter not used on ppc4xx */ |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 176 | |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 177 | /* |
| 178 | * sdtr1 (register SDRAM0_TR) must take into account timings listed |
| 179 | * in SDRAM chip datasheet. rtr (register SDRAM0_RTR) must take into |
| 180 | * account actual SDRAM size. So we can set up sdtr1 according to what |
| 181 | * is specified in board configuration file while rtr dependds on SDRAM |
| 182 | * size we are assuming before detection. |
| 183 | */ |
| 184 | sdtr1 = compute_sdtr1(speed); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 185 | |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 186 | for (i=0; i<N_MB0CF; i++) { |
stroese | 51c57b9 | 2003-02-10 16:26:37 +0000 | [diff] [blame] | 187 | /* |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 188 | * Disable memory controller. |
stroese | 51c57b9 | 2003-02-10 16:26:37 +0000 | [diff] [blame] | 189 | */ |
Stefan Roese | a5d182e | 2007-08-14 14:44:41 +0200 | [diff] [blame] | 190 | mtsdram(mem_mcopt1, 0x00000000); |
wdenk | 41e2e05 | 2003-02-11 01:49:43 +0000 | [diff] [blame] | 191 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 192 | /* |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 193 | * Set MB0CF for bank 0. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 194 | */ |
Stefan Roese | a5d182e | 2007-08-14 14:44:41 +0200 | [diff] [blame] | 195 | mtsdram(mem_mb0cf, mb0cf[i].reg); |
| 196 | mtsdram(mem_sdtr1, sdtr1); |
| 197 | mtsdram(mem_rtr, compute_rtr(speed, mb0cf[i].rows, 64)); |
wdenk | 41e2e05 | 2003-02-11 01:49:43 +0000 | [diff] [blame] | 198 | |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 199 | udelay(200); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 200 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 201 | /* |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 202 | * Set memory controller options reg, MCOPT1. |
| 203 | * Set DC_EN to '1' and BRD_PRF to '01' for 16 byte PLB Burst |
| 204 | * read/prefetch. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 205 | */ |
Stefan Roese | a5d182e | 2007-08-14 14:44:41 +0200 | [diff] [blame] | 206 | mtsdram(mem_mcopt1, 0x80800000); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 207 | |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 208 | udelay(10000); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 209 | |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 210 | if (get_ram_size(0, mb0cf[i].size) == mb0cf[i].size) { |
| 211 | /* |
John Otken | 9aa3677 | 2007-07-26 17:49:11 +0200 | [diff] [blame] | 212 | * OK, size detected. Enable second bank if |
| 213 | * defined (assumes same type as bank 0) |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 214 | */ |
John Otken | 9aa3677 | 2007-07-26 17:49:11 +0200 | [diff] [blame] | 215 | #ifdef CONFIG_SDRAM_BANK1 |
| 216 | u32 b1cr = mb0cf[i].size | mb0cf[i].reg; |
| 217 | |
Stefan Roese | a5d182e | 2007-08-14 14:44:41 +0200 | [diff] [blame] | 218 | mtsdram(mem_mcopt1, 0x00000000); |
| 219 | mtsdram(mem_mb1cf, b1cr); /* SDRAM0_B1CR */ |
| 220 | mtsdram(mem_mcopt1, 0x80800000); |
John Otken | 9aa3677 | 2007-07-26 17:49:11 +0200 | [diff] [blame] | 221 | udelay(10000); |
Stefan Roese | a5d182e | 2007-08-14 14:44:41 +0200 | [diff] [blame] | 222 | |
| 223 | /* |
| 224 | * Check if 2nd bank is really available. |
| 225 | * If the size not equal to the size of the first |
| 226 | * bank, then disable the 2nd bank completely. |
| 227 | */ |
| 228 | if (get_ram_size((long *)mb0cf[i].size, mb0cf[i].size) != |
| 229 | mb0cf[i].size) { |
| 230 | mtsdram(mem_mb1cf, 0); |
| 231 | mtsdram(mem_mcopt1, 0); |
| 232 | } |
John Otken | 9aa3677 | 2007-07-26 17:49:11 +0200 | [diff] [blame] | 233 | #endif |
stroese | 55ca749 | 2004-07-15 14:41:13 +0000 | [diff] [blame] | 234 | return; |
| 235 | } |
stroese | 51c57b9 | 2003-02-10 16:26:37 +0000 | [diff] [blame] | 236 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 239 | #else /* CONFIG_440 */ |
| 240 | |
Stefan Roese | 8d98230 | 2007-01-18 10:25:34 +0100 | [diff] [blame] | 241 | /* |
| 242 | * Define some default values. Those can be overwritten in the |
| 243 | * board config file. |
| 244 | */ |
| 245 | |
| 246 | #ifndef CFG_SDRAM_TABLE |
| 247 | sdram_conf_t mb0cf[] = { |
| 248 | {(256 << 20), 13, 0x000C4001}, /* 256MB mode 3, 13x10(4) */ |
| 249 | {(64 << 20), 12, 0x00082001} /* 64MB mode 2, 12x9(4) */ |
| 250 | }; |
| 251 | #else |
| 252 | sdram_conf_t mb0cf[] = CFG_SDRAM_TABLE; |
| 253 | #endif |
| 254 | |
| 255 | #ifndef CFG_SDRAM0_TR0 |
| 256 | #define CFG_SDRAM0_TR0 0x41094012 |
| 257 | #endif |
| 258 | |
| 259 | #define N_MB0CF (sizeof(mb0cf) / sizeof(mb0cf[0])) |
| 260 | |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 261 | #define NUM_TRIES 64 |
| 262 | #define NUM_READS 10 |
| 263 | |
| 264 | static void sdram_tr1_set(int ram_address, int* tr1_value) |
| 265 | { |
| 266 | int i; |
| 267 | int j, k; |
| 268 | volatile unsigned int* ram_pointer = (unsigned int *)ram_address; |
| 269 | int first_good = -1, last_bad = 0x1ff; |
| 270 | |
| 271 | unsigned long test[NUM_TRIES] = { |
| 272 | 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF, |
| 273 | 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF, |
| 274 | 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000, |
| 275 | 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000, |
| 276 | 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555, |
| 277 | 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555, |
| 278 | 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA, |
| 279 | 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA, |
| 280 | 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A, |
| 281 | 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A, |
| 282 | 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5, |
| 283 | 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5, |
| 284 | 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA, |
| 285 | 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA, |
| 286 | 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55, |
| 287 | 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55 }; |
| 288 | |
| 289 | /* go through all possible SDRAM0_TR1[RDCT] values */ |
| 290 | for (i=0; i<=0x1ff; i++) { |
| 291 | /* set the current value for TR1 */ |
| 292 | mtsdram(mem_tr1, (0x80800800 | i)); |
| 293 | |
| 294 | /* write values */ |
| 295 | for (j=0; j<NUM_TRIES; j++) { |
| 296 | ram_pointer[j] = test[j]; |
| 297 | |
| 298 | /* clear any cache at ram location */ |
| 299 | __asm__("dcbf 0,%0": :"r" (&ram_pointer[j])); |
| 300 | } |
| 301 | |
| 302 | /* read values back */ |
| 303 | for (j=0; j<NUM_TRIES; j++) { |
| 304 | for (k=0; k<NUM_READS; k++) { |
| 305 | /* clear any cache at ram location */ |
| 306 | __asm__("dcbf 0,%0": :"r" (&ram_pointer[j])); |
| 307 | |
| 308 | if (ram_pointer[j] != test[j]) |
| 309 | break; |
| 310 | } |
| 311 | |
| 312 | /* read error */ |
| 313 | if (k != NUM_READS) |
| 314 | break; |
| 315 | } |
| 316 | |
| 317 | /* we have a SDRAM0_TR1[RDCT] that is part of the window */ |
| 318 | if (j == NUM_TRIES) { |
| 319 | if (first_good == -1) |
| 320 | first_good = i; /* found beginning of window */ |
| 321 | } else { /* bad read */ |
| 322 | /* if we have not had a good read then don't care */ |
| 323 | if (first_good != -1) { |
| 324 | /* first failure after a good read */ |
| 325 | last_bad = i-1; |
| 326 | break; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /* return the current value for TR1 */ |
| 332 | *tr1_value = (first_good + last_bad) / 2; |
| 333 | } |
| 334 | |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 335 | #ifdef CONFIG_SDRAM_ECC |
| 336 | static void ecc_init(ulong start, ulong size) |
| 337 | { |
| 338 | ulong current_addr; /* current byte address */ |
| 339 | ulong end_addr; /* end of memory region */ |
| 340 | ulong addr_inc; /* address skip between writes */ |
| 341 | ulong cfg0_reg; /* for restoring ECC state */ |
| 342 | |
| 343 | /* |
| 344 | * TODO: Enable dcache before running this test (speedup) |
| 345 | */ |
| 346 | |
| 347 | mfsdram(mem_cfg0, cfg0_reg); |
| 348 | mtsdram(mem_cfg0, (cfg0_reg & ~SDRAM_CFG0_MEMCHK) | SDRAM_CFG0_MEMCHK_GEN); |
| 349 | |
| 350 | /* |
| 351 | * look at geometry of SDRAM (data width) to determine whether we |
| 352 | * can skip words when writing |
| 353 | */ |
| 354 | if ((cfg0_reg & SDRAM_CFG0_DRAMWDTH) == SDRAM_CFG0_DRAMWDTH_32) |
| 355 | addr_inc = 4; |
| 356 | else |
| 357 | addr_inc = 8; |
| 358 | |
| 359 | current_addr = start; |
| 360 | end_addr = start + size; |
| 361 | |
| 362 | while (current_addr < end_addr) { |
| 363 | *((ulong *)current_addr) = 0x00000000; |
| 364 | current_addr += addr_inc; |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * TODO: Flush dcache and disable it again |
| 369 | */ |
| 370 | |
| 371 | /* |
| 372 | * Enable ecc checking and parity errors |
| 373 | */ |
| 374 | mtsdram(mem_cfg0, (cfg0_reg & ~SDRAM_CFG0_MEMCHK) | SDRAM_CFG0_MEMCHK_CHK); |
| 375 | } |
| 376 | #endif |
| 377 | |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 378 | /* |
| 379 | * Autodetect onboard DDR SDRAM on 440 platforms |
| 380 | * |
| 381 | * NOTE: Some of the hardcoded values are hardware dependant, |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 382 | * so this should be extended for other future boards |
| 383 | * using this routine! |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 384 | */ |
| 385 | long int initdram(int board_type) |
| 386 | { |
| 387 | int i; |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 388 | int tr1_bank1; |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 389 | |
Stefan Roese | 8d98230 | 2007-01-18 10:25:34 +0100 | [diff] [blame] | 390 | #if defined(CONFIG_440GX) || defined(CONFIG_440EP) || \ |
| 391 | defined(CONFIG_440GR) || defined(CONFIG_440SP) |
Stefan Roese | 4963968 | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 392 | /* |
| 393 | * Soft-reset SDRAM controller. |
| 394 | */ |
| 395 | mtsdr(sdr_srst, SDR0_SRST_DMC); |
| 396 | mtsdr(sdr_srst, 0x00000000); |
| 397 | #endif |
| 398 | |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 399 | for (i=0; i<N_MB0CF; i++) { |
| 400 | /* |
| 401 | * Disable memory controller. |
| 402 | */ |
| 403 | mtsdram(mem_cfg0, 0x00000000); |
| 404 | |
| 405 | /* |
| 406 | * Setup some default |
| 407 | */ |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 408 | mtsdram(mem_uabba, 0x00000000); /* ubba=0 (default) */ |
| 409 | mtsdram(mem_slio, 0x00000000); /* rdre=0 wrre=0 rarw=0 */ |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 410 | mtsdram(mem_devopt, 0x00000000); /* dll=0 ds=0 (normal) */ |
| 411 | mtsdram(mem_wddctr, 0x00000000); /* wrcp=0 dcd=0 */ |
Wolfgang Denk | ebd3deb | 2006-04-16 10:51:58 +0200 | [diff] [blame] | 412 | mtsdram(mem_clktr, 0x40000000); /* clkp=1 (90 deg wr) dcdt=0 */ |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 413 | |
| 414 | /* |
| 415 | * Following for CAS Latency = 2.5 @ 133 MHz PLB |
| 416 | */ |
| 417 | mtsdram(mem_b0cr, mb0cf[i].reg); |
Stefan Roese | 8d98230 | 2007-01-18 10:25:34 +0100 | [diff] [blame] | 418 | mtsdram(mem_tr0, CFG_SDRAM0_TR0); |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 419 | mtsdram(mem_tr1, 0x80800800); /* SS=T2 SL=STAGE 3 CD=1 CT=0x00*/ |
Stefan Roese | 76e3d2a | 2007-01-06 15:58:09 +0100 | [diff] [blame] | 420 | mtsdram(mem_rtr, 0x04100000); /* Interval 7.8µs @ 133MHz PLB */ |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 421 | mtsdram(mem_cfg1, 0x00000000); /* Self-refresh exit, disable PM*/ |
| 422 | udelay(400); /* Delay 200 usecs (min) */ |
| 423 | |
| 424 | /* |
| 425 | * Enable the controller, then wait for DCEN to complete |
| 426 | */ |
Stefan Roese | e335ee0 | 2006-07-28 18:34:58 +0200 | [diff] [blame] | 427 | mtsdram(mem_cfg0, 0x82000000); /* DCEN=1, PMUD=0, 64-bit */ |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 428 | udelay(10000); |
| 429 | |
| 430 | if (get_ram_size(0, mb0cf[i].size) == mb0cf[i].size) { |
| 431 | /* |
Stefan Roese | fd63793 | 2006-03-17 10:28:24 +0100 | [diff] [blame] | 432 | * Optimize TR1 to current hardware environment |
| 433 | */ |
| 434 | sdram_tr1_set(0x00000000, &tr1_bank1); |
| 435 | mtsdram(mem_tr1, (tr1_bank1 | 0x80800800)); |
| 436 | |
| 437 | #ifdef CONFIG_SDRAM_ECC |
| 438 | ecc_init(0, mb0cf[i].size); |
| 439 | #endif |
| 440 | |
| 441 | /* |
Stefan Roese | c443fe9 | 2005-11-22 13:20:42 +0100 | [diff] [blame] | 442 | * OK, size detected -> all done |
| 443 | */ |
| 444 | return mb0cf[i].size; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | return 0; /* nothing found ! */ |
| 449 | } |
| 450 | |
| 451 | #endif /* CONFIG_440 */ |
| 452 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 453 | #endif /* CONFIG_SDRAM_BANK0 */ |