]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - common/cmd_nand.c
x86: Add CBMEM console driver for coreboot
[karo-tx-uboot.git] / common / cmd_nand.c
index 634d03684b4365362e31d38c42cb395bfaea9aa7..1568594ca4109c0944292fb2ea7762fa343e7e76 100644 (file)
@@ -11,7 +11,7 @@
  * Added 16-bit nand support
  * (C) 2004 Texas Instruments
  *
- * Copyright 2010 Freescale Semiconductor
+ * Copyright 2010, 2012 Freescale Semiconductor
  * The portions of this file whose copyright is held by Freescale and which
  * are not considered a derived work of GPL v2-only code may be distributed
  * and/or modified under the terms of the GNU General Public License as
@@ -48,8 +48,8 @@ static int nand_dump(nand_info_t *nand, ulong off, int only_oob, int repeat)
 
        last = off;
 
-       datbuf = malloc(nand->writesize + nand->oobsize);
-       oobbuf = malloc(nand->oobsize);
+       datbuf = memalign(ARCH_DMA_MINALIGN, nand->writesize);
+       oobbuf = memalign(ARCH_DMA_MINALIGN, nand->oobsize);
        if (!datbuf || !oobbuf) {
                puts("No memory for page buffer\n");
                return 1;
@@ -59,7 +59,7 @@ static int nand_dump(nand_info_t *nand, ulong off, int only_oob, int repeat)
        struct mtd_oob_ops ops;
        memset(&ops, 0, sizeof(ops));
        ops.datbuf = datbuf;
-       ops.oobbuf = oobbuf; /* must exist, but oob data will be appended to ops.datbuf */
+       ops.oobbuf = oobbuf;
        ops.len = nand->writesize;
        ops.ooblen = nand->oobsize;
        ops.mode = MTD_OOB_RAW;
@@ -85,6 +85,7 @@ static int nand_dump(nand_info_t *nand, ulong off, int only_oob, int repeat)
        }
        puts("OOB:\n");
        i = nand->oobsize >> 3;
+       p = oobbuf;
        while (i--) {
                printf("\t%02x %02x %02x %02x %02x %02x %02x %02x\n",
                       p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
@@ -190,7 +191,7 @@ static int arg_off_size(int argc, char *const argv[], int *idx,
                        loff_t *off, loff_t *size)
 {
        int ret;
-       loff_t maxsize;
+       loff_t maxsize = 0;
 
        if (argc == 0) {
                *off = 0;
@@ -230,12 +231,18 @@ print:
 #ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
 static void print_status(ulong start, ulong end, ulong erasesize, int status)
 {
+       /*
+        * Micron NAND flash (e.g. MT29F4G08ABADAH4) BLOCK LOCK READ STATUS is
+        * not the same as others.  Instead of bit 1 being lock, it is
+        * #lock_tight. To make the driver support either format, ignore bit 1
+        * and use only bit 0 and bit 2.
+        */
        printf("%08lx - %08lx: %08lx blocks %s%s%s\n",
                start,
                end - 1,
                (end - start) / erasesize,
                ((status & NAND_LOCK_STATUS_TIGHT) ?  "TIGHT " : ""),
-               ((status & NAND_LOCK_STATUS_LOCK) ?  "LOCK " : ""),
+               (!(status & NAND_LOCK_STATUS_UNLOCK) ?  "LOCK " : ""),
                ((status & NAND_LOCK_STATUS_UNLOCK) ?  "UNLOCK " : ""));
 }
 
@@ -357,23 +364,71 @@ int do_nand_env_oob(cmd_tbl_t *cmdtp, int argc, char *const argv[])
        return ret;
 
 usage:
-       return cmd_usage(cmdtp);
+       return CMD_RET_USAGE;
 }
 
 #endif
 
-static void nand_print_info(int idx)
+static void nand_print_and_set_info(int idx)
 {
        nand_info_t *nand = &nand_info[idx];
        struct nand_chip *chip = nand->priv;
+       char buf[32];
+
        printf("Device %d: ", idx);
        if (chip->numchips > 1)
                printf("%dx ", chip->numchips);
        printf("%s, sector size %u KiB\n",
               nand->name, nand->erasesize >> 10);
+       printf("  Page size  %8d b\n", nand->writesize);
+       printf("  OOB size   %8d b\n", nand->oobsize);
+       printf("  Erase size %8d b\n", nand->erasesize);
+
+       /* Set geometry info */
+       sprintf(buf, "%x", nand->writesize);
+       setenv("nand_writesize", buf);
+
+       sprintf(buf, "%x", nand->oobsize);
+       setenv("nand_oobsize", buf);
+
+       sprintf(buf, "%x", nand->erasesize);
+       setenv("nand_erasesize", buf);
+}
+
+static int raw_access(nand_info_t *nand, ulong addr, loff_t off, ulong count,
+                       int read)
+{
+       int ret = 0;
+
+       while (count--) {
+               /* Raw access */
+               mtd_oob_ops_t ops = {
+                       .datbuf = (u8 *)addr,
+                       .oobbuf = ((u8 *)addr) + nand->writesize,
+                       .len = nand->writesize,
+                       .ooblen = nand->oobsize,
+                       .mode = MTD_OOB_RAW
+               };
+
+               if (read)
+                       ret = nand->read_oob(nand, off, &ops);
+               else
+                       ret = nand->write_oob(nand, off, &ops);
+
+               if (ret) {
+                       printf("%s: error at offset %llx, ret %d\n",
+                               __func__, (long long)off, ret);
+                       break;
+               }
+
+               addr += nand->writesize + nand->oobsize;
+               off += nand->writesize;
+       }
+
+       return ret;
 }
 
-int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
+static int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
        int i, ret = 0;
        ulong addr;
@@ -407,7 +462,7 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
                putc('\n');
                for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
                        if (nand_info[i].name)
-                               nand_print_info(i);
+                               nand_print_and_set_info(i);
                }
                return 0;
        }
@@ -418,7 +473,7 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
                        if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE)
                                puts("no devices available\n");
                        else
