blob: 419d0eb75f4fe71eef512e6e3ca6c55dbf06570e [file] [log] [blame]
Patrick Georgid0835952010-10-05 09:07:10 +00001#include <stdint.h>
Myles Watson34261952010-03-19 02:33:40 +00002#include <lib.h> /* Prototypes */
Patrick Georgid0835952010-10-05 09:07:10 +00003#include <console/console.h>
Myles Watson34261952010-03-19 02:33:40 +00004
Stefan Reinauere0d607a2010-03-28 21:31:30 +00005static void write_phys(unsigned long addr, u32 value)
Eric Biederman8ca8d762003-04-22 19:02:15 +00006{
Stefan Reinauera7acc512010-02-25 13:40:49 +00007 // 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 Biederman8d9c1232003-06-17 08:42:17 +000010 asm volatile(
11 "movnti %1, (%0)"
12 : /* outputs */
13 : "r" (addr), "r" (value) /* inputs */
Stefan Reinauere0d607a2010-03-28 21:31:30 +000014#ifndef __GNUC__ /* GCC does not like empty clobbers? */
Eric Biederman8d9c1232003-06-17 08:42:17 +000015 : /* clobbers */
Stefan Reinauer76712932004-05-27 11:13:24 +000016#endif
Eric Biederman8d9c1232003-06-17 08:42:17 +000017 );
18#else
Eric Biederman52685572003-05-19 19:16:21 +000019 volatile unsigned long *ptr;
Eric Biederman8ca8d762003-04-22 19:02:15 +000020 ptr = (void *)addr;
21 *ptr = value;
Eric Biederman8d9c1232003-06-17 08:42:17 +000022#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +000023}
24
Stefan Reinauere0d607a2010-03-28 21:31:30 +000025static u32 read_phys(unsigned long addr)
Eric Biederman8ca8d762003-04-22 19:02:15 +000026{
Eric Biederman52685572003-05-19 19:16:21 +000027 volatile unsigned long *ptr;
Eric Biederman8ca8d762003-04-22 19:02:15 +000028 ptr = (void *)addr;
29 return *ptr;
30}
31
Stefan Reinauere0d607a2010-03-28 21:31:30 +000032static 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älkkie77b9a02012-03-17 08:09:14 +010050/**
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 Roth5f066b22015-01-04 16:47:39 -070055 * @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älkkie77b9a02012-03-17 08:09:14 +010058 */
59static inline void test_pattern(unsigned short int idx,
60 unsigned long *addr, unsigned long *value)
Eric Biederman8ca8d762003-04-22 19:02:15 +000061{
Kyösti Mälkkie77b9a02012-03-17 08:09:14 +010062 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 Biederman8ca8d762003-04-22 19:02:15 +000071}
72
Kyösti Mälkkie77b9a02012-03-17 08:09:14 +010073/**
74 * Simple write-read-verify memory test. See console debug output for
75 * any dislocated bytes.
76 *
Martin Roth5f066b22015-01-04 16:47:39 -070077 * @param start System memory offset, aligned to 128bytes
Kyösti Mälkkie77b9a02012-03-17 08:09:14 +010078 */
79static int ram_bitset_nodie(unsigned long start)
Eric Biederman8ca8d762003-04-22 19:02:15 +000080{
Kyösti Mälkkie77b9a02012-03-17 08:09:14 +010081 unsigned long addr, value, value2;
82 unsigned short int idx;
83 unsigned char failed, failures;
84 uint8_t verbose = 0;
85
Kyösti Mälkkie77b9a02012-03-17 08:09:14 +010086 printk(BIOS_DEBUG, "DRAM bitset write: 0x%08lx\n", start);
Lee Leahy35af5c42017-03-09 17:35:28 -080087 for (idx = 0; idx < 0x400; idx += 4) {
Kyösti Mälkkie77b9a02012-03-17 08:09:14 +010088 test_pattern(idx, &addr, &value);
89 write_phys(start + addr, value);
90 }
91
92 /* Make sure we don't read before we wrote */
93 phys_memory_barrier();
94
Kyösti Mälkkie77b9a02012-03-17 08:09:14 +010095 printk(BIOS_DEBUG, "DRAM bitset verify: 0x%08lx\n", start);
Kyösti Mälkkie77b9a02012-03-17 08:09:14 +010096 failures = 0;
Lee Leahy35af5c42017-03-09 17:35:28 -080097 for (idx = 0; idx < 0x400; idx += 4) {
Kyösti Mälkkie77b9a02012-03-17 08:09:14 +010098 test_pattern(idx, &addr, &value);
99 value2 = read_phys(start + addr);
100
101 failed = (value2 != value);
102 failures |= failed;
103 if (failed && !verbose) {
Kyösti Mälkkie77b9a02012-03-17 08:09:14 +0100104 printk(BIOS_ERR, "0x%08lx wr: 0x%08lx rd: 0x%08lx FAIL\n",
105 start + addr, value, value2);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000106 }
Kyösti Mälkkie77b9a02012-03-17 08:09:14 +0100107 if (verbose) {
Kyösti Mälkkie77b9a02012-03-17 08:09:14 +0100108 if ((addr & 0x0f) == 0)
109 printk(BIOS_DEBUG, "%08lx wr: %08lx rd:",
110 start + addr, value);
111 if (failed)
112 printk(BIOS_DEBUG, " %08lx!", value2);
113 else
114 printk(BIOS_DEBUG, " %08lx ", value2);
115 if ((addr & 0x0f) == 0xc)
116 printk(BIOS_DEBUG, "\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000117 }
118 }
Kyösti Mälkkie77b9a02012-03-17 08:09:14 +0100119 if (failures) {
120 post_code(0xea);
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000121 printk(BIOS_DEBUG, "\nDRAM did _NOT_ verify!\n");
Sven Schnelle3ad8c542011-12-02 16:23:06 +0100122 return 1;
Richard Smithffb7d8a2006-04-01 04:10:44 +0000123 }
Lee Leahy3e1cab42017-03-10 17:48:31 -0800124 printk(BIOS_DEBUG, "\nDRAM range verified.\n");
Sven Schnelle3ad8c542011-12-02 16:23:06 +0100125 return 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000126}
127
128
Eric Biederman8d9c1232003-06-17 08:42:17 +0000129void ram_check(unsigned long start, unsigned long stop)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000130{
Eric Biederman8ca8d762003-04-22 19:02:15 +0000131 /*
132 * This is much more of a "Is my DRAM properly configured?"
133 * test than a "Is my DRAM faulty?" test. Not all bits
134 * are tested. -Tyson
135 */
Kyösti Mälkkie77b9a02012-03-17 08:09:14 +0100136 printk(BIOS_DEBUG, "Testing DRAM at: %08lx\n", start);
Kyösti Mälkkie77b9a02012-03-17 08:09:14 +0100137 if (ram_bitset_nodie(start))
Sven Schnelle3ad8c542011-12-02 16:23:06 +0100138 die("DRAM ERROR");
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000139 printk(BIOS_DEBUG, "Done.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000140}
141
Sven Schnelle3ad8c542011-12-02 16:23:06 +0100142
143int ram_check_nodie(unsigned long start, unsigned long stop)
144{
145 int ret;
146 /*
147 * This is much more of a "Is my DRAM properly configured?"
148 * test than a "Is my DRAM faulty?" test. Not all bits
149 * are tested. -Tyson
150 */
Kyösti Mälkkie77b9a02012-03-17 08:09:14 +0100151 printk(BIOS_DEBUG, "Testing DRAM at : %08lx\n", start);
Sven Schnelle3ad8c542011-12-02 16:23:06 +0100152
Kyösti Mälkkie77b9a02012-03-17 08:09:14 +0100153 ret = ram_bitset_nodie(start);
Sven Schnelle3ad8c542011-12-02 16:23:06 +0100154 printk(BIOS_DEBUG, "Done.\n");
Sven Schnelle3ad8c542011-12-02 16:23:06 +0100155 return ret;
156}
157
Alexandru Gagniuc5239ba22013-06-08 11:32:36 -0500158int ram_check_noprint_nodie(unsigned long start, unsigned long stop)
159{
160 unsigned long addr, value, value2;
161 unsigned short int idx;
162 unsigned char failed, failures;
163
Lee Leahy35af5c42017-03-09 17:35:28 -0800164 for (idx = 0; idx < 0x400; idx += 4) {
Alexandru Gagniuc5239ba22013-06-08 11:32:36 -0500165 test_pattern(idx, &addr, &value);
166 write_phys(start + addr, value);
167 }
168
169 /* Make sure we don't read before we wrote */
170 phys_memory_barrier();
171
172 failures = 0;
Lee Leahy35af5c42017-03-09 17:35:28 -0800173 for (idx = 0; idx < 0x400; idx += 4) {
Alexandru Gagniuc5239ba22013-06-08 11:32:36 -0500174 test_pattern(idx, &addr, &value);
175 value2 = read_phys(start + addr);
176
177 failed = (value2 != value);
178 failures |= failed;
179 }
180 return failures;
181}
182
Kyösti Mälkki19652e62016-06-17 23:31:42 +0300183static void __quick_ram_check(uintptr_t dst)
Stefan Reinauere0d607a2010-03-28 21:31:30 +0000184{
185 int fail = 0;
186 u32 backup;
Kyösti Mälkki19652e62016-06-17 23:31:42 +0300187 backup = read_phys(dst);
188 write_phys(dst, 0x55555555);
Stefan Reinauere0d607a2010-03-28 21:31:30 +0000189 phys_memory_barrier();
Kyösti Mälkki19652e62016-06-17 23:31:42 +0300190 if (read_phys(dst) != 0x55555555)
Lee Leahy35af5c42017-03-09 17:35:28 -0800191 fail = 1;
Kyösti Mälkki19652e62016-06-17 23:31:42 +0300192 write_phys(dst, 0xaaaaaaaa);
Stefan Reinauere0d607a2010-03-28 21:31:30 +0000193 phys_memory_barrier();
Kyösti Mälkki19652e62016-06-17 23:31:42 +0300194 if (read_phys(dst) != 0xaaaaaaaa)
Lee Leahy35af5c42017-03-09 17:35:28 -0800195 fail = 1;
Kyösti Mälkki19652e62016-06-17 23:31:42 +0300196 write_phys(dst, 0x00000000);
Stefan Reinauere0d607a2010-03-28 21:31:30 +0000197 phys_memory_barrier();
Kyösti Mälkki19652e62016-06-17 23:31:42 +0300198 if (read_phys(dst) != 0x00000000)
Lee Leahy35af5c42017-03-09 17:35:28 -0800199 fail = 1;
Kyösti Mälkki19652e62016-06-17 23:31:42 +0300200 write_phys(dst, 0xffffffff);
Stefan Reinauere0d607a2010-03-28 21:31:30 +0000201 phys_memory_barrier();
Kyösti Mälkki19652e62016-06-17 23:31:42 +0300202 if (read_phys(dst) != 0xffffffff)
Lee Leahy35af5c42017-03-09 17:35:28 -0800203 fail = 1;
Stefan Reinauere0d607a2010-03-28 21:31:30 +0000204
Kyösti Mälkki19652e62016-06-17 23:31:42 +0300205 write_phys(dst, backup);
Stefan Reinauere0d607a2010-03-28 21:31:30 +0000206 if (fail) {
207 post_code(0xea);
208 die("RAM INIT FAILURE!\n");
209 }
210 phys_memory_barrier();
211}
Kyösti Mälkki19652e62016-06-17 23:31:42 +0300212
213void quick_ram_check(void)
214{
215 __quick_ram_check(0x100000);
216}