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)
34 #if (CONFIG_COMMANDS & (CFG_CMD_MEMORY | CFG_CMD_PCI | CFG_CMD_I2C\
36 int cmd_get_data_size(char* arg, int default_size)
38 /* Check for a size specification .b, .w or .l.
40 int len = strlen(arg);
41 if (len > 2 && arg[len-2] == '.') {
55 #if (CONFIG_COMMANDS & CFG_CMD_MEMORY)
58 #define PRINTF(fmt,args...) printf (fmt ,##args)
60 #define PRINTF(fmt,args...)
63 static int mod_mem(cmd_tbl_t *, int, int, int, char *[]);
65 /* Display values from last command.
66 * Memory modify remembered values are different from display memory.
68 uint dp_last_addr, dp_last_size;
69 uint dp_last_length = 0x40;
70 uint mm_last_addr, mm_last_size;
72 static ulong base_address = 0;
77 * md{.b, .w, .l} {addr} {len}
79 #define DISP_LINE_LEN 16
80 int do_mem_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
82 ulong addr, size, length;
83 ulong i, nbytes, linebytes;
87 /* We use the last specified parameters, unless new ones are
92 length = dp_last_length;
95 printf ("Usage:\n%s\n", cmdtp->usage);
99 if ((flag & CMD_FLAG_REPEAT) == 0) {
100 /* New command specified. Check for a size specification.
101 * Defaults to long if no or incorrect specification.
103 size = cmd_get_data_size(argv[0], 4);
105 /* Address is specified since argc > 1
107 addr = simple_strtoul(argv[1], NULL, 16);
108 addr += base_address;
110 /* If another parameter, it is the length to display.
111 * Length is the number of objects, not number of bytes.
114 length = simple_strtoul(argv[2], NULL, 16);
119 * We buffer all read data, so we can make sure data is read only
120 * once, and all accesses are with the specified bus width.
122 nbytes = length * size;
124 char linebuf[DISP_LINE_LEN];
125 uint *uip = (uint *)linebuf;
126 ushort *usp = (ushort *)linebuf;
127 u_char *ucp = (u_char *)linebuf;
129 printf("%08lx:", addr);
130 linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
131 for (i=0; i<linebytes; i+= size) {
133 printf(" %08x", (*uip++ = *((uint *)addr)));
134 } else if (size == 2) {
135 printf(" %04x", (*usp++ = *((ushort *)addr)));
137 printf(" %02x", (*ucp++ = *((u_char *)addr)));
143 for (i=0; i<linebytes; i++) {
144 if ((*cp < 0x20) || (*cp > 0x7e))
156 } while (nbytes > 0);
159 dp_last_length = length;
164 int do_mem_mm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
166 return mod_mem (cmdtp, 1, flag, argc, argv);
168 int do_mem_nm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
170 return mod_mem (cmdtp, 0, flag, argc, argv);
173 int do_mem_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
175 ulong addr, size, writeval, count;
177 if ((argc < 3) || (argc > 4)) {
178 printf ("Usage:\n%s\n", cmdtp->usage);
182 /* Check for size specification.
184 size = cmd_get_data_size(argv[0], 4);
186 /* Address is specified since argc > 1
188 addr = simple_strtoul(argv[1], NULL, 16);
189 addr += base_address;
191 /* Get the value to write.
193 writeval = simple_strtoul(argv[2], NULL, 16);
197 count = simple_strtoul(argv[3], NULL, 16);
202 while (count-- > 0) {
204 *((ulong *)addr) = (ulong )writeval;
206 *((ushort *)addr) = (ushort)writeval;
208 *((u_char *)addr) = (u_char)writeval;
214 int do_mem_cmp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
216 ulong size, addr1, addr2, count, ngood;
220 printf ("Usage:\n%s\n", cmdtp->usage);
224 /* Check for size specification.
226 size = cmd_get_data_size(argv[0], 4);
228 addr1 = simple_strtoul(argv[1], NULL, 16);
229 addr1 += base_address;
231 addr2 = simple_strtoul(argv[2], NULL, 16);
232 addr2 += base_address;
234 count = simple_strtoul(argv[3], NULL, 16);
238 while (count-- > 0) {
240 ulong word1 = *(ulong *)addr1;
241 ulong word2 = *(ulong *)addr2;
242 if (word1 != word2) {
243 printf("word at 0x%08lx (0x%08lx) "
244 "!= word at 0x%08lx (0x%08lx)\n",
245 addr1, word1, addr2, word2);
250 else if (size == 2) {
251 ushort hword1 = *(ushort *)addr1;
252 ushort hword2 = *(ushort *)addr2;
253 if (hword1 != hword2) {
254 printf("halfword at 0x%08lx (0x%04x) "
255 "!= halfword at 0x%08lx (0x%04x)\n",
256 addr1, hword1, addr2, hword2);
262 u_char byte1 = *(u_char *)addr1;
263 u_char byte2 = *(u_char *)addr2;
264 if (byte1 != byte2) {
265 printf("byte at 0x%08lx (0x%02x) "
266 "!= byte at 0x%08lx (0x%02x)\n",
267 addr1, byte1, addr2, byte2);
277 printf("Total of %ld %s%s were the same\n",
278 ngood, size == 4 ? "word" : size == 2 ? "halfword" : "byte",
279 ngood == 1 ? "" : "s");
283 int do_mem_cp ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
285 ulong addr, size, dest, count;
288 printf ("Usage:\n%s\n", cmdtp->usage);
292 /* Check for size specification.
294 size = cmd_get_data_size(argv[0], 4);
296 addr = simple_strtoul(argv[1], NULL, 16);
297 addr += base_address;
299 dest = simple_strtoul(argv[2], NULL, 16);
300 dest += base_address;
302 count = simple_strtoul(argv[3], NULL, 16);
305 puts ("Zero length ???\n");
310 /* check if we are copying to Flash */
311 if (addr2info(dest) != NULL) {
314 printf ("Copy to Flash... ");
316 rc = flash_write ((uchar *)addr, dest, count*size);
326 while (count-- > 0) {
328 *((ulong *)dest) = *((ulong *)addr);
330 *((ushort *)dest) = *((ushort *)addr);
332 *((u_char *)dest) = *((u_char *)addr);
339 int do_mem_base (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
342 /* Set new base address.
344 base_address = simple_strtoul(argv[1], NULL, 16);
346 /* Print the current base address.
348 printf("Base Address: 0x%08lx\n", base_address);
352 int do_mem_loop (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
354 ulong addr, size, length, i, junk;
355 volatile uint *longp;
356 volatile ushort *shortp;
360 printf ("Usage:\n%s\n", cmdtp->usage);
364 /* Check for a size spefication.
365 * Defaults to long if no or incorrect specification.
367 size = cmd_get_data_size(argv[0], 4);
369 /* Address is always specified.
371 addr = simple_strtoul(argv[1], NULL, 16);
373 /* Length is the number of objects, not number of bytes.
375 length = simple_strtoul(argv[2], NULL, 16);
377 /* We want to optimize the loops to run as fast as possible.
378 * If we have only one object, just run infinite loops.
382 longp = (uint *)addr;
387 shortp = (ushort *)addr;
398 longp = (uint *)addr;
406 shortp = (ushort *)addr;
421 * Perform a memory test. A more complete alternative test can be
422 * configured using CFG_ALT_MEMTEST. The complete test loops until
423 * interrupted by ctrl-c or by a failure of one of the sub-tests.
425 int do_mem_mtest (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
427 vu_long *addr, *start, *end;
431 #if defined(CFG_ALT_MEMTEST)
437 vu_long anti_pattern;
439 vu_long *dummy = NULL;
443 static const ulong bitpattern[] = {
444 0x00000001, /* single bit */
445 0x00000003, /* two adjacent bits */
446 0x00000007, /* three adjacent bits */
447 0x0000000F, /* four adjacent bits */
448 0x00000005, /* two non-adjacent bits */
449 0x00000015, /* three non-adjacent bits */
450 0x00000055, /* four non-adjacent bits */
451 0xaaaaaaaa, /* alternating 1/0 */
460 start = (ulong *)simple_strtoul(argv[1], NULL, 16);
462 start = (ulong *)CFG_MEMTEST_START;
466 end = (ulong *)simple_strtoul(argv[2], NULL, 16);
468 end = (ulong *)(CFG_MEMTEST_END);
472 pattern = (ulong)simple_strtoul(argv[3], NULL, 16);
477 #if defined(CFG_ALT_MEMTEST)
478 printf ("Testing %08x ... %08x:\n", (uint)start, (uint)end);
479 PRINTF("%s:%d: start 0x%p end 0x%p\n",
480 __FUNCTION__, __LINE__, start, end);
488 printf("Iteration: %6d\r", iterations);
489 PRINTF("Iteration: %6d\n", iterations);
493 * Data line test: write a pattern to the first
494 * location, write the 1's complement to a 'parking'
495 * address (changes the state of the data bus so a
496 * floating bus doen't give a false OK), and then
497 * read the value back. Note that we read it back
498 * into a variable because the next time we read it,
499 * it might be right (been there, tough to explain to
500 * the quality guys why it prints a failure when the
501 * "is" and "should be" are obviously the same in the
504 * Rather than exhaustively testing, we test some
505 * patterns by shifting '1' bits through a field of
506 * '0's and '0' bits through a field of '1's (i.e.
507 * pattern and ~pattern).
510 for (j = 0; j < sizeof(bitpattern)/sizeof(bitpattern[0]); j++) {
512 for(; val != 0; val <<= 1) {
514 *dummy = ~val; /* clear the test data off of the bus */
516 if(readback != val) {
517 printf ("FAILURE (data line): "
518 "expected %08lx, actual %08lx\n",
524 if(readback != ~val) {
525 printf ("FAILURE (data line): "
526 "Is %08lx, should be %08lx\n",
533 * Based on code whose Original Author and Copyright
534 * information follows: Copyright (c) 1998 by Michael
535 * Barr. This software is placed into the public
536 * domain and may be used for any purpose. However,
537 * this notice must not be changed or removed and no
538 * warranty is either expressed or implied by its
539 * publication or distribution.
545 * Description: Test the address bus wiring in a
546 * memory region by performing a walking
547 * 1's test on the relevant bits of the
548 * address and checking for aliasing.
549 * This test will find single-bit
550 * address failures such as stuck -high,
551 * stuck-low, and shorted pins. The base
552 * address and size of the region are
553 * selected by the caller.
555 * Notes: For best results, the selected base
556 * address should have enough LSB 0's to
557 * guarantee single address bit changes.
558 * For example, to test a 64-Kbyte
559 * region, select a base address on a
560 * 64-Kbyte boundary. Also, select the
561 * region size as a power-of-two if at
564 * Returns: 0 if the test succeeds, 1 if the test fails.
566 * ## NOTE ## Be sure to specify start and end
567 * addresses such that addr_mask has
568 * lots of bits set. For example an
569 * address range of 01000000 02000000 is
570 * bad while a range of 01000000
571 * 01ffffff is perfect.
573 addr_mask = ((ulong)end - (ulong)start)/sizeof(vu_long);
574 pattern = (vu_long) 0xaaaaaaaa;
575 anti_pattern = (vu_long) 0x55555555;
577 PRINTF("%s:%d: addr mask = 0x%.8lx\n",
578 __FUNCTION__, __LINE__,
581 * Write the default pattern at each of the
582 * power-of-two offsets.
584 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
585 start[offset] = pattern;
589 * Check for address bits stuck high.
592 start[test_offset] = anti_pattern;
594 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
595 temp = start[offset];
596 if (temp != pattern) {
597 printf ("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
598 " expected 0x%.8lx, actual 0x%.8lx\n",
599 (ulong)&start[offset], pattern, temp);
603 start[test_offset] = pattern;
606 * Check for addr bits stuck low or shorted.
608 for (test_offset = 1; (test_offset & addr_mask) != 0; test_offset <<= 1) {
609 start[test_offset] = anti_pattern;
611 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
612 temp = start[offset];
613 if ((temp != pattern) && (offset != test_offset)) {
614 printf ("\nFAILURE: Address bit stuck low or shorted @"
615 " 0x%.8lx: expected 0x%.8lx, actual 0x%.8lx\n",
616 (ulong)&start[offset], pattern, temp);
620 start[test_offset] = pattern;
624 * Description: Test the integrity of a physical
625 * memory device by performing an
626 * increment/decrement test over the
627 * entire region. In the process every
628 * storage bit in the device is tested
629 * as a zero and a one. The base address
630 * and the size of the region are
631 * selected by the caller.
633 * Returns: 0 if the test succeeds, 1 if the test fails.
635 num_words = ((ulong)end - (ulong)start)/sizeof(vu_long) + 1;
638 * Fill memory with a known pattern.
640 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
641 start[offset] = pattern;
645 * Check each location and invert it for the second pass.
647 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
648 temp = start[offset];
649 if (temp != pattern) {
650 printf ("\nFAILURE (read/write) @ 0x%.8lx:"
651 " expected 0x%.8lx, actual 0x%.8lx)\n",
652 (ulong)&start[offset], pattern, temp);
656 anti_pattern = ~pattern;
657 start[offset] = anti_pattern;
661 * Check each location for the inverted pattern and zero it.
663 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
664 anti_pattern = ~pattern;
665 temp = start[offset];
666 if (temp != anti_pattern) {
667 printf ("\nFAILURE (read/write): @ 0x%.8lx:"
668 " expected 0x%.8lx, actual 0x%.8lx)\n",
669 (ulong)&start[offset], anti_pattern, temp);
676 #else /* The original, quickie test */
684 printf ("\rPattern %08lX Writing..."
686 "\b\b\b\b\b\b\b\b\b\b",
689 for (addr=start,val=pattern; addr<end; addr++) {
694 printf("Reading...");
696 for (addr=start,val=pattern; addr<end; addr++) {
698 if (readback != val) {
699 printf ("\nMem error @ 0x%08X: "
700 "found %08lX, expected %08lX\n",
701 (uint)addr, readback, val);
708 * Flip the pattern each time to make lots of zeros and
709 * then, the next time, lots of ones. We decrement
710 * the "negative" patterns and increment the "positive"
711 * patterns to preserve this feature.
713 if(pattern & 0x80000000) {
714 pattern = -pattern; /* complement & increment */
729 * mm{.b, .w, .l} {addr}
730 * nm{.b, .w, .l} {addr}
733 mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[])
737 extern char console_buffer[];
740 printf ("Usage:\n%s\n", cmdtp->usage);
744 #ifdef CONFIG_BOOT_RETRY_TIME
745 reset_cmd_timeout(); /* got a good command to get here */
747 /* We use the last specified parameters, unless new ones are
753 if ((flag & CMD_FLAG_REPEAT) == 0) {
754 /* New command specified. Check for a size specification.
755 * Defaults to long if no or incorrect specification.
757 size = cmd_get_data_size(argv[0], 4);
759 /* Address is specified since argc > 1
761 addr = simple_strtoul(argv[1], NULL, 16);
762 addr += base_address;
765 /* Print the address, followed by value. Then accept input for
766 * the next value. A non-converted value exits.
769 printf("%08lx:", addr);
771 printf(" %08x", *((uint *)addr));
773 printf(" %04x", *((ushort *)addr));
775 printf(" %02x", *((u_char *)addr));
777 nbytes = readline (" ? ");
778 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
779 /* <CR> pressed as only input, don't modify current
780 * location and move to next. "-" pressed will go back.
783 addr += nbytes ? -size : size;
785 #ifdef CONFIG_BOOT_RETRY_TIME
786 reset_cmd_timeout(); /* good enough to not time out */
789 #ifdef CONFIG_BOOT_RETRY_TIME
790 else if (nbytes == -2) {
791 break; /* timed out, exit the command */
796 i = simple_strtoul(console_buffer, &endp, 16);
797 nbytes = endp - console_buffer;
799 #ifdef CONFIG_BOOT_RETRY_TIME
800 /* good enough to not time out
807 *((ushort *)addr) = i;
809 *((u_char *)addr) = i;
821 int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
828 printf ("Usage:\n%s\n", cmdtp->usage);
832 addr = simple_strtoul(argv[1], NULL, 16);
833 addr += base_address;
835 length = simple_strtoul(argv[2], NULL, 16);
837 crc = crc32 (0, (const uchar *)addr, length);
839 printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
840 addr, addr + length -1, crc);
844 ptr = (ulong *)simple_strtoul(argv[3], NULL, 16);
851 #endif /* CFG_CMD_MEMORY */