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