]>
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 /*--------------------------------------------------------------------------*/
42 void z180_read_buf(uint8_t *buf
, uint32_t addr
, uint8_t count
)
46 *buf
++ = z80_read(addr
++);
50 /*--------------------------------------------------------------------------*/
57 command_ret_t
do_mem_md(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
59 uint32_t addr
, length
;
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>");
71 /* We use the last specified parameters, unless new ones are
75 length
= dp_last_length
;
80 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
81 /* Address is specified since argc > 1 */
82 addr
= strtoul(argv
[1], NULL
, 16);
85 /* If another parameter, it is the length to display. */
87 length
= strtoul(argv
[2], NULL
, 16);
90 /* Print the lines. */
91 dump_mem(addr
, length
, z180_read_buf
, NULL
);
93 dp_last_addr
= addr
+ length
;
94 dp_last_length
= length
;
95 return CMD_RET_SUCCESS
;
105 mod_mem(cmd_tbl_t
*cmdtp
, int incrflag
, int flag
, int argc
, char * const argv
[])
114 return CMD_RET_USAGE
;
116 /* We use the last specified parameters, unless new ones are
121 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
122 /* New command specified.
125 /* Address is specified since argc > 1
127 addr
= strtoul(argv
[1], NULL
, 16);
128 addr
+= base_address
;
131 /* Print the address, followed by value. Then accept input for
132 * the next value. A non-converted value exits.
135 z80_bus_cmd(Request
);
136 data
= z80_read(addr
);
137 printf_P(PSTR("%05lx: %02x"), addr
, data
);
138 z80_bus_cmd(Release
);
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.
146 addr
+= nbytes
? -1 : 1;
151 data
= strtoul(console_buffer
, &endp
, 16);
152 nbytes
= endp
- console_buffer
;
154 z80_bus_cmd(Request
);
155 z80_write(addr
, data
);
156 z80_bus_cmd(Release
);
164 return CMD_RET_SUCCESS
;
168 command_ret_t
do_mem_mm(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
170 return mod_mem (cmdtp
, 1, flag
, argc
, argv
);
172 command_ret_t
do_mem_nm(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
174 return mod_mem (cmdtp
, 0, flag
, argc
, argv
);
177 command_ret_t
do_mem_mw(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
180 uint32_t addr
, count
;
185 if ((argc
< 3) || (argc
> 4))
186 return CMD_RET_USAGE
;
188 /* Address is specified since argc > 1
190 addr
= strtoul(argv
[1], NULL
, 16);
191 addr
+= base_address
;
193 /* Get the value to write.
195 writeval
= (uint8_t) strtoul(argv
[2], NULL
, 16);
199 count
= strtoul(argv
[3], NULL
, 16);
204 z80_bus_cmd(Request
);
205 while (count
-- > 0) {
206 z80_write(addr
, writeval
);
209 z80_bus_cmd(Release
);
211 return CMD_RET_SUCCESS
;
214 #ifdef CONFIG_MX_CYCLIC
215 command_ret_t
do_mem_mdc ( cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
221 return CMD_RET_USAGE
;
223 count
= strtoul(argv
[3], NULL
, 10);
226 do_mem_md (NULL
, 0, 3, argv
);
228 /* delay for <count> ms... */
229 /* TODO: use timer */
230 for (i
=0; i
<count
; i
++)
233 /* check for ctrl-c to abort... */
235 my_puts_P(PSTR("Abort\n"));
236 return CMD_RET_SUCCESS
;
240 return CMD_RET_SUCCESS
;
243 command_ret_t
do_mem_mwc ( cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
249 return CMD_RET_USAGE
;
251 count
= strtoul(argv
[3], NULL
, 10);
254 do_mem_mw (NULL
, 0, 3, argv
);
256 /* delay for <count> ms... */
257 /* TODO: use timer */
258 for (i
=0; i
<count
; i
++)
261 /* check for ctrl-c to abort... */
263 my_puts_P(PSTR("Abort\n"));
264 return CMD_RET_SUCCESS
;
268 return CMD_RET_SUCCESS
;
270 #endif /* CONFIG_MX_CYCLIC */
272 command_ret_t
do_mem_cmp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
274 uint32_t addr1
, addr2
, count
, ngood
;
275 command_ret_t rcode
= CMD_RET_SUCCESS
;
276 uint8_t byte1
, byte2
;
282 return CMD_RET_USAGE
;
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);
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
;
306 /* check for ctrl-c to abort... */
308 my_puts_P(PSTR("Abort\n"));
309 return CMD_RET_SUCCESS
;
313 printf_P(PSTR("Total of %ld byte(s) (0x%lx) were the same\n"), ngood
, ngood
);
317 command_ret_t
do_mem_cp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
319 uint32_t src
, dest
, count
;
326 return CMD_RET_USAGE
;
328 src
= strtoul(argv
[1], NULL
, 16);
330 dest
= strtoul(argv
[2], NULL
, 16);
331 dest
+= base_address
;
332 count
= strtoul(argv
[3], NULL
, 16);
335 my_puts_P(PSTR("Zero length?\n"));
336 return CMD_RET_FAILURE
;
346 while (count
-- > 0) {
348 z80_bus_cmd(Request
);
349 data
= z80_read(src
);
350 z80_write(dest
, data
);
351 z80_bus_cmd(Release
);
355 /* check for ctrl-c to abort... */
357 my_puts_P(PSTR("Abort\n"));
358 return CMD_RET_SUCCESS
;
361 return CMD_RET_SUCCESS
;
364 command_ret_t
do_mem_base(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
371 /* Set new base address. */
372 base_address
= strtoul(argv
[1], NULL
, 16);
374 /* Print the current base address. */
375 printf_P(PSTR("Base Address: 0x%05lx\n"), base_address
);
376 return CMD_RET_SUCCESS
;
379 command_ret_t
do_mem_loop(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
382 uint32_t addr
, length
;
388 return CMD_RET_USAGE
;
390 /* Address is always specified. */
391 addr
= strtoul(argv
[1], NULL
, 16);
393 /* Length is the number of bytes. */
394 length
= strtoul(argv
[2], NULL
, 16);
397 /* We want to optimize the loops to run as fast as possible.
398 * If we have only one object, just run infinite loops.
401 z80_bus_cmd(Request
);
404 z80_bus_cmd(Release
);
407 z80_bus_cmd(Request
);
414 z80_bus_cmd(Release
);
416 return CMD_RET_SUCCESS
;
420 command_ret_t
do_mem_loopw (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
422 uint32_t addr
, length
;
429 return CMD_RET_USAGE
;
431 /* Address is always specified. */
432 addr
= strtoul(argv
[1], NULL
, 16);
434 /* Length is the number of bytes. */
435 length
= strtoul(argv
[2], NULL
, 16);
437 data
= strtoul(argv
[3], NULL
, 16);
439 /* We want to optimize the loops to run as fast as possible.
440 * If we have only one object, just run infinite loops.
443 z80_bus_cmd(Request
);
445 z80_write(addr
, data
);
452 z80_write(p
++, data
);
455 #endif /* CONFIG_LOOPW */
457 #ifdef CONFIG_CMD_MEMTEST
458 static uint32_t mem_test_alt(vu_long
*buf
, uint32_t start_addr
, uint32_t end_addr
,
463 uint32_t val
, readback
;
469 vu_long anti_pattern
;
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 */
482 num_words
= (end_addr
- start_addr
) / sizeof(vu_long
);
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
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).
502 for (j
= 0; j
< sizeof(bitpattern
) / sizeof(bitpattern
[0]); j
++) {
504 for (; val
!= 0; val
<<= 1) {
506 *dummy
= ~val
; /* clear the test data off the bus */
508 if (readback
!= val
) {
509 printf_P(PSTR("FAILURE (data line): "
510 "expected %05lx, actual %05lx\n"),
519 if (readback
!= ~val
) {
520 printf_P(PSTR("FAILURE (data line): "
521 "Is %05lx, should be %05lx\n"),
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.
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.
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
562 * Returns: 0 if the test succeeds, 1 if the test fails.
564 pattern
= (vu_long
) 0xaaaaaaaa;
565 anti_pattern
= (vu_long
) 0x55555555;
567 debug("%s:%d: length = 0x%.5lx\n", __func__
, __LINE__
, num_words
);
569 * Write the default pattern at each of the
570 * power-of-two offsets.
572 for (offset
= 1; offset
< num_words
; offset
<<= 1)
573 addr
[offset
] = pattern
;
576 * Check for address bits stuck high.
579 addr
[test_offset
] = anti_pattern
;
581 for (offset
= 1; offset
< num_words
; offset
<<= 1) {
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
),
593 addr
[test_offset
] = pattern
;
596 * Check for addr bits stuck low or shorted.
598 for (test_offset
= 1; test_offset
< num_words
; test_offset
<<= 1) {
599 addr
[test_offset
] = anti_pattern
;
601 for (offset
= 1; offset
< num_words
; offset
<<= 1) {
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
),
614 addr
[test_offset
] = pattern
;
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.
627 * Returns: 0 if the test succeeds, 1 if the test fails.
632 * Fill memory with a known pattern.
634 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, offset
++) {
635 addr
[offset
] = pattern
;
639 * Check each location and invert it for the second pass.
641 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, 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
),
653 anti_pattern
= ~pattern
;
654 addr
[offset
] = anti_pattern
;
658 * Check each location for the inverted pattern and zero it.
660 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, offset
++) {
662 anti_pattern
= ~pattern
;
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
),
679 static uint32_t mem_test_quick(vu_long
*buf
, uint32_t start_addr
, uint32_t end_addr
,
680 vu_long pattern
, int iteration
)
685 uint32_t incr
, length
;
686 uint32_t val
, readback
;
688 /* Alternate the pattern */
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.
698 if (pattern
& 0x80000000)
699 pattern
= -pattern
; /* complement & increment */
703 length
= (end_addr
- start_addr
) / sizeof(uint32_t);
705 printf_P(PSTR("\rPattern %08lX Writing..."
707 "\b\b\b\b\b\b\b\b\b\b"),
710 for (addr
= buf
, val
= pattern
; addr
< end
; addr
++) {
715 my_puts_P(PSTR("Reading..."));
717 for (addr
= buf
, val
= pattern
; addr
< end
; addr
++) {
719 if (readback
!= val
) {
720 uint32_t offset
= addr
- buf
;
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
)),
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.
741 command_ret_t
do_mem_mtest(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
745 vu_long
*buf
, *dummy
;
747 /* TODO: command_ret_t */
749 uint32_t errs
= 0; /* number of errors, or -1 if interrupted */
752 #if defined(CONFIG_SYS_ALT_MEMTEST)
753 const int alt_test
= 1;
755 const int alt_test
= 0;
759 start
= strtoul(argv
[1], NULL
, 16);
761 start
= CONFIG_SYS_MEMTEST_START
;
764 end
= strtoul(argv
[2], NULL
, 16);
766 end
= CONFIG_SYS_MEMTEST_END
;
769 pattern
= (uint32_t)strtoul(argv
[3], NULL
, 16);
774 iteration_limit
= (uint32_t)strtoul(argv
[4], NULL
, 16);
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__
,
783 // buf = map_sysmem(start, end - start);
784 // dummy = map_sysmem(CONFIG_SYS_MEMTEST_SCRATCH, sizeof(vu_long));
786 !iteration_limit
|| iteration
< iteration_limit
;
793 printf_P(PSTR("Iteration: %6d\r"), iteration
+ 1);
796 errs
= mem_test_alt(buf
, start
, end
, dummy
);
798 errs
= mem_test_quick(buf
, start
, end
, pattern
,
806 /* Memory test was aborted - write a newline to finish off */
810 printf_P(PSTR("Tested %d iteration(s) with %lu errors.\n"),
815 return ret
; /* not reached */
817 #endif /* CONFIG_CMD_MEMTEST */