]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
3863585b WD |
2 | /* |
3 | * (C) Copyright 2000 | |
4 | * Wolfgang Denk, DENX Software Engineering, [email protected]. | |
3863585b WD |
5 | */ |
6 | ||
7 | /* | |
8 | * Memory Functions | |
9 | * | |
10 | * Copied from FADS ROM, Dan Malek ([email protected]) | |
11 | */ | |
12 | ||
13 | #include <common.h> | |
24b852a7 | 14 | #include <console.h> |
0098e179 | 15 | #include <bootretry.h> |
18d66533 | 16 | #include <cli.h> |
3863585b | 17 | #include <command.h> |
24b852a7 | 18 | #include <console.h> |
4e4bf944 | 19 | #include <display_options.h> |
17ead040 | 20 | #ifdef CONFIG_MTD_NOR_FLASH |
0ee48252 | 21 | #include <flash.h> |
17ead040 | 22 | #endif |
d20a40de | 23 | #include <hash.h> |
f7ae49fc | 24 | #include <log.h> |
0eb25b61 | 25 | #include <mapmem.h> |
90526e9f | 26 | #include <rand.h> |
a6e6fc61 | 27 | #include <watchdog.h> |
401d1c4f | 28 | #include <asm/global_data.h> |
0628ab8e | 29 | #include <asm/io.h> |
cd93d625 | 30 | #include <linux/bitops.h> |
15a33e49 | 31 | #include <linux/compiler.h> |
bdded201 | 32 | #include <linux/ctype.h> |
c05ed00a | 33 | #include <linux/delay.h> |
15a33e49 SG |
34 | |
35 | DECLARE_GLOBAL_DATA_PTR; | |
3863585b | 36 | |
76be8f75 | 37 | /* Create a compile-time value */ |
4680976f SG |
38 | #ifdef MEM_SUPPORT_64BIT_DATA |
39 | #define SUPPORT_64BIT_DATA 1 | |
76be8f75 SG |
40 | #define HELP_Q ", .q" |
41 | #else | |
4680976f | 42 | #define SUPPORT_64BIT_DATA 0 |
76be8f75 SG |
43 | #define HELP_Q "" |
44 | #endif | |
45 | ||
09140113 | 46 | static int mod_mem(struct cmd_tbl *, int, int, int, char * const []); |
3863585b WD |
47 | |
48 | /* Display values from last command. | |
49 | * Memory modify remembered values are different from display memory. | |
50 | */ | |
581508bd SW |
51 | static ulong dp_last_addr, dp_last_size; |
52 | static ulong dp_last_length = 0x40; | |
53 | static ulong mm_last_addr, mm_last_size; | |
3863585b WD |
54 | |
55 | static ulong base_address = 0; | |
550a9e79 SG |
56 | #ifdef CONFIG_CMD_MEM_SEARCH |
57 | static ulong dp_last_ms_length; | |
bdded201 SG |
58 | static u8 search_buf[64]; |
59 | static uint search_len; | |
60 | #endif | |
3863585b WD |
61 | |
62 | /* Memory Display | |
63 | * | |
64 | * Syntax: | |
4d1fd7f1 | 65 | * md{.b, .w, .l, .q} {addr} {len} |
3863585b WD |
66 | */ |
67 | #define DISP_LINE_LEN 16 | |
09140113 SG |
68 | static int do_mem_md(struct cmd_tbl *cmdtp, int flag, int argc, |
69 | char *const argv[]) | |
3863585b | 70 | { |
c68c03f5 TT |
71 | ulong addr, length, bytes; |
72 | const void *buf; | |
27b207fd | 73 | int size; |
3863585b WD |
74 | int rc = 0; |
75 | ||
76 | /* We use the last specified parameters, unless new ones are | |
77 | * entered. | |
78 | */ | |
79 | addr = dp_last_addr; | |
80 | size = dp_last_size; | |
81 | length = dp_last_length; | |
82 | ||
47e26b1b | 83 | if (argc < 2) |
4c12eeb8 | 84 | return CMD_RET_USAGE; |
3863585b WD |
85 | |
86 | if ((flag & CMD_FLAG_REPEAT) == 0) { | |
87 | /* New command specified. Check for a size specification. | |
88 | * Defaults to long if no or incorrect specification. | |
89 | */ | |
27b207fd WD |
90 | if ((size = cmd_get_data_size(argv[0], 4)) < 0) |
91 | return 1; | |
3863585b WD |
92 | |
93 | /* Address is specified since argc > 1 | |
94 | */ | |
7e5f460e | 95 | addr = hextoul(argv[1], NULL); |
3863585b WD |
96 | addr += base_address; |
97 | ||
98 | /* If another parameter, it is the length to display. | |
99 | * Length is the number of objects, not number of bytes. | |
100 | */ | |
101 | if (argc > 2) | |
7e5f460e | 102 | length = hextoul(argv[2], NULL); |
3863585b WD |
103 | } |
104 | ||
c68c03f5 TT |
105 | bytes = size * length; |
106 | buf = map_sysmem(addr, bytes); | |
0628ab8e | 107 | |
c68c03f5 TT |
108 | /* Print the lines. */ |
109 | print_buffer(addr, buf, size, length, DISP_LINE_LEN / size); | |
110 | addr += bytes; | |
111 | unmap_sysmem(buf); | |
3863585b WD |
112 | |
113 | dp_last_addr = addr; | |
114 | dp_last_length = length; | |
115 | dp_last_size = size; | |
116 | return (rc); | |
117 | } | |
118 | ||
09140113 SG |
119 | static int do_mem_mm(struct cmd_tbl *cmdtp, int flag, int argc, |
120 | char *const argv[]) | |
3863585b WD |
121 | { |
122 | return mod_mem (cmdtp, 1, flag, argc, argv); | |
123 | } | |
09140113 SG |
124 | |
125 | static int do_mem_nm(struct cmd_tbl *cmdtp, int flag, int argc, | |
126 | char *const argv[]) | |
3863585b WD |
127 | { |
128 | return mod_mem (cmdtp, 0, flag, argc, argv); | |
129 | } | |
130 | ||
09140113 SG |
131 | static int do_mem_mw(struct cmd_tbl *cmdtp, int flag, int argc, |
132 | char *const argv[]) | |
3863585b | 133 | { |
4680976f | 134 | ulong writeval; /* 64-bit if SUPPORT_64BIT_DATA */ |
4d1fd7f1 | 135 | ulong addr, count; |
27b207fd | 136 | int size; |
cc5e196e | 137 | void *buf, *start; |
0628ab8e | 138 | ulong bytes; |
3863585b | 139 | |
47e26b1b | 140 | if ((argc < 3) || (argc > 4)) |
4c12eeb8 | 141 | return CMD_RET_USAGE; |
3863585b WD |
142 | |
143 | /* Check for size specification. | |
144 | */ | |
27b207fd WD |
145 | if ((size = cmd_get_data_size(argv[0], 4)) < 1) |
146 | return 1; | |
3863585b WD |
147 | |
148 | /* Address is specified since argc > 1 | |
149 | */ | |
7e5f460e | 150 | addr = hextoul(argv[1], NULL); |
3863585b WD |
151 | addr += base_address; |
152 | ||
153 | /* Get the value to write. | |
154 | */ | |
4680976f SG |
155 | if (SUPPORT_64BIT_DATA) |
156 | writeval = simple_strtoull(argv[2], NULL, 16); | |
157 | else | |
7e5f460e | 158 | writeval = hextoul(argv[2], NULL); |
3863585b WD |
159 | |
160 | /* Count ? */ | |
161 | if (argc == 4) { | |
7e5f460e | 162 | count = hextoul(argv[3], NULL); |
3863585b WD |
163 | } else { |
164 | count = 1; | |
165 | } | |
166 | ||
0628ab8e | 167 | bytes = size * count; |
cc5e196e SG |
168 | start = map_sysmem(addr, bytes); |
169 | buf = start; | |
3863585b WD |
170 | while (count-- > 0) { |
171 | if (size == 4) | |
76698b4e | 172 | *((u32 *)buf) = (u32)writeval; |
4680976f SG |
173 | else if (SUPPORT_64BIT_DATA && size == 8) |
174 | *((ulong *)buf) = writeval; | |
3863585b | 175 | else if (size == 2) |
76698b4e | 176 | *((u16 *)buf) = (u16)writeval; |
3863585b | 177 | else |
76698b4e | 178 | *((u8 *)buf) = (u8)writeval; |
0628ab8e | 179 | buf += size; |
3863585b | 180 | } |
cc5e196e | 181 | unmap_sysmem(start); |
3863585b WD |
182 | return 0; |
183 | } | |
184 | ||
72732318 | 185 | #ifdef CONFIG_CMD_MX_CYCLIC |
09140113 SG |
186 | static int do_mem_mdc(struct cmd_tbl *cmdtp, int flag, int argc, |
187 | char *const argv[]) | |
4aaf29b2 SR |
188 | { |
189 | int i; | |
190 | ulong count; | |
191 | ||
47e26b1b | 192 | if (argc < 4) |
4c12eeb8 | 193 | return CMD_RET_USAGE; |
4aaf29b2 | 194 | |
0b1284eb | 195 | count = dectoul(argv[3], NULL); |
4aaf29b2 SR |
196 | |
197 | for (;;) { | |
198 | do_mem_md (NULL, 0, 3, argv); | |
199 | ||
200 | /* delay for <count> ms... */ | |
201 | for (i=0; i<count; i++) | |
07e11146 | 202 | udelay(1000); |
4aaf29b2 SR |
203 | |
204 | /* check for ctrl-c to abort... */ | |
205 | if (ctrlc()) { | |
206 | puts("Abort\n"); | |
207 | return 0; | |
208 | } | |
209 | } | |
210 | ||
211 | return 0; | |
212 | } | |
213 | ||
09140113 SG |
214 | static int do_mem_mwc(struct cmd_tbl *cmdtp, int flag, int argc, |
215 | char *const argv[]) | |
4aaf29b2 SR |
216 | { |
217 | int i; | |
218 | ulong count; | |
219 | ||
47e26b1b | 220 | if (argc < 4) |
4c12eeb8 | 221 | return CMD_RET_USAGE; |
4aaf29b2 | 222 | |
0b1284eb | 223 | count = dectoul(argv[3], NULL); |
4aaf29b2 SR |
224 | |
225 | for (;;) { | |
226 | do_mem_mw (NULL, 0, 3, argv); | |
227 | ||
228 | /* delay for <count> ms... */ | |
229 | for (i=0; i<count; i++) | |
07e11146 | 230 | udelay(1000); |
4aaf29b2 SR |
231 | |
232 | /* check for ctrl-c to abort... */ | |
233 | if (ctrlc()) { | |
234 | puts("Abort\n"); | |
235 | return 0; | |
236 | } | |
237 | } | |
238 | ||
239 | return 0; | |
240 | } | |
72732318 | 241 | #endif /* CONFIG_CMD_MX_CYCLIC */ |
4aaf29b2 | 242 | |
09140113 SG |
243 | static int do_mem_cmp(struct cmd_tbl *cmdtp, int flag, int argc, |
244 | char *const argv[]) | |
3863585b | 245 | { |
0628ab8e | 246 | ulong addr1, addr2, count, ngood, bytes; |
27b207fd | 247 | int size; |
3863585b | 248 | int rcode = 0; |
054ea170 | 249 | const char *type; |
0628ab8e | 250 | const void *buf1, *buf2, *base; |
4680976f | 251 | ulong word1, word2; /* 64-bit if SUPPORT_64BIT_DATA */ |
3863585b | 252 | |
47e26b1b | 253 | if (argc != 4) |
4c12eeb8 | 254 | return CMD_RET_USAGE; |
3863585b WD |
255 | |
256 | /* Check for size specification. | |
257 | */ | |
27b207fd WD |
258 | if ((size = cmd_get_data_size(argv[0], 4)) < 0) |
259 | return 1; | |
4d1fd7f1 YS |
260 | type = size == 8 ? "double word" : |
261 | size == 4 ? "word" : | |
262 | size == 2 ? "halfword" : "byte"; | |
3863585b | 263 | |
7e5f460e | 264 | addr1 = hextoul(argv[1], NULL); |
3863585b WD |
265 | addr1 += base_address; |
266 | ||
7e5f460e | 267 | addr2 = hextoul(argv[2], NULL); |
3863585b WD |
268 | addr2 += base_address; |
269 | ||
7e5f460e | 270 | count = hextoul(argv[3], NULL); |
3863585b | 271 | |
0628ab8e SG |
272 | bytes = size * count; |
273 | base = buf1 = map_sysmem(addr1, bytes); | |
274 | buf2 = map_sysmem(addr2, bytes); | |
feb12a1f | 275 | for (ngood = 0; ngood < count; ++ngood) { |
3863585b | 276 | if (size == 4) { |
76698b4e YS |
277 | word1 = *(u32 *)buf1; |
278 | word2 = *(u32 *)buf2; | |
4680976f SG |
279 | } else if (SUPPORT_64BIT_DATA && size == 8) { |
280 | word1 = *(ulong *)buf1; | |
281 | word2 = *(ulong *)buf2; | |
054ea170 | 282 | } else if (size == 2) { |
76698b4e YS |
283 | word1 = *(u16 *)buf1; |
284 | word2 = *(u16 *)buf2; | |
054ea170 | 285 | } else { |
76698b4e YS |
286 | word1 = *(u8 *)buf1; |
287 | word2 = *(u8 *)buf2; | |
3863585b | 288 | } |
054ea170 | 289 | if (word1 != word2) { |
0628ab8e | 290 | ulong offset = buf1 - base; |
054ea170 | 291 | printf("%s at 0x%08lx (%#0*lx) != %s at 0x%08lx (%#0*lx)\n", |
0628ab8e SG |
292 | type, (ulong)(addr1 + offset), size, word1, |
293 | type, (ulong)(addr2 + offset), size, word2); | |
054ea170 MF |
294 | rcode = 1; |
295 | break; | |
3863585b | 296 | } |
054ea170 | 297 | |
0628ab8e SG |
298 | buf1 += size; |
299 | buf2 += size; | |
eaadb44e SR |
300 | |
301 | /* reset watchdog from time to time */ | |
feb12a1f | 302 | if ((ngood % (64 << 10)) == 0) |
eaadb44e | 303 | WATCHDOG_RESET(); |
3863585b | 304 | } |
0628ab8e SG |
305 | unmap_sysmem(buf1); |
306 | unmap_sysmem(buf2); | |
3863585b | 307 | |
054ea170 | 308 | printf("Total of %ld %s(s) were the same\n", ngood, type); |
3863585b WD |
309 | return rcode; |
310 | } | |
311 | ||
09140113 SG |
312 | static int do_mem_cp(struct cmd_tbl *cmdtp, int flag, int argc, |
313 | char *const argv[]) | |
3863585b | 314 | { |
c2538421 | 315 | ulong addr, dest, count; |
787f10a9 | 316 | void *src, *dst; |
27b207fd | 317 | int size; |
3863585b | 318 | |
47e26b1b | 319 | if (argc != 4) |
4c12eeb8 | 320 | return CMD_RET_USAGE; |
3863585b WD |
321 | |
322 | /* Check for size specification. | |
323 | */ | |
27b207fd WD |
324 | if ((size = cmd_get_data_size(argv[0], 4)) < 0) |
325 | return 1; | |
3863585b | 326 | |
7e5f460e | 327 | addr = hextoul(argv[1], NULL); |
3863585b WD |
328 | addr += base_address; |
329 | ||
7e5f460e | 330 | dest = hextoul(argv[2], NULL); |
3863585b WD |
331 | dest += base_address; |
332 | ||
7e5f460e | 333 | count = hextoul(argv[3], NULL); |
3863585b WD |
334 | |
335 | if (count == 0) { | |
336 | puts ("Zero length ???\n"); | |
337 | return 1; | |
338 | } | |
339 | ||
787f10a9 PR |
340 | src = map_sysmem(addr, count * size); |
341 | dst = map_sysmem(dest, count * size); | |
342 | ||
e856bdcf | 343 | #ifdef CONFIG_MTD_NOR_FLASH |
3863585b | 344 | /* check if we are copying to Flash */ |
787f10a9 | 345 | if (addr2info((ulong)dst)) { |
3863585b WD |
346 | int rc; |
347 | ||
4b9206ed | 348 | puts ("Copy to Flash... "); |
3863585b | 349 | |
787f10a9 | 350 | rc = flash_write((char *)src, (ulong)dst, count * size); |
3863585b | 351 | if (rc != 0) { |
0ee48252 | 352 | flash_perror(rc); |
787f10a9 PR |
353 | unmap_sysmem(src); |
354 | unmap_sysmem(dst); | |
3863585b WD |
355 | return (1); |
356 | } | |
357 | puts ("done\n"); | |
787f10a9 PR |
358 | unmap_sysmem(src); |
359 | unmap_sysmem(dst); | |
3863585b WD |
360 | return 0; |
361 | } | |
362 | #endif | |
363 | ||
787f10a9 | 364 | memcpy(dst, src, count * size); |
a3a4749d | 365 | |
787f10a9 PR |
366 | unmap_sysmem(src); |
367 | unmap_sysmem(dst); | |
3863585b WD |
368 | return 0; |
369 | } | |
370 | ||
550a9e79 | 371 | #ifdef CONFIG_CMD_MEM_SEARCH |
bdded201 SG |
372 | static int do_mem_search(struct cmd_tbl *cmdtp, int flag, int argc, |
373 | char *const argv[]) | |
374 | { | |
375 | ulong addr, length, bytes, offset; | |
376 | u8 *ptr, *end, *buf; | |
377 | bool quiet = false; | |
378 | ulong last_pos; /* Offset of last match in 'size' units*/ | |
379 | ulong last_addr; /* Address of last displayed line */ | |
380 | int limit = 10; | |
550a9e79 | 381 | int used_len; |
bdded201 SG |
382 | int count; |
383 | int size; | |
384 | int i; | |
385 | ||
386 | /* We use the last specified parameters, unless new ones are entered */ | |
387 | addr = dp_last_addr; | |
388 | size = dp_last_size; | |
550a9e79 | 389 | length = dp_last_ms_length; |
bdded201 SG |
390 | |
391 | if (argc < 3) | |
392 | return CMD_RET_USAGE; | |
393 | ||
550a9e79 | 394 | if (!(flag & CMD_FLAG_REPEAT)) { |
bdded201 SG |
395 | /* |
396 | * Check for a size specification. | |
397 | * Defaults to long if no or incorrect specification. | |
398 | */ | |
399 | size = cmd_get_data_size(argv[0], 4); | |
7526deec | 400 | if (size < 0 && size != CMD_DATA_SIZE_STR) |
bdded201 SG |
401 | return 1; |
402 | ||
550a9e79 SG |
403 | argc--; |
404 | argv++; | |
bdded201 SG |
405 | while (argc && *argv[0] == '-') { |
406 | int ch = argv[0][1]; | |
407 | ||
408 | if (ch == 'q') | |
409 | quiet = true; | |
410 | else if (ch == 'l' && isxdigit(argv[0][2])) | |
7e5f460e | 411 | limit = hextoul(argv[0] + 2, NULL); |
bdded201 SG |
412 | else |
413 | return CMD_RET_USAGE; | |
550a9e79 SG |
414 | argc--; |
415 | argv++; | |
bdded201 SG |
416 | } |
417 | ||
418 | /* Address is specified since argc > 1 */ | |
7e5f460e | 419 | addr = hextoul(argv[0], NULL); |
bdded201 SG |
420 | addr += base_address; |
421 | ||
422 | /* Length is the number of objects, not number of bytes */ | |
7e5f460e | 423 | length = hextoul(argv[1], NULL); |
bdded201 SG |
424 | |
425 | /* Read the bytes to search for */ | |
426 | end = search_buf + sizeof(search_buf); | |
427 | for (i = 2, ptr = search_buf; i < argc && ptr < end; i++) { | |
550a9e79 | 428 | if (MEM_SUPPORT_64BIT_DATA && size == 8) { |
bdded201 SG |
429 | u64 val = simple_strtoull(argv[i], NULL, 16); |
430 | ||
431 | *(u64 *)ptr = val; | |
432 | } else if (size == -2) { /* string */ | |
433 | int len = min(strlen(argv[i]), | |
434 | (size_t)(end - ptr)); | |
435 | ||
436 | memcpy(ptr, argv[i], len); | |
437 | ptr += len; | |
438 | continue; | |
439 | } else { | |
7e5f460e | 440 | u32 val = hextoul(argv[i], NULL); |
bdded201 SG |
441 | |
442 | switch (size) { | |
443 | case 1: | |
444 | *ptr = val; | |
445 | break; | |
446 | case 2: | |
447 | *(u16 *)ptr = val; | |
448 | break; | |
449 | case 4: | |
450 | *(u32 *)ptr = val; | |
451 | break; | |
452 | } | |
453 | } | |
454 | ptr += size; | |
455 | } | |
456 | search_len = ptr - search_buf; | |
457 | } | |
458 | ||
459 | /* Do the search */ | |
460 | if (size == -2) | |
461 | size = 1; | |
462 | bytes = size * length; | |
463 | buf = map_sysmem(addr, bytes); | |
464 | last_pos = 0; | |
465 | last_addr = 0; | |
466 | count = 0; | |
550a9e79 SG |
467 | for (offset = 0; |
468 | offset < bytes && offset <= bytes - search_len && count < limit; | |
bdded201 SG |
469 | offset += size) { |
470 | void *ptr = buf + offset; | |
471 | ||
472 | if (!memcmp(ptr, search_buf, search_len)) { | |
473 | uint align = (addr + offset) & 0xf; | |
474 | ulong match = addr + offset; | |
475 | ||
476 | if (!count || (last_addr & ~0xf) != (match & ~0xf)) { | |
477 | if (!quiet) { | |
478 | if (count) | |
479 | printf("--\n"); | |
480 | print_buffer(match - align, ptr - align, | |
481 | size, | |
482 | ALIGN(search_len + align, | |
483 | 16) / size, 0); | |
484 | } | |
485 | last_addr = match; | |
486 | last_pos = offset / size; | |
487 | } | |
488 | count++; | |
489 | } | |
490 | } | |
491 | if (!quiet) { | |
492 | printf("%d match%s", count, count == 1 ? "" : "es"); | |
493 | if (count == limit) | |
494 | printf(" (repeat command to check for more)"); | |
495 | printf("\n"); | |
496 | } | |
497 | env_set_hex("memmatches", count); | |
498 | env_set_hex("memaddr", last_addr); | |
499 | env_set_hex("mempos", last_pos); | |
500 | ||
501 | unmap_sysmem(buf); | |
502 | ||
550a9e79 SG |
503 | used_len = offset / size; |
504 | dp_last_addr = addr + used_len; | |
bdded201 | 505 | dp_last_size = size; |
550a9e79 | 506 | dp_last_ms_length = length < used_len ? 0 : length - used_len; |
bdded201 SG |
507 | |
508 | return count ? 0 : CMD_RET_FAILURE; | |
509 | } | |
510 | #endif | |
511 | ||
09140113 SG |
512 | static int do_mem_base(struct cmd_tbl *cmdtp, int flag, int argc, |
513 | char *const argv[]) | |
3863585b WD |
514 | { |
515 | if (argc > 1) { | |
516 | /* Set new base address. | |
517 | */ | |
7e5f460e | 518 | base_address = hextoul(argv[1], NULL); |
3863585b WD |
519 | } |
520 | /* Print the current base address. | |
521 | */ | |
522 | printf("Base Address: 0x%08lx\n", base_address); | |
523 | return 0; | |
524 | } | |
525 | ||
09140113 SG |
526 | static int do_mem_loop(struct cmd_tbl *cmdtp, int flag, int argc, |
527 | char *const argv[]) | |
3863585b | 528 | { |
0628ab8e | 529 | ulong addr, length, i, bytes; |
27b207fd | 530 | int size; |
4680976f | 531 | volatile ulong *llp; /* 64-bit if SUPPORT_64BIT_DATA */ |
76698b4e YS |
532 | volatile u32 *longp; |
533 | volatile u16 *shortp; | |
534 | volatile u8 *cp; | |
0628ab8e | 535 | const void *buf; |
3863585b | 536 | |
47e26b1b | 537 | if (argc < 3) |
4c12eeb8 | 538 | return CMD_RET_USAGE; |
3863585b | 539 | |
85de63e2 RD |
540 | /* |
541 | * Check for a size specification. | |
3863585b WD |
542 | * Defaults to long if no or incorrect specification. |
543 | */ | |
27b207fd WD |
544 | if ((size = cmd_get_data_size(argv[0], 4)) < 0) |
545 | return 1; | |
3863585b WD |
546 | |
547 | /* Address is always specified. | |
548 | */ | |
7e5f460e | 549 | addr = hextoul(argv[1], NULL); |
3863585b WD |
550 | |
551 | /* Length is the number of objects, not number of bytes. | |
552 | */ | |
7e5f460e | 553 | length = hextoul(argv[2], NULL); |
3863585b | 554 | |
0628ab8e SG |
555 | bytes = size * length; |
556 | buf = map_sysmem(addr, bytes); | |
557 | ||
3863585b WD |
558 | /* We want to optimize the loops to run as fast as possible. |
559 | * If we have only one object, just run infinite loops. | |
560 | */ | |
561 | if (length == 1) { | |
4680976f SG |
562 | if (SUPPORT_64BIT_DATA && size == 8) { |
563 | llp = (ulong *)buf; | |
4d1fd7f1 YS |
564 | for (;;) |
565 | i = *llp; | |
566 | } | |
3863585b | 567 | if (size == 4) { |
76698b4e | 568 | longp = (u32 *)buf; |
3863585b WD |
569 | for (;;) |
570 | i = *longp; | |
571 | } | |
572 | if (size == 2) { | |
76698b4e | 573 | shortp = (u16 *)buf; |
3863585b WD |
574 | for (;;) |
575 | i = *shortp; | |
576 | } | |
76698b4e | 577 | cp = (u8 *)buf; |
3863585b WD |
578 | for (;;) |
579 | i = *cp; | |
580 | } | |
581 | ||
4680976f | 582 | if (SUPPORT_64BIT_DATA && size == 8) { |
4d1fd7f1 | 583 | for (;;) { |
4680976f | 584 | llp = (ulong *)buf; |
4d1fd7f1 YS |
585 | i = length; |
586 | while (i-- > 0) | |
587 | *llp++; | |
588 | } | |
589 | } | |
3863585b WD |
590 | if (size == 4) { |
591 | for (;;) { | |
76698b4e | 592 | longp = (u32 *)buf; |
3863585b WD |
593 | i = length; |
594 | while (i-- > 0) | |
f3b3c3df | 595 | *longp++; |
3863585b WD |
596 | } |
597 | } | |
598 | if (size == 2) { | |
599 | for (;;) { | |
76698b4e | 600 | shortp = (u16 *)buf; |
3863585b WD |
601 | i = length; |
602 | while (i-- > 0) | |
f3b3c3df | 603 | *shortp++; |
3863585b WD |
604 | } |
605 | } | |
606 | for (;;) { | |
76698b4e | 607 | cp = (u8 *)buf; |
3863585b WD |
608 | i = length; |
609 | while (i-- > 0) | |
f3b3c3df | 610 | *cp++; |
3863585b | 611 | } |
0628ab8e | 612 | unmap_sysmem(buf); |
92765f42 SG |
613 | |
614 | return 0; | |
3863585b WD |
615 | } |
616 | ||
56523f12 | 617 | #ifdef CONFIG_LOOPW |
09140113 SG |
618 | static int do_mem_loopw(struct cmd_tbl *cmdtp, int flag, int argc, |
619 | char *const argv[]) | |
56523f12 | 620 | { |
4d1fd7f1 | 621 | ulong addr, length, i, bytes; |
56523f12 | 622 | int size; |
4680976f SG |
623 | volatile ulong *llp; /* 64-bit if SUPPORT_64BIT_DATA */ |
624 | ulong data; /* 64-bit if SUPPORT_64BIT_DATA */ | |
76698b4e YS |
625 | volatile u32 *longp; |
626 | volatile u16 *shortp; | |
627 | volatile u8 *cp; | |
0628ab8e | 628 | void *buf; |
81050926 | 629 | |
47e26b1b | 630 | if (argc < 4) |
4c12eeb8 | 631 | return CMD_RET_USAGE; |
56523f12 | 632 | |
85de63e2 RD |
633 | /* |
634 | * Check for a size specification. | |
56523f12 WD |
635 | * Defaults to long if no or incorrect specification. |
636 | */ | |
637 | if ((size = cmd_get_data_size(argv[0], 4)) < 0) | |
638 | return 1; | |
639 | ||
640 | /* Address is always specified. | |
641 | */ | |
7e5f460e | 642 | addr = hextoul(argv[1], NULL); |
56523f12 WD |
643 | |
644 | /* Length is the number of objects, not number of bytes. | |
645 | */ | |
7e5f460e | 646 | length = hextoul(argv[2], NULL); |
56523f12 WD |
647 | |
648 | /* data to write */ | |
4680976f SG |
649 | if (SUPPORT_64BIT_DATA) |
650 | data = simple_strtoull(argv[3], NULL, 16); | |
651 | else | |
7e5f460e | 652 | data = hextoul(argv[3], NULL); |
81050926 | 653 | |
0628ab8e SG |
654 | bytes = size * length; |
655 | buf = map_sysmem(addr, bytes); | |
656 | ||
56523f12 WD |
657 | /* We want to optimize the loops to run as fast as possible. |
658 | * If we have only one object, just run infinite loops. | |
659 | */ | |
660 | if (length == 1) { | |
4680976f SG |
661 | if (SUPPORT_64BIT_DATA && size == 8) { |
662 | llp = (ulong *)buf; | |
4d1fd7f1 YS |
663 | for (;;) |
664 | *llp = data; | |
665 | } | |
56523f12 | 666 | if (size == 4) { |
76698b4e | 667 | longp = (u32 *)buf; |
56523f12 WD |
668 | for (;;) |
669 | *longp = data; | |
4d1fd7f1 | 670 | } |
56523f12 | 671 | if (size == 2) { |
76698b4e | 672 | shortp = (u16 *)buf; |
56523f12 WD |
673 | for (;;) |
674 | *shortp = data; | |
675 | } | |
76698b4e | 676 | cp = (u8 *)buf; |
56523f12 WD |
677 | for (;;) |
678 | *cp = data; | |
679 | } | |
680 | ||
4680976f | 681 | if (SUPPORT_64BIT_DATA && size == 8) { |
4d1fd7f1 | 682 | for (;;) { |
4680976f | 683 | llp = (ulong *)buf; |
4d1fd7f1 YS |
684 | i = length; |
685 | while (i-- > 0) | |
686 | *llp++ = data; | |
687 | } | |
688 | } | |
56523f12 WD |
689 | if (size == 4) { |
690 | for (;;) { | |
76698b4e | 691 | longp = (u32 *)buf; |
56523f12 WD |
692 | i = length; |
693 | while (i-- > 0) | |
694 | *longp++ = data; | |
695 | } | |
696 | } | |
697 | if (size == 2) { | |
698 | for (;;) { | |
76698b4e | 699 | shortp = (u16 *)buf; |
56523f12 WD |
700 | i = length; |
701 | while (i-- > 0) | |
702 | *shortp++ = data; | |
703 | } | |
704 | } | |
705 | for (;;) { | |
76698b4e | 706 | cp = (u8 *)buf; |
56523f12 WD |
707 | i = length; |
708 | while (i-- > 0) | |
709 | *cp++ = data; | |
710 | } | |
711 | } | |
712 | #endif /* CONFIG_LOOPW */ | |
713 | ||
68149e94 | 714 | #ifdef CONFIG_CMD_MEMTEST |
5512d5b0 SG |
715 | static ulong mem_test_alt(vu_long *buf, ulong start_addr, ulong end_addr, |
716 | vu_long *dummy) | |
3863585b | 717 | { |
c9638f50 | 718 | vu_long *addr; |
c9638f50 SG |
719 | ulong errs = 0; |
720 | ulong val, readback; | |
721 | int j; | |
c9638f50 SG |
722 | vu_long offset; |
723 | vu_long test_offset; | |
724 | vu_long pattern; | |
725 | vu_long temp; | |
726 | vu_long anti_pattern; | |
727 | vu_long num_words; | |
3863585b WD |
728 | static const ulong bitpattern[] = { |
729 | 0x00000001, /* single bit */ | |
730 | 0x00000003, /* two adjacent bits */ | |
731 | 0x00000007, /* three adjacent bits */ | |
732 | 0x0000000F, /* four adjacent bits */ | |
733 | 0x00000005, /* two non-adjacent bits */ | |
734 | 0x00000015, /* three non-adjacent bits */ | |
735 | 0x00000055, /* four non-adjacent bits */ | |
736 | 0xaaaaaaaa, /* alternating 1/0 */ | |
737 | }; | |
3863585b | 738 | |
5512d5b0 | 739 | num_words = (end_addr - start_addr) / sizeof(vu_long); |
8c86bbe0 | 740 | |
7ecbd4d7 SG |
741 | /* |
742 | * Data line test: write a pattern to the first | |
743 | * location, write the 1's complement to a 'parking' | |
744 | * address (changes the state of the data bus so a | |
745 | * floating bus doesn't give a false OK), and then | |
746 | * read the value back. Note that we read it back | |
747 | * into a variable because the next time we read it, | |
748 | * it might be right (been there, tough to explain to | |
749 | * the quality guys why it prints a failure when the | |
750 | * "is" and "should be" are obviously the same in the | |
751 | * error message). | |
752 | * | |
753 | * Rather than exhaustively testing, we test some | |
754 | * patterns by shifting '1' bits through a field of | |
755 | * '0's and '0' bits through a field of '1's (i.e. | |
756 | * pattern and ~pattern). | |
757 | */ | |
5512d5b0 | 758 | addr = buf; |
7ecbd4d7 SG |
759 | for (j = 0; j < sizeof(bitpattern) / sizeof(bitpattern[0]); j++) { |
760 | val = bitpattern[j]; | |
761 | for (; val != 0; val <<= 1) { | |
5512d5b0 | 762 | *addr = val; |
c9638f50 | 763 | *dummy = ~val; /* clear the test data off the bus */ |
3863585b | 764 | readback = *addr; |
7ecbd4d7 | 765 | if (readback != val) { |
c9638f50 SG |
766 | printf("FAILURE (data line): " |
767 | "expected %08lx, actual %08lx\n", | |
768 | val, readback); | |
769 | errs++; | |
c44d4386 | 770 | if (ctrlc()) |
51209b1f | 771 | return -1; |
3863585b WD |
772 | } |
773 | *addr = ~val; | |
774 | *dummy = val; | |
775 | readback = *addr; | |
c9638f50 SG |
776 | if (readback != ~val) { |
777 | printf("FAILURE (data line): " | |
778 | "Is %08lx, should be %08lx\n", | |
779 | readback, ~val); | |
780 | errs++; | |
c44d4386 | 781 | if (ctrlc()) |
51209b1f | 782 | return -1; |
3863585b | 783 | } |
3863585b | 784 | } |
7ecbd4d7 | 785 | } |
3863585b | 786 | |
7ecbd4d7 SG |
787 | /* |
788 | * Based on code whose Original Author and Copyright | |
789 | * information follows: Copyright (c) 1998 by Michael | |
790 | * Barr. This software is placed into the public | |
791 | * domain and may be used for any purpose. However, | |
792 | * this notice must not be changed or removed and no | |
793 | * warranty is either expressed or implied by its | |
794 | * publication or distribution. | |
795 | */ | |
3863585b | 796 | |
7ecbd4d7 SG |
797 | /* |
798 | * Address line test | |
799 | ||
800 | * Description: Test the address bus wiring in a | |
801 | * memory region by performing a walking | |
802 | * 1's test on the relevant bits of the | |
803 | * address and checking for aliasing. | |
804 | * This test will find single-bit | |
805 | * address failures such as stuck-high, | |
806 | * stuck-low, and shorted pins. The base | |
807 | * address and size of the region are | |
808 | * selected by the caller. | |
809 | ||
810 | * Notes: For best results, the selected base | |
811 | * address should have enough LSB 0's to | |
812 | * guarantee single address bit changes. | |
813 | * For example, to test a 64-Kbyte | |
814 | * region, select a base address on a | |
815 | * 64-Kbyte boundary. Also, select the | |
816 | * region size as a power-of-two if at | |
817 | * all possible. | |
818 | * | |
819 | * Returns: 0 if the test succeeds, 1 if the test fails. | |
820 | */ | |
7ecbd4d7 SG |
821 | pattern = (vu_long) 0xaaaaaaaa; |
822 | anti_pattern = (vu_long) 0x55555555; | |
3863585b | 823 | |
5512d5b0 | 824 | debug("%s:%d: length = 0x%.8lx\n", __func__, __LINE__, num_words); |
7ecbd4d7 SG |
825 | /* |
826 | * Write the default pattern at each of the | |
827 | * power-of-two offsets. | |
828 | */ | |
5512d5b0 SG |
829 | for (offset = 1; offset < num_words; offset <<= 1) |
830 | addr[offset] = pattern; | |
3863585b | 831 | |
7ecbd4d7 SG |
832 | /* |
833 | * Check for address bits stuck high. | |
834 | */ | |
835 | test_offset = 0; | |
5512d5b0 | 836 | addr[test_offset] = anti_pattern; |
3863585b | 837 | |
5512d5b0 SG |
838 | for (offset = 1; offset < num_words; offset <<= 1) { |
839 | temp = addr[offset]; | |
7ecbd4d7 | 840 | if (temp != pattern) { |
c9638f50 | 841 | printf("\nFAILURE: Address bit stuck high @ 0x%.8lx:" |
3863585b | 842 | " expected 0x%.8lx, actual 0x%.8lx\n", |
102c051f DF |
843 | start_addr + offset*sizeof(vu_long), |
844 | pattern, temp); | |
87b22b77 | 845 | errs++; |
c44d4386 | 846 | if (ctrlc()) |
7ecbd4d7 | 847 | return -1; |
3863585b | 848 | } |
7ecbd4d7 | 849 | } |
5512d5b0 | 850 | addr[test_offset] = pattern; |
7ecbd4d7 | 851 | WATCHDOG_RESET(); |
3863585b | 852 | |
7ecbd4d7 SG |
853 | /* |
854 | * Check for addr bits stuck low or shorted. | |
855 | */ | |
5512d5b0 SG |
856 | for (test_offset = 1; test_offset < num_words; test_offset <<= 1) { |
857 | addr[test_offset] = anti_pattern; | |
3863585b | 858 | |
5512d5b0 SG |
859 | for (offset = 1; offset < num_words; offset <<= 1) { |
860 | temp = addr[offset]; | |
3863585b | 861 | if ((temp != pattern) && (offset != test_offset)) { |
7ecbd4d7 SG |
862 | printf("\nFAILURE: Address bit stuck low or" |
863 | " shorted @ 0x%.8lx: expected 0x%.8lx," | |
864 | " actual 0x%.8lx\n", | |
102c051f DF |
865 | start_addr + offset*sizeof(vu_long), |
866 | pattern, temp); | |
7ecbd4d7 | 867 | errs++; |
c44d4386 | 868 | if (ctrlc()) |
7ecbd4d7 | 869 | return -1; |
3863585b | 870 | } |
3863585b | 871 | } |
5512d5b0 | 872 | addr[test_offset] = pattern; |
7ecbd4d7 | 873 | } |
3863585b | 874 | |
7ecbd4d7 SG |
875 | /* |
876 | * Description: Test the integrity of a physical | |
877 | * memory device by performing an | |
878 | * increment/decrement test over the | |
879 | * entire region. In the process every | |
880 | * storage bit in the device is tested | |
881 | * as a zero and a one. The base address | |
882 | * and the size of the region are | |
883 | * selected by the caller. | |
884 | * | |
885 | * Returns: 0 if the test succeeds, 1 if the test fails. | |
886 | */ | |
5512d5b0 | 887 | num_words++; |
3863585b | 888 | |
7ecbd4d7 SG |
889 | /* |
890 | * Fill memory with a known pattern. | |
891 | */ | |
892 | for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) { | |
893 | WATCHDOG_RESET(); | |
5512d5b0 | 894 | addr[offset] = pattern; |
7ecbd4d7 | 895 | } |
3863585b | 896 | |
7ecbd4d7 SG |
897 | /* |
898 | * Check each location and invert it for the second pass. | |
899 | */ | |
900 | for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) { | |
901 | WATCHDOG_RESET(); | |
5512d5b0 | 902 | temp = addr[offset]; |
7ecbd4d7 | 903 | if (temp != pattern) { |
c9638f50 | 904 | printf("\nFAILURE (read/write) @ 0x%.8lx:" |
3863585b | 905 | " expected 0x%.8lx, actual 0x%.8lx)\n", |
102c051f DF |
906 | start_addr + offset*sizeof(vu_long), |
907 | pattern, temp); | |
87b22b77 | 908 | errs++; |
c44d4386 | 909 | if (ctrlc()) |
51209b1f | 910 | return -1; |
3863585b WD |
911 | } |
912 | ||
7ecbd4d7 | 913 | anti_pattern = ~pattern; |
5512d5b0 | 914 | addr[offset] = anti_pattern; |
7ecbd4d7 SG |
915 | } |
916 | ||
917 | /* | |
918 | * Check each location for the inverted pattern and zero it. | |
919 | */ | |
920 | for (pattern = 1, offset = 0; offset < num_words; pattern++, offset++) { | |
921 | WATCHDOG_RESET(); | |
922 | anti_pattern = ~pattern; | |
5512d5b0 | 923 | temp = addr[offset]; |
7ecbd4d7 | 924 | if (temp != anti_pattern) { |
c9638f50 | 925 | printf("\nFAILURE (read/write): @ 0x%.8lx:" |
3863585b | 926 | " expected 0x%.8lx, actual 0x%.8lx)\n", |
102c051f DF |
927 | start_addr + offset*sizeof(vu_long), |
928 | anti_pattern, temp); | |
87b22b77 | 929 | errs++; |
c44d4386 | 930 | if (ctrlc()) |
51209b1f | 931 | return -1; |
3863585b | 932 | } |
5512d5b0 | 933 | addr[offset] = 0; |
7ecbd4d7 | 934 | } |
51209b1f | 935 | |
fea6730e | 936 | return errs; |
c9638f50 SG |
937 | } |
938 | ||
8e434cb7 SR |
939 | static int compare_regions(volatile unsigned long *bufa, |
940 | volatile unsigned long *bufb, size_t count) | |
941 | { | |
942 | volatile unsigned long *p1 = bufa; | |
943 | volatile unsigned long *p2 = bufb; | |
944 | int errs = 0; | |
945 | size_t i; | |
946 | ||
947 | for (i = 0; i < count; i++, p1++, p2++) { | |
948 | if (*p1 != *p2) { | |
949 | printf("FAILURE: 0x%08lx != 0x%08lx (delta=0x%08lx -> bit %ld) at offset 0x%08lx\n", | |
950 | (unsigned long)*p1, (unsigned long)*p2, | |
951 | *p1 ^ *p2, __ffs(*p1 ^ *p2), | |
952 | (unsigned long)(i * sizeof(unsigned long))); | |
953 | errs++; | |
954 | } | |
955 | } | |
956 | ||
957 | return errs; | |
958 | } | |
959 | ||
960 | static ulong test_bitflip_comparison(volatile unsigned long *bufa, | |
961 | volatile unsigned long *bufb, size_t count) | |
962 | { | |
963 | volatile unsigned long *p1 = bufa; | |
964 | volatile unsigned long *p2 = bufb; | |
965 | unsigned int j, k; | |
966 | unsigned long q; | |
967 | size_t i; | |
968 | int max; | |
969 | int errs = 0; | |
970 | ||
971 | max = sizeof(unsigned long) * 8; | |
972 | for (k = 0; k < max; k++) { | |
973 | q = 0x00000001L << k; | |
974 | for (j = 0; j < 8; j++) { | |
975 | WATCHDOG_RESET(); | |
976 | q = ~q; | |
977 | p1 = (volatile unsigned long *)bufa; | |
978 | p2 = (volatile unsigned long *)bufb; | |
979 | for (i = 0; i < count; i++) | |
980 | *p1++ = *p2++ = (i % 2) == 0 ? q : ~q; | |
981 | ||
982 | errs += compare_regions(bufa, bufb, count); | |
983 | } | |
984 | ||
985 | if (ctrlc()) | |
986 | return -1UL; | |
987 | } | |
988 | ||
989 | return errs; | |
990 | } | |
991 | ||
9989fb18 RS |
992 | static ulong mem_test_bitflip(vu_long *buf, ulong start, ulong end) |
993 | { | |
994 | /* | |
995 | * Split the specified range into two halves. | |
996 | * Note that mtest range is inclusive of start,end. | |
997 | * Bitflip test instead uses a count (of 32-bit words). | |
998 | */ | |
999 | ulong half_size = (end - start + 1) / 2 / sizeof(unsigned long); | |
1000 | ||
1001 | return test_bitflip_comparison(buf, buf + half_size, half_size); | |
1002 | } | |
1003 | ||
5512d5b0 SG |
1004 | static ulong mem_test_quick(vu_long *buf, ulong start_addr, ulong end_addr, |
1005 | vu_long pattern, int iteration) | |
c9638f50 | 1006 | { |
5512d5b0 | 1007 | vu_long *end; |
c9638f50 | 1008 | vu_long *addr; |
c9638f50 | 1009 | ulong errs = 0; |
5512d5b0 | 1010 | ulong incr, length; |
c9638f50 | 1011 | ulong val, readback; |
3863585b | 1012 | |
51209b1f | 1013 | /* Alternate the pattern */ |
3863585b | 1014 | incr = 1; |
51209b1f SG |
1015 | if (iteration & 1) { |
1016 | incr = -incr; | |
1017 | /* | |
1018 | * Flip the pattern each time to make lots of zeros and | |
1019 | * then, the next time, lots of ones. We decrement | |
1020 | * the "negative" patterns and increment the "positive" | |
1021 | * patterns to preserve this feature. | |
1022 | */ | |
1023 | if (pattern & 0x80000000) | |
1024 | pattern = -pattern; /* complement & increment */ | |
1025 | else | |
1026 | pattern = ~pattern; | |
1027 | } | |
5512d5b0 SG |
1028 | length = (end_addr - start_addr) / sizeof(ulong); |
1029 | end = buf + length; | |
7ecbd4d7 SG |
1030 | printf("\rPattern %08lX Writing..." |
1031 | "%12s" | |
1032 | "\b\b\b\b\b\b\b\b\b\b", | |
1033 | pattern, ""); | |
3863585b | 1034 | |
5512d5b0 | 1035 | for (addr = buf, val = pattern; addr < end; addr++) { |
7ecbd4d7 SG |
1036 | WATCHDOG_RESET(); |
1037 | *addr = val; | |
1038 | val += incr; | |
1039 | } | |
3863585b | 1040 | |
7ecbd4d7 | 1041 | puts("Reading..."); |
3863585b | 1042 | |
5512d5b0 | 1043 | for (addr = buf, val = pattern; addr < end; addr++) { |
7ecbd4d7 SG |
1044 | WATCHDOG_RESET(); |
1045 | readback = *addr; | |
1046 | if (readback != val) { | |
5512d5b0 SG |
1047 | ulong offset = addr - buf; |
1048 | ||
7ecbd4d7 SG |
1049 | printf("\nMem error @ 0x%08X: " |
1050 | "found %08lX, expected %08lX\n", | |
102c051f | 1051 | (uint)(uintptr_t)(start_addr + offset*sizeof(vu_long)), |
5512d5b0 | 1052 | readback, val); |
7ecbd4d7 | 1053 | errs++; |
c44d4386 | 1054 | if (ctrlc()) |
7ecbd4d7 | 1055 | return -1; |
3863585b | 1056 | } |
7ecbd4d7 SG |
1057 | val += incr; |
1058 | } | |
3863585b | 1059 | |
fea6730e | 1060 | return errs; |
c9638f50 SG |
1061 | } |
1062 | ||
1063 | /* | |
1064 | * Perform a memory test. A more complete alternative test can be | |
1065 | * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until | |
1066 | * interrupted by ctrl-c or by a failure of one of the sub-tests. | |
1067 | */ | |
09140113 SG |
1068 | static int do_mem_mtest(struct cmd_tbl *cmdtp, int flag, int argc, |
1069 | char *const argv[]) | |
c9638f50 | 1070 | { |
8c86bbe0 | 1071 | ulong start, end; |
e519f03a MS |
1072 | vu_long scratch_space; |
1073 | vu_long *buf, *dummy = &scratch_space; | |
adcc5705 | 1074 | ulong iteration_limit = 0; |
a8c708ea | 1075 | ulong count = 0; |
51209b1f | 1076 | ulong errs = 0; /* number of errors, or -1 if interrupted */ |
e37f1eb4 | 1077 | ulong pattern = 0; |
51209b1f | 1078 | int iteration; |
c9638f50 | 1079 | |
e37f1eb4 PM |
1080 | start = CONFIG_SYS_MEMTEST_START; |
1081 | end = CONFIG_SYS_MEMTEST_END; | |
1082 | ||
c9638f50 | 1083 | if (argc > 1) |
e37f1eb4 PM |
1084 | if (strict_strtoul(argv[1], 16, &start) < 0) |
1085 | return CMD_RET_USAGE; | |
c9638f50 SG |
1086 | |
1087 | if (argc > 2) | |
e37f1eb4 PM |
1088 | if (strict_strtoul(argv[2], 16, &end) < 0) |
1089 | return CMD_RET_USAGE; | |
c9638f50 SG |
1090 | |
1091 | if (argc > 3) | |
e37f1eb4 PM |
1092 | if (strict_strtoul(argv[3], 16, &pattern) < 0) |
1093 | return CMD_RET_USAGE; | |
c9638f50 SG |
1094 | |
1095 | if (argc > 4) | |
e37f1eb4 PM |
1096 | if (strict_strtoul(argv[4], 16, &iteration_limit) < 0) |
1097 | return CMD_RET_USAGE; | |
1098 | ||
1099 | if (end < start) { | |
1100 | printf("Refusing to do empty test\n"); | |
1101 | return -1; | |
1102 | } | |
c9638f50 | 1103 | |
dfe461d6 | 1104 | printf("Testing %08lx ... %08lx:\n", start, end); |
8c86bbe0 SG |
1105 | debug("%s:%d: start %#08lx end %#08lx\n", __func__, __LINE__, |
1106 | start, end); | |
51209b1f | 1107 | |
5512d5b0 | 1108 | buf = map_sysmem(start, end - start); |
51209b1f SG |
1109 | for (iteration = 0; |
1110 | !iteration_limit || iteration < iteration_limit; | |
1111 | iteration++) { | |
1112 | if (ctrlc()) { | |
51209b1f SG |
1113 | errs = -1UL; |
1114 | break; | |
1115 | } | |
1116 | ||
1117 | printf("Iteration: %6d\r", iteration + 1); | |
1118 | debug("\n"); | |
f14bfa7e | 1119 | if (IS_ENABLED(CONFIG_SYS_ALT_MEMTEST)) { |
5512d5b0 | 1120 | errs = mem_test_alt(buf, start, end, dummy); |
8e434cb7 SR |
1121 | if (errs == -1UL) |
1122 | break; | |
9989fb18 RS |
1123 | if (IS_ENABLED(CONFIG_SYS_ALT_MEMTEST_BITFLIP)) { |
1124 | count += errs; | |
1125 | errs = mem_test_bitflip(buf, start, end); | |
1126 | } | |
5512d5b0 SG |
1127 | } else { |
1128 | errs = mem_test_quick(buf, start, end, pattern, | |
1129 | iteration); | |
1130 | } | |
1131 | if (errs == -1UL) | |
1132 | break; | |
a8c708ea | 1133 | count += errs; |
5512d5b0 SG |
1134 | } |
1135 | ||
54de244c | 1136 | unmap_sysmem((void *)buf); |
51209b1f SG |
1137 | |
1138 | if (errs == -1UL) { | |
c44d4386 SG |
1139 | /* Memory test was aborted - write a newline to finish off */ |
1140 | putc('\n'); | |
51209b1f | 1141 | } |
a8c708ea | 1142 | printf("Tested %d iteration(s) with %lu errors.\n", iteration, count); |
c9638f50 | 1143 | |
a8c708ea | 1144 | return errs != 0; |
3863585b | 1145 | } |
a2681707 | 1146 | #endif /* CONFIG_CMD_MEMTEST */ |
3863585b WD |
1147 | |
1148 | /* Modify memory. | |
1149 | * | |
1150 | * Syntax: | |
4d1fd7f1 | 1151 | * mm{.b, .w, .l, .q} {addr} |
3863585b WD |
1152 | */ |
1153 | static int | |
09140113 SG |
1154 | mod_mem(struct cmd_tbl *cmdtp, int incrflag, int flag, int argc, |
1155 | char *const argv[]) | |
3863585b | 1156 | { |
4d1fd7f1 | 1157 | ulong addr; |
4680976f | 1158 | ulong i; /* 64-bit if SUPPORT_64BIT_DATA */ |
27b207fd | 1159 | int nbytes, size; |
0628ab8e | 1160 | void *ptr = NULL; |
3863585b | 1161 | |
47e26b1b | 1162 | if (argc != 2) |
4c12eeb8 | 1163 | return CMD_RET_USAGE; |
3863585b | 1164 | |
b26440f1 | 1165 | bootretry_reset_cmd_timeout(); /* got a good command to get here */ |
3863585b WD |
1166 | /* We use the last specified parameters, unless new ones are |
1167 | * entered. | |
1168 | */ | |
1169 | addr = mm_last_addr; | |
1170 | size = mm_last_size; | |
1171 | ||
1172 | if ((flag & CMD_FLAG_REPEAT) == 0) { | |
1173 | /* New command specified. Check for a size specification. | |
1174 | * Defaults to long if no or incorrect specification. | |
1175 | */ | |
27b207fd WD |
1176 | if ((size = cmd_get_data_size(argv[0], 4)) < 0) |
1177 | return 1; | |
3863585b WD |
1178 | |
1179 | /* Address is specified since argc > 1 | |
1180 | */ | |
7e5f460e | 1181 | addr = hextoul(argv[1], NULL); |
3863585b WD |
1182 | addr += base_address; |
1183 | } | |
1184 | ||
1185 | /* Print the address, followed by value. Then accept input for | |
1186 | * the next value. A non-converted value exits. | |
1187 | */ | |
1188 | do { | |
0628ab8e | 1189 | ptr = map_sysmem(addr, size); |
3863585b WD |
1190 | printf("%08lx:", addr); |
1191 | if (size == 4) | |
76698b4e | 1192 | printf(" %08x", *((u32 *)ptr)); |
4680976f SG |
1193 | else if (SUPPORT_64BIT_DATA && size == 8) |
1194 | printf(" %0lx", *((ulong *)ptr)); | |
3863585b | 1195 | else if (size == 2) |
76698b4e | 1196 | printf(" %04x", *((u16 *)ptr)); |
3863585b | 1197 | else |
76698b4e | 1198 | printf(" %02x", *((u8 *)ptr)); |
3863585b | 1199 | |
e1bf824d | 1200 | nbytes = cli_readline(" ? "); |
3863585b WD |
1201 | if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) { |
1202 | /* <CR> pressed as only input, don't modify current | |
1203 | * location and move to next. "-" pressed will go back. | |
1204 | */ | |
1205 | if (incrflag) | |
1206 | addr += nbytes ? -size : size; | |
1207 | nbytes = 1; | |
b26440f1 SG |
1208 | /* good enough to not time out */ |
1209 | bootretry_reset_cmd_timeout(); | |
3863585b WD |
1210 | } |
1211 | #ifdef CONFIG_BOOT_RETRY_TIME | |
1212 | else if (nbytes == -2) { | |
1213 | break; /* timed out, exit the command */ | |
1214 | } | |
1215 | #endif | |
1216 | else { | |
1217 | char *endp; | |
4680976f SG |
1218 | if (SUPPORT_64BIT_DATA) |
1219 | i = simple_strtoull(console_buffer, &endp, 16); | |
1220 | else | |
7e5f460e | 1221 | i = hextoul(console_buffer, &endp); |
3863585b WD |
1222 | nbytes = endp - console_buffer; |
1223 | if (nbytes) { | |
3863585b WD |
1224 | /* good enough to not time out |
1225 | */ | |
b26440f1 | 1226 | bootretry_reset_cmd_timeout(); |
3863585b | 1227 | if (size == 4) |
76698b4e | 1228 | *((u32 *)ptr) = i; |
4680976f SG |
1229 | else if (SUPPORT_64BIT_DATA && size == 8) |
1230 | *((ulong *)ptr) = i; | |
3863585b | 1231 | else if (size == 2) |
76698b4e | 1232 | *((u16 *)ptr) = i; |
3863585b | 1233 | else |
76698b4e | 1234 | *((u8 *)ptr) = i; |
3863585b WD |
1235 | if (incrflag) |
1236 | addr += size; | |
1237 | } | |
1238 | } | |
1239 | } while (nbytes); | |
0628ab8e SG |
1240 | if (ptr) |
1241 | unmap_sysmem(ptr); | |
3863585b WD |
1242 | |
1243 | mm_last_addr = addr; | |
1244 | mm_last_size = size; | |
1245 | return 0; | |
1246 | } | |
1247 | ||
710b9938 MF |
1248 | #ifdef CONFIG_CMD_CRC32 |
1249 | ||
09140113 SG |
1250 | static int do_mem_crc(struct cmd_tbl *cmdtp, int flag, int argc, |
1251 | char *const argv[]) | |
3863585b | 1252 | { |
d20a40de | 1253 | int flags = 0; |
c26e454d | 1254 | int ac; |
54841ab5 | 1255 | char * const *av; |
c26e454d | 1256 | |
d20a40de | 1257 | if (argc < 3) |
4c12eeb8 | 1258 | return CMD_RET_USAGE; |
c26e454d WD |
1259 | |
1260 | av = argv + 1; | |
1261 | ac = argc - 1; | |
221a949e | 1262 | #ifdef CONFIG_CRC32_VERIFY |
c26e454d | 1263 | if (strcmp(*av, "-v") == 0) { |
a69bdba9 | 1264 | flags |= HASH_FLAG_VERIFY | HASH_FLAG_ENV; |
c26e454d WD |
1265 | av++; |
1266 | ac--; | |
c26e454d | 1267 | } |
d20a40de | 1268 | #endif |
c26e454d | 1269 | |
d20a40de | 1270 | return hash_command("crc32", flags, cmdtp, flag, ac, av); |
c26e454d | 1271 | } |
c26e454d | 1272 | |
710b9938 MF |
1273 | #endif |
1274 | ||
803e1a3d | 1275 | #ifdef CONFIG_CMD_RANDOM |
09140113 SG |
1276 | static int do_random(struct cmd_tbl *cmdtp, int flag, int argc, |
1277 | char *const argv[]) | |
803e1a3d JJH |
1278 | { |
1279 | unsigned long addr, len; | |
1280 | unsigned long seed; // NOT INITIALIZED ON PURPOSE | |
1281 | unsigned int *buf, *start; | |
1282 | unsigned char *buf8; | |
1283 | unsigned int i; | |
1284 | ||
5f2c4e01 EP |
1285 | if (argc < 3 || argc > 4) |
1286 | return CMD_RET_USAGE; | |
803e1a3d | 1287 | |
7e5f460e SG |
1288 | len = hextoul(argv[2], NULL); |
1289 | addr = hextoul(argv[1], NULL); | |
803e1a3d JJH |
1290 | |
1291 | if (argc == 4) { | |
7e5f460e | 1292 | seed = hextoul(argv[3], NULL); |
803e1a3d JJH |
1293 | if (seed == 0) { |
1294 | printf("The seed cannot be 0. Using 0xDEADBEEF.\n"); | |
1295 | seed = 0xDEADBEEF; | |
1296 | } | |
1297 | } else { | |
1298 | seed = get_timer(0) ^ rand(); | |
1299 | } | |
1300 | ||
1301 | srand(seed); | |
1302 | start = map_sysmem(addr, len); | |
1303 | buf = start; | |
1304 | for (i = 0; i < (len / 4); i++) | |
1305 | *buf++ = rand(); | |
1306 | ||
1307 | buf8 = (unsigned char *)buf; | |
1308 | for (i = 0; i < (len % 4); i++) | |
1309 | *buf8++ = rand() & 0xFF; | |
1310 | ||
1311 | unmap_sysmem(start); | |
1312 | printf("%lu bytes filled with random data\n", len); | |
5f2c4e01 EP |
1313 | |
1314 | return CMD_RET_SUCCESS; | |
803e1a3d JJH |
1315 | } |
1316 | #endif | |
1317 | ||
8bde7f77 | 1318 | /**************************************************/ |
0d498393 | 1319 | U_BOOT_CMD( |
53677ef1 | 1320 | md, 3, 1, do_mem_md, |
2fb2604d | 1321 | "memory display", |
76be8f75 | 1322 | "[.b, .w, .l" HELP_Q "] address [# of objects]" |
8bde7f77 WD |
1323 | ); |
1324 | ||
1325 | ||
0d498393 | 1326 | U_BOOT_CMD( |
53677ef1 | 1327 | mm, 2, 1, do_mem_mm, |
a89c33db | 1328 | "memory modify (auto-incrementing address)", |
76be8f75 | 1329 | "[.b, .w, .l" HELP_Q "] address" |
8bde7f77 WD |
1330 | ); |
1331 | ||
1332 | ||
0d498393 | 1333 | U_BOOT_CMD( |
53677ef1 | 1334 | nm, 2, 1, do_mem_nm, |
2fb2604d | 1335 | "memory modify (constant address)", |
76be8f75 | 1336 | "[.b, .w, .l" HELP_Q "] address" |
8bde7f77 WD |
1337 | ); |
1338 | ||
0d498393 | 1339 | U_BOOT_CMD( |
53677ef1 | 1340 | mw, 4, 1, do_mem_mw, |
2fb2604d | 1341 | "memory write (fill)", |
76be8f75 | 1342 | "[.b, .w, .l" HELP_Q "] address value [count]" |
8bde7f77 WD |
1343 | ); |
1344 | ||
0d498393 | 1345 | U_BOOT_CMD( |
53677ef1 | 1346 | cp, 4, 1, do_mem_cp, |
2fb2604d | 1347 | "memory copy", |
76be8f75 | 1348 | "[.b, .w, .l" HELP_Q "] source target count" |
8bde7f77 WD |
1349 | ); |
1350 | ||
0d498393 | 1351 | U_BOOT_CMD( |
53677ef1 | 1352 | cmp, 4, 1, do_mem_cmp, |
2fb2604d | 1353 | "memory compare", |
76be8f75 | 1354 | "[.b, .w, .l" HELP_Q "] addr1 addr2 count" |
8bde7f77 WD |
1355 | ); |
1356 | ||
550a9e79 | 1357 | #ifdef CONFIG_CMD_MEM_SEARCH |
bdded201 SG |
1358 | /**************************************************/ |
1359 | U_BOOT_CMD( | |
1360 | ms, 255, 1, do_mem_search, | |
1361 | "memory search", | |
1362 | "[.b, .w, .l" HELP_Q ", .s] [-q | -<n>] address #-of-objects <value>..." | |
550a9e79 | 1363 | " -q = quiet, -l<val> = match limit" |
bdded201 SG |
1364 | ); |
1365 | #endif | |
1366 | ||
710b9938 MF |
1367 | #ifdef CONFIG_CMD_CRC32 |
1368 | ||
221a949e | 1369 | #ifndef CONFIG_CRC32_VERIFY |
c26e454d | 1370 | |
0d498393 | 1371 | U_BOOT_CMD( |
53677ef1 | 1372 | crc32, 4, 1, do_mem_crc, |
2fb2604d | 1373 | "checksum calculation", |
a89c33db | 1374 | "address count [addr]\n - compute CRC32 checksum [save at addr]" |
8bde7f77 WD |
1375 | ); |
1376 | ||
221a949e | 1377 | #else /* CONFIG_CRC32_VERIFY */ |
c26e454d WD |
1378 | |
1379 | U_BOOT_CMD( | |
53677ef1 | 1380 | crc32, 5, 1, do_mem_crc, |
2fb2604d | 1381 | "checksum calculation", |
c26e454d | 1382 | "address count [addr]\n - compute CRC32 checksum [save at addr]\n" |
a89c33db | 1383 | "-v address count crc\n - verify crc of memory area" |
c26e454d WD |
1384 | ); |
1385 | ||
221a949e | 1386 | #endif /* CONFIG_CRC32_VERIFY */ |
c26e454d | 1387 | |
710b9938 MF |
1388 | #endif |
1389 | ||
15a33e49 | 1390 | #ifdef CONFIG_CMD_MEMINFO |
09140113 SG |
1391 | static int do_mem_info(struct cmd_tbl *cmdtp, int flag, int argc, |
1392 | char *const argv[]) | |
15a33e49 | 1393 | { |
428a6a18 SG |
1394 | puts("DRAM: "); |
1395 | print_size(gd->ram_size, "\n"); | |
15a33e49 SG |
1396 | |
1397 | return 0; | |
1398 | } | |
1399 | #endif | |
1400 | ||
0d498393 | 1401 | U_BOOT_CMD( |
53677ef1 | 1402 | base, 2, 1, do_mem_base, |
2fb2604d | 1403 | "print or set address offset", |
8bde7f77 | 1404 | "\n - print address offset for memory commands\n" |
a89c33db | 1405 | "base off\n - set address offset for memory commands to 'off'" |
8bde7f77 WD |
1406 | ); |
1407 | ||
0d498393 | 1408 | U_BOOT_CMD( |
53677ef1 | 1409 | loop, 3, 1, do_mem_loop, |
2fb2604d | 1410 | "infinite loop on address range", |
76be8f75 | 1411 | "[.b, .w, .l" HELP_Q "] address number_of_objects" |
8bde7f77 WD |
1412 | ); |
1413 | ||
56523f12 WD |
1414 | #ifdef CONFIG_LOOPW |
1415 | U_BOOT_CMD( | |
53677ef1 | 1416 | loopw, 4, 1, do_mem_loopw, |
2fb2604d | 1417 | "infinite write loop on address range", |
76be8f75 | 1418 | "[.b, .w, .l" HELP_Q "] address number_of_objects data_to_write" |
56523f12 WD |
1419 | ); |
1420 | #endif /* CONFIG_LOOPW */ | |
1421 | ||
a2681707 | 1422 | #ifdef CONFIG_CMD_MEMTEST |
0d498393 | 1423 | U_BOOT_CMD( |
b6fc6fd4 | 1424 | mtest, 5, 1, do_mem_mtest, |
a89c33db WD |
1425 | "simple RAM read/write test", |
1426 | "[start [end [pattern [iterations]]]]" | |
8bde7f77 | 1427 | ); |
a2681707 | 1428 | #endif /* CONFIG_CMD_MEMTEST */ |
8bde7f77 | 1429 | |
72732318 | 1430 | #ifdef CONFIG_CMD_MX_CYCLIC |
4aaf29b2 | 1431 | U_BOOT_CMD( |
53677ef1 | 1432 | mdc, 4, 1, do_mem_mdc, |
2fb2604d | 1433 | "memory display cyclic", |
76be8f75 | 1434 | "[.b, .w, .l" HELP_Q "] address count delay(ms)" |
4aaf29b2 SR |
1435 | ); |
1436 | ||
1437 | U_BOOT_CMD( | |
53677ef1 | 1438 | mwc, 4, 1, do_mem_mwc, |
2fb2604d | 1439 | "memory write cyclic", |
76be8f75 | 1440 | "[.b, .w, .l" HELP_Q "] address value delay(ms)" |
4aaf29b2 | 1441 | ); |
72732318 | 1442 | #endif /* CONFIG_CMD_MX_CYCLIC */ |
15a33e49 SG |
1443 | |
1444 | #ifdef CONFIG_CMD_MEMINFO | |
1445 | U_BOOT_CMD( | |
1446 | meminfo, 3, 1, do_mem_info, | |
1447 | "display memory information", | |
1448 | "" | |
1449 | ); | |
1450 | #endif | |
803e1a3d JJH |
1451 | |
1452 | #ifdef CONFIG_CMD_RANDOM | |
1453 | U_BOOT_CMD( | |
1454 | random, 4, 0, do_random, | |
1455 | "fill memory with random pattern", | |
1456 | "<addr> <len> [<seed>]\n" | |
1457 | " - Fill 'len' bytes of memory starting at 'addr' with random data\n" | |
1458 | ); | |
1459 | #endif |