mtd: Add an helper to make erase request aligned on ->erasesize
authorBoris Brezillon <boris.brezillon@free-electrons.com>
Fri, 15 Dec 2017 12:39:52 +0000 (13:39 +0100)
committerBoris Brezillon <boris.brezillon@free-electrons.com>
Sat, 6 Jan 2018 14:09:54 +0000 (15:09 +0100)
There's currently nothing forcing alignment of einfo->addr and
einfo->len on mtd->erasesize. Since we don't know if automatically
aligning those field in mtd_erase() will hurt some drivers, we add an
helper function to let drivers that need such an alignment explicitly
ask for it.

Signed-off-by: Boris Brezillon <boris.brezillon@free-electrons.com>
Reviewed-by: Miquel Raynal <miquel.raynal@free-electrons.com>
include/linux/mtd/mtd.h

index cd55bf14ad5141c1082753e9780417a0d5555501..205ededccc60e11e67645dddd978c90501570494 100644 (file)
@@ -489,6 +489,34 @@ static inline uint32_t mtd_mod_by_eb(uint64_t sz, struct mtd_info *mtd)
        return do_div(sz, mtd->erasesize);
 }
 
+/**
+ * mtd_align_erase_req - Adjust an erase request to align things on eraseblock
+ *                      boundaries.
+ * @mtd: the MTD device this erase request applies on
+ * @req: the erase request to adjust
+ *
+ * This function will adjust @req->addr and @req->len to align them on
+ * @mtd->erasesize. Of course we expect @mtd->erasesize to be != 0.
+ */
+static inline void mtd_align_erase_req(struct mtd_info *mtd,
+                                      struct erase_info *req)
+{
+       u32 mod;
+
+       if (WARN_ON(!mtd->erasesize))
+               return;
+
+       mod = mtd_mod_by_eb(req->addr, mtd);
+       if (mod) {
+               req->addr -= mod;
+               req->len += mod;
+       }
+
+       mod = mtd_mod_by_eb(req->addr + req->len, mtd);
+       if (mod)
+               req->len += mtd->erasesize - mod;
+}
+
 static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
 {
        if (mtd->writesize_shift)