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