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