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%zx\n", *off, *size);
167 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
168 static void print_status(ulong start, ulong end, ulong erasesize, int status)
170 printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
173 (end - start) / erasesize,
174 ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
175 ((status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
176 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
179 static void do_nand_status(nand_info_t *nand)
181 ulong block_start = 0;
183 int last_status = -1;
185 struct nand_chip *nand_chip = nand->priv;
186 /* check the WP bit */
187 nand_chip->cmdfunc(nand, NAND_CMD_STATUS, -1, -1);
188 printf("device is %swrite protected\n",
189 (nand_chip->read_byte(nand) & 0x80 ?
192 for (off = 0; off < nand->size; off += nand->erasesize) {
193 int s = nand_get_lock_status(nand, off);
195 /* print message only if status has changed */
196 if (s != last_status && off != 0) {
197 print_status(block_start, off, nand->erasesize,
203 /* Print the last block info */
204 print_status(block_start, off, nand->erasesize, last_status);
208 int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
215 #ifdef CONFIG_SYS_NAND_QUIET
216 int quiet = CONFIG_SYS_NAND_QUIET;
220 const char *quiet_str = getenv("quiet");
222 /* at least two arguments please */
227 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
231 if (strcmp(cmd, "info") == 0) {
234 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
235 if (nand_info[i].name)
236 printf("Device %d: %s, sector size %u KiB\n",
237 i, nand_info[i].name,
238 nand_info[i].erasesize >> 10);
243 if (strcmp(cmd, "device") == 0) {
246 if ((nand_curr_device < 0) ||
247 (nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE))
248 puts("\nno devices available\n");
250 printf("\nDevice %d: %s\n", nand_curr_device,
251 nand_info[nand_curr_device].name);
254 dev = (int)simple_strtoul(argv[2], NULL, 10);
255 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev].name) {
256 puts("No such device\n");
259 printf("Device %d: %s", dev, nand_info[dev].name);
260 puts("... is now current device\n");
261 nand_curr_device = dev;
263 #ifdef CONFIG_SYS_NAND_SELECT_DEVICE
265 * Select the chip in the board/cpu specific driver
267 board_nand_select_device(nand_info[dev].priv, dev);
273 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
274 strncmp(cmd, "dump", 4) != 0 &&
275 strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
276 strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
277 strcmp(cmd, "biterr") != 0 &&
278 strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
281 /* the following commands operate on the current device */
282 if (nand_curr_device < 0 || nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
283 !nand_info[nand_curr_device].name) {
284 puts("\nno devices available\n");
287 nand = &nand_info[nand_curr_device];
289 if (strcmp(cmd, "bad") == 0) {
290 printf("\nDevice %d bad blocks:\n", nand_curr_device);
291 for (off = 0; off < nand->size; off += nand->erasesize)
292 if (nand_block_isbad(nand, off))
293 printf(" %08lx\n", off);
300 * nand erase [clean] [off size]
302 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
303 nand_erase_options_t opts;
304 /* "clean" at index 2 means request to write cleanmarker */
305 int clean = argc > 2 && !strcmp("clean", argv[2]);
306 int o = clean ? 3 : 2;
307 int scrub = !strcmp(cmd, "scrub");
309 printf("\nNAND %s: ", scrub ? "scrub" : "erase");
310 /* skip first two or three arguments, look for offset and size */
311 if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
314 memset(&opts, 0, sizeof(opts));
322 "scrub option will erase all factory set "
325 "There is no reliable way to recover them.\n"
327 "Use this command only for testing purposes "
330 "are sure of what you are doing!\n"
331 "\nReally scrub this NAND flash? <y/N>\n");
333 if (getc() == 'y' && getc() == '\r') {
336 puts("scrub aborted\n");
340 ret = nand_erase_opts(nand, &opts);
341 printf("%s\n", ret ? "ERROR" : "OK");
343 return ret == 0 ? 0 : 1;
346 if (strncmp(cmd, "dump", 4) == 0) {
350 s = strchr(cmd, '.');
351 off = (int)simple_strtoul(argv[2], NULL, 16);
353 if (s != NULL && strcmp(s, ".oob") == 0)
354 ret = nand_dump(nand, off, 1);
356 ret = nand_dump(nand, off, 0);
358 return ret == 0 ? 1 : 0;
362 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
368 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
370 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
371 printf("\nNAND %s: ", read ? "read" : "write");
372 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
375 s = strchr(cmd, '.');
376 if (!s || !strcmp(s, ".jffs2") ||
377 !strcmp(s, ".e") || !strcmp(s, ".i")) {
379 ret = nand_read_skip_bad(nand, off, &size,
382 ret = nand_write_skip_bad(nand, off, &size,
384 } else if (s != NULL && !strcmp(s, ".oob")) {
385 /* out-of-band data */
386 mtd_oob_ops_t ops = {
387 .oobbuf = (u8 *)addr,
393 ret = nand->read_oob(nand, off, &ops);
395 ret = nand->write_oob(nand, off, &ops);
397 printf("Unknown nand command suffix '%s'.\n", s);
401 printf(" %zu bytes %s: %s\n", size,
402 read ? "read" : "written", ret ? "ERROR" : "OK");
404 return ret == 0 ? 0 : 1;
407 if (strcmp(cmd, "markbad") == 0) {
408 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
410 int ret = nand->block_markbad(nand, addr);
412 printf("block 0x%08lx successfully marked as bad\n",
416 printf("block 0x%08lx NOT marked as bad! ERROR %d\n",
422 if (strcmp(cmd, "biterr") == 0) {
427 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
428 if (strcmp(cmd, "lock") == 0) {
432 if (!strcmp("tight", argv[2]))
434 if (!strcmp("status", argv[2]))
438 do_nand_status(nand);
440 if (!nand_lock(nand, tight)) {
441 puts("NAND flash successfully locked\n");
443 puts("Error locking NAND flash\n");
450 if (strcmp(cmd, "unlock") == 0) {
451 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
454 if (!nand_unlock(nand, off, size)) {
455 puts("NAND flash successfully unlocked\n");
457 puts("Error unlocking NAND flash, "
458 "write and erase will probably fail\n");
470 U_BOOT_CMD(nand, 5, 1, do_nand,
472 "info - show available NAND devices\n"
473 "nand device [dev] - show or set current device\n"
474 "nand read - addr off|partition size\n"
475 "nand write - addr off|partition size\n"
476 " read/write 'size' bytes starting at offset 'off'\n"
477 " to/from memory address 'addr', skipping bad blocks.\n"
478 "nand erase [clean] [off size] - erase 'size' bytes from\n"
479 " offset 'off' (entire device if not specified)\n"
480 "nand bad - show bad blocks\n"
481 "nand dump[.oob] off - dump page\n"
482 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
483 "nand markbad off - mark bad block at offset (UNSAFE)\n"
484 "nand biterr off - make a bit error at offset (UNSAFE)\n"
485 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
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"
492 static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
493 ulong offset, ulong addr, char *cmd)
499 #if defined(CONFIG_FIT)
500 const void *fit_hdr = NULL;
503 s = strchr(cmd, '.');
505 (strcmp(s, ".jffs2") && !strcmp(s, ".e") && !strcmp(s, ".i"))) {
506 printf("Unknown nand load suffix '%s'\n", s);
507 show_boot_progress(-53);
511 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
513 cnt = nand->writesize;
514 r = nand_read(nand, offset, &cnt, (u_char *) addr);
516 puts("** Read error\n");
517 show_boot_progress (-56);
520 show_boot_progress (56);
522 switch (genimg_get_format ((void *)addr)) {
523 case IMAGE_FORMAT_LEGACY:
524 hdr = (image_header_t *)addr;
526 show_boot_progress (57);
527 image_print_contents (hdr);
529 cnt = image_get_image_size (hdr);
531 #if defined(CONFIG_FIT)
532 case IMAGE_FORMAT_FIT:
533 fit_hdr = (const void *)addr;
534 puts ("Fit image detected...\n");
536 cnt = fit_get_size (fit_hdr);
540 show_boot_progress (-57);
541 puts ("** Unknown image type\n");
544 show_boot_progress (57);
546 /* FIXME: skip bad blocks */
547 r = nand_read(nand, offset, &cnt, (u_char *) addr);
549 puts("** Read error\n");
550 show_boot_progress (-58);
553 show_boot_progress (58);
555 #if defined(CONFIG_FIT)
556 /* This cannot be done earlier, we need complete FIT image in RAM first */
557 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
558 if (!fit_check_format (fit_hdr)) {
559 show_boot_progress (-150);
560 puts ("** Bad FIT image format\n");
563 show_boot_progress (151);
564 fit_print_contents (fit_hdr);
568 /* Loading ok, update default load address */
572 /* Check if we should attempt an auto-start */
573 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
575 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
578 local_args[1] = NULL;
580 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
582 do_bootm(cmdtp, 0, 1, local_args);
588 int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
590 char *boot_device = NULL;
592 ulong addr, offset = 0;
593 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
594 struct mtd_device *dev;
595 struct part_info *part;
599 char *p = (argc == 2) ? argv[1] : argv[2];
600 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
601 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
602 if (dev->id->type != MTD_DEV_TYPE_NAND) {
603 puts("Not a NAND device\n");
609 addr = simple_strtoul(argv[1], NULL, 16);
611 addr = CONFIG_SYS_LOAD_ADDR;
612 return nand_load_image(cmdtp, &nand_info[dev->id->num],
613 part->offset, addr, argv[0]);
618 show_boot_progress(52);
621 addr = CONFIG_SYS_LOAD_ADDR;
622 boot_device = getenv("bootdevice");
625 addr = simple_strtoul(argv[1], NULL, 16);
626 boot_device = getenv("bootdevice");
629 addr = simple_strtoul(argv[1], NULL, 16);
630 boot_device = argv[2];
633 addr = simple_strtoul(argv[1], NULL, 16);
634 boot_device = argv[2];
635 offset = simple_strtoul(argv[3], NULL, 16);
638 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
642 show_boot_progress(-53);
646 show_boot_progress(53);
648 puts("\n** No boot device **\n");
649 show_boot_progress(-54);
652 show_boot_progress(54);
654 idx = simple_strtoul(boot_device, NULL, 16);
656 if (idx < 0 || idx >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[idx].name) {
657 printf("\n** Device %d not available\n", idx);
658 show_boot_progress(-55);
661 show_boot_progress(55);
663 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
666 U_BOOT_CMD(nboot, 4, 1, do_nandboot,
667 "boot from NAND device",
668 "[partition] | [[[loadAddr] dev] offset]\n");
672 #else /* CONFIG_NAND_LEGACY */
675 * Legacy NAND support - to be phased out
681 #include <watchdog.h>
683 #ifdef CONFIG_show_boot_progress
684 # include <status_led.h>
685 # define show_boot_progress(arg) show_boot_progress(arg)
687 # define show_boot_progress(arg)
690 #if defined(CONFIG_CMD_NAND)
691 #include <linux/mtd/nand_legacy.h>
693 #include <linux/mtd/nand_ids.h>
694 #include <jffs2/jffs2.h>
697 #ifdef CONFIG_OMAP1510
698 void archflashwp(void *archdata, int wp);
701 #define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
706 /* ****************** WARNING *********************
707 * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
708 * erase (or at least attempt to erase) blocks that are marked
709 * bad. This can be very handy if you are _sure_ that the block
710 * is OK, say because you marked a good block bad to test bad
711 * block handling and you are done testing, or if you have
712 * accidentally marked blocks bad.
714 * Erasing factory marked bad blocks is a _bad_ idea. If the
715 * erase succeeds there is no reliable way to find them again,
716 * and attempting to program or erase bad blocks can affect
717 * the data in _other_ (good) blocks.
719 #define ALLOW_ERASE_BAD_DEBUG 0
721 #define CONFIG_MTD_NAND_ECC /* enable ECC */
722 #define CONFIG_MTD_NAND_ECC_JFFS2
724 /* bits for nand_legacy_rw() `cmd'; or together as needed */
725 #define NANDRW_READ 0x01
726 #define NANDRW_WRITE 0x00
727 #define NANDRW_JFFS2 0x02
728 #define NANDRW_JFFS2_SKIP 0x04
731 * Imports from nand_legacy.c
733 extern struct nand_chip nand_dev_desc[CONFIG_SYS_MAX_NAND_DEVICE];
734 extern int curr_device;
735 extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs,
736 size_t len, int clean);
737 extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start,
738 size_t len, size_t *retlen, u_char *buf);
739 extern void nand_print(struct nand_chip *nand);
740 extern void nand_print_bad(struct nand_chip *nand);
741 extern int nand_read_oob(struct nand_chip *nand, size_t ofs,
742 size_t len, size_t *retlen, u_char *buf);
743 extern int nand_write_oob(struct nand_chip *nand, size_t ofs,
744 size_t len, size_t *retlen, const u_char *buf);
747 int do_nand (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
757 if (strcmp (argv[1], "info") == 0) {
762 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; ++i) {
763 if (nand_dev_desc[i].ChipID ==
765 continue; /* list only known devices */
766 printf ("Device %d: ", i);
767 nand_print (&nand_dev_desc[i]);
771 } else if (strcmp (argv[1], "device") == 0) {
772 if ((curr_device < 0)
773 || (curr_device >= CONFIG_SYS_MAX_NAND_DEVICE)) {
774 puts ("\nno devices available\n");
777 printf ("\nDevice %d: ", curr_device);
778 nand_print (&nand_dev_desc[curr_device]);
781 } else if (strcmp (argv[1], "bad") == 0) {
782 if ((curr_device < 0)
783 || (curr_device >= CONFIG_SYS_MAX_NAND_DEVICE)) {
784 puts ("\nno devices available\n");
787 printf ("\nDevice %d bad blocks:\n", curr_device);
788 nand_print_bad (&nand_dev_desc[curr_device]);
795 if (strcmp (argv[1], "device") == 0) {
796 int dev = (int) simple_strtoul (argv[2], NULL, 10);
798 printf ("\nDevice %d: ", dev);
799 if (dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
800 puts ("unknown device\n");
803 nand_print (&nand_dev_desc[dev]);
804 /*nand_print (dev); */
806 if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
812 puts ("... is now current device\n");
815 } else if (strcmp (argv[1], "erase") == 0
816 && strcmp (argv[2], "clean") == 0) {
817 struct nand_chip *nand = &nand_dev_desc[curr_device];
819 ulong size = nand->totlen;
822 printf ("\nNAND erase: device %d offset %ld, size %ld ... ", curr_device, off, size);
824 ret = nand_legacy_erase (nand, off, size, 1);
826 printf ("%s\n", ret ? "ERROR" : "OK");
834 /* at least 4 args */
836 if (strncmp (argv[1], "read", 4) == 0 ||
837 strncmp (argv[1], "write", 5) == 0) {
838 ulong addr = simple_strtoul (argv[2], NULL, 16);
839 off_t off = simple_strtoul (argv[3], NULL, 16);
840 size_t size = simple_strtoul (argv[4], NULL, 16);
841 int cmd = (strncmp (argv[1], "read", 4) == 0) ?
842 NANDRW_READ : NANDRW_WRITE;
845 char *cmdtail = strchr (argv[1], '.');
847 if (cmdtail && !strncmp (cmdtail, ".oob", 2)) {
848 /* read out-of-band data */
849 if (cmd & NANDRW_READ) {
850 ret = nand_read_oob (nand_dev_desc + curr_device,
854 ret = nand_write_oob (nand_dev_desc + curr_device,
859 } else if (cmdtail && !strncmp (cmdtail, ".jffs2s", 7)) {
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) */
863 } else if (cmdtail && !strncmp (cmdtail, ".jffs2", 2))
864 cmd |= NANDRW_JFFS2; /* skip bad blocks */
866 /* need ".e" same as ".j" for compatibility with older units */
867 else if (cmdtail && !strcmp (cmdtail, ".e"))
868 cmd |= NANDRW_JFFS2; /* skip bad blocks */
870 #ifdef CONFIG_SYS_NAND_SKIP_BAD_DOT_I
871 /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
872 /* ".i" for image -> read skips bad block (no 0xff) */
873 else if (cmdtail && !strcmp (cmdtail, ".i")) {
874 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
875 if (cmd & NANDRW_READ)
876 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
878 #endif /* CONFIG_SYS_NAND_SKIP_BAD_DOT_I */
884 printf ("\nNAND %s: device %d offset %ld, size %lu ...\n",
885 (cmd & NANDRW_READ) ? "read" : "write",
886 curr_device, off, (ulong)size);
888 ret = nand_legacy_rw (nand_dev_desc + curr_device,
890 &total, (u_char *) addr);
892 printf (" %d bytes %s: %s\n", total,
893 (cmd & NANDRW_READ) ? "read" : "written",
894 ret ? "ERROR" : "OK");
897 } else if (strcmp (argv[1], "erase") == 0 &&
898 (argc == 4 || strcmp ("clean", argv[2]) == 0)) {
899 int clean = argc == 5;
901 simple_strtoul (argv[2 + clean], NULL, 16);
903 simple_strtoul (argv[3 + clean], NULL, 16);
906 printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
907 curr_device, off, size);
909 ret = nand_legacy_erase (nand_dev_desc + curr_device,
912 printf ("%s\n", ret ? "ERROR" : "OK");
926 "legacy NAND sub-system",
927 "info - show available NAND devices\n"
928 "nand device [dev] - show or set current device\n"
929 "nand read[.jffs2[s]] addr off size\n"
930 "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
931 " at offset `off' to/from memory address `addr'\n"
932 "nand erase [clean] [off size] - erase `size' bytes from\n"
933 " offset `off' (entire device if not specified)\n"
934 "nand bad - show bad blocks\n"
935 "nand read.oob addr off size - read out-of-band data\n"
936 "nand write.oob addr off size - read out-of-band data\n"
939 int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
941 char *boot_device = NULL;
949 #if defined(CONFIG_FIT)
950 const void *fit_hdr = NULL;
953 show_boot_progress (52);
956 addr = CONFIG_SYS_LOAD_ADDR;
957 boot_device = getenv ("bootdevice");
960 addr = simple_strtoul(argv[1], NULL, 16);
961 boot_device = getenv ("bootdevice");
964 addr = simple_strtoul(argv[1], NULL, 16);
965 boot_device = argv[2];
968 addr = simple_strtoul(argv[1], NULL, 16);
969 boot_device = argv[2];
970 offset = simple_strtoul(argv[3], NULL, 16);
974 show_boot_progress (-53);
978 show_boot_progress (53);
980 puts ("\n** No boot device **\n");
981 show_boot_progress (-54);
984 show_boot_progress (54);
986 dev = simple_strtoul(boot_device, &ep, 16);
988 if ((dev >= CONFIG_SYS_MAX_NAND_DEVICE) ||
989 (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
990 printf ("\n** Device %d not available\n", dev);
991 show_boot_progress (-55);
994 show_boot_progress (55);
996 printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
997 dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
1000 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
1001 SECTORSIZE, NULL, (u_char *)addr)) {
1002 printf ("** Read error on %d\n", dev);
1003 show_boot_progress (-56);
1006 show_boot_progress (56);
1008 switch (genimg_get_format ((void *)addr)) {
1009 case IMAGE_FORMAT_LEGACY:
1010 hdr = (image_header_t *)addr;
1011 image_print_contents (hdr);
1013 cnt = image_get_image_size (hdr);
1016 #if defined(CONFIG_FIT)
1017 case IMAGE_FORMAT_FIT:
1018 fit_hdr = (const void *)addr;
1019 puts ("Fit image detected...\n");
1021 cnt = fit_get_size (fit_hdr);
1025 show_boot_progress (-57);
1026 puts ("** Unknown image type\n");
1029 show_boot_progress (57);
1031 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
1032 offset + SECTORSIZE, cnt, NULL,
1033 (u_char *)(addr+SECTORSIZE))) {
1034 printf ("** Read error on %d\n", dev);
1035 show_boot_progress (-58);
1038 show_boot_progress (58);
1040 #if defined(CONFIG_FIT)
1041 /* This cannot be done earlier, we need complete FIT image in RAM first */
1042 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
1043 if (!fit_check_format (fit_hdr)) {
1044 show_boot_progress (-150);
1045 puts ("** Bad FIT image format\n");
1048 show_boot_progress (151);
1049 fit_print_contents (fit_hdr);
1053 /* Loading ok, update default load address */
1057 /* Check if we should attempt an auto-start */
1058 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
1059 char *local_args[2];
1060 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
1062 local_args[0] = argv[0];
1063 local_args[1] = NULL;
1065 printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
1067 do_bootm (cmdtp, 0, 1, local_args);
1074 nboot, 4, 1, do_nandboot,
1075 "boot from NAND device",
1081 #endif /* CONFIG_NAND_LEGACY */