arm start.c: Make runtime function address calculation tolerant for more compilers
[barebox-mini2440.git] / commands / memtest.c
blobd9c8b3d89e6311dca237f91b0a4e76bd410685fc
1 /*
2 * mtest - Perform a memory test
4 * (C) Copyright 2000
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * See file CREDITS for list of people who contributed to this
8 * project.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
26 #include <common.h>
27 #include <command.h>
28 #include <types.h>
31 * Perform a memory test. A more complete alternative test can be
32 * configured using CONFIG_CMD_MTEST_ALTERNATIVE. The complete test
33 * loops until interrupted by ctrl-c or by a failure of one of the
34 * sub-tests.
36 #ifdef CONFIG_CMD_MTEST_ALTERNATIVE
37 static int mem_test(ulong _start, ulong _end, ulong pattern_unused)
39 vu_long *start = (vu_long *)_start;
40 vu_long *end = (vu_long *)_end;
41 vu_long *addr;
42 ulong val;
43 ulong readback;
44 vu_long addr_mask;
45 vu_long offset;
46 vu_long test_offset;
47 vu_long pattern;
48 vu_long temp;
49 vu_long anti_pattern;
50 vu_long num_words;
51 #ifdef CFG_MEMTEST_SCRATCH
52 vu_long *dummy = (vu_long*)CFG_MEMTEST_SCRATCH;
53 #else
54 vu_long *dummy = start;
55 #endif
56 int j;
57 int iterations = 1;
59 static const ulong bitpattern[] = {
60 0x00000001, /* single bit */
61 0x00000003, /* two adjacent bits */
62 0x00000007, /* three adjacent bits */
63 0x0000000F, /* four adjacent bits */
64 0x00000005, /* two non-adjacent bits */
65 0x00000015, /* three non-adjacent bits */
66 0x00000055, /* four non-adjacent bits */
67 0xaaaaaaaa, /* alternating 1/0 */
70 /* XXX: enforce alignment of start and end? */
71 for (;;) {
72 if (ctrlc()) {
73 putchar ('\n');
74 return 1;
77 printf("Iteration: %6d\r", iterations);
78 iterations++;
81 * Data line test: write a pattern to the first
82 * location, write the 1's complement to a 'parking'
83 * address (changes the state of the data bus so a
84 * floating bus doen't give a false OK), and then
85 * read the value back. Note that we read it back
86 * into a variable because the next time we read it,
87 * it might be right (been there, tough to explain to
88 * the quality guys why it prints a failure when the
89 * "is" and "should be" are obviously the same in the
90 * error message).
92 * Rather than exhaustively testing, we test some
93 * patterns by shifting '1' bits through a field of
94 * '0's and '0' bits through a field of '1's (i.e.
95 * pattern and ~pattern).
97 addr = start;
98 /* XXX */
99 if (addr == dummy) ++addr;
100 for (j = 0; j < sizeof(bitpattern)/sizeof(bitpattern[0]); j++) {
101 val = bitpattern[j];
102 for(; val != 0; val <<= 1) {
103 *addr = val;
104 *dummy = ~val; /* clear the test data off of the bus */
105 readback = *addr;
106 if(readback != val) {
107 printf ("FAILURE (data line): "
108 "expected 0x%08lx, actual 0x%08lx at address 0x%p\n",
109 val, readback, addr);
111 *addr = ~val;
112 *dummy = val;
113 readback = *addr;
114 if(readback != ~val) {
115 printf ("FAILURE (data line): "
116 "Is 0x%08lx, should be 0x%08lx at address 0x%p\n",
117 readback, ~val, addr);
123 * Based on code whose Original Author and Copyright
124 * information follows: Copyright (c) 1998 by Michael
125 * Barr. This software is placed into the public
126 * domain and may be used for any purpose. However,
127 * this notice must not be changed or removed and no
128 * warranty is either expressed or implied by its
129 * publication or distribution.
133 * Address line test
135 * Description: Test the address bus wiring in a
136 * memory region by performing a walking
137 * 1's test on the relevant bits of the
138 * address and checking for aliasing.
139 * This test will find single-bit
140 * address failures such as stuck -high,
141 * stuck-low, and shorted pins. The base
142 * address and size of the region are
143 * selected by the caller.
145 * Notes: For best results, the selected base
146 * address should have enough LSB 0's to
147 * guarantee single address bit changes.
148 * For example, to test a 64-Kbyte
149 * region, select a base address on a
150 * 64-Kbyte boundary. Also, select the
151 * region size as a power-of-two if at
152 * all possible.
154 * Returns: 0 if the test succeeds, 1 if the test fails.
156 * ## NOTE ## Be sure to specify start and end
157 * addresses such that addr_mask has
158 * lots of bits set. For example an
159 * address range of 01000000 02000000 is
160 * bad while a range of 01000000
161 * 01ffffff is perfect.
163 addr_mask = ((ulong)end - (ulong)start)/sizeof(vu_long);
164 pattern = (vu_long) 0xaaaaaaaa;
165 anti_pattern = (vu_long) 0x55555555;
167 debug("%s:%d: addr mask = 0x%.8lx\n",
168 __FUNCTION__, __LINE__,
169 addr_mask);
171 * Write the default pattern at each of the
172 * power-of-two offsets.
174 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1)
175 start[offset] = pattern;
178 * Check for address bits stuck high.
180 test_offset = 0;
181 start[test_offset] = anti_pattern;
183 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
184 temp = start[offset];
185 if (temp != pattern) {
186 printf ("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
187 " expected 0x%.8lx, actual 0x%.8lx\n",
188 (ulong)&start[offset], pattern, temp);
189 return 1;
192 start[test_offset] = pattern;
195 * Check for addr bits stuck low or shorted.
197 for (test_offset = 1; (test_offset & addr_mask) != 0; test_offset <<= 1) {
198 start[test_offset] = anti_pattern;
200 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
201 temp = start[offset];
202 if ((temp != pattern) && (offset != test_offset)) {
203 printf ("\nFAILURE: Address bit stuck low or shorted @"
204 " 0x%.8lx: expected 0x%.8lx, actual 0x%.8lx\n",
205 (ulong)&start[offset], pattern, temp);
206 return 1;
209 start[test_offset] = pattern;
213 * Description: Test the integrity of a physical
214 * memory device by performing an
215 * increment/decrement test over the
216 * entire region. In the process every
217 * storage bit in the device is tested
218 * as a zero and a one. The base address
219 * and the size of the region are
220 * selected by the caller.
222 * Returns: 0 if the test succeeds, 1 if the test fails.
224 num_words = ((ulong)end - (ulong)start)/sizeof(vu_long) + 1;
227 * Fill memory with a known pattern.
229 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
230 start[offset] = pattern;
234 * Check each location and invert it for the second pass.
236 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
237 temp = start[offset];
238 if (temp != pattern) {
239 printf ("\nFAILURE (read/write) @ 0x%.8lx:"
240 " expected 0x%.8lx, actual 0x%.8lx)\n",
241 (ulong)&start[offset], pattern, temp);
242 return 1;
245 anti_pattern = ~pattern;
246 start[offset] = anti_pattern;
250 * Check each location for the inverted pattern and zero it.
252 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
253 anti_pattern = ~pattern;
254 temp = start[offset];
255 if (temp != anti_pattern) {
256 printf ("\nFAILURE (read/write): @ 0x%.8lx:"
257 " expected 0x%.8lx, actual 0x%.8lx)\n",
258 (ulong)&start[offset], anti_pattern, temp);
259 return 1;
261 start[offset] = 0;
266 #else
267 static int mem_test(ulong _start, ulong _end, ulong pattern)
269 vu_long *addr;
270 vu_long *start = (vu_long *)_start;
271 vu_long *end = (vu_long *)_end;
272 ulong val;
273 ulong readback;
274 ulong incr;
275 int rcode;
277 incr = 1;
278 for (;;) {
279 if (ctrlc()) {
280 putchar('\n');
281 return 1;
284 printf ("\rPattern 0x%08lX Writing..."
285 "%12s"
286 "\b\b\b\b\b\b\b\b\b\b",
287 pattern, "");
289 for (addr=start,val=pattern; addr<end; addr++) {
290 *addr = val;
291 val += incr;
294 puts ("Reading...");
296 for (addr=start,val=pattern; addr<end; addr++) {
297 readback = *addr;
298 if (readback != val) {
299 printf ("\nMem error @ 0x%08X: "
300 "found 0x%08lX, expected 0x%08lX\n",
301 (uint)addr, readback, val);
302 rcode = 1;
304 val += incr;
308 * Flip the pattern each time to make lots of zeros and
309 * then, the next time, lots of ones. We decrement
310 * the "negative" patterns and increment the "positive"
311 * patterns to preserve this feature.
313 if(pattern & 0x80000000) {
314 pattern = -pattern; /* complement & increment */
316 else {
317 pattern = ~pattern;
319 incr = -incr;
321 return rcode;
323 #endif
325 static int do_mem_mtest(struct command *cmdtp, int argc, char *argv[])
327 ulong start, end, pattern = 0;
329 if (argc < 3)
330 return COMMAND_ERROR_USAGE;
332 start = simple_strtoul(argv[1], NULL, 0);
333 end = simple_strtoul(argv[2], NULL, 0);
335 if (argc > 3)
336 pattern = simple_strtoul(argv[3], NULL, 0);
338 printf ("Testing 0x%08x ... 0x%08x:\n", (uint)start, (uint)end);
340 return mem_test(start, end, pattern);
343 static const __maybe_unused char cmd_mtest_help[] =
344 "Usage: <start> <end> "
345 #ifdef CONFIG_CMD_MTEST_ALTERNATIVE
346 "[pattern]"
347 #endif
348 "\nsimple RAM read/write test\n";
350 BAREBOX_CMD_START(mtest)
351 .cmd = do_mem_mtest,
352 .usage = "simple RAM test",
353 BAREBOX_CMD_HELP(cmd_mtest_help)
354 BAREBOX_CMD_END