blob: a6c9a824a7d415fae44d9f7a228db87b915df556 [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
Boris Brezillon0f808c12018-09-06 14:05:26 +020076static void gpio_nand_cmd_ctrl(struct nand_chip *chip, int cmd,
77 unsigned int ctrl)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020078{
Boris Brezillon0f808c12018-09-06 14:05:26 +020079 struct gpiomtd *gpiomtd = gpio_nand_getpriv(nand_to_mtd(chip));
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020080
81 gpio_nand_dosync(gpiomtd);
82
83 if (ctrl & NAND_CTRL_CHANGE) {
Linus Walleijf3d0d8d2017-09-24 19:39:12 +020084 if (gpiomtd->nce)
85 gpiod_set_value(gpiomtd->nce, !(ctrl & NAND_NCE));
86 gpiod_set_value(gpiomtd->cle, !!(ctrl & NAND_CLE));
87 gpiod_set_value(gpiomtd->ale, !!(ctrl & NAND_ALE));
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020088 gpio_nand_dosync(gpiomtd);
89 }
90 if (cmd == NAND_CMD_NONE)
91 return;
92
Boris Brezillon82fc5092018-09-07 00:38:34 +020093 writeb(cmd, gpiomtd->nand_chip.legacy.IO_ADDR_W);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020094 gpio_nand_dosync(gpiomtd);
95}
96
Boris Brezillon50a487e2018-09-06 14:05:27 +020097static int gpio_nand_devready(struct nand_chip *chip)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020098{
Boris Brezillon50a487e2018-09-06 14:05:27 +020099 struct gpiomtd *gpiomtd = gpio_nand_getpriv(nand_to_mtd(chip));
Alexander Shiyan18afbc52012-10-17 10:08:27 +0400100
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200101 return gpiod_get_value(gpiomtd->rdy);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200102}
103
Jamie Iles775c32202011-12-18 10:00:49 +0000104#ifdef CONFIG_OF
105static const struct of_device_id gpio_nand_id_table[] = {
106 { .compatible = "gpio-control-nand" },
107 {}
108};
109MODULE_DEVICE_TABLE(of, gpio_nand_id_table);
110
111static int gpio_nand_get_config_of(const struct device *dev,
112 struct gpio_nand_platdata *plat)
113{
114 u32 val;
115
Alexander Shiyanee4f3662013-05-06 17:53:50 +0400116 if (!dev->of_node)
117 return -ENODEV;
118
Jamie Iles775c32202011-12-18 10:00:49 +0000119 if (!of_property_read_u32(dev->of_node, "bank-width", &val)) {
120 if (val == 2) {
121 plat->options |= NAND_BUSWIDTH_16;
122 } else if (val != 1) {
123 dev_err(dev, "invalid bank-width %u\n", val);
124 return -EINVAL;
125 }
126 }
127
Jamie Iles775c32202011-12-18 10:00:49 +0000128 if (!of_property_read_u32(dev->of_node, "chip-delay", &val))
129 plat->chip_delay = val;
130
131 return 0;
132}
133
134static struct resource *gpio_nand_get_io_sync_of(struct platform_device *pdev)
135{
Brian Norris103cdd82013-12-13 21:19:58 -0800136 struct resource *r;
Jamie Iles775c32202011-12-18 10:00:49 +0000137 u64 addr;
138
Brian Norris103cdd82013-12-13 21:19:58 -0800139 if (of_property_read_u64(pdev->dev.of_node,
Jamie Iles775c32202011-12-18 10:00:49 +0000140 "gpio-control-nand,io-sync-reg", &addr))
141 return NULL;
142
Brian Norris103cdd82013-12-13 21:19:58 -0800143 r = devm_kzalloc(&pdev->dev, sizeof(*r), GFP_KERNEL);
144 if (!r)
145 return NULL;
146
Jamie Iles775c32202011-12-18 10:00:49 +0000147 r->start = addr;
148 r->end = r->start + 0x3;
149 r->flags = IORESOURCE_MEM;
150
151 return r;
152}
153#else /* CONFIG_OF */
Jamie Iles775c32202011-12-18 10:00:49 +0000154static inline int gpio_nand_get_config_of(const struct device *dev,
155 struct gpio_nand_platdata *plat)
156{
157 return -ENOSYS;
158}
159
160static inline struct resource *
161gpio_nand_get_io_sync_of(struct platform_device *pdev)
162{
163 return NULL;
164}
165#endif /* CONFIG_OF */
166
167static inline int gpio_nand_get_config(const struct device *dev,
168 struct gpio_nand_platdata *plat)
169{
170 int ret = gpio_nand_get_config_of(dev, plat);
171
172 if (!ret)
173 return ret;
174
Jingoo Han453810b2013-07-30 17:18:33 +0900175 if (dev_get_platdata(dev)) {
176 memcpy(plat, dev_get_platdata(dev), sizeof(*plat));
Jamie Iles775c32202011-12-18 10:00:49 +0000177 return 0;
178 }
179
180 return -EINVAL;
181}
182
183static inline struct resource *
184gpio_nand_get_io_sync(struct platform_device *pdev)
185{
186 struct resource *r = gpio_nand_get_io_sync_of(pdev);
187
188 if (r)
189 return r;
190
191 return platform_get_resource(pdev, IORESOURCE_MEM, 1);
192}
193
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400194static int gpio_nand_remove(struct platform_device *pdev)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200195{
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400196 struct gpiomtd *gpiomtd = platform_get_drvdata(pdev);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200197
Boris Brezillon59ac2762018-09-06 14:05:15 +0200198 nand_release(&gpiomtd->nand_chip);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200199
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200200 /* Enable write protection and disable the chip */
201 if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
202 gpiod_set_value(gpiomtd->nwp, 0);
203 if (gpiomtd->nce && !IS_ERR(gpiomtd->nce))
204 gpiod_set_value(gpiomtd->nce, 0);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200205
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200206 return 0;
207}
208
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400209static int gpio_nand_probe(struct platform_device *pdev)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200210{
211 struct gpiomtd *gpiomtd;
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400212 struct nand_chip *chip;
Boris BREZILLONdc2948c2015-12-10 09:00:06 +0100213 struct mtd_info *mtd;
Alexander Shiyan283df422013-05-06 17:53:48 +0400214 struct resource *res;
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200215 struct device *dev = &pdev->dev;
Jamie Iles775c32202011-12-18 10:00:49 +0000216 int ret = 0;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200217
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200218 if (!dev->of_node && !dev_get_platdata(dev))
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200219 return -EINVAL;
220
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200221 gpiomtd = devm_kzalloc(dev, sizeof(*gpiomtd), GFP_KERNEL);
Jingoo Han24e99712013-12-26 12:17:42 +0900222 if (!gpiomtd)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200223 return -ENOMEM;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200224
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400225 chip = &gpiomtd->nand_chip;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200226
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400227 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Boris Brezillon82fc5092018-09-07 00:38:34 +0200228 chip->legacy.IO_ADDR_R = devm_ioremap_resource(dev, res);
229 if (IS_ERR(chip->legacy.IO_ADDR_R))
230 return PTR_ERR(chip->legacy.IO_ADDR_R);
Alexander Shiyan283df422013-05-06 17:53:48 +0400231
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400232 res = gpio_nand_get_io_sync(pdev);
Alexander Shiyan283df422013-05-06 17:53:48 +0400233 if (res) {
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200234 gpiomtd->io_sync = devm_ioremap_resource(dev, res);
Alexander Shiyan283df422013-05-06 17:53:48 +0400235 if (IS_ERR(gpiomtd->io_sync))
236 return PTR_ERR(gpiomtd->io_sync);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200237 }
238
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200239 ret = gpio_nand_get_config(dev, &gpiomtd->plat);
Jamie Iles775c32202011-12-18 10:00:49 +0000240 if (ret)
Alexander Shiyan283df422013-05-06 17:53:48 +0400241 return ret;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200242
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200243 /* Just enable the chip */
244 gpiomtd->nce = devm_gpiod_get_optional(dev, "nce", GPIOD_OUT_HIGH);
245 if (IS_ERR(gpiomtd->nce))
246 return PTR_ERR(gpiomtd->nce);
247
248 /* We disable write protection once we know probe() will succeed */
249 gpiomtd->nwp = devm_gpiod_get_optional(dev, "nwp", GPIOD_OUT_LOW);
250 if (IS_ERR(gpiomtd->nwp)) {
251 ret = PTR_ERR(gpiomtd->nwp);
252 goto out_ce;
Christophe Leroy44dd1822017-02-10 15:01:10 +0100253 }
Alexander Shiyan283df422013-05-06 17:53:48 +0400254
Christophe Leroybc2fd1b2017-12-06 18:27:14 +0100255 gpiomtd->ale = devm_gpiod_get(dev, "ale", GPIOD_OUT_LOW);
256 if (IS_ERR(gpiomtd->ale)) {
257 ret = PTR_ERR(gpiomtd->ale);
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200258 goto out_ce;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200259 }
Alexander Shiyan283df422013-05-06 17:53:48 +0400260
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200261 gpiomtd->cle = devm_gpiod_get(dev, "cle", GPIOD_OUT_LOW);
262 if (IS_ERR(gpiomtd->cle)) {
263 ret = PTR_ERR(gpiomtd->cle);
264 goto out_ce;
265 }
Alexander Shiyan283df422013-05-06 17:53:48 +0400266
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200267 gpiomtd->rdy = devm_gpiod_get_optional(dev, "rdy", GPIOD_IN);
268 if (IS_ERR(gpiomtd->rdy)) {
269 ret = PTR_ERR(gpiomtd->rdy);
270 goto out_ce;
271 }
272 /* Using RDY pin */
273 if (gpiomtd->rdy)
Boris Brezillon8395b752018-09-07 00:38:37 +0200274 chip->legacy.dev_ready = gpio_nand_devready;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200275
Brian Norrisa61ae812015-10-30 20:33:25 -0700276 nand_set_flash_node(chip, pdev->dev.of_node);
Boris Brezillon82fc5092018-09-07 00:38:34 +0200277 chip->legacy.IO_ADDR_W = chip->legacy.IO_ADDR_R;
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400278 chip->ecc.mode = NAND_ECC_SOFT;
Rafał Miłecki050658c2016-04-08 12:23:45 +0200279 chip->ecc.algo = NAND_ECC_HAMMING;
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400280 chip->options = gpiomtd->plat.options;
Boris Brezillon3cece3a2018-09-07 00:38:41 +0200281 chip->legacy.chip_delay = gpiomtd->plat.chip_delay;
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200282 chip->legacy.cmd_ctrl = gpio_nand_cmd_ctrl;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200283
Boris BREZILLONdc2948c2015-12-10 09:00:06 +0100284 mtd = nand_to_mtd(chip);
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200285 mtd->dev.parent = dev;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200286
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400287 platform_set_drvdata(pdev, gpiomtd);
Alexander Shiyan283df422013-05-06 17:53:48 +0400288
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200289 /* Disable write protection, if wired up */
290 if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
291 gpiod_direction_output(gpiomtd->nwp, 1);
Alexander Shiyan283df422013-05-06 17:53:48 +0400292
Boris Brezillon00ad3782018-09-06 14:05:14 +0200293 ret = nand_scan(chip, 1);
Masahiro Yamada408bf512016-11-04 19:42:52 +0900294 if (ret)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200295 goto err_wp;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200296
297 if (gpiomtd->plat.adjust_parts)
Boris BREZILLONdc2948c2015-12-10 09:00:06 +0100298 gpiomtd->plat.adjust_parts(&gpiomtd->plat, mtd->size);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200299
Boris BREZILLONdc2948c2015-12-10 09:00:06 +0100300 ret = mtd_device_register(mtd, gpiomtd->plat.parts,
Brian Norrisa61ae812015-10-30 20:33:25 -0700301 gpiomtd->plat.num_parts);
Alexander Shiyan283df422013-05-06 17:53:48 +0400302 if (!ret)
303 return 0;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200304
305err_wp:
Linus Walleijf3d0d8d2017-09-24 19:39:12 +0200306 if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
307 gpiod_set_value(gpiomtd->nwp, 0);
308out_ce:
309 if (gpiomtd->nce && !IS_ERR(gpiomtd->nce))
310 gpiod_set_value(gpiomtd->nce, 0);
Alexander Shiyan283df422013-05-06 17:53:48 +0400311
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200312 return ret;
313}
314
315static struct platform_driver gpio_nand_driver = {
316 .probe = gpio_nand_probe,
317 .remove = gpio_nand_remove,
318 .driver = {
319 .name = "gpio-nand",
Sachin Kamatb57d43f2013-03-14 15:37:03 +0530320 .of_match_table = of_match_ptr(gpio_nand_id_table),
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200321 },
322};
323
Sachin Kamat2fe87ae2012-09-05 15:31:32 +0530324module_platform_driver(gpio_nand_driver);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200325
326MODULE_LICENSE("GPL");
327MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
328MODULE_DESCRIPTION("GPIO NAND Driver");