[PATCH v11 22/26] mtd: rawnand: sunxi: scan all chips before registering MTD devices
From: James Hilliard
Date: Fri Nov 20 2026 - 20:13:46 EST
On a controller with several NAND chips, I/O to the first chip can
overlap identification of the next one. The driver registers each MTD
device as soon as it has scanned that chip. Normal I/O holds the
controller mutex, but nand_scan_ident() does not: its RESET and READ ID
operations can change the chip select and timings under the active
transfer. The transfer can also interfere with identification.
Scan all chips before registering the first MTD device. Track which
scanned chips have been registered so that a failure in either phase
cleans up every scanned chip and unregisters only published devices.
Fixes: 1fef62c1423b ("mtd: nand: add sunxi NAND flash controller support")
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/mtd/nand/raw/sunxi_nand.c | 28 +++++++++++++++++++---------
1 file changed, 19 insertions(+), 9 deletions(-)
diff --git a/drivers/mtd/nand/raw/sunxi_nand.c b/drivers/mtd/nand/raw/sunxi_nand.c
index 73d6cd89ca19..abea2ab0cc42 100644
--- a/drivers/mtd/nand/raw/sunxi_nand.c
+++ b/drivers/mtd/nand/raw/sunxi_nand.c
@@ -274,6 +274,7 @@ struct sunxi_nfc_timings {
* @sels: array of CS lines descriptions
* @user_data_total: protected user bytes in a programmable layout
* @user_data_step: maximum bytes assigned to each step, zero for a fixed layout
+ * @registered: whether the MTD device has been registered
*/
struct sunxi_nand_chip {
struct list_head node;
@@ -284,6 +285,7 @@ struct sunxi_nand_chip {
u32 timing_ctl;
u16 user_data_total;
u8 user_data_step;
+ bool registered;
int nsels;
struct sunxi_nand_chip_sel sels[] __counted_by(nsels);
};
@@ -2563,8 +2565,10 @@ static void sunxi_nand_chips_cleanup(struct sunxi_nfc *nfc)
struct sunxi_nand_chip,
node);
chip = &sunxi_nand->nand;
- ret = mtd_device_unregister(nand_to_mtd(chip));
- WARN_ON(ret);
+ if (sunxi_nand->registered) {
+ ret = mtd_device_unregister(nand_to_mtd(chip));
+ WARN_ON(ret);
+ }
nand_cleanup(chip);
list_del(&sunxi_nand->node);
}
@@ -2644,13 +2648,6 @@ static int sunxi_nand_chip_init(struct device *dev, struct sunxi_nfc *nfc,
if (ret)
return ret;
- ret = mtd_device_register(mtd, NULL, 0);
- if (ret) {
- dev_err(dev, "failed to register mtd device: %d\n", ret);
- nand_cleanup(nand);
- return ret;
- }
-
list_add_tail(&sunxi_nand->node, &nfc->chips);
return 0;
@@ -2658,6 +2655,7 @@ static int sunxi_nand_chip_init(struct device *dev, struct sunxi_nfc *nfc,
static int sunxi_nand_chips_init(struct device *dev, struct sunxi_nfc *nfc)
{
+ struct sunxi_nand_chip *sunxi_nand;
struct device_node *np = dev->of_node;
int ret;
@@ -2669,6 +2667,18 @@ static int sunxi_nand_chips_init(struct device *dev, struct sunxi_nfc *nfc)
}
}
+ /* NAND identification must finish before MTD users can access the NFC. */
+ list_for_each_entry(sunxi_nand, &nfc->chips, node) {
+ ret = mtd_device_register(nand_to_mtd(&sunxi_nand->nand), NULL, 0);
+ if (ret) {
+ dev_err(dev, "failed to register mtd device: %d\n", ret);
+ sunxi_nand_chips_cleanup(nfc);
+ return ret;
+ }
+
+ sunxi_nand->registered = true;
+ }
+
return 0;
}
--
2.53.0