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 connected via GPIO
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.
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/slab.h>
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/gpio.h>
26 #include <linux/mtd/mtd.h>
27 #include <linux/mtd/nand.h>
28 #include <linux/mtd/partitions.h>
29 #include <linux/mtd/nand-gpio.h>
31 #include <linux/of_address.h>
32 #include <linux/of_gpio.h>
35 void __iomem
*io_sync
;
36 struct mtd_info mtd_info
;
37 struct nand_chip nand_chip
;
38 struct gpio_nand_platdata plat
;
41 #define gpio_nand_getpriv(x) container_of(x, struct gpiomtd, mtd_info)
47 * Make sure the GPIO state changes occur in-order with writes to NAND
49 * Needed on PXA due to bus-reordering within the SoC itself (see section on
50 * I/O ordering in PXA manual (section 2.3, p35)
52 static void gpio_nand_dosync(struct gpiomtd
*gpiomtd
)
56 if (gpiomtd
->io_sync
) {
58 * Linux memory barriers don't cater for what's required here.
59 * What's required is what's here - a read from a separate
60 * region with a dependency on that read.
62 tmp
= readl(gpiomtd
->io_sync
);
63 asm volatile("mov %1, %0\n" : "=r" (tmp
) : "r" (tmp
));
67 static inline void gpio_nand_dosync(struct gpiomtd
*gpiomtd
) {}
70 static void gpio_nand_cmd_ctrl(struct mtd_info
*mtd
, int cmd
, unsigned int ctrl
)
72 struct gpiomtd
*gpiomtd
= gpio_nand_getpriv(mtd
);
74 gpio_nand_dosync(gpiomtd
);
76 if (ctrl
& NAND_CTRL_CHANGE
) {
77 gpio_set_value(gpiomtd
->plat
.gpio_nce
, !(ctrl
& NAND_NCE
));
78 gpio_set_value(gpiomtd
->plat
.gpio_cle
, !!(ctrl
& NAND_CLE
));
79 gpio_set_value(gpiomtd
->plat
.gpio_ale
, !!(ctrl
& NAND_ALE
));
80 gpio_nand_dosync(gpiomtd
);
82 if (cmd
== NAND_CMD_NONE
)
85 writeb(cmd
, gpiomtd
->nand_chip
.IO_ADDR_W
);
86 gpio_nand_dosync(gpiomtd
);
89 static void gpio_nand_writebuf(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
91 struct nand_chip
*this = mtd
->priv
;
93 writesb(this->IO_ADDR_W
, buf
, len
);
96 static void gpio_nand_readbuf(struct mtd_info
*mtd
, u_char
*buf
, int len
)
98 struct nand_chip
*this = mtd
->priv
;
100 readsb(this->IO_ADDR_R
, buf
, len
);
103 static void gpio_nand_writebuf16(struct mtd_info
*mtd
, const u_char
*buf
,
106 struct nand_chip
*this = mtd
->priv
;
108 if (IS_ALIGNED((unsigned long)buf
, 2)) {
109 writesw(this->IO_ADDR_W
, buf
, len
>>1);
112 unsigned short *ptr
= (unsigned short *)buf
;
114 for (i
= 0; i
< len
; i
+= 2, ptr
++)
115 writew(*ptr
, this->IO_ADDR_W
);
119 static void gpio_nand_readbuf16(struct mtd_info
*mtd
, u_char
*buf
, int len
)
121 struct nand_chip
*this = mtd
->priv
;
123 if (IS_ALIGNED((unsigned long)buf
, 2)) {
124 readsw(this->IO_ADDR_R
, buf
, len
>>1);
127 unsigned short *ptr
= (unsigned short *)buf
;
129 for (i
= 0; i
< len
; i
+= 2, ptr
++)
130 *ptr
= readw(this->IO_ADDR_R
);
134 static int gpio_nand_devready(struct mtd_info
*mtd
)
136 struct gpiomtd
*gpiomtd
= gpio_nand_getpriv(mtd
);
138 if (gpio_is_valid(gpiomtd
->plat
.gpio_rdy
))
139 return gpio_get_value(gpiomtd
->plat
.gpio_rdy
);
145 static const struct of_device_id gpio_nand_id_table
[] = {
146 { .compatible
= "gpio-control-nand" },
149 MODULE_DEVICE_TABLE(of
, gpio_nand_id_table
);
151 static int gpio_nand_get_config_of(const struct device
*dev
,
152 struct gpio_nand_platdata
*plat
)
156 if (!of_property_read_u32(dev
->of_node
, "bank-width", &val
)) {
158 plat
->options
|= NAND_BUSWIDTH_16
;
159 } else if (val
!= 1) {
160 dev_err(dev
, "invalid bank-width %u\n", val
);
165 plat
->gpio_rdy
= of_get_gpio(dev
->of_node
, 0);
166 plat
->gpio_nce
= of_get_gpio(dev
->of_node
, 1);
167 plat
->gpio_ale
= of_get_gpio(dev
->of_node
, 2);
168 plat
->gpio_cle
= of_get_gpio(dev
->of_node
, 3);
169 plat
->gpio_nwp
= of_get_gpio(dev
->of_node
, 4);
171 if (!of_property_read_u32(dev
->of_node
, "chip-delay", &val
))
172 plat
->chip_delay
= val
;
177 static struct resource
*gpio_nand_get_io_sync_of(struct platform_device
*pdev
)
179 struct resource
*r
= devm_kzalloc(&pdev
->dev
, sizeof(*r
), GFP_KERNEL
);
182 if (!r
|| of_property_read_u64(pdev
->dev
.of_node
,
183 "gpio-control-nand,io-sync-reg", &addr
))
187 r
->end
= r
->start
+ 0x3;
188 r
->flags
= IORESOURCE_MEM
;
192 #else /* CONFIG_OF */
193 #define gpio_nand_id_table NULL
194 static inline int gpio_nand_get_config_of(const struct device
*dev
,
195 struct gpio_nand_platdata
*plat
)
200 static inline struct resource
*
201 gpio_nand_get_io_sync_of(struct platform_device
*pdev
)
205 #endif /* CONFIG_OF */
207 static inline int gpio_nand_get_config(const struct device
*dev
,
208 struct gpio_nand_platdata
*plat
)
210 int ret
= gpio_nand_get_config_of(dev
, plat
);
215 if (dev
->platform_data
) {
216 memcpy(plat
, dev
->platform_data
, sizeof(*plat
));
223 static inline struct resource
*
224 gpio_nand_get_io_sync(struct platform_device
*pdev
)
226 struct resource
*r
= gpio_nand_get_io_sync_of(pdev
);
231 return platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
234 static int gpio_nand_remove(struct platform_device
*dev
)
236 struct gpiomtd
*gpiomtd
= platform_get_drvdata(dev
);
237 struct resource
*res
;
239 nand_release(&gpiomtd
->mtd_info
);
241 res
= gpio_nand_get_io_sync(dev
);
242 iounmap(gpiomtd
->io_sync
);
244 release_mem_region(res
->start
, resource_size(res
));
246 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
247 iounmap(gpiomtd
->nand_chip
.IO_ADDR_R
);
248 release_mem_region(res
->start
, resource_size(res
));
250 if (gpio_is_valid(gpiomtd
->plat
.gpio_nwp
))
251 gpio_set_value(gpiomtd
->plat
.gpio_nwp
, 0);
252 gpio_set_value(gpiomtd
->plat
.gpio_nce
, 1);
254 gpio_free(gpiomtd
->plat
.gpio_cle
);
255 gpio_free(gpiomtd
->plat
.gpio_ale
);
256 gpio_free(gpiomtd
->plat
.gpio_nce
);
257 if (gpio_is_valid(gpiomtd
->plat
.gpio_nwp
))
258 gpio_free(gpiomtd
->plat
.gpio_nwp
);
259 if (gpio_is_valid(gpiomtd
->plat
.gpio_rdy
))
260 gpio_free(gpiomtd
->plat
.gpio_rdy
);
267 static void __iomem
*request_and_remap(struct resource
*res
, size_t size
,
268 const char *name
, int *err
)
272 if (!request_mem_region(res
->start
, resource_size(res
), name
)) {
277 ptr
= ioremap(res
->start
, size
);
279 release_mem_region(res
->start
, resource_size(res
));
285 static int gpio_nand_probe(struct platform_device
*dev
)
287 struct gpiomtd
*gpiomtd
;
288 struct nand_chip
*this;
289 struct resource
*res0
, *res1
;
290 struct mtd_part_parser_data ppdata
= {};
293 if (!dev
->dev
.of_node
&& !dev
->dev
.platform_data
)
296 res0
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
300 gpiomtd
= kzalloc(sizeof(*gpiomtd
), GFP_KERNEL
);
301 if (gpiomtd
== NULL
) {
302 dev_err(&dev
->dev
, "failed to create NAND MTD\n");
306 this = &gpiomtd
->nand_chip
;
307 this->IO_ADDR_R
= request_and_remap(res0
, 2, "NAND", &ret
);
308 if (!this->IO_ADDR_R
) {
309 dev_err(&dev
->dev
, "unable to map NAND\n");
313 res1
= gpio_nand_get_io_sync(dev
);
315 gpiomtd
->io_sync
= request_and_remap(res1
, 4, "NAND sync", &ret
);
316 if (!gpiomtd
->io_sync
) {
317 dev_err(&dev
->dev
, "unable to map sync NAND\n");
322 ret
= gpio_nand_get_config(&dev
->dev
, &gpiomtd
->plat
);
326 ret
= gpio_request(gpiomtd
->plat
.gpio_nce
, "NAND NCE");
329 gpio_direction_output(gpiomtd
->plat
.gpio_nce
, 1);
330 if (gpio_is_valid(gpiomtd
->plat
.gpio_nwp
)) {
331 ret
= gpio_request(gpiomtd
->plat
.gpio_nwp
, "NAND NWP");
334 gpio_direction_output(gpiomtd
->plat
.gpio_nwp
, 1);
336 ret
= gpio_request(gpiomtd
->plat
.gpio_ale
, "NAND ALE");
339 gpio_direction_output(gpiomtd
->plat
.gpio_ale
, 0);
340 ret
= gpio_request(gpiomtd
->plat
.gpio_cle
, "NAND CLE");
343 gpio_direction_output(gpiomtd
->plat
.gpio_cle
, 0);
344 if (gpio_is_valid(gpiomtd
->plat
.gpio_rdy
)) {
345 ret
= gpio_request(gpiomtd
->plat
.gpio_rdy
, "NAND RDY");
348 gpio_direction_input(gpiomtd
->plat
.gpio_rdy
);
352 this->IO_ADDR_W
= this->IO_ADDR_R
;
353 this->ecc
.mode
= NAND_ECC_SOFT
;
354 this->options
= gpiomtd
->plat
.options
;
355 this->chip_delay
= gpiomtd
->plat
.chip_delay
;
357 /* install our routines */
358 this->cmd_ctrl
= gpio_nand_cmd_ctrl
;
359 this->dev_ready
= gpio_nand_devready
;
361 if (this->options
& NAND_BUSWIDTH_16
) {
362 this->read_buf
= gpio_nand_readbuf16
;
363 this->write_buf
= gpio_nand_writebuf16
;
365 this->read_buf
= gpio_nand_readbuf
;
366 this->write_buf
= gpio_nand_writebuf
;
369 /* set the mtd private data for the nand driver */
370 gpiomtd
->mtd_info
.priv
= this;
371 gpiomtd
->mtd_info
.owner
= THIS_MODULE
;
373 if (nand_scan(&gpiomtd
->mtd_info
, 1)) {
374 dev_err(&dev
->dev
, "no nand chips found?\n");
379 if (gpiomtd
->plat
.adjust_parts
)
380 gpiomtd
->plat
.adjust_parts(&gpiomtd
->plat
,
381 gpiomtd
->mtd_info
.size
);
383 ppdata
.of_node
= dev
->dev
.of_node
;
384 ret
= mtd_device_parse_register(&gpiomtd
->mtd_info
, NULL
, &ppdata
,
386 gpiomtd
->plat
.num_parts
);
389 platform_set_drvdata(dev
, gpiomtd
);
394 if (gpio_is_valid(gpiomtd
->plat
.gpio_nwp
))
395 gpio_set_value(gpiomtd
->plat
.gpio_nwp
, 0);
396 if (gpio_is_valid(gpiomtd
->plat
.gpio_rdy
))
397 gpio_free(gpiomtd
->plat
.gpio_rdy
);
399 gpio_free(gpiomtd
->plat
.gpio_cle
);
401 gpio_free(gpiomtd
->plat
.gpio_ale
);
403 if (gpio_is_valid(gpiomtd
->plat
.gpio_nwp
))
404 gpio_free(gpiomtd
->plat
.gpio_nwp
);
406 gpio_free(gpiomtd
->plat
.gpio_nce
);
408 iounmap(gpiomtd
->io_sync
);
410 release_mem_region(res1
->start
, resource_size(res1
));
412 iounmap(gpiomtd
->nand_chip
.IO_ADDR_R
);
413 release_mem_region(res0
->start
, resource_size(res0
));
419 static struct platform_driver gpio_nand_driver
= {
420 .probe
= gpio_nand_probe
,
421 .remove
= gpio_nand_remove
,
424 .of_match_table
= gpio_nand_id_table
,
428 module_platform_driver(gpio_nand_driver
);
430 MODULE_LICENSE("GPL");
431 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
432 MODULE_DESCRIPTION("GPIO NAND Driver");