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