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
16 #include <linux/mtd/mtd.h>
20 #include <asm/byteorder.h>
21 #include <jffs2/jffs2.h>
24 #if defined(CONFIG_CMD_MTDPARTS)
26 /* partition handling routines */
27 int mtdparts_init(void);
28 int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
29 int find_dev_and_part(const char *id, struct mtd_device **dev,
30 u8 *part_num, struct part_info **part);
33 static int nand_dump(nand_info_t *nand, ulong off, int only_oob)
36 u_char *datbuf, *oobbuf, *p;
38 datbuf = malloc(nand->writesize + nand->oobsize);
39 oobbuf = malloc(nand->oobsize);
40 if (!datbuf || !oobbuf) {
41 puts("No memory for page buffer\n");
44 off &= ~(nand->writesize - 1);
45 loff_t addr = (loff_t) off;
46 struct mtd_oob_ops ops;
47 memset(&ops, 0, sizeof(ops));
49 ops.oobbuf = oobbuf; /* must exist, but oob data will be appended to ops.datbuf */
50 ops.len = nand->writesize;
51 ops.ooblen = nand->oobsize;
52 ops.mode = MTD_OOB_RAW;
53 i = nand->read_oob(nand, addr, &ops);
55 printf("Error (%d) reading page %08lx\n", i, off);
60 printf("Page %08lx dump:\n", off);
61 i = nand->writesize >> 4;
66 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
67 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
68 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
69 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
74 i = nand->oobsize >> 3;
76 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
77 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
86 /* ------------------------------------------------------------------------- */
88 static inline int str2long(char *p, ulong *num)
92 *num = simple_strtoul(p, &endptr, 16);
93 return (*p != '\0' && *endptr == '\0') ? 1 : 0;
97 arg_off_size(int argc, char * const argv[], nand_info_t *nand, ulong *off, size_t *size)
99 int idx = nand_curr_device;
100 #if defined(CONFIG_CMD_MTDPARTS)
101 struct mtd_device *dev;
102 struct part_info *part;
105 if (argc >= 1 && !(str2long(argv[0], off))) {
106 if ((mtdparts_init() == 0) &&
107 (find_dev_and_part(argv[0], &dev, &pnum, &part) == 0)) {
108 if (dev->id->type != MTD_DEV_TYPE_NAND) {
109 puts("not a NAND device\n");
114 if (!(str2long(argv[1], (ulong *)size))) {
115 printf("'%s' is not a number\n", argv[1]);
118 if (*size > part->size)
124 *nand = nand_info[idx];
131 if (!(str2long(argv[0], off))) {
132 printf("'%s' is not a number\n", argv[0]);
140 if (!(str2long(argv[1], (ulong *)size))) {
141 printf("'%s' is not a number\n", argv[1]);
145 *size = nand->size - *off;
148 #if defined(CONFIG_CMD_MTDPARTS)
151 printf("device %d ", idx);
152 if (*size == nand->size)
153 puts("whole chip\n");
155 printf("offset 0x%lx, size 0x%zx\n", *off, *size);
159 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
160 static void print_status(ulong start, ulong end, ulong erasesize, int status)
162 printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
165 (end - start) / erasesize,
166 ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
167 ((status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
168 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
171 static void do_nand_status(nand_info_t *nand)
173 ulong block_start = 0;
175 int last_status = -1;
177 struct nand_chip *nand_chip = nand->priv;
178 /* check the WP bit */
179 nand_chip->cmdfunc(nand, NAND_CMD_STATUS, -1, -1);
180 printf("device is %swrite protected\n",
181 (nand_chip->read_byte(nand) & 0x80 ?
184 for (off = 0; off < nand->size; off += nand->erasesize) {
185 int s = nand_get_lock_status(nand, off);
187 /* print message only if status has changed */
188 if (s != last_status && off != 0) {
189 print_status(block_start, off, nand->erasesize,
195 /* Print the last block info */
196 print_status(block_start, off, nand->erasesize, last_status);
200 #ifdef CONFIG_ENV_OFFSET_OOB
201 unsigned long nand_env_oob_offset;
203 int do_nand_env_oob(cmd_tbl_t *cmdtp, nand_info_t *nand,
204 int argc, char * const argv[])
207 uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
211 if (!strcmp(cmd, "get")) {
212 ret = get_nand_env_oob(nand, &nand_env_oob_offset);
216 printf("0x%08lx\n", nand_env_oob_offset);
217 } else if (!strcmp(cmd, "set")) {
220 struct mtd_oob_ops ops;
225 if (arg_off_size(argc - 2, argv + 2, nand, &addr,
227 printf("Offset or partition name expected\n");
231 if (nand->oobavail < ENV_OFFSET_SIZE) {
232 printf("Insufficient available OOB bytes:\n"
233 "%d OOB bytes available but %d required for "
235 nand->oobavail, ENV_OFFSET_SIZE);
239 if ((addr & (nand->erasesize - 1)) != 0) {
240 printf("Environment offset must be block-aligned\n");
245 ops.mode = MTD_OOB_AUTO;
247 ops.ooblen = ENV_OFFSET_SIZE;
248 ops.oobbuf = (void *) oob_buf;
250 oob_buf[0] = ENV_OOB_MARKER;
251 oob_buf[1] = addr / nand->erasesize;
253 ret = nand->write_oob(nand, ENV_OFFSET_SIZE, &ops);
255 printf("Error writing OOB block 0\n");
259 ret = get_nand_env_oob(nand, &nand_env_oob_offset);
261 printf("Error reading env offset in OOB\n");
265 if (addr != nand_env_oob_offset) {
266 printf("Verification of env offset in OOB failed: "
267 "0x%08lx expected but got 0x%08lx\n",
268 addr, nand_env_oob_offset);
278 return cmd_usage(cmdtp);
283 static void nand_print_info(int idx)
285 nand_info_t *nand = &nand_info[idx];
286 struct nand_chip *chip = nand->priv;
287 printf("Device %d: ", idx);
288 if (chip->numchips > 1)
289 printf("%dx ", chip->numchips);
290 printf("%s, sector size %u KiB\n",
291 nand->name, nand->erasesize >> 10);
294 int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
301 #ifdef CONFIG_SYS_NAND_QUIET
302 int quiet = CONFIG_SYS_NAND_QUIET;
306 const char *quiet_str = getenv("quiet");
308 /* at least two arguments please */
313 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
317 if (strcmp(cmd, "info") == 0) {
320 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
321 if (nand_info[i].name)
327 if (strcmp(cmd, "device") == 0) {
331 if ((nand_curr_device < 0) ||
332 (nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE))
333 puts("no devices available\n");
335 nand_print_info(nand_curr_device);
338 dev = (int)simple_strtoul(argv[2], NULL, 10);
339 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev].name) {
340 puts("No such device\n");
343 printf("Device %d: %s", dev, nand_info[dev].name);
344 puts("... is now current device\n");
345 nand_curr_device = dev;
347 #ifdef CONFIG_SYS_NAND_SELECT_DEVICE
349 * Select the chip in the board/cpu specific driver
351 board_nand_select_device(nand_info[dev].priv, dev);
357 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
358 strncmp(cmd, "dump", 4) != 0 &&
359 strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
360 strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
361 strcmp(cmd, "biterr") != 0 &&
362 strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0
363 #ifdef CONFIG_ENV_OFFSET_OOB
364 && strcmp(cmd, "env.oob") != 0
369 #ifdef CONFIG_ENV_OFFSET_OOB
370 /* this command operates only on the first nand device */
371 if (strcmp(cmd, "env.oob") == 0) {
372 return do_nand_env_oob(cmdtp, &nand_info[0],
377 /* the following commands operate on the current device */
378 if (nand_curr_device < 0 || nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
379 !nand_info[nand_curr_device].name) {
380 puts("\nno devices available\n");
383 nand = &nand_info[nand_curr_device];
385 if (strcmp(cmd, "bad") == 0) {
386 printf("\nDevice %d bad blocks:\n", nand_curr_device);
387 for (off = 0; off < nand->size; off += nand->erasesize)
388 if (nand_block_isbad(nand, off))
389 printf(" %08lx\n", off);
396 * nand erase [clean] [off size]
398 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
399 nand_erase_options_t opts;
400 /* "clean" at index 2 means request to write cleanmarker */
401 int clean = argc > 2 && !strcmp("clean", argv[2]);
402 int o = clean ? 3 : 2;
403 int scrub = !strcmp(cmd, "scrub");
405 printf("\nNAND %s: ", scrub ? "scrub" : "erase");
406 /* skip first two or three arguments, look for offset and size */
407 if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
410 memset(&opts, 0, sizeof(opts));
418 "scrub option will erase all factory set "
421 "There is no reliable way to recover them.\n"
423 "Use this command only for testing purposes "
426 "are sure of what you are doing!\n"
427 "\nReally scrub this NAND flash? <y/N>\n");
434 puts("scrub aborted\n");
438 puts("scrub aborted\n");
442 ret = nand_erase_opts(nand, &opts);
443 printf("%s\n", ret ? "ERROR" : "OK");
445 return ret == 0 ? 0 : 1;
448 if (strncmp(cmd, "dump", 4) == 0) {
452 s = strchr(cmd, '.');
453 off = (int)simple_strtoul(argv[2], NULL, 16);
455 if (s != NULL && strcmp(s, ".oob") == 0)
456 ret = nand_dump(nand, off, 1);
458 ret = nand_dump(nand, off, 0);
460 return ret == 0 ? 1 : 0;
464 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
470 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
472 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
473 printf("\nNAND %s: ", read ? "read" : "write");
474 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
477 s = strchr(cmd, '.');
478 if (!s || !strcmp(s, ".jffs2") ||
479 !strcmp(s, ".e") || !strcmp(s, ".i")) {
481 ret = nand_read_skip_bad(nand, off, &size,
484 ret = nand_write_skip_bad(nand, off, &size,
486 } else if (!strcmp(s, ".oob")) {
487 /* out-of-band data */
488 mtd_oob_ops_t ops = {
489 .oobbuf = (u8 *)addr,
495 ret = nand->read_oob(nand, off, &ops);
497 ret = nand->write_oob(nand, off, &ops);
499 printf("Unknown nand command suffix '%s'.\n", s);
503 printf(" %zu bytes %s: %s\n", size,
504 read ? "read" : "written", ret ? "ERROR" : "OK");
506 return ret == 0 ? 0 : 1;
509 if (strcmp(cmd, "markbad") == 0) {
517 addr = simple_strtoul(*argv, NULL, 16);
519 if (nand->block_markbad(nand, addr)) {
520 printf("block 0x%08lx NOT marked "
521 "as bad! ERROR %d\n",
525 printf("block 0x%08lx successfully "
535 if (strcmp(cmd, "biterr") == 0) {
540 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
541 if (strcmp(cmd, "lock") == 0) {
545 if (!strcmp("tight", argv[2]))
547 if (!strcmp("status", argv[2]))
551 do_nand_status(nand);
553 if (!nand_lock(nand, tight)) {
554 puts("NAND flash successfully locked\n");
556 puts("Error locking NAND flash\n");
563 if (strcmp(cmd, "unlock") == 0) {
564 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
567 if (!nand_unlock(nand, off, size)) {
568 puts("NAND flash successfully unlocked\n");
570 puts("Error unlocking NAND flash, "
571 "write and erase will probably fail\n");
579 return cmd_usage(cmdtp);
583 nand, CONFIG_SYS_MAXARGS, 1, do_nand,
585 "info - show available NAND devices\n"
586 "nand device [dev] - show or set current device\n"
587 "nand read - addr off|partition size\n"
588 "nand write - addr off|partition size\n"
589 " read/write 'size' bytes starting at offset 'off'\n"
590 " to/from memory address 'addr', skipping bad blocks.\n"
591 "nand erase [clean] [off size] - erase 'size' bytes from\n"
592 " offset 'off' (entire device if not specified)\n"
593 "nand bad - show bad blocks\n"
594 "nand dump[.oob] off - dump page\n"
595 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
596 "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
597 "nand biterr off - make a bit error at offset (UNSAFE)"
598 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
600 "nand lock [tight] [status]\n"
601 " bring nand to lock state or display locked pages\n"
602 "nand unlock [offset] [size] - unlock section"
604 #ifdef CONFIG_ENV_OFFSET_OOB
606 "nand env.oob - environment offset in OOB of block 0 of"
608 "nand env.oob set off|partition - set enviromnent offset\n"
609 "nand env.oob get - get environment offset"
613 static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
614 ulong offset, ulong addr, char *cmd)
620 #if defined(CONFIG_FIT)
621 const void *fit_hdr = NULL;
624 s = strchr(cmd, '.');
626 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
627 printf("Unknown nand load suffix '%s'\n", s);
628 show_boot_progress(-53);
632 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
634 cnt = nand->writesize;
635 r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
637 puts("** Read error\n");
638 show_boot_progress (-56);
641 show_boot_progress (56);
643 switch (genimg_get_format ((void *)addr)) {
644 case IMAGE_FORMAT_LEGACY:
645 hdr = (image_header_t *)addr;
647 show_boot_progress (57);
648 image_print_contents (hdr);
650 cnt = image_get_image_size (hdr);
652 #if defined(CONFIG_FIT)
653 case IMAGE_FORMAT_FIT:
654 fit_hdr = (const void *)addr;
655 puts ("Fit image detected...\n");
657 cnt = fit_get_size (fit_hdr);
661 show_boot_progress (-57);
662 puts ("** Unknown image type\n");
665 show_boot_progress (57);
667 r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
669 puts("** Read error\n");
670 show_boot_progress (-58);
673 show_boot_progress (58);
675 #if defined(CONFIG_FIT)
676 /* This cannot be done earlier, we need complete FIT image in RAM first */
677 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
678 if (!fit_check_format (fit_hdr)) {
679 show_boot_progress (-150);
680 puts ("** Bad FIT image format\n");
683 show_boot_progress (151);
684 fit_print_contents (fit_hdr);
688 /* Loading ok, update default load address */
692 /* Check if we should attempt an auto-start */
693 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
695 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
698 local_args[1] = NULL;
700 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
702 do_bootm(cmdtp, 0, 1, local_args);
708 int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
710 char *boot_device = NULL;
712 ulong addr, offset = 0;
713 #if defined(CONFIG_CMD_MTDPARTS)
714 struct mtd_device *dev;
715 struct part_info *part;
719 char *p = (argc == 2) ? argv[1] : argv[2];
720 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
721 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
722 if (dev->id->type != MTD_DEV_TYPE_NAND) {
723 puts("Not a NAND device\n");
729 addr = simple_strtoul(argv[1], NULL, 16);
731 addr = CONFIG_SYS_LOAD_ADDR;
732 return nand_load_image(cmdtp, &nand_info[dev->id->num],
733 part->offset, addr, argv[0]);
738 show_boot_progress(52);
741 addr = CONFIG_SYS_LOAD_ADDR;
742 boot_device = getenv("bootdevice");
745 addr = simple_strtoul(argv[1], NULL, 16);
746 boot_device = getenv("bootdevice");
749 addr = simple_strtoul(argv[1], NULL, 16);
750 boot_device = argv[2];
753 addr = simple_strtoul(argv[1], NULL, 16);
754 boot_device = argv[2];
755 offset = simple_strtoul(argv[3], NULL, 16);
758 #if defined(CONFIG_CMD_MTDPARTS)
761 show_boot_progress(-53);
762 return cmd_usage(cmdtp);
765 show_boot_progress(53);
767 puts("\n** No boot device **\n");
768 show_boot_progress(-54);
771 show_boot_progress(54);
773 idx = simple_strtoul(boot_device, NULL, 16);
775 if (idx < 0 || idx >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[idx].name) {
776 printf("\n** Device %d not available\n", idx);
777 show_boot_progress(-55);
780 show_boot_progress(55);
782 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
785 U_BOOT_CMD(nboot, 4, 1, do_nandboot,
786 "boot from NAND device",
787 "[partition] | [[[loadAddr] dev] offset]"