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