blob: 575893c18dfe2ca16db7bc4a02bbeacf1071617c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk38635852002-08-27 05:55:31 +00002/*
3 * (C) Copyright 2000
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk38635852002-08-27 05:55:31 +00005 */
6
7/*
8 * Memory Functions
9 *
10 * Copied from FADS ROM, Dan Malek (dmalek@jlc.net)
11 */
12
13#include <common.h>
Simon Glass24b852a2015-11-08 23:47:45 -070014#include <console.h>
Simon Glass0098e172014-04-10 20:01:30 -060015#include <bootretry.h>
Simon Glass18d66532014-04-10 20:01:25 -060016#include <cli.h>
wdenk38635852002-08-27 05:55:31 +000017#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070018#include <console.h>
Simon Glass0ee482522019-12-28 10:44:40 -070019#include <flash.h>
Simon Glassd20a40d2013-02-24 20:30:22 +000020#include <hash.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060021#include <log.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050022#include <mapmem.h>
Simon Glass90526e92020-05-10 11:39:56 -060023#include <rand.h>
Sergei Poselenova6e6fc62008-04-09 16:09:41 +020024#include <watchdog.h>
Simon Glass0628ab82013-02-24 17:33:15 +000025#include <asm/io.h>
Simon Glasscd93d622020-05-10 11:40:13 -060026#include <linux/bitops.h>
Simon Glass15a33e42012-11-30 13:01:20 +000027#include <linux/compiler.h>
Simon Glassbdded202020-06-02 19:26:49 -060028#include <linux/ctype.h>
Simon Glassc05ed002020-05-10 11:40:11 -060029#include <linux/delay.h>
Simon Glass15a33e42012-11-30 13:01:20 +000030
31DECLARE_GLOBAL_DATA_PTR;
wdenk38635852002-08-27 05:55:31 +000032
Simon Glass09140112020-05-10 11:40:03 -060033#ifndef CONFIG_SYS_MEMTEST_SCRATCH
34#define CONFIG_SYS_MEMTEST_SCRATCH 0
35#endif
36
Simon Glass76be8f72020-06-02 19:26:45 -060037/* Create a compile-time value */
Simon Glass46809762020-06-02 19:26:46 -060038#ifdef MEM_SUPPORT_64BIT_DATA
39#define SUPPORT_64BIT_DATA 1
Simon Glass76be8f72020-06-02 19:26:45 -060040#define HELP_Q ", .q"
41#else
Simon Glass46809762020-06-02 19:26:46 -060042#define SUPPORT_64BIT_DATA 0
Simon Glass76be8f72020-06-02 19:26:45 -060043#define HELP_Q ""
44#endif
45
Simon Glass09140112020-05-10 11:40:03 -060046static int mod_mem(struct cmd_tbl *, int, int, int, char * const []);
wdenk38635852002-08-27 05:55:31 +000047
48/* Display values from last command.
49 * Memory modify remembered values are different from display memory.
50 */
Scott Wood581508b2015-03-19 09:43:12 -070051static ulong dp_last_addr, dp_last_size;
52static ulong dp_last_length = 0x40;
53static ulong mm_last_addr, mm_last_size;
wdenk38635852002-08-27 05:55:31 +000054
55static ulong base_address = 0;
Simon Glassbdded202020-06-02 19:26:49 -060056#ifdef CONFIG_MEM_SEARCH
57static u8 search_buf[64];
58static uint search_len;
59#endif
wdenk38635852002-08-27 05:55:31 +000060
61/* Memory Display
62 *
63 * Syntax:
York Sun4d1fd7f2014-02-26 17:03:19 -080064 * md{.b, .w, .l, .q} {addr} {len}
wdenk38635852002-08-27 05:55:31 +000065 */
66#define DISP_LINE_LEN 16
Simon Glass09140112020-05-10 11:40:03 -060067static int do_mem_md(struct cmd_tbl *cmdtp, int flag, int argc,
68 char *const argv[])
wdenk38635852002-08-27 05:55:31 +000069{
Tuomas Tynkkynenc68c03f2017-10-10 21:59:42 +030070 ulong addr, length, bytes;
71 const void *buf;
wdenk27b207f2003-07-24 23:38:38 +000072 int size;
wdenk38635852002-08-27 05:55:31 +000073 int rc = 0;
74
75 /* We use the last specified parameters, unless new ones are
76 * entered.
77 */
78 addr = dp_last_addr;
79 size = dp_last_size;
80 length = dp_last_length;
81
Wolfgang Denk47e26b12010-07-17 01:06:04 +020082 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +000083 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +000084
85 if ((flag & CMD_FLAG_REPEAT) == 0) {
86 /* New command specified. Check for a size specification.
87 * Defaults to long if no or incorrect specification.
88 */
wdenk27b207f2003-07-24 23:38:38 +000089 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
90 return 1;
wdenk38635852002-08-27 05:55:31 +000091
92 /* Address is specified since argc > 1
93 */
94 addr = simple_strtoul(argv[1], NULL, 16);
95 addr += base_address;
96
97 /* If another parameter, it is the length to display.
98 * Length is the number of objects, not number of bytes.
99 */
100 if (argc > 2)
101 length = simple_strtoul(argv[2], NULL, 16);
102 }
103
Tuomas Tynkkynenc68c03f2017-10-10 21:59:42 +0300104 bytes = size * length;
105 buf = map_sysmem(addr, bytes);
wdenk2abbe072003-06-16 23:50:08 +0000106
Tuomas Tynkkynenc68c03f2017-10-10 21:59:42 +0300107 /* Print the lines. */
108 print_buffer(addr, buf, size, length, DISP_LINE_LEN / size);
109 addr += bytes;
110 unmap_sysmem(buf);
wdenk38635852002-08-27 05:55:31 +0000111
112 dp_last_addr = addr;
113 dp_last_length = length;
114 dp_last_size = size;
115 return (rc);
116}
117
Simon Glass09140112020-05-10 11:40:03 -0600118static int do_mem_mm(struct cmd_tbl *cmdtp, int flag, int argc,
119 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000120{
121 return mod_mem (cmdtp, 1, flag, argc, argv);
122}
Simon Glass09140112020-05-10 11:40:03 -0600123
124static int do_mem_nm(struct cmd_tbl *cmdtp, int flag, int argc,
125 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000126{
127 return mod_mem (cmdtp, 0, flag, argc, argv);
128}
129
Simon Glass09140112020-05-10 11:40:03 -0600130static int do_mem_mw(struct cmd_tbl *cmdtp, int flag, int argc,
131 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000132{
Simon Glass46809762020-06-02 19:26:46 -0600133 ulong writeval; /* 64-bit if SUPPORT_64BIT_DATA */
York Sun4d1fd7f2014-02-26 17:03:19 -0800134 ulong addr, count;
wdenk27b207f2003-07-24 23:38:38 +0000135 int size;
Simon Glasscc5e1962015-03-05 12:25:18 -0700136 void *buf, *start;
Simon Glass0628ab82013-02-24 17:33:15 +0000137 ulong bytes;
wdenk38635852002-08-27 05:55:31 +0000138
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200139 if ((argc < 3) || (argc > 4))
Simon Glass4c12eeb2011-12-10 08:44:01 +0000140 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000141
142 /* Check for size specification.
143 */
wdenk27b207f2003-07-24 23:38:38 +0000144 if ((size = cmd_get_data_size(argv[0], 4)) < 1)
145 return 1;
wdenk38635852002-08-27 05:55:31 +0000146
147 /* Address is specified since argc > 1
148 */
149 addr = simple_strtoul(argv[1], NULL, 16);
150 addr += base_address;
151
152 /* Get the value to write.
153 */
Simon Glass46809762020-06-02 19:26:46 -0600154 if (SUPPORT_64BIT_DATA)
155 writeval = simple_strtoull(argv[2], NULL, 16);
156 else
157 writeval = simple_strtoul(argv[2], NULL, 16);
wdenk38635852002-08-27 05:55:31 +0000158
159 /* Count ? */
160 if (argc == 4) {
161 count = simple_strtoul(argv[3], NULL, 16);
162 } else {
163 count = 1;
164 }
165
Simon Glass0628ab82013-02-24 17:33:15 +0000166 bytes = size * count;
Simon Glasscc5e1962015-03-05 12:25:18 -0700167 start = map_sysmem(addr, bytes);
168 buf = start;
wdenk38635852002-08-27 05:55:31 +0000169 while (count-- > 0) {
170 if (size == 4)
York Sun76698b42014-02-12 15:55:35 -0800171 *((u32 *)buf) = (u32)writeval;
Simon Glass46809762020-06-02 19:26:46 -0600172 else if (SUPPORT_64BIT_DATA && size == 8)
173 *((ulong *)buf) = writeval;
wdenk38635852002-08-27 05:55:31 +0000174 else if (size == 2)
York Sun76698b42014-02-12 15:55:35 -0800175 *((u16 *)buf) = (u16)writeval;
wdenk38635852002-08-27 05:55:31 +0000176 else
York Sun76698b42014-02-12 15:55:35 -0800177 *((u8 *)buf) = (u8)writeval;
Simon Glass0628ab82013-02-24 17:33:15 +0000178 buf += size;
wdenk38635852002-08-27 05:55:31 +0000179 }
Simon Glasscc5e1962015-03-05 12:25:18 -0700180 unmap_sysmem(start);
wdenk38635852002-08-27 05:55:31 +0000181 return 0;
182}
183
Joel Johnson72732312020-01-29 09:17:18 -0700184#ifdef CONFIG_CMD_MX_CYCLIC
Simon Glass09140112020-05-10 11:40:03 -0600185static int do_mem_mdc(struct cmd_tbl *cmdtp, int flag, int argc,
186 char *const argv[])
stroese4aaf29b2004-12-16 17:42:39 +0000187{
188 int i;
189 ulong count;
190
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200191 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000192 return CMD_RET_USAGE;
stroese4aaf29b2004-12-16 17:42:39 +0000193
194 count = simple_strtoul(argv[3], NULL, 10);
195
196 for (;;) {
197 do_mem_md (NULL, 0, 3, argv);
198
199 /* delay for <count> ms... */
200 for (i=0; i<count; i++)
Simon Glass07e11142020-05-10 11:40:10 -0600201 udelay(1000);
stroese4aaf29b2004-12-16 17:42:39 +0000202
203 /* check for ctrl-c to abort... */
204 if (ctrlc()) {
205 puts("Abort\n");
206 return 0;
207 }
208 }
209
210 return 0;
211}
212
Simon Glass09140112020-05-10 11:40:03 -0600213static int do_mem_mwc(struct cmd_tbl *cmdtp, int flag, int argc,
214 char *const argv[])
stroese4aaf29b2004-12-16 17:42:39 +0000215{
216 int i;
217 ulong count;
218
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200219 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000220 return CMD_RET_USAGE;
stroese4aaf29b2004-12-16 17:42:39 +0000221
222 count = simple_strtoul(argv[3], NULL, 10);
223
224 for (;;) {
225 do_mem_mw (NULL, 0, 3, argv);
226
227 /* delay for <count> ms... */
228 for (i=0; i<count; i++)
Simon Glass07e11142020-05-10 11:40:10 -0600229 udelay(1000);
stroese4aaf29b2004-12-16 17:42:39 +0000230
231 /* check for ctrl-c to abort... */
232 if (ctrlc()) {
233 puts("Abort\n");
234 return 0;
235 }
236 }
237
238 return 0;
239}
Joel Johnson72732312020-01-29 09:17:18 -0700240#endif /* CONFIG_CMD_MX_CYCLIC */
stroese4aaf29b2004-12-16 17:42:39 +0000241
Simon Glass09140112020-05-10 11:40:03 -0600242static int do_mem_cmp(struct cmd_tbl *cmdtp, int flag, int argc,
243 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000244{
Simon Glass0628ab82013-02-24 17:33:15 +0000245 ulong addr1, addr2, count, ngood, bytes;
wdenk27b207f2003-07-24 23:38:38 +0000246 int size;
wdenk38635852002-08-27 05:55:31 +0000247 int rcode = 0;
Mike Frysinger054ea172012-01-20 09:07:21 +0000248 const char *type;
Simon Glass0628ab82013-02-24 17:33:15 +0000249 const void *buf1, *buf2, *base;
Simon Glass46809762020-06-02 19:26:46 -0600250 ulong word1, word2; /* 64-bit if SUPPORT_64BIT_DATA */
wdenk38635852002-08-27 05:55:31 +0000251
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200252 if (argc != 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000253 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000254
255 /* Check for size specification.
256 */
wdenk27b207f2003-07-24 23:38:38 +0000257 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
258 return 1;
York Sun4d1fd7f2014-02-26 17:03:19 -0800259 type = size == 8 ? "double word" :
260 size == 4 ? "word" :
261 size == 2 ? "halfword" : "byte";
wdenk38635852002-08-27 05:55:31 +0000262
263 addr1 = simple_strtoul(argv[1], NULL, 16);
264 addr1 += base_address;
265
266 addr2 = simple_strtoul(argv[2], NULL, 16);
267 addr2 += base_address;
268
269 count = simple_strtoul(argv[3], NULL, 16);
270
Simon Glass0628ab82013-02-24 17:33:15 +0000271 bytes = size * count;
272 base = buf1 = map_sysmem(addr1, bytes);
273 buf2 = map_sysmem(addr2, bytes);
Mike Frysingerfeb12a12012-01-20 09:07:22 +0000274 for (ngood = 0; ngood < count; ++ngood) {
wdenk38635852002-08-27 05:55:31 +0000275 if (size == 4) {
York Sun76698b42014-02-12 15:55:35 -0800276 word1 = *(u32 *)buf1;
277 word2 = *(u32 *)buf2;
Simon Glass46809762020-06-02 19:26:46 -0600278 } else if (SUPPORT_64BIT_DATA && size == 8) {
279 word1 = *(ulong *)buf1;
280 word2 = *(ulong *)buf2;
Mike Frysinger054ea172012-01-20 09:07:21 +0000281 } else if (size == 2) {
York Sun76698b42014-02-12 15:55:35 -0800282 word1 = *(u16 *)buf1;
283 word2 = *(u16 *)buf2;
Mike Frysinger054ea172012-01-20 09:07:21 +0000284 } else {
York Sun76698b42014-02-12 15:55:35 -0800285 word1 = *(u8 *)buf1;
286 word2 = *(u8 *)buf2;
wdenk38635852002-08-27 05:55:31 +0000287 }
Mike Frysinger054ea172012-01-20 09:07:21 +0000288 if (word1 != word2) {
Simon Glass0628ab82013-02-24 17:33:15 +0000289 ulong offset = buf1 - base;
Mike Frysinger054ea172012-01-20 09:07:21 +0000290 printf("%s at 0x%08lx (%#0*lx) != %s at 0x%08lx (%#0*lx)\n",
Simon Glass0628ab82013-02-24 17:33:15 +0000291 type, (ulong)(addr1 + offset), size, word1,
292 type, (ulong)(addr2 + offset), size, word2);
Mike Frysinger054ea172012-01-20 09:07:21 +0000293 rcode = 1;
294 break;
wdenk38635852002-08-27 05:55:31 +0000295 }
Mike Frysinger054ea172012-01-20 09:07:21 +0000296
Simon Glass0628ab82013-02-24 17:33:15 +0000297 buf1 += size;
298 buf2 += size;
Stefan Roeseeaadb442010-09-13 11:10:34 +0200299
300 /* reset watchdog from time to time */
Mike Frysingerfeb12a12012-01-20 09:07:22 +0000301 if ((ngood % (64 << 10)) == 0)
Stefan Roeseeaadb442010-09-13 11:10:34 +0200302 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000303 }
Simon Glass0628ab82013-02-24 17:33:15 +0000304 unmap_sysmem(buf1);
305 unmap_sysmem(buf2);
wdenk38635852002-08-27 05:55:31 +0000306
Mike Frysinger054ea172012-01-20 09:07:21 +0000307 printf("Total of %ld %s(s) were the same\n", ngood, type);
wdenk38635852002-08-27 05:55:31 +0000308 return rcode;
309}
310
Simon Glass09140112020-05-10 11:40:03 -0600311static int do_mem_cp(struct cmd_tbl *cmdtp, int flag, int argc,
312 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000313{
Fabio Estevamc2538422016-12-15 16:00:13 -0200314 ulong addr, dest, count;
Philippe Reynes787f10a2019-12-02 17:33:22 +0100315 void *src, *dst;
wdenk27b207f2003-07-24 23:38:38 +0000316 int size;
wdenk38635852002-08-27 05:55:31 +0000317
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200318 if (argc != 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000319 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000320
321 /* Check for size specification.
322 */
wdenk27b207f2003-07-24 23:38:38 +0000323 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
324 return 1;
wdenk38635852002-08-27 05:55:31 +0000325
326 addr = simple_strtoul(argv[1], NULL, 16);
327 addr += base_address;
328
329 dest = simple_strtoul(argv[2], NULL, 16);
330 dest += base_address;
331
332 count = simple_strtoul(argv[3], NULL, 16);
333
334 if (count == 0) {
335 puts ("Zero length ???\n");
336 return 1;
337 }
338
Philippe Reynes787f10a2019-12-02 17:33:22 +0100339 src = map_sysmem(addr, count * size);
340 dst = map_sysmem(dest, count * size);
341
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900342#ifdef CONFIG_MTD_NOR_FLASH
wdenk38635852002-08-27 05:55:31 +0000343 /* check if we are copying to Flash */
Philippe Reynes787f10a2019-12-02 17:33:22 +0100344 if (addr2info((ulong)dst)) {
wdenk38635852002-08-27 05:55:31 +0000345 int rc;
346
wdenk4b9206e2004-03-23 22:14:11 +0000347 puts ("Copy to Flash... ");
wdenk38635852002-08-27 05:55:31 +0000348
Philippe Reynes787f10a2019-12-02 17:33:22 +0100349 rc = flash_write((char *)src, (ulong)dst, count * size);
wdenk38635852002-08-27 05:55:31 +0000350 if (rc != 0) {
Simon Glass0ee482522019-12-28 10:44:40 -0700351 flash_perror(rc);
Philippe Reynes787f10a2019-12-02 17:33:22 +0100352 unmap_sysmem(src);
353 unmap_sysmem(dst);
wdenk38635852002-08-27 05:55:31 +0000354 return (1);
355 }
356 puts ("done\n");
Philippe Reynes787f10a2019-12-02 17:33:22 +0100357 unmap_sysmem(src);
358 unmap_sysmem(dst);
wdenk38635852002-08-27 05:55:31 +0000359 return 0;
360 }
361#endif
362
Philippe Reynes787f10a2019-12-02 17:33:22 +0100363 memcpy(dst, src, count * size);
Masahiro Yamadaa3a47492014-10-23 17:46:24 +0900364
Philippe Reynes787f10a2019-12-02 17:33:22 +0100365 unmap_sysmem(src);
366 unmap_sysmem(dst);
wdenk38635852002-08-27 05:55:31 +0000367 return 0;
368}
369
Simon Glassbdded202020-06-02 19:26:49 -0600370#ifdef CONFIG_MEM_SEARCH
371static int do_mem_search(struct cmd_tbl *cmdtp, int flag, int argc,
372 char *const argv[])
373{
374 ulong addr, length, bytes, offset;
375 u8 *ptr, *end, *buf;
376 bool quiet = false;
377 ulong last_pos; /* Offset of last match in 'size' units*/
378 ulong last_addr; /* Address of last displayed line */
379 int limit = 10;
380 int count;
381 int size;
382 int i;
383
384 /* We use the last specified parameters, unless new ones are entered */
385 addr = dp_last_addr;
386 size = dp_last_size;
387 length = dp_last_length;
388
389 if (argc < 3)
390 return CMD_RET_USAGE;
391
392 if ((!flag & CMD_FLAG_REPEAT)) {
393 /*
394 * Check for a size specification.
395 * Defaults to long if no or incorrect specification.
396 */
397 size = cmd_get_data_size(argv[0], 4);
398 if (size < 0 && size != -2 /* string */)
399 return 1;
400
401 argc--; argv++;
402 while (argc && *argv[0] == '-') {
403 int ch = argv[0][1];
404
405 if (ch == 'q')
406 quiet = true;
407 else if (ch == 'l' && isxdigit(argv[0][2]))
408 limit = simple_strtoul(argv[0] + 2, NULL, 16);
409 else
410 return CMD_RET_USAGE;
411 argc--; argv++;
412 }
413
414 /* Address is specified since argc > 1 */
415 addr = simple_strtoul(argv[0], NULL, 16);
416 addr += base_address;
417
418 /* Length is the number of objects, not number of bytes */
419 length = simple_strtoul(argv[1], NULL, 16);
420
421 /* Read the bytes to search for */
422 end = search_buf + sizeof(search_buf);
423 for (i = 2, ptr = search_buf; i < argc && ptr < end; i++) {
424 if (SUPPORT_64BIT_DATA && size == 8) {
425 u64 val = simple_strtoull(argv[i], NULL, 16);
426
427 *(u64 *)ptr = val;
428 } else if (size == -2) { /* string */
429 int len = min(strlen(argv[i]),
430 (size_t)(end - ptr));
431
432 memcpy(ptr, argv[i], len);
433 ptr += len;
434 continue;
435 } else {
436 u32 val = simple_strtoul(argv[i], NULL, 16);
437
438 switch (size) {
439 case 1:
440 *ptr = val;
441 break;
442 case 2:
443 *(u16 *)ptr = val;
444 break;
445 case 4:
446 *(u32 *)ptr = val;
447 break;
448 }
449 }
450 ptr += size;
451 }
452 search_len = ptr - search_buf;
453 }
454
455 /* Do the search */
456 if (size == -2)
457 size = 1;
458 bytes = size * length;
459 buf = map_sysmem(addr, bytes);
460 last_pos = 0;
461 last_addr = 0;
462 count = 0;
463 for (offset = 0; offset <= bytes - search_len && count < limit;
464 offset += size) {
465 void *ptr = buf + offset;
466
467 if (!memcmp(ptr, search_buf, search_len)) {
468 uint align = (addr + offset) & 0xf;
469 ulong match = addr + offset;
470
471 if (!count || (last_addr & ~0xf) != (match & ~0xf)) {
472 if (!quiet) {
473 if (count)
474 printf("--\n");
475 print_buffer(match - align, ptr - align,
476 size,
477 ALIGN(search_len + align,
478 16) / size, 0);
479 }
480 last_addr = match;
481 last_pos = offset / size;
482 }
483 count++;
484 }
485 }
486 if (!quiet) {
487 printf("%d match%s", count, count == 1 ? "" : "es");
488 if (count == limit)
489 printf(" (repeat command to check for more)");
490 printf("\n");
491 }
492 env_set_hex("memmatches", count);
493 env_set_hex("memaddr", last_addr);
494 env_set_hex("mempos", last_pos);
495
496 unmap_sysmem(buf);
497
498 dp_last_addr = addr + offset / size;
499 dp_last_size = size;
500 dp_last_length = length - offset / size;
501
502 return count ? 0 : CMD_RET_FAILURE;
503}
504#endif
505
Simon Glass09140112020-05-10 11:40:03 -0600506static int do_mem_base(struct cmd_tbl *cmdtp, int flag, int argc,
507 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000508{
509 if (argc > 1) {
510 /* Set new base address.
511 */
512 base_address = simple_strtoul(argv[1], NULL, 16);
513 }
514 /* Print the current base address.
515 */
516 printf("Base Address: 0x%08lx\n", base_address);
517 return 0;
518}
519
Simon Glass09140112020-05-10 11:40:03 -0600520static int do_mem_loop(struct cmd_tbl *cmdtp, int flag, int argc,
521 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000522{
Simon Glass0628ab82013-02-24 17:33:15 +0000523 ulong addr, length, i, bytes;
wdenk27b207f2003-07-24 23:38:38 +0000524 int size;
Simon Glass46809762020-06-02 19:26:46 -0600525 volatile ulong *llp; /* 64-bit if SUPPORT_64BIT_DATA */
York Sun76698b42014-02-12 15:55:35 -0800526 volatile u32 *longp;
527 volatile u16 *shortp;
528 volatile u8 *cp;
Simon Glass0628ab82013-02-24 17:33:15 +0000529 const void *buf;
wdenk38635852002-08-27 05:55:31 +0000530
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200531 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000532 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000533
Robert P. J. Day85de63e2013-02-03 02:29:54 +0000534 /*
535 * Check for a size specification.
wdenk38635852002-08-27 05:55:31 +0000536 * Defaults to long if no or incorrect specification.
537 */
wdenk27b207f2003-07-24 23:38:38 +0000538 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
539 return 1;
wdenk38635852002-08-27 05:55:31 +0000540
541 /* Address is always specified.
542 */
543 addr = simple_strtoul(argv[1], NULL, 16);
544
545 /* Length is the number of objects, not number of bytes.
546 */
547 length = simple_strtoul(argv[2], NULL, 16);
548
Simon Glass0628ab82013-02-24 17:33:15 +0000549 bytes = size * length;
550 buf = map_sysmem(addr, bytes);
551
wdenk38635852002-08-27 05:55:31 +0000552 /* We want to optimize the loops to run as fast as possible.
553 * If we have only one object, just run infinite loops.
554 */
555 if (length == 1) {
Simon Glass46809762020-06-02 19:26:46 -0600556 if (SUPPORT_64BIT_DATA && size == 8) {
557 llp = (ulong *)buf;
York Sun4d1fd7f2014-02-26 17:03:19 -0800558 for (;;)
559 i = *llp;
560 }
wdenk38635852002-08-27 05:55:31 +0000561 if (size == 4) {
York Sun76698b42014-02-12 15:55:35 -0800562 longp = (u32 *)buf;
wdenk38635852002-08-27 05:55:31 +0000563 for (;;)
564 i = *longp;
565 }
566 if (size == 2) {
York Sun76698b42014-02-12 15:55:35 -0800567 shortp = (u16 *)buf;
wdenk38635852002-08-27 05:55:31 +0000568 for (;;)
569 i = *shortp;
570 }
York Sun76698b42014-02-12 15:55:35 -0800571 cp = (u8 *)buf;
wdenk38635852002-08-27 05:55:31 +0000572 for (;;)
573 i = *cp;
574 }
575
Simon Glass46809762020-06-02 19:26:46 -0600576 if (SUPPORT_64BIT_DATA && size == 8) {
York Sun4d1fd7f2014-02-26 17:03:19 -0800577 for (;;) {
Simon Glass46809762020-06-02 19:26:46 -0600578 llp = (ulong *)buf;
York Sun4d1fd7f2014-02-26 17:03:19 -0800579 i = length;
580 while (i-- > 0)
581 *llp++;
582 }
583 }
wdenk38635852002-08-27 05:55:31 +0000584 if (size == 4) {
585 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800586 longp = (u32 *)buf;
wdenk38635852002-08-27 05:55:31 +0000587 i = length;
588 while (i-- > 0)
Marek Vasutf3b3c3d2011-09-26 02:26:06 +0200589 *longp++;
wdenk38635852002-08-27 05:55:31 +0000590 }
591 }
592 if (size == 2) {
593 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800594 shortp = (u16 *)buf;
wdenk38635852002-08-27 05:55:31 +0000595 i = length;
596 while (i-- > 0)
Marek Vasutf3b3c3d2011-09-26 02:26:06 +0200597 *shortp++;
wdenk38635852002-08-27 05:55:31 +0000598 }
599 }
600 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800601 cp = (u8 *)buf;
wdenk38635852002-08-27 05:55:31 +0000602 i = length;
603 while (i-- > 0)
Marek Vasutf3b3c3d2011-09-26 02:26:06 +0200604 *cp++;
wdenk38635852002-08-27 05:55:31 +0000605 }
Simon Glass0628ab82013-02-24 17:33:15 +0000606 unmap_sysmem(buf);
Simon Glass92765f422013-06-11 11:14:35 -0700607
608 return 0;
wdenk38635852002-08-27 05:55:31 +0000609}
610
wdenk56523f12004-07-11 17:40:54 +0000611#ifdef CONFIG_LOOPW
Simon Glass09140112020-05-10 11:40:03 -0600612static int do_mem_loopw(struct cmd_tbl *cmdtp, int flag, int argc,
613 char *const argv[])
wdenk56523f12004-07-11 17:40:54 +0000614{
York Sun4d1fd7f2014-02-26 17:03:19 -0800615 ulong addr, length, i, bytes;
wdenk56523f12004-07-11 17:40:54 +0000616 int size;
Simon Glass46809762020-06-02 19:26:46 -0600617 volatile ulong *llp; /* 64-bit if SUPPORT_64BIT_DATA */
618 ulong data; /* 64-bit if SUPPORT_64BIT_DATA */
York Sun76698b42014-02-12 15:55:35 -0800619 volatile u32 *longp;
620 volatile u16 *shortp;
621 volatile u8 *cp;
Simon Glass0628ab82013-02-24 17:33:15 +0000622 void *buf;
wdenk81050922004-07-11 20:04:51 +0000623
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200624 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000625 return CMD_RET_USAGE;
wdenk56523f12004-07-11 17:40:54 +0000626
Robert P. J. Day85de63e2013-02-03 02:29:54 +0000627 /*
628 * Check for a size specification.
wdenk56523f12004-07-11 17:40:54 +0000629 * Defaults to long if no or incorrect specification.
630 */
631 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
632 return 1;
633
634 /* Address is always specified.
635 */
636 addr = simple_strtoul(argv[1], NULL, 16);
637
638 /* Length is the number of objects, not number of bytes.
639 */
640 length = simple_strtoul(argv[2], NULL, 16);
641
642 /* data to write */
Simon Glass46809762020-06-02 19:26:46 -0600643 if (SUPPORT_64BIT_DATA)
644 data = simple_strtoull(argv[3], NULL, 16);
645 else
646 data = simple_strtoul(argv[3], NULL, 16);
wdenk81050922004-07-11 20:04:51 +0000647
Simon Glass0628ab82013-02-24 17:33:15 +0000648 bytes = size * length;
649 buf = map_sysmem(addr, bytes);
650
wdenk56523f12004-07-11 17:40:54 +0000651 /* We want to optimize the loops to run as fast as possible.
652 * If we have only one object, just run infinite loops.
653 */
654 if (length == 1) {
Simon Glass46809762020-06-02 19:26:46 -0600655 if (SUPPORT_64BIT_DATA && size == 8) {
656 llp = (ulong *)buf;
York Sun4d1fd7f2014-02-26 17:03:19 -0800657 for (;;)
658 *llp = data;
659 }
wdenk56523f12004-07-11 17:40:54 +0000660 if (size == 4) {
York Sun76698b42014-02-12 15:55:35 -0800661 longp = (u32 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000662 for (;;)
663 *longp = data;
York Sun4d1fd7f2014-02-26 17:03:19 -0800664 }
wdenk56523f12004-07-11 17:40:54 +0000665 if (size == 2) {
York Sun76698b42014-02-12 15:55:35 -0800666 shortp = (u16 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000667 for (;;)
668 *shortp = data;
669 }
York Sun76698b42014-02-12 15:55:35 -0800670 cp = (u8 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000671 for (;;)
672 *cp = data;
673 }
674
Simon Glass46809762020-06-02 19:26:46 -0600675 if (SUPPORT_64BIT_DATA && size == 8) {
York Sun4d1fd7f2014-02-26 17:03:19 -0800676 for (;;) {
Simon Glass46809762020-06-02 19:26:46 -0600677 llp = (ulong *)buf;
York Sun4d1fd7f2014-02-26 17:03:19 -0800678 i = length;
679 while (i-- > 0)
680 *llp++ = data;
681 }
682 }
wdenk56523f12004-07-11 17:40:54 +0000683 if (size == 4) {
684 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800685 longp = (u32 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000686 i = length;
687 while (i-- > 0)
688 *longp++ = data;
689 }
690 }
691 if (size == 2) {
692 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800693 shortp = (u16 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000694 i = length;
695 while (i-- > 0)
696 *shortp++ = data;
697 }
698 }
699 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800700 cp = (u8 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000701 i = length;
702 while (i-- > 0)
703 *cp++ = data;
704 }
705}
706#endif /* CONFIG_LOOPW */
707
Tom Rini68149e92013-03-12 10:07:19 -0400708#ifdef CONFIG_CMD_MEMTEST
Simon Glass5512d5b2013-02-28 17:47:14 +0000709static ulong mem_test_alt(vu_long *buf, ulong start_addr, ulong end_addr,
710 vu_long *dummy)
wdenk38635852002-08-27 05:55:31 +0000711{
Simon Glassc9638f52013-02-24 17:33:16 +0000712 vu_long *addr;
Simon Glassc9638f52013-02-24 17:33:16 +0000713 ulong errs = 0;
714 ulong val, readback;
715 int j;
Simon Glassc9638f52013-02-24 17:33:16 +0000716 vu_long offset;
717 vu_long test_offset;
718 vu_long pattern;
719 vu_long temp;
720 vu_long anti_pattern;
721 vu_long num_words;
wdenk38635852002-08-27 05:55:31 +0000722 static const ulong bitpattern[] = {
723 0x00000001, /* single bit */
724 0x00000003, /* two adjacent bits */
725 0x00000007, /* three adjacent bits */
726 0x0000000F, /* four adjacent bits */
727 0x00000005, /* two non-adjacent bits */
728 0x00000015, /* three non-adjacent bits */
729 0x00000055, /* four non-adjacent bits */
730 0xaaaaaaaa, /* alternating 1/0 */
731 };
wdenk38635852002-08-27 05:55:31 +0000732
Simon Glass5512d5b2013-02-28 17:47:14 +0000733 num_words = (end_addr - start_addr) / sizeof(vu_long);
Simon Glass8c86bbe2013-02-24 17:33:20 +0000734
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000735 /*
736 * Data line test: write a pattern to the first
737 * location, write the 1's complement to a 'parking'
738 * address (changes the state of the data bus so a
739 * floating bus doesn't give a false OK), and then
740 * read the value back. Note that we read it back
741 * into a variable because the next time we read it,
742 * it might be right (been there, tough to explain to
743 * the quality guys why it prints a failure when the
744 * "is" and "should be" are obviously the same in the
745 * error message).
746 *
747 * Rather than exhaustively testing, we test some
748 * patterns by shifting '1' bits through a field of
749 * '0's and '0' bits through a field of '1's (i.e.
750 * pattern and ~pattern).
751 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000752 addr = buf;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000753 for (j = 0; j < sizeof(bitpattern) / sizeof(bitpattern[0]); j++) {
754 val = bitpattern[j];
755 for (; val != 0; val <<= 1) {
Simon Glass5512d5b2013-02-28 17:47:14 +0000756 *addr = val;
Simon Glassc9638f52013-02-24 17:33:16 +0000757 *dummy = ~val; /* clear the test data off the bus */
wdenk38635852002-08-27 05:55:31 +0000758 readback = *addr;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000759 if (readback != val) {
Simon Glassc9638f52013-02-24 17:33:16 +0000760 printf("FAILURE (data line): "
761 "expected %08lx, actual %08lx\n",
762 val, readback);
763 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000764 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000765 return -1;
wdenk38635852002-08-27 05:55:31 +0000766 }
767 *addr = ~val;
768 *dummy = val;
769 readback = *addr;
Simon Glassc9638f52013-02-24 17:33:16 +0000770 if (readback != ~val) {
771 printf("FAILURE (data line): "
772 "Is %08lx, should be %08lx\n",
773 readback, ~val);
774 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000775 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000776 return -1;
wdenk38635852002-08-27 05:55:31 +0000777 }
wdenk38635852002-08-27 05:55:31 +0000778 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000779 }
wdenk38635852002-08-27 05:55:31 +0000780
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000781 /*
782 * Based on code whose Original Author and Copyright
783 * information follows: Copyright (c) 1998 by Michael
784 * Barr. This software is placed into the public
785 * domain and may be used for any purpose. However,
786 * this notice must not be changed or removed and no
787 * warranty is either expressed or implied by its
788 * publication or distribution.
789 */
wdenk38635852002-08-27 05:55:31 +0000790
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000791 /*
792 * Address line test
wdenk38635852002-08-27 05:55:31 +0000793
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000794 * Description: Test the address bus wiring in a
795 * memory region by performing a walking
796 * 1's test on the relevant bits of the
797 * address and checking for aliasing.
798 * This test will find single-bit
799 * address failures such as stuck-high,
800 * stuck-low, and shorted pins. The base
801 * address and size of the region are
802 * selected by the caller.
wdenk38635852002-08-27 05:55:31 +0000803
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000804 * Notes: For best results, the selected base
805 * address should have enough LSB 0's to
806 * guarantee single address bit changes.
807 * For example, to test a 64-Kbyte
808 * region, select a base address on a
809 * 64-Kbyte boundary. Also, select the
810 * region size as a power-of-two if at
811 * all possible.
812 *
813 * Returns: 0 if the test succeeds, 1 if the test fails.
814 */
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000815 pattern = (vu_long) 0xaaaaaaaa;
816 anti_pattern = (vu_long) 0x55555555;
wdenk38635852002-08-27 05:55:31 +0000817
Simon Glass5512d5b2013-02-28 17:47:14 +0000818 debug("%s:%d: length = 0x%.8lx\n", __func__, __LINE__, num_words);
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000819 /*
820 * Write the default pattern at each of the
821 * power-of-two offsets.
822 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000823 for (offset = 1; offset < num_words; offset <<= 1)
824 addr[offset] = pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000825
826 /*
827 * Check for address bits stuck high.
828 */
829 test_offset = 0;
Simon Glass5512d5b2013-02-28 17:47:14 +0000830 addr[test_offset] = anti_pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000831
Simon Glass5512d5b2013-02-28 17:47:14 +0000832 for (offset = 1; offset < num_words; offset <<= 1) {
833 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000834 if (temp != pattern) {
Simon Glassc9638f52013-02-24 17:33:16 +0000835 printf("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000836 " expected 0x%.8lx, actual 0x%.8lx\n",
David Feng102c0512014-02-12 16:10:08 +0800837 start_addr + offset*sizeof(vu_long),
838 pattern, temp);
Paul Gortmaker87b22b72009-10-02 18:18:33 -0400839 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000840 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000841 return -1;
wdenk38635852002-08-27 05:55:31 +0000842 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000843 }
Simon Glass5512d5b2013-02-28 17:47:14 +0000844 addr[test_offset] = pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000845 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000846
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000847 /*
848 * Check for addr bits stuck low or shorted.
849 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000850 for (test_offset = 1; test_offset < num_words; test_offset <<= 1) {
851 addr[test_offset] = anti_pattern;
wdenk38635852002-08-27 05:55:31 +0000852
Simon Glass5512d5b2013-02-28 17:47:14 +0000853 for (offset = 1; offset < num_words; offset <<= 1) {
854 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000855 if ((temp != pattern) && (offset != test_offset)) {
856 printf("\nFAILURE: Address bit stuck low or"
857 " shorted @ 0x%.8lx: expected 0x%.8lx,"
858 " actual 0x%.8lx\n",
David Feng102c0512014-02-12 16:10:08 +0800859 start_addr + offset*sizeof(vu_long),
860 pattern, temp);
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000861 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000862 if (ctrlc())
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000863 return -1;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000864 }
wdenk38635852002-08-27 05:55:31 +0000865 }
Simon Glass5512d5b2013-02-28 17:47:14 +0000866 addr[test_offset] = pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000867 }
wdenk38635852002-08-27 05:55:31 +0000868
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000869 /*
870 * Description: Test the integrity of a physical
871 * memory device by performing an
872 * increment/decrement test over the
873 * entire region. In the process every
874 * storage bit in the device is tested
875 * as a zero and a one. The base address
876 * and the size of the region are
877 * selected by the caller.
878 *
879 * Returns: 0 if the test succeeds, 1 if the test fails.
880 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000881 num_words++;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000882
883 /*
884 * Fill memory with a known pattern.
885 */
886 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
887 WATCHDOG_RESET();
Simon Glass5512d5b2013-02-28 17:47:14 +0000888 addr[offset] = pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000889 }
890
891 /*
892 * Check each location and invert it for the second pass.
893 */
894 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
895 WATCHDOG_RESET();
Simon Glass5512d5b2013-02-28 17:47:14 +0000896 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000897 if (temp != pattern) {
Simon Glassc9638f52013-02-24 17:33:16 +0000898 printf("\nFAILURE (read/write) @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000899 " expected 0x%.8lx, actual 0x%.8lx)\n",
David Feng102c0512014-02-12 16:10:08 +0800900 start_addr + offset*sizeof(vu_long),
901 pattern, temp);
Paul Gortmaker87b22b72009-10-02 18:18:33 -0400902 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000903 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000904 return -1;
wdenk38635852002-08-27 05:55:31 +0000905 }
906
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000907 anti_pattern = ~pattern;
Simon Glass5512d5b2013-02-28 17:47:14 +0000908 addr[offset] = anti_pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000909 }
910
911 /*
912 * Check each location for the inverted pattern and zero it.
913 */
914 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
915 WATCHDOG_RESET();
916 anti_pattern = ~pattern;
Simon Glass5512d5b2013-02-28 17:47:14 +0000917 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000918 if (temp != anti_pattern) {
Simon Glassc9638f52013-02-24 17:33:16 +0000919 printf("\nFAILURE (read/write): @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000920 " expected 0x%.8lx, actual 0x%.8lx)\n",
David Feng102c0512014-02-12 16:10:08 +0800921 start_addr + offset*sizeof(vu_long),
922 anti_pattern, temp);
Paul Gortmaker87b22b72009-10-02 18:18:33 -0400923 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000924 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000925 return -1;
wdenk38635852002-08-27 05:55:31 +0000926 }
Simon Glass5512d5b2013-02-28 17:47:14 +0000927 addr[offset] = 0;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000928 }
Simon Glass51209b12013-02-24 17:33:17 +0000929
Rasmus Villemoesfea67302016-01-07 11:36:04 +0100930 return errs;
Simon Glassc9638f52013-02-24 17:33:16 +0000931}
wdenk38635852002-08-27 05:55:31 +0000932
Stefan Roese8e434cb2020-03-05 07:21:32 +0100933static int compare_regions(volatile unsigned long *bufa,
934 volatile unsigned long *bufb, size_t count)
935{
936 volatile unsigned long *p1 = bufa;
937 volatile unsigned long *p2 = bufb;
938 int errs = 0;
939 size_t i;
940
941 for (i = 0; i < count; i++, p1++, p2++) {
942 if (*p1 != *p2) {
943 printf("FAILURE: 0x%08lx != 0x%08lx (delta=0x%08lx -> bit %ld) at offset 0x%08lx\n",
944 (unsigned long)*p1, (unsigned long)*p2,
945 *p1 ^ *p2, __ffs(*p1 ^ *p2),
946 (unsigned long)(i * sizeof(unsigned long)));
947 errs++;
948 }
949 }
950
951 return errs;
952}
953
954static ulong test_bitflip_comparison(volatile unsigned long *bufa,
955 volatile unsigned long *bufb, size_t count)
956{
957 volatile unsigned long *p1 = bufa;
958 volatile unsigned long *p2 = bufb;
959 unsigned int j, k;
960 unsigned long q;
961 size_t i;
962 int max;
963 int errs = 0;
964
965 max = sizeof(unsigned long) * 8;
966 for (k = 0; k < max; k++) {
967 q = 0x00000001L << k;
968 for (j = 0; j < 8; j++) {
969 WATCHDOG_RESET();
970 q = ~q;
971 p1 = (volatile unsigned long *)bufa;
972 p2 = (volatile unsigned long *)bufb;
973 for (i = 0; i < count; i++)
974 *p1++ = *p2++ = (i % 2) == 0 ? q : ~q;
975
976 errs += compare_regions(bufa, bufb, count);
977 }
978
979 if (ctrlc())
980 return -1UL;
981 }
982
983 return errs;
984}
985
Simon Glass5512d5b2013-02-28 17:47:14 +0000986static ulong mem_test_quick(vu_long *buf, ulong start_addr, ulong end_addr,
987 vu_long pattern, int iteration)
Simon Glassc9638f52013-02-24 17:33:16 +0000988{
Simon Glass5512d5b2013-02-28 17:47:14 +0000989 vu_long *end;
Simon Glassc9638f52013-02-24 17:33:16 +0000990 vu_long *addr;
Simon Glassc9638f52013-02-24 17:33:16 +0000991 ulong errs = 0;
Simon Glass5512d5b2013-02-28 17:47:14 +0000992 ulong incr, length;
Simon Glassc9638f52013-02-24 17:33:16 +0000993 ulong val, readback;
994
Simon Glass51209b12013-02-24 17:33:17 +0000995 /* Alternate the pattern */
wdenk38635852002-08-27 05:55:31 +0000996 incr = 1;
Simon Glass51209b12013-02-24 17:33:17 +0000997 if (iteration & 1) {
998 incr = -incr;
999 /*
1000 * Flip the pattern each time to make lots of zeros and
1001 * then, the next time, lots of ones. We decrement
1002 * the "negative" patterns and increment the "positive"
1003 * patterns to preserve this feature.
1004 */
1005 if (pattern & 0x80000000)
1006 pattern = -pattern; /* complement & increment */
1007 else
1008 pattern = ~pattern;
1009 }
Simon Glass5512d5b2013-02-28 17:47:14 +00001010 length = (end_addr - start_addr) / sizeof(ulong);
1011 end = buf + length;
Simon Glass7ecbd4d2013-02-24 17:33:18 +00001012 printf("\rPattern %08lX Writing..."
1013 "%12s"
1014 "\b\b\b\b\b\b\b\b\b\b",
1015 pattern, "");
wdenk38635852002-08-27 05:55:31 +00001016
Simon Glass5512d5b2013-02-28 17:47:14 +00001017 for (addr = buf, val = pattern; addr < end; addr++) {
Simon Glass7ecbd4d2013-02-24 17:33:18 +00001018 WATCHDOG_RESET();
1019 *addr = val;
1020 val += incr;
1021 }
wdenk38635852002-08-27 05:55:31 +00001022
Simon Glass7ecbd4d2013-02-24 17:33:18 +00001023 puts("Reading...");
wdenk38635852002-08-27 05:55:31 +00001024
Simon Glass5512d5b2013-02-28 17:47:14 +00001025 for (addr = buf, val = pattern; addr < end; addr++) {
Simon Glass7ecbd4d2013-02-24 17:33:18 +00001026 WATCHDOG_RESET();
1027 readback = *addr;
1028 if (readback != val) {
Simon Glass5512d5b2013-02-28 17:47:14 +00001029 ulong offset = addr - buf;
1030
Simon Glass7ecbd4d2013-02-24 17:33:18 +00001031 printf("\nMem error @ 0x%08X: "
1032 "found %08lX, expected %08lX\n",
David Feng102c0512014-02-12 16:10:08 +08001033 (uint)(uintptr_t)(start_addr + offset*sizeof(vu_long)),
Simon Glass5512d5b2013-02-28 17:47:14 +00001034 readback, val);
Simon Glass7ecbd4d2013-02-24 17:33:18 +00001035 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +00001036 if (ctrlc())
Simon Glass7ecbd4d2013-02-24 17:33:18 +00001037 return -1;
wdenk38635852002-08-27 05:55:31 +00001038 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +00001039 val += incr;
1040 }
wdenk38635852002-08-27 05:55:31 +00001041
Rasmus Villemoesfea67302016-01-07 11:36:04 +01001042 return errs;
Simon Glassc9638f52013-02-24 17:33:16 +00001043}
1044
1045/*
1046 * Perform a memory test. A more complete alternative test can be
1047 * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
1048 * interrupted by ctrl-c or by a failure of one of the sub-tests.
1049 */
Simon Glass09140112020-05-10 11:40:03 -06001050static int do_mem_mtest(struct cmd_tbl *cmdtp, int flag, int argc,
1051 char *const argv[])
Simon Glassc9638f52013-02-24 17:33:16 +00001052{
Simon Glass8c86bbe2013-02-24 17:33:20 +00001053 ulong start, end;
Michal Simeke519f032020-05-04 13:54:40 +02001054 vu_long scratch_space;
1055 vu_long *buf, *dummy = &scratch_space;
Tom Riniadcc5702015-04-07 09:38:54 -04001056 ulong iteration_limit = 0;
Stefan Roesea8c708e2020-03-05 07:21:29 +01001057 ulong count = 0;
Simon Glass51209b12013-02-24 17:33:17 +00001058 ulong errs = 0; /* number of errors, or -1 if interrupted */
Pavel Macheke37f1eb2015-04-01 13:50:41 +02001059 ulong pattern = 0;
Simon Glass51209b12013-02-24 17:33:17 +00001060 int iteration;
Simon Glassc9638f52013-02-24 17:33:16 +00001061
Pavel Macheke37f1eb2015-04-01 13:50:41 +02001062 start = CONFIG_SYS_MEMTEST_START;
1063 end = CONFIG_SYS_MEMTEST_END;
1064
Simon Glassc9638f52013-02-24 17:33:16 +00001065 if (argc > 1)
Pavel Macheke37f1eb2015-04-01 13:50:41 +02001066 if (strict_strtoul(argv[1], 16, &start) < 0)
1067 return CMD_RET_USAGE;
Simon Glassc9638f52013-02-24 17:33:16 +00001068
1069 if (argc > 2)
Pavel Macheke37f1eb2015-04-01 13:50:41 +02001070 if (strict_strtoul(argv[2], 16, &end) < 0)
1071 return CMD_RET_USAGE;
Simon Glassc9638f52013-02-24 17:33:16 +00001072
1073 if (argc > 3)
Pavel Macheke37f1eb2015-04-01 13:50:41 +02001074 if (strict_strtoul(argv[3], 16, &pattern) < 0)
1075 return CMD_RET_USAGE;
Simon Glassc9638f52013-02-24 17:33:16 +00001076
1077 if (argc > 4)
Pavel Macheke37f1eb2015-04-01 13:50:41 +02001078 if (strict_strtoul(argv[4], 16, &iteration_limit) < 0)
1079 return CMD_RET_USAGE;
1080
1081 if (end < start) {
1082 printf("Refusing to do empty test\n");
1083 return -1;
1084 }
Simon Glassc9638f52013-02-24 17:33:16 +00001085
Michal Simekdfe461d2016-02-24 08:36:02 +01001086 printf("Testing %08lx ... %08lx:\n", start, end);
Simon Glass8c86bbe2013-02-24 17:33:20 +00001087 debug("%s:%d: start %#08lx end %#08lx\n", __func__, __LINE__,
1088 start, end);
Simon Glass51209b12013-02-24 17:33:17 +00001089
Simon Glass5512d5b2013-02-28 17:47:14 +00001090 buf = map_sysmem(start, end - start);
Simon Glass51209b12013-02-24 17:33:17 +00001091 for (iteration = 0;
1092 !iteration_limit || iteration < iteration_limit;
1093 iteration++) {
1094 if (ctrlc()) {
Simon Glass51209b12013-02-24 17:33:17 +00001095 errs = -1UL;
1096 break;
1097 }
1098
1099 printf("Iteration: %6d\r", iteration + 1);
1100 debug("\n");
Stefan Roesef14bfa72020-03-05 07:21:31 +01001101 if (IS_ENABLED(CONFIG_SYS_ALT_MEMTEST)) {
Simon Glass5512d5b2013-02-28 17:47:14 +00001102 errs = mem_test_alt(buf, start, end, dummy);
Stefan Roese8e434cb2020-03-05 07:21:32 +01001103 if (errs == -1UL)
1104 break;
1105 count += errs;
1106 errs = test_bitflip_comparison(buf,
1107 buf + (end - start) / 2,
1108 (end - start) /
1109 sizeof(unsigned long));
Simon Glass5512d5b2013-02-28 17:47:14 +00001110 } else {
1111 errs = mem_test_quick(buf, start, end, pattern,
1112 iteration);
1113 }
1114 if (errs == -1UL)
1115 break;
Stefan Roesea8c708e2020-03-05 07:21:29 +01001116 count += errs;
Simon Glass5512d5b2013-02-28 17:47:14 +00001117 }
1118
Stefan Roese54de2442020-03-05 07:21:30 +01001119 unmap_sysmem((void *)buf);
Simon Glass51209b12013-02-24 17:33:17 +00001120
1121 if (errs == -1UL) {
Simon Glassc44d4382013-02-24 17:33:19 +00001122 /* Memory test was aborted - write a newline to finish off */
1123 putc('\n');
Simon Glass51209b12013-02-24 17:33:17 +00001124 }
Stefan Roesea8c708e2020-03-05 07:21:29 +01001125 printf("Tested %d iteration(s) with %lu errors.\n", iteration, count);
Simon Glassc9638f52013-02-24 17:33:16 +00001126
Stefan Roesea8c708e2020-03-05 07:21:29 +01001127 return errs != 0;
wdenk38635852002-08-27 05:55:31 +00001128}
Wolfgang Denka2681702013-03-08 10:51:32 +00001129#endif /* CONFIG_CMD_MEMTEST */
wdenk38635852002-08-27 05:55:31 +00001130
1131/* Modify memory.
1132 *
1133 * Syntax:
York Sun4d1fd7f2014-02-26 17:03:19 -08001134 * mm{.b, .w, .l, .q} {addr}
wdenk38635852002-08-27 05:55:31 +00001135 */
1136static int
Simon Glass09140112020-05-10 11:40:03 -06001137mod_mem(struct cmd_tbl *cmdtp, int incrflag, int flag, int argc,
1138 char *const argv[])
wdenk38635852002-08-27 05:55:31 +00001139{
York Sun4d1fd7f2014-02-26 17:03:19 -08001140 ulong addr;
Simon Glass46809762020-06-02 19:26:46 -06001141 ulong i; /* 64-bit if SUPPORT_64BIT_DATA */
wdenk27b207f2003-07-24 23:38:38 +00001142 int nbytes, size;
Simon Glass0628ab82013-02-24 17:33:15 +00001143 void *ptr = NULL;
wdenk38635852002-08-27 05:55:31 +00001144
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001145 if (argc != 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001146 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +00001147
Simon Glassb26440f2014-04-10 20:01:31 -06001148 bootretry_reset_cmd_timeout(); /* got a good command to get here */
wdenk38635852002-08-27 05:55:31 +00001149 /* We use the last specified parameters, unless new ones are
1150 * entered.
1151 */
1152 addr = mm_last_addr;
1153 size = mm_last_size;
1154
1155 if ((flag & CMD_FLAG_REPEAT) == 0) {
1156 /* New command specified. Check for a size specification.
1157 * Defaults to long if no or incorrect specification.
1158 */
wdenk27b207f2003-07-24 23:38:38 +00001159 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
1160 return 1;
wdenk38635852002-08-27 05:55:31 +00001161
1162 /* Address is specified since argc > 1
1163 */
1164 addr = simple_strtoul(argv[1], NULL, 16);
1165 addr += base_address;
1166 }
1167
1168 /* Print the address, followed by value. Then accept input for
1169 * the next value. A non-converted value exits.
1170 */
1171 do {
Simon Glass0628ab82013-02-24 17:33:15 +00001172 ptr = map_sysmem(addr, size);
wdenk38635852002-08-27 05:55:31 +00001173 printf("%08lx:", addr);
1174 if (size == 4)
York Sun76698b42014-02-12 15:55:35 -08001175 printf(" %08x", *((u32 *)ptr));
Simon Glass46809762020-06-02 19:26:46 -06001176 else if (SUPPORT_64BIT_DATA && size == 8)
1177 printf(" %0lx", *((ulong *)ptr));
wdenk38635852002-08-27 05:55:31 +00001178 else if (size == 2)
York Sun76698b42014-02-12 15:55:35 -08001179 printf(" %04x", *((u16 *)ptr));
wdenk38635852002-08-27 05:55:31 +00001180 else
York Sun76698b42014-02-12 15:55:35 -08001181 printf(" %02x", *((u8 *)ptr));
wdenk38635852002-08-27 05:55:31 +00001182
Simon Glasse1bf8242014-04-10 20:01:27 -06001183 nbytes = cli_readline(" ? ");
wdenk38635852002-08-27 05:55:31 +00001184 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
1185 /* <CR> pressed as only input, don't modify current
1186 * location and move to next. "-" pressed will go back.
1187 */
1188 if (incrflag)
1189 addr += nbytes ? -size : size;
1190 nbytes = 1;
Simon Glassb26440f2014-04-10 20:01:31 -06001191 /* good enough to not time out */
1192 bootretry_reset_cmd_timeout();
wdenk38635852002-08-27 05:55:31 +00001193 }
1194#ifdef CONFIG_BOOT_RETRY_TIME
1195 else if (nbytes == -2) {
1196 break; /* timed out, exit the command */
1197 }
1198#endif
1199 else {
1200 char *endp;
Simon Glass46809762020-06-02 19:26:46 -06001201 if (SUPPORT_64BIT_DATA)
1202 i = simple_strtoull(console_buffer, &endp, 16);
1203 else
1204 i = simple_strtoul(console_buffer, &endp, 16);
wdenk38635852002-08-27 05:55:31 +00001205 nbytes = endp - console_buffer;
1206 if (nbytes) {
wdenk38635852002-08-27 05:55:31 +00001207 /* good enough to not time out
1208 */
Simon Glassb26440f2014-04-10 20:01:31 -06001209 bootretry_reset_cmd_timeout();
wdenk38635852002-08-27 05:55:31 +00001210 if (size == 4)
York Sun76698b42014-02-12 15:55:35 -08001211 *((u32 *)ptr) = i;
Simon Glass46809762020-06-02 19:26:46 -06001212 else if (SUPPORT_64BIT_DATA && size == 8)
1213 *((ulong *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001214 else if (size == 2)
York Sun76698b42014-02-12 15:55:35 -08001215 *((u16 *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001216 else
York Sun76698b42014-02-12 15:55:35 -08001217 *((u8 *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001218 if (incrflag)
1219 addr += size;
1220 }
1221 }
1222 } while (nbytes);
Simon Glass0628ab82013-02-24 17:33:15 +00001223 if (ptr)
1224 unmap_sysmem(ptr);
wdenk38635852002-08-27 05:55:31 +00001225
1226 mm_last_addr = addr;
1227 mm_last_size = size;
1228 return 0;
1229}
1230
Mike Frysinger710b9932010-12-21 14:19:51 -05001231#ifdef CONFIG_CMD_CRC32
1232
Simon Glass09140112020-05-10 11:40:03 -06001233static int do_mem_crc(struct cmd_tbl *cmdtp, int flag, int argc,
1234 char *const argv[])
wdenk38635852002-08-27 05:55:31 +00001235{
Simon Glassd20a40d2013-02-24 20:30:22 +00001236 int flags = 0;
1237 int ac;
1238 char * const *av;
wdenk38635852002-08-27 05:55:31 +00001239
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001240 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001241 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +00001242
wdenkc26e4542004-04-18 10:13:26 +00001243 av = argv + 1;
1244 ac = argc - 1;
Daniel Thompson221a9492017-05-19 17:26:58 +01001245#ifdef CONFIG_CRC32_VERIFY
wdenkc26e4542004-04-18 10:13:26 +00001246 if (strcmp(*av, "-v") == 0) {
Joe Hershbergera69bdba2015-05-05 12:23:53 -05001247 flags |= HASH_FLAG_VERIFY | HASH_FLAG_ENV;
wdenkc26e4542004-04-18 10:13:26 +00001248 av++;
1249 ac--;
wdenkc26e4542004-04-18 10:13:26 +00001250 }
Simon Glassd20a40d2013-02-24 20:30:22 +00001251#endif
wdenkc26e4542004-04-18 10:13:26 +00001252
Simon Glassd20a40d2013-02-24 20:30:22 +00001253 return hash_command("crc32", flags, cmdtp, flag, ac, av);
wdenkc26e4542004-04-18 10:13:26 +00001254}
wdenkc26e4542004-04-18 10:13:26 +00001255
Mike Frysinger710b9932010-12-21 14:19:51 -05001256#endif
1257
Jean-Jacques Hiblot803e1a32019-07-02 14:23:26 +02001258#ifdef CONFIG_CMD_RANDOM
Simon Glass09140112020-05-10 11:40:03 -06001259static int do_random(struct cmd_tbl *cmdtp, int flag, int argc,
1260 char *const argv[])
Jean-Jacques Hiblot803e1a32019-07-02 14:23:26 +02001261{
1262 unsigned long addr, len;
1263 unsigned long seed; // NOT INITIALIZED ON PURPOSE
1264 unsigned int *buf, *start;
1265 unsigned char *buf8;
1266 unsigned int i;
1267
Eugeniy Paltsev5f2c4e02020-03-20 19:38:17 +03001268 if (argc < 3 || argc > 4)
1269 return CMD_RET_USAGE;
Jean-Jacques Hiblot803e1a32019-07-02 14:23:26 +02001270
1271 len = simple_strtoul(argv[2], NULL, 16);
1272 addr = simple_strtoul(argv[1], NULL, 16);
1273
1274 if (argc == 4) {
1275 seed = simple_strtoul(argv[3], NULL, 16);
1276 if (seed == 0) {
1277 printf("The seed cannot be 0. Using 0xDEADBEEF.\n");
1278 seed = 0xDEADBEEF;
1279 }
1280 } else {
1281 seed = get_timer(0) ^ rand();
1282 }
1283
1284 srand(seed);
1285 start = map_sysmem(addr, len);
1286 buf = start;
1287 for (i = 0; i < (len / 4); i++)
1288 *buf++ = rand();
1289
1290 buf8 = (unsigned char *)buf;
1291 for (i = 0; i < (len % 4); i++)
1292 *buf8++ = rand() & 0xFF;
1293
1294 unmap_sysmem(start);
1295 printf("%lu bytes filled with random data\n", len);
Eugeniy Paltsev5f2c4e02020-03-20 19:38:17 +03001296
1297 return CMD_RET_SUCCESS;
Jean-Jacques Hiblot803e1a32019-07-02 14:23:26 +02001298}
1299#endif
1300
wdenk8bde7f72003-06-27 21:31:46 +00001301/**************************************************/
wdenk0d498392003-07-01 21:06:45 +00001302U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001303 md, 3, 1, do_mem_md,
Peter Tyser2fb26042009-01-27 18:03:12 -06001304 "memory display",
Simon Glass76be8f72020-06-02 19:26:45 -06001305 "[.b, .w, .l" HELP_Q "] address [# of objects]"
wdenk8bde7f72003-06-27 21:31:46 +00001306);
1307
1308
wdenk0d498392003-07-01 21:06:45 +00001309U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001310 mm, 2, 1, do_mem_mm,
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001311 "memory modify (auto-incrementing address)",
Simon Glass76be8f72020-06-02 19:26:45 -06001312 "[.b, .w, .l" HELP_Q "] address"
wdenk8bde7f72003-06-27 21:31:46 +00001313);
1314
1315
wdenk0d498392003-07-01 21:06:45 +00001316U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001317 nm, 2, 1, do_mem_nm,
Peter Tyser2fb26042009-01-27 18:03:12 -06001318 "memory modify (constant address)",
Simon Glass76be8f72020-06-02 19:26:45 -06001319 "[.b, .w, .l" HELP_Q "] address"
wdenk8bde7f72003-06-27 21:31:46 +00001320);
1321
wdenk0d498392003-07-01 21:06:45 +00001322U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001323 mw, 4, 1, do_mem_mw,
Peter Tyser2fb26042009-01-27 18:03:12 -06001324 "memory write (fill)",
Simon Glass76be8f72020-06-02 19:26:45 -06001325 "[.b, .w, .l" HELP_Q "] address value [count]"
wdenk8bde7f72003-06-27 21:31:46 +00001326);
1327
wdenk0d498392003-07-01 21:06:45 +00001328U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001329 cp, 4, 1, do_mem_cp,
Peter Tyser2fb26042009-01-27 18:03:12 -06001330 "memory copy",
Simon Glass76be8f72020-06-02 19:26:45 -06001331 "[.b, .w, .l" HELP_Q "] source target count"
wdenk8bde7f72003-06-27 21:31:46 +00001332);
1333
wdenk0d498392003-07-01 21:06:45 +00001334U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001335 cmp, 4, 1, do_mem_cmp,
Peter Tyser2fb26042009-01-27 18:03:12 -06001336 "memory compare",
Simon Glass76be8f72020-06-02 19:26:45 -06001337 "[.b, .w, .l" HELP_Q "] addr1 addr2 count"
wdenk8bde7f72003-06-27 21:31:46 +00001338);
1339
Simon Glassbdded202020-06-02 19:26:49 -06001340#ifdef CONFIG_MEM_SEARCH
1341/**************************************************/
1342U_BOOT_CMD(
1343 ms, 255, 1, do_mem_search,
1344 "memory search",
1345 "[.b, .w, .l" HELP_Q ", .s] [-q | -<n>] address #-of-objects <value>..."
1346 " -q = quiet, -l<val> = match limit" :
1347);
1348#endif
1349
Mike Frysinger710b9932010-12-21 14:19:51 -05001350#ifdef CONFIG_CMD_CRC32
1351
Daniel Thompson221a9492017-05-19 17:26:58 +01001352#ifndef CONFIG_CRC32_VERIFY
wdenkc26e4542004-04-18 10:13:26 +00001353
wdenk0d498392003-07-01 21:06:45 +00001354U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001355 crc32, 4, 1, do_mem_crc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001356 "checksum calculation",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001357 "address count [addr]\n - compute CRC32 checksum [save at addr]"
wdenk8bde7f72003-06-27 21:31:46 +00001358);
1359
Daniel Thompson221a9492017-05-19 17:26:58 +01001360#else /* CONFIG_CRC32_VERIFY */
wdenkc26e4542004-04-18 10:13:26 +00001361
1362U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001363 crc32, 5, 1, do_mem_crc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001364 "checksum calculation",
wdenkc26e4542004-04-18 10:13:26 +00001365 "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001366 "-v address count crc\n - verify crc of memory area"
wdenkc26e4542004-04-18 10:13:26 +00001367);
1368
Daniel Thompson221a9492017-05-19 17:26:58 +01001369#endif /* CONFIG_CRC32_VERIFY */
wdenkc26e4542004-04-18 10:13:26 +00001370
Mike Frysinger710b9932010-12-21 14:19:51 -05001371#endif
1372
Simon Glass15a33e42012-11-30 13:01:20 +00001373#ifdef CONFIG_CMD_MEMINFO
Simon Glass09140112020-05-10 11:40:03 -06001374static int do_mem_info(struct cmd_tbl *cmdtp, int flag, int argc,
1375 char *const argv[])
Simon Glass15a33e42012-11-30 13:01:20 +00001376{
Simon Glass428a6a12019-11-14 12:57:44 -07001377 puts("DRAM: ");
1378 print_size(gd->ram_size, "\n");
Simon Glass15a33e42012-11-30 13:01:20 +00001379
1380 return 0;
1381}
1382#endif
1383
wdenk0d498392003-07-01 21:06:45 +00001384U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001385 base, 2, 1, do_mem_base,
Peter Tyser2fb26042009-01-27 18:03:12 -06001386 "print or set address offset",
wdenk8bde7f72003-06-27 21:31:46 +00001387 "\n - print address offset for memory commands\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001388 "base off\n - set address offset for memory commands to 'off'"
wdenk8bde7f72003-06-27 21:31:46 +00001389);
1390
wdenk0d498392003-07-01 21:06:45 +00001391U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001392 loop, 3, 1, do_mem_loop,
Peter Tyser2fb26042009-01-27 18:03:12 -06001393 "infinite loop on address range",
Simon Glass76be8f72020-06-02 19:26:45 -06001394 "[.b, .w, .l" HELP_Q "] address number_of_objects"
wdenk8bde7f72003-06-27 21:31:46 +00001395);
1396
wdenk56523f12004-07-11 17:40:54 +00001397#ifdef CONFIG_LOOPW
1398U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001399 loopw, 4, 1, do_mem_loopw,
Peter Tyser2fb26042009-01-27 18:03:12 -06001400 "infinite write loop on address range",
Simon Glass76be8f72020-06-02 19:26:45 -06001401 "[.b, .w, .l" HELP_Q "] address number_of_objects data_to_write"
wdenk56523f12004-07-11 17:40:54 +00001402);
1403#endif /* CONFIG_LOOPW */
1404
Wolfgang Denka2681702013-03-08 10:51:32 +00001405#ifdef CONFIG_CMD_MEMTEST
wdenk0d498392003-07-01 21:06:45 +00001406U_BOOT_CMD(
Dirk Eibachb6fc6fd2008-12-16 14:51:56 +01001407 mtest, 5, 1, do_mem_mtest,
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001408 "simple RAM read/write test",
1409 "[start [end [pattern [iterations]]]]"
wdenk8bde7f72003-06-27 21:31:46 +00001410);
Wolfgang Denka2681702013-03-08 10:51:32 +00001411#endif /* CONFIG_CMD_MEMTEST */
wdenk8bde7f72003-06-27 21:31:46 +00001412
Joel Johnson72732312020-01-29 09:17:18 -07001413#ifdef CONFIG_CMD_MX_CYCLIC
stroese4aaf29b2004-12-16 17:42:39 +00001414U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001415 mdc, 4, 1, do_mem_mdc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001416 "memory display cyclic",
Simon Glass76be8f72020-06-02 19:26:45 -06001417 "[.b, .w, .l" HELP_Q "] address count delay(ms)"
stroese4aaf29b2004-12-16 17:42:39 +00001418);
1419
1420U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001421 mwc, 4, 1, do_mem_mwc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001422 "memory write cyclic",
Simon Glass76be8f72020-06-02 19:26:45 -06001423 "[.b, .w, .l" HELP_Q "] address value delay(ms)"
stroese4aaf29b2004-12-16 17:42:39 +00001424);
Joel Johnson72732312020-01-29 09:17:18 -07001425#endif /* CONFIG_CMD_MX_CYCLIC */
Simon Glass15a33e42012-11-30 13:01:20 +00001426
1427#ifdef CONFIG_CMD_MEMINFO
1428U_BOOT_CMD(
1429 meminfo, 3, 1, do_mem_info,
1430 "display memory information",
1431 ""
1432);
1433#endif
Jean-Jacques Hiblot803e1a32019-07-02 14:23:26 +02001434
1435#ifdef CONFIG_CMD_RANDOM
1436U_BOOT_CMD(
1437 random, 4, 0, do_random,
1438 "fill memory with random pattern",
1439 "<addr> <len> [<seed>]\n"
1440 " - Fill 'len' bytes of memory starting at 'addr' with random data\n"
1441);
1442#endif