hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/mtd/nand/raw/gpio.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Updated, and converted to generic GPIO based driver by Russell King.
34 *
....@@ -9,11 +10,6 @@
910 * Device driver for NAND flash that uses a memory mapped interface to
1011 * read/write the NAND commands and data, and GPIO pins for control signals
1112 * (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
- *
1713 */
1814
1915 #include <linux/kernel.h>
....@@ -29,8 +25,11 @@
2925 #include <linux/mtd/nand-gpio.h>
3026 #include <linux/of.h>
3127 #include <linux/of_address.h>
28
+#include <linux/delay.h>
3229
3330 struct gpiomtd {
31
+ struct nand_controller base;
32
+ void __iomem *io;
3433 void __iomem *io_sync;
3534 struct nand_chip nand_chip;
3635 struct gpio_nand_platdata plat;
....@@ -73,32 +72,108 @@
7372 static inline void gpio_nand_dosync(struct gpiomtd *gpiomtd) {}
7473 #endif
7574
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)
7777 {
78
- struct gpiomtd *gpiomtd = gpio_nand_getpriv(mtd);
78
+ struct gpiomtd *gpiomtd = gpio_nand_getpriv(nand_to_mtd(chip));
79
+ unsigned int i;
7980
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:
8783 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;
88132 }
89
- if (cmd == NAND_CMD_NONE)
90
- return;
91133
92
- writeb(cmd, gpiomtd->nand_chip.IO_ADDR_W);
93
- gpio_nand_dosync(gpiomtd);
134
+ return 0;
94135 }
95136
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)
97140 {
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;
99144
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;
101162 }
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
+};
102177
103178 #ifdef CONFIG_OF
104179 static const struct of_device_id gpio_nand_id_table[] = {
....@@ -193,8 +268,12 @@
193268 static int gpio_nand_remove(struct platform_device *pdev)
194269 {
195270 struct gpiomtd *gpiomtd = platform_get_drvdata(pdev);
271
+ struct nand_chip *chip = &gpiomtd->nand_chip;
272
+ int ret;
196273
197
- nand_release(&gpiomtd->nand_chip);
274
+ ret = mtd_device_unregister(nand_to_mtd(chip));
275
+ WARN_ON(ret);
276
+ nand_cleanup(chip);
198277
199278 /* Enable write protection and disable the chip */
200279 if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
....@@ -224,9 +303,9 @@
224303 chip = &gpiomtd->nand_chip;
225304
226305 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);
230309
231310 res = gpio_nand_get_io_sync(pdev);
232311 if (res) {
....@@ -268,17 +347,13 @@
268347 ret = PTR_ERR(gpiomtd->rdy);
269348 goto out_ce;
270349 }
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;
274353
275354 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;
279355 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;
282357
283358 mtd = nand_to_mtd(chip);
284359 mtd->dev.parent = dev;
....@@ -289,6 +364,13 @@
289364 if (gpiomtd->nwp && !IS_ERR(gpiomtd->nwp))
290365 gpiod_direction_output(gpiomtd->nwp, 1);
291366
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
+
292374 ret = nand_scan(chip, 1);
293375 if (ret)
294376 goto err_wp;