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,
33 #if (CONFIG_COMMANDS & CFG_CMD_MMC)
36 #ifdef CONFIG_HAS_DATAFLASH
37 #include <dataflash.h>
40 #if (CONFIG_COMMANDS & (CFG_CMD_MEMORY | CFG_CMD_PCI | CFG_CMD_I2C\
42 int cmd_get_data_size(char* arg, int default_size)
44 /* Check for a size specification .b, .w or .l.
46 int len = strlen(arg);
47 if (len > 2 && arg[len-2] == '.') {
61 #if (CONFIG_COMMANDS & CFG_CMD_MEMORY)
64 #define PRINTF(fmt,args...) printf (fmt ,##args)
66 #define PRINTF(fmt,args...)
69 static int mod_mem(cmd_tbl_t *, int, int, int, char *[]);
71 /* Display values from last command.
72 * Memory modify remembered values are different from display memory.
74 uint dp_last_addr, dp_last_size;
75 uint dp_last_length = 0x40;
76 uint mm_last_addr, mm_last_size;
78 static ulong base_address = 0;
83 * md{.b, .w, .l} {addr} {len}
85 #define DISP_LINE_LEN 16
86 int do_mem_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
88 ulong addr, size, length;
89 ulong i, nbytes, linebytes;
93 /* We use the last specified parameters, unless new ones are
98 length = dp_last_length;
101 printf ("Usage:\n%s\n", cmdtp->usage);
105 if ((flag & CMD_FLAG_REPEAT) == 0) {
106 /* New command specified. Check for a size specification.
107 * Defaults to long if no or incorrect specification.
109 size = cmd_get_data_size(argv[0], 4);
111 /* Address is specified since argc > 1
113 addr = simple_strtoul(argv[1], NULL, 16);
114 addr += base_address;
116 /* If another parameter, it is the length to display.
117 * Length is the number of objects, not number of bytes.
120 length = simple_strtoul(argv[2], NULL, 16);
125 * We buffer all read data, so we can make sure data is read only
126 * once, and all accesses are with the specified bus width.
128 nbytes = length * size;
130 char linebuf[DISP_LINE_LEN];
131 uint *uip = (uint *)linebuf;
132 ushort *usp = (ushort *)linebuf;
133 u_char *ucp = (u_char *)linebuf;
135 printf("%08lx:", addr);
136 linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
138 #ifdef CONFIG_HAS_DATAFLASH
139 if (read_dataflash(addr, (linebytes/size)*size, linebuf) != -1){
141 for (i=0; i<linebytes; i+= size) {
143 printf(" %08x", *uip++);
144 } else if (size == 2) {
145 printf(" %04x", *usp++);
147 printf(" %02x", *ucp++);
152 } else { /* addr does not correspond to DataFlash */
154 for (i=0; i<linebytes; i+= size) {
156 printf(" %08x", (*uip++ = *((uint *)addr)));
157 } else if (size == 2) {
158 printf(" %04x", (*usp++ = *((ushort *)addr)));
160 printf(" %02x", (*ucp++ = *((u_char *)addr)));
164 #ifdef CONFIG_HAS_DATAFLASH
169 for (i=0; i<linebytes; i++) {
170 if ((*cp < 0x20) || (*cp > 0x7e))
182 } while (nbytes > 0);
185 dp_last_length = length;
190 int do_mem_mm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
192 return mod_mem (cmdtp, 1, flag, argc, argv);
194 int do_mem_nm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
196 return mod_mem (cmdtp, 0, flag, argc, argv);
199 int do_mem_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
201 ulong addr, size, writeval, count;
203 if ((argc < 3) || (argc > 4)) {
204 printf ("Usage:\n%s\n", cmdtp->usage);
208 /* Check for size specification.
210 size = cmd_get_data_size(argv[0], 4);
212 /* Address is specified since argc > 1
214 addr = simple_strtoul(argv[1], NULL, 16);
215 addr += base_address;
217 /* Get the value to write.
219 writeval = simple_strtoul(argv[2], NULL, 16);
223 count = simple_strtoul(argv[3], NULL, 16);
228 while (count-- > 0) {
230 *((ulong *)addr) = (ulong )writeval;
232 *((ushort *)addr) = (ushort)writeval;
234 *((u_char *)addr) = (u_char)writeval;
240 int do_mem_cmp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
242 ulong size, addr1, addr2, count, ngood;
246 printf ("Usage:\n%s\n", cmdtp->usage);
250 /* Check for size specification.
252 size = cmd_get_data_size(argv[0], 4);
254 addr1 = simple_strtoul(argv[1], NULL, 16);
255 addr1 += base_address;
257 addr2 = simple_strtoul(argv[2], NULL, 16);
258 addr2 += base_address;
260 count = simple_strtoul(argv[3], NULL, 16);
262 #ifdef CONFIG_HAS_DATAFLASH
263 if (addr_dataflash(addr1) | addr_dataflash(addr2)){
264 printf("Comparison with DataFlash space not supported.\n\r");
271 while (count-- > 0) {
273 ulong word1 = *(ulong *)addr1;
274 ulong word2 = *(ulong *)addr2;
275 if (word1 != word2) {
276 printf("word at 0x%08lx (0x%08lx) "
277 "!= word at 0x%08lx (0x%08lx)\n",
278 addr1, word1, addr2, word2);
283 else if (size == 2) {
284 ushort hword1 = *(ushort *)addr1;
285 ushort hword2 = *(ushort *)addr2;
286 if (hword1 != hword2) {
287 printf("halfword at 0x%08lx (0x%04x) "
288 "!= halfword at 0x%08lx (0x%04x)\n",
289 addr1, hword1, addr2, hword2);
295 u_char byte1 = *(u_char *)addr1;
296 u_char byte2 = *(u_char *)addr2;
297 if (byte1 != byte2) {
298 printf("byte at 0x%08lx (0x%02x) "
299 "!= byte at 0x%08lx (0x%02x)\n",
300 addr1, byte1, addr2, byte2);
310 printf("Total of %ld %s%s were the same\n",
311 ngood, size == 4 ? "word" : size == 2 ? "halfword" : "byte",
312 ngood == 1 ? "" : "s");
316 int do_mem_cp ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
318 ulong addr, size, dest, count;
321 printf ("Usage:\n%s\n", cmdtp->usage);
325 /* Check for size specification.
327 size = cmd_get_data_size(argv[0], 4);
329 addr = simple_strtoul(argv[1], NULL, 16);
330 addr += base_address;
332 dest = simple_strtoul(argv[2], NULL, 16);
333 dest += base_address;
335 count = simple_strtoul(argv[3], NULL, 16);
338 puts ("Zero length ???\n");
343 /* check if we are copying to Flash */
344 if ( (addr2info(dest) != NULL)
345 #ifdef CONFIG_HAS_DATAFLASH
346 && (!addr_dataflash(addr))
351 printf ("Copy to Flash... ");
353 rc = flash_write ((uchar *)addr, dest, count*size);
363 #if (CONFIG_COMMANDS & CFG_CMD_MMC)
364 if (mmc2info(dest)) {
367 printf ("Copy to MMC... ");
368 switch (rc = mmc_write ((uchar *)addr, dest, count*size)) {
376 printf ("%s[%d] FIXME: rc=%d\n",__FILE__,__LINE__,rc);
383 if (mmc2info(addr)) {
386 printf ("Copy from MMC... ");
387 switch (rc = mmc_read (addr, (uchar *)dest, count*size)) {
395 printf ("%s[%d] FIXME: rc=%d\n",__FILE__,__LINE__,rc);
403 #ifdef CONFIG_HAS_DATAFLASH
404 /* Check if we are copying from RAM or Flash to DataFlash */
405 if (addr_dataflash(dest) && !addr_dataflash(addr)){
408 printf ("Copy to DataFlash... ");
410 rc = write_dataflash (dest, addr, count*size);
413 dataflash_perror (rc);
420 /* Check if we are copying from DataFlash to RAM */
421 if (addr_dataflash(addr) && !addr_dataflash(dest) && (addr2info(dest)==NULL) ){
422 read_dataflash(addr, count * size, (char *) dest);
426 if (addr_dataflash(addr) && addr_dataflash(dest)){
427 printf("Unsupported combination of source/destination.\n\r");
432 while (count-- > 0) {
434 *((ulong *)dest) = *((ulong *)addr);
436 *((ushort *)dest) = *((ushort *)addr);
438 *((u_char *)dest) = *((u_char *)addr);
445 int do_mem_base (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
448 /* Set new base address.
450 base_address = simple_strtoul(argv[1], NULL, 16);
452 /* Print the current base address.
454 printf("Base Address: 0x%08lx\n", base_address);
458 int do_mem_loop (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
460 ulong addr, size, length, i, junk;
461 volatile uint *longp;
462 volatile ushort *shortp;
466 printf ("Usage:\n%s\n", cmdtp->usage);
470 /* Check for a size spefication.
471 * Defaults to long if no or incorrect specification.
473 size = cmd_get_data_size(argv[0], 4);
475 /* Address is always specified.
477 addr = simple_strtoul(argv[1], NULL, 16);
479 /* Length is the number of objects, not number of bytes.
481 length = simple_strtoul(argv[2], NULL, 16);
483 /* We want to optimize the loops to run as fast as possible.
484 * If we have only one object, just run infinite loops.
488 longp = (uint *)addr;
493 shortp = (ushort *)addr;
504 longp = (uint *)addr;
512 shortp = (ushort *)addr;
527 * Perform a memory test. A more complete alternative test can be
528 * configured using CFG_ALT_MEMTEST. The complete test loops until
529 * interrupted by ctrl-c or by a failure of one of the sub-tests.
531 int do_mem_mtest (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
533 vu_long *addr, *start, *end;
537 #if defined(CFG_ALT_MEMTEST)
543 vu_long anti_pattern;
545 vu_long *dummy = NULL;
549 static const ulong bitpattern[] = {
550 0x00000001, /* single bit */
551 0x00000003, /* two adjacent bits */
552 0x00000007, /* three adjacent bits */
553 0x0000000F, /* four adjacent bits */
554 0x00000005, /* two non-adjacent bits */
555 0x00000015, /* three non-adjacent bits */
556 0x00000055, /* four non-adjacent bits */
557 0xaaaaaaaa, /* alternating 1/0 */
566 start = (ulong *)simple_strtoul(argv[1], NULL, 16);
568 start = (ulong *)CFG_MEMTEST_START;
572 end = (ulong *)simple_strtoul(argv[2], NULL, 16);
574 end = (ulong *)(CFG_MEMTEST_END);
578 pattern = (ulong)simple_strtoul(argv[3], NULL, 16);
583 #if defined(CFG_ALT_MEMTEST)
584 printf ("Testing %08x ... %08x:\n", (uint)start, (uint)end);
585 PRINTF("%s:%d: start 0x%p end 0x%p\n",
586 __FUNCTION__, __LINE__, start, end);
594 printf("Iteration: %6d\r", iterations);
595 PRINTF("Iteration: %6d\n", iterations);
599 * Data line test: write a pattern to the first
600 * location, write the 1's complement to a 'parking'
601 * address (changes the state of the data bus so a
602 * floating bus doen't give a false OK), and then
603 * read the value back. Note that we read it back
604 * into a variable because the next time we read it,
605 * it might be right (been there, tough to explain to
606 * the quality guys why it prints a failure when the
607 * "is" and "should be" are obviously the same in the
610 * Rather than exhaustively testing, we test some
611 * patterns by shifting '1' bits through a field of
612 * '0's and '0' bits through a field of '1's (i.e.
613 * pattern and ~pattern).
616 for (j = 0; j < sizeof(bitpattern)/sizeof(bitpattern[0]); j++) {
618 for(; val != 0; val <<= 1) {
620 *dummy = ~val; /* clear the test data off of the bus */
622 if(readback != val) {
623 printf ("FAILURE (data line): "
624 "expected %08lx, actual %08lx\n",
630 if(readback != ~val) {
631 printf ("FAILURE (data line): "
632 "Is %08lx, should be %08lx\n",
639 * Based on code whose Original Author and Copyright
640 * information follows: Copyright (c) 1998 by Michael
641 * Barr. This software is placed into the public
642 * domain and may be used for any purpose. However,
643 * this notice must not be changed or removed and no
644 * warranty is either expressed or implied by its
645 * publication or distribution.
651 * Description: Test the address bus wiring in a
652 * memory region by performing a walking
653 * 1's test on the relevant bits of the
654 * address and checking for aliasing.
655 * This test will find single-bit
656 * address failures such as stuck -high,
657 * stuck-low, and shorted pins. The base
658 * address and size of the region are
659 * selected by the caller.
661 * Notes: For best results, the selected base
662 * address should have enough LSB 0's to
663 * guarantee single address bit changes.
664 * For example, to test a 64-Kbyte
665 * region, select a base address on a
666 * 64-Kbyte boundary. Also, select the
667 * region size as a power-of-two if at
670 * Returns: 0 if the test succeeds, 1 if the test fails.
672 * ## NOTE ## Be sure to specify start and end
673 * addresses such that addr_mask has
674 * lots of bits set. For example an
675 * address range of 01000000 02000000 is
676 * bad while a range of 01000000
677 * 01ffffff is perfect.
679 addr_mask = ((ulong)end - (ulong)start)/sizeof(vu_long);
680 pattern = (vu_long) 0xaaaaaaaa;
681 anti_pattern = (vu_long) 0x55555555;
683 PRINTF("%s:%d: addr mask = 0x%.8lx\n",
684 __FUNCTION__, __LINE__,
687 * Write the default pattern at each of the
688 * power-of-two offsets.
690 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
691 start[offset] = pattern;
695 * Check for address bits stuck high.
698 start[test_offset] = anti_pattern;
700 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
701 temp = start[offset];
702 if (temp != pattern) {
703 printf ("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
704 " expected 0x%.8lx, actual 0x%.8lx\n",
705 (ulong)&start[offset], pattern, temp);
709 start[test_offset] = pattern;
712 * Check for addr bits stuck low or shorted.
714 for (test_offset = 1; (test_offset & addr_mask) != 0; test_offset <<= 1) {
715 start[test_offset] = anti_pattern;
717 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
718 temp = start[offset];
719 if ((temp != pattern) && (offset != test_offset)) {
720 printf ("\nFAILURE: Address bit stuck low or shorted @"
721 " 0x%.8lx: expected 0x%.8lx, actual 0x%.8lx\n",
722 (ulong)&start[offset], pattern, temp);
726 start[test_offset] = pattern;
730 * Description: Test the integrity of a physical
731 * memory device by performing an
732 * increment/decrement test over the
733 * entire region. In the process every
734 * storage bit in the device is tested
735 * as a zero and a one. The base address
736 * and the size of the region are
737 * selected by the caller.
739 * Returns: 0 if the test succeeds, 1 if the test fails.
741 num_words = ((ulong)end - (ulong)start)/sizeof(vu_long) + 1;
744 * Fill memory with a known pattern.
746 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
747 start[offset] = pattern;
751 * Check each location and invert it for the second pass.
753 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
754 temp = start[offset];
755 if (temp != pattern) {
756 printf ("\nFAILURE (read/write) @ 0x%.8lx:"
757 " expected 0x%.8lx, actual 0x%.8lx)\n",
758 (ulong)&start[offset], pattern, temp);
762 anti_pattern = ~pattern;
763 start[offset] = anti_pattern;
767 * Check each location for the inverted pattern and zero it.
769 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
770 anti_pattern = ~pattern;
771 temp = start[offset];
772 if (temp != anti_pattern) {
773 printf ("\nFAILURE (read/write): @ 0x%.8lx:"
774 " expected 0x%.8lx, actual 0x%.8lx)\n",
775 (ulong)&start[offset], anti_pattern, temp);
782 #else /* The original, quickie test */
790 printf ("\rPattern %08lX Writing..."
792 "\b\b\b\b\b\b\b\b\b\b",
795 for (addr=start,val=pattern; addr<end; addr++) {
800 printf("Reading...");
802 for (addr=start,val=pattern; addr<end; addr++) {
804 if (readback != val) {
805 printf ("\nMem error @ 0x%08X: "
806 "found %08lX, expected %08lX\n",
807 (uint)addr, readback, val);
814 * Flip the pattern each time to make lots of zeros and
815 * then, the next time, lots of ones. We decrement
816 * the "negative" patterns and increment the "positive"
817 * patterns to preserve this feature.
819 if(pattern & 0x80000000) {
820 pattern = -pattern; /* complement & increment */
835 * mm{.b, .w, .l} {addr}
836 * nm{.b, .w, .l} {addr}
839 mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[])
843 extern char console_buffer[];
846 printf ("Usage:\n%s\n", cmdtp->usage);
850 #ifdef CONFIG_BOOT_RETRY_TIME
851 reset_cmd_timeout(); /* got a good command to get here */
853 /* We use the last specified parameters, unless new ones are
859 if ((flag & CMD_FLAG_REPEAT) == 0) {
860 /* New command specified. Check for a size specification.
861 * Defaults to long if no or incorrect specification.
863 size = cmd_get_data_size(argv[0], 4);
865 /* Address is specified since argc > 1
867 addr = simple_strtoul(argv[1], NULL, 16);
868 addr += base_address;
871 #ifdef CONFIG_HAS_DATAFLASH
872 if (addr_dataflash(addr)){
873 printf("Can't modify DataFlash in place. Use cp instead.\n\r");
878 /* Print the address, followed by value. Then accept input for
879 * the next value. A non-converted value exits.
882 printf("%08lx:", addr);
884 printf(" %08x", *((uint *)addr));
886 printf(" %04x", *((ushort *)addr));
888 printf(" %02x", *((u_char *)addr));
890 nbytes = readline (" ? ");
891 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
892 /* <CR> pressed as only input, don't modify current
893 * location and move to next. "-" pressed will go back.
896 addr += nbytes ? -size : size;
898 #ifdef CONFIG_BOOT_RETRY_TIME
899 reset_cmd_timeout(); /* good enough to not time out */
902 #ifdef CONFIG_BOOT_RETRY_TIME
903 else if (nbytes == -2) {
904 break; /* timed out, exit the command */
909 i = simple_strtoul(console_buffer, &endp, 16);
910 nbytes = endp - console_buffer;
912 #ifdef CONFIG_BOOT_RETRY_TIME
913 /* good enough to not time out
920 *((ushort *)addr) = i;
922 *((u_char *)addr) = i;
934 int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
941 printf ("Usage:\n%s\n", cmdtp->usage);
945 addr = simple_strtoul (argv[1], NULL, 16);
946 addr += base_address;
948 length = simple_strtoul (argv[2], NULL, 16);
950 crc = crc32 (0, (const uchar *) addr, length);
952 printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
953 addr, addr + length - 1, crc);
956 ptr = (ulong *) simple_strtoul (argv[3], NULL, 16);
963 #endif /* CFG_CMD_MEMORY */