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