blob: 5235bfe5812cca0dae00ae2108aa8aa61d256e7c [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>
wdenk2abbe072003-06-16 23:50:08 +000032#ifdef CONFIG_HAS_DATAFLASH
33#include <dataflash.h>
34#endif
Sergei Poselenova6e6fc62008-04-09 16:09:41 +020035#include <watchdog.h>
Simon Glass0628ab82013-02-24 17:33:15 +000036#include <asm/io.h>
Simon Glass15a33e42012-11-30 13:01:20 +000037#include <linux/compiler.h>
38
39DECLARE_GLOBAL_DATA_PTR;
wdenk38635852002-08-27 05:55:31 +000040
Wolfgang Denk54841ab2010-06-28 22:00:46 +020041static int mod_mem(cmd_tbl_t *, int, int, int, char * const []);
wdenk38635852002-08-27 05:55:31 +000042
43/* Display values from last command.
44 * Memory modify remembered values are different from display memory.
45 */
Mike Frysingerd6efe242010-12-22 09:40:29 -050046static uint dp_last_addr, dp_last_size;
47static uint dp_last_length = 0x40;
48static uint mm_last_addr, mm_last_size;
wdenk38635852002-08-27 05:55:31 +000049
50static ulong base_address = 0;
51
52/* Memory Display
53 *
54 * Syntax:
55 * md{.b, .w, .l} {addr} {len}
56 */
57#define DISP_LINE_LEN 16
Kim Phillips088f1b12012-10-29 13:34:31 +000058static int do_mem_md(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000059{
wdenk27b207f2003-07-24 23:38:38 +000060 ulong addr, length;
Grant Likelyc95c4282007-02-20 09:05:00 +010061#if defined(CONFIG_HAS_DATAFLASH)
62 ulong nbytes, linebytes;
63#endif
wdenk27b207f2003-07-24 23:38:38 +000064 int size;
wdenk38635852002-08-27 05:55:31 +000065 int rc = 0;
66
67 /* We use the last specified parameters, unless new ones are
68 * entered.
69 */
70 addr = dp_last_addr;
71 size = dp_last_size;
72 length = dp_last_length;
73
Wolfgang Denk47e26b12010-07-17 01:06:04 +020074 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +000075 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +000076
77 if ((flag & CMD_FLAG_REPEAT) == 0) {
78 /* New command specified. Check for a size specification.
79 * Defaults to long if no or incorrect specification.
80 */
wdenk27b207f2003-07-24 23:38:38 +000081 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
82 return 1;
wdenk38635852002-08-27 05:55:31 +000083
84 /* Address is specified since argc > 1
85 */
86 addr = simple_strtoul(argv[1], NULL, 16);
87 addr += base_address;
88
89 /* If another parameter, it is the length to display.
90 * Length is the number of objects, not number of bytes.
91 */
92 if (argc > 2)
93 length = simple_strtoul(argv[2], NULL, 16);
94 }
95
Grant Likelyc95c4282007-02-20 09:05:00 +010096#if defined(CONFIG_HAS_DATAFLASH)
wdenk38635852002-08-27 05:55:31 +000097 /* Print the lines.
98 *
99 * We buffer all read data, so we can make sure data is read only
100 * once, and all accesses are with the specified bus width.
101 */
102 nbytes = length * size;
103 do {
104 char linebuf[DISP_LINE_LEN];
Grant Likelyc95c4282007-02-20 09:05:00 +0100105 void* p;
wdenk38635852002-08-27 05:55:31 +0000106 linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
wdenk2abbe072003-06-16 23:50:08 +0000107
Grant Likelyc95c4282007-02-20 09:05:00 +0100108 rc = read_dataflash(addr, (linebytes/size)*size, linebuf);
109 p = (rc == DATAFLASH_OK) ? linebuf : (void*)addr;
110 print_buffer(addr, p, size, linebytes/size, DISP_LINE_LEN/size);
wdenk8bde7f72003-06-27 21:31:46 +0000111
wdenk38635852002-08-27 05:55:31 +0000112 nbytes -= linebytes;
Grant Likelyc95c4282007-02-20 09:05:00 +0100113 addr += linebytes;
wdenk38635852002-08-27 05:55:31 +0000114 if (ctrlc()) {
115 rc = 1;
116 break;
117 }
118 } while (nbytes > 0);
Grant Likelyc95c4282007-02-20 09:05:00 +0100119#else
Mike Frysinger4c727c72008-02-04 19:26:56 -0500120
121# if defined(CONFIG_BLACKFIN)
122 /* See if we're trying to display L1 inst */
123 if (addr_bfin_on_chip_mem(addr)) {
124 char linebuf[DISP_LINE_LEN];
125 ulong linebytes, nbytes = length * size;
126 do {
127 linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
128 memcpy(linebuf, (void *)addr, linebytes);
129 print_buffer(addr, linebuf, size, linebytes/size, DISP_LINE_LEN/size);
130
131 nbytes -= linebytes;
132 addr += linebytes;
133 if (ctrlc()) {
134 rc = 1;
135 break;
136 }
137 } while (nbytes > 0);
138 } else
139# endif
140
141 {
Simon Glass0628ab82013-02-24 17:33:15 +0000142 ulong bytes = size * length;
143 const void *buf = map_sysmem(addr, bytes);
144
Mike Frysinger4c727c72008-02-04 19:26:56 -0500145 /* Print the lines. */
Simon Glass0628ab82013-02-24 17:33:15 +0000146 print_buffer(addr, buf, size, length, DISP_LINE_LEN / size);
147 addr += bytes;
148 unmap_sysmem(buf);
Mike Frysinger4c727c72008-02-04 19:26:56 -0500149 }
Grant Likelyc95c4282007-02-20 09:05:00 +0100150#endif
wdenk38635852002-08-27 05:55:31 +0000151
152 dp_last_addr = addr;
153 dp_last_length = length;
154 dp_last_size = size;
155 return (rc);
156}
157
Kim Phillips088f1b12012-10-29 13:34:31 +0000158static int do_mem_mm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000159{
160 return mod_mem (cmdtp, 1, flag, argc, argv);
161}
Kim Phillips088f1b12012-10-29 13:34:31 +0000162static int do_mem_nm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000163{
164 return mod_mem (cmdtp, 0, flag, argc, argv);
165}
166
Kim Phillips088f1b12012-10-29 13:34:31 +0000167static int do_mem_mw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000168{
wdenk27b207f2003-07-24 23:38:38 +0000169 ulong addr, writeval, count;
170 int size;
Simon Glass0628ab82013-02-24 17:33:15 +0000171 void *buf;
172 ulong bytes;
wdenk38635852002-08-27 05:55:31 +0000173
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200174 if ((argc < 3) || (argc > 4))
Simon Glass4c12eeb2011-12-10 08:44:01 +0000175 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000176
177 /* Check for size specification.
178 */
wdenk27b207f2003-07-24 23:38:38 +0000179 if ((size = cmd_get_data_size(argv[0], 4)) < 1)
180 return 1;
wdenk38635852002-08-27 05:55:31 +0000181
182 /* Address is specified since argc > 1
183 */
184 addr = simple_strtoul(argv[1], NULL, 16);
185 addr += base_address;
186
187 /* Get the value to write.
188 */
189 writeval = simple_strtoul(argv[2], NULL, 16);
190
191 /* Count ? */
192 if (argc == 4) {
193 count = simple_strtoul(argv[3], NULL, 16);
194 } else {
195 count = 1;
196 }
197
Simon Glass0628ab82013-02-24 17:33:15 +0000198 bytes = size * count;
199 buf = map_sysmem(addr, bytes);
wdenk38635852002-08-27 05:55:31 +0000200 while (count-- > 0) {
201 if (size == 4)
Simon Glass0628ab82013-02-24 17:33:15 +0000202 *((ulong *)buf) = (ulong)writeval;
wdenk38635852002-08-27 05:55:31 +0000203 else if (size == 2)
Simon Glass0628ab82013-02-24 17:33:15 +0000204 *((ushort *)buf) = (ushort)writeval;
wdenk38635852002-08-27 05:55:31 +0000205 else
Simon Glass0628ab82013-02-24 17:33:15 +0000206 *((u_char *)buf) = (u_char)writeval;
207 buf += size;
wdenk38635852002-08-27 05:55:31 +0000208 }
Simon Glass0628ab82013-02-24 17:33:15 +0000209 unmap_sysmem(buf);
wdenk38635852002-08-27 05:55:31 +0000210 return 0;
211}
212
stroese4aaf29b2004-12-16 17:42:39 +0000213#ifdef CONFIG_MX_CYCLIC
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200214int do_mem_mdc ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
stroese4aaf29b2004-12-16 17:42:39 +0000215{
216 int i;
217 ulong count;
218
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200219 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000220 return CMD_RET_USAGE;
stroese4aaf29b2004-12-16 17:42:39 +0000221
222 count = simple_strtoul(argv[3], NULL, 10);
223
224 for (;;) {
225 do_mem_md (NULL, 0, 3, argv);
226
227 /* delay for <count> ms... */
228 for (i=0; i<count; i++)
229 udelay (1000);
230
231 /* check for ctrl-c to abort... */
232 if (ctrlc()) {
233 puts("Abort\n");
234 return 0;
235 }
236 }
237
238 return 0;
239}
240
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200241int do_mem_mwc ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
stroese4aaf29b2004-12-16 17:42:39 +0000242{
243 int i;
244 ulong count;
245
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200246 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000247 return CMD_RET_USAGE;
stroese4aaf29b2004-12-16 17:42:39 +0000248
249 count = simple_strtoul(argv[3], NULL, 10);
250
251 for (;;) {
252 do_mem_mw (NULL, 0, 3, argv);
253
254 /* delay for <count> ms... */
255 for (i=0; i<count; i++)
256 udelay (1000);
257
258 /* check for ctrl-c to abort... */
259 if (ctrlc()) {
260 puts("Abort\n");
261 return 0;
262 }
263 }
264
265 return 0;
266}
267#endif /* CONFIG_MX_CYCLIC */
268
Kim Phillips088f1b12012-10-29 13:34:31 +0000269static int do_mem_cmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000270{
Simon Glass0628ab82013-02-24 17:33:15 +0000271 ulong addr1, addr2, count, ngood, bytes;
wdenk27b207f2003-07-24 23:38:38 +0000272 int size;
wdenk38635852002-08-27 05:55:31 +0000273 int rcode = 0;
Mike Frysinger054ea172012-01-20 09:07:21 +0000274 const char *type;
Simon Glass0628ab82013-02-24 17:33:15 +0000275 const void *buf1, *buf2, *base;
wdenk38635852002-08-27 05:55:31 +0000276
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200277 if (argc != 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000278 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000279
280 /* Check for size specification.
281 */
wdenk27b207f2003-07-24 23:38:38 +0000282 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
283 return 1;
Mike Frysinger054ea172012-01-20 09:07:21 +0000284 type = size == 4 ? "word" : size == 2 ? "halfword" : "byte";
wdenk38635852002-08-27 05:55:31 +0000285
286 addr1 = simple_strtoul(argv[1], NULL, 16);
287 addr1 += base_address;
288
289 addr2 = simple_strtoul(argv[2], NULL, 16);
290 addr2 += base_address;
291
292 count = simple_strtoul(argv[3], NULL, 16);
293
wdenk2abbe072003-06-16 23:50:08 +0000294#ifdef CONFIG_HAS_DATAFLASH
295 if (addr_dataflash(addr1) | addr_dataflash(addr2)){
wdenk4b9206e2004-03-23 22:14:11 +0000296 puts ("Comparison with DataFlash space not supported.\n\r");
wdenk2abbe072003-06-16 23:50:08 +0000297 return 0;
298 }
299#endif
300
Mike Frysinger4c727c72008-02-04 19:26:56 -0500301#ifdef CONFIG_BLACKFIN
302 if (addr_bfin_on_chip_mem(addr1) || addr_bfin_on_chip_mem(addr2)) {
303 puts ("Comparison with L1 instruction memory not supported.\n\r");
304 return 0;
305 }
306#endif
307
Simon Glass0628ab82013-02-24 17:33:15 +0000308 bytes = size * count;
309 base = buf1 = map_sysmem(addr1, bytes);
310 buf2 = map_sysmem(addr2, bytes);
Mike Frysingerfeb12a12012-01-20 09:07:22 +0000311 for (ngood = 0; ngood < count; ++ngood) {
Mike Frysinger054ea172012-01-20 09:07:21 +0000312 ulong word1, word2;
wdenk38635852002-08-27 05:55:31 +0000313 if (size == 4) {
Simon Glass0628ab82013-02-24 17:33:15 +0000314 word1 = *(ulong *)buf1;
315 word2 = *(ulong *)buf2;
Mike Frysinger054ea172012-01-20 09:07:21 +0000316 } else if (size == 2) {
Simon Glass0628ab82013-02-24 17:33:15 +0000317 word1 = *(ushort *)buf1;
318 word2 = *(ushort *)buf2;
Mike Frysinger054ea172012-01-20 09:07:21 +0000319 } else {
Simon Glass0628ab82013-02-24 17:33:15 +0000320 word1 = *(u_char *)buf1;
321 word2 = *(u_char *)buf2;
wdenk38635852002-08-27 05:55:31 +0000322 }
Mike Frysinger054ea172012-01-20 09:07:21 +0000323 if (word1 != word2) {
Simon Glass0628ab82013-02-24 17:33:15 +0000324 ulong offset = buf1 - base;
325
Mike Frysinger054ea172012-01-20 09:07:21 +0000326 printf("%s at 0x%08lx (%#0*lx) != %s at 0x%08lx (%#0*lx)\n",
Simon Glass0628ab82013-02-24 17:33:15 +0000327 type, (ulong)(addr1 + offset), size, word1,
328 type, (ulong)(addr2 + offset), size, word2);
Mike Frysinger054ea172012-01-20 09:07:21 +0000329 rcode = 1;
330 break;
wdenk38635852002-08-27 05:55:31 +0000331 }
Mike Frysinger054ea172012-01-20 09:07:21 +0000332
Simon Glass0628ab82013-02-24 17:33:15 +0000333 buf1 += size;
334 buf2 += size;
Stefan Roeseeaadb442010-09-13 11:10:34 +0200335
336 /* reset watchdog from time to time */
Mike Frysingerfeb12a12012-01-20 09:07:22 +0000337 if ((ngood % (64 << 10)) == 0)
Stefan Roeseeaadb442010-09-13 11:10:34 +0200338 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000339 }
Simon Glass0628ab82013-02-24 17:33:15 +0000340 unmap_sysmem(buf1);
341 unmap_sysmem(buf2);
wdenk38635852002-08-27 05:55:31 +0000342
Mike Frysinger054ea172012-01-20 09:07:21 +0000343 printf("Total of %ld %s(s) were the same\n", ngood, type);
wdenk38635852002-08-27 05:55:31 +0000344 return rcode;
345}
346
Kim Phillips088f1b12012-10-29 13:34:31 +0000347static int do_mem_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000348{
Simon Glass0628ab82013-02-24 17:33:15 +0000349 ulong addr, dest, count, bytes;
wdenk27b207f2003-07-24 23:38:38 +0000350 int size;
Simon Glass0628ab82013-02-24 17:33:15 +0000351 const void *src;
352 void *buf;
wdenk38635852002-08-27 05:55:31 +0000353
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200354 if (argc != 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000355 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000356
357 /* Check for size specification.
358 */
wdenk27b207f2003-07-24 23:38:38 +0000359 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
360 return 1;
wdenk38635852002-08-27 05:55:31 +0000361
362 addr = simple_strtoul(argv[1], NULL, 16);
363 addr += base_address;
364
365 dest = simple_strtoul(argv[2], NULL, 16);
366 dest += base_address;
367
368 count = simple_strtoul(argv[3], NULL, 16);
369
370 if (count == 0) {
371 puts ("Zero length ???\n");
372 return 1;
373 }
374
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200375#ifndef CONFIG_SYS_NO_FLASH
wdenk38635852002-08-27 05:55:31 +0000376 /* check if we are copying to Flash */
wdenk2abbe072003-06-16 23:50:08 +0000377 if ( (addr2info(dest) != NULL)
378#ifdef CONFIG_HAS_DATAFLASH
Kim B. Heino84d0c2f2008-03-03 10:39:13 +0200379 && (!addr_dataflash(dest))
wdenk2abbe072003-06-16 23:50:08 +0000380#endif
381 ) {
wdenk38635852002-08-27 05:55:31 +0000382 int rc;
383
wdenk4b9206e2004-03-23 22:14:11 +0000384 puts ("Copy to Flash... ");
wdenk38635852002-08-27 05:55:31 +0000385
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200386 rc = flash_write ((char *)addr, dest, count*size);
wdenk38635852002-08-27 05:55:31 +0000387 if (rc != 0) {
388 flash_perror (rc);
389 return (1);
390 }
391 puts ("done\n");
392 return 0;
393 }
394#endif
395
wdenk2abbe072003-06-16 23:50:08 +0000396#ifdef CONFIG_HAS_DATAFLASH
397 /* Check if we are copying from RAM or Flash to DataFlash */
398 if (addr_dataflash(dest) && !addr_dataflash(addr)){
399 int rc;
400
wdenk4b9206e2004-03-23 22:14:11 +0000401 puts ("Copy to DataFlash... ");
wdenk2abbe072003-06-16 23:50:08 +0000402
403 rc = write_dataflash (dest, addr, count*size);
404
405 if (rc != 1) {
406 dataflash_perror (rc);
407 return (1);
408 }
409 puts ("done\n");
410 return 0;
411 }
wdenk8bde7f72003-06-27 21:31:46 +0000412
wdenk2abbe072003-06-16 23:50:08 +0000413 /* Check if we are copying from DataFlash to RAM */
Stelian Pop880cc432008-03-26 22:52:35 +0100414 if (addr_dataflash(addr) && !addr_dataflash(dest)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200415#ifndef CONFIG_SYS_NO_FLASH
Stelian Pop880cc432008-03-26 22:52:35 +0100416 && (addr2info(dest) == NULL)
417#endif
418 ){
wdenk5779d8d2003-12-06 23:55:10 +0000419 int rc;
420 rc = read_dataflash(addr, count * size, (char *) dest);
421 if (rc != 1) {
wdenkd4ca31c2004-01-02 14:00:00 +0000422 dataflash_perror (rc);
423 return (1);
424 }
wdenk2abbe072003-06-16 23:50:08 +0000425 return 0;
426 }
427
428 if (addr_dataflash(addr) && addr_dataflash(dest)){
wdenk4b9206e2004-03-23 22:14:11 +0000429 puts ("Unsupported combination of source/destination.\n\r");
wdenk2abbe072003-06-16 23:50:08 +0000430 return 1;
431 }
432#endif
433
Mike Frysinger4c727c72008-02-04 19:26:56 -0500434#ifdef CONFIG_BLACKFIN
435 /* See if we're copying to/from L1 inst */
436 if (addr_bfin_on_chip_mem(dest) || addr_bfin_on_chip_mem(addr)) {
437 memcpy((void *)dest, (void *)addr, count * size);
438 return 0;
439 }
440#endif
441
Simon Glass0628ab82013-02-24 17:33:15 +0000442 bytes = size * count;
443 buf = map_sysmem(addr, bytes);
444 src = map_sysmem(addr, bytes);
wdenk38635852002-08-27 05:55:31 +0000445 while (count-- > 0) {
446 if (size == 4)
Simon Glass0628ab82013-02-24 17:33:15 +0000447 *((ulong *)buf) = *((ulong *)src);
wdenk38635852002-08-27 05:55:31 +0000448 else if (size == 2)
Simon Glass0628ab82013-02-24 17:33:15 +0000449 *((ushort *)buf) = *((ushort *)src);
wdenk38635852002-08-27 05:55:31 +0000450 else
Simon Glass0628ab82013-02-24 17:33:15 +0000451 *((u_char *)buf) = *((u_char *)src);
452 src += size;
453 buf += size;
Stefan Roeseeaadb442010-09-13 11:10:34 +0200454
455 /* reset watchdog from time to time */
456 if ((count % (64 << 10)) == 0)
457 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000458 }
459 return 0;
460}
461
Kim Phillips088f1b12012-10-29 13:34:31 +0000462static int do_mem_base(cmd_tbl_t *cmdtp, int flag, int argc,
463 char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000464{
465 if (argc > 1) {
466 /* Set new base address.
467 */
468 base_address = simple_strtoul(argv[1], NULL, 16);
469 }
470 /* Print the current base address.
471 */
472 printf("Base Address: 0x%08lx\n", base_address);
473 return 0;
474}
475
Kim Phillips088f1b12012-10-29 13:34:31 +0000476static int do_mem_loop(cmd_tbl_t *cmdtp, int flag, int argc,
477 char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000478{
Simon Glass0628ab82013-02-24 17:33:15 +0000479 ulong addr, length, i, bytes;
wdenk27b207f2003-07-24 23:38:38 +0000480 int size;
wdenk38635852002-08-27 05:55:31 +0000481 volatile uint *longp;
482 volatile ushort *shortp;
483 volatile u_char *cp;
Simon Glass0628ab82013-02-24 17:33:15 +0000484 const void *buf;
wdenk38635852002-08-27 05:55:31 +0000485
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200486 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000487 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000488
Robert P. J. Day85de63e2013-02-03 02:29:54 +0000489 /*
490 * Check for a size specification.
wdenk38635852002-08-27 05:55:31 +0000491 * Defaults to long if no or incorrect specification.
492 */
wdenk27b207f2003-07-24 23:38:38 +0000493 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
494 return 1;
wdenk38635852002-08-27 05:55:31 +0000495
496 /* Address is always specified.
497 */
498 addr = simple_strtoul(argv[1], NULL, 16);
499
500 /* Length is the number of objects, not number of bytes.
501 */
502 length = simple_strtoul(argv[2], NULL, 16);
503
Simon Glass0628ab82013-02-24 17:33:15 +0000504 bytes = size * length;
505 buf = map_sysmem(addr, bytes);
506
wdenk38635852002-08-27 05:55:31 +0000507 /* We want to optimize the loops to run as fast as possible.
508 * If we have only one object, just run infinite loops.
509 */
510 if (length == 1) {
511 if (size == 4) {
Simon Glass0628ab82013-02-24 17:33:15 +0000512 longp = (uint *)buf;
wdenk38635852002-08-27 05:55:31 +0000513 for (;;)
514 i = *longp;
515 }
516 if (size == 2) {
Simon Glass0628ab82013-02-24 17:33:15 +0000517 shortp = (ushort *)buf;
wdenk38635852002-08-27 05:55:31 +0000518 for (;;)
519 i = *shortp;
520 }
Simon Glass0628ab82013-02-24 17:33:15 +0000521 cp = (u_char *)buf;
wdenk38635852002-08-27 05:55:31 +0000522 for (;;)
523 i = *cp;
524 }
525
526 if (size == 4) {
527 for (;;) {
Simon Glass0628ab82013-02-24 17:33:15 +0000528 longp = (uint *)buf;
wdenk38635852002-08-27 05:55:31 +0000529 i = length;
530 while (i-- > 0)
Marek Vasutf3b3c3d2011-09-26 02:26:06 +0200531 *longp++;
wdenk38635852002-08-27 05:55:31 +0000532 }
533 }
534 if (size == 2) {
535 for (;;) {
Simon Glass0628ab82013-02-24 17:33:15 +0000536 shortp = (ushort *)buf;
wdenk38635852002-08-27 05:55:31 +0000537 i = length;
538 while (i-- > 0)
Marek Vasutf3b3c3d2011-09-26 02:26:06 +0200539 *shortp++;
wdenk38635852002-08-27 05:55:31 +0000540 }
541 }
542 for (;;) {
Simon Glass0628ab82013-02-24 17:33:15 +0000543 cp = (u_char *)buf;
wdenk38635852002-08-27 05:55:31 +0000544 i = length;
545 while (i-- > 0)
Marek Vasutf3b3c3d2011-09-26 02:26:06 +0200546 *cp++;
wdenk38635852002-08-27 05:55:31 +0000547 }
Simon Glass0628ab82013-02-24 17:33:15 +0000548 unmap_sysmem(buf);
wdenk38635852002-08-27 05:55:31 +0000549}
550
wdenk56523f12004-07-11 17:40:54 +0000551#ifdef CONFIG_LOOPW
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200552int do_mem_loopw (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk56523f12004-07-11 17:40:54 +0000553{
Simon Glass0628ab82013-02-24 17:33:15 +0000554 ulong addr, length, i, data, bytes;
wdenk56523f12004-07-11 17:40:54 +0000555 int size;
556 volatile uint *longp;
557 volatile ushort *shortp;
558 volatile u_char *cp;
Simon Glass0628ab82013-02-24 17:33:15 +0000559 void *buf;
wdenk81050922004-07-11 20:04:51 +0000560
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200561 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000562 return CMD_RET_USAGE;
wdenk56523f12004-07-11 17:40:54 +0000563
Robert P. J. Day85de63e2013-02-03 02:29:54 +0000564 /*
565 * Check for a size specification.
wdenk56523f12004-07-11 17:40:54 +0000566 * Defaults to long if no or incorrect specification.
567 */
568 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
569 return 1;
570
571 /* Address is always specified.
572 */
573 addr = simple_strtoul(argv[1], NULL, 16);
574
575 /* Length is the number of objects, not number of bytes.
576 */
577 length = simple_strtoul(argv[2], NULL, 16);
578
579 /* data to write */
580 data = simple_strtoul(argv[3], NULL, 16);
wdenk81050922004-07-11 20:04:51 +0000581
Simon Glass0628ab82013-02-24 17:33:15 +0000582 bytes = size * length;
583 buf = map_sysmem(addr, bytes);
584
wdenk56523f12004-07-11 17:40:54 +0000585 /* We want to optimize the loops to run as fast as possible.
586 * If we have only one object, just run infinite loops.
587 */
588 if (length == 1) {
589 if (size == 4) {
Simon Glass0628ab82013-02-24 17:33:15 +0000590 longp = (uint *)buf;
wdenk56523f12004-07-11 17:40:54 +0000591 for (;;)
592 *longp = data;
593 }
594 if (size == 2) {
Simon Glass0628ab82013-02-24 17:33:15 +0000595 shortp = (ushort *)buf;
wdenk56523f12004-07-11 17:40:54 +0000596 for (;;)
597 *shortp = data;
598 }
Simon Glass0628ab82013-02-24 17:33:15 +0000599 cp = (u_char *)buf;
wdenk56523f12004-07-11 17:40:54 +0000600 for (;;)
601 *cp = data;
602 }
603
604 if (size == 4) {
605 for (;;) {
Simon Glass0628ab82013-02-24 17:33:15 +0000606 longp = (uint *)buf;
wdenk56523f12004-07-11 17:40:54 +0000607 i = length;
608 while (i-- > 0)
609 *longp++ = data;
610 }
611 }
612 if (size == 2) {
613 for (;;) {
Simon Glass0628ab82013-02-24 17:33:15 +0000614 shortp = (ushort *)buf;
wdenk56523f12004-07-11 17:40:54 +0000615 i = length;
616 while (i-- > 0)
617 *shortp++ = data;
618 }
619 }
620 for (;;) {
Simon Glass0628ab82013-02-24 17:33:15 +0000621 cp = (u_char *)buf;
wdenk56523f12004-07-11 17:40:54 +0000622 i = length;
623 while (i-- > 0)
624 *cp++ = data;
625 }
626}
627#endif /* CONFIG_LOOPW */
628
Simon Glass51209b12013-02-24 17:33:17 +0000629static ulong mem_test_alt(vu_long *start, vu_long *end)
wdenk38635852002-08-27 05:55:31 +0000630{
Simon Glassc9638f52013-02-24 17:33:16 +0000631 vu_long *addr;
Simon Glassc9638f52013-02-24 17:33:16 +0000632 ulong errs = 0;
633 ulong val, readback;
634 int j;
635 vu_long len;
636 vu_long offset;
637 vu_long test_offset;
638 vu_long pattern;
639 vu_long temp;
640 vu_long anti_pattern;
641 vu_long num_words;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200642#if defined(CONFIG_SYS_MEMTEST_SCRATCH)
Simon Glassc9638f52013-02-24 17:33:16 +0000643 vu_long *dummy = (vu_long *)CONFIG_SYS_MEMTEST_SCRATCH;
wdenk5f535fe2003-09-18 09:21:33 +0000644#else
Kim Phillips088f1b12012-10-29 13:34:31 +0000645 vu_long *dummy = NULL; /* yes, this is address 0x0, not NULL */
wdenk5f535fe2003-09-18 09:21:33 +0000646#endif
wdenk38635852002-08-27 05:55:31 +0000647 static const ulong bitpattern[] = {
648 0x00000001, /* single bit */
649 0x00000003, /* two adjacent bits */
650 0x00000007, /* three adjacent bits */
651 0x0000000F, /* four adjacent bits */
652 0x00000005, /* two non-adjacent bits */
653 0x00000015, /* three non-adjacent bits */
654 0x00000055, /* four non-adjacent bits */
655 0xaaaaaaaa, /* alternating 1/0 */
656 };
wdenk38635852002-08-27 05:55:31 +0000657
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000658 /*
659 * Data line test: write a pattern to the first
660 * location, write the 1's complement to a 'parking'
661 * address (changes the state of the data bus so a
662 * floating bus doesn't give a false OK), and then
663 * read the value back. Note that we read it back
664 * into a variable because the next time we read it,
665 * it might be right (been there, tough to explain to
666 * the quality guys why it prints a failure when the
667 * "is" and "should be" are obviously the same in the
668 * error message).
669 *
670 * Rather than exhaustively testing, we test some
671 * patterns by shifting '1' bits through a field of
672 * '0's and '0' bits through a field of '1's (i.e.
673 * pattern and ~pattern).
674 */
675 addr = start;
676 for (j = 0; j < sizeof(bitpattern) / sizeof(bitpattern[0]); j++) {
677 val = bitpattern[j];
678 for (; val != 0; val <<= 1) {
wdenk38635852002-08-27 05:55:31 +0000679 *addr = val;
Simon Glassc9638f52013-02-24 17:33:16 +0000680 *dummy = ~val; /* clear the test data off the bus */
wdenk38635852002-08-27 05:55:31 +0000681 readback = *addr;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000682 if (readback != val) {
Simon Glassc9638f52013-02-24 17:33:16 +0000683 printf("FAILURE (data line): "
684 "expected %08lx, actual %08lx\n",
685 val, readback);
686 errs++;
687 if (ctrlc()) {
688 putc('\n');
Simon Glass51209b12013-02-24 17:33:17 +0000689 return -1;
Simon Glassc9638f52013-02-24 17:33:16 +0000690 }
wdenk38635852002-08-27 05:55:31 +0000691 }
692 *addr = ~val;
693 *dummy = val;
694 readback = *addr;
Simon Glassc9638f52013-02-24 17:33:16 +0000695 if (readback != ~val) {
696 printf("FAILURE (data line): "
697 "Is %08lx, should be %08lx\n",
698 readback, ~val);
699 errs++;
700 if (ctrlc()) {
701 putc('\n');
Simon Glass51209b12013-02-24 17:33:17 +0000702 return -1;
Simon Glassc9638f52013-02-24 17:33:16 +0000703 }
wdenk38635852002-08-27 05:55:31 +0000704 }
wdenk38635852002-08-27 05:55:31 +0000705 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000706 }
wdenk38635852002-08-27 05:55:31 +0000707
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000708 /*
709 * Based on code whose Original Author and Copyright
710 * information follows: Copyright (c) 1998 by Michael
711 * Barr. This software is placed into the public
712 * domain and may be used for any purpose. However,
713 * this notice must not be changed or removed and no
714 * warranty is either expressed or implied by its
715 * publication or distribution.
716 */
wdenk38635852002-08-27 05:55:31 +0000717
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000718 /*
719 * Address line test
wdenk38635852002-08-27 05:55:31 +0000720
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000721 * Description: Test the address bus wiring in a
722 * memory region by performing a walking
723 * 1's test on the relevant bits of the
724 * address and checking for aliasing.
725 * This test will find single-bit
726 * address failures such as stuck-high,
727 * stuck-low, and shorted pins. The base
728 * address and size of the region are
729 * selected by the caller.
wdenk38635852002-08-27 05:55:31 +0000730
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000731 * Notes: For best results, the selected base
732 * address should have enough LSB 0's to
733 * guarantee single address bit changes.
734 * For example, to test a 64-Kbyte
735 * region, select a base address on a
736 * 64-Kbyte boundary. Also, select the
737 * region size as a power-of-two if at
738 * all possible.
739 *
740 * Returns: 0 if the test succeeds, 1 if the test fails.
741 */
742 len = ((ulong)end - (ulong)start)/sizeof(vu_long);
743 pattern = (vu_long) 0xaaaaaaaa;
744 anti_pattern = (vu_long) 0x55555555;
wdenk38635852002-08-27 05:55:31 +0000745
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000746 debug("%s:%d: length = 0x%.8lx\n",
747 __func__, __LINE__, len);
748 /*
749 * Write the default pattern at each of the
750 * power-of-two offsets.
751 */
752 for (offset = 1; offset < len; offset <<= 1)
753 start[offset] = pattern;
754
755 /*
756 * Check for address bits stuck high.
757 */
758 test_offset = 0;
759 start[test_offset] = anti_pattern;
760
761 for (offset = 1; offset < len; offset <<= 1) {
762 temp = start[offset];
763 if (temp != pattern) {
Simon Glassc9638f52013-02-24 17:33:16 +0000764 printf("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000765 " expected 0x%.8lx, actual 0x%.8lx\n",
766 (ulong)&start[offset], pattern, temp);
Paul Gortmaker87b22b72009-10-02 18:18:33 -0400767 errs++;
768 if (ctrlc()) {
Simon Glassc9638f52013-02-24 17:33:16 +0000769 putc('\n');
Simon Glass51209b12013-02-24 17:33:17 +0000770 return -1;
wdenk38635852002-08-27 05:55:31 +0000771 }
wdenk38635852002-08-27 05:55:31 +0000772 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000773 }
774 start[test_offset] = pattern;
775 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000776
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000777 /*
778 * Check for addr bits stuck low or shorted.
779 */
780 for (test_offset = 1; test_offset < len; test_offset <<= 1) {
781 start[test_offset] = anti_pattern;
wdenk38635852002-08-27 05:55:31 +0000782
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000783 for (offset = 1; offset < len; offset <<= 1) {
784 temp = start[offset];
785 if ((temp != pattern) && (offset != test_offset)) {
786 printf("\nFAILURE: Address bit stuck low or"
787 " shorted @ 0x%.8lx: expected 0x%.8lx,"
788 " actual 0x%.8lx\n",
789 (ulong)&start[offset], pattern, temp);
790 errs++;
791 if (ctrlc()) {
792 putc('\n');
793 return -1;
794 }
795 }
wdenk38635852002-08-27 05:55:31 +0000796 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000797 start[test_offset] = pattern;
798 }
wdenk38635852002-08-27 05:55:31 +0000799
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000800 /*
801 * Description: Test the integrity of a physical
802 * memory device by performing an
803 * increment/decrement test over the
804 * entire region. In the process every
805 * storage bit in the device is tested
806 * as a zero and a one. The base address
807 * and the size of the region are
808 * selected by the caller.
809 *
810 * Returns: 0 if the test succeeds, 1 if the test fails.
811 */
812 num_words = ((ulong)end - (ulong)start)/sizeof(vu_long) + 1;
813
814 /*
815 * Fill memory with a known pattern.
816 */
817 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
818 WATCHDOG_RESET();
819 start[offset] = pattern;
820 }
821
822 /*
823 * Check each location and invert it for the second pass.
824 */
825 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
826 WATCHDOG_RESET();
827 temp = start[offset];
828 if (temp != pattern) {
Simon Glassc9638f52013-02-24 17:33:16 +0000829 printf("\nFAILURE (read/write) @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000830 " expected 0x%.8lx, actual 0x%.8lx)\n",
831 (ulong)&start[offset], pattern, temp);
Paul Gortmaker87b22b72009-10-02 18:18:33 -0400832 errs++;
833 if (ctrlc()) {
Simon Glassc9638f52013-02-24 17:33:16 +0000834 putc('\n');
Simon Glass51209b12013-02-24 17:33:17 +0000835 return -1;
Paul Gortmaker87b22b72009-10-02 18:18:33 -0400836 }
wdenk38635852002-08-27 05:55:31 +0000837 }
838
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000839 anti_pattern = ~pattern;
840 start[offset] = anti_pattern;
841 }
842
843 /*
844 * Check each location for the inverted pattern and zero it.
845 */
846 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
847 WATCHDOG_RESET();
848 anti_pattern = ~pattern;
849 temp = start[offset];
850 if (temp != anti_pattern) {
Simon Glassc9638f52013-02-24 17:33:16 +0000851 printf("\nFAILURE (read/write): @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000852 " expected 0x%.8lx, actual 0x%.8lx)\n",
853 (ulong)&start[offset], anti_pattern, temp);
Paul Gortmaker87b22b72009-10-02 18:18:33 -0400854 errs++;
855 if (ctrlc()) {
Simon Glassc9638f52013-02-24 17:33:16 +0000856 putc('\n');
Simon Glass51209b12013-02-24 17:33:17 +0000857 return -1;
Paul Gortmaker87b22b72009-10-02 18:18:33 -0400858 }
wdenk38635852002-08-27 05:55:31 +0000859 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000860 start[offset] = 0;
861 }
Simon Glass51209b12013-02-24 17:33:17 +0000862
863 return 0;
Simon Glassc9638f52013-02-24 17:33:16 +0000864}
wdenk38635852002-08-27 05:55:31 +0000865
Simon Glass51209b12013-02-24 17:33:17 +0000866static ulong mem_test_quick(vu_long *start, vu_long *end, vu_long pattern,
867 int iteration)
Simon Glassc9638f52013-02-24 17:33:16 +0000868{
869 vu_long *addr;
Simon Glassc9638f52013-02-24 17:33:16 +0000870 ulong errs = 0;
871 ulong incr;
872 ulong val, readback;
873
Simon Glass51209b12013-02-24 17:33:17 +0000874 /* Alternate the pattern */
wdenk38635852002-08-27 05:55:31 +0000875 incr = 1;
Simon Glass51209b12013-02-24 17:33:17 +0000876 if (iteration & 1) {
877 incr = -incr;
878 /*
879 * Flip the pattern each time to make lots of zeros and
880 * then, the next time, lots of ones. We decrement
881 * the "negative" patterns and increment the "positive"
882 * patterns to preserve this feature.
883 */
884 if (pattern & 0x80000000)
885 pattern = -pattern; /* complement & increment */
886 else
887 pattern = ~pattern;
888 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000889 printf("\rPattern %08lX Writing..."
890 "%12s"
891 "\b\b\b\b\b\b\b\b\b\b",
892 pattern, "");
wdenk38635852002-08-27 05:55:31 +0000893
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000894 for (addr = start, val = pattern; addr < end; addr++) {
895 WATCHDOG_RESET();
896 *addr = val;
897 val += incr;
898 }
wdenk38635852002-08-27 05:55:31 +0000899
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000900 puts("Reading...");
wdenk38635852002-08-27 05:55:31 +0000901
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000902 for (addr = start, val = pattern; addr < end; addr++) {
903 WATCHDOG_RESET();
904 readback = *addr;
905 if (readback != val) {
906 printf("\nMem error @ 0x%08X: "
907 "found %08lX, expected %08lX\n",
908 (uint)(uintptr_t)addr, readback, val);
909 errs++;
910 if (ctrlc()) {
911 putc('\n');
912 return -1;
wdenk38635852002-08-27 05:55:31 +0000913 }
wdenk38635852002-08-27 05:55:31 +0000914 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000915 val += incr;
916 }
wdenk38635852002-08-27 05:55:31 +0000917
Simon Glass51209b12013-02-24 17:33:17 +0000918 return 0;
Simon Glassc9638f52013-02-24 17:33:16 +0000919}
920
921/*
922 * Perform a memory test. A more complete alternative test can be
923 * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
924 * interrupted by ctrl-c or by a failure of one of the sub-tests.
925 */
926static int do_mem_mtest(cmd_tbl_t *cmdtp, int flag, int argc,
927 char * const argv[])
928{
929 vu_long *start, *end;
930 int iteration_limit;
931 int ret;
Simon Glass51209b12013-02-24 17:33:17 +0000932 ulong errs = 0; /* number of errors, or -1 if interrupted */
Simon Glassc9638f52013-02-24 17:33:16 +0000933 ulong pattern;
Simon Glass51209b12013-02-24 17:33:17 +0000934 int iteration;
Simon Glassc9638f52013-02-24 17:33:16 +0000935#if defined(CONFIG_SYS_ALT_MEMTEST)
936 const int alt_test = 1;
937#else
938 const int alt_test = 0;
wdenk38635852002-08-27 05:55:31 +0000939#endif
Simon Glassc9638f52013-02-24 17:33:16 +0000940
941 if (argc > 1)
942 start = (ulong *)simple_strtoul(argv[1], NULL, 16);
943 else
944 start = (ulong *)CONFIG_SYS_MEMTEST_START;
945
946 if (argc > 2)
947 end = (ulong *)simple_strtoul(argv[2], NULL, 16);
948 else
949 end = (ulong *)(CONFIG_SYS_MEMTEST_END);
950
951 if (argc > 3)
952 pattern = (ulong)simple_strtoul(argv[3], NULL, 16);
953 else
954 pattern = 0;
955
956 if (argc > 4)
957 iteration_limit = (ulong)simple_strtoul(argv[4], NULL, 16);
958 else
959 iteration_limit = 0;
960
Simon Glass51209b12013-02-24 17:33:17 +0000961 printf("Testing %08x ... %08x:\n", (uint)(uintptr_t)start,
962 (uint)(uintptr_t)end);
963 debug("%s:%d: start 0x%p end 0x%p\n",
964 __func__, __LINE__, start, end);
965
966 for (iteration = 0;
967 !iteration_limit || iteration < iteration_limit;
968 iteration++) {
969 if (ctrlc()) {
970 putc('\n');
971 errs = -1UL;
972 break;
973 }
974
975 printf("Iteration: %6d\r", iteration + 1);
976 debug("\n");
977 if (alt_test)
978 errs = mem_test_alt(start, end);
979 else
980 errs = mem_test_quick(start, end, pattern, iteration);
981 }
982
983 if (errs == -1UL) {
984 /* Memory test was aborted */
985 ret = 1;
986 } else {
987 printf("Tested %d iteration(s) with %lu errors.\n",
988 iteration, errs);
989 ret = errs != 0;
990 }
Simon Glassc9638f52013-02-24 17:33:16 +0000991
992 return ret; /* not reached */
wdenk38635852002-08-27 05:55:31 +0000993}
994
995
996/* Modify memory.
997 *
998 * Syntax:
999 * mm{.b, .w, .l} {addr}
1000 * nm{.b, .w, .l} {addr}
1001 */
1002static int
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001003mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +00001004{
wdenk27b207f2003-07-24 23:38:38 +00001005 ulong addr, i;
1006 int nbytes, size;
Simon Glass0628ab82013-02-24 17:33:15 +00001007 void *ptr = NULL;
wdenk38635852002-08-27 05:55:31 +00001008
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001009 if (argc != 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001010 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +00001011
1012#ifdef CONFIG_BOOT_RETRY_TIME
1013 reset_cmd_timeout(); /* got a good command to get here */
1014#endif
1015 /* We use the last specified parameters, unless new ones are
1016 * entered.
1017 */
1018 addr = mm_last_addr;
1019 size = mm_last_size;
1020
1021 if ((flag & CMD_FLAG_REPEAT) == 0) {
1022 /* New command specified. Check for a size specification.
1023 * Defaults to long if no or incorrect specification.
1024 */
wdenk27b207f2003-07-24 23:38:38 +00001025 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
1026 return 1;
wdenk38635852002-08-27 05:55:31 +00001027
1028 /* Address is specified since argc > 1
1029 */
1030 addr = simple_strtoul(argv[1], NULL, 16);
1031 addr += base_address;
1032 }
1033
wdenk2abbe072003-06-16 23:50:08 +00001034#ifdef CONFIG_HAS_DATAFLASH
1035 if (addr_dataflash(addr)){
wdenk4b9206e2004-03-23 22:14:11 +00001036 puts ("Can't modify DataFlash in place. Use cp instead.\n\r");
wdenk2abbe072003-06-16 23:50:08 +00001037 return 0;
1038 }
1039#endif
1040
Mike Frysinger4c727c72008-02-04 19:26:56 -05001041#ifdef CONFIG_BLACKFIN
1042 if (addr_bfin_on_chip_mem(addr)) {
1043 puts ("Can't modify L1 instruction in place. Use cp instead.\n\r");
1044 return 0;
1045 }
1046#endif
1047
wdenk38635852002-08-27 05:55:31 +00001048 /* Print the address, followed by value. Then accept input for
1049 * the next value. A non-converted value exits.
1050 */
1051 do {
Simon Glass0628ab82013-02-24 17:33:15 +00001052 ptr = map_sysmem(addr, size);
wdenk38635852002-08-27 05:55:31 +00001053 printf("%08lx:", addr);
1054 if (size == 4)
Simon Glass0628ab82013-02-24 17:33:15 +00001055 printf(" %08x", *((uint *)ptr));
wdenk38635852002-08-27 05:55:31 +00001056 else if (size == 2)
Simon Glass0628ab82013-02-24 17:33:15 +00001057 printf(" %04x", *((ushort *)ptr));
wdenk38635852002-08-27 05:55:31 +00001058 else
Simon Glass0628ab82013-02-24 17:33:15 +00001059 printf(" %02x", *((u_char *)ptr));
wdenk38635852002-08-27 05:55:31 +00001060
1061 nbytes = readline (" ? ");
1062 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
1063 /* <CR> pressed as only input, don't modify current
1064 * location and move to next. "-" pressed will go back.
1065 */
1066 if (incrflag)
1067 addr += nbytes ? -size : size;
1068 nbytes = 1;
1069#ifdef CONFIG_BOOT_RETRY_TIME
1070 reset_cmd_timeout(); /* good enough to not time out */
1071#endif
1072 }
1073#ifdef CONFIG_BOOT_RETRY_TIME
1074 else if (nbytes == -2) {
1075 break; /* timed out, exit the command */
1076 }
1077#endif
1078 else {
1079 char *endp;
1080 i = simple_strtoul(console_buffer, &endp, 16);
1081 nbytes = endp - console_buffer;
1082 if (nbytes) {
1083#ifdef CONFIG_BOOT_RETRY_TIME
1084 /* good enough to not time out
1085 */
1086 reset_cmd_timeout();
1087#endif
1088 if (size == 4)
Simon Glass0628ab82013-02-24 17:33:15 +00001089 *((uint *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001090 else if (size == 2)
Simon Glass0628ab82013-02-24 17:33:15 +00001091 *((ushort *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001092 else
Simon Glass0628ab82013-02-24 17:33:15 +00001093 *((u_char *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001094 if (incrflag)
1095 addr += size;
1096 }
1097 }
1098 } while (nbytes);
Simon Glass0628ab82013-02-24 17:33:15 +00001099 if (ptr)
1100 unmap_sysmem(ptr);
wdenk38635852002-08-27 05:55:31 +00001101
1102 mm_last_addr = addr;
1103 mm_last_size = size;
1104 return 0;
1105}
1106
Mike Frysinger710b9932010-12-21 14:19:51 -05001107#ifdef CONFIG_CMD_CRC32
1108
wdenkc26e4542004-04-18 10:13:26 +00001109#ifndef CONFIG_CRC32_VERIFY
1110
Kim Phillips088f1b12012-10-29 13:34:31 +00001111static int do_mem_crc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +00001112{
wdenk71f95112003-06-15 22:40:42 +00001113 ulong addr, length;
1114 ulong crc;
1115 ulong *ptr;
wdenk38635852002-08-27 05:55:31 +00001116
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001117 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001118 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +00001119
wdenk71f95112003-06-15 22:40:42 +00001120 addr = simple_strtoul (argv[1], NULL, 16);
wdenk38635852002-08-27 05:55:31 +00001121 addr += base_address;
1122
wdenk71f95112003-06-15 22:40:42 +00001123 length = simple_strtoul (argv[2], NULL, 16);
wdenk38635852002-08-27 05:55:31 +00001124
Jens Scharsig39c6e032011-07-18 08:46:26 +02001125 crc = crc32_wd (0, (const uchar *) addr, length, CHUNKSZ_CRC32);
wdenk38635852002-08-27 05:55:31 +00001126
1127 printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
wdenk71f95112003-06-15 22:40:42 +00001128 addr, addr + length - 1, crc);
wdenk38635852002-08-27 05:55:31 +00001129
wdenk71f95112003-06-15 22:40:42 +00001130 if (argc > 3) {
1131 ptr = (ulong *) simple_strtoul (argv[3], NULL, 16);
1132 *ptr = crc;
1133 }
wdenk38635852002-08-27 05:55:31 +00001134
1135 return 0;
1136}
1137
wdenkc26e4542004-04-18 10:13:26 +00001138#else /* CONFIG_CRC32_VERIFY */
1139
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001140int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenkc26e4542004-04-18 10:13:26 +00001141{
1142 ulong addr, length;
1143 ulong crc;
1144 ulong *ptr;
wdenk6e592382004-04-18 17:39:38 +00001145 ulong vcrc;
wdenkc26e4542004-04-18 10:13:26 +00001146 int verify;
1147 int ac;
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001148 char * const *av;
wdenkc26e4542004-04-18 10:13:26 +00001149
1150 if (argc < 3) {
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001151usage:
Simon Glass4c12eeb2011-12-10 08:44:01 +00001152 return CMD_RET_USAGE;
wdenkc26e4542004-04-18 10:13:26 +00001153 }
1154
1155 av = argv + 1;
1156 ac = argc - 1;
1157 if (strcmp(*av, "-v") == 0) {
1158 verify = 1;
1159 av++;
1160 ac--;
1161 if (ac < 3)
1162 goto usage;
1163 } else
1164 verify = 0;
1165
1166 addr = simple_strtoul(*av++, NULL, 16);
1167 addr += base_address;
1168 length = simple_strtoul(*av++, NULL, 16);
1169
Jens Scharsig39c6e032011-07-18 08:46:26 +02001170 crc = crc32_wd (0, (const uchar *) addr, length, CHUNKSZ_CRC32);
wdenkc26e4542004-04-18 10:13:26 +00001171
1172 if (!verify) {
1173 printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
1174 addr, addr + length - 1, crc);
1175 if (ac > 2) {
1176 ptr = (ulong *) simple_strtoul (*av++, NULL, 16);
1177 *ptr = crc;
1178 }
1179 } else {
1180 vcrc = simple_strtoul(*av++, NULL, 16);
1181 if (vcrc != crc) {
1182 printf ("CRC32 for %08lx ... %08lx ==> %08lx != %08lx ** ERROR **\n",
1183 addr, addr + length - 1, crc, vcrc);
1184 return 1;
1185 }
1186 }
1187
1188 return 0;
1189
1190}
1191#endif /* CONFIG_CRC32_VERIFY */
1192
Mike Frysinger710b9932010-12-21 14:19:51 -05001193#endif
1194
wdenk8bde7f72003-06-27 21:31:46 +00001195/**************************************************/
wdenk0d498392003-07-01 21:06:45 +00001196U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001197 md, 3, 1, do_mem_md,
Peter Tyser2fb26042009-01-27 18:03:12 -06001198 "memory display",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001199 "[.b, .w, .l] address [# of objects]"
wdenk8bde7f72003-06-27 21:31:46 +00001200);
1201
1202
wdenk0d498392003-07-01 21:06:45 +00001203U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001204 mm, 2, 1, do_mem_mm,
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001205 "memory modify (auto-incrementing address)",
1206 "[.b, .w, .l] address"
wdenk8bde7f72003-06-27 21:31:46 +00001207);
1208
1209
wdenk0d498392003-07-01 21:06:45 +00001210U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001211 nm, 2, 1, do_mem_nm,
Peter Tyser2fb26042009-01-27 18:03:12 -06001212 "memory modify (constant address)",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001213 "[.b, .w, .l] address"
wdenk8bde7f72003-06-27 21:31:46 +00001214);
1215
wdenk0d498392003-07-01 21:06:45 +00001216U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001217 mw, 4, 1, do_mem_mw,
Peter Tyser2fb26042009-01-27 18:03:12 -06001218 "memory write (fill)",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001219 "[.b, .w, .l] address value [count]"
wdenk8bde7f72003-06-27 21:31:46 +00001220);
1221
wdenk0d498392003-07-01 21:06:45 +00001222U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001223 cp, 4, 1, do_mem_cp,
Peter Tyser2fb26042009-01-27 18:03:12 -06001224 "memory copy",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001225 "[.b, .w, .l] source target count"
wdenk8bde7f72003-06-27 21:31:46 +00001226);
1227
wdenk0d498392003-07-01 21:06:45 +00001228U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001229 cmp, 4, 1, do_mem_cmp,
Peter Tyser2fb26042009-01-27 18:03:12 -06001230 "memory compare",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001231 "[.b, .w, .l] addr1 addr2 count"
wdenk8bde7f72003-06-27 21:31:46 +00001232);
1233
Mike Frysinger710b9932010-12-21 14:19:51 -05001234#ifdef CONFIG_CMD_CRC32
1235
wdenkc26e4542004-04-18 10:13:26 +00001236#ifndef CONFIG_CRC32_VERIFY
1237
wdenk0d498392003-07-01 21:06:45 +00001238U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001239 crc32, 4, 1, do_mem_crc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001240 "checksum calculation",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001241 "address count [addr]\n - compute CRC32 checksum [save at addr]"
wdenk8bde7f72003-06-27 21:31:46 +00001242);
1243
wdenkc26e4542004-04-18 10:13:26 +00001244#else /* CONFIG_CRC32_VERIFY */
1245
1246U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001247 crc32, 5, 1, do_mem_crc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001248 "checksum calculation",
wdenkc26e4542004-04-18 10:13:26 +00001249 "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001250 "-v address count crc\n - verify crc of memory area"
wdenkc26e4542004-04-18 10:13:26 +00001251);
1252
1253#endif /* CONFIG_CRC32_VERIFY */
1254
Mike Frysinger710b9932010-12-21 14:19:51 -05001255#endif
1256
Simon Glass15a33e42012-11-30 13:01:20 +00001257#ifdef CONFIG_CMD_MEMINFO
1258__weak void board_show_dram(ulong size)
1259{
1260 puts("DRAM: ");
1261 print_size(size, "\n");
1262}
1263
1264static int do_mem_info(cmd_tbl_t *cmdtp, int flag, int argc,
1265 char * const argv[])
1266{
1267 board_show_dram(gd->ram_size);
1268
1269 return 0;
1270}
1271#endif
1272
wdenk0d498392003-07-01 21:06:45 +00001273U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001274 base, 2, 1, do_mem_base,
Peter Tyser2fb26042009-01-27 18:03:12 -06001275 "print or set address offset",
wdenk8bde7f72003-06-27 21:31:46 +00001276 "\n - print address offset for memory commands\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001277 "base off\n - set address offset for memory commands to 'off'"
wdenk8bde7f72003-06-27 21:31:46 +00001278);
1279
wdenk0d498392003-07-01 21:06:45 +00001280U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001281 loop, 3, 1, do_mem_loop,
Peter Tyser2fb26042009-01-27 18:03:12 -06001282 "infinite loop on address range",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001283 "[.b, .w, .l] address number_of_objects"
wdenk8bde7f72003-06-27 21:31:46 +00001284);
1285
wdenk56523f12004-07-11 17:40:54 +00001286#ifdef CONFIG_LOOPW
1287U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001288 loopw, 4, 1, do_mem_loopw,
Peter Tyser2fb26042009-01-27 18:03:12 -06001289 "infinite write loop on address range",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001290 "[.b, .w, .l] address number_of_objects data_to_write"
wdenk56523f12004-07-11 17:40:54 +00001291);
1292#endif /* CONFIG_LOOPW */
1293
wdenk0d498392003-07-01 21:06:45 +00001294U_BOOT_CMD(
Dirk Eibachb6fc6fd2008-12-16 14:51:56 +01001295 mtest, 5, 1, do_mem_mtest,
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001296 "simple RAM read/write test",
1297 "[start [end [pattern [iterations]]]]"
wdenk8bde7f72003-06-27 21:31:46 +00001298);
1299
stroese4aaf29b2004-12-16 17:42:39 +00001300#ifdef CONFIG_MX_CYCLIC
1301U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001302 mdc, 4, 1, do_mem_mdc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001303 "memory display cyclic",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001304 "[.b, .w, .l] address count delay(ms)"
stroese4aaf29b2004-12-16 17:42:39 +00001305);
1306
1307U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001308 mwc, 4, 1, do_mem_mwc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001309 "memory write cyclic",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001310 "[.b, .w, .l] address value delay(ms)"
stroese4aaf29b2004-12-16 17:42:39 +00001311);
1312#endif /* CONFIG_MX_CYCLIC */
Simon Glass15a33e42012-11-30 13:01:20 +00001313
1314#ifdef CONFIG_CMD_MEMINFO
1315U_BOOT_CMD(
1316 meminfo, 3, 1, do_mem_info,
1317 "display memory information",
1318 ""
1319);
1320#endif