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