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