193db446aSBoris Brezillon /* 293db446aSBoris Brezillon * ST Microelectronics 393db446aSBoris Brezillon * Flexible Static Memory Controller (FSMC) 493db446aSBoris Brezillon * Driver for NAND portions 593db446aSBoris Brezillon * 693db446aSBoris Brezillon * Copyright © 2010 ST Microelectronics 793db446aSBoris Brezillon * Vipin Kumar <vipin.kumar@st.com> 893db446aSBoris Brezillon * Ashish Priyadarshi 993db446aSBoris Brezillon * 1093db446aSBoris Brezillon * Based on drivers/mtd/nand/nomadik_nand.c (removed in v3.8) 1193db446aSBoris Brezillon * Copyright © 2007 STMicroelectronics Pvt. Ltd. 1293db446aSBoris Brezillon * Copyright © 2009 Alessandro Rubini 1393db446aSBoris Brezillon * 1493db446aSBoris Brezillon * This file is licensed under the terms of the GNU General Public 1593db446aSBoris Brezillon * License version 2. This program is licensed "as is" without any 1693db446aSBoris Brezillon * warranty of any kind, whether express or implied. 1793db446aSBoris Brezillon */ 1893db446aSBoris Brezillon 1993db446aSBoris Brezillon #include <linux/clk.h> 2093db446aSBoris Brezillon #include <linux/completion.h> 2193db446aSBoris Brezillon #include <linux/dmaengine.h> 2293db446aSBoris Brezillon #include <linux/dma-direction.h> 2393db446aSBoris Brezillon #include <linux/dma-mapping.h> 2493db446aSBoris Brezillon #include <linux/err.h> 2593db446aSBoris Brezillon #include <linux/init.h> 2693db446aSBoris Brezillon #include <linux/module.h> 2793db446aSBoris Brezillon #include <linux/resource.h> 2893db446aSBoris Brezillon #include <linux/sched.h> 2993db446aSBoris Brezillon #include <linux/types.h> 3093db446aSBoris Brezillon #include <linux/mtd/mtd.h> 3193db446aSBoris Brezillon #include <linux/mtd/rawnand.h> 3293db446aSBoris Brezillon #include <linux/mtd/nand_ecc.h> 3393db446aSBoris Brezillon #include <linux/platform_device.h> 3493db446aSBoris Brezillon #include <linux/of.h> 3593db446aSBoris Brezillon #include <linux/mtd/partitions.h> 3693db446aSBoris Brezillon #include <linux/io.h> 3793db446aSBoris Brezillon #include <linux/slab.h> 3893db446aSBoris Brezillon #include <linux/amba/bus.h> 3993db446aSBoris Brezillon #include <mtd/mtd-abi.h> 4093db446aSBoris Brezillon 4193db446aSBoris Brezillon /* fsmc controller registers for NOR flash */ 4293db446aSBoris Brezillon #define CTRL 0x0 4393db446aSBoris Brezillon /* ctrl register definitions */ 4493db446aSBoris Brezillon #define BANK_ENABLE (1 << 0) 4593db446aSBoris Brezillon #define MUXED (1 << 1) 4693db446aSBoris Brezillon #define NOR_DEV (2 << 2) 4793db446aSBoris Brezillon #define WIDTH_8 (0 << 4) 4893db446aSBoris Brezillon #define WIDTH_16 (1 << 4) 4993db446aSBoris Brezillon #define RSTPWRDWN (1 << 6) 5093db446aSBoris Brezillon #define WPROT (1 << 7) 5193db446aSBoris Brezillon #define WRT_ENABLE (1 << 12) 5293db446aSBoris Brezillon #define WAIT_ENB (1 << 13) 5393db446aSBoris Brezillon 5493db446aSBoris Brezillon #define CTRL_TIM 0x4 5593db446aSBoris Brezillon /* ctrl_tim register definitions */ 5693db446aSBoris Brezillon 5793db446aSBoris Brezillon #define FSMC_NOR_BANK_SZ 0x8 5893db446aSBoris Brezillon #define FSMC_NOR_REG_SIZE 0x40 5993db446aSBoris Brezillon 6093db446aSBoris Brezillon #define FSMC_NOR_REG(base, bank, reg) (base + \ 6193db446aSBoris Brezillon FSMC_NOR_BANK_SZ * (bank) + \ 6293db446aSBoris Brezillon reg) 6393db446aSBoris Brezillon 6493db446aSBoris Brezillon /* fsmc controller registers for NAND flash */ 658f3931edSBoris Brezillon #define FSMC_PC 0x00 6693db446aSBoris Brezillon /* pc register definitions */ 6793db446aSBoris Brezillon #define FSMC_RESET (1 << 0) 6893db446aSBoris Brezillon #define FSMC_WAITON (1 << 1) 6993db446aSBoris Brezillon #define FSMC_ENABLE (1 << 2) 7093db446aSBoris Brezillon #define FSMC_DEVTYPE_NAND (1 << 3) 7193db446aSBoris Brezillon #define FSMC_DEVWID_8 (0 << 4) 7293db446aSBoris Brezillon #define FSMC_DEVWID_16 (1 << 4) 7393db446aSBoris Brezillon #define FSMC_ECCEN (1 << 6) 7493db446aSBoris Brezillon #define FSMC_ECCPLEN_512 (0 << 7) 7593db446aSBoris Brezillon #define FSMC_ECCPLEN_256 (1 << 7) 7693db446aSBoris Brezillon #define FSMC_TCLR_1 (1) 7793db446aSBoris Brezillon #define FSMC_TCLR_SHIFT (9) 7893db446aSBoris Brezillon #define FSMC_TCLR_MASK (0xF) 7993db446aSBoris Brezillon #define FSMC_TAR_1 (1) 8093db446aSBoris Brezillon #define FSMC_TAR_SHIFT (13) 8193db446aSBoris Brezillon #define FSMC_TAR_MASK (0xF) 8293db446aSBoris Brezillon #define STS 0x04 8393db446aSBoris Brezillon /* sts register definitions */ 8493db446aSBoris Brezillon #define FSMC_CODE_RDY (1 << 15) 8593db446aSBoris Brezillon #define COMM 0x08 8693db446aSBoris Brezillon /* comm register definitions */ 8793db446aSBoris Brezillon #define FSMC_TSET_0 0 8893db446aSBoris Brezillon #define FSMC_TSET_SHIFT 0 8993db446aSBoris Brezillon #define FSMC_TSET_MASK 0xFF 9093db446aSBoris Brezillon #define FSMC_TWAIT_6 6 9193db446aSBoris Brezillon #define FSMC_TWAIT_SHIFT 8 9293db446aSBoris Brezillon #define FSMC_TWAIT_MASK 0xFF 9393db446aSBoris Brezillon #define FSMC_THOLD_4 4 9493db446aSBoris Brezillon #define FSMC_THOLD_SHIFT 16 9593db446aSBoris Brezillon #define FSMC_THOLD_MASK 0xFF 9693db446aSBoris Brezillon #define FSMC_THIZ_1 1 9793db446aSBoris Brezillon #define FSMC_THIZ_SHIFT 24 9893db446aSBoris Brezillon #define FSMC_THIZ_MASK 0xFF 9993db446aSBoris Brezillon #define ATTRIB 0x0C 10093db446aSBoris Brezillon #define IOATA 0x10 10193db446aSBoris Brezillon #define ECC1 0x14 10293db446aSBoris Brezillon #define ECC2 0x18 10393db446aSBoris Brezillon #define ECC3 0x1C 10493db446aSBoris Brezillon #define FSMC_NAND_BANK_SZ 0x20 10593db446aSBoris Brezillon 10693db446aSBoris Brezillon #define FSMC_BUSY_WAIT_TIMEOUT (1 * HZ) 10793db446aSBoris Brezillon 10893db446aSBoris Brezillon struct fsmc_nand_timings { 10993db446aSBoris Brezillon uint8_t tclr; 11093db446aSBoris Brezillon uint8_t tar; 11193db446aSBoris Brezillon uint8_t thiz; 11293db446aSBoris Brezillon uint8_t thold; 11393db446aSBoris Brezillon uint8_t twait; 11493db446aSBoris Brezillon uint8_t tset; 11593db446aSBoris Brezillon }; 11693db446aSBoris Brezillon 11793db446aSBoris Brezillon enum access_mode { 11893db446aSBoris Brezillon USE_DMA_ACCESS = 1, 11993db446aSBoris Brezillon USE_WORD_ACCESS, 12093db446aSBoris Brezillon }; 12193db446aSBoris Brezillon 12293db446aSBoris Brezillon /** 12393db446aSBoris Brezillon * struct fsmc_nand_data - structure for FSMC NAND device state 12493db446aSBoris Brezillon * 12593db446aSBoris Brezillon * @pid: Part ID on the AMBA PrimeCell format 12693db446aSBoris Brezillon * @mtd: MTD info for a NAND flash. 12793db446aSBoris Brezillon * @nand: Chip related info for a NAND flash. 12893db446aSBoris Brezillon * @partitions: Partition info for a NAND Flash. 12993db446aSBoris Brezillon * @nr_partitions: Total number of partition of a NAND flash. 13093db446aSBoris Brezillon * 13193db446aSBoris Brezillon * @bank: Bank number for probed device. 13293db446aSBoris Brezillon * @clk: Clock structure for FSMC. 13393db446aSBoris Brezillon * 13493db446aSBoris Brezillon * @read_dma_chan: DMA channel for read access 13593db446aSBoris Brezillon * @write_dma_chan: DMA channel for write access to NAND 13693db446aSBoris Brezillon * @dma_access_complete: Completion structure 13793db446aSBoris Brezillon * 13893db446aSBoris Brezillon * @data_pa: NAND Physical port for Data. 13993db446aSBoris Brezillon * @data_va: NAND port for Data. 14093db446aSBoris Brezillon * @cmd_va: NAND port for Command. 14193db446aSBoris Brezillon * @addr_va: NAND port for Address. 1424df6ed4fSMiquel Raynal * @regs_va: Registers base address for a given bank. 14393db446aSBoris Brezillon */ 14493db446aSBoris Brezillon struct fsmc_nand_data { 14593db446aSBoris Brezillon u32 pid; 14693db446aSBoris Brezillon struct nand_chip nand; 14793db446aSBoris Brezillon 14893db446aSBoris Brezillon unsigned int bank; 14993db446aSBoris Brezillon struct device *dev; 15093db446aSBoris Brezillon enum access_mode mode; 15193db446aSBoris Brezillon struct clk *clk; 15293db446aSBoris Brezillon 15393db446aSBoris Brezillon /* DMA related objects */ 15493db446aSBoris Brezillon struct dma_chan *read_dma_chan; 15593db446aSBoris Brezillon struct dma_chan *write_dma_chan; 15693db446aSBoris Brezillon struct completion dma_access_complete; 15793db446aSBoris Brezillon 15893db446aSBoris Brezillon struct fsmc_nand_timings *dev_timings; 15993db446aSBoris Brezillon 16093db446aSBoris Brezillon dma_addr_t data_pa; 16193db446aSBoris Brezillon void __iomem *data_va; 16293db446aSBoris Brezillon void __iomem *cmd_va; 16393db446aSBoris Brezillon void __iomem *addr_va; 16493db446aSBoris Brezillon void __iomem *regs_va; 16593db446aSBoris Brezillon }; 16693db446aSBoris Brezillon 16793db446aSBoris Brezillon static int fsmc_ecc1_ooblayout_ecc(struct mtd_info *mtd, int section, 16893db446aSBoris Brezillon struct mtd_oob_region *oobregion) 16993db446aSBoris Brezillon { 17093db446aSBoris Brezillon struct nand_chip *chip = mtd_to_nand(mtd); 17193db446aSBoris Brezillon 17293db446aSBoris Brezillon if (section >= chip->ecc.steps) 17393db446aSBoris Brezillon return -ERANGE; 17493db446aSBoris Brezillon 17593db446aSBoris Brezillon oobregion->offset = (section * 16) + 2; 17693db446aSBoris Brezillon oobregion->length = 3; 17793db446aSBoris Brezillon 17893db446aSBoris Brezillon return 0; 17993db446aSBoris Brezillon } 18093db446aSBoris Brezillon 18193db446aSBoris Brezillon static int fsmc_ecc1_ooblayout_free(struct mtd_info *mtd, int section, 18293db446aSBoris Brezillon struct mtd_oob_region *oobregion) 18393db446aSBoris Brezillon { 18493db446aSBoris Brezillon struct nand_chip *chip = mtd_to_nand(mtd); 18593db446aSBoris Brezillon 18693db446aSBoris Brezillon if (section >= chip->ecc.steps) 18793db446aSBoris Brezillon return -ERANGE; 18893db446aSBoris Brezillon 18993db446aSBoris Brezillon oobregion->offset = (section * 16) + 8; 19093db446aSBoris Brezillon 19193db446aSBoris Brezillon if (section < chip->ecc.steps - 1) 19293db446aSBoris Brezillon oobregion->length = 8; 19393db446aSBoris Brezillon else 19493db446aSBoris Brezillon oobregion->length = mtd->oobsize - oobregion->offset; 19593db446aSBoris Brezillon 19693db446aSBoris Brezillon return 0; 19793db446aSBoris Brezillon } 19893db446aSBoris Brezillon 19993db446aSBoris Brezillon static const struct mtd_ooblayout_ops fsmc_ecc1_ooblayout_ops = { 20093db446aSBoris Brezillon .ecc = fsmc_ecc1_ooblayout_ecc, 20193db446aSBoris Brezillon .free = fsmc_ecc1_ooblayout_free, 20293db446aSBoris Brezillon }; 20393db446aSBoris Brezillon 20493db446aSBoris Brezillon /* 20593db446aSBoris Brezillon * ECC placement definitions in oobfree type format. 20693db446aSBoris Brezillon * There are 13 bytes of ecc for every 512 byte block and it has to be read 20793db446aSBoris Brezillon * consecutively and immediately after the 512 byte data block for hardware to 20893db446aSBoris Brezillon * generate the error bit offsets in 512 byte data. 20993db446aSBoris Brezillon */ 21093db446aSBoris Brezillon static int fsmc_ecc4_ooblayout_ecc(struct mtd_info *mtd, int section, 21193db446aSBoris Brezillon struct mtd_oob_region *oobregion) 21293db446aSBoris Brezillon { 21393db446aSBoris Brezillon struct nand_chip *chip = mtd_to_nand(mtd); 21493db446aSBoris Brezillon 21593db446aSBoris Brezillon if (section >= chip->ecc.steps) 21693db446aSBoris Brezillon return -ERANGE; 21793db446aSBoris Brezillon 21893db446aSBoris Brezillon oobregion->length = chip->ecc.bytes; 21993db446aSBoris Brezillon 22093db446aSBoris Brezillon if (!section && mtd->writesize <= 512) 22193db446aSBoris Brezillon oobregion->offset = 0; 22293db446aSBoris Brezillon else 22393db446aSBoris Brezillon oobregion->offset = (section * 16) + 2; 22493db446aSBoris Brezillon 22593db446aSBoris Brezillon return 0; 22693db446aSBoris Brezillon } 22793db446aSBoris Brezillon 22893db446aSBoris Brezillon static int fsmc_ecc4_ooblayout_free(struct mtd_info *mtd, int section, 22993db446aSBoris Brezillon struct mtd_oob_region *oobregion) 23093db446aSBoris Brezillon { 23193db446aSBoris Brezillon struct nand_chip *chip = mtd_to_nand(mtd); 23293db446aSBoris Brezillon 23393db446aSBoris Brezillon if (section >= chip->ecc.steps) 23493db446aSBoris Brezillon return -ERANGE; 23593db446aSBoris Brezillon 23693db446aSBoris Brezillon oobregion->offset = (section * 16) + 15; 23793db446aSBoris Brezillon 23893db446aSBoris Brezillon if (section < chip->ecc.steps - 1) 23993db446aSBoris Brezillon oobregion->length = 3; 24093db446aSBoris Brezillon else 24193db446aSBoris Brezillon oobregion->length = mtd->oobsize - oobregion->offset; 24293db446aSBoris Brezillon 24393db446aSBoris Brezillon return 0; 24493db446aSBoris Brezillon } 24593db446aSBoris Brezillon 24693db446aSBoris Brezillon static const struct mtd_ooblayout_ops fsmc_ecc4_ooblayout_ops = { 24793db446aSBoris Brezillon .ecc = fsmc_ecc4_ooblayout_ecc, 24893db446aSBoris Brezillon .free = fsmc_ecc4_ooblayout_free, 24993db446aSBoris Brezillon }; 25093db446aSBoris Brezillon 25193db446aSBoris Brezillon static inline struct fsmc_nand_data *mtd_to_fsmc(struct mtd_info *mtd) 25293db446aSBoris Brezillon { 25393db446aSBoris Brezillon return container_of(mtd_to_nand(mtd), struct fsmc_nand_data, nand); 25493db446aSBoris Brezillon } 25593db446aSBoris Brezillon 25693db446aSBoris Brezillon /* 25793db446aSBoris Brezillon * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine 25893db446aSBoris Brezillon * 25993db446aSBoris Brezillon * This routine initializes timing parameters related to NAND memory access in 26093db446aSBoris Brezillon * FSMC registers 26193db446aSBoris Brezillon */ 26293db446aSBoris Brezillon static void fsmc_nand_setup(struct fsmc_nand_data *host, 26393db446aSBoris Brezillon struct fsmc_nand_timings *tims) 26493db446aSBoris Brezillon { 26593db446aSBoris Brezillon uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON; 26693db446aSBoris Brezillon uint32_t tclr, tar, thiz, thold, twait, tset; 26793db446aSBoris Brezillon 26893db446aSBoris Brezillon tclr = (tims->tclr & FSMC_TCLR_MASK) << FSMC_TCLR_SHIFT; 26993db446aSBoris Brezillon tar = (tims->tar & FSMC_TAR_MASK) << FSMC_TAR_SHIFT; 27093db446aSBoris Brezillon thiz = (tims->thiz & FSMC_THIZ_MASK) << FSMC_THIZ_SHIFT; 27193db446aSBoris Brezillon thold = (tims->thold & FSMC_THOLD_MASK) << FSMC_THOLD_SHIFT; 27293db446aSBoris Brezillon twait = (tims->twait & FSMC_TWAIT_MASK) << FSMC_TWAIT_SHIFT; 27393db446aSBoris Brezillon tset = (tims->tset & FSMC_TSET_MASK) << FSMC_TSET_SHIFT; 27493db446aSBoris Brezillon 27593db446aSBoris Brezillon if (host->nand.options & NAND_BUSWIDTH_16) 2768f3931edSBoris Brezillon writel_relaxed(value | FSMC_DEVWID_16, 2778f3931edSBoris Brezillon host->regs_va + FSMC_PC); 27893db446aSBoris Brezillon else 2798f3931edSBoris Brezillon writel_relaxed(value | FSMC_DEVWID_8, host->regs_va + FSMC_PC); 28093db446aSBoris Brezillon 2818f3931edSBoris Brezillon writel_relaxed(readl(host->regs_va + FSMC_PC) | tclr | tar, 2828f3931edSBoris Brezillon host->regs_va + FSMC_PC); 2834df6ed4fSMiquel Raynal writel_relaxed(thiz | thold | twait | tset, host->regs_va + COMM); 2844df6ed4fSMiquel Raynal writel_relaxed(thiz | thold | twait | tset, host->regs_va + ATTRIB); 28593db446aSBoris Brezillon } 28693db446aSBoris Brezillon 28793db446aSBoris Brezillon static int fsmc_calc_timings(struct fsmc_nand_data *host, 28893db446aSBoris Brezillon const struct nand_sdr_timings *sdrt, 28993db446aSBoris Brezillon struct fsmc_nand_timings *tims) 29093db446aSBoris Brezillon { 29193db446aSBoris Brezillon unsigned long hclk = clk_get_rate(host->clk); 29293db446aSBoris Brezillon unsigned long hclkn = NSEC_PER_SEC / hclk; 29393db446aSBoris Brezillon uint32_t thiz, thold, twait, tset; 29493db446aSBoris Brezillon 29593db446aSBoris Brezillon if (sdrt->tRC_min < 30000) 29693db446aSBoris Brezillon return -EOPNOTSUPP; 29793db446aSBoris Brezillon 29893db446aSBoris Brezillon tims->tar = DIV_ROUND_UP(sdrt->tAR_min / 1000, hclkn) - 1; 29993db446aSBoris Brezillon if (tims->tar > FSMC_TAR_MASK) 30093db446aSBoris Brezillon tims->tar = FSMC_TAR_MASK; 30193db446aSBoris Brezillon tims->tclr = DIV_ROUND_UP(sdrt->tCLR_min / 1000, hclkn) - 1; 30293db446aSBoris Brezillon if (tims->tclr > FSMC_TCLR_MASK) 30393db446aSBoris Brezillon tims->tclr = FSMC_TCLR_MASK; 30493db446aSBoris Brezillon 30593db446aSBoris Brezillon thiz = sdrt->tCS_min - sdrt->tWP_min; 30693db446aSBoris Brezillon tims->thiz = DIV_ROUND_UP(thiz / 1000, hclkn); 30793db446aSBoris Brezillon 30893db446aSBoris Brezillon thold = sdrt->tDH_min; 30993db446aSBoris Brezillon if (thold < sdrt->tCH_min) 31093db446aSBoris Brezillon thold = sdrt->tCH_min; 31193db446aSBoris Brezillon if (thold < sdrt->tCLH_min) 31293db446aSBoris Brezillon thold = sdrt->tCLH_min; 31393db446aSBoris Brezillon if (thold < sdrt->tWH_min) 31493db446aSBoris Brezillon thold = sdrt->tWH_min; 31593db446aSBoris Brezillon if (thold < sdrt->tALH_min) 31693db446aSBoris Brezillon thold = sdrt->tALH_min; 31793db446aSBoris Brezillon if (thold < sdrt->tREH_min) 31893db446aSBoris Brezillon thold = sdrt->tREH_min; 31993db446aSBoris Brezillon tims->thold = DIV_ROUND_UP(thold / 1000, hclkn); 32093db446aSBoris Brezillon if (tims->thold == 0) 32193db446aSBoris Brezillon tims->thold = 1; 32293db446aSBoris Brezillon else if (tims->thold > FSMC_THOLD_MASK) 32393db446aSBoris Brezillon tims->thold = FSMC_THOLD_MASK; 32493db446aSBoris Brezillon 32593db446aSBoris Brezillon twait = max(sdrt->tRP_min, sdrt->tWP_min); 32693db446aSBoris Brezillon tims->twait = DIV_ROUND_UP(twait / 1000, hclkn) - 1; 32793db446aSBoris Brezillon if (tims->twait == 0) 32893db446aSBoris Brezillon tims->twait = 1; 32993db446aSBoris Brezillon else if (tims->twait > FSMC_TWAIT_MASK) 33093db446aSBoris Brezillon tims->twait = FSMC_TWAIT_MASK; 33193db446aSBoris Brezillon 33293db446aSBoris Brezillon tset = max(sdrt->tCS_min - sdrt->tWP_min, 33393db446aSBoris Brezillon sdrt->tCEA_max - sdrt->tREA_max); 33493db446aSBoris Brezillon tims->tset = DIV_ROUND_UP(tset / 1000, hclkn) - 1; 33593db446aSBoris Brezillon if (tims->tset == 0) 33693db446aSBoris Brezillon tims->tset = 1; 33793db446aSBoris Brezillon else if (tims->tset > FSMC_TSET_MASK) 33893db446aSBoris Brezillon tims->tset = FSMC_TSET_MASK; 33993db446aSBoris Brezillon 34093db446aSBoris Brezillon return 0; 34193db446aSBoris Brezillon } 34293db446aSBoris Brezillon 34393db446aSBoris Brezillon static int fsmc_setup_data_interface(struct mtd_info *mtd, int csline, 34493db446aSBoris Brezillon const struct nand_data_interface *conf) 34593db446aSBoris Brezillon { 34693db446aSBoris Brezillon struct nand_chip *nand = mtd_to_nand(mtd); 34793db446aSBoris Brezillon struct fsmc_nand_data *host = nand_get_controller_data(nand); 34893db446aSBoris Brezillon struct fsmc_nand_timings tims; 34993db446aSBoris Brezillon const struct nand_sdr_timings *sdrt; 35093db446aSBoris Brezillon int ret; 35193db446aSBoris Brezillon 35293db446aSBoris Brezillon sdrt = nand_get_sdr_timings(conf); 35393db446aSBoris Brezillon if (IS_ERR(sdrt)) 35493db446aSBoris Brezillon return PTR_ERR(sdrt); 35593db446aSBoris Brezillon 35693db446aSBoris Brezillon ret = fsmc_calc_timings(host, sdrt, &tims); 35793db446aSBoris Brezillon if (ret) 35893db446aSBoris Brezillon return ret; 35993db446aSBoris Brezillon 36093db446aSBoris Brezillon if (csline == NAND_DATA_IFACE_CHECK_ONLY) 36193db446aSBoris Brezillon return 0; 36293db446aSBoris Brezillon 36393db446aSBoris Brezillon fsmc_nand_setup(host, &tims); 36493db446aSBoris Brezillon 36593db446aSBoris Brezillon return 0; 36693db446aSBoris Brezillon } 36793db446aSBoris Brezillon 36893db446aSBoris Brezillon /* 36993db446aSBoris Brezillon * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers 37093db446aSBoris Brezillon */ 371ec47636cSBoris Brezillon static void fsmc_enable_hwecc(struct nand_chip *chip, int mode) 37293db446aSBoris Brezillon { 373ec47636cSBoris Brezillon struct fsmc_nand_data *host = mtd_to_fsmc(nand_to_mtd(chip)); 37493db446aSBoris Brezillon 3758f3931edSBoris Brezillon writel_relaxed(readl(host->regs_va + FSMC_PC) & ~FSMC_ECCPLEN_256, 3768f3931edSBoris Brezillon host->regs_va + FSMC_PC); 3778f3931edSBoris Brezillon writel_relaxed(readl(host->regs_va + FSMC_PC) & ~FSMC_ECCEN, 3788f3931edSBoris Brezillon host->regs_va + FSMC_PC); 3798f3931edSBoris Brezillon writel_relaxed(readl(host->regs_va + FSMC_PC) | FSMC_ECCEN, 3808f3931edSBoris Brezillon host->regs_va + FSMC_PC); 38193db446aSBoris Brezillon } 38293db446aSBoris Brezillon 38393db446aSBoris Brezillon /* 38493db446aSBoris Brezillon * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by 38593db446aSBoris Brezillon * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to 38693db446aSBoris Brezillon * max of 8-bits) 38793db446aSBoris Brezillon */ 388af37d2c3SBoris Brezillon static int fsmc_read_hwecc_ecc4(struct nand_chip *chip, const uint8_t *data, 38993db446aSBoris Brezillon uint8_t *ecc) 39093db446aSBoris Brezillon { 391af37d2c3SBoris Brezillon struct fsmc_nand_data *host = mtd_to_fsmc(nand_to_mtd(chip)); 39293db446aSBoris Brezillon uint32_t ecc_tmp; 39393db446aSBoris Brezillon unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT; 39493db446aSBoris Brezillon 39593db446aSBoris Brezillon do { 3964df6ed4fSMiquel Raynal if (readl_relaxed(host->regs_va + STS) & FSMC_CODE_RDY) 39793db446aSBoris Brezillon break; 39893db446aSBoris Brezillon else 39993db446aSBoris Brezillon cond_resched(); 40093db446aSBoris Brezillon } while (!time_after_eq(jiffies, deadline)); 40193db446aSBoris Brezillon 40293db446aSBoris Brezillon if (time_after_eq(jiffies, deadline)) { 40393db446aSBoris Brezillon dev_err(host->dev, "calculate ecc timed out\n"); 40493db446aSBoris Brezillon return -ETIMEDOUT; 40593db446aSBoris Brezillon } 40693db446aSBoris Brezillon 4074df6ed4fSMiquel Raynal ecc_tmp = readl_relaxed(host->regs_va + ECC1); 40893db446aSBoris Brezillon ecc[0] = (uint8_t) (ecc_tmp >> 0); 40993db446aSBoris Brezillon ecc[1] = (uint8_t) (ecc_tmp >> 8); 41093db446aSBoris Brezillon ecc[2] = (uint8_t) (ecc_tmp >> 16); 41193db446aSBoris Brezillon ecc[3] = (uint8_t) (ecc_tmp >> 24); 41293db446aSBoris Brezillon 4134df6ed4fSMiquel Raynal ecc_tmp = readl_relaxed(host->regs_va + ECC2); 41493db446aSBoris Brezillon ecc[4] = (uint8_t) (ecc_tmp >> 0); 41593db446aSBoris Brezillon ecc[5] = (uint8_t) (ecc_tmp >> 8); 41693db446aSBoris Brezillon ecc[6] = (uint8_t) (ecc_tmp >> 16); 41793db446aSBoris Brezillon ecc[7] = (uint8_t) (ecc_tmp >> 24); 41893db446aSBoris Brezillon 4194df6ed4fSMiquel Raynal ecc_tmp = readl_relaxed(host->regs_va + ECC3); 42093db446aSBoris Brezillon ecc[8] = (uint8_t) (ecc_tmp >> 0); 42193db446aSBoris Brezillon ecc[9] = (uint8_t) (ecc_tmp >> 8); 42293db446aSBoris Brezillon ecc[10] = (uint8_t) (ecc_tmp >> 16); 42393db446aSBoris Brezillon ecc[11] = (uint8_t) (ecc_tmp >> 24); 42493db446aSBoris Brezillon 4254df6ed4fSMiquel Raynal ecc_tmp = readl_relaxed(host->regs_va + STS); 42693db446aSBoris Brezillon ecc[12] = (uint8_t) (ecc_tmp >> 16); 42793db446aSBoris Brezillon 42893db446aSBoris Brezillon return 0; 42993db446aSBoris Brezillon } 43093db446aSBoris Brezillon 43193db446aSBoris Brezillon /* 43293db446aSBoris Brezillon * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by 43393db446aSBoris Brezillon * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to 43493db446aSBoris Brezillon * max of 1-bit) 43593db446aSBoris Brezillon */ 436af37d2c3SBoris Brezillon static int fsmc_read_hwecc_ecc1(struct nand_chip *chip, const uint8_t *data, 43793db446aSBoris Brezillon uint8_t *ecc) 43893db446aSBoris Brezillon { 439af37d2c3SBoris Brezillon struct fsmc_nand_data *host = mtd_to_fsmc(nand_to_mtd(chip)); 44093db446aSBoris Brezillon uint32_t ecc_tmp; 44193db446aSBoris Brezillon 4424df6ed4fSMiquel Raynal ecc_tmp = readl_relaxed(host->regs_va + ECC1); 44393db446aSBoris Brezillon ecc[0] = (uint8_t) (ecc_tmp >> 0); 44493db446aSBoris Brezillon ecc[1] = (uint8_t) (ecc_tmp >> 8); 44593db446aSBoris Brezillon ecc[2] = (uint8_t) (ecc_tmp >> 16); 44693db446aSBoris Brezillon 44793db446aSBoris Brezillon return 0; 44893db446aSBoris Brezillon } 44993db446aSBoris Brezillon 45093db446aSBoris Brezillon /* Count the number of 0's in buff upto a max of max_bits */ 45193db446aSBoris Brezillon static int count_written_bits(uint8_t *buff, int size, int max_bits) 45293db446aSBoris Brezillon { 45393db446aSBoris Brezillon int k, written_bits = 0; 45493db446aSBoris Brezillon 45593db446aSBoris Brezillon for (k = 0; k < size; k++) { 45693db446aSBoris Brezillon written_bits += hweight8(~buff[k]); 45793db446aSBoris Brezillon if (written_bits > max_bits) 45893db446aSBoris Brezillon break; 45993db446aSBoris Brezillon } 46093db446aSBoris Brezillon 46193db446aSBoris Brezillon return written_bits; 46293db446aSBoris Brezillon } 46393db446aSBoris Brezillon 46493db446aSBoris Brezillon static void dma_complete(void *param) 46593db446aSBoris Brezillon { 46693db446aSBoris Brezillon struct fsmc_nand_data *host = param; 46793db446aSBoris Brezillon 46893db446aSBoris Brezillon complete(&host->dma_access_complete); 46993db446aSBoris Brezillon } 47093db446aSBoris Brezillon 47193db446aSBoris Brezillon static int dma_xfer(struct fsmc_nand_data *host, void *buffer, int len, 47293db446aSBoris Brezillon enum dma_data_direction direction) 47393db446aSBoris Brezillon { 47493db446aSBoris Brezillon struct dma_chan *chan; 47593db446aSBoris Brezillon struct dma_device *dma_dev; 47693db446aSBoris Brezillon struct dma_async_tx_descriptor *tx; 47793db446aSBoris Brezillon dma_addr_t dma_dst, dma_src, dma_addr; 47893db446aSBoris Brezillon dma_cookie_t cookie; 47993db446aSBoris Brezillon unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT; 48093db446aSBoris Brezillon int ret; 48193db446aSBoris Brezillon unsigned long time_left; 48293db446aSBoris Brezillon 48393db446aSBoris Brezillon if (direction == DMA_TO_DEVICE) 48493db446aSBoris Brezillon chan = host->write_dma_chan; 48593db446aSBoris Brezillon else if (direction == DMA_FROM_DEVICE) 48693db446aSBoris Brezillon chan = host->read_dma_chan; 48793db446aSBoris Brezillon else 48893db446aSBoris Brezillon return -EINVAL; 48993db446aSBoris Brezillon 49093db446aSBoris Brezillon dma_dev = chan->device; 49193db446aSBoris Brezillon dma_addr = dma_map_single(dma_dev->dev, buffer, len, direction); 49293db446aSBoris Brezillon 49393db446aSBoris Brezillon if (direction == DMA_TO_DEVICE) { 49493db446aSBoris Brezillon dma_src = dma_addr; 49593db446aSBoris Brezillon dma_dst = host->data_pa; 49693db446aSBoris Brezillon } else { 49793db446aSBoris Brezillon dma_src = host->data_pa; 49893db446aSBoris Brezillon dma_dst = dma_addr; 49993db446aSBoris Brezillon } 50093db446aSBoris Brezillon 50193db446aSBoris Brezillon tx = dma_dev->device_prep_dma_memcpy(chan, dma_dst, dma_src, 50293db446aSBoris Brezillon len, flags); 50393db446aSBoris Brezillon if (!tx) { 50493db446aSBoris Brezillon dev_err(host->dev, "device_prep_dma_memcpy error\n"); 50593db446aSBoris Brezillon ret = -EIO; 50693db446aSBoris Brezillon goto unmap_dma; 50793db446aSBoris Brezillon } 50893db446aSBoris Brezillon 50993db446aSBoris Brezillon tx->callback = dma_complete; 51093db446aSBoris Brezillon tx->callback_param = host; 51193db446aSBoris Brezillon cookie = tx->tx_submit(tx); 51293db446aSBoris Brezillon 51393db446aSBoris Brezillon ret = dma_submit_error(cookie); 51493db446aSBoris Brezillon if (ret) { 51593db446aSBoris Brezillon dev_err(host->dev, "dma_submit_error %d\n", cookie); 51693db446aSBoris Brezillon goto unmap_dma; 51793db446aSBoris Brezillon } 51893db446aSBoris Brezillon 51993db446aSBoris Brezillon dma_async_issue_pending(chan); 52093db446aSBoris Brezillon 52193db446aSBoris Brezillon time_left = 52293db446aSBoris Brezillon wait_for_completion_timeout(&host->dma_access_complete, 52393db446aSBoris Brezillon msecs_to_jiffies(3000)); 52493db446aSBoris Brezillon if (time_left == 0) { 52593db446aSBoris Brezillon dmaengine_terminate_all(chan); 52693db446aSBoris Brezillon dev_err(host->dev, "wait_for_completion_timeout\n"); 52793db446aSBoris Brezillon ret = -ETIMEDOUT; 52893db446aSBoris Brezillon goto unmap_dma; 52993db446aSBoris Brezillon } 53093db446aSBoris Brezillon 53193db446aSBoris Brezillon ret = 0; 53293db446aSBoris Brezillon 53393db446aSBoris Brezillon unmap_dma: 53493db446aSBoris Brezillon dma_unmap_single(dma_dev->dev, dma_addr, len, direction); 53593db446aSBoris Brezillon 53693db446aSBoris Brezillon return ret; 53793db446aSBoris Brezillon } 53893db446aSBoris Brezillon 53993db446aSBoris Brezillon /* 54093db446aSBoris Brezillon * fsmc_write_buf - write buffer to chip 54193db446aSBoris Brezillon * @mtd: MTD device structure 54293db446aSBoris Brezillon * @buf: data buffer 54393db446aSBoris Brezillon * @len: number of bytes to write 54493db446aSBoris Brezillon */ 54593db446aSBoris Brezillon static void fsmc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len) 54693db446aSBoris Brezillon { 5474df6ed4fSMiquel Raynal struct fsmc_nand_data *host = mtd_to_fsmc(mtd); 54893db446aSBoris Brezillon int i; 54993db446aSBoris Brezillon 550f55824c6SBoris Brezillon if (IS_ALIGNED((uintptr_t)buf, sizeof(uint32_t)) && 55193db446aSBoris Brezillon IS_ALIGNED(len, sizeof(uint32_t))) { 55293db446aSBoris Brezillon uint32_t *p = (uint32_t *)buf; 55393db446aSBoris Brezillon len = len >> 2; 55493db446aSBoris Brezillon for (i = 0; i < len; i++) 5554df6ed4fSMiquel Raynal writel_relaxed(p[i], host->data_va); 55693db446aSBoris Brezillon } else { 55793db446aSBoris Brezillon for (i = 0; i < len; i++) 5584df6ed4fSMiquel Raynal writeb_relaxed(buf[i], host->data_va); 55993db446aSBoris Brezillon } 56093db446aSBoris Brezillon } 56193db446aSBoris Brezillon 56293db446aSBoris Brezillon /* 56393db446aSBoris Brezillon * fsmc_read_buf - read chip data into buffer 56493db446aSBoris Brezillon * @mtd: MTD device structure 56593db446aSBoris Brezillon * @buf: buffer to store date 56693db446aSBoris Brezillon * @len: number of bytes to read 56793db446aSBoris Brezillon */ 56893db446aSBoris Brezillon static void fsmc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) 56993db446aSBoris Brezillon { 5704df6ed4fSMiquel Raynal struct fsmc_nand_data *host = mtd_to_fsmc(mtd); 57193db446aSBoris Brezillon int i; 57293db446aSBoris Brezillon 573f55824c6SBoris Brezillon if (IS_ALIGNED((uintptr_t)buf, sizeof(uint32_t)) && 57493db446aSBoris Brezillon IS_ALIGNED(len, sizeof(uint32_t))) { 57593db446aSBoris Brezillon uint32_t *p = (uint32_t *)buf; 57693db446aSBoris Brezillon len = len >> 2; 57793db446aSBoris Brezillon for (i = 0; i < len; i++) 5784df6ed4fSMiquel Raynal p[i] = readl_relaxed(host->data_va); 57993db446aSBoris Brezillon } else { 58093db446aSBoris Brezillon for (i = 0; i < len; i++) 5814df6ed4fSMiquel Raynal buf[i] = readb_relaxed(host->data_va); 58293db446aSBoris Brezillon } 58393db446aSBoris Brezillon } 58493db446aSBoris Brezillon 58593db446aSBoris Brezillon /* 58693db446aSBoris Brezillon * fsmc_read_buf_dma - read chip data into buffer 58793db446aSBoris Brezillon * @mtd: MTD device structure 58893db446aSBoris Brezillon * @buf: buffer to store date 58993db446aSBoris Brezillon * @len: number of bytes to read 59093db446aSBoris Brezillon */ 59193db446aSBoris Brezillon static void fsmc_read_buf_dma(struct mtd_info *mtd, uint8_t *buf, int len) 59293db446aSBoris Brezillon { 59393db446aSBoris Brezillon struct fsmc_nand_data *host = mtd_to_fsmc(mtd); 59493db446aSBoris Brezillon 59593db446aSBoris Brezillon dma_xfer(host, buf, len, DMA_FROM_DEVICE); 59693db446aSBoris Brezillon } 59793db446aSBoris Brezillon 59893db446aSBoris Brezillon /* 59993db446aSBoris Brezillon * fsmc_write_buf_dma - write buffer to chip 60093db446aSBoris Brezillon * @mtd: MTD device structure 60193db446aSBoris Brezillon * @buf: data buffer 60293db446aSBoris Brezillon * @len: number of bytes to write 60393db446aSBoris Brezillon */ 60493db446aSBoris Brezillon static void fsmc_write_buf_dma(struct mtd_info *mtd, const uint8_t *buf, 60593db446aSBoris Brezillon int len) 60693db446aSBoris Brezillon { 60793db446aSBoris Brezillon struct fsmc_nand_data *host = mtd_to_fsmc(mtd); 60893db446aSBoris Brezillon 60993db446aSBoris Brezillon dma_xfer(host, (void *)buf, len, DMA_TO_DEVICE); 61093db446aSBoris Brezillon } 61193db446aSBoris Brezillon 6124da712e7SMiquel Raynal /* fsmc_select_chip - assert or deassert nCE */ 6134da712e7SMiquel Raynal static void fsmc_select_chip(struct mtd_info *mtd, int chipnr) 6144da712e7SMiquel Raynal { 6154da712e7SMiquel Raynal struct fsmc_nand_data *host = mtd_to_fsmc(mtd); 6164da712e7SMiquel Raynal u32 pc; 6174da712e7SMiquel Raynal 6184da712e7SMiquel Raynal /* Support only one CS */ 6194da712e7SMiquel Raynal if (chipnr > 0) 6204da712e7SMiquel Raynal return; 6214da712e7SMiquel Raynal 6228f3931edSBoris Brezillon pc = readl(host->regs_va + FSMC_PC); 6234da712e7SMiquel Raynal if (chipnr < 0) 6248f3931edSBoris Brezillon writel_relaxed(pc & ~FSMC_ENABLE, host->regs_va + FSMC_PC); 6254da712e7SMiquel Raynal else 6268f3931edSBoris Brezillon writel_relaxed(pc | FSMC_ENABLE, host->regs_va + FSMC_PC); 6274da712e7SMiquel Raynal 6284da712e7SMiquel Raynal /* nCE line must be asserted before starting any operation */ 6294da712e7SMiquel Raynal mb(); 6304da712e7SMiquel Raynal } 6314da712e7SMiquel Raynal 6324da712e7SMiquel Raynal /* 6334da712e7SMiquel Raynal * fsmc_exec_op - hook called by the core to execute NAND operations 6344da712e7SMiquel Raynal * 6354da712e7SMiquel Raynal * This controller is simple enough and thus does not need to use the parser 6364da712e7SMiquel Raynal * provided by the core, instead, handle every situation here. 6374da712e7SMiquel Raynal */ 6384da712e7SMiquel Raynal static int fsmc_exec_op(struct nand_chip *chip, const struct nand_operation *op, 6394da712e7SMiquel Raynal bool check_only) 6404da712e7SMiquel Raynal { 6414da712e7SMiquel Raynal struct mtd_info *mtd = nand_to_mtd(chip); 6424da712e7SMiquel Raynal struct fsmc_nand_data *host = mtd_to_fsmc(mtd); 6434da712e7SMiquel Raynal const struct nand_op_instr *instr = NULL; 6444da712e7SMiquel Raynal int ret = 0; 6454da712e7SMiquel Raynal unsigned int op_id; 6464da712e7SMiquel Raynal int i; 6474da712e7SMiquel Raynal 6484da712e7SMiquel Raynal pr_debug("Executing operation [%d instructions]:\n", op->ninstrs); 6494da712e7SMiquel Raynal for (op_id = 0; op_id < op->ninstrs; op_id++) { 6504da712e7SMiquel Raynal instr = &op->instrs[op_id]; 6514da712e7SMiquel Raynal 6524da712e7SMiquel Raynal switch (instr->type) { 6534da712e7SMiquel Raynal case NAND_OP_CMD_INSTR: 6544da712e7SMiquel Raynal pr_debug(" ->CMD [0x%02x]\n", 6554da712e7SMiquel Raynal instr->ctx.cmd.opcode); 6564da712e7SMiquel Raynal 6574da712e7SMiquel Raynal writeb_relaxed(instr->ctx.cmd.opcode, host->cmd_va); 6584da712e7SMiquel Raynal break; 6594da712e7SMiquel Raynal 6604da712e7SMiquel Raynal case NAND_OP_ADDR_INSTR: 6614da712e7SMiquel Raynal pr_debug(" ->ADDR [%d cyc]", 6624da712e7SMiquel Raynal instr->ctx.addr.naddrs); 6634da712e7SMiquel Raynal 6644da712e7SMiquel Raynal for (i = 0; i < instr->ctx.addr.naddrs; i++) 6654da712e7SMiquel Raynal writeb_relaxed(instr->ctx.addr.addrs[i], 6664da712e7SMiquel Raynal host->addr_va); 6674da712e7SMiquel Raynal break; 6684da712e7SMiquel Raynal 6694da712e7SMiquel Raynal case NAND_OP_DATA_IN_INSTR: 6704da712e7SMiquel Raynal pr_debug(" ->DATA_IN [%d B%s]\n", instr->ctx.data.len, 6714da712e7SMiquel Raynal instr->ctx.data.force_8bit ? 6724da712e7SMiquel Raynal ", force 8-bit" : ""); 6734da712e7SMiquel Raynal 6744da712e7SMiquel Raynal if (host->mode == USE_DMA_ACCESS) 6754da712e7SMiquel Raynal fsmc_read_buf_dma(mtd, instr->ctx.data.buf.in, 6764da712e7SMiquel Raynal instr->ctx.data.len); 6774da712e7SMiquel Raynal else 6784da712e7SMiquel Raynal fsmc_read_buf(mtd, instr->ctx.data.buf.in, 6794da712e7SMiquel Raynal instr->ctx.data.len); 6804da712e7SMiquel Raynal break; 6814da712e7SMiquel Raynal 6824da712e7SMiquel Raynal case NAND_OP_DATA_OUT_INSTR: 6834da712e7SMiquel Raynal pr_debug(" ->DATA_OUT [%d B%s]\n", instr->ctx.data.len, 6844da712e7SMiquel Raynal instr->ctx.data.force_8bit ? 6854da712e7SMiquel Raynal ", force 8-bit" : ""); 6864da712e7SMiquel Raynal 6874da712e7SMiquel Raynal if (host->mode == USE_DMA_ACCESS) 6884da712e7SMiquel Raynal fsmc_write_buf_dma(mtd, instr->ctx.data.buf.out, 6894da712e7SMiquel Raynal instr->ctx.data.len); 6904da712e7SMiquel Raynal else 6914da712e7SMiquel Raynal fsmc_write_buf(mtd, instr->ctx.data.buf.out, 6924da712e7SMiquel Raynal instr->ctx.data.len); 6934da712e7SMiquel Raynal break; 6944da712e7SMiquel Raynal 6954da712e7SMiquel Raynal case NAND_OP_WAITRDY_INSTR: 6964da712e7SMiquel Raynal pr_debug(" ->WAITRDY [max %d ms]\n", 6974da712e7SMiquel Raynal instr->ctx.waitrdy.timeout_ms); 6984da712e7SMiquel Raynal 6994da712e7SMiquel Raynal ret = nand_soft_waitrdy(chip, 7004da712e7SMiquel Raynal instr->ctx.waitrdy.timeout_ms); 7014da712e7SMiquel Raynal break; 7024da712e7SMiquel Raynal } 7034da712e7SMiquel Raynal } 7044da712e7SMiquel Raynal 7054da712e7SMiquel Raynal return ret; 7064da712e7SMiquel Raynal } 7074da712e7SMiquel Raynal 70893db446aSBoris Brezillon /* 70993db446aSBoris Brezillon * fsmc_read_page_hwecc 71093db446aSBoris Brezillon * @mtd: mtd info structure 71193db446aSBoris Brezillon * @chip: nand chip info structure 71293db446aSBoris Brezillon * @buf: buffer to store read data 71393db446aSBoris Brezillon * @oob_required: caller expects OOB data read to chip->oob_poi 71493db446aSBoris Brezillon * @page: page number to read 71593db446aSBoris Brezillon * 71693db446aSBoris Brezillon * This routine is needed for fsmc version 8 as reading from NAND chip has to be 71793db446aSBoris Brezillon * performed in a strict sequence as follows: 71893db446aSBoris Brezillon * data(512 byte) -> ecc(13 byte) 71993db446aSBoris Brezillon * After this read, fsmc hardware generates and reports error data bits(up to a 72093db446aSBoris Brezillon * max of 8 bits) 72193db446aSBoris Brezillon */ 72293db446aSBoris Brezillon static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, 72393db446aSBoris Brezillon uint8_t *buf, int oob_required, int page) 72493db446aSBoris Brezillon { 72593db446aSBoris Brezillon int i, j, s, stat, eccsize = chip->ecc.size; 72693db446aSBoris Brezillon int eccbytes = chip->ecc.bytes; 72793db446aSBoris Brezillon int eccsteps = chip->ecc.steps; 72893db446aSBoris Brezillon uint8_t *p = buf; 72993db446aSBoris Brezillon uint8_t *ecc_calc = chip->ecc.calc_buf; 73093db446aSBoris Brezillon uint8_t *ecc_code = chip->ecc.code_buf; 73193db446aSBoris Brezillon int off, len, group = 0; 73293db446aSBoris Brezillon /* 73393db446aSBoris Brezillon * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we 73493db446aSBoris Brezillon * end up reading 14 bytes (7 words) from oob. The local array is 73593db446aSBoris Brezillon * to maintain word alignment 73693db446aSBoris Brezillon */ 73793db446aSBoris Brezillon uint16_t ecc_oob[7]; 73893db446aSBoris Brezillon uint8_t *oob = (uint8_t *)&ecc_oob[0]; 73993db446aSBoris Brezillon unsigned int max_bitflips = 0; 74093db446aSBoris Brezillon 74193db446aSBoris Brezillon for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) { 74293db446aSBoris Brezillon nand_read_page_op(chip, page, s * eccsize, NULL, 0); 743ec47636cSBoris Brezillon chip->ecc.hwctl(chip, NAND_ECC_READ); 74479e1ca37SBoris Brezillon nand_read_data_op(chip, p, eccsize, false); 74593db446aSBoris Brezillon 74693db446aSBoris Brezillon for (j = 0; j < eccbytes;) { 74793db446aSBoris Brezillon struct mtd_oob_region oobregion; 74893db446aSBoris Brezillon int ret; 74993db446aSBoris Brezillon 75093db446aSBoris Brezillon ret = mtd_ooblayout_ecc(mtd, group++, &oobregion); 75193db446aSBoris Brezillon if (ret) 75293db446aSBoris Brezillon return ret; 75393db446aSBoris Brezillon 75493db446aSBoris Brezillon off = oobregion.offset; 75593db446aSBoris Brezillon len = oobregion.length; 75693db446aSBoris Brezillon 75793db446aSBoris Brezillon /* 75893db446aSBoris Brezillon * length is intentionally kept a higher multiple of 2 75993db446aSBoris Brezillon * to read at least 13 bytes even in case of 16 bit NAND 76093db446aSBoris Brezillon * devices 76193db446aSBoris Brezillon */ 76293db446aSBoris Brezillon if (chip->options & NAND_BUSWIDTH_16) 76393db446aSBoris Brezillon len = roundup(len, 2); 76493db446aSBoris Brezillon 76593db446aSBoris Brezillon nand_read_oob_op(chip, page, off, oob + j, len); 76693db446aSBoris Brezillon j += len; 76793db446aSBoris Brezillon } 76893db446aSBoris Brezillon 76993db446aSBoris Brezillon memcpy(&ecc_code[i], oob, chip->ecc.bytes); 770af37d2c3SBoris Brezillon chip->ecc.calculate(chip, p, &ecc_calc[i]); 77193db446aSBoris Brezillon 77293db446aSBoris Brezillon stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); 77393db446aSBoris Brezillon if (stat < 0) { 77493db446aSBoris Brezillon mtd->ecc_stats.failed++; 77593db446aSBoris Brezillon } else { 77693db446aSBoris Brezillon mtd->ecc_stats.corrected += stat; 77793db446aSBoris Brezillon max_bitflips = max_t(unsigned int, max_bitflips, stat); 77893db446aSBoris Brezillon } 77993db446aSBoris Brezillon } 78093db446aSBoris Brezillon 78193db446aSBoris Brezillon return max_bitflips; 78293db446aSBoris Brezillon } 78393db446aSBoris Brezillon 78493db446aSBoris Brezillon /* 78593db446aSBoris Brezillon * fsmc_bch8_correct_data 78693db446aSBoris Brezillon * @mtd: mtd info structure 78793db446aSBoris Brezillon * @dat: buffer of read data 78893db446aSBoris Brezillon * @read_ecc: ecc read from device spare area 78993db446aSBoris Brezillon * @calc_ecc: ecc calculated from read data 79093db446aSBoris Brezillon * 79193db446aSBoris Brezillon * calc_ecc is a 104 bit information containing maximum of 8 error 79293db446aSBoris Brezillon * offset informations of 13 bits each in 512 bytes of read data. 79393db446aSBoris Brezillon */ 79493db446aSBoris Brezillon static int fsmc_bch8_correct_data(struct mtd_info *mtd, uint8_t *dat, 79593db446aSBoris Brezillon uint8_t *read_ecc, uint8_t *calc_ecc) 79693db446aSBoris Brezillon { 79793db446aSBoris Brezillon struct nand_chip *chip = mtd_to_nand(mtd); 79893db446aSBoris Brezillon struct fsmc_nand_data *host = mtd_to_fsmc(mtd); 79993db446aSBoris Brezillon uint32_t err_idx[8]; 80093db446aSBoris Brezillon uint32_t num_err, i; 80193db446aSBoris Brezillon uint32_t ecc1, ecc2, ecc3, ecc4; 80293db446aSBoris Brezillon 8034df6ed4fSMiquel Raynal num_err = (readl_relaxed(host->regs_va + STS) >> 10) & 0xF; 80493db446aSBoris Brezillon 80593db446aSBoris Brezillon /* no bit flipping */ 80693db446aSBoris Brezillon if (likely(num_err == 0)) 80793db446aSBoris Brezillon return 0; 80893db446aSBoris Brezillon 80993db446aSBoris Brezillon /* too many errors */ 81093db446aSBoris Brezillon if (unlikely(num_err > 8)) { 81193db446aSBoris Brezillon /* 81293db446aSBoris Brezillon * This is a temporary erase check. A newly erased page read 81393db446aSBoris Brezillon * would result in an ecc error because the oob data is also 81493db446aSBoris Brezillon * erased to FF and the calculated ecc for an FF data is not 81593db446aSBoris Brezillon * FF..FF. 81693db446aSBoris Brezillon * This is a workaround to skip performing correction in case 81793db446aSBoris Brezillon * data is FF..FF 81893db446aSBoris Brezillon * 81993db446aSBoris Brezillon * Logic: 82093db446aSBoris Brezillon * For every page, each bit written as 0 is counted until these 82193db446aSBoris Brezillon * number of bits are greater than 8 (the maximum correction 82293db446aSBoris Brezillon * capability of FSMC for each 512 + 13 bytes) 82393db446aSBoris Brezillon */ 82493db446aSBoris Brezillon 82593db446aSBoris Brezillon int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8); 82693db446aSBoris Brezillon int bits_data = count_written_bits(dat, chip->ecc.size, 8); 82793db446aSBoris Brezillon 82893db446aSBoris Brezillon if ((bits_ecc + bits_data) <= 8) { 82993db446aSBoris Brezillon if (bits_data) 83093db446aSBoris Brezillon memset(dat, 0xff, chip->ecc.size); 83193db446aSBoris Brezillon return bits_data; 83293db446aSBoris Brezillon } 83393db446aSBoris Brezillon 83493db446aSBoris Brezillon return -EBADMSG; 83593db446aSBoris Brezillon } 83693db446aSBoris Brezillon 83793db446aSBoris Brezillon /* 83893db446aSBoris Brezillon * ------------------- calc_ecc[] bit wise -----------|--13 bits--| 83993db446aSBoris Brezillon * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--| 84093db446aSBoris Brezillon * 84193db446aSBoris Brezillon * calc_ecc is a 104 bit information containing maximum of 8 error 84293db446aSBoris Brezillon * offset informations of 13 bits each. calc_ecc is copied into a 84393db446aSBoris Brezillon * uint64_t array and error offset indexes are populated in err_idx 84493db446aSBoris Brezillon * array 84593db446aSBoris Brezillon */ 8464df6ed4fSMiquel Raynal ecc1 = readl_relaxed(host->regs_va + ECC1); 8474df6ed4fSMiquel Raynal ecc2 = readl_relaxed(host->regs_va + ECC2); 8484df6ed4fSMiquel Raynal ecc3 = readl_relaxed(host->regs_va + ECC3); 8494df6ed4fSMiquel Raynal ecc4 = readl_relaxed(host->regs_va + STS); 85093db446aSBoris Brezillon 85193db446aSBoris Brezillon err_idx[0] = (ecc1 >> 0) & 0x1FFF; 85293db446aSBoris Brezillon err_idx[1] = (ecc1 >> 13) & 0x1FFF; 85393db446aSBoris Brezillon err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F); 85493db446aSBoris Brezillon err_idx[3] = (ecc2 >> 7) & 0x1FFF; 85593db446aSBoris Brezillon err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF); 85693db446aSBoris Brezillon err_idx[5] = (ecc3 >> 1) & 0x1FFF; 85793db446aSBoris Brezillon err_idx[6] = (ecc3 >> 14) & 0x1FFF; 85893db446aSBoris Brezillon err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F); 85993db446aSBoris Brezillon 86093db446aSBoris Brezillon i = 0; 86193db446aSBoris Brezillon while (num_err--) { 86293db446aSBoris Brezillon change_bit(0, (unsigned long *)&err_idx[i]); 86393db446aSBoris Brezillon change_bit(1, (unsigned long *)&err_idx[i]); 86493db446aSBoris Brezillon 86593db446aSBoris Brezillon if (err_idx[i] < chip->ecc.size * 8) { 86693db446aSBoris Brezillon change_bit(err_idx[i], (unsigned long *)dat); 86793db446aSBoris Brezillon i++; 86893db446aSBoris Brezillon } 86993db446aSBoris Brezillon } 87093db446aSBoris Brezillon return i; 87193db446aSBoris Brezillon } 87293db446aSBoris Brezillon 87393db446aSBoris Brezillon static bool filter(struct dma_chan *chan, void *slave) 87493db446aSBoris Brezillon { 87593db446aSBoris Brezillon chan->private = slave; 87693db446aSBoris Brezillon return true; 87793db446aSBoris Brezillon } 87893db446aSBoris Brezillon 87993db446aSBoris Brezillon static int fsmc_nand_probe_config_dt(struct platform_device *pdev, 88093db446aSBoris Brezillon struct fsmc_nand_data *host, 88193db446aSBoris Brezillon struct nand_chip *nand) 88293db446aSBoris Brezillon { 88393db446aSBoris Brezillon struct device_node *np = pdev->dev.of_node; 88493db446aSBoris Brezillon u32 val; 88593db446aSBoris Brezillon int ret; 88693db446aSBoris Brezillon 88793db446aSBoris Brezillon nand->options = 0; 88893db446aSBoris Brezillon 88993db446aSBoris Brezillon if (!of_property_read_u32(np, "bank-width", &val)) { 89093db446aSBoris Brezillon if (val == 2) { 89193db446aSBoris Brezillon nand->options |= NAND_BUSWIDTH_16; 89293db446aSBoris Brezillon } else if (val != 1) { 89393db446aSBoris Brezillon dev_err(&pdev->dev, "invalid bank-width %u\n", val); 89493db446aSBoris Brezillon return -EINVAL; 89593db446aSBoris Brezillon } 89693db446aSBoris Brezillon } 89793db446aSBoris Brezillon 89893db446aSBoris Brezillon if (of_get_property(np, "nand-skip-bbtscan", NULL)) 89993db446aSBoris Brezillon nand->options |= NAND_SKIP_BBTSCAN; 90093db446aSBoris Brezillon 90193db446aSBoris Brezillon host->dev_timings = devm_kzalloc(&pdev->dev, 90293db446aSBoris Brezillon sizeof(*host->dev_timings), GFP_KERNEL); 90393db446aSBoris Brezillon if (!host->dev_timings) 90493db446aSBoris Brezillon return -ENOMEM; 90593db446aSBoris Brezillon ret = of_property_read_u8_array(np, "timings", (u8 *)host->dev_timings, 90693db446aSBoris Brezillon sizeof(*host->dev_timings)); 90793db446aSBoris Brezillon if (ret) 90893db446aSBoris Brezillon host->dev_timings = NULL; 90993db446aSBoris Brezillon 91093db446aSBoris Brezillon /* Set default NAND bank to 0 */ 91193db446aSBoris Brezillon host->bank = 0; 91293db446aSBoris Brezillon if (!of_property_read_u32(np, "bank", &val)) { 91393db446aSBoris Brezillon if (val > 3) { 91493db446aSBoris Brezillon dev_err(&pdev->dev, "invalid bank %u\n", val); 91593db446aSBoris Brezillon return -EINVAL; 91693db446aSBoris Brezillon } 91793db446aSBoris Brezillon host->bank = val; 91893db446aSBoris Brezillon } 91993db446aSBoris Brezillon return 0; 92093db446aSBoris Brezillon } 92193db446aSBoris Brezillon 9223bbddfa3SMiquel Raynal static int fsmc_nand_attach_chip(struct nand_chip *nand) 9233bbddfa3SMiquel Raynal { 9243bbddfa3SMiquel Raynal struct mtd_info *mtd = nand_to_mtd(nand); 9253bbddfa3SMiquel Raynal struct fsmc_nand_data *host = mtd_to_fsmc(mtd); 9263bbddfa3SMiquel Raynal 9273bbddfa3SMiquel Raynal if (AMBA_REV_BITS(host->pid) >= 8) { 9283bbddfa3SMiquel Raynal switch (mtd->oobsize) { 9293bbddfa3SMiquel Raynal case 16: 9303bbddfa3SMiquel Raynal case 64: 9313bbddfa3SMiquel Raynal case 128: 9323bbddfa3SMiquel Raynal case 224: 9333bbddfa3SMiquel Raynal case 256: 9343bbddfa3SMiquel Raynal break; 9353bbddfa3SMiquel Raynal default: 9363bbddfa3SMiquel Raynal dev_warn(host->dev, 9373bbddfa3SMiquel Raynal "No oob scheme defined for oobsize %d\n", 9383bbddfa3SMiquel Raynal mtd->oobsize); 9393bbddfa3SMiquel Raynal return -EINVAL; 9403bbddfa3SMiquel Raynal } 9413bbddfa3SMiquel Raynal 9423bbddfa3SMiquel Raynal mtd_set_ooblayout(mtd, &fsmc_ecc4_ooblayout_ops); 9433bbddfa3SMiquel Raynal 9443bbddfa3SMiquel Raynal return 0; 9453bbddfa3SMiquel Raynal } 9463bbddfa3SMiquel Raynal 9473bbddfa3SMiquel Raynal switch (nand->ecc.mode) { 9483bbddfa3SMiquel Raynal case NAND_ECC_HW: 9493bbddfa3SMiquel Raynal dev_info(host->dev, "Using 1-bit HW ECC scheme\n"); 9503bbddfa3SMiquel Raynal nand->ecc.calculate = fsmc_read_hwecc_ecc1; 9513bbddfa3SMiquel Raynal nand->ecc.correct = nand_correct_data; 9523bbddfa3SMiquel Raynal nand->ecc.bytes = 3; 9533bbddfa3SMiquel Raynal nand->ecc.strength = 1; 9543bbddfa3SMiquel Raynal break; 9553bbddfa3SMiquel Raynal 9563bbddfa3SMiquel Raynal case NAND_ECC_SOFT: 9573bbddfa3SMiquel Raynal if (nand->ecc.algo == NAND_ECC_BCH) { 9583bbddfa3SMiquel Raynal dev_info(host->dev, 9593bbddfa3SMiquel Raynal "Using 4-bit SW BCH ECC scheme\n"); 9603bbddfa3SMiquel Raynal break; 9613bbddfa3SMiquel Raynal } 9623bbddfa3SMiquel Raynal 9633bbddfa3SMiquel Raynal case NAND_ECC_ON_DIE: 9643bbddfa3SMiquel Raynal break; 9653bbddfa3SMiquel Raynal 9663bbddfa3SMiquel Raynal default: 9673bbddfa3SMiquel Raynal dev_err(host->dev, "Unsupported ECC mode!\n"); 9683bbddfa3SMiquel Raynal return -ENOTSUPP; 9693bbddfa3SMiquel Raynal } 9703bbddfa3SMiquel Raynal 9713bbddfa3SMiquel Raynal /* 9723bbddfa3SMiquel Raynal * Don't set layout for BCH4 SW ECC. This will be 9733bbddfa3SMiquel Raynal * generated later in nand_bch_init() later. 9743bbddfa3SMiquel Raynal */ 9753bbddfa3SMiquel Raynal if (nand->ecc.mode == NAND_ECC_HW) { 9763bbddfa3SMiquel Raynal switch (mtd->oobsize) { 9773bbddfa3SMiquel Raynal case 16: 9783bbddfa3SMiquel Raynal case 64: 9793bbddfa3SMiquel Raynal case 128: 9803bbddfa3SMiquel Raynal mtd_set_ooblayout(mtd, 9813bbddfa3SMiquel Raynal &fsmc_ecc1_ooblayout_ops); 9823bbddfa3SMiquel Raynal break; 9833bbddfa3SMiquel Raynal default: 9843bbddfa3SMiquel Raynal dev_warn(host->dev, 9853bbddfa3SMiquel Raynal "No oob scheme defined for oobsize %d\n", 9863bbddfa3SMiquel Raynal mtd->oobsize); 9873bbddfa3SMiquel Raynal return -EINVAL; 9883bbddfa3SMiquel Raynal } 9893bbddfa3SMiquel Raynal } 9903bbddfa3SMiquel Raynal 9913bbddfa3SMiquel Raynal return 0; 9923bbddfa3SMiquel Raynal } 9933bbddfa3SMiquel Raynal 9943bbddfa3SMiquel Raynal static const struct nand_controller_ops fsmc_nand_controller_ops = { 9953bbddfa3SMiquel Raynal .attach_chip = fsmc_nand_attach_chip, 9963bbddfa3SMiquel Raynal }; 9973bbddfa3SMiquel Raynal 99893db446aSBoris Brezillon /* 99993db446aSBoris Brezillon * fsmc_nand_probe - Probe function 100093db446aSBoris Brezillon * @pdev: platform device structure 100193db446aSBoris Brezillon */ 100293db446aSBoris Brezillon static int __init fsmc_nand_probe(struct platform_device *pdev) 100393db446aSBoris Brezillon { 100493db446aSBoris Brezillon struct fsmc_nand_data *host; 100593db446aSBoris Brezillon struct mtd_info *mtd; 100693db446aSBoris Brezillon struct nand_chip *nand; 100793db446aSBoris Brezillon struct resource *res; 10084df6ed4fSMiquel Raynal void __iomem *base; 100993db446aSBoris Brezillon dma_cap_mask_t mask; 101093db446aSBoris Brezillon int ret = 0; 101193db446aSBoris Brezillon u32 pid; 101293db446aSBoris Brezillon int i; 101393db446aSBoris Brezillon 101493db446aSBoris Brezillon /* Allocate memory for the device structure (and zero it) */ 101593db446aSBoris Brezillon host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL); 101693db446aSBoris Brezillon if (!host) 101793db446aSBoris Brezillon return -ENOMEM; 101893db446aSBoris Brezillon 101993db446aSBoris Brezillon nand = &host->nand; 102093db446aSBoris Brezillon 102193db446aSBoris Brezillon ret = fsmc_nand_probe_config_dt(pdev, host, nand); 102293db446aSBoris Brezillon if (ret) 102393db446aSBoris Brezillon return ret; 102493db446aSBoris Brezillon 102593db446aSBoris Brezillon res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data"); 102693db446aSBoris Brezillon host->data_va = devm_ioremap_resource(&pdev->dev, res); 102793db446aSBoris Brezillon if (IS_ERR(host->data_va)) 102893db446aSBoris Brezillon return PTR_ERR(host->data_va); 102993db446aSBoris Brezillon 103093db446aSBoris Brezillon host->data_pa = (dma_addr_t)res->start; 103193db446aSBoris Brezillon 103293db446aSBoris Brezillon res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_addr"); 103393db446aSBoris Brezillon host->addr_va = devm_ioremap_resource(&pdev->dev, res); 103493db446aSBoris Brezillon if (IS_ERR(host->addr_va)) 103593db446aSBoris Brezillon return PTR_ERR(host->addr_va); 103693db446aSBoris Brezillon 103793db446aSBoris Brezillon res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_cmd"); 103893db446aSBoris Brezillon host->cmd_va = devm_ioremap_resource(&pdev->dev, res); 103993db446aSBoris Brezillon if (IS_ERR(host->cmd_va)) 104093db446aSBoris Brezillon return PTR_ERR(host->cmd_va); 104193db446aSBoris Brezillon 104293db446aSBoris Brezillon res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs"); 10434df6ed4fSMiquel Raynal base = devm_ioremap_resource(&pdev->dev, res); 10444df6ed4fSMiquel Raynal if (IS_ERR(base)) 10454df6ed4fSMiquel Raynal return PTR_ERR(base); 10464df6ed4fSMiquel Raynal 10474df6ed4fSMiquel Raynal host->regs_va = base + FSMC_NOR_REG_SIZE + 10484df6ed4fSMiquel Raynal (host->bank * FSMC_NAND_BANK_SZ); 104993db446aSBoris Brezillon 105093db446aSBoris Brezillon host->clk = devm_clk_get(&pdev->dev, NULL); 105193db446aSBoris Brezillon if (IS_ERR(host->clk)) { 105293db446aSBoris Brezillon dev_err(&pdev->dev, "failed to fetch block clock\n"); 105393db446aSBoris Brezillon return PTR_ERR(host->clk); 105493db446aSBoris Brezillon } 105593db446aSBoris Brezillon 105693db446aSBoris Brezillon ret = clk_prepare_enable(host->clk); 105793db446aSBoris Brezillon if (ret) 105893db446aSBoris Brezillon return ret; 105993db446aSBoris Brezillon 106093db446aSBoris Brezillon /* 106193db446aSBoris Brezillon * This device ID is actually a common AMBA ID as used on the 106293db446aSBoris Brezillon * AMBA PrimeCell bus. However it is not a PrimeCell. 106393db446aSBoris Brezillon */ 106493db446aSBoris Brezillon for (pid = 0, i = 0; i < 4; i++) 10654df6ed4fSMiquel Raynal pid |= (readl(base + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8); 106693db446aSBoris Brezillon host->pid = pid; 106793db446aSBoris Brezillon dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, " 106893db446aSBoris Brezillon "revision %02x, config %02x\n", 106993db446aSBoris Brezillon AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid), 107093db446aSBoris Brezillon AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid)); 107193db446aSBoris Brezillon 107293db446aSBoris Brezillon host->dev = &pdev->dev; 107393db446aSBoris Brezillon 107493db446aSBoris Brezillon if (host->mode == USE_DMA_ACCESS) 107593db446aSBoris Brezillon init_completion(&host->dma_access_complete); 107693db446aSBoris Brezillon 107793db446aSBoris Brezillon /* Link all private pointers */ 107893db446aSBoris Brezillon mtd = nand_to_mtd(&host->nand); 107993db446aSBoris Brezillon nand_set_controller_data(nand, host); 108093db446aSBoris Brezillon nand_set_flash_node(nand, pdev->dev.of_node); 108193db446aSBoris Brezillon 108293db446aSBoris Brezillon mtd->dev.parent = &pdev->dev; 10834da712e7SMiquel Raynal nand->exec_op = fsmc_exec_op; 10844da712e7SMiquel Raynal nand->select_chip = fsmc_select_chip; 108593db446aSBoris Brezillon nand->chip_delay = 30; 108693db446aSBoris Brezillon 108793db446aSBoris Brezillon /* 108893db446aSBoris Brezillon * Setup default ECC mode. nand_dt_init() called from nand_scan_ident() 108993db446aSBoris Brezillon * can overwrite this value if the DT provides a different value. 109093db446aSBoris Brezillon */ 109193db446aSBoris Brezillon nand->ecc.mode = NAND_ECC_HW; 109293db446aSBoris Brezillon nand->ecc.hwctl = fsmc_enable_hwecc; 109393db446aSBoris Brezillon nand->ecc.size = 512; 109493db446aSBoris Brezillon nand->badblockbits = 7; 109593db446aSBoris Brezillon 10964da712e7SMiquel Raynal if (host->mode == USE_DMA_ACCESS) { 109793db446aSBoris Brezillon dma_cap_zero(mask); 109893db446aSBoris Brezillon dma_cap_set(DMA_MEMCPY, mask); 109993db446aSBoris Brezillon host->read_dma_chan = dma_request_channel(mask, filter, NULL); 110093db446aSBoris Brezillon if (!host->read_dma_chan) { 110193db446aSBoris Brezillon dev_err(&pdev->dev, "Unable to get read dma channel\n"); 110243fab011SMiquel Raynal goto disable_clk; 110393db446aSBoris Brezillon } 110493db446aSBoris Brezillon host->write_dma_chan = dma_request_channel(mask, filter, NULL); 110593db446aSBoris Brezillon if (!host->write_dma_chan) { 110693db446aSBoris Brezillon dev_err(&pdev->dev, "Unable to get write dma channel\n"); 110743fab011SMiquel Raynal goto release_dma_read_chan; 110893db446aSBoris Brezillon } 110993db446aSBoris Brezillon } 111093db446aSBoris Brezillon 111193db446aSBoris Brezillon if (host->dev_timings) 111293db446aSBoris Brezillon fsmc_nand_setup(host, host->dev_timings); 111393db446aSBoris Brezillon else 111493db446aSBoris Brezillon nand->setup_data_interface = fsmc_setup_data_interface; 111593db446aSBoris Brezillon 111693db446aSBoris Brezillon if (AMBA_REV_BITS(host->pid) >= 8) { 111793db446aSBoris Brezillon nand->ecc.read_page = fsmc_read_page_hwecc; 111893db446aSBoris Brezillon nand->ecc.calculate = fsmc_read_hwecc_ecc4; 111993db446aSBoris Brezillon nand->ecc.correct = fsmc_bch8_correct_data; 112093db446aSBoris Brezillon nand->ecc.bytes = 13; 112193db446aSBoris Brezillon nand->ecc.strength = 8; 112293db446aSBoris Brezillon } 112393db446aSBoris Brezillon 112493db446aSBoris Brezillon /* 112593db446aSBoris Brezillon * Scan to find existence of the device 112693db446aSBoris Brezillon */ 11273bbddfa3SMiquel Raynal nand->dummy_controller.ops = &fsmc_nand_controller_ops; 112800ad378fSBoris Brezillon ret = nand_scan(nand, 1); 112993db446aSBoris Brezillon if (ret) 113043fab011SMiquel Raynal goto release_dma_write_chan; 113193db446aSBoris Brezillon 113293db446aSBoris Brezillon mtd->name = "nand"; 113393db446aSBoris Brezillon ret = mtd_device_register(mtd, NULL, 0); 113493db446aSBoris Brezillon if (ret) 1135682cae27SMiquel Raynal goto cleanup_nand; 113693db446aSBoris Brezillon 113793db446aSBoris Brezillon platform_set_drvdata(pdev, host); 113893db446aSBoris Brezillon dev_info(&pdev->dev, "FSMC NAND driver registration successful\n"); 113943fab011SMiquel Raynal 114093db446aSBoris Brezillon return 0; 114193db446aSBoris Brezillon 1142682cae27SMiquel Raynal cleanup_nand: 1143682cae27SMiquel Raynal nand_cleanup(nand); 114443fab011SMiquel Raynal release_dma_write_chan: 114593db446aSBoris Brezillon if (host->mode == USE_DMA_ACCESS) 114693db446aSBoris Brezillon dma_release_channel(host->write_dma_chan); 114743fab011SMiquel Raynal release_dma_read_chan: 114893db446aSBoris Brezillon if (host->mode == USE_DMA_ACCESS) 114993db446aSBoris Brezillon dma_release_channel(host->read_dma_chan); 115043fab011SMiquel Raynal disable_clk: 115193db446aSBoris Brezillon clk_disable_unprepare(host->clk); 115243fab011SMiquel Raynal 115393db446aSBoris Brezillon return ret; 115493db446aSBoris Brezillon } 115593db446aSBoris Brezillon 115693db446aSBoris Brezillon /* 115793db446aSBoris Brezillon * Clean up routine 115893db446aSBoris Brezillon */ 115993db446aSBoris Brezillon static int fsmc_nand_remove(struct platform_device *pdev) 116093db446aSBoris Brezillon { 116193db446aSBoris Brezillon struct fsmc_nand_data *host = platform_get_drvdata(pdev); 116293db446aSBoris Brezillon 116393db446aSBoris Brezillon if (host) { 116459ac276fSBoris Brezillon nand_release(&host->nand); 116593db446aSBoris Brezillon 116693db446aSBoris Brezillon if (host->mode == USE_DMA_ACCESS) { 116793db446aSBoris Brezillon dma_release_channel(host->write_dma_chan); 116893db446aSBoris Brezillon dma_release_channel(host->read_dma_chan); 116993db446aSBoris Brezillon } 117093db446aSBoris Brezillon clk_disable_unprepare(host->clk); 117193db446aSBoris Brezillon } 117293db446aSBoris Brezillon 117393db446aSBoris Brezillon return 0; 117493db446aSBoris Brezillon } 117593db446aSBoris Brezillon 117693db446aSBoris Brezillon #ifdef CONFIG_PM_SLEEP 117793db446aSBoris Brezillon static int fsmc_nand_suspend(struct device *dev) 117893db446aSBoris Brezillon { 117993db446aSBoris Brezillon struct fsmc_nand_data *host = dev_get_drvdata(dev); 118093db446aSBoris Brezillon if (host) 118193db446aSBoris Brezillon clk_disable_unprepare(host->clk); 118293db446aSBoris Brezillon return 0; 118393db446aSBoris Brezillon } 118493db446aSBoris Brezillon 118593db446aSBoris Brezillon static int fsmc_nand_resume(struct device *dev) 118693db446aSBoris Brezillon { 118793db446aSBoris Brezillon struct fsmc_nand_data *host = dev_get_drvdata(dev); 118893db446aSBoris Brezillon if (host) { 118993db446aSBoris Brezillon clk_prepare_enable(host->clk); 119093db446aSBoris Brezillon if (host->dev_timings) 119193db446aSBoris Brezillon fsmc_nand_setup(host, host->dev_timings); 119293db446aSBoris Brezillon } 119393db446aSBoris Brezillon return 0; 119493db446aSBoris Brezillon } 119593db446aSBoris Brezillon #endif 119693db446aSBoris Brezillon 119793db446aSBoris Brezillon static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume); 119893db446aSBoris Brezillon 119993db446aSBoris Brezillon static const struct of_device_id fsmc_nand_id_table[] = { 120093db446aSBoris Brezillon { .compatible = "st,spear600-fsmc-nand" }, 120193db446aSBoris Brezillon { .compatible = "stericsson,fsmc-nand" }, 120293db446aSBoris Brezillon {} 120393db446aSBoris Brezillon }; 120493db446aSBoris Brezillon MODULE_DEVICE_TABLE(of, fsmc_nand_id_table); 120593db446aSBoris Brezillon 120693db446aSBoris Brezillon static struct platform_driver fsmc_nand_driver = { 120793db446aSBoris Brezillon .remove = fsmc_nand_remove, 120893db446aSBoris Brezillon .driver = { 120993db446aSBoris Brezillon .name = "fsmc-nand", 121093db446aSBoris Brezillon .of_match_table = fsmc_nand_id_table, 121193db446aSBoris Brezillon .pm = &fsmc_nand_pm_ops, 121293db446aSBoris Brezillon }, 121393db446aSBoris Brezillon }; 121493db446aSBoris Brezillon 121593db446aSBoris Brezillon module_platform_driver_probe(fsmc_nand_driver, fsmc_nand_probe); 121693db446aSBoris Brezillon 121793db446aSBoris Brezillon MODULE_LICENSE("GPL"); 121893db446aSBoris Brezillon MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi"); 121993db446aSBoris Brezillon MODULE_DESCRIPTION("NAND driver for SPEAr Platforms"); 1220