]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/cmd_mem.c
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
;
140 printf("%08lx:", addr
);
141 linebytes
= (nbytes
>DISP_LINE_LEN
)?DISP_LINE_LEN
:nbytes
;
143 #ifdef CONFIG_HAS_DATAFLASH
144 if (read_dataflash(addr
, (linebytes
/size
)*size
, linebuf
) != -1){
146 for (i
=0; i
<linebytes
; i
+= size
) {
148 printf(" %08x", *uip
++);
149 } else if (size
== 2) {
150 printf(" %04x", *usp
++);
152 printf(" %02x", *ucp
++);
157 } else { /* addr does not correspond to DataFlash */
159 for (i
=0; i
<linebytes
; i
+= size
) {
161 printf(" %08x", (*uip
++ = *((uint
*)addr
)));
162 } else if (size
== 2) {
163 printf(" %04x", (*usp
++ = *((ushort
*)addr
)));
165 printf(" %02x", (*ucp
++ = *((u_char
*)addr
)));
169 #ifdef CONFIG_HAS_DATAFLASH
174 for (i
=0; i
<linebytes
; i
++) {
175 if ((*cp
< 0x20) || (*cp
> 0x7e))
187 } while (nbytes
> 0);
190 dp_last_length
= length
;
195 int do_mem_mm ( cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
197 return mod_mem (cmdtp
, 1, flag
, argc
, argv
);
199 int do_mem_nm ( cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
201 return mod_mem (cmdtp
, 0, flag
, argc
, argv
);
204 int do_mem_mw ( cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
206 ulong addr
, writeval
, count
;
209 if ((argc
< 3) || (argc
> 4)) {
210 printf ("Usage:\n%s\n", cmdtp
->usage
);
214 /* Check for size specification.
216 if ((size
= cmd_get_data_size(argv
[0], 4)) < 1)
219 /* Address is specified since argc > 1
221 addr
= simple_strtoul(argv
[1], NULL
, 16);
222 addr
+= base_address
;
224 /* Get the value to write.
226 writeval
= simple_strtoul(argv
[2], NULL
, 16);
230 count
= simple_strtoul(argv
[3], NULL
, 16);
235 while (count
-- > 0) {
237 *((ulong
*)addr
) = (ulong
)writeval
;
239 *((ushort
*)addr
) = (ushort
)writeval
;
241 *((u_char
*)addr
) = (u_char
)writeval
;
247 int do_mem_cmp (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
249 ulong addr1
, addr2
, count
, ngood
;
254 printf ("Usage:\n%s\n", cmdtp
->usage
);
258 /* Check for size specification.
260 if ((size
= cmd_get_data_size(argv
[0], 4)) < 0)
263 addr1
= simple_strtoul(argv
[1], NULL
, 16);
264 addr1
+= base_address
;
266 addr2
= simple_strtoul(argv
[2], NULL
, 16);
267 addr2
+= base_address
;
269 count
= simple_strtoul(argv
[3], NULL
, 16);
271 #ifdef CONFIG_HAS_DATAFLASH
272 if (addr_dataflash(addr1
) | addr_dataflash(addr2
)){
273 printf("Comparison with DataFlash space not supported.\n\r");
280 while (count
-- > 0) {
282 ulong word1
= *(ulong
*)addr1
;
283 ulong word2
= *(ulong
*)addr2
;
284 if (word1
!= word2
) {
285 printf("word at 0x%08lx (0x%08lx) "
286 "!= word at 0x%08lx (0x%08lx)\n",
287 addr1
, word1
, addr2
, word2
);
292 else if (size
== 2) {
293 ushort hword1
= *(ushort
*)addr1
;
294 ushort hword2
= *(ushort
*)addr2
;
295 if (hword1
!= hword2
) {
296 printf("halfword at 0x%08lx (0x%04x) "
297 "!= halfword at 0x%08lx (0x%04x)\n",
298 addr1
, hword1
, addr2
, hword2
);
304 u_char byte1
= *(u_char
*)addr1
;
305 u_char byte2
= *(u_char
*)addr2
;
306 if (byte1
!= byte2
) {
307 printf("byte at 0x%08lx (0x%02x) "
308 "!= byte at 0x%08lx (0x%02x)\n",
309 addr1
, byte1
, addr2
, byte2
);
319 printf("Total of %ld %s%s were the same\n",
320 ngood
, size
== 4 ? "word" : size
== 2 ? "halfword" : "byte",
321 ngood
== 1 ? "" : "s");
325 int do_mem_cp ( cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
327 ulong addr
, dest
, count
;
331 printf ("Usage:\n%s\n", cmdtp
->usage
);
335 /* Check for size specification.
337 if ((size
= cmd_get_data_size(argv
[0], 4)) < 0)
340 addr
= simple_strtoul(argv
[1], NULL
, 16);
341 addr
+= base_address
;
343 dest
= simple_strtoul(argv
[2], NULL
, 16);
344 dest
+= base_address
;
346 count
= simple_strtoul(argv
[3], NULL
, 16);
349 puts ("Zero length ???\n");
354 /* check if we are copying to Flash */
355 if ( (addr2info(dest
) != NULL
)
356 #ifdef CONFIG_HAS_DATAFLASH
357 && (!addr_dataflash(addr
))
362 printf ("Copy to Flash... ");
364 rc
= flash_write ((uchar
*)addr
, dest
, count
*size
);
374 #if (CONFIG_COMMANDS & CFG_CMD_MMC)
375 if (mmc2info(dest
)) {
378 printf ("Copy to MMC... ");
379 switch (rc
= mmc_write ((uchar
*)addr
, dest
, count
*size
)) {
387 printf ("%s[%d] FIXME: rc=%d\n",__FILE__
,__LINE__
,rc
);
394 if (mmc2info(addr
)) {
397 printf ("Copy from MMC... ");
398 switch (rc
= mmc_read (addr
, (uchar
*)dest
, count
*size
)) {
406 printf ("%s[%d] FIXME: rc=%d\n",__FILE__
,__LINE__
,rc
);
414 #ifdef CONFIG_HAS_DATAFLASH
415 /* Check if we are copying from RAM or Flash to DataFlash */
416 if (addr_dataflash(dest
) && !addr_dataflash(addr
)){
419 printf ("Copy to DataFlash... ");
421 rc
= write_dataflash (dest
, addr
, count
*size
);
424 dataflash_perror (rc
);
431 /* Check if we are copying from DataFlash to RAM */
432 if (addr_dataflash(addr
) && !addr_dataflash(dest
) && (addr2info(dest
)==NULL
) ){
433 read_dataflash(addr
, count
* size
, (char *) dest
);
437 if (addr_dataflash(addr
) && addr_dataflash(dest
)){
438 printf("Unsupported combination of source/destination.\n\r");
443 while (count
-- > 0) {
445 *((ulong
*)dest
) = *((ulong
*)addr
);
447 *((ushort
*)dest
) = *((ushort
*)addr
);
449 *((u_char
*)dest
) = *((u_char
*)addr
);
456 int do_mem_base (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
459 /* Set new base address.
461 base_address
= simple_strtoul(argv
[1], NULL
, 16);
463 /* Print the current base address.
465 printf("Base Address: 0x%08lx\n", base_address
);
469 int do_mem_loop (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
471 ulong addr
, length
, i
, junk
;
473 volatile uint
*longp
;
474 volatile ushort
*shortp
;
478 printf ("Usage:\n%s\n", cmdtp
->usage
);
482 /* Check for a size spefication.
483 * Defaults to long if no or incorrect specification.
485 if ((size
= cmd_get_data_size(argv
[0], 4)) < 0)
488 /* Address is always specified.
490 addr
= simple_strtoul(argv
[1], NULL
, 16);
492 /* Length is the number of objects, not number of bytes.
494 length
= simple_strtoul(argv
[2], NULL
, 16);
496 /* We want to optimize the loops to run as fast as possible.
497 * If we have only one object, just run infinite loops.
501 longp
= (uint
*)addr
;
506 shortp
= (ushort
*)addr
;
517 longp
= (uint
*)addr
;
525 shortp
= (ushort
*)addr
;
540 * Perform a memory test. A more complete alternative test can be
541 * configured using CFG_ALT_MEMTEST. The complete test loops until
542 * interrupted by ctrl-c or by a failure of one of the sub-tests.
544 int do_mem_mtest (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
546 vu_long
*addr
, *start
, *end
;
550 #if defined(CFG_ALT_MEMTEST)
556 vu_long anti_pattern
;
558 vu_long
*dummy
= NULL
;
562 static const ulong bitpattern
[] = {
563 0x00000001, /* single bit */
564 0x00000003, /* two adjacent bits */
565 0x00000007, /* three adjacent bits */
566 0x0000000F, /* four adjacent bits */
567 0x00000005, /* two non-adjacent bits */
568 0x00000015, /* three non-adjacent bits */
569 0x00000055, /* four non-adjacent bits */
570 0xaaaaaaaa, /* alternating 1/0 */
579 start
= (ulong
*)simple_strtoul(argv
[1], NULL
, 16);
581 start
= (ulong
*)CFG_MEMTEST_START
;
585 end
= (ulong
*)simple_strtoul(argv
[2], NULL
, 16);
587 end
= (ulong
*)(CFG_MEMTEST_END
);
591 pattern
= (ulong
)simple_strtoul(argv
[3], NULL
, 16);
596 #if defined(CFG_ALT_MEMTEST)
597 printf ("Testing %08x ... %08x:\n", (uint
)start
, (uint
)end
);
598 PRINTF("%s:%d: start 0x%p end 0x%p\n",
599 __FUNCTION__
, __LINE__
, start
, end
);
607 printf("Iteration: %6d\r", iterations
);
608 PRINTF("Iteration: %6d\n", iterations
);
612 * Data line test: write a pattern to the first
613 * location, write the 1's complement to a 'parking'
614 * address (changes the state of the data bus so a
615 * floating bus doen't give a false OK), and then
616 * read the value back. Note that we read it back
617 * into a variable because the next time we read it,
618 * it might be right (been there, tough to explain to
619 * the quality guys why it prints a failure when the
620 * "is" and "should be" are obviously the same in the
623 * Rather than exhaustively testing, we test some
624 * patterns by shifting '1' bits through a field of
625 * '0's and '0' bits through a field of '1's (i.e.
626 * pattern and ~pattern).
629 for (j
= 0; j
< sizeof(bitpattern
)/sizeof(bitpattern
[0]); j
++) {
631 for(; val
!= 0; val
<<= 1) {
633 *dummy
= ~val
; /* clear the test data off of the bus */
635 if(readback
!= val
) {
636 printf ("FAILURE (data line): "
637 "expected %08lx, actual %08lx\n",
643 if(readback
!= ~val
) {
644 printf ("FAILURE (data line): "
645 "Is %08lx, should be %08lx\n",
652 * Based on code whose Original Author and Copyright
653 * information follows: Copyright (c) 1998 by Michael
654 * Barr. This software is placed into the public
655 * domain and may be used for any purpose. However,
656 * this notice must not be changed or removed and no
657 * warranty is either expressed or implied by its
658 * publication or distribution.
664 * Description: Test the address bus wiring in a
665 * memory region by performing a walking
666 * 1's test on the relevant bits of the
667 * address and checking for aliasing.
668 * This test will find single-bit
669 * address failures such as stuck -high,
670 * stuck-low, and shorted pins. The base
671 * address and size of the region are
672 * selected by the caller.
674 * Notes: For best results, the selected base
675 * address should have enough LSB 0's to
676 * guarantee single address bit changes.
677 * For example, to test a 64-Kbyte
678 * region, select a base address on a
679 * 64-Kbyte boundary. Also, select the
680 * region size as a power-of-two if at
683 * Returns: 0 if the test succeeds, 1 if the test fails.
685 * ## NOTE ## Be sure to specify start and end
686 * addresses such that addr_mask has
687 * lots of bits set. For example an
688 * address range of 01000000 02000000 is
689 * bad while a range of 01000000
690 * 01ffffff is perfect.
692 addr_mask
= ((ulong
)end
- (ulong
)start
)/sizeof(vu_long
);
693 pattern
= (vu_long
) 0xaaaaaaaa;
694 anti_pattern
= (vu_long
) 0x55555555;
696 PRINTF("%s:%d: addr mask = 0x%.8lx\n",
697 __FUNCTION__
, __LINE__
,
700 * Write the default pattern at each of the
701 * power-of-two offsets.
703 for (offset
= 1; (offset
& addr_mask
) != 0; offset
<<= 1) {
704 start
[offset
] = pattern
;
708 * Check for address bits stuck high.
711 start
[test_offset
] = anti_pattern
;
713 for (offset
= 1; (offset
& addr_mask
) != 0; offset
<<= 1) {
714 temp
= start
[offset
];
715 if (temp
!= pattern
) {
716 printf ("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
717 " expected 0x%.8lx, actual 0x%.8lx\n",
718 (ulong
)&start
[offset
], pattern
, temp
);
722 start
[test_offset
] = pattern
;
725 * Check for addr bits stuck low or shorted.
727 for (test_offset
= 1; (test_offset
& addr_mask
) != 0; test_offset
<<= 1) {
728 start
[test_offset
] = anti_pattern
;
730 for (offset
= 1; (offset
& addr_mask
) != 0; offset
<<= 1) {
731 temp
= start
[offset
];
732 if ((temp
!= pattern
) && (offset
!= test_offset
)) {
733 printf ("\nFAILURE: Address bit stuck low or shorted @"
734 " 0x%.8lx: expected 0x%.8lx, actual 0x%.8lx\n",
735 (ulong
)&start
[offset
], pattern
, temp
);
739 start
[test_offset
] = pattern
;
743 * Description: Test the integrity of a physical
744 * memory device by performing an
745 * increment/decrement test over the
746 * entire region. In the process every
747 * storage bit in the device is tested
748 * as a zero and a one. The base address
749 * and the size of the region are
750 * selected by the caller.
752 * Returns: 0 if the test succeeds, 1 if the test fails.
754 num_words
= ((ulong
)end
- (ulong
)start
)/sizeof(vu_long
) + 1;
757 * Fill memory with a known pattern.
759 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, offset
++) {
760 start
[offset
] = pattern
;
764 * Check each location and invert it for the second pass.
766 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, offset
++) {
767 temp
= start
[offset
];
768 if (temp
!= pattern
) {
769 printf ("\nFAILURE (read/write) @ 0x%.8lx:"
770 " expected 0x%.8lx, actual 0x%.8lx)\n",
771 (ulong
)&start
[offset
], pattern
, temp
);
775 anti_pattern
= ~pattern
;
776 start
[offset
] = anti_pattern
;
780 * Check each location for the inverted pattern and zero it.
782 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, offset
++) {
783 anti_pattern
= ~pattern
;
784 temp
= start
[offset
];
785 if (temp
!= anti_pattern
) {
786 printf ("\nFAILURE (read/write): @ 0x%.8lx:"
787 " expected 0x%.8lx, actual 0x%.8lx)\n",
788 (ulong
)&start
[offset
], anti_pattern
, temp
);
795 #else /* The original, quickie test */
803 printf ("\rPattern %08lX Writing..."
805 "\b\b\b\b\b\b\b\b\b\b",
808 for (addr
=start
,val
=pattern
; addr
<end
; addr
++) {
813 printf("Reading...");
815 for (addr
=start
,val
=pattern
; addr
<end
; addr
++) {
817 if (readback
!= val
) {
818 printf ("\nMem error @ 0x%08X: "
819 "found %08lX, expected %08lX\n",
820 (uint
)addr
, readback
, val
);
827 * Flip the pattern each time to make lots of zeros and
828 * then, the next time, lots of ones. We decrement
829 * the "negative" patterns and increment the "positive"
830 * patterns to preserve this feature.
832 if(pattern
& 0x80000000) {
833 pattern
= -pattern
; /* complement & increment */
848 * mm{.b, .w, .l} {addr}
849 * nm{.b, .w, .l} {addr}
852 mod_mem(cmd_tbl_t
*cmdtp
, int incrflag
, int flag
, int argc
, char *argv
[])
856 extern char console_buffer
[];
859 printf ("Usage:\n%s\n", cmdtp
->usage
);
863 #ifdef CONFIG_BOOT_RETRY_TIME
864 reset_cmd_timeout(); /* got a good command to get here */
866 /* We use the last specified parameters, unless new ones are
872 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
873 /* New command specified. Check for a size specification.
874 * Defaults to long if no or incorrect specification.
876 if ((size
= cmd_get_data_size(argv
[0], 4)) < 0)
879 /* Address is specified since argc > 1
881 addr
= simple_strtoul(argv
[1], NULL
, 16);
882 addr
+= base_address
;
885 #ifdef CONFIG_HAS_DATAFLASH
886 if (addr_dataflash(addr
)){
887 printf("Can't modify DataFlash in place. Use cp instead.\n\r");
892 /* Print the address, followed by value. Then accept input for
893 * the next value. A non-converted value exits.
896 printf("%08lx:", addr
);
898 printf(" %08x", *((uint
*)addr
));
900 printf(" %04x", *((ushort
*)addr
));
902 printf(" %02x", *((u_char
*)addr
));
904 nbytes
= readline (" ? ");
905 if (nbytes
== 0 || (nbytes
== 1 && console_buffer
[0] == '-')) {
906 /* <CR> pressed as only input, don't modify current
907 * location and move to next. "-" pressed will go back.
910 addr
+= nbytes
? -size
: size
;
912 #ifdef CONFIG_BOOT_RETRY_TIME
913 reset_cmd_timeout(); /* good enough to not time out */
916 #ifdef CONFIG_BOOT_RETRY_TIME
917 else if (nbytes
== -2) {
918 break; /* timed out, exit the command */
923 i
= simple_strtoul(console_buffer
, &endp
, 16);
924 nbytes
= endp
- console_buffer
;
926 #ifdef CONFIG_BOOT_RETRY_TIME
927 /* good enough to not time out
934 *((ushort
*)addr
) = i
;
936 *((u_char
*)addr
) = i
;
948 int do_mem_crc (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
955 printf ("Usage:\n%s\n", cmdtp
->usage
);
959 addr
= simple_strtoul (argv
[1], NULL
, 16);
960 addr
+= base_address
;
962 length
= simple_strtoul (argv
[2], NULL
, 16);
964 crc
= crc32 (0, (const uchar
*) addr
, length
);
966 printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
967 addr
, addr
+ length
- 1, crc
);
970 ptr
= (ulong
*) simple_strtoul (argv
[3], NULL
, 16);
977 /**************************************************/
978 #if (CONFIG_COMMANDS & CFG_CMD_MEMORY)
981 "md - memory display\n",
982 "[.b, .w, .l] address [# of objects]\n - memory display\n"
988 "mm - memory modify (auto-incrementing)\n",
989 "[.b, .w, .l] address\n" " - memory modify, auto increment address\n"
995 "nm - memory modify (constant address)\n",
996 "[.b, .w, .l] address\n - memory modify, read and keep address\n"
1000 mw
, 4, 1, do_mem_mw
,
1001 "mw - memory write (fill)\n",
1002 "[.b, .w, .l] address value [count]\n - write memory\n"
1006 cp
, 4, 1, do_mem_cp
,
1007 "cp - memory copy\n",
1008 "[.b, .w, .l] source target count\n - copy memory\n"
1012 cmp
, 4, 1, do_mem_cmp
,
1013 "cmp - memory compare\n",
1014 "[.b, .w, .l] addr1 addr2 count\n - compare memory\n"
1018 crc32
, 4, 1, do_mem_crc
,
1019 "crc32 - checksum calculation\n",
1020 "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
1024 base
, 2, 1, do_mem_base
,
1025 "base - print or set address offset\n",
1026 "\n - print address offset for memory commands\n"
1027 "base off\n - set address offset for memory commands to 'off'\n"
1031 loop
, 3, 1, do_mem_loop
,
1032 "loop - infinite loop on address range\n",
1033 "[.b, .w, .l] address number_of_objects\n"
1034 " - loop on a set of addresses\n"
1038 mtest
, 4, 1, do_mem_mtest
,
1039 "mtest - simple RAM test\n",
1040 "[start [end [pattern]]]\n"
1041 " - simple RAM read/write test\n"
1045 #endif /* CFG_CMD_MEMORY */