1 // SPDX-License-Identifier: GPL-2.0-only
3 * Updated, and converted to generic GPIO based driver by Russell King.
5 * Written by Ben Dooks <ben@simtec.co.uk>
6 * Based on 2.4 version by Mark Whittaker
8 * © 2004 Simtec Electronics
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")
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>
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>
27 #include <linux/of_address.h>
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 */
40 static inline struct gpiomtd
*gpio_nand_getpriv(struct mtd_info
*mtd
)
42 return container_of(mtd_to_nand(mtd
), struct gpiomtd
, nand_chip
);
49 * Make sure the GPIO state changes occur in-order with writes to NAND
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)
54 static void gpio_nand_dosync(struct gpiomtd
*gpiomtd
)
58 if (gpiomtd
->io_sync
) {
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.
64 tmp
= readl(gpiomtd
->io_sync
);
65 asm volatile("mov %1, %0\n" : "=r" (tmp
) : "r" (tmp
));
69 static inline void gpio_nand_dosync(struct gpiomtd
*gpiomtd
) {}
72 static void gpio_nand_cmd_ctrl(struct nand_chip
*chip
, int cmd
,
75 struct gpiomtd
*gpiomtd
= gpio_nand_getpriv(nand_to_mtd(chip
));
77 gpio_nand_dosync(gpiomtd
);
79 if (ctrl
& NAND_CTRL_CHANGE
) {
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
);
86 if (cmd
== NAND_CMD_NONE
)
89 writeb(cmd
, gpiomtd
->nand_chip
.legacy
.IO_ADDR_W
);
90 gpio_nand_dosync(gpiomtd
);
93 static int gpio_nand_devready(struct nand_chip
*chip
)
95 struct gpiomtd
*gpiomtd
= gpio_nand_getpriv(nand_to_mtd(chip
));
97 return gpiod_get_value(gpiomtd
->rdy
);
101 static const struct of_device_id gpio_nand_id_table
[] = {
102 { .compatible
= "gpio-control-nand" },
105 MODULE_DEVICE_TABLE(of
, gpio_nand_id_table
);
107 static int gpio_nand_get_config_of(const struct device
*dev
,
108 struct gpio_nand_platdata
*plat
)
115 if (!of_property_read_u32(dev
->of_node
, "bank-width", &val
)) {
117 plat
->options
|= NAND_BUSWIDTH_16
;
118 } else if (val
!= 1) {
119 dev_err(dev
, "invalid bank-width %u\n", val
);
124 if (!of_property_read_u32(dev
->of_node
, "chip-delay", &val
))
125 plat
->chip_delay
= val
;
130 static struct resource
*gpio_nand_get_io_sync_of(struct platform_device
*pdev
)
135 if (of_property_read_u64(pdev
->dev
.of_node
,
136 "gpio-control-nand,io-sync-reg", &addr
))
139 r
= devm_kzalloc(&pdev
->dev
, sizeof(*r
), GFP_KERNEL
);
144 r
->end
= r
->start
+ 0x3;
145 r
->flags
= IORESOURCE_MEM
;
149 #else /* CONFIG_OF */
150 static inline int gpio_nand_get_config_of(const struct device
*dev
,
151 struct gpio_nand_platdata
*plat
)
156 static inline struct resource
*
157 gpio_nand_get_io_sync_of(struct platform_device
*pdev
)
161 #endif /* CONFIG_OF */
163 static inline int gpio_nand_get_config(const struct device
*dev
,
164 struct gpio_nand_platdata
*plat
)
166 int ret
= gpio_nand_get_config_of(dev
, plat
);
171 if (dev_get_platdata(dev
)) {
172 memcpy(plat
, dev_get_platdata(dev
), sizeof(*plat
));
179 static inline struct resource
*
180 gpio_nand_get_io_sync(struct platform_device
*pdev
)
182 struct resource
*r
= gpio_nand_get_io_sync_of(pdev
);
187 return platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
190 static int gpio_nand_remove(struct platform_device
*pdev
)
192 struct gpiomtd
*gpiomtd
= platform_get_drvdata(pdev
);
194 nand_release(&gpiomtd
->nand_chip
);
196 /* Enable write protection and disable the chip */
197 if (gpiomtd
->nwp
&& !IS_ERR(gpiomtd
->nwp
))
198 gpiod_set_value(gpiomtd
->nwp
, 0);
199 if (gpiomtd
->nce
&& !IS_ERR(gpiomtd
->nce
))
200 gpiod_set_value(gpiomtd
->nce
, 0);
205 static int gpio_nand_probe(struct platform_device
*pdev
)
207 struct gpiomtd
*gpiomtd
;
208 struct nand_chip
*chip
;
209 struct mtd_info
*mtd
;
210 struct resource
*res
;
211 struct device
*dev
= &pdev
->dev
;
214 if (!dev
->of_node
&& !dev_get_platdata(dev
))
217 gpiomtd
= devm_kzalloc(dev
, sizeof(*gpiomtd
), GFP_KERNEL
);
221 chip
= &gpiomtd
->nand_chip
;
223 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
224 chip
->legacy
.IO_ADDR_R
= devm_ioremap_resource(dev
, res
);
225 if (IS_ERR(chip
->legacy
.IO_ADDR_R
))
226 return PTR_ERR(chip
->legacy
.IO_ADDR_R
);
228 res
= gpio_nand_get_io_sync(pdev
);
230 gpiomtd
->io_sync
= devm_ioremap_resource(dev
, res
);
231 if (IS_ERR(gpiomtd
->io_sync
))
232 return PTR_ERR(gpiomtd
->io_sync
);
235 ret
= gpio_nand_get_config(dev
, &gpiomtd
->plat
);
239 /* Just enable the chip */
240 gpiomtd
->nce
= devm_gpiod_get_optional(dev
, "nce", GPIOD_OUT_HIGH
);
241 if (IS_ERR(gpiomtd
->nce
))
242 return PTR_ERR(gpiomtd
->nce
);
244 /* We disable write protection once we know probe() will succeed */
245 gpiomtd
->nwp
= devm_gpiod_get_optional(dev
, "nwp", GPIOD_OUT_LOW
);
246 if (IS_ERR(gpiomtd
->nwp
)) {
247 ret
= PTR_ERR(gpiomtd
->nwp
);
251 gpiomtd
->ale
= devm_gpiod_get(dev
, "ale", GPIOD_OUT_LOW
);
252 if (IS_ERR(gpiomtd
->ale
)) {
253 ret
= PTR_ERR(gpiomtd
->ale
);
257 gpiomtd
->cle
= devm_gpiod_get(dev
, "cle", GPIOD_OUT_LOW
);
258 if (IS_ERR(gpiomtd
->cle
)) {
259 ret
= PTR_ERR(gpiomtd
->cle
);
263 gpiomtd
->rdy
= devm_gpiod_get_optional(dev
, "rdy", GPIOD_IN
);
264 if (IS_ERR(gpiomtd
->rdy
)) {
265 ret
= PTR_ERR(gpiomtd
->rdy
);
270 chip
->legacy
.dev_ready
= gpio_nand_devready
;
272 nand_set_flash_node(chip
, pdev
->dev
.of_node
);
273 chip
->legacy
.IO_ADDR_W
= chip
->legacy
.IO_ADDR_R
;
274 chip
->ecc
.mode
= NAND_ECC_SOFT
;
275 chip
->ecc
.algo
= NAND_ECC_HAMMING
;
276 chip
->options
= gpiomtd
->plat
.options
;
277 chip
->legacy
.chip_delay
= gpiomtd
->plat
.chip_delay
;
278 chip
->legacy
.cmd_ctrl
= gpio_nand_cmd_ctrl
;
280 mtd
= nand_to_mtd(chip
);
281 mtd
->dev
.parent
= dev
;
283 platform_set_drvdata(pdev
, gpiomtd
);
285 /* Disable write protection, if wired up */
286 if (gpiomtd
->nwp
&& !IS_ERR(gpiomtd
->nwp
))
287 gpiod_direction_output(gpiomtd
->nwp
, 1);
289 ret
= nand_scan(chip
, 1);
293 if (gpiomtd
->plat
.adjust_parts
)
294 gpiomtd
->plat
.adjust_parts(&gpiomtd
->plat
, mtd
->size
);
296 ret
= mtd_device_register(mtd
, gpiomtd
->plat
.parts
,
297 gpiomtd
->plat
.num_parts
);
302 if (gpiomtd
->nwp
&& !IS_ERR(gpiomtd
->nwp
))
303 gpiod_set_value(gpiomtd
->nwp
, 0);
305 if (gpiomtd
->nce
&& !IS_ERR(gpiomtd
->nce
))
306 gpiod_set_value(gpiomtd
->nce
, 0);
311 static struct platform_driver gpio_nand_driver
= {
312 .probe
= gpio_nand_probe
,
313 .remove
= gpio_nand_remove
,
316 .of_match_table
= of_match_ptr(gpio_nand_id_table
),
320 module_platform_driver(gpio_nand_driver
);
322 MODULE_LICENSE("GPL");
323 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
324 MODULE_DESCRIPTION("GPIO NAND Driver");