]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_mem.c
Roll crc32 into hash infrastructure
[karo-tx-uboot.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 #ifdef CONFIG_HAS_DATAFLASH
33 #include <dataflash.h>
34 #endif
35 #include <hash.h>
36 #include <watchdog.h>
37 #include <asm/io.h>
38 #include <linux/compiler.h>
39
40 DECLARE_GLOBAL_DATA_PTR;
41
42 static int mod_mem(cmd_tbl_t *, int, int, int, char * const []);
43
44 /* Display values from last command.
45  * Memory modify remembered values are different from display memory.
46  */
47 static uint     dp_last_addr, dp_last_size;
48 static uint     dp_last_length = 0x40;
49 static uint     mm_last_addr, mm_last_size;
50
51 static  ulong   base_address = 0;
52
53 /* Memory Display
54  *
55  * Syntax:
56  *      md{.b, .w, .l} {addr} {len}
57  */
58 #define DISP_LINE_LEN   16
59 static int do_mem_md(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
60 {
61         ulong   addr, length;
62 #if defined(CONFIG_HAS_DATAFLASH)
63         ulong   nbytes, linebytes;
64 #endif
65         int     size;
66         int rc = 0;
67
68         /* We use the last specified parameters, unless new ones are
69          * entered.
70          */
71         addr = dp_last_addr;
72         size = dp_last_size;
73         length = dp_last_length;
74
75         if (argc < 2)
76                 return CMD_RET_USAGE;
77
78         if ((flag & CMD_FLAG_REPEAT) == 0) {
79                 /* New command specified.  Check for a size specification.
80                  * Defaults to long if no or incorrect specification.
81                  */
82                 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
83                         return 1;
84
85                 /* Address is specified since argc > 1
86                 */
87                 addr = simple_strtoul(argv[1], NULL, 16);
88                 addr += base_address;
89
90                 /* If another parameter, it is the length to display.
91                  * Length is the number of objects, not number of bytes.
92                  */
93                 if (argc > 2)
94                         length = simple_strtoul(argv[2], NULL, 16);
95         }
96
97 #if defined(CONFIG_HAS_DATAFLASH)
98         /* Print the lines.
99          *
100          * We buffer all read data, so we can make sure data is read only
101          * once, and all accesses are with the specified bus width.
102          */
103         nbytes = length * size;
104         do {
105                 char    linebuf[DISP_LINE_LEN];
106                 void* p;
107                 linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
108
109                 rc = read_dataflash(addr, (linebytes/size)*size, linebuf);
110                 p = (rc == DATAFLASH_OK) ? linebuf : (void*)addr;
111                 print_buffer(addr, p, size, linebytes/size, DISP_LINE_LEN/size);
112
113                 nbytes -= linebytes;
114                 addr += linebytes;
115                 if (ctrlc()) {
116                         rc = 1;
117                         break;
118                 }
119         } while (nbytes > 0);
120 #else
121
122 # if defined(CONFIG_BLACKFIN)
123         /* See if we're trying to display L1 inst */
124         if (addr_bfin_on_chip_mem(addr)) {
125                 char linebuf[DISP_LINE_LEN];
126                 ulong linebytes, nbytes = length * size;
127                 do {
128                         linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
129                         memcpy(linebuf, (void *)addr, linebytes);
130                         print_buffer(addr, linebuf, size, linebytes/size, DISP_LINE_LEN/size);
131
132                         nbytes -= linebytes;
133                         addr += linebytes;
134                         if (ctrlc()) {
135                                 rc = 1;
136                                 break;
137                         }
138                 } while (nbytes > 0);
139         } else
140 # endif
141
142         {
143                 ulong bytes = size * length;
144                 const void *buf = map_sysmem(addr, bytes);
145
146                 /* Print the lines. */
147                 print_buffer(addr, buf, size, length, DISP_LINE_LEN / size);
148                 addr += bytes;
149                 unmap_sysmem(buf);
150         }
151 #endif
152
153         dp_last_addr = addr;
154         dp_last_length = length;
155         dp_last_size = size;
156         return (rc);
157 }
158
159 static int do_mem_mm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
160 {
161         return mod_mem (cmdtp, 1, flag, argc, argv);
162 }
163 static int do_mem_nm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
164 {
165         return mod_mem (cmdtp, 0, flag, argc, argv);
166 }
167
168 static int do_mem_mw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
169 {
170         ulong   addr, writeval, count;
171         int     size;
172         void *buf;
173         ulong bytes;
174
175         if ((argc < 3) || (argc > 4))
176                 return CMD_RET_USAGE;
177
178         /* Check for size specification.
179         */
180         if ((size = cmd_get_data_size(argv[0], 4)) < 1)
181                 return 1;
182
183         /* Address is specified since argc > 1
184         */
185         addr = simple_strtoul(argv[1], NULL, 16);
186         addr += base_address;
187
188         /* Get the value to write.
189         */
190         writeval = simple_strtoul(argv[2], NULL, 16);
191
192         /* Count ? */
193         if (argc == 4) {
194                 count = simple_strtoul(argv[3], NULL, 16);
195         } else {
196                 count = 1;
197         }
198
199         bytes = size * count;
200         buf = map_sysmem(addr, bytes);
201         while (count-- > 0) {
202                 if (size == 4)
203                         *((ulong *)buf) = (ulong)writeval;
204                 else if (size == 2)
205                         *((ushort *)buf) = (ushort)writeval;
206                 else
207                         *((u_char *)buf) = (u_char)writeval;
208                 buf += size;
209         }
210         unmap_sysmem(buf);
211         return 0;
212 }
213
214 #ifdef CONFIG_MX_CYCLIC
215 int do_mem_mdc ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
216 {
217         int i;
218         ulong count;
219
220         if (argc < 4)
221                 return CMD_RET_USAGE;
222
223         count = simple_strtoul(argv[3], NULL, 10);
224
225         for (;;) {
226                 do_mem_md (NULL, 0, 3, argv);
227
228                 /* delay for <count> ms... */
229                 for (i=0; i<count; i++)
230                         udelay (1000);
231
232                 /* check for ctrl-c to abort... */
233                 if (ctrlc()) {
234                         puts("Abort\n");
235                         return 0;
236                 }
237         }
238
239         return 0;
240 }
241
242 int do_mem_mwc ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
243 {
244         int i;
245         ulong count;
246
247         if (argc < 4)
248                 return CMD_RET_USAGE;
249
250         count = simple_strtoul(argv[3], NULL, 10);
251
252         for (;;) {
253                 do_mem_mw (NULL, 0, 3, argv);
254
255                 /* delay for <count> ms... */
256                 for (i=0; i<count; i++)
257                         udelay (1000);
258
259                 /* check for ctrl-c to abort... */
260                 if (ctrlc()) {
261                         puts("Abort\n");
262                         return 0;
263                 }
264         }
265
266         return 0;
267 }
268 #endif /* CONFIG_MX_CYCLIC */
269
270 static int do_mem_cmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
271 {
272         ulong   addr1, addr2, count, ngood, bytes;
273         int     size;
274         int     rcode = 0;
275         const char *type;
276         const void *buf1, *buf2, *base;
277
278         if (argc != 4)
279                 return CMD_RET_USAGE;
280
281         /* Check for size specification.
282         */
283         if ((size = cmd_get_data_size(argv[0], 4)) < 0)
284                 return 1;
285         type = size == 4 ? "word" : size == 2 ? "halfword" : "byte";
286
287         addr1 = simple_strtoul(argv[1], NULL, 16);
288         addr1 += base_address;
289
290         addr2 = simple_strtoul(argv[2], NULL, 16);
291         addr2 += base_address;
292
293         count = simple_strtoul(argv[3], NULL, 16);
294
295 #ifdef CONFIG_HAS_DATAFLASH
296         if (addr_dataflash(addr1) | addr_dataflash(addr2)){
297                 puts ("Comparison with DataFlash space not supported.\n\r");
298                 return 0;
299         }
300 #endif
301
302 #ifdef CONFIG_BLACKFIN
303         if (addr_bfin_on_chip_mem(addr1) || addr_bfin_on_chip_mem(addr2)) {
304                 puts ("Comparison with L1 instruction memory not supported.\n\r");
305                 return 0;
306         }
307 #endif
308
309         bytes = size * count;
310         base = buf1 = map_sysmem(addr1, bytes);
311         buf2 = map_sysmem(addr2, bytes);
312         for (ngood = 0; ngood < count; ++ngood) {
313                 ulong word1, word2;
314                 if (size == 4) {
315                         word1 = *(ulong *)buf1;
316                         word2 = *(ulong *)buf2;
317                 } else if (size == 2) {
318                         word1 = *(ushort *)buf1;
319                         word2 = *(ushort *)buf2;
320                 } else {
321                         word1 = *(u_char *)buf1;
322                         word2 = *(u_char *)buf2;
323                 }
324                 if (word1 != word2) {
325                         ulong offset = buf1 - base;
326
327                         printf("%s at 0x%08lx (%#0*lx) != %s at 0x%08lx (%#0*lx)\n",
328                                 type, (ulong)(addr1 + offset), size, word1,
329                                 type, (ulong)(addr2 + offset), size, word2);
330                         rcode = 1;
331                         break;
332                 }
333
334                 buf1 += size;
335                 buf2 += size;
336
337                 /* reset watchdog from time to time */
338                 if ((ngood % (64 << 10)) == 0)
339                         WATCHDOG_RESET();
340         }
341         unmap_sysmem(buf1);
342         unmap_sysmem(buf2);
343
344         printf("Total of %ld %s(s) were the same\n", ngood, type);
345         return rcode;
346 }
347
348 static int do_mem_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
349 {
350         ulong   addr, dest, count, bytes;
351         int     size;
352         const void *src;
353         void *buf;
354
355         if (argc != 4)
356                 return CMD_RET_USAGE;
357
358         /* Check for size specification.
359         */
360         if ((size = cmd_get_data_size(argv[0], 4)) < 0)
361                 return 1;
362
363         addr = simple_strtoul(argv[1], NULL, 16);
364         addr += base_address;
365
366         dest = simple_strtoul(argv[2], NULL, 16);
367         dest += base_address;
368
369         count = simple_strtoul(argv[3], NULL, 16);
370
371         if (count == 0) {
372                 puts ("Zero length ???\n");
373                 return 1;
374         }
375
376 #ifndef CONFIG_SYS_NO_FLASH
377         /* check if we are copying to Flash */
378         if ( (addr2info(dest) != NULL)
379 #ifdef CONFIG_HAS_DATAFLASH
380            && (!addr_dataflash(dest))
381 #endif
382            ) {
383                 int rc;
384
385                 puts ("Copy to Flash... ");
386
387                 rc = flash_write ((char *)addr, dest, count*size);
388                 if (rc != 0) {
389                         flash_perror (rc);
390                         return (1);
391                 }
392                 puts ("done\n");
393                 return 0;
394         }
395 #endif
396
397 #ifdef CONFIG_HAS_DATAFLASH
398         /* Check if we are copying from RAM or Flash to DataFlash */
399         if (addr_dataflash(dest) && !addr_dataflash(addr)){
400                 int rc;
401
402                 puts ("Copy to DataFlash... ");
403
404                 rc = write_dataflash (dest, addr, count*size);
405
406                 if (rc != 1) {
407                         dataflash_perror (rc);
408                         return (1);
409                 }
410                 puts ("done\n");
411                 return 0;
412         }
413
414         /* Check if we are copying from DataFlash to RAM */
415         if (addr_dataflash(addr) && !addr_dataflash(dest)
416 #ifndef CONFIG_SYS_NO_FLASH
417                                  && (addr2info(dest) == NULL)
418 #endif
419            ){
420                 int rc;
421                 rc = read_dataflash(addr, count * size, (char *) dest);
422                 if (rc != 1) {
423                         dataflash_perror (rc);
424                         return (1);
425                 }
426                 return 0;
427         }
428
429         if (addr_dataflash(addr) && addr_dataflash(dest)){
430                 puts ("Unsupported combination of source/destination.\n\r");
431                 return 1;
432         }
433 #endif
434
435 #ifdef CONFIG_BLACKFIN
436         /* See if we're copying to/from L1 inst */
437         if (addr_bfin_on_chip_mem(dest) || addr_bfin_on_chip_mem(addr)) {
438                 memcpy((void *)dest, (void *)addr, count * size);
439                 return 0;
440         }
441 #endif
442
443         bytes = size * count;
444         buf = map_sysmem(addr, bytes);
445         src = map_sysmem(addr, bytes);
446         while (count-- > 0) {
447                 if (size == 4)
448                         *((ulong *)buf) = *((ulong  *)src);
449                 else if (size == 2)
450                         *((ushort *)buf) = *((ushort *)src);
451                 else
452                         *((u_char *)buf) = *((u_char *)src);
453                 src += size;
454                 buf += size;
455
456                 /* reset watchdog from time to time */
457                 if ((count % (64 << 10)) == 0)
458                         WATCHDOG_RESET();
459         }
460         return 0;
461 }
462
463 static int do_mem_base(cmd_tbl_t *cmdtp, int flag, int argc,
464                        char * const argv[])
465 {
466         if (argc > 1) {
467                 /* Set new base address.
468                 */
469                 base_address = simple_strtoul(argv[1], NULL, 16);
470         }
471         /* Print the current base address.
472         */
473         printf("Base Address: 0x%08lx\n", base_address);
474         return 0;
475 }
476
477 static int do_mem_loop(cmd_tbl_t *cmdtp, int flag, int argc,
478                        char * const argv[])
479 {
480         ulong   addr, length, i, bytes;
481         int     size;
482         volatile uint   *longp;
483         volatile ushort *shortp;
484         volatile u_char *cp;
485         const void *buf;
486
487         if (argc < 3)
488                 return CMD_RET_USAGE;
489
490         /*
491          * Check for a size specification.
492          * Defaults to long if no or incorrect specification.
493          */
494         if ((size = cmd_get_data_size(argv[0], 4)) < 0)
495                 return 1;
496
497         /* Address is always specified.
498         */
499         addr = simple_strtoul(argv[1], NULL, 16);
500
501         /* Length is the number of objects, not number of bytes.
502         */
503         length = simple_strtoul(argv[2], NULL, 16);
504
505         bytes = size * length;
506         buf = map_sysmem(addr, bytes);
507
508         /* We want to optimize the loops to run as fast as possible.
509          * If we have only one object, just run infinite loops.
510          */
511         if (length == 1) {
512                 if (size == 4) {
513                         longp = (uint *)buf;
514                         for (;;)
515                                 i = *longp;
516                 }
517                 if (size == 2) {
518                         shortp = (ushort *)buf;
519                         for (;;)
520                                 i = *shortp;
521                 }
522                 cp = (u_char *)buf;
523                 for (;;)
524                         i = *cp;
525         }
526
527         if (size == 4) {
528                 for (;;) {
529                         longp = (uint *)buf;
530                         i = length;
531                         while (i-- > 0)
532                                 *longp++;
533                 }
534         }
535         if (size == 2) {
536                 for (;;) {
537                         shortp = (ushort *)buf;
538                         i = length;
539                         while (i-- > 0)
540                                 *shortp++;
541                 }
542         }
543         for (;;) {
544                 cp = (u_char *)buf;
545                 i = length;
546                 while (i-- > 0)
547                         *cp++;
548         }
549         unmap_sysmem(buf);
550 }
551
552 #ifdef CONFIG_LOOPW
553 int do_mem_loopw (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
554 {
555         ulong   addr, length, i, data, bytes;
556         int     size;
557         volatile uint   *longp;
558         volatile ushort *shortp;
559         volatile u_char *cp;
560         void *buf;
561
562         if (argc < 4)
563                 return CMD_RET_USAGE;
564
565         /*
566          * Check for a size specification.
567          * Defaults to long if no or incorrect specification.
568          */
569         if ((size = cmd_get_data_size(argv[0], 4)) < 0)
570                 return 1;
571
572         /* Address is always specified.
573         */
574         addr = simple_strtoul(argv[1], NULL, 16);
575
576         /* Length is the number of objects, not number of bytes.
577         */
578         length = simple_strtoul(argv[2], NULL, 16);
579
580         /* data to write */
581         data = simple_strtoul(argv[3], NULL, 16);
582
583         bytes = size * length;
584         buf = map_sysmem(addr, bytes);
585
586         /* We want to optimize the loops to run as fast as possible.
587          * If we have only one object, just run infinite loops.
588          */
589         if (length == 1) {
590                 if (size == 4) {
591                         longp = (uint *)buf;
592                         for (;;)
593                                 *longp = data;
594                                         }
595                 if (size == 2) {
596                         shortp = (ushort *)buf;
597                         for (;;)
598                                 *shortp = data;
599                 }
600                 cp = (u_char *)buf;
601                 for (;;)
602                         *cp = data;
603         }
604
605         if (size == 4) {
606                 for (;;) {
607                         longp = (uint *)buf;
608                         i = length;
609                         while (i-- > 0)
610                                 *longp++ = data;
611                 }
612         }
613         if (size == 2) {
614                 for (;;) {
615                         shortp = (ushort *)buf;
616                         i = length;
617                         while (i-- > 0)
618                                 *shortp++ = data;
619                 }
620         }
621         for (;;) {
622                 cp = (u_char *)buf;
623                 i = length;
624                 while (i-- > 0)
625                         *cp++ = data;
626         }
627 }
628 #endif /* CONFIG_LOOPW */
629
630 static ulong mem_test_alt(ulong start_addr, ulong end_addr)
631 {
632         vu_long *start, *end;
633         vu_long *addr;
634         ulong errs = 0;
635         ulong val, readback;
636         int j;
637         vu_long len;
638         vu_long offset;
639         vu_long test_offset;
640         vu_long pattern;
641         vu_long temp;
642         vu_long anti_pattern;
643         vu_long num_words;
644 #if defined(CONFIG_SYS_MEMTEST_SCRATCH)
645         vu_long *dummy = (vu_long *)CONFIG_SYS_MEMTEST_SCRATCH;
646 #else
647         vu_long *dummy = NULL;  /* yes, this is address 0x0, not NULL */
648 #endif
649         static const ulong bitpattern[] = {
650                 0x00000001,     /* single bit */
651                 0x00000003,     /* two adjacent bits */
652                 0x00000007,     /* three adjacent bits */
653                 0x0000000F,     /* four adjacent bits */
654                 0x00000005,     /* two non-adjacent bits */
655                 0x00000015,     /* three non-adjacent bits */
656                 0x00000055,     /* four non-adjacent bits */
657                 0xaaaaaaaa,     /* alternating 1/0 */
658         };
659
660         start = (vu_long *)start_addr;
661         end = (vu_long *)end_addr;
662
663         /*
664          * Data line test: write a pattern to the first
665          * location, write the 1's complement to a 'parking'
666          * address (changes the state of the data bus so a
667          * floating bus doesn't give a false OK), and then
668          * read the value back. Note that we read it back
669          * into a variable because the next time we read it,
670          * it might be right (been there, tough to explain to
671          * the quality guys why it prints a failure when the
672          * "is" and "should be" are obviously the same in the
673          * error message).
674          *
675          * Rather than exhaustively testing, we test some
676          * patterns by shifting '1' bits through a field of
677          * '0's and '0' bits through a field of '1's (i.e.
678          * pattern and ~pattern).
679          */
680         addr = start;
681         for (j = 0; j < sizeof(bitpattern) / sizeof(bitpattern[0]); j++) {
682                 val = bitpattern[j];
683                 for (; val != 0; val <<= 1) {
684                         *addr  = val;
685                         *dummy  = ~val; /* clear the test data off the bus */
686                         readback = *addr;
687                         if (readback != val) {
688                                 printf("FAILURE (data line): "
689                                         "expected %08lx, actual %08lx\n",
690                                                 val, readback);
691                                 errs++;
692                                 if (ctrlc())
693                                         return -1;
694                         }
695                         *addr  = ~val;
696                         *dummy  = val;
697                         readback = *addr;
698                         if (readback != ~val) {
699                                 printf("FAILURE (data line): "
700                                         "Is %08lx, should be %08lx\n",
701                                                 readback, ~val);
702                                 errs++;
703                                 if (ctrlc())
704                                         return -1;
705                         }
706                 }
707         }
708
709         /*
710          * Based on code whose Original Author and Copyright
711          * information follows: Copyright (c) 1998 by Michael
712          * Barr. This software is placed into the public
713          * domain and may be used for any purpose. However,
714          * this notice must not be changed or removed and no
715          * warranty is either expressed or implied by its
716          * publication or distribution.
717          */
718
719         /*
720         * Address line test
721
722          * Description: Test the address bus wiring in a
723          *              memory region by performing a walking
724          *              1's test on the relevant bits of the
725          *              address and checking for aliasing.
726          *              This test will find single-bit
727          *              address failures such as stuck-high,
728          *              stuck-low, and shorted pins. The base
729          *              address and size of the region are
730          *              selected by the caller.
731
732          * Notes:       For best results, the selected base
733          *              address should have enough LSB 0's to
734          *              guarantee single address bit changes.
735          *              For example, to test a 64-Kbyte
736          *              region, select a base address on a
737          *              64-Kbyte boundary. Also, select the
738          *              region size as a power-of-two if at
739          *              all possible.
740          *
741          * Returns:     0 if the test succeeds, 1 if the test fails.
742          */
743         len = (end_addr - start_addr) / sizeof(vu_long);
744         pattern = (vu_long) 0xaaaaaaaa;
745         anti_pattern = (vu_long) 0x55555555;
746
747         debug("%s:%d: length = 0x%.8lx\n",
748                 __func__, __LINE__, len);
749         /*
750          * Write the default pattern at each of the
751          * power-of-two offsets.
752          */
753         for (offset = 1; offset < len; offset <<= 1)
754                 start[offset] = pattern;
755
756         /*
757          * Check for address bits stuck high.
758          */
759         test_offset = 0;
760         start[test_offset] = anti_pattern;
761
762         for (offset = 1; offset < len; offset <<= 1) {
763                 temp = start[offset];
764                 if (temp != pattern) {
765                         printf("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
766                                 " expected 0x%.8lx, actual 0x%.8lx\n",
767                                 (ulong)&start[offset], pattern, temp);
768                         errs++;
769                         if (ctrlc())
770                                 return -1;
771                 }
772         }
773         start[test_offset] = pattern;
774         WATCHDOG_RESET();
775
776         /*
777          * Check for addr bits stuck low or shorted.
778          */
779         for (test_offset = 1; test_offset < len; test_offset <<= 1) {
780                 start[test_offset] = anti_pattern;
781
782                 for (offset = 1; offset < len; offset <<= 1) {
783                         temp = start[offset];
784                         if ((temp != pattern) && (offset != test_offset)) {
785                                 printf("\nFAILURE: Address bit stuck low or"
786                                         " shorted @ 0x%.8lx: expected 0x%.8lx,"
787                                         " actual 0x%.8lx\n",
788                                         (ulong)&start[offset], pattern, temp);
789                                 errs++;
790                                 if (ctrlc())
791                                         return -1;
792                         }
793                 }
794                 start[test_offset] = pattern;
795         }
796
797         /*
798          * Description: Test the integrity of a physical
799          *              memory device by performing an
800          *              increment/decrement test over the
801          *              entire region. In the process every
802          *              storage bit in the device is tested
803          *              as a zero and a one. The base address
804          *              and the size of the region are
805          *              selected by the caller.
806          *
807          * Returns:     0 if the test succeeds, 1 if the test fails.
808          */
809         num_words = ((ulong)end - (ulong)start)/sizeof(vu_long) + 1;
810
811         /*
812          * Fill memory with a known pattern.
813          */
814         for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
815                 WATCHDOG_RESET();
816                 start[offset] = pattern;
817         }
818
819         /*
820          * Check each location and invert it for the second pass.
821          */
822         for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
823                 WATCHDOG_RESET();
824                 temp = start[offset];
825                 if (temp != pattern) {
826                         printf("\nFAILURE (read/write) @ 0x%.8lx:"
827                                 " expected 0x%.8lx, actual 0x%.8lx)\n",
828                                 (ulong)&start[offset], pattern, temp);
829                         errs++;
830                         if (ctrlc())
831                                 return -1;
832                 }
833
834                 anti_pattern = ~pattern;
835                 start[offset] = anti_pattern;
836         }
837
838         /*
839          * Check each location for the inverted pattern and zero it.
840          */
841         for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
842                 WATCHDOG_RESET();
843                 anti_pattern = ~pattern;
844                 temp = start[offset];
845                 if (temp != anti_pattern) {
846                         printf("\nFAILURE (read/write): @ 0x%.8lx:"
847                                 " expected 0x%.8lx, actual 0x%.8lx)\n",
848                                 (ulong)&start[offset], anti_pattern, temp);
849                         errs++;
850                         if (ctrlc())
851                                 return -1;
852                 }
853                 start[offset] = 0;
854         }
855
856         return 0;
857 }
858
859 static ulong mem_test_quick(ulong start_addr, ulong end_addr, vu_long pattern,
860                             int iteration)
861 {
862         vu_long *start, *end;
863         vu_long *addr;
864         ulong errs = 0;
865         ulong incr;
866         ulong val, readback;
867
868         /* Alternate the pattern */
869         incr = 1;
870         if (iteration & 1) {
871                 incr = -incr;
872                 /*
873                  * Flip the pattern each time to make lots of zeros and
874                  * then, the next time, lots of ones.  We decrement
875                  * the "negative" patterns and increment the "positive"
876                  * patterns to preserve this feature.
877                  */
878                 if (pattern & 0x80000000)
879                         pattern = -pattern;     /* complement & increment */
880                 else
881                         pattern = ~pattern;
882         }
883         start = (vu_long *)start_addr;
884         end = (vu_long *)end_addr;
885         printf("\rPattern %08lX  Writing..."
886                 "%12s"
887                 "\b\b\b\b\b\b\b\b\b\b",
888                 pattern, "");
889
890         for (addr = start, val = pattern; addr < end; addr++) {
891                 WATCHDOG_RESET();
892                 *addr = val;
893                 val += incr;
894         }
895
896         puts("Reading...");
897
898         for (addr = start, val = pattern; addr < end; addr++) {
899                 WATCHDOG_RESET();
900                 readback = *addr;
901                 if (readback != val) {
902                         printf("\nMem error @ 0x%08X: "
903                                 "found %08lX, expected %08lX\n",
904                                 (uint)(uintptr_t)addr, readback, val);
905                         errs++;
906                         if (ctrlc())
907                                 return -1;
908                 }
909                 val += incr;
910         }
911
912         return 0;
913 }
914
915 /*
916  * Perform a memory test. A more complete alternative test can be
917  * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
918  * interrupted by ctrl-c or by a failure of one of the sub-tests.
919  */
920 static int do_mem_mtest(cmd_tbl_t *cmdtp, int flag, int argc,
921                         char * const argv[])
922 {
923         ulong start, end;
924         int iteration_limit;
925         int ret;
926         ulong errs = 0; /* number of errors, or -1 if interrupted */
927         ulong pattern;
928         int iteration;
929 #if defined(CONFIG_SYS_ALT_MEMTEST)
930         const int alt_test = 1;
931 #else
932         const int alt_test = 0;
933 #endif
934
935         if (argc > 1)
936                 start = simple_strtoul(argv[1], NULL, 16);
937         else
938                 start = CONFIG_SYS_MEMTEST_START;
939
940         if (argc > 2)
941                 end = simple_strtoul(argv[2], NULL, 16);
942         else
943                 end = CONFIG_SYS_MEMTEST_END;
944
945         if (argc > 3)
946                 pattern = (ulong)simple_strtoul(argv[3], NULL, 16);
947         else
948                 pattern = 0;
949
950         if (argc > 4)
951                 iteration_limit = (ulong)simple_strtoul(argv[4], NULL, 16);
952         else
953                 iteration_limit = 0;
954
955         printf("Testing %08x ... %08x:\n", (uint)start, (uint)end);
956         debug("%s:%d: start %#08lx end %#08lx\n", __func__, __LINE__,
957               start, end);
958
959         for (iteration = 0;
960                         !iteration_limit || iteration < iteration_limit;
961                         iteration++) {
962                 if (ctrlc()) {
963                         errs = -1UL;
964                         break;
965                 }
966
967                 printf("Iteration: %6d\r", iteration + 1);
968                 debug("\n");
969                 if (alt_test)
970                         errs = mem_test_alt(start, end);
971                 else
972                         errs = mem_test_quick(start, end, pattern, iteration);
973         }
974
975         if (errs == -1UL) {
976                 /* Memory test was aborted - write a newline to finish off */
977                 putc('\n');
978                 ret = 1;
979         } else {
980                 printf("Tested %d iteration(s) with %lu errors.\n",
981                         iteration, errs);
982                 ret = errs != 0;
983         }
984
985         return ret;     /* not reached */
986 }
987
988
989 /* Modify memory.
990  *
991  * Syntax:
992  *      mm{.b, .w, .l} {addr}
993  *      nm{.b, .w, .l} {addr}
994  */
995 static int
996 mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
997 {
998         ulong   addr, i;
999         int     nbytes, size;
1000         void *ptr = NULL;
1001
1002         if (argc != 2)
1003                 return CMD_RET_USAGE;
1004
1005 #ifdef CONFIG_BOOT_RETRY_TIME
1006         reset_cmd_timeout();    /* got a good command to get here */
1007 #endif
1008         /* We use the last specified parameters, unless new ones are
1009          * entered.
1010          */
1011         addr = mm_last_addr;
1012         size = mm_last_size;
1013
1014         if ((flag & CMD_FLAG_REPEAT) == 0) {
1015                 /* New command specified.  Check for a size specification.
1016                  * Defaults to long if no or incorrect specification.
1017                  */
1018                 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
1019                         return 1;
1020
1021                 /* Address is specified since argc > 1
1022                 */
1023                 addr = simple_strtoul(argv[1], NULL, 16);
1024                 addr += base_address;
1025         }
1026
1027 #ifdef CONFIG_HAS_DATAFLASH
1028         if (addr_dataflash(addr)){
1029                 puts ("Can't modify DataFlash in place. Use cp instead.\n\r");
1030                 return 0;
1031         }
1032 #endif
1033
1034 #ifdef CONFIG_BLACKFIN
1035         if (addr_bfin_on_chip_mem(addr)) {
1036                 puts ("Can't modify L1 instruction in place. Use cp instead.\n\r");
1037                 return 0;
1038         }
1039 #endif
1040
1041         /* Print the address, followed by value.  Then accept input for
1042          * the next value.  A non-converted value exits.
1043          */
1044         do {
1045                 ptr = map_sysmem(addr, size);
1046                 printf("%08lx:", addr);
1047                 if (size == 4)
1048                         printf(" %08x", *((uint *)ptr));
1049                 else if (size == 2)
1050                         printf(" %04x", *((ushort *)ptr));
1051                 else
1052                         printf(" %02x", *((u_char *)ptr));
1053
1054                 nbytes = readline (" ? ");
1055                 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
1056                         /* <CR> pressed as only input, don't modify current
1057                          * location and move to next. "-" pressed will go back.
1058                          */
1059                         if (incrflag)
1060                                 addr += nbytes ? -size : size;
1061                         nbytes = 1;
1062 #ifdef CONFIG_BOOT_RETRY_TIME
1063                         reset_cmd_timeout(); /* good enough to not time out */
1064 #endif
1065                 }
1066 #ifdef CONFIG_BOOT_RETRY_TIME
1067                 else if (nbytes == -2) {
1068                         break;  /* timed out, exit the command  */
1069                 }
1070 #endif
1071                 else {
1072                         char *endp;
1073                         i = simple_strtoul(console_buffer, &endp, 16);
1074                         nbytes = endp - console_buffer;
1075                         if (nbytes) {
1076 #ifdef CONFIG_BOOT_RETRY_TIME
1077                                 /* good enough to not time out
1078                                  */
1079                                 reset_cmd_timeout();
1080 #endif
1081                                 if (size == 4)
1082                                         *((uint *)ptr) = i;
1083                                 else if (size == 2)
1084                                         *((ushort *)ptr) = i;
1085                                 else
1086                                         *((u_char *)ptr) = i;
1087                                 if (incrflag)
1088                                         addr += size;
1089                         }
1090                 }
1091         } while (nbytes);
1092         if (ptr)
1093                 unmap_sysmem(ptr);
1094
1095         mm_last_addr = addr;
1096         mm_last_size = size;
1097         return 0;
1098 }
1099
1100 #ifdef CONFIG_CMD_CRC32
1101
1102 static int do_mem_crc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1103 {
1104         int flags = 0;
1105         int ac;
1106         char * const *av;
1107
1108         if (argc < 3)
1109                 return CMD_RET_USAGE;
1110
1111         av = argv + 1;
1112         ac = argc - 1;
1113 #ifdef CONFIG_HASH_VERIFY
1114         if (strcmp(*av, "-v") == 0) {
1115                 flags |= HASH_FLAG_VERIFY;
1116                 av++;
1117                 ac--;
1118         }
1119 #endif
1120
1121         return hash_command("crc32", flags, cmdtp, flag, ac, av);
1122 }
1123
1124 #endif
1125
1126 /**************************************************/
1127 U_BOOT_CMD(
1128         md,     3,      1,      do_mem_md,
1129         "memory display",
1130         "[.b, .w, .l] address [# of objects]"
1131 );
1132
1133
1134 U_BOOT_CMD(
1135         mm,     2,      1,      do_mem_mm,
1136         "memory modify (auto-incrementing address)",
1137         "[.b, .w, .l] address"
1138 );
1139
1140
1141 U_BOOT_CMD(
1142         nm,     2,      1,      do_mem_nm,
1143         "memory modify (constant address)",
1144         "[.b, .w, .l] address"
1145 );
1146
1147 U_BOOT_CMD(
1148         mw,     4,      1,      do_mem_mw,
1149         "memory write (fill)",
1150         "[.b, .w, .l] address value [count]"
1151 );
1152
1153 U_BOOT_CMD(
1154         cp,     4,      1,      do_mem_cp,
1155         "memory copy",
1156         "[.b, .w, .l] source target count"
1157 );
1158
1159 U_BOOT_CMD(
1160         cmp,    4,      1,      do_mem_cmp,
1161         "memory compare",
1162         "[.b, .w, .l] addr1 addr2 count"
1163 );
1164
1165 #ifdef CONFIG_CMD_CRC32
1166
1167 #ifndef CONFIG_CRC32_VERIFY
1168
1169 U_BOOT_CMD(
1170         crc32,  4,      1,      do_mem_crc,
1171         "checksum calculation",
1172         "address count [addr]\n    - compute CRC32 checksum [save at addr]"
1173 );
1174
1175 #else   /* CONFIG_CRC32_VERIFY */
1176
1177 U_BOOT_CMD(
1178         crc32,  5,      1,      do_mem_crc,
1179         "checksum calculation",
1180         "address count [addr]\n    - compute CRC32 checksum [save at addr]\n"
1181         "-v address count crc\n    - verify crc of memory area"
1182 );
1183
1184 #endif  /* CONFIG_CRC32_VERIFY */
1185
1186 #endif
1187
1188 #ifdef CONFIG_CMD_MEMINFO
1189 __weak void board_show_dram(ulong size)
1190 {
1191         puts("DRAM:  ");
1192         print_size(size, "\n");
1193 }
1194
1195 static int do_mem_info(cmd_tbl_t *cmdtp, int flag, int argc,
1196                        char * const argv[])
1197 {
1198         board_show_dram(gd->ram_size);
1199
1200         return 0;
1201 }
1202 #endif
1203
1204 U_BOOT_CMD(
1205         base,   2,      1,      do_mem_base,
1206         "print or set address offset",
1207         "\n    - print address offset for memory commands\n"
1208         "base off\n    - set address offset for memory commands to 'off'"
1209 );
1210
1211 U_BOOT_CMD(
1212         loop,   3,      1,      do_mem_loop,
1213         "infinite loop on address range",
1214         "[.b, .w, .l] address number_of_objects"
1215 );
1216
1217 #ifdef CONFIG_LOOPW
1218 U_BOOT_CMD(
1219         loopw,  4,      1,      do_mem_loopw,
1220         "infinite write loop on address range",
1221         "[.b, .w, .l] address number_of_objects data_to_write"
1222 );
1223 #endif /* CONFIG_LOOPW */
1224
1225 U_BOOT_CMD(
1226         mtest,  5,      1,      do_mem_mtest,
1227         "simple RAM read/write test",
1228         "[start [end [pattern [iterations]]]]"
1229 );
1230
1231 #ifdef CONFIG_MX_CYCLIC
1232 U_BOOT_CMD(
1233         mdc,    4,      1,      do_mem_mdc,
1234         "memory display cyclic",
1235         "[.b, .w, .l] address count delay(ms)"
1236 );
1237
1238 U_BOOT_CMD(
1239         mwc,    4,      1,      do_mem_mwc,
1240         "memory write cyclic",
1241         "[.b, .w, .l] address value delay(ms)"
1242 );
1243 #endif /* CONFIG_MX_CYCLIC */
1244
1245 #ifdef CONFIG_CMD_MEMINFO
1246 U_BOOT_CMD(
1247         meminfo,        3,      1,      do_mem_info,
1248         "display memory information",
1249         ""
1250 );
1251 #endif