blob: 2dcaeeae206832c5f28cbc8c31e400a348be2255 [file] [log] [blame]
Andrew Victor42cb1402006-10-19 18:24:35 +02001/*
2 * drivers/mtd/nand/at91_nand.c
3 *
4 * Copyright (C) 2003 Rick Bronson
5 *
6 * Derived from drivers/mtd/nand/autcpu12.c
7 * Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de)
8 *
9 * Derived from drivers/mtd/spia.c
10 * Copyright (C) 2000 Steven J. Hill (sjhill@cotw.com)
11 *
Richard Genoud77f54922008-04-23 19:51:14 +020012 *
13 * Add Hardware ECC support for AT91SAM9260 / AT91SAM9263
14 * Richard Genoud (richard.genoud@gmail.com), Adeneo Copyright (C) 2007
15 *
16 * Derived from Das U-Boot source code
17 * (u-boot-1.1.5/board/atmel/at91sam9263ek/nand.c)
18 * (C) Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
19 *
20 *
Andrew Victor42cb1402006-10-19 18:24:35 +020021 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License version 2 as
23 * published by the Free Software Foundation.
24 *
25 */
26
27#include <linux/slab.h>
28#include <linux/module.h>
29#include <linux/platform_device.h>
30#include <linux/mtd/mtd.h>
31#include <linux/mtd/nand.h>
32#include <linux/mtd/partitions.h>
33
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +020034#include <asm/gpio.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020035#include <asm/io.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020036
Andrew Victor42cb1402006-10-19 18:24:35 +020037#include <asm/arch/board.h>
Andrew Victor42cb1402006-10-19 18:24:35 +020038
Richard Genoud77f54922008-04-23 19:51:14 +020039#ifdef CONFIG_MTD_NAND_AT91_ECC_HW
40#define hard_ecc 1
41#else
42#define hard_ecc 0
43#endif
44
45#ifdef CONFIG_MTD_NAND_AT91_ECC_NONE
46#define no_ecc 1
47#else
48#define no_ecc 0
49#endif
50
51/* Register access macros */
52#define ecc_readl(add, reg) \
53 __raw_readl(add + AT91_ECC_##reg)
54#define ecc_writel(add, reg, value) \
55 __raw_writel((value), add + AT91_ECC_##reg)
56
57#include <asm/arch/at91_ecc.h> /* AT91SAM9260/3 ECC registers */
58
59/* oob layout for large page size
60 * bad block info is on bytes 0 and 1
61 * the bytes have to be consecutives to avoid
62 * several NAND_CMD_RNDOUT during read
63 */
64static struct nand_ecclayout at91_oobinfo_large = {
65 .eccbytes = 4,
66 .eccpos = {60, 61, 62, 63},
67 .oobfree = {
68 {2, 58}
69 },
70};
71
72/* oob layout for small page size
73 * bad block info is on bytes 4 and 5
74 * the bytes have to be consecutives to avoid
75 * several NAND_CMD_RNDOUT during read
76 */
77static struct nand_ecclayout at91_oobinfo_small = {
78 .eccbytes = 4,
79 .eccpos = {0, 1, 2, 3},
80 .oobfree = {
81 {6, 10}
82 },
83};
84
Andrew Victor42cb1402006-10-19 18:24:35 +020085struct at91_nand_host {
86 struct nand_chip nand_chip;
87 struct mtd_info mtd;
88 void __iomem *io_base;
89 struct at91_nand_data *board;
Richard Genoud77f54922008-04-23 19:51:14 +020090 struct device *dev;
91 void __iomem *ecc;
Andrew Victor42cb1402006-10-19 18:24:35 +020092};
93
94/*
Atsushi Nemoto81365082008-04-27 01:51:12 +090095 * Enable NAND.
96 */
97static void at91_nand_enable(struct at91_nand_host *host)
98{
99 if (host->board->enable_pin)
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +0200100 gpio_set_value(host->board->enable_pin, 0);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900101}
102
103/*
104 * Disable NAND.
105 */
106static void at91_nand_disable(struct at91_nand_host *host)
107{
108 if (host->board->enable_pin)
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +0200109 gpio_set_value(host->board->enable_pin, 1);
Atsushi Nemoto81365082008-04-27 01:51:12 +0900110}
111
112/*
Andrew Victor42cb1402006-10-19 18:24:35 +0200113 * Hardware specific access to control-lines
114 */
115static void at91_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
116{
117 struct nand_chip *nand_chip = mtd->priv;
118 struct at91_nand_host *host = nand_chip->priv;
119
Atsushi Nemoto81365082008-04-27 01:51:12 +0900120 if (ctrl & NAND_CTRL_CHANGE) {
Atsushi Nemoto23144882008-04-24 23:51:29 +0900121 if (ctrl & NAND_NCE)
Atsushi Nemoto81365082008-04-27 01:51:12 +0900122 at91_nand_enable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900123 else
Atsushi Nemoto81365082008-04-27 01:51:12 +0900124 at91_nand_disable(host);
Atsushi Nemoto23144882008-04-24 23:51:29 +0900125 }
Andrew Victor42cb1402006-10-19 18:24:35 +0200126 if (cmd == NAND_CMD_NONE)
127 return;
128
129 if (ctrl & NAND_CLE)
130 writeb(cmd, host->io_base + (1 << host->board->cle));
131 else
132 writeb(cmd, host->io_base + (1 << host->board->ale));
133}
134
135/*
136 * Read the Device Ready pin.
137 */
138static int at91_nand_device_ready(struct mtd_info *mtd)
139{
140 struct nand_chip *nand_chip = mtd->priv;
141 struct at91_nand_host *host = nand_chip->priv;
142
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +0200143 return gpio_get_value(host->board->rdy_pin);
Andrew Victor42cb1402006-10-19 18:24:35 +0200144}
145
146/*
Richard Genoud77f54922008-04-23 19:51:14 +0200147 * write oob for small pages
148 */
149static int at91_nand_write_oob_512(struct mtd_info *mtd,
150 struct nand_chip *chip, int page)
151{
152 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
153 int eccsize = chip->ecc.size, length = mtd->oobsize;
154 int len, pos, status = 0;
155 const uint8_t *bufpoi = chip->oob_poi;
156
157 pos = eccsize + chunk;
158
159 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
160 len = min_t(int, length, chunk);
161 chip->write_buf(mtd, bufpoi, len);
162 bufpoi += len;
163 length -= len;
164 if (length > 0)
165 chip->write_buf(mtd, bufpoi, length);
166
167 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
168 status = chip->waitfunc(mtd, chip);
169
170 return status & NAND_STATUS_FAIL ? -EIO : 0;
171
172}
173
174/*
175 * read oob for small pages
176 */
177static int at91_nand_read_oob_512(struct mtd_info *mtd,
178 struct nand_chip *chip, int page, int sndcmd)
179{
180 if (sndcmd) {
181 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
182 sndcmd = 0;
183 }
184 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
185 return sndcmd;
186}
187
188/*
189 * Calculate HW ECC
190 *
191 * function called after a write
192 *
193 * mtd: MTD block structure
194 * dat: raw data (unused)
195 * ecc_code: buffer for ECC
196 */
197static int at91_nand_calculate(struct mtd_info *mtd,
198 const u_char *dat, unsigned char *ecc_code)
199{
200 struct nand_chip *nand_chip = mtd->priv;
201 struct at91_nand_host *host = nand_chip->priv;
202 uint32_t *eccpos = nand_chip->ecc.layout->eccpos;
203 unsigned int ecc_value;
204
205 /* get the first 2 ECC bytes */
Richard Genoudd43fa142008-04-25 09:32:26 +0200206 ecc_value = ecc_readl(host->ecc, PR);
Richard Genoud77f54922008-04-23 19:51:14 +0200207
208 ecc_code[eccpos[0]] = ecc_value & 0xFF;
209 ecc_code[eccpos[1]] = (ecc_value >> 8) & 0xFF;
210
211 /* get the last 2 ECC bytes */
212 ecc_value = ecc_readl(host->ecc, NPR) & AT91_ECC_NPARITY;
213
214 ecc_code[eccpos[2]] = ecc_value & 0xFF;
215 ecc_code[eccpos[3]] = (ecc_value >> 8) & 0xFF;
216
217 return 0;
218}
219
220/*
221 * HW ECC read page function
222 *
223 * mtd: mtd info structure
224 * chip: nand chip info structure
225 * buf: buffer to store read data
226 */
227static int at91_nand_read_page(struct mtd_info *mtd,
228 struct nand_chip *chip, uint8_t *buf)
229{
230 int eccsize = chip->ecc.size;
231 int eccbytes = chip->ecc.bytes;
232 uint32_t *eccpos = chip->ecc.layout->eccpos;
233 uint8_t *p = buf;
234 uint8_t *oob = chip->oob_poi;
235 uint8_t *ecc_pos;
236 int stat;
237
238 /* read the page */
239 chip->read_buf(mtd, p, eccsize);
240
241 /* move to ECC position if needed */
242 if (eccpos[0] != 0) {
243 /* This only works on large pages
244 * because the ECC controller waits for
245 * NAND_CMD_RNDOUTSTART after the
246 * NAND_CMD_RNDOUT.
247 * anyway, for small pages, the eccpos[0] == 0
248 */
249 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
250 mtd->writesize + eccpos[0], -1);
251 }
252
253 /* the ECC controller needs to read the ECC just after the data */
254 ecc_pos = oob + eccpos[0];
255 chip->read_buf(mtd, ecc_pos, eccbytes);
256
257 /* check if there's an error */
258 stat = chip->ecc.correct(mtd, p, oob, NULL);
259
260 if (stat < 0)
261 mtd->ecc_stats.failed++;
262 else
263 mtd->ecc_stats.corrected += stat;
264
265 /* get back to oob start (end of page) */
266 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
267
268 /* read the oob */
269 chip->read_buf(mtd, oob, mtd->oobsize);
270
271 return 0;
272}
273
274/*
275 * HW ECC Correction
276 *
277 * function called after a read
278 *
279 * mtd: MTD block structure
280 * dat: raw data read from the chip
281 * read_ecc: ECC from the chip (unused)
282 * isnull: unused
283 *
284 * Detect and correct a 1 bit error for a page
285 */
286static int at91_nand_correct(struct mtd_info *mtd, u_char *dat,
287 u_char *read_ecc, u_char *isnull)
288{
289 struct nand_chip *nand_chip = mtd->priv;
290 struct at91_nand_host *host = nand_chip->priv;
291 unsigned int ecc_status;
292 unsigned int ecc_word, ecc_bit;
293
294 /* get the status from the Status Register */
295 ecc_status = ecc_readl(host->ecc, SR);
296
297 /* if there's no error */
298 if (likely(!(ecc_status & AT91_ECC_RECERR)))
299 return 0;
300
301 /* get error bit offset (4 bits) */
302 ecc_bit = ecc_readl(host->ecc, PR) & AT91_ECC_BITADDR;
303 /* get word address (12 bits) */
304 ecc_word = ecc_readl(host->ecc, PR) & AT91_ECC_WORDADDR;
305 ecc_word >>= 4;
306
307 /* if there are multiple errors */
308 if (ecc_status & AT91_ECC_MULERR) {
309 /* check if it is a freshly erased block
310 * (filled with 0xff) */
311 if ((ecc_bit == AT91_ECC_BITADDR)
312 && (ecc_word == (AT91_ECC_WORDADDR >> 4))) {
313 /* the block has just been erased, return OK */
314 return 0;
315 }
316 /* it doesn't seems to be a freshly
317 * erased block.
318 * We can't correct so many errors */
319 dev_dbg(host->dev, "at91_nand : multiple errors detected."
320 " Unable to correct.\n");
321 return -EIO;
322 }
323
324 /* if there's a single bit error : we can correct it */
325 if (ecc_status & AT91_ECC_ECCERR) {
326 /* there's nothing much to do here.
327 * the bit error is on the ECC itself.
328 */
329 dev_dbg(host->dev, "at91_nand : one bit error on ECC code."
330 " Nothing to correct\n");
331 return 0;
332 }
333
334 dev_dbg(host->dev, "at91_nand : one bit error on data."
335 " (word offset in the page :"
336 " 0x%x bit offset : 0x%x)\n",
337 ecc_word, ecc_bit);
338 /* correct the error */
339 if (nand_chip->options & NAND_BUSWIDTH_16) {
340 /* 16 bits words */
341 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
342 } else {
343 /* 8 bits words */
344 dat[ecc_word] ^= (1 << ecc_bit);
345 }
346 dev_dbg(host->dev, "at91_nand : error corrected\n");
347 return 1;
348}
349
350/*
351 * Enable HW ECC : unsused
352 */
353static void at91_nand_hwctl(struct mtd_info *mtd, int mode) { ; }
354
Andrew Victor693ef662007-05-03 08:16:44 +0200355#ifdef CONFIG_MTD_PARTITIONS
Atsushi Nemoto52f83012008-03-30 21:59:37 +0900356static const char *part_probes[] = { "cmdlinepart", NULL };
Andrew Victor693ef662007-05-03 08:16:44 +0200357#endif
358
Andrew Victor42cb1402006-10-19 18:24:35 +0200359/*
360 * Probe for the NAND device.
361 */
362static int __init at91_nand_probe(struct platform_device *pdev)
363{
364 struct at91_nand_host *host;
365 struct mtd_info *mtd;
366 struct nand_chip *nand_chip;
Richard Genoud77f54922008-04-23 19:51:14 +0200367 struct resource *regs;
368 struct resource *mem;
Andrew Victor42cb1402006-10-19 18:24:35 +0200369 int res;
370
371#ifdef CONFIG_MTD_PARTITIONS
372 struct mtd_partition *partitions = NULL;
373 int num_partitions = 0;
374#endif
375
376 /* Allocate memory for the device structure (and zero it) */
377 host = kzalloc(sizeof(struct at91_nand_host), GFP_KERNEL);
378 if (!host) {
379 printk(KERN_ERR "at91_nand: failed to allocate device structure.\n");
380 return -ENOMEM;
381 }
382
Richard Genoud77f54922008-04-23 19:51:14 +0200383 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
384 if (!mem) {
385 printk(KERN_ERR "at91_nand: can't get I/O resource mem\n");
386 return -ENXIO;
387 }
388
389 host->io_base = ioremap(mem->start, mem->end - mem->start + 1);
Andrew Victor42cb1402006-10-19 18:24:35 +0200390 if (host->io_base == NULL) {
391 printk(KERN_ERR "at91_nand: ioremap failed\n");
392 kfree(host);
393 return -EIO;
394 }
395
396 mtd = &host->mtd;
397 nand_chip = &host->nand_chip;
398 host->board = pdev->dev.platform_data;
Richard Genoud77f54922008-04-23 19:51:14 +0200399 host->dev = &pdev->dev;
Andrew Victor42cb1402006-10-19 18:24:35 +0200400
401 nand_chip->priv = host; /* link the private data structures */
402 mtd->priv = nand_chip;
403 mtd->owner = THIS_MODULE;
404
405 /* Set address of NAND IO lines */
406 nand_chip->IO_ADDR_R = host->io_base;
407 nand_chip->IO_ADDR_W = host->io_base;
408 nand_chip->cmd_ctrl = at91_nand_cmd_ctrl;
Ivan Kutena4265f82007-05-24 14:35:58 +0300409
410 if (host->board->rdy_pin)
411 nand_chip->dev_ready = at91_nand_device_ready;
412
Richard Genoud77f54922008-04-23 19:51:14 +0200413 regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
414 if (!regs && hard_ecc) {
415 printk(KERN_ERR "at91_nand: can't get I/O resource "
416 "regs\nFalling back on software ECC\n");
417 }
418
Andrew Victor42cb1402006-10-19 18:24:35 +0200419 nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
Richard Genoud77f54922008-04-23 19:51:14 +0200420 if (no_ecc)
421 nand_chip->ecc.mode = NAND_ECC_NONE;
422 if (hard_ecc && regs) {
423 host->ecc = ioremap(regs->start, regs->end - regs->start + 1);
424 if (host->ecc == NULL) {
425 printk(KERN_ERR "at91_nand: ioremap failed\n");
426 res = -EIO;
427 goto err_ecc_ioremap;
428 }
429 nand_chip->ecc.mode = NAND_ECC_HW_SYNDROME;
430 nand_chip->ecc.calculate = at91_nand_calculate;
431 nand_chip->ecc.correct = at91_nand_correct;
432 nand_chip->ecc.hwctl = at91_nand_hwctl;
433 nand_chip->ecc.read_page = at91_nand_read_page;
434 nand_chip->ecc.bytes = 4;
435 nand_chip->ecc.prepad = 0;
436 nand_chip->ecc.postpad = 0;
437 }
438
Andrew Victor42cb1402006-10-19 18:24:35 +0200439 nand_chip->chip_delay = 20; /* 20us command delay time */
440
Andrew Victordd11b8c2006-12-08 13:49:42 +0200441 if (host->board->bus_width_16) /* 16-bit bus width */
442 nand_chip->options |= NAND_BUSWIDTH_16;
443
Andrew Victor42cb1402006-10-19 18:24:35 +0200444 platform_set_drvdata(pdev, host);
445 at91_nand_enable(host);
446
447 if (host->board->det_pin) {
Håvard Skinnemoen62fd71f2008-06-06 18:04:51 +0200448 if (gpio_get_value(host->board->det_pin)) {
Andrew Victor42cb1402006-10-19 18:24:35 +0200449 printk ("No SmartMedia card inserted.\n");
450 res = ENXIO;
451 goto out;
452 }
453 }
454
Richard Genoud77f54922008-04-23 19:51:14 +0200455 /* first scan to find the device and get the page size */
456 if (nand_scan_ident(mtd, 1)) {
457 res = -ENXIO;
458 goto out;
459 }
460
461 if (nand_chip->ecc.mode == NAND_ECC_HW_SYNDROME) {
462 /* ECC is calculated for the whole page (1 step) */
463 nand_chip->ecc.size = mtd->writesize;
464
465 /* set ECC page size and oob layout */
466 switch (mtd->writesize) {
467 case 512:
468 nand_chip->ecc.layout = &at91_oobinfo_small;
469 nand_chip->ecc.read_oob = at91_nand_read_oob_512;
470 nand_chip->ecc.write_oob = at91_nand_write_oob_512;
471 ecc_writel(host->ecc, MR, AT91_ECC_PAGESIZE_528);
472 break;
473 case 1024:
474 nand_chip->ecc.layout = &at91_oobinfo_large;
475 ecc_writel(host->ecc, MR, AT91_ECC_PAGESIZE_1056);
476 break;
477 case 2048:
478 nand_chip->ecc.layout = &at91_oobinfo_large;
479 ecc_writel(host->ecc, MR, AT91_ECC_PAGESIZE_2112);
480 break;
481 case 4096:
482 nand_chip->ecc.layout = &at91_oobinfo_large;
483 ecc_writel(host->ecc, MR, AT91_ECC_PAGESIZE_4224);
484 break;
485 default:
486 /* page size not handled by HW ECC */
487 /* switching back to soft ECC */
488 nand_chip->ecc.mode = NAND_ECC_SOFT;
489 nand_chip->ecc.calculate = NULL;
490 nand_chip->ecc.correct = NULL;
491 nand_chip->ecc.hwctl = NULL;
492 nand_chip->ecc.read_page = NULL;
493 nand_chip->ecc.postpad = 0;
494 nand_chip->ecc.prepad = 0;
495 nand_chip->ecc.bytes = 0;
496 break;
497 }
498 }
499
500 /* second phase scan */
501 if (nand_scan_tail(mtd)) {
Andrew Victor42cb1402006-10-19 18:24:35 +0200502 res = -ENXIO;
503 goto out;
504 }
505
506#ifdef CONFIG_MTD_PARTITIONS
Andrew Victor693ef662007-05-03 08:16:44 +0200507#ifdef CONFIG_MTD_CMDLINE_PARTS
Atsushi Nemoto842b1a102008-01-29 22:28:22 +0900508 mtd->name = "at91_nand";
509 num_partitions = parse_mtd_partitions(mtd, part_probes,
510 &partitions, 0);
Andrew Victor693ef662007-05-03 08:16:44 +0200511#endif
Atsushi Nemoto842b1a102008-01-29 22:28:22 +0900512 if (num_partitions <= 0 && host->board->partition_info)
513 partitions = host->board->partition_info(mtd->size,
514 &num_partitions);
Andrew Victor42cb1402006-10-19 18:24:35 +0200515
516 if ((!partitions) || (num_partitions == 0)) {
517 printk(KERN_ERR "at91_nand: No parititions defined, or unsupported device.\n");
518 res = ENXIO;
519 goto release;
520 }
521
522 res = add_mtd_partitions(mtd, partitions, num_partitions);
523#else
524 res = add_mtd_device(mtd);
525#endif
526
527 if (!res)
528 return res;
529
Richard Genoud77f54922008-04-23 19:51:14 +0200530#ifdef CONFIG_MTD_PARTITIONS
Andrew Victor42cb1402006-10-19 18:24:35 +0200531release:
Richard Genoud77f54922008-04-23 19:51:14 +0200532#endif
Andrew Victor42cb1402006-10-19 18:24:35 +0200533 nand_release(mtd);
Richard Genoud77f54922008-04-23 19:51:14 +0200534
Andrew Victor42cb1402006-10-19 18:24:35 +0200535out:
Richard Genoud77f54922008-04-23 19:51:14 +0200536 iounmap(host->ecc);
537
538err_ecc_ioremap:
Andrew Victor42cb1402006-10-19 18:24:35 +0200539 at91_nand_disable(host);
540 platform_set_drvdata(pdev, NULL);
541 iounmap(host->io_base);
542 kfree(host);
543 return res;
544}
545
546/*
547 * Remove a NAND device.
548 */
549static int __devexit at91_nand_remove(struct platform_device *pdev)
550{
551 struct at91_nand_host *host = platform_get_drvdata(pdev);
552 struct mtd_info *mtd = &host->mtd;
553
554 nand_release(mtd);
555
556 at91_nand_disable(host);
557
558 iounmap(host->io_base);
Richard Genoud77f54922008-04-23 19:51:14 +0200559 iounmap(host->ecc);
Andrew Victor42cb1402006-10-19 18:24:35 +0200560 kfree(host);
561
562 return 0;
563}
564
565static struct platform_driver at91_nand_driver = {
566 .probe = at91_nand_probe,
567 .remove = at91_nand_remove,
568 .driver = {
569 .name = "at91_nand",
570 .owner = THIS_MODULE,
571 },
572};
573
574static int __init at91_nand_init(void)
575{
576 return platform_driver_register(&at91_nand_driver);
577}
578
579
580static void __exit at91_nand_exit(void)
581{
582 platform_driver_unregister(&at91_nand_driver);
583}
584
585
586module_init(at91_nand_init);
587module_exit(at91_nand_exit);
588
589MODULE_LICENSE("GPL");
590MODULE_AUTHOR("Rick Bronson");
Richard Genoud77f54922008-04-23 19:51:14 +0200591MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91RM9200 / AT91SAM9");
Kay Sievers1ff18422008-04-18 13:44:27 -0700592MODULE_ALIAS("platform:at91_nand");