-                               nand_print_info(dev);
+                               nand_print_and_set_info(dev);
                        return 0;
                }
 
@@ -464,21 +519,28 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
                nand_erase_options_t opts;
                /* "clean" at index 2 means request to write cleanmarker */
                int clean = argc > 2 && !strcmp("clean", argv[2]);
-               int o = clean ? 3 : 2;
+               int scrub_yes = argc > 2 && !strcmp("-y", argv[2]);
+               int o = (clean || scrub_yes) ? 3 : 2;
                int scrub = !strncmp(cmd, "scrub", 5);
-               int part = 0;
-               int chip = 0;
                int spread = 0;
                int args = 2;
+               const char *scrub_warn =
+                       "Warning: "
+                       "scrub option will erase all factory set bad blocks!\n"
+                       "         "
+                       "There is no reliable way to recover them.\n"
+                       "         "
+                       "Use this command only for testing purposes if you\n"
+                       "         "
+                       "are sure of what you are doing!\n"
+                       "\nReally scrub this NAND flash? <y/N>\n";
 
                if (cmd[5] != 0) {
                        if (!strcmp(&cmd[5], ".spread")) {
                                spread = 1;
                        } else if (!strcmp(&cmd[5], ".part")) {
-                               part = 1;
                                args = 1;
                        } else if (!strcmp(&cmd[5], ".chip")) {
-                               chip = 1;
                                args = 0;
                        } else {
                                goto usage;
@@ -508,19 +570,12 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
                opts.spread = spread;
 
                if (scrub) {
-                       puts("Warning: "
-                            "scrub option will erase all factory set "
-                            "bad blocks!\n"
-                            "         "
-                            "There is no reliable way to recover them.\n"
-                            "         "
-                            "Use this command only for testing purposes "
-                            "if you\n"
-                            "         "
-                            "are sure of what you are doing!\n"
-                            "\nReally scrub this NAND flash? <y/N>\n");
-
-                       if (getc() == 'y') {
+                       if (!scrub_yes)
+                               puts(scrub_warn);
+
+                       if (scrub_yes)
+                               opts.scrub = 1;
+                       else if (getc() == 'y') {
                                puts("y");
                                if (getc() == '\r')
                                        opts.scrub = 1;
@@ -551,7 +606,9 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
 
        if (strncmp(cmd, "read", 4) == 0 || strncmp(cmd, "write", 5) == 0) {
                size_t rwsize;
+               ulong pagecount = 1;
                int read;
+               int raw;
 
                if (argc < 4)
                        goto usage;
@@ -560,13 +617,36 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
 
                read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
                printf("\nNAND %s: ", read ? "read" : "write");
-               if (arg_off_size(argc - 3, argv + 3, &dev, &off, &size) != 0)
-                       return 1;
 
                nand = &nand_info[dev];
-               rwsize = size;
 
                s = strchr(cmd, '.');
+
+               if (s && !strcmp(s, ".raw")) {
+                       raw = 1;
+
+                       if (arg_off(argv[3], &dev, &off, &size))
+                               return 1;
+
+                       if (argc > 4 && !str2long(argv[4], &pagecount)) {
+                               printf("'%s' is not a number\n", argv[4]);
+                               return 1;
+                       }
+
+                       if (pagecount * nand->writesize > size) {
+                               puts("Size exceeds partition or device limit\n");
+                               return -1;
+                       }
+
+                       rwsize = pagecount * (nand->writesize + nand->oobsize);
+               } else {
+                       if (arg_off_size(argc - 3, argv + 3, &dev,
+                                               &off, &size) != 0)
+                               return 1;
+
+                       rwsize = size;
+               }
+
                if (!s || !strcmp(s, ".jffs2") ||
                    !strcmp(s, ".e") || !strcmp(s, ".i")) {
                        if (read)
@@ -574,7 +654,27 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
                                                         (u_char *)addr);
                        else
                                ret = nand_write_skip_bad(nand, off, &rwsize,
-                                                         (u_char *)addr);
+                                                         (u_char *)addr, 0);
+#ifdef CONFIG_CMD_NAND_TRIMFFS
+               } else if (!strcmp(s, ".trimffs")) {
+                       if (read) {
+                               printf("Unknown nand command suffix '%s'\n", s);
+                               return 1;
+                       }
+                       ret = nand_write_skip_bad(nand, off, &rwsize,
+                                               (u_char *)addr,
+                                               WITH_DROP_FFS);
+#endif
+#ifdef CONFIG_CMD_NAND_YAFFS
+               } else if (!strcmp(s, ".yaffs")) {
+                       if (read) {
+                               printf("Unknown nand command suffix '%s'.\n", s);
+                               return 1;
+                       }
+                       ret = nand_write_skip_bad(nand, off, &rwsize,
+                                               (u_char *)addr,
+                                               WITH_INLINE_OOB);
+#endif
                } else if (!strcmp(s, ".oob")) {
                        /* out-of-band data */
                        mtd_oob_ops_t ops = {
@@ -587,6 +687,8 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
                                ret = nand->read_oob(nand, off, &ops);
                        else
                                ret = nand->write_oob(nand, off, &ops);
+               } else if (raw) {
+                       ret = raw_access(nand, addr, off, pagecount, read);
                } else {
                        printf("Unknown nand command suffix '%s'.\n", s);
                        return 1;
@@ -598,6 +700,25 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
                return ret == 0 ? 0 : 1;
        }
 
+#ifdef CONFIG_CMD_NAND_TORTURE
+       if (strcmp(cmd, "torture") == 0) {
+               if (argc < 3)
+                       goto usage;
+
+               if (!str2off(argv[2], &off)) {
+                       puts("Offset is not a valid number\n");
+                       return 1;
+               }
+
+               printf("\nNAND torture: device %d offset 0x%llx size 0x%x\n",
+                       dev, off, nand->erasesize);
+               ret = nand_torture(nand, off);
+               printf(" %s\n", ret ? "Failed" : "Passed");
+
+               return ret == 0 ? 0 : 1;
+       }
+#endif
+
        if (strcmp(cmd, "markbad") == 0) {
                argc -= 2;
                argv += 2;
@@ -652,11 +773,18 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
                return 0;
        }
 
-       if (strcmp(cmd, "unlock") == 0) {
+       if (strncmp(cmd, "unlock", 5) == 0) {
+               int allexcept = 0;
+
+               s = strchr(cmd, '.');
+
+               if (s && !strcmp(s, ".allexcept"))
+                       allexcept = 1;
+
                if (arg_off_size(argc - 2, argv + 2, &dev, &off, &size) < 0)
                        return 1;
 
-               if (!nand_unlock(&nand_info[dev], off, size)) {
+               if (!nand_unlock(&nand_info[dev], off, size, allexcept)) {
                        puts("NAND flash successfully unlocked\n");
                } else {
                        puts("Error unlocking NAND flash, "
@@ -668,19 +796,32 @@ int do_nand(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
 #endif
 
 usage:
-       return cmd_usage(cmdtp);
+       return CMD_RET_USAGE;
 }
 
-U_BOOT_CMD(
-       nand, CONFIG_SYS_MAXARGS, 1, do_nand,
-       "NAND sub-system",
+#ifdef CONFIG_SYS_LONGHELP
+static char nand_help_text[] =
        "info - show available NAND devices\n"
        "nand device [dev] - show or set current device\n"
        "nand read - addr off|partition size\n"
        "nand write - addr off|partition size\n"
        "    read/write 'size' bytes starting at offset 'off'\n"
        "    to/from memory address 'addr', skipping bad blocks.\n"
-       "nand erase[.spread] [clean] [off [size]] - erase 'size' bytes "
+       "nand read.raw - addr off|partition [count]\n"
+       "nand write.raw - addr off|partition [count]\n"
+       "    Use read.raw/write.raw to avoid ECC and access the flash as-is.\n"
+#ifdef CONFIG_CMD_NAND_TRIMFFS
+       "nand write.trimffs - addr off|partition size\n"
+       "    write 'size' bytes starting at offset 'off' from memory address\n"
+       "    'addr', skipping bad blocks and dropping any pages at the end\n"
+       "    of eraseblocks that contain only 0xFF\n"
+#endif
+#ifdef CONFIG_CMD_NAND_YAFFS
+       "nand write.yaffs - addr off|partition size\n"
+       "    write 'size' bytes starting at offset 'off' with yaffs format\n"
+       "    from memory address 'addr', skipping bad blocks.\n"
+#endif
+       "nand erase[.spread] [clean] off size - erase 'size' bytes "
        "from offset 'off'\n"
        "    With '.spread', erase enough for given file size, otherwise,\n"
        "    'size' includes skipped bad blocks.\n"
@@ -688,7 +829,10 @@ U_BOOT_CMD(
        "nand erase.chip [clean] - erase entire chip'\n"
        "nand bad - show bad blocks\n"
        "nand dump[.oob] off - dump page\n"
-       "nand scrub off size | scrub.part partition | scrub.chip\n"
+#ifdef CONFIG_CMD_NAND_TORTURE
+       "nand torture off - torture block at offset\n"
+#endif
+       "nand scrub [-y] off size | scrub.part partition | scrub.chip\n"
        "    really clean NAND erasing bad blocks (UNSAFE)\n"
        "nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
        "nand biterr off - make a bit error at offset (UNSAFE)"
@@ -696,7 +840,7 @@ U_BOOT_CMD(
        "\n"
        "nand lock [tight] [status]\n"
        "    bring nand to lock state or display locked pages\n"
-       "nand unlock [offset] [size] - unlock section"
+       "nand unlock[.allexcept] [offset] [size] - unlock section"
 #endif
 #ifdef CONFIG_ENV_OFFSET_OOB
        "\n"
@@ -705,13 +849,19 @@ U_BOOT_CMD(
        "nand env.oob set off|partition - set enviromnent offset\n"
        "nand env.oob get - get environment offset"
 #endif
+       "";
+#endif
+
+U_BOOT_CMD(
+       nand, CONFIG_SYS_MAXARGS, 1, do_nand,
+       "NAND sub-system", nand_help_text
 );
 
 static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
                           ulong offset, ulong addr, char *cmd)
 {
        int r;
-       char *ep, *s;
+       char *s;
        size_t cnt;
        image_header_t *hdr;
 #if defined(CONFIG_FIT)
@@ -722,7 +872,7 @@ static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
        if (s != NULL &&
            (strcmp(s, ".jffs2") && strcmp(s, ".e") && strcmp(s, ".i"))) {
                printf("Unknown nand load suffix '%s'\n", s);
-               show_boot_progress(-53);
+               bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
                return 1;
        }
 
@@ -732,16 +882,16 @@ static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
        r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
        if (r) {
                puts("** Read error\n");
-               show_boot_progress (-56);
+               bootstage_error(BOOTSTAGE_ID_NAND_HDR_READ);
                return 1;
        }
-       show_boot_progress (56);
+       bootstage_mark(BOOTSTAGE_ID_NAND_HDR_READ);
 
        switch (genimg_get_format ((void *)addr)) {
        case IMAGE_FORMAT_LEGACY:
                hdr = (image_header_t *)addr;
 
-               show_boot_progress (57);
+               bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
                image_print_contents (hdr);
 
                cnt = image_get_image_size (hdr);
@@ -755,29 +905,29 @@ static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
                break;
 #endif
        default:
-               show_boot_progress (-57);
+               bootstage_error(BOOTSTAGE_ID_NAND_TYPE);
                puts ("** Unknown image type\n");
                return 1;
        }
-       show_boot_progress (57);
+       bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
 
        r = nand_read_skip_bad(nand, offset, &cnt, (u_char *) addr);
        if (r) {
                puts("** Read error\n");
-               show_boot_progress (-58);
+               bootstage_error(BOOTSTAGE_ID_NAND_READ);
                return 1;
        }
-       show_boot_progress (58);
+       bootstage_mark(BOOTSTAGE_ID_NAND_READ);
 
 #if defined(CONFIG_FIT)
        /* This cannot be done earlier, we need complete FIT image in RAM first */
        if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
                if (!fit_check_format (fit_hdr)) {
-                       show_boot_progress (-150);
+                       bootstage_error(BOOTSTAGE_ID_NAND_FIT_READ);
                        puts ("** Bad FIT image format\n");
                        return 1;
                }
-               show_boot_progress (151);
+               bootstage_mark(BOOTSTAGE_ID_NAND_FIT_READ_OK);
                fit_print_contents (fit_hdr);
        }
 #endif
@@ -786,23 +936,11 @@ static int nand_load_image(cmd_tbl_t *cmdtp, nand_info_t *nand,
 
        load_addr = addr;
 
-       /* Check if we should attempt an auto-start */
-       if (((ep = getenv("autostart")) != NULL) && (strcmp(ep, "yes") == 0)) {
-               char *local_args[2];
-               extern int do_bootm(cmd_tbl_t *, int, int, char *[]);
-
-               local_args[0] = cmd;
-               local_args[1] = NULL;
-
-               printf("Automatic boot of image at addr 0x%08lx ...\n", addr);
-
-               do_bootm(cmdtp, 0, 1, local_args);
-               return 1;
-       }
-       return 0;
+       return bootm_maybe_autostart(cmdtp, cmd);
 }
 
-int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
+static int do_nandboot(cmd_tbl_t *cmdtp, int flag, int argc,
+                      char * const argv[])
 {
        char *boot_device = NULL;
        int idx;
@@ -832,7 +970,7 @@ int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
        }
 #endif
 
-       show_boot_progress(52);
+       bootstage_mark(BOOTSTAGE_ID_NAND_PART);
        switch (argc) {
        case 1:
                addr = CONFIG_SYS_LOAD_ADDR;
@@ -855,26 +993,26 @@ int do_nandboot(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
 #if defined(CONFIG_CMD_MTDPARTS)
 usage:
 #endif
-               show_boot_progress(-53);
-               return cmd_usage(cmdtp);
+               bootstage_error(BOOTSTAGE_ID_NAND_SUFFIX);
+               return CMD_RET_USAGE;
        }
+       bootstage_mark(BOOTSTAGE_ID_NAND_SUFFIX);
 
-       show_boot_progress(53);
        if (!boot_device) {
                puts("\n** No boot device **\n");
-               show_boot_progress(-54);
+               bootstage_error(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
                return 1;
        }
-       show_boot_progress(54);
+       bootstage_mark(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
 
        idx = simple_strtoul(boot_device, NULL, 16);
 
        if (idx < 0 || idx >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[idx].name) {
                printf("\n** Device %d not available\n", idx);
-               show_boot_progress(-55);
+               bootstage_error(BOOTSTAGE_ID_NAND_AVAILABLE);
                return 1;
        }
-       show_boot_progress(55);
+       bootstage_mark(BOOTSTAGE_ID_NAND_AVAILABLE);
 
        return nand_load_image(cmdtp, &nand_info[idx], offset, addr, argv[0]);
 }