blob: 392ed1756b636ea89bfc40302134ccc05993cf41 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk38635852002-08-27 05:55:31 +00002/*
3 * (C) Copyright 2000
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk38635852002-08-27 05:55:31 +00005 */
6
7/*
8 * Memory Functions
9 *
10 * Copied from FADS ROM, Dan Malek (dmalek@jlc.net)
11 */
12
13#include <common.h>
Simon Glass24b852a2015-11-08 23:47:45 -070014#include <console.h>
Simon Glass0098e172014-04-10 20:01:30 -060015#include <bootretry.h>
Simon Glass18d66532014-04-10 20:01:25 -060016#include <cli.h>
wdenk38635852002-08-27 05:55:31 +000017#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070018#include <console.h>
Simon Glassd20a40d2013-02-24 20:30:22 +000019#include <hash.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050020#include <mapmem.h>
Sergei Poselenova6e6fc62008-04-09 16:09:41 +020021#include <watchdog.h>
Simon Glass0628ab82013-02-24 17:33:15 +000022#include <asm/io.h>
Simon Glass15a33e42012-11-30 13:01:20 +000023#include <linux/compiler.h>
24
25DECLARE_GLOBAL_DATA_PTR;
wdenk38635852002-08-27 05:55:31 +000026
Simon Glass8e169cc2013-02-24 17:33:29 +000027#ifndef CONFIG_SYS_MEMTEST_SCRATCH
28#define CONFIG_SYS_MEMTEST_SCRATCH 0
29#endif
30
Wolfgang Denk54841ab2010-06-28 22:00:46 +020031static int mod_mem(cmd_tbl_t *, int, int, int, char * const []);
wdenk38635852002-08-27 05:55:31 +000032
33/* Display values from last command.
34 * Memory modify remembered values are different from display memory.
35 */
Scott Wood581508b2015-03-19 09:43:12 -070036static ulong dp_last_addr, dp_last_size;
37static ulong dp_last_length = 0x40;
38static ulong mm_last_addr, mm_last_size;
wdenk38635852002-08-27 05:55:31 +000039
40static ulong base_address = 0;
41
42/* Memory Display
43 *
44 * Syntax:
York Sun4d1fd7f2014-02-26 17:03:19 -080045 * md{.b, .w, .l, .q} {addr} {len}
wdenk38635852002-08-27 05:55:31 +000046 */
47#define DISP_LINE_LEN 16
Kim Phillips088f1b12012-10-29 13:34:31 +000048static int do_mem_md(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000049{
Tuomas Tynkkynenc68c03f2017-10-10 21:59:42 +030050 ulong addr, length, bytes;
51 const void *buf;
wdenk27b207f2003-07-24 23:38:38 +000052 int size;
wdenk38635852002-08-27 05:55:31 +000053 int rc = 0;
54
55 /* We use the last specified parameters, unless new ones are
56 * entered.
57 */
58 addr = dp_last_addr;
59 size = dp_last_size;
60 length = dp_last_length;
61
Wolfgang Denk47e26b12010-07-17 01:06:04 +020062 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +000063 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +000064
65 if ((flag & CMD_FLAG_REPEAT) == 0) {
66 /* New command specified. Check for a size specification.
67 * Defaults to long if no or incorrect specification.
68 */
wdenk27b207f2003-07-24 23:38:38 +000069 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
70 return 1;
wdenk38635852002-08-27 05:55:31 +000071
72 /* Address is specified since argc > 1
73 */
74 addr = simple_strtoul(argv[1], NULL, 16);
75 addr += base_address;
76
77 /* If another parameter, it is the length to display.
78 * Length is the number of objects, not number of bytes.
79 */
80 if (argc > 2)
81 length = simple_strtoul(argv[2], NULL, 16);
82 }
83
Tuomas Tynkkynenc68c03f2017-10-10 21:59:42 +030084 bytes = size * length;
85 buf = map_sysmem(addr, bytes);
wdenk2abbe072003-06-16 23:50:08 +000086
Tuomas Tynkkynenc68c03f2017-10-10 21:59:42 +030087 /* Print the lines. */
88 print_buffer(addr, buf, size, length, DISP_LINE_LEN / size);
89 addr += bytes;
90 unmap_sysmem(buf);
wdenk38635852002-08-27 05:55:31 +000091
92 dp_last_addr = addr;
93 dp_last_length = length;
94 dp_last_size = size;
95 return (rc);
96}
97
Kim Phillips088f1b12012-10-29 13:34:31 +000098static int do_mem_mm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +000099{
100 return mod_mem (cmdtp, 1, flag, argc, argv);
101}
Kim Phillips088f1b12012-10-29 13:34:31 +0000102static int do_mem_nm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000103{
104 return mod_mem (cmdtp, 0, flag, argc, argv);
105}
106
Kim Phillips088f1b12012-10-29 13:34:31 +0000107static int do_mem_mw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000108{
York Sun4d1fd7f2014-02-26 17:03:19 -0800109#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
110 u64 writeval;
111#else
112 ulong writeval;
113#endif
114 ulong addr, count;
wdenk27b207f2003-07-24 23:38:38 +0000115 int size;
Simon Glasscc5e1962015-03-05 12:25:18 -0700116 void *buf, *start;
Simon Glass0628ab82013-02-24 17:33:15 +0000117 ulong bytes;
wdenk38635852002-08-27 05:55:31 +0000118
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200119 if ((argc < 3) || (argc > 4))
Simon Glass4c12eeb2011-12-10 08:44:01 +0000120 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000121
122 /* Check for size specification.
123 */
wdenk27b207f2003-07-24 23:38:38 +0000124 if ((size = cmd_get_data_size(argv[0], 4)) < 1)
125 return 1;
wdenk38635852002-08-27 05:55:31 +0000126
127 /* Address is specified since argc > 1
128 */
129 addr = simple_strtoul(argv[1], NULL, 16);
130 addr += base_address;
131
132 /* Get the value to write.
133 */
York Sun4d1fd7f2014-02-26 17:03:19 -0800134#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
135 writeval = simple_strtoull(argv[2], NULL, 16);
136#else
wdenk38635852002-08-27 05:55:31 +0000137 writeval = simple_strtoul(argv[2], NULL, 16);
York Sun4d1fd7f2014-02-26 17:03:19 -0800138#endif
wdenk38635852002-08-27 05:55:31 +0000139
140 /* Count ? */
141 if (argc == 4) {
142 count = simple_strtoul(argv[3], NULL, 16);
143 } else {
144 count = 1;
145 }
146
Simon Glass0628ab82013-02-24 17:33:15 +0000147 bytes = size * count;
Simon Glasscc5e1962015-03-05 12:25:18 -0700148 start = map_sysmem(addr, bytes);
149 buf = start;
wdenk38635852002-08-27 05:55:31 +0000150 while (count-- > 0) {
151 if (size == 4)
York Sun76698b42014-02-12 15:55:35 -0800152 *((u32 *)buf) = (u32)writeval;
York Sun4d1fd7f2014-02-26 17:03:19 -0800153#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
154 else if (size == 8)
155 *((u64 *)buf) = (u64)writeval;
156#endif
wdenk38635852002-08-27 05:55:31 +0000157 else if (size == 2)
York Sun76698b42014-02-12 15:55:35 -0800158 *((u16 *)buf) = (u16)writeval;
wdenk38635852002-08-27 05:55:31 +0000159 else
York Sun76698b42014-02-12 15:55:35 -0800160 *((u8 *)buf) = (u8)writeval;
Simon Glass0628ab82013-02-24 17:33:15 +0000161 buf += size;
wdenk38635852002-08-27 05:55:31 +0000162 }
Simon Glasscc5e1962015-03-05 12:25:18 -0700163 unmap_sysmem(start);
wdenk38635852002-08-27 05:55:31 +0000164 return 0;
165}
166
stroese4aaf29b2004-12-16 17:42:39 +0000167#ifdef CONFIG_MX_CYCLIC
Masahiro Yamada5a8608e2014-09-04 00:38:26 +0900168static int do_mem_mdc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
stroese4aaf29b2004-12-16 17:42:39 +0000169{
170 int i;
171 ulong count;
172
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200173 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000174 return CMD_RET_USAGE;
stroese4aaf29b2004-12-16 17:42:39 +0000175
176 count = simple_strtoul(argv[3], NULL, 10);
177
178 for (;;) {
179 do_mem_md (NULL, 0, 3, argv);
180
181 /* delay for <count> ms... */
182 for (i=0; i<count; i++)
183 udelay (1000);
184
185 /* check for ctrl-c to abort... */
186 if (ctrlc()) {
187 puts("Abort\n");
188 return 0;
189 }
190 }
191
192 return 0;
193}
194
Masahiro Yamada5a8608e2014-09-04 00:38:26 +0900195static int do_mem_mwc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
stroese4aaf29b2004-12-16 17:42:39 +0000196{
197 int i;
198 ulong count;
199
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200200 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000201 return CMD_RET_USAGE;
stroese4aaf29b2004-12-16 17:42:39 +0000202
203 count = simple_strtoul(argv[3], NULL, 10);
204
205 for (;;) {
206 do_mem_mw (NULL, 0, 3, argv);
207
208 /* delay for <count> ms... */
209 for (i=0; i<count; i++)
210 udelay (1000);
211
212 /* check for ctrl-c to abort... */
213 if (ctrlc()) {
214 puts("Abort\n");
215 return 0;
216 }
217 }
218
219 return 0;
220}
221#endif /* CONFIG_MX_CYCLIC */
222
Kim Phillips088f1b12012-10-29 13:34:31 +0000223static int do_mem_cmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000224{
Simon Glass0628ab82013-02-24 17:33:15 +0000225 ulong addr1, addr2, count, ngood, bytes;
wdenk27b207f2003-07-24 23:38:38 +0000226 int size;
wdenk38635852002-08-27 05:55:31 +0000227 int rcode = 0;
Mike Frysinger054ea172012-01-20 09:07:21 +0000228 const char *type;
Simon Glass0628ab82013-02-24 17:33:15 +0000229 const void *buf1, *buf2, *base;
York Sun4d1fd7f2014-02-26 17:03:19 -0800230#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
231 u64 word1, word2;
232#else
233 ulong word1, word2;
234#endif
wdenk38635852002-08-27 05:55:31 +0000235
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200236 if (argc != 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000237 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000238
239 /* Check for size specification.
240 */
wdenk27b207f2003-07-24 23:38:38 +0000241 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
242 return 1;
York Sun4d1fd7f2014-02-26 17:03:19 -0800243 type = size == 8 ? "double word" :
244 size == 4 ? "word" :
245 size == 2 ? "halfword" : "byte";
wdenk38635852002-08-27 05:55:31 +0000246
247 addr1 = simple_strtoul(argv[1], NULL, 16);
248 addr1 += base_address;
249
250 addr2 = simple_strtoul(argv[2], NULL, 16);
251 addr2 += base_address;
252
253 count = simple_strtoul(argv[3], NULL, 16);
254
Simon Glass0628ab82013-02-24 17:33:15 +0000255 bytes = size * count;
256 base = buf1 = map_sysmem(addr1, bytes);
257 buf2 = map_sysmem(addr2, bytes);
Mike Frysingerfeb12a12012-01-20 09:07:22 +0000258 for (ngood = 0; ngood < count; ++ngood) {
wdenk38635852002-08-27 05:55:31 +0000259 if (size == 4) {
York Sun76698b42014-02-12 15:55:35 -0800260 word1 = *(u32 *)buf1;
261 word2 = *(u32 *)buf2;
York Sun4d1fd7f2014-02-26 17:03:19 -0800262#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
263 } else if (size == 8) {
264 word1 = *(u64 *)buf1;
265 word2 = *(u64 *)buf2;
266#endif
Mike Frysinger054ea172012-01-20 09:07:21 +0000267 } else if (size == 2) {
York Sun76698b42014-02-12 15:55:35 -0800268 word1 = *(u16 *)buf1;
269 word2 = *(u16 *)buf2;
Mike Frysinger054ea172012-01-20 09:07:21 +0000270 } else {
York Sun76698b42014-02-12 15:55:35 -0800271 word1 = *(u8 *)buf1;
272 word2 = *(u8 *)buf2;
wdenk38635852002-08-27 05:55:31 +0000273 }
Mike Frysinger054ea172012-01-20 09:07:21 +0000274 if (word1 != word2) {
Simon Glass0628ab82013-02-24 17:33:15 +0000275 ulong offset = buf1 - base;
York Sun4d1fd7f2014-02-26 17:03:19 -0800276#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900277 printf("%s at 0x%p (%#0*llx) != %s at 0x%p (%#0*llx)\n",
York Sun4d1fd7f2014-02-26 17:03:19 -0800278 type, (void *)(addr1 + offset), size, word1,
279 type, (void *)(addr2 + offset), size, word2);
280#else
Mike Frysinger054ea172012-01-20 09:07:21 +0000281 printf("%s at 0x%08lx (%#0*lx) != %s at 0x%08lx (%#0*lx)\n",
Simon Glass0628ab82013-02-24 17:33:15 +0000282 type, (ulong)(addr1 + offset), size, word1,
283 type, (ulong)(addr2 + offset), size, word2);
York Sun4d1fd7f2014-02-26 17:03:19 -0800284#endif
Mike Frysinger054ea172012-01-20 09:07:21 +0000285 rcode = 1;
286 break;
wdenk38635852002-08-27 05:55:31 +0000287 }
Mike Frysinger054ea172012-01-20 09:07:21 +0000288
Simon Glass0628ab82013-02-24 17:33:15 +0000289 buf1 += size;
290 buf2 += size;
Stefan Roeseeaadb442010-09-13 11:10:34 +0200291
292 /* reset watchdog from time to time */
Mike Frysingerfeb12a12012-01-20 09:07:22 +0000293 if ((ngood % (64 << 10)) == 0)
Stefan Roeseeaadb442010-09-13 11:10:34 +0200294 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000295 }
Simon Glass0628ab82013-02-24 17:33:15 +0000296 unmap_sysmem(buf1);
297 unmap_sysmem(buf2);
wdenk38635852002-08-27 05:55:31 +0000298
Mike Frysinger054ea172012-01-20 09:07:21 +0000299 printf("Total of %ld %s(s) were the same\n", ngood, type);
wdenk38635852002-08-27 05:55:31 +0000300 return rcode;
301}
302
Kim Phillips088f1b12012-10-29 13:34:31 +0000303static int do_mem_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000304{
Fabio Estevamc2538422016-12-15 16:00:13 -0200305 ulong addr, dest, count;
wdenk27b207f2003-07-24 23:38:38 +0000306 int size;
wdenk38635852002-08-27 05:55:31 +0000307
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200308 if (argc != 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000309 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000310
311 /* Check for size specification.
312 */
wdenk27b207f2003-07-24 23:38:38 +0000313 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
314 return 1;
wdenk38635852002-08-27 05:55:31 +0000315
316 addr = simple_strtoul(argv[1], NULL, 16);
317 addr += base_address;
318
319 dest = simple_strtoul(argv[2], NULL, 16);
320 dest += base_address;
321
322 count = simple_strtoul(argv[3], NULL, 16);
323
324 if (count == 0) {
325 puts ("Zero length ???\n");
326 return 1;
327 }
328
Masahiro Yamadae856bdc2017-02-11 22:43:54 +0900329#ifdef CONFIG_MTD_NOR_FLASH
wdenk38635852002-08-27 05:55:31 +0000330 /* check if we are copying to Flash */
Tuomas Tynkkynenc68c03f2017-10-10 21:59:42 +0300331 if (addr2info(dest) != NULL) {
wdenk38635852002-08-27 05:55:31 +0000332 int rc;
333
wdenk4b9206e2004-03-23 22:14:11 +0000334 puts ("Copy to Flash... ");
wdenk38635852002-08-27 05:55:31 +0000335
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200336 rc = flash_write ((char *)addr, dest, count*size);
wdenk38635852002-08-27 05:55:31 +0000337 if (rc != 0) {
338 flash_perror (rc);
339 return (1);
340 }
341 puts ("done\n");
342 return 0;
343 }
344#endif
345
Fabio Estevamc2538422016-12-15 16:00:13 -0200346 memcpy((void *)dest, (void *)addr, count * size);
Masahiro Yamadaa3a47492014-10-23 17:46:24 +0900347
wdenk38635852002-08-27 05:55:31 +0000348 return 0;
349}
350
Kim Phillips088f1b12012-10-29 13:34:31 +0000351static int do_mem_base(cmd_tbl_t *cmdtp, int flag, int argc,
352 char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000353{
354 if (argc > 1) {
355 /* Set new base address.
356 */
357 base_address = simple_strtoul(argv[1], NULL, 16);
358 }
359 /* Print the current base address.
360 */
361 printf("Base Address: 0x%08lx\n", base_address);
362 return 0;
363}
364
Kim Phillips088f1b12012-10-29 13:34:31 +0000365static int do_mem_loop(cmd_tbl_t *cmdtp, int flag, int argc,
366 char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000367{
Simon Glass0628ab82013-02-24 17:33:15 +0000368 ulong addr, length, i, bytes;
wdenk27b207f2003-07-24 23:38:38 +0000369 int size;
York Sun4d1fd7f2014-02-26 17:03:19 -0800370#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
371 volatile u64 *llp;
372#endif
York Sun76698b42014-02-12 15:55:35 -0800373 volatile u32 *longp;
374 volatile u16 *shortp;
375 volatile u8 *cp;
Simon Glass0628ab82013-02-24 17:33:15 +0000376 const void *buf;
wdenk38635852002-08-27 05:55:31 +0000377
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200378 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000379 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000380
Robert P. J. Day85de63e2013-02-03 02:29:54 +0000381 /*
382 * Check for a size specification.
wdenk38635852002-08-27 05:55:31 +0000383 * Defaults to long if no or incorrect specification.
384 */
wdenk27b207f2003-07-24 23:38:38 +0000385 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
386 return 1;
wdenk38635852002-08-27 05:55:31 +0000387
388 /* Address is always specified.
389 */
390 addr = simple_strtoul(argv[1], NULL, 16);
391
392 /* Length is the number of objects, not number of bytes.
393 */
394 length = simple_strtoul(argv[2], NULL, 16);
395
Simon Glass0628ab82013-02-24 17:33:15 +0000396 bytes = size * length;
397 buf = map_sysmem(addr, bytes);
398
wdenk38635852002-08-27 05:55:31 +0000399 /* We want to optimize the loops to run as fast as possible.
400 * If we have only one object, just run infinite loops.
401 */
402 if (length == 1) {
York Sun4d1fd7f2014-02-26 17:03:19 -0800403#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
404 if (size == 8) {
405 llp = (u64 *)buf;
406 for (;;)
407 i = *llp;
408 }
409#endif
wdenk38635852002-08-27 05:55:31 +0000410 if (size == 4) {
York Sun76698b42014-02-12 15:55:35 -0800411 longp = (u32 *)buf;
wdenk38635852002-08-27 05:55:31 +0000412 for (;;)
413 i = *longp;
414 }
415 if (size == 2) {
York Sun76698b42014-02-12 15:55:35 -0800416 shortp = (u16 *)buf;
wdenk38635852002-08-27 05:55:31 +0000417 for (;;)
418 i = *shortp;
419 }
York Sun76698b42014-02-12 15:55:35 -0800420 cp = (u8 *)buf;
wdenk38635852002-08-27 05:55:31 +0000421 for (;;)
422 i = *cp;
423 }
424
York Sun4d1fd7f2014-02-26 17:03:19 -0800425#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
426 if (size == 8) {
427 for (;;) {
428 llp = (u64 *)buf;
429 i = length;
430 while (i-- > 0)
431 *llp++;
432 }
433 }
434#endif
wdenk38635852002-08-27 05:55:31 +0000435 if (size == 4) {
436 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800437 longp = (u32 *)buf;
wdenk38635852002-08-27 05:55:31 +0000438 i = length;
439 while (i-- > 0)
Marek Vasutf3b3c3d2011-09-26 02:26:06 +0200440 *longp++;
wdenk38635852002-08-27 05:55:31 +0000441 }
442 }
443 if (size == 2) {
444 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800445 shortp = (u16 *)buf;
wdenk38635852002-08-27 05:55:31 +0000446 i = length;
447 while (i-- > 0)
Marek Vasutf3b3c3d2011-09-26 02:26:06 +0200448 *shortp++;
wdenk38635852002-08-27 05:55:31 +0000449 }
450 }
451 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800452 cp = (u8 *)buf;
wdenk38635852002-08-27 05:55:31 +0000453 i = length;
454 while (i-- > 0)
Marek Vasutf3b3c3d2011-09-26 02:26:06 +0200455 *cp++;
wdenk38635852002-08-27 05:55:31 +0000456 }
Simon Glass0628ab82013-02-24 17:33:15 +0000457 unmap_sysmem(buf);
Simon Glass92765f422013-06-11 11:14:35 -0700458
459 return 0;
wdenk38635852002-08-27 05:55:31 +0000460}
461
wdenk56523f12004-07-11 17:40:54 +0000462#ifdef CONFIG_LOOPW
Masahiro Yamada5a8608e2014-09-04 00:38:26 +0900463static int do_mem_loopw(cmd_tbl_t *cmdtp, int flag, int argc,
464 char * const argv[])
wdenk56523f12004-07-11 17:40:54 +0000465{
York Sun4d1fd7f2014-02-26 17:03:19 -0800466 ulong addr, length, i, bytes;
wdenk56523f12004-07-11 17:40:54 +0000467 int size;
York Sun4d1fd7f2014-02-26 17:03:19 -0800468#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
469 volatile u64 *llp;
470 u64 data;
471#else
472 ulong data;
473#endif
York Sun76698b42014-02-12 15:55:35 -0800474 volatile u32 *longp;
475 volatile u16 *shortp;
476 volatile u8 *cp;
Simon Glass0628ab82013-02-24 17:33:15 +0000477 void *buf;
wdenk81050922004-07-11 20:04:51 +0000478
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200479 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000480 return CMD_RET_USAGE;
wdenk56523f12004-07-11 17:40:54 +0000481
Robert P. J. Day85de63e2013-02-03 02:29:54 +0000482 /*
483 * Check for a size specification.
wdenk56523f12004-07-11 17:40:54 +0000484 * Defaults to long if no or incorrect specification.
485 */
486 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
487 return 1;
488
489 /* Address is always specified.
490 */
491 addr = simple_strtoul(argv[1], NULL, 16);
492
493 /* Length is the number of objects, not number of bytes.
494 */
495 length = simple_strtoul(argv[2], NULL, 16);
496
497 /* data to write */
York Sun4d1fd7f2014-02-26 17:03:19 -0800498#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
499 data = simple_strtoull(argv[3], NULL, 16);
500#else
wdenk56523f12004-07-11 17:40:54 +0000501 data = simple_strtoul(argv[3], NULL, 16);
York Sun4d1fd7f2014-02-26 17:03:19 -0800502#endif
wdenk81050922004-07-11 20:04:51 +0000503
Simon Glass0628ab82013-02-24 17:33:15 +0000504 bytes = size * length;
505 buf = map_sysmem(addr, bytes);
506
wdenk56523f12004-07-11 17:40:54 +0000507 /* We want to optimize the loops to run as fast as possible.
508 * If we have only one object, just run infinite loops.
509 */
510 if (length == 1) {
York Sun4d1fd7f2014-02-26 17:03:19 -0800511#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
512 if (size == 8) {
513 llp = (u64 *)buf;
514 for (;;)
515 *llp = data;
516 }
517#endif
wdenk56523f12004-07-11 17:40:54 +0000518 if (size == 4) {
York Sun76698b42014-02-12 15:55:35 -0800519 longp = (u32 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000520 for (;;)
521 *longp = data;
York Sun4d1fd7f2014-02-26 17:03:19 -0800522 }
wdenk56523f12004-07-11 17:40:54 +0000523 if (size == 2) {
York Sun76698b42014-02-12 15:55:35 -0800524 shortp = (u16 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000525 for (;;)
526 *shortp = data;
527 }
York Sun76698b42014-02-12 15:55:35 -0800528 cp = (u8 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000529 for (;;)
530 *cp = data;
531 }
532
York Sun4d1fd7f2014-02-26 17:03:19 -0800533#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
534 if (size == 8) {
535 for (;;) {
536 llp = (u64 *)buf;
537 i = length;
538 while (i-- > 0)
539 *llp++ = data;
540 }
541 }
542#endif
wdenk56523f12004-07-11 17:40:54 +0000543 if (size == 4) {
544 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800545 longp = (u32 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000546 i = length;
547 while (i-- > 0)
548 *longp++ = data;
549 }
550 }
551 if (size == 2) {
552 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800553 shortp = (u16 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000554 i = length;
555 while (i-- > 0)
556 *shortp++ = data;
557 }
558 }
559 for (;;) {
York Sun76698b42014-02-12 15:55:35 -0800560 cp = (u8 *)buf;
wdenk56523f12004-07-11 17:40:54 +0000561 i = length;
562 while (i-- > 0)
563 *cp++ = data;
564 }
565}
566#endif /* CONFIG_LOOPW */
567
Tom Rini68149e92013-03-12 10:07:19 -0400568#ifdef CONFIG_CMD_MEMTEST
Simon Glass5512d5b2013-02-28 17:47:14 +0000569static ulong mem_test_alt(vu_long *buf, ulong start_addr, ulong end_addr,
570 vu_long *dummy)
wdenk38635852002-08-27 05:55:31 +0000571{
Simon Glassc9638f52013-02-24 17:33:16 +0000572 vu_long *addr;
Simon Glassc9638f52013-02-24 17:33:16 +0000573 ulong errs = 0;
574 ulong val, readback;
575 int j;
Simon Glassc9638f52013-02-24 17:33:16 +0000576 vu_long offset;
577 vu_long test_offset;
578 vu_long pattern;
579 vu_long temp;
580 vu_long anti_pattern;
581 vu_long num_words;
wdenk38635852002-08-27 05:55:31 +0000582 static const ulong bitpattern[] = {
583 0x00000001, /* single bit */
584 0x00000003, /* two adjacent bits */
585 0x00000007, /* three adjacent bits */
586 0x0000000F, /* four adjacent bits */
587 0x00000005, /* two non-adjacent bits */
588 0x00000015, /* three non-adjacent bits */
589 0x00000055, /* four non-adjacent bits */
590 0xaaaaaaaa, /* alternating 1/0 */
591 };
wdenk38635852002-08-27 05:55:31 +0000592
Simon Glass5512d5b2013-02-28 17:47:14 +0000593 num_words = (end_addr - start_addr) / sizeof(vu_long);
Simon Glass8c86bbe2013-02-24 17:33:20 +0000594
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000595 /*
596 * Data line test: write a pattern to the first
597 * location, write the 1's complement to a 'parking'
598 * address (changes the state of the data bus so a
599 * floating bus doesn't give a false OK), and then
600 * read the value back. Note that we read it back
601 * into a variable because the next time we read it,
602 * it might be right (been there, tough to explain to
603 * the quality guys why it prints a failure when the
604 * "is" and "should be" are obviously the same in the
605 * error message).
606 *
607 * Rather than exhaustively testing, we test some
608 * patterns by shifting '1' bits through a field of
609 * '0's and '0' bits through a field of '1's (i.e.
610 * pattern and ~pattern).
611 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000612 addr = buf;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000613 for (j = 0; j < sizeof(bitpattern) / sizeof(bitpattern[0]); j++) {
614 val = bitpattern[j];
615 for (; val != 0; val <<= 1) {
Simon Glass5512d5b2013-02-28 17:47:14 +0000616 *addr = val;
Simon Glassc9638f52013-02-24 17:33:16 +0000617 *dummy = ~val; /* clear the test data off the bus */
wdenk38635852002-08-27 05:55:31 +0000618 readback = *addr;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000619 if (readback != val) {
Simon Glassc9638f52013-02-24 17:33:16 +0000620 printf("FAILURE (data line): "
621 "expected %08lx, actual %08lx\n",
622 val, readback);
623 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000624 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000625 return -1;
wdenk38635852002-08-27 05:55:31 +0000626 }
627 *addr = ~val;
628 *dummy = val;
629 readback = *addr;
Simon Glassc9638f52013-02-24 17:33:16 +0000630 if (readback != ~val) {
631 printf("FAILURE (data line): "
632 "Is %08lx, should be %08lx\n",
633 readback, ~val);
634 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000635 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000636 return -1;
wdenk38635852002-08-27 05:55:31 +0000637 }
wdenk38635852002-08-27 05:55:31 +0000638 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000639 }
wdenk38635852002-08-27 05:55:31 +0000640
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000641 /*
642 * Based on code whose Original Author and Copyright
643 * information follows: Copyright (c) 1998 by Michael
644 * Barr. This software is placed into the public
645 * domain and may be used for any purpose. However,
646 * this notice must not be changed or removed and no
647 * warranty is either expressed or implied by its
648 * publication or distribution.
649 */
wdenk38635852002-08-27 05:55:31 +0000650
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000651 /*
652 * Address line test
wdenk38635852002-08-27 05:55:31 +0000653
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000654 * Description: Test the address bus wiring in a
655 * memory region by performing a walking
656 * 1's test on the relevant bits of the
657 * address and checking for aliasing.
658 * This test will find single-bit
659 * address failures such as stuck-high,
660 * stuck-low, and shorted pins. The base
661 * address and size of the region are
662 * selected by the caller.
wdenk38635852002-08-27 05:55:31 +0000663
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000664 * Notes: For best results, the selected base
665 * address should have enough LSB 0's to
666 * guarantee single address bit changes.
667 * For example, to test a 64-Kbyte
668 * region, select a base address on a
669 * 64-Kbyte boundary. Also, select the
670 * region size as a power-of-two if at
671 * all possible.
672 *
673 * Returns: 0 if the test succeeds, 1 if the test fails.
674 */
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000675 pattern = (vu_long) 0xaaaaaaaa;
676 anti_pattern = (vu_long) 0x55555555;
wdenk38635852002-08-27 05:55:31 +0000677
Simon Glass5512d5b2013-02-28 17:47:14 +0000678 debug("%s:%d: length = 0x%.8lx\n", __func__, __LINE__, num_words);
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000679 /*
680 * Write the default pattern at each of the
681 * power-of-two offsets.
682 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000683 for (offset = 1; offset < num_words; offset <<= 1)
684 addr[offset] = pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000685
686 /*
687 * Check for address bits stuck high.
688 */
689 test_offset = 0;
Simon Glass5512d5b2013-02-28 17:47:14 +0000690 addr[test_offset] = anti_pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000691
Simon Glass5512d5b2013-02-28 17:47:14 +0000692 for (offset = 1; offset < num_words; offset <<= 1) {
693 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000694 if (temp != pattern) {
Simon Glassc9638f52013-02-24 17:33:16 +0000695 printf("\nFAILURE: Address bit stuck high @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000696 " expected 0x%.8lx, actual 0x%.8lx\n",
David Feng102c0512014-02-12 16:10:08 +0800697 start_addr + offset*sizeof(vu_long),
698 pattern, temp);
Paul Gortmaker87b22b72009-10-02 18:18:33 -0400699 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000700 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000701 return -1;
wdenk38635852002-08-27 05:55:31 +0000702 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000703 }
Simon Glass5512d5b2013-02-28 17:47:14 +0000704 addr[test_offset] = pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000705 WATCHDOG_RESET();
wdenk38635852002-08-27 05:55:31 +0000706
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000707 /*
708 * Check for addr bits stuck low or shorted.
709 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000710 for (test_offset = 1; test_offset < num_words; test_offset <<= 1) {
711 addr[test_offset] = anti_pattern;
wdenk38635852002-08-27 05:55:31 +0000712
Simon Glass5512d5b2013-02-28 17:47:14 +0000713 for (offset = 1; offset < num_words; offset <<= 1) {
714 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000715 if ((temp != pattern) && (offset != test_offset)) {
716 printf("\nFAILURE: Address bit stuck low or"
717 " shorted @ 0x%.8lx: expected 0x%.8lx,"
718 " actual 0x%.8lx\n",
David Feng102c0512014-02-12 16:10:08 +0800719 start_addr + offset*sizeof(vu_long),
720 pattern, temp);
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000721 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000722 if (ctrlc())
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000723 return -1;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000724 }
wdenk38635852002-08-27 05:55:31 +0000725 }
Simon Glass5512d5b2013-02-28 17:47:14 +0000726 addr[test_offset] = pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000727 }
wdenk38635852002-08-27 05:55:31 +0000728
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000729 /*
730 * Description: Test the integrity of a physical
731 * memory device by performing an
732 * increment/decrement test over the
733 * entire region. In the process every
734 * storage bit in the device is tested
735 * as a zero and a one. The base address
736 * and the size of the region are
737 * selected by the caller.
738 *
739 * Returns: 0 if the test succeeds, 1 if the test fails.
740 */
Simon Glass5512d5b2013-02-28 17:47:14 +0000741 num_words++;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000742
743 /*
744 * Fill memory with a known pattern.
745 */
746 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
747 WATCHDOG_RESET();
Simon Glass5512d5b2013-02-28 17:47:14 +0000748 addr[offset] = pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000749 }
750
751 /*
752 * Check each location and invert it for the second pass.
753 */
754 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
755 WATCHDOG_RESET();
Simon Glass5512d5b2013-02-28 17:47:14 +0000756 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000757 if (temp != pattern) {
Simon Glassc9638f52013-02-24 17:33:16 +0000758 printf("\nFAILURE (read/write) @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000759 " expected 0x%.8lx, actual 0x%.8lx)\n",
David Feng102c0512014-02-12 16:10:08 +0800760 start_addr + offset*sizeof(vu_long),
761 pattern, temp);
Paul Gortmaker87b22b72009-10-02 18:18:33 -0400762 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000763 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000764 return -1;
wdenk38635852002-08-27 05:55:31 +0000765 }
766
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000767 anti_pattern = ~pattern;
Simon Glass5512d5b2013-02-28 17:47:14 +0000768 addr[offset] = anti_pattern;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000769 }
770
771 /*
772 * Check each location for the inverted pattern and zero it.
773 */
774 for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) {
775 WATCHDOG_RESET();
776 anti_pattern = ~pattern;
Simon Glass5512d5b2013-02-28 17:47:14 +0000777 temp = addr[offset];
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000778 if (temp != anti_pattern) {
Simon Glassc9638f52013-02-24 17:33:16 +0000779 printf("\nFAILURE (read/write): @ 0x%.8lx:"
wdenk38635852002-08-27 05:55:31 +0000780 " expected 0x%.8lx, actual 0x%.8lx)\n",
David Feng102c0512014-02-12 16:10:08 +0800781 start_addr + offset*sizeof(vu_long),
782 anti_pattern, temp);
Paul Gortmaker87b22b72009-10-02 18:18:33 -0400783 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000784 if (ctrlc())
Simon Glass51209b12013-02-24 17:33:17 +0000785 return -1;
wdenk38635852002-08-27 05:55:31 +0000786 }
Simon Glass5512d5b2013-02-28 17:47:14 +0000787 addr[offset] = 0;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000788 }
Simon Glass51209b12013-02-24 17:33:17 +0000789
Rasmus Villemoesfea67302016-01-07 11:36:04 +0100790 return errs;
Simon Glassc9638f52013-02-24 17:33:16 +0000791}
wdenk38635852002-08-27 05:55:31 +0000792
Simon Glass5512d5b2013-02-28 17:47:14 +0000793static ulong mem_test_quick(vu_long *buf, ulong start_addr, ulong end_addr,
794 vu_long pattern, int iteration)
Simon Glassc9638f52013-02-24 17:33:16 +0000795{
Simon Glass5512d5b2013-02-28 17:47:14 +0000796 vu_long *end;
Simon Glassc9638f52013-02-24 17:33:16 +0000797 vu_long *addr;
Simon Glassc9638f52013-02-24 17:33:16 +0000798 ulong errs = 0;
Simon Glass5512d5b2013-02-28 17:47:14 +0000799 ulong incr, length;
Simon Glassc9638f52013-02-24 17:33:16 +0000800 ulong val, readback;
801
Simon Glass51209b12013-02-24 17:33:17 +0000802 /* Alternate the pattern */
wdenk38635852002-08-27 05:55:31 +0000803 incr = 1;
Simon Glass51209b12013-02-24 17:33:17 +0000804 if (iteration & 1) {
805 incr = -incr;
806 /*
807 * Flip the pattern each time to make lots of zeros and
808 * then, the next time, lots of ones. We decrement
809 * the "negative" patterns and increment the "positive"
810 * patterns to preserve this feature.
811 */
812 if (pattern & 0x80000000)
813 pattern = -pattern; /* complement & increment */
814 else
815 pattern = ~pattern;
816 }
Simon Glass5512d5b2013-02-28 17:47:14 +0000817 length = (end_addr - start_addr) / sizeof(ulong);
818 end = buf + length;
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000819 printf("\rPattern %08lX Writing..."
820 "%12s"
821 "\b\b\b\b\b\b\b\b\b\b",
822 pattern, "");
wdenk38635852002-08-27 05:55:31 +0000823
Simon Glass5512d5b2013-02-28 17:47:14 +0000824 for (addr = buf, val = pattern; addr < end; addr++) {
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000825 WATCHDOG_RESET();
826 *addr = val;
827 val += incr;
828 }
wdenk38635852002-08-27 05:55:31 +0000829
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000830 puts("Reading...");
wdenk38635852002-08-27 05:55:31 +0000831
Simon Glass5512d5b2013-02-28 17:47:14 +0000832 for (addr = buf, val = pattern; addr < end; addr++) {
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000833 WATCHDOG_RESET();
834 readback = *addr;
835 if (readback != val) {
Simon Glass5512d5b2013-02-28 17:47:14 +0000836 ulong offset = addr - buf;
837
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000838 printf("\nMem error @ 0x%08X: "
839 "found %08lX, expected %08lX\n",
David Feng102c0512014-02-12 16:10:08 +0800840 (uint)(uintptr_t)(start_addr + offset*sizeof(vu_long)),
Simon Glass5512d5b2013-02-28 17:47:14 +0000841 readback, val);
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000842 errs++;
Simon Glassc44d4382013-02-24 17:33:19 +0000843 if (ctrlc())
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000844 return -1;
wdenk38635852002-08-27 05:55:31 +0000845 }
Simon Glass7ecbd4d2013-02-24 17:33:18 +0000846 val += incr;
847 }
wdenk38635852002-08-27 05:55:31 +0000848
Rasmus Villemoesfea67302016-01-07 11:36:04 +0100849 return errs;
Simon Glassc9638f52013-02-24 17:33:16 +0000850}
851
852/*
853 * Perform a memory test. A more complete alternative test can be
854 * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
855 * interrupted by ctrl-c or by a failure of one of the sub-tests.
856 */
857static int do_mem_mtest(cmd_tbl_t *cmdtp, int flag, int argc,
858 char * const argv[])
859{
Simon Glass8c86bbe2013-02-24 17:33:20 +0000860 ulong start, end;
Simon Glass5512d5b2013-02-28 17:47:14 +0000861 vu_long *buf, *dummy;
Tom Riniadcc5702015-04-07 09:38:54 -0400862 ulong iteration_limit = 0;
Simon Glassc9638f52013-02-24 17:33:16 +0000863 int ret;
Simon Glass51209b12013-02-24 17:33:17 +0000864 ulong errs = 0; /* number of errors, or -1 if interrupted */
Pavel Macheke37f1eb2015-04-01 13:50:41 +0200865 ulong pattern = 0;
Simon Glass51209b12013-02-24 17:33:17 +0000866 int iteration;
Simon Glassc9638f52013-02-24 17:33:16 +0000867#if defined(CONFIG_SYS_ALT_MEMTEST)
868 const int alt_test = 1;
869#else
870 const int alt_test = 0;
wdenk38635852002-08-27 05:55:31 +0000871#endif
Simon Glassc9638f52013-02-24 17:33:16 +0000872
Pavel Macheke37f1eb2015-04-01 13:50:41 +0200873 start = CONFIG_SYS_MEMTEST_START;
874 end = CONFIG_SYS_MEMTEST_END;
875
Simon Glassc9638f52013-02-24 17:33:16 +0000876 if (argc > 1)
Pavel Macheke37f1eb2015-04-01 13:50:41 +0200877 if (strict_strtoul(argv[1], 16, &start) < 0)
878 return CMD_RET_USAGE;
Simon Glassc9638f52013-02-24 17:33:16 +0000879
880 if (argc > 2)
Pavel Macheke37f1eb2015-04-01 13:50:41 +0200881 if (strict_strtoul(argv[2], 16, &end) < 0)
882 return CMD_RET_USAGE;
Simon Glassc9638f52013-02-24 17:33:16 +0000883
884 if (argc > 3)
Pavel Macheke37f1eb2015-04-01 13:50:41 +0200885 if (strict_strtoul(argv[3], 16, &pattern) < 0)
886 return CMD_RET_USAGE;
Simon Glassc9638f52013-02-24 17:33:16 +0000887
888 if (argc > 4)
Pavel Macheke37f1eb2015-04-01 13:50:41 +0200889 if (strict_strtoul(argv[4], 16, &iteration_limit) < 0)
890 return CMD_RET_USAGE;
891
892 if (end < start) {
893 printf("Refusing to do empty test\n");
894 return -1;
895 }
Simon Glassc9638f52013-02-24 17:33:16 +0000896
Michal Simekdfe461d2016-02-24 08:36:02 +0100897 printf("Testing %08lx ... %08lx:\n", start, end);
Simon Glass8c86bbe2013-02-24 17:33:20 +0000898 debug("%s:%d: start %#08lx end %#08lx\n", __func__, __LINE__,
899 start, end);
Simon Glass51209b12013-02-24 17:33:17 +0000900
Simon Glass5512d5b2013-02-28 17:47:14 +0000901 buf = map_sysmem(start, end - start);
902 dummy = map_sysmem(CONFIG_SYS_MEMTEST_SCRATCH, sizeof(vu_long));
Simon Glass51209b12013-02-24 17:33:17 +0000903 for (iteration = 0;
904 !iteration_limit || iteration < iteration_limit;
905 iteration++) {
906 if (ctrlc()) {
Simon Glass51209b12013-02-24 17:33:17 +0000907 errs = -1UL;
908 break;
909 }
910
911 printf("Iteration: %6d\r", iteration + 1);
912 debug("\n");
Simon Glass5512d5b2013-02-28 17:47:14 +0000913 if (alt_test) {
914 errs = mem_test_alt(buf, start, end, dummy);
915 } else {
916 errs = mem_test_quick(buf, start, end, pattern,
917 iteration);
918 }
919 if (errs == -1UL)
920 break;
921 }
922
923 /*
924 * Work-around for eldk-4.2 which gives this warning if we try to
925 * case in the unmap_sysmem() call:
926 * warning: initialization discards qualifiers from pointer target type
927 */
928 {
929 void *vbuf = (void *)buf;
930 void *vdummy = (void *)dummy;
931
932 unmap_sysmem(vbuf);
933 unmap_sysmem(vdummy);
Simon Glass51209b12013-02-24 17:33:17 +0000934 }
935
936 if (errs == -1UL) {
Simon Glassc44d4382013-02-24 17:33:19 +0000937 /* Memory test was aborted - write a newline to finish off */
938 putc('\n');
Simon Glass51209b12013-02-24 17:33:17 +0000939 ret = 1;
940 } else {
941 printf("Tested %d iteration(s) with %lu errors.\n",
942 iteration, errs);
943 ret = errs != 0;
944 }
Simon Glassc9638f52013-02-24 17:33:16 +0000945
Pavel Macheke37f1eb2015-04-01 13:50:41 +0200946 return ret;
wdenk38635852002-08-27 05:55:31 +0000947}
Wolfgang Denka2681702013-03-08 10:51:32 +0000948#endif /* CONFIG_CMD_MEMTEST */
wdenk38635852002-08-27 05:55:31 +0000949
950/* Modify memory.
951 *
952 * Syntax:
York Sun4d1fd7f2014-02-26 17:03:19 -0800953 * mm{.b, .w, .l, .q} {addr}
954 * nm{.b, .w, .l, .q} {addr}
wdenk38635852002-08-27 05:55:31 +0000955 */
956static int
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200957mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +0000958{
York Sun4d1fd7f2014-02-26 17:03:19 -0800959 ulong addr;
960#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
961 u64 i;
962#else
963 ulong i;
964#endif
wdenk27b207f2003-07-24 23:38:38 +0000965 int nbytes, size;
Simon Glass0628ab82013-02-24 17:33:15 +0000966 void *ptr = NULL;
wdenk38635852002-08-27 05:55:31 +0000967
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200968 if (argc != 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000969 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +0000970
Simon Glassb26440f2014-04-10 20:01:31 -0600971 bootretry_reset_cmd_timeout(); /* got a good command to get here */
wdenk38635852002-08-27 05:55:31 +0000972 /* We use the last specified parameters, unless new ones are
973 * entered.
974 */
975 addr = mm_last_addr;
976 size = mm_last_size;
977
978 if ((flag & CMD_FLAG_REPEAT) == 0) {
979 /* New command specified. Check for a size specification.
980 * Defaults to long if no or incorrect specification.
981 */
wdenk27b207f2003-07-24 23:38:38 +0000982 if ((size = cmd_get_data_size(argv[0], 4)) < 0)
983 return 1;
wdenk38635852002-08-27 05:55:31 +0000984
985 /* Address is specified since argc > 1
986 */
987 addr = simple_strtoul(argv[1], NULL, 16);
988 addr += base_address;
989 }
990
991 /* Print the address, followed by value. Then accept input for
992 * the next value. A non-converted value exits.
993 */
994 do {
Simon Glass0628ab82013-02-24 17:33:15 +0000995 ptr = map_sysmem(addr, size);
wdenk38635852002-08-27 05:55:31 +0000996 printf("%08lx:", addr);
997 if (size == 4)
York Sun76698b42014-02-12 15:55:35 -0800998 printf(" %08x", *((u32 *)ptr));
York Sun4d1fd7f2014-02-26 17:03:19 -0800999#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1000 else if (size == 8)
Masahiro Yamadadee37fc2018-08-06 20:47:40 +09001001 printf(" %016llx", *((u64 *)ptr));
York Sun4d1fd7f2014-02-26 17:03:19 -08001002#endif
wdenk38635852002-08-27 05:55:31 +00001003 else if (size == 2)
York Sun76698b42014-02-12 15:55:35 -08001004 printf(" %04x", *((u16 *)ptr));
wdenk38635852002-08-27 05:55:31 +00001005 else
York Sun76698b42014-02-12 15:55:35 -08001006 printf(" %02x", *((u8 *)ptr));
wdenk38635852002-08-27 05:55:31 +00001007
Simon Glasse1bf8242014-04-10 20:01:27 -06001008 nbytes = cli_readline(" ? ");
wdenk38635852002-08-27 05:55:31 +00001009 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
1010 /* <CR> pressed as only input, don't modify current
1011 * location and move to next. "-" pressed will go back.
1012 */
1013 if (incrflag)
1014 addr += nbytes ? -size : size;
1015 nbytes = 1;
Simon Glassb26440f2014-04-10 20:01:31 -06001016 /* good enough to not time out */
1017 bootretry_reset_cmd_timeout();
wdenk38635852002-08-27 05:55:31 +00001018 }
1019#ifdef CONFIG_BOOT_RETRY_TIME
1020 else if (nbytes == -2) {
1021 break; /* timed out, exit the command */
1022 }
1023#endif
1024 else {
1025 char *endp;
York Sun4d1fd7f2014-02-26 17:03:19 -08001026#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1027 i = simple_strtoull(console_buffer, &endp, 16);
1028#else
wdenk38635852002-08-27 05:55:31 +00001029 i = simple_strtoul(console_buffer, &endp, 16);
York Sun4d1fd7f2014-02-26 17:03:19 -08001030#endif
wdenk38635852002-08-27 05:55:31 +00001031 nbytes = endp - console_buffer;
1032 if (nbytes) {
wdenk38635852002-08-27 05:55:31 +00001033 /* good enough to not time out
1034 */
Simon Glassb26440f2014-04-10 20:01:31 -06001035 bootretry_reset_cmd_timeout();
wdenk38635852002-08-27 05:55:31 +00001036 if (size == 4)
York Sun76698b42014-02-12 15:55:35 -08001037 *((u32 *)ptr) = i;
York Sun4d1fd7f2014-02-26 17:03:19 -08001038#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1039 else if (size == 8)
1040 *((u64 *)ptr) = i;
1041#endif
wdenk38635852002-08-27 05:55:31 +00001042 else if (size == 2)
York Sun76698b42014-02-12 15:55:35 -08001043 *((u16 *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001044 else
York Sun76698b42014-02-12 15:55:35 -08001045 *((u8 *)ptr) = i;
wdenk38635852002-08-27 05:55:31 +00001046 if (incrflag)
1047 addr += size;
1048 }
1049 }
1050 } while (nbytes);
Simon Glass0628ab82013-02-24 17:33:15 +00001051 if (ptr)
1052 unmap_sysmem(ptr);
wdenk38635852002-08-27 05:55:31 +00001053
1054 mm_last_addr = addr;
1055 mm_last_size = size;
1056 return 0;
1057}
1058
Mike Frysinger710b9932010-12-21 14:19:51 -05001059#ifdef CONFIG_CMD_CRC32
1060
Kim Phillips088f1b12012-10-29 13:34:31 +00001061static int do_mem_crc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk38635852002-08-27 05:55:31 +00001062{
Simon Glassd20a40d2013-02-24 20:30:22 +00001063 int flags = 0;
1064 int ac;
1065 char * const *av;
wdenk38635852002-08-27 05:55:31 +00001066
Wolfgang Denk47e26b12010-07-17 01:06:04 +02001067 if (argc < 3)
Simon Glass4c12eeb2011-12-10 08:44:01 +00001068 return CMD_RET_USAGE;
wdenk38635852002-08-27 05:55:31 +00001069
wdenkc26e4542004-04-18 10:13:26 +00001070 av = argv + 1;
1071 ac = argc - 1;
Daniel Thompson221a9492017-05-19 17:26:58 +01001072#ifdef CONFIG_CRC32_VERIFY
wdenkc26e4542004-04-18 10:13:26 +00001073 if (strcmp(*av, "-v") == 0) {
Joe Hershbergera69bdba2015-05-05 12:23:53 -05001074 flags |= HASH_FLAG_VERIFY | HASH_FLAG_ENV;
wdenkc26e4542004-04-18 10:13:26 +00001075 av++;
1076 ac--;
wdenkc26e4542004-04-18 10:13:26 +00001077 }
Simon Glassd20a40d2013-02-24 20:30:22 +00001078#endif
wdenkc26e4542004-04-18 10:13:26 +00001079
Simon Glassd20a40d2013-02-24 20:30:22 +00001080 return hash_command("crc32", flags, cmdtp, flag, ac, av);
wdenkc26e4542004-04-18 10:13:26 +00001081}
wdenkc26e4542004-04-18 10:13:26 +00001082
Mike Frysinger710b9932010-12-21 14:19:51 -05001083#endif
1084
wdenk8bde7f72003-06-27 21:31:46 +00001085/**************************************************/
wdenk0d498392003-07-01 21:06:45 +00001086U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001087 md, 3, 1, do_mem_md,
Peter Tyser2fb26042009-01-27 18:03:12 -06001088 "memory display",
York Sun4d1fd7f2014-02-26 17:03:19 -08001089#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1090 "[.b, .w, .l, .q] address [# of objects]"
1091#else
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001092 "[.b, .w, .l] address [# of objects]"
York Sun4d1fd7f2014-02-26 17:03:19 -08001093#endif
wdenk8bde7f72003-06-27 21:31:46 +00001094);
1095
1096
wdenk0d498392003-07-01 21:06:45 +00001097U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001098 mm, 2, 1, do_mem_mm,
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001099 "memory modify (auto-incrementing address)",
York Sun4d1fd7f2014-02-26 17:03:19 -08001100#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1101 "[.b, .w, .l, .q] address"
1102#else
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001103 "[.b, .w, .l] address"
York Sun4d1fd7f2014-02-26 17:03:19 -08001104#endif
wdenk8bde7f72003-06-27 21:31:46 +00001105);
1106
1107
wdenk0d498392003-07-01 21:06:45 +00001108U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001109 nm, 2, 1, do_mem_nm,
Peter Tyser2fb26042009-01-27 18:03:12 -06001110 "memory modify (constant address)",
York Sun4d1fd7f2014-02-26 17:03:19 -08001111#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1112 "[.b, .w, .l, .q] address"
1113#else
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001114 "[.b, .w, .l] address"
York Sun4d1fd7f2014-02-26 17:03:19 -08001115#endif
wdenk8bde7f72003-06-27 21:31:46 +00001116);
1117
wdenk0d498392003-07-01 21:06:45 +00001118U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001119 mw, 4, 1, do_mem_mw,
Peter Tyser2fb26042009-01-27 18:03:12 -06001120 "memory write (fill)",
York Sun4d1fd7f2014-02-26 17:03:19 -08001121#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1122 "[.b, .w, .l, .q] address value [count]"
1123#else
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001124 "[.b, .w, .l] address value [count]"
York Sun4d1fd7f2014-02-26 17:03:19 -08001125#endif
wdenk8bde7f72003-06-27 21:31:46 +00001126);
1127
wdenk0d498392003-07-01 21:06:45 +00001128U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001129 cp, 4, 1, do_mem_cp,
Peter Tyser2fb26042009-01-27 18:03:12 -06001130 "memory copy",
York Sun4d1fd7f2014-02-26 17:03:19 -08001131#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1132 "[.b, .w, .l, .q] source target count"
1133#else
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001134 "[.b, .w, .l] source target count"
York Sun4d1fd7f2014-02-26 17:03:19 -08001135#endif
wdenk8bde7f72003-06-27 21:31:46 +00001136);
1137
wdenk0d498392003-07-01 21:06:45 +00001138U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001139 cmp, 4, 1, do_mem_cmp,
Peter Tyser2fb26042009-01-27 18:03:12 -06001140 "memory compare",
York Sun4d1fd7f2014-02-26 17:03:19 -08001141#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1142 "[.b, .w, .l, .q] addr1 addr2 count"
1143#else
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001144 "[.b, .w, .l] addr1 addr2 count"
York Sun4d1fd7f2014-02-26 17:03:19 -08001145#endif
wdenk8bde7f72003-06-27 21:31:46 +00001146);
1147
Mike Frysinger710b9932010-12-21 14:19:51 -05001148#ifdef CONFIG_CMD_CRC32
1149
Daniel Thompson221a9492017-05-19 17:26:58 +01001150#ifndef CONFIG_CRC32_VERIFY
wdenkc26e4542004-04-18 10:13:26 +00001151
wdenk0d498392003-07-01 21:06:45 +00001152U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001153 crc32, 4, 1, do_mem_crc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001154 "checksum calculation",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001155 "address count [addr]\n - compute CRC32 checksum [save at addr]"
wdenk8bde7f72003-06-27 21:31:46 +00001156);
1157
Daniel Thompson221a9492017-05-19 17:26:58 +01001158#else /* CONFIG_CRC32_VERIFY */
wdenkc26e4542004-04-18 10:13:26 +00001159
1160U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001161 crc32, 5, 1, do_mem_crc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001162 "checksum calculation",
wdenkc26e4542004-04-18 10:13:26 +00001163 "address count [addr]\n - compute CRC32 checksum [save at addr]\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001164 "-v address count crc\n - verify crc of memory area"
wdenkc26e4542004-04-18 10:13:26 +00001165);
1166
Daniel Thompson221a9492017-05-19 17:26:58 +01001167#endif /* CONFIG_CRC32_VERIFY */
wdenkc26e4542004-04-18 10:13:26 +00001168
Mike Frysinger710b9932010-12-21 14:19:51 -05001169#endif
1170
Simon Glass15a33e42012-11-30 13:01:20 +00001171#ifdef CONFIG_CMD_MEMINFO
Andrew Bradfordea11b402015-05-22 08:30:14 -04001172__weak void board_show_dram(phys_size_t size)
Simon Glass15a33e42012-11-30 13:01:20 +00001173{
1174 puts("DRAM: ");
1175 print_size(size, "\n");
1176}
1177
1178static int do_mem_info(cmd_tbl_t *cmdtp, int flag, int argc,
1179 char * const argv[])
1180{
1181 board_show_dram(gd->ram_size);
1182
1183 return 0;
1184}
1185#endif
1186
wdenk0d498392003-07-01 21:06:45 +00001187U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001188 base, 2, 1, do_mem_base,
Peter Tyser2fb26042009-01-27 18:03:12 -06001189 "print or set address offset",
wdenk8bde7f72003-06-27 21:31:46 +00001190 "\n - print address offset for memory commands\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001191 "base off\n - set address offset for memory commands to 'off'"
wdenk8bde7f72003-06-27 21:31:46 +00001192);
1193
wdenk0d498392003-07-01 21:06:45 +00001194U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001195 loop, 3, 1, do_mem_loop,
Peter Tyser2fb26042009-01-27 18:03:12 -06001196 "infinite loop on address range",
York Sun4d1fd7f2014-02-26 17:03:19 -08001197#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1198 "[.b, .w, .l, .q] address number_of_objects"
1199#else
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001200 "[.b, .w, .l] address number_of_objects"
York Sun4d1fd7f2014-02-26 17:03:19 -08001201#endif
wdenk8bde7f72003-06-27 21:31:46 +00001202);
1203
wdenk56523f12004-07-11 17:40:54 +00001204#ifdef CONFIG_LOOPW
1205U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001206 loopw, 4, 1, do_mem_loopw,
Peter Tyser2fb26042009-01-27 18:03:12 -06001207 "infinite write loop on address range",
York Sun4d1fd7f2014-02-26 17:03:19 -08001208#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1209 "[.b, .w, .l, .q] address number_of_objects data_to_write"
1210#else
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001211 "[.b, .w, .l] address number_of_objects data_to_write"
York Sun4d1fd7f2014-02-26 17:03:19 -08001212#endif
wdenk56523f12004-07-11 17:40:54 +00001213);
1214#endif /* CONFIG_LOOPW */
1215
Wolfgang Denka2681702013-03-08 10:51:32 +00001216#ifdef CONFIG_CMD_MEMTEST
wdenk0d498392003-07-01 21:06:45 +00001217U_BOOT_CMD(
Dirk Eibachb6fc6fd2008-12-16 14:51:56 +01001218 mtest, 5, 1, do_mem_mtest,
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001219 "simple RAM read/write test",
1220 "[start [end [pattern [iterations]]]]"
wdenk8bde7f72003-06-27 21:31:46 +00001221);
Wolfgang Denka2681702013-03-08 10:51:32 +00001222#endif /* CONFIG_CMD_MEMTEST */
wdenk8bde7f72003-06-27 21:31:46 +00001223
stroese4aaf29b2004-12-16 17:42:39 +00001224#ifdef CONFIG_MX_CYCLIC
1225U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001226 mdc, 4, 1, do_mem_mdc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001227 "memory display cyclic",
York Sun4d1fd7f2014-02-26 17:03:19 -08001228#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1229 "[.b, .w, .l, .q] address count delay(ms)"
1230#else
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001231 "[.b, .w, .l] address count delay(ms)"
York Sun4d1fd7f2014-02-26 17:03:19 -08001232#endif
stroese4aaf29b2004-12-16 17:42:39 +00001233);
1234
1235U_BOOT_CMD(
Wolfgang Denk53677ef2008-05-20 16:00:29 +02001236 mwc, 4, 1, do_mem_mwc,
Peter Tyser2fb26042009-01-27 18:03:12 -06001237 "memory write cyclic",
York Sun4d1fd7f2014-02-26 17:03:19 -08001238#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
1239 "[.b, .w, .l, .q] address value delay(ms)"
1240#else
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001241 "[.b, .w, .l] address value delay(ms)"
York Sun4d1fd7f2014-02-26 17:03:19 -08001242#endif
stroese4aaf29b2004-12-16 17:42:39 +00001243);
1244#endif /* CONFIG_MX_CYCLIC */
Simon Glass15a33e42012-11-30 13:01:20 +00001245
1246#ifdef CONFIG_CMD_MEMINFO
1247U_BOOT_CMD(
1248 meminfo, 3, 1, do_mem_info,
1249 "display memory information",
1250 ""
1251);
1252#endif