]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_mem.c
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
11 * Copied from FADS ROM, Dan Malek (dmalek@jlc.net)
19 #include "cli_readline.h"
20 #include "print-utils.h"
21 #include "con-utils.h"
26 #ifndef CONFIG_SYS_MEMTEST_SCRATCH
27 #define CONFIG_SYS_MEMTEST_SCRATCH 0
30 /* Display values from last command.
31 * Memory modify remembered values are different from display memory.
33 static uint32_t dp_last_addr
;
34 static uint32_t dp_last_length
= 0x100;
35 static uint32_t mm_last_addr
;
37 static uint32_t base_address
= 0;
39 /*--------------------------------------------------------------------------*/
41 int z180_dump_mem(uint32_t startaddr
, uint32_t len
, const char *title
)
45 uint8_t pre
= startaddr
% 16;
46 uint32_t addr
= startaddr
& ~0x0f;
51 printf_P(PSTR("%s\n"),title
);
58 for (i
= pre
; i
< llen
; i
++)
59 buf
[i
] = z80_read(addr
+ i
);
62 printf_P(PSTR("%.5lx:"), addr
);
64 print_blanks(3 * pre
);
66 /* Print hex values */
67 for (i
= pre
; i
< llen
; i
++)
68 printf_P(PSTR(" %.2x"), buf
[i
]);
70 for (i
= 0; i
< llen
; i
++) {
74 printf_P(PSTR(".. "));
76 printf_P(PSTR("%.2x "), buf
[i
]);
79 /* fill line with whitespace for nice ASCII print */
81 print_blanks(3 * (16u - i
) + (16u-i
)/8 + 1 + pre
);
85 /* Print data in ASCII characters */
86 for (i
= pre
; i
< llen
; i
++)
87 printf_P(PSTR("%c"), isprint(buf
[i
]) ? buf
[i
] : '.');
101 /*--------------------------------------------------------------------------*/
108 command_ret_t
do_mem_md(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
110 uint32_t addr
, length
;
115 printf_P(PSTR("flag: %d, argc: %d"), flag
, argc
);
116 for (int i
= 0; i
< argc
; i
++) {
117 printf_P(PSTR(", argv[%d]: %s"), i
, argv
[i
] ? argv
[i
] : "<NULL>");
122 /* We use the last specified parameters, unless new ones are
126 length
= dp_last_length
;
129 return CMD_RET_USAGE
;
131 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
132 /* Address is specified since argc > 1 */
133 addr
= strtoul(argv
[1], NULL
, 16);
134 addr
+= base_address
;
136 /* If another parameter, it is the length to display. */
138 length
= strtoul(argv
[2], NULL
, 16);
141 /* Print the lines. */
142 z180_dump_mem(addr
, length
, NULL
);
144 dp_last_addr
= addr
+ length
;
145 dp_last_length
= length
;
146 return CMD_RET_SUCCESS
;
156 mod_mem(cmd_tbl_t
*cmdtp
, int incrflag
, int flag
, int argc
, char * const argv
[])
165 return CMD_RET_USAGE
;
167 /* We use the last specified parameters, unless new ones are
172 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
173 /* New command specified.
176 /* Address is specified since argc > 1
178 addr
= strtoul(argv
[1], NULL
, 16);
179 addr
+= base_address
;
182 /* Print the address, followed by value. Then accept input for
183 * the next value. A non-converted value exits.
186 z80_bus_cmd(Request
);
187 data
= z80_read(addr
);
188 printf_P(PSTR("%05lx: %02x"), addr
, data
);
189 z80_bus_cmd(Release
);
191 nbytes
= cli_readline(PSTR(" ? "));
192 if (nbytes
== 0 || (nbytes
== 1 && console_buffer
[0] == '-')) {
193 /* <CR> pressed as only input, don't modify current
194 * location and move to next. "-" pressed will go back.
197 addr
+= nbytes
? -1 : 1;
202 data
= strtoul(console_buffer
, &endp
, 16);
203 nbytes
= endp
- console_buffer
;
205 z80_bus_cmd(Request
);
206 z80_write(addr
, data
);
207 z80_bus_cmd(Release
);
215 return CMD_RET_SUCCESS
;
219 command_ret_t
do_mem_mm(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
221 return mod_mem (cmdtp
, 1, flag
, argc
, argv
);
223 command_ret_t
do_mem_nm(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
225 return mod_mem (cmdtp
, 0, flag
, argc
, argv
);
228 command_ret_t
do_mem_mw(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
231 uint32_t addr
, count
;
236 if ((argc
< 3) || (argc
> 4))
237 return CMD_RET_USAGE
;
239 /* Address is specified since argc > 1
241 addr
= strtoul(argv
[1], NULL
, 16);
242 addr
+= base_address
;
244 /* Get the value to write.
246 writeval
= (uint8_t) strtoul(argv
[2], NULL
, 16);
250 count
= strtoul(argv
[3], NULL
, 16);
255 z80_bus_cmd(Request
);
256 while (count
-- > 0) {
257 z80_write(addr
, writeval
);
260 z80_bus_cmd(Release
);
262 return CMD_RET_SUCCESS
;
265 #ifdef CONFIG_MX_CYCLIC
266 command_ret_t
do_mem_mdc ( cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
272 return CMD_RET_USAGE
;
274 count
= strtoul(argv
[3], NULL
, 10);
277 do_mem_md (NULL
, 0, 3, argv
);
279 /* delay for <count> ms... */
280 /* TODO: use timer */
281 for (i
=0; i
<count
; i
++)
284 /* check for ctrl-c to abort... */
286 my_puts_P(PSTR("Abort\n"));
287 return CMD_RET_SUCCESS
;
291 return CMD_RET_SUCCESS
;
294 command_ret_t
do_mem_mwc ( cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
300 return CMD_RET_USAGE
;
302 count
= strtoul(argv
[3], NULL
, 10);
305 do_mem_mw (NULL
, 0, 3, argv
);
307 /* delay for <count> ms... */
308 /* TODO: use timer */
309 for (i
=0; i
<count
; i
++)
312 /* check for ctrl-c to abort... */
314 my_puts_P(PSTR("Abort\n"));
315 return CMD_RET_SUCCESS
;
319 return CMD_RET_SUCCESS
;
321 #endif /* CONFIG_MX_CYCLIC */
323 command_ret_t
do_mem_cmp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
325 uint32_t addr1
, addr2
, count
, ngood
;
326 command_ret_t rcode
= CMD_RET_SUCCESS
;
327 uint8_t byte1
, byte2
;
333 return CMD_RET_USAGE
;
336 addr1
= strtoul(argv
[1], NULL
, 16);
337 addr1
+= base_address
;
338 addr2
= strtoul(argv
[2], NULL
, 16);
339 addr2
+= base_address
;
340 count
= strtoul(argv
[3], NULL
, 16);
342 for (ngood
= 0; ngood
< count
; ++ngood
) {
343 z80_bus_cmd(Request
);
344 byte1
= z80_read(addr1
);
345 byte2
= z80_read(addr2
);
346 z80_bus_cmd(Release
);
347 if (byte1
!= byte2
) {
348 printf_P(PSTR("byte at 0x%05lx (%#02x) != "
349 "byte at 0x%05lx (%#02x)\n"),
350 addr1
, byte1
, addr2
, byte2
);
351 rcode
= CMD_RET_FAILURE
;
357 /* check for ctrl-c to abort... */
359 my_puts_P(PSTR("Abort\n"));
360 return CMD_RET_SUCCESS
;
364 printf_P(PSTR("Total of %ld byte(s) (0x%lx) were the same\n"), ngood
, ngood
);
368 command_ret_t
do_mem_cp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
370 uint32_t src
, dest
, count
;
377 return CMD_RET_USAGE
;
379 src
= strtoul(argv
[1], NULL
, 16);
381 dest
= strtoul(argv
[2], NULL
, 16);
382 dest
+= base_address
;
383 count
= strtoul(argv
[3], NULL
, 16);
386 my_puts_P(PSTR("Zero length?\n"));
387 return CMD_RET_FAILURE
;
397 while (count
-- > 0) {
399 z80_bus_cmd(Request
);
400 data
= z80_read(src
);
401 z80_write(dest
, data
);
402 z80_bus_cmd(Release
);
406 /* check for ctrl-c to abort... */
408 my_puts_P(PSTR("Abort\n"));
409 return CMD_RET_SUCCESS
;
412 return CMD_RET_SUCCESS
;
415 command_ret_t
do_mem_base(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
422 /* Set new base address. */
423 base_address
= strtoul(argv
[1], NULL
, 16);
425 /* Print the current base address. */
426 printf_P(PSTR("Base Address: 0x%05lx\n"), base_address
);
427 return CMD_RET_SUCCESS
;
430 command_ret_t
do_mem_loop(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
433 uint32_t addr
, length
;
439 return CMD_RET_USAGE
;
441 /* Address is always specified. */
442 addr
= strtoul(argv
[1], NULL
, 16);
444 /* Length is the number of bytes. */
445 length
= strtoul(argv
[2], NULL
, 16);
448 /* We want to optimize the loops to run as fast as possible.
449 * If we have only one object, just run infinite loops.
452 z80_bus_cmd(Request
);
455 z80_bus_cmd(Release
);
458 z80_bus_cmd(Request
);
465 z80_bus_cmd(Release
);
467 return CMD_RET_SUCCESS
;
471 command_ret_t
do_mem_loopw (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
473 uint32_t addr
, length
;
480 return CMD_RET_USAGE
;
482 /* Address is always specified. */
483 addr
= strtoul(argv
[1], NULL
, 16);
485 /* Length is the number of bytes. */
486 length
= strtoul(argv
[2], NULL
, 16);
488 data
= strtoul(argv
[3], NULL
, 16);
490 /* We want to optimize the loops to run as fast as possible.
491 * If we have only one object, just run infinite loops.
494 z80_bus_cmd(Request
);
496 z80_write(addr
, data
);
503 z80_write(p
++, data
);
506 #endif /* CONFIG_LOOPW */
508 #ifdef CONFIG_CMD_MEMTEST
509 static uint32_t mem_test_alt(vu_long
*buf
, uint32_t start_addr
, uint32_t end_addr
,
514 uint32_t val
, readback
;
520 vu_long anti_pattern
;
522 static const FLASH
uint32_t bitpattern
[] = {
523 0x00000001, /* single bit */
524 0x00000003, /* two adjacent bits */
525 0x00000007, /* three adjacent bits */
526 0x0000000F, /* four adjacent bits */
527 0x00000005, /* two non-adjacent bits */
528 0x00000015, /* three non-adjacent bits */
529 0x00000055, /* four non-adjacent bits */
530 0xaaaaaaaa, /* alternating 1/0 */
533 num_words
= (end_addr
- start_addr
) / sizeof(vu_long
);
536 * Data line test: write a pattern to the first
537 * location, write the 1's complement to a 'parking'
538 * address (changes the state of the data bus so a
539 * floating bus doesn't give a false OK), and then
540 * read the value back. Note that we read it back
541 * into a variable because the next time we read it,
542 * it might be right (been there, tough to explain to
543 * the quality guys why it prints a failure when the
544 * "is" and "should be" are obviously the same in the
547 * Rather than exhaustively testing, we test some
548 * patterns by shifting '1' bits through a field of
549 * '0's and '0' bits through a field of '1's (i.e.
550 * pattern and ~pattern).
553 for (j
= 0; j
< sizeof(bitpattern
) / sizeof(bitpattern
[0]); j
++) {
555 for (; val
!= 0; val
<<= 1) {
557 *dummy
= ~val
; /* clear the test data off the bus */
559 if (readback
!= val
) {
560 printf_P(PSTR("FAILURE (data line): "
561 "expected %05lx, actual %05lx\n"),
570 if (readback
!= ~val
) {
571 printf_P(PSTR("FAILURE (data line): "
572 "Is %05lx, should be %05lx\n"),
582 * Based on code whose Original Author and Copyright
583 * information follows: Copyright (c) 1998 by Michael
584 * Barr. This software is placed into the public
585 * domain and may be used for any purpose. However,
586 * this notice must not be changed or removed and no
587 * warranty is either expressed or implied by its
588 * publication or distribution.
594 * Description: Test the address bus wiring in a
595 * memory region by performing a walking
596 * 1's test on the relevant bits of the
597 * address and checking for aliasing.
598 * This test will find single-bit
599 * address failures such as stuck-high,
600 * stuck-low, and shorted pins. The base
601 * address and size of the region are
602 * selected by the caller.
604 * Notes: For best results, the selected base
605 * address should have enough LSB 0's to
606 * guarantee single address bit changes.
607 * For example, to test a 64-Kbyte
608 * region, select a base address on a
609 * 64-Kbyte boundary. Also, select the
610 * region size as a power-of-two if at
613 * Returns: 0 if the test succeeds, 1 if the test fails.
615 pattern
= (vu_long
) 0xaaaaaaaa;
616 anti_pattern
= (vu_long
) 0x55555555;
618 debug("%s:%d: length = 0x%.5lx\n", __func__
, __LINE__
, num_words
);
620 * Write the default pattern at each of the
621 * power-of-two offsets.
623 for (offset
= 1; offset
< num_words
; offset
<<= 1)
624 addr
[offset
] = pattern
;
627 * Check for address bits stuck high.
630 addr
[test_offset
] = anti_pattern
;
632 for (offset
= 1; offset
< num_words
; offset
<<= 1) {
634 if (temp
!= pattern
) {
635 printf_P(PSTR("\nFAILURE: Address bit stuck high @ 0x%.5lx:"
636 " expected 0x%.5lx, actual 0x%.5lx\n"),
637 start_addr
+ offset
*sizeof(vu_long
),
644 addr
[test_offset
] = pattern
;
647 * Check for addr bits stuck low or shorted.
649 for (test_offset
= 1; test_offset
< num_words
; test_offset
<<= 1) {
650 addr
[test_offset
] = anti_pattern
;
652 for (offset
= 1; offset
< num_words
; offset
<<= 1) {
654 if ((temp
!= pattern
) && (offset
!= test_offset
)) {
655 printf_P(PSTR("\nFAILURE: Address bit stuck low or"
656 " shorted @ 0x%.5lx: expected 0x%.5lx,"
657 " actual 0x%.5lx\n"),
658 start_addr
+ offset
*sizeof(vu_long
),
665 addr
[test_offset
] = pattern
;
669 * Description: Test the integrity of a physical
670 * memory device by performing an
671 * increment/decrement test over the
672 * entire region. In the process every
673 * storage bit in the device is tested
674 * as a zero and a one. The base address
675 * and the size of the region are
676 * selected by the caller.
678 * Returns: 0 if the test succeeds, 1 if the test fails.
683 * Fill memory with a known pattern.
685 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, offset
++) {
686 addr
[offset
] = pattern
;
690 * Check each location and invert it for the second pass.
692 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, offset
++) {
694 if (temp
!= pattern
) {
695 printf_P(PSTR("\nFAILURE (read/write) @ 0x%.5lx:"
696 " expected 0x%.5lx, actual 0x%.5lx)\n"),
697 start_addr
+ offset
*sizeof(vu_long
),
704 anti_pattern
= ~pattern
;
705 addr
[offset
] = anti_pattern
;
709 * Check each location for the inverted pattern and zero it.
711 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, offset
++) {
713 anti_pattern
= ~pattern
;
715 if (temp
!= anti_pattern
) {
716 printf_P(PSTR("\nFAILURE (read/write): @ 0x%.5lx:"
717 " expected 0x%.5lx, actual 0x%.5lx)\n"),
718 start_addr
+ offset
*sizeof(vu_long
),
730 static uint32_t mem_test_quick(vu_long
*buf
, uint32_t start_addr
, uint32_t end_addr
,
731 vu_long pattern
, int iteration
)
736 uint32_t incr
, length
;
737 uint32_t val
, readback
;
739 /* Alternate the pattern */
744 * Flip the pattern each time to make lots of zeros and
745 * then, the next time, lots of ones. We decrement
746 * the "negative" patterns and increment the "positive"
747 * patterns to preserve this feature.
749 if (pattern
& 0x80000000)
750 pattern
= -pattern
; /* complement & increment */
754 length
= (end_addr
- start_addr
) / sizeof(uint32_t);
756 printf_P(PSTR("\rPattern %08lX Writing..."
758 "\b\b\b\b\b\b\b\b\b\b"),
761 for (addr
= buf
, val
= pattern
; addr
< end
; addr
++) {
766 my_puts_P(PSTR("Reading..."));
768 for (addr
= buf
, val
= pattern
; addr
< end
; addr
++) {
770 if (readback
!= val
) {
771 uint32_t offset
= addr
- buf
;
773 printf_P(PSTR("\nMem error @ 0x%08X: "
774 "found %08lX, expected %08lX\n"),
775 (unsigned int)(uintptr_t)(start_addr
+ offset
*sizeof(vu_long
)),
788 * Perform a memory test. A more complete alternative test can be
789 * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
790 * interrupted by ctrl-c or by a failure of one of the sub-tests.
792 command_ret_t
do_mem_mtest(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
796 vu_long
*buf
, *dummy
;
798 /* TODO: command_ret_t */
800 uint32_t errs
= 0; /* number of errors, or -1 if interrupted */
803 #if defined(CONFIG_SYS_ALT_MEMTEST)
804 const int alt_test
= 1;
806 const int alt_test
= 0;
810 start
= strtoul(argv
[1], NULL
, 16);
812 start
= CONFIG_SYS_MEMTEST_START
;
815 end
= strtoul(argv
[2], NULL
, 16);
817 end
= CONFIG_SYS_MEMTEST_END
;
820 pattern
= (uint32_t)strtoul(argv
[3], NULL
, 16);
825 iteration_limit
= (uint32_t)strtoul(argv
[4], NULL
, 16);
829 printf_P(PSTR("Testing %08x ... %08x:\n"), (unsigned int)start
, (unsigned int)end
);
830 debug("%s:%d: start %#05lx end %#05lx\n", __func__
, __LINE__
,
834 // buf = map_sysmem(start, end - start);
835 // dummy = map_sysmem(CONFIG_SYS_MEMTEST_SCRATCH, sizeof(vu_long));
837 !iteration_limit
|| iteration
< iteration_limit
;
844 printf_P(PSTR("Iteration: %6d\r"), iteration
+ 1);
847 errs
= mem_test_alt(buf
, start
, end
, dummy
);
849 errs
= mem_test_quick(buf
, start
, end
, pattern
,
857 /* Memory test was aborted - write a newline to finish off */
861 printf_P(PSTR("Tested %d iteration(s) with %lu errors.\n"),
866 return ret
; /* not reached */
868 #endif /* CONFIG_CMD_MEMTEST */