]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
drivers: introduce mtdblock abstraction
authorAlexey Romanov <avromanov@salutedevices.com>
Thu, 18 Jul 2024 05:46:05 +0000 (08:46 +0300)
committerMichael Trimarchi <michael@amarulasolutions.com>
Thu, 8 Aug 2024 07:27:50 +0000 (09:27 +0200)
MTD block - abstraction over MTD subsystem, allowing
to read and write in blocks using BLK UCLASS.

Signed-off-by: Alexey Romanov <avromanov@salutedevices.com>
Signed-off-by: Michael Trimarchi <michael@amarulasolutions.com>
drivers/block/blk-uclass.c
drivers/mtd/Kconfig
drivers/mtd/Makefile
drivers/mtd/mtdblock.c [new file with mode: 0644]
include/linux/mtd/mtd.h

index 512c952f4d7af7becdda51a7dd4b86e1cc46abc5..9b8aa45667e2d796b0177efe8a4e966bcba6a88c 100644 (file)
@@ -36,6 +36,7 @@ static struct {
        { UCLASS_PVBLOCK, "pvblock" },
        { UCLASS_BLKMAP, "blkmap" },
        { UCLASS_RKMTD, "rkmtd" },
+       { UCLASS_MTD, "mtd" },
 };
 
 static enum uclass_id uclass_name_to_iftype(const char *uclass_idname)
index e4e29e0a3c39c6a734e5331bd025c26a1430e75a..ce74ef31d7557b77dabcfe114b4bb0f0822bffbd 100644 (file)
@@ -32,6 +32,14 @@ config MTD_CONCAT
          into a single logical device. The larger logical device can then
          be partitioned.
 
+config MTD_BLOCK
+       bool "Enable block device access to MTD devices"
+       depends on BLK
+       default n
+       help
+         Enable support for block device access to MTD devices
+         using blk_ops abstraction.
+
 config SYS_MTDPARTS_RUNTIME
        bool "Allow MTDPARTS to be configured at runtime"
        help
index c2fc80b10f09560331ce04347c2d6cd3bf4c088d..10d575e9f93b519d763b40a7ceaddac15ded4aa5 100644 (file)
@@ -26,6 +26,7 @@ obj-y += onenand/
 obj-y += spi/
 obj-$(CONFIG_MTD_UBI) += ubi/
 obj-$(CONFIG_NVMXIP) += nvmxip/
+obj-$(CONFIG_MTD_BLOCK) += mtdblock.o
 
 #SPL/TPL build
 else
diff --git a/drivers/mtd/mtdblock.c b/drivers/mtd/mtdblock.c
new file mode 100644 (file)
index 0000000..66a79b8
--- /dev/null
@@ -0,0 +1,227 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * MTD block - abstraction over MTD subsystem, allowing
+ * to read and write in blocks using BLK UCLASS.
+ *
+ * - Read algorithm:
+ *
+ *   1. Convert start block number to start address.
+ *   2. Read block_dev->blksz bytes using mtd_read() and
+ *      add to start address pointer block_dev->blksz bytes,
+ *      until the requested number of blocks have been read.
+ *
+ * - Write algorithm:
+ *
+ *   1. Convert start block number to start address.
+ *   2. Round this address down by mtd->erasesize.
+ *
+ *   Erase addr      Start addr
+ *      |                |
+ *      v                v
+ *      +----------------+----------------+----------------+
+ *      |     blksz      |      blksz     |      blksz     |
+ *      +----------------+----------------+----------------+
+ *
+ *   3. Calculate offset between this two addresses.
+ *   4. Read mtd->erasesize bytes using mtd_read() into
+ *      temporary buffer from erase address.
+ *
+ *   Erase addr      Start addr
+ *      |                |
+ *      v                v
+ *      +----------------+----------------+----------------+
+ *      |     blksz      |      blksz     |      blksz     |
+ *      +----------------+----------------+----------------+
+ *      ^
+ *      |
+ *      |
+ *   mtd_read()
+ *   from here
+ *
+ *   5. Copy data from user buffer to temporary buffer with offset,
+ *      calculated at step 3.
+ *   6. Erase and write mtd->erasesize bytes at erase address
+ *      pointer using mtd_erase/mtd_write().
+ *   7. Add to erase address pointer mtd->erasesize bytes.
+ *   8. goto 1 until the requested number of blocks have
+ *      been written.
+ *
+ * (C) Copyright 2024 SaluteDevices, Inc.
+ *
+ * Author: Alexey Romanov <avromanov@salutedevices.com>
+ */
+
+#include <blk.h>
+#include <part.h>
+#include <dm/device.h>
+#include <dm/device-internal.h>
+#include <linux/mtd/mtd.h>
+
+int mtd_bind(struct udevice *dev, struct mtd_info **mtd)
+{
+       struct blk_desc *bdesc;
+       struct udevice *bdev;
+       int ret;
+
+       ret = blk_create_devicef(dev, "mtd_blk", "blk", UCLASS_MTD,
+                                -1, 512, 0, &bdev);
+       if (ret) {
+               pr_err("Cannot create block device\n");
+               return ret;
+       }
+
+       bdesc = dev_get_uclass_plat(bdev);
+       dev_set_priv(bdev, mtd);
+       bdesc->bdev = bdev;
+       bdesc->part_type = PART_TYPE_MTD;
+
+       return 0;
+}
+
+static ulong mtd_blk_read(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
+                         void *dst)
+{
+       struct blk_desc *block_dev = dev_get_uclass_plat(dev);
+       struct mtd_info *mtd = blk_desc_to_mtd(block_dev);
+       unsigned int sect_size = block_dev->blksz;
+       lbaint_t cur = start;
+       ulong read_cnt = 0;
+
+       while (read_cnt < blkcnt) {
+               int ret;
+               loff_t sect_start = cur * sect_size;
+               size_t retlen;
+
+               ret = mtd_read(mtd, sect_start, sect_size, &retlen, dst);
+               if (ret)
+                       return ret;
+
+               if (retlen != sect_size) {
+                       pr_err("mtdblock: failed to read block 0x" LBAF "\n", cur);
+                       return -EIO;
+               }
+
+               cur++;
+               dst += sect_size;
+               read_cnt++;
+       }
+
+       return read_cnt;
+}
+
+static int mtd_erase_write(struct mtd_info *mtd, uint64_t start, const void *src)
+{
+       int ret;
+       size_t retlen;
+       struct erase_info erase = { 0 };
+
+       erase.mtd = mtd;
+       erase.addr = start;
+       erase.len = mtd->erasesize;
+
+       ret = mtd_erase(mtd, &erase);
+       if (ret)
+               return ret;
+
+       ret = mtd_write(mtd, start, mtd->erasesize, &retlen, src);
+       if (ret)
+               return ret;
+
+       if (retlen != mtd->erasesize) {
+               pr_err("mtdblock: failed to read block at 0x%llx\n", start);
+               return -EIO;
+       }
+
+       return 0;
+}
+
+static ulong mtd_blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
+                          const void *src)
+{
+       struct blk_desc *block_dev = dev_get_uclass_plat(dev);
+       struct mtd_info *mtd = blk_desc_to_mtd(block_dev);
+       unsigned int sect_size = block_dev->blksz;
+       lbaint_t cur = start, blocks_todo = blkcnt;
+       ulong write_cnt = 0;
+       u8 *buf;
+       int ret = 0;
+
+       buf = malloc(mtd->erasesize);
+       if (!buf)
+               return -ENOMEM;
+
+       while (blocks_todo > 0) {
+               loff_t sect_start = cur * sect_size;
+               loff_t erase_start = ALIGN_DOWN(sect_start, mtd->erasesize);
+               u32 offset = sect_start - erase_start;
+               size_t cur_size = min_t(size_t,  mtd->erasesize - offset,
+                                       blocks_todo * sect_size);
+               size_t retlen;
+               lbaint_t written;
+
+               ret = mtd_read(mtd, erase_start, mtd->erasesize, &retlen, buf);
+               if (ret)
+                       goto out;
+
+               if (retlen != mtd->erasesize) {
+                       pr_err("mtdblock: failed to read block 0x" LBAF "\n", cur);
+                       ret = -EIO;
+                       goto out;
+               }
+
+               memcpy(buf + offset, src, cur_size);
+
+               ret = mtd_erase_write(mtd, erase_start, buf);
+               if (ret)
+                       goto out;
+
+               written = cur_size / sect_size;
+
+               blocks_todo -= written;
+               cur += written;
+               src += cur_size;
+               write_cnt += written;
+       }
+
+out:
+       free(buf);
+
+       if (ret)
+               return ret;
+
+       return write_cnt;
+}
+
+static int mtd_blk_probe(struct udevice *dev)
+{
+       struct blk_desc *bdesc;
+       struct mtd_info *mtd;
+       int ret;
+
+       ret = device_probe(dev);
+       if (ret) {
+               pr_err("Probing %s failed (err=%d)\n", dev->name, ret);
+               return ret;
+       }
+
+       bdesc = dev_get_uclass_plat(dev);
+       mtd = blk_desc_to_mtd(bdesc);
+
+       if (mtd_type_is_nand(mtd))
+               pr_warn("MTD device '%s' is NAND, please use UBI devices instead\n",
+                       mtd->name);
+
+       return 0;
+}
+
+static const struct blk_ops mtd_blk_ops = {
+       .read = mtd_blk_read,
+       .write = mtd_blk_write,
+};
+
+U_BOOT_DRIVER(mtd_blk) = {
+       .name = "mtd_blk",
+       .id = UCLASS_BLK,
+       .ops = &mtd_blk_ops,
+       .probe = mtd_blk_probe,
+};
index 983a55ce70e570a75d0eb14d890ff698c04b33f4..6751fb52c5a5fd82f89078af0eda6387fd319cd1 100644 (file)
@@ -26,6 +26,7 @@
 #include <dm/device.h>
 #endif
 #include <dm/ofnode.h>
+#include <blk.h>
 
 #define MAX_MTD_DEVICES 32
 #endif
@@ -412,6 +413,30 @@ int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
 int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
                    const u_char *buf);
 
+#if CONFIG_IS_ENABLED(MTD_BLOCK)
+static inline struct mtd_info *blk_desc_to_mtd(struct blk_desc *bdesc)
+{
+       void *priv = dev_get_priv(bdesc->bdev);
+
+       if (!priv)
+               return NULL;
+
+       return *((struct mtd_info **)priv);
+}
+
+int mtd_bind(struct udevice *dev, struct mtd_info **mtd);
+#else
+static inline struct mtd_info *blk_desc_to_mtd(struct blk_desc *bdesc)
+{
+       return NULL;
+}
+
+static inline int mtd_bind(struct udevice *dev, struct mtd_info **mtd)
+{
+       return -EOPNOTSUPP;
+}
+#endif
+
 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops);
 int mtd_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops);