xref: /openbmc/linux/drivers/mtd/nand/raw/fsmc_nand.c (revision 4df6ed4f)
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 */
6593db446aSBoris Brezillon #define 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_cmd_ctrl - For facilitaing Hardware access
25893db446aSBoris Brezillon  * This routine allows hardware specific access to control-lines(ALE,CLE)
25993db446aSBoris Brezillon  */
26093db446aSBoris Brezillon static void fsmc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
26193db446aSBoris Brezillon {
26293db446aSBoris Brezillon 	struct nand_chip *this = mtd_to_nand(mtd);
26393db446aSBoris Brezillon 	struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
26493db446aSBoris Brezillon 
26593db446aSBoris Brezillon 	if (ctrl & NAND_CTRL_CHANGE) {
26693db446aSBoris Brezillon 		u32 pc;
26793db446aSBoris Brezillon 
26893db446aSBoris Brezillon 		if (ctrl & NAND_CLE) {
26993db446aSBoris Brezillon 			this->IO_ADDR_R = host->cmd_va;
27093db446aSBoris Brezillon 			this->IO_ADDR_W = host->cmd_va;
27193db446aSBoris Brezillon 		} else if (ctrl & NAND_ALE) {
27293db446aSBoris Brezillon 			this->IO_ADDR_R = host->addr_va;
27393db446aSBoris Brezillon 			this->IO_ADDR_W = host->addr_va;
27493db446aSBoris Brezillon 		} else {
27593db446aSBoris Brezillon 			this->IO_ADDR_R = host->data_va;
27693db446aSBoris Brezillon 			this->IO_ADDR_W = host->data_va;
27793db446aSBoris Brezillon 		}
27893db446aSBoris Brezillon 
2794df6ed4fSMiquel Raynal 		pc = readl(host->regs_va + PC);
28093db446aSBoris Brezillon 		if (ctrl & NAND_NCE)
28193db446aSBoris Brezillon 			pc |= FSMC_ENABLE;
28293db446aSBoris Brezillon 		else
28393db446aSBoris Brezillon 			pc &= ~FSMC_ENABLE;
2844df6ed4fSMiquel Raynal 		writel_relaxed(pc, host->regs_va + PC);
28593db446aSBoris Brezillon 	}
28693db446aSBoris Brezillon 
28793db446aSBoris Brezillon 	mb();
28893db446aSBoris Brezillon 
28993db446aSBoris Brezillon 	if (cmd != NAND_CMD_NONE)
29093db446aSBoris Brezillon 		writeb_relaxed(cmd, this->IO_ADDR_W);
29193db446aSBoris Brezillon }
29293db446aSBoris Brezillon 
29393db446aSBoris Brezillon /*
29493db446aSBoris Brezillon  * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
29593db446aSBoris Brezillon  *
29693db446aSBoris Brezillon  * This routine initializes timing parameters related to NAND memory access in
29793db446aSBoris Brezillon  * FSMC registers
29893db446aSBoris Brezillon  */
29993db446aSBoris Brezillon static void fsmc_nand_setup(struct fsmc_nand_data *host,
30093db446aSBoris Brezillon 			    struct fsmc_nand_timings *tims)
30193db446aSBoris Brezillon {
30293db446aSBoris Brezillon 	uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
30393db446aSBoris Brezillon 	uint32_t tclr, tar, thiz, thold, twait, tset;
30493db446aSBoris Brezillon 
30593db446aSBoris Brezillon 	tclr = (tims->tclr & FSMC_TCLR_MASK) << FSMC_TCLR_SHIFT;
30693db446aSBoris Brezillon 	tar = (tims->tar & FSMC_TAR_MASK) << FSMC_TAR_SHIFT;
30793db446aSBoris Brezillon 	thiz = (tims->thiz & FSMC_THIZ_MASK) << FSMC_THIZ_SHIFT;
30893db446aSBoris Brezillon 	thold = (tims->thold & FSMC_THOLD_MASK) << FSMC_THOLD_SHIFT;
30993db446aSBoris Brezillon 	twait = (tims->twait & FSMC_TWAIT_MASK) << FSMC_TWAIT_SHIFT;
31093db446aSBoris Brezillon 	tset = (tims->tset & FSMC_TSET_MASK) << FSMC_TSET_SHIFT;
31193db446aSBoris Brezillon 
31293db446aSBoris Brezillon 	if (host->nand.options & NAND_BUSWIDTH_16)
3134df6ed4fSMiquel Raynal 		writel_relaxed(value | FSMC_DEVWID_16, host->regs_va + PC);
31493db446aSBoris Brezillon 	else
3154df6ed4fSMiquel Raynal 		writel_relaxed(value | FSMC_DEVWID_8, host->regs_va + PC);
31693db446aSBoris Brezillon 
3174df6ed4fSMiquel Raynal 	writel_relaxed(readl(host->regs_va + PC) | tclr | tar,
3184df6ed4fSMiquel Raynal 		       host->regs_va + PC);
3194df6ed4fSMiquel Raynal 	writel_relaxed(thiz | thold | twait | tset, host->regs_va + COMM);
3204df6ed4fSMiquel Raynal 	writel_relaxed(thiz | thold | twait | tset, host->regs_va + ATTRIB);
32193db446aSBoris Brezillon }
32293db446aSBoris Brezillon 
32393db446aSBoris Brezillon static int fsmc_calc_timings(struct fsmc_nand_data *host,
32493db446aSBoris Brezillon 			     const struct nand_sdr_timings *sdrt,
32593db446aSBoris Brezillon 			     struct fsmc_nand_timings *tims)
32693db446aSBoris Brezillon {
32793db446aSBoris Brezillon 	unsigned long hclk = clk_get_rate(host->clk);
32893db446aSBoris Brezillon 	unsigned long hclkn = NSEC_PER_SEC / hclk;
32993db446aSBoris Brezillon 	uint32_t thiz, thold, twait, tset;
33093db446aSBoris Brezillon 
33193db446aSBoris Brezillon 	if (sdrt->tRC_min < 30000)
33293db446aSBoris Brezillon 		return -EOPNOTSUPP;
33393db446aSBoris Brezillon 
33493db446aSBoris Brezillon 	tims->tar = DIV_ROUND_UP(sdrt->tAR_min / 1000, hclkn) - 1;
33593db446aSBoris Brezillon 	if (tims->tar > FSMC_TAR_MASK)
33693db446aSBoris Brezillon 		tims->tar = FSMC_TAR_MASK;
33793db446aSBoris Brezillon 	tims->tclr = DIV_ROUND_UP(sdrt->tCLR_min / 1000, hclkn) - 1;
33893db446aSBoris Brezillon 	if (tims->tclr > FSMC_TCLR_MASK)
33993db446aSBoris Brezillon 		tims->tclr = FSMC_TCLR_MASK;
34093db446aSBoris Brezillon 
34193db446aSBoris Brezillon 	thiz = sdrt->tCS_min - sdrt->tWP_min;
34293db446aSBoris Brezillon 	tims->thiz = DIV_ROUND_UP(thiz / 1000, hclkn);
34393db446aSBoris Brezillon 
34493db446aSBoris Brezillon 	thold = sdrt->tDH_min;
34593db446aSBoris Brezillon 	if (thold < sdrt->tCH_min)
34693db446aSBoris Brezillon 		thold = sdrt->tCH_min;
34793db446aSBoris Brezillon 	if (thold < sdrt->tCLH_min)
34893db446aSBoris Brezillon 		thold = sdrt->tCLH_min;
34993db446aSBoris Brezillon 	if (thold < sdrt->tWH_min)
35093db446aSBoris Brezillon 		thold = sdrt->tWH_min;
35193db446aSBoris Brezillon 	if (thold < sdrt->tALH_min)
35293db446aSBoris Brezillon 		thold = sdrt->tALH_min;
35393db446aSBoris Brezillon 	if (thold < sdrt->tREH_min)
35493db446aSBoris Brezillon 		thold = sdrt->tREH_min;
35593db446aSBoris Brezillon 	tims->thold = DIV_ROUND_UP(thold / 1000, hclkn);
35693db446aSBoris Brezillon 	if (tims->thold == 0)
35793db446aSBoris Brezillon 		tims->thold = 1;
35893db446aSBoris Brezillon 	else if (tims->thold > FSMC_THOLD_MASK)
35993db446aSBoris Brezillon 		tims->thold = FSMC_THOLD_MASK;
36093db446aSBoris Brezillon 
36193db446aSBoris Brezillon 	twait = max(sdrt->tRP_min, sdrt->tWP_min);
36293db446aSBoris Brezillon 	tims->twait = DIV_ROUND_UP(twait / 1000, hclkn) - 1;
36393db446aSBoris Brezillon 	if (tims->twait == 0)
36493db446aSBoris Brezillon 		tims->twait = 1;
36593db446aSBoris Brezillon 	else if (tims->twait > FSMC_TWAIT_MASK)
36693db446aSBoris Brezillon 		tims->twait = FSMC_TWAIT_MASK;
36793db446aSBoris Brezillon 
36893db446aSBoris Brezillon 	tset = max(sdrt->tCS_min - sdrt->tWP_min,
36993db446aSBoris Brezillon 		   sdrt->tCEA_max - sdrt->tREA_max);
37093db446aSBoris Brezillon 	tims->tset = DIV_ROUND_UP(tset / 1000, hclkn) - 1;
37193db446aSBoris Brezillon 	if (tims->tset == 0)
37293db446aSBoris Brezillon 		tims->tset = 1;
37393db446aSBoris Brezillon 	else if (tims->tset > FSMC_TSET_MASK)
37493db446aSBoris Brezillon 		tims->tset = FSMC_TSET_MASK;
37593db446aSBoris Brezillon 
37693db446aSBoris Brezillon 	return 0;
37793db446aSBoris Brezillon }
37893db446aSBoris Brezillon 
37993db446aSBoris Brezillon static int fsmc_setup_data_interface(struct mtd_info *mtd, int csline,
38093db446aSBoris Brezillon 				     const struct nand_data_interface *conf)
38193db446aSBoris Brezillon {
38293db446aSBoris Brezillon 	struct nand_chip *nand = mtd_to_nand(mtd);
38393db446aSBoris Brezillon 	struct fsmc_nand_data *host = nand_get_controller_data(nand);
38493db446aSBoris Brezillon 	struct fsmc_nand_timings tims;
38593db446aSBoris Brezillon 	const struct nand_sdr_timings *sdrt;
38693db446aSBoris Brezillon 	int ret;
38793db446aSBoris Brezillon 
38893db446aSBoris Brezillon 	sdrt = nand_get_sdr_timings(conf);
38993db446aSBoris Brezillon 	if (IS_ERR(sdrt))
39093db446aSBoris Brezillon 		return PTR_ERR(sdrt);
39193db446aSBoris Brezillon 
39293db446aSBoris Brezillon 	ret = fsmc_calc_timings(host, sdrt, &tims);
39393db446aSBoris Brezillon 	if (ret)
39493db446aSBoris Brezillon 		return ret;
39593db446aSBoris Brezillon 
39693db446aSBoris Brezillon 	if (csline == NAND_DATA_IFACE_CHECK_ONLY)
39793db446aSBoris Brezillon 		return 0;
39893db446aSBoris Brezillon 
39993db446aSBoris Brezillon 	fsmc_nand_setup(host, &tims);
40093db446aSBoris Brezillon 
40193db446aSBoris Brezillon 	return 0;
40293db446aSBoris Brezillon }
40393db446aSBoris Brezillon 
40493db446aSBoris Brezillon /*
40593db446aSBoris Brezillon  * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
40693db446aSBoris Brezillon  */
40793db446aSBoris Brezillon static void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
40893db446aSBoris Brezillon {
40993db446aSBoris Brezillon 	struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
41093db446aSBoris Brezillon 
4114df6ed4fSMiquel Raynal 	writel_relaxed(readl(host->regs_va + PC) & ~FSMC_ECCPLEN_256,
4124df6ed4fSMiquel Raynal 		       host->regs_va + PC);
4134df6ed4fSMiquel Raynal 	writel_relaxed(readl(host->regs_va + PC) & ~FSMC_ECCEN,
4144df6ed4fSMiquel Raynal 		       host->regs_va + PC);
4154df6ed4fSMiquel Raynal 	writel_relaxed(readl(host->regs_va + PC) | FSMC_ECCEN,
4164df6ed4fSMiquel Raynal 		       host->regs_va + PC);
41793db446aSBoris Brezillon }
41893db446aSBoris Brezillon 
41993db446aSBoris Brezillon /*
42093db446aSBoris Brezillon  * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
42193db446aSBoris Brezillon  * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
42293db446aSBoris Brezillon  * max of 8-bits)
42393db446aSBoris Brezillon  */
42493db446aSBoris Brezillon static int fsmc_read_hwecc_ecc4(struct mtd_info *mtd, const uint8_t *data,
42593db446aSBoris Brezillon 				uint8_t *ecc)
42693db446aSBoris Brezillon {
42793db446aSBoris Brezillon 	struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
42893db446aSBoris Brezillon 	uint32_t ecc_tmp;
42993db446aSBoris Brezillon 	unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
43093db446aSBoris Brezillon 
43193db446aSBoris Brezillon 	do {
4324df6ed4fSMiquel Raynal 		if (readl_relaxed(host->regs_va + STS) & FSMC_CODE_RDY)
43393db446aSBoris Brezillon 			break;
43493db446aSBoris Brezillon 		else
43593db446aSBoris Brezillon 			cond_resched();
43693db446aSBoris Brezillon 	} while (!time_after_eq(jiffies, deadline));
43793db446aSBoris Brezillon 
43893db446aSBoris Brezillon 	if (time_after_eq(jiffies, deadline)) {
43993db446aSBoris Brezillon 		dev_err(host->dev, "calculate ecc timed out\n");
44093db446aSBoris Brezillon 		return -ETIMEDOUT;
44193db446aSBoris Brezillon 	}
44293db446aSBoris Brezillon 
4434df6ed4fSMiquel Raynal 	ecc_tmp = readl_relaxed(host->regs_va + ECC1);
44493db446aSBoris Brezillon 	ecc[0] = (uint8_t) (ecc_tmp >> 0);
44593db446aSBoris Brezillon 	ecc[1] = (uint8_t) (ecc_tmp >> 8);
44693db446aSBoris Brezillon 	ecc[2] = (uint8_t) (ecc_tmp >> 16);
44793db446aSBoris Brezillon 	ecc[3] = (uint8_t) (ecc_tmp >> 24);
44893db446aSBoris Brezillon 
4494df6ed4fSMiquel Raynal 	ecc_tmp = readl_relaxed(host->regs_va + ECC2);
45093db446aSBoris Brezillon 	ecc[4] = (uint8_t) (ecc_tmp >> 0);
45193db446aSBoris Brezillon 	ecc[5] = (uint8_t) (ecc_tmp >> 8);
45293db446aSBoris Brezillon 	ecc[6] = (uint8_t) (ecc_tmp >> 16);
45393db446aSBoris Brezillon 	ecc[7] = (uint8_t) (ecc_tmp >> 24);
45493db446aSBoris Brezillon 
4554df6ed4fSMiquel Raynal 	ecc_tmp = readl_relaxed(host->regs_va + ECC3);
45693db446aSBoris Brezillon 	ecc[8] = (uint8_t) (ecc_tmp >> 0);
45793db446aSBoris Brezillon 	ecc[9] = (uint8_t) (ecc_tmp >> 8);
45893db446aSBoris Brezillon 	ecc[10] = (uint8_t) (ecc_tmp >> 16);
45993db446aSBoris Brezillon 	ecc[11] = (uint8_t) (ecc_tmp >> 24);
46093db446aSBoris Brezillon 
4614df6ed4fSMiquel Raynal 	ecc_tmp = readl_relaxed(host->regs_va + STS);
46293db446aSBoris Brezillon 	ecc[12] = (uint8_t) (ecc_tmp >> 16);
46393db446aSBoris Brezillon 
46493db446aSBoris Brezillon 	return 0;
46593db446aSBoris Brezillon }
46693db446aSBoris Brezillon 
46793db446aSBoris Brezillon /*
46893db446aSBoris Brezillon  * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
46993db446aSBoris Brezillon  * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
47093db446aSBoris Brezillon  * max of 1-bit)
47193db446aSBoris Brezillon  */
47293db446aSBoris Brezillon static int fsmc_read_hwecc_ecc1(struct mtd_info *mtd, const uint8_t *data,
47393db446aSBoris Brezillon 				uint8_t *ecc)
47493db446aSBoris Brezillon {
47593db446aSBoris Brezillon 	struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
47693db446aSBoris Brezillon 	uint32_t ecc_tmp;
47793db446aSBoris Brezillon 
4784df6ed4fSMiquel Raynal 	ecc_tmp = readl_relaxed(host->regs_va + ECC1);
47993db446aSBoris Brezillon 	ecc[0] = (uint8_t) (ecc_tmp >> 0);
48093db446aSBoris Brezillon 	ecc[1] = (uint8_t) (ecc_tmp >> 8);
48193db446aSBoris Brezillon 	ecc[2] = (uint8_t) (ecc_tmp >> 16);
48293db446aSBoris Brezillon 
48393db446aSBoris Brezillon 	return 0;
48493db446aSBoris Brezillon }
48593db446aSBoris Brezillon 
48693db446aSBoris Brezillon /* Count the number of 0's in buff upto a max of max_bits */
48793db446aSBoris Brezillon static int count_written_bits(uint8_t *buff, int size, int max_bits)
48893db446aSBoris Brezillon {
48993db446aSBoris Brezillon 	int k, written_bits = 0;
49093db446aSBoris Brezillon 
49193db446aSBoris Brezillon 	for (k = 0; k < size; k++) {
49293db446aSBoris Brezillon 		written_bits += hweight8(~buff[k]);
49393db446aSBoris Brezillon 		if (written_bits > max_bits)
49493db446aSBoris Brezillon 			break;
49593db446aSBoris Brezillon 	}
49693db446aSBoris Brezillon 
49793db446aSBoris Brezillon 	return written_bits;
49893db446aSBoris Brezillon }
49993db446aSBoris Brezillon 
50093db446aSBoris Brezillon static void dma_complete(void *param)
50193db446aSBoris Brezillon {
50293db446aSBoris Brezillon 	struct fsmc_nand_data *host = param;
50393db446aSBoris Brezillon 
50493db446aSBoris Brezillon 	complete(&host->dma_access_complete);
50593db446aSBoris Brezillon }
50693db446aSBoris Brezillon 
50793db446aSBoris Brezillon static int dma_xfer(struct fsmc_nand_data *host, void *buffer, int len,
50893db446aSBoris Brezillon 		enum dma_data_direction direction)
50993db446aSBoris Brezillon {
51093db446aSBoris Brezillon 	struct dma_chan *chan;
51193db446aSBoris Brezillon 	struct dma_device *dma_dev;
51293db446aSBoris Brezillon 	struct dma_async_tx_descriptor *tx;
51393db446aSBoris Brezillon 	dma_addr_t dma_dst, dma_src, dma_addr;
51493db446aSBoris Brezillon 	dma_cookie_t cookie;
51593db446aSBoris Brezillon 	unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
51693db446aSBoris Brezillon 	int ret;
51793db446aSBoris Brezillon 	unsigned long time_left;
51893db446aSBoris Brezillon 
51993db446aSBoris Brezillon 	if (direction == DMA_TO_DEVICE)
52093db446aSBoris Brezillon 		chan = host->write_dma_chan;
52193db446aSBoris Brezillon 	else if (direction == DMA_FROM_DEVICE)
52293db446aSBoris Brezillon 		chan = host->read_dma_chan;
52393db446aSBoris Brezillon 	else
52493db446aSBoris Brezillon 		return -EINVAL;
52593db446aSBoris Brezillon 
52693db446aSBoris Brezillon 	dma_dev = chan->device;
52793db446aSBoris Brezillon 	dma_addr = dma_map_single(dma_dev->dev, buffer, len, direction);
52893db446aSBoris Brezillon 
52993db446aSBoris Brezillon 	if (direction == DMA_TO_DEVICE) {
53093db446aSBoris Brezillon 		dma_src = dma_addr;
53193db446aSBoris Brezillon 		dma_dst = host->data_pa;
53293db446aSBoris Brezillon 	} else {
53393db446aSBoris Brezillon 		dma_src = host->data_pa;
53493db446aSBoris Brezillon 		dma_dst = dma_addr;
53593db446aSBoris Brezillon 	}
53693db446aSBoris Brezillon 
53793db446aSBoris Brezillon 	tx = dma_dev->device_prep_dma_memcpy(chan, dma_dst, dma_src,
53893db446aSBoris Brezillon 			len, flags);
53993db446aSBoris Brezillon 	if (!tx) {
54093db446aSBoris Brezillon 		dev_err(host->dev, "device_prep_dma_memcpy error\n");
54193db446aSBoris Brezillon 		ret = -EIO;
54293db446aSBoris Brezillon 		goto unmap_dma;
54393db446aSBoris Brezillon 	}
54493db446aSBoris Brezillon 
54593db446aSBoris Brezillon 	tx->callback = dma_complete;
54693db446aSBoris Brezillon 	tx->callback_param = host;
54793db446aSBoris Brezillon 	cookie = tx->tx_submit(tx);
54893db446aSBoris Brezillon 
54993db446aSBoris Brezillon 	ret = dma_submit_error(cookie);
55093db446aSBoris Brezillon 	if (ret) {
55193db446aSBoris Brezillon 		dev_err(host->dev, "dma_submit_error %d\n", cookie);
55293db446aSBoris Brezillon 		goto unmap_dma;
55393db446aSBoris Brezillon 	}
55493db446aSBoris Brezillon 
55593db446aSBoris Brezillon 	dma_async_issue_pending(chan);
55693db446aSBoris Brezillon 
55793db446aSBoris Brezillon 	time_left =
55893db446aSBoris Brezillon 	wait_for_completion_timeout(&host->dma_access_complete,
55993db446aSBoris Brezillon 				msecs_to_jiffies(3000));
56093db446aSBoris Brezillon 	if (time_left == 0) {
56193db446aSBoris Brezillon 		dmaengine_terminate_all(chan);
56293db446aSBoris Brezillon 		dev_err(host->dev, "wait_for_completion_timeout\n");
56393db446aSBoris Brezillon 		ret = -ETIMEDOUT;
56493db446aSBoris Brezillon 		goto unmap_dma;
56593db446aSBoris Brezillon 	}
56693db446aSBoris Brezillon 
56793db446aSBoris Brezillon 	ret = 0;
56893db446aSBoris Brezillon 
56993db446aSBoris Brezillon unmap_dma:
57093db446aSBoris Brezillon 	dma_unmap_single(dma_dev->dev, dma_addr, len, direction);
57193db446aSBoris Brezillon 
57293db446aSBoris Brezillon 	return ret;
57393db446aSBoris Brezillon }
57493db446aSBoris Brezillon 
57593db446aSBoris Brezillon /*
57693db446aSBoris Brezillon  * fsmc_write_buf - write buffer to chip
57793db446aSBoris Brezillon  * @mtd:	MTD device structure
57893db446aSBoris Brezillon  * @buf:	data buffer
57993db446aSBoris Brezillon  * @len:	number of bytes to write
58093db446aSBoris Brezillon  */
58193db446aSBoris Brezillon static void fsmc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
58293db446aSBoris Brezillon {
5834df6ed4fSMiquel Raynal 	struct fsmc_nand_data *host  = mtd_to_fsmc(mtd);
58493db446aSBoris Brezillon 	int i;
58593db446aSBoris Brezillon 
58693db446aSBoris Brezillon 	if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
58793db446aSBoris Brezillon 			IS_ALIGNED(len, sizeof(uint32_t))) {
58893db446aSBoris Brezillon 		uint32_t *p = (uint32_t *)buf;
58993db446aSBoris Brezillon 		len = len >> 2;
59093db446aSBoris Brezillon 		for (i = 0; i < len; i++)
5914df6ed4fSMiquel Raynal 			writel_relaxed(p[i], host->data_va);
59293db446aSBoris Brezillon 	} else {
59393db446aSBoris Brezillon 		for (i = 0; i < len; i++)
5944df6ed4fSMiquel Raynal 			writeb_relaxed(buf[i], host->data_va);
59593db446aSBoris Brezillon 	}
59693db446aSBoris Brezillon }
59793db446aSBoris Brezillon 
59893db446aSBoris Brezillon /*
59993db446aSBoris Brezillon  * fsmc_read_buf - read chip data into buffer
60093db446aSBoris Brezillon  * @mtd:	MTD device structure
60193db446aSBoris Brezillon  * @buf:	buffer to store date
60293db446aSBoris Brezillon  * @len:	number of bytes to read
60393db446aSBoris Brezillon  */
60493db446aSBoris Brezillon static void fsmc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
60593db446aSBoris Brezillon {
6064df6ed4fSMiquel Raynal 	struct fsmc_nand_data *host  = mtd_to_fsmc(mtd);
60793db446aSBoris Brezillon 	int i;
60893db446aSBoris Brezillon 
60993db446aSBoris Brezillon 	if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
61093db446aSBoris Brezillon 			IS_ALIGNED(len, sizeof(uint32_t))) {
61193db446aSBoris Brezillon 		uint32_t *p = (uint32_t *)buf;
61293db446aSBoris Brezillon 		len = len >> 2;
61393db446aSBoris Brezillon 		for (i = 0; i < len; i++)
6144df6ed4fSMiquel Raynal 			p[i] = readl_relaxed(host->data_va);
61593db446aSBoris Brezillon 	} else {
61693db446aSBoris Brezillon 		for (i = 0; i < len; i++)
6174df6ed4fSMiquel Raynal 			buf[i] = readb_relaxed(host->data_va);
61893db446aSBoris Brezillon 	}
61993db446aSBoris Brezillon }
62093db446aSBoris Brezillon 
62193db446aSBoris Brezillon /*
62293db446aSBoris Brezillon  * fsmc_read_buf_dma - read chip data into buffer
62393db446aSBoris Brezillon  * @mtd:	MTD device structure
62493db446aSBoris Brezillon  * @buf:	buffer to store date
62593db446aSBoris Brezillon  * @len:	number of bytes to read
62693db446aSBoris Brezillon  */
62793db446aSBoris Brezillon static void fsmc_read_buf_dma(struct mtd_info *mtd, uint8_t *buf, int len)
62893db446aSBoris Brezillon {
62993db446aSBoris Brezillon 	struct fsmc_nand_data *host  = mtd_to_fsmc(mtd);
63093db446aSBoris Brezillon 
63193db446aSBoris Brezillon 	dma_xfer(host, buf, len, DMA_FROM_DEVICE);
63293db446aSBoris Brezillon }
63393db446aSBoris Brezillon 
63493db446aSBoris Brezillon /*
63593db446aSBoris Brezillon  * fsmc_write_buf_dma - write buffer to chip
63693db446aSBoris Brezillon  * @mtd:	MTD device structure
63793db446aSBoris Brezillon  * @buf:	data buffer
63893db446aSBoris Brezillon  * @len:	number of bytes to write
63993db446aSBoris Brezillon  */
64093db446aSBoris Brezillon static void fsmc_write_buf_dma(struct mtd_info *mtd, const uint8_t *buf,
64193db446aSBoris Brezillon 		int len)
64293db446aSBoris Brezillon {
64393db446aSBoris Brezillon 	struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
64493db446aSBoris Brezillon 
64593db446aSBoris Brezillon 	dma_xfer(host, (void *)buf, len, DMA_TO_DEVICE);
64693db446aSBoris Brezillon }
64793db446aSBoris Brezillon 
64893db446aSBoris Brezillon /*
64993db446aSBoris Brezillon  * fsmc_read_page_hwecc
65093db446aSBoris Brezillon  * @mtd:	mtd info structure
65193db446aSBoris Brezillon  * @chip:	nand chip info structure
65293db446aSBoris Brezillon  * @buf:	buffer to store read data
65393db446aSBoris Brezillon  * @oob_required:	caller expects OOB data read to chip->oob_poi
65493db446aSBoris Brezillon  * @page:	page number to read
65593db446aSBoris Brezillon  *
65693db446aSBoris Brezillon  * This routine is needed for fsmc version 8 as reading from NAND chip has to be
65793db446aSBoris Brezillon  * performed in a strict sequence as follows:
65893db446aSBoris Brezillon  * data(512 byte) -> ecc(13 byte)
65993db446aSBoris Brezillon  * After this read, fsmc hardware generates and reports error data bits(up to a
66093db446aSBoris Brezillon  * max of 8 bits)
66193db446aSBoris Brezillon  */
66293db446aSBoris Brezillon static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
66393db446aSBoris Brezillon 				 uint8_t *buf, int oob_required, int page)
66493db446aSBoris Brezillon {
66593db446aSBoris Brezillon 	int i, j, s, stat, eccsize = chip->ecc.size;
66693db446aSBoris Brezillon 	int eccbytes = chip->ecc.bytes;
66793db446aSBoris Brezillon 	int eccsteps = chip->ecc.steps;
66893db446aSBoris Brezillon 	uint8_t *p = buf;
66993db446aSBoris Brezillon 	uint8_t *ecc_calc = chip->ecc.calc_buf;
67093db446aSBoris Brezillon 	uint8_t *ecc_code = chip->ecc.code_buf;
67193db446aSBoris Brezillon 	int off, len, group = 0;
67293db446aSBoris Brezillon 	/*
67393db446aSBoris Brezillon 	 * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
67493db446aSBoris Brezillon 	 * end up reading 14 bytes (7 words) from oob. The local array is
67593db446aSBoris Brezillon 	 * to maintain word alignment
67693db446aSBoris Brezillon 	 */
67793db446aSBoris Brezillon 	uint16_t ecc_oob[7];
67893db446aSBoris Brezillon 	uint8_t *oob = (uint8_t *)&ecc_oob[0];
67993db446aSBoris Brezillon 	unsigned int max_bitflips = 0;
68093db446aSBoris Brezillon 
68193db446aSBoris Brezillon 	for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
68293db446aSBoris Brezillon 		nand_read_page_op(chip, page, s * eccsize, NULL, 0);
68393db446aSBoris Brezillon 		chip->ecc.hwctl(mtd, NAND_ECC_READ);
68493db446aSBoris Brezillon 		chip->read_buf(mtd, p, eccsize);
68593db446aSBoris Brezillon 
68693db446aSBoris Brezillon 		for (j = 0; j < eccbytes;) {
68793db446aSBoris Brezillon 			struct mtd_oob_region oobregion;
68893db446aSBoris Brezillon 			int ret;
68993db446aSBoris Brezillon 
69093db446aSBoris Brezillon 			ret = mtd_ooblayout_ecc(mtd, group++, &oobregion);
69193db446aSBoris Brezillon 			if (ret)
69293db446aSBoris Brezillon 				return ret;
69393db446aSBoris Brezillon 
69493db446aSBoris Brezillon 			off = oobregion.offset;
69593db446aSBoris Brezillon 			len = oobregion.length;
69693db446aSBoris Brezillon 
69793db446aSBoris Brezillon 			/*
69893db446aSBoris Brezillon 			 * length is intentionally kept a higher multiple of 2
69993db446aSBoris Brezillon 			 * to read at least 13 bytes even in case of 16 bit NAND
70093db446aSBoris Brezillon 			 * devices
70193db446aSBoris Brezillon 			 */
70293db446aSBoris Brezillon 			if (chip->options & NAND_BUSWIDTH_16)
70393db446aSBoris Brezillon 				len = roundup(len, 2);
70493db446aSBoris Brezillon 
70593db446aSBoris Brezillon 			nand_read_oob_op(chip, page, off, oob + j, len);
70693db446aSBoris Brezillon 			j += len;
70793db446aSBoris Brezillon 		}
70893db446aSBoris Brezillon 
70993db446aSBoris Brezillon 		memcpy(&ecc_code[i], oob, chip->ecc.bytes);
71093db446aSBoris Brezillon 		chip->ecc.calculate(mtd, p, &ecc_calc[i]);
71193db446aSBoris Brezillon 
71293db446aSBoris Brezillon 		stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
71393db446aSBoris Brezillon 		if (stat < 0) {
71493db446aSBoris Brezillon 			mtd->ecc_stats.failed++;
71593db446aSBoris Brezillon 		} else {
71693db446aSBoris Brezillon 			mtd->ecc_stats.corrected += stat;
71793db446aSBoris Brezillon 			max_bitflips = max_t(unsigned int, max_bitflips, stat);
71893db446aSBoris Brezillon 		}
71993db446aSBoris Brezillon 	}
72093db446aSBoris Brezillon 
72193db446aSBoris Brezillon 	return max_bitflips;
72293db446aSBoris Brezillon }
72393db446aSBoris Brezillon 
72493db446aSBoris Brezillon /*
72593db446aSBoris Brezillon  * fsmc_bch8_correct_data
72693db446aSBoris Brezillon  * @mtd:	mtd info structure
72793db446aSBoris Brezillon  * @dat:	buffer of read data
72893db446aSBoris Brezillon  * @read_ecc:	ecc read from device spare area
72993db446aSBoris Brezillon  * @calc_ecc:	ecc calculated from read data
73093db446aSBoris Brezillon  *
73193db446aSBoris Brezillon  * calc_ecc is a 104 bit information containing maximum of 8 error
73293db446aSBoris Brezillon  * offset informations of 13 bits each in 512 bytes of read data.
73393db446aSBoris Brezillon  */
73493db446aSBoris Brezillon static int fsmc_bch8_correct_data(struct mtd_info *mtd, uint8_t *dat,
73593db446aSBoris Brezillon 			     uint8_t *read_ecc, uint8_t *calc_ecc)
73693db446aSBoris Brezillon {
73793db446aSBoris Brezillon 	struct nand_chip *chip = mtd_to_nand(mtd);
73893db446aSBoris Brezillon 	struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
73993db446aSBoris Brezillon 	uint32_t err_idx[8];
74093db446aSBoris Brezillon 	uint32_t num_err, i;
74193db446aSBoris Brezillon 	uint32_t ecc1, ecc2, ecc3, ecc4;
74293db446aSBoris Brezillon 
7434df6ed4fSMiquel Raynal 	num_err = (readl_relaxed(host->regs_va + STS) >> 10) & 0xF;
74493db446aSBoris Brezillon 
74593db446aSBoris Brezillon 	/* no bit flipping */
74693db446aSBoris Brezillon 	if (likely(num_err == 0))
74793db446aSBoris Brezillon 		return 0;
74893db446aSBoris Brezillon 
74993db446aSBoris Brezillon 	/* too many errors */
75093db446aSBoris Brezillon 	if (unlikely(num_err > 8)) {
75193db446aSBoris Brezillon 		/*
75293db446aSBoris Brezillon 		 * This is a temporary erase check. A newly erased page read
75393db446aSBoris Brezillon 		 * would result in an ecc error because the oob data is also
75493db446aSBoris Brezillon 		 * erased to FF and the calculated ecc for an FF data is not
75593db446aSBoris Brezillon 		 * FF..FF.
75693db446aSBoris Brezillon 		 * This is a workaround to skip performing correction in case
75793db446aSBoris Brezillon 		 * data is FF..FF
75893db446aSBoris Brezillon 		 *
75993db446aSBoris Brezillon 		 * Logic:
76093db446aSBoris Brezillon 		 * For every page, each bit written as 0 is counted until these
76193db446aSBoris Brezillon 		 * number of bits are greater than 8 (the maximum correction
76293db446aSBoris Brezillon 		 * capability of FSMC for each 512 + 13 bytes)
76393db446aSBoris Brezillon 		 */
76493db446aSBoris Brezillon 
76593db446aSBoris Brezillon 		int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
76693db446aSBoris Brezillon 		int bits_data = count_written_bits(dat, chip->ecc.size, 8);
76793db446aSBoris Brezillon 
76893db446aSBoris Brezillon 		if ((bits_ecc + bits_data) <= 8) {
76993db446aSBoris Brezillon 			if (bits_data)
77093db446aSBoris Brezillon 				memset(dat, 0xff, chip->ecc.size);
77193db446aSBoris Brezillon 			return bits_data;
77293db446aSBoris Brezillon 		}
77393db446aSBoris Brezillon 
77493db446aSBoris Brezillon 		return -EBADMSG;
77593db446aSBoris Brezillon 	}
77693db446aSBoris Brezillon 
77793db446aSBoris Brezillon 	/*
77893db446aSBoris Brezillon 	 * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
77993db446aSBoris Brezillon 	 * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
78093db446aSBoris Brezillon 	 *
78193db446aSBoris Brezillon 	 * calc_ecc is a 104 bit information containing maximum of 8 error
78293db446aSBoris Brezillon 	 * offset informations of 13 bits each. calc_ecc is copied into a
78393db446aSBoris Brezillon 	 * uint64_t array and error offset indexes are populated in err_idx
78493db446aSBoris Brezillon 	 * array
78593db446aSBoris Brezillon 	 */
7864df6ed4fSMiquel Raynal 	ecc1 = readl_relaxed(host->regs_va + ECC1);
7874df6ed4fSMiquel Raynal 	ecc2 = readl_relaxed(host->regs_va + ECC2);
7884df6ed4fSMiquel Raynal 	ecc3 = readl_relaxed(host->regs_va + ECC3);
7894df6ed4fSMiquel Raynal 	ecc4 = readl_relaxed(host->regs_va + STS);
79093db446aSBoris Brezillon 
79193db446aSBoris Brezillon 	err_idx[0] = (ecc1 >> 0) & 0x1FFF;
79293db446aSBoris Brezillon 	err_idx[1] = (ecc1 >> 13) & 0x1FFF;
79393db446aSBoris Brezillon 	err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
79493db446aSBoris Brezillon 	err_idx[3] = (ecc2 >> 7) & 0x1FFF;
79593db446aSBoris Brezillon 	err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
79693db446aSBoris Brezillon 	err_idx[5] = (ecc3 >> 1) & 0x1FFF;
79793db446aSBoris Brezillon 	err_idx[6] = (ecc3 >> 14) & 0x1FFF;
79893db446aSBoris Brezillon 	err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
79993db446aSBoris Brezillon 
80093db446aSBoris Brezillon 	i = 0;
80193db446aSBoris Brezillon 	while (num_err--) {
80293db446aSBoris Brezillon 		change_bit(0, (unsigned long *)&err_idx[i]);
80393db446aSBoris Brezillon 		change_bit(1, (unsigned long *)&err_idx[i]);
80493db446aSBoris Brezillon 
80593db446aSBoris Brezillon 		if (err_idx[i] < chip->ecc.size * 8) {
80693db446aSBoris Brezillon 			change_bit(err_idx[i], (unsigned long *)dat);
80793db446aSBoris Brezillon 			i++;
80893db446aSBoris Brezillon 		}
80993db446aSBoris Brezillon 	}
81093db446aSBoris Brezillon 	return i;
81193db446aSBoris Brezillon }
81293db446aSBoris Brezillon 
81393db446aSBoris Brezillon static bool filter(struct dma_chan *chan, void *slave)
81493db446aSBoris Brezillon {
81593db446aSBoris Brezillon 	chan->private = slave;
81693db446aSBoris Brezillon 	return true;
81793db446aSBoris Brezillon }
81893db446aSBoris Brezillon 
81993db446aSBoris Brezillon static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
82093db446aSBoris Brezillon 				     struct fsmc_nand_data *host,
82193db446aSBoris Brezillon 				     struct nand_chip *nand)
82293db446aSBoris Brezillon {
82393db446aSBoris Brezillon 	struct device_node *np = pdev->dev.of_node;
82493db446aSBoris Brezillon 	u32 val;
82593db446aSBoris Brezillon 	int ret;
82693db446aSBoris Brezillon 
82793db446aSBoris Brezillon 	nand->options = 0;
82893db446aSBoris Brezillon 
82993db446aSBoris Brezillon 	if (!of_property_read_u32(np, "bank-width", &val)) {
83093db446aSBoris Brezillon 		if (val == 2) {
83193db446aSBoris Brezillon 			nand->options |= NAND_BUSWIDTH_16;
83293db446aSBoris Brezillon 		} else if (val != 1) {
83393db446aSBoris Brezillon 			dev_err(&pdev->dev, "invalid bank-width %u\n", val);
83493db446aSBoris Brezillon 			return -EINVAL;
83593db446aSBoris Brezillon 		}
83693db446aSBoris Brezillon 	}
83793db446aSBoris Brezillon 
83893db446aSBoris Brezillon 	if (of_get_property(np, "nand-skip-bbtscan", NULL))
83993db446aSBoris Brezillon 		nand->options |= NAND_SKIP_BBTSCAN;
84093db446aSBoris Brezillon 
84193db446aSBoris Brezillon 	host->dev_timings = devm_kzalloc(&pdev->dev,
84293db446aSBoris Brezillon 				sizeof(*host->dev_timings), GFP_KERNEL);
84393db446aSBoris Brezillon 	if (!host->dev_timings)
84493db446aSBoris Brezillon 		return -ENOMEM;
84593db446aSBoris Brezillon 	ret = of_property_read_u8_array(np, "timings", (u8 *)host->dev_timings,
84693db446aSBoris Brezillon 						sizeof(*host->dev_timings));
84793db446aSBoris Brezillon 	if (ret)
84893db446aSBoris Brezillon 		host->dev_timings = NULL;
84993db446aSBoris Brezillon 
85093db446aSBoris Brezillon 	/* Set default NAND bank to 0 */
85193db446aSBoris Brezillon 	host->bank = 0;
85293db446aSBoris Brezillon 	if (!of_property_read_u32(np, "bank", &val)) {
85393db446aSBoris Brezillon 		if (val > 3) {
85493db446aSBoris Brezillon 			dev_err(&pdev->dev, "invalid bank %u\n", val);
85593db446aSBoris Brezillon 			return -EINVAL;
85693db446aSBoris Brezillon 		}
85793db446aSBoris Brezillon 		host->bank = val;
85893db446aSBoris Brezillon 	}
85993db446aSBoris Brezillon 	return 0;
86093db446aSBoris Brezillon }
86193db446aSBoris Brezillon 
86293db446aSBoris Brezillon /*
86393db446aSBoris Brezillon  * fsmc_nand_probe - Probe function
86493db446aSBoris Brezillon  * @pdev:       platform device structure
86593db446aSBoris Brezillon  */
86693db446aSBoris Brezillon static int __init fsmc_nand_probe(struct platform_device *pdev)
86793db446aSBoris Brezillon {
86893db446aSBoris Brezillon 	struct fsmc_nand_data *host;
86993db446aSBoris Brezillon 	struct mtd_info *mtd;
87093db446aSBoris Brezillon 	struct nand_chip *nand;
87193db446aSBoris Brezillon 	struct resource *res;
8724df6ed4fSMiquel Raynal 	void __iomem *base;
87393db446aSBoris Brezillon 	dma_cap_mask_t mask;
87493db446aSBoris Brezillon 	int ret = 0;
87593db446aSBoris Brezillon 	u32 pid;
87693db446aSBoris Brezillon 	int i;
87793db446aSBoris Brezillon 
87893db446aSBoris Brezillon 	/* Allocate memory for the device structure (and zero it) */
87993db446aSBoris Brezillon 	host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
88093db446aSBoris Brezillon 	if (!host)
88193db446aSBoris Brezillon 		return -ENOMEM;
88293db446aSBoris Brezillon 
88393db446aSBoris Brezillon 	nand = &host->nand;
88493db446aSBoris Brezillon 
88593db446aSBoris Brezillon 	ret = fsmc_nand_probe_config_dt(pdev, host, nand);
88693db446aSBoris Brezillon 	if (ret)
88793db446aSBoris Brezillon 		return ret;
88893db446aSBoris Brezillon 
88993db446aSBoris Brezillon 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
89093db446aSBoris Brezillon 	host->data_va = devm_ioremap_resource(&pdev->dev, res);
89193db446aSBoris Brezillon 	if (IS_ERR(host->data_va))
89293db446aSBoris Brezillon 		return PTR_ERR(host->data_va);
89393db446aSBoris Brezillon 
89493db446aSBoris Brezillon 	host->data_pa = (dma_addr_t)res->start;
89593db446aSBoris Brezillon 
89693db446aSBoris Brezillon 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_addr");
89793db446aSBoris Brezillon 	host->addr_va = devm_ioremap_resource(&pdev->dev, res);
89893db446aSBoris Brezillon 	if (IS_ERR(host->addr_va))
89993db446aSBoris Brezillon 		return PTR_ERR(host->addr_va);
90093db446aSBoris Brezillon 
90193db446aSBoris Brezillon 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_cmd");
90293db446aSBoris Brezillon 	host->cmd_va = devm_ioremap_resource(&pdev->dev, res);
90393db446aSBoris Brezillon 	if (IS_ERR(host->cmd_va))
90493db446aSBoris Brezillon 		return PTR_ERR(host->cmd_va);
90593db446aSBoris Brezillon 
90693db446aSBoris Brezillon 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
9074df6ed4fSMiquel Raynal 	base = devm_ioremap_resource(&pdev->dev, res);
9084df6ed4fSMiquel Raynal 	if (IS_ERR(base))
9094df6ed4fSMiquel Raynal 		return PTR_ERR(base);
9104df6ed4fSMiquel Raynal 
9114df6ed4fSMiquel Raynal 	host->regs_va = base + FSMC_NOR_REG_SIZE +
9124df6ed4fSMiquel Raynal 		(host->bank * FSMC_NAND_BANK_SZ);
91393db446aSBoris Brezillon 
91493db446aSBoris Brezillon 	host->clk = devm_clk_get(&pdev->dev, NULL);
91593db446aSBoris Brezillon 	if (IS_ERR(host->clk)) {
91693db446aSBoris Brezillon 		dev_err(&pdev->dev, "failed to fetch block clock\n");
91793db446aSBoris Brezillon 		return PTR_ERR(host->clk);
91893db446aSBoris Brezillon 	}
91993db446aSBoris Brezillon 
92093db446aSBoris Brezillon 	ret = clk_prepare_enable(host->clk);
92193db446aSBoris Brezillon 	if (ret)
92293db446aSBoris Brezillon 		return ret;
92393db446aSBoris Brezillon 
92493db446aSBoris Brezillon 	/*
92593db446aSBoris Brezillon 	 * This device ID is actually a common AMBA ID as used on the
92693db446aSBoris Brezillon 	 * AMBA PrimeCell bus. However it is not a PrimeCell.
92793db446aSBoris Brezillon 	 */
92893db446aSBoris Brezillon 	for (pid = 0, i = 0; i < 4; i++)
9294df6ed4fSMiquel Raynal 		pid |= (readl(base + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
93093db446aSBoris Brezillon 	host->pid = pid;
93193db446aSBoris Brezillon 	dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
93293db446aSBoris Brezillon 		 "revision %02x, config %02x\n",
93393db446aSBoris Brezillon 		 AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
93493db446aSBoris Brezillon 		 AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
93593db446aSBoris Brezillon 
93693db446aSBoris Brezillon 	host->dev = &pdev->dev;
93793db446aSBoris Brezillon 
93893db446aSBoris Brezillon 	if (host->mode == USE_DMA_ACCESS)
93993db446aSBoris Brezillon 		init_completion(&host->dma_access_complete);
94093db446aSBoris Brezillon 
94193db446aSBoris Brezillon 	/* Link all private pointers */
94293db446aSBoris Brezillon 	mtd = nand_to_mtd(&host->nand);
94393db446aSBoris Brezillon 	nand_set_controller_data(nand, host);
94493db446aSBoris Brezillon 	nand_set_flash_node(nand, pdev->dev.of_node);
94593db446aSBoris Brezillon 
94693db446aSBoris Brezillon 	mtd->dev.parent = &pdev->dev;
94793db446aSBoris Brezillon 	nand->IO_ADDR_R = host->data_va;
94893db446aSBoris Brezillon 	nand->IO_ADDR_W = host->data_va;
94993db446aSBoris Brezillon 	nand->cmd_ctrl = fsmc_cmd_ctrl;
95093db446aSBoris Brezillon 	nand->chip_delay = 30;
95193db446aSBoris Brezillon 
95293db446aSBoris Brezillon 	/*
95393db446aSBoris Brezillon 	 * Setup default ECC mode. nand_dt_init() called from nand_scan_ident()
95493db446aSBoris Brezillon 	 * can overwrite this value if the DT provides a different value.
95593db446aSBoris Brezillon 	 */
95693db446aSBoris Brezillon 	nand->ecc.mode = NAND_ECC_HW;
95793db446aSBoris Brezillon 	nand->ecc.hwctl = fsmc_enable_hwecc;
95893db446aSBoris Brezillon 	nand->ecc.size = 512;
95993db446aSBoris Brezillon 	nand->badblockbits = 7;
96093db446aSBoris Brezillon 
96193db446aSBoris Brezillon 	switch (host->mode) {
96293db446aSBoris Brezillon 	case USE_DMA_ACCESS:
96393db446aSBoris Brezillon 		dma_cap_zero(mask);
96493db446aSBoris Brezillon 		dma_cap_set(DMA_MEMCPY, mask);
96593db446aSBoris Brezillon 		host->read_dma_chan = dma_request_channel(mask, filter, NULL);
96693db446aSBoris Brezillon 		if (!host->read_dma_chan) {
96793db446aSBoris Brezillon 			dev_err(&pdev->dev, "Unable to get read dma channel\n");
96893db446aSBoris Brezillon 			goto err_req_read_chnl;
96993db446aSBoris Brezillon 		}
97093db446aSBoris Brezillon 		host->write_dma_chan = dma_request_channel(mask, filter, NULL);
97193db446aSBoris Brezillon 		if (!host->write_dma_chan) {
97293db446aSBoris Brezillon 			dev_err(&pdev->dev, "Unable to get write dma channel\n");
97393db446aSBoris Brezillon 			goto err_req_write_chnl;
97493db446aSBoris Brezillon 		}
97593db446aSBoris Brezillon 		nand->read_buf = fsmc_read_buf_dma;
97693db446aSBoris Brezillon 		nand->write_buf = fsmc_write_buf_dma;
97793db446aSBoris Brezillon 		break;
97893db446aSBoris Brezillon 
97993db446aSBoris Brezillon 	default:
98093db446aSBoris Brezillon 	case USE_WORD_ACCESS:
98193db446aSBoris Brezillon 		nand->read_buf = fsmc_read_buf;
98293db446aSBoris Brezillon 		nand->write_buf = fsmc_write_buf;
98393db446aSBoris Brezillon 		break;
98493db446aSBoris Brezillon 	}
98593db446aSBoris Brezillon 
98693db446aSBoris Brezillon 	if (host->dev_timings)
98793db446aSBoris Brezillon 		fsmc_nand_setup(host, host->dev_timings);
98893db446aSBoris Brezillon 	else
98993db446aSBoris Brezillon 		nand->setup_data_interface = fsmc_setup_data_interface;
99093db446aSBoris Brezillon 
99193db446aSBoris Brezillon 	if (AMBA_REV_BITS(host->pid) >= 8) {
99293db446aSBoris Brezillon 		nand->ecc.read_page = fsmc_read_page_hwecc;
99393db446aSBoris Brezillon 		nand->ecc.calculate = fsmc_read_hwecc_ecc4;
99493db446aSBoris Brezillon 		nand->ecc.correct = fsmc_bch8_correct_data;
99593db446aSBoris Brezillon 		nand->ecc.bytes = 13;
99693db446aSBoris Brezillon 		nand->ecc.strength = 8;
99793db446aSBoris Brezillon 	}
99893db446aSBoris Brezillon 
99993db446aSBoris Brezillon 	/*
100093db446aSBoris Brezillon 	 * Scan to find existence of the device
100193db446aSBoris Brezillon 	 */
100293db446aSBoris Brezillon 	ret = nand_scan_ident(mtd, 1, NULL);
100393db446aSBoris Brezillon 	if (ret) {
100493db446aSBoris Brezillon 		dev_err(&pdev->dev, "No NAND Device found!\n");
100593db446aSBoris Brezillon 		goto err_scan_ident;
100693db446aSBoris Brezillon 	}
100793db446aSBoris Brezillon 
100893db446aSBoris Brezillon 	if (AMBA_REV_BITS(host->pid) >= 8) {
100993db446aSBoris Brezillon 		switch (mtd->oobsize) {
101093db446aSBoris Brezillon 		case 16:
101193db446aSBoris Brezillon 		case 64:
101293db446aSBoris Brezillon 		case 128:
101393db446aSBoris Brezillon 		case 224:
101493db446aSBoris Brezillon 		case 256:
101593db446aSBoris Brezillon 			break;
101693db446aSBoris Brezillon 		default:
101793db446aSBoris Brezillon 			dev_warn(&pdev->dev, "No oob scheme defined for oobsize %d\n",
101893db446aSBoris Brezillon 				 mtd->oobsize);
101993db446aSBoris Brezillon 			ret = -EINVAL;
102093db446aSBoris Brezillon 			goto err_probe;
102193db446aSBoris Brezillon 		}
102293db446aSBoris Brezillon 
102393db446aSBoris Brezillon 		mtd_set_ooblayout(mtd, &fsmc_ecc4_ooblayout_ops);
102493db446aSBoris Brezillon 	} else {
102593db446aSBoris Brezillon 		switch (nand->ecc.mode) {
102693db446aSBoris Brezillon 		case NAND_ECC_HW:
102793db446aSBoris Brezillon 			dev_info(&pdev->dev, "Using 1-bit HW ECC scheme\n");
102893db446aSBoris Brezillon 			nand->ecc.calculate = fsmc_read_hwecc_ecc1;
102993db446aSBoris Brezillon 			nand->ecc.correct = nand_correct_data;
103093db446aSBoris Brezillon 			nand->ecc.bytes = 3;
103193db446aSBoris Brezillon 			nand->ecc.strength = 1;
103293db446aSBoris Brezillon 			break;
103393db446aSBoris Brezillon 
103493db446aSBoris Brezillon 		case NAND_ECC_SOFT:
103593db446aSBoris Brezillon 			if (nand->ecc.algo == NAND_ECC_BCH) {
103693db446aSBoris Brezillon 				dev_info(&pdev->dev, "Using 4-bit SW BCH ECC scheme\n");
103793db446aSBoris Brezillon 				break;
103893db446aSBoris Brezillon 			}
103993db446aSBoris Brezillon 
104093db446aSBoris Brezillon 		case NAND_ECC_ON_DIE:
104193db446aSBoris Brezillon 			break;
104293db446aSBoris Brezillon 
104393db446aSBoris Brezillon 		default:
104493db446aSBoris Brezillon 			dev_err(&pdev->dev, "Unsupported ECC mode!\n");
104593db446aSBoris Brezillon 			goto err_probe;
104693db446aSBoris Brezillon 		}
104793db446aSBoris Brezillon 
104893db446aSBoris Brezillon 		/*
104993db446aSBoris Brezillon 		 * Don't set layout for BCH4 SW ECC. This will be
105093db446aSBoris Brezillon 		 * generated later in nand_bch_init() later.
105193db446aSBoris Brezillon 		 */
105293db446aSBoris Brezillon 		if (nand->ecc.mode == NAND_ECC_HW) {
105393db446aSBoris Brezillon 			switch (mtd->oobsize) {
105493db446aSBoris Brezillon 			case 16:
105593db446aSBoris Brezillon 			case 64:
105693db446aSBoris Brezillon 			case 128:
105793db446aSBoris Brezillon 				mtd_set_ooblayout(mtd,
105893db446aSBoris Brezillon 						  &fsmc_ecc1_ooblayout_ops);
105993db446aSBoris Brezillon 				break;
106093db446aSBoris Brezillon 			default:
106193db446aSBoris Brezillon 				dev_warn(&pdev->dev,
106293db446aSBoris Brezillon 					 "No oob scheme defined for oobsize %d\n",
106393db446aSBoris Brezillon 					 mtd->oobsize);
106493db446aSBoris Brezillon 				ret = -EINVAL;
106593db446aSBoris Brezillon 				goto err_probe;
106693db446aSBoris Brezillon 			}
106793db446aSBoris Brezillon 		}
106893db446aSBoris Brezillon 	}
106993db446aSBoris Brezillon 
107093db446aSBoris Brezillon 	/* Second stage of scan to fill MTD data-structures */
107193db446aSBoris Brezillon 	ret = nand_scan_tail(mtd);
107293db446aSBoris Brezillon 	if (ret)
107393db446aSBoris Brezillon 		goto err_probe;
107493db446aSBoris Brezillon 
107593db446aSBoris Brezillon 	mtd->name = "nand";
107693db446aSBoris Brezillon 	ret = mtd_device_register(mtd, NULL, 0);
107793db446aSBoris Brezillon 	if (ret)
107893db446aSBoris Brezillon 		goto err_probe;
107993db446aSBoris Brezillon 
108093db446aSBoris Brezillon 	platform_set_drvdata(pdev, host);
108193db446aSBoris Brezillon 	dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
108293db446aSBoris Brezillon 	return 0;
108393db446aSBoris Brezillon 
108493db446aSBoris Brezillon err_probe:
108593db446aSBoris Brezillon err_scan_ident:
108693db446aSBoris Brezillon 	if (host->mode == USE_DMA_ACCESS)
108793db446aSBoris Brezillon 		dma_release_channel(host->write_dma_chan);
108893db446aSBoris Brezillon err_req_write_chnl:
108993db446aSBoris Brezillon 	if (host->mode == USE_DMA_ACCESS)
109093db446aSBoris Brezillon 		dma_release_channel(host->read_dma_chan);
109193db446aSBoris Brezillon err_req_read_chnl:
109293db446aSBoris Brezillon 	clk_disable_unprepare(host->clk);
109393db446aSBoris Brezillon 	return ret;
109493db446aSBoris Brezillon }
109593db446aSBoris Brezillon 
109693db446aSBoris Brezillon /*
109793db446aSBoris Brezillon  * Clean up routine
109893db446aSBoris Brezillon  */
109993db446aSBoris Brezillon static int fsmc_nand_remove(struct platform_device *pdev)
110093db446aSBoris Brezillon {
110193db446aSBoris Brezillon 	struct fsmc_nand_data *host = platform_get_drvdata(pdev);
110293db446aSBoris Brezillon 
110393db446aSBoris Brezillon 	if (host) {
110493db446aSBoris Brezillon 		nand_release(nand_to_mtd(&host->nand));
110593db446aSBoris Brezillon 
110693db446aSBoris Brezillon 		if (host->mode == USE_DMA_ACCESS) {
110793db446aSBoris Brezillon 			dma_release_channel(host->write_dma_chan);
110893db446aSBoris Brezillon 			dma_release_channel(host->read_dma_chan);
110993db446aSBoris Brezillon 		}
111093db446aSBoris Brezillon 		clk_disable_unprepare(host->clk);
111193db446aSBoris Brezillon 	}
111293db446aSBoris Brezillon 
111393db446aSBoris Brezillon 	return 0;
111493db446aSBoris Brezillon }
111593db446aSBoris Brezillon 
111693db446aSBoris Brezillon #ifdef CONFIG_PM_SLEEP
111793db446aSBoris Brezillon static int fsmc_nand_suspend(struct device *dev)
111893db446aSBoris Brezillon {
111993db446aSBoris Brezillon 	struct fsmc_nand_data *host = dev_get_drvdata(dev);
112093db446aSBoris Brezillon 	if (host)
112193db446aSBoris Brezillon 		clk_disable_unprepare(host->clk);
112293db446aSBoris Brezillon 	return 0;
112393db446aSBoris Brezillon }
112493db446aSBoris Brezillon 
112593db446aSBoris Brezillon static int fsmc_nand_resume(struct device *dev)
112693db446aSBoris Brezillon {
112793db446aSBoris Brezillon 	struct fsmc_nand_data *host = dev_get_drvdata(dev);
112893db446aSBoris Brezillon 	if (host) {
112993db446aSBoris Brezillon 		clk_prepare_enable(host->clk);
113093db446aSBoris Brezillon 		if (host->dev_timings)
113193db446aSBoris Brezillon 			fsmc_nand_setup(host, host->dev_timings);
113293db446aSBoris Brezillon 	}
113393db446aSBoris Brezillon 	return 0;
113493db446aSBoris Brezillon }
113593db446aSBoris Brezillon #endif
113693db446aSBoris Brezillon 
113793db446aSBoris Brezillon static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
113893db446aSBoris Brezillon 
113993db446aSBoris Brezillon static const struct of_device_id fsmc_nand_id_table[] = {
114093db446aSBoris Brezillon 	{ .compatible = "st,spear600-fsmc-nand" },
114193db446aSBoris Brezillon 	{ .compatible = "stericsson,fsmc-nand" },
114293db446aSBoris Brezillon 	{}
114393db446aSBoris Brezillon };
114493db446aSBoris Brezillon MODULE_DEVICE_TABLE(of, fsmc_nand_id_table);
114593db446aSBoris Brezillon 
114693db446aSBoris Brezillon static struct platform_driver fsmc_nand_driver = {
114793db446aSBoris Brezillon 	.remove = fsmc_nand_remove,
114893db446aSBoris Brezillon 	.driver = {
114993db446aSBoris Brezillon 		.name = "fsmc-nand",
115093db446aSBoris Brezillon 		.of_match_table = fsmc_nand_id_table,
115193db446aSBoris Brezillon 		.pm = &fsmc_nand_pm_ops,
115293db446aSBoris Brezillon 	},
115393db446aSBoris Brezillon };
115493db446aSBoris Brezillon 
115593db446aSBoris Brezillon module_platform_driver_probe(fsmc_nand_driver, fsmc_nand_probe);
115693db446aSBoris Brezillon 
115793db446aSBoris Brezillon MODULE_LICENSE("GPL");
115893db446aSBoris Brezillon MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
115993db446aSBoris Brezillon MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");
1160