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
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], p[15]);
81 i = nand->oobsize >> 3;
83 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
84 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
93 /* ------------------------------------------------------------------------- */
95 static inline int str2long(char *p, ulong *num)
99 *num = simple_strtoul(p, &endptr, 16);
100 return (*p != '\0' && *endptr == '\0') ? 1 : 0;
104 arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size)
106 int idx = nand_curr_device;
107 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
108 struct mtd_device *dev;
109 struct part_info *part;
112 if (argc >= 1 && !(str2long(argv[0], off))) {
113 if ((mtdparts_init() == 0) &&
114 (find_dev_and_part(argv[0], &dev, &pnum, &part) == 0)) {
115 if (dev->id->type != MTD_DEV_TYPE_NAND) {
116 puts("not a NAND device\n");
121 if (!(str2long(argv[1], (ulong *)size))) {
122 printf("'%s' is not a number\n", argv[1]);
125 if (*size > part->size)
131 *nand = nand_info[idx];
138 if (!(str2long(argv[0], off))) {
139 printf("'%s' is not a number\n", argv[0]);
147 if (!(str2long(argv[1], (ulong *)size))) {
148 printf("'%s' is not a number\n", argv[1]);
152 *size = nand->size - *off;
155 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
158 printf("device %d ", idx);
159 if (*size == nand->size)
160 puts("whole chip\n");
162 printf("offset 0x%lx, size 0x%x\n", *off, *size);
166 int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
173 #ifdef CFG_NAND_QUIET
174 int quiet = CFG_NAND_QUIET;
178 const char *quiet_str = getenv("quiet");
180 /* at least two arguments please */
185 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
189 if (strcmp(cmd, "info") == 0) {
192 for (i = 0; i < CFG_MAX_NAND_DEVICE; i++) {
193 if (nand_info[i].name)
194 printf("Device %d: %s, sector size %u KiB\n",
195 i, nand_info[i].name,
196 nand_info[i].erasesize >> 10);
201 if (strcmp(cmd, "device") == 0) {
204 if ((nand_curr_device < 0) ||
205 (nand_curr_device >= CFG_MAX_NAND_DEVICE))
206 puts("\nno devices available\n");
208 printf("\nDevice %d: %s\n", nand_curr_device,
209 nand_info[nand_curr_device].name);
212 dev = (int)simple_strtoul(argv[2], NULL, 10);
213 if (dev < 0 || dev >= CFG_MAX_NAND_DEVICE || !nand_info[dev].name) {
214 puts("No such device\n");
217 printf("Device %d: %s", dev, nand_info[dev].name);
218 puts("... is now current device\n");
219 nand_curr_device = dev;
221 #ifdef CFG_NAND_SELECT_DEVICE
223 * Select the chip in the board/cpu specific driver
225 board_nand_select_device(nand_info[dev].priv, dev);
231 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
232 strncmp(cmd, "dump", 4) != 0 &&
233 strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
234 strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
235 strcmp(cmd, "biterr") != 0 &&
236 strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
239 /* the following commands operate on the current device */
240 if (nand_curr_device < 0 || nand_curr_device >= CFG_MAX_NAND_DEVICE ||
241 !nand_info[nand_curr_device].name) {
242 puts("\nno devices available\n");
245 nand = &nand_info[nand_curr_device];
247 if (strcmp(cmd, "bad") == 0) {
248 printf("\nDevice %d bad blocks:\n", nand_curr_device);
249 for (off = 0; off < nand->size; off += nand->erasesize)
250 if (nand_block_isbad(nand, off))
251 printf(" %08lx\n", off);
258 * nand erase [clean] [off size]
260 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
261 nand_erase_options_t opts;
262 /* "clean" at index 2 means request to write cleanmarker */
263 int clean = argc > 2 && !strcmp("clean", argv[2]);
264 int o = clean ? 3 : 2;
265 int scrub = !strcmp(cmd, "scrub");
267 printf("\nNAND %s: ", scrub ? "scrub" : "erase");
268 /* skip first two or three arguments, look for offset and size */
269 if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
272 memset(&opts, 0, sizeof(opts));
280 "scrub option will erase all factory set "
283 "There is no reliable way to recover them.\n"
285 "Use this command only for testing purposes "
288 "are sure of what you are doing!\n"
289 "\nReally scrub this NAND flash? <y/N>\n");
291 if (getc() == 'y' && getc() == '\r') {
294 puts("scrub aborted\n");
298 ret = nand_erase_opts(nand, &opts);
299 printf("%s\n", ret ? "ERROR" : "OK");
301 return ret == 0 ? 0 : 1;
304 if (strncmp(cmd, "dump", 4) == 0) {
308 s = strchr(cmd, '.');
309 off = (int)simple_strtoul(argv[2], NULL, 16);
311 if (s != NULL && strcmp(s, ".oob") == 0)
312 ret = nand_dump(nand, off, 1);
314 ret = nand_dump(nand, off, 0);
316 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")) {
339 nand_read_options_t opts;
340 memset(&opts, 0, sizeof(opts));
341 opts.buffer = (u_char*) addr;
348 * TODO: Function must be implemented
350 * ret = nand_read_opts(nand, &opts);
355 memset(&opts, 0, sizeof(opts));
356 opts.datbuf = (u_char*) addr;
359 opts.mode = MTD_OOB_AUTO;
360 ret = nand_write_opts(nand, off, &opts);
362 } else if (s != NULL && !strcmp(s, ".oob")) {
363 /* out-of-band data */
364 mtd_oob_ops_t ops = {
365 .oobbuf = (u8 *)addr,
371 ret = nand->read_oob(nand, off, &ops);
373 ret = nand->write_oob(nand, off, &ops);
375 printf("Unknown nand command suffix '%s'.\n", s);
379 printf(" %d bytes %s: %s\n", size,
380 read ? "read" : "written", ret ? "ERROR" : "OK");
382 return ret == 0 ? 0 : 1;
385 if (strcmp(cmd, "markbad") == 0) {
386 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
388 int ret = nand->block_markbad(nand, addr);
390 printf("block 0x%08lx successfully marked as bad\n",
394 printf("block 0x%08lx NOT marked as bad! ERROR %d\n",
399 if (strcmp(cmd, "biterr") == 0) {
404 if (strcmp(cmd, "lock") == 0) {
408 if (!strcmp("tight", argv[2]))
410 if (!strcmp("status", argv[2]))
416 * TODO: must be implemented and tested by someone with HW
420 ulong block_start = 0;
422 int last_status = -1;
424 struct nand_chip *nand_chip = nand->priv;
425 /* check the WP bit */
426 nand_chip->cmdfunc (nand, NAND_CMD_STATUS, -1, -1);
427 printf("device is %swrite protected\n",
428 (nand_chip->read_byte(nand) & 0x80 ?
431 for (off = 0; off < nand->size; off += nand->writesize) {
432 int s = nand_get_lock_status(nand, off);
434 /* print message only if status has changed
437 if (off == nand->size - nand->writesize
438 || (s != last_status && off != 0)) {
440 printf("%08lx - %08lx: %8d pages %s%s%s\n",
443 (off-block_start)/nand->writesize,
444 ((last_status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
445 ((last_status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
446 ((last_status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
452 if (!nand_lock(nand, tight)) {
453 puts("NAND flash successfully locked\n");
455 puts("Error locking NAND flash\n");
463 if (strcmp(cmd, "unlock") == 0) {
464 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
470 * TODO: must be implemented and tested by someone with HW
473 if (!nand_unlock(nand, off, size)) {
474 puts("NAND flash successfully unlocked\n");
476 puts("Error unlocking NAND flash, "
477 "write and erase will probably fail\n");
485 printf("Usage:\n%s\n", cmdtp->usage);
489 U_BOOT_CMD(nand, 5, 1, do_nand,
490 "nand - NAND sub-system\n",
491 "info - show available NAND devices\n"
492 "nand device [dev] - show or set current device\n"
493 "nand read - addr off|partition size\n"
494 "nand write - addr off|partition size\n"
495 " read/write 'size' bytes starting at offset 'off'\n"
496 " to/from memory address 'addr', skipping bad blocks.\n"
497 "nand erase [clean] [off size] - erase 'size' bytes from\n"
498 " offset 'off' (entire device if not specified)\n"
499 "nand bad - show bad blocks\n"
500 "nand dump[.oob] off - dump page\n"
501 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
502 "nand markbad off - mark bad block at offset (UNSAFE)\n"
503 "nand biterr off - make a bit error at offset (UNSAFE)\n"
504 "nand lock [tight] [status]\n"
505 " bring nand to lock state or display locked pages\n"
506 "nand unlock [offset] [size] - unlock section\n");
508 static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
509 ulong offset, ulong addr, char *cmd)
515 #if defined(CONFIG_FIT)
516 const void *fit_hdr = NULL;
519 s = strchr(cmd, '.');
521 (strcmp(s, ".jffs2") && !strcmp(s, ".e") && !strcmp(s, ".i"))) {
522 printf("Unknown nand load suffix '%s'\n", s);
523 show_boot_progress(-53);
527 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
529 cnt = nand->writesize;
530 r = nand_read(nand, offset, &cnt, (u_char *) addr);
532 puts("** Read error\n");
533 show_boot_progress (-56);
536 show_boot_progress (56);
538 switch (genimg_get_format ((void *)addr)) {
539 case IMAGE_FORMAT_LEGACY:
540 hdr = (image_header_t *)addr;
542 show_boot_progress (57);
543 image_print_contents (hdr);
545 cnt = image_get_image_size (hdr);
547 #if defined(CONFIG_FIT)
548 case IMAGE_FORMAT_FIT:
549 fit_hdr = (const void *)addr;
550 puts ("Fit image detected...\n");
552 cnt = fit_get_size (fit_hdr);
556 show_boot_progress (-57);
557 puts ("** Unknown image type\n");
560 show_boot_progress (57);
562 /* FIXME: skip bad blocks */
563 r = nand_read(nand, offset, &cnt, (u_char *) addr);
565 puts("** Read error\n");
566 show_boot_progress (-58);
569 show_boot_progress (58);
571 #if defined(CONFIG_FIT)
572 /* This cannot be done earlier, we need complete FIT image in RAM first */
573 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
574 if (!fit_check_format (fit_hdr)) {
575 show_boot_progress (-150);
576 puts ("** Bad FIT image format\n");
579 show_boot_progress (151);
580 fit_print_contents (fit_hdr);
584 /* Loading ok, update default load address */
588 /* Check if we should attempt an auto-start */
589 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
591 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
594 local_args[1] = NULL;
596 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
598 do_bootm(cmdtp, 0, 1, local_args);
604 int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
606 char *boot_device = NULL;
608 ulong addr, offset = 0;
609 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
610 struct mtd_device *dev;
611 struct part_info *part;
615 char *p = (argc == 2) ? argv[1] : argv[2];
616 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
617 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
618 if (dev->id->type != MTD_DEV_TYPE_NAND) {
619 puts("Not a NAND device\n");
625 addr = simple_strtoul(argv[1], NULL, 16);
627 addr = CFG_LOAD_ADDR;
628 return nand_load_image(cmdtp, &nand_info[dev->id->num],
629 part->offset, addr, argv[0]);
634 show_boot_progress(52);
637 addr = CFG_LOAD_ADDR;
638 boot_device = getenv("bootdevice");
641 addr = simple_strtoul(argv[1], NULL, 16);
642 boot_device = getenv("bootdevice");
645 addr = simple_strtoul(argv[1], NULL, 16);
646 boot_device = argv[2];
649 addr = simple_strtoul(argv[1], NULL, 16);
650 boot_device = argv[2];
651 offset = simple_strtoul(argv[3], NULL, 16);
654 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_JFFS2_CMDLINE)
657 printf("Usage:\n%s\n", cmdtp->usage);
658 show_boot_progress(-53);
662 show_boot_progress(53);
664 puts("\n** No boot device **\n");
665 show_boot_progress(-54);
668 show_boot_progress(54);
670 idx = simple_strtoul(boot_device, NULL, 16);
672 if (idx < 0 || idx >= CFG_MAX_NAND_DEVICE || !nand_info[idx].name) {
673 printf("\n** Device %d not available\n", idx);
674 show_boot_progress(-55);
677 show_boot_progress(55);
679 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
682 U_BOOT_CMD(nboot, 4, 1, do_nandboot,
683 "nboot - boot from NAND device\n",
684 "[partition] | [[[loadAddr] dev] offset]\n");
688 #else /* CFG_NAND_LEGACY */
691 * Legacy NAND support - to be phased out
697 #include <watchdog.h>
699 #ifdef CONFIG_show_boot_progress
700 # include <status_led.h>
701 # define show_boot_progress(arg) show_boot_progress(arg)
703 # define show_boot_progress(arg)
706 #if defined(CONFIG_CMD_NAND)
707 #include <linux/mtd/nand_legacy.h>
709 #include <linux/mtd/nand_ids.h>
710 #include <jffs2/jffs2.h>
713 #ifdef CONFIG_OMAP1510
714 void archflashwp(void *archdata, int wp);
717 #define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
722 /* ****************** WARNING *********************
723 * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
724 * erase (or at least attempt to erase) blocks that are marked
725 * bad. This can be very handy if you are _sure_ that the block
726 * is OK, say because you marked a good block bad to test bad
727 * block handling and you are done testing, or if you have
728 * accidentally marked blocks bad.
730 * Erasing factory marked bad blocks is a _bad_ idea. If the
731 * erase succeeds there is no reliable way to find them again,
732 * and attempting to program or erase bad blocks can affect
733 * the data in _other_ (good) blocks.
735 #define ALLOW_ERASE_BAD_DEBUG 0
737 #define CONFIG_MTD_NAND_ECC /* enable ECC */
738 #define CONFIG_MTD_NAND_ECC_JFFS2
740 /* bits for nand_legacy_rw() `cmd'; or together as needed */
741 #define NANDRW_READ 0x01
742 #define NANDRW_WRITE 0x00
743 #define NANDRW_JFFS2 0x02
744 #define NANDRW_JFFS2_SKIP 0x04
747 * Imports from nand_legacy.c
749 extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
750 extern int curr_device;
751 extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs,
752 size_t len, int clean);
753 extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start,
754 size_t len, size_t *retlen, u_char *buf);
755 extern void nand_print(struct nand_chip *nand);
756 extern void nand_print_bad(struct nand_chip *nand);
757 extern int nand_read_oob(struct nand_chip *nand, size_t ofs,
758 size_t len, size_t *retlen, u_char *buf);
759 extern int nand_write_oob(struct nand_chip *nand, size_t ofs,
760 size_t len, size_t *retlen, const u_char *buf);
763 int do_nand (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
770 printf ("Usage:\n%s\n", cmdtp->usage);
773 if (strcmp (argv[1], "info") == 0) {
778 for (i = 0; i < CFG_MAX_NAND_DEVICE; ++i) {
779 if (nand_dev_desc[i].ChipID ==
781 continue; /* list only known devices */
782 printf ("Device %d: ", i);
783 nand_print (&nand_dev_desc[i]);
787 } else if (strcmp (argv[1], "device") == 0) {
788 if ((curr_device < 0)
789 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
790 puts ("\nno devices available\n");
793 printf ("\nDevice %d: ", curr_device);
794 nand_print (&nand_dev_desc[curr_device]);
797 } else if (strcmp (argv[1], "bad") == 0) {
798 if ((curr_device < 0)
799 || (curr_device >= CFG_MAX_NAND_DEVICE)) {
800 puts ("\nno devices available\n");
803 printf ("\nDevice %d bad blocks:\n", curr_device);
804 nand_print_bad (&nand_dev_desc[curr_device]);
808 printf ("Usage:\n%s\n", cmdtp->usage);
811 if (strcmp (argv[1], "device") == 0) {
812 int dev = (int) simple_strtoul (argv[2], NULL, 10);
814 printf ("\nDevice %d: ", dev);
815 if (dev >= CFG_MAX_NAND_DEVICE) {
816 puts ("unknown device\n");
819 nand_print (&nand_dev_desc[dev]);
820 /*nand_print (dev); */
822 if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
828 puts ("... is now current device\n");
831 } else if (strcmp (argv[1], "erase") == 0
832 && strcmp (argv[2], "clean") == 0) {
833 struct nand_chip *nand = &nand_dev_desc[curr_device];
835 ulong size = nand->totlen;
838 printf ("\nNAND erase: device %d offset %ld, size %ld ... ", curr_device, off, size);
840 ret = nand_legacy_erase (nand, off, size, 1);
842 printf ("%s\n", ret ? "ERROR" : "OK");
847 printf ("Usage:\n%s\n", cmdtp->usage);
850 /* at least 4 args */
852 if (strncmp (argv[1], "read", 4) == 0 ||
853 strncmp (argv[1], "write", 5) == 0) {
854 ulong addr = simple_strtoul (argv[2], NULL, 16);
855 off_t off = simple_strtoul (argv[3], NULL, 16);
856 size_t size = simple_strtoul (argv[4], NULL, 16);
857 int cmd = (strncmp (argv[1], "read", 4) == 0) ?
858 NANDRW_READ : NANDRW_WRITE;
861 char *cmdtail = strchr (argv[1], '.');
863 if (cmdtail && !strncmp (cmdtail, ".oob", 2)) {
864 /* read out-of-band data */
865 if (cmd & NANDRW_READ) {
866 ret = nand_read_oob (nand_dev_desc + curr_device,
870 ret = nand_write_oob (nand_dev_desc + curr_device,
875 } else if (cmdtail && !strncmp (cmdtail, ".jffs2", 2))
876 cmd |= NANDRW_JFFS2; /* skip bad blocks */
877 else if (cmdtail && !strncmp (cmdtail, ".jffs2s", 2)) {
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) */
883 /* need ".e" same as ".j" for compatibility with older units */
884 else if (cmdtail && !strcmp (cmdtail, ".e"))
885 cmd |= NANDRW_JFFS2; /* skip bad blocks */
887 #ifdef CFG_NAND_SKIP_BAD_DOT_I
888 /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
889 /* ".i" for image -> read skips bad block (no 0xff) */
890 else if (cmdtail && !strcmp (cmdtail, ".i")) {
891 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
892 if (cmd & NANDRW_READ)
893 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
895 #endif /* CFG_NAND_SKIP_BAD_DOT_I */
897 printf ("Usage:\n%s\n", cmdtp->usage);
901 printf ("\nNAND %s: device %d offset %ld, size %lu ...\n",
902 (cmd & NANDRW_READ) ? "read" : "write",
903 curr_device, off, (ulong)size);
905 ret = nand_legacy_rw (nand_dev_desc + curr_device,
907 &total, (u_char *) addr);
909 printf (" %d bytes %s: %s\n", total,
910 (cmd & NANDRW_READ) ? "read" : "written",
911 ret ? "ERROR" : "OK");
914 } else if (strcmp (argv[1], "erase") == 0 &&
915 (argc == 4 || strcmp ("clean", argv[2]) == 0)) {
916 int clean = argc == 5;
918 simple_strtoul (argv[2 + clean], NULL, 16);
920 simple_strtoul (argv[3 + clean], NULL, 16);
923 printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
924 curr_device, off, size);
926 ret = nand_legacy_erase (nand_dev_desc + curr_device,
929 printf ("%s\n", ret ? "ERROR" : "OK");
933 printf ("Usage:\n%s\n", cmdtp->usage);
943 "nand - legacy NAND sub-system\n",
944 "info - show available NAND devices\n"
945 "nand device [dev] - show or set current device\n"
946 "nand read[.jffs2[s]] addr off size\n"
947 "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
948 " at offset `off' to/from memory address `addr'\n"
949 "nand erase [clean] [off size] - erase `size' bytes from\n"
950 " offset `off' (entire device if not specified)\n"
951 "nand bad - show bad blocks\n"
952 "nand read.oob addr off size - read out-of-band data\n"
953 "nand write.oob addr off size - read out-of-band data\n"
956 int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
958 char *boot_device = NULL;
966 #if defined(CONFIG_FIT)
967 const void *fit_hdr = NULL;
970 show_boot_progress (52);
973 addr = CFG_LOAD_ADDR;
974 boot_device = getenv ("bootdevice");
977 addr = simple_strtoul(argv[1], NULL, 16);
978 boot_device = getenv ("bootdevice");
981 addr = simple_strtoul(argv[1], NULL, 16);
982 boot_device = argv[2];
985 addr = simple_strtoul(argv[1], NULL, 16);
986 boot_device = argv[2];
987 offset = simple_strtoul(argv[3], NULL, 16);
990 printf ("Usage:\n%s\n", cmdtp->usage);
991 show_boot_progress (-53);
995 show_boot_progress (53);
997 puts ("\n** No boot device **\n");
998 show_boot_progress (-54);
1001 show_boot_progress (54);
1003 dev = simple_strtoul(boot_device, &ep, 16);
1005 if ((dev >= CFG_MAX_NAND_DEVICE) ||
1006 (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
1007 printf ("\n** Device %d not available\n", dev);
1008 show_boot_progress (-55);
1011 show_boot_progress (55);
1013 printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
1014 dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
1017 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
1018 SECTORSIZE, NULL, (u_char *)addr)) {
1019 printf ("** Read error on %d\n", dev);
1020 show_boot_progress (-56);
1023 show_boot_progress (56);
1025 switch (genimg_get_format ((void *)addr)) {
1026 case IMAGE_FORMAT_LEGACY:
1027 hdr = (image_header_t *)addr;
1028 image_print_contents (hdr);
1030 cnt = image_get_image_size (hdr);
1033 #if defined(CONFIG_FIT)
1034 case IMAGE_FORMAT_FIT:
1035 fit_hdr = (const void *)addr;
1036 puts ("Fit image detected...\n");
1038 cnt = fit_get_size (fit_hdr);
1042 show_boot_progress (-57);
1043 puts ("** Unknown image type\n");
1046 show_boot_progress (57);
1048 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
1049 offset + SECTORSIZE, cnt, NULL,
1050 (u_char *)(addr+SECTORSIZE))) {
1051 printf ("** Read error on %d\n", dev);
1052 show_boot_progress (-58);
1055 show_boot_progress (58);
1057 #if defined(CONFIG_FIT)
1058 /* This cannot be done earlier, we need complete FIT image in RAM first */
1059 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
1060 if (!fit_check_format (fit_hdr)) {
1061 show_boot_progress (-150);
1062 puts ("** Bad FIT image format\n");
1065 show_boot_progress (151);
1066 fit_print_contents (fit_hdr);
1070 /* Loading ok, update default load address */
1074 /* Check if we should attempt an auto-start */
1075 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
1076 char *local_args[2];
1077 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
1079 local_args[0] = argv[0];
1080 local_args[1] = NULL;
1082 printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
1084 do_bootm (cmdtp, 0, 1, local_args);
1091 nboot, 4, 1, do_nandboot,
1092 "nboot - boot from NAND device\n",
1098 #endif /* CFG_NAND_LEGACY */