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>
7 * Added 16-bit nand support
8 * (C) 2004 Texas Instruments
14 #ifndef CONFIG_NAND_LEGACY
21 #include <linux/mtd/mtd.h>
23 #if defined(CONFIG_CMD_NAND)
28 #include <asm/byteorder.h>
29 #include <jffs2/jffs2.h>
32 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
34 /* parition handling routines */
35 int mtdparts_init(void);
36 int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
37 int find_dev_and_part(const char *id, struct mtd_device **dev,
38 u8 *part_num, struct part_info **part);
41 static int nand_dump(nand_info_t *nand, ulong off, int only_oob)
44 u_char *datbuf, *oobbuf, *p;
46 datbuf = malloc(nand->writesize + nand->oobsize);
47 oobbuf = malloc(nand->oobsize);
48 if (!datbuf || !oobbuf) {
49 puts("No memory for page buffer\n");
52 off &= ~(nand->writesize - 1);
53 loff_t addr = (loff_t) off;
54 struct mtd_oob_ops ops;
55 memset(&ops, 0, sizeof(ops));
57 ops.oobbuf = oobbuf; /* must exist, but oob data will be appended to ops.datbuf */
58 ops.len = nand->writesize;
59 ops.ooblen = nand->oobsize;
60 ops.mode = MTD_OOB_RAW;
61 i = nand->read_oob(nand, addr, &ops);
63 printf("Error (%d) reading page %08lx\n", i, off);
68 printf("Page %08lx dump:\n", off);
69 i = nand->writesize >> 4;
74 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
75 " %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], p[9], p[10], p[11], p[12], p[13], p[14],
82 i = nand->oobsize >> 3;
84 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
85 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
94 /* ------------------------------------------------------------------------- */
96 static inline int str2long(char *p, ulong *num)
100 *num = simple_strtoul(p, &endptr, 16);
101 return (*p != '\0' && *endptr == '\0') ? 1 : 0;
105 arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size)
107 int idx = nand_curr_device;
108 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
109 struct mtd_device *dev;
110 struct part_info *part;
113 if (argc >= 1 && !(str2long(argv[0], off))) {
114 if ((mtdparts_init() == 0) &&
115 (find_dev_and_part(argv[0], &dev, &pnum, &part) == 0)) {
116 if (dev->id->type != MTD_DEV_TYPE_NAND) {
117 puts("not a NAND device\n");
122 if (!(str2long(argv[1], (ulong *)size))) {
123 printf("'%s' is not a number\n", argv[1]);
126 if (*size > part->size)
132 *nand = nand_info[idx];
139 if (!(str2long(argv[0], off))) {
140 printf("'%s' is not a number\n", argv[0]);
148 if (!(str2long(argv[1], (ulong *)size))) {
149 printf("'%s' is not a number\n", argv[1]);
153 *size = nand->size - *off;
156 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
159 printf("device %d ", idx);
160 if (*size == nand->size)
161 puts("whole chip\n");
163 printf("offset 0x%lx, size 0x%x\n", *off, *size);
167 int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
174 #ifdef CFG_NAND_QUIET
175 int quiet = CFG_NAND_QUIET;
179 const char *quiet_str = getenv("quiet");
181 /* at least two arguments please */
186 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
190 if (strcmp(cmd, "info") == 0) {
193 for (i = 0; i < CFG_MAX_NAND_DEVICE; i++) {
194 if (nand_info[i].name)
195 printf("Device %d: %s, sector size %u KiB\n",
196 i, nand_info[i].name,
197 nand_info[i].erasesize >> 10);
202 if (strcmp(cmd, "device") == 0) {
205 if ((nand_curr_device < 0) ||
206 (nand_curr_device >= CFG_MAX_NAND_DEVICE))
207 puts("\nno devices available\n");
209 printf("\nDevice %d: %s\n", nand_curr_device,
210 nand_info[nand_curr_device].name);
213 dev = (int)simple_strtoul(argv[2], NULL, 10);
214 if (dev < 0 || dev >= CFG_MAX_NAND_DEVICE || !nand_info[dev].name) {
215 puts("No such device\n");
218 printf("Device %d: %s", dev, nand_info[dev].name);
219 puts("... is now current device\n");
220 nand_curr_device = dev;
222 #ifdef CFG_NAND_SELECT_DEVICE
224 * Select the chip in the board/cpu specific driver
226 board_nand_select_device(nand_info[dev].priv, dev);
232 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
233 strncmp(cmd, "dump", 4) != 0 &&
234 strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
235 strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
236 strcmp(cmd, "biterr") != 0 &&
237 strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
240 /* the following commands operate on the current device */
241 if (nand_curr_device < 0 || nand_curr_device >= CFG_MAX_NAND_DEVICE ||
242 !nand_info[nand_curr_device].name) {
243 puts("\nno devices available\n");
246 nand = &nand_info[nand_curr_device];
248 if (strcmp(cmd, "bad") == 0) {
249 printf("\nDevice %d bad blocks:\n", nand_curr_device);
250 for (off = 0; off < nand->size; off += nand->erasesize)
251 if (nand_block_isbad(nand, off))
252 printf(" %08lx\n", off);
259 * nand erase [clean] [off size]
261 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
262 nand_erase_options_t opts;
263 /* "clean" at index 2 means request to write cleanmarker */
264 int clean = argc > 2 && !strcmp("clean", argv[2]);
265 int o = clean ? 3 : 2;
266 int scrub = !strcmp(cmd, "scrub");
268 printf("\nNAND %s: ", scrub ? "scrub" : "erase");
269 /* skip first two or three arguments, look for offset and size */
270 if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
273 memset(&opts, 0, sizeof(opts));
281 "scrub option will erase all factory set "
284 "There is no reliable way to recover them.\n"
286 "Use this command only for testing purposes "
289 "are sure of what you are doing!\n"
290 "\nReally scrub this NAND flash? <y/N>\n");
292 if (getc() == 'y' && getc() == '\r') {
295 puts("scrub aborted\n");
299 ret = nand_erase_opts(nand, &opts);
300 printf("%s\n", ret ? "ERROR" : "OK");
302 return ret == 0 ? 0 : 1;
305 if (strncmp(cmd, "dump", 4) == 0) {
309 s = strchr(cmd, '.');
310 off = (int)simple_strtoul(argv[2], NULL, 16);
312 if (s != NULL && strcmp(s, ".oob") == 0)
313 ret = nand_dump(nand, off, 1);
315 ret = nand_dump(nand, off, 0);
317 return ret == 0 ? 1 : 0;
321 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
327 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
329 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
330 printf("\nNAND %s: ", read ? "read" : "write");
331 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
334 s = strchr(cmd, '.');
335 if (!s || !strcmp(s, ".jffs2") ||
336 !strcmp(s, ".e") || !strcmp(s, ".i")) {
338 ret = nand_read_skip_bad(nand, off, &size,
341 ret = nand_write_skip_bad(nand, off, &size,
343 } else if (s != NULL && !strcmp(s, ".oob")) {
344 /* out-of-band data */
345 mtd_oob_ops_t ops = {
346 .oobbuf = (u8 *)addr,
352 ret = nand->read_oob(nand, off, &ops);
354 ret = nand->write_oob(nand, off, &ops);
356 printf("Unknown nand command suffix '%s'.\n", s);
360 printf(" %d bytes %s: %s\n", size,
361 read ? "read" : "written", ret ? "ERROR" : "OK");
363 return ret == 0 ? 0 : 1;
366 if (strcmp(cmd, "markbad") == 0) {
367 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
369 int ret = nand->block_markbad(nand, addr);
371 printf("block 0x%08lx successfully marked as bad\n",
375 printf("block 0x%08lx NOT marked as bad! ERROR %d\n",
381 if (strcmp(cmd, "biterr") == 0) {
386 if (strcmp(cmd, "lock") == 0) {
390 if (!strcmp("tight", argv[2]))
392 if (!strcmp("status", argv[2]))
398 * TODO: must be implemented and tested by someone with HW
402 ulong block_start = 0;
404 int last_status = -1;
406 struct nand_chip *nand_chip = nand->priv;
407 /* check the WP bit */
408 nand_chip->cmdfunc (nand, NAND_CMD_STATUS, -1, -1);
409 printf("device is %swrite protected\n",
410 (nand_chip->read_byte(nand) & 0x80 ?
413 for (off = 0; off < nand->size; off += nand->writesize) {
414 int s = nand_get_lock_status(nand, off);
416 /* print message only if status has changed
419 if (off == nand->size - nand->writesize
420 || (s != last_status && off != 0)) {
422 printf("%08lx - %08lx: %8d pages %s%s%s\n",
425 (off-block_start)/nand->writesize,
426 ((last_status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
427 ((last_status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
428 ((last_status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
434 if (!nand_lock(nand, tight)) {
435 puts("NAND flash successfully locked\n");
437 puts("Error locking NAND flash\n");
445 if (strcmp(cmd, "unlock") == 0) {
446 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
452 * TODO: must be implemented and tested by someone with HW
455 if (!nand_unlock(nand, off, size)) {
456 puts("NAND flash successfully unlocked\n");
458 puts("Error unlocking NAND flash, "
459 "write and erase will probably fail\n");
467 printf("Usage:\n%s\n", cmdtp->usage);
471 U_BOOT_CMD(nand, 5, 1, do_nand,
472 "nand - NAND sub-system\n",
473 "info - show available NAND devices\n"
474 "nand device [dev] - show or set current device\n"
475 "nand read - addr off|partition size\n"
476 "nand write - addr off|partition size\n"
477 " read/write 'size' bytes starting at offset 'off'\n"
478 " to/from memory address 'addr', skipping bad blocks.\n"
479 "nand erase [clean] [off size] - erase 'size' bytes from\n"
480 " offset 'off' (entire device if not specified)\n"
481 "nand bad - show bad blocks\n"
482 "nand dump[.oob] off - dump page\n"
483 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
484 "nand markbad off - mark bad block at offset (UNSAFE)\n"
485 "nand biterr off - make a bit error at offset (UNSAFE)\n"
486 "nand lock [tight] [status]\n"
487 " bring nand to lock state or display locked pages\n"
488 "nand unlock [offset] [size] - unlock section\n");
490 static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
491 ulong offset, ulong addr, char *cmd)
497 #if defined(CONFIG_FIT)
498 const void *fit_hdr = NULL;
501 s = strchr(cmd, '.');
503 (strcmp(s, ".jffs2") && !strcmp(s, ".e") && !strcmp(s, ".i"))) {
504 printf("Unknown nand load suffix '%s'\n", s);
505 show_boot_progress(-53);
509 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
511 cnt = nand->writesize;
512 r = nand_read(nand, offset, &cnt, (u_char *) addr);
514 puts("** Read error\n");
515 show_boot_progress (-56);
518 show_boot_progress (56);
520 switch (genimg_get_format ((void *)addr)) {
521 case IMAGE_FORMAT_LEGACY:
522 hdr = (image_header_t *)addr;
524 show_boot_progress (57);
525 image_print_contents (hdr);
527 cnt = image_get_image_size (hdr);
529 #if defined(CONFIG_FIT)
530 case IMAGE_FORMAT_FIT:
531 fit_hdr = (const void *)addr;
532 puts ("Fit image detected...\n");
534 cnt = fit_get_size (fit_hdr);
538 show_boot_progress (-57);
539 puts ("** Unknown image type\n");
542 show_boot_progress (57);
544 /* FIXME: skip bad blocks */
545 r = nand_read(nand, offset, &cnt, (u_char *) addr);
547 puts("** Read error\n");
548 show_boot_progress (-58);
551 show_boot_progress (58);
553 #if defined(CONFIG_FIT)
554 /* This cannot be done earlier, we need complete FIT image in RAM first */
555 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
556 if (!fit_check_format (fit_hdr)) {
557 show_boot_progress (-150);
558 puts ("** Bad FIT image format\n");
561 show_boot_progress (151);
562 fit_print_contents (fit_hdr);
566 /* Loading ok, update default load address */
570 /* Check if we should attempt an auto-start */
571 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
573 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
576 local_args[1] = NULL;
578 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
580 do_bootm(cmdtp, 0, 1, local_args);
586 int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
588 char *boot_device = NULL;
590 ulong addr, offset = 0;
591 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
592 struct mtd_device *dev;
593 struct part_info *part;
597 char *p = (argc == 2) ? argv[1] : argv[2];
598 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
599 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
600 if (dev->id->type != MTD_DEV_TYPE_NAND) {
601 puts("Not a NAND device\n");
607 addr = simple_strtoul(argv[1], NULL, 16);
609 addr = CFG_LOAD_ADDR;
610 return nand_load_image(cmdtp, &nand_info[dev->id->num],
611 part->offset, addr, argv[0]);
616 show_boot_progress(52);
619 addr = CFG_LOAD_ADDR;
620 boot_device = getenv("bootdevice");
623 addr = simple_strtoul(argv[1], NULL, 16);
624 boot_device = getenv("bootdevice");
627 addr = simple_strtoul(argv[1], NULL, 16);
628 boot_device = argv[2];
631 addr = simple_strtoul(argv[1], NULL, 16);
632 boot_device = argv[2];
633 offset = simple_strtoul(argv[3], NULL, 16);
636 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
639 printf("Usage:\n%s\n", cmdtp->usage);
640 show_boot_progress(-53);
644 show_boot_progress(53);
646 puts("\n** No boot device **\n");
647 show_boot_progress(-54);
650 show_boot_progress(54);
652 idx = simple_strtoul(boot_device, NULL, 16);
654 if (idx < 0 || idx >= CFG_MAX_NAND_DEVICE || !nand_info[idx].name) {
655 printf("\n** Device %d not available\n", idx);
656 show_boot_progress(-55);
659 show_boot_progress(55);
661 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
664 U_BOOT_CMD(nboot, 4, 1, do_nandboot,
665 "nboot - boot from NAND device\n",
666 "[partition] | [[[loadAddr] dev] offset]\n");
670 #else /* CONFIG_NAND_LEGACY */
673 * Legacy NAND support - to be phased out
679 #include <watchdog.h>
681 #ifdef CONFIG_show_boot_progress
682 # include <status_led.h>
683 # define show_boot_progress(arg) show_boot_progress(arg)
685 # define show_boot_progress(arg)
688 #if defined(CONFIG_CMD_NAND)
689 #include <linux/mtd/nand_legacy.h>
691 #include <linux/mtd/nand_ids.h>
692 #include <jffs2/jffs2.h>
695 #ifdef CONFIG_OMAP1510
696 void archflashwp(void *archdata, int wp);
699 #define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
704 /* ****************** WARNING *********************
705 * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
706 * erase (or at least attempt to erase) blocks that are marked
707 * bad. This can be very handy if you are _sure_ that the block
708 * is OK, say because you marked a good block bad to test bad
709 * block handling and you are done testing, or if you have
710 * accidentally marked blocks bad.
712 * Erasing factory marked bad blocks is a _bad_ idea. If the
713 * erase succeeds there is no reliable way to find them again,
714 * and attempting to program or erase bad blocks can affect
715 * the data in _other_ (good) blocks.
717 #define ALLOW_ERASE_BAD_DEBUG 0
719 #define CONFIG_MTD_NAND_ECC /* enable ECC */
720 #define CONFIG_MTD_NAND_ECC_JFFS2
722 /* bits for nand_legacy_rw() `cmd'; or together as needed */
723 #define NANDRW_READ 0x01
724 #define NANDRW_WRITE 0x00
725 #define NANDRW_JFFS2 0x02
726 #define NANDRW_JFFS2_SKIP 0x04
729 * Imports from nand_legacy.c
731 extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
732 extern int curr_device;
733 extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs,
734 size_t len, int clean);
735 extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start,
736 size_t len, size_t *retlen, u_char *buf);
737 extern void nand_print(struct nand_chip *nand);
738 extern void nand_print_bad(struct nand_chip *nand);
739 extern int nand_read_oob(struct nand_chip *nand, size_t ofs,
740 size_t len, size_t *retlen, u_char *buf);
741 extern int nand_write_oob(struct nand_chip *nand, size_t ofs,
742 size_t len, size_t *retlen, const u_char *buf);
745 int do_nand (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
752 printf ("Usage:\n%s\n", cmdtp->usage);
755 if (strcmp (argv[1], "info") == 0) {
760 for (i = 0; i < CFG_MAX_NAND_DEVICE; ++i) {
761 if (nand_dev_desc[i].ChipID ==
763 continue; /* list only known devices */
764 printf ("Device %d: ", i);
765 nand_print (&nand_dev_desc[i]);
769 } else if (strcmp (argv[1], "device") == 0) {
770 if ((curr_device < 0)
771 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
772 puts ("\nno devices available\n");
775 printf ("\nDevice %d: ", curr_device);
776 nand_print (&nand_dev_desc[curr_device]);
779 } else if (strcmp (argv[1], "bad") == 0) {
780 if ((curr_device < 0)
781 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
782 puts ("\nno devices available\n");
785 printf ("\nDevice %d bad blocks:\n", curr_device);
786 nand_print_bad (&nand_dev_desc[curr_device]);
790 printf ("Usage:\n%s\n", cmdtp->usage);
793 if (strcmp (argv[1], "device") == 0) {
794 int dev = (int) simple_strtoul (argv[2], NULL, 10);
796 printf ("\nDevice %d: ", dev);
797 if (dev >= CFG_MAX_NAND_DEVICE) {
798 puts ("unknown device\n");
801 nand_print (&nand_dev_desc[dev]);
802 /*nand_print (dev); */
804 if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
810 puts ("... is now current device\n");
813 } else if (strcmp (argv[1], "erase") == 0
814 && strcmp (argv[2], "clean") == 0) {
815 struct nand_chip *nand = &nand_dev_desc[curr_device];
817 ulong size = nand->totlen;
820 printf ("\nNAND erase: device %d offset %ld, size %ld ... ", curr_device, off, size);
822 ret = nand_legacy_erase (nand, off, size, 1);
824 printf ("%s\n", ret ? "ERROR" : "OK");
829 printf ("Usage:\n%s\n", cmdtp->usage);
832 /* at least 4 args */
834 if (strncmp (argv[1], "read", 4) == 0 ||
835 strncmp (argv[1], "write", 5) == 0) {
836 ulong addr = simple_strtoul (argv[2], NULL, 16);
837 off_t off = simple_strtoul (argv[3], NULL, 16);
838 size_t size = simple_strtoul (argv[4], NULL, 16);
839 int cmd = (strncmp (argv[1], "read", 4) == 0) ?
840 NANDRW_READ : NANDRW_WRITE;
843 char *cmdtail = strchr (argv[1], '.');
845 if (cmdtail && !strncmp (cmdtail, ".oob", 2)) {
846 /* read out-of-band data */
847 if (cmd & NANDRW_READ) {
848 ret = nand_read_oob (nand_dev_desc + curr_device,
852 ret = nand_write_oob (nand_dev_desc + curr_device,
857 } else if (cmdtail && !strncmp (cmdtail, ".jffs2", 2))
858 cmd |= NANDRW_JFFS2; /* skip bad blocks */
859 else if (cmdtail && !strncmp (cmdtail, ".jffs2s", 2)) {
860 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
861 if (cmd & NANDRW_READ)
862 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
865 /* need ".e" same as ".j" for compatibility with older units */
866 else if (cmdtail && !strcmp (cmdtail, ".e"))
867 cmd |= NANDRW_JFFS2; /* skip bad blocks */
869 #ifdef CFG_NAND_SKIP_BAD_DOT_I
870 /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
871 /* ".i" for image -> read skips bad block (no 0xff) */
872 else if (cmdtail && !strcmp (cmdtail, ".i")) {
873 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
874 if (cmd & NANDRW_READ)
875 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
877 #endif /* CFG_NAND_SKIP_BAD_DOT_I */
879 printf ("Usage:\n%s\n", cmdtp->usage);
883 printf ("\nNAND %s: device %d offset %ld, size %lu ...\n",
884 (cmd & NANDRW_READ) ? "read" : "write",
885 curr_device, off, (ulong)size);
887 ret = nand_legacy_rw (nand_dev_desc + curr_device,
889 &total, (u_char *) addr);
891 printf (" %d bytes %s: %s\n", total,
892 (cmd & NANDRW_READ) ? "read" : "written",
893 ret ? "ERROR" : "OK");
896 } else if (strcmp (argv[1], "erase") == 0 &&
897 (argc == 4 || strcmp ("clean", argv[2]) == 0)) {
898 int clean = argc == 5;
900 simple_strtoul (argv[2 + clean], NULL, 16);
902 simple_strtoul (argv[3 + clean], NULL, 16);
905 printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
906 curr_device, off, size);
908 ret = nand_legacy_erase (nand_dev_desc + curr_device,
911 printf ("%s\n", ret ? "ERROR" : "OK");
915 printf ("Usage:\n%s\n", cmdtp->usage);
925 "nand - legacy NAND sub-system\n",
926 "info - show available NAND devices\n"
927 "nand device [dev] - show or set current device\n"
928 "nand read[.jffs2[s]] addr off size\n"
929 "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
930 " at offset `off' to/from memory address `addr'\n"
931 "nand erase [clean] [off size] - erase `size' bytes from\n"
932 " offset `off' (entire device if not specified)\n"
933 "nand bad - show bad blocks\n"
934 "nand read.oob addr off size - read out-of-band data\n"
935 "nand write.oob addr off size - read out-of-band data\n"
938 int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
940 char *boot_device = NULL;
948 #if defined(CONFIG_FIT)
949 const void *fit_hdr = NULL;
952 show_boot_progress (52);
955 addr = CFG_LOAD_ADDR;
956 boot_device = getenv ("bootdevice");
959 addr = simple_strtoul(argv[1], NULL, 16);
960 boot_device = getenv ("bootdevice");
963 addr = simple_strtoul(argv[1], NULL, 16);
964 boot_device = argv[2];
967 addr = simple_strtoul(argv[1], NULL, 16);
968 boot_device = argv[2];
969 offset = simple_strtoul(argv[3], NULL, 16);
972 printf ("Usage:\n%s\n", cmdtp->usage);
973 show_boot_progress (-53);
977 show_boot_progress (53);
979 puts ("\n** No boot device **\n");
980 show_boot_progress (-54);
983 show_boot_progress (54);
985 dev = simple_strtoul(boot_device, &ep, 16);
987 if ((dev >= CFG_MAX_NAND_DEVICE) ||
988 (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
989 printf ("\n** Device %d not available\n", dev);
990 show_boot_progress (-55);
993 show_boot_progress (55);
995 printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
996 dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
999 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
1000 SECTORSIZE, NULL, (u_char *)addr)) {
1001 printf ("** Read error on %d\n", dev);
1002 show_boot_progress (-56);
1005 show_boot_progress (56);
1007 switch (genimg_get_format ((void *)addr)) {
1008 case IMAGE_FORMAT_LEGACY:
1009 hdr = (image_header_t *)addr;
1010 image_print_contents (hdr);
1012 cnt = image_get_image_size (hdr);
1015 #if defined(CONFIG_FIT)
1016 case IMAGE_FORMAT_FIT:
1017 fit_hdr = (const void *)addr;
1018 puts ("Fit image detected...\n");
1020 cnt = fit_get_size (fit_hdr);
1024 show_boot_progress (-57);
1025 puts ("** Unknown image type\n");
1028 show_boot_progress (57);
1030 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
1031 offset + SECTORSIZE, cnt, NULL,
1032 (u_char *)(addr+SECTORSIZE))) {
1033 printf ("** Read error on %d\n", dev);
1034 show_boot_progress (-58);
1037 show_boot_progress (58);
1039 #if defined(CONFIG_FIT)
1040 /* This cannot be done earlier, we need complete FIT image in RAM first */
1041 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
1042 if (!fit_check_format (fit_hdr)) {
1043 show_boot_progress (-150);
1044 puts ("** Bad FIT image format\n");
1047 show_boot_progress (151);
1048 fit_print_contents (fit_hdr);
1052 /* Loading ok, update default load address */
1056 /* Check if we should attempt an auto-start */
1057 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
1058 char *local_args[2];
1059 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
1061 local_args[0] = argv[0];
1062 local_args[1] = NULL;
1064 printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
1066 do_bootm (cmdtp, 0, 1, local_args);
1073 nboot, 4, 1, do_nandboot,
1074 "nboot - boot from NAND device\n",
1080 #endif /* CONFIG_NAND_LEGACY */