10b1039f0SRamuthevar Vadivel Murugan // SPDX-License-Identifier: GPL-2.0+ 20b1039f0SRamuthevar Vadivel Murugan /* Copyright (c) 2020 Intel Corporation. */ 30b1039f0SRamuthevar Vadivel Murugan 40b1039f0SRamuthevar Vadivel Murugan #include <linux/clk.h> 50b1039f0SRamuthevar Vadivel Murugan #include <linux/completion.h> 60b1039f0SRamuthevar Vadivel Murugan #include <linux/dmaengine.h> 70b1039f0SRamuthevar Vadivel Murugan #include <linux/dma-direction.h> 80b1039f0SRamuthevar Vadivel Murugan #include <linux/dma-mapping.h> 90b1039f0SRamuthevar Vadivel Murugan #include <linux/err.h> 100b1039f0SRamuthevar Vadivel Murugan #include <linux/init.h> 110b1039f0SRamuthevar Vadivel Murugan #include <linux/iopoll.h> 120b1039f0SRamuthevar Vadivel Murugan #include <linux/kernel.h> 130b1039f0SRamuthevar Vadivel Murugan #include <linux/module.h> 140b1039f0SRamuthevar Vadivel Murugan 150b1039f0SRamuthevar Vadivel Murugan #include <linux/mtd/mtd.h> 160b1039f0SRamuthevar Vadivel Murugan #include <linux/mtd/rawnand.h> 170b1039f0SRamuthevar Vadivel Murugan #include <linux/mtd/nand.h> 180b1039f0SRamuthevar Vadivel Murugan 190b1039f0SRamuthevar Vadivel Murugan #include <linux/platform_device.h> 200b1039f0SRamuthevar Vadivel Murugan #include <linux/sched.h> 210b1039f0SRamuthevar Vadivel Murugan #include <linux/slab.h> 220b1039f0SRamuthevar Vadivel Murugan #include <linux/types.h> 239ef347c3SDaniel Lezcano #include <linux/units.h> 240b1039f0SRamuthevar Vadivel Murugan #include <asm/unaligned.h> 250b1039f0SRamuthevar Vadivel Murugan 260b1039f0SRamuthevar Vadivel Murugan #define EBU_CLC 0x000 270b1039f0SRamuthevar Vadivel Murugan #define EBU_CLC_RST 0x00000000u 280b1039f0SRamuthevar Vadivel Murugan 290b1039f0SRamuthevar Vadivel Murugan #define EBU_ADDR_SEL(n) (0x020 + (n) * 4) 300b1039f0SRamuthevar Vadivel Murugan /* 5 bits 26:22 included for comparison in the ADDR_SELx */ 310b1039f0SRamuthevar Vadivel Murugan #define EBU_ADDR_MASK(x) ((x) << 4) 320b1039f0SRamuthevar Vadivel Murugan #define EBU_ADDR_SEL_REGEN 0x1 330b1039f0SRamuthevar Vadivel Murugan 340b1039f0SRamuthevar Vadivel Murugan #define EBU_BUSCON(n) (0x060 + (n) * 4) 350b1039f0SRamuthevar Vadivel Murugan #define EBU_BUSCON_CMULT_V4 0x1 360b1039f0SRamuthevar Vadivel Murugan #define EBU_BUSCON_RECOVC(n) ((n) << 2) 370b1039f0SRamuthevar Vadivel Murugan #define EBU_BUSCON_HOLDC(n) ((n) << 4) 380b1039f0SRamuthevar Vadivel Murugan #define EBU_BUSCON_WAITRDC(n) ((n) << 6) 390b1039f0SRamuthevar Vadivel Murugan #define EBU_BUSCON_WAITWRC(n) ((n) << 8) 400b1039f0SRamuthevar Vadivel Murugan #define EBU_BUSCON_BCGEN_CS 0x0 410b1039f0SRamuthevar Vadivel Murugan #define EBU_BUSCON_SETUP_EN BIT(22) 420b1039f0SRamuthevar Vadivel Murugan #define EBU_BUSCON_ALEC 0xC000 430b1039f0SRamuthevar Vadivel Murugan 440b1039f0SRamuthevar Vadivel Murugan #define EBU_CON 0x0B0 450b1039f0SRamuthevar Vadivel Murugan #define EBU_CON_NANDM_EN BIT(0) 460b1039f0SRamuthevar Vadivel Murugan #define EBU_CON_NANDM_DIS 0x0 470b1039f0SRamuthevar Vadivel Murugan #define EBU_CON_CSMUX_E_EN BIT(1) 480b1039f0SRamuthevar Vadivel Murugan #define EBU_CON_ALE_P_LOW BIT(2) 490b1039f0SRamuthevar Vadivel Murugan #define EBU_CON_CLE_P_LOW BIT(3) 500b1039f0SRamuthevar Vadivel Murugan #define EBU_CON_CS_P_LOW BIT(4) 510b1039f0SRamuthevar Vadivel Murugan #define EBU_CON_SE_P_LOW BIT(5) 520b1039f0SRamuthevar Vadivel Murugan #define EBU_CON_WP_P_LOW BIT(6) 530b1039f0SRamuthevar Vadivel Murugan #define EBU_CON_PRE_P_LOW BIT(7) 540b1039f0SRamuthevar Vadivel Murugan #define EBU_CON_IN_CS_S(n) ((n) << 8) 550b1039f0SRamuthevar Vadivel Murugan #define EBU_CON_OUT_CS_S(n) ((n) << 10) 560b1039f0SRamuthevar Vadivel Murugan #define EBU_CON_LAT_EN_CS_P ((0x3D) << 18) 570b1039f0SRamuthevar Vadivel Murugan 580b1039f0SRamuthevar Vadivel Murugan #define EBU_WAIT 0x0B4 590b1039f0SRamuthevar Vadivel Murugan #define EBU_WAIT_RDBY BIT(0) 600b1039f0SRamuthevar Vadivel Murugan #define EBU_WAIT_WR_C BIT(3) 610b1039f0SRamuthevar Vadivel Murugan 620b1039f0SRamuthevar Vadivel Murugan #define HSNAND_CTL1 0x110 630b1039f0SRamuthevar Vadivel Murugan #define HSNAND_CTL1_ADDR_SHIFT 24 640b1039f0SRamuthevar Vadivel Murugan 650b1039f0SRamuthevar Vadivel Murugan #define HSNAND_CTL2 0x114 660b1039f0SRamuthevar Vadivel Murugan #define HSNAND_CTL2_ADDR_SHIFT 8 670b1039f0SRamuthevar Vadivel Murugan #define HSNAND_CTL2_CYC_N_V5 (0x2 << 16) 680b1039f0SRamuthevar Vadivel Murugan 690b1039f0SRamuthevar Vadivel Murugan #define HSNAND_INT_MSK_CTL 0x124 700b1039f0SRamuthevar Vadivel Murugan #define HSNAND_INT_MSK_CTL_WR_C BIT(4) 710b1039f0SRamuthevar Vadivel Murugan 720b1039f0SRamuthevar Vadivel Murugan #define HSNAND_INT_STA 0x128 730b1039f0SRamuthevar Vadivel Murugan #define HSNAND_INT_STA_WR_C BIT(4) 740b1039f0SRamuthevar Vadivel Murugan 750b1039f0SRamuthevar Vadivel Murugan #define HSNAND_CTL 0x130 760b1039f0SRamuthevar Vadivel Murugan #define HSNAND_CTL_ENABLE_ECC BIT(0) 770b1039f0SRamuthevar Vadivel Murugan #define HSNAND_CTL_GO BIT(2) 780b1039f0SRamuthevar Vadivel Murugan #define HSNAND_CTL_CE_SEL_CS(n) BIT(3 + (n)) 790b1039f0SRamuthevar Vadivel Murugan #define HSNAND_CTL_RW_READ 0x0 800b1039f0SRamuthevar Vadivel Murugan #define HSNAND_CTL_RW_WRITE BIT(10) 810b1039f0SRamuthevar Vadivel Murugan #define HSNAND_CTL_ECC_OFF_V8TH BIT(11) 820b1039f0SRamuthevar Vadivel Murugan #define HSNAND_CTL_CKFF_EN 0x0 830b1039f0SRamuthevar Vadivel Murugan #define HSNAND_CTL_MSG_EN BIT(17) 840b1039f0SRamuthevar Vadivel Murugan 850b1039f0SRamuthevar Vadivel Murugan #define HSNAND_PARA0 0x13c 860b1039f0SRamuthevar Vadivel Murugan #define HSNAND_PARA0_PAGE_V8192 0x3 870b1039f0SRamuthevar Vadivel Murugan #define HSNAND_PARA0_PIB_V256 (0x3 << 4) 880b1039f0SRamuthevar Vadivel Murugan #define HSNAND_PARA0_BYP_EN_NP 0x0 890b1039f0SRamuthevar Vadivel Murugan #define HSNAND_PARA0_BYP_DEC_NP 0x0 900b1039f0SRamuthevar Vadivel Murugan #define HSNAND_PARA0_TYPE_ONFI BIT(18) 910b1039f0SRamuthevar Vadivel Murugan #define HSNAND_PARA0_ADEP_EN BIT(21) 920b1039f0SRamuthevar Vadivel Murugan 930b1039f0SRamuthevar Vadivel Murugan #define HSNAND_CMSG_0 0x150 940b1039f0SRamuthevar Vadivel Murugan #define HSNAND_CMSG_1 0x154 950b1039f0SRamuthevar Vadivel Murugan 960b1039f0SRamuthevar Vadivel Murugan #define HSNAND_ALE_OFFS BIT(2) 970b1039f0SRamuthevar Vadivel Murugan #define HSNAND_CLE_OFFS BIT(3) 980b1039f0SRamuthevar Vadivel Murugan #define HSNAND_CS_OFFS BIT(4) 990b1039f0SRamuthevar Vadivel Murugan 1000b1039f0SRamuthevar Vadivel Murugan #define HSNAND_ECC_OFFSET 0x008 1010b1039f0SRamuthevar Vadivel Murugan 1020b1039f0SRamuthevar Vadivel Murugan #define NAND_DATA_IFACE_CHECK_ONLY -1 1030b1039f0SRamuthevar Vadivel Murugan 1040b1039f0SRamuthevar Vadivel Murugan #define MAX_CS 2 1050b1039f0SRamuthevar Vadivel Murugan 1060b1039f0SRamuthevar Vadivel Murugan #define USEC_PER_SEC 1000000L 1070b1039f0SRamuthevar Vadivel Murugan 1080b1039f0SRamuthevar Vadivel Murugan struct ebu_nand_cs { 1090b1039f0SRamuthevar Vadivel Murugan void __iomem *chipaddr; 1100b1039f0SRamuthevar Vadivel Murugan dma_addr_t nand_pa; 1110b1039f0SRamuthevar Vadivel Murugan u32 addr_sel; 1120b1039f0SRamuthevar Vadivel Murugan }; 1130b1039f0SRamuthevar Vadivel Murugan 1140b1039f0SRamuthevar Vadivel Murugan struct ebu_nand_controller { 1150b1039f0SRamuthevar Vadivel Murugan struct nand_controller controller; 1160b1039f0SRamuthevar Vadivel Murugan struct nand_chip chip; 1170b1039f0SRamuthevar Vadivel Murugan struct device *dev; 1180b1039f0SRamuthevar Vadivel Murugan void __iomem *ebu; 1190b1039f0SRamuthevar Vadivel Murugan void __iomem *hsnand; 1200b1039f0SRamuthevar Vadivel Murugan struct dma_chan *dma_tx; 1210b1039f0SRamuthevar Vadivel Murugan struct dma_chan *dma_rx; 1220b1039f0SRamuthevar Vadivel Murugan struct completion dma_access_complete; 1230b1039f0SRamuthevar Vadivel Murugan unsigned long clk_rate; 1240b1039f0SRamuthevar Vadivel Murugan struct clk *clk; 1250b1039f0SRamuthevar Vadivel Murugan u32 nd_para0; 1260b1039f0SRamuthevar Vadivel Murugan u8 cs_num; 1270b1039f0SRamuthevar Vadivel Murugan struct ebu_nand_cs cs[MAX_CS]; 1280b1039f0SRamuthevar Vadivel Murugan }; 1290b1039f0SRamuthevar Vadivel Murugan 1300b1039f0SRamuthevar Vadivel Murugan static inline struct ebu_nand_controller *nand_to_ebu(struct nand_chip *chip) 1310b1039f0SRamuthevar Vadivel Murugan { 1320b1039f0SRamuthevar Vadivel Murugan return container_of(chip, struct ebu_nand_controller, chip); 1330b1039f0SRamuthevar Vadivel Murugan } 1340b1039f0SRamuthevar Vadivel Murugan 1350b1039f0SRamuthevar Vadivel Murugan static int ebu_nand_waitrdy(struct nand_chip *chip, int timeout_ms) 1360b1039f0SRamuthevar Vadivel Murugan { 1370b1039f0SRamuthevar Vadivel Murugan struct ebu_nand_controller *ctrl = nand_to_ebu(chip); 1380b1039f0SRamuthevar Vadivel Murugan u32 status; 1390b1039f0SRamuthevar Vadivel Murugan 1400b1039f0SRamuthevar Vadivel Murugan return readl_poll_timeout(ctrl->ebu + EBU_WAIT, status, 1410b1039f0SRamuthevar Vadivel Murugan (status & EBU_WAIT_RDBY) || 1420b1039f0SRamuthevar Vadivel Murugan (status & EBU_WAIT_WR_C), 20, timeout_ms); 1430b1039f0SRamuthevar Vadivel Murugan } 1440b1039f0SRamuthevar Vadivel Murugan 1450b1039f0SRamuthevar Vadivel Murugan static u8 ebu_nand_readb(struct nand_chip *chip) 1460b1039f0SRamuthevar Vadivel Murugan { 1470b1039f0SRamuthevar Vadivel Murugan struct ebu_nand_controller *ebu_host = nand_get_controller_data(chip); 1480b1039f0SRamuthevar Vadivel Murugan u8 cs_num = ebu_host->cs_num; 1490b1039f0SRamuthevar Vadivel Murugan u8 val; 1500b1039f0SRamuthevar Vadivel Murugan 1510b1039f0SRamuthevar Vadivel Murugan val = readb(ebu_host->cs[cs_num].chipaddr + HSNAND_CS_OFFS); 1520b1039f0SRamuthevar Vadivel Murugan ebu_nand_waitrdy(chip, 1000); 1530b1039f0SRamuthevar Vadivel Murugan return val; 1540b1039f0SRamuthevar Vadivel Murugan } 1550b1039f0SRamuthevar Vadivel Murugan 1560b1039f0SRamuthevar Vadivel Murugan static void ebu_nand_writeb(struct nand_chip *chip, u32 offset, u8 value) 1570b1039f0SRamuthevar Vadivel Murugan { 1580b1039f0SRamuthevar Vadivel Murugan struct ebu_nand_controller *ebu_host = nand_get_controller_data(chip); 1590b1039f0SRamuthevar Vadivel Murugan u8 cs_num = ebu_host->cs_num; 1600b1039f0SRamuthevar Vadivel Murugan 1610b1039f0SRamuthevar Vadivel Murugan writeb(value, ebu_host->cs[cs_num].chipaddr + offset); 1620b1039f0SRamuthevar Vadivel Murugan ebu_nand_waitrdy(chip, 1000); 1630b1039f0SRamuthevar Vadivel Murugan } 1640b1039f0SRamuthevar Vadivel Murugan 1650b1039f0SRamuthevar Vadivel Murugan static void ebu_read_buf(struct nand_chip *chip, u_char *buf, unsigned int len) 1660b1039f0SRamuthevar Vadivel Murugan { 1670b1039f0SRamuthevar Vadivel Murugan int i; 1680b1039f0SRamuthevar Vadivel Murugan 1690b1039f0SRamuthevar Vadivel Murugan for (i = 0; i < len; i++) 1700b1039f0SRamuthevar Vadivel Murugan buf[i] = ebu_nand_readb(chip); 1710b1039f0SRamuthevar Vadivel Murugan } 1720b1039f0SRamuthevar Vadivel Murugan 1730b1039f0SRamuthevar Vadivel Murugan static void ebu_write_buf(struct nand_chip *chip, const u_char *buf, int len) 1740b1039f0SRamuthevar Vadivel Murugan { 1750b1039f0SRamuthevar Vadivel Murugan int i; 1760b1039f0SRamuthevar Vadivel Murugan 1770b1039f0SRamuthevar Vadivel Murugan for (i = 0; i < len; i++) 1780b1039f0SRamuthevar Vadivel Murugan ebu_nand_writeb(chip, HSNAND_CS_OFFS, buf[i]); 1790b1039f0SRamuthevar Vadivel Murugan } 1800b1039f0SRamuthevar Vadivel Murugan 1810b1039f0SRamuthevar Vadivel Murugan static void ebu_nand_disable(struct nand_chip *chip) 1820b1039f0SRamuthevar Vadivel Murugan { 1830b1039f0SRamuthevar Vadivel Murugan struct ebu_nand_controller *ebu_host = nand_get_controller_data(chip); 1840b1039f0SRamuthevar Vadivel Murugan 1850b1039f0SRamuthevar Vadivel Murugan writel(0, ebu_host->ebu + EBU_CON); 1860b1039f0SRamuthevar Vadivel Murugan } 1870b1039f0SRamuthevar Vadivel Murugan 1880b1039f0SRamuthevar Vadivel Murugan static void ebu_select_chip(struct nand_chip *chip) 1890b1039f0SRamuthevar Vadivel Murugan { 1900b1039f0SRamuthevar Vadivel Murugan struct ebu_nand_controller *ebu_host = nand_get_controller_data(chip); 1910b1039f0SRamuthevar Vadivel Murugan void __iomem *nand_con = ebu_host->ebu + EBU_CON; 1920b1039f0SRamuthevar Vadivel Murugan u32 cs = ebu_host->cs_num; 1930b1039f0SRamuthevar Vadivel Murugan 1940b1039f0SRamuthevar Vadivel Murugan writel(EBU_CON_NANDM_EN | EBU_CON_CSMUX_E_EN | EBU_CON_CS_P_LOW | 1950b1039f0SRamuthevar Vadivel Murugan EBU_CON_SE_P_LOW | EBU_CON_WP_P_LOW | EBU_CON_PRE_P_LOW | 1960b1039f0SRamuthevar Vadivel Murugan EBU_CON_IN_CS_S(cs) | EBU_CON_OUT_CS_S(cs) | 1970b1039f0SRamuthevar Vadivel Murugan EBU_CON_LAT_EN_CS_P, nand_con); 1980b1039f0SRamuthevar Vadivel Murugan } 1990b1039f0SRamuthevar Vadivel Murugan 2000b1039f0SRamuthevar Vadivel Murugan static int ebu_nand_set_timings(struct nand_chip *chip, int csline, 2010b1039f0SRamuthevar Vadivel Murugan const struct nand_interface_config *conf) 2020b1039f0SRamuthevar Vadivel Murugan { 2030b1039f0SRamuthevar Vadivel Murugan struct ebu_nand_controller *ctrl = nand_to_ebu(chip); 2040b1039f0SRamuthevar Vadivel Murugan unsigned int rate = clk_get_rate(ctrl->clk) / HZ_PER_MHZ; 2050b1039f0SRamuthevar Vadivel Murugan unsigned int period = DIV_ROUND_UP(USEC_PER_SEC, rate); 2060b1039f0SRamuthevar Vadivel Murugan const struct nand_sdr_timings *timings; 2070b1039f0SRamuthevar Vadivel Murugan u32 trecov, thold, twrwait, trdwait; 2080b1039f0SRamuthevar Vadivel Murugan u32 reg = 0; 2090b1039f0SRamuthevar Vadivel Murugan 2100b1039f0SRamuthevar Vadivel Murugan timings = nand_get_sdr_timings(conf); 2110b1039f0SRamuthevar Vadivel Murugan if (IS_ERR(timings)) 2120b1039f0SRamuthevar Vadivel Murugan return PTR_ERR(timings); 2130b1039f0SRamuthevar Vadivel Murugan 2140b1039f0SRamuthevar Vadivel Murugan if (csline == NAND_DATA_IFACE_CHECK_ONLY) 2150b1039f0SRamuthevar Vadivel Murugan return 0; 2160b1039f0SRamuthevar Vadivel Murugan 2170b1039f0SRamuthevar Vadivel Murugan trecov = DIV_ROUND_UP(max(timings->tREA_max, timings->tREH_min), 2180b1039f0SRamuthevar Vadivel Murugan period); 2190b1039f0SRamuthevar Vadivel Murugan reg |= EBU_BUSCON_RECOVC(trecov); 2200b1039f0SRamuthevar Vadivel Murugan 2210b1039f0SRamuthevar Vadivel Murugan thold = DIV_ROUND_UP(max(timings->tDH_min, timings->tDS_min), period); 2220b1039f0SRamuthevar Vadivel Murugan reg |= EBU_BUSCON_HOLDC(thold); 2230b1039f0SRamuthevar Vadivel Murugan 2240b1039f0SRamuthevar Vadivel Murugan trdwait = DIV_ROUND_UP(max(timings->tRC_min, timings->tREH_min), 2250b1039f0SRamuthevar Vadivel Murugan period); 2260b1039f0SRamuthevar Vadivel Murugan reg |= EBU_BUSCON_WAITRDC(trdwait); 2270b1039f0SRamuthevar Vadivel Murugan 2280b1039f0SRamuthevar Vadivel Murugan twrwait = DIV_ROUND_UP(max(timings->tWC_min, timings->tWH_min), period); 2290b1039f0SRamuthevar Vadivel Murugan reg |= EBU_BUSCON_WAITWRC(twrwait); 2300b1039f0SRamuthevar Vadivel Murugan 2310b1039f0SRamuthevar Vadivel Murugan reg |= EBU_BUSCON_CMULT_V4 | EBU_BUSCON_BCGEN_CS | EBU_BUSCON_ALEC | 2320b1039f0SRamuthevar Vadivel Murugan EBU_BUSCON_SETUP_EN; 2330b1039f0SRamuthevar Vadivel Murugan 2340b1039f0SRamuthevar Vadivel Murugan writel(reg, ctrl->ebu + EBU_BUSCON(ctrl->cs_num)); 2350b1039f0SRamuthevar Vadivel Murugan 2360b1039f0SRamuthevar Vadivel Murugan return 0; 2370b1039f0SRamuthevar Vadivel Murugan } 2380b1039f0SRamuthevar Vadivel Murugan 2390b1039f0SRamuthevar Vadivel Murugan static int ebu_nand_ooblayout_ecc(struct mtd_info *mtd, int section, 2400b1039f0SRamuthevar Vadivel Murugan struct mtd_oob_region *oobregion) 2410b1039f0SRamuthevar Vadivel Murugan { 2420b1039f0SRamuthevar Vadivel Murugan struct nand_chip *chip = mtd_to_nand(mtd); 2430b1039f0SRamuthevar Vadivel Murugan 2440b1039f0SRamuthevar Vadivel Murugan if (section) 2450b1039f0SRamuthevar Vadivel Murugan return -ERANGE; 2460b1039f0SRamuthevar Vadivel Murugan 2470b1039f0SRamuthevar Vadivel Murugan oobregion->offset = HSNAND_ECC_OFFSET; 2480b1039f0SRamuthevar Vadivel Murugan oobregion->length = chip->ecc.total; 2490b1039f0SRamuthevar Vadivel Murugan 2500b1039f0SRamuthevar Vadivel Murugan return 0; 2510b1039f0SRamuthevar Vadivel Murugan } 2520b1039f0SRamuthevar Vadivel Murugan 2530b1039f0SRamuthevar Vadivel Murugan static int ebu_nand_ooblayout_free(struct mtd_info *mtd, int section, 2540b1039f0SRamuthevar Vadivel Murugan struct mtd_oob_region *oobregion) 2550b1039f0SRamuthevar Vadivel Murugan { 2560b1039f0SRamuthevar Vadivel Murugan struct nand_chip *chip = mtd_to_nand(mtd); 2570b1039f0SRamuthevar Vadivel Murugan 2580b1039f0SRamuthevar Vadivel Murugan if (section) 2590b1039f0SRamuthevar Vadivel Murugan return -ERANGE; 2600b1039f0SRamuthevar Vadivel Murugan 2610b1039f0SRamuthevar Vadivel Murugan oobregion->offset = chip->ecc.total + HSNAND_ECC_OFFSET; 2620b1039f0SRamuthevar Vadivel Murugan oobregion->length = mtd->oobsize - oobregion->offset; 2630b1039f0SRamuthevar Vadivel Murugan 2640b1039f0SRamuthevar Vadivel Murugan return 0; 2650b1039f0SRamuthevar Vadivel Murugan } 2660b1039f0SRamuthevar Vadivel Murugan 2670b1039f0SRamuthevar Vadivel Murugan static const struct mtd_ooblayout_ops ebu_nand_ooblayout_ops = { 2680b1039f0SRamuthevar Vadivel Murugan .ecc = ebu_nand_ooblayout_ecc, 2690b1039f0SRamuthevar Vadivel Murugan .free = ebu_nand_ooblayout_free, 2700b1039f0SRamuthevar Vadivel Murugan }; 2710b1039f0SRamuthevar Vadivel Murugan 2720b1039f0SRamuthevar Vadivel Murugan static void ebu_dma_rx_callback(void *cookie) 2730b1039f0SRamuthevar Vadivel Murugan { 2740b1039f0SRamuthevar Vadivel Murugan struct ebu_nand_controller *ebu_host = cookie; 2750b1039f0SRamuthevar Vadivel Murugan 2760b1039f0SRamuthevar Vadivel Murugan dmaengine_terminate_async(ebu_host->dma_rx); 2770b1039f0SRamuthevar Vadivel Murugan 2780b1039f0SRamuthevar Vadivel Murugan complete(&ebu_host->dma_access_complete); 2790b1039f0SRamuthevar Vadivel Murugan } 2800b1039f0SRamuthevar Vadivel Murugan 2810b1039f0SRamuthevar Vadivel Murugan static void ebu_dma_tx_callback(void *cookie) 2820b1039f0SRamuthevar Vadivel Murugan { 2830b1039f0SRamuthevar Vadivel Murugan struct ebu_nand_controller *ebu_host = cookie; 2840b1039f0SRamuthevar Vadivel Murugan 2850b1039f0SRamuthevar Vadivel Murugan dmaengine_terminate_async(ebu_host->dma_tx); 2860b1039f0SRamuthevar Vadivel Murugan 2870b1039f0SRamuthevar Vadivel Murugan complete(&ebu_host->dma_access_complete); 2880b1039f0SRamuthevar Vadivel Murugan } 2890b1039f0SRamuthevar Vadivel Murugan 2900b1039f0SRamuthevar Vadivel Murugan static int ebu_dma_start(struct ebu_nand_controller *ebu_host, u32 dir, 2910b1039f0SRamuthevar Vadivel Murugan const u8 *buf, u32 len) 2920b1039f0SRamuthevar Vadivel Murugan { 2930b1039f0SRamuthevar Vadivel Murugan struct dma_async_tx_descriptor *tx; 2940b1039f0SRamuthevar Vadivel Murugan struct completion *dma_completion; 2950b1039f0SRamuthevar Vadivel Murugan dma_async_tx_callback callback; 2960b1039f0SRamuthevar Vadivel Murugan struct dma_chan *chan; 2970b1039f0SRamuthevar Vadivel Murugan dma_cookie_t cookie; 2980b1039f0SRamuthevar Vadivel Murugan unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT; 2990b1039f0SRamuthevar Vadivel Murugan dma_addr_t buf_dma; 3000b1039f0SRamuthevar Vadivel Murugan int ret; 3010b1039f0SRamuthevar Vadivel Murugan u32 timeout; 3020b1039f0SRamuthevar Vadivel Murugan 3030b1039f0SRamuthevar Vadivel Murugan if (dir == DMA_DEV_TO_MEM) { 3040b1039f0SRamuthevar Vadivel Murugan chan = ebu_host->dma_rx; 3050b1039f0SRamuthevar Vadivel Murugan dma_completion = &ebu_host->dma_access_complete; 3060b1039f0SRamuthevar Vadivel Murugan callback = ebu_dma_rx_callback; 3070b1039f0SRamuthevar Vadivel Murugan } else { 3080b1039f0SRamuthevar Vadivel Murugan chan = ebu_host->dma_tx; 3090b1039f0SRamuthevar Vadivel Murugan dma_completion = &ebu_host->dma_access_complete; 3100b1039f0SRamuthevar Vadivel Murugan callback = ebu_dma_tx_callback; 3110b1039f0SRamuthevar Vadivel Murugan } 3120b1039f0SRamuthevar Vadivel Murugan 3130b1039f0SRamuthevar Vadivel Murugan buf_dma = dma_map_single(chan->device->dev, (void *)buf, len, dir); 3140b1039f0SRamuthevar Vadivel Murugan if (dma_mapping_error(chan->device->dev, buf_dma)) { 3150b1039f0SRamuthevar Vadivel Murugan dev_err(ebu_host->dev, "Failed to map DMA buffer\n"); 3160b1039f0SRamuthevar Vadivel Murugan ret = -EIO; 3170b1039f0SRamuthevar Vadivel Murugan goto err_unmap; 3180b1039f0SRamuthevar Vadivel Murugan } 3190b1039f0SRamuthevar Vadivel Murugan 3200b1039f0SRamuthevar Vadivel Murugan tx = dmaengine_prep_slave_single(chan, buf_dma, len, dir, flags); 321073abfa7SChristophe JAILLET if (!tx) { 322073abfa7SChristophe JAILLET ret = -ENXIO; 323073abfa7SChristophe JAILLET goto err_unmap; 324073abfa7SChristophe JAILLET } 3250b1039f0SRamuthevar Vadivel Murugan 3260b1039f0SRamuthevar Vadivel Murugan tx->callback = callback; 3270b1039f0SRamuthevar Vadivel Murugan tx->callback_param = ebu_host; 3280b1039f0SRamuthevar Vadivel Murugan cookie = tx->tx_submit(tx); 3290b1039f0SRamuthevar Vadivel Murugan 3300b1039f0SRamuthevar Vadivel Murugan ret = dma_submit_error(cookie); 3310b1039f0SRamuthevar Vadivel Murugan if (ret) { 3320b1039f0SRamuthevar Vadivel Murugan dev_err(ebu_host->dev, "dma_submit_error %d\n", cookie); 3330b1039f0SRamuthevar Vadivel Murugan ret = -EIO; 3340b1039f0SRamuthevar Vadivel Murugan goto err_unmap; 3350b1039f0SRamuthevar Vadivel Murugan } 3360b1039f0SRamuthevar Vadivel Murugan 3370b1039f0SRamuthevar Vadivel Murugan init_completion(dma_completion); 3380b1039f0SRamuthevar Vadivel Murugan dma_async_issue_pending(chan); 3390b1039f0SRamuthevar Vadivel Murugan 3400b1039f0SRamuthevar Vadivel Murugan /* Wait DMA to finish the data transfer.*/ 3410b1039f0SRamuthevar Vadivel Murugan timeout = wait_for_completion_timeout(dma_completion, msecs_to_jiffies(1000)); 3420b1039f0SRamuthevar Vadivel Murugan if (!timeout) { 3430b1039f0SRamuthevar Vadivel Murugan dev_err(ebu_host->dev, "I/O Error in DMA RX (status %d)\n", 3440b1039f0SRamuthevar Vadivel Murugan dmaengine_tx_status(chan, cookie, NULL)); 3450b1039f0SRamuthevar Vadivel Murugan dmaengine_terminate_sync(chan); 3460b1039f0SRamuthevar Vadivel Murugan ret = -ETIMEDOUT; 3470b1039f0SRamuthevar Vadivel Murugan goto err_unmap; 3480b1039f0SRamuthevar Vadivel Murugan } 3490b1039f0SRamuthevar Vadivel Murugan 3500b1039f0SRamuthevar Vadivel Murugan return 0; 3510b1039f0SRamuthevar Vadivel Murugan 3520b1039f0SRamuthevar Vadivel Murugan err_unmap: 3530b1039f0SRamuthevar Vadivel Murugan dma_unmap_single(ebu_host->dev, buf_dma, len, dir); 3540b1039f0SRamuthevar Vadivel Murugan 3550b1039f0SRamuthevar Vadivel Murugan return ret; 3560b1039f0SRamuthevar Vadivel Murugan } 3570b1039f0SRamuthevar Vadivel Murugan 3580b1039f0SRamuthevar Vadivel Murugan static void ebu_nand_trigger(struct ebu_nand_controller *ebu_host, 3590b1039f0SRamuthevar Vadivel Murugan int page, u32 cmd) 3600b1039f0SRamuthevar Vadivel Murugan { 3610b1039f0SRamuthevar Vadivel Murugan unsigned int val; 3620b1039f0SRamuthevar Vadivel Murugan 3630b1039f0SRamuthevar Vadivel Murugan val = cmd | (page & 0xFF) << HSNAND_CTL1_ADDR_SHIFT; 3640b1039f0SRamuthevar Vadivel Murugan writel(val, ebu_host->hsnand + HSNAND_CTL1); 3650b1039f0SRamuthevar Vadivel Murugan val = (page & 0xFFFF00) >> 8 | HSNAND_CTL2_CYC_N_V5; 3660b1039f0SRamuthevar Vadivel Murugan writel(val, ebu_host->hsnand + HSNAND_CTL2); 3670b1039f0SRamuthevar Vadivel Murugan 3680b1039f0SRamuthevar Vadivel Murugan writel(ebu_host->nd_para0, ebu_host->hsnand + HSNAND_PARA0); 3690b1039f0SRamuthevar Vadivel Murugan 3700b1039f0SRamuthevar Vadivel Murugan /* clear first, will update later */ 3710b1039f0SRamuthevar Vadivel Murugan writel(0xFFFFFFFF, ebu_host->hsnand + HSNAND_CMSG_0); 3720b1039f0SRamuthevar Vadivel Murugan writel(0xFFFFFFFF, ebu_host->hsnand + HSNAND_CMSG_1); 3730b1039f0SRamuthevar Vadivel Murugan 3740b1039f0SRamuthevar Vadivel Murugan writel(HSNAND_INT_MSK_CTL_WR_C, 3750b1039f0SRamuthevar Vadivel Murugan ebu_host->hsnand + HSNAND_INT_MSK_CTL); 3760b1039f0SRamuthevar Vadivel Murugan 3770b1039f0SRamuthevar Vadivel Murugan if (!cmd) 3780b1039f0SRamuthevar Vadivel Murugan val = HSNAND_CTL_RW_READ; 3790b1039f0SRamuthevar Vadivel Murugan else 3800b1039f0SRamuthevar Vadivel Murugan val = HSNAND_CTL_RW_WRITE; 3810b1039f0SRamuthevar Vadivel Murugan 3820b1039f0SRamuthevar Vadivel Murugan writel(HSNAND_CTL_MSG_EN | HSNAND_CTL_CKFF_EN | 3830b1039f0SRamuthevar Vadivel Murugan HSNAND_CTL_ECC_OFF_V8TH | HSNAND_CTL_CE_SEL_CS(ebu_host->cs_num) | 3840b1039f0SRamuthevar Vadivel Murugan HSNAND_CTL_ENABLE_ECC | HSNAND_CTL_GO | val, 3850b1039f0SRamuthevar Vadivel Murugan ebu_host->hsnand + HSNAND_CTL); 3860b1039f0SRamuthevar Vadivel Murugan } 3870b1039f0SRamuthevar Vadivel Murugan 3880b1039f0SRamuthevar Vadivel Murugan static int ebu_nand_read_page_hwecc(struct nand_chip *chip, u8 *buf, 3890b1039f0SRamuthevar Vadivel Murugan int oob_required, int page) 3900b1039f0SRamuthevar Vadivel Murugan { 3910b1039f0SRamuthevar Vadivel Murugan struct mtd_info *mtd = nand_to_mtd(chip); 3920b1039f0SRamuthevar Vadivel Murugan struct ebu_nand_controller *ebu_host = nand_get_controller_data(chip); 3930b1039f0SRamuthevar Vadivel Murugan int ret, reg_data; 3940b1039f0SRamuthevar Vadivel Murugan 3950b1039f0SRamuthevar Vadivel Murugan ebu_nand_trigger(ebu_host, page, NAND_CMD_READ0); 3960b1039f0SRamuthevar Vadivel Murugan 3970b1039f0SRamuthevar Vadivel Murugan ret = ebu_dma_start(ebu_host, DMA_DEV_TO_MEM, buf, mtd->writesize); 3980b1039f0SRamuthevar Vadivel Murugan if (ret) 3990b1039f0SRamuthevar Vadivel Murugan return ret; 4000b1039f0SRamuthevar Vadivel Murugan 4010b1039f0SRamuthevar Vadivel Murugan if (oob_required) 4020b1039f0SRamuthevar Vadivel Murugan chip->ecc.read_oob(chip, page); 4030b1039f0SRamuthevar Vadivel Murugan 4040b1039f0SRamuthevar Vadivel Murugan reg_data = readl(ebu_host->hsnand + HSNAND_CTL); 4050b1039f0SRamuthevar Vadivel Murugan reg_data &= ~HSNAND_CTL_GO; 4060b1039f0SRamuthevar Vadivel Murugan writel(reg_data, ebu_host->hsnand + HSNAND_CTL); 4070b1039f0SRamuthevar Vadivel Murugan 4080b1039f0SRamuthevar Vadivel Murugan return 0; 4090b1039f0SRamuthevar Vadivel Murugan } 4100b1039f0SRamuthevar Vadivel Murugan 4110b1039f0SRamuthevar Vadivel Murugan static int ebu_nand_write_page_hwecc(struct nand_chip *chip, const u8 *buf, 4120b1039f0SRamuthevar Vadivel Murugan int oob_required, int page) 4130b1039f0SRamuthevar Vadivel Murugan { 4140b1039f0SRamuthevar Vadivel Murugan struct mtd_info *mtd = nand_to_mtd(chip); 4150b1039f0SRamuthevar Vadivel Murugan struct ebu_nand_controller *ebu_host = nand_get_controller_data(chip); 4160b1039f0SRamuthevar Vadivel Murugan void __iomem *int_sta = ebu_host->hsnand + HSNAND_INT_STA; 4170b1039f0SRamuthevar Vadivel Murugan int reg_data, ret, val; 4180b1039f0SRamuthevar Vadivel Murugan u32 reg; 4190b1039f0SRamuthevar Vadivel Murugan 4200b1039f0SRamuthevar Vadivel Murugan ebu_nand_trigger(ebu_host, page, NAND_CMD_SEQIN); 4210b1039f0SRamuthevar Vadivel Murugan 4220b1039f0SRamuthevar Vadivel Murugan ret = ebu_dma_start(ebu_host, DMA_MEM_TO_DEV, buf, mtd->writesize); 4230b1039f0SRamuthevar Vadivel Murugan if (ret) 4240b1039f0SRamuthevar Vadivel Murugan return ret; 4250b1039f0SRamuthevar Vadivel Murugan 4260b1039f0SRamuthevar Vadivel Murugan if (oob_required) { 4270b1039f0SRamuthevar Vadivel Murugan reg = get_unaligned_le32(chip->oob_poi); 4280b1039f0SRamuthevar Vadivel Murugan writel(reg, ebu_host->hsnand + HSNAND_CMSG_0); 4290b1039f0SRamuthevar Vadivel Murugan 4300b1039f0SRamuthevar Vadivel Murugan reg = get_unaligned_le32(chip->oob_poi + 4); 4310b1039f0SRamuthevar Vadivel Murugan writel(reg, ebu_host->hsnand + HSNAND_CMSG_1); 4320b1039f0SRamuthevar Vadivel Murugan } 4330b1039f0SRamuthevar Vadivel Murugan 4340b1039f0SRamuthevar Vadivel Murugan ret = readl_poll_timeout_atomic(int_sta, val, !(val & HSNAND_INT_STA_WR_C), 4350b1039f0SRamuthevar Vadivel Murugan 10, 1000); 4360b1039f0SRamuthevar Vadivel Murugan if (ret) 4370b1039f0SRamuthevar Vadivel Murugan return ret; 4380b1039f0SRamuthevar Vadivel Murugan 4390b1039f0SRamuthevar Vadivel Murugan reg_data = readl(ebu_host->hsnand + HSNAND_CTL); 4400b1039f0SRamuthevar Vadivel Murugan reg_data &= ~HSNAND_CTL_GO; 4410b1039f0SRamuthevar Vadivel Murugan writel(reg_data, ebu_host->hsnand + HSNAND_CTL); 4420b1039f0SRamuthevar Vadivel Murugan 4430b1039f0SRamuthevar Vadivel Murugan return 0; 4440b1039f0SRamuthevar Vadivel Murugan } 4450b1039f0SRamuthevar Vadivel Murugan 4460b1039f0SRamuthevar Vadivel Murugan static const u8 ecc_strength[] = { 1, 1, 4, 8, 24, 32, 40, 60, }; 4470b1039f0SRamuthevar Vadivel Murugan 4480b1039f0SRamuthevar Vadivel Murugan static int ebu_nand_attach_chip(struct nand_chip *chip) 4490b1039f0SRamuthevar Vadivel Murugan { 4500b1039f0SRamuthevar Vadivel Murugan struct mtd_info *mtd = nand_to_mtd(chip); 4510b1039f0SRamuthevar Vadivel Murugan struct ebu_nand_controller *ebu_host = nand_get_controller_data(chip); 4520b1039f0SRamuthevar Vadivel Murugan u32 ecc_steps, ecc_bytes, ecc_total, pagesize, pg_per_blk; 4530b1039f0SRamuthevar Vadivel Murugan u32 ecc_strength_ds = chip->ecc.strength; 4540b1039f0SRamuthevar Vadivel Murugan u32 ecc_size = chip->ecc.size; 4550b1039f0SRamuthevar Vadivel Murugan u32 writesize = mtd->writesize; 4560b1039f0SRamuthevar Vadivel Murugan u32 blocksize = mtd->erasesize; 4570b1039f0SRamuthevar Vadivel Murugan int bch_algo, start, val; 4580b1039f0SRamuthevar Vadivel Murugan 4590b1039f0SRamuthevar Vadivel Murugan /* Default to an ECC size of 512 */ 4600b1039f0SRamuthevar Vadivel Murugan if (!chip->ecc.size) 4610b1039f0SRamuthevar Vadivel Murugan chip->ecc.size = 512; 4620b1039f0SRamuthevar Vadivel Murugan 4630b1039f0SRamuthevar Vadivel Murugan switch (ecc_size) { 4640b1039f0SRamuthevar Vadivel Murugan case 512: 4650b1039f0SRamuthevar Vadivel Murugan start = 1; 4660b1039f0SRamuthevar Vadivel Murugan if (!ecc_strength_ds) 4670b1039f0SRamuthevar Vadivel Murugan ecc_strength_ds = 4; 4680b1039f0SRamuthevar Vadivel Murugan break; 4690b1039f0SRamuthevar Vadivel Murugan case 1024: 4700b1039f0SRamuthevar Vadivel Murugan start = 4; 4710b1039f0SRamuthevar Vadivel Murugan if (!ecc_strength_ds) 4720b1039f0SRamuthevar Vadivel Murugan ecc_strength_ds = 32; 4730b1039f0SRamuthevar Vadivel Murugan break; 4740b1039f0SRamuthevar Vadivel Murugan default: 4750b1039f0SRamuthevar Vadivel Murugan return -EINVAL; 4760b1039f0SRamuthevar Vadivel Murugan } 4770b1039f0SRamuthevar Vadivel Murugan 4780b1039f0SRamuthevar Vadivel Murugan /* BCH ECC algorithm Settings for number of bits per 512B/1024B */ 4790b1039f0SRamuthevar Vadivel Murugan bch_algo = round_up(start + 1, 4); 4800b1039f0SRamuthevar Vadivel Murugan for (val = start; val < bch_algo; val++) { 4810b1039f0SRamuthevar Vadivel Murugan if (ecc_strength_ds == ecc_strength[val]) 4820b1039f0SRamuthevar Vadivel Murugan break; 4830b1039f0SRamuthevar Vadivel Murugan } 4840b1039f0SRamuthevar Vadivel Murugan if (val == bch_algo) 4850b1039f0SRamuthevar Vadivel Murugan return -EINVAL; 4860b1039f0SRamuthevar Vadivel Murugan 4870b1039f0SRamuthevar Vadivel Murugan if (ecc_strength_ds == 8) 4880b1039f0SRamuthevar Vadivel Murugan ecc_bytes = 14; 4890b1039f0SRamuthevar Vadivel Murugan else 4900b1039f0SRamuthevar Vadivel Murugan ecc_bytes = DIV_ROUND_UP(ecc_strength_ds * fls(8 * ecc_size), 8); 4910b1039f0SRamuthevar Vadivel Murugan 4920b1039f0SRamuthevar Vadivel Murugan ecc_steps = writesize / ecc_size; 4930b1039f0SRamuthevar Vadivel Murugan ecc_total = ecc_steps * ecc_bytes; 4940b1039f0SRamuthevar Vadivel Murugan if ((ecc_total + 8) > mtd->oobsize) 4950b1039f0SRamuthevar Vadivel Murugan return -ERANGE; 4960b1039f0SRamuthevar Vadivel Murugan 4970b1039f0SRamuthevar Vadivel Murugan chip->ecc.total = ecc_total; 4980b1039f0SRamuthevar Vadivel Murugan pagesize = fls(writesize >> 11); 4990b1039f0SRamuthevar Vadivel Murugan if (pagesize > HSNAND_PARA0_PAGE_V8192) 5000b1039f0SRamuthevar Vadivel Murugan return -ERANGE; 5010b1039f0SRamuthevar Vadivel Murugan 5020b1039f0SRamuthevar Vadivel Murugan pg_per_blk = fls((blocksize / writesize) >> 6) / 8; 5030b1039f0SRamuthevar Vadivel Murugan if (pg_per_blk > HSNAND_PARA0_PIB_V256) 5040b1039f0SRamuthevar Vadivel Murugan return -ERANGE; 5050b1039f0SRamuthevar Vadivel Murugan 5060b1039f0SRamuthevar Vadivel Murugan ebu_host->nd_para0 = pagesize | pg_per_blk | HSNAND_PARA0_BYP_EN_NP | 5070b1039f0SRamuthevar Vadivel Murugan HSNAND_PARA0_BYP_DEC_NP | HSNAND_PARA0_ADEP_EN | 5080b1039f0SRamuthevar Vadivel Murugan HSNAND_PARA0_TYPE_ONFI | (val << 29); 5090b1039f0SRamuthevar Vadivel Murugan 5100b1039f0SRamuthevar Vadivel Murugan mtd_set_ooblayout(mtd, &ebu_nand_ooblayout_ops); 5110b1039f0SRamuthevar Vadivel Murugan chip->ecc.read_page = ebu_nand_read_page_hwecc; 5120b1039f0SRamuthevar Vadivel Murugan chip->ecc.write_page = ebu_nand_write_page_hwecc; 5130b1039f0SRamuthevar Vadivel Murugan 5140b1039f0SRamuthevar Vadivel Murugan return 0; 5150b1039f0SRamuthevar Vadivel Murugan } 5160b1039f0SRamuthevar Vadivel Murugan 5170b1039f0SRamuthevar Vadivel Murugan static int ebu_nand_exec_op(struct nand_chip *chip, 5180b1039f0SRamuthevar Vadivel Murugan const struct nand_operation *op, bool check_only) 5190b1039f0SRamuthevar Vadivel Murugan { 5200b1039f0SRamuthevar Vadivel Murugan const struct nand_op_instr *instr = NULL; 5210b1039f0SRamuthevar Vadivel Murugan unsigned int op_id; 5220b1039f0SRamuthevar Vadivel Murugan int i, timeout_ms, ret = 0; 5230b1039f0SRamuthevar Vadivel Murugan 5240b1039f0SRamuthevar Vadivel Murugan if (check_only) 5250b1039f0SRamuthevar Vadivel Murugan return 0; 5260b1039f0SRamuthevar Vadivel Murugan 5270b1039f0SRamuthevar Vadivel Murugan ebu_select_chip(chip); 5280b1039f0SRamuthevar Vadivel Murugan for (op_id = 0; op_id < op->ninstrs; op_id++) { 5290b1039f0SRamuthevar Vadivel Murugan instr = &op->instrs[op_id]; 5300b1039f0SRamuthevar Vadivel Murugan 5310b1039f0SRamuthevar Vadivel Murugan switch (instr->type) { 5320b1039f0SRamuthevar Vadivel Murugan case NAND_OP_CMD_INSTR: 5330b1039f0SRamuthevar Vadivel Murugan ebu_nand_writeb(chip, HSNAND_CLE_OFFS | HSNAND_CS_OFFS, 5340b1039f0SRamuthevar Vadivel Murugan instr->ctx.cmd.opcode); 5350b1039f0SRamuthevar Vadivel Murugan break; 5360b1039f0SRamuthevar Vadivel Murugan 5370b1039f0SRamuthevar Vadivel Murugan case NAND_OP_ADDR_INSTR: 5380b1039f0SRamuthevar Vadivel Murugan for (i = 0; i < instr->ctx.addr.naddrs; i++) 5390b1039f0SRamuthevar Vadivel Murugan ebu_nand_writeb(chip, 5400b1039f0SRamuthevar Vadivel Murugan HSNAND_ALE_OFFS | HSNAND_CS_OFFS, 5410b1039f0SRamuthevar Vadivel Murugan instr->ctx.addr.addrs[i]); 5420b1039f0SRamuthevar Vadivel Murugan break; 5430b1039f0SRamuthevar Vadivel Murugan 5440b1039f0SRamuthevar Vadivel Murugan case NAND_OP_DATA_IN_INSTR: 5450b1039f0SRamuthevar Vadivel Murugan ebu_read_buf(chip, instr->ctx.data.buf.in, 5460b1039f0SRamuthevar Vadivel Murugan instr->ctx.data.len); 5470b1039f0SRamuthevar Vadivel Murugan break; 5480b1039f0SRamuthevar Vadivel Murugan 5490b1039f0SRamuthevar Vadivel Murugan case NAND_OP_DATA_OUT_INSTR: 5500b1039f0SRamuthevar Vadivel Murugan ebu_write_buf(chip, instr->ctx.data.buf.out, 5510b1039f0SRamuthevar Vadivel Murugan instr->ctx.data.len); 5520b1039f0SRamuthevar Vadivel Murugan break; 5530b1039f0SRamuthevar Vadivel Murugan 5540b1039f0SRamuthevar Vadivel Murugan case NAND_OP_WAITRDY_INSTR: 5550b1039f0SRamuthevar Vadivel Murugan timeout_ms = instr->ctx.waitrdy.timeout_ms * 1000; 5560b1039f0SRamuthevar Vadivel Murugan ret = ebu_nand_waitrdy(chip, timeout_ms); 5570b1039f0SRamuthevar Vadivel Murugan break; 5580b1039f0SRamuthevar Vadivel Murugan } 5590b1039f0SRamuthevar Vadivel Murugan } 5600b1039f0SRamuthevar Vadivel Murugan 5610b1039f0SRamuthevar Vadivel Murugan return ret; 5620b1039f0SRamuthevar Vadivel Murugan } 5630b1039f0SRamuthevar Vadivel Murugan 5640b1039f0SRamuthevar Vadivel Murugan static const struct nand_controller_ops ebu_nand_controller_ops = { 5650b1039f0SRamuthevar Vadivel Murugan .attach_chip = ebu_nand_attach_chip, 5660b1039f0SRamuthevar Vadivel Murugan .setup_interface = ebu_nand_set_timings, 5670b1039f0SRamuthevar Vadivel Murugan .exec_op = ebu_nand_exec_op, 5680b1039f0SRamuthevar Vadivel Murugan }; 5690b1039f0SRamuthevar Vadivel Murugan 5700b1039f0SRamuthevar Vadivel Murugan static void ebu_dma_cleanup(struct ebu_nand_controller *ebu_host) 5710b1039f0SRamuthevar Vadivel Murugan { 5720b1039f0SRamuthevar Vadivel Murugan if (ebu_host->dma_rx) 5730b1039f0SRamuthevar Vadivel Murugan dma_release_channel(ebu_host->dma_rx); 5740b1039f0SRamuthevar Vadivel Murugan 5750b1039f0SRamuthevar Vadivel Murugan if (ebu_host->dma_tx) 5760b1039f0SRamuthevar Vadivel Murugan dma_release_channel(ebu_host->dma_tx); 5770b1039f0SRamuthevar Vadivel Murugan } 5780b1039f0SRamuthevar Vadivel Murugan 5790b1039f0SRamuthevar Vadivel Murugan static int ebu_nand_probe(struct platform_device *pdev) 5800b1039f0SRamuthevar Vadivel Murugan { 5810b1039f0SRamuthevar Vadivel Murugan struct device *dev = &pdev->dev; 5820b1039f0SRamuthevar Vadivel Murugan struct ebu_nand_controller *ebu_host; 5830b1039f0SRamuthevar Vadivel Murugan struct nand_chip *nand; 58418f62614SMartin Blumenstingl struct mtd_info *mtd; 5850b1039f0SRamuthevar Vadivel Murugan struct resource *res; 5860b1039f0SRamuthevar Vadivel Murugan char *resname; 5870b1039f0SRamuthevar Vadivel Murugan int ret; 5880b1039f0SRamuthevar Vadivel Murugan u32 cs; 5890b1039f0SRamuthevar Vadivel Murugan 5900b1039f0SRamuthevar Vadivel Murugan ebu_host = devm_kzalloc(dev, sizeof(*ebu_host), GFP_KERNEL); 5910b1039f0SRamuthevar Vadivel Murugan if (!ebu_host) 5920b1039f0SRamuthevar Vadivel Murugan return -ENOMEM; 5930b1039f0SRamuthevar Vadivel Murugan 5940b1039f0SRamuthevar Vadivel Murugan ebu_host->dev = dev; 5950b1039f0SRamuthevar Vadivel Murugan nand_controller_init(&ebu_host->controller); 5960b1039f0SRamuthevar Vadivel Murugan 5970b1039f0SRamuthevar Vadivel Murugan res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ebunand"); 5980b1039f0SRamuthevar Vadivel Murugan ebu_host->ebu = devm_ioremap_resource(&pdev->dev, res); 5990b1039f0SRamuthevar Vadivel Murugan if (IS_ERR(ebu_host->ebu)) 6000b1039f0SRamuthevar Vadivel Murugan return PTR_ERR(ebu_host->ebu); 6010b1039f0SRamuthevar Vadivel Murugan 6020b1039f0SRamuthevar Vadivel Murugan res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hsnand"); 6030b1039f0SRamuthevar Vadivel Murugan ebu_host->hsnand = devm_ioremap_resource(&pdev->dev, res); 6040b1039f0SRamuthevar Vadivel Murugan if (IS_ERR(ebu_host->hsnand)) 6050b1039f0SRamuthevar Vadivel Murugan return PTR_ERR(ebu_host->hsnand); 6060b1039f0SRamuthevar Vadivel Murugan 6070b1039f0SRamuthevar Vadivel Murugan ret = device_property_read_u32(dev, "reg", &cs); 6080b1039f0SRamuthevar Vadivel Murugan if (ret) { 6090b1039f0SRamuthevar Vadivel Murugan dev_err(dev, "failed to get chip select: %d\n", ret); 6100b1039f0SRamuthevar Vadivel Murugan return ret; 6110b1039f0SRamuthevar Vadivel Murugan } 612*46a0dc10SEvgeny Novikov if (cs >= MAX_CS) { 613*46a0dc10SEvgeny Novikov dev_err(dev, "got invalid chip select: %d\n", cs); 614*46a0dc10SEvgeny Novikov return -EINVAL; 615*46a0dc10SEvgeny Novikov } 616*46a0dc10SEvgeny Novikov 6170b1039f0SRamuthevar Vadivel Murugan ebu_host->cs_num = cs; 6180b1039f0SRamuthevar Vadivel Murugan 6190b1039f0SRamuthevar Vadivel Murugan resname = devm_kasprintf(dev, GFP_KERNEL, "nand_cs%d", cs); 6200b1039f0SRamuthevar Vadivel Murugan res = platform_get_resource_byname(pdev, IORESOURCE_MEM, resname); 6210b1039f0SRamuthevar Vadivel Murugan ebu_host->cs[cs].chipaddr = devm_ioremap_resource(dev, res); 6220b1039f0SRamuthevar Vadivel Murugan ebu_host->cs[cs].nand_pa = res->start; 6230b1039f0SRamuthevar Vadivel Murugan if (IS_ERR(ebu_host->cs[cs].chipaddr)) 6240b1039f0SRamuthevar Vadivel Murugan return PTR_ERR(ebu_host->cs[cs].chipaddr); 6250b1039f0SRamuthevar Vadivel Murugan 6260b1039f0SRamuthevar Vadivel Murugan ebu_host->clk = devm_clk_get(dev, NULL); 6270b1039f0SRamuthevar Vadivel Murugan if (IS_ERR(ebu_host->clk)) 6280b1039f0SRamuthevar Vadivel Murugan return dev_err_probe(dev, PTR_ERR(ebu_host->clk), 6290b1039f0SRamuthevar Vadivel Murugan "failed to get clock\n"); 6300b1039f0SRamuthevar Vadivel Murugan 6310b1039f0SRamuthevar Vadivel Murugan ret = clk_prepare_enable(ebu_host->clk); 6320b1039f0SRamuthevar Vadivel Murugan if (ret) { 6330b1039f0SRamuthevar Vadivel Murugan dev_err(dev, "failed to enable clock: %d\n", ret); 6340b1039f0SRamuthevar Vadivel Murugan return ret; 6350b1039f0SRamuthevar Vadivel Murugan } 6360b1039f0SRamuthevar Vadivel Murugan ebu_host->clk_rate = clk_get_rate(ebu_host->clk); 6370b1039f0SRamuthevar Vadivel Murugan 6380b1039f0SRamuthevar Vadivel Murugan ebu_host->dma_tx = dma_request_chan(dev, "tx"); 6390792ec82SEvgeny Novikov if (IS_ERR(ebu_host->dma_tx)) { 6400792ec82SEvgeny Novikov ret = dev_err_probe(dev, PTR_ERR(ebu_host->dma_tx), 6410b1039f0SRamuthevar Vadivel Murugan "failed to request DMA tx chan!.\n"); 6420792ec82SEvgeny Novikov goto err_disable_unprepare_clk; 6430792ec82SEvgeny Novikov } 6440b1039f0SRamuthevar Vadivel Murugan 6450b1039f0SRamuthevar Vadivel Murugan ebu_host->dma_rx = dma_request_chan(dev, "rx"); 6460792ec82SEvgeny Novikov if (IS_ERR(ebu_host->dma_rx)) { 6470792ec82SEvgeny Novikov ret = dev_err_probe(dev, PTR_ERR(ebu_host->dma_rx), 6480b1039f0SRamuthevar Vadivel Murugan "failed to request DMA rx chan!.\n"); 6490792ec82SEvgeny Novikov ebu_host->dma_rx = NULL; 6500792ec82SEvgeny Novikov goto err_cleanup_dma; 6510792ec82SEvgeny Novikov } 6520b1039f0SRamuthevar Vadivel Murugan 6530b1039f0SRamuthevar Vadivel Murugan resname = devm_kasprintf(dev, GFP_KERNEL, "addr_sel%d", cs); 6540b1039f0SRamuthevar Vadivel Murugan res = platform_get_resource_byname(pdev, IORESOURCE_MEM, resname); 6550792ec82SEvgeny Novikov if (!res) { 6560792ec82SEvgeny Novikov ret = -EINVAL; 6570792ec82SEvgeny Novikov goto err_cleanup_dma; 6580792ec82SEvgeny Novikov } 6590b1039f0SRamuthevar Vadivel Murugan ebu_host->cs[cs].addr_sel = res->start; 6600b1039f0SRamuthevar Vadivel Murugan writel(ebu_host->cs[cs].addr_sel | EBU_ADDR_MASK(5) | EBU_ADDR_SEL_REGEN, 6610b1039f0SRamuthevar Vadivel Murugan ebu_host->ebu + EBU_ADDR_SEL(cs)); 6620b1039f0SRamuthevar Vadivel Murugan 6630b1039f0SRamuthevar Vadivel Murugan nand_set_flash_node(&ebu_host->chip, dev->of_node); 66418f62614SMartin Blumenstingl 66518f62614SMartin Blumenstingl mtd = nand_to_mtd(&ebu_host->chip); 6660b1039f0SRamuthevar Vadivel Murugan if (!mtd->name) { 6670b1039f0SRamuthevar Vadivel Murugan dev_err(ebu_host->dev, "NAND label property is mandatory\n"); 6680792ec82SEvgeny Novikov ret = -EINVAL; 6690792ec82SEvgeny Novikov goto err_cleanup_dma; 6700b1039f0SRamuthevar Vadivel Murugan } 6710b1039f0SRamuthevar Vadivel Murugan 6720b1039f0SRamuthevar Vadivel Murugan mtd->dev.parent = dev; 6730b1039f0SRamuthevar Vadivel Murugan ebu_host->dev = dev; 6740b1039f0SRamuthevar Vadivel Murugan 6750b1039f0SRamuthevar Vadivel Murugan platform_set_drvdata(pdev, ebu_host); 6760b1039f0SRamuthevar Vadivel Murugan nand_set_controller_data(&ebu_host->chip, ebu_host); 6770b1039f0SRamuthevar Vadivel Murugan 6780b1039f0SRamuthevar Vadivel Murugan nand = &ebu_host->chip; 6790b1039f0SRamuthevar Vadivel Murugan nand->controller = &ebu_host->controller; 6800b1039f0SRamuthevar Vadivel Murugan nand->controller->ops = &ebu_nand_controller_ops; 6810b1039f0SRamuthevar Vadivel Murugan 6820b1039f0SRamuthevar Vadivel Murugan /* Scan to find existence of the device */ 6830b1039f0SRamuthevar Vadivel Murugan ret = nand_scan(&ebu_host->chip, 1); 6840b1039f0SRamuthevar Vadivel Murugan if (ret) 6850b1039f0SRamuthevar Vadivel Murugan goto err_cleanup_dma; 6860b1039f0SRamuthevar Vadivel Murugan 6870b1039f0SRamuthevar Vadivel Murugan ret = mtd_device_register(mtd, NULL, 0); 6880b1039f0SRamuthevar Vadivel Murugan if (ret) 6890b1039f0SRamuthevar Vadivel Murugan goto err_clean_nand; 6900b1039f0SRamuthevar Vadivel Murugan 6910b1039f0SRamuthevar Vadivel Murugan return 0; 6920b1039f0SRamuthevar Vadivel Murugan 6930b1039f0SRamuthevar Vadivel Murugan err_clean_nand: 6940b1039f0SRamuthevar Vadivel Murugan nand_cleanup(&ebu_host->chip); 6950b1039f0SRamuthevar Vadivel Murugan err_cleanup_dma: 6960b1039f0SRamuthevar Vadivel Murugan ebu_dma_cleanup(ebu_host); 6970792ec82SEvgeny Novikov err_disable_unprepare_clk: 6980b1039f0SRamuthevar Vadivel Murugan clk_disable_unprepare(ebu_host->clk); 6990b1039f0SRamuthevar Vadivel Murugan 7000b1039f0SRamuthevar Vadivel Murugan return ret; 7010b1039f0SRamuthevar Vadivel Murugan } 7020b1039f0SRamuthevar Vadivel Murugan 7030b1039f0SRamuthevar Vadivel Murugan static int ebu_nand_remove(struct platform_device *pdev) 7040b1039f0SRamuthevar Vadivel Murugan { 7050b1039f0SRamuthevar Vadivel Murugan struct ebu_nand_controller *ebu_host = platform_get_drvdata(pdev); 7060b1039f0SRamuthevar Vadivel Murugan int ret; 7070b1039f0SRamuthevar Vadivel Murugan 7080b1039f0SRamuthevar Vadivel Murugan ret = mtd_device_unregister(nand_to_mtd(&ebu_host->chip)); 7090b1039f0SRamuthevar Vadivel Murugan WARN_ON(ret); 7100b1039f0SRamuthevar Vadivel Murugan nand_cleanup(&ebu_host->chip); 7110b1039f0SRamuthevar Vadivel Murugan ebu_nand_disable(&ebu_host->chip); 7120b1039f0SRamuthevar Vadivel Murugan ebu_dma_cleanup(ebu_host); 7130b1039f0SRamuthevar Vadivel Murugan clk_disable_unprepare(ebu_host->clk); 7140b1039f0SRamuthevar Vadivel Murugan 7150b1039f0SRamuthevar Vadivel Murugan return 0; 7160b1039f0SRamuthevar Vadivel Murugan } 7170b1039f0SRamuthevar Vadivel Murugan 7180b1039f0SRamuthevar Vadivel Murugan static const struct of_device_id ebu_nand_match[] = { 7190b1039f0SRamuthevar Vadivel Murugan { .compatible = "intel,nand-controller" }, 7200b1039f0SRamuthevar Vadivel Murugan { .compatible = "intel,lgm-ebunand" }, 7210b1039f0SRamuthevar Vadivel Murugan {} 7220b1039f0SRamuthevar Vadivel Murugan }; 7230b1039f0SRamuthevar Vadivel Murugan MODULE_DEVICE_TABLE(of, ebu_nand_match); 7240b1039f0SRamuthevar Vadivel Murugan 7250b1039f0SRamuthevar Vadivel Murugan static struct platform_driver ebu_nand_driver = { 7260b1039f0SRamuthevar Vadivel Murugan .probe = ebu_nand_probe, 7270b1039f0SRamuthevar Vadivel Murugan .remove = ebu_nand_remove, 7280b1039f0SRamuthevar Vadivel Murugan .driver = { 7290b1039f0SRamuthevar Vadivel Murugan .name = "intel-nand-controller", 7300b1039f0SRamuthevar Vadivel Murugan .of_match_table = ebu_nand_match, 7310b1039f0SRamuthevar Vadivel Murugan }, 7320b1039f0SRamuthevar Vadivel Murugan 7330b1039f0SRamuthevar Vadivel Murugan }; 7340b1039f0SRamuthevar Vadivel Murugan module_platform_driver(ebu_nand_driver); 7350b1039f0SRamuthevar Vadivel Murugan 7360b1039f0SRamuthevar Vadivel Murugan MODULE_LICENSE("GPL v2"); 7370b1039f0SRamuthevar Vadivel Murugan MODULE_AUTHOR("Vadivel Murugan R <vadivel.muruganx.ramuthevar@intel.com>"); 7380b1039f0SRamuthevar Vadivel Murugan MODULE_DESCRIPTION("Intel's LGM External Bus NAND Controller driver"); 739