Merge branch 'master' of http://www.denx.de/git/u-boot
[platform/kernel/u-boot.git] / common / cmd_nand.c
1 /*
2  * Driver for NAND support, Rick Bronson
3  * borrowed heavily from:
4  * (c) 1999 Machine Vision Holdings, Inc.
5  * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
6  *
7  * Added 16-bit nand support
8  * (C) 2004 Texas Instruments
9  */
10
11 #include <common.h>
12
13
14 #ifndef CFG_NAND_LEGACY
15 /*
16  *
17  * New NAND support
18  *
19  */
20 #include <common.h>
21
22 #if (CONFIG_COMMANDS & CFG_CMD_NAND)
23
24 #include <command.h>
25 #include <watchdog.h>
26 #include <malloc.h>
27 #include <asm/byteorder.h>
28
29 #ifdef CONFIG_SHOW_BOOT_PROGRESS
30 # include <status_led.h>
31 # define SHOW_BOOT_PROGRESS(arg)        show_boot_progress(arg)
32 #else
33 # define SHOW_BOOT_PROGRESS(arg)
34 #endif
35
36 #include <jffs2/jffs2.h>
37 #include <nand.h>
38
39 extern nand_info_t nand_info[];       /* info for NAND chips */
40
41 static int nand_dump_oob(nand_info_t *nand, ulong off)
42 {
43         return 0;
44 }
45
46 static int nand_dump(nand_info_t *nand, ulong off)
47 {
48         int i;
49         u_char *buf, *p;
50
51         buf = malloc(nand->oobblock + nand->oobsize);
52         if (!buf) {
53                 puts("No memory for page buffer\n");
54                 return 1;
55         }
56         off &= ~(nand->oobblock - 1);
57         i = nand_read_raw(nand, buf, off, nand->oobblock, nand->oobsize);
58         if (i < 0) {
59                 printf("Error (%d) reading page %08x\n", i, off);
60                 free(buf);
61                 return 1;
62         }
63         printf("Page %08x dump:\n", off);
64         i = nand->oobblock >> 4; p = buf;
65         while (i--) {
66                 printf( "\t%02x %02x %02x %02x %02x %02x %02x %02x"
67                         "  %02x %02x %02x %02x %02x %02x %02x %02x\n",
68                         p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
69                         p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
70                 p += 16;
71         }
72         puts("OOB:\n");
73         i = nand->oobsize >> 3;
74         while (i--) {
75                 printf( "\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
76                         p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
77                 p += 8;
78         }
79         free(buf);
80
81         return 0;
82 }
83
84 /* ------------------------------------------------------------------------- */
85
86 static void
87 arg_off_size(int argc, char *argv[], ulong *off, ulong *size, ulong totsize)
88 {
89         *off = 0;
90         *size = 0;
91
92 #if defined(CONFIG_JFFS2_NAND) && defined(CFG_JFFS_CUSTOM_PART)
93         if (argc >= 1 && strcmp(argv[0], "partition") == 0) {
94                 int part_num;
95                 struct part_info *part;
96                 const char *partstr;
97
98                 if (argc >= 2)
99                         partstr = argv[1];
100                 else
101                         partstr = getenv("partition");
102
103                 if (partstr)
104                         part_num = (int)simple_strtoul(partstr, NULL, 10);
105                 else
106                         part_num = 0;
107
108                 part = jffs2_part_info(part_num);
109                 if (part == NULL) {
110                         printf("\nInvalid partition %d\n", part_num);
111                         return;
112                 }
113                 *size = part->size;
114                 *off = (ulong)part->offset;
115         } else
116 #endif
117         {
118                 if (argc >= 1)
119                         *off = (ulong)simple_strtoul(argv[0], NULL, 16);
120                 else
121                         *off = 0;
122
123                 if (argc >= 2)
124                         *size = (ulong)simple_strtoul(argv[1], NULL, 16);
125                 else
126                         *size = totsize - *off;
127
128         }
129
130 }
131
132 int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
133 {
134         int i, dev, ret;
135         ulong addr, off, size;
136         char *cmd, *s;
137         nand_info_t *nand;
138         int quiet = 0;
139         const char *quiet_str = getenv("quiet");
140
141         /* at least two arguments please */
142         if (argc < 2)
143                 goto usage;
144
145         if (quiet_str)
146                 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
147
148         cmd = argv[1];
149
150         if (strcmp(cmd, "info") == 0) {
151
152                 putc('\n');
153                 for (i = 0; i < CFG_MAX_NAND_DEVICE; i++) {
154                         if (nand_info[i].name)
155                                 printf("Device %d: %s, sector size %lu KiB\n",
156                                         i, nand_info[i].name,
157                                         nand_info[i].erasesize >> 10);
158                 }
159                 return 0;
160         }
161
162         if (strcmp(cmd, "device") == 0) {
163
164                 if (argc < 3) {
165                         if ((nand_curr_device < 0) ||
166                             (nand_curr_device >= CFG_MAX_NAND_DEVICE))
167                                 puts("\nno devices available\n");
168                         else
169                                 printf("\nDevice %d: %s\n", nand_curr_device,
170                                         nand_info[nand_curr_device].name);
171                         return 0;
172                 }
173                 dev = (int)simple_strtoul(argv[2], NULL, 10);
174                 if (dev < 0 || dev >= CFG_MAX_NAND_DEVICE || !nand_info[dev].name) {
175                         puts("No such device\n");
176                         return 1;
177                 }
178                 printf("Device %d: %s", dev, nand_info[dev].name);
179                 puts("... is now current device\n");
180                 nand_curr_device = dev;
181                 return 0;
182         }
183
184         if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
185             strncmp(cmd, "dump", 4) != 0 &&
186             strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
187             strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
188             strcmp(cmd, "biterr") != 0 &&
189             strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
190                 goto usage;
191
192         /* the following commands operate on the current device */
193         if (nand_curr_device < 0 || nand_curr_device >= CFG_MAX_NAND_DEVICE ||
194             !nand_info[nand_curr_device].name) {
195                 puts("\nno devices available\n");
196                 return 1;
197         }
198         nand = &nand_info[nand_curr_device];
199
200         if (strcmp(cmd, "bad") == 0) {
201                 printf("\nDevice %d bad blocks:\n", nand_curr_device);
202                 for (off = 0; off < nand->size; off += nand->erasesize)
203                         if (nand_block_isbad(nand, off))
204                                 printf("  %08x\n", off);
205                 return 0;
206         }
207
208         if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
209                 nand_erase_options_t opts;
210                 int clean = argc >= 3 && !strcmp("clean", argv[2]);
211                 int rest_argc = argc - 2;
212                 char **rest_argv = argv + 2;
213                 int scrub = !strcmp(cmd, "scrub");
214
215                 if (clean) {
216                         rest_argc--;
217                         rest_argv++;
218                 }
219
220                 if (rest_argc == 0) {
221
222                         printf("\nNAND %s: device %d whole chip\n",
223                                cmd,
224                                nand_curr_device);
225
226                         off = size = 0;
227                 } else {
228                         arg_off_size(rest_argc, rest_argv, &off, &size,
229                                      nand->size);
230
231                         if (off == 0 && size == 0)
232                                 return 1;
233
234                         printf("\nNAND %s: device %d offset 0x%x, size 0x%x\n",
235                                cmd, nand_curr_device, off, size);
236                 }
237
238                 memset(&opts, 0, sizeof(opts));
239                 opts.offset = off;
240                 opts.length = size;
241                 opts.jffs2  = clean;
242                 opts.quiet  = quiet;
243
244                 if (scrub) {
245                         printf("Warning: "
246                                "scrub option will erase all factory set "
247                                "bad blocks!\n"
248                                "         "
249                                "There is no reliable way to recover them.\n"
250                                "         "
251                                "Use this command only for testing purposes "
252                                "if you\n"
253                                "         "
254                                "are sure of what you are doing!\n"
255                                "\nReally scrub this NAND flash? <y/N>\n"
256                                 );
257
258                         if (getc() == 'y' && getc() == '\r') {
259                                 opts.scrub = 1;
260                         } else {
261                                 printf("scrub aborted\n");
262                                 return -1;
263                         }
264                 }
265                 ret = nand_erase_opts(nand, &opts);
266                 printf("%s\n", ret ? "ERROR" : "OK");
267
268                 return ret == 0 ? 0 : 1;
269         }
270
271         if (strncmp(cmd, "dump", 4) == 0) {
272                 if (argc < 3)
273                         goto usage;
274
275                 s = strchr(cmd, '.');
276                 off = (int)simple_strtoul(argv[2], NULL, 16);
277
278                 if (s != NULL && strcmp(s, ".oob") == 0)
279                         ret = nand_dump_oob(nand, off);
280                 else
281                         ret = nand_dump(nand, off);
282
283                 return ret == 0 ? 1 : 0;
284
285         }
286
287         /* read write */
288         if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
289                 int read;
290
291                 if (argc < 4)
292                         goto usage;
293
294                 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
295
296                 arg_off_size(argc - 3, argv + 3, &off, &size, nand->size);
297                 if (off == 0 && size == 0)
298                         return 1;
299
300                 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
301                 printf("\nNAND %s: device %d offset %u, size %u ... ",
302                        read ? "read" : "write", nand_curr_device, off, size);
303
304                 s = strchr(cmd, '.');
305                 if (s != NULL &&
306                     (!strcmp(s, ".jffs2") || !strcmp(s, ".e") || !strcmp(s, ".i"))) {
307                         if (read) {
308                                 /* read */
309                                 nand_read_options_t opts;
310                                 memset(&opts, 0, sizeof(opts));
311                                 opts.buffer     = (u_char*) addr;
312                                 opts.length     = size;
313                                 opts.offset     = off;
314                                 opts.quiet      = quiet;
315                                 ret = nand_read_opts(nand, &opts);
316                         } else {
317                                 /* write */
318                                 nand_write_options_t opts;
319                                 memset(&opts, 0, sizeof(opts));
320                                 opts.buffer     = (u_char*) addr;
321                                 opts.length     = size;
322                                 opts.offset     = off;
323                                 /* opts.forcejffs2 = 1; */
324                                 opts.pad        = 1;
325                                 opts.blockalign = 1;
326                                 opts.quiet      = quiet;
327                                 ret = nand_write_opts(nand, &opts);
328                         }
329                         printf("%s\n", ret ? "ERROR" : "OK");
330                         return ret == 0 ? 0 : 1;
331                 }
332
333                 if (read)
334                         ret = nand_read(nand, off, &size, (u_char *)addr);
335                 else
336                         ret = nand_write(nand, off, &size, (u_char *)addr);
337
338                 printf(" %d bytes %s: %s\n", size,
339                        read ? "read" : "written", ret ? "ERROR" : "OK");
340
341                 return ret == 0 ? 0 : 1;
342         }
343
344         if (strcmp(cmd, "markbad") == 0) {
345                 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
346
347                 int ret = nand->block_markbad(nand, addr);
348                 if (ret == 0) {
349                         printf("block 0x%08lx successfully marked as bad\n",
350                                (ulong) addr);
351                         return 0;
352                 } else {
353                         printf("block 0x%08lx NOT marked as bad! ERROR %d\n",
354                                (ulong) addr, ret);
355                 }
356                 return 1;
357         }
358         if (strcmp(cmd, "biterr") == 0) {
359                 /* todo */
360                 return 1;
361         }
362
363         if (strcmp(cmd, "lock") == 0) {
364                 int tight  = 0;
365                 int status = 0;
366                 if (argc == 3) {
367                         if (!strcmp("tight", argv[2]))
368                                 tight = 1;
369                         if (!strcmp("status", argv[2]))
370                                 status = 1;
371                 }
372
373                 if (status) {
374                         ulong block_start = 0;
375                         ulong off;
376                         int last_status = -1;
377
378                         struct nand_chip *nand_chip = nand->priv;
379                         /* check the WP bit */
380                         nand_chip->cmdfunc (nand, NAND_CMD_STATUS, -1, -1);
381                         printf("device is %swrite protected\n",
382                                (nand_chip->read_byte(nand) & 0x80 ?
383                                 "NOT " : "" ) );
384
385                         for (off = 0; off < nand->size; off += nand->oobblock) {
386                                 int s = nand_get_lock_status(nand, off);
387
388                                 /* print message only if status has changed
389                                  * or at end of chip
390                                  */
391                                 if (off == nand->size - nand->oobblock
392                                     || (s != last_status && off != 0))  {
393
394                                         printf("%08x - %08x: %8d pages %s%s%s\n",
395                                                block_start,
396                                                off-1,
397                                                (off-block_start)/nand->oobblock,
398                                                ((last_status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
399                                                ((last_status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
400                                                ((last_status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
401                                 }
402
403                                 last_status = s;
404                        }
405                 } else {
406                         if (!nand_lock(nand, tight)) {
407                                 printf ("NAND flash successfully locked\n");
408                         } else {
409                                 printf ("Error locking NAND flash. \n");
410                                 return 1;
411                         }
412                 }
413                 return 0;
414         }
415
416         if (strcmp(cmd, "unlock") == 0) {
417                 if (argc == 2) {
418                         off = 0;
419                         size = nand->size;
420                 } else {
421                         arg_off_size(argc - 2, argv + 2, &off, &size,
422                                      nand->size);
423                 }
424
425                 if (!nand_unlock(nand, off, size)) {
426                         printf("NAND flash successfully unlocked\n");
427                 } else {
428                         printf("Error unlocking NAND flash. "
429                                "Write and erase will probably fail\n");
430                         return 1;
431                 }
432                 return 0;
433         }
434
435 usage:
436         printf("Usage:\n%s\n", cmdtp->usage);
437         return 1;
438 }
439
440 U_BOOT_CMD(nand, 5, 1, do_nand,
441         "nand    - NAND sub-system\n",
442         "info                  - show available NAND devices\n"
443         "nand device [dev]     - show or set current device\n"
444         "nand read[.jffs2]     - addr off size\n"
445         "nand write[.jffs2]    - addr off size - read/write `size' bytes starting\n"
446         "    at offset `off' to/from memory address `addr'\n"
447         "nand erase [clean] [off size] - erase `size' bytes from\n"
448         "    offset `off' (entire device if not specified)\n"
449         "nand bad - show bad blocks\n"
450         "nand dump[.oob] off - dump page\n"
451         "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
452         "nand markbad off - mark bad block at offset (UNSAFE)\n"
453         "nand biterr off - make a bit error at offset (UNSAFE)\n"
454         "nand lock [tight] [status] - bring nand to lock state or display locked pages\n"
455         "nand unlock [offset] [size] - unlock section\n");
456
457 int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
458 {
459         char *boot_device = NULL;
460         char *ep;
461         int dev;
462         int r;
463         ulong addr, cnt, offset = 0;
464         image_header_t *hdr;
465         nand_info_t *nand;
466
467         switch (argc) {
468         case 1:
469                 addr = CFG_LOAD_ADDR;
470                 boot_device = getenv("bootdevice");
471                 break;
472         case 2:
473                 addr = simple_strtoul(argv[1], NULL, 16);
474                 boot_device = getenv("bootdevice");
475                 break;
476         case 3:
477                 addr = simple_strtoul(argv[1], NULL, 16);
478                 boot_device = argv[2];
479                 break;
480         case 4:
481                 addr = simple_strtoul(argv[1], NULL, 16);
482                 boot_device = argv[2];
483                 offset = simple_strtoul(argv[3], NULL, 16);
484                 break;
485         default:
486                 printf("Usage:\n%s\n", cmdtp->usage);
487                 SHOW_BOOT_PROGRESS(-1);
488                 return 1;
489         }
490
491         if (!boot_device) {
492                 puts("\n** No boot device **\n");
493                 SHOW_BOOT_PROGRESS(-1);
494                 return 1;
495         }
496
497         dev = simple_strtoul(boot_device, &ep, 16);
498
499         if (dev < 0 || dev >= CFG_MAX_NAND_DEVICE || !nand_info[dev].name) {
500                 printf("\n** Device %d not available\n", dev);
501                 SHOW_BOOT_PROGRESS(-1);
502                 return 1;
503         }
504
505         nand = &nand_info[dev];
506         printf("\nLoading from device %d: %s (offset 0x%lx)\n",
507                dev, nand->name, offset);
508
509         cnt = nand->oobblock;
510         r = nand_read(nand, offset, &cnt, (u_char *) addr);
511         if (r) {
512                 printf("** Read error on %d\n", dev);
513                 SHOW_BOOT_PROGRESS(-1);
514                 return 1;
515         }
516
517         hdr = (image_header_t *) addr;
518
519         if (ntohl(hdr->ih_magic) != IH_MAGIC) {
520                 printf("\n** Bad Magic Number 0x%x **\n", hdr->ih_magic);
521                 SHOW_BOOT_PROGRESS(-1);
522                 return 1;
523         }
524
525         print_image_hdr(hdr);
526
527         cnt = (ntohl(hdr->ih_size) + sizeof (image_header_t));
528
529         r = nand_read(nand, offset, &cnt, (u_char *) addr);
530         if (r) {
531                 printf("** Read error on %d\n", dev);
532                 SHOW_BOOT_PROGRESS(-1);
533                 return 1;
534         }
535
536         /* Loading ok, update default load address */
537
538         load_addr = addr;
539
540         /* Check if we should attempt an auto-start */
541         if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
542                 char *local_args[2];
543                 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
544
545                 local_args[0] = argv[0];
546                 local_args[1] = NULL;
547
548                 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
549
550                 do_bootm(cmdtp, 0, 1, local_args);
551                 return 1;
552         }
553         return 0;
554 }
555
556 U_BOOT_CMD(nboot, 4, 1, do_nandboot,
557         "nboot   - boot from NAND device\n", "loadAddr dev\n");
558
559
560 #endif                          /* (CONFIG_COMMANDS & CFG_CMD_NAND) */
561
562 #else /* CFG_NAND_LEGACY */
563 /*
564  *
565  * Legacy NAND support - to be phased out
566  *
567  */
568 #include <command.h>
569 #include <malloc.h>
570 #include <asm/io.h>
571 #include <watchdog.h>
572
573 #ifdef CONFIG_SHOW_BOOT_PROGRESS
574 # include <status_led.h>
575 # define SHOW_BOOT_PROGRESS(arg)        show_boot_progress(arg)
576 #else
577 # define SHOW_BOOT_PROGRESS(arg)
578 #endif
579
580 #if (CONFIG_COMMANDS & CFG_CMD_NAND)
581 #include <linux/mtd/nand_legacy.h>
582 #if 0
583 #include <linux/mtd/nand_ids.h>
584 #include <jffs2/jffs2.h>
585 #endif
586
587 #ifdef CONFIG_OMAP1510
588 void archflashwp(void *archdata, int wp);
589 #endif
590
591 #define ROUND_DOWN(value,boundary)      ((value) & (~((boundary)-1)))
592
593 #undef  NAND_DEBUG
594 #undef  PSYCHO_DEBUG
595
596 /* ****************** WARNING *********************
597  * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
598  * erase (or at least attempt to erase) blocks that are marked
599  * bad. This can be very handy if you are _sure_ that the block
600  * is OK, say because you marked a good block bad to test bad
601  * block handling and you are done testing, or if you have
602  * accidentally marked blocks bad.
603  *
604  * Erasing factory marked bad blocks is a _bad_ idea. If the
605  * erase succeeds there is no reliable way to find them again,
606  * and attempting to program or erase bad blocks can affect
607  * the data in _other_ (good) blocks.
608  */
609 #define  ALLOW_ERASE_BAD_DEBUG 0
610
611 #define CONFIG_MTD_NAND_ECC  /* enable ECC */
612 #define CONFIG_MTD_NAND_ECC_JFFS2
613
614 /* bits for nand_legacy_rw() `cmd'; or together as needed */
615 #define NANDRW_READ     0x01
616 #define NANDRW_WRITE    0x00
617 #define NANDRW_JFFS2    0x02
618 #define NANDRW_JFFS2_SKIP       0x04
619
620 /*
621  * Imports from nand_legacy.c
622  */
623 extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
624 extern int curr_device;
625 extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs,
626                             size_t len, int clean);
627 extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start,
628                          size_t len, size_t *retlen, u_char *buf);
629 extern void nand_print(struct nand_chip *nand);
630 extern void nand_print_bad(struct nand_chip *nand);
631 extern int nand_read_oob(struct nand_chip *nand, size_t ofs,
632                                size_t len, size_t *retlen, u_char *buf);
633 extern int nand_write_oob(struct nand_chip *nand, size_t ofs,
634                                 size_t len, size_t *retlen, const u_char *buf);
635
636
637 int do_nand (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
638 {
639     int rcode = 0;
640
641     switch (argc) {
642     case 0:
643     case 1:
644         printf ("Usage:\n%s\n", cmdtp->usage);
645         return 1;
646     case 2:
647         if (strcmp(argv[1],"info") == 0) {
648                 int i;
649
650                 putc ('\n');
651
652                 for (i=0; i<CFG_MAX_NAND_DEVICE; ++i) {
653                         if(nand_dev_desc[i].ChipID == NAND_ChipID_UNKNOWN)
654                                 continue; /* list only known devices */
655                         printf ("Device %d: ", i);
656                         nand_print(&nand_dev_desc[i]);
657                 }
658                 return 0;
659
660         } else if (strcmp(argv[1],"device") == 0) {
661                 if ((curr_device < 0) || (curr_device >= CFG_MAX_NAND_DEVICE)) {
662                         puts ("\nno devices available\n");
663                         return 1;
664                 }
665                 printf ("\nDevice %d: ", curr_device);
666                 nand_print(&nand_dev_desc[curr_device]);
667                 return 0;
668
669         } else if (strcmp(argv[1],"bad") == 0) {
670                 if ((curr_device < 0) || (curr_device >= CFG_MAX_NAND_DEVICE)) {
671                         puts ("\nno devices available\n");
672                         return 1;
673                 }
674                 printf ("\nDevice %d bad blocks:\n", curr_device);
675                 nand_print_bad(&nand_dev_desc[curr_device]);
676                 return 0;
677
678         }
679         printf ("Usage:\n%s\n", cmdtp->usage);
680         return 1;
681     case 3:
682         if (strcmp(argv[1],"device") == 0) {
683                 int dev = (int)simple_strtoul(argv[2], NULL, 10);
684
685                 printf ("\nDevice %d: ", dev);
686                 if (dev >= CFG_MAX_NAND_DEVICE) {
687                         puts ("unknown device\n");
688                         return 1;
689                 }
690                 nand_print(&nand_dev_desc[dev]);
691                 /*nand_print (dev);*/
692
693                 if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
694                         return 1;
695                 }
696
697                 curr_device = dev;
698
699                 puts ("... is now current device\n");
700
701                 return 0;
702         }
703         else if (strcmp(argv[1],"erase") == 0 && strcmp(argv[2], "clean") == 0) {
704                 struct nand_chip* nand = &nand_dev_desc[curr_device];
705                 ulong off = 0;
706                 ulong size = nand->totlen;
707                 int ret;
708
709                 printf ("\nNAND erase: device %d offset %ld, size %ld ... ",
710                         curr_device, off, size);
711
712                 ret = nand_legacy_erase (nand, off, size, 1);
713
714                 printf("%s\n", ret ? "ERROR" : "OK");
715
716                 return ret;
717         }
718
719         printf ("Usage:\n%s\n", cmdtp->usage);
720         return 1;
721     default:
722         /* at least 4 args */
723
724         if (strncmp(argv[1], "read", 4) == 0 ||
725             strncmp(argv[1], "write", 5) == 0) {
726                 ulong addr = simple_strtoul(argv[2], NULL, 16);
727                 ulong off  = simple_strtoul(argv[3], NULL, 16);
728                 ulong size = simple_strtoul(argv[4], NULL, 16);
729                 int cmd    = (strncmp(argv[1], "read", 4) == 0) ?
730                                 NANDRW_READ : NANDRW_WRITE;
731                 int ret, total;
732                 char* cmdtail = strchr(argv[1], '.');
733
734                 if (cmdtail && !strncmp(cmdtail, ".oob", 2)) {
735                         /* read out-of-band data */
736                         if (cmd & NANDRW_READ) {
737                                 ret = nand_read_oob(nand_dev_desc + curr_device,
738                                                     off, size, (size_t *)&total,
739                                                     (u_char*)addr);
740                         }
741                         else {
742                                 ret = nand_write_oob(nand_dev_desc + curr_device,
743                                                      off, size, (size_t *)&total,
744                                                      (u_char*)addr);
745                         }
746                         return ret;
747                 }
748                 else if (cmdtail && !strncmp(cmdtail, ".jffs2", 2))
749                         cmd |= NANDRW_JFFS2;    /* skip bad blocks */
750                 else if (cmdtail && !strncmp(cmdtail, ".jffs2s", 2)) {
751                         cmd |= NANDRW_JFFS2;    /* skip bad blocks (on read too) */
752                         if (cmd & NANDRW_READ)
753                                 cmd |= NANDRW_JFFS2_SKIP;       /* skip bad blocks (on read too) */
754                 }
755 #ifdef SXNI855T
756                 /* need ".e" same as ".j" for compatibility with older units */
757                 else if (cmdtail && !strcmp(cmdtail, ".e"))
758                         cmd |= NANDRW_JFFS2;    /* skip bad blocks */
759 #endif
760 #ifdef CFG_NAND_SKIP_BAD_DOT_I
761                 /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
762                 /* ".i" for image -> read skips bad block (no 0xff) */
763                 else if (cmdtail && !strcmp(cmdtail, ".i")) {
764                         cmd |= NANDRW_JFFS2;    /* skip bad blocks (on read too) */
765                         if (cmd & NANDRW_READ)
766                                 cmd |= NANDRW_JFFS2_SKIP;       /* skip bad blocks (on read too) */
767                 }
768 #endif /* CFG_NAND_SKIP_BAD_DOT_I */
769                 else if (cmdtail) {
770                         printf ("Usage:\n%s\n", cmdtp->usage);
771                         return 1;
772                 }
773
774                 printf ("\nNAND %s: device %d offset %ld, size %ld ...\n",
775                         (cmd & NANDRW_READ) ? "read" : "write",
776                         curr_device, off, size);
777
778                 ret = nand_legacy_rw(nand_dev_desc + curr_device, cmd, off, size,
779                              (size_t *)&total, (u_char*)addr);
780
781                 printf (" %d bytes %s: %s\n", total,
782                         (cmd & NANDRW_READ) ? "read" : "written",
783                         ret ? "ERROR" : "OK");
784
785                 return ret;
786         } else if (strcmp(argv[1],"erase") == 0 &&
787                    (argc == 4 || strcmp("clean", argv[2]) == 0)) {
788                 int clean = argc == 5;
789                 ulong off = simple_strtoul(argv[2 + clean], NULL, 16);
790                 ulong size = simple_strtoul(argv[3 + clean], NULL, 16);
791                 int ret;
792
793                 printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
794                         curr_device, off, size);
795
796                 ret = nand_legacy_erase (nand_dev_desc + curr_device,
797                                         off, size, clean);
798
799                 printf("%s\n", ret ? "ERROR" : "OK");
800
801                 return ret;
802         } else {
803                 printf ("Usage:\n%s\n", cmdtp->usage);
804                 rcode = 1;
805         }
806
807         return rcode;
808     }
809 }
810
811 U_BOOT_CMD(
812         nand,   5,      1,      do_nand,
813         "nand    - legacy NAND sub-system\n",
814         "info  - show available NAND devices\n"
815         "nand device [dev] - show or set current device\n"
816         "nand read[.jffs2[s]]  addr off size\n"
817         "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
818         "    at offset `off' to/from memory address `addr'\n"
819         "nand erase [clean] [off size] - erase `size' bytes from\n"
820         "    offset `off' (entire device if not specified)\n"
821         "nand bad - show bad blocks\n"
822         "nand read.oob addr off size - read out-of-band data\n"
823         "nand write.oob addr off size - read out-of-band data\n"
824 );
825
826 int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
827 {
828         char *boot_device = NULL;
829         char *ep;
830         int dev;
831         ulong cnt;
832         ulong addr;
833         ulong offset = 0;
834         image_header_t *hdr;
835         int rcode = 0;
836         switch (argc) {
837         case 1:
838                 addr = CFG_LOAD_ADDR;
839                 boot_device = getenv ("bootdevice");
840                 break;
841         case 2:
842                 addr = simple_strtoul(argv[1], NULL, 16);
843                 boot_device = getenv ("bootdevice");
844                 break;
845         case 3:
846                 addr = simple_strtoul(argv[1], NULL, 16);
847                 boot_device = argv[2];
848                 break;
849         case 4:
850                 addr = simple_strtoul(argv[1], NULL, 16);
851                 boot_device = argv[2];
852                 offset = simple_strtoul(argv[3], NULL, 16);
853                 break;
854         default:
855                 printf ("Usage:\n%s\n", cmdtp->usage);
856                 SHOW_BOOT_PROGRESS (-1);
857                 return 1;
858         }
859
860         if (!boot_device) {
861                 puts ("\n** No boot device **\n");
862                 SHOW_BOOT_PROGRESS (-1);
863                 return 1;
864         }
865
866         dev = simple_strtoul(boot_device, &ep, 16);
867
868         if ((dev >= CFG_MAX_NAND_DEVICE) ||
869             (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
870                 printf ("\n** Device %d not available\n", dev);
871                 SHOW_BOOT_PROGRESS (-1);
872                 return 1;
873         }
874
875         printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
876                 dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
877                 offset);
878
879         if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
880                         SECTORSIZE, NULL, (u_char *)addr)) {
881                 printf ("** Read error on %d\n", dev);
882                 SHOW_BOOT_PROGRESS (-1);
883                 return 1;
884         }
885
886         hdr = (image_header_t *)addr;
887
888         if (ntohl(hdr->ih_magic) == IH_MAGIC) {
889
890                 print_image_hdr (hdr);
891
892                 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
893                 cnt -= SECTORSIZE;
894         } else {
895                 printf ("\n** Bad Magic Number 0x%x **\n", ntohl(hdr->ih_magic));
896                 SHOW_BOOT_PROGRESS (-1);
897                 return 1;
898         }
899
900         if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
901                         offset + SECTORSIZE, cnt, NULL,
902                         (u_char *)(addr+SECTORSIZE))) {
903                 printf ("** Read error on %d\n", dev);
904                 SHOW_BOOT_PROGRESS (-1);
905                 return 1;
906         }
907
908         /* Loading ok, update default load address */
909
910         load_addr = addr;
911
912         /* Check if we should attempt an auto-start */
913         if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
914                 char *local_args[2];
915                 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
916
917                 local_args[0] = argv[0];
918                 local_args[1] = NULL;
919
920                 printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
921
922                 do_bootm (cmdtp, 0, 1, local_args);
923                 rcode = 1;
924         }
925         return rcode;
926 }
927
928 U_BOOT_CMD(
929         nboot,  4,      1,      do_nandboot,
930         "nboot   - boot from NAND device\n",
931         "loadAddr dev\n"
932 );
933
934 #endif /* (CONFIG_COMMANDS & CFG_CMD_NAND) */
935
936 #endif /* CFG_NAND_LEGACY */