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