[PATCH v4 51/58] mtd: rawnand: mpc5121: retain chip state across removal

From: James Hilliard

Date: Fri Nov 13 2026 - 14:12:46 EST


Drain NAND access before unmapping the external chip-select registers.
Keep the embedded chip and controller alive until the final MTD
reference, and unwind scanned NAND state on unsupported geometry and
publication failure.

Run NAND cleanup during disconnection; the final core-owned put only
releases retained allocation and layout data.

Fixes: bb315f749f8c ("mtd: nand: Add MPC5121 NAND Flash Controller driver")
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/mtd/nand/raw/mpc5121_nfc.c | 24 +++++++++++++++++-------
1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/drivers/mtd/nand/raw/mpc5121_nfc.c b/drivers/mtd/nand/raw/mpc5121_nfc.c
index 0aeeb6327472..42e167e1db7e 100644
--- a/drivers/mtd/nand/raw/mpc5121_nfc.c
+++ b/drivers/mtd/nand/raw/mpc5121_nfc.c
@@ -636,6 +636,13 @@ static int mpc5121_nfc_probe(struct platform_device *op)
return -ENXIO;
}

+ prv = kzalloc_obj(*prv);
+ if (!prv)
+ return -ENOMEM;
+ retval = devm_nand_controller_init(dev, &prv->controller, prv);
+ if (retval)
+ return retval;
+
regs = devm_platform_ioremap_resource(op, 0);
if (IS_ERR(regs))
return PTR_ERR(regs);
@@ -644,14 +651,9 @@ static int mpc5121_nfc_probe(struct platform_device *op)
if (irq < 0)
return irq;

- prv = devm_kzalloc(dev, sizeof(*prv), GFP_KERNEL);
- if (!prv)
- return -ENOMEM;
-
chip = &prv->chip;
mtd = nand_to_mtd(chip);

- nand_controller_init(&prv->controller);
prv->controller.ops = &mpc5121_nfc_ops;
chip->controller = &prv->controller;

@@ -782,19 +784,26 @@ static int mpc5121_nfc_probe(struct platform_device *op)
default:
dev_err(dev, "Unsupported NAND flash!\n");
retval = -ENXIO;
- goto error;
+ goto cleanup_nand;
}

dev_set_drvdata(dev, mtd);

/* Register device in MTD */
+ retval = nand_set_lifetime(chip, NULL);
+ if (retval)
+ goto cleanup_nand;
retval = mtd_device_register(mtd, NULL, 0);
if (retval) {
dev_err(dev, "Error adding MTD device!\n");
+ nand_disconnect(chip);
+ mtd_put_master(mtd);
goto error;
}

return 0;
+cleanup_nand:
+ nand_cleanup(chip);
error:
mpc5121_nfc_free(dev, mtd);
return retval;
@@ -808,8 +817,9 @@ static void mpc5121_nfc_remove(struct platform_device *op)

ret = mtd_device_unregister(mtd);
WARN_ON(ret);
- nand_cleanup(mtd_to_nand(mtd));
+ nand_disconnect(mtd_to_nand(mtd));
mpc5121_nfc_free(dev, mtd);
+ mtd_put_master(mtd);
}

static const struct of_device_id mpc5121_nfc_match[] = {

--
2.53.0