]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - drivers/mtd/nand/nand_util.c
Merge branch 'u-boot-samsung/master' into 'u-boot-arm/master'
[karo-tx-uboot.git] / drivers / mtd / nand / nand_util.c
index 3f111038f16dd8aeffbf570389e99c5e158900d8..ff2d3483076b1a5d92868270d136d8b599ea5b08 100644 (file)
@@ -80,8 +80,8 @@ int nand_erase_opts(nand_info_t *meminfo, const nand_erase_options_t *opts)
        struct mtd_oob_ops oob_opts;
        struct nand_chip *chip = meminfo->priv;
 
-       if ((opts->offset & (meminfo->writesize - 1)) != 0) {
-               printf("Attempt to erase non page-aligned data\n");
+       if ((opts->offset & (meminfo->erasesize - 1)) != 0) {
+               printf("Attempt to erase non block-aligned data\n");
                return -1;
        }
 
@@ -237,6 +237,14 @@ int nand_lock(struct mtd_info *mtd, int tight)
        /* select the NAND device */
        chip->select_chip(mtd, 0);
 
+       /* check the Lock Tight Status */
+       chip->cmdfunc(mtd, NAND_CMD_LOCK_STATUS, -1, 0);
+       if (chip->read_byte(mtd) & NAND_LOCK_STATUS_TIGHT) {
+               printf("nand_lock: Device is locked tight!\n");
+               ret = -1;
+               goto out;
+       }
+
        chip->cmdfunc(mtd,
                      (tight ? NAND_CMD_LOCK_TIGHT : NAND_CMD_LOCK),
                      -1, -1);
@@ -249,6 +257,7 @@ int nand_lock(struct mtd_info *mtd, int tight)
                ret = -1;
        }
 
+ out:
        /* de-select the NAND device */
        chip->select_chip(mtd, -1);
        return ret;
@@ -337,6 +346,15 @@ int nand_unlock(struct mtd_info *mtd, loff_t start, size_t length,
                goto out;
        }
 
+       /* check the Lock Tight Status */
+       page = (int)(start >> chip->page_shift);
+       chip->cmdfunc(mtd, NAND_CMD_LOCK_STATUS, -1, page & chip->pagemask);
+       if (chip->read_byte(mtd) & NAND_LOCK_STATUS_TIGHT) {
+               printf("nand_unlock: Device is locked tight!\n");
+               ret = -1;
+               goto out;
+       }
+
        if ((start & (mtd->erasesize - 1)) != 0) {
                printf("nand_unlock: Start address must be beginning of "
                        "nand block!\n");
@@ -358,7 +376,6 @@ int nand_unlock(struct mtd_info *mtd, loff_t start, size_t length,
        length -= mtd->erasesize;
 
        /* submit address of first page to unlock */
-       page = (int)(start >> chip->page_shift);
        chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
 
        /* submit ADDRESS of LAST page to unlock */
@@ -683,3 +700,125 @@ int nand_read_skip_bad(nand_info_t *nand, loff_t offset, size_t *length,
 
        return 0;
 }
+
+#ifdef CONFIG_CMD_NAND_TORTURE
+
+/**
+ * check_pattern:
+ *
+ * Check if buffer contains only a certain byte pattern.
+ *
+ * @param buf buffer to check
+ * @param patt the pattern to check
+ * @param size buffer size in bytes
+ * @return 1 if there are only patt bytes in buf
+ *         0 if something else was found
+ */
+static int check_pattern(const u_char *buf, u_char patt, int size)
+{
+       int i;
+
+       for (i = 0; i < size; i++)
+               if (buf[i] != patt)
+                       return 0;
+       return 1;
+}
+
+/**
+ * nand_torture:
+ *
+ * Torture a block of NAND flash.
+ * This is useful to determine if a block that caused a write error is still
+ * good or should be marked as bad.
+ *
+ * @param nand NAND device
+ * @param offset offset in flash
+ * @return 0 if the block is still good
+ */
+int nand_torture(nand_info_t *nand, loff_t offset)
+{
+       u_char patterns[] = {0xa5, 0x5a, 0x00};
+       struct erase_info instr = {
+               .mtd = nand,
+               .addr = offset,
+               .len = nand->erasesize,
+       };
+       size_t retlen;
+       int err, ret = -1, i, patt_count;
+       u_char *buf;
+
+       if ((offset & (nand->erasesize - 1)) != 0) {
+               puts("Attempt to torture a block at a non block-aligned offset\n");
+               return -EINVAL;
+       }
+
+       if (offset + nand->erasesize > nand->size) {
+               puts("Attempt to torture a block outside the flash area\n");
+               return -EINVAL;
+       }
+
+       patt_count = ARRAY_SIZE(patterns);
+
+       buf = malloc(nand->erasesize);
+       if (buf == NULL) {
+               puts("Out of memory for erase block buffer\n");
+               return -ENOMEM;
+       }
+
+       for (i = 0; i < patt_count; i++) {
+               err = nand->erase(nand, &instr);
+               if (err) {
+                       printf("%s: erase() failed for block at 0x%llx: %d\n",
+                               nand->name, instr.addr, err);
+                       goto out;
+               }
+
+               /* Make sure the block contains only 0xff bytes */
+               err = nand->read(nand, offset, nand->erasesize, &retlen, buf);
+               if ((err && err != -EUCLEAN) || retlen != nand->erasesize) {
+                       printf("%s: read() failed for block at 0x%llx: %d\n",
+                               nand->name, instr.addr, err);
+                       goto out;
+               }
+
+               err = check_pattern(buf, 0xff, nand->erasesize);
+               if (!err) {
+                       printf("Erased block at 0x%llx, but a non-0xff byte was found\n",
+                               offset);
+                       ret = -EIO;
+                       goto out;
+               }
+
+               /* Write a pattern and check it */
+               memset(buf, patterns[i], nand->erasesize);
+               err = nand->write(nand, offset, nand->erasesize, &retlen, buf);
+               if (err || retlen != nand->erasesize) {
+                       printf("%s: write() failed for block at 0x%llx: %d\n",
+                               nand->name, instr.addr, err);
+                       goto out;
+               }
+
+               err = nand->read(nand, offset, nand->erasesize, &retlen, buf);
+               if ((err && err != -EUCLEAN) || retlen != nand->erasesize) {
+                       printf("%s: read() failed for block at 0x%llx: %d\n",
+                               nand->name, instr.addr, err);
+                       goto out;
+               }
+
+               err = check_pattern(buf, patterns[i], nand->erasesize);
+               if (!err) {
+                       printf("Pattern 0x%.2x checking failed for block at "
+                                       "0x%llx\n", patterns[i], offset);
+                       ret = -EIO;
+                       goto out;
+               }
+       }
+
+       ret = 0;
+
+out:
+       free(buf);
+       return ret;
+}
+
+#endif