2 * drivers/mtd/nand/at91_nand.c
4 * Copyright (C) 2003 Rick Bronson
6 * Derived from drivers/mtd/nand/autcpu12.c
7 * Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de)
9 * Derived from drivers/mtd/spia.c
10 * Copyright (C) 2000 Steven J. Hill (sjhill@cotw.com)
13 * Add Hardware ECC support for AT91SAM9260 / AT91SAM9263
14 * Richard Genoud (richard.genoud@gmail.com), Adeneo Copyright (C) 2007
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
21 * 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.
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>
35 #include <asm/sizes.h>
37 #include <asm/hardware.h>
38 #include <asm/arch/board.h>
39 #include <asm/arch/gpio.h>
41 #ifdef CONFIG_MTD_NAND_AT91_ECC_HW
47 #ifdef CONFIG_MTD_NAND_AT91_ECC_NONE
53 /* Register access macros */
54 #define ecc_readl(add, reg) \
55 __raw_readl(add + AT91_ECC_##reg)
56 #define ecc_writel(add, reg, value) \
57 __raw_writel((value), add + AT91_ECC_##reg)
59 #include <asm/arch/at91_ecc.h> /* AT91SAM9260/3 ECC registers */
61 /* oob layout for large page size
62 * bad block info is on bytes 0 and 1
63 * the bytes have to be consecutives to avoid
64 * several NAND_CMD_RNDOUT during read
66 static struct nand_ecclayout at91_oobinfo_large
= {
68 .eccpos
= {60, 61, 62, 63},
74 /* oob layout for small page size
75 * bad block info is on bytes 4 and 5
76 * the bytes have to be consecutives to avoid
77 * several NAND_CMD_RNDOUT during read
79 static struct nand_ecclayout at91_oobinfo_small
= {
81 .eccpos
= {0, 1, 2, 3},
87 struct at91_nand_host
{
88 struct nand_chip nand_chip
;
90 void __iomem
*io_base
;
91 struct at91_nand_data
*board
;
99 static void at91_nand_enable(struct at91_nand_host
*host
)
101 if (host
->board
->enable_pin
)
102 at91_set_gpio_value(host
->board
->enable_pin
, 0);
108 static void at91_nand_disable(struct at91_nand_host
*host
)
110 if (host
->board
->enable_pin
)
111 at91_set_gpio_value(host
->board
->enable_pin
, 1);
115 * Hardware specific access to control-lines
117 static void at91_nand_cmd_ctrl(struct mtd_info
*mtd
, int cmd
, unsigned int ctrl
)
119 struct nand_chip
*nand_chip
= mtd
->priv
;
120 struct at91_nand_host
*host
= nand_chip
->priv
;
122 if (ctrl
& NAND_CTRL_CHANGE
) {
124 at91_nand_enable(host
);
126 at91_nand_disable(host
);
128 if (cmd
== NAND_CMD_NONE
)
132 writeb(cmd
, host
->io_base
+ (1 << host
->board
->cle
));
134 writeb(cmd
, host
->io_base
+ (1 << host
->board
->ale
));
138 * Read the Device Ready pin.
140 static int at91_nand_device_ready(struct mtd_info
*mtd
)
142 struct nand_chip
*nand_chip
= mtd
->priv
;
143 struct at91_nand_host
*host
= nand_chip
->priv
;
145 return at91_get_gpio_value(host
->board
->rdy_pin
);
149 * write oob for small pages
151 static int at91_nand_write_oob_512(struct mtd_info
*mtd
,
152 struct nand_chip
*chip
, int page
)
154 int chunk
= chip
->ecc
.bytes
+ chip
->ecc
.prepad
+ chip
->ecc
.postpad
;
155 int eccsize
= chip
->ecc
.size
, length
= mtd
->oobsize
;
156 int len
, pos
, status
= 0;
157 const uint8_t *bufpoi
= chip
->oob_poi
;
159 pos
= eccsize
+ chunk
;
161 chip
->cmdfunc(mtd
, NAND_CMD_SEQIN
, pos
, page
);
162 len
= min_t(int, length
, chunk
);
163 chip
->write_buf(mtd
, bufpoi
, len
);
167 chip
->write_buf(mtd
, bufpoi
, length
);
169 chip
->cmdfunc(mtd
, NAND_CMD_PAGEPROG
, -1, -1);
170 status
= chip
->waitfunc(mtd
, chip
);
172 return status
& NAND_STATUS_FAIL
? -EIO
: 0;
177 * read oob for small pages
179 static int at91_nand_read_oob_512(struct mtd_info
*mtd
,
180 struct nand_chip
*chip
, int page
, int sndcmd
)
183 chip
->cmdfunc(mtd
, NAND_CMD_READOOB
, 0, page
);
186 chip
->read_buf(mtd
, chip
->oob_poi
, mtd
->oobsize
);
193 * function called after a write
195 * mtd: MTD block structure
196 * dat: raw data (unused)
197 * ecc_code: buffer for ECC
199 static int at91_nand_calculate(struct mtd_info
*mtd
,
200 const u_char
*dat
, unsigned char *ecc_code
)
202 struct nand_chip
*nand_chip
= mtd
->priv
;
203 struct at91_nand_host
*host
= nand_chip
->priv
;
204 uint32_t *eccpos
= nand_chip
->ecc
.layout
->eccpos
;
205 unsigned int ecc_value
;
207 /* get the first 2 ECC bytes */
208 ecc_value
= ecc_readl(host
->ecc
, PR
);
210 ecc_code
[eccpos
[0]] = ecc_value
& 0xFF;
211 ecc_code
[eccpos
[1]] = (ecc_value
>> 8) & 0xFF;
213 /* get the last 2 ECC bytes */
214 ecc_value
= ecc_readl(host
->ecc
, NPR
) & AT91_ECC_NPARITY
;
216 ecc_code
[eccpos
[2]] = ecc_value
& 0xFF;
217 ecc_code
[eccpos
[3]] = (ecc_value
>> 8) & 0xFF;
223 * HW ECC read page function
225 * mtd: mtd info structure
226 * chip: nand chip info structure
227 * buf: buffer to store read data
229 static int at91_nand_read_page(struct mtd_info
*mtd
,
230 struct nand_chip
*chip
, uint8_t *buf
)
232 int eccsize
= chip
->ecc
.size
;
233 int eccbytes
= chip
->ecc
.bytes
;
234 uint32_t *eccpos
= chip
->ecc
.layout
->eccpos
;
236 uint8_t *oob
= chip
->oob_poi
;
241 chip
->read_buf(mtd
, p
, eccsize
);
243 /* move to ECC position if needed */
244 if (eccpos
[0] != 0) {
245 /* This only works on large pages
246 * because the ECC controller waits for
247 * NAND_CMD_RNDOUTSTART after the
249 * anyway, for small pages, the eccpos[0] == 0
251 chip
->cmdfunc(mtd
, NAND_CMD_RNDOUT
,
252 mtd
->writesize
+ eccpos
[0], -1);
255 /* the ECC controller needs to read the ECC just after the data */
256 ecc_pos
= oob
+ eccpos
[0];
257 chip
->read_buf(mtd
, ecc_pos
, eccbytes
);
259 /* check if there's an error */
260 stat
= chip
->ecc
.correct(mtd
, p
, oob
, NULL
);
263 mtd
->ecc_stats
.failed
++;
265 mtd
->ecc_stats
.corrected
+= stat
;
267 /* get back to oob start (end of page) */
268 chip
->cmdfunc(mtd
, NAND_CMD_RNDOUT
, mtd
->writesize
, -1);
271 chip
->read_buf(mtd
, oob
, mtd
->oobsize
);
279 * function called after a read
281 * mtd: MTD block structure
282 * dat: raw data read from the chip
283 * read_ecc: ECC from the chip (unused)
286 * Detect and correct a 1 bit error for a page
288 static int at91_nand_correct(struct mtd_info
*mtd
, u_char
*dat
,
289 u_char
*read_ecc
, u_char
*isnull
)
291 struct nand_chip
*nand_chip
= mtd
->priv
;
292 struct at91_nand_host
*host
= nand_chip
->priv
;
293 unsigned int ecc_status
;
294 unsigned int ecc_word
, ecc_bit
;
296 /* get the status from the Status Register */
297 ecc_status
= ecc_readl(host
->ecc
, SR
);
299 /* if there's no error */
300 if (likely(!(ecc_status
& AT91_ECC_RECERR
)))
303 /* get error bit offset (4 bits) */
304 ecc_bit
= ecc_readl(host
->ecc
, PR
) & AT91_ECC_BITADDR
;
305 /* get word address (12 bits) */
306 ecc_word
= ecc_readl(host
->ecc
, PR
) & AT91_ECC_WORDADDR
;
309 /* if there are multiple errors */
310 if (ecc_status
& AT91_ECC_MULERR
) {
311 /* check if it is a freshly erased block
312 * (filled with 0xff) */
313 if ((ecc_bit
== AT91_ECC_BITADDR
)
314 && (ecc_word
== (AT91_ECC_WORDADDR
>> 4))) {
315 /* the block has just been erased, return OK */
318 /* it doesn't seems to be a freshly
320 * We can't correct so many errors */
321 dev_dbg(host
->dev
, "at91_nand : multiple errors detected."
322 " Unable to correct.\n");
326 /* if there's a single bit error : we can correct it */
327 if (ecc_status
& AT91_ECC_ECCERR
) {
328 /* there's nothing much to do here.
329 * the bit error is on the ECC itself.
331 dev_dbg(host
->dev
, "at91_nand : one bit error on ECC code."
332 " Nothing to correct\n");
336 dev_dbg(host
->dev
, "at91_nand : one bit error on data."
337 " (word offset in the page :"
338 " 0x%x bit offset : 0x%x)\n",
340 /* correct the error */
341 if (nand_chip
->options
& NAND_BUSWIDTH_16
) {
343 ((unsigned short *) dat
)[ecc_word
] ^= (1 << ecc_bit
);
346 dat
[ecc_word
] ^= (1 << ecc_bit
);
348 dev_dbg(host
->dev
, "at91_nand : error corrected\n");
353 * Enable HW ECC : unsused
355 static void at91_nand_hwctl(struct mtd_info
*mtd
, int mode
) { ; }
357 #ifdef CONFIG_MTD_PARTITIONS
358 static const char *part_probes
[] = { "cmdlinepart", NULL
};
362 * Probe for the NAND device.
364 static int __init
at91_nand_probe(struct platform_device
*pdev
)
366 struct at91_nand_host
*host
;
367 struct mtd_info
*mtd
;
368 struct nand_chip
*nand_chip
;
369 struct resource
*regs
;
370 struct resource
*mem
;
373 #ifdef CONFIG_MTD_PARTITIONS
374 struct mtd_partition
*partitions
= NULL
;
375 int num_partitions
= 0;
378 /* Allocate memory for the device structure (and zero it) */
379 host
= kzalloc(sizeof(struct at91_nand_host
), GFP_KERNEL
);
381 printk(KERN_ERR
"at91_nand: failed to allocate device structure.\n");
385 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
387 printk(KERN_ERR
"at91_nand: can't get I/O resource mem\n");
391 host
->io_base
= ioremap(mem
->start
, mem
->end
- mem
->start
+ 1);
392 if (host
->io_base
== NULL
) {
393 printk(KERN_ERR
"at91_nand: ioremap failed\n");
399 nand_chip
= &host
->nand_chip
;
400 host
->board
= pdev
->dev
.platform_data
;
401 host
->dev
= &pdev
->dev
;
403 nand_chip
->priv
= host
; /* link the private data structures */
404 mtd
->priv
= nand_chip
;
405 mtd
->owner
= THIS_MODULE
;
407 /* Set address of NAND IO lines */
408 nand_chip
->IO_ADDR_R
= host
->io_base
;
409 nand_chip
->IO_ADDR_W
= host
->io_base
;
410 nand_chip
->cmd_ctrl
= at91_nand_cmd_ctrl
;
412 if (host
->board
->rdy_pin
)
413 nand_chip
->dev_ready
= at91_nand_device_ready
;
415 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
416 if (!regs
&& hard_ecc
) {
417 printk(KERN_ERR
"at91_nand: can't get I/O resource "
418 "regs\nFalling back on software ECC\n");
421 nand_chip
->ecc
.mode
= NAND_ECC_SOFT
; /* enable ECC */
423 nand_chip
->ecc
.mode
= NAND_ECC_NONE
;
424 if (hard_ecc
&& regs
) {
425 host
->ecc
= ioremap(regs
->start
, regs
->end
- regs
->start
+ 1);
426 if (host
->ecc
== NULL
) {
427 printk(KERN_ERR
"at91_nand: ioremap failed\n");
429 goto err_ecc_ioremap
;
431 nand_chip
->ecc
.mode
= NAND_ECC_HW_SYNDROME
;
432 nand_chip
->ecc
.calculate
= at91_nand_calculate
;
433 nand_chip
->ecc
.correct
= at91_nand_correct
;
434 nand_chip
->ecc
.hwctl
= at91_nand_hwctl
;
435 nand_chip
->ecc
.read_page
= at91_nand_read_page
;
436 nand_chip
->ecc
.bytes
= 4;
437 nand_chip
->ecc
.prepad
= 0;
438 nand_chip
->ecc
.postpad
= 0;
441 nand_chip
->chip_delay
= 20; /* 20us command delay time */
443 if (host
->board
->bus_width_16
) /* 16-bit bus width */
444 nand_chip
->options
|= NAND_BUSWIDTH_16
;
446 platform_set_drvdata(pdev
, host
);
447 at91_nand_enable(host
);
449 if (host
->board
->det_pin
) {
450 if (at91_get_gpio_value(host
->board
->det_pin
)) {
451 printk ("No SmartMedia card inserted.\n");
457 /* first scan to find the device and get the page size */
458 if (nand_scan_ident(mtd
, 1)) {
463 if (nand_chip
->ecc
.mode
== NAND_ECC_HW_SYNDROME
) {
464 /* ECC is calculated for the whole page (1 step) */
465 nand_chip
->ecc
.size
= mtd
->writesize
;
467 /* set ECC page size and oob layout */
468 switch (mtd
->writesize
) {
470 nand_chip
->ecc
.layout
= &at91_oobinfo_small
;
471 nand_chip
->ecc
.read_oob
= at91_nand_read_oob_512
;
472 nand_chip
->ecc
.write_oob
= at91_nand_write_oob_512
;
473 ecc_writel(host
->ecc
, MR
, AT91_ECC_PAGESIZE_528
);
476 nand_chip
->ecc
.layout
= &at91_oobinfo_large
;
477 ecc_writel(host
->ecc
, MR
, AT91_ECC_PAGESIZE_1056
);
480 nand_chip
->ecc
.layout
= &at91_oobinfo_large
;
481 ecc_writel(host
->ecc
, MR
, AT91_ECC_PAGESIZE_2112
);
484 nand_chip
->ecc
.layout
= &at91_oobinfo_large
;
485 ecc_writel(host
->ecc
, MR
, AT91_ECC_PAGESIZE_4224
);
488 /* page size not handled by HW ECC */
489 /* switching back to soft ECC */
490 nand_chip
->ecc
.mode
= NAND_ECC_SOFT
;
491 nand_chip
->ecc
.calculate
= NULL
;
492 nand_chip
->ecc
.correct
= NULL
;
493 nand_chip
->ecc
.hwctl
= NULL
;
494 nand_chip
->ecc
.read_page
= NULL
;
495 nand_chip
->ecc
.postpad
= 0;
496 nand_chip
->ecc
.prepad
= 0;
497 nand_chip
->ecc
.bytes
= 0;
502 /* second phase scan */
503 if (nand_scan_tail(mtd
)) {
508 #ifdef CONFIG_MTD_PARTITIONS
509 #ifdef CONFIG_MTD_CMDLINE_PARTS
510 mtd
->name
= "at91_nand";
511 num_partitions
= parse_mtd_partitions(mtd
, part_probes
,
514 if (num_partitions
<= 0 && host
->board
->partition_info
)
515 partitions
= host
->board
->partition_info(mtd
->size
,
518 if ((!partitions
) || (num_partitions
== 0)) {
519 printk(KERN_ERR
"at91_nand: No parititions defined, or unsupported device.\n");
524 res
= add_mtd_partitions(mtd
, partitions
, num_partitions
);
526 res
= add_mtd_device(mtd
);
532 #ifdef CONFIG_MTD_PARTITIONS
541 at91_nand_disable(host
);
542 platform_set_drvdata(pdev
, NULL
);
543 iounmap(host
->io_base
);
549 * Remove a NAND device.
551 static int __devexit
at91_nand_remove(struct platform_device
*pdev
)
553 struct at91_nand_host
*host
= platform_get_drvdata(pdev
);
554 struct mtd_info
*mtd
= &host
->mtd
;
558 at91_nand_disable(host
);
560 iounmap(host
->io_base
);
567 static struct platform_driver at91_nand_driver
= {
568 .probe
= at91_nand_probe
,
569 .remove
= at91_nand_remove
,
572 .owner
= THIS_MODULE
,
576 static int __init
at91_nand_init(void)
578 return platform_driver_register(&at91_nand_driver
);
582 static void __exit
at91_nand_exit(void)
584 platform_driver_unregister(&at91_nand_driver
);
588 module_init(at91_nand_init
);
589 module_exit(at91_nand_exit
);
591 MODULE_LICENSE("GPL");
592 MODULE_AUTHOR("Rick Bronson");
593 MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91RM9200 / AT91SAM9");
594 MODULE_ALIAS("platform:at91_nand");