| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Updated, and converted to generic GPIO based driver by Russell King. |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 9 | 10 | * Device driver for NAND flash that uses a memory mapped interface to |
|---|
| 10 | 11 | * read/write the NAND commands and data, and GPIO pins for control signals |
|---|
| 11 | 12 | * (the DT binding refers to this as "GPIO assisted NAND flash") |
|---|
| 12 | | - * |
|---|
| 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. |
|---|
| 16 | | - * |
|---|
| 17 | 13 | */ |
|---|
| 18 | 14 | |
|---|
| 19 | 15 | #include <linux/kernel.h> |
|---|
| .. | .. |
|---|
| 29 | 25 | #include <linux/mtd/nand-gpio.h> |
|---|
| 30 | 26 | #include <linux/of.h> |
|---|
| 31 | 27 | #include <linux/of_address.h> |
|---|
| 28 | +#include <linux/delay.h> |
|---|
| 32 | 29 | |
|---|
| 33 | 30 | struct gpiomtd { |
|---|
| 31 | + struct nand_controller base; |
|---|
| 32 | + void __iomem *io; |
|---|
| 34 | 33 | void __iomem *io_sync; |
|---|
| 35 | 34 | struct nand_chip nand_chip; |
|---|
| 36 | 35 | struct gpio_nand_platdata plat; |
|---|
| .. | .. |
|---|
| 73 | 72 | static inline void gpio_nand_dosync(struct gpiomtd *gpiomtd) {} |
|---|
| 74 | 73 | #endif |
|---|
| 75 | 74 | |
|---|
| 76 | | -static void gpio_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl) |
|---|
| 75 | +static int gpio_nand_exec_instr(struct nand_chip *chip, |
|---|
| 76 | + const struct nand_op_instr *instr) |
|---|
| 77 | 77 | { |
|---|
| 78 | | - struct gpiomtd *gpiomtd = gpio_nand_getpriv(mtd); |
|---|
| 78 | + struct gpiomtd *gpiomtd = gpio_nand_getpriv(nand_to_mtd(chip)); |
|---|
| 79 | + unsigned int i; |
|---|
| 79 | 80 | |
|---|
| 80 | | - gpio_nand_dosync(gpiomtd); |
|---|
| 81 | | - |
|---|
| 82 | | - if (ctrl & NAND_CTRL_CHANGE) { |
|---|
| 83 | | - if (gpiomtd->nce) |
|---|
| 84 | | - gpiod_set_value(gpiomtd->nce, !(ctrl & NAND_NCE)); |
|---|
| 85 | | - gpiod_set_value(gpiomtd->cle, !!(ctrl & NAND_CLE)); |
|---|
| 86 | | - gpiod_set_value(gpiomtd->ale, !!(ctrl & NAND_ALE)); |
|---|
| 81 | + switch (instr->type) { |
|---|
| 82 | + case NAND_OP_CMD_INSTR: |
|---|
| 87 | 83 | gpio_nand_dosync(gpiomtd); |
|---|
| 84 | + gpiod_set_value(gpiomtd->cle, 1); |
|---|
| 85 | + gpio_nand_dosync(gpiomtd); |
|---|
| 86 | + writeb(instr->ctx.cmd.opcode, gpiomtd->io); |
|---|
| 87 | + gpio_nand_dosync(gpiomtd); |
|---|
| 88 | + gpiod_set_value(gpiomtd->cle, 0); |
|---|
| 89 | + return 0; |
|---|
| 90 | + |
|---|
| 91 | + case NAND_OP_ADDR_INSTR: |
|---|
| 92 | + gpio_nand_dosync(gpiomtd); |
|---|
| 93 | + gpiod_set_value(gpiomtd->ale, 1); |
|---|
| 94 | + gpio_nand_dosync(gpiomtd); |
|---|
| 95 | + for (i = 0; i < instr->ctx.addr.naddrs; i++) |
|---|
| 96 | + writeb(instr->ctx.addr.addrs[i], gpiomtd->io); |
|---|
| 97 | + gpio_nand_dosync(gpiomtd); |
|---|
| 98 | + gpiod_set_value(gpiomtd->ale, 0); |
|---|
| 99 | + return 0; |
|---|
| 100 | + |
|---|
| 101 | + case NAND_OP_DATA_IN_INSTR: |
|---|
| 102 | + gpio_nand_dosync(gpiomtd); |
|---|
| 103 | + if ((chip->options & NAND_BUSWIDTH_16) && |
|---|
| 104 | + !instr->ctx.data.force_8bit) |
|---|
| 105 | + ioread16_rep(gpiomtd->io, instr->ctx.data.buf.in, |
|---|
| 106 | + instr->ctx.data.len / 2); |
|---|
| 107 | + else |
|---|
| 108 | + ioread8_rep(gpiomtd->io, instr->ctx.data.buf.in, |
|---|
| 109 | + instr->ctx.data.len); |
|---|
| 110 | + return 0; |
|---|
| 111 | + |
|---|
| 112 | + case NAND_OP_DATA_OUT_INSTR: |
|---|
| 113 | + gpio_nand_dosync(gpiomtd); |
|---|
| 114 | + if ((chip->options & NAND_BUSWIDTH_16) && |
|---|
| 115 | + !instr->ctx.data.force_8bit) |
|---|
| 116 | + iowrite16_rep(gpiomtd->io, instr->ctx.data.buf.out, |
|---|
| 117 | + instr->ctx.data.len / 2); |
|---|
| 118 | + else |
|---|
| 119 | + iowrite8_rep(gpiomtd->io, instr->ctx.data.buf.out, |
|---|
| 120 | + instr->ctx.data.len); |
|---|
| 121 | + return 0; |
|---|
| 122 | + |
|---|
| 123 | + case NAND_OP_WAITRDY_INSTR: |
|---|
| 124 | + if (!gpiomtd->rdy) |
|---|
| 125 | + return nand_soft_waitrdy(chip, instr->ctx.waitrdy.timeout_ms); |
|---|
| 126 | + |
|---|
| 127 | + return nand_gpio_waitrdy(chip, gpiomtd->rdy, |
|---|
| 128 | + instr->ctx.waitrdy.timeout_ms); |
|---|
| 129 | + |
|---|
| 130 | + default: |
|---|
| 131 | + return -EINVAL; |
|---|
| 88 | 132 | } |
|---|
| 89 | | - if (cmd == NAND_CMD_NONE) |
|---|
| 90 | | - return; |
|---|
| 91 | 133 | |
|---|
| 92 | | - writeb(cmd, gpiomtd->nand_chip.IO_ADDR_W); |
|---|
| 93 | | - gpio_nand_dosync(gpiomtd); |
|---|
| 134 | + return 0; |
|---|
| 94 | 135 | } |
|---|
| 95 | 136 | |
|---|
| 96 | | -static int gpio_nand_devready(struct mtd_info *mtd) |
|---|
| 137 | +static int gpio_nand_exec_op(struct nand_chip *chip, |
|---|
| 138 | + const struct nand_operation *op, |
|---|
| 139 | + bool check_only) |
|---|
| 97 | 140 | { |
|---|
| 98 | | - struct gpiomtd *gpiomtd = gpio_nand_getpriv(mtd); |
|---|
| 141 | + struct gpiomtd *gpiomtd = gpio_nand_getpriv(nand_to_mtd(chip)); |
|---|
| 142 | + unsigned int i; |
|---|
| 143 | + int ret = 0; |
|---|
| 99 | 144 | |
|---|
| 100 | | - return gpiod_get_value(gpiomtd->rdy); |
|---|
| 145 | + if (check_only) |
|---|
| 146 | + return 0; |
|---|
| 147 | + |
|---|
| 148 | + gpio_nand_dosync(gpiomtd); |
|---|
| 149 | + gpiod_set_value(gpiomtd->nce, 0); |
|---|
| 150 | + for (i = 0; i < op->ninstrs; i++) { |
|---|
| 151 | + ret = gpio_nand_exec_instr(chip, &op->instrs[i]); |
|---|
| 152 | + if (ret) |
|---|
| 153 | + break; |
|---|
| 154 | + |
|---|
| 155 | + if (op->instrs[i].delay_ns) |
|---|
| 156 | + ndelay(op->instrs[i].delay_ns); |
|---|
| 157 | + } |
|---|
| 158 | + gpio_nand_dosync(gpiomtd); |
|---|
| 159 | + gpiod_set_value(gpiomtd->nce, 1); |
|---|
| 160 | + |
|---|
| 161 | + return ret; |
|---|
| 101 | 162 | } |
|---|
| 163 | + |
|---|
| 164 | +static int gpio_nand_attach_chip(struct nand_chip *chip) |
|---|
| 165 | +{ |
|---|
| 166 | + if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT && |
|---|
| 167 | + chip->ecc.algo == NAND_ECC_ALGO_UNKNOWN) |
|---|
| 168 | + chip->ecc.algo = NAND_ECC_ALGO_HAMMING; |
|---|
| 169 | + |
|---|
| 170 | + return 0; |
|---|
| 171 | +} |
|---|
| 172 | + |
|---|
| 173 | +static const struct nand_controller_ops gpio_nand_ops = { |
|---|
| 174 | + .exec_op = gpio_nand_exec_op, |
|---|
| 175 | + .attach_chip = gpio_nand_attach_chip, |
|---|
| 176 | +}; |
|---|
| 102 | 177 | |
|---|
| 103 | 178 | #ifdef CONFIG_OF |
|---|
| 104 | 179 | static const struct of_device_id gpio_nand_id_table[] = { |
|---|
| .. | .. |
|---|
| 193 | 268 | static int gpio_nand_remove(struct platform_device *pdev) |
|---|
| 194 | 269 | { |
|---|
| 195 | 270 | struct gpiomtd *gpiomtd = platform_get_drvdata(pdev); |
|---|
| 271 | + struct nand_chip *chip = &gpiomtd->nand_chip; |
|---|
| 272 | + int ret; |
|---|
| 196 | 273 | |
|---|
| 197 | | - nand_release(&gpiomtd->nand_chip); |
|---|
| 274 | + ret = mtd_device_unregister(nand_to_mtd(chip)); |
|---|
| 275 | + WARN_ON(ret); |
|---|
| 276 | + nand_cleanup(chip); |
|---|
| 198 | 277 | |
|---|
| 199 | 278 | /* Enable write protection and disable the chip */ |
|---|
| 200 | 279 | if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp)) |
|---|
| .. | .. |
|---|
| 224 | 303 | chip = &gpiomtd->nand_chip; |
|---|
| 225 | 304 | |
|---|
| 226 | 305 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
|---|
| 227 | | - chip->IO_ADDR_R = devm_ioremap_resource(dev, res); |
|---|
| 228 | | - if (IS_ERR(chip->IO_ADDR_R)) |
|---|
| 229 | | - return PTR_ERR(chip->IO_ADDR_R); |
|---|
| 306 | + gpiomtd->io = devm_ioremap_resource(dev, res); |
|---|
| 307 | + if (IS_ERR(gpiomtd->io)) |
|---|
| 308 | + return PTR_ERR(gpiomtd->io); |
|---|
| 230 | 309 | |
|---|
| 231 | 310 | res = gpio_nand_get_io_sync(pdev); |
|---|
| 232 | 311 | if (res) { |
|---|
| .. | .. |
|---|
| 268 | 347 | ret = PTR_ERR(gpiomtd->rdy); |
|---|
| 269 | 348 | goto out_ce; |
|---|
| 270 | 349 | } |
|---|
| 271 | | - /* Using RDY pin */ |
|---|
| 272 | | - if (gpiomtd->rdy) |
|---|
| 273 | | - chip->dev_ready = gpio_nand_devready; |
|---|
| 350 | + |
|---|
| 351 | + nand_controller_init(&gpiomtd->base); |
|---|
| 352 | + gpiomtd->base.ops = &gpio_nand_ops; |
|---|
| 274 | 353 | |
|---|
| 275 | 354 | nand_set_flash_node(chip, pdev->dev.of_node); |
|---|
| 276 | | - chip->IO_ADDR_W = chip->IO_ADDR_R; |
|---|
| 277 | | - chip->ecc.mode = NAND_ECC_SOFT; |
|---|
| 278 | | - chip->ecc.algo = NAND_ECC_HAMMING; |
|---|
| 279 | 355 | chip->options = gpiomtd->plat.options; |
|---|
| 280 | | - chip->chip_delay = gpiomtd->plat.chip_delay; |
|---|
| 281 | | - chip->cmd_ctrl = gpio_nand_cmd_ctrl; |
|---|
| 356 | + chip->controller = &gpiomtd->base; |
|---|
| 282 | 357 | |
|---|
| 283 | 358 | mtd = nand_to_mtd(chip); |
|---|
| 284 | 359 | mtd->dev.parent = dev; |
|---|
| .. | .. |
|---|
| 289 | 364 | if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp)) |
|---|
| 290 | 365 | gpiod_direction_output(gpiomtd->nwp, 1); |
|---|
| 291 | 366 | |
|---|
| 367 | + /* |
|---|
| 368 | + * This driver assumes that the default ECC engine should be TYPE_SOFT. |
|---|
| 369 | + * Set ->engine_type before registering the NAND devices in order to |
|---|
| 370 | + * provide a driver specific default value. |
|---|
| 371 | + */ |
|---|
| 372 | + chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT; |
|---|
| 373 | + |
|---|
| 292 | 374 | ret = nand_scan(chip, 1); |
|---|
| 293 | 375 | if (ret) |
|---|
| 294 | 376 | goto err_wp; |
|---|