blob: 0e7a4c67c21c9f877fa7e035d757426c13e58bc3 [file] [log] [blame]
wdenk38635852002-08-27 05:55:31 +00001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24/*
25 * Memory Functions
26 *
27 * Copied from FADS ROM, Dan Malek (dmalek@jlc.net)
28 */
29
30#include <common.h>
31#include <command.h>
wdenk381669a2003-06-16 23:50:08 +000032#ifdef CONFIG_HAS_DATAFLASH
33#include <dataflash.h>
34#endif
Sergei Poselenov474dbb82008-04-09 16:09:41 +020035#include <watchdog.h>
Simon Glasse6d0ca22013-02-24 17:33:15 +000036#include <asm/io.h>
Simon Glasseacd14f2012-11-30 13:01:20 +000037#include <linux/compiler.h>
38
39DECLARE_GLOBAL_DATA_PTR;
wdenk38635852002-08-27 05:55:31 +000040
Wolfgang Denk6262d0212010-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 Frysinger1a4af8c2010-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 Phillipsdc00a682012-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{
wdenk874ac262003-07-24 23:38:38 +000060 ulong addr, length;
Grant Likely02a00782007-02-20 09:05:00 +010061#if defined(CONFIG_HAS_DATAFLASH)
62 ulong nbytes, linebytes;
63#endif
wdenk874ac262003-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 Denk3b683112010-07-17 01:06:04 +020074 if (argc < 2)
Simon Glassa06dfc72011-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 */
wdenk874ac262003-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 Likely02a00782007-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 Likely02a00782007-02-20 09:05:00 +0100105 void* p;
wdenk38635852002-08-27 05:55:31 +0000106 linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
wdenk381669a2003-06-16 23:50:08 +0000107
Grant Likely02a00782007-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);
wdenk57b2d802003-06-27 21:31:46 +0000111
wdenk38635852002-08-27 05:55:31 +0000112 nbytes -= linebytes;
Grant Likely02a00782007-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 Likely02a00782007-02-20 09:05:00 +0100119#else
Mike Frysingere578d682008-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 Glasse6d0ca22013-02-24 17:33:15 +0000142 ulong bytes = size * length;
143 const void *buf = map_sysmem(addr, bytes);
144
Mike Frysingere578d682008-02-04 19:26:56 -0500145 /* Print the lines. */
Simon Glasse6d0ca22013-02-24 17:33:15 +0000146 print_buffer(addr, buf, size, length, DISP_LINE_LEN / size);
147 addr += bytes;
148 unmap_sysmem(buf);
Mike Frysingere578d682008-02-04 19:26:56 -0500149 }
Grant Likely02a00782007-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 Phillipsdc00a682012-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 Phillipsdc00a682012-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 Phillipsdc00a682012-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{
wdenk874ac262003-07-24 23:38:38 +0000169 ulong addr, writeval, count;
170 int size;
Simon Glasse6d0ca22013-02-24 17:33:15 +0000171 void *buf;
172 ulong bytes;
wdenk38635852002-08-27 05:55:31 +0000173
Wolfgang Denk3b683112010-07-17 01:06:04 +0200174 if ((argc < 3) || (argc > 4))
Simon Glassa06dfc72011-12-10 08:44:01 +0000175 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000176
177 /* Check for size specification.
178 */
wdenk874ac262003-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 Glasse6d0ca22013-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 Glasse6d0ca22013-02-24 17:33:15 +0000202 *((ulong *)buf) = (ulong)writeval;
wdenk38635852002-08-27 05:55:31 +0000203 else if (size == 2)
Simon Glasse6d0ca22013-02-24 17:33:15 +0000204 *((ushort *)buf) = (ushort)writeval;
wdenk38635852002-08-27 05:55:31 +0000205 else
Simon Glasse6d0ca22013-02-24 17:33:15 +0000206 *((u_char *)buf) = (u_char)writeval;
207 buf += size;
wdenk38635852002-08-27 05:55:31 +0000208 }
Simon Glasse6d0ca22013-02-24 17:33:15 +0000209 unmap_sysmem(buf);
wdenk38635852002-08-27 05:55:31 +0000210 return 0;
211}
212
stroesefb5e0ac2004-12-16 17:42:39 +0000213#ifdef CONFIG_MX_CYCLIC
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200214int do_mem_mdc ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
stroesefb5e0ac2004-12-16 17:42:39 +0000215{
216 int i;
217 ulong count;
218
Wolfgang Denk3b683112010-07-17 01:06:04 +0200219 if (argc < 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000220 return CMD_RET_USAGE;
stroesefb5e0ac2004-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 Denk6262d0212010-06-28 22:00:46 +0200241int do_mem_mwc ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
stroesefb5e0ac2004-12-16 17:42:39 +0000242{
243 int i;
244 ulong count;
245
Wolfgang Denk3b683112010-07-17 01:06:04 +0200246 if (argc < 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000247 return CMD_RET_USAGE;
stroesefb5e0ac2004-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 Phillipsdc00a682012-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 Glasse6d0ca22013-02-24 17:33:15 +0000271 ulong addr1, addr2, count, ngood, bytes;
wdenk874ac262003-07-24 23:38:38 +0000272 int size;
wdenk38635852002-08-27 05:55:31 +0000273 int rcode = 0;
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000274 const char *type;
Simon Glasse6d0ca22013-02-24 17:33:15 +0000275 const void *buf1, *buf2, *base;
wdenk38635852002-08-27 05:55:31 +0000276
Wolfgang Denk3b683112010-07-17 01:06:04 +0200277 if (argc != 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000278 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000279
280 /* Check for size specification.
281 */
wdenk874ac262003-07-24 23:38:38 +0000282 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
283 return 1;
Mike Frysinger62a7ea72012-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
wdenk381669a2003-06-16 23:50:08 +0000294#ifdef CONFIG_HAS_DATAFLASH
295 if (addr_dataflash(addr1) | addr_dataflash(addr2)){
wdenk42c05472004-03-23 22:14:11 +0000296 puts ("Comparison with DataFlash space not supported.\n\r");
wdenk381669a2003-06-16 23:50:08 +0000297 return 0;
298 }
299#endif
300
Mike Frysingere578d682008-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 Glasse6d0ca22013-02-24 17:33:15 +0000308 bytes = size * count;
309 base = buf1 = map_sysmem(addr1, bytes);
310 buf2 = map_sysmem(addr2, bytes);
Mike Frysingera66afbd2012-01-20 09:07:22 +0000311 for (ngood = 0; ngood < count; ++ngood) {
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000312 ulong word1, word2;
wdenk38635852002-08-27 05:55:31 +0000313 if (size == 4) {
Simon Glasse6d0ca22013-02-24 17:33:15 +0000314 word1 = *(ulong *)buf1;
315 word2 = *(ulong *)buf2;
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000316 } else if (size == 2) {
Simon Glasse6d0ca22013-02-24 17:33:15 +0000317 word1 = *(ushort *)buf1;
318 word2 = *(ushort *)buf2;
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000319 } else {
Simon Glasse6d0ca22013-02-24 17:33:15 +0000320 word1 = *(u_char *)buf1;
321 word2 = *(u_char *)buf2;
wdenk38635852002-08-27 05:55:31 +0000322 }
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000323 if (word1 != word2) {
Simon Glasse6d0ca22013-02-24 17:33:15 +0000324 ulong offset = buf1 - base;
325
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000326 printf("%s at 0x%08lx (%#0*lx) != %s at 0x%08lx (%#0*lx)\n",
Simon Glasse6d0ca22013-02-24 17:33:15 +0000327 type, (ulong)(addr1 + offset), size, word1,
328 type, (ulong)(addr2 + offset), size, word2);
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000329 rcode = 1;
330 break;
wdenk38635852002-08-27 05:55:31 +0000331 }
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000332
Simon Glasse6d0ca22013-02-24 17:33:15 +0000333 buf1 += size;
334 buf2 += size;
Stefan Roesed4ef82b2010-09-13 11:10:34 +0200335
336 /* reset watchdog from time to time */
Mike Frysingera66afbd2012-01-20 09:07:22 +0000337 if ((ngood % (64 << 10)) == 0)
Stefan Roesed4ef82b2010-09-13 11:10:34 +0200338 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000339 }
Simon Glasse6d0ca22013-02-24 17:33:15 +0000340 unmap_sysmem(buf1);
341 unmap_sysmem(buf2);
wdenk38635852002-08-27 05:55:31 +0000342
Mike Frysinger62a7ea72012-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 Phillipsdc00a682012-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 Glasse6d0ca22013-02-24 17:33:15 +0000349 ulong addr, dest, count, bytes;
wdenk874ac262003-07-24 23:38:38 +0000350 int size;
Simon Glasse6d0ca22013-02-24 17:33:15 +0000351 const void *src;
352 void *buf;
wdenk38635852002-08-27 05:55:31 +0000353
Wolfgang Denk3b683112010-07-17 01:06:04 +0200354 if (argc != 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000355 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000356
357 /* Check for size specification.
358 */
wdenk874ac262003-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-VILLARD03836942008-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 */
wdenk381669a2003-06-16 23:50:08 +0000377 if ( (addr2info(dest) != NULL)
378#ifdef CONFIG_HAS_DATAFLASH
Kim B. Heino1eb0fd52008-03-03 10:39:13 +0200379 && (!addr_dataflash(dest))
wdenk381669a2003-06-16 23:50:08 +0000380#endif
381 ) {
wdenk38635852002-08-27 05:55:31 +0000382 int rc;
383
wdenk42c05472004-03-23 22:14:11 +0000384 puts ("Copy to Flash... ");
wdenk38635852002-08-27 05:55:31 +0000385
Wolfgang Denk7fb52662005-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
wdenk381669a2003-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
wdenk42c05472004-03-23 22:14:11 +0000401 puts ("Copy to DataFlash... ");
wdenk381669a2003-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 }
wdenk57b2d802003-06-27 21:31:46 +0000412
wdenk381669a2003-06-16 23:50:08 +0000413 /* Check if we are copying from DataFlash to RAM */
Stelian Popa63d2eb2008-03-26 22:52:35 +0100414 if (addr_dataflash(addr) && !addr_dataflash(dest)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200415#ifndef CONFIG_SYS_NO_FLASH
Stelian Popa63d2eb2008-03-26 22:52:35 +0100416 && (addr2info(dest) == NULL)
417#endif
418 ){
wdenk86765902003-12-06 23:55:10 +0000419 int rc;
420 rc = read_dataflash(addr, count * size, (char *) dest);
421 if (rc != 1) {
wdenk1ebf41e2004-01-02 14:00:00 +0000422 dataflash_perror (rc);
423 return (1);
424 }
wdenk381669a2003-06-16 23:50:08 +0000425 return 0;
426 }
427
428 if (addr_dataflash(addr) && addr_dataflash(dest)){
wdenk42c05472004-03-23 22:14:11 +0000429 puts ("Unsupported combination of source/destination.\n\r");
wdenk381669a2003-06-16 23:50:08 +0000430 return 1;
431 }
wdenk7a428cc2003-06-15 22:40:42 +0000432#endif
433
Mike Frysingere578d682008-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 Glasse6d0ca22013-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 Glasse6d0ca22013-02-24 17:33:15 +0000447 *((ulong *)buf) = *((ulong *)src);
wdenk38635852002-08-27 05:55:31 +0000448 else if (size == 2)
Simon Glasse6d0ca22013-02-24 17:33:15 +0000449 *((ushort *)buf) = *((ushort *)src);
wdenk38635852002-08-27 05:55:31 +0000450 else
Simon Glasse6d0ca22013-02-24 17:33:15 +0000451 *((u_char *)buf) = *((u_char *)src);
452 src += size;
453 buf += size;
Stefan Roesed4ef82b2010-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 Phillipsdc00a682012-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 Phillipsdc00a682012-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 Glasse6d0ca22013-02-24 17:33:15 +0000479 ulong addr, length, i, bytes;
wdenk874ac262003-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 Glasse6d0ca22013-02-24 17:33:15 +0000484 const void *buf;
wdenk38635852002-08-27 05:55:31 +0000485
Wolfgang Denk3b683112010-07-17 01:06:04 +0200486 if (argc < 3)
Simon Glassa06dfc72011-12-10 08:44:01 +0000487 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000488
Robert P. J. Day21c9b782013-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 */
wdenk874ac262003-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 Glasse6d0ca22013-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 Glasse6d0ca22013-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 Glasse6d0ca22013-02-24 17:33:15 +0000517 shortp = (ushort *)buf;
wdenk38635852002-08-27 05:55:31 +0000518 for (;;)
519 i = *shortp;
520 }
Simon Glasse6d0ca22013-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 Glasse6d0ca22013-02-24 17:33:15 +0000528 longp = (uint *)buf;
wdenk38635852002-08-27 05:55:31 +0000529 i = length;
530 while (i-- > 0)
Marek Vasut7d335822011-09-26 02:26:06 +0200531 *longp++;
wdenk38635852002-08-27 05:55:31 +0000532 }
533 }
534 if (size == 2) {
535 for (;;) {
Simon Glasse6d0ca22013-02-24 17:33:15 +0000536 shortp = (ushort *)buf;
wdenk38635852002-08-27 05:55:31 +0000537 i = length;
538 while (i-- > 0)
Marek Vasut7d335822011-09-26 02:26:06 +0200539 *shortp++;
wdenk38635852002-08-27 05:55:31 +0000540 }
541 }
542 for (;;) {
Simon Glasse6d0ca22013-02-24 17:33:15 +0000543 cp = (u_char *)buf;
wdenk38635852002-08-27 05:55:31 +0000544 i = length;
545 while (i-- > 0)
Marek Vasut7d335822011-09-26 02:26:06 +0200546 *cp++;
wdenk38635852002-08-27 05:55:31 +0000547 }
Simon Glasse6d0ca22013-02-24 17:33:15 +0000548 unmap_sysmem(buf);
wdenk38635852002-08-27 05:55:31 +0000549}
550
wdenk64519362004-07-11 17:40:54 +0000551#ifdef CONFIG_LOOPW
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200552int do_mem_loopw (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk64519362004-07-11 17:40:54 +0000553{
Simon Glasse6d0ca22013-02-24 17:33:15 +0000554 ulong addr, length, i, data, bytes;
wdenk64519362004-07-11 17:40:54 +0000555 int size;
556 volatile uint *longp;
557 volatile ushort *shortp;
558 volatile u_char *cp;
Simon Glasse6d0ca22013-02-24 17:33:15 +0000559 void *buf;
wdenk7dd13292004-07-11 20:04:51 +0000560
Wolfgang Denk3b683112010-07-17 01:06:04 +0200561 if (argc < 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000562 return CMD_RET_USAGE;
wdenk64519362004-07-11 17:40:54 +0000563
Robert P. J. Day21c9b782013-02-03 02:29:54 +0000564 /*
565 * Check for a size specification.
wdenk64519362004-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);
wdenk7dd13292004-07-11 20:04:51 +0000581
Simon Glasse6d0ca22013-02-24 17:33:15 +0000582 bytes = size * length;
583 buf = map_sysmem(addr, bytes);
584
wdenk64519362004-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 Glasse6d0ca22013-02-24 17:33:15 +0000590 longp = (uint *)buf;
wdenk64519362004-07-11 17:40:54 +0000591 for (;;)
592 *longp = data;
593 }
594 if (size == 2) {
Simon Glasse6d0ca22013-02-24 17:33:15 +0000595 shortp = (ushort *)buf;
wdenk64519362004-07-11 17:40:54 +0000596 for (;;)
597 *shortp = data;
598 }
Simon Glasse6d0ca22013-02-24 17:33:15 +0000599 cp = (u_char *)buf;
wdenk64519362004-07-11 17:40:54 +0000600 for (;;)
601 *cp = data;
602 }
603
604 if (size == 4) {
605 for (;;) {
Simon Glasse6d0ca22013-02-24 17:33:15 +0000606 longp = (uint *)buf;
wdenk64519362004-07-11 17:40:54 +0000607 i = length;
608 while (i-- > 0)
609 *longp++ = data;
610 }
611 }
612 if (size == 2) {
613 for (;;) {
Simon Glasse6d0ca22013-02-24 17:33:15 +0000614 shortp = (ushort *)buf;
wdenk64519362004-07-11 17:40:54 +0000615 i = length;
616 while (i-- > 0)
617 *shortp++ = data;
618 }
619 }
620 for (;;) {
Simon Glasse6d0ca22013-02-24 17:33:15 +0000621 cp = (u_char *)buf;
wdenk64519362004-07-11 17:40:54 +0000622 i = length;
623 while (i-- > 0)
624 *cp++ = data;
625 }
626}
627#endif /* CONFIG_LOOPW */
628
Simon Glass8f2c7582013-02-24 17:33:16 +0000629static int mem_test_alt(vu_long *start, vu_long *end,
630 int iteration_limit)
wdenk38635852002-08-27 05:55:31 +0000631{
Simon Glass8f2c7582013-02-24 17:33:16 +0000632 vu_long *addr;
Dirk Eibachb2659892008-12-16 14:51:56 +0100633 int iterations = 1;
Simon Glass8f2c7582013-02-24 17:33:16 +0000634 ulong errs = 0;
635 ulong val, readback;
636 int j;
637 vu_long len;
638 vu_long offset;
639 vu_long test_offset;
640 vu_long pattern;
641 vu_long temp;
642 vu_long anti_pattern;
643 vu_long num_words;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200644#if defined(CONFIG_SYS_MEMTEST_SCRATCH)
Simon Glass8f2c7582013-02-24 17:33:16 +0000645 vu_long *dummy = (vu_long *)CONFIG_SYS_MEMTEST_SCRATCH;
wdenk5958f4a2003-09-18 09:21:33 +0000646#else
Kim Phillipsdc00a682012-10-29 13:34:31 +0000647 vu_long *dummy = NULL; /* yes, this is address 0x0, not NULL */
wdenk5958f4a2003-09-18 09:21:33 +0000648#endif
wdenk38635852002-08-27 05:55:31 +0000649 static const ulong bitpattern[] = {
650 0x00000001, /* single bit */
651 0x00000003, /* two adjacent bits */
652 0x00000007, /* three adjacent bits */
653 0x0000000F, /* four adjacent bits */
654 0x00000005, /* two non-adjacent bits */
655 0x00000015, /* three non-adjacent bits */
656 0x00000055, /* four non-adjacent bits */
657 0xaaaaaaaa, /* alternating 1/0 */
658 };
wdenk38635852002-08-27 05:55:31 +0000659
Simon Glass8f2c7582013-02-24 17:33:16 +0000660 printf("Testing %08x ... %08x:\n", (uint)(uintptr_t)start,
661 (uint)(uintptr_t)end);
Mike Frysinger3afa8032012-01-20 09:07:20 +0000662 debug("%s:%d: start 0x%p end 0x%p\n",
Simon Glass8f2c7582013-02-24 17:33:16 +0000663 __func__, __LINE__, start, end);
wdenk38635852002-08-27 05:55:31 +0000664
665 for (;;) {
666 if (ctrlc()) {
Simon Glass8f2c7582013-02-24 17:33:16 +0000667 putc('\n');
wdenk38635852002-08-27 05:55:31 +0000668 return 1;
669 }
670
Dirk Eibachb2659892008-12-16 14:51:56 +0100671 if (iteration_limit && iterations > iteration_limit) {
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400672 printf("Tested %d iteration(s) with %lu errors.\n",
673 iterations-1, errs);
674 return errs != 0;
Dirk Eibachb2659892008-12-16 14:51:56 +0100675 }
676
wdenk38635852002-08-27 05:55:31 +0000677 printf("Iteration: %6d\r", iterations);
Mike Frysinger3afa8032012-01-20 09:07:20 +0000678 debug("\n");
wdenk38635852002-08-27 05:55:31 +0000679 iterations++;
680
681 /*
682 * Data line test: write a pattern to the first
683 * location, write the 1's complement to a 'parking'
684 * address (changes the state of the data bus so a
Robert P. J. Day21c9b782013-02-03 02:29:54 +0000685 * floating bus doesn't give a false OK), and then
wdenk38635852002-08-27 05:55:31 +0000686 * read the value back. Note that we read it back
687 * into a variable because the next time we read it,
688 * it might be right (been there, tough to explain to
689 * the quality guys why it prints a failure when the
690 * "is" and "should be" are obviously the same in the
691 * error message).
692 *
693 * Rather than exhaustively testing, we test some
694 * patterns by shifting '1' bits through a field of
695 * '0's and '0' bits through a field of '1's (i.e.
696 * pattern and ~pattern).
697 */
698 addr = start;
Simon Glass8f2c7582013-02-24 17:33:16 +0000699 for (j = 0; j < sizeof(bitpattern) / sizeof(bitpattern[0]);
700 j++) {
wdenk38635852002-08-27 05:55:31 +0000701 val = bitpattern[j];
Simon Glass8f2c7582013-02-24 17:33:16 +0000702 for (; val != 0; val <<= 1) {
wdenk38635852002-08-27 05:55:31 +0000703 *addr = val;
Simon Glass8f2c7582013-02-24 17:33:16 +0000704 *dummy = ~val; /* clear the test data off the bus */
wdenk38635852002-08-27 05:55:31 +0000705 readback = *addr;
706 if(readback != val) {
Simon Glass8f2c7582013-02-24 17:33:16 +0000707 printf("FAILURE (data line): "
708 "expected %08lx, actual %08lx\n",
709 val, readback);
710 errs++;
711 if (ctrlc()) {
712 putc('\n');
713 return 1;
714 }
wdenk38635852002-08-27 05:55:31 +0000715 }
716 *addr = ~val;
717 *dummy = val;
718 readback = *addr;
Simon Glass8f2c7582013-02-24 17:33:16 +0000719 if (readback != ~val) {
720 printf("FAILURE (data line): "
721 "Is %08lx, should be %08lx\n",
722 readback, ~val);
723 errs++;
724 if (ctrlc()) {
725 putc('\n');
726 return 1;
727 }
wdenk38635852002-08-27 05:55:31 +0000728 }
729 }
730 }
731
732 /*
733 * Based on code whose Original Author and Copyright
734 * information follows: Copyright (c) 1998 by Michael
735 * Barr. This software is placed into the public
736 * domain and may be used for any purpose. However,
737 * this notice must not be changed or removed and no
738 * warranty is either expressed or implied by its
739 * publication or distribution.
740 */
741
742 /*
743 * Address line test
744 *
745 * Description: Test the address bus wiring in a
746 * memory region by performing a walking
747 * 1's test on the relevant bits of the
748 * address and checking for aliasing.
749 * This test will find single-bit
Robert P. J. Day21c9b782013-02-03 02:29:54 +0000750 * address failures such as stuck-high,
wdenk38635852002-08-27 05:55:31 +0000751 * stuck-low, and shorted pins. The base
752 * address and size of the region are
753 * selected by the caller.
754 *
755 * Notes: For best results, the selected base
756 * address should have enough LSB 0's to
757 * guarantee single address bit changes.
758 * For example, to test a 64-Kbyte
759 * region, select a base address on a
760 * 64-Kbyte boundary. Also, select the
761 * region size as a power-of-two if at
762 * all possible.
763 *
764 * Returns: 0 if the test succeeds, 1 if the test fails.
wdenk38635852002-08-27 05:55:31 +0000765 */
Guennadi Liakhovetskid4e3fb42008-02-08 21:25:58 +0100766 len = ((ulong)end - (ulong)start)/sizeof(vu_long);
wdenk38635852002-08-27 05:55:31 +0000767 pattern = (vu_long) 0xaaaaaaaa;
768 anti_pattern = (vu_long) 0x55555555;
769
Mike Frysinger3afa8032012-01-20 09:07:20 +0000770 debug("%s:%d: length = 0x%.8lx\n",
Simon Glass8f2c7582013-02-24 17:33:16 +0000771 __func__, __LINE__, len);
wdenk38635852002-08-27 05:55:31 +0000772 /*
773 * Write the default pattern at each of the
774 * power-of-two offsets.
775 */
Simon Glass8f2c7582013-02-24 17:33:16 +0000776 for (offset = 1; offset < len; offset <<= 1)
wdenk38635852002-08-27 05:55:31 +0000777 start[offset] = pattern;
wdenk38635852002-08-27 05:55:31 +0000778
779 /*
780 * Check for address bits stuck high.
781 */
782 test_offset = 0;
783 start[test_offset] = anti_pattern;
784
Guennadi Liakhovetskid4e3fb42008-02-08 21:25:58 +0100785 for (offset = 1; offset < len; offset <<= 1) {
wdenk38635852002-08-27 05:55:31 +0000786 temp = start[offset];
787 if (temp != pattern) {
Simon Glass8f2c7582013-02-24 17:33:16 +0000788 printf("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000789 " expected 0x%.8lx, actual 0x%.8lx\n",
790 (ulong)&start[offset], pattern, temp);
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400791 errs++;
792 if (ctrlc()) {
Simon Glass8f2c7582013-02-24 17:33:16 +0000793 putc('\n');
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400794 return 1;
795 }
wdenk38635852002-08-27 05:55:31 +0000796 }
797 }
798 start[test_offset] = pattern;
Sergei Poselenov474dbb82008-04-09 16:09:41 +0200799 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000800
801 /*
802 * Check for addr bits stuck low or shorted.
803 */
Guennadi Liakhovetskid4e3fb42008-02-08 21:25:58 +0100804 for (test_offset = 1; test_offset < len; test_offset <<= 1) {
wdenk38635852002-08-27 05:55:31 +0000805 start[test_offset] = anti_pattern;
806
Guennadi Liakhovetskid4e3fb42008-02-08 21:25:58 +0100807 for (offset = 1; offset < len; offset <<= 1) {
wdenk38635852002-08-27 05:55:31 +0000808 temp = start[offset];
809 if ((temp != pattern) && (offset != test_offset)) {
Simon Glass8f2c7582013-02-24 17:33:16 +0000810 printf("\nFAILURE: Address bit stuck low or shorted @"
wdenk38635852002-08-27 05:55:31 +0000811 " 0x%.8lx: expected 0x%.8lx, actual 0x%.8lx\n",
812 (ulong)&start[offset], pattern, temp);
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400813 errs++;
814 if (ctrlc()) {
Simon Glass8f2c7582013-02-24 17:33:16 +0000815 putc('\n');
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400816 return 1;
817 }
wdenk38635852002-08-27 05:55:31 +0000818 }
819 }
820 start[test_offset] = pattern;
821 }
822
823 /*
824 * Description: Test the integrity of a physical
825 * memory device by performing an
826 * increment/decrement test over the
827 * entire region. In the process every
828 * storage bit in the device is tested
829 * as a zero and a one. The base address
830 * and the size of the region are
831 * selected by the caller.
832 *
833 * Returns: 0 if the test succeeds, 1 if the test fails.
834 */
835 num_words = ((ulong)end - (ulong)start)/sizeof(vu_long) + 1;
836
837 /*
838 * Fill memory with a known pattern.
839 */
840 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
Sergei Poselenov474dbb82008-04-09 16:09:41 +0200841 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000842 start[offset] = pattern;
843 }
844
845 /*
846 * Check each location and invert it for the second pass.
847 */
848 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
Sergei Poselenov474dbb82008-04-09 16:09:41 +0200849 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000850 temp = start[offset];
851 if (temp != pattern) {
Simon Glass8f2c7582013-02-24 17:33:16 +0000852 printf("\nFAILURE (read/write) @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000853 " expected 0x%.8lx, actual 0x%.8lx)\n",
854 (ulong)&start[offset], pattern, temp);
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400855 errs++;
856 if (ctrlc()) {
Simon Glass8f2c7582013-02-24 17:33:16 +0000857 putc('\n');
858 return 1;
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400859 }
wdenk38635852002-08-27 05:55:31 +0000860 }
861
862 anti_pattern = ~pattern;
863 start[offset] = anti_pattern;
864 }
865
866 /*
867 * Check each location for the inverted pattern and zero it.
868 */
869 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
Sergei Poselenov474dbb82008-04-09 16:09:41 +0200870 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000871 anti_pattern = ~pattern;
872 temp = start[offset];
873 if (temp != anti_pattern) {
Simon Glass8f2c7582013-02-24 17:33:16 +0000874 printf("\nFAILURE (read/write): @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000875 " expected 0x%.8lx, actual 0x%.8lx)\n",
876 (ulong)&start[offset], anti_pattern, temp);
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400877 errs++;
878 if (ctrlc()) {
Simon Glass8f2c7582013-02-24 17:33:16 +0000879 putc('\n');
880 return 1;
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400881 }
wdenk38635852002-08-27 05:55:31 +0000882 }
883 start[offset] = 0;
884 }
885 }
Simon Glass8f2c7582013-02-24 17:33:16 +0000886}
887
888static int mem_test_quick(vu_long *start, vu_long *end,
889 int iteration_limit, vu_long pattern)
890{
891 vu_long *addr;
892 int iterations = 1;
893 ulong errs = 0;
894 ulong incr;
895 ulong val, readback;
wdenk38635852002-08-27 05:55:31 +0000896
wdenk38635852002-08-27 05:55:31 +0000897 incr = 1;
898 for (;;) {
899 if (ctrlc()) {
Simon Glass8f2c7582013-02-24 17:33:16 +0000900 putc('\n');
wdenk38635852002-08-27 05:55:31 +0000901 return 1;
902 }
Dirk Eibachb2659892008-12-16 14:51:56 +0100903
904 if (iteration_limit && iterations > iteration_limit) {
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400905 printf("Tested %d iteration(s) with %lu errors.\n",
906 iterations-1, errs);
907 return errs != 0;
Dirk Eibachb2659892008-12-16 14:51:56 +0100908 }
909 ++iterations;
wdenk38635852002-08-27 05:55:31 +0000910
Simon Glass8f2c7582013-02-24 17:33:16 +0000911 printf("\rPattern %08lX Writing..."
wdenk38635852002-08-27 05:55:31 +0000912 "%12s"
913 "\b\b\b\b\b\b\b\b\b\b",
914 pattern, "");
915
Simon Glass8f2c7582013-02-24 17:33:16 +0000916 for (addr = start, val = pattern; addr < end; addr++) {
Sergei Poselenov474dbb82008-04-09 16:09:41 +0200917 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000918 *addr = val;
Simon Glass8f2c7582013-02-24 17:33:16 +0000919 val += incr;
wdenk38635852002-08-27 05:55:31 +0000920 }
921
Simon Glass8f2c7582013-02-24 17:33:16 +0000922 puts("Reading...");
wdenk38635852002-08-27 05:55:31 +0000923
Simon Glass8f2c7582013-02-24 17:33:16 +0000924 for (addr = start, val = pattern; addr < end; addr++) {
Sergei Poselenov474dbb82008-04-09 16:09:41 +0200925 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000926 readback = *addr;
927 if (readback != val) {
Simon Glass8f2c7582013-02-24 17:33:16 +0000928 printf("\nMem error @ 0x%08X: "
wdenk38635852002-08-27 05:55:31 +0000929 "found %08lX, expected %08lX\n",
Simon Glassfc953242011-09-17 06:48:58 +0000930 (uint)(uintptr_t)addr, readback, val);
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400931 errs++;
932 if (ctrlc()) {
Simon Glass8f2c7582013-02-24 17:33:16 +0000933 putc('\n');
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400934 return 1;
935 }
wdenk38635852002-08-27 05:55:31 +0000936 }
937 val += incr;
938 }
939
940 /*
941 * Flip the pattern each time to make lots of zeros and
942 * then, the next time, lots of ones. We decrement
943 * the "negative" patterns and increment the "positive"
944 * patterns to preserve this feature.
945 */
Simon Glass8f2c7582013-02-24 17:33:16 +0000946 if (pattern & 0x80000000)
wdenk38635852002-08-27 05:55:31 +0000947 pattern = -pattern; /* complement & increment */
Simon Glass8f2c7582013-02-24 17:33:16 +0000948 else
wdenk38635852002-08-27 05:55:31 +0000949 pattern = ~pattern;
wdenk38635852002-08-27 05:55:31 +0000950 incr = -incr;
951 }
Simon Glass8f2c7582013-02-24 17:33:16 +0000952}
953
954/*
955 * Perform a memory test. A more complete alternative test can be
956 * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
957 * interrupted by ctrl-c or by a failure of one of the sub-tests.
958 */
959static int do_mem_mtest(cmd_tbl_t *cmdtp, int flag, int argc,
960 char * const argv[])
961{
962 vu_long *start, *end;
963 int iteration_limit;
964 int ret;
965 ulong pattern;
966#if defined(CONFIG_SYS_ALT_MEMTEST)
967 const int alt_test = 1;
968#else
969 const int alt_test = 0;
wdenk38635852002-08-27 05:55:31 +0000970#endif
Simon Glass8f2c7582013-02-24 17:33:16 +0000971
972 if (argc > 1)
973 start = (ulong *)simple_strtoul(argv[1], NULL, 16);
974 else
975 start = (ulong *)CONFIG_SYS_MEMTEST_START;
976
977 if (argc > 2)
978 end = (ulong *)simple_strtoul(argv[2], NULL, 16);
979 else
980 end = (ulong *)(CONFIG_SYS_MEMTEST_END);
981
982 if (argc > 3)
983 pattern = (ulong)simple_strtoul(argv[3], NULL, 16);
984 else
985 pattern = 0;
986
987 if (argc > 4)
988 iteration_limit = (ulong)simple_strtoul(argv[4], NULL, 16);
989 else
990 iteration_limit = 0;
991
992 if (alt_test)
993 ret = mem_test_alt(start, end, iteration_limit);
994 else
995 ret = mem_test_quick(start, end, iteration_limit, pattern);
996
997 return ret; /* not reached */
wdenk38635852002-08-27 05:55:31 +0000998}
999
1000
1001/* Modify memory.
1002 *
1003 * Syntax:
1004 * mm{.b, .w, .l} {addr}
1005 * nm{.b, .w, .l} {addr}
1006 */
1007static int
Wolfgang Denk6262d0212010-06-28 22:00:46 +02001008mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +00001009{
wdenk874ac262003-07-24 23:38:38 +00001010 ulong addr, i;
1011 int nbytes, size;
Simon Glasse6d0ca22013-02-24 17:33:15 +00001012 void *ptr = NULL;
wdenk38635852002-08-27 05:55:31 +00001013
Wolfgang Denk3b683112010-07-17 01:06:04 +02001014 if (argc != 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001015 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +00001016
1017#ifdef CONFIG_BOOT_RETRY_TIME
1018 reset_cmd_timeout(); /* got a good command to get here */
1019#endif
1020 /* We use the last specified parameters, unless new ones are
1021 * entered.
1022 */
1023 addr = mm_last_addr;
1024 size = mm_last_size;
1025
1026 if ((flag & CMD_FLAG_REPEAT) == 0) {
1027 /* New command specified. Check for a size specification.
1028 * Defaults to long if no or incorrect specification.
1029 */
wdenk874ac262003-07-24 23:38:38 +00001030 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
1031 return 1;
wdenk38635852002-08-27 05:55:31 +00001032
1033 /* Address is specified since argc > 1
1034 */
1035 addr = simple_strtoul(argv[1], NULL, 16);
1036 addr += base_address;
1037 }
1038
wdenk381669a2003-06-16 23:50:08 +00001039#ifdef CONFIG_HAS_DATAFLASH
1040 if (addr_dataflash(addr)){
wdenk42c05472004-03-23 22:14:11 +00001041 puts ("Can't modify DataFlash in place. Use cp instead.\n\r");
wdenk381669a2003-06-16 23:50:08 +00001042 return 0;
1043 }
1044#endif
1045
Mike Frysingere578d682008-02-04 19:26:56 -05001046#ifdef CONFIG_BLACKFIN
1047 if (addr_bfin_on_chip_mem(addr)) {
1048 puts ("Can't modify L1 instruction in place. Use cp instead.\n\r");
1049 return 0;
1050 }
1051#endif
1052
wdenk38635852002-08-27 05:55:31 +00001053 /* Print the address, followed by value. Then accept input for
1054 * the next value. A non-converted value exits.
1055 */
1056 do {
Simon Glasse6d0ca22013-02-24 17:33:15 +00001057 ptr = map_sysmem(addr, size);
wdenk38635852002-08-27 05:55:31 +00001058 printf("%08lx:", addr);
1059 if (size == 4)
Simon Glasse6d0ca22013-02-24 17:33:15 +00001060 printf(" %08x", *((uint *)ptr));
wdenk38635852002-08-27 05:55:31 +00001061 else if (size == 2)
Simon Glasse6d0ca22013-02-24 17:33:15 +00001062 printf(" %04x", *((ushort *)ptr));
wdenk38635852002-08-27 05:55:31 +00001063 else
Simon Glasse6d0ca22013-02-24 17:33:15 +00001064 printf(" %02x", *((u_char *)ptr));
wdenk38635852002-08-27 05:55:31 +00001065
1066 nbytes = readline (" ? ");
1067 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
1068 /* <CR> pressed as only input, don't modify current
1069 * location and move to next. "-" pressed will go back.
1070 */
1071 if (incrflag)
1072 addr += nbytes ? -size : size;
1073 nbytes = 1;
1074#ifdef CONFIG_BOOT_RETRY_TIME
1075 reset_cmd_timeout(); /* good enough to not time out */
1076#endif
1077 }
1078#ifdef CONFIG_BOOT_RETRY_TIME
1079 else if (nbytes == -2) {
1080 break; /* timed out, exit the command */
1081 }
1082#endif
1083 else {
1084 char *endp;
1085 i = simple_strtoul(console_buffer, &endp, 16);
1086 nbytes = endp - console_buffer;
1087 if (nbytes) {
1088#ifdef CONFIG_BOOT_RETRY_TIME
1089 /* good enough to not time out
1090 */
1091 reset_cmd_timeout();
1092#endif
1093 if (size == 4)
Simon Glasse6d0ca22013-02-24 17:33:15 +00001094 *((uint *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001095 else if (size == 2)
Simon Glasse6d0ca22013-02-24 17:33:15 +00001096 *((ushort *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001097 else
Simon Glasse6d0ca22013-02-24 17:33:15 +00001098 *((u_char *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001099 if (incrflag)
1100 addr += size;
1101 }
1102 }
1103 } while (nbytes);
Simon Glasse6d0ca22013-02-24 17:33:15 +00001104 if (ptr)
1105 unmap_sysmem(ptr);
wdenk38635852002-08-27 05:55:31 +00001106
1107 mm_last_addr = addr;
1108 mm_last_size = size;
1109 return 0;
1110}
1111
Mike Frysinger321ab9e2010-12-21 14:19:51 -05001112#ifdef CONFIG_CMD_CRC32
1113
wdenk6203e402004-04-18 10:13:26 +00001114#ifndef CONFIG_CRC32_VERIFY
1115
Kim Phillipsdc00a682012-10-29 13:34:31 +00001116static int do_mem_crc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +00001117{
wdenk7a428cc2003-06-15 22:40:42 +00001118 ulong addr, length;
1119 ulong crc;
1120 ulong *ptr;
wdenk38635852002-08-27 05:55:31 +00001121
Wolfgang Denk3b683112010-07-17 01:06:04 +02001122 if (argc < 3)
Simon Glassa06dfc72011-12-10 08:44:01 +00001123 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +00001124
wdenk7a428cc2003-06-15 22:40:42 +00001125 addr = simple_strtoul (argv[1], NULL, 16);
wdenk38635852002-08-27 05:55:31 +00001126 addr += base_address;
1127
wdenk7a428cc2003-06-15 22:40:42 +00001128 length = simple_strtoul (argv[2], NULL, 16);
wdenk38635852002-08-27 05:55:31 +00001129
Jens Scharsig5b2e6c22011-07-18 08:46:26 +02001130 crc = crc32_wd (0, (const uchar *) addr, length, CHUNKSZ_CRC32);
wdenk38635852002-08-27 05:55:31 +00001131
1132 printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
wdenk7a428cc2003-06-15 22:40:42 +00001133 addr, addr + length - 1, crc);
wdenk38635852002-08-27 05:55:31 +00001134
wdenk7a428cc2003-06-15 22:40:42 +00001135 if (argc > 3) {
1136 ptr = (ulong *) simple_strtoul (argv[3], NULL, 16);
1137 *ptr = crc;
1138 }
wdenk38635852002-08-27 05:55:31 +00001139
1140 return 0;
1141}
1142
wdenk6203e402004-04-18 10:13:26 +00001143#else /* CONFIG_CRC32_VERIFY */
1144
Wolfgang Denk6262d0212010-06-28 22:00:46 +02001145int do_mem_crc (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk6203e402004-04-18 10:13:26 +00001146{
1147 ulong addr, length;
1148 ulong crc;
1149 ulong *ptr;
wdenk05939202004-04-18 17:39:38 +00001150 ulong vcrc;
wdenk6203e402004-04-18 10:13:26 +00001151 int verify;
1152 int ac;
Wolfgang Denk6262d0212010-06-28 22:00:46 +02001153 char * const *av;
wdenk6203e402004-04-18 10:13:26 +00001154
1155 if (argc < 3) {
Wolfgang Denk3b683112010-07-17 01:06:04 +02001156usage:
Simon Glassa06dfc72011-12-10 08:44:01 +00001157 return CMD_RET_USAGE;
wdenk6203e402004-04-18 10:13:26 +00001158 }
1159
1160 av = argv + 1;
1161 ac = argc - 1;
1162 if (strcmp(*av, "-v") == 0) {
1163 verify = 1;
1164 av++;
1165 ac--;
1166 if (ac < 3)
1167 goto usage;
1168 } else
1169 verify = 0;
1170
1171 addr = simple_strtoul(*av++, NULL, 16);
1172 addr += base_address;
1173 length = simple_strtoul(*av++, NULL, 16);
1174
Jens Scharsig5b2e6c22011-07-18 08:46:26 +02001175 crc = crc32_wd (0, (const uchar *) addr, length, CHUNKSZ_CRC32);
wdenk6203e402004-04-18 10:13:26 +00001176
1177 if (!verify) {
1178 printf ("CRC32 for %08lx ... %08lx ==> %08lx\n",
1179 addr, addr + length - 1, crc);
1180 if (ac > 2) {
1181 ptr = (ulong *) simple_strtoul (*av++, NULL, 16);
1182 *ptr = crc;
1183 }
1184 } else {
1185 vcrc = simple_strtoul(*av++, NULL, 16);
1186 if (vcrc != crc) {
1187 printf ("CRC32 for %08lx ... %08lx ==> %08lx != %08lx ** ERROR **\n",
1188 addr, addr + length - 1, crc, vcrc);
1189 return 1;
1190 }
1191 }
1192
1193 return 0;
1194
1195}
1196#endif /* CONFIG_CRC32_VERIFY */
1197
Robin Getz93d6cb02009-07-27 00:07:59 -04001198#endif
1199
wdenk57b2d802003-06-27 21:31:46 +00001200/**************************************************/
wdenkf287a242003-07-01 21:06:45 +00001201U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001202 md, 3, 1, do_mem_md,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001203 "memory display",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001204 "[.b, .w, .l] address [# of objects]"
wdenk57b2d802003-06-27 21:31:46 +00001205);
1206
1207
wdenkf287a242003-07-01 21:06:45 +00001208U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001209 mm, 2, 1, do_mem_mm,
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001210 "memory modify (auto-incrementing address)",
1211 "[.b, .w, .l] address"
wdenk57b2d802003-06-27 21:31:46 +00001212);
1213
1214
wdenkf287a242003-07-01 21:06:45 +00001215U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001216 nm, 2, 1, do_mem_nm,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001217 "memory modify (constant address)",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001218 "[.b, .w, .l] address"
wdenk57b2d802003-06-27 21:31:46 +00001219);
1220
wdenkf287a242003-07-01 21:06:45 +00001221U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001222 mw, 4, 1, do_mem_mw,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001223 "memory write (fill)",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001224 "[.b, .w, .l] address value [count]"
wdenk57b2d802003-06-27 21:31:46 +00001225);
1226
wdenkf287a242003-07-01 21:06:45 +00001227U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001228 cp, 4, 1, do_mem_cp,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001229 "memory copy",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001230 "[.b, .w, .l] source target count"
wdenk57b2d802003-06-27 21:31:46 +00001231);
1232
wdenkf287a242003-07-01 21:06:45 +00001233U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001234 cmp, 4, 1, do_mem_cmp,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001235 "memory compare",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001236 "[.b, .w, .l] addr1 addr2 count"
wdenk57b2d802003-06-27 21:31:46 +00001237);
1238
Mike Frysinger321ab9e2010-12-21 14:19:51 -05001239#ifdef CONFIG_CMD_CRC32
1240
wdenk6203e402004-04-18 10:13:26 +00001241#ifndef CONFIG_CRC32_VERIFY
1242
wdenkf287a242003-07-01 21:06:45 +00001243U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001244 crc32, 4, 1, do_mem_crc,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001245 "checksum calculation",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001246 "address count [addr]\n - compute CRC32 checksum [save at addr]"
wdenk57b2d802003-06-27 21:31:46 +00001247);
1248
wdenk6203e402004-04-18 10:13:26 +00001249#else /* CONFIG_CRC32_VERIFY */
1250
1251U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001252 crc32, 5, 1, do_mem_crc,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001253 "checksum calculation",
wdenk6203e402004-04-18 10:13:26 +00001254 "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001255 "-v address count crc\n - verify crc of memory area"
wdenk6203e402004-04-18 10:13:26 +00001256);
1257
1258#endif /* CONFIG_CRC32_VERIFY */
1259
Mike Frysinger321ab9e2010-12-21 14:19:51 -05001260#endif
1261
Simon Glasseacd14f2012-11-30 13:01:20 +00001262#ifdef CONFIG_CMD_MEMINFO
1263__weak void board_show_dram(ulong size)
1264{
1265 puts("DRAM: ");
1266 print_size(size, "\n");
1267}
1268
1269static int do_mem_info(cmd_tbl_t *cmdtp, int flag, int argc,
1270 char * const argv[])
1271{
1272 board_show_dram(gd->ram_size);
1273
1274 return 0;
1275}
1276#endif
1277
wdenkf287a242003-07-01 21:06:45 +00001278U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001279 base, 2, 1, do_mem_base,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001280 "print or set address offset",
wdenk57b2d802003-06-27 21:31:46 +00001281 "\n - print address offset for memory commands\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001282 "base off\n - set address offset for memory commands to 'off'"
wdenk57b2d802003-06-27 21:31:46 +00001283);
1284
wdenkf287a242003-07-01 21:06:45 +00001285U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001286 loop, 3, 1, do_mem_loop,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001287 "infinite loop on address range",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001288 "[.b, .w, .l] address number_of_objects"
wdenk57b2d802003-06-27 21:31:46 +00001289);
wdenk64519362004-07-11 17:40:54 +00001290
1291#ifdef CONFIG_LOOPW
1292U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001293 loopw, 4, 1, do_mem_loopw,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001294 "infinite write loop on address range",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001295 "[.b, .w, .l] address number_of_objects data_to_write"
wdenk64519362004-07-11 17:40:54 +00001296);
1297#endif /* CONFIG_LOOPW */
wdenk57b2d802003-06-27 21:31:46 +00001298
wdenkf287a242003-07-01 21:06:45 +00001299U_BOOT_CMD(
Dirk Eibachb2659892008-12-16 14:51:56 +01001300 mtest, 5, 1, do_mem_mtest,
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001301 "simple RAM read/write test",
1302 "[start [end [pattern [iterations]]]]"
wdenk57b2d802003-06-27 21:31:46 +00001303);
stroesefb5e0ac2004-12-16 17:42:39 +00001304
1305#ifdef CONFIG_MX_CYCLIC
1306U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001307 mdc, 4, 1, do_mem_mdc,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001308 "memory display cyclic",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001309 "[.b, .w, .l] address count delay(ms)"
stroesefb5e0ac2004-12-16 17:42:39 +00001310);
1311
1312U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001313 mwc, 4, 1, do_mem_mwc,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001314 "memory write cyclic",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001315 "[.b, .w, .l] address value delay(ms)"
stroesefb5e0ac2004-12-16 17:42:39 +00001316);
1317#endif /* CONFIG_MX_CYCLIC */
Simon Glasseacd14f2012-11-30 13:01:20 +00001318
1319#ifdef CONFIG_CMD_MEMINFO
1320U_BOOT_CMD(
1321 meminfo, 3, 1, do_mem_info,
1322 "display memory information",
1323 ""
1324);
1325#endif