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)
36 #if (CONFIG_COMMANDS & (CFG_CMD_MEMORY | \
41 * Check for a size specification .b, .w or .l.
43 int cmd_get_data_size(char* arg, int default_size){
44 int len = strlen(arg);
46 if(len > 2 && arg[len - 2] == '.'){
65 #if (CONFIG_COMMANDS & CFG_CMD_MEMORY)
67 static int mod_mem(cmd_tbl_t *, int, int, int, char *[]);
70 * Display values from last command.
71 * Memory modify remembered values are different from display memory.
73 uint dp_last_addr, dp_last_size;
74 uint dp_last_length = 0x40;
75 uint mm_last_addr, mm_last_size;
81 * md{.b, .w, .l} {addr} {len}
83 #define DISP_LINE_LEN 16
84 int do_mem_md(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){
86 ulong i, nbytes, linebytes;
92 * We use the last specified parameters, unless new ones are
97 length = dp_last_length;
101 if(cmdtp->help != NULL){
102 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->help);
104 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->usage);
107 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->usage);
112 if((flag & CMD_FLAG_REPEAT) == 0){
114 * New command specified. Check for a size specification.
115 * Defaults to long if no or incorrect specification.
117 if((size = cmd_get_data_size(argv[0], 4)) < 0){
122 * Address is specified since argc > 1
124 addr = simple_strtoul(argv[1], NULL, 16);
127 * If another parameter, it is the length to display.
128 * Length is the number of objects, not number of bytes.
131 length = simple_strtoul(argv[2], NULL, 16);
138 * We buffer all read data, so we can make sure data is read only
139 * once, and all accesses are with the specified bus width.
141 nbytes = length * size;
144 char linebuf[DISP_LINE_LEN];
145 uint *uip = (uint *)linebuf;
146 ushort *usp = (ushort *)linebuf;
147 u_char *ucp = (u_char *)linebuf;
149 printf("%08lX:", addr);
151 linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
153 for(i = 0; i < linebytes; i += size){
155 printf(" %08X", (*uip++ = *((uint *)addr)));
156 } else if(size == 2){
157 printf(" %04X", (*usp++ = *((ushort *)addr)));
159 printf(" %02X", (*ucp++ = *((u_char *)addr)));
165 cp = (u_char *)linebuf;
167 for(i = 0; i < linebytes; i++){
168 if((*cp < 0x20) || (*cp > 0x7e)){
187 dp_last_length = length;
193 int do_mem_mm(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){
194 return(mod_mem(cmdtp, 1, flag, argc, argv));
196 int do_mem_nm(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){
197 return(mod_mem(cmdtp, 0, flag, argc, argv));
200 int do_mem_mw(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){
201 ulong addr, writeval, count;
204 if((argc < 3) || (argc > 4)){
206 if(cmdtp->help != NULL){
207 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->help);
209 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->usage);
212 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->usage);
218 * Check for size specification.
220 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);
230 * Get the value to write.
232 writeval = simple_strtoul(argv[2], NULL, 16);
236 count = simple_strtoul(argv[3], NULL, 16);
243 *((ulong *)addr) = (ulong)writeval;
244 } else if(size == 2){
245 *((ushort *)addr) = (ushort)writeval;
247 *((u_char *)addr) = (u_char)writeval;
255 int do_mem_cp(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){
256 ulong addr, dest, count;
261 if(cmdtp->help != NULL){
262 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->help);
264 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->usage);
267 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->usage);
273 * Check for size specification.
275 if((size = cmd_get_data_size(argv[0], 4)) < 0){
279 addr = simple_strtoul(argv[1], NULL, 16);
280 dest = simple_strtoul(argv[2], NULL, 16);
281 count = simple_strtoul(argv[3], NULL, 16);
284 puts("## Error: zero length?\n");
289 /* check if we are copying to Flash */
290 if(addr2info(dest) != NULL){
293 puts("Copying to flash...\n");
295 rc = flash_write((char *)addr, dest, count * size);
309 *((ulong *)dest) = *((ulong *)addr);
310 } else if(size == 2){
311 *((ushort *)dest) = *((ushort *)addr);
313 *((u_char *)dest) = *((u_char *)addr);
323 * Perform a memory test. A more complete alternative test can be
324 * configured using CFG_ALT_MEMTEST. The complete test loops until
325 * interrupted by ctrl-c or by a failure of one of the sub-tests.
327 int do_mem_mtest(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){
328 vu_long *addr, *start, *end;
332 #if defined(CFG_ALT_MEMTEST)
338 vu_long anti_pattern;
340 #if defined(CFG_MEMTEST_SCRATCH)
341 vu_long *dummy = (vu_long*)CFG_MEMTEST_SCRATCH;
343 vu_long *dummy = NULL;
348 static const ulong bitpattern[] = {
349 0x00000001, /* single bit */
350 0x00000003, /* two adjacent bits */
351 0x00000007, /* three adjacent bits */
352 0x0000000F, /* four adjacent bits */
353 0x00000005, /* two non-adjacent bits */
354 0x00000015, /* three non-adjacent bits */
355 0x00000055, /* four non-adjacent bits */
356 0xaaaaaaaa, /* alternating 1/0 */
365 start = (ulong *)simple_strtoul(argv[1], NULL, 16);
367 start = (ulong *)CFG_MEMTEST_START;
371 end = (ulong *)simple_strtoul(argv[2], NULL, 16);
373 end = (ulong *)(CFG_MEMTEST_END);
377 pattern = (ulong)simple_strtoul(argv[3], NULL, 16);
382 #if defined(CFG_ALT_MEMTEST)
383 printf("Testing RAM 0x%08X...0x%08X:\n", (uint)start, (uint)end);
391 printf("Iteration: %6d\r", iterations);
396 * Data line test: write a pattern to the first
397 * location, write the 1's complement to a 'parking'
398 * address (changes the state of the data bus so a
399 * floating bus doen't give a false OK), and then
400 * read the value back. Note that we read it back
401 * into a variable because the next time we read it,
402 * it might be right (been there, tough to explain to
403 * the quality guys why it prints a failure when the
404 * "is" and "should be" are obviously the same in the
407 * Rather than exhaustively testing, we test some
408 * patterns by shifting '1' bits through a field of
409 * '0's and '0' bits through a field of '1's (i.e.
410 * pattern and ~pattern).
414 for(j = 0; j < sizeof(bitpattern)/sizeof(bitpattern[0]); j++){
417 for(; val != 0; val <<= 1){
419 *dummy = ~val; /* clear the test data off of the bus */
423 printf("## Error (data line): expected 0x%08lX, found 0x%08lX\n", val, readback);
430 if(readback != ~val){
431 printf("## Error (data line): expected %08lX, found %08lX\n", ~val, readback);
437 * Based on code whose Original Author and Copyright
438 * information follows: Copyright (c) 1998 by Michael
439 * Barr. This software is placed into the public
440 * domain and may be used for any purpose. However,
441 * this notice must not be changed or removed and no
442 * warranty is either expressed or implied by its
443 * publication or distribution.
449 * Description: Test the address bus wiring in a
450 * memory region by performing a walking
451 * 1's test on the relevant bits of the
452 * address and checking for aliasing.
453 * This test will find single-bit
454 * address failures such as stuck -high,
455 * stuck-low, and shorted pins. The base
456 * address and size of the region are
457 * selected by the caller.
459 * Notes: For best results, the selected base
460 * address should have enough LSB 0's to
461 * guarantee single address bit changes.
462 * For example, to test a 64-Kbyte
463 * region, select a base address on a
464 * 64-Kbyte boundary. Also, select the
465 * region size as a power-of-two if at
468 * Returns: 0 if the test succeeds, 1 if the test fails.
470 * ## NOTE ## Be sure to specify start and end
471 * addresses such that addr_mask has
472 * lots of bits set. For example an
473 * address range of 01000000 02000000 is
474 * bad while a range of 01000000
475 * 01ffffff is perfect.
477 addr_mask = ((ulong)end - (ulong)start)/sizeof(vu_long);
478 pattern = (vu_long)0xaaaaaaaa;
479 anti_pattern = (vu_long)0x55555555;
482 * Write the default pattern at each of the
483 * power-of-two offsets.
485 for(offset = 1; (offset & addr_mask) != 0; offset <<= 1){
486 start[offset] = pattern;
490 * Check for address bits stuck high.
493 start[test_offset] = anti_pattern;
495 for(offset = 1; (offset & addr_mask) != 0; offset <<= 1){
496 temp = start[offset];
499 printf("\n## Error: address bit stuck high @ 0x%.8lX, expected 0x%.8lX, found 0x%.8lX\n", (ulong)&start[offset], pattern, temp);
503 start[test_offset] = pattern;
506 * Check for addr bits stuck low or shorted.
508 for(test_offset = 1; (test_offset & addr_mask) != 0; test_offset <<= 1){
509 start[test_offset] = anti_pattern;
511 for(offset = 1; (offset & addr_mask) != 0; offset <<= 1){
512 temp = start[offset];
514 if((temp != pattern) && (offset != test_offset)){
515 printf("\n## Error: address bit stuck low or shorted @ 0x%.8lX, expected 0x%.8lX, found 0x%.8lX\n", (ulong)&start[offset], pattern, temp);
519 start[test_offset] = pattern;
523 * Description: Test the integrity of a physical
524 * memory device by performing an
525 * increment/decrement test over the
526 * entire region. In the process every
527 * storage bit in the device is tested
528 * as a zero and a one. The base address
529 * and the size of the region are
530 * selected by the caller.
532 * Returns: 0 if the test succeeds, 1 if the test fails.
534 num_words = ((ulong)end - (ulong)start)/sizeof(vu_long) + 1;
537 * Fill memory with a known pattern.
539 for(pattern = 1, offset = 0; offset < num_words; pattern++, offset++){
540 start[offset] = pattern;
544 * Check each location and invert it for the second pass.
546 for(pattern = 1, offset = 0; offset < num_words; pattern++, offset++){
547 temp = start[offset];
550 printf("\n## Error (read/write) @ 0x%.8lX: expected 0x%.8lX, found 0x%.8lX)\n", (ulong)&start[offset], pattern, temp);
554 anti_pattern = ~pattern;
555 start[offset] = anti_pattern;
559 * Check each location for the inverted pattern and zero it.
561 for(pattern = 1, offset = 0; offset < num_words; pattern++, offset++){
562 anti_pattern = ~pattern;
563 temp = start[offset];
565 if(temp != anti_pattern){
566 printf("\n## Error (read/write): @ 0x%.8lX: expected 0x%.8lX, found 0x%.8lX)\n", (ulong)&start[offset], anti_pattern, temp);
573 #else /* The original, quickie test */
581 printf("\rPattern %08lX Writing...%12s\b\b\b\b\b\b\b\b\b\b", pattern, "");
583 for(addr = start, val = pattern; addr < end; addr++){
590 for(addr = start, val = pattern; addr < end; addr++){
593 printf("\n## Error @ 0x%08X: found %08lX, expected %08lX\n", (uint)addr, readback, val);
600 * Flip the pattern each time to make lots of zeros and
601 * then, the next time, lots of ones. We decrement
602 * the "negative" patterns and increment the "positive"
603 * patterns to preserve this feature.
605 if(pattern & 0x80000000){
606 pattern = -pattern; /* complement & increment */
619 * mm{.b, .w, .l} {addr}
620 * nm{.b, .w, .l} {addr}
622 static int mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[]){
625 extern char console_buffer[];
629 if(cmdtp->help != NULL){
630 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->help);
632 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->usage);
635 printf("Usage:\n%s %s\n", cmdtp->name, cmdtp->usage);
640 /* We use the last specified parameters, unless new ones are
646 if((flag & CMD_FLAG_REPEAT) == 0){
647 /* New command specified. Check for a size specification.
648 * Defaults to long if no or incorrect specification.
650 if((size = cmd_get_data_size(argv[0], 4)) < 0){
654 /* Address is specified since argc > 1
656 addr = simple_strtoul(argv[1], NULL, 16);
659 /* Print the address, followed by value. Then accept input for
660 * the next value. A non-converted value exits.
663 printf("%08lX:", addr);
666 printf(" %08X", *((uint *)addr));
667 } else if(size == 2){
668 printf(" %04X", *((ushort *)addr));
670 printf(" %02X", *((u_char *)addr));
673 nbytes = readline(" ? ");
674 if(nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')){
675 /* <CR> pressed as only input, don't modify current
676 * location and move to next. "-" pressed will go back.
679 addr += nbytes ? -size : size;
684 i = simple_strtoul(console_buffer, &endp, 16);
685 nbytes = endp - console_buffer;
689 } else if(size == 2){
690 *((ushort *)addr) = i;
692 *((u_char *)addr) = i;
707 /**************************************************/
708 U_BOOT_CMD(md, 3, 1, do_mem_md, "memory display\n", "[.b, .w, .l] address [# of objects]\n\t- memory display\n");
709 U_BOOT_CMD(mm, 2, 1, do_mem_mm, "memory modify (auto-incrementing)\n", "[.b, .w, .l] address\n\t- memory modify, auto increment address\n");
710 U_BOOT_CMD(nm, 2, 1, do_mem_nm, "memory modify (constant address)\n", "[.b, .w, .l] address\n\t- memory modify, read and keep address\n");
711 U_BOOT_CMD(mw, 4, 1, do_mem_mw, "memory write (fill)\n", "[.b, .w, .l] address value [count]\n\t- write memory\n");
712 U_BOOT_CMD(mtest, 4, 1, do_mem_mtest, "simple RAM test\n", "[start [end [pattern]]]\n\t- simple RAM read/write test\n");
713 U_BOOT_CMD(cp, 4, 1, do_mem_cp, "memory copy\n", "[.b, .w, .l] source target count\n\t- copy memory\n");
715 #endif /* CFG_CMD_MEMORY */