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