13 #include <linux/device.h>
19 #include <linux/pci.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
27 #define CAFE_NAND_CTRL1 0x00
28 #define CAFE_NAND_CTRL2 0x04
29 #define CAFE_NAND_CTRL3 0x08
30 #define CAFE_NAND_STATUS 0x0c
31 #define CAFE_NAND_IRQ 0x10
32 #define CAFE_NAND_IRQ_MASK 0x14
33 #define CAFE_NAND_DATA_LEN 0x18
34 #define CAFE_NAND_ADDR1 0x1c
35 #define CAFE_NAND_ADDR2 0x20
36 #define CAFE_NAND_TIMING1 0x24
37 #define CAFE_NAND_TIMING2 0x28
38 #define CAFE_NAND_TIMING3 0x2c
39 #define CAFE_NAND_NONMEM 0x30
40 #define CAFE_NAND_ECC_RESULT 0x3C
41 #define CAFE_NAND_DMA_CTRL 0x40
42 #define CAFE_NAND_DMA_ADDR0 0x44
43 #define CAFE_NAND_DMA_ADDR1 0x48
44 #define CAFE_NAND_ECC_SYN01 0x50
45 #define CAFE_NAND_ECC_SYN23 0x54
46 #define CAFE_NAND_ECC_SYN45 0x58
47 #define CAFE_NAND_ECC_SYN67 0x5c
48 #define CAFE_NAND_READ_DATA 0x1000
49 #define CAFE_NAND_WRITE_DATA 0x2000
51 #define CAFE_GLOBAL_CTRL 0x3004
52 #define CAFE_GLOBAL_IRQ 0x3008
53 #define CAFE_GLOBAL_IRQ_MASK 0x300c
54 #define CAFE_NAND_RESET 0x3034
57 #define CTRL1_CHIPSELECT (1<<19)
74 static int usedma = 1;
77 static int skipbbt = 0;
83 static int regdebug = 0;
86 static int checkecc = 1;
89 static unsigned int numtimings;
93 static const char *part_probes[] = {
"cmdlinepart",
"RedBoot",
NULL };
96 #define cafe_dev_dbg(dev, args...) do { if (debug) dev_dbg(dev, ##args); } while(0)
99 #define cafe_readl(cafe, addr) readl((cafe)->mmio + CAFE_##addr)
100 #define cafe_writel(cafe, datum, addr) writel(datum, (cafe)->mmio + CAFE_##addr)
102 static int cafe_device_ready(
struct mtd_info *mtd)
110 cafe_dev_dbg(&cafe->
pdev->dev,
"NAND device is%s ready, IRQ %x (%x) (%x,%x)\n",
111 result?
"":
" not", irqs,
cafe_readl(cafe, NAND_IRQ),
129 cafe_dev_dbg(&cafe->
pdev->dev,
"Copy 0x%x bytes to write buffer. datalen 0x%x\n",
142 cafe_dev_dbg(&cafe->
pdev->dev,
"Copy 0x%x bytes from position 0x%x in read buffer.\n",
152 cafe_read_buf(mtd, &d, 1);
167 command, column, page_addr);
173 cafe->
ctl2 &= ~(1<<30);
196 }
else if (page_addr != -1) {
228 ctl1 |= ((adrbytes-1)|8) << 27;
234 cafe_dev_dbg(&cafe->
pdev->dev,
"Setup for delayed command, ctl1 %08x, dlen %x\n",
251 if (usedma && (ctl1 & (3<<25))) {
254 if (ctl1 & (1<<26)) {
259 doneint = 0x10000000;
267 printk(
"About to write command %08x to register 0\n", ctl1);
268 for (i=4; i< 0x5c; i+=4)
281 for (c = 500000; c != 0; c--) {
291 cafe_dev_dbg(&cafe->
pdev->dev,
"Command %x completed after %d usec, irqs %x (%x)\n",
292 command, 500000-c, irqs,
cafe_readl(cafe, NAND_IRQ));
320 static void cafe_select_chip(
struct mtd_info *mtd,
int chipnr)
334 static irqreturn_t cafe_nand_interrupt(
int irq,
void *
id)
347 static void cafe_nand_bug(
struct mtd_info *mtd)
352 static int cafe_nand_write_oob(
struct mtd_info *mtd,
384 uint8_t *buf,
int oob_required,
int page)
387 unsigned int max_bitflips = 0;
396 if (checkecc &&
cafe_readl(cafe, NAND_ECC_RESULT) & (1<<18)) {
397 unsigned short syn[8],
pat[4];
402 for (i=0; i<8; i+=2) {
404 syn[
i] = cafe->
rs->index_of[tmp & 0xfff];
405 syn[i+1] = cafe->
rs->index_of[(tmp >> 16) & 0xfff];
408 n = decode_rs16(cafe->
rs,
NULL,
NULL, 1367, syn, 0, pos, 0,
411 for (i = 0; i <
n; i++) {
425 }
else if (p == 1365) {
426 buf[2047] ^= pat[
i] >> 4;
427 oob[0] ^= pat[
i] << 4;
428 }
else if (p > 1365) {
430 oob[3*p/2 - 2048] ^= pat[
i] >> 4;
431 oob[3*p/2 - 2047] ^= pat[
i] << 4;
433 oob[3*p/2 - 2049] ^= pat[
i] >> 8;
434 oob[3*p/2 - 2048] ^= pat[
i];
436 }
else if ((p & 1) == 1) {
437 buf[3*p/2] ^= pat[
i] >> 4;
438 buf[3*p/2 + 1] ^= pat[
i] << 4;
440 buf[3*p/2 - 1] ^= pat[
i] >> 8;
441 buf[3*p/2] ^= pat[
i];
446 dev_dbg(&cafe->
pdev->dev,
"Failed to correct ECC at %08x\n",
448 for (i = 0; i < 0x5c; i += 4)
452 dev_dbg(&cafe->
pdev->dev,
"Corrected %d symbol errors\n", n);
454 max_bitflips =
max_t(
unsigned int, max_bitflips, n);
463 .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
464 .oobfree = {{14, 50}}
469 static uint8_t cafe_bbt_pattern_2048[] = {
'B',
'b',
't',
'0' };
470 static uint8_t cafe_mirror_pattern_2048[] = {
'1',
't',
'b',
'B' };
472 static uint8_t cafe_bbt_pattern_512[] = { 0xBB };
473 static uint8_t cafe_mirror_pattern_512[] = { 0xBC };
483 .pattern = cafe_bbt_pattern_2048
493 .pattern = cafe_mirror_pattern_2048
498 .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
509 .pattern = cafe_bbt_pattern_512
519 .pattern = cafe_mirror_pattern_512
523 static int cafe_nand_write_page_lowlevel(
struct mtd_info *mtd,
525 const uint8_t *buf,
int oob_required)
533 cafe->
ctl2 |= (1<<30);
539 const uint8_t *buf,
int oob_required,
int page,
547 status = chip->
ecc.write_page_raw(mtd, chip, buf, oob_required);
549 status = chip->
ecc.write_page(mtd, chip, buf, oob_required);
572 if (status & NAND_STATUS_FAIL)
582 static int cafe_nand_block_bad(
struct mtd_info *mtd, loff_t ofs,
int getchip)
594 for (i = 0; i < 6; i++) {
616 ch = gf64_mul(ah ^ al, bh ^ bl) ^ gf64_mul(al, bl);
617 cl = gf64_mul(gf64_mul(ah, bh), 0x21) ^ gf64_mul(al, bl);
619 return (ch << 6) ^
cl;
626 return gf4096_mul(x, 0xe01);
650 dev_warn(&pdev->
dev,
"failed to alloc mtd_info\n");
653 cafe = (
void *)(&mtd[1]);
655 mtd->
dev.parent = &pdev->
dev;
660 cafe->
mmio = pci_iomap(pdev, 0, 0);
672 cafe->
nand.buffers = (
void *)cafe->
dmabuf + 2112;
680 cafe->
nand.cmdfunc = cafe_nand_cmdfunc;
681 cafe->
nand.dev_ready = cafe_device_ready;
682 cafe->
nand.read_byte = cafe_read_byte;
683 cafe->
nand.read_buf = cafe_read_buf;
684 cafe->
nand.write_buf = cafe_write_buf;
685 cafe->
nand.select_chip = cafe_select_chip;
687 cafe->
nand.chip_delay = 0;
695 cafe->
nand.block_bad = cafe_nand_block_bad;
698 if (numtimings && numtimings != 3) {
699 dev_warn(&cafe->
pdev->dev,
"%d timing register values ignored; precisely three are required\n", numtimings);
702 if (numtimings == 3) {
711 cafe_dev_dbg(&cafe->
pdev->dev,
"Timing registers already set (%08x %08x %08x)\n",
714 dev_warn(&cafe->
pdev->dev,
"Timing registers unset; using most conservative defaults\n");
774 cafe->
nand.ecc.layout = &cafe_oobinfo_2048;
775 cafe->
nand.bbt_td = &cafe_bbt_main_descr_2048;
776 cafe->
nand.bbt_md = &cafe_bbt_mirror_descr_2048;
778 cafe->
nand.ecc.layout = &cafe_oobinfo_512;
779 cafe->
nand.bbt_td = &cafe_bbt_main_descr_512;
780 cafe->
nand.bbt_md = &cafe_bbt_mirror_descr_512;
788 cafe->
nand.ecc.bytes = 14;
789 cafe->
nand.ecc.strength = 4;
790 cafe->
nand.ecc.hwctl = (
void *)cafe_nand_bug;
791 cafe->
nand.ecc.calculate = (
void *)cafe_nand_bug;
792 cafe->
nand.ecc.correct = (
void *)cafe_nand_bug;
793 cafe->
nand.write_page = cafe_nand_write_page;
794 cafe->
nand.ecc.write_page = cafe_nand_write_page_lowlevel;
795 cafe->
nand.ecc.write_oob = cafe_nand_write_oob;
796 cafe->
nand.ecc.read_page = cafe_nand_read_page;
797 cafe->
nand.ecc.read_oob = cafe_nand_read_oob;
803 pci_set_drvdata(pdev, mtd);
805 mtd->
name =
"cafe_nand";
826 struct mtd_info *mtd = pci_get_drvdata(pdev);
847 static int cafe_nand_resume(
struct pci_dev *pdev)
850 struct mtd_info *mtd = pci_get_drvdata(pdev);
886 static struct pci_driver cafe_nand_pci_driver = {
888 .id_table = cafe_nand_tbl,
889 .probe = cafe_nand_probe,
891 .resume = cafe_nand_resume,