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