]> asedeno.scripts.mit.edu Git - linux.git/blobdiff - drivers/mtd/nand/core.c
Merge tag 'block-5.6-2020-02-05' of git://git.kernel.dk/linux-block
[linux.git] / drivers / mtd / nand / core.c
index 9c9f8936b63bc37bbcbd7c6d22d6ca85d69f388f..b6de955ac8bf106d3e9e9c4438ff96e36a9ec183 100644 (file)
@@ -173,6 +173,40 @@ int nanddev_mtd_erase(struct mtd_info *mtd, struct erase_info *einfo)
 }
 EXPORT_SYMBOL_GPL(nanddev_mtd_erase);
 
+/**
+ * nanddev_mtd_max_bad_blocks() - Get the maximum number of bad eraseblock on
+ *                               a specific region of the NAND device
+ * @mtd: MTD device
+ * @offs: offset of the NAND region
+ * @len: length of the NAND region
+ *
+ * Default implementation for mtd->_max_bad_blocks(). Only works if
+ * nand->memorg.max_bad_eraseblocks_per_lun is > 0.
+ *
+ * Return: a positive number encoding the maximum number of eraseblocks on a
+ * portion of memory, a negative error code otherwise.
+ */
+int nanddev_mtd_max_bad_blocks(struct mtd_info *mtd, loff_t offs, size_t len)
+{
+       struct nand_device *nand = mtd_to_nanddev(mtd);
+       struct nand_pos pos, end;
+       unsigned int max_bb = 0;
+
+       if (!nand->memorg.max_bad_eraseblocks_per_lun)
+               return -ENOTSUPP;
+
+       nanddev_offs_to_pos(nand, offs, &pos);
+       nanddev_offs_to_pos(nand, offs + len, &end);
+
+       for (nanddev_offs_to_pos(nand, offs, &pos);
+            nanddev_pos_cmp(&pos, &end) < 0;
+            nanddev_pos_next_lun(nand, &pos))
+               max_bb += nand->memorg.max_bad_eraseblocks_per_lun;
+
+       return max_bb;
+}
+EXPORT_SYMBOL_GPL(nanddev_mtd_max_bad_blocks);
+
 /**
  * nanddev_init() - Initialize a NAND device
  * @nand: NAND device