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