mtd: rawnand: Pass a nand_chip object to ecc->write_xxx() hooks
[linux-2.6-block.git] / drivers / mtd / nand / raw / tmio_nand.c
CommitLineData
ec43b816
IM
1/*
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
25985edc 7 * Original work, independent of sharps code. Included hardware ECC support.
ec43b816
IM
8 * 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>
d4092d76 37#include <linux/mtd/rawnand.h>
ec43b816
IM
38#include <linux/mtd/nand_ecc.h>
39#include <linux/mtd/partitions.h>
5a0e3ad6 40#include <linux/slab.h>
ec43b816
IM
41
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 {
ec43b816
IM
106 struct nand_chip chip;
107
108 struct platform_device *dev;
109
110 void __iomem *ccr;
111 void __iomem *fcr;
076c7f4c 112 unsigned long fcr_base;
ec43b816
IM
113
114 unsigned int irq;
115
116 /* for tmio_nand_read_byte */
117 u8 read;
118 unsigned read_good:1;
119};
120
66c9595d
BB
121static inline struct tmio_nand *mtd_to_tmio(struct mtd_info *mtd)
122{
123 return container_of(mtd_to_nand(mtd), struct tmio_nand, chip);
124}
ec43b816 125
ec43b816
IM
126
127/*--------------------------------------------------------------------------*/
128
129static void tmio_nand_hwcontrol(struct mtd_info *mtd, int cmd,
130 unsigned int ctrl)
131{
132 struct tmio_nand *tmio = mtd_to_tmio(mtd);
4bd4ebcc 133 struct nand_chip *chip = mtd_to_nand(mtd);
ec43b816
IM
134
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)
159 tmio_iowrite8(cmd, chip->IO_ADDR_W);
160}
161
162static int tmio_nand_dev_ready(struct mtd_info *mtd)
163{
164 struct tmio_nand *tmio = mtd_to_tmio(mtd);
165
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;
172 struct nand_chip *nand_chip = &tmio->chip;
173
174 /* disable RDYREQ interrupt */
175 tmio_iowrite8(0x00, tmio->fcr + FCR_IMR);
176
177 if (unlikely(!waitqueue_active(&nand_chip->controller->wq)))
178 dev_warn(&tmio->dev->dev, "spurious interrupt\n");
179
180 wake_up(&nand_chip->controller->wq);
181 return IRQ_HANDLED;
182}
183
184/*
185 *The TMIO core has a RDYREQ interrupt on the posedge of #SMRB.
186 *This interrupt is normally disabled, but for long operations like
187 *erase and write, we enable it to wake us up. The irq handler
188 *disables the interrupt.
189 */
190static int
191tmio_nand_wait(struct mtd_info *mtd, struct nand_chip *nand_chip)
192{
193 struct tmio_nand *tmio = mtd_to_tmio(mtd);
194 long timeout;
97d90da8 195 u8 status;
ec43b816
IM
196
197 /* enable RDYREQ interrupt */
198 tmio_iowrite8(0x0f, tmio->fcr + FCR_ISR);
199 tmio_iowrite8(0x81, tmio->fcr + FCR_IMR);
200
201 timeout = wait_event_timeout(nand_chip->controller->wq,
202 tmio_nand_dev_ready(mtd),
203 msecs_to_jiffies(nand_chip->state == FL_ERASING ? 400 : 20));
204
205 if (unlikely(!tmio_nand_dev_ready(mtd))) {
206 tmio_iowrite8(0x00, tmio->fcr + FCR_IMR);
207 dev_warn(&tmio->dev->dev, "still busy with %s after %d ms\n",
208 nand_chip->state == FL_ERASING ? "erase" : "program",
209 nand_chip->state == FL_ERASING ? 400 : 20);
210
211 } else if (unlikely(!timeout)) {
212 tmio_iowrite8(0x00, tmio->fcr + FCR_IMR);
213 dev_warn(&tmio->dev->dev, "timeout waiting for interrupt\n");
214 }
215
97d90da8
BB
216 nand_status_op(nand_chip, &status);
217 return status;
ec43b816
IM
218}
219
220/*
221 *The TMIO controller combines two 8-bit data bytes into one 16-bit
222 *word. This function separates them so nand_base.c works as expected,
223 *especially its NAND_CMD_READID routines.
224 *
225 *To prevent stale data from being read, tmio_nand_hwcontrol() clears
226 *tmio->read_good.
227 */
228static u_char tmio_nand_read_byte(struct mtd_info *mtd)
229{
230 struct tmio_nand *tmio = mtd_to_tmio(mtd);
231 unsigned int data;
232
233 if (tmio->read_good--)
234 return tmio->read;
235
236 data = tmio_ioread16(tmio->fcr + FCR_DATA);
237 tmio->read = data >> 8;
238 return data;
239}
240
241/*
242 *The TMIO controller converts an 8-bit NAND interface to a 16-bit
243 *bus interface, so all data reads and writes must be 16-bit wide.
244 *Thus, we implement 16-bit versions of the read, write, and verify
245 *buffer functions.
246 */
247static void
248tmio_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
249{
250 struct tmio_nand *tmio = mtd_to_tmio(mtd);
251
252 tmio_iowrite16_rep(tmio->fcr + FCR_DATA, buf, len >> 1);
253}
254
255static void tmio_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
256{
257 struct tmio_nand *tmio = mtd_to_tmio(mtd);
258
259 tmio_ioread16_rep(tmio->fcr + FCR_DATA, buf, len >> 1);
260}
261
ec47636c 262static void tmio_nand_enable_hwecc(struct nand_chip *chip, int mode)
ec43b816 263{
ec47636c 264 struct tmio_nand *tmio = mtd_to_tmio(nand_to_mtd(chip));
ec43b816
IM
265
266 tmio_iowrite8(FCR_MODE_HWECC_RESET, tmio->fcr + FCR_MODE);
267 tmio_ioread8(tmio->fcr + FCR_DATA); /* dummy read */
268 tmio_iowrite8(FCR_MODE_HWECC_CALC, tmio->fcr + FCR_MODE);
269}
270
af37d2c3
BB
271static int tmio_nand_calculate_ecc(struct nand_chip *chip, const u_char *dat,
272 u_char *ecc_code)
ec43b816 273{
af37d2c3 274 struct tmio_nand *tmio = mtd_to_tmio(nand_to_mtd(chip));
ec43b816
IM
275 unsigned int ecc;
276
277 tmio_iowrite8(FCR_MODE_HWECC_RESULT, tmio->fcr + FCR_MODE);
278
279 ecc = tmio_ioread16(tmio->fcr + FCR_DATA);
280 ecc_code[1] = ecc; /* 000-255 LP7-0 */
281 ecc_code[0] = ecc >> 8; /* 000-255 LP15-8 */
282 ecc = tmio_ioread16(tmio->fcr + FCR_DATA);
283 ecc_code[2] = ecc; /* 000-255 CP5-0,11b */
284 ecc_code[4] = ecc >> 8; /* 256-511 LP7-0 */
285 ecc = tmio_ioread16(tmio->fcr + FCR_DATA);
286 ecc_code[3] = ecc; /* 256-511 LP15-8 */
287 ecc_code[5] = ecc >> 8; /* 256-511 CP5-0,11b */
288
289 tmio_iowrite8(FCR_MODE_DATA, tmio->fcr + FCR_MODE);
290 return 0;
291}
292
00da2ea9
BB
293static int tmio_nand_correct_data(struct nand_chip *chip, unsigned char *buf,
294 unsigned char *read_ecc,
295 unsigned char *calc_ecc)
0f777fb9
AN
296{
297 int r0, r1;
298
299 /* assume ecc.size = 512 and ecc.bytes = 6 */
300 r0 = __nand_correct_data(buf, read_ecc, calc_ecc, 256);
301 if (r0 < 0)
302 return r0;
303 r1 = __nand_correct_data(buf + 256, read_ecc + 3, calc_ecc + 3, 256);
304 if (r1 < 0)
305 return r1;
306 return r0 + r1;
307}
308
ec43b816
IM
309static int tmio_hw_init(struct platform_device *dev, struct tmio_nand *tmio)
310{
944dc035 311 const struct mfd_cell *cell = mfd_get_cell(dev);
ec43b816
IM
312 int ret;
313
314 if (cell->enable) {
315 ret = cell->enable(dev);
316 if (ret)
317 return ret;
318 }
319
320 /* (4Ch) CLKRUN Enable 1st spcrunc */
321 tmio_iowrite8(0x81, tmio->ccr + CCR_ICC);
322
323 /* (10h)BaseAddress 0x1000 spba.spba2 */
076c7f4c
DB
324 tmio_iowrite16(tmio->fcr_base, tmio->ccr + CCR_BASE);
325 tmio_iowrite16(tmio->fcr_base >> 16, tmio->ccr + CCR_BASE + 2);
ec43b816
IM
326
327 /* (04h)Command Register I/O spcmd */
328 tmio_iowrite8(0x02, tmio->ccr + CCR_COMMAND);
329
330 /* (62h) Power Supply Control ssmpwc */
331 /* HardPowerOFF - SuspendOFF - PowerSupplyWait_4MS */
332 tmio_iowrite8(0x02, tmio->ccr + CCR_NFPSC);
333
334 /* (63h) Detect Control ssmdtc */
335 tmio_iowrite8(0x02, tmio->ccr + CCR_NFDC);
336
337 /* Interrupt status register clear sintst */
338 tmio_iowrite8(0x0f, tmio->fcr + FCR_ISR);
339
340 /* After power supply, Media are reset smode */
341 tmio_iowrite8(FCR_MODE_POWER_ON, tmio->fcr + FCR_MODE);
342 tmio_iowrite8(FCR_MODE_COMMAND, tmio->fcr + FCR_MODE);
343 tmio_iowrite8(NAND_CMD_RESET, tmio->fcr + FCR_DATA);
344
345 /* Standby Mode smode */
346 tmio_iowrite8(FCR_MODE_STANDBY, tmio->fcr + FCR_MODE);
347
348 mdelay(5);
349
350 return 0;
351}
352
353static void tmio_hw_stop(struct platform_device *dev, struct tmio_nand *tmio)
354{
944dc035 355 const struct mfd_cell *cell = mfd_get_cell(dev);
ec43b816
IM
356
357 tmio_iowrite8(FCR_MODE_POWER_OFF, tmio->fcr + FCR_MODE);
358 if (cell->disable)
359 cell->disable(dev);
360}
361
362static int tmio_probe(struct platform_device *dev)
363{
453810b7 364 struct tmio_nand_data *data = dev_get_platdata(&dev->dev);
ec43b816
IM
365 struct resource *fcr = platform_get_resource(dev,
366 IORESOURCE_MEM, 0);
367 struct resource *ccr = platform_get_resource(dev,
368 IORESOURCE_MEM, 1);
369 int irq = platform_get_irq(dev, 0);
370 struct tmio_nand *tmio;
371 struct mtd_info *mtd;
372 struct nand_chip *nand_chip;
ec43b816
IM
373 int retval;
374
375 if (data == NULL)
376 dev_warn(&dev->dev, "NULL platform data!\n");
377
8f91fb68
JH
378 tmio = devm_kzalloc(&dev->dev, sizeof(*tmio), GFP_KERNEL);
379 if (!tmio)
380 return -ENOMEM;
ec43b816
IM
381
382 tmio->dev = dev;
383
384 platform_set_drvdata(dev, tmio);
ec43b816 385 nand_chip = &tmio->chip;
66c9595d 386 mtd = nand_to_mtd(nand_chip);
ec43b816 387 mtd->name = "tmio-nand";
7b679053 388 mtd->dev.parent = &dev->dev;
ec43b816 389
8f91fb68
JH
390 tmio->ccr = devm_ioremap(&dev->dev, ccr->start, resource_size(ccr));
391 if (!tmio->ccr)
392 return -EIO;
ec43b816 393
076c7f4c 394 tmio->fcr_base = fcr->start & 0xfffff;
8f91fb68
JH
395 tmio->fcr = devm_ioremap(&dev->dev, fcr->start, resource_size(fcr));
396 if (!tmio->fcr)
397 return -EIO;
ec43b816
IM
398
399 retval = tmio_hw_init(dev, tmio);
400 if (retval)
8f91fb68 401 return retval;
ec43b816
IM
402
403 /* Set address of NAND IO lines */
404 nand_chip->IO_ADDR_R = tmio->fcr;
405 nand_chip->IO_ADDR_W = tmio->fcr;
406
407 /* Set address of hardware control function */
408 nand_chip->cmd_ctrl = tmio_nand_hwcontrol;
409 nand_chip->dev_ready = tmio_nand_dev_ready;
410 nand_chip->read_byte = tmio_nand_read_byte;
411 nand_chip->write_buf = tmio_nand_write_buf;
412 nand_chip->read_buf = tmio_nand_read_buf;
ec43b816
IM
413
414 /* set eccmode using hardware ECC */
415 nand_chip->ecc.mode = NAND_ECC_HW;
416 nand_chip->ecc.size = 512;
417 nand_chip->ecc.bytes = 6;
6a918bad 418 nand_chip->ecc.strength = 2;
ec43b816
IM
419 nand_chip->ecc.hwctl = tmio_nand_enable_hwecc;
420 nand_chip->ecc.calculate = tmio_nand_calculate_ecc;
0f777fb9 421 nand_chip->ecc.correct = tmio_nand_correct_data;
ec43b816
IM
422
423 if (data)
424 nand_chip->badblock_pattern = data->badblock_pattern;
425
426 /* 15 us command delay time */
427 nand_chip->chip_delay = 15;
428
8f91fb68
JH
429 retval = devm_request_irq(&dev->dev, irq, &tmio_irq, 0,
430 dev_name(&dev->dev), tmio);
ec43b816
IM
431 if (retval) {
432 dev_err(&dev->dev, "request_irq error %d\n", retval);
433 goto err_irq;
434 }
435
436 tmio->irq = irq;
437 nand_chip->waitfunc = tmio_nand_wait;
438
439 /* Scan to find existence of the device */
00ad378f 440 retval = nand_scan(nand_chip, 1);
43358c17 441 if (retval)
8f91fb68 442 goto err_irq;
43358c17 443
ec43b816 444 /* Register the partitions */
311bba10
AA
445 retval = mtd_device_parse_register(mtd,
446 data ? data->part_parsers : NULL,
447 NULL,
42d7fbe2
AB
448 data ? data->partition : NULL,
449 data ? data->num_partitions : 0);
ec43b816
IM
450 if (!retval)
451 return retval;
452
59ac276f 453 nand_release(nand_chip);
ec43b816 454
ec43b816
IM
455err_irq:
456 tmio_hw_stop(dev, tmio);
ec43b816
IM
457 return retval;
458}
459
460static int tmio_remove(struct platform_device *dev)
461{
462 struct tmio_nand *tmio = platform_get_drvdata(dev);
463
59ac276f 464 nand_release(&tmio->chip);
ec43b816 465 tmio_hw_stop(dev, tmio);
ec43b816
IM
466 return 0;
467}
468
469#ifdef CONFIG_PM
470static int tmio_suspend(struct platform_device *dev, pm_message_t state)
471{
944dc035 472 const struct mfd_cell *cell = mfd_get_cell(dev);
ec43b816
IM
473
474 if (cell->suspend)
475 cell->suspend(dev);
476
477 tmio_hw_stop(dev, platform_get_drvdata(dev));
478 return 0;
479}
480
481static int tmio_resume(struct platform_device *dev)
482{
944dc035 483 const struct mfd_cell *cell = mfd_get_cell(dev);
ec43b816
IM
484
485 /* FIXME - is this required or merely another attack of the broken
486 * SHARP platform? Looks suspicious.
487 */
488 tmio_hw_init(dev, platform_get_drvdata(dev));
489
490 if (cell->resume)
491 cell->resume(dev);
492
493 return 0;
494}
495#else
496#define tmio_suspend NULL
497#define tmio_resume NULL
498#endif
499
500static struct platform_driver tmio_driver = {
501 .driver.name = "tmio-nand",
502 .driver.owner = THIS_MODULE,
503 .probe = tmio_probe,
504 .remove = tmio_remove,
505 .suspend = tmio_suspend,
506 .resume = tmio_resume,
507};
508
f99640de 509module_platform_driver(tmio_driver);
ec43b816
IM
510
511MODULE_LICENSE("GPL v2");
512MODULE_AUTHOR("Ian Molton, Dirk Opfer, Chris Humbert, Dmitry Baryshkov");
513MODULE_DESCRIPTION("NAND flash driver on Toshiba Mobile IO controller");
514MODULE_ALIAS("platform:tmio-nand");