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 * Ported 'dynenv' to 'nand env.oob' command
8 * (C) 2010 Nanometrics, Inc.
9 * 'dynenv' -- Dynamic environment offset in NAND OOB
10 * (C) Copyright 2006-2007 OpenMoko, Inc.
11 * Added 16-bit nand support
12 * (C) 2004 Texas Instruments
14 * Copyright 2010 Freescale Semiconductor
15 * The portions of this file whose copyright is held by Freescale and which
16 * are not considered a derived work of GPL v2-only code may be distributed
17 * and/or modified under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of the
19 * License, or (at your option) any later version.
23 #include <linux/mtd/mtd.h>
27 #include <asm/byteorder.h>
28 #include <jffs2/jffs2.h>
31 #if defined(CONFIG_CMD_MTDPARTS)
33 /* partition 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(nand_info_t *nand, ulong off, int only_oob)
43 u_char *datbuf, *oobbuf, *p;
45 datbuf = malloc(nand->writesize + nand->oobsize);
46 oobbuf = malloc(nand->oobsize);
47 if (!datbuf || !oobbuf) {
48 puts("No memory for page buffer\n");
51 off &= ~(nand->writesize - 1);
52 loff_t addr = (loff_t) off;
53 struct mtd_oob_ops ops;
54 memset(&ops, 0, sizeof(ops));
56 ops.oobbuf = oobbuf; /* must exist, but oob data will be appended to ops.datbuf */
57 ops.len = nand->writesize;
58 ops.ooblen = nand->oobsize;
59 ops.mode = MTD_OOB_RAW;
60 i = nand->read_oob(nand, addr, &ops);
62 printf("Error (%d) reading page %08lx\n", i, off);
67 printf("Page %08lx dump:\n", off);
68 i = nand->writesize >> 4;
73 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
74 " %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],
76 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
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 int set_dev(int dev)
97 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE ||
98 !nand_info[dev].name) {
99 puts("No such device\n");
103 if (nand_curr_device == dev)
106 printf("Device %d: %s", dev, nand_info[dev].name);
107 puts("... is now current device\n");
108 nand_curr_device = dev;
110 #ifdef CONFIG_SYS_NAND_SELECT_DEVICE
111 board_nand_select_device(nand_info[dev].priv, dev);
117 static inline int str2off(const char *p, loff_t *num)
121 *num = simple_strtoull(p, &endptr, 16);
122 return *p != '\0' && *endptr == '\0';
125 static inline int str2long(const char *p, ulong *num)
129 *num = simple_strtoul(p, &endptr, 16);
130 return *p != '\0' && *endptr == '\0';
133 static int get_part(const char *partname, int *idx, loff_t *off, loff_t *size)
135 #ifdef CONFIG_CMD_MTDPARTS
136 struct mtd_device *dev;
137 struct part_info *part;
141 ret = mtdparts_init();
145 ret = find_dev_and_part(partname, &dev, &pnum, &part);
149 if (dev->id->type != MTD_DEV_TYPE_NAND) {
150 puts("not a NAND device\n");
164 puts("offset is not a number\n");
169 static int arg_off(const char *arg, int *idx, loff_t *off, loff_t *maxsize)
171 if (!str2off(arg, off))
172 return get_part(arg, idx, off, maxsize);
174 if (*off >= nand_info[*idx].size) {
175 puts("Offset exceeds device limit\n");
179 *maxsize = nand_info[*idx].size - *off;
183 static int arg_off_size(int argc, char *const argv[], int *idx,
184 loff_t *off, loff_t *size)
191 *size = nand_info[*idx].size;
195 ret = arg_off(argv[0], idx, off, &maxsize);
204 if (!str2off(argv[1], size)) {
205 printf("'%s' is not a number\n", argv[1]);
209 if (*size > maxsize) {
210 puts("Size exceeds partition or device limit\n");
215 printf("device %d ", *idx);
216 if (*size == nand_info[*idx].size)
217 puts("whole chip\n");
219 printf("offset 0x%llx, size 0x%llx\n",
220 (unsigned long long)*off, (unsigned long long)*size);
224 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
225 static void print_status(ulong start, ulong end, ulong erasesize, int status)
227 printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
230 (end - start) / erasesize,
231 ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
232 ((status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
233 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
236 static void do_nand_status(nand_info_t *nand)
238 ulong block_start = 0;
240 int last_status = -1;
242 struct nand_chip *nand_chip = nand->priv;
243 /* check the WP bit */
244 nand_chip->cmdfunc(nand, NAND_CMD_STATUS, -1, -1);
245 printf("device is %swrite protected\n",
246 (nand_chip->read_byte(nand) & 0x80 ?
249 for (off = 0; off < nand->size; off += nand->erasesize) {
250 int s = nand_get_lock_status(nand, off);
252 /* print message only if status has changed */
253 if (s != last_status && off != 0) {
254 print_status(block_start, off, nand->erasesize,
260 /* Print the last block info */
261 print_status(block_start, off, nand->erasesize, last_status);
265 #ifdef CONFIG_ENV_OFFSET_OOB
266 unsigned long nand_env_oob_offset;
268 int do_nand_env_oob(cmd_tbl_t *cmdtp, int argc, char *const argv[])
271 uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
272 nand_info_t *nand = &nand_info[0];
275 if (CONFIG_SYS_MAX_NAND_DEVICE == 0 || !nand->name) {
276 puts("no devices available\n");
282 if (!strcmp(cmd, "get")) {
283 ret = get_nand_env_oob(nand, &nand_env_oob_offset);
287 printf("0x%08lx\n", nand_env_oob_offset);
288 } else if (!strcmp(cmd, "set")) {
291 struct mtd_oob_ops ops;
297 if (arg_off(argv[2], &idx, &addr, &maxsize)) {
298 puts("Offset or partition name expected\n");
303 puts("Partition not on first NAND device\n");
307 if (nand->oobavail < ENV_OFFSET_SIZE) {
308 printf("Insufficient available OOB bytes:\n"
309 "%d OOB bytes available but %d required for "
311 nand->oobavail, ENV_OFFSET_SIZE);
315 if ((addr & (nand->erasesize - 1)) != 0) {
316 printf("Environment offset must be block-aligned\n");
321 ops.mode = MTD_OOB_AUTO;
323 ops.ooblen = ENV_OFFSET_SIZE;
324 ops.oobbuf = (void *) oob_buf;
326 oob_buf[0] = ENV_OOB_MARKER;
327 oob_buf[1] = addr / nand->erasesize;
329 ret = nand->write_oob(nand, ENV_OFFSET_SIZE, &ops);
331 printf("Error writing OOB block 0\n");
335 ret = get_nand_env_oob(nand, &nand_env_oob_offset);
337 printf("Error reading env offset in OOB\n");
341 if (addr != nand_env_oob_offset) {
342 printf("Verification of env offset in OOB failed: "
343 "0x%08llx expected but got 0x%08lx\n",
344 (unsigned long long)addr, nand_env_oob_offset);
354 return cmd_usage(cmdtp);
359 static void nand_print_info(int idx)
361 nand_info_t *nand = &nand_info[idx];
362 struct nand_chip *chip = nand->priv;
363 printf("Device %d: ", idx);
364 if (chip->numchips > 1)
365 printf("%dx ", chip->numchips);
366 printf("%s, sector size %u KiB\n",
367 nand->name, nand->erasesize >> 10);
370 int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
377 #ifdef CONFIG_SYS_NAND_QUIET
378 int quiet = CONFIG_SYS_NAND_QUIET;
382 const char *quiet_str = getenv("quiet");
383 int dev = nand_curr_device;
385 /* at least two arguments please */
390 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
394 if (strcmp(cmd, "info") == 0) {
397 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
398 if (nand_info[i].name)
404 if (strcmp(cmd, "device") == 0) {
407 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE)
408 puts("no devices available\n");
410 nand_print_info(dev);
414 dev = (int)simple_strtoul(argv[2], NULL, 10);
420 #ifdef CONFIG_ENV_OFFSET_OOB
421 /* this command operates only on the first nand device */
422 if (strcmp(cmd, "env.oob") == 0)
423 return do_nand_env_oob(cmdtp, argc - 1, argv + 1);
426 /* The following commands operate on the current device, unless
427 * overridden by a partition specifier. Note that if somehow the
428 * current device is invalid, it will have to be changed to a valid
429 * one before these commands can run, even if a partition specifier
430 * for another device is to be used.
432 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE ||
433 !nand_info[dev].name) {
434 puts("\nno devices available\n");
437 nand = &nand_info[dev];
439 if (strcmp(cmd, "bad") == 0) {
440 printf("\nDevice %d bad blocks:\n", dev);
441 for (off = 0; off < nand->size; off += nand->erasesize)
442 if (nand_block_isbad(nand, off))
443 printf(" %08llx\n", (unsigned long long)off);
450 * nand erase [clean] [off size]
452 if (strncmp(cmd, "erase", 5) == 0 || strncmp(cmd, "scrub", 5) == 0) {
453 nand_erase_options_t opts;
454 /* "clean" at index 2 means request to write cleanmarker */
455 int clean = argc > 2 && !strcmp("clean", argv[2]);
456 int o = clean ? 3 : 2;
457 int scrub = !strncmp(cmd, "scrub", 5);
464 if (!strcmp(&cmd[5], ".spread")) {
466 } else if (!strcmp(&cmd[5], ".part")) {
469 } else if (!strcmp(&cmd[5], ".chip")) {
478 * Don't allow missing arguments to cause full chip/partition
479 * erases -- easy to do accidentally, e.g. with a misspelled
482 if (argc != o + args)
485 printf("\nNAND %s: ", cmd);
486 /* skip first two or three arguments, look for offset and size */
487 if (arg_off_size(argc - o, argv + o, &dev, &off, &size) != 0)
490 nand = &nand_info[dev];
492 memset(&opts, 0, sizeof(opts));
497 opts.spread = spread;
501 "scrub option will erase all factory set "
504 "There is no reliable way to recover them.\n"
506 "Use this command only for testing purposes "
509 "are sure of what you are doing!\n"
510 "\nReally scrub this NAND flash? <y/N>\n");
517 puts("scrub aborted\n");
521 puts("scrub aborted\n");
525 ret = nand_erase_opts(nand, &opts);
526 printf("%s\n", ret ? "ERROR" : "OK");
528 return ret == 0 ? 0 : 1;
531 if (strncmp(cmd, "dump", 4) == 0) {
535 s = strchr(cmd, '.');
536 off = (int)simple_strtoul(argv[2], NULL, 16);
538 if (s != NULL && strcmp(s, ".oob") == 0)
539 ret = nand_dump(nand, off, 1);
541 ret = nand_dump(nand, off, 0);
543 return ret == 0 ? 1 : 0;
547 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
554 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
556 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
557 printf("\nNAND %s: ", read ? "read" : "write");
558 if (arg_off_size(argc - 3, argv + 3, &dev, &off, &size) != 0)
561 nand = &nand_info[dev];
564 s = strchr(cmd, '.');
565 if (!s || !strcmp(s, ".jffs2") ||
566 !strcmp(s, ".e") || !strcmp(s, ".i")) {
568 ret = nand_read_skip_bad(nand, off, &rwsize,
571 ret = nand_write_skip_bad(nand, off, &rwsize,
573 } else if (!strcmp(s, ".oob")) {
574 /* out-of-band data */
575 mtd_oob_ops_t ops = {
576 .oobbuf = (u8 *)addr,
582 ret = nand->read_oob(nand, off, &ops);
584 ret = nand->write_oob(nand, off, &ops);
586 printf("Unknown nand command suffix '%s'.\n", s);
590 printf(" %zu bytes %s: %s\n", rwsize,
591 read ? "read" : "written", ret ? "ERROR" : "OK");
593 return ret == 0 ? 0 : 1;
596 if (strcmp(cmd, "markbad") == 0) {
604 addr = simple_strtoul(*argv, NULL, 16);
606 if (nand->block_markbad(nand, addr)) {
607 printf("block 0x%08lx NOT marked "
608 "as bad! ERROR %d\n",
612 printf("block 0x%08lx successfully "
622 if (strcmp(cmd, "biterr") == 0) {
627 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
628 if (strcmp(cmd, "lock") == 0) {
632 if (!strcmp("tight", argv[2]))
634 if (!strcmp("status", argv[2]))
638 do_nand_status(nand);
640 if (!nand_lock(nand, tight)) {
641 puts("NAND flash successfully locked\n");
643 puts("Error locking NAND flash\n");
650 if (strcmp(cmd, "unlock") == 0) {
651 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
654 if (!nand_unlock(&nand_info[dev], off, size)) {
655 puts("NAND flash successfully unlocked\n");
657 puts("Error unlocking NAND flash, "
658 "write and erase will probably fail\n");
666 return cmd_usage(cmdtp);
670 nand, CONFIG_SYS_MAXARGS, 1, do_nand,
672 "info - show available NAND devices\n"
673 "nand device [dev] - show or set current device\n"
674 "nand read - addr off|partition size\n"
675 "nand write - addr off|partition size\n"
676 " read/write 'size' bytes starting at offset 'off'\n"
677 " to/from memory address 'addr', skipping bad blocks.\n"
678 "nand erase[.spread] [clean] [off [size]] - erase 'size' bytes "
679 "from offset 'off'\n"
680 " With '.spread', erase enough for given file size, otherwise,\n"
681 " 'size' includes skipped bad blocks.\n"
682 "nand erase.part [clean] partition - erase entire mtd partition'\n"
683 "nand erase.chip [clean] - erase entire chip'\n"
684 "nand bad - show bad blocks\n"
685 "nand dump[.oob] off - dump page\n"
686 "nand scrub off size | scrub.part partition | scrub.chip\n"
687 " really clean NAND erasing bad blocks (UNSAFE)\n"
688 "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
689 "nand biterr off - make a bit error at offset (UNSAFE)"
690 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
692 "nand lock [tight] [status]\n"
693 " bring nand to lock state or display locked pages\n"
694 "nand unlock [offset] [size] - unlock section"
696 #ifdef CONFIG_ENV_OFFSET_OOB
698 "nand env.oob - environment offset in OOB of block 0 of"
700 "nand env.oob set off|partition - set enviromnent offset\n"
701 "nand env.oob get - get environment offset"
705 static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
706 ulong offset, ulong addr, char *cmd)
712 #if defined(CONFIG_FIT)
713 const void *fit_hdr = NULL;
716 s = strchr(cmd, '.');
718 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
719 printf("Unknown nand load suffix '%s'\n", s);
720 show_boot_progress(-53);
724 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
726 cnt = nand->writesize;
727 r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
729 puts("** Read error\n");
730 show_boot_progress (-56);
733 show_boot_progress (56);
735 switch (genimg_get_format ((void *)addr)) {
736 case IMAGE_FORMAT_LEGACY:
737 hdr = (image_header_t *)addr;
739 show_boot_progress (57);
740 image_print_contents (hdr);
742 cnt = image_get_image_size (hdr);
744 #if defined(CONFIG_FIT)
745 case IMAGE_FORMAT_FIT:
746 fit_hdr = (const void *)addr;
747 puts ("Fit image detected...\n");
749 cnt = fit_get_size (fit_hdr);
753 show_boot_progress (-57);
754 puts ("** Unknown image type\n");
757 show_boot_progress (57);
759 r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
761 puts("** Read error\n");
762 show_boot_progress (-58);
765 show_boot_progress (58);
767 #if defined(CONFIG_FIT)
768 /* This cannot be done earlier, we need complete FIT image in RAM first */
769 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
770 if (!fit_check_format (fit_hdr)) {
771 show_boot_progress (-150);
772 puts ("** Bad FIT image format\n");
775 show_boot_progress (151);
776 fit_print_contents (fit_hdr);
780 /* Loading ok, update default load address */
784 /* Check if we should attempt an auto-start */
785 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
787 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
790 local_args[1] = NULL;
792 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
794 do_bootm(cmdtp, 0, 1, local_args);
800 int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
802 char *boot_device = NULL;
804 ulong addr, offset = 0;
805 #if defined(CONFIG_CMD_MTDPARTS)
806 struct mtd_device *dev;
807 struct part_info *part;
811 char *p = (argc == 2) ? argv[1] : argv[2];
812 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
813 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
814 if (dev->id->type != MTD_DEV_TYPE_NAND) {
815 puts("Not a NAND device\n");
821 addr = simple_strtoul(argv[1], NULL, 16);
823 addr = CONFIG_SYS_LOAD_ADDR;
824 return nand_load_image(cmdtp, &nand_info[dev->id->num],
825 part->offset, addr, argv[0]);
830 show_boot_progress(52);
833 addr = CONFIG_SYS_LOAD_ADDR;
834 boot_device = getenv("bootdevice");
837 addr = simple_strtoul(argv[1], NULL, 16);
838 boot_device = getenv("bootdevice");
841 addr = simple_strtoul(argv[1], NULL, 16);
842 boot_device = argv[2];
845 addr = simple_strtoul(argv[1], NULL, 16);
846 boot_device = argv[2];
847 offset = simple_strtoul(argv[3], NULL, 16);
850 #if defined(CONFIG_CMD_MTDPARTS)
853 show_boot_progress(-53);
854 return cmd_usage(cmdtp);
857 show_boot_progress(53);
859 puts("\n** No boot device **\n");
860 show_boot_progress(-54);
863 show_boot_progress(54);
865 idx = simple_strtoul(boot_device, NULL, 16);
867 if (idx < 0 || idx >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[idx].name) {
868 printf("\n** Device %d not available\n", idx);
869 show_boot_progress(-55);
872 show_boot_progress(55);
874 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
877 U_BOOT_CMD(nboot, 4, 1, do_nandboot,
878 "boot from NAND device",
879 "[partition] | [[[loadAddr] dev] offset]"