Skip to contentSkip to conversation

Introduce mtdblock device

PATCH Superseded Alexey Romanov 2 participants 6 messages Jun 26, 2024 – Jul 11, 2024

Subsystems (3)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v6 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)).

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]/

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         |   1 +
 drivers/mtd/Makefile        |   3 +
 drivers/mtd/mtdblock.c      | 227 ++++++++++++++++++++++++++++++++++++
 drivers/mtd/mtdpart.c       |  76 ++++++++++++
 drivers/mtd/nand/spi/core.c |  20 ++++
 include/linux/mtd/mtd.h     |  25 ++++
 include/part.h              |   3 +
 9 files changed, 358 insertions(+), 1 deletion(-)
 create mode 100644 drivers/mtd/mtdblock.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH v6 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 2bee669582..3f0fce5cfa 100644
@@ -305,7 +305,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 v6 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/Makefile       |   3 +
 drivers/mtd/mtdblock.c     | 227 +++++++++++++++++++++++++++++++++++++
 include/linux/mtd/mtd.h    |  25 ++++
 4 files changed, 256 insertions(+)
 create mode 100644 drivers/mtd/mtdblock.c
drivers/block/blk-uclass.c +1 -0
index 77066da352..ab0a9105c9 100644
@@ -37,6 +37,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/Makefile +3 -0
index c2fc80b10f..d87b4d32a5 100644
@@ -26,6 +26,9 @@ obj-y += onenand/
 obj-y += spi/
 obj-$(CONFIG_MTD_UBI) += ubi/
 obj-$(CONFIG_NVMXIP) += nvmxip/
+ifdef CONFIG_MTD
+obj-$(CONFIG_BLK) += mtdblock.o
+endif
 
 #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..4a3c393471 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(BLK)
+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 v6 3/3 spinand: bind mtdblock

In reply to Alexey Romanov

Bind SPI-NAND driver to MTD block driver.

---
 drivers/mtd/nand/spi/core.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/mtd/nand/spi/core.c +20 -0
index 62c28aa422..8edb468aed 100644
@@ -34,6 +34,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;
 
@@ -1173,12 +1177,24 @@ 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);
+
+		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__
@@ -1218,6 +1234,8 @@ static int spinand_probe(struct udevice *dev)
 	if (ret)
 		goto err_spinand_cleanup;
 
+	plat->mtd = mtd;
+
 	return 0;
 
 err_spinand_cleanup:
@@ -1287,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

Hello, ping.

Quoted 76 lines · Alexey Romanov

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)).

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]/

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         |   1 +
 drivers/mtd/Makefile        |   3 +
 drivers/mtd/mtdblock.c      | 227 ++++++++++++++++++++++++++++++++++++
 drivers/mtd/mtdpart.c       |  76 ++++++++++++
 drivers/mtd/nand/spi/core.c |  20 ++++
 include/linux/mtd/mtd.h     |  25 ++++
 include/part.h              |   3 +
 9 files changed, 358 insertions(+), 1 deletion(-)
 create mode 100644 drivers/mtd/mtdblock.c
Signature · 1 line
2.34.1
Signature · 2 lines
Thank you,
Alexey
#5 lore raw

Hello Alexey,

Quoted 2 lines · Alexey Romanov

Hello, ping.

I ran the CI tests, but they failed once again.

The "Introduce mtdblock device" series fails the "build all 32bit ARM platforms" test and
raises "exceeds file size limit" errors like the following:

arm: + mx6sabresd
1056+u-boot.img exceeds file size limit:
1057+ limit: 0xaebf6 bytes
1058+ actual: 0xaef88 bytes
1059+ excess: 0x392 bytes
1060+make[1]: *** [Makefile:1478: u-boot.img] Error 1 1061+make[1]: *** Deleting file 'u-boot.img' 1062+make: *** [Makefile:177: sub-make] Error 2

While the "Introduce UBI block device", applied over the "Introduce mtdblock device" falis the
following tests:
- vexpress_ca9x4:
- xilinx_zynq_virt:
- build all 32bit ARM platforms

All with the following error:
1450+arm-linux-gnueabi-ld.bfd: drivers/mtd/ubi/part.o: in function
`part_get_info_ubi':

    1451+drivers/mtd/ubi/part.c:55:(.text.part_get_info_ubi+0x4c):

undefined reference to `__aeabi_ldivmod'

Furthermore the "Introduce mtdblock device" series has to be rebased.

Please, run the buildman tests before submitting the patches.

Thanks and regards,
Dario

Quoted 82 lines · Alexey Romanov
Quoted 73 lines

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)).

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]/

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         |   1 +
 drivers/mtd/Makefile        |   3 +
 drivers/mtd/mtdblock.c      | 227 ++++++++++++++++++++++++++++++++++++
 drivers/mtd/mtdpart.c       |  76 ++++++++++++
 drivers/mtd/nand/spi/core.c |  20 ++++
 include/linux/mtd/mtd.h     |  25 ++++
 include/part.h              |   3 +
 9 files changed, 358 insertions(+), 1 deletion(-)
 create mode 100644 drivers/mtd/mtdblock.c
Quoted 3 lines

--
2.34.1

--
Thank you,
Alexey

Signature · 17 lines
Dario Binacchi

Senior Embedded Linux Developer

[email protected]

__________________________________


Amarula Solutions SRL

Via Le Canevare 30, 31100 Treviso, Veneto, IT

T. +39 042 243 5310
[email protected]

www.amarulasolutions.com