Skip to contentSkip to conversation

mtd: spinand: add merge-two-spinand function

PATCH In review CGEL 2 participants 2 messages Sep 23, 2021

Subsystems (2)
lore
Share & tools
lore

CGEL

author
#0 lore raw

PATCH 1/1 mtd: spinand: add merge-two-spinand function

From: Ren Xiaohui <[email protected]>

Combine the two SPI NAND flash in the MTD layer

---
 drivers/mtd/nand/spi/Kconfig          |   8 +
 drivers/mtd/nand/spi/Makefile         |   2 +-
 drivers/mtd/nand/spi/core.c           |  36 +++-
 drivers/mtd/nand/spi/spi_nand_merge.c | 350 ++++++++++++++++++++++++++++++++++
 include/linux/mtd/spinand.h           |  14 +-
 5 files changed, 400 insertions(+), 10 deletions(-)
 create mode 100644 drivers/mtd/nand/spi/spi_nand_merge.c
drivers/mtd/nand/spi/Kconfig +8 -0
index 3d7649a..6aec3ef 100644
@@ -7,3 +7,11 @@ menuconfig MTD_SPI_NAND
 	select SPI_MEM
 	help
 	  This is the framework for the SPI NAND device drivers.
+
+menuconfig MTD_SPI_NAND_MERGE
+	tristate "Two SPI NAND merge a mtd device"
+	select MTD_NAND_CORE
+	depends on MTD_SPI_NAND
+	select SPI_MEM
+	help
+	  This is the framework for Two SPI NAND merge a mtd device.
drivers/mtd/nand/spi/Makefile +1 -1
index 9662b9c..5d6475d 100644
@@ -1,3 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0
-spinand-objs := core.o gigadevice.o macronix.o micron.o paragon.o toshiba.o winbond.o
+spinand-objs := core.o gigadevice.o macronix.o micron.o paragon.o spi_nand_merge.o toshiba.o winbond.o
 obj-$(CONFIG_MTD_SPI_NAND) += spinand.o
drivers/mtd/nand/spi/core.c +28 -8
index 2c8685f..ee5e653 100644
@@ -624,7 +624,7 @@ static int spinand_write_page(struct spinand_device *spinand,
 	return nand_ecc_finish_io_req(nand, (struct nand_page_io_req *)req);
 }
 
-static int spinand_mtd_read(struct mtd_info *mtd, loff_t from,
+int spinand_mtd_read(struct mtd_info *mtd, loff_t from,
 			    struct mtd_oob_ops *ops)
 {
 	struct spinand_device *spinand = mtd_to_spinand(mtd);
@@ -669,8 +669,9 @@ static int spinand_mtd_read(struct mtd_info *mtd, loff_t from,
 
 	return ret ? ret : max_bitflips;
 }
+EXPORT_SYMBOL(spinand_mtd_read);
 
-static int spinand_mtd_write(struct mtd_info *mtd, loff_t to,
+int spinand_mtd_write(struct mtd_info *mtd, loff_t to,
 			     struct mtd_oob_ops *ops)
 {
 	struct spinand_device *spinand = mtd_to_spinand(mtd);
@@ -704,6 +705,7 @@ static int spinand_mtd_write(struct mtd_info *mtd, loff_t to,
 
 	return ret;
 }
+EXPORT_SYMBOL(spinand_mtd_write);
 
 static bool spinand_isbad(struct nand_device *nand, const struct nand_pos *pos)
 {
@@ -725,7 +727,7 @@ static bool spinand_isbad(struct nand_device *nand, const struct nand_pos *pos)
 	return false;
 }
 
-static int spinand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs)
+int spinand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs)
 {
 	struct nand_device *nand = mtd_to_nanddev(mtd);
 	struct spinand_device *spinand = nand_to_spinand(nand);
@@ -739,6 +741,7 @@ static int spinand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs)
 
 	return ret;
 }
+EXPORT_SYMBOL(spinand_mtd_block_isbad);
 
 static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos)
 {
@@ -764,7 +767,7 @@ static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos)
 	return spinand_write_page(spinand, &req);
 }
 
-static int spinand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs)
+int spinand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs)
 {
 	struct nand_device *nand = mtd_to_nanddev(mtd);
 	struct spinand_device *spinand = nand_to_spinand(nand);
@@ -778,6 +781,7 @@ static int spinand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs)
 
 	return ret;
 }
+EXPORT_SYMBOL(spinand_mtd_block_markbad);
 
 static int spinand_erase(struct nand_device *nand, const struct nand_pos *pos)
 {
@@ -808,8 +812,7 @@ static int spinand_erase(struct nand_device *nand, const struct nand_pos *pos)
 	return ret;
 }
 
