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)
33 #if (CONFIG_COMMANDS & CFG_CMD_MMC)
37 #if (CONFIG_COMMANDS & (CFG_CMD_MEMORY | CFG_CMD_PCI | CFG_CMD_I2C\
39 int cmd_get_data_size(char* arg, int default_size)
41 /* Check for a size specification .b, .w or .l.
43 int len = strlen(arg);
44 if (len > 2 && arg[len-2] == '.') {
58 #if (CONFIG_COMMANDS & CFG_CMD_MEMORY)
61 #define PRINTF(fmt,args...) printf (fmt ,##args)
63 #define PRINTF(fmt,args...)
66 static int mod_mem(cmd_tbl_t *, int, int, int, char *[]);
68 /* Display values from last command.
69 * Memory modify remembered values are different from display memory.
71 uint dp_last_addr, dp_last_size;
72 uint dp_last_length = 0x40;
73 uint mm_last_addr, mm_last_size;
75 static ulong base_address = 0;
80 * md{.b, .w, .l} {addr} {len}
82 #define DISP_LINE_LEN 16
83 int do_mem_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
85 ulong addr, size, length;
86 ulong i, nbytes, linebytes;
90 /* We use the last specified parameters, unless new ones are
95 length = dp_last_length;
98 printf ("Usage:\n%s\n", cmdtp->usage);
102 if ((flag & CMD_FLAG_REPEAT) == 0) {
103 /* New command specified. Check for a size specification.
104 * Defaults to long if no or incorrect specification.
106 size = cmd_get_data_size(argv[0], 4);
108 /* Address is specified since argc > 1
110 addr = simple_strtoul(argv[1], NULL, 16);
111 addr += base_address;
113 /* If another parameter, it is the length to display.
114 * Length is the number of objects, not number of bytes.
117 length = simple_strtoul(argv[2], NULL, 16);
122 * We buffer all read data, so we can make sure data is read only
123 * once, and all accesses are with the specified bus width.
125 nbytes = length * size;
127 char linebuf[DISP_LINE_LEN];
128 uint *uip = (uint *)linebuf;
129 ushort *usp = (ushort *)linebuf;
130 u_char *ucp = (u_char *)linebuf;
132 printf("%08lx:", addr);
133 linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
134 for (i=0; i<linebytes; i+= size) {
136 printf(" %08x", (*uip++ = *((uint *)addr)));
137 } else if (size == 2) {
138 printf(" %04x", (*usp++ = *((ushort *)addr)));
140 printf(" %02x", (*ucp++ = *((u_char *)addr)));
146 for (i=0; i<linebytes; i++) {
147 if ((*cp < 0x20) || (*cp > 0x7e))
159 } while (nbytes > 0);
162 dp_last_length = length;
167 int do_mem_mm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
169 return mod_mem (cmdtp, 1, flag, argc, argv);
171 int do_mem_nm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
173 return mod_mem (cmdtp, 0, flag, argc, argv);
176 int do_mem_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
178 ulong addr, size, writeval, count;
180 if ((argc < 3) || (argc > 4)) {
181 printf ("Usage:\n%s\n", cmdtp->usage);
185 /* Check for size specification.
187 size = cmd_get_data_size(argv[0], 4);
189 /* Address is specified since argc > 1
191 addr = simple_strtoul(argv[1], NULL, 16);
192 addr += base_address;
194 /* Get the value to write.
196 writeval = simple_strtoul(argv[2], NULL, 16);
200 count = simple_strtoul(argv[3], NULL, 16);
205 while (count-- > 0) {
207 *((ulong *)addr) = (ulong )writeval;
209 *((ushort *)addr) = (ushort)writeval;
211 *((u_char *)addr) = (u_char)writeval;
217 int do_mem_cmp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
219 ulong size, addr1, addr2, count, ngood;
223 printf ("Usage:\n%s\n", cmdtp->usage);
227 /* Check for size specification.
229 size = cmd_get_data_size(argv[0], 4);
231 addr1 = simple_strtoul(argv[1], NULL, 16);
232 addr1 += base_address;
234 addr2 = simple_strtoul(argv[2], NULL, 16);
235 addr2 += base_address;
237 count = simple_strtoul(argv[3], NULL, 16);
241 while (count-- > 0) {
243 ulong word1 = *(ulong *)addr1;
244 ulong word2 = *(ulong *)addr2;
245 if (word1 != word2) {
246 printf("word at 0x%08lx (0x%08lx) "
247 "!= word at 0x%08lx (0x%08lx)\n",
248 addr1, word1, addr2, word2);
253 else if (size == 2) {
254 ushort hword1 = *(ushort *)addr1;
255 ushort hword2 = *(ushort *)addr2;
256 if (hword1 != hword2) {
257 printf("halfword at 0x%08lx (0x%04x) "
258 "!= halfword at 0x%08lx (0x%04x)\n",
259 addr1, hword1, addr2, hword2);
265 u_char byte1 = *(u_char *)addr1;
266 u_char byte2 = *(u_char *)addr2;
267 if (byte1 != byte2) {
268 printf("byte at 0x%08lx (0x%02x) "
269 "!= byte at 0x%08lx (0x%02x)\n",
270 addr1, byte1, addr2, byte2);
280 printf("Total of %ld %s%s were the same\n",
281 ngood, size == 4 ? "word" : size == 2 ? "halfword" : "byte",
282 ngood == 1 ? "" : "s");
286 int do_mem_cp ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
288 ulong addr, size, dest, count;
291 printf ("Usage:\n%s\n", cmdtp->usage);
295 /* Check for size specification.
297 size = cmd_get_data_size(argv[0], 4);
299 addr = simple_strtoul(argv[1], NULL, 16);
300 addr += base_address;
302 dest = simple_strtoul(argv[2], NULL, 16);
303 dest += base_address;
305 count = simple_strtoul(argv[3], NULL, 16);
308 puts ("Zero length ???\n");
313 /* check if we are copying to Flash */
314 if (addr2info(dest) != NULL) {
317 printf ("Copy to Flash... ");
319 rc = flash_write ((uchar *)addr, dest, count*size);
329 #if (CONFIG_COMMANDS & CFG_CMD_MMC)
330 if (mmc2info(dest)) {
333 printf ("Copy to MMC... ");
334 switch (rc = mmc_write ((uchar *)addr, dest, count*size)) {
342 printf ("%s[%d] FIXME: rc=%d\n",__FILE__,__LINE__,rc);
349 if (mmc2info(addr)) {
352 printf ("Copy from MMC... ");
353 switch (rc = mmc_read (addr, (uchar *)dest, count*size)) {
361 printf ("%s[%d] FIXME: rc=%d\n",__FILE__,__LINE__,rc);
369 while (count-- > 0) {
371 *((ulong *)dest) = *((ulong *)addr);
373 *((ushort *)dest) = *((ushort *)addr);
375 *((u_char *)dest) = *((u_char *)addr);
382 int do_mem_base (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
385 /* Set new base address.
387 base_address = simple_strtoul(argv[1], NULL, 16);
389 /* Print the current base address.
391 printf("Base Address: 0x%08lx\n", base_address);
395 int do_mem_loop (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
397 ulong addr, size, length, i, junk;
398 volatile uint *longp;
399 volatile ushort *shortp;
403 printf ("Usage:\n%s\n", cmdtp->usage);
407 /* Check for a size spefication.
408 * Defaults to long if no or incorrect specification.
410 size = cmd_get_data_size(argv[0], 4);
412 /* Address is always specified.
414 addr = simple_strtoul(argv[1], NULL, 16);
416 /* Length is the number of objects, not number of bytes.
418 length = simple_strtoul(argv[2], NULL, 16);
420 /* We want to optimize the loops to run as fast as possible.
421 * If we have only one object, just run infinite loops.
425 longp = (uint *)addr;
430 shortp = (ushort *)addr;
441 longp = (uint *)addr;
449 shortp = (ushort *)addr;
464 * Perform a memory test. A more complete alternative test can be
465 * configured using CFG_ALT_MEMTEST. The complete test loops until
466 * interrupted by ctrl-c or by a failure of one of the sub-tests.
468 int do_mem_mtest (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
470 vu_long *addr, *start, *end;
474 #if defined(CFG_ALT_MEMTEST)
480 vu_long anti_pattern;
482 vu_long *dummy = NULL;
486 static const ulong bitpattern[] = {
487 0x00000001, /* single bit */
488 0x00000003, /* two adjacent bits */
489 0x00000007, /* three adjacent bits */
490 0x0000000F, /* four adjacent bits */
491 0x00000005, /* two non-adjacent bits */
492 0x00000015, /* three non-adjacent bits */
493 0x00000055, /* four non-adjacent bits */
494 0xaaaaaaaa, /* alternating 1/0 */
503 start = (ulong *)simple_strtoul(argv[1], NULL, 16);
505 start = (ulong *)CFG_MEMTEST_START;
509 end = (ulong *)simple_strtoul(argv[2], NULL, 16);
511 end = (ulong *)(CFG_MEMTEST_END);
515 pattern = (ulong)simple_strtoul(argv[3], NULL, 16);
520 #if defined(CFG_ALT_MEMTEST)
521 printf ("Testing %08x ... %08x:\n", (uint)start, (uint)end);
522 PRINTF("%s:%d: start 0x%p end 0x%p\n",
523 __FUNCTION__, __LINE__, start, end);
531 printf("Iteration: %6d\r", iterations);
532 PRINTF("Iteration: %6d\n", iterations);
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 doen'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 of the bus */
559 if(readback != val) {
560 printf ("FAILURE (data line): "
561 "expected %08lx, actual %08lx\n",
567 if(readback != ~val) {
568 printf ("FAILURE (data line): "
569 "Is %08lx, should be %08lx\n",
576 * Based on code whose Original Author and Copyright
577 * information follows: Copyright (c) 1998 by Michael
578 * Barr. This software is placed into the public
579 * domain and may be used for any purpose. However,
580 * this notice must not be changed or removed and no
581 * warranty is either expressed or implied by its
582 * publication or distribution.
588 * Description: Test the address bus wiring in a
589 * memory region by performing a walking
590 * 1's test on the relevant bits of the
591 * address and checking for aliasing.
592 * This test will find single-bit
593 * address failures such as stuck -high,
594 * stuck-low, and shorted pins. The base
595 * address and size of the region are
596 * selected by the caller.
598 * Notes: For best results, the selected base
599 * address should have enough LSB 0's to
600 * guarantee single address bit changes.
601 * For example, to test a 64-Kbyte
602 * region, select a base address on a
603 * 64-Kbyte boundary. Also, select the
604 * region size as a power-of-two if at
607 * Returns: 0 if the test succeeds, 1 if the test fails.
609 * ## NOTE ## Be sure to specify start and end
610 * addresses such that addr_mask has
611 * lots of bits set. For example an
612 * address range of 01000000 02000000 is
613 * bad while a range of 01000000
614 * 01ffffff is perfect.
616 addr_mask = ((ulong)end - (ulong)start)/sizeof(vu_long);
617 pattern = (vu_long) 0xaaaaaaaa;
618 anti_pattern = (vu_long) 0x55555555;
620 PRINTF("%s:%d: addr mask = 0x%.8lx\n",
621 __FUNCTION__, __LINE__,
624 * Write the default pattern at each of the
625 * power-of-two offsets.
627 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
628 start[offset] = pattern;
632 * Check for address bits stuck high.
635 start[test_offset] = anti_pattern;
637 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
638 temp = start[offset];
639 if (temp != pattern) {
640 printf ("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
641 " expected 0x%.8lx, actual 0x%.8lx\n",
642 (ulong)&start[offset], pattern, temp);
646 start[test_offset] = pattern;
649 * Check for addr bits stuck low or shorted.
651 for (test_offset = 1; (test_offset & addr_mask) != 0; test_offset <<= 1) {
652 start[test_offset] = anti_pattern;
654 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
655 temp = start[offset];
656 if ((temp != pattern) && (offset != test_offset)) {
657 printf ("\nFAILURE: Address bit stuck low or shorted @"
658 " 0x%.8lx: expected 0x%.8lx, actual 0x%.8lx\n",
659 (ulong)&start[offset], pattern, temp);
663 start[test_offset] = pattern;
667 * Description: Test the integrity of a physical
668 * memory device by performing an
669 * increment/decrement test over the
670 * entire region. In the process every
671 * storage bit in the device is tested
672 * as a zero and a one. The base address
673 * and the size of the region are
674 * selected by the caller.
676 * Returns: 0 if the test succeeds, 1 if the test fails.
678 num_words = ((ulong)end - (ulong)start)/sizeof(vu_long) + 1;
681 * Fill memory with a known pattern.
683 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
684 start[offset] = pattern;
688 * Check each location and invert it for the second pass.
690 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
691 temp = start[offset];
692 if (temp != pattern) {
693 printf ("\nFAILURE (read/write) @ 0x%.8lx:"
694 " expected 0x%.8lx, actual 0x%.8lx)\n",
695 (ulong)&start[offset], pattern, temp);
699 anti_pattern = ~pattern;
700 start[offset] = anti_pattern;
704 * Check each location for the inverted pattern and zero it.
706 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
707 anti_pattern = ~pattern;
708 temp = start[offset];
709 if (temp != anti_pattern) {
710 printf ("\nFAILURE (read/write): @ 0x%.8lx:"
711 " expected 0x%.8lx, actual 0x%.8lx)\n",
712 (ulong)&start[offset], anti_pattern, temp);
719 #else /* The original, quickie test */
727 printf ("\rPattern %08lX Writing..."
729 "\b\b\b\b\b\b\b\b\b\b",
732 for (addr=start,val=pattern; addr<end; addr++) {
737 printf("Reading...");
739 for (addr=start,val=pattern; addr<end; addr++) {
741 if (readback != val) {
742 printf ("\nMem error @ 0x%08X: "
743 "found %08lX, expected %08lX\n",
744 (uint)addr, readback, val);
751 * Flip the pattern each time to make lots of zeros and
752 * then, the next time, lots of ones. We decrement
753 * the "negative" patterns and increment the "positive"
754 * patterns to preserve this feature.
756 if(pattern & 0x80000000) {
757 pattern = -pattern; /* complement & increment */
772 * mm{.b, .w, .l} {addr}
773 * nm{.b, .w, .l} {addr}
776 mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[])
780 extern char console_buffer[];
783 printf ("Usage:\n%s\n", cmdtp->usage);
787 #ifdef CONFIG_BOOT_RETRY_TIME
788 reset_cmd_timeout(); /* got a good command to get here */
790 /* We use the last specified parameters, unless new ones are
796 if ((flag & CMD_FLAG_REPEAT) == 0) {
797 /* New command specified. Check for a size specification.
798 * Defaults to long if no or incorrect specification.
800 size = cmd_get_data_size(argv[0], 4);
802 /* Address is specified since argc > 1
804 addr = simple_strtoul(argv[1], NULL, 16);
805 addr += base_address;
808 /* Print the address, followed by value. Then accept input for
809 * the next value. A non-converted value exits.
812 printf("%08lx:", addr);
814 printf(" %08x", *((uint *)addr));
816 printf(" %04x", *((ushort *)addr));
818 printf(" %02x", *((u_char *)addr));
820 nbytes = readline (" ? ");
821 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
822 /* <CR> pressed as only input, don't modify current
823 * location and move to next. "-" pressed will go back.
826 addr += nbytes ? -size : size;
828 #ifdef CONFIG_BOOT_RETRY_TIME
829 reset_cmd_timeout(); /* good enough to not time out */
832 #ifdef CONFIG_BOOT_RETRY_TIME
833 else if (nbytes == -2) {
834 break; /* timed out, exit the command */
839 i = simple_strtoul(console_buffer, &endp, 16);
840 nbytes = endp - console_buffer;
842 #ifdef CONFIG_BOOT_RETRY_TIME
843 /* good enough to not time out
850 *((ushort *)addr) = i;
852 *((u_char *)addr) = i;
864 int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
871 printf ("Usage:\n%s\n", cmdtp->usage);
875 addr = simple_strtoul (argv[1], NULL, 16);
876 addr += base_address;
878 length = simple_strtoul (argv[2], NULL, 16);
880 crc = crc32 (0, (const uchar *) addr, length);
882 printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
883 addr, addr + length - 1, crc);
886 ptr = (ulong *) simple_strtoul (argv[3], NULL, 16);
893 #endif /* CFG_CMD_MEMORY */