Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / mtd / nand / gpio.c
bloba8bde6665c24f7e20e6103959ceee16c5d3ec5c8
1 /*
2 * drivers/mtd/nand/gpio.c
4 * Updated, and converted to generic GPIO based driver by Russell King.
6 * Written by Ben Dooks <ben@simtec.co.uk>
7 * Based on 2.4 version by Mark Whittaker
9 * © 2004 Simtec Electronics
11 * 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")
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.
21 #include <linux/kernel.h>
22 #include <linux/err.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/gpio/consumer.h>
27 #include <linux/io.h>
28 #include <linux/mtd/mtd.h>
29 #include <linux/mtd/rawnand.h>
30 #include <linux/mtd/partitions.h>
31 #include <linux/mtd/nand-gpio.h>
32 #include <linux/of.h>
33 #include <linux/of_address.h>
35 struct gpiomtd {
36 void __iomem *io_sync;
37 struct nand_chip nand_chip;
38 struct gpio_nand_platdata plat;
39 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 */
46 static inline struct gpiomtd *gpio_nand_getpriv(struct mtd_info *mtd)
48 return container_of(mtd_to_nand(mtd), struct gpiomtd, nand_chip);
52 #ifdef CONFIG_ARM
53 /* gpio_nand_dosync()
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)
60 static void gpio_nand_dosync(struct gpiomtd *gpiomtd)
62 unsigned long tmp;
64 if (gpiomtd->io_sync) {
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.
70 tmp = readl(gpiomtd->io_sync);
71 asm volatile("mov %1, %0\n" : "=r" (tmp) : "r" (tmp));
74 #else
75 static inline void gpio_nand_dosync(struct gpiomtd *gpiomtd) {}
76 #endif
78 static void gpio_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
80 struct gpiomtd *gpiomtd = gpio_nand_getpriv(mtd);
82 gpio_nand_dosync(gpiomtd);
84 if (ctrl & NAND_CTRL_CHANGE) {
85 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));
89 gpio_nand_dosync(gpiomtd);
91 if (cmd == NAND_CMD_NONE)
92 return;
94 writeb(cmd, gpiomtd->nand_chip.IO_ADDR_W);
95 gpio_nand_dosync(gpiomtd);
98 static int gpio_nand_devready(struct mtd_info *mtd)
100 struct gpiomtd *gpiomtd = gpio_nand_getpriv(mtd);
102 return gpiod_get_value(gpiomtd->rdy);
105 #ifdef CONFIG_OF
106 static const struct of_device_id gpio_nand_id_table[] = {
107 { .compatible = "gpio-control-nand" },
110 MODULE_DEVICE_TABLE(of, gpio_nand_id_table);
112 static int gpio_nand_get_config_of(const struct device *dev,
113 struct gpio_nand_platdata *plat)
115 u32 val;
117 if (!dev->of_node)
118 return -ENODEV;
120 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;
129 if (!of_property_read_u32(dev->of_node, "chip-delay", &val))
130 plat->chip_delay = val;
132 return 0;
135 static struct resource *gpio_nand_get_io_sync_of(struct platform_device *pdev)
137 struct resource *r;
138 u64 addr;
140 if (of_property_read_u64(pdev->dev.of_node,
141 "gpio-control-nand,io-sync-reg", &addr))
142 return NULL;
144 r = devm_kzalloc(&pdev->dev, sizeof(*r), GFP_KERNEL);
145 if (!r)
146 return NULL;
148 r->start = addr;
149 r->end = r->start + 0x3;
150 r->flags = IORESOURCE_MEM;
152 return r;
154 #else /* CONFIG_OF */
155 static inline int gpio_nand_get_config_of(const struct device *dev,
156 struct gpio_nand_platdata *plat)
158 return -ENOSYS;
161 static inline struct resource *
162 gpio_nand_get_io_sync_of(struct platform_device *pdev)
164 return NULL;
166 #endif /* CONFIG_OF */
168 static inline int gpio_nand_get_config(const struct device *dev,
169 struct gpio_nand_platdata *plat)
171 int ret = gpio_nand_get_config_of(dev, plat);
173 if (!ret)
174 return ret;
176 if (dev_get_platdata(dev)) {
177 memcpy(plat, dev_get_platdata(dev), sizeof(*plat));
178 return 0;
181 return -EINVAL;
184 static inline struct resource *
185 gpio_nand_get_io_sync(struct platform_device *pdev)
187 struct resource *r = gpio_nand_get_io_sync_of(pdev);
189 if (r)
190 return r;
192 return platform_get_resource(pdev, IORESOURCE_MEM, 1);
195 static int gpio_nand_remove(struct platform_device *pdev)
197 struct gpiomtd *gpiomtd = platform_get_drvdata(pdev);
199 nand_release(nand_to_mtd(&gpiomtd->nand_chip));
201 /* 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);
207 return 0;
210 static int gpio_nand_probe(struct platform_device *pdev)
212 struct gpiomtd *gpiomtd;
213 struct nand_chip *chip;
214 struct mtd_info *mtd;
215 struct resource *res;
216 struct device *dev = &pdev->dev;
217 int ret = 0;
219 if (!dev->of_node && !dev_get_platdata(dev))
220 return -EINVAL;
222 gpiomtd = devm_kzalloc(dev, sizeof(*gpiomtd), GFP_KERNEL);
223 if (!gpiomtd)
224 return -ENOMEM;
226 chip = &gpiomtd->nand_chip;
228 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
229 chip->IO_ADDR_R = devm_ioremap_resource(dev, res);
230 if (IS_ERR(chip->IO_ADDR_R))
231 return PTR_ERR(chip->IO_ADDR_R);
233 res = gpio_nand_get_io_sync(pdev);
234 if (res) {
235 gpiomtd->io_sync = devm_ioremap_resource(dev, res);
236 if (IS_ERR(gpiomtd->io_sync))
237 return PTR_ERR(gpiomtd->io_sync);
240 ret = gpio_nand_get_config(dev, &gpiomtd->plat);
241 if (ret)
242 return ret;
244 /* 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);
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;
256 gpiomtd->ale = devm_gpiod_get(dev, "ale", GPIOD_OUT_LOW);
257 if (IS_ERR(gpiomtd->ale)) {
258 ret = PTR_ERR(gpiomtd->ale);
259 goto out_ce;
262 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;
268 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;
273 /* Using RDY pin */
274 if (gpiomtd->rdy)
275 chip->dev_ready = gpio_nand_devready;
277 nand_set_flash_node(chip, pdev->dev.of_node);
278 chip->IO_ADDR_W = chip->IO_ADDR_R;
279 chip->ecc.mode = NAND_ECC_SOFT;
280 chip->ecc.algo = NAND_ECC_HAMMING;
281 chip->options = gpiomtd->plat.options;
282 chip->chip_delay = gpiomtd->plat.chip_delay;
283 chip->cmd_ctrl = gpio_nand_cmd_ctrl;
285 mtd = nand_to_mtd(chip);
286 mtd->dev.parent = dev;
288 platform_set_drvdata(pdev, gpiomtd);
290 /* Disable write protection, if wired up */
291 if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
292 gpiod_direction_output(gpiomtd->nwp, 1);
294 ret = nand_scan(mtd, 1);
295 if (ret)
296 goto err_wp;
298 if (gpiomtd->plat.adjust_parts)
299 gpiomtd->plat.adjust_parts(&gpiomtd->plat, mtd->size);
301 ret = mtd_device_register(mtd, gpiomtd->plat.parts,
302 gpiomtd->plat.num_parts);
303 if (!ret)
304 return 0;
306 err_wp:
307 if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
308 gpiod_set_value(gpiomtd->nwp, 0);
309 out_ce:
310 if (gpiomtd->nce && !IS_ERR(gpiomtd->nce))
311 gpiod_set_value(gpiomtd->nce, 0);
313 return ret;
316 static struct platform_driver gpio_nand_driver = {
317 .probe = gpio_nand_probe,
318 .remove = gpio_nand_remove,
319 .driver = {
320 .name = "gpio-nand",
321 .of_match_table = of_match_ptr(gpio_nand_id_table),
325 module_platform_driver(gpio_nand_driver);
327 MODULE_LICENSE("GPL");
328 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
329 MODULE_DESCRIPTION("GPIO NAND Driver");