]>
Commit | Line | Data |
---|---|---|
72f58822 L |
1 | /* |
2 | * (C) Copyright 2000 | |
3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. | |
4 | * | |
5 | * SPDX-License-Identifier: GPL-2.0+ | |
6 | */ | |
7 | ||
8 | /* | |
9 | * Memory Functions | |
10 | * | |
11 | * Copied from FADS ROM, Dan Malek (dmalek@jlc.net) | |
12 | */ | |
13 | ||
14 | #include "common.h" | |
15 | #include <stdlib.h> | |
16 | #include <ctype.h> | |
8f23e84c | 17 | #include <avr/pgmspace.h> |
72f58822 | 18 | |
72f58822 L |
19 | #include "command.h" |
20 | #include "cli_readline.h" | |
8f23e84c | 21 | #include "print-utils.h" |
72f58822 L |
22 | #include "con-utils.h" |
23 | #include "z80-if.h" | |
8f23e84c | 24 | //#include "debug.h" |
72f58822 | 25 | |
72f58822 L |
26 | |
27 | #ifndef CONFIG_SYS_MEMTEST_SCRATCH | |
28 | #define CONFIG_SYS_MEMTEST_SCRATCH 0 | |
29 | #endif | |
30 | ||
72f58822 L |
31 | /* Display values from last command. |
32 | * Memory modify remembered values are different from display memory. | |
33 | */ | |
34 | static uint32_t dp_last_addr; | |
35 | static uint32_t dp_last_length = 0x100; | |
36 | static uint32_t mm_last_addr; | |
37 | ||
38 | static uint32_t base_address = 0; | |
39 | ||
40 | /*--------------------------------------------------------------------------*/ | |
41 | ||
72f58822 L |
42 | int z180_dump_mem(uint32_t startaddr, uint32_t len, const char *title) |
43 | { | |
44 | uint8_t buf[16]; | |
45 | uint8_t llen = 16; | |
46 | uint8_t pre = startaddr % 16; | |
47 | uint32_t addr = startaddr & ~0x0f; | |
48 | len += pre; | |
49 | uint8_t i; | |
6035a17b | 50 | |
72f58822 L |
51 | if (title && *title) |
52 | printf_P(PSTR("%s\n"),title); | |
6035a17b | 53 | |
72f58822 L |
54 | while (len) { |
55 | if (len < 16) | |
56 | llen = len; | |
57 | ||
62f624d3 | 58 | z80_bus_cmd(Request); |
72f58822 L |
59 | for (i = pre; i < llen; i++) |
60 | buf[i] = z80_read(addr + i); | |
62f624d3 | 61 | z80_bus_cmd(Release); |
72f58822 L |
62 | |
63 | printf_P(PSTR("%.5lx:"), addr); | |
64 | #if 0 | |
65 | print_blanks(3 * pre); | |
66 | ||
67 | /* Print hex values */ | |
68 | for (i = pre; i < llen; i++) | |
69 | printf_P(PSTR(" %.2x"), buf[i]); | |
70 | #else | |
71 | for (i = 0; i < llen; i++) { | |
72 | if ((i % 8) == 0) | |
73 | putchar(' '); | |
74 | if (i < pre) | |
75 | printf_P(PSTR(".. ")); | |
76 | else | |
77 | printf_P(PSTR("%.2x "), buf[i]); | |
78 | } | |
79 | #endif | |
80 | /* fill line with whitespace for nice ASCII print */ | |
81 | #if 1 | |
82 | print_blanks(3 * (16u - i) + (16u-i)/8 + 1 + pre); | |
83 | #else | |
84 | ||
85 | #endif | |
86 | /* Print data in ASCII characters */ | |
87 | for (i = pre; i < llen; i++) | |
88 | printf_P(PSTR("%c"), isprint(buf[i]) ? buf[i] : '.'); | |
89 | putchar('\n'); | |
90 | ||
91 | pre = 0; | |
92 | addr += 16; | |
93 | len -= llen; | |
94 | ||
95 | if (ctrlc()) | |
96 | return -1; | |
97 | } | |
98 | return 0; | |
99 | } | |
100 | ||
101 | ||
102 | /*--------------------------------------------------------------------------*/ | |
103 | ||
104 | /* Memory Display | |
105 | * | |
106 | * Syntax: | |
107 | * md {addr} {len} | |
108 | */ | |
d0581f88 | 109 | command_ret_t do_mem_md(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
72f58822 L |
110 | { |
111 | uint32_t addr, length; | |
6035a17b | 112 | |
72f58822 L |
113 | (void) cmdtp; |
114 | ||
6035a17b | 115 | #if 0 |
72f58822 L |
116 | printf_P(PSTR("flag: %d, argc: %d"), flag, argc); |
117 | for (int i = 0; i < argc; i++) { | |
118 | printf_P(PSTR(", argv[%d]: %s"), i, argv[i] ? argv[i] : "<NULL>"); | |
119 | } | |
120 | putchar('\n'); | |
121 | #endif | |
122 | ||
123 | /* We use the last specified parameters, unless new ones are | |
124 | * entered. | |
125 | */ | |
126 | addr = dp_last_addr; | |
127 | length = dp_last_length; | |
128 | ||
129 | if (argc < 2) | |
130 | return CMD_RET_USAGE; | |
131 | ||
132 | if ((flag & CMD_FLAG_REPEAT) == 0) { | |
133 | /* Address is specified since argc > 1 */ | |
134 | addr = strtoul(argv[1], NULL, 16); | |
135 | addr += base_address; | |
136 | ||
137 | /* If another parameter, it is the length to display. */ | |
138 | if (argc > 2) | |
139 | length = strtoul(argv[2], NULL, 16); | |
140 | } | |
141 | ||
142 | /* Print the lines. */ | |
143 | z180_dump_mem(addr, length, NULL); | |
144 | ||
145 | dp_last_addr = addr + length; | |
146 | dp_last_length = length; | |
d0581f88 | 147 | return CMD_RET_SUCCESS; |
72f58822 L |
148 | } |
149 | ||
d0581f88 L |
150 | /* Modify memory. |
151 | * | |
152 | * Syntax: | |
153 | * mm {addr} | |
154 | * nm {addr} | |
155 | */ | |
156 | static command_ret_t | |
157 | mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[]) | |
158 | { | |
159 | uint32_t addr; | |
160 | uint8_t data; | |
161 | int nbytes; | |
162 | ||
163 | (void) cmdtp; | |
164 | ||
165 | if (argc != 2) | |
166 | return CMD_RET_USAGE; | |
167 | ||
168 | /* We use the last specified parameters, unless new ones are | |
169 | * entered. | |
170 | */ | |
171 | addr = mm_last_addr; | |
172 | ||
173 | if ((flag & CMD_FLAG_REPEAT) == 0) { | |
174 | /* New command specified. | |
175 | */ | |
176 | ||
177 | /* Address is specified since argc > 1 | |
178 | */ | |
179 | addr = strtoul(argv[1], NULL, 16); | |
180 | addr += base_address; | |
181 | } | |
182 | ||
183 | /* Print the address, followed by value. Then accept input for | |
184 | * the next value. A non-converted value exits. | |
185 | */ | |
186 | do { | |
187 | z80_bus_cmd(Request); | |
188 | data = z80_read(addr); | |
69988dc1 | 189 | printf_P(PSTR("%05lx: %02x"), addr, data); |
d0581f88 L |
190 | z80_bus_cmd(Release); |
191 | ||
192 | nbytes = cli_readline(PSTR(" ? ")); | |
193 | if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) { | |
194 | /* <CR> pressed as only input, don't modify current | |
195 | * location and move to next. "-" pressed will go back. | |
196 | */ | |
197 | if (incrflag) | |
198 | addr += nbytes ? -1 : 1; | |
199 | nbytes = 1; | |
200 | } | |
201 | else { | |
202 | char *endp; | |
203 | data = strtoul(console_buffer, &endp, 16); | |
204 | nbytes = endp - console_buffer; | |
205 | if (nbytes) { | |
206 | z80_bus_cmd(Request); | |
207 | z80_write(addr, data); | |
208 | z80_bus_cmd(Release); | |
209 | if (incrflag) | |
210 | addr++; | |
211 | } | |
212 | } | |
213 | } while (nbytes); | |
214 | ||
215 | mm_last_addr = addr; | |
216 | return CMD_RET_SUCCESS; | |
217 | } | |
218 | ||
219 | ||
220 | command_ret_t do_mem_mm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
72f58822 L |
221 | { |
222 | return mod_mem (cmdtp, 1, flag, argc, argv); | |
223 | } | |
d0581f88 | 224 | command_ret_t do_mem_nm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
72f58822 L |
225 | { |
226 | return mod_mem (cmdtp, 0, flag, argc, argv); | |
227 | } | |
228 | ||
d0581f88 | 229 | command_ret_t do_mem_mw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
72f58822 L |
230 | { |
231 | uint8_t writeval; | |
232 | uint32_t addr, count; | |
233 | ||
234 | (void) cmdtp; | |
235 | (void) flag; | |
236 | ||
237 | if ((argc < 3) || (argc > 4)) | |
238 | return CMD_RET_USAGE; | |
239 | ||
240 | /* Address is specified since argc > 1 | |
241 | */ | |
242 | addr = strtoul(argv[1], NULL, 16); | |
243 | addr += base_address; | |
244 | ||
245 | /* Get the value to write. | |
246 | */ | |
247 | writeval = (uint8_t) strtoul(argv[2], NULL, 16); | |
248 | ||
249 | /* Count ? */ | |
250 | if (argc == 4) { | |
251 | count = strtoul(argv[3], NULL, 16); | |
252 | } else { | |
253 | count = 1; | |
254 | } | |
255 | ||
62f624d3 | 256 | z80_bus_cmd(Request); |
72f58822 L |
257 | while (count-- > 0) { |
258 | z80_write(addr, writeval); | |
6035a17b | 259 | ++addr; |
72f58822 | 260 | } |
62f624d3 | 261 | z80_bus_cmd(Release); |
72f58822 | 262 | |
d0581f88 | 263 | return CMD_RET_SUCCESS; |
72f58822 L |
264 | } |
265 | ||
266 | #ifdef CONFIG_MX_CYCLIC | |
d0581f88 | 267 | command_ret_t do_mem_mdc ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
72f58822 L |
268 | { |
269 | int i; | |
270 | uint32_t count; | |
271 | ||
272 | if (argc < 4) | |
273 | return CMD_RET_USAGE; | |
274 | ||
275 | count = strtoul(argv[3], NULL, 10); | |
276 | ||
277 | for (;;) { | |
278 | do_mem_md (NULL, 0, 3, argv); | |
279 | ||
280 | /* delay for <count> ms... */ | |
281 | /* TODO: use timer */ | |
282 | for (i=0; i<count; i++) | |
283 | udelay (1000); | |
284 | ||
285 | /* check for ctrl-c to abort... */ | |
286 | if (ctrlc()) { | |
69988dc1 | 287 | my_puts_P(PSTR("Abort\n")); |
d0581f88 | 288 | return CMD_RET_SUCCESS; |
72f58822 L |
289 | } |
290 | } | |
291 | ||
d0581f88 | 292 | return CMD_RET_SUCCESS; |
72f58822 L |
293 | } |
294 | ||
d0581f88 | 295 | command_ret_t do_mem_mwc ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
72f58822 L |
296 | { |
297 | int i; | |
298 | uint32_t count; | |
299 | ||
300 | if (argc < 4) | |
301 | return CMD_RET_USAGE; | |
302 | ||
303 | count = strtoul(argv[3], NULL, 10); | |
304 | ||
305 | for (;;) { | |
306 | do_mem_mw (NULL, 0, 3, argv); | |
307 | ||
308 | /* delay for <count> ms... */ | |
309 | /* TODO: use timer */ | |
310 | for (i=0; i<count; i++) | |
311 | udelay (1000); | |
312 | ||
313 | /* check for ctrl-c to abort... */ | |
314 | if (ctrlc()) { | |
69988dc1 | 315 | my_puts_P(PSTR("Abort\n")); |
d0581f88 | 316 | return CMD_RET_SUCCESS; |
72f58822 L |
317 | } |
318 | } | |
319 | ||
d0581f88 | 320 | return CMD_RET_SUCCESS; |
72f58822 L |
321 | } |
322 | #endif /* CONFIG_MX_CYCLIC */ | |
323 | ||
d0581f88 | 324 | command_ret_t do_mem_cmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
72f58822 L |
325 | { |
326 | uint32_t addr1, addr2, count, ngood; | |
d0581f88 | 327 | command_ret_t rcode = CMD_RET_SUCCESS; |
72f58822 L |
328 | uint8_t byte1, byte2; |
329 | ||
330 | (void) cmdtp; | |
331 | (void) flag; | |
332 | ||
333 | if (argc != 4) | |
334 | return CMD_RET_USAGE; | |
335 | ||
336 | ||
337 | addr1 = strtoul(argv[1], NULL, 16); | |
338 | addr1 += base_address; | |
339 | addr2 = strtoul(argv[2], NULL, 16); | |
340 | addr2 += base_address; | |
341 | count = strtoul(argv[3], NULL, 16); | |
342 | ||
343 | for (ngood = 0; ngood < count; ++ngood) { | |
62f624d3 | 344 | z80_bus_cmd(Request); |
72f58822 L |
345 | byte1 = z80_read(addr1); |
346 | byte2 = z80_read(addr2); | |
62f624d3 | 347 | z80_bus_cmd(Release); |
72f58822 | 348 | if (byte1 != byte2) { |
6035a17b L |
349 | printf_P(PSTR("byte at 0x%05lx (%#02x) != " |
350 | "byte at 0x%05lx (%#02x)\n"), | |
72f58822 | 351 | addr1, byte1, addr2, byte2); |
d0581f88 | 352 | rcode = CMD_RET_FAILURE; |
72f58822 L |
353 | break; |
354 | } | |
355 | addr1++; | |
356 | addr2++; | |
357 | ||
358 | /* check for ctrl-c to abort... */ | |
359 | if (ctrlc()) { | |
69988dc1 | 360 | my_puts_P(PSTR("Abort\n")); |
d0581f88 | 361 | return CMD_RET_SUCCESS; |
72f58822 L |
362 | } |
363 | } | |
364 | ||
69988dc1 | 365 | printf_P(PSTR("Total of %ld byte(s) (0x%lx) were the same\n"), ngood, ngood); |
72f58822 L |
366 | return rcode; |
367 | } | |
368 | ||
d0581f88 | 369 | command_ret_t do_mem_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
72f58822 L |
370 | { |
371 | uint32_t src, dest, count; | |
372 | int_fast8_t step; | |
373 | ||
374 | (void) cmdtp; | |
375 | (void) flag; | |
376 | ||
377 | if (argc != 4) | |
378 | return CMD_RET_USAGE; | |
379 | ||
380 | src = strtoul(argv[1], NULL, 16); | |
381 | src += base_address; | |
382 | dest = strtoul(argv[2], NULL, 16); | |
383 | dest += base_address; | |
384 | count = strtoul(argv[3], NULL, 16); | |
385 | ||
386 | if (count == 0) { | |
69988dc1 | 387 | my_puts_P(PSTR("Zero length?\n")); |
d0581f88 | 388 | return CMD_RET_FAILURE; |
72f58822 | 389 | } |
6035a17b | 390 | |
72f58822 L |
391 | if (dest > src) { |
392 | src += count - 1; | |
393 | dest += count - 1; | |
394 | step = -1; | |
395 | } else | |
396 | step = 1; | |
397 | ||
398 | while (count-- > 0) { | |
399 | uint8_t data; | |
62f624d3 | 400 | z80_bus_cmd(Request); |
72f58822 L |
401 | data = z80_read(src); |
402 | z80_write(dest, data); | |
62f624d3 | 403 | z80_bus_cmd(Release); |
72f58822 L |
404 | src += step; |
405 | dest += step; | |
406 | ||
407 | /* check for ctrl-c to abort... */ | |
408 | if (ctrlc()) { | |
69988dc1 | 409 | my_puts_P(PSTR("Abort\n")); |
d0581f88 | 410 | return CMD_RET_SUCCESS; |
72f58822 L |
411 | } |
412 | } | |
d0581f88 | 413 | return CMD_RET_SUCCESS; |
72f58822 L |
414 | } |
415 | ||
d0581f88 | 416 | command_ret_t do_mem_base(cmd_tbl_t *cmdtp, int flag, int argc, |
72f58822 L |
417 | char * const argv[]) |
418 | { | |
419 | (void) cmdtp; | |
420 | (void) flag; | |
421 | ||
422 | if (argc > 1) { | |
423 | /* Set new base address. */ | |
424 | base_address = strtoul(argv[1], NULL, 16); | |
425 | } | |
426 | /* Print the current base address. */ | |
69988dc1 | 427 | printf_P(PSTR("Base Address: 0x%05lx\n"), base_address); |
d0581f88 | 428 | return CMD_RET_SUCCESS; |
72f58822 L |
429 | } |
430 | ||
d0581f88 | 431 | command_ret_t do_mem_loop(cmd_tbl_t *cmdtp, int flag, int argc, |
72f58822 L |
432 | char * const argv[]) |
433 | { | |
434 | uint32_t addr, length; | |
435 | ||
436 | (void) cmdtp; | |
437 | (void) flag; | |
438 | ||
439 | if (argc < 3) | |
440 | return CMD_RET_USAGE; | |
441 | ||
442 | /* Address is always specified. */ | |
443 | addr = strtoul(argv[1], NULL, 16); | |
444 | ||
445 | /* Length is the number of bytes. */ | |
446 | length = strtoul(argv[2], NULL, 16); | |
447 | ||
448 | ||
449 | /* We want to optimize the loops to run as fast as possible. | |
450 | * If we have only one object, just run infinite loops. | |
451 | */ | |
452 | if (length == 1) { | |
62f624d3 | 453 | z80_bus_cmd(Request); |
72f58822 L |
454 | for (;;) |
455 | z80_read(addr); | |
62f624d3 | 456 | z80_bus_cmd(Release); |
72f58822 L |
457 | } |
458 | ||
62f624d3 | 459 | z80_bus_cmd(Request); |
72f58822 L |
460 | for (;;) { |
461 | uint32_t i = length; | |
462 | uint32_t p = addr; | |
463 | while (i-- > 0) | |
464 | z80_read(p++); | |
465 | } | |
62f624d3 | 466 | z80_bus_cmd(Release); |
72f58822 | 467 | |
d0581f88 | 468 | return CMD_RET_SUCCESS; |
72f58822 L |
469 | } |
470 | ||
471 | #ifdef CONFIG_LOOPW | |
d0581f88 | 472 | command_ret_t do_mem_loopw (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
72f58822 L |
473 | { |
474 | uint32_t addr, length; | |
475 | uint8_t data; | |
476 | ||
477 | (void) cmdtp; | |
478 | (void) flag; | |
479 | ||
480 | if (argc < 4) | |
481 | return CMD_RET_USAGE; | |
482 | ||
483 | /* Address is always specified. */ | |
484 | addr = strtoul(argv[1], NULL, 16); | |
485 | ||
486 | /* Length is the number of bytes. */ | |
487 | length = strtoul(argv[2], NULL, 16); | |
488 | ||
489 | data = strtoul(argv[3], NULL, 16); | |
490 | ||
491 | /* We want to optimize the loops to run as fast as possible. | |
492 | * If we have only one object, just run infinite loops. | |
493 | */ | |
494 | if (length == 1) { | |
62f624d3 | 495 | z80_bus_cmd(Request); |
72f58822 L |
496 | for (;;) |
497 | z80_write(addr, data); | |
498 | } | |
499 | ||
500 | for (;;) { | |
501 | uint32_t i = length; | |
502 | uint32_t p = addr; | |
503 | while (i-- > 0) | |
504 | z80_write(p++, data); | |
505 | } | |
506 | } | |
507 | #endif /* CONFIG_LOOPW */ | |
508 | ||
509 | #ifdef CONFIG_CMD_MEMTEST | |
510 | static uint32_t mem_test_alt(vu_long *buf, uint32_t start_addr, uint32_t end_addr, | |
511 | vu_long *dummy) | |
512 | { | |
513 | vu_long *addr; | |
514 | uint32_t errs = 0; | |
515 | uint32_t val, readback; | |
516 | int j; | |
517 | vu_long offset; | |
518 | vu_long test_offset; | |
519 | vu_long pattern; | |
520 | vu_long temp; | |
521 | vu_long anti_pattern; | |
522 | vu_long num_words; | |
523 | static const FLASH uint32_t bitpattern[] = { | |
524 | 0x00000001, /* single bit */ | |
525 | 0x00000003, /* two adjacent bits */ | |
526 | 0x00000007, /* three adjacent bits */ | |
527 | 0x0000000F, /* four adjacent bits */ | |
528 | 0x00000005, /* two non-adjacent bits */ | |
529 | 0x00000015, /* three non-adjacent bits */ | |
530 | 0x00000055, /* four non-adjacent bits */ | |
531 | 0xaaaaaaaa, /* alternating 1/0 */ | |
532 | }; | |
533 | ||
534 | num_words = (end_addr - start_addr) / sizeof(vu_long); | |
535 | ||
536 | /* | |
537 | * Data line test: write a pattern to the first | |
538 | * location, write the 1's complement to a 'parking' | |
539 | * address (changes the state of the data bus so a | |
540 | * floating bus doesn't give a false OK), and then | |
541 | * read the value back. Note that we read it back | |
542 | * into a variable because the next time we read it, | |
543 | * it might be right (been there, tough to explain to | |
544 | * the quality guys why it prints a failure when the | |
545 | * "is" and "should be" are obviously the same in the | |
546 | * error message). | |
547 | * | |
548 | * Rather than exhaustively testing, we test some | |
549 | * patterns by shifting '1' bits through a field of | |
550 | * '0's and '0' bits through a field of '1's (i.e. | |
551 | * pattern and ~pattern). | |
552 | */ | |
553 | addr = buf; | |
554 | for (j = 0; j < sizeof(bitpattern) / sizeof(bitpattern[0]); j++) { | |
555 | val = bitpattern[j]; | |
556 | for (; val != 0; val <<= 1) { | |
557 | *addr = val; | |
558 | *dummy = ~val; /* clear the test data off the bus */ | |
559 | readback = *addr; | |
560 | if (readback != val) { | |
69988dc1 L |
561 | printf_P(PSTR("FAILURE (data line): " |
562 | "expected %05lx, actual %05lx\n"), | |
72f58822 L |
563 | val, readback); |
564 | errs++; | |
565 | if (ctrlc()) | |
566 | return -1; | |
567 | } | |
568 | *addr = ~val; | |
569 | *dummy = val; | |
570 | readback = *addr; | |
571 | if (readback != ~val) { | |
69988dc1 L |
572 | printf_P(PSTR("FAILURE (data line): " |
573 | "Is %05lx, should be %05lx\n"), | |
72f58822 L |
574 | readback, ~val); |
575 | errs++; | |
576 | if (ctrlc()) | |
577 | return -1; | |
578 | } | |
579 | } | |
580 | } | |
581 | ||
582 | /* | |
583 | * Based on code whose Original Author and Copyright | |
584 | * information follows: Copyright (c) 1998 by Michael | |
585 | * Barr. This software is placed into the public | |
586 | * domain and may be used for any purpose. However, | |
587 | * this notice must not be changed or removed and no | |
588 | * warranty is either expressed or implied by its | |
589 | * publication or distribution. | |
590 | */ | |
591 | ||
592 | /* | |
593 | * Address line test | |
594 | ||
595 | * Description: Test the address bus wiring in a | |
596 | * memory region by performing a walking | |
597 | * 1's test on the relevant bits of the | |
598 | * address and checking for aliasing. | |
599 | * This test will find single-bit | |
600 | * address failures such as stuck-high, | |
601 | * stuck-low, and shorted pins. The base | |
602 | * address and size of the region are | |
603 | * selected by the caller. | |
604 | ||
605 | * Notes: For best results, the selected base | |
606 | * address should have enough LSB 0's to | |
607 | * guarantee single address bit changes. | |
608 | * For example, to test a 64-Kbyte | |
609 | * region, select a base address on a | |
610 | * 64-Kbyte boundary. Also, select the | |
611 | * region size as a power-of-two if at | |
612 | * all possible. | |
613 | * | |
614 | * Returns: 0 if the test succeeds, 1 if the test fails. | |
615 | */ | |
616 | pattern = (vu_long) 0xaaaaaaaa; | |
617 | anti_pattern = (vu_long) 0x55555555; | |
618 | ||
619 | debug("%s:%d: length = 0x%.5lx\n", __func__, __LINE__, num_words); | |
620 | /* | |
621 | * Write the default pattern at each of the | |
622 | * power-of-two offsets. | |
623 | */ | |
624 | for (offset = 1; offset < num_words; offset <<= 1) | |
625 | addr[offset] = pattern; | |
626 | ||
627 | /* | |
628 | * Check for address bits stuck high. | |
629 | */ | |
630 | test_offset = 0; | |
631 | addr[test_offset] = anti_pattern; | |
632 | ||
633 | for (offset = 1; offset < num_words; offset <<= 1) { | |
634 | temp = addr[offset]; | |
635 | if (temp != pattern) { | |
69988dc1 L |
636 | printf_P(PSTR("\nFAILURE: Address bit stuck high @ 0x%.5lx:" |
637 | " expected 0x%.5lx, actual 0x%.5lx\n"), | |
72f58822 L |
638 | start_addr + offset*sizeof(vu_long), |
639 | pattern, temp); | |
640 | errs++; | |
641 | if (ctrlc()) | |
642 | return -1; | |
643 | } | |
644 | } | |
645 | addr[test_offset] = pattern; | |
646 | ||
647 | /* | |
648 | * Check for addr bits stuck low or shorted. | |
649 | */ | |
650 | for (test_offset = 1; test_offset < num_words; test_offset <<= 1) { | |
651 | addr[test_offset] = anti_pattern; | |
652 | ||
653 | for (offset = 1; offset < num_words; offset <<= 1) { | |
654 | temp = addr[offset]; | |
655 | if ((temp != pattern) && (offset != test_offset)) { | |
69988dc1 | 656 | printf_P(PSTR("\nFAILURE: Address bit stuck low or" |
72f58822 | 657 | " shorted @ 0x%.5lx: expected 0x%.5lx," |
69988dc1 | 658 | " actual 0x%.5lx\n"), |
72f58822 L |
659 | start_addr + offset*sizeof(vu_long), |
660 | pattern, temp); | |
661 | errs++; | |
662 | if (ctrlc()) | |
663 | return -1; | |
664 | } | |
665 | } | |
666 | addr[test_offset] = pattern; | |
667 | } | |
668 | ||
669 | /* | |
670 | * Description: Test the integrity of a physical | |
671 | * memory device by performing an | |
672 | * increment/decrement test over the | |
673 | * entire region. In the process every | |
674 | * storage bit in the device is tested | |
675 | * as a zero and a one. The base address | |
676 | * and the size of the region are | |
677 | * selected by the caller. | |
678 | * | |
679 | * Returns: 0 if the test succeeds, 1 if the test fails. | |
680 | */ | |
681 | num_words++; | |
682 | ||
683 | /* | |
684 | * Fill memory with a known pattern. | |
685 | */ | |
686 | for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) { | |
687 | addr[offset] = pattern; | |
688 | } | |
689 | ||
690 | /* | |
691 | * Check each location and invert it for the second pass. | |
692 | */ | |
693 | for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) { | |
694 | temp = addr[offset]; | |
695 | if (temp != pattern) { | |
69988dc1 L |
696 | printf_P(PSTR("\nFAILURE (read/write) @ 0x%.5lx:" |
697 | " expected 0x%.5lx, actual 0x%.5lx)\n"), | |
72f58822 L |
698 | start_addr + offset*sizeof(vu_long), |
699 | pattern, temp); | |
700 | errs++; | |
701 | if (ctrlc()) | |
702 | return -1; | |
703 | } | |
704 | ||
705 | anti_pattern = ~pattern; | |
706 | addr[offset] = anti_pattern; | |
707 | } | |
708 | ||
709 | /* | |
710 | * Check each location for the inverted pattern and zero it. | |
711 | */ | |
712 | for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) { | |
713 | WATCHDOG_RESET(); | |
714 | anti_pattern = ~pattern; | |
715 | temp = addr[offset]; | |
716 | if (temp != anti_pattern) { | |
69988dc1 L |
717 | printf_P(PSTR("\nFAILURE (read/write): @ 0x%.5lx:" |
718 | " expected 0x%.5lx, actual 0x%.5lx)\n"), | |
72f58822 L |
719 | start_addr + offset*sizeof(vu_long), |
720 | anti_pattern, temp); | |
721 | errs++; | |
722 | if (ctrlc()) | |
723 | return -1; | |
724 | } | |
725 | addr[offset] = 0; | |
726 | } | |
727 | ||
728 | return 0; | |
729 | } | |
730 | ||
731 | static uint32_t mem_test_quick(vu_long *buf, uint32_t start_addr, uint32_t end_addr, | |
732 | vu_long pattern, int iteration) | |
733 | { | |
734 | vu_long *end; | |
735 | vu_long *addr; | |
736 | uint32_t errs = 0; | |
737 | uint32_t incr, length; | |
738 | uint32_t val, readback; | |
739 | ||
740 | /* Alternate the pattern */ | |
741 | incr = 1; | |
742 | if (iteration & 1) { | |
743 | incr = -incr; | |
744 | /* | |
745 | * Flip the pattern each time to make lots of zeros and | |
746 | * then, the next time, lots of ones. We decrement | |
747 | * the "negative" patterns and increment the "positive" | |
748 | * patterns to preserve this feature. | |
749 | */ | |
750 | if (pattern & 0x80000000) | |
751 | pattern = -pattern; /* complement & increment */ | |
752 | else | |
753 | pattern = ~pattern; | |
754 | } | |
755 | length = (end_addr - start_addr) / sizeof(uint32_t); | |
756 | end = buf + length; | |
69988dc1 | 757 | printf_P(PSTR("\rPattern %08lX Writing..." |
72f58822 | 758 | "%12s" |
69988dc1 | 759 | "\b\b\b\b\b\b\b\b\b\b"), |
72f58822 L |
760 | pattern, ""); |
761 | ||
762 | for (addr = buf, val = pattern; addr < end; addr++) { | |
763 | *addr = val; | |
764 | val += incr; | |
765 | } | |
766 | ||
69988dc1 | 767 | my_puts_P(PSTR("Reading...")); |
72f58822 L |
768 | |
769 | for (addr = buf, val = pattern; addr < end; addr++) { | |
770 | readback = *addr; | |
771 | if (readback != val) { | |
772 | uint32_t offset = addr - buf; | |
773 | ||
69988dc1 L |
774 | printf_P(PSTR("\nMem error @ 0x%08X: " |
775 | "found %08lX, expected %08lX\n"), | |
72f58822 L |
776 | (unsigned int)(uintptr_t)(start_addr + offset*sizeof(vu_long)), |
777 | readback, val); | |
778 | errs++; | |
779 | if (ctrlc()) | |
780 | return -1; | |
781 | } | |
782 | val += incr; | |
783 | } | |
784 | ||
785 | return 0; | |
786 | } | |
787 | ||
788 | /* | |
789 | * Perform a memory test. A more complete alternative test can be | |
790 | * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until | |
791 | * interrupted by ctrl-c or by a failure of one of the sub-tests. | |
792 | */ | |
d0581f88 | 793 | command_ret_t do_mem_mtest(cmd_tbl_t *cmdtp, int flag, int argc, |
72f58822 L |
794 | char * const argv[]) |
795 | { | |
796 | uint32_t start, end; | |
797 | vu_long *buf, *dummy; | |
798 | int iteration_limit; | |
d0581f88 | 799 | /* TODO: command_ret_t */ |
72f58822 L |
800 | int ret; |
801 | uint32_t errs = 0; /* number of errors, or -1 if interrupted */ | |
802 | uint32_t pattern; | |
803 | int iteration; | |
804 | #if defined(CONFIG_SYS_ALT_MEMTEST) | |
805 | const int alt_test = 1; | |
806 | #else | |
807 | const int alt_test = 0; | |
808 | #endif | |
809 | ||
810 | if (argc > 1) | |
811 | start = strtoul(argv[1], NULL, 16); | |
812 | else | |
813 | start = CONFIG_SYS_MEMTEST_START; | |
814 | ||
815 | if (argc > 2) | |
816 | end = strtoul(argv[2], NULL, 16); | |
817 | else | |
818 | end = CONFIG_SYS_MEMTEST_END; | |
819 | ||
820 | if (argc > 3) | |
821 | pattern = (uint32_t)strtoul(argv[3], NULL, 16); | |
822 | else | |
823 | pattern = 0; | |
824 | ||
825 | if (argc > 4) | |
826 | iteration_limit = (uint32_t)strtoul(argv[4], NULL, 16); | |
827 | else | |
828 | iteration_limit = 0; | |
829 | ||
69988dc1 | 830 | printf_P(PSTR("Testing %08x ... %08x:\n"), (unsigned int)start, (unsigned int)end); |
72f58822 L |
831 | debug("%s:%d: start %#05lx end %#05lx\n", __func__, __LINE__, |
832 | start, end); | |
833 | ||
834 | /* TODO: */ | |
835 | // buf = map_sysmem(start, end - start); | |
836 | // dummy = map_sysmem(CONFIG_SYS_MEMTEST_SCRATCH, sizeof(vu_long)); | |
837 | for (iteration = 0; | |
838 | !iteration_limit || iteration < iteration_limit; | |
839 | iteration++) { | |
840 | if (ctrlc()) { | |
841 | errs = -1UL; | |
842 | break; | |
843 | } | |
844 | ||
69988dc1 | 845 | printf_P(PSTR("Iteration: %6d\r"), iteration + 1); |
72f58822 L |
846 | debug("\n"); |
847 | if (alt_test) { | |
848 | errs = mem_test_alt(buf, start, end, dummy); | |
849 | } else { | |
850 | errs = mem_test_quick(buf, start, end, pattern, | |
851 | iteration); | |
852 | } | |
853 | if (errs == -1UL) | |
854 | break; | |
855 | } | |
856 | ||
857 | if (errs == -1UL) { | |
858 | /* Memory test was aborted - write a newline to finish off */ | |
859 | putc('\n'); | |
860 | ret = 1; | |
861 | } else { | |
69988dc1 | 862 | printf_P(PSTR("Tested %d iteration(s) with %lu errors.\n"), |
72f58822 L |
863 | iteration, errs); |
864 | ret = errs != 0; | |
865 | } | |
866 | ||
867 | return ret; /* not reached */ | |
868 | } | |
869 | #endif /* CONFIG_CMD_MEMTEST */ |