blob: 6775ab77336c401b2605a4f5c1abce3747342318 [file] [log] [blame]
wdenk38635852002-08-27 05:55:31 +00001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk38635852002-08-27 05:55:31 +00006 */
7
8/*
9 * Memory Functions
10 *
11 * Copied from FADS ROM, Dan Malek (dmalek@jlc.net)
12 */
13
14#include <common.h>
Simon Glass24b852a2015-11-08 23:47:45 -070015#include <console.h>
Simon Glass0098e172014-04-10 20:01:30 -060016#include <bootretry.h>
Simon Glass18d66532014-04-10 20:01:25 -060017#include <cli.h>
wdenk38635852002-08-27 05:55:31 +000018#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070019#include <console.h>
Simon Glassd20a40d2013-02-24 20:30:22 +000020#include <hash.h>
Simon Glasse48f3742014-11-11 12:47:07 -070021#include <inttypes.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050022#include <mapmem.h>
Sergei Poselenova6e6fc62008-04-09 16:09:41 +020023#include <watchdog.h>
Simon Glass0628ab82013-02-24 17:33:15 +000024#include <asm/io.h>
Simon Glass15a33e42012-11-30 13:01:20 +000025#include <linux/compiler.h>
26
27DECLARE_GLOBAL_DATA_PTR;
wdenk38635852002-08-27 05:55:31 +000028
Simon Glass8e169cc2013-02-24 17:33:29 +000029#ifndef CONFIG_SYS_MEMTEST_SCRATCH
30#define CONFIG_SYS_MEMTEST_SCRATCH 0
31#endif
32
Wolfgang Denk54841ab2010-06-28 22:00:46 +020033static int mod_mem(cmd_tbl_t *, int, int, int, char * const []);
wdenk38635852002-08-27 05:55:31 +000034
35/* Display values from last command.
36 * Memory modify remembered values are different from display memory.
37 */
Scott Wood581508b2015-03-19 09:43:12 -070038static ulong dp_last_addr, dp_last_size;
39static ulong dp_last_length = 0x40;
40static ulong mm_last_addr, mm_last_size;
wdenk38635852002-08-27 05:55:31 +000041
42static ulong base_address = 0;
43
44/* Memory Display
45 *
46 * Syntax:
York Sun4d1fd7f2014-02-26 17:03:19 -080047 * md{.b, .w, .l, .q} {addr} {len}
wdenk38635852002-08-27 05:55:31 +000048 */
49#define DISP_LINE_LEN 16
Kim Phillips088f1b12012-10-29 13:34:31 +000050static int do_mem_md(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000051{
Tuomas Tynkkynenc68c03f2017-10-10 21:59:42 +030052 ulong addr, length, bytes;
53 const void *buf;
wdenk27b207f2003-07-24 23:38:38 +000054 int size;
wdenk38635852002-08-27 05:55:31 +000055 int rc = 0;
56
57 /* We use the last specified parameters, unless new ones are
58 * entered.
59 */
60 addr = dp_last_addr;
61 size = dp_last_size;
62 length = dp_last_length;
63
Wolfgang Denk47e26b12010-07-17 01:06:04 +020064 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +000065 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +000066
67 if ((flag & CMD_FLAG_REPEAT) == 0) {
68 /* New command specified. Check for a size specification.
69 * Defaults to long if no or incorrect specification.
70 */
wdenk27b207f2003-07-24 23:38:38 +000071 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
72 return 1;
wdenk38635852002-08-27 05:55:31 +000073
74 /* Address is specified since argc > 1
75 */
76 addr = simple_strtoul(argv[1], NULL, 16);
77 addr += base_address;
78
79 /* If another parameter, it is the length to display.
80 * Length is the number of objects, not number of bytes.
81 */
82 if (argc > 2)
83 length = simple_strtoul(argv[2], NULL, 16);
84 }
85
Tuomas Tynkkynenc68c03f2017-10-10 21:59:42 +030086 bytes = size * length;
87 buf = map_sysmem(addr, bytes);
wdenk2abbe072003-06-16 23:50:08 +000088
Tuomas Tynkkynenc68c03f2017-10-10 21:59:42 +030089 /* Print the lines. */
90 print_buffer(addr, buf, size, length, DISP_LINE_LEN / size);
91 addr += bytes;
92 unmap_sysmem(buf);
wdenk38635852002-08-27 05:55:31 +000093
94 dp_last_addr = addr;
95 dp_last_length = length;
96 dp_last_size = size;
97 return (rc);
98}
99
Kim Phillips088f1b12012-10-29 13:34:31 +0000100static int do_mem_mm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000101{
102 return mod_mem (cmdtp, 1, flag, argc, argv);
103}
Kim Phillips088f1b12012-10-29 13:34:31 +0000104static int do_mem_nm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000105{
106 return mod_mem (cmdtp, 0, flag, argc, argv);
107}
108
Kim Phillips088f1b12012-10-29 13:34:31 +0000109static int do_mem_mw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000110{
York Sun4d1fd7f2014-02-26 17:03:19 -0800111#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
112 u64 writeval;
113#else
114 ulong writeval;
115#endif
116 ulong addr, count;
wdenk27b207f2003-07-24 23:38:38 +0000117 int size;
Simon Glasscc5e1962015-03-05 12:25:18 -0700118 void *buf, *start;
Simon Glass0628ab82013-02-24 17:33:15 +0000119 ulong bytes;
wdenk38635852002-08-27 05:55:31 +0000120
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200121 if ((argc < 3) || (argc > 4))
Simon Glass4c12eeb2011-12-10 08:44:01 +0000122 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000123
124 /* Check for size specification.
125 */
wdenk27b207f2003-07-24 23:38:38 +0000126 if ((size = cmd_get_data_size(argv[0], 4)) < 1)
127 return 1;
wdenk38635852002-08-27 05:55:31 +0000128
129 /* Address is specified since argc > 1
130 */
131 addr = simple_strtoul(argv[1], NULL, 16);
132 addr += base_address;
133
134 /* Get the value to write.
135 */
York Sun4d1fd7f2014-02-26 17:03:19 -0800136#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
137 writeval = simple_strtoull(argv[2], NULL, 16);
138#else
wdenk38635852002-08-27 05:55:31 +0000139 writeval = simple_strtoul(argv[2], NULL, 16);
York Sun4d1fd7f2014-02-26 17:03:19 -0800140#endif
wdenk38635852002-08-27 05:55:31 +0000141
142 /* Count ? */
143 if (argc == 4) {
144 count = simple_strtoul(argv[3], NULL, 16);
145 } else {
146 count = 1;
147 }
148
Simon Glass0628ab82013-02-24 17:33:15 +0000149 bytes = size * count;
Simon Glasscc5e1962015-03-05 12:25:18 -0700150 start = map_sysmem(addr, bytes);
151 buf = start;
wdenk38635852002-08-27 05:55:31 +0000152 while (count-- > 0) {
153 if (size == 4)
York Sun76698b42014-02-12 15:55:35 -0800154 *((u32 *)buf) = (u32)writeval;
York Sun4d1fd7f2014-02-26 17:03:19 -0800155#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
156 else if (size == 8)
157 *((u64 *)buf) = (u64)writeval;
158#endif
wdenk38635852002-08-27 05:55:31 +0000159 else if (size == 2)
York Sun76698b42014-02-12 15:55:35 -0800160 *((u16 *)buf) = (u16)writeval;
wdenk38635852002-08-27 05:55:31 +0000161 else
York Sun76698b42014-02-12 15:55:35 -0800162 *((u8 *)buf) = (u8)writeval;
Simon Glass0628ab82013-02-24 17:33:15 +0000163 buf += size;
wdenk38635852002-08-27 05:55:31 +0000164 }
Simon Glasscc5e1962015-03-05 12:25:18 -0700165 unmap_sysmem(start);
wdenk38635852002-08-27 05:55:31 +0000166 return 0;
167}
168
stroese4aaf29b2004-12-16 17:42:39 +0000169#ifdef CONFIG_MX_CYCLIC
Masahiro Yamada5a8608e2014-09-04 00:38:26 +0900170static int do_mem_mdc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
stroese4aaf29b2004-12-16 17:42:39 +0000171{
172 int i;
173 ulong count;
174
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200175 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000176 return CMD_RET_USAGE;
stroese4aaf29b2004-12-16 17:42:39 +0000177
178 count = simple_strtoul(argv[3], NULL, 10);
179
180 for (;;) {
181 do_mem_md (NULL, 0, 3, argv);
182
183 /* delay for <count> ms... */
184 for (i=0; i<count; i++)
185 udelay (1000);
186
187 /* check for ctrl-c to abort... */
188 if (ctrlc()) {
189 puts("Abort\n");
190 return 0;
191 }
192 }
193
194 return 0;
195}
196
Masahiro Yamada5a8608e2014-09-04 00:38:26 +0900197static int do_mem_mwc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
stroese4aaf29b2004-12-16 17:42:39 +0000198{
199 int i;
200 ulong count;
201
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200202 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000203 return CMD_RET_USAGE;
stroese4aaf29b2004-12-16 17:42:39 +0000204
205 count = simple_strtoul(argv[3], NULL, 10);
206
207 for (;;) {
208 do_mem_mw (NULL, 0, 3, argv);
209
210 /* delay for <count> ms... */
211 for (i=0; i<count; i++)
212 udelay (1000);
213
214 /* check for ctrl-c to abort... */
215 if (ctrlc()) {
216 puts("Abort\n");
217 return 0;
218 }
219 }
220
221 return 0;
222}
223#endif /* CONFIG_MX_CYCLIC */
224
Kim Phillips088f1b12012-10-29 13:34:31 +0000225static int do_mem_cmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000226{
Simon Glass0628ab82013-02-24 17:33:15 +0000227 ulong addr1, addr2, count, ngood, bytes;
wdenk27b207f2003-07-24 23:38:38 +0000228 int size;
wdenk38635852002-08-27 05:55:31 +0000229 int rcode = 0;
Mike Frysinger054ea172012-01-20 09:07:21 +0000230 const char *type;
Simon Glass0628ab82013-02-24 17:33:15 +0000231 const void *buf1, *buf2, *base;
York Sun4d1fd7f2014-02-26 17:03:19 -0800232#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
233 u64 word1, word2;
234#else
235 ulong word1, word2;
236#endif
wdenk38635852002-08-27 05:55:31 +0000237
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200238 if (argc != 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000239 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000240
241 /* Check for size specification.
242 */
wdenk27b207f2003-07-24 23:38:38 +0000243 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
244 return 1;
York Sun4d1fd7f2014-02-26 17:03:19 -0800245 type = size == 8 ? "double word" :
246 size == 4 ? "word" :
247 size == 2 ? "halfword" : "byte";
wdenk38635852002-08-27 05:55:31 +0000248
249 addr1 = simple_strtoul(argv[1], NULL, 16);
250 addr1 += base_address;
251
252 addr2 = simple_strtoul(argv[2], NULL, 16);
253 addr2 += base_address;
254
255 count = simple_strtoul(argv[3], NULL, 16);
256
Simon Glass0628ab82013-02-24 17:33:15 +0000257 bytes = size * count;
258 base = buf1 = map_sysmem(addr1, bytes);
259 buf2 = map_sysmem(addr2, bytes);
Mike Frysingerfeb12a12012-01-20 09:07:22 +0000260 for (ngood = 0; ngood < count; ++ngood) {
wdenk38635852002-08-27 05:55:31 +0000261 if (size == 4) {
York Sun76698b42014-02-12 15:55:35 -0800262 word1 = *(u32 *)buf1;
263 word2 = *(u32 *)buf2;
York Sun4d1fd7f2014-02-26 17:03:19 -0800264#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
265 } else if (size == 8) {
266 word1 = *(u64 *)buf1;
267 word2 = *(u64 *)buf2;
268#endif
Mike Frysinger054ea172012-01-20 09:07:21 +0000269 } else if (size == 2) {
York Sun76698b42014-02-12 15:55:35 -0800270 word1 = *(u16 *)buf1;
271 word2 = *(u16 *)buf2;
Mike Frysinger054ea172012-01-20 09:07:21 +0000272 } else {
York Sun76698b42014-02-12 15:55:35 -0800273 word1 = *(u8 *)buf1;
274 word2 = *(u8 *)buf2;
wdenk38635852002-08-27 05:55:31 +0000275 }
Mike Frysinger054ea172012-01-20 09:07:21 +0000276 if (word1 != word2) {
Simon Glass0628ab82013-02-24 17:33:15 +0000277 ulong offset = buf1 - base;
York Sun4d1fd7f2014-02-26 17:03:19 -0800278#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
Simon Glasse48f3742014-11-11 12:47:07 -0700279 printf("%s at 0x%p (%#0*"PRIx64") != %s at 0x%p (%#0*"
280 PRIx64 ")\n",
York Sun4d1fd7f2014-02-26 17:03:19 -0800281 type, (void *)(addr1 + offset), size, word1,
282 type, (void *)(addr2 + offset), size, word2);
283#else
Mike Frysinger054ea172012-01-20 09:07:21 +0000284 printf("%s at 0x%08lx (%#0*lx) != %s at 0x%08lx (%#0*lx)\n",
Simon Glass0628ab82013-02-24 17:33:15 +0000285 type, (ulong)(addr1 + offset), size, word1,
286 type, (ulong)(addr2 + offset), size, word2);
York Sun4d1fd7f2014-02-26 17:03:19 -0800287#endif
Mike Frysinger054ea172012-01-20 09:07:21 +0000288 rcode = 1;
289 break;
wdenk38635852002-08-27 05:55:31 +0000290 }
Mike Frysinger054ea172012-01-20 09:07:21 +0000291
Simon Glass0628ab82013-02-24 17:33:15 +0000292 buf1 += size;
293 buf2 += size;
Stefan Roeseeaadb442010-09-13 11:10:34 +0200294
295 /* reset watchdog from time to time */
Mike Frysingerfeb12a12012-01-20 09:07:22 +0000296 if ((ngood % (64 << 10)) == 0)
Stefan Roeseeaadb442010-09-13 11:10:34 +0200297 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000298 }
Simon Glass0628ab82013-02-24 17:33:15 +0000299 unmap_sysmem(buf1);
300 unmap_sysmem(buf2);
wdenk38635852002-08-27 05:55:31 +0000301
Mike Frysinger054ea172012-01-20 09:07:21 +0000302 printf("Total of %ld %s(s) were the same\n", ngood, type);
wdenk38635852002-08-27 05:55:31 +0000303 return rcode;
304}
305
Kim Phillips088f1b12012-10-29 13:34:31 +0000306static int do_mem_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000307{
Fabio Estevamc2538422016-12-15 16:00:13 -0200308 ulong addr, dest, count;
wdenk27b207f2003-07-24 23:38:38 +0000309 int size;
wdenk38635852002-08-27 05:55:31 +0000310
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200311 if (argc != 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000312 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000313
314 /* Check for size specification.
315 */
wdenk27b207f2003-07-24 23:38:38 +0000316 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
317 return 1;
wdenk38635852002-08-27 05:55:31 +0000318
319 addr = simple_strtoul(argv[1], NULL, 16);
320 addr += base_address;
321
322 dest = simple_strtoul(argv[2], NULL, 16);
323 dest += base_address;
324
325 count = simple_strtoul(argv[3], NULL, 16);
326
327 if (count == 0) {
328 puts ("Zero length ???\n");
329 return 1;
330 }
331
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900332#ifdef CONFIG_MTD_NOR_FLASH
wdenk38635852002-08-27 05:55:31 +0000333 /* check if we are copying to Flash */
Tuomas Tynkkynenc68c03f2017-10-10 21:59:42 +0300334 if (addr2info(dest) != NULL) {
wdenk38635852002-08-27 05:55:31 +0000335 int rc;
336
wdenk4b9206e2004-03-23 22:14:11 +0000337 puts ("Copy to Flash... ");
wdenk38635852002-08-27 05:55:31 +0000338
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200339 rc = flash_write ((char *)addr, dest, count*size);
wdenk38635852002-08-27 05:55:31 +0000340 if (rc != 0) {
341 flash_perror (rc);
342 return (1);
343 }
344 puts ("done\n");
345 return 0;
346 }
347#endif
348
Fabio Estevamc2538422016-12-15 16:00:13 -0200349 memcpy((void *)dest, (void *)addr, count * size);
Masahiro Yamadaa3a47492014-10-23 17:46:24 +0900350
wdenk38635852002-08-27 05:55:31 +0000351 return 0;
352}
353
Kim Phillips088f1b12012-10-29 13:34:31 +0000354static int do_mem_base(cmd_tbl_t *cmdtp, int flag, int argc,
355 char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000356{
357 if (argc > 1) {
358 /* Set new base address.
359 */
360 base_address = simple_strtoul(argv[1], NULL, 16);
361 }
362 /* Print the current base address.
363 */
364 printf("Base Address: 0x%08lx\n", base_address);
365 return 0;
366}
367
Kim Phillips088f1b12012-10-29 13:34:31 +0000368static int do_mem_loop(cmd_tbl_t *cmdtp, int flag, int argc,
369 char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000370{
Simon Glass0628ab82013-02-24 17:33:15 +0000371 ulong addr, length, i, bytes;
wdenk27b207f2003-07-24 23:38:38 +0000372 int size;
York Sun4d1fd7f2014-02-26 17:03:19 -0800373#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
374 volatile u64 *llp;
375#endif
York Sun76698b42014-02-12 15:55:35 -0800376 volatile u32 *longp;
377 volatile u16 *shortp;
378 volatile u8 *cp;
Simon Glass0628ab82013-02-24 17:33:15 +0000379 const void *buf;
wdenk38635852002-08-27 05:55:31 +0000380
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200381 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000382 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000383
Robert P. J. Day85de63e2013-02-03 02:29:54 +0000384 /*
385 * Check for a size specification.
wdenk38635852002-08-27 05:55:31 +0000386 * Defaults to long if no or incorrect specification.
387 */
wdenk27b207f2003-07-24 23:38:38 +0000388 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
389 return 1;
wdenk38635852002-08-27 05:55:31 +0000390
391 /* Address is always specified.
392 */
393 addr = simple_strtoul(argv[1], NULL, 16);
394
395 /* Length is the number of objects, not number of bytes.
396 */
397 length = simple_strtoul(argv[2], NULL, 16);
398
Simon Glass0628ab82013-02-24 17:33:15 +0000399 bytes = size * length;
400 buf = map_sysmem(addr, bytes);
401
wdenk38635852002-08-27 05:55:31 +0000402 /* We want to optimize the loops to run as fast as possible.
403 * If we have only one object, just run infinite loops.
404 */
405 if (length == 1) {
York Sun4d1fd7f2014-02-26 17:03:19 -0800406#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
407 if (size == 8) {
408 llp = (u64 *)buf;
409 for (;;)
410 i = *llp;
411 }
412#endif
wdenk38635852002-08-27 05:55:31 +0000413 if (size == 4) {
York Sun76698b42014-02-12 15:55:35 -0800414 longp = (u32 *)buf;
wdenk38635852002-08-27 05:55:31 +0000415 for (;;)
416 i = *longp;
417 }
418 if (size == 2) {
York Sun76698b42014-02-12 15:55:35 -0800419 shortp = (u16 *)buf;
wdenk38635852002-08-27 05:55:31 +0000420 for (;;)
421 i = *shortp;
422 }
York Sun76698b42014-02-12 15:55:35 -0800423 cp = (u8 *)buf;
wdenk38635852002-08-27 05:55:31 +0000424 for (;;)
425 i = *cp;
426 }
427
York Sun4d1fd7f2014-02-26 17:03:19 -0800428#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
429 if (size == 8) {
430 for (;;) {
431 llp = (u64 *)buf;
432 i = length;
433 while (i-- > 0)
434 *llp++;
435 }
436 }
437#endif
wdenk38635852002-08-27 05:55:31 +0000438 if (size == 4) {
439 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800440 longp = (u32 *)buf;
wdenk38635852002-08-27 05:55:31 +0000441 i = length;
442 while (i-- > 0)
Marek Vasutf3b3c3d2011-09-26 02:26:06 +0200443 *longp++;
wdenk38635852002-08-27 05:55:31 +0000444 }
445 }
446 if (size == 2) {
447 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800448 shortp = (u16 *)buf;
wdenk38635852002-08-27 05:55:31 +0000449 i = length;
450 while (i-- > 0)
Marek Vasutf3b3c3d2011-09-26 02:26:06 +0200451 *shortp++;
wdenk38635852002-08-27 05:55:31 +0000452 }
453 }
454 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800455 cp = (u8 *)buf;
wdenk38635852002-08-27 05:55:31 +0000456 i = length;
457 while (i-- > 0)
Marek Vasutf3b3c3d2011-09-26 02:26:06 +0200458 *cp++;
wdenk38635852002-08-27 05:55:31 +0000459 }
Simon Glass0628ab82013-02-24 17:33:15 +0000460 unmap_sysmem(buf);
Simon Glass92765f422013-06-11 11:14:35 -0700461
462 return 0;
wdenk38635852002-08-27 05:55:31 +0000463}
464
wdenk56523f12004-07-11 17:40:54 +0000465#ifdef CONFIG_LOOPW
Masahiro Yamada5a8608e2014-09-04 00:38:26 +0900466static int do_mem_loopw(cmd_tbl_t *cmdtp, int flag, int argc,
467 char * const argv[])
wdenk56523f12004-07-11 17:40:54 +0000468{
York Sun4d1fd7f2014-02-26 17:03:19 -0800469 ulong addr, length, i, bytes;
wdenk56523f12004-07-11 17:40:54 +0000470 int size;
York Sun4d1fd7f2014-02-26 17:03:19 -0800471#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
472 volatile u64 *llp;
473 u64 data;
474#else
475 ulong data;
476#endif
York Sun76698b42014-02-12 15:55:35 -0800477 volatile u32 *longp;
478 volatile u16 *shortp;
479 volatile u8 *cp;
Simon Glass0628ab82013-02-24 17:33:15 +0000480 void *buf;
wdenk81050922004-07-11 20:04:51 +0000481
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200482 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000483 return CMD_RET_USAGE;
wdenk56523f12004-07-11 17:40:54 +0000484
Robert P. J. Day85de63e2013-02-03 02:29:54 +0000485 /*
486 * Check for a size specification.
wdenk56523f12004-07-11 17:40:54 +0000487 * Defaults to long if no or incorrect specification.
488 */
489 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
490 return 1;
491
492 /* Address is always specified.
493 */
494 addr = simple_strtoul(argv[1], NULL, 16);
495
496 /* Length is the number of objects, not number of bytes.
497 */
498 length = simple_strtoul(argv[2], NULL, 16);
499
500 /* data to write */
York Sun4d1fd7f2014-02-26 17:03:19 -0800501#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
502 data = simple_strtoull(argv[3], NULL, 16);
503#else
wdenk56523f12004-07-11 17:40:54 +0000504 data = simple_strtoul(argv[3], NULL, 16);
York Sun4d1fd7f2014-02-26 17:03:19 -0800505#endif
wdenk81050922004-07-11 20:04:51 +0000506
Simon Glass0628ab82013-02-24 17:33:15 +0000507 bytes = size * length;
508 buf = map_sysmem(addr, bytes);
509
wdenk56523f12004-07-11 17:40:54 +0000510 /* We want to optimize the loops to run as fast as possible.
511 * If we have only one object, just run infinite loops.
512 */
513 if (length == 1) {
York Sun4d1fd7f2014-02-26 17:03:19 -0800514#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
515 if (size == 8) {
516 llp = (u64 *)buf;
517 for (;;)
518 *llp = data;
519 }
520#endif
wdenk56523f12004-07-11 17:40:54 +0000521 if (size == 4) {
York Sun76698b42014-02-12 15:55:35 -0800522 longp = (u32 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000523 for (;;)
524 *longp = data;
York Sun4d1fd7f2014-02-26 17:03:19 -0800525 }
wdenk56523f12004-07-11 17:40:54 +0000526 if (size == 2) {
York Sun76698b42014-02-12 15:55:35 -0800527 shortp = (u16 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000528 for (;;)
529 *shortp = data;
530 }
York Sun76698b42014-02-12 15:55:35 -0800531 cp = (u8 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000532 for (;;)
533 *cp = data;
534 }
535
York Sun4d1fd7f2014-02-26 17:03:19 -0800536#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
537 if (size == 8) {
538 for (;;) {
539 llp = (u64 *)buf;
540 i = length;
541 while (i-- > 0)
542 *llp++ = data;
543 }
544 }
545#endif
wdenk56523f12004-07-11 17:40:54 +0000546 if (size == 4) {
547 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800548 longp = (u32 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000549 i = length;
550 while (i-- > 0)
551 *longp++ = data;
552 }
553 }
554 if (size == 2) {
555 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800556 shortp = (u16 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000557 i = length;
558 while (i-- > 0)
559 *shortp++ = data;
560 }
561 }
562 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800563 cp = (u8 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000564 i = length;
565 while (i-- > 0)
566 *cp++ = data;
567 }
568}
569#endif /* CONFIG_LOOPW */
570
Tom Rini68149e92013-03-12 10:07:19 -0400571#ifdef CONFIG_CMD_MEMTEST
Simon Glass5512d5b2013-02-28 17:47:14 +0000572static ulong mem_test_alt(vu_long *buf, ulong start_addr, ulong end_addr,
573 vu_long *dummy)
wdenk38635852002-08-27 05:55:31 +0000574{
Simon Glassc9638f52013-02-24 17:33:16 +0000575 vu_long *addr;
Simon Glassc9638f52013-02-24 17:33:16 +0000576 ulong errs = 0;
577 ulong val, readback;
578 int j;
Simon Glassc9638f52013-02-24 17:33:16 +0000579 vu_long offset;
580 vu_long test_offset;
581 vu_long pattern;
582 vu_long temp;
583 vu_long anti_pattern;
584 vu_long num_words;
wdenk38635852002-08-27 05:55:31 +0000585 static const ulong bitpattern[] = {
586 0x00000001, /* single bit */
587 0x00000003, /* two adjacent bits */
588 0x00000007, /* three adjacent bits */
589 0x0000000F, /* four adjacent bits */
590 0x00000005, /* two non-adjacent bits */
591 0x00000015, /* three non-adjacent bits */
592 0x00000055, /* four non-adjacent bits */
593 0xaaaaaaaa, /* alternating 1/0 */
594 };
wdenk38635852002-08-27 05:55:31 +0000595
Simon Glass5512d5b2013-02-28 17:47:14 +0000596 num_words = (end_addr - start_addr) / sizeof(vu_long);
Simon Glass8c86bbe2013-02-24 17:33:20 +0000597
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000598 /*
599 * Data line test: write a pattern to the first
600 * location, write the 1's complement to a 'parking'
601 * address (changes the state of the data bus so a
602 * floating bus doesn't give a false OK), and then
603 * read the value back. Note that we read it back
604 * into a variable because the next time we read it,
605 * it might be right (been there, tough to explain to
606 * the quality guys why it prints a failure when the
607 * "is" and "should be" are obviously the same in the
608 * error message).
609 *
610 * Rather than exhaustively testing, we test some
611 * patterns by shifting '1' bits through a field of
612 * '0's and '0' bits through a field of '1's (i.e.
613 * pattern and ~pattern).
614 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000615 addr = buf;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000616 for (j = 0; j < sizeof(bitpattern) / sizeof(bitpattern[0]); j++) {
617 val = bitpattern[j];
618 for (; val != 0; val <<= 1) {
Simon Glass5512d5b2013-02-28 17:47:14 +0000619 *addr = val;
Simon Glassc9638f52013-02-24 17:33:16 +0000620 *dummy = ~val; /* clear the test data off the bus */
wdenk38635852002-08-27 05:55:31 +0000621 readback = *addr;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000622 if (readback != val) {
Simon Glassc9638f52013-02-24 17:33:16 +0000623 printf("FAILURE (data line): "
624 "expected %08lx, actual %08lx\n",
625 val, readback);
626 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000627 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000628 return -1;
wdenk38635852002-08-27 05:55:31 +0000629 }
630 *addr = ~val;
631 *dummy = val;
632 readback = *addr;
Simon Glassc9638f52013-02-24 17:33:16 +0000633 if (readback != ~val) {
634 printf("FAILURE (data line): "
635 "Is %08lx, should be %08lx\n",
636 readback, ~val);
637 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000638 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000639 return -1;
wdenk38635852002-08-27 05:55:31 +0000640 }
wdenk38635852002-08-27 05:55:31 +0000641 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000642 }
wdenk38635852002-08-27 05:55:31 +0000643
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000644 /*
645 * Based on code whose Original Author and Copyright
646 * information follows: Copyright (c) 1998 by Michael
647 * Barr. This software is placed into the public
648 * domain and may be used for any purpose. However,
649 * this notice must not be changed or removed and no
650 * warranty is either expressed or implied by its
651 * publication or distribution.
652 */
wdenk38635852002-08-27 05:55:31 +0000653
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000654 /*
655 * Address line test
wdenk38635852002-08-27 05:55:31 +0000656
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000657 * Description: Test the address bus wiring in a
658 * memory region by performing a walking
659 * 1's test on the relevant bits of the
660 * address and checking for aliasing.
661 * This test will find single-bit
662 * address failures such as stuck-high,
663 * stuck-low, and shorted pins. The base
664 * address and size of the region are
665 * selected by the caller.
wdenk38635852002-08-27 05:55:31 +0000666
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000667 * Notes: For best results, the selected base
668 * address should have enough LSB 0's to
669 * guarantee single address bit changes.
670 * For example, to test a 64-Kbyte
671 * region, select a base address on a
672 * 64-Kbyte boundary. Also, select the
673 * region size as a power-of-two if at
674 * all possible.
675 *
676 * Returns: 0 if the test succeeds, 1 if the test fails.
677 */
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000678 pattern = (vu_long) 0xaaaaaaaa;
679 anti_pattern = (vu_long) 0x55555555;
wdenk38635852002-08-27 05:55:31 +0000680
Simon Glass5512d5b2013-02-28 17:47:14 +0000681 debug("%s:%d: length = 0x%.8lx\n", __func__, __LINE__, num_words);
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000682 /*
683 * Write the default pattern at each of the
684 * power-of-two offsets.
685 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000686 for (offset = 1; offset < num_words; offset <<= 1)
687 addr[offset] = pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000688
689 /*
690 * Check for address bits stuck high.
691 */
692 test_offset = 0;
Simon Glass5512d5b2013-02-28 17:47:14 +0000693 addr[test_offset] = anti_pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000694
Simon Glass5512d5b2013-02-28 17:47:14 +0000695 for (offset = 1; offset < num_words; offset <<= 1) {
696 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000697 if (temp != pattern) {
Simon Glassc9638f52013-02-24 17:33:16 +0000698 printf("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000699 " expected 0x%.8lx, actual 0x%.8lx\n",
David Feng102c0512014-02-12 16:10:08 +0800700 start_addr + offset*sizeof(vu_long),
701 pattern, temp);
Paul Gortmaker87b22b72009-10-02 18:18:33 -0400702 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000703 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000704 return -1;
wdenk38635852002-08-27 05:55:31 +0000705 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000706 }
Simon Glass5512d5b2013-02-28 17:47:14 +0000707 addr[test_offset] = pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000708 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000709
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000710 /*
711 * Check for addr bits stuck low or shorted.
712 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000713 for (test_offset = 1; test_offset < num_words; test_offset <<= 1) {
714 addr[test_offset] = anti_pattern;
wdenk38635852002-08-27 05:55:31 +0000715
Simon Glass5512d5b2013-02-28 17:47:14 +0000716 for (offset = 1; offset < num_words; offset <<= 1) {
717 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000718 if ((temp != pattern) && (offset != test_offset)) {
719 printf("\nFAILURE: Address bit stuck low or"
720 " shorted @ 0x%.8lx: expected 0x%.8lx,"
721 " actual 0x%.8lx\n",
David Feng102c0512014-02-12 16:10:08 +0800722 start_addr + offset*sizeof(vu_long),
723 pattern, temp);
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000724 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000725 if (ctrlc())
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000726 return -1;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000727 }
wdenk38635852002-08-27 05:55:31 +0000728 }
Simon Glass5512d5b2013-02-28 17:47:14 +0000729 addr[test_offset] = pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000730 }
wdenk38635852002-08-27 05:55:31 +0000731
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000732 /*
733 * Description: Test the integrity of a physical
734 * memory device by performing an
735 * increment/decrement test over the
736 * entire region. In the process every
737 * storage bit in the device is tested
738 * as a zero and a one. The base address
739 * and the size of the region are
740 * selected by the caller.
741 *
742 * Returns: 0 if the test succeeds, 1 if the test fails.
743 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000744 num_words++;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000745
746 /*
747 * Fill memory with a known pattern.
748 */
749 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
750 WATCHDOG_RESET();
Simon Glass5512d5b2013-02-28 17:47:14 +0000751 addr[offset] = pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000752 }
753
754 /*
755 * Check each location and invert it for the second pass.
756 */
757 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
758 WATCHDOG_RESET();
Simon Glass5512d5b2013-02-28 17:47:14 +0000759 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000760 if (temp != pattern) {
Simon Glassc9638f52013-02-24 17:33:16 +0000761 printf("\nFAILURE (read/write) @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000762 " expected 0x%.8lx, actual 0x%.8lx)\n",
David Feng102c0512014-02-12 16:10:08 +0800763 start_addr + offset*sizeof(vu_long),
764 pattern, temp);
Paul Gortmaker87b22b72009-10-02 18:18:33 -0400765 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000766 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000767 return -1;
wdenk38635852002-08-27 05:55:31 +0000768 }
769
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000770 anti_pattern = ~pattern;
Simon Glass5512d5b2013-02-28 17:47:14 +0000771 addr[offset] = anti_pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000772 }
773
774 /*
775 * Check each location for the inverted pattern and zero it.
776 */
777 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
778 WATCHDOG_RESET();
779 anti_pattern = ~pattern;
Simon Glass5512d5b2013-02-28 17:47:14 +0000780 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000781 if (temp != anti_pattern) {
Simon Glassc9638f52013-02-24 17:33:16 +0000782 printf("\nFAILURE (read/write): @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000783 " expected 0x%.8lx, actual 0x%.8lx)\n",
David Feng102c0512014-02-12 16:10:08 +0800784 start_addr + offset*sizeof(vu_long),
785 anti_pattern, temp);
Paul Gortmaker87b22b72009-10-02 18:18:33 -0400786 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000787 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000788 return -1;
wdenk38635852002-08-27 05:55:31 +0000789 }
Simon Glass5512d5b2013-02-28 17:47:14 +0000790 addr[offset] = 0;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000791 }
Simon Glass51209b12013-02-24 17:33:17 +0000792
Rasmus Villemoesfea67302016-01-07 11:36:04 +0100793 return errs;
Simon Glassc9638f52013-02-24 17:33:16 +0000794}
wdenk38635852002-08-27 05:55:31 +0000795
Simon Glass5512d5b2013-02-28 17:47:14 +0000796static ulong mem_test_quick(vu_long *buf, ulong start_addr, ulong end_addr,
797 vu_long pattern, int iteration)
Simon Glassc9638f52013-02-24 17:33:16 +0000798{
Simon Glass5512d5b2013-02-28 17:47:14 +0000799 vu_long *end;
Simon Glassc9638f52013-02-24 17:33:16 +0000800 vu_long *addr;
Simon Glassc9638f52013-02-24 17:33:16 +0000801 ulong errs = 0;
Simon Glass5512d5b2013-02-28 17:47:14 +0000802 ulong incr, length;
Simon Glassc9638f52013-02-24 17:33:16 +0000803 ulong val, readback;
804
Simon Glass51209b12013-02-24 17:33:17 +0000805 /* Alternate the pattern */
wdenk38635852002-08-27 05:55:31 +0000806 incr = 1;
Simon Glass51209b12013-02-24 17:33:17 +0000807 if (iteration & 1) {
808 incr = -incr;
809 /*
810 * Flip the pattern each time to make lots of zeros and
811 * then, the next time, lots of ones. We decrement
812 * the "negative" patterns and increment the "positive"
813 * patterns to preserve this feature.
814 */
815 if (pattern & 0x80000000)
816 pattern = -pattern; /* complement & increment */
817 else
818 pattern = ~pattern;
819 }
Simon Glass5512d5b2013-02-28 17:47:14 +0000820 length = (end_addr - start_addr) / sizeof(ulong);
821 end = buf + length;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000822 printf("\rPattern %08lX Writing..."
823 "%12s"
824 "\b\b\b\b\b\b\b\b\b\b",
825 pattern, "");
wdenk38635852002-08-27 05:55:31 +0000826
Simon Glass5512d5b2013-02-28 17:47:14 +0000827 for (addr = buf, val = pattern; addr < end; addr++) {
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000828 WATCHDOG_RESET();
829 *addr = val;
830 val += incr;
831 }
wdenk38635852002-08-27 05:55:31 +0000832
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000833 puts("Reading...");
wdenk38635852002-08-27 05:55:31 +0000834
Simon Glass5512d5b2013-02-28 17:47:14 +0000835 for (addr = buf, val = pattern; addr < end; addr++) {
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000836 WATCHDOG_RESET();
837 readback = *addr;
838 if (readback != val) {
Simon Glass5512d5b2013-02-28 17:47:14 +0000839 ulong offset = addr - buf;
840
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000841 printf("\nMem error @ 0x%08X: "
842 "found %08lX, expected %08lX\n",
David Feng102c0512014-02-12 16:10:08 +0800843 (uint)(uintptr_t)(start_addr + offset*sizeof(vu_long)),
Simon Glass5512d5b2013-02-28 17:47:14 +0000844 readback, val);
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000845 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000846 if (ctrlc())
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000847 return -1;
wdenk38635852002-08-27 05:55:31 +0000848 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000849 val += incr;
850 }
wdenk38635852002-08-27 05:55:31 +0000851
Rasmus Villemoesfea67302016-01-07 11:36:04 +0100852 return errs;
Simon Glassc9638f52013-02-24 17:33:16 +0000853}
854
855/*
856 * Perform a memory test. A more complete alternative test can be
857 * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
858 * interrupted by ctrl-c or by a failure of one of the sub-tests.
859 */
860static int do_mem_mtest(cmd_tbl_t *cmdtp, int flag, int argc,
861 char * const argv[])
862{
Simon Glass8c86bbe2013-02-24 17:33:20 +0000863 ulong start, end;
Simon Glass5512d5b2013-02-28 17:47:14 +0000864 vu_long *buf, *dummy;
Tom Riniadcc5702015-04-07 09:38:54 -0400865 ulong iteration_limit = 0;
Simon Glassc9638f52013-02-24 17:33:16 +0000866 int ret;
Simon Glass51209b12013-02-24 17:33:17 +0000867 ulong errs = 0; /* number of errors, or -1 if interrupted */
Pavel Macheke37f1eb2015-04-01 13:50:41 +0200868 ulong pattern = 0;
Simon Glass51209b12013-02-24 17:33:17 +0000869 int iteration;
Simon Glassc9638f52013-02-24 17:33:16 +0000870#if defined(CONFIG_SYS_ALT_MEMTEST)
871 const int alt_test = 1;
872#else
873 const int alt_test = 0;
wdenk38635852002-08-27 05:55:31 +0000874#endif
Simon Glassc9638f52013-02-24 17:33:16 +0000875
Pavel Macheke37f1eb2015-04-01 13:50:41 +0200876 start = CONFIG_SYS_MEMTEST_START;
877 end = CONFIG_SYS_MEMTEST_END;
878
Simon Glassc9638f52013-02-24 17:33:16 +0000879 if (argc > 1)
Pavel Macheke37f1eb2015-04-01 13:50:41 +0200880 if (strict_strtoul(argv[1], 16, &start) < 0)
881 return CMD_RET_USAGE;
Simon Glassc9638f52013-02-24 17:33:16 +0000882
883 if (argc > 2)
Pavel Macheke37f1eb2015-04-01 13:50:41 +0200884 if (strict_strtoul(argv[2], 16, &end) < 0)
885 return CMD_RET_USAGE;
Simon Glassc9638f52013-02-24 17:33:16 +0000886
887 if (argc > 3)
Pavel Macheke37f1eb2015-04-01 13:50:41 +0200888 if (strict_strtoul(argv[3], 16, &pattern) < 0)
889 return CMD_RET_USAGE;
Simon Glassc9638f52013-02-24 17:33:16 +0000890
891 if (argc > 4)
Pavel Macheke37f1eb2015-04-01 13:50:41 +0200892 if (strict_strtoul(argv[4], 16, &iteration_limit) < 0)
893 return CMD_RET_USAGE;
894
895 if (end < start) {
896 printf("Refusing to do empty test\n");
897 return -1;
898 }
Simon Glassc9638f52013-02-24 17:33:16 +0000899
Michal Simekdfe461d2016-02-24 08:36:02 +0100900 printf("Testing %08lx ... %08lx:\n", start, end);
Simon Glass8c86bbe2013-02-24 17:33:20 +0000901 debug("%s:%d: start %#08lx end %#08lx\n", __func__, __LINE__,
902 start, end);
Simon Glass51209b12013-02-24 17:33:17 +0000903
Simon Glass5512d5b2013-02-28 17:47:14 +0000904 buf = map_sysmem(start, end - start);
905 dummy = map_sysmem(CONFIG_SYS_MEMTEST_SCRATCH, sizeof(vu_long));
Simon Glass51209b12013-02-24 17:33:17 +0000906 for (iteration = 0;
907 !iteration_limit || iteration < iteration_limit;
908 iteration++) {
909 if (ctrlc()) {
Simon Glass51209b12013-02-24 17:33:17 +0000910 errs = -1UL;
911 break;
912 }
913
914 printf("Iteration: %6d\r", iteration + 1);
915 debug("\n");
Simon Glass5512d5b2013-02-28 17:47:14 +0000916 if (alt_test) {
917 errs = mem_test_alt(buf, start, end, dummy);
918 } else {
919 errs = mem_test_quick(buf, start, end, pattern,
920 iteration);
921 }
922 if (errs == -1UL)
923 break;
924 }
925
926 /*
927 * Work-around for eldk-4.2 which gives this warning if we try to
928 * case in the unmap_sysmem() call:
929 * warning: initialization discards qualifiers from pointer target type
930 */
931 {
932 void *vbuf = (void *)buf;
933 void *vdummy = (void *)dummy;
934
935 unmap_sysmem(vbuf);
936 unmap_sysmem(vdummy);
Simon Glass51209b12013-02-24 17:33:17 +0000937 }
938
939 if (errs == -1UL) {
Simon Glassc44d4382013-02-24 17:33:19 +0000940 /* Memory test was aborted - write a newline to finish off */
941 putc('\n');
Simon Glass51209b12013-02-24 17:33:17 +0000942 ret = 1;
943 } else {
944 printf("Tested %d iteration(s) with %lu errors.\n",
945 iteration, errs);
946 ret = errs != 0;
947 }
Simon Glassc9638f52013-02-24 17:33:16 +0000948
Pavel Macheke37f1eb2015-04-01 13:50:41 +0200949 return ret;
wdenk38635852002-08-27 05:55:31 +0000950}
Wolfgang Denka2681702013-03-08 10:51:32 +0000951#endif /* CONFIG_CMD_MEMTEST */
wdenk38635852002-08-27 05:55:31 +0000952
953/* Modify memory.
954 *
955 * Syntax:
York Sun4d1fd7f2014-02-26 17:03:19 -0800956 * mm{.b, .w, .l, .q} {addr}
957 * nm{.b, .w, .l, .q} {addr}
wdenk38635852002-08-27 05:55:31 +0000958 */
959static int
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200960mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000961{
York Sun4d1fd7f2014-02-26 17:03:19 -0800962 ulong addr;
963#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
964 u64 i;
965#else
966 ulong i;
967#endif
wdenk27b207f2003-07-24 23:38:38 +0000968 int nbytes, size;
Simon Glass0628ab82013-02-24 17:33:15 +0000969 void *ptr = NULL;
wdenk38635852002-08-27 05:55:31 +0000970
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200971 if (argc != 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000972 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000973
Simon Glassb26440f2014-04-10 20:01:31 -0600974 bootretry_reset_cmd_timeout(); /* got a good command to get here */
wdenk38635852002-08-27 05:55:31 +0000975 /* We use the last specified parameters, unless new ones are
976 * entered.
977 */
978 addr = mm_last_addr;
979 size = mm_last_size;
980
981 if ((flag & CMD_FLAG_REPEAT) == 0) {
982 /* New command specified. Check for a size specification.
983 * Defaults to long if no or incorrect specification.
984 */
wdenk27b207f2003-07-24 23:38:38 +0000985 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
986 return 1;
wdenk38635852002-08-27 05:55:31 +0000987
988 /* Address is specified since argc > 1
989 */
990 addr = simple_strtoul(argv[1], NULL, 16);
991 addr += base_address;
992 }
993
994 /* Print the address, followed by value. Then accept input for
995 * the next value. A non-converted value exits.
996 */
997 do {
Simon Glass0628ab82013-02-24 17:33:15 +0000998 ptr = map_sysmem(addr, size);
wdenk38635852002-08-27 05:55:31 +0000999 printf("%08lx:", addr);
1000 if (size == 4)
York Sun76698b42014-02-12 15:55:35 -08001001 printf(" %08x", *((u32 *)ptr));
York Sun4d1fd7f2014-02-26 17:03:19 -08001002#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1003 else if (size == 8)
Simon Glasse48f3742014-11-11 12:47:07 -07001004 printf(" %016" PRIx64, *((u64 *)ptr));
York Sun4d1fd7f2014-02-26 17:03:19 -08001005#endif
wdenk38635852002-08-27 05:55:31 +00001006 else if (size == 2)
York Sun76698b42014-02-12 15:55:35 -08001007 printf(" %04x", *((u16 *)ptr));
wdenk38635852002-08-27 05:55:31 +00001008 else
York Sun76698b42014-02-12 15:55:35 -08001009 printf(" %02x", *((u8 *)ptr));
wdenk38635852002-08-27 05:55:31 +00001010
Simon Glasse1bf8242014-04-10 20:01:27 -06001011 nbytes = cli_readline(" ? ");
wdenk38635852002-08-27 05:55:31 +00001012 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
1013 /* <CR> pressed as only input, don't modify current
1014 * location and move to next. "-" pressed will go back.
1015 */
1016 if (incrflag)
1017 addr += nbytes ? -size : size;
1018 nbytes = 1;
Simon Glassb26440f2014-04-10 20:01:31 -06001019 /* good enough to not time out */
1020 bootretry_reset_cmd_timeout();
wdenk38635852002-08-27 05:55:31 +00001021 }
1022#ifdef CONFIG_BOOT_RETRY_TIME
1023 else if (nbytes == -2) {
1024 break; /* timed out, exit the command */
1025 }
1026#endif
1027 else {
1028 char *endp;
York Sun4d1fd7f2014-02-26 17:03:19 -08001029#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1030 i = simple_strtoull(console_buffer, &endp, 16);
1031#else
wdenk38635852002-08-27 05:55:31 +00001032 i = simple_strtoul(console_buffer, &endp, 16);
York Sun4d1fd7f2014-02-26 17:03:19 -08001033#endif
wdenk38635852002-08-27 05:55:31 +00001034 nbytes = endp - console_buffer;
1035 if (nbytes) {
wdenk38635852002-08-27 05:55:31 +00001036 /* good enough to not time out
1037 */
Simon Glassb26440f2014-04-10 20:01:31 -06001038 bootretry_reset_cmd_timeout();
wdenk38635852002-08-27 05:55:31 +00001039 if (size == 4)
York Sun76698b42014-02-12 15:55:35 -08001040 *((u32 *)ptr) = i;
York Sun4d1fd7f2014-02-26 17:03:19 -08001041#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1042 else if (size == 8)
1043 *((u64 *)ptr) = i;
1044#endif
wdenk38635852002-08-27 05:55:31 +00001045 else if (size == 2)
York Sun76698b42014-02-12 15:55:35 -08001046 *((u16 *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001047 else
York Sun76698b42014-02-12 15:55:35 -08001048 *((u8 *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001049 if (incrflag)
1050 addr += size;
1051 }
1052 }
1053 } while (nbytes);
Simon Glass0628ab82013-02-24 17:33:15 +00001054 if (ptr)
1055 unmap_sysmem(ptr);
wdenk38635852002-08-27 05:55:31 +00001056
1057 mm_last_addr = addr;
1058 mm_last_size = size;
1059 return 0;
1060}
1061
Mike Frysinger710b9932010-12-21 14:19:51 -05001062#ifdef CONFIG_CMD_CRC32
1063
Kim Phillips088f1b12012-10-29 13:34:31 +00001064static int do_mem_crc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +00001065{
Simon Glassd20a40d2013-02-24 20:30:22 +00001066 int flags = 0;
1067 int ac;
1068 char * const *av;
wdenk38635852002-08-27 05:55:31 +00001069
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001070 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001071 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +00001072
wdenkc26e4542004-04-18 10:13:26 +00001073 av = argv + 1;
1074 ac = argc - 1;
Daniel Thompson221a9492017-05-19 17:26:58 +01001075#ifdef CONFIG_CRC32_VERIFY
wdenkc26e4542004-04-18 10:13:26 +00001076 if (strcmp(*av, "-v") == 0) {
Joe Hershbergera69bdba2015-05-05 12:23:53 -05001077 flags |= HASH_FLAG_VERIFY | HASH_FLAG_ENV;
wdenkc26e4542004-04-18 10:13:26 +00001078 av++;
1079 ac--;
wdenkc26e4542004-04-18 10:13:26 +00001080 }
Simon Glassd20a40d2013-02-24 20:30:22 +00001081#endif
wdenkc26e4542004-04-18 10:13:26 +00001082
Simon Glassd20a40d2013-02-24 20:30:22 +00001083 return hash_command("crc32", flags, cmdtp, flag, ac, av);
wdenkc26e4542004-04-18 10:13:26 +00001084}
wdenkc26e4542004-04-18 10:13:26 +00001085
Mike Frysinger710b9932010-12-21 14:19:51 -05001086#endif
1087
wdenk8bde7f72003-06-27 21:31:46 +00001088/**************************************************/
wdenk0d498392003-07-01 21:06:45 +00001089U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001090 md, 3, 1, do_mem_md,
Peter Tyser2fb26042009-01-27 18:03:12 -06001091 "memory display",
York Sun4d1fd7f2014-02-26 17:03:19 -08001092#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1093 "[.b, .w, .l, .q] address [# of objects]"
1094#else
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001095 "[.b, .w, .l] address [# of objects]"
York Sun4d1fd7f2014-02-26 17:03:19 -08001096#endif
wdenk8bde7f72003-06-27 21:31:46 +00001097);
1098
1099
wdenk0d498392003-07-01 21:06:45 +00001100U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001101 mm, 2, 1, do_mem_mm,
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001102 "memory modify (auto-incrementing address)",
York Sun4d1fd7f2014-02-26 17:03:19 -08001103#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1104 "[.b, .w, .l, .q] address"
1105#else
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001106 "[.b, .w, .l] address"
York Sun4d1fd7f2014-02-26 17:03:19 -08001107#endif
wdenk8bde7f72003-06-27 21:31:46 +00001108);
1109
1110
wdenk0d498392003-07-01 21:06:45 +00001111U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001112 nm, 2, 1, do_mem_nm,
Peter Tyser2fb26042009-01-27 18:03:12 -06001113 "memory modify (constant address)",
York Sun4d1fd7f2014-02-26 17:03:19 -08001114#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1115 "[.b, .w, .l, .q] address"
1116#else
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001117 "[.b, .w, .l] address"
York Sun4d1fd7f2014-02-26 17:03:19 -08001118#endif
wdenk8bde7f72003-06-27 21:31:46 +00001119);
1120
wdenk0d498392003-07-01 21:06:45 +00001121U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001122 mw, 4, 1, do_mem_mw,
Peter Tyser2fb26042009-01-27 18:03:12 -06001123 "memory write (fill)",
York Sun4d1fd7f2014-02-26 17:03:19 -08001124#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1125 "[.b, .w, .l, .q] address value [count]"
1126#else
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001127 "[.b, .w, .l] address value [count]"
York Sun4d1fd7f2014-02-26 17:03:19 -08001128#endif
wdenk8bde7f72003-06-27 21:31:46 +00001129);
1130
wdenk0d498392003-07-01 21:06:45 +00001131U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001132 cp, 4, 1, do_mem_cp,
Peter Tyser2fb26042009-01-27 18:03:12 -06001133 "memory copy",
York Sun4d1fd7f2014-02-26 17:03:19 -08001134#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1135 "[.b, .w, .l, .q] source target count"
1136#else
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001137 "[.b, .w, .l] source target count"
York Sun4d1fd7f2014-02-26 17:03:19 -08001138#endif
wdenk8bde7f72003-06-27 21:31:46 +00001139);
1140
wdenk0d498392003-07-01 21:06:45 +00001141U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001142 cmp, 4, 1, do_mem_cmp,
Peter Tyser2fb26042009-01-27 18:03:12 -06001143 "memory compare",
York Sun4d1fd7f2014-02-26 17:03:19 -08001144#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1145 "[.b, .w, .l, .q] addr1 addr2 count"
1146#else
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001147 "[.b, .w, .l] addr1 addr2 count"
York Sun4d1fd7f2014-02-26 17:03:19 -08001148#endif
wdenk8bde7f72003-06-27 21:31:46 +00001149);
1150
Mike Frysinger710b9932010-12-21 14:19:51 -05001151#ifdef CONFIG_CMD_CRC32
1152
Daniel Thompson221a9492017-05-19 17:26:58 +01001153#ifndef CONFIG_CRC32_VERIFY
wdenkc26e4542004-04-18 10:13:26 +00001154
wdenk0d498392003-07-01 21:06:45 +00001155U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001156 crc32, 4, 1, do_mem_crc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001157 "checksum calculation",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001158 "address count [addr]\n - compute CRC32 checksum [save at addr]"
wdenk8bde7f72003-06-27 21:31:46 +00001159);
1160
Daniel Thompson221a9492017-05-19 17:26:58 +01001161#else /* CONFIG_CRC32_VERIFY */
wdenkc26e4542004-04-18 10:13:26 +00001162
1163U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001164 crc32, 5, 1, do_mem_crc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001165 "checksum calculation",
wdenkc26e4542004-04-18 10:13:26 +00001166 "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001167 "-v address count crc\n - verify crc of memory area"
wdenkc26e4542004-04-18 10:13:26 +00001168);
1169
Daniel Thompson221a9492017-05-19 17:26:58 +01001170#endif /* CONFIG_CRC32_VERIFY */
wdenkc26e4542004-04-18 10:13:26 +00001171
Mike Frysinger710b9932010-12-21 14:19:51 -05001172#endif
1173
Simon Glass15a33e42012-11-30 13:01:20 +00001174#ifdef CONFIG_CMD_MEMINFO
Andrew Bradfordea11b402015-05-22 08:30:14 -04001175__weak void board_show_dram(phys_size_t size)
Simon Glass15a33e42012-11-30 13:01:20 +00001176{
1177 puts("DRAM: ");
1178 print_size(size, "\n");
1179}
1180
1181static int do_mem_info(cmd_tbl_t *cmdtp, int flag, int argc,
1182 char * const argv[])
1183{
1184 board_show_dram(gd->ram_size);
1185
1186 return 0;
1187}
1188#endif
1189
wdenk0d498392003-07-01 21:06:45 +00001190U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001191 base, 2, 1, do_mem_base,
Peter Tyser2fb26042009-01-27 18:03:12 -06001192 "print or set address offset",
wdenk8bde7f72003-06-27 21:31:46 +00001193 "\n - print address offset for memory commands\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001194 "base off\n - set address offset for memory commands to 'off'"
wdenk8bde7f72003-06-27 21:31:46 +00001195);
1196
wdenk0d498392003-07-01 21:06:45 +00001197U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001198 loop, 3, 1, do_mem_loop,
Peter Tyser2fb26042009-01-27 18:03:12 -06001199 "infinite loop on address range",
York Sun4d1fd7f2014-02-26 17:03:19 -08001200#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1201 "[.b, .w, .l, .q] address number_of_objects"
1202#else
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001203 "[.b, .w, .l] address number_of_objects"
York Sun4d1fd7f2014-02-26 17:03:19 -08001204#endif
wdenk8bde7f72003-06-27 21:31:46 +00001205);
1206
wdenk56523f12004-07-11 17:40:54 +00001207#ifdef CONFIG_LOOPW
1208U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001209 loopw, 4, 1, do_mem_loopw,
Peter Tyser2fb26042009-01-27 18:03:12 -06001210 "infinite write loop on address range",
York Sun4d1fd7f2014-02-26 17:03:19 -08001211#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1212 "[.b, .w, .l, .q] address number_of_objects data_to_write"
1213#else
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001214 "[.b, .w, .l] address number_of_objects data_to_write"
York Sun4d1fd7f2014-02-26 17:03:19 -08001215#endif
wdenk56523f12004-07-11 17:40:54 +00001216);
1217#endif /* CONFIG_LOOPW */
1218
Wolfgang Denka2681702013-03-08 10:51:32 +00001219#ifdef CONFIG_CMD_MEMTEST
wdenk0d498392003-07-01 21:06:45 +00001220U_BOOT_CMD(
Dirk Eibachb6fc6fd2008-12-16 14:51:56 +01001221 mtest, 5, 1, do_mem_mtest,
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001222 "simple RAM read/write test",
1223 "[start [end [pattern [iterations]]]]"
wdenk8bde7f72003-06-27 21:31:46 +00001224);
Wolfgang Denka2681702013-03-08 10:51:32 +00001225#endif /* CONFIG_CMD_MEMTEST */
wdenk8bde7f72003-06-27 21:31:46 +00001226
stroese4aaf29b2004-12-16 17:42:39 +00001227#ifdef CONFIG_MX_CYCLIC
1228U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001229 mdc, 4, 1, do_mem_mdc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001230 "memory display cyclic",
York Sun4d1fd7f2014-02-26 17:03:19 -08001231#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1232 "[.b, .w, .l, .q] address count delay(ms)"
1233#else
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001234 "[.b, .w, .l] address count delay(ms)"
York Sun4d1fd7f2014-02-26 17:03:19 -08001235#endif
stroese4aaf29b2004-12-16 17:42:39 +00001236);
1237
1238U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001239 mwc, 4, 1, do_mem_mwc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001240 "memory write cyclic",
York Sun4d1fd7f2014-02-26 17:03:19 -08001241#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1242 "[.b, .w, .l, .q] address value delay(ms)"
1243#else
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001244 "[.b, .w, .l] address value delay(ms)"
York Sun4d1fd7f2014-02-26 17:03:19 -08001245#endif
stroese4aaf29b2004-12-16 17:42:39 +00001246);
1247#endif /* CONFIG_MX_CYCLIC */
Simon Glass15a33e42012-11-30 13:01:20 +00001248
1249#ifdef CONFIG_CMD_MEMINFO
1250U_BOOT_CMD(
1251 meminfo, 3, 1, do_mem_info,
1252 "display memory information",
1253 ""
1254);
1255#endif