[PATCH v3 46/58] mtd: rawnand: lpc32xx_slc: retain chip state across removal

From: James Hilliard

Date: Wed Nov 11 2026 - 20:41:13 EST


Drain chip operations before releasing DMA and changing the controller
and write-protect signals. Retain the embedded NAND chip and controller
until the final MTD reference releases their metadata.

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

Cc: linux-arm-kernel@xxxxxxxxxxxxxxxxxxx
Cc: Piotr Wojtaszczyk <piotr.wojtaszczyk@xxxxxxxxxxx>
Cc: Vladimir Zapolskiy <vz@xxxxxxxxx>
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/mtd/nand/raw/lpc32xx_slc.c | 22 +++++++++++++++++-----
1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/drivers/mtd/nand/raw/lpc32xx_slc.c b/drivers/mtd/nand/raw/lpc32xx_slc.c
index f75ae484ce86..85472c3d4a40 100644
--- a/drivers/mtd/nand/raw/lpc32xx_slc.c
+++ b/drivers/mtd/nand/raw/lpc32xx_slc.c
@@ -829,9 +829,14 @@ static int lpc32xx_nand_probe(struct platform_device *pdev)
int res;

/* Allocate memory for the device structure (and zero it) */
- host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
+ host = kzalloc_obj(*host);
if (!host)
return -ENOMEM;
+ chip = &host->nand_chip;
+ chip->controller = &chip->legacy.dummy_controller;
+ res = devm_nand_controller_init(&pdev->dev, chip->controller, host);
+ if (res)
+ return res;

host->io_base = devm_platform_get_and_ioremap_resource(pdev, 0, &rc);
if (IS_ERR(host->io_base))
@@ -862,7 +867,7 @@ static int lpc32xx_nand_probe(struct platform_device *pdev)
mtd = nand_to_mtd(chip);
nand_set_controller_data(chip, host);
nand_set_flash_node(chip, pdev->dev.of_node);
- mtd->owner = THIS_MODULE;
+
mtd->parent_dev = &pdev->dev;

/* Get NAND clock */
@@ -915,10 +920,16 @@ static int lpc32xx_nand_probe(struct platform_device *pdev)
goto release_dma;

mtd->name = "nxp_lpc3220_slc";
- res = mtd_device_register(mtd, host->ncfg->parts,
- host->ncfg->num_parts);
+ res = nand_set_lifetime(chip, NULL);
if (res)
goto cleanup_nand;
+ res = mtd_device_register(mtd, host->ncfg->parts,
+ host->ncfg->num_parts);
+ if (res) {
+ nand_disconnect(chip);
+ mtd_put_master(mtd);
+ goto release_dma;
+ }

return 0;

@@ -944,7 +955,7 @@ static void lpc32xx_nand_remove(struct platform_device *pdev)

ret = mtd_device_unregister(nand_to_mtd(chip));
WARN_ON(ret);
- nand_cleanup(chip);
+ nand_disconnect(chip);
dma_release_channel(host->dma_chan);

/* Force CE high */
@@ -953,6 +964,7 @@ static void lpc32xx_nand_remove(struct platform_device *pdev)
writel(tmp, SLC_CTRL(host->io_base));

lpc32xx_wp_enable(host);
+ mtd_put_master(nand_to_mtd(chip));
}

static int lpc32xx_nand_resume(struct platform_device *pdev)

--
2.53.0