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