Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 2 | /* |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 3 | * Updated, and converted to generic GPIO based driver by Russell King. |
| 4 | * |
| 5 | * Written by Ben Dooks <ben@simtec.co.uk> |
| 6 | * Based on 2.4 version by Mark Whittaker |
| 7 | * |
| 8 | * © 2004 Simtec Electronics |
| 9 | * |
Gerhard Sittig | c9d79c4 | 2014-08-05 10:37:26 +0200 | [diff] [blame] | 10 | * Device driver for NAND flash that uses a memory mapped interface to |
| 11 | * read/write the NAND commands and data, and GPIO pins for control signals |
| 12 | * (the DT binding refers to this as "GPIO assisted NAND flash") |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <linux/kernel.h> |
Alexander Shiyan | 283df42 | 2013-05-06 17:53:48 +0400 | [diff] [blame] | 16 | #include <linux/err.h> |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 17 | #include <linux/slab.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/platform_device.h> |
Linus Walleij | f3d0d8d | 2017-09-24 19:39:12 +0200 | [diff] [blame] | 20 | #include <linux/gpio/consumer.h> |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 21 | #include <linux/io.h> |
| 22 | #include <linux/mtd/mtd.h> |
Boris Brezillon | d4092d7 | 2017-08-04 17:29:10 +0200 | [diff] [blame] | 23 | #include <linux/mtd/rawnand.h> |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 24 | #include <linux/mtd/partitions.h> |
| 25 | #include <linux/mtd/nand-gpio.h> |
Jamie Iles | 775c3220 | 2011-12-18 10:00:49 +0000 | [diff] [blame] | 26 | #include <linux/of.h> |
| 27 | #include <linux/of_address.h> |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 28 | |
| 29 | struct gpiomtd { |
| 30 | void __iomem *io_sync; |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 31 | struct nand_chip nand_chip; |
| 32 | struct gpio_nand_platdata plat; |
Linus Walleij | f3d0d8d | 2017-09-24 19:39:12 +0200 | [diff] [blame] | 33 | struct gpio_desc *nce; /* Optional chip enable */ |
| 34 | struct gpio_desc *cle; |
| 35 | struct gpio_desc *ale; |
| 36 | struct gpio_desc *rdy; |
| 37 | struct gpio_desc *nwp; /* Optional write protection */ |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 38 | }; |
| 39 | |
Boris BREZILLON | dc2948c | 2015-12-10 09:00:06 +0100 | [diff] [blame] | 40 | static inline struct gpiomtd *gpio_nand_getpriv(struct mtd_info *mtd) |
| 41 | { |
| 42 | return container_of(mtd_to_nand(mtd), struct gpiomtd, nand_chip); |
| 43 | } |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 44 | |
| 45 | |
| 46 | #ifdef CONFIG_ARM |
| 47 | /* gpio_nand_dosync() |
| 48 | * |
| 49 | * Make sure the GPIO state changes occur in-order with writes to NAND |
| 50 | * memory region. |
| 51 | * Needed on PXA due to bus-reordering within the SoC itself (see section on |
| 52 | * I/O ordering in PXA manual (section 2.3, p35) |
| 53 | */ |
| 54 | static void gpio_nand_dosync(struct gpiomtd *gpiomtd) |
| 55 | { |
| 56 | unsigned long tmp; |
| 57 | |
| 58 | if (gpiomtd->io_sync) { |
| 59 | /* |
| 60 | * Linux memory barriers don't cater for what's required here. |
| 61 | * What's required is what's here - a read from a separate |
| 62 | * region with a dependency on that read. |
| 63 | */ |
| 64 | tmp = readl(gpiomtd->io_sync); |
| 65 | asm volatile("mov %1, %0\n" : "=r" (tmp) : "r" (tmp)); |
| 66 | } |
| 67 | } |
| 68 | #else |
| 69 | static inline void gpio_nand_dosync(struct gpiomtd *gpiomtd) {} |
| 70 | #endif |
| 71 | |
Boris Brezillon | 0f808c1 | 2018-09-06 14:05:26 +0200 | [diff] [blame] | 72 | static void gpio_nand_cmd_ctrl(struct nand_chip *chip, int cmd, |
| 73 | unsigned int ctrl) |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 74 | { |
Boris Brezillon | 0f808c1 | 2018-09-06 14:05:26 +0200 | [diff] [blame] | 75 | struct gpiomtd *gpiomtd = gpio_nand_getpriv(nand_to_mtd(chip)); |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 76 | |
| 77 | gpio_nand_dosync(gpiomtd); |
| 78 | |
| 79 | if (ctrl & NAND_CTRL_CHANGE) { |
Linus Walleij | f3d0d8d | 2017-09-24 19:39:12 +0200 | [diff] [blame] | 80 | if (gpiomtd->nce) |
| 81 | gpiod_set_value(gpiomtd->nce, !(ctrl & NAND_NCE)); |
| 82 | gpiod_set_value(gpiomtd->cle, !!(ctrl & NAND_CLE)); |
| 83 | gpiod_set_value(gpiomtd->ale, !!(ctrl & NAND_ALE)); |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 84 | gpio_nand_dosync(gpiomtd); |
| 85 | } |
| 86 | if (cmd == NAND_CMD_NONE) |
| 87 | return; |
| 88 | |
Boris Brezillon | 82fc509 | 2018-09-07 00:38:34 +0200 | [diff] [blame] | 89 | writeb(cmd, gpiomtd->nand_chip.legacy.IO_ADDR_W); |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 90 | gpio_nand_dosync(gpiomtd); |
| 91 | } |
| 92 | |
Boris Brezillon | 50a487e | 2018-09-06 14:05:27 +0200 | [diff] [blame] | 93 | static int gpio_nand_devready(struct nand_chip *chip) |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 94 | { |
Boris Brezillon | 50a487e | 2018-09-06 14:05:27 +0200 | [diff] [blame] | 95 | struct gpiomtd *gpiomtd = gpio_nand_getpriv(nand_to_mtd(chip)); |
Alexander Shiyan | 18afbc5 | 2012-10-17 10:08:27 +0400 | [diff] [blame] | 96 | |
Linus Walleij | f3d0d8d | 2017-09-24 19:39:12 +0200 | [diff] [blame] | 97 | return gpiod_get_value(gpiomtd->rdy); |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Jamie Iles | 775c3220 | 2011-12-18 10:00:49 +0000 | [diff] [blame] | 100 | #ifdef CONFIG_OF |
| 101 | static const struct of_device_id gpio_nand_id_table[] = { |
| 102 | { .compatible = "gpio-control-nand" }, |
| 103 | {} |
| 104 | }; |
| 105 | MODULE_DEVICE_TABLE(of, gpio_nand_id_table); |
| 106 | |
| 107 | static int gpio_nand_get_config_of(const struct device *dev, |
| 108 | struct gpio_nand_platdata *plat) |
| 109 | { |
| 110 | u32 val; |
| 111 | |
Alexander Shiyan | ee4f366 | 2013-05-06 17:53:50 +0400 | [diff] [blame] | 112 | if (!dev->of_node) |
| 113 | return -ENODEV; |
| 114 | |
Jamie Iles | 775c3220 | 2011-12-18 10:00:49 +0000 | [diff] [blame] | 115 | if (!of_property_read_u32(dev->of_node, "bank-width", &val)) { |
| 116 | if (val == 2) { |
| 117 | plat->options |= NAND_BUSWIDTH_16; |
| 118 | } else if (val != 1) { |
| 119 | dev_err(dev, "invalid bank-width %u\n", val); |
| 120 | return -EINVAL; |
| 121 | } |
| 122 | } |
| 123 | |
Jamie Iles | 775c3220 | 2011-12-18 10:00:49 +0000 | [diff] [blame] | 124 | if (!of_property_read_u32(dev->of_node, "chip-delay", &val)) |
| 125 | plat->chip_delay = val; |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static struct resource *gpio_nand_get_io_sync_of(struct platform_device *pdev) |
| 131 | { |
Brian Norris | 103cdd8 | 2013-12-13 21:19:58 -0800 | [diff] [blame] | 132 | struct resource *r; |
Jamie Iles | 775c3220 | 2011-12-18 10:00:49 +0000 | [diff] [blame] | 133 | u64 addr; |
| 134 | |
Brian Norris | 103cdd8 | 2013-12-13 21:19:58 -0800 | [diff] [blame] | 135 | if (of_property_read_u64(pdev->dev.of_node, |
Jamie Iles | 775c3220 | 2011-12-18 10:00:49 +0000 | [diff] [blame] | 136 | "gpio-control-nand,io-sync-reg", &addr)) |
| 137 | return NULL; |
| 138 | |
Brian Norris | 103cdd8 | 2013-12-13 21:19:58 -0800 | [diff] [blame] | 139 | r = devm_kzalloc(&pdev->dev, sizeof(*r), GFP_KERNEL); |
| 140 | if (!r) |
| 141 | return NULL; |
| 142 | |
Jamie Iles | 775c3220 | 2011-12-18 10:00:49 +0000 | [diff] [blame] | 143 | r->start = addr; |
| 144 | r->end = r->start + 0x3; |
| 145 | r->flags = IORESOURCE_MEM; |
| 146 | |
| 147 | return r; |
| 148 | } |
| 149 | #else /* CONFIG_OF */ |
Jamie Iles | 775c3220 | 2011-12-18 10:00:49 +0000 | [diff] [blame] | 150 | static inline int gpio_nand_get_config_of(const struct device *dev, |
| 151 | struct gpio_nand_platdata *plat) |
| 152 | { |
| 153 | return -ENOSYS; |
| 154 | } |
| 155 | |
| 156 | static inline struct resource * |
| 157 | gpio_nand_get_io_sync_of(struct platform_device *pdev) |
| 158 | { |
| 159 | return NULL; |
| 160 | } |
| 161 | #endif /* CONFIG_OF */ |
| 162 | |
| 163 | static inline int gpio_nand_get_config(const struct device *dev, |
| 164 | struct gpio_nand_platdata *plat) |
| 165 | { |
| 166 | int ret = gpio_nand_get_config_of(dev, plat); |
| 167 | |
| 168 | if (!ret) |
| 169 | return ret; |
| 170 | |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 171 | if (dev_get_platdata(dev)) { |
| 172 | memcpy(plat, dev_get_platdata(dev), sizeof(*plat)); |
Jamie Iles | 775c3220 | 2011-12-18 10:00:49 +0000 | [diff] [blame] | 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | return -EINVAL; |
| 177 | } |
| 178 | |
| 179 | static inline struct resource * |
| 180 | gpio_nand_get_io_sync(struct platform_device *pdev) |
| 181 | { |
| 182 | struct resource *r = gpio_nand_get_io_sync_of(pdev); |
| 183 | |
| 184 | if (r) |
| 185 | return r; |
| 186 | |
| 187 | return platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 188 | } |
| 189 | |
Alexander Shiyan | f8e81c2 | 2013-05-06 17:53:53 +0400 | [diff] [blame] | 190 | static int gpio_nand_remove(struct platform_device *pdev) |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 191 | { |
Alexander Shiyan | f8e81c2 | 2013-05-06 17:53:53 +0400 | [diff] [blame] | 192 | struct gpiomtd *gpiomtd = platform_get_drvdata(pdev); |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 193 | |
Boris Brezillon | 59ac276 | 2018-09-06 14:05:15 +0200 | [diff] [blame] | 194 | nand_release(&gpiomtd->nand_chip); |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 195 | |
Linus Walleij | f3d0d8d | 2017-09-24 19:39:12 +0200 | [diff] [blame] | 196 | /* Enable write protection and disable the chip */ |
| 197 | if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp)) |
| 198 | gpiod_set_value(gpiomtd->nwp, 0); |
| 199 | if (gpiomtd->nce && !IS_ERR(gpiomtd->nce)) |
| 200 | gpiod_set_value(gpiomtd->nce, 0); |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 201 | |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 202 | return 0; |
| 203 | } |
| 204 | |
Alexander Shiyan | f8e81c2 | 2013-05-06 17:53:53 +0400 | [diff] [blame] | 205 | static int gpio_nand_probe(struct platform_device *pdev) |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 206 | { |
| 207 | struct gpiomtd *gpiomtd; |
Alexander Shiyan | f8e81c2 | 2013-05-06 17:53:53 +0400 | [diff] [blame] | 208 | struct nand_chip *chip; |
Boris BREZILLON | dc2948c | 2015-12-10 09:00:06 +0100 | [diff] [blame] | 209 | struct mtd_info *mtd; |
Alexander Shiyan | 283df42 | 2013-05-06 17:53:48 +0400 | [diff] [blame] | 210 | struct resource *res; |
Linus Walleij | f3d0d8d | 2017-09-24 19:39:12 +0200 | [diff] [blame] | 211 | struct device *dev = &pdev->dev; |
Jamie Iles | 775c3220 | 2011-12-18 10:00:49 +0000 | [diff] [blame] | 212 | int ret = 0; |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 213 | |
Linus Walleij | f3d0d8d | 2017-09-24 19:39:12 +0200 | [diff] [blame] | 214 | if (!dev->of_node && !dev_get_platdata(dev)) |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 215 | return -EINVAL; |
| 216 | |
Linus Walleij | f3d0d8d | 2017-09-24 19:39:12 +0200 | [diff] [blame] | 217 | gpiomtd = devm_kzalloc(dev, sizeof(*gpiomtd), GFP_KERNEL); |
Jingoo Han | 24e9971 | 2013-12-26 12:17:42 +0900 | [diff] [blame] | 218 | if (!gpiomtd) |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 219 | return -ENOMEM; |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 220 | |
Alexander Shiyan | f8e81c2 | 2013-05-06 17:53:53 +0400 | [diff] [blame] | 221 | chip = &gpiomtd->nand_chip; |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 222 | |
Alexander Shiyan | f8e81c2 | 2013-05-06 17:53:53 +0400 | [diff] [blame] | 223 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Boris Brezillon | 82fc509 | 2018-09-07 00:38:34 +0200 | [diff] [blame] | 224 | chip->legacy.IO_ADDR_R = devm_ioremap_resource(dev, res); |
| 225 | if (IS_ERR(chip->legacy.IO_ADDR_R)) |
| 226 | return PTR_ERR(chip->legacy.IO_ADDR_R); |
Alexander Shiyan | 283df42 | 2013-05-06 17:53:48 +0400 | [diff] [blame] | 227 | |
Alexander Shiyan | f8e81c2 | 2013-05-06 17:53:53 +0400 | [diff] [blame] | 228 | res = gpio_nand_get_io_sync(pdev); |
Alexander Shiyan | 283df42 | 2013-05-06 17:53:48 +0400 | [diff] [blame] | 229 | if (res) { |
Linus Walleij | f3d0d8d | 2017-09-24 19:39:12 +0200 | [diff] [blame] | 230 | gpiomtd->io_sync = devm_ioremap_resource(dev, res); |
Alexander Shiyan | 283df42 | 2013-05-06 17:53:48 +0400 | [diff] [blame] | 231 | if (IS_ERR(gpiomtd->io_sync)) |
| 232 | return PTR_ERR(gpiomtd->io_sync); |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 233 | } |
| 234 | |
Linus Walleij | f3d0d8d | 2017-09-24 19:39:12 +0200 | [diff] [blame] | 235 | ret = gpio_nand_get_config(dev, &gpiomtd->plat); |
Jamie Iles | 775c3220 | 2011-12-18 10:00:49 +0000 | [diff] [blame] | 236 | if (ret) |
Alexander Shiyan | 283df42 | 2013-05-06 17:53:48 +0400 | [diff] [blame] | 237 | return ret; |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 238 | |
Linus Walleij | f3d0d8d | 2017-09-24 19:39:12 +0200 | [diff] [blame] | 239 | /* Just enable the chip */ |
| 240 | gpiomtd->nce = devm_gpiod_get_optional(dev, "nce", GPIOD_OUT_HIGH); |
| 241 | if (IS_ERR(gpiomtd->nce)) |
| 242 | return PTR_ERR(gpiomtd->nce); |
| 243 | |
| 244 | /* We disable write protection once we know probe() will succeed */ |
| 245 | gpiomtd->nwp = devm_gpiod_get_optional(dev, "nwp", GPIOD_OUT_LOW); |
| 246 | if (IS_ERR(gpiomtd->nwp)) { |
| 247 | ret = PTR_ERR(gpiomtd->nwp); |
| 248 | goto out_ce; |
Christophe Leroy | 44dd182 | 2017-02-10 15:01:10 +0100 | [diff] [blame] | 249 | } |
Alexander Shiyan | 283df42 | 2013-05-06 17:53:48 +0400 | [diff] [blame] | 250 | |
Christophe Leroy | bc2fd1b | 2017-12-06 18:27:14 +0100 | [diff] [blame] | 251 | gpiomtd->ale = devm_gpiod_get(dev, "ale", GPIOD_OUT_LOW); |
| 252 | if (IS_ERR(gpiomtd->ale)) { |
| 253 | ret = PTR_ERR(gpiomtd->ale); |
Linus Walleij | f3d0d8d | 2017-09-24 19:39:12 +0200 | [diff] [blame] | 254 | goto out_ce; |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 255 | } |
Alexander Shiyan | 283df42 | 2013-05-06 17:53:48 +0400 | [diff] [blame] | 256 | |
Linus Walleij | f3d0d8d | 2017-09-24 19:39:12 +0200 | [diff] [blame] | 257 | gpiomtd->cle = devm_gpiod_get(dev, "cle", GPIOD_OUT_LOW); |
| 258 | if (IS_ERR(gpiomtd->cle)) { |
| 259 | ret = PTR_ERR(gpiomtd->cle); |
| 260 | goto out_ce; |
| 261 | } |
Alexander Shiyan | 283df42 | 2013-05-06 17:53:48 +0400 | [diff] [blame] | 262 | |
Linus Walleij | f3d0d8d | 2017-09-24 19:39:12 +0200 | [diff] [blame] | 263 | gpiomtd->rdy = devm_gpiod_get_optional(dev, "rdy", GPIOD_IN); |
| 264 | if (IS_ERR(gpiomtd->rdy)) { |
| 265 | ret = PTR_ERR(gpiomtd->rdy); |
| 266 | goto out_ce; |
| 267 | } |
| 268 | /* Using RDY pin */ |
| 269 | if (gpiomtd->rdy) |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 270 | chip->legacy.dev_ready = gpio_nand_devready; |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 271 | |
Brian Norris | a61ae81 | 2015-10-30 20:33:25 -0700 | [diff] [blame] | 272 | nand_set_flash_node(chip, pdev->dev.of_node); |
Boris Brezillon | 82fc509 | 2018-09-07 00:38:34 +0200 | [diff] [blame] | 273 | chip->legacy.IO_ADDR_W = chip->legacy.IO_ADDR_R; |
Alexander Shiyan | f8e81c2 | 2013-05-06 17:53:53 +0400 | [diff] [blame] | 274 | chip->ecc.mode = NAND_ECC_SOFT; |
Rafał Miłecki | 050658c | 2016-04-08 12:23:45 +0200 | [diff] [blame] | 275 | chip->ecc.algo = NAND_ECC_HAMMING; |
Alexander Shiyan | f8e81c2 | 2013-05-06 17:53:53 +0400 | [diff] [blame] | 276 | chip->options = gpiomtd->plat.options; |
Boris Brezillon | 3cece3a | 2018-09-07 00:38:41 +0200 | [diff] [blame] | 277 | chip->legacy.chip_delay = gpiomtd->plat.chip_delay; |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 278 | chip->legacy.cmd_ctrl = gpio_nand_cmd_ctrl; |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 279 | |
Boris BREZILLON | dc2948c | 2015-12-10 09:00:06 +0100 | [diff] [blame] | 280 | mtd = nand_to_mtd(chip); |
Linus Walleij | f3d0d8d | 2017-09-24 19:39:12 +0200 | [diff] [blame] | 281 | mtd->dev.parent = dev; |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 282 | |
Alexander Shiyan | f8e81c2 | 2013-05-06 17:53:53 +0400 | [diff] [blame] | 283 | platform_set_drvdata(pdev, gpiomtd); |
Alexander Shiyan | 283df42 | 2013-05-06 17:53:48 +0400 | [diff] [blame] | 284 | |
Linus Walleij | f3d0d8d | 2017-09-24 19:39:12 +0200 | [diff] [blame] | 285 | /* Disable write protection, if wired up */ |
| 286 | if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp)) |
| 287 | gpiod_direction_output(gpiomtd->nwp, 1); |
Alexander Shiyan | 283df42 | 2013-05-06 17:53:48 +0400 | [diff] [blame] | 288 | |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 289 | ret = nand_scan(chip, 1); |
Masahiro Yamada | 408bf51 | 2016-11-04 19:42:52 +0900 | [diff] [blame] | 290 | if (ret) |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 291 | goto err_wp; |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 292 | |
| 293 | if (gpiomtd->plat.adjust_parts) |
Boris BREZILLON | dc2948c | 2015-12-10 09:00:06 +0100 | [diff] [blame] | 294 | gpiomtd->plat.adjust_parts(&gpiomtd->plat, mtd->size); |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 295 | |
Boris BREZILLON | dc2948c | 2015-12-10 09:00:06 +0100 | [diff] [blame] | 296 | ret = mtd_device_register(mtd, gpiomtd->plat.parts, |
Brian Norris | a61ae81 | 2015-10-30 20:33:25 -0700 | [diff] [blame] | 297 | gpiomtd->plat.num_parts); |
Alexander Shiyan | 283df42 | 2013-05-06 17:53:48 +0400 | [diff] [blame] | 298 | if (!ret) |
| 299 | return 0; |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 300 | |
| 301 | err_wp: |
Linus Walleij | f3d0d8d | 2017-09-24 19:39:12 +0200 | [diff] [blame] | 302 | if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp)) |
| 303 | gpiod_set_value(gpiomtd->nwp, 0); |
| 304 | out_ce: |
| 305 | if (gpiomtd->nce && !IS_ERR(gpiomtd->nce)) |
| 306 | gpiod_set_value(gpiomtd->nce, 0); |
Alexander Shiyan | 283df42 | 2013-05-06 17:53:48 +0400 | [diff] [blame] | 307 | |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 308 | return ret; |
| 309 | } |
| 310 | |
| 311 | static struct platform_driver gpio_nand_driver = { |
| 312 | .probe = gpio_nand_probe, |
| 313 | .remove = gpio_nand_remove, |
| 314 | .driver = { |
| 315 | .name = "gpio-nand", |
Sachin Kamat | b57d43f | 2013-03-14 15:37:03 +0530 | [diff] [blame] | 316 | .of_match_table = of_match_ptr(gpio_nand_id_table), |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 317 | }, |
| 318 | }; |
| 319 | |
Sachin Kamat | 2fe87ae | 2012-09-05 15:31:32 +0530 | [diff] [blame] | 320 | module_platform_driver(gpio_nand_driver); |
Mike Rapoport | aaf7ea2 | 2008-10-15 08:38:49 +0200 | [diff] [blame] | 321 | |
| 322 | MODULE_LICENSE("GPL"); |
| 323 | MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>"); |
| 324 | MODULE_DESCRIPTION("GPIO NAND Driver"); |