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)
32 #if (CONFIG_COMMANDS & CFG_CMD_MMC)
35 #ifdef CONFIG_HAS_DATAFLASH
36 #include <dataflash.h>
39 #if (CONFIG_COMMANDS & (CFG_CMD_MEMORY | \
43 int cmd_get_data_size(char* arg, int default_size)
45 /* Check for a size specification .b, .w or .l.
47 int len = strlen(arg);
48 if (len > 2 && arg[len-2] == '.') {
62 #if (CONFIG_COMMANDS & CFG_CMD_MEMORY)
65 #define PRINTF(fmt,args...) printf (fmt ,##args)
67 #define PRINTF(fmt,args...)
70 static int mod_mem(cmd_tbl_t *, int, int, int, char *[]);
72 /* Display values from last command.
73 * Memory modify remembered values are different from display memory.
75 uint dp_last_addr, dp_last_size;
76 uint dp_last_length = 0x40;
77 uint mm_last_addr, mm_last_size;
79 static ulong base_address = 0;
84 * md{.b, .w, .l} {addr} {len}
86 #define DISP_LINE_LEN 16
87 int do_mem_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
89 ulong addr, size, length;
90 ulong i, nbytes, linebytes;
94 /* We use the last specified parameters, unless new ones are
99 length = dp_last_length;
102 printf ("Usage:\n%s\n", cmdtp->usage);
106 if ((flag & CMD_FLAG_REPEAT) == 0) {
107 /* New command specified. Check for a size specification.
108 * Defaults to long if no or incorrect specification.
110 size = cmd_get_data_size(argv[0], 4);
112 /* Address is specified since argc > 1
114 addr = simple_strtoul(argv[1], NULL, 16);
115 addr += base_address;
117 /* If another parameter, it is the length to display.
118 * Length is the number of objects, not number of bytes.
121 length = simple_strtoul(argv[2], NULL, 16);
126 * We buffer all read data, so we can make sure data is read only
127 * once, and all accesses are with the specified bus width.
129 nbytes = length * size;
131 char linebuf[DISP_LINE_LEN];
132 uint *uip = (uint *)linebuf;
133 ushort *usp = (ushort *)linebuf;
134 u_char *ucp = (u_char *)linebuf;
136 printf("%08lx:", addr);
137 linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
139 #ifdef CONFIG_HAS_DATAFLASH
140 if (read_dataflash(addr, (linebytes/size)*size, linebuf) != -1){
142 for (i=0; i<linebytes; i+= size) {
144 printf(" %08x", *uip++);
145 } else if (size == 2) {
146 printf(" %04x", *usp++);
148 printf(" %02x", *ucp++);
153 } else { /* addr does not correspond to DataFlash */
155 for (i=0; i<linebytes; i+= size) {
157 printf(" %08x", (*uip++ = *((uint *)addr)));
158 } else if (size == 2) {
159 printf(" %04x", (*usp++ = *((ushort *)addr)));
161 printf(" %02x", (*ucp++ = *((u_char *)addr)));
165 #ifdef CONFIG_HAS_DATAFLASH
170 for (i=0; i<linebytes; i++) {
171 if ((*cp < 0x20) || (*cp > 0x7e))
183 } while (nbytes > 0);
186 dp_last_length = length;
191 int do_mem_mm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
193 return mod_mem (cmdtp, 1, flag, argc, argv);
195 int do_mem_nm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
197 return mod_mem (cmdtp, 0, flag, argc, argv);
200 int do_mem_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
202 ulong addr, size, writeval, count;
204 if ((argc < 3) || (argc > 4)) {
205 printf ("Usage:\n%s\n", cmdtp->usage);
209 /* Check for size specification.
211 size = cmd_get_data_size(argv[0], 4);
213 /* Address is specified since argc > 1
215 addr = simple_strtoul(argv[1], NULL, 16);
216 addr += base_address;
218 /* Get the value to write.
220 writeval = simple_strtoul(argv[2], NULL, 16);
224 count = simple_strtoul(argv[3], NULL, 16);
229 while (count-- > 0) {
231 *((ulong *)addr) = (ulong )writeval;
233 *((ushort *)addr) = (ushort)writeval;
235 *((u_char *)addr) = (u_char)writeval;
241 int do_mem_cmp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
243 ulong size, addr1, addr2, count, ngood;
247 printf ("Usage:\n%s\n", cmdtp->usage);
251 /* Check for size specification.
253 size = cmd_get_data_size(argv[0], 4);
255 addr1 = simple_strtoul(argv[1], NULL, 16);
256 addr1 += base_address;
258 addr2 = simple_strtoul(argv[2], NULL, 16);
259 addr2 += base_address;
261 count = simple_strtoul(argv[3], NULL, 16);
263 #ifdef CONFIG_HAS_DATAFLASH
264 if (addr_dataflash(addr1) | addr_dataflash(addr2)){
265 printf("Comparison with DataFlash space not supported.\n\r");
272 while (count-- > 0) {
274 ulong word1 = *(ulong *)addr1;
275 ulong word2 = *(ulong *)addr2;
276 if (word1 != word2) {
277 printf("word at 0x%08lx (0x%08lx) "
278 "!= word at 0x%08lx (0x%08lx)\n",
279 addr1, word1, addr2, word2);
284 else if (size == 2) {
285 ushort hword1 = *(ushort *)addr1;
286 ushort hword2 = *(ushort *)addr2;
287 if (hword1 != hword2) {
288 printf("halfword at 0x%08lx (0x%04x) "
289 "!= halfword at 0x%08lx (0x%04x)\n",
290 addr1, hword1, addr2, hword2);
296 u_char byte1 = *(u_char *)addr1;
297 u_char byte2 = *(u_char *)addr2;
298 if (byte1 != byte2) {
299 printf("byte at 0x%08lx (0x%02x) "
300 "!= byte at 0x%08lx (0x%02x)\n",
301 addr1, byte1, addr2, byte2);
311 printf("Total of %ld %s%s were the same\n",
312 ngood, size == 4 ? "word" : size == 2 ? "halfword" : "byte",
313 ngood == 1 ? "" : "s");
317 int do_mem_cp ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
319 ulong addr, size, dest, count;
322 printf ("Usage:\n%s\n", cmdtp->usage);
326 /* Check for size specification.
328 size = cmd_get_data_size(argv[0], 4);
330 addr = simple_strtoul(argv[1], NULL, 16);
331 addr += base_address;
333 dest = simple_strtoul(argv[2], NULL, 16);
334 dest += base_address;
336 count = simple_strtoul(argv[3], NULL, 16);
339 puts ("Zero length ???\n");
344 /* check if we are copying to Flash */
345 if ( (addr2info(dest) != NULL)
346 #ifdef CONFIG_HAS_DATAFLASH
347 && (!addr_dataflash(addr))
352 printf ("Copy to Flash... ");
354 rc = flash_write ((uchar *)addr, dest, count*size);
364 #if (CONFIG_COMMANDS & CFG_CMD_MMC)
365 if (mmc2info(dest)) {
368 printf ("Copy to MMC... ");
369 switch (rc = mmc_write ((uchar *)addr, dest, count*size)) {
377 printf ("%s[%d] FIXME: rc=%d\n",__FILE__,__LINE__,rc);
384 if (mmc2info(addr)) {
387 printf ("Copy from MMC... ");
388 switch (rc = mmc_read (addr, (uchar *)dest, count*size)) {
396 printf ("%s[%d] FIXME: rc=%d\n",__FILE__,__LINE__,rc);
404 #ifdef CONFIG_HAS_DATAFLASH
405 /* Check if we are copying from RAM or Flash to DataFlash */
406 if (addr_dataflash(dest) && !addr_dataflash(addr)){
409 printf ("Copy to DataFlash... ");
411 rc = write_dataflash (dest, addr, count*size);
414 dataflash_perror (rc);
421 /* Check if we are copying from DataFlash to RAM */
422 if (addr_dataflash(addr) && !addr_dataflash(dest) && (addr2info(dest)==NULL) ){
423 read_dataflash(addr, count * size, (char *) dest);
427 if (addr_dataflash(addr) && addr_dataflash(dest)){
428 printf("Unsupported combination of source/destination.\n\r");
433 while (count-- > 0) {
435 *((ulong *)dest) = *((ulong *)addr);
437 *((ushort *)dest) = *((ushort *)addr);
439 *((u_char *)dest) = *((u_char *)addr);
446 int do_mem_base (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
449 /* Set new base address.
451 base_address = simple_strtoul(argv[1], NULL, 16);
453 /* Print the current base address.
455 printf("Base Address: 0x%08lx\n", base_address);
459 int do_mem_loop (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
461 ulong addr, size, length, i, junk;
462 volatile uint *longp;
463 volatile ushort *shortp;
467 printf ("Usage:\n%s\n", cmdtp->usage);
471 /* Check for a size spefication.
472 * Defaults to long if no or incorrect specification.
474 size = cmd_get_data_size(argv[0], 4);
476 /* Address is always specified.
478 addr = simple_strtoul(argv[1], NULL, 16);
480 /* Length is the number of objects, not number of bytes.
482 length = simple_strtoul(argv[2], NULL, 16);
484 /* We want to optimize the loops to run as fast as possible.
485 * If we have only one object, just run infinite loops.
489 longp = (uint *)addr;
494 shortp = (ushort *)addr;
505 longp = (uint *)addr;
513 shortp = (ushort *)addr;
528 * Perform a memory test. A more complete alternative test can be
529 * configured using CFG_ALT_MEMTEST. The complete test loops until
530 * interrupted by ctrl-c or by a failure of one of the sub-tests.
532 int do_mem_mtest (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
534 vu_long *addr, *start, *end;
538 #if defined(CFG_ALT_MEMTEST)
544 vu_long anti_pattern;
546 vu_long *dummy = NULL;
550 static const ulong bitpattern[] = {
551 0x00000001, /* single bit */
552 0x00000003, /* two adjacent bits */
553 0x00000007, /* three adjacent bits */
554 0x0000000F, /* four adjacent bits */
555 0x00000005, /* two non-adjacent bits */
556 0x00000015, /* three non-adjacent bits */
557 0x00000055, /* four non-adjacent bits */
558 0xaaaaaaaa, /* alternating 1/0 */
567 start = (ulong *)simple_strtoul(argv[1], NULL, 16);
569 start = (ulong *)CFG_MEMTEST_START;
573 end = (ulong *)simple_strtoul(argv[2], NULL, 16);
575 end = (ulong *)(CFG_MEMTEST_END);
579 pattern = (ulong)simple_strtoul(argv[3], NULL, 16);
584 #if defined(CFG_ALT_MEMTEST)
585 printf ("Testing %08x ... %08x:\n", (uint)start, (uint)end);
586 PRINTF("%s:%d: start 0x%p end 0x%p\n",
587 __FUNCTION__, __LINE__, start, end);
595 printf("Iteration: %6d\r", iterations);
596 PRINTF("Iteration: %6d\n", iterations);
600 * Data line test: write a pattern to the first
601 * location, write the 1's complement to a 'parking'
602 * address (changes the state of the data bus so a
603 * floating bus doen't give a false OK), and then
604 * read the value back. Note that we read it back
605 * into a variable because the next time we read it,
606 * it might be right (been there, tough to explain to
607 * the quality guys why it prints a failure when the
608 * "is" and "should be" are obviously the same in the
611 * Rather than exhaustively testing, we test some
612 * patterns by shifting '1' bits through a field of
613 * '0's and '0' bits through a field of '1's (i.e.
614 * pattern and ~pattern).
617 for (j = 0; j < sizeof(bitpattern)/sizeof(bitpattern[0]); j++) {
619 for(; val != 0; val <<= 1) {
621 *dummy = ~val; /* clear the test data off of the bus */
623 if(readback != val) {
624 printf ("FAILURE (data line): "
625 "expected %08lx, actual %08lx\n",
631 if(readback != ~val) {
632 printf ("FAILURE (data line): "
633 "Is %08lx, should be %08lx\n",
640 * Based on code whose Original Author and Copyright
641 * information follows: Copyright (c) 1998 by Michael
642 * Barr. This software is placed into the public
643 * domain and may be used for any purpose. However,
644 * this notice must not be changed or removed and no
645 * warranty is either expressed or implied by its
646 * publication or distribution.
652 * Description: Test the address bus wiring in a
653 * memory region by performing a walking
654 * 1's test on the relevant bits of the
655 * address and checking for aliasing.
656 * This test will find single-bit
657 * address failures such as stuck -high,
658 * stuck-low, and shorted pins. The base
659 * address and size of the region are
660 * selected by the caller.
662 * Notes: For best results, the selected base
663 * address should have enough LSB 0's to
664 * guarantee single address bit changes.
665 * For example, to test a 64-Kbyte
666 * region, select a base address on a
667 * 64-Kbyte boundary. Also, select the
668 * region size as a power-of-two if at
671 * Returns: 0 if the test succeeds, 1 if the test fails.
673 * ## NOTE ## Be sure to specify start and end
674 * addresses such that addr_mask has
675 * lots of bits set. For example an
676 * address range of 01000000 02000000 is
677 * bad while a range of 01000000
678 * 01ffffff is perfect.
680 addr_mask = ((ulong)end - (ulong)start)/sizeof(vu_long);
681 pattern = (vu_long) 0xaaaaaaaa;
682 anti_pattern = (vu_long) 0x55555555;
684 PRINTF("%s:%d: addr mask = 0x%.8lx\n",
685 __FUNCTION__, __LINE__,
688 * Write the default pattern at each of the
689 * power-of-two offsets.
691 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
692 start[offset] = pattern;
696 * Check for address bits stuck high.
699 start[test_offset] = anti_pattern;
701 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
702 temp = start[offset];
703 if (temp != pattern) {
704 printf ("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
705 " expected 0x%.8lx, actual 0x%.8lx\n",
706 (ulong)&start[offset], pattern, temp);
710 start[test_offset] = pattern;
713 * Check for addr bits stuck low or shorted.
715 for (test_offset = 1; (test_offset & addr_mask) != 0; test_offset <<= 1) {
716 start[test_offset] = anti_pattern;
718 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
719 temp = start[offset];
720 if ((temp != pattern) && (offset != test_offset)) {
721 printf ("\nFAILURE: Address bit stuck low or shorted @"
722 " 0x%.8lx: expected 0x%.8lx, actual 0x%.8lx\n",
723 (ulong)&start[offset], pattern, temp);
727 start[test_offset] = pattern;
731 * Description: Test the integrity of a physical
732 * memory device by performing an
733 * increment/decrement test over the
734 * entire region. In the process every
735 * storage bit in the device is tested
736 * as a zero and a one. The base address
737 * and the size of the region are
738 * selected by the caller.
740 * Returns: 0 if the test succeeds, 1 if the test fails.
742 num_words = ((ulong)end - (ulong)start)/sizeof(vu_long) + 1;
745 * Fill memory with a known pattern.
747 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
748 start[offset] = pattern;
752 * Check each location and invert it for the second pass.
754 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
755 temp = start[offset];
756 if (temp != pattern) {
757 printf ("\nFAILURE (read/write) @ 0x%.8lx:"
758 " expected 0x%.8lx, actual 0x%.8lx)\n",
759 (ulong)&start[offset], pattern, temp);
763 anti_pattern = ~pattern;
764 start[offset] = anti_pattern;
768 * Check each location for the inverted pattern and zero it.
770 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
771 anti_pattern = ~pattern;
772 temp = start[offset];
773 if (temp != anti_pattern) {
774 printf ("\nFAILURE (read/write): @ 0x%.8lx:"
775 " expected 0x%.8lx, actual 0x%.8lx)\n",
776 (ulong)&start[offset], anti_pattern, temp);
783 #else /* The original, quickie test */
791 printf ("\rPattern %08lX Writing..."
793 "\b\b\b\b\b\b\b\b\b\b",
796 for (addr=start,val=pattern; addr<end; addr++) {
801 printf("Reading...");
803 for (addr=start,val=pattern; addr<end; addr++) {
805 if (readback != val) {
806 printf ("\nMem error @ 0x%08X: "
807 "found %08lX, expected %08lX\n",
808 (uint)addr, readback, val);
815 * Flip the pattern each time to make lots of zeros and
816 * then, the next time, lots of ones. We decrement
817 * the "negative" patterns and increment the "positive"
818 * patterns to preserve this feature.
820 if(pattern & 0x80000000) {
821 pattern = -pattern; /* complement & increment */
836 * mm{.b, .w, .l} {addr}
837 * nm{.b, .w, .l} {addr}
840 mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[])
844 extern char console_buffer[];
847 printf ("Usage:\n%s\n", cmdtp->usage);
851 #ifdef CONFIG_BOOT_RETRY_TIME
852 reset_cmd_timeout(); /* got a good command to get here */
854 /* We use the last specified parameters, unless new ones are
860 if ((flag & CMD_FLAG_REPEAT) == 0) {
861 /* New command specified. Check for a size specification.
862 * Defaults to long if no or incorrect specification.
864 size = cmd_get_data_size(argv[0], 4);
866 /* Address is specified since argc > 1
868 addr = simple_strtoul(argv[1], NULL, 16);
869 addr += base_address;
872 #ifdef CONFIG_HAS_DATAFLASH
873 if (addr_dataflash(addr)){
874 printf("Can't modify DataFlash in place. Use cp instead.\n\r");
879 /* Print the address, followed by value. Then accept input for
880 * the next value. A non-converted value exits.
883 printf("%08lx:", addr);
885 printf(" %08x", *((uint *)addr));
887 printf(" %04x", *((ushort *)addr));
889 printf(" %02x", *((u_char *)addr));
891 nbytes = readline (" ? ");
892 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
893 /* <CR> pressed as only input, don't modify current
894 * location and move to next. "-" pressed will go back.
897 addr += nbytes ? -size : size;
899 #ifdef CONFIG_BOOT_RETRY_TIME
900 reset_cmd_timeout(); /* good enough to not time out */
903 #ifdef CONFIG_BOOT_RETRY_TIME
904 else if (nbytes == -2) {
905 break; /* timed out, exit the command */
910 i = simple_strtoul(console_buffer, &endp, 16);
911 nbytes = endp - console_buffer;
913 #ifdef CONFIG_BOOT_RETRY_TIME
914 /* good enough to not time out
921 *((ushort *)addr) = i;
923 *((u_char *)addr) = i;
935 int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
942 printf ("Usage:\n%s\n", cmdtp->usage);
946 addr = simple_strtoul (argv[1], NULL, 16);
947 addr += base_address;
949 length = simple_strtoul (argv[2], NULL, 16);
951 crc = crc32 (0, (const uchar *) addr, length);
953 printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
954 addr, addr + length - 1, crc);
957 ptr = (ulong *) simple_strtoul (argv[3], NULL, 16);
964 /**************************************************/
965 #if (CONFIG_COMMANDS & CFG_CMD_MEMORY)
968 "md - memory display\n",
969 "[.b, .w, .l] address [# of objects]\n - memory display\n"
975 "mm - memory modify (auto-incrementing)\n",
976 "[.b, .w, .l] address\n" " - memory modify, auto increment address\n"
982 "nm - memory modify (constant address)\n",
983 "[.b, .w, .l] address\n - memory modify, read and keep address\n"
988 "mw - memory write (fill)\n",
989 "[.b, .w, .l] address value [count]\n - write memory\n"
994 "cp - memory copy\n",
995 "[.b, .w, .l] source target count\n - copy memory\n"
999 cmp, 4, 1, do_mem_cmp,
1000 "cmp - memory compare\n",
1001 "[.b, .w, .l] addr1 addr2 count\n - compare memory\n"
1005 crc32, 4, 1, do_mem_crc,
1006 "crc32 - checksum calculation\n",
1007 "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
1011 base, 2, 1, do_mem_base,
1012 "base - print or set address offset\n",
1013 "\n - print address offset for memory commands\n"
1014 "base off\n - set address offset for memory commands to 'off'\n"
1018 loop, 3, 1, do_mem_loop,
1019 "loop - infinite loop on address range\n",
1020 "[.b, .w, .l] address number_of_objects\n"
1021 " - loop on a set of addresses\n"
1025 mtest, 4, 1, do_mem_mtest,
1026 "mtest - simple RAM test\n",
1027 "[start [end [pattern]]]\n"
1028 " - simple RAM read/write test\n"
1032 #endif /* CFG_CMD_MEMORY */