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