Lines Matching +full:nand +full:- +full:int +full:- +full:base
1 // SPDX-License-Identifier: GPL-2.0-only
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")
25 #include <linux/mtd/nand-gpio.h>
31 struct nand_controller base; member
52 * Make sure the GPIO state changes occur in-order with writes to NAND
54 * Needed on PXA due to bus-reordering within the SoC itself (see section on
61 if (gpiomtd->io_sync) { in gpio_nand_dosync()
64 * What's required is what's here - a read from a separate in gpio_nand_dosync()
67 tmp = readl(gpiomtd->io_sync); in gpio_nand_dosync()
75 static int gpio_nand_exec_instr(struct nand_chip *chip, in gpio_nand_exec_instr()
79 unsigned int i; in gpio_nand_exec_instr()
81 switch (instr->type) { in gpio_nand_exec_instr()
84 gpiod_set_value(gpiomtd->cle, 1); in gpio_nand_exec_instr()
86 writeb(instr->ctx.cmd.opcode, gpiomtd->io); in gpio_nand_exec_instr()
88 gpiod_set_value(gpiomtd->cle, 0); in gpio_nand_exec_instr()
93 gpiod_set_value(gpiomtd->ale, 1); in gpio_nand_exec_instr()
95 for (i = 0; i < instr->ctx.addr.naddrs; i++) in gpio_nand_exec_instr()
96 writeb(instr->ctx.addr.addrs[i], gpiomtd->io); in gpio_nand_exec_instr()
98 gpiod_set_value(gpiomtd->ale, 0); in gpio_nand_exec_instr()
103 if ((chip->options & NAND_BUSWIDTH_16) && in gpio_nand_exec_instr()
104 !instr->ctx.data.force_8bit) in gpio_nand_exec_instr()
105 ioread16_rep(gpiomtd->io, instr->ctx.data.buf.in, in gpio_nand_exec_instr()
106 instr->ctx.data.len / 2); in gpio_nand_exec_instr()
108 ioread8_rep(gpiomtd->io, instr->ctx.data.buf.in, in gpio_nand_exec_instr()
109 instr->ctx.data.len); in gpio_nand_exec_instr()
114 if ((chip->options & NAND_BUSWIDTH_16) && in gpio_nand_exec_instr()
115 !instr->ctx.data.force_8bit) in gpio_nand_exec_instr()
116 iowrite16_rep(gpiomtd->io, instr->ctx.data.buf.out, in gpio_nand_exec_instr()
117 instr->ctx.data.len / 2); in gpio_nand_exec_instr()
119 iowrite8_rep(gpiomtd->io, instr->ctx.data.buf.out, in gpio_nand_exec_instr()
120 instr->ctx.data.len); in gpio_nand_exec_instr()
124 if (!gpiomtd->rdy) in gpio_nand_exec_instr()
125 return nand_soft_waitrdy(chip, instr->ctx.waitrdy.timeout_ms); in gpio_nand_exec_instr()
127 return nand_gpio_waitrdy(chip, gpiomtd->rdy, in gpio_nand_exec_instr()
128 instr->ctx.waitrdy.timeout_ms); in gpio_nand_exec_instr()
131 return -EINVAL; in gpio_nand_exec_instr()
137 static int gpio_nand_exec_op(struct nand_chip *chip, in gpio_nand_exec_op()
142 unsigned int i; in gpio_nand_exec_op()
143 int ret = 0; in gpio_nand_exec_op()
149 gpiod_set_value(gpiomtd->nce, 0); in gpio_nand_exec_op()
150 for (i = 0; i < op->ninstrs; i++) { in gpio_nand_exec_op()
151 ret = gpio_nand_exec_instr(chip, &op->instrs[i]); in gpio_nand_exec_op()
155 if (op->instrs[i].delay_ns) in gpio_nand_exec_op()
156 ndelay(op->instrs[i].delay_ns); in gpio_nand_exec_op()
159 gpiod_set_value(gpiomtd->nce, 1); in gpio_nand_exec_op()
164 static int gpio_nand_attach_chip(struct nand_chip *chip) in gpio_nand_attach_chip()
166 if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT && in gpio_nand_attach_chip()
167 chip->ecc.algo == NAND_ECC_ALGO_UNKNOWN) in gpio_nand_attach_chip()
168 chip->ecc.algo = NAND_ECC_ALGO_HAMMING; in gpio_nand_attach_chip()
180 { .compatible = "gpio-control-nand" },
185 static int gpio_nand_get_config_of(const struct device *dev, in gpio_nand_get_config_of()
190 if (!dev->of_node) in gpio_nand_get_config_of()
191 return -ENODEV; in gpio_nand_get_config_of()
193 if (!of_property_read_u32(dev->of_node, "bank-width", &val)) { in gpio_nand_get_config_of()
195 plat->options |= NAND_BUSWIDTH_16; in gpio_nand_get_config_of()
197 dev_err(dev, "invalid bank-width %u\n", val); in gpio_nand_get_config_of()
198 return -EINVAL; in gpio_nand_get_config_of()
202 if (!of_property_read_u32(dev->of_node, "chip-delay", &val)) in gpio_nand_get_config_of()
203 plat->chip_delay = val; in gpio_nand_get_config_of()
213 if (of_property_read_u64(pdev->dev.of_node, in gpio_nand_get_io_sync_of()
214 "gpio-control-nand,io-sync-reg", &addr)) in gpio_nand_get_io_sync_of()
217 r = devm_kzalloc(&pdev->dev, sizeof(*r), GFP_KERNEL); in gpio_nand_get_io_sync_of()
221 r->start = addr; in gpio_nand_get_io_sync_of()
222 r->end = r->start + 0x3; in gpio_nand_get_io_sync_of()
223 r->flags = IORESOURCE_MEM; in gpio_nand_get_io_sync_of()
228 static inline int gpio_nand_get_config_of(const struct device *dev, in gpio_nand_get_config_of()
231 return -ENOSYS; in gpio_nand_get_config_of()
241 static inline int gpio_nand_get_config(const struct device *dev, in gpio_nand_get_config()
244 int ret = gpio_nand_get_config_of(dev, plat); in gpio_nand_get_config()
254 return -EINVAL; in gpio_nand_get_config()
268 static int gpio_nand_remove(struct platform_device *pdev) in gpio_nand_remove()
271 struct nand_chip *chip = &gpiomtd->nand_chip; in gpio_nand_remove()
272 int ret; in gpio_nand_remove()
279 if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp)) in gpio_nand_remove()
280 gpiod_set_value(gpiomtd->nwp, 0); in gpio_nand_remove()
281 if (gpiomtd->nce && !IS_ERR(gpiomtd->nce)) in gpio_nand_remove()
282 gpiod_set_value(gpiomtd->nce, 0); in gpio_nand_remove()
287 static int gpio_nand_probe(struct platform_device *pdev) in gpio_nand_probe()
293 struct device *dev = &pdev->dev; in gpio_nand_probe()
294 int ret = 0; in gpio_nand_probe()
296 if (!dev->of_node && !dev_get_platdata(dev)) in gpio_nand_probe()
297 return -EINVAL; in gpio_nand_probe()
301 return -ENOMEM; in gpio_nand_probe()
303 chip = &gpiomtd->nand_chip; in gpio_nand_probe()
305 gpiomtd->io = devm_platform_ioremap_resource(pdev, 0); in gpio_nand_probe()
306 if (IS_ERR(gpiomtd->io)) in gpio_nand_probe()
307 return PTR_ERR(gpiomtd->io); in gpio_nand_probe()
311 gpiomtd->io_sync = devm_ioremap_resource(dev, res); in gpio_nand_probe()
312 if (IS_ERR(gpiomtd->io_sync)) in gpio_nand_probe()
313 return PTR_ERR(gpiomtd->io_sync); in gpio_nand_probe()
316 ret = gpio_nand_get_config(dev, &gpiomtd->plat); in gpio_nand_probe()
321 gpiomtd->nce = devm_gpiod_get_optional(dev, "nce", GPIOD_OUT_HIGH); in gpio_nand_probe()
322 if (IS_ERR(gpiomtd->nce)) in gpio_nand_probe()
323 return PTR_ERR(gpiomtd->nce); in gpio_nand_probe()
326 gpiomtd->nwp = devm_gpiod_get_optional(dev, "nwp", GPIOD_OUT_LOW); in gpio_nand_probe()
327 if (IS_ERR(gpiomtd->nwp)) { in gpio_nand_probe()
328 ret = PTR_ERR(gpiomtd->nwp); in gpio_nand_probe()
332 gpiomtd->ale = devm_gpiod_get(dev, "ale", GPIOD_OUT_LOW); in gpio_nand_probe()
333 if (IS_ERR(gpiomtd->ale)) { in gpio_nand_probe()
334 ret = PTR_ERR(gpiomtd->ale); in gpio_nand_probe()
338 gpiomtd->cle = devm_gpiod_get(dev, "cle", GPIOD_OUT_LOW); in gpio_nand_probe()
339 if (IS_ERR(gpiomtd->cle)) { in gpio_nand_probe()
340 ret = PTR_ERR(gpiomtd->cle); in gpio_nand_probe()
344 gpiomtd->rdy = devm_gpiod_get_optional(dev, "rdy", GPIOD_IN); in gpio_nand_probe()
345 if (IS_ERR(gpiomtd->rdy)) { in gpio_nand_probe()
346 ret = PTR_ERR(gpiomtd->rdy); in gpio_nand_probe()
350 nand_controller_init(&gpiomtd->base); in gpio_nand_probe()
351 gpiomtd->base.ops = &gpio_nand_ops; in gpio_nand_probe()
353 nand_set_flash_node(chip, pdev->dev.of_node); in gpio_nand_probe()
354 chip->options = gpiomtd->plat.options; in gpio_nand_probe()
355 chip->controller = &gpiomtd->base; in gpio_nand_probe()
358 mtd->dev.parent = dev; in gpio_nand_probe()
363 if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp)) in gpio_nand_probe()
364 gpiod_direction_output(gpiomtd->nwp, 1); in gpio_nand_probe()
368 * Set ->engine_type before registering the NAND devices in order to in gpio_nand_probe()
371 chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT; in gpio_nand_probe()
377 if (gpiomtd->plat.adjust_parts) in gpio_nand_probe()
378 gpiomtd->plat.adjust_parts(&gpiomtd->plat, mtd->size); in gpio_nand_probe()
380 ret = mtd_device_register(mtd, gpiomtd->plat.parts, in gpio_nand_probe()
381 gpiomtd->plat.num_parts); in gpio_nand_probe()
386 if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp)) in gpio_nand_probe()
387 gpiod_set_value(gpiomtd->nwp, 0); in gpio_nand_probe()
389 if (gpiomtd->nce && !IS_ERR(gpiomtd->nce)) in gpio_nand_probe()
390 gpiod_set_value(gpiomtd->nce, 0); in gpio_nand_probe()
399 .name = "gpio-nand",
408 MODULE_DESCRIPTION("GPIO NAND Driver");