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