Patrick Georgi | d083595 | 2010-10-05 09:07:10 +0000 | [diff] [blame] | 1 | #include <stdint.h> |
Myles Watson | 3426195 | 2010-03-19 02:33:40 +0000 | [diff] [blame] | 2 | #include <lib.h> /* Prototypes */ |
Patrick Georgi | d083595 | 2010-10-05 09:07:10 +0000 | [diff] [blame] | 3 | #include <console/console.h> |
Myles Watson | 3426195 | 2010-03-19 02:33:40 +0000 | [diff] [blame] | 4 | |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 5 | static void write_phys(unsigned long addr, u32 value) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 6 | { |
Stefan Reinauer | a7acc51 | 2010-02-25 13:40:49 +0000 | [diff] [blame] | 7 | // Assembler in lib/ is very ugly. But we properly guarded |
| 8 | // it so let's obey this one for now |
| 9 | #if CONFIG_SSE2 |
Eric Biederman | 8d9c123 | 2003-06-17 08:42:17 +0000 | [diff] [blame] | 10 | asm volatile( |
| 11 | "movnti %1, (%0)" |
| 12 | : /* outputs */ |
| 13 | : "r" (addr), "r" (value) /* inputs */ |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 14 | #ifndef __GNUC__ /* GCC does not like empty clobbers? */ |
Eric Biederman | 8d9c123 | 2003-06-17 08:42:17 +0000 | [diff] [blame] | 15 | : /* clobbers */ |
Stefan Reinauer | 7671293 | 2004-05-27 11:13:24 +0000 | [diff] [blame] | 16 | #endif |
Eric Biederman | 8d9c123 | 2003-06-17 08:42:17 +0000 | [diff] [blame] | 17 | ); |
| 18 | #else |
Eric Biederman | 5268557 | 2003-05-19 19:16:21 +0000 | [diff] [blame] | 19 | volatile unsigned long *ptr; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 20 | ptr = (void *)addr; |
| 21 | *ptr = value; |
Eric Biederman | 8d9c123 | 2003-06-17 08:42:17 +0000 | [diff] [blame] | 22 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 23 | } |
| 24 | |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 25 | static u32 read_phys(unsigned long addr) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 26 | { |
Eric Biederman | 5268557 | 2003-05-19 19:16:21 +0000 | [diff] [blame] | 27 | volatile unsigned long *ptr; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 28 | ptr = (void *)addr; |
| 29 | return *ptr; |
| 30 | } |
| 31 | |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 32 | static void phys_memory_barrier(void) |
| 33 | { |
| 34 | #if CONFIG_SSE2 |
| 35 | // Needed for movnti |
| 36 | asm volatile ( |
| 37 | "sfence" |
| 38 | :: |
| 39 | #ifdef __GNUC__ /* ROMCC does not like memory clobbers */ |
| 40 | : "memory" |
| 41 | #endif |
| 42 | ); |
| 43 | #else |
| 44 | #ifdef __GNUC__ /* ROMCC does not like empty asm statements */ |
| 45 | asm volatile ("" ::: "memory"); |
| 46 | #endif |
| 47 | #endif |
| 48 | } |
| 49 | |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 50 | /** |
| 51 | * Rotate ones test pattern that access every bit on a 128bit wide |
| 52 | * memory bus. To test most address lines, addresses are scattered |
| 53 | * using 256B, 4kB and 64kB increments. |
| 54 | * |
Martin Roth | 5f066b2 | 2015-01-04 16:47:39 -0700 | [diff] [blame] | 55 | * @param idx Index to test pattern (0=<idx<0x400) |
| 56 | * @param addr Memory to access on idx |
| 57 | * @param value Value to write or read at addr |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 58 | */ |
| 59 | static inline void test_pattern(unsigned short int idx, |
| 60 | unsigned long *addr, unsigned long *value) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 61 | { |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 62 | uint8_t j, k; |
| 63 | |
| 64 | k = (idx >> 8) + 1; |
| 65 | j = (idx >> 4) & 0x0f; |
| 66 | *addr = idx & 0x0f; |
| 67 | *addr |= j << (4*k); |
| 68 | *value = 0x01010101 << (j & 7); |
| 69 | if (j & 8) |
| 70 | *value = ~(*value); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 73 | /** |
| 74 | * Simple write-read-verify memory test. See console debug output for |
| 75 | * any dislocated bytes. |
| 76 | * |
Martin Roth | 5f066b2 | 2015-01-04 16:47:39 -0700 | [diff] [blame] | 77 | * @param start System memory offset, aligned to 128bytes |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 78 | */ |
| 79 | static int ram_bitset_nodie(unsigned long start) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 80 | { |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 81 | unsigned long addr, value, value2; |
| 82 | unsigned short int idx; |
| 83 | unsigned char failed, failures; |
| 84 | uint8_t verbose = 0; |
| 85 | |
Kyösti Mälkki | 481814d | 2011-10-28 16:15:47 +0300 | [diff] [blame] | 86 | #if !defined(__ROMCC__) |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 87 | printk(BIOS_DEBUG, "DRAM bitset write: 0x%08lx\n", start); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 88 | #else |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 89 | print_debug("DRAM bitset write: 0x"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 90 | print_debug_hex32(start); |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 91 | print_debug("\n"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 92 | #endif |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 93 | for (idx=0; idx<0x400; idx+=4) { |
| 94 | test_pattern(idx, &addr, &value); |
| 95 | write_phys(start + addr, value); |
| 96 | } |
| 97 | |
| 98 | /* Make sure we don't read before we wrote */ |
| 99 | phys_memory_barrier(); |
| 100 | |
Kyösti Mälkki | 481814d | 2011-10-28 16:15:47 +0300 | [diff] [blame] | 101 | #if !defined(__ROMCC__) |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 102 | printk(BIOS_DEBUG, "DRAM bitset verify: 0x%08lx\n", start); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 103 | #else |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 104 | print_debug("DRAM bitset verify: 0x"); |
| 105 | print_debug_hex32(start); |
| 106 | print_debug("\n"); |
| 107 | #endif |
| 108 | failures = 0; |
| 109 | for (idx=0; idx<0x400; idx+=4) { |
| 110 | test_pattern(idx, &addr, &value); |
| 111 | value2 = read_phys(start + addr); |
| 112 | |
| 113 | failed = (value2 != value); |
| 114 | failures |= failed; |
| 115 | if (failed && !verbose) { |
| 116 | #if !defined(__ROMCC__) |
| 117 | printk(BIOS_ERR, "0x%08lx wr: 0x%08lx rd: 0x%08lx FAIL\n", |
| 118 | start + addr, value, value2); |
| 119 | #else |
| 120 | print_err_hex32(start + addr); |
| 121 | print_err(" wr: 0x"); |
| 122 | print_err_hex32(value); |
| 123 | print_err(" rd: 0x"); |
| 124 | print_err_hex32(value2); |
| 125 | print_err(" FAIL\n"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 126 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 127 | } |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 128 | if (verbose) { |
Kyösti Mälkki | 481814d | 2011-10-28 16:15:47 +0300 | [diff] [blame] | 129 | #if !defined(__ROMCC__) |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 130 | if ((addr & 0x0f) == 0) |
| 131 | printk(BIOS_DEBUG, "%08lx wr: %08lx rd:", |
| 132 | start + addr, value); |
| 133 | if (failed) |
| 134 | printk(BIOS_DEBUG, " %08lx!", value2); |
| 135 | else |
| 136 | printk(BIOS_DEBUG, " %08lx ", value2); |
| 137 | if ((addr & 0x0f) == 0xc) |
| 138 | printk(BIOS_DEBUG, "\n"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 139 | #else |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 140 | if ((addr & 0x0f) == 0) { |
| 141 | print_dbg_hex32(start + addr); |
| 142 | print_dbg(" wr: "); |
| 143 | print_dbg_hex32(value); |
| 144 | print_dbg(" rd: "); |
Richard Smith | ffb7d8a | 2006-04-01 04:10:44 +0000 | [diff] [blame] | 145 | } |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 146 | print_dbg_hex32(value2); |
| 147 | if (failed) |
| 148 | print_dbg("! "); |
| 149 | else |
| 150 | print_dbg(" "); |
| 151 | if ((addr & 0x0f) == 0xc) |
| 152 | print_dbg("\n"); |
| 153 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 156 | if (failures) { |
| 157 | post_code(0xea); |
Kyösti Mälkki | 481814d | 2011-10-28 16:15:47 +0300 | [diff] [blame] | 158 | #if !defined(__ROMCC__) |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 159 | printk(BIOS_DEBUG, "\nDRAM did _NOT_ verify!\n"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 160 | #else |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 161 | print_debug("\nDRAM did _NOT_ verify!\n"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 162 | #endif |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 163 | return 1; |
Richard Smith | ffb7d8a | 2006-04-01 04:10:44 +0000 | [diff] [blame] | 164 | } |
| 165 | else { |
Kyösti Mälkki | 481814d | 2011-10-28 16:15:47 +0300 | [diff] [blame] | 166 | #if !defined(__ROMCC__) |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 167 | printk(BIOS_DEBUG, "\nDRAM range verified.\n"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 168 | #else |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 169 | print_debug("\nDRAM range verified.\n"); |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 170 | return 0; |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 171 | #endif |
Richard Smith | ffb7d8a | 2006-04-01 04:10:44 +0000 | [diff] [blame] | 172 | } |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 173 | return 0; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | |
Eric Biederman | 8d9c123 | 2003-06-17 08:42:17 +0000 | [diff] [blame] | 177 | void ram_check(unsigned long start, unsigned long stop) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 178 | { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 179 | /* |
| 180 | * This is much more of a "Is my DRAM properly configured?" |
| 181 | * test than a "Is my DRAM faulty?" test. Not all bits |
| 182 | * are tested. -Tyson |
| 183 | */ |
Kyösti Mälkki | 481814d | 2011-10-28 16:15:47 +0300 | [diff] [blame] | 184 | #if !defined(__ROMCC__) |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 185 | printk(BIOS_DEBUG, "Testing DRAM at: %08lx\n", start); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 186 | #else |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 187 | print_debug("Testing DRAM at: "); |
Ronald G. Minnich | bfdc562 | 2004-03-22 04:24:29 +0000 | [diff] [blame] | 188 | print_debug_hex32(start); |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 189 | print_debug("\n"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 190 | #endif |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 191 | if (ram_bitset_nodie(start)) |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 192 | die("DRAM ERROR"); |
Kyösti Mälkki | 481814d | 2011-10-28 16:15:47 +0300 | [diff] [blame] | 193 | #if !defined(__ROMCC__) |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 194 | printk(BIOS_DEBUG, "Done.\n"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 195 | #else |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 196 | print_debug("Done.\n"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 197 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 200 | |
| 201 | int ram_check_nodie(unsigned long start, unsigned long stop) |
| 202 | { |
| 203 | int ret; |
| 204 | /* |
| 205 | * This is much more of a "Is my DRAM properly configured?" |
| 206 | * test than a "Is my DRAM faulty?" test. Not all bits |
| 207 | * are tested. -Tyson |
| 208 | */ |
| 209 | #if !defined(__ROMCC__) |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 210 | printk(BIOS_DEBUG, "Testing DRAM at : %08lx\n", start); |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 211 | #else |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 212 | print_debug("Testing DRAM at : "); |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 213 | print_debug_hex32(start); |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 214 | print_debug("\n"); |
| 215 | #endif |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 216 | |
Kyösti Mälkki | e77b9a0 | 2012-03-17 08:09:14 +0100 | [diff] [blame] | 217 | ret = ram_bitset_nodie(start); |
Sven Schnelle | 3ad8c54 | 2011-12-02 16:23:06 +0100 | [diff] [blame] | 218 | #if !defined(__ROMCC__) |
| 219 | printk(BIOS_DEBUG, "Done.\n"); |
| 220 | #else |
| 221 | print_debug("Done.\n"); |
| 222 | #endif |
| 223 | return ret; |
| 224 | } |
| 225 | |
Alexandru Gagniuc | 5239ba2 | 2013-06-08 11:32:36 -0500 | [diff] [blame] | 226 | int ram_check_noprint_nodie(unsigned long start, unsigned long stop) |
| 227 | { |
| 228 | unsigned long addr, value, value2; |
| 229 | unsigned short int idx; |
| 230 | unsigned char failed, failures; |
| 231 | |
| 232 | for (idx=0; idx<0x400; idx+=4) { |
| 233 | test_pattern(idx, &addr, &value); |
| 234 | write_phys(start + addr, value); |
| 235 | } |
| 236 | |
| 237 | /* Make sure we don't read before we wrote */ |
| 238 | phys_memory_barrier(); |
| 239 | |
| 240 | failures = 0; |
| 241 | for (idx=0; idx<0x400; idx+=4) { |
| 242 | test_pattern(idx, &addr, &value); |
| 243 | value2 = read_phys(start + addr); |
| 244 | |
| 245 | failed = (value2 != value); |
| 246 | failures |= failed; |
| 247 | } |
| 248 | return failures; |
| 249 | } |
| 250 | |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 251 | void quick_ram_check(void) |
| 252 | { |
| 253 | int fail = 0; |
| 254 | u32 backup; |
| 255 | backup = read_phys(CONFIG_RAMBASE); |
| 256 | write_phys(CONFIG_RAMBASE, 0x55555555); |
| 257 | phys_memory_barrier(); |
| 258 | if (read_phys(CONFIG_RAMBASE) != 0x55555555) |
| 259 | fail=1; |
| 260 | write_phys(CONFIG_RAMBASE, 0xaaaaaaaa); |
| 261 | phys_memory_barrier(); |
| 262 | if (read_phys(CONFIG_RAMBASE) != 0xaaaaaaaa) |
| 263 | fail=1; |
| 264 | write_phys(CONFIG_RAMBASE, 0x00000000); |
| 265 | phys_memory_barrier(); |
| 266 | if (read_phys(CONFIG_RAMBASE) != 0x00000000) |
| 267 | fail=1; |
| 268 | write_phys(CONFIG_RAMBASE, 0xffffffff); |
| 269 | phys_memory_barrier(); |
| 270 | if (read_phys(CONFIG_RAMBASE) != 0xffffffff) |
| 271 | fail=1; |
| 272 | |
| 273 | write_phys(CONFIG_RAMBASE, backup); |
| 274 | if (fail) { |
| 275 | post_code(0xea); |
| 276 | die("RAM INIT FAILURE!\n"); |
| 277 | } |
| 278 | phys_memory_barrier(); |
| 279 | } |