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