blob: 235a2f7b1baddc30aaa04d96d9bef46ff800af83 [file] [log] [blame]
Ian Moltonec43b812008-07-15 16:04:22 +01001/*
2 * Toshiba TMIO NAND flash controller driver
3 *
4 * Slightly murky pre-git history of the driver:
5 *
6 * Copyright (c) Ian Molton 2004, 2005, 2008
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007 * Original work, independent of sharps code. Included hardware ECC support.
Ian Moltonec43b812008-07-15 16:04:22 +01008 * Hard ECC did not work for writes in the early revisions.
9 * Copyright (c) Dirk Opfer 2005.
10 * Modifications developed from sharps code but
11 * NOT containing any, ported onto Ians base.
12 * Copyright (c) Chris Humbert 2005
13 * Copyright (c) Dmitry Baryshkov 2008
14 * Minor fixes
15 *
16 * Parts copyright Sebastian Carlier
17 *
18 * This file is licensed under
19 * the terms of the GNU General Public License version 2. This program
20 * is licensed "as is" without any warranty of any kind, whether express
21 * or implied.
22 *
23 */
24
25
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/platform_device.h>
29#include <linux/mfd/core.h>
30#include <linux/mfd/tmio.h>
31#include <linux/delay.h>
32#include <linux/io.h>
33#include <linux/irq.h>
34#include <linux/interrupt.h>
35#include <linux/ioport.h>
36#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020037#include <linux/mtd/rawnand.h>
Ian Moltonec43b812008-07-15 16:04:22 +010038#include <linux/mtd/nand_ecc.h>
39#include <linux/mtd/partitions.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Ian Moltonec43b812008-07-15 16:04:22 +010041
42/*--------------------------------------------------------------------------*/
43
44/*
45 * NAND Flash Host Controller Configuration Register
46 */
47#define CCR_COMMAND 0x04 /* w Command */
48#define CCR_BASE 0x10 /* l NAND Flash Control Reg Base Addr */
49#define CCR_INTP 0x3d /* b Interrupt Pin */
50#define CCR_INTE 0x48 /* b Interrupt Enable */
51#define CCR_EC 0x4a /* b Event Control */
52#define CCR_ICC 0x4c /* b Internal Clock Control */
53#define CCR_ECCC 0x5b /* b ECC Control */
54#define CCR_NFTC 0x60 /* b NAND Flash Transaction Control */
55#define CCR_NFM 0x61 /* b NAND Flash Monitor */
56#define CCR_NFPSC 0x62 /* b NAND Flash Power Supply Control */
57#define CCR_NFDC 0x63 /* b NAND Flash Detect Control */
58
59/*
60 * NAND Flash Control Register
61 */
62#define FCR_DATA 0x00 /* bwl Data Register */
63#define FCR_MODE 0x04 /* b Mode Register */
64#define FCR_STATUS 0x05 /* b Status Register */
65#define FCR_ISR 0x06 /* b Interrupt Status Register */
66#define FCR_IMR 0x07 /* b Interrupt Mask Register */
67
68/* FCR_MODE Register Command List */
69#define FCR_MODE_DATA 0x94 /* Data Data_Mode */
70#define FCR_MODE_COMMAND 0x95 /* Data Command_Mode */
71#define FCR_MODE_ADDRESS 0x96 /* Data Address_Mode */
72
73#define FCR_MODE_HWECC_CALC 0xB4 /* HW-ECC Data */
74#define FCR_MODE_HWECC_RESULT 0xD4 /* HW-ECC Calc result Read_Mode */
75#define FCR_MODE_HWECC_RESET 0xF4 /* HW-ECC Reset */
76
77#define FCR_MODE_POWER_ON 0x0C /* Power Supply ON to SSFDC card */
78#define FCR_MODE_POWER_OFF 0x08 /* Power Supply OFF to SSFDC card */
79
80#define FCR_MODE_LED_OFF 0x00 /* LED OFF */
81#define FCR_MODE_LED_ON 0x04 /* LED ON */
82
83#define FCR_MODE_EJECT_ON 0x68 /* Ejection events active */
84#define FCR_MODE_EJECT_OFF 0x08 /* Ejection events ignored */
85
86#define FCR_MODE_LOCK 0x6C /* Lock_Mode. Eject Switch Invalid */
87#define FCR_MODE_UNLOCK 0x0C /* UnLock_Mode. Eject Switch is valid */
88
89#define FCR_MODE_CONTROLLER_ID 0x40 /* Controller ID Read */
90#define FCR_MODE_STANDBY 0x00 /* SSFDC card Changes Standby State */
91
92#define FCR_MODE_WE 0x80
93#define FCR_MODE_ECC1 0x40
94#define FCR_MODE_ECC0 0x20
95#define FCR_MODE_CE 0x10
96#define FCR_MODE_PCNT1 0x08
97#define FCR_MODE_PCNT0 0x04
98#define FCR_MODE_ALE 0x02
99#define FCR_MODE_CLE 0x01
100
101#define FCR_STATUS_BUSY 0x80
102
103/*--------------------------------------------------------------------------*/
104
105struct tmio_nand {
Ian Moltonec43b812008-07-15 16:04:22 +0100106 struct nand_chip chip;
Boris Brezillona0916c92018-11-20 11:57:17 +0100107 struct completion comp;
Ian Moltonec43b812008-07-15 16:04:22 +0100108
109 struct platform_device *dev;
110
111 void __iomem *ccr;
112 void __iomem *fcr;
Dmitry Baryshkov076c7f42008-09-04 13:28:33 +0400113 unsigned long fcr_base;
Ian Moltonec43b812008-07-15 16:04:22 +0100114
115 unsigned int irq;
116
117 /* for tmio_nand_read_byte */
118 u8 read;
119 unsigned read_good:1;
120};
121
Boris BREZILLON66c95952015-12-10 09:00:27 +0100122static inline struct tmio_nand *mtd_to_tmio(struct mtd_info *mtd)
123{
124 return container_of(mtd_to_nand(mtd), struct tmio_nand, chip);
125}
Ian Moltonec43b812008-07-15 16:04:22 +0100126
Ian Moltonec43b812008-07-15 16:04:22 +0100127
128/*--------------------------------------------------------------------------*/
129
Boris Brezillon0f808c12018-09-06 14:05:26 +0200130static void tmio_nand_hwcontrol(struct nand_chip *chip, int cmd,
131 unsigned int ctrl)
Ian Moltonec43b812008-07-15 16:04:22 +0100132{
Boris Brezillon0f808c12018-09-06 14:05:26 +0200133 struct tmio_nand *tmio = mtd_to_tmio(nand_to_mtd(chip));
Ian Moltonec43b812008-07-15 16:04:22 +0100134
135 if (ctrl & NAND_CTRL_CHANGE) {
136 u8 mode;
137
138 if (ctrl & NAND_NCE) {
139 mode = FCR_MODE_DATA;
140
141 if (ctrl & NAND_CLE)
142 mode |= FCR_MODE_CLE;
143 else
144 mode &= ~FCR_MODE_CLE;
145
146 if (ctrl & NAND_ALE)
147 mode |= FCR_MODE_ALE;
148 else
149 mode &= ~FCR_MODE_ALE;
150 } else {
151 mode = FCR_MODE_STANDBY;
152 }
153
154 tmio_iowrite8(mode, tmio->fcr + FCR_MODE);
155 tmio->read_good = 0;
156 }
157
158 if (cmd != NAND_CMD_NONE)
Boris Brezillon82fc5092018-09-07 00:38:34 +0200159 tmio_iowrite8(cmd, chip->legacy.IO_ADDR_W);
Ian Moltonec43b812008-07-15 16:04:22 +0100160}
161
Boris Brezillon50a487e2018-09-06 14:05:27 +0200162static int tmio_nand_dev_ready(struct nand_chip *chip)
Ian Moltonec43b812008-07-15 16:04:22 +0100163{
Boris Brezillon50a487e2018-09-06 14:05:27 +0200164 struct tmio_nand *tmio = mtd_to_tmio(nand_to_mtd(chip));
Ian Moltonec43b812008-07-15 16:04:22 +0100165
166 return !(tmio_ioread8(tmio->fcr + FCR_STATUS) & FCR_STATUS_BUSY);
167}
168
169static irqreturn_t tmio_irq(int irq, void *__tmio)
170{
171 struct tmio_nand *tmio = __tmio;
Ian Moltonec43b812008-07-15 16:04:22 +0100172
173 /* disable RDYREQ interrupt */
174 tmio_iowrite8(0x00, tmio->fcr + FCR_IMR);
Boris Brezillona0916c92018-11-20 11:57:17 +0100175 complete(&tmio->comp);
Ian Moltonec43b812008-07-15 16:04:22 +0100176
Ian Moltonec43b812008-07-15 16:04:22 +0100177 return IRQ_HANDLED;
178}
179
180/*
181 *The TMIO core has a RDYREQ interrupt on the posedge of #SMRB.
182 *This interrupt is normally disabled, but for long operations like
183 *erase and write, we enable it to wake us up. The irq handler
184 *disables the interrupt.
185 */
Boris Brezillonf1d46942018-09-06 14:05:29 +0200186static int tmio_nand_wait(struct nand_chip *nand_chip)
Ian Moltonec43b812008-07-15 16:04:22 +0100187{
Boris Brezillonf1d46942018-09-06 14:05:29 +0200188 struct tmio_nand *tmio = mtd_to_tmio(nand_to_mtd(nand_chip));
Ian Moltonec43b812008-07-15 16:04:22 +0100189 long timeout;
Boris Brezillon97d90da2017-11-30 18:01:29 +0100190 u8 status;
Ian Moltonec43b812008-07-15 16:04:22 +0100191
192 /* enable RDYREQ interrupt */
Boris Brezillona0916c92018-11-20 11:57:17 +0100193
Ian Moltonec43b812008-07-15 16:04:22 +0100194 tmio_iowrite8(0x0f, tmio->fcr + FCR_ISR);
Boris Brezillona0916c92018-11-20 11:57:17 +0100195 reinit_completion(&tmio->comp);
Ian Moltonec43b812008-07-15 16:04:22 +0100196 tmio_iowrite8(0x81, tmio->fcr + FCR_IMR);
197
Boris Brezillon661803b2018-11-20 11:57:19 +0100198 timeout = 400;
Boris Brezillona0916c92018-11-20 11:57:17 +0100199 timeout = wait_for_completion_timeout(&tmio->comp,
200 msecs_to_jiffies(timeout));
Ian Moltonec43b812008-07-15 16:04:22 +0100201
Boris Brezillon50a487e2018-09-06 14:05:27 +0200202 if (unlikely(!tmio_nand_dev_ready(nand_chip))) {
Ian Moltonec43b812008-07-15 16:04:22 +0100203 tmio_iowrite8(0x00, tmio->fcr + FCR_IMR);
Boris Brezillon661803b2018-11-20 11:57:19 +0100204 dev_warn(&tmio->dev->dev, "still busy after 400 ms\n");
Ian Moltonec43b812008-07-15 16:04:22 +0100205
206 } else if (unlikely(!timeout)) {
207 tmio_iowrite8(0x00, tmio->fcr + FCR_IMR);
208 dev_warn(&tmio->dev->dev, "timeout waiting for interrupt\n");
209 }
210
Boris Brezillon97d90da2017-11-30 18:01:29 +0100211 nand_status_op(nand_chip, &status);
212 return status;
Ian Moltonec43b812008-07-15 16:04:22 +0100213}
214
215/*
216 *The TMIO controller combines two 8-bit data bytes into one 16-bit
217 *word. This function separates them so nand_base.c works as expected,
218 *especially its NAND_CMD_READID routines.
219 *
220 *To prevent stale data from being read, tmio_nand_hwcontrol() clears
221 *tmio->read_good.
222 */
Boris Brezillon7e534322018-09-06 14:05:22 +0200223static u_char tmio_nand_read_byte(struct nand_chip *chip)
Ian Moltonec43b812008-07-15 16:04:22 +0100224{
Boris Brezillon7e534322018-09-06 14:05:22 +0200225 struct tmio_nand *tmio = mtd_to_tmio(nand_to_mtd(chip));
Ian Moltonec43b812008-07-15 16:04:22 +0100226 unsigned int data;
227
228 if (tmio->read_good--)
229 return tmio->read;
230
231 data = tmio_ioread16(tmio->fcr + FCR_DATA);
232 tmio->read = data >> 8;
233 return data;
234}
235
236/*
237 *The TMIO controller converts an 8-bit NAND interface to a 16-bit
238 *bus interface, so all data reads and writes must be 16-bit wide.
239 *Thus, we implement 16-bit versions of the read, write, and verify
240 *buffer functions.
241 */
242static void
Boris Brezillonc0739d82018-09-06 14:05:23 +0200243tmio_nand_write_buf(struct nand_chip *chip, const u_char *buf, int len)
Ian Moltonec43b812008-07-15 16:04:22 +0100244{
Boris Brezillonc0739d82018-09-06 14:05:23 +0200245 struct tmio_nand *tmio = mtd_to_tmio(nand_to_mtd(chip));
Ian Moltonec43b812008-07-15 16:04:22 +0100246
247 tmio_iowrite16_rep(tmio->fcr + FCR_DATA, buf, len >> 1);
248}
249
Boris Brezillon7e534322018-09-06 14:05:22 +0200250static void tmio_nand_read_buf(struct nand_chip *chip, u_char *buf, int len)
Ian Moltonec43b812008-07-15 16:04:22 +0100251{
Boris Brezillon7e534322018-09-06 14:05:22 +0200252 struct tmio_nand *tmio = mtd_to_tmio(nand_to_mtd(chip));
Ian Moltonec43b812008-07-15 16:04:22 +0100253
254 tmio_ioread16_rep(tmio->fcr + FCR_DATA, buf, len >> 1);
255}
256
Boris Brezillonec476362018-09-06 14:05:17 +0200257static void tmio_nand_enable_hwecc(struct nand_chip *chip, int mode)
Ian Moltonec43b812008-07-15 16:04:22 +0100258{
Boris Brezillonec476362018-09-06 14:05:17 +0200259 struct tmio_nand *tmio = mtd_to_tmio(nand_to_mtd(chip));
Ian Moltonec43b812008-07-15 16:04:22 +0100260
261 tmio_iowrite8(FCR_MODE_HWECC_RESET, tmio->fcr + FCR_MODE);
262 tmio_ioread8(tmio->fcr + FCR_DATA); /* dummy read */
263 tmio_iowrite8(FCR_MODE_HWECC_CALC, tmio->fcr + FCR_MODE);
264}
265
Boris Brezillonaf37d2c2018-09-06 14:05:18 +0200266static int tmio_nand_calculate_ecc(struct nand_chip *chip, const u_char *dat,
267 u_char *ecc_code)
Ian Moltonec43b812008-07-15 16:04:22 +0100268{
Boris Brezillonaf37d2c2018-09-06 14:05:18 +0200269 struct tmio_nand *tmio = mtd_to_tmio(nand_to_mtd(chip));
Ian Moltonec43b812008-07-15 16:04:22 +0100270 unsigned int ecc;
271
272 tmio_iowrite8(FCR_MODE_HWECC_RESULT, tmio->fcr + FCR_MODE);
273
274 ecc = tmio_ioread16(tmio->fcr + FCR_DATA);
275 ecc_code[1] = ecc; /* 000-255 LP7-0 */
276 ecc_code[0] = ecc >> 8; /* 000-255 LP15-8 */
277 ecc = tmio_ioread16(tmio->fcr + FCR_DATA);
278 ecc_code[2] = ecc; /* 000-255 CP5-0,11b */
279 ecc_code[4] = ecc >> 8; /* 256-511 LP7-0 */
280 ecc = tmio_ioread16(tmio->fcr + FCR_DATA);
281 ecc_code[3] = ecc; /* 256-511 LP15-8 */
282 ecc_code[5] = ecc >> 8; /* 256-511 CP5-0,11b */
283
284 tmio_iowrite8(FCR_MODE_DATA, tmio->fcr + FCR_MODE);
285 return 0;
286}
287
Boris Brezillon00da2ea2018-09-06 14:05:19 +0200288static int tmio_nand_correct_data(struct nand_chip *chip, unsigned char *buf,
289 unsigned char *read_ecc,
290 unsigned char *calc_ecc)
Atsushi Nemoto0f777fb2009-09-05 01:20:44 +0900291{
292 int r0, r1;
293
294 /* assume ecc.size = 512 and ecc.bytes = 6 */
Boris Brezillon309600c2018-09-04 16:23:28 +0200295 r0 = __nand_correct_data(buf, read_ecc, calc_ecc, 256, false);
Atsushi Nemoto0f777fb2009-09-05 01:20:44 +0900296 if (r0 < 0)
297 return r0;
Boris Brezillon309600c2018-09-04 16:23:28 +0200298 r1 = __nand_correct_data(buf + 256, read_ecc + 3, calc_ecc + 3, 256,
299 false);
Atsushi Nemoto0f777fb2009-09-05 01:20:44 +0900300 if (r1 < 0)
301 return r1;
302 return r0 + r1;
303}
304
Ian Moltonec43b812008-07-15 16:04:22 +0100305static int tmio_hw_init(struct platform_device *dev, struct tmio_nand *tmio)
306{
Andres Salomon944dc032011-03-01 12:32:20 -0800307 const struct mfd_cell *cell = mfd_get_cell(dev);
Ian Moltonec43b812008-07-15 16:04:22 +0100308 int ret;
309
310 if (cell->enable) {
311 ret = cell->enable(dev);
312 if (ret)
313 return ret;
314 }
315
316 /* (4Ch) CLKRUN Enable 1st spcrunc */
317 tmio_iowrite8(0x81, tmio->ccr + CCR_ICC);
318
319 /* (10h)BaseAddress 0x1000 spba.spba2 */
Dmitry Baryshkov076c7f42008-09-04 13:28:33 +0400320 tmio_iowrite16(tmio->fcr_base, tmio->ccr + CCR_BASE);
321 tmio_iowrite16(tmio->fcr_base >> 16, tmio->ccr + CCR_BASE + 2);
Ian Moltonec43b812008-07-15 16:04:22 +0100322
323 /* (04h)Command Register I/O spcmd */
324 tmio_iowrite8(0x02, tmio->ccr + CCR_COMMAND);
325
326 /* (62h) Power Supply Control ssmpwc */
327 /* HardPowerOFF - SuspendOFF - PowerSupplyWait_4MS */
328 tmio_iowrite8(0x02, tmio->ccr + CCR_NFPSC);
329
330 /* (63h) Detect Control ssmdtc */
331 tmio_iowrite8(0x02, tmio->ccr + CCR_NFDC);
332
333 /* Interrupt status register clear sintst */
334 tmio_iowrite8(0x0f, tmio->fcr + FCR_ISR);
335
336 /* After power supply, Media are reset smode */
337 tmio_iowrite8(FCR_MODE_POWER_ON, tmio->fcr + FCR_MODE);
338 tmio_iowrite8(FCR_MODE_COMMAND, tmio->fcr + FCR_MODE);
339 tmio_iowrite8(NAND_CMD_RESET, tmio->fcr + FCR_DATA);
340
341 /* Standby Mode smode */
342 tmio_iowrite8(FCR_MODE_STANDBY, tmio->fcr + FCR_MODE);
343
344 mdelay(5);
345
346 return 0;
347}
348
349static void tmio_hw_stop(struct platform_device *dev, struct tmio_nand *tmio)
350{
Andres Salomon944dc032011-03-01 12:32:20 -0800351 const struct mfd_cell *cell = mfd_get_cell(dev);
Ian Moltonec43b812008-07-15 16:04:22 +0100352
353 tmio_iowrite8(FCR_MODE_POWER_OFF, tmio->fcr + FCR_MODE);
354 if (cell->disable)
355 cell->disable(dev);
356}
357
358static int tmio_probe(struct platform_device *dev)
359{
Jingoo Han453810b2013-07-30 17:18:33 +0900360 struct tmio_nand_data *data = dev_get_platdata(&dev->dev);
Ian Moltonec43b812008-07-15 16:04:22 +0100361 struct resource *fcr = platform_get_resource(dev,
362 IORESOURCE_MEM, 0);
363 struct resource *ccr = platform_get_resource(dev,
364 IORESOURCE_MEM, 1);
365 int irq = platform_get_irq(dev, 0);
366 struct tmio_nand *tmio;
367 struct mtd_info *mtd;
368 struct nand_chip *nand_chip;
Ian Moltonec43b812008-07-15 16:04:22 +0100369 int retval;
370
371 if (data == NULL)
372 dev_warn(&dev->dev, "NULL platform data!\n");
373
Jingoo Han8f91fb62013-12-26 10:45:55 +0900374 tmio = devm_kzalloc(&dev->dev, sizeof(*tmio), GFP_KERNEL);
375 if (!tmio)
376 return -ENOMEM;
Ian Moltonec43b812008-07-15 16:04:22 +0100377
Boris Brezillona0916c92018-11-20 11:57:17 +0100378 init_completion(&tmio->comp);
379
Ian Moltonec43b812008-07-15 16:04:22 +0100380 tmio->dev = dev;
381
382 platform_set_drvdata(dev, tmio);
Ian Moltonec43b812008-07-15 16:04:22 +0100383 nand_chip = &tmio->chip;
Boris BREZILLON66c95952015-12-10 09:00:27 +0100384 mtd = nand_to_mtd(nand_chip);
Ian Moltonec43b812008-07-15 16:04:22 +0100385 mtd->name = "tmio-nand";
Frans Klaver7b679052015-06-10 22:39:08 +0200386 mtd->dev.parent = &dev->dev;
Ian Moltonec43b812008-07-15 16:04:22 +0100387
Jingoo Han8f91fb62013-12-26 10:45:55 +0900388 tmio->ccr = devm_ioremap(&dev->dev, ccr->start, resource_size(ccr));
389 if (!tmio->ccr)
390 return -EIO;
Ian Moltonec43b812008-07-15 16:04:22 +0100391
Dmitry Baryshkov076c7f42008-09-04 13:28:33 +0400392 tmio->fcr_base = fcr->start & 0xfffff;
Jingoo Han8f91fb62013-12-26 10:45:55 +0900393 tmio->fcr = devm_ioremap(&dev->dev, fcr->start, resource_size(fcr));
394 if (!tmio->fcr)
395 return -EIO;
Ian Moltonec43b812008-07-15 16:04:22 +0100396
397 retval = tmio_hw_init(dev, tmio);
398 if (retval)
Jingoo Han8f91fb62013-12-26 10:45:55 +0900399 return retval;
Ian Moltonec43b812008-07-15 16:04:22 +0100400
401 /* Set address of NAND IO lines */
Boris Brezillon82fc5092018-09-07 00:38:34 +0200402 nand_chip->legacy.IO_ADDR_R = tmio->fcr;
403 nand_chip->legacy.IO_ADDR_W = tmio->fcr;
Ian Moltonec43b812008-07-15 16:04:22 +0100404
405 /* Set address of hardware control function */
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200406 nand_chip->legacy.cmd_ctrl = tmio_nand_hwcontrol;
Boris Brezillon8395b752018-09-07 00:38:37 +0200407 nand_chip->legacy.dev_ready = tmio_nand_dev_ready;
Boris Brezillon716bbba2018-09-07 00:38:35 +0200408 nand_chip->legacy.read_byte = tmio_nand_read_byte;
409 nand_chip->legacy.write_buf = tmio_nand_write_buf;
410 nand_chip->legacy.read_buf = tmio_nand_read_buf;
Ian Moltonec43b812008-07-15 16:04:22 +0100411
412 /* set eccmode using hardware ECC */
Miquel Raynalbace41f2020-08-27 10:51:58 +0200413 nand_chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
Ian Moltonec43b812008-07-15 16:04:22 +0100414 nand_chip->ecc.size = 512;
415 nand_chip->ecc.bytes = 6;
Mike Dunn6a918ba2012-03-11 14:21:11 -0700416 nand_chip->ecc.strength = 2;
Ian Moltonec43b812008-07-15 16:04:22 +0100417 nand_chip->ecc.hwctl = tmio_nand_enable_hwecc;
418 nand_chip->ecc.calculate = tmio_nand_calculate_ecc;
Atsushi Nemoto0f777fb2009-09-05 01:20:44 +0900419 nand_chip->ecc.correct = tmio_nand_correct_data;
Ian Moltonec43b812008-07-15 16:04:22 +0100420
421 if (data)
422 nand_chip->badblock_pattern = data->badblock_pattern;
423
424 /* 15 us command delay time */
Boris Brezillon3cece3a2018-09-07 00:38:41 +0200425 nand_chip->legacy.chip_delay = 15;
Ian Moltonec43b812008-07-15 16:04:22 +0100426
Jingoo Han8f91fb62013-12-26 10:45:55 +0900427 retval = devm_request_irq(&dev->dev, irq, &tmio_irq, 0,
428 dev_name(&dev->dev), tmio);
Ian Moltonec43b812008-07-15 16:04:22 +0100429 if (retval) {
430 dev_err(&dev->dev, "request_irq error %d\n", retval);
431 goto err_irq;
432 }
433
434 tmio->irq = irq;
Boris Brezillon8395b752018-09-07 00:38:37 +0200435 nand_chip->legacy.waitfunc = tmio_nand_wait;
Ian Moltonec43b812008-07-15 16:04:22 +0100436
437 /* Scan to find existence of the device */
Boris Brezillon00ad3782018-09-06 14:05:14 +0200438 retval = nand_scan(nand_chip, 1);
Masahiro Yamada43358c12016-11-04 19:42:54 +0900439 if (retval)
Jingoo Han8f91fb62013-12-26 10:45:55 +0900440 goto err_irq;
Masahiro Yamada43358c12016-11-04 19:42:54 +0900441
Ian Moltonec43b812008-07-15 16:04:22 +0100442 /* Register the partitions */
Andrea Adami311bba12017-08-14 22:48:36 +0200443 retval = mtd_device_parse_register(mtd,
444 data ? data->part_parsers : NULL,
445 NULL,
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +0200446 data ? data->partition : NULL,
447 data ? data->num_partitions : 0);
Ian Moltonec43b812008-07-15 16:04:22 +0100448 if (!retval)
449 return retval;
450
Miquel Raynal75e9a332020-05-19 15:00:29 +0200451 nand_cleanup(nand_chip);
Ian Moltonec43b812008-07-15 16:04:22 +0100452
Ian Moltonec43b812008-07-15 16:04:22 +0100453err_irq:
454 tmio_hw_stop(dev, tmio);
Ian Moltonec43b812008-07-15 16:04:22 +0100455 return retval;
456}
457
458static int tmio_remove(struct platform_device *dev)
459{
460 struct tmio_nand *tmio = platform_get_drvdata(dev);
Miquel Raynalf3e169f2020-05-19 15:00:30 +0200461 struct nand_chip *chip = &tmio->chip;
462 int ret;
Ian Moltonec43b812008-07-15 16:04:22 +0100463
Miquel Raynalf3e169f2020-05-19 15:00:30 +0200464 ret = mtd_device_unregister(nand_to_mtd(chip));
465 WARN_ON(ret);
466 nand_cleanup(chip);
Ian Moltonec43b812008-07-15 16:04:22 +0100467 tmio_hw_stop(dev, tmio);
Ian Moltonec43b812008-07-15 16:04:22 +0100468 return 0;
469}
470
471#ifdef CONFIG_PM
472static int tmio_suspend(struct platform_device *dev, pm_message_t state)
473{
Andres Salomon944dc032011-03-01 12:32:20 -0800474 const struct mfd_cell *cell = mfd_get_cell(dev);
Ian Moltonec43b812008-07-15 16:04:22 +0100475
476 if (cell->suspend)
477 cell->suspend(dev);
478
479 tmio_hw_stop(dev, platform_get_drvdata(dev));
480 return 0;
481}
482
483static int tmio_resume(struct platform_device *dev)
484{
Andres Salomon944dc032011-03-01 12:32:20 -0800485 const struct mfd_cell *cell = mfd_get_cell(dev);
Ian Moltonec43b812008-07-15 16:04:22 +0100486
487 /* FIXME - is this required or merely another attack of the broken
488 * SHARP platform? Looks suspicious.
489 */
490 tmio_hw_init(dev, platform_get_drvdata(dev));
491
492 if (cell->resume)
493 cell->resume(dev);
494
495 return 0;
496}
497#else
498#define tmio_suspend NULL
499#define tmio_resume NULL
500#endif
501
502static struct platform_driver tmio_driver = {
503 .driver.name = "tmio-nand",
504 .driver.owner = THIS_MODULE,
505 .probe = tmio_probe,
506 .remove = tmio_remove,
507 .suspend = tmio_suspend,
508 .resume = tmio_resume,
509};
510
Axel Linf99640d2011-11-27 20:45:03 +0800511module_platform_driver(tmio_driver);
Ian Moltonec43b812008-07-15 16:04:22 +0100512
513MODULE_LICENSE("GPL v2");
514MODULE_AUTHOR("Ian Molton, Dirk Opfer, Chris Humbert, Dmitry Baryshkov");
515MODULE_DESCRIPTION("NAND flash driver on Toshiba Mobile IO controller");
516MODULE_ALIAS("platform:tmio-nand");