-static int spinand_mtd_erase(struct mtd_info *mtd,
-			     struct erase_info *einfo)
+int spinand_mtd_erase(struct mtd_info *mtd, struct erase_info *einfo)
 {
 	struct spinand_device *spinand = mtd_to_spinand(mtd);
 	int ret;
@@ -820,8 +823,9 @@ static int spinand_mtd_erase(struct mtd_info *mtd,
 
 	return ret;
 }
+EXPORT_SYMBOL(spinand_mtd_erase);
 
-static int spinand_mtd_block_isreserved(struct mtd_info *mtd, loff_t offs)
+int spinand_mtd_block_isreserved(struct mtd_info *mtd, loff_t offs)
 {
 	struct spinand_device *spinand = mtd_to_spinand(mtd);
 	struct nand_device *nand = mtd_to_nanddev(mtd);
@@ -835,6 +839,7 @@ static int spinand_mtd_block_isreserved(struct mtd_info *mtd, loff_t offs)
 
 	return ret;
 }
+EXPORT_SYMBOL(spinand_mtd_block_isreserved);
 
 static int spinand_create_dirmap(struct spinand_device *spinand,
 				 unsigned int plane)
@@ -1017,7 +1022,8 @@ spinand_select_op_variant(struct spinand_device *spinand,
  * @table_size: size of the device description table
  * @rdid_method: read id method to match
  *
- * Match between a device ID retrieved through the READ_ID command and an
+ * Should be used by SPI NAND manufacturer drivers when they want to find a
+ * match between a device ID retrieved through the READ_ID command and an
  * entry in the SPI NAND description table. If a match is found, the spinand
  * object will be initialized with information provided by the matching
  * spinand_info entry.
@@ -1295,6 +1301,10 @@ static int spinand_probe(struct spi_mem *mem)
 	mtd = spinand_to_mtd(spinand);
 	mtd->dev.parent = &mem->spi->dev;
 
+#ifdef CONFIG_MTD_SPI_NAND_MERGE
+	merge_mtd_register(mtd);
+#endif
+
 	ret = spinand_init(spinand);
 	if (ret)
 		return ret;
@@ -1320,10 +1330,20 @@ static int spinand_remove(struct spi_mem *mem)
 	spinand = spi_mem_get_drvdata(mem);
 	mtd = spinand_to_mtd(spinand);
 
+#ifdef CONFIG_MTD_SPI_NAND_MERGE
+	if (mtd == get_merge_mtd(0) || mtd == get_merge_mtd(1)) {
+		pr_warn("this mtd device is merging, It is illegal.");
+		return 0;
+	}
 	ret = mtd_device_unregister(mtd);
 	if (ret)
 		return ret;
+	spinand_cleanup(spinand);
+#endif
 
+	ret = mtd_device_unregister(mtd);
+	if (ret)
+		return ret;
 	spinand_cleanup(spinand);
 
 	return 0;
drivers/mtd/nand/spi/spi_nand_merge.c new file +350 -0

350 lines not on this page · raw message

include/linux/mtd/spinand.h +13 -1
index 6988956..9c5fb0f 100644
@@ -504,12 +504,24 @@ static inline void spinand_set_of_node(struct spinand_device *spinand,
 	nanddev_set_of_node(&spinand->base, np);
 }
 
-int spinand_match_and_init(struct spinand_device *spinand,
+
+struct mtd_info *merge_mtd_register(struct mtd_info *mtd);
+struct mtd_info *get_merge_mtd(int index);
+
+int spinand_match_and_init(struct spinand_device *dev,
 			   const struct spinand_info *table,
 			   unsigned int table_size,
 			   enum spinand_readid_method rdid_method);
 
 int spinand_upd_cfg(struct spinand_device *spinand, u8 mask, u8 val);
 int spinand_select_target(struct spinand_device *spinand, unsigned int target);
+int spinand_mtd_erase(struct mtd_info *mtd, struct erase_info *einfo);
+int spinand_mtd_block_isreserved(struct mtd_info *mtd, loff_t offs);
+int spinand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs);
+int spinand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs);
+int spinand_mtd_write(struct mtd_info *mtd, loff_t to,
+			     struct mtd_oob_ops *ops);
+int spinand_mtd_read(struct mtd_info *mtd, loff_t from,
+			    struct mtd_oob_ops *ops);
 
 #endif /* __LINUX_MTD_SPINAND_H */
Signature · 1 line
2.15.2
#1 lore raw
Quoted 3 lines · On Thu, 23 Sep 2021 01:52:41 +0000 CGEL <[email protected]> wrote:

From: Ren Xiaohui <[email protected]>

Combine the two SPI NAND flash in the MTD layer

Just had a quick look, but it looks like you're re-implementing mtdconcat. Maybe you should just extend mtdconcat to cover your use cases.