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