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_CMD_MTDPARTS)
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_CMD_MTDPARTS)
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_CMD_MTDPARTS)
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 static void nand_print_info(int idx)
210 nand_info_t *nand = &nand_info[idx];
211 struct nand_chip *chip = nand->priv;
212 printf("Device %d: ", idx);
213 if (chip->numchips > 1)
214 printf("%dx ", chip->numchips);
215 printf("%s, sector size %u KiB\n",
216 nand->name, nand->erasesize >> 10);
219 int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
226 #ifdef CONFIG_SYS_NAND_QUIET
227 int quiet = CONFIG_SYS_NAND_QUIET;
231 const char *quiet_str = getenv("quiet");
233 /* at least two arguments please */
238 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
242 if (strcmp(cmd, "info") == 0) {
245 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
246 if (nand_info[i].name)
252 if (strcmp(cmd, "device") == 0) {
256 if ((nand_curr_device < 0) ||
257 (nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE))
258 puts("no devices available\n");
260 nand_print_info(nand_curr_device);
263 dev = (int)simple_strtoul(argv[2], NULL, 10);
264 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev].name) {
265 puts("No such device\n");
268 printf("Device %d: %s", dev, nand_info[dev].name);
269 puts("... is now current device\n");
270 nand_curr_device = dev;
272 #ifdef CONFIG_SYS_NAND_SELECT_DEVICE
274 * Select the chip in the board/cpu specific driver
276 board_nand_select_device(nand_info[dev].priv, dev);
282 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
283 strncmp(cmd, "dump", 4) != 0 &&
284 strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
285 strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
286 strcmp(cmd, "biterr") != 0 &&
287 strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
290 /* the following commands operate on the current device */
291 if (nand_curr_device < 0 || nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
292 !nand_info[nand_curr_device].name) {
293 puts("\nno devices available\n");
296 nand = &nand_info[nand_curr_device];
298 if (strcmp(cmd, "bad") == 0) {
299 printf("\nDevice %d bad blocks:\n", nand_curr_device);
300 for (off = 0; off < nand->size; off += nand->erasesize)
301 if (nand_block_isbad(nand, off))
302 printf(" %08lx\n", off);
309 * nand erase [clean] [off size]
311 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
312 nand_erase_options_t opts;
313 /* "clean" at index 2 means request to write cleanmarker */
314 int clean = argc > 2 && !strcmp("clean", argv[2]);
315 int o = clean ? 3 : 2;
316 int scrub = !strcmp(cmd, "scrub");
318 printf("\nNAND %s: ", scrub ? "scrub" : "erase");
319 /* skip first two or three arguments, look for offset and size */
320 if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
323 memset(&opts, 0, sizeof(opts));
331 "scrub option will erase all factory set "
334 "There is no reliable way to recover them.\n"
336 "Use this command only for testing purposes "
339 "are sure of what you are doing!\n"
340 "\nReally scrub this NAND flash? <y/N>\n");
342 if (getc() == 'y' && getc() == '\r') {
345 puts("scrub aborted\n");
349 ret = nand_erase_opts(nand, &opts);
350 printf("%s\n", ret ? "ERROR" : "OK");
352 return ret == 0 ? 0 : 1;
355 if (strncmp(cmd, "dump", 4) == 0) {
359 s = strchr(cmd, '.');
360 off = (int)simple_strtoul(argv[2], NULL, 16);
362 if (s != NULL && strcmp(s, ".oob") == 0)
363 ret = nand_dump(nand, off, 1);
365 ret = nand_dump(nand, off, 0);
367 return ret == 0 ? 1 : 0;
371 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
377 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
379 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
380 printf("\nNAND %s: ", read ? "read" : "write");
381 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
384 s = strchr(cmd, '.');
385 if (!s || !strcmp(s, ".jffs2") ||
386 !strcmp(s, ".e") || !strcmp(s, ".i")) {
388 ret = nand_read_skip_bad(nand, off, &size,
391 ret = nand_write_skip_bad(nand, off, &size,
393 } else if (!strcmp(s, ".oob")) {
394 /* out-of-band data */
395 mtd_oob_ops_t ops = {
396 .oobbuf = (u8 *)addr,
402 ret = nand->read_oob(nand, off, &ops);
404 ret = nand->write_oob(nand, off, &ops);
406 printf("Unknown nand command suffix '%s'.\n", s);
410 printf(" %zu bytes %s: %s\n", size,
411 read ? "read" : "written", ret ? "ERROR" : "OK");
413 return ret == 0 ? 0 : 1;
416 if (strcmp(cmd, "markbad") == 0) {
417 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
419 int ret = nand->block_markbad(nand, addr);
421 printf("block 0x%08lx successfully marked as bad\n",
425 printf("block 0x%08lx NOT marked as bad! ERROR %d\n",
431 if (strcmp(cmd, "biterr") == 0) {
436 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
437 if (strcmp(cmd, "lock") == 0) {
441 if (!strcmp("tight", argv[2]))
443 if (!strcmp("status", argv[2]))
447 do_nand_status(nand);
449 if (!nand_lock(nand, tight)) {
450 puts("NAND flash successfully locked\n");
452 puts("Error locking NAND flash\n");
459 if (strcmp(cmd, "unlock") == 0) {
460 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
463 if (!nand_unlock(nand, off, size)) {
464 puts("NAND flash successfully unlocked\n");
466 puts("Error unlocking NAND flash, "
467 "write and erase will probably fail\n");
479 U_BOOT_CMD(nand, 5, 1, do_nand,
481 "info - show available NAND devices\n"
482 "nand device [dev] - show or set current device\n"
483 "nand read - addr off|partition size\n"
484 "nand write - addr off|partition size\n"
485 " read/write 'size' bytes starting at offset 'off'\n"
486 " to/from memory address 'addr', skipping bad blocks.\n"
487 "nand erase [clean] [off size] - erase 'size' bytes from\n"
488 " offset 'off' (entire device if not specified)\n"
489 "nand bad - show bad blocks\n"
490 "nand dump[.oob] off - dump page\n"
491 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
492 "nand markbad off - mark bad block at offset (UNSAFE)\n"
493 "nand biterr off - make a bit error at offset (UNSAFE)\n"
494 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
495 "nand lock [tight] [status]\n"
496 " bring nand to lock state or display locked pages\n"
497 "nand unlock [offset] [size] - unlock section\n"
501 static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
502 ulong offset, ulong addr, char *cmd)
508 #if defined(CONFIG_FIT)
509 const void *fit_hdr = NULL;
512 s = strchr(cmd, '.');
514 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
515 printf("Unknown nand load suffix '%s'\n", s);
516 show_boot_progress(-53);
520 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
522 cnt = nand->writesize;
523 r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
525 puts("** Read error\n");
526 show_boot_progress (-56);
529 show_boot_progress (56);
531 switch (genimg_get_format ((void *)addr)) {
532 case IMAGE_FORMAT_LEGACY:
533 hdr = (image_header_t *)addr;
535 show_boot_progress (57);
536 image_print_contents (hdr);
538 cnt = image_get_image_size (hdr);
540 #if defined(CONFIG_FIT)
541 case IMAGE_FORMAT_FIT:
542 fit_hdr = (const void *)addr;
543 puts ("Fit image detected...\n");
545 cnt = fit_get_size (fit_hdr);
549 show_boot_progress (-57);
550 puts ("** Unknown image type\n");
553 show_boot_progress (57);
555 r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
557 puts("** Read error\n");
558 show_boot_progress (-58);
561 show_boot_progress (58);
563 #if defined(CONFIG_FIT)
564 /* This cannot be done earlier, we need complete FIT image in RAM first */
565 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
566 if (!fit_check_format (fit_hdr)) {
567 show_boot_progress (-150);
568 puts ("** Bad FIT image format\n");
571 show_boot_progress (151);
572 fit_print_contents (fit_hdr);
576 /* Loading ok, update default load address */
580 /* Check if we should attempt an auto-start */
581 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
583 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
586 local_args[1] = NULL;
588 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
590 do_bootm(cmdtp, 0, 1, local_args);
596 int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
598 char *boot_device = NULL;
600 ulong addr, offset = 0;
601 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_CMD_MTDPARTS)
602 struct mtd_device *dev;
603 struct part_info *part;
607 char *p = (argc == 2) ? argv[1] : argv[2];
608 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
609 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
610 if (dev->id->type != MTD_DEV_TYPE_NAND) {
611 puts("Not a NAND device\n");
617 addr = simple_strtoul(argv[1], NULL, 16);
619 addr = CONFIG_SYS_LOAD_ADDR;
620 return nand_load_image(cmdtp, &nand_info[dev->id->num],
621 part->offset, addr, argv[0]);
626 show_boot_progress(52);
629 addr = CONFIG_SYS_LOAD_ADDR;
630 boot_device = getenv("bootdevice");
633 addr = simple_strtoul(argv[1], NULL, 16);
634 boot_device = getenv("bootdevice");
637 addr = simple_strtoul(argv[1], NULL, 16);
638 boot_device = argv[2];
641 addr = simple_strtoul(argv[1], NULL, 16);
642 boot_device = argv[2];
643 offset = simple_strtoul(argv[3], NULL, 16);
646 #if defined(CONFIG_CMD_JFFS2) && defined(CONFIG_CMD_MTDPARTS)
650 show_boot_progress(-53);
654 show_boot_progress(53);
656 puts("\n** No boot device **\n");
657 show_boot_progress(-54);
660 show_boot_progress(54);
662 idx = simple_strtoul(boot_device, NULL, 16);
664 if (idx < 0 || idx >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[idx].name) {
665 printf("\n** Device %d not available\n", idx);
666 show_boot_progress(-55);
669 show_boot_progress(55);
671 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
674 U_BOOT_CMD(nboot, 4, 1, do_nandboot,
675 "boot from NAND device",
676 "[partition] | [[[loadAddr] dev] offset]\n");
680 #else /* CONFIG_NAND_LEGACY */
683 * Legacy NAND support - to be phased out
689 #include <watchdog.h>
691 #ifdef CONFIG_show_boot_progress
692 # include <status_led.h>
693 # define show_boot_progress(arg) show_boot_progress(arg)
695 # define show_boot_progress(arg)
698 #if defined(CONFIG_CMD_NAND)
699 #include <linux/mtd/nand_legacy.h>
701 #include <linux/mtd/nand_ids.h>
702 #include <jffs2/jffs2.h>
705 #ifdef CONFIG_OMAP1510
706 void archflashwp(void *archdata, int wp);
709 #define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
714 /* ****************** WARNING *********************
715 * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
716 * erase (or at least attempt to erase) blocks that are marked
717 * bad. This can be very handy if you are _sure_ that the block
718 * is OK, say because you marked a good block bad to test bad
719 * block handling and you are done testing, or if you have
720 * accidentally marked blocks bad.
722 * Erasing factory marked bad blocks is a _bad_ idea. If the
723 * erase succeeds there is no reliable way to find them again,
724 * and attempting to program or erase bad blocks can affect
725 * the data in _other_ (good) blocks.
727 #define ALLOW_ERASE_BAD_DEBUG 0
729 #define CONFIG_MTD_NAND_ECC /* enable ECC */
730 #define CONFIG_MTD_NAND_ECC_JFFS2
732 /* bits for nand_legacy_rw() `cmd'; or together as needed */
733 #define NANDRW_READ 0x01
734 #define NANDRW_WRITE 0x00
735 #define NANDRW_JFFS2 0x02
736 #define NANDRW_JFFS2_SKIP 0x04
739 * Imports from nand_legacy.c
741 extern struct nand_chip nand_dev_desc[CONFIG_SYS_MAX_NAND_DEVICE];
742 extern int curr_device;
743 extern int nand_legacy_erase(struct nand_chip *nand, size_t ofs,
744 size_t len, int clean);
745 extern int nand_legacy_rw(struct nand_chip *nand, int cmd, size_t start,
746 size_t len, size_t *retlen, u_char *buf);
747 extern void nand_print(struct nand_chip *nand);
748 extern void nand_print_bad(struct nand_chip *nand);
749 extern int nand_read_oob(struct nand_chip *nand, size_t ofs,
750 size_t len, size_t *retlen, u_char *buf);
751 extern int nand_write_oob(struct nand_chip *nand, size_t ofs,
752 size_t len, size_t *retlen, const u_char *buf);
755 int do_nand (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
765 if (strcmp (argv[1], "info") == 0) {
770 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; ++i) {
771 if (nand_dev_desc[i].ChipID ==
773 continue; /* list only known devices */
774 printf ("Device %d: ", i);
775 nand_print (&nand_dev_desc[i]);
779 } else if (strcmp (argv[1], "device") == 0) {
780 if ((curr_device < 0)
781 || (curr_device >= CONFIG_SYS_MAX_NAND_DEVICE)) {
782 puts ("\nno devices available\n");
785 printf ("\nDevice %d: ", curr_device);
786 nand_print (&nand_dev_desc[curr_device]);
789 } else if (strcmp (argv[1], "bad") == 0) {
790 if ((curr_device < 0)
791 || (curr_device >= CONFIG_SYS_MAX_NAND_DEVICE)) {
792 puts ("\nno devices available\n");
795 printf ("\nDevice %d bad blocks:\n", curr_device);
796 nand_print_bad (&nand_dev_desc[curr_device]);
803 if (strcmp (argv[1], "device") == 0) {
804 int dev = (int) simple_strtoul (argv[2], NULL, 10);
806 printf ("\nDevice %d: ", dev);
807 if (dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
808 puts ("unknown device\n");
811 nand_print (&nand_dev_desc[dev]);
812 /*nand_print (dev); */
814 if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
820 puts ("... is now current device\n");
823 } else if (strcmp (argv[1], "erase") == 0
824 && strcmp (argv[2], "clean") == 0) {
825 struct nand_chip *nand = &nand_dev_desc[curr_device];
827 ulong size = nand->totlen;
830 printf ("\nNAND erase: device %d offset %ld, size %ld ... ", curr_device, off, size);
832 ret = nand_legacy_erase (nand, off, size, 1);
834 printf ("%s\n", ret ? "ERROR" : "OK");
842 /* at least 4 args */
844 if (strncmp (argv[1], "read", 4) == 0 ||
845 strncmp (argv[1], "write", 5) == 0) {
846 ulong addr = simple_strtoul (argv[2], NULL, 16);
847 off_t off = simple_strtoul (argv[3], NULL, 16);
848 size_t size = simple_strtoul (argv[4], NULL, 16);
849 int cmd = (strncmp (argv[1], "read", 4) == 0) ?
850 NANDRW_READ : NANDRW_WRITE;
853 char *cmdtail = strchr (argv[1], '.');
855 if (cmdtail && !strncmp (cmdtail, ".oob", 2)) {
856 /* read out-of-band data */
857 if (cmd & NANDRW_READ) {
858 ret = nand_read_oob (nand_dev_desc + curr_device,
862 ret = nand_write_oob (nand_dev_desc + curr_device,
867 } else if (cmdtail && !strncmp (cmdtail, ".jffs2s", 7)) {
868 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
869 if (cmd & NANDRW_READ)
870 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
871 } else if (cmdtail && !strncmp (cmdtail, ".jffs2", 2))
872 cmd |= NANDRW_JFFS2; /* skip bad blocks */
874 /* need ".e" same as ".j" for compatibility with older units */
875 else if (cmdtail && !strcmp (cmdtail, ".e"))
876 cmd |= NANDRW_JFFS2; /* skip bad blocks */
878 #ifdef CONFIG_SYS_NAND_SKIP_BAD_DOT_I
879 /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
880 /* ".i" for image -> read skips bad block (no 0xff) */
881 else if (cmdtail && !strcmp (cmdtail, ".i")) {
882 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
883 if (cmd & NANDRW_READ)
884 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
886 #endif /* CONFIG_SYS_NAND_SKIP_BAD_DOT_I */
892 printf ("\nNAND %s: device %d offset %ld, size %lu ...\n",
893 (cmd & NANDRW_READ) ? "read" : "write",
894 curr_device, off, (ulong)size);
896 ret = nand_legacy_rw (nand_dev_desc + curr_device,
898 &total, (u_char *) addr);
900 printf (" %d bytes %s: %s\n", total,
901 (cmd & NANDRW_READ) ? "read" : "written",
902 ret ? "ERROR" : "OK");
905 } else if (strcmp (argv[1], "erase") == 0 &&
906 (argc == 4 || strcmp ("clean", argv[2]) == 0)) {
907 int clean = argc == 5;
909 simple_strtoul (argv[2 + clean], NULL, 16);
911 simple_strtoul (argv[3 + clean], NULL, 16);
914 printf ("\nNAND erase: device %d offset %ld, size %ld ...\n",
915 curr_device, off, size);
917 ret = nand_legacy_erase (nand_dev_desc + curr_device,
920 printf ("%s\n", ret ? "ERROR" : "OK");
934 "legacy NAND sub-system",
935 "info - show available NAND devices\n"
936 "nand device [dev] - show or set current device\n"
937 "nand read[.jffs2[s]] addr off size\n"
938 "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
939 " at offset `off' to/from memory address `addr'\n"
940 "nand erase [clean] [off size] - erase `size' bytes from\n"
941 " offset `off' (entire device if not specified)\n"
942 "nand bad - show bad blocks\n"
943 "nand read.oob addr off size - read out-of-band data\n"
944 "nand write.oob addr off size - read out-of-band data\n"
947 int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
949 char *boot_device = NULL;
957 #if defined(CONFIG_FIT)
958 const void *fit_hdr = NULL;
961 show_boot_progress (52);
964 addr = CONFIG_SYS_LOAD_ADDR;
965 boot_device = getenv ("bootdevice");
968 addr = simple_strtoul(argv[1], NULL, 16);
969 boot_device = getenv ("bootdevice");
972 addr = simple_strtoul(argv[1], NULL, 16);
973 boot_device = argv[2];
976 addr = simple_strtoul(argv[1], NULL, 16);
977 boot_device = argv[2];
978 offset = simple_strtoul(argv[3], NULL, 16);
982 show_boot_progress (-53);
986 show_boot_progress (53);
988 puts ("\n** No boot device **\n");
989 show_boot_progress (-54);
992 show_boot_progress (54);
994 dev = simple_strtoul(boot_device, &ep, 16);
996 if ((dev >= CONFIG_SYS_MAX_NAND_DEVICE) ||
997 (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
998 printf ("\n** Device %d not available\n", dev);
999 show_boot_progress (-55);
1002 show_boot_progress (55);
1004 printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
1005 dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
1008 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ, offset,
1009 SECTORSIZE, NULL, (u_char *)addr)) {
1010 printf ("** Read error on %d\n", dev);
1011 show_boot_progress (-56);
1014 show_boot_progress (56);
1016 switch (genimg_get_format ((void *)addr)) {
1017 case IMAGE_FORMAT_LEGACY:
1018 hdr = (image_header_t *)addr;
1019 image_print_contents (hdr);
1021 cnt = image_get_image_size (hdr);
1024 #if defined(CONFIG_FIT)
1025 case IMAGE_FORMAT_FIT:
1026 fit_hdr = (const void *)addr;
1027 puts ("Fit image detected...\n");
1029 cnt = fit_get_size (fit_hdr);
1033 show_boot_progress (-57);
1034 puts ("** Unknown image type\n");
1037 show_boot_progress (57);
1039 if (nand_legacy_rw (nand_dev_desc + dev, NANDRW_READ,
1040 offset + SECTORSIZE, cnt, NULL,
1041 (u_char *)(addr+SECTORSIZE))) {
1042 printf ("** Read error on %d\n", dev);
1043 show_boot_progress (-58);
1046 show_boot_progress (58);
1048 #if defined(CONFIG_FIT)
1049 /* This cannot be done earlier, we need complete FIT image in RAM first */
1050 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
1051 if (!fit_check_format (fit_hdr)) {
1052 show_boot_progress (-150);
1053 puts ("** Bad FIT image format\n");
1056 show_boot_progress (151);
1057 fit_print_contents (fit_hdr);
1061 /* Loading ok, update default load address */
1065 /* Check if we should attempt an auto-start */
1066 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
1067 char *local_args[2];
1068 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
1070 local_args[0] = argv[0];
1071 local_args[1] = NULL;
1073 printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
1075 do_bootm (cmdtp, 0, 1, local_args);
1082 nboot, 4, 1, do_nandboot,
1083 "boot from NAND device",
1089 #endif /* CONFIG_NAND_LEGACY */