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