blob: 1f4e3fcdede19c6723bc1e58337bd6494b9413c6 [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>
Tom Rini17ead042022-07-23 13:05:03 -040019#ifdef CONFIG_MTD_NOR_FLASH
Simon Glass0ee482522019-12-28 10:44:40 -070020#include <flash.h>
Tom Rini17ead042022-07-23 13:05:03 -040021#endif
Simon Glassd20a40d2013-02-24 20:30:22 +000022#include <hash.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060023#include <log.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050024#include <mapmem.h>
Simon Glass90526e92020-05-10 11:39:56 -060025#include <rand.h>
Sergei Poselenova6e6fc62008-04-09 16:09:41 +020026#include <watchdog.h>
Simon Glass401d1c42020-10-30 21:38:53 -060027#include <asm/global_data.h>
Simon Glass0628ab82013-02-24 17:33:15 +000028#include <asm/io.h>
Simon Glasscd93d622020-05-10 11:40:13 -060029#include <linux/bitops.h>
Simon Glass15a33e42012-11-30 13:01:20 +000030#include <linux/compiler.h>
Simon Glassbdded202020-06-02 19:26:49 -060031#include <linux/ctype.h>
Simon Glassc05ed002020-05-10 11:40:11 -060032#include <linux/delay.h>
Simon Glass15a33e42012-11-30 13:01:20 +000033
34DECLARE_GLOBAL_DATA_PTR;
wdenk38635852002-08-27 05:55:31 +000035
Simon Glass76be8f72020-06-02 19:26:45 -060036/* Create a compile-time value */
Simon Glass46809762020-06-02 19:26:46 -060037#ifdef MEM_SUPPORT_64BIT_DATA
38#define SUPPORT_64BIT_DATA 1
Simon Glass76be8f72020-06-02 19:26:45 -060039#define HELP_Q ", .q"
40#else
Simon Glass46809762020-06-02 19:26:46 -060041#define SUPPORT_64BIT_DATA 0
Simon Glass76be8f72020-06-02 19:26:45 -060042#define HELP_Q ""
43#endif
44
Simon Glass09140112020-05-10 11:40:03 -060045static int mod_mem(struct cmd_tbl *, int, int, int, char * const []);
wdenk38635852002-08-27 05:55:31 +000046
47/* Display values from last command.
48 * Memory modify remembered values are different from display memory.
49 */
Scott Wood581508b2015-03-19 09:43:12 -070050static ulong dp_last_addr, dp_last_size;
51static ulong dp_last_length = 0x40;
52static ulong mm_last_addr, mm_last_size;
wdenk38635852002-08-27 05:55:31 +000053
54static ulong base_address = 0;
Simon Glass550a9e72020-07-28 19:41:14 -060055#ifdef CONFIG_CMD_MEM_SEARCH
56static ulong dp_last_ms_length;
Simon Glassbdded202020-06-02 19:26:49 -060057static 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 */
Simon Glass7e5f4602021-07-24 09:03:29 -060094 addr = hextoul(argv[1], NULL);
wdenk38635852002-08-27 05:55:31 +000095 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)
Simon Glass7e5f4602021-07-24 09:03:29 -0600101 length = hextoul(argv[2], NULL);
wdenk38635852002-08-27 05:55:31 +0000102 }
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 */
Simon Glass7e5f4602021-07-24 09:03:29 -0600149 addr = hextoul(argv[1], NULL);
wdenk38635852002-08-27 05:55:31 +0000150 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
Simon Glass7e5f4602021-07-24 09:03:29 -0600157 writeval = hextoul(argv[2], NULL);
wdenk38635852002-08-27 05:55:31 +0000158
159 /* Count ? */
160 if (argc == 4) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600161 count = hextoul(argv[3], NULL);
wdenk38635852002-08-27 05:55:31 +0000162 } 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
Simon Glass0b1284e2021-07-24 09:03:30 -0600194 count = dectoul(argv[3], NULL);
stroese4aaf29b2004-12-16 17:42:39 +0000195
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
Simon Glass0b1284e2021-07-24 09:03:30 -0600222 count = dectoul(argv[3], NULL);
stroese4aaf29b2004-12-16 17:42:39 +0000223
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
Simon Glass7e5f4602021-07-24 09:03:29 -0600263 addr1 = hextoul(argv[1], NULL);
wdenk38635852002-08-27 05:55:31 +0000264 addr1 += base_address;
265
Simon Glass7e5f4602021-07-24 09:03:29 -0600266 addr2 = hextoul(argv[2], NULL);
wdenk38635852002-08-27 05:55:31 +0000267 addr2 += base_address;
268
Simon Glass7e5f4602021-07-24 09:03:29 -0600269 count = hextoul(argv[3], NULL);
wdenk38635852002-08-27 05:55:31 +0000270
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
Simon Glass7e5f4602021-07-24 09:03:29 -0600326 addr = hextoul(argv[1], NULL);
wdenk38635852002-08-27 05:55:31 +0000327 addr += base_address;
328
Simon Glass7e5f4602021-07-24 09:03:29 -0600329 dest = hextoul(argv[2], NULL);
wdenk38635852002-08-27 05:55:31 +0000330 dest += base_address;
331
Simon Glass7e5f4602021-07-24 09:03:29 -0600332 count = hextoul(argv[3], NULL);
wdenk38635852002-08-27 05:55:31 +0000333
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 Glass550a9e72020-07-28 19:41:14 -0600370#ifdef CONFIG_CMD_MEM_SEARCH
Simon Glassbdded202020-06-02 19:26:49 -0600371static 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;
Simon Glass550a9e72020-07-28 19:41:14 -0600380 int used_len;
Simon Glassbdded202020-06-02 19:26:49 -0600381 int count;
382 int size;
383 int i;
384
385 /* We use the last specified parameters, unless new ones are entered */
386 addr = dp_last_addr;
387 size = dp_last_size;
Simon Glass550a9e72020-07-28 19:41:14 -0600388 length = dp_last_ms_length;
Simon Glassbdded202020-06-02 19:26:49 -0600389
390 if (argc < 3)
391 return CMD_RET_USAGE;
392
Simon Glass550a9e72020-07-28 19:41:14 -0600393 if (!(flag & CMD_FLAG_REPEAT)) {
Simon Glassbdded202020-06-02 19:26:49 -0600394 /*
395 * Check for a size specification.
396 * Defaults to long if no or incorrect specification.
397 */
398 size = cmd_get_data_size(argv[0], 4);
Simon Glass7526dee2020-11-01 14:15:36 -0700399 if (size < 0 && size != CMD_DATA_SIZE_STR)
Simon Glassbdded202020-06-02 19:26:49 -0600400 return 1;
401
Simon Glass550a9e72020-07-28 19:41:14 -0600402 argc--;
403 argv++;
Simon Glassbdded202020-06-02 19:26:49 -0600404 while (argc && *argv[0] == '-') {
405 int ch = argv[0][1];
406
407 if (ch == 'q')
408 quiet = true;
409 else if (ch == 'l' && isxdigit(argv[0][2]))
Simon Glass7e5f4602021-07-24 09:03:29 -0600410 limit = hextoul(argv[0] + 2, NULL);
Simon Glassbdded202020-06-02 19:26:49 -0600411 else
412 return CMD_RET_USAGE;
Simon Glass550a9e72020-07-28 19:41:14 -0600413 argc--;
414 argv++;
Simon Glassbdded202020-06-02 19:26:49 -0600415 }
416
417 /* Address is specified since argc > 1 */
Simon Glass7e5f4602021-07-24 09:03:29 -0600418 addr = hextoul(argv[0], NULL);
Simon Glassbdded202020-06-02 19:26:49 -0600419 addr += base_address;
420
421 /* Length is the number of objects, not number of bytes */
Simon Glass7e5f4602021-07-24 09:03:29 -0600422 length = hextoul(argv[1], NULL);
Simon Glassbdded202020-06-02 19:26:49 -0600423
424 /* Read the bytes to search for */
425 end = search_buf + sizeof(search_buf);
426 for (i = 2, ptr = search_buf; i < argc && ptr < end; i++) {
Simon Glass550a9e72020-07-28 19:41:14 -0600427 if (MEM_SUPPORT_64BIT_DATA && size == 8) {
Simon Glassbdded202020-06-02 19:26:49 -0600428 u64 val = simple_strtoull(argv[i], NULL, 16);
429
430 *(u64 *)ptr = val;
431 } else if (size == -2) { /* string */
432 int len = min(strlen(argv[i]),
433 (size_t)(end - ptr));
434
435 memcpy(ptr, argv[i], len);
436 ptr += len;
437 continue;
438 } else {
Simon Glass7e5f4602021-07-24 09:03:29 -0600439 u32 val = hextoul(argv[i], NULL);
Simon Glassbdded202020-06-02 19:26:49 -0600440
441 switch (size) {
442 case 1:
443 *ptr = val;
444 break;
445 case 2:
446 *(u16 *)ptr = val;
447 break;
448 case 4:
449 *(u32 *)ptr = val;
450 break;
451 }
452 }
453 ptr += size;
454 }
455 search_len = ptr - search_buf;
456 }
457
458 /* Do the search */
459 if (size == -2)
460 size = 1;
461 bytes = size * length;
462 buf = map_sysmem(addr, bytes);
463 last_pos = 0;
464 last_addr = 0;
465 count = 0;
Simon Glass550a9e72020-07-28 19:41:14 -0600466 for (offset = 0;
467 offset < bytes && offset <= bytes - search_len && count < limit;
Simon Glassbdded202020-06-02 19:26:49 -0600468 offset += size) {
469 void *ptr = buf + offset;
470
471 if (!memcmp(ptr, search_buf, search_len)) {
472 uint align = (addr + offset) & 0xf;
473 ulong match = addr + offset;
474
475 if (!count || (last_addr & ~0xf) != (match & ~0xf)) {
476 if (!quiet) {
477 if (count)
478 printf("--\n");
479 print_buffer(match - align, ptr - align,
480 size,
481 ALIGN(search_len + align,
482 16) / size, 0);
483 }
484 last_addr = match;
485 last_pos = offset / size;
486 }
487 count++;
488 }
489 }
490 if (!quiet) {
491 printf("%d match%s", count, count == 1 ? "" : "es");
492 if (count == limit)
493 printf(" (repeat command to check for more)");
494 printf("\n");
495 }
496 env_set_hex("memmatches", count);
497 env_set_hex("memaddr", last_addr);
498 env_set_hex("mempos", last_pos);
499
500 unmap_sysmem(buf);
501
Simon Glass550a9e72020-07-28 19:41:14 -0600502 used_len = offset / size;
503 dp_last_addr = addr + used_len;
Simon Glassbdded202020-06-02 19:26:49 -0600504 dp_last_size = size;
Simon Glass550a9e72020-07-28 19:41:14 -0600505 dp_last_ms_length = length < used_len ? 0 : length - used_len;
Simon Glassbdded202020-06-02 19:26:49 -0600506
507 return count ? 0 : CMD_RET_FAILURE;
508}
509#endif
510
Simon Glass09140112020-05-10 11:40:03 -0600511static int do_mem_base(struct cmd_tbl *cmdtp, int flag, int argc,
512 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000513{
514 if (argc > 1) {
515 /* Set new base address.
516 */
Simon Glass7e5f4602021-07-24 09:03:29 -0600517 base_address = hextoul(argv[1], NULL);
wdenk38635852002-08-27 05:55:31 +0000518 }
519 /* Print the current base address.
520 */
521 printf("Base Address: 0x%08lx\n", base_address);
522 return 0;
523}
524
Simon Glass09140112020-05-10 11:40:03 -0600525static int do_mem_loop(struct cmd_tbl *cmdtp, int flag, int argc,
526 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000527{
Simon Glass0628ab82013-02-24 17:33:15 +0000528 ulong addr, length, i, bytes;
wdenk27b207f2003-07-24 23:38:38 +0000529 int size;
Simon Glass46809762020-06-02 19:26:46 -0600530 volatile ulong *llp; /* 64-bit if SUPPORT_64BIT_DATA */
York Sun76698b42014-02-12 15:55:35 -0800531 volatile u32 *longp;
532 volatile u16 *shortp;
533 volatile u8 *cp;
Simon Glass0628ab82013-02-24 17:33:15 +0000534 const void *buf;
wdenk38635852002-08-27 05:55:31 +0000535
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200536 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000537 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000538
Robert P. J. Day85de63e2013-02-03 02:29:54 +0000539 /*
540 * Check for a size specification.
wdenk38635852002-08-27 05:55:31 +0000541 * Defaults to long if no or incorrect specification.
542 */
wdenk27b207f2003-07-24 23:38:38 +0000543 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
544 return 1;
wdenk38635852002-08-27 05:55:31 +0000545
546 /* Address is always specified.
547 */
Simon Glass7e5f4602021-07-24 09:03:29 -0600548 addr = hextoul(argv[1], NULL);
wdenk38635852002-08-27 05:55:31 +0000549
550 /* Length is the number of objects, not number of bytes.
551 */
Simon Glass7e5f4602021-07-24 09:03:29 -0600552 length = hextoul(argv[2], NULL);
wdenk38635852002-08-27 05:55:31 +0000553
Simon Glass0628ab82013-02-24 17:33:15 +0000554 bytes = size * length;
555 buf = map_sysmem(addr, bytes);
556
wdenk38635852002-08-27 05:55:31 +0000557 /* We want to optimize the loops to run as fast as possible.
558 * If we have only one object, just run infinite loops.
559 */
560 if (length == 1) {
Simon Glass46809762020-06-02 19:26:46 -0600561 if (SUPPORT_64BIT_DATA && size == 8) {
562 llp = (ulong *)buf;
York Sun4d1fd7f2014-02-26 17:03:19 -0800563 for (;;)
564 i = *llp;
565 }
wdenk38635852002-08-27 05:55:31 +0000566 if (size == 4) {
York Sun76698b42014-02-12 15:55:35 -0800567 longp = (u32 *)buf;
wdenk38635852002-08-27 05:55:31 +0000568 for (;;)
569 i = *longp;
570 }
571 if (size == 2) {
York Sun76698b42014-02-12 15:55:35 -0800572 shortp = (u16 *)buf;
wdenk38635852002-08-27 05:55:31 +0000573 for (;;)
574 i = *shortp;
575 }
York Sun76698b42014-02-12 15:55:35 -0800576 cp = (u8 *)buf;
wdenk38635852002-08-27 05:55:31 +0000577 for (;;)
578 i = *cp;
579 }
580
Simon Glass46809762020-06-02 19:26:46 -0600581 if (SUPPORT_64BIT_DATA && size == 8) {
York Sun4d1fd7f2014-02-26 17:03:19 -0800582 for (;;) {
Simon Glass46809762020-06-02 19:26:46 -0600583 llp = (ulong *)buf;
York Sun4d1fd7f2014-02-26 17:03:19 -0800584 i = length;
585 while (i-- > 0)
586 *llp++;
587 }
588 }
wdenk38635852002-08-27 05:55:31 +0000589 if (size == 4) {
590 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800591 longp = (u32 *)buf;
wdenk38635852002-08-27 05:55:31 +0000592 i = length;
593 while (i-- > 0)
Marek Vasutf3b3c3d2011-09-26 02:26:06 +0200594 *longp++;
wdenk38635852002-08-27 05:55:31 +0000595 }
596 }
597 if (size == 2) {
598 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800599 shortp = (u16 *)buf;
wdenk38635852002-08-27 05:55:31 +0000600 i = length;
601 while (i-- > 0)
Marek Vasutf3b3c3d2011-09-26 02:26:06 +0200602 *shortp++;
wdenk38635852002-08-27 05:55:31 +0000603 }
604 }
605 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800606 cp = (u8 *)buf;
wdenk38635852002-08-27 05:55:31 +0000607 i = length;
608 while (i-- > 0)
Marek Vasutf3b3c3d2011-09-26 02:26:06 +0200609 *cp++;
wdenk38635852002-08-27 05:55:31 +0000610 }
Simon Glass0628ab82013-02-24 17:33:15 +0000611 unmap_sysmem(buf);
Simon Glass92765f422013-06-11 11:14:35 -0700612
613 return 0;
wdenk38635852002-08-27 05:55:31 +0000614}
615
wdenk56523f12004-07-11 17:40:54 +0000616#ifdef CONFIG_LOOPW
Simon Glass09140112020-05-10 11:40:03 -0600617static int do_mem_loopw(struct cmd_tbl *cmdtp, int flag, int argc,
618 char *const argv[])
wdenk56523f12004-07-11 17:40:54 +0000619{
York Sun4d1fd7f2014-02-26 17:03:19 -0800620 ulong addr, length, i, bytes;
wdenk56523f12004-07-11 17:40:54 +0000621 int size;
Simon Glass46809762020-06-02 19:26:46 -0600622 volatile ulong *llp; /* 64-bit if SUPPORT_64BIT_DATA */
623 ulong data; /* 64-bit if SUPPORT_64BIT_DATA */
York Sun76698b42014-02-12 15:55:35 -0800624 volatile u32 *longp;
625 volatile u16 *shortp;
626 volatile u8 *cp;
Simon Glass0628ab82013-02-24 17:33:15 +0000627 void *buf;
wdenk81050922004-07-11 20:04:51 +0000628
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200629 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000630 return CMD_RET_USAGE;
wdenk56523f12004-07-11 17:40:54 +0000631
Robert P. J. Day85de63e2013-02-03 02:29:54 +0000632 /*
633 * Check for a size specification.
wdenk56523f12004-07-11 17:40:54 +0000634 * Defaults to long if no or incorrect specification.
635 */
636 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
637 return 1;
638
639 /* Address is always specified.
640 */
Simon Glass7e5f4602021-07-24 09:03:29 -0600641 addr = hextoul(argv[1], NULL);
wdenk56523f12004-07-11 17:40:54 +0000642
643 /* Length is the number of objects, not number of bytes.
644 */
Simon Glass7e5f4602021-07-24 09:03:29 -0600645 length = hextoul(argv[2], NULL);
wdenk56523f12004-07-11 17:40:54 +0000646
647 /* data to write */
Simon Glass46809762020-06-02 19:26:46 -0600648 if (SUPPORT_64BIT_DATA)
649 data = simple_strtoull(argv[3], NULL, 16);
650 else
Simon Glass7e5f4602021-07-24 09:03:29 -0600651 data = hextoul(argv[3], NULL);
wdenk81050922004-07-11 20:04:51 +0000652
Simon Glass0628ab82013-02-24 17:33:15 +0000653 bytes = size * length;
654 buf = map_sysmem(addr, bytes);
655
wdenk56523f12004-07-11 17:40:54 +0000656 /* We want to optimize the loops to run as fast as possible.
657 * If we have only one object, just run infinite loops.
658 */
659 if (length == 1) {
Simon Glass46809762020-06-02 19:26:46 -0600660 if (SUPPORT_64BIT_DATA && size == 8) {
661 llp = (ulong *)buf;
York Sun4d1fd7f2014-02-26 17:03:19 -0800662 for (;;)
663 *llp = data;
664 }
wdenk56523f12004-07-11 17:40:54 +0000665 if (size == 4) {
York Sun76698b42014-02-12 15:55:35 -0800666 longp = (u32 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000667 for (;;)
668 *longp = data;
York Sun4d1fd7f2014-02-26 17:03:19 -0800669 }
wdenk56523f12004-07-11 17:40:54 +0000670 if (size == 2) {
York Sun76698b42014-02-12 15:55:35 -0800671 shortp = (u16 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000672 for (;;)
673 *shortp = data;
674 }
York Sun76698b42014-02-12 15:55:35 -0800675 cp = (u8 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000676 for (;;)
677 *cp = data;
678 }
679
Simon Glass46809762020-06-02 19:26:46 -0600680 if (SUPPORT_64BIT_DATA && size == 8) {
York Sun4d1fd7f2014-02-26 17:03:19 -0800681 for (;;) {
Simon Glass46809762020-06-02 19:26:46 -0600682 llp = (ulong *)buf;
York Sun4d1fd7f2014-02-26 17:03:19 -0800683 i = length;
684 while (i-- > 0)
685 *llp++ = data;
686 }
687 }
wdenk56523f12004-07-11 17:40:54 +0000688 if (size == 4) {
689 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800690 longp = (u32 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000691 i = length;
692 while (i-- > 0)
693 *longp++ = data;
694 }
695 }
696 if (size == 2) {
697 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800698 shortp = (u16 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000699 i = length;
700 while (i-- > 0)
701 *shortp++ = data;
702 }
703 }
704 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800705 cp = (u8 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000706 i = length;
707 while (i-- > 0)
708 *cp++ = data;
709 }
710}
711#endif /* CONFIG_LOOPW */
712
Tom Rini68149e92013-03-12 10:07:19 -0400713#ifdef CONFIG_CMD_MEMTEST
Simon Glass5512d5b2013-02-28 17:47:14 +0000714static ulong mem_test_alt(vu_long *buf, ulong start_addr, ulong end_addr,
715 vu_long *dummy)
wdenk38635852002-08-27 05:55:31 +0000716{
Simon Glassc9638f52013-02-24 17:33:16 +0000717 vu_long *addr;
Simon Glassc9638f52013-02-24 17:33:16 +0000718 ulong errs = 0;
719 ulong val, readback;
720 int j;
Simon Glassc9638f52013-02-24 17:33:16 +0000721 vu_long offset;
722 vu_long test_offset;
723 vu_long pattern;
724 vu_long temp;
725 vu_long anti_pattern;
726 vu_long num_words;
wdenk38635852002-08-27 05:55:31 +0000727 static const ulong bitpattern[] = {
728 0x00000001, /* single bit */
729 0x00000003, /* two adjacent bits */
730 0x00000007, /* three adjacent bits */
731 0x0000000F, /* four adjacent bits */
732 0x00000005, /* two non-adjacent bits */
733 0x00000015, /* three non-adjacent bits */
734 0x00000055, /* four non-adjacent bits */
735 0xaaaaaaaa, /* alternating 1/0 */
736 };
wdenk38635852002-08-27 05:55:31 +0000737
Simon Glass5512d5b2013-02-28 17:47:14 +0000738 num_words = (end_addr - start_addr) / sizeof(vu_long);
Simon Glass8c86bbe2013-02-24 17:33:20 +0000739
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000740 /*
741 * Data line test: write a pattern to the first
742 * location, write the 1's complement to a 'parking'
743 * address (changes the state of the data bus so a
744 * floating bus doesn't give a false OK), and then
745 * read the value back. Note that we read it back
746 * into a variable because the next time we read it,
747 * it might be right (been there, tough to explain to
748 * the quality guys why it prints a failure when the
749 * "is" and "should be" are obviously the same in the
750 * error message).
751 *
752 * Rather than exhaustively testing, we test some
753 * patterns by shifting '1' bits through a field of
754 * '0's and '0' bits through a field of '1's (i.e.
755 * pattern and ~pattern).
756 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000757 addr = buf;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000758 for (j = 0; j < sizeof(bitpattern) / sizeof(bitpattern[0]); j++) {
759 val = bitpattern[j];
760 for (; val != 0; val <<= 1) {
Simon Glass5512d5b2013-02-28 17:47:14 +0000761 *addr = val;
Simon Glassc9638f52013-02-24 17:33:16 +0000762 *dummy = ~val; /* clear the test data off the bus */
wdenk38635852002-08-27 05:55:31 +0000763 readback = *addr;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000764 if (readback != val) {
Simon Glassc9638f52013-02-24 17:33:16 +0000765 printf("FAILURE (data line): "
766 "expected %08lx, actual %08lx\n",
767 val, readback);
768 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000769 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000770 return -1;
wdenk38635852002-08-27 05:55:31 +0000771 }
772 *addr = ~val;
773 *dummy = val;
774 readback = *addr;
Simon Glassc9638f52013-02-24 17:33:16 +0000775 if (readback != ~val) {
776 printf("FAILURE (data line): "
777 "Is %08lx, should be %08lx\n",
778 readback, ~val);
779 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000780 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000781 return -1;
wdenk38635852002-08-27 05:55:31 +0000782 }
wdenk38635852002-08-27 05:55:31 +0000783 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000784 }
wdenk38635852002-08-27 05:55:31 +0000785
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000786 /*
787 * Based on code whose Original Author and Copyright
788 * information follows: Copyright (c) 1998 by Michael
789 * Barr. This software is placed into the public
790 * domain and may be used for any purpose. However,
791 * this notice must not be changed or removed and no
792 * warranty is either expressed or implied by its
793 * publication or distribution.
794 */
wdenk38635852002-08-27 05:55:31 +0000795
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000796 /*
797 * Address line test
wdenk38635852002-08-27 05:55:31 +0000798
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000799 * Description: Test the address bus wiring in a
800 * memory region by performing a walking
801 * 1's test on the relevant bits of the
802 * address and checking for aliasing.
803 * This test will find single-bit
804 * address failures such as stuck-high,
805 * stuck-low, and shorted pins. The base
806 * address and size of the region are
807 * selected by the caller.
wdenk38635852002-08-27 05:55:31 +0000808
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000809 * Notes: For best results, the selected base
810 * address should have enough LSB 0's to
811 * guarantee single address bit changes.
812 * For example, to test a 64-Kbyte
813 * region, select a base address on a
814 * 64-Kbyte boundary. Also, select the
815 * region size as a power-of-two if at
816 * all possible.
817 *
818 * Returns: 0 if the test succeeds, 1 if the test fails.
819 */
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000820 pattern = (vu_long) 0xaaaaaaaa;
821 anti_pattern = (vu_long) 0x55555555;
wdenk38635852002-08-27 05:55:31 +0000822
Simon Glass5512d5b2013-02-28 17:47:14 +0000823 debug("%s:%d: length = 0x%.8lx\n", __func__, __LINE__, num_words);
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000824 /*
825 * Write the default pattern at each of the
826 * power-of-two offsets.
827 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000828 for (offset = 1; offset < num_words; offset <<= 1)
829 addr[offset] = pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000830
831 /*
832 * Check for address bits stuck high.
833 */
834 test_offset = 0;
Simon Glass5512d5b2013-02-28 17:47:14 +0000835 addr[test_offset] = anti_pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000836
Simon Glass5512d5b2013-02-28 17:47:14 +0000837 for (offset = 1; offset < num_words; offset <<= 1) {
838 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000839 if (temp != pattern) {
Simon Glassc9638f52013-02-24 17:33:16 +0000840 printf("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000841 " expected 0x%.8lx, actual 0x%.8lx\n",
David Feng102c0512014-02-12 16:10:08 +0800842 start_addr + offset*sizeof(vu_long),
843 pattern, temp);
Paul Gortmaker87b22b72009-10-02 18:18:33 -0400844 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000845 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000846 return -1;
wdenk38635852002-08-27 05:55:31 +0000847 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000848 }
Simon Glass5512d5b2013-02-28 17:47:14 +0000849 addr[test_offset] = pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000850 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000851
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000852 /*
853 * Check for addr bits stuck low or shorted.
854 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000855 for (test_offset = 1; test_offset < num_words; test_offset <<= 1) {
856 addr[test_offset] = anti_pattern;
wdenk38635852002-08-27 05:55:31 +0000857
Simon Glass5512d5b2013-02-28 17:47:14 +0000858 for (offset = 1; offset < num_words; offset <<= 1) {
859 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000860 if ((temp != pattern) && (offset != test_offset)) {
861 printf("\nFAILURE: Address bit stuck low or"
862 " shorted @ 0x%.8lx: expected 0x%.8lx,"
863 " actual 0x%.8lx\n",
David Feng102c0512014-02-12 16:10:08 +0800864 start_addr + offset*sizeof(vu_long),
865 pattern, temp);
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000866 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000867 if (ctrlc())
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000868 return -1;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000869 }
wdenk38635852002-08-27 05:55:31 +0000870 }
Simon Glass5512d5b2013-02-28 17:47:14 +0000871 addr[test_offset] = pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000872 }
wdenk38635852002-08-27 05:55:31 +0000873
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000874 /*
875 * Description: Test the integrity of a physical
876 * memory device by performing an
877 * increment/decrement test over the
878 * entire region. In the process every
879 * storage bit in the device is tested
880 * as a zero and a one. The base address
881 * and the size of the region are
882 * selected by the caller.
883 *
884 * Returns: 0 if the test succeeds, 1 if the test fails.
885 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000886 num_words++;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000887
888 /*
889 * Fill memory with a known pattern.
890 */
891 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
892 WATCHDOG_RESET();
Simon Glass5512d5b2013-02-28 17:47:14 +0000893 addr[offset] = pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000894 }
895
896 /*
897 * Check each location and invert it for the second pass.
898 */
899 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
900 WATCHDOG_RESET();
Simon Glass5512d5b2013-02-28 17:47:14 +0000901 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000902 if (temp != pattern) {
Simon Glassc9638f52013-02-24 17:33:16 +0000903 printf("\nFAILURE (read/write) @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000904 " expected 0x%.8lx, actual 0x%.8lx)\n",
David Feng102c0512014-02-12 16:10:08 +0800905 start_addr + offset*sizeof(vu_long),
906 pattern, temp);
Paul Gortmaker87b22b72009-10-02 18:18:33 -0400907 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000908 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000909 return -1;
wdenk38635852002-08-27 05:55:31 +0000910 }
911
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000912 anti_pattern = ~pattern;
Simon Glass5512d5b2013-02-28 17:47:14 +0000913 addr[offset] = anti_pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000914 }
915
916 /*
917 * Check each location for the inverted pattern and zero it.
918 */
919 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
920 WATCHDOG_RESET();
921 anti_pattern = ~pattern;
Simon Glass5512d5b2013-02-28 17:47:14 +0000922 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000923 if (temp != anti_pattern) {
Simon Glassc9638f52013-02-24 17:33:16 +0000924 printf("\nFAILURE (read/write): @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000925 " expected 0x%.8lx, actual 0x%.8lx)\n",
David Feng102c0512014-02-12 16:10:08 +0800926 start_addr + offset*sizeof(vu_long),
927 anti_pattern, temp);
Paul Gortmaker87b22b72009-10-02 18:18:33 -0400928 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000929 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000930 return -1;
wdenk38635852002-08-27 05:55:31 +0000931 }
Simon Glass5512d5b2013-02-28 17:47:14 +0000932 addr[offset] = 0;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000933 }
Simon Glass51209b12013-02-24 17:33:17 +0000934
Rasmus Villemoesfea67302016-01-07 11:36:04 +0100935 return errs;
Simon Glassc9638f52013-02-24 17:33:16 +0000936}
wdenk38635852002-08-27 05:55:31 +0000937
Stefan Roese8e434cb2020-03-05 07:21:32 +0100938static int compare_regions(volatile unsigned long *bufa,
939 volatile unsigned long *bufb, size_t count)
940{
941 volatile unsigned long *p1 = bufa;
942 volatile unsigned long *p2 = bufb;
943 int errs = 0;
944 size_t i;
945
946 for (i = 0; i < count; i++, p1++, p2++) {
947 if (*p1 != *p2) {
948 printf("FAILURE: 0x%08lx != 0x%08lx (delta=0x%08lx -> bit %ld) at offset 0x%08lx\n",
949 (unsigned long)*p1, (unsigned long)*p2,
950 *p1 ^ *p2, __ffs(*p1 ^ *p2),
951 (unsigned long)(i * sizeof(unsigned long)));
952 errs++;
953 }
954 }
955
956 return errs;
957}
958
959static ulong test_bitflip_comparison(volatile unsigned long *bufa,
960 volatile unsigned long *bufb, size_t count)
961{
962 volatile unsigned long *p1 = bufa;
963 volatile unsigned long *p2 = bufb;
964 unsigned int j, k;
965 unsigned long q;
966 size_t i;
967 int max;
968 int errs = 0;
969
970 max = sizeof(unsigned long) * 8;
971 for (k = 0; k < max; k++) {
972 q = 0x00000001L << k;
973 for (j = 0; j < 8; j++) {
974 WATCHDOG_RESET();
975 q = ~q;
976 p1 = (volatile unsigned long *)bufa;
977 p2 = (volatile unsigned long *)bufb;
978 for (i = 0; i < count; i++)
979 *p1++ = *p2++ = (i % 2) == 0 ? q : ~q;
980
981 errs += compare_regions(bufa, bufb, count);
982 }
983
984 if (ctrlc())
985 return -1UL;
986 }
987
988 return errs;
989}
990
Ralph Siemsen9989fb12020-09-09 12:10:00 -0400991static ulong mem_test_bitflip(vu_long *buf, ulong start, ulong end)
992{
993 /*
994 * Split the specified range into two halves.
995 * Note that mtest range is inclusive of start,end.
996 * Bitflip test instead uses a count (of 32-bit words).
997 */
998 ulong half_size = (end - start + 1) / 2 / sizeof(unsigned long);
999
1000 return test_bitflip_comparison(buf, buf + half_size, half_size);
1001}
1002
Simon Glass5512d5b2013-02-28 17:47:14 +00001003static ulong mem_test_quick(vu_long *buf, ulong start_addr, ulong end_addr,
1004 vu_long pattern, int iteration)
Simon Glassc9638f52013-02-24 17:33:16 +00001005{
Simon Glass5512d5b2013-02-28 17:47:14 +00001006 vu_long *end;
Simon Glassc9638f52013-02-24 17:33:16 +00001007 vu_long *addr;
Simon Glassc9638f52013-02-24 17:33:16 +00001008 ulong errs = 0;
Simon Glass5512d5b2013-02-28 17:47:14 +00001009 ulong incr, length;
Simon Glassc9638f52013-02-24 17:33:16 +00001010 ulong val, readback;
1011
Simon Glass51209b12013-02-24 17:33:17 +00001012 /* Alternate the pattern */
wdenk38635852002-08-27 05:55:31 +00001013 incr = 1;
Simon Glass51209b12013-02-24 17:33:17 +00001014 if (iteration & 1) {
1015 incr = -incr;
1016 /*
1017 * Flip the pattern each time to make lots of zeros and
1018 * then, the next time, lots of ones. We decrement
1019 * the "negative" patterns and increment the "positive"
1020 * patterns to preserve this feature.
1021 */
1022 if (pattern & 0x80000000)
1023 pattern = -pattern; /* complement & increment */
1024 else
1025 pattern = ~pattern;
1026 }
Simon Glass5512d5b2013-02-28 17:47:14 +00001027 length = (end_addr - start_addr) / sizeof(ulong);
1028 end = buf + length;
Simon Glass7ecbd4d2013-02-24 17:33:18 +00001029 printf("\rPattern %08lX Writing..."
1030 "%12s"
1031 "\b\b\b\b\b\b\b\b\b\b",
1032 pattern, "");
wdenk38635852002-08-27 05:55:31 +00001033
Simon Glass5512d5b2013-02-28 17:47:14 +00001034 for (addr = buf, val = pattern; addr < end; addr++) {
Simon Glass7ecbd4d2013-02-24 17:33:18 +00001035 WATCHDOG_RESET();
1036 *addr = val;
1037 val += incr;
1038 }
wdenk38635852002-08-27 05:55:31 +00001039
Simon Glass7ecbd4d2013-02-24 17:33:18 +00001040 puts("Reading...");
wdenk38635852002-08-27 05:55:31 +00001041
Simon Glass5512d5b2013-02-28 17:47:14 +00001042 for (addr = buf, val = pattern; addr < end; addr++) {
Simon Glass7ecbd4d2013-02-24 17:33:18 +00001043 WATCHDOG_RESET();
1044 readback = *addr;
1045 if (readback != val) {
Simon Glass5512d5b2013-02-28 17:47:14 +00001046 ulong offset = addr - buf;
1047
Simon Glass7ecbd4d2013-02-24 17:33:18 +00001048 printf("\nMem error @ 0x%08X: "
1049 "found %08lX, expected %08lX\n",
David Feng102c0512014-02-12 16:10:08 +08001050 (uint)(uintptr_t)(start_addr + offset*sizeof(vu_long)),
Simon Glass5512d5b2013-02-28 17:47:14 +00001051 readback, val);
Simon Glass7ecbd4d2013-02-24 17:33:18 +00001052 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +00001053 if (ctrlc())
Simon Glass7ecbd4d2013-02-24 17:33:18 +00001054 return -1;
wdenk38635852002-08-27 05:55:31 +00001055 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +00001056 val += incr;
1057 }
wdenk38635852002-08-27 05:55:31 +00001058
Rasmus Villemoesfea67302016-01-07 11:36:04 +01001059 return errs;
Simon Glassc9638f52013-02-24 17:33:16 +00001060}
1061
1062/*
1063 * Perform a memory test. A more complete alternative test can be
1064 * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
1065 * interrupted by ctrl-c or by a failure of one of the sub-tests.
1066 */
Simon Glass09140112020-05-10 11:40:03 -06001067static int do_mem_mtest(struct cmd_tbl *cmdtp, int flag, int argc,
1068 char *const argv[])
Simon Glassc9638f52013-02-24 17:33:16 +00001069{
Simon Glass8c86bbe2013-02-24 17:33:20 +00001070 ulong start, end;
Michal Simeke519f032020-05-04 13:54:40 +02001071 vu_long scratch_space;
1072 vu_long *buf, *dummy = &scratch_space;
Tom Riniadcc5702015-04-07 09:38:54 -04001073 ulong iteration_limit = 0;
Stefan Roesea8c708e2020-03-05 07:21:29 +01001074 ulong count = 0;
Simon Glass51209b12013-02-24 17:33:17 +00001075 ulong errs = 0; /* number of errors, or -1 if interrupted */
Pavel Macheke37f1eb2015-04-01 13:50:41 +02001076 ulong pattern = 0;
Simon Glass51209b12013-02-24 17:33:17 +00001077 int iteration;
Simon Glassc9638f52013-02-24 17:33:16 +00001078
Pavel Macheke37f1eb2015-04-01 13:50:41 +02001079 start = CONFIG_SYS_MEMTEST_START;
1080 end = CONFIG_SYS_MEMTEST_END;
1081
Simon Glassc9638f52013-02-24 17:33:16 +00001082 if (argc > 1)
Pavel Macheke37f1eb2015-04-01 13:50:41 +02001083 if (strict_strtoul(argv[1], 16, &start) < 0)
1084 return CMD_RET_USAGE;
Simon Glassc9638f52013-02-24 17:33:16 +00001085
1086 if (argc > 2)
Pavel Macheke37f1eb2015-04-01 13:50:41 +02001087 if (strict_strtoul(argv[2], 16, &end) < 0)
1088 return CMD_RET_USAGE;
Simon Glassc9638f52013-02-24 17:33:16 +00001089
1090 if (argc > 3)
Pavel Macheke37f1eb2015-04-01 13:50:41 +02001091 if (strict_strtoul(argv[3], 16, &pattern) < 0)
1092 return CMD_RET_USAGE;
Simon Glassc9638f52013-02-24 17:33:16 +00001093
1094 if (argc > 4)
Pavel Macheke37f1eb2015-04-01 13:50:41 +02001095 if (strict_strtoul(argv[4], 16, &iteration_limit) < 0)
1096 return CMD_RET_USAGE;
1097
1098 if (end < start) {
1099 printf("Refusing to do empty test\n");
1100 return -1;
1101 }
Simon Glassc9638f52013-02-24 17:33:16 +00001102
Michal Simekdfe461d2016-02-24 08:36:02 +01001103 printf("Testing %08lx ... %08lx:\n", start, end);
Simon Glass8c86bbe2013-02-24 17:33:20 +00001104 debug("%s:%d: start %#08lx end %#08lx\n", __func__, __LINE__,
1105 start, end);
Simon Glass51209b12013-02-24 17:33:17 +00001106
Simon Glass5512d5b2013-02-28 17:47:14 +00001107 buf = map_sysmem(start, end - start);
Simon Glass51209b12013-02-24 17:33:17 +00001108 for (iteration = 0;
1109 !iteration_limit || iteration < iteration_limit;
1110 iteration++) {
1111 if (ctrlc()) {
Simon Glass51209b12013-02-24 17:33:17 +00001112 errs = -1UL;
1113 break;
1114 }
1115
1116 printf("Iteration: %6d\r", iteration + 1);
1117 debug("\n");
Stefan Roesef14bfa72020-03-05 07:21:31 +01001118 if (IS_ENABLED(CONFIG_SYS_ALT_MEMTEST)) {
Simon Glass5512d5b2013-02-28 17:47:14 +00001119 errs = mem_test_alt(buf, start, end, dummy);
Stefan Roese8e434cb2020-03-05 07:21:32 +01001120 if (errs == -1UL)
1121 break;
Ralph Siemsen9989fb12020-09-09 12:10:00 -04001122 if (IS_ENABLED(CONFIG_SYS_ALT_MEMTEST_BITFLIP)) {
1123 count += errs;
1124 errs = mem_test_bitflip(buf, start, end);
1125 }
Simon Glass5512d5b2013-02-28 17:47:14 +00001126 } else {
1127 errs = mem_test_quick(buf, start, end, pattern,
1128 iteration);
1129 }
1130 if (errs == -1UL)
1131 break;
Stefan Roesea8c708e2020-03-05 07:21:29 +01001132 count += errs;
Simon Glass5512d5b2013-02-28 17:47:14 +00001133 }
1134
Stefan Roese54de2442020-03-05 07:21:30 +01001135 unmap_sysmem((void *)buf);
Simon Glass51209b12013-02-24 17:33:17 +00001136
1137 if (errs == -1UL) {
Simon Glassc44d4382013-02-24 17:33:19 +00001138 /* Memory test was aborted - write a newline to finish off */
1139 putc('\n');
Simon Glass51209b12013-02-24 17:33:17 +00001140 }
Stefan Roesea8c708e2020-03-05 07:21:29 +01001141 printf("Tested %d iteration(s) with %lu errors.\n", iteration, count);
Simon Glassc9638f52013-02-24 17:33:16 +00001142
Stefan Roesea8c708e2020-03-05 07:21:29 +01001143 return errs != 0;
wdenk38635852002-08-27 05:55:31 +00001144}
Wolfgang Denka2681702013-03-08 10:51:32 +00001145#endif /* CONFIG_CMD_MEMTEST */
wdenk38635852002-08-27 05:55:31 +00001146
1147/* Modify memory.
1148 *
1149 * Syntax:
York Sun4d1fd7f2014-02-26 17:03:19 -08001150 * mm{.b, .w, .l, .q} {addr}
wdenk38635852002-08-27 05:55:31 +00001151 */
1152static int
Simon Glass09140112020-05-10 11:40:03 -06001153mod_mem(struct cmd_tbl *cmdtp, int incrflag, int flag, int argc,
1154 char *const argv[])
wdenk38635852002-08-27 05:55:31 +00001155{
York Sun4d1fd7f2014-02-26 17:03:19 -08001156 ulong addr;
Simon Glass46809762020-06-02 19:26:46 -06001157 ulong i; /* 64-bit if SUPPORT_64BIT_DATA */
wdenk27b207f2003-07-24 23:38:38 +00001158 int nbytes, size;
Simon Glass0628ab82013-02-24 17:33:15 +00001159 void *ptr = NULL;
wdenk38635852002-08-27 05:55:31 +00001160
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001161 if (argc != 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001162 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +00001163
Simon Glassb26440f2014-04-10 20:01:31 -06001164 bootretry_reset_cmd_timeout(); /* got a good command to get here */
wdenk38635852002-08-27 05:55:31 +00001165 /* We use the last specified parameters, unless new ones are
1166 * entered.
1167 */
1168 addr = mm_last_addr;
1169 size = mm_last_size;
1170
1171 if ((flag & CMD_FLAG_REPEAT) == 0) {
1172 /* New command specified. Check for a size specification.
1173 * Defaults to long if no or incorrect specification.
1174 */
wdenk27b207f2003-07-24 23:38:38 +00001175 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
1176 return 1;
wdenk38635852002-08-27 05:55:31 +00001177
1178 /* Address is specified since argc > 1
1179 */
Simon Glass7e5f4602021-07-24 09:03:29 -06001180 addr = hextoul(argv[1], NULL);
wdenk38635852002-08-27 05:55:31 +00001181 addr += base_address;
1182 }
1183
1184 /* Print the address, followed by value. Then accept input for
1185 * the next value. A non-converted value exits.
1186 */
1187 do {
Simon Glass0628ab82013-02-24 17:33:15 +00001188 ptr = map_sysmem(addr, size);
wdenk38635852002-08-27 05:55:31 +00001189 printf("%08lx:", addr);
1190 if (size == 4)
York Sun76698b42014-02-12 15:55:35 -08001191 printf(" %08x", *((u32 *)ptr));
Simon Glass46809762020-06-02 19:26:46 -06001192 else if (SUPPORT_64BIT_DATA && size == 8)
1193 printf(" %0lx", *((ulong *)ptr));
wdenk38635852002-08-27 05:55:31 +00001194 else if (size == 2)
York Sun76698b42014-02-12 15:55:35 -08001195 printf(" %04x", *((u16 *)ptr));
wdenk38635852002-08-27 05:55:31 +00001196 else
York Sun76698b42014-02-12 15:55:35 -08001197 printf(" %02x", *((u8 *)ptr));
wdenk38635852002-08-27 05:55:31 +00001198
Simon Glasse1bf8242014-04-10 20:01:27 -06001199 nbytes = cli_readline(" ? ");
wdenk38635852002-08-27 05:55:31 +00001200 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
1201 /* <CR> pressed as only input, don't modify current
1202 * location and move to next. "-" pressed will go back.
1203 */
1204 if (incrflag)
1205 addr += nbytes ? -size : size;
1206 nbytes = 1;
Simon Glassb26440f2014-04-10 20:01:31 -06001207 /* good enough to not time out */
1208 bootretry_reset_cmd_timeout();
wdenk38635852002-08-27 05:55:31 +00001209 }
1210#ifdef CONFIG_BOOT_RETRY_TIME
1211 else if (nbytes == -2) {
1212 break; /* timed out, exit the command */
1213 }
1214#endif
1215 else {
1216 char *endp;
Simon Glass46809762020-06-02 19:26:46 -06001217 if (SUPPORT_64BIT_DATA)
1218 i = simple_strtoull(console_buffer, &endp, 16);
1219 else
Simon Glass7e5f4602021-07-24 09:03:29 -06001220 i = hextoul(console_buffer, &endp);
wdenk38635852002-08-27 05:55:31 +00001221 nbytes = endp - console_buffer;
1222 if (nbytes) {
wdenk38635852002-08-27 05:55:31 +00001223 /* good enough to not time out
1224 */
Simon Glassb26440f2014-04-10 20:01:31 -06001225 bootretry_reset_cmd_timeout();
wdenk38635852002-08-27 05:55:31 +00001226 if (size == 4)
York Sun76698b42014-02-12 15:55:35 -08001227 *((u32 *)ptr) = i;
Simon Glass46809762020-06-02 19:26:46 -06001228 else if (SUPPORT_64BIT_DATA && size == 8)
1229 *((ulong *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001230 else if (size == 2)
York Sun76698b42014-02-12 15:55:35 -08001231 *((u16 *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001232 else
York Sun76698b42014-02-12 15:55:35 -08001233 *((u8 *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001234 if (incrflag)
1235 addr += size;
1236 }
1237 }
1238 } while (nbytes);
Simon Glass0628ab82013-02-24 17:33:15 +00001239 if (ptr)
1240 unmap_sysmem(ptr);
wdenk38635852002-08-27 05:55:31 +00001241
1242 mm_last_addr = addr;
1243 mm_last_size = size;
1244 return 0;
1245}
1246
Mike Frysinger710b9932010-12-21 14:19:51 -05001247#ifdef CONFIG_CMD_CRC32
1248
Simon Glass09140112020-05-10 11:40:03 -06001249static int do_mem_crc(struct cmd_tbl *cmdtp, int flag, int argc,
1250 char *const argv[])
wdenk38635852002-08-27 05:55:31 +00001251{
Simon Glassd20a40d2013-02-24 20:30:22 +00001252 int flags = 0;
1253 int ac;
1254 char * const *av;
wdenk38635852002-08-27 05:55:31 +00001255
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001256 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001257 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +00001258
wdenkc26e4542004-04-18 10:13:26 +00001259 av = argv + 1;
1260 ac = argc - 1;
Daniel Thompson221a9492017-05-19 17:26:58 +01001261#ifdef CONFIG_CRC32_VERIFY
wdenkc26e4542004-04-18 10:13:26 +00001262 if (strcmp(*av, "-v") == 0) {
Joe Hershbergera69bdba2015-05-05 12:23:53 -05001263 flags |= HASH_FLAG_VERIFY | HASH_FLAG_ENV;
wdenkc26e4542004-04-18 10:13:26 +00001264 av++;
1265 ac--;
wdenkc26e4542004-04-18 10:13:26 +00001266 }
Simon Glassd20a40d2013-02-24 20:30:22 +00001267#endif
wdenkc26e4542004-04-18 10:13:26 +00001268
Simon Glassd20a40d2013-02-24 20:30:22 +00001269 return hash_command("crc32", flags, cmdtp, flag, ac, av);
wdenkc26e4542004-04-18 10:13:26 +00001270}
wdenkc26e4542004-04-18 10:13:26 +00001271
Mike Frysinger710b9932010-12-21 14:19:51 -05001272#endif
1273
Jean-Jacques Hiblot803e1a32019-07-02 14:23:26 +02001274#ifdef CONFIG_CMD_RANDOM
Simon Glass09140112020-05-10 11:40:03 -06001275static int do_random(struct cmd_tbl *cmdtp, int flag, int argc,
1276 char *const argv[])
Jean-Jacques Hiblot803e1a32019-07-02 14:23:26 +02001277{
1278 unsigned long addr, len;
1279 unsigned long seed; // NOT INITIALIZED ON PURPOSE
1280 unsigned int *buf, *start;
1281 unsigned char *buf8;
1282 unsigned int i;
1283
Eugeniy Paltsev5f2c4e02020-03-20 19:38:17 +03001284 if (argc < 3 || argc > 4)
1285 return CMD_RET_USAGE;
Jean-Jacques Hiblot803e1a32019-07-02 14:23:26 +02001286
Simon Glass7e5f4602021-07-24 09:03:29 -06001287 len = hextoul(argv[2], NULL);
1288 addr = hextoul(argv[1], NULL);
Jean-Jacques Hiblot803e1a32019-07-02 14:23:26 +02001289
1290 if (argc == 4) {
Simon Glass7e5f4602021-07-24 09:03:29 -06001291 seed = hextoul(argv[3], NULL);
Jean-Jacques Hiblot803e1a32019-07-02 14:23:26 +02001292 if (seed == 0) {
1293 printf("The seed cannot be 0. Using 0xDEADBEEF.\n");
1294 seed = 0xDEADBEEF;
1295 }
1296 } else {
1297 seed = get_timer(0) ^ rand();
1298 }
1299
1300 srand(seed);
1301 start = map_sysmem(addr, len);
1302 buf = start;
1303 for (i = 0; i < (len / 4); i++)
1304 *buf++ = rand();
1305
1306 buf8 = (unsigned char *)buf;
1307 for (i = 0; i < (len % 4); i++)
1308 *buf8++ = rand() & 0xFF;
1309
1310 unmap_sysmem(start);
1311 printf("%lu bytes filled with random data\n", len);
Eugeniy Paltsev5f2c4e02020-03-20 19:38:17 +03001312
1313 return CMD_RET_SUCCESS;
Jean-Jacques Hiblot803e1a32019-07-02 14:23:26 +02001314}
1315#endif
1316
wdenk8bde7f72003-06-27 21:31:46 +00001317/**************************************************/
wdenk0d498392003-07-01 21:06:45 +00001318U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001319 md, 3, 1, do_mem_md,
Peter Tyser2fb26042009-01-27 18:03:12 -06001320 "memory display",
Simon Glass76be8f72020-06-02 19:26:45 -06001321 "[.b, .w, .l" HELP_Q "] address [# of objects]"
wdenk8bde7f72003-06-27 21:31:46 +00001322);
1323
1324
wdenk0d498392003-07-01 21:06:45 +00001325U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001326 mm, 2, 1, do_mem_mm,
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001327 "memory modify (auto-incrementing address)",
Simon Glass76be8f72020-06-02 19:26:45 -06001328 "[.b, .w, .l" HELP_Q "] address"
wdenk8bde7f72003-06-27 21:31:46 +00001329);
1330
1331
wdenk0d498392003-07-01 21:06:45 +00001332U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001333 nm, 2, 1, do_mem_nm,
Peter Tyser2fb26042009-01-27 18:03:12 -06001334 "memory modify (constant address)",
Simon Glass76be8f72020-06-02 19:26:45 -06001335 "[.b, .w, .l" HELP_Q "] address"
wdenk8bde7f72003-06-27 21:31:46 +00001336);
1337
wdenk0d498392003-07-01 21:06:45 +00001338U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001339 mw, 4, 1, do_mem_mw,
Peter Tyser2fb26042009-01-27 18:03:12 -06001340 "memory write (fill)",
Simon Glass76be8f72020-06-02 19:26:45 -06001341 "[.b, .w, .l" HELP_Q "] address value [count]"
wdenk8bde7f72003-06-27 21:31:46 +00001342);
1343
wdenk0d498392003-07-01 21:06:45 +00001344U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001345 cp, 4, 1, do_mem_cp,
Peter Tyser2fb26042009-01-27 18:03:12 -06001346 "memory copy",
Simon Glass76be8f72020-06-02 19:26:45 -06001347 "[.b, .w, .l" HELP_Q "] source target count"
wdenk8bde7f72003-06-27 21:31:46 +00001348);
1349
wdenk0d498392003-07-01 21:06:45 +00001350U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001351 cmp, 4, 1, do_mem_cmp,
Peter Tyser2fb26042009-01-27 18:03:12 -06001352 "memory compare",
Simon Glass76be8f72020-06-02 19:26:45 -06001353 "[.b, .w, .l" HELP_Q "] addr1 addr2 count"
wdenk8bde7f72003-06-27 21:31:46 +00001354);
1355
Simon Glass550a9e72020-07-28 19:41:14 -06001356#ifdef CONFIG_CMD_MEM_SEARCH
Simon Glassbdded202020-06-02 19:26:49 -06001357/**************************************************/
1358U_BOOT_CMD(
1359 ms, 255, 1, do_mem_search,
1360 "memory search",
1361 "[.b, .w, .l" HELP_Q ", .s] [-q | -<n>] address #-of-objects <value>..."
Simon Glass550a9e72020-07-28 19:41:14 -06001362 " -q = quiet, -l<val> = match limit"
Simon Glassbdded202020-06-02 19:26:49 -06001363);
1364#endif
1365
Mike Frysinger710b9932010-12-21 14:19:51 -05001366#ifdef CONFIG_CMD_CRC32
1367
Daniel Thompson221a9492017-05-19 17:26:58 +01001368#ifndef CONFIG_CRC32_VERIFY
wdenkc26e4542004-04-18 10:13:26 +00001369
wdenk0d498392003-07-01 21:06:45 +00001370U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001371 crc32, 4, 1, do_mem_crc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001372 "checksum calculation",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001373 "address count [addr]\n - compute CRC32 checksum [save at addr]"
wdenk8bde7f72003-06-27 21:31:46 +00001374);
1375
Daniel Thompson221a9492017-05-19 17:26:58 +01001376#else /* CONFIG_CRC32_VERIFY */
wdenkc26e4542004-04-18 10:13:26 +00001377
1378U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001379 crc32, 5, 1, do_mem_crc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001380 "checksum calculation",
wdenkc26e4542004-04-18 10:13:26 +00001381 "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001382 "-v address count crc\n - verify crc of memory area"
wdenkc26e4542004-04-18 10:13:26 +00001383);
1384
Daniel Thompson221a9492017-05-19 17:26:58 +01001385#endif /* CONFIG_CRC32_VERIFY */
wdenkc26e4542004-04-18 10:13:26 +00001386
Mike Frysinger710b9932010-12-21 14:19:51 -05001387#endif
1388
Simon Glass15a33e42012-11-30 13:01:20 +00001389#ifdef CONFIG_CMD_MEMINFO
Simon Glass09140112020-05-10 11:40:03 -06001390static int do_mem_info(struct cmd_tbl *cmdtp, int flag, int argc,
1391 char *const argv[])
Simon Glass15a33e42012-11-30 13:01:20 +00001392{
Simon Glass428a6a12019-11-14 12:57:44 -07001393 puts("DRAM: ");
1394 print_size(gd->ram_size, "\n");
Simon Glass15a33e42012-11-30 13:01:20 +00001395
1396 return 0;
1397}
1398#endif
1399
wdenk0d498392003-07-01 21:06:45 +00001400U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001401 base, 2, 1, do_mem_base,
Peter Tyser2fb26042009-01-27 18:03:12 -06001402 "print or set address offset",
wdenk8bde7f72003-06-27 21:31:46 +00001403 "\n - print address offset for memory commands\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001404 "base off\n - set address offset for memory commands to 'off'"
wdenk8bde7f72003-06-27 21:31:46 +00001405);
1406
wdenk0d498392003-07-01 21:06:45 +00001407U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001408 loop, 3, 1, do_mem_loop,
Peter Tyser2fb26042009-01-27 18:03:12 -06001409 "infinite loop on address range",
Simon Glass76be8f72020-06-02 19:26:45 -06001410 "[.b, .w, .l" HELP_Q "] address number_of_objects"
wdenk8bde7f72003-06-27 21:31:46 +00001411);
1412
wdenk56523f12004-07-11 17:40:54 +00001413#ifdef CONFIG_LOOPW
1414U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001415 loopw, 4, 1, do_mem_loopw,
Peter Tyser2fb26042009-01-27 18:03:12 -06001416 "infinite write loop on address range",
Simon Glass76be8f72020-06-02 19:26:45 -06001417 "[.b, .w, .l" HELP_Q "] address number_of_objects data_to_write"
wdenk56523f12004-07-11 17:40:54 +00001418);
1419#endif /* CONFIG_LOOPW */
1420
Wolfgang Denka2681702013-03-08 10:51:32 +00001421#ifdef CONFIG_CMD_MEMTEST
wdenk0d498392003-07-01 21:06:45 +00001422U_BOOT_CMD(
Dirk Eibachb6fc6fd2008-12-16 14:51:56 +01001423 mtest, 5, 1, do_mem_mtest,
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001424 "simple RAM read/write test",
1425 "[start [end [pattern [iterations]]]]"
wdenk8bde7f72003-06-27 21:31:46 +00001426);
Wolfgang Denka2681702013-03-08 10:51:32 +00001427#endif /* CONFIG_CMD_MEMTEST */
wdenk8bde7f72003-06-27 21:31:46 +00001428
Joel Johnson72732312020-01-29 09:17:18 -07001429#ifdef CONFIG_CMD_MX_CYCLIC
stroese4aaf29b2004-12-16 17:42:39 +00001430U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001431 mdc, 4, 1, do_mem_mdc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001432 "memory display cyclic",
Simon Glass76be8f72020-06-02 19:26:45 -06001433 "[.b, .w, .l" HELP_Q "] address count delay(ms)"
stroese4aaf29b2004-12-16 17:42:39 +00001434);
1435
1436U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001437 mwc, 4, 1, do_mem_mwc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001438 "memory write cyclic",
Simon Glass76be8f72020-06-02 19:26:45 -06001439 "[.b, .w, .l" HELP_Q "] address value delay(ms)"
stroese4aaf29b2004-12-16 17:42:39 +00001440);
Joel Johnson72732312020-01-29 09:17:18 -07001441#endif /* CONFIG_CMD_MX_CYCLIC */
Simon Glass15a33e42012-11-30 13:01:20 +00001442
1443#ifdef CONFIG_CMD_MEMINFO
1444U_BOOT_CMD(
1445 meminfo, 3, 1, do_mem_info,
1446 "display memory information",
1447 ""
1448);
1449#endif
Jean-Jacques Hiblot803e1a32019-07-02 14:23:26 +02001450
1451#ifdef CONFIG_CMD_RANDOM
1452U_BOOT_CMD(
1453 random, 4, 0, do_random,
1454 "fill memory with random pattern",
1455 "<addr> <len> [<seed>]\n"
1456 " - Fill 'len' bytes of memory starting at 'addr' with random data\n"
1457);
1458#endif