blob: a29fa25ece2bb7bcaf2dbd1fc8c23a7077023c0e [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);
Kyösti Mälkkie77b9a02012-03-17 08:09:14 +010087 for (idx=0; idx<0x400; idx+=4) {
88 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;
97 for (idx=0; idx<0x400; idx+=4) {
98 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;
Stefan Reinauerd6865222015-01-05 13:12:38 -0800123 } else {
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000124 printk(BIOS_DEBUG, "\nDRAM range verified.\n");
Richard Smithffb7d8a2006-04-01 04:10:44 +0000125 }
Sven Schnelle3ad8c542011-12-02 16:23:06 +0100126 return 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000127}
128
129
Eric Biederman8d9c1232003-06-17 08:42:17 +0000130void ram_check(unsigned long start, unsigned long stop)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000131{
Eric Biederman8ca8d762003-04-22 19:02:15 +0000132 /*
133 * This is much more of a "Is my DRAM properly configured?"
134 * test than a "Is my DRAM faulty?" test. Not all bits
135 * are tested. -Tyson
136 */
Kyösti Mälkkie77b9a02012-03-17 08:09:14 +0100137 printk(BIOS_DEBUG, "Testing DRAM at: %08lx\n", start);
Kyösti Mälkkie77b9a02012-03-17 08:09:14 +0100138 if (ram_bitset_nodie(start))
Sven Schnelle3ad8c542011-12-02 16:23:06 +0100139 die("DRAM ERROR");
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000140 printk(BIOS_DEBUG, "Done.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000141}
142
Sven Schnelle3ad8c542011-12-02 16:23:06 +0100143
144int ram_check_nodie(unsigned long start, unsigned long stop)
145{
146 int ret;
147 /*
148 * This is much more of a "Is my DRAM properly configured?"
149 * test than a "Is my DRAM faulty?" test. Not all bits
150 * are tested. -Tyson
151 */
Kyösti Mälkkie77b9a02012-03-17 08:09:14 +0100152 printk(BIOS_DEBUG, "Testing DRAM at : %08lx\n", start);
Sven Schnelle3ad8c542011-12-02 16:23:06 +0100153
Kyösti Mälkkie77b9a02012-03-17 08:09:14 +0100154 ret = ram_bitset_nodie(start);
Sven Schnelle3ad8c542011-12-02 16:23:06 +0100155 printk(BIOS_DEBUG, "Done.\n");
Sven Schnelle3ad8c542011-12-02 16:23:06 +0100156 return ret;
157}
158
Alexandru Gagniuc5239ba22013-06-08 11:32:36 -0500159int ram_check_noprint_nodie(unsigned long start, unsigned long stop)
160{
161 unsigned long addr, value, value2;
162 unsigned short int idx;
163 unsigned char failed, failures;
164
165 for (idx=0; idx<0x400; idx+=4) {
166 test_pattern(idx, &addr, &value);
167 write_phys(start + addr, value);
168 }
169
170 /* Make sure we don't read before we wrote */
171 phys_memory_barrier();
172
173 failures = 0;
174 for (idx=0; idx<0x400; idx+=4) {
175 test_pattern(idx, &addr, &value);
176 value2 = read_phys(start + addr);
177
178 failed = (value2 != value);
179 failures |= failed;
180 }
181 return failures;
182}
183
Stefan Reinauere0d607a2010-03-28 21:31:30 +0000184void quick_ram_check(void)
185{
186 int fail = 0;
187 u32 backup;
188 backup = read_phys(CONFIG_RAMBASE);
189 write_phys(CONFIG_RAMBASE, 0x55555555);
190 phys_memory_barrier();
191 if (read_phys(CONFIG_RAMBASE) != 0x55555555)
192 fail=1;
193 write_phys(CONFIG_RAMBASE, 0xaaaaaaaa);
194 phys_memory_barrier();
195 if (read_phys(CONFIG_RAMBASE) != 0xaaaaaaaa)
196 fail=1;
197 write_phys(CONFIG_RAMBASE, 0x00000000);
198 phys_memory_barrier();
199 if (read_phys(CONFIG_RAMBASE) != 0x00000000)
200 fail=1;
201 write_phys(CONFIG_RAMBASE, 0xffffffff);
202 phys_memory_barrier();
203 if (read_phys(CONFIG_RAMBASE) != 0xffffffff)
204 fail=1;
205
206 write_phys(CONFIG_RAMBASE, backup);
207 if (fail) {
208 post_code(0xea);
209 die("RAM INIT FAILURE!\n");
210 }
211 phys_memory_barrier();
212}