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