]>
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)
17 #include <avr/pgmspace.h>
20 #include "cli_readline.h"
21 #include "print-utils.h"
22 #include "con-utils.h"
27 #ifndef CONFIG_SYS_MEMTEST_SCRATCH
28 #define CONFIG_SYS_MEMTEST_SCRATCH 0
31 /* Display values from last command.
32 * Memory modify remembered values are different from display memory.
34 static uint32_t dp_last_addr
;
35 static uint32_t dp_last_length
= 0x100;
36 static uint32_t mm_last_addr
;
38 static uint32_t base_address
= 0;
40 /*--------------------------------------------------------------------------*/
42 int z180_dump_mem(uint32_t startaddr
, uint32_t len
, const char *title
)
46 uint8_t pre
= startaddr
% 16;
47 uint32_t addr
= startaddr
& ~0x0f;
52 printf_P(PSTR("%s\n"),title
);
59 for (i
= pre
; i
< llen
; i
++)
60 buf
[i
] = z80_read(addr
+ i
);
63 printf_P(PSTR("%.5lx:"), addr
);
65 print_blanks(3 * pre
);
67 /* Print hex values */
68 for (i
= pre
; i
< llen
; i
++)
69 printf_P(PSTR(" %.2x"), buf
[i
]);
71 for (i
= 0; i
< llen
; i
++) {
75 printf_P(PSTR(".. "));
77 printf_P(PSTR("%.2x "), buf
[i
]);
80 /* fill line with whitespace for nice ASCII print */
82 print_blanks(3 * (16u - i
) + (16u-i
)/8 + 1 + pre
);
86 /* Print data in ASCII characters */
87 for (i
= pre
; i
< llen
; i
++)
88 printf_P(PSTR("%c"), isprint(buf
[i
]) ? buf
[i
] : '.');
102 /*--------------------------------------------------------------------------*/
109 command_ret_t
do_mem_md(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
111 uint32_t addr
, length
;
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>");
123 /* We use the last specified parameters, unless new ones are
127 length
= dp_last_length
;
130 return CMD_RET_USAGE
;
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
;
137 /* If another parameter, it is the length to display. */
139 length
= strtoul(argv
[2], NULL
, 16);
142 /* Print the lines. */
143 z180_dump_mem(addr
, length
, NULL
);
145 dp_last_addr
= addr
+ length
;
146 dp_last_length
= length
;
147 return CMD_RET_SUCCESS
;
157 mod_mem(cmd_tbl_t
*cmdtp
, int incrflag
, int flag
, int argc
, char * const argv
[])
166 return CMD_RET_USAGE
;
168 /* We use the last specified parameters, unless new ones are
173 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
174 /* New command specified.
177 /* Address is specified since argc > 1
179 addr
= strtoul(argv
[1], NULL
, 16);
180 addr
+= base_address
;
183 /* Print the address, followed by value. Then accept input for
184 * the next value. A non-converted value exits.
187 z80_bus_cmd(Request
);
188 data
= z80_read(addr
);
189 printf_P(PSTR("%05lx: %02x"), addr
, data
);
190 z80_bus_cmd(Release
);
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.
198 addr
+= nbytes
? -1 : 1;
203 data
= strtoul(console_buffer
, &endp
, 16);
204 nbytes
= endp
- console_buffer
;
206 z80_bus_cmd(Request
);
207 z80_write(addr
, data
);
208 z80_bus_cmd(Release
);
216 return CMD_RET_SUCCESS
;
220 command_ret_t
do_mem_mm(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
222 return mod_mem (cmdtp
, 1, flag
, argc
, argv
);
224 command_ret_t
do_mem_nm(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
226 return mod_mem (cmdtp
, 0, flag
, argc
, argv
);
229 command_ret_t
do_mem_mw(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
232 uint32_t addr
, count
;
237 if ((argc
< 3) || (argc
> 4))
238 return CMD_RET_USAGE
;
240 /* Address is specified since argc > 1
242 addr
= strtoul(argv
[1], NULL
, 16);
243 addr
+= base_address
;
245 /* Get the value to write.
247 writeval
= (uint8_t) strtoul(argv
[2], NULL
, 16);
251 count
= strtoul(argv
[3], NULL
, 16);
256 z80_bus_cmd(Request
);
257 while (count
-- > 0) {
258 z80_write(addr
, writeval
);
261 z80_bus_cmd(Release
);
263 return CMD_RET_SUCCESS
;
266 #ifdef CONFIG_MX_CYCLIC
267 command_ret_t
do_mem_mdc ( cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
273 return CMD_RET_USAGE
;
275 count
= strtoul(argv
[3], NULL
, 10);
278 do_mem_md (NULL
, 0, 3, argv
);
280 /* delay for <count> ms... */
281 /* TODO: use timer */
282 for (i
=0; i
<count
; i
++)
285 /* check for ctrl-c to abort... */
287 my_puts_P(PSTR("Abort\n"));
288 return CMD_RET_SUCCESS
;
292 return CMD_RET_SUCCESS
;
295 command_ret_t
do_mem_mwc ( cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
301 return CMD_RET_USAGE
;
303 count
= strtoul(argv
[3], NULL
, 10);
306 do_mem_mw (NULL
, 0, 3, argv
);
308 /* delay for <count> ms... */
309 /* TODO: use timer */
310 for (i
=0; i
<count
; i
++)
313 /* check for ctrl-c to abort... */
315 my_puts_P(PSTR("Abort\n"));
316 return CMD_RET_SUCCESS
;
320 return CMD_RET_SUCCESS
;
322 #endif /* CONFIG_MX_CYCLIC */
324 command_ret_t
do_mem_cmp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
326 uint32_t addr1
, addr2
, count
, ngood
;
327 command_ret_t rcode
= CMD_RET_SUCCESS
;
328 uint8_t byte1
, byte2
;
334 return CMD_RET_USAGE
;
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);
343 for (ngood
= 0; ngood
< count
; ++ngood
) {
344 z80_bus_cmd(Request
);
345 byte1
= z80_read(addr1
);
346 byte2
= z80_read(addr2
);
347 z80_bus_cmd(Release
);
348 if (byte1
!= byte2
) {
349 printf_P(PSTR("byte at 0x%05lx (%#02x) != "
350 "byte at 0x%05lx (%#02x)\n"),
351 addr1
, byte1
, addr2
, byte2
);
352 rcode
= CMD_RET_FAILURE
;
358 /* check for ctrl-c to abort... */
360 my_puts_P(PSTR("Abort\n"));
361 return CMD_RET_SUCCESS
;
365 printf_P(PSTR("Total of %ld byte(s) (0x%lx) were the same\n"), ngood
, ngood
);
369 command_ret_t
do_mem_cp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
371 uint32_t src
, dest
, count
;
378 return CMD_RET_USAGE
;
380 src
= strtoul(argv
[1], NULL
, 16);
382 dest
= strtoul(argv
[2], NULL
, 16);
383 dest
+= base_address
;
384 count
= strtoul(argv
[3], NULL
, 16);
387 my_puts_P(PSTR("Zero length?\n"));
388 return CMD_RET_FAILURE
;
398 while (count
-- > 0) {
400 z80_bus_cmd(Request
);
401 data
= z80_read(src
);
402 z80_write(dest
, data
);
403 z80_bus_cmd(Release
);
407 /* check for ctrl-c to abort... */
409 my_puts_P(PSTR("Abort\n"));
410 return CMD_RET_SUCCESS
;
413 return CMD_RET_SUCCESS
;
416 command_ret_t
do_mem_base(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
423 /* Set new base address. */
424 base_address
= strtoul(argv
[1], NULL
, 16);
426 /* Print the current base address. */
427 printf_P(PSTR("Base Address: 0x%05lx\n"), base_address
);
428 return CMD_RET_SUCCESS
;
431 command_ret_t
do_mem_loop(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
434 uint32_t addr
, length
;
440 return CMD_RET_USAGE
;
442 /* Address is always specified. */
443 addr
= strtoul(argv
[1], NULL
, 16);
445 /* Length is the number of bytes. */
446 length
= strtoul(argv
[2], NULL
, 16);
449 /* We want to optimize the loops to run as fast as possible.
450 * If we have only one object, just run infinite loops.
453 z80_bus_cmd(Request
);
456 z80_bus_cmd(Release
);
459 z80_bus_cmd(Request
);
466 z80_bus_cmd(Release
);
468 return CMD_RET_SUCCESS
;
472 command_ret_t
do_mem_loopw (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
474 uint32_t addr
, length
;
481 return CMD_RET_USAGE
;
483 /* Address is always specified. */
484 addr
= strtoul(argv
[1], NULL
, 16);
486 /* Length is the number of bytes. */
487 length
= strtoul(argv
[2], NULL
, 16);
489 data
= strtoul(argv
[3], NULL
, 16);
491 /* We want to optimize the loops to run as fast as possible.
492 * If we have only one object, just run infinite loops.
495 z80_bus_cmd(Request
);
497 z80_write(addr
, data
);
504 z80_write(p
++, data
);
507 #endif /* CONFIG_LOOPW */
509 #ifdef CONFIG_CMD_MEMTEST
510 static uint32_t mem_test_alt(vu_long
*buf
, uint32_t start_addr
, uint32_t end_addr
,
515 uint32_t val
, readback
;
521 vu_long anti_pattern
;
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 */
534 num_words
= (end_addr
- start_addr
) / sizeof(vu_long
);
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
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).
554 for (j
= 0; j
< sizeof(bitpattern
) / sizeof(bitpattern
[0]); j
++) {
556 for (; val
!= 0; val
<<= 1) {
558 *dummy
= ~val
; /* clear the test data off the bus */
560 if (readback
!= val
) {
561 printf_P(PSTR("FAILURE (data line): "
562 "expected %05lx, actual %05lx\n"),
571 if (readback
!= ~val
) {
572 printf_P(PSTR("FAILURE (data line): "
573 "Is %05lx, should be %05lx\n"),
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.
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.
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
614 * Returns: 0 if the test succeeds, 1 if the test fails.
616 pattern
= (vu_long
) 0xaaaaaaaa;
617 anti_pattern
= (vu_long
) 0x55555555;
619 debug("%s:%d: length = 0x%.5lx\n", __func__
, __LINE__
, num_words
);
621 * Write the default pattern at each of the
622 * power-of-two offsets.
624 for (offset
= 1; offset
< num_words
; offset
<<= 1)
625 addr
[offset
] = pattern
;
628 * Check for address bits stuck high.
631 addr
[test_offset
] = anti_pattern
;
633 for (offset
= 1; offset
< num_words
; offset
<<= 1) {
635 if (temp
!= pattern
) {
636 printf_P(PSTR("\nFAILURE: Address bit stuck high @ 0x%.5lx:"
637 " expected 0x%.5lx, actual 0x%.5lx\n"),
638 start_addr
+ offset
*sizeof(vu_long
),
645 addr
[test_offset
] = pattern
;
648 * Check for addr bits stuck low or shorted.
650 for (test_offset
= 1; test_offset
< num_words
; test_offset
<<= 1) {
651 addr
[test_offset
] = anti_pattern
;
653 for (offset
= 1; offset
< num_words
; offset
<<= 1) {
655 if ((temp
!= pattern
) && (offset
!= test_offset
)) {
656 printf_P(PSTR("\nFAILURE: Address bit stuck low or"
657 " shorted @ 0x%.5lx: expected 0x%.5lx,"
658 " actual 0x%.5lx\n"),
659 start_addr
+ offset
*sizeof(vu_long
),
666 addr
[test_offset
] = pattern
;
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.
679 * Returns: 0 if the test succeeds, 1 if the test fails.
684 * Fill memory with a known pattern.
686 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, offset
++) {
687 addr
[offset
] = pattern
;
691 * Check each location and invert it for the second pass.
693 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, offset
++) {
695 if (temp
!= pattern
) {
696 printf_P(PSTR("\nFAILURE (read/write) @ 0x%.5lx:"
697 " expected 0x%.5lx, actual 0x%.5lx)\n"),
698 start_addr
+ offset
*sizeof(vu_long
),
705 anti_pattern
= ~pattern
;
706 addr
[offset
] = anti_pattern
;
710 * Check each location for the inverted pattern and zero it.
712 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, offset
++) {
714 anti_pattern
= ~pattern
;
716 if (temp
!= anti_pattern
) {
717 printf_P(PSTR("\nFAILURE (read/write): @ 0x%.5lx:"
718 " expected 0x%.5lx, actual 0x%.5lx)\n"),
719 start_addr
+ offset
*sizeof(vu_long
),
731 static uint32_t mem_test_quick(vu_long
*buf
, uint32_t start_addr
, uint32_t end_addr
,
732 vu_long pattern
, int iteration
)
737 uint32_t incr
, length
;
738 uint32_t val
, readback
;
740 /* Alternate the pattern */
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.
750 if (pattern
& 0x80000000)
751 pattern
= -pattern
; /* complement & increment */
755 length
= (end_addr
- start_addr
) / sizeof(uint32_t);
757 printf_P(PSTR("\rPattern %08lX Writing..."
759 "\b\b\b\b\b\b\b\b\b\b"),
762 for (addr
= buf
, val
= pattern
; addr
< end
; addr
++) {
767 my_puts_P(PSTR("Reading..."));
769 for (addr
= buf
, val
= pattern
; addr
< end
; addr
++) {
771 if (readback
!= val
) {
772 uint32_t offset
= addr
- buf
;
774 printf_P(PSTR("\nMem error @ 0x%08X: "
775 "found %08lX, expected %08lX\n"),
776 (unsigned int)(uintptr_t)(start_addr
+ offset
*sizeof(vu_long
)),
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.
793 command_ret_t
do_mem_mtest(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
797 vu_long
*buf
, *dummy
;
799 /* TODO: command_ret_t */
801 uint32_t errs
= 0; /* number of errors, or -1 if interrupted */
804 #if defined(CONFIG_SYS_ALT_MEMTEST)
805 const int alt_test
= 1;
807 const int alt_test
= 0;
811 start
= strtoul(argv
[1], NULL
, 16);
813 start
= CONFIG_SYS_MEMTEST_START
;
816 end
= strtoul(argv
[2], NULL
, 16);
818 end
= CONFIG_SYS_MEMTEST_END
;
821 pattern
= (uint32_t)strtoul(argv
[3], NULL
, 16);
826 iteration_limit
= (uint32_t)strtoul(argv
[4], NULL
, 16);
830 printf_P(PSTR("Testing %08x ... %08x:\n"), (unsigned int)start
, (unsigned int)end
);
831 debug("%s:%d: start %#05lx end %#05lx\n", __func__
, __LINE__
,
835 // buf = map_sysmem(start, end - start);
836 // dummy = map_sysmem(CONFIG_SYS_MEMTEST_SCRATCH, sizeof(vu_long));
838 !iteration_limit
|| iteration
< iteration_limit
;
845 printf_P(PSTR("Iteration: %6d\r"), iteration
+ 1);
848 errs
= mem_test_alt(buf
, start
, end
, dummy
);
850 errs
= mem_test_quick(buf
, start
, end
, pattern
,
858 /* Memory test was aborted - write a newline to finish off */
862 printf_P(PSTR("Tested %d iteration(s) with %lu errors.\n"),
867 return ret
; /* not reached */
869 #endif /* CONFIG_CMD_MEMTEST */