Skip to contentSkip to conversation

Introduce mtdblock device

PATCH In review Alexey Romanov 2 participants 6 messages Jul 18, 2024 – Jul 24, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v7 cover Introduce mtdblock device

Hello!

This series adds support for the mtdblock device, which allows to read/write data block by block. For example, it can now be used for BCB or Android AB command:

  $ bcb load mtd 0 part_name

Tested only on SPI NAND, so bind is made only for
SPI NAND drivers.

---

Changes V1 -> V2 [1]:

- Drop patch [2].
- Add warning if bind NAND mtdblock device.
- Move documentation of mtdblock implementation from commit message to the source code.
- Remove __maybe_unused from mtd partition functions description.
- Use blk_enabled() instead of #ifdefs.

Changes V2 -> V3 [2]:

- Rebased over [3].
- Rename mtd_bread/bwrite -> mtd_blk_read/write.
- Fix GPL-2.0 license short name definiton in headers.
- Add empty line after MTD_ENTRY_NUMBERS define.

Changes V3 -> V4 [4]:

- Fix build warnings: use LBAF printf format string for lbaint_t types.

Changes V4 -> V5 [5]:

- Rebased over u-boot/master.
- Fix build errors and warnings if CONFIG_BLK isn't enabled.
- Fix failed tests in cases is mtdblock device isn't binded.

Changes V5 -> V6 [6]:

- Rebased over u-boot/master.
- Fix build errors when CONFIG_MTD isn't enabled.
- Use auto blk device devnum generation when call blk_create_devicef (pass -1 parameter instead of dev_seq(dev)).

Changes V6 -> V7 [7]:

- Rebased over u-boot/master.
- Introduce CONFIG_MTD_BLOCK which is disabled by default.

Links:

- [1] https://lore.kernel.org/all/[email protected]/
- [2] https://lore.kernel.org/all/[email protected]/
- [3] https://lore.kernel.org/u-boot/[email protected]/T/#u
- [4] https://lore.kernel.org/all/[email protected]/
- [5] https://lore.kernel.org/all/[email protected]/
- [6] https://lore.kernel.org/all/[email protected]/
- [7] https://lore.kernel.org/all/[email protected]/

Alexey Romanov (3):
disk: support MTD partitions
drivers: introduce mtdblock abstraction
spinand: bind mtdblock

 disk/part.c                 |   3 +-
 drivers/block/blk-uclass.c  |   1 +
 drivers/mtd/Kconfig         |   9 ++
 drivers/mtd/Makefile        |   1 +
 drivers/mtd/mtdblock.c      | 227 ++++++++++++++++++++++++++++++++++++
 drivers/mtd/mtdpart.c       |  76 ++++++++++++
 drivers/mtd/nand/spi/core.c |  21 ++++
 include/linux/mtd/mtd.h     |  25 ++++
 include/part.h              |   3 +
 9 files changed, 365 insertions(+), 1 deletion(-)
 create mode 100644 drivers/mtd/mtdblock.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH v7 1/3 disk: support MTD partitions

Add new MTD partition driver, which can be useful with mtdblock driver combination.

---
 disk/part.c           |  3 +-
 drivers/mtd/Kconfig   |  1 +
 drivers/mtd/mtdpart.c | 76 +++++++++++++++++++++++++++++++++++++++++++
 include/part.h        |  3 ++
 4 files changed, 82 insertions(+), 1 deletion(-)
disk/part.c +2 -1
index bc932526f9..86f669926e 100644
@@ -304,7 +304,8 @@ static void print_part_header(const char *type, struct blk_desc *desc)
 	CONFIG_IS_ENABLED(DOS_PARTITION) || \
 	CONFIG_IS_ENABLED(ISO_PARTITION) || \
 	CONFIG_IS_ENABLED(AMIGA_PARTITION) || \
-	CONFIG_IS_ENABLED(EFI_PARTITION)
+	CONFIG_IS_ENABLED(EFI_PARTITION) || \
+	CONFIG_IS_ENABLED(MTD_PARTITIONS)
 	printf("\nPartition Map for %s device %d  --   Partition Type: %s\n\n",
 	       uclass_get_name(desc->uclass_id), desc->devnum, type);
 #endif /* any CONFIG_..._PARTITION */
drivers/mtd/Kconfig +1 -0
index 4fdc9645d0..e4e29e0a3c 100644
@@ -2,6 +2,7 @@ menu "MTD Support"
 
 config MTD_PARTITIONS
 	bool
+	select PARTITIONS
 
 config MTD
 	bool "Enable MTD layer"
drivers/mtd/mtdpart.c +76 -0

85 lines not on this page · raw message

