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