blob: 0d26f8b7d9d0af4f02342ed74ac77c247482b1a3 [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 *
11 * Device driver for NAND connected via GPIO
12 *
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/init.h>
22#include <linux/slab.h>
23#include <linux/module.h>
24#include <linux/platform_device.h>
25#include <linux/gpio.h>
26#include <linux/io.h>
27#include <linux/mtd/mtd.h>
28#include <linux/mtd/nand.h>
29#include <linux/mtd/partitions.h>
30#include <linux/mtd/nand-gpio.h>
Jamie Iles775c32202011-12-18 10:00:49 +000031#include <linux/of.h>
32#include <linux/of_address.h>
33#include <linux/of_gpio.h>
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020034
35struct gpiomtd {
36 void __iomem *io_sync;
37 struct mtd_info mtd_info;
38 struct nand_chip nand_chip;
39 struct gpio_nand_platdata plat;
40};
41
42#define gpio_nand_getpriv(x) container_of(x, struct gpiomtd, mtd_info)
43
44
45#ifdef CONFIG_ARM
46/* gpio_nand_dosync()
47 *
48 * Make sure the GPIO state changes occur in-order with writes to NAND
49 * memory region.
50 * Needed on PXA due to bus-reordering within the SoC itself (see section on
51 * I/O ordering in PXA manual (section 2.3, p35)
52 */
53static void gpio_nand_dosync(struct gpiomtd *gpiomtd)
54{
55 unsigned long tmp;
56
57 if (gpiomtd->io_sync) {
58 /*
59 * Linux memory barriers don't cater for what's required here.
60 * What's required is what's here - a read from a separate
61 * region with a dependency on that read.
62 */
63 tmp = readl(gpiomtd->io_sync);
64 asm volatile("mov %1, %0\n" : "=r" (tmp) : "r" (tmp));
65 }
66}
67#else
68static inline void gpio_nand_dosync(struct gpiomtd *gpiomtd) {}
69#endif
70
71static void gpio_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
72{
73 struct gpiomtd *gpiomtd = gpio_nand_getpriv(mtd);
74
75 gpio_nand_dosync(gpiomtd);
76
77 if (ctrl & NAND_CTRL_CHANGE) {
78 gpio_set_value(gpiomtd->plat.gpio_nce, !(ctrl & NAND_NCE));
79 gpio_set_value(gpiomtd->plat.gpio_cle, !!(ctrl & NAND_CLE));
80 gpio_set_value(gpiomtd->plat.gpio_ale, !!(ctrl & NAND_ALE));
81 gpio_nand_dosync(gpiomtd);
82 }
83 if (cmd == NAND_CMD_NONE)
84 return;
85
86 writeb(cmd, gpiomtd->nand_chip.IO_ADDR_W);
87 gpio_nand_dosync(gpiomtd);
88}
89
90static void gpio_nand_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
91{
92 struct nand_chip *this = mtd->priv;
93
Matthew Leach82045362012-12-10 19:12:39 +000094 iowrite8_rep(this->IO_ADDR_W, buf, len);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020095}
96
97static void gpio_nand_readbuf(struct mtd_info *mtd, u_char *buf, int len)
98{
99 struct nand_chip *this = mtd->priv;
100
Matthew Leach82045362012-12-10 19:12:39 +0000101 ioread8_rep(this->IO_ADDR_R, buf, len);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200102}
103
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200104static void gpio_nand_writebuf16(struct mtd_info *mtd, const u_char *buf,
105 int len)
106{
107 struct nand_chip *this = mtd->priv;
108
109 if (IS_ALIGNED((unsigned long)buf, 2)) {
Matthew Leach82045362012-12-10 19:12:39 +0000110 iowrite16_rep(this->IO_ADDR_W, buf, len>>1);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200111 } else {
112 int i;
113 unsigned short *ptr = (unsigned short *)buf;
114
115 for (i = 0; i < len; i += 2, ptr++)
116 writew(*ptr, this->IO_ADDR_W);
117 }
118}
119
120static void gpio_nand_readbuf16(struct mtd_info *mtd, u_char *buf, int len)
121{
122 struct nand_chip *this = mtd->priv;
123
124 if (IS_ALIGNED((unsigned long)buf, 2)) {
Matthew Leach82045362012-12-10 19:12:39 +0000125 ioread16_rep(this->IO_ADDR_R, buf, len>>1);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200126 } else {
127 int i;
128 unsigned short *ptr = (unsigned short *)buf;
129
130 for (i = 0; i < len; i += 2, ptr++)
131 *ptr = readw(this->IO_ADDR_R);
132 }
133}
134
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200135static int gpio_nand_devready(struct mtd_info *mtd)
136{
137 struct gpiomtd *gpiomtd = gpio_nand_getpriv(mtd);
Alexander Shiyan18afbc52012-10-17 10:08:27 +0400138
Alexander Shiyanc85d32d52013-05-06 17:53:49 +0400139 return gpio_get_value(gpiomtd->plat.gpio_rdy);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200140}
141
Jamie Iles775c32202011-12-18 10:00:49 +0000142#ifdef CONFIG_OF
143static const struct of_device_id gpio_nand_id_table[] = {
144 { .compatible = "gpio-control-nand" },
145 {}
146};
147MODULE_DEVICE_TABLE(of, gpio_nand_id_table);
148
149static int gpio_nand_get_config_of(const struct device *dev,
150 struct gpio_nand_platdata *plat)
151{
152 u32 val;
153
Alexander Shiyanee4f3662013-05-06 17:53:50 +0400154 if (!dev->of_node)
155 return -ENODEV;
156
Jamie Iles775c32202011-12-18 10:00:49 +0000157 if (!of_property_read_u32(dev->of_node, "bank-width", &val)) {
158 if (val == 2) {
159 plat->options |= NAND_BUSWIDTH_16;
160 } else if (val != 1) {
161 dev_err(dev, "invalid bank-width %u\n", val);
162 return -EINVAL;
163 }
164 }
165
166 plat->gpio_rdy = of_get_gpio(dev->of_node, 0);
167 plat->gpio_nce = of_get_gpio(dev->of_node, 1);
168 plat->gpio_ale = of_get_gpio(dev->of_node, 2);
169 plat->gpio_cle = of_get_gpio(dev->of_node, 3);
170 plat->gpio_nwp = of_get_gpio(dev->of_node, 4);
171
172 if (!of_property_read_u32(dev->of_node, "chip-delay", &val))
173 plat->chip_delay = val;
174
175 return 0;
176}
177
178static struct resource *gpio_nand_get_io_sync_of(struct platform_device *pdev)
179{
180 struct resource *r = devm_kzalloc(&pdev->dev, sizeof(*r), GFP_KERNEL);
181 u64 addr;
182
183 if (!r || of_property_read_u64(pdev->dev.of_node,
184 "gpio-control-nand,io-sync-reg", &addr))
185 return NULL;
186
187 r->start = addr;
188 r->end = r->start + 0x3;
189 r->flags = IORESOURCE_MEM;
190
191 return r;
192}
193#else /* CONFIG_OF */
Jamie Iles775c32202011-12-18 10:00:49 +0000194static inline int gpio_nand_get_config_of(const struct device *dev,
195 struct gpio_nand_platdata *plat)
196{
197 return -ENOSYS;
198}
199
200static inline struct resource *
201gpio_nand_get_io_sync_of(struct platform_device *pdev)
202{
203 return NULL;
204}
205#endif /* CONFIG_OF */
206
207static inline int gpio_nand_get_config(const struct device *dev,
208 struct gpio_nand_platdata *plat)
209{
210 int ret = gpio_nand_get_config_of(dev, plat);
211
212 if (!ret)
213 return ret;
214
215 if (dev->platform_data) {
216 memcpy(plat, dev->platform_data, sizeof(*plat));
217 return 0;
218 }
219
220 return -EINVAL;
221}
222
223static inline struct resource *
224gpio_nand_get_io_sync(struct platform_device *pdev)
225{
226 struct resource *r = gpio_nand_get_io_sync_of(pdev);
227
228 if (r)
229 return r;
230
231 return platform_get_resource(pdev, IORESOURCE_MEM, 1);
232}
233
Bill Pemberton810b7e02012-11-19 13:26:04 -0500234static int gpio_nand_remove(struct platform_device *dev)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200235{
236 struct gpiomtd *gpiomtd = platform_get_drvdata(dev);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200237
238 nand_release(&gpiomtd->mtd_info);
239
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200240 if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
241 gpio_set_value(gpiomtd->plat.gpio_nwp, 0);
242 gpio_set_value(gpiomtd->plat.gpio_nce, 1);
243
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200244 return 0;
245}
246
Bill Pemberton06f25512012-11-19 13:23:07 -0500247static int gpio_nand_probe(struct platform_device *dev)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200248{
249 struct gpiomtd *gpiomtd;
250 struct nand_chip *this;
Alexander Shiyan283df422013-05-06 17:53:48 +0400251 struct resource *res;
Jamie Iles775c32202011-12-18 10:00:49 +0000252 struct mtd_part_parser_data ppdata = {};
253 int ret = 0;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200254
Jamie Iles775c32202011-12-18 10:00:49 +0000255 if (!dev->dev.of_node && !dev->dev.platform_data)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200256 return -EINVAL;
257
Sachin Kamatb60c7242013-03-14 15:37:02 +0530258 gpiomtd = devm_kzalloc(&dev->dev, sizeof(*gpiomtd), GFP_KERNEL);
Alexander Shiyan283df422013-05-06 17:53:48 +0400259 if (!gpiomtd) {
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200260 dev_err(&dev->dev, "failed to create NAND MTD\n");
261 return -ENOMEM;
262 }
263
264 this = &gpiomtd->nand_chip;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200265
Alexander Shiyan283df422013-05-06 17:53:48 +0400266 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
267 this->IO_ADDR_R = devm_ioremap_resource(&dev->dev, res);
268 if (IS_ERR(this->IO_ADDR_R))
269 return PTR_ERR(this->IO_ADDR_R);
270
271 res = gpio_nand_get_io_sync(dev);
272 if (res) {
273 gpiomtd->io_sync = devm_ioremap_resource(&dev->dev, res);
274 if (IS_ERR(gpiomtd->io_sync))
275 return PTR_ERR(gpiomtd->io_sync);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200276 }
277
Jamie Iles775c32202011-12-18 10:00:49 +0000278 ret = gpio_nand_get_config(&dev->dev, &gpiomtd->plat);
279 if (ret)
Alexander Shiyan283df422013-05-06 17:53:48 +0400280 return ret;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200281
Alexander Shiyan283df422013-05-06 17:53:48 +0400282 ret = devm_gpio_request(&dev->dev, gpiomtd->plat.gpio_nce, "NAND NCE");
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200283 if (ret)
Alexander Shiyan283df422013-05-06 17:53:48 +0400284 return ret;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200285 gpio_direction_output(gpiomtd->plat.gpio_nce, 1);
Alexander Shiyan283df422013-05-06 17:53:48 +0400286
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200287 if (gpio_is_valid(gpiomtd->plat.gpio_nwp)) {
Alexander Shiyan283df422013-05-06 17:53:48 +0400288 ret = devm_gpio_request(&dev->dev, gpiomtd->plat.gpio_nwp,
289 "NAND NWP");
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200290 if (ret)
Alexander Shiyan283df422013-05-06 17:53:48 +0400291 return ret;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200292 }
Alexander Shiyan283df422013-05-06 17:53:48 +0400293
294 ret = devm_gpio_request(&dev->dev, gpiomtd->plat.gpio_ale, "NAND ALE");
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200295 if (ret)
Alexander Shiyan283df422013-05-06 17:53:48 +0400296 return ret;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200297 gpio_direction_output(gpiomtd->plat.gpio_ale, 0);
Alexander Shiyan283df422013-05-06 17:53:48 +0400298
299 ret = devm_gpio_request(&dev->dev, gpiomtd->plat.gpio_cle, "NAND CLE");
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200300 if (ret)
Alexander Shiyan283df422013-05-06 17:53:48 +0400301 return ret;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200302 gpio_direction_output(gpiomtd->plat.gpio_cle, 0);
Alexander Shiyan283df422013-05-06 17:53:48 +0400303
Alexander Shiyan18afbc52012-10-17 10:08:27 +0400304 if (gpio_is_valid(gpiomtd->plat.gpio_rdy)) {
Alexander Shiyan283df422013-05-06 17:53:48 +0400305 ret = devm_gpio_request(&dev->dev, gpiomtd->plat.gpio_rdy,
306 "NAND RDY");
Alexander Shiyan18afbc52012-10-17 10:08:27 +0400307 if (ret)
Alexander Shiyan283df422013-05-06 17:53:48 +0400308 return ret;
Alexander Shiyan18afbc52012-10-17 10:08:27 +0400309 gpio_direction_input(gpiomtd->plat.gpio_rdy);
Alexander Shiyanc85d32d52013-05-06 17:53:49 +0400310 this->dev_ready = gpio_nand_devready;
Alexander Shiyan18afbc52012-10-17 10:08:27 +0400311 }
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200312
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200313 this->IO_ADDR_W = this->IO_ADDR_R;
314 this->ecc.mode = NAND_ECC_SOFT;
315 this->options = gpiomtd->plat.options;
316 this->chip_delay = gpiomtd->plat.chip_delay;
317
318 /* install our routines */
319 this->cmd_ctrl = gpio_nand_cmd_ctrl;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200320
321 if (this->options & NAND_BUSWIDTH_16) {
322 this->read_buf = gpio_nand_readbuf16;
323 this->write_buf = gpio_nand_writebuf16;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200324 } else {
325 this->read_buf = gpio_nand_readbuf;
326 this->write_buf = gpio_nand_writebuf;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200327 }
328
329 /* set the mtd private data for the nand driver */
330 gpiomtd->mtd_info.priv = this;
331 gpiomtd->mtd_info.owner = THIS_MODULE;
332
Alexander Shiyan283df422013-05-06 17:53:48 +0400333 platform_set_drvdata(dev, gpiomtd);
334
335 if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
336 gpio_direction_output(gpiomtd->plat.gpio_nwp, 1);
337
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200338 if (nand_scan(&gpiomtd->mtd_info, 1)) {
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200339 ret = -ENXIO;
340 goto err_wp;
341 }
342
343 if (gpiomtd->plat.adjust_parts)
344 gpiomtd->plat.adjust_parts(&gpiomtd->plat,
345 gpiomtd->mtd_info.size);
346
Jamie Iles775c32202011-12-18 10:00:49 +0000347 ppdata.of_node = dev->dev.of_node;
348 ret = mtd_device_parse_register(&gpiomtd->mtd_info, NULL, &ppdata,
349 gpiomtd->plat.parts,
350 gpiomtd->plat.num_parts);
Alexander Shiyan283df422013-05-06 17:53:48 +0400351 if (!ret)
352 return 0;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200353
354err_wp:
355 if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
356 gpio_set_value(gpiomtd->plat.gpio_nwp, 0);
Alexander Shiyan283df422013-05-06 17:53:48 +0400357
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200358 return ret;
359}
360
361static struct platform_driver gpio_nand_driver = {
362 .probe = gpio_nand_probe,
363 .remove = gpio_nand_remove,
364 .driver = {
365 .name = "gpio-nand",
Sachin Kamatb57d43f2013-03-14 15:37:03 +0530366 .of_match_table = of_match_ptr(gpio_nand_id_table),
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200367 },
368};
369
Sachin Kamat2fe87ae2012-09-05 15:31:32 +0530370module_platform_driver(gpio_nand_driver);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200371
372MODULE_LICENSE("GPL");
373MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
374MODULE_DESCRIPTION("GPIO NAND Driver");