blob: d44aa1d7339a711ba04db2031ba74866b50c273c [file] [log] [blame]
wdenk38635852002-08-27 05:55:31 +00001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24/*
25 * Memory Functions
26 *
27 * Copied from FADS ROM, Dan Malek (dmalek@jlc.net)
28 */
29
30#include <common.h>
31#include <command.h>
wdenk381669a2003-06-16 23:50:08 +000032#ifdef CONFIG_HAS_DATAFLASH
33#include <dataflash.h>
34#endif
Sergei Poselenov474dbb82008-04-09 16:09:41 +020035#include <watchdog.h>
Simon Glasseacd14f2012-11-30 13:01:20 +000036#include <linux/compiler.h>
37
38DECLARE_GLOBAL_DATA_PTR;
wdenk38635852002-08-27 05:55:31 +000039
Wolfgang Denk6262d0212010-06-28 22:00:46 +020040static int mod_mem(cmd_tbl_t *, int, int, int, char * const []);
wdenk38635852002-08-27 05:55:31 +000041
42/* Display values from last command.
43 * Memory modify remembered values are different from display memory.
44 */
Mike Frysinger1a4af8c2010-12-22 09:40:29 -050045static uint dp_last_addr, dp_last_size;
46static uint dp_last_length = 0x40;
47static uint mm_last_addr, mm_last_size;
wdenk38635852002-08-27 05:55:31 +000048
49static ulong base_address = 0;
50
51/* Memory Display
52 *
53 * Syntax:
54 * md{.b, .w, .l} {addr} {len}
55 */
56#define DISP_LINE_LEN 16
Kim Phillipsdc00a682012-10-29 13:34:31 +000057static int do_mem_md(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000058{
wdenk874ac262003-07-24 23:38:38 +000059 ulong addr, length;
Grant Likely02a00782007-02-20 09:05:00 +010060#if defined(CONFIG_HAS_DATAFLASH)
61 ulong nbytes, linebytes;
62#endif
wdenk874ac262003-07-24 23:38:38 +000063 int size;
wdenk38635852002-08-27 05:55:31 +000064 int rc = 0;
65
66 /* We use the last specified parameters, unless new ones are
67 * entered.
68 */
69 addr = dp_last_addr;
70 size = dp_last_size;
71 length = dp_last_length;
72
Wolfgang Denk3b683112010-07-17 01:06:04 +020073 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +000074 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +000075
76 if ((flag & CMD_FLAG_REPEAT) == 0) {
77 /* New command specified. Check for a size specification.
78 * Defaults to long if no or incorrect specification.
79 */
wdenk874ac262003-07-24 23:38:38 +000080 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
81 return 1;
wdenk38635852002-08-27 05:55:31 +000082
83 /* Address is specified since argc > 1
84 */
85 addr = simple_strtoul(argv[1], NULL, 16);
86 addr += base_address;
87
88 /* If another parameter, it is the length to display.
89 * Length is the number of objects, not number of bytes.
90 */
91 if (argc > 2)
92 length = simple_strtoul(argv[2], NULL, 16);
93 }
94
Grant Likely02a00782007-02-20 09:05:00 +010095#if defined(CONFIG_HAS_DATAFLASH)
wdenk38635852002-08-27 05:55:31 +000096 /* Print the lines.
97 *
98 * We buffer all read data, so we can make sure data is read only
99 * once, and all accesses are with the specified bus width.
100 */
101 nbytes = length * size;
102 do {
103 char linebuf[DISP_LINE_LEN];
Grant Likely02a00782007-02-20 09:05:00 +0100104 void* p;
wdenk38635852002-08-27 05:55:31 +0000105 linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
wdenk381669a2003-06-16 23:50:08 +0000106
Grant Likely02a00782007-02-20 09:05:00 +0100107 rc = read_dataflash(addr, (linebytes/size)*size, linebuf);
108 p = (rc == DATAFLASH_OK) ? linebuf : (void*)addr;
109 print_buffer(addr, p, size, linebytes/size, DISP_LINE_LEN/size);
wdenk57b2d802003-06-27 21:31:46 +0000110
wdenk38635852002-08-27 05:55:31 +0000111 nbytes -= linebytes;
Grant Likely02a00782007-02-20 09:05:00 +0100112 addr += linebytes;
wdenk38635852002-08-27 05:55:31 +0000113 if (ctrlc()) {
114 rc = 1;
115 break;
116 }
117 } while (nbytes > 0);
Grant Likely02a00782007-02-20 09:05:00 +0100118#else
Mike Frysingere578d682008-02-04 19:26:56 -0500119
120# if defined(CONFIG_BLACKFIN)
121 /* See if we're trying to display L1 inst */
122 if (addr_bfin_on_chip_mem(addr)) {
123 char linebuf[DISP_LINE_LEN];
124 ulong linebytes, nbytes = length * size;
125 do {
126 linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
127 memcpy(linebuf, (void *)addr, linebytes);
128 print_buffer(addr, linebuf, size, linebytes/size, DISP_LINE_LEN/size);
129
130 nbytes -= linebytes;
131 addr += linebytes;
132 if (ctrlc()) {
133 rc = 1;
134 break;
135 }
136 } while (nbytes > 0);
137 } else
138# endif
139
140 {
141 /* Print the lines. */
Kumar Gala9f4d72f2011-11-12 08:02:12 +0000142 print_buffer(addr, (void*)addr, size, length, DISP_LINE_LEN/size);
143 addr += size*length;
Mike Frysingere578d682008-02-04 19:26:56 -0500144 }
Grant Likely02a00782007-02-20 09:05:00 +0100145#endif
wdenk38635852002-08-27 05:55:31 +0000146
147 dp_last_addr = addr;
148 dp_last_length = length;
149 dp_last_size = size;
150 return (rc);
151}
152
Kim Phillipsdc00a682012-10-29 13:34:31 +0000153static int do_mem_mm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000154{
155 return mod_mem (cmdtp, 1, flag, argc, argv);
156}
Kim Phillipsdc00a682012-10-29 13:34:31 +0000157static int do_mem_nm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000158{
159 return mod_mem (cmdtp, 0, flag, argc, argv);
160}
161
Kim Phillipsdc00a682012-10-29 13:34:31 +0000162static int do_mem_mw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000163{
wdenk874ac262003-07-24 23:38:38 +0000164 ulong addr, writeval, count;
165 int size;
wdenk38635852002-08-27 05:55:31 +0000166
Wolfgang Denk3b683112010-07-17 01:06:04 +0200167 if ((argc < 3) || (argc > 4))
Simon Glassa06dfc72011-12-10 08:44:01 +0000168 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000169
170 /* Check for size specification.
171 */
wdenk874ac262003-07-24 23:38:38 +0000172 if ((size = cmd_get_data_size(argv[0], 4)) < 1)
173 return 1;
wdenk38635852002-08-27 05:55:31 +0000174
175 /* Address is specified since argc > 1
176 */
177 addr = simple_strtoul(argv[1], NULL, 16);
178 addr += base_address;
179
180 /* Get the value to write.
181 */
182 writeval = simple_strtoul(argv[2], NULL, 16);
183
184 /* Count ? */
185 if (argc == 4) {
186 count = simple_strtoul(argv[3], NULL, 16);
187 } else {
188 count = 1;
189 }
190
191 while (count-- > 0) {
192 if (size == 4)
193 *((ulong *)addr) = (ulong )writeval;
194 else if (size == 2)
195 *((ushort *)addr) = (ushort)writeval;
196 else
197 *((u_char *)addr) = (u_char)writeval;
198 addr += size;
199 }
200 return 0;
201}
202
stroesefb5e0ac2004-12-16 17:42:39 +0000203#ifdef CONFIG_MX_CYCLIC
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200204int do_mem_mdc ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
stroesefb5e0ac2004-12-16 17:42:39 +0000205{
206 int i;
207 ulong count;
208
Wolfgang Denk3b683112010-07-17 01:06:04 +0200209 if (argc < 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000210 return CMD_RET_USAGE;
stroesefb5e0ac2004-12-16 17:42:39 +0000211
212 count = simple_strtoul(argv[3], NULL, 10);
213
214 for (;;) {
215 do_mem_md (NULL, 0, 3, argv);
216
217 /* delay for <count> ms... */
218 for (i=0; i<count; i++)
219 udelay (1000);
220
221 /* check for ctrl-c to abort... */
222 if (ctrlc()) {
223 puts("Abort\n");
224 return 0;
225 }
226 }
227
228 return 0;
229}
230
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200231int do_mem_mwc ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
stroesefb5e0ac2004-12-16 17:42:39 +0000232{
233 int i;
234 ulong count;
235
Wolfgang Denk3b683112010-07-17 01:06:04 +0200236 if (argc < 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000237 return CMD_RET_USAGE;
stroesefb5e0ac2004-12-16 17:42:39 +0000238
239 count = simple_strtoul(argv[3], NULL, 10);
240
241 for (;;) {
242 do_mem_mw (NULL, 0, 3, argv);
243
244 /* delay for <count> ms... */
245 for (i=0; i<count; i++)
246 udelay (1000);
247
248 /* check for ctrl-c to abort... */
249 if (ctrlc()) {
250 puts("Abort\n");
251 return 0;
252 }
253 }
254
255 return 0;
256}
257#endif /* CONFIG_MX_CYCLIC */
258
Kim Phillipsdc00a682012-10-29 13:34:31 +0000259static int do_mem_cmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000260{
wdenk874ac262003-07-24 23:38:38 +0000261 ulong addr1, addr2, count, ngood;
262 int size;
wdenk38635852002-08-27 05:55:31 +0000263 int rcode = 0;
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000264 const char *type;
wdenk38635852002-08-27 05:55:31 +0000265
Wolfgang Denk3b683112010-07-17 01:06:04 +0200266 if (argc != 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000267 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000268
269 /* Check for size specification.
270 */
wdenk874ac262003-07-24 23:38:38 +0000271 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
272 return 1;
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000273 type = size == 4 ? "word" : size == 2 ? "halfword" : "byte";
wdenk38635852002-08-27 05:55:31 +0000274
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
wdenk381669a2003-06-16 23:50:08 +0000283#ifdef CONFIG_HAS_DATAFLASH
284 if (addr_dataflash(addr1) | addr_dataflash(addr2)){
wdenk42c05472004-03-23 22:14:11 +0000285 puts ("Comparison with DataFlash space not supported.\n\r");
wdenk381669a2003-06-16 23:50:08 +0000286 return 0;
287 }
288#endif
289
Mike Frysingere578d682008-02-04 19:26:56 -0500290#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
Mike Frysingera66afbd2012-01-20 09:07:22 +0000297 for (ngood = 0; ngood < count; ++ngood) {
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000298 ulong word1, word2;
wdenk38635852002-08-27 05:55:31 +0000299 if (size == 4) {
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000300 word1 = *(ulong *)addr1;
301 word2 = *(ulong *)addr2;
302 } else if (size == 2) {
303 word1 = *(ushort *)addr1;
304 word2 = *(ushort *)addr2;
305 } else {
306 word1 = *(u_char *)addr1;
307 word2 = *(u_char *)addr2;
wdenk38635852002-08-27 05:55:31 +0000308 }
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000309 if (word1 != word2) {
310 printf("%s at 0x%08lx (%#0*lx) != %s at 0x%08lx (%#0*lx)\n",
311 type, addr1, size, word1,
312 type, addr2, size, word2);
313 rcode = 1;
314 break;
wdenk38635852002-08-27 05:55:31 +0000315 }
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000316
wdenk38635852002-08-27 05:55:31 +0000317 addr1 += size;
318 addr2 += size;
Stefan Roesed4ef82b2010-09-13 11:10:34 +0200319
320 /* reset watchdog from time to time */
Mike Frysingera66afbd2012-01-20 09:07:22 +0000321 if ((ngood % (64 << 10)) == 0)
Stefan Roesed4ef82b2010-09-13 11:10:34 +0200322 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000323 }
324
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000325 printf("Total of %ld %s(s) were the same\n", ngood, type);
wdenk38635852002-08-27 05:55:31 +0000326 return rcode;
327}
328
Kim Phillipsdc00a682012-10-29 13:34:31 +0000329static int do_mem_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000330{
wdenk874ac262003-07-24 23:38:38 +0000331 ulong addr, dest, count;
332 int size;
wdenk38635852002-08-27 05:55:31 +0000333
Wolfgang Denk3b683112010-07-17 01:06:04 +0200334 if (argc != 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000335 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000336
337 /* Check for size specification.
338 */
wdenk874ac262003-07-24 23:38:38 +0000339 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
340 return 1;
wdenk38635852002-08-27 05:55:31 +0000341
342 addr = simple_strtoul(argv[1], NULL, 16);
343 addr += base_address;
344
345 dest = simple_strtoul(argv[2], NULL, 16);
346 dest += base_address;
347
348 count = simple_strtoul(argv[3], NULL, 16);
349
350 if (count == 0) {
351 puts ("Zero length ???\n");
352 return 1;
353 }
354
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200355#ifndef CONFIG_SYS_NO_FLASH
wdenk38635852002-08-27 05:55:31 +0000356 /* check if we are copying to Flash */
wdenk381669a2003-06-16 23:50:08 +0000357 if ( (addr2info(dest) != NULL)
358#ifdef CONFIG_HAS_DATAFLASH
Kim B. Heino1eb0fd52008-03-03 10:39:13 +0200359 && (!addr_dataflash(dest))
wdenk381669a2003-06-16 23:50:08 +0000360#endif
361 ) {
wdenk38635852002-08-27 05:55:31 +0000362 int rc;
363
wdenk42c05472004-03-23 22:14:11 +0000364 puts ("Copy to Flash... ");
wdenk38635852002-08-27 05:55:31 +0000365
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200366 rc = flash_write ((char *)addr, dest, count*size);
wdenk38635852002-08-27 05:55:31 +0000367 if (rc != 0) {
368 flash_perror (rc);
369 return (1);
370 }
371 puts ("done\n");
372 return 0;
373 }
374#endif
375
wdenk381669a2003-06-16 23:50:08 +0000376#ifdef CONFIG_HAS_DATAFLASH
377 /* Check if we are copying from RAM or Flash to DataFlash */
378 if (addr_dataflash(dest) && !addr_dataflash(addr)){
379 int rc;
380
wdenk42c05472004-03-23 22:14:11 +0000381 puts ("Copy to DataFlash... ");
wdenk381669a2003-06-16 23:50:08 +0000382
383 rc = write_dataflash (dest, addr, count*size);
384
385 if (rc != 1) {
386 dataflash_perror (rc);
387 return (1);
388 }
389 puts ("done\n");
390 return 0;
391 }
wdenk57b2d802003-06-27 21:31:46 +0000392
wdenk381669a2003-06-16 23:50:08 +0000393 /* Check if we are copying from DataFlash to RAM */
Stelian Popa63d2eb2008-03-26 22:52:35 +0100394 if (addr_dataflash(addr) && !addr_dataflash(dest)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200395#ifndef CONFIG_SYS_NO_FLASH
Stelian Popa63d2eb2008-03-26 22:52:35 +0100396 && (addr2info(dest) == NULL)
397#endif
398 ){
wdenk86765902003-12-06 23:55:10 +0000399 int rc;
400 rc = read_dataflash(addr, count * size, (char *) dest);
401 if (rc != 1) {
wdenk1ebf41e2004-01-02 14:00:00 +0000402 dataflash_perror (rc);
403 return (1);
404 }
wdenk381669a2003-06-16 23:50:08 +0000405 return 0;
406 }
407
408 if (addr_dataflash(addr) && addr_dataflash(dest)){
wdenk42c05472004-03-23 22:14:11 +0000409 puts ("Unsupported combination of source/destination.\n\r");
wdenk381669a2003-06-16 23:50:08 +0000410 return 1;
411 }
wdenk7a428cc2003-06-15 22:40:42 +0000412#endif
413
Mike Frysingere578d682008-02-04 19:26:56 -0500414#ifdef CONFIG_BLACKFIN
415 /* See if we're copying to/from L1 inst */
416 if (addr_bfin_on_chip_mem(dest) || addr_bfin_on_chip_mem(addr)) {
417 memcpy((void *)dest, (void *)addr, count * size);
418 return 0;
419 }
420#endif
421
wdenk38635852002-08-27 05:55:31 +0000422 while (count-- > 0) {
423 if (size == 4)
424 *((ulong *)dest) = *((ulong *)addr);
425 else if (size == 2)
426 *((ushort *)dest) = *((ushort *)addr);
427 else
428 *((u_char *)dest) = *((u_char *)addr);
429 addr += size;
430 dest += size;
Stefan Roesed4ef82b2010-09-13 11:10:34 +0200431
432 /* reset watchdog from time to time */
433 if ((count % (64 << 10)) == 0)
434 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000435 }
436 return 0;
437}
438
Kim Phillipsdc00a682012-10-29 13:34:31 +0000439static int do_mem_base(cmd_tbl_t *cmdtp, int flag, int argc,
440 char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000441{
442 if (argc > 1) {
443 /* Set new base address.
444 */
445 base_address = simple_strtoul(argv[1], NULL, 16);
446 }
447 /* Print the current base address.
448 */
449 printf("Base Address: 0x%08lx\n", base_address);
450 return 0;
451}
452
Kim Phillipsdc00a682012-10-29 13:34:31 +0000453static int do_mem_loop(cmd_tbl_t *cmdtp, int flag, int argc,
454 char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000455{
Marek Vasut7d335822011-09-26 02:26:06 +0200456 ulong addr, length, i;
wdenk874ac262003-07-24 23:38:38 +0000457 int size;
wdenk38635852002-08-27 05:55:31 +0000458 volatile uint *longp;
459 volatile ushort *shortp;
460 volatile u_char *cp;
461
Wolfgang Denk3b683112010-07-17 01:06:04 +0200462 if (argc < 3)
Simon Glassa06dfc72011-12-10 08:44:01 +0000463 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000464
Robert P. J. Day21c9b782013-02-03 02:29:54 +0000465 /*
466 * Check for a size specification.
wdenk38635852002-08-27 05:55:31 +0000467 * Defaults to long if no or incorrect specification.
468 */
wdenk874ac262003-07-24 23:38:38 +0000469 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
470 return 1;
wdenk38635852002-08-27 05:55:31 +0000471
472 /* Address is always specified.
473 */
474 addr = simple_strtoul(argv[1], NULL, 16);
475
476 /* Length is the number of objects, not number of bytes.
477 */
478 length = simple_strtoul(argv[2], NULL, 16);
479
480 /* We want to optimize the loops to run as fast as possible.
481 * If we have only one object, just run infinite loops.
482 */
483 if (length == 1) {
484 if (size == 4) {
485 longp = (uint *)addr;
486 for (;;)
487 i = *longp;
488 }
489 if (size == 2) {
490 shortp = (ushort *)addr;
491 for (;;)
492 i = *shortp;
493 }
494 cp = (u_char *)addr;
495 for (;;)
496 i = *cp;
497 }
498
499 if (size == 4) {
500 for (;;) {
501 longp = (uint *)addr;
502 i = length;
503 while (i-- > 0)
Marek Vasut7d335822011-09-26 02:26:06 +0200504 *longp++;
wdenk38635852002-08-27 05:55:31 +0000505 }
506 }
507 if (size == 2) {
508 for (;;) {
509 shortp = (ushort *)addr;
510 i = length;
511 while (i-- > 0)
Marek Vasut7d335822011-09-26 02:26:06 +0200512 *shortp++;
wdenk38635852002-08-27 05:55:31 +0000513 }
514 }
515 for (;;) {
516 cp = (u_char *)addr;
517 i = length;
518 while (i-- > 0)
Marek Vasut7d335822011-09-26 02:26:06 +0200519 *cp++;
wdenk38635852002-08-27 05:55:31 +0000520 }
521}
522
wdenk64519362004-07-11 17:40:54 +0000523#ifdef CONFIG_LOOPW
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200524int do_mem_loopw (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk64519362004-07-11 17:40:54 +0000525{
526 ulong addr, length, i, data;
527 int size;
528 volatile uint *longp;
529 volatile ushort *shortp;
530 volatile u_char *cp;
wdenk7dd13292004-07-11 20:04:51 +0000531
Wolfgang Denk3b683112010-07-17 01:06:04 +0200532 if (argc < 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000533 return CMD_RET_USAGE;
wdenk64519362004-07-11 17:40:54 +0000534
Robert P. J. Day21c9b782013-02-03 02:29:54 +0000535 /*
536 * Check for a size specification.
wdenk64519362004-07-11 17:40:54 +0000537 * Defaults to long if no or incorrect specification.
538 */
539 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
540 return 1;
541
542 /* Address is always specified.
543 */
544 addr = simple_strtoul(argv[1], NULL, 16);
545
546 /* Length is the number of objects, not number of bytes.
547 */
548 length = simple_strtoul(argv[2], NULL, 16);
549
550 /* data to write */
551 data = simple_strtoul(argv[3], NULL, 16);
wdenk7dd13292004-07-11 20:04:51 +0000552
wdenk64519362004-07-11 17:40:54 +0000553 /* We want to optimize the loops to run as fast as possible.
554 * If we have only one object, just run infinite loops.
555 */
556 if (length == 1) {
557 if (size == 4) {
558 longp = (uint *)addr;
559 for (;;)
560 *longp = data;
561 }
562 if (size == 2) {
563 shortp = (ushort *)addr;
564 for (;;)
565 *shortp = data;
566 }
567 cp = (u_char *)addr;
568 for (;;)
569 *cp = data;
570 }
571
572 if (size == 4) {
573 for (;;) {
574 longp = (uint *)addr;
575 i = length;
576 while (i-- > 0)
577 *longp++ = data;
578 }
579 }
580 if (size == 2) {
581 for (;;) {
582 shortp = (ushort *)addr;
583 i = length;
584 while (i-- > 0)
585 *shortp++ = data;
586 }
587 }
588 for (;;) {
589 cp = (u_char *)addr;
590 i = length;
591 while (i-- > 0)
592 *cp++ = data;
593 }
594}
595#endif /* CONFIG_LOOPW */
596
wdenk38635852002-08-27 05:55:31 +0000597/*
598 * Perform a memory test. A more complete alternative test can be
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200599 * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
wdenk38635852002-08-27 05:55:31 +0000600 * interrupted by ctrl-c or by a failure of one of the sub-tests.
601 */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000602static int do_mem_mtest(cmd_tbl_t *cmdtp, int flag, int argc,
603 char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000604{
605 vu_long *addr, *start, *end;
606 ulong val;
607 ulong readback;
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400608 ulong errs = 0;
Dirk Eibachb2659892008-12-16 14:51:56 +0100609 int iterations = 1;
610 int iteration_limit;
wdenk38635852002-08-27 05:55:31 +0000611
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200612#if defined(CONFIG_SYS_ALT_MEMTEST)
Guennadi Liakhovetskid4e3fb42008-02-08 21:25:58 +0100613 vu_long len;
wdenk38635852002-08-27 05:55:31 +0000614 vu_long offset;
615 vu_long test_offset;
616 vu_long pattern;
617 vu_long temp;
618 vu_long anti_pattern;
619 vu_long num_words;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200620#if defined(CONFIG_SYS_MEMTEST_SCRATCH)
621 vu_long *dummy = (vu_long*)CONFIG_SYS_MEMTEST_SCRATCH;
wdenk5958f4a2003-09-18 09:21:33 +0000622#else
Kim Phillipsdc00a682012-10-29 13:34:31 +0000623 vu_long *dummy = NULL; /* yes, this is address 0x0, not NULL */
wdenk5958f4a2003-09-18 09:21:33 +0000624#endif
wdenk38635852002-08-27 05:55:31 +0000625 int j;
wdenk38635852002-08-27 05:55:31 +0000626
627 static const ulong bitpattern[] = {
628 0x00000001, /* single bit */
629 0x00000003, /* two adjacent bits */
630 0x00000007, /* three adjacent bits */
631 0x0000000F, /* four adjacent bits */
632 0x00000005, /* two non-adjacent bits */
633 0x00000015, /* three non-adjacent bits */
634 0x00000055, /* four non-adjacent bits */
635 0xaaaaaaaa, /* alternating 1/0 */
636 };
637#else
638 ulong incr;
639 ulong pattern;
wdenk38635852002-08-27 05:55:31 +0000640#endif
641
Dirk Eibachb2659892008-12-16 14:51:56 +0100642 if (argc > 1)
wdenk38635852002-08-27 05:55:31 +0000643 start = (ulong *)simple_strtoul(argv[1], NULL, 16);
Dirk Eibachb2659892008-12-16 14:51:56 +0100644 else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200645 start = (ulong *)CONFIG_SYS_MEMTEST_START;
wdenk38635852002-08-27 05:55:31 +0000646
Dirk Eibachb2659892008-12-16 14:51:56 +0100647 if (argc > 2)
wdenk38635852002-08-27 05:55:31 +0000648 end = (ulong *)simple_strtoul(argv[2], NULL, 16);
Dirk Eibachb2659892008-12-16 14:51:56 +0100649 else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200650 end = (ulong *)(CONFIG_SYS_MEMTEST_END);
wdenk38635852002-08-27 05:55:31 +0000651
Dirk Eibachb2659892008-12-16 14:51:56 +0100652 if (argc > 3)
wdenk38635852002-08-27 05:55:31 +0000653 pattern = (ulong)simple_strtoul(argv[3], NULL, 16);
Dirk Eibachb2659892008-12-16 14:51:56 +0100654 else
wdenk38635852002-08-27 05:55:31 +0000655 pattern = 0;
Dirk Eibachb2659892008-12-16 14:51:56 +0100656
657 if (argc > 4)
658 iteration_limit = (ulong)simple_strtoul(argv[4], NULL, 16);
659 else
660 iteration_limit = 0;
wdenk38635852002-08-27 05:55:31 +0000661
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200662#if defined(CONFIG_SYS_ALT_MEMTEST)
wdenk38635852002-08-27 05:55:31 +0000663 printf ("Testing %08x ... %08x:\n", (uint)start, (uint)end);
Mike Frysinger3afa8032012-01-20 09:07:20 +0000664 debug("%s:%d: start 0x%p end 0x%p\n",
wdenk38635852002-08-27 05:55:31 +0000665 __FUNCTION__, __LINE__, start, end);
666
667 for (;;) {
668 if (ctrlc()) {
669 putc ('\n');
670 return 1;
671 }
672
Dirk Eibachb2659892008-12-16 14:51:56 +0100673
674 if (iteration_limit && iterations > iteration_limit) {
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400675 printf("Tested %d iteration(s) with %lu errors.\n",
676 iterations-1, errs);
677 return errs != 0;
Dirk Eibachb2659892008-12-16 14:51:56 +0100678 }
679
wdenk38635852002-08-27 05:55:31 +0000680 printf("Iteration: %6d\r", iterations);
Mike Frysinger3afa8032012-01-20 09:07:20 +0000681 debug("\n");
wdenk38635852002-08-27 05:55:31 +0000682 iterations++;
683
684 /*
685 * Data line test: write a pattern to the first
686 * location, write the 1's complement to a 'parking'
687 * address (changes the state of the data bus so a
Robert P. J. Day21c9b782013-02-03 02:29:54 +0000688 * floating bus doesn't give a false OK), and then
wdenk38635852002-08-27 05:55:31 +0000689 * read the value back. Note that we read it back
690 * into a variable because the next time we read it,
691 * it might be right (been there, tough to explain to
692 * the quality guys why it prints a failure when the
693 * "is" and "should be" are obviously the same in the
694 * error message).
695 *
696 * Rather than exhaustively testing, we test some
697 * patterns by shifting '1' bits through a field of
698 * '0's and '0' bits through a field of '1's (i.e.
699 * pattern and ~pattern).
700 */
701 addr = start;
702 for (j = 0; j < sizeof(bitpattern)/sizeof(bitpattern[0]); j++) {
703 val = bitpattern[j];
704 for(; val != 0; val <<= 1) {
705 *addr = val;
706 *dummy = ~val; /* clear the test data off of the bus */
707 readback = *addr;
708 if(readback != val) {
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400709 printf ("FAILURE (data line): "
wdenk38635852002-08-27 05:55:31 +0000710 "expected %08lx, actual %08lx\n",
711 val, readback);
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400712 errs++;
713 if (ctrlc()) {
714 putc ('\n');
715 return 1;
716 }
wdenk38635852002-08-27 05:55:31 +0000717 }
718 *addr = ~val;
719 *dummy = val;
720 readback = *addr;
721 if(readback != ~val) {
722 printf ("FAILURE (data line): "
723 "Is %08lx, should be %08lx\n",
wdenk391b5742004-10-10 23:27:33 +0000724 readback, ~val);
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400725 errs++;
726 if (ctrlc()) {
727 putc ('\n');
728 return 1;
729 }
wdenk38635852002-08-27 05:55:31 +0000730 }
731 }
732 }
733
734 /*
735 * Based on code whose Original Author and Copyright
736 * information follows: Copyright (c) 1998 by Michael
737 * Barr. This software is placed into the public
738 * domain and may be used for any purpose. However,
739 * this notice must not be changed or removed and no
740 * warranty is either expressed or implied by its
741 * publication or distribution.
742 */
743
744 /*
745 * Address line test
746 *
747 * Description: Test the address bus wiring in a
748 * memory region by performing a walking
749 * 1's test on the relevant bits of the
750 * address and checking for aliasing.
751 * This test will find single-bit
Robert P. J. Day21c9b782013-02-03 02:29:54 +0000752 * address failures such as stuck-high,
wdenk38635852002-08-27 05:55:31 +0000753 * stuck-low, and shorted pins. The base
754 * address and size of the region are
755 * selected by the caller.
756 *
757 * Notes: For best results, the selected base
758 * address should have enough LSB 0's to
759 * guarantee single address bit changes.
760 * For example, to test a 64-Kbyte
761 * region, select a base address on a
762 * 64-Kbyte boundary. Also, select the
763 * region size as a power-of-two if at
764 * all possible.
765 *
766 * Returns: 0 if the test succeeds, 1 if the test fails.
wdenk38635852002-08-27 05:55:31 +0000767 */
Guennadi Liakhovetskid4e3fb42008-02-08 21:25:58 +0100768 len = ((ulong)end - (ulong)start)/sizeof(vu_long);
wdenk38635852002-08-27 05:55:31 +0000769 pattern = (vu_long) 0xaaaaaaaa;
770 anti_pattern = (vu_long) 0x55555555;
771
Mike Frysinger3afa8032012-01-20 09:07:20 +0000772 debug("%s:%d: length = 0x%.8lx\n",
wdenk38635852002-08-27 05:55:31 +0000773 __FUNCTION__, __LINE__,
Guennadi Liakhovetskid4e3fb42008-02-08 21:25:58 +0100774 len);
wdenk38635852002-08-27 05:55:31 +0000775 /*
776 * Write the default pattern at each of the
777 * power-of-two offsets.
778 */
Guennadi Liakhovetskid4e3fb42008-02-08 21:25:58 +0100779 for (offset = 1; offset < len; offset <<= 1) {
wdenk38635852002-08-27 05:55:31 +0000780 start[offset] = pattern;
781 }
782
783 /*
784 * Check for address bits stuck high.
785 */
786 test_offset = 0;
787 start[test_offset] = anti_pattern;
788
Guennadi Liakhovetskid4e3fb42008-02-08 21:25:58 +0100789 for (offset = 1; offset < len; offset <<= 1) {
wdenk38635852002-08-27 05:55:31 +0000790 temp = start[offset];
791 if (temp != pattern) {
792 printf ("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
793 " expected 0x%.8lx, actual 0x%.8lx\n",
794 (ulong)&start[offset], pattern, temp);
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400795 errs++;
796 if (ctrlc()) {
797 putc ('\n');
798 return 1;
799 }
wdenk38635852002-08-27 05:55:31 +0000800 }
801 }
802 start[test_offset] = pattern;
Sergei Poselenov474dbb82008-04-09 16:09:41 +0200803 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000804
805 /*
806 * Check for addr bits stuck low or shorted.
807 */
Guennadi Liakhovetskid4e3fb42008-02-08 21:25:58 +0100808 for (test_offset = 1; test_offset < len; test_offset <<= 1) {
wdenk38635852002-08-27 05:55:31 +0000809 start[test_offset] = anti_pattern;
810
Guennadi Liakhovetskid4e3fb42008-02-08 21:25:58 +0100811 for (offset = 1; offset < len; offset <<= 1) {
wdenk38635852002-08-27 05:55:31 +0000812 temp = start[offset];
813 if ((temp != pattern) && (offset != test_offset)) {
814 printf ("\nFAILURE: Address bit stuck low or shorted @"
815 " 0x%.8lx: expected 0x%.8lx, actual 0x%.8lx\n",
816 (ulong)&start[offset], pattern, temp);
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400817 errs++;
818 if (ctrlc()) {
819 putc ('\n');
820 return 1;
821 }
wdenk38635852002-08-27 05:55:31 +0000822 }
823 }
824 start[test_offset] = pattern;
825 }
826
827 /*
828 * Description: Test the integrity of a physical
829 * memory device by performing an
830 * increment/decrement test over the
831 * entire region. In the process every
832 * storage bit in the device is tested
833 * as a zero and a one. The base address
834 * and the size of the region are
835 * selected by the caller.
836 *
837 * Returns: 0 if the test succeeds, 1 if the test fails.
838 */
839 num_words = ((ulong)end - (ulong)start)/sizeof(vu_long) + 1;
840
841 /*
842 * Fill memory with a known pattern.
843 */
844 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
Sergei Poselenov474dbb82008-04-09 16:09:41 +0200845 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000846 start[offset] = pattern;
847 }
848
849 /*
850 * Check each location and invert it for the second pass.
851 */
852 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
Sergei Poselenov474dbb82008-04-09 16:09:41 +0200853 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000854 temp = start[offset];
855 if (temp != pattern) {
856 printf ("\nFAILURE (read/write) @ 0x%.8lx:"
857 " expected 0x%.8lx, actual 0x%.8lx)\n",
858 (ulong)&start[offset], pattern, temp);
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400859 errs++;
860 if (ctrlc()) {
861 putc ('\n');
862 return 1;
863 }
wdenk38635852002-08-27 05:55:31 +0000864 }
865
866 anti_pattern = ~pattern;
867 start[offset] = anti_pattern;
868 }
869
870 /*
871 * Check each location for the inverted pattern and zero it.
872 */
873 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
Sergei Poselenov474dbb82008-04-09 16:09:41 +0200874 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000875 anti_pattern = ~pattern;
876 temp = start[offset];
877 if (temp != anti_pattern) {
878 printf ("\nFAILURE (read/write): @ 0x%.8lx:"
879 " expected 0x%.8lx, actual 0x%.8lx)\n",
880 (ulong)&start[offset], anti_pattern, temp);
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400881 errs++;
882 if (ctrlc()) {
883 putc ('\n');
884 return 1;
885 }
wdenk38635852002-08-27 05:55:31 +0000886 }
887 start[offset] = 0;
888 }
889 }
890
891#else /* The original, quickie test */
892 incr = 1;
893 for (;;) {
894 if (ctrlc()) {
895 putc ('\n');
896 return 1;
897 }
Dirk Eibachb2659892008-12-16 14:51:56 +0100898
899 if (iteration_limit && iterations > iteration_limit) {
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400900 printf("Tested %d iteration(s) with %lu errors.\n",
901 iterations-1, errs);
902 return errs != 0;
Dirk Eibachb2659892008-12-16 14:51:56 +0100903 }
904 ++iterations;
wdenk38635852002-08-27 05:55:31 +0000905
906 printf ("\rPattern %08lX Writing..."
907 "%12s"
908 "\b\b\b\b\b\b\b\b\b\b",
909 pattern, "");
910
911 for (addr=start,val=pattern; addr<end; addr++) {
Sergei Poselenov474dbb82008-04-09 16:09:41 +0200912 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000913 *addr = val;
914 val += incr;
915 }
916
wdenk42c05472004-03-23 22:14:11 +0000917 puts ("Reading...");
wdenk38635852002-08-27 05:55:31 +0000918
919 for (addr=start,val=pattern; addr<end; addr++) {
Sergei Poselenov474dbb82008-04-09 16:09:41 +0200920 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000921 readback = *addr;
922 if (readback != val) {
923 printf ("\nMem error @ 0x%08X: "
924 "found %08lX, expected %08lX\n",
Simon Glassfc953242011-09-17 06:48:58 +0000925 (uint)(uintptr_t)addr, readback, val);
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400926 errs++;
927 if (ctrlc()) {
928 putc ('\n');
929 return 1;
930 }
wdenk38635852002-08-27 05:55:31 +0000931 }
932 val += incr;
933 }
934
935 /*
936 * Flip the pattern each time to make lots of zeros and
937 * then, the next time, lots of ones. We decrement
938 * the "negative" patterns and increment the "positive"
939 * patterns to preserve this feature.
940 */
941 if(pattern & 0x80000000) {
942 pattern = -pattern; /* complement & increment */
943 }
944 else {
945 pattern = ~pattern;
946 }
947 incr = -incr;
948 }
wdenk38635852002-08-27 05:55:31 +0000949#endif
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400950 return 0; /* not reached */
wdenk38635852002-08-27 05:55:31 +0000951}
952
953
954/* Modify memory.
955 *
956 * Syntax:
957 * mm{.b, .w, .l} {addr}
958 * nm{.b, .w, .l} {addr}
959 */
960static int
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200961mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000962{
wdenk874ac262003-07-24 23:38:38 +0000963 ulong addr, i;
964 int nbytes, size;
wdenk38635852002-08-27 05:55:31 +0000965
Wolfgang Denk3b683112010-07-17 01:06:04 +0200966 if (argc != 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000967 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000968
969#ifdef CONFIG_BOOT_RETRY_TIME
970 reset_cmd_timeout(); /* got a good command to get here */
971#endif
972 /* We use the last specified parameters, unless new ones are
973 * entered.
974 */
975 addr = mm_last_addr;
976 size = mm_last_size;
977
978 if ((flag & CMD_FLAG_REPEAT) == 0) {
979 /* New command specified. Check for a size specification.
980 * Defaults to long if no or incorrect specification.
981 */
wdenk874ac262003-07-24 23:38:38 +0000982 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
983 return 1;
wdenk38635852002-08-27 05:55:31 +0000984
985 /* Address is specified since argc > 1
986 */
987 addr = simple_strtoul(argv[1], NULL, 16);
988 addr += base_address;
989 }
990
wdenk381669a2003-06-16 23:50:08 +0000991#ifdef CONFIG_HAS_DATAFLASH
992 if (addr_dataflash(addr)){
wdenk42c05472004-03-23 22:14:11 +0000993 puts ("Can't modify DataFlash in place. Use cp instead.\n\r");
wdenk381669a2003-06-16 23:50:08 +0000994 return 0;
995 }
996#endif
997
Mike Frysingere578d682008-02-04 19:26:56 -0500998#ifdef CONFIG_BLACKFIN
999 if (addr_bfin_on_chip_mem(addr)) {
1000 puts ("Can't modify L1 instruction in place. Use cp instead.\n\r");
1001 return 0;
1002 }
1003#endif
1004
wdenk38635852002-08-27 05:55:31 +00001005 /* Print the address, followed by value. Then accept input for
1006 * the next value. A non-converted value exits.
1007 */
1008 do {
1009 printf("%08lx:", addr);
1010 if (size == 4)
1011 printf(" %08x", *((uint *)addr));
1012 else if (size == 2)
1013 printf(" %04x", *((ushort *)addr));
1014 else
1015 printf(" %02x", *((u_char *)addr));
1016
1017 nbytes = readline (" ? ");
1018 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
1019 /* <CR> pressed as only input, don't modify current
1020 * location and move to next. "-" pressed will go back.
1021 */
1022 if (incrflag)
1023 addr += nbytes ? -size : size;
1024 nbytes = 1;
1025#ifdef CONFIG_BOOT_RETRY_TIME
1026 reset_cmd_timeout(); /* good enough to not time out */
1027#endif
1028 }
1029#ifdef CONFIG_BOOT_RETRY_TIME
1030 else if (nbytes == -2) {
1031 break; /* timed out, exit the command */
1032 }
1033#endif
1034 else {
1035 char *endp;
1036 i = simple_strtoul(console_buffer, &endp, 16);
1037 nbytes = endp - console_buffer;
1038 if (nbytes) {
1039#ifdef CONFIG_BOOT_RETRY_TIME
1040 /* good enough to not time out
1041 */
1042 reset_cmd_timeout();
1043#endif
1044 if (size == 4)
1045 *((uint *)addr) = i;
1046 else if (size == 2)
1047 *((ushort *)addr) = i;
1048 else
1049 *((u_char *)addr) = i;
1050 if (incrflag)
1051 addr += size;
1052 }
1053 }
1054 } while (nbytes);
1055
1056 mm_last_addr = addr;
1057 mm_last_size = size;
1058 return 0;
1059}
1060
Mike Frysinger321ab9e2010-12-21 14:19:51 -05001061#ifdef CONFIG_CMD_CRC32
1062
wdenk6203e402004-04-18 10:13:26 +00001063#ifndef CONFIG_CRC32_VERIFY
1064
Kim Phillipsdc00a682012-10-29 13:34:31 +00001065static int do_mem_crc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +00001066{
wdenk7a428cc2003-06-15 22:40:42 +00001067 ulong addr, length;
1068 ulong crc;
1069 ulong *ptr;
wdenk38635852002-08-27 05:55:31 +00001070
Wolfgang Denk3b683112010-07-17 01:06:04 +02001071 if (argc < 3)
Simon Glassa06dfc72011-12-10 08:44:01 +00001072 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +00001073
wdenk7a428cc2003-06-15 22:40:42 +00001074 addr = simple_strtoul (argv[1], NULL, 16);
wdenk38635852002-08-27 05:55:31 +00001075 addr += base_address;
1076
wdenk7a428cc2003-06-15 22:40:42 +00001077 length = simple_strtoul (argv[2], NULL, 16);
wdenk38635852002-08-27 05:55:31 +00001078
Jens Scharsig5b2e6c22011-07-18 08:46:26 +02001079 crc = crc32_wd (0, (const uchar *) addr, length, CHUNKSZ_CRC32);
wdenk38635852002-08-27 05:55:31 +00001080
1081 printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
wdenk7a428cc2003-06-15 22:40:42 +00001082 addr, addr + length - 1, crc);
wdenk38635852002-08-27 05:55:31 +00001083
wdenk7a428cc2003-06-15 22:40:42 +00001084 if (argc > 3) {
1085 ptr = (ulong *) simple_strtoul (argv[3], NULL, 16);
1086 *ptr = crc;
1087 }
wdenk38635852002-08-27 05:55:31 +00001088
1089 return 0;
1090}
1091
wdenk6203e402004-04-18 10:13:26 +00001092#else /* CONFIG_CRC32_VERIFY */
1093
Wolfgang Denk6262d0212010-06-28 22:00:46 +02001094int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk6203e402004-04-18 10:13:26 +00001095{
1096 ulong addr, length;
1097 ulong crc;
1098 ulong *ptr;
wdenk05939202004-04-18 17:39:38 +00001099 ulong vcrc;
wdenk6203e402004-04-18 10:13:26 +00001100 int verify;
1101 int ac;
Wolfgang Denk6262d0212010-06-28 22:00:46 +02001102 char * const *av;
wdenk6203e402004-04-18 10:13:26 +00001103
1104 if (argc < 3) {
Wolfgang Denk3b683112010-07-17 01:06:04 +02001105usage:
Simon Glassa06dfc72011-12-10 08:44:01 +00001106 return CMD_RET_USAGE;
wdenk6203e402004-04-18 10:13:26 +00001107 }
1108
1109 av = argv + 1;
1110 ac = argc - 1;
1111 if (strcmp(*av, "-v") == 0) {
1112 verify = 1;
1113 av++;
1114 ac--;
1115 if (ac < 3)
1116 goto usage;
1117 } else
1118 verify = 0;
1119
1120 addr = simple_strtoul(*av++, NULL, 16);
1121 addr += base_address;
1122 length = simple_strtoul(*av++, NULL, 16);
1123
Jens Scharsig5b2e6c22011-07-18 08:46:26 +02001124 crc = crc32_wd (0, (const uchar *) addr, length, CHUNKSZ_CRC32);
wdenk6203e402004-04-18 10:13:26 +00001125
1126 if (!verify) {
1127 printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
1128 addr, addr + length - 1, crc);
1129 if (ac > 2) {
1130 ptr = (ulong *) simple_strtoul (*av++, NULL, 16);
1131 *ptr = crc;
1132 }
1133 } else {
1134 vcrc = simple_strtoul(*av++, NULL, 16);
1135 if (vcrc != crc) {
1136 printf ("CRC32 for %08lx ... %08lx ==> %08lx != %08lx ** ERROR **\n",
1137 addr, addr + length - 1, crc, vcrc);
1138 return 1;
1139 }
1140 }
1141
1142 return 0;
1143
1144}
1145#endif /* CONFIG_CRC32_VERIFY */
1146
Robin Getz93d6cb02009-07-27 00:07:59 -04001147#endif
1148
wdenk57b2d802003-06-27 21:31:46 +00001149/**************************************************/
wdenkf287a242003-07-01 21:06:45 +00001150U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001151 md, 3, 1, do_mem_md,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001152 "memory display",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001153 "[.b, .w, .l] address [# of objects]"
wdenk57b2d802003-06-27 21:31:46 +00001154);
1155
1156
wdenkf287a242003-07-01 21:06:45 +00001157U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001158 mm, 2, 1, do_mem_mm,
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001159 "memory modify (auto-incrementing address)",
1160 "[.b, .w, .l] address"
wdenk57b2d802003-06-27 21:31:46 +00001161);
1162
1163
wdenkf287a242003-07-01 21:06:45 +00001164U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001165 nm, 2, 1, do_mem_nm,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001166 "memory modify (constant address)",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001167 "[.b, .w, .l] address"
wdenk57b2d802003-06-27 21:31:46 +00001168);
1169
wdenkf287a242003-07-01 21:06:45 +00001170U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001171 mw, 4, 1, do_mem_mw,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001172 "memory write (fill)",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001173 "[.b, .w, .l] address value [count]"
wdenk57b2d802003-06-27 21:31:46 +00001174);
1175
wdenkf287a242003-07-01 21:06:45 +00001176U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001177 cp, 4, 1, do_mem_cp,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001178 "memory copy",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001179 "[.b, .w, .l] source target count"
wdenk57b2d802003-06-27 21:31:46 +00001180);
1181
wdenkf287a242003-07-01 21:06:45 +00001182U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001183 cmp, 4, 1, do_mem_cmp,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001184 "memory compare",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001185 "[.b, .w, .l] addr1 addr2 count"
wdenk57b2d802003-06-27 21:31:46 +00001186);
1187
Mike Frysinger321ab9e2010-12-21 14:19:51 -05001188#ifdef CONFIG_CMD_CRC32
1189
wdenk6203e402004-04-18 10:13:26 +00001190#ifndef CONFIG_CRC32_VERIFY
1191
wdenkf287a242003-07-01 21:06:45 +00001192U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001193 crc32, 4, 1, do_mem_crc,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001194 "checksum calculation",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001195 "address count [addr]\n - compute CRC32 checksum [save at addr]"
wdenk57b2d802003-06-27 21:31:46 +00001196);
1197
wdenk6203e402004-04-18 10:13:26 +00001198#else /* CONFIG_CRC32_VERIFY */
1199
1200U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001201 crc32, 5, 1, do_mem_crc,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001202 "checksum calculation",
wdenk6203e402004-04-18 10:13:26 +00001203 "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001204 "-v address count crc\n - verify crc of memory area"
wdenk6203e402004-04-18 10:13:26 +00001205);
1206
1207#endif /* CONFIG_CRC32_VERIFY */
1208
Mike Frysinger321ab9e2010-12-21 14:19:51 -05001209#endif
1210
Simon Glasseacd14f2012-11-30 13:01:20 +00001211#ifdef CONFIG_CMD_MEMINFO
1212__weak void board_show_dram(ulong size)
1213{
1214 puts("DRAM: ");
1215 print_size(size, "\n");
1216}
1217
1218static int do_mem_info(cmd_tbl_t *cmdtp, int flag, int argc,
1219 char * const argv[])
1220{
1221 board_show_dram(gd->ram_size);
1222
1223 return 0;
1224}
1225#endif
1226
wdenkf287a242003-07-01 21:06:45 +00001227U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001228 base, 2, 1, do_mem_base,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001229 "print or set address offset",
wdenk57b2d802003-06-27 21:31:46 +00001230 "\n - print address offset for memory commands\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001231 "base off\n - set address offset for memory commands to 'off'"
wdenk57b2d802003-06-27 21:31:46 +00001232);
1233
wdenkf287a242003-07-01 21:06:45 +00001234U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001235 loop, 3, 1, do_mem_loop,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001236 "infinite loop on address range",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001237 "[.b, .w, .l] address number_of_objects"
wdenk57b2d802003-06-27 21:31:46 +00001238);
wdenk64519362004-07-11 17:40:54 +00001239
1240#ifdef CONFIG_LOOPW
1241U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001242 loopw, 4, 1, do_mem_loopw,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001243 "infinite write loop on address range",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001244 "[.b, .w, .l] address number_of_objects data_to_write"
wdenk64519362004-07-11 17:40:54 +00001245);
1246#endif /* CONFIG_LOOPW */
wdenk57b2d802003-06-27 21:31:46 +00001247
wdenkf287a242003-07-01 21:06:45 +00001248U_BOOT_CMD(
Dirk Eibachb2659892008-12-16 14:51:56 +01001249 mtest, 5, 1, do_mem_mtest,
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001250 "simple RAM read/write test",
1251 "[start [end [pattern [iterations]]]]"
wdenk57b2d802003-06-27 21:31:46 +00001252);
stroesefb5e0ac2004-12-16 17:42:39 +00001253
1254#ifdef CONFIG_MX_CYCLIC
1255U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001256 mdc, 4, 1, do_mem_mdc,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001257 "memory display cyclic",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001258 "[.b, .w, .l] address count delay(ms)"
stroesefb5e0ac2004-12-16 17:42:39 +00001259);
1260
1261U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001262 mwc, 4, 1, do_mem_mwc,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001263 "memory write cyclic",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001264 "[.b, .w, .l] address value delay(ms)"
stroesefb5e0ac2004-12-16 17:42:39 +00001265);
1266#endif /* CONFIG_MX_CYCLIC */
Simon Glasseacd14f2012-11-30 13:01:20 +00001267
1268#ifdef CONFIG_CMD_MEMINFO
1269U_BOOT_CMD(
1270 meminfo, 3, 1, do_mem_info,
1271 "display memory information",
1272 ""
1273);
1274#endif