3e225988bba1e989d7a4060fb37b01474a336a88
[kernel/u-boot.git] / common / cmd_mem.c
1 /*
2  * (C) Copyright 2000
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
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.
12  *
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.
17  *
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,
21  * MA 02111-1307 USA
22  */
23
24 /*
25  * Memory Functions
26  *
27  * Copied from FADS ROM, Dan Malek (dmalek@jlc.net)
28  */
29
30 #include <common.h>
31 #include <command.h>
32 #if (CONFIG_COMMANDS & CFG_CMD_MMC)
33 #include <mmc.h>
34 #endif
35 #ifdef CONFIG_HAS_DATAFLASH
36 #include <dataflash.h>
37 #endif
38
39 #if (CONFIG_COMMANDS & (CFG_CMD_MEMORY  | \
40                         CFG_CMD_I2C     | \
41                         CFG_CMD_PCI     | \
42                         CMD_CMD_PORTIO  ) )
43 int cmd_get_data_size(char* arg, int default_size)
44 {
45         /* Check for a size specification .b, .w or .l.
46          */
47         int len = strlen(arg);
48         if (len > 2 && arg[len-2] == '.') {
49                 switch(arg[len-1]) {
50                 case 'b':
51                         return 1;
52                 case 'w':
53                         return 2;
54                 case 'l':
55                         return 4;
56                 default:
57                         return -1;
58                 }
59         }
60         return default_size;
61 }
62 #endif
63
64 #if (CONFIG_COMMANDS & CFG_CMD_MEMORY)
65
66 #ifdef  CMD_MEM_DEBUG
67 #define PRINTF(fmt,args...)     printf (fmt ,##args)
68 #else
69 #define PRINTF(fmt,args...)
70 #endif
71
72 static int mod_mem(cmd_tbl_t *, int, int, int, char *[]);
73
74 /* Display values from last command.
75  * Memory modify remembered values are different from display memory.
76  */
77 uint    dp_last_addr, dp_last_size;
78 uint    dp_last_length = 0x40;
79 uint    mm_last_addr, mm_last_size;
80
81 static  ulong   base_address = 0;
82
83 /* Memory Display
84  *
85  * Syntax:
86  *      md{.b, .w, .l} {addr} {len}
87  */
88 #define DISP_LINE_LEN   16
89 int do_mem_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
90 {
91         ulong   addr, length;
92         ulong   i, nbytes, linebytes;
93         u_char  *cp;
94         int     size;
95         int rc = 0;
96
97         /* We use the last specified parameters, unless new ones are
98          * entered.
99          */
100         addr = dp_last_addr;
101         size = dp_last_size;
102         length = dp_last_length;
103
104         if (argc < 2) {
105                 printf ("Usage:\n%s\n", cmdtp->usage);
106                 return 1;
107         }
108
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.
112                  */
113                 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
114                         return 1;
115
116                 /* Address is specified since argc > 1
117                 */
118                 addr = simple_strtoul(argv[1], NULL, 16);
119                 addr += base_address;
120
121                 /* If another parameter, it is the length to display.
122                  * Length is the number of objects, not number of bytes.
123                  */
124                 if (argc > 2)
125                         length = simple_strtoul(argv[2], NULL, 16);
126         }
127
128         /* Print the lines.
129          *
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.
132          */
133         nbytes = length * size;
134         do {
135                 char    linebuf[DISP_LINE_LEN];
136                 uint    *uip = (uint   *)linebuf;
137                 ushort  *usp = (ushort *)linebuf;
138                 u_char  *ucp = (u_char *)linebuf;
139
140                 printf("%08lx:", addr);
141                 linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
142
143 #ifdef CONFIG_HAS_DATAFLASH
144                 if (read_dataflash(addr, (linebytes/size)*size, linebuf) != -1){
145
146                         for (i=0; i<linebytes; i+= size) {
147                                 if (size == 4) {
148                                         printf(" %08x", *uip++);
149                                 } else if (size == 2) {
150                                         printf(" %04x", *usp++);
151                                 } else {
152                                         printf(" %02x", *ucp++);
153                                 }
154                                 addr += size;
155                         }
156
157                 } else {        /* addr does not correspond to DataFlash */
158 #endif
159                 for (i=0; i<linebytes; i+= size) {
160                         if (size == 4) {
161                                 printf(" %08x", (*uip++ = *((uint *)addr)));
162                         } else if (size == 2) {
163                                 printf(" %04x", (*usp++ = *((ushort *)addr)));
164                         } else {
165                                 printf(" %02x", (*ucp++ = *((u_char *)addr)));
166                         }
167                         addr += size;
168                 }
169 #ifdef CONFIG_HAS_DATAFLASH
170                 }
171 #endif
172                 printf("    ");
173                 cp = linebuf;
174                 for (i=0; i<linebytes; i++) {
175                         if ((*cp < 0x20) || (*cp > 0x7e))
176                                 printf(".");
177                         else
178                                 printf("%c", *cp);
179                         cp++;
180                 }
181                 printf("\n");
182                 nbytes -= linebytes;
183                 if (ctrlc()) {
184                         rc = 1;
185                         break;
186                 }
187         } while (nbytes > 0);
188
189         dp_last_addr = addr;
190         dp_last_length = length;
191         dp_last_size = size;
192         return (rc);
193 }
194
195 int do_mem_mm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
196 {
197         return mod_mem (cmdtp, 1, flag, argc, argv);
198 }
199 int do_mem_nm ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
200 {
201         return mod_mem (cmdtp, 0, flag, argc, argv);
202 }
203
204 int do_mem_mw ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
205 {
206         ulong   addr, writeval, count;
207         int     size;
208
209         if ((argc < 3) || (argc > 4)) {
210                 printf ("Usage:\n%s\n", cmdtp->usage);
211                 return 1;
212         }
213
214         /* Check for size specification.
215         */
216         if ((size = cmd_get_data_size(argv[0], 4)) < 1)
217                 return 1;
218
219         /* Address is specified since argc > 1
220         */
221         addr = simple_strtoul(argv[1], NULL, 16);
222         addr += base_address;
223
224         /* Get the value to write.
225         */
226         writeval = simple_strtoul(argv[2], NULL, 16);
227
228         /* Count ? */
229         if (argc == 4) {
230                 count = simple_strtoul(argv[3], NULL, 16);
231         } else {
232                 count = 1;
233         }
234
235         while (count-- > 0) {
236                 if (size == 4)
237                         *((ulong  *)addr) = (ulong )writeval;
238                 else if (size == 2)
239                         *((ushort *)addr) = (ushort)writeval;
240                 else
241                         *((u_char *)addr) = (u_char)writeval;
242                 addr += size;
243         }
244         return 0;
245 }
246
247 int do_mem_cmp (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
248 {
249         ulong   addr1, addr2, count, ngood;
250         int     size;
251         int     rcode = 0;
252
253         if (argc != 4) {
254                 printf ("Usage:\n%s\n", cmdtp->usage);
255                 return 1;
256         }
257
258         /* Check for size specification.
259         */
260         if ((size = cmd_get_data_size(argv[0], 4)) < 0)
261                 return 1;
262
263         addr1 = simple_strtoul(argv[1], NULL, 16);
264         addr1 += base_address;
265
266         addr2 = simple_strtoul(argv[2], NULL, 16);
267         addr2 += base_address;
268
269         count = simple_strtoul(argv[3], NULL, 16);
270
271 #ifdef CONFIG_HAS_DATAFLASH
272         if (addr_dataflash(addr1) | addr_dataflash(addr2)){
273                 printf("Comparison with DataFlash space not supported.\n\r");
274                 return 0;
275         }
276 #endif
277
278         ngood = 0;
279
280         while (count-- > 0) {
281                 if (size == 4) {
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);
288                                 rcode = 1;
289                                 break;
290                         }
291                 }
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);
299                                 rcode = 1;
300                                 break;
301                         }
302                 }
303                 else {
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);
310                                 rcode = 1;
311                                 break;
312                         }
313                 }
314                 ngood++;
315                 addr1 += size;
316                 addr2 += size;
317         }
318
319         printf("Total of %ld %s%s were the same\n",
320                 ngood, size == 4 ? "word" : size == 2 ? "halfword" : "byte",
321                 ngood == 1 ? "" : "s");
322         return rcode;
323 }
324
325 int do_mem_cp ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
326 {
327         ulong   addr, dest, count;
328         int     size;
329
330         if (argc != 4) {
331                 printf ("Usage:\n%s\n", cmdtp->usage);
332                 return 1;
333         }
334
335         /* Check for size specification.
336         */
337         if ((size = cmd_get_data_size(argv[0], 4)) < 0)
338                 return 1;
339
340         addr = simple_strtoul(argv[1], NULL, 16);
341         addr += base_address;
342
343         dest = simple_strtoul(argv[2], NULL, 16);
344         dest += base_address;
345
346         count = simple_strtoul(argv[3], NULL, 16);
347
348         if (count == 0) {
349                 puts ("Zero length ???\n");
350                 return 1;
351         }
352
353 #ifndef CFG_NO_FLASH
354         /* check if we are copying to Flash */
355         if ( (addr2info(dest) != NULL)
356 #ifdef CONFIG_HAS_DATAFLASH
357            && (!addr_dataflash(addr))
358 #endif
359            ) {
360                 int rc;
361
362                 printf ("Copy to Flash... ");
363
364                 rc = flash_write ((uchar *)addr, dest, count*size);
365                 if (rc != 0) {
366                         flash_perror (rc);
367                         return (1);
368                 }
369                 puts ("done\n");
370                 return 0;
371         }
372 #endif
373
374 #if (CONFIG_COMMANDS & CFG_CMD_MMC)
375         if (mmc2info(dest)) {
376                 int rc;
377
378                 printf ("Copy to MMC... ");
379                 switch (rc = mmc_write ((uchar *)addr, dest, count*size)) {
380                 case 0:
381                         printf ("\n");
382                         return 1;
383                 case -1:
384                         printf("failed\n");
385                         return 1;
386                 default:
387                         printf ("%s[%d] FIXME: rc=%d\n",__FILE__,__LINE__,rc);
388                         return 1;
389                 }
390                 puts ("done\n");
391                 return 0;
392         }
393
394         if (mmc2info(addr)) {
395                 int rc;
396
397                 printf ("Copy from MMC... ");
398                 switch (rc = mmc_read (addr, (uchar *)dest, count*size)) {
399                 case 0:
400                         printf ("\n");
401                         return 1;
402                 case -1:
403                         printf("failed\n");
404                         return 1;
405                 default:
406                         printf ("%s[%d] FIXME: rc=%d\n",__FILE__,__LINE__,rc);
407                         return 1;
408                 }
409                 puts ("done\n");
410                 return 0;
411         }
412 #endif
413
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)){
417                 int rc;
418
419                 printf ("Copy to DataFlash... ");
420
421                 rc = write_dataflash (dest, addr, count*size);
422
423                 if (rc != 1) {
424                         dataflash_perror (rc);
425                         return (1);
426                 }
427                 puts ("done\n");
428                 return 0;
429         }
430
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);
434                 return 0;
435         }
436
437         if (addr_dataflash(addr) && addr_dataflash(dest)){
438                 printf("Unsupported combination of source/destination.\n\r");
439                 return 1;
440         }
441 #endif
442
443         while (count-- > 0) {
444                 if (size == 4)
445                         *((ulong  *)dest) = *((ulong  *)addr);
446                 else if (size == 2)
447                         *((ushort *)dest) = *((ushort *)addr);
448                 else
449                         *((u_char *)dest) = *((u_char *)addr);
450                 addr += size;
451                 dest += size;
452         }
453         return 0;
454 }
455
456 int do_mem_base (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
457 {
458         if (argc > 1) {
459                 /* Set new base address.
460                 */
461                 base_address = simple_strtoul(argv[1], NULL, 16);
462         }
463         /* Print the current base address.
464         */
465         printf("Base Address: 0x%08lx\n", base_address);
466         return 0;
467 }
468
469 int do_mem_loop (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
470 {
471         ulong   addr, length, i, junk;
472         int     size;
473         volatile uint   *longp;
474         volatile ushort *shortp;
475         volatile u_char *cp;
476
477         if (argc < 3) {
478                 printf ("Usage:\n%s\n", cmdtp->usage);
479                 return 1;
480         }
481
482         /* Check for a size spefication.
483          * Defaults to long if no or incorrect specification.
484          */
485         if ((size = cmd_get_data_size(argv[0], 4)) < 0)
486                 return 1;
487
488         /* Address is always specified.
489         */
490         addr = simple_strtoul(argv[1], NULL, 16);
491
492         /* Length is the number of objects, not number of bytes.
493         */
494         length = simple_strtoul(argv[2], NULL, 16);
495
496         /* We want to optimize the loops to run as fast as possible.
497          * If we have only one object, just run infinite loops.
498          */
499         if (length == 1) {
500                 if (size == 4) {
501                         longp = (uint *)addr;
502                         for (;;)
503                                 i = *longp;
504                 }
505                 if (size == 2) {
506                         shortp = (ushort *)addr;
507                         for (;;)
508                                 i = *shortp;
509                 }
510                 cp = (u_char *)addr;
511                 for (;;)
512                         i = *cp;
513         }
514
515         if (size == 4) {
516                 for (;;) {
517                         longp = (uint *)addr;
518                         i = length;
519                         while (i-- > 0)
520                                 junk = *longp++;
521                 }
522         }
523         if (size == 2) {
524                 for (;;) {
525                         shortp = (ushort *)addr;
526                         i = length;
527                         while (i-- > 0)
528                                 junk = *shortp++;
529                 }
530         }
531         for (;;) {
532                 cp = (u_char *)addr;
533                 i = length;
534                 while (i-- > 0)
535                         junk = *cp++;
536         }
537 }
538
539 /*
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.
543  */
544 int do_mem_mtest (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
545 {
546         vu_long *addr, *start, *end;
547         ulong   val;
548         ulong   readback;
549
550 #if defined(CFG_ALT_MEMTEST)
551         vu_long addr_mask;
552         vu_long offset;
553         vu_long test_offset;
554         vu_long pattern;
555         vu_long temp;
556         vu_long anti_pattern;
557         vu_long num_words;
558         vu_long *dummy = NULL;
559         int     j;
560         int iterations = 1;
561
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 */
571         };
572 #else
573         ulong   incr;
574         ulong   pattern;
575         int     rcode = 0;
576 #endif
577
578         if (argc > 1) {
579                 start = (ulong *)simple_strtoul(argv[1], NULL, 16);
580         } else {
581                 start = (ulong *)CFG_MEMTEST_START;
582         }
583
584         if (argc > 2) {
585                 end = (ulong *)simple_strtoul(argv[2], NULL, 16);
586         } else {
587                 end = (ulong *)(CFG_MEMTEST_END);
588         }
589
590         if (argc > 3) {
591                 pattern = (ulong)simple_strtoul(argv[3], NULL, 16);
592         } else {
593                 pattern = 0;
594         }
595
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);
600
601         for (;;) {
602                 if (ctrlc()) {
603                         putc ('\n');
604                         return 1;
605                 }
606
607                 printf("Iteration: %6d\r", iterations);
608                 PRINTF("Iteration: %6d\n", iterations);
609                 iterations++;
610
611                 /*
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
621                  * error message).
622                  *
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).
627                  */
628                 addr = start;
629                 for (j = 0; j < sizeof(bitpattern)/sizeof(bitpattern[0]); j++) {
630                     val = bitpattern[j];
631                     for(; val != 0; val <<= 1) {
632                         *addr  = val;
633                         *dummy  = ~val; /* clear the test data off of the bus */
634                         readback = *addr;
635                         if(readback != val) {
636                              printf ("FAILURE (data line): "
637                                 "expected %08lx, actual %08lx\n",
638                                           val, readback);
639                         }
640                         *addr  = ~val;
641                         *dummy  = val;
642                         readback = *addr;
643                         if(readback != ~val) {
644                             printf ("FAILURE (data line): "
645                                 "Is %08lx, should be %08lx\n",
646                                         val, readback);
647                         }
648                     }
649                 }
650
651                 /*
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.
659                  */
660
661                 /*
662                  * Address line test
663                  *
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.
673                  *
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
681                  *              all possible.
682                  *
683                  * Returns:     0 if the test succeeds, 1 if the test fails.
684                  *
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.
691                  */
692                 addr_mask = ((ulong)end - (ulong)start)/sizeof(vu_long);
693                 pattern = (vu_long) 0xaaaaaaaa;
694                 anti_pattern = (vu_long) 0x55555555;
695
696                 PRINTF("%s:%d: addr mask = 0x%.8lx\n",
697                         __FUNCTION__, __LINE__,
698                         addr_mask);
699                 /*
700                  * Write the default pattern at each of the
701                  * power-of-two offsets.
702                  */
703                 for (offset = 1; (offset & addr_mask) != 0; offset <<= 1) {
704                         start[offset] = pattern;
705                 }
706
707                 /*
708                  * Check for address bits stuck high.
709                  */
710                 test_offset = 0;
711                 start[test_offset] = anti_pattern;
712
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);
719                         return 1;
720                     }
721                 }
722                 start[test_offset] = pattern;
723
724                 /*
725                  * Check for addr bits stuck low or shorted.
726                  */
727                 for (test_offset = 1; (test_offset & addr_mask) != 0; test_offset <<= 1) {
728                     start[test_offset] = anti_pattern;
729
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);
736                             return 1;
737                         }
738                     }
739                     start[test_offset] = pattern;
740                 }
741
742                 /*
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.
751                  *
752                  * Returns:     0 if the test succeeds, 1 if the test fails.
753                  */
754                 num_words = ((ulong)end - (ulong)start)/sizeof(vu_long) + 1;
755
756                 /*
757                  * Fill memory with a known pattern.
758                  */
759                 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
760                         start[offset] = pattern;
761                 }
762
763                 /*
764                  * Check each location and invert it for the second pass.
765                  */
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);
772                         return 1;
773                     }
774
775                     anti_pattern = ~pattern;
776                     start[offset] = anti_pattern;
777                 }
778
779                 /*
780                  * Check each location for the inverted pattern and zero it.
781                  */
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);
789                         return 1;
790                     }
791                     start[offset] = 0;
792                 }
793         }
794
795 #else /* The original, quickie test */
796         incr = 1;
797         for (;;) {
798                 if (ctrlc()) {
799                         putc ('\n');
800                         return 1;
801                 }
802
803                 printf ("\rPattern %08lX  Writing..."
804                         "%12s"
805                         "\b\b\b\b\b\b\b\b\b\b",
806                         pattern, "");
807
808                 for (addr=start,val=pattern; addr<end; addr++) {
809                         *addr = val;
810                         val  += incr;
811                 }
812
813                 printf("Reading...");
814
815                 for (addr=start,val=pattern; addr<end; addr++) {
816                         readback = *addr;
817                         if (readback != val) {
818                                 printf ("\nMem error @ 0x%08X: "
819                                         "found %08lX, expected %08lX\n",
820                                         (uint)addr, readback, val);
821                                 rcode = 1;
822                         }
823                         val += incr;
824                 }
825
826                 /*
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.
831                  */
832                 if(pattern & 0x80000000) {
833                         pattern = -pattern;     /* complement & increment */
834                 }
835                 else {
836                         pattern = ~pattern;
837                 }
838                 incr = -incr;
839         }
840         return rcode;
841 #endif
842 }
843
844
845 /* Modify memory.
846  *
847  * Syntax:
848  *      mm{.b, .w, .l} {addr}
849  *      nm{.b, .w, .l} {addr}
850  */
851 static int
852 mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char *argv[])
853 {
854         ulong   addr, i;
855         int     nbytes, size;
856         extern char console_buffer[];
857
858         if (argc != 2) {
859                 printf ("Usage:\n%s\n", cmdtp->usage);
860                 return 1;
861         }
862
863 #ifdef CONFIG_BOOT_RETRY_TIME
864         reset_cmd_timeout();    /* got a good command to get here */
865 #endif
866         /* We use the last specified parameters, unless new ones are
867          * entered.
868          */
869         addr = mm_last_addr;
870         size = mm_last_size;
871
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.
875                  */
876                 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
877                         return 1;
878
879                 /* Address is specified since argc > 1
880                 */
881                 addr = simple_strtoul(argv[1], NULL, 16);
882                 addr += base_address;
883         }
884
885 #ifdef CONFIG_HAS_DATAFLASH
886         if (addr_dataflash(addr)){
887                 printf("Can't modify DataFlash in place. Use cp instead.\n\r");
888                 return 0;
889         }
890 #endif
891
892         /* Print the address, followed by value.  Then accept input for
893          * the next value.  A non-converted value exits.
894          */
895         do {
896                 printf("%08lx:", addr);
897                 if (size == 4)
898                         printf(" %08x", *((uint   *)addr));
899                 else if (size == 2)
900                         printf(" %04x", *((ushort *)addr));
901                 else
902                         printf(" %02x", *((u_char *)addr));
903
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.
908                          */
909                         if (incrflag)
910                                 addr += nbytes ? -size : size;
911                         nbytes = 1;
912 #ifdef CONFIG_BOOT_RETRY_TIME
913                         reset_cmd_timeout(); /* good enough to not time out */
914 #endif
915                 }
916 #ifdef CONFIG_BOOT_RETRY_TIME
917                 else if (nbytes == -2) {
918                         break;  /* timed out, exit the command  */
919                 }
920 #endif
921                 else {
922                         char *endp;
923                         i = simple_strtoul(console_buffer, &endp, 16);
924                         nbytes = endp - console_buffer;
925                         if (nbytes) {
926 #ifdef CONFIG_BOOT_RETRY_TIME
927                                 /* good enough to not time out
928                                  */
929                                 reset_cmd_timeout();
930 #endif
931                                 if (size == 4)
932                                         *((uint   *)addr) = i;
933                                 else if (size == 2)
934                                         *((ushort *)addr) = i;
935                                 else
936                                         *((u_char *)addr) = i;
937                                 if (incrflag)
938                                         addr += size;
939                         }
940                 }
941         } while (nbytes);
942
943         mm_last_addr = addr;
944         mm_last_size = size;
945         return 0;
946 }
947
948 int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
949 {
950         ulong addr, length;
951         ulong crc;
952         ulong *ptr;
953
954         if (argc < 3) {
955                 printf ("Usage:\n%s\n", cmdtp->usage);
956                 return 1;
957         }
958
959         addr = simple_strtoul (argv[1], NULL, 16);
960         addr += base_address;
961
962         length = simple_strtoul (argv[2], NULL, 16);
963
964         crc = crc32 (0, (const uchar *) addr, length);
965
966         printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
967                         addr, addr + length - 1, crc);
968
969         if (argc > 3) {
970                 ptr = (ulong *) simple_strtoul (argv[3], NULL, 16);
971                 *ptr = crc;
972         }
973
974         return 0;
975 }
976
977 /**************************************************/
978 #if (CONFIG_COMMANDS & CFG_CMD_MEMORY)
979 U_BOOT_CMD(
980         md,     3,     1,      do_mem_md,
981         "md      - memory display\n",
982         "[.b, .w, .l] address [# of objects]\n    - memory display\n"
983 );
984
985
986 U_BOOT_CMD(
987         mm,     2,      1,       do_mem_mm,
988         "mm      - memory modify (auto-incrementing)\n",
989         "[.b, .w, .l] address\n" "    - memory modify, auto increment address\n"
990 );
991
992
993 U_BOOT_CMD(
994         nm,     2,          1,          do_mem_nm,
995         "nm      - memory modify (constant address)\n",
996         "[.b, .w, .l] address\n    - memory modify, read and keep address\n"
997 );
998
999 U_BOOT_CMD(
1000         mw,    4,    1,     do_mem_mw,
1001         "mw      - memory write (fill)\n",
1002         "[.b, .w, .l] address value [count]\n    - write memory\n"
1003 );
1004
1005 U_BOOT_CMD(
1006         cp,    4,    1,    do_mem_cp,
1007         "cp      - memory copy\n",
1008         "[.b, .w, .l] source target count\n    - copy memory\n"
1009 );
1010
1011 U_BOOT_CMD(
1012         cmp,    4,     1,     do_mem_cmp,
1013         "cmp     - memory compare\n",
1014         "[.b, .w, .l] addr1 addr2 count\n    - compare memory\n"
1015 );
1016
1017 U_BOOT_CMD(
1018         crc32,    4,    1,     do_mem_crc,
1019         "crc32   - checksum calculation\n",
1020         "address count [addr]\n    - compute CRC32 checksum [save at addr]\n"
1021 );
1022
1023 U_BOOT_CMD(
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"
1028 );
1029
1030 U_BOOT_CMD(
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"
1035 );
1036
1037 U_BOOT_CMD(
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"
1042 );
1043
1044 #endif
1045 #endif  /* CFG_CMD_MEMORY */