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