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