blob: 0e7d00faf33ca89df99358aea8a95c7010a89fec [file] [log] [blame]
Mike Rapoportaaf7ea22008-10-15 08:38:49 +02001/*
Mike Rapoportaaf7ea22008-10-15 08:38:49 +02002 * Updated, and converted to generic GPIO based driver by Russell King.
3 *
4 * Written by Ben Dooks <ben@simtec.co.uk>
5 * Based on 2.4 version by Mark Whittaker
6 *
7 * © 2004 Simtec Electronics
8 *
Gerhard Sittigc9d79c42014-08-05 10:37:26 +02009 * Device driver for NAND flash that uses a memory mapped interface to
10 * read/write the NAND commands and data, and GPIO pins for control signals
11 * (the DT binding refers to this as "GPIO assisted NAND flash")
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020012 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 *
17 */
18
19#include <linux/kernel.h>
Alexander Shiyan283df422013-05-06 17:53:48 +040020#include <linux/err.h>
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020021#include <linux/slab.h>
22#include <linux/module.h>
23#include <linux/platform_device.h>
Linus Walleijf3d0d8d2017-09-24 19:39:12 +020024#include <linux/gpio/consumer.h>
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020025#include <linux/io.h>
26#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020027#include <linux/mtd/rawnand.h>
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020028#include <linux/mtd/partitions.h>
29#include <linux/mtd/nand-gpio.h>
Jamie Iles775c32202011-12-18 10:00:49 +000030#include <linux/of.h>
31#include <linux/of_address.h>
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020032
33struct gpiomtd {
34 void __iomem *io_sync;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020035 struct nand_chip nand_chip;
36 struct gpio_nand_platdata plat;
Linus Walleijf3d0d8d2017-09-24 19:39:12 +020037 struct gpio_desc *nce; /* Optional chip enable */
38 struct gpio_desc *cle;
39 struct gpio_desc *ale;
40 struct gpio_desc *rdy;
41 struct gpio_desc *nwp; /* Optional write protection */
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020042};
43
Boris BREZILLONdc2948c2015-12-10 09:00:06 +010044static inline struct gpiomtd *gpio_nand_getpriv(struct mtd_info *mtd)
45{
46 return container_of(mtd_to_nand(mtd), struct gpiomtd, nand_chip);
47}
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020048
49
50#ifdef CONFIG_ARM
51/* gpio_nand_dosync()
52 *
53 * Make sure the GPIO state changes occur in-order with writes to NAND
54 * memory region.
55 * Needed on PXA due to bus-reordering within the SoC itself (see section on
56 * I/O ordering in PXA manual (section 2.3, p35)
57 */
58static void gpio_nand_dosync(struct gpiomtd *gpiomtd)
59{
60 unsigned long tmp;
61
62 if (gpiomtd->io_sync) {
63 /*
64 * Linux memory barriers don't cater for what's required here.
65 * What's required is what's here - a read from a separate
66 * region with a dependency on that read.
67 */
68 tmp = readl(gpiomtd->io_sync);
69 asm volatile("mov %1, %0\n" : "=r" (tmp) : "r" (tmp));
70 }
71}
72#else
73static inline void gpio_nand_dosync(struct gpiomtd *gpiomtd) {}
74#endif
75
76static void gpio_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
77{
78 struct gpiomtd *gpiomtd = gpio_nand_getpriv(mtd);
79
80 gpio_nand_dosync(gpiomtd);
81
82 if (ctrl & NAND_CTRL_CHANGE) {
Linus Walleijf3d0d8d2017-09-24 19:39:12 +020083 if (gpiomtd->nce)
84 gpiod_set_value(gpiomtd->nce, !(ctrl & NAND_NCE));
85 gpiod_set_value(gpiomtd->cle, !!(ctrl & NAND_CLE));
86 gpiod_set_value(gpiomtd->ale, !!(ctrl & NAND_ALE));
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020087 gpio_nand_dosync(gpiomtd);
88 }
89 if (cmd == NAND_CMD_NONE)
90 return;
91
92 writeb(cmd, gpiomtd->nand_chip.IO_ADDR_W);
93 gpio_nand_dosync(gpiomtd);
94}
95
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020096static int gpio_nand_devready(struct mtd_info *mtd)
97{
98 struct gpiomtd *gpiomtd = gpio_nand_getpriv(mtd);
Alexander Shiyan18afbc52012-10-17 10:08:27 +040099
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200100 return gpiod_get_value(gpiomtd->rdy);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200101}
102
Jamie Iles775c32202011-12-18 10:00:49 +0000103#ifdef CONFIG_OF
104static const struct of_device_id gpio_nand_id_table[] = {
105 { .compatible = "gpio-control-nand" },
106 {}
107};
108MODULE_DEVICE_TABLE(of, gpio_nand_id_table);
109
110static int gpio_nand_get_config_of(const struct device *dev,
111 struct gpio_nand_platdata *plat)
112{
113 u32 val;
114
Alexander Shiyanee4f3662013-05-06 17:53:50 +0400115 if (!dev->of_node)
116 return -ENODEV;
117
Jamie Iles775c32202011-12-18 10:00:49 +0000118 if (!of_property_read_u32(dev->of_node, "bank-width", &val)) {
119 if (val == 2) {
120 plat->options |= NAND_BUSWIDTH_16;
121 } else if (val != 1) {
122 dev_err(dev, "invalid bank-width %u\n", val);
123 return -EINVAL;
124 }
125 }
126
Jamie Iles775c32202011-12-18 10:00:49 +0000127 if (!of_property_read_u32(dev->of_node, "chip-delay", &val))
128 plat->chip_delay = val;
129
130 return 0;
131}
132
133static struct resource *gpio_nand_get_io_sync_of(struct platform_device *pdev)
134{
Brian Norris103cdd82013-12-13 21:19:58 -0800135 struct resource *r;
Jamie Iles775c32202011-12-18 10:00:49 +0000136 u64 addr;
137
Brian Norris103cdd82013-12-13 21:19:58 -0800138 if (of_property_read_u64(pdev->dev.of_node,
Jamie Iles775c32202011-12-18 10:00:49 +0000139 "gpio-control-nand,io-sync-reg", &addr))
140 return NULL;
141
Brian Norris103cdd82013-12-13 21:19:58 -0800142 r = devm_kzalloc(&pdev->dev, sizeof(*r), GFP_KERNEL);
143 if (!r)
144 return NULL;
145
Jamie Iles775c32202011-12-18 10:00:49 +0000146 r->start = addr;
147 r->end = r->start + 0x3;
148 r->flags = IORESOURCE_MEM;
149
150 return r;
151}
152#else /* CONFIG_OF */
Jamie Iles775c32202011-12-18 10:00:49 +0000153static inline int gpio_nand_get_config_of(const struct device *dev,
154 struct gpio_nand_platdata *plat)
155{
156 return -ENOSYS;
157}
158
159static inline struct resource *
160gpio_nand_get_io_sync_of(struct platform_device *pdev)
161{
162 return NULL;
163}
164#endif /* CONFIG_OF */
165
166static inline int gpio_nand_get_config(const struct device *dev,
167 struct gpio_nand_platdata *plat)
168{
169 int ret = gpio_nand_get_config_of(dev, plat);
170
171 if (!ret)
172 return ret;
173
Jingoo Han453810b2013-07-30 17:18:33 +0900174 if (dev_get_platdata(dev)) {
175 memcpy(plat, dev_get_platdata(dev), sizeof(*plat));
Jamie Iles775c32202011-12-18 10:00:49 +0000176 return 0;
177 }
178
179 return -EINVAL;
180}
181
182static inline struct resource *
183gpio_nand_get_io_sync(struct platform_device *pdev)
184{
185 struct resource *r = gpio_nand_get_io_sync_of(pdev);
186
187 if (r)
188 return r;
189
190 return platform_get_resource(pdev, IORESOURCE_MEM, 1);
191}
192
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400193static int gpio_nand_remove(struct platform_device *pdev)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200194{
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400195 struct gpiomtd *gpiomtd = platform_get_drvdata(pdev);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200196
Boris Brezillon59ac2762018-09-06 14:05:15 +0200197 nand_release(&gpiomtd->nand_chip);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200198
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200199 /* Enable write protection and disable the chip */
200 if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
201 gpiod_set_value(gpiomtd->nwp, 0);
202 if (gpiomtd->nce && !IS_ERR(gpiomtd->nce))
203 gpiod_set_value(gpiomtd->nce, 0);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200204
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200205 return 0;
206}
207
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400208static int gpio_nand_probe(struct platform_device *pdev)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200209{
210 struct gpiomtd *gpiomtd;
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400211 struct nand_chip *chip;
Boris BREZILLONdc2948c2015-12-10 09:00:06 +0100212 struct mtd_info *mtd;
Alexander Shiyan283df422013-05-06 17:53:48 +0400213 struct resource *res;
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200214 struct device *dev = &pdev->dev;
Jamie Iles775c32202011-12-18 10:00:49 +0000215 int ret = 0;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200216
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200217 if (!dev->of_node && !dev_get_platdata(dev))
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200218 return -EINVAL;
219
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200220 gpiomtd = devm_kzalloc(dev, sizeof(*gpiomtd), GFP_KERNEL);
Jingoo Han24e99712013-12-26 12:17:42 +0900221 if (!gpiomtd)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200222 return -ENOMEM;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200223
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400224 chip = &gpiomtd->nand_chip;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200225
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400226 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200227 chip->IO_ADDR_R = devm_ioremap_resource(dev, res);
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400228 if (IS_ERR(chip->IO_ADDR_R))
229 return PTR_ERR(chip->IO_ADDR_R);
Alexander Shiyan283df422013-05-06 17:53:48 +0400230
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400231 res = gpio_nand_get_io_sync(pdev);
Alexander Shiyan283df422013-05-06 17:53:48 +0400232 if (res) {
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200233 gpiomtd->io_sync = devm_ioremap_resource(dev, res);
Alexander Shiyan283df422013-05-06 17:53:48 +0400234 if (IS_ERR(gpiomtd->io_sync))
235 return PTR_ERR(gpiomtd->io_sync);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200236 }
237
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200238 ret = gpio_nand_get_config(dev, &gpiomtd->plat);
Jamie Iles775c32202011-12-18 10:00:49 +0000239 if (ret)
Alexander Shiyan283df422013-05-06 17:53:48 +0400240 return ret;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200241
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200242 /* Just enable the chip */
243 gpiomtd->nce = devm_gpiod_get_optional(dev, "nce", GPIOD_OUT_HIGH);
244 if (IS_ERR(gpiomtd->nce))
245 return PTR_ERR(gpiomtd->nce);
246
247 /* We disable write protection once we know probe() will succeed */
248 gpiomtd->nwp = devm_gpiod_get_optional(dev, "nwp", GPIOD_OUT_LOW);
249 if (IS_ERR(gpiomtd->nwp)) {
250 ret = PTR_ERR(gpiomtd->nwp);
251 goto out_ce;
Christophe Leroy44dd1822017-02-10 15:01:10 +0100252 }
Alexander Shiyan283df422013-05-06 17:53:48 +0400253
Christophe Leroybc2fd1b2017-12-06 18:27:14 +0100254 gpiomtd->ale = devm_gpiod_get(dev, "ale", GPIOD_OUT_LOW);
255 if (IS_ERR(gpiomtd->ale)) {
256 ret = PTR_ERR(gpiomtd->ale);
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200257 goto out_ce;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200258 }
Alexander Shiyan283df422013-05-06 17:53:48 +0400259
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200260 gpiomtd->cle = devm_gpiod_get(dev, "cle", GPIOD_OUT_LOW);
261 if (IS_ERR(gpiomtd->cle)) {
262 ret = PTR_ERR(gpiomtd->cle);
263 goto out_ce;
264 }
Alexander Shiyan283df422013-05-06 17:53:48 +0400265
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200266 gpiomtd->rdy = devm_gpiod_get_optional(dev, "rdy", GPIOD_IN);
267 if (IS_ERR(gpiomtd->rdy)) {
268 ret = PTR_ERR(gpiomtd->rdy);
269 goto out_ce;
270 }
271 /* Using RDY pin */
272 if (gpiomtd->rdy)
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400273 chip->dev_ready = gpio_nand_devready;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200274
Brian Norrisa61ae812015-10-30 20:33:25 -0700275 nand_set_flash_node(chip, pdev->dev.of_node);
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400276 chip->IO_ADDR_W = chip->IO_ADDR_R;
277 chip->ecc.mode = NAND_ECC_SOFT;
Rafał Miłecki050658c2016-04-08 12:23:45 +0200278 chip->ecc.algo = NAND_ECC_HAMMING;
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400279 chip->options = gpiomtd->plat.options;
280 chip->chip_delay = gpiomtd->plat.chip_delay;
281 chip->cmd_ctrl = gpio_nand_cmd_ctrl;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200282
Boris BREZILLONdc2948c2015-12-10 09:00:06 +0100283 mtd = nand_to_mtd(chip);
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200284 mtd->dev.parent = dev;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200285
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400286 platform_set_drvdata(pdev, gpiomtd);
Alexander Shiyan283df422013-05-06 17:53:48 +0400287
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200288 /* Disable write protection, if wired up */
289 if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
290 gpiod_direction_output(gpiomtd->nwp, 1);
Alexander Shiyan283df422013-05-06 17:53:48 +0400291
Boris Brezillon00ad3782018-09-06 14:05:14 +0200292 ret = nand_scan(chip, 1);
Masahiro Yamada408bf512016-11-04 19:42:52 +0900293 if (ret)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200294 goto err_wp;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200295
296 if (gpiomtd->plat.adjust_parts)
Boris BREZILLONdc2948c2015-12-10 09:00:06 +0100297 gpiomtd->plat.adjust_parts(&gpiomtd->plat, mtd->size);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200298
Boris BREZILLONdc2948c2015-12-10 09:00:06 +0100299 ret = mtd_device_register(mtd, gpiomtd->plat.parts,
Brian Norrisa61ae812015-10-30 20:33:25 -0700300 gpiomtd->plat.num_parts);
Alexander Shiyan283df422013-05-06 17:53:48 +0400301 if (!ret)
302 return 0;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200303
304err_wp:
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200305 if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
306 gpiod_set_value(gpiomtd->nwp, 0);
307out_ce:
308 if (gpiomtd->nce && !IS_ERR(gpiomtd->nce))
309 gpiod_set_value(gpiomtd->nce, 0);
Alexander Shiyan283df422013-05-06 17:53:48 +0400310
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200311 return ret;
312}
313
314static struct platform_driver gpio_nand_driver = {
315 .probe = gpio_nand_probe,
316 .remove = gpio_nand_remove,
317 .driver = {
318 .name = "gpio-nand",
Sachin Kamatb57d43f2013-03-14 15:37:03 +0530319 .of_match_table = of_match_ptr(gpio_nand_id_table),
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200320 },
321};
322
Sachin Kamat2fe87ae2012-09-05 15:31:32 +0530323module_platform_driver(gpio_nand_driver);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200324
325MODULE_LICENSE("GPL");
326MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
327MODULE_DESCRIPTION("GPIO NAND Driver");