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