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