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] == '.') {
64 #if (CONFIG_COMMANDS & CFG_CMD_MEMORY)
67 #define PRINTF(fmt,args...) printf (fmt ,##args)
69 #define PRINTF(fmt,args...)
72 static int mod_mem(cmd_tbl_t *, int, int, int, char *[]);
74 /* Display values from last command.
75 * Memory modify remembered values are different from display memory.
77 uint dp_last_addr, dp_last_size;
78 uint dp_last_length = 0x40;
79 uint mm_last_addr, mm_last_size;
81 static ulong base_address = 0;
86 * md{.b, .w, .l} {addr} {len}
88 #define DISP_LINE_LEN 16
89 int do_mem_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
92 ulong i, nbytes, linebytes;
97 /* We use the last specified parameters, unless new ones are
102 length = dp_last_length;
105 printf ("Usage:\n%s\n", cmdtp->usage);
109 if ((flag & CMD_FLAG_REPEAT) == 0) {
110 /* New command specified. Check for a size specification.
111 * Defaults to long if no or incorrect specification.
113 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
116 /* Address is specified since argc > 1
118 addr = simple_strtoul(argv[1], NULL, 16);
119 addr += base_address;
121 /* If another parameter, it is the length to display.
122 * Length is the number of objects, not number of bytes.
125 length = simple_strtoul(argv[2], NULL, 16);
130 * We buffer all read data, so we can make sure data is read only
131 * once, and all accesses are with the specified bus width.
133 nbytes = length * size;
135 char linebuf[DISP_LINE_LEN];
136 uint *uip = (uint *)linebuf;
137 ushort *usp = (ushort *)linebuf;
138 u_char *ucp = (u_char *)linebuf;
139 #ifdef CONFIG_HAS_DATAFLASH
142 printf("%08lx:", addr);
143 linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
145 #ifdef CONFIG_HAS_DATAFLASH
146 if ((rc = read_dataflash(addr, (linebytes/size)*size, linebuf)) == DATAFLASH_OK){
147 /* if outside dataflash */
149 dataflash_perror (rc);
152 for (i=0; i<linebytes; i+= size) {
154 printf(" %08x", *uip++);
155 } else if (size == 2) {
156 printf(" %04x", *usp++);
158 printf(" %02x", *ucp++);
163 } else { /* addr does not correspond to DataFlash */
165 for (i=0; i<linebytes; i+= size) {
167 printf(" %08x", (*uip++ = *((uint *)addr)));
168 } else if (size == 2) {
169 printf(" %04x", (*usp++ = *((ushort *)addr)));
171 printf(" %02x", (*ucp++ = *((u_char *)addr)));
175 #ifdef CONFIG_HAS_DATAFLASH
180 for (i=0; i<linebytes; i++) {
181 if ((*cp < 0x20) || (*cp > 0x7e))
193 } while (nbytes > 0);
196 dp_last_length = length;
201 int do_mem_mm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
203 return mod_mem (cmdtp, 1, flag, argc, argv);
205 int do_mem_nm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
207 return mod_mem (cmdtp, 0, flag, argc, argv);
210 int do_mem_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
212 ulong addr, writeval, count;
215 if ((argc < 3) || (argc > 4)) {
216 printf ("Usage:\n%s\n", cmdtp->usage);
220 /* Check for size specification.
222 if ((size = cmd_get_data_size(argv[0], 4)) < 1)
225 /* Address is specified since argc > 1
227 addr = simple_strtoul(argv[1], NULL, 16);
228 addr += base_address;
230 /* Get the value to write.
232 writeval = simple_strtoul(argv[2], NULL, 16);
236 count = simple_strtoul(argv[3], NULL, 16);
241 while (count-- > 0) {
243 *((ulong *)addr) = (ulong )writeval;
245 *((ushort *)addr) = (ushort)writeval;
247 *((u_char *)addr) = (u_char)writeval;
253 int do_mem_cmp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
255 ulong addr1, addr2, count, ngood;
260 printf ("Usage:\n%s\n", cmdtp->usage);
264 /* Check for size specification.
266 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
269 addr1 = simple_strtoul(argv[1], NULL, 16);
270 addr1 += base_address;
272 addr2 = simple_strtoul(argv[2], NULL, 16);
273 addr2 += base_address;
275 count = simple_strtoul(argv[3], NULL, 16);
277 #ifdef CONFIG_HAS_DATAFLASH
278 if (addr_dataflash(addr1) | addr_dataflash(addr2)){
279 printf("Comparison with DataFlash space not supported.\n\r");
286 while (count-- > 0) {
288 ulong word1 = *(ulong *)addr1;
289 ulong word2 = *(ulong *)addr2;
290 if (word1 != word2) {
291 printf("word at 0x%08lx (0x%08lx) "
292 "!= word at 0x%08lx (0x%08lx)\n",
293 addr1, word1, addr2, word2);
298 else if (size == 2) {
299 ushort hword1 = *(ushort *)addr1;
300 ushort hword2 = *(ushort *)addr2;
301 if (hword1 != hword2) {
302 printf("halfword at 0x%08lx (0x%04x) "
303 "!= halfword at 0x%08lx (0x%04x)\n",
304 addr1, hword1, addr2, hword2);
310 u_char byte1 = *(u_char *)addr1;
311 u_char byte2 = *(u_char *)addr2;
312 if (byte1 != byte2) {
313 printf("byte at 0x%08lx (0x%02x) "
314 "!= byte at 0x%08lx (0x%02x)\n",
315 addr1, byte1, addr2, byte2);
325 printf("Total of %ld %s%s were the same\n",
326 ngood, size == 4 ? "word" : size == 2 ? "halfword" : "byte",
327 ngood == 1 ? "" : "s");
331 int do_mem_cp ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
333 ulong addr, dest, count;
337 printf ("Usage:\n%s\n", cmdtp->usage);
341 /* Check for size specification.
343 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
346 addr = simple_strtoul(argv[1], NULL, 16);
347 addr += base_address;
349 dest = simple_strtoul(argv[2], NULL, 16);
350 dest += base_address;
352 count = simple_strtoul(argv[3], NULL, 16);
355 puts ("Zero length ???\n");
360 /* check if we are copying to Flash */
361 if ( (addr2info(dest) != NULL)
362 #ifdef CONFIG_HAS_DATAFLASH
363 && (!addr_dataflash(addr))
368 printf ("Copy to Flash... ");
370 rc = flash_write ((uchar *)addr, dest, count*size);
380 #if (CONFIG_COMMANDS & CFG_CMD_MMC)
381 if (mmc2info(dest)) {
384 printf ("Copy to MMC... ");
385 switch (rc = mmc_write ((uchar *)addr, dest, count*size)) {
393 printf ("%s[%d] FIXME: rc=%d\n",__FILE__,__LINE__,rc);
400 if (mmc2info(addr)) {
403 printf ("Copy from MMC... ");
404 switch (rc = mmc_read (addr, (uchar *)dest, count*size)) {
412 printf ("%s[%d] FIXME: rc=%d\n",__FILE__,__LINE__,rc);
420 #ifdef CONFIG_HAS_DATAFLASH
421 /* Check if we are copying from RAM or Flash to DataFlash */
422 if (addr_dataflash(dest) && !addr_dataflash(addr)){
425 printf ("Copy to DataFlash... ");
427 rc = write_dataflash (dest, addr, count*size);
430 dataflash_perror (rc);
437 /* Check if we are copying from DataFlash to RAM */
438 if (addr_dataflash(addr) && !addr_dataflash(dest) && (addr2info(dest)==NULL) ){
440 rc = read_dataflash(addr, count * size, (char *) dest);
442 dataflash_perror (rc);
448 if (addr_dataflash(addr) && addr_dataflash(dest)){
449 printf("Unsupported combination of source/destination.\n\r");
454 while (count-- > 0) {
456 *((ulong *)dest) = *((ulong *)addr);
458 *((ushort *)dest) = *((ushort *)addr);
460 *((u_char *)dest) = *((u_char *)addr);
467 int do_mem_base (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
470 /* Set new base address.
472 base_address = simple_strtoul(argv[1], NULL, 16);
474 /* Print the current base address.
476 printf("Base Address: 0x%08lx\n", base_address);
480 int do_mem_loop (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
482 ulong addr, length, i, junk;
484 volatile uint *longp;
485 volatile ushort *shortp;
489 printf ("Usage:\n%s\n", cmdtp->usage);
493 /* Check for a size spefication.
494 * Defaults to long if no or incorrect specification.
496 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
499 /* Address is always specified.
501 addr = simple_strtoul(argv[1], NULL, 16);
503 /* Length is the number of objects, not number of bytes.
505 length = simple_strtoul(argv[2], NULL, 16);
507 /* We want to optimize the loops to run as fast as possible.
508 * If we have only one object, just run infinite loops.
512 longp = (uint *)addr;
517 shortp = (ushort *)addr;
528 longp = (uint *)addr;
536 shortp = (ushort *)addr;
551 * Perform a memory test. A more complete alternative test can be
552 * configured using CFG_ALT_MEMTEST. The complete test loops until
553 * interrupted by ctrl-c or by a failure of one of the sub-tests.
555 int do_mem_mtest (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
557 vu_long *addr, *start, *end;
561 #if defined(CFG_ALT_MEMTEST)
567 vu_long anti_pattern;
569 #if defined(CFG_MEMTEST_SCRATCH)
570 vu_long *dummy = (vu_long*)CFG_MEMTEST_SCRATCH;
572 vu_long *dummy = NULL;
577 static const ulong bitpattern[] = {
578 0x00000001, /* single bit */
579 0x00000003, /* two adjacent bits */
580 0x00000007, /* three adjacent bits */
581 0x0000000F, /* four adjacent bits */
582 0x00000005, /* two non-adjacent bits */
583 0x00000015, /* three non-adjacent bits */
584 0x00000055, /* four non-adjacent bits */
585 0xaaaaaaaa, /* alternating 1/0 */
594 start = (ulong *)simple_strtoul(argv[1], NULL, 16);
596 start = (ulong *)CFG_MEMTEST_START;
600 end = (ulong *)simple_strtoul(argv[2], NULL, 16);
602 end = (ulong *)(CFG_MEMTEST_END);
606 pattern = (ulong)simple_strtoul(argv[3], NULL, 16);
611 #if defined(CFG_ALT_MEMTEST)
612 printf ("Testing %08x ... %08x:\n", (uint)start, (uint)end);
613 PRINTF("%s:%d: start 0x%p end 0x%p\n",
614 __FUNCTION__, __LINE__, start, end);
622 printf("Iteration: %6d\r", iterations);
623 PRINTF("Iteration: %6d\n", iterations);
627 * Data line test: write a pattern to the first
628 * location, write the 1's complement to a 'parking'
629 * address (changes the state of the data bus so a
630 * floating bus doen't give a false OK), and then
631 * read the value back. Note that we read it back
632 * into a variable because the next time we read it,
633 * it might be right (been there, tough to explain to
634 * the quality guys why it prints a failure when the
635 * "is" and "should be" are obviously the same in the
638 * Rather than exhaustively testing, we test some
639 * patterns by shifting '1' bits through a field of
640 * '0's and '0' bits through a field of '1's (i.e.
641 * pattern and ~pattern).
644 for (j = 0; j < sizeof(bitpattern)/sizeof(bitpattern[0]); j++) {
646 for(; val != 0; val <<= 1) {
648 *dummy = ~val; /* clear the test data off of the bus */
650 if(readback != val) {
651 printf ("FAILURE (data line): "
652 "expected %08lx, actual %08lx\n",
658 if(readback != ~val) {
659 printf ("FAILURE (data line): "
660 "Is %08lx, should be %08lx\n",
667 * Based on code whose Original Author and Copyright
668 * information follows: Copyright (c) 1998 by Michael
669 * Barr. This software is placed into the public
670 * domain and may be used for any purpose. However,
671 * this notice must not be changed or removed and no
672 * warranty is either expressed or implied by its
673 * publication or distribution.
679 * Description: Test the address bus wiring in a
680 * memory region by performing a walking
681 * 1's test on the relevant bits of the
682 * address and checking for aliasing.
683 * This test will find single-bit
684 * address failures such as stuck -high,
685 * stuck-low, and shorted pins. The base
686 * address and size of the region are
687 * selected by the caller.
689 * Notes: For best results, the selected base
690 * address should have enough LSB 0's to
691 * guarantee single address bit changes.
692 * For example, to test a 64-Kbyte
693 * region, select a base address on a
694 * 64-Kbyte boundary. Also, select the
695 * region size as a power-of-two if at
698 * Returns: 0 if the test succeeds, 1 if the test fails.
700 * ## NOTE ## Be sure to specify start and end
701 * addresses such that addr_mask has
702 * lots of bits set. For example an
703 * address range of 01000000 02000000 is
704 * bad while a range of 01000000
705 * 01ffffff is perfect.
707 addr_mask = ((ulong)end - (ulong)start)/sizeof(vu_long);
708 pattern = (vu_long) 0xaaaaaaaa;
709 anti_pattern = (vu_long) 0x55555555;
711 PRINTF("%s:%d: addr mask = 0x%.8lx\n",
712 __FUNCTION__, __LINE__,
715 * Write the default pattern at each of the
716 * power-of-two offsets.
718 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
719 start[offset] = pattern;
723 * Check for address bits stuck high.
726 start[test_offset] = anti_pattern;
728 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
729 temp = start[offset];
730 if (temp != pattern) {
731 printf ("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
732 " expected 0x%.8lx, actual 0x%.8lx\n",
733 (ulong)&start[offset], pattern, temp);
737 start[test_offset] = pattern;
740 * Check for addr bits stuck low or shorted.
742 for (test_offset = 1; (test_offset & addr_mask) != 0; test_offset <<= 1) {
743 start[test_offset] = anti_pattern;
745 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
746 temp = start[offset];
747 if ((temp != pattern) && (offset != test_offset)) {
748 printf ("\nFAILURE: Address bit stuck low or shorted @"
749 " 0x%.8lx: expected 0x%.8lx, actual 0x%.8lx\n",
750 (ulong)&start[offset], pattern, temp);
754 start[test_offset] = pattern;
758 * Description: Test the integrity of a physical
759 * memory device by performing an
760 * increment/decrement test over the
761 * entire region. In the process every
762 * storage bit in the device is tested
763 * as a zero and a one. The base address
764 * and the size of the region are
765 * selected by the caller.
767 * Returns: 0 if the test succeeds, 1 if the test fails.
769 num_words = ((ulong)end - (ulong)start)/sizeof(vu_long) + 1;
772 * Fill memory with a known pattern.
774 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
775 start[offset] = pattern;
779 * Check each location and invert it for the second pass.
781 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
782 temp = start[offset];
783 if (temp != pattern) {
784 printf ("\nFAILURE (read/write) @ 0x%.8lx:"
785 " expected 0x%.8lx, actual 0x%.8lx)\n",
786 (ulong)&start[offset], pattern, temp);
790 anti_pattern = ~pattern;
791 start[offset] = anti_pattern;
795 * Check each location for the inverted pattern and zero it.
797 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
798 anti_pattern = ~pattern;
799 temp = start[offset];
800 if (temp != anti_pattern) {
801 printf ("\nFAILURE (read/write): @ 0x%.8lx:"
802 " expected 0x%.8lx, actual 0x%.8lx)\n",
803 (ulong)&start[offset], anti_pattern, temp);
810 #else /* The original, quickie test */
818 printf ("\rPattern %08lX Writing..."
820 "\b\b\b\b\b\b\b\b\b\b",
823 for (addr=start,val=pattern; addr<end; addr++) {
828 printf("Reading...");
830 for (addr=start,val=pattern; addr<end; addr++) {
832 if (readback != val) {
833 printf ("\nMem error @ 0x%08X: "
834 "found %08lX, expected %08lX\n",
835 (uint)addr, readback, val);
842 * Flip the pattern each time to make lots of zeros and
843 * then, the next time, lots of ones. We decrement
844 * the "negative" patterns and increment the "positive"
845 * patterns to preserve this feature.
847 if(pattern & 0x80000000) {
848 pattern = -pattern; /* complement & increment */
863 * mm{.b, .w, .l} {addr}
864 * nm{.b, .w, .l} {addr}
867 mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[])
871 extern char console_buffer[];
874 printf ("Usage:\n%s\n", cmdtp->usage);
878 #ifdef CONFIG_BOOT_RETRY_TIME
879 reset_cmd_timeout(); /* got a good command to get here */
881 /* We use the last specified parameters, unless new ones are
887 if ((flag & CMD_FLAG_REPEAT) == 0) {
888 /* New command specified. Check for a size specification.
889 * Defaults to long if no or incorrect specification.
891 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
894 /* Address is specified since argc > 1
896 addr = simple_strtoul(argv[1], NULL, 16);
897 addr += base_address;
900 #ifdef CONFIG_HAS_DATAFLASH
901 if (addr_dataflash(addr)){
902 printf("Can't modify DataFlash in place. Use cp instead.\n\r");
907 /* Print the address, followed by value. Then accept input for
908 * the next value. A non-converted value exits.
911 printf("%08lx:", addr);
913 printf(" %08x", *((uint *)addr));
915 printf(" %04x", *((ushort *)addr));
917 printf(" %02x", *((u_char *)addr));
919 nbytes = readline (" ? ");
920 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
921 /* <CR> pressed as only input, don't modify current
922 * location and move to next. "-" pressed will go back.
925 addr += nbytes ? -size : size;
927 #ifdef CONFIG_BOOT_RETRY_TIME
928 reset_cmd_timeout(); /* good enough to not time out */
931 #ifdef CONFIG_BOOT_RETRY_TIME
932 else if (nbytes == -2) {
933 break; /* timed out, exit the command */
938 i = simple_strtoul(console_buffer, &endp, 16);
939 nbytes = endp - console_buffer;
941 #ifdef CONFIG_BOOT_RETRY_TIME
942 /* good enough to not time out
949 *((ushort *)addr) = i;
951 *((u_char *)addr) = i;
963 int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
970 printf ("Usage:\n%s\n", cmdtp->usage);
974 addr = simple_strtoul (argv[1], NULL, 16);
975 addr += base_address;
977 length = simple_strtoul (argv[2], NULL, 16);
979 crc = crc32 (0, (const uchar *) addr, length);
981 printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
982 addr, addr + length - 1, crc);
985 ptr = (ulong *) simple_strtoul (argv[3], NULL, 16);
992 /**************************************************/
993 #if (CONFIG_COMMANDS & CFG_CMD_MEMORY)
996 "md - memory display\n",
997 "[.b, .w, .l] address [# of objects]\n - memory display\n"
1002 mm, 2, 1, do_mem_mm,
1003 "mm - memory modify (auto-incrementing)\n",
1004 "[.b, .w, .l] address\n" " - memory modify, auto increment address\n"
1009 nm, 2, 1, do_mem_nm,
1010 "nm - memory modify (constant address)\n",
1011 "[.b, .w, .l] address\n - memory modify, read and keep address\n"
1015 mw, 4, 1, do_mem_mw,
1016 "mw - memory write (fill)\n",
1017 "[.b, .w, .l] address value [count]\n - write memory\n"
1021 cp, 4, 1, do_mem_cp,
1022 "cp - memory copy\n",
1023 "[.b, .w, .l] source target count\n - copy memory\n"
1027 cmp, 4, 1, do_mem_cmp,
1028 "cmp - memory compare\n",
1029 "[.b, .w, .l] addr1 addr2 count\n - compare memory\n"
1033 crc32, 4, 1, do_mem_crc,
1034 "crc32 - checksum calculation\n",
1035 "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
1039 base, 2, 1, do_mem_base,
1040 "base - print or set address offset\n",
1041 "\n - print address offset for memory commands\n"
1042 "base off\n - set address offset for memory commands to 'off'\n"
1046 loop, 3, 1, do_mem_loop,
1047 "loop - infinite loop on address range\n",
1048 "[.b, .w, .l] address number_of_objects\n"
1049 " - loop on a set of addresses\n"
1053 mtest, 4, 1, do_mem_mtest,
1054 "mtest - simple RAM test\n",
1055 "[start [end [pattern]]]\n"
1056 " - simple RAM read/write test\n"
1060 #endif /* CFG_CMD_MEMORY */