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/dma-mapping.h>
26 #include <linux/slab.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.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>
34 #include <linux/dmaengine.h>
35 #include <linux/gpio.h>
38 #include <mach/board.h>
41 #ifdef CONFIG_MTD_NAND_ATMEL_ECC_HW
47 #ifdef CONFIG_MTD_NAND_ATMEL_ECC_NONE
53 static int use_dma
= 1;
54 module_param(use_dma
, int, 0);
56 static int on_flash_bbt
= 0;
57 module_param(on_flash_bbt
, int, 0);
59 /* Register access macros */
60 #define ecc_readl(add, reg) \
61 __raw_readl(add + ATMEL_ECC_##reg)
62 #define ecc_writel(add, reg, value) \
63 __raw_writel((value), add + ATMEL_ECC_##reg)
65 #include "atmel_nand_ecc.h" /* Hardware ECC registers */
67 /* oob layout for large page size
68 * bad block info is on bytes 0 and 1
69 * the bytes have to be consecutives to avoid
70 * several NAND_CMD_RNDOUT during read
72 static struct nand_ecclayout atmel_oobinfo_large
= {
74 .eccpos
= {60, 61, 62, 63},
80 /* oob layout for small page size
81 * bad block info is on bytes 4 and 5
82 * the bytes have to be consecutives to avoid
83 * several NAND_CMD_RNDOUT during read
85 static struct nand_ecclayout atmel_oobinfo_small
= {
87 .eccpos
= {0, 1, 2, 3},
93 struct atmel_nand_host
{
94 struct nand_chip nand_chip
;
96 void __iomem
*io_base
;
98 struct atmel_nand_data
*board
;
102 struct completion comp
;
103 struct dma_chan
*dma_chan
;
106 static int cpu_has_dma(void)
108 return cpu_is_at91sam9rl() || cpu_is_at91sam9g45();
114 static void atmel_nand_enable(struct atmel_nand_host
*host
)
116 if (host
->board
->enable_pin
)
117 gpio_set_value(host
->board
->enable_pin
, 0);
123 static void atmel_nand_disable(struct atmel_nand_host
*host
)
125 if (host
->board
->enable_pin
)
126 gpio_set_value(host
->board
->enable_pin
, 1);
130 * Hardware specific access to control-lines
132 static void atmel_nand_cmd_ctrl(struct mtd_info
*mtd
, int cmd
, unsigned int ctrl
)
134 struct nand_chip
*nand_chip
= mtd
->priv
;
135 struct atmel_nand_host
*host
= nand_chip
->priv
;
137 if (ctrl
& NAND_CTRL_CHANGE
) {
139 atmel_nand_enable(host
);
141 atmel_nand_disable(host
);
143 if (cmd
== NAND_CMD_NONE
)
147 writeb(cmd
, host
->io_base
+ (1 << host
->board
->cle
));
149 writeb(cmd
, host
->io_base
+ (1 << host
->board
->ale
));
153 * Read the Device Ready pin.
155 static int atmel_nand_device_ready(struct mtd_info
*mtd
)
157 struct nand_chip
*nand_chip
= mtd
->priv
;
158 struct atmel_nand_host
*host
= nand_chip
->priv
;
160 return gpio_get_value(host
->board
->rdy_pin
) ^
161 !!host
->board
->rdy_pin_active_low
;
164 static void dma_complete_func(void *completion
)
166 complete(completion
);
169 static int atmel_nand_dma_op(struct mtd_info
*mtd
, void *buf
, int len
,
172 struct dma_device
*dma_dev
;
173 enum dma_ctrl_flags flags
;
174 dma_addr_t dma_src_addr
, dma_dst_addr
, phys_addr
;
175 struct dma_async_tx_descriptor
*tx
= NULL
;
177 struct nand_chip
*chip
= mtd
->priv
;
178 struct atmel_nand_host
*host
= chip
->priv
;
181 enum dma_data_direction dir
= is_read
? DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
183 if (buf
>= high_memory
)
186 dma_dev
= host
->dma_chan
->device
;
188 flags
= DMA_CTRL_ACK
| DMA_PREP_INTERRUPT
| DMA_COMPL_SKIP_SRC_UNMAP
|
189 DMA_COMPL_SKIP_DEST_UNMAP
;
191 phys_addr
= dma_map_single(dma_dev
->dev
, p
, len
, dir
);
192 if (dma_mapping_error(dma_dev
->dev
, phys_addr
)) {
193 dev_err(host
->dev
, "Failed to dma_map_single\n");
198 dma_src_addr
= host
->io_phys
;
199 dma_dst_addr
= phys_addr
;
201 dma_src_addr
= phys_addr
;
202 dma_dst_addr
= host
->io_phys
;
205 tx
= dma_dev
->device_prep_dma_memcpy(host
->dma_chan
, dma_dst_addr
,
206 dma_src_addr
, len
, flags
);
208 dev_err(host
->dev
, "Failed to prepare DMA memcpy\n");
212 init_completion(&host
->comp
);
213 tx
->callback
= dma_complete_func
;
214 tx
->callback_param
= &host
->comp
;
216 cookie
= tx
->tx_submit(tx
);
217 if (dma_submit_error(cookie
)) {
218 dev_err(host
->dev
, "Failed to do DMA tx_submit\n");
222 dma_async_issue_pending(host
->dma_chan
);
223 wait_for_completion(&host
->comp
);
228 dma_unmap_single(dma_dev
->dev
, phys_addr
, len
, dir
);
231 dev_warn(host
->dev
, "Fall back to CPU I/O\n");
235 static void atmel_read_buf(struct mtd_info
*mtd
, u8
*buf
, int len
)
237 struct nand_chip
*chip
= mtd
->priv
;
239 if (use_dma
&& len
> mtd
->oobsize
)
240 /* only use DMA for bigger than oob size: better performances */
241 if (atmel_nand_dma_op(mtd
, buf
, len
, 1) == 0)
244 /* if no DMA operation possible, use PIO */
245 memcpy_fromio(buf
, chip
->IO_ADDR_R
, len
);
248 static void atmel_write_buf(struct mtd_info
*mtd
, const u8
*buf
, int len
)
250 struct nand_chip
*chip
= mtd
->priv
;
252 if (use_dma
&& len
> mtd
->oobsize
)
253 /* only use DMA for bigger than oob size: better performances */
254 if (atmel_nand_dma_op(mtd
, (void *)buf
, len
, 0) == 0)
257 /* if no DMA operation possible, use PIO */
258 memcpy_toio(chip
->IO_ADDR_W
, buf
, len
);
264 * function called after a write
266 * mtd: MTD block structure
267 * dat: raw data (unused)
268 * ecc_code: buffer for ECC
270 static int atmel_nand_calculate(struct mtd_info
*mtd
,
271 const u_char
*dat
, unsigned char *ecc_code
)
273 struct nand_chip
*nand_chip
= mtd
->priv
;
274 struct atmel_nand_host
*host
= nand_chip
->priv
;
275 unsigned int ecc_value
;
277 /* get the first 2 ECC bytes */
278 ecc_value
= ecc_readl(host
->ecc
, PR
);
280 ecc_code
[0] = ecc_value
& 0xFF;
281 ecc_code
[1] = (ecc_value
>> 8) & 0xFF;
283 /* get the last 2 ECC bytes */
284 ecc_value
= ecc_readl(host
->ecc
, NPR
) & ATMEL_ECC_NPARITY
;
286 ecc_code
[2] = ecc_value
& 0xFF;
287 ecc_code
[3] = (ecc_value
>> 8) & 0xFF;
293 * HW ECC read page function
295 * mtd: mtd info structure
296 * chip: nand chip info structure
297 * buf: buffer to store read data
299 static int atmel_nand_read_page(struct mtd_info
*mtd
,
300 struct nand_chip
*chip
, uint8_t *buf
, int page
)
302 int eccsize
= chip
->ecc
.size
;
303 int eccbytes
= chip
->ecc
.bytes
;
304 uint32_t *eccpos
= chip
->ecc
.layout
->eccpos
;
306 uint8_t *oob
= chip
->oob_poi
;
311 * Errata: ALE is incorrectly wired up to the ECC controller
312 * on the AP7000, so it will include the address cycles in the
315 * Workaround: Reset the parity registers before reading the
318 if (cpu_is_at32ap7000()) {
319 struct atmel_nand_host
*host
= chip
->priv
;
320 ecc_writel(host
->ecc
, CR
, ATMEL_ECC_RST
);
324 chip
->read_buf(mtd
, p
, eccsize
);
326 /* move to ECC position if needed */
327 if (eccpos
[0] != 0) {
328 /* This only works on large pages
329 * because the ECC controller waits for
330 * NAND_CMD_RNDOUTSTART after the
332 * anyway, for small pages, the eccpos[0] == 0
334 chip
->cmdfunc(mtd
, NAND_CMD_RNDOUT
,
335 mtd
->writesize
+ eccpos
[0], -1);
338 /* the ECC controller needs to read the ECC just after the data */
339 ecc_pos
= oob
+ eccpos
[0];
340 chip
->read_buf(mtd
, ecc_pos
, eccbytes
);
342 /* check if there's an error */
343 stat
= chip
->ecc
.correct(mtd
, p
, oob
, NULL
);
346 mtd
->ecc_stats
.failed
++;
348 mtd
->ecc_stats
.corrected
+= stat
;
350 /* get back to oob start (end of page) */
351 chip
->cmdfunc(mtd
, NAND_CMD_RNDOUT
, mtd
->writesize
, -1);
354 chip
->read_buf(mtd
, oob
, mtd
->oobsize
);
362 * function called after a read
364 * mtd: MTD block structure
365 * dat: raw data read from the chip
366 * read_ecc: ECC from the chip (unused)
369 * Detect and correct a 1 bit error for a page
371 static int atmel_nand_correct(struct mtd_info
*mtd
, u_char
*dat
,
372 u_char
*read_ecc
, u_char
*isnull
)
374 struct nand_chip
*nand_chip
= mtd
->priv
;
375 struct atmel_nand_host
*host
= nand_chip
->priv
;
376 unsigned int ecc_status
;
377 unsigned int ecc_word
, ecc_bit
;
379 /* get the status from the Status Register */
380 ecc_status
= ecc_readl(host
->ecc
, SR
);
382 /* if there's no error */
383 if (likely(!(ecc_status
& ATMEL_ECC_RECERR
)))
386 /* get error bit offset (4 bits) */
387 ecc_bit
= ecc_readl(host
->ecc
, PR
) & ATMEL_ECC_BITADDR
;
388 /* get word address (12 bits) */
389 ecc_word
= ecc_readl(host
->ecc
, PR
) & ATMEL_ECC_WORDADDR
;
392 /* if there are multiple errors */
393 if (ecc_status
& ATMEL_ECC_MULERR
) {
394 /* check if it is a freshly erased block
395 * (filled with 0xff) */
396 if ((ecc_bit
== ATMEL_ECC_BITADDR
)
397 && (ecc_word
== (ATMEL_ECC_WORDADDR
>> 4))) {
398 /* the block has just been erased, return OK */
401 /* it doesn't seems to be a freshly
403 * We can't correct so many errors */
404 dev_dbg(host
->dev
, "atmel_nand : multiple errors detected."
405 " Unable to correct.\n");
409 /* if there's a single bit error : we can correct it */
410 if (ecc_status
& ATMEL_ECC_ECCERR
) {
411 /* there's nothing much to do here.
412 * the bit error is on the ECC itself.
414 dev_dbg(host
->dev
, "atmel_nand : one bit error on ECC code."
415 " Nothing to correct\n");
419 dev_dbg(host
->dev
, "atmel_nand : one bit error on data."
420 " (word offset in the page :"
421 " 0x%x bit offset : 0x%x)\n",
423 /* correct the error */
424 if (nand_chip
->options
& NAND_BUSWIDTH_16
) {
426 ((unsigned short *) dat
)[ecc_word
] ^= (1 << ecc_bit
);
429 dat
[ecc_word
] ^= (1 << ecc_bit
);
431 dev_dbg(host
->dev
, "atmel_nand : error corrected\n");
436 * Enable HW ECC : unused on most chips
438 static void atmel_nand_hwctl(struct mtd_info
*mtd
, int mode
)
440 if (cpu_is_at32ap7000()) {
441 struct nand_chip
*nand_chip
= mtd
->priv
;
442 struct atmel_nand_host
*host
= nand_chip
->priv
;
443 ecc_writel(host
->ecc
, CR
, ATMEL_ECC_RST
);
448 * Probe for the NAND device.
450 static int __init
atmel_nand_probe(struct platform_device
*pdev
)
452 struct atmel_nand_host
*host
;
453 struct mtd_info
*mtd
;
454 struct nand_chip
*nand_chip
;
455 struct resource
*regs
;
456 struct resource
*mem
;
459 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
461 printk(KERN_ERR
"atmel_nand: can't get I/O resource mem\n");
465 /* Allocate memory for the device structure (and zero it) */
466 host
= kzalloc(sizeof(struct atmel_nand_host
), GFP_KERNEL
);
468 printk(KERN_ERR
"atmel_nand: failed to allocate device structure.\n");
472 host
->io_phys
= (dma_addr_t
)mem
->start
;
474 host
->io_base
= ioremap(mem
->start
, resource_size(mem
));
475 if (host
->io_base
== NULL
) {
476 printk(KERN_ERR
"atmel_nand: ioremap failed\n");
478 goto err_nand_ioremap
;
482 nand_chip
= &host
->nand_chip
;
483 host
->board
= pdev
->dev
.platform_data
;
484 host
->dev
= &pdev
->dev
;
486 nand_chip
->priv
= host
; /* link the private data structures */
487 mtd
->priv
= nand_chip
;
488 mtd
->owner
= THIS_MODULE
;
490 /* Set address of NAND IO lines */
491 nand_chip
->IO_ADDR_R
= host
->io_base
;
492 nand_chip
->IO_ADDR_W
= host
->io_base
;
493 nand_chip
->cmd_ctrl
= atmel_nand_cmd_ctrl
;
495 if (host
->board
->rdy_pin
)
496 nand_chip
->dev_ready
= atmel_nand_device_ready
;
498 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
499 if (!regs
&& hard_ecc
) {
500 printk(KERN_ERR
"atmel_nand: can't get I/O resource "
501 "regs\nFalling back on software ECC\n");
504 nand_chip
->ecc
.mode
= NAND_ECC_SOFT
; /* enable ECC */
506 nand_chip
->ecc
.mode
= NAND_ECC_NONE
;
507 if (hard_ecc
&& regs
) {
508 host
->ecc
= ioremap(regs
->start
, resource_size(regs
));
509 if (host
->ecc
== NULL
) {
510 printk(KERN_ERR
"atmel_nand: ioremap failed\n");
512 goto err_ecc_ioremap
;
514 nand_chip
->ecc
.mode
= NAND_ECC_HW
;
515 nand_chip
->ecc
.calculate
= atmel_nand_calculate
;
516 nand_chip
->ecc
.correct
= atmel_nand_correct
;
517 nand_chip
->ecc
.hwctl
= atmel_nand_hwctl
;
518 nand_chip
->ecc
.read_page
= atmel_nand_read_page
;
519 nand_chip
->ecc
.bytes
= 4;
522 nand_chip
->chip_delay
= 20; /* 20us command delay time */
524 if (host
->board
->bus_width_16
) /* 16-bit bus width */
525 nand_chip
->options
|= NAND_BUSWIDTH_16
;
527 nand_chip
->read_buf
= atmel_read_buf
;
528 nand_chip
->write_buf
= atmel_write_buf
;
530 platform_set_drvdata(pdev
, host
);
531 atmel_nand_enable(host
);
533 if (host
->board
->det_pin
) {
534 if (gpio_get_value(host
->board
->det_pin
)) {
535 printk(KERN_INFO
"No SmartMedia card inserted.\n");
542 printk(KERN_INFO
"atmel_nand: Use On Flash BBT\n");
543 nand_chip
->bbt_options
|= NAND_BBT_USE_FLASH
;
553 dma_cap_set(DMA_MEMCPY
, mask
);
554 host
->dma_chan
= dma_request_channel(mask
, NULL
, NULL
);
555 if (!host
->dma_chan
) {
556 dev_err(host
->dev
, "Failed to request DMA channel\n");
561 dev_info(host
->dev
, "Using %s for DMA transfers.\n",
562 dma_chan_name(host
->dma_chan
));
564 dev_info(host
->dev
, "No DMA support for NAND access.\n");
566 /* first scan to find the device and get the page size */
567 if (nand_scan_ident(mtd
, 1, NULL
)) {
572 if (nand_chip
->ecc
.mode
== NAND_ECC_HW
) {
573 /* ECC is calculated for the whole page (1 step) */
574 nand_chip
->ecc
.size
= mtd
->writesize
;
576 /* set ECC page size and oob layout */
577 switch (mtd
->writesize
) {
579 nand_chip
->ecc
.layout
= &atmel_oobinfo_small
;
580 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_528
);
583 nand_chip
->ecc
.layout
= &atmel_oobinfo_large
;
584 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_1056
);
587 nand_chip
->ecc
.layout
= &atmel_oobinfo_large
;
588 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_2112
);
591 nand_chip
->ecc
.layout
= &atmel_oobinfo_large
;
592 ecc_writel(host
->ecc
, MR
, ATMEL_ECC_PAGESIZE_4224
);
595 /* page size not handled by HW ECC */
596 /* switching back to soft ECC */
597 nand_chip
->ecc
.mode
= NAND_ECC_SOFT
;
598 nand_chip
->ecc
.calculate
= NULL
;
599 nand_chip
->ecc
.correct
= NULL
;
600 nand_chip
->ecc
.hwctl
= NULL
;
601 nand_chip
->ecc
.read_page
= NULL
;
602 nand_chip
->ecc
.postpad
= 0;
603 nand_chip
->ecc
.prepad
= 0;
604 nand_chip
->ecc
.bytes
= 0;
609 /* second phase scan */
610 if (nand_scan_tail(mtd
)) {
615 mtd
->name
= "atmel_nand";
616 res
= mtd_device_parse_register(mtd
, NULL
, 0,
617 host
->board
->parts
, host
->board
->num_parts
);
624 atmel_nand_disable(host
);
625 platform_set_drvdata(pdev
, NULL
);
627 dma_release_channel(host
->dma_chan
);
631 iounmap(host
->io_base
);
638 * Remove a NAND device.
640 static int __exit
atmel_nand_remove(struct platform_device
*pdev
)
642 struct atmel_nand_host
*host
= platform_get_drvdata(pdev
);
643 struct mtd_info
*mtd
= &host
->mtd
;
647 atmel_nand_disable(host
);
653 dma_release_channel(host
->dma_chan
);
655 iounmap(host
->io_base
);
661 static struct platform_driver atmel_nand_driver
= {
662 .remove
= __exit_p(atmel_nand_remove
),
664 .name
= "atmel_nand",
665 .owner
= THIS_MODULE
,
669 static int __init
atmel_nand_init(void)
671 return platform_driver_probe(&atmel_nand_driver
, atmel_nand_probe
);
675 static void __exit
atmel_nand_exit(void)
677 platform_driver_unregister(&atmel_nand_driver
);
681 module_init(atmel_nand_init
);
682 module_exit(atmel_nand_exit
);
684 MODULE_LICENSE("GPL");
685 MODULE_AUTHOR("Rick Bronson");
686 MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
687 MODULE_ALIAS("platform:atmel_nand");