]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_mem.c
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * SPDX-License-Identifier: GPL-2.0+
13 * Copied from FADS ROM, Dan Malek (dmalek@jlc.net)
21 #include "cli_readline.h"
22 #include "print-utils.h"
23 #include "con-utils.h"
28 #ifndef CONFIG_SYS_MEMTEST_SCRATCH
29 #define CONFIG_SYS_MEMTEST_SCRATCH 0
32 /* Display values from last command.
33 * Memory modify remembered values are different from display memory.
35 static uint32_t dp_last_addr
;
36 static uint32_t dp_last_length
= 0x100;
37 static uint32_t mm_last_addr
;
39 static uint32_t base_address
= 0;
41 /*--------------------------------------------------------------------------*/
44 void z180_read_buf(uint8_t *buf
, uint32_t addr
, uint8_t count
)
46 if (z80_bus_cmd(Request
) & ZST_ACQUIRED
) {
47 z80_read_block (buf
, addr
, count
);
52 /*--------------------------------------------------------------------------*/
59 command_ret_t
do_mem_md(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
61 uint32_t addr
, length
;
66 printf_P(PSTR("flag: %d, argc: %d"), flag
, argc
);
67 for (int i
= 0; i
< argc
; i
++) {
68 printf_P(PSTR(", argv[%d]: %s"), i
, argv
[i
] ? argv
[i
] : "<NULL>");
73 /* We use the last specified parameters, unless new ones are
77 length
= dp_last_length
;
82 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
83 /* Address is specified since argc > 1 */
84 addr
= strtoul(argv
[1], NULL
, 16);
87 /* If another parameter, it is the length to display. */
89 length
= strtoul(argv
[2], NULL
, 16);
92 /* Print the lines. */
93 dump_mem(addr
, addr
, length
, z180_read_buf
, NULL
);
95 dp_last_addr
= addr
+ length
;
96 dp_last_length
= length
;
97 return CMD_RET_SUCCESS
;
107 mod_mem(cmd_tbl_t
*cmdtp
, int incrflag
, int flag
, int argc
, char * const argv
[])
116 return CMD_RET_USAGE
;
118 /* We use the last specified parameters, unless new ones are
123 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
124 /* New command specified.
127 /* Address is specified since argc > 1
129 addr
= strtoul(argv
[1], NULL
, 16);
130 addr
+= base_address
;
133 /* Print the address, followed by value. Then accept input for
134 * the next value. A non-converted value exits.
137 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
138 my_puts_P(PSTR("Bus timeout\n"));
139 return CMD_RET_FAILURE
;
141 data
= z80_read(addr
);
142 z80_bus_cmd(Release
);
143 printf_P(PSTR("%05lx: %02x"), addr
, data
);
145 nbytes
= cli_readline(PSTR(" ? "));
146 if (nbytes
== 0 || (nbytes
== 1 && console_buffer
[0] == '-')) {
147 /* <CR> pressed as only input, don't modify current
148 * location and move to next. "-" pressed will go back.
151 addr
+= nbytes
? -1 : 1;
156 data
= strtoul(console_buffer
, &endp
, 16);
157 nbytes
= endp
- console_buffer
;
159 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
160 my_puts_P(PSTR("Bus timeout\n"));
161 return CMD_RET_FAILURE
;
163 z80_write(addr
, data
);
164 z80_bus_cmd(Release
);
172 return CMD_RET_SUCCESS
;
176 command_ret_t
do_mem_mm(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
178 return mod_mem (cmdtp
, 1, flag
, argc
, argv
);
180 command_ret_t
do_mem_nm(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
182 return mod_mem (cmdtp
, 0, flag
, argc
, argv
);
185 command_ret_t
do_mem_mw(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
188 uint32_t addr
, count
;
193 if ((argc
< 3) || (argc
> 4))
194 return CMD_RET_USAGE
;
196 /* Address is specified since argc > 1
198 addr
= strtoul(argv
[1], NULL
, 16);
199 addr
+= base_address
;
201 /* Get the value to write.
203 writeval
= (uint8_t) strtoul(argv
[2], NULL
, 16);
207 count
= strtoul(argv
[3], NULL
, 16);
212 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
213 my_puts_P(PSTR("Bus timeout\n"));
214 return CMD_RET_FAILURE
;
216 z80_memset(addr
, writeval
, count
);
217 z80_bus_cmd(Release
);
219 return CMD_RET_SUCCESS
;
222 #ifdef CONFIG_MX_CYCLIC
223 command_ret_t
do_mem_mdc ( cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
229 return CMD_RET_USAGE
;
231 count
= strtoul(argv
[3], NULL
, 10);
234 do_mem_md (NULL
, 0, 3, argv
);
236 /* delay for <count> ms... */
237 /* TODO: use timer */
238 for (i
=0; i
<count
; i
++)
241 /* check for ctrl-c to abort... */
243 my_puts_P(PSTR("Abort\n"));
244 return CMD_RET_SUCCESS
;
248 return CMD_RET_SUCCESS
;
251 command_ret_t
do_mem_mwc ( cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
257 return CMD_RET_USAGE
;
259 count
= strtoul(argv
[3], NULL
, 10);
262 do_mem_mw (NULL
, 0, 3, argv
);
264 /* delay for <count> ms... */
265 /* TODO: use timer */
266 for (i
=0; i
<count
; i
++)
269 /* check for ctrl-c to abort... */
271 my_puts_P(PSTR("Abort\n"));
272 return CMD_RET_SUCCESS
;
276 return CMD_RET_SUCCESS
;
278 #endif /* CONFIG_MX_CYCLIC */
280 command_ret_t
do_mem_cmp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
282 uint32_t addr1
, addr2
, count
, ngood
;
283 command_ret_t rcode
= CMD_RET_SUCCESS
;
284 uint8_t byte1
, byte2
;
290 return CMD_RET_USAGE
;
293 addr1
= strtoul(argv
[1], NULL
, 16);
294 addr1
+= base_address
;
295 addr2
= strtoul(argv
[2], NULL
, 16);
296 addr2
+= base_address
;
297 count
= strtoul(argv
[3], NULL
, 16);
299 for (ngood
= 0; ngood
< count
; ++ngood
) {
300 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
301 my_puts_P(PSTR("Bus timeout\n"));
302 rcode
= CMD_RET_FAILURE
;
305 byte1
= z80_read(addr1
);
306 byte2
= z80_read(addr2
);
307 z80_bus_cmd(Release
);
308 if (byte1
!= byte2
) {
309 printf_P(PSTR("byte at 0x%05lx (%#02x) != "
310 "byte at 0x%05lx (%#02x)\n"),
311 addr1
, byte1
, addr2
, byte2
);
312 rcode
= CMD_RET_FAILURE
;
318 /* check for ctrl-c to abort... */
320 my_puts_P(PSTR("Abort\n"));
321 return CMD_RET_SUCCESS
;
325 printf_P(PSTR("Total of %ld byte(s) (0x%lx) were the same\n"), ngood
, ngood
);
329 command_ret_t
do_mem_cp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
331 uint32_t src
, dest
, count
;
338 return CMD_RET_USAGE
;
340 src
= strtoul(argv
[1], NULL
, 16);
342 dest
= strtoul(argv
[2], NULL
, 16);
343 dest
+= base_address
;
344 count
= strtoul(argv
[3], NULL
, 16);
347 my_puts_P(PSTR("Zero length?\n"));
348 return CMD_RET_FAILURE
;
358 while (count
-- > 0) {
360 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
361 my_puts_P(PSTR("Bus timeout\n"));
362 return CMD_RET_FAILURE
;
364 data
= z80_read(src
);
365 z80_write(dest
, data
);
366 z80_bus_cmd(Release
);
370 /* check for ctrl-c to abort... */
372 my_puts_P(PSTR("Abort\n"));
373 return CMD_RET_SUCCESS
;
376 return CMD_RET_SUCCESS
;
379 command_ret_t
do_mem_base(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
386 /* Set new base address. */
387 base_address
= strtoul(argv
[1], NULL
, 16);
389 /* Print the current base address. */
390 printf_P(PSTR("Base Address: 0x%05lx\n"), base_address
);
391 return CMD_RET_SUCCESS
;
394 command_ret_t
do_mem_loop(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
397 uint32_t addr
, length
;
403 return CMD_RET_USAGE
;
405 /* Address is always specified. */
406 addr
= strtoul(argv
[1], NULL
, 16);
408 /* Length is the number of bytes. */
409 length
= strtoul(argv
[2], NULL
, 16);
412 /* We want to optimize the loops to run as fast as possible.
413 * If we have only one object, just run infinite loops.
416 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
417 my_puts_P(PSTR("Bus timeout\n"));
418 return CMD_RET_FAILURE
;
422 z80_bus_cmd(Release
);
425 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
426 my_puts_P(PSTR("Bus timeout\n"));
427 return CMD_RET_FAILURE
;
435 z80_bus_cmd(Release
);
437 return CMD_RET_SUCCESS
;
441 command_ret_t
do_mem_loopw (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
443 uint32_t addr
, length
;
450 return CMD_RET_USAGE
;
452 /* Address is always specified. */
453 addr
= strtoul(argv
[1], NULL
, 16);
455 /* Length is the number of bytes. */
456 length
= strtoul(argv
[2], NULL
, 16);
458 data
= strtoul(argv
[3], NULL
, 16);
460 /* We want to optimize the loops to run as fast as possible.
461 * If we have only one object, just run infinite loops.
464 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
465 my_puts_P(PSTR("Bus timeout\n"));
466 return CMD_RET_FAILURE
;
469 z80_write(addr
, data
);
472 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
473 my_puts_P(PSTR("Bus timeout\n"));
474 return CMD_RET_FAILURE
;
480 z80_write(p
++, data
);
483 #endif /* CONFIG_LOOPW */
485 #ifdef CONFIG_CMD_MEMTEST
486 static uint32_t mem_test_alt(vu_long
*buf
, uint32_t start_addr
, uint32_t end_addr
,
491 uint32_t val
, readback
;
497 vu_long anti_pattern
;
499 static const FLASH
uint32_t bitpattern
[] = {
500 0x00000001, /* single bit */
501 0x00000003, /* two adjacent bits */
502 0x00000007, /* three adjacent bits */
503 0x0000000F, /* four adjacent bits */
504 0x00000005, /* two non-adjacent bits */
505 0x00000015, /* three non-adjacent bits */
506 0x00000055, /* four non-adjacent bits */
507 0xaaaaaaaa, /* alternating 1/0 */
510 num_words
= (end_addr
- start_addr
) / sizeof(vu_long
);
513 * Data line test: write a pattern to the first
514 * location, write the 1's complement to a 'parking'
515 * address (changes the state of the data bus so a
516 * floating bus doesn't give a false OK), and then
517 * read the value back. Note that we read it back
518 * into a variable because the next time we read it,
519 * it might be right (been there, tough to explain to
520 * the quality guys why it prints a failure when the
521 * "is" and "should be" are obviously the same in the
524 * Rather than exhaustively testing, we test some
525 * patterns by shifting '1' bits through a field of
526 * '0's and '0' bits through a field of '1's (i.e.
527 * pattern and ~pattern).
530 for (j
= 0; j
< sizeof(bitpattern
) / sizeof(bitpattern
[0]); j
++) {
532 for (; val
!= 0; val
<<= 1) {
534 *dummy
= ~val
; /* clear the test data off the bus */
536 if (readback
!= val
) {
537 printf_P(PSTR("FAILURE (data line): "
538 "expected %05lx, actual %05lx\n"),
547 if (readback
!= ~val
) {
548 printf_P(PSTR("FAILURE (data line): "
549 "Is %05lx, should be %05lx\n"),
559 * Based on code whose Original Author and Copyright
560 * information follows: Copyright (c) 1998 by Michael
561 * Barr. This software is placed into the public
562 * domain and may be used for any purpose. However,
563 * this notice must not be changed or removed and no
564 * warranty is either expressed or implied by its
565 * publication or distribution.
571 * Description: Test the address bus wiring in a
572 * memory region by performing a walking
573 * 1's test on the relevant bits of the
574 * address and checking for aliasing.
575 * This test will find single-bit
576 * address failures such as stuck-high,
577 * stuck-low, and shorted pins. The base
578 * address and size of the region are
579 * selected by the caller.
581 * Notes: For best results, the selected base
582 * address should have enough LSB 0's to
583 * guarantee single address bit changes.
584 * For example, to test a 64-Kbyte
585 * region, select a base address on a
586 * 64-Kbyte boundary. Also, select the
587 * region size as a power-of-two if at
590 * Returns: 0 if the test succeeds, 1 if the test fails.
592 pattern
= (vu_long
) 0xaaaaaaaa;
593 anti_pattern
= (vu_long
) 0x55555555;
595 debug("%s:%d: length = 0x%.5lx\n", __func__
, __LINE__
, num_words
);
597 * Write the default pattern at each of the
598 * power-of-two offsets.
600 for (offset
= 1; offset
< num_words
; offset
<<= 1)
601 addr
[offset
] = pattern
;
604 * Check for address bits stuck high.
607 addr
[test_offset
] = anti_pattern
;
609 for (offset
= 1; offset
< num_words
; offset
<<= 1) {
611 if (temp
!= pattern
) {
612 printf_P(PSTR("\nFAILURE: Address bit stuck high @ 0x%.5lx:"
613 " expected 0x%.5lx, actual 0x%.5lx\n"),
614 start_addr
+ offset
*sizeof(vu_long
),
621 addr
[test_offset
] = pattern
;
624 * Check for addr bits stuck low or shorted.
626 for (test_offset
= 1; test_offset
< num_words
; test_offset
<<= 1) {
627 addr
[test_offset
] = anti_pattern
;
629 for (offset
= 1; offset
< num_words
; offset
<<= 1) {
631 if ((temp
!= pattern
) && (offset
!= test_offset
)) {
632 printf_P(PSTR("\nFAILURE: Address bit stuck low or"
633 " shorted @ 0x%.5lx: expected 0x%.5lx,"
634 " actual 0x%.5lx\n"),
635 start_addr
+ offset
*sizeof(vu_long
),
642 addr
[test_offset
] = pattern
;
646 * Description: Test the integrity of a physical
647 * memory device by performing an
648 * increment/decrement test over the
649 * entire region. In the process every
650 * storage bit in the device is tested
651 * as a zero and a one. The base address
652 * and the size of the region are
653 * selected by the caller.
655 * Returns: 0 if the test succeeds, 1 if the test fails.
660 * Fill memory with a known pattern.
662 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, offset
++) {
663 addr
[offset
] = pattern
;
667 * Check each location and invert it for the second pass.
669 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, offset
++) {
671 if (temp
!= pattern
) {
672 printf_P(PSTR("\nFAILURE (read/write) @ 0x%.5lx:"
673 " expected 0x%.5lx, actual 0x%.5lx)\n"),
674 start_addr
+ offset
*sizeof(vu_long
),
681 anti_pattern
= ~pattern
;
682 addr
[offset
] = anti_pattern
;
686 * Check each location for the inverted pattern and zero it.
688 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, offset
++) {
690 anti_pattern
= ~pattern
;
692 if (temp
!= anti_pattern
) {
693 printf_P(PSTR("\nFAILURE (read/write): @ 0x%.5lx:"
694 " expected 0x%.5lx, actual 0x%.5lx)\n"),
695 start_addr
+ offset
*sizeof(vu_long
),
707 static uint32_t mem_test_quick(vu_long
*buf
, uint32_t start_addr
, uint32_t end_addr
,
708 vu_long pattern
, int iteration
)
713 uint32_t incr
, length
;
714 uint32_t val
, readback
;
716 /* Alternate the pattern */
721 * Flip the pattern each time to make lots of zeros and
722 * then, the next time, lots of ones. We decrement
723 * the "negative" patterns and increment the "positive"
724 * patterns to preserve this feature.
726 if (pattern
& 0x80000000)
727 pattern
= -pattern
; /* complement & increment */
731 length
= (end_addr
- start_addr
) / sizeof(uint32_t);
733 printf_P(PSTR("\rPattern %08lX Writing..."
735 "\b\b\b\b\b\b\b\b\b\b"),
738 for (addr
= buf
, val
= pattern
; addr
< end
; addr
++) {
743 my_puts_P(PSTR("Reading..."));
745 for (addr
= buf
, val
= pattern
; addr
< end
; addr
++) {
747 if (readback
!= val
) {
748 uint32_t offset
= addr
- buf
;
750 printf_P(PSTR("\nMem error @ 0x%08X: "
751 "found %08lX, expected %08lX\n"),
752 (unsigned int)(uintptr_t)(start_addr
+ offset
*sizeof(vu_long
)),
765 * Perform a memory test. A more complete alternative test can be
766 * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
767 * interrupted by ctrl-c or by a failure of one of the sub-tests.
769 command_ret_t
do_mem_mtest(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
773 vu_long
*buf
, *dummy
;
775 /* TODO: command_ret_t */
777 uint32_t errs
= 0; /* number of errors, or -1 if interrupted */
780 #if defined(CONFIG_SYS_ALT_MEMTEST)
781 const int alt_test
= 1;
783 const int alt_test
= 0;
787 start
= strtoul(argv
[1], NULL
, 16);
789 start
= CONFIG_SYS_MEMTEST_START
;
792 end
= strtoul(argv
[2], NULL
, 16);
794 end
= CONFIG_SYS_MEMTEST_END
;
797 pattern
= (uint32_t)strtoul(argv
[3], NULL
, 16);
802 iteration_limit
= (uint32_t)strtoul(argv
[4], NULL
, 16);
806 printf_P(PSTR("Testing %08x ... %08x:\n"), (unsigned int)start
, (unsigned int)end
);
807 debug("%s:%d: start %#05lx end %#05lx\n", __func__
, __LINE__
,
811 // buf = map_sysmem(start, end - start);
812 // dummy = map_sysmem(CONFIG_SYS_MEMTEST_SCRATCH, sizeof(vu_long));
814 !iteration_limit
|| iteration
< iteration_limit
;
821 printf_P(PSTR("Iteration: %6d\r"), iteration
+ 1);
824 errs
= mem_test_alt(buf
, start
, end
, dummy
);
826 errs
= mem_test_quick(buf
, start
, end
, pattern
,
834 /* Memory test was aborted - write a newline to finish off */
838 printf_P(PSTR("Tested %d iteration(s) with %lu errors.\n"),
843 return ret
; /* not reached */
845 #endif /* CONFIG_CMD_MEMTEST */