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
12 #include <linux/mtd/mtd.h>
16 #include <asm/byteorder.h>
17 #include <jffs2/jffs2.h>
20 #if defined(CONFIG_CMD_MTDPARTS)
22 /* parition handling routines */
23 int mtdparts_init(void);
24 int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
25 int find_dev_and_part(const char *id, struct mtd_device **dev,
26 u8 *part_num, struct part_info **part);
29 static int nand_dump(nand_info_t *nand, ulong off, int only_oob)
32 u_char *datbuf, *oobbuf, *p;
34 datbuf = malloc(nand->writesize + nand->oobsize);
35 oobbuf = malloc(nand->oobsize);
36 if (!datbuf || !oobbuf) {
37 puts("No memory for page buffer\n");
40 off &= ~(nand->writesize - 1);
41 loff_t addr = (loff_t) off;
42 struct mtd_oob_ops ops;
43 memset(&ops, 0, sizeof(ops));
45 ops.oobbuf = oobbuf; /* must exist, but oob data will be appended to ops.datbuf */
46 ops.len = nand->writesize;
47 ops.ooblen = nand->oobsize;
48 ops.mode = MTD_OOB_RAW;
49 i = nand->read_oob(nand, addr, &ops);
51 printf("Error (%d) reading page %08lx\n", i, off);
56 printf("Page %08lx dump:\n", off);
57 i = nand->writesize >> 4;
62 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x"
63 " %02x %02x %02x %02x %02x %02x %02x %02x\n",
64 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
65 p[8], p[9], p[10], p[11], p[12], p[13], p[14],
70 i = nand->oobsize >> 3;
72 printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
73 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
82 /* ------------------------------------------------------------------------- */
84 static inline int str2long(char *p, ulong *num)
88 *num = simple_strtoul(p, &endptr, 16);
89 return (*p != '\0' && *endptr == '\0') ? 1 : 0;
93 arg_off_size(int argc, char *argv[], nand_info_t *nand, ulong *off, size_t *size)
95 int idx = nand_curr_device;
96 #if defined(CONFIG_CMD_MTDPARTS)
97 struct mtd_device *dev;
98 struct part_info *part;
101 if (argc >= 1 && !(str2long(argv[0], off))) {
102 if ((mtdparts_init() == 0) &&
103 (find_dev_and_part(argv[0], &dev, &pnum, &part) == 0)) {
104 if (dev->id->type != MTD_DEV_TYPE_NAND) {
105 puts("not a NAND device\n");
110 if (!(str2long(argv[1], (ulong *)size))) {
111 printf("'%s' is not a number\n", argv[1]);
114 if (*size > part->size)
120 *nand = nand_info[idx];
127 if (!(str2long(argv[0], off))) {
128 printf("'%s' is not a number\n", argv[0]);
136 if (!(str2long(argv[1], (ulong *)size))) {
137 printf("'%s' is not a number\n", argv[1]);
141 *size = nand->size - *off;
144 #if defined(CONFIG_CMD_MTDPARTS)
147 printf("device %d ", idx);
148 if (*size == nand->size)
149 puts("whole chip\n");
151 printf("offset 0x%lx, size 0x%zx\n", *off, *size);
155 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
156 static void print_status(ulong start, ulong end, ulong erasesize, int status)
158 printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
161 (end - start) / erasesize,
162 ((status & NAND_LOCK_STATUS_TIGHT) ? "TIGHT " : ""),
163 ((status & NAND_LOCK_STATUS_LOCK) ? "LOCK " : ""),
164 ((status & NAND_LOCK_STATUS_UNLOCK) ? "UNLOCK " : ""));
167 static void do_nand_status(nand_info_t *nand)
169 ulong block_start = 0;
171 int last_status = -1;
173 struct nand_chip *nand_chip = nand->priv;
174 /* check the WP bit */
175 nand_chip->cmdfunc(nand, NAND_CMD_STATUS, -1, -1);
176 printf("device is %swrite protected\n",
177 (nand_chip->read_byte(nand) & 0x80 ?
180 for (off = 0; off < nand->size; off += nand->erasesize) {
181 int s = nand_get_lock_status(nand, off);
183 /* print message only if status has changed */
184 if (s != last_status && off != 0) {
185 print_status(block_start, off, nand->erasesize,
191 /* Print the last block info */
192 print_status(block_start, off, nand->erasesize, last_status);
196 static void nand_print_info(int idx)
198 nand_info_t *nand = &nand_info[idx];
199 struct nand_chip *chip = nand->priv;
200 printf("Device %d: ", idx);
201 if (chip->numchips > 1)
202 printf("%dx ", chip->numchips);
203 printf("%s, sector size %u KiB\n",
204 nand->name, nand->erasesize >> 10);
207 int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
214 #ifdef CONFIG_SYS_NAND_QUIET
215 int quiet = CONFIG_SYS_NAND_QUIET;
219 const char *quiet_str = getenv("quiet");
221 /* at least two arguments please */
226 quiet = simple_strtoul(quiet_str, NULL, 0) != 0;
230 if (strcmp(cmd, "info") == 0) {
233 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
234 if (nand_info[i].name)
240 if (strcmp(cmd, "device") == 0) {
244 if ((nand_curr_device < 0) ||
245 (nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE))
246 puts("no devices available\n");
248 nand_print_info(nand_curr_device);
251 dev = (int)simple_strtoul(argv[2], NULL, 10);
252 if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev].name) {
253 puts("No such device\n");
256 printf("Device %d: %s", dev, nand_info[dev].name);
257 puts("... is now current device\n");
258 nand_curr_device = dev;
260 #ifdef CONFIG_SYS_NAND_SELECT_DEVICE
262 * Select the chip in the board/cpu specific driver
264 board_nand_select_device(nand_info[dev].priv, dev);
270 if (strcmp(cmd, "bad") != 0 && strcmp(cmd, "erase") != 0 &&
271 strncmp(cmd, "dump", 4) != 0 &&
272 strncmp(cmd, "read", 4) != 0 && strncmp(cmd, "write", 5) != 0 &&
273 strcmp(cmd, "scrub") != 0 && strcmp(cmd, "markbad") != 0 &&
274 strcmp(cmd, "biterr") != 0 &&
275 strcmp(cmd, "lock") != 0 && strcmp(cmd, "unlock") != 0 )
278 /* the following commands operate on the current device */
279 if (nand_curr_device < 0 || nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
280 !nand_info[nand_curr_device].name) {
281 puts("\nno devices available\n");
284 nand = &nand_info[nand_curr_device];
286 if (strcmp(cmd, "bad") == 0) {
287 printf("\nDevice %d bad blocks:\n", nand_curr_device);
288 for (off = 0; off < nand->size; off += nand->erasesize)
289 if (nand_block_isbad(nand, off))
290 printf(" %08lx\n", off);
297 * nand erase [clean] [off size]
299 if (strcmp(cmd, "erase") == 0 || strcmp(cmd, "scrub") == 0) {
300 nand_erase_options_t opts;
301 /* "clean" at index 2 means request to write cleanmarker */
302 int clean = argc > 2 && !strcmp("clean", argv[2]);
303 int o = clean ? 3 : 2;
304 int scrub = !strcmp(cmd, "scrub");
306 printf("\nNAND %s: ", scrub ? "scrub" : "erase");
307 /* skip first two or three arguments, look for offset and size */
308 if (arg_off_size(argc - o, argv + o, nand, &off, &size) != 0)
311 memset(&opts, 0, sizeof(opts));
319 "scrub option will erase all factory set "
322 "There is no reliable way to recover them.\n"
324 "Use this command only for testing purposes "
327 "are sure of what you are doing!\n"
328 "\nReally scrub this NAND flash? <y/N>\n");
330 if (getc() == 'y' && getc() == '\r') {
333 puts("scrub aborted\n");
337 ret = nand_erase_opts(nand, &opts);
338 printf("%s\n", ret ? "ERROR" : "OK");
340 return ret == 0 ? 0 : 1;
343 if (strncmp(cmd, "dump", 4) == 0) {
347 s = strchr(cmd, '.');
348 off = (int)simple_strtoul(argv[2], NULL, 16);
350 if (s != NULL && strcmp(s, ".oob") == 0)
351 ret = nand_dump(nand, off, 1);
353 ret = nand_dump(nand, off, 0);
355 return ret == 0 ? 1 : 0;
359 if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
365 addr = (ulong)simple_strtoul(argv[2], NULL, 16);
367 read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
368 printf("\nNAND %s: ", read ? "read" : "write");
369 if (arg_off_size(argc - 3, argv + 3, nand, &off, &size) != 0)
372 s = strchr(cmd, '.');
373 if (!s || !strcmp(s, ".jffs2") ||
374 !strcmp(s, ".e") || !strcmp(s, ".i")) {
376 ret = nand_read_skip_bad(nand, off, &size,
379 ret = nand_write_skip_bad(nand, off, &size,
381 } else if (!strcmp(s, ".oob")) {
382 /* out-of-band data */
383 mtd_oob_ops_t ops = {
384 .oobbuf = (u8 *)addr,
390 ret = nand->read_oob(nand, off, &ops);
392 ret = nand->write_oob(nand, off, &ops);
394 printf("Unknown nand command suffix '%s'.\n", s);
398 printf(" %zu bytes %s: %s\n", size,
399 read ? "read" : "written", ret ? "ERROR" : "OK");
401 return ret == 0 ? 0 : 1;
404 if (strcmp(cmd, "markbad") == 0) {
412 addr = simple_strtoul(*argv, NULL, 16);
414 if (nand->block_markbad(nand, addr)) {
415 printf("block 0x%08lx NOT marked "
416 "as bad! ERROR %d\n",
420 printf("block 0x%08lx successfully "
430 if (strcmp(cmd, "biterr") == 0) {
435 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
436 if (strcmp(cmd, "lock") == 0) {
440 if (!strcmp("tight", argv[2]))
442 if (!strcmp("status", argv[2]))
446 do_nand_status(nand);
448 if (!nand_lock(nand, tight)) {
449 puts("NAND flash successfully locked\n");
451 puts("Error locking NAND flash\n");
458 if (strcmp(cmd, "unlock") == 0) {
459 if (arg_off_size(argc - 2, argv + 2, nand, &off, &size) < 0)
462 if (!nand_unlock(nand, off, size)) {
463 puts("NAND flash successfully unlocked\n");
465 puts("Error unlocking NAND flash, "
466 "write and erase will probably fail\n");
478 U_BOOT_CMD(nand, CONFIG_SYS_MAXARGS, 1, do_nand,
480 "info - show available NAND devices\n"
481 "nand device [dev] - show or set current device\n"
482 "nand read - addr off|partition size\n"
483 "nand write - addr off|partition size\n"
484 " read/write 'size' bytes starting at offset 'off'\n"
485 " to/from memory address 'addr', skipping bad blocks.\n"
486 "nand erase [clean] [off size] - erase 'size' bytes from\n"
487 " offset 'off' (entire device if not specified)\n"
488 "nand bad - show bad blocks\n"
489 "nand dump[.oob] off - dump page\n"
490 "nand scrub - really clean NAND erasing bad blocks (UNSAFE)\n"
491 "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
492 "nand biterr off - make a bit error at offset (UNSAFE)"
493 #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"
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_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_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]"