18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Driver for One Laptop Per Child ‘CAFÉ’ controller, aka Marvell 88ALP01 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * The data sheet for this device can be found at: 68c2ecf20Sopenharmony_ci * http://wiki.laptop.org/go/Datasheets 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Copyright © 2006 Red Hat, Inc. 98c2ecf20Sopenharmony_ci * Copyright © 2006 David Woodhouse <dwmw2@infradead.org> 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#define DEBUG 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include <linux/device.h> 158c2ecf20Sopenharmony_ci#undef DEBUG 168c2ecf20Sopenharmony_ci#include <linux/mtd/mtd.h> 178c2ecf20Sopenharmony_ci#include <linux/mtd/rawnand.h> 188c2ecf20Sopenharmony_ci#include <linux/mtd/partitions.h> 198c2ecf20Sopenharmony_ci#include <linux/rslib.h> 208c2ecf20Sopenharmony_ci#include <linux/pci.h> 218c2ecf20Sopenharmony_ci#include <linux/delay.h> 228c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 238c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h> 248c2ecf20Sopenharmony_ci#include <linux/slab.h> 258c2ecf20Sopenharmony_ci#include <linux/module.h> 268c2ecf20Sopenharmony_ci#include <asm/io.h> 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci#define CAFE_NAND_CTRL1 0x00 298c2ecf20Sopenharmony_ci#define CAFE_NAND_CTRL2 0x04 308c2ecf20Sopenharmony_ci#define CAFE_NAND_CTRL3 0x08 318c2ecf20Sopenharmony_ci#define CAFE_NAND_STATUS 0x0c 328c2ecf20Sopenharmony_ci#define CAFE_NAND_IRQ 0x10 338c2ecf20Sopenharmony_ci#define CAFE_NAND_IRQ_MASK 0x14 348c2ecf20Sopenharmony_ci#define CAFE_NAND_DATA_LEN 0x18 358c2ecf20Sopenharmony_ci#define CAFE_NAND_ADDR1 0x1c 368c2ecf20Sopenharmony_ci#define CAFE_NAND_ADDR2 0x20 378c2ecf20Sopenharmony_ci#define CAFE_NAND_TIMING1 0x24 388c2ecf20Sopenharmony_ci#define CAFE_NAND_TIMING2 0x28 398c2ecf20Sopenharmony_ci#define CAFE_NAND_TIMING3 0x2c 408c2ecf20Sopenharmony_ci#define CAFE_NAND_NONMEM 0x30 418c2ecf20Sopenharmony_ci#define CAFE_NAND_ECC_RESULT 0x3C 428c2ecf20Sopenharmony_ci#define CAFE_NAND_DMA_CTRL 0x40 438c2ecf20Sopenharmony_ci#define CAFE_NAND_DMA_ADDR0 0x44 448c2ecf20Sopenharmony_ci#define CAFE_NAND_DMA_ADDR1 0x48 458c2ecf20Sopenharmony_ci#define CAFE_NAND_ECC_SYN01 0x50 468c2ecf20Sopenharmony_ci#define CAFE_NAND_ECC_SYN23 0x54 478c2ecf20Sopenharmony_ci#define CAFE_NAND_ECC_SYN45 0x58 488c2ecf20Sopenharmony_ci#define CAFE_NAND_ECC_SYN67 0x5c 498c2ecf20Sopenharmony_ci#define CAFE_NAND_READ_DATA 0x1000 508c2ecf20Sopenharmony_ci#define CAFE_NAND_WRITE_DATA 0x2000 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci#define CAFE_GLOBAL_CTRL 0x3004 538c2ecf20Sopenharmony_ci#define CAFE_GLOBAL_IRQ 0x3008 548c2ecf20Sopenharmony_ci#define CAFE_GLOBAL_IRQ_MASK 0x300c 558c2ecf20Sopenharmony_ci#define CAFE_NAND_RESET 0x3034 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci/* Missing from the datasheet: bit 19 of CTRL1 sets CE0 vs. CE1 */ 588c2ecf20Sopenharmony_ci#define CTRL1_CHIPSELECT (1<<19) 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_cistruct cafe_priv { 618c2ecf20Sopenharmony_ci struct nand_chip nand; 628c2ecf20Sopenharmony_ci struct pci_dev *pdev; 638c2ecf20Sopenharmony_ci void __iomem *mmio; 648c2ecf20Sopenharmony_ci struct rs_control *rs; 658c2ecf20Sopenharmony_ci uint32_t ctl1; 668c2ecf20Sopenharmony_ci uint32_t ctl2; 678c2ecf20Sopenharmony_ci int datalen; 688c2ecf20Sopenharmony_ci int nr_data; 698c2ecf20Sopenharmony_ci int data_pos; 708c2ecf20Sopenharmony_ci int page_addr; 718c2ecf20Sopenharmony_ci bool usedma; 728c2ecf20Sopenharmony_ci dma_addr_t dmaaddr; 738c2ecf20Sopenharmony_ci unsigned char *dmabuf; 748c2ecf20Sopenharmony_ci}; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_cistatic int usedma = 1; 778c2ecf20Sopenharmony_cimodule_param(usedma, int, 0644); 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_cistatic int skipbbt = 0; 808c2ecf20Sopenharmony_cimodule_param(skipbbt, int, 0644); 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_cistatic int debug = 0; 838c2ecf20Sopenharmony_cimodule_param(debug, int, 0644); 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_cistatic int regdebug = 0; 868c2ecf20Sopenharmony_cimodule_param(regdebug, int, 0644); 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_cistatic int checkecc = 1; 898c2ecf20Sopenharmony_cimodule_param(checkecc, int, 0644); 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_cistatic unsigned int numtimings; 928c2ecf20Sopenharmony_cistatic int timing[3]; 938c2ecf20Sopenharmony_cimodule_param_array(timing, int, &numtimings, 0644); 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_cistatic const char *part_probes[] = { "cmdlinepart", "RedBoot", NULL }; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci/* Hrm. Why isn't this already conditional on something in the struct device? */ 988c2ecf20Sopenharmony_ci#define cafe_dev_dbg(dev, args...) do { if (debug) dev_dbg(dev, ##args); } while(0) 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci/* Make it easier to switch to PIO if we need to */ 1018c2ecf20Sopenharmony_ci#define cafe_readl(cafe, addr) readl((cafe)->mmio + CAFE_##addr) 1028c2ecf20Sopenharmony_ci#define cafe_writel(cafe, datum, addr) writel(datum, (cafe)->mmio + CAFE_##addr) 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_cistatic int cafe_device_ready(struct nand_chip *chip) 1058c2ecf20Sopenharmony_ci{ 1068c2ecf20Sopenharmony_ci struct cafe_priv *cafe = nand_get_controller_data(chip); 1078c2ecf20Sopenharmony_ci int result = !!(cafe_readl(cafe, NAND_STATUS) & 0x40000000); 1088c2ecf20Sopenharmony_ci uint32_t irqs = cafe_readl(cafe, NAND_IRQ); 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci cafe_writel(cafe, irqs, NAND_IRQ); 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci cafe_dev_dbg(&cafe->pdev->dev, "NAND device is%s ready, IRQ %x (%x) (%x,%x)\n", 1138c2ecf20Sopenharmony_ci result?"":" not", irqs, cafe_readl(cafe, NAND_IRQ), 1148c2ecf20Sopenharmony_ci cafe_readl(cafe, GLOBAL_IRQ), cafe_readl(cafe, GLOBAL_IRQ_MASK)); 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci return result; 1178c2ecf20Sopenharmony_ci} 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_cistatic void cafe_write_buf(struct nand_chip *chip, const uint8_t *buf, int len) 1218c2ecf20Sopenharmony_ci{ 1228c2ecf20Sopenharmony_ci struct cafe_priv *cafe = nand_get_controller_data(chip); 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci if (cafe->usedma) 1258c2ecf20Sopenharmony_ci memcpy(cafe->dmabuf + cafe->datalen, buf, len); 1268c2ecf20Sopenharmony_ci else 1278c2ecf20Sopenharmony_ci memcpy_toio(cafe->mmio + CAFE_NAND_WRITE_DATA + cafe->datalen, buf, len); 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci cafe->datalen += len; 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci cafe_dev_dbg(&cafe->pdev->dev, "Copy 0x%x bytes to write buffer. datalen 0x%x\n", 1328c2ecf20Sopenharmony_ci len, cafe->datalen); 1338c2ecf20Sopenharmony_ci} 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_cistatic void cafe_read_buf(struct nand_chip *chip, uint8_t *buf, int len) 1368c2ecf20Sopenharmony_ci{ 1378c2ecf20Sopenharmony_ci struct cafe_priv *cafe = nand_get_controller_data(chip); 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci if (cafe->usedma) 1408c2ecf20Sopenharmony_ci memcpy(buf, cafe->dmabuf + cafe->datalen, len); 1418c2ecf20Sopenharmony_ci else 1428c2ecf20Sopenharmony_ci memcpy_fromio(buf, cafe->mmio + CAFE_NAND_READ_DATA + cafe->datalen, len); 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci cafe_dev_dbg(&cafe->pdev->dev, "Copy 0x%x bytes from position 0x%x in read buffer.\n", 1458c2ecf20Sopenharmony_ci len, cafe->datalen); 1468c2ecf20Sopenharmony_ci cafe->datalen += len; 1478c2ecf20Sopenharmony_ci} 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_cistatic uint8_t cafe_read_byte(struct nand_chip *chip) 1508c2ecf20Sopenharmony_ci{ 1518c2ecf20Sopenharmony_ci struct cafe_priv *cafe = nand_get_controller_data(chip); 1528c2ecf20Sopenharmony_ci uint8_t d; 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci cafe_read_buf(chip, &d, 1); 1558c2ecf20Sopenharmony_ci cafe_dev_dbg(&cafe->pdev->dev, "Read %02x\n", d); 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci return d; 1588c2ecf20Sopenharmony_ci} 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_cistatic void cafe_nand_cmdfunc(struct nand_chip *chip, unsigned command, 1618c2ecf20Sopenharmony_ci int column, int page_addr) 1628c2ecf20Sopenharmony_ci{ 1638c2ecf20Sopenharmony_ci struct mtd_info *mtd = nand_to_mtd(chip); 1648c2ecf20Sopenharmony_ci struct cafe_priv *cafe = nand_get_controller_data(chip); 1658c2ecf20Sopenharmony_ci int adrbytes = 0; 1668c2ecf20Sopenharmony_ci uint32_t ctl1; 1678c2ecf20Sopenharmony_ci uint32_t doneint = 0x80000000; 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci cafe_dev_dbg(&cafe->pdev->dev, "cmdfunc %02x, 0x%x, 0x%x\n", 1708c2ecf20Sopenharmony_ci command, column, page_addr); 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci if (command == NAND_CMD_ERASE2 || command == NAND_CMD_PAGEPROG) { 1738c2ecf20Sopenharmony_ci /* Second half of a command we already calculated */ 1748c2ecf20Sopenharmony_ci cafe_writel(cafe, cafe->ctl2 | 0x100 | command, NAND_CTRL2); 1758c2ecf20Sopenharmony_ci ctl1 = cafe->ctl1; 1768c2ecf20Sopenharmony_ci cafe->ctl2 &= ~(1<<30); 1778c2ecf20Sopenharmony_ci cafe_dev_dbg(&cafe->pdev->dev, "Continue command, ctl1 %08x, #data %d\n", 1788c2ecf20Sopenharmony_ci cafe->ctl1, cafe->nr_data); 1798c2ecf20Sopenharmony_ci goto do_command; 1808c2ecf20Sopenharmony_ci } 1818c2ecf20Sopenharmony_ci /* Reset ECC engine */ 1828c2ecf20Sopenharmony_ci cafe_writel(cafe, 0, NAND_CTRL2); 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci /* Emulate NAND_CMD_READOOB on large-page chips */ 1858c2ecf20Sopenharmony_ci if (mtd->writesize > 512 && 1868c2ecf20Sopenharmony_ci command == NAND_CMD_READOOB) { 1878c2ecf20Sopenharmony_ci column += mtd->writesize; 1888c2ecf20Sopenharmony_ci command = NAND_CMD_READ0; 1898c2ecf20Sopenharmony_ci } 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci /* FIXME: Do we need to send read command before sending data 1928c2ecf20Sopenharmony_ci for small-page chips, to position the buffer correctly? */ 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci if (column != -1) { 1958c2ecf20Sopenharmony_ci cafe_writel(cafe, column, NAND_ADDR1); 1968c2ecf20Sopenharmony_ci adrbytes = 2; 1978c2ecf20Sopenharmony_ci if (page_addr != -1) 1988c2ecf20Sopenharmony_ci goto write_adr2; 1998c2ecf20Sopenharmony_ci } else if (page_addr != -1) { 2008c2ecf20Sopenharmony_ci cafe_writel(cafe, page_addr & 0xffff, NAND_ADDR1); 2018c2ecf20Sopenharmony_ci page_addr >>= 16; 2028c2ecf20Sopenharmony_ci write_adr2: 2038c2ecf20Sopenharmony_ci cafe_writel(cafe, page_addr, NAND_ADDR2); 2048c2ecf20Sopenharmony_ci adrbytes += 2; 2058c2ecf20Sopenharmony_ci if (mtd->size > mtd->writesize << 16) 2068c2ecf20Sopenharmony_ci adrbytes++; 2078c2ecf20Sopenharmony_ci } 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci cafe->data_pos = cafe->datalen = 0; 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci /* Set command valid bit, mask in the chip select bit */ 2128c2ecf20Sopenharmony_ci ctl1 = 0x80000000 | command | (cafe->ctl1 & CTRL1_CHIPSELECT); 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci /* Set RD or WR bits as appropriate */ 2158c2ecf20Sopenharmony_ci if (command == NAND_CMD_READID || command == NAND_CMD_STATUS) { 2168c2ecf20Sopenharmony_ci ctl1 |= (1<<26); /* rd */ 2178c2ecf20Sopenharmony_ci /* Always 5 bytes, for now */ 2188c2ecf20Sopenharmony_ci cafe->datalen = 4; 2198c2ecf20Sopenharmony_ci /* And one address cycle -- even for STATUS, since the controller doesn't work without */ 2208c2ecf20Sopenharmony_ci adrbytes = 1; 2218c2ecf20Sopenharmony_ci } else if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 || 2228c2ecf20Sopenharmony_ci command == NAND_CMD_READOOB || command == NAND_CMD_RNDOUT) { 2238c2ecf20Sopenharmony_ci ctl1 |= 1<<26; /* rd */ 2248c2ecf20Sopenharmony_ci /* For now, assume just read to end of page */ 2258c2ecf20Sopenharmony_ci cafe->datalen = mtd->writesize + mtd->oobsize - column; 2268c2ecf20Sopenharmony_ci } else if (command == NAND_CMD_SEQIN) 2278c2ecf20Sopenharmony_ci ctl1 |= 1<<25; /* wr */ 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci /* Set number of address bytes */ 2308c2ecf20Sopenharmony_ci if (adrbytes) 2318c2ecf20Sopenharmony_ci ctl1 |= ((adrbytes-1)|8) << 27; 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci if (command == NAND_CMD_SEQIN || command == NAND_CMD_ERASE1) { 2348c2ecf20Sopenharmony_ci /* Ignore the first command of a pair; the hardware 2358c2ecf20Sopenharmony_ci deals with them both at once, later */ 2368c2ecf20Sopenharmony_ci cafe->ctl1 = ctl1; 2378c2ecf20Sopenharmony_ci cafe_dev_dbg(&cafe->pdev->dev, "Setup for delayed command, ctl1 %08x, dlen %x\n", 2388c2ecf20Sopenharmony_ci cafe->ctl1, cafe->datalen); 2398c2ecf20Sopenharmony_ci return; 2408c2ecf20Sopenharmony_ci } 2418c2ecf20Sopenharmony_ci /* RNDOUT and READ0 commands need a following byte */ 2428c2ecf20Sopenharmony_ci if (command == NAND_CMD_RNDOUT) 2438c2ecf20Sopenharmony_ci cafe_writel(cafe, cafe->ctl2 | 0x100 | NAND_CMD_RNDOUTSTART, NAND_CTRL2); 2448c2ecf20Sopenharmony_ci else if (command == NAND_CMD_READ0 && mtd->writesize > 512) 2458c2ecf20Sopenharmony_ci cafe_writel(cafe, cafe->ctl2 | 0x100 | NAND_CMD_READSTART, NAND_CTRL2); 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ci do_command: 2488c2ecf20Sopenharmony_ci cafe_dev_dbg(&cafe->pdev->dev, "dlen %x, ctl1 %x, ctl2 %x\n", 2498c2ecf20Sopenharmony_ci cafe->datalen, ctl1, cafe_readl(cafe, NAND_CTRL2)); 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci /* NB: The datasheet lies -- we really should be subtracting 1 here */ 2528c2ecf20Sopenharmony_ci cafe_writel(cafe, cafe->datalen, NAND_DATA_LEN); 2538c2ecf20Sopenharmony_ci cafe_writel(cafe, 0x90000000, NAND_IRQ); 2548c2ecf20Sopenharmony_ci if (cafe->usedma && (ctl1 & (3<<25))) { 2558c2ecf20Sopenharmony_ci uint32_t dmactl = 0xc0000000 + cafe->datalen; 2568c2ecf20Sopenharmony_ci /* If WR or RD bits set, set up DMA */ 2578c2ecf20Sopenharmony_ci if (ctl1 & (1<<26)) { 2588c2ecf20Sopenharmony_ci /* It's a read */ 2598c2ecf20Sopenharmony_ci dmactl |= (1<<29); 2608c2ecf20Sopenharmony_ci /* ... so it's done when the DMA is done, not just 2618c2ecf20Sopenharmony_ci the command. */ 2628c2ecf20Sopenharmony_ci doneint = 0x10000000; 2638c2ecf20Sopenharmony_ci } 2648c2ecf20Sopenharmony_ci cafe_writel(cafe, dmactl, NAND_DMA_CTRL); 2658c2ecf20Sopenharmony_ci } 2668c2ecf20Sopenharmony_ci cafe->datalen = 0; 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci if (unlikely(regdebug)) { 2698c2ecf20Sopenharmony_ci int i; 2708c2ecf20Sopenharmony_ci printk("About to write command %08x to register 0\n", ctl1); 2718c2ecf20Sopenharmony_ci for (i=4; i< 0x5c; i+=4) 2728c2ecf20Sopenharmony_ci printk("Register %x: %08x\n", i, readl(cafe->mmio + i)); 2738c2ecf20Sopenharmony_ci } 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci cafe_writel(cafe, ctl1, NAND_CTRL1); 2768c2ecf20Sopenharmony_ci /* Apply this short delay always to ensure that we do wait tWB in 2778c2ecf20Sopenharmony_ci * any case on any machine. */ 2788c2ecf20Sopenharmony_ci ndelay(100); 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci if (1) { 2818c2ecf20Sopenharmony_ci int c; 2828c2ecf20Sopenharmony_ci uint32_t irqs; 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci for (c = 500000; c != 0; c--) { 2858c2ecf20Sopenharmony_ci irqs = cafe_readl(cafe, NAND_IRQ); 2868c2ecf20Sopenharmony_ci if (irqs & doneint) 2878c2ecf20Sopenharmony_ci break; 2888c2ecf20Sopenharmony_ci udelay(1); 2898c2ecf20Sopenharmony_ci if (!(c % 100000)) 2908c2ecf20Sopenharmony_ci cafe_dev_dbg(&cafe->pdev->dev, "Wait for ready, IRQ %x\n", irqs); 2918c2ecf20Sopenharmony_ci cpu_relax(); 2928c2ecf20Sopenharmony_ci } 2938c2ecf20Sopenharmony_ci cafe_writel(cafe, doneint, NAND_IRQ); 2948c2ecf20Sopenharmony_ci cafe_dev_dbg(&cafe->pdev->dev, "Command %x completed after %d usec, irqs %x (%x)\n", 2958c2ecf20Sopenharmony_ci command, 500000-c, irqs, cafe_readl(cafe, NAND_IRQ)); 2968c2ecf20Sopenharmony_ci } 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ci WARN_ON(cafe->ctl2 & (1<<30)); 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_ci switch (command) { 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci case NAND_CMD_CACHEDPROG: 3038c2ecf20Sopenharmony_ci case NAND_CMD_PAGEPROG: 3048c2ecf20Sopenharmony_ci case NAND_CMD_ERASE1: 3058c2ecf20Sopenharmony_ci case NAND_CMD_ERASE2: 3068c2ecf20Sopenharmony_ci case NAND_CMD_SEQIN: 3078c2ecf20Sopenharmony_ci case NAND_CMD_RNDIN: 3088c2ecf20Sopenharmony_ci case NAND_CMD_STATUS: 3098c2ecf20Sopenharmony_ci case NAND_CMD_RNDOUT: 3108c2ecf20Sopenharmony_ci cafe_writel(cafe, cafe->ctl2, NAND_CTRL2); 3118c2ecf20Sopenharmony_ci return; 3128c2ecf20Sopenharmony_ci } 3138c2ecf20Sopenharmony_ci nand_wait_ready(chip); 3148c2ecf20Sopenharmony_ci cafe_writel(cafe, cafe->ctl2, NAND_CTRL2); 3158c2ecf20Sopenharmony_ci} 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_cistatic void cafe_select_chip(struct nand_chip *chip, int chipnr) 3188c2ecf20Sopenharmony_ci{ 3198c2ecf20Sopenharmony_ci struct cafe_priv *cafe = nand_get_controller_data(chip); 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_ci cafe_dev_dbg(&cafe->pdev->dev, "select_chip %d\n", chipnr); 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ci /* Mask the appropriate bit into the stored value of ctl1 3248c2ecf20Sopenharmony_ci which will be used by cafe_nand_cmdfunc() */ 3258c2ecf20Sopenharmony_ci if (chipnr) 3268c2ecf20Sopenharmony_ci cafe->ctl1 |= CTRL1_CHIPSELECT; 3278c2ecf20Sopenharmony_ci else 3288c2ecf20Sopenharmony_ci cafe->ctl1 &= ~CTRL1_CHIPSELECT; 3298c2ecf20Sopenharmony_ci} 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_cistatic irqreturn_t cafe_nand_interrupt(int irq, void *id) 3328c2ecf20Sopenharmony_ci{ 3338c2ecf20Sopenharmony_ci struct mtd_info *mtd = id; 3348c2ecf20Sopenharmony_ci struct nand_chip *chip = mtd_to_nand(mtd); 3358c2ecf20Sopenharmony_ci struct cafe_priv *cafe = nand_get_controller_data(chip); 3368c2ecf20Sopenharmony_ci uint32_t irqs = cafe_readl(cafe, NAND_IRQ); 3378c2ecf20Sopenharmony_ci cafe_writel(cafe, irqs & ~0x90000000, NAND_IRQ); 3388c2ecf20Sopenharmony_ci if (!irqs) 3398c2ecf20Sopenharmony_ci return IRQ_NONE; 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci cafe_dev_dbg(&cafe->pdev->dev, "irq, bits %x (%x)\n", irqs, cafe_readl(cafe, NAND_IRQ)); 3428c2ecf20Sopenharmony_ci return IRQ_HANDLED; 3438c2ecf20Sopenharmony_ci} 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_cistatic int cafe_nand_write_oob(struct nand_chip *chip, int page) 3468c2ecf20Sopenharmony_ci{ 3478c2ecf20Sopenharmony_ci struct mtd_info *mtd = nand_to_mtd(chip); 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_ci return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi, 3508c2ecf20Sopenharmony_ci mtd->oobsize); 3518c2ecf20Sopenharmony_ci} 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci/* Don't use -- use nand_read_oob_std for now */ 3548c2ecf20Sopenharmony_cistatic int cafe_nand_read_oob(struct nand_chip *chip, int page) 3558c2ecf20Sopenharmony_ci{ 3568c2ecf20Sopenharmony_ci struct mtd_info *mtd = nand_to_mtd(chip); 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize); 3598c2ecf20Sopenharmony_ci} 3608c2ecf20Sopenharmony_ci/** 3618c2ecf20Sopenharmony_ci * cafe_nand_read_page_syndrome - [REPLACEABLE] hardware ecc syndrome based page read 3628c2ecf20Sopenharmony_ci * @mtd: mtd info structure 3638c2ecf20Sopenharmony_ci * @chip: nand chip info structure 3648c2ecf20Sopenharmony_ci * @buf: buffer to store read data 3658c2ecf20Sopenharmony_ci * @oob_required: caller expects OOB data read to chip->oob_poi 3668c2ecf20Sopenharmony_ci * 3678c2ecf20Sopenharmony_ci * The hw generator calculates the error syndrome automatically. Therefore 3688c2ecf20Sopenharmony_ci * we need a special oob layout and handling. 3698c2ecf20Sopenharmony_ci */ 3708c2ecf20Sopenharmony_cistatic int cafe_nand_read_page(struct nand_chip *chip, uint8_t *buf, 3718c2ecf20Sopenharmony_ci int oob_required, int page) 3728c2ecf20Sopenharmony_ci{ 3738c2ecf20Sopenharmony_ci struct mtd_info *mtd = nand_to_mtd(chip); 3748c2ecf20Sopenharmony_ci struct cafe_priv *cafe = nand_get_controller_data(chip); 3758c2ecf20Sopenharmony_ci unsigned int max_bitflips = 0; 3768c2ecf20Sopenharmony_ci 3778c2ecf20Sopenharmony_ci cafe_dev_dbg(&cafe->pdev->dev, "ECC result %08x SYN1,2 %08x\n", 3788c2ecf20Sopenharmony_ci cafe_readl(cafe, NAND_ECC_RESULT), 3798c2ecf20Sopenharmony_ci cafe_readl(cafe, NAND_ECC_SYN01)); 3808c2ecf20Sopenharmony_ci 3818c2ecf20Sopenharmony_ci nand_read_page_op(chip, page, 0, buf, mtd->writesize); 3828c2ecf20Sopenharmony_ci chip->legacy.read_buf(chip, chip->oob_poi, mtd->oobsize); 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci if (checkecc && cafe_readl(cafe, NAND_ECC_RESULT) & (1<<18)) { 3858c2ecf20Sopenharmony_ci unsigned short syn[8], pat[4]; 3868c2ecf20Sopenharmony_ci int pos[4]; 3878c2ecf20Sopenharmony_ci u8 *oob = chip->oob_poi; 3888c2ecf20Sopenharmony_ci int i, n; 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_ci for (i=0; i<8; i+=2) { 3918c2ecf20Sopenharmony_ci uint32_t tmp = cafe_readl(cafe, NAND_ECC_SYN01 + (i*2)); 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci syn[i] = cafe->rs->codec->index_of[tmp & 0xfff]; 3948c2ecf20Sopenharmony_ci syn[i+1] = cafe->rs->codec->index_of[(tmp >> 16) & 0xfff]; 3958c2ecf20Sopenharmony_ci } 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ci n = decode_rs16(cafe->rs, NULL, NULL, 1367, syn, 0, pos, 0, 3988c2ecf20Sopenharmony_ci pat); 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_ci for (i = 0; i < n; i++) { 4018c2ecf20Sopenharmony_ci int p = pos[i]; 4028c2ecf20Sopenharmony_ci 4038c2ecf20Sopenharmony_ci /* The 12-bit symbols are mapped to bytes here */ 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci if (p > 1374) { 4068c2ecf20Sopenharmony_ci /* out of range */ 4078c2ecf20Sopenharmony_ci n = -1374; 4088c2ecf20Sopenharmony_ci } else if (p == 0) { 4098c2ecf20Sopenharmony_ci /* high four bits do not correspond to data */ 4108c2ecf20Sopenharmony_ci if (pat[i] > 0xff) 4118c2ecf20Sopenharmony_ci n = -2048; 4128c2ecf20Sopenharmony_ci else 4138c2ecf20Sopenharmony_ci buf[0] ^= pat[i]; 4148c2ecf20Sopenharmony_ci } else if (p == 1365) { 4158c2ecf20Sopenharmony_ci buf[2047] ^= pat[i] >> 4; 4168c2ecf20Sopenharmony_ci oob[0] ^= pat[i] << 4; 4178c2ecf20Sopenharmony_ci } else if (p > 1365) { 4188c2ecf20Sopenharmony_ci if ((p & 1) == 1) { 4198c2ecf20Sopenharmony_ci oob[3*p/2 - 2048] ^= pat[i] >> 4; 4208c2ecf20Sopenharmony_ci oob[3*p/2 - 2047] ^= pat[i] << 4; 4218c2ecf20Sopenharmony_ci } else { 4228c2ecf20Sopenharmony_ci oob[3*p/2 - 2049] ^= pat[i] >> 8; 4238c2ecf20Sopenharmony_ci oob[3*p/2 - 2048] ^= pat[i]; 4248c2ecf20Sopenharmony_ci } 4258c2ecf20Sopenharmony_ci } else if ((p & 1) == 1) { 4268c2ecf20Sopenharmony_ci buf[3*p/2] ^= pat[i] >> 4; 4278c2ecf20Sopenharmony_ci buf[3*p/2 + 1] ^= pat[i] << 4; 4288c2ecf20Sopenharmony_ci } else { 4298c2ecf20Sopenharmony_ci buf[3*p/2 - 1] ^= pat[i] >> 8; 4308c2ecf20Sopenharmony_ci buf[3*p/2] ^= pat[i]; 4318c2ecf20Sopenharmony_ci } 4328c2ecf20Sopenharmony_ci } 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_ci if (n < 0) { 4358c2ecf20Sopenharmony_ci dev_dbg(&cafe->pdev->dev, "Failed to correct ECC at %08x\n", 4368c2ecf20Sopenharmony_ci cafe_readl(cafe, NAND_ADDR2) * 2048); 4378c2ecf20Sopenharmony_ci for (i = 0; i < 0x5c; i += 4) 4388c2ecf20Sopenharmony_ci printk("Register %x: %08x\n", i, readl(cafe->mmio + i)); 4398c2ecf20Sopenharmony_ci mtd->ecc_stats.failed++; 4408c2ecf20Sopenharmony_ci } else { 4418c2ecf20Sopenharmony_ci dev_dbg(&cafe->pdev->dev, "Corrected %d symbol errors\n", n); 4428c2ecf20Sopenharmony_ci mtd->ecc_stats.corrected += n; 4438c2ecf20Sopenharmony_ci max_bitflips = max_t(unsigned int, max_bitflips, n); 4448c2ecf20Sopenharmony_ci } 4458c2ecf20Sopenharmony_ci } 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_ci return max_bitflips; 4488c2ecf20Sopenharmony_ci} 4498c2ecf20Sopenharmony_ci 4508c2ecf20Sopenharmony_cistatic int cafe_ooblayout_ecc(struct mtd_info *mtd, int section, 4518c2ecf20Sopenharmony_ci struct mtd_oob_region *oobregion) 4528c2ecf20Sopenharmony_ci{ 4538c2ecf20Sopenharmony_ci struct nand_chip *chip = mtd_to_nand(mtd); 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci if (section) 4568c2ecf20Sopenharmony_ci return -ERANGE; 4578c2ecf20Sopenharmony_ci 4588c2ecf20Sopenharmony_ci oobregion->offset = 0; 4598c2ecf20Sopenharmony_ci oobregion->length = chip->ecc.total; 4608c2ecf20Sopenharmony_ci 4618c2ecf20Sopenharmony_ci return 0; 4628c2ecf20Sopenharmony_ci} 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_cistatic int cafe_ooblayout_free(struct mtd_info *mtd, int section, 4658c2ecf20Sopenharmony_ci struct mtd_oob_region *oobregion) 4668c2ecf20Sopenharmony_ci{ 4678c2ecf20Sopenharmony_ci struct nand_chip *chip = mtd_to_nand(mtd); 4688c2ecf20Sopenharmony_ci 4698c2ecf20Sopenharmony_ci if (section) 4708c2ecf20Sopenharmony_ci return -ERANGE; 4718c2ecf20Sopenharmony_ci 4728c2ecf20Sopenharmony_ci oobregion->offset = chip->ecc.total; 4738c2ecf20Sopenharmony_ci oobregion->length = mtd->oobsize - chip->ecc.total; 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci return 0; 4768c2ecf20Sopenharmony_ci} 4778c2ecf20Sopenharmony_ci 4788c2ecf20Sopenharmony_cistatic const struct mtd_ooblayout_ops cafe_ooblayout_ops = { 4798c2ecf20Sopenharmony_ci .ecc = cafe_ooblayout_ecc, 4808c2ecf20Sopenharmony_ci .free = cafe_ooblayout_free, 4818c2ecf20Sopenharmony_ci}; 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_ci/* Ick. The BBT code really ought to be able to work this bit out 4848c2ecf20Sopenharmony_ci for itself from the above, at least for the 2KiB case */ 4858c2ecf20Sopenharmony_cistatic uint8_t cafe_bbt_pattern_2048[] = { 'B', 'b', 't', '0' }; 4868c2ecf20Sopenharmony_cistatic uint8_t cafe_mirror_pattern_2048[] = { '1', 't', 'b', 'B' }; 4878c2ecf20Sopenharmony_ci 4888c2ecf20Sopenharmony_cistatic uint8_t cafe_bbt_pattern_512[] = { 0xBB }; 4898c2ecf20Sopenharmony_cistatic uint8_t cafe_mirror_pattern_512[] = { 0xBC }; 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_cistatic struct nand_bbt_descr cafe_bbt_main_descr_2048 = { 4938c2ecf20Sopenharmony_ci .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE 4948c2ecf20Sopenharmony_ci | NAND_BBT_2BIT | NAND_BBT_VERSION, 4958c2ecf20Sopenharmony_ci .offs = 14, 4968c2ecf20Sopenharmony_ci .len = 4, 4978c2ecf20Sopenharmony_ci .veroffs = 18, 4988c2ecf20Sopenharmony_ci .maxblocks = 4, 4998c2ecf20Sopenharmony_ci .pattern = cafe_bbt_pattern_2048 5008c2ecf20Sopenharmony_ci}; 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_cistatic struct nand_bbt_descr cafe_bbt_mirror_descr_2048 = { 5038c2ecf20Sopenharmony_ci .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE 5048c2ecf20Sopenharmony_ci | NAND_BBT_2BIT | NAND_BBT_VERSION, 5058c2ecf20Sopenharmony_ci .offs = 14, 5068c2ecf20Sopenharmony_ci .len = 4, 5078c2ecf20Sopenharmony_ci .veroffs = 18, 5088c2ecf20Sopenharmony_ci .maxblocks = 4, 5098c2ecf20Sopenharmony_ci .pattern = cafe_mirror_pattern_2048 5108c2ecf20Sopenharmony_ci}; 5118c2ecf20Sopenharmony_ci 5128c2ecf20Sopenharmony_cistatic struct nand_bbt_descr cafe_bbt_main_descr_512 = { 5138c2ecf20Sopenharmony_ci .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE 5148c2ecf20Sopenharmony_ci | NAND_BBT_2BIT | NAND_BBT_VERSION, 5158c2ecf20Sopenharmony_ci .offs = 14, 5168c2ecf20Sopenharmony_ci .len = 1, 5178c2ecf20Sopenharmony_ci .veroffs = 15, 5188c2ecf20Sopenharmony_ci .maxblocks = 4, 5198c2ecf20Sopenharmony_ci .pattern = cafe_bbt_pattern_512 5208c2ecf20Sopenharmony_ci}; 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_cistatic struct nand_bbt_descr cafe_bbt_mirror_descr_512 = { 5238c2ecf20Sopenharmony_ci .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE 5248c2ecf20Sopenharmony_ci | NAND_BBT_2BIT | NAND_BBT_VERSION, 5258c2ecf20Sopenharmony_ci .offs = 14, 5268c2ecf20Sopenharmony_ci .len = 1, 5278c2ecf20Sopenharmony_ci .veroffs = 15, 5288c2ecf20Sopenharmony_ci .maxblocks = 4, 5298c2ecf20Sopenharmony_ci .pattern = cafe_mirror_pattern_512 5308c2ecf20Sopenharmony_ci}; 5318c2ecf20Sopenharmony_ci 5328c2ecf20Sopenharmony_ci 5338c2ecf20Sopenharmony_cistatic int cafe_nand_write_page_lowlevel(struct nand_chip *chip, 5348c2ecf20Sopenharmony_ci const uint8_t *buf, int oob_required, 5358c2ecf20Sopenharmony_ci int page) 5368c2ecf20Sopenharmony_ci{ 5378c2ecf20Sopenharmony_ci struct mtd_info *mtd = nand_to_mtd(chip); 5388c2ecf20Sopenharmony_ci struct cafe_priv *cafe = nand_get_controller_data(chip); 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_ci nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize); 5418c2ecf20Sopenharmony_ci chip->legacy.write_buf(chip, chip->oob_poi, mtd->oobsize); 5428c2ecf20Sopenharmony_ci 5438c2ecf20Sopenharmony_ci /* Set up ECC autogeneration */ 5448c2ecf20Sopenharmony_ci cafe->ctl2 |= (1<<30); 5458c2ecf20Sopenharmony_ci 5468c2ecf20Sopenharmony_ci return nand_prog_page_end_op(chip); 5478c2ecf20Sopenharmony_ci} 5488c2ecf20Sopenharmony_ci 5498c2ecf20Sopenharmony_ci/* F_2[X]/(X**6+X+1) */ 5508c2ecf20Sopenharmony_cistatic unsigned short gf64_mul(u8 a, u8 b) 5518c2ecf20Sopenharmony_ci{ 5528c2ecf20Sopenharmony_ci u8 c; 5538c2ecf20Sopenharmony_ci unsigned int i; 5548c2ecf20Sopenharmony_ci 5558c2ecf20Sopenharmony_ci c = 0; 5568c2ecf20Sopenharmony_ci for (i = 0; i < 6; i++) { 5578c2ecf20Sopenharmony_ci if (a & 1) 5588c2ecf20Sopenharmony_ci c ^= b; 5598c2ecf20Sopenharmony_ci a >>= 1; 5608c2ecf20Sopenharmony_ci b <<= 1; 5618c2ecf20Sopenharmony_ci if ((b & 0x40) != 0) 5628c2ecf20Sopenharmony_ci b ^= 0x43; 5638c2ecf20Sopenharmony_ci } 5648c2ecf20Sopenharmony_ci 5658c2ecf20Sopenharmony_ci return c; 5668c2ecf20Sopenharmony_ci} 5678c2ecf20Sopenharmony_ci 5688c2ecf20Sopenharmony_ci/* F_64[X]/(X**2+X+A**-1) with A the generator of F_64[X] */ 5698c2ecf20Sopenharmony_cistatic u16 gf4096_mul(u16 a, u16 b) 5708c2ecf20Sopenharmony_ci{ 5718c2ecf20Sopenharmony_ci u8 ah, al, bh, bl, ch, cl; 5728c2ecf20Sopenharmony_ci 5738c2ecf20Sopenharmony_ci ah = a >> 6; 5748c2ecf20Sopenharmony_ci al = a & 0x3f; 5758c2ecf20Sopenharmony_ci bh = b >> 6; 5768c2ecf20Sopenharmony_ci bl = b & 0x3f; 5778c2ecf20Sopenharmony_ci 5788c2ecf20Sopenharmony_ci ch = gf64_mul(ah ^ al, bh ^ bl) ^ gf64_mul(al, bl); 5798c2ecf20Sopenharmony_ci cl = gf64_mul(gf64_mul(ah, bh), 0x21) ^ gf64_mul(al, bl); 5808c2ecf20Sopenharmony_ci 5818c2ecf20Sopenharmony_ci return (ch << 6) ^ cl; 5828c2ecf20Sopenharmony_ci} 5838c2ecf20Sopenharmony_ci 5848c2ecf20Sopenharmony_cistatic int cafe_mul(int x) 5858c2ecf20Sopenharmony_ci{ 5868c2ecf20Sopenharmony_ci if (x == 0) 5878c2ecf20Sopenharmony_ci return 1; 5888c2ecf20Sopenharmony_ci return gf4096_mul(x, 0xe01); 5898c2ecf20Sopenharmony_ci} 5908c2ecf20Sopenharmony_ci 5918c2ecf20Sopenharmony_cistatic int cafe_nand_attach_chip(struct nand_chip *chip) 5928c2ecf20Sopenharmony_ci{ 5938c2ecf20Sopenharmony_ci struct mtd_info *mtd = nand_to_mtd(chip); 5948c2ecf20Sopenharmony_ci struct cafe_priv *cafe = nand_get_controller_data(chip); 5958c2ecf20Sopenharmony_ci int err = 0; 5968c2ecf20Sopenharmony_ci 5978c2ecf20Sopenharmony_ci cafe->dmabuf = dma_alloc_coherent(&cafe->pdev->dev, 2112, 5988c2ecf20Sopenharmony_ci &cafe->dmaaddr, GFP_KERNEL); 5998c2ecf20Sopenharmony_ci if (!cafe->dmabuf) 6008c2ecf20Sopenharmony_ci return -ENOMEM; 6018c2ecf20Sopenharmony_ci 6028c2ecf20Sopenharmony_ci /* Set up DMA address */ 6038c2ecf20Sopenharmony_ci cafe_writel(cafe, lower_32_bits(cafe->dmaaddr), NAND_DMA_ADDR0); 6048c2ecf20Sopenharmony_ci cafe_writel(cafe, upper_32_bits(cafe->dmaaddr), NAND_DMA_ADDR1); 6058c2ecf20Sopenharmony_ci 6068c2ecf20Sopenharmony_ci cafe_dev_dbg(&cafe->pdev->dev, "Set DMA address to %x (virt %p)\n", 6078c2ecf20Sopenharmony_ci cafe_readl(cafe, NAND_DMA_ADDR0), cafe->dmabuf); 6088c2ecf20Sopenharmony_ci 6098c2ecf20Sopenharmony_ci /* Restore the DMA flag */ 6108c2ecf20Sopenharmony_ci cafe->usedma = usedma; 6118c2ecf20Sopenharmony_ci 6128c2ecf20Sopenharmony_ci cafe->ctl2 = BIT(27); /* Reed-Solomon ECC */ 6138c2ecf20Sopenharmony_ci if (mtd->writesize == 2048) 6148c2ecf20Sopenharmony_ci cafe->ctl2 |= BIT(29); /* 2KiB page size */ 6158c2ecf20Sopenharmony_ci 6168c2ecf20Sopenharmony_ci /* Set up ECC according to the type of chip we found */ 6178c2ecf20Sopenharmony_ci mtd_set_ooblayout(mtd, &cafe_ooblayout_ops); 6188c2ecf20Sopenharmony_ci if (mtd->writesize == 2048) { 6198c2ecf20Sopenharmony_ci cafe->nand.bbt_td = &cafe_bbt_main_descr_2048; 6208c2ecf20Sopenharmony_ci cafe->nand.bbt_md = &cafe_bbt_mirror_descr_2048; 6218c2ecf20Sopenharmony_ci } else if (mtd->writesize == 512) { 6228c2ecf20Sopenharmony_ci cafe->nand.bbt_td = &cafe_bbt_main_descr_512; 6238c2ecf20Sopenharmony_ci cafe->nand.bbt_md = &cafe_bbt_mirror_descr_512; 6248c2ecf20Sopenharmony_ci } else { 6258c2ecf20Sopenharmony_ci dev_warn(&cafe->pdev->dev, 6268c2ecf20Sopenharmony_ci "Unexpected NAND flash writesize %d. Aborting\n", 6278c2ecf20Sopenharmony_ci mtd->writesize); 6288c2ecf20Sopenharmony_ci err = -ENOTSUPP; 6298c2ecf20Sopenharmony_ci goto out_free_dma; 6308c2ecf20Sopenharmony_ci } 6318c2ecf20Sopenharmony_ci 6328c2ecf20Sopenharmony_ci cafe->nand.ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST; 6338c2ecf20Sopenharmony_ci cafe->nand.ecc.placement = NAND_ECC_PLACEMENT_INTERLEAVED; 6348c2ecf20Sopenharmony_ci cafe->nand.ecc.size = mtd->writesize; 6358c2ecf20Sopenharmony_ci cafe->nand.ecc.bytes = 14; 6368c2ecf20Sopenharmony_ci cafe->nand.ecc.strength = 4; 6378c2ecf20Sopenharmony_ci cafe->nand.ecc.write_page = cafe_nand_write_page_lowlevel; 6388c2ecf20Sopenharmony_ci cafe->nand.ecc.write_oob = cafe_nand_write_oob; 6398c2ecf20Sopenharmony_ci cafe->nand.ecc.read_page = cafe_nand_read_page; 6408c2ecf20Sopenharmony_ci cafe->nand.ecc.read_oob = cafe_nand_read_oob; 6418c2ecf20Sopenharmony_ci 6428c2ecf20Sopenharmony_ci return 0; 6438c2ecf20Sopenharmony_ci 6448c2ecf20Sopenharmony_ci out_free_dma: 6458c2ecf20Sopenharmony_ci dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr); 6468c2ecf20Sopenharmony_ci 6478c2ecf20Sopenharmony_ci return err; 6488c2ecf20Sopenharmony_ci} 6498c2ecf20Sopenharmony_ci 6508c2ecf20Sopenharmony_cistatic void cafe_nand_detach_chip(struct nand_chip *chip) 6518c2ecf20Sopenharmony_ci{ 6528c2ecf20Sopenharmony_ci struct cafe_priv *cafe = nand_get_controller_data(chip); 6538c2ecf20Sopenharmony_ci 6548c2ecf20Sopenharmony_ci dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr); 6558c2ecf20Sopenharmony_ci} 6568c2ecf20Sopenharmony_ci 6578c2ecf20Sopenharmony_cistatic const struct nand_controller_ops cafe_nand_controller_ops = { 6588c2ecf20Sopenharmony_ci .attach_chip = cafe_nand_attach_chip, 6598c2ecf20Sopenharmony_ci .detach_chip = cafe_nand_detach_chip, 6608c2ecf20Sopenharmony_ci}; 6618c2ecf20Sopenharmony_ci 6628c2ecf20Sopenharmony_cistatic int cafe_nand_probe(struct pci_dev *pdev, 6638c2ecf20Sopenharmony_ci const struct pci_device_id *ent) 6648c2ecf20Sopenharmony_ci{ 6658c2ecf20Sopenharmony_ci struct mtd_info *mtd; 6668c2ecf20Sopenharmony_ci struct cafe_priv *cafe; 6678c2ecf20Sopenharmony_ci uint32_t ctrl; 6688c2ecf20Sopenharmony_ci int err = 0; 6698c2ecf20Sopenharmony_ci 6708c2ecf20Sopenharmony_ci /* Very old versions shared the same PCI ident for all three 6718c2ecf20Sopenharmony_ci functions on the chip. Verify the class too... */ 6728c2ecf20Sopenharmony_ci if ((pdev->class >> 8) != PCI_CLASS_MEMORY_FLASH) 6738c2ecf20Sopenharmony_ci return -ENODEV; 6748c2ecf20Sopenharmony_ci 6758c2ecf20Sopenharmony_ci err = pci_enable_device(pdev); 6768c2ecf20Sopenharmony_ci if (err) 6778c2ecf20Sopenharmony_ci return err; 6788c2ecf20Sopenharmony_ci 6798c2ecf20Sopenharmony_ci pci_set_master(pdev); 6808c2ecf20Sopenharmony_ci 6818c2ecf20Sopenharmony_ci cafe = kzalloc(sizeof(*cafe), GFP_KERNEL); 6828c2ecf20Sopenharmony_ci if (!cafe) 6838c2ecf20Sopenharmony_ci return -ENOMEM; 6848c2ecf20Sopenharmony_ci 6858c2ecf20Sopenharmony_ci mtd = nand_to_mtd(&cafe->nand); 6868c2ecf20Sopenharmony_ci mtd->dev.parent = &pdev->dev; 6878c2ecf20Sopenharmony_ci nand_set_controller_data(&cafe->nand, cafe); 6888c2ecf20Sopenharmony_ci 6898c2ecf20Sopenharmony_ci cafe->pdev = pdev; 6908c2ecf20Sopenharmony_ci cafe->mmio = pci_iomap(pdev, 0, 0); 6918c2ecf20Sopenharmony_ci if (!cafe->mmio) { 6928c2ecf20Sopenharmony_ci dev_warn(&pdev->dev, "failed to iomap\n"); 6938c2ecf20Sopenharmony_ci err = -ENOMEM; 6948c2ecf20Sopenharmony_ci goto out_free_mtd; 6958c2ecf20Sopenharmony_ci } 6968c2ecf20Sopenharmony_ci 6978c2ecf20Sopenharmony_ci cafe->rs = init_rs_non_canonical(12, &cafe_mul, 0, 1, 8); 6988c2ecf20Sopenharmony_ci if (!cafe->rs) { 6998c2ecf20Sopenharmony_ci err = -ENOMEM; 7008c2ecf20Sopenharmony_ci goto out_ior; 7018c2ecf20Sopenharmony_ci } 7028c2ecf20Sopenharmony_ci 7038c2ecf20Sopenharmony_ci cafe->nand.legacy.cmdfunc = cafe_nand_cmdfunc; 7048c2ecf20Sopenharmony_ci cafe->nand.legacy.dev_ready = cafe_device_ready; 7058c2ecf20Sopenharmony_ci cafe->nand.legacy.read_byte = cafe_read_byte; 7068c2ecf20Sopenharmony_ci cafe->nand.legacy.read_buf = cafe_read_buf; 7078c2ecf20Sopenharmony_ci cafe->nand.legacy.write_buf = cafe_write_buf; 7088c2ecf20Sopenharmony_ci cafe->nand.legacy.select_chip = cafe_select_chip; 7098c2ecf20Sopenharmony_ci cafe->nand.legacy.set_features = nand_get_set_features_notsupp; 7108c2ecf20Sopenharmony_ci cafe->nand.legacy.get_features = nand_get_set_features_notsupp; 7118c2ecf20Sopenharmony_ci 7128c2ecf20Sopenharmony_ci cafe->nand.legacy.chip_delay = 0; 7138c2ecf20Sopenharmony_ci 7148c2ecf20Sopenharmony_ci /* Enable the following for a flash based bad block table */ 7158c2ecf20Sopenharmony_ci cafe->nand.bbt_options = NAND_BBT_USE_FLASH; 7168c2ecf20Sopenharmony_ci 7178c2ecf20Sopenharmony_ci if (skipbbt) 7188c2ecf20Sopenharmony_ci cafe->nand.options |= NAND_SKIP_BBTSCAN | NAND_NO_BBM_QUIRK; 7198c2ecf20Sopenharmony_ci 7208c2ecf20Sopenharmony_ci if (numtimings && numtimings != 3) { 7218c2ecf20Sopenharmony_ci dev_warn(&cafe->pdev->dev, "%d timing register values ignored; precisely three are required\n", numtimings); 7228c2ecf20Sopenharmony_ci } 7238c2ecf20Sopenharmony_ci 7248c2ecf20Sopenharmony_ci if (numtimings == 3) { 7258c2ecf20Sopenharmony_ci cafe_dev_dbg(&cafe->pdev->dev, "Using provided timings (%08x %08x %08x)\n", 7268c2ecf20Sopenharmony_ci timing[0], timing[1], timing[2]); 7278c2ecf20Sopenharmony_ci } else { 7288c2ecf20Sopenharmony_ci timing[0] = cafe_readl(cafe, NAND_TIMING1); 7298c2ecf20Sopenharmony_ci timing[1] = cafe_readl(cafe, NAND_TIMING2); 7308c2ecf20Sopenharmony_ci timing[2] = cafe_readl(cafe, NAND_TIMING3); 7318c2ecf20Sopenharmony_ci 7328c2ecf20Sopenharmony_ci if (timing[0] | timing[1] | timing[2]) { 7338c2ecf20Sopenharmony_ci cafe_dev_dbg(&cafe->pdev->dev, "Timing registers already set (%08x %08x %08x)\n", 7348c2ecf20Sopenharmony_ci timing[0], timing[1], timing[2]); 7358c2ecf20Sopenharmony_ci } else { 7368c2ecf20Sopenharmony_ci dev_warn(&cafe->pdev->dev, "Timing registers unset; using most conservative defaults\n"); 7378c2ecf20Sopenharmony_ci timing[0] = timing[1] = timing[2] = 0xffffffff; 7388c2ecf20Sopenharmony_ci } 7398c2ecf20Sopenharmony_ci } 7408c2ecf20Sopenharmony_ci 7418c2ecf20Sopenharmony_ci /* Start off by resetting the NAND controller completely */ 7428c2ecf20Sopenharmony_ci cafe_writel(cafe, 1, NAND_RESET); 7438c2ecf20Sopenharmony_ci cafe_writel(cafe, 0, NAND_RESET); 7448c2ecf20Sopenharmony_ci 7458c2ecf20Sopenharmony_ci cafe_writel(cafe, timing[0], NAND_TIMING1); 7468c2ecf20Sopenharmony_ci cafe_writel(cafe, timing[1], NAND_TIMING2); 7478c2ecf20Sopenharmony_ci cafe_writel(cafe, timing[2], NAND_TIMING3); 7488c2ecf20Sopenharmony_ci 7498c2ecf20Sopenharmony_ci cafe_writel(cafe, 0xffffffff, NAND_IRQ_MASK); 7508c2ecf20Sopenharmony_ci err = request_irq(pdev->irq, &cafe_nand_interrupt, IRQF_SHARED, 7518c2ecf20Sopenharmony_ci "CAFE NAND", mtd); 7528c2ecf20Sopenharmony_ci if (err) { 7538c2ecf20Sopenharmony_ci dev_warn(&pdev->dev, "Could not register IRQ %d\n", pdev->irq); 7548c2ecf20Sopenharmony_ci goto out_free_rs; 7558c2ecf20Sopenharmony_ci } 7568c2ecf20Sopenharmony_ci 7578c2ecf20Sopenharmony_ci /* Disable master reset, enable NAND clock */ 7588c2ecf20Sopenharmony_ci ctrl = cafe_readl(cafe, GLOBAL_CTRL); 7598c2ecf20Sopenharmony_ci ctrl &= 0xffffeff0; 7608c2ecf20Sopenharmony_ci ctrl |= 0x00007000; 7618c2ecf20Sopenharmony_ci cafe_writel(cafe, ctrl | 0x05, GLOBAL_CTRL); 7628c2ecf20Sopenharmony_ci cafe_writel(cafe, ctrl | 0x0a, GLOBAL_CTRL); 7638c2ecf20Sopenharmony_ci cafe_writel(cafe, 0, NAND_DMA_CTRL); 7648c2ecf20Sopenharmony_ci 7658c2ecf20Sopenharmony_ci cafe_writel(cafe, 0x7006, GLOBAL_CTRL); 7668c2ecf20Sopenharmony_ci cafe_writel(cafe, 0x700a, GLOBAL_CTRL); 7678c2ecf20Sopenharmony_ci 7688c2ecf20Sopenharmony_ci /* Enable NAND IRQ in global IRQ mask register */ 7698c2ecf20Sopenharmony_ci cafe_writel(cafe, 0x80000007, GLOBAL_IRQ_MASK); 7708c2ecf20Sopenharmony_ci cafe_dev_dbg(&cafe->pdev->dev, "Control %x, IRQ mask %x\n", 7718c2ecf20Sopenharmony_ci cafe_readl(cafe, GLOBAL_CTRL), 7728c2ecf20Sopenharmony_ci cafe_readl(cafe, GLOBAL_IRQ_MASK)); 7738c2ecf20Sopenharmony_ci 7748c2ecf20Sopenharmony_ci /* Do not use the DMA during the NAND identification */ 7758c2ecf20Sopenharmony_ci cafe->usedma = 0; 7768c2ecf20Sopenharmony_ci 7778c2ecf20Sopenharmony_ci /* Scan to find existence of the device */ 7788c2ecf20Sopenharmony_ci cafe->nand.legacy.dummy_controller.ops = &cafe_nand_controller_ops; 7798c2ecf20Sopenharmony_ci err = nand_scan(&cafe->nand, 2); 7808c2ecf20Sopenharmony_ci if (err) 7818c2ecf20Sopenharmony_ci goto out_irq; 7828c2ecf20Sopenharmony_ci 7838c2ecf20Sopenharmony_ci pci_set_drvdata(pdev, mtd); 7848c2ecf20Sopenharmony_ci 7858c2ecf20Sopenharmony_ci mtd->name = "cafe_nand"; 7868c2ecf20Sopenharmony_ci err = mtd_device_parse_register(mtd, part_probes, NULL, NULL, 0); 7878c2ecf20Sopenharmony_ci if (err) 7888c2ecf20Sopenharmony_ci goto out_cleanup_nand; 7898c2ecf20Sopenharmony_ci 7908c2ecf20Sopenharmony_ci goto out; 7918c2ecf20Sopenharmony_ci 7928c2ecf20Sopenharmony_ci out_cleanup_nand: 7938c2ecf20Sopenharmony_ci nand_cleanup(&cafe->nand); 7948c2ecf20Sopenharmony_ci out_irq: 7958c2ecf20Sopenharmony_ci /* Disable NAND IRQ in global IRQ mask register */ 7968c2ecf20Sopenharmony_ci cafe_writel(cafe, ~1 & cafe_readl(cafe, GLOBAL_IRQ_MASK), GLOBAL_IRQ_MASK); 7978c2ecf20Sopenharmony_ci free_irq(pdev->irq, mtd); 7988c2ecf20Sopenharmony_ci out_free_rs: 7998c2ecf20Sopenharmony_ci free_rs(cafe->rs); 8008c2ecf20Sopenharmony_ci out_ior: 8018c2ecf20Sopenharmony_ci pci_iounmap(pdev, cafe->mmio); 8028c2ecf20Sopenharmony_ci out_free_mtd: 8038c2ecf20Sopenharmony_ci kfree(cafe); 8048c2ecf20Sopenharmony_ci out: 8058c2ecf20Sopenharmony_ci return err; 8068c2ecf20Sopenharmony_ci} 8078c2ecf20Sopenharmony_ci 8088c2ecf20Sopenharmony_cistatic void cafe_nand_remove(struct pci_dev *pdev) 8098c2ecf20Sopenharmony_ci{ 8108c2ecf20Sopenharmony_ci struct mtd_info *mtd = pci_get_drvdata(pdev); 8118c2ecf20Sopenharmony_ci struct nand_chip *chip = mtd_to_nand(mtd); 8128c2ecf20Sopenharmony_ci struct cafe_priv *cafe = nand_get_controller_data(chip); 8138c2ecf20Sopenharmony_ci int ret; 8148c2ecf20Sopenharmony_ci 8158c2ecf20Sopenharmony_ci /* Disable NAND IRQ in global IRQ mask register */ 8168c2ecf20Sopenharmony_ci cafe_writel(cafe, ~1 & cafe_readl(cafe, GLOBAL_IRQ_MASK), GLOBAL_IRQ_MASK); 8178c2ecf20Sopenharmony_ci free_irq(pdev->irq, mtd); 8188c2ecf20Sopenharmony_ci ret = mtd_device_unregister(mtd); 8198c2ecf20Sopenharmony_ci WARN_ON(ret); 8208c2ecf20Sopenharmony_ci nand_cleanup(chip); 8218c2ecf20Sopenharmony_ci free_rs(cafe->rs); 8228c2ecf20Sopenharmony_ci pci_iounmap(pdev, cafe->mmio); 8238c2ecf20Sopenharmony_ci dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr); 8248c2ecf20Sopenharmony_ci kfree(cafe); 8258c2ecf20Sopenharmony_ci} 8268c2ecf20Sopenharmony_ci 8278c2ecf20Sopenharmony_cistatic const struct pci_device_id cafe_nand_tbl[] = { 8288c2ecf20Sopenharmony_ci { PCI_VENDOR_ID_MARVELL, PCI_DEVICE_ID_MARVELL_88ALP01_NAND, 8298c2ecf20Sopenharmony_ci PCI_ANY_ID, PCI_ANY_ID }, 8308c2ecf20Sopenharmony_ci { } 8318c2ecf20Sopenharmony_ci}; 8328c2ecf20Sopenharmony_ci 8338c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, cafe_nand_tbl); 8348c2ecf20Sopenharmony_ci 8358c2ecf20Sopenharmony_cistatic int cafe_nand_resume(struct pci_dev *pdev) 8368c2ecf20Sopenharmony_ci{ 8378c2ecf20Sopenharmony_ci uint32_t ctrl; 8388c2ecf20Sopenharmony_ci struct mtd_info *mtd = pci_get_drvdata(pdev); 8398c2ecf20Sopenharmony_ci struct nand_chip *chip = mtd_to_nand(mtd); 8408c2ecf20Sopenharmony_ci struct cafe_priv *cafe = nand_get_controller_data(chip); 8418c2ecf20Sopenharmony_ci 8428c2ecf20Sopenharmony_ci /* Start off by resetting the NAND controller completely */ 8438c2ecf20Sopenharmony_ci cafe_writel(cafe, 1, NAND_RESET); 8448c2ecf20Sopenharmony_ci cafe_writel(cafe, 0, NAND_RESET); 8458c2ecf20Sopenharmony_ci cafe_writel(cafe, 0xffffffff, NAND_IRQ_MASK); 8468c2ecf20Sopenharmony_ci 8478c2ecf20Sopenharmony_ci /* Restore timing configuration */ 8488c2ecf20Sopenharmony_ci cafe_writel(cafe, timing[0], NAND_TIMING1); 8498c2ecf20Sopenharmony_ci cafe_writel(cafe, timing[1], NAND_TIMING2); 8508c2ecf20Sopenharmony_ci cafe_writel(cafe, timing[2], NAND_TIMING3); 8518c2ecf20Sopenharmony_ci 8528c2ecf20Sopenharmony_ci /* Disable master reset, enable NAND clock */ 8538c2ecf20Sopenharmony_ci ctrl = cafe_readl(cafe, GLOBAL_CTRL); 8548c2ecf20Sopenharmony_ci ctrl &= 0xffffeff0; 8558c2ecf20Sopenharmony_ci ctrl |= 0x00007000; 8568c2ecf20Sopenharmony_ci cafe_writel(cafe, ctrl | 0x05, GLOBAL_CTRL); 8578c2ecf20Sopenharmony_ci cafe_writel(cafe, ctrl | 0x0a, GLOBAL_CTRL); 8588c2ecf20Sopenharmony_ci cafe_writel(cafe, 0, NAND_DMA_CTRL); 8598c2ecf20Sopenharmony_ci cafe_writel(cafe, 0x7006, GLOBAL_CTRL); 8608c2ecf20Sopenharmony_ci cafe_writel(cafe, 0x700a, GLOBAL_CTRL); 8618c2ecf20Sopenharmony_ci 8628c2ecf20Sopenharmony_ci /* Set up DMA address */ 8638c2ecf20Sopenharmony_ci cafe_writel(cafe, cafe->dmaaddr & 0xffffffff, NAND_DMA_ADDR0); 8648c2ecf20Sopenharmony_ci if (sizeof(cafe->dmaaddr) > 4) 8658c2ecf20Sopenharmony_ci /* Shift in two parts to shut the compiler up */ 8668c2ecf20Sopenharmony_ci cafe_writel(cafe, (cafe->dmaaddr >> 16) >> 16, NAND_DMA_ADDR1); 8678c2ecf20Sopenharmony_ci else 8688c2ecf20Sopenharmony_ci cafe_writel(cafe, 0, NAND_DMA_ADDR1); 8698c2ecf20Sopenharmony_ci 8708c2ecf20Sopenharmony_ci /* Enable NAND IRQ in global IRQ mask register */ 8718c2ecf20Sopenharmony_ci cafe_writel(cafe, 0x80000007, GLOBAL_IRQ_MASK); 8728c2ecf20Sopenharmony_ci return 0; 8738c2ecf20Sopenharmony_ci} 8748c2ecf20Sopenharmony_ci 8758c2ecf20Sopenharmony_cistatic struct pci_driver cafe_nand_pci_driver = { 8768c2ecf20Sopenharmony_ci .name = "CAFÉ NAND", 8778c2ecf20Sopenharmony_ci .id_table = cafe_nand_tbl, 8788c2ecf20Sopenharmony_ci .probe = cafe_nand_probe, 8798c2ecf20Sopenharmony_ci .remove = cafe_nand_remove, 8808c2ecf20Sopenharmony_ci .resume = cafe_nand_resume, 8818c2ecf20Sopenharmony_ci}; 8828c2ecf20Sopenharmony_ci 8838c2ecf20Sopenharmony_cimodule_pci_driver(cafe_nand_pci_driver); 8848c2ecf20Sopenharmony_ci 8858c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 8868c2ecf20Sopenharmony_ciMODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); 8878c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("NAND flash driver for OLPC CAFÉ chip"); 888