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