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 CFG_NAND_LEGACY
22 #if defined(CONFIG_CMD_NAND)
27 #include <asm/byteorder.h>
28 #include <jffs2/jffs2.h>
31 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
33 /* parition handling routines */
34 int mtdparts_init(void);
35 int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
36 int find_dev_and_part(const char *id, struct mtd_device **dev,
37 u8 *part_num, struct part_info **part);
40 static int nand_dump_oob(nand_info_t *nand, ulong off)
45 static int nand_dump(nand_info_t *nand, ulong off)
50 buf = malloc(nand->oobblock + nand->oobsize);
52 puts("No memory for page buffer\n");
55 off &= ~(nand->oobblock - 1);
56 i = nand_read_raw(nand, buf, off, nand->oobblock, nand->oobsize);
58 printf("Error (%d) reading page %08x\n", i, off);
62 printf("Page %08x dump:\n", off);
63 i = nand->oobblock >> 4; p = buf;
65 printf( "\t%02x %02x %02x %02x %02x %02x %02x %02x"
66 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
67 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
68 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
72 i = nand->oobsize >> 3;
74 printf( "\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
75 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
83 /* ------------------------------------------------------------------------- */
85 static inline int str2long(char *p, ulong *num)
89 *num = simple_strtoul(p, &endptr, 16);
90 return (*p != '\0' && *endptr == '\0') ? 1 : 0;
94 arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size)
96 int idx = nand_curr_device;
97 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
98 struct mtd_device *dev;
99 struct part_info *part;
102 if (argc >= 1 && !(str2long(argv[0], off))) {
103 if ((mtdparts_init() == 0) &&
104 (find_dev_and_part(argv[0], &dev, &pnum, &part) == 0)) {
105 if (dev->id->type != MTD_DEV_TYPE_NAND) {
106 puts("not a NAND device\n");
111 if (!(str2long(argv[1], (ulong *)size))) {
112 printf("'%s' is not a number\n", argv[1]);
115 if (*size > part->size)
121 *nand = nand_info[idx];
128 if (!(str2long(argv[0], off))) {
129 printf("'%s' is not a number\n", argv[0]);
137 if (!(str2long(argv[1], (ulong *)size))) {
138 printf("'%s' is not a number\n", argv[1]);
142 *size = nand->size - *off;
145 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
148 printf("device %d ", idx);
149 if (*size == nand->size)
150 puts("whole chip\n");
152 printf("offset 0x%x, size 0x%x\n", *off, *size);
156 int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
163 #ifdef CFG_NAND_QUIET
164 int quiet = CFG_NAND_QUIET;
168 const char *quiet_str = getenv("quiet");
170 /* at least two arguments please */
175 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
179 if (strcmp(cmd, "info") == 0) {
182 for (i = 0; i < CFG_MAX_NAND_DEVICE; i++) {
183 if (nand_info[i].name)
184 printf("Device %d: %s, sector size %lu KiB\n",
185 i, nand_info[i].name,
186 nand_info[i].erasesize >> 10);
191 if (strcmp(cmd, "device") == 0) {
194 if ((nand_curr_device < 0) ||
195 (nand_curr_device >= CFG_MAX_NAND_DEVICE))
196 puts("\nno devices available\n");
198 printf("\nDevice %d: %s\n", nand_curr_device,
199 nand_info[nand_curr_device].name);
202 dev = (int)simple_strtoul(argv[2], NULL, 10);
203 if (dev < 0 || dev >= CFG_MAX_NAND_DEVICE || !nand_info[dev].name) {
204 puts("No such device\n");
207 printf("Device %d: %s", dev, nand_info[dev].name);
208 puts("... is now current device\n");
209 nand_curr_device = dev;
211 #ifdef CFG_NAND_SELECT_DEVICE
213 * Select the chip in the board/cpu specific driver
215 board_nand_select_device(nand_info[dev].priv, dev);
221 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
222 strncmp(cmd, "dump", 4) != 0 &&
223 strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
224 strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
225 strcmp(cmd, "biterr") != 0 &&
226 strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
229 /* the following commands operate on the current device */
230 if (nand_curr_device < 0 || nand_curr_device >= CFG_MAX_NAND_DEVICE ||
231 !nand_info[nand_curr_device].name) {
232 puts("\nno devices available\n");
235 nand = &nand_info[nand_curr_device];
237 if (strcmp(cmd, "bad") == 0) {
238 printf("\nDevice %d bad blocks:\n", nand_curr_device);
239 for (off = 0; off < nand->size; off += nand->erasesize)
240 if (nand_block_isbad(nand, off))
241 printf(" %08x\n", off);
248 * nand erase [clean] [off size]
250 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
251 nand_erase_options_t opts;
252 /* "clean" at index 2 means request to write cleanmarker */
253 int clean = argc > 2 && !strcmp("clean", argv[2]);
254 int o = clean ? 3 : 2;
255 int scrub = !strcmp(cmd, "scrub");
257 printf("\nNAND %s: ", scrub ? "scrub" : "erase");
258 /* skip first two or three arguments, look for offset and size */
259 if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
262 memset(&opts, 0, sizeof(opts));
270 "scrub option will erase all factory set "
273 "There is no reliable way to recover them.\n"
275 "Use this command only for testing purposes "
278 "are sure of what you are doing!\n"
279 "\nReally scrub this NAND flash? <y/N>\n");
281 if (getc() == 'y' && getc() == '\r') {
284 puts("scrub aborted\n");
288 ret = nand_erase_opts(nand, &opts);
289 printf("%s\n", ret ? "ERROR" : "OK");
291 return ret == 0 ? 0 : 1;
294 if (strncmp(cmd, "dump", 4) == 0) {
298 s = strchr(cmd, '.');
299 off = (int)simple_strtoul(argv[2], NULL, 16);
301 if (s != NULL && strcmp(s, ".oob") == 0)
302 ret = nand_dump_oob(nand, off);
304 ret = nand_dump(nand, off);
306 return ret == 0 ? 1 : 0;
311 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
317 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
319 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
320 printf("\nNAND %s: ", read ? "read" : "write");
321 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
324 s = strchr(cmd, '.');
326 (!strcmp(s, ".jffs2") || !strcmp(s, ".e") || !strcmp(s, ".i"))) {
329 nand_read_options_t opts;
330 memset(&opts, 0, sizeof(opts));
331 opts.buffer = (u_char*) addr;
335 ret = nand_read_opts(nand, &opts);
338 nand_write_options_t opts;
339 memset(&opts, 0, sizeof(opts));
340 opts.buffer = (u_char*) addr;
343 /* opts.forcejffs2 = 1; */
347 ret = nand_write_opts(nand, &opts);
349 } else if (s != NULL && !strcmp(s, ".oob")) {
350 /* read out-of-band data */
352 ret = nand->read_oob(nand, off, size, &size,
355 ret = nand->write_oob(nand, off, size, &size,
359 ret = nand_read(nand, off, &size, (u_char *)addr);
361 ret = nand_write(nand, off, &size, (u_char *)addr);
364 printf(" %d bytes %s: %s\n", size,
365 read ? "read" : "written", ret ? "ERROR" : "OK");
367 return ret == 0 ? 0 : 1;
370 if (strcmp(cmd, "markbad") == 0) {
371 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
373 int ret = nand->block_markbad(nand, addr);
375 printf("block 0x%08lx successfully marked as bad\n",
379 printf("block 0x%08lx NOT marked as bad! ERROR %d\n",
384 if (strcmp(cmd, "biterr") == 0) {
389 if (strcmp(cmd, "lock") == 0) {
393 if (!strcmp("tight", argv[2]))
395 if (!strcmp("status", argv[2]))
400 ulong block_start = 0;
402 int last_status = -1;
404 struct nand_chip *nand_chip = nand->priv;
405 /* check the WP bit */
406 nand_chip->cmdfunc (nand, NAND_CMD_STATUS, -1, -1);
407 printf("device is %swrite protected\n",
408 (nand_chip->read_byte(nand) & 0x80 ?
411 for (off = 0; off < nand->size; off += nand->oobblock) {
412 int s = nand_get_lock_status(nand, off);
414 /* print message only if status has changed
417 if (off == nand->size - nand->oobblock
418 || (s != last_status && off != 0)) {
420 printf("%08x - %08x: %8d pages %s%s%s\n",
423 (off-block_start)/nand->oobblock,
424 ((last_status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
425 ((last_status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
426 ((last_status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
432 if (!nand_lock(nand, tight)) {
433 puts("NAND flash successfully locked\n");
435 puts("Error locking NAND flash\n");
442 if (strcmp(cmd, "unlock") == 0) {
443 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
446 if (!nand_unlock(nand, off, size)) {
447 puts("NAND flash successfully unlocked\n");
449 puts("Error unlocking NAND flash, "
450 "write and erase will probably fail\n");
457 printf("Usage:\n%s\n", cmdtp->usage);
461 U_BOOT_CMD(nand, 5, 1, do_nand,
462 "nand - NAND sub-system\n",
463 "info - show available NAND devices\n"
464 "nand device [dev] - show or set current device\n"
465 "nand read[.jffs2] - addr off|partition size\n"
466 "nand write[.jffs2] - addr off|partition size - read/write `size' bytes starting\n"
467 " at offset `off' to/from memory address `addr'\n"
468 "nand erase [clean] [off size] - erase `size' bytes from\n"
469 " offset `off' (entire device if not specified)\n"
470 "nand bad - show bad blocks\n"
471 "nand dump[.oob] off - dump page\n"
472 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
473 "nand markbad off - mark bad block at offset (UNSAFE)\n"
474 "nand biterr off - make a bit error at offset (UNSAFE)\n"
475 "nand lock [tight] [status] - bring nand to lock state or display locked pages\n"
476 "nand unlock [offset] [size] - unlock section\n");
478 static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
479 ulong offset, ulong addr, char *cmd)
486 #if defined(CONFIG_FIT)
490 s = strchr(cmd, '.');
492 (!strcmp(s, ".jffs2") || !strcmp(s, ".e") || !strcmp(s, ".i")))
495 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
497 cnt = nand->oobblock;
499 nand_read_options_t opts;
500 memset(&opts, 0, sizeof(opts));
501 opts.buffer = (u_char*) addr;
503 opts.offset = offset;
505 r = nand_read_opts(nand, &opts);
507 r = nand_read(nand, offset, &cnt, (u_char *) addr);
511 puts("** Read error\n");
512 show_boot_progress (-56);
515 show_boot_progress (56);
517 switch (genimg_get_format ((void *)addr)) {
518 case IMAGE_FORMAT_LEGACY:
519 hdr = (image_header_t *)addr;
521 show_boot_progress (57);
522 image_print_contents (hdr);
524 cnt = image_get_image_size (hdr);
526 #if defined(CONFIG_FIT)
527 case IMAGE_FORMAT_FIT:
528 fit_hdr = (const void *)addr;
529 if (!fit_check_format (fit_hdr)) {
530 show_boot_progress (-150);
531 puts ("** Bad FIT image format\n");
534 show_boot_progress (151);
535 puts ("Fit image detected...\n");
537 cnt = fit_get_size (fit_hdr);
541 show_boot_progress (-57);
542 puts ("** Unknown image type\n");
547 nand_read_options_t opts;
548 memset(&opts, 0, sizeof(opts));
549 opts.buffer = (u_char*) addr;
551 opts.offset = offset;
553 r = nand_read_opts(nand, &opts);
555 r = nand_read(nand, offset, &cnt, (u_char *) addr);
559 puts("** Read error\n");
560 show_boot_progress (-58);
563 show_boot_progress (58);
565 #if defined(CONFIG_FIT)
566 /* This cannot be done earlier, we need complete FIT image in RAM first */
567 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT)
568 fit_print_contents ((const void *)addr);
571 /* Loading ok, update default load address */
575 /* Check if we should attempt an auto-start */
576 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
578 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
581 local_args[1] = NULL;
583 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
585 do_bootm(cmdtp, 0, 1, local_args);
591 int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
593 char *boot_device = NULL;
595 ulong addr, offset = 0;
596 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
597 struct mtd_device *dev;
598 struct part_info *part;
602 char *p = (argc == 2) ? argv[1] : argv[2];
603 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
604 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
605 if (dev->id->type != MTD_DEV_TYPE_NAND) {
606 puts("Not a NAND device\n");
612 addr = simple_strtoul(argv[1], NULL, 16);
614 addr = CFG_LOAD_ADDR;
615 return nand_load_image(cmdtp, &nand_info[dev->id->num],
616 part->offset, addr, argv[0]);
621 show_boot_progress(52);
624 addr = CFG_LOAD_ADDR;
625 boot_device = getenv("bootdevice");
628 addr = simple_strtoul(argv[1], NULL, 16);
629 boot_device = getenv("bootdevice");
632 addr = simple_strtoul(argv[1], NULL, 16);
633 boot_device = argv[2];
636 addr = simple_strtoul(argv[1], NULL, 16);
637 boot_device = argv[2];
638 offset = simple_strtoul(argv[3], NULL, 16);
641 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
644 printf("Usage:\n%s\n", cmdtp->usage);
645 show_boot_progress(-53);
649 show_boot_progress(53);
651 puts("\n** No boot device **\n");
652 show_boot_progress(-54);
655 show_boot_progress(54);
657 idx = simple_strtoul(boot_device, NULL, 16);
659 if (idx < 0 || idx >= CFG_MAX_NAND_DEVICE || !nand_info[idx].name) {
660 printf("\n** Device %d not available\n", idx);
661 show_boot_progress(-55);
664 show_boot_progress(55);
666 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
669 U_BOOT_CMD(nboot, 4, 1, do_nandboot,
670 "nboot - boot from NAND device\n",
671 "[.jffs2] [partition] | [[[loadAddr] dev] offset]\n");
675 #else /* CFG_NAND_LEGACY */
678 * Legacy NAND support - to be phased out
684 #include <watchdog.h>
686 #ifdef CONFIG_show_boot_progress
687 # include <status_led.h>
688 # define show_boot_progress(arg) show_boot_progress(arg)
690 # define show_boot_progress(arg)
693 #if defined(CONFIG_CMD_NAND)
694 #include <linux/mtd/nand_legacy.h>
696 #include <linux/mtd/nand_ids.h>
697 #include <jffs2/jffs2.h>
700 #ifdef CONFIG_OMAP1510
701 void archflashwp(void *archdata, int wp);
704 #define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
709 /* ****************** WARNING *********************
710 * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
711 * erase (or at least attempt to erase) blocks that are marked
712 * bad. This can be very handy if you are _sure_ that the block
713 * is OK, say because you marked a good block bad to test bad
714 * block handling and you are done testing, or if you have
715 * accidentally marked blocks bad.
717 * Erasing factory marked bad blocks is a _bad_ idea. If the
718 * erase succeeds there is no reliable way to find them again,
719 * and attempting to program or erase bad blocks can affect
720 * the data in _other_ (good) blocks.
722 #define ALLOW_ERASE_BAD_DEBUG 0
724 #define CONFIG_MTD_NAND_ECC /* enable ECC */
725 #define CONFIG_MTD_NAND_ECC_JFFS2
727 /* bits for nand_legacy_rw() `cmd'; or together as needed */
728 #define NANDRW_READ 0x01
729 #define NANDRW_WRITE 0x00
730 #define NANDRW_JFFS2 0x02
731 #define NANDRW_JFFS2_SKIP 0x04
734 * Imports from nand_legacy.c
736 extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
737 extern int curr_device;
738 extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs,
739 size_t len, int clean);
740 extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start,
741 size_t len, size_t *retlen, u_char *buf);
742 extern void nand_print(struct nand_chip *nand);
743 extern void nand_print_bad(struct nand_chip *nand);
744 extern int nand_read_oob(struct nand_chip *nand, size_t ofs,
745 size_t len, size_t *retlen, u_char *buf);
746 extern int nand_write_oob(struct nand_chip *nand, size_t ofs,
747 size_t len, size_t *retlen, const u_char *buf);
750 int do_nand (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
757 printf ("Usage:\n%s\n", cmdtp->usage);
760 if (strcmp (argv[1], "info") == 0) {
765 for (i = 0; i < CFG_MAX_NAND_DEVICE; ++i) {
766 if (nand_dev_desc[i].ChipID ==
768 continue; /* list only known devices */
769 printf ("Device %d: ", i);
770 nand_print (&nand_dev_desc[i]);
774 } else if (strcmp (argv[1], "device") == 0) {
775 if ((curr_device < 0)
776 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
777 puts ("\nno devices available\n");
780 printf ("\nDevice %d: ", curr_device);
781 nand_print (&nand_dev_desc[curr_device]);
784 } else if (strcmp (argv[1], "bad") == 0) {
785 if ((curr_device < 0)
786 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
787 puts ("\nno devices available\n");
790 printf ("\nDevice %d bad blocks:\n", curr_device);
791 nand_print_bad (&nand_dev_desc[curr_device]);
795 printf ("Usage:\n%s\n", cmdtp->usage);
798 if (strcmp (argv[1], "device") == 0) {
799 int dev = (int) simple_strtoul (argv[2], NULL, 10);
801 printf ("\nDevice %d: ", dev);
802 if (dev >= CFG_MAX_NAND_DEVICE) {
803 puts ("unknown device\n");
806 nand_print (&nand_dev_desc[dev]);
807 /*nand_print (dev); */
809 if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
815 puts ("... is now current device\n");
818 } else if (strcmp (argv[1], "erase") == 0
819 && strcmp (argv[2], "clean") == 0) {
820 struct nand_chip *nand = &nand_dev_desc[curr_device];
822 ulong size = nand->totlen;
825 printf ("\nNAND erase: device %d offset %ld, size %ld ... ", curr_device, off, size);
827 ret = nand_legacy_erase (nand, off, size, 1);
829 printf ("%s\n", ret ? "ERROR" : "OK");
834 printf ("Usage:\n%s\n", cmdtp->usage);
837 /* at least 4 args */
839 if (strncmp (argv[1], "read", 4) == 0 ||
840 strncmp (argv[1], "write", 5) == 0) {
841 ulong addr = simple_strtoul (argv[2], NULL, 16);
842 off_t off = simple_strtoul (argv[3], NULL, 16);
843 size_t size = simple_strtoul (argv[4], NULL, 16);
844 int cmd = (strncmp (argv[1], "read", 4) == 0) ?
845 NANDRW_READ : NANDRW_WRITE;
848 char *cmdtail = strchr (argv[1], '.');
850 if (cmdtail && !strncmp (cmdtail, ".oob", 2)) {
851 /* read out-of-band data */
852 if (cmd & NANDRW_READ) {
853 ret = nand_read_oob (nand_dev_desc + curr_device,
857 ret = nand_write_oob (nand_dev_desc + curr_device,
862 } else if (cmdtail && !strncmp (cmdtail, ".jffs2", 2))
863 cmd |= NANDRW_JFFS2; /* skip bad blocks */
864 else if (cmdtail && !strncmp (cmdtail, ".jffs2s", 2)) {
865 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
866 if (cmd & NANDRW_READ)
867 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
870 /* need ".e" same as ".j" for compatibility with older units */
871 else if (cmdtail && !strcmp (cmdtail, ".e"))
872 cmd |= NANDRW_JFFS2; /* skip bad blocks */
874 #ifdef CFG_NAND_SKIP_BAD_DOT_I
875 /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
876 /* ".i" for image -> read skips bad block (no 0xff) */
877 else if (cmdtail && !strcmp (cmdtail, ".i")) {
878 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
879 if (cmd & NANDRW_READ)
880 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
882 #endif /* CFG_NAND_SKIP_BAD_DOT_I */
884 printf ("Usage:\n%s\n", cmdtp->usage);
888 printf ("\nNAND %s: device %d offset %ld, size %ld ...\n",
889 (cmd & NANDRW_READ) ? "read" : "write",
890 curr_device, off, size);
892 ret = nand_legacy_rw (nand_dev_desc + curr_device,
897 printf (" %d bytes %s: %s\n", total,
898 (cmd & NANDRW_READ) ? "read" : "written",
899 ret ? "ERROR" : "OK");
902 } else if (strcmp (argv[1], "erase") == 0 &&
903 (argc == 4 || strcmp ("clean", argv[2]) == 0)) {
904 int clean = argc == 5;
906 simple_strtoul (argv[2 + clean], NULL, 16);
908 simple_strtoul (argv[3 + clean], NULL, 16);
911 printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
912 curr_device, off, size);
914 ret = nand_legacy_erase (nand_dev_desc + curr_device,
917 printf ("%s\n", ret ? "ERROR" : "OK");
921 printf ("Usage:\n%s\n", cmdtp->usage);
931 "nand - legacy NAND sub-system\n",
932 "info - show available NAND devices\n"
933 "nand device [dev] - show or set current device\n"
934 "nand read[.jffs2[s]] addr off size\n"
935 "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
936 " at offset `off' to/from memory address `addr'\n"
937 "nand erase [clean] [off size] - erase `size' bytes from\n"
938 " offset `off' (entire device if not specified)\n"
939 "nand bad - show bad blocks\n"
940 "nand read.oob addr off size - read out-of-band data\n"
941 "nand write.oob addr off size - read out-of-band data\n"
944 int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
946 char *boot_device = NULL;
954 #if defined(CONFIG_FIT)
958 show_boot_progress (52);
961 addr = CFG_LOAD_ADDR;
962 boot_device = getenv ("bootdevice");
965 addr = simple_strtoul(argv[1], NULL, 16);
966 boot_device = getenv ("bootdevice");
969 addr = simple_strtoul(argv[1], NULL, 16);
970 boot_device = argv[2];
973 addr = simple_strtoul(argv[1], NULL, 16);
974 boot_device = argv[2];
975 offset = simple_strtoul(argv[3], NULL, 16);
978 printf ("Usage:\n%s\n", cmdtp->usage);
979 show_boot_progress (-53);
983 show_boot_progress (53);
985 puts ("\n** No boot device **\n");
986 show_boot_progress (-54);
989 show_boot_progress (54);
991 dev = simple_strtoul(boot_device, &ep, 16);
993 if ((dev >= CFG_MAX_NAND_DEVICE) ||
994 (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
995 printf ("\n** Device %d not available\n", dev);
996 show_boot_progress (-55);
999 show_boot_progress (55);
1001 printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
1002 dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
1005 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
1006 SECTORSIZE, NULL, (u_char *)addr)) {
1007 printf ("** Read error on %d\n", dev);
1008 show_boot_progress (-56);
1011 show_boot_progress (56);
1013 switch (genimg_get_format ((void *)addr)) {
1014 case IMAGE_FORMAT_LEGACY:
1015 hdr = (image_header_t *)addr;
1016 image_print_contents (hdr);
1018 cnt = image_get_image_size (hdr);
1021 #if defined(CONFIG_FIT)
1022 case IMAGE_FORMAT_FIT:
1023 fit_hdr = (const void *)addr;
1024 if (!fit_check_format (fit_hdr)) {
1025 show_boot_progress (-150);
1026 puts ("** Bad FIT image format\n");
1029 show_boot_progress (151);
1030 puts ("Fit image detected...\n");
1032 cnt = fit_get_size (fit_hdr);
1036 show_boot_progress (-57);
1037 puts ("** Unknown image type\n");
1040 show_boot_progress (57);
1042 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
1043 offset + SECTORSIZE, cnt, NULL,
1044 (u_char *)(addr+SECTORSIZE))) {
1045 printf ("** Read error on %d\n", dev);
1046 show_boot_progress (-58);
1049 show_boot_progress (58);
1051 #if defined(CONFIG_FIT)
1052 /* This cannot be done earlier, we need complete FIT image in RAM first */
1053 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT)
1054 fit_print_contents ((const void *)addr);
1057 /* Loading ok, update default load address */
1061 /* Check if we should attempt an auto-start */
1062 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
1063 char *local_args[2];
1064 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
1066 local_args[0] = argv[0];
1067 local_args[1] = NULL;
1069 printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
1071 do_bootm (cmdtp, 0, 1, local_args);
1078 nboot, 4, 1, do_nandboot,
1079 "nboot - boot from NAND device\n",
1085 #endif /* CFG_NAND_LEGACY */