[PATCH 24/58] mtd: rawnand: xway: retain open-handle state on removal
James Hilliard
james.hilliard1 at gmail.com
Sat Nov 7 02:16:21 PST 2026
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.
Retain the embedded controller and chip across platform-device removal.
Signed-off-by: James Hilliard <james.hilliard1 at gmail.com>
---
drivers/mtd/nand/raw/xway_nand.c | 27 +++++++++++++++++++++------
1 file changed, 21 insertions(+), 6 deletions(-)
diff --git a/drivers/mtd/nand/raw/xway_nand.c b/drivers/mtd/nand/raw/xway_nand.c
index af84395dc66e..4686f28797bc 100644
--- a/drivers/mtd/nand/raw/xway_nand.c
+++ b/drivers/mtd/nand/raw/xway_nand.c
@@ -8,6 +8,7 @@
#include <linux/mtd/rawnand.h>
#include <linux/of.h>
#include <linux/platform_device.h>
+#include <linux/slab.h>
#include <lantiq_soc.h>
@@ -162,6 +163,11 @@ static const struct nand_controller_ops xway_nand_ops = {
/*
* Probe for the NAND device.
*/
+static void xway_nand_release(struct nand_controller *controller)
+{
+ kfree(container_of(controller, struct xway_nand_data, controller));
+}
+
static int xway_nand_probe(struct platform_device *pdev)
{
struct xway_nand_data *data;
@@ -171,10 +177,12 @@ static int xway_nand_probe(struct platform_device *pdev)
u32 cs_flag = 0;
/* Allocate memory for the device structure (and zero it) */
- data = devm_kzalloc(&pdev->dev, sizeof(struct xway_nand_data),
- GFP_KERNEL);
+ data = kzalloc_obj(*data);
if (!data)
return -ENOMEM;
+ err = devm_nand_controller_init(&pdev->dev, &data->controller, xway_nand_release);
+ if (err)
+ return err;
data->nandaddr = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(data->nandaddr))
@@ -192,7 +200,6 @@ static int xway_nand_probe(struct platform_device *pdev)
data->chip.legacy.read_byte = xway_read_byte;
data->chip.legacy.chip_delay = 30;
- nand_controller_init(&data->controller);
data->controller.ops = &xway_nand_ops;
data->chip.controller = &data->controller;
@@ -228,9 +235,16 @@ static int xway_nand_probe(struct platform_device *pdev)
if (err)
return err;
- err = mtd_device_register(mtd, NULL, 0);
- if (err)
+ err = nand_set_release(&data->chip, NULL);
+ if (err) {
nand_cleanup(&data->chip);
+ return err;
+ }
+ err = mtd_device_register(mtd, NULL, 0);
+ if (err) {
+ nand_disconnect(&data->chip);
+ mtd_put_master(mtd);
+ }
return err;
}
@@ -246,7 +260,8 @@ static void xway_nand_remove(struct platform_device *pdev)
ret = mtd_device_unregister(nand_to_mtd(chip));
WARN_ON(ret);
- nand_cleanup(chip);
+ nand_disconnect(chip);
+ mtd_put_master(nand_to_mtd(chip));
}
static const struct of_device_id xway_nand_match[] = {
--
2.53.0
More information about the linux-mtd
mailing list