include/part.h +3 -0
index 32ee404885..40419fdf2f 100644
@@ -30,12 +30,15 @@ struct block_drvr {
 #define PART_TYPE_ISO		0x03
 #define PART_TYPE_AMIGA		0x04
 #define PART_TYPE_EFI		0x05
+#define PART_TYPE_MTD		0x06
 
 /* maximum number of partition entries supported by search */
 #define DOS_ENTRY_NUMBERS	8
 #define ISO_ENTRY_NUMBERS	64
 #define MAC_ENTRY_NUMBERS	64
 #define AMIGA_ENTRY_NUMBERS	8
+#define MTD_ENTRY_NUMBERS	64
+
 /*
  * Type string for U-Boot bootable partitions
  */
Signature · 1 line
2.34.1
#2 lore raw

PATCH v7 2/3 drivers: introduce mtdblock abstraction

In reply to Alexey Romanov

MTD block - abstraction over MTD subsystem, allowing to read and write in blocks using BLK UCLASS.

---
 drivers/block/blk-uclass.c |   1 +
 drivers/mtd/Kconfig        |   8 ++
 drivers/mtd/Makefile       |   1 +
 drivers/mtd/mtdblock.c     | 227 +++++++++++++++++++++++++++++++++++++
 include/linux/mtd/mtd.h    |  25 ++++
 5 files changed, 262 insertions(+)
 create mode 100644 drivers/mtd/mtdblock.c
drivers/block/blk-uclass.c +1 -0
index 512c952f4d..9b8aa45667 100644
@@ -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)
drivers/mtd/Kconfig +8 -0
index e4e29e0a3c..ce74ef31d7 100644
@@ -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
drivers/mtd/Makefile +1 -0
index c2fc80b10f..10d575e9f9 100644
@@ -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
drivers/mtd/mtdblock.c new file +227 -0

227 lines not on this page · raw message

include/linux/mtd/mtd.h +25 -0
index ee18a63e46..3f66ea17c1 100644
@@ -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);
 
Signature · 1 line
2.34.1
#3 lore raw

PATCH v7 3/3 spinand: bind mtdblock

In reply to Alexey Romanov

Bind SPI-NAND driver to MTD block driver.

---
 drivers/mtd/nand/spi/core.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
drivers/mtd/nand/spi/core.c +21 -0
index ef50237f10..54a90ff295 100644
@@ -33,6 +33,10 @@
 #include <linux/printk.h>
 #endif
 
+struct spinand_plat {
+	struct mtd_info *mtd;
+};
+
 /* SPI NAND index visible in MTD names */
 static int spi_nand_idx;
 
@@ -1172,12 +1176,25 @@ static void spinand_cleanup(struct spinand_device *spinand)
 	kfree(spinand->scratchbuf);
 }
 
+static int spinand_bind(struct udevice *dev)
+{
+	if (blk_enabled()) {
+		struct spinand_plat *plat = dev_get_plat(dev);
+
+		if (CONFIG_IS_ENABLED(MTD_BLOCK))
+			return mtd_bind(dev, &plat->mtd);
+	}
+
+	return 0;
+}
+
 static int spinand_probe(struct udevice *dev)
 {
 	struct spinand_device *spinand = dev_get_priv(dev);
 	struct spi_slave *slave = dev_get_parent_priv(dev);
 	struct mtd_info *mtd = dev_get_uclass_priv(dev);
 	struct nand_device *nand = spinand_to_nand(spinand);
+	struct spinand_plat *plat = dev_get_plat(dev);
 	int ret;
 
 #ifndef __UBOOT__
@@ -1217,6 +1234,8 @@ static int spinand_probe(struct udevice *dev)
 	if (ret)
 		goto err_spinand_cleanup;
 
+	plat->mtd = mtd;
+
 	return 0;
 
 err_spinand_cleanup:
@@ -1286,4 +1305,6 @@ U_BOOT_DRIVER(spinand) = {
 	.of_match = spinand_ids,
 	.priv_auto	= sizeof(struct spinand_device),
 	.probe = spinand_probe,
+	.bind = spinand_bind,
+	.plat_auto = sizeof(struct spinand_plat),
 };
Signature · 1 line
2.34.1
#4 lore raw
In reply to Alexey Romanov

Hi,

Quoted 2 lines

Tested only on SPI NAND, so bind is made only for
SPI NAND drivers.

If tried to test this with the SPI NOR framework but I couldn't get it to work. The SPI NOR flash didn't register with/was found by the mtdblock subsystem. I guess that is probably because the SPI NOR flash has it's own UCLASS_SPI_FLASH. There is some kind of compatibility layer (drivers/mtd/spi/sf_mtd.c) but it doesn't register a UCLASS_MTD.

Do you have any idea how to get this to work?

-michael

#5 lore raw

Hi Michael,

Sorry for the late reply.

Quoted 5 lines · Michael Walle

Hi,

Quoted 2 lines

Tested only on SPI NAND, so bind is made only for
SPI NAND drivers.

Quoted 8 lines

If tried to test this with the SPI NOR framework but I couldn't get it to work. The SPI NOR flash didn't register with/was found by the mtdblock subsystem. I guess that is probably because the SPI NOR flash has it's own UCLASS_SPI_FLASH. There is some kind of compatibility layer (drivers/mtd/spi/sf_mtd.c) but it doesn't register a UCLASS_MTD.

Do you have any idea how to get this to work?

I think you have to try bind driver via mtd_bind(), just like I do for SPI-NAND in patch "spinand: bind mtdblock".

I see what in file drivers/mtd/spi/sf_probe.c there is struct mtd_info and also you have U_BOOT_DRIVER. Maybe try to bind there?

Quoted 2 lines

-michael

Signature · 2 lines
Thank you,
Alexey