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] == '.') {
66 #if (CONFIG_COMMANDS & CFG_CMD_MEMORY)
69 #define PRINTF(fmt,args...) printf (fmt ,##args)
71 #define PRINTF(fmt,args...)
74 static int mod_mem(cmd_tbl_t *, int, int, int, char *[]);
76 /* Display values from last command.
77 * Memory modify remembered values are different from display memory.
79 uint dp_last_addr, dp_last_size;
80 uint dp_last_length = 0x40;
81 uint mm_last_addr, mm_last_size;
83 static ulong base_address = 0;
88 * md{.b, .w, .l} {addr} {len}
90 #define DISP_LINE_LEN 16
91 int do_mem_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
94 ulong i, nbytes, linebytes;
99 /* We use the last specified parameters, unless new ones are
104 length = dp_last_length;
107 printf ("Usage:\n%s\n", cmdtp->usage);
111 if ((flag & CMD_FLAG_REPEAT) == 0) {
112 /* New command specified. Check for a size specification.
113 * Defaults to long if no or incorrect specification.
115 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
118 /* Address is specified since argc > 1
120 addr = simple_strtoul(argv[1], NULL, 16);
121 addr += base_address;
123 /* If another parameter, it is the length to display.
124 * Length is the number of objects, not number of bytes.
127 length = simple_strtoul(argv[2], NULL, 16);
132 * We buffer all read data, so we can make sure data is read only
133 * once, and all accesses are with the specified bus width.
135 nbytes = length * size;
137 char linebuf[DISP_LINE_LEN];
138 uint *uip = (uint *)linebuf;
139 ushort *usp = (ushort *)linebuf;
140 u_char *ucp = (u_char *)linebuf;
141 #ifdef CONFIG_HAS_DATAFLASH
144 printf("%08lx:", addr);
145 linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
147 #ifdef CONFIG_HAS_DATAFLASH
148 if ((rc = read_dataflash(addr, (linebytes/size)*size, linebuf)) == DATAFLASH_OK){
149 /* if outside dataflash */
151 dataflash_perror (rc);
154 for (i=0; i<linebytes; i+= size) {
156 printf(" %08x", *uip++);
157 } else if (size == 2) {
158 printf(" %04x", *usp++);
160 printf(" %02x", *ucp++);
165 } else { /* addr does not correspond to DataFlash */
167 for (i=0; i<linebytes; i+= size) {
169 printf(" %08x", (*uip++ = *((uint *)addr)));
170 } else if (size == 2) {
171 printf(" %04x", (*usp++ = *((ushort *)addr)));
173 printf(" %02x", (*ucp++ = *((u_char *)addr)));
177 #ifdef CONFIG_HAS_DATAFLASH
182 for (i=0; i<linebytes; i++) {
183 if ((*cp < 0x20) || (*cp > 0x7e))
195 } while (nbytes > 0);
198 dp_last_length = length;
203 int do_mem_mm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
205 return mod_mem (cmdtp, 1, flag, argc, argv);
207 int do_mem_nm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
209 return mod_mem (cmdtp, 0, flag, argc, argv);
212 int do_mem_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
214 ulong addr, writeval, count;
217 if ((argc < 3) || (argc > 4)) {
218 printf ("Usage:\n%s\n", cmdtp->usage);
222 /* Check for size specification.
224 if ((size = cmd_get_data_size(argv[0], 4)) < 1)
227 /* Address is specified since argc > 1
229 addr = simple_strtoul(argv[1], NULL, 16);
230 addr += base_address;
232 /* Get the value to write.
234 writeval = simple_strtoul(argv[2], NULL, 16);
238 count = simple_strtoul(argv[3], NULL, 16);
243 while (count-- > 0) {
245 *((ulong *)addr) = (ulong )writeval;
247 *((ushort *)addr) = (ushort)writeval;
249 *((u_char *)addr) = (u_char)writeval;
255 int do_mem_cmp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
257 ulong addr1, addr2, count, ngood;
262 printf ("Usage:\n%s\n", cmdtp->usage);
266 /* Check for size specification.
268 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
271 addr1 = simple_strtoul(argv[1], NULL, 16);
272 addr1 += base_address;
274 addr2 = simple_strtoul(argv[2], NULL, 16);
275 addr2 += base_address;
277 count = simple_strtoul(argv[3], NULL, 16);
279 #ifdef CONFIG_HAS_DATAFLASH
280 if (addr_dataflash(addr1) | addr_dataflash(addr2)){
281 printf("Comparison with DataFlash space not supported.\n\r");
288 while (count-- > 0) {
290 ulong word1 = *(ulong *)addr1;
291 ulong word2 = *(ulong *)addr2;
292 if (word1 != word2) {
293 printf("word at 0x%08lx (0x%08lx) "
294 "!= word at 0x%08lx (0x%08lx)\n",
295 addr1, word1, addr2, word2);
300 else if (size == 2) {
301 ushort hword1 = *(ushort *)addr1;
302 ushort hword2 = *(ushort *)addr2;
303 if (hword1 != hword2) {
304 printf("halfword at 0x%08lx (0x%04x) "
305 "!= halfword at 0x%08lx (0x%04x)\n",
306 addr1, hword1, addr2, hword2);
312 u_char byte1 = *(u_char *)addr1;
313 u_char byte2 = *(u_char *)addr2;
314 if (byte1 != byte2) {
315 printf("byte at 0x%08lx (0x%02x) "
316 "!= byte at 0x%08lx (0x%02x)\n",
317 addr1, byte1, addr2, byte2);
327 printf("Total of %ld %s%s were the same\n",
328 ngood, size == 4 ? "word" : size == 2 ? "halfword" : "byte",
329 ngood == 1 ? "" : "s");
333 int do_mem_cp ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
335 ulong addr, dest, count;
339 printf ("Usage:\n%s\n", cmdtp->usage);
343 /* Check for size specification.
345 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
348 addr = simple_strtoul(argv[1], NULL, 16);
349 addr += base_address;
351 dest = simple_strtoul(argv[2], NULL, 16);
352 dest += base_address;
354 count = simple_strtoul(argv[3], NULL, 16);
357 puts ("Zero length ???\n");
362 /* check if we are copying to Flash */
363 if ( (addr2info(dest) != NULL)
364 #ifdef CONFIG_HAS_DATAFLASH
365 && (!addr_dataflash(addr))
370 printf ("Copy to Flash... ");
372 rc = flash_write ((uchar *)addr, dest, count*size);
382 #if (CONFIG_COMMANDS & CFG_CMD_MMC)
383 if (mmc2info(dest)) {
386 printf ("Copy to MMC... ");
387 switch (rc = mmc_write ((uchar *)addr, dest, count*size)) {
395 printf ("%s[%d] FIXME: rc=%d\n",__FILE__,__LINE__,rc);
402 if (mmc2info(addr)) {
405 printf ("Copy from MMC... ");
406 switch (rc = mmc_read (addr, (uchar *)dest, count*size)) {
414 printf ("%s[%d] FIXME: rc=%d\n",__FILE__,__LINE__,rc);
422 #ifdef CONFIG_HAS_DATAFLASH
423 /* Check if we are copying from RAM or Flash to DataFlash */
424 if (addr_dataflash(dest) && !addr_dataflash(addr)){
427 printf ("Copy to DataFlash... ");
429 rc = write_dataflash (dest, addr, count*size);
432 dataflash_perror (rc);
439 /* Check if we are copying from DataFlash to RAM */
440 if (addr_dataflash(addr) && !addr_dataflash(dest) && (addr2info(dest)==NULL) ){
442 rc = read_dataflash(addr, count * size, (char *) dest);
444 dataflash_perror (rc);
450 if (addr_dataflash(addr) && addr_dataflash(dest)){
451 printf("Unsupported combination of source/destination.\n\r");
456 while (count-- > 0) {
458 *((ulong *)dest) = *((ulong *)addr);
460 *((ushort *)dest) = *((ushort *)addr);
462 *((u_char *)dest) = *((u_char *)addr);
469 int do_mem_base (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
472 /* Set new base address.
474 base_address = simple_strtoul(argv[1], NULL, 16);
476 /* Print the current base address.
478 printf("Base Address: 0x%08lx\n", base_address);
482 int do_mem_loop (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
484 ulong addr, length, i, junk;
486 volatile uint *longp;
487 volatile ushort *shortp;
491 printf ("Usage:\n%s\n", cmdtp->usage);
495 /* Check for a size spefication.
496 * Defaults to long if no or incorrect specification.
498 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
501 /* Address is always specified.
503 addr = simple_strtoul(argv[1], NULL, 16);
505 /* Length is the number of objects, not number of bytes.
507 length = simple_strtoul(argv[2], NULL, 16);
509 /* We want to optimize the loops to run as fast as possible.
510 * If we have only one object, just run infinite loops.
514 longp = (uint *)addr;
519 shortp = (ushort *)addr;
530 longp = (uint *)addr;
538 shortp = (ushort *)addr;
553 * Perform a memory test. A more complete alternative test can be
554 * configured using CFG_ALT_MEMTEST. The complete test loops until
555 * interrupted by ctrl-c or by a failure of one of the sub-tests.
557 int do_mem_mtest (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
559 vu_long *addr, *start, *end;
563 #if defined(CFG_ALT_MEMTEST)
569 vu_long anti_pattern;
571 #if defined(CFG_MEMTEST_SCRATCH)
572 vu_long *dummy = (vu_long*)CFG_MEMTEST_SCRATCH;
574 vu_long *dummy = NULL;
579 static const ulong bitpattern[] = {
580 0x00000001, /* single bit */
581 0x00000003, /* two adjacent bits */
582 0x00000007, /* three adjacent bits */
583 0x0000000F, /* four adjacent bits */
584 0x00000005, /* two non-adjacent bits */
585 0x00000015, /* three non-adjacent bits */
586 0x00000055, /* four non-adjacent bits */
587 0xaaaaaaaa, /* alternating 1/0 */
596 start = (ulong *)simple_strtoul(argv[1], NULL, 16);
598 start = (ulong *)CFG_MEMTEST_START;
602 end = (ulong *)simple_strtoul(argv[2], NULL, 16);
604 end = (ulong *)(CFG_MEMTEST_END);
608 pattern = (ulong)simple_strtoul(argv[3], NULL, 16);
613 #if defined(CFG_ALT_MEMTEST)
614 printf ("Testing %08x ... %08x:\n", (uint)start, (uint)end);
615 PRINTF("%s:%d: start 0x%p end 0x%p\n",
616 __FUNCTION__, __LINE__, start, end);
624 printf("Iteration: %6d\r", iterations);
625 PRINTF("Iteration: %6d\n", iterations);
629 * Data line test: write a pattern to the first
630 * location, write the 1's complement to a 'parking'
631 * address (changes the state of the data bus so a
632 * floating bus doen't give a false OK), and then
633 * read the value back. Note that we read it back
634 * into a variable because the next time we read it,
635 * it might be right (been there, tough to explain to
636 * the quality guys why it prints a failure when the
637 * "is" and "should be" are obviously the same in the
640 * Rather than exhaustively testing, we test some
641 * patterns by shifting '1' bits through a field of
642 * '0's and '0' bits through a field of '1's (i.e.
643 * pattern and ~pattern).
646 for (j = 0; j < sizeof(bitpattern)/sizeof(bitpattern[0]); j++) {
648 for(; val != 0; val <<= 1) {
650 *dummy = ~val; /* clear the test data off of the bus */
652 if(readback != val) {
653 printf ("FAILURE (data line): "
654 "expected %08lx, actual %08lx\n",
660 if(readback != ~val) {
661 printf ("FAILURE (data line): "
662 "Is %08lx, should be %08lx\n",
669 * Based on code whose Original Author and Copyright
670 * information follows: Copyright (c) 1998 by Michael
671 * Barr. This software is placed into the public
672 * domain and may be used for any purpose. However,
673 * this notice must not be changed or removed and no
674 * warranty is either expressed or implied by its
675 * publication or distribution.
681 * Description: Test the address bus wiring in a
682 * memory region by performing a walking
683 * 1's test on the relevant bits of the
684 * address and checking for aliasing.
685 * This test will find single-bit
686 * address failures such as stuck -high,
687 * stuck-low, and shorted pins. The base
688 * address and size of the region are
689 * selected by the caller.
691 * Notes: For best results, the selected base
692 * address should have enough LSB 0's to
693 * guarantee single address bit changes.
694 * For example, to test a 64-Kbyte
695 * region, select a base address on a
696 * 64-Kbyte boundary. Also, select the
697 * region size as a power-of-two if at
700 * Returns: 0 if the test succeeds, 1 if the test fails.
702 * ## NOTE ## Be sure to specify start and end
703 * addresses such that addr_mask has
704 * lots of bits set. For example an
705 * address range of 01000000 02000000 is
706 * bad while a range of 01000000
707 * 01ffffff is perfect.
709 addr_mask = ((ulong)end - (ulong)start)/sizeof(vu_long);
710 pattern = (vu_long) 0xaaaaaaaa;
711 anti_pattern = (vu_long) 0x55555555;
713 PRINTF("%s:%d: addr mask = 0x%.8lx\n",
714 __FUNCTION__, __LINE__,
717 * Write the default pattern at each of the
718 * power-of-two offsets.
720 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
721 start[offset] = pattern;
725 * Check for address bits stuck high.
728 start[test_offset] = anti_pattern;
730 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
731 temp = start[offset];
732 if (temp != pattern) {
733 printf ("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
734 " expected 0x%.8lx, actual 0x%.8lx\n",
735 (ulong)&start[offset], pattern, temp);
739 start[test_offset] = pattern;
742 * Check for addr bits stuck low or shorted.
744 for (test_offset = 1; (test_offset & addr_mask) != 0; test_offset <<= 1) {
745 start[test_offset] = anti_pattern;
747 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
748 temp = start[offset];
749 if ((temp != pattern) && (offset != test_offset)) {
750 printf ("\nFAILURE: Address bit stuck low or shorted @"
751 " 0x%.8lx: expected 0x%.8lx, actual 0x%.8lx\n",
752 (ulong)&start[offset], pattern, temp);
756 start[test_offset] = pattern;
760 * Description: Test the integrity of a physical
761 * memory device by performing an
762 * increment/decrement test over the
763 * entire region. In the process every
764 * storage bit in the device is tested
765 * as a zero and a one. The base address
766 * and the size of the region are
767 * selected by the caller.
769 * Returns: 0 if the test succeeds, 1 if the test fails.
771 num_words = ((ulong)end - (ulong)start)/sizeof(vu_long) + 1;
774 * Fill memory with a known pattern.
776 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
777 start[offset] = pattern;
781 * Check each location and invert it for the second pass.
783 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
784 temp = start[offset];
785 if (temp != pattern) {
786 printf ("\nFAILURE (read/write) @ 0x%.8lx:"
787 " expected 0x%.8lx, actual 0x%.8lx)\n",
788 (ulong)&start[offset], pattern, temp);
792 anti_pattern = ~pattern;
793 start[offset] = anti_pattern;
797 * Check each location for the inverted pattern and zero it.
799 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
800 anti_pattern = ~pattern;
801 temp = start[offset];
802 if (temp != anti_pattern) {
803 printf ("\nFAILURE (read/write): @ 0x%.8lx:"
804 " expected 0x%.8lx, actual 0x%.8lx)\n",
805 (ulong)&start[offset], anti_pattern, temp);
812 #else /* The original, quickie test */
820 printf ("\rPattern %08lX Writing..."
822 "\b\b\b\b\b\b\b\b\b\b",
825 for (addr=start,val=pattern; addr<end; addr++) {
830 printf("Reading...");
832 for (addr=start,val=pattern; addr<end; addr++) {
834 if (readback != val) {
835 printf ("\nMem error @ 0x%08X: "
836 "found %08lX, expected %08lX\n",
837 (uint)addr, readback, val);
844 * Flip the pattern each time to make lots of zeros and
845 * then, the next time, lots of ones. We decrement
846 * the "negative" patterns and increment the "positive"
847 * patterns to preserve this feature.
849 if(pattern & 0x80000000) {
850 pattern = -pattern; /* complement & increment */
865 * mm{.b, .w, .l} {addr}
866 * nm{.b, .w, .l} {addr}
869 mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[])
873 extern char console_buffer[];
876 printf ("Usage:\n%s\n", cmdtp->usage);
880 #ifdef CONFIG_BOOT_RETRY_TIME
881 reset_cmd_timeout(); /* got a good command to get here */
883 /* We use the last specified parameters, unless new ones are
889 if ((flag & CMD_FLAG_REPEAT) == 0) {
890 /* New command specified. Check for a size specification.
891 * Defaults to long if no or incorrect specification.
893 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
896 /* Address is specified since argc > 1
898 addr = simple_strtoul(argv[1], NULL, 16);
899 addr += base_address;
902 #ifdef CONFIG_HAS_DATAFLASH
903 if (addr_dataflash(addr)){
904 printf("Can't modify DataFlash in place. Use cp instead.\n\r");
909 /* Print the address, followed by value. Then accept input for
910 * the next value. A non-converted value exits.
913 printf("%08lx:", addr);
915 printf(" %08x", *((uint *)addr));
917 printf(" %04x", *((ushort *)addr));
919 printf(" %02x", *((u_char *)addr));
921 nbytes = readline (" ? ");
922 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
923 /* <CR> pressed as only input, don't modify current
924 * location and move to next. "-" pressed will go back.
927 addr += nbytes ? -size : size;
929 #ifdef CONFIG_BOOT_RETRY_TIME
930 reset_cmd_timeout(); /* good enough to not time out */
933 #ifdef CONFIG_BOOT_RETRY_TIME
934 else if (nbytes == -2) {
935 break; /* timed out, exit the command */
940 i = simple_strtoul(console_buffer, &endp, 16);
941 nbytes = endp - console_buffer;
943 #ifdef CONFIG_BOOT_RETRY_TIME
944 /* good enough to not time out
951 *((ushort *)addr) = i;
953 *((u_char *)addr) = i;
965 int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
972 printf ("Usage:\n%s\n", cmdtp->usage);
976 addr = simple_strtoul (argv[1], NULL, 16);
977 addr += base_address;
979 length = simple_strtoul (argv[2], NULL, 16);
981 crc = crc32 (0, (const uchar *) addr, length);
983 printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
984 addr, addr + length - 1, crc);
987 ptr = (ulong *) simple_strtoul (argv[3], NULL, 16);
994 /**************************************************/
995 #if (CONFIG_COMMANDS & CFG_CMD_MEMORY)
998 "md - memory display\n",
999 "[.b, .w, .l] address [# of objects]\n - memory display\n"
1004 mm, 2, 1, do_mem_mm,
1005 "mm - memory modify (auto-incrementing)\n",
1006 "[.b, .w, .l] address\n" " - memory modify, auto increment address\n"
1011 nm, 2, 1, do_mem_nm,
1012 "nm - memory modify (constant address)\n",
1013 "[.b, .w, .l] address\n - memory modify, read and keep address\n"
1017 mw, 4, 1, do_mem_mw,
1018 "mw - memory write (fill)\n",
1019 "[.b, .w, .l] address value [count]\n - write memory\n"
1023 cp, 4, 1, do_mem_cp,
1024 "cp - memory copy\n",
1025 "[.b, .w, .l] source target count\n - copy memory\n"
1029 cmp, 4, 1, do_mem_cmp,
1030 "cmp - memory compare\n",
1031 "[.b, .w, .l] addr1 addr2 count\n - compare memory\n"
1035 crc32, 4, 1, do_mem_crc,
1036 "crc32 - checksum calculation\n",
1037 "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
1041 base, 2, 1, do_mem_base,
1042 "base - print or set address offset\n",
1043 "\n - print address offset for memory commands\n"
1044 "base off\n - set address offset for memory commands to 'off'\n"
1048 loop, 3, 1, do_mem_loop,
1049 "loop - infinite loop on address range\n",
1050 "[.b, .w, .l] address number_of_objects\n"
1051 " - loop on a set of addresses\n"
1055 mtest, 4, 1, do_mem_mtest,
1056 "mtest - simple RAM test\n",
1057 "[start [end [pattern]]]\n"
1058 " - simple RAM read/write test\n"
1062 #endif /* CFG_CMD_MEMORY */