blob: f6b12354024f4f1b8b2e0862c4a3165d63c3a32e [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Mike Rapoportaaf7ea22008-10-15 08:38:49 +02002/*
Mike Rapoportaaf7ea22008-10-15 08:38:49 +02003 * 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 Sittigc9d79c42014-08-05 10:37:26 +020010 * 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 Rapoportaaf7ea22008-10-15 08:38:49 +020013 */
14
15#include <linux/kernel.h>
Alexander Shiyan283df422013-05-06 17:53:48 +040016#include <linux/err.h>
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020017#include <linux/slab.h>
18#include <linux/module.h>
19#include <linux/platform_device.h>
Linus Walleijf3d0d8d2017-09-24 19:39:12 +020020#include <linux/gpio/consumer.h>
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020021#include <linux/io.h>
22#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020023#include <linux/mtd/rawnand.h>
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020024#include <linux/mtd/partitions.h>
25#include <linux/mtd/nand-gpio.h>
Jamie Iles775c32202011-12-18 10:00:49 +000026#include <linux/of.h>
27#include <linux/of_address.h>
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020028
29struct gpiomtd {
30 void __iomem *io_sync;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020031 struct nand_chip nand_chip;
32 struct gpio_nand_platdata plat;
Linus Walleijf3d0d8d2017-09-24 19:39:12 +020033 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 Rapoportaaf7ea22008-10-15 08:38:49 +020038};
39
Boris BREZILLONdc2948c2015-12-10 09:00:06 +010040static 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 Rapoportaaf7ea22008-10-15 08:38:49 +020044
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 */
54static 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
69static inline void gpio_nand_dosync(struct gpiomtd *gpiomtd) {}
70#endif
71
Boris Brezillon0f808c12018-09-06 14:05:26 +020072static void gpio_nand_cmd_ctrl(struct nand_chip *chip, int cmd,
73 unsigned int ctrl)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020074{
Boris Brezillon0f808c12018-09-06 14:05:26 +020075 struct gpiomtd *gpiomtd = gpio_nand_getpriv(nand_to_mtd(chip));
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020076
77 gpio_nand_dosync(gpiomtd);
78
79 if (ctrl & NAND_CTRL_CHANGE) {
Linus Walleijf3d0d8d2017-09-24 19:39:12 +020080 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 Rapoportaaf7ea22008-10-15 08:38:49 +020084 gpio_nand_dosync(gpiomtd);
85 }
86 if (cmd == NAND_CMD_NONE)
87 return;
88
Boris Brezillon82fc5092018-09-07 00:38:34 +020089 writeb(cmd, gpiomtd->nand_chip.legacy.IO_ADDR_W);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020090 gpio_nand_dosync(gpiomtd);
91}
92
Boris Brezillon50a487e2018-09-06 14:05:27 +020093static int gpio_nand_devready(struct nand_chip *chip)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020094{
Boris Brezillon50a487e2018-09-06 14:05:27 +020095 struct gpiomtd *gpiomtd = gpio_nand_getpriv(nand_to_mtd(chip));
Alexander Shiyan18afbc52012-10-17 10:08:27 +040096
Linus Walleijf3d0d8d2017-09-24 19:39:12 +020097 return gpiod_get_value(gpiomtd->rdy);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020098}
99
Jamie Iles775c32202011-12-18 10:00:49 +0000100#ifdef CONFIG_OF
101static const struct of_device_id gpio_nand_id_table[] = {
102 { .compatible = "gpio-control-nand" },
103 {}
104};
105MODULE_DEVICE_TABLE(of, gpio_nand_id_table);
106
107static int gpio_nand_get_config_of(const struct device *dev,
108 struct gpio_nand_platdata *plat)
109{
110 u32 val;
111
Alexander Shiyanee4f3662013-05-06 17:53:50 +0400112 if (!dev->of_node)
113 return -ENODEV;
114
Jamie Iles775c32202011-12-18 10:00:49 +0000115 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 Iles775c32202011-12-18 10:00:49 +0000124 if (!of_property_read_u32(dev->of_node, "chip-delay", &val))
125 plat->chip_delay = val;
126
127 return 0;
128}
129
130static struct resource *gpio_nand_get_io_sync_of(struct platform_device *pdev)
131{
Brian Norris103cdd82013-12-13 21:19:58 -0800132 struct resource *r;
Jamie Iles775c32202011-12-18 10:00:49 +0000133 u64 addr;
134
Brian Norris103cdd82013-12-13 21:19:58 -0800135 if (of_property_read_u64(pdev->dev.of_node,
Jamie Iles775c32202011-12-18 10:00:49 +0000136 "gpio-control-nand,io-sync-reg", &addr))
137 return NULL;
138
Brian Norris103cdd82013-12-13 21:19:58 -0800139 r = devm_kzalloc(&pdev->dev, sizeof(*r), GFP_KERNEL);
140 if (!r)
141 return NULL;
142
Jamie Iles775c32202011-12-18 10:00:49 +0000143 r->start = addr;
144 r->end = r->start + 0x3;
145 r->flags = IORESOURCE_MEM;
146
147 return r;
148}
149#else /* CONFIG_OF */
Jamie Iles775c32202011-12-18 10:00:49 +0000150static inline int gpio_nand_get_config_of(const struct device *dev,
151 struct gpio_nand_platdata *plat)
152{
153 return -ENOSYS;
154}
155
156static inline struct resource *
157gpio_nand_get_io_sync_of(struct platform_device *pdev)
158{
159 return NULL;
160}
161#endif /* CONFIG_OF */
162
163static 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 Han453810b2013-07-30 17:18:33 +0900171 if (dev_get_platdata(dev)) {
172 memcpy(plat, dev_get_platdata(dev), sizeof(*plat));
Jamie Iles775c32202011-12-18 10:00:49 +0000173 return 0;
174 }
175
176 return -EINVAL;
177}
178
179static inline struct resource *
180gpio_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 Shiyanf8e81c22013-05-06 17:53:53 +0400190static int gpio_nand_remove(struct platform_device *pdev)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200191{
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400192 struct gpiomtd *gpiomtd = platform_get_drvdata(pdev);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200193
Boris Brezillon59ac2762018-09-06 14:05:15 +0200194 nand_release(&gpiomtd->nand_chip);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200195
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200196 /* 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 Rapoportaaf7ea22008-10-15 08:38:49 +0200201
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200202 return 0;
203}
204
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400205static int gpio_nand_probe(struct platform_device *pdev)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200206{
207 struct gpiomtd *gpiomtd;
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400208 struct nand_chip *chip;
Boris BREZILLONdc2948c2015-12-10 09:00:06 +0100209 struct mtd_info *mtd;
Alexander Shiyan283df422013-05-06 17:53:48 +0400210 struct resource *res;
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200211 struct device *dev = &pdev->dev;
Jamie Iles775c32202011-12-18 10:00:49 +0000212 int ret = 0;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200213
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200214 if (!dev->of_node && !dev_get_platdata(dev))
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200215 return -EINVAL;
216
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200217 gpiomtd = devm_kzalloc(dev, sizeof(*gpiomtd), GFP_KERNEL);
Jingoo Han24e99712013-12-26 12:17:42 +0900218 if (!gpiomtd)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200219 return -ENOMEM;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200220
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400221 chip = &gpiomtd->nand_chip;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200222
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400223 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Boris Brezillon82fc5092018-09-07 00:38:34 +0200224 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 Shiyan283df422013-05-06 17:53:48 +0400227
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400228 res = gpio_nand_get_io_sync(pdev);
Alexander Shiyan283df422013-05-06 17:53:48 +0400229 if (res) {
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200230 gpiomtd->io_sync = devm_ioremap_resource(dev, res);
Alexander Shiyan283df422013-05-06 17:53:48 +0400231 if (IS_ERR(gpiomtd->io_sync))
232 return PTR_ERR(gpiomtd->io_sync);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200233 }
234
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200235 ret = gpio_nand_get_config(dev, &gpiomtd->plat);
Jamie Iles775c32202011-12-18 10:00:49 +0000236 if (ret)
Alexander Shiyan283df422013-05-06 17:53:48 +0400237 return ret;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200238
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200239 /* 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 Leroy44dd1822017-02-10 15:01:10 +0100249 }
Alexander Shiyan283df422013-05-06 17:53:48 +0400250
Christophe Leroybc2fd1b2017-12-06 18:27:14 +0100251 gpiomtd->ale = devm_gpiod_get(dev, "ale", GPIOD_OUT_LOW);
252 if (IS_ERR(gpiomtd->ale)) {
253 ret = PTR_ERR(gpiomtd->ale);
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200254 goto out_ce;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200255 }
Alexander Shiyan283df422013-05-06 17:53:48 +0400256
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200257 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 Shiyan283df422013-05-06 17:53:48 +0400262
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200263 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 Brezillon8395b752018-09-07 00:38:37 +0200270 chip->legacy.dev_ready = gpio_nand_devready;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200271
Brian Norrisa61ae812015-10-30 20:33:25 -0700272 nand_set_flash_node(chip, pdev->dev.of_node);
Boris Brezillon82fc5092018-09-07 00:38:34 +0200273 chip->legacy.IO_ADDR_W = chip->legacy.IO_ADDR_R;
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400274 chip->ecc.mode = NAND_ECC_SOFT;
Rafał Miłecki050658c2016-04-08 12:23:45 +0200275 chip->ecc.algo = NAND_ECC_HAMMING;
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400276 chip->options = gpiomtd->plat.options;
Boris Brezillon3cece3a2018-09-07 00:38:41 +0200277 chip->legacy.chip_delay = gpiomtd->plat.chip_delay;
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200278 chip->legacy.cmd_ctrl = gpio_nand_cmd_ctrl;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200279
Boris BREZILLONdc2948c2015-12-10 09:00:06 +0100280 mtd = nand_to_mtd(chip);
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200281 mtd->dev.parent = dev;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200282
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400283 platform_set_drvdata(pdev, gpiomtd);
Alexander Shiyan283df422013-05-06 17:53:48 +0400284
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200285 /* Disable write protection, if wired up */
286 if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
287 gpiod_direction_output(gpiomtd->nwp, 1);
Alexander Shiyan283df422013-05-06 17:53:48 +0400288
Boris Brezillon00ad3782018-09-06 14:05:14 +0200289 ret = nand_scan(chip, 1);
Masahiro Yamada408bf512016-11-04 19:42:52 +0900290 if (ret)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200291 goto err_wp;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200292
293 if (gpiomtd->plat.adjust_parts)
Boris BREZILLONdc2948c2015-12-10 09:00:06 +0100294 gpiomtd->plat.adjust_parts(&gpiomtd->plat, mtd->size);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200295
Boris BREZILLONdc2948c2015-12-10 09:00:06 +0100296 ret = mtd_device_register(mtd, gpiomtd->plat.parts,
Brian Norrisa61ae812015-10-30 20:33:25 -0700297 gpiomtd->plat.num_parts);
Alexander Shiyan283df422013-05-06 17:53:48 +0400298 if (!ret)
299 return 0;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200300
301err_wp:
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200302 if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
303 gpiod_set_value(gpiomtd->nwp, 0);
304out_ce:
305 if (gpiomtd->nce && !IS_ERR(gpiomtd->nce))
306 gpiod_set_value(gpiomtd->nce, 0);
Alexander Shiyan283df422013-05-06 17:53:48 +0400307
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200308 return ret;
309}
310
311static struct platform_driver gpio_nand_driver = {
312 .probe = gpio_nand_probe,
313 .remove = gpio_nand_remove,
314 .driver = {
315 .name = "gpio-nand",
Sachin Kamatb57d43f2013-03-14 15:37:03 +0530316 .of_match_table = of_match_ptr(gpio_nand_id_table),
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200317 },
318};
319
Sachin Kamat2fe87ae2012-09-05 15:31:32 +0530320module_platform_driver(gpio_nand_driver);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200321
322MODULE_LICENSE("GPL");
323MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
324MODULE_DESCRIPTION("GPIO NAND Driver");