38 #include <linux/module.h>
39 #include <linux/types.h>
41 #include <linux/kernel.h>
42 #include <linux/string.h>
48 #include <linux/slab.h>
50 #include <linux/bitops.h>
59 #include <asm/cacheflush.h>
63 #define DRV_NAME "bf5xx-nand"
64 #define DRV_VERSION "1.2"
66 #define DRV_DESC "BF5xx on-chip NAND FLash Controller Driver"
71 #define PG_WR_STAT 0x04
72 #define PG_RD_STAT 0x08
86 #define PG_RD_START 0x01
87 #define PG_WR_START 0x02
89 #ifdef CONFIG_MTD_NAND_BF5XX_HWECC
90 static int hardware_ecc = 1;
92 static int hardware_ecc;
95 static const unsigned short bfin_nfc_pin_req[] =
112 #ifdef CONFIG_MTD_NAND_BF5XX_BOOTROM_ECC
116 0x8 * 0, 0x8 * 0 + 1, 0x8 * 0 + 2,
117 0x8 * 1, 0x8 * 1 + 1, 0x8 * 1 + 2,
118 0x8 * 2, 0x8 * 2 + 1, 0x8 * 2 + 2,
119 0x8 * 3, 0x8 * 3 + 1, 0x8 * 3 + 2,
120 0x8 * 4, 0x8 * 4 + 1, 0x8 * 4 + 2,
121 0x8 * 5, 0x8 * 5 + 1, 0x8 * 5 + 2,
122 0x8 * 6, 0x8 * 6 + 1, 0x8 * 6 + 2,
123 0x8 * 7, 0x8 * 7 + 1, 0x8 * 7 + 2
169 return platform_get_drvdata(pdev);
174 return pdev->
dev.platform_data;
186 static void bf5xx_nand_hwcontrol(
struct mtd_info *mtd,
int cmd,
207 static int bf5xx_nand_devready(
struct mtd_info *mtd)
211 if ((val &
NBUSY) == NBUSY)
233 unsigned short failing_bit, failing_byte;
236 calced = calc_ecc[0] | (calc_ecc[1] << 8) | (calc_ecc[2] << 16);
237 stored = read_ecc[0] | (read_ecc[1] << 8) | (read_ecc[2] << 16);
239 syndrome[0] = (calced ^ stored);
246 if (!syndrome[0] || !calced || !stored)
259 syndrome[1] = (calced & 0x7FF) ^ (stored & 0x7FF);
260 syndrome[2] = (calced & 0x7FF) ^ ((calced >> 11) & 0x7FF);
261 syndrome[3] = (stored & 0x7FF) ^ ((stored >> 11) & 0x7FF);
262 syndrome[4] = syndrome[2] ^ syndrome[3];
264 for (i = 0; i < 5; i++)
268 "calced[0x%08x], stored[0x%08x]\n",
277 if (
hweight32(syndrome[0]) == 11 && syndrome[4] == 0x7FF) {
279 "1-bit correctable error, correct it.\n");
281 "syndrome[1] 0x%08x\n", syndrome[1]);
283 failing_bit = syndrome[1] & 0x7;
284 failing_byte = syndrome[1] >> 0x3;
285 data = *(dat + failing_byte);
286 data = data ^ (0x1 << failing_bit);
287 *(dat + failing_byte) = data;
298 "More than 1-bit error, non-correctable error.\n");
300 "Please discard data, mark bad block\n");
305 static int bf5xx_nand_correct_data(
struct mtd_info *mtd,
u_char *dat,
311 ret = bf5xx_nand_correct_data_256(mtd, dat, read_ecc, calc_ecc);
314 if (chip->
ecc.size == 512) {
318 ret |= bf5xx_nand_correct_data_256(mtd, dat, read_ecc, calc_ecc);
324 static void bf5xx_nand_enable_hwecc(
struct mtd_info *mtd,
int mode)
329 static int bf5xx_nand_calculate_ecc(
struct mtd_info *mtd,
342 code[0] = (ecc0 & 0x7ff) | ((ecc1 & 0x7ff) << 11);
350 if (chip->
ecc.size == 512) {
353 code[1] = (ecc0 & 0x7ff) | ((ecc1 & 0x7ff) << 11);
358 p = (
u8 *) (code + 1);
359 memcpy((ecc_code + 3), p, 3);
378 for (i = 0; i < len; i++) {
402 bf5xx_nand_read_buf(mtd, &val, 1);
407 static void bf5xx_nand_write_buf(
struct mtd_info *mtd,
412 for (i = 0; i < len; i++) {
421 static void bf5xx_nand_read_buf16(
struct mtd_info *mtd,
uint8_t *buf,
int len)
435 for (i = 0; i < len; i++)
439 static void bf5xx_nand_write_buf16(
struct mtd_info *mtd,
446 for (i = 0; i < len; i++)
459 clear_dma_irqstat(
CH_NFC);
466 static void bf5xx_nand_dma_rw(
struct mtd_info *mtd,
484 (
unsigned int)(buf + chip->
ecc.size));
487 (
unsigned int)(buf + chip->
ecc.size));
500 clear_dma_irqstat(
CH_NFC);
503 set_dma_config(
CH_NFC, 0x0);
504 set_dma_start_addr(
CH_NFC, (
unsigned long) buf);
508 set_dma_x_count(
CH_NFC, (chip->
ecc.size >> 1));
509 set_dma_x_modify(
CH_NFC, 2);
514 set_dma_x_count(
CH_NFC, (chip->
ecc.size >> 2));
515 set_dma_x_modify(
CH_NFC, 4);
521 set_dma_config(
CH_NFC, val);
532 static void bf5xx_nand_dma_read_buf(
struct mtd_info *mtd,
538 dev_dbg(info->
device,
"mtd->%p, buf->%p, int %d\n", mtd, buf, len);
540 if (len == chip->
ecc.size)
541 bf5xx_nand_dma_rw(mtd, buf, 1);
543 bf5xx_nand_read_buf(mtd, buf, len);
546 static void bf5xx_nand_dma_write_buf(
struct mtd_info *mtd,
552 dev_dbg(info->
device,
"mtd->%p, buf->%p, len %d\n", mtd, buf, len);
554 if (len == chip->
ecc.size)
555 bf5xx_nand_dma_rw(mtd, (
uint8_t *)buf, 0);
557 bf5xx_nand_write_buf(mtd, buf, len);
560 static int bf5xx_nand_read_page_raw(
struct mtd_info *mtd,
struct nand_chip *chip,
563 bf5xx_nand_read_buf(mtd, buf, mtd->
writesize);
569 static int bf5xx_nand_write_page_raw(
struct mtd_info *mtd,
572 bf5xx_nand_write_buf(mtd, buf, mtd->
writesize);
631 "data_width=%d, wr_dly=%d, rd_dly=%d\n",
652 if (bf5xx_nand_dma_init(info))
674 platform_set_drvdata(pdev,
NULL);
683 bf5xx_nand_dma_remove(info);
691 static int bf5xx_nand_scan(
struct mtd_info *mtd)
705 chip->
ecc.size = 512;
707 chip->
ecc.strength = 2;
709 chip->
ecc.size = 256;
711 chip->
ecc.strength = 1;
739 dev_err(&pdev->
dev,
"no platform specific information\n");
744 dev_err(&pdev->
dev,
"requesting Peripherals failed\n");
750 dev_err(&pdev->
dev,
"no memory for flash info\n");
752 goto out_err_kzalloc;
755 platform_set_drvdata(pdev, info);
772 bf5xx_nand_read_buf16 : bf5xx_nand_read_buf;
774 bf5xx_nand_write_buf16 : bf5xx_nand_write_buf;
778 chip->
cmd_ctrl = bf5xx_nand_hwcontrol;
795 err = bf5xx_nand_hw_init(info);
797 goto out_err_hw_init;
801 #ifdef CONFIG_MTD_NAND_BF5XX_BOOTROM_ECC
802 chip->
ecc.layout = &bootrom_ecclayout;
804 chip->
read_buf = bf5xx_nand_dma_read_buf;
805 chip->
write_buf = bf5xx_nand_dma_write_buf;
806 chip->
ecc.calculate = bf5xx_nand_calculate_ecc;
807 chip->
ecc.correct = bf5xx_nand_correct_data;
809 chip->
ecc.hwctl = bf5xx_nand_enable_hwecc;
810 chip->
ecc.read_page_raw = bf5xx_nand_read_page_raw;
811 chip->
ecc.write_page_raw = bf5xx_nand_write_page_raw;
817 if (bf5xx_nand_scan(mtd)) {
819 goto out_err_nand_scan;
822 #ifdef CONFIG_MTD_NAND_BF5XX_BOOTROM_ECC
827 bf5xx_nand_add_partition(info);
833 bf5xx_nand_dma_remove(info);
835 platform_set_drvdata(pdev,
NULL);
861 #define bf5xx_nand_suspend NULL
862 #define bf5xx_nand_resume NULL
867 .probe = bf5xx_nand_probe,
877 static int __init bf5xx_nand_init(
void)
885 static void __exit bf5xx_nand_exit(
void)