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