1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 22944a44dSRoland Stigge /* 32944a44dSRoland Stigge * NXP LPC32XX NAND SLC driver 42944a44dSRoland Stigge * 52944a44dSRoland Stigge * Authors: 62944a44dSRoland Stigge * Kevin Wells <kevin.wells@nxp.com> 72944a44dSRoland Stigge * Roland Stigge <stigge@antcom.de> 82944a44dSRoland Stigge * 92944a44dSRoland Stigge * Copyright © 2011 NXP Semiconductors 102944a44dSRoland Stigge * Copyright © 2012 Roland Stigge 112944a44dSRoland Stigge */ 122944a44dSRoland Stigge 132944a44dSRoland Stigge #include <linux/slab.h> 142944a44dSRoland Stigge #include <linux/module.h> 152944a44dSRoland Stigge #include <linux/platform_device.h> 162944a44dSRoland Stigge #include <linux/mtd/mtd.h> 17d4092d76SBoris Brezillon #include <linux/mtd/rawnand.h> 182944a44dSRoland Stigge #include <linux/mtd/partitions.h> 192944a44dSRoland Stigge #include <linux/clk.h> 202944a44dSRoland Stigge #include <linux/err.h> 212944a44dSRoland Stigge #include <linux/delay.h> 222944a44dSRoland Stigge #include <linux/io.h> 232944a44dSRoland Stigge #include <linux/mm.h> 242944a44dSRoland Stigge #include <linux/dma-mapping.h> 252944a44dSRoland Stigge #include <linux/dmaengine.h> 262944a44dSRoland Stigge #include <linux/gpio.h> 272944a44dSRoland Stigge #include <linux/of.h> 282944a44dSRoland Stigge #include <linux/of_gpio.h> 29de20c22dSRoland Stigge #include <linux/mtd/lpc32xx_slc.h> 30*c4b7d7c4SMiquel Raynal #include <linux/mtd/nand-ecc-sw-hamming.h> 312944a44dSRoland Stigge 322944a44dSRoland Stigge #define LPC32XX_MODNAME "lpc32xx-nand" 332944a44dSRoland Stigge 342944a44dSRoland Stigge /********************************************************************** 352944a44dSRoland Stigge * SLC NAND controller register offsets 362944a44dSRoland Stigge **********************************************************************/ 372944a44dSRoland Stigge 382944a44dSRoland Stigge #define SLC_DATA(x) (x + 0x000) 392944a44dSRoland Stigge #define SLC_ADDR(x) (x + 0x004) 402944a44dSRoland Stigge #define SLC_CMD(x) (x + 0x008) 412944a44dSRoland Stigge #define SLC_STOP(x) (x + 0x00C) 422944a44dSRoland Stigge #define SLC_CTRL(x) (x + 0x010) 432944a44dSRoland Stigge #define SLC_CFG(x) (x + 0x014) 442944a44dSRoland Stigge #define SLC_STAT(x) (x + 0x018) 452944a44dSRoland Stigge #define SLC_INT_STAT(x) (x + 0x01C) 462944a44dSRoland Stigge #define SLC_IEN(x) (x + 0x020) 472944a44dSRoland Stigge #define SLC_ISR(x) (x + 0x024) 482944a44dSRoland Stigge #define SLC_ICR(x) (x + 0x028) 492944a44dSRoland Stigge #define SLC_TAC(x) (x + 0x02C) 502944a44dSRoland Stigge #define SLC_TC(x) (x + 0x030) 512944a44dSRoland Stigge #define SLC_ECC(x) (x + 0x034) 522944a44dSRoland Stigge #define SLC_DMA_DATA(x) (x + 0x038) 532944a44dSRoland Stigge 542944a44dSRoland Stigge /********************************************************************** 552944a44dSRoland Stigge * slc_ctrl register definitions 562944a44dSRoland Stigge **********************************************************************/ 572944a44dSRoland Stigge #define SLCCTRL_SW_RESET (1 << 2) /* Reset the NAND controller bit */ 582944a44dSRoland Stigge #define SLCCTRL_ECC_CLEAR (1 << 1) /* Reset ECC bit */ 592944a44dSRoland Stigge #define SLCCTRL_DMA_START (1 << 0) /* Start DMA channel bit */ 602944a44dSRoland Stigge 612944a44dSRoland Stigge /********************************************************************** 622944a44dSRoland Stigge * slc_cfg register definitions 632944a44dSRoland Stigge **********************************************************************/ 642944a44dSRoland Stigge #define SLCCFG_CE_LOW (1 << 5) /* Force CE low bit */ 652944a44dSRoland Stigge #define SLCCFG_DMA_ECC (1 << 4) /* Enable DMA ECC bit */ 662944a44dSRoland Stigge #define SLCCFG_ECC_EN (1 << 3) /* ECC enable bit */ 672944a44dSRoland Stigge #define SLCCFG_DMA_BURST (1 << 2) /* DMA burst bit */ 682944a44dSRoland Stigge #define SLCCFG_DMA_DIR (1 << 1) /* DMA write(0)/read(1) bit */ 692944a44dSRoland Stigge #define SLCCFG_WIDTH (1 << 0) /* External device width, 0=8bit */ 702944a44dSRoland Stigge 712944a44dSRoland Stigge /********************************************************************** 722944a44dSRoland Stigge * slc_stat register definitions 732944a44dSRoland Stigge **********************************************************************/ 742944a44dSRoland Stigge #define SLCSTAT_DMA_FIFO (1 << 2) /* DMA FIFO has data bit */ 752944a44dSRoland Stigge #define SLCSTAT_SLC_FIFO (1 << 1) /* SLC FIFO has data bit */ 762944a44dSRoland Stigge #define SLCSTAT_NAND_READY (1 << 0) /* NAND device is ready bit */ 772944a44dSRoland Stigge 782944a44dSRoland Stigge /********************************************************************** 792944a44dSRoland Stigge * slc_int_stat, slc_ien, slc_isr, and slc_icr register definitions 802944a44dSRoland Stigge **********************************************************************/ 812944a44dSRoland Stigge #define SLCSTAT_INT_TC (1 << 1) /* Transfer count bit */ 822944a44dSRoland Stigge #define SLCSTAT_INT_RDY_EN (1 << 0) /* Ready interrupt bit */ 832944a44dSRoland Stigge 842944a44dSRoland Stigge /********************************************************************** 852944a44dSRoland Stigge * slc_tac register definitions 862944a44dSRoland Stigge **********************************************************************/ 87641f6342SVladimir Zapolskiy /* Computation of clock cycles on basis of controller and device clock rates */ 88d54e8801SVladimir Zapolskiy #define SLCTAC_CLOCKS(c, n, s) (min_t(u32, DIV_ROUND_UP(c, n) - 1, 0xF) << s) 89641f6342SVladimir Zapolskiy 902944a44dSRoland Stigge /* Clock setting for RDY write sample wait time in 2*n clocks */ 912944a44dSRoland Stigge #define SLCTAC_WDR(n) (((n) & 0xF) << 28) 922944a44dSRoland Stigge /* Write pulse width in clock cycles, 1 to 16 clocks */ 93641f6342SVladimir Zapolskiy #define SLCTAC_WWIDTH(c, n) (SLCTAC_CLOCKS(c, n, 24)) 942944a44dSRoland Stigge /* Write hold time of control and data signals, 1 to 16 clocks */ 95641f6342SVladimir Zapolskiy #define SLCTAC_WHOLD(c, n) (SLCTAC_CLOCKS(c, n, 20)) 962944a44dSRoland Stigge /* Write setup time of control and data signals, 1 to 16 clocks */ 97641f6342SVladimir Zapolskiy #define SLCTAC_WSETUP(c, n) (SLCTAC_CLOCKS(c, n, 16)) 982944a44dSRoland Stigge /* Clock setting for RDY read sample wait time in 2*n clocks */ 992944a44dSRoland Stigge #define SLCTAC_RDR(n) (((n) & 0xF) << 12) 1002944a44dSRoland Stigge /* Read pulse width in clock cycles, 1 to 16 clocks */ 101641f6342SVladimir Zapolskiy #define SLCTAC_RWIDTH(c, n) (SLCTAC_CLOCKS(c, n, 8)) 1022944a44dSRoland Stigge /* Read hold time of control and data signals, 1 to 16 clocks */ 103641f6342SVladimir Zapolskiy #define SLCTAC_RHOLD(c, n) (SLCTAC_CLOCKS(c, n, 4)) 1042944a44dSRoland Stigge /* Read setup time of control and data signals, 1 to 16 clocks */ 105641f6342SVladimir Zapolskiy #define SLCTAC_RSETUP(c, n) (SLCTAC_CLOCKS(c, n, 0)) 1062944a44dSRoland Stigge 1072944a44dSRoland Stigge /********************************************************************** 1082944a44dSRoland Stigge * slc_ecc register definitions 1092944a44dSRoland Stigge **********************************************************************/ 1102944a44dSRoland Stigge /* ECC line party fetch macro */ 1112944a44dSRoland Stigge #define SLCECC_TO_LINEPAR(n) (((n) >> 6) & 0x7FFF) 1122944a44dSRoland Stigge #define SLCECC_TO_COLPAR(n) ((n) & 0x3F) 1132944a44dSRoland Stigge 1142944a44dSRoland Stigge /* 1152944a44dSRoland Stigge * DMA requires storage space for the DMA local buffer and the hardware ECC 1162944a44dSRoland Stigge * storage area. The DMA local buffer is only used if DMA mapping fails 1172944a44dSRoland Stigge * during runtime. 1182944a44dSRoland Stigge */ 1192944a44dSRoland Stigge #define LPC32XX_DMA_DATA_SIZE 4096 1202944a44dSRoland Stigge #define LPC32XX_ECC_SAVE_SIZE ((4096 / 256) * 4) 1212944a44dSRoland Stigge 1222944a44dSRoland Stigge /* Number of bytes used for ECC stored in NAND per 256 bytes */ 1232944a44dSRoland Stigge #define LPC32XX_SLC_DEV_ECC_BYTES 3 1242944a44dSRoland Stigge 1252944a44dSRoland Stigge /* 1262944a44dSRoland Stigge * If the NAND base clock frequency can't be fetched, this frequency will be 1272944a44dSRoland Stigge * used instead as the base. This rate is used to setup the timing registers 1282944a44dSRoland Stigge * used for NAND accesses. 1292944a44dSRoland Stigge */ 1302944a44dSRoland Stigge #define LPC32XX_DEF_BUS_RATE 133250000 1312944a44dSRoland Stigge 1322944a44dSRoland Stigge /* Milliseconds for DMA FIFO timeout (unlikely anyway) */ 1332944a44dSRoland Stigge #define LPC32XX_DMA_TIMEOUT 100 1342944a44dSRoland Stigge 1352944a44dSRoland Stigge /* 1362944a44dSRoland Stigge * NAND ECC Layout for small page NAND devices 1372944a44dSRoland Stigge * Note: For large and huge page devices, the default layouts are used 1382944a44dSRoland Stigge */ 139d50b5239SBoris Brezillon static int lpc32xx_ooblayout_ecc(struct mtd_info *mtd, int section, 140d50b5239SBoris Brezillon struct mtd_oob_region *oobregion) 141d50b5239SBoris Brezillon { 142d50b5239SBoris Brezillon if (section) 143d50b5239SBoris Brezillon return -ERANGE; 144d50b5239SBoris Brezillon 145d50b5239SBoris Brezillon oobregion->length = 6; 146d50b5239SBoris Brezillon oobregion->offset = 10; 147d50b5239SBoris Brezillon 148d50b5239SBoris Brezillon return 0; 149d50b5239SBoris Brezillon } 150d50b5239SBoris Brezillon 151d50b5239SBoris Brezillon static int lpc32xx_ooblayout_free(struct mtd_info *mtd, int section, 152d50b5239SBoris Brezillon struct mtd_oob_region *oobregion) 153d50b5239SBoris Brezillon { 154d50b5239SBoris Brezillon if (section > 1) 155d50b5239SBoris Brezillon return -ERANGE; 156d50b5239SBoris Brezillon 157d50b5239SBoris Brezillon if (!section) { 158d50b5239SBoris Brezillon oobregion->offset = 0; 159d50b5239SBoris Brezillon oobregion->length = 4; 160d50b5239SBoris Brezillon } else { 161d50b5239SBoris Brezillon oobregion->offset = 6; 162d50b5239SBoris Brezillon oobregion->length = 4; 163d50b5239SBoris Brezillon } 164d50b5239SBoris Brezillon 165d50b5239SBoris Brezillon return 0; 166d50b5239SBoris Brezillon } 167d50b5239SBoris Brezillon 168d50b5239SBoris Brezillon static const struct mtd_ooblayout_ops lpc32xx_ooblayout_ops = { 169d50b5239SBoris Brezillon .ecc = lpc32xx_ooblayout_ecc, 170d50b5239SBoris Brezillon .free = lpc32xx_ooblayout_free, 1712944a44dSRoland Stigge }; 1722944a44dSRoland Stigge 1732944a44dSRoland Stigge static u8 bbt_pattern[] = {'B', 'b', 't', '0' }; 1742944a44dSRoland Stigge static u8 mirror_pattern[] = {'1', 't', 'b', 'B' }; 1752944a44dSRoland Stigge 1762944a44dSRoland Stigge /* 1772944a44dSRoland Stigge * Small page FLASH BBT descriptors, marker at offset 0, version at offset 6 1782944a44dSRoland Stigge * Note: Large page devices used the default layout 1792944a44dSRoland Stigge */ 1802944a44dSRoland Stigge static struct nand_bbt_descr bbt_smallpage_main_descr = { 1812944a44dSRoland Stigge .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE 1822944a44dSRoland Stigge | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, 1832944a44dSRoland Stigge .offs = 0, 1842944a44dSRoland Stigge .len = 4, 1852944a44dSRoland Stigge .veroffs = 6, 1862944a44dSRoland Stigge .maxblocks = 4, 1872944a44dSRoland Stigge .pattern = bbt_pattern 1882944a44dSRoland Stigge }; 1892944a44dSRoland Stigge 1902944a44dSRoland Stigge static struct nand_bbt_descr bbt_smallpage_mirror_descr = { 1912944a44dSRoland Stigge .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE 1922944a44dSRoland Stigge | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, 1932944a44dSRoland Stigge .offs = 0, 1942944a44dSRoland Stigge .len = 4, 1952944a44dSRoland Stigge .veroffs = 6, 1962944a44dSRoland Stigge .maxblocks = 4, 1972944a44dSRoland Stigge .pattern = mirror_pattern 1982944a44dSRoland Stigge }; 1992944a44dSRoland Stigge 2002944a44dSRoland Stigge /* 2012944a44dSRoland Stigge * NAND platform configuration structure 2022944a44dSRoland Stigge */ 2032944a44dSRoland Stigge struct lpc32xx_nand_cfg_slc { 2042944a44dSRoland Stigge uint32_t wdr_clks; 2052944a44dSRoland Stigge uint32_t wwidth; 2062944a44dSRoland Stigge uint32_t whold; 2072944a44dSRoland Stigge uint32_t wsetup; 2082944a44dSRoland Stigge uint32_t rdr_clks; 2092944a44dSRoland Stigge uint32_t rwidth; 2102944a44dSRoland Stigge uint32_t rhold; 2112944a44dSRoland Stigge uint32_t rsetup; 212df63fe76SAlexandre Pereira da Silva int wp_gpio; 2132944a44dSRoland Stigge struct mtd_partition *parts; 2142944a44dSRoland Stigge unsigned num_parts; 2152944a44dSRoland Stigge }; 2162944a44dSRoland Stigge 2172944a44dSRoland Stigge struct lpc32xx_nand_host { 2182944a44dSRoland Stigge struct nand_chip nand_chip; 219de20c22dSRoland Stigge struct lpc32xx_slc_platform_data *pdata; 2202944a44dSRoland Stigge struct clk *clk; 2212944a44dSRoland Stigge void __iomem *io_base; 2222944a44dSRoland Stigge struct lpc32xx_nand_cfg_slc *ncfg; 2232944a44dSRoland Stigge 2242944a44dSRoland Stigge struct completion comp; 2252944a44dSRoland Stigge struct dma_chan *dma_chan; 2262944a44dSRoland Stigge uint32_t dma_buf_len; 2272944a44dSRoland Stigge struct dma_slave_config dma_slave_config; 2282944a44dSRoland Stigge struct scatterlist sgl; 2292944a44dSRoland Stigge 2302944a44dSRoland Stigge /* 2312944a44dSRoland Stigge * DMA and CPU addresses of ECC work area and data buffer 2322944a44dSRoland Stigge */ 2332944a44dSRoland Stigge uint32_t *ecc_buf; 2342944a44dSRoland Stigge uint8_t *data_buf; 2352944a44dSRoland Stigge dma_addr_t io_base_dma; 2362944a44dSRoland Stigge }; 2372944a44dSRoland Stigge 2382944a44dSRoland Stigge static void lpc32xx_nand_setup(struct lpc32xx_nand_host *host) 2392944a44dSRoland Stigge { 2402944a44dSRoland Stigge uint32_t clkrate, tmp; 2412944a44dSRoland Stigge 2422944a44dSRoland Stigge /* Reset SLC controller */ 2432944a44dSRoland Stigge writel(SLCCTRL_SW_RESET, SLC_CTRL(host->io_base)); 2442944a44dSRoland Stigge udelay(1000); 2452944a44dSRoland Stigge 2462944a44dSRoland Stigge /* Basic setup */ 2472944a44dSRoland Stigge writel(0, SLC_CFG(host->io_base)); 2482944a44dSRoland Stigge writel(0, SLC_IEN(host->io_base)); 2492944a44dSRoland Stigge writel((SLCSTAT_INT_TC | SLCSTAT_INT_RDY_EN), 2502944a44dSRoland Stigge SLC_ICR(host->io_base)); 2512944a44dSRoland Stigge 2522944a44dSRoland Stigge /* Get base clock for SLC block */ 2532944a44dSRoland Stigge clkrate = clk_get_rate(host->clk); 2542944a44dSRoland Stigge if (clkrate == 0) 2552944a44dSRoland Stigge clkrate = LPC32XX_DEF_BUS_RATE; 2562944a44dSRoland Stigge 2572944a44dSRoland Stigge /* Compute clock setup values */ 2582944a44dSRoland Stigge tmp = SLCTAC_WDR(host->ncfg->wdr_clks) | 259641f6342SVladimir Zapolskiy SLCTAC_WWIDTH(clkrate, host->ncfg->wwidth) | 260641f6342SVladimir Zapolskiy SLCTAC_WHOLD(clkrate, host->ncfg->whold) | 261641f6342SVladimir Zapolskiy SLCTAC_WSETUP(clkrate, host->ncfg->wsetup) | 2622944a44dSRoland Stigge SLCTAC_RDR(host->ncfg->rdr_clks) | 263641f6342SVladimir Zapolskiy SLCTAC_RWIDTH(clkrate, host->ncfg->rwidth) | 264641f6342SVladimir Zapolskiy SLCTAC_RHOLD(clkrate, host->ncfg->rhold) | 265641f6342SVladimir Zapolskiy SLCTAC_RSETUP(clkrate, host->ncfg->rsetup); 2662944a44dSRoland Stigge writel(tmp, SLC_TAC(host->io_base)); 2672944a44dSRoland Stigge } 2682944a44dSRoland Stigge 2692944a44dSRoland Stigge /* 2702944a44dSRoland Stigge * Hardware specific access to control lines 2712944a44dSRoland Stigge */ 2720f808c16SBoris Brezillon static void lpc32xx_nand_cmd_ctrl(struct nand_chip *chip, int cmd, 2732944a44dSRoland Stigge unsigned int ctrl) 2742944a44dSRoland Stigge { 2752944a44dSRoland Stigge uint32_t tmp; 276d699ed25SBoris BREZILLON struct lpc32xx_nand_host *host = nand_get_controller_data(chip); 2772944a44dSRoland Stigge 2782944a44dSRoland Stigge /* Does CE state need to be changed? */ 2792944a44dSRoland Stigge tmp = readl(SLC_CFG(host->io_base)); 2802944a44dSRoland Stigge if (ctrl & NAND_NCE) 2812944a44dSRoland Stigge tmp |= SLCCFG_CE_LOW; 2822944a44dSRoland Stigge else 2832944a44dSRoland Stigge tmp &= ~SLCCFG_CE_LOW; 2842944a44dSRoland Stigge writel(tmp, SLC_CFG(host->io_base)); 2852944a44dSRoland Stigge 2862944a44dSRoland Stigge if (cmd != NAND_CMD_NONE) { 2872944a44dSRoland Stigge if (ctrl & NAND_CLE) 2882944a44dSRoland Stigge writel(cmd, SLC_CMD(host->io_base)); 2892944a44dSRoland Stigge else 2902944a44dSRoland Stigge writel(cmd, SLC_ADDR(host->io_base)); 2912944a44dSRoland Stigge } 2922944a44dSRoland Stigge } 2932944a44dSRoland Stigge 2942944a44dSRoland Stigge /* 2952944a44dSRoland Stigge * Read the Device Ready pin 2962944a44dSRoland Stigge */ 29750a487e7SBoris Brezillon static int lpc32xx_nand_device_ready(struct nand_chip *chip) 2982944a44dSRoland Stigge { 299d699ed25SBoris BREZILLON struct lpc32xx_nand_host *host = nand_get_controller_data(chip); 3002944a44dSRoland Stigge int rdy = 0; 3012944a44dSRoland Stigge 3022944a44dSRoland Stigge if ((readl(SLC_STAT(host->io_base)) & SLCSTAT_NAND_READY) != 0) 3032944a44dSRoland Stigge rdy = 1; 3042944a44dSRoland Stigge 3052944a44dSRoland Stigge return rdy; 3062944a44dSRoland Stigge } 3072944a44dSRoland Stigge 3082944a44dSRoland Stigge /* 3092944a44dSRoland Stigge * Enable NAND write protect 3102944a44dSRoland Stigge */ 3112944a44dSRoland Stigge static void lpc32xx_wp_enable(struct lpc32xx_nand_host *host) 3122944a44dSRoland Stigge { 313df63fe76SAlexandre Pereira da Silva if (gpio_is_valid(host->ncfg->wp_gpio)) 3142944a44dSRoland Stigge gpio_set_value(host->ncfg->wp_gpio, 0); 3152944a44dSRoland Stigge } 3162944a44dSRoland Stigge 3172944a44dSRoland Stigge /* 3182944a44dSRoland Stigge * Disable NAND write protect 3192944a44dSRoland Stigge */ 3202944a44dSRoland Stigge static void lpc32xx_wp_disable(struct lpc32xx_nand_host *host) 3212944a44dSRoland Stigge { 322df63fe76SAlexandre Pereira da Silva if (gpio_is_valid(host->ncfg->wp_gpio)) 3232944a44dSRoland Stigge gpio_set_value(host->ncfg->wp_gpio, 1); 3242944a44dSRoland Stigge } 3252944a44dSRoland Stigge 3262944a44dSRoland Stigge /* 3272944a44dSRoland Stigge * Prepares SLC for transfers with H/W ECC enabled 3282944a44dSRoland Stigge */ 329ec47636cSBoris Brezillon static void lpc32xx_nand_ecc_enable(struct nand_chip *chip, int mode) 3302944a44dSRoland Stigge { 3312944a44dSRoland Stigge /* Hardware ECC is enabled automatically in hardware as needed */ 3322944a44dSRoland Stigge } 3332944a44dSRoland Stigge 3342944a44dSRoland Stigge /* 3352944a44dSRoland Stigge * Calculates the ECC for the data 3362944a44dSRoland Stigge */ 337af37d2c3SBoris Brezillon static int lpc32xx_nand_ecc_calculate(struct nand_chip *chip, 3382944a44dSRoland Stigge const unsigned char *buf, 3392944a44dSRoland Stigge unsigned char *code) 3402944a44dSRoland Stigge { 3412944a44dSRoland Stigge /* 3422944a44dSRoland Stigge * ECC is calculated automatically in hardware during syndrome read 3432944a44dSRoland Stigge * and write operations, so it doesn't need to be calculated here. 3442944a44dSRoland Stigge */ 3452944a44dSRoland Stigge return 0; 3462944a44dSRoland Stigge } 3472944a44dSRoland Stigge 3482944a44dSRoland Stigge /* 349*c4b7d7c4SMiquel Raynal * Corrects the data 350*c4b7d7c4SMiquel Raynal */ 351*c4b7d7c4SMiquel Raynal static int lpc32xx_nand_ecc_correct(struct nand_chip *chip, 352*c4b7d7c4SMiquel Raynal unsigned char *buf, 353*c4b7d7c4SMiquel Raynal unsigned char *read_ecc, 354*c4b7d7c4SMiquel Raynal unsigned char *calc_ecc) 355*c4b7d7c4SMiquel Raynal { 356*c4b7d7c4SMiquel Raynal return ecc_sw_hamming_correct(buf, read_ecc, calc_ecc, 357*c4b7d7c4SMiquel Raynal chip->ecc.size, false); 358*c4b7d7c4SMiquel Raynal } 359*c4b7d7c4SMiquel Raynal 360*c4b7d7c4SMiquel Raynal /* 3612944a44dSRoland Stigge * Read a single byte from NAND device 3622944a44dSRoland Stigge */ 3637e534323SBoris Brezillon static uint8_t lpc32xx_nand_read_byte(struct nand_chip *chip) 3642944a44dSRoland Stigge { 365d699ed25SBoris BREZILLON struct lpc32xx_nand_host *host = nand_get_controller_data(chip); 3662944a44dSRoland Stigge 3672944a44dSRoland Stigge return (uint8_t)readl(SLC_DATA(host->io_base)); 3682944a44dSRoland Stigge } 3692944a44dSRoland Stigge 3702944a44dSRoland Stigge /* 3712944a44dSRoland Stigge * Simple device read without ECC 3722944a44dSRoland Stigge */ 3737e534323SBoris Brezillon static void lpc32xx_nand_read_buf(struct nand_chip *chip, u_char *buf, int len) 3742944a44dSRoland Stigge { 375d699ed25SBoris BREZILLON struct lpc32xx_nand_host *host = nand_get_controller_data(chip); 3762944a44dSRoland Stigge 3772944a44dSRoland Stigge /* Direct device read with no ECC */ 3782944a44dSRoland Stigge while (len-- > 0) 3792944a44dSRoland Stigge *buf++ = (uint8_t)readl(SLC_DATA(host->io_base)); 3802944a44dSRoland Stigge } 3812944a44dSRoland Stigge 3822944a44dSRoland Stigge /* 3832944a44dSRoland Stigge * Simple device write without ECC 3842944a44dSRoland Stigge */ 385c0739d85SBoris Brezillon static void lpc32xx_nand_write_buf(struct nand_chip *chip, const uint8_t *buf, 386c0739d85SBoris Brezillon int len) 3872944a44dSRoland Stigge { 388d699ed25SBoris BREZILLON struct lpc32xx_nand_host *host = nand_get_controller_data(chip); 3892944a44dSRoland Stigge 3902944a44dSRoland Stigge /* Direct device write with no ECC */ 3912944a44dSRoland Stigge while (len-- > 0) 3922944a44dSRoland Stigge writel((uint32_t)*buf++, SLC_DATA(host->io_base)); 3932944a44dSRoland Stigge } 3942944a44dSRoland Stigge 3952944a44dSRoland Stigge /* 3962944a44dSRoland Stigge * Read the OOB data from the device without ECC using FIFO method 3972944a44dSRoland Stigge */ 398b9761687SBoris Brezillon static int lpc32xx_nand_read_oob_syndrome(struct nand_chip *chip, int page) 3992944a44dSRoland Stigge { 400b9761687SBoris Brezillon struct mtd_info *mtd = nand_to_mtd(chip); 401b9761687SBoris Brezillon 40297d90da8SBoris Brezillon return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize); 4032944a44dSRoland Stigge } 4042944a44dSRoland Stigge 4052944a44dSRoland Stigge /* 4062944a44dSRoland Stigge * Write the OOB data to the device without ECC using FIFO method 4072944a44dSRoland Stigge */ 408767eb6fbSBoris Brezillon static int lpc32xx_nand_write_oob_syndrome(struct nand_chip *chip, int page) 4092944a44dSRoland Stigge { 410767eb6fbSBoris Brezillon struct mtd_info *mtd = nand_to_mtd(chip); 411767eb6fbSBoris Brezillon 41297d90da8SBoris Brezillon return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi, 41397d90da8SBoris Brezillon mtd->oobsize); 4142944a44dSRoland Stigge } 4152944a44dSRoland Stigge 4162944a44dSRoland Stigge /* 4172944a44dSRoland Stigge * Fills in the ECC fields in the OOB buffer with the hardware generated ECC 4182944a44dSRoland Stigge */ 4192944a44dSRoland Stigge static void lpc32xx_slc_ecc_copy(uint8_t *spare, const uint32_t *ecc, int count) 4202944a44dSRoland Stigge { 4212944a44dSRoland Stigge int i; 4222944a44dSRoland Stigge 4232944a44dSRoland Stigge for (i = 0; i < (count * 3); i += 3) { 4242944a44dSRoland Stigge uint32_t ce = ecc[i / 3]; 4252944a44dSRoland Stigge ce = ~(ce << 2) & 0xFFFFFF; 4262944a44dSRoland Stigge spare[i + 2] = (uint8_t)(ce & 0xFF); 4272944a44dSRoland Stigge ce >>= 8; 4282944a44dSRoland Stigge spare[i + 1] = (uint8_t)(ce & 0xFF); 4292944a44dSRoland Stigge ce >>= 8; 4302944a44dSRoland Stigge spare[i] = (uint8_t)(ce & 0xFF); 4312944a44dSRoland Stigge } 4322944a44dSRoland Stigge } 4332944a44dSRoland Stigge 4342944a44dSRoland Stigge static void lpc32xx_dma_complete_func(void *completion) 4352944a44dSRoland Stigge { 4362944a44dSRoland Stigge complete(completion); 4372944a44dSRoland Stigge } 4382944a44dSRoland Stigge 4392944a44dSRoland Stigge static int lpc32xx_xmit_dma(struct mtd_info *mtd, dma_addr_t dma, 4402944a44dSRoland Stigge void *mem, int len, enum dma_transfer_direction dir) 4412944a44dSRoland Stigge { 4424bd4ebccSBoris BREZILLON struct nand_chip *chip = mtd_to_nand(mtd); 443d699ed25SBoris BREZILLON struct lpc32xx_nand_host *host = nand_get_controller_data(chip); 4442944a44dSRoland Stigge struct dma_async_tx_descriptor *desc; 4452944a44dSRoland Stigge int flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT; 4462944a44dSRoland Stigge int res; 4472944a44dSRoland Stigge 4482944a44dSRoland Stigge host->dma_slave_config.direction = dir; 4492944a44dSRoland Stigge host->dma_slave_config.src_addr = dma; 4502944a44dSRoland Stigge host->dma_slave_config.dst_addr = dma; 4512944a44dSRoland Stigge host->dma_slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 4522944a44dSRoland Stigge host->dma_slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 4532944a44dSRoland Stigge host->dma_slave_config.src_maxburst = 4; 4542944a44dSRoland Stigge host->dma_slave_config.dst_maxburst = 4; 4552944a44dSRoland Stigge /* DMA controller does flow control: */ 4562944a44dSRoland Stigge host->dma_slave_config.device_fc = false; 4572944a44dSRoland Stigge if (dmaengine_slave_config(host->dma_chan, &host->dma_slave_config)) { 4582944a44dSRoland Stigge dev_err(mtd->dev.parent, "Failed to setup DMA slave\n"); 4592944a44dSRoland Stigge return -ENXIO; 4602944a44dSRoland Stigge } 4612944a44dSRoland Stigge 4622944a44dSRoland Stigge sg_init_one(&host->sgl, mem, len); 4632944a44dSRoland Stigge 4642944a44dSRoland Stigge res = dma_map_sg(host->dma_chan->device->dev, &host->sgl, 1, 4652944a44dSRoland Stigge DMA_BIDIRECTIONAL); 4662944a44dSRoland Stigge if (res != 1) { 4672944a44dSRoland Stigge dev_err(mtd->dev.parent, "Failed to map sg list\n"); 4682944a44dSRoland Stigge return -ENXIO; 4692944a44dSRoland Stigge } 4702944a44dSRoland Stigge desc = dmaengine_prep_slave_sg(host->dma_chan, &host->sgl, 1, dir, 4712944a44dSRoland Stigge flags); 4722944a44dSRoland Stigge if (!desc) { 4732944a44dSRoland Stigge dev_err(mtd->dev.parent, "Failed to prepare slave sg\n"); 4742944a44dSRoland Stigge goto out1; 4752944a44dSRoland Stigge } 4762944a44dSRoland Stigge 4772944a44dSRoland Stigge init_completion(&host->comp); 4782944a44dSRoland Stigge desc->callback = lpc32xx_dma_complete_func; 4792944a44dSRoland Stigge desc->callback_param = &host->comp; 4802944a44dSRoland Stigge 4812944a44dSRoland Stigge dmaengine_submit(desc); 4822944a44dSRoland Stigge dma_async_issue_pending(host->dma_chan); 4832944a44dSRoland Stigge 4842944a44dSRoland Stigge wait_for_completion_timeout(&host->comp, msecs_to_jiffies(1000)); 4852944a44dSRoland Stigge 4862944a44dSRoland Stigge dma_unmap_sg(host->dma_chan->device->dev, &host->sgl, 1, 4872944a44dSRoland Stigge DMA_BIDIRECTIONAL); 4882944a44dSRoland Stigge 4892944a44dSRoland Stigge return 0; 4902944a44dSRoland Stigge out1: 4912944a44dSRoland Stigge dma_unmap_sg(host->dma_chan->device->dev, &host->sgl, 1, 4922944a44dSRoland Stigge DMA_BIDIRECTIONAL); 4932944a44dSRoland Stigge return -ENXIO; 4942944a44dSRoland Stigge } 4952944a44dSRoland Stigge 4962944a44dSRoland Stigge /* 4972944a44dSRoland Stigge * DMA read/write transfers with ECC support 4982944a44dSRoland Stigge */ 4992944a44dSRoland Stigge static int lpc32xx_xfer(struct mtd_info *mtd, uint8_t *buf, int eccsubpages, 5002944a44dSRoland Stigge int read) 5012944a44dSRoland Stigge { 5024bd4ebccSBoris BREZILLON struct nand_chip *chip = mtd_to_nand(mtd); 503d699ed25SBoris BREZILLON struct lpc32xx_nand_host *host = nand_get_controller_data(chip); 5042944a44dSRoland Stigge int i, status = 0; 5052944a44dSRoland Stigge unsigned long timeout; 5062944a44dSRoland Stigge int res; 5072944a44dSRoland Stigge enum dma_transfer_direction dir = 5082944a44dSRoland Stigge read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV; 5092944a44dSRoland Stigge uint8_t *dma_buf; 5102944a44dSRoland Stigge bool dma_mapped; 5112944a44dSRoland Stigge 5122944a44dSRoland Stigge if ((void *)buf <= high_memory) { 5132944a44dSRoland Stigge dma_buf = buf; 5142944a44dSRoland Stigge dma_mapped = true; 5152944a44dSRoland Stigge } else { 5162944a44dSRoland Stigge dma_buf = host->data_buf; 5172944a44dSRoland Stigge dma_mapped = false; 5182944a44dSRoland Stigge if (!read) 5192944a44dSRoland Stigge memcpy(host->data_buf, buf, mtd->writesize); 5202944a44dSRoland Stigge } 5212944a44dSRoland Stigge 5222944a44dSRoland Stigge if (read) { 5232944a44dSRoland Stigge writel(readl(SLC_CFG(host->io_base)) | 5242944a44dSRoland Stigge SLCCFG_DMA_DIR | SLCCFG_ECC_EN | SLCCFG_DMA_ECC | 5252944a44dSRoland Stigge SLCCFG_DMA_BURST, SLC_CFG(host->io_base)); 5262944a44dSRoland Stigge } else { 5272944a44dSRoland Stigge writel((readl(SLC_CFG(host->io_base)) | 5282944a44dSRoland Stigge SLCCFG_ECC_EN | SLCCFG_DMA_ECC | SLCCFG_DMA_BURST) & 5292944a44dSRoland Stigge ~SLCCFG_DMA_DIR, 5302944a44dSRoland Stigge SLC_CFG(host->io_base)); 5312944a44dSRoland Stigge } 5322944a44dSRoland Stigge 5332944a44dSRoland Stigge /* Clear initial ECC */ 5342944a44dSRoland Stigge writel(SLCCTRL_ECC_CLEAR, SLC_CTRL(host->io_base)); 5352944a44dSRoland Stigge 5362944a44dSRoland Stigge /* Transfer size is data area only */ 5372944a44dSRoland Stigge writel(mtd->writesize, SLC_TC(host->io_base)); 5382944a44dSRoland Stigge 5392944a44dSRoland Stigge /* Start transfer in the NAND controller */ 5402944a44dSRoland Stigge writel(readl(SLC_CTRL(host->io_base)) | SLCCTRL_DMA_START, 5412944a44dSRoland Stigge SLC_CTRL(host->io_base)); 5422944a44dSRoland Stigge 5432944a44dSRoland Stigge for (i = 0; i < chip->ecc.steps; i++) { 5442944a44dSRoland Stigge /* Data */ 5452944a44dSRoland Stigge res = lpc32xx_xmit_dma(mtd, SLC_DMA_DATA(host->io_base_dma), 5462944a44dSRoland Stigge dma_buf + i * chip->ecc.size, 5472944a44dSRoland Stigge mtd->writesize / chip->ecc.steps, dir); 5482944a44dSRoland Stigge if (res) 5492944a44dSRoland Stigge return res; 5502944a44dSRoland Stigge 5512944a44dSRoland Stigge /* Always _read_ ECC */ 5522944a44dSRoland Stigge if (i == chip->ecc.steps - 1) 5532944a44dSRoland Stigge break; 5542944a44dSRoland Stigge if (!read) /* ECC availability delayed on write */ 5552944a44dSRoland Stigge udelay(10); 5562944a44dSRoland Stigge res = lpc32xx_xmit_dma(mtd, SLC_ECC(host->io_base_dma), 5572944a44dSRoland Stigge &host->ecc_buf[i], 4, DMA_DEV_TO_MEM); 5582944a44dSRoland Stigge if (res) 5592944a44dSRoland Stigge return res; 5602944a44dSRoland Stigge } 5612944a44dSRoland Stigge 5622944a44dSRoland Stigge /* 5632944a44dSRoland Stigge * According to NXP, the DMA can be finished here, but the NAND 5642944a44dSRoland Stigge * controller may still have buffered data. After porting to using the 5652944a44dSRoland Stigge * dmaengine DMA driver (amba-pl080), the condition (DMA_FIFO empty) 5662944a44dSRoland Stigge * appears to be always true, according to tests. Keeping the check for 5672944a44dSRoland Stigge * safety reasons for now. 5682944a44dSRoland Stigge */ 5692944a44dSRoland Stigge if (readl(SLC_STAT(host->io_base)) & SLCSTAT_DMA_FIFO) { 5702944a44dSRoland Stigge dev_warn(mtd->dev.parent, "FIFO not empty!\n"); 5712944a44dSRoland Stigge timeout = jiffies + msecs_to_jiffies(LPC32XX_DMA_TIMEOUT); 5722944a44dSRoland Stigge while ((readl(SLC_STAT(host->io_base)) & SLCSTAT_DMA_FIFO) && 5732944a44dSRoland Stigge time_before(jiffies, timeout)) 5742944a44dSRoland Stigge cpu_relax(); 5752944a44dSRoland Stigge if (!time_before(jiffies, timeout)) { 5762944a44dSRoland Stigge dev_err(mtd->dev.parent, "FIFO held data too long\n"); 5772944a44dSRoland Stigge status = -EIO; 5782944a44dSRoland Stigge } 5792944a44dSRoland Stigge } 5802944a44dSRoland Stigge 5812944a44dSRoland Stigge /* Read last calculated ECC value */ 5822944a44dSRoland Stigge if (!read) 5832944a44dSRoland Stigge udelay(10); 5842944a44dSRoland Stigge host->ecc_buf[chip->ecc.steps - 1] = 5852944a44dSRoland Stigge readl(SLC_ECC(host->io_base)); 5862944a44dSRoland Stigge 5872944a44dSRoland Stigge /* Flush DMA */ 5882944a44dSRoland Stigge dmaengine_terminate_all(host->dma_chan); 5892944a44dSRoland Stigge 5902944a44dSRoland Stigge if (readl(SLC_STAT(host->io_base)) & SLCSTAT_DMA_FIFO || 5912944a44dSRoland Stigge readl(SLC_TC(host->io_base))) { 5922944a44dSRoland Stigge /* Something is left in the FIFO, something is wrong */ 5932944a44dSRoland Stigge dev_err(mtd->dev.parent, "DMA FIFO failure\n"); 5942944a44dSRoland Stigge status = -EIO; 5952944a44dSRoland Stigge } 5962944a44dSRoland Stigge 5972944a44dSRoland Stigge /* Stop DMA & HW ECC */ 5982944a44dSRoland Stigge writel(readl(SLC_CTRL(host->io_base)) & ~SLCCTRL_DMA_START, 5992944a44dSRoland Stigge SLC_CTRL(host->io_base)); 6002944a44dSRoland Stigge writel(readl(SLC_CFG(host->io_base)) & 6012944a44dSRoland Stigge ~(SLCCFG_DMA_DIR | SLCCFG_ECC_EN | SLCCFG_DMA_ECC | 6022944a44dSRoland Stigge SLCCFG_DMA_BURST), SLC_CFG(host->io_base)); 6032944a44dSRoland Stigge 6042944a44dSRoland Stigge if (!dma_mapped && read) 6052944a44dSRoland Stigge memcpy(buf, host->data_buf, mtd->writesize); 6062944a44dSRoland Stigge 6072944a44dSRoland Stigge return status; 6082944a44dSRoland Stigge } 6092944a44dSRoland Stigge 6102944a44dSRoland Stigge /* 6112944a44dSRoland Stigge * Read the data and OOB data from the device, use ECC correction with the 6122944a44dSRoland Stigge * data, disable ECC for the OOB data 6132944a44dSRoland Stigge */ 614b9761687SBoris Brezillon static int lpc32xx_nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf, 6152944a44dSRoland Stigge int oob_required, int page) 6162944a44dSRoland Stigge { 617b9761687SBoris Brezillon struct mtd_info *mtd = nand_to_mtd(chip); 618d699ed25SBoris BREZILLON struct lpc32xx_nand_host *host = nand_get_controller_data(chip); 619b9c0f65fSBoris Brezillon struct mtd_oob_region oobregion = { }; 620b9c0f65fSBoris Brezillon int stat, i, status, error; 6212944a44dSRoland Stigge uint8_t *oobecc, tmpecc[LPC32XX_ECC_SAVE_SIZE]; 6222944a44dSRoland Stigge 6232944a44dSRoland Stigge /* Issue read command */ 62497d90da8SBoris Brezillon nand_read_page_op(chip, page, 0, NULL, 0); 6252944a44dSRoland Stigge 6262944a44dSRoland Stigge /* Read data and oob, calculate ECC */ 6272944a44dSRoland Stigge status = lpc32xx_xfer(mtd, buf, chip->ecc.steps, 1); 6282944a44dSRoland Stigge 6292944a44dSRoland Stigge /* Get OOB data */ 630716bbbabSBoris Brezillon chip->legacy.read_buf(chip, chip->oob_poi, mtd->oobsize); 6312944a44dSRoland Stigge 6322944a44dSRoland Stigge /* Convert to stored ECC format */ 6332944a44dSRoland Stigge lpc32xx_slc_ecc_copy(tmpecc, (uint32_t *) host->ecc_buf, chip->ecc.steps); 6342944a44dSRoland Stigge 6352944a44dSRoland Stigge /* Pointer to ECC data retrieved from NAND spare area */ 636b9c0f65fSBoris Brezillon error = mtd_ooblayout_ecc(mtd, 0, &oobregion); 637b9c0f65fSBoris Brezillon if (error) 638b9c0f65fSBoris Brezillon return error; 639b9c0f65fSBoris Brezillon 640b9c0f65fSBoris Brezillon oobecc = chip->oob_poi + oobregion.offset; 6412944a44dSRoland Stigge 6422944a44dSRoland Stigge for (i = 0; i < chip->ecc.steps; i++) { 64300da2ea9SBoris Brezillon stat = chip->ecc.correct(chip, buf, oobecc, 6442944a44dSRoland Stigge &tmpecc[i * chip->ecc.bytes]); 6452944a44dSRoland Stigge if (stat < 0) 6462944a44dSRoland Stigge mtd->ecc_stats.failed++; 6472944a44dSRoland Stigge else 6482944a44dSRoland Stigge mtd->ecc_stats.corrected += stat; 6492944a44dSRoland Stigge 6502944a44dSRoland Stigge buf += chip->ecc.size; 6512944a44dSRoland Stigge oobecc += chip->ecc.bytes; 6522944a44dSRoland Stigge } 6532944a44dSRoland Stigge 6542944a44dSRoland Stigge return status; 6552944a44dSRoland Stigge } 6562944a44dSRoland Stigge 6572944a44dSRoland Stigge /* 6582944a44dSRoland Stigge * Read the data and OOB data from the device, no ECC correction with the 6592944a44dSRoland Stigge * data or OOB data 6602944a44dSRoland Stigge */ 661b9761687SBoris Brezillon static int lpc32xx_nand_read_page_raw_syndrome(struct nand_chip *chip, 6622944a44dSRoland Stigge uint8_t *buf, int oob_required, 6632944a44dSRoland Stigge int page) 6642944a44dSRoland Stigge { 665b9761687SBoris Brezillon struct mtd_info *mtd = nand_to_mtd(chip); 666b9761687SBoris Brezillon 6672944a44dSRoland Stigge /* Issue read command */ 66897d90da8SBoris Brezillon nand_read_page_op(chip, page, 0, NULL, 0); 6692944a44dSRoland Stigge 6702944a44dSRoland Stigge /* Raw reads can just use the FIFO interface */ 671716bbbabSBoris Brezillon chip->legacy.read_buf(chip, buf, chip->ecc.size * chip->ecc.steps); 672716bbbabSBoris Brezillon chip->legacy.read_buf(chip, chip->oob_poi, mtd->oobsize); 6732944a44dSRoland Stigge 6742944a44dSRoland Stigge return 0; 6752944a44dSRoland Stigge } 6762944a44dSRoland Stigge 6772944a44dSRoland Stigge /* 6782944a44dSRoland Stigge * Write the data and OOB data to the device, use ECC with the data, 6792944a44dSRoland Stigge * disable ECC for the OOB data 6802944a44dSRoland Stigge */ 681767eb6fbSBoris Brezillon static int lpc32xx_nand_write_page_syndrome(struct nand_chip *chip, 68245aaeff9SBoris BREZILLON const uint8_t *buf, 68345aaeff9SBoris BREZILLON int oob_required, int page) 6842944a44dSRoland Stigge { 685767eb6fbSBoris Brezillon struct mtd_info *mtd = nand_to_mtd(chip); 686d699ed25SBoris BREZILLON struct lpc32xx_nand_host *host = nand_get_controller_data(chip); 687b9c0f65fSBoris Brezillon struct mtd_oob_region oobregion = { }; 688b9c0f65fSBoris Brezillon uint8_t *pb; 6892944a44dSRoland Stigge int error; 6902944a44dSRoland Stigge 69125f815f6SBoris Brezillon nand_prog_page_begin_op(chip, page, 0, NULL, 0); 69225f815f6SBoris Brezillon 6932944a44dSRoland Stigge /* Write data, calculate ECC on outbound data */ 6942944a44dSRoland Stigge error = lpc32xx_xfer(mtd, (uint8_t *)buf, chip->ecc.steps, 0); 6952944a44dSRoland Stigge if (error) 6962944a44dSRoland Stigge return error; 6972944a44dSRoland Stigge 6982944a44dSRoland Stigge /* 6992944a44dSRoland Stigge * The calculated ECC needs some manual work done to it before 7002944a44dSRoland Stigge * committing it to NAND. Process the calculated ECC and place 7012944a44dSRoland Stigge * the resultant values directly into the OOB buffer. */ 702b9c0f65fSBoris Brezillon error = mtd_ooblayout_ecc(mtd, 0, &oobregion); 703b9c0f65fSBoris Brezillon if (error) 704b9c0f65fSBoris Brezillon return error; 705b9c0f65fSBoris Brezillon 706b9c0f65fSBoris Brezillon pb = chip->oob_poi + oobregion.offset; 7072944a44dSRoland Stigge lpc32xx_slc_ecc_copy(pb, (uint32_t *)host->ecc_buf, chip->ecc.steps); 7082944a44dSRoland Stigge 7092944a44dSRoland Stigge /* Write ECC data to device */ 710716bbbabSBoris Brezillon chip->legacy.write_buf(chip, chip->oob_poi, mtd->oobsize); 71125f815f6SBoris Brezillon 71225f815f6SBoris Brezillon return nand_prog_page_end_op(chip); 7132944a44dSRoland Stigge } 7142944a44dSRoland Stigge 7152944a44dSRoland Stigge /* 7162944a44dSRoland Stigge * Write the data and OOB data to the device, no ECC correction with the 7172944a44dSRoland Stigge * data or OOB data 7182944a44dSRoland Stigge */ 719767eb6fbSBoris Brezillon static int lpc32xx_nand_write_page_raw_syndrome(struct nand_chip *chip, 7202944a44dSRoland Stigge const uint8_t *buf, 72145aaeff9SBoris BREZILLON int oob_required, int page) 7222944a44dSRoland Stigge { 723767eb6fbSBoris Brezillon struct mtd_info *mtd = nand_to_mtd(chip); 724767eb6fbSBoris Brezillon 7252944a44dSRoland Stigge /* Raw writes can just use the FIFO interface */ 72625f815f6SBoris Brezillon nand_prog_page_begin_op(chip, page, 0, buf, 72725f815f6SBoris Brezillon chip->ecc.size * chip->ecc.steps); 728716bbbabSBoris Brezillon chip->legacy.write_buf(chip, chip->oob_poi, mtd->oobsize); 72925f815f6SBoris Brezillon 73025f815f6SBoris Brezillon return nand_prog_page_end_op(chip); 7312944a44dSRoland Stigge } 7322944a44dSRoland Stigge 7332944a44dSRoland Stigge static int lpc32xx_nand_dma_setup(struct lpc32xx_nand_host *host) 7342944a44dSRoland Stigge { 7350faf8c39SBoris BREZILLON struct mtd_info *mtd = nand_to_mtd(&host->nand_chip); 7362944a44dSRoland Stigge dma_cap_mask_t mask; 7372944a44dSRoland Stigge 738de20c22dSRoland Stigge if (!host->pdata || !host->pdata->dma_filter) { 739de20c22dSRoland Stigge dev_err(mtd->dev.parent, "no DMA platform data\n"); 740de20c22dSRoland Stigge return -ENOENT; 741de20c22dSRoland Stigge } 742de20c22dSRoland Stigge 7432944a44dSRoland Stigge dma_cap_zero(mask); 7442944a44dSRoland Stigge dma_cap_set(DMA_SLAVE, mask); 745de20c22dSRoland Stigge host->dma_chan = dma_request_channel(mask, host->pdata->dma_filter, 746de20c22dSRoland Stigge "nand-slc"); 7472944a44dSRoland Stigge if (!host->dma_chan) { 7482944a44dSRoland Stigge dev_err(mtd->dev.parent, "Failed to request DMA channel\n"); 7492944a44dSRoland Stigge return -EBUSY; 7502944a44dSRoland Stigge } 7512944a44dSRoland Stigge 7522944a44dSRoland Stigge return 0; 7532944a44dSRoland Stigge } 7542944a44dSRoland Stigge 7552944a44dSRoland Stigge static struct lpc32xx_nand_cfg_slc *lpc32xx_parse_dt(struct device *dev) 7562944a44dSRoland Stigge { 75710594f67SRoland Stigge struct lpc32xx_nand_cfg_slc *ncfg; 7582944a44dSRoland Stigge struct device_node *np = dev->of_node; 7592944a44dSRoland Stigge 76010594f67SRoland Stigge ncfg = devm_kzalloc(dev, sizeof(*ncfg), GFP_KERNEL); 7618ecb66baSJingoo Han if (!ncfg) 7622944a44dSRoland Stigge return NULL; 7632944a44dSRoland Stigge 76410594f67SRoland Stigge of_property_read_u32(np, "nxp,wdr-clks", &ncfg->wdr_clks); 76510594f67SRoland Stigge of_property_read_u32(np, "nxp,wwidth", &ncfg->wwidth); 76610594f67SRoland Stigge of_property_read_u32(np, "nxp,whold", &ncfg->whold); 76710594f67SRoland Stigge of_property_read_u32(np, "nxp,wsetup", &ncfg->wsetup); 76810594f67SRoland Stigge of_property_read_u32(np, "nxp,rdr-clks", &ncfg->rdr_clks); 76910594f67SRoland Stigge of_property_read_u32(np, "nxp,rwidth", &ncfg->rwidth); 77010594f67SRoland Stigge of_property_read_u32(np, "nxp,rhold", &ncfg->rhold); 77110594f67SRoland Stigge of_property_read_u32(np, "nxp,rsetup", &ncfg->rsetup); 7722944a44dSRoland Stigge 77310594f67SRoland Stigge if (!ncfg->wdr_clks || !ncfg->wwidth || !ncfg->whold || 77410594f67SRoland Stigge !ncfg->wsetup || !ncfg->rdr_clks || !ncfg->rwidth || 77510594f67SRoland Stigge !ncfg->rhold || !ncfg->rsetup) { 7762944a44dSRoland Stigge dev_err(dev, "chip parameters not specified correctly\n"); 7772944a44dSRoland Stigge return NULL; 7782944a44dSRoland Stigge } 7792944a44dSRoland Stigge 78010594f67SRoland Stigge ncfg->wp_gpio = of_get_named_gpio(np, "gpios", 0); 7812944a44dSRoland Stigge 78210594f67SRoland Stigge return ncfg; 7832944a44dSRoland Stigge } 7842944a44dSRoland Stigge 785f4a48d7bSMiquel Raynal static int lpc32xx_nand_attach_chip(struct nand_chip *chip) 786f4a48d7bSMiquel Raynal { 787f4a48d7bSMiquel Raynal struct mtd_info *mtd = nand_to_mtd(chip); 788f4a48d7bSMiquel Raynal struct lpc32xx_nand_host *host = nand_get_controller_data(chip); 789f4a48d7bSMiquel Raynal 790e044b8b7SMiquel Raynal if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST) 791e044b8b7SMiquel Raynal return 0; 792e044b8b7SMiquel Raynal 793f4a48d7bSMiquel Raynal /* OOB and ECC CPU and DMA work areas */ 794f4a48d7bSMiquel Raynal host->ecc_buf = (uint32_t *)(host->data_buf + LPC32XX_DMA_DATA_SIZE); 795f4a48d7bSMiquel Raynal 796f4a48d7bSMiquel Raynal /* 797f4a48d7bSMiquel Raynal * Small page FLASH has a unique OOB layout, but large and huge 798f4a48d7bSMiquel Raynal * page FLASH use the standard layout. Small page FLASH uses a 799f4a48d7bSMiquel Raynal * custom BBT marker layout. 800f4a48d7bSMiquel Raynal */ 801f4a48d7bSMiquel Raynal if (mtd->writesize <= 512) 802f4a48d7bSMiquel Raynal mtd_set_ooblayout(mtd, &lpc32xx_ooblayout_ops); 803f4a48d7bSMiquel Raynal 804e044b8b7SMiquel Raynal chip->ecc.placement = NAND_ECC_PLACEMENT_INTERLEAVED; 805f4a48d7bSMiquel Raynal /* These sizes remain the same regardless of page size */ 806f4a48d7bSMiquel Raynal chip->ecc.size = 256; 807e044b8b7SMiquel Raynal chip->ecc.strength = 1; 808f4a48d7bSMiquel Raynal chip->ecc.bytes = LPC32XX_SLC_DEV_ECC_BYTES; 809f4a48d7bSMiquel Raynal chip->ecc.prepad = 0; 810f4a48d7bSMiquel Raynal chip->ecc.postpad = 0; 811e044b8b7SMiquel Raynal chip->ecc.read_page_raw = lpc32xx_nand_read_page_raw_syndrome; 812e044b8b7SMiquel Raynal chip->ecc.read_page = lpc32xx_nand_read_page_syndrome; 813e044b8b7SMiquel Raynal chip->ecc.write_page_raw = lpc32xx_nand_write_page_raw_syndrome; 814e044b8b7SMiquel Raynal chip->ecc.write_page = lpc32xx_nand_write_page_syndrome; 815e044b8b7SMiquel Raynal chip->ecc.write_oob = lpc32xx_nand_write_oob_syndrome; 816e044b8b7SMiquel Raynal chip->ecc.read_oob = lpc32xx_nand_read_oob_syndrome; 817e044b8b7SMiquel Raynal chip->ecc.calculate = lpc32xx_nand_ecc_calculate; 818*c4b7d7c4SMiquel Raynal chip->ecc.correct = lpc32xx_nand_ecc_correct; 819e044b8b7SMiquel Raynal chip->ecc.hwctl = lpc32xx_nand_ecc_enable; 820f4a48d7bSMiquel Raynal 821f4a48d7bSMiquel Raynal /* 822f4a48d7bSMiquel Raynal * Use a custom BBT marker setup for small page FLASH that 823f4a48d7bSMiquel Raynal * won't interfere with the ECC layout. Large and huge page 824f4a48d7bSMiquel Raynal * FLASH use the standard layout. 825f4a48d7bSMiquel Raynal */ 826f4a48d7bSMiquel Raynal if ((chip->bbt_options & NAND_BBT_USE_FLASH) && 827f4a48d7bSMiquel Raynal mtd->writesize <= 512) { 828f4a48d7bSMiquel Raynal chip->bbt_td = &bbt_smallpage_main_descr; 829f4a48d7bSMiquel Raynal chip->bbt_md = &bbt_smallpage_mirror_descr; 830f4a48d7bSMiquel Raynal } 831f4a48d7bSMiquel Raynal 832f4a48d7bSMiquel Raynal return 0; 833f4a48d7bSMiquel Raynal } 834f4a48d7bSMiquel Raynal 835f4a48d7bSMiquel Raynal static const struct nand_controller_ops lpc32xx_nand_controller_ops = { 836f4a48d7bSMiquel Raynal .attach_chip = lpc32xx_nand_attach_chip, 837f4a48d7bSMiquel Raynal }; 838f4a48d7bSMiquel Raynal 8392944a44dSRoland Stigge /* 8402944a44dSRoland Stigge * Probe for NAND controller 8412944a44dSRoland Stigge */ 84206f25510SBill Pemberton static int lpc32xx_nand_probe(struct platform_device *pdev) 8432944a44dSRoland Stigge { 8442944a44dSRoland Stigge struct lpc32xx_nand_host *host; 8452944a44dSRoland Stigge struct mtd_info *mtd; 8462944a44dSRoland Stigge struct nand_chip *chip; 8472944a44dSRoland Stigge struct resource *rc; 8482944a44dSRoland Stigge int res; 8492944a44dSRoland Stigge 8502944a44dSRoland Stigge /* Allocate memory for the device structure (and zero it) */ 8512944a44dSRoland Stigge host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL); 8528ecb66baSJingoo Han if (!host) 8532944a44dSRoland Stigge return -ENOMEM; 8542944a44dSRoland Stigge 8554339b7fdSFabio Estevam rc = platform_get_resource(pdev, IORESOURCE_MEM, 0); 856b0de774cSThierry Reding host->io_base = devm_ioremap_resource(&pdev->dev, rc); 857b0de774cSThierry Reding if (IS_ERR(host->io_base)) 858b0de774cSThierry Reding return PTR_ERR(host->io_base); 8592944a44dSRoland Stigge 8604339b7fdSFabio Estevam host->io_base_dma = rc->start; 8612944a44dSRoland Stigge if (pdev->dev.of_node) 8622944a44dSRoland Stigge host->ncfg = lpc32xx_parse_dt(&pdev->dev); 8632944a44dSRoland Stigge if (!host->ncfg) { 86410594f67SRoland Stigge dev_err(&pdev->dev, 86510594f67SRoland Stigge "Missing or bad NAND config from device tree\n"); 8662944a44dSRoland Stigge return -ENOENT; 8672944a44dSRoland Stigge } 868d5842ab7SRoland Stigge if (host->ncfg->wp_gpio == -EPROBE_DEFER) 869d5842ab7SRoland Stigge return -EPROBE_DEFER; 870133432a7SJingoo Han if (gpio_is_valid(host->ncfg->wp_gpio) && devm_gpio_request(&pdev->dev, 871133432a7SJingoo Han host->ncfg->wp_gpio, "NAND WP")) { 8722944a44dSRoland Stigge dev_err(&pdev->dev, "GPIO not available\n"); 8732944a44dSRoland Stigge return -EBUSY; 8742944a44dSRoland Stigge } 8752944a44dSRoland Stigge lpc32xx_wp_disable(host); 8762944a44dSRoland Stigge 877453810b7SJingoo Han host->pdata = dev_get_platdata(&pdev->dev); 878de20c22dSRoland Stigge 8792944a44dSRoland Stigge chip = &host->nand_chip; 8800faf8c39SBoris BREZILLON mtd = nand_to_mtd(chip); 881d699ed25SBoris BREZILLON nand_set_controller_data(chip, host); 882a61ae81aSBrian Norris nand_set_flash_node(chip, pdev->dev.of_node); 8832944a44dSRoland Stigge mtd->owner = THIS_MODULE; 8842944a44dSRoland Stigge mtd->dev.parent = &pdev->dev; 8852944a44dSRoland Stigge 8862944a44dSRoland Stigge /* Get NAND clock */ 887133432a7SJingoo Han host->clk = devm_clk_get(&pdev->dev, NULL); 8882944a44dSRoland Stigge if (IS_ERR(host->clk)) { 8892944a44dSRoland Stigge dev_err(&pdev->dev, "Clock failure\n"); 8902944a44dSRoland Stigge res = -ENOENT; 891e0ea20bfSMiquel Raynal goto enable_wp; 8922944a44dSRoland Stigge } 8937c941281SArvind Yadav res = clk_prepare_enable(host->clk); 8947c941281SArvind Yadav if (res) 895e0ea20bfSMiquel Raynal goto enable_wp; 8962944a44dSRoland Stigge 8972944a44dSRoland Stigge /* Set NAND IO addresses and command/ready functions */ 89882fc5099SBoris Brezillon chip->legacy.IO_ADDR_R = SLC_DATA(host->io_base); 89982fc5099SBoris Brezillon chip->legacy.IO_ADDR_W = SLC_DATA(host->io_base); 900bf6065c6SBoris Brezillon chip->legacy.cmd_ctrl = lpc32xx_nand_cmd_ctrl; 9018395b753SBoris Brezillon chip->legacy.dev_ready = lpc32xx_nand_device_ready; 9023cece3abSBoris Brezillon chip->legacy.chip_delay = 20; /* 20us command delay time */ 9032944a44dSRoland Stigge 9042944a44dSRoland Stigge /* Init NAND controller */ 9052944a44dSRoland Stigge lpc32xx_nand_setup(host); 9062944a44dSRoland Stigge 9072944a44dSRoland Stigge platform_set_drvdata(pdev, host); 9082944a44dSRoland Stigge 9092944a44dSRoland Stigge /* NAND callbacks for LPC32xx SLC hardware */ 910716bbbabSBoris Brezillon chip->legacy.read_byte = lpc32xx_nand_read_byte; 911716bbbabSBoris Brezillon chip->legacy.read_buf = lpc32xx_nand_read_buf; 912716bbbabSBoris Brezillon chip->legacy.write_buf = lpc32xx_nand_write_buf; 9132944a44dSRoland Stigge 9142944a44dSRoland Stigge /* 9152944a44dSRoland Stigge * Allocate a large enough buffer for a single huge page plus 9162944a44dSRoland Stigge * extra space for the spare area and ECC storage area 9172944a44dSRoland Stigge */ 9182944a44dSRoland Stigge host->dma_buf_len = LPC32XX_DMA_DATA_SIZE + LPC32XX_ECC_SAVE_SIZE; 9192944a44dSRoland Stigge host->data_buf = devm_kzalloc(&pdev->dev, host->dma_buf_len, 9202944a44dSRoland Stigge GFP_KERNEL); 9212944a44dSRoland Stigge if (host->data_buf == NULL) { 9222944a44dSRoland Stigge res = -ENOMEM; 923e0ea20bfSMiquel Raynal goto unprepare_clk; 9242944a44dSRoland Stigge } 9252944a44dSRoland Stigge 9262944a44dSRoland Stigge res = lpc32xx_nand_dma_setup(host); 9272944a44dSRoland Stigge if (res) { 9282944a44dSRoland Stigge res = -EIO; 929e0ea20bfSMiquel Raynal goto unprepare_clk; 9302944a44dSRoland Stigge } 9312944a44dSRoland Stigge 9322944a44dSRoland Stigge /* Find NAND device */ 9337b6a9b28SBoris Brezillon chip->legacy.dummy_controller.ops = &lpc32xx_nand_controller_ops; 93400ad378fSBoris Brezillon res = nand_scan(chip, 1); 935b04bafcaSMasahiro Yamada if (res) 936e0ea20bfSMiquel Raynal goto release_dma; 9372944a44dSRoland Stigge 9382944a44dSRoland Stigge mtd->name = "nxp_lpc3220_slc"; 939a61ae81aSBrian Norris res = mtd_device_register(mtd, host->ncfg->parts, 9402944a44dSRoland Stigge host->ncfg->num_parts); 941e0ea20bfSMiquel Raynal if (res) 942553b0c64SMiquel Raynal goto cleanup_nand; 9432944a44dSRoland Stigge 944e0ea20bfSMiquel Raynal return 0; 945e0ea20bfSMiquel Raynal 946553b0c64SMiquel Raynal cleanup_nand: 947553b0c64SMiquel Raynal nand_cleanup(chip); 948e0ea20bfSMiquel Raynal release_dma: 9492944a44dSRoland Stigge dma_release_channel(host->dma_chan); 950e0ea20bfSMiquel Raynal unprepare_clk: 95144cab9c9SVladimir Zapolskiy clk_disable_unprepare(host->clk); 952e0ea20bfSMiquel Raynal enable_wp: 9532944a44dSRoland Stigge lpc32xx_wp_enable(host); 9542944a44dSRoland Stigge 9552944a44dSRoland Stigge return res; 9562944a44dSRoland Stigge } 9572944a44dSRoland Stigge 9582944a44dSRoland Stigge /* 9592944a44dSRoland Stigge * Remove NAND device. 9602944a44dSRoland Stigge */ 961810b7e06SBill Pemberton static int lpc32xx_nand_remove(struct platform_device *pdev) 9622944a44dSRoland Stigge { 9632944a44dSRoland Stigge uint32_t tmp; 9642944a44dSRoland Stigge struct lpc32xx_nand_host *host = platform_get_drvdata(pdev); 96521b75827SMiquel Raynal struct nand_chip *chip = &host->nand_chip; 96621b75827SMiquel Raynal int ret; 9672944a44dSRoland Stigge 96821b75827SMiquel Raynal ret = mtd_device_unregister(nand_to_mtd(chip)); 96921b75827SMiquel Raynal WARN_ON(ret); 97021b75827SMiquel Raynal nand_cleanup(chip); 9712944a44dSRoland Stigge dma_release_channel(host->dma_chan); 9722944a44dSRoland Stigge 9732944a44dSRoland Stigge /* Force CE high */ 9742944a44dSRoland Stigge tmp = readl(SLC_CTRL(host->io_base)); 9752944a44dSRoland Stigge tmp &= ~SLCCFG_CE_LOW; 9762944a44dSRoland Stigge writel(tmp, SLC_CTRL(host->io_base)); 9772944a44dSRoland Stigge 97844cab9c9SVladimir Zapolskiy clk_disable_unprepare(host->clk); 9792944a44dSRoland Stigge lpc32xx_wp_enable(host); 9802944a44dSRoland Stigge 9812944a44dSRoland Stigge return 0; 9822944a44dSRoland Stigge } 9832944a44dSRoland Stigge 9842944a44dSRoland Stigge #ifdef CONFIG_PM 9852944a44dSRoland Stigge static int lpc32xx_nand_resume(struct platform_device *pdev) 9862944a44dSRoland Stigge { 9872944a44dSRoland Stigge struct lpc32xx_nand_host *host = platform_get_drvdata(pdev); 9887c941281SArvind Yadav int ret; 9892944a44dSRoland Stigge 9902944a44dSRoland Stigge /* Re-enable NAND clock */ 9917c941281SArvind Yadav ret = clk_prepare_enable(host->clk); 9927c941281SArvind Yadav if (ret) 9937c941281SArvind Yadav return ret; 9942944a44dSRoland Stigge 9952944a44dSRoland Stigge /* Fresh init of NAND controller */ 9962944a44dSRoland Stigge lpc32xx_nand_setup(host); 9972944a44dSRoland Stigge 9982944a44dSRoland Stigge /* Disable write protect */ 9992944a44dSRoland Stigge lpc32xx_wp_disable(host); 10002944a44dSRoland Stigge 10012944a44dSRoland Stigge return 0; 10022944a44dSRoland Stigge } 10032944a44dSRoland Stigge 10042944a44dSRoland Stigge static int lpc32xx_nand_suspend(struct platform_device *pdev, pm_message_t pm) 10052944a44dSRoland Stigge { 10062944a44dSRoland Stigge uint32_t tmp; 10072944a44dSRoland Stigge struct lpc32xx_nand_host *host = platform_get_drvdata(pdev); 10082944a44dSRoland Stigge 10092944a44dSRoland Stigge /* Force CE high */ 10102944a44dSRoland Stigge tmp = readl(SLC_CTRL(host->io_base)); 10112944a44dSRoland Stigge tmp &= ~SLCCFG_CE_LOW; 10122944a44dSRoland Stigge writel(tmp, SLC_CTRL(host->io_base)); 10132944a44dSRoland Stigge 10142944a44dSRoland Stigge /* Enable write protect for safety */ 10152944a44dSRoland Stigge lpc32xx_wp_enable(host); 10162944a44dSRoland Stigge 10172944a44dSRoland Stigge /* Disable clock */ 101844cab9c9SVladimir Zapolskiy clk_disable_unprepare(host->clk); 10192944a44dSRoland Stigge 10202944a44dSRoland Stigge return 0; 10212944a44dSRoland Stigge } 10222944a44dSRoland Stigge 10232944a44dSRoland Stigge #else 10242944a44dSRoland Stigge #define lpc32xx_nand_resume NULL 10252944a44dSRoland Stigge #define lpc32xx_nand_suspend NULL 10262944a44dSRoland Stigge #endif 10272944a44dSRoland Stigge 10282944a44dSRoland Stigge static const struct of_device_id lpc32xx_nand_match[] = { 10292944a44dSRoland Stigge { .compatible = "nxp,lpc3220-slc" }, 10302944a44dSRoland Stigge { /* sentinel */ }, 10312944a44dSRoland Stigge }; 10322944a44dSRoland Stigge MODULE_DEVICE_TABLE(of, lpc32xx_nand_match); 10332944a44dSRoland Stigge 10342944a44dSRoland Stigge static struct platform_driver lpc32xx_nand_driver = { 10352944a44dSRoland Stigge .probe = lpc32xx_nand_probe, 10365153b88cSBill Pemberton .remove = lpc32xx_nand_remove, 10372944a44dSRoland Stigge .resume = lpc32xx_nand_resume, 10382944a44dSRoland Stigge .suspend = lpc32xx_nand_suspend, 10392944a44dSRoland Stigge .driver = { 10402944a44dSRoland Stigge .name = LPC32XX_MODNAME, 1041fea7b569SSachin Kamat .of_match_table = lpc32xx_nand_match, 10422944a44dSRoland Stigge }, 10432944a44dSRoland Stigge }; 10442944a44dSRoland Stigge 10452944a44dSRoland Stigge module_platform_driver(lpc32xx_nand_driver); 10462944a44dSRoland Stigge 10472944a44dSRoland Stigge MODULE_LICENSE("GPL"); 10482944a44dSRoland Stigge MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>"); 10492944a44dSRoland Stigge MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>"); 10502944a44dSRoland Stigge MODULE_DESCRIPTION("NAND driver for the NXP LPC32XX SLC controller"); 1051