3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 * Copied from FADS ROM, Dan Malek (dmalek@jlc.net)
34 #if (CONFIG_COMMANDS & (CFG_CMD_MEMORY | CFG_CMD_PCI | CFG_CMD_I2C))
35 int cmd_get_data_size(char* arg, int default_size)
37 /* Check for a size specification .b, .w or .l.
39 int len = strlen(arg);
40 if (len > 2 && arg[len-2] == '.') {
54 #if (CONFIG_COMMANDS & CFG_CMD_MEMORY)
57 #define PRINTF(fmt,args...) printf (fmt ,##args)
59 #define PRINTF(fmt,args...)
62 static int mod_mem(cmd_tbl_t *, int, int, int, char *[]);
64 /* Display values from last command.
65 * Memory modify remembered values are different from display memory.
67 uint dp_last_addr, dp_last_size;
68 uint dp_last_length = 0x40;
69 uint mm_last_addr, mm_last_size;
71 static ulong base_address = 0;
76 * md{.b, .w, .l} {addr} {len}
78 #define DISP_LINE_LEN 16
79 int do_mem_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
81 ulong addr, size, length;
82 ulong i, nbytes, linebytes;
86 /* We use the last specified parameters, unless new ones are
91 length = dp_last_length;
94 printf ("Usage:\n%s\n", cmdtp->usage);
98 if ((flag & CMD_FLAG_REPEAT) == 0) {
99 /* New command specified. Check for a size specification.
100 * Defaults to long if no or incorrect specification.
102 size = cmd_get_data_size(argv[0], 4);
104 /* Address is specified since argc > 1
106 addr = simple_strtoul(argv[1], NULL, 16);
107 addr += base_address;
109 /* If another parameter, it is the length to display.
110 * Length is the number of objects, not number of bytes.
113 length = simple_strtoul(argv[2], NULL, 16);
118 * We buffer all read data, so we can make sure data is read only
119 * once, and all accesses are with the specified bus width.
121 nbytes = length * size;
123 char linebuf[DISP_LINE_LEN];
124 uint *uip = (uint *)linebuf;
125 ushort *usp = (ushort *)linebuf;
126 u_char *ucp = (u_char *)linebuf;
128 printf("%08lx:", addr);
129 linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
130 for (i=0; i<linebytes; i+= size) {
132 printf(" %08x", (*uip++ = *((uint *)addr)));
133 } else if (size == 2) {
134 printf(" %04x", (*usp++ = *((ushort *)addr)));
136 printf(" %02x", (*ucp++ = *((u_char *)addr)));
142 for (i=0; i<linebytes; i++) {
143 if ((*cp < 0x20) || (*cp > 0x7e))
155 } while (nbytes > 0);
158 dp_last_length = length;
163 int do_mem_mm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
165 return mod_mem (cmdtp, 1, flag, argc, argv);
167 int do_mem_nm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
169 return mod_mem (cmdtp, 0, flag, argc, argv);
172 int do_mem_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
174 ulong addr, size, writeval, count;
176 if ((argc < 3) || (argc > 4)) {
177 printf ("Usage:\n%s\n", cmdtp->usage);
181 /* Check for size specification.
183 size = cmd_get_data_size(argv[0], 4);
185 /* Address is specified since argc > 1
187 addr = simple_strtoul(argv[1], NULL, 16);
188 addr += base_address;
190 /* Get the value to write.
192 writeval = simple_strtoul(argv[2], NULL, 16);
196 count = simple_strtoul(argv[3], NULL, 16);
201 while (count-- > 0) {
203 *((ulong *)addr) = (ulong )writeval;
205 *((ushort *)addr) = (ushort)writeval;
207 *((u_char *)addr) = (u_char)writeval;
213 int do_mem_cmp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
215 ulong size, addr1, addr2, count, ngood;
219 printf ("Usage:\n%s\n", cmdtp->usage);
223 /* Check for size specification.
225 size = cmd_get_data_size(argv[0], 4);
227 addr1 = simple_strtoul(argv[1], NULL, 16);
228 addr1 += base_address;
230 addr2 = simple_strtoul(argv[2], NULL, 16);
231 addr2 += base_address;
233 count = simple_strtoul(argv[3], NULL, 16);
237 while (count-- > 0) {
239 ulong word1 = *(ulong *)addr1;
240 ulong word2 = *(ulong *)addr2;
241 if (word1 != word2) {
242 printf("word at 0x%08lx (0x%08lx) "
243 "!= word at 0x%08lx (0x%08lx)\n",
244 addr1, word1, addr2, word2);
249 else if (size == 2) {
250 ushort hword1 = *(ushort *)addr1;
251 ushort hword2 = *(ushort *)addr2;
252 if (hword1 != hword2) {
253 printf("halfword at 0x%08lx (0x%04x) "
254 "!= halfword at 0x%08lx (0x%04x)\n",
255 addr1, hword1, addr2, hword2);
261 u_char byte1 = *(u_char *)addr1;
262 u_char byte2 = *(u_char *)addr2;
263 if (byte1 != byte2) {
264 printf("byte at 0x%08lx (0x%02x) "
265 "!= byte at 0x%08lx (0x%02x)\n",
266 addr1, byte1, addr2, byte2);
276 printf("Total of %ld %s%s were the same\n",
277 ngood, size == 4 ? "word" : size == 2 ? "halfword" : "byte",
278 ngood == 1 ? "" : "s");
282 int do_mem_cp ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
284 ulong addr, size, dest, count;
287 printf ("Usage:\n%s\n", cmdtp->usage);
291 /* Check for size specification.
293 size = cmd_get_data_size(argv[0], 4);
295 addr = simple_strtoul(argv[1], NULL, 16);
296 addr += base_address;
298 dest = simple_strtoul(argv[2], NULL, 16);
299 dest += base_address;
301 count = simple_strtoul(argv[3], NULL, 16);
304 puts ("Zero length ???\n");
309 /* check if we are copying to Flash */
310 if (addr2info(dest) != NULL) {
313 printf ("Copy to Flash... ");
315 rc = flash_write ((uchar *)addr, dest, count*size);
325 while (count-- > 0) {
327 *((ulong *)dest) = *((ulong *)addr);
329 *((ushort *)dest) = *((ushort *)addr);
331 *((u_char *)dest) = *((u_char *)addr);
338 int do_mem_base (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
341 /* Set new base address.
343 base_address = simple_strtoul(argv[1], NULL, 16);
345 /* Print the current base address.
347 printf("Base Address: 0x%08lx\n", base_address);
351 int do_mem_loop (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
353 ulong addr, size, length, i, junk;
354 volatile uint *longp;
355 volatile ushort *shortp;
359 printf ("Usage:\n%s\n", cmdtp->usage);
363 /* Check for a size spefication.
364 * Defaults to long if no or incorrect specification.
366 size = cmd_get_data_size(argv[0], 4);
368 /* Address is always specified.
370 addr = simple_strtoul(argv[1], NULL, 16);
372 /* Length is the number of objects, not number of bytes.
374 length = simple_strtoul(argv[2], NULL, 16);
376 /* We want to optimize the loops to run as fast as possible.
377 * If we have only one object, just run infinite loops.
381 longp = (uint *)addr;
386 shortp = (ushort *)addr;
397 longp = (uint *)addr;
405 shortp = (ushort *)addr;
420 * Perform a memory test. A more complete alternative test can be
421 * configured using CFG_ALT_MEMTEST. The complete test loops until
422 * interrupted by ctrl-c or by a failure of one of the sub-tests.
424 int do_mem_mtest (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
426 vu_long *addr, *start, *end;
430 #if defined(CFG_ALT_MEMTEST)
436 vu_long anti_pattern;
438 vu_long *dummy = NULL;
442 static const ulong bitpattern[] = {
443 0x00000001, /* single bit */
444 0x00000003, /* two adjacent bits */
445 0x00000007, /* three adjacent bits */
446 0x0000000F, /* four adjacent bits */
447 0x00000005, /* two non-adjacent bits */
448 0x00000015, /* three non-adjacent bits */
449 0x00000055, /* four non-adjacent bits */
450 0xaaaaaaaa, /* alternating 1/0 */
459 start = (ulong *)simple_strtoul(argv[1], NULL, 16);
461 start = (ulong *)CFG_MEMTEST_START;
465 end = (ulong *)simple_strtoul(argv[2], NULL, 16);
467 end = (ulong *)(CFG_MEMTEST_END);
471 pattern = (ulong)simple_strtoul(argv[3], NULL, 16);
476 #if defined(CFG_ALT_MEMTEST)
477 printf ("Testing %08x ... %08x:\n", (uint)start, (uint)end);
478 PRINTF("%s:%d: start 0x%p end 0x%p\n",
479 __FUNCTION__, __LINE__, start, end);
487 printf("Iteration: %6d\r", iterations);
488 PRINTF("Iteration: %6d\n", iterations);
492 * Data line test: write a pattern to the first
493 * location, write the 1's complement to a 'parking'
494 * address (changes the state of the data bus so a
495 * floating bus doen't give a false OK), and then
496 * read the value back. Note that we read it back
497 * into a variable because the next time we read it,
498 * it might be right (been there, tough to explain to
499 * the quality guys why it prints a failure when the
500 * "is" and "should be" are obviously the same in the
503 * Rather than exhaustively testing, we test some
504 * patterns by shifting '1' bits through a field of
505 * '0's and '0' bits through a field of '1's (i.e.
506 * pattern and ~pattern).
509 for (j = 0; j < sizeof(bitpattern)/sizeof(bitpattern[0]); j++) {
511 for(; val != 0; val <<= 1) {
513 *dummy = ~val; /* clear the test data off of the bus */
515 if(readback != val) {
516 printf ("FAILURE (data line): "
517 "expected %08lx, actual %08lx\n",
523 if(readback != ~val) {
524 printf ("FAILURE (data line): "
525 "Is %08lx, should be %08lx\n",
532 * Based on code whose Original Author and Copyright
533 * information follows: Copyright (c) 1998 by Michael
534 * Barr. This software is placed into the public
535 * domain and may be used for any purpose. However,
536 * this notice must not be changed or removed and no
537 * warranty is either expressed or implied by its
538 * publication or distribution.
544 * Description: Test the address bus wiring in a
545 * memory region by performing a walking
546 * 1's test on the relevant bits of the
547 * address and checking for aliasing.
548 * This test will find single-bit
549 * address failures such as stuck -high,
550 * stuck-low, and shorted pins. The base
551 * address and size of the region are
552 * selected by the caller.
554 * Notes: For best results, the selected base
555 * address should have enough LSB 0's to
556 * guarantee single address bit changes.
557 * For example, to test a 64-Kbyte
558 * region, select a base address on a
559 * 64-Kbyte boundary. Also, select the
560 * region size as a power-of-two if at
563 * Returns: 0 if the test succeeds, 1 if the test fails.
565 * ## NOTE ## Be sure to specify start and end
566 * addresses such that addr_mask has
567 * lots of bits set. For example an
568 * address range of 01000000 02000000 is
569 * bad while a range of 01000000
570 * 01ffffff is perfect.
572 addr_mask = ((ulong)end - (ulong)start)/sizeof(vu_long);
573 pattern = (vu_long) 0xaaaaaaaa;
574 anti_pattern = (vu_long) 0x55555555;
576 PRINTF("%s:%d: addr mask = 0x%.8lx\n",
577 __FUNCTION__, __LINE__,
580 * Write the default pattern at each of the
581 * power-of-two offsets.
583 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
584 start[offset] = pattern;
588 * Check for address bits stuck high.
591 start[test_offset] = anti_pattern;
593 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
594 temp = start[offset];
595 if (temp != pattern) {
596 printf ("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
597 " expected 0x%.8lx, actual 0x%.8lx\n",
598 (ulong)&start[offset], pattern, temp);
602 start[test_offset] = pattern;
605 * Check for addr bits stuck low or shorted.
607 for (test_offset = 1; (test_offset & addr_mask) != 0; test_offset <<= 1) {
608 start[test_offset] = anti_pattern;
610 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
611 temp = start[offset];
612 if ((temp != pattern) && (offset != test_offset)) {
613 printf ("\nFAILURE: Address bit stuck low or shorted @"
614 " 0x%.8lx: expected 0x%.8lx, actual 0x%.8lx\n",
615 (ulong)&start[offset], pattern, temp);
619 start[test_offset] = pattern;
623 * Description: Test the integrity of a physical
624 * memory device by performing an
625 * increment/decrement test over the
626 * entire region. In the process every
627 * storage bit in the device is tested
628 * as a zero and a one. The base address
629 * and the size of the region are
630 * selected by the caller.
632 * Returns: 0 if the test succeeds, 1 if the test fails.
634 num_words = ((ulong)end - (ulong)start)/sizeof(vu_long) + 1;
637 * Fill memory with a known pattern.
639 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
640 start[offset] = pattern;
644 * Check each location and invert it for the second pass.
646 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
647 temp = start[offset];
648 if (temp != pattern) {
649 printf ("\nFAILURE (read/write) @ 0x%.8lx:"
650 " expected 0x%.8lx, actual 0x%.8lx)\n",
651 (ulong)&start[offset], pattern, temp);
655 anti_pattern = ~pattern;
656 start[offset] = anti_pattern;
660 * Check each location for the inverted pattern and zero it.
662 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
663 anti_pattern = ~pattern;
664 temp = start[offset];
665 if (temp != anti_pattern) {
666 printf ("\nFAILURE (read/write): @ 0x%.8lx:"
667 " expected 0x%.8lx, actual 0x%.8lx)\n",
668 (ulong)&start[offset], anti_pattern, temp);
675 #else /* The original, quickie test */
683 printf ("\rPattern %08lX Writing..."
685 "\b\b\b\b\b\b\b\b\b\b",
688 for (addr=start,val=pattern; addr<end; addr++) {
693 printf("Reading...");
695 for (addr=start,val=pattern; addr<end; addr++) {
697 if (readback != val) {
698 printf ("\nMem error @ 0x%08X: "
699 "found %08lX, expected %08lX\n",
700 (uint)addr, readback, val);
707 * Flip the pattern each time to make lots of zeros and
708 * then, the next time, lots of ones. We decrement
709 * the "negative" patterns and increment the "positive"
710 * patterns to preserve this feature.
712 if(pattern & 0x80000000) {
713 pattern = -pattern; /* complement & increment */
728 * mm{.b, .w, .l} {addr}
729 * nm{.b, .w, .l} {addr}
732 mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[])
736 extern char console_buffer[];
739 printf ("Usage:\n%s\n", cmdtp->usage);
743 #ifdef CONFIG_BOOT_RETRY_TIME
744 reset_cmd_timeout(); /* got a good command to get here */
746 /* We use the last specified parameters, unless new ones are
752 if ((flag & CMD_FLAG_REPEAT) == 0) {
753 /* New command specified. Check for a size specification.
754 * Defaults to long if no or incorrect specification.
756 size = cmd_get_data_size(argv[0], 4);
758 /* Address is specified since argc > 1
760 addr = simple_strtoul(argv[1], NULL, 16);
761 addr += base_address;
764 /* Print the address, followed by value. Then accept input for
765 * the next value. A non-converted value exits.
768 printf("%08lx:", addr);
770 printf(" %08x", *((uint *)addr));
772 printf(" %04x", *((ushort *)addr));
774 printf(" %02x", *((u_char *)addr));
776 nbytes = readline (" ? ");
777 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
778 /* <CR> pressed as only input, don't modify current
779 * location and move to next. "-" pressed will go back.
782 addr += nbytes ? -size : size;
784 #ifdef CONFIG_BOOT_RETRY_TIME
785 reset_cmd_timeout(); /* good enough to not time out */
788 #ifdef CONFIG_BOOT_RETRY_TIME
789 else if (nbytes == -2) {
790 break; /* timed out, exit the command */
795 i = simple_strtoul(console_buffer, &endp, 16);
796 nbytes = endp - console_buffer;
798 #ifdef CONFIG_BOOT_RETRY_TIME
799 /* good enough to not time out
806 *((ushort *)addr) = i;
808 *((u_char *)addr) = i;
820 int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
827 printf ("Usage:\n%s\n", cmdtp->usage);
831 addr = simple_strtoul(argv[1], NULL, 16);
832 addr += base_address;
834 length = simple_strtoul(argv[2], NULL, 16);
836 crc = crc32 (0, (const uchar *)addr, length);
838 printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
839 addr, addr + length -1, crc);
843 ptr = (ulong *)simple_strtoul(argv[3], NULL, 16);
850 #endif /* CFG_CMD_MEMORY */