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