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