18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * NXP LPC32XX NAND SLC driver 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Authors: 68c2ecf20Sopenharmony_ci * Kevin Wells <kevin.wells@nxp.com> 78c2ecf20Sopenharmony_ci * Roland Stigge <stigge@antcom.de> 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Copyright © 2011 NXP Semiconductors 108c2ecf20Sopenharmony_ci * Copyright © 2012 Roland Stigge 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include <linux/slab.h> 148c2ecf20Sopenharmony_ci#include <linux/module.h> 158c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 168c2ecf20Sopenharmony_ci#include <linux/mtd/mtd.h> 178c2ecf20Sopenharmony_ci#include <linux/mtd/rawnand.h> 188c2ecf20Sopenharmony_ci#include <linux/mtd/partitions.h> 198c2ecf20Sopenharmony_ci#include <linux/clk.h> 208c2ecf20Sopenharmony_ci#include <linux/err.h> 218c2ecf20Sopenharmony_ci#include <linux/delay.h> 228c2ecf20Sopenharmony_ci#include <linux/io.h> 238c2ecf20Sopenharmony_ci#include <linux/mm.h> 248c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h> 258c2ecf20Sopenharmony_ci#include <linux/dmaengine.h> 268c2ecf20Sopenharmony_ci#include <linux/mtd/nand_ecc.h> 278c2ecf20Sopenharmony_ci#include <linux/gpio.h> 288c2ecf20Sopenharmony_ci#include <linux/of.h> 298c2ecf20Sopenharmony_ci#include <linux/of_gpio.h> 308c2ecf20Sopenharmony_ci#include <linux/mtd/lpc32xx_slc.h> 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci#define LPC32XX_MODNAME "lpc32xx-nand" 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci/********************************************************************** 358c2ecf20Sopenharmony_ci* SLC NAND controller register offsets 368c2ecf20Sopenharmony_ci**********************************************************************/ 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci#define SLC_DATA(x) (x + 0x000) 398c2ecf20Sopenharmony_ci#define SLC_ADDR(x) (x + 0x004) 408c2ecf20Sopenharmony_ci#define SLC_CMD(x) (x + 0x008) 418c2ecf20Sopenharmony_ci#define SLC_STOP(x) (x + 0x00C) 428c2ecf20Sopenharmony_ci#define SLC_CTRL(x) (x + 0x010) 438c2ecf20Sopenharmony_ci#define SLC_CFG(x) (x + 0x014) 448c2ecf20Sopenharmony_ci#define SLC_STAT(x) (x + 0x018) 458c2ecf20Sopenharmony_ci#define SLC_INT_STAT(x) (x + 0x01C) 468c2ecf20Sopenharmony_ci#define SLC_IEN(x) (x + 0x020) 478c2ecf20Sopenharmony_ci#define SLC_ISR(x) (x + 0x024) 488c2ecf20Sopenharmony_ci#define SLC_ICR(x) (x + 0x028) 498c2ecf20Sopenharmony_ci#define SLC_TAC(x) (x + 0x02C) 508c2ecf20Sopenharmony_ci#define SLC_TC(x) (x + 0x030) 518c2ecf20Sopenharmony_ci#define SLC_ECC(x) (x + 0x034) 528c2ecf20Sopenharmony_ci#define SLC_DMA_DATA(x) (x + 0x038) 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci/********************************************************************** 558c2ecf20Sopenharmony_ci* slc_ctrl register definitions 568c2ecf20Sopenharmony_ci**********************************************************************/ 578c2ecf20Sopenharmony_ci#define SLCCTRL_SW_RESET (1 << 2) /* Reset the NAND controller bit */ 588c2ecf20Sopenharmony_ci#define SLCCTRL_ECC_CLEAR (1 << 1) /* Reset ECC bit */ 598c2ecf20Sopenharmony_ci#define SLCCTRL_DMA_START (1 << 0) /* Start DMA channel bit */ 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci/********************************************************************** 628c2ecf20Sopenharmony_ci* slc_cfg register definitions 638c2ecf20Sopenharmony_ci**********************************************************************/ 648c2ecf20Sopenharmony_ci#define SLCCFG_CE_LOW (1 << 5) /* Force CE low bit */ 658c2ecf20Sopenharmony_ci#define SLCCFG_DMA_ECC (1 << 4) /* Enable DMA ECC bit */ 668c2ecf20Sopenharmony_ci#define SLCCFG_ECC_EN (1 << 3) /* ECC enable bit */ 678c2ecf20Sopenharmony_ci#define SLCCFG_DMA_BURST (1 << 2) /* DMA burst bit */ 688c2ecf20Sopenharmony_ci#define SLCCFG_DMA_DIR (1 << 1) /* DMA write(0)/read(1) bit */ 698c2ecf20Sopenharmony_ci#define SLCCFG_WIDTH (1 << 0) /* External device width, 0=8bit */ 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci/********************************************************************** 728c2ecf20Sopenharmony_ci* slc_stat register definitions 738c2ecf20Sopenharmony_ci**********************************************************************/ 748c2ecf20Sopenharmony_ci#define SLCSTAT_DMA_FIFO (1 << 2) /* DMA FIFO has data bit */ 758c2ecf20Sopenharmony_ci#define SLCSTAT_SLC_FIFO (1 << 1) /* SLC FIFO has data bit */ 768c2ecf20Sopenharmony_ci#define SLCSTAT_NAND_READY (1 << 0) /* NAND device is ready bit */ 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci/********************************************************************** 798c2ecf20Sopenharmony_ci* slc_int_stat, slc_ien, slc_isr, and slc_icr register definitions 808c2ecf20Sopenharmony_ci**********************************************************************/ 818c2ecf20Sopenharmony_ci#define SLCSTAT_INT_TC (1 << 1) /* Transfer count bit */ 828c2ecf20Sopenharmony_ci#define SLCSTAT_INT_RDY_EN (1 << 0) /* Ready interrupt bit */ 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci/********************************************************************** 858c2ecf20Sopenharmony_ci* slc_tac register definitions 868c2ecf20Sopenharmony_ci**********************************************************************/ 878c2ecf20Sopenharmony_ci/* Computation of clock cycles on basis of controller and device clock rates */ 888c2ecf20Sopenharmony_ci#define SLCTAC_CLOCKS(c, n, s) (min_t(u32, DIV_ROUND_UP(c, n) - 1, 0xF) << s) 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci/* Clock setting for RDY write sample wait time in 2*n clocks */ 918c2ecf20Sopenharmony_ci#define SLCTAC_WDR(n) (((n) & 0xF) << 28) 928c2ecf20Sopenharmony_ci/* Write pulse width in clock cycles, 1 to 16 clocks */ 938c2ecf20Sopenharmony_ci#define SLCTAC_WWIDTH(c, n) (SLCTAC_CLOCKS(c, n, 24)) 948c2ecf20Sopenharmony_ci/* Write hold time of control and data signals, 1 to 16 clocks */ 958c2ecf20Sopenharmony_ci#define SLCTAC_WHOLD(c, n) (SLCTAC_CLOCKS(c, n, 20)) 968c2ecf20Sopenharmony_ci/* Write setup time of control and data signals, 1 to 16 clocks */ 978c2ecf20Sopenharmony_ci#define SLCTAC_WSETUP(c, n) (SLCTAC_CLOCKS(c, n, 16)) 988c2ecf20Sopenharmony_ci/* Clock setting for RDY read sample wait time in 2*n clocks */ 998c2ecf20Sopenharmony_ci#define SLCTAC_RDR(n) (((n) & 0xF) << 12) 1008c2ecf20Sopenharmony_ci/* Read pulse width in clock cycles, 1 to 16 clocks */ 1018c2ecf20Sopenharmony_ci#define SLCTAC_RWIDTH(c, n) (SLCTAC_CLOCKS(c, n, 8)) 1028c2ecf20Sopenharmony_ci/* Read hold time of control and data signals, 1 to 16 clocks */ 1038c2ecf20Sopenharmony_ci#define SLCTAC_RHOLD(c, n) (SLCTAC_CLOCKS(c, n, 4)) 1048c2ecf20Sopenharmony_ci/* Read setup time of control and data signals, 1 to 16 clocks */ 1058c2ecf20Sopenharmony_ci#define SLCTAC_RSETUP(c, n) (SLCTAC_CLOCKS(c, n, 0)) 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci/********************************************************************** 1088c2ecf20Sopenharmony_ci* slc_ecc register definitions 1098c2ecf20Sopenharmony_ci**********************************************************************/ 1108c2ecf20Sopenharmony_ci/* ECC line party fetch macro */ 1118c2ecf20Sopenharmony_ci#define SLCECC_TO_LINEPAR(n) (((n) >> 6) & 0x7FFF) 1128c2ecf20Sopenharmony_ci#define SLCECC_TO_COLPAR(n) ((n) & 0x3F) 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci/* 1158c2ecf20Sopenharmony_ci * DMA requires storage space for the DMA local buffer and the hardware ECC 1168c2ecf20Sopenharmony_ci * storage area. The DMA local buffer is only used if DMA mapping fails 1178c2ecf20Sopenharmony_ci * during runtime. 1188c2ecf20Sopenharmony_ci */ 1198c2ecf20Sopenharmony_ci#define LPC32XX_DMA_DATA_SIZE 4096 1208c2ecf20Sopenharmony_ci#define LPC32XX_ECC_SAVE_SIZE ((4096 / 256) * 4) 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci/* Number of bytes used for ECC stored in NAND per 256 bytes */ 1238c2ecf20Sopenharmony_ci#define LPC32XX_SLC_DEV_ECC_BYTES 3 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci/* 1268c2ecf20Sopenharmony_ci * If the NAND base clock frequency can't be fetched, this frequency will be 1278c2ecf20Sopenharmony_ci * used instead as the base. This rate is used to setup the timing registers 1288c2ecf20Sopenharmony_ci * used for NAND accesses. 1298c2ecf20Sopenharmony_ci */ 1308c2ecf20Sopenharmony_ci#define LPC32XX_DEF_BUS_RATE 133250000 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci/* Milliseconds for DMA FIFO timeout (unlikely anyway) */ 1338c2ecf20Sopenharmony_ci#define LPC32XX_DMA_TIMEOUT 100 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci/* 1368c2ecf20Sopenharmony_ci * NAND ECC Layout for small page NAND devices 1378c2ecf20Sopenharmony_ci * Note: For large and huge page devices, the default layouts are used 1388c2ecf20Sopenharmony_ci */ 1398c2ecf20Sopenharmony_cistatic int lpc32xx_ooblayout_ecc(struct mtd_info *mtd, int section, 1408c2ecf20Sopenharmony_ci struct mtd_oob_region *oobregion) 1418c2ecf20Sopenharmony_ci{ 1428c2ecf20Sopenharmony_ci if (section) 1438c2ecf20Sopenharmony_ci return -ERANGE; 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci oobregion->length = 6; 1468c2ecf20Sopenharmony_ci oobregion->offset = 10; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci return 0; 1498c2ecf20Sopenharmony_ci} 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_cistatic int lpc32xx_ooblayout_free(struct mtd_info *mtd, int section, 1528c2ecf20Sopenharmony_ci struct mtd_oob_region *oobregion) 1538c2ecf20Sopenharmony_ci{ 1548c2ecf20Sopenharmony_ci if (section > 1) 1558c2ecf20Sopenharmony_ci return -ERANGE; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci if (!section) { 1588c2ecf20Sopenharmony_ci oobregion->offset = 0; 1598c2ecf20Sopenharmony_ci oobregion->length = 4; 1608c2ecf20Sopenharmony_ci } else { 1618c2ecf20Sopenharmony_ci oobregion->offset = 6; 1628c2ecf20Sopenharmony_ci oobregion->length = 4; 1638c2ecf20Sopenharmony_ci } 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci return 0; 1668c2ecf20Sopenharmony_ci} 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_cistatic const struct mtd_ooblayout_ops lpc32xx_ooblayout_ops = { 1698c2ecf20Sopenharmony_ci .ecc = lpc32xx_ooblayout_ecc, 1708c2ecf20Sopenharmony_ci .free = lpc32xx_ooblayout_free, 1718c2ecf20Sopenharmony_ci}; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_cistatic u8 bbt_pattern[] = {'B', 'b', 't', '0' }; 1748c2ecf20Sopenharmony_cistatic u8 mirror_pattern[] = {'1', 't', 'b', 'B' }; 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci/* 1778c2ecf20Sopenharmony_ci * Small page FLASH BBT descriptors, marker at offset 0, version at offset 6 1788c2ecf20Sopenharmony_ci * Note: Large page devices used the default layout 1798c2ecf20Sopenharmony_ci */ 1808c2ecf20Sopenharmony_cistatic struct nand_bbt_descr bbt_smallpage_main_descr = { 1818c2ecf20Sopenharmony_ci .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE 1828c2ecf20Sopenharmony_ci | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, 1838c2ecf20Sopenharmony_ci .offs = 0, 1848c2ecf20Sopenharmony_ci .len = 4, 1858c2ecf20Sopenharmony_ci .veroffs = 6, 1868c2ecf20Sopenharmony_ci .maxblocks = 4, 1878c2ecf20Sopenharmony_ci .pattern = bbt_pattern 1888c2ecf20Sopenharmony_ci}; 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_cistatic struct nand_bbt_descr bbt_smallpage_mirror_descr = { 1918c2ecf20Sopenharmony_ci .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE 1928c2ecf20Sopenharmony_ci | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, 1938c2ecf20Sopenharmony_ci .offs = 0, 1948c2ecf20Sopenharmony_ci .len = 4, 1958c2ecf20Sopenharmony_ci .veroffs = 6, 1968c2ecf20Sopenharmony_ci .maxblocks = 4, 1978c2ecf20Sopenharmony_ci .pattern = mirror_pattern 1988c2ecf20Sopenharmony_ci}; 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci/* 2018c2ecf20Sopenharmony_ci * NAND platform configuration structure 2028c2ecf20Sopenharmony_ci */ 2038c2ecf20Sopenharmony_cistruct lpc32xx_nand_cfg_slc { 2048c2ecf20Sopenharmony_ci uint32_t wdr_clks; 2058c2ecf20Sopenharmony_ci uint32_t wwidth; 2068c2ecf20Sopenharmony_ci uint32_t whold; 2078c2ecf20Sopenharmony_ci uint32_t wsetup; 2088c2ecf20Sopenharmony_ci uint32_t rdr_clks; 2098c2ecf20Sopenharmony_ci uint32_t rwidth; 2108c2ecf20Sopenharmony_ci uint32_t rhold; 2118c2ecf20Sopenharmony_ci uint32_t rsetup; 2128c2ecf20Sopenharmony_ci int wp_gpio; 2138c2ecf20Sopenharmony_ci struct mtd_partition *parts; 2148c2ecf20Sopenharmony_ci unsigned num_parts; 2158c2ecf20Sopenharmony_ci}; 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_cistruct lpc32xx_nand_host { 2188c2ecf20Sopenharmony_ci struct nand_chip nand_chip; 2198c2ecf20Sopenharmony_ci struct lpc32xx_slc_platform_data *pdata; 2208c2ecf20Sopenharmony_ci struct clk *clk; 2218c2ecf20Sopenharmony_ci void __iomem *io_base; 2228c2ecf20Sopenharmony_ci struct lpc32xx_nand_cfg_slc *ncfg; 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci struct completion comp; 2258c2ecf20Sopenharmony_ci struct dma_chan *dma_chan; 2268c2ecf20Sopenharmony_ci uint32_t dma_buf_len; 2278c2ecf20Sopenharmony_ci struct dma_slave_config dma_slave_config; 2288c2ecf20Sopenharmony_ci struct scatterlist sgl; 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci /* 2318c2ecf20Sopenharmony_ci * DMA and CPU addresses of ECC work area and data buffer 2328c2ecf20Sopenharmony_ci */ 2338c2ecf20Sopenharmony_ci uint32_t *ecc_buf; 2348c2ecf20Sopenharmony_ci uint8_t *data_buf; 2358c2ecf20Sopenharmony_ci dma_addr_t io_base_dma; 2368c2ecf20Sopenharmony_ci}; 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_cistatic void lpc32xx_nand_setup(struct lpc32xx_nand_host *host) 2398c2ecf20Sopenharmony_ci{ 2408c2ecf20Sopenharmony_ci uint32_t clkrate, tmp; 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ci /* Reset SLC controller */ 2438c2ecf20Sopenharmony_ci writel(SLCCTRL_SW_RESET, SLC_CTRL(host->io_base)); 2448c2ecf20Sopenharmony_ci udelay(1000); 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci /* Basic setup */ 2478c2ecf20Sopenharmony_ci writel(0, SLC_CFG(host->io_base)); 2488c2ecf20Sopenharmony_ci writel(0, SLC_IEN(host->io_base)); 2498c2ecf20Sopenharmony_ci writel((SLCSTAT_INT_TC | SLCSTAT_INT_RDY_EN), 2508c2ecf20Sopenharmony_ci SLC_ICR(host->io_base)); 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci /* Get base clock for SLC block */ 2538c2ecf20Sopenharmony_ci clkrate = clk_get_rate(host->clk); 2548c2ecf20Sopenharmony_ci if (clkrate == 0) 2558c2ecf20Sopenharmony_ci clkrate = LPC32XX_DEF_BUS_RATE; 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci /* Compute clock setup values */ 2588c2ecf20Sopenharmony_ci tmp = SLCTAC_WDR(host->ncfg->wdr_clks) | 2598c2ecf20Sopenharmony_ci SLCTAC_WWIDTH(clkrate, host->ncfg->wwidth) | 2608c2ecf20Sopenharmony_ci SLCTAC_WHOLD(clkrate, host->ncfg->whold) | 2618c2ecf20Sopenharmony_ci SLCTAC_WSETUP(clkrate, host->ncfg->wsetup) | 2628c2ecf20Sopenharmony_ci SLCTAC_RDR(host->ncfg->rdr_clks) | 2638c2ecf20Sopenharmony_ci SLCTAC_RWIDTH(clkrate, host->ncfg->rwidth) | 2648c2ecf20Sopenharmony_ci SLCTAC_RHOLD(clkrate, host->ncfg->rhold) | 2658c2ecf20Sopenharmony_ci SLCTAC_RSETUP(clkrate, host->ncfg->rsetup); 2668c2ecf20Sopenharmony_ci writel(tmp, SLC_TAC(host->io_base)); 2678c2ecf20Sopenharmony_ci} 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci/* 2708c2ecf20Sopenharmony_ci * Hardware specific access to control lines 2718c2ecf20Sopenharmony_ci */ 2728c2ecf20Sopenharmony_cistatic void lpc32xx_nand_cmd_ctrl(struct nand_chip *chip, int cmd, 2738c2ecf20Sopenharmony_ci unsigned int ctrl) 2748c2ecf20Sopenharmony_ci{ 2758c2ecf20Sopenharmony_ci uint32_t tmp; 2768c2ecf20Sopenharmony_ci struct lpc32xx_nand_host *host = nand_get_controller_data(chip); 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_ci /* Does CE state need to be changed? */ 2798c2ecf20Sopenharmony_ci tmp = readl(SLC_CFG(host->io_base)); 2808c2ecf20Sopenharmony_ci if (ctrl & NAND_NCE) 2818c2ecf20Sopenharmony_ci tmp |= SLCCFG_CE_LOW; 2828c2ecf20Sopenharmony_ci else 2838c2ecf20Sopenharmony_ci tmp &= ~SLCCFG_CE_LOW; 2848c2ecf20Sopenharmony_ci writel(tmp, SLC_CFG(host->io_base)); 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_ci if (cmd != NAND_CMD_NONE) { 2878c2ecf20Sopenharmony_ci if (ctrl & NAND_CLE) 2888c2ecf20Sopenharmony_ci writel(cmd, SLC_CMD(host->io_base)); 2898c2ecf20Sopenharmony_ci else 2908c2ecf20Sopenharmony_ci writel(cmd, SLC_ADDR(host->io_base)); 2918c2ecf20Sopenharmony_ci } 2928c2ecf20Sopenharmony_ci} 2938c2ecf20Sopenharmony_ci 2948c2ecf20Sopenharmony_ci/* 2958c2ecf20Sopenharmony_ci * Read the Device Ready pin 2968c2ecf20Sopenharmony_ci */ 2978c2ecf20Sopenharmony_cistatic int lpc32xx_nand_device_ready(struct nand_chip *chip) 2988c2ecf20Sopenharmony_ci{ 2998c2ecf20Sopenharmony_ci struct lpc32xx_nand_host *host = nand_get_controller_data(chip); 3008c2ecf20Sopenharmony_ci int rdy = 0; 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci if ((readl(SLC_STAT(host->io_base)) & SLCSTAT_NAND_READY) != 0) 3038c2ecf20Sopenharmony_ci rdy = 1; 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci return rdy; 3068c2ecf20Sopenharmony_ci} 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_ci/* 3098c2ecf20Sopenharmony_ci * Enable NAND write protect 3108c2ecf20Sopenharmony_ci */ 3118c2ecf20Sopenharmony_cistatic void lpc32xx_wp_enable(struct lpc32xx_nand_host *host) 3128c2ecf20Sopenharmony_ci{ 3138c2ecf20Sopenharmony_ci if (gpio_is_valid(host->ncfg->wp_gpio)) 3148c2ecf20Sopenharmony_ci gpio_set_value(host->ncfg->wp_gpio, 0); 3158c2ecf20Sopenharmony_ci} 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_ci/* 3188c2ecf20Sopenharmony_ci * Disable NAND write protect 3198c2ecf20Sopenharmony_ci */ 3208c2ecf20Sopenharmony_cistatic void lpc32xx_wp_disable(struct lpc32xx_nand_host *host) 3218c2ecf20Sopenharmony_ci{ 3228c2ecf20Sopenharmony_ci if (gpio_is_valid(host->ncfg->wp_gpio)) 3238c2ecf20Sopenharmony_ci gpio_set_value(host->ncfg->wp_gpio, 1); 3248c2ecf20Sopenharmony_ci} 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ci/* 3278c2ecf20Sopenharmony_ci * Prepares SLC for transfers with H/W ECC enabled 3288c2ecf20Sopenharmony_ci */ 3298c2ecf20Sopenharmony_cistatic void lpc32xx_nand_ecc_enable(struct nand_chip *chip, int mode) 3308c2ecf20Sopenharmony_ci{ 3318c2ecf20Sopenharmony_ci /* Hardware ECC is enabled automatically in hardware as needed */ 3328c2ecf20Sopenharmony_ci} 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci/* 3358c2ecf20Sopenharmony_ci * Calculates the ECC for the data 3368c2ecf20Sopenharmony_ci */ 3378c2ecf20Sopenharmony_cistatic int lpc32xx_nand_ecc_calculate(struct nand_chip *chip, 3388c2ecf20Sopenharmony_ci const unsigned char *buf, 3398c2ecf20Sopenharmony_ci unsigned char *code) 3408c2ecf20Sopenharmony_ci{ 3418c2ecf20Sopenharmony_ci /* 3428c2ecf20Sopenharmony_ci * ECC is calculated automatically in hardware during syndrome read 3438c2ecf20Sopenharmony_ci * and write operations, so it doesn't need to be calculated here. 3448c2ecf20Sopenharmony_ci */ 3458c2ecf20Sopenharmony_ci return 0; 3468c2ecf20Sopenharmony_ci} 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_ci/* 3498c2ecf20Sopenharmony_ci * Read a single byte from NAND device 3508c2ecf20Sopenharmony_ci */ 3518c2ecf20Sopenharmony_cistatic uint8_t lpc32xx_nand_read_byte(struct nand_chip *chip) 3528c2ecf20Sopenharmony_ci{ 3538c2ecf20Sopenharmony_ci struct lpc32xx_nand_host *host = nand_get_controller_data(chip); 3548c2ecf20Sopenharmony_ci 3558c2ecf20Sopenharmony_ci return (uint8_t)readl(SLC_DATA(host->io_base)); 3568c2ecf20Sopenharmony_ci} 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci/* 3598c2ecf20Sopenharmony_ci * Simple device read without ECC 3608c2ecf20Sopenharmony_ci */ 3618c2ecf20Sopenharmony_cistatic void lpc32xx_nand_read_buf(struct nand_chip *chip, u_char *buf, int len) 3628c2ecf20Sopenharmony_ci{ 3638c2ecf20Sopenharmony_ci struct lpc32xx_nand_host *host = nand_get_controller_data(chip); 3648c2ecf20Sopenharmony_ci 3658c2ecf20Sopenharmony_ci /* Direct device read with no ECC */ 3668c2ecf20Sopenharmony_ci while (len-- > 0) 3678c2ecf20Sopenharmony_ci *buf++ = (uint8_t)readl(SLC_DATA(host->io_base)); 3688c2ecf20Sopenharmony_ci} 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci/* 3718c2ecf20Sopenharmony_ci * Simple device write without ECC 3728c2ecf20Sopenharmony_ci */ 3738c2ecf20Sopenharmony_cistatic void lpc32xx_nand_write_buf(struct nand_chip *chip, const uint8_t *buf, 3748c2ecf20Sopenharmony_ci int len) 3758c2ecf20Sopenharmony_ci{ 3768c2ecf20Sopenharmony_ci struct lpc32xx_nand_host *host = nand_get_controller_data(chip); 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci /* Direct device write with no ECC */ 3798c2ecf20Sopenharmony_ci while (len-- > 0) 3808c2ecf20Sopenharmony_ci writel((uint32_t)*buf++, SLC_DATA(host->io_base)); 3818c2ecf20Sopenharmony_ci} 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_ci/* 3848c2ecf20Sopenharmony_ci * Read the OOB data from the device without ECC using FIFO method 3858c2ecf20Sopenharmony_ci */ 3868c2ecf20Sopenharmony_cistatic int lpc32xx_nand_read_oob_syndrome(struct nand_chip *chip, int page) 3878c2ecf20Sopenharmony_ci{ 3888c2ecf20Sopenharmony_ci struct mtd_info *mtd = nand_to_mtd(chip); 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_ci return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize); 3918c2ecf20Sopenharmony_ci} 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci/* 3948c2ecf20Sopenharmony_ci * Write the OOB data to the device without ECC using FIFO method 3958c2ecf20Sopenharmony_ci */ 3968c2ecf20Sopenharmony_cistatic int lpc32xx_nand_write_oob_syndrome(struct nand_chip *chip, int page) 3978c2ecf20Sopenharmony_ci{ 3988c2ecf20Sopenharmony_ci struct mtd_info *mtd = nand_to_mtd(chip); 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_ci return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi, 4018c2ecf20Sopenharmony_ci mtd->oobsize); 4028c2ecf20Sopenharmony_ci} 4038c2ecf20Sopenharmony_ci 4048c2ecf20Sopenharmony_ci/* 4058c2ecf20Sopenharmony_ci * Fills in the ECC fields in the OOB buffer with the hardware generated ECC 4068c2ecf20Sopenharmony_ci */ 4078c2ecf20Sopenharmony_cistatic void lpc32xx_slc_ecc_copy(uint8_t *spare, const uint32_t *ecc, int count) 4088c2ecf20Sopenharmony_ci{ 4098c2ecf20Sopenharmony_ci int i; 4108c2ecf20Sopenharmony_ci 4118c2ecf20Sopenharmony_ci for (i = 0; i < (count * 3); i += 3) { 4128c2ecf20Sopenharmony_ci uint32_t ce = ecc[i / 3]; 4138c2ecf20Sopenharmony_ci ce = ~(ce << 2) & 0xFFFFFF; 4148c2ecf20Sopenharmony_ci spare[i + 2] = (uint8_t)(ce & 0xFF); 4158c2ecf20Sopenharmony_ci ce >>= 8; 4168c2ecf20Sopenharmony_ci spare[i + 1] = (uint8_t)(ce & 0xFF); 4178c2ecf20Sopenharmony_ci ce >>= 8; 4188c2ecf20Sopenharmony_ci spare[i] = (uint8_t)(ce & 0xFF); 4198c2ecf20Sopenharmony_ci } 4208c2ecf20Sopenharmony_ci} 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_cistatic void lpc32xx_dma_complete_func(void *completion) 4238c2ecf20Sopenharmony_ci{ 4248c2ecf20Sopenharmony_ci complete(completion); 4258c2ecf20Sopenharmony_ci} 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_cistatic int lpc32xx_xmit_dma(struct mtd_info *mtd, dma_addr_t dma, 4288c2ecf20Sopenharmony_ci void *mem, int len, enum dma_transfer_direction dir) 4298c2ecf20Sopenharmony_ci{ 4308c2ecf20Sopenharmony_ci struct nand_chip *chip = mtd_to_nand(mtd); 4318c2ecf20Sopenharmony_ci struct lpc32xx_nand_host *host = nand_get_controller_data(chip); 4328c2ecf20Sopenharmony_ci struct dma_async_tx_descriptor *desc; 4338c2ecf20Sopenharmony_ci int flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT; 4348c2ecf20Sopenharmony_ci int res; 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci host->dma_slave_config.direction = dir; 4378c2ecf20Sopenharmony_ci host->dma_slave_config.src_addr = dma; 4388c2ecf20Sopenharmony_ci host->dma_slave_config.dst_addr = dma; 4398c2ecf20Sopenharmony_ci host->dma_slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 4408c2ecf20Sopenharmony_ci host->dma_slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; 4418c2ecf20Sopenharmony_ci host->dma_slave_config.src_maxburst = 4; 4428c2ecf20Sopenharmony_ci host->dma_slave_config.dst_maxburst = 4; 4438c2ecf20Sopenharmony_ci /* DMA controller does flow control: */ 4448c2ecf20Sopenharmony_ci host->dma_slave_config.device_fc = false; 4458c2ecf20Sopenharmony_ci if (dmaengine_slave_config(host->dma_chan, &host->dma_slave_config)) { 4468c2ecf20Sopenharmony_ci dev_err(mtd->dev.parent, "Failed to setup DMA slave\n"); 4478c2ecf20Sopenharmony_ci return -ENXIO; 4488c2ecf20Sopenharmony_ci } 4498c2ecf20Sopenharmony_ci 4508c2ecf20Sopenharmony_ci sg_init_one(&host->sgl, mem, len); 4518c2ecf20Sopenharmony_ci 4528c2ecf20Sopenharmony_ci res = dma_map_sg(host->dma_chan->device->dev, &host->sgl, 1, 4538c2ecf20Sopenharmony_ci DMA_BIDIRECTIONAL); 4548c2ecf20Sopenharmony_ci if (res != 1) { 4558c2ecf20Sopenharmony_ci dev_err(mtd->dev.parent, "Failed to map sg list\n"); 4568c2ecf20Sopenharmony_ci return -ENXIO; 4578c2ecf20Sopenharmony_ci } 4588c2ecf20Sopenharmony_ci desc = dmaengine_prep_slave_sg(host->dma_chan, &host->sgl, 1, dir, 4598c2ecf20Sopenharmony_ci flags); 4608c2ecf20Sopenharmony_ci if (!desc) { 4618c2ecf20Sopenharmony_ci dev_err(mtd->dev.parent, "Failed to prepare slave sg\n"); 4628c2ecf20Sopenharmony_ci goto out1; 4638c2ecf20Sopenharmony_ci } 4648c2ecf20Sopenharmony_ci 4658c2ecf20Sopenharmony_ci init_completion(&host->comp); 4668c2ecf20Sopenharmony_ci desc->callback = lpc32xx_dma_complete_func; 4678c2ecf20Sopenharmony_ci desc->callback_param = &host->comp; 4688c2ecf20Sopenharmony_ci 4698c2ecf20Sopenharmony_ci dmaengine_submit(desc); 4708c2ecf20Sopenharmony_ci dma_async_issue_pending(host->dma_chan); 4718c2ecf20Sopenharmony_ci 4728c2ecf20Sopenharmony_ci wait_for_completion_timeout(&host->comp, msecs_to_jiffies(1000)); 4738c2ecf20Sopenharmony_ci 4748c2ecf20Sopenharmony_ci dma_unmap_sg(host->dma_chan->device->dev, &host->sgl, 1, 4758c2ecf20Sopenharmony_ci DMA_BIDIRECTIONAL); 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_ci return 0; 4788c2ecf20Sopenharmony_ciout1: 4798c2ecf20Sopenharmony_ci dma_unmap_sg(host->dma_chan->device->dev, &host->sgl, 1, 4808c2ecf20Sopenharmony_ci DMA_BIDIRECTIONAL); 4818c2ecf20Sopenharmony_ci return -ENXIO; 4828c2ecf20Sopenharmony_ci} 4838c2ecf20Sopenharmony_ci 4848c2ecf20Sopenharmony_ci/* 4858c2ecf20Sopenharmony_ci * DMA read/write transfers with ECC support 4868c2ecf20Sopenharmony_ci */ 4878c2ecf20Sopenharmony_cistatic int lpc32xx_xfer(struct mtd_info *mtd, uint8_t *buf, int eccsubpages, 4888c2ecf20Sopenharmony_ci int read) 4898c2ecf20Sopenharmony_ci{ 4908c2ecf20Sopenharmony_ci struct nand_chip *chip = mtd_to_nand(mtd); 4918c2ecf20Sopenharmony_ci struct lpc32xx_nand_host *host = nand_get_controller_data(chip); 4928c2ecf20Sopenharmony_ci int i, status = 0; 4938c2ecf20Sopenharmony_ci unsigned long timeout; 4948c2ecf20Sopenharmony_ci int res; 4958c2ecf20Sopenharmony_ci enum dma_transfer_direction dir = 4968c2ecf20Sopenharmony_ci read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV; 4978c2ecf20Sopenharmony_ci uint8_t *dma_buf; 4988c2ecf20Sopenharmony_ci bool dma_mapped; 4998c2ecf20Sopenharmony_ci 5008c2ecf20Sopenharmony_ci if ((void *)buf <= high_memory) { 5018c2ecf20Sopenharmony_ci dma_buf = buf; 5028c2ecf20Sopenharmony_ci dma_mapped = true; 5038c2ecf20Sopenharmony_ci } else { 5048c2ecf20Sopenharmony_ci dma_buf = host->data_buf; 5058c2ecf20Sopenharmony_ci dma_mapped = false; 5068c2ecf20Sopenharmony_ci if (!read) 5078c2ecf20Sopenharmony_ci memcpy(host->data_buf, buf, mtd->writesize); 5088c2ecf20Sopenharmony_ci } 5098c2ecf20Sopenharmony_ci 5108c2ecf20Sopenharmony_ci if (read) { 5118c2ecf20Sopenharmony_ci writel(readl(SLC_CFG(host->io_base)) | 5128c2ecf20Sopenharmony_ci SLCCFG_DMA_DIR | SLCCFG_ECC_EN | SLCCFG_DMA_ECC | 5138c2ecf20Sopenharmony_ci SLCCFG_DMA_BURST, SLC_CFG(host->io_base)); 5148c2ecf20Sopenharmony_ci } else { 5158c2ecf20Sopenharmony_ci writel((readl(SLC_CFG(host->io_base)) | 5168c2ecf20Sopenharmony_ci SLCCFG_ECC_EN | SLCCFG_DMA_ECC | SLCCFG_DMA_BURST) & 5178c2ecf20Sopenharmony_ci ~SLCCFG_DMA_DIR, 5188c2ecf20Sopenharmony_ci SLC_CFG(host->io_base)); 5198c2ecf20Sopenharmony_ci } 5208c2ecf20Sopenharmony_ci 5218c2ecf20Sopenharmony_ci /* Clear initial ECC */ 5228c2ecf20Sopenharmony_ci writel(SLCCTRL_ECC_CLEAR, SLC_CTRL(host->io_base)); 5238c2ecf20Sopenharmony_ci 5248c2ecf20Sopenharmony_ci /* Transfer size is data area only */ 5258c2ecf20Sopenharmony_ci writel(mtd->writesize, SLC_TC(host->io_base)); 5268c2ecf20Sopenharmony_ci 5278c2ecf20Sopenharmony_ci /* Start transfer in the NAND controller */ 5288c2ecf20Sopenharmony_ci writel(readl(SLC_CTRL(host->io_base)) | SLCCTRL_DMA_START, 5298c2ecf20Sopenharmony_ci SLC_CTRL(host->io_base)); 5308c2ecf20Sopenharmony_ci 5318c2ecf20Sopenharmony_ci for (i = 0; i < chip->ecc.steps; i++) { 5328c2ecf20Sopenharmony_ci /* Data */ 5338c2ecf20Sopenharmony_ci res = lpc32xx_xmit_dma(mtd, SLC_DMA_DATA(host->io_base_dma), 5348c2ecf20Sopenharmony_ci dma_buf + i * chip->ecc.size, 5358c2ecf20Sopenharmony_ci mtd->writesize / chip->ecc.steps, dir); 5368c2ecf20Sopenharmony_ci if (res) 5378c2ecf20Sopenharmony_ci return res; 5388c2ecf20Sopenharmony_ci 5398c2ecf20Sopenharmony_ci /* Always _read_ ECC */ 5408c2ecf20Sopenharmony_ci if (i == chip->ecc.steps - 1) 5418c2ecf20Sopenharmony_ci break; 5428c2ecf20Sopenharmony_ci if (!read) /* ECC availability delayed on write */ 5438c2ecf20Sopenharmony_ci udelay(10); 5448c2ecf20Sopenharmony_ci res = lpc32xx_xmit_dma(mtd, SLC_ECC(host->io_base_dma), 5458c2ecf20Sopenharmony_ci &host->ecc_buf[i], 4, DMA_DEV_TO_MEM); 5468c2ecf20Sopenharmony_ci if (res) 5478c2ecf20Sopenharmony_ci return res; 5488c2ecf20Sopenharmony_ci } 5498c2ecf20Sopenharmony_ci 5508c2ecf20Sopenharmony_ci /* 5518c2ecf20Sopenharmony_ci * According to NXP, the DMA can be finished here, but the NAND 5528c2ecf20Sopenharmony_ci * controller may still have buffered data. After porting to using the 5538c2ecf20Sopenharmony_ci * dmaengine DMA driver (amba-pl080), the condition (DMA_FIFO empty) 5548c2ecf20Sopenharmony_ci * appears to be always true, according to tests. Keeping the check for 5558c2ecf20Sopenharmony_ci * safety reasons for now. 5568c2ecf20Sopenharmony_ci */ 5578c2ecf20Sopenharmony_ci if (readl(SLC_STAT(host->io_base)) & SLCSTAT_DMA_FIFO) { 5588c2ecf20Sopenharmony_ci dev_warn(mtd->dev.parent, "FIFO not empty!\n"); 5598c2ecf20Sopenharmony_ci timeout = jiffies + msecs_to_jiffies(LPC32XX_DMA_TIMEOUT); 5608c2ecf20Sopenharmony_ci while ((readl(SLC_STAT(host->io_base)) & SLCSTAT_DMA_FIFO) && 5618c2ecf20Sopenharmony_ci time_before(jiffies, timeout)) 5628c2ecf20Sopenharmony_ci cpu_relax(); 5638c2ecf20Sopenharmony_ci if (!time_before(jiffies, timeout)) { 5648c2ecf20Sopenharmony_ci dev_err(mtd->dev.parent, "FIFO held data too long\n"); 5658c2ecf20Sopenharmony_ci status = -EIO; 5668c2ecf20Sopenharmony_ci } 5678c2ecf20Sopenharmony_ci } 5688c2ecf20Sopenharmony_ci 5698c2ecf20Sopenharmony_ci /* Read last calculated ECC value */ 5708c2ecf20Sopenharmony_ci if (!read) 5718c2ecf20Sopenharmony_ci udelay(10); 5728c2ecf20Sopenharmony_ci host->ecc_buf[chip->ecc.steps - 1] = 5738c2ecf20Sopenharmony_ci readl(SLC_ECC(host->io_base)); 5748c2ecf20Sopenharmony_ci 5758c2ecf20Sopenharmony_ci /* Flush DMA */ 5768c2ecf20Sopenharmony_ci dmaengine_terminate_all(host->dma_chan); 5778c2ecf20Sopenharmony_ci 5788c2ecf20Sopenharmony_ci if (readl(SLC_STAT(host->io_base)) & SLCSTAT_DMA_FIFO || 5798c2ecf20Sopenharmony_ci readl(SLC_TC(host->io_base))) { 5808c2ecf20Sopenharmony_ci /* Something is left in the FIFO, something is wrong */ 5818c2ecf20Sopenharmony_ci dev_err(mtd->dev.parent, "DMA FIFO failure\n"); 5828c2ecf20Sopenharmony_ci status = -EIO; 5838c2ecf20Sopenharmony_ci } 5848c2ecf20Sopenharmony_ci 5858c2ecf20Sopenharmony_ci /* Stop DMA & HW ECC */ 5868c2ecf20Sopenharmony_ci writel(readl(SLC_CTRL(host->io_base)) & ~SLCCTRL_DMA_START, 5878c2ecf20Sopenharmony_ci SLC_CTRL(host->io_base)); 5888c2ecf20Sopenharmony_ci writel(readl(SLC_CFG(host->io_base)) & 5898c2ecf20Sopenharmony_ci ~(SLCCFG_DMA_DIR | SLCCFG_ECC_EN | SLCCFG_DMA_ECC | 5908c2ecf20Sopenharmony_ci SLCCFG_DMA_BURST), SLC_CFG(host->io_base)); 5918c2ecf20Sopenharmony_ci 5928c2ecf20Sopenharmony_ci if (!dma_mapped && read) 5938c2ecf20Sopenharmony_ci memcpy(buf, host->data_buf, mtd->writesize); 5948c2ecf20Sopenharmony_ci 5958c2ecf20Sopenharmony_ci return status; 5968c2ecf20Sopenharmony_ci} 5978c2ecf20Sopenharmony_ci 5988c2ecf20Sopenharmony_ci/* 5998c2ecf20Sopenharmony_ci * Read the data and OOB data from the device, use ECC correction with the 6008c2ecf20Sopenharmony_ci * data, disable ECC for the OOB data 6018c2ecf20Sopenharmony_ci */ 6028c2ecf20Sopenharmony_cistatic int lpc32xx_nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf, 6038c2ecf20Sopenharmony_ci int oob_required, int page) 6048c2ecf20Sopenharmony_ci{ 6058c2ecf20Sopenharmony_ci struct mtd_info *mtd = nand_to_mtd(chip); 6068c2ecf20Sopenharmony_ci struct lpc32xx_nand_host *host = nand_get_controller_data(chip); 6078c2ecf20Sopenharmony_ci struct mtd_oob_region oobregion = { }; 6088c2ecf20Sopenharmony_ci int stat, i, status, error; 6098c2ecf20Sopenharmony_ci uint8_t *oobecc, tmpecc[LPC32XX_ECC_SAVE_SIZE]; 6108c2ecf20Sopenharmony_ci 6118c2ecf20Sopenharmony_ci /* Issue read command */ 6128c2ecf20Sopenharmony_ci nand_read_page_op(chip, page, 0, NULL, 0); 6138c2ecf20Sopenharmony_ci 6148c2ecf20Sopenharmony_ci /* Read data and oob, calculate ECC */ 6158c2ecf20Sopenharmony_ci status = lpc32xx_xfer(mtd, buf, chip->ecc.steps, 1); 6168c2ecf20Sopenharmony_ci 6178c2ecf20Sopenharmony_ci /* Get OOB data */ 6188c2ecf20Sopenharmony_ci chip->legacy.read_buf(chip, chip->oob_poi, mtd->oobsize); 6198c2ecf20Sopenharmony_ci 6208c2ecf20Sopenharmony_ci /* Convert to stored ECC format */ 6218c2ecf20Sopenharmony_ci lpc32xx_slc_ecc_copy(tmpecc, (uint32_t *) host->ecc_buf, chip->ecc.steps); 6228c2ecf20Sopenharmony_ci 6238c2ecf20Sopenharmony_ci /* Pointer to ECC data retrieved from NAND spare area */ 6248c2ecf20Sopenharmony_ci error = mtd_ooblayout_ecc(mtd, 0, &oobregion); 6258c2ecf20Sopenharmony_ci if (error) 6268c2ecf20Sopenharmony_ci return error; 6278c2ecf20Sopenharmony_ci 6288c2ecf20Sopenharmony_ci oobecc = chip->oob_poi + oobregion.offset; 6298c2ecf20Sopenharmony_ci 6308c2ecf20Sopenharmony_ci for (i = 0; i < chip->ecc.steps; i++) { 6318c2ecf20Sopenharmony_ci stat = chip->ecc.correct(chip, buf, oobecc, 6328c2ecf20Sopenharmony_ci &tmpecc[i * chip->ecc.bytes]); 6338c2ecf20Sopenharmony_ci if (stat < 0) 6348c2ecf20Sopenharmony_ci mtd->ecc_stats.failed++; 6358c2ecf20Sopenharmony_ci else 6368c2ecf20Sopenharmony_ci mtd->ecc_stats.corrected += stat; 6378c2ecf20Sopenharmony_ci 6388c2ecf20Sopenharmony_ci buf += chip->ecc.size; 6398c2ecf20Sopenharmony_ci oobecc += chip->ecc.bytes; 6408c2ecf20Sopenharmony_ci } 6418c2ecf20Sopenharmony_ci 6428c2ecf20Sopenharmony_ci return status; 6438c2ecf20Sopenharmony_ci} 6448c2ecf20Sopenharmony_ci 6458c2ecf20Sopenharmony_ci/* 6468c2ecf20Sopenharmony_ci * Read the data and OOB data from the device, no ECC correction with the 6478c2ecf20Sopenharmony_ci * data or OOB data 6488c2ecf20Sopenharmony_ci */ 6498c2ecf20Sopenharmony_cistatic int lpc32xx_nand_read_page_raw_syndrome(struct nand_chip *chip, 6508c2ecf20Sopenharmony_ci uint8_t *buf, int oob_required, 6518c2ecf20Sopenharmony_ci int page) 6528c2ecf20Sopenharmony_ci{ 6538c2ecf20Sopenharmony_ci struct mtd_info *mtd = nand_to_mtd(chip); 6548c2ecf20Sopenharmony_ci 6558c2ecf20Sopenharmony_ci /* Issue read command */ 6568c2ecf20Sopenharmony_ci nand_read_page_op(chip, page, 0, NULL, 0); 6578c2ecf20Sopenharmony_ci 6588c2ecf20Sopenharmony_ci /* Raw reads can just use the FIFO interface */ 6598c2ecf20Sopenharmony_ci chip->legacy.read_buf(chip, buf, chip->ecc.size * chip->ecc.steps); 6608c2ecf20Sopenharmony_ci chip->legacy.read_buf(chip, chip->oob_poi, mtd->oobsize); 6618c2ecf20Sopenharmony_ci 6628c2ecf20Sopenharmony_ci return 0; 6638c2ecf20Sopenharmony_ci} 6648c2ecf20Sopenharmony_ci 6658c2ecf20Sopenharmony_ci/* 6668c2ecf20Sopenharmony_ci * Write the data and OOB data to the device, use ECC with the data, 6678c2ecf20Sopenharmony_ci * disable ECC for the OOB data 6688c2ecf20Sopenharmony_ci */ 6698c2ecf20Sopenharmony_cistatic int lpc32xx_nand_write_page_syndrome(struct nand_chip *chip, 6708c2ecf20Sopenharmony_ci const uint8_t *buf, 6718c2ecf20Sopenharmony_ci int oob_required, int page) 6728c2ecf20Sopenharmony_ci{ 6738c2ecf20Sopenharmony_ci struct mtd_info *mtd = nand_to_mtd(chip); 6748c2ecf20Sopenharmony_ci struct lpc32xx_nand_host *host = nand_get_controller_data(chip); 6758c2ecf20Sopenharmony_ci struct mtd_oob_region oobregion = { }; 6768c2ecf20Sopenharmony_ci uint8_t *pb; 6778c2ecf20Sopenharmony_ci int error; 6788c2ecf20Sopenharmony_ci 6798c2ecf20Sopenharmony_ci nand_prog_page_begin_op(chip, page, 0, NULL, 0); 6808c2ecf20Sopenharmony_ci 6818c2ecf20Sopenharmony_ci /* Write data, calculate ECC on outbound data */ 6828c2ecf20Sopenharmony_ci error = lpc32xx_xfer(mtd, (uint8_t *)buf, chip->ecc.steps, 0); 6838c2ecf20Sopenharmony_ci if (error) 6848c2ecf20Sopenharmony_ci return error; 6858c2ecf20Sopenharmony_ci 6868c2ecf20Sopenharmony_ci /* 6878c2ecf20Sopenharmony_ci * The calculated ECC needs some manual work done to it before 6888c2ecf20Sopenharmony_ci * committing it to NAND. Process the calculated ECC and place 6898c2ecf20Sopenharmony_ci * the resultant values directly into the OOB buffer. */ 6908c2ecf20Sopenharmony_ci error = mtd_ooblayout_ecc(mtd, 0, &oobregion); 6918c2ecf20Sopenharmony_ci if (error) 6928c2ecf20Sopenharmony_ci return error; 6938c2ecf20Sopenharmony_ci 6948c2ecf20Sopenharmony_ci pb = chip->oob_poi + oobregion.offset; 6958c2ecf20Sopenharmony_ci lpc32xx_slc_ecc_copy(pb, (uint32_t *)host->ecc_buf, chip->ecc.steps); 6968c2ecf20Sopenharmony_ci 6978c2ecf20Sopenharmony_ci /* Write ECC data to device */ 6988c2ecf20Sopenharmony_ci chip->legacy.write_buf(chip, chip->oob_poi, mtd->oobsize); 6998c2ecf20Sopenharmony_ci 7008c2ecf20Sopenharmony_ci return nand_prog_page_end_op(chip); 7018c2ecf20Sopenharmony_ci} 7028c2ecf20Sopenharmony_ci 7038c2ecf20Sopenharmony_ci/* 7048c2ecf20Sopenharmony_ci * Write the data and OOB data to the device, no ECC correction with the 7058c2ecf20Sopenharmony_ci * data or OOB data 7068c2ecf20Sopenharmony_ci */ 7078c2ecf20Sopenharmony_cistatic int lpc32xx_nand_write_page_raw_syndrome(struct nand_chip *chip, 7088c2ecf20Sopenharmony_ci const uint8_t *buf, 7098c2ecf20Sopenharmony_ci int oob_required, int page) 7108c2ecf20Sopenharmony_ci{ 7118c2ecf20Sopenharmony_ci struct mtd_info *mtd = nand_to_mtd(chip); 7128c2ecf20Sopenharmony_ci 7138c2ecf20Sopenharmony_ci /* Raw writes can just use the FIFO interface */ 7148c2ecf20Sopenharmony_ci nand_prog_page_begin_op(chip, page, 0, buf, 7158c2ecf20Sopenharmony_ci chip->ecc.size * chip->ecc.steps); 7168c2ecf20Sopenharmony_ci chip->legacy.write_buf(chip, chip->oob_poi, mtd->oobsize); 7178c2ecf20Sopenharmony_ci 7188c2ecf20Sopenharmony_ci return nand_prog_page_end_op(chip); 7198c2ecf20Sopenharmony_ci} 7208c2ecf20Sopenharmony_ci 7218c2ecf20Sopenharmony_cistatic int lpc32xx_nand_dma_setup(struct lpc32xx_nand_host *host) 7228c2ecf20Sopenharmony_ci{ 7238c2ecf20Sopenharmony_ci struct mtd_info *mtd = nand_to_mtd(&host->nand_chip); 7248c2ecf20Sopenharmony_ci dma_cap_mask_t mask; 7258c2ecf20Sopenharmony_ci 7268c2ecf20Sopenharmony_ci if (!host->pdata || !host->pdata->dma_filter) { 7278c2ecf20Sopenharmony_ci dev_err(mtd->dev.parent, "no DMA platform data\n"); 7288c2ecf20Sopenharmony_ci return -ENOENT; 7298c2ecf20Sopenharmony_ci } 7308c2ecf20Sopenharmony_ci 7318c2ecf20Sopenharmony_ci dma_cap_zero(mask); 7328c2ecf20Sopenharmony_ci dma_cap_set(DMA_SLAVE, mask); 7338c2ecf20Sopenharmony_ci host->dma_chan = dma_request_channel(mask, host->pdata->dma_filter, 7348c2ecf20Sopenharmony_ci "nand-slc"); 7358c2ecf20Sopenharmony_ci if (!host->dma_chan) { 7368c2ecf20Sopenharmony_ci dev_err(mtd->dev.parent, "Failed to request DMA channel\n"); 7378c2ecf20Sopenharmony_ci return -EBUSY; 7388c2ecf20Sopenharmony_ci } 7398c2ecf20Sopenharmony_ci 7408c2ecf20Sopenharmony_ci return 0; 7418c2ecf20Sopenharmony_ci} 7428c2ecf20Sopenharmony_ci 7438c2ecf20Sopenharmony_cistatic struct lpc32xx_nand_cfg_slc *lpc32xx_parse_dt(struct device *dev) 7448c2ecf20Sopenharmony_ci{ 7458c2ecf20Sopenharmony_ci struct lpc32xx_nand_cfg_slc *ncfg; 7468c2ecf20Sopenharmony_ci struct device_node *np = dev->of_node; 7478c2ecf20Sopenharmony_ci 7488c2ecf20Sopenharmony_ci ncfg = devm_kzalloc(dev, sizeof(*ncfg), GFP_KERNEL); 7498c2ecf20Sopenharmony_ci if (!ncfg) 7508c2ecf20Sopenharmony_ci return NULL; 7518c2ecf20Sopenharmony_ci 7528c2ecf20Sopenharmony_ci of_property_read_u32(np, "nxp,wdr-clks", &ncfg->wdr_clks); 7538c2ecf20Sopenharmony_ci of_property_read_u32(np, "nxp,wwidth", &ncfg->wwidth); 7548c2ecf20Sopenharmony_ci of_property_read_u32(np, "nxp,whold", &ncfg->whold); 7558c2ecf20Sopenharmony_ci of_property_read_u32(np, "nxp,wsetup", &ncfg->wsetup); 7568c2ecf20Sopenharmony_ci of_property_read_u32(np, "nxp,rdr-clks", &ncfg->rdr_clks); 7578c2ecf20Sopenharmony_ci of_property_read_u32(np, "nxp,rwidth", &ncfg->rwidth); 7588c2ecf20Sopenharmony_ci of_property_read_u32(np, "nxp,rhold", &ncfg->rhold); 7598c2ecf20Sopenharmony_ci of_property_read_u32(np, "nxp,rsetup", &ncfg->rsetup); 7608c2ecf20Sopenharmony_ci 7618c2ecf20Sopenharmony_ci if (!ncfg->wdr_clks || !ncfg->wwidth || !ncfg->whold || 7628c2ecf20Sopenharmony_ci !ncfg->wsetup || !ncfg->rdr_clks || !ncfg->rwidth || 7638c2ecf20Sopenharmony_ci !ncfg->rhold || !ncfg->rsetup) { 7648c2ecf20Sopenharmony_ci dev_err(dev, "chip parameters not specified correctly\n"); 7658c2ecf20Sopenharmony_ci return NULL; 7668c2ecf20Sopenharmony_ci } 7678c2ecf20Sopenharmony_ci 7688c2ecf20Sopenharmony_ci ncfg->wp_gpio = of_get_named_gpio(np, "gpios", 0); 7698c2ecf20Sopenharmony_ci 7708c2ecf20Sopenharmony_ci return ncfg; 7718c2ecf20Sopenharmony_ci} 7728c2ecf20Sopenharmony_ci 7738c2ecf20Sopenharmony_cistatic int lpc32xx_nand_attach_chip(struct nand_chip *chip) 7748c2ecf20Sopenharmony_ci{ 7758c2ecf20Sopenharmony_ci struct mtd_info *mtd = nand_to_mtd(chip); 7768c2ecf20Sopenharmony_ci struct lpc32xx_nand_host *host = nand_get_controller_data(chip); 7778c2ecf20Sopenharmony_ci 7788c2ecf20Sopenharmony_ci if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST) 7798c2ecf20Sopenharmony_ci return 0; 7808c2ecf20Sopenharmony_ci 7818c2ecf20Sopenharmony_ci /* OOB and ECC CPU and DMA work areas */ 7828c2ecf20Sopenharmony_ci host->ecc_buf = (uint32_t *)(host->data_buf + LPC32XX_DMA_DATA_SIZE); 7838c2ecf20Sopenharmony_ci 7848c2ecf20Sopenharmony_ci /* 7858c2ecf20Sopenharmony_ci * Small page FLASH has a unique OOB layout, but large and huge 7868c2ecf20Sopenharmony_ci * page FLASH use the standard layout. Small page FLASH uses a 7878c2ecf20Sopenharmony_ci * custom BBT marker layout. 7888c2ecf20Sopenharmony_ci */ 7898c2ecf20Sopenharmony_ci if (mtd->writesize <= 512) 7908c2ecf20Sopenharmony_ci mtd_set_ooblayout(mtd, &lpc32xx_ooblayout_ops); 7918c2ecf20Sopenharmony_ci 7928c2ecf20Sopenharmony_ci chip->ecc.placement = NAND_ECC_PLACEMENT_INTERLEAVED; 7938c2ecf20Sopenharmony_ci /* These sizes remain the same regardless of page size */ 7948c2ecf20Sopenharmony_ci chip->ecc.size = 256; 7958c2ecf20Sopenharmony_ci chip->ecc.strength = 1; 7968c2ecf20Sopenharmony_ci chip->ecc.bytes = LPC32XX_SLC_DEV_ECC_BYTES; 7978c2ecf20Sopenharmony_ci chip->ecc.prepad = 0; 7988c2ecf20Sopenharmony_ci chip->ecc.postpad = 0; 7998c2ecf20Sopenharmony_ci chip->ecc.read_page_raw = lpc32xx_nand_read_page_raw_syndrome; 8008c2ecf20Sopenharmony_ci chip->ecc.read_page = lpc32xx_nand_read_page_syndrome; 8018c2ecf20Sopenharmony_ci chip->ecc.write_page_raw = lpc32xx_nand_write_page_raw_syndrome; 8028c2ecf20Sopenharmony_ci chip->ecc.write_page = lpc32xx_nand_write_page_syndrome; 8038c2ecf20Sopenharmony_ci chip->ecc.write_oob = lpc32xx_nand_write_oob_syndrome; 8048c2ecf20Sopenharmony_ci chip->ecc.read_oob = lpc32xx_nand_read_oob_syndrome; 8058c2ecf20Sopenharmony_ci chip->ecc.calculate = lpc32xx_nand_ecc_calculate; 8068c2ecf20Sopenharmony_ci chip->ecc.correct = nand_correct_data; 8078c2ecf20Sopenharmony_ci chip->ecc.hwctl = lpc32xx_nand_ecc_enable; 8088c2ecf20Sopenharmony_ci 8098c2ecf20Sopenharmony_ci /* 8108c2ecf20Sopenharmony_ci * Use a custom BBT marker setup for small page FLASH that 8118c2ecf20Sopenharmony_ci * won't interfere with the ECC layout. Large and huge page 8128c2ecf20Sopenharmony_ci * FLASH use the standard layout. 8138c2ecf20Sopenharmony_ci */ 8148c2ecf20Sopenharmony_ci if ((chip->bbt_options & NAND_BBT_USE_FLASH) && 8158c2ecf20Sopenharmony_ci mtd->writesize <= 512) { 8168c2ecf20Sopenharmony_ci chip->bbt_td = &bbt_smallpage_main_descr; 8178c2ecf20Sopenharmony_ci chip->bbt_md = &bbt_smallpage_mirror_descr; 8188c2ecf20Sopenharmony_ci } 8198c2ecf20Sopenharmony_ci 8208c2ecf20Sopenharmony_ci return 0; 8218c2ecf20Sopenharmony_ci} 8228c2ecf20Sopenharmony_ci 8238c2ecf20Sopenharmony_cistatic const struct nand_controller_ops lpc32xx_nand_controller_ops = { 8248c2ecf20Sopenharmony_ci .attach_chip = lpc32xx_nand_attach_chip, 8258c2ecf20Sopenharmony_ci}; 8268c2ecf20Sopenharmony_ci 8278c2ecf20Sopenharmony_ci/* 8288c2ecf20Sopenharmony_ci * Probe for NAND controller 8298c2ecf20Sopenharmony_ci */ 8308c2ecf20Sopenharmony_cistatic int lpc32xx_nand_probe(struct platform_device *pdev) 8318c2ecf20Sopenharmony_ci{ 8328c2ecf20Sopenharmony_ci struct lpc32xx_nand_host *host; 8338c2ecf20Sopenharmony_ci struct mtd_info *mtd; 8348c2ecf20Sopenharmony_ci struct nand_chip *chip; 8358c2ecf20Sopenharmony_ci struct resource *rc; 8368c2ecf20Sopenharmony_ci int res; 8378c2ecf20Sopenharmony_ci 8388c2ecf20Sopenharmony_ci /* Allocate memory for the device structure (and zero it) */ 8398c2ecf20Sopenharmony_ci host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL); 8408c2ecf20Sopenharmony_ci if (!host) 8418c2ecf20Sopenharmony_ci return -ENOMEM; 8428c2ecf20Sopenharmony_ci 8438c2ecf20Sopenharmony_ci rc = platform_get_resource(pdev, IORESOURCE_MEM, 0); 8448c2ecf20Sopenharmony_ci host->io_base = devm_ioremap_resource(&pdev->dev, rc); 8458c2ecf20Sopenharmony_ci if (IS_ERR(host->io_base)) 8468c2ecf20Sopenharmony_ci return PTR_ERR(host->io_base); 8478c2ecf20Sopenharmony_ci 8488c2ecf20Sopenharmony_ci host->io_base_dma = rc->start; 8498c2ecf20Sopenharmony_ci if (pdev->dev.of_node) 8508c2ecf20Sopenharmony_ci host->ncfg = lpc32xx_parse_dt(&pdev->dev); 8518c2ecf20Sopenharmony_ci if (!host->ncfg) { 8528c2ecf20Sopenharmony_ci dev_err(&pdev->dev, 8538c2ecf20Sopenharmony_ci "Missing or bad NAND config from device tree\n"); 8548c2ecf20Sopenharmony_ci return -ENOENT; 8558c2ecf20Sopenharmony_ci } 8568c2ecf20Sopenharmony_ci if (host->ncfg->wp_gpio == -EPROBE_DEFER) 8578c2ecf20Sopenharmony_ci return -EPROBE_DEFER; 8588c2ecf20Sopenharmony_ci if (gpio_is_valid(host->ncfg->wp_gpio) && devm_gpio_request(&pdev->dev, 8598c2ecf20Sopenharmony_ci host->ncfg->wp_gpio, "NAND WP")) { 8608c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "GPIO not available\n"); 8618c2ecf20Sopenharmony_ci return -EBUSY; 8628c2ecf20Sopenharmony_ci } 8638c2ecf20Sopenharmony_ci lpc32xx_wp_disable(host); 8648c2ecf20Sopenharmony_ci 8658c2ecf20Sopenharmony_ci host->pdata = dev_get_platdata(&pdev->dev); 8668c2ecf20Sopenharmony_ci 8678c2ecf20Sopenharmony_ci chip = &host->nand_chip; 8688c2ecf20Sopenharmony_ci mtd = nand_to_mtd(chip); 8698c2ecf20Sopenharmony_ci nand_set_controller_data(chip, host); 8708c2ecf20Sopenharmony_ci nand_set_flash_node(chip, pdev->dev.of_node); 8718c2ecf20Sopenharmony_ci mtd->owner = THIS_MODULE; 8728c2ecf20Sopenharmony_ci mtd->dev.parent = &pdev->dev; 8738c2ecf20Sopenharmony_ci 8748c2ecf20Sopenharmony_ci /* Get NAND clock */ 8758c2ecf20Sopenharmony_ci host->clk = devm_clk_get(&pdev->dev, NULL); 8768c2ecf20Sopenharmony_ci if (IS_ERR(host->clk)) { 8778c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "Clock failure\n"); 8788c2ecf20Sopenharmony_ci res = -ENOENT; 8798c2ecf20Sopenharmony_ci goto enable_wp; 8808c2ecf20Sopenharmony_ci } 8818c2ecf20Sopenharmony_ci res = clk_prepare_enable(host->clk); 8828c2ecf20Sopenharmony_ci if (res) 8838c2ecf20Sopenharmony_ci goto enable_wp; 8848c2ecf20Sopenharmony_ci 8858c2ecf20Sopenharmony_ci /* Set NAND IO addresses and command/ready functions */ 8868c2ecf20Sopenharmony_ci chip->legacy.IO_ADDR_R = SLC_DATA(host->io_base); 8878c2ecf20Sopenharmony_ci chip->legacy.IO_ADDR_W = SLC_DATA(host->io_base); 8888c2ecf20Sopenharmony_ci chip->legacy.cmd_ctrl = lpc32xx_nand_cmd_ctrl; 8898c2ecf20Sopenharmony_ci chip->legacy.dev_ready = lpc32xx_nand_device_ready; 8908c2ecf20Sopenharmony_ci chip->legacy.chip_delay = 20; /* 20us command delay time */ 8918c2ecf20Sopenharmony_ci 8928c2ecf20Sopenharmony_ci /* Init NAND controller */ 8938c2ecf20Sopenharmony_ci lpc32xx_nand_setup(host); 8948c2ecf20Sopenharmony_ci 8958c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, host); 8968c2ecf20Sopenharmony_ci 8978c2ecf20Sopenharmony_ci /* NAND callbacks for LPC32xx SLC hardware */ 8988c2ecf20Sopenharmony_ci chip->legacy.read_byte = lpc32xx_nand_read_byte; 8998c2ecf20Sopenharmony_ci chip->legacy.read_buf = lpc32xx_nand_read_buf; 9008c2ecf20Sopenharmony_ci chip->legacy.write_buf = lpc32xx_nand_write_buf; 9018c2ecf20Sopenharmony_ci 9028c2ecf20Sopenharmony_ci /* 9038c2ecf20Sopenharmony_ci * Allocate a large enough buffer for a single huge page plus 9048c2ecf20Sopenharmony_ci * extra space for the spare area and ECC storage area 9058c2ecf20Sopenharmony_ci */ 9068c2ecf20Sopenharmony_ci host->dma_buf_len = LPC32XX_DMA_DATA_SIZE + LPC32XX_ECC_SAVE_SIZE; 9078c2ecf20Sopenharmony_ci host->data_buf = devm_kzalloc(&pdev->dev, host->dma_buf_len, 9088c2ecf20Sopenharmony_ci GFP_KERNEL); 9098c2ecf20Sopenharmony_ci if (host->data_buf == NULL) { 9108c2ecf20Sopenharmony_ci res = -ENOMEM; 9118c2ecf20Sopenharmony_ci goto unprepare_clk; 9128c2ecf20Sopenharmony_ci } 9138c2ecf20Sopenharmony_ci 9148c2ecf20Sopenharmony_ci res = lpc32xx_nand_dma_setup(host); 9158c2ecf20Sopenharmony_ci if (res) { 9168c2ecf20Sopenharmony_ci res = -EIO; 9178c2ecf20Sopenharmony_ci goto unprepare_clk; 9188c2ecf20Sopenharmony_ci } 9198c2ecf20Sopenharmony_ci 9208c2ecf20Sopenharmony_ci /* Find NAND device */ 9218c2ecf20Sopenharmony_ci chip->legacy.dummy_controller.ops = &lpc32xx_nand_controller_ops; 9228c2ecf20Sopenharmony_ci res = nand_scan(chip, 1); 9238c2ecf20Sopenharmony_ci if (res) 9248c2ecf20Sopenharmony_ci goto release_dma; 9258c2ecf20Sopenharmony_ci 9268c2ecf20Sopenharmony_ci mtd->name = "nxp_lpc3220_slc"; 9278c2ecf20Sopenharmony_ci res = mtd_device_register(mtd, host->ncfg->parts, 9288c2ecf20Sopenharmony_ci host->ncfg->num_parts); 9298c2ecf20Sopenharmony_ci if (res) 9308c2ecf20Sopenharmony_ci goto cleanup_nand; 9318c2ecf20Sopenharmony_ci 9328c2ecf20Sopenharmony_ci return 0; 9338c2ecf20Sopenharmony_ci 9348c2ecf20Sopenharmony_cicleanup_nand: 9358c2ecf20Sopenharmony_ci nand_cleanup(chip); 9368c2ecf20Sopenharmony_cirelease_dma: 9378c2ecf20Sopenharmony_ci dma_release_channel(host->dma_chan); 9388c2ecf20Sopenharmony_ciunprepare_clk: 9398c2ecf20Sopenharmony_ci clk_disable_unprepare(host->clk); 9408c2ecf20Sopenharmony_cienable_wp: 9418c2ecf20Sopenharmony_ci lpc32xx_wp_enable(host); 9428c2ecf20Sopenharmony_ci 9438c2ecf20Sopenharmony_ci return res; 9448c2ecf20Sopenharmony_ci} 9458c2ecf20Sopenharmony_ci 9468c2ecf20Sopenharmony_ci/* 9478c2ecf20Sopenharmony_ci * Remove NAND device. 9488c2ecf20Sopenharmony_ci */ 9498c2ecf20Sopenharmony_cistatic int lpc32xx_nand_remove(struct platform_device *pdev) 9508c2ecf20Sopenharmony_ci{ 9518c2ecf20Sopenharmony_ci uint32_t tmp; 9528c2ecf20Sopenharmony_ci struct lpc32xx_nand_host *host = platform_get_drvdata(pdev); 9538c2ecf20Sopenharmony_ci struct nand_chip *chip = &host->nand_chip; 9548c2ecf20Sopenharmony_ci int ret; 9558c2ecf20Sopenharmony_ci 9568c2ecf20Sopenharmony_ci ret = mtd_device_unregister(nand_to_mtd(chip)); 9578c2ecf20Sopenharmony_ci WARN_ON(ret); 9588c2ecf20Sopenharmony_ci nand_cleanup(chip); 9598c2ecf20Sopenharmony_ci dma_release_channel(host->dma_chan); 9608c2ecf20Sopenharmony_ci 9618c2ecf20Sopenharmony_ci /* Force CE high */ 9628c2ecf20Sopenharmony_ci tmp = readl(SLC_CTRL(host->io_base)); 9638c2ecf20Sopenharmony_ci tmp &= ~SLCCFG_CE_LOW; 9648c2ecf20Sopenharmony_ci writel(tmp, SLC_CTRL(host->io_base)); 9658c2ecf20Sopenharmony_ci 9668c2ecf20Sopenharmony_ci clk_disable_unprepare(host->clk); 9678c2ecf20Sopenharmony_ci lpc32xx_wp_enable(host); 9688c2ecf20Sopenharmony_ci 9698c2ecf20Sopenharmony_ci return 0; 9708c2ecf20Sopenharmony_ci} 9718c2ecf20Sopenharmony_ci 9728c2ecf20Sopenharmony_ci#ifdef CONFIG_PM 9738c2ecf20Sopenharmony_cistatic int lpc32xx_nand_resume(struct platform_device *pdev) 9748c2ecf20Sopenharmony_ci{ 9758c2ecf20Sopenharmony_ci struct lpc32xx_nand_host *host = platform_get_drvdata(pdev); 9768c2ecf20Sopenharmony_ci int ret; 9778c2ecf20Sopenharmony_ci 9788c2ecf20Sopenharmony_ci /* Re-enable NAND clock */ 9798c2ecf20Sopenharmony_ci ret = clk_prepare_enable(host->clk); 9808c2ecf20Sopenharmony_ci if (ret) 9818c2ecf20Sopenharmony_ci return ret; 9828c2ecf20Sopenharmony_ci 9838c2ecf20Sopenharmony_ci /* Fresh init of NAND controller */ 9848c2ecf20Sopenharmony_ci lpc32xx_nand_setup(host); 9858c2ecf20Sopenharmony_ci 9868c2ecf20Sopenharmony_ci /* Disable write protect */ 9878c2ecf20Sopenharmony_ci lpc32xx_wp_disable(host); 9888c2ecf20Sopenharmony_ci 9898c2ecf20Sopenharmony_ci return 0; 9908c2ecf20Sopenharmony_ci} 9918c2ecf20Sopenharmony_ci 9928c2ecf20Sopenharmony_cistatic int lpc32xx_nand_suspend(struct platform_device *pdev, pm_message_t pm) 9938c2ecf20Sopenharmony_ci{ 9948c2ecf20Sopenharmony_ci uint32_t tmp; 9958c2ecf20Sopenharmony_ci struct lpc32xx_nand_host *host = platform_get_drvdata(pdev); 9968c2ecf20Sopenharmony_ci 9978c2ecf20Sopenharmony_ci /* Force CE high */ 9988c2ecf20Sopenharmony_ci tmp = readl(SLC_CTRL(host->io_base)); 9998c2ecf20Sopenharmony_ci tmp &= ~SLCCFG_CE_LOW; 10008c2ecf20Sopenharmony_ci writel(tmp, SLC_CTRL(host->io_base)); 10018c2ecf20Sopenharmony_ci 10028c2ecf20Sopenharmony_ci /* Enable write protect for safety */ 10038c2ecf20Sopenharmony_ci lpc32xx_wp_enable(host); 10048c2ecf20Sopenharmony_ci 10058c2ecf20Sopenharmony_ci /* Disable clock */ 10068c2ecf20Sopenharmony_ci clk_disable_unprepare(host->clk); 10078c2ecf20Sopenharmony_ci 10088c2ecf20Sopenharmony_ci return 0; 10098c2ecf20Sopenharmony_ci} 10108c2ecf20Sopenharmony_ci 10118c2ecf20Sopenharmony_ci#else 10128c2ecf20Sopenharmony_ci#define lpc32xx_nand_resume NULL 10138c2ecf20Sopenharmony_ci#define lpc32xx_nand_suspend NULL 10148c2ecf20Sopenharmony_ci#endif 10158c2ecf20Sopenharmony_ci 10168c2ecf20Sopenharmony_cistatic const struct of_device_id lpc32xx_nand_match[] = { 10178c2ecf20Sopenharmony_ci { .compatible = "nxp,lpc3220-slc" }, 10188c2ecf20Sopenharmony_ci { /* sentinel */ }, 10198c2ecf20Sopenharmony_ci}; 10208c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, lpc32xx_nand_match); 10218c2ecf20Sopenharmony_ci 10228c2ecf20Sopenharmony_cistatic struct platform_driver lpc32xx_nand_driver = { 10238c2ecf20Sopenharmony_ci .probe = lpc32xx_nand_probe, 10248c2ecf20Sopenharmony_ci .remove = lpc32xx_nand_remove, 10258c2ecf20Sopenharmony_ci .resume = lpc32xx_nand_resume, 10268c2ecf20Sopenharmony_ci .suspend = lpc32xx_nand_suspend, 10278c2ecf20Sopenharmony_ci .driver = { 10288c2ecf20Sopenharmony_ci .name = LPC32XX_MODNAME, 10298c2ecf20Sopenharmony_ci .of_match_table = lpc32xx_nand_match, 10308c2ecf20Sopenharmony_ci }, 10318c2ecf20Sopenharmony_ci}; 10328c2ecf20Sopenharmony_ci 10338c2ecf20Sopenharmony_cimodule_platform_driver(lpc32xx_nand_driver); 10348c2ecf20Sopenharmony_ci 10358c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 10368c2ecf20Sopenharmony_ciMODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>"); 10378c2ecf20Sopenharmony_ciMODULE_AUTHOR("Roland Stigge <stigge@antcom.de>"); 10388c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("NAND driver for the NXP LPC32XX SLC controller"); 1039