26 #include <linux/kernel.h>
28 #include <linux/module.h>
35 #include <linux/slab.h>
80 static bool ecc4_busy;
82 #define to_davinci_nand(m) container_of(m, struct davinci_nand_info, mtd)
103 static void nand_davinci_hwcontrol(
struct mtd_info *mtd,
int cmd,
124 static void nand_davinci_select_chip(
struct mtd_info *mtd,
int chip)
135 info->
chip.IO_ADDR_R = info->
chip.IO_ADDR_W;
152 static void nand_davinci_hwctl_1bit(
struct mtd_info *mtd,
int mode)
161 nand_davinci_readecc_1bit(mtd);
170 spin_unlock_irqrestore(&davinci_nand_lock, flags);
176 static int nand_davinci_calculate_1bit(
struct mtd_info *mtd,
179 unsigned int ecc_val = nand_davinci_readecc_1bit(mtd);
180 unsigned int ecc24 = (ecc_val & 0x0fff) | ((ecc_val & 0x0fff0000) >> 4);
184 ecc_code[0] = (
u_char)(ecc24);
185 ecc_code[1] = (
u_char)(ecc24 >> 8);
186 ecc_code[2] = (
u_char)(ecc24 >> 16);
191 static int nand_davinci_correct_1bit(
struct mtd_info *mtd,
u_char *dat,
195 uint32_t eccNand = read_ecc[0] | (read_ecc[1] << 8) |
197 uint32_t eccCalc = calc_ecc[0] | (calc_ecc[1] << 8) |
202 if ((((diff >> 12) ^ diff) & 0xfff) == 0xfff) {
204 if ((diff >> (12 + 3)) < chip->
ecc.size) {
205 dat[diff >> (12 + 3)] ^=
BIT((diff >> 12) & 7);
210 }
else if (!(diff & (diff - 1))) {
237 static void nand_davinci_hwctl_4bit(
struct mtd_info *mtd,
int mode)
253 spin_unlock_irqrestore(&davinci_nand_lock, flags);
269 static int nand_davinci_calculate_4bit(
struct mtd_info *mtd,
291 nand_davinci_readecc_4bit(info, raw_ecc);
292 for (i = 0, p = raw_ecc; i < 2; i++, p += 2) {
293 *ecc_code++ = p[0] & 0xff;
294 *ecc_code++ = ((p[0] >> 8) & 0x03) | ((p[0] >> 14) & 0xfc);
295 *ecc_code++ = ((p[0] >> 22) & 0x0f) | ((p[1] << 4) & 0xf0);
296 *ecc_code++ = ((p[1] >> 4) & 0x3f) | ((p[1] >> 10) & 0xc0);
297 *ecc_code++ = (p[1] >> 18) & 0xff;
306 static int nand_davinci_correct_4bit(
struct mtd_info *mtd,
311 unsigned short ecc10[8];
312 unsigned short *ecc16;
319 for (i = 0; i < 10; i++) {
320 if (ecc_code[i] != 0xff)
329 if (
WARN_ON(0x01 & (
unsigned) ecc_code))
331 ecc16 = (
unsigned short *)ecc_code;
333 ecc10[0] = (ecc16[0] >> 0) & 0x3ff;
334 ecc10[1] = ((ecc16[0] >> 10) & 0x3f) | ((ecc16[1] << 6) & 0x3c0);
335 ecc10[2] = (ecc16[1] >> 4) & 0x3ff;
336 ecc10[3] = ((ecc16[1] >> 14) & 0x3) | ((ecc16[2] << 2) & 0x3fc);
337 ecc10[4] = (ecc16[2] >> 8) | ((ecc16[3] << 8) & 0x300);
338 ecc10[5] = (ecc16[3] >> 2) & 0x3ff;
339 ecc10[6] = ((ecc16[3] >> 12) & 0xf) | ((ecc16[4] << 4) & 0x3f0);
340 ecc10[7] = (ecc16[4] >> 6) & 0x3ff;
343 for (i = 7; i >= 0; i--)
350 nand_davinci_readecc_4bit(info, syndrome);
351 if (!(syndrome[0] | syndrome[1] | syndrome[2] | syndrome[3]))
378 ecc_state = (davinci_nand_readl(info,
381 }
while ((ecc_state < 4) &&
time_before(jiffies, timeo));
386 switch ((fsr >> 8) & 0x0f) {
395 num_errors = 1 + ((fsr >> 16) & 0x03);
405 for (i = 0, corrected = 0; i <
num_errors; i++) {
406 int error_address, error_value;
409 error_address = davinci_nand_readl(info,
411 error_value = davinci_nand_readl(info,
414 error_address = davinci_nand_readl(info,
416 error_value = davinci_nand_readl(info,
421 error_address >>= 16;
424 error_address &= 0x3ff;
425 error_address = (512 + 7) - error_address;
427 if (error_address < 512) {
428 data[error_address] ^= error_value;
451 if ((0x03 & ((
unsigned)buf)) == 0 && (0x03 & len) == 0)
453 else if ((0x01 & ((
unsigned)buf)) == 0 && (0x01 & len) == 0)
459 static void nand_davinci_write_buf(
struct mtd_info *mtd,
464 if ((0x03 & ((
unsigned)buf)) == 0 && (0x03 & len) == 0)
466 else if ((0x01 & ((
unsigned)buf)) == 0 && (0x01 & len) == 0)
476 static int nand_davinci_dev_ready(
struct mtd_info *mtd)
491 .eccpos = { 0, 1, 2, 3, 4,
496 {.offset = 8, .length = 5, },
509 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
510 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
511 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
512 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
516 {.offset = 2, .length = 22, },
522 #if defined(CONFIG_OF)
524 {.compatible =
"ti,davinci-nand", },
532 if (!pdev->
dev.platform_data && pdev->
dev.of_node) {
544 if (!of_property_read_u32(pdev->
dev.of_node,
545 "ti,davinci-chipselect", &prop))
547 if (!of_property_read_u32(pdev->
dev.of_node,
548 "ti,davinci-mask-ale", &prop))
550 if (!of_property_read_u32(pdev->
dev.of_node,
551 "ti,davinci-mask-cle", &prop))
553 if (!of_property_read_u32(pdev->
dev.of_node,
554 "ti,davinci-mask-chipsel", &prop))
557 "ti,davinci-ecc-mode", &mode)) {
565 if (!of_property_read_u32(pdev->
dev.of_node,
566 "ti,davinci-ecc-bits", &prop))
568 if (!of_property_read_u32(pdev->
dev.of_node,
569 "ti,davinci-nand-buswidth", &prop))
573 "ti,davinci-nand-use-bbt", &len))
577 return pdev->
dev.platform_data;
580 #define davinci_nand_of_match NULL
584 return pdev->
dev.platform_data;
600 pdata = nand_davinci_get_pdata(pdev);
606 if (pdev->
id < 0 || pdev->
id > 3)
611 dev_err(&pdev->
dev,
"unable to allocate memory\n");
616 platform_set_drvdata(pdev, info);
620 if (!res1 || !res2) {
628 if (!vaddr || !base) {
639 info->
mtd.name = dev_name(&pdev->
dev);
642 info->
mtd.dev.parent = &pdev->
dev;
646 info->
chip.chip_delay = 0;
647 info->
chip.select_chip = nand_davinci_select_chip;
668 info->
chip.cmd_ctrl = nand_davinci_hwcontrol;
669 info->
chip.dev_ready = nand_davinci_dev_ready;
672 info->
chip.read_buf = nand_davinci_read_buf;
673 info->
chip.write_buf = nand_davinci_write_buf;
691 spin_lock_irq(&davinci_nand_lock);
696 spin_unlock_irq(&davinci_nand_lock);
701 info->
chip.ecc.calculate = nand_davinci_calculate_4bit;
702 info->
chip.ecc.correct = nand_davinci_correct_4bit;
703 info->
chip.ecc.hwctl = nand_davinci_hwctl_4bit;
704 info->
chip.ecc.bytes = 10;
706 info->
chip.ecc.calculate = nand_davinci_calculate_1bit;
707 info->
chip.ecc.correct = nand_davinci_correct_1bit;
708 info->
chip.ecc.hwctl = nand_davinci_hwctl_1bit;
709 info->
chip.ecc.bytes = 3;
711 info->
chip.ecc.size = 512;
718 info->
chip.ecc.mode = ecc_mode;
721 if (IS_ERR(info->
clk)) {
722 ret = PTR_ERR(info->
clk);
723 dev_dbg(&pdev->
dev,
"unable to get AEMIF clock, err %d\n", ret);
727 ret = clk_prepare_enable(info->
clk);
729 dev_dbg(&pdev->
dev,
"unable to enable AEMIF clock, err %d\n",
752 dev_dbg(&pdev->
dev,
"NAND timing values setup fail\n");
756 spin_lock_irq(&davinci_nand_lock);
763 spin_unlock_irq(&davinci_nand_lock);
768 dev_dbg(&pdev->
dev,
"no NAND chip(s) found\n");
778 int chunks = info->
mtd.writesize / 512;
780 if (!chunks || info->
mtd.oobsize < 16) {
793 info->
mtd.oobsize - 16;
812 "for 4KiB-page NAND\n");
832 (val >> 8) & 0xff, val & 0xff);
838 clk_disable_unprepare(info->
clk);
843 spin_lock_irq(&davinci_nand_lock);
846 spin_unlock_irq(&davinci_nand_lock);
865 spin_lock_irq(&davinci_nand_lock);
868 spin_unlock_irq(&davinci_nand_lock);
875 clk_disable_unprepare(info->
clk);
884 .remove =
__exit_p(nand_davinci_remove),
886 .name =
"davinci_nand",
893 static int __init nand_davinci_init(
void)
899 static void __exit nand_davinci_exit(
void)