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