blob: 484f7fbc3f7d2d11cd66fc3416e64ab38d47f852 [file] [log] [blame]
Mike Rapoportaaf7ea22008-10-15 08:38:49 +02001/*
2 * drivers/mtd/nand/gpio.c
3 *
4 * Updated, and converted to generic GPIO based driver by Russell King.
5 *
6 * Written by Ben Dooks <ben@simtec.co.uk>
7 * Based on 2.4 version by Mark Whittaker
8 *
9 * © 2004 Simtec Electronics
10 *
Gerhard Sittigc9d79c42014-08-05 10:37:26 +020011 * Device driver for NAND flash that uses a memory mapped interface to
12 * read/write the NAND commands and data, and GPIO pins for control signals
13 * (the DT binding refers to this as "GPIO assisted NAND flash")
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020014 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License version 2 as
17 * published by the Free Software Foundation.
18 *
19 */
20
21#include <linux/kernel.h>
Alexander Shiyan283df422013-05-06 17:53:48 +040022#include <linux/err.h>
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020023#include <linux/slab.h>
24#include <linux/module.h>
25#include <linux/platform_device.h>
Linus Walleijf3d0d8d2017-09-24 19:39:12 +020026#include <linux/gpio/consumer.h>
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020027#include <linux/io.h>
28#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020029#include <linux/mtd/rawnand.h>
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020030#include <linux/mtd/partitions.h>
31#include <linux/mtd/nand-gpio.h>
Jamie Iles775c32202011-12-18 10:00:49 +000032#include <linux/of.h>
33#include <linux/of_address.h>
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020034
35struct gpiomtd {
36 void __iomem *io_sync;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020037 struct nand_chip nand_chip;
38 struct gpio_nand_platdata plat;
Linus Walleijf3d0d8d2017-09-24 19:39:12 +020039 struct gpio_desc *nce; /* Optional chip enable */
40 struct gpio_desc *cle;
41 struct gpio_desc *ale;
42 struct gpio_desc *rdy;
43 struct gpio_desc *nwp; /* Optional write protection */
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020044};
45
Boris BREZILLONdc2948c2015-12-10 09:00:06 +010046static inline struct gpiomtd *gpio_nand_getpriv(struct mtd_info *mtd)
47{
48 return container_of(mtd_to_nand(mtd), struct gpiomtd, nand_chip);
49}
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020050
51
52#ifdef CONFIG_ARM
53/* gpio_nand_dosync()
54 *
55 * Make sure the GPIO state changes occur in-order with writes to NAND
56 * memory region.
57 * Needed on PXA due to bus-reordering within the SoC itself (see section on
58 * I/O ordering in PXA manual (section 2.3, p35)
59 */
60static void gpio_nand_dosync(struct gpiomtd *gpiomtd)
61{
62 unsigned long tmp;
63
64 if (gpiomtd->io_sync) {
65 /*
66 * Linux memory barriers don't cater for what's required here.
67 * What's required is what's here - a read from a separate
68 * region with a dependency on that read.
69 */
70 tmp = readl(gpiomtd->io_sync);
71 asm volatile("mov %1, %0\n" : "=r" (tmp) : "r" (tmp));
72 }
73}
74#else
75static inline void gpio_nand_dosync(struct gpiomtd *gpiomtd) {}
76#endif
77
78static void gpio_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
79{
80 struct gpiomtd *gpiomtd = gpio_nand_getpriv(mtd);
81
82 gpio_nand_dosync(gpiomtd);
83
84 if (ctrl & NAND_CTRL_CHANGE) {
Linus Walleijf3d0d8d2017-09-24 19:39:12 +020085 if (gpiomtd->nce)
86 gpiod_set_value(gpiomtd->nce, !(ctrl & NAND_NCE));
87 gpiod_set_value(gpiomtd->cle, !!(ctrl & NAND_CLE));
88 gpiod_set_value(gpiomtd->ale, !!(ctrl & NAND_ALE));
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020089 gpio_nand_dosync(gpiomtd);
90 }
91 if (cmd == NAND_CMD_NONE)
92 return;
93
94 writeb(cmd, gpiomtd->nand_chip.IO_ADDR_W);
95 gpio_nand_dosync(gpiomtd);
96}
97
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020098static int gpio_nand_devready(struct mtd_info *mtd)
99{
100 struct gpiomtd *gpiomtd = gpio_nand_getpriv(mtd);
Alexander Shiyan18afbc52012-10-17 10:08:27 +0400101
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200102 return gpiod_get_value(gpiomtd->rdy);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200103}
104
Jamie Iles775c32202011-12-18 10:00:49 +0000105#ifdef CONFIG_OF
106static const struct of_device_id gpio_nand_id_table[] = {
107 { .compatible = "gpio-control-nand" },
108 {}
109};
110MODULE_DEVICE_TABLE(of, gpio_nand_id_table);
111
112static int gpio_nand_get_config_of(const struct device *dev,
113 struct gpio_nand_platdata *plat)
114{
115 u32 val;
116
Alexander Shiyanee4f3662013-05-06 17:53:50 +0400117 if (!dev->of_node)
118 return -ENODEV;
119
Jamie Iles775c32202011-12-18 10:00:49 +0000120 if (!of_property_read_u32(dev->of_node, "bank-width", &val)) {
121 if (val == 2) {
122 plat->options |= NAND_BUSWIDTH_16;
123 } else if (val != 1) {
124 dev_err(dev, "invalid bank-width %u\n", val);
125 return -EINVAL;
126 }
127 }
128
Jamie Iles775c32202011-12-18 10:00:49 +0000129 if (!of_property_read_u32(dev->of_node, "chip-delay", &val))
130 plat->chip_delay = val;
131
132 return 0;
133}
134
135static struct resource *gpio_nand_get_io_sync_of(struct platform_device *pdev)
136{
Brian Norris103cdd82013-12-13 21:19:58 -0800137 struct resource *r;
Jamie Iles775c32202011-12-18 10:00:49 +0000138 u64 addr;
139
Brian Norris103cdd82013-12-13 21:19:58 -0800140 if (of_property_read_u64(pdev->dev.of_node,
Jamie Iles775c32202011-12-18 10:00:49 +0000141 "gpio-control-nand,io-sync-reg", &addr))
142 return NULL;
143
Brian Norris103cdd82013-12-13 21:19:58 -0800144 r = devm_kzalloc(&pdev->dev, sizeof(*r), GFP_KERNEL);
145 if (!r)
146 return NULL;
147
Jamie Iles775c32202011-12-18 10:00:49 +0000148 r->start = addr;
149 r->end = r->start + 0x3;
150 r->flags = IORESOURCE_MEM;
151
152 return r;
153}
154#else /* CONFIG_OF */
Jamie Iles775c32202011-12-18 10:00:49 +0000155static inline int gpio_nand_get_config_of(const struct device *dev,
156 struct gpio_nand_platdata *plat)
157{
158 return -ENOSYS;
159}
160
161static inline struct resource *
162gpio_nand_get_io_sync_of(struct platform_device *pdev)
163{
164 return NULL;
165}
166#endif /* CONFIG_OF */
167
168static inline int gpio_nand_get_config(const struct device *dev,
169 struct gpio_nand_platdata *plat)
170{
171 int ret = gpio_nand_get_config_of(dev, plat);
172
173 if (!ret)
174 return ret;
175
Jingoo Han453810b2013-07-30 17:18:33 +0900176 if (dev_get_platdata(dev)) {
177 memcpy(plat, dev_get_platdata(dev), sizeof(*plat));
Jamie Iles775c32202011-12-18 10:00:49 +0000178 return 0;
179 }
180
181 return -EINVAL;
182}
183
184static inline struct resource *
185gpio_nand_get_io_sync(struct platform_device *pdev)
186{
187 struct resource *r = gpio_nand_get_io_sync_of(pdev);
188
189 if (r)
190 return r;
191
192 return platform_get_resource(pdev, IORESOURCE_MEM, 1);
193}
194
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400195static int gpio_nand_remove(struct platform_device *pdev)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200196{
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400197 struct gpiomtd *gpiomtd = platform_get_drvdata(pdev);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200198
Boris BREZILLONdc2948c2015-12-10 09:00:06 +0100199 nand_release(nand_to_mtd(&gpiomtd->nand_chip));
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200200
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200201 /* Enable write protection and disable the chip */
202 if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
203 gpiod_set_value(gpiomtd->nwp, 0);
204 if (gpiomtd->nce && !IS_ERR(gpiomtd->nce))
205 gpiod_set_value(gpiomtd->nce, 0);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200206
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200207 return 0;
208}
209
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400210static int gpio_nand_probe(struct platform_device *pdev)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200211{
212 struct gpiomtd *gpiomtd;
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400213 struct nand_chip *chip;
Boris BREZILLONdc2948c2015-12-10 09:00:06 +0100214 struct mtd_info *mtd;
Alexander Shiyan283df422013-05-06 17:53:48 +0400215 struct resource *res;
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200216 struct device *dev = &pdev->dev;
Jamie Iles775c32202011-12-18 10:00:49 +0000217 int ret = 0;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200218
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200219 if (!dev->of_node && !dev_get_platdata(dev))
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200220 return -EINVAL;
221
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200222 gpiomtd = devm_kzalloc(dev, sizeof(*gpiomtd), GFP_KERNEL);
Jingoo Han24e99712013-12-26 12:17:42 +0900223 if (!gpiomtd)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200224 return -ENOMEM;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200225
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400226 chip = &gpiomtd->nand_chip;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200227
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400228 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200229 chip->IO_ADDR_R = devm_ioremap_resource(dev, res);
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400230 if (IS_ERR(chip->IO_ADDR_R))
231 return PTR_ERR(chip->IO_ADDR_R);
Alexander Shiyan283df422013-05-06 17:53:48 +0400232
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400233 res = gpio_nand_get_io_sync(pdev);
Alexander Shiyan283df422013-05-06 17:53:48 +0400234 if (res) {
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200235 gpiomtd->io_sync = devm_ioremap_resource(dev, res);
Alexander Shiyan283df422013-05-06 17:53:48 +0400236 if (IS_ERR(gpiomtd->io_sync))
237 return PTR_ERR(gpiomtd->io_sync);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200238 }
239
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200240 ret = gpio_nand_get_config(dev, &gpiomtd->plat);
Jamie Iles775c32202011-12-18 10:00:49 +0000241 if (ret)
Alexander Shiyan283df422013-05-06 17:53:48 +0400242 return ret;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200243
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200244 /* Just enable the chip */
245 gpiomtd->nce = devm_gpiod_get_optional(dev, "nce", GPIOD_OUT_HIGH);
246 if (IS_ERR(gpiomtd->nce))
247 return PTR_ERR(gpiomtd->nce);
248
249 /* We disable write protection once we know probe() will succeed */
250 gpiomtd->nwp = devm_gpiod_get_optional(dev, "nwp", GPIOD_OUT_LOW);
251 if (IS_ERR(gpiomtd->nwp)) {
252 ret = PTR_ERR(gpiomtd->nwp);
253 goto out_ce;
Christophe Leroy44dd1822017-02-10 15:01:10 +0100254 }
Alexander Shiyan283df422013-05-06 17:53:48 +0400255
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200256 gpiomtd->nwp = devm_gpiod_get(dev, "ale", GPIOD_OUT_LOW);
257 if (IS_ERR(gpiomtd->nwp)) {
258 ret = PTR_ERR(gpiomtd->nwp);
259 goto out_ce;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200260 }
Alexander Shiyan283df422013-05-06 17:53:48 +0400261
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200262 gpiomtd->cle = devm_gpiod_get(dev, "cle", GPIOD_OUT_LOW);
263 if (IS_ERR(gpiomtd->cle)) {
264 ret = PTR_ERR(gpiomtd->cle);
265 goto out_ce;
266 }
Alexander Shiyan283df422013-05-06 17:53:48 +0400267
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200268 gpiomtd->rdy = devm_gpiod_get_optional(dev, "rdy", GPIOD_IN);
269 if (IS_ERR(gpiomtd->rdy)) {
270 ret = PTR_ERR(gpiomtd->rdy);
271 goto out_ce;
272 }
273 /* Using RDY pin */
274 if (gpiomtd->rdy)
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400275 chip->dev_ready = gpio_nand_devready;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200276
Brian Norrisa61ae812015-10-30 20:33:25 -0700277 nand_set_flash_node(chip, pdev->dev.of_node);
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400278 chip->IO_ADDR_W = chip->IO_ADDR_R;
279 chip->ecc.mode = NAND_ECC_SOFT;
Rafał Miłecki050658c2016-04-08 12:23:45 +0200280 chip->ecc.algo = NAND_ECC_HAMMING;
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400281 chip->options = gpiomtd->plat.options;
282 chip->chip_delay = gpiomtd->plat.chip_delay;
283 chip->cmd_ctrl = gpio_nand_cmd_ctrl;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200284
Boris BREZILLONdc2948c2015-12-10 09:00:06 +0100285 mtd = nand_to_mtd(chip);
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200286 mtd->dev.parent = dev;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200287
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400288 platform_set_drvdata(pdev, gpiomtd);
Alexander Shiyan283df422013-05-06 17:53:48 +0400289
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200290 /* Disable write protection, if wired up */
291 if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
292 gpiod_direction_output(gpiomtd->nwp, 1);
Alexander Shiyan283df422013-05-06 17:53:48 +0400293
Masahiro Yamada408bf512016-11-04 19:42:52 +0900294 ret = nand_scan(mtd, 1);
295 if (ret)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200296 goto err_wp;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200297
298 if (gpiomtd->plat.adjust_parts)
Boris BREZILLONdc2948c2015-12-10 09:00:06 +0100299 gpiomtd->plat.adjust_parts(&gpiomtd->plat, mtd->size);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200300
Boris BREZILLONdc2948c2015-12-10 09:00:06 +0100301 ret = mtd_device_register(mtd, gpiomtd->plat.parts,
Brian Norrisa61ae812015-10-30 20:33:25 -0700302 gpiomtd->plat.num_parts);
Alexander Shiyan283df422013-05-06 17:53:48 +0400303 if (!ret)
304 return 0;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200305
306err_wp:
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200307 if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
308 gpiod_set_value(gpiomtd->nwp, 0);
309out_ce:
310 if (gpiomtd->nce && !IS_ERR(gpiomtd->nce))
311 gpiod_set_value(gpiomtd->nce, 0);
Alexander Shiyan283df422013-05-06 17:53:48 +0400312
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200313 return ret;
314}
315
316static struct platform_driver gpio_nand_driver = {
317 .probe = gpio_nand_probe,
318 .remove = gpio_nand_remove,
319 .driver = {
320 .name = "gpio-nand",
Sachin Kamatb57d43f2013-03-14 15:37:03 +0530321 .of_match_table = of_match_ptr(gpio_nand_id_table),
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200322 },
323};
324
Sachin Kamat2fe87ae2012-09-05 15:31:32 +0530325module_platform_driver(gpio_nand_driver);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200326
327MODULE_LICENSE("GPL");
328MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
329MODULE_DESCRIPTION("GPIO NAND Driver");