blob: a493f1dc6677aa035d6357197480c346167317fd [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 Krzysztofik7416bd32018-11-21 12:08:05 +010025#include <linux/platform_device.h>
Boris Brezillonfbb080a2018-11-11 08:55:08 +010026#include <linux/sizes.h>
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010027
28/*
29 * MTD structure for E3 (Delta)
30 */
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +020031struct ams_delta_nand {
Boris Brezillon9fd6bcf2018-11-11 08:55:13 +010032 struct nand_controller base;
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +020033 struct nand_chip nand_chip;
34 struct gpio_desc *gpiod_rdy;
35 struct gpio_desc *gpiod_nce;
36 struct gpio_desc *gpiod_nre;
37 struct gpio_desc *gpiod_nwp;
38 struct gpio_desc *gpiod_nwe;
39 struct gpio_desc *gpiod_ale;
40 struct gpio_desc *gpiod_cle;
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +010041 struct gpio_descs *data_gpiods;
Janusz Krzysztofik9c076d72018-09-20 00:52:55 +020042 bool data_in;
Janusz Krzysztofikccada492020-02-12 01:39:22 +010043 unsigned int tRP;
44 unsigned int tWP;
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +020045};
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010046
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +010047static void ams_delta_write_commit(struct ams_delta_nand *priv)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010048{
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +020049 gpiod_set_value(priv->gpiod_nwe, 1);
Janusz Krzysztofikccada492020-02-12 01:39:22 +010050 ndelay(priv->tWP);
Janusz Krzysztofik241008e2020-02-12 01:39:21 +010051 gpiod_set_value(priv->gpiod_nwe, 0);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010052}
53
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +010054static void ams_delta_io_write(struct ams_delta_nand *priv, u8 byte)
55{
56 struct gpio_descs *data_gpiods = priv->data_gpiods;
57 DECLARE_BITMAP(values, BITS_PER_TYPE(byte)) = { byte, };
58
59 gpiod_set_raw_array_value(data_gpiods->ndescs, data_gpiods->desc,
60 data_gpiods->info, values);
61
62 ams_delta_write_commit(priv);
63}
64
65static void ams_delta_dir_output(struct ams_delta_nand *priv, u8 byte)
66{
67 struct gpio_descs *data_gpiods = priv->data_gpiods;
68 DECLARE_BITMAP(values, BITS_PER_TYPE(byte)) = { byte, };
69 int i;
70
71 for (i = 0; i < data_gpiods->ndescs; i++)
72 gpiod_direction_output_raw(data_gpiods->desc[i],
73 test_bit(i, values));
74
75 ams_delta_write_commit(priv);
76
77 priv->data_in = false;
78}
79
Boris Brezillond54445d2018-11-11 08:55:10 +010080static u8 ams_delta_io_read(struct ams_delta_nand *priv)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010081{
Boris Brezillond54445d2018-11-11 08:55:10 +010082 u8 res;
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +010083 struct gpio_descs *data_gpiods = priv->data_gpiods;
84 DECLARE_BITMAP(values, BITS_PER_TYPE(res)) = { 0, };
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010085
Janusz Krzysztofik241008e2020-02-12 01:39:21 +010086 gpiod_set_value(priv->gpiod_nre, 1);
Janusz Krzysztofikccada492020-02-12 01:39:22 +010087 ndelay(priv->tRP);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +010088
89 gpiod_get_raw_array_value(data_gpiods->ndescs, data_gpiods->desc,
90 data_gpiods->info, values);
91
Janusz Krzysztofik241008e2020-02-12 01:39:21 +010092 gpiod_set_value(priv->gpiod_nre, 0);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010093
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +010094 res = values[0];
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010095 return res;
96}
97
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +010098static void ams_delta_dir_input(struct ams_delta_nand *priv)
Janusz Krzysztofik9c076d72018-09-20 00:52:55 +020099{
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100100 struct gpio_descs *data_gpiods = priv->data_gpiods;
101 int i;
102
103 for (i = 0; i < data_gpiods->ndescs; i++)
104 gpiod_direction_input(data_gpiods->desc[i]);
105
106 priv->data_in = true;
Janusz Krzysztofik9c076d72018-09-20 00:52:55 +0200107}
108
Boris Brezillond54445d2018-11-11 08:55:10 +0100109static void ams_delta_write_buf(struct ams_delta_nand *priv, const u8 *buf,
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100110 int len)
111{
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100112 int i = 0;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100113
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100114 if (len > 0 && priv->data_in)
115 ams_delta_dir_output(priv, buf[i++]);
Janusz Krzysztofik9c076d72018-09-20 00:52:55 +0200116
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100117 while (i < len)
118 ams_delta_io_write(priv, buf[i++]);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100119}
120
Boris Brezillond54445d2018-11-11 08:55:10 +0100121static void ams_delta_read_buf(struct ams_delta_nand *priv, u8 *buf, int len)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100122{
123 int i;
124
Janusz Krzysztofik9c076d72018-09-20 00:52:55 +0200125 if (!priv->data_in)
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100126 ams_delta_dir_input(priv);
Janusz Krzysztofik9c076d72018-09-20 00:52:55 +0200127
128 for (i = 0; i < len; i++)
129 buf[i] = ams_delta_io_read(priv);
130}
131
Boris Brezillon17700222018-11-11 08:55:21 +0100132static void ams_delta_ctrl_cs(struct ams_delta_nand *priv, bool assert)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200133{
Janusz Krzysztofik241008e2020-02-12 01:39:21 +0100134 gpiod_set_value(priv->gpiod_nce, assert);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200135}
136
Janusz Krzysztofik861fbd62018-10-15 21:41:30 +0200137static int ams_delta_exec_op(struct nand_chip *this,
138 const struct nand_operation *op, bool check_only)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100139{
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200140 struct ams_delta_nand *priv = nand_get_controller_data(this);
Janusz Krzysztofik861fbd62018-10-15 21:41:30 +0200141 const struct nand_op_instr *instr;
142 int ret = 0;
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200143
Janusz Krzysztofik861fbd62018-10-15 21:41:30 +0200144 if (check_only)
145 return 0;
146
Boris Brezillon17700222018-11-11 08:55:21 +0100147 ams_delta_ctrl_cs(priv, 1);
148
Janusz Krzysztofik861fbd62018-10-15 21:41:30 +0200149 for (instr = op->instrs; instr < op->instrs + op->ninstrs; instr++) {
Janusz Krzysztofik861fbd62018-10-15 21:41:30 +0200150 switch (instr->type) {
151 case NAND_OP_CMD_INSTR:
152 gpiod_set_value(priv->gpiod_cle, 1);
153 ams_delta_write_buf(priv, &instr->ctx.cmd.opcode, 1);
154 gpiod_set_value(priv->gpiod_cle, 0);
155 break;
156
157 case NAND_OP_ADDR_INSTR:
158 gpiod_set_value(priv->gpiod_ale, 1);
159 ams_delta_write_buf(priv, instr->ctx.addr.addrs,
160 instr->ctx.addr.naddrs);
161 gpiod_set_value(priv->gpiod_ale, 0);
162 break;
163
164 case NAND_OP_DATA_IN_INSTR:
165 ams_delta_read_buf(priv, instr->ctx.data.buf.in,
166 instr->ctx.data.len);
167 break;
168
169 case NAND_OP_DATA_OUT_INSTR:
170 ams_delta_write_buf(priv, instr->ctx.data.buf.out,
171 instr->ctx.data.len);
172 break;
173
174 case NAND_OP_WAITRDY_INSTR:
175 ret = priv->gpiod_rdy ?
176 nand_gpio_waitrdy(this, priv->gpiod_rdy,
177 instr->ctx.waitrdy.timeout_ms) :
178 nand_soft_waitrdy(this,
179 instr->ctx.waitrdy.timeout_ms);
180 break;
181 }
182
183 if (ret)
184 break;
185 }
186
Boris Brezillon17700222018-11-11 08:55:21 +0100187 ams_delta_ctrl_cs(priv, 0);
188
Janusz Krzysztofik861fbd62018-10-15 21:41:30 +0200189 return ret;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100190}
191
Janusz Krzysztofikccada492020-02-12 01:39:22 +0100192static int ams_delta_setup_data_interface(struct nand_chip *this, int csline,
193 const struct nand_data_interface *cf)
194{
195 struct ams_delta_nand *priv = nand_get_controller_data(this);
196 const struct nand_sdr_timings *sdr = nand_get_sdr_timings(cf);
197 struct device *dev = &nand_to_mtd(this)->dev;
198
199 if (IS_ERR(sdr))
200 return PTR_ERR(sdr);
201
202 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
203 return 0;
204
Janusz Krzysztofik586a7462020-02-12 01:39:23 +0100205 if (priv->gpiod_nre) {
206 priv->tRP = DIV_ROUND_UP(sdr->tRP_min, 1000);
207 dev_dbg(dev, "using %u ns read pulse width\n", priv->tRP);
208 }
Janusz Krzysztofikccada492020-02-12 01:39:22 +0100209
210 priv->tWP = DIV_ROUND_UP(sdr->tWP_min, 1000);
211 dev_dbg(dev, "using %u ns write pulse width\n", priv->tWP);
212
213 return 0;
214}
215
Boris Brezillonf2abfeb2018-11-11 08:55:23 +0100216static const struct nand_controller_ops ams_delta_ops = {
217 .exec_op = ams_delta_exec_op,
Janusz Krzysztofikccada492020-02-12 01:39:22 +0100218 .setup_data_interface = ams_delta_setup_data_interface,
Boris Brezillonf2abfeb2018-11-11 08:55:23 +0100219};
220
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100221/*
222 * Main initialization routine
223 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500224static int ams_delta_init(struct platform_device *pdev)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100225{
Janusz Krzysztofik1698ea32020-02-12 01:39:17 +0100226 struct gpio_nand_platdata *pdata = dev_get_platdata(&pdev->dev);
Janusz Krzysztofikd7ffe382020-02-12 01:39:19 +0100227 const struct mtd_partition *partitions = NULL;
228 int num_partitions = 0;
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200229 struct ams_delta_nand *priv;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100230 struct nand_chip *this;
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200231 struct mtd_info *mtd;
Janusz Krzysztofik97738612018-11-21 12:08:04 +0100232 struct gpio_descs *data_gpiods;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100233 int err = 0;
234
Janusz Krzysztofik1698ea32020-02-12 01:39:17 +0100235 if (pdata) {
236 partitions = pdata->parts;
237 num_partitions = pdata->num_parts;
238 }
239
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100240 /* Allocate memory for MTD device structure and private data */
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200241 priv = devm_kzalloc(&pdev->dev, sizeof(struct ams_delta_nand),
242 GFP_KERNEL);
Boris Brezillond54445d2018-11-11 08:55:10 +0100243 if (!priv)
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200244 return -ENOMEM;
Boris Brezillond54445d2018-11-11 08:55:10 +0100245
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200246 this = &priv->nand_chip;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100247
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200248 mtd = nand_to_mtd(this);
249 mtd->dev.parent = &pdev->dev;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100250
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200251 nand_set_controller_data(this, priv);
Janusz Krzysztofik2cef3d42020-02-12 01:39:20 +0100252 nand_set_flash_node(this, pdev->dev.of_node);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100253
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200254 priv->gpiod_rdy = devm_gpiod_get_optional(&pdev->dev, "rdy", GPIOD_IN);
255 if (IS_ERR(priv->gpiod_rdy)) {
256 err = PTR_ERR(priv->gpiod_rdy);
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200257 dev_warn(&pdev->dev, "RDY GPIO request failed (%d)\n", err);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100258 return err;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100259 }
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200260
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200261 this->ecc.mode = NAND_ECC_SOFT;
Rafał Miłeckie58dd3c2016-04-08 12:23:44 +0200262 this->ecc.algo = NAND_ECC_HAMMING;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100263
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200264 platform_set_drvdata(pdev, priv);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100265
Janusz Krzysztofik91a1abf2020-02-12 01:39:16 +0100266 /* Set chip enabled but write protected */
Janusz Krzysztofikea5ea9f2020-02-12 01:39:24 +0100267 priv->gpiod_nwp = devm_gpiod_get_optional(&pdev->dev, "nwp",
268 GPIOD_OUT_HIGH);
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200269 if (IS_ERR(priv->gpiod_nwp)) {
270 err = PTR_ERR(priv->gpiod_nwp);
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200271 dev_err(&pdev->dev, "NWP GPIO request failed (%d)\n", err);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100272 return err;
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200273 }
274
Janusz Krzysztofikea5ea9f2020-02-12 01:39:24 +0100275 priv->gpiod_nce = devm_gpiod_get_optional(&pdev->dev, "nce",
276 GPIOD_OUT_LOW);
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200277 if (IS_ERR(priv->gpiod_nce)) {
278 err = PTR_ERR(priv->gpiod_nce);
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200279 dev_err(&pdev->dev, "NCE GPIO request failed (%d)\n", err);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100280 return err;
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200281 }
282
Janusz Krzysztofik586a7462020-02-12 01:39:23 +0100283 priv->gpiod_nre = devm_gpiod_get_optional(&pdev->dev, "nre",
284 GPIOD_OUT_LOW);
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200285 if (IS_ERR(priv->gpiod_nre)) {
286 err = PTR_ERR(priv->gpiod_nre);
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200287 dev_err(&pdev->dev, "NRE GPIO request failed (%d)\n", err);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100288 return err;
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200289 }
290
Janusz Krzysztofik241008e2020-02-12 01:39:21 +0100291 priv->gpiod_nwe = devm_gpiod_get(&pdev->dev, "nwe", GPIOD_OUT_LOW);
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200292 if (IS_ERR(priv->gpiod_nwe)) {
293 err = PTR_ERR(priv->gpiod_nwe);
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200294 dev_err(&pdev->dev, "NWE GPIO request failed (%d)\n", err);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100295 return err;
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200296 }
297
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200298 priv->gpiod_ale = devm_gpiod_get(&pdev->dev, "ale", GPIOD_OUT_LOW);
299 if (IS_ERR(priv->gpiod_ale)) {
300 err = PTR_ERR(priv->gpiod_ale);
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200301 dev_err(&pdev->dev, "ALE GPIO request failed (%d)\n", err);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100302 return err;
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200303 }
304
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200305 priv->gpiod_cle = devm_gpiod_get(&pdev->dev, "cle", GPIOD_OUT_LOW);
306 if (IS_ERR(priv->gpiod_cle)) {
307 err = PTR_ERR(priv->gpiod_cle);
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200308 dev_err(&pdev->dev, "CLE GPIO request failed (%d)\n", err);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100309 return err;
Janusz Krzysztofikf1a97e02018-09-20 00:17:29 +0200310 }
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100311
Janusz Krzysztofik97738612018-11-21 12:08:04 +0100312 /* Request array of data pins, initialize them as input */
313 data_gpiods = devm_gpiod_get_array(&pdev->dev, "data", GPIOD_IN);
314 if (IS_ERR(data_gpiods)) {
315 err = PTR_ERR(data_gpiods);
316 dev_err(&pdev->dev, "data GPIO request failed: %d\n", err);
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100317 return err;
Janusz Krzysztofik97738612018-11-21 12:08:04 +0100318 }
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100319 priv->data_gpiods = data_gpiods;
Janusz Krzysztofik97738612018-11-21 12:08:04 +0100320 priv->data_in = true;
Janusz Krzysztofik9c076d72018-09-20 00:52:55 +0200321
Boris Brezillon9fd6bcf2018-11-11 08:55:13 +0100322 /* Initialize the NAND controller object embedded in ams_delta_nand. */
Boris Brezillonf2abfeb2018-11-11 08:55:23 +0100323 priv->base.ops = &ams_delta_ops;
Boris Brezillon9fd6bcf2018-11-11 08:55:13 +0100324 nand_controller_init(&priv->base);
325 this->controller = &priv->base;
326
Janusz Krzysztofik91a1abf2020-02-12 01:39:16 +0100327 /*
328 * FIXME: We should release write protection only after nand_scan() to
329 * be on the safe side but we can't do that until we have a generic way
330 * to assert/deassert WP from the core. Even if the core shouldn't
331 * write things in the nand_scan() path, it should have control on this
332 * pin just in case we ever need to disable write protection during
333 * chip detection/initialization.
334 */
335 /* Release write protection */
Janusz Krzysztofik241008e2020-02-12 01:39:21 +0100336 gpiod_set_value(priv->gpiod_nwp, 0);
Janusz Krzysztofik91a1abf2020-02-12 01:39:16 +0100337
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300338 /* Scan to find existence of the device */
Boris Brezillon00ad3782018-09-06 14:05:14 +0200339 err = nand_scan(this, 1);
Masahiro Yamada0d0aa862016-11-04 19:42:49 +0900340 if (err)
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100341 return err;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100342
343 /* Register the partitions */
Janusz Krzysztofik1698ea32020-02-12 01:39:17 +0100344 err = mtd_device_register(mtd, partitions, num_partitions);
Boris Brezillon876ba602018-11-11 08:55:12 +0100345 if (err)
346 goto err_nand_cleanup;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100347
Boris Brezillon8bbc3c02018-11-11 08:55:11 +0100348 return 0;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100349
Boris Brezillon876ba602018-11-11 08:55:12 +0100350err_nand_cleanup:
351 nand_cleanup(this);
352
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100353 return err;
354}
355
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100356/*
357 * Clean up routine
358 */
Bill Pemberton810b7e02012-11-19 13:26:04 -0500359static int ams_delta_cleanup(struct platform_device *pdev)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100360{
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200361 struct ams_delta_nand *priv = platform_get_drvdata(pdev);
362 struct mtd_info *mtd = nand_to_mtd(&priv->nand_chip);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100363
Janusz Krzysztofik91a1abf2020-02-12 01:39:16 +0100364 /* Apply write protection */
Janusz Krzysztofik241008e2020-02-12 01:39:21 +0100365 gpiod_set_value(priv->gpiod_nwp, 1);
Janusz Krzysztofik91a1abf2020-02-12 01:39:16 +0100366
Janusz Krzysztofik7416bd32018-11-21 12:08:05 +0100367 /* Unregister device */
Janusz Krzysztofik2b44af32018-09-20 00:52:54 +0200368 nand_release(mtd_to_nand(mtd));
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100369
Janusz Krzysztofik7e95d1f12010-12-14 21:09:40 +0100370 return 0;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100371}
Janusz Krzysztofik7e95d1f12010-12-14 21:09:40 +0100372
Janusz Krzysztofik7c2f66a2020-02-12 01:39:25 +0100373static const struct of_device_id gpio_nand_of_id_table[] = {
374 {
375 /* sentinel */
376 },
377};
378MODULE_DEVICE_TABLE(of, gpio_nand_of_id_table);
379
380static const struct platform_device_id gpio_nand_plat_id_table[] = {
381 {
382 .name = "ams-delta-nand",
383 }, {
384 /* sentinel */
385 },
386};
387MODULE_DEVICE_TABLE(platform, gpio_nand_plat_id_table);
388
Janusz Krzysztofik7e95d1f12010-12-14 21:09:40 +0100389static struct platform_driver ams_delta_nand_driver = {
390 .probe = ams_delta_init,
Bill Pemberton5153b882012-11-19 13:21:24 -0500391 .remove = ams_delta_cleanup,
Janusz Krzysztofik7c2f66a2020-02-12 01:39:25 +0100392 .id_table = gpio_nand_plat_id_table,
Janusz Krzysztofik7e95d1f12010-12-14 21:09:40 +0100393 .driver = {
394 .name = "ams-delta-nand",
Janusz Krzysztofik7c2f66a2020-02-12 01:39:25 +0100395 .of_match_table = of_match_ptr(gpio_nand_of_id_table),
Janusz Krzysztofik7e95d1f12010-12-14 21:09:40 +0100396 },
397};
398
Axel Linf99640d2011-11-27 20:45:03 +0800399module_platform_driver(ams_delta_nand_driver);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100400
Boris Brezillon48573932018-11-11 08:55:09 +0100401MODULE_LICENSE("GPL v2");
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100402MODULE_AUTHOR("Jonathan McDowell <noodles@earth.li>");
403MODULE_DESCRIPTION("Glue layer for NAND flash on Amstrad E3 (Delta)");