2 * Copyright (C) 2003 Rick Bronson
4 * Derived from drivers/mtd/nand/autcpu12.c
5 * Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de)
7 * Derived from drivers/mtd/spia.c
8 * Copyright (C) 2000 Steven J. Hill (sjhill@cotw.com)
11 * Add Hardware ECC support for AT91SAM9260 / AT91SAM9263
12 * Richard Genoud (richard.genoud@gmail.com), Adeneo Copyright (C) 2007
14 * Derived from Das U-Boot source code
15 * (u-boot-1.1.5/board/atmel/at91sam9263ek/nand.c)
16 * (C) Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License version 2 as
21 * published by the Free Software Foundation.
25 #include <linux/slab.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/platform_device.h>
29 #include <linux/mtd/mtd.h>
30 #include <linux/mtd/nand.h>
31 #include <linux/mtd/partitions.h>
33 #include <linux/dmaengine.h>
34 #include <linux/gpio.h>
37 #include <mach/board.h>
40 #ifdef CONFIG_MTD_NAND_ATMEL_ECC_HW
46 #ifdef CONFIG_MTD_NAND_ATMEL_ECC_NONE
52 static int use_dma
= 1;
53 module_param(use_dma
, int, 0);
55 static int on_flash_bbt
= 0;
56 module_param(on_flash_bbt
, int, 0);
58 /* Register access macros */
59 #define ecc_readl(add, reg) \
60 __raw_readl(add + ATMEL_ECC_##reg)
61 #define ecc_writel(add, reg, value) \
62 __raw_writel((value), add + ATMEL_ECC_##reg)
64 #include "atmel_nand_ecc.h" /* Hardware ECC registers */
66 /* oob layout for large page size
67 * bad block info is on bytes 0 and 1
68 * the bytes have to be consecutives to avoid
69 * several NAND_CMD_RNDOUT during read
71 static struct nand_ecclayout atmel_oobinfo_large
= {
73 .eccpos
= {60, 61, 62, 63},
79 /* oob layout for small page size
80 * bad block info is on bytes 4 and 5
81 * the bytes have to be consecutives to avoid
82 * several NAND_CMD_RNDOUT during read
84 static struct nand_ecclayout atmel_oobinfo_small
= {
86 .eccpos
= {0, 1, 2, 3},
92 struct atmel_nand_host
{
93 struct nand_chip nand_chip
;
95 void __iomem
*io_base
;
97 struct atmel_nand_data
*board
;
101 struct completion comp
;
102 struct dma_chan
*dma_chan
;
105 static int cpu_has_dma(void)
107 return cpu_is_at91sam9rl() || cpu_is_at91sam9g45();
113 static void atmel_nand_enable(struct atmel_nand_host
*host
)
115 if (host
->board
->enable_pin
)
116 gpio_set_value(host
->board
->enable_pin
, 0);
122 static void atmel_nand_disable(struct atmel_nand_host
*host
)
124 if (host
->board
->enable_pin
)
125 gpio_set_value(host
->board
->enable_pin
, 1);
129 * Hardware specific access to control-lines
131 static void atmel_nand_cmd_ctrl(struct mtd_info
*mtd
, int cmd
, unsigned int ctrl
)
133 struct nand_chip
*nand_chip
= mtd
->priv
;
134 struct atmel_nand_host
*host
= nand_chip
->priv
;
136 if (ctrl
& NAND_CTRL_CHANGE
) {
138 atmel_nand_enable(host
);
140 atmel_nand_disable(host
);
142 if (cmd
== NAND_CMD_NONE
)
146 writeb(cmd
, host
->io_base
+ (1 << host
->board
->cle
));
148 writeb(cmd
, host
->io_base
+ (1 << host
->board
->ale
));
152 * Read the Device Ready pin.
154 static int atmel_nand_device_ready(struct mtd_info
*mtd
)
156 struct nand_chip
*nand_chip
= mtd
->priv
;
157 struct atmel_nand_host
*host
= nand_chip
->priv
;
159 return gpio_get_value(host
->board
->rdy_pin
) ^
160 !!host
->board
->rdy_pin_active_low
;
164 * Minimal-overhead PIO for data access.
166 static void atmel_read_buf8(struct mtd_info
*mtd
, u8
*buf
, int len
)
168 struct nand_chip
*nand_chip
= mtd
->priv
;
170 __raw_readsb(nand_chip
->IO_ADDR_R
, buf
, len
);
173 static void atmel_read_buf16(struct mtd_info
*mtd
, u8
*buf
, int len
)
175 struct nand_chip
*nand_chip
= mtd
->priv
;
177 __raw_readsw(nand_chip
->IO_ADDR_R
, buf
, len
/ 2);
180 static void atmel_write_buf8(struct mtd_info
*mtd
, const u8
*buf
, int len
)
182 struct nand_chip
*nand_chip
= mtd
->priv
;
184 __raw_writesb(nand_chip
->IO_ADDR_W
, buf
, len
);
187 static void atmel_write_buf16(struct mtd_info
*mtd
, const u8
*buf
, int len
)
189 struct nand_chip
*nand_chip
= mtd
->priv
;
191 __raw_writesw(nand_chip
->IO_ADDR_W
, buf
, len
/ 2);
194 static void dma_complete_func(void *completion
)
196 complete(completion
);
199 static int atmel_nand_dma_op(struct mtd_info
*mtd
, void *buf
, int len
,
202 struct dma_device
*dma_dev
;
203 enum dma_ctrl_flags flags
;
204 dma_addr_t dma_src_addr
, dma_dst_addr
, phys_addr
;
205 struct dma_async_tx_descriptor
*tx
= NULL
;
207 struct nand_chip
*chip
= mtd
->priv
;
208 struct atmel_nand_host
*host
= chip
->priv
;
211 enum dma_data_direction dir
= is_read
? DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
213 if (buf
>= high_memory
)
216 dma_dev
= host
->dma_chan
->device
;
218 flags
= DMA_CTRL_ACK
| DMA_PREP_INTERRUPT
| DMA_COMPL_SKIP_SRC_UNMAP
|
219 DMA_COMPL_SKIP_DEST_UNMAP
;
221 phys_addr
= dma_map_single(dma_dev
->dev
, p
, len
, dir
);
222 if (dma_mapping_error(dma_dev
->dev
, phys_addr
)) {
223 dev_err(host
->dev
, "Failed to dma_map_single\n");
228 dma_src_addr
= host
->io_phys
;
229 dma_dst_addr
= phys_addr
;
231 dma_src_addr
= phys_addr
;
232 dma_dst_addr
= host
->io_phys
;
235 tx
= dma_dev
->device_prep_dma_memcpy(host
->dma_chan
, dma_dst_addr
,
236 dma_src_addr
, len
, flags
);
238 dev_err(host
->dev
, "Failed to prepare DMA memcpy\n");
242 init_completion(&host
->comp
);
243 tx
->callback
= dma_complete_func
;
244 tx
->callback_param
= &host
->comp
;
246 cookie
= tx
->tx_submit(tx
);
247 if (dma_submit_error(cookie
)) {
248 dev_err(host
->dev
, "Failed to do DMA tx_submit\n");
252 dma_async_issue_pending(host
->dma_chan
);
253 wait_for_completion(&host
->comp
);
258 dma_unmap_single(dma_dev
->dev
, phys_addr
, len
, dir
);
261 dev_warn(host
->dev
, "Fall back to CPU I/O\n");
265 static void atmel_read_buf(struct mtd_info
*mtd
, u8
*buf
, int len
)
267 struct nand_chip
*chip
= mtd
->priv
;
268 struct atmel_nand_host
*host
= chip
->priv
;
270 if (use_dma
&& len
> mtd
->oobsize
)
271 /* only use DMA for bigger than oob size: better performances */
272 if (atmel_nand_dma_op(mtd
, buf
, len
, 1) == 0)
275 if (host
->board
->bus_width_16
)
276 atmel_read_buf16(mtd
, buf
, len
);
278 atmel_read_buf8(mtd
, buf
, len
);
281 static void atmel_write_buf(struct mtd_info
*mtd
, const u8
*buf
, int len
)
283 struct nand_chip
*chip
= mtd
->priv
;
284 struct atmel_nand_host
*host
= chip
->priv
;
286 if (use_dma
&& len
> mtd
->oobsize
)
287 /* only use DMA for bigger than oob size: better performances */
288 if (atmel_nand_dma_op(mtd
, (void *)buf
, len
, 0) == 0)
291 if (host
->board
->bus_width_16
)
292 atmel_write_buf16(mtd
, buf
, len
);
294 atmel_write_buf8(mtd
, buf
, len
);
300 * function called after a write
302 * mtd: MTD block structure
303 * dat: raw data (unused)
304 * ecc_code: buffer for ECC
306 static int atmel_nand_calculate(struct mtd_info
*mtd
,
307 const u_char
*dat
, unsigned char *ecc_code
)
309 struct nand_chip
*nand_chip
= mtd
->priv
;
310 struct atmel_nand_host
*host
= nand_chip
->priv
;
311 unsigned int ecc_value
;
313 /* get the first 2 ECC bytes */
314 ecc_value
= ecc_readl(host
->ecc
, PR
);
316 ecc_code
[0] = ecc_value
& 0xFF;
317 ecc_code
[1] = (ecc_value
>> 8) & 0xFF;
319 /* get the last 2 ECC bytes */
320 ecc_value
= ecc_readl(host
->ecc
, NPR
) & ATMEL_ECC_NPARITY
;
322 ecc_code
[2] = ecc_value
& 0xFF;
323 ecc_code
[3] = (ecc_value
>> 8) & 0xFF;
329 * HW ECC read page function
331 * mtd: mtd info structure
332 * chip: nand chip info structure
333 * buf: buffer to store read data
335 static int atmel_nand_read_page(struct mtd_info
*mtd
,
336 struct nand_chip
*chip
, uint8_t *buf
, int page
)
338 int eccsize
= chip
->ecc
.size
;
339 int eccbytes
= chip
->ecc
.bytes
;
340 uint32_t *eccpos
= chip
->ecc
.layout
->eccpos
;
342 uint8_t *oob
= chip
->oob_poi
;
347 * Errata: ALE is incorrectly wired up to the ECC controller
348 * on the AP7000, so it will include the address cycles in the
351 * Workaround: Reset the parity registers before reading the
354 if (cpu_is_at32ap7000()) {
355 struct atmel_nand_host
*host
= chip
->priv
;
356 ecc_writel(host
->ecc
, CR
, ATMEL_ECC_RST
);
360 chip
->read_buf(mtd
, p
, eccsize
);
362 /* move to ECC position if needed */
363 if (eccpos
[0] != 0) {
364 /* This only works on large pages
365 * because the ECC controller waits for
366 * NAND_CMD_RNDOUTSTART after the
368 * anyway, for small pages, the eccpos[0] == 0
370 chip
->cmdfunc(mtd
, NAND_CMD_RNDOUT
,
371 mtd
->writesize
+ eccpos
[0], -1);
374 /* the ECC controller needs to read the ECC just after the data */
375 ecc_pos
= oob
+ eccpos
[0];
376 chip
->read_buf(mtd
, ecc_pos
, eccbytes
);
378 /* check if there's an error */
379 stat
= chip
->ecc
.correct(mtd
, p
, oob
, NULL
);
382 mtd
->ecc_stats
.failed
++;
384 mtd
->ecc_stats
.corrected
+= stat
;
386 /* get back to oob start (end of page) */
387 chip
->cmdfunc(mtd
, NAND_CMD_RNDOUT
, mtd
->writesize
, -1);
390 chip
->read_buf(mtd
, oob
, mtd
->oobsize
);
398 * function called after a read
400 * mtd: MTD block structure
401 * dat: raw data read from the chip
402 * read_ecc: ECC from the chip (unused)
405 * Detect and correct a 1 bit error for a page
407 static int atmel_nand_correct(struct mtd_info
*mtd
, u_char
*dat
,
408 u_char
*read_ecc
, u_char
*isnull
)
410 struct nand_chip
*nand_chip
= mtd
->priv
;
411 struct atmel_nand_host
*host
= nand_chip
->priv
;
412 unsigned int ecc_status
;
413 unsigned int ecc_word
, ecc_bit
;
415 /* get the status from the Status Register */
416 ecc_status
= ecc_readl(host
->ecc
, SR
);
418 /* if there's no error */
419 if (likely(!(ecc_status
& ATMEL_ECC_RECERR
)))
422 /* get error bit offset (4 bits) */
423 ecc_bit
= ecc_readl(host
->ecc
, PR
) & ATMEL_ECC_BITADDR
;
424 /* get word address (12 bits) */
425 ecc_word
= ecc_readl(host
->ecc
, PR
) & ATMEL_ECC_WORDADDR
;
428 /* if there are multiple errors */
429 if (ecc_status
& ATMEL_ECC_MULERR
) {
430 /* check if it is a freshly erased block
431 * (filled with 0xff) */
432 if ((ecc_bit
== ATMEL_ECC_BITADDR
)
433 && (ecc_word
== (ATMEL_ECC_WORDADDR
>> 4))) {
434 /* the block has just been erased, return OK */
437 /* it doesn't seems to be a freshly
439 * We can't correct so many errors */
440 dev_dbg(host
->dev
, "atmel_nand : multiple errors detected."
441 " Unable to correct.\n");
445 /* if there's a single bit error : we can correct it */
446 if (ecc_status
& ATMEL_ECC_ECCERR
) {
447 /* there's nothing much to do here.
448 * the bit error is on the ECC itself.
450 dev_dbg(host
->dev
, "atmel_nand : one bit error on ECC code."
451 " Nothing to correct\n");
455 dev_dbg(host
->dev
, "atmel_nand : one bit error on data."
456 " (word offset in the page :"
457 " 0x%x bit offset : 0x%x)\n",
459 /* correct the error */
460 if (nand_chip
->options
& NAND_BUSWIDTH_16
) {
462 ((unsigned short *) dat
)[ecc_word
] ^= (1 << ecc_bit
);
465 dat
[ecc_word
] ^= (1 << ecc_bit
);
467 dev_dbg(host
->dev
, "atmel_nand : error corrected\n");
472 * Enable HW ECC : unused on most chips
474 static void atmel_nand_hwctl(struct mtd_info
*mtd
, int mode
)
476 if (cpu_is_at32ap7000()) {
477 struct nand_chip
*nand_chip
= mtd
->priv
;
478 struct atmel_nand_host
*host
= nand_chip
->priv
;
479 ecc_writel(host
->ecc
, CR
, ATMEL_ECC_RST
);
483 #ifdef CONFIG_MTD_CMDLINE_PARTS
484 static const char *part_probes
[] = { "cmdlinepart", NULL
};
488 * Probe for the NAND device.
490 static int __init
atmel_nand_probe(struct platform_device
*pdev
)
492 struct atmel_nand_host
*host
;
493 struct mtd_info
*mtd
;
494 struct nand_chip
*nand_chip
;
495 struct resource
*regs
;
496 struct resource
*mem
;
498 struct mtd_partition
*partitions
= NULL
;
499 int num_partitions
= 0;
501 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
503 printk(KERN_ERR
"atmel_nand: can't get I/O resource mem\n");
507 /* Allocate memory for the device structure (and zero it) */
508 host
= kzalloc(sizeof(struct atmel_nand_host
), GFP_KERNEL
);
510 printk(KERN_ERR
"atmel_nand: failed to allocate device structure.\n");
514 host
->io_phys
= (dma_addr_t
)mem
->start
;
516 host
->io_base
= ioremap(mem
->start
, mem
->end
- mem
->start
+ 1);
517 if (host
->io_base
== NULL
) {
518 printk(KERN_ERR
"atmel_nand: ioremap failed\n");
520 goto err_nand_ioremap
;
524 nand_chip
= &host
->nand_chip
;
525 host
->board
= pdev
->dev
.platform_data
;
526 host
->dev
= &pdev
->dev
;
528 nand_chip
->priv
= host
; /* link the private data structures */
529 mtd
->priv
= nand_chip
;
530 mtd
->owner
= THIS_MODULE
;
532 /* Set address of NAND IO lines */
533 nand_chip
->IO_ADDR_R
= host
->io_base
;
534 nand_chip
->IO_ADDR_W
= host
->io_base
;
535 nand_chip
->cmd_ctrl
= atmel_nand_cmd_ctrl
;
537 if (host
->board
->rdy_pin
)
538 nand_chip
->dev_ready
= atmel_nand_device_ready
;
540 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
541 if (!regs
&& hard_ecc
) {
542 printk(KERN_ERR
"atmel_nand: can't get I/O resource "
543 "regs\nFalling back on software ECC\n");
546 nand_chip
->ecc
.mode
= NAND_ECC_SOFT
; /* enable ECC */
548 nand_chip
->ecc
.mode
= NAND_ECC_NONE
;
549 if (hard_ecc
&& regs
) {
550 host
->ecc
= ioremap(regs
->start
, regs
->end
- regs
->start
+ 1);
551 if (host
->ecc
== NULL
) {
552 printk(KERN_ERR
"atmel_nand: ioremap failed\n");
554 goto err_ecc_ioremap
;
556 nand_chip
->ecc
.mode
= NAND_ECC_HW
;
557 nand_chip
->ecc
.calculate
= atmel_nand_calculate
;
558 nand_chip
->ecc
.correct
= atmel_nand_correct
;
559 nand_chip
->ecc
.hwctl
= atmel_nand_hwctl
;
560 nand_chip
->ecc
.read_page
= atmel_nand_read_page
;
561 nand_chip
->ecc
.bytes
= 4;
564 nand_chip
->chip_delay
= 20; /* 20us command delay time */
566 if (host
->board
->bus_width_16
) /* 16-bit bus width */
567 nand_chip
->options
|= NAND_BUSWIDTH_16
;
569 nand_chip
->read_buf
= atmel_read_buf
;
570 nand_chip
->write_buf
= atmel_write_buf
;
572 platform_set_drvdata(pdev
, host
);
573 atmel_nand_enable(host
);
575 if (host
->board
->det_pin
) {
576 if (gpio_get_value(host
->board
->det_pin
)) {
577 printk(KERN_INFO
"No SmartMedia card inserted.\n");
584 printk(KERN_INFO
"atmel_nand: Use On Flash BBT\n");
585 nand_chip
->options
|= NAND_USE_FLASH_BBT
;
595 dma_cap_set(DMA_MEMCPY
, mask
);
596 host
->dma_chan
= dma_request_channel(mask
, 0, NULL
);
597 if (!host
->dma_chan
) {
598 dev_err(host
->dev
, "Failed to request DMA channel\n");
603 dev_info(host
->dev
, "Using %s for DMA transfers.\n",
604 dma_chan_name(host
->dma_chan
));
606 dev_info(host
->dev
, "No DMA support for NAND access.\n");
608 /* first scan to find the device and get the page size */
609 if (nand_scan_ident(mtd
, 1, NULL
)) {
614 if (nand_chip
->ecc
.mode
== NAND_ECC_HW
) {
615 /* ECC is calculated for the whole page (1 step) */
616 nand_chip
->ecc
.size
= mtd
->writesize
;
618 /* set ECC page size and oob layout */
619 switch (mtd
->writesize
) {
621 nand_chip
->ecc
.layout
= &atmel_oobinfo_small
;
622 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_528
);
625 nand_chip
->ecc
.layout
= &atmel_oobinfo_large
;
626 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_1056
);
629 nand_chip
->ecc
.layout
= &atmel_oobinfo_large
;
630 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_2112
);
633 nand_chip
->ecc
.layout
= &atmel_oobinfo_large
;
634 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_4224
);
637 /* page size not handled by HW ECC */
638 /* switching back to soft ECC */
639 nand_chip
->ecc
.mode
= NAND_ECC_SOFT
;
640 nand_chip
->ecc
.calculate
= NULL
;
641 nand_chip
->ecc
.correct
= NULL
;
642 nand_chip
->ecc
.hwctl
= NULL
;
643 nand_chip
->ecc
.read_page
= NULL
;
644 nand_chip
->ecc
.postpad
= 0;
645 nand_chip
->ecc
.prepad
= 0;
646 nand_chip
->ecc
.bytes
= 0;
651 /* second phase scan */
652 if (nand_scan_tail(mtd
)) {
657 #ifdef CONFIG_MTD_CMDLINE_PARTS
658 mtd
->name
= "atmel_nand";
659 num_partitions
= parse_mtd_partitions(mtd
, part_probes
,
662 if (num_partitions
<= 0 && host
->board
->partition_info
)
663 partitions
= host
->board
->partition_info(mtd
->size
,
666 if ((!partitions
) || (num_partitions
== 0)) {
667 printk(KERN_ERR
"atmel_nand: No partitions defined, or unsupported device.\n");
669 goto err_no_partitions
;
672 res
= mtd_device_register(mtd
, partitions
, num_partitions
);
681 atmel_nand_disable(host
);
682 platform_set_drvdata(pdev
, NULL
);
684 dma_release_channel(host
->dma_chan
);
688 iounmap(host
->io_base
);
695 * Remove a NAND device.
697 static int __exit
atmel_nand_remove(struct platform_device
*pdev
)
699 struct atmel_nand_host
*host
= platform_get_drvdata(pdev
);
700 struct mtd_info
*mtd
= &host
->mtd
;
704 atmel_nand_disable(host
);
710 dma_release_channel(host
->dma_chan
);
712 iounmap(host
->io_base
);
718 static struct platform_driver atmel_nand_driver
= {
719 .remove
= __exit_p(atmel_nand_remove
),
721 .name
= "atmel_nand",
722 .owner
= THIS_MODULE
,
726 static int __init
atmel_nand_init(void)
728 return platform_driver_probe(&atmel_nand_driver
, atmel_nand_probe
);
732 static void __exit
atmel_nand_exit(void)
734 platform_driver_unregister(&atmel_nand_driver
);
738 module_init(atmel_nand_init
);
739 module_exit(atmel_nand_exit
);
741 MODULE_LICENSE("GPL");
742 MODULE_AUTHOR("Rick Bronson");
743 MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
744 MODULE_ALIAS("platform:atmel_nand");