mtd: rawnand: sunxi: Clean up chips after failed init
authorSamuel Holland <samuel@sholland.org>
Thu, 29 Dec 2022 18:15:20 +0000 (12:15 -0600)
committerMiquel Raynal <miquel.raynal@bootlin.com>
Mon, 2 Jan 2023 11:14:21 +0000 (12:14 +0100)
If a chip fails to initialize, we need to clean up any chips that were
already initialized/registered.

Fixes: 1fef62c1423b ("mtd: nand: add sunxi NAND flash controller support")
Signed-off-by: Samuel Holland <samuel@sholland.org>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/linux-mtd/20221229181526.53766-2-samuel@sholland.org
drivers/mtd/nand/raw/sunxi_nand.c

index ea953e3..2ee86f7 100644 (file)
@@ -1950,6 +1950,25 @@ static const struct nand_controller_ops sunxi_nand_controller_ops = {
        .exec_op = sunxi_nfc_exec_op,
 };
 
+static void sunxi_nand_chips_cleanup(struct sunxi_nfc *nfc)
+{
+       struct sunxi_nand_chip *sunxi_nand;
+       struct nand_chip *chip;
+       int ret;
+
+       while (!list_empty(&nfc->chips)) {
+               sunxi_nand = list_first_entry(&nfc->chips,
+                                             struct sunxi_nand_chip,
+                                             node);
+               chip = &sunxi_nand->nand;
+               ret = mtd_device_unregister(nand_to_mtd(chip));
+               WARN_ON(ret);
+               nand_cleanup(chip);
+               sunxi_nand_ecc_cleanup(sunxi_nand);
+               list_del(&sunxi_nand->node);
+       }
+}
+
 static int sunxi_nand_chip_init(struct device *dev, struct sunxi_nfc *nfc,
                                struct device_node *np)
 {
@@ -2053,6 +2072,7 @@ static int sunxi_nand_chips_init(struct device *dev, struct sunxi_nfc *nfc)
                ret = sunxi_nand_chip_init(dev, nfc, nand_np);
                if (ret) {
                        of_node_put(nand_np);
+                       sunxi_nand_chips_cleanup(nfc);
                        return ret;
                }
        }
@@ -2060,25 +2080,6 @@ static int sunxi_nand_chips_init(struct device *dev, struct sunxi_nfc *nfc)
        return 0;
 }
 
-static void sunxi_nand_chips_cleanup(struct sunxi_nfc *nfc)
-{
-       struct sunxi_nand_chip *sunxi_nand;
-       struct nand_chip *chip;
-       int ret;
-
-       while (!list_empty(&nfc->chips)) {
-               sunxi_nand = list_first_entry(&nfc->chips,
-                                             struct sunxi_nand_chip,
-                                             node);
-               chip = &sunxi_nand->nand;
-               ret = mtd_device_unregister(nand_to_mtd(chip));
-               WARN_ON(ret);
-               nand_cleanup(chip);
-               sunxi_nand_ecc_cleanup(sunxi_nand);
-               list_del(&sunxi_nand->node);
-       }
-}
-
 static int sunxi_nfc_dma_init(struct sunxi_nfc *nfc, struct resource *r)
 {
        int ret;