blob: 13de39aa3288f273695f6ae26c3687508a12bbf9 [file] [log] [blame]
Boris Brezillon48573932018-11-11 08:55:09 +01001// SPDX-License-Identifier: GPL-2.0
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +01002/*
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +01003 * Copyright (C) 2006 Jonathan McDowell <noodles@earth.li>
4 *
Boris Brezillon187c54482018-02-05 23:02:02 +01005 * Derived from drivers/mtd/nand/toto.c (removed in v2.6.28)
Boris Brezillon7b6afee2018-02-05 23:02:03 +01006 * Copyright (c) 2003 Texas Instruments
7 * Copyright (c) 2002 Thomas Gleixner <tgxl@linutronix.de>
8 *
Janusz Krzysztofik7e95d1f12010-12-14 21:09:40 +01009 * Converted to platform driver by Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
Boris Brezillon93cbd6f32018-02-05 23:02:00 +010010 * Partially stolen from plat_nand.c
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010011 *
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010012 * Overview:
13 * This is a device driver for the NAND flash device found on the
14 * Amstrad E3 (Delta).
15 */
16
17#include <linux/slab.h>
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010018#include <linux/module.h>
19#include <linux/delay.h>
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +020020#include <linux/gpio/consumer.h>
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010021#include <linux/mtd/mtd.h>
Janusz Krzysztofik1698ea32020-02-12 01:39:17 +010022#include <linux/mtd/nand-gpio.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020023#include <linux/mtd/rawnand.h>
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010024#include <linux/mtd/partitions.h>
Janusz Krzysztofikd1b1a8f2020-02-12 01:39:26 +010025#include <linux/of_device.h>
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +010026#include <linux/platform_device.h>
Boris Brezillonfbb080a2018-11-11 08:55:08 +010027#include <linux/sizes.h>
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010028
29/*
30 * MTD structure for E3 (Delta)
31 */
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +010032struct gpio_nand {
Boris Brezillon9fd6bcf2018-11-11 08:55:13 +010033 struct nand_controller base;
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +020034 struct nand_chip nand_chip;
35 struct gpio_desc *gpiod_rdy;
36 struct gpio_desc *gpiod_nce;
37 struct gpio_desc *gpiod_nre;
38 struct gpio_desc *gpiod_nwp;
39 struct gpio_desc *gpiod_nwe;
40 struct gpio_desc *gpiod_ale;
41 struct gpio_desc *gpiod_cle;
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +010042 struct gpio_descs *data_gpiods;
Janusz Krzysztofik9c076d72018-09-20 00:52:55 +020043 bool data_in;
Janusz Krzysztofikccada492020-02-12 01:39:22 +010044 unsigned int tRP;
45 unsigned int tWP;
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +010046 u8 (*io_read)(struct gpio_nand *this);
47 void (*io_write)(struct gpio_nand *this, u8 byte);
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +020048};
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010049
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +010050static void gpio_nand_write_commit(struct gpio_nand *priv)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010051{
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +020052 gpiod_set_value(priv->gpiod_nwe, 1);
Janusz Krzysztofikccada492020-02-12 01:39:22 +010053 ndelay(priv->tWP);
Janusz Krzysztofik241008e2020-02-12 01:39:21 +010054 gpiod_set_value(priv->gpiod_nwe, 0);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010055}
56
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +010057static void gpio_nand_io_write(struct gpio_nand *priv, u8 byte)
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +010058{
59 struct gpio_descs *data_gpiods = priv->data_gpiods;
60 DECLARE_BITMAP(values, BITS_PER_TYPE(byte)) = { byte, };
61
62 gpiod_set_raw_array_value(data_gpiods->ndescs, data_gpiods->desc,
63 data_gpiods->info, values);
64
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +010065 gpio_nand_write_commit(priv);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +010066}
67
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +010068static void gpio_nand_dir_output(struct gpio_nand *priv, u8 byte)
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +010069{
70 struct gpio_descs *data_gpiods = priv->data_gpiods;
71 DECLARE_BITMAP(values, BITS_PER_TYPE(byte)) = { byte, };
72 int i;
73
74 for (i = 0; i < data_gpiods->ndescs; i++)
75 gpiod_direction_output_raw(data_gpiods->desc[i],
76 test_bit(i, values));
77
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +010078 gpio_nand_write_commit(priv);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +010079
80 priv->data_in = false;
81}
82
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +010083static u8 gpio_nand_io_read(struct gpio_nand *priv)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010084{
Boris Brezillond54445d2018-11-11 08:55:10 +010085 u8 res;
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +010086 struct gpio_descs *data_gpiods = priv->data_gpiods;
87 DECLARE_BITMAP(values, BITS_PER_TYPE(res)) = { 0, };
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010088
Janusz Krzysztofik241008e2020-02-12 01:39:21 +010089 gpiod_set_value(priv->gpiod_nre, 1);
Janusz Krzysztofikccada492020-02-12 01:39:22 +010090 ndelay(priv->tRP);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +010091
92 gpiod_get_raw_array_value(data_gpiods->ndescs, data_gpiods->desc,
93 data_gpiods->info, values);
94
Janusz Krzysztofik241008e2020-02-12 01:39:21 +010095 gpiod_set_value(priv->gpiod_nre, 0);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010096
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +010097 res = values[0];
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010098 return res;
99}
100
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100101static void gpio_nand_dir_input(struct gpio_nand *priv)
Janusz Krzysztofik9c076d72018-09-20 00:52:55 +0200102{
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100103 struct gpio_descs *data_gpiods = priv->data_gpiods;
104 int i;
105
106 for (i = 0; i < data_gpiods->ndescs; i++)
107 gpiod_direction_input(data_gpiods->desc[i]);
108
109 priv->data_in = true;
Janusz Krzysztofik9c076d72018-09-20 00:52:55 +0200110}
111
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100112static void gpio_nand_write_buf(struct gpio_nand *priv, const u8 *buf, int len)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100113{
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100114 int i = 0;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100115
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100116 if (len > 0 && priv->data_in)
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100117 gpio_nand_dir_output(priv, buf[i++]);
Janusz Krzysztofik9c076d72018-09-20 00:52:55 +0200118
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100119 while (i < len)
Janusz Krzysztofik2b1dcee2020-02-12 01:39:28 +0100120 priv->io_write(priv, buf[i++]);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100121}
122
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100123static void gpio_nand_read_buf(struct gpio_nand *priv, u8 *buf, int len)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100124{
125 int i;
126
Janusz Krzysztofik2b1dcee2020-02-12 01:39:28 +0100127 if (priv->data_gpiods && !priv->data_in)
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100128 gpio_nand_dir_input(priv);
Janusz Krzysztofik9c076d72018-09-20 00:52:55 +0200129
130 for (i = 0; i < len; i++)
Janusz Krzysztofik2b1dcee2020-02-12 01:39:28 +0100131 buf[i] = priv->io_read(priv);
Janusz Krzysztofik9c076d72018-09-20 00:52:55 +0200132}
133
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100134static void gpio_nand_ctrl_cs(struct gpio_nand *priv, bool assert)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200135{
Janusz Krzysztofik241008e2020-02-12 01:39:21 +0100136 gpiod_set_value(priv->gpiod_nce, assert);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200137}
138
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100139static int gpio_nand_exec_op(struct nand_chip *this,
Janusz Krzysztofik861fbd62018-10-15 21:41:30 +0200140 const struct nand_operation *op, bool check_only)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100141{
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100142 struct gpio_nand *priv = nand_get_controller_data(this);
Janusz Krzysztofik861fbd62018-10-15 21:41:30 +0200143 const struct nand_op_instr *instr;
144 int ret = 0;
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200145
Janusz Krzysztofik861fbd62018-10-15 21:41:30 +0200146 if (check_only)
147 return 0;
148
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100149 gpio_nand_ctrl_cs(priv, 1);
Boris Brezillon17700222018-11-11 08:55:21 +0100150
Janusz Krzysztofik861fbd62018-10-15 21:41:30 +0200151 for (instr = op->instrs; instr < op->instrs + op->ninstrs; instr++) {
Janusz Krzysztofik861fbd62018-10-15 21:41:30 +0200152 switch (instr->type) {
153 case NAND_OP_CMD_INSTR:
154 gpiod_set_value(priv->gpiod_cle, 1);
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100155 gpio_nand_write_buf(priv, &instr->ctx.cmd.opcode, 1);
Janusz Krzysztofik861fbd62018-10-15 21:41:30 +0200156 gpiod_set_value(priv->gpiod_cle, 0);
157 break;
158
159 case NAND_OP_ADDR_INSTR:
160 gpiod_set_value(priv->gpiod_ale, 1);
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100161 gpio_nand_write_buf(priv, instr->ctx.addr.addrs,
Janusz Krzysztofik861fbd62018-10-15 21:41:30 +0200162 instr->ctx.addr.naddrs);
163 gpiod_set_value(priv->gpiod_ale, 0);
164 break;
165
166 case NAND_OP_DATA_IN_INSTR:
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100167 gpio_nand_read_buf(priv, instr->ctx.data.buf.in,
Janusz Krzysztofik861fbd62018-10-15 21:41:30 +0200168 instr->ctx.data.len);
169 break;
170
171 case NAND_OP_DATA_OUT_INSTR:
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100172 gpio_nand_write_buf(priv, instr->ctx.data.buf.out,
Janusz Krzysztofik861fbd62018-10-15 21:41:30 +0200173 instr->ctx.data.len);
174 break;
175
176 case NAND_OP_WAITRDY_INSTR:
177 ret = priv->gpiod_rdy ?
178 nand_gpio_waitrdy(this, priv->gpiod_rdy,
179 instr->ctx.waitrdy.timeout_ms) :
180 nand_soft_waitrdy(this,
181 instr->ctx.waitrdy.timeout_ms);
182 break;
183 }
184
185 if (ret)
186 break;
187 }
188
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100189 gpio_nand_ctrl_cs(priv, 0);
Boris Brezillon17700222018-11-11 08:55:21 +0100190
Janusz Krzysztofik861fbd62018-10-15 21:41:30 +0200191 return ret;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100192}
193
Miquel Raynal4c466672020-05-29 13:13:13 +0200194static int gpio_nand_setup_interface(struct nand_chip *this, int csline,
195 const struct nand_interface_config *cf)
Janusz Krzysztofikccada492020-02-12 01:39:22 +0100196{
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100197 struct gpio_nand *priv = nand_get_controller_data(this);
Janusz Krzysztofikccada492020-02-12 01:39:22 +0100198 const struct nand_sdr_timings *sdr = nand_get_sdr_timings(cf);
199 struct device *dev = &nand_to_mtd(this)->dev;
200
201 if (IS_ERR(sdr))
202 return PTR_ERR(sdr);
203
204 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
205 return 0;
206
Janusz Krzysztofik586a7462020-02-12 01:39:23 +0100207 if (priv->gpiod_nre) {
208 priv->tRP = DIV_ROUND_UP(sdr->tRP_min, 1000);
209 dev_dbg(dev, "using %u ns read pulse width\n", priv->tRP);
210 }
Janusz Krzysztofikccada492020-02-12 01:39:22 +0100211
212 priv->tWP = DIV_ROUND_UP(sdr->tWP_min, 1000);
213 dev_dbg(dev, "using %u ns write pulse width\n", priv->tWP);
214
215 return 0;
216}
217
Miquel Raynal59d93472020-11-13 13:34:08 +0100218static int gpio_nand_attach_chip(struct nand_chip *chip)
219{
Miquel Raynald707bb72021-09-29 00:22:40 +0200220 if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT &&
221 chip->ecc.algo == NAND_ECC_ALGO_UNKNOWN)
Miquel Raynal249a9592020-12-03 20:03:32 +0100222 chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
Miquel Raynal59d93472020-11-13 13:34:08 +0100223
224 return 0;
225}
226
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100227static const struct nand_controller_ops gpio_nand_ops = {
228 .exec_op = gpio_nand_exec_op,
Miquel Raynal59d93472020-11-13 13:34:08 +0100229 .attach_chip = gpio_nand_attach_chip,
Miquel Raynal4c466672020-05-29 13:13:13 +0200230 .setup_interface = gpio_nand_setup_interface,
Boris Brezillonf2abfeb2018-11-11 08:55:23 +0100231};
232
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100233/*
234 * Main initialization routine
235 */
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100236static int gpio_nand_probe(struct platform_device *pdev)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100237{
Janusz Krzysztofik1698ea32020-02-12 01:39:17 +0100238 struct gpio_nand_platdata *pdata = dev_get_platdata(&pdev->dev);
Janusz Krzysztofikd7ffe382020-02-12 01:39:19 +0100239 const struct mtd_partition *partitions = NULL;
240 int num_partitions = 0;
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100241 struct gpio_nand *priv;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100242 struct nand_chip *this;
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200243 struct mtd_info *mtd;
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100244 int (*probe)(struct platform_device *pdev, struct gpio_nand *priv);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100245 int err = 0;
246
Janusz Krzysztofik1698ea32020-02-12 01:39:17 +0100247 if (pdata) {
248 partitions = pdata->parts;
249 num_partitions = pdata->num_parts;
250 }
251
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100252 /* Allocate memory for MTD device structure and private data */
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100253 priv = devm_kzalloc(&pdev->dev, sizeof(struct gpio_nand),
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200254 GFP_KERNEL);
Boris Brezillond54445d2018-11-11 08:55:10 +0100255 if (!priv)
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200256 return -ENOMEM;
Boris Brezillond54445d2018-11-11 08:55:10 +0100257
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200258 this = &priv->nand_chip;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100259
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200260 mtd = nand_to_mtd(this);
261 mtd->dev.parent = &pdev->dev;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100262
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200263 nand_set_controller_data(this, priv);
Janusz Krzysztofik2cef3d42020-02-12 01:39:20 +0100264 nand_set_flash_node(this, pdev->dev.of_node);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100265
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200266 priv->gpiod_rdy = devm_gpiod_get_optional(&pdev->dev, "rdy", GPIOD_IN);
267 if (IS_ERR(priv->gpiod_rdy)) {
268 err = PTR_ERR(priv->gpiod_rdy);
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200269 dev_warn(&pdev->dev, "RDY GPIO request failed (%d)\n", err);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100270 return err;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100271 }
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200272
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200273 platform_set_drvdata(pdev, priv);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100274
Janusz Krzysztofik91a1abf2020-02-12 01:39:16 +0100275 /* Set chip enabled but write protected */
Janusz Krzysztofikea5ea9f2020-02-12 01:39:24 +0100276 priv->gpiod_nwp = devm_gpiod_get_optional(&pdev->dev, "nwp",
277 GPIOD_OUT_HIGH);
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200278 if (IS_ERR(priv->gpiod_nwp)) {
279 err = PTR_ERR(priv->gpiod_nwp);
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200280 dev_err(&pdev->dev, "NWP GPIO request failed (%d)\n", err);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100281 return err;
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200282 }
283
Janusz Krzysztofikea5ea9f2020-02-12 01:39:24 +0100284 priv->gpiod_nce = devm_gpiod_get_optional(&pdev->dev, "nce",
285 GPIOD_OUT_LOW);
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200286 if (IS_ERR(priv->gpiod_nce)) {
287 err = PTR_ERR(priv->gpiod_nce);
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200288 dev_err(&pdev->dev, "NCE GPIO request failed (%d)\n", err);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100289 return err;
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200290 }
291
Janusz Krzysztofik586a7462020-02-12 01:39:23 +0100292 priv->gpiod_nre = devm_gpiod_get_optional(&pdev->dev, "nre",
293 GPIOD_OUT_LOW);
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200294 if (IS_ERR(priv->gpiod_nre)) {
295 err = PTR_ERR(priv->gpiod_nre);
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200296 dev_err(&pdev->dev, "NRE GPIO request failed (%d)\n", err);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100297 return err;
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200298 }
299
Janusz Krzysztofik2b1dcee2020-02-12 01:39:28 +0100300 priv->gpiod_nwe = devm_gpiod_get_optional(&pdev->dev, "nwe",
301 GPIOD_OUT_LOW);
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200302 if (IS_ERR(priv->gpiod_nwe)) {
303 err = PTR_ERR(priv->gpiod_nwe);
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200304 dev_err(&pdev->dev, "NWE GPIO request failed (%d)\n", err);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100305 return err;
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200306 }
307
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200308 priv->gpiod_ale = devm_gpiod_get(&pdev->dev, "ale", GPIOD_OUT_LOW);
309 if (IS_ERR(priv->gpiod_ale)) {
310 err = PTR_ERR(priv->gpiod_ale);
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200311 dev_err(&pdev->dev, "ALE GPIO request failed (%d)\n", err);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100312 return err;
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200313 }
314
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200315 priv->gpiod_cle = devm_gpiod_get(&pdev->dev, "cle", GPIOD_OUT_LOW);
316 if (IS_ERR(priv->gpiod_cle)) {
317 err = PTR_ERR(priv->gpiod_cle);
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200318 dev_err(&pdev->dev, "CLE GPIO request failed (%d)\n", err);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100319 return err;
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200320 }
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100321
Janusz Krzysztofik97738612018-11-21 12:08:04 +0100322 /* Request array of data pins, initialize them as input */
Janusz Krzysztofik2b1dcee2020-02-12 01:39:28 +0100323 priv->data_gpiods = devm_gpiod_get_array_optional(&pdev->dev, "data",
324 GPIOD_IN);
Janusz Krzysztofikedfd8d92020-02-12 01:39:27 +0100325 if (IS_ERR(priv->data_gpiods)) {
326 err = PTR_ERR(priv->data_gpiods);
Janusz Krzysztofik97738612018-11-21 12:08:04 +0100327 dev_err(&pdev->dev, "data GPIO request failed: %d\n", err);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100328 return err;
Janusz Krzysztofik97738612018-11-21 12:08:04 +0100329 }
Janusz Krzysztofik2b1dcee2020-02-12 01:39:28 +0100330 if (priv->data_gpiods) {
331 if (!priv->gpiod_nwe) {
332 dev_err(&pdev->dev,
333 "mandatory NWE pin not provided by platform\n");
334 return -ENODEV;
335 }
336
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100337 priv->io_read = gpio_nand_io_read;
338 priv->io_write = gpio_nand_io_write;
Janusz Krzysztofik2b1dcee2020-02-12 01:39:28 +0100339 priv->data_in = true;
340 }
Janusz Krzysztofik9c076d72018-09-20 00:52:55 +0200341
Janusz Krzysztofikd1b1a8f2020-02-12 01:39:26 +0100342 if (pdev->id_entry)
343 probe = (void *) pdev->id_entry->driver_data;
344 else
345 probe = of_device_get_match_data(&pdev->dev);
346 if (probe)
347 err = probe(pdev, priv);
348 if (err)
349 return err;
350
Janusz Krzysztofik2b1dcee2020-02-12 01:39:28 +0100351 if (!priv->io_read || !priv->io_write) {
352 dev_err(&pdev->dev, "incomplete device configuration\n");
353 return -ENODEV;
354 }
355
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100356 /* Initialize the NAND controller object embedded in gpio_nand. */
357 priv->base.ops = &gpio_nand_ops;
Boris Brezillon9fd6bcf2018-11-11 08:55:13 +0100358 nand_controller_init(&priv->base);
359 this->controller = &priv->base;
360
Janusz Krzysztofik91a1abf2020-02-12 01:39:16 +0100361 /*
362 * FIXME: We should release write protection only after nand_scan() to
363 * be on the safe side but we can't do that until we have a generic way
364 * to assert/deassert WP from the core. Even if the core shouldn't
365 * write things in the nand_scan() path, it should have control on this
366 * pin just in case we ever need to disable write protection during
367 * chip detection/initialization.
368 */
369 /* Release write protection */
Janusz Krzysztofik241008e2020-02-12 01:39:21 +0100370 gpiod_set_value(priv->gpiod_nwp, 0);
Janusz Krzysztofik91a1abf2020-02-12 01:39:16 +0100371
Miquel Raynald707bb72021-09-29 00:22:40 +0200372 /*
373 * This driver assumes that the default ECC engine should be TYPE_SOFT.
374 * Set ->engine_type before registering the NAND devices in order to
375 * provide a driver specific default value.
376 */
377 this->ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
378
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300379 /* Scan to find existence of the device */
Boris Brezillon00ad3782018-09-06 14:05:14 +0200380 err = nand_scan(this, 1);
Masahiro Yamada0d0aa862016-11-04 19:42:49 +0900381 if (err)
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100382 return err;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100383
384 /* Register the partitions */
Janusz Krzysztofik1698ea32020-02-12 01:39:17 +0100385 err = mtd_device_register(mtd, partitions, num_partitions);
Boris Brezillon876ba602018-11-11 08:55:12 +0100386 if (err)
387 goto err_nand_cleanup;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100388
Boris Brezillon8bbc3c02018-11-11 08:55:11 +0100389 return 0;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100390
Boris Brezillon876ba602018-11-11 08:55:12 +0100391err_nand_cleanup:
392 nand_cleanup(this);
393
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100394 return err;
395}
396
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100397/*
398 * Clean up routine
399 */
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100400static int gpio_nand_remove(struct platform_device *pdev)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100401{
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100402 struct gpio_nand *priv = platform_get_drvdata(pdev);
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200403 struct mtd_info *mtd = nand_to_mtd(&priv->nand_chip);
Miquel Raynal08f25cd2020-05-19 14:59:34 +0200404 int ret;
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100405
Janusz Krzysztofik91a1abf2020-02-12 01:39:16 +0100406 /* Apply write protection */
Janusz Krzysztofik241008e2020-02-12 01:39:21 +0100407 gpiod_set_value(priv->gpiod_nwp, 1);
Janusz Krzysztofik91a1abf2020-02-12 01:39:16 +0100408
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100409 /* Unregister device */
Miquel Raynal08f25cd2020-05-19 14:59:34 +0200410 ret = mtd_device_unregister(mtd);
411 WARN_ON(ret);
412 nand_cleanup(mtd_to_nand(mtd));
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100413
Janusz Krzysztofik7e95d1f12010-12-14 21:09:40 +0100414 return 0;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100415}
Janusz Krzysztofik7e95d1f12010-12-14 21:09:40 +0100416
Janusz Krzysztofik6d111782020-09-19 10:04:03 +0200417#ifdef CONFIG_OF
Janusz Krzysztofik7c2f66a2020-02-12 01:39:25 +0100418static const struct of_device_id gpio_nand_of_id_table[] = {
419 {
420 /* sentinel */
421 },
422};
423MODULE_DEVICE_TABLE(of, gpio_nand_of_id_table);
Janusz Krzysztofik6d111782020-09-19 10:04:03 +0200424#endif
Janusz Krzysztofik7c2f66a2020-02-12 01:39:25 +0100425
426static const struct platform_device_id gpio_nand_plat_id_table[] = {
427 {
428 .name = "ams-delta-nand",
429 }, {
430 /* sentinel */
431 },
432};
433MODULE_DEVICE_TABLE(platform, gpio_nand_plat_id_table);
434
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100435static struct platform_driver gpio_nand_driver = {
436 .probe = gpio_nand_probe,
437 .remove = gpio_nand_remove,
Janusz Krzysztofik7c2f66a2020-02-12 01:39:25 +0100438 .id_table = gpio_nand_plat_id_table,
Janusz Krzysztofik7e95d1f12010-12-14 21:09:40 +0100439 .driver = {
440 .name = "ams-delta-nand",
Janusz Krzysztofik7c2f66a2020-02-12 01:39:25 +0100441 .of_match_table = of_match_ptr(gpio_nand_of_id_table),
Janusz Krzysztofik7e95d1f12010-12-14 21:09:40 +0100442 },
443};
444
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100445module_platform_driver(gpio_nand_driver);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100446
Boris Brezillon48573932018-11-11 08:55:09 +0100447MODULE_LICENSE("GPL v2");
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100448MODULE_AUTHOR("Jonathan McDowell <noodles@earth.li>");
449MODULE_DESCRIPTION("Glue layer for NAND flash on Amstrad E3 (Delta)");