blob: d57a07a597becfd5e995ada7b6b80c367ed245b2 [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>
26#include <linux/gpio.h>
27#include <linux/io.h>
28#include <linux/mtd/mtd.h>
29#include <linux/mtd/nand.h>
30#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>
34#include <linux/of_gpio.h>
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020035
36struct gpiomtd {
37 void __iomem *io_sync;
38 struct mtd_info mtd_info;
39 struct nand_chip nand_chip;
40 struct gpio_nand_platdata plat;
41};
42
43#define gpio_nand_getpriv(x) container_of(x, struct gpiomtd, mtd_info)
44
45
46#ifdef CONFIG_ARM
47/* gpio_nand_dosync()
48 *
49 * Make sure the GPIO state changes occur in-order with writes to NAND
50 * memory region.
51 * Needed on PXA due to bus-reordering within the SoC itself (see section on
52 * I/O ordering in PXA manual (section 2.3, p35)
53 */
54static void gpio_nand_dosync(struct gpiomtd *gpiomtd)
55{
56 unsigned long tmp;
57
58 if (gpiomtd->io_sync) {
59 /*
60 * Linux memory barriers don't cater for what's required here.
61 * What's required is what's here - a read from a separate
62 * region with a dependency on that read.
63 */
64 tmp = readl(gpiomtd->io_sync);
65 asm volatile("mov %1, %0\n" : "=r" (tmp) : "r" (tmp));
66 }
67}
68#else
69static inline void gpio_nand_dosync(struct gpiomtd *gpiomtd) {}
70#endif
71
72static void gpio_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
73{
74 struct gpiomtd *gpiomtd = gpio_nand_getpriv(mtd);
75
76 gpio_nand_dosync(gpiomtd);
77
78 if (ctrl & NAND_CTRL_CHANGE) {
79 gpio_set_value(gpiomtd->plat.gpio_nce, !(ctrl & NAND_NCE));
80 gpio_set_value(gpiomtd->plat.gpio_cle, !!(ctrl & NAND_CLE));
81 gpio_set_value(gpiomtd->plat.gpio_ale, !!(ctrl & NAND_ALE));
82 gpio_nand_dosync(gpiomtd);
83 }
84 if (cmd == NAND_CMD_NONE)
85 return;
86
87 writeb(cmd, gpiomtd->nand_chip.IO_ADDR_W);
88 gpio_nand_dosync(gpiomtd);
89}
90
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020091static int gpio_nand_devready(struct mtd_info *mtd)
92{
93 struct gpiomtd *gpiomtd = gpio_nand_getpriv(mtd);
Alexander Shiyan18afbc52012-10-17 10:08:27 +040094
Alexander Shiyanc85d32d52013-05-06 17:53:49 +040095 return gpio_get_value(gpiomtd->plat.gpio_rdy);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +020096}
97
Jamie Iles775c32202011-12-18 10:00:49 +000098#ifdef CONFIG_OF
99static const struct of_device_id gpio_nand_id_table[] = {
100 { .compatible = "gpio-control-nand" },
101 {}
102};
103MODULE_DEVICE_TABLE(of, gpio_nand_id_table);
104
105static int gpio_nand_get_config_of(const struct device *dev,
106 struct gpio_nand_platdata *plat)
107{
108 u32 val;
109
Alexander Shiyanee4f3662013-05-06 17:53:50 +0400110 if (!dev->of_node)
111 return -ENODEV;
112
Jamie Iles775c32202011-12-18 10:00:49 +0000113 if (!of_property_read_u32(dev->of_node, "bank-width", &val)) {
114 if (val == 2) {
115 plat->options |= NAND_BUSWIDTH_16;
116 } else if (val != 1) {
117 dev_err(dev, "invalid bank-width %u\n", val);
118 return -EINVAL;
119 }
120 }
121
122 plat->gpio_rdy = of_get_gpio(dev->of_node, 0);
123 plat->gpio_nce = of_get_gpio(dev->of_node, 1);
124 plat->gpio_ale = of_get_gpio(dev->of_node, 2);
125 plat->gpio_cle = of_get_gpio(dev->of_node, 3);
126 plat->gpio_nwp = of_get_gpio(dev->of_node, 4);
127
128 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
198 nand_release(&gpiomtd->mtd_info);
199
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200200 if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
201 gpio_set_value(gpiomtd->plat.gpio_nwp, 0);
202 gpio_set_value(gpiomtd->plat.gpio_nce, 1);
203
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200204 return 0;
205}
206
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400207static int gpio_nand_probe(struct platform_device *pdev)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200208{
209 struct gpiomtd *gpiomtd;
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400210 struct nand_chip *chip;
Alexander Shiyan283df422013-05-06 17:53:48 +0400211 struct resource *res;
Jamie Iles775c32202011-12-18 10:00:49 +0000212 int ret = 0;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200213
Jingoo Han453810b2013-07-30 17:18:33 +0900214 if (!pdev->dev.of_node && !dev_get_platdata(&pdev->dev))
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200215 return -EINVAL;
216
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400217 gpiomtd = devm_kzalloc(&pdev->dev, sizeof(*gpiomtd), GFP_KERNEL);
Jingoo Han24e99712013-12-26 12:17:42 +0900218 if (!gpiomtd)
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200219 return -ENOMEM;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200220
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400221 chip = &gpiomtd->nand_chip;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200222
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400223 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
224 chip->IO_ADDR_R = devm_ioremap_resource(&pdev->dev, res);
225 if (IS_ERR(chip->IO_ADDR_R))
226 return PTR_ERR(chip->IO_ADDR_R);
Alexander Shiyan283df422013-05-06 17:53:48 +0400227
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400228 res = gpio_nand_get_io_sync(pdev);
Alexander Shiyan283df422013-05-06 17:53:48 +0400229 if (res) {
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400230 gpiomtd->io_sync = devm_ioremap_resource(&pdev->dev, res);
Alexander Shiyan283df422013-05-06 17:53:48 +0400231 if (IS_ERR(gpiomtd->io_sync))
232 return PTR_ERR(gpiomtd->io_sync);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200233 }
234
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400235 ret = gpio_nand_get_config(&pdev->dev, &gpiomtd->plat);
Jamie Iles775c32202011-12-18 10:00:49 +0000236 if (ret)
Alexander Shiyan283df422013-05-06 17:53:48 +0400237 return ret;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200238
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400239 ret = devm_gpio_request(&pdev->dev, gpiomtd->plat.gpio_nce, "NAND NCE");
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200240 if (ret)
Alexander Shiyan283df422013-05-06 17:53:48 +0400241 return ret;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200242 gpio_direction_output(gpiomtd->plat.gpio_nce, 1);
Alexander Shiyan283df422013-05-06 17:53:48 +0400243
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200244 if (gpio_is_valid(gpiomtd->plat.gpio_nwp)) {
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400245 ret = devm_gpio_request(&pdev->dev, gpiomtd->plat.gpio_nwp,
Alexander Shiyan283df422013-05-06 17:53:48 +0400246 "NAND NWP");
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200247 if (ret)
Alexander Shiyan283df422013-05-06 17:53:48 +0400248 return ret;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200249 }
Alexander Shiyan283df422013-05-06 17:53:48 +0400250
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400251 ret = devm_gpio_request(&pdev->dev, gpiomtd->plat.gpio_ale, "NAND ALE");
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200252 if (ret)
Alexander Shiyan283df422013-05-06 17:53:48 +0400253 return ret;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200254 gpio_direction_output(gpiomtd->plat.gpio_ale, 0);
Alexander Shiyan283df422013-05-06 17:53:48 +0400255
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400256 ret = devm_gpio_request(&pdev->dev, gpiomtd->plat.gpio_cle, "NAND CLE");
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200257 if (ret)
Alexander Shiyan283df422013-05-06 17:53:48 +0400258 return ret;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200259 gpio_direction_output(gpiomtd->plat.gpio_cle, 0);
Alexander Shiyan283df422013-05-06 17:53:48 +0400260
Alexander Shiyan18afbc52012-10-17 10:08:27 +0400261 if (gpio_is_valid(gpiomtd->plat.gpio_rdy)) {
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400262 ret = devm_gpio_request(&pdev->dev, gpiomtd->plat.gpio_rdy,
Alexander Shiyan283df422013-05-06 17:53:48 +0400263 "NAND RDY");
Alexander Shiyan18afbc52012-10-17 10:08:27 +0400264 if (ret)
Alexander Shiyan283df422013-05-06 17:53:48 +0400265 return ret;
Alexander Shiyan18afbc52012-10-17 10:08:27 +0400266 gpio_direction_input(gpiomtd->plat.gpio_rdy);
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400267 chip->dev_ready = gpio_nand_devready;
Alexander Shiyan18afbc52012-10-17 10:08:27 +0400268 }
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200269
Brian Norrisa61ae812015-10-30 20:33:25 -0700270 nand_set_flash_node(chip, pdev->dev.of_node);
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400271 chip->IO_ADDR_W = chip->IO_ADDR_R;
272 chip->ecc.mode = NAND_ECC_SOFT;
273 chip->options = gpiomtd->plat.options;
274 chip->chip_delay = gpiomtd->plat.chip_delay;
275 chip->cmd_ctrl = gpio_nand_cmd_ctrl;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200276
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400277 gpiomtd->mtd_info.priv = chip;
Frans Klavere6c6c282015-06-10 22:38:48 +0200278 gpiomtd->mtd_info.dev.parent = &pdev->dev;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200279
Alexander Shiyanf8e81c22013-05-06 17:53:53 +0400280 platform_set_drvdata(pdev, gpiomtd);
Alexander Shiyan283df422013-05-06 17:53:48 +0400281
282 if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
283 gpio_direction_output(gpiomtd->plat.gpio_nwp, 1);
284
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200285 if (nand_scan(&gpiomtd->mtd_info, 1)) {
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200286 ret = -ENXIO;
287 goto err_wp;
288 }
289
290 if (gpiomtd->plat.adjust_parts)
291 gpiomtd->plat.adjust_parts(&gpiomtd->plat,
292 gpiomtd->mtd_info.size);
293
Brian Norrisa61ae812015-10-30 20:33:25 -0700294 ret = mtd_device_register(&gpiomtd->mtd_info, gpiomtd->plat.parts,
295 gpiomtd->plat.num_parts);
Alexander Shiyan283df422013-05-06 17:53:48 +0400296 if (!ret)
297 return 0;
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200298
299err_wp:
300 if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
301 gpio_set_value(gpiomtd->plat.gpio_nwp, 0);
Alexander Shiyan283df422013-05-06 17:53:48 +0400302
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200303 return ret;
304}
305
306static struct platform_driver gpio_nand_driver = {
307 .probe = gpio_nand_probe,
308 .remove = gpio_nand_remove,
309 .driver = {
310 .name = "gpio-nand",
Sachin Kamatb57d43f2013-03-14 15:37:03 +0530311 .of_match_table = of_match_ptr(gpio_nand_id_table),
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200312 },
313};
314
Sachin Kamat2fe87ae2012-09-05 15:31:32 +0530315module_platform_driver(gpio_nand_driver);
Mike Rapoportaaf7ea22008-10-15 08:38:49 +0200316
317MODULE_LICENSE("GPL");
318MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
319MODULE_DESCRIPTION("GPIO NAND Driver");