[PATCH v4 21/58] mtd: rawnand: fsl_upm: retain open-handle state on removal

From: James Hilliard

Date: Fri Nov 13 2026 - 16:01:52 EST


Keep chip metadata and controller storage alive until the final MTD
reference is released. Unregister and disconnect each published chip
before releasing the resources used by its hardware operations.

Also clean up scanned chips when partition registration fails.

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

Fixes: 5c249c5a57dc ("[MTD] [NAND] FSL UPM NAND driver")
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/mtd/nand/raw/fsl_upm.c | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/drivers/mtd/nand/raw/fsl_upm.c b/drivers/mtd/nand/raw/fsl_upm.c
index 28335dedb987..f9e02e355317 100644
--- a/drivers/mtd/nand/raw/fsl_upm.c
+++ b/drivers/mtd/nand/raw/fsl_upm.c
@@ -69,7 +69,16 @@ static int fun_chip_init(struct fsl_upm_nand *fun,
if (ret)
goto err;

+ ret = nand_set_lifetime(&fun->chip, NULL);
+ if (ret) {
+ nand_cleanup(&fun->chip);
+ goto err;
+ }
ret = mtd_device_register(mtd, NULL, 0);
+ if (ret) {
+ nand_disconnect(&fun->chip);
+ mtd_put_master(mtd);
+ }
err:
of_node_put(flash_np);
return ret;
@@ -169,9 +178,12 @@ static int fun_probe(struct platform_device *ofdev)
int size;
int i;

- fun = devm_kzalloc(&ofdev->dev, sizeof(*fun), GFP_KERNEL);
+ fun = kzalloc_obj(*fun);
if (!fun)
return -ENOMEM;
+ ret = devm_nand_controller_init(&ofdev->dev, &fun->base, fun);
+ if (ret)
+ return ret;

fun->io_base = devm_platform_get_and_ioremap_resource(ofdev, 0, &io_res);
if (IS_ERR(fun->io_base))
@@ -222,7 +234,6 @@ static int fun_probe(struct platform_device *ofdev)
}
}

- nand_controller_init(&fun->base);
fun->base.ops = &fun_ops;
fun->dev = &ofdev->dev;

@@ -244,7 +255,8 @@ static void fun_remove(struct platform_device *ofdev)

ret = mtd_device_unregister(mtd);
WARN_ON(ret);
- nand_cleanup(chip);
+ nand_disconnect(chip);
+ mtd_put_master(mtd);
}

static const struct of_device_id of_fun_match[] = {

--
2.53.0