blob: 66c2d36a14800b3cfcc2c8201d7245dcb9806aeb [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 Glass4e4bf942022-07-31 12:28:48 -060019#include <display_options.h>
Tom Rini17ead042022-07-23 13:05:03 -040020#ifdef CONFIG_MTD_NOR_FLASH
Simon Glass0ee482522019-12-28 10:44:40 -070021#include <flash.h>
Tom Rini17ead042022-07-23 13:05:03 -040022#endif
Simon Glassd20a40d2013-02-24 20:30:22 +000023#include <hash.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060024#include <log.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050025#include <mapmem.h>
Simon Glass90526e92020-05-10 11:39:56 -060026#include <rand.h>
Sergei Poselenova6e6fc62008-04-09 16:09:41 +020027#include <watchdog.h>
Simon Glass401d1c42020-10-30 21:38:53 -060028#include <asm/global_data.h>
Simon Glass0628ab82013-02-24 17:33:15 +000029#include <asm/io.h>
Simon Glasscd93d622020-05-10 11:40:13 -060030#include <linux/bitops.h>
Simon Glass15a33e42012-11-30 13:01:20 +000031#include <linux/compiler.h>
Simon Glassbdded202020-06-02 19:26:49 -060032#include <linux/ctype.h>
Simon Glassc05ed002020-05-10 11:40:11 -060033#include <linux/delay.h>
Simon Glass15a33e42012-11-30 13:01:20 +000034
35DECLARE_GLOBAL_DATA_PTR;
wdenk38635852002-08-27 05:55:31 +000036
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 Glass550a9e72020-07-28 19:41:14 -060056#ifdef CONFIG_CMD_MEM_SEARCH
57static ulong dp_last_ms_length;
Simon Glassbdded202020-06-02 19:26:49 -060058static u8 search_buf[64];
59static uint search_len;
60#endif
wdenk38635852002-08-27 05:55:31 +000061
62/* Memory Display
63 *
64 * Syntax:
York Sun4d1fd7f2014-02-26 17:03:19 -080065 * md{.b, .w, .l, .q} {addr} {len}
wdenk38635852002-08-27 05:55:31 +000066 */
67#define DISP_LINE_LEN 16
Simon Glass09140112020-05-10 11:40:03 -060068static int do_mem_md(struct cmd_tbl *cmdtp, int flag, int argc,
69 char *const argv[])
wdenk38635852002-08-27 05:55:31 +000070{
Tuomas Tynkkynenc68c03f2017-10-10 21:59:42 +030071 ulong addr, length, bytes;
72 const void *buf;
wdenk27b207f2003-07-24 23:38:38 +000073 int size;
wdenk38635852002-08-27 05:55:31 +000074 int rc = 0;
75
76 /* We use the last specified parameters, unless new ones are
77 * entered.
78 */
79 addr = dp_last_addr;
80 size = dp_last_size;
81 length = dp_last_length;
82
Wolfgang Denk47e26b12010-07-17 01:06:04 +020083 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +000084 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +000085
86 if ((flag & CMD_FLAG_REPEAT) == 0) {
87 /* New command specified. Check for a size specification.
88 * Defaults to long if no or incorrect specification.
89 */
wdenk27b207f2003-07-24 23:38:38 +000090 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
91 return 1;
wdenk38635852002-08-27 05:55:31 +000092
93 /* Address is specified since argc > 1
94 */
Simon Glass7e5f4602021-07-24 09:03:29 -060095 addr = hextoul(argv[1], NULL);
wdenk38635852002-08-27 05:55:31 +000096 addr += base_address;
97
98 /* If another parameter, it is the length to display.
99 * Length is the number of objects, not number of bytes.
100 */
101 if (argc > 2)
Simon Glass7e5f4602021-07-24 09:03:29 -0600102 length = hextoul(argv[2], NULL);
wdenk38635852002-08-27 05:55:31 +0000103 }
104
Tuomas Tynkkynenc68c03f2017-10-10 21:59:42 +0300105 bytes = size * length;
106 buf = map_sysmem(addr, bytes);
wdenk2abbe072003-06-16 23:50:08 +0000107
Tuomas Tynkkynenc68c03f2017-10-10 21:59:42 +0300108 /* Print the lines. */
109 print_buffer(addr, buf, size, length, DISP_LINE_LEN / size);
110 addr += bytes;
111 unmap_sysmem(buf);
wdenk38635852002-08-27 05:55:31 +0000112
113 dp_last_addr = addr;
114 dp_last_length = length;
115 dp_last_size = size;
116 return (rc);
117}
118
Simon Glass09140112020-05-10 11:40:03 -0600119static int do_mem_mm(struct cmd_tbl *cmdtp, int flag, int argc,
120 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000121{
122 return mod_mem (cmdtp, 1, flag, argc, argv);
123}
Simon Glass09140112020-05-10 11:40:03 -0600124
125static int do_mem_nm(struct cmd_tbl *cmdtp, int flag, int argc,
126 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000127{
128 return mod_mem (cmdtp, 0, flag, argc, argv);
129}
130
Simon Glass09140112020-05-10 11:40:03 -0600131static int do_mem_mw(struct cmd_tbl *cmdtp, int flag, int argc,
132 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000133{
Simon Glass46809762020-06-02 19:26:46 -0600134 ulong writeval; /* 64-bit if SUPPORT_64BIT_DATA */
York Sun4d1fd7f2014-02-26 17:03:19 -0800135 ulong addr, count;
wdenk27b207f2003-07-24 23:38:38 +0000136 int size;
Simon Glasscc5e1962015-03-05 12:25:18 -0700137 void *buf, *start;
Simon Glass0628ab82013-02-24 17:33:15 +0000138 ulong bytes;
wdenk38635852002-08-27 05:55:31 +0000139
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200140 if ((argc < 3) || (argc > 4))
Simon Glass4c12eeb2011-12-10 08:44:01 +0000141 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000142
143 /* Check for size specification.
144 */
wdenk27b207f2003-07-24 23:38:38 +0000145 if ((size = cmd_get_data_size(argv[0], 4)) < 1)
146 return 1;
wdenk38635852002-08-27 05:55:31 +0000147
148 /* Address is specified since argc > 1
149 */
Simon Glass7e5f4602021-07-24 09:03:29 -0600150 addr = hextoul(argv[1], NULL);
wdenk38635852002-08-27 05:55:31 +0000151 addr += base_address;
152
153 /* Get the value to write.
154 */
Simon Glass46809762020-06-02 19:26:46 -0600155 if (SUPPORT_64BIT_DATA)
156 writeval = simple_strtoull(argv[2], NULL, 16);
157 else
Simon Glass7e5f4602021-07-24 09:03:29 -0600158 writeval = hextoul(argv[2], NULL);
wdenk38635852002-08-27 05:55:31 +0000159
160 /* Count ? */
161 if (argc == 4) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600162 count = hextoul(argv[3], NULL);
wdenk38635852002-08-27 05:55:31 +0000163 } else {
164 count = 1;
165 }
166
Simon Glass0628ab82013-02-24 17:33:15 +0000167 bytes = size * count;
Simon Glasscc5e1962015-03-05 12:25:18 -0700168 start = map_sysmem(addr, bytes);
169 buf = start;
wdenk38635852002-08-27 05:55:31 +0000170 while (count-- > 0) {
171 if (size == 4)
York Sun76698b42014-02-12 15:55:35 -0800172 *((u32 *)buf) = (u32)writeval;
Simon Glass46809762020-06-02 19:26:46 -0600173 else if (SUPPORT_64BIT_DATA && size == 8)
174 *((ulong *)buf) = writeval;
wdenk38635852002-08-27 05:55:31 +0000175 else if (size == 2)
York Sun76698b42014-02-12 15:55:35 -0800176 *((u16 *)buf) = (u16)writeval;
wdenk38635852002-08-27 05:55:31 +0000177 else
York Sun76698b42014-02-12 15:55:35 -0800178 *((u8 *)buf) = (u8)writeval;
Simon Glass0628ab82013-02-24 17:33:15 +0000179 buf += size;
wdenk38635852002-08-27 05:55:31 +0000180 }
Simon Glasscc5e1962015-03-05 12:25:18 -0700181 unmap_sysmem(start);
wdenk38635852002-08-27 05:55:31 +0000182 return 0;
183}
184
Joel Johnson72732312020-01-29 09:17:18 -0700185#ifdef CONFIG_CMD_MX_CYCLIC
Simon Glass09140112020-05-10 11:40:03 -0600186static int do_mem_mdc(struct cmd_tbl *cmdtp, int flag, int argc,
187 char *const argv[])
stroese4aaf29b2004-12-16 17:42:39 +0000188{
189 int i;
190 ulong count;
191
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200192 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000193 return CMD_RET_USAGE;
stroese4aaf29b2004-12-16 17:42:39 +0000194
Simon Glass0b1284e2021-07-24 09:03:30 -0600195 count = dectoul(argv[3], NULL);
stroese4aaf29b2004-12-16 17:42:39 +0000196
197 for (;;) {
198 do_mem_md (NULL, 0, 3, argv);
199
200 /* delay for <count> ms... */
201 for (i=0; i<count; i++)
Simon Glass07e11142020-05-10 11:40:10 -0600202 udelay(1000);
stroese4aaf29b2004-12-16 17:42:39 +0000203
204 /* check for ctrl-c to abort... */
205 if (ctrlc()) {
206 puts("Abort\n");
207 return 0;
208 }
209 }
210
211 return 0;
212}
213
Simon Glass09140112020-05-10 11:40:03 -0600214static int do_mem_mwc(struct cmd_tbl *cmdtp, int flag, int argc,
215 char *const argv[])
stroese4aaf29b2004-12-16 17:42:39 +0000216{
217 int i;
218 ulong count;
219
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200220 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000221 return CMD_RET_USAGE;
stroese4aaf29b2004-12-16 17:42:39 +0000222
Simon Glass0b1284e2021-07-24 09:03:30 -0600223 count = dectoul(argv[3], NULL);
stroese4aaf29b2004-12-16 17:42:39 +0000224
225 for (;;) {
226 do_mem_mw (NULL, 0, 3, argv);
227
228 /* delay for <count> ms... */
229 for (i=0; i<count; i++)
Simon Glass07e11142020-05-10 11:40:10 -0600230 udelay(1000);
stroese4aaf29b2004-12-16 17:42:39 +0000231
232 /* check for ctrl-c to abort... */
233 if (ctrlc()) {
234 puts("Abort\n");
235 return 0;
236 }
237 }
238
239 return 0;
240}
Joel Johnson72732312020-01-29 09:17:18 -0700241#endif /* CONFIG_CMD_MX_CYCLIC */
stroese4aaf29b2004-12-16 17:42:39 +0000242
Simon Glass09140112020-05-10 11:40:03 -0600243static int do_mem_cmp(struct cmd_tbl *cmdtp, int flag, int argc,
244 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000245{
Simon Glass0628ab82013-02-24 17:33:15 +0000246 ulong addr1, addr2, count, ngood, bytes;
wdenk27b207f2003-07-24 23:38:38 +0000247 int size;
wdenk38635852002-08-27 05:55:31 +0000248 int rcode = 0;
Mike Frysinger054ea172012-01-20 09:07:21 +0000249 const char *type;
Simon Glass0628ab82013-02-24 17:33:15 +0000250 const void *buf1, *buf2, *base;
Simon Glass46809762020-06-02 19:26:46 -0600251 ulong word1, word2; /* 64-bit if SUPPORT_64BIT_DATA */
wdenk38635852002-08-27 05:55:31 +0000252
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200253 if (argc != 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000254 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000255
256 /* Check for size specification.
257 */
wdenk27b207f2003-07-24 23:38:38 +0000258 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
259 return 1;
York Sun4d1fd7f2014-02-26 17:03:19 -0800260 type = size == 8 ? "double word" :
261 size == 4 ? "word" :
262 size == 2 ? "halfword" : "byte";
wdenk38635852002-08-27 05:55:31 +0000263
Simon Glass7e5f4602021-07-24 09:03:29 -0600264 addr1 = hextoul(argv[1], NULL);
wdenk38635852002-08-27 05:55:31 +0000265 addr1 += base_address;
266
Simon Glass7e5f4602021-07-24 09:03:29 -0600267 addr2 = hextoul(argv[2], NULL);
wdenk38635852002-08-27 05:55:31 +0000268 addr2 += base_address;
269
Simon Glass7e5f4602021-07-24 09:03:29 -0600270 count = hextoul(argv[3], NULL);
wdenk38635852002-08-27 05:55:31 +0000271
Simon Glass0628ab82013-02-24 17:33:15 +0000272 bytes = size * count;
273 base = buf1 = map_sysmem(addr1, bytes);
274 buf2 = map_sysmem(addr2, bytes);
Mike Frysingerfeb12a12012-01-20 09:07:22 +0000275 for (ngood = 0; ngood < count; ++ngood) {
wdenk38635852002-08-27 05:55:31 +0000276 if (size == 4) {
York Sun76698b42014-02-12 15:55:35 -0800277 word1 = *(u32 *)buf1;
278 word2 = *(u32 *)buf2;
Simon Glass46809762020-06-02 19:26:46 -0600279 } else if (SUPPORT_64BIT_DATA && size == 8) {
280 word1 = *(ulong *)buf1;
281 word2 = *(ulong *)buf2;
Mike Frysinger054ea172012-01-20 09:07:21 +0000282 } else if (size == 2) {
York Sun76698b42014-02-12 15:55:35 -0800283 word1 = *(u16 *)buf1;
284 word2 = *(u16 *)buf2;
Mike Frysinger054ea172012-01-20 09:07:21 +0000285 } else {
York Sun76698b42014-02-12 15:55:35 -0800286 word1 = *(u8 *)buf1;
287 word2 = *(u8 *)buf2;
wdenk38635852002-08-27 05:55:31 +0000288 }
Mike Frysinger054ea172012-01-20 09:07:21 +0000289 if (word1 != word2) {
Simon Glass0628ab82013-02-24 17:33:15 +0000290 ulong offset = buf1 - base;
Mike Frysinger054ea172012-01-20 09:07:21 +0000291 printf("%s at 0x%08lx (%#0*lx) != %s at 0x%08lx (%#0*lx)\n",
Simon Glass0628ab82013-02-24 17:33:15 +0000292 type, (ulong)(addr1 + offset), size, word1,
293 type, (ulong)(addr2 + offset), size, word2);
Mike Frysinger054ea172012-01-20 09:07:21 +0000294 rcode = 1;
295 break;
wdenk38635852002-08-27 05:55:31 +0000296 }
Mike Frysinger054ea172012-01-20 09:07:21 +0000297
Simon Glass0628ab82013-02-24 17:33:15 +0000298 buf1 += size;
299 buf2 += size;
Stefan Roeseeaadb442010-09-13 11:10:34 +0200300
301 /* reset watchdog from time to time */
Mike Frysingerfeb12a12012-01-20 09:07:22 +0000302 if ((ngood % (64 << 10)) == 0)
Stefan Roese29caf932022-09-02 14:10:46 +0200303 schedule();
wdenk38635852002-08-27 05:55:31 +0000304 }
Simon Glass0628ab82013-02-24 17:33:15 +0000305 unmap_sysmem(buf1);
306 unmap_sysmem(buf2);
wdenk38635852002-08-27 05:55:31 +0000307
Mike Frysinger054ea172012-01-20 09:07:21 +0000308 printf("Total of %ld %s(s) were the same\n", ngood, type);
wdenk38635852002-08-27 05:55:31 +0000309 return rcode;
310}
311
Simon Glass09140112020-05-10 11:40:03 -0600312static int do_mem_cp(struct cmd_tbl *cmdtp, int flag, int argc,
313 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000314{
Fabio Estevamc2538422016-12-15 16:00:13 -0200315 ulong addr, dest, count;
Philippe Reynes787f10a2019-12-02 17:33:22 +0100316 void *src, *dst;
wdenk27b207f2003-07-24 23:38:38 +0000317 int size;
wdenk38635852002-08-27 05:55:31 +0000318
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200319 if (argc != 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000320 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000321
322 /* Check for size specification.
323 */
wdenk27b207f2003-07-24 23:38:38 +0000324 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
325 return 1;
wdenk38635852002-08-27 05:55:31 +0000326
Simon Glass7e5f4602021-07-24 09:03:29 -0600327 addr = hextoul(argv[1], NULL);
wdenk38635852002-08-27 05:55:31 +0000328 addr += base_address;
329
Simon Glass7e5f4602021-07-24 09:03:29 -0600330 dest = hextoul(argv[2], NULL);
wdenk38635852002-08-27 05:55:31 +0000331 dest += base_address;
332
Simon Glass7e5f4602021-07-24 09:03:29 -0600333 count = hextoul(argv[3], NULL);
wdenk38635852002-08-27 05:55:31 +0000334
335 if (count == 0) {
336 puts ("Zero length ???\n");
337 return 1;
338 }
339
Philippe Reynes787f10a2019-12-02 17:33:22 +0100340 src = map_sysmem(addr, count * size);
341 dst = map_sysmem(dest, count * size);
342
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900343#ifdef CONFIG_MTD_NOR_FLASH
wdenk38635852002-08-27 05:55:31 +0000344 /* check if we are copying to Flash */
Philippe Reynes787f10a2019-12-02 17:33:22 +0100345 if (addr2info((ulong)dst)) {
wdenk38635852002-08-27 05:55:31 +0000346 int rc;
347
wdenk4b9206e2004-03-23 22:14:11 +0000348 puts ("Copy to Flash... ");
wdenk38635852002-08-27 05:55:31 +0000349
Philippe Reynes787f10a2019-12-02 17:33:22 +0100350 rc = flash_write((char *)src, (ulong)dst, count * size);
wdenk38635852002-08-27 05:55:31 +0000351 if (rc != 0) {
Simon Glass0ee482522019-12-28 10:44:40 -0700352 flash_perror(rc);
Philippe Reynes787f10a2019-12-02 17:33:22 +0100353 unmap_sysmem(src);
354 unmap_sysmem(dst);
wdenk38635852002-08-27 05:55:31 +0000355 return (1);
356 }
357 puts ("done\n");
Philippe Reynes787f10a2019-12-02 17:33:22 +0100358 unmap_sysmem(src);
359 unmap_sysmem(dst);
wdenk38635852002-08-27 05:55:31 +0000360 return 0;
361 }
362#endif
363
Philippe Reynes787f10a2019-12-02 17:33:22 +0100364 memcpy(dst, src, count * size);
Masahiro Yamadaa3a47492014-10-23 17:46:24 +0900365
Philippe Reynes787f10a2019-12-02 17:33:22 +0100366 unmap_sysmem(src);
367 unmap_sysmem(dst);
wdenk38635852002-08-27 05:55:31 +0000368 return 0;
369}
370
Simon Glass550a9e72020-07-28 19:41:14 -0600371#ifdef CONFIG_CMD_MEM_SEARCH
Simon Glassbdded202020-06-02 19:26:49 -0600372static int do_mem_search(struct cmd_tbl *cmdtp, int flag, int argc,
373 char *const argv[])
374{
375 ulong addr, length, bytes, offset;
376 u8 *ptr, *end, *buf;
377 bool quiet = false;
378 ulong last_pos; /* Offset of last match in 'size' units*/
379 ulong last_addr; /* Address of last displayed line */
380 int limit = 10;
Simon Glass550a9e72020-07-28 19:41:14 -0600381 int used_len;
Simon Glassbdded202020-06-02 19:26:49 -0600382 int count;
383 int size;
384 int i;
385
386 /* We use the last specified parameters, unless new ones are entered */
387 addr = dp_last_addr;
388 size = dp_last_size;
Simon Glass550a9e72020-07-28 19:41:14 -0600389 length = dp_last_ms_length;
Simon Glassbdded202020-06-02 19:26:49 -0600390
391 if (argc < 3)
392 return CMD_RET_USAGE;
393
Simon Glass550a9e72020-07-28 19:41:14 -0600394 if (!(flag & CMD_FLAG_REPEAT)) {
Simon Glassbdded202020-06-02 19:26:49 -0600395 /*
396 * Check for a size specification.
397 * Defaults to long if no or incorrect specification.
398 */
399 size = cmd_get_data_size(argv[0], 4);
Simon Glass7526dee2020-11-01 14:15:36 -0700400 if (size < 0 && size != CMD_DATA_SIZE_STR)
Simon Glassbdded202020-06-02 19:26:49 -0600401 return 1;
402
Simon Glass550a9e72020-07-28 19:41:14 -0600403 argc--;
404 argv++;
Simon Glassbdded202020-06-02 19:26:49 -0600405 while (argc && *argv[0] == '-') {
406 int ch = argv[0][1];
407
408 if (ch == 'q')
409 quiet = true;
410 else if (ch == 'l' && isxdigit(argv[0][2]))
Simon Glass7e5f4602021-07-24 09:03:29 -0600411 limit = hextoul(argv[0] + 2, NULL);
Simon Glassbdded202020-06-02 19:26:49 -0600412 else
413 return CMD_RET_USAGE;
Simon Glass550a9e72020-07-28 19:41:14 -0600414 argc--;
415 argv++;
Simon Glassbdded202020-06-02 19:26:49 -0600416 }
417
418 /* Address is specified since argc > 1 */
Simon Glass7e5f4602021-07-24 09:03:29 -0600419 addr = hextoul(argv[0], NULL);
Simon Glassbdded202020-06-02 19:26:49 -0600420 addr += base_address;
421
422 /* Length is the number of objects, not number of bytes */
Simon Glass7e5f4602021-07-24 09:03:29 -0600423 length = hextoul(argv[1], NULL);
Simon Glassbdded202020-06-02 19:26:49 -0600424
425 /* Read the bytes to search for */
426 end = search_buf + sizeof(search_buf);
427 for (i = 2, ptr = search_buf; i < argc && ptr < end; i++) {
Simon Glass550a9e72020-07-28 19:41:14 -0600428 if (MEM_SUPPORT_64BIT_DATA && size == 8) {
Simon Glassbdded202020-06-02 19:26:49 -0600429 u64 val = simple_strtoull(argv[i], NULL, 16);
430
431 *(u64 *)ptr = val;
432 } else if (size == -2) { /* string */
433 int len = min(strlen(argv[i]),
434 (size_t)(end - ptr));
435
436 memcpy(ptr, argv[i], len);
437 ptr += len;
438 continue;
439 } else {
Simon Glass7e5f4602021-07-24 09:03:29 -0600440 u32 val = hextoul(argv[i], NULL);
Simon Glassbdded202020-06-02 19:26:49 -0600441
442 switch (size) {
443 case 1:
444 *ptr = val;
445 break;
446 case 2:
447 *(u16 *)ptr = val;
448 break;
449 case 4:
450 *(u32 *)ptr = val;
451 break;
452 }
453 }
454 ptr += size;
455 }
456 search_len = ptr - search_buf;
457 }
458
459 /* Do the search */
460 if (size == -2)
461 size = 1;
462 bytes = size * length;
463 buf = map_sysmem(addr, bytes);
464 last_pos = 0;
465 last_addr = 0;
466 count = 0;
Simon Glass550a9e72020-07-28 19:41:14 -0600467 for (offset = 0;
468 offset < bytes && offset <= bytes - search_len && count < limit;
Simon Glassbdded202020-06-02 19:26:49 -0600469 offset += size) {
470 void *ptr = buf + offset;
471
472 if (!memcmp(ptr, search_buf, search_len)) {
473 uint align = (addr + offset) & 0xf;
474 ulong match = addr + offset;
475
476 if (!count || (last_addr & ~0xf) != (match & ~0xf)) {
477 if (!quiet) {
478 if (count)
479 printf("--\n");
480 print_buffer(match - align, ptr - align,
481 size,
482 ALIGN(search_len + align,
483 16) / size, 0);
484 }
485 last_addr = match;
486 last_pos = offset / size;
487 }
488 count++;
489 }
490 }
491 if (!quiet) {
492 printf("%d match%s", count, count == 1 ? "" : "es");
493 if (count == limit)
494 printf(" (repeat command to check for more)");
495 printf("\n");
496 }
497 env_set_hex("memmatches", count);
498 env_set_hex("memaddr", last_addr);
499 env_set_hex("mempos", last_pos);
500
501 unmap_sysmem(buf);
502
Simon Glass550a9e72020-07-28 19:41:14 -0600503 used_len = offset / size;
504 dp_last_addr = addr + used_len;
Simon Glassbdded202020-06-02 19:26:49 -0600505 dp_last_size = size;
Simon Glass550a9e72020-07-28 19:41:14 -0600506 dp_last_ms_length = length < used_len ? 0 : length - used_len;
Simon Glassbdded202020-06-02 19:26:49 -0600507
508 return count ? 0 : CMD_RET_FAILURE;
509}
510#endif
511
Simon Glass09140112020-05-10 11:40:03 -0600512static int do_mem_base(struct cmd_tbl *cmdtp, int flag, int argc,
513 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000514{
515 if (argc > 1) {
516 /* Set new base address.
517 */
Simon Glass7e5f4602021-07-24 09:03:29 -0600518 base_address = hextoul(argv[1], NULL);
wdenk38635852002-08-27 05:55:31 +0000519 }
520 /* Print the current base address.
521 */
522 printf("Base Address: 0x%08lx\n", base_address);
523 return 0;
524}
525
Simon Glass09140112020-05-10 11:40:03 -0600526static int do_mem_loop(struct cmd_tbl *cmdtp, int flag, int argc,
527 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000528{
Simon Glass0628ab82013-02-24 17:33:15 +0000529 ulong addr, length, i, bytes;
wdenk27b207f2003-07-24 23:38:38 +0000530 int size;
Simon Glass46809762020-06-02 19:26:46 -0600531 volatile ulong *llp; /* 64-bit if SUPPORT_64BIT_DATA */
York Sun76698b42014-02-12 15:55:35 -0800532 volatile u32 *longp;
533 volatile u16 *shortp;
534 volatile u8 *cp;
Simon Glass0628ab82013-02-24 17:33:15 +0000535 const void *buf;
wdenk38635852002-08-27 05:55:31 +0000536
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200537 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000538 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000539
Robert P. J. Day85de63e2013-02-03 02:29:54 +0000540 /*
541 * Check for a size specification.
wdenk38635852002-08-27 05:55:31 +0000542 * Defaults to long if no or incorrect specification.
543 */
wdenk27b207f2003-07-24 23:38:38 +0000544 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
545 return 1;
wdenk38635852002-08-27 05:55:31 +0000546
547 /* Address is always specified.
548 */
Simon Glass7e5f4602021-07-24 09:03:29 -0600549 addr = hextoul(argv[1], NULL);
wdenk38635852002-08-27 05:55:31 +0000550
551 /* Length is the number of objects, not number of bytes.
552 */
Simon Glass7e5f4602021-07-24 09:03:29 -0600553 length = hextoul(argv[2], NULL);
wdenk38635852002-08-27 05:55:31 +0000554
Simon Glass0628ab82013-02-24 17:33:15 +0000555 bytes = size * length;
556 buf = map_sysmem(addr, bytes);
557
wdenk38635852002-08-27 05:55:31 +0000558 /* We want to optimize the loops to run as fast as possible.
559 * If we have only one object, just run infinite loops.
560 */
561 if (length == 1) {
Simon Glass46809762020-06-02 19:26:46 -0600562 if (SUPPORT_64BIT_DATA && size == 8) {
563 llp = (ulong *)buf;
York Sun4d1fd7f2014-02-26 17:03:19 -0800564 for (;;)
565 i = *llp;
566 }
wdenk38635852002-08-27 05:55:31 +0000567 if (size == 4) {
York Sun76698b42014-02-12 15:55:35 -0800568 longp = (u32 *)buf;
wdenk38635852002-08-27 05:55:31 +0000569 for (;;)
570 i = *longp;
571 }
572 if (size == 2) {
York Sun76698b42014-02-12 15:55:35 -0800573 shortp = (u16 *)buf;
wdenk38635852002-08-27 05:55:31 +0000574 for (;;)
575 i = *shortp;
576 }
York Sun76698b42014-02-12 15:55:35 -0800577 cp = (u8 *)buf;
wdenk38635852002-08-27 05:55:31 +0000578 for (;;)
579 i = *cp;
580 }
581
Simon Glass46809762020-06-02 19:26:46 -0600582 if (SUPPORT_64BIT_DATA && size == 8) {
York Sun4d1fd7f2014-02-26 17:03:19 -0800583 for (;;) {
Simon Glass46809762020-06-02 19:26:46 -0600584 llp = (ulong *)buf;
York Sun4d1fd7f2014-02-26 17:03:19 -0800585 i = length;
586 while (i-- > 0)
587 *llp++;
588 }
589 }
wdenk38635852002-08-27 05:55:31 +0000590 if (size == 4) {
591 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800592 longp = (u32 *)buf;
wdenk38635852002-08-27 05:55:31 +0000593 i = length;
594 while (i-- > 0)
Marek Vasutf3b3c3d2011-09-26 02:26:06 +0200595 *longp++;
wdenk38635852002-08-27 05:55:31 +0000596 }
597 }
598 if (size == 2) {
599 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800600 shortp = (u16 *)buf;
wdenk38635852002-08-27 05:55:31 +0000601 i = length;
602 while (i-- > 0)
Marek Vasutf3b3c3d2011-09-26 02:26:06 +0200603 *shortp++;
wdenk38635852002-08-27 05:55:31 +0000604 }
605 }
606 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800607 cp = (u8 *)buf;
wdenk38635852002-08-27 05:55:31 +0000608 i = length;
609 while (i-- > 0)
Marek Vasutf3b3c3d2011-09-26 02:26:06 +0200610 *cp++;
wdenk38635852002-08-27 05:55:31 +0000611 }
Simon Glass0628ab82013-02-24 17:33:15 +0000612 unmap_sysmem(buf);
Simon Glass92765f422013-06-11 11:14:35 -0700613
614 return 0;
wdenk38635852002-08-27 05:55:31 +0000615}
616
wdenk56523f12004-07-11 17:40:54 +0000617#ifdef CONFIG_LOOPW
Simon Glass09140112020-05-10 11:40:03 -0600618static int do_mem_loopw(struct cmd_tbl *cmdtp, int flag, int argc,
619 char *const argv[])
wdenk56523f12004-07-11 17:40:54 +0000620{
York Sun4d1fd7f2014-02-26 17:03:19 -0800621 ulong addr, length, i, bytes;
wdenk56523f12004-07-11 17:40:54 +0000622 int size;
Simon Glass46809762020-06-02 19:26:46 -0600623 volatile ulong *llp; /* 64-bit if SUPPORT_64BIT_DATA */
624 ulong data; /* 64-bit if SUPPORT_64BIT_DATA */
York Sun76698b42014-02-12 15:55:35 -0800625 volatile u32 *longp;
626 volatile u16 *shortp;
627 volatile u8 *cp;
Simon Glass0628ab82013-02-24 17:33:15 +0000628 void *buf;
wdenk81050922004-07-11 20:04:51 +0000629
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200630 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000631 return CMD_RET_USAGE;
wdenk56523f12004-07-11 17:40:54 +0000632
Robert P. J. Day85de63e2013-02-03 02:29:54 +0000633 /*
634 * Check for a size specification.
wdenk56523f12004-07-11 17:40:54 +0000635 * Defaults to long if no or incorrect specification.
636 */
637 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
638 return 1;
639
640 /* Address is always specified.
641 */
Simon Glass7e5f4602021-07-24 09:03:29 -0600642 addr = hextoul(argv[1], NULL);
wdenk56523f12004-07-11 17:40:54 +0000643
644 /* Length is the number of objects, not number of bytes.
645 */
Simon Glass7e5f4602021-07-24 09:03:29 -0600646 length = hextoul(argv[2], NULL);
wdenk56523f12004-07-11 17:40:54 +0000647
648 /* data to write */
Simon Glass46809762020-06-02 19:26:46 -0600649 if (SUPPORT_64BIT_DATA)
650 data = simple_strtoull(argv[3], NULL, 16);
651 else
Simon Glass7e5f4602021-07-24 09:03:29 -0600652 data = hextoul(argv[3], NULL);
wdenk81050922004-07-11 20:04:51 +0000653
Simon Glass0628ab82013-02-24 17:33:15 +0000654 bytes = size * length;
655 buf = map_sysmem(addr, bytes);
656
wdenk56523f12004-07-11 17:40:54 +0000657 /* We want to optimize the loops to run as fast as possible.
658 * If we have only one object, just run infinite loops.
659 */
660 if (length == 1) {
Simon Glass46809762020-06-02 19:26:46 -0600661 if (SUPPORT_64BIT_DATA && size == 8) {
662 llp = (ulong *)buf;
York Sun4d1fd7f2014-02-26 17:03:19 -0800663 for (;;)
664 *llp = data;
665 }
wdenk56523f12004-07-11 17:40:54 +0000666 if (size == 4) {
York Sun76698b42014-02-12 15:55:35 -0800667 longp = (u32 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000668 for (;;)
669 *longp = data;
York Sun4d1fd7f2014-02-26 17:03:19 -0800670 }
wdenk56523f12004-07-11 17:40:54 +0000671 if (size == 2) {
York Sun76698b42014-02-12 15:55:35 -0800672 shortp = (u16 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000673 for (;;)
674 *shortp = data;
675 }
York Sun76698b42014-02-12 15:55:35 -0800676 cp = (u8 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000677 for (;;)
678 *cp = data;
679 }
680
Simon Glass46809762020-06-02 19:26:46 -0600681 if (SUPPORT_64BIT_DATA && size == 8) {
York Sun4d1fd7f2014-02-26 17:03:19 -0800682 for (;;) {
Simon Glass46809762020-06-02 19:26:46 -0600683 llp = (ulong *)buf;
York Sun4d1fd7f2014-02-26 17:03:19 -0800684 i = length;
685 while (i-- > 0)
686 *llp++ = data;
687 }
688 }
wdenk56523f12004-07-11 17:40:54 +0000689 if (size == 4) {
690 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800691 longp = (u32 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000692 i = length;
693 while (i-- > 0)
694 *longp++ = data;
695 }
696 }
697 if (size == 2) {
698 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800699 shortp = (u16 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000700 i = length;
701 while (i-- > 0)
702 *shortp++ = data;
703 }
704 }
705 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800706 cp = (u8 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000707 i = length;
708 while (i-- > 0)
709 *cp++ = data;
710 }
711}
712#endif /* CONFIG_LOOPW */
713
Tom Rini68149e92013-03-12 10:07:19 -0400714#ifdef CONFIG_CMD_MEMTEST
Simon Glass5512d5b2013-02-28 17:47:14 +0000715static ulong mem_test_alt(vu_long *buf, ulong start_addr, ulong end_addr,
716 vu_long *dummy)
wdenk38635852002-08-27 05:55:31 +0000717{
Simon Glassc9638f52013-02-24 17:33:16 +0000718 vu_long *addr;
Simon Glassc9638f52013-02-24 17:33:16 +0000719 ulong errs = 0;
720 ulong val, readback;
721 int j;
Simon Glassc9638f52013-02-24 17:33:16 +0000722 vu_long offset;
723 vu_long test_offset;
724 vu_long pattern;
725 vu_long temp;
726 vu_long anti_pattern;
727 vu_long num_words;
wdenk38635852002-08-27 05:55:31 +0000728 static const ulong bitpattern[] = {
729 0x00000001, /* single bit */
730 0x00000003, /* two adjacent bits */
731 0x00000007, /* three adjacent bits */
732 0x0000000F, /* four adjacent bits */
733 0x00000005, /* two non-adjacent bits */
734 0x00000015, /* three non-adjacent bits */
735 0x00000055, /* four non-adjacent bits */
736 0xaaaaaaaa, /* alternating 1/0 */
737 };
wdenk38635852002-08-27 05:55:31 +0000738
Simon Glass5512d5b2013-02-28 17:47:14 +0000739 num_words = (end_addr - start_addr) / sizeof(vu_long);
Simon Glass8c86bbe2013-02-24 17:33:20 +0000740
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000741 /*
742 * Data line test: write a pattern to the first
743 * location, write the 1's complement to a 'parking'
744 * address (changes the state of the data bus so a
745 * floating bus doesn't give a false OK), and then
746 * read the value back. Note that we read it back
747 * into a variable because the next time we read it,
748 * it might be right (been there, tough to explain to
749 * the quality guys why it prints a failure when the
750 * "is" and "should be" are obviously the same in the
751 * error message).
752 *
753 * Rather than exhaustively testing, we test some
754 * patterns by shifting '1' bits through a field of
755 * '0's and '0' bits through a field of '1's (i.e.
756 * pattern and ~pattern).
757 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000758 addr = buf;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000759 for (j = 0; j < sizeof(bitpattern) / sizeof(bitpattern[0]); j++) {
760 val = bitpattern[j];
761 for (; val != 0; val <<= 1) {
Simon Glass5512d5b2013-02-28 17:47:14 +0000762 *addr = val;
Simon Glassc9638f52013-02-24 17:33:16 +0000763 *dummy = ~val; /* clear the test data off the bus */
wdenk38635852002-08-27 05:55:31 +0000764 readback = *addr;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000765 if (readback != val) {
Simon Glassc9638f52013-02-24 17:33:16 +0000766 printf("FAILURE (data line): "
767 "expected %08lx, actual %08lx\n",
768 val, readback);
769 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000770 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000771 return -1;
wdenk38635852002-08-27 05:55:31 +0000772 }
773 *addr = ~val;
774 *dummy = val;
775 readback = *addr;
Simon Glassc9638f52013-02-24 17:33:16 +0000776 if (readback != ~val) {
777 printf("FAILURE (data line): "
778 "Is %08lx, should be %08lx\n",
779 readback, ~val);
780 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000781 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000782 return -1;
wdenk38635852002-08-27 05:55:31 +0000783 }
wdenk38635852002-08-27 05:55:31 +0000784 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000785 }
wdenk38635852002-08-27 05:55:31 +0000786
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000787 /*
788 * Based on code whose Original Author and Copyright
789 * information follows: Copyright (c) 1998 by Michael
790 * Barr. This software is placed into the public
791 * domain and may be used for any purpose. However,
792 * this notice must not be changed or removed and no
793 * warranty is either expressed or implied by its
794 * publication or distribution.
795 */
wdenk38635852002-08-27 05:55:31 +0000796
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000797 /*
798 * Address line test
wdenk38635852002-08-27 05:55:31 +0000799
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000800 * Description: Test the address bus wiring in a
801 * memory region by performing a walking
802 * 1's test on the relevant bits of the
803 * address and checking for aliasing.
804 * This test will find single-bit
805 * address failures such as stuck-high,
806 * stuck-low, and shorted pins. The base
807 * address and size of the region are
808 * selected by the caller.
wdenk38635852002-08-27 05:55:31 +0000809
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000810 * Notes: For best results, the selected base
811 * address should have enough LSB 0's to
812 * guarantee single address bit changes.
813 * For example, to test a 64-Kbyte
814 * region, select a base address on a
815 * 64-Kbyte boundary. Also, select the
816 * region size as a power-of-two if at
817 * all possible.
818 *
819 * Returns: 0 if the test succeeds, 1 if the test fails.
820 */
Heinrich Schuchardt5c3ea292023-01-27 00:38:50 +0100821 pattern = (vu_long)0xaaaaaaaaaaaaaaaa;
822 anti_pattern = (vu_long)0x5555555555555555;
wdenk38635852002-08-27 05:55:31 +0000823
Simon Glass5512d5b2013-02-28 17:47:14 +0000824 debug("%s:%d: length = 0x%.8lx\n", __func__, __LINE__, num_words);
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000825 /*
826 * Write the default pattern at each of the
827 * power-of-two offsets.
828 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000829 for (offset = 1; offset < num_words; offset <<= 1)
830 addr[offset] = pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000831
832 /*
833 * Check for address bits stuck high.
834 */
835 test_offset = 0;
Simon Glass5512d5b2013-02-28 17:47:14 +0000836 addr[test_offset] = anti_pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000837
Simon Glass5512d5b2013-02-28 17:47:14 +0000838 for (offset = 1; offset < num_words; offset <<= 1) {
839 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000840 if (temp != pattern) {
Simon Glassc9638f52013-02-24 17:33:16 +0000841 printf("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000842 " expected 0x%.8lx, actual 0x%.8lx\n",
David Feng102c0512014-02-12 16:10:08 +0800843 start_addr + offset*sizeof(vu_long),
844 pattern, temp);
Paul Gortmaker87b22b72009-10-02 18:18:33 -0400845 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000846 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000847 return -1;
wdenk38635852002-08-27 05:55:31 +0000848 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000849 }
Simon Glass5512d5b2013-02-28 17:47:14 +0000850 addr[test_offset] = pattern;
Stefan Roese29caf932022-09-02 14:10:46 +0200851 schedule();
wdenk38635852002-08-27 05:55:31 +0000852
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000853 /*
854 * Check for addr bits stuck low or shorted.
855 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000856 for (test_offset = 1; test_offset < num_words; test_offset <<= 1) {
857 addr[test_offset] = anti_pattern;
wdenk38635852002-08-27 05:55:31 +0000858
Simon Glass5512d5b2013-02-28 17:47:14 +0000859 for (offset = 1; offset < num_words; offset <<= 1) {
860 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000861 if ((temp != pattern) && (offset != test_offset)) {
862 printf("\nFAILURE: Address bit stuck low or"
863 " shorted @ 0x%.8lx: expected 0x%.8lx,"
864 " actual 0x%.8lx\n",
David Feng102c0512014-02-12 16:10:08 +0800865 start_addr + offset*sizeof(vu_long),
866 pattern, temp);
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000867 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000868 if (ctrlc())
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000869 return -1;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000870 }
wdenk38635852002-08-27 05:55:31 +0000871 }
Simon Glass5512d5b2013-02-28 17:47:14 +0000872 addr[test_offset] = pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000873 }
wdenk38635852002-08-27 05:55:31 +0000874
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000875 /*
876 * Description: Test the integrity of a physical
877 * memory device by performing an
878 * increment/decrement test over the
879 * entire region. In the process every
880 * storage bit in the device is tested
881 * as a zero and a one. The base address
882 * and the size of the region are
883 * selected by the caller.
884 *
885 * Returns: 0 if the test succeeds, 1 if the test fails.
886 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000887 num_words++;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000888
889 /*
890 * Fill memory with a known pattern.
891 */
892 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
Stefan Roese29caf932022-09-02 14:10:46 +0200893 schedule();
Simon Glass5512d5b2013-02-28 17:47:14 +0000894 addr[offset] = pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000895 }
896
897 /*
898 * Check each location and invert it for the second pass.
899 */
900 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
Stefan Roese29caf932022-09-02 14:10:46 +0200901 schedule();
Simon Glass5512d5b2013-02-28 17:47:14 +0000902 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000903 if (temp != pattern) {
Simon Glassc9638f52013-02-24 17:33:16 +0000904 printf("\nFAILURE (read/write) @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000905 " expected 0x%.8lx, actual 0x%.8lx)\n",
David Feng102c0512014-02-12 16:10:08 +0800906 start_addr + offset*sizeof(vu_long),
907 pattern, temp);
Paul Gortmaker87b22b72009-10-02 18:18:33 -0400908 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000909 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000910 return -1;
wdenk38635852002-08-27 05:55:31 +0000911 }
912
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000913 anti_pattern = ~pattern;
Simon Glass5512d5b2013-02-28 17:47:14 +0000914 addr[offset] = anti_pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000915 }
916
917 /*
918 * Check each location for the inverted pattern and zero it.
919 */
920 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
Stefan Roese29caf932022-09-02 14:10:46 +0200921 schedule();
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000922 anti_pattern = ~pattern;
Simon Glass5512d5b2013-02-28 17:47:14 +0000923 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000924 if (temp != anti_pattern) {
Simon Glassc9638f52013-02-24 17:33:16 +0000925 printf("\nFAILURE (read/write): @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000926 " expected 0x%.8lx, actual 0x%.8lx)\n",
David Feng102c0512014-02-12 16:10:08 +0800927 start_addr + offset*sizeof(vu_long),
928 anti_pattern, temp);
Paul Gortmaker87b22b72009-10-02 18:18:33 -0400929 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000930 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000931 return -1;
wdenk38635852002-08-27 05:55:31 +0000932 }
Simon Glass5512d5b2013-02-28 17:47:14 +0000933 addr[offset] = 0;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000934 }
Simon Glass51209b12013-02-24 17:33:17 +0000935
Rasmus Villemoesfea67302016-01-07 11:36:04 +0100936 return errs;
Simon Glassc9638f52013-02-24 17:33:16 +0000937}
wdenk38635852002-08-27 05:55:31 +0000938
Stefan Roese8e434cb2020-03-05 07:21:32 +0100939static int compare_regions(volatile unsigned long *bufa,
940 volatile unsigned long *bufb, size_t count)
941{
942 volatile unsigned long *p1 = bufa;
943 volatile unsigned long *p2 = bufb;
944 int errs = 0;
945 size_t i;
946
947 for (i = 0; i < count; i++, p1++, p2++) {
948 if (*p1 != *p2) {
949 printf("FAILURE: 0x%08lx != 0x%08lx (delta=0x%08lx -> bit %ld) at offset 0x%08lx\n",
950 (unsigned long)*p1, (unsigned long)*p2,
951 *p1 ^ *p2, __ffs(*p1 ^ *p2),
952 (unsigned long)(i * sizeof(unsigned long)));
953 errs++;
954 }
955 }
956
957 return errs;
958}
959
960static ulong test_bitflip_comparison(volatile unsigned long *bufa,
961 volatile unsigned long *bufb, size_t count)
962{
963 volatile unsigned long *p1 = bufa;
964 volatile unsigned long *p2 = bufb;
965 unsigned int j, k;
966 unsigned long q;
967 size_t i;
968 int max;
969 int errs = 0;
970
971 max = sizeof(unsigned long) * 8;
972 for (k = 0; k < max; k++) {
Heinrich Schuchardt5c3ea292023-01-27 00:38:50 +0100973 q = 1UL << k;
Stefan Roese8e434cb2020-03-05 07:21:32 +0100974 for (j = 0; j < 8; j++) {
Stefan Roese29caf932022-09-02 14:10:46 +0200975 schedule();
Stefan Roese8e434cb2020-03-05 07:21:32 +0100976 q = ~q;
977 p1 = (volatile unsigned long *)bufa;
978 p2 = (volatile unsigned long *)bufb;
979 for (i = 0; i < count; i++)
980 *p1++ = *p2++ = (i % 2) == 0 ? q : ~q;
981
982 errs += compare_regions(bufa, bufb, count);
983 }
984
985 if (ctrlc())
986 return -1UL;
987 }
988
989 return errs;
990}
991
Ralph Siemsen9989fb12020-09-09 12:10:00 -0400992static ulong mem_test_bitflip(vu_long *buf, ulong start, ulong end)
993{
994 /*
995 * Split the specified range into two halves.
996 * Note that mtest range is inclusive of start,end.
997 * Bitflip test instead uses a count (of 32-bit words).
998 */
999 ulong half_size = (end - start + 1) / 2 / sizeof(unsigned long);
1000
1001 return test_bitflip_comparison(buf, buf + half_size, half_size);
1002}
1003
Simon Glass5512d5b2013-02-28 17:47:14 +00001004static ulong mem_test_quick(vu_long *buf, ulong start_addr, ulong end_addr,
1005 vu_long pattern, int iteration)
Simon Glassc9638f52013-02-24 17:33:16 +00001006{
Simon Glass5512d5b2013-02-28 17:47:14 +00001007 vu_long *end;
Simon Glassc9638f52013-02-24 17:33:16 +00001008 vu_long *addr;
Simon Glassc9638f52013-02-24 17:33:16 +00001009 ulong errs = 0;
Simon Glass5512d5b2013-02-28 17:47:14 +00001010 ulong incr, length;
Simon Glassc9638f52013-02-24 17:33:16 +00001011 ulong val, readback;
Heinrich Schuchardt5c3ea292023-01-27 00:38:50 +01001012 const int plen = 2 * sizeof(ulong);
Simon Glassc9638f52013-02-24 17:33:16 +00001013
Simon Glass51209b12013-02-24 17:33:17 +00001014 /* Alternate the pattern */
wdenk38635852002-08-27 05:55:31 +00001015 incr = 1;
Simon Glass51209b12013-02-24 17:33:17 +00001016 if (iteration & 1) {
1017 incr = -incr;
1018 /*
1019 * Flip the pattern each time to make lots of zeros and
1020 * then, the next time, lots of ones. We decrement
1021 * the "negative" patterns and increment the "positive"
1022 * patterns to preserve this feature.
1023 */
Heinrich Schuchardt5c3ea292023-01-27 00:38:50 +01001024 if (pattern > (ulong)LONG_MAX)
Simon Glass51209b12013-02-24 17:33:17 +00001025 pattern = -pattern; /* complement & increment */
1026 else
1027 pattern = ~pattern;
1028 }
Simon Glass5512d5b2013-02-28 17:47:14 +00001029 length = (end_addr - start_addr) / sizeof(ulong);
1030 end = buf + length;
Heinrich Schuchardt5c3ea292023-01-27 00:38:50 +01001031 printf("\rPattern %0*lX Writing..."
Simon Glass7ecbd4d2013-02-24 17:33:18 +00001032 "%12s"
1033 "\b\b\b\b\b\b\b\b\b\b",
Heinrich Schuchardt5c3ea292023-01-27 00:38:50 +01001034 plen, pattern, "");
wdenk38635852002-08-27 05:55:31 +00001035
Simon Glass5512d5b2013-02-28 17:47:14 +00001036 for (addr = buf, val = pattern; addr < end; addr++) {
Stefan Roese29caf932022-09-02 14:10:46 +02001037 schedule();
Simon Glass7ecbd4d2013-02-24 17:33:18 +00001038 *addr = val;
1039 val += incr;
1040 }
wdenk38635852002-08-27 05:55:31 +00001041
Simon Glass7ecbd4d2013-02-24 17:33:18 +00001042 puts("Reading...");
wdenk38635852002-08-27 05:55:31 +00001043
Simon Glass5512d5b2013-02-28 17:47:14 +00001044 for (addr = buf, val = pattern; addr < end; addr++) {
Stefan Roese29caf932022-09-02 14:10:46 +02001045 schedule();
Simon Glass7ecbd4d2013-02-24 17:33:18 +00001046 readback = *addr;
1047 if (readback != val) {
Simon Glass5512d5b2013-02-28 17:47:14 +00001048 ulong offset = addr - buf;
1049
Heinrich Schuchardt5c3ea292023-01-27 00:38:50 +01001050 printf("\nMem error @ 0x%0*lX: found %0*lX, expected %0*lX\n",
1051 plen, start_addr + offset * sizeof(vu_long),
1052 plen, readback, plen, val);
Simon Glass7ecbd4d2013-02-24 17:33:18 +00001053 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +00001054 if (ctrlc())
Simon Glass7ecbd4d2013-02-24 17:33:18 +00001055 return -1;
wdenk38635852002-08-27 05:55:31 +00001056 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +00001057 val += incr;
1058 }
wdenk38635852002-08-27 05:55:31 +00001059
Rasmus Villemoesfea67302016-01-07 11:36:04 +01001060 return errs;
Simon Glassc9638f52013-02-24 17:33:16 +00001061}
1062
1063/*
1064 * Perform a memory test. A more complete alternative test can be
1065 * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
1066 * interrupted by ctrl-c or by a failure of one of the sub-tests.
1067 */
Simon Glass09140112020-05-10 11:40:03 -06001068static int do_mem_mtest(struct cmd_tbl *cmdtp, int flag, int argc,
1069 char *const argv[])
Simon Glassc9638f52013-02-24 17:33:16 +00001070{
Simon Glass8c86bbe2013-02-24 17:33:20 +00001071 ulong start, end;
Michal Simeke519f032020-05-04 13:54:40 +02001072 vu_long scratch_space;
1073 vu_long *buf, *dummy = &scratch_space;
Tom Riniadcc5702015-04-07 09:38:54 -04001074 ulong iteration_limit = 0;
Stefan Roesea8c708e2020-03-05 07:21:29 +01001075 ulong count = 0;
Simon Glass51209b12013-02-24 17:33:17 +00001076 ulong errs = 0; /* number of errors, or -1 if interrupted */
Pavel Macheke37f1eb2015-04-01 13:50:41 +02001077 ulong pattern = 0;
Simon Glass51209b12013-02-24 17:33:17 +00001078 int iteration;
Simon Glassc9638f52013-02-24 17:33:16 +00001079
Pavel Macheke37f1eb2015-04-01 13:50:41 +02001080 start = CONFIG_SYS_MEMTEST_START;
1081 end = CONFIG_SYS_MEMTEST_END;
1082
Simon Glassc9638f52013-02-24 17:33:16 +00001083 if (argc > 1)
Pavel Macheke37f1eb2015-04-01 13:50:41 +02001084 if (strict_strtoul(argv[1], 16, &start) < 0)
1085 return CMD_RET_USAGE;
Simon Glassc9638f52013-02-24 17:33:16 +00001086
1087 if (argc > 2)
Pavel Macheke37f1eb2015-04-01 13:50:41 +02001088 if (strict_strtoul(argv[2], 16, &end) < 0)
1089 return CMD_RET_USAGE;
Simon Glassc9638f52013-02-24 17:33:16 +00001090
1091 if (argc > 3)
Pavel Macheke37f1eb2015-04-01 13:50:41 +02001092 if (strict_strtoul(argv[3], 16, &pattern) < 0)
1093 return CMD_RET_USAGE;
Simon Glassc9638f52013-02-24 17:33:16 +00001094
1095 if (argc > 4)
Pavel Macheke37f1eb2015-04-01 13:50:41 +02001096 if (strict_strtoul(argv[4], 16, &iteration_limit) < 0)
1097 return CMD_RET_USAGE;
1098
1099 if (end < start) {
1100 printf("Refusing to do empty test\n");
1101 return -1;
1102 }
Simon Glassc9638f52013-02-24 17:33:16 +00001103
Michal Simekdfe461d2016-02-24 08:36:02 +01001104 printf("Testing %08lx ... %08lx:\n", start, end);
Simon Glass8c86bbe2013-02-24 17:33:20 +00001105 debug("%s:%d: start %#08lx end %#08lx\n", __func__, __LINE__,
1106 start, end);
Simon Glass51209b12013-02-24 17:33:17 +00001107
Simon Glass5512d5b2013-02-28 17:47:14 +00001108 buf = map_sysmem(start, end - start);
Simon Glass51209b12013-02-24 17:33:17 +00001109 for (iteration = 0;
1110 !iteration_limit || iteration < iteration_limit;
1111 iteration++) {
1112 if (ctrlc()) {
Simon Glass51209b12013-02-24 17:33:17 +00001113 errs = -1UL;
1114 break;
1115 }
1116
1117 printf("Iteration: %6d\r", iteration + 1);
1118 debug("\n");
Stefan Roesef14bfa72020-03-05 07:21:31 +01001119 if (IS_ENABLED(CONFIG_SYS_ALT_MEMTEST)) {
Simon Glass5512d5b2013-02-28 17:47:14 +00001120 errs = mem_test_alt(buf, start, end, dummy);
Stefan Roese8e434cb2020-03-05 07:21:32 +01001121 if (errs == -1UL)
1122 break;
Ralph Siemsen9989fb12020-09-09 12:10:00 -04001123 if (IS_ENABLED(CONFIG_SYS_ALT_MEMTEST_BITFLIP)) {
1124 count += errs;
1125 errs = mem_test_bitflip(buf, start, end);
1126 }
Simon Glass5512d5b2013-02-28 17:47:14 +00001127 } else {
1128 errs = mem_test_quick(buf, start, end, pattern,
1129 iteration);
1130 }
1131 if (errs == -1UL)
1132 break;
Stefan Roesea8c708e2020-03-05 07:21:29 +01001133 count += errs;
Simon Glass5512d5b2013-02-28 17:47:14 +00001134 }
1135
Stefan Roese54de2442020-03-05 07:21:30 +01001136 unmap_sysmem((void *)buf);
Simon Glass51209b12013-02-24 17:33:17 +00001137
Heinrich Schuchardt5c3ea292023-01-27 00:38:50 +01001138 printf("\nTested %d iteration(s) with %lu errors.\n", iteration, count);
Simon Glassc9638f52013-02-24 17:33:16 +00001139
Stefan Roesea8c708e2020-03-05 07:21:29 +01001140 return errs != 0;
wdenk38635852002-08-27 05:55:31 +00001141}
Wolfgang Denka2681702013-03-08 10:51:32 +00001142#endif /* CONFIG_CMD_MEMTEST */
wdenk38635852002-08-27 05:55:31 +00001143
1144/* Modify memory.
1145 *
1146 * Syntax:
York Sun4d1fd7f2014-02-26 17:03:19 -08001147 * mm{.b, .w, .l, .q} {addr}
wdenk38635852002-08-27 05:55:31 +00001148 */
1149static int
Simon Glass09140112020-05-10 11:40:03 -06001150mod_mem(struct cmd_tbl *cmdtp, int incrflag, int flag, int argc,
1151 char *const argv[])
wdenk38635852002-08-27 05:55:31 +00001152{
York Sun4d1fd7f2014-02-26 17:03:19 -08001153 ulong addr;
Simon Glass46809762020-06-02 19:26:46 -06001154 ulong i; /* 64-bit if SUPPORT_64BIT_DATA */
wdenk27b207f2003-07-24 23:38:38 +00001155 int nbytes, size;
Simon Glass0628ab82013-02-24 17:33:15 +00001156 void *ptr = NULL;
wdenk38635852002-08-27 05:55:31 +00001157
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001158 if (argc != 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001159 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +00001160
Simon Glassb26440f2014-04-10 20:01:31 -06001161 bootretry_reset_cmd_timeout(); /* got a good command to get here */
wdenk38635852002-08-27 05:55:31 +00001162 /* We use the last specified parameters, unless new ones are
1163 * entered.
1164 */
1165 addr = mm_last_addr;
1166 size = mm_last_size;
1167
1168 if ((flag & CMD_FLAG_REPEAT) == 0) {
1169 /* New command specified. Check for a size specification.
1170 * Defaults to long if no or incorrect specification.
1171 */
wdenk27b207f2003-07-24 23:38:38 +00001172 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
1173 return 1;
wdenk38635852002-08-27 05:55:31 +00001174
1175 /* Address is specified since argc > 1
1176 */
Simon Glass7e5f4602021-07-24 09:03:29 -06001177 addr = hextoul(argv[1], NULL);
wdenk38635852002-08-27 05:55:31 +00001178 addr += base_address;
1179 }
1180
1181 /* Print the address, followed by value. Then accept input for
1182 * the next value. A non-converted value exits.
1183 */
1184 do {
Simon Glass0628ab82013-02-24 17:33:15 +00001185 ptr = map_sysmem(addr, size);
wdenk38635852002-08-27 05:55:31 +00001186 printf("%08lx:", addr);
1187 if (size == 4)
York Sun76698b42014-02-12 15:55:35 -08001188 printf(" %08x", *((u32 *)ptr));
Simon Glass46809762020-06-02 19:26:46 -06001189 else if (SUPPORT_64BIT_DATA && size == 8)
1190 printf(" %0lx", *((ulong *)ptr));
wdenk38635852002-08-27 05:55:31 +00001191 else if (size == 2)
York Sun76698b42014-02-12 15:55:35 -08001192 printf(" %04x", *((u16 *)ptr));
wdenk38635852002-08-27 05:55:31 +00001193 else
York Sun76698b42014-02-12 15:55:35 -08001194 printf(" %02x", *((u8 *)ptr));
wdenk38635852002-08-27 05:55:31 +00001195
Simon Glasse1bf8242014-04-10 20:01:27 -06001196 nbytes = cli_readline(" ? ");
wdenk38635852002-08-27 05:55:31 +00001197 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
1198 /* <CR> pressed as only input, don't modify current
1199 * location and move to next. "-" pressed will go back.
1200 */
1201 if (incrflag)
1202 addr += nbytes ? -size : size;
1203 nbytes = 1;
Simon Glassb26440f2014-04-10 20:01:31 -06001204 /* good enough to not time out */
1205 bootretry_reset_cmd_timeout();
wdenk38635852002-08-27 05:55:31 +00001206 }
1207#ifdef CONFIG_BOOT_RETRY_TIME
1208 else if (nbytes == -2) {
1209 break; /* timed out, exit the command */
1210 }
1211#endif
1212 else {
1213 char *endp;
Simon Glass46809762020-06-02 19:26:46 -06001214 if (SUPPORT_64BIT_DATA)
1215 i = simple_strtoull(console_buffer, &endp, 16);
1216 else
Simon Glass7e5f4602021-07-24 09:03:29 -06001217 i = hextoul(console_buffer, &endp);
wdenk38635852002-08-27 05:55:31 +00001218 nbytes = endp - console_buffer;
1219 if (nbytes) {
wdenk38635852002-08-27 05:55:31 +00001220 /* good enough to not time out
1221 */
Simon Glassb26440f2014-04-10 20:01:31 -06001222 bootretry_reset_cmd_timeout();
wdenk38635852002-08-27 05:55:31 +00001223 if (size == 4)
York Sun76698b42014-02-12 15:55:35 -08001224 *((u32 *)ptr) = i;
Simon Glass46809762020-06-02 19:26:46 -06001225 else if (SUPPORT_64BIT_DATA && size == 8)
1226 *((ulong *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001227 else if (size == 2)
York Sun76698b42014-02-12 15:55:35 -08001228 *((u16 *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001229 else
York Sun76698b42014-02-12 15:55:35 -08001230 *((u8 *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001231 if (incrflag)
1232 addr += size;
1233 }
1234 }
1235 } while (nbytes);
Simon Glass0628ab82013-02-24 17:33:15 +00001236 if (ptr)
1237 unmap_sysmem(ptr);
wdenk38635852002-08-27 05:55:31 +00001238
1239 mm_last_addr = addr;
1240 mm_last_size = size;
1241 return 0;
1242}
1243
Mike Frysinger710b9932010-12-21 14:19:51 -05001244#ifdef CONFIG_CMD_CRC32
1245
Simon Glass09140112020-05-10 11:40:03 -06001246static int do_mem_crc(struct cmd_tbl *cmdtp, int flag, int argc,
1247 char *const argv[])
wdenk38635852002-08-27 05:55:31 +00001248{
Simon Glassd20a40d2013-02-24 20:30:22 +00001249 int flags = 0;
1250 int ac;
1251 char * const *av;
wdenk38635852002-08-27 05:55:31 +00001252
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001253 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001254 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +00001255
wdenkc26e4542004-04-18 10:13:26 +00001256 av = argv + 1;
1257 ac = argc - 1;
Daniel Thompson221a9492017-05-19 17:26:58 +01001258#ifdef CONFIG_CRC32_VERIFY
wdenkc26e4542004-04-18 10:13:26 +00001259 if (strcmp(*av, "-v") == 0) {
Joe Hershbergera69bdba2015-05-05 12:23:53 -05001260 flags |= HASH_FLAG_VERIFY | HASH_FLAG_ENV;
wdenkc26e4542004-04-18 10:13:26 +00001261 av++;
1262 ac--;
wdenkc26e4542004-04-18 10:13:26 +00001263 }
Simon Glassd20a40d2013-02-24 20:30:22 +00001264#endif
wdenkc26e4542004-04-18 10:13:26 +00001265
Simon Glassd20a40d2013-02-24 20:30:22 +00001266 return hash_command("crc32", flags, cmdtp, flag, ac, av);
wdenkc26e4542004-04-18 10:13:26 +00001267}
wdenkc26e4542004-04-18 10:13:26 +00001268
Mike Frysinger710b9932010-12-21 14:19:51 -05001269#endif
1270
Jean-Jacques Hiblot803e1a32019-07-02 14:23:26 +02001271#ifdef CONFIG_CMD_RANDOM
Simon Glass09140112020-05-10 11:40:03 -06001272static int do_random(struct cmd_tbl *cmdtp, int flag, int argc,
1273 char *const argv[])
Jean-Jacques Hiblot803e1a32019-07-02 14:23:26 +02001274{
1275 unsigned long addr, len;
1276 unsigned long seed; // NOT INITIALIZED ON PURPOSE
1277 unsigned int *buf, *start;
1278 unsigned char *buf8;
1279 unsigned int i;
1280
Eugeniy Paltsev5f2c4e02020-03-20 19:38:17 +03001281 if (argc < 3 || argc > 4)
1282 return CMD_RET_USAGE;
Jean-Jacques Hiblot803e1a32019-07-02 14:23:26 +02001283
Simon Glass7e5f4602021-07-24 09:03:29 -06001284 len = hextoul(argv[2], NULL);
1285 addr = hextoul(argv[1], NULL);
Jean-Jacques Hiblot803e1a32019-07-02 14:23:26 +02001286
1287 if (argc == 4) {
Simon Glass7e5f4602021-07-24 09:03:29 -06001288 seed = hextoul(argv[3], NULL);
Jean-Jacques Hiblot803e1a32019-07-02 14:23:26 +02001289 if (seed == 0) {
1290 printf("The seed cannot be 0. Using 0xDEADBEEF.\n");
1291 seed = 0xDEADBEEF;
1292 }
1293 } else {
1294 seed = get_timer(0) ^ rand();
1295 }
1296
1297 srand(seed);
1298 start = map_sysmem(addr, len);
1299 buf = start;
1300 for (i = 0; i < (len / 4); i++)
1301 *buf++ = rand();
1302
1303 buf8 = (unsigned char *)buf;
1304 for (i = 0; i < (len % 4); i++)
1305 *buf8++ = rand() & 0xFF;
1306
1307 unmap_sysmem(start);
1308 printf("%lu bytes filled with random data\n", len);
Eugeniy Paltsev5f2c4e02020-03-20 19:38:17 +03001309
1310 return CMD_RET_SUCCESS;
Jean-Jacques Hiblot803e1a32019-07-02 14:23:26 +02001311}
1312#endif
1313
wdenk8bde7f72003-06-27 21:31:46 +00001314/**************************************************/
wdenk0d498392003-07-01 21:06:45 +00001315U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001316 md, 3, 1, do_mem_md,
Peter Tyser2fb26042009-01-27 18:03:12 -06001317 "memory display",
Simon Glass76be8f72020-06-02 19:26:45 -06001318 "[.b, .w, .l" HELP_Q "] address [# of objects]"
wdenk8bde7f72003-06-27 21:31:46 +00001319);
1320
1321
wdenk0d498392003-07-01 21:06:45 +00001322U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001323 mm, 2, 1, do_mem_mm,
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001324 "memory modify (auto-incrementing address)",
Simon Glass76be8f72020-06-02 19:26:45 -06001325 "[.b, .w, .l" HELP_Q "] address"
wdenk8bde7f72003-06-27 21:31:46 +00001326);
1327
1328
wdenk0d498392003-07-01 21:06:45 +00001329U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001330 nm, 2, 1, do_mem_nm,
Peter Tyser2fb26042009-01-27 18:03:12 -06001331 "memory modify (constant address)",
Simon Glass76be8f72020-06-02 19:26:45 -06001332 "[.b, .w, .l" HELP_Q "] address"
wdenk8bde7f72003-06-27 21:31:46 +00001333);
1334
wdenk0d498392003-07-01 21:06:45 +00001335U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001336 mw, 4, 1, do_mem_mw,
Peter Tyser2fb26042009-01-27 18:03:12 -06001337 "memory write (fill)",
Simon Glass76be8f72020-06-02 19:26:45 -06001338 "[.b, .w, .l" HELP_Q "] address value [count]"
wdenk8bde7f72003-06-27 21:31:46 +00001339);
1340
wdenk0d498392003-07-01 21:06:45 +00001341U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001342 cp, 4, 1, do_mem_cp,
Peter Tyser2fb26042009-01-27 18:03:12 -06001343 "memory copy",
Simon Glass76be8f72020-06-02 19:26:45 -06001344 "[.b, .w, .l" HELP_Q "] source target count"
wdenk8bde7f72003-06-27 21:31:46 +00001345);
1346
wdenk0d498392003-07-01 21:06:45 +00001347U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001348 cmp, 4, 1, do_mem_cmp,
Peter Tyser2fb26042009-01-27 18:03:12 -06001349 "memory compare",
Simon Glass76be8f72020-06-02 19:26:45 -06001350 "[.b, .w, .l" HELP_Q "] addr1 addr2 count"
wdenk8bde7f72003-06-27 21:31:46 +00001351);
1352
Simon Glass550a9e72020-07-28 19:41:14 -06001353#ifdef CONFIG_CMD_MEM_SEARCH
Simon Glassbdded202020-06-02 19:26:49 -06001354/**************************************************/
1355U_BOOT_CMD(
1356 ms, 255, 1, do_mem_search,
1357 "memory search",
1358 "[.b, .w, .l" HELP_Q ", .s] [-q | -<n>] address #-of-objects <value>..."
Simon Glass550a9e72020-07-28 19:41:14 -06001359 " -q = quiet, -l<val> = match limit"
Simon Glassbdded202020-06-02 19:26:49 -06001360);
1361#endif
1362
Mike Frysinger710b9932010-12-21 14:19:51 -05001363#ifdef CONFIG_CMD_CRC32
1364
Daniel Thompson221a9492017-05-19 17:26:58 +01001365#ifndef CONFIG_CRC32_VERIFY
wdenkc26e4542004-04-18 10:13:26 +00001366
wdenk0d498392003-07-01 21:06:45 +00001367U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001368 crc32, 4, 1, do_mem_crc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001369 "checksum calculation",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001370 "address count [addr]\n - compute CRC32 checksum [save at addr]"
wdenk8bde7f72003-06-27 21:31:46 +00001371);
1372
Daniel Thompson221a9492017-05-19 17:26:58 +01001373#else /* CONFIG_CRC32_VERIFY */
wdenkc26e4542004-04-18 10:13:26 +00001374
1375U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001376 crc32, 5, 1, do_mem_crc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001377 "checksum calculation",
wdenkc26e4542004-04-18 10:13:26 +00001378 "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001379 "-v address count crc\n - verify crc of memory area"
wdenkc26e4542004-04-18 10:13:26 +00001380);
1381
Daniel Thompson221a9492017-05-19 17:26:58 +01001382#endif /* CONFIG_CRC32_VERIFY */
wdenkc26e4542004-04-18 10:13:26 +00001383
Mike Frysinger710b9932010-12-21 14:19:51 -05001384#endif
1385
Simon Glass15a33e42012-11-30 13:01:20 +00001386#ifdef CONFIG_CMD_MEMINFO
Simon Glass09140112020-05-10 11:40:03 -06001387static int do_mem_info(struct cmd_tbl *cmdtp, int flag, int argc,
1388 char *const argv[])
Simon Glass15a33e42012-11-30 13:01:20 +00001389{
Simon Glass428a6a12019-11-14 12:57:44 -07001390 puts("DRAM: ");
1391 print_size(gd->ram_size, "\n");
Simon Glass15a33e42012-11-30 13:01:20 +00001392
1393 return 0;
1394}
1395#endif
1396
wdenk0d498392003-07-01 21:06:45 +00001397U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001398 base, 2, 1, do_mem_base,
Peter Tyser2fb26042009-01-27 18:03:12 -06001399 "print or set address offset",
wdenk8bde7f72003-06-27 21:31:46 +00001400 "\n - print address offset for memory commands\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001401 "base off\n - set address offset for memory commands to 'off'"
wdenk8bde7f72003-06-27 21:31:46 +00001402);
1403
wdenk0d498392003-07-01 21:06:45 +00001404U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001405 loop, 3, 1, do_mem_loop,
Peter Tyser2fb26042009-01-27 18:03:12 -06001406 "infinite loop on address range",
Simon Glass76be8f72020-06-02 19:26:45 -06001407 "[.b, .w, .l" HELP_Q "] address number_of_objects"
wdenk8bde7f72003-06-27 21:31:46 +00001408);
1409
wdenk56523f12004-07-11 17:40:54 +00001410#ifdef CONFIG_LOOPW
1411U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001412 loopw, 4, 1, do_mem_loopw,
Peter Tyser2fb26042009-01-27 18:03:12 -06001413 "infinite write loop on address range",
Simon Glass76be8f72020-06-02 19:26:45 -06001414 "[.b, .w, .l" HELP_Q "] address number_of_objects data_to_write"
wdenk56523f12004-07-11 17:40:54 +00001415);
1416#endif /* CONFIG_LOOPW */
1417
Wolfgang Denka2681702013-03-08 10:51:32 +00001418#ifdef CONFIG_CMD_MEMTEST
wdenk0d498392003-07-01 21:06:45 +00001419U_BOOT_CMD(
Dirk Eibachb6fc6fd2008-12-16 14:51:56 +01001420 mtest, 5, 1, do_mem_mtest,
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001421 "simple RAM read/write test",
1422 "[start [end [pattern [iterations]]]]"
wdenk8bde7f72003-06-27 21:31:46 +00001423);
Wolfgang Denka2681702013-03-08 10:51:32 +00001424#endif /* CONFIG_CMD_MEMTEST */
wdenk8bde7f72003-06-27 21:31:46 +00001425
Joel Johnson72732312020-01-29 09:17:18 -07001426#ifdef CONFIG_CMD_MX_CYCLIC
stroese4aaf29b2004-12-16 17:42:39 +00001427U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001428 mdc, 4, 1, do_mem_mdc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001429 "memory display cyclic",
Simon Glass76be8f72020-06-02 19:26:45 -06001430 "[.b, .w, .l" HELP_Q "] address count delay(ms)"
stroese4aaf29b2004-12-16 17:42:39 +00001431);
1432
1433U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001434 mwc, 4, 1, do_mem_mwc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001435 "memory write cyclic",
Simon Glass76be8f72020-06-02 19:26:45 -06001436 "[.b, .w, .l" HELP_Q "] address value delay(ms)"
stroese4aaf29b2004-12-16 17:42:39 +00001437);
Joel Johnson72732312020-01-29 09:17:18 -07001438#endif /* CONFIG_CMD_MX_CYCLIC */
Simon Glass15a33e42012-11-30 13:01:20 +00001439
1440#ifdef CONFIG_CMD_MEMINFO
1441U_BOOT_CMD(
1442 meminfo, 3, 1, do_mem_info,
1443 "display memory information",
1444 ""
1445);
1446#endif
Jean-Jacques Hiblot803e1a32019-07-02 14:23:26 +02001447
1448#ifdef CONFIG_CMD_RANDOM
1449U_BOOT_CMD(
1450 random, 4, 0, do_random,
1451 "fill memory with random pattern",
1452 "<addr> <len> [<seed>]\n"
1453 " - Fill 'len' bytes of memory starting at 'addr' with random data\n"
1454);
1455#endif