blob: 9df5eb068a79b09bc972a2f119c51eaaa8e37a6f [file] [log] [blame]
Tom Rini10e47792018-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 Glassa73bda42015-11-08 23:47:45 -070014#include <console.h>
Simon Glass399ed9a2014-04-10 20:01:30 -060015#include <bootretry.h>
Simon Glassdec3c012014-04-10 20:01:25 -060016#include <cli.h>
wdenk38635852002-08-27 05:55:31 +000017#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070018#include <console.h>
Simon Glassa606ffc2019-12-28 10:44:40 -070019#include <flash.h>
Simon Glass0bbd76f2013-02-24 20:30:22 +000020#include <hash.h>
Simon Glass0f2af882020-05-10 11:40:05 -060021#include <log.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050022#include <mapmem.h>
Simon Glass274e0b02020-05-10 11:39:56 -060023#include <rand.h>
Sergei Poselenov474dbb82008-04-09 16:09:41 +020024#include <watchdog.h>
Simon Glasse6d0ca22013-02-24 17:33:15 +000025#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060026#include <linux/bitops.h>
Simon Glasseacd14f2012-11-30 13:01:20 +000027#include <linux/compiler.h>
Simon Glass19038de2020-06-02 19:26:49 -060028#include <linux/ctype.h>
Simon Glassdbd79542020-05-10 11:40:11 -060029#include <linux/delay.h>
Simon Glasseacd14f2012-11-30 13:01:20 +000030
31DECLARE_GLOBAL_DATA_PTR;
wdenk38635852002-08-27 05:55:31 +000032
Simon Glass683e39e2020-06-02 19:26:45 -060033/* Create a compile-time value */
Simon Glassd1c117b2020-06-02 19:26:46 -060034#ifdef MEM_SUPPORT_64BIT_DATA
35#define SUPPORT_64BIT_DATA 1
Simon Glass683e39e2020-06-02 19:26:45 -060036#define HELP_Q ", .q"
37#else
Simon Glassd1c117b2020-06-02 19:26:46 -060038#define SUPPORT_64BIT_DATA 0
Simon Glass683e39e2020-06-02 19:26:45 -060039#define HELP_Q ""
40#endif
41
Simon Glassed38aef2020-05-10 11:40:03 -060042static int mod_mem(struct cmd_tbl *, int, int, int, char * const []);
wdenk38635852002-08-27 05:55:31 +000043
44/* Display values from last command.
45 * Memory modify remembered values are different from display memory.
46 */
Scott Wood3ec02882015-03-19 09:43:12 -070047static ulong dp_last_addr, dp_last_size;
48static ulong dp_last_length = 0x40;
49static ulong mm_last_addr, mm_last_size;
wdenk38635852002-08-27 05:55:31 +000050
51static ulong base_address = 0;
Simon Glass573c0fa2020-07-28 19:41:14 -060052#ifdef CONFIG_CMD_MEM_SEARCH
53static ulong dp_last_ms_length;
Simon Glass19038de2020-06-02 19:26:49 -060054static u8 search_buf[64];
55static uint search_len;
56#endif
wdenk38635852002-08-27 05:55:31 +000057
58/* Memory Display
59 *
60 * Syntax:
York Sun6c480012014-02-26 17:03:19 -080061 * md{.b, .w, .l, .q} {addr} {len}
wdenk38635852002-08-27 05:55:31 +000062 */
63#define DISP_LINE_LEN 16
Simon Glassed38aef2020-05-10 11:40:03 -060064static int do_mem_md(struct cmd_tbl *cmdtp, int flag, int argc,
65 char *const argv[])
wdenk38635852002-08-27 05:55:31 +000066{
Tuomas Tynkkynen1b725202017-10-10 21:59:42 +030067 ulong addr, length, bytes;
68 const void *buf;
wdenk874ac262003-07-24 23:38:38 +000069 int size;
wdenk38635852002-08-27 05:55:31 +000070 int rc = 0;
71
72 /* We use the last specified parameters, unless new ones are
73 * entered.
74 */
75 addr = dp_last_addr;
76 size = dp_last_size;
77 length = dp_last_length;
78
Wolfgang Denk3b683112010-07-17 01:06:04 +020079 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +000080 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +000081
82 if ((flag & CMD_FLAG_REPEAT) == 0) {
83 /* New command specified. Check for a size specification.
84 * Defaults to long if no or incorrect specification.
85 */
wdenk874ac262003-07-24 23:38:38 +000086 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
87 return 1;
wdenk38635852002-08-27 05:55:31 +000088
89 /* Address is specified since argc > 1
90 */
91 addr = simple_strtoul(argv[1], NULL, 16);
92 addr += base_address;
93
94 /* If another parameter, it is the length to display.
95 * Length is the number of objects, not number of bytes.
96 */
97 if (argc > 2)
98 length = simple_strtoul(argv[2], NULL, 16);
99 }
100
Tuomas Tynkkynen1b725202017-10-10 21:59:42 +0300101 bytes = size * length;
102 buf = map_sysmem(addr, bytes);
Simon Glasse6d0ca22013-02-24 17:33:15 +0000103
Tuomas Tynkkynen1b725202017-10-10 21:59:42 +0300104 /* Print the lines. */
105 print_buffer(addr, buf, size, length, DISP_LINE_LEN / size);
106 addr += bytes;
107 unmap_sysmem(buf);
wdenk38635852002-08-27 05:55:31 +0000108
109 dp_last_addr = addr;
110 dp_last_length = length;
111 dp_last_size = size;
112 return (rc);
113}
114
Simon Glassed38aef2020-05-10 11:40:03 -0600115static int do_mem_mm(struct cmd_tbl *cmdtp, int flag, int argc,
116 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000117{
118 return mod_mem (cmdtp, 1, flag, argc, argv);
119}
Simon Glassed38aef2020-05-10 11:40:03 -0600120
121static int do_mem_nm(struct cmd_tbl *cmdtp, int flag, int argc,
122 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000123{
124 return mod_mem (cmdtp, 0, flag, argc, argv);
125}
126
Simon Glassed38aef2020-05-10 11:40:03 -0600127static int do_mem_mw(struct cmd_tbl *cmdtp, int flag, int argc,
128 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000129{
Simon Glassd1c117b2020-06-02 19:26:46 -0600130 ulong writeval; /* 64-bit if SUPPORT_64BIT_DATA */
York Sun6c480012014-02-26 17:03:19 -0800131 ulong addr, count;
wdenk874ac262003-07-24 23:38:38 +0000132 int size;
Simon Glass3c4a2872015-03-05 12:25:18 -0700133 void *buf, *start;
Simon Glasse6d0ca22013-02-24 17:33:15 +0000134 ulong bytes;
wdenk38635852002-08-27 05:55:31 +0000135
Wolfgang Denk3b683112010-07-17 01:06:04 +0200136 if ((argc < 3) || (argc > 4))
Simon Glassa06dfc72011-12-10 08:44:01 +0000137 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000138
139 /* Check for size specification.
140 */
wdenk874ac262003-07-24 23:38:38 +0000141 if ((size = cmd_get_data_size(argv[0], 4)) < 1)
142 return 1;
wdenk38635852002-08-27 05:55:31 +0000143
144 /* Address is specified since argc > 1
145 */
146 addr = simple_strtoul(argv[1], NULL, 16);
147 addr += base_address;
148
149 /* Get the value to write.
150 */
Simon Glassd1c117b2020-06-02 19:26:46 -0600151 if (SUPPORT_64BIT_DATA)
152 writeval = simple_strtoull(argv[2], NULL, 16);
153 else
154 writeval = simple_strtoul(argv[2], NULL, 16);
wdenk38635852002-08-27 05:55:31 +0000155
156 /* Count ? */
157 if (argc == 4) {
158 count = simple_strtoul(argv[3], NULL, 16);
159 } else {
160 count = 1;
161 }
162
Simon Glasse6d0ca22013-02-24 17:33:15 +0000163 bytes = size * count;
Simon Glass3c4a2872015-03-05 12:25:18 -0700164 start = map_sysmem(addr, bytes);
165 buf = start;
wdenk38635852002-08-27 05:55:31 +0000166 while (count-- > 0) {
167 if (size == 4)
York Sund8a405d2014-02-12 15:55:35 -0800168 *((u32 *)buf) = (u32)writeval;
Simon Glassd1c117b2020-06-02 19:26:46 -0600169 else if (SUPPORT_64BIT_DATA && size == 8)
170 *((ulong *)buf) = writeval;
wdenk38635852002-08-27 05:55:31 +0000171 else if (size == 2)
York Sund8a405d2014-02-12 15:55:35 -0800172 *((u16 *)buf) = (u16)writeval;
wdenk38635852002-08-27 05:55:31 +0000173 else
York Sund8a405d2014-02-12 15:55:35 -0800174 *((u8 *)buf) = (u8)writeval;
Simon Glasse6d0ca22013-02-24 17:33:15 +0000175 buf += size;
wdenk38635852002-08-27 05:55:31 +0000176 }
Simon Glass3c4a2872015-03-05 12:25:18 -0700177 unmap_sysmem(start);
wdenk38635852002-08-27 05:55:31 +0000178 return 0;
179}
180
Joel Johnsondb5a97e2020-01-29 09:17:18 -0700181#ifdef CONFIG_CMD_MX_CYCLIC
Simon Glassed38aef2020-05-10 11:40:03 -0600182static int do_mem_mdc(struct cmd_tbl *cmdtp, int flag, int argc,
183 char *const argv[])
stroesefb5e0ac2004-12-16 17:42:39 +0000184{
185 int i;
186 ulong count;
187
Wolfgang Denk3b683112010-07-17 01:06:04 +0200188 if (argc < 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000189 return CMD_RET_USAGE;
stroesefb5e0ac2004-12-16 17:42:39 +0000190
191 count = simple_strtoul(argv[3], NULL, 10);
192
193 for (;;) {
194 do_mem_md (NULL, 0, 3, argv);
195
196 /* delay for <count> ms... */
197 for (i=0; i<count; i++)
Simon Glass0db4b942020-05-10 11:40:10 -0600198 udelay(1000);
stroesefb5e0ac2004-12-16 17:42:39 +0000199
200 /* check for ctrl-c to abort... */
201 if (ctrlc()) {
202 puts("Abort\n");
203 return 0;
204 }
205 }
206
207 return 0;
208}
209
Simon Glassed38aef2020-05-10 11:40:03 -0600210static int do_mem_mwc(struct cmd_tbl *cmdtp, int flag, int argc,
211 char *const argv[])
stroesefb5e0ac2004-12-16 17:42:39 +0000212{
213 int i;
214 ulong count;
215
Wolfgang Denk3b683112010-07-17 01:06:04 +0200216 if (argc < 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000217 return CMD_RET_USAGE;
stroesefb5e0ac2004-12-16 17:42:39 +0000218
219 count = simple_strtoul(argv[3], NULL, 10);
220
221 for (;;) {
222 do_mem_mw (NULL, 0, 3, argv);
223
224 /* delay for <count> ms... */
225 for (i=0; i<count; i++)
Simon Glass0db4b942020-05-10 11:40:10 -0600226 udelay(1000);
stroesefb5e0ac2004-12-16 17:42:39 +0000227
228 /* check for ctrl-c to abort... */
229 if (ctrlc()) {
230 puts("Abort\n");
231 return 0;
232 }
233 }
234
235 return 0;
236}
Joel Johnsondb5a97e2020-01-29 09:17:18 -0700237#endif /* CONFIG_CMD_MX_CYCLIC */
stroesefb5e0ac2004-12-16 17:42:39 +0000238
Simon Glassed38aef2020-05-10 11:40:03 -0600239static int do_mem_cmp(struct cmd_tbl *cmdtp, int flag, int argc,
240 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000241{
Simon Glasse6d0ca22013-02-24 17:33:15 +0000242 ulong addr1, addr2, count, ngood, bytes;
wdenk874ac262003-07-24 23:38:38 +0000243 int size;
wdenk38635852002-08-27 05:55:31 +0000244 int rcode = 0;
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000245 const char *type;
Simon Glasse6d0ca22013-02-24 17:33:15 +0000246 const void *buf1, *buf2, *base;
Simon Glassd1c117b2020-06-02 19:26:46 -0600247 ulong word1, word2; /* 64-bit if SUPPORT_64BIT_DATA */
wdenk38635852002-08-27 05:55:31 +0000248
Wolfgang Denk3b683112010-07-17 01:06:04 +0200249 if (argc != 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000250 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000251
252 /* Check for size specification.
253 */
wdenk874ac262003-07-24 23:38:38 +0000254 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
255 return 1;
York Sun6c480012014-02-26 17:03:19 -0800256 type = size == 8 ? "double word" :
257 size == 4 ? "word" :
258 size == 2 ? "halfword" : "byte";
wdenk38635852002-08-27 05:55:31 +0000259
260 addr1 = simple_strtoul(argv[1], NULL, 16);
261 addr1 += base_address;
262
263 addr2 = simple_strtoul(argv[2], NULL, 16);
264 addr2 += base_address;
265
266 count = simple_strtoul(argv[3], NULL, 16);
267
Simon Glasse6d0ca22013-02-24 17:33:15 +0000268 bytes = size * count;
269 base = buf1 = map_sysmem(addr1, bytes);
270 buf2 = map_sysmem(addr2, bytes);
Mike Frysingera66afbd2012-01-20 09:07:22 +0000271 for (ngood = 0; ngood < count; ++ngood) {
wdenk38635852002-08-27 05:55:31 +0000272 if (size == 4) {
York Sund8a405d2014-02-12 15:55:35 -0800273 word1 = *(u32 *)buf1;
274 word2 = *(u32 *)buf2;
Simon Glassd1c117b2020-06-02 19:26:46 -0600275 } else if (SUPPORT_64BIT_DATA && size == 8) {
276 word1 = *(ulong *)buf1;
277 word2 = *(ulong *)buf2;
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000278 } else if (size == 2) {
York Sund8a405d2014-02-12 15:55:35 -0800279 word1 = *(u16 *)buf1;
280 word2 = *(u16 *)buf2;
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000281 } else {
York Sund8a405d2014-02-12 15:55:35 -0800282 word1 = *(u8 *)buf1;
283 word2 = *(u8 *)buf2;
wdenk38635852002-08-27 05:55:31 +0000284 }
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000285 if (word1 != word2) {
Simon Glasse6d0ca22013-02-24 17:33:15 +0000286 ulong offset = buf1 - base;
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000287 printf("%s at 0x%08lx (%#0*lx) != %s at 0x%08lx (%#0*lx)\n",
Simon Glasse6d0ca22013-02-24 17:33:15 +0000288 type, (ulong)(addr1 + offset), size, word1,
289 type, (ulong)(addr2 + offset), size, word2);
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000290 rcode = 1;
291 break;
wdenk38635852002-08-27 05:55:31 +0000292 }
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000293
Simon Glasse6d0ca22013-02-24 17:33:15 +0000294 buf1 += size;
295 buf2 += size;
Stefan Roesed4ef82b2010-09-13 11:10:34 +0200296
297 /* reset watchdog from time to time */
Mike Frysingera66afbd2012-01-20 09:07:22 +0000298 if ((ngood % (64 << 10)) == 0)
Stefan Roesed4ef82b2010-09-13 11:10:34 +0200299 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000300 }
Simon Glasse6d0ca22013-02-24 17:33:15 +0000301 unmap_sysmem(buf1);
302 unmap_sysmem(buf2);
wdenk38635852002-08-27 05:55:31 +0000303
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000304 printf("Total of %ld %s(s) were the same\n", ngood, type);
wdenk38635852002-08-27 05:55:31 +0000305 return rcode;
306}
307
Simon Glassed38aef2020-05-10 11:40:03 -0600308static int do_mem_cp(struct cmd_tbl *cmdtp, int flag, int argc,
309 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000310{
Fabio Estevamb03b0c82016-12-15 16:00:13 -0200311 ulong addr, dest, count;
Philippe Reynesb5f461a2019-12-02 17:33:22 +0100312 void *src, *dst;
wdenk874ac262003-07-24 23:38:38 +0000313 int size;
wdenk38635852002-08-27 05:55:31 +0000314
Wolfgang Denk3b683112010-07-17 01:06:04 +0200315 if (argc != 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000316 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000317
318 /* Check for size specification.
319 */
wdenk874ac262003-07-24 23:38:38 +0000320 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
321 return 1;
wdenk38635852002-08-27 05:55:31 +0000322
323 addr = simple_strtoul(argv[1], NULL, 16);
324 addr += base_address;
325
326 dest = simple_strtoul(argv[2], NULL, 16);
327 dest += base_address;
328
329 count = simple_strtoul(argv[3], NULL, 16);
330
331 if (count == 0) {
332 puts ("Zero length ???\n");
333 return 1;
334 }
335
Philippe Reynesb5f461a2019-12-02 17:33:22 +0100336 src = map_sysmem(addr, count * size);
337 dst = map_sysmem(dest, count * size);
338
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900339#ifdef CONFIG_MTD_NOR_FLASH
wdenk38635852002-08-27 05:55:31 +0000340 /* check if we are copying to Flash */
Philippe Reynesb5f461a2019-12-02 17:33:22 +0100341 if (addr2info((ulong)dst)) {
wdenk38635852002-08-27 05:55:31 +0000342 int rc;
343
wdenk42c05472004-03-23 22:14:11 +0000344 puts ("Copy to Flash... ");
wdenk38635852002-08-27 05:55:31 +0000345
Philippe Reynesb5f461a2019-12-02 17:33:22 +0100346 rc = flash_write((char *)src, (ulong)dst, count * size);
wdenk38635852002-08-27 05:55:31 +0000347 if (rc != 0) {
Simon Glassa606ffc2019-12-28 10:44:40 -0700348 flash_perror(rc);
Philippe Reynesb5f461a2019-12-02 17:33:22 +0100349 unmap_sysmem(src);
350 unmap_sysmem(dst);
wdenk38635852002-08-27 05:55:31 +0000351 return (1);
352 }
353 puts ("done\n");
Philippe Reynesb5f461a2019-12-02 17:33:22 +0100354 unmap_sysmem(src);
355 unmap_sysmem(dst);
wdenk38635852002-08-27 05:55:31 +0000356 return 0;
357 }
wdenk7a428cc2003-06-15 22:40:42 +0000358#endif
359
Philippe Reynesb5f461a2019-12-02 17:33:22 +0100360 memcpy(dst, src, count * size);
Masahiro Yamada787b0c52014-10-23 17:46:24 +0900361
Philippe Reynesb5f461a2019-12-02 17:33:22 +0100362 unmap_sysmem(src);
363 unmap_sysmem(dst);
wdenk38635852002-08-27 05:55:31 +0000364 return 0;
365}
366
Simon Glass573c0fa2020-07-28 19:41:14 -0600367#ifdef CONFIG_CMD_MEM_SEARCH
Simon Glass19038de2020-06-02 19:26:49 -0600368static int do_mem_search(struct cmd_tbl *cmdtp, int flag, int argc,
369 char *const argv[])
370{
371 ulong addr, length, bytes, offset;
372 u8 *ptr, *end, *buf;
373 bool quiet = false;
374 ulong last_pos; /* Offset of last match in 'size' units*/
375 ulong last_addr; /* Address of last displayed line */
376 int limit = 10;
Simon Glass573c0fa2020-07-28 19:41:14 -0600377 int used_len;
Simon Glass19038de2020-06-02 19:26:49 -0600378 int count;
379 int size;
380 int i;
381
382 /* We use the last specified parameters, unless new ones are entered */
383 addr = dp_last_addr;
384 size = dp_last_size;
Simon Glass573c0fa2020-07-28 19:41:14 -0600385 length = dp_last_ms_length;
Simon Glass19038de2020-06-02 19:26:49 -0600386
387 if (argc < 3)
388 return CMD_RET_USAGE;
389
Simon Glass573c0fa2020-07-28 19:41:14 -0600390 if (!(flag & CMD_FLAG_REPEAT)) {
Simon Glass19038de2020-06-02 19:26:49 -0600391 /*
392 * Check for a size specification.
393 * Defaults to long if no or incorrect specification.
394 */
395 size = cmd_get_data_size(argv[0], 4);
396 if (size < 0 && size != -2 /* string */)
397 return 1;
398
Simon Glass573c0fa2020-07-28 19:41:14 -0600399 argc--;
400 argv++;
Simon Glass19038de2020-06-02 19:26:49 -0600401 while (argc && *argv[0] == '-') {
402 int ch = argv[0][1];
403
404 if (ch == 'q')
405 quiet = true;
406 else if (ch == 'l' && isxdigit(argv[0][2]))
407 limit = simple_strtoul(argv[0] + 2, NULL, 16);
408 else
409 return CMD_RET_USAGE;
Simon Glass573c0fa2020-07-28 19:41:14 -0600410 argc--;
411 argv++;
Simon Glass19038de2020-06-02 19:26:49 -0600412 }
413
414 /* Address is specified since argc > 1 */
415 addr = simple_strtoul(argv[0], NULL, 16);
416 addr += base_address;
417
418 /* Length is the number of objects, not number of bytes */
419 length = simple_strtoul(argv[1], NULL, 16);
420
421 /* Read the bytes to search for */
422 end = search_buf + sizeof(search_buf);
423 for (i = 2, ptr = search_buf; i < argc && ptr < end; i++) {
Simon Glass573c0fa2020-07-28 19:41:14 -0600424 if (MEM_SUPPORT_64BIT_DATA && size == 8) {
Simon Glass19038de2020-06-02 19:26:49 -0600425 u64 val = simple_strtoull(argv[i], NULL, 16);
426
427 *(u64 *)ptr = val;
428 } else if (size == -2) { /* string */
429 int len = min(strlen(argv[i]),
430 (size_t)(end - ptr));
431
432 memcpy(ptr, argv[i], len);
433 ptr += len;
434 continue;
435 } else {
436 u32 val = simple_strtoul(argv[i], NULL, 16);
437
438 switch (size) {
439 case 1:
440 *ptr = val;
441 break;
442 case 2:
443 *(u16 *)ptr = val;
444 break;
445 case 4:
446 *(u32 *)ptr = val;
447 break;
448 }
449 }
450 ptr += size;
451 }
452 search_len = ptr - search_buf;
453 }
454
455 /* Do the search */
456 if (size == -2)
457 size = 1;
458 bytes = size * length;
459 buf = map_sysmem(addr, bytes);
460 last_pos = 0;
461 last_addr = 0;
462 count = 0;
Simon Glass573c0fa2020-07-28 19:41:14 -0600463 for (offset = 0;
464 offset < bytes && offset <= bytes - search_len && count < limit;
Simon Glass19038de2020-06-02 19:26:49 -0600465 offset += size) {
466 void *ptr = buf + offset;
467
468 if (!memcmp(ptr, search_buf, search_len)) {
469 uint align = (addr + offset) & 0xf;
470 ulong match = addr + offset;
471
472 if (!count || (last_addr & ~0xf) != (match & ~0xf)) {
473 if (!quiet) {
474 if (count)
475 printf("--\n");
476 print_buffer(match - align, ptr - align,
477 size,
478 ALIGN(search_len + align,
479 16) / size, 0);
480 }
481 last_addr = match;
482 last_pos = offset / size;
483 }
484 count++;
485 }
486 }
487 if (!quiet) {
488 printf("%d match%s", count, count == 1 ? "" : "es");
489 if (count == limit)
490 printf(" (repeat command to check for more)");
491 printf("\n");
492 }
493 env_set_hex("memmatches", count);
494 env_set_hex("memaddr", last_addr);
495 env_set_hex("mempos", last_pos);
496
497 unmap_sysmem(buf);
498
Simon Glass573c0fa2020-07-28 19:41:14 -0600499 used_len = offset / size;
500 dp_last_addr = addr + used_len;
Simon Glass19038de2020-06-02 19:26:49 -0600501 dp_last_size = size;
Simon Glass573c0fa2020-07-28 19:41:14 -0600502 dp_last_ms_length = length < used_len ? 0 : length - used_len;
Simon Glass19038de2020-06-02 19:26:49 -0600503
504 return count ? 0 : CMD_RET_FAILURE;
505}
506#endif
507
Simon Glassed38aef2020-05-10 11:40:03 -0600508static int do_mem_base(struct cmd_tbl *cmdtp, int flag, int argc,
509 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000510{
511 if (argc > 1) {
512 /* Set new base address.
513 */
514 base_address = simple_strtoul(argv[1], NULL, 16);
515 }
516 /* Print the current base address.
517 */
518 printf("Base Address: 0x%08lx\n", base_address);
519 return 0;
520}
521
Simon Glassed38aef2020-05-10 11:40:03 -0600522static int do_mem_loop(struct cmd_tbl *cmdtp, int flag, int argc,
523 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000524{
Simon Glasse6d0ca22013-02-24 17:33:15 +0000525 ulong addr, length, i, bytes;
wdenk874ac262003-07-24 23:38:38 +0000526 int size;
Simon Glassd1c117b2020-06-02 19:26:46 -0600527 volatile ulong *llp; /* 64-bit if SUPPORT_64BIT_DATA */
York Sund8a405d2014-02-12 15:55:35 -0800528 volatile u32 *longp;
529 volatile u16 *shortp;
530 volatile u8 *cp;
Simon Glasse6d0ca22013-02-24 17:33:15 +0000531 const void *buf;
wdenk38635852002-08-27 05:55:31 +0000532
Wolfgang Denk3b683112010-07-17 01:06:04 +0200533 if (argc < 3)
Simon Glassa06dfc72011-12-10 08:44:01 +0000534 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000535
Robert P. J. Day21c9b782013-02-03 02:29:54 +0000536 /*
537 * Check for a size specification.
wdenk38635852002-08-27 05:55:31 +0000538 * Defaults to long if no or incorrect specification.
539 */
wdenk874ac262003-07-24 23:38:38 +0000540 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
541 return 1;
wdenk38635852002-08-27 05:55:31 +0000542
543 /* Address is always specified.
544 */
545 addr = simple_strtoul(argv[1], NULL, 16);
546
547 /* Length is the number of objects, not number of bytes.
548 */
549 length = simple_strtoul(argv[2], NULL, 16);
550
Simon Glasse6d0ca22013-02-24 17:33:15 +0000551 bytes = size * length;
552 buf = map_sysmem(addr, bytes);
553
wdenk38635852002-08-27 05:55:31 +0000554 /* We want to optimize the loops to run as fast as possible.
555 * If we have only one object, just run infinite loops.
556 */
557 if (length == 1) {
Simon Glassd1c117b2020-06-02 19:26:46 -0600558 if (SUPPORT_64BIT_DATA && size == 8) {
559 llp = (ulong *)buf;
York Sun6c480012014-02-26 17:03:19 -0800560 for (;;)
561 i = *llp;
562 }
wdenk38635852002-08-27 05:55:31 +0000563 if (size == 4) {
York Sund8a405d2014-02-12 15:55:35 -0800564 longp = (u32 *)buf;
wdenk38635852002-08-27 05:55:31 +0000565 for (;;)
566 i = *longp;
567 }
568 if (size == 2) {
York Sund8a405d2014-02-12 15:55:35 -0800569 shortp = (u16 *)buf;
wdenk38635852002-08-27 05:55:31 +0000570 for (;;)
571 i = *shortp;
572 }
York Sund8a405d2014-02-12 15:55:35 -0800573 cp = (u8 *)buf;
wdenk38635852002-08-27 05:55:31 +0000574 for (;;)
575 i = *cp;
576 }
577
Simon Glassd1c117b2020-06-02 19:26:46 -0600578 if (SUPPORT_64BIT_DATA && size == 8) {
York Sun6c480012014-02-26 17:03:19 -0800579 for (;;) {
Simon Glassd1c117b2020-06-02 19:26:46 -0600580 llp = (ulong *)buf;
York Sun6c480012014-02-26 17:03:19 -0800581 i = length;
582 while (i-- > 0)
583 *llp++;
584 }
585 }
wdenk38635852002-08-27 05:55:31 +0000586 if (size == 4) {
587 for (;;) {
York Sund8a405d2014-02-12 15:55:35 -0800588 longp = (u32 *)buf;
wdenk38635852002-08-27 05:55:31 +0000589 i = length;
590 while (i-- > 0)
Marek Vasut7d335822011-09-26 02:26:06 +0200591 *longp++;
wdenk38635852002-08-27 05:55:31 +0000592 }
593 }
594 if (size == 2) {
595 for (;;) {
York Sund8a405d2014-02-12 15:55:35 -0800596 shortp = (u16 *)buf;
wdenk38635852002-08-27 05:55:31 +0000597 i = length;
598 while (i-- > 0)
Marek Vasut7d335822011-09-26 02:26:06 +0200599 *shortp++;
wdenk38635852002-08-27 05:55:31 +0000600 }
601 }
602 for (;;) {
York Sund8a405d2014-02-12 15:55:35 -0800603 cp = (u8 *)buf;
wdenk38635852002-08-27 05:55:31 +0000604 i = length;
605 while (i-- > 0)
Marek Vasut7d335822011-09-26 02:26:06 +0200606 *cp++;
wdenk38635852002-08-27 05:55:31 +0000607 }
Simon Glasse6d0ca22013-02-24 17:33:15 +0000608 unmap_sysmem(buf);
Simon Glass50d9eb92013-06-11 11:14:35 -0700609
610 return 0;
wdenk38635852002-08-27 05:55:31 +0000611}
612
wdenk64519362004-07-11 17:40:54 +0000613#ifdef CONFIG_LOOPW
Simon Glassed38aef2020-05-10 11:40:03 -0600614static int do_mem_loopw(struct cmd_tbl *cmdtp, int flag, int argc,
615 char *const argv[])
wdenk64519362004-07-11 17:40:54 +0000616{
York Sun6c480012014-02-26 17:03:19 -0800617 ulong addr, length, i, bytes;
wdenk64519362004-07-11 17:40:54 +0000618 int size;
Simon Glassd1c117b2020-06-02 19:26:46 -0600619 volatile ulong *llp; /* 64-bit if SUPPORT_64BIT_DATA */
620 ulong data; /* 64-bit if SUPPORT_64BIT_DATA */
York Sund8a405d2014-02-12 15:55:35 -0800621 volatile u32 *longp;
622 volatile u16 *shortp;
623 volatile u8 *cp;
Simon Glasse6d0ca22013-02-24 17:33:15 +0000624 void *buf;
wdenk7dd13292004-07-11 20:04:51 +0000625
Wolfgang Denk3b683112010-07-17 01:06:04 +0200626 if (argc < 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000627 return CMD_RET_USAGE;
wdenk64519362004-07-11 17:40:54 +0000628
Robert P. J. Day21c9b782013-02-03 02:29:54 +0000629 /*
630 * Check for a size specification.
wdenk64519362004-07-11 17:40:54 +0000631 * Defaults to long if no or incorrect specification.
632 */
633 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
634 return 1;
635
636 /* Address is always specified.
637 */
638 addr = simple_strtoul(argv[1], NULL, 16);
639
640 /* Length is the number of objects, not number of bytes.
641 */
642 length = simple_strtoul(argv[2], NULL, 16);
643
644 /* data to write */
Simon Glassd1c117b2020-06-02 19:26:46 -0600645 if (SUPPORT_64BIT_DATA)
646 data = simple_strtoull(argv[3], NULL, 16);
647 else
648 data = simple_strtoul(argv[3], NULL, 16);
wdenk7dd13292004-07-11 20:04:51 +0000649
Simon Glasse6d0ca22013-02-24 17:33:15 +0000650 bytes = size * length;
651 buf = map_sysmem(addr, bytes);
652
wdenk64519362004-07-11 17:40:54 +0000653 /* We want to optimize the loops to run as fast as possible.
654 * If we have only one object, just run infinite loops.
655 */
656 if (length == 1) {
Simon Glassd1c117b2020-06-02 19:26:46 -0600657 if (SUPPORT_64BIT_DATA && size == 8) {
658 llp = (ulong *)buf;
York Sun6c480012014-02-26 17:03:19 -0800659 for (;;)
660 *llp = data;
661 }
wdenk64519362004-07-11 17:40:54 +0000662 if (size == 4) {
York Sund8a405d2014-02-12 15:55:35 -0800663 longp = (u32 *)buf;
wdenk64519362004-07-11 17:40:54 +0000664 for (;;)
665 *longp = data;
York Sun6c480012014-02-26 17:03:19 -0800666 }
wdenk64519362004-07-11 17:40:54 +0000667 if (size == 2) {
York Sund8a405d2014-02-12 15:55:35 -0800668 shortp = (u16 *)buf;
wdenk64519362004-07-11 17:40:54 +0000669 for (;;)
670 *shortp = data;
671 }
York Sund8a405d2014-02-12 15:55:35 -0800672 cp = (u8 *)buf;
wdenk64519362004-07-11 17:40:54 +0000673 for (;;)
674 *cp = data;
675 }
676
Simon Glassd1c117b2020-06-02 19:26:46 -0600677 if (SUPPORT_64BIT_DATA && size == 8) {
York Sun6c480012014-02-26 17:03:19 -0800678 for (;;) {
Simon Glassd1c117b2020-06-02 19:26:46 -0600679 llp = (ulong *)buf;
York Sun6c480012014-02-26 17:03:19 -0800680 i = length;
681 while (i-- > 0)
682 *llp++ = data;
683 }
684 }
wdenk64519362004-07-11 17:40:54 +0000685 if (size == 4) {
686 for (;;) {
York Sund8a405d2014-02-12 15:55:35 -0800687 longp = (u32 *)buf;
wdenk64519362004-07-11 17:40:54 +0000688 i = length;
689 while (i-- > 0)
690 *longp++ = data;
691 }
692 }
693 if (size == 2) {
694 for (;;) {
York Sund8a405d2014-02-12 15:55:35 -0800695 shortp = (u16 *)buf;
wdenk64519362004-07-11 17:40:54 +0000696 i = length;
697 while (i-- > 0)
698 *shortp++ = data;
699 }
700 }
701 for (;;) {
York Sund8a405d2014-02-12 15:55:35 -0800702 cp = (u8 *)buf;
wdenk64519362004-07-11 17:40:54 +0000703 i = length;
704 while (i-- > 0)
705 *cp++ = data;
706 }
707}
708#endif /* CONFIG_LOOPW */
709
Tom Rinia5f02702013-03-12 10:07:19 -0400710#ifdef CONFIG_CMD_MEMTEST
Simon Glassad02a012013-02-28 17:47:14 +0000711static ulong mem_test_alt(vu_long *buf, ulong start_addr, ulong end_addr,
712 vu_long *dummy)
wdenk38635852002-08-27 05:55:31 +0000713{
Simon Glass8f2c7582013-02-24 17:33:16 +0000714 vu_long *addr;
Simon Glass8f2c7582013-02-24 17:33:16 +0000715 ulong errs = 0;
716 ulong val, readback;
717 int j;
Simon Glass8f2c7582013-02-24 17:33:16 +0000718 vu_long offset;
719 vu_long test_offset;
720 vu_long pattern;
721 vu_long temp;
722 vu_long anti_pattern;
723 vu_long num_words;
wdenk38635852002-08-27 05:55:31 +0000724 static const ulong bitpattern[] = {
725 0x00000001, /* single bit */
726 0x00000003, /* two adjacent bits */
727 0x00000007, /* three adjacent bits */
728 0x0000000F, /* four adjacent bits */
729 0x00000005, /* two non-adjacent bits */
730 0x00000015, /* three non-adjacent bits */
731 0x00000055, /* four non-adjacent bits */
732 0xaaaaaaaa, /* alternating 1/0 */
733 };
wdenk38635852002-08-27 05:55:31 +0000734
Simon Glassad02a012013-02-28 17:47:14 +0000735 num_words = (end_addr - start_addr) / sizeof(vu_long);
Simon Glass51075252013-02-24 17:33:20 +0000736
Simon Glass130103e2013-02-24 17:33:18 +0000737 /*
738 * Data line test: write a pattern to the first
739 * location, write the 1's complement to a 'parking'
740 * address (changes the state of the data bus so a
741 * floating bus doesn't give a false OK), and then
742 * read the value back. Note that we read it back
743 * into a variable because the next time we read it,
744 * it might be right (been there, tough to explain to
745 * the quality guys why it prints a failure when the
746 * "is" and "should be" are obviously the same in the
747 * error message).
748 *
749 * Rather than exhaustively testing, we test some
750 * patterns by shifting '1' bits through a field of
751 * '0's and '0' bits through a field of '1's (i.e.
752 * pattern and ~pattern).
753 */
Simon Glassad02a012013-02-28 17:47:14 +0000754 addr = buf;
Simon Glass130103e2013-02-24 17:33:18 +0000755 for (j = 0; j < sizeof(bitpattern) / sizeof(bitpattern[0]); j++) {
756 val = bitpattern[j];
757 for (; val != 0; val <<= 1) {
Simon Glassad02a012013-02-28 17:47:14 +0000758 *addr = val;
Simon Glass8f2c7582013-02-24 17:33:16 +0000759 *dummy = ~val; /* clear the test data off the bus */
wdenk38635852002-08-27 05:55:31 +0000760 readback = *addr;
Simon Glass130103e2013-02-24 17:33:18 +0000761 if (readback != val) {
Simon Glass8f2c7582013-02-24 17:33:16 +0000762 printf("FAILURE (data line): "
763 "expected %08lx, actual %08lx\n",
764 val, readback);
765 errs++;
Simon Glasseb4598b2013-02-24 17:33:19 +0000766 if (ctrlc())
Simon Glass57ee4432013-02-24 17:33:17 +0000767 return -1;
wdenk38635852002-08-27 05:55:31 +0000768 }
769 *addr = ~val;
770 *dummy = val;
771 readback = *addr;
Simon Glass8f2c7582013-02-24 17:33:16 +0000772 if (readback != ~val) {
773 printf("FAILURE (data line): "
774 "Is %08lx, should be %08lx\n",
775 readback, ~val);
776 errs++;
Simon Glasseb4598b2013-02-24 17:33:19 +0000777 if (ctrlc())
Simon Glass57ee4432013-02-24 17:33:17 +0000778 return -1;
wdenk38635852002-08-27 05:55:31 +0000779 }
wdenk38635852002-08-27 05:55:31 +0000780 }
Simon Glass130103e2013-02-24 17:33:18 +0000781 }
wdenk38635852002-08-27 05:55:31 +0000782
Simon Glass130103e2013-02-24 17:33:18 +0000783 /*
784 * Based on code whose Original Author and Copyright
785 * information follows: Copyright (c) 1998 by Michael
786 * Barr. This software is placed into the public
787 * domain and may be used for any purpose. However,
788 * this notice must not be changed or removed and no
789 * warranty is either expressed or implied by its
790 * publication or distribution.
791 */
wdenk38635852002-08-27 05:55:31 +0000792
Simon Glass130103e2013-02-24 17:33:18 +0000793 /*
794 * Address line test
wdenk38635852002-08-27 05:55:31 +0000795
Simon Glass130103e2013-02-24 17:33:18 +0000796 * Description: Test the address bus wiring in a
797 * memory region by performing a walking
798 * 1's test on the relevant bits of the
799 * address and checking for aliasing.
800 * This test will find single-bit
801 * address failures such as stuck-high,
802 * stuck-low, and shorted pins. The base
803 * address and size of the region are
804 * selected by the caller.
wdenk38635852002-08-27 05:55:31 +0000805
Simon Glass130103e2013-02-24 17:33:18 +0000806 * Notes: For best results, the selected base
807 * address should have enough LSB 0's to
808 * guarantee single address bit changes.
809 * For example, to test a 64-Kbyte
810 * region, select a base address on a
811 * 64-Kbyte boundary. Also, select the
812 * region size as a power-of-two if at
813 * all possible.
814 *
815 * Returns: 0 if the test succeeds, 1 if the test fails.
816 */
Simon Glass130103e2013-02-24 17:33:18 +0000817 pattern = (vu_long) 0xaaaaaaaa;
818 anti_pattern = (vu_long) 0x55555555;
wdenk38635852002-08-27 05:55:31 +0000819
Simon Glassad02a012013-02-28 17:47:14 +0000820 debug("%s:%d: length = 0x%.8lx\n", __func__, __LINE__, num_words);
Simon Glass130103e2013-02-24 17:33:18 +0000821 /*
822 * Write the default pattern at each of the
823 * power-of-two offsets.
824 */
Simon Glassad02a012013-02-28 17:47:14 +0000825 for (offset = 1; offset < num_words; offset <<= 1)
826 addr[offset] = pattern;
Simon Glass130103e2013-02-24 17:33:18 +0000827
828 /*
829 * Check for address bits stuck high.
830 */
831 test_offset = 0;
Simon Glassad02a012013-02-28 17:47:14 +0000832 addr[test_offset] = anti_pattern;
Simon Glass130103e2013-02-24 17:33:18 +0000833
Simon Glassad02a012013-02-28 17:47:14 +0000834 for (offset = 1; offset < num_words; offset <<= 1) {
835 temp = addr[offset];
Simon Glass130103e2013-02-24 17:33:18 +0000836 if (temp != pattern) {
Simon Glass8f2c7582013-02-24 17:33:16 +0000837 printf("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000838 " expected 0x%.8lx, actual 0x%.8lx\n",
David Fengf40b2212014-02-12 16:10:08 +0800839 start_addr + offset*sizeof(vu_long),
840 pattern, temp);
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400841 errs++;
Simon Glasseb4598b2013-02-24 17:33:19 +0000842 if (ctrlc())
Simon Glass130103e2013-02-24 17:33:18 +0000843 return -1;
wdenk38635852002-08-27 05:55:31 +0000844 }
Simon Glass130103e2013-02-24 17:33:18 +0000845 }
Simon Glassad02a012013-02-28 17:47:14 +0000846 addr[test_offset] = pattern;
Simon Glass130103e2013-02-24 17:33:18 +0000847 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000848
Simon Glass130103e2013-02-24 17:33:18 +0000849 /*
850 * Check for addr bits stuck low or shorted.
851 */
Simon Glassad02a012013-02-28 17:47:14 +0000852 for (test_offset = 1; test_offset < num_words; test_offset <<= 1) {
853 addr[test_offset] = anti_pattern;
wdenk38635852002-08-27 05:55:31 +0000854
Simon Glassad02a012013-02-28 17:47:14 +0000855 for (offset = 1; offset < num_words; offset <<= 1) {
856 temp = addr[offset];
wdenk38635852002-08-27 05:55:31 +0000857 if ((temp != pattern) && (offset != test_offset)) {
Simon Glass130103e2013-02-24 17:33:18 +0000858 printf("\nFAILURE: Address bit stuck low or"
859 " shorted @ 0x%.8lx: expected 0x%.8lx,"
860 " actual 0x%.8lx\n",
David Fengf40b2212014-02-12 16:10:08 +0800861 start_addr + offset*sizeof(vu_long),
862 pattern, temp);
Simon Glass130103e2013-02-24 17:33:18 +0000863 errs++;
Simon Glasseb4598b2013-02-24 17:33:19 +0000864 if (ctrlc())
Simon Glass130103e2013-02-24 17:33:18 +0000865 return -1;
wdenk38635852002-08-27 05:55:31 +0000866 }
wdenk38635852002-08-27 05:55:31 +0000867 }
Simon Glassad02a012013-02-28 17:47:14 +0000868 addr[test_offset] = pattern;
Simon Glass130103e2013-02-24 17:33:18 +0000869 }
wdenk38635852002-08-27 05:55:31 +0000870
Simon Glass130103e2013-02-24 17:33:18 +0000871 /*
872 * Description: Test the integrity of a physical
873 * memory device by performing an
874 * increment/decrement test over the
875 * entire region. In the process every
876 * storage bit in the device is tested
877 * as a zero and a one. The base address
878 * and the size of the region are
879 * selected by the caller.
880 *
881 * Returns: 0 if the test succeeds, 1 if the test fails.
882 */
Simon Glassad02a012013-02-28 17:47:14 +0000883 num_words++;
wdenk38635852002-08-27 05:55:31 +0000884
Simon Glass130103e2013-02-24 17:33:18 +0000885 /*
886 * Fill memory with a known pattern.
887 */
888 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
889 WATCHDOG_RESET();
Simon Glassad02a012013-02-28 17:47:14 +0000890 addr[offset] = pattern;
Simon Glass130103e2013-02-24 17:33:18 +0000891 }
wdenk38635852002-08-27 05:55:31 +0000892
Simon Glass130103e2013-02-24 17:33:18 +0000893 /*
894 * Check each location and invert it for the second pass.
895 */
896 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
897 WATCHDOG_RESET();
Simon Glassad02a012013-02-28 17:47:14 +0000898 temp = addr[offset];
Simon Glass130103e2013-02-24 17:33:18 +0000899 if (temp != pattern) {
Simon Glass8f2c7582013-02-24 17:33:16 +0000900 printf("\nFAILURE (read/write) @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000901 " expected 0x%.8lx, actual 0x%.8lx)\n",
David Fengf40b2212014-02-12 16:10:08 +0800902 start_addr + offset*sizeof(vu_long),
903 pattern, temp);
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400904 errs++;
Simon Glasseb4598b2013-02-24 17:33:19 +0000905 if (ctrlc())
Simon Glass57ee4432013-02-24 17:33:17 +0000906 return -1;
wdenk38635852002-08-27 05:55:31 +0000907 }
908
Simon Glass130103e2013-02-24 17:33:18 +0000909 anti_pattern = ~pattern;
Simon Glassad02a012013-02-28 17:47:14 +0000910 addr[offset] = anti_pattern;
Simon Glass130103e2013-02-24 17:33:18 +0000911 }
912
913 /*
914 * Check each location for the inverted pattern and zero it.
915 */
916 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
917 WATCHDOG_RESET();
918 anti_pattern = ~pattern;
Simon Glassad02a012013-02-28 17:47:14 +0000919 temp = addr[offset];
Simon Glass130103e2013-02-24 17:33:18 +0000920 if (temp != anti_pattern) {
Simon Glass8f2c7582013-02-24 17:33:16 +0000921 printf("\nFAILURE (read/write): @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000922 " expected 0x%.8lx, actual 0x%.8lx)\n",
David Fengf40b2212014-02-12 16:10:08 +0800923 start_addr + offset*sizeof(vu_long),
924 anti_pattern, temp);
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400925 errs++;
Simon Glasseb4598b2013-02-24 17:33:19 +0000926 if (ctrlc())
Simon Glass57ee4432013-02-24 17:33:17 +0000927 return -1;
wdenk38635852002-08-27 05:55:31 +0000928 }
Simon Glassad02a012013-02-28 17:47:14 +0000929 addr[offset] = 0;
Simon Glass130103e2013-02-24 17:33:18 +0000930 }
Simon Glass57ee4432013-02-24 17:33:17 +0000931
Rasmus Villemoesabd06c92016-01-07 11:36:04 +0100932 return errs;
Simon Glass8f2c7582013-02-24 17:33:16 +0000933}
934
Stefan Roesefbfd5a12020-03-05 07:21:32 +0100935static int compare_regions(volatile unsigned long *bufa,
936 volatile unsigned long *bufb, size_t count)
937{
938 volatile unsigned long *p1 = bufa;
939 volatile unsigned long *p2 = bufb;
940 int errs = 0;
941 size_t i;
942
943 for (i = 0; i < count; i++, p1++, p2++) {
944 if (*p1 != *p2) {
945 printf("FAILURE: 0x%08lx != 0x%08lx (delta=0x%08lx -> bit %ld) at offset 0x%08lx\n",
946 (unsigned long)*p1, (unsigned long)*p2,
947 *p1 ^ *p2, __ffs(*p1 ^ *p2),
948 (unsigned long)(i * sizeof(unsigned long)));
949 errs++;
950 }
951 }
952
953 return errs;
954}
955
956static ulong test_bitflip_comparison(volatile unsigned long *bufa,
957 volatile unsigned long *bufb, size_t count)
958{
959 volatile unsigned long *p1 = bufa;
960 volatile unsigned long *p2 = bufb;
961 unsigned int j, k;
962 unsigned long q;
963 size_t i;
964 int max;
965 int errs = 0;
966
967 max = sizeof(unsigned long) * 8;
968 for (k = 0; k < max; k++) {
969 q = 0x00000001L << k;
970 for (j = 0; j < 8; j++) {
971 WATCHDOG_RESET();
972 q = ~q;
973 p1 = (volatile unsigned long *)bufa;
974 p2 = (volatile unsigned long *)bufb;
975 for (i = 0; i < count; i++)
976 *p1++ = *p2++ = (i % 2) == 0 ? q : ~q;
977
978 errs += compare_regions(bufa, bufb, count);
979 }
980
981 if (ctrlc())
982 return -1UL;
983 }
984
985 return errs;
986}
987
Simon Glassad02a012013-02-28 17:47:14 +0000988static ulong mem_test_quick(vu_long *buf, ulong start_addr, ulong end_addr,
989 vu_long pattern, int iteration)
Simon Glass8f2c7582013-02-24 17:33:16 +0000990{
Simon Glassad02a012013-02-28 17:47:14 +0000991 vu_long *end;
Simon Glass8f2c7582013-02-24 17:33:16 +0000992 vu_long *addr;
Simon Glass8f2c7582013-02-24 17:33:16 +0000993 ulong errs = 0;
Simon Glassad02a012013-02-28 17:47:14 +0000994 ulong incr, length;
Simon Glass8f2c7582013-02-24 17:33:16 +0000995 ulong val, readback;
wdenk38635852002-08-27 05:55:31 +0000996
Simon Glass57ee4432013-02-24 17:33:17 +0000997 /* Alternate the pattern */
wdenk38635852002-08-27 05:55:31 +0000998 incr = 1;
Simon Glass57ee4432013-02-24 17:33:17 +0000999 if (iteration & 1) {
1000 incr = -incr;
1001 /*
1002 * Flip the pattern each time to make lots of zeros and
1003 * then, the next time, lots of ones. We decrement
1004 * the "negative" patterns and increment the "positive"
1005 * patterns to preserve this feature.
1006 */
1007 if (pattern & 0x80000000)
1008 pattern = -pattern; /* complement & increment */
1009 else
1010 pattern = ~pattern;
1011 }
Simon Glassad02a012013-02-28 17:47:14 +00001012 length = (end_addr - start_addr) / sizeof(ulong);
1013 end = buf + length;
Simon Glass130103e2013-02-24 17:33:18 +00001014 printf("\rPattern %08lX Writing..."
1015 "%12s"
1016 "\b\b\b\b\b\b\b\b\b\b",
1017 pattern, "");
wdenk38635852002-08-27 05:55:31 +00001018
Simon Glassad02a012013-02-28 17:47:14 +00001019 for (addr = buf, val = pattern; addr < end; addr++) {
Simon Glass130103e2013-02-24 17:33:18 +00001020 WATCHDOG_RESET();
1021 *addr = val;
1022 val += incr;
1023 }
wdenk38635852002-08-27 05:55:31 +00001024
Simon Glass130103e2013-02-24 17:33:18 +00001025 puts("Reading...");
wdenk38635852002-08-27 05:55:31 +00001026
Simon Glassad02a012013-02-28 17:47:14 +00001027 for (addr = buf, val = pattern; addr < end; addr++) {
Simon Glass130103e2013-02-24 17:33:18 +00001028 WATCHDOG_RESET();
1029 readback = *addr;
1030 if (readback != val) {
Simon Glassad02a012013-02-28 17:47:14 +00001031 ulong offset = addr - buf;
1032
Simon Glass130103e2013-02-24 17:33:18 +00001033 printf("\nMem error @ 0x%08X: "
1034 "found %08lX, expected %08lX\n",
David Fengf40b2212014-02-12 16:10:08 +08001035 (uint)(uintptr_t)(start_addr + offset*sizeof(vu_long)),
Simon Glassad02a012013-02-28 17:47:14 +00001036 readback, val);
Simon Glass130103e2013-02-24 17:33:18 +00001037 errs++;
Simon Glasseb4598b2013-02-24 17:33:19 +00001038 if (ctrlc())
Simon Glass130103e2013-02-24 17:33:18 +00001039 return -1;
wdenk38635852002-08-27 05:55:31 +00001040 }
Simon Glass130103e2013-02-24 17:33:18 +00001041 val += incr;
1042 }
wdenk38635852002-08-27 05:55:31 +00001043
Rasmus Villemoesabd06c92016-01-07 11:36:04 +01001044 return errs;
Simon Glass8f2c7582013-02-24 17:33:16 +00001045}
1046
1047/*
1048 * Perform a memory test. A more complete alternative test can be
1049 * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
1050 * interrupted by ctrl-c or by a failure of one of the sub-tests.
1051 */
Simon Glassed38aef2020-05-10 11:40:03 -06001052static int do_mem_mtest(struct cmd_tbl *cmdtp, int flag, int argc,
1053 char *const argv[])
Simon Glass8f2c7582013-02-24 17:33:16 +00001054{
Simon Glass51075252013-02-24 17:33:20 +00001055 ulong start, end;
Michal Simek9eaf6442020-05-04 13:54:40 +02001056 vu_long scratch_space;
1057 vu_long *buf, *dummy = &scratch_space;
Tom Rinib9180882015-04-07 09:38:54 -04001058 ulong iteration_limit = 0;
Stefan Roese32265fc2020-03-05 07:21:29 +01001059 ulong count = 0;
Simon Glass57ee4432013-02-24 17:33:17 +00001060 ulong errs = 0; /* number of errors, or -1 if interrupted */
Pavel Machekd319f592015-04-01 13:50:41 +02001061 ulong pattern = 0;
Simon Glass57ee4432013-02-24 17:33:17 +00001062 int iteration;
Simon Glass8f2c7582013-02-24 17:33:16 +00001063
Pavel Machekd319f592015-04-01 13:50:41 +02001064 start = CONFIG_SYS_MEMTEST_START;
1065 end = CONFIG_SYS_MEMTEST_END;
1066
Simon Glass8f2c7582013-02-24 17:33:16 +00001067 if (argc > 1)
Pavel Machekd319f592015-04-01 13:50:41 +02001068 if (strict_strtoul(argv[1], 16, &start) < 0)
1069 return CMD_RET_USAGE;
Simon Glass8f2c7582013-02-24 17:33:16 +00001070
1071 if (argc > 2)
Pavel Machekd319f592015-04-01 13:50:41 +02001072 if (strict_strtoul(argv[2], 16, &end) < 0)
1073 return CMD_RET_USAGE;
Simon Glass8f2c7582013-02-24 17:33:16 +00001074
1075 if (argc > 3)
Pavel Machekd319f592015-04-01 13:50:41 +02001076 if (strict_strtoul(argv[3], 16, &pattern) < 0)
1077 return CMD_RET_USAGE;
Simon Glass8f2c7582013-02-24 17:33:16 +00001078
1079 if (argc > 4)
Pavel Machekd319f592015-04-01 13:50:41 +02001080 if (strict_strtoul(argv[4], 16, &iteration_limit) < 0)
1081 return CMD_RET_USAGE;
1082
1083 if (end < start) {
1084 printf("Refusing to do empty test\n");
1085 return -1;
1086 }
Simon Glass8f2c7582013-02-24 17:33:16 +00001087
Michal Simek45f8e332016-02-24 08:36:02 +01001088 printf("Testing %08lx ... %08lx:\n", start, end);
Simon Glass51075252013-02-24 17:33:20 +00001089 debug("%s:%d: start %#08lx end %#08lx\n", __func__, __LINE__,
1090 start, end);
Simon Glass57ee4432013-02-24 17:33:17 +00001091
Simon Glassad02a012013-02-28 17:47:14 +00001092 buf = map_sysmem(start, end - start);
Simon Glass57ee4432013-02-24 17:33:17 +00001093 for (iteration = 0;
1094 !iteration_limit || iteration < iteration_limit;
1095 iteration++) {
1096 if (ctrlc()) {
Simon Glass57ee4432013-02-24 17:33:17 +00001097 errs = -1UL;
1098 break;
1099 }
1100
1101 printf("Iteration: %6d\r", iteration + 1);
1102 debug("\n");
Stefan Roese1eb8b1e2020-03-05 07:21:31 +01001103 if (IS_ENABLED(CONFIG_SYS_ALT_MEMTEST)) {
Simon Glassad02a012013-02-28 17:47:14 +00001104 errs = mem_test_alt(buf, start, end, dummy);
Stefan Roesefbfd5a12020-03-05 07:21:32 +01001105 if (errs == -1UL)
1106 break;
1107 count += errs;
1108 errs = test_bitflip_comparison(buf,
1109 buf + (end - start) / 2,
1110 (end - start) /
1111 sizeof(unsigned long));
Simon Glassad02a012013-02-28 17:47:14 +00001112 } else {
1113 errs = mem_test_quick(buf, start, end, pattern,
1114 iteration);
1115 }
1116 if (errs == -1UL)
1117 break;
Stefan Roese32265fc2020-03-05 07:21:29 +01001118 count += errs;
Simon Glassad02a012013-02-28 17:47:14 +00001119 }
1120
Stefan Roesec1d47db2020-03-05 07:21:30 +01001121 unmap_sysmem((void *)buf);
Simon Glass57ee4432013-02-24 17:33:17 +00001122
1123 if (errs == -1UL) {
Simon Glasseb4598b2013-02-24 17:33:19 +00001124 /* Memory test was aborted - write a newline to finish off */
1125 putc('\n');
Simon Glass57ee4432013-02-24 17:33:17 +00001126 }
Stefan Roese32265fc2020-03-05 07:21:29 +01001127 printf("Tested %d iteration(s) with %lu errors.\n", iteration, count);
Simon Glass8f2c7582013-02-24 17:33:16 +00001128
Stefan Roese32265fc2020-03-05 07:21:29 +01001129 return errs != 0;
wdenk38635852002-08-27 05:55:31 +00001130}
Wolfgang Denk9d009282013-03-08 10:51:32 +00001131#endif /* CONFIG_CMD_MEMTEST */
wdenk38635852002-08-27 05:55:31 +00001132
1133/* Modify memory.
1134 *
1135 * Syntax:
York Sun6c480012014-02-26 17:03:19 -08001136 * mm{.b, .w, .l, .q} {addr}
wdenk38635852002-08-27 05:55:31 +00001137 */
1138static int
Simon Glassed38aef2020-05-10 11:40:03 -06001139mod_mem(struct cmd_tbl *cmdtp, int incrflag, int flag, int argc,
1140 char *const argv[])
wdenk38635852002-08-27 05:55:31 +00001141{
York Sun6c480012014-02-26 17:03:19 -08001142 ulong addr;
Simon Glassd1c117b2020-06-02 19:26:46 -06001143 ulong i; /* 64-bit if SUPPORT_64BIT_DATA */
wdenk874ac262003-07-24 23:38:38 +00001144 int nbytes, size;
Simon Glasse6d0ca22013-02-24 17:33:15 +00001145 void *ptr = NULL;
wdenk38635852002-08-27 05:55:31 +00001146
Wolfgang Denk3b683112010-07-17 01:06:04 +02001147 if (argc != 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001148 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +00001149
Simon Glass09007c42014-04-10 20:01:31 -06001150 bootretry_reset_cmd_timeout(); /* got a good command to get here */
wdenk38635852002-08-27 05:55:31 +00001151 /* We use the last specified parameters, unless new ones are
1152 * entered.
1153 */
1154 addr = mm_last_addr;
1155 size = mm_last_size;
1156
1157 if ((flag & CMD_FLAG_REPEAT) == 0) {
1158 /* New command specified. Check for a size specification.
1159 * Defaults to long if no or incorrect specification.
1160 */
wdenk874ac262003-07-24 23:38:38 +00001161 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
1162 return 1;
wdenk38635852002-08-27 05:55:31 +00001163
1164 /* Address is specified since argc > 1
1165 */
1166 addr = simple_strtoul(argv[1], NULL, 16);
1167 addr += base_address;
1168 }
wdenk381669a2003-06-16 23:50:08 +00001169
wdenk38635852002-08-27 05:55:31 +00001170 /* Print the address, followed by value. Then accept input for
1171 * the next value. A non-converted value exits.
1172 */
1173 do {
Simon Glasse6d0ca22013-02-24 17:33:15 +00001174 ptr = map_sysmem(addr, size);
wdenk38635852002-08-27 05:55:31 +00001175 printf("%08lx:", addr);
1176 if (size == 4)
York Sund8a405d2014-02-12 15:55:35 -08001177 printf(" %08x", *((u32 *)ptr));
Simon Glassd1c117b2020-06-02 19:26:46 -06001178 else if (SUPPORT_64BIT_DATA && size == 8)
1179 printf(" %0lx", *((ulong *)ptr));
wdenk38635852002-08-27 05:55:31 +00001180 else if (size == 2)
York Sund8a405d2014-02-12 15:55:35 -08001181 printf(" %04x", *((u16 *)ptr));
wdenk38635852002-08-27 05:55:31 +00001182 else
York Sund8a405d2014-02-12 15:55:35 -08001183 printf(" %02x", *((u8 *)ptr));
wdenk38635852002-08-27 05:55:31 +00001184
Simon Glassbe6aafc2014-04-10 20:01:27 -06001185 nbytes = cli_readline(" ? ");
wdenk38635852002-08-27 05:55:31 +00001186 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
1187 /* <CR> pressed as only input, don't modify current
1188 * location and move to next. "-" pressed will go back.
1189 */
1190 if (incrflag)
1191 addr += nbytes ? -size : size;
1192 nbytes = 1;
Simon Glass09007c42014-04-10 20:01:31 -06001193 /* good enough to not time out */
1194 bootretry_reset_cmd_timeout();
wdenk38635852002-08-27 05:55:31 +00001195 }
1196#ifdef CONFIG_BOOT_RETRY_TIME
1197 else if (nbytes == -2) {
1198 break; /* timed out, exit the command */
1199 }
1200#endif
1201 else {
1202 char *endp;
Simon Glassd1c117b2020-06-02 19:26:46 -06001203 if (SUPPORT_64BIT_DATA)
1204 i = simple_strtoull(console_buffer, &endp, 16);
1205 else
1206 i = simple_strtoul(console_buffer, &endp, 16);
wdenk38635852002-08-27 05:55:31 +00001207 nbytes = endp - console_buffer;
1208 if (nbytes) {
wdenk38635852002-08-27 05:55:31 +00001209 /* good enough to not time out
1210 */
Simon Glass09007c42014-04-10 20:01:31 -06001211 bootretry_reset_cmd_timeout();
wdenk38635852002-08-27 05:55:31 +00001212 if (size == 4)
York Sund8a405d2014-02-12 15:55:35 -08001213 *((u32 *)ptr) = i;
Simon Glassd1c117b2020-06-02 19:26:46 -06001214 else if (SUPPORT_64BIT_DATA && size == 8)
1215 *((ulong *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001216 else if (size == 2)
York Sund8a405d2014-02-12 15:55:35 -08001217 *((u16 *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001218 else
York Sund8a405d2014-02-12 15:55:35 -08001219 *((u8 *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001220 if (incrflag)
1221 addr += size;
1222 }
1223 }
1224 } while (nbytes);
Simon Glasse6d0ca22013-02-24 17:33:15 +00001225 if (ptr)
1226 unmap_sysmem(ptr);
wdenk38635852002-08-27 05:55:31 +00001227
1228 mm_last_addr = addr;
1229 mm_last_size = size;
1230 return 0;
1231}
1232
Mike Frysinger321ab9e2010-12-21 14:19:51 -05001233#ifdef CONFIG_CMD_CRC32
1234
Simon Glassed38aef2020-05-10 11:40:03 -06001235static int do_mem_crc(struct cmd_tbl *cmdtp, int flag, int argc,
1236 char *const argv[])
wdenk38635852002-08-27 05:55:31 +00001237{
Simon Glass0bbd76f2013-02-24 20:30:22 +00001238 int flags = 0;
wdenk6203e402004-04-18 10:13:26 +00001239 int ac;
Wolfgang Denk6262d0212010-06-28 22:00:46 +02001240 char * const *av;
wdenk6203e402004-04-18 10:13:26 +00001241
Simon Glass0bbd76f2013-02-24 20:30:22 +00001242 if (argc < 3)
Simon Glassa06dfc72011-12-10 08:44:01 +00001243 return CMD_RET_USAGE;
wdenk6203e402004-04-18 10:13:26 +00001244
1245 av = argv + 1;
1246 ac = argc - 1;
Daniel Thompsona9e2c672017-05-19 17:26:58 +01001247#ifdef CONFIG_CRC32_VERIFY
wdenk6203e402004-04-18 10:13:26 +00001248 if (strcmp(*av, "-v") == 0) {
Joe Hershberger5fcee9a2015-05-05 12:23:53 -05001249 flags |= HASH_FLAG_VERIFY | HASH_FLAG_ENV;
wdenk6203e402004-04-18 10:13:26 +00001250 av++;
1251 ac--;
wdenk6203e402004-04-18 10:13:26 +00001252 }
Simon Glass0bbd76f2013-02-24 20:30:22 +00001253#endif
wdenk6203e402004-04-18 10:13:26 +00001254
Simon Glass0bbd76f2013-02-24 20:30:22 +00001255 return hash_command("crc32", flags, cmdtp, flag, ac, av);
wdenk6203e402004-04-18 10:13:26 +00001256}
wdenk6203e402004-04-18 10:13:26 +00001257
Robin Getz93d6cb02009-07-27 00:07:59 -04001258#endif
1259
Jean-Jacques Hiblotd3f09372019-07-02 14:23:26 +02001260#ifdef CONFIG_CMD_RANDOM
Simon Glassed38aef2020-05-10 11:40:03 -06001261static int do_random(struct cmd_tbl *cmdtp, int flag, int argc,
1262 char *const argv[])
Jean-Jacques Hiblotd3f09372019-07-02 14:23:26 +02001263{
1264 unsigned long addr, len;
1265 unsigned long seed; // NOT INITIALIZED ON PURPOSE
1266 unsigned int *buf, *start;
1267 unsigned char *buf8;
1268 unsigned int i;
1269
Eugeniy Paltsev82accda2020-03-20 19:38:17 +03001270 if (argc < 3 || argc > 4)
1271 return CMD_RET_USAGE;
Jean-Jacques Hiblotd3f09372019-07-02 14:23:26 +02001272
1273 len = simple_strtoul(argv[2], NULL, 16);
1274 addr = simple_strtoul(argv[1], NULL, 16);
1275
1276 if (argc == 4) {
1277 seed = simple_strtoul(argv[3], NULL, 16);
1278 if (seed == 0) {
1279 printf("The seed cannot be 0. Using 0xDEADBEEF.\n");
1280 seed = 0xDEADBEEF;
1281 }
1282 } else {
1283 seed = get_timer(0) ^ rand();
1284 }
1285
1286 srand(seed);
1287 start = map_sysmem(addr, len);
1288 buf = start;
1289 for (i = 0; i < (len / 4); i++)
1290 *buf++ = rand();
1291
1292 buf8 = (unsigned char *)buf;
1293 for (i = 0; i < (len % 4); i++)
1294 *buf8++ = rand() & 0xFF;
1295
1296 unmap_sysmem(start);
1297 printf("%lu bytes filled with random data\n", len);
Eugeniy Paltsev82accda2020-03-20 19:38:17 +03001298
1299 return CMD_RET_SUCCESS;
Jean-Jacques Hiblotd3f09372019-07-02 14:23:26 +02001300}
1301#endif
1302
wdenk57b2d802003-06-27 21:31:46 +00001303/**************************************************/
wdenkf287a242003-07-01 21:06:45 +00001304U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001305 md, 3, 1, do_mem_md,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001306 "memory display",
Simon Glass683e39e2020-06-02 19:26:45 -06001307 "[.b, .w, .l" HELP_Q "] address [# of objects]"
wdenk57b2d802003-06-27 21:31:46 +00001308);
1309
1310
wdenkf287a242003-07-01 21:06:45 +00001311U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001312 mm, 2, 1, do_mem_mm,
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001313 "memory modify (auto-incrementing address)",
Simon Glass683e39e2020-06-02 19:26:45 -06001314 "[.b, .w, .l" HELP_Q "] address"
wdenk57b2d802003-06-27 21:31:46 +00001315);
1316
1317
wdenkf287a242003-07-01 21:06:45 +00001318U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001319 nm, 2, 1, do_mem_nm,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001320 "memory modify (constant address)",
Simon Glass683e39e2020-06-02 19:26:45 -06001321 "[.b, .w, .l" HELP_Q "] address"
wdenk57b2d802003-06-27 21:31:46 +00001322);
1323
wdenkf287a242003-07-01 21:06:45 +00001324U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001325 mw, 4, 1, do_mem_mw,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001326 "memory write (fill)",
Simon Glass683e39e2020-06-02 19:26:45 -06001327 "[.b, .w, .l" HELP_Q "] address value [count]"
wdenk57b2d802003-06-27 21:31:46 +00001328);
1329
wdenkf287a242003-07-01 21:06:45 +00001330U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001331 cp, 4, 1, do_mem_cp,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001332 "memory copy",
Simon Glass683e39e2020-06-02 19:26:45 -06001333 "[.b, .w, .l" HELP_Q "] source target count"
wdenk57b2d802003-06-27 21:31:46 +00001334);
1335
wdenkf287a242003-07-01 21:06:45 +00001336U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001337 cmp, 4, 1, do_mem_cmp,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001338 "memory compare",
Simon Glass683e39e2020-06-02 19:26:45 -06001339 "[.b, .w, .l" HELP_Q "] addr1 addr2 count"
wdenk57b2d802003-06-27 21:31:46 +00001340);
Simon Glass19038de2020-06-02 19:26:49 -06001341
Simon Glass573c0fa2020-07-28 19:41:14 -06001342#ifdef CONFIG_CMD_MEM_SEARCH
Simon Glass19038de2020-06-02 19:26:49 -06001343/**************************************************/
1344U_BOOT_CMD(
1345 ms, 255, 1, do_mem_search,
1346 "memory search",
1347 "[.b, .w, .l" HELP_Q ", .s] [-q | -<n>] address #-of-objects <value>..."
Simon Glass573c0fa2020-07-28 19:41:14 -06001348 " -q = quiet, -l<val> = match limit"
Simon Glass19038de2020-06-02 19:26:49 -06001349);
1350#endif
wdenk57b2d802003-06-27 21:31:46 +00001351
Mike Frysinger321ab9e2010-12-21 14:19:51 -05001352#ifdef CONFIG_CMD_CRC32
1353
Daniel Thompsona9e2c672017-05-19 17:26:58 +01001354#ifndef CONFIG_CRC32_VERIFY
wdenk6203e402004-04-18 10:13:26 +00001355
wdenkf287a242003-07-01 21:06:45 +00001356U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001357 crc32, 4, 1, do_mem_crc,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001358 "checksum calculation",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001359 "address count [addr]\n - compute CRC32 checksum [save at addr]"
wdenk57b2d802003-06-27 21:31:46 +00001360);
1361
Daniel Thompsona9e2c672017-05-19 17:26:58 +01001362#else /* CONFIG_CRC32_VERIFY */
wdenk6203e402004-04-18 10:13:26 +00001363
1364U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001365 crc32, 5, 1, do_mem_crc,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001366 "checksum calculation",
wdenk6203e402004-04-18 10:13:26 +00001367 "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001368 "-v address count crc\n - verify crc of memory area"
wdenk6203e402004-04-18 10:13:26 +00001369);
1370
Daniel Thompsona9e2c672017-05-19 17:26:58 +01001371#endif /* CONFIG_CRC32_VERIFY */
wdenk6203e402004-04-18 10:13:26 +00001372
Mike Frysinger321ab9e2010-12-21 14:19:51 -05001373#endif
1374
Simon Glasseacd14f2012-11-30 13:01:20 +00001375#ifdef CONFIG_CMD_MEMINFO
Simon Glassed38aef2020-05-10 11:40:03 -06001376static int do_mem_info(struct cmd_tbl *cmdtp, int flag, int argc,
1377 char *const argv[])
Simon Glasseacd14f2012-11-30 13:01:20 +00001378{
Simon Glass320a5d42019-11-14 12:57:44 -07001379 puts("DRAM: ");
1380 print_size(gd->ram_size, "\n");
Simon Glasseacd14f2012-11-30 13:01:20 +00001381
1382 return 0;
1383}
1384#endif
1385
wdenkf287a242003-07-01 21:06:45 +00001386U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001387 base, 2, 1, do_mem_base,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001388 "print or set address offset",
wdenk57b2d802003-06-27 21:31:46 +00001389 "\n - print address offset for memory commands\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001390 "base off\n - set address offset for memory commands to 'off'"
wdenk57b2d802003-06-27 21:31:46 +00001391);
1392
wdenkf287a242003-07-01 21:06:45 +00001393U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001394 loop, 3, 1, do_mem_loop,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001395 "infinite loop on address range",
Simon Glass683e39e2020-06-02 19:26:45 -06001396 "[.b, .w, .l" HELP_Q "] address number_of_objects"
wdenk57b2d802003-06-27 21:31:46 +00001397);
wdenk64519362004-07-11 17:40:54 +00001398
1399#ifdef CONFIG_LOOPW
1400U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001401 loopw, 4, 1, do_mem_loopw,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001402 "infinite write loop on address range",
Simon Glass683e39e2020-06-02 19:26:45 -06001403 "[.b, .w, .l" HELP_Q "] address number_of_objects data_to_write"
wdenk64519362004-07-11 17:40:54 +00001404);
1405#endif /* CONFIG_LOOPW */
wdenk57b2d802003-06-27 21:31:46 +00001406
Wolfgang Denk9d009282013-03-08 10:51:32 +00001407#ifdef CONFIG_CMD_MEMTEST
wdenkf287a242003-07-01 21:06:45 +00001408U_BOOT_CMD(
Dirk Eibachb2659892008-12-16 14:51:56 +01001409 mtest, 5, 1, do_mem_mtest,
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001410 "simple RAM read/write test",
1411 "[start [end [pattern [iterations]]]]"
wdenk57b2d802003-06-27 21:31:46 +00001412);
Wolfgang Denk9d009282013-03-08 10:51:32 +00001413#endif /* CONFIG_CMD_MEMTEST */
stroesefb5e0ac2004-12-16 17:42:39 +00001414
Joel Johnsondb5a97e2020-01-29 09:17:18 -07001415#ifdef CONFIG_CMD_MX_CYCLIC
stroesefb5e0ac2004-12-16 17:42:39 +00001416U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001417 mdc, 4, 1, do_mem_mdc,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001418 "memory display cyclic",
Simon Glass683e39e2020-06-02 19:26:45 -06001419 "[.b, .w, .l" HELP_Q "] address count delay(ms)"
stroesefb5e0ac2004-12-16 17:42:39 +00001420);
1421
1422U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001423 mwc, 4, 1, do_mem_mwc,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001424 "memory write cyclic",
Simon Glass683e39e2020-06-02 19:26:45 -06001425 "[.b, .w, .l" HELP_Q "] address value delay(ms)"
stroesefb5e0ac2004-12-16 17:42:39 +00001426);
Joel Johnsondb5a97e2020-01-29 09:17:18 -07001427#endif /* CONFIG_CMD_MX_CYCLIC */
Simon Glasseacd14f2012-11-30 13:01:20 +00001428
1429#ifdef CONFIG_CMD_MEMINFO
1430U_BOOT_CMD(
1431 meminfo, 3, 1, do_mem_info,
1432 "display memory information",
1433 ""
1434);
1435#endif
Jean-Jacques Hiblotd3f09372019-07-02 14:23:26 +02001436
1437#ifdef CONFIG_CMD_RANDOM
1438U_BOOT_CMD(
1439 random, 4, 0, do_random,
1440 "fill memory with random pattern",
1441 "<addr> <len> [<seed>]\n"
1442 " - Fill 'len' bytes of memory starting at 'addr' with random data\n"
1443);
1444#endif