1 /* linux/drivers/mtd/nand/bf5xx_nand.c
3 * Copyright 2006-2008 Analog Devices Inc.
4 * http://blackfin.uclinux.org/
5 * Bryan Wu <bryan.wu@analog.com>
7 * Blackfin BF5xx on-chip NAND flash controller driver
9 * Derived from drivers/mtd/nand/s3c2410.c
10 * Copyright (c) 2007 Ben Dooks <ben@simtec.co.uk>
12 * Derived from drivers/mtd/nand/cafe.c
13 * Copyright © 2006 Red Hat, Inc.
14 * Copyright © 2006 David Woodhouse <dwmw2@infradead.org>
17 * 12-Jun-2007 Bryan Wu: Initial version
18 * 18-Jul-2007 Bryan Wu:
19 * - ECC_HW and ECC_SW supported
20 * - DMA supported in ECC_HW
21 * - YAFFS tested as rootfs in both ECC_HW and ECC_SW
24 * Enable JFFS2 over NAND as rootfs
26 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program; if not, write to the Free Software
38 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
41 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/init.h>
44 #include <linux/kernel.h>
45 #include <linux/string.h>
46 #include <linux/ioport.h>
47 #include <linux/platform_device.h>
48 #include <linux/delay.h>
49 #include <linux/dma-mapping.h>
50 #include <linux/err.h>
51 #include <linux/slab.h>
53 #include <linux/bitops.h>
55 #include <linux/mtd/mtd.h>
56 #include <linux/mtd/nand.h>
57 #include <linux/mtd/nand_ecc.h>
58 #include <linux/mtd/partitions.h>
60 #include <asm/blackfin.h>
62 #include <asm/cacheflush.h>
64 #include <asm/portmux.h>
66 #define DRV_NAME "bf5xx-nand"
67 #define DRV_VERSION "1.2"
68 #define DRV_AUTHOR "Bryan Wu <bryan.wu@analog.com>"
69 #define DRV_DESC "BF5xx on-chip NAND FLash Controller Driver"
71 #ifdef CONFIG_MTD_NAND_BF5XX_HWECC
72 static int hardware_ecc = 1;
74 static int hardware_ecc;
77 static const unsigned short bfin_nfc_pin_req[] =
94 #ifdef CONFIG_MTD_NAND_BF5XX_BOOTROM_ECC
95 static uint8_t bbt_pattern[] = { 0xff };
97 static struct nand_bbt_descr bootrom_bbt = {
101 .pattern = bbt_pattern,
104 static struct nand_ecclayout bootrom_ecclayout = {
107 0x8 * 0, 0x8 * 0 + 1, 0x8 * 0 + 2,
108 0x8 * 1, 0x8 * 1 + 1, 0x8 * 1 + 2,
109 0x8 * 2, 0x8 * 2 + 1, 0x8 * 2 + 2,
110 0x8 * 3, 0x8 * 3 + 1, 0x8 * 3 + 2,
111 0x8 * 4, 0x8 * 4 + 1, 0x8 * 4 + 2,
112 0x8 * 5, 0x8 * 5 + 1, 0x8 * 5 + 2,
113 0x8 * 6, 0x8 * 6 + 1, 0x8 * 6 + 2,
114 0x8 * 7, 0x8 * 7 + 1, 0x8 * 7 + 2
130 * Data structures for bf5xx nand flash controller driver
133 /* bf5xx nand info */
134 struct bf5xx_nand_info {
136 struct nand_hw_control controller;
138 struct nand_chip chip;
141 struct bf5xx_nand_platform *platform;
144 struct device *device;
147 struct completion dma_completion;
151 * Conversion functions
153 static struct bf5xx_nand_info *mtd_to_nand_info(struct mtd_info *mtd)
155 return container_of(mtd, struct bf5xx_nand_info, mtd);
158 static struct bf5xx_nand_info *to_nand_info(struct platform_device *pdev)
160 return platform_get_drvdata(pdev);
163 static struct bf5xx_nand_platform *to_nand_plat(struct platform_device *pdev)
165 return pdev->dev.platform_data;
169 * struct nand_chip interface function pointers
173 * bf5xx_nand_hwcontrol
175 * Issue command and address cycles to the chip
177 static void bf5xx_nand_hwcontrol(struct mtd_info *mtd, int cmd,
180 if (cmd == NAND_CMD_NONE)
183 while (bfin_read_NFC_STAT() & WB_FULL)
187 bfin_write_NFC_CMD(cmd);
189 bfin_write_NFC_ADDR(cmd);
194 * bf5xx_nand_devready()
196 * returns 0 if the nand is busy, 1 if it is ready
198 static int bf5xx_nand_devready(struct mtd_info *mtd)
200 unsigned short val = bfin_read_NFC_IRQSTAT();
202 if ((val & NBUSYIRQ) == NBUSYIRQ)
210 * These allow the bf5xx to use the controller's ECC
211 * generator block to ECC the data as it passes through
215 * ECC error correction function
217 static int bf5xx_nand_correct_data_256(struct mtd_info *mtd, u_char *dat,
218 u_char *read_ecc, u_char *calc_ecc)
220 struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
224 unsigned short failing_bit, failing_byte;
227 calced = calc_ecc[0] | (calc_ecc[1] << 8) | (calc_ecc[2] << 16);
228 stored = read_ecc[0] | (read_ecc[1] << 8) | (read_ecc[2] << 16);
230 syndrome[0] = (calced ^ stored);
233 * syndrome 0: all zero
237 if (!syndrome[0] || !calced || !stored)
241 * sysdrome 0: only one bit is one
242 * ECC data was incorrect
245 if (hweight32(syndrome[0]) == 1) {
246 dev_err(info->device, "ECC data was incorrect!\n");
250 syndrome[1] = (calced & 0x7FF) ^ (stored & 0x7FF);
251 syndrome[2] = (calced & 0x7FF) ^ ((calced >> 11) & 0x7FF);
252 syndrome[3] = (stored & 0x7FF) ^ ((stored >> 11) & 0x7FF);
253 syndrome[4] = syndrome[2] ^ syndrome[3];
255 for (i = 0; i < 5; i++)
256 dev_info(info->device, "syndrome[%d] 0x%08x\n", i, syndrome[i]);
258 dev_info(info->device,
259 "calced[0x%08x], stored[0x%08x]\n",
263 * sysdrome 0: exactly 11 bits are one, each parity
264 * and parity' pair is 1 & 0 or 0 & 1.
265 * 1-bit correctable error
268 if (hweight32(syndrome[0]) == 11 && syndrome[4] == 0x7FF) {
269 dev_info(info->device,
270 "1-bit correctable error, correct it.\n");
271 dev_info(info->device,
272 "syndrome[1] 0x%08x\n", syndrome[1]);
274 failing_bit = syndrome[1] & 0x7;
275 failing_byte = syndrome[1] >> 0x3;
276 data = *(dat + failing_byte);
277 data = data ^ (0x1 << failing_bit);
278 *(dat + failing_byte) = data;
284 * sysdrome 0: random data
285 * More than 1-bit error, non-correctable error
286 * Discard data, mark bad block
288 dev_err(info->device,
289 "More than 1-bit error, non-correctable error.\n");
290 dev_err(info->device,
291 "Please discard data, mark bad block\n");
296 static int bf5xx_nand_correct_data(struct mtd_info *mtd, u_char *dat,
297 u_char *read_ecc, u_char *calc_ecc)
299 struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
300 struct bf5xx_nand_platform *plat = info->platform;
301 unsigned short page_size = (plat->page_size ? 512 : 256);
304 ret = bf5xx_nand_correct_data_256(mtd, dat, read_ecc, calc_ecc);
306 /* If page size is 512, correct second 256 bytes */
307 if (page_size == 512) {
311 ret |= bf5xx_nand_correct_data_256(mtd, dat, read_ecc, calc_ecc);
317 static void bf5xx_nand_enable_hwecc(struct mtd_info *mtd, int mode)
322 static int bf5xx_nand_calculate_ecc(struct mtd_info *mtd,
323 const u_char *dat, u_char *ecc_code)
325 struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
326 struct bf5xx_nand_platform *plat = info->platform;
327 u16 page_size = (plat->page_size ? 512 : 256);
332 /* first 4 bytes ECC code for 256 page size */
333 ecc0 = bfin_read_NFC_ECC0();
334 ecc1 = bfin_read_NFC_ECC1();
336 code[0] = (ecc0 & 0x7ff) | ((ecc1 & 0x7ff) << 11);
338 dev_dbg(info->device, "returning ecc 0x%08x\n", code[0]);
340 /* first 3 bytes in ecc_code for 256 page size */
342 memcpy(ecc_code, p, 3);
344 /* second 4 bytes ECC code for 512 page size */
345 if (page_size == 512) {
346 ecc0 = bfin_read_NFC_ECC2();
347 ecc1 = bfin_read_NFC_ECC3();
348 code[1] = (ecc0 & 0x7ff) | ((ecc1 & 0x7ff) << 11);
350 /* second 3 bytes in ecc_code for second 256
351 * bytes of 512 page size
353 p = (u8 *) (code + 1);
354 memcpy((ecc_code + 3), p, 3);
355 dev_dbg(info->device, "returning ecc 0x%08x\n", code[1]);
362 * PIO mode for buffer writing and reading
364 static void bf5xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
370 * Data reads are requested by first writing to NFC_DATA_RD
371 * and then reading back from NFC_READ.
373 for (i = 0; i < len; i++) {
374 while (bfin_read_NFC_STAT() & WB_FULL)
377 /* Contents do not matter */
378 bfin_write_NFC_DATA_RD(0x0000);
381 while ((bfin_read_NFC_IRQSTAT() & RD_RDY) != RD_RDY)
384 buf[i] = bfin_read_NFC_READ();
386 val = bfin_read_NFC_IRQSTAT();
388 bfin_write_NFC_IRQSTAT(val);
393 static uint8_t bf5xx_nand_read_byte(struct mtd_info *mtd)
397 bf5xx_nand_read_buf(mtd, &val, 1);
402 static void bf5xx_nand_write_buf(struct mtd_info *mtd,
403 const uint8_t *buf, int len)
407 for (i = 0; i < len; i++) {
408 while (bfin_read_NFC_STAT() & WB_FULL)
411 bfin_write_NFC_DATA_WR(buf[i]);
416 static void bf5xx_nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
419 u16 *p = (u16 *) buf;
423 * Data reads are requested by first writing to NFC_DATA_RD
424 * and then reading back from NFC_READ.
426 bfin_write_NFC_DATA_RD(0x5555);
430 for (i = 0; i < len; i++)
431 p[i] = bfin_read_NFC_READ();
434 static void bf5xx_nand_write_buf16(struct mtd_info *mtd,
435 const uint8_t *buf, int len)
438 u16 *p = (u16 *) buf;
441 for (i = 0; i < len; i++)
442 bfin_write_NFC_DATA_WR(p[i]);
448 * DMA functions for buffer writing and reading
450 static irqreturn_t bf5xx_nand_dma_irq(int irq, void *dev_id)
452 struct bf5xx_nand_info *info = dev_id;
454 clear_dma_irqstat(CH_NFC);
456 complete(&info->dma_completion);
461 static void bf5xx_nand_dma_rw(struct mtd_info *mtd,
462 uint8_t *buf, int is_read)
464 struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
465 struct bf5xx_nand_platform *plat = info->platform;
466 unsigned short page_size = (plat->page_size ? 512 : 256);
469 dev_dbg(info->device, " mtd->%p, buf->%p, is_read %d\n",
473 * Before starting a dma transfer, be sure to invalidate/flush
474 * the cache over the address range of your DMA buffer to
475 * prevent cache coherency problems. Otherwise very subtle bugs
476 * can be introduced to your driver.
479 invalidate_dcache_range((unsigned int)buf,
480 (unsigned int)(buf + page_size));
482 flush_dcache_range((unsigned int)buf,
483 (unsigned int)(buf + page_size));
486 * This register must be written before each page is
487 * transferred to generate the correct ECC register
490 bfin_write_NFC_RST(0x1);
494 clear_dma_irqstat(CH_NFC);
496 /* setup DMA register with Blackfin DMA API */
497 set_dma_config(CH_NFC, 0x0);
498 set_dma_start_addr(CH_NFC, (unsigned long) buf);
500 /* The DMAs have different size on BF52x and BF54x */
502 set_dma_x_count(CH_NFC, (page_size >> 1));
503 set_dma_x_modify(CH_NFC, 2);
504 val = DI_EN | WDSIZE_16;
508 set_dma_x_count(CH_NFC, (page_size >> 2));
509 set_dma_x_modify(CH_NFC, 4);
510 val = DI_EN | WDSIZE_32;
512 /* setup write or read operation */
515 set_dma_config(CH_NFC, val);
518 /* Start PAGE read/write operation */
520 bfin_write_NFC_PGCTL(0x1);
522 bfin_write_NFC_PGCTL(0x2);
523 wait_for_completion(&info->dma_completion);
526 static void bf5xx_nand_dma_read_buf(struct mtd_info *mtd,
527 uint8_t *buf, int len)
529 struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
530 struct bf5xx_nand_platform *plat = info->platform;
531 unsigned short page_size = (plat->page_size ? 512 : 256);
533 dev_dbg(info->device, "mtd->%p, buf->%p, int %d\n", mtd, buf, len);
535 if (len == page_size)
536 bf5xx_nand_dma_rw(mtd, buf, 1);
538 bf5xx_nand_read_buf(mtd, buf, len);
541 static void bf5xx_nand_dma_write_buf(struct mtd_info *mtd,
542 const uint8_t *buf, int len)
544 struct bf5xx_nand_info *info = mtd_to_nand_info(mtd);
545 struct bf5xx_nand_platform *plat = info->platform;
546 unsigned short page_size = (plat->page_size ? 512 : 256);
548 dev_dbg(info->device, "mtd->%p, buf->%p, len %d\n", mtd, buf, len);
550 if (len == page_size)
551 bf5xx_nand_dma_rw(mtd, (uint8_t *)buf, 0);
553 bf5xx_nand_write_buf(mtd, buf, len);
557 * System initialization functions
559 static int bf5xx_nand_dma_init(struct bf5xx_nand_info *info)
567 init_completion(&info->dma_completion);
569 /* Request NFC DMA channel */
570 ret = request_dma(CH_NFC, "BF5XX NFC driver");
572 dev_err(info->device, " unable to get DMA channel\n");
577 /* Setup DMAC1 channel mux for NFC which shared with SDH */
578 bfin_write_DMAC1_PERIMUX(bfin_read_DMAC1_PERIMUX() & ~1);
582 set_dma_callback(CH_NFC, bf5xx_nand_dma_irq, info);
584 /* Turn off the DMA channel first */
589 static void bf5xx_nand_dma_remove(struct bf5xx_nand_info *info)
591 /* Free NFC DMA channel */
597 * BF5XX NFC hardware initialization
599 * - clear interrupt status
601 static int bf5xx_nand_hw_init(struct bf5xx_nand_info *info)
605 struct bf5xx_nand_platform *plat = info->platform;
607 /* setup NFC_CTL register */
608 dev_info(info->device,
609 "page_size=%d, data_width=%d, wr_dly=%d, rd_dly=%d\n",
610 (plat->page_size ? 512 : 256),
611 (plat->data_width ? 16 : 8),
612 plat->wr_dly, plat->rd_dly);
614 val = (plat->page_size << NFC_PG_SIZE_OFFSET) |
615 (plat->data_width << NFC_NWIDTH_OFFSET) |
616 (plat->rd_dly << NFC_RDDLY_OFFSET) |
617 (plat->rd_dly << NFC_WRDLY_OFFSET);
618 dev_dbg(info->device, "NFC_CTL is 0x%04x\n", val);
620 bfin_write_NFC_CTL(val);
623 /* clear interrupt status */
624 bfin_write_NFC_IRQMASK(0x0);
626 val = bfin_read_NFC_IRQSTAT();
627 bfin_write_NFC_IRQSTAT(val);
630 /* DMA initialization */
631 if (bf5xx_nand_dma_init(info))
638 * Device management interface
640 static int __devinit bf5xx_nand_add_partition(struct bf5xx_nand_info *info)
642 struct mtd_info *mtd = &info->mtd;
644 #ifdef CONFIG_MTD_PARTITIONS
645 struct mtd_partition *parts = info->platform->partitions;
646 int nr = info->platform->nr_partitions;
648 return add_mtd_partitions(mtd, parts, nr);
650 return add_mtd_device(mtd);
654 static int __devexit bf5xx_nand_remove(struct platform_device *pdev)
656 struct bf5xx_nand_info *info = to_nand_info(pdev);
657 struct mtd_info *mtd = NULL;
659 platform_set_drvdata(pdev, NULL);
661 /* first thing we need to do is release all our mtds
662 * and their partitions, then go through freeing the
671 peripheral_free_list(bfin_nfc_pin_req);
672 bf5xx_nand_dma_remove(info);
674 /* free the common resources */
683 * called by device layer when it finds a device matching
684 * one our driver can handled. This code checks to see if
685 * it can allocate all necessary resources then calls the
686 * nand layer to look for devices
688 static int __devinit bf5xx_nand_probe(struct platform_device *pdev)
690 struct bf5xx_nand_platform *plat = to_nand_plat(pdev);
691 struct bf5xx_nand_info *info = NULL;
692 struct nand_chip *chip = NULL;
693 struct mtd_info *mtd = NULL;
696 dev_dbg(&pdev->dev, "(%p)\n", pdev);
699 dev_err(&pdev->dev, "no platform specific information\n");
703 if (peripheral_request_list(bfin_nfc_pin_req, DRV_NAME)) {
704 dev_err(&pdev->dev, "requesting Peripherals failed\n");
708 info = kzalloc(sizeof(*info), GFP_KERNEL);
710 dev_err(&pdev->dev, "no memory for flash info\n");
712 goto out_err_kzalloc;
715 platform_set_drvdata(pdev, info);
717 spin_lock_init(&info->controller.lock);
718 init_waitqueue_head(&info->controller.wq);
720 info->device = &pdev->dev;
721 info->platform = plat;
723 /* initialise chip data struct */
726 if (plat->data_width)
727 chip->options |= NAND_BUSWIDTH_16;
729 chip->options |= NAND_CACHEPRG | NAND_SKIP_BBTSCAN;
731 chip->read_buf = (plat->data_width) ?
732 bf5xx_nand_read_buf16 : bf5xx_nand_read_buf;
733 chip->write_buf = (plat->data_width) ?
734 bf5xx_nand_write_buf16 : bf5xx_nand_write_buf;
736 chip->read_byte = bf5xx_nand_read_byte;
738 chip->cmd_ctrl = bf5xx_nand_hwcontrol;
739 chip->dev_ready = bf5xx_nand_devready;
741 chip->priv = &info->mtd;
742 chip->controller = &info->controller;
744 chip->IO_ADDR_R = (void __iomem *) NFC_READ;
745 chip->IO_ADDR_W = (void __iomem *) NFC_DATA_WR;
747 chip->chip_delay = 0;
749 /* initialise mtd info data struct */
752 mtd->owner = THIS_MODULE;
754 /* initialise the hardware */
755 err = bf5xx_nand_hw_init(info);
757 goto out_err_hw_init;
759 /* setup hardware ECC data struct */
761 #ifdef CONFIG_MTD_NAND_BF5XX_BOOTROM_ECC
762 chip->badblock_pattern = &bootrom_bbt;
763 chip->ecc.layout = &bootrom_ecclayout;
766 if (plat->page_size == NFC_PG_SIZE_256) {
768 chip->ecc.size = 256;
769 } else if (plat->page_size == NFC_PG_SIZE_512) {
771 chip->ecc.size = 512;
774 chip->read_buf = bf5xx_nand_dma_read_buf;
775 chip->write_buf = bf5xx_nand_dma_write_buf;
776 chip->ecc.calculate = bf5xx_nand_calculate_ecc;
777 chip->ecc.correct = bf5xx_nand_correct_data;
778 chip->ecc.mode = NAND_ECC_HW;
779 chip->ecc.hwctl = bf5xx_nand_enable_hwecc;
781 chip->ecc.mode = NAND_ECC_SOFT;
784 /* scan hardware nand chip and setup mtd info data struct */
785 if (nand_scan(mtd, 1)) {
787 goto out_err_nand_scan;
790 /* add NAND partition */
791 bf5xx_nand_add_partition(info);
793 dev_dbg(&pdev->dev, "initialised ok\n");
797 bf5xx_nand_dma_remove(info);
799 platform_set_drvdata(pdev, NULL);
802 peripheral_free_list(bfin_nfc_pin_req);
810 static int bf5xx_nand_suspend(struct platform_device *dev, pm_message_t pm)
812 struct bf5xx_nand_info *info = platform_get_drvdata(dev);
817 static int bf5xx_nand_resume(struct platform_device *dev)
819 struct bf5xx_nand_info *info = platform_get_drvdata(dev);
825 #define bf5xx_nand_suspend NULL
826 #define bf5xx_nand_resume NULL
829 /* driver device registration */
830 static struct platform_driver bf5xx_nand_driver = {
831 .probe = bf5xx_nand_probe,
832 .remove = __devexit_p(bf5xx_nand_remove),
833 .suspend = bf5xx_nand_suspend,
834 .resume = bf5xx_nand_resume,
837 .owner = THIS_MODULE,
841 static int __init bf5xx_nand_init(void)
843 printk(KERN_INFO "%s, Version %s (c) 2007 Analog Devices, Inc.\n",
844 DRV_DESC, DRV_VERSION);
846 return platform_driver_register(&bf5xx_nand_driver);
849 static void __exit bf5xx_nand_exit(void)
851 platform_driver_unregister(&bf5xx_nand_driver);
854 module_init(bf5xx_nand_init);
855 module_exit(bf5xx_nand_exit);
857 MODULE_LICENSE("GPL");
858 MODULE_AUTHOR(DRV_AUTHOR);
859 MODULE_DESCRIPTION(DRV_DESC);
860 MODULE_ALIAS("platform:" DRV_NAME);