xref: /openbmc/linux/drivers/mtd/nand/raw/gpio.c (revision 34facb04)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Updated, and converted to generic GPIO based driver by Russell King.
4  *
5  * Written by Ben Dooks <ben@simtec.co.uk>
6  *   Based on 2.4 version by Mark Whittaker
7  *
8  * © 2004 Simtec Electronics
9  *
10  * Device driver for NAND flash that uses a memory mapped interface to
11  * read/write the NAND commands and data, and GPIO pins for control signals
12  * (the DT binding refers to this as "GPIO assisted NAND flash")
13  */
14 
15 #include <linux/kernel.h>
16 #include <linux/err.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/io.h>
22 #include <linux/mtd/mtd.h>
23 #include <linux/mtd/rawnand.h>
24 #include <linux/mtd/partitions.h>
25 #include <linux/mtd/nand-gpio.h>
26 #include <linux/of.h>
27 #include <linux/of_address.h>
28 
29 struct gpiomtd {
30 	void __iomem		*io_sync;
31 	struct nand_chip	nand_chip;
32 	struct gpio_nand_platdata plat;
33 	struct gpio_desc *nce; /* Optional chip enable */
34 	struct gpio_desc *cle;
35 	struct gpio_desc *ale;
36 	struct gpio_desc *rdy;
37 	struct gpio_desc *nwp; /* Optional write protection */
38 };
39 
40 static inline struct gpiomtd *gpio_nand_getpriv(struct mtd_info *mtd)
41 {
42 	return container_of(mtd_to_nand(mtd), struct gpiomtd, nand_chip);
43 }
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  */
54 static 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
69 static inline void gpio_nand_dosync(struct gpiomtd *gpiomtd) {}
70 #endif
71 
72 static void gpio_nand_cmd_ctrl(struct nand_chip *chip, int cmd,
73 			       unsigned int ctrl)
74 {
75 	struct gpiomtd *gpiomtd = gpio_nand_getpriv(nand_to_mtd(chip));
76 
77 	gpio_nand_dosync(gpiomtd);
78 
79 	if (ctrl & NAND_CTRL_CHANGE) {
80 		if (gpiomtd->nce)
81 			gpiod_set_value(gpiomtd->nce, !(ctrl & NAND_NCE));
82 		gpiod_set_value(gpiomtd->cle, !!(ctrl & NAND_CLE));
83 		gpiod_set_value(gpiomtd->ale, !!(ctrl & NAND_ALE));
84 		gpio_nand_dosync(gpiomtd);
85 	}
86 	if (cmd == NAND_CMD_NONE)
87 		return;
88 
89 	writeb(cmd, gpiomtd->nand_chip.legacy.IO_ADDR_W);
90 	gpio_nand_dosync(gpiomtd);
91 }
92 
93 static int gpio_nand_devready(struct nand_chip *chip)
94 {
95 	struct gpiomtd *gpiomtd = gpio_nand_getpriv(nand_to_mtd(chip));
96 
97 	return gpiod_get_value(gpiomtd->rdy);
98 }
99 
100 #ifdef CONFIG_OF
101 static const struct of_device_id gpio_nand_id_table[] = {
102 	{ .compatible = "gpio-control-nand" },
103 	{}
104 };
105 MODULE_DEVICE_TABLE(of, gpio_nand_id_table);
106 
107 static int gpio_nand_get_config_of(const struct device *dev,
108 				   struct gpio_nand_platdata *plat)
109 {
110 	u32 val;
111 
112 	if (!dev->of_node)
113 		return -ENODEV;
114 
115 	if (!of_property_read_u32(dev->of_node, "bank-width", &val)) {
116 		if (val == 2) {
117 			plat->options |= NAND_BUSWIDTH_16;
118 		} else if (val != 1) {
119 			dev_err(dev, "invalid bank-width %u\n", val);
120 			return -EINVAL;
121 		}
122 	}
123 
124 	if (!of_property_read_u32(dev->of_node, "chip-delay", &val))
125 		plat->chip_delay = val;
126 
127 	return 0;
128 }
129 
130 static struct resource *gpio_nand_get_io_sync_of(struct platform_device *pdev)
131 {
132 	struct resource *r;
133 	u64 addr;
134 
135 	if (of_property_read_u64(pdev->dev.of_node,
136 				       "gpio-control-nand,io-sync-reg", &addr))
137 		return NULL;
138 
139 	r = devm_kzalloc(&pdev->dev, sizeof(*r), GFP_KERNEL);
140 	if (!r)
141 		return NULL;
142 
143 	r->start = addr;
144 	r->end = r->start + 0x3;
145 	r->flags = IORESOURCE_MEM;
146 
147 	return r;
148 }
149 #else /* CONFIG_OF */
150 static inline int gpio_nand_get_config_of(const struct device *dev,
151 					  struct gpio_nand_platdata *plat)
152 {
153 	return -ENOSYS;
154 }
155 
156 static inline struct resource *
157 gpio_nand_get_io_sync_of(struct platform_device *pdev)
158 {
159 	return NULL;
160 }
161 #endif /* CONFIG_OF */
162 
163 static inline int gpio_nand_get_config(const struct device *dev,
164 				       struct gpio_nand_platdata *plat)
165 {
166 	int ret = gpio_nand_get_config_of(dev, plat);
167 
168 	if (!ret)
169 		return ret;
170 
171 	if (dev_get_platdata(dev)) {
172 		memcpy(plat, dev_get_platdata(dev), sizeof(*plat));
173 		return 0;
174 	}
175 
176 	return -EINVAL;
177 }
178 
179 static inline struct resource *
180 gpio_nand_get_io_sync(struct platform_device *pdev)
181 {
182 	struct resource *r = gpio_nand_get_io_sync_of(pdev);
183 
184 	if (r)
185 		return r;
186 
187 	return platform_get_resource(pdev, IORESOURCE_MEM, 1);
188 }
189 
190 static int gpio_nand_remove(struct platform_device *pdev)
191 {
192 	struct gpiomtd *gpiomtd = platform_get_drvdata(pdev);
193 	struct nand_chip *chip = &gpiomtd->nand_chip;
194 	int ret;
195 
196 	ret = mtd_device_unregister(nand_to_mtd(chip));
197 	WARN_ON(ret);
198 	nand_cleanup(chip);
199 
200 	/* 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);
205 
206 	return 0;
207 }
208 
209 static int gpio_nand_probe(struct platform_device *pdev)
210 {
211 	struct gpiomtd *gpiomtd;
212 	struct nand_chip *chip;
213 	struct mtd_info *mtd;
214 	struct resource *res;
215 	struct device *dev = &pdev->dev;
216 	int ret = 0;
217 
218 	if (!dev->of_node && !dev_get_platdata(dev))
219 		return -EINVAL;
220 
221 	gpiomtd = devm_kzalloc(dev, sizeof(*gpiomtd), GFP_KERNEL);
222 	if (!gpiomtd)
223 		return -ENOMEM;
224 
225 	chip = &gpiomtd->nand_chip;
226 
227 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
228 	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);
231 
232 	res = gpio_nand_get_io_sync(pdev);
233 	if (res) {
234 		gpiomtd->io_sync = devm_ioremap_resource(dev, res);
235 		if (IS_ERR(gpiomtd->io_sync))
236 			return PTR_ERR(gpiomtd->io_sync);
237 	}
238 
239 	ret = gpio_nand_get_config(dev, &gpiomtd->plat);
240 	if (ret)
241 		return ret;
242 
243 	/* 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;
253 	}
254 
255 	gpiomtd->ale = devm_gpiod_get(dev, "ale", GPIOD_OUT_LOW);
256 	if (IS_ERR(gpiomtd->ale)) {
257 		ret = PTR_ERR(gpiomtd->ale);
258 		goto out_ce;
259 	}
260 
261 	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 	}
266 
267 	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)
274 		chip->legacy.dev_ready = gpio_nand_devready;
275 
276 	nand_set_flash_node(chip, pdev->dev.of_node);
277 	chip->legacy.IO_ADDR_W	= chip->legacy.IO_ADDR_R;
278 	chip->ecc.mode		= NAND_ECC_SOFT;
279 	chip->ecc.algo		= NAND_ECC_HAMMING;
280 	chip->options		= gpiomtd->plat.options;
281 	chip->legacy.chip_delay	= gpiomtd->plat.chip_delay;
282 	chip->legacy.cmd_ctrl	= gpio_nand_cmd_ctrl;
283 
284 	mtd			= nand_to_mtd(chip);
285 	mtd->dev.parent		= dev;
286 
287 	platform_set_drvdata(pdev, gpiomtd);
288 
289 	/* Disable write protection, if wired up */
290 	if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
291 		gpiod_direction_output(gpiomtd->nwp, 1);
292 
293 	ret = nand_scan(chip, 1);
294 	if (ret)
295 		goto err_wp;
296 
297 	if (gpiomtd->plat.adjust_parts)
298 		gpiomtd->plat.adjust_parts(&gpiomtd->plat, mtd->size);
299 
300 	ret = mtd_device_register(mtd, gpiomtd->plat.parts,
301 				  gpiomtd->plat.num_parts);
302 	if (!ret)
303 		return 0;
304 
305 err_wp:
306 	if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
307 		gpiod_set_value(gpiomtd->nwp, 0);
308 out_ce:
309 	if (gpiomtd->nce && !IS_ERR(gpiomtd->nce))
310 		gpiod_set_value(gpiomtd->nce, 0);
311 
312 	return ret;
313 }
314 
315 static struct platform_driver gpio_nand_driver = {
316 	.probe		= gpio_nand_probe,
317 	.remove		= gpio_nand_remove,
318 	.driver		= {
319 		.name	= "gpio-nand",
320 		.of_match_table = of_match_ptr(gpio_nand_id_table),
321 	},
322 };
323 
324 module_platform_driver(gpio_nand_driver);
325 
326 MODULE_LICENSE("GPL");
327 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
328 MODULE_DESCRIPTION("GPIO NAND Driver");
329