xref: /openbmc/linux/drivers/mtd/nand/raw/gpmi-nand/gpmi-nand.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1b04a3fe3SFabio Estevam // SPDX-License-Identifier: GPL-2.0+
293db446aSBoris Brezillon /*
393db446aSBoris Brezillon  * Freescale GPMI NAND Flash Driver
493db446aSBoris Brezillon  *
593db446aSBoris Brezillon  * Copyright (C) 2010-2015 Freescale Semiconductor, Inc.
693db446aSBoris Brezillon  * Copyright (C) 2008 Embedded Alley Solutions, Inc.
793db446aSBoris Brezillon  */
893db446aSBoris Brezillon #include <linux/clk.h>
93045f8e3SSascha Hauer #include <linux/delay.h>
1093db446aSBoris Brezillon #include <linux/slab.h>
1193db446aSBoris Brezillon #include <linux/sched/task_stack.h>
1293db446aSBoris Brezillon #include <linux/interrupt.h>
1393db446aSBoris Brezillon #include <linux/module.h>
1493db446aSBoris Brezillon #include <linux/mtd/partitions.h>
1593db446aSBoris Brezillon #include <linux/of.h>
16*c2fc6b69SRob Herring #include <linux/platform_device.h>
1704141468SSascha Hauer #include <linux/pm_runtime.h>
18e0ddaab7SSascha Hauer #include <linux/dma/mxs-dma.h>
1993db446aSBoris Brezillon #include "gpmi-nand.h"
203045f8e3SSascha Hauer #include "gpmi-regs.h"
2193db446aSBoris Brezillon #include "bch-regs.h"
2293db446aSBoris Brezillon 
2393db446aSBoris Brezillon /* Resource names for the GPMI NAND driver. */
2493db446aSBoris Brezillon #define GPMI_NAND_GPMI_REGS_ADDR_RES_NAME  "gpmi-nand"
2593db446aSBoris Brezillon #define GPMI_NAND_BCH_REGS_ADDR_RES_NAME   "bch"
2693db446aSBoris Brezillon #define GPMI_NAND_BCH_INTERRUPT_RES_NAME   "bch"
2793db446aSBoris Brezillon 
283045f8e3SSascha Hauer /* Converts time to clock cycles */
293045f8e3SSascha Hauer #define TO_CYCLES(duration, period) DIV_ROUND_UP_ULL(duration, period)
303045f8e3SSascha Hauer 
313045f8e3SSascha Hauer #define MXS_SET_ADDR		0x4
323045f8e3SSascha Hauer #define MXS_CLR_ADDR		0x8
333045f8e3SSascha Hauer /*
343045f8e3SSascha Hauer  * Clear the bit and poll it cleared.  This is usually called with
353045f8e3SSascha Hauer  * a reset address and mask being either SFTRST(bit 31) or CLKGATE
363045f8e3SSascha Hauer  * (bit 30).
373045f8e3SSascha Hauer  */
clear_poll_bit(void __iomem * addr,u32 mask)383045f8e3SSascha Hauer static int clear_poll_bit(void __iomem *addr, u32 mask)
393045f8e3SSascha Hauer {
403045f8e3SSascha Hauer 	int timeout = 0x400;
413045f8e3SSascha Hauer 
423045f8e3SSascha Hauer 	/* clear the bit */
433045f8e3SSascha Hauer 	writel(mask, addr + MXS_CLR_ADDR);
4493db446aSBoris Brezillon 
4593db446aSBoris Brezillon 	/*
463045f8e3SSascha Hauer 	 * SFTRST needs 3 GPMI clocks to settle, the reference manual
473045f8e3SSascha Hauer 	 * recommends to wait 1us.
4893db446aSBoris Brezillon 	 */
493045f8e3SSascha Hauer 	udelay(1);
503045f8e3SSascha Hauer 
513045f8e3SSascha Hauer 	/* poll the bit becoming clear */
523045f8e3SSascha Hauer 	while ((readl(addr) & mask) && --timeout)
533045f8e3SSascha Hauer 		/* nothing */;
543045f8e3SSascha Hauer 
553045f8e3SSascha Hauer 	return !timeout;
563045f8e3SSascha Hauer }
573045f8e3SSascha Hauer 
583045f8e3SSascha Hauer #define MODULE_CLKGATE		(1 << 30)
593045f8e3SSascha Hauer #define MODULE_SFTRST		(1 << 31)
603045f8e3SSascha Hauer /*
613045f8e3SSascha Hauer  * The current mxs_reset_block() will do two things:
623045f8e3SSascha Hauer  *  [1] enable the module.
633045f8e3SSascha Hauer  *  [2] reset the module.
643045f8e3SSascha Hauer  *
653045f8e3SSascha Hauer  * In most of the cases, it's ok.
663045f8e3SSascha Hauer  * But in MX23, there is a hardware bug in the BCH block (see erratum #2847).
673045f8e3SSascha Hauer  * If you try to soft reset the BCH block, it becomes unusable until
683045f8e3SSascha Hauer  * the next hard reset. This case occurs in the NAND boot mode. When the board
693045f8e3SSascha Hauer  * boots by NAND, the ROM of the chip will initialize the BCH blocks itself.
703045f8e3SSascha Hauer  * So If the driver tries to reset the BCH again, the BCH will not work anymore.
713045f8e3SSascha Hauer  * You will see a DMA timeout in this case. The bug has been fixed
723045f8e3SSascha Hauer  * in the following chips, such as MX28.
733045f8e3SSascha Hauer  *
743045f8e3SSascha Hauer  * To avoid this bug, just add a new parameter `just_enable` for
753045f8e3SSascha Hauer  * the mxs_reset_block(), and rewrite it here.
763045f8e3SSascha Hauer  */
gpmi_reset_block(void __iomem * reset_addr,bool just_enable)773045f8e3SSascha Hauer static int gpmi_reset_block(void __iomem *reset_addr, bool just_enable)
7893db446aSBoris Brezillon {
793045f8e3SSascha Hauer 	int ret;
803045f8e3SSascha Hauer 	int timeout = 0x400;
8193db446aSBoris Brezillon 
823045f8e3SSascha Hauer 	/* clear and poll SFTRST */
833045f8e3SSascha Hauer 	ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
843045f8e3SSascha Hauer 	if (unlikely(ret))
853045f8e3SSascha Hauer 		goto error;
8693db446aSBoris Brezillon 
873045f8e3SSascha Hauer 	/* clear CLKGATE */
883045f8e3SSascha Hauer 	writel(MODULE_CLKGATE, reset_addr + MXS_CLR_ADDR);
893045f8e3SSascha Hauer 
903045f8e3SSascha Hauer 	if (!just_enable) {
913045f8e3SSascha Hauer 		/* set SFTRST to reset the block */
923045f8e3SSascha Hauer 		writel(MODULE_SFTRST, reset_addr + MXS_SET_ADDR);
933045f8e3SSascha Hauer 		udelay(1);
943045f8e3SSascha Hauer 
953045f8e3SSascha Hauer 		/* poll CLKGATE becoming set */
963045f8e3SSascha Hauer 		while ((!(readl(reset_addr) & MODULE_CLKGATE)) && --timeout)
973045f8e3SSascha Hauer 			/* nothing */;
983045f8e3SSascha Hauer 		if (unlikely(!timeout))
993045f8e3SSascha Hauer 			goto error;
1003045f8e3SSascha Hauer 	}
1013045f8e3SSascha Hauer 
1023045f8e3SSascha Hauer 	/* clear and poll SFTRST */
1033045f8e3SSascha Hauer 	ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
1043045f8e3SSascha Hauer 	if (unlikely(ret))
1053045f8e3SSascha Hauer 		goto error;
1063045f8e3SSascha Hauer 
1073045f8e3SSascha Hauer 	/* clear and poll CLKGATE */
1083045f8e3SSascha Hauer 	ret = clear_poll_bit(reset_addr, MODULE_CLKGATE);
1093045f8e3SSascha Hauer 	if (unlikely(ret))
1103045f8e3SSascha Hauer 		goto error;
11193db446aSBoris Brezillon 
11293db446aSBoris Brezillon 	return 0;
1133045f8e3SSascha Hauer 
1143045f8e3SSascha Hauer error:
1153045f8e3SSascha Hauer 	pr_err("%s(%p): module reset timeout\n", __func__, reset_addr);
1163045f8e3SSascha Hauer 	return -ETIMEDOUT;
11793db446aSBoris Brezillon }
11893db446aSBoris Brezillon 
__gpmi_enable_clk(struct gpmi_nand_data * this,bool v)1193045f8e3SSascha Hauer static int __gpmi_enable_clk(struct gpmi_nand_data *this, bool v)
12093db446aSBoris Brezillon {
1213045f8e3SSascha Hauer 	struct clk *clk;
1223045f8e3SSascha Hauer 	int ret;
1233045f8e3SSascha Hauer 	int i;
12493db446aSBoris Brezillon 
1253045f8e3SSascha Hauer 	for (i = 0; i < GPMI_CLK_MAX; i++) {
1263045f8e3SSascha Hauer 		clk = this->resources.clock[i];
1273045f8e3SSascha Hauer 		if (!clk)
1283045f8e3SSascha Hauer 			break;
12993db446aSBoris Brezillon 
1303045f8e3SSascha Hauer 		if (v) {
1313045f8e3SSascha Hauer 			ret = clk_prepare_enable(clk);
1323045f8e3SSascha Hauer 			if (ret)
1333045f8e3SSascha Hauer 				goto err_clk;
1343045f8e3SSascha Hauer 		} else {
1353045f8e3SSascha Hauer 			clk_disable_unprepare(clk);
13693db446aSBoris Brezillon 		}
1373045f8e3SSascha Hauer 	}
13893db446aSBoris Brezillon 	return 0;
1393045f8e3SSascha Hauer 
1403045f8e3SSascha Hauer err_clk:
1413045f8e3SSascha Hauer 	for (; i > 0; i--)
1423045f8e3SSascha Hauer 		clk_disable_unprepare(this->resources.clock[i - 1]);
1433045f8e3SSascha Hauer 	return ret;
14493db446aSBoris Brezillon }
14593db446aSBoris Brezillon 
gpmi_init(struct gpmi_nand_data * this)1463045f8e3SSascha Hauer static int gpmi_init(struct gpmi_nand_data *this)
1473045f8e3SSascha Hauer {
1483045f8e3SSascha Hauer 	struct resources *r = &this->resources;
1493045f8e3SSascha Hauer 	int ret;
1503045f8e3SSascha Hauer 
15178e2d541SZhang Qilong 	ret = pm_runtime_resume_and_get(this->dev);
15278e2d541SZhang Qilong 	if (ret < 0)
1535bc6bb60SEsben Haabendal 		return ret;
1545bc6bb60SEsben Haabendal 
1553045f8e3SSascha Hauer 	ret = gpmi_reset_block(r->gpmi_regs, false);
1563045f8e3SSascha Hauer 	if (ret)
1573045f8e3SSascha Hauer 		goto err_out;
1583045f8e3SSascha Hauer 
15993db446aSBoris Brezillon 	/*
1603045f8e3SSascha Hauer 	 * Reset BCH here, too. We got failures otherwise :(
1613045f8e3SSascha Hauer 	 * See later BCH reset for explanation of MX23 and MX28 handling
16293db446aSBoris Brezillon 	 */
1633045f8e3SSascha Hauer 	ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MXS(this));
1643045f8e3SSascha Hauer 	if (ret)
1653045f8e3SSascha Hauer 		goto err_out;
1663045f8e3SSascha Hauer 
1673045f8e3SSascha Hauer 	/* Choose NAND mode. */
1683045f8e3SSascha Hauer 	writel(BM_GPMI_CTRL1_GPMI_MODE, r->gpmi_regs + HW_GPMI_CTRL1_CLR);
1693045f8e3SSascha Hauer 
1703045f8e3SSascha Hauer 	/* Set the IRQ polarity. */
1713045f8e3SSascha Hauer 	writel(BM_GPMI_CTRL1_ATA_IRQRDY_POLARITY,
1723045f8e3SSascha Hauer 				r->gpmi_regs + HW_GPMI_CTRL1_SET);
1733045f8e3SSascha Hauer 
1743045f8e3SSascha Hauer 	/* Disable Write-Protection. */
1753045f8e3SSascha Hauer 	writel(BM_GPMI_CTRL1_DEV_RESET, r->gpmi_regs + HW_GPMI_CTRL1_SET);
1763045f8e3SSascha Hauer 
1773045f8e3SSascha Hauer 	/* Select BCH ECC. */
1783045f8e3SSascha Hauer 	writel(BM_GPMI_CTRL1_BCH_MODE, r->gpmi_regs + HW_GPMI_CTRL1_SET);
1793045f8e3SSascha Hauer 
1803045f8e3SSascha Hauer 	/*
1813045f8e3SSascha Hauer 	 * Decouple the chip select from dma channel. We use dma0 for all
18246337d15SHan Xu 	 * the chips, force all NAND RDY_BUSY inputs to be sourced from
18346337d15SHan Xu 	 * RDY_BUSY0.
1843045f8e3SSascha Hauer 	 */
18546337d15SHan Xu 	writel(BM_GPMI_CTRL1_DECOUPLE_CS | BM_GPMI_CTRL1_GANGED_RDYBUSY,
18646337d15SHan Xu 	       r->gpmi_regs + HW_GPMI_CTRL1_SET);
1873045f8e3SSascha Hauer 
1883045f8e3SSascha Hauer err_out:
1895bc6bb60SEsben Haabendal 	pm_runtime_mark_last_busy(this->dev);
1905bc6bb60SEsben Haabendal 	pm_runtime_put_autosuspend(this->dev);
1913045f8e3SSascha Hauer 	return ret;
1923045f8e3SSascha Hauer }
1933045f8e3SSascha Hauer 
1943045f8e3SSascha Hauer /* This function is very useful. It is called only when the bug occur. */
gpmi_dump_info(struct gpmi_nand_data * this)1953045f8e3SSascha Hauer static void gpmi_dump_info(struct gpmi_nand_data *this)
19693db446aSBoris Brezillon {
1973045f8e3SSascha Hauer 	struct resources *r = &this->resources;
19893db446aSBoris Brezillon 	struct bch_geometry *geo = &this->bch_geometry;
1993045f8e3SSascha Hauer 	u32 reg;
2003045f8e3SSascha Hauer 	int i;
20193db446aSBoris Brezillon 
2023045f8e3SSascha Hauer 	dev_err(this->dev, "Show GPMI registers :\n");
2033045f8e3SSascha Hauer 	for (i = 0; i <= HW_GPMI_DEBUG / 0x10 + 1; i++) {
2043045f8e3SSascha Hauer 		reg = readl(r->gpmi_regs + i * 0x10);
2053045f8e3SSascha Hauer 		dev_err(this->dev, "offset 0x%.3x : 0x%.8x\n", i * 0x10, reg);
2063045f8e3SSascha Hauer 	}
20793db446aSBoris Brezillon 
2083045f8e3SSascha Hauer 	/* start to print out the BCH info */
2093045f8e3SSascha Hauer 	dev_err(this->dev, "Show BCH registers :\n");
2103045f8e3SSascha Hauer 	for (i = 0; i <= HW_BCH_VERSION / 0x10 + 1; i++) {
2113045f8e3SSascha Hauer 		reg = readl(r->bch_regs + i * 0x10);
2123045f8e3SSascha Hauer 		dev_err(this->dev, "offset 0x%.3x : 0x%.8x\n", i * 0x10, reg);
2133045f8e3SSascha Hauer 	}
2143045f8e3SSascha Hauer 	dev_err(this->dev, "BCH Geometry :\n"
2153045f8e3SSascha Hauer 		"GF length              : %u\n"
2163045f8e3SSascha Hauer 		"ECC Strength           : %u\n"
2173045f8e3SSascha Hauer 		"Page Size in Bytes     : %u\n"
2183045f8e3SSascha Hauer 		"Metadata Size in Bytes : %u\n"
2192fb038eaSHan Xu 		"ECC0 Chunk Size in Bytes: %u\n"
2202fb038eaSHan Xu 		"ECCn Chunk Size in Bytes: %u\n"
2213045f8e3SSascha Hauer 		"ECC Chunk Count        : %u\n"
2223045f8e3SSascha Hauer 		"Payload Size in Bytes  : %u\n"
2233045f8e3SSascha Hauer 		"Auxiliary Size in Bytes: %u\n"
2243045f8e3SSascha Hauer 		"Auxiliary Status Offset: %u\n"
2253045f8e3SSascha Hauer 		"Block Mark Byte Offset : %u\n"
2263045f8e3SSascha Hauer 		"Block Mark Bit Offset  : %u\n",
2273045f8e3SSascha Hauer 		geo->gf_len,
2283045f8e3SSascha Hauer 		geo->ecc_strength,
2293045f8e3SSascha Hauer 		geo->page_size,
2303045f8e3SSascha Hauer 		geo->metadata_size,
2312fb038eaSHan Xu 		geo->ecc0_chunk_size,
2322fb038eaSHan Xu 		geo->eccn_chunk_size,
2333045f8e3SSascha Hauer 		geo->ecc_chunk_count,
2343045f8e3SSascha Hauer 		geo->payload_size,
2353045f8e3SSascha Hauer 		geo->auxiliary_size,
2363045f8e3SSascha Hauer 		geo->auxiliary_status_offset,
2373045f8e3SSascha Hauer 		geo->block_mark_byte_offset,
2383045f8e3SSascha Hauer 		geo->block_mark_bit_offset);
23993db446aSBoris Brezillon }
24093db446aSBoris Brezillon 
gpmi_check_ecc(struct gpmi_nand_data * this)24110915857SHan Xu static bool gpmi_check_ecc(struct gpmi_nand_data *this)
24293db446aSBoris Brezillon {
243d10af38aSHan Xu 	struct nand_chip *chip = &this->nand;
24493db446aSBoris Brezillon 	struct bch_geometry *geo = &this->bch_geometry;
245d10af38aSHan Xu 	struct nand_device *nand = &chip->base;
246d10af38aSHan Xu 	struct nand_ecc_props *conf = &nand->ecc.ctx.conf;
247d10af38aSHan Xu 
248d10af38aSHan Xu 	conf->step_size = geo->eccn_chunk_size;
249d10af38aSHan Xu 	conf->strength = geo->ecc_strength;
25093db446aSBoris Brezillon 
25193db446aSBoris Brezillon 	/* Do the sanity check. */
252f67ed146SFabio Estevam 	if (GPMI_IS_MXS(this)) {
25393db446aSBoris Brezillon 		/* The mx23/mx28 only support the GF13. */
25493db446aSBoris Brezillon 		if (geo->gf_len == 14)
25593db446aSBoris Brezillon 			return false;
25693db446aSBoris Brezillon 	}
257d10af38aSHan Xu 
258d10af38aSHan Xu 	if (geo->ecc_strength > this->devdata->bch_max_ecc_strength)
259d10af38aSHan Xu 		return false;
260d10af38aSHan Xu 
261d10af38aSHan Xu 	if (!nand_ecc_is_strong_enough(nand))
262d10af38aSHan Xu 		return false;
263d10af38aSHan Xu 
264d10af38aSHan Xu 	return true;
26593db446aSBoris Brezillon }
26693db446aSBoris Brezillon 
267d9edc4bcSHan Xu /* check if bbm locates in data chunk rather than ecc chunk */
bbm_in_data_chunk(struct gpmi_nand_data * this,unsigned int * chunk_num)268d9edc4bcSHan Xu static bool bbm_in_data_chunk(struct gpmi_nand_data *this,
269d9edc4bcSHan Xu 			unsigned int *chunk_num)
270d9edc4bcSHan Xu {
271d9edc4bcSHan Xu 	struct bch_geometry *geo = &this->bch_geometry;
272d9edc4bcSHan Xu 	struct nand_chip *chip = &this->nand;
273d9edc4bcSHan Xu 	struct mtd_info *mtd = nand_to_mtd(chip);
274d9edc4bcSHan Xu 	unsigned int i, j;
275d9edc4bcSHan Xu 
276d9edc4bcSHan Xu 	if (geo->ecc0_chunk_size != geo->eccn_chunk_size) {
277d9edc4bcSHan Xu 		dev_err(this->dev,
278d9edc4bcSHan Xu 			"The size of ecc0_chunk must equal to eccn_chunk\n");
279d9edc4bcSHan Xu 		return false;
280d9edc4bcSHan Xu 	}
281d9edc4bcSHan Xu 
282d9edc4bcSHan Xu 	i = (mtd->writesize * 8 - geo->metadata_size * 8) /
283d9edc4bcSHan Xu 		(geo->gf_len * geo->ecc_strength +
284d9edc4bcSHan Xu 			geo->eccn_chunk_size * 8);
285d9edc4bcSHan Xu 
286d9edc4bcSHan Xu 	j = (mtd->writesize * 8 - geo->metadata_size * 8) -
287d9edc4bcSHan Xu 		(geo->gf_len * geo->ecc_strength +
288d9edc4bcSHan Xu 			geo->eccn_chunk_size * 8) * i;
289d9edc4bcSHan Xu 
290d9edc4bcSHan Xu 	if (j < geo->eccn_chunk_size * 8) {
291d9edc4bcSHan Xu 		*chunk_num = i+1;
292d9edc4bcSHan Xu 		dev_dbg(this->dev, "Set ecc to %d and bbm in chunk %d\n",
293d9edc4bcSHan Xu 				geo->ecc_strength, *chunk_num);
294d9edc4bcSHan Xu 		return true;
295d9edc4bcSHan Xu 	}
296d9edc4bcSHan Xu 
297d9edc4bcSHan Xu 	return false;
298d9edc4bcSHan Xu }
299d9edc4bcSHan Xu 
30093db446aSBoris Brezillon /*
30193db446aSBoris Brezillon  * If we can get the ECC information from the nand chip, we do not
30293db446aSBoris Brezillon  * need to calculate them ourselves.
30393db446aSBoris Brezillon  *
30493db446aSBoris Brezillon  * We may have available oob space in this case.
30593db446aSBoris Brezillon  */
set_geometry_by_ecc_info(struct gpmi_nand_data * this,unsigned int ecc_strength,unsigned int ecc_step)3066bf6ec52SStefan Agner static int set_geometry_by_ecc_info(struct gpmi_nand_data *this,
3076bf6ec52SStefan Agner 				    unsigned int ecc_strength,
3086bf6ec52SStefan Agner 				    unsigned int ecc_step)
30993db446aSBoris Brezillon {
31093db446aSBoris Brezillon 	struct bch_geometry *geo = &this->bch_geometry;
31193db446aSBoris Brezillon 	struct nand_chip *chip = &this->nand;
31293db446aSBoris Brezillon 	struct mtd_info *mtd = nand_to_mtd(chip);
31393db446aSBoris Brezillon 	unsigned int block_mark_bit_offset;
31493db446aSBoris Brezillon 
3156bf6ec52SStefan Agner 	switch (ecc_step) {
31693db446aSBoris Brezillon 	case SZ_512:
31793db446aSBoris Brezillon 		geo->gf_len = 13;
31893db446aSBoris Brezillon 		break;
31993db446aSBoris Brezillon 	case SZ_1K:
32093db446aSBoris Brezillon 		geo->gf_len = 14;
32193db446aSBoris Brezillon 		break;
32293db446aSBoris Brezillon 	default:
32393db446aSBoris Brezillon 		dev_err(this->dev,
32493db446aSBoris Brezillon 			"unsupported nand chip. ecc bits : %d, ecc size : %d\n",
32553576c7bSMiquel Raynal 			nanddev_get_ecc_requirements(&chip->base)->strength,
32653576c7bSMiquel Raynal 			nanddev_get_ecc_requirements(&chip->base)->step_size);
32793db446aSBoris Brezillon 		return -EINVAL;
32893db446aSBoris Brezillon 	}
3292fb038eaSHan Xu 	geo->ecc0_chunk_size = ecc_step;
3302fb038eaSHan Xu 	geo->eccn_chunk_size = ecc_step;
3316bf6ec52SStefan Agner 	geo->ecc_strength = round_up(ecc_strength, 2);
33293db446aSBoris Brezillon 	if (!gpmi_check_ecc(this))
33393db446aSBoris Brezillon 		return -EINVAL;
33493db446aSBoris Brezillon 
33593db446aSBoris Brezillon 	/* Keep the C >= O */
3362fb038eaSHan Xu 	if (geo->eccn_chunk_size < mtd->oobsize) {
33793db446aSBoris Brezillon 		dev_err(this->dev,
33893db446aSBoris Brezillon 			"unsupported nand chip. ecc size: %d, oob size : %d\n",
3396bf6ec52SStefan Agner 			ecc_step, mtd->oobsize);
34093db446aSBoris Brezillon 		return -EINVAL;
34193db446aSBoris Brezillon 	}
34293db446aSBoris Brezillon 
34393db446aSBoris Brezillon 	/* The default value, see comment in the legacy_set_geometry(). */
34493db446aSBoris Brezillon 	geo->metadata_size = 10;
34593db446aSBoris Brezillon 
3462fb038eaSHan Xu 	geo->ecc_chunk_count = mtd->writesize / geo->eccn_chunk_size;
34793db446aSBoris Brezillon 
34893db446aSBoris Brezillon 	/*
34993db446aSBoris Brezillon 	 * Now, the NAND chip with 2K page(data chunk is 512byte) shows below:
35093db446aSBoris Brezillon 	 *
35193db446aSBoris Brezillon 	 *    |                          P                            |
35293db446aSBoris Brezillon 	 *    |<----------------------------------------------------->|
35393db446aSBoris Brezillon 	 *    |                                                       |
35493db446aSBoris Brezillon 	 *    |                                        (Block Mark)   |
35593db446aSBoris Brezillon 	 *    |                      P'                      |      | |     |
35693db446aSBoris Brezillon 	 *    |<-------------------------------------------->|  D   | |  O' |
35793db446aSBoris Brezillon 	 *    |                                              |<---->| |<--->|
35893db446aSBoris Brezillon 	 *    V                                              V      V V     V
35993db446aSBoris Brezillon 	 *    +---+----------+-+----------+-+----------+-+----------+-+-----+
36093db446aSBoris Brezillon 	 *    | M |   data   |E|   data   |E|   data   |E|   data   |E|     |
36193db446aSBoris Brezillon 	 *    +---+----------+-+----------+-+----------+-+----------+-+-----+
36293db446aSBoris Brezillon 	 *                                                   ^              ^
36393db446aSBoris Brezillon 	 *                                                   |      O       |
36493db446aSBoris Brezillon 	 *                                                   |<------------>|
36593db446aSBoris Brezillon 	 *                                                   |              |
36693db446aSBoris Brezillon 	 *
36793db446aSBoris Brezillon 	 *	P : the page size for BCH module.
36893db446aSBoris Brezillon 	 *	E : The ECC strength.
36993db446aSBoris Brezillon 	 *	G : the length of Galois Field.
37093db446aSBoris Brezillon 	 *	N : The chunk count of per page.
37193db446aSBoris Brezillon 	 *	M : the metasize of per page.
37293db446aSBoris Brezillon 	 *	C : the ecc chunk size, aka the "data" above.
37393db446aSBoris Brezillon 	 *	P': the nand chip's page size.
37493db446aSBoris Brezillon 	 *	O : the nand chip's oob size.
37593db446aSBoris Brezillon 	 *	O': the free oob.
37693db446aSBoris Brezillon 	 *
37793db446aSBoris Brezillon 	 *	The formula for P is :
37893db446aSBoris Brezillon 	 *
37993db446aSBoris Brezillon 	 *	            E * G * N
38093db446aSBoris Brezillon 	 *	       P = ------------ + P' + M
38193db446aSBoris Brezillon 	 *                      8
38293db446aSBoris Brezillon 	 *
38393db446aSBoris Brezillon 	 * The position of block mark moves forward in the ECC-based view
38493db446aSBoris Brezillon 	 * of page, and the delta is:
38593db446aSBoris Brezillon 	 *
38693db446aSBoris Brezillon 	 *                   E * G * (N - 1)
38793db446aSBoris Brezillon 	 *             D = (---------------- + M)
38893db446aSBoris Brezillon 	 *                          8
38993db446aSBoris Brezillon 	 *
39093db446aSBoris Brezillon 	 * Please see the comment in legacy_set_geometry().
39193db446aSBoris Brezillon 	 * With the condition C >= O , we still can get same result.
39293db446aSBoris Brezillon 	 * So the bit position of the physical block mark within the ECC-based
39393db446aSBoris Brezillon 	 * view of the page is :
39493db446aSBoris Brezillon 	 *             (P' - D) * 8
39593db446aSBoris Brezillon 	 */
39693db446aSBoris Brezillon 	geo->page_size = mtd->writesize + geo->metadata_size +
39793db446aSBoris Brezillon 		(geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
39893db446aSBoris Brezillon 
39993db446aSBoris Brezillon 	geo->payload_size = mtd->writesize;
40093db446aSBoris Brezillon 
40193db446aSBoris Brezillon 	geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4);
40293db446aSBoris Brezillon 	geo->auxiliary_size = ALIGN(geo->metadata_size, 4)
40393db446aSBoris Brezillon 				+ ALIGN(geo->ecc_chunk_count, 4);
40493db446aSBoris Brezillon 
40593db446aSBoris Brezillon 	if (!this->swap_block_mark)
40693db446aSBoris Brezillon 		return 0;
40793db446aSBoris Brezillon 
40893db446aSBoris Brezillon 	/* For bit swap. */
40993db446aSBoris Brezillon 	block_mark_bit_offset = mtd->writesize * 8 -
41093db446aSBoris Brezillon 		(geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
41193db446aSBoris Brezillon 				+ geo->metadata_size * 8);
41293db446aSBoris Brezillon 
41393db446aSBoris Brezillon 	geo->block_mark_byte_offset = block_mark_bit_offset / 8;
41493db446aSBoris Brezillon 	geo->block_mark_bit_offset  = block_mark_bit_offset % 8;
41593db446aSBoris Brezillon 	return 0;
41693db446aSBoris Brezillon }
41793db446aSBoris Brezillon 
4183045f8e3SSascha Hauer /*
4193045f8e3SSascha Hauer  *  Calculate the ECC strength by hand:
4203045f8e3SSascha Hauer  *	E : The ECC strength.
4213045f8e3SSascha Hauer  *	G : the length of Galois Field.
4223045f8e3SSascha Hauer  *	N : The chunk count of per page.
4233045f8e3SSascha Hauer  *	O : the oobsize of the NAND chip.
4243045f8e3SSascha Hauer  *	M : the metasize of per page.
4253045f8e3SSascha Hauer  *
4263045f8e3SSascha Hauer  *	The formula is :
4273045f8e3SSascha Hauer  *		E * G * N
4283045f8e3SSascha Hauer  *	      ------------ <= (O - M)
4293045f8e3SSascha Hauer  *                  8
4303045f8e3SSascha Hauer  *
4313045f8e3SSascha Hauer  *      So, we get E by:
4323045f8e3SSascha Hauer  *                    (O - M) * 8
4333045f8e3SSascha Hauer  *              E <= -------------
4343045f8e3SSascha Hauer  *                       G * N
4353045f8e3SSascha Hauer  */
get_ecc_strength(struct gpmi_nand_data * this)4363045f8e3SSascha Hauer static inline int get_ecc_strength(struct gpmi_nand_data *this)
4373045f8e3SSascha Hauer {
4383045f8e3SSascha Hauer 	struct bch_geometry *geo = &this->bch_geometry;
4393045f8e3SSascha Hauer 	struct mtd_info	*mtd = nand_to_mtd(&this->nand);
4403045f8e3SSascha Hauer 	int ecc_strength;
4413045f8e3SSascha Hauer 
4423045f8e3SSascha Hauer 	ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8)
4433045f8e3SSascha Hauer 			/ (geo->gf_len * geo->ecc_chunk_count);
4443045f8e3SSascha Hauer 
4453045f8e3SSascha Hauer 	/* We need the minor even number. */
4463045f8e3SSascha Hauer 	return round_down(ecc_strength, 2);
4473045f8e3SSascha Hauer }
4483045f8e3SSascha Hauer 
set_geometry_for_large_oob(struct gpmi_nand_data * this)449d9edc4bcSHan Xu static int set_geometry_for_large_oob(struct gpmi_nand_data *this)
450d9edc4bcSHan Xu {
451d9edc4bcSHan Xu 	struct bch_geometry *geo = &this->bch_geometry;
452d9edc4bcSHan Xu 	struct nand_chip *chip = &this->nand;
453d9edc4bcSHan Xu 	struct mtd_info *mtd = nand_to_mtd(chip);
454d9edc4bcSHan Xu 	const struct nand_ecc_props *requirements =
455d9edc4bcSHan Xu 		nanddev_get_ecc_requirements(&chip->base);
456d9edc4bcSHan Xu 	unsigned int block_mark_bit_offset;
457d9edc4bcSHan Xu 	unsigned int max_ecc;
458d9edc4bcSHan Xu 	unsigned int bbm_chunk;
459d9edc4bcSHan Xu 	unsigned int i;
460d9edc4bcSHan Xu 
461d9edc4bcSHan Xu 	/* sanity check for the minimum ecc nand required */
462d9edc4bcSHan Xu 	if (!(requirements->strength > 0 &&
463d9edc4bcSHan Xu 	      requirements->step_size > 0))
464d9edc4bcSHan Xu 		return -EINVAL;
465d9edc4bcSHan Xu 	geo->ecc_strength = requirements->strength;
466d9edc4bcSHan Xu 
467d9edc4bcSHan Xu 	/* check if platform can support this nand */
468d9edc4bcSHan Xu 	if (!gpmi_check_ecc(this)) {
469d9edc4bcSHan Xu 		dev_err(this->dev,
470d9edc4bcSHan Xu 			"unsupported NAND chip, minimum ecc required %d\n",
471d9edc4bcSHan Xu 			geo->ecc_strength);
472d9edc4bcSHan Xu 		return -EINVAL;
473d9edc4bcSHan Xu 	}
474d9edc4bcSHan Xu 
475d9edc4bcSHan Xu 	/* calculate the maximum ecc platform can support*/
476d9edc4bcSHan Xu 	geo->metadata_size = 10;
477d9edc4bcSHan Xu 	geo->gf_len = 14;
478d9edc4bcSHan Xu 	geo->ecc0_chunk_size = 1024;
479d9edc4bcSHan Xu 	geo->eccn_chunk_size = 1024;
480d9edc4bcSHan Xu 	geo->ecc_chunk_count = mtd->writesize / geo->eccn_chunk_size;
481d9edc4bcSHan Xu 	max_ecc = min(get_ecc_strength(this),
482d9edc4bcSHan Xu 		      this->devdata->bch_max_ecc_strength);
483d9edc4bcSHan Xu 
484d9edc4bcSHan Xu 	/*
485d9edc4bcSHan Xu 	 * search a supported ecc strength that makes bbm
486d9edc4bcSHan Xu 	 * located in data chunk
487d9edc4bcSHan Xu 	 */
488d9edc4bcSHan Xu 	geo->ecc_strength = max_ecc;
489d9edc4bcSHan Xu 	while (!(geo->ecc_strength < requirements->strength)) {
490d9edc4bcSHan Xu 		if (bbm_in_data_chunk(this, &bbm_chunk))
491d9edc4bcSHan Xu 			goto geo_setting;
492d9edc4bcSHan Xu 		geo->ecc_strength -= 2;
493d9edc4bcSHan Xu 	}
494d9edc4bcSHan Xu 
495d9edc4bcSHan Xu 	/* if none of them works, keep using the minimum ecc */
496d9edc4bcSHan Xu 	/* nand required but changing ecc page layout  */
497d9edc4bcSHan Xu 	geo->ecc_strength = requirements->strength;
498d9edc4bcSHan Xu 	/* add extra ecc for meta data */
499d9edc4bcSHan Xu 	geo->ecc0_chunk_size = 0;
500d9edc4bcSHan Xu 	geo->ecc_chunk_count = (mtd->writesize / geo->eccn_chunk_size) + 1;
501d9edc4bcSHan Xu 	geo->ecc_for_meta = 1;
502d9edc4bcSHan Xu 	/* check if oob can afford this extra ecc chunk */
503d9edc4bcSHan Xu 	if (mtd->oobsize * 8 < geo->metadata_size * 8 +
504d9edc4bcSHan Xu 	    geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) {
505d9edc4bcSHan Xu 		dev_err(this->dev, "unsupported NAND chip with new layout\n");
506d9edc4bcSHan Xu 		return -EINVAL;
507d9edc4bcSHan Xu 	}
508d9edc4bcSHan Xu 
509d9edc4bcSHan Xu 	/* calculate in which chunk bbm located */
510d9edc4bcSHan Xu 	bbm_chunk = (mtd->writesize * 8 - geo->metadata_size * 8 -
511d9edc4bcSHan Xu 		     geo->gf_len * geo->ecc_strength) /
512d9edc4bcSHan Xu 		     (geo->gf_len * geo->ecc_strength +
513d9edc4bcSHan Xu 		     geo->eccn_chunk_size * 8) + 1;
514d9edc4bcSHan Xu 
515d9edc4bcSHan Xu geo_setting:
516d9edc4bcSHan Xu 
517d9edc4bcSHan Xu 	geo->page_size = mtd->writesize + geo->metadata_size +
518d9edc4bcSHan Xu 		(geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
519d9edc4bcSHan Xu 	geo->payload_size = mtd->writesize;
520d9edc4bcSHan Xu 
521d9edc4bcSHan Xu 	/*
522d9edc4bcSHan Xu 	 * The auxiliary buffer contains the metadata and the ECC status. The
523d9edc4bcSHan Xu 	 * metadata is padded to the nearest 32-bit boundary. The ECC status
524d9edc4bcSHan Xu 	 * contains one byte for every ECC chunk, and is also padded to the
525d9edc4bcSHan Xu 	 * nearest 32-bit boundary.
526d9edc4bcSHan Xu 	 */
527d9edc4bcSHan Xu 	geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4);
528d9edc4bcSHan Xu 	geo->auxiliary_size = ALIGN(geo->metadata_size, 4)
529d9edc4bcSHan Xu 				    + ALIGN(geo->ecc_chunk_count, 4);
530d9edc4bcSHan Xu 
531d9edc4bcSHan Xu 	if (!this->swap_block_mark)
532d9edc4bcSHan Xu 		return 0;
533d9edc4bcSHan Xu 
534d9edc4bcSHan Xu 	/* calculate the number of ecc chunk behind the bbm */
535d9edc4bcSHan Xu 	i = (mtd->writesize / geo->eccn_chunk_size) - bbm_chunk + 1;
536d9edc4bcSHan Xu 
537d9edc4bcSHan Xu 	block_mark_bit_offset = mtd->writesize * 8 -
538d9edc4bcSHan Xu 		(geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - i)
539d9edc4bcSHan Xu 		+ geo->metadata_size * 8);
540d9edc4bcSHan Xu 
541d9edc4bcSHan Xu 	geo->block_mark_byte_offset = block_mark_bit_offset / 8;
542d9edc4bcSHan Xu 	geo->block_mark_bit_offset  = block_mark_bit_offset % 8;
543d9edc4bcSHan Xu 
544d9edc4bcSHan Xu 	dev_dbg(this->dev, "BCH Geometry :\n"
545d9edc4bcSHan Xu 		"GF length              : %u\n"
546d9edc4bcSHan Xu 		"ECC Strength           : %u\n"
547d9edc4bcSHan Xu 		"Page Size in Bytes     : %u\n"
548d9edc4bcSHan Xu 		"Metadata Size in Bytes : %u\n"
549d9edc4bcSHan Xu 		"ECC0 Chunk Size in Bytes: %u\n"
550d9edc4bcSHan Xu 		"ECCn Chunk Size in Bytes: %u\n"
551d9edc4bcSHan Xu 		"ECC Chunk Count        : %u\n"
552d9edc4bcSHan Xu 		"Payload Size in Bytes  : %u\n"
553d9edc4bcSHan Xu 		"Auxiliary Size in Bytes: %u\n"
554d9edc4bcSHan Xu 		"Auxiliary Status Offset: %u\n"
555d9edc4bcSHan Xu 		"Block Mark Byte Offset : %u\n"
556d9edc4bcSHan Xu 		"Block Mark Bit Offset  : %u\n"
557d9edc4bcSHan Xu 		"Block Mark in chunk	: %u\n"
558d9edc4bcSHan Xu 		"Ecc for Meta data	: %u\n",
559d9edc4bcSHan Xu 		geo->gf_len,
560d9edc4bcSHan Xu 		geo->ecc_strength,
561d9edc4bcSHan Xu 		geo->page_size,
562d9edc4bcSHan Xu 		geo->metadata_size,
563d9edc4bcSHan Xu 		geo->ecc0_chunk_size,
564d9edc4bcSHan Xu 		geo->eccn_chunk_size,
565d9edc4bcSHan Xu 		geo->ecc_chunk_count,
566d9edc4bcSHan Xu 		geo->payload_size,
567d9edc4bcSHan Xu 		geo->auxiliary_size,
568d9edc4bcSHan Xu 		geo->auxiliary_status_offset,
569d9edc4bcSHan Xu 		geo->block_mark_byte_offset,
570d9edc4bcSHan Xu 		geo->block_mark_bit_offset,
571d9edc4bcSHan Xu 		bbm_chunk,
572d9edc4bcSHan Xu 		geo->ecc_for_meta);
573d9edc4bcSHan Xu 
574d9edc4bcSHan Xu 	return 0;
575d9edc4bcSHan Xu }
576d9edc4bcSHan Xu 
legacy_set_geometry(struct gpmi_nand_data * this)57793db446aSBoris Brezillon static int legacy_set_geometry(struct gpmi_nand_data *this)
57893db446aSBoris Brezillon {
57993db446aSBoris Brezillon 	struct bch_geometry *geo = &this->bch_geometry;
58093db446aSBoris Brezillon 	struct mtd_info *mtd = nand_to_mtd(&this->nand);
58193db446aSBoris Brezillon 	unsigned int metadata_size;
58293db446aSBoris Brezillon 	unsigned int status_size;
58393db446aSBoris Brezillon 	unsigned int block_mark_bit_offset;
58493db446aSBoris Brezillon 
58593db446aSBoris Brezillon 	/*
58693db446aSBoris Brezillon 	 * The size of the metadata can be changed, though we set it to 10
58793db446aSBoris Brezillon 	 * bytes now. But it can't be too large, because we have to save
58893db446aSBoris Brezillon 	 * enough space for BCH.
58993db446aSBoris Brezillon 	 */
59093db446aSBoris Brezillon 	geo->metadata_size = 10;
59193db446aSBoris Brezillon 
59293db446aSBoris Brezillon 	/* The default for the length of Galois Field. */
59393db446aSBoris Brezillon 	geo->gf_len = 13;
59493db446aSBoris Brezillon 
59593db446aSBoris Brezillon 	/* The default for chunk size. */
5962fb038eaSHan Xu 	geo->ecc0_chunk_size = 512;
5972fb038eaSHan Xu 	geo->eccn_chunk_size = 512;
5982fb038eaSHan Xu 	while (geo->eccn_chunk_size < mtd->oobsize) {
5992fb038eaSHan Xu 		geo->ecc0_chunk_size *= 2; /* keep C >= O */
6002fb038eaSHan Xu 		geo->eccn_chunk_size *= 2; /* keep C >= O */
60193db446aSBoris Brezillon 		geo->gf_len = 14;
60293db446aSBoris Brezillon 	}
60393db446aSBoris Brezillon 
6042fb038eaSHan Xu 	geo->ecc_chunk_count = mtd->writesize / geo->eccn_chunk_size;
60593db446aSBoris Brezillon 
60693db446aSBoris Brezillon 	/* We use the same ECC strength for all chunks. */
60793db446aSBoris Brezillon 	geo->ecc_strength = get_ecc_strength(this);
60893db446aSBoris Brezillon 	if (!gpmi_check_ecc(this)) {
60993db446aSBoris Brezillon 		dev_err(this->dev,
61093db446aSBoris Brezillon 			"ecc strength: %d cannot be supported by the controller (%d)\n"
61193db446aSBoris Brezillon 			"try to use minimum ecc strength that NAND chip required\n",
61293db446aSBoris Brezillon 			geo->ecc_strength,
61393db446aSBoris Brezillon 			this->devdata->bch_max_ecc_strength);
61493db446aSBoris Brezillon 		return -EINVAL;
61593db446aSBoris Brezillon 	}
61693db446aSBoris Brezillon 
61793db446aSBoris Brezillon 	geo->page_size = mtd->writesize + geo->metadata_size +
61893db446aSBoris Brezillon 		(geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
61993db446aSBoris Brezillon 	geo->payload_size = mtd->writesize;
62093db446aSBoris Brezillon 
62193db446aSBoris Brezillon 	/*
62293db446aSBoris Brezillon 	 * The auxiliary buffer contains the metadata and the ECC status. The
62393db446aSBoris Brezillon 	 * metadata is padded to the nearest 32-bit boundary. The ECC status
62493db446aSBoris Brezillon 	 * contains one byte for every ECC chunk, and is also padded to the
62593db446aSBoris Brezillon 	 * nearest 32-bit boundary.
62693db446aSBoris Brezillon 	 */
62793db446aSBoris Brezillon 	metadata_size = ALIGN(geo->metadata_size, 4);
62893db446aSBoris Brezillon 	status_size   = ALIGN(geo->ecc_chunk_count, 4);
62993db446aSBoris Brezillon 
63093db446aSBoris Brezillon 	geo->auxiliary_size = metadata_size + status_size;
63193db446aSBoris Brezillon 	geo->auxiliary_status_offset = metadata_size;
63293db446aSBoris Brezillon 
63393db446aSBoris Brezillon 	if (!this->swap_block_mark)
63493db446aSBoris Brezillon 		return 0;
63593db446aSBoris Brezillon 
63693db446aSBoris Brezillon 	/*
63793db446aSBoris Brezillon 	 * We need to compute the byte and bit offsets of
63893db446aSBoris Brezillon 	 * the physical block mark within the ECC-based view of the page.
63993db446aSBoris Brezillon 	 *
64093db446aSBoris Brezillon 	 * NAND chip with 2K page shows below:
64193db446aSBoris Brezillon 	 *                                             (Block Mark)
64293db446aSBoris Brezillon 	 *                                                   |      |
64393db446aSBoris Brezillon 	 *                                                   |  D   |
64493db446aSBoris Brezillon 	 *                                                   |<---->|
64593db446aSBoris Brezillon 	 *                                                   V      V
64693db446aSBoris Brezillon 	 *    +---+----------+-+----------+-+----------+-+----------+-+
64793db446aSBoris Brezillon 	 *    | M |   data   |E|   data   |E|   data   |E|   data   |E|
64893db446aSBoris Brezillon 	 *    +---+----------+-+----------+-+----------+-+----------+-+
64993db446aSBoris Brezillon 	 *
65093db446aSBoris Brezillon 	 * The position of block mark moves forward in the ECC-based view
65193db446aSBoris Brezillon 	 * of page, and the delta is:
65293db446aSBoris Brezillon 	 *
65393db446aSBoris Brezillon 	 *                   E * G * (N - 1)
65493db446aSBoris Brezillon 	 *             D = (---------------- + M)
65593db446aSBoris Brezillon 	 *                          8
65693db446aSBoris Brezillon 	 *
65793db446aSBoris Brezillon 	 * With the formula to compute the ECC strength, and the condition
65893db446aSBoris Brezillon 	 *       : C >= O         (C is the ecc chunk size)
65993db446aSBoris Brezillon 	 *
66093db446aSBoris Brezillon 	 * It's easy to deduce to the following result:
66193db446aSBoris Brezillon 	 *
66293db446aSBoris Brezillon 	 *         E * G       (O - M)      C - M         C - M
66393db446aSBoris Brezillon 	 *      ----------- <= ------- <=  --------  <  ---------
66493db446aSBoris Brezillon 	 *           8            N           N          (N - 1)
66593db446aSBoris Brezillon 	 *
66693db446aSBoris Brezillon 	 *  So, we get:
66793db446aSBoris Brezillon 	 *
66893db446aSBoris Brezillon 	 *                   E * G * (N - 1)
66993db446aSBoris Brezillon 	 *             D = (---------------- + M) < C
67093db446aSBoris Brezillon 	 *                          8
67193db446aSBoris Brezillon 	 *
67293db446aSBoris Brezillon 	 *  The above inequality means the position of block mark
67393db446aSBoris Brezillon 	 *  within the ECC-based view of the page is still in the data chunk,
67493db446aSBoris Brezillon 	 *  and it's NOT in the ECC bits of the chunk.
67593db446aSBoris Brezillon 	 *
67693db446aSBoris Brezillon 	 *  Use the following to compute the bit position of the
67793db446aSBoris Brezillon 	 *  physical block mark within the ECC-based view of the page:
67893db446aSBoris Brezillon 	 *          (page_size - D) * 8
67993db446aSBoris Brezillon 	 *
68093db446aSBoris Brezillon 	 *  --Huang Shijie
68193db446aSBoris Brezillon 	 */
68293db446aSBoris Brezillon 	block_mark_bit_offset = mtd->writesize * 8 -
68393db446aSBoris Brezillon 		(geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
68493db446aSBoris Brezillon 				+ geo->metadata_size * 8);
68593db446aSBoris Brezillon 
68693db446aSBoris Brezillon 	geo->block_mark_byte_offset = block_mark_bit_offset / 8;
68793db446aSBoris Brezillon 	geo->block_mark_bit_offset  = block_mark_bit_offset % 8;
68893db446aSBoris Brezillon 	return 0;
68993db446aSBoris Brezillon }
69093db446aSBoris Brezillon 
common_nfc_set_geometry(struct gpmi_nand_data * this)6913045f8e3SSascha Hauer static int common_nfc_set_geometry(struct gpmi_nand_data *this)
69293db446aSBoris Brezillon {
6936bf6ec52SStefan Agner 	struct nand_chip *chip = &this->nand;
69415616c7cSHan Xu 	struct mtd_info *mtd = nand_to_mtd(&this->nand);
69553576c7bSMiquel Raynal 	const struct nand_ecc_props *requirements =
69653576c7bSMiquel Raynal 		nanddev_get_ecc_requirements(&chip->base);
69715616c7cSHan Xu 	bool use_minimun_ecc;
69815616c7cSHan Xu 	int err;
6996bf6ec52SStefan Agner 
70015616c7cSHan Xu 	use_minimun_ecc = of_property_read_bool(this->dev->of_node,
70115616c7cSHan Xu 						"fsl,use-minimum-ecc");
7026bf6ec52SStefan Agner 
70315616c7cSHan Xu 	/* use legacy bch geometry settings by default*/
70415616c7cSHan Xu 	if ((!use_minimun_ecc && mtd->oobsize < 1024) ||
70515616c7cSHan Xu 	    !(requirements->strength > 0 && requirements->step_size > 0)) {
70615616c7cSHan Xu 		dev_dbg(this->dev, "use legacy bch geometry\n");
70715616c7cSHan Xu 		err = legacy_set_geometry(this);
70815616c7cSHan Xu 		if (!err)
70915616c7cSHan Xu 			return 0;
7106bf6ec52SStefan Agner 	}
71193db446aSBoris Brezillon 
712d9edc4bcSHan Xu 	/* for large oob nand */
713d9edc4bcSHan Xu 	if (mtd->oobsize > 1024) {
714d9edc4bcSHan Xu 		dev_dbg(this->dev, "use large oob bch geometry\n");
715d9edc4bcSHan Xu 		err = set_geometry_for_large_oob(this);
716d9edc4bcSHan Xu 		if (!err)
717d9edc4bcSHan Xu 			return 0;
718d9edc4bcSHan Xu 	}
719d9edc4bcSHan Xu 
72015616c7cSHan Xu 	/* otherwise use the minimum ecc nand chip required */
72115616c7cSHan Xu 	dev_dbg(this->dev, "use minimum ecc bch geometry\n");
72215616c7cSHan Xu 	err = set_geometry_by_ecc_info(this, requirements->strength,
72315616c7cSHan Xu 					requirements->step_size);
72415616c7cSHan Xu 	if (err)
72515616c7cSHan Xu 		dev_err(this->dev, "none of the bch geometry setting works\n");
72615616c7cSHan Xu 
72715616c7cSHan Xu 	return err;
72893db446aSBoris Brezillon }
72993db446aSBoris Brezillon 
7303045f8e3SSascha Hauer /* Configures the geometry for BCH.  */
bch_set_geometry(struct gpmi_nand_data * this)7313045f8e3SSascha Hauer static int bch_set_geometry(struct gpmi_nand_data *this)
7323045f8e3SSascha Hauer {
7333045f8e3SSascha Hauer 	struct resources *r = &this->resources;
7343045f8e3SSascha Hauer 	int ret;
7353045f8e3SSascha Hauer 
7363045f8e3SSascha Hauer 	ret = common_nfc_set_geometry(this);
7373045f8e3SSascha Hauer 	if (ret)
7383045f8e3SSascha Hauer 		return ret;
7393045f8e3SSascha Hauer 
74004141468SSascha Hauer 	ret = pm_runtime_get_sync(this->dev);
741550e68eaSDinghao Liu 	if (ret < 0) {
742550e68eaSDinghao Liu 		pm_runtime_put_autosuspend(this->dev);
7433045f8e3SSascha Hauer 		return ret;
744550e68eaSDinghao Liu 	}
7453045f8e3SSascha Hauer 
7463045f8e3SSascha Hauer 	/*
7473045f8e3SSascha Hauer 	* Due to erratum #2847 of the MX23, the BCH cannot be soft reset on this
7483045f8e3SSascha Hauer 	* chip, otherwise it will lock up. So we skip resetting BCH on the MX23.
7493045f8e3SSascha Hauer 	* and MX28.
7503045f8e3SSascha Hauer 	*/
7513045f8e3SSascha Hauer 	ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MXS(this));
7523045f8e3SSascha Hauer 	if (ret)
7533045f8e3SSascha Hauer 		goto err_out;
7543045f8e3SSascha Hauer 
7553045f8e3SSascha Hauer 	/* Set *all* chip selects to use layout 0. */
7563045f8e3SSascha Hauer 	writel(0, r->bch_regs + HW_BCH_LAYOUTSELECT);
7573045f8e3SSascha Hauer 
75804141468SSascha Hauer 	ret = 0;
7593045f8e3SSascha Hauer err_out:
76004141468SSascha Hauer 	pm_runtime_mark_last_busy(this->dev);
76104141468SSascha Hauer 	pm_runtime_put_autosuspend(this->dev);
76204141468SSascha Hauer 
7633045f8e3SSascha Hauer 	return ret;
7643045f8e3SSascha Hauer }
7653045f8e3SSascha Hauer 
7663045f8e3SSascha Hauer /*
7673045f8e3SSascha Hauer  * <1> Firstly, we should know what's the GPMI-clock means.
7683045f8e3SSascha Hauer  *     The GPMI-clock is the internal clock in the gpmi nand controller.
7693045f8e3SSascha Hauer  *     If you set 100MHz to gpmi nand controller, the GPMI-clock's period
7703045f8e3SSascha Hauer  *     is 10ns. Mark the GPMI-clock's period as GPMI-clock-period.
7713045f8e3SSascha Hauer  *
7723045f8e3SSascha Hauer  * <2> Secondly, we should know what's the frequency on the nand chip pins.
7733045f8e3SSascha Hauer  *     The frequency on the nand chip pins is derived from the GPMI-clock.
7743045f8e3SSascha Hauer  *     We can get it from the following equation:
7753045f8e3SSascha Hauer  *
7763045f8e3SSascha Hauer  *         F = G / (DS + DH)
7773045f8e3SSascha Hauer  *
7783045f8e3SSascha Hauer  *         F  : the frequency on the nand chip pins.
7793045f8e3SSascha Hauer  *         G  : the GPMI clock, such as 100MHz.
7803045f8e3SSascha Hauer  *         DS : GPMI_HW_GPMI_TIMING0:DATA_SETUP
7813045f8e3SSascha Hauer  *         DH : GPMI_HW_GPMI_TIMING0:DATA_HOLD
7823045f8e3SSascha Hauer  *
7833045f8e3SSascha Hauer  * <3> Thirdly, when the frequency on the nand chip pins is above 33MHz,
7843045f8e3SSascha Hauer  *     the nand EDO(extended Data Out) timing could be applied.
7853045f8e3SSascha Hauer  *     The GPMI implements a feedback read strobe to sample the read data.
7863045f8e3SSascha Hauer  *     The feedback read strobe can be delayed to support the nand EDO timing
7873045f8e3SSascha Hauer  *     where the read strobe may deasserts before the read data is valid, and
7883045f8e3SSascha Hauer  *     read data is valid for some time after read strobe.
7893045f8e3SSascha Hauer  *
7903045f8e3SSascha Hauer  *     The following figure illustrates some aspects of a NAND Flash read:
7913045f8e3SSascha Hauer  *
7923045f8e3SSascha Hauer  *                   |<---tREA---->|
7933045f8e3SSascha Hauer  *                   |             |
7943045f8e3SSascha Hauer  *                   |         |   |
7953045f8e3SSascha Hauer  *                   |<--tRP-->|   |
7963045f8e3SSascha Hauer  *                   |         |   |
7973045f8e3SSascha Hauer  *                  __          ___|__________________________________
7983045f8e3SSascha Hauer  *     RDN            \________/   |
7993045f8e3SSascha Hauer  *                                 |
8003045f8e3SSascha Hauer  *                                 /---------\
8013045f8e3SSascha Hauer  *     Read Data    --------------<           >---------
8023045f8e3SSascha Hauer  *                                 \---------/
8033045f8e3SSascha Hauer  *                                |     |
8043045f8e3SSascha Hauer  *                                |<-D->|
8053045f8e3SSascha Hauer  *     FeedbackRDN  ________             ____________
8063045f8e3SSascha Hauer  *                          \___________/
8073045f8e3SSascha Hauer  *
8083045f8e3SSascha Hauer  *          D stands for delay, set in the HW_GPMI_CTRL1:RDN_DELAY.
8093045f8e3SSascha Hauer  *
8103045f8e3SSascha Hauer  *
8113045f8e3SSascha Hauer  * <4> Now, we begin to describe how to compute the right RDN_DELAY.
8123045f8e3SSascha Hauer  *
8133045f8e3SSascha Hauer  *  4.1) From the aspect of the nand chip pins:
8143045f8e3SSascha Hauer  *        Delay = (tREA + C - tRP)               {1}
8153045f8e3SSascha Hauer  *
8163045f8e3SSascha Hauer  *        tREA : the maximum read access time.
8173045f8e3SSascha Hauer  *        C    : a constant to adjust the delay. default is 4000ps.
8183045f8e3SSascha Hauer  *        tRP  : the read pulse width, which is exactly:
8193045f8e3SSascha Hauer  *                   tRP = (GPMI-clock-period) * DATA_SETUP
8203045f8e3SSascha Hauer  *
8213045f8e3SSascha Hauer  *  4.2) From the aspect of the GPMI nand controller:
8223045f8e3SSascha Hauer  *         Delay = RDN_DELAY * 0.125 * RP        {2}
8233045f8e3SSascha Hauer  *
8243045f8e3SSascha Hauer  *         RP   : the DLL reference period.
8253045f8e3SSascha Hauer  *            if (GPMI-clock-period > DLL_THRETHOLD)
8263045f8e3SSascha Hauer  *                   RP = GPMI-clock-period / 2;
8273045f8e3SSascha Hauer  *            else
8283045f8e3SSascha Hauer  *                   RP = GPMI-clock-period;
8293045f8e3SSascha Hauer  *
8303045f8e3SSascha Hauer  *            Set the HW_GPMI_CTRL1:HALF_PERIOD if GPMI-clock-period
8313045f8e3SSascha Hauer  *            is greater DLL_THRETHOLD. In other SOCs, the DLL_THRETHOLD
8323045f8e3SSascha Hauer  *            is 16000ps, but in mx6q, we use 12000ps.
8333045f8e3SSascha Hauer  *
8343045f8e3SSascha Hauer  *  4.3) since {1} equals {2}, we get:
8353045f8e3SSascha Hauer  *
8363045f8e3SSascha Hauer  *                     (tREA + 4000 - tRP) * 8
8373045f8e3SSascha Hauer  *         RDN_DELAY = -----------------------     {3}
8383045f8e3SSascha Hauer  *                           RP
8393045f8e3SSascha Hauer  */
gpmi_nfc_compute_timings(struct gpmi_nand_data * this,const struct nand_sdr_timings * sdr)84015e27d19SDario Binacchi static int gpmi_nfc_compute_timings(struct gpmi_nand_data *this,
8413045f8e3SSascha Hauer 				    const struct nand_sdr_timings *sdr)
8423045f8e3SSascha Hauer {
8433045f8e3SSascha Hauer 	struct gpmi_nfc_hardware_timing *hw = &this->hw;
8442970bf5aSDario Binacchi 	struct resources *r = &this->resources;
8453045f8e3SSascha Hauer 	unsigned int dll_threshold_ps = this->devdata->max_chain_delay;
8463045f8e3SSascha Hauer 	unsigned int period_ps, reference_period_ps;
8473045f8e3SSascha Hauer 	unsigned int data_setup_cycles, data_hold_cycles, addr_setup_cycles;
8483045f8e3SSascha Hauer 	unsigned int tRP_ps;
8493045f8e3SSascha Hauer 	bool use_half_period;
8503045f8e3SSascha Hauer 	int sample_delay_ps, sample_delay_factor;
8510fddf9adSSascha Hauer 	unsigned int busy_timeout_cycles;
8523045f8e3SSascha Hauer 	u8 wrn_dly_sel;
85315e27d19SDario Binacchi 	unsigned long clk_rate, min_rate;
8540fddf9adSSascha Hauer 	u64 busy_timeout_ps;
8553045f8e3SSascha Hauer 
8563045f8e3SSascha Hauer 	if (sdr->tRC_min >= 30000) {
8573045f8e3SSascha Hauer 		/* ONFI non-EDO modes [0-3] */
8583045f8e3SSascha Hauer 		hw->clk_rate = 22000000;
85915e27d19SDario Binacchi 		min_rate = 0;
8603045f8e3SSascha Hauer 		wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_4_TO_8NS;
8613045f8e3SSascha Hauer 	} else if (sdr->tRC_min >= 25000) {
8623045f8e3SSascha Hauer 		/* ONFI EDO mode 4 */
8633045f8e3SSascha Hauer 		hw->clk_rate = 80000000;
86415e27d19SDario Binacchi 		min_rate = 22000000;
8653045f8e3SSascha Hauer 		wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY;
8663045f8e3SSascha Hauer 	} else {
8673045f8e3SSascha Hauer 		/* ONFI EDO mode 5 */
8683045f8e3SSascha Hauer 		hw->clk_rate = 100000000;
86915e27d19SDario Binacchi 		min_rate = 80000000;
8703045f8e3SSascha Hauer 		wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY;
8713045f8e3SSascha Hauer 	}
8723045f8e3SSascha Hauer 
87315e27d19SDario Binacchi 	clk_rate = clk_round_rate(r->clock[0], hw->clk_rate);
87415e27d19SDario Binacchi 	if (clk_rate <= min_rate) {
87515e27d19SDario Binacchi 		dev_err(this->dev, "clock setting: expected %ld, got %ld\n",
87615e27d19SDario Binacchi 			hw->clk_rate, clk_rate);
87715e27d19SDario Binacchi 		return -ENOTSUPP;
87815e27d19SDario Binacchi 	}
8792970bf5aSDario Binacchi 
88015e27d19SDario Binacchi 	hw->clk_rate = clk_rate;
8813045f8e3SSascha Hauer 	/* SDR core timings are given in picoseconds */
8823045f8e3SSascha Hauer 	period_ps = div_u64((u64)NSEC_PER_SEC * 1000, hw->clk_rate);
8833045f8e3SSascha Hauer 
8843045f8e3SSascha Hauer 	addr_setup_cycles = TO_CYCLES(sdr->tALS_min, period_ps);
8853045f8e3SSascha Hauer 	data_setup_cycles = TO_CYCLES(sdr->tDS_min, period_ps);
8863045f8e3SSascha Hauer 	data_hold_cycles = TO_CYCLES(sdr->tDH_min, period_ps);
8870fddf9adSSascha Hauer 	busy_timeout_ps = max(sdr->tBERS_max, sdr->tPROG_max);
8880fddf9adSSascha Hauer 	busy_timeout_cycles = TO_CYCLES(busy_timeout_ps, period_ps);
8893045f8e3SSascha Hauer 
8903045f8e3SSascha Hauer 	hw->timing0 = BF_GPMI_TIMING0_ADDRESS_SETUP(addr_setup_cycles) |
8913045f8e3SSascha Hauer 		      BF_GPMI_TIMING0_DATA_HOLD(data_hold_cycles) |
8923045f8e3SSascha Hauer 		      BF_GPMI_TIMING0_DATA_SETUP(data_setup_cycles);
89306781a50SSascha Hauer 	hw->timing1 = BF_GPMI_TIMING1_BUSY_TIMEOUT(DIV_ROUND_UP(busy_timeout_cycles, 4096));
8943045f8e3SSascha Hauer 
8953045f8e3SSascha Hauer 	/*
8963045f8e3SSascha Hauer 	 * Derive NFC ideal delay from {3}:
8973045f8e3SSascha Hauer 	 *
8983045f8e3SSascha Hauer 	 *                     (tREA + 4000 - tRP) * 8
8993045f8e3SSascha Hauer 	 *         RDN_DELAY = -----------------------
9003045f8e3SSascha Hauer 	 *                                RP
9013045f8e3SSascha Hauer 	 */
9023045f8e3SSascha Hauer 	if (period_ps > dll_threshold_ps) {
9033045f8e3SSascha Hauer 		use_half_period = true;
9043045f8e3SSascha Hauer 		reference_period_ps = period_ps / 2;
9053045f8e3SSascha Hauer 	} else {
9063045f8e3SSascha Hauer 		use_half_period = false;
9073045f8e3SSascha Hauer 		reference_period_ps = period_ps;
9083045f8e3SSascha Hauer 	}
9093045f8e3SSascha Hauer 
9103045f8e3SSascha Hauer 	tRP_ps = data_setup_cycles * period_ps;
9113045f8e3SSascha Hauer 	sample_delay_ps = (sdr->tREA_max + 4000 - tRP_ps) * 8;
9123045f8e3SSascha Hauer 	if (sample_delay_ps > 0)
9133045f8e3SSascha Hauer 		sample_delay_factor = sample_delay_ps / reference_period_ps;
9143045f8e3SSascha Hauer 	else
9153045f8e3SSascha Hauer 		sample_delay_factor = 0;
9163045f8e3SSascha Hauer 
9173045f8e3SSascha Hauer 	hw->ctrl1n = BF_GPMI_CTRL1_WRN_DLY_SEL(wrn_dly_sel);
9183045f8e3SSascha Hauer 	if (sample_delay_factor)
9193045f8e3SSascha Hauer 		hw->ctrl1n |= BF_GPMI_CTRL1_RDN_DELAY(sample_delay_factor) |
9203045f8e3SSascha Hauer 			      BM_GPMI_CTRL1_DLL_ENABLE |
9213045f8e3SSascha Hauer 			      (use_half_period ? BM_GPMI_CTRL1_HALF_PERIOD : 0);
92215e27d19SDario Binacchi 	return 0;
9233045f8e3SSascha Hauer }
9243045f8e3SSascha Hauer 
gpmi_nfc_apply_timings(struct gpmi_nand_data * this)925f53d4c10SChristian Eggers static int gpmi_nfc_apply_timings(struct gpmi_nand_data *this)
9263045f8e3SSascha Hauer {
9273045f8e3SSascha Hauer 	struct gpmi_nfc_hardware_timing *hw = &this->hw;
9283045f8e3SSascha Hauer 	struct resources *r = &this->resources;
9293045f8e3SSascha Hauer 	void __iomem *gpmi_regs = r->gpmi_regs;
9303045f8e3SSascha Hauer 	unsigned int dll_wait_time_us;
931f53d4c10SChristian Eggers 	int ret;
9323045f8e3SSascha Hauer 
933f53d4c10SChristian Eggers 	/* Clock dividers do NOT guarantee a clean clock signal on its output
934f53d4c10SChristian Eggers 	 * during the change of the divide factor on i.MX6Q/UL/SX. On i.MX7/8,
935f53d4c10SChristian Eggers 	 * all clock dividers provide these guarantee.
936f53d4c10SChristian Eggers 	 */
937f53d4c10SChristian Eggers 	if (GPMI_IS_MX6Q(this) || GPMI_IS_MX6SX(this))
938f53d4c10SChristian Eggers 		clk_disable_unprepare(r->clock[0]);
939f53d4c10SChristian Eggers 
940f53d4c10SChristian Eggers 	ret = clk_set_rate(r->clock[0], hw->clk_rate);
941f53d4c10SChristian Eggers 	if (ret) {
942f53d4c10SChristian Eggers 		dev_err(this->dev, "cannot set clock rate to %lu Hz: %d\n", hw->clk_rate, ret);
943f53d4c10SChristian Eggers 		return ret;
944f53d4c10SChristian Eggers 	}
945f53d4c10SChristian Eggers 
946f53d4c10SChristian Eggers 	if (GPMI_IS_MX6Q(this) || GPMI_IS_MX6SX(this)) {
947f53d4c10SChristian Eggers 		ret = clk_prepare_enable(r->clock[0]);
948f53d4c10SChristian Eggers 		if (ret)
949f53d4c10SChristian Eggers 			return ret;
950f53d4c10SChristian Eggers 	}
9513045f8e3SSascha Hauer 
9523045f8e3SSascha Hauer 	writel(hw->timing0, gpmi_regs + HW_GPMI_TIMING0);
9533045f8e3SSascha Hauer 	writel(hw->timing1, gpmi_regs + HW_GPMI_TIMING1);
9543045f8e3SSascha Hauer 
9553045f8e3SSascha Hauer 	/*
9563045f8e3SSascha Hauer 	 * Clear several CTRL1 fields, DLL must be disabled when setting
9573045f8e3SSascha Hauer 	 * RDN_DELAY or HALF_PERIOD.
9583045f8e3SSascha Hauer 	 */
9593045f8e3SSascha Hauer 	writel(BM_GPMI_CTRL1_CLEAR_MASK, gpmi_regs + HW_GPMI_CTRL1_CLR);
9603045f8e3SSascha Hauer 	writel(hw->ctrl1n, gpmi_regs + HW_GPMI_CTRL1_SET);
9613045f8e3SSascha Hauer 
9623045f8e3SSascha Hauer 	/* Wait 64 clock cycles before using the GPMI after enabling the DLL */
9633045f8e3SSascha Hauer 	dll_wait_time_us = USEC_PER_SEC / hw->clk_rate * 64;
9643045f8e3SSascha Hauer 	if (!dll_wait_time_us)
9653045f8e3SSascha Hauer 		dll_wait_time_us = 1;
9663045f8e3SSascha Hauer 
9673045f8e3SSascha Hauer 	/* Wait for the DLL to settle. */
9683045f8e3SSascha Hauer 	udelay(dll_wait_time_us);
969f53d4c10SChristian Eggers 
970f53d4c10SChristian Eggers 	return 0;
9713045f8e3SSascha Hauer }
9723045f8e3SSascha Hauer 
gpmi_setup_interface(struct nand_chip * chip,int chipnr,const struct nand_interface_config * conf)9734c46667bSMiquel Raynal static int gpmi_setup_interface(struct nand_chip *chip, int chipnr,
9744c46667bSMiquel Raynal 				const struct nand_interface_config *conf)
9753045f8e3SSascha Hauer {
9763045f8e3SSascha Hauer 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
9773045f8e3SSascha Hauer 	const struct nand_sdr_timings *sdr;
97815e27d19SDario Binacchi 	int ret;
9793045f8e3SSascha Hauer 
9803045f8e3SSascha Hauer 	/* Retrieve required NAND timings */
9813045f8e3SSascha Hauer 	sdr = nand_get_sdr_timings(conf);
9823045f8e3SSascha Hauer 	if (IS_ERR(sdr))
9833045f8e3SSascha Hauer 		return PTR_ERR(sdr);
9843045f8e3SSascha Hauer 
985ac178a21SDario Binacchi 	/* Only MX28/MX6 GPMI controller can reach EDO timings */
986ac178a21SDario Binacchi 	if (sdr->tRC_min <= 25000 && !GPMI_IS_MX28(this) && !GPMI_IS_MX6(this))
9873045f8e3SSascha Hauer 		return -ENOTSUPP;
9883045f8e3SSascha Hauer 
9893045f8e3SSascha Hauer 	/* Stop here if this call was just a check */
9903045f8e3SSascha Hauer 	if (chipnr < 0)
9913045f8e3SSascha Hauer 		return 0;
9923045f8e3SSascha Hauer 
9933045f8e3SSascha Hauer 	/* Do the actual derivation of the controller timings */
99415e27d19SDario Binacchi 	ret = gpmi_nfc_compute_timings(this, sdr);
99515e27d19SDario Binacchi 	if (ret)
99615e27d19SDario Binacchi 		return ret;
9973045f8e3SSascha Hauer 
9983045f8e3SSascha Hauer 	this->hw.must_apply_timings = true;
9993045f8e3SSascha Hauer 
10003045f8e3SSascha Hauer 	return 0;
10013045f8e3SSascha Hauer }
10023045f8e3SSascha Hauer 
10033045f8e3SSascha Hauer /* Clears a BCH interrupt. */
gpmi_clear_bch(struct gpmi_nand_data * this)10043045f8e3SSascha Hauer static void gpmi_clear_bch(struct gpmi_nand_data *this)
10053045f8e3SSascha Hauer {
10063045f8e3SSascha Hauer 	struct resources *r = &this->resources;
10073045f8e3SSascha Hauer 	writel(BM_BCH_CTRL_COMPLETE_IRQ, r->bch_regs + HW_BCH_CTRL_CLR);
10083045f8e3SSascha Hauer }
10093045f8e3SSascha Hauer 
get_dma_chan(struct gpmi_nand_data * this)10103045f8e3SSascha Hauer static struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
101193db446aSBoris Brezillon {
101293db446aSBoris Brezillon 	/* We use the DMA channel 0 to access all the nand chips. */
101393db446aSBoris Brezillon 	return this->dma_chans[0];
101493db446aSBoris Brezillon }
101593db446aSBoris Brezillon 
10163045f8e3SSascha Hauer /* This will be called after the DMA operation is finished. */
dma_irq_callback(void * param)10173045f8e3SSascha Hauer static void dma_irq_callback(void *param)
10183045f8e3SSascha Hauer {
10193045f8e3SSascha Hauer 	struct gpmi_nand_data *this = param;
10203045f8e3SSascha Hauer 	struct completion *dma_c = &this->dma_done;
10213045f8e3SSascha Hauer 
10223045f8e3SSascha Hauer 	complete(dma_c);
10233045f8e3SSascha Hauer }
10243045f8e3SSascha Hauer 
bch_irq(int irq,void * cookie)10253045f8e3SSascha Hauer static irqreturn_t bch_irq(int irq, void *cookie)
10263045f8e3SSascha Hauer {
10273045f8e3SSascha Hauer 	struct gpmi_nand_data *this = cookie;
10283045f8e3SSascha Hauer 
10293045f8e3SSascha Hauer 	gpmi_clear_bch(this);
10303045f8e3SSascha Hauer 	complete(&this->bch_done);
10313045f8e3SSascha Hauer 	return IRQ_HANDLED;
10323045f8e3SSascha Hauer }
10333045f8e3SSascha Hauer 
gpmi_raw_len_to_len(struct gpmi_nand_data * this,int raw_len)1034ef347c0cSSascha Hauer static int gpmi_raw_len_to_len(struct gpmi_nand_data *this, int raw_len)
1035ef347c0cSSascha Hauer {
10363045f8e3SSascha Hauer 	/*
1037ef347c0cSSascha Hauer 	 * raw_len is the length to read/write including bch data which
1038ef347c0cSSascha Hauer 	 * we are passed in exec_op. Calculate the data length from it.
10393045f8e3SSascha Hauer 	 */
1040ef347c0cSSascha Hauer 	if (this->bch)
10412fb038eaSHan Xu 		return ALIGN_DOWN(raw_len, this->bch_geometry.eccn_chunk_size);
1042ef347c0cSSascha Hauer 	else
1043ef347c0cSSascha Hauer 		return raw_len;
10443045f8e3SSascha Hauer }
10453045f8e3SSascha Hauer 
104693db446aSBoris Brezillon /* Can we use the upper's buffer directly for DMA? */
prepare_data_dma(struct gpmi_nand_data * this,const void * buf,int raw_len,struct scatterlist * sgl,enum dma_data_direction dr)10473045f8e3SSascha Hauer static bool prepare_data_dma(struct gpmi_nand_data *this, const void *buf,
1048ef347c0cSSascha Hauer 			     int raw_len, struct scatterlist *sgl,
1049ef347c0cSSascha Hauer 			     enum dma_data_direction dr)
105093db446aSBoris Brezillon {
105193db446aSBoris Brezillon 	int ret;
1052ef347c0cSSascha Hauer 	int len = gpmi_raw_len_to_len(this, raw_len);
105393db446aSBoris Brezillon 
105493db446aSBoris Brezillon 	/* first try to map the upper buffer directly */
1055ba3900e6SSascha Hauer 	if (virt_addr_valid(buf) && !object_is_on_stack(buf)) {
1056ba3900e6SSascha Hauer 		sg_init_one(sgl, buf, len);
105793db446aSBoris Brezillon 		ret = dma_map_sg(this->dev, sgl, 1, dr);
105893db446aSBoris Brezillon 		if (ret == 0)
105993db446aSBoris Brezillon 			goto map_fail;
106093db446aSBoris Brezillon 
1061111bfed4SSascha Hauer 		return true;
106293db446aSBoris Brezillon 	}
106393db446aSBoris Brezillon 
106493db446aSBoris Brezillon map_fail:
106593db446aSBoris Brezillon 	/* We have to use our own DMA buffer. */
1066ba3900e6SSascha Hauer 	sg_init_one(sgl, this->data_buffer_dma, len);
106793db446aSBoris Brezillon 
1068ef347c0cSSascha Hauer 	if (dr == DMA_TO_DEVICE && buf != this->data_buffer_dma)
1069ba3900e6SSascha Hauer 		memcpy(this->data_buffer_dma, buf, len);
107093db446aSBoris Brezillon 
107193db446aSBoris Brezillon 	dma_map_sg(this->dev, sgl, 1, dr);
107293db446aSBoris Brezillon 
1073111bfed4SSascha Hauer 	return false;
107493db446aSBoris Brezillon }
107593db446aSBoris Brezillon 
10763045f8e3SSascha Hauer /* add our owner bbt descriptor */
10773045f8e3SSascha Hauer static uint8_t scan_ff_pattern[] = { 0xff };
10783045f8e3SSascha Hauer static struct nand_bbt_descr gpmi_bbt_descr = {
10793045f8e3SSascha Hauer 	.options	= 0,
10803045f8e3SSascha Hauer 	.offs		= 0,
10813045f8e3SSascha Hauer 	.len		= 1,
10823045f8e3SSascha Hauer 	.pattern	= scan_ff_pattern
10833045f8e3SSascha Hauer };
10843045f8e3SSascha Hauer 
10853045f8e3SSascha Hauer /*
10863045f8e3SSascha Hauer  * We may change the layout if we can get the ECC info from the datasheet,
10873045f8e3SSascha Hauer  * else we will use all the (page + OOB).
10883045f8e3SSascha Hauer  */
gpmi_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)10893045f8e3SSascha Hauer static int gpmi_ooblayout_ecc(struct mtd_info *mtd, int section,
10903045f8e3SSascha Hauer 			      struct mtd_oob_region *oobregion)
10913045f8e3SSascha Hauer {
10923045f8e3SSascha Hauer 	struct nand_chip *chip = mtd_to_nand(mtd);
10933045f8e3SSascha Hauer 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
10943045f8e3SSascha Hauer 	struct bch_geometry *geo = &this->bch_geometry;
10953045f8e3SSascha Hauer 
10963045f8e3SSascha Hauer 	if (section)
10973045f8e3SSascha Hauer 		return -ERANGE;
10983045f8e3SSascha Hauer 
10993045f8e3SSascha Hauer 	oobregion->offset = 0;
11003045f8e3SSascha Hauer 	oobregion->length = geo->page_size - mtd->writesize;
11013045f8e3SSascha Hauer 
110293db446aSBoris Brezillon 	return 0;
110393db446aSBoris Brezillon }
110493db446aSBoris Brezillon 
gpmi_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)11053045f8e3SSascha Hauer static int gpmi_ooblayout_free(struct mtd_info *mtd, int section,
11063045f8e3SSascha Hauer 			       struct mtd_oob_region *oobregion)
11073045f8e3SSascha Hauer {
11083045f8e3SSascha Hauer 	struct nand_chip *chip = mtd_to_nand(mtd);
11093045f8e3SSascha Hauer 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
11103045f8e3SSascha Hauer 	struct bch_geometry *geo = &this->bch_geometry;
11113045f8e3SSascha Hauer 
11123045f8e3SSascha Hauer 	if (section)
11133045f8e3SSascha Hauer 		return -ERANGE;
11143045f8e3SSascha Hauer 
11153045f8e3SSascha Hauer 	/* The available oob size we have. */
11163045f8e3SSascha Hauer 	if (geo->page_size < mtd->writesize + mtd->oobsize) {
11173045f8e3SSascha Hauer 		oobregion->offset = geo->page_size - mtd->writesize;
11183045f8e3SSascha Hauer 		oobregion->length = mtd->oobsize - oobregion->offset;
11193045f8e3SSascha Hauer 	}
11203045f8e3SSascha Hauer 
11213045f8e3SSascha Hauer 	return 0;
11223045f8e3SSascha Hauer }
11233045f8e3SSascha Hauer 
11243045f8e3SSascha Hauer static const char * const gpmi_clks_for_mx2x[] = {
11253045f8e3SSascha Hauer 	"gpmi_io",
11263045f8e3SSascha Hauer };
11273045f8e3SSascha Hauer 
11283045f8e3SSascha Hauer static const struct mtd_ooblayout_ops gpmi_ooblayout_ops = {
11293045f8e3SSascha Hauer 	.ecc = gpmi_ooblayout_ecc,
11303045f8e3SSascha Hauer 	.free = gpmi_ooblayout_free,
11313045f8e3SSascha Hauer };
11323045f8e3SSascha Hauer 
11333045f8e3SSascha Hauer static const struct gpmi_devdata gpmi_devdata_imx23 = {
11343045f8e3SSascha Hauer 	.type = IS_MX23,
11353045f8e3SSascha Hauer 	.bch_max_ecc_strength = 20,
11363045f8e3SSascha Hauer 	.max_chain_delay = 16000,
11373045f8e3SSascha Hauer 	.clks = gpmi_clks_for_mx2x,
11383045f8e3SSascha Hauer 	.clks_count = ARRAY_SIZE(gpmi_clks_for_mx2x),
11393045f8e3SSascha Hauer };
11403045f8e3SSascha Hauer 
11413045f8e3SSascha Hauer static const struct gpmi_devdata gpmi_devdata_imx28 = {
11423045f8e3SSascha Hauer 	.type = IS_MX28,
11433045f8e3SSascha Hauer 	.bch_max_ecc_strength = 20,
11443045f8e3SSascha Hauer 	.max_chain_delay = 16000,
11453045f8e3SSascha Hauer 	.clks = gpmi_clks_for_mx2x,
11463045f8e3SSascha Hauer 	.clks_count = ARRAY_SIZE(gpmi_clks_for_mx2x),
11473045f8e3SSascha Hauer };
11483045f8e3SSascha Hauer 
11493045f8e3SSascha Hauer static const char * const gpmi_clks_for_mx6[] = {
11503045f8e3SSascha Hauer 	"gpmi_io", "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch",
11513045f8e3SSascha Hauer };
11523045f8e3SSascha Hauer 
11533045f8e3SSascha Hauer static const struct gpmi_devdata gpmi_devdata_imx6q = {
11543045f8e3SSascha Hauer 	.type = IS_MX6Q,
11553045f8e3SSascha Hauer 	.bch_max_ecc_strength = 40,
11563045f8e3SSascha Hauer 	.max_chain_delay = 12000,
11573045f8e3SSascha Hauer 	.clks = gpmi_clks_for_mx6,
11583045f8e3SSascha Hauer 	.clks_count = ARRAY_SIZE(gpmi_clks_for_mx6),
11593045f8e3SSascha Hauer };
11603045f8e3SSascha Hauer 
11613045f8e3SSascha Hauer static const struct gpmi_devdata gpmi_devdata_imx6sx = {
11623045f8e3SSascha Hauer 	.type = IS_MX6SX,
11633045f8e3SSascha Hauer 	.bch_max_ecc_strength = 62,
11643045f8e3SSascha Hauer 	.max_chain_delay = 12000,
11653045f8e3SSascha Hauer 	.clks = gpmi_clks_for_mx6,
11663045f8e3SSascha Hauer 	.clks_count = ARRAY_SIZE(gpmi_clks_for_mx6),
11673045f8e3SSascha Hauer };
11683045f8e3SSascha Hauer 
11693045f8e3SSascha Hauer static const char * const gpmi_clks_for_mx7d[] = {
11703045f8e3SSascha Hauer 	"gpmi_io", "gpmi_bch_apb",
11713045f8e3SSascha Hauer };
11723045f8e3SSascha Hauer 
11733045f8e3SSascha Hauer static const struct gpmi_devdata gpmi_devdata_imx7d = {
11743045f8e3SSascha Hauer 	.type = IS_MX7D,
11753045f8e3SSascha Hauer 	.bch_max_ecc_strength = 62,
11763045f8e3SSascha Hauer 	.max_chain_delay = 12000,
11773045f8e3SSascha Hauer 	.clks = gpmi_clks_for_mx7d,
11783045f8e3SSascha Hauer 	.clks_count = ARRAY_SIZE(gpmi_clks_for_mx7d),
11793045f8e3SSascha Hauer };
11803045f8e3SSascha Hauer 
acquire_register_block(struct gpmi_nand_data * this,const char * res_name)118193db446aSBoris Brezillon static int acquire_register_block(struct gpmi_nand_data *this,
118293db446aSBoris Brezillon 				  const char *res_name)
118393db446aSBoris Brezillon {
118493db446aSBoris Brezillon 	struct platform_device *pdev = this->pdev;
118593db446aSBoris Brezillon 	struct resources *res = &this->resources;
118693db446aSBoris Brezillon 	void __iomem *p;
118793db446aSBoris Brezillon 
1188fe6b7a9fSCai Huoqing 	p = devm_platform_ioremap_resource_byname(pdev, res_name);
118993db446aSBoris Brezillon 	if (IS_ERR(p))
119093db446aSBoris Brezillon 		return PTR_ERR(p);
119193db446aSBoris Brezillon 
119293db446aSBoris Brezillon 	if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
119393db446aSBoris Brezillon 		res->gpmi_regs = p;
119493db446aSBoris Brezillon 	else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
119593db446aSBoris Brezillon 		res->bch_regs = p;
119693db446aSBoris Brezillon 	else
119793db446aSBoris Brezillon 		dev_err(this->dev, "unknown resource name : %s\n", res_name);
119893db446aSBoris Brezillon 
119993db446aSBoris Brezillon 	return 0;
120093db446aSBoris Brezillon }
120193db446aSBoris Brezillon 
acquire_bch_irq(struct gpmi_nand_data * this,irq_handler_t irq_h)120293db446aSBoris Brezillon static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
120393db446aSBoris Brezillon {
120493db446aSBoris Brezillon 	struct platform_device *pdev = this->pdev;
120593db446aSBoris Brezillon 	const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
120693db446aSBoris Brezillon 	int err;
120793db446aSBoris Brezillon 
1208ecb78b29SLad Prabhakar 	err = platform_get_irq_byname(pdev, res_name);
1209ecb78b29SLad Prabhakar 	if (err < 0)
1210ecb78b29SLad Prabhakar 		return err;
121193db446aSBoris Brezillon 
1212ecb78b29SLad Prabhakar 	err = devm_request_irq(this->dev, err, irq_h, 0, res_name, this);
121393db446aSBoris Brezillon 	if (err)
121493db446aSBoris Brezillon 		dev_err(this->dev, "error requesting BCH IRQ\n");
121593db446aSBoris Brezillon 
121693db446aSBoris Brezillon 	return err;
121793db446aSBoris Brezillon }
121893db446aSBoris Brezillon 
release_dma_channels(struct gpmi_nand_data * this)121993db446aSBoris Brezillon static void release_dma_channels(struct gpmi_nand_data *this)
122093db446aSBoris Brezillon {
122193db446aSBoris Brezillon 	unsigned int i;
122293db446aSBoris Brezillon 	for (i = 0; i < DMA_CHANS; i++)
122393db446aSBoris Brezillon 		if (this->dma_chans[i]) {
122493db446aSBoris Brezillon 			dma_release_channel(this->dma_chans[i]);
122593db446aSBoris Brezillon 			this->dma_chans[i] = NULL;
122693db446aSBoris Brezillon 		}
122793db446aSBoris Brezillon }
122893db446aSBoris Brezillon 
acquire_dma_channels(struct gpmi_nand_data * this)122993db446aSBoris Brezillon static int acquire_dma_channels(struct gpmi_nand_data *this)
123093db446aSBoris Brezillon {
123193db446aSBoris Brezillon 	struct platform_device *pdev = this->pdev;
123293db446aSBoris Brezillon 	struct dma_chan *dma_chan;
12337cd8c0adSPeter Ujfalusi 	int ret = 0;
123493db446aSBoris Brezillon 
123593db446aSBoris Brezillon 	/* request dma channel */
12367cd8c0adSPeter Ujfalusi 	dma_chan = dma_request_chan(&pdev->dev, "rx-tx");
12377cd8c0adSPeter Ujfalusi 	if (IS_ERR(dma_chan)) {
123878a73491SKrzysztof Kozlowski 		ret = dev_err_probe(this->dev, PTR_ERR(dma_chan),
123978a73491SKrzysztof Kozlowski 				    "DMA channel request failed\n");
12407cd8c0adSPeter Ujfalusi 		release_dma_channels(this);
12417cd8c0adSPeter Ujfalusi 	} else {
12427cd8c0adSPeter Ujfalusi 		this->dma_chans[0] = dma_chan;
124393db446aSBoris Brezillon 	}
124493db446aSBoris Brezillon 
12457cd8c0adSPeter Ujfalusi 	return ret;
124693db446aSBoris Brezillon }
124793db446aSBoris Brezillon 
gpmi_get_clks(struct gpmi_nand_data * this)124893db446aSBoris Brezillon static int gpmi_get_clks(struct gpmi_nand_data *this)
124993db446aSBoris Brezillon {
125093db446aSBoris Brezillon 	struct resources *r = &this->resources;
125193db446aSBoris Brezillon 	struct clk *clk;
125293db446aSBoris Brezillon 	int err, i;
125393db446aSBoris Brezillon 
125493db446aSBoris Brezillon 	for (i = 0; i < this->devdata->clks_count; i++) {
125593db446aSBoris Brezillon 		clk = devm_clk_get(this->dev, this->devdata->clks[i]);
125693db446aSBoris Brezillon 		if (IS_ERR(clk)) {
125793db446aSBoris Brezillon 			err = PTR_ERR(clk);
125893db446aSBoris Brezillon 			goto err_clock;
125993db446aSBoris Brezillon 		}
126093db446aSBoris Brezillon 
126193db446aSBoris Brezillon 		r->clock[i] = clk;
126293db446aSBoris Brezillon 	}
126393db446aSBoris Brezillon 
126493db446aSBoris Brezillon 	return 0;
126593db446aSBoris Brezillon 
126693db446aSBoris Brezillon err_clock:
126793db446aSBoris Brezillon 	dev_dbg(this->dev, "failed in finding the clocks.\n");
126893db446aSBoris Brezillon 	return err;
126993db446aSBoris Brezillon }
127093db446aSBoris Brezillon 
acquire_resources(struct gpmi_nand_data * this)127193db446aSBoris Brezillon static int acquire_resources(struct gpmi_nand_data *this)
127293db446aSBoris Brezillon {
127393db446aSBoris Brezillon 	int ret;
127493db446aSBoris Brezillon 
127593db446aSBoris Brezillon 	ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
127693db446aSBoris Brezillon 	if (ret)
127793db446aSBoris Brezillon 		goto exit_regs;
127893db446aSBoris Brezillon 
127993db446aSBoris Brezillon 	ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
128093db446aSBoris Brezillon 	if (ret)
128193db446aSBoris Brezillon 		goto exit_regs;
128293db446aSBoris Brezillon 
128393db446aSBoris Brezillon 	ret = acquire_bch_irq(this, bch_irq);
128493db446aSBoris Brezillon 	if (ret)
128593db446aSBoris Brezillon 		goto exit_regs;
128693db446aSBoris Brezillon 
128793db446aSBoris Brezillon 	ret = acquire_dma_channels(this);
128893db446aSBoris Brezillon 	if (ret)
128993db446aSBoris Brezillon 		goto exit_regs;
129093db446aSBoris Brezillon 
129193db446aSBoris Brezillon 	ret = gpmi_get_clks(this);
129293db446aSBoris Brezillon 	if (ret)
129393db446aSBoris Brezillon 		goto exit_clock;
129493db446aSBoris Brezillon 	return 0;
129593db446aSBoris Brezillon 
129693db446aSBoris Brezillon exit_clock:
129793db446aSBoris Brezillon 	release_dma_channels(this);
129893db446aSBoris Brezillon exit_regs:
129993db446aSBoris Brezillon 	return ret;
130093db446aSBoris Brezillon }
130193db446aSBoris Brezillon 
release_resources(struct gpmi_nand_data * this)130293db446aSBoris Brezillon static void release_resources(struct gpmi_nand_data *this)
130393db446aSBoris Brezillon {
130493db446aSBoris Brezillon 	release_dma_channels(this);
130593db446aSBoris Brezillon }
130693db446aSBoris Brezillon 
gpmi_free_dma_buffer(struct gpmi_nand_data * this)130793db446aSBoris Brezillon static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
130893db446aSBoris Brezillon {
130993db446aSBoris Brezillon 	struct device *dev = this->dev;
1310ef347c0cSSascha Hauer 	struct bch_geometry *geo = &this->bch_geometry;
131193db446aSBoris Brezillon 
1312ef347c0cSSascha Hauer 	if (this->auxiliary_virt && virt_addr_valid(this->auxiliary_virt))
1313ef347c0cSSascha Hauer 		dma_free_coherent(dev, geo->auxiliary_size,
1314ef347c0cSSascha Hauer 					this->auxiliary_virt,
1315ef347c0cSSascha Hauer 					this->auxiliary_phys);
131693db446aSBoris Brezillon 	kfree(this->data_buffer_dma);
131793db446aSBoris Brezillon 	kfree(this->raw_buffer);
131893db446aSBoris Brezillon 
131993db446aSBoris Brezillon 	this->data_buffer_dma	= NULL;
132093db446aSBoris Brezillon 	this->raw_buffer	= NULL;
132193db446aSBoris Brezillon }
132293db446aSBoris Brezillon 
132393db446aSBoris Brezillon /* Allocate the DMA buffers */
gpmi_alloc_dma_buffer(struct gpmi_nand_data * this)132493db446aSBoris Brezillon static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
132593db446aSBoris Brezillon {
132693db446aSBoris Brezillon 	struct bch_geometry *geo = &this->bch_geometry;
132793db446aSBoris Brezillon 	struct device *dev = this->dev;
132893db446aSBoris Brezillon 	struct mtd_info *mtd = nand_to_mtd(&this->nand);
132993db446aSBoris Brezillon 
133093db446aSBoris Brezillon 	/*
133193db446aSBoris Brezillon 	 * [2] Allocate a read/write data buffer.
133293db446aSBoris Brezillon 	 *     The gpmi_alloc_dma_buffer can be called twice.
133393db446aSBoris Brezillon 	 *     We allocate a PAGE_SIZE length buffer if gpmi_alloc_dma_buffer
13345f8374d9SMiquel Raynal 	 *     is called before the NAND identification; and we allocate a
13355f8374d9SMiquel Raynal 	 *     buffer of the real NAND page size when the gpmi_alloc_dma_buffer
13365f8374d9SMiquel Raynal 	 *     is called after.
133793db446aSBoris Brezillon 	 */
133893db446aSBoris Brezillon 	this->data_buffer_dma = kzalloc(mtd->writesize ?: PAGE_SIZE,
133993db446aSBoris Brezillon 					GFP_DMA | GFP_KERNEL);
134093db446aSBoris Brezillon 	if (this->data_buffer_dma == NULL)
134193db446aSBoris Brezillon 		goto error_alloc;
134293db446aSBoris Brezillon 
1343ef347c0cSSascha Hauer 	this->auxiliary_virt = dma_alloc_coherent(dev, geo->auxiliary_size,
1344ef347c0cSSascha Hauer 					&this->auxiliary_phys, GFP_DMA);
1345ef347c0cSSascha Hauer 	if (!this->auxiliary_virt)
134693db446aSBoris Brezillon 		goto error_alloc;
134793db446aSBoris Brezillon 
1348ef347c0cSSascha Hauer 	this->raw_buffer = kzalloc((mtd->writesize ?: PAGE_SIZE) + mtd->oobsize, GFP_KERNEL);
134993db446aSBoris Brezillon 	if (!this->raw_buffer)
135093db446aSBoris Brezillon 		goto error_alloc;
135193db446aSBoris Brezillon 
135293db446aSBoris Brezillon 	return 0;
135393db446aSBoris Brezillon 
135493db446aSBoris Brezillon error_alloc:
135593db446aSBoris Brezillon 	gpmi_free_dma_buffer(this);
135693db446aSBoris Brezillon 	return -ENOMEM;
135793db446aSBoris Brezillon }
135893db446aSBoris Brezillon 
135993db446aSBoris Brezillon /*
136093db446aSBoris Brezillon  * Handles block mark swapping.
136193db446aSBoris Brezillon  * It can be called in swapping the block mark, or swapping it back,
1362d16da6d1SSlark Xiao  * because the operations are the same.
136393db446aSBoris Brezillon  */
block_mark_swapping(struct gpmi_nand_data * this,void * payload,void * auxiliary)136493db446aSBoris Brezillon static void block_mark_swapping(struct gpmi_nand_data *this,
136593db446aSBoris Brezillon 				void *payload, void *auxiliary)
136693db446aSBoris Brezillon {
136793db446aSBoris Brezillon 	struct bch_geometry *nfc_geo = &this->bch_geometry;
136893db446aSBoris Brezillon 	unsigned char *p;
136993db446aSBoris Brezillon 	unsigned char *a;
137093db446aSBoris Brezillon 	unsigned int  bit;
137193db446aSBoris Brezillon 	unsigned char mask;
137293db446aSBoris Brezillon 	unsigned char from_data;
137393db446aSBoris Brezillon 	unsigned char from_oob;
137493db446aSBoris Brezillon 
137593db446aSBoris Brezillon 	if (!this->swap_block_mark)
137693db446aSBoris Brezillon 		return;
137793db446aSBoris Brezillon 
137893db446aSBoris Brezillon 	/*
137993db446aSBoris Brezillon 	 * If control arrives here, we're swapping. Make some convenience
138093db446aSBoris Brezillon 	 * variables.
138193db446aSBoris Brezillon 	 */
138293db446aSBoris Brezillon 	bit = nfc_geo->block_mark_bit_offset;
138393db446aSBoris Brezillon 	p   = payload + nfc_geo->block_mark_byte_offset;
138493db446aSBoris Brezillon 	a   = auxiliary;
138593db446aSBoris Brezillon 
138693db446aSBoris Brezillon 	/*
138793db446aSBoris Brezillon 	 * Get the byte from the data area that overlays the block mark. Since
138893db446aSBoris Brezillon 	 * the ECC engine applies its own view to the bits in the page, the
138993db446aSBoris Brezillon 	 * physical block mark won't (in general) appear on a byte boundary in
139093db446aSBoris Brezillon 	 * the data.
139193db446aSBoris Brezillon 	 */
139293db446aSBoris Brezillon 	from_data = (p[0] >> bit) | (p[1] << (8 - bit));
139393db446aSBoris Brezillon 
139493db446aSBoris Brezillon 	/* Get the byte from the OOB. */
139593db446aSBoris Brezillon 	from_oob = a[0];
139693db446aSBoris Brezillon 
139793db446aSBoris Brezillon 	/* Swap them. */
139893db446aSBoris Brezillon 	a[0] = from_data;
139993db446aSBoris Brezillon 
140093db446aSBoris Brezillon 	mask = (0x1 << bit) - 1;
140193db446aSBoris Brezillon 	p[0] = (p[0] & mask) | (from_oob << bit);
140293db446aSBoris Brezillon 
140393db446aSBoris Brezillon 	mask = ~0 << bit;
140493db446aSBoris Brezillon 	p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
140593db446aSBoris Brezillon }
140693db446aSBoris Brezillon 
gpmi_count_bitflips(struct nand_chip * chip,void * buf,int first,int last,int meta)1407ef347c0cSSascha Hauer static int gpmi_count_bitflips(struct nand_chip *chip, void *buf, int first,
1408ef347c0cSSascha Hauer 			       int last, int meta)
140993db446aSBoris Brezillon {
141093db446aSBoris Brezillon 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
141193db446aSBoris Brezillon 	struct bch_geometry *nfc_geo = &this->bch_geometry;
141293db446aSBoris Brezillon 	struct mtd_info *mtd = nand_to_mtd(chip);
1413ef347c0cSSascha Hauer 	int i;
141493db446aSBoris Brezillon 	unsigned char *status;
141593db446aSBoris Brezillon 	unsigned int max_bitflips = 0;
141693db446aSBoris Brezillon 
141793db446aSBoris Brezillon 	/* Loop over status bytes, accumulating ECC status. */
1418ef347c0cSSascha Hauer 	status = this->auxiliary_virt + ALIGN(meta, 4);
141993db446aSBoris Brezillon 
1420ef347c0cSSascha Hauer 	for (i = first; i < last; i++, status++) {
142193db446aSBoris Brezillon 		if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
142293db446aSBoris Brezillon 			continue;
142393db446aSBoris Brezillon 
142493db446aSBoris Brezillon 		if (*status == STATUS_UNCORRECTABLE) {
142593db446aSBoris Brezillon 			int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
142693db446aSBoris Brezillon 			u8 *eccbuf = this->raw_buffer;
142793db446aSBoris Brezillon 			int offset, bitoffset;
142893db446aSBoris Brezillon 			int eccbytes;
142993db446aSBoris Brezillon 			int flips;
143093db446aSBoris Brezillon 
143193db446aSBoris Brezillon 			/* Read ECC bytes into our internal raw_buffer */
143293db446aSBoris Brezillon 			offset = nfc_geo->metadata_size * 8;
14332fb038eaSHan Xu 			offset += ((8 * nfc_geo->eccn_chunk_size) + eccbits) * (i + 1);
143493db446aSBoris Brezillon 			offset -= eccbits;
143593db446aSBoris Brezillon 			bitoffset = offset % 8;
143693db446aSBoris Brezillon 			eccbytes = DIV_ROUND_UP(offset + eccbits, 8);
143793db446aSBoris Brezillon 			offset /= 8;
143893db446aSBoris Brezillon 			eccbytes -= offset;
143993db446aSBoris Brezillon 			nand_change_read_column_op(chip, offset, eccbuf,
144093db446aSBoris Brezillon 						   eccbytes, false);
144193db446aSBoris Brezillon 
144293db446aSBoris Brezillon 			/*
144393db446aSBoris Brezillon 			 * ECC data are not byte aligned and we may have
144493db446aSBoris Brezillon 			 * in-band data in the first and last byte of
144593db446aSBoris Brezillon 			 * eccbuf. Set non-eccbits to one so that
144693db446aSBoris Brezillon 			 * nand_check_erased_ecc_chunk() does not count them
144793db446aSBoris Brezillon 			 * as bitflips.
144893db446aSBoris Brezillon 			 */
144993db446aSBoris Brezillon 			if (bitoffset)
145093db446aSBoris Brezillon 				eccbuf[0] |= GENMASK(bitoffset - 1, 0);
145193db446aSBoris Brezillon 
145293db446aSBoris Brezillon 			bitoffset = (bitoffset + eccbits) % 8;
145393db446aSBoris Brezillon 			if (bitoffset)
145493db446aSBoris Brezillon 				eccbuf[eccbytes - 1] |= GENMASK(7, bitoffset);
145593db446aSBoris Brezillon 
145693db446aSBoris Brezillon 			/*
145793db446aSBoris Brezillon 			 * The ECC hardware has an uncorrectable ECC status
145893db446aSBoris Brezillon 			 * code in case we have bitflips in an erased page. As
145993db446aSBoris Brezillon 			 * nothing was written into this subpage the ECC is
146093db446aSBoris Brezillon 			 * obviously wrong and we can not trust it. We assume
146193db446aSBoris Brezillon 			 * at this point that we are reading an erased page and
146293db446aSBoris Brezillon 			 * try to correct the bitflips in buffer up to
146393db446aSBoris Brezillon 			 * ecc_strength bitflips. If this is a page with random
146493db446aSBoris Brezillon 			 * data, we exceed this number of bitflips and have a
146593db446aSBoris Brezillon 			 * ECC failure. Otherwise we use the corrected buffer.
146693db446aSBoris Brezillon 			 */
146793db446aSBoris Brezillon 			if (i == 0) {
146893db446aSBoris Brezillon 				/* The first block includes metadata */
146993db446aSBoris Brezillon 				flips = nand_check_erased_ecc_chunk(
14702fb038eaSHan Xu 						buf + i * nfc_geo->eccn_chunk_size,
14712fb038eaSHan Xu 						nfc_geo->eccn_chunk_size,
147293db446aSBoris Brezillon 						eccbuf, eccbytes,
1473f6b74db8SSascha Hauer 						this->auxiliary_virt,
147493db446aSBoris Brezillon 						nfc_geo->metadata_size,
147593db446aSBoris Brezillon 						nfc_geo->ecc_strength);
147693db446aSBoris Brezillon 			} else {
147793db446aSBoris Brezillon 				flips = nand_check_erased_ecc_chunk(
14782fb038eaSHan Xu 						buf + i * nfc_geo->eccn_chunk_size,
14792fb038eaSHan Xu 						nfc_geo->eccn_chunk_size,
148093db446aSBoris Brezillon 						eccbuf, eccbytes,
148193db446aSBoris Brezillon 						NULL, 0,
148293db446aSBoris Brezillon 						nfc_geo->ecc_strength);
148393db446aSBoris Brezillon 			}
148493db446aSBoris Brezillon 
148593db446aSBoris Brezillon 			if (flips > 0) {
148693db446aSBoris Brezillon 				max_bitflips = max_t(unsigned int, max_bitflips,
148793db446aSBoris Brezillon 						     flips);
148893db446aSBoris Brezillon 				mtd->ecc_stats.corrected += flips;
148993db446aSBoris Brezillon 				continue;
149093db446aSBoris Brezillon 			}
149193db446aSBoris Brezillon 
149293db446aSBoris Brezillon 			mtd->ecc_stats.failed++;
149393db446aSBoris Brezillon 			continue;
149493db446aSBoris Brezillon 		}
149593db446aSBoris Brezillon 
149693db446aSBoris Brezillon 		mtd->ecc_stats.corrected += *status;
149793db446aSBoris Brezillon 		max_bitflips = max_t(unsigned int, max_bitflips, *status);
149893db446aSBoris Brezillon 	}
149993db446aSBoris Brezillon 
1500ad8b4f14SSascha Hauer 	return max_bitflips;
1501ad8b4f14SSascha Hauer }
1502ad8b4f14SSascha Hauer 
gpmi_bch_layout_std(struct gpmi_nand_data * this)1503ef347c0cSSascha Hauer static void gpmi_bch_layout_std(struct gpmi_nand_data *this)
1504ef347c0cSSascha Hauer {
1505ef347c0cSSascha Hauer 	struct bch_geometry *geo = &this->bch_geometry;
1506ef347c0cSSascha Hauer 	unsigned int ecc_strength = geo->ecc_strength >> 1;
1507ef347c0cSSascha Hauer 	unsigned int gf_len = geo->gf_len;
15082fb038eaSHan Xu 	unsigned int block0_size = geo->ecc0_chunk_size;
15092fb038eaSHan Xu 	unsigned int blockn_size = geo->eccn_chunk_size;
1510ef347c0cSSascha Hauer 
1511ef347c0cSSascha Hauer 	this->bch_flashlayout0 =
1512ef347c0cSSascha Hauer 		BF_BCH_FLASH0LAYOUT0_NBLOCKS(geo->ecc_chunk_count - 1) |
1513ef347c0cSSascha Hauer 		BF_BCH_FLASH0LAYOUT0_META_SIZE(geo->metadata_size) |
1514ef347c0cSSascha Hauer 		BF_BCH_FLASH0LAYOUT0_ECC0(ecc_strength, this) |
1515ef347c0cSSascha Hauer 		BF_BCH_FLASH0LAYOUT0_GF(gf_len, this) |
15162fb038eaSHan Xu 		BF_BCH_FLASH0LAYOUT0_DATA0_SIZE(block0_size, this);
1517ef347c0cSSascha Hauer 
1518ef347c0cSSascha Hauer 	this->bch_flashlayout1 =
1519ef347c0cSSascha Hauer 		BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(geo->page_size) |
1520ef347c0cSSascha Hauer 		BF_BCH_FLASH0LAYOUT1_ECCN(ecc_strength, this) |
1521ef347c0cSSascha Hauer 		BF_BCH_FLASH0LAYOUT1_GF(gf_len, this) |
15222fb038eaSHan Xu 		BF_BCH_FLASH0LAYOUT1_DATAN_SIZE(blockn_size, this);
1523ef347c0cSSascha Hauer }
1524ef347c0cSSascha Hauer 
gpmi_ecc_read_page(struct nand_chip * chip,uint8_t * buf,int oob_required,int page)1525ad8b4f14SSascha Hauer static int gpmi_ecc_read_page(struct nand_chip *chip, uint8_t *buf,
1526ad8b4f14SSascha Hauer 			      int oob_required, int page)
1527ad8b4f14SSascha Hauer {
1528ad8b4f14SSascha Hauer 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
1529ad8b4f14SSascha Hauer 	struct mtd_info *mtd = nand_to_mtd(chip);
1530ef347c0cSSascha Hauer 	struct bch_geometry *geo = &this->bch_geometry;
1531ef347c0cSSascha Hauer 	unsigned int max_bitflips;
1532ad8b4f14SSascha Hauer 	int ret;
1533ad8b4f14SSascha Hauer 
1534ef347c0cSSascha Hauer 	gpmi_bch_layout_std(this);
1535ef347c0cSSascha Hauer 	this->bch = true;
1536ad8b4f14SSascha Hauer 
1537ef347c0cSSascha Hauer 	ret = nand_read_page_op(chip, page, 0, buf, geo->page_size);
1538ef347c0cSSascha Hauer 	if (ret)
1539ad8b4f14SSascha Hauer 		return ret;
1540ad8b4f14SSascha Hauer 
1541ef347c0cSSascha Hauer 	max_bitflips = gpmi_count_bitflips(chip, buf, 0,
1542ef347c0cSSascha Hauer 					   geo->ecc_chunk_count,
1543ef347c0cSSascha Hauer 					   geo->auxiliary_status_offset);
1544ef347c0cSSascha Hauer 
1545ef347c0cSSascha Hauer 	/* handle the block mark swapping */
1546ef347c0cSSascha Hauer 	block_mark_swapping(this, buf, this->auxiliary_virt);
1547ef347c0cSSascha Hauer 
154893db446aSBoris Brezillon 	if (oob_required) {
154993db446aSBoris Brezillon 		/*
155093db446aSBoris Brezillon 		 * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
155193db446aSBoris Brezillon 		 * for details about our policy for delivering the OOB.
155293db446aSBoris Brezillon 		 *
155393db446aSBoris Brezillon 		 * We fill the caller's buffer with set bits, and then copy the
155493db446aSBoris Brezillon 		 * block mark to th caller's buffer. Note that, if block mark
155593db446aSBoris Brezillon 		 * swapping was necessary, it has already been done, so we can
155693db446aSBoris Brezillon 		 * rely on the first byte of the auxiliary buffer to contain
155793db446aSBoris Brezillon 		 * the block mark.
155893db446aSBoris Brezillon 		 */
155993db446aSBoris Brezillon 		memset(chip->oob_poi, ~0, mtd->oobsize);
1560f6b74db8SSascha Hauer 		chip->oob_poi[0] = ((uint8_t *)this->auxiliary_virt)[0];
156193db446aSBoris Brezillon 	}
156293db446aSBoris Brezillon 
1563ef347c0cSSascha Hauer 	return max_bitflips;
156493db446aSBoris Brezillon }
156593db446aSBoris Brezillon 
156693db446aSBoris Brezillon /* Fake a virtual small page for the subpage read */
gpmi_ecc_read_subpage(struct nand_chip * chip,uint32_t offs,uint32_t len,uint8_t * buf,int page)1567b9761687SBoris Brezillon static int gpmi_ecc_read_subpage(struct nand_chip *chip, uint32_t offs,
1568b9761687SBoris Brezillon 				 uint32_t len, uint8_t *buf, int page)
156993db446aSBoris Brezillon {
157093db446aSBoris Brezillon 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
157193db446aSBoris Brezillon 	struct bch_geometry *geo = &this->bch_geometry;
157293db446aSBoris Brezillon 	int size = chip->ecc.size; /* ECC chunk size */
157393db446aSBoris Brezillon 	int meta, n, page_size;
157493db446aSBoris Brezillon 	unsigned int max_bitflips;
1575ef347c0cSSascha Hauer 	unsigned int ecc_strength;
157693db446aSBoris Brezillon 	int first, last, marker_pos;
157793db446aSBoris Brezillon 	int ecc_parity_size;
157893db446aSBoris Brezillon 	int col = 0;
1579ef347c0cSSascha Hauer 	int ret;
158093db446aSBoris Brezillon 
158193db446aSBoris Brezillon 	/* The size of ECC parity */
158293db446aSBoris Brezillon 	ecc_parity_size = geo->gf_len * geo->ecc_strength / 8;
158393db446aSBoris Brezillon 
158493db446aSBoris Brezillon 	/* Align it with the chunk size */
158593db446aSBoris Brezillon 	first = offs / size;
158693db446aSBoris Brezillon 	last = (offs + len - 1) / size;
158793db446aSBoris Brezillon 
158893db446aSBoris Brezillon 	if (this->swap_block_mark) {
158993db446aSBoris Brezillon 		/*
159093db446aSBoris Brezillon 		 * Find the chunk which contains the Block Marker.
159193db446aSBoris Brezillon 		 * If this chunk is in the range of [first, last],
159293db446aSBoris Brezillon 		 * we have to read out the whole page.
159393db446aSBoris Brezillon 		 * Why? since we had swapped the data at the position of Block
159493db446aSBoris Brezillon 		 * Marker to the metadata which is bound with the chunk 0.
159593db446aSBoris Brezillon 		 */
159693db446aSBoris Brezillon 		marker_pos = geo->block_mark_byte_offset / size;
159793db446aSBoris Brezillon 		if (last >= marker_pos && first <= marker_pos) {
159893db446aSBoris Brezillon 			dev_dbg(this->dev,
159993db446aSBoris Brezillon 				"page:%d, first:%d, last:%d, marker at:%d\n",
160093db446aSBoris Brezillon 				page, first, last, marker_pos);
1601b9761687SBoris Brezillon 			return gpmi_ecc_read_page(chip, buf, 0, page);
160293db446aSBoris Brezillon 		}
160393db446aSBoris Brezillon 	}
160493db446aSBoris Brezillon 
1605d9edc4bcSHan Xu 	/*
1606d9edc4bcSHan Xu 	 * if there is an ECC dedicate for meta:
1607d9edc4bcSHan Xu 	 * - need to add an extra ECC size when calculating col and page_size,
1608d9edc4bcSHan Xu 	 *   if the meta size is NOT zero.
1609d9edc4bcSHan Xu 	 * - ecc0_chunk size need to set to the same size as other chunks,
1610d9edc4bcSHan Xu 	 *   if the meta size is zero.
1611d9edc4bcSHan Xu 	 */
1612d9edc4bcSHan Xu 
161393db446aSBoris Brezillon 	meta = geo->metadata_size;
161493db446aSBoris Brezillon 	if (first) {
1615d9edc4bcSHan Xu 		if (geo->ecc_for_meta)
1616d9edc4bcSHan Xu 			col = meta + ecc_parity_size
1617d9edc4bcSHan Xu 				+ (size + ecc_parity_size) * first;
1618d9edc4bcSHan Xu 		else
161993db446aSBoris Brezillon 			col = meta + (size + ecc_parity_size) * first;
1620d9edc4bcSHan Xu 
162193db446aSBoris Brezillon 		meta = 0;
162293db446aSBoris Brezillon 		buf = buf + first * size;
162393db446aSBoris Brezillon 	}
162493db446aSBoris Brezillon 
1625ef347c0cSSascha Hauer 	ecc_parity_size = geo->gf_len * geo->ecc_strength / 8;
162693db446aSBoris Brezillon 	n = last - first + 1;
1627d9edc4bcSHan Xu 
1628d9edc4bcSHan Xu 	if (geo->ecc_for_meta && meta)
1629d9edc4bcSHan Xu 		page_size = meta + ecc_parity_size
1630d9edc4bcSHan Xu 			    + (size + ecc_parity_size) * n;
1631d9edc4bcSHan Xu 	else
163293db446aSBoris Brezillon 		page_size = meta + (size + ecc_parity_size) * n;
1633d9edc4bcSHan Xu 
1634ef347c0cSSascha Hauer 	ecc_strength = geo->ecc_strength >> 1;
163593db446aSBoris Brezillon 
1636d9edc4bcSHan Xu 	this->bch_flashlayout0 = BF_BCH_FLASH0LAYOUT0_NBLOCKS(
1637d9edc4bcSHan Xu 		(geo->ecc_for_meta ? n : n - 1)) |
1638ef347c0cSSascha Hauer 		BF_BCH_FLASH0LAYOUT0_META_SIZE(meta) |
1639ef347c0cSSascha Hauer 		BF_BCH_FLASH0LAYOUT0_ECC0(ecc_strength, this) |
1640ef347c0cSSascha Hauer 		BF_BCH_FLASH0LAYOUT0_GF(geo->gf_len, this) |
1641d9edc4bcSHan Xu 		BF_BCH_FLASH0LAYOUT0_DATA0_SIZE((geo->ecc_for_meta ?
1642d9edc4bcSHan Xu 		0 : geo->ecc0_chunk_size), this);
164393db446aSBoris Brezillon 
1644ef347c0cSSascha Hauer 	this->bch_flashlayout1 = BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size) |
1645ef347c0cSSascha Hauer 		BF_BCH_FLASH0LAYOUT1_ECCN(ecc_strength, this) |
1646ef347c0cSSascha Hauer 		BF_BCH_FLASH0LAYOUT1_GF(geo->gf_len, this) |
16472fb038eaSHan Xu 		BF_BCH_FLASH0LAYOUT1_DATAN_SIZE(geo->eccn_chunk_size, this);
164893db446aSBoris Brezillon 
1649ef347c0cSSascha Hauer 	this->bch = true;
1650ef347c0cSSascha Hauer 
1651ef347c0cSSascha Hauer 	ret = nand_read_page_op(chip, page, col, buf, page_size);
1652ef347c0cSSascha Hauer 	if (ret)
1653ef347c0cSSascha Hauer 		return ret;
165493db446aSBoris Brezillon 
165593db446aSBoris Brezillon 	dev_dbg(this->dev, "page:%d(%d:%d)%d, chunk:(%d:%d), BCH PG size:%d\n",
165693db446aSBoris Brezillon 		page, offs, len, col, first, n, page_size);
165793db446aSBoris Brezillon 
1658ef347c0cSSascha Hauer 	max_bitflips = gpmi_count_bitflips(chip, buf, first, last, meta);
165993db446aSBoris Brezillon 
166093db446aSBoris Brezillon 	return max_bitflips;
166193db446aSBoris Brezillon }
166293db446aSBoris Brezillon 
gpmi_ecc_write_page(struct nand_chip * chip,const uint8_t * buf,int oob_required,int page)1663767eb6fbSBoris Brezillon static int gpmi_ecc_write_page(struct nand_chip *chip, const uint8_t *buf,
1664767eb6fbSBoris Brezillon 			       int oob_required, int page)
166593db446aSBoris Brezillon {
1666767eb6fbSBoris Brezillon 	struct mtd_info *mtd = nand_to_mtd(chip);
166793db446aSBoris Brezillon 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
166893db446aSBoris Brezillon 	struct bch_geometry *nfc_geo = &this->bch_geometry;
166993db446aSBoris Brezillon 
167093db446aSBoris Brezillon 	dev_dbg(this->dev, "ecc write page.\n");
167193db446aSBoris Brezillon 
1672ef347c0cSSascha Hauer 	gpmi_bch_layout_std(this);
1673ef347c0cSSascha Hauer 	this->bch = true;
1674ef347c0cSSascha Hauer 
1675ef347c0cSSascha Hauer 	memcpy(this->auxiliary_virt, chip->oob_poi, nfc_geo->auxiliary_size);
167693db446aSBoris Brezillon 
167793db446aSBoris Brezillon 	if (this->swap_block_mark) {
167893db446aSBoris Brezillon 		/*
1679ef347c0cSSascha Hauer 		 * When doing bad block marker swapping we must always copy the
1680ef347c0cSSascha Hauer 		 * input buffer as we can't modify the const buffer.
168193db446aSBoris Brezillon 		 */
1682ef347c0cSSascha Hauer 		memcpy(this->data_buffer_dma, buf, mtd->writesize);
1683ef347c0cSSascha Hauer 		buf = this->data_buffer_dma;
1684ef347c0cSSascha Hauer 		block_mark_swapping(this, this->data_buffer_dma,
1685ef347c0cSSascha Hauer 				    this->auxiliary_virt);
168693db446aSBoris Brezillon 	}
168793db446aSBoris Brezillon 
168835a441eeSMinghao Chi 	return nand_prog_page_op(chip, page, 0, buf, nfc_geo->page_size);
168993db446aSBoris Brezillon }
169093db446aSBoris Brezillon 
169193db446aSBoris Brezillon /*
169293db446aSBoris Brezillon  * There are several places in this driver where we have to handle the OOB and
169393db446aSBoris Brezillon  * block marks. This is the function where things are the most complicated, so
169493db446aSBoris Brezillon  * this is where we try to explain it all. All the other places refer back to
169593db446aSBoris Brezillon  * here.
169693db446aSBoris Brezillon  *
169793db446aSBoris Brezillon  * These are the rules, in order of decreasing importance:
169893db446aSBoris Brezillon  *
169993db446aSBoris Brezillon  * 1) Nothing the caller does can be allowed to imperil the block mark.
170093db446aSBoris Brezillon  *
170193db446aSBoris Brezillon  * 2) In read operations, the first byte of the OOB we return must reflect the
170293db446aSBoris Brezillon  *    true state of the block mark, no matter where that block mark appears in
170393db446aSBoris Brezillon  *    the physical page.
170493db446aSBoris Brezillon  *
170593db446aSBoris Brezillon  * 3) ECC-based read operations return an OOB full of set bits (since we never
170693db446aSBoris Brezillon  *    allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
170793db446aSBoris Brezillon  *    return).
170893db446aSBoris Brezillon  *
170993db446aSBoris Brezillon  * 4) "Raw" read operations return a direct view of the physical bytes in the
171093db446aSBoris Brezillon  *    page, using the conventional definition of which bytes are data and which
171193db446aSBoris Brezillon  *    are OOB. This gives the caller a way to see the actual, physical bytes
171293db446aSBoris Brezillon  *    in the page, without the distortions applied by our ECC engine.
171393db446aSBoris Brezillon  *
171493db446aSBoris Brezillon  *
171593db446aSBoris Brezillon  * What we do for this specific read operation depends on two questions:
171693db446aSBoris Brezillon  *
171793db446aSBoris Brezillon  * 1) Are we doing a "raw" read, or an ECC-based read?
171893db446aSBoris Brezillon  *
171993db446aSBoris Brezillon  * 2) Are we using block mark swapping or transcription?
172093db446aSBoris Brezillon  *
172193db446aSBoris Brezillon  * There are four cases, illustrated by the following Karnaugh map:
172293db446aSBoris Brezillon  *
172393db446aSBoris Brezillon  *                    |           Raw           |         ECC-based       |
172493db446aSBoris Brezillon  *       -------------+-------------------------+-------------------------+
172593db446aSBoris Brezillon  *                    | Read the conventional   |                         |
172693db446aSBoris Brezillon  *                    | OOB at the end of the   |                         |
172793db446aSBoris Brezillon  *       Swapping     | page and return it. It  |                         |
172893db446aSBoris Brezillon  *                    | contains exactly what   |                         |
172993db446aSBoris Brezillon  *                    | we want.                | Read the block mark and |
173093db446aSBoris Brezillon  *       -------------+-------------------------+ return it in a buffer   |
173193db446aSBoris Brezillon  *                    | Read the conventional   | full of set bits.       |
173293db446aSBoris Brezillon  *                    | OOB at the end of the   |                         |
173393db446aSBoris Brezillon  *                    | page and also the block |                         |
173493db446aSBoris Brezillon  *       Transcribing | mark in the metadata.   |                         |
173593db446aSBoris Brezillon  *                    | Copy the block mark     |                         |
173693db446aSBoris Brezillon  *                    | into the first byte of  |                         |
173793db446aSBoris Brezillon  *                    | the OOB.                |                         |
173893db446aSBoris Brezillon  *       -------------+-------------------------+-------------------------+
173993db446aSBoris Brezillon  *
174093db446aSBoris Brezillon  * Note that we break rule #4 in the Transcribing/Raw case because we're not
174193db446aSBoris Brezillon  * giving an accurate view of the actual, physical bytes in the page (we're
174293db446aSBoris Brezillon  * overwriting the block mark). That's OK because it's more important to follow
174393db446aSBoris Brezillon  * rule #2.
174493db446aSBoris Brezillon  *
174593db446aSBoris Brezillon  * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
174693db446aSBoris Brezillon  * easy. When reading a page, for example, the NAND Flash MTD code calls our
174793db446aSBoris Brezillon  * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
174893db446aSBoris Brezillon  * ECC-based or raw view of the page is implicit in which function it calls
174993db446aSBoris Brezillon  * (there is a similar pair of ECC-based/raw functions for writing).
175093db446aSBoris Brezillon  */
gpmi_ecc_read_oob(struct nand_chip * chip,int page)1751b9761687SBoris Brezillon static int gpmi_ecc_read_oob(struct nand_chip *chip, int page)
175293db446aSBoris Brezillon {
1753b9761687SBoris Brezillon 	struct mtd_info *mtd = nand_to_mtd(chip);
175493db446aSBoris Brezillon 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
1755b05d73d2SSascha Hauer 	int ret;
175693db446aSBoris Brezillon 
175793db446aSBoris Brezillon 	/* clear the OOB buffer */
175893db446aSBoris Brezillon 	memset(chip->oob_poi, ~0, mtd->oobsize);
175993db446aSBoris Brezillon 
176093db446aSBoris Brezillon 	/* Read out the conventional OOB. */
1761b05d73d2SSascha Hauer 	ret = nand_read_page_op(chip, page, mtd->writesize, chip->oob_poi,
1762b05d73d2SSascha Hauer 				mtd->oobsize);
1763b05d73d2SSascha Hauer 	if (ret)
1764b05d73d2SSascha Hauer 		return ret;
176593db446aSBoris Brezillon 
176693db446aSBoris Brezillon 	/*
176793db446aSBoris Brezillon 	 * Now, we want to make sure the block mark is correct. In the
176893db446aSBoris Brezillon 	 * non-transcribing case (!GPMI_IS_MX23()), we already have it.
176993db446aSBoris Brezillon 	 * Otherwise, we need to explicitly read it.
177093db446aSBoris Brezillon 	 */
177193db446aSBoris Brezillon 	if (GPMI_IS_MX23(this)) {
177293db446aSBoris Brezillon 		/* Read the block mark into the first byte of the OOB buffer. */
1773b05d73d2SSascha Hauer 		ret = nand_read_page_op(chip, page, 0, chip->oob_poi, 1);
1774b05d73d2SSascha Hauer 		if (ret)
1775b05d73d2SSascha Hauer 			return ret;
177693db446aSBoris Brezillon 	}
177793db446aSBoris Brezillon 
177893db446aSBoris Brezillon 	return 0;
177993db446aSBoris Brezillon }
178093db446aSBoris Brezillon 
gpmi_ecc_write_oob(struct nand_chip * chip,int page)1781767eb6fbSBoris Brezillon static int gpmi_ecc_write_oob(struct nand_chip *chip, int page)
178293db446aSBoris Brezillon {
1783767eb6fbSBoris Brezillon 	struct mtd_info *mtd = nand_to_mtd(chip);
178493db446aSBoris Brezillon 	struct mtd_oob_region of = { };
178593db446aSBoris Brezillon 
178693db446aSBoris Brezillon 	/* Do we have available oob area? */
178793db446aSBoris Brezillon 	mtd_ooblayout_free(mtd, 0, &of);
178893db446aSBoris Brezillon 	if (!of.length)
178993db446aSBoris Brezillon 		return -EPERM;
179093db446aSBoris Brezillon 
179193db446aSBoris Brezillon 	if (!nand_is_slc(chip))
179293db446aSBoris Brezillon 		return -EPERM;
179393db446aSBoris Brezillon 
179493db446aSBoris Brezillon 	return nand_prog_page_op(chip, page, mtd->writesize + of.offset,
179593db446aSBoris Brezillon 				 chip->oob_poi + of.offset, of.length);
179693db446aSBoris Brezillon }
179793db446aSBoris Brezillon 
179893db446aSBoris Brezillon /*
179993db446aSBoris Brezillon  * This function reads a NAND page without involving the ECC engine (no HW
180093db446aSBoris Brezillon  * ECC correction).
180193db446aSBoris Brezillon  * The tricky part in the GPMI/BCH controller is that it stores ECC bits
180293db446aSBoris Brezillon  * inline (interleaved with payload DATA), and do not align data chunk on
180393db446aSBoris Brezillon  * byte boundaries.
180493db446aSBoris Brezillon  * We thus need to take care moving the payload data and ECC bits stored in the
1805e5e5631cSMiquel Raynal  * page into the provided buffers, which is why we're using nand_extract_bits().
180693db446aSBoris Brezillon  *
180793db446aSBoris Brezillon  * See set_geometry_by_ecc_info inline comments to have a full description
180893db446aSBoris Brezillon  * of the layout used by the GPMI controller.
180993db446aSBoris Brezillon  */
gpmi_ecc_read_page_raw(struct nand_chip * chip,uint8_t * buf,int oob_required,int page)1810b9761687SBoris Brezillon static int gpmi_ecc_read_page_raw(struct nand_chip *chip, uint8_t *buf,
181193db446aSBoris Brezillon 				  int oob_required, int page)
181293db446aSBoris Brezillon {
1813b9761687SBoris Brezillon 	struct mtd_info *mtd = nand_to_mtd(chip);
181493db446aSBoris Brezillon 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
181593db446aSBoris Brezillon 	struct bch_geometry *nfc_geo = &this->bch_geometry;
18162fb038eaSHan Xu 	int eccsize = nfc_geo->eccn_chunk_size;
181793db446aSBoris Brezillon 	int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
181893db446aSBoris Brezillon 	u8 *tmp_buf = this->raw_buffer;
181993db446aSBoris Brezillon 	size_t src_bit_off;
182093db446aSBoris Brezillon 	size_t oob_bit_off;
182193db446aSBoris Brezillon 	size_t oob_byte_off;
182293db446aSBoris Brezillon 	uint8_t *oob = chip->oob_poi;
182393db446aSBoris Brezillon 	int step;
1824ef347c0cSSascha Hauer 	int ret;
182593db446aSBoris Brezillon 
1826ef347c0cSSascha Hauer 	ret = nand_read_page_op(chip, page, 0, tmp_buf,
182793db446aSBoris Brezillon 				mtd->writesize + mtd->oobsize);
1828ef347c0cSSascha Hauer 	if (ret)
1829ef347c0cSSascha Hauer 		return ret;
183093db446aSBoris Brezillon 
183193db446aSBoris Brezillon 	/*
183293db446aSBoris Brezillon 	 * If required, swap the bad block marker and the data stored in the
183393db446aSBoris Brezillon 	 * metadata section, so that we don't wrongly consider a block as bad.
183493db446aSBoris Brezillon 	 *
183593db446aSBoris Brezillon 	 * See the layout description for a detailed explanation on why this
183693db446aSBoris Brezillon 	 * is needed.
183793db446aSBoris Brezillon 	 */
183893db446aSBoris Brezillon 	if (this->swap_block_mark)
183993db446aSBoris Brezillon 		swap(tmp_buf[0], tmp_buf[mtd->writesize]);
184093db446aSBoris Brezillon 
184193db446aSBoris Brezillon 	/*
184293db446aSBoris Brezillon 	 * Copy the metadata section into the oob buffer (this section is
184393db446aSBoris Brezillon 	 * guaranteed to be aligned on a byte boundary).
184493db446aSBoris Brezillon 	 */
184593db446aSBoris Brezillon 	if (oob_required)
184693db446aSBoris Brezillon 		memcpy(oob, tmp_buf, nfc_geo->metadata_size);
184793db446aSBoris Brezillon 
184893db446aSBoris Brezillon 	oob_bit_off = nfc_geo->metadata_size * 8;
184993db446aSBoris Brezillon 	src_bit_off = oob_bit_off;
185093db446aSBoris Brezillon 
185193db446aSBoris Brezillon 	/* Extract interleaved payload data and ECC bits */
185293db446aSBoris Brezillon 	for (step = 0; step < nfc_geo->ecc_chunk_count; step++) {
185393db446aSBoris Brezillon 		if (buf)
18544883a60cSSean Nyekjaer 			nand_extract_bits(buf, step * eccsize * 8, tmp_buf,
1855e5e5631cSMiquel Raynal 					  src_bit_off, eccsize * 8);
185693db446aSBoris Brezillon 		src_bit_off += eccsize * 8;
185793db446aSBoris Brezillon 
185893db446aSBoris Brezillon 		/* Align last ECC block to align a byte boundary */
185993db446aSBoris Brezillon 		if (step == nfc_geo->ecc_chunk_count - 1 &&
186093db446aSBoris Brezillon 		    (oob_bit_off + eccbits) % 8)
186193db446aSBoris Brezillon 			eccbits += 8 - ((oob_bit_off + eccbits) % 8);
186293db446aSBoris Brezillon 
186393db446aSBoris Brezillon 		if (oob_required)
1864e5e5631cSMiquel Raynal 			nand_extract_bits(oob, oob_bit_off, tmp_buf,
1865e5e5631cSMiquel Raynal 					  src_bit_off, eccbits);
186693db446aSBoris Brezillon 
186793db446aSBoris Brezillon 		src_bit_off += eccbits;
186893db446aSBoris Brezillon 		oob_bit_off += eccbits;
186993db446aSBoris Brezillon 	}
187093db446aSBoris Brezillon 
187193db446aSBoris Brezillon 	if (oob_required) {
187293db446aSBoris Brezillon 		oob_byte_off = oob_bit_off / 8;
187393db446aSBoris Brezillon 
187493db446aSBoris Brezillon 		if (oob_byte_off < mtd->oobsize)
187593db446aSBoris Brezillon 			memcpy(oob + oob_byte_off,
187693db446aSBoris Brezillon 			       tmp_buf + mtd->writesize + oob_byte_off,
187793db446aSBoris Brezillon 			       mtd->oobsize - oob_byte_off);
187893db446aSBoris Brezillon 	}
187993db446aSBoris Brezillon 
188093db446aSBoris Brezillon 	return 0;
188193db446aSBoris Brezillon }
188293db446aSBoris Brezillon 
188393db446aSBoris Brezillon /*
188493db446aSBoris Brezillon  * This function writes a NAND page without involving the ECC engine (no HW
188593db446aSBoris Brezillon  * ECC generation).
188693db446aSBoris Brezillon  * The tricky part in the GPMI/BCH controller is that it stores ECC bits
188793db446aSBoris Brezillon  * inline (interleaved with payload DATA), and do not align data chunk on
188893db446aSBoris Brezillon  * byte boundaries.
188993db446aSBoris Brezillon  * We thus need to take care moving the OOB area at the right place in the
1890e5e5631cSMiquel Raynal  * final page, which is why we're using nand_extract_bits().
189193db446aSBoris Brezillon  *
189293db446aSBoris Brezillon  * See set_geometry_by_ecc_info inline comments to have a full description
189393db446aSBoris Brezillon  * of the layout used by the GPMI controller.
189493db446aSBoris Brezillon  */
gpmi_ecc_write_page_raw(struct nand_chip * chip,const uint8_t * buf,int oob_required,int page)1895767eb6fbSBoris Brezillon static int gpmi_ecc_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
189693db446aSBoris Brezillon 				   int oob_required, int page)
189793db446aSBoris Brezillon {
1898767eb6fbSBoris Brezillon 	struct mtd_info *mtd = nand_to_mtd(chip);
189993db446aSBoris Brezillon 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
190093db446aSBoris Brezillon 	struct bch_geometry *nfc_geo = &this->bch_geometry;
19012fb038eaSHan Xu 	int eccsize = nfc_geo->eccn_chunk_size;
190293db446aSBoris Brezillon 	int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
190393db446aSBoris Brezillon 	u8 *tmp_buf = this->raw_buffer;
190493db446aSBoris Brezillon 	uint8_t *oob = chip->oob_poi;
190593db446aSBoris Brezillon 	size_t dst_bit_off;
190693db446aSBoris Brezillon 	size_t oob_bit_off;
190793db446aSBoris Brezillon 	size_t oob_byte_off;
190893db446aSBoris Brezillon 	int step;
190993db446aSBoris Brezillon 
191093db446aSBoris Brezillon 	/*
191193db446aSBoris Brezillon 	 * Initialize all bits to 1 in case we don't have a buffer for the
191293db446aSBoris Brezillon 	 * payload or oob data in order to leave unspecified bits of data
191393db446aSBoris Brezillon 	 * to their initial state.
191493db446aSBoris Brezillon 	 */
191593db446aSBoris Brezillon 	if (!buf || !oob_required)
191693db446aSBoris Brezillon 		memset(tmp_buf, 0xff, mtd->writesize + mtd->oobsize);
191793db446aSBoris Brezillon 
191893db446aSBoris Brezillon 	/*
191993db446aSBoris Brezillon 	 * First copy the metadata section (stored in oob buffer) at the
192093db446aSBoris Brezillon 	 * beginning of the page, as imposed by the GPMI layout.
192193db446aSBoris Brezillon 	 */
192293db446aSBoris Brezillon 	memcpy(tmp_buf, oob, nfc_geo->metadata_size);
192393db446aSBoris Brezillon 	oob_bit_off = nfc_geo->metadata_size * 8;
192493db446aSBoris Brezillon 	dst_bit_off = oob_bit_off;
192593db446aSBoris Brezillon 
192693db446aSBoris Brezillon 	/* Interleave payload data and ECC bits */
192793db446aSBoris Brezillon 	for (step = 0; step < nfc_geo->ecc_chunk_count; step++) {
192893db446aSBoris Brezillon 		if (buf)
1929e5e5631cSMiquel Raynal 			nand_extract_bits(tmp_buf, dst_bit_off, buf,
1930e5e5631cSMiquel Raynal 					  step * eccsize * 8, eccsize * 8);
193193db446aSBoris Brezillon 		dst_bit_off += eccsize * 8;
193293db446aSBoris Brezillon 
193393db446aSBoris Brezillon 		/* Align last ECC block to align a byte boundary */
193493db446aSBoris Brezillon 		if (step == nfc_geo->ecc_chunk_count - 1 &&
193593db446aSBoris Brezillon 		    (oob_bit_off + eccbits) % 8)
193693db446aSBoris Brezillon 			eccbits += 8 - ((oob_bit_off + eccbits) % 8);
193793db446aSBoris Brezillon 
193893db446aSBoris Brezillon 		if (oob_required)
1939e5e5631cSMiquel Raynal 			nand_extract_bits(tmp_buf, dst_bit_off, oob,
1940e5e5631cSMiquel Raynal 					  oob_bit_off, eccbits);
194193db446aSBoris Brezillon 
194293db446aSBoris Brezillon 		dst_bit_off += eccbits;
194393db446aSBoris Brezillon 		oob_bit_off += eccbits;
194493db446aSBoris Brezillon 	}
194593db446aSBoris Brezillon 
194693db446aSBoris Brezillon 	oob_byte_off = oob_bit_off / 8;
194793db446aSBoris Brezillon 
194893db446aSBoris Brezillon 	if (oob_required && oob_byte_off < mtd->oobsize)
194993db446aSBoris Brezillon 		memcpy(tmp_buf + mtd->writesize + oob_byte_off,
195093db446aSBoris Brezillon 		       oob + oob_byte_off, mtd->oobsize - oob_byte_off);
195193db446aSBoris Brezillon 
195293db446aSBoris Brezillon 	/*
195393db446aSBoris Brezillon 	 * If required, swap the bad block marker and the first byte of the
195493db446aSBoris Brezillon 	 * metadata section, so that we don't modify the bad block marker.
195593db446aSBoris Brezillon 	 *
195693db446aSBoris Brezillon 	 * See the layout description for a detailed explanation on why this
195793db446aSBoris Brezillon 	 * is needed.
195893db446aSBoris Brezillon 	 */
195993db446aSBoris Brezillon 	if (this->swap_block_mark)
196093db446aSBoris Brezillon 		swap(tmp_buf[0], tmp_buf[mtd->writesize]);
196193db446aSBoris Brezillon 
196293db446aSBoris Brezillon 	return nand_prog_page_op(chip, page, 0, tmp_buf,
196393db446aSBoris Brezillon 				 mtd->writesize + mtd->oobsize);
196493db446aSBoris Brezillon }
196593db446aSBoris Brezillon 
gpmi_ecc_read_oob_raw(struct nand_chip * chip,int page)1966b9761687SBoris Brezillon static int gpmi_ecc_read_oob_raw(struct nand_chip *chip, int page)
196793db446aSBoris Brezillon {
1968b9761687SBoris Brezillon 	return gpmi_ecc_read_page_raw(chip, NULL, 1, page);
196993db446aSBoris Brezillon }
197093db446aSBoris Brezillon 
gpmi_ecc_write_oob_raw(struct nand_chip * chip,int page)1971767eb6fbSBoris Brezillon static int gpmi_ecc_write_oob_raw(struct nand_chip *chip, int page)
197293db446aSBoris Brezillon {
1973767eb6fbSBoris Brezillon 	return gpmi_ecc_write_page_raw(chip, NULL, 1, page);
197493db446aSBoris Brezillon }
197593db446aSBoris Brezillon 
gpmi_block_markbad(struct nand_chip * chip,loff_t ofs)1976c17556f5SBoris Brezillon static int gpmi_block_markbad(struct nand_chip *chip, loff_t ofs)
197793db446aSBoris Brezillon {
1978c17556f5SBoris Brezillon 	struct mtd_info *mtd = nand_to_mtd(chip);
197993db446aSBoris Brezillon 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
198093db446aSBoris Brezillon 	int ret = 0;
198193db446aSBoris Brezillon 	uint8_t *block_mark;
198293db446aSBoris Brezillon 	int column, page, chipnr;
198393db446aSBoris Brezillon 
198493db446aSBoris Brezillon 	chipnr = (int)(ofs >> chip->chip_shift);
19851d017859SBoris Brezillon 	nand_select_target(chip, chipnr);
198693db446aSBoris Brezillon 
198793db446aSBoris Brezillon 	column = !GPMI_IS_MX23(this) ? mtd->writesize : 0;
198893db446aSBoris Brezillon 
198993db446aSBoris Brezillon 	/* Write the block mark. */
199093db446aSBoris Brezillon 	block_mark = this->data_buffer_dma;
199193db446aSBoris Brezillon 	block_mark[0] = 0; /* bad block marker */
199293db446aSBoris Brezillon 
199393db446aSBoris Brezillon 	/* Shift to get page */
199493db446aSBoris Brezillon 	page = (int)(ofs >> chip->page_shift);
199593db446aSBoris Brezillon 
199693db446aSBoris Brezillon 	ret = nand_prog_page_op(chip, page, column, block_mark, 1);
199793db446aSBoris Brezillon 
19981d017859SBoris Brezillon 	nand_deselect_target(chip);
199993db446aSBoris Brezillon 
200093db446aSBoris Brezillon 	return ret;
200193db446aSBoris Brezillon }
200293db446aSBoris Brezillon 
nand_boot_set_geometry(struct gpmi_nand_data * this)200393db446aSBoris Brezillon static int nand_boot_set_geometry(struct gpmi_nand_data *this)
200493db446aSBoris Brezillon {
200593db446aSBoris Brezillon 	struct boot_rom_geometry *geometry = &this->rom_geometry;
200693db446aSBoris Brezillon 
200793db446aSBoris Brezillon 	/*
200893db446aSBoris Brezillon 	 * Set the boot block stride size.
200993db446aSBoris Brezillon 	 *
201093db446aSBoris Brezillon 	 * In principle, we should be reading this from the OTP bits, since
201193db446aSBoris Brezillon 	 * that's where the ROM is going to get it. In fact, we don't have any
201293db446aSBoris Brezillon 	 * way to read the OTP bits, so we go with the default and hope for the
201393db446aSBoris Brezillon 	 * best.
201493db446aSBoris Brezillon 	 */
201593db446aSBoris Brezillon 	geometry->stride_size_in_pages = 64;
201693db446aSBoris Brezillon 
201793db446aSBoris Brezillon 	/*
201893db446aSBoris Brezillon 	 * Set the search area stride exponent.
201993db446aSBoris Brezillon 	 *
202093db446aSBoris Brezillon 	 * In principle, we should be reading this from the OTP bits, since
202193db446aSBoris Brezillon 	 * that's where the ROM is going to get it. In fact, we don't have any
202293db446aSBoris Brezillon 	 * way to read the OTP bits, so we go with the default and hope for the
202393db446aSBoris Brezillon 	 * best.
202493db446aSBoris Brezillon 	 */
202593db446aSBoris Brezillon 	geometry->search_area_stride_exponent = 2;
202693db446aSBoris Brezillon 	return 0;
202793db446aSBoris Brezillon }
202893db446aSBoris Brezillon 
202993db446aSBoris Brezillon static const char  *fingerprint = "STMP";
mx23_check_transcription_stamp(struct gpmi_nand_data * this)203093db446aSBoris Brezillon static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
203193db446aSBoris Brezillon {
203293db446aSBoris Brezillon 	struct boot_rom_geometry *rom_geo = &this->rom_geometry;
203393db446aSBoris Brezillon 	struct device *dev = this->dev;
203493db446aSBoris Brezillon 	struct nand_chip *chip = &this->nand;
203593db446aSBoris Brezillon 	unsigned int search_area_size_in_strides;
203693db446aSBoris Brezillon 	unsigned int stride;
203793db446aSBoris Brezillon 	unsigned int page;
2038eeab7174SBoris Brezillon 	u8 *buffer = nand_get_data_buf(chip);
203993db446aSBoris Brezillon 	int found_an_ncb_fingerprint = false;
2040b05d73d2SSascha Hauer 	int ret;
204193db446aSBoris Brezillon 
204293db446aSBoris Brezillon 	/* Compute the number of strides in a search area. */
204393db446aSBoris Brezillon 	search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
204493db446aSBoris Brezillon 
20451d017859SBoris Brezillon 	nand_select_target(chip, 0);
204693db446aSBoris Brezillon 
204793db446aSBoris Brezillon 	/*
204893db446aSBoris Brezillon 	 * Loop through the first search area, looking for the NCB fingerprint.
204993db446aSBoris Brezillon 	 */
205093db446aSBoris Brezillon 	dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
205193db446aSBoris Brezillon 
205293db446aSBoris Brezillon 	for (stride = 0; stride < search_area_size_in_strides; stride++) {
205393db446aSBoris Brezillon 		/* Compute the page addresses. */
205493db446aSBoris Brezillon 		page = stride * rom_geo->stride_size_in_pages;
205593db446aSBoris Brezillon 
205693db446aSBoris Brezillon 		dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
205793db446aSBoris Brezillon 
205893db446aSBoris Brezillon 		/*
205993db446aSBoris Brezillon 		 * Read the NCB fingerprint. The fingerprint is four bytes long
206093db446aSBoris Brezillon 		 * and starts in the 12th byte of the page.
206193db446aSBoris Brezillon 		 */
2062b05d73d2SSascha Hauer 		ret = nand_read_page_op(chip, page, 12, buffer,
2063b05d73d2SSascha Hauer 					strlen(fingerprint));
2064b05d73d2SSascha Hauer 		if (ret)
2065b05d73d2SSascha Hauer 			continue;
206693db446aSBoris Brezillon 
206793db446aSBoris Brezillon 		/* Look for the fingerprint. */
206893db446aSBoris Brezillon 		if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
206993db446aSBoris Brezillon 			found_an_ncb_fingerprint = true;
207093db446aSBoris Brezillon 			break;
207193db446aSBoris Brezillon 		}
207293db446aSBoris Brezillon 
207393db446aSBoris Brezillon 	}
207493db446aSBoris Brezillon 
20751d017859SBoris Brezillon 	nand_deselect_target(chip);
207693db446aSBoris Brezillon 
207793db446aSBoris Brezillon 	if (found_an_ncb_fingerprint)
207893db446aSBoris Brezillon 		dev_dbg(dev, "\tFound a fingerprint\n");
207993db446aSBoris Brezillon 	else
208093db446aSBoris Brezillon 		dev_dbg(dev, "\tNo fingerprint found\n");
208193db446aSBoris Brezillon 	return found_an_ncb_fingerprint;
208293db446aSBoris Brezillon }
208393db446aSBoris Brezillon 
208493db446aSBoris Brezillon /* Writes a transcription stamp. */
mx23_write_transcription_stamp(struct gpmi_nand_data * this)208593db446aSBoris Brezillon static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
208693db446aSBoris Brezillon {
208793db446aSBoris Brezillon 	struct device *dev = this->dev;
208893db446aSBoris Brezillon 	struct boot_rom_geometry *rom_geo = &this->rom_geometry;
208993db446aSBoris Brezillon 	struct nand_chip *chip = &this->nand;
209093db446aSBoris Brezillon 	struct mtd_info *mtd = nand_to_mtd(chip);
209193db446aSBoris Brezillon 	unsigned int block_size_in_pages;
209293db446aSBoris Brezillon 	unsigned int search_area_size_in_strides;
209393db446aSBoris Brezillon 	unsigned int search_area_size_in_pages;
209493db446aSBoris Brezillon 	unsigned int search_area_size_in_blocks;
209593db446aSBoris Brezillon 	unsigned int block;
209693db446aSBoris Brezillon 	unsigned int stride;
209793db446aSBoris Brezillon 	unsigned int page;
2098eeab7174SBoris Brezillon 	u8 *buffer = nand_get_data_buf(chip);
209993db446aSBoris Brezillon 	int status;
210093db446aSBoris Brezillon 
210193db446aSBoris Brezillon 	/* Compute the search area geometry. */
210293db446aSBoris Brezillon 	block_size_in_pages = mtd->erasesize / mtd->writesize;
210393db446aSBoris Brezillon 	search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
210493db446aSBoris Brezillon 	search_area_size_in_pages = search_area_size_in_strides *
210593db446aSBoris Brezillon 					rom_geo->stride_size_in_pages;
210693db446aSBoris Brezillon 	search_area_size_in_blocks =
210793db446aSBoris Brezillon 		  (search_area_size_in_pages + (block_size_in_pages - 1)) /
210893db446aSBoris Brezillon 				    block_size_in_pages;
210993db446aSBoris Brezillon 
211093db446aSBoris Brezillon 	dev_dbg(dev, "Search Area Geometry :\n");
211193db446aSBoris Brezillon 	dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
211293db446aSBoris Brezillon 	dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
211393db446aSBoris Brezillon 	dev_dbg(dev, "\tin Pages  : %u\n", search_area_size_in_pages);
211493db446aSBoris Brezillon 
21151d017859SBoris Brezillon 	nand_select_target(chip, 0);
211693db446aSBoris Brezillon 
211793db446aSBoris Brezillon 	/* Loop over blocks in the first search area, erasing them. */
211893db446aSBoris Brezillon 	dev_dbg(dev, "Erasing the search area...\n");
211993db446aSBoris Brezillon 
212093db446aSBoris Brezillon 	for (block = 0; block < search_area_size_in_blocks; block++) {
212193db446aSBoris Brezillon 		/* Erase this block. */
212293db446aSBoris Brezillon 		dev_dbg(dev, "\tErasing block 0x%x\n", block);
212393db446aSBoris Brezillon 		status = nand_erase_op(chip, block);
212493db446aSBoris Brezillon 		if (status)
212593db446aSBoris Brezillon 			dev_err(dev, "[%s] Erase failed.\n", __func__);
212693db446aSBoris Brezillon 	}
212793db446aSBoris Brezillon 
212893db446aSBoris Brezillon 	/* Write the NCB fingerprint into the page buffer. */
212993db446aSBoris Brezillon 	memset(buffer, ~0, mtd->writesize);
213093db446aSBoris Brezillon 	memcpy(buffer + 12, fingerprint, strlen(fingerprint));
213193db446aSBoris Brezillon 
213293db446aSBoris Brezillon 	/* Loop through the first search area, writing NCB fingerprints. */
213393db446aSBoris Brezillon 	dev_dbg(dev, "Writing NCB fingerprints...\n");
213493db446aSBoris Brezillon 	for (stride = 0; stride < search_area_size_in_strides; stride++) {
213593db446aSBoris Brezillon 		/* Compute the page addresses. */
213693db446aSBoris Brezillon 		page = stride * rom_geo->stride_size_in_pages;
213793db446aSBoris Brezillon 
213893db446aSBoris Brezillon 		/* Write the first page of the current stride. */
213993db446aSBoris Brezillon 		dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
214093db446aSBoris Brezillon 
2141767eb6fbSBoris Brezillon 		status = chip->ecc.write_page_raw(chip, buffer, 0, page);
214293db446aSBoris Brezillon 		if (status)
214393db446aSBoris Brezillon 			dev_err(dev, "[%s] Write failed.\n", __func__);
214493db446aSBoris Brezillon 	}
214593db446aSBoris Brezillon 
21461d017859SBoris Brezillon 	nand_deselect_target(chip);
21471d017859SBoris Brezillon 
214893db446aSBoris Brezillon 	return 0;
214993db446aSBoris Brezillon }
215093db446aSBoris Brezillon 
mx23_boot_init(struct gpmi_nand_data * this)215193db446aSBoris Brezillon static int mx23_boot_init(struct gpmi_nand_data  *this)
215293db446aSBoris Brezillon {
215393db446aSBoris Brezillon 	struct device *dev = this->dev;
215493db446aSBoris Brezillon 	struct nand_chip *chip = &this->nand;
215593db446aSBoris Brezillon 	struct mtd_info *mtd = nand_to_mtd(chip);
215693db446aSBoris Brezillon 	unsigned int block_count;
215793db446aSBoris Brezillon 	unsigned int block;
215893db446aSBoris Brezillon 	int     chipnr;
215993db446aSBoris Brezillon 	int     page;
216093db446aSBoris Brezillon 	loff_t  byte;
216193db446aSBoris Brezillon 	uint8_t block_mark;
216293db446aSBoris Brezillon 	int     ret = 0;
216393db446aSBoris Brezillon 
216493db446aSBoris Brezillon 	/*
216593db446aSBoris Brezillon 	 * If control arrives here, we can't use block mark swapping, which
216693db446aSBoris Brezillon 	 * means we're forced to use transcription. First, scan for the
216793db446aSBoris Brezillon 	 * transcription stamp. If we find it, then we don't have to do
216893db446aSBoris Brezillon 	 * anything -- the block marks are already transcribed.
216993db446aSBoris Brezillon 	 */
217093db446aSBoris Brezillon 	if (mx23_check_transcription_stamp(this))
217193db446aSBoris Brezillon 		return 0;
217293db446aSBoris Brezillon 
217393db446aSBoris Brezillon 	/*
217493db446aSBoris Brezillon 	 * If control arrives here, we couldn't find a transcription stamp, so
217593db446aSBoris Brezillon 	 * so we presume the block marks are in the conventional location.
217693db446aSBoris Brezillon 	 */
217793db446aSBoris Brezillon 	dev_dbg(dev, "Transcribing bad block marks...\n");
217893db446aSBoris Brezillon 
217993db446aSBoris Brezillon 	/* Compute the number of blocks in the entire medium. */
21806c836d51SBoris Brezillon 	block_count = nanddev_eraseblocks_per_target(&chip->base);
218193db446aSBoris Brezillon 
218293db446aSBoris Brezillon 	/*
218393db446aSBoris Brezillon 	 * Loop over all the blocks in the medium, transcribing block marks as
218493db446aSBoris Brezillon 	 * we go.
218593db446aSBoris Brezillon 	 */
218693db446aSBoris Brezillon 	for (block = 0; block < block_count; block++) {
218793db446aSBoris Brezillon 		/*
218893db446aSBoris Brezillon 		 * Compute the chip, page and byte addresses for this block's
218993db446aSBoris Brezillon 		 * conventional mark.
219093db446aSBoris Brezillon 		 */
219193db446aSBoris Brezillon 		chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
219293db446aSBoris Brezillon 		page = block << (chip->phys_erase_shift - chip->page_shift);
219393db446aSBoris Brezillon 		byte = block <<  chip->phys_erase_shift;
219493db446aSBoris Brezillon 
219593db446aSBoris Brezillon 		/* Send the command to read the conventional block mark. */
21961d017859SBoris Brezillon 		nand_select_target(chip, chipnr);
2197b05d73d2SSascha Hauer 		ret = nand_read_page_op(chip, page, mtd->writesize, &block_mark,
2198b05d73d2SSascha Hauer 					1);
21991d017859SBoris Brezillon 		nand_deselect_target(chip);
220093db446aSBoris Brezillon 
2201b05d73d2SSascha Hauer 		if (ret)
2202b05d73d2SSascha Hauer 			continue;
2203b05d73d2SSascha Hauer 
220493db446aSBoris Brezillon 		/*
220593db446aSBoris Brezillon 		 * Check if the block is marked bad. If so, we need to mark it
220693db446aSBoris Brezillon 		 * again, but this time the result will be a mark in the
220793db446aSBoris Brezillon 		 * location where we transcribe block marks.
220893db446aSBoris Brezillon 		 */
220993db446aSBoris Brezillon 		if (block_mark != 0xff) {
221093db446aSBoris Brezillon 			dev_dbg(dev, "Transcribing mark in block %u\n", block);
2211cdc784c7SBoris Brezillon 			ret = chip->legacy.block_markbad(chip, byte);
221293db446aSBoris Brezillon 			if (ret)
221393db446aSBoris Brezillon 				dev_err(dev,
221493db446aSBoris Brezillon 					"Failed to mark block bad with ret %d\n",
221593db446aSBoris Brezillon 					ret);
221693db446aSBoris Brezillon 		}
221793db446aSBoris Brezillon 	}
221893db446aSBoris Brezillon 
221993db446aSBoris Brezillon 	/* Write the stamp that indicates we've transcribed the block marks. */
222093db446aSBoris Brezillon 	mx23_write_transcription_stamp(this);
222193db446aSBoris Brezillon 	return 0;
222293db446aSBoris Brezillon }
222393db446aSBoris Brezillon 
nand_boot_init(struct gpmi_nand_data * this)222493db446aSBoris Brezillon static int nand_boot_init(struct gpmi_nand_data  *this)
222593db446aSBoris Brezillon {
222693db446aSBoris Brezillon 	nand_boot_set_geometry(this);
222793db446aSBoris Brezillon 
222893db446aSBoris Brezillon 	/* This is ROM arch-specific initilization before the BBT scanning. */
222993db446aSBoris Brezillon 	if (GPMI_IS_MX23(this))
223093db446aSBoris Brezillon 		return mx23_boot_init(this);
223193db446aSBoris Brezillon 	return 0;
223293db446aSBoris Brezillon }
223393db446aSBoris Brezillon 
gpmi_set_geometry(struct gpmi_nand_data * this)223493db446aSBoris Brezillon static int gpmi_set_geometry(struct gpmi_nand_data *this)
223593db446aSBoris Brezillon {
223693db446aSBoris Brezillon 	int ret;
223793db446aSBoris Brezillon 
223893db446aSBoris Brezillon 	/* Free the temporary DMA memory for reading ID. */
223993db446aSBoris Brezillon 	gpmi_free_dma_buffer(this);
224093db446aSBoris Brezillon 
224193db446aSBoris Brezillon 	/* Set up the NFC geometry which is used by BCH. */
224293db446aSBoris Brezillon 	ret = bch_set_geometry(this);
224393db446aSBoris Brezillon 	if (ret) {
224493db446aSBoris Brezillon 		dev_err(this->dev, "Error setting BCH geometry : %d\n", ret);
224593db446aSBoris Brezillon 		return ret;
224693db446aSBoris Brezillon 	}
224793db446aSBoris Brezillon 
224893db446aSBoris Brezillon 	/* Alloc the new DMA buffers according to the pagesize and oobsize */
224993db446aSBoris Brezillon 	return gpmi_alloc_dma_buffer(this);
225093db446aSBoris Brezillon }
225193db446aSBoris Brezillon 
gpmi_init_last(struct gpmi_nand_data * this)225293db446aSBoris Brezillon static int gpmi_init_last(struct gpmi_nand_data *this)
225393db446aSBoris Brezillon {
225493db446aSBoris Brezillon 	struct nand_chip *chip = &this->nand;
225593db446aSBoris Brezillon 	struct mtd_info *mtd = nand_to_mtd(chip);
225693db446aSBoris Brezillon 	struct nand_ecc_ctrl *ecc = &chip->ecc;
225793db446aSBoris Brezillon 	struct bch_geometry *bch_geo = &this->bch_geometry;
225893db446aSBoris Brezillon 	int ret;
225993db446aSBoris Brezillon 
226093db446aSBoris Brezillon 	/* Set up the medium geometry */
226193db446aSBoris Brezillon 	ret = gpmi_set_geometry(this);
226293db446aSBoris Brezillon 	if (ret)
226393db446aSBoris Brezillon 		return ret;
226493db446aSBoris Brezillon 
226593db446aSBoris Brezillon 	/* Init the nand_ecc_ctrl{} */
226693db446aSBoris Brezillon 	ecc->read_page	= gpmi_ecc_read_page;
226793db446aSBoris Brezillon 	ecc->write_page	= gpmi_ecc_write_page;
226893db446aSBoris Brezillon 	ecc->read_oob	= gpmi_ecc_read_oob;
226993db446aSBoris Brezillon 	ecc->write_oob	= gpmi_ecc_write_oob;
227093db446aSBoris Brezillon 	ecc->read_page_raw = gpmi_ecc_read_page_raw;
227193db446aSBoris Brezillon 	ecc->write_page_raw = gpmi_ecc_write_page_raw;
227293db446aSBoris Brezillon 	ecc->read_oob_raw = gpmi_ecc_read_oob_raw;
227393db446aSBoris Brezillon 	ecc->write_oob_raw = gpmi_ecc_write_oob_raw;
2274bace41f8SMiquel Raynal 	ecc->engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
22752fb038eaSHan Xu 	ecc->size	= bch_geo->eccn_chunk_size;
227693db446aSBoris Brezillon 	ecc->strength	= bch_geo->ecc_strength;
227793db446aSBoris Brezillon 	mtd_set_ooblayout(mtd, &gpmi_ooblayout_ops);
227893db446aSBoris Brezillon 
227993db446aSBoris Brezillon 	/*
228093db446aSBoris Brezillon 	 * We only enable the subpage read when:
228193db446aSBoris Brezillon 	 *  (1) the chip is imx6, and
228293db446aSBoris Brezillon 	 *  (2) the size of the ECC parity is byte aligned.
228393db446aSBoris Brezillon 	 */
228493db446aSBoris Brezillon 	if (GPMI_IS_MX6(this) &&
228593db446aSBoris Brezillon 		((bch_geo->gf_len * bch_geo->ecc_strength) % 8) == 0) {
228693db446aSBoris Brezillon 		ecc->read_subpage = gpmi_ecc_read_subpage;
228793db446aSBoris Brezillon 		chip->options |= NAND_SUBPAGE_READ;
228893db446aSBoris Brezillon 	}
228993db446aSBoris Brezillon 
229093db446aSBoris Brezillon 	return 0;
229193db446aSBoris Brezillon }
229293db446aSBoris Brezillon 
gpmi_nand_attach_chip(struct nand_chip * chip)22935f8374d9SMiquel Raynal static int gpmi_nand_attach_chip(struct nand_chip *chip)
22945f8374d9SMiquel Raynal {
22955f8374d9SMiquel Raynal 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
22965f8374d9SMiquel Raynal 	int ret;
22975f8374d9SMiquel Raynal 
22985f8374d9SMiquel Raynal 	if (chip->bbt_options & NAND_BBT_USE_FLASH) {
22995f8374d9SMiquel Raynal 		chip->bbt_options |= NAND_BBT_NO_OOB;
23005f8374d9SMiquel Raynal 
23015f8374d9SMiquel Raynal 		if (of_property_read_bool(this->dev->of_node,
23025f8374d9SMiquel Raynal 					  "fsl,no-blockmark-swap"))
23035f8374d9SMiquel Raynal 			this->swap_block_mark = false;
23045f8374d9SMiquel Raynal 	}
23055f8374d9SMiquel Raynal 	dev_dbg(this->dev, "Blockmark swapping %sabled\n",
23065f8374d9SMiquel Raynal 		this->swap_block_mark ? "en" : "dis");
23075f8374d9SMiquel Raynal 
23085f8374d9SMiquel Raynal 	ret = gpmi_init_last(this);
23095f8374d9SMiquel Raynal 	if (ret)
23105f8374d9SMiquel Raynal 		return ret;
23115f8374d9SMiquel Raynal 
23125f8374d9SMiquel Raynal 	chip->options |= NAND_SKIP_BBTSCAN;
23135f8374d9SMiquel Raynal 
23145f8374d9SMiquel Raynal 	return 0;
23155f8374d9SMiquel Raynal }
23165f8374d9SMiquel Raynal 
get_next_transfer(struct gpmi_nand_data * this)2317ef347c0cSSascha Hauer static struct gpmi_transfer *get_next_transfer(struct gpmi_nand_data *this)
2318ef347c0cSSascha Hauer {
2319ef347c0cSSascha Hauer 	struct gpmi_transfer *transfer = &this->transfers[this->ntransfers];
2320ef347c0cSSascha Hauer 
2321ef347c0cSSascha Hauer 	this->ntransfers++;
2322ef347c0cSSascha Hauer 
2323ef347c0cSSascha Hauer 	if (this->ntransfers == GPMI_MAX_TRANSFERS)
2324ef347c0cSSascha Hauer 		return NULL;
2325ef347c0cSSascha Hauer 
2326ef347c0cSSascha Hauer 	return transfer;
2327ef347c0cSSascha Hauer }
2328ef347c0cSSascha Hauer 
gpmi_chain_command(struct gpmi_nand_data * this,u8 cmd,const u8 * addr,int naddr)2329ef347c0cSSascha Hauer static struct dma_async_tx_descriptor *gpmi_chain_command(
2330ef347c0cSSascha Hauer 	struct gpmi_nand_data *this, u8 cmd, const u8 *addr, int naddr)
2331ef347c0cSSascha Hauer {
2332ef347c0cSSascha Hauer 	struct dma_chan *channel = get_dma_chan(this);
2333ef347c0cSSascha Hauer 	struct dma_async_tx_descriptor *desc;
2334ef347c0cSSascha Hauer 	struct gpmi_transfer *transfer;
2335ef347c0cSSascha Hauer 	int chip = this->nand.cur_cs;
2336ef347c0cSSascha Hauer 	u32 pio[3];
2337ef347c0cSSascha Hauer 
2338ef347c0cSSascha Hauer 	/* [1] send out the PIO words */
2339ef347c0cSSascha Hauer 	pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WRITE)
2340ef347c0cSSascha Hauer 		| BM_GPMI_CTRL0_WORD_LENGTH
2341ef347c0cSSascha Hauer 		| BF_GPMI_CTRL0_CS(chip, this)
2342ef347c0cSSascha Hauer 		| BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
2343ef347c0cSSascha Hauer 		| BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_CLE)
2344ef347c0cSSascha Hauer 		| BM_GPMI_CTRL0_ADDRESS_INCREMENT
2345ef347c0cSSascha Hauer 		| BF_GPMI_CTRL0_XFER_COUNT(naddr + 1);
2346ef347c0cSSascha Hauer 	pio[1] = 0;
2347ef347c0cSSascha Hauer 	pio[2] = 0;
2348ef347c0cSSascha Hauer 	desc = mxs_dmaengine_prep_pio(channel, pio, ARRAY_SIZE(pio),
2349ef347c0cSSascha Hauer 				      DMA_TRANS_NONE, 0);
2350ef347c0cSSascha Hauer 	if (!desc)
2351ef347c0cSSascha Hauer 		return NULL;
2352ef347c0cSSascha Hauer 
2353ef347c0cSSascha Hauer 	transfer = get_next_transfer(this);
2354ef347c0cSSascha Hauer 	if (!transfer)
2355ef347c0cSSascha Hauer 		return NULL;
2356ef347c0cSSascha Hauer 
2357ef347c0cSSascha Hauer 	transfer->cmdbuf[0] = cmd;
2358ef347c0cSSascha Hauer 	if (naddr)
2359ef347c0cSSascha Hauer 		memcpy(&transfer->cmdbuf[1], addr, naddr);
2360ef347c0cSSascha Hauer 
2361ef347c0cSSascha Hauer 	sg_init_one(&transfer->sgl, transfer->cmdbuf, naddr + 1);
2362ef347c0cSSascha Hauer 	dma_map_sg(this->dev, &transfer->sgl, 1, DMA_TO_DEVICE);
2363ef347c0cSSascha Hauer 
2364ef347c0cSSascha Hauer 	transfer->direction = DMA_TO_DEVICE;
2365ef347c0cSSascha Hauer 
2366ef347c0cSSascha Hauer 	desc = dmaengine_prep_slave_sg(channel, &transfer->sgl, 1, DMA_MEM_TO_DEV,
2367ef347c0cSSascha Hauer 				       MXS_DMA_CTRL_WAIT4END);
2368ef347c0cSSascha Hauer 	return desc;
2369ef347c0cSSascha Hauer }
2370ef347c0cSSascha Hauer 
gpmi_chain_wait_ready(struct gpmi_nand_data * this)2371ef347c0cSSascha Hauer static struct dma_async_tx_descriptor *gpmi_chain_wait_ready(
2372ef347c0cSSascha Hauer 	struct gpmi_nand_data *this)
2373ef347c0cSSascha Hauer {
2374ef347c0cSSascha Hauer 	struct dma_chan *channel = get_dma_chan(this);
2375ef347c0cSSascha Hauer 	u32 pio[2];
2376ef347c0cSSascha Hauer 
2377ef347c0cSSascha Hauer 	pio[0] =  BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY)
2378ef347c0cSSascha Hauer 		| BM_GPMI_CTRL0_WORD_LENGTH
2379ef347c0cSSascha Hauer 		| BF_GPMI_CTRL0_CS(this->nand.cur_cs, this)
2380ef347c0cSSascha Hauer 		| BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
2381ef347c0cSSascha Hauer 		| BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA)
2382ef347c0cSSascha Hauer 		| BF_GPMI_CTRL0_XFER_COUNT(0);
2383ef347c0cSSascha Hauer 	pio[1] = 0;
2384ef347c0cSSascha Hauer 
2385ef347c0cSSascha Hauer 	return mxs_dmaengine_prep_pio(channel, pio, 2, DMA_TRANS_NONE,
2386ef347c0cSSascha Hauer 				MXS_DMA_CTRL_WAIT4END | MXS_DMA_CTRL_WAIT4RDY);
2387ef347c0cSSascha Hauer }
2388ef347c0cSSascha Hauer 
gpmi_chain_data_read(struct gpmi_nand_data * this,void * buf,int raw_len,bool * direct)2389ef347c0cSSascha Hauer static struct dma_async_tx_descriptor *gpmi_chain_data_read(
2390ef347c0cSSascha Hauer 	struct gpmi_nand_data *this, void *buf, int raw_len, bool *direct)
2391ef347c0cSSascha Hauer {
2392ef347c0cSSascha Hauer 	struct dma_async_tx_descriptor *desc;
2393ef347c0cSSascha Hauer 	struct dma_chan *channel = get_dma_chan(this);
2394ef347c0cSSascha Hauer 	struct gpmi_transfer *transfer;
2395ef347c0cSSascha Hauer 	u32 pio[6] = {};
2396ef347c0cSSascha Hauer 
2397ef347c0cSSascha Hauer 	transfer = get_next_transfer(this);
2398ef347c0cSSascha Hauer 	if (!transfer)
2399ef347c0cSSascha Hauer 		return NULL;
2400ef347c0cSSascha Hauer 
2401ef347c0cSSascha Hauer 	transfer->direction = DMA_FROM_DEVICE;
2402ef347c0cSSascha Hauer 
2403ef347c0cSSascha Hauer 	*direct = prepare_data_dma(this, buf, raw_len, &transfer->sgl,
2404ef347c0cSSascha Hauer 				   DMA_FROM_DEVICE);
2405ef347c0cSSascha Hauer 
2406ef347c0cSSascha Hauer 	pio[0] =  BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__READ)
2407ef347c0cSSascha Hauer 		| BM_GPMI_CTRL0_WORD_LENGTH
2408ef347c0cSSascha Hauer 		| BF_GPMI_CTRL0_CS(this->nand.cur_cs, this)
2409ef347c0cSSascha Hauer 		| BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
2410ef347c0cSSascha Hauer 		| BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA)
2411ef347c0cSSascha Hauer 		| BF_GPMI_CTRL0_XFER_COUNT(raw_len);
2412ef347c0cSSascha Hauer 
2413ef347c0cSSascha Hauer 	if (this->bch) {
2414ef347c0cSSascha Hauer 		pio[2] =  BM_GPMI_ECCCTRL_ENABLE_ECC
2415ef347c0cSSascha Hauer 			| BF_GPMI_ECCCTRL_ECC_CMD(BV_GPMI_ECCCTRL_ECC_CMD__BCH_DECODE)
2416ef347c0cSSascha Hauer 			| BF_GPMI_ECCCTRL_BUFFER_MASK(BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE
2417ef347c0cSSascha Hauer 				| BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY);
2418ef347c0cSSascha Hauer 		pio[3] = raw_len;
2419ef347c0cSSascha Hauer 		pio[4] = transfer->sgl.dma_address;
2420ef347c0cSSascha Hauer 		pio[5] = this->auxiliary_phys;
2421ef347c0cSSascha Hauer 	}
2422ef347c0cSSascha Hauer 
2423ef347c0cSSascha Hauer 	desc = mxs_dmaengine_prep_pio(channel, pio, ARRAY_SIZE(pio),
2424ef347c0cSSascha Hauer 				      DMA_TRANS_NONE, 0);
2425ef347c0cSSascha Hauer 	if (!desc)
2426ef347c0cSSascha Hauer 		return NULL;
2427ef347c0cSSascha Hauer 
2428ef347c0cSSascha Hauer 	if (!this->bch)
2429ef347c0cSSascha Hauer 		desc = dmaengine_prep_slave_sg(channel, &transfer->sgl, 1,
2430ef347c0cSSascha Hauer 					     DMA_DEV_TO_MEM,
2431ef347c0cSSascha Hauer 					     MXS_DMA_CTRL_WAIT4END);
2432ef347c0cSSascha Hauer 
2433ef347c0cSSascha Hauer 	return desc;
2434ef347c0cSSascha Hauer }
2435ef347c0cSSascha Hauer 
gpmi_chain_data_write(struct gpmi_nand_data * this,const void * buf,int raw_len)2436ef347c0cSSascha Hauer static struct dma_async_tx_descriptor *gpmi_chain_data_write(
2437ef347c0cSSascha Hauer 	struct gpmi_nand_data *this, const void *buf, int raw_len)
2438ef347c0cSSascha Hauer {
2439ef347c0cSSascha Hauer 	struct dma_chan *channel = get_dma_chan(this);
2440ef347c0cSSascha Hauer 	struct dma_async_tx_descriptor *desc;
2441ef347c0cSSascha Hauer 	struct gpmi_transfer *transfer;
2442ef347c0cSSascha Hauer 	u32 pio[6] = {};
2443ef347c0cSSascha Hauer 
2444ef347c0cSSascha Hauer 	transfer = get_next_transfer(this);
2445ef347c0cSSascha Hauer 	if (!transfer)
2446ef347c0cSSascha Hauer 		return NULL;
2447ef347c0cSSascha Hauer 
2448ef347c0cSSascha Hauer 	transfer->direction = DMA_TO_DEVICE;
2449ef347c0cSSascha Hauer 
2450ef347c0cSSascha Hauer 	prepare_data_dma(this, buf, raw_len, &transfer->sgl, DMA_TO_DEVICE);
2451ef347c0cSSascha Hauer 
2452ef347c0cSSascha Hauer 	pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WRITE)
2453ef347c0cSSascha Hauer 		| BM_GPMI_CTRL0_WORD_LENGTH
2454ef347c0cSSascha Hauer 		| BF_GPMI_CTRL0_CS(this->nand.cur_cs, this)
2455ef347c0cSSascha Hauer 		| BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
2456ef347c0cSSascha Hauer 		| BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA)
2457ef347c0cSSascha Hauer 		| BF_GPMI_CTRL0_XFER_COUNT(raw_len);
2458ef347c0cSSascha Hauer 
2459ef347c0cSSascha Hauer 	if (this->bch) {
2460ef347c0cSSascha Hauer 		pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC
2461ef347c0cSSascha Hauer 			| BF_GPMI_ECCCTRL_ECC_CMD(BV_GPMI_ECCCTRL_ECC_CMD__BCH_ENCODE)
2462ef347c0cSSascha Hauer 			| BF_GPMI_ECCCTRL_BUFFER_MASK(BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE |
2463ef347c0cSSascha Hauer 					BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY);
2464ef347c0cSSascha Hauer 		pio[3] = raw_len;
2465ef347c0cSSascha Hauer 		pio[4] = transfer->sgl.dma_address;
2466ef347c0cSSascha Hauer 		pio[5] = this->auxiliary_phys;
2467ef347c0cSSascha Hauer 	}
2468ef347c0cSSascha Hauer 
2469ef347c0cSSascha Hauer 	desc = mxs_dmaengine_prep_pio(channel, pio, ARRAY_SIZE(pio),
2470ef347c0cSSascha Hauer 				      DMA_TRANS_NONE,
2471ef347c0cSSascha Hauer 				      (this->bch ? MXS_DMA_CTRL_WAIT4END : 0));
2472ef347c0cSSascha Hauer 	if (!desc)
2473ef347c0cSSascha Hauer 		return NULL;
2474ef347c0cSSascha Hauer 
2475ef347c0cSSascha Hauer 	if (!this->bch)
2476ef347c0cSSascha Hauer 		desc = dmaengine_prep_slave_sg(channel, &transfer->sgl, 1,
2477ef347c0cSSascha Hauer 					       DMA_MEM_TO_DEV,
2478ef347c0cSSascha Hauer 					       MXS_DMA_CTRL_WAIT4END);
2479ef347c0cSSascha Hauer 
2480ef347c0cSSascha Hauer 	return desc;
2481ef347c0cSSascha Hauer }
2482ef347c0cSSascha Hauer 
gpmi_nfc_exec_op(struct nand_chip * chip,const struct nand_operation * op,bool check_only)2483ef347c0cSSascha Hauer static int gpmi_nfc_exec_op(struct nand_chip *chip,
2484ef347c0cSSascha Hauer 			     const struct nand_operation *op,
2485ef347c0cSSascha Hauer 			     bool check_only)
2486ef347c0cSSascha Hauer {
2487ef347c0cSSascha Hauer 	const struct nand_op_instr *instr;
2488ef347c0cSSascha Hauer 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
2489ef347c0cSSascha Hauer 	struct dma_async_tx_descriptor *desc = NULL;
2490ef347c0cSSascha Hauer 	int i, ret, buf_len = 0, nbufs = 0;
2491ef347c0cSSascha Hauer 	u8 cmd = 0;
2492ef347c0cSSascha Hauer 	void *buf_read = NULL;
2493ef347c0cSSascha Hauer 	const void *buf_write = NULL;
2494ef347c0cSSascha Hauer 	bool direct = false;
24957671edebSHan Xu 	struct completion *dma_completion, *bch_completion;
2496ef347c0cSSascha Hauer 	unsigned long to;
2497ef347c0cSSascha Hauer 
2498ce446b4bSBoris Brezillon 	if (check_only)
2499ce446b4bSBoris Brezillon 		return 0;
2500ce446b4bSBoris Brezillon 
2501ef347c0cSSascha Hauer 	this->ntransfers = 0;
2502ef347c0cSSascha Hauer 	for (i = 0; i < GPMI_MAX_TRANSFERS; i++)
2503ef347c0cSSascha Hauer 		this->transfers[i].direction = DMA_NONE;
2504ef347c0cSSascha Hauer 
250578e2d541SZhang Qilong 	ret = pm_runtime_resume_and_get(this->dev);
250678e2d541SZhang Qilong 	if (ret < 0)
2507ef347c0cSSascha Hauer 		return ret;
2508ef347c0cSSascha Hauer 
2509ef347c0cSSascha Hauer 	/*
2510ef347c0cSSascha Hauer 	 * This driver currently supports only one NAND chip. Plus, dies share
2511ef347c0cSSascha Hauer 	 * the same configuration. So once timings have been applied on the
2512ef347c0cSSascha Hauer 	 * controller side, they will not change anymore. When the time will
2513ef347c0cSSascha Hauer 	 * come, the check on must_apply_timings will have to be dropped.
2514ef347c0cSSascha Hauer 	 */
2515ef347c0cSSascha Hauer 	if (this->hw.must_apply_timings) {
2516ef347c0cSSascha Hauer 		this->hw.must_apply_timings = false;
2517f53d4c10SChristian Eggers 		ret = gpmi_nfc_apply_timings(this);
2518f53d4c10SChristian Eggers 		if (ret)
25199161f365SChristian Eggers 			goto out_pm;
2520ef347c0cSSascha Hauer 	}
2521ef347c0cSSascha Hauer 
2522ef347c0cSSascha Hauer 	dev_dbg(this->dev, "%s: %d instructions\n", __func__, op->ninstrs);
2523ef347c0cSSascha Hauer 
2524ef347c0cSSascha Hauer 	for (i = 0; i < op->ninstrs; i++) {
2525ef347c0cSSascha Hauer 		instr = &op->instrs[i];
2526ef347c0cSSascha Hauer 
2527ef347c0cSSascha Hauer 		nand_op_trace("  ", instr);
2528ef347c0cSSascha Hauer 
2529ef347c0cSSascha Hauer 		switch (instr->type) {
2530ef347c0cSSascha Hauer 		case NAND_OP_WAITRDY_INSTR:
2531ef347c0cSSascha Hauer 			desc = gpmi_chain_wait_ready(this);
2532ef347c0cSSascha Hauer 			break;
2533ef347c0cSSascha Hauer 		case NAND_OP_CMD_INSTR:
2534ef347c0cSSascha Hauer 			cmd = instr->ctx.cmd.opcode;
2535ef347c0cSSascha Hauer 
2536ef347c0cSSascha Hauer 			/*
2537ef347c0cSSascha Hauer 			 * When this command has an address cycle chain it
2538ef347c0cSSascha Hauer 			 * together with the address cycle
2539ef347c0cSSascha Hauer 			 */
2540ef347c0cSSascha Hauer 			if (i + 1 != op->ninstrs &&
2541ef347c0cSSascha Hauer 			    op->instrs[i + 1].type == NAND_OP_ADDR_INSTR)
2542ef347c0cSSascha Hauer 				continue;
2543ef347c0cSSascha Hauer 
2544ef347c0cSSascha Hauer 			desc = gpmi_chain_command(this, cmd, NULL, 0);
2545ef347c0cSSascha Hauer 
2546ef347c0cSSascha Hauer 			break;
2547ef347c0cSSascha Hauer 		case NAND_OP_ADDR_INSTR:
2548ef347c0cSSascha Hauer 			desc = gpmi_chain_command(this, cmd, instr->ctx.addr.addrs,
2549ef347c0cSSascha Hauer 						  instr->ctx.addr.naddrs);
2550ef347c0cSSascha Hauer 			break;
2551ef347c0cSSascha Hauer 		case NAND_OP_DATA_OUT_INSTR:
2552ef347c0cSSascha Hauer 			buf_write = instr->ctx.data.buf.out;
2553ef347c0cSSascha Hauer 			buf_len = instr->ctx.data.len;
2554ef347c0cSSascha Hauer 			nbufs++;
2555ef347c0cSSascha Hauer 
2556ef347c0cSSascha Hauer 			desc = gpmi_chain_data_write(this, buf_write, buf_len);
2557ef347c0cSSascha Hauer 
2558ef347c0cSSascha Hauer 			break;
2559ef347c0cSSascha Hauer 		case NAND_OP_DATA_IN_INSTR:
2560ef347c0cSSascha Hauer 			if (!instr->ctx.data.len)
2561ef347c0cSSascha Hauer 				break;
2562ef347c0cSSascha Hauer 			buf_read = instr->ctx.data.buf.in;
2563ef347c0cSSascha Hauer 			buf_len = instr->ctx.data.len;
2564ef347c0cSSascha Hauer 			nbufs++;
2565ef347c0cSSascha Hauer 
2566ef347c0cSSascha Hauer 			desc = gpmi_chain_data_read(this, buf_read, buf_len,
2567ef347c0cSSascha Hauer 						   &direct);
2568ef347c0cSSascha Hauer 			break;
2569ef347c0cSSascha Hauer 		}
2570ef347c0cSSascha Hauer 
2571ef347c0cSSascha Hauer 		if (!desc) {
2572ef347c0cSSascha Hauer 			ret = -ENXIO;
2573ef347c0cSSascha Hauer 			goto unmap;
2574ef347c0cSSascha Hauer 		}
2575ef347c0cSSascha Hauer 	}
2576ef347c0cSSascha Hauer 
2577ef347c0cSSascha Hauer 	dev_dbg(this->dev, "%s setup done\n", __func__);
2578ef347c0cSSascha Hauer 
2579ef347c0cSSascha Hauer 	if (nbufs > 1) {
2580ef347c0cSSascha Hauer 		dev_err(this->dev, "Multiple data instructions not supported\n");
2581ef347c0cSSascha Hauer 		ret = -EINVAL;
2582ef347c0cSSascha Hauer 		goto unmap;
2583ef347c0cSSascha Hauer 	}
2584ef347c0cSSascha Hauer 
2585ef347c0cSSascha Hauer 	if (this->bch) {
2586ef347c0cSSascha Hauer 		writel(this->bch_flashlayout0,
2587ef347c0cSSascha Hauer 		       this->resources.bch_regs + HW_BCH_FLASH0LAYOUT0);
2588ef347c0cSSascha Hauer 		writel(this->bch_flashlayout1,
2589ef347c0cSSascha Hauer 		       this->resources.bch_regs + HW_BCH_FLASH0LAYOUT1);
2590ef347c0cSSascha Hauer 	}
2591ef347c0cSSascha Hauer 
25927671edebSHan Xu 	desc->callback = dma_irq_callback;
25937671edebSHan Xu 	desc->callback_param = this;
25947671edebSHan Xu 	dma_completion = &this->dma_done;
25957671edebSHan Xu 	bch_completion = NULL;
25967671edebSHan Xu 
25977671edebSHan Xu 	init_completion(dma_completion);
25987671edebSHan Xu 
2599ef347c0cSSascha Hauer 	if (this->bch && buf_read) {
2600ef347c0cSSascha Hauer 		writel(BM_BCH_CTRL_COMPLETE_IRQ_EN,
2601ef347c0cSSascha Hauer 		       this->resources.bch_regs + HW_BCH_CTRL_SET);
26027671edebSHan Xu 		bch_completion = &this->bch_done;
26037671edebSHan Xu 		init_completion(bch_completion);
2604ef347c0cSSascha Hauer 	}
2605ef347c0cSSascha Hauer 
2606ef347c0cSSascha Hauer 	dmaengine_submit(desc);
2607ef347c0cSSascha Hauer 	dma_async_issue_pending(get_dma_chan(this));
2608ef347c0cSSascha Hauer 
26097671edebSHan Xu 	to = wait_for_completion_timeout(dma_completion, msecs_to_jiffies(1000));
2610ef347c0cSSascha Hauer 	if (!to) {
2611ef347c0cSSascha Hauer 		dev_err(this->dev, "DMA timeout, last DMA\n");
2612ef347c0cSSascha Hauer 		gpmi_dump_info(this);
2613ef347c0cSSascha Hauer 		ret = -ETIMEDOUT;
2614ef347c0cSSascha Hauer 		goto unmap;
2615ef347c0cSSascha Hauer 	}
2616ef347c0cSSascha Hauer 
26177671edebSHan Xu 	if (this->bch && buf_read) {
26187671edebSHan Xu 		to = wait_for_completion_timeout(bch_completion, msecs_to_jiffies(1000));
26197671edebSHan Xu 		if (!to) {
26207671edebSHan Xu 			dev_err(this->dev, "BCH timeout, last DMA\n");
26217671edebSHan Xu 			gpmi_dump_info(this);
26227671edebSHan Xu 			ret = -ETIMEDOUT;
26237671edebSHan Xu 			goto unmap;
26247671edebSHan Xu 		}
26257671edebSHan Xu 	}
26267671edebSHan Xu 
2627ef347c0cSSascha Hauer 	writel(BM_BCH_CTRL_COMPLETE_IRQ_EN,
2628ef347c0cSSascha Hauer 	       this->resources.bch_regs + HW_BCH_CTRL_CLR);
2629ef347c0cSSascha Hauer 	gpmi_clear_bch(this);
2630ef347c0cSSascha Hauer 
2631ef347c0cSSascha Hauer 	ret = 0;
2632ef347c0cSSascha Hauer 
2633ef347c0cSSascha Hauer unmap:
2634ef347c0cSSascha Hauer 	for (i = 0; i < this->ntransfers; i++) {
2635ef347c0cSSascha Hauer 		struct gpmi_transfer *transfer = &this->transfers[i];
2636ef347c0cSSascha Hauer 
2637ef347c0cSSascha Hauer 		if (transfer->direction != DMA_NONE)
2638ef347c0cSSascha Hauer 			dma_unmap_sg(this->dev, &transfer->sgl, 1,
2639ef347c0cSSascha Hauer 				     transfer->direction);
2640ef347c0cSSascha Hauer 	}
2641ef347c0cSSascha Hauer 
2642ef347c0cSSascha Hauer 	if (!ret && buf_read && !direct)
2643ef347c0cSSascha Hauer 		memcpy(buf_read, this->data_buffer_dma,
2644ef347c0cSSascha Hauer 		       gpmi_raw_len_to_len(this, buf_len));
2645ef347c0cSSascha Hauer 
2646ef347c0cSSascha Hauer 	this->bch = false;
2647ef347c0cSSascha Hauer 
26489161f365SChristian Eggers out_pm:
2649ef347c0cSSascha Hauer 	pm_runtime_mark_last_busy(this->dev);
2650ef347c0cSSascha Hauer 	pm_runtime_put_autosuspend(this->dev);
2651ef347c0cSSascha Hauer 
2652ef347c0cSSascha Hauer 	return ret;
2653ef347c0cSSascha Hauer }
2654ef347c0cSSascha Hauer 
26555f8374d9SMiquel Raynal static const struct nand_controller_ops gpmi_nand_controller_ops = {
26565f8374d9SMiquel Raynal 	.attach_chip = gpmi_nand_attach_chip,
26574c46667bSMiquel Raynal 	.setup_interface = gpmi_setup_interface,
2658ef347c0cSSascha Hauer 	.exec_op = gpmi_nfc_exec_op,
26595f8374d9SMiquel Raynal };
26605f8374d9SMiquel Raynal 
gpmi_nand_init(struct gpmi_nand_data * this)266193db446aSBoris Brezillon static int gpmi_nand_init(struct gpmi_nand_data *this)
266293db446aSBoris Brezillon {
266393db446aSBoris Brezillon 	struct nand_chip *chip = &this->nand;
266493db446aSBoris Brezillon 	struct mtd_info  *mtd = nand_to_mtd(chip);
266593db446aSBoris Brezillon 	int ret;
266693db446aSBoris Brezillon 
266793db446aSBoris Brezillon 	/* init the MTD data structures */
266893db446aSBoris Brezillon 	mtd->name		= "gpmi-nand";
266993db446aSBoris Brezillon 	mtd->dev.parent		= this->dev;
267093db446aSBoris Brezillon 
267193db446aSBoris Brezillon 	/* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
267293db446aSBoris Brezillon 	nand_set_controller_data(chip, this);
267393db446aSBoris Brezillon 	nand_set_flash_node(chip, this->pdev->dev.of_node);
2674cdc784c7SBoris Brezillon 	chip->legacy.block_markbad = gpmi_block_markbad;
2675ef347c0cSSascha Hauer 	chip->badblock_pattern	= &gpmi_bbt_descr;
267693db446aSBoris Brezillon 	chip->options		|= NAND_NO_SUBPAGE_WRITE;
267793db446aSBoris Brezillon 
267893db446aSBoris Brezillon 	/* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
267993db446aSBoris Brezillon 	this->swap_block_mark = !GPMI_IS_MX23(this);
268093db446aSBoris Brezillon 
268193db446aSBoris Brezillon 	/*
268293db446aSBoris Brezillon 	 * Allocate a temporary DMA buffer for reading ID in the
268393db446aSBoris Brezillon 	 * nand_scan_ident().
268493db446aSBoris Brezillon 	 */
268593db446aSBoris Brezillon 	this->bch_geometry.payload_size = 1024;
268693db446aSBoris Brezillon 	this->bch_geometry.auxiliary_size = 128;
268793db446aSBoris Brezillon 	ret = gpmi_alloc_dma_buffer(this);
268893db446aSBoris Brezillon 	if (ret)
2689076de75dSLv Yunlong 		return ret;
269093db446aSBoris Brezillon 
2691ef347c0cSSascha Hauer 	nand_controller_init(&this->base);
2692ef347c0cSSascha Hauer 	this->base.ops = &gpmi_nand_controller_ops;
2693ef347c0cSSascha Hauer 	chip->controller = &this->base;
2694ef347c0cSSascha Hauer 
269500ad378fSBoris Brezillon 	ret = nand_scan(chip, GPMI_IS_MX6(this) ? 2 : 1);
269693db446aSBoris Brezillon 	if (ret)
269793db446aSBoris Brezillon 		goto err_out;
269893db446aSBoris Brezillon 
269993db446aSBoris Brezillon 	ret = nand_boot_init(this);
270093db446aSBoris Brezillon 	if (ret)
270193db446aSBoris Brezillon 		goto err_nand_cleanup;
2702e80eba75SBoris Brezillon 	ret = nand_create_bbt(chip);
270393db446aSBoris Brezillon 	if (ret)
270493db446aSBoris Brezillon 		goto err_nand_cleanup;
270593db446aSBoris Brezillon 
270693db446aSBoris Brezillon 	ret = mtd_device_register(mtd, NULL, 0);
270793db446aSBoris Brezillon 	if (ret)
270893db446aSBoris Brezillon 		goto err_nand_cleanup;
270993db446aSBoris Brezillon 	return 0;
271093db446aSBoris Brezillon 
271193db446aSBoris Brezillon err_nand_cleanup:
271293db446aSBoris Brezillon 	nand_cleanup(chip);
271393db446aSBoris Brezillon err_out:
271493db446aSBoris Brezillon 	gpmi_free_dma_buffer(this);
271593db446aSBoris Brezillon 	return ret;
271693db446aSBoris Brezillon }
271793db446aSBoris Brezillon 
271893db446aSBoris Brezillon static const struct of_device_id gpmi_nand_id_table[] = {
2719ea7110b8SFabio Estevam 	{ .compatible = "fsl,imx23-gpmi-nand", .data = &gpmi_devdata_imx23, },
2720ea7110b8SFabio Estevam 	{ .compatible = "fsl,imx28-gpmi-nand", .data = &gpmi_devdata_imx28, },
2721ea7110b8SFabio Estevam 	{ .compatible = "fsl,imx6q-gpmi-nand", .data = &gpmi_devdata_imx6q, },
2722ea7110b8SFabio Estevam 	{ .compatible = "fsl,imx6sx-gpmi-nand", .data = &gpmi_devdata_imx6sx, },
2723ea7110b8SFabio Estevam 	{ .compatible = "fsl,imx7d-gpmi-nand", .data = &gpmi_devdata_imx7d,},
2724ea7110b8SFabio Estevam 	{}
272593db446aSBoris Brezillon };
272693db446aSBoris Brezillon MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
272793db446aSBoris Brezillon 
gpmi_nand_probe(struct platform_device * pdev)272893db446aSBoris Brezillon static int gpmi_nand_probe(struct platform_device *pdev)
272993db446aSBoris Brezillon {
273093db446aSBoris Brezillon 	struct gpmi_nand_data *this;
273193db446aSBoris Brezillon 	int ret;
273293db446aSBoris Brezillon 
273393db446aSBoris Brezillon 	this = devm_kzalloc(&pdev->dev, sizeof(*this), GFP_KERNEL);
273493db446aSBoris Brezillon 	if (!this)
273593db446aSBoris Brezillon 		return -ENOMEM;
273693db446aSBoris Brezillon 
2737d1c3ede6SFabio Estevam 	this->devdata = of_device_get_match_data(&pdev->dev);
273893db446aSBoris Brezillon 	platform_set_drvdata(pdev, this);
273993db446aSBoris Brezillon 	this->pdev  = pdev;
274093db446aSBoris Brezillon 	this->dev   = &pdev->dev;
274193db446aSBoris Brezillon 
274293db446aSBoris Brezillon 	ret = acquire_resources(this);
274393db446aSBoris Brezillon 	if (ret)
274493db446aSBoris Brezillon 		goto exit_acquire_resources;
274593db446aSBoris Brezillon 
274604141468SSascha Hauer 	ret = __gpmi_enable_clk(this, true);
274704141468SSascha Hauer 	if (ret)
27488e935b92SDinghao Liu 		goto exit_acquire_resources;
274904141468SSascha Hauer 
275004141468SSascha Hauer 	pm_runtime_set_autosuspend_delay(&pdev->dev, 500);
275104141468SSascha Hauer 	pm_runtime_use_autosuspend(&pdev->dev);
275204141468SSascha Hauer 	pm_runtime_set_active(&pdev->dev);
275304141468SSascha Hauer 	pm_runtime_enable(&pdev->dev);
275404141468SSascha Hauer 	pm_runtime_get_sync(&pdev->dev);
275504141468SSascha Hauer 
2756b1206122SMiquel Raynal 	ret = gpmi_init(this);
275793db446aSBoris Brezillon 	if (ret)
275893db446aSBoris Brezillon 		goto exit_nfc_init;
275993db446aSBoris Brezillon 
276093db446aSBoris Brezillon 	ret = gpmi_nand_init(this);
276193db446aSBoris Brezillon 	if (ret)
276293db446aSBoris Brezillon 		goto exit_nfc_init;
276393db446aSBoris Brezillon 
276404141468SSascha Hauer 	pm_runtime_mark_last_busy(&pdev->dev);
276504141468SSascha Hauer 	pm_runtime_put_autosuspend(&pdev->dev);
276604141468SSascha Hauer 
276793db446aSBoris Brezillon 	dev_info(this->dev, "driver registered.\n");
276893db446aSBoris Brezillon 
276993db446aSBoris Brezillon 	return 0;
277093db446aSBoris Brezillon 
277193db446aSBoris Brezillon exit_nfc_init:
277204141468SSascha Hauer 	pm_runtime_put(&pdev->dev);
277304141468SSascha Hauer 	pm_runtime_disable(&pdev->dev);
277493db446aSBoris Brezillon 	release_resources(this);
277593db446aSBoris Brezillon exit_acquire_resources:
277693db446aSBoris Brezillon 
277793db446aSBoris Brezillon 	return ret;
277893db446aSBoris Brezillon }
277993db446aSBoris Brezillon 
gpmi_nand_remove(struct platform_device * pdev)2780ec185b18SUwe Kleine-König static void gpmi_nand_remove(struct platform_device *pdev)
278193db446aSBoris Brezillon {
278293db446aSBoris Brezillon 	struct gpmi_nand_data *this = platform_get_drvdata(pdev);
2783194f6c48SMiquel Raynal 	struct nand_chip *chip = &this->nand;
2784194f6c48SMiquel Raynal 	int ret;
278593db446aSBoris Brezillon 
278604141468SSascha Hauer 	pm_runtime_put_sync(&pdev->dev);
278704141468SSascha Hauer 	pm_runtime_disable(&pdev->dev);
278804141468SSascha Hauer 
2789194f6c48SMiquel Raynal 	ret = mtd_device_unregister(nand_to_mtd(chip));
2790194f6c48SMiquel Raynal 	WARN_ON(ret);
2791194f6c48SMiquel Raynal 	nand_cleanup(chip);
279293db446aSBoris Brezillon 	gpmi_free_dma_buffer(this);
279393db446aSBoris Brezillon 	release_resources(this);
279493db446aSBoris Brezillon }
279593db446aSBoris Brezillon 
279693db446aSBoris Brezillon #ifdef CONFIG_PM_SLEEP
gpmi_pm_suspend(struct device * dev)279793db446aSBoris Brezillon static int gpmi_pm_suspend(struct device *dev)
279893db446aSBoris Brezillon {
279993db446aSBoris Brezillon 	struct gpmi_nand_data *this = dev_get_drvdata(dev);
280093db446aSBoris Brezillon 
280193db446aSBoris Brezillon 	release_dma_channels(this);
280293db446aSBoris Brezillon 	return 0;
280393db446aSBoris Brezillon }
280493db446aSBoris Brezillon 
gpmi_pm_resume(struct device * dev)280593db446aSBoris Brezillon static int gpmi_pm_resume(struct device *dev)
280693db446aSBoris Brezillon {
280793db446aSBoris Brezillon 	struct gpmi_nand_data *this = dev_get_drvdata(dev);
280893db446aSBoris Brezillon 	int ret;
280993db446aSBoris Brezillon 
281093db446aSBoris Brezillon 	ret = acquire_dma_channels(this);
281193db446aSBoris Brezillon 	if (ret < 0)
281293db446aSBoris Brezillon 		return ret;
281393db446aSBoris Brezillon 
281493db446aSBoris Brezillon 	/* re-init the GPMI registers */
281593db446aSBoris Brezillon 	ret = gpmi_init(this);
281693db446aSBoris Brezillon 	if (ret) {
281793db446aSBoris Brezillon 		dev_err(this->dev, "Error setting GPMI : %d\n", ret);
281893db446aSBoris Brezillon 		return ret;
281993db446aSBoris Brezillon 	}
282093db446aSBoris Brezillon 
2821d7048666SEsben Haabendal 	/* Set flag to get timing setup restored for next exec_op */
2822d7048666SEsben Haabendal 	if (this->hw.clk_rate)
2823d7048666SEsben Haabendal 		this->hw.must_apply_timings = true;
2824d7048666SEsben Haabendal 
282593db446aSBoris Brezillon 	/* re-init the BCH registers */
282693db446aSBoris Brezillon 	ret = bch_set_geometry(this);
282793db446aSBoris Brezillon 	if (ret) {
282893db446aSBoris Brezillon 		dev_err(this->dev, "Error setting BCH : %d\n", ret);
282993db446aSBoris Brezillon 		return ret;
283093db446aSBoris Brezillon 	}
283193db446aSBoris Brezillon 
283293db446aSBoris Brezillon 	return 0;
283393db446aSBoris Brezillon }
283493db446aSBoris Brezillon #endif /* CONFIG_PM_SLEEP */
283593db446aSBoris Brezillon 
gpmi_runtime_suspend(struct device * dev)283604141468SSascha Hauer static int __maybe_unused gpmi_runtime_suspend(struct device *dev)
283704141468SSascha Hauer {
283804141468SSascha Hauer 	struct gpmi_nand_data *this = dev_get_drvdata(dev);
283904141468SSascha Hauer 
284004141468SSascha Hauer 	return __gpmi_enable_clk(this, false);
284104141468SSascha Hauer }
284204141468SSascha Hauer 
gpmi_runtime_resume(struct device * dev)284304141468SSascha Hauer static int __maybe_unused gpmi_runtime_resume(struct device *dev)
284404141468SSascha Hauer {
284504141468SSascha Hauer 	struct gpmi_nand_data *this = dev_get_drvdata(dev);
284604141468SSascha Hauer 
284704141468SSascha Hauer 	return __gpmi_enable_clk(this, true);
284804141468SSascha Hauer }
284904141468SSascha Hauer 
285093db446aSBoris Brezillon static const struct dev_pm_ops gpmi_pm_ops = {
285193db446aSBoris Brezillon 	SET_SYSTEM_SLEEP_PM_OPS(gpmi_pm_suspend, gpmi_pm_resume)
285204141468SSascha Hauer 	SET_RUNTIME_PM_OPS(gpmi_runtime_suspend, gpmi_runtime_resume, NULL)
285393db446aSBoris Brezillon };
285493db446aSBoris Brezillon 
285593db446aSBoris Brezillon static struct platform_driver gpmi_nand_driver = {
285693db446aSBoris Brezillon 	.driver = {
285793db446aSBoris Brezillon 		.name = "gpmi-nand",
285893db446aSBoris Brezillon 		.pm = &gpmi_pm_ops,
285993db446aSBoris Brezillon 		.of_match_table = gpmi_nand_id_table,
286093db446aSBoris Brezillon 	},
286193db446aSBoris Brezillon 	.probe   = gpmi_nand_probe,
2862ec185b18SUwe Kleine-König 	.remove_new = gpmi_nand_remove,
286393db446aSBoris Brezillon };
286493db446aSBoris Brezillon module_platform_driver(gpmi_nand_driver);
286593db446aSBoris Brezillon 
286693db446aSBoris Brezillon MODULE_AUTHOR("Freescale Semiconductor, Inc.");
286793db446aSBoris Brezillon MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
286893db446aSBoris Brezillon MODULE_LICENSE("GPL");
2869