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
20 #include <linux/mtd/mtd.h>
22 #if defined(CONFIG_CMD_NAND)
27 #include <asm/byteorder.h>
28 #include <jffs2/jffs2.h>
31 #if defined(CONFIG_CMD_MTDPARTS)
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(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 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_MTDPARTS)
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_MTDPARTS)
158 printf("device %d ", idx);
159 if (*size == nand->size)
160 puts("whole chip\n");
162 printf("offset 0x%lx, size 0x%zx\n", *off, *size);
166 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
167 static void print_status(ulong start, ulong end, ulong erasesize, int status)
169 printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
172 (end - start) / erasesize,
173 ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
174 ((status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
175 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
178 static void do_nand_status(nand_info_t *nand)
180 ulong block_start = 0;
182 int last_status = -1;
184 struct nand_chip *nand_chip = nand->priv;
185 /* check the WP bit */
186 nand_chip->cmdfunc(nand, NAND_CMD_STATUS, -1, -1);
187 printf("device is %swrite protected\n",
188 (nand_chip->read_byte(nand) & 0x80 ?
191 for (off = 0; off < nand->size; off += nand->erasesize) {
192 int s = nand_get_lock_status(nand, off);
194 /* print message only if status has changed */
195 if (s != last_status && off != 0) {
196 print_status(block_start, off, nand->erasesize,
202 /* Print the last block info */
203 print_status(block_start, off, nand->erasesize, last_status);
207 static void nand_print_info(int idx)
209 nand_info_t *nand = &nand_info[idx];
210 struct nand_chip *chip = nand->priv;
211 printf("Device %d: ", idx);
212 if (chip->numchips > 1)
213 printf("%dx ", chip->numchips);
214 printf("%s, sector size %u KiB\n",
215 nand->name, nand->erasesize >> 10);
218 int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
225 #ifdef CONFIG_SYS_NAND_QUIET
226 int quiet = CONFIG_SYS_NAND_QUIET;
230 const char *quiet_str = getenv("quiet");
232 /* at least two arguments please */
237 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
241 if (strcmp(cmd, "info") == 0) {
244 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
245 if (nand_info[i].name)
251 if (strcmp(cmd, "device") == 0) {
255 if ((nand_curr_device < 0) ||
256 (nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE))
257 puts("no devices available\n");
259 nand_print_info(nand_curr_device);
262 dev = (int)simple_strtoul(argv[2], NULL, 10);
263 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev].name) {
264 puts("No such device\n");
267 printf("Device %d: %s", dev, nand_info[dev].name);
268 puts("... is now current device\n");
269 nand_curr_device = dev;
271 #ifdef CONFIG_SYS_NAND_SELECT_DEVICE
273 * Select the chip in the board/cpu specific driver
275 board_nand_select_device(nand_info[dev].priv, dev);
281 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
282 strncmp(cmd, "dump", 4) != 0 &&
283 strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
284 strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
285 strcmp(cmd, "biterr") != 0 &&
286 strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
289 /* the following commands operate on the current device */
290 if (nand_curr_device < 0 || nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
291 !nand_info[nand_curr_device].name) {
292 puts("\nno devices available\n");
295 nand = &nand_info[nand_curr_device];
297 if (strcmp(cmd, "bad") == 0) {
298 printf("\nDevice %d bad blocks:\n", nand_curr_device);
299 for (off = 0; off < nand->size; off += nand->erasesize)
300 if (nand_block_isbad(nand, off))
301 printf(" %08lx\n", off);
308 * nand erase [clean] [off size]
310 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
311 nand_erase_options_t opts;
312 /* "clean" at index 2 means request to write cleanmarker */
313 int clean = argc > 2 && !strcmp("clean", argv[2]);
314 int o = clean ? 3 : 2;
315 int scrub = !strcmp(cmd, "scrub");
317 printf("\nNAND %s: ", scrub ? "scrub" : "erase");
318 /* skip first two or three arguments, look for offset and size */
319 if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
322 memset(&opts, 0, sizeof(opts));
330 "scrub option will erase all factory set "
333 "There is no reliable way to recover them.\n"
335 "Use this command only for testing purposes "
338 "are sure of what you are doing!\n"
339 "\nReally scrub this NAND flash? <y/N>\n");
341 if (getc() == 'y' && getc() == '\r') {
344 puts("scrub aborted\n");
348 ret = nand_erase_opts(nand, &opts);
349 printf("%s\n", ret ? "ERROR" : "OK");
351 return ret == 0 ? 0 : 1;
354 if (strncmp(cmd, "dump", 4) == 0) {
358 s = strchr(cmd, '.');
359 off = (int)simple_strtoul(argv[2], NULL, 16);
361 if (s != NULL && strcmp(s, ".oob") == 0)
362 ret = nand_dump(nand, off, 1);
364 ret = nand_dump(nand, off, 0);
366 return ret == 0 ? 1 : 0;
370 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
376 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
378 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
379 printf("\nNAND %s: ", read ? "read" : "write");
380 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
383 s = strchr(cmd, '.');
384 if (!s || !strcmp(s, ".jffs2") ||
385 !strcmp(s, ".e") || !strcmp(s, ".i")) {
387 ret = nand_read_skip_bad(nand, off, &size,
390 ret = nand_write_skip_bad(nand, off, &size,
392 } else if (!strcmp(s, ".oob")) {
393 /* out-of-band data */
394 mtd_oob_ops_t ops = {
395 .oobbuf = (u8 *)addr,
401 ret = nand->read_oob(nand, off, &ops);
403 ret = nand->write_oob(nand, off, &ops);
405 printf("Unknown nand command suffix '%s'.\n", s);
409 printf(" %zu bytes %s: %s\n", size,
410 read ? "read" : "written", ret ? "ERROR" : "OK");
412 return ret == 0 ? 0 : 1;
415 if (strcmp(cmd, "markbad") == 0) {
423 addr = simple_strtoul(*argv, NULL, 16);
425 if (nand->block_markbad(nand, addr)) {
426 printf("block 0x%08lx NOT marked "
427 "as bad! ERROR %d\n",
431 printf("block 0x%08lx successfully "
441 if (strcmp(cmd, "biterr") == 0) {
446 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
447 if (strcmp(cmd, "lock") == 0) {
451 if (!strcmp("tight", argv[2]))
453 if (!strcmp("status", argv[2]))
457 do_nand_status(nand);
459 if (!nand_lock(nand, tight)) {
460 puts("NAND flash successfully locked\n");
462 puts("Error locking NAND flash\n");
469 if (strcmp(cmd, "unlock") == 0) {
470 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
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");
489 U_BOOT_CMD(nand, CONFIG_SYS_MAXARGS, 1, do_nand,
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(s) at offset (UNSAFE)\n"
503 "nand biterr off - make a bit error at offset (UNSAFE)"
504 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
506 "nand lock [tight] [status]\n"
507 " bring nand to lock state or display locked pages\n"
508 "nand unlock [offset] [size] - unlock section"
512 static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
513 ulong offset, ulong addr, char *cmd)
519 #if defined(CONFIG_FIT)
520 const void *fit_hdr = NULL;
523 s = strchr(cmd, '.');
525 (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
526 printf("Unknown nand load suffix '%s'\n", s);
527 show_boot_progress(-53);
531 printf("\nLoading from %s, offset 0x%lx\n", nand->name, offset);
533 cnt = nand->writesize;
534 r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
536 puts("** Read error\n");
537 show_boot_progress (-56);
540 show_boot_progress (56);
542 switch (genimg_get_format ((void *)addr)) {
543 case IMAGE_FORMAT_LEGACY:
544 hdr = (image_header_t *)addr;
546 show_boot_progress (57);
547 image_print_contents (hdr);
549 cnt = image_get_image_size (hdr);
551 #if defined(CONFIG_FIT)
552 case IMAGE_FORMAT_FIT:
553 fit_hdr = (const void *)addr;
554 puts ("Fit image detected...\n");
556 cnt = fit_get_size (fit_hdr);
560 show_boot_progress (-57);
561 puts ("** Unknown image type\n");
564 show_boot_progress (57);
566 r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
568 puts("** Read error\n");
569 show_boot_progress (-58);
572 show_boot_progress (58);
574 #if defined(CONFIG_FIT)
575 /* This cannot be done earlier, we need complete FIT image in RAM first */
576 if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
577 if (!fit_check_format (fit_hdr)) {
578 show_boot_progress (-150);
579 puts ("** Bad FIT image format\n");
582 show_boot_progress (151);
583 fit_print_contents (fit_hdr);
587 /* Loading ok, update default load address */
591 /* Check if we should attempt an auto-start */
592 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
594 extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
597 local_args[1] = NULL;
599 printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
601 do_bootm(cmdtp, 0, 1, local_args);
607 int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
609 char *boot_device = NULL;
611 ulong addr, offset = 0;
612 #if defined(CONFIG_CMD_MTDPARTS)
613 struct mtd_device *dev;
614 struct part_info *part;
618 char *p = (argc == 2) ? argv[1] : argv[2];
619 if (!(str2long(p, &addr)) && (mtdparts_init() == 0) &&
620 (find_dev_and_part(p, &dev, &pnum, &part) == 0)) {
621 if (dev->id->type != MTD_DEV_TYPE_NAND) {
622 puts("Not a NAND device\n");
628 addr = simple_strtoul(argv[1], NULL, 16);
630 addr = CONFIG_SYS_LOAD_ADDR;
631 return nand_load_image(cmdtp, &nand_info[dev->id->num],
632 part->offset, addr, argv[0]);
637 show_boot_progress(52);
640 addr = CONFIG_SYS_LOAD_ADDR;
641 boot_device = getenv("bootdevice");
644 addr = simple_strtoul(argv[1], NULL, 16);
645 boot_device = getenv("bootdevice");
648 addr = simple_strtoul(argv[1], NULL, 16);
649 boot_device = argv[2];
652 addr = simple_strtoul(argv[1], NULL, 16);
653 boot_device = argv[2];
654 offset = simple_strtoul(argv[3], NULL, 16);
657 #if defined(CONFIG_CMD_MTDPARTS)
661 show_boot_progress(-53);
665 show_boot_progress(53);
667 puts("\n** No boot device **\n");
668 show_boot_progress(-54);
671 show_boot_progress(54);
673 idx = simple_strtoul(boot_device, NULL, 16);
675 if (idx < 0 || idx >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[idx].name) {
676 printf("\n** Device %d not available\n", idx);
677 show_boot_progress(-55);
680 show_boot_progress(55);
682 return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
685 U_BOOT_CMD(nboot, 4, 1, do_nandboot,
686 "boot from NAND device",
687 "[partition] | [[[loadAddr] dev] offset]"