blob: fdba155416d25bdb83e617b04ac2fd6a905cb1f0 [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
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100218static const struct nand_controller_ops gpio_nand_ops = {
219 .exec_op = gpio_nand_exec_op,
Miquel Raynal4c466672020-05-29 13:13:13 +0200220 .setup_interface = gpio_nand_setup_interface,
Boris Brezillonf2abfeb2018-11-11 08:55:23 +0100221};
222
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100223/*
224 * Main initialization routine
225 */
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100226static int gpio_nand_probe(struct platform_device *pdev)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100227{
Janusz Krzysztofik1698ea32020-02-12 01:39:17 +0100228 struct gpio_nand_platdata *pdata = dev_get_platdata(&pdev->dev);
Janusz Krzysztofikd7ffe382020-02-12 01:39:19 +0100229 const struct mtd_partition *partitions = NULL;
230 int num_partitions = 0;
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100231 struct gpio_nand *priv;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100232 struct nand_chip *this;
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200233 struct mtd_info *mtd;
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100234 int (*probe)(struct platform_device *pdev, struct gpio_nand *priv);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100235 int err = 0;
236
Janusz Krzysztofik1698ea32020-02-12 01:39:17 +0100237 if (pdata) {
238 partitions = pdata->parts;
239 num_partitions = pdata->num_parts;
240 }
241
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100242 /* Allocate memory for MTD device structure and private data */
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100243 priv = devm_kzalloc(&pdev->dev, sizeof(struct gpio_nand),
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200244 GFP_KERNEL);
Boris Brezillond54445d2018-11-11 08:55:10 +0100245 if (!priv)
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200246 return -ENOMEM;
Boris Brezillond54445d2018-11-11 08:55:10 +0100247
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200248 this = &priv->nand_chip;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100249
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200250 mtd = nand_to_mtd(this);
251 mtd->dev.parent = &pdev->dev;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100252
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200253 nand_set_controller_data(this, priv);
Janusz Krzysztofik2cef3d42020-02-12 01:39:20 +0100254 nand_set_flash_node(this, pdev->dev.of_node);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100255
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200256 priv->gpiod_rdy = devm_gpiod_get_optional(&pdev->dev, "rdy", GPIOD_IN);
257 if (IS_ERR(priv->gpiod_rdy)) {
258 err = PTR_ERR(priv->gpiod_rdy);
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200259 dev_warn(&pdev->dev, "RDY GPIO request failed (%d)\n", err);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100260 return err;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100261 }
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200262
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200263 this->ecc.mode = NAND_ECC_SOFT;
Rafał Miłeckie58dd3c2016-04-08 12:23:44 +0200264 this->ecc.algo = NAND_ECC_HAMMING;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100265
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200266 platform_set_drvdata(pdev, priv);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100267
Janusz Krzysztofik91a1abf2020-02-12 01:39:16 +0100268 /* Set chip enabled but write protected */
Janusz Krzysztofikea5ea9f2020-02-12 01:39:24 +0100269 priv->gpiod_nwp = devm_gpiod_get_optional(&pdev->dev, "nwp",
270 GPIOD_OUT_HIGH);
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200271 if (IS_ERR(priv->gpiod_nwp)) {
272 err = PTR_ERR(priv->gpiod_nwp);
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200273 dev_err(&pdev->dev, "NWP GPIO request failed (%d)\n", err);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100274 return err;
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200275 }
276
Janusz Krzysztofikea5ea9f2020-02-12 01:39:24 +0100277 priv->gpiod_nce = devm_gpiod_get_optional(&pdev->dev, "nce",
278 GPIOD_OUT_LOW);
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200279 if (IS_ERR(priv->gpiod_nce)) {
280 err = PTR_ERR(priv->gpiod_nce);
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200281 dev_err(&pdev->dev, "NCE GPIO request failed (%d)\n", err);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100282 return err;
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200283 }
284
Janusz Krzysztofik586a7462020-02-12 01:39:23 +0100285 priv->gpiod_nre = devm_gpiod_get_optional(&pdev->dev, "nre",
286 GPIOD_OUT_LOW);
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200287 if (IS_ERR(priv->gpiod_nre)) {
288 err = PTR_ERR(priv->gpiod_nre);
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200289 dev_err(&pdev->dev, "NRE GPIO request failed (%d)\n", err);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100290 return err;
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200291 }
292
Janusz Krzysztofik2b1dcee2020-02-12 01:39:28 +0100293 priv->gpiod_nwe = devm_gpiod_get_optional(&pdev->dev, "nwe",
294 GPIOD_OUT_LOW);
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200295 if (IS_ERR(priv->gpiod_nwe)) {
296 err = PTR_ERR(priv->gpiod_nwe);
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200297 dev_err(&pdev->dev, "NWE GPIO request failed (%d)\n", err);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100298 return err;
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200299 }
300
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200301 priv->gpiod_ale = devm_gpiod_get(&pdev->dev, "ale", GPIOD_OUT_LOW);
302 if (IS_ERR(priv->gpiod_ale)) {
303 err = PTR_ERR(priv->gpiod_ale);
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200304 dev_err(&pdev->dev, "ALE 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_cle = devm_gpiod_get(&pdev->dev, "cle", GPIOD_OUT_LOW);
309 if (IS_ERR(priv->gpiod_cle)) {
310 err = PTR_ERR(priv->gpiod_cle);
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200311 dev_err(&pdev->dev, "CLE GPIO request failed (%d)\n", err);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100312 return err;
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200313 }
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100314
Janusz Krzysztofik97738612018-11-21 12:08:04 +0100315 /* Request array of data pins, initialize them as input */
Janusz Krzysztofik2b1dcee2020-02-12 01:39:28 +0100316 priv->data_gpiods = devm_gpiod_get_array_optional(&pdev->dev, "data",
317 GPIOD_IN);
Janusz Krzysztofikedfd8d92020-02-12 01:39:27 +0100318 if (IS_ERR(priv->data_gpiods)) {
319 err = PTR_ERR(priv->data_gpiods);
Janusz Krzysztofik97738612018-11-21 12:08:04 +0100320 dev_err(&pdev->dev, "data GPIO request failed: %d\n", err);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100321 return err;
Janusz Krzysztofik97738612018-11-21 12:08:04 +0100322 }
Janusz Krzysztofik2b1dcee2020-02-12 01:39:28 +0100323 if (priv->data_gpiods) {
324 if (!priv->gpiod_nwe) {
325 dev_err(&pdev->dev,
326 "mandatory NWE pin not provided by platform\n");
327 return -ENODEV;
328 }
329
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100330 priv->io_read = gpio_nand_io_read;
331 priv->io_write = gpio_nand_io_write;
Janusz Krzysztofik2b1dcee2020-02-12 01:39:28 +0100332 priv->data_in = true;
333 }
Janusz Krzysztofik9c076d72018-09-20 00:52:55 +0200334
Janusz Krzysztofikd1b1a8f2020-02-12 01:39:26 +0100335 if (pdev->id_entry)
336 probe = (void *) pdev->id_entry->driver_data;
337 else
338 probe = of_device_get_match_data(&pdev->dev);
339 if (probe)
340 err = probe(pdev, priv);
341 if (err)
342 return err;
343
Janusz Krzysztofik2b1dcee2020-02-12 01:39:28 +0100344 if (!priv->io_read || !priv->io_write) {
345 dev_err(&pdev->dev, "incomplete device configuration\n");
346 return -ENODEV;
347 }
348
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100349 /* Initialize the NAND controller object embedded in gpio_nand. */
350 priv->base.ops = &gpio_nand_ops;
Boris Brezillon9fd6bcf2018-11-11 08:55:13 +0100351 nand_controller_init(&priv->base);
352 this->controller = &priv->base;
353
Janusz Krzysztofik91a1abf2020-02-12 01:39:16 +0100354 /*
355 * FIXME: We should release write protection only after nand_scan() to
356 * be on the safe side but we can't do that until we have a generic way
357 * to assert/deassert WP from the core. Even if the core shouldn't
358 * write things in the nand_scan() path, it should have control on this
359 * pin just in case we ever need to disable write protection during
360 * chip detection/initialization.
361 */
362 /* Release write protection */
Janusz Krzysztofik241008e2020-02-12 01:39:21 +0100363 gpiod_set_value(priv->gpiod_nwp, 0);
Janusz Krzysztofik91a1abf2020-02-12 01:39:16 +0100364
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300365 /* Scan to find existence of the device */
Boris Brezillon00ad3782018-09-06 14:05:14 +0200366 err = nand_scan(this, 1);
Masahiro Yamada0d0aa862016-11-04 19:42:49 +0900367 if (err)
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100368 return err;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100369
370 /* Register the partitions */
Janusz Krzysztofik1698ea32020-02-12 01:39:17 +0100371 err = mtd_device_register(mtd, partitions, num_partitions);
Boris Brezillon876ba602018-11-11 08:55:12 +0100372 if (err)
373 goto err_nand_cleanup;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100374
Boris Brezillon8bbc3c02018-11-11 08:55:11 +0100375 return 0;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100376
Boris Brezillon876ba602018-11-11 08:55:12 +0100377err_nand_cleanup:
378 nand_cleanup(this);
379
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100380 return err;
381}
382
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100383/*
384 * Clean up routine
385 */
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100386static int gpio_nand_remove(struct platform_device *pdev)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100387{
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100388 struct gpio_nand *priv = platform_get_drvdata(pdev);
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200389 struct mtd_info *mtd = nand_to_mtd(&priv->nand_chip);
Miquel Raynal08f25cd2020-05-19 14:59:34 +0200390 int ret;
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100391
Janusz Krzysztofik91a1abf2020-02-12 01:39:16 +0100392 /* Apply write protection */
Janusz Krzysztofik241008e2020-02-12 01:39:21 +0100393 gpiod_set_value(priv->gpiod_nwp, 1);
Janusz Krzysztofik91a1abf2020-02-12 01:39:16 +0100394
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100395 /* Unregister device */
Miquel Raynal08f25cd2020-05-19 14:59:34 +0200396 ret = mtd_device_unregister(mtd);
397 WARN_ON(ret);
398 nand_cleanup(mtd_to_nand(mtd));
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100399
Janusz Krzysztofik7e95d1f12010-12-14 21:09:40 +0100400 return 0;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100401}
Janusz Krzysztofik7e95d1f12010-12-14 21:09:40 +0100402
Janusz Krzysztofik7c2f66a2020-02-12 01:39:25 +0100403static const struct of_device_id gpio_nand_of_id_table[] = {
404 {
405 /* sentinel */
406 },
407};
408MODULE_DEVICE_TABLE(of, gpio_nand_of_id_table);
409
410static const struct platform_device_id gpio_nand_plat_id_table[] = {
411 {
412 .name = "ams-delta-nand",
413 }, {
414 /* sentinel */
415 },
416};
417MODULE_DEVICE_TABLE(platform, gpio_nand_plat_id_table);
418
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100419static struct platform_driver gpio_nand_driver = {
420 .probe = gpio_nand_probe,
421 .remove = gpio_nand_remove,
Janusz Krzysztofik7c2f66a2020-02-12 01:39:25 +0100422 .id_table = gpio_nand_plat_id_table,
Janusz Krzysztofik7e95d1f12010-12-14 21:09:40 +0100423 .driver = {
424 .name = "ams-delta-nand",
Janusz Krzysztofik7c2f66a2020-02-12 01:39:25 +0100425 .of_match_table = of_match_ptr(gpio_nand_of_id_table),
Janusz Krzysztofik7e95d1f12010-12-14 21:09:40 +0100426 },
427};
428
Janusz Krzysztofik16d00cd2020-02-12 01:39:29 +0100429module_platform_driver(gpio_nand_driver);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100430
Boris Brezillon48573932018-11-11 08:55:09 +0100431MODULE_LICENSE("GPL v2");
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100432MODULE_AUTHOR("Jonathan McDowell <noodles@earth.li>");
433MODULE_DESCRIPTION("Glue layer for NAND flash on Amstrad E3 (Delta)");