blob: 9e716776393ab1af2ac1d9709dcf1bd882d5aeb8 [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
Simon Glassa73bda42015-11-08 23:47:45 -070013#include <console.h>
Simon Glass399ed9a2014-04-10 20:01:30 -060014#include <bootretry.h>
Simon Glassdec3c012014-04-10 20:01:25 -060015#include <cli.h>
wdenk38635852002-08-27 05:55:31 +000016#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070017#include <console.h>
Simon Glass1ab16922022-07-31 12:28:48 -060018#include <display_options.h>
Tom Rini063c9382022-07-23 13:05:03 -040019#ifdef CONFIG_MTD_NOR_FLASH
Simon Glassa606ffc2019-12-28 10:44:40 -070020#include <flash.h>
Tom Rini063c9382022-07-23 13:05:03 -040021#endif
Simon Glass0bbd76f2013-02-24 20:30:22 +000022#include <hash.h>
Simon Glass0f2af882020-05-10 11:40:05 -060023#include <log.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050024#include <mapmem.h>
Simon Glass274e0b02020-05-10 11:39:56 -060025#include <rand.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060026#include <time.h>
Sergei Poselenov474dbb82008-04-09 16:09:41 +020027#include <watchdog.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060028#include <asm/global_data.h>
Simon Glasse6d0ca22013-02-24 17:33:15 +000029#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060030#include <linux/bitops.h>
Simon Glasseacd14f2012-11-30 13:01:20 +000031#include <linux/compiler.h>
Simon Glass19038de2020-06-02 19:26:49 -060032#include <linux/ctype.h>
Simon Glassdbd79542020-05-10 11:40:11 -060033#include <linux/delay.h>
Simon Glasseacd14f2012-11-30 13:01:20 +000034
35DECLARE_GLOBAL_DATA_PTR;
wdenk38635852002-08-27 05:55:31 +000036
Simon Glass683e39e2020-06-02 19:26:45 -060037/* Create a compile-time value */
Rasmus Villemoesa6f37532024-01-03 11:47:08 +010038#if MEM_SUPPORT_64BIT_DATA
Simon Glass683e39e2020-06-02 19:26:45 -060039#define HELP_Q ", .q"
40#else
41#define HELP_Q ""
42#endif
43
Simon Glassed38aef2020-05-10 11:40:03 -060044static int mod_mem(struct cmd_tbl *, int, int, int, char * const []);
wdenk38635852002-08-27 05:55:31 +000045
46/* Display values from last command.
47 * Memory modify remembered values are different from display memory.
48 */
Scott Wood3ec02882015-03-19 09:43:12 -070049static ulong dp_last_addr, dp_last_size;
50static ulong dp_last_length = 0x40;
51static ulong mm_last_addr, mm_last_size;
wdenk38635852002-08-27 05:55:31 +000052
53static ulong base_address = 0;
Simon Glass573c0fa2020-07-28 19:41:14 -060054#ifdef CONFIG_CMD_MEM_SEARCH
55static ulong dp_last_ms_length;
Simon Glass19038de2020-06-02 19:26:49 -060056static u8 search_buf[64];
57static uint search_len;
58#endif
wdenk38635852002-08-27 05:55:31 +000059
60/* Memory Display
61 *
62 * Syntax:
York Sun6c480012014-02-26 17:03:19 -080063 * md{.b, .w, .l, .q} {addr} {len}
wdenk38635852002-08-27 05:55:31 +000064 */
65#define DISP_LINE_LEN 16
Simon Glassed38aef2020-05-10 11:40:03 -060066static int do_mem_md(struct cmd_tbl *cmdtp, int flag, int argc,
67 char *const argv[])
wdenk38635852002-08-27 05:55:31 +000068{
Tuomas Tynkkynen1b725202017-10-10 21:59:42 +030069 ulong addr, length, bytes;
70 const void *buf;
wdenk874ac262003-07-24 23:38:38 +000071 int size;
wdenk38635852002-08-27 05:55:31 +000072 int rc = 0;
73
74 /* We use the last specified parameters, unless new ones are
75 * entered.
76 */
77 addr = dp_last_addr;
78 size = dp_last_size;
79 length = dp_last_length;
80
Wolfgang Denk3b683112010-07-17 01:06:04 +020081 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +000082 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +000083
84 if ((flag & CMD_FLAG_REPEAT) == 0) {
85 /* New command specified. Check for a size specification.
86 * Defaults to long if no or incorrect specification.
87 */
wdenk874ac262003-07-24 23:38:38 +000088 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
89 return 1;
wdenk38635852002-08-27 05:55:31 +000090
91 /* Address is specified since argc > 1
92 */
Simon Glass3ff49ec2021-07-24 09:03:29 -060093 addr = hextoul(argv[1], NULL);
wdenk38635852002-08-27 05:55:31 +000094 addr += base_address;
95
96 /* If another parameter, it is the length to display.
97 * Length is the number of objects, not number of bytes.
98 */
99 if (argc > 2)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600100 length = hextoul(argv[2], NULL);
wdenk38635852002-08-27 05:55:31 +0000101 }
102
Tuomas Tynkkynen1b725202017-10-10 21:59:42 +0300103 bytes = size * length;
104 buf = map_sysmem(addr, bytes);
Simon Glasse6d0ca22013-02-24 17:33:15 +0000105
Tuomas Tynkkynen1b725202017-10-10 21:59:42 +0300106 /* Print the lines. */
107 print_buffer(addr, buf, size, length, DISP_LINE_LEN / size);
108 addr += bytes;
109 unmap_sysmem(buf);
wdenk38635852002-08-27 05:55:31 +0000110
111 dp_last_addr = addr;
112 dp_last_length = length;
113 dp_last_size = size;
114 return (rc);
115}
116
Simon Glassed38aef2020-05-10 11:40:03 -0600117static int do_mem_mm(struct cmd_tbl *cmdtp, int flag, int argc,
118 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000119{
120 return mod_mem (cmdtp, 1, flag, argc, argv);
121}
Simon Glassed38aef2020-05-10 11:40:03 -0600122
123static int do_mem_nm(struct cmd_tbl *cmdtp, int flag, int argc,
124 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000125{
126 return mod_mem (cmdtp, 0, flag, argc, argv);
127}
128
Simon Glassed38aef2020-05-10 11:40:03 -0600129static int do_mem_mw(struct cmd_tbl *cmdtp, int flag, int argc,
130 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000131{
Rasmus Villemoesa6f37532024-01-03 11:47:08 +0100132 ulong writeval; /* 64-bit if MEM_SUPPORT_64BIT_DATA */
York Sun6c480012014-02-26 17:03:19 -0800133 ulong addr, count;
wdenk874ac262003-07-24 23:38:38 +0000134 int size;
Simon Glass3c4a2872015-03-05 12:25:18 -0700135 void *buf, *start;
Simon Glasse6d0ca22013-02-24 17:33:15 +0000136 ulong bytes;
wdenk38635852002-08-27 05:55:31 +0000137
Wolfgang Denk3b683112010-07-17 01:06:04 +0200138 if ((argc < 3) || (argc > 4))
Simon Glassa06dfc72011-12-10 08:44:01 +0000139 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000140
141 /* Check for size specification.
142 */
wdenk874ac262003-07-24 23:38:38 +0000143 if ((size = cmd_get_data_size(argv[0], 4)) < 1)
144 return 1;
wdenk38635852002-08-27 05:55:31 +0000145
146 /* Address is specified since argc > 1
147 */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600148 addr = hextoul(argv[1], NULL);
wdenk38635852002-08-27 05:55:31 +0000149 addr += base_address;
150
151 /* Get the value to write.
152 */
Rasmus Villemoesa6f37532024-01-03 11:47:08 +0100153 if (MEM_SUPPORT_64BIT_DATA)
Simon Glassd1c117b2020-06-02 19:26:46 -0600154 writeval = simple_strtoull(argv[2], NULL, 16);
155 else
Simon Glass3ff49ec2021-07-24 09:03:29 -0600156 writeval = hextoul(argv[2], NULL);
wdenk38635852002-08-27 05:55:31 +0000157
158 /* Count ? */
159 if (argc == 4) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600160 count = hextoul(argv[3], NULL);
wdenk38635852002-08-27 05:55:31 +0000161 } else {
162 count = 1;
163 }
164
Simon Glasse6d0ca22013-02-24 17:33:15 +0000165 bytes = size * count;
Simon Glass3c4a2872015-03-05 12:25:18 -0700166 start = map_sysmem(addr, bytes);
167 buf = start;
wdenk38635852002-08-27 05:55:31 +0000168 while (count-- > 0) {
169 if (size == 4)
York Sund8a405d2014-02-12 15:55:35 -0800170 *((u32 *)buf) = (u32)writeval;
Rasmus Villemoesa6f37532024-01-03 11:47:08 +0100171 else if (MEM_SUPPORT_64BIT_DATA && size == 8)
Simon Glassd1c117b2020-06-02 19:26:46 -0600172 *((ulong *)buf) = writeval;
wdenk38635852002-08-27 05:55:31 +0000173 else if (size == 2)
York Sund8a405d2014-02-12 15:55:35 -0800174 *((u16 *)buf) = (u16)writeval;
wdenk38635852002-08-27 05:55:31 +0000175 else
York Sund8a405d2014-02-12 15:55:35 -0800176 *((u8 *)buf) = (u8)writeval;
Simon Glasse6d0ca22013-02-24 17:33:15 +0000177 buf += size;
wdenk38635852002-08-27 05:55:31 +0000178 }
Simon Glass3c4a2872015-03-05 12:25:18 -0700179 unmap_sysmem(start);
wdenk38635852002-08-27 05:55:31 +0000180 return 0;
181}
182
Joel Johnsondb5a97e2020-01-29 09:17:18 -0700183#ifdef CONFIG_CMD_MX_CYCLIC
Simon Glassed38aef2020-05-10 11:40:03 -0600184static int do_mem_mdc(struct cmd_tbl *cmdtp, int flag, int argc,
185 char *const argv[])
stroesefb5e0ac2004-12-16 17:42:39 +0000186{
187 int i;
188 ulong count;
189
Wolfgang Denk3b683112010-07-17 01:06:04 +0200190 if (argc < 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000191 return CMD_RET_USAGE;
stroesefb5e0ac2004-12-16 17:42:39 +0000192
Simon Glassff9b9032021-07-24 09:03:30 -0600193 count = dectoul(argv[3], NULL);
stroesefb5e0ac2004-12-16 17:42:39 +0000194
195 for (;;) {
196 do_mem_md (NULL, 0, 3, argv);
197
198 /* delay for <count> ms... */
199 for (i=0; i<count; i++)
Simon Glass0db4b942020-05-10 11:40:10 -0600200 udelay(1000);
stroesefb5e0ac2004-12-16 17:42:39 +0000201
202 /* check for ctrl-c to abort... */
203 if (ctrlc()) {
204 puts("Abort\n");
205 return 0;
206 }
207 }
208
209 return 0;
210}
211
Simon Glassed38aef2020-05-10 11:40:03 -0600212static int do_mem_mwc(struct cmd_tbl *cmdtp, int flag, int argc,
213 char *const argv[])
stroesefb5e0ac2004-12-16 17:42:39 +0000214{
215 int i;
216 ulong count;
217
Wolfgang Denk3b683112010-07-17 01:06:04 +0200218 if (argc < 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000219 return CMD_RET_USAGE;
stroesefb5e0ac2004-12-16 17:42:39 +0000220
Simon Glassff9b9032021-07-24 09:03:30 -0600221 count = dectoul(argv[3], NULL);
stroesefb5e0ac2004-12-16 17:42:39 +0000222
223 for (;;) {
224 do_mem_mw (NULL, 0, 3, argv);
225
226 /* delay for <count> ms... */
227 for (i=0; i<count; i++)
Simon Glass0db4b942020-05-10 11:40:10 -0600228 udelay(1000);
stroesefb5e0ac2004-12-16 17:42:39 +0000229
230 /* check for ctrl-c to abort... */
231 if (ctrlc()) {
232 puts("Abort\n");
233 return 0;
234 }
235 }
236
237 return 0;
238}
Joel Johnsondb5a97e2020-01-29 09:17:18 -0700239#endif /* CONFIG_CMD_MX_CYCLIC */
stroesefb5e0ac2004-12-16 17:42:39 +0000240
Simon Glassed38aef2020-05-10 11:40:03 -0600241static int do_mem_cmp(struct cmd_tbl *cmdtp, int flag, int argc,
242 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000243{
Simon Glasse6d0ca22013-02-24 17:33:15 +0000244 ulong addr1, addr2, count, ngood, bytes;
wdenk874ac262003-07-24 23:38:38 +0000245 int size;
wdenk38635852002-08-27 05:55:31 +0000246 int rcode = 0;
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000247 const char *type;
Simon Glass99a1c382024-09-01 16:26:30 -0600248 const void *buf1, *buf2, *base, *ptr1, *ptr2;
Rasmus Villemoesa6f37532024-01-03 11:47:08 +0100249 ulong word1, word2; /* 64-bit if MEM_SUPPORT_64BIT_DATA */
wdenk38635852002-08-27 05:55:31 +0000250
Wolfgang Denk3b683112010-07-17 01:06:04 +0200251 if (argc != 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000252 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000253
254 /* Check for size specification.
255 */
wdenk874ac262003-07-24 23:38:38 +0000256 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
257 return 1;
York Sun6c480012014-02-26 17:03:19 -0800258 type = size == 8 ? "double word" :
259 size == 4 ? "word" :
260 size == 2 ? "halfword" : "byte";
wdenk38635852002-08-27 05:55:31 +0000261
Simon Glass3ff49ec2021-07-24 09:03:29 -0600262 addr1 = hextoul(argv[1], NULL);
wdenk38635852002-08-27 05:55:31 +0000263 addr1 += base_address;
264
Simon Glass3ff49ec2021-07-24 09:03:29 -0600265 addr2 = hextoul(argv[2], NULL);
wdenk38635852002-08-27 05:55:31 +0000266 addr2 += base_address;
267
Simon Glass3ff49ec2021-07-24 09:03:29 -0600268 count = hextoul(argv[3], NULL);
wdenk38635852002-08-27 05:55:31 +0000269
Simon Glasse6d0ca22013-02-24 17:33:15 +0000270 bytes = size * count;
271 base = buf1 = map_sysmem(addr1, bytes);
272 buf2 = map_sysmem(addr2, bytes);
Simon Glass99a1c382024-09-01 16:26:30 -0600273 for (ngood = 0, ptr1 = buf1, ptr2 = buf2; ngood < count; ++ngood) {
wdenk38635852002-08-27 05:55:31 +0000274 if (size == 4) {
Simon Glass99a1c382024-09-01 16:26:30 -0600275 word1 = *(u32 *)ptr1;
276 word2 = *(u32 *)ptr2;
Rasmus Villemoesa6f37532024-01-03 11:47:08 +0100277 } else if (MEM_SUPPORT_64BIT_DATA && size == 8) {
Simon Glass99a1c382024-09-01 16:26:30 -0600278 word1 = *(ulong *)ptr1;
279 word2 = *(ulong *)ptr2;
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000280 } else if (size == 2) {
Simon Glass99a1c382024-09-01 16:26:30 -0600281 word1 = *(u16 *)ptr1;
282 word2 = *(u16 *)ptr2;
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000283 } else {
Simon Glass99a1c382024-09-01 16:26:30 -0600284 word1 = *(u8 *)ptr1;
285 word2 = *(u8 *)ptr2;
wdenk38635852002-08-27 05:55:31 +0000286 }
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000287 if (word1 != word2) {
Simon Glass99a1c382024-09-01 16:26:30 -0600288 ulong offset = ptr1 - base;
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000289 printf("%s at 0x%08lx (%#0*lx) != %s at 0x%08lx (%#0*lx)\n",
Simon Glasse6d0ca22013-02-24 17:33:15 +0000290 type, (ulong)(addr1 + offset), size, word1,
291 type, (ulong)(addr2 + offset), size, word2);
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000292 rcode = 1;
293 break;
wdenk38635852002-08-27 05:55:31 +0000294 }
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000295
Simon Glass99a1c382024-09-01 16:26:30 -0600296 ptr1 += size;
297 ptr2 += size;
Stefan Roesed4ef82b2010-09-13 11:10:34 +0200298
299 /* reset watchdog from time to time */
Mike Frysingera66afbd2012-01-20 09:07:22 +0000300 if ((ngood % (64 << 10)) == 0)
Stefan Roese80877fa2022-09-02 14:10:46 +0200301 schedule();
wdenk38635852002-08-27 05:55:31 +0000302 }
Simon Glasse6d0ca22013-02-24 17:33:15 +0000303 unmap_sysmem(buf1);
304 unmap_sysmem(buf2);
wdenk38635852002-08-27 05:55:31 +0000305
Mike Frysinger62a7ea72012-01-20 09:07:21 +0000306 printf("Total of %ld %s(s) were the same\n", ngood, type);
wdenk38635852002-08-27 05:55:31 +0000307 return rcode;
308}
309
Simon Glassed38aef2020-05-10 11:40:03 -0600310static int do_mem_cp(struct cmd_tbl *cmdtp, int flag, int argc,
311 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000312{
Fabio Estevamb03b0c82016-12-15 16:00:13 -0200313 ulong addr, dest, count;
Philippe Reynesb5f461a2019-12-02 17:33:22 +0100314 void *src, *dst;
wdenk874ac262003-07-24 23:38:38 +0000315 int size;
wdenk38635852002-08-27 05:55:31 +0000316
Wolfgang Denk3b683112010-07-17 01:06:04 +0200317 if (argc != 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000318 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000319
320 /* Check for size specification.
321 */
wdenk874ac262003-07-24 23:38:38 +0000322 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
323 return 1;
wdenk38635852002-08-27 05:55:31 +0000324
Simon Glass3ff49ec2021-07-24 09:03:29 -0600325 addr = hextoul(argv[1], NULL);
wdenk38635852002-08-27 05:55:31 +0000326 addr += base_address;
327
Simon Glass3ff49ec2021-07-24 09:03:29 -0600328 dest = hextoul(argv[2], NULL);
wdenk38635852002-08-27 05:55:31 +0000329 dest += base_address;
330
Simon Glass3ff49ec2021-07-24 09:03:29 -0600331 count = hextoul(argv[3], NULL);
wdenk38635852002-08-27 05:55:31 +0000332
333 if (count == 0) {
334 puts ("Zero length ???\n");
335 return 1;
336 }
337
Philippe Reynesb5f461a2019-12-02 17:33:22 +0100338 src = map_sysmem(addr, count * size);
339 dst = map_sysmem(dest, count * size);
340
Masahiro Yamada8cea9b52017-02-11 22:43:54 +0900341#ifdef CONFIG_MTD_NOR_FLASH
wdenk38635852002-08-27 05:55:31 +0000342 /* check if we are copying to Flash */
Philippe Reynesb5f461a2019-12-02 17:33:22 +0100343 if (addr2info((ulong)dst)) {
wdenk38635852002-08-27 05:55:31 +0000344 int rc;
345
wdenk42c05472004-03-23 22:14:11 +0000346 puts ("Copy to Flash... ");
wdenk38635852002-08-27 05:55:31 +0000347
Philippe Reynesb5f461a2019-12-02 17:33:22 +0100348 rc = flash_write((char *)src, (ulong)dst, count * size);
wdenk38635852002-08-27 05:55:31 +0000349 if (rc != 0) {
Simon Glassa606ffc2019-12-28 10:44:40 -0700350 flash_perror(rc);
Philippe Reynesb5f461a2019-12-02 17:33:22 +0100351 unmap_sysmem(src);
352 unmap_sysmem(dst);
wdenk38635852002-08-27 05:55:31 +0000353 return (1);
354 }
355 puts ("done\n");
Philippe Reynesb5f461a2019-12-02 17:33:22 +0100356 unmap_sysmem(src);
357 unmap_sysmem(dst);
wdenk38635852002-08-27 05:55:31 +0000358 return 0;
359 }
wdenk7a428cc2003-06-15 22:40:42 +0000360#endif
361
Rasmus Villemoesb0796e42024-01-03 11:47:03 +0100362 memmove(dst, src, count * size);
Masahiro Yamada787b0c52014-10-23 17:46:24 +0900363
Philippe Reynesb5f461a2019-12-02 17:33:22 +0100364 unmap_sysmem(src);
365 unmap_sysmem(dst);
wdenk38635852002-08-27 05:55:31 +0000366 return 0;
367}
368
Simon Glass573c0fa2020-07-28 19:41:14 -0600369#ifdef CONFIG_CMD_MEM_SEARCH
Simon Glass19038de2020-06-02 19:26:49 -0600370static int do_mem_search(struct cmd_tbl *cmdtp, int flag, int argc,
371 char *const argv[])
372{
373 ulong addr, length, bytes, offset;
374 u8 *ptr, *end, *buf;
375 bool quiet = false;
376 ulong last_pos; /* Offset of last match in 'size' units*/
377 ulong last_addr; /* Address of last displayed line */
378 int limit = 10;
Simon Glass573c0fa2020-07-28 19:41:14 -0600379 int used_len;
Simon Glass19038de2020-06-02 19:26:49 -0600380 int count;
381 int size;
382 int i;
383
384 /* We use the last specified parameters, unless new ones are entered */
385 addr = dp_last_addr;
386 size = dp_last_size;
Simon Glass573c0fa2020-07-28 19:41:14 -0600387 length = dp_last_ms_length;
Simon Glass19038de2020-06-02 19:26:49 -0600388
389 if (argc < 3)
390 return CMD_RET_USAGE;
391
Simon Glass573c0fa2020-07-28 19:41:14 -0600392 if (!(flag & CMD_FLAG_REPEAT)) {
Simon Glass19038de2020-06-02 19:26:49 -0600393 /*
394 * Check for a size specification.
395 * Defaults to long if no or incorrect specification.
396 */
397 size = cmd_get_data_size(argv[0], 4);
Simon Glass3d3c3972020-11-01 14:15:36 -0700398 if (size < 0 && size != CMD_DATA_SIZE_STR)
Simon Glass19038de2020-06-02 19:26:49 -0600399 return 1;
400
Simon Glass573c0fa2020-07-28 19:41:14 -0600401 argc--;
402 argv++;
Simon Glass19038de2020-06-02 19:26:49 -0600403 while (argc && *argv[0] == '-') {
404 int ch = argv[0][1];
405
406 if (ch == 'q')
407 quiet = true;
408 else if (ch == 'l' && isxdigit(argv[0][2]))
Simon Glass3ff49ec2021-07-24 09:03:29 -0600409 limit = hextoul(argv[0] + 2, NULL);
Simon Glass19038de2020-06-02 19:26:49 -0600410 else
411 return CMD_RET_USAGE;
Simon Glass573c0fa2020-07-28 19:41:14 -0600412 argc--;
413 argv++;
Simon Glass19038de2020-06-02 19:26:49 -0600414 }
415
416 /* Address is specified since argc > 1 */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600417 addr = hextoul(argv[0], NULL);
Simon Glass19038de2020-06-02 19:26:49 -0600418 addr += base_address;
419
420 /* Length is the number of objects, not number of bytes */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600421 length = hextoul(argv[1], NULL);
Simon Glass19038de2020-06-02 19:26:49 -0600422
423 /* Read the bytes to search for */
424 end = search_buf + sizeof(search_buf);
425 for (i = 2, ptr = search_buf; i < argc && ptr < end; i++) {
Simon Glass573c0fa2020-07-28 19:41:14 -0600426 if (MEM_SUPPORT_64BIT_DATA && size == 8) {
Simon Glass19038de2020-06-02 19:26:49 -0600427 u64 val = simple_strtoull(argv[i], NULL, 16);
428
429 *(u64 *)ptr = val;
430 } else if (size == -2) { /* string */
431 int len = min(strlen(argv[i]),
432 (size_t)(end - ptr));
433
434 memcpy(ptr, argv[i], len);
435 ptr += len;
436 continue;
437 } else {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600438 u32 val = hextoul(argv[i], NULL);
Simon Glass19038de2020-06-02 19:26:49 -0600439
440 switch (size) {
441 case 1:
442 *ptr = val;
443 break;
444 case 2:
445 *(u16 *)ptr = val;
446 break;
447 case 4:
448 *(u32 *)ptr = val;
449 break;
450 }
451 }
452 ptr += size;
453 }
454 search_len = ptr - search_buf;
455 }
456
457 /* Do the search */
458 if (size == -2)
459 size = 1;
460 bytes = size * length;
461 buf = map_sysmem(addr, bytes);
462 last_pos = 0;
463 last_addr = 0;
464 count = 0;
Simon Glass573c0fa2020-07-28 19:41:14 -0600465 for (offset = 0;
466 offset < bytes && offset <= bytes - search_len && count < limit;
Simon Glass19038de2020-06-02 19:26:49 -0600467 offset += size) {
468 void *ptr = buf + offset;
469
470 if (!memcmp(ptr, search_buf, search_len)) {
471 uint align = (addr + offset) & 0xf;
472 ulong match = addr + offset;
473
474 if (!count || (last_addr & ~0xf) != (match & ~0xf)) {
475 if (!quiet) {
476 if (count)
477 printf("--\n");
478 print_buffer(match - align, ptr - align,
479 size,
480 ALIGN(search_len + align,
481 16) / size, 0);
482 }
483 last_addr = match;
484 last_pos = offset / size;
485 }
486 count++;
487 }
488 }
489 if (!quiet) {
490 printf("%d match%s", count, count == 1 ? "" : "es");
491 if (count == limit)
492 printf(" (repeat command to check for more)");
493 printf("\n");
494 }
495 env_set_hex("memmatches", count);
496 env_set_hex("memaddr", last_addr);
497 env_set_hex("mempos", last_pos);
498
499 unmap_sysmem(buf);
500
Simon Glass573c0fa2020-07-28 19:41:14 -0600501 used_len = offset / size;
502 dp_last_addr = addr + used_len;
Simon Glass19038de2020-06-02 19:26:49 -0600503 dp_last_size = size;
Simon Glass573c0fa2020-07-28 19:41:14 -0600504 dp_last_ms_length = length < used_len ? 0 : length - used_len;
Simon Glass19038de2020-06-02 19:26:49 -0600505
506 return count ? 0 : CMD_RET_FAILURE;
507}
508#endif
509
Simon Glassed38aef2020-05-10 11:40:03 -0600510static int do_mem_base(struct cmd_tbl *cmdtp, int flag, int argc,
511 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000512{
513 if (argc > 1) {
514 /* Set new base address.
515 */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600516 base_address = hextoul(argv[1], NULL);
wdenk38635852002-08-27 05:55:31 +0000517 }
518 /* Print the current base address.
519 */
520 printf("Base Address: 0x%08lx\n", base_address);
521 return 0;
522}
523
Simon Glassed38aef2020-05-10 11:40:03 -0600524static int do_mem_loop(struct cmd_tbl *cmdtp, int flag, int argc,
525 char *const argv[])
wdenk38635852002-08-27 05:55:31 +0000526{
Simon Glasse6d0ca22013-02-24 17:33:15 +0000527 ulong addr, length, i, bytes;
wdenk874ac262003-07-24 23:38:38 +0000528 int size;
Rasmus Villemoesa6f37532024-01-03 11:47:08 +0100529 volatile ulong *llp; /* 64-bit if MEM_SUPPORT_64BIT_DATA */
York Sund8a405d2014-02-12 15:55:35 -0800530 volatile u32 *longp;
531 volatile u16 *shortp;
532 volatile u8 *cp;
Simon Glasse6d0ca22013-02-24 17:33:15 +0000533 const void *buf;
wdenk38635852002-08-27 05:55:31 +0000534
Wolfgang Denk3b683112010-07-17 01:06:04 +0200535 if (argc < 3)
Simon Glassa06dfc72011-12-10 08:44:01 +0000536 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000537
Robert P. J. Day21c9b782013-02-03 02:29:54 +0000538 /*
539 * Check for a size specification.
wdenk38635852002-08-27 05:55:31 +0000540 * Defaults to long if no or incorrect specification.
541 */
wdenk874ac262003-07-24 23:38:38 +0000542 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
543 return 1;
wdenk38635852002-08-27 05:55:31 +0000544
545 /* Address is always specified.
546 */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600547 addr = hextoul(argv[1], NULL);
wdenk38635852002-08-27 05:55:31 +0000548
549 /* Length is the number of objects, not number of bytes.
550 */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600551 length = hextoul(argv[2], NULL);
wdenk38635852002-08-27 05:55:31 +0000552
Simon Glasse6d0ca22013-02-24 17:33:15 +0000553 bytes = size * length;
554 buf = map_sysmem(addr, bytes);
555
wdenk38635852002-08-27 05:55:31 +0000556 /* We want to optimize the loops to run as fast as possible.
557 * If we have only one object, just run infinite loops.
558 */
559 if (length == 1) {
Rasmus Villemoesa6f37532024-01-03 11:47:08 +0100560 if (MEM_SUPPORT_64BIT_DATA && size == 8) {
Simon Glassd1c117b2020-06-02 19:26:46 -0600561 llp = (ulong *)buf;
York Sun6c480012014-02-26 17:03:19 -0800562 for (;;)
563 i = *llp;
564 }
wdenk38635852002-08-27 05:55:31 +0000565 if (size == 4) {
York Sund8a405d2014-02-12 15:55:35 -0800566 longp = (u32 *)buf;
wdenk38635852002-08-27 05:55:31 +0000567 for (;;)
568 i = *longp;
569 }
570 if (size == 2) {
York Sund8a405d2014-02-12 15:55:35 -0800571 shortp = (u16 *)buf;
wdenk38635852002-08-27 05:55:31 +0000572 for (;;)
573 i = *shortp;
574 }
York Sund8a405d2014-02-12 15:55:35 -0800575 cp = (u8 *)buf;
wdenk38635852002-08-27 05:55:31 +0000576 for (;;)
577 i = *cp;
578 }
579
Rasmus Villemoesa6f37532024-01-03 11:47:08 +0100580 if (MEM_SUPPORT_64BIT_DATA && size == 8) {
York Sun6c480012014-02-26 17:03:19 -0800581 for (;;) {
Simon Glassd1c117b2020-06-02 19:26:46 -0600582 llp = (ulong *)buf;
York Sun6c480012014-02-26 17:03:19 -0800583 i = length;
584 while (i-- > 0)
585 *llp++;
586 }
587 }
wdenk38635852002-08-27 05:55:31 +0000588 if (size == 4) {
589 for (;;) {
York Sund8a405d2014-02-12 15:55:35 -0800590 longp = (u32 *)buf;
wdenk38635852002-08-27 05:55:31 +0000591 i = length;
592 while (i-- > 0)
Marek Vasut7d335822011-09-26 02:26:06 +0200593 *longp++;
wdenk38635852002-08-27 05:55:31 +0000594 }
595 }
596 if (size == 2) {
597 for (;;) {
York Sund8a405d2014-02-12 15:55:35 -0800598 shortp = (u16 *)buf;
wdenk38635852002-08-27 05:55:31 +0000599 i = length;
600 while (i-- > 0)
Marek Vasut7d335822011-09-26 02:26:06 +0200601 *shortp++;
wdenk38635852002-08-27 05:55:31 +0000602 }
603 }
604 for (;;) {
York Sund8a405d2014-02-12 15:55:35 -0800605 cp = (u8 *)buf;
wdenk38635852002-08-27 05:55:31 +0000606 i = length;
607 while (i-- > 0)
Marek Vasut7d335822011-09-26 02:26:06 +0200608 *cp++;
wdenk38635852002-08-27 05:55:31 +0000609 }
Simon Glasse6d0ca22013-02-24 17:33:15 +0000610 unmap_sysmem(buf);
Simon Glass50d9eb92013-06-11 11:14:35 -0700611
612 return 0;
wdenk38635852002-08-27 05:55:31 +0000613}
614
wdenk64519362004-07-11 17:40:54 +0000615#ifdef CONFIG_LOOPW
Simon Glassed38aef2020-05-10 11:40:03 -0600616static int do_mem_loopw(struct cmd_tbl *cmdtp, int flag, int argc,
617 char *const argv[])
wdenk64519362004-07-11 17:40:54 +0000618{
York Sun6c480012014-02-26 17:03:19 -0800619 ulong addr, length, i, bytes;
wdenk64519362004-07-11 17:40:54 +0000620 int size;
Rasmus Villemoesa6f37532024-01-03 11:47:08 +0100621 volatile ulong *llp; /* 64-bit if MEM_SUPPORT_64BIT_DATA */
622 ulong data; /* 64-bit if MEM_SUPPORT_64BIT_DATA */
York Sund8a405d2014-02-12 15:55:35 -0800623 volatile u32 *longp;
624 volatile u16 *shortp;
625 volatile u8 *cp;
Simon Glasse6d0ca22013-02-24 17:33:15 +0000626 void *buf;
wdenk7dd13292004-07-11 20:04:51 +0000627
Wolfgang Denk3b683112010-07-17 01:06:04 +0200628 if (argc < 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000629 return CMD_RET_USAGE;
wdenk64519362004-07-11 17:40:54 +0000630
Robert P. J. Day21c9b782013-02-03 02:29:54 +0000631 /*
632 * Check for a size specification.
wdenk64519362004-07-11 17:40:54 +0000633 * Defaults to long if no or incorrect specification.
634 */
635 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
636 return 1;
637
638 /* Address is always specified.
639 */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600640 addr = hextoul(argv[1], NULL);
wdenk64519362004-07-11 17:40:54 +0000641
642 /* Length is the number of objects, not number of bytes.
643 */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600644 length = hextoul(argv[2], NULL);
wdenk64519362004-07-11 17:40:54 +0000645
646 /* data to write */
Rasmus Villemoesa6f37532024-01-03 11:47:08 +0100647 if (MEM_SUPPORT_64BIT_DATA)
Simon Glassd1c117b2020-06-02 19:26:46 -0600648 data = simple_strtoull(argv[3], NULL, 16);
649 else
Simon Glass3ff49ec2021-07-24 09:03:29 -0600650 data = hextoul(argv[3], NULL);
wdenk7dd13292004-07-11 20:04:51 +0000651
Simon Glasse6d0ca22013-02-24 17:33:15 +0000652 bytes = size * length;
653 buf = map_sysmem(addr, bytes);
654
wdenk64519362004-07-11 17:40:54 +0000655 /* We want to optimize the loops to run as fast as possible.
656 * If we have only one object, just run infinite loops.
657 */
658 if (length == 1) {
Rasmus Villemoesa6f37532024-01-03 11:47:08 +0100659 if (MEM_SUPPORT_64BIT_DATA && size == 8) {
Simon Glassd1c117b2020-06-02 19:26:46 -0600660 llp = (ulong *)buf;
York Sun6c480012014-02-26 17:03:19 -0800661 for (;;)
662 *llp = data;
663 }
wdenk64519362004-07-11 17:40:54 +0000664 if (size == 4) {
York Sund8a405d2014-02-12 15:55:35 -0800665 longp = (u32 *)buf;
wdenk64519362004-07-11 17:40:54 +0000666 for (;;)
667 *longp = data;
York Sun6c480012014-02-26 17:03:19 -0800668 }
wdenk64519362004-07-11 17:40:54 +0000669 if (size == 2) {
York Sund8a405d2014-02-12 15:55:35 -0800670 shortp = (u16 *)buf;
wdenk64519362004-07-11 17:40:54 +0000671 for (;;)
672 *shortp = data;
673 }
York Sund8a405d2014-02-12 15:55:35 -0800674 cp = (u8 *)buf;
wdenk64519362004-07-11 17:40:54 +0000675 for (;;)
676 *cp = data;
677 }
678
Rasmus Villemoesa6f37532024-01-03 11:47:08 +0100679 if (MEM_SUPPORT_64BIT_DATA && size == 8) {
York Sun6c480012014-02-26 17:03:19 -0800680 for (;;) {
Simon Glassd1c117b2020-06-02 19:26:46 -0600681 llp = (ulong *)buf;
York Sun6c480012014-02-26 17:03:19 -0800682 i = length;
683 while (i-- > 0)
684 *llp++ = data;
685 }
686 }
wdenk64519362004-07-11 17:40:54 +0000687 if (size == 4) {
688 for (;;) {
York Sund8a405d2014-02-12 15:55:35 -0800689 longp = (u32 *)buf;
wdenk64519362004-07-11 17:40:54 +0000690 i = length;
691 while (i-- > 0)
692 *longp++ = data;
693 }
694 }
695 if (size == 2) {
696 for (;;) {
York Sund8a405d2014-02-12 15:55:35 -0800697 shortp = (u16 *)buf;
wdenk64519362004-07-11 17:40:54 +0000698 i = length;
699 while (i-- > 0)
700 *shortp++ = data;
701 }
702 }
703 for (;;) {
York Sund8a405d2014-02-12 15:55:35 -0800704 cp = (u8 *)buf;
wdenk64519362004-07-11 17:40:54 +0000705 i = length;
706 while (i-- > 0)
707 *cp++ = data;
708 }
709}
710#endif /* CONFIG_LOOPW */
711
Tom Rinia5f02702013-03-12 10:07:19 -0400712#ifdef CONFIG_CMD_MEMTEST
Simon Glassad02a012013-02-28 17:47:14 +0000713static ulong mem_test_alt(vu_long *buf, ulong start_addr, ulong end_addr,
714 vu_long *dummy)
wdenk38635852002-08-27 05:55:31 +0000715{
Simon Glass8f2c7582013-02-24 17:33:16 +0000716 vu_long *addr;
Simon Glass8f2c7582013-02-24 17:33:16 +0000717 ulong errs = 0;
718 ulong val, readback;
719 int j;
Simon Glass8f2c7582013-02-24 17:33:16 +0000720 vu_long offset;
721 vu_long test_offset;
722 vu_long pattern;
723 vu_long temp;
724 vu_long anti_pattern;
725 vu_long num_words;
wdenk38635852002-08-27 05:55:31 +0000726 static const ulong bitpattern[] = {
727 0x00000001, /* single bit */
728 0x00000003, /* two adjacent bits */
729 0x00000007, /* three adjacent bits */
730 0x0000000F, /* four adjacent bits */
731 0x00000005, /* two non-adjacent bits */
732 0x00000015, /* three non-adjacent bits */
733 0x00000055, /* four non-adjacent bits */
734 0xaaaaaaaa, /* alternating 1/0 */
735 };
wdenk38635852002-08-27 05:55:31 +0000736
Simon Glassad02a012013-02-28 17:47:14 +0000737 num_words = (end_addr - start_addr) / sizeof(vu_long);
Simon Glass51075252013-02-24 17:33:20 +0000738
Simon Glass130103e2013-02-24 17:33:18 +0000739 /*
740 * Data line test: write a pattern to the first
741 * location, write the 1's complement to a 'parking'
742 * address (changes the state of the data bus so a
743 * floating bus doesn't give a false OK), and then
744 * read the value back. Note that we read it back
745 * into a variable because the next time we read it,
746 * it might be right (been there, tough to explain to
747 * the quality guys why it prints a failure when the
748 * "is" and "should be" are obviously the same in the
749 * error message).
750 *
751 * Rather than exhaustively testing, we test some
752 * patterns by shifting '1' bits through a field of
753 * '0's and '0' bits through a field of '1's (i.e.
754 * pattern and ~pattern).
755 */
Simon Glassad02a012013-02-28 17:47:14 +0000756 addr = buf;
Simon Glass130103e2013-02-24 17:33:18 +0000757 for (j = 0; j < sizeof(bitpattern) / sizeof(bitpattern[0]); j++) {
758 val = bitpattern[j];
759 for (; val != 0; val <<= 1) {
Simon Glassad02a012013-02-28 17:47:14 +0000760 *addr = val;
Simon Glass8f2c7582013-02-24 17:33:16 +0000761 *dummy = ~val; /* clear the test data off the bus */
wdenk38635852002-08-27 05:55:31 +0000762 readback = *addr;
Simon Glass130103e2013-02-24 17:33:18 +0000763 if (readback != val) {
Simon Glass8f2c7582013-02-24 17:33:16 +0000764 printf("FAILURE (data line): "
765 "expected %08lx, actual %08lx\n",
766 val, readback);
767 errs++;
Simon Glasseb4598b2013-02-24 17:33:19 +0000768 if (ctrlc())
Simon Glass57ee4432013-02-24 17:33:17 +0000769 return -1;
wdenk38635852002-08-27 05:55:31 +0000770 }
771 *addr = ~val;
772 *dummy = val;
773 readback = *addr;
Simon Glass8f2c7582013-02-24 17:33:16 +0000774 if (readback != ~val) {
775 printf("FAILURE (data line): "
776 "Is %08lx, should be %08lx\n",
777 readback, ~val);
778 errs++;
Simon Glasseb4598b2013-02-24 17:33:19 +0000779 if (ctrlc())
Simon Glass57ee4432013-02-24 17:33:17 +0000780 return -1;
wdenk38635852002-08-27 05:55:31 +0000781 }
wdenk38635852002-08-27 05:55:31 +0000782 }
Simon Glass130103e2013-02-24 17:33:18 +0000783 }
wdenk38635852002-08-27 05:55:31 +0000784
Simon Glass130103e2013-02-24 17:33:18 +0000785 /*
786 * Based on code whose Original Author and Copyright
787 * information follows: Copyright (c) 1998 by Michael
788 * Barr. This software is placed into the public
789 * domain and may be used for any purpose. However,
790 * this notice must not be changed or removed and no
791 * warranty is either expressed or implied by its
792 * publication or distribution.
793 */
wdenk38635852002-08-27 05:55:31 +0000794
Simon Glass130103e2013-02-24 17:33:18 +0000795 /*
796 * Address line test
wdenk38635852002-08-27 05:55:31 +0000797
Simon Glass130103e2013-02-24 17:33:18 +0000798 * Description: Test the address bus wiring in a
799 * memory region by performing a walking
800 * 1's test on the relevant bits of the
801 * address and checking for aliasing.
802 * This test will find single-bit
803 * address failures such as stuck-high,
804 * stuck-low, and shorted pins. The base
805 * address and size of the region are
806 * selected by the caller.
wdenk38635852002-08-27 05:55:31 +0000807
Simon Glass130103e2013-02-24 17:33:18 +0000808 * Notes: For best results, the selected base
809 * address should have enough LSB 0's to
810 * guarantee single address bit changes.
811 * For example, to test a 64-Kbyte
812 * region, select a base address on a
813 * 64-Kbyte boundary. Also, select the
814 * region size as a power-of-two if at
815 * all possible.
816 *
817 * Returns: 0 if the test succeeds, 1 if the test fails.
818 */
Heinrich Schuchardtcffb3ba2023-01-27 00:38:50 +0100819 pattern = (vu_long)0xaaaaaaaaaaaaaaaa;
820 anti_pattern = (vu_long)0x5555555555555555;
wdenk38635852002-08-27 05:55:31 +0000821
Simon Glassad02a012013-02-28 17:47:14 +0000822 debug("%s:%d: length = 0x%.8lx\n", __func__, __LINE__, num_words);
Simon Glass130103e2013-02-24 17:33:18 +0000823 /*
824 * Write the default pattern at each of the
825 * power-of-two offsets.
826 */
Simon Glassad02a012013-02-28 17:47:14 +0000827 for (offset = 1; offset < num_words; offset <<= 1)
828 addr[offset] = pattern;
Simon Glass130103e2013-02-24 17:33:18 +0000829
830 /*
831 * Check for address bits stuck high.
832 */
833 test_offset = 0;
Simon Glassad02a012013-02-28 17:47:14 +0000834 addr[test_offset] = anti_pattern;
Simon Glass130103e2013-02-24 17:33:18 +0000835
Simon Glassad02a012013-02-28 17:47:14 +0000836 for (offset = 1; offset < num_words; offset <<= 1) {
837 temp = addr[offset];
Simon Glass130103e2013-02-24 17:33:18 +0000838 if (temp != pattern) {
Simon Glass8f2c7582013-02-24 17:33:16 +0000839 printf("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000840 " expected 0x%.8lx, actual 0x%.8lx\n",
David Fengf40b2212014-02-12 16:10:08 +0800841 start_addr + offset*sizeof(vu_long),
842 pattern, temp);
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400843 errs++;
Simon Glasseb4598b2013-02-24 17:33:19 +0000844 if (ctrlc())
Simon Glass130103e2013-02-24 17:33:18 +0000845 return -1;
wdenk38635852002-08-27 05:55:31 +0000846 }
Simon Glass130103e2013-02-24 17:33:18 +0000847 }
Simon Glassad02a012013-02-28 17:47:14 +0000848 addr[test_offset] = pattern;
Stefan Roese80877fa2022-09-02 14:10:46 +0200849 schedule();
wdenk38635852002-08-27 05:55:31 +0000850
Simon Glass130103e2013-02-24 17:33:18 +0000851 /*
852 * Check for addr bits stuck low or shorted.
853 */
Simon Glassad02a012013-02-28 17:47:14 +0000854 for (test_offset = 1; test_offset < num_words; test_offset <<= 1) {
855 addr[test_offset] = anti_pattern;
wdenk38635852002-08-27 05:55:31 +0000856
Simon Glassad02a012013-02-28 17:47:14 +0000857 for (offset = 1; offset < num_words; offset <<= 1) {
858 temp = addr[offset];
wdenk38635852002-08-27 05:55:31 +0000859 if ((temp != pattern) && (offset != test_offset)) {
Simon Glass130103e2013-02-24 17:33:18 +0000860 printf("\nFAILURE: Address bit stuck low or"
861 " shorted @ 0x%.8lx: expected 0x%.8lx,"
862 " actual 0x%.8lx\n",
David Fengf40b2212014-02-12 16:10:08 +0800863 start_addr + offset*sizeof(vu_long),
864 pattern, temp);
Simon Glass130103e2013-02-24 17:33:18 +0000865 errs++;
Simon Glasseb4598b2013-02-24 17:33:19 +0000866 if (ctrlc())
Simon Glass130103e2013-02-24 17:33:18 +0000867 return -1;
wdenk38635852002-08-27 05:55:31 +0000868 }
wdenk38635852002-08-27 05:55:31 +0000869 }
Simon Glassad02a012013-02-28 17:47:14 +0000870 addr[test_offset] = pattern;
Simon Glass130103e2013-02-24 17:33:18 +0000871 }
wdenk38635852002-08-27 05:55:31 +0000872
Simon Glass130103e2013-02-24 17:33:18 +0000873 /*
874 * Description: Test the integrity of a physical
875 * memory device by performing an
876 * increment/decrement test over the
877 * entire region. In the process every
878 * storage bit in the device is tested
879 * as a zero and a one. The base address
880 * and the size of the region are
881 * selected by the caller.
882 *
883 * Returns: 0 if the test succeeds, 1 if the test fails.
884 */
Simon Glassad02a012013-02-28 17:47:14 +0000885 num_words++;
wdenk38635852002-08-27 05:55:31 +0000886
Simon Glass130103e2013-02-24 17:33:18 +0000887 /*
888 * Fill memory with a known pattern.
889 */
890 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
Stefan Roese80877fa2022-09-02 14:10:46 +0200891 schedule();
Simon Glassad02a012013-02-28 17:47:14 +0000892 addr[offset] = pattern;
Simon Glass130103e2013-02-24 17:33:18 +0000893 }
wdenk38635852002-08-27 05:55:31 +0000894
Simon Glass130103e2013-02-24 17:33:18 +0000895 /*
896 * Check each location and invert it for the second pass.
897 */
898 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
Stefan Roese80877fa2022-09-02 14:10:46 +0200899 schedule();
Simon Glassad02a012013-02-28 17:47:14 +0000900 temp = addr[offset];
Simon Glass130103e2013-02-24 17:33:18 +0000901 if (temp != pattern) {
Simon Glass8f2c7582013-02-24 17:33:16 +0000902 printf("\nFAILURE (read/write) @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000903 " expected 0x%.8lx, actual 0x%.8lx)\n",
David Fengf40b2212014-02-12 16:10:08 +0800904 start_addr + offset*sizeof(vu_long),
905 pattern, temp);
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400906 errs++;
Simon Glasseb4598b2013-02-24 17:33:19 +0000907 if (ctrlc())
Simon Glass57ee4432013-02-24 17:33:17 +0000908 return -1;
wdenk38635852002-08-27 05:55:31 +0000909 }
910
Simon Glass130103e2013-02-24 17:33:18 +0000911 anti_pattern = ~pattern;
Simon Glassad02a012013-02-28 17:47:14 +0000912 addr[offset] = anti_pattern;
Simon Glass130103e2013-02-24 17:33:18 +0000913 }
914
915 /*
916 * Check each location for the inverted pattern and zero it.
917 */
918 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
Stefan Roese80877fa2022-09-02 14:10:46 +0200919 schedule();
Simon Glass130103e2013-02-24 17:33:18 +0000920 anti_pattern = ~pattern;
Simon Glassad02a012013-02-28 17:47:14 +0000921 temp = addr[offset];
Simon Glass130103e2013-02-24 17:33:18 +0000922 if (temp != anti_pattern) {
Simon Glass8f2c7582013-02-24 17:33:16 +0000923 printf("\nFAILURE (read/write): @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000924 " expected 0x%.8lx, actual 0x%.8lx)\n",
David Fengf40b2212014-02-12 16:10:08 +0800925 start_addr + offset*sizeof(vu_long),
926 anti_pattern, temp);
Paul Gortmakercb47b4f2009-10-02 18:18:33 -0400927 errs++;
Simon Glasseb4598b2013-02-24 17:33:19 +0000928 if (ctrlc())
Simon Glass57ee4432013-02-24 17:33:17 +0000929 return -1;
wdenk38635852002-08-27 05:55:31 +0000930 }
Simon Glassad02a012013-02-28 17:47:14 +0000931 addr[offset] = 0;
Simon Glass130103e2013-02-24 17:33:18 +0000932 }
Simon Glass57ee4432013-02-24 17:33:17 +0000933
Rasmus Villemoesabd06c92016-01-07 11:36:04 +0100934 return errs;
Simon Glass8f2c7582013-02-24 17:33:16 +0000935}
936
Stefan Roesefbfd5a12020-03-05 07:21:32 +0100937static int compare_regions(volatile unsigned long *bufa,
938 volatile unsigned long *bufb, size_t count)
939{
940 volatile unsigned long *p1 = bufa;
941 volatile unsigned long *p2 = bufb;
942 int errs = 0;
943 size_t i;
944
945 for (i = 0; i < count; i++, p1++, p2++) {
946 if (*p1 != *p2) {
947 printf("FAILURE: 0x%08lx != 0x%08lx (delta=0x%08lx -> bit %ld) at offset 0x%08lx\n",
948 (unsigned long)*p1, (unsigned long)*p2,
949 *p1 ^ *p2, __ffs(*p1 ^ *p2),
950 (unsigned long)(i * sizeof(unsigned long)));
951 errs++;
952 }
953 }
954
955 return errs;
956}
957
958static ulong test_bitflip_comparison(volatile unsigned long *bufa,
959 volatile unsigned long *bufb, size_t count)
960{
961 volatile unsigned long *p1 = bufa;
962 volatile unsigned long *p2 = bufb;
963 unsigned int j, k;
964 unsigned long q;
965 size_t i;
966 int max;
967 int errs = 0;
968
969 max = sizeof(unsigned long) * 8;
970 for (k = 0; k < max; k++) {
Heinrich Schuchardtcffb3ba2023-01-27 00:38:50 +0100971 q = 1UL << k;
Stefan Roesefbfd5a12020-03-05 07:21:32 +0100972 for (j = 0; j < 8; j++) {
Stefan Roese80877fa2022-09-02 14:10:46 +0200973 schedule();
Stefan Roesefbfd5a12020-03-05 07:21:32 +0100974 q = ~q;
975 p1 = (volatile unsigned long *)bufa;
976 p2 = (volatile unsigned long *)bufb;
977 for (i = 0; i < count; i++)
978 *p1++ = *p2++ = (i % 2) == 0 ? q : ~q;
979
980 errs += compare_regions(bufa, bufb, count);
981 }
982
983 if (ctrlc())
984 return -1UL;
985 }
986
987 return errs;
988}
989
Ralph Siemsen1c39c102020-09-09 12:10:00 -0400990static ulong mem_test_bitflip(vu_long *buf, ulong start, ulong end)
991{
992 /*
993 * Split the specified range into two halves.
994 * Note that mtest range is inclusive of start,end.
995 * Bitflip test instead uses a count (of 32-bit words).
996 */
997 ulong half_size = (end - start + 1) / 2 / sizeof(unsigned long);
998
999 return test_bitflip_comparison(buf, buf + half_size, half_size);
1000}
1001
Simon Glassad02a012013-02-28 17:47:14 +00001002static ulong mem_test_quick(vu_long *buf, ulong start_addr, ulong end_addr,
1003 vu_long pattern, int iteration)
Simon Glass8f2c7582013-02-24 17:33:16 +00001004{
Simon Glassad02a012013-02-28 17:47:14 +00001005 vu_long *end;
Simon Glass8f2c7582013-02-24 17:33:16 +00001006 vu_long *addr;
Simon Glass8f2c7582013-02-24 17:33:16 +00001007 ulong errs = 0;
Simon Glassad02a012013-02-28 17:47:14 +00001008 ulong incr, length;
Simon Glass8f2c7582013-02-24 17:33:16 +00001009 ulong val, readback;
Heinrich Schuchardtcffb3ba2023-01-27 00:38:50 +01001010 const int plen = 2 * sizeof(ulong);
wdenk38635852002-08-27 05:55:31 +00001011
Simon Glass57ee4432013-02-24 17:33:17 +00001012 /* Alternate the pattern */
wdenk38635852002-08-27 05:55:31 +00001013 incr = 1;
Simon Glass57ee4432013-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 */
Heinrich Schuchardtcffb3ba2023-01-27 00:38:50 +01001022 if (pattern > (ulong)LONG_MAX)
Simon Glass57ee4432013-02-24 17:33:17 +00001023 pattern = -pattern; /* complement & increment */
1024 else
1025 pattern = ~pattern;
1026 }
Simon Glassad02a012013-02-28 17:47:14 +00001027 length = (end_addr - start_addr) / sizeof(ulong);
1028 end = buf + length;
Heinrich Schuchardtcffb3ba2023-01-27 00:38:50 +01001029 printf("\rPattern %0*lX Writing..."
Simon Glass130103e2013-02-24 17:33:18 +00001030 "%12s"
1031 "\b\b\b\b\b\b\b\b\b\b",
Heinrich Schuchardtcffb3ba2023-01-27 00:38:50 +01001032 plen, pattern, "");
wdenk38635852002-08-27 05:55:31 +00001033
Simon Glassad02a012013-02-28 17:47:14 +00001034 for (addr = buf, val = pattern; addr < end; addr++) {
Stefan Roese80877fa2022-09-02 14:10:46 +02001035 schedule();
Simon Glass130103e2013-02-24 17:33:18 +00001036 *addr = val;
1037 val += incr;
1038 }
wdenk38635852002-08-27 05:55:31 +00001039
Simon Glass130103e2013-02-24 17:33:18 +00001040 puts("Reading...");
wdenk38635852002-08-27 05:55:31 +00001041
Simon Glassad02a012013-02-28 17:47:14 +00001042 for (addr = buf, val = pattern; addr < end; addr++) {
Stefan Roese80877fa2022-09-02 14:10:46 +02001043 schedule();
Simon Glass130103e2013-02-24 17:33:18 +00001044 readback = *addr;
1045 if (readback != val) {
Simon Glassad02a012013-02-28 17:47:14 +00001046 ulong offset = addr - buf;
1047
Heinrich Schuchardtcffb3ba2023-01-27 00:38:50 +01001048 printf("\nMem error @ 0x%0*lX: found %0*lX, expected %0*lX\n",
1049 plen, start_addr + offset * sizeof(vu_long),
1050 plen, readback, plen, val);
Simon Glass130103e2013-02-24 17:33:18 +00001051 errs++;
Simon Glasseb4598b2013-02-24 17:33:19 +00001052 if (ctrlc())
Simon Glass130103e2013-02-24 17:33:18 +00001053 return -1;
wdenk38635852002-08-27 05:55:31 +00001054 }
Simon Glass130103e2013-02-24 17:33:18 +00001055 val += incr;
1056 }
wdenk38635852002-08-27 05:55:31 +00001057
Rasmus Villemoesabd06c92016-01-07 11:36:04 +01001058 return errs;
Simon Glass8f2c7582013-02-24 17:33:16 +00001059}
1060
1061/*
1062 * Perform a memory test. A more complete alternative test can be
1063 * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
1064 * interrupted by ctrl-c or by a failure of one of the sub-tests.
1065 */
Simon Glassed38aef2020-05-10 11:40:03 -06001066static int do_mem_mtest(struct cmd_tbl *cmdtp, int flag, int argc,
1067 char *const argv[])
Simon Glass8f2c7582013-02-24 17:33:16 +00001068{
Simon Glass51075252013-02-24 17:33:20 +00001069 ulong start, end;
Michal Simek9eaf6442020-05-04 13:54:40 +02001070 vu_long scratch_space;
1071 vu_long *buf, *dummy = &scratch_space;
Tom Rinib9180882015-04-07 09:38:54 -04001072 ulong iteration_limit = 0;
Stefan Roese32265fc2020-03-05 07:21:29 +01001073 ulong count = 0;
Simon Glass57ee4432013-02-24 17:33:17 +00001074 ulong errs = 0; /* number of errors, or -1 if interrupted */
Pavel Machekd319f592015-04-01 13:50:41 +02001075 ulong pattern = 0;
Simon Glass57ee4432013-02-24 17:33:17 +00001076 int iteration;
Simon Glass8f2c7582013-02-24 17:33:16 +00001077
Pavel Machekd319f592015-04-01 13:50:41 +02001078 start = CONFIG_SYS_MEMTEST_START;
1079 end = CONFIG_SYS_MEMTEST_END;
1080
Simon Glass8f2c7582013-02-24 17:33:16 +00001081 if (argc > 1)
Pavel Machekd319f592015-04-01 13:50:41 +02001082 if (strict_strtoul(argv[1], 16, &start) < 0)
1083 return CMD_RET_USAGE;
Simon Glass8f2c7582013-02-24 17:33:16 +00001084
1085 if (argc > 2)
Pavel Machekd319f592015-04-01 13:50:41 +02001086 if (strict_strtoul(argv[2], 16, &end) < 0)
1087 return CMD_RET_USAGE;
Simon Glass8f2c7582013-02-24 17:33:16 +00001088
1089 if (argc > 3)
Pavel Machekd319f592015-04-01 13:50:41 +02001090 if (strict_strtoul(argv[3], 16, &pattern) < 0)
1091 return CMD_RET_USAGE;
Simon Glass8f2c7582013-02-24 17:33:16 +00001092
1093 if (argc > 4)
Pavel Machekd319f592015-04-01 13:50:41 +02001094 if (strict_strtoul(argv[4], 16, &iteration_limit) < 0)
1095 return CMD_RET_USAGE;
1096
1097 if (end < start) {
1098 printf("Refusing to do empty test\n");
1099 return -1;
1100 }
Simon Glass8f2c7582013-02-24 17:33:16 +00001101
Michal Simek45f8e332016-02-24 08:36:02 +01001102 printf("Testing %08lx ... %08lx:\n", start, end);
Simon Glass51075252013-02-24 17:33:20 +00001103 debug("%s:%d: start %#08lx end %#08lx\n", __func__, __LINE__,
1104 start, end);
Simon Glass57ee4432013-02-24 17:33:17 +00001105
Simon Glassad02a012013-02-28 17:47:14 +00001106 buf = map_sysmem(start, end - start);
Simon Glass57ee4432013-02-24 17:33:17 +00001107 for (iteration = 0;
1108 !iteration_limit || iteration < iteration_limit;
1109 iteration++) {
1110 if (ctrlc()) {
Simon Glass57ee4432013-02-24 17:33:17 +00001111 errs = -1UL;
1112 break;
1113 }
1114
1115 printf("Iteration: %6d\r", iteration + 1);
1116 debug("\n");
Stefan Roese1eb8b1e2020-03-05 07:21:31 +01001117 if (IS_ENABLED(CONFIG_SYS_ALT_MEMTEST)) {
Simon Glassad02a012013-02-28 17:47:14 +00001118 errs = mem_test_alt(buf, start, end, dummy);
Stefan Roesefbfd5a12020-03-05 07:21:32 +01001119 if (errs == -1UL)
1120 break;
Ralph Siemsen1c39c102020-09-09 12:10:00 -04001121 if (IS_ENABLED(CONFIG_SYS_ALT_MEMTEST_BITFLIP)) {
1122 count += errs;
1123 errs = mem_test_bitflip(buf, start, end);
1124 }
Simon Glassad02a012013-02-28 17:47:14 +00001125 } else {
1126 errs = mem_test_quick(buf, start, end, pattern,
1127 iteration);
1128 }
1129 if (errs == -1UL)
1130 break;
Stefan Roese32265fc2020-03-05 07:21:29 +01001131 count += errs;
Simon Glassad02a012013-02-28 17:47:14 +00001132 }
1133
Stefan Roesec1d47db2020-03-05 07:21:30 +01001134 unmap_sysmem((void *)buf);
Simon Glass57ee4432013-02-24 17:33:17 +00001135
Heinrich Schuchardtcffb3ba2023-01-27 00:38:50 +01001136 printf("\nTested %d iteration(s) with %lu errors.\n", iteration, count);
Simon Glass8f2c7582013-02-24 17:33:16 +00001137
Stefan Roese32265fc2020-03-05 07:21:29 +01001138 return errs != 0;
wdenk38635852002-08-27 05:55:31 +00001139}
Wolfgang Denk9d009282013-03-08 10:51:32 +00001140#endif /* CONFIG_CMD_MEMTEST */
wdenk38635852002-08-27 05:55:31 +00001141
1142/* Modify memory.
1143 *
1144 * Syntax:
York Sun6c480012014-02-26 17:03:19 -08001145 * mm{.b, .w, .l, .q} {addr}
wdenk38635852002-08-27 05:55:31 +00001146 */
1147static int
Simon Glassed38aef2020-05-10 11:40:03 -06001148mod_mem(struct cmd_tbl *cmdtp, int incrflag, int flag, int argc,
1149 char *const argv[])
wdenk38635852002-08-27 05:55:31 +00001150{
York Sun6c480012014-02-26 17:03:19 -08001151 ulong addr;
Rasmus Villemoesa6f37532024-01-03 11:47:08 +01001152 ulong i; /* 64-bit if MEM_SUPPORT_64BIT_DATA */
wdenk874ac262003-07-24 23:38:38 +00001153 int nbytes, size;
Simon Glasse6d0ca22013-02-24 17:33:15 +00001154 void *ptr = NULL;
wdenk38635852002-08-27 05:55:31 +00001155
Wolfgang Denk3b683112010-07-17 01:06:04 +02001156 if (argc != 2)
Simon Glassa06dfc72011-12-10 08:44:01 +00001157 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +00001158
Simon Glass09007c42014-04-10 20:01:31 -06001159 bootretry_reset_cmd_timeout(); /* got a good command to get here */
wdenk38635852002-08-27 05:55:31 +00001160 /* We use the last specified parameters, unless new ones are
1161 * entered.
1162 */
1163 addr = mm_last_addr;
1164 size = mm_last_size;
1165
1166 if ((flag & CMD_FLAG_REPEAT) == 0) {
1167 /* New command specified. Check for a size specification.
1168 * Defaults to long if no or incorrect specification.
1169 */
wdenk874ac262003-07-24 23:38:38 +00001170 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
1171 return 1;
wdenk38635852002-08-27 05:55:31 +00001172
1173 /* Address is specified since argc > 1
1174 */
Simon Glass3ff49ec2021-07-24 09:03:29 -06001175 addr = hextoul(argv[1], NULL);
wdenk38635852002-08-27 05:55:31 +00001176 addr += base_address;
1177 }
wdenk381669a2003-06-16 23:50:08 +00001178
wdenk38635852002-08-27 05:55:31 +00001179 /* Print the address, followed by value. Then accept input for
1180 * the next value. A non-converted value exits.
1181 */
1182 do {
Simon Glasse6d0ca22013-02-24 17:33:15 +00001183 ptr = map_sysmem(addr, size);
wdenk38635852002-08-27 05:55:31 +00001184 printf("%08lx:", addr);
1185 if (size == 4)
York Sund8a405d2014-02-12 15:55:35 -08001186 printf(" %08x", *((u32 *)ptr));
Rasmus Villemoesa6f37532024-01-03 11:47:08 +01001187 else if (MEM_SUPPORT_64BIT_DATA && size == 8)
Simon Glassd1c117b2020-06-02 19:26:46 -06001188 printf(" %0lx", *((ulong *)ptr));
wdenk38635852002-08-27 05:55:31 +00001189 else if (size == 2)
York Sund8a405d2014-02-12 15:55:35 -08001190 printf(" %04x", *((u16 *)ptr));
wdenk38635852002-08-27 05:55:31 +00001191 else
York Sund8a405d2014-02-12 15:55:35 -08001192 printf(" %02x", *((u8 *)ptr));
wdenk38635852002-08-27 05:55:31 +00001193
Simon Glassbe6aafc2014-04-10 20:01:27 -06001194 nbytes = cli_readline(" ? ");
wdenk38635852002-08-27 05:55:31 +00001195 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
1196 /* <CR> pressed as only input, don't modify current
1197 * location and move to next. "-" pressed will go back.
1198 */
1199 if (incrflag)
1200 addr += nbytes ? -size : size;
1201 nbytes = 1;
Simon Glass09007c42014-04-10 20:01:31 -06001202 /* good enough to not time out */
1203 bootretry_reset_cmd_timeout();
wdenk38635852002-08-27 05:55:31 +00001204 }
1205#ifdef CONFIG_BOOT_RETRY_TIME
1206 else if (nbytes == -2) {
1207 break; /* timed out, exit the command */
1208 }
1209#endif
1210 else {
1211 char *endp;
Rasmus Villemoesa6f37532024-01-03 11:47:08 +01001212 if (MEM_SUPPORT_64BIT_DATA)
Simon Glassd1c117b2020-06-02 19:26:46 -06001213 i = simple_strtoull(console_buffer, &endp, 16);
1214 else
Simon Glass3ff49ec2021-07-24 09:03:29 -06001215 i = hextoul(console_buffer, &endp);
wdenk38635852002-08-27 05:55:31 +00001216 nbytes = endp - console_buffer;
1217 if (nbytes) {
wdenk38635852002-08-27 05:55:31 +00001218 /* good enough to not time out
1219 */
Simon Glass09007c42014-04-10 20:01:31 -06001220 bootretry_reset_cmd_timeout();
wdenk38635852002-08-27 05:55:31 +00001221 if (size == 4)
York Sund8a405d2014-02-12 15:55:35 -08001222 *((u32 *)ptr) = i;
Rasmus Villemoesa6f37532024-01-03 11:47:08 +01001223 else if (MEM_SUPPORT_64BIT_DATA && size == 8)
Simon Glassd1c117b2020-06-02 19:26:46 -06001224 *((ulong *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001225 else if (size == 2)
York Sund8a405d2014-02-12 15:55:35 -08001226 *((u16 *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001227 else
York Sund8a405d2014-02-12 15:55:35 -08001228 *((u8 *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001229 if (incrflag)
1230 addr += size;
1231 }
1232 }
1233 } while (nbytes);
Simon Glasse6d0ca22013-02-24 17:33:15 +00001234 if (ptr)
1235 unmap_sysmem(ptr);
wdenk38635852002-08-27 05:55:31 +00001236
1237 mm_last_addr = addr;
1238 mm_last_size = size;
1239 return 0;
1240}
1241
Mike Frysinger321ab9e2010-12-21 14:19:51 -05001242#ifdef CONFIG_CMD_CRC32
1243
Simon Glassed38aef2020-05-10 11:40:03 -06001244static int do_mem_crc(struct cmd_tbl *cmdtp, int flag, int argc,
1245 char *const argv[])
wdenk38635852002-08-27 05:55:31 +00001246{
Simon Glass0bbd76f2013-02-24 20:30:22 +00001247 int flags = 0;
wdenk6203e402004-04-18 10:13:26 +00001248 int ac;
Wolfgang Denk6262d0212010-06-28 22:00:46 +02001249 char * const *av;
wdenk6203e402004-04-18 10:13:26 +00001250
Simon Glass0bbd76f2013-02-24 20:30:22 +00001251 if (argc < 3)
Simon Glassa06dfc72011-12-10 08:44:01 +00001252 return CMD_RET_USAGE;
wdenk6203e402004-04-18 10:13:26 +00001253
1254 av = argv + 1;
1255 ac = argc - 1;
Daniel Thompsona9e2c672017-05-19 17:26:58 +01001256#ifdef CONFIG_CRC32_VERIFY
wdenk6203e402004-04-18 10:13:26 +00001257 if (strcmp(*av, "-v") == 0) {
Joe Hershberger5fcee9a2015-05-05 12:23:53 -05001258 flags |= HASH_FLAG_VERIFY | HASH_FLAG_ENV;
wdenk6203e402004-04-18 10:13:26 +00001259 av++;
1260 ac--;
wdenk6203e402004-04-18 10:13:26 +00001261 }
Simon Glass0bbd76f2013-02-24 20:30:22 +00001262#endif
wdenk6203e402004-04-18 10:13:26 +00001263
Simon Glass0bbd76f2013-02-24 20:30:22 +00001264 return hash_command("crc32", flags, cmdtp, flag, ac, av);
wdenk6203e402004-04-18 10:13:26 +00001265}
wdenk6203e402004-04-18 10:13:26 +00001266
Robin Getz93d6cb02009-07-27 00:07:59 -04001267#endif
1268
Jean-Jacques Hiblotd3f09372019-07-02 14:23:26 +02001269#ifdef CONFIG_CMD_RANDOM
Simon Glassed38aef2020-05-10 11:40:03 -06001270static int do_random(struct cmd_tbl *cmdtp, int flag, int argc,
1271 char *const argv[])
Jean-Jacques Hiblotd3f09372019-07-02 14:23:26 +02001272{
1273 unsigned long addr, len;
1274 unsigned long seed; // NOT INITIALIZED ON PURPOSE
1275 unsigned int *buf, *start;
1276 unsigned char *buf8;
1277 unsigned int i;
1278
Eugeniy Paltsev82accda2020-03-20 19:38:17 +03001279 if (argc < 3 || argc > 4)
1280 return CMD_RET_USAGE;
Jean-Jacques Hiblotd3f09372019-07-02 14:23:26 +02001281
Simon Glass3ff49ec2021-07-24 09:03:29 -06001282 len = hextoul(argv[2], NULL);
1283 addr = hextoul(argv[1], NULL);
Jean-Jacques Hiblotd3f09372019-07-02 14:23:26 +02001284
1285 if (argc == 4) {
Simon Glass3ff49ec2021-07-24 09:03:29 -06001286 seed = hextoul(argv[3], NULL);
Jean-Jacques Hiblotd3f09372019-07-02 14:23:26 +02001287 if (seed == 0) {
1288 printf("The seed cannot be 0. Using 0xDEADBEEF.\n");
1289 seed = 0xDEADBEEF;
1290 }
1291 } else {
1292 seed = get_timer(0) ^ rand();
1293 }
1294
1295 srand(seed);
1296 start = map_sysmem(addr, len);
1297 buf = start;
1298 for (i = 0; i < (len / 4); i++)
1299 *buf++ = rand();
1300
1301 buf8 = (unsigned char *)buf;
1302 for (i = 0; i < (len % 4); i++)
1303 *buf8++ = rand() & 0xFF;
1304
1305 unmap_sysmem(start);
1306 printf("%lu bytes filled with random data\n", len);
Eugeniy Paltsev82accda2020-03-20 19:38:17 +03001307
1308 return CMD_RET_SUCCESS;
Jean-Jacques Hiblotd3f09372019-07-02 14:23:26 +02001309}
1310#endif
1311
wdenk57b2d802003-06-27 21:31:46 +00001312/**************************************************/
wdenkf287a242003-07-01 21:06:45 +00001313U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001314 md, 3, 1, do_mem_md,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001315 "memory display",
Simon Glass683e39e2020-06-02 19:26:45 -06001316 "[.b, .w, .l" HELP_Q "] address [# of objects]"
wdenk57b2d802003-06-27 21:31:46 +00001317);
1318
wdenkf287a242003-07-01 21:06:45 +00001319U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001320 mm, 2, 1, do_mem_mm,
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001321 "memory modify (auto-incrementing address)",
Simon Glass683e39e2020-06-02 19:26:45 -06001322 "[.b, .w, .l" HELP_Q "] address"
wdenk57b2d802003-06-27 21:31:46 +00001323);
1324
wdenkf287a242003-07-01 21:06:45 +00001325U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001326 nm, 2, 1, do_mem_nm,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001327 "memory modify (constant address)",
Simon Glass683e39e2020-06-02 19:26:45 -06001328 "[.b, .w, .l" HELP_Q "] address"
wdenk57b2d802003-06-27 21:31:46 +00001329);
1330
wdenkf287a242003-07-01 21:06:45 +00001331U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001332 mw, 4, 1, do_mem_mw,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001333 "memory write (fill)",
Simon Glass683e39e2020-06-02 19:26:45 -06001334 "[.b, .w, .l" HELP_Q "] address value [count]"
wdenk57b2d802003-06-27 21:31:46 +00001335);
1336
wdenkf287a242003-07-01 21:06:45 +00001337U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001338 cp, 4, 1, do_mem_cp,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001339 "memory copy",
Simon Glass683e39e2020-06-02 19:26:45 -06001340 "[.b, .w, .l" HELP_Q "] source target count"
wdenk57b2d802003-06-27 21:31:46 +00001341);
1342
wdenkf287a242003-07-01 21:06:45 +00001343U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001344 cmp, 4, 1, do_mem_cmp,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001345 "memory compare",
Simon Glass683e39e2020-06-02 19:26:45 -06001346 "[.b, .w, .l" HELP_Q "] addr1 addr2 count"
wdenk57b2d802003-06-27 21:31:46 +00001347);
Simon Glass19038de2020-06-02 19:26:49 -06001348
Simon Glass573c0fa2020-07-28 19:41:14 -06001349#ifdef CONFIG_CMD_MEM_SEARCH
Simon Glass19038de2020-06-02 19:26:49 -06001350/**************************************************/
1351U_BOOT_CMD(
1352 ms, 255, 1, do_mem_search,
1353 "memory search",
1354 "[.b, .w, .l" HELP_Q ", .s] [-q | -<n>] address #-of-objects <value>..."
Simon Glass573c0fa2020-07-28 19:41:14 -06001355 " -q = quiet, -l<val> = match limit"
Simon Glass19038de2020-06-02 19:26:49 -06001356);
1357#endif
wdenk57b2d802003-06-27 21:31:46 +00001358
Mike Frysinger321ab9e2010-12-21 14:19:51 -05001359#ifdef CONFIG_CMD_CRC32
1360
Daniel Thompsona9e2c672017-05-19 17:26:58 +01001361#ifndef CONFIG_CRC32_VERIFY
wdenk6203e402004-04-18 10:13:26 +00001362
wdenkf287a242003-07-01 21:06:45 +00001363U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001364 crc32, 4, 1, do_mem_crc,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001365 "checksum calculation",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001366 "address count [addr]\n - compute CRC32 checksum [save at addr]"
wdenk57b2d802003-06-27 21:31:46 +00001367);
1368
Daniel Thompsona9e2c672017-05-19 17:26:58 +01001369#else /* CONFIG_CRC32_VERIFY */
wdenk6203e402004-04-18 10:13:26 +00001370
1371U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001372 crc32, 5, 1, do_mem_crc,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001373 "checksum calculation",
wdenk6203e402004-04-18 10:13:26 +00001374 "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001375 "-v address count crc\n - verify crc of memory area"
wdenk6203e402004-04-18 10:13:26 +00001376);
1377
Daniel Thompsona9e2c672017-05-19 17:26:58 +01001378#endif /* CONFIG_CRC32_VERIFY */
wdenk6203e402004-04-18 10:13:26 +00001379
Mike Frysinger321ab9e2010-12-21 14:19:51 -05001380#endif
1381
wdenkf287a242003-07-01 21:06:45 +00001382U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001383 base, 2, 1, do_mem_base,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001384 "print or set address offset",
wdenk57b2d802003-06-27 21:31:46 +00001385 "\n - print address offset for memory commands\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001386 "base off\n - set address offset for memory commands to 'off'"
wdenk57b2d802003-06-27 21:31:46 +00001387);
1388
wdenkf287a242003-07-01 21:06:45 +00001389U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001390 loop, 3, 1, do_mem_loop,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001391 "infinite loop on address range",
Simon Glass683e39e2020-06-02 19:26:45 -06001392 "[.b, .w, .l" HELP_Q "] address number_of_objects"
wdenk57b2d802003-06-27 21:31:46 +00001393);
wdenk64519362004-07-11 17:40:54 +00001394
1395#ifdef CONFIG_LOOPW
1396U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001397 loopw, 4, 1, do_mem_loopw,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001398 "infinite write loop on address range",
Simon Glass683e39e2020-06-02 19:26:45 -06001399 "[.b, .w, .l" HELP_Q "] address number_of_objects data_to_write"
wdenk64519362004-07-11 17:40:54 +00001400);
1401#endif /* CONFIG_LOOPW */
wdenk57b2d802003-06-27 21:31:46 +00001402
Wolfgang Denk9d009282013-03-08 10:51:32 +00001403#ifdef CONFIG_CMD_MEMTEST
wdenkf287a242003-07-01 21:06:45 +00001404U_BOOT_CMD(
Dirk Eibachb2659892008-12-16 14:51:56 +01001405 mtest, 5, 1, do_mem_mtest,
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001406 "simple RAM read/write test",
1407 "[start [end [pattern [iterations]]]]"
wdenk57b2d802003-06-27 21:31:46 +00001408);
Wolfgang Denk9d009282013-03-08 10:51:32 +00001409#endif /* CONFIG_CMD_MEMTEST */
stroesefb5e0ac2004-12-16 17:42:39 +00001410
Joel Johnsondb5a97e2020-01-29 09:17:18 -07001411#ifdef CONFIG_CMD_MX_CYCLIC
stroesefb5e0ac2004-12-16 17:42:39 +00001412U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001413 mdc, 4, 1, do_mem_mdc,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001414 "memory display cyclic",
Simon Glass683e39e2020-06-02 19:26:45 -06001415 "[.b, .w, .l" HELP_Q "] address count delay(ms)"
stroesefb5e0ac2004-12-16 17:42:39 +00001416);
1417
1418U_BOOT_CMD(
Wolfgang Denka1be4762008-05-20 16:00:29 +02001419 mwc, 4, 1, do_mem_mwc,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001420 "memory write cyclic",
Simon Glass683e39e2020-06-02 19:26:45 -06001421 "[.b, .w, .l" HELP_Q "] address value delay(ms)"
stroesefb5e0ac2004-12-16 17:42:39 +00001422);
Joel Johnsondb5a97e2020-01-29 09:17:18 -07001423#endif /* CONFIG_CMD_MX_CYCLIC */
Simon Glasseacd14f2012-11-30 13:01:20 +00001424
Jean-Jacques Hiblotd3f09372019-07-02 14:23:26 +02001425#ifdef CONFIG_CMD_RANDOM
1426U_BOOT_CMD(
1427 random, 4, 0, do_random,
1428 "fill memory with random pattern",
1429 "<addr> <len> [<seed>]\n"
1430 " - Fill 'len' bytes of memory starting at 'addr' with random data\n"
1431);
1432#endif