MAINTAINERS: Add myself as maintainer for the getac/p470
[coreboot.git] / src / lib / ramtest.c
blob461a028b11dd343e00e6ce55e927b888e71ff6f2
1 #include <stdint.h>
2 #include <lib.h> /* Prototypes */
3 #include <console/console.h>
5 static void write_phys(unsigned long addr, u32 value)
7 // Assembler in lib/ is very ugly. But we properly guarded
8 // it so let's obey this one for now
9 #if IS_ENABLED(CONFIG_SSE2)
10 asm volatile(
11 "movnti %1, (%0)"
12 : /* outputs */
13 : "r" (addr), "r" (value) /* inputs */
14 #ifndef __GNUC__ /* GCC does not like empty clobbers? */
15 : /* clobbers */
16 #endif
18 #else
19 volatile unsigned long *ptr;
20 ptr = (void *)addr;
21 *ptr = value;
22 #endif
25 static u32 read_phys(unsigned long addr)
27 volatile unsigned long *ptr;
28 ptr = (void *)addr;
29 return *ptr;
32 static void phys_memory_barrier(void)
34 #if IS_ENABLED(CONFIG_SSE2)
35 // Needed for movnti
36 asm volatile (
37 "sfence"
39 #ifdef __GNUC__ /* ROMCC does not like memory clobbers */
40 : "memory"
41 #endif
43 #else
44 #ifdef __GNUC__ /* ROMCC does not like empty asm statements */
45 asm volatile ("" ::: "memory");
46 #endif
47 #endif
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.
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
59 static inline void test_pattern(unsigned short int idx,
60 unsigned long *addr, unsigned long *value)
62 uint8_t j, k;
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);
73 /**
74 * Simple write-read-verify memory test. See console debug output for
75 * any dislocated bytes.
77 * Tests 1MiB of memory starting from start.
79 * @param start System memory offset, aligned to 128bytes
81 static int ram_bitset_nodie(unsigned long start)
83 unsigned long addr, value, value2;
84 unsigned short int idx;
85 unsigned char failed, failures;
86 uint8_t verbose = 0;
88 printk(BIOS_DEBUG, "DRAM bitset write: 0x%08lx\n", start);
89 for (idx = 0; idx < 0x400; idx += 4) {
90 test_pattern(idx, &addr, &value);
91 write_phys(start + addr, value);
94 /* Make sure we don't read before we wrote */
95 phys_memory_barrier();
97 printk(BIOS_DEBUG, "DRAM bitset verify: 0x%08lx\n", start);
98 failures = 0;
99 for (idx = 0; idx < 0x400; idx += 4) {
100 test_pattern(idx, &addr, &value);
101 value2 = read_phys(start + addr);
103 failed = (value2 != value);
104 failures |= failed;
105 if (failed && !verbose) {
106 printk(BIOS_ERR, "0x%08lx wr: 0x%08lx rd: 0x%08lx FAIL\n",
107 start + addr, value, value2);
109 if (verbose) {
110 if ((addr & 0x0f) == 0)
111 printk(BIOS_DEBUG, "%08lx wr: %08lx rd:",
112 start + addr, value);
113 if (failed)
114 printk(BIOS_DEBUG, " %08lx!", value2);
115 else
116 printk(BIOS_DEBUG, " %08lx ", value2);
117 if ((addr & 0x0f) == 0xc)
118 printk(BIOS_DEBUG, "\n");
121 if (failures) {
122 post_code(0xea);
123 printk(BIOS_DEBUG, "\nDRAM did _NOT_ verify!\n");
124 return 1;
126 printk(BIOS_DEBUG, "\nDRAM range verified.\n");
127 return 0;
131 void ram_check(unsigned long start, unsigned long stop)
134 * This is much more of a "Is my DRAM properly configured?"
135 * test than a "Is my DRAM faulty?" test. Not all bits
136 * are tested. -Tyson
138 printk(BIOS_DEBUG, "Testing DRAM at: %08lx\n", start);
139 if (ram_bitset_nodie(start))
140 die("DRAM ERROR");
141 printk(BIOS_DEBUG, "Done.\n");
145 int ram_check_nodie(unsigned long start, unsigned long stop)
147 int ret;
149 * This is much more of a "Is my DRAM properly configured?"
150 * test than a "Is my DRAM faulty?" test. Not all bits
151 * are tested. -Tyson
153 printk(BIOS_DEBUG, "Testing DRAM at : %08lx\n", start);
155 ret = ram_bitset_nodie(start);
156 printk(BIOS_DEBUG, "Done.\n");
157 return ret;
160 int ram_check_noprint_nodie(unsigned long start, unsigned long stop)
162 unsigned long addr, value, value2;
163 unsigned short int idx;
164 unsigned char failed, failures;
166 for (idx = 0; idx < 0x400; idx += 4) {
167 test_pattern(idx, &addr, &value);
168 write_phys(start + addr, value);
171 /* Make sure we don't read before we wrote */
172 phys_memory_barrier();
174 failures = 0;
175 for (idx = 0; idx < 0x400; idx += 4) {
176 test_pattern(idx, &addr, &value);
177 value2 = read_phys(start + addr);
179 failed = (value2 != value);
180 failures |= failed;
182 return failures;
185 static void __quick_ram_check(uintptr_t dst)
187 int fail = 0;
188 u32 backup;
189 backup = read_phys(dst);
190 write_phys(dst, 0x55555555);
191 phys_memory_barrier();
192 if (read_phys(dst) != 0x55555555)
193 fail = 1;
194 write_phys(dst, 0xaaaaaaaa);
195 phys_memory_barrier();
196 if (read_phys(dst) != 0xaaaaaaaa)
197 fail = 1;
198 write_phys(dst, 0x00000000);
199 phys_memory_barrier();
200 if (read_phys(dst) != 0x00000000)
201 fail = 1;
202 write_phys(dst, 0xffffffff);
203 phys_memory_barrier();
204 if (read_phys(dst) != 0xffffffff)
205 fail = 1;
207 write_phys(dst, backup);
208 if (fail) {
209 post_code(0xea);
210 die("RAM INIT FAILURE!\n");
212 phys_memory_barrier();
215 void quick_ram_check(void)
217 __quick_ram_check(0x100000);