blob: 042c994a1bb53b7d535b5dd4a9c7e9951e6f3e1c [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
Simon Glassd20a40d2013-02-24 20:30:22 +000035#include <hash.h>
Sergei Poselenova6e6fc62008-04-09 16:09:41 +020036#include <watchdog.h>
Simon Glass0628ab82013-02-24 17:33:15 +000037#include <asm/io.h>
Simon Glass15a33e42012-11-30 13:01:20 +000038#include <linux/compiler.h>
39
40DECLARE_GLOBAL_DATA_PTR;
wdenk38635852002-08-27 05:55:31 +000041
Simon Glass8e169cc2013-02-24 17:33:29 +000042#ifndef CONFIG_SYS_MEMTEST_SCRATCH
43#define CONFIG_SYS_MEMTEST_SCRATCH 0
44#endif
45
Wolfgang Denk54841ab2010-06-28 22:00:46 +020046static int mod_mem(cmd_tbl_t *, int, int, int, char * const []);
wdenk38635852002-08-27 05:55:31 +000047
48/* Display values from last command.
49 * Memory modify remembered values are different from display memory.
50 */
Mike Frysingerd6efe242010-12-22 09:40:29 -050051static uint dp_last_addr, dp_last_size;
52static uint dp_last_length = 0x40;
53static uint mm_last_addr, mm_last_size;
wdenk38635852002-08-27 05:55:31 +000054
55static ulong base_address = 0;
56
57/* Memory Display
58 *
59 * Syntax:
60 * md{.b, .w, .l} {addr} {len}
61 */
62#define DISP_LINE_LEN 16
Kim Phillips088f1b12012-10-29 13:34:31 +000063static int do_mem_md(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000064{
wdenk27b207f2003-07-24 23:38:38 +000065 ulong addr, length;
Grant Likelyc95c4282007-02-20 09:05:00 +010066#if defined(CONFIG_HAS_DATAFLASH)
67 ulong nbytes, linebytes;
68#endif
wdenk27b207f2003-07-24 23:38:38 +000069 int size;
wdenk38635852002-08-27 05:55:31 +000070 int rc = 0;
71
72 /* We use the last specified parameters, unless new ones are
73 * entered.
74 */
75 addr = dp_last_addr;
76 size = dp_last_size;
77 length = dp_last_length;
78
Wolfgang Denk47e26b12010-07-17 01:06:04 +020079 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +000080 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +000081
82 if ((flag & CMD_FLAG_REPEAT) == 0) {
83 /* New command specified. Check for a size specification.
84 * Defaults to long if no or incorrect specification.
85 */
wdenk27b207f2003-07-24 23:38:38 +000086 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
87 return 1;
wdenk38635852002-08-27 05:55:31 +000088
89 /* Address is specified since argc > 1
90 */
91 addr = simple_strtoul(argv[1], NULL, 16);
92 addr += base_address;
93
94 /* If another parameter, it is the length to display.
95 * Length is the number of objects, not number of bytes.
96 */
97 if (argc > 2)
98 length = simple_strtoul(argv[2], NULL, 16);
99 }
100
Grant Likelyc95c4282007-02-20 09:05:00 +0100101#if defined(CONFIG_HAS_DATAFLASH)
wdenk38635852002-08-27 05:55:31 +0000102 /* Print the lines.
103 *
104 * We buffer all read data, so we can make sure data is read only
105 * once, and all accesses are with the specified bus width.
106 */
107 nbytes = length * size;
108 do {
109 char linebuf[DISP_LINE_LEN];
Grant Likelyc95c4282007-02-20 09:05:00 +0100110 void* p;
wdenk38635852002-08-27 05:55:31 +0000111 linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
wdenk2abbe072003-06-16 23:50:08 +0000112
Grant Likelyc95c4282007-02-20 09:05:00 +0100113 rc = read_dataflash(addr, (linebytes/size)*size, linebuf);
114 p = (rc == DATAFLASH_OK) ? linebuf : (void*)addr;
115 print_buffer(addr, p, size, linebytes/size, DISP_LINE_LEN/size);
wdenk8bde7f72003-06-27 21:31:46 +0000116
wdenk38635852002-08-27 05:55:31 +0000117 nbytes -= linebytes;
Grant Likelyc95c4282007-02-20 09:05:00 +0100118 addr += linebytes;
wdenk38635852002-08-27 05:55:31 +0000119 if (ctrlc()) {
120 rc = 1;
121 break;
122 }
123 } while (nbytes > 0);
Grant Likelyc95c4282007-02-20 09:05:00 +0100124#else
Mike Frysinger4c727c72008-02-04 19:26:56 -0500125
126# if defined(CONFIG_BLACKFIN)
127 /* See if we're trying to display L1 inst */
128 if (addr_bfin_on_chip_mem(addr)) {
129 char linebuf[DISP_LINE_LEN];
130 ulong linebytes, nbytes = length * size;
131 do {
132 linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
133 memcpy(linebuf, (void *)addr, linebytes);
134 print_buffer(addr, linebuf, size, linebytes/size, DISP_LINE_LEN/size);
135
136 nbytes -= linebytes;
137 addr += linebytes;
138 if (ctrlc()) {
139 rc = 1;
140 break;
141 }
142 } while (nbytes > 0);
143 } else
144# endif
145
146 {
Simon Glass0628ab82013-02-24 17:33:15 +0000147 ulong bytes = size * length;
148 const void *buf = map_sysmem(addr, bytes);
149
Mike Frysinger4c727c72008-02-04 19:26:56 -0500150 /* Print the lines. */
Simon Glass0628ab82013-02-24 17:33:15 +0000151 print_buffer(addr, buf, size, length, DISP_LINE_LEN / size);
152 addr += bytes;
153 unmap_sysmem(buf);
Mike Frysinger4c727c72008-02-04 19:26:56 -0500154 }
Grant Likelyc95c4282007-02-20 09:05:00 +0100155#endif
wdenk38635852002-08-27 05:55:31 +0000156
157 dp_last_addr = addr;
158 dp_last_length = length;
159 dp_last_size = size;
160 return (rc);
161}
162
Kim Phillips088f1b12012-10-29 13:34:31 +0000163static int do_mem_mm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000164{
165 return mod_mem (cmdtp, 1, flag, argc, argv);
166}
Kim Phillips088f1b12012-10-29 13:34:31 +0000167static int do_mem_nm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000168{
169 return mod_mem (cmdtp, 0, flag, argc, argv);
170}
171
Kim Phillips088f1b12012-10-29 13:34:31 +0000172static int do_mem_mw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000173{
wdenk27b207f2003-07-24 23:38:38 +0000174 ulong addr, writeval, count;
175 int size;
Simon Glass0628ab82013-02-24 17:33:15 +0000176 void *buf;
177 ulong bytes;
wdenk38635852002-08-27 05:55:31 +0000178
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200179 if ((argc < 3) || (argc > 4))
Simon Glass4c12eeb2011-12-10 08:44:01 +0000180 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000181
182 /* Check for size specification.
183 */
wdenk27b207f2003-07-24 23:38:38 +0000184 if ((size = cmd_get_data_size(argv[0], 4)) < 1)
185 return 1;
wdenk38635852002-08-27 05:55:31 +0000186
187 /* Address is specified since argc > 1
188 */
189 addr = simple_strtoul(argv[1], NULL, 16);
190 addr += base_address;
191
192 /* Get the value to write.
193 */
194 writeval = simple_strtoul(argv[2], NULL, 16);
195
196 /* Count ? */
197 if (argc == 4) {
198 count = simple_strtoul(argv[3], NULL, 16);
199 } else {
200 count = 1;
201 }
202
Simon Glass0628ab82013-02-24 17:33:15 +0000203 bytes = size * count;
204 buf = map_sysmem(addr, bytes);
wdenk38635852002-08-27 05:55:31 +0000205 while (count-- > 0) {
206 if (size == 4)
Simon Glass0628ab82013-02-24 17:33:15 +0000207 *((ulong *)buf) = (ulong)writeval;
wdenk38635852002-08-27 05:55:31 +0000208 else if (size == 2)
Simon Glass0628ab82013-02-24 17:33:15 +0000209 *((ushort *)buf) = (ushort)writeval;
wdenk38635852002-08-27 05:55:31 +0000210 else
Simon Glass0628ab82013-02-24 17:33:15 +0000211 *((u_char *)buf) = (u_char)writeval;
212 buf += size;
wdenk38635852002-08-27 05:55:31 +0000213 }
Simon Glass0628ab82013-02-24 17:33:15 +0000214 unmap_sysmem(buf);
wdenk38635852002-08-27 05:55:31 +0000215 return 0;
216}
217
stroese4aaf29b2004-12-16 17:42:39 +0000218#ifdef CONFIG_MX_CYCLIC
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200219int do_mem_mdc ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
stroese4aaf29b2004-12-16 17:42:39 +0000220{
221 int i;
222 ulong count;
223
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200224 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000225 return CMD_RET_USAGE;
stroese4aaf29b2004-12-16 17:42:39 +0000226
227 count = simple_strtoul(argv[3], NULL, 10);
228
229 for (;;) {
230 do_mem_md (NULL, 0, 3, argv);
231
232 /* delay for <count> ms... */
233 for (i=0; i<count; i++)
234 udelay (1000);
235
236 /* check for ctrl-c to abort... */
237 if (ctrlc()) {
238 puts("Abort\n");
239 return 0;
240 }
241 }
242
243 return 0;
244}
245
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200246int do_mem_mwc ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
stroese4aaf29b2004-12-16 17:42:39 +0000247{
248 int i;
249 ulong count;
250
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200251 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000252 return CMD_RET_USAGE;
stroese4aaf29b2004-12-16 17:42:39 +0000253
254 count = simple_strtoul(argv[3], NULL, 10);
255
256 for (;;) {
257 do_mem_mw (NULL, 0, 3, argv);
258
259 /* delay for <count> ms... */
260 for (i=0; i<count; i++)
261 udelay (1000);
262
263 /* check for ctrl-c to abort... */
264 if (ctrlc()) {
265 puts("Abort\n");
266 return 0;
267 }
268 }
269
270 return 0;
271}
272#endif /* CONFIG_MX_CYCLIC */
273
Kim Phillips088f1b12012-10-29 13:34:31 +0000274static int do_mem_cmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000275{
Simon Glass0628ab82013-02-24 17:33:15 +0000276 ulong addr1, addr2, count, ngood, bytes;
wdenk27b207f2003-07-24 23:38:38 +0000277 int size;
wdenk38635852002-08-27 05:55:31 +0000278 int rcode = 0;
Mike Frysinger054ea172012-01-20 09:07:21 +0000279 const char *type;
Simon Glass0628ab82013-02-24 17:33:15 +0000280 const void *buf1, *buf2, *base;
wdenk38635852002-08-27 05:55:31 +0000281
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200282 if (argc != 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000283 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000284
285 /* Check for size specification.
286 */
wdenk27b207f2003-07-24 23:38:38 +0000287 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
288 return 1;
Mike Frysinger054ea172012-01-20 09:07:21 +0000289 type = size == 4 ? "word" : size == 2 ? "halfword" : "byte";
wdenk38635852002-08-27 05:55:31 +0000290
291 addr1 = simple_strtoul(argv[1], NULL, 16);
292 addr1 += base_address;
293
294 addr2 = simple_strtoul(argv[2], NULL, 16);
295 addr2 += base_address;
296
297 count = simple_strtoul(argv[3], NULL, 16);
298
wdenk2abbe072003-06-16 23:50:08 +0000299#ifdef CONFIG_HAS_DATAFLASH
300 if (addr_dataflash(addr1) | addr_dataflash(addr2)){
wdenk4b9206e2004-03-23 22:14:11 +0000301 puts ("Comparison with DataFlash space not supported.\n\r");
wdenk2abbe072003-06-16 23:50:08 +0000302 return 0;
303 }
304#endif
305
Mike Frysinger4c727c72008-02-04 19:26:56 -0500306#ifdef CONFIG_BLACKFIN
307 if (addr_bfin_on_chip_mem(addr1) || addr_bfin_on_chip_mem(addr2)) {
308 puts ("Comparison with L1 instruction memory not supported.\n\r");
309 return 0;
310 }
311#endif
312
Simon Glass0628ab82013-02-24 17:33:15 +0000313 bytes = size * count;
314 base = buf1 = map_sysmem(addr1, bytes);
315 buf2 = map_sysmem(addr2, bytes);
Mike Frysingerfeb12a12012-01-20 09:07:22 +0000316 for (ngood = 0; ngood < count; ++ngood) {
Mike Frysinger054ea172012-01-20 09:07:21 +0000317 ulong word1, word2;
wdenk38635852002-08-27 05:55:31 +0000318 if (size == 4) {
Simon Glass0628ab82013-02-24 17:33:15 +0000319 word1 = *(ulong *)buf1;
320 word2 = *(ulong *)buf2;
Mike Frysinger054ea172012-01-20 09:07:21 +0000321 } else if (size == 2) {
Simon Glass0628ab82013-02-24 17:33:15 +0000322 word1 = *(ushort *)buf1;
323 word2 = *(ushort *)buf2;
Mike Frysinger054ea172012-01-20 09:07:21 +0000324 } else {
Simon Glass0628ab82013-02-24 17:33:15 +0000325 word1 = *(u_char *)buf1;
326 word2 = *(u_char *)buf2;
wdenk38635852002-08-27 05:55:31 +0000327 }
Mike Frysinger054ea172012-01-20 09:07:21 +0000328 if (word1 != word2) {
Simon Glass0628ab82013-02-24 17:33:15 +0000329 ulong offset = buf1 - base;
330
Mike Frysinger054ea172012-01-20 09:07:21 +0000331 printf("%s at 0x%08lx (%#0*lx) != %s at 0x%08lx (%#0*lx)\n",
Simon Glass0628ab82013-02-24 17:33:15 +0000332 type, (ulong)(addr1 + offset), size, word1,
333 type, (ulong)(addr2 + offset), size, word2);
Mike Frysinger054ea172012-01-20 09:07:21 +0000334 rcode = 1;
335 break;
wdenk38635852002-08-27 05:55:31 +0000336 }
Mike Frysinger054ea172012-01-20 09:07:21 +0000337
Simon Glass0628ab82013-02-24 17:33:15 +0000338 buf1 += size;
339 buf2 += size;
Stefan Roeseeaadb442010-09-13 11:10:34 +0200340
341 /* reset watchdog from time to time */
Mike Frysingerfeb12a12012-01-20 09:07:22 +0000342 if ((ngood % (64 << 10)) == 0)
Stefan Roeseeaadb442010-09-13 11:10:34 +0200343 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000344 }
Simon Glass0628ab82013-02-24 17:33:15 +0000345 unmap_sysmem(buf1);
346 unmap_sysmem(buf2);
wdenk38635852002-08-27 05:55:31 +0000347
Mike Frysinger054ea172012-01-20 09:07:21 +0000348 printf("Total of %ld %s(s) were the same\n", ngood, type);
wdenk38635852002-08-27 05:55:31 +0000349 return rcode;
350}
351
Kim Phillips088f1b12012-10-29 13:34:31 +0000352static int do_mem_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000353{
Simon Glass0628ab82013-02-24 17:33:15 +0000354 ulong addr, dest, count, bytes;
wdenk27b207f2003-07-24 23:38:38 +0000355 int size;
Simon Glass0628ab82013-02-24 17:33:15 +0000356 const void *src;
357 void *buf;
wdenk38635852002-08-27 05:55:31 +0000358
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200359 if (argc != 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000360 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000361
362 /* Check for size specification.
363 */
wdenk27b207f2003-07-24 23:38:38 +0000364 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
365 return 1;
wdenk38635852002-08-27 05:55:31 +0000366
367 addr = simple_strtoul(argv[1], NULL, 16);
368 addr += base_address;
369
370 dest = simple_strtoul(argv[2], NULL, 16);
371 dest += base_address;
372
373 count = simple_strtoul(argv[3], NULL, 16);
374
375 if (count == 0) {
376 puts ("Zero length ???\n");
377 return 1;
378 }
379
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200380#ifndef CONFIG_SYS_NO_FLASH
wdenk38635852002-08-27 05:55:31 +0000381 /* check if we are copying to Flash */
wdenk2abbe072003-06-16 23:50:08 +0000382 if ( (addr2info(dest) != NULL)
383#ifdef CONFIG_HAS_DATAFLASH
Kim B. Heino84d0c2f2008-03-03 10:39:13 +0200384 && (!addr_dataflash(dest))
wdenk2abbe072003-06-16 23:50:08 +0000385#endif
386 ) {
wdenk38635852002-08-27 05:55:31 +0000387 int rc;
388
wdenk4b9206e2004-03-23 22:14:11 +0000389 puts ("Copy to Flash... ");
wdenk38635852002-08-27 05:55:31 +0000390
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200391 rc = flash_write ((char *)addr, dest, count*size);
wdenk38635852002-08-27 05:55:31 +0000392 if (rc != 0) {
393 flash_perror (rc);
394 return (1);
395 }
396 puts ("done\n");
397 return 0;
398 }
399#endif
400
wdenk2abbe072003-06-16 23:50:08 +0000401#ifdef CONFIG_HAS_DATAFLASH
402 /* Check if we are copying from RAM or Flash to DataFlash */
403 if (addr_dataflash(dest) && !addr_dataflash(addr)){
404 int rc;
405
wdenk4b9206e2004-03-23 22:14:11 +0000406 puts ("Copy to DataFlash... ");
wdenk2abbe072003-06-16 23:50:08 +0000407
408 rc = write_dataflash (dest, addr, count*size);
409
410 if (rc != 1) {
411 dataflash_perror (rc);
412 return (1);
413 }
414 puts ("done\n");
415 return 0;
416 }
wdenk8bde7f72003-06-27 21:31:46 +0000417
wdenk2abbe072003-06-16 23:50:08 +0000418 /* Check if we are copying from DataFlash to RAM */
Stelian Pop880cc432008-03-26 22:52:35 +0100419 if (addr_dataflash(addr) && !addr_dataflash(dest)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200420#ifndef CONFIG_SYS_NO_FLASH
Stelian Pop880cc432008-03-26 22:52:35 +0100421 && (addr2info(dest) == NULL)
422#endif
423 ){
wdenk5779d8d2003-12-06 23:55:10 +0000424 int rc;
425 rc = read_dataflash(addr, count * size, (char *) dest);
426 if (rc != 1) {
wdenkd4ca31c2004-01-02 14:00:00 +0000427 dataflash_perror (rc);
428 return (1);
429 }
wdenk2abbe072003-06-16 23:50:08 +0000430 return 0;
431 }
432
433 if (addr_dataflash(addr) && addr_dataflash(dest)){
wdenk4b9206e2004-03-23 22:14:11 +0000434 puts ("Unsupported combination of source/destination.\n\r");
wdenk2abbe072003-06-16 23:50:08 +0000435 return 1;
436 }
437#endif
438
Mike Frysinger4c727c72008-02-04 19:26:56 -0500439#ifdef CONFIG_BLACKFIN
440 /* See if we're copying to/from L1 inst */
441 if (addr_bfin_on_chip_mem(dest) || addr_bfin_on_chip_mem(addr)) {
442 memcpy((void *)dest, (void *)addr, count * size);
443 return 0;
444 }
445#endif
446
Simon Glass0628ab82013-02-24 17:33:15 +0000447 bytes = size * count;
448 buf = map_sysmem(addr, bytes);
449 src = map_sysmem(addr, bytes);
wdenk38635852002-08-27 05:55:31 +0000450 while (count-- > 0) {
451 if (size == 4)
Simon Glass0628ab82013-02-24 17:33:15 +0000452 *((ulong *)buf) = *((ulong *)src);
wdenk38635852002-08-27 05:55:31 +0000453 else if (size == 2)
Simon Glass0628ab82013-02-24 17:33:15 +0000454 *((ushort *)buf) = *((ushort *)src);
wdenk38635852002-08-27 05:55:31 +0000455 else
Simon Glass0628ab82013-02-24 17:33:15 +0000456 *((u_char *)buf) = *((u_char *)src);
457 src += size;
458 buf += size;
Stefan Roeseeaadb442010-09-13 11:10:34 +0200459
460 /* reset watchdog from time to time */
461 if ((count % (64 << 10)) == 0)
462 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000463 }
464 return 0;
465}
466
Kim Phillips088f1b12012-10-29 13:34:31 +0000467static int do_mem_base(cmd_tbl_t *cmdtp, int flag, int argc,
468 char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000469{
470 if (argc > 1) {
471 /* Set new base address.
472 */
473 base_address = simple_strtoul(argv[1], NULL, 16);
474 }
475 /* Print the current base address.
476 */
477 printf("Base Address: 0x%08lx\n", base_address);
478 return 0;
479}
480
Kim Phillips088f1b12012-10-29 13:34:31 +0000481static int do_mem_loop(cmd_tbl_t *cmdtp, int flag, int argc,
482 char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000483{
Simon Glass0628ab82013-02-24 17:33:15 +0000484 ulong addr, length, i, bytes;
wdenk27b207f2003-07-24 23:38:38 +0000485 int size;
wdenk38635852002-08-27 05:55:31 +0000486 volatile uint *longp;
487 volatile ushort *shortp;
488 volatile u_char *cp;
Simon Glass0628ab82013-02-24 17:33:15 +0000489 const void *buf;
wdenk38635852002-08-27 05:55:31 +0000490
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200491 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000492 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000493
Robert P. J. Day85de63e2013-02-03 02:29:54 +0000494 /*
495 * Check for a size specification.
wdenk38635852002-08-27 05:55:31 +0000496 * Defaults to long if no or incorrect specification.
497 */
wdenk27b207f2003-07-24 23:38:38 +0000498 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
499 return 1;
wdenk38635852002-08-27 05:55:31 +0000500
501 /* Address is always specified.
502 */
503 addr = simple_strtoul(argv[1], NULL, 16);
504
505 /* Length is the number of objects, not number of bytes.
506 */
507 length = simple_strtoul(argv[2], NULL, 16);
508
Simon Glass0628ab82013-02-24 17:33:15 +0000509 bytes = size * length;
510 buf = map_sysmem(addr, bytes);
511
wdenk38635852002-08-27 05:55:31 +0000512 /* We want to optimize the loops to run as fast as possible.
513 * If we have only one object, just run infinite loops.
514 */
515 if (length == 1) {
516 if (size == 4) {
Simon Glass0628ab82013-02-24 17:33:15 +0000517 longp = (uint *)buf;
wdenk38635852002-08-27 05:55:31 +0000518 for (;;)
519 i = *longp;
520 }
521 if (size == 2) {
Simon Glass0628ab82013-02-24 17:33:15 +0000522 shortp = (ushort *)buf;
wdenk38635852002-08-27 05:55:31 +0000523 for (;;)
524 i = *shortp;
525 }
Simon Glass0628ab82013-02-24 17:33:15 +0000526 cp = (u_char *)buf;
wdenk38635852002-08-27 05:55:31 +0000527 for (;;)
528 i = *cp;
529 }
530
531 if (size == 4) {
532 for (;;) {
Simon Glass0628ab82013-02-24 17:33:15 +0000533 longp = (uint *)buf;
wdenk38635852002-08-27 05:55:31 +0000534 i = length;
535 while (i-- > 0)
Marek Vasutf3b3c3d2011-09-26 02:26:06 +0200536 *longp++;
wdenk38635852002-08-27 05:55:31 +0000537 }
538 }
539 if (size == 2) {
540 for (;;) {
Simon Glass0628ab82013-02-24 17:33:15 +0000541 shortp = (ushort *)buf;
wdenk38635852002-08-27 05:55:31 +0000542 i = length;
543 while (i-- > 0)
Marek Vasutf3b3c3d2011-09-26 02:26:06 +0200544 *shortp++;
wdenk38635852002-08-27 05:55:31 +0000545 }
546 }
547 for (;;) {
Simon Glass0628ab82013-02-24 17:33:15 +0000548 cp = (u_char *)buf;
wdenk38635852002-08-27 05:55:31 +0000549 i = length;
550 while (i-- > 0)
Marek Vasutf3b3c3d2011-09-26 02:26:06 +0200551 *cp++;
wdenk38635852002-08-27 05:55:31 +0000552 }
Simon Glass0628ab82013-02-24 17:33:15 +0000553 unmap_sysmem(buf);
wdenk38635852002-08-27 05:55:31 +0000554}
555
wdenk56523f12004-07-11 17:40:54 +0000556#ifdef CONFIG_LOOPW
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200557int do_mem_loopw (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk56523f12004-07-11 17:40:54 +0000558{
Simon Glass0628ab82013-02-24 17:33:15 +0000559 ulong addr, length, i, data, bytes;
wdenk56523f12004-07-11 17:40:54 +0000560 int size;
561 volatile uint *longp;
562 volatile ushort *shortp;
563 volatile u_char *cp;
Simon Glass0628ab82013-02-24 17:33:15 +0000564 void *buf;
wdenk81050922004-07-11 20:04:51 +0000565
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200566 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000567 return CMD_RET_USAGE;
wdenk56523f12004-07-11 17:40:54 +0000568
Robert P. J. Day85de63e2013-02-03 02:29:54 +0000569 /*
570 * Check for a size specification.
wdenk56523f12004-07-11 17:40:54 +0000571 * Defaults to long if no or incorrect specification.
572 */
573 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
574 return 1;
575
576 /* Address is always specified.
577 */
578 addr = simple_strtoul(argv[1], NULL, 16);
579
580 /* Length is the number of objects, not number of bytes.
581 */
582 length = simple_strtoul(argv[2], NULL, 16);
583
584 /* data to write */
585 data = simple_strtoul(argv[3], NULL, 16);
wdenk81050922004-07-11 20:04:51 +0000586
Simon Glass0628ab82013-02-24 17:33:15 +0000587 bytes = size * length;
588 buf = map_sysmem(addr, bytes);
589
wdenk56523f12004-07-11 17:40:54 +0000590 /* We want to optimize the loops to run as fast as possible.
591 * If we have only one object, just run infinite loops.
592 */
593 if (length == 1) {
594 if (size == 4) {
Simon Glass0628ab82013-02-24 17:33:15 +0000595 longp = (uint *)buf;
wdenk56523f12004-07-11 17:40:54 +0000596 for (;;)
597 *longp = data;
598 }
599 if (size == 2) {
Simon Glass0628ab82013-02-24 17:33:15 +0000600 shortp = (ushort *)buf;
wdenk56523f12004-07-11 17:40:54 +0000601 for (;;)
602 *shortp = data;
603 }
Simon Glass0628ab82013-02-24 17:33:15 +0000604 cp = (u_char *)buf;
wdenk56523f12004-07-11 17:40:54 +0000605 for (;;)
606 *cp = data;
607 }
608
609 if (size == 4) {
610 for (;;) {
Simon Glass0628ab82013-02-24 17:33:15 +0000611 longp = (uint *)buf;
wdenk56523f12004-07-11 17:40:54 +0000612 i = length;
613 while (i-- > 0)
614 *longp++ = data;
615 }
616 }
617 if (size == 2) {
618 for (;;) {
Simon Glass0628ab82013-02-24 17:33:15 +0000619 shortp = (ushort *)buf;
wdenk56523f12004-07-11 17:40:54 +0000620 i = length;
621 while (i-- > 0)
622 *shortp++ = data;
623 }
624 }
625 for (;;) {
Simon Glass0628ab82013-02-24 17:33:15 +0000626 cp = (u_char *)buf;
wdenk56523f12004-07-11 17:40:54 +0000627 i = length;
628 while (i-- > 0)
629 *cp++ = data;
630 }
631}
632#endif /* CONFIG_LOOPW */
633
Simon Glass5512d5b2013-02-28 17:47:14 +0000634static ulong mem_test_alt(vu_long *buf, ulong start_addr, ulong end_addr,
635 vu_long *dummy)
wdenk38635852002-08-27 05:55:31 +0000636{
Simon Glassc9638f52013-02-24 17:33:16 +0000637 vu_long *addr;
Simon Glassc9638f52013-02-24 17:33:16 +0000638 ulong errs = 0;
639 ulong val, readback;
640 int j;
Simon Glassc9638f52013-02-24 17:33:16 +0000641 vu_long offset;
642 vu_long test_offset;
643 vu_long pattern;
644 vu_long temp;
645 vu_long anti_pattern;
646 vu_long num_words;
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 Glass5512d5b2013-02-28 17:47:14 +0000658 num_words = (end_addr - start_addr) / sizeof(vu_long);
Simon Glass8c86bbe2013-02-24 17:33:20 +0000659
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000660 /*
661 * Data line test: write a pattern to the first
662 * location, write the 1's complement to a 'parking'
663 * address (changes the state of the data bus so a
664 * floating bus doesn't give a false OK), and then
665 * read the value back. Note that we read it back
666 * into a variable because the next time we read it,
667 * it might be right (been there, tough to explain to
668 * the quality guys why it prints a failure when the
669 * "is" and "should be" are obviously the same in the
670 * error message).
671 *
672 * Rather than exhaustively testing, we test some
673 * patterns by shifting '1' bits through a field of
674 * '0's and '0' bits through a field of '1's (i.e.
675 * pattern and ~pattern).
676 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000677 addr = buf;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000678 for (j = 0; j < sizeof(bitpattern) / sizeof(bitpattern[0]); j++) {
679 val = bitpattern[j];
680 for (; val != 0; val <<= 1) {
Simon Glass5512d5b2013-02-28 17:47:14 +0000681 *addr = val;
Simon Glassc9638f52013-02-24 17:33:16 +0000682 *dummy = ~val; /* clear the test data off the bus */
wdenk38635852002-08-27 05:55:31 +0000683 readback = *addr;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000684 if (readback != val) {
Simon Glassc9638f52013-02-24 17:33:16 +0000685 printf("FAILURE (data line): "
686 "expected %08lx, actual %08lx\n",
687 val, readback);
688 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000689 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000690 return -1;
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++;
Simon Glassc44d4382013-02-24 17:33:19 +0000700 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000701 return -1;
wdenk38635852002-08-27 05:55:31 +0000702 }
wdenk38635852002-08-27 05:55:31 +0000703 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000704 }
wdenk38635852002-08-27 05:55:31 +0000705
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000706 /*
707 * Based on code whose Original Author and Copyright
708 * information follows: Copyright (c) 1998 by Michael
709 * Barr. This software is placed into the public
710 * domain and may be used for any purpose. However,
711 * this notice must not be changed or removed and no
712 * warranty is either expressed or implied by its
713 * publication or distribution.
714 */
wdenk38635852002-08-27 05:55:31 +0000715
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000716 /*
717 * Address line test
wdenk38635852002-08-27 05:55:31 +0000718
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000719 * Description: Test the address bus wiring in a
720 * memory region by performing a walking
721 * 1's test on the relevant bits of the
722 * address and checking for aliasing.
723 * This test will find single-bit
724 * address failures such as stuck-high,
725 * stuck-low, and shorted pins. The base
726 * address and size of the region are
727 * selected by the caller.
wdenk38635852002-08-27 05:55:31 +0000728
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000729 * Notes: For best results, the selected base
730 * address should have enough LSB 0's to
731 * guarantee single address bit changes.
732 * For example, to test a 64-Kbyte
733 * region, select a base address on a
734 * 64-Kbyte boundary. Also, select the
735 * region size as a power-of-two if at
736 * all possible.
737 *
738 * Returns: 0 if the test succeeds, 1 if the test fails.
739 */
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000740 pattern = (vu_long) 0xaaaaaaaa;
741 anti_pattern = (vu_long) 0x55555555;
wdenk38635852002-08-27 05:55:31 +0000742
Simon Glass5512d5b2013-02-28 17:47:14 +0000743 debug("%s:%d: length = 0x%.8lx\n", __func__, __LINE__, num_words);
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000744 /*
745 * Write the default pattern at each of the
746 * power-of-two offsets.
747 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000748 for (offset = 1; offset < num_words; offset <<= 1)
749 addr[offset] = pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000750
751 /*
752 * Check for address bits stuck high.
753 */
754 test_offset = 0;
Simon Glass5512d5b2013-02-28 17:47:14 +0000755 addr[test_offset] = anti_pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000756
Simon Glass5512d5b2013-02-28 17:47:14 +0000757 for (offset = 1; offset < num_words; offset <<= 1) {
758 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000759 if (temp != pattern) {
Simon Glassc9638f52013-02-24 17:33:16 +0000760 printf("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000761 " expected 0x%.8lx, actual 0x%.8lx\n",
Simon Glass5512d5b2013-02-28 17:47:14 +0000762 start_addr + offset, pattern, temp);
Paul Gortmaker87b22b72009-10-02 18:18:33 -0400763 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000764 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000765 return -1;
wdenk38635852002-08-27 05:55:31 +0000766 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000767 }
Simon Glass5512d5b2013-02-28 17:47:14 +0000768 addr[test_offset] = pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000769 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000770
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000771 /*
772 * Check for addr bits stuck low or shorted.
773 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000774 for (test_offset = 1; test_offset < num_words; test_offset <<= 1) {
775 addr[test_offset] = anti_pattern;
wdenk38635852002-08-27 05:55:31 +0000776
Simon Glass5512d5b2013-02-28 17:47:14 +0000777 for (offset = 1; offset < num_words; offset <<= 1) {
778 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000779 if ((temp != pattern) && (offset != test_offset)) {
780 printf("\nFAILURE: Address bit stuck low or"
781 " shorted @ 0x%.8lx: expected 0x%.8lx,"
782 " actual 0x%.8lx\n",
Simon Glass5512d5b2013-02-28 17:47:14 +0000783 start_addr + offset, pattern, temp);
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000784 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000785 if (ctrlc())
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000786 return -1;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000787 }
wdenk38635852002-08-27 05:55:31 +0000788 }
Simon Glass5512d5b2013-02-28 17:47:14 +0000789 addr[test_offset] = pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000790 }
wdenk38635852002-08-27 05:55:31 +0000791
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000792 /*
793 * Description: Test the integrity of a physical
794 * memory device by performing an
795 * increment/decrement test over the
796 * entire region. In the process every
797 * storage bit in the device is tested
798 * as a zero and a one. The base address
799 * and the size of the region are
800 * selected by the caller.
801 *
802 * Returns: 0 if the test succeeds, 1 if the test fails.
803 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000804 num_words++;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000805
806 /*
807 * Fill memory with a known pattern.
808 */
809 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
810 WATCHDOG_RESET();
Simon Glass5512d5b2013-02-28 17:47:14 +0000811 addr[offset] = pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000812 }
813
814 /*
815 * Check each location and invert it for the second pass.
816 */
817 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
818 WATCHDOG_RESET();
Simon Glass5512d5b2013-02-28 17:47:14 +0000819 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000820 if (temp != pattern) {
Simon Glassc9638f52013-02-24 17:33:16 +0000821 printf("\nFAILURE (read/write) @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000822 " expected 0x%.8lx, actual 0x%.8lx)\n",
Simon Glass5512d5b2013-02-28 17:47:14 +0000823 start_addr + offset, pattern, temp);
Paul Gortmaker87b22b72009-10-02 18:18:33 -0400824 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000825 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000826 return -1;
wdenk38635852002-08-27 05:55:31 +0000827 }
828
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000829 anti_pattern = ~pattern;
Simon Glass5512d5b2013-02-28 17:47:14 +0000830 addr[offset] = anti_pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000831 }
832
833 /*
834 * Check each location for the inverted pattern and zero it.
835 */
836 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
837 WATCHDOG_RESET();
838 anti_pattern = ~pattern;
Simon Glass5512d5b2013-02-28 17:47:14 +0000839 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000840 if (temp != anti_pattern) {
Simon Glassc9638f52013-02-24 17:33:16 +0000841 printf("\nFAILURE (read/write): @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000842 " expected 0x%.8lx, actual 0x%.8lx)\n",
Simon Glass5512d5b2013-02-28 17:47:14 +0000843 start_addr + offset, anti_pattern, temp);
Paul Gortmaker87b22b72009-10-02 18:18:33 -0400844 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000845 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000846 return -1;
wdenk38635852002-08-27 05:55:31 +0000847 }
Simon Glass5512d5b2013-02-28 17:47:14 +0000848 addr[offset] = 0;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000849 }
Simon Glass51209b12013-02-24 17:33:17 +0000850
851 return 0;
Simon Glassc9638f52013-02-24 17:33:16 +0000852}
wdenk38635852002-08-27 05:55:31 +0000853
Simon Glass5512d5b2013-02-28 17:47:14 +0000854static ulong mem_test_quick(vu_long *buf, ulong start_addr, ulong end_addr,
855 vu_long pattern, int iteration)
Simon Glassc9638f52013-02-24 17:33:16 +0000856{
Simon Glass5512d5b2013-02-28 17:47:14 +0000857 vu_long *end;
Simon Glassc9638f52013-02-24 17:33:16 +0000858 vu_long *addr;
Simon Glassc9638f52013-02-24 17:33:16 +0000859 ulong errs = 0;
Simon Glass5512d5b2013-02-28 17:47:14 +0000860 ulong incr, length;
Simon Glassc9638f52013-02-24 17:33:16 +0000861 ulong val, readback;
862
Simon Glass51209b12013-02-24 17:33:17 +0000863 /* Alternate the pattern */
wdenk38635852002-08-27 05:55:31 +0000864 incr = 1;
Simon Glass51209b12013-02-24 17:33:17 +0000865 if (iteration & 1) {
866 incr = -incr;
867 /*
868 * Flip the pattern each time to make lots of zeros and
869 * then, the next time, lots of ones. We decrement
870 * the "negative" patterns and increment the "positive"
871 * patterns to preserve this feature.
872 */
873 if (pattern & 0x80000000)
874 pattern = -pattern; /* complement & increment */
875 else
876 pattern = ~pattern;
877 }
Simon Glass5512d5b2013-02-28 17:47:14 +0000878 length = (end_addr - start_addr) / sizeof(ulong);
879 end = buf + length;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000880 printf("\rPattern %08lX Writing..."
881 "%12s"
882 "\b\b\b\b\b\b\b\b\b\b",
883 pattern, "");
wdenk38635852002-08-27 05:55:31 +0000884
Simon Glass5512d5b2013-02-28 17:47:14 +0000885 for (addr = buf, val = pattern; addr < end; addr++) {
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000886 WATCHDOG_RESET();
887 *addr = val;
888 val += incr;
889 }
wdenk38635852002-08-27 05:55:31 +0000890
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000891 puts("Reading...");
wdenk38635852002-08-27 05:55:31 +0000892
Simon Glass5512d5b2013-02-28 17:47:14 +0000893 for (addr = buf, val = pattern; addr < end; addr++) {
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000894 WATCHDOG_RESET();
895 readback = *addr;
896 if (readback != val) {
Simon Glass5512d5b2013-02-28 17:47:14 +0000897 ulong offset = addr - buf;
898
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000899 printf("\nMem error @ 0x%08X: "
900 "found %08lX, expected %08lX\n",
Simon Glass5512d5b2013-02-28 17:47:14 +0000901 (uint)(uintptr_t)(start_addr + offset),
902 readback, val);
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000903 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000904 if (ctrlc())
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000905 return -1;
wdenk38635852002-08-27 05:55:31 +0000906 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000907 val += incr;
908 }
wdenk38635852002-08-27 05:55:31 +0000909
Simon Glass51209b12013-02-24 17:33:17 +0000910 return 0;
Simon Glassc9638f52013-02-24 17:33:16 +0000911}
912
913/*
914 * Perform a memory test. A more complete alternative test can be
915 * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
916 * interrupted by ctrl-c or by a failure of one of the sub-tests.
917 */
918static int do_mem_mtest(cmd_tbl_t *cmdtp, int flag, int argc,
919 char * const argv[])
920{
Simon Glass8c86bbe2013-02-24 17:33:20 +0000921 ulong start, end;
Simon Glass5512d5b2013-02-28 17:47:14 +0000922 vu_long *buf, *dummy;
Simon Glassc9638f52013-02-24 17:33:16 +0000923 int iteration_limit;
924 int ret;
Simon Glass51209b12013-02-24 17:33:17 +0000925 ulong errs = 0; /* number of errors, or -1 if interrupted */
Simon Glassc9638f52013-02-24 17:33:16 +0000926 ulong pattern;
Simon Glass51209b12013-02-24 17:33:17 +0000927 int iteration;
Simon Glassc9638f52013-02-24 17:33:16 +0000928#if defined(CONFIG_SYS_ALT_MEMTEST)
929 const int alt_test = 1;
930#else
931 const int alt_test = 0;
wdenk38635852002-08-27 05:55:31 +0000932#endif
Simon Glassc9638f52013-02-24 17:33:16 +0000933
934 if (argc > 1)
Simon Glass8c86bbe2013-02-24 17:33:20 +0000935 start = simple_strtoul(argv[1], NULL, 16);
Simon Glassc9638f52013-02-24 17:33:16 +0000936 else
Simon Glass8c86bbe2013-02-24 17:33:20 +0000937 start = CONFIG_SYS_MEMTEST_START;
Simon Glassc9638f52013-02-24 17:33:16 +0000938
939 if (argc > 2)
Simon Glass8c86bbe2013-02-24 17:33:20 +0000940 end = simple_strtoul(argv[2], NULL, 16);
Simon Glassc9638f52013-02-24 17:33:16 +0000941 else
Simon Glass8c86bbe2013-02-24 17:33:20 +0000942 end = CONFIG_SYS_MEMTEST_END;
Simon Glassc9638f52013-02-24 17:33:16 +0000943
944 if (argc > 3)
945 pattern = (ulong)simple_strtoul(argv[3], NULL, 16);
946 else
947 pattern = 0;
948
949 if (argc > 4)
950 iteration_limit = (ulong)simple_strtoul(argv[4], NULL, 16);
951 else
952 iteration_limit = 0;
953
Simon Glass8c86bbe2013-02-24 17:33:20 +0000954 printf("Testing %08x ... %08x:\n", (uint)start, (uint)end);
955 debug("%s:%d: start %#08lx end %#08lx\n", __func__, __LINE__,
956 start, end);
Simon Glass51209b12013-02-24 17:33:17 +0000957
Simon Glass5512d5b2013-02-28 17:47:14 +0000958 buf = map_sysmem(start, end - start);
959 dummy = map_sysmem(CONFIG_SYS_MEMTEST_SCRATCH, sizeof(vu_long));
Simon Glass51209b12013-02-24 17:33:17 +0000960 for (iteration = 0;
961 !iteration_limit || iteration < iteration_limit;
962 iteration++) {
963 if (ctrlc()) {
Simon Glass51209b12013-02-24 17:33:17 +0000964 errs = -1UL;
965 break;
966 }
967
968 printf("Iteration: %6d\r", iteration + 1);
969 debug("\n");
Simon Glass5512d5b2013-02-28 17:47:14 +0000970 if (alt_test) {
971 errs = mem_test_alt(buf, start, end, dummy);
972 } else {
973 errs = mem_test_quick(buf, start, end, pattern,
974 iteration);
975 }
976 if (errs == -1UL)
977 break;
978 }
979
980 /*
981 * Work-around for eldk-4.2 which gives this warning if we try to
982 * case in the unmap_sysmem() call:
983 * warning: initialization discards qualifiers from pointer target type
984 */
985 {
986 void *vbuf = (void *)buf;
987 void *vdummy = (void *)dummy;
988
989 unmap_sysmem(vbuf);
990 unmap_sysmem(vdummy);
Simon Glass51209b12013-02-24 17:33:17 +0000991 }
992
993 if (errs == -1UL) {
Simon Glassc44d4382013-02-24 17:33:19 +0000994 /* Memory test was aborted - write a newline to finish off */
995 putc('\n');
Simon Glass51209b12013-02-24 17:33:17 +0000996 ret = 1;
997 } else {
998 printf("Tested %d iteration(s) with %lu errors.\n",
999 iteration, errs);
1000 ret = errs != 0;
1001 }
Simon Glassc9638f52013-02-24 17:33:16 +00001002
1003 return ret; /* not reached */
wdenk38635852002-08-27 05:55:31 +00001004}
1005
1006
1007/* Modify memory.
1008 *
1009 * Syntax:
1010 * mm{.b, .w, .l} {addr}
1011 * nm{.b, .w, .l} {addr}
1012 */
1013static int
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001014mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +00001015{
wdenk27b207f2003-07-24 23:38:38 +00001016 ulong addr, i;
1017 int nbytes, size;
Simon Glass0628ab82013-02-24 17:33:15 +00001018 void *ptr = NULL;
wdenk38635852002-08-27 05:55:31 +00001019
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001020 if (argc != 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001021 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +00001022
1023#ifdef CONFIG_BOOT_RETRY_TIME
1024 reset_cmd_timeout(); /* got a good command to get here */
1025#endif
1026 /* We use the last specified parameters, unless new ones are
1027 * entered.
1028 */
1029 addr = mm_last_addr;
1030 size = mm_last_size;
1031
1032 if ((flag & CMD_FLAG_REPEAT) == 0) {
1033 /* New command specified. Check for a size specification.
1034 * Defaults to long if no or incorrect specification.
1035 */
wdenk27b207f2003-07-24 23:38:38 +00001036 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
1037 return 1;
wdenk38635852002-08-27 05:55:31 +00001038
1039 /* Address is specified since argc > 1
1040 */
1041 addr = simple_strtoul(argv[1], NULL, 16);
1042 addr += base_address;
1043 }
1044
wdenk2abbe072003-06-16 23:50:08 +00001045#ifdef CONFIG_HAS_DATAFLASH
1046 if (addr_dataflash(addr)){
wdenk4b9206e2004-03-23 22:14:11 +00001047 puts ("Can't modify DataFlash in place. Use cp instead.\n\r");
wdenk2abbe072003-06-16 23:50:08 +00001048 return 0;
1049 }
1050#endif
1051
Mike Frysinger4c727c72008-02-04 19:26:56 -05001052#ifdef CONFIG_BLACKFIN
1053 if (addr_bfin_on_chip_mem(addr)) {
1054 puts ("Can't modify L1 instruction in place. Use cp instead.\n\r");
1055 return 0;
1056 }
1057#endif
1058
wdenk38635852002-08-27 05:55:31 +00001059 /* Print the address, followed by value. Then accept input for
1060 * the next value. A non-converted value exits.
1061 */
1062 do {
Simon Glass0628ab82013-02-24 17:33:15 +00001063 ptr = map_sysmem(addr, size);
wdenk38635852002-08-27 05:55:31 +00001064 printf("%08lx:", addr);
1065 if (size == 4)
Simon Glass0628ab82013-02-24 17:33:15 +00001066 printf(" %08x", *((uint *)ptr));
wdenk38635852002-08-27 05:55:31 +00001067 else if (size == 2)
Simon Glass0628ab82013-02-24 17:33:15 +00001068 printf(" %04x", *((ushort *)ptr));
wdenk38635852002-08-27 05:55:31 +00001069 else
Simon Glass0628ab82013-02-24 17:33:15 +00001070 printf(" %02x", *((u_char *)ptr));
wdenk38635852002-08-27 05:55:31 +00001071
1072 nbytes = readline (" ? ");
1073 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
1074 /* <CR> pressed as only input, don't modify current
1075 * location and move to next. "-" pressed will go back.
1076 */
1077 if (incrflag)
1078 addr += nbytes ? -size : size;
1079 nbytes = 1;
1080#ifdef CONFIG_BOOT_RETRY_TIME
1081 reset_cmd_timeout(); /* good enough to not time out */
1082#endif
1083 }
1084#ifdef CONFIG_BOOT_RETRY_TIME
1085 else if (nbytes == -2) {
1086 break; /* timed out, exit the command */
1087 }
1088#endif
1089 else {
1090 char *endp;
1091 i = simple_strtoul(console_buffer, &endp, 16);
1092 nbytes = endp - console_buffer;
1093 if (nbytes) {
1094#ifdef CONFIG_BOOT_RETRY_TIME
1095 /* good enough to not time out
1096 */
1097 reset_cmd_timeout();
1098#endif
1099 if (size == 4)
Simon Glass0628ab82013-02-24 17:33:15 +00001100 *((uint *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001101 else if (size == 2)
Simon Glass0628ab82013-02-24 17:33:15 +00001102 *((ushort *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001103 else
Simon Glass0628ab82013-02-24 17:33:15 +00001104 *((u_char *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001105 if (incrflag)
1106 addr += size;
1107 }
1108 }
1109 } while (nbytes);
Simon Glass0628ab82013-02-24 17:33:15 +00001110 if (ptr)
1111 unmap_sysmem(ptr);
wdenk38635852002-08-27 05:55:31 +00001112
1113 mm_last_addr = addr;
1114 mm_last_size = size;
1115 return 0;
1116}
1117
Mike Frysinger710b9932010-12-21 14:19:51 -05001118#ifdef CONFIG_CMD_CRC32
1119
Kim Phillips088f1b12012-10-29 13:34:31 +00001120static int do_mem_crc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +00001121{
Simon Glassd20a40d2013-02-24 20:30:22 +00001122 int flags = 0;
1123 int ac;
1124 char * const *av;
wdenk38635852002-08-27 05:55:31 +00001125
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001126 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001127 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +00001128
wdenkc26e4542004-04-18 10:13:26 +00001129 av = argv + 1;
1130 ac = argc - 1;
Simon Glassd20a40d2013-02-24 20:30:22 +00001131#ifdef CONFIG_HASH_VERIFY
wdenkc26e4542004-04-18 10:13:26 +00001132 if (strcmp(*av, "-v") == 0) {
Simon Glassd20a40d2013-02-24 20:30:22 +00001133 flags |= HASH_FLAG_VERIFY;
wdenkc26e4542004-04-18 10:13:26 +00001134 av++;
1135 ac--;
wdenkc26e4542004-04-18 10:13:26 +00001136 }
Simon Glassd20a40d2013-02-24 20:30:22 +00001137#endif
wdenkc26e4542004-04-18 10:13:26 +00001138
Simon Glassd20a40d2013-02-24 20:30:22 +00001139 return hash_command("crc32", flags, cmdtp, flag, ac, av);
wdenkc26e4542004-04-18 10:13:26 +00001140}
wdenkc26e4542004-04-18 10:13:26 +00001141
Mike Frysinger710b9932010-12-21 14:19:51 -05001142#endif
1143
wdenk8bde7f72003-06-27 21:31:46 +00001144/**************************************************/
wdenk0d498392003-07-01 21:06:45 +00001145U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001146 md, 3, 1, do_mem_md,
Peter Tyser2fb26042009-01-27 18:03:12 -06001147 "memory display",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001148 "[.b, .w, .l] address [# of objects]"
wdenk8bde7f72003-06-27 21:31:46 +00001149);
1150
1151
wdenk0d498392003-07-01 21:06:45 +00001152U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001153 mm, 2, 1, do_mem_mm,
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001154 "memory modify (auto-incrementing address)",
1155 "[.b, .w, .l] address"
wdenk8bde7f72003-06-27 21:31:46 +00001156);
1157
1158
wdenk0d498392003-07-01 21:06:45 +00001159U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001160 nm, 2, 1, do_mem_nm,
Peter Tyser2fb26042009-01-27 18:03:12 -06001161 "memory modify (constant address)",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001162 "[.b, .w, .l] address"
wdenk8bde7f72003-06-27 21:31:46 +00001163);
1164
wdenk0d498392003-07-01 21:06:45 +00001165U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001166 mw, 4, 1, do_mem_mw,
Peter Tyser2fb26042009-01-27 18:03:12 -06001167 "memory write (fill)",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001168 "[.b, .w, .l] address value [count]"
wdenk8bde7f72003-06-27 21:31:46 +00001169);
1170
wdenk0d498392003-07-01 21:06:45 +00001171U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001172 cp, 4, 1, do_mem_cp,
Peter Tyser2fb26042009-01-27 18:03:12 -06001173 "memory copy",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001174 "[.b, .w, .l] source target count"
wdenk8bde7f72003-06-27 21:31:46 +00001175);
1176
wdenk0d498392003-07-01 21:06:45 +00001177U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001178 cmp, 4, 1, do_mem_cmp,
Peter Tyser2fb26042009-01-27 18:03:12 -06001179 "memory compare",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001180 "[.b, .w, .l] addr1 addr2 count"
wdenk8bde7f72003-06-27 21:31:46 +00001181);
1182
Mike Frysinger710b9932010-12-21 14:19:51 -05001183#ifdef CONFIG_CMD_CRC32
1184
wdenkc26e4542004-04-18 10:13:26 +00001185#ifndef CONFIG_CRC32_VERIFY
1186
wdenk0d498392003-07-01 21:06:45 +00001187U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001188 crc32, 4, 1, do_mem_crc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001189 "checksum calculation",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001190 "address count [addr]\n - compute CRC32 checksum [save at addr]"
wdenk8bde7f72003-06-27 21:31:46 +00001191);
1192
wdenkc26e4542004-04-18 10:13:26 +00001193#else /* CONFIG_CRC32_VERIFY */
1194
1195U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001196 crc32, 5, 1, do_mem_crc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001197 "checksum calculation",
wdenkc26e4542004-04-18 10:13:26 +00001198 "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001199 "-v address count crc\n - verify crc of memory area"
wdenkc26e4542004-04-18 10:13:26 +00001200);
1201
1202#endif /* CONFIG_CRC32_VERIFY */
1203
Mike Frysinger710b9932010-12-21 14:19:51 -05001204#endif
1205
Simon Glass15a33e42012-11-30 13:01:20 +00001206#ifdef CONFIG_CMD_MEMINFO
1207__weak void board_show_dram(ulong size)
1208{
1209 puts("DRAM: ");
1210 print_size(size, "\n");
1211}
1212
1213static int do_mem_info(cmd_tbl_t *cmdtp, int flag, int argc,
1214 char * const argv[])
1215{
1216 board_show_dram(gd->ram_size);
1217
1218 return 0;
1219}
1220#endif
1221
wdenk0d498392003-07-01 21:06:45 +00001222U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001223 base, 2, 1, do_mem_base,
Peter Tyser2fb26042009-01-27 18:03:12 -06001224 "print or set address offset",
wdenk8bde7f72003-06-27 21:31:46 +00001225 "\n - print address offset for memory commands\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001226 "base off\n - set address offset for memory commands to 'off'"
wdenk8bde7f72003-06-27 21:31:46 +00001227);
1228
wdenk0d498392003-07-01 21:06:45 +00001229U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001230 loop, 3, 1, do_mem_loop,
Peter Tyser2fb26042009-01-27 18:03:12 -06001231 "infinite loop on address range",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001232 "[.b, .w, .l] address number_of_objects"
wdenk8bde7f72003-06-27 21:31:46 +00001233);
1234
wdenk56523f12004-07-11 17:40:54 +00001235#ifdef CONFIG_LOOPW
1236U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001237 loopw, 4, 1, do_mem_loopw,
Peter Tyser2fb26042009-01-27 18:03:12 -06001238 "infinite write loop on address range",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001239 "[.b, .w, .l] address number_of_objects data_to_write"
wdenk56523f12004-07-11 17:40:54 +00001240);
1241#endif /* CONFIG_LOOPW */
1242
wdenk0d498392003-07-01 21:06:45 +00001243U_BOOT_CMD(
Dirk Eibachb6fc6fd2008-12-16 14:51:56 +01001244 mtest, 5, 1, do_mem_mtest,
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001245 "simple RAM read/write test",
1246 "[start [end [pattern [iterations]]]]"
wdenk8bde7f72003-06-27 21:31:46 +00001247);
1248
stroese4aaf29b2004-12-16 17:42:39 +00001249#ifdef CONFIG_MX_CYCLIC
1250U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001251 mdc, 4, 1, do_mem_mdc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001252 "memory display cyclic",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001253 "[.b, .w, .l] address count delay(ms)"
stroese4aaf29b2004-12-16 17:42:39 +00001254);
1255
1256U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001257 mwc, 4, 1, do_mem_mwc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001258 "memory write cyclic",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001259 "[.b, .w, .l] address value delay(ms)"
stroese4aaf29b2004-12-16 17:42:39 +00001260);
1261#endif /* CONFIG_MX_CYCLIC */
Simon Glass15a33e42012-11-30 13:01:20 +00001262
1263#ifdef CONFIG_CMD_MEMINFO
1264U_BOOT_CMD(
1265 meminfo, 3, 1, do_mem_info,
1266 "display memory information",
1267 ""
1268);
1269#endif