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 | |
Eric Biederman | 8d9c123 | 2003-06-17 08:42:17 +0000 | [diff] [blame] | 50 | static void ram_fill(unsigned long start, unsigned long stop) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 51 | { |
| 52 | unsigned long addr; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 53 | /* |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 54 | * Fill. |
| 55 | */ |
Kyösti Mälkki | 481814d | 2011-10-28 16:15:47 +0300 | [diff] [blame^] | 56 | #if !defined(__ROMCC__) |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 57 | printk(BIOS_DEBUG, "DRAM fill: 0x%08lx-0x%08lx\n", start, stop); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 58 | #else |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 59 | print_debug("DRAM fill: "); |
| 60 | print_debug_hex32(start); |
| 61 | print_debug("-"); |
| 62 | print_debug_hex32(stop); |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 63 | print_debug("\n"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 64 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 65 | for(addr = start; addr < stop ; addr += 4) { |
| 66 | /* Display address being filled */ |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 67 | if (!(addr & 0xfffff)) { |
Kyösti Mälkki | 481814d | 2011-10-28 16:15:47 +0300 | [diff] [blame^] | 68 | #if !defined(__ROMCC__) |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 69 | printk(BIOS_DEBUG, "%08lx \r", addr); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 70 | #else |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 71 | print_debug_hex32(addr); |
Eric Biederman | 69afe28 | 2004-11-11 06:53:24 +0000 | [diff] [blame] | 72 | print_debug(" \r"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 73 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 74 | } |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 75 | write_phys(addr, (u32)addr); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 76 | }; |
| 77 | /* Display final address */ |
Kyösti Mälkki | 481814d | 2011-10-28 16:15:47 +0300 | [diff] [blame^] | 78 | #if !defined(__ROMCC__) |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 79 | printk(BIOS_DEBUG, "%08lx\nDRAM filled\n", addr); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 80 | #else |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 81 | print_debug_hex32(addr); |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 82 | print_debug("\nDRAM filled\n"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 83 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Eric Biederman | 8d9c123 | 2003-06-17 08:42:17 +0000 | [diff] [blame] | 86 | static void ram_verify(unsigned long start, unsigned long stop) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 87 | { |
| 88 | unsigned long addr; |
Eric Biederman | 69afe28 | 2004-11-11 06:53:24 +0000 | [diff] [blame] | 89 | int i = 0; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 90 | /* |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 91 | * Verify. |
| 92 | */ |
Kyösti Mälkki | 481814d | 2011-10-28 16:15:47 +0300 | [diff] [blame^] | 93 | #if !defined(__ROMCC__) |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 94 | printk(BIOS_DEBUG, "DRAM verify: 0x%08lx-0x%08lx\n", start, stop); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 95 | #else |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 96 | print_debug("DRAM verify: "); |
| 97 | print_debug_hex32(start); |
| 98 | print_debug_char('-'); |
| 99 | print_debug_hex32(stop); |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 100 | print_debug("\n"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 101 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 102 | for(addr = start; addr < stop ; addr += 4) { |
| 103 | unsigned long value; |
| 104 | /* Display address being tested */ |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 105 | if (!(addr & 0xfffff)) { |
Kyösti Mälkki | 481814d | 2011-10-28 16:15:47 +0300 | [diff] [blame^] | 106 | #if !defined(__ROMCC__) |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 107 | printk(BIOS_DEBUG, "%08lx \r", addr); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 108 | #else |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 109 | print_debug_hex32(addr); |
Eric Biederman | 69afe28 | 2004-11-11 06:53:24 +0000 | [diff] [blame] | 110 | print_debug(" \r"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 111 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 112 | } |
| 113 | value = read_phys(addr); |
| 114 | if (value != addr) { |
| 115 | /* Display address with error */ |
Kyösti Mälkki | 481814d | 2011-10-28 16:15:47 +0300 | [diff] [blame^] | 116 | #if !defined(__ROMCC__) |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 117 | printk(BIOS_ERR, "Fail: @0x%08lx Read value=0x%08lx\n", addr, value); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 118 | #else |
Richard Smith | ffb7d8a | 2006-04-01 04:10:44 +0000 | [diff] [blame] | 119 | print_err("Fail: @0x"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 120 | print_err_hex32(addr); |
Richard Smith | ffb7d8a | 2006-04-01 04:10:44 +0000 | [diff] [blame] | 121 | print_err(" Read value=0x"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 122 | print_err_hex32(value); |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 123 | print_err("\n"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 124 | #endif |
Eric Biederman | 69afe28 | 2004-11-11 06:53:24 +0000 | [diff] [blame] | 125 | i++; |
Richard Smith | ffb7d8a | 2006-04-01 04:10:44 +0000 | [diff] [blame] | 126 | if(i>256) { |
Kyösti Mälkki | 481814d | 2011-10-28 16:15:47 +0300 | [diff] [blame^] | 127 | #if !defined(__ROMCC__) |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 128 | printk(BIOS_DEBUG, "Aborting.\n"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 129 | #else |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 130 | print_debug("Aborting.\n"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 131 | #endif |
Richard Smith | ffb7d8a | 2006-04-01 04:10:44 +0000 | [diff] [blame] | 132 | break; |
| 133 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | /* Display final address */ |
Kyösti Mälkki | 481814d | 2011-10-28 16:15:47 +0300 | [diff] [blame^] | 137 | #if !defined(__ROMCC__) |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 138 | printk(BIOS_DEBUG, "%08lx", addr); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 139 | #else |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 140 | print_debug_hex32(addr); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 141 | #endif |
| 142 | |
Richard Smith | ffb7d8a | 2006-04-01 04:10:44 +0000 | [diff] [blame] | 143 | if (i) { |
Kyösti Mälkki | 481814d | 2011-10-28 16:15:47 +0300 | [diff] [blame^] | 144 | #if !defined(__ROMCC__) |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 145 | printk(BIOS_DEBUG, "\nDRAM did _NOT_ verify!\n"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 146 | #else |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 147 | print_debug("\nDRAM did _NOT_ verify!\n"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 148 | #endif |
| 149 | die("DRAM ERROR"); |
Richard Smith | ffb7d8a | 2006-04-01 04:10:44 +0000 | [diff] [blame] | 150 | } |
| 151 | else { |
Kyösti Mälkki | 481814d | 2011-10-28 16:15:47 +0300 | [diff] [blame^] | 152 | #if !defined(__ROMCC__) |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 153 | printk(BIOS_DEBUG, "\nDRAM range verified.\n"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 154 | #else |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 155 | print_debug("\nDRAM range verified.\n"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 156 | #endif |
Richard Smith | ffb7d8a | 2006-04-01 04:10:44 +0000 | [diff] [blame] | 157 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | |
Eric Biederman | 8d9c123 | 2003-06-17 08:42:17 +0000 | [diff] [blame] | 161 | void ram_check(unsigned long start, unsigned long stop) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 162 | { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 163 | /* |
| 164 | * This is much more of a "Is my DRAM properly configured?" |
| 165 | * test than a "Is my DRAM faulty?" test. Not all bits |
| 166 | * are tested. -Tyson |
| 167 | */ |
Kyösti Mälkki | 481814d | 2011-10-28 16:15:47 +0300 | [diff] [blame^] | 168 | #if !defined(__ROMCC__) |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 169 | printk(BIOS_DEBUG, "Testing DRAM : %08lx - %08lx\n", start, stop); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 170 | #else |
Ronald G. Minnich | bfdc562 | 2004-03-22 04:24:29 +0000 | [diff] [blame] | 171 | print_debug("Testing DRAM : "); |
| 172 | print_debug_hex32(start); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 173 | print_debug("-"); |
Ronald G. Minnich | bfdc562 | 2004-03-22 04:24:29 +0000 | [diff] [blame] | 174 | print_debug_hex32(stop); |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 175 | print_debug("\n"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 176 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 177 | ram_fill(start, stop); |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 178 | /* Make sure we don't read before we wrote */ |
| 179 | phys_memory_barrier(); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 180 | ram_verify(start, stop); |
Kyösti Mälkki | 481814d | 2011-10-28 16:15:47 +0300 | [diff] [blame^] | 181 | #if !defined(__ROMCC__) |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 182 | printk(BIOS_DEBUG, "Done.\n"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 183 | #else |
Stefan Reinauer | 64ed2b7 | 2010-03-31 14:47:43 +0000 | [diff] [blame] | 184 | print_debug("Done.\n"); |
Stefan Reinauer | 48b85fc | 2008-08-01 12:12:37 +0000 | [diff] [blame] | 185 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Stefan Reinauer | e0d607a | 2010-03-28 21:31:30 +0000 | [diff] [blame] | 188 | void quick_ram_check(void) |
| 189 | { |
| 190 | int fail = 0; |
| 191 | u32 backup; |
| 192 | backup = read_phys(CONFIG_RAMBASE); |
| 193 | write_phys(CONFIG_RAMBASE, 0x55555555); |
| 194 | phys_memory_barrier(); |
| 195 | if (read_phys(CONFIG_RAMBASE) != 0x55555555) |
| 196 | fail=1; |
| 197 | write_phys(CONFIG_RAMBASE, 0xaaaaaaaa); |
| 198 | phys_memory_barrier(); |
| 199 | if (read_phys(CONFIG_RAMBASE) != 0xaaaaaaaa) |
| 200 | fail=1; |
| 201 | write_phys(CONFIG_RAMBASE, 0x00000000); |
| 202 | phys_memory_barrier(); |
| 203 | if (read_phys(CONFIG_RAMBASE) != 0x00000000) |
| 204 | fail=1; |
| 205 | write_phys(CONFIG_RAMBASE, 0xffffffff); |
| 206 | phys_memory_barrier(); |
| 207 | if (read_phys(CONFIG_RAMBASE) != 0xffffffff) |
| 208 | fail=1; |
| 209 | |
| 210 | write_phys(CONFIG_RAMBASE, backup); |
| 211 | if (fail) { |
| 212 | post_code(0xea); |
| 213 | die("RAM INIT FAILURE!\n"); |
| 214 | } |
| 215 | phys_memory_barrier(); |
| 216 | } |
| 217 | |