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