18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Marvell NAND flash controller driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2017 Marvell
68c2ecf20Sopenharmony_ci * Author: Miquel RAYNAL <miquel.raynal@free-electrons.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * This NAND controller driver handles two versions of the hardware,
108c2ecf20Sopenharmony_ci * one is called NFCv1 and is available on PXA SoCs and the other is
118c2ecf20Sopenharmony_ci * called NFCv2 and is available on Armada SoCs.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * The main visible difference is that NFCv1 only has Hamming ECC
148c2ecf20Sopenharmony_ci * capabilities, while NFCv2 also embeds a BCH ECC engine. Also, DMA
158c2ecf20Sopenharmony_ci * is not used with NFCv2.
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * The ECC layouts are depicted in details in Marvell AN-379, but here
188c2ecf20Sopenharmony_ci * is a brief description.
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci * When using Hamming, the data is split in 512B chunks (either 1, 2
218c2ecf20Sopenharmony_ci * or 4) and each chunk will have its own ECC "digest" of 6B at the
228c2ecf20Sopenharmony_ci * beginning of the OOB area and eventually the remaining free OOB
238c2ecf20Sopenharmony_ci * bytes (also called "spare" bytes in the driver). This engine
248c2ecf20Sopenharmony_ci * corrects up to 1 bit per chunk and detects reliably an error if
258c2ecf20Sopenharmony_ci * there are at most 2 bitflips. Here is the page layout used by the
268c2ecf20Sopenharmony_ci * controller when Hamming is chosen:
278c2ecf20Sopenharmony_ci *
288c2ecf20Sopenharmony_ci * +-------------------------------------------------------------+
298c2ecf20Sopenharmony_ci * | Data 1 | ... | Data N | ECC 1 | ... | ECCN | Free OOB bytes |
308c2ecf20Sopenharmony_ci * +-------------------------------------------------------------+
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci * When using the BCH engine, there are N identical (data + free OOB +
338c2ecf20Sopenharmony_ci * ECC) sections and potentially an extra one to deal with
348c2ecf20Sopenharmony_ci * configurations where the chosen (data + free OOB + ECC) sizes do
358c2ecf20Sopenharmony_ci * not align with the page (data + OOB) size. ECC bytes are always
368c2ecf20Sopenharmony_ci * 30B per ECC chunk. Here is the page layout used by the controller
378c2ecf20Sopenharmony_ci * when BCH is chosen:
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci * +-----------------------------------------
408c2ecf20Sopenharmony_ci * | Data 1 | Free OOB bytes 1 | ECC 1 | ...
418c2ecf20Sopenharmony_ci * +-----------------------------------------
428c2ecf20Sopenharmony_ci *
438c2ecf20Sopenharmony_ci *      -------------------------------------------
448c2ecf20Sopenharmony_ci *       ... | Data N | Free OOB bytes N | ECC N |
458c2ecf20Sopenharmony_ci *      -------------------------------------------
468c2ecf20Sopenharmony_ci *
478c2ecf20Sopenharmony_ci *           --------------------------------------------+
488c2ecf20Sopenharmony_ci *            Last Data | Last Free OOB bytes | Last ECC |
498c2ecf20Sopenharmony_ci *           --------------------------------------------+
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci * In both cases, the layout seen by the user is always: all data
528c2ecf20Sopenharmony_ci * first, then all free OOB bytes and finally all ECC bytes. With BCH,
538c2ecf20Sopenharmony_ci * ECC bytes are 30B long and are padded with 0xFF to align on 32
548c2ecf20Sopenharmony_ci * bytes.
558c2ecf20Sopenharmony_ci *
568c2ecf20Sopenharmony_ci * The controller has certain limitations that are handled by the
578c2ecf20Sopenharmony_ci * driver:
588c2ecf20Sopenharmony_ci *   - It can only read 2k at a time. To overcome this limitation, the
598c2ecf20Sopenharmony_ci *     driver issues data cycles on the bus, without issuing new
608c2ecf20Sopenharmony_ci *     CMD + ADDR cycles. The Marvell term is "naked" operations.
618c2ecf20Sopenharmony_ci *   - The ECC strength in BCH mode cannot be tuned. It is fixed 16
628c2ecf20Sopenharmony_ci *     bits. What can be tuned is the ECC block size as long as it
638c2ecf20Sopenharmony_ci *     stays between 512B and 2kiB. It's usually chosen based on the
648c2ecf20Sopenharmony_ci *     chip ECC requirements. For instance, using 2kiB ECC chunks
658c2ecf20Sopenharmony_ci *     provides 4b/512B correctability.
668c2ecf20Sopenharmony_ci *   - The controller will always treat data bytes, free OOB bytes
678c2ecf20Sopenharmony_ci *     and ECC bytes in that order, no matter what the real layout is
688c2ecf20Sopenharmony_ci *     (which is usually all data then all OOB bytes). The
698c2ecf20Sopenharmony_ci *     marvell_nfc_layouts array below contains the currently
708c2ecf20Sopenharmony_ci *     supported layouts.
718c2ecf20Sopenharmony_ci *   - Because of these weird layouts, the Bad Block Markers can be
728c2ecf20Sopenharmony_ci *     located in data section. In this case, the NAND_BBT_NO_OOB_BBM
738c2ecf20Sopenharmony_ci *     option must be set to prevent scanning/writing bad block
748c2ecf20Sopenharmony_ci *     markers.
758c2ecf20Sopenharmony_ci */
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#include <linux/module.h>
788c2ecf20Sopenharmony_ci#include <linux/clk.h>
798c2ecf20Sopenharmony_ci#include <linux/mtd/rawnand.h>
808c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
818c2ecf20Sopenharmony_ci#include <linux/iopoll.h>
828c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
838c2ecf20Sopenharmony_ci#include <linux/slab.h>
848c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
858c2ecf20Sopenharmony_ci#include <linux/regmap.h>
868c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci#include <linux/dmaengine.h>
898c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
908c2ecf20Sopenharmony_ci#include <linux/dma/pxa-dma.h>
918c2ecf20Sopenharmony_ci#include <linux/platform_data/mtd-nand-pxa3xx.h>
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci/* Data FIFO granularity, FIFO reads/writes must be a multiple of this length */
948c2ecf20Sopenharmony_ci#define FIFO_DEPTH		8
958c2ecf20Sopenharmony_ci#define FIFO_REP(x)		(x / sizeof(u32))
968c2ecf20Sopenharmony_ci#define BCH_SEQ_READS		(32 / FIFO_DEPTH)
978c2ecf20Sopenharmony_ci/* NFC does not support transfers of larger chunks at a time */
988c2ecf20Sopenharmony_ci#define MAX_CHUNK_SIZE		2112
998c2ecf20Sopenharmony_ci/* NFCv1 cannot read more that 7 bytes of ID */
1008c2ecf20Sopenharmony_ci#define NFCV1_READID_LEN	7
1018c2ecf20Sopenharmony_ci/* Polling is done at a pace of POLL_PERIOD us until POLL_TIMEOUT is reached */
1028c2ecf20Sopenharmony_ci#define POLL_PERIOD		0
1038c2ecf20Sopenharmony_ci#define POLL_TIMEOUT		100000
1048c2ecf20Sopenharmony_ci/* Interrupt maximum wait period in ms */
1058c2ecf20Sopenharmony_ci#define IRQ_TIMEOUT		1000
1068c2ecf20Sopenharmony_ci/* Latency in clock cycles between SoC pins and NFC logic */
1078c2ecf20Sopenharmony_ci#define MIN_RD_DEL_CNT		3
1088c2ecf20Sopenharmony_ci/* Maximum number of contiguous address cycles */
1098c2ecf20Sopenharmony_ci#define MAX_ADDRESS_CYC_NFCV1	5
1108c2ecf20Sopenharmony_ci#define MAX_ADDRESS_CYC_NFCV2	7
1118c2ecf20Sopenharmony_ci/* System control registers/bits to enable the NAND controller on some SoCs */
1128c2ecf20Sopenharmony_ci#define GENCONF_SOC_DEVICE_MUX	0x208
1138c2ecf20Sopenharmony_ci#define GENCONF_SOC_DEVICE_MUX_NFC_EN BIT(0)
1148c2ecf20Sopenharmony_ci#define GENCONF_SOC_DEVICE_MUX_ECC_CLK_RST BIT(20)
1158c2ecf20Sopenharmony_ci#define GENCONF_SOC_DEVICE_MUX_ECC_CORE_RST BIT(21)
1168c2ecf20Sopenharmony_ci#define GENCONF_SOC_DEVICE_MUX_NFC_INT_EN BIT(25)
1178c2ecf20Sopenharmony_ci#define GENCONF_CLK_GATING_CTRL	0x220
1188c2ecf20Sopenharmony_ci#define GENCONF_CLK_GATING_CTRL_ND_GATE BIT(2)
1198c2ecf20Sopenharmony_ci#define GENCONF_ND_CLK_CTRL	0x700
1208c2ecf20Sopenharmony_ci#define GENCONF_ND_CLK_CTRL_EN	BIT(0)
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci/* NAND controller data flash control register */
1238c2ecf20Sopenharmony_ci#define NDCR			0x00
1248c2ecf20Sopenharmony_ci#define NDCR_ALL_INT		GENMASK(11, 0)
1258c2ecf20Sopenharmony_ci#define NDCR_CS1_CMDDM		BIT(7)
1268c2ecf20Sopenharmony_ci#define NDCR_CS0_CMDDM		BIT(8)
1278c2ecf20Sopenharmony_ci#define NDCR_RDYM		BIT(11)
1288c2ecf20Sopenharmony_ci#define NDCR_ND_ARB_EN		BIT(12)
1298c2ecf20Sopenharmony_ci#define NDCR_RA_START		BIT(15)
1308c2ecf20Sopenharmony_ci#define NDCR_RD_ID_CNT(x)	(min_t(unsigned int, x, 0x7) << 16)
1318c2ecf20Sopenharmony_ci#define NDCR_PAGE_SZ(x)		(x >= 2048 ? BIT(24) : 0)
1328c2ecf20Sopenharmony_ci#define NDCR_DWIDTH_M		BIT(26)
1338c2ecf20Sopenharmony_ci#define NDCR_DWIDTH_C		BIT(27)
1348c2ecf20Sopenharmony_ci#define NDCR_ND_RUN		BIT(28)
1358c2ecf20Sopenharmony_ci#define NDCR_DMA_EN		BIT(29)
1368c2ecf20Sopenharmony_ci#define NDCR_ECC_EN		BIT(30)
1378c2ecf20Sopenharmony_ci#define NDCR_SPARE_EN		BIT(31)
1388c2ecf20Sopenharmony_ci#define NDCR_GENERIC_FIELDS_MASK (~(NDCR_RA_START | NDCR_PAGE_SZ(2048) | \
1398c2ecf20Sopenharmony_ci				    NDCR_DWIDTH_M | NDCR_DWIDTH_C))
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci/* NAND interface timing parameter 0 register */
1428c2ecf20Sopenharmony_ci#define NDTR0			0x04
1438c2ecf20Sopenharmony_ci#define NDTR0_TRP(x)		((min_t(unsigned int, x, 0xF) & 0x7) << 0)
1448c2ecf20Sopenharmony_ci#define NDTR0_TRH(x)		(min_t(unsigned int, x, 0x7) << 3)
1458c2ecf20Sopenharmony_ci#define NDTR0_ETRP(x)		((min_t(unsigned int, x, 0xF) & 0x8) << 3)
1468c2ecf20Sopenharmony_ci#define NDTR0_SEL_NRE_EDGE	BIT(7)
1478c2ecf20Sopenharmony_ci#define NDTR0_TWP(x)		(min_t(unsigned int, x, 0x7) << 8)
1488c2ecf20Sopenharmony_ci#define NDTR0_TWH(x)		(min_t(unsigned int, x, 0x7) << 11)
1498c2ecf20Sopenharmony_ci#define NDTR0_TCS(x)		(min_t(unsigned int, x, 0x7) << 16)
1508c2ecf20Sopenharmony_ci#define NDTR0_TCH(x)		(min_t(unsigned int, x, 0x7) << 19)
1518c2ecf20Sopenharmony_ci#define NDTR0_RD_CNT_DEL(x)	(min_t(unsigned int, x, 0xF) << 22)
1528c2ecf20Sopenharmony_ci#define NDTR0_SELCNTR		BIT(26)
1538c2ecf20Sopenharmony_ci#define NDTR0_TADL(x)		(min_t(unsigned int, x, 0x1F) << 27)
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci/* NAND interface timing parameter 1 register */
1568c2ecf20Sopenharmony_ci#define NDTR1			0x0C
1578c2ecf20Sopenharmony_ci#define NDTR1_TAR(x)		(min_t(unsigned int, x, 0xF) << 0)
1588c2ecf20Sopenharmony_ci#define NDTR1_TWHR(x)		(min_t(unsigned int, x, 0xF) << 4)
1598c2ecf20Sopenharmony_ci#define NDTR1_TRHW(x)		(min_t(unsigned int, x / 16, 0x3) << 8)
1608c2ecf20Sopenharmony_ci#define NDTR1_PRESCALE		BIT(14)
1618c2ecf20Sopenharmony_ci#define NDTR1_WAIT_MODE		BIT(15)
1628c2ecf20Sopenharmony_ci#define NDTR1_TR(x)		(min_t(unsigned int, x, 0xFFFF) << 16)
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci/* NAND controller status register */
1658c2ecf20Sopenharmony_ci#define NDSR			0x14
1668c2ecf20Sopenharmony_ci#define NDSR_WRCMDREQ		BIT(0)
1678c2ecf20Sopenharmony_ci#define NDSR_RDDREQ		BIT(1)
1688c2ecf20Sopenharmony_ci#define NDSR_WRDREQ		BIT(2)
1698c2ecf20Sopenharmony_ci#define NDSR_CORERR		BIT(3)
1708c2ecf20Sopenharmony_ci#define NDSR_UNCERR		BIT(4)
1718c2ecf20Sopenharmony_ci#define NDSR_CMDD(cs)		BIT(8 - cs)
1728c2ecf20Sopenharmony_ci#define NDSR_RDY(rb)		BIT(11 + rb)
1738c2ecf20Sopenharmony_ci#define NDSR_ERRCNT(x)		((x >> 16) & 0x1F)
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci/* NAND ECC control register */
1768c2ecf20Sopenharmony_ci#define NDECCCTRL		0x28
1778c2ecf20Sopenharmony_ci#define NDECCCTRL_BCH_EN	BIT(0)
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci/* NAND controller data buffer register */
1808c2ecf20Sopenharmony_ci#define NDDB			0x40
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci/* NAND controller command buffer 0 register */
1838c2ecf20Sopenharmony_ci#define NDCB0			0x48
1848c2ecf20Sopenharmony_ci#define NDCB0_CMD1(x)		((x & 0xFF) << 0)
1858c2ecf20Sopenharmony_ci#define NDCB0_CMD2(x)		((x & 0xFF) << 8)
1868c2ecf20Sopenharmony_ci#define NDCB0_ADDR_CYC(x)	((x & 0x7) << 16)
1878c2ecf20Sopenharmony_ci#define NDCB0_ADDR_GET_NUM_CYC(x) (((x) >> 16) & 0x7)
1888c2ecf20Sopenharmony_ci#define NDCB0_DBC		BIT(19)
1898c2ecf20Sopenharmony_ci#define NDCB0_CMD_TYPE(x)	((x & 0x7) << 21)
1908c2ecf20Sopenharmony_ci#define NDCB0_CSEL		BIT(24)
1918c2ecf20Sopenharmony_ci#define NDCB0_RDY_BYP		BIT(27)
1928c2ecf20Sopenharmony_ci#define NDCB0_LEN_OVRD		BIT(28)
1938c2ecf20Sopenharmony_ci#define NDCB0_CMD_XTYPE(x)	((x & 0x7) << 29)
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci/* NAND controller command buffer 1 register */
1968c2ecf20Sopenharmony_ci#define NDCB1			0x4C
1978c2ecf20Sopenharmony_ci#define NDCB1_COLS(x)		((x & 0xFFFF) << 0)
1988c2ecf20Sopenharmony_ci#define NDCB1_ADDRS_PAGE(x)	(x << 16)
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci/* NAND controller command buffer 2 register */
2018c2ecf20Sopenharmony_ci#define NDCB2			0x50
2028c2ecf20Sopenharmony_ci#define NDCB2_ADDR5_PAGE(x)	(((x >> 16) & 0xFF) << 0)
2038c2ecf20Sopenharmony_ci#define NDCB2_ADDR5_CYC(x)	((x & 0xFF) << 0)
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci/* NAND controller command buffer 3 register */
2068c2ecf20Sopenharmony_ci#define NDCB3			0x54
2078c2ecf20Sopenharmony_ci#define NDCB3_ADDR6_CYC(x)	((x & 0xFF) << 16)
2088c2ecf20Sopenharmony_ci#define NDCB3_ADDR7_CYC(x)	((x & 0xFF) << 24)
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci/* NAND controller command buffer 0 register 'type' and 'xtype' fields */
2118c2ecf20Sopenharmony_ci#define TYPE_READ		0
2128c2ecf20Sopenharmony_ci#define TYPE_WRITE		1
2138c2ecf20Sopenharmony_ci#define TYPE_ERASE		2
2148c2ecf20Sopenharmony_ci#define TYPE_READ_ID		3
2158c2ecf20Sopenharmony_ci#define TYPE_STATUS		4
2168c2ecf20Sopenharmony_ci#define TYPE_RESET		5
2178c2ecf20Sopenharmony_ci#define TYPE_NAKED_CMD		6
2188c2ecf20Sopenharmony_ci#define TYPE_NAKED_ADDR		7
2198c2ecf20Sopenharmony_ci#define TYPE_MASK		7
2208c2ecf20Sopenharmony_ci#define XTYPE_MONOLITHIC_RW	0
2218c2ecf20Sopenharmony_ci#define XTYPE_LAST_NAKED_RW	1
2228c2ecf20Sopenharmony_ci#define XTYPE_FINAL_COMMAND	3
2238c2ecf20Sopenharmony_ci#define XTYPE_READ		4
2248c2ecf20Sopenharmony_ci#define XTYPE_WRITE_DISPATCH	4
2258c2ecf20Sopenharmony_ci#define XTYPE_NAKED_RW		5
2268c2ecf20Sopenharmony_ci#define XTYPE_COMMAND_DISPATCH	6
2278c2ecf20Sopenharmony_ci#define XTYPE_MASK		7
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci/**
2308c2ecf20Sopenharmony_ci * struct marvell_hw_ecc_layout - layout of Marvell ECC
2318c2ecf20Sopenharmony_ci *
2328c2ecf20Sopenharmony_ci * Marvell ECC engine works differently than the others, in order to limit the
2338c2ecf20Sopenharmony_ci * size of the IP, hardware engineers chose to set a fixed strength at 16 bits
2348c2ecf20Sopenharmony_ci * per subpage, and depending on a the desired strength needed by the NAND chip,
2358c2ecf20Sopenharmony_ci * a particular layout mixing data/spare/ecc is defined, with a possible last
2368c2ecf20Sopenharmony_ci * chunk smaller that the others.
2378c2ecf20Sopenharmony_ci *
2388c2ecf20Sopenharmony_ci * @writesize:		Full page size on which the layout applies
2398c2ecf20Sopenharmony_ci * @chunk:		Desired ECC chunk size on which the layout applies
2408c2ecf20Sopenharmony_ci * @strength:		Desired ECC strength (per chunk size bytes) on which the
2418c2ecf20Sopenharmony_ci *			layout applies
2428c2ecf20Sopenharmony_ci * @nchunks:		Total number of chunks
2438c2ecf20Sopenharmony_ci * @full_chunk_cnt:	Number of full-sized chunks, which is the number of
2448c2ecf20Sopenharmony_ci *			repetitions of the pattern:
2458c2ecf20Sopenharmony_ci *			(data_bytes + spare_bytes + ecc_bytes).
2468c2ecf20Sopenharmony_ci * @data_bytes:		Number of data bytes per chunk
2478c2ecf20Sopenharmony_ci * @spare_bytes:	Number of spare bytes per chunk
2488c2ecf20Sopenharmony_ci * @ecc_bytes:		Number of ecc bytes per chunk
2498c2ecf20Sopenharmony_ci * @last_data_bytes:	Number of data bytes in the last chunk
2508c2ecf20Sopenharmony_ci * @last_spare_bytes:	Number of spare bytes in the last chunk
2518c2ecf20Sopenharmony_ci * @last_ecc_bytes:	Number of ecc bytes in the last chunk
2528c2ecf20Sopenharmony_ci */
2538c2ecf20Sopenharmony_cistruct marvell_hw_ecc_layout {
2548c2ecf20Sopenharmony_ci	/* Constraints */
2558c2ecf20Sopenharmony_ci	int writesize;
2568c2ecf20Sopenharmony_ci	int chunk;
2578c2ecf20Sopenharmony_ci	int strength;
2588c2ecf20Sopenharmony_ci	/* Corresponding layout */
2598c2ecf20Sopenharmony_ci	int nchunks;
2608c2ecf20Sopenharmony_ci	int full_chunk_cnt;
2618c2ecf20Sopenharmony_ci	int data_bytes;
2628c2ecf20Sopenharmony_ci	int spare_bytes;
2638c2ecf20Sopenharmony_ci	int ecc_bytes;
2648c2ecf20Sopenharmony_ci	int last_data_bytes;
2658c2ecf20Sopenharmony_ci	int last_spare_bytes;
2668c2ecf20Sopenharmony_ci	int last_ecc_bytes;
2678c2ecf20Sopenharmony_ci};
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci#define MARVELL_LAYOUT(ws, dc, ds, nc, fcc, db, sb, eb, ldb, lsb, leb)	\
2708c2ecf20Sopenharmony_ci	{								\
2718c2ecf20Sopenharmony_ci		.writesize = ws,					\
2728c2ecf20Sopenharmony_ci		.chunk = dc,						\
2738c2ecf20Sopenharmony_ci		.strength = ds,						\
2748c2ecf20Sopenharmony_ci		.nchunks = nc,						\
2758c2ecf20Sopenharmony_ci		.full_chunk_cnt = fcc,					\
2768c2ecf20Sopenharmony_ci		.data_bytes = db,					\
2778c2ecf20Sopenharmony_ci		.spare_bytes = sb,					\
2788c2ecf20Sopenharmony_ci		.ecc_bytes = eb,					\
2798c2ecf20Sopenharmony_ci		.last_data_bytes = ldb,					\
2808c2ecf20Sopenharmony_ci		.last_spare_bytes = lsb,				\
2818c2ecf20Sopenharmony_ci		.last_ecc_bytes = leb,					\
2828c2ecf20Sopenharmony_ci	}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci/* Layouts explained in AN-379_Marvell_SoC_NFC_ECC */
2858c2ecf20Sopenharmony_cistatic const struct marvell_hw_ecc_layout marvell_nfc_layouts[] = {
2868c2ecf20Sopenharmony_ci	MARVELL_LAYOUT(  512,   512,  1,  1,  1,  512,  8,  8,  0,  0,  0),
2878c2ecf20Sopenharmony_ci	MARVELL_LAYOUT( 2048,   512,  1,  1,  1, 2048, 40, 24,  0,  0,  0),
2888c2ecf20Sopenharmony_ci	MARVELL_LAYOUT( 2048,   512,  4,  1,  1, 2048, 32, 30,  0,  0,  0),
2898c2ecf20Sopenharmony_ci	MARVELL_LAYOUT( 2048,   512,  8,  2,  1, 1024,  0, 30,1024,32, 30),
2908c2ecf20Sopenharmony_ci	MARVELL_LAYOUT( 4096,   512,  4,  2,  2, 2048, 32, 30,  0,  0,  0),
2918c2ecf20Sopenharmony_ci	MARVELL_LAYOUT( 4096,   512,  8,  5,  4, 1024,  0, 30,  0, 64, 30),
2928c2ecf20Sopenharmony_ci	MARVELL_LAYOUT( 8192,   512,  4,  4,  4, 2048,  0, 30,  0,  0,  0),
2938c2ecf20Sopenharmony_ci	MARVELL_LAYOUT( 8192,   512,  8,  9,  8, 1024,  0, 30,  0, 160, 30),
2948c2ecf20Sopenharmony_ci};
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci/**
2978c2ecf20Sopenharmony_ci * struct marvell_nand_chip_sel - CS line description
2988c2ecf20Sopenharmony_ci *
2998c2ecf20Sopenharmony_ci * The Nand Flash Controller has up to 4 CE and 2 RB pins. The CE selection
3008c2ecf20Sopenharmony_ci * is made by a field in NDCB0 register, and in another field in NDCB2 register.
3018c2ecf20Sopenharmony_ci * The datasheet describes the logic with an error: ADDR5 field is once
3028c2ecf20Sopenharmony_ci * declared at the beginning of NDCB2, and another time at its end. Because the
3038c2ecf20Sopenharmony_ci * ADDR5 field of NDCB2 may be used by other bytes, it would be more logical
3048c2ecf20Sopenharmony_ci * to use the last bit of this field instead of the first ones.
3058c2ecf20Sopenharmony_ci *
3068c2ecf20Sopenharmony_ci * @cs:			Wanted CE lane.
3078c2ecf20Sopenharmony_ci * @ndcb0_csel:		Value of the NDCB0 register with or without the flag
3088c2ecf20Sopenharmony_ci *			selecting the wanted CE lane. This is set once when
3098c2ecf20Sopenharmony_ci *			the Device Tree is probed.
3108c2ecf20Sopenharmony_ci * @rb:			Ready/Busy pin for the flash chip
3118c2ecf20Sopenharmony_ci */
3128c2ecf20Sopenharmony_cistruct marvell_nand_chip_sel {
3138c2ecf20Sopenharmony_ci	unsigned int cs;
3148c2ecf20Sopenharmony_ci	u32 ndcb0_csel;
3158c2ecf20Sopenharmony_ci	unsigned int rb;
3168c2ecf20Sopenharmony_ci};
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci/**
3198c2ecf20Sopenharmony_ci * struct marvell_nand_chip - stores NAND chip device related information
3208c2ecf20Sopenharmony_ci *
3218c2ecf20Sopenharmony_ci * @chip:		Base NAND chip structure
3228c2ecf20Sopenharmony_ci * @node:		Used to store NAND chips into a list
3238c2ecf20Sopenharmony_ci * @layout:		NAND layout when using hardware ECC
3248c2ecf20Sopenharmony_ci * @ndcr:		Controller register value for this NAND chip
3258c2ecf20Sopenharmony_ci * @ndtr0:		Timing registers 0 value for this NAND chip
3268c2ecf20Sopenharmony_ci * @ndtr1:		Timing registers 1 value for this NAND chip
3278c2ecf20Sopenharmony_ci * @addr_cyc:		Amount of cycles needed to pass column address
3288c2ecf20Sopenharmony_ci * @selected_die:	Current active CS
3298c2ecf20Sopenharmony_ci * @nsels:		Number of CS lines required by the NAND chip
3308c2ecf20Sopenharmony_ci * @sels:		Array of CS lines descriptions
3318c2ecf20Sopenharmony_ci */
3328c2ecf20Sopenharmony_cistruct marvell_nand_chip {
3338c2ecf20Sopenharmony_ci	struct nand_chip chip;
3348c2ecf20Sopenharmony_ci	struct list_head node;
3358c2ecf20Sopenharmony_ci	const struct marvell_hw_ecc_layout *layout;
3368c2ecf20Sopenharmony_ci	u32 ndcr;
3378c2ecf20Sopenharmony_ci	u32 ndtr0;
3388c2ecf20Sopenharmony_ci	u32 ndtr1;
3398c2ecf20Sopenharmony_ci	int addr_cyc;
3408c2ecf20Sopenharmony_ci	int selected_die;
3418c2ecf20Sopenharmony_ci	unsigned int nsels;
3428c2ecf20Sopenharmony_ci	struct marvell_nand_chip_sel sels[];
3438c2ecf20Sopenharmony_ci};
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_cistatic inline struct marvell_nand_chip *to_marvell_nand(struct nand_chip *chip)
3468c2ecf20Sopenharmony_ci{
3478c2ecf20Sopenharmony_ci	return container_of(chip, struct marvell_nand_chip, chip);
3488c2ecf20Sopenharmony_ci}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_cistatic inline struct marvell_nand_chip_sel *to_nand_sel(struct marvell_nand_chip
3518c2ecf20Sopenharmony_ci							*nand)
3528c2ecf20Sopenharmony_ci{
3538c2ecf20Sopenharmony_ci	return &nand->sels[nand->selected_die];
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci/**
3578c2ecf20Sopenharmony_ci * struct marvell_nfc_caps - NAND controller capabilities for distinction
3588c2ecf20Sopenharmony_ci *                           between compatible strings
3598c2ecf20Sopenharmony_ci *
3608c2ecf20Sopenharmony_ci * @max_cs_nb:		Number of Chip Select lines available
3618c2ecf20Sopenharmony_ci * @max_rb_nb:		Number of Ready/Busy lines available
3628c2ecf20Sopenharmony_ci * @need_system_controller: Indicates if the SoC needs to have access to the
3638c2ecf20Sopenharmony_ci *                      system controller (ie. to enable the NAND controller)
3648c2ecf20Sopenharmony_ci * @legacy_of_bindings:	Indicates if DT parsing must be done using the old
3658c2ecf20Sopenharmony_ci *			fashion way
3668c2ecf20Sopenharmony_ci * @is_nfcv2:		NFCv2 has numerous enhancements compared to NFCv1, ie.
3678c2ecf20Sopenharmony_ci *			BCH error detection and correction algorithm,
3688c2ecf20Sopenharmony_ci *			NDCB3 register has been added
3698c2ecf20Sopenharmony_ci * @use_dma:		Use dma for data transfers
3708c2ecf20Sopenharmony_ci */
3718c2ecf20Sopenharmony_cistruct marvell_nfc_caps {
3728c2ecf20Sopenharmony_ci	unsigned int max_cs_nb;
3738c2ecf20Sopenharmony_ci	unsigned int max_rb_nb;
3748c2ecf20Sopenharmony_ci	bool need_system_controller;
3758c2ecf20Sopenharmony_ci	bool legacy_of_bindings;
3768c2ecf20Sopenharmony_ci	bool is_nfcv2;
3778c2ecf20Sopenharmony_ci	bool use_dma;
3788c2ecf20Sopenharmony_ci};
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci/**
3818c2ecf20Sopenharmony_ci * struct marvell_nfc - stores Marvell NAND controller information
3828c2ecf20Sopenharmony_ci *
3838c2ecf20Sopenharmony_ci * @controller:		Base controller structure
3848c2ecf20Sopenharmony_ci * @dev:		Parent device (used to print error messages)
3858c2ecf20Sopenharmony_ci * @regs:		NAND controller registers
3868c2ecf20Sopenharmony_ci * @core_clk:		Core clock
3878c2ecf20Sopenharmony_ci * @reg_clk:		Registers clock
3888c2ecf20Sopenharmony_ci * @complete:		Completion object to wait for NAND controller events
3898c2ecf20Sopenharmony_ci * @assigned_cs:	Bitmask describing already assigned CS lines
3908c2ecf20Sopenharmony_ci * @chips:		List containing all the NAND chips attached to
3918c2ecf20Sopenharmony_ci *			this NAND controller
3928c2ecf20Sopenharmony_ci * @selected_chip:	Currently selected target chip
3938c2ecf20Sopenharmony_ci * @caps:		NAND controller capabilities for each compatible string
3948c2ecf20Sopenharmony_ci * @use_dma:		Whetner DMA is used
3958c2ecf20Sopenharmony_ci * @dma_chan:		DMA channel (NFCv1 only)
3968c2ecf20Sopenharmony_ci * @dma_buf:		32-bit aligned buffer for DMA transfers (NFCv1 only)
3978c2ecf20Sopenharmony_ci */
3988c2ecf20Sopenharmony_cistruct marvell_nfc {
3998c2ecf20Sopenharmony_ci	struct nand_controller controller;
4008c2ecf20Sopenharmony_ci	struct device *dev;
4018c2ecf20Sopenharmony_ci	void __iomem *regs;
4028c2ecf20Sopenharmony_ci	struct clk *core_clk;
4038c2ecf20Sopenharmony_ci	struct clk *reg_clk;
4048c2ecf20Sopenharmony_ci	struct completion complete;
4058c2ecf20Sopenharmony_ci	unsigned long assigned_cs;
4068c2ecf20Sopenharmony_ci	struct list_head chips;
4078c2ecf20Sopenharmony_ci	struct nand_chip *selected_chip;
4088c2ecf20Sopenharmony_ci	const struct marvell_nfc_caps *caps;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	/* DMA (NFCv1 only) */
4118c2ecf20Sopenharmony_ci	bool use_dma;
4128c2ecf20Sopenharmony_ci	struct dma_chan *dma_chan;
4138c2ecf20Sopenharmony_ci	u8 *dma_buf;
4148c2ecf20Sopenharmony_ci};
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_cistatic inline struct marvell_nfc *to_marvell_nfc(struct nand_controller *ctrl)
4178c2ecf20Sopenharmony_ci{
4188c2ecf20Sopenharmony_ci	return container_of(ctrl, struct marvell_nfc, controller);
4198c2ecf20Sopenharmony_ci}
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci/**
4228c2ecf20Sopenharmony_ci * struct marvell_nfc_timings - NAND controller timings expressed in NAND
4238c2ecf20Sopenharmony_ci *                              Controller clock cycles
4248c2ecf20Sopenharmony_ci *
4258c2ecf20Sopenharmony_ci * @tRP:		ND_nRE pulse width
4268c2ecf20Sopenharmony_ci * @tRH:		ND_nRE high duration
4278c2ecf20Sopenharmony_ci * @tWP:		ND_nWE pulse time
4288c2ecf20Sopenharmony_ci * @tWH:		ND_nWE high duration
4298c2ecf20Sopenharmony_ci * @tCS:		Enable signal setup time
4308c2ecf20Sopenharmony_ci * @tCH:		Enable signal hold time
4318c2ecf20Sopenharmony_ci * @tADL:		Address to write data delay
4328c2ecf20Sopenharmony_ci * @tAR:		ND_ALE low to ND_nRE low delay
4338c2ecf20Sopenharmony_ci * @tWHR:		ND_nWE high to ND_nRE low for status read
4348c2ecf20Sopenharmony_ci * @tRHW:		ND_nRE high duration, read to write delay
4358c2ecf20Sopenharmony_ci * @tR:			ND_nWE high to ND_nRE low for read
4368c2ecf20Sopenharmony_ci */
4378c2ecf20Sopenharmony_cistruct marvell_nfc_timings {
4388c2ecf20Sopenharmony_ci	/* NDTR0 fields */
4398c2ecf20Sopenharmony_ci	unsigned int tRP;
4408c2ecf20Sopenharmony_ci	unsigned int tRH;
4418c2ecf20Sopenharmony_ci	unsigned int tWP;
4428c2ecf20Sopenharmony_ci	unsigned int tWH;
4438c2ecf20Sopenharmony_ci	unsigned int tCS;
4448c2ecf20Sopenharmony_ci	unsigned int tCH;
4458c2ecf20Sopenharmony_ci	unsigned int tADL;
4468c2ecf20Sopenharmony_ci	/* NDTR1 fields */
4478c2ecf20Sopenharmony_ci	unsigned int tAR;
4488c2ecf20Sopenharmony_ci	unsigned int tWHR;
4498c2ecf20Sopenharmony_ci	unsigned int tRHW;
4508c2ecf20Sopenharmony_ci	unsigned int tR;
4518c2ecf20Sopenharmony_ci};
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci/**
4548c2ecf20Sopenharmony_ci * Derives a duration in numbers of clock cycles.
4558c2ecf20Sopenharmony_ci *
4568c2ecf20Sopenharmony_ci * @ps: Duration in pico-seconds
4578c2ecf20Sopenharmony_ci * @period_ns:  Clock period in nano-seconds
4588c2ecf20Sopenharmony_ci *
4598c2ecf20Sopenharmony_ci * Convert the duration in nano-seconds, then divide by the period and
4608c2ecf20Sopenharmony_ci * return the number of clock periods.
4618c2ecf20Sopenharmony_ci */
4628c2ecf20Sopenharmony_ci#define TO_CYCLES(ps, period_ns) (DIV_ROUND_UP(ps / 1000, period_ns))
4638c2ecf20Sopenharmony_ci#define TO_CYCLES64(ps, period_ns) (DIV_ROUND_UP_ULL(div_u64(ps, 1000), \
4648c2ecf20Sopenharmony_ci						     period_ns))
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci/**
4678c2ecf20Sopenharmony_ci * struct marvell_nfc_op - filled during the parsing of the ->exec_op()
4688c2ecf20Sopenharmony_ci *                         subop subset of instructions.
4698c2ecf20Sopenharmony_ci *
4708c2ecf20Sopenharmony_ci * @ndcb:		Array of values written to NDCBx registers
4718c2ecf20Sopenharmony_ci * @cle_ale_delay_ns:	Optional delay after the last CMD or ADDR cycle
4728c2ecf20Sopenharmony_ci * @rdy_timeout_ms:	Timeout for waits on Ready/Busy pin
4738c2ecf20Sopenharmony_ci * @rdy_delay_ns:	Optional delay after waiting for the RB pin
4748c2ecf20Sopenharmony_ci * @data_delay_ns:	Optional delay after the data xfer
4758c2ecf20Sopenharmony_ci * @data_instr_idx:	Index of the data instruction in the subop
4768c2ecf20Sopenharmony_ci * @data_instr:		Pointer to the data instruction in the subop
4778c2ecf20Sopenharmony_ci */
4788c2ecf20Sopenharmony_cistruct marvell_nfc_op {
4798c2ecf20Sopenharmony_ci	u32 ndcb[4];
4808c2ecf20Sopenharmony_ci	unsigned int cle_ale_delay_ns;
4818c2ecf20Sopenharmony_ci	unsigned int rdy_timeout_ms;
4828c2ecf20Sopenharmony_ci	unsigned int rdy_delay_ns;
4838c2ecf20Sopenharmony_ci	unsigned int data_delay_ns;
4848c2ecf20Sopenharmony_ci	unsigned int data_instr_idx;
4858c2ecf20Sopenharmony_ci	const struct nand_op_instr *data_instr;
4868c2ecf20Sopenharmony_ci};
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci/*
4898c2ecf20Sopenharmony_ci * Internal helper to conditionnally apply a delay (from the above structure,
4908c2ecf20Sopenharmony_ci * most of the time).
4918c2ecf20Sopenharmony_ci */
4928c2ecf20Sopenharmony_cistatic void cond_delay(unsigned int ns)
4938c2ecf20Sopenharmony_ci{
4948c2ecf20Sopenharmony_ci	if (!ns)
4958c2ecf20Sopenharmony_ci		return;
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	if (ns < 10000)
4988c2ecf20Sopenharmony_ci		ndelay(ns);
4998c2ecf20Sopenharmony_ci	else
5008c2ecf20Sopenharmony_ci		udelay(DIV_ROUND_UP(ns, 1000));
5018c2ecf20Sopenharmony_ci}
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci/*
5048c2ecf20Sopenharmony_ci * The controller has many flags that could generate interrupts, most of them
5058c2ecf20Sopenharmony_ci * are disabled and polling is used. For the very slow signals, using interrupts
5068c2ecf20Sopenharmony_ci * may relax the CPU charge.
5078c2ecf20Sopenharmony_ci */
5088c2ecf20Sopenharmony_cistatic void marvell_nfc_disable_int(struct marvell_nfc *nfc, u32 int_mask)
5098c2ecf20Sopenharmony_ci{
5108c2ecf20Sopenharmony_ci	u32 reg;
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	/* Writing 1 disables the interrupt */
5138c2ecf20Sopenharmony_ci	reg = readl_relaxed(nfc->regs + NDCR);
5148c2ecf20Sopenharmony_ci	writel_relaxed(reg | int_mask, nfc->regs + NDCR);
5158c2ecf20Sopenharmony_ci}
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_cistatic void marvell_nfc_enable_int(struct marvell_nfc *nfc, u32 int_mask)
5188c2ecf20Sopenharmony_ci{
5198c2ecf20Sopenharmony_ci	u32 reg;
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	/* Writing 0 enables the interrupt */
5228c2ecf20Sopenharmony_ci	reg = readl_relaxed(nfc->regs + NDCR);
5238c2ecf20Sopenharmony_ci	writel_relaxed(reg & ~int_mask, nfc->regs + NDCR);
5248c2ecf20Sopenharmony_ci}
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_cistatic u32 marvell_nfc_clear_int(struct marvell_nfc *nfc, u32 int_mask)
5278c2ecf20Sopenharmony_ci{
5288c2ecf20Sopenharmony_ci	u32 reg;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	reg = readl_relaxed(nfc->regs + NDSR);
5318c2ecf20Sopenharmony_ci	writel_relaxed(int_mask, nfc->regs + NDSR);
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	return reg & int_mask;
5348c2ecf20Sopenharmony_ci}
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_cistatic void marvell_nfc_force_byte_access(struct nand_chip *chip,
5378c2ecf20Sopenharmony_ci					  bool force_8bit)
5388c2ecf20Sopenharmony_ci{
5398c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
5408c2ecf20Sopenharmony_ci	u32 ndcr;
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	/*
5438c2ecf20Sopenharmony_ci	 * Callers of this function do not verify if the NAND is using a 16-bit
5448c2ecf20Sopenharmony_ci	 * an 8-bit bus for normal operations, so we need to take care of that
5458c2ecf20Sopenharmony_ci	 * here by leaving the configuration unchanged if the NAND does not have
5468c2ecf20Sopenharmony_ci	 * the NAND_BUSWIDTH_16 flag set.
5478c2ecf20Sopenharmony_ci	 */
5488c2ecf20Sopenharmony_ci	if (!(chip->options & NAND_BUSWIDTH_16))
5498c2ecf20Sopenharmony_ci		return;
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	ndcr = readl_relaxed(nfc->regs + NDCR);
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	if (force_8bit)
5548c2ecf20Sopenharmony_ci		ndcr &= ~(NDCR_DWIDTH_M | NDCR_DWIDTH_C);
5558c2ecf20Sopenharmony_ci	else
5568c2ecf20Sopenharmony_ci		ndcr |= NDCR_DWIDTH_M | NDCR_DWIDTH_C;
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	writel_relaxed(ndcr, nfc->regs + NDCR);
5598c2ecf20Sopenharmony_ci}
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_cistatic int marvell_nfc_wait_ndrun(struct nand_chip *chip)
5628c2ecf20Sopenharmony_ci{
5638c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
5648c2ecf20Sopenharmony_ci	u32 val;
5658c2ecf20Sopenharmony_ci	int ret;
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	/*
5688c2ecf20Sopenharmony_ci	 * The command is being processed, wait for the ND_RUN bit to be
5698c2ecf20Sopenharmony_ci	 * cleared by the NFC. If not, we must clear it by hand.
5708c2ecf20Sopenharmony_ci	 */
5718c2ecf20Sopenharmony_ci	ret = readl_relaxed_poll_timeout(nfc->regs + NDCR, val,
5728c2ecf20Sopenharmony_ci					 (val & NDCR_ND_RUN) == 0,
5738c2ecf20Sopenharmony_ci					 POLL_PERIOD, POLL_TIMEOUT);
5748c2ecf20Sopenharmony_ci	if (ret) {
5758c2ecf20Sopenharmony_ci		dev_err(nfc->dev, "Timeout on NAND controller run mode\n");
5768c2ecf20Sopenharmony_ci		writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
5778c2ecf20Sopenharmony_ci			       nfc->regs + NDCR);
5788c2ecf20Sopenharmony_ci		return ret;
5798c2ecf20Sopenharmony_ci	}
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	return 0;
5828c2ecf20Sopenharmony_ci}
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci/*
5858c2ecf20Sopenharmony_ci * Any time a command has to be sent to the controller, the following sequence
5868c2ecf20Sopenharmony_ci * has to be followed:
5878c2ecf20Sopenharmony_ci * - call marvell_nfc_prepare_cmd()
5888c2ecf20Sopenharmony_ci *      -> activate the ND_RUN bit that will kind of 'start a job'
5898c2ecf20Sopenharmony_ci *      -> wait the signal indicating the NFC is waiting for a command
5908c2ecf20Sopenharmony_ci * - send the command (cmd and address cycles)
5918c2ecf20Sopenharmony_ci * - enventually send or receive the data
5928c2ecf20Sopenharmony_ci * - call marvell_nfc_end_cmd() with the corresponding flag
5938c2ecf20Sopenharmony_ci *      -> wait the flag to be triggered or cancel the job with a timeout
5948c2ecf20Sopenharmony_ci *
5958c2ecf20Sopenharmony_ci * The following helpers are here to factorize the code a bit so that
5968c2ecf20Sopenharmony_ci * specialized functions responsible for executing the actual NAND
5978c2ecf20Sopenharmony_ci * operations do not have to replicate the same code blocks.
5988c2ecf20Sopenharmony_ci */
5998c2ecf20Sopenharmony_cistatic int marvell_nfc_prepare_cmd(struct nand_chip *chip)
6008c2ecf20Sopenharmony_ci{
6018c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
6028c2ecf20Sopenharmony_ci	u32 ndcr, val;
6038c2ecf20Sopenharmony_ci	int ret;
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	/* Poll ND_RUN and clear NDSR before issuing any command */
6068c2ecf20Sopenharmony_ci	ret = marvell_nfc_wait_ndrun(chip);
6078c2ecf20Sopenharmony_ci	if (ret) {
6088c2ecf20Sopenharmony_ci		dev_err(nfc->dev, "Last operation did not succeed\n");
6098c2ecf20Sopenharmony_ci		return ret;
6108c2ecf20Sopenharmony_ci	}
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	ndcr = readl_relaxed(nfc->regs + NDCR);
6138c2ecf20Sopenharmony_ci	writel_relaxed(readl(nfc->regs + NDSR), nfc->regs + NDSR);
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	/* Assert ND_RUN bit and wait the NFC to be ready */
6168c2ecf20Sopenharmony_ci	writel_relaxed(ndcr | NDCR_ND_RUN, nfc->regs + NDCR);
6178c2ecf20Sopenharmony_ci	ret = readl_relaxed_poll_timeout(nfc->regs + NDSR, val,
6188c2ecf20Sopenharmony_ci					 val & NDSR_WRCMDREQ,
6198c2ecf20Sopenharmony_ci					 POLL_PERIOD, POLL_TIMEOUT);
6208c2ecf20Sopenharmony_ci	if (ret) {
6218c2ecf20Sopenharmony_ci		dev_err(nfc->dev, "Timeout on WRCMDRE\n");
6228c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
6238c2ecf20Sopenharmony_ci	}
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	/* Command may be written, clear WRCMDREQ status bit */
6268c2ecf20Sopenharmony_ci	writel_relaxed(NDSR_WRCMDREQ, nfc->regs + NDSR);
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	return 0;
6298c2ecf20Sopenharmony_ci}
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_cistatic void marvell_nfc_send_cmd(struct nand_chip *chip,
6328c2ecf20Sopenharmony_ci				 struct marvell_nfc_op *nfc_op)
6338c2ecf20Sopenharmony_ci{
6348c2ecf20Sopenharmony_ci	struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
6358c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	dev_dbg(nfc->dev, "\nNDCR:  0x%08x\n"
6388c2ecf20Sopenharmony_ci		"NDCB0: 0x%08x\nNDCB1: 0x%08x\nNDCB2: 0x%08x\nNDCB3: 0x%08x\n",
6398c2ecf20Sopenharmony_ci		(u32)readl_relaxed(nfc->regs + NDCR), nfc_op->ndcb[0],
6408c2ecf20Sopenharmony_ci		nfc_op->ndcb[1], nfc_op->ndcb[2], nfc_op->ndcb[3]);
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci	writel_relaxed(to_nand_sel(marvell_nand)->ndcb0_csel | nfc_op->ndcb[0],
6438c2ecf20Sopenharmony_ci		       nfc->regs + NDCB0);
6448c2ecf20Sopenharmony_ci	writel_relaxed(nfc_op->ndcb[1], nfc->regs + NDCB0);
6458c2ecf20Sopenharmony_ci	writel(nfc_op->ndcb[2], nfc->regs + NDCB0);
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci	/*
6488c2ecf20Sopenharmony_ci	 * Write NDCB0 four times only if LEN_OVRD is set or if ADDR6 or ADDR7
6498c2ecf20Sopenharmony_ci	 * fields are used (only available on NFCv2).
6508c2ecf20Sopenharmony_ci	 */
6518c2ecf20Sopenharmony_ci	if (nfc_op->ndcb[0] & NDCB0_LEN_OVRD ||
6528c2ecf20Sopenharmony_ci	    NDCB0_ADDR_GET_NUM_CYC(nfc_op->ndcb[0]) >= 6) {
6538c2ecf20Sopenharmony_ci		if (!WARN_ON_ONCE(!nfc->caps->is_nfcv2))
6548c2ecf20Sopenharmony_ci			writel(nfc_op->ndcb[3], nfc->regs + NDCB0);
6558c2ecf20Sopenharmony_ci	}
6568c2ecf20Sopenharmony_ci}
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_cistatic int marvell_nfc_end_cmd(struct nand_chip *chip, int flag,
6598c2ecf20Sopenharmony_ci			       const char *label)
6608c2ecf20Sopenharmony_ci{
6618c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
6628c2ecf20Sopenharmony_ci	u32 val;
6638c2ecf20Sopenharmony_ci	int ret;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	ret = readl_relaxed_poll_timeout(nfc->regs + NDSR, val,
6668c2ecf20Sopenharmony_ci					 val & flag,
6678c2ecf20Sopenharmony_ci					 POLL_PERIOD, POLL_TIMEOUT);
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	if (ret) {
6708c2ecf20Sopenharmony_ci		dev_err(nfc->dev, "Timeout on %s (NDSR: 0x%08x)\n",
6718c2ecf20Sopenharmony_ci			label, val);
6728c2ecf20Sopenharmony_ci		if (nfc->dma_chan)
6738c2ecf20Sopenharmony_ci			dmaengine_terminate_all(nfc->dma_chan);
6748c2ecf20Sopenharmony_ci		return ret;
6758c2ecf20Sopenharmony_ci	}
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	/*
6788c2ecf20Sopenharmony_ci	 * DMA function uses this helper to poll on CMDD bits without wanting
6798c2ecf20Sopenharmony_ci	 * them to be cleared.
6808c2ecf20Sopenharmony_ci	 */
6818c2ecf20Sopenharmony_ci	if (nfc->use_dma && (readl_relaxed(nfc->regs + NDCR) & NDCR_DMA_EN))
6828c2ecf20Sopenharmony_ci		return 0;
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	writel_relaxed(flag, nfc->regs + NDSR);
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	return 0;
6878c2ecf20Sopenharmony_ci}
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_cistatic int marvell_nfc_wait_cmdd(struct nand_chip *chip)
6908c2ecf20Sopenharmony_ci{
6918c2ecf20Sopenharmony_ci	struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
6928c2ecf20Sopenharmony_ci	int cs_flag = NDSR_CMDD(to_nand_sel(marvell_nand)->ndcb0_csel);
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	return marvell_nfc_end_cmd(chip, cs_flag, "CMDD");
6958c2ecf20Sopenharmony_ci}
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_cistatic int marvell_nfc_poll_status(struct marvell_nfc *nfc, u32 mask,
6988c2ecf20Sopenharmony_ci				   u32 expected_val, unsigned long timeout_ms)
6998c2ecf20Sopenharmony_ci{
7008c2ecf20Sopenharmony_ci	unsigned long limit;
7018c2ecf20Sopenharmony_ci	u32 st;
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci	limit = jiffies + msecs_to_jiffies(timeout_ms);
7048c2ecf20Sopenharmony_ci	do {
7058c2ecf20Sopenharmony_ci		st = readl_relaxed(nfc->regs + NDSR);
7068c2ecf20Sopenharmony_ci		if (st & NDSR_RDY(1))
7078c2ecf20Sopenharmony_ci			st |= NDSR_RDY(0);
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci		if ((st & mask) == expected_val)
7108c2ecf20Sopenharmony_ci			return 0;
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci		cpu_relax();
7138c2ecf20Sopenharmony_ci	} while (time_after(limit, jiffies));
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	return -ETIMEDOUT;
7168c2ecf20Sopenharmony_ci}
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_cistatic int marvell_nfc_wait_op(struct nand_chip *chip, unsigned int timeout_ms)
7198c2ecf20Sopenharmony_ci{
7208c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
7218c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
7228c2ecf20Sopenharmony_ci	u32 pending;
7238c2ecf20Sopenharmony_ci	int ret;
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	/* Timeout is expressed in ms */
7268c2ecf20Sopenharmony_ci	if (!timeout_ms)
7278c2ecf20Sopenharmony_ci		timeout_ms = IRQ_TIMEOUT;
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	if (mtd->oops_panic_write) {
7308c2ecf20Sopenharmony_ci		ret = marvell_nfc_poll_status(nfc, NDSR_RDY(0),
7318c2ecf20Sopenharmony_ci					      NDSR_RDY(0),
7328c2ecf20Sopenharmony_ci					      timeout_ms);
7338c2ecf20Sopenharmony_ci	} else {
7348c2ecf20Sopenharmony_ci		init_completion(&nfc->complete);
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci		marvell_nfc_enable_int(nfc, NDCR_RDYM);
7378c2ecf20Sopenharmony_ci		ret = wait_for_completion_timeout(&nfc->complete,
7388c2ecf20Sopenharmony_ci						  msecs_to_jiffies(timeout_ms));
7398c2ecf20Sopenharmony_ci		marvell_nfc_disable_int(nfc, NDCR_RDYM);
7408c2ecf20Sopenharmony_ci	}
7418c2ecf20Sopenharmony_ci	pending = marvell_nfc_clear_int(nfc, NDSR_RDY(0) | NDSR_RDY(1));
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	/*
7448c2ecf20Sopenharmony_ci	 * In case the interrupt was not served in the required time frame,
7458c2ecf20Sopenharmony_ci	 * check if the ISR was not served or if something went actually wrong.
7468c2ecf20Sopenharmony_ci	 */
7478c2ecf20Sopenharmony_ci	if (!ret && !pending) {
7488c2ecf20Sopenharmony_ci		dev_err(nfc->dev, "Timeout waiting for RB signal\n");
7498c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
7508c2ecf20Sopenharmony_ci	}
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	return 0;
7538c2ecf20Sopenharmony_ci}
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_cistatic void marvell_nfc_select_target(struct nand_chip *chip,
7568c2ecf20Sopenharmony_ci				      unsigned int die_nr)
7578c2ecf20Sopenharmony_ci{
7588c2ecf20Sopenharmony_ci	struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
7598c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
7608c2ecf20Sopenharmony_ci	u32 ndcr_generic;
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci	/*
7638c2ecf20Sopenharmony_ci	 * Reset the NDCR register to a clean state for this particular chip,
7648c2ecf20Sopenharmony_ci	 * also clear ND_RUN bit.
7658c2ecf20Sopenharmony_ci	 */
7668c2ecf20Sopenharmony_ci	ndcr_generic = readl_relaxed(nfc->regs + NDCR) &
7678c2ecf20Sopenharmony_ci		       NDCR_GENERIC_FIELDS_MASK & ~NDCR_ND_RUN;
7688c2ecf20Sopenharmony_ci	writel_relaxed(ndcr_generic | marvell_nand->ndcr, nfc->regs + NDCR);
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci	/* Also reset the interrupt status register */
7718c2ecf20Sopenharmony_ci	marvell_nfc_clear_int(nfc, NDCR_ALL_INT);
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	if (chip == nfc->selected_chip && die_nr == marvell_nand->selected_die)
7748c2ecf20Sopenharmony_ci		return;
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	writel_relaxed(marvell_nand->ndtr0, nfc->regs + NDTR0);
7778c2ecf20Sopenharmony_ci	writel_relaxed(marvell_nand->ndtr1, nfc->regs + NDTR1);
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	nfc->selected_chip = chip;
7808c2ecf20Sopenharmony_ci	marvell_nand->selected_die = die_nr;
7818c2ecf20Sopenharmony_ci}
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_cistatic irqreturn_t marvell_nfc_isr(int irq, void *dev_id)
7848c2ecf20Sopenharmony_ci{
7858c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = dev_id;
7868c2ecf20Sopenharmony_ci	u32 st = readl_relaxed(nfc->regs + NDSR);
7878c2ecf20Sopenharmony_ci	u32 ien = (~readl_relaxed(nfc->regs + NDCR)) & NDCR_ALL_INT;
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	/*
7908c2ecf20Sopenharmony_ci	 * RDY interrupt mask is one bit in NDCR while there are two status
7918c2ecf20Sopenharmony_ci	 * bit in NDSR (RDY[cs0/cs2] and RDY[cs1/cs3]).
7928c2ecf20Sopenharmony_ci	 */
7938c2ecf20Sopenharmony_ci	if (st & NDSR_RDY(1))
7948c2ecf20Sopenharmony_ci		st |= NDSR_RDY(0);
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	if (!(st & ien))
7978c2ecf20Sopenharmony_ci		return IRQ_NONE;
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci	marvell_nfc_disable_int(nfc, st & NDCR_ALL_INT);
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	if (st & (NDSR_RDY(0) | NDSR_RDY(1)))
8028c2ecf20Sopenharmony_ci		complete(&nfc->complete);
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
8058c2ecf20Sopenharmony_ci}
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci/* HW ECC related functions */
8088c2ecf20Sopenharmony_cistatic void marvell_nfc_enable_hw_ecc(struct nand_chip *chip)
8098c2ecf20Sopenharmony_ci{
8108c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
8118c2ecf20Sopenharmony_ci	u32 ndcr = readl_relaxed(nfc->regs + NDCR);
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci	if (!(ndcr & NDCR_ECC_EN)) {
8148c2ecf20Sopenharmony_ci		writel_relaxed(ndcr | NDCR_ECC_EN, nfc->regs + NDCR);
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci		/*
8178c2ecf20Sopenharmony_ci		 * When enabling BCH, set threshold to 0 to always know the
8188c2ecf20Sopenharmony_ci		 * number of corrected bitflips.
8198c2ecf20Sopenharmony_ci		 */
8208c2ecf20Sopenharmony_ci		if (chip->ecc.algo == NAND_ECC_ALGO_BCH)
8218c2ecf20Sopenharmony_ci			writel_relaxed(NDECCCTRL_BCH_EN, nfc->regs + NDECCCTRL);
8228c2ecf20Sopenharmony_ci	}
8238c2ecf20Sopenharmony_ci}
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_cistatic void marvell_nfc_disable_hw_ecc(struct nand_chip *chip)
8268c2ecf20Sopenharmony_ci{
8278c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
8288c2ecf20Sopenharmony_ci	u32 ndcr = readl_relaxed(nfc->regs + NDCR);
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	if (ndcr & NDCR_ECC_EN) {
8318c2ecf20Sopenharmony_ci		writel_relaxed(ndcr & ~NDCR_ECC_EN, nfc->regs + NDCR);
8328c2ecf20Sopenharmony_ci		if (chip->ecc.algo == NAND_ECC_ALGO_BCH)
8338c2ecf20Sopenharmony_ci			writel_relaxed(0, nfc->regs + NDECCCTRL);
8348c2ecf20Sopenharmony_ci	}
8358c2ecf20Sopenharmony_ci}
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci/* DMA related helpers */
8388c2ecf20Sopenharmony_cistatic void marvell_nfc_enable_dma(struct marvell_nfc *nfc)
8398c2ecf20Sopenharmony_ci{
8408c2ecf20Sopenharmony_ci	u32 reg;
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	reg = readl_relaxed(nfc->regs + NDCR);
8438c2ecf20Sopenharmony_ci	writel_relaxed(reg | NDCR_DMA_EN, nfc->regs + NDCR);
8448c2ecf20Sopenharmony_ci}
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_cistatic void marvell_nfc_disable_dma(struct marvell_nfc *nfc)
8478c2ecf20Sopenharmony_ci{
8488c2ecf20Sopenharmony_ci	u32 reg;
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci	reg = readl_relaxed(nfc->regs + NDCR);
8518c2ecf20Sopenharmony_ci	writel_relaxed(reg & ~NDCR_DMA_EN, nfc->regs + NDCR);
8528c2ecf20Sopenharmony_ci}
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci/* Read/write PIO/DMA accessors */
8558c2ecf20Sopenharmony_cistatic int marvell_nfc_xfer_data_dma(struct marvell_nfc *nfc,
8568c2ecf20Sopenharmony_ci				     enum dma_data_direction direction,
8578c2ecf20Sopenharmony_ci				     unsigned int len)
8588c2ecf20Sopenharmony_ci{
8598c2ecf20Sopenharmony_ci	unsigned int dma_len = min_t(int, ALIGN(len, 32), MAX_CHUNK_SIZE);
8608c2ecf20Sopenharmony_ci	struct dma_async_tx_descriptor *tx;
8618c2ecf20Sopenharmony_ci	struct scatterlist sg;
8628c2ecf20Sopenharmony_ci	dma_cookie_t cookie;
8638c2ecf20Sopenharmony_ci	int ret;
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	marvell_nfc_enable_dma(nfc);
8668c2ecf20Sopenharmony_ci	/* Prepare the DMA transfer */
8678c2ecf20Sopenharmony_ci	sg_init_one(&sg, nfc->dma_buf, dma_len);
8688c2ecf20Sopenharmony_ci	dma_map_sg(nfc->dma_chan->device->dev, &sg, 1, direction);
8698c2ecf20Sopenharmony_ci	tx = dmaengine_prep_slave_sg(nfc->dma_chan, &sg, 1,
8708c2ecf20Sopenharmony_ci				     direction == DMA_FROM_DEVICE ?
8718c2ecf20Sopenharmony_ci				     DMA_DEV_TO_MEM : DMA_MEM_TO_DEV,
8728c2ecf20Sopenharmony_ci				     DMA_PREP_INTERRUPT);
8738c2ecf20Sopenharmony_ci	if (!tx) {
8748c2ecf20Sopenharmony_ci		dev_err(nfc->dev, "Could not prepare DMA S/G list\n");
8758c2ecf20Sopenharmony_ci		return -ENXIO;
8768c2ecf20Sopenharmony_ci	}
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	/* Do the task and wait for it to finish */
8798c2ecf20Sopenharmony_ci	cookie = dmaengine_submit(tx);
8808c2ecf20Sopenharmony_ci	ret = dma_submit_error(cookie);
8818c2ecf20Sopenharmony_ci	if (ret)
8828c2ecf20Sopenharmony_ci		return -EIO;
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci	dma_async_issue_pending(nfc->dma_chan);
8858c2ecf20Sopenharmony_ci	ret = marvell_nfc_wait_cmdd(nfc->selected_chip);
8868c2ecf20Sopenharmony_ci	dma_unmap_sg(nfc->dma_chan->device->dev, &sg, 1, direction);
8878c2ecf20Sopenharmony_ci	marvell_nfc_disable_dma(nfc);
8888c2ecf20Sopenharmony_ci	if (ret) {
8898c2ecf20Sopenharmony_ci		dev_err(nfc->dev, "Timeout waiting for DMA (status: %d)\n",
8908c2ecf20Sopenharmony_ci			dmaengine_tx_status(nfc->dma_chan, cookie, NULL));
8918c2ecf20Sopenharmony_ci		dmaengine_terminate_all(nfc->dma_chan);
8928c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
8938c2ecf20Sopenharmony_ci	}
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	return 0;
8968c2ecf20Sopenharmony_ci}
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_cistatic int marvell_nfc_xfer_data_in_pio(struct marvell_nfc *nfc, u8 *in,
8998c2ecf20Sopenharmony_ci					unsigned int len)
9008c2ecf20Sopenharmony_ci{
9018c2ecf20Sopenharmony_ci	unsigned int last_len = len % FIFO_DEPTH;
9028c2ecf20Sopenharmony_ci	unsigned int last_full_offset = round_down(len, FIFO_DEPTH);
9038c2ecf20Sopenharmony_ci	int i;
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	for (i = 0; i < last_full_offset; i += FIFO_DEPTH)
9068c2ecf20Sopenharmony_ci		ioread32_rep(nfc->regs + NDDB, in + i, FIFO_REP(FIFO_DEPTH));
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_ci	if (last_len) {
9098c2ecf20Sopenharmony_ci		u8 tmp_buf[FIFO_DEPTH];
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci		ioread32_rep(nfc->regs + NDDB, tmp_buf, FIFO_REP(FIFO_DEPTH));
9128c2ecf20Sopenharmony_ci		memcpy(in + last_full_offset, tmp_buf, last_len);
9138c2ecf20Sopenharmony_ci	}
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci	return 0;
9168c2ecf20Sopenharmony_ci}
9178c2ecf20Sopenharmony_ci
9188c2ecf20Sopenharmony_cistatic int marvell_nfc_xfer_data_out_pio(struct marvell_nfc *nfc, const u8 *out,
9198c2ecf20Sopenharmony_ci					 unsigned int len)
9208c2ecf20Sopenharmony_ci{
9218c2ecf20Sopenharmony_ci	unsigned int last_len = len % FIFO_DEPTH;
9228c2ecf20Sopenharmony_ci	unsigned int last_full_offset = round_down(len, FIFO_DEPTH);
9238c2ecf20Sopenharmony_ci	int i;
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_ci	for (i = 0; i < last_full_offset; i += FIFO_DEPTH)
9268c2ecf20Sopenharmony_ci		iowrite32_rep(nfc->regs + NDDB, out + i, FIFO_REP(FIFO_DEPTH));
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci	if (last_len) {
9298c2ecf20Sopenharmony_ci		u8 tmp_buf[FIFO_DEPTH];
9308c2ecf20Sopenharmony_ci
9318c2ecf20Sopenharmony_ci		memcpy(tmp_buf, out + last_full_offset, last_len);
9328c2ecf20Sopenharmony_ci		iowrite32_rep(nfc->regs + NDDB, tmp_buf, FIFO_REP(FIFO_DEPTH));
9338c2ecf20Sopenharmony_ci	}
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_ci	return 0;
9368c2ecf20Sopenharmony_ci}
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_cistatic void marvell_nfc_check_empty_chunk(struct nand_chip *chip,
9398c2ecf20Sopenharmony_ci					  u8 *data, int data_len,
9408c2ecf20Sopenharmony_ci					  u8 *spare, int spare_len,
9418c2ecf20Sopenharmony_ci					  u8 *ecc, int ecc_len,
9428c2ecf20Sopenharmony_ci					  unsigned int *max_bitflips)
9438c2ecf20Sopenharmony_ci{
9448c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
9458c2ecf20Sopenharmony_ci	int bf;
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_ci	/*
9488c2ecf20Sopenharmony_ci	 * Blank pages (all 0xFF) that have not been written may be recognized
9498c2ecf20Sopenharmony_ci	 * as bad if bitflips occur, so whenever an uncorrectable error occurs,
9508c2ecf20Sopenharmony_ci	 * check if the entire page (with ECC bytes) is actually blank or not.
9518c2ecf20Sopenharmony_ci	 */
9528c2ecf20Sopenharmony_ci	if (!data)
9538c2ecf20Sopenharmony_ci		data_len = 0;
9548c2ecf20Sopenharmony_ci	if (!spare)
9558c2ecf20Sopenharmony_ci		spare_len = 0;
9568c2ecf20Sopenharmony_ci	if (!ecc)
9578c2ecf20Sopenharmony_ci		ecc_len = 0;
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci	bf = nand_check_erased_ecc_chunk(data, data_len, ecc, ecc_len,
9608c2ecf20Sopenharmony_ci					 spare, spare_len, chip->ecc.strength);
9618c2ecf20Sopenharmony_ci	if (bf < 0) {
9628c2ecf20Sopenharmony_ci		mtd->ecc_stats.failed++;
9638c2ecf20Sopenharmony_ci		return;
9648c2ecf20Sopenharmony_ci	}
9658c2ecf20Sopenharmony_ci
9668c2ecf20Sopenharmony_ci	/* Update the stats and max_bitflips */
9678c2ecf20Sopenharmony_ci	mtd->ecc_stats.corrected += bf;
9688c2ecf20Sopenharmony_ci	*max_bitflips = max_t(unsigned int, *max_bitflips, bf);
9698c2ecf20Sopenharmony_ci}
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci/*
9728c2ecf20Sopenharmony_ci * Check if a chunk is correct or not according to the hardware ECC engine.
9738c2ecf20Sopenharmony_ci * mtd->ecc_stats.corrected is updated, as well as max_bitflips, however
9748c2ecf20Sopenharmony_ci * mtd->ecc_stats.failure is not, the function will instead return a non-zero
9758c2ecf20Sopenharmony_ci * value indicating that a check on the emptyness of the subpage must be
9768c2ecf20Sopenharmony_ci * performed before actually declaring the subpage as "corrupted".
9778c2ecf20Sopenharmony_ci */
9788c2ecf20Sopenharmony_cistatic int marvell_nfc_hw_ecc_check_bitflips(struct nand_chip *chip,
9798c2ecf20Sopenharmony_ci					     unsigned int *max_bitflips)
9808c2ecf20Sopenharmony_ci{
9818c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
9828c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
9838c2ecf20Sopenharmony_ci	int bf = 0;
9848c2ecf20Sopenharmony_ci	u32 ndsr;
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_ci	ndsr = readl_relaxed(nfc->regs + NDSR);
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci	/* Check uncorrectable error flag */
9898c2ecf20Sopenharmony_ci	if (ndsr & NDSR_UNCERR) {
9908c2ecf20Sopenharmony_ci		writel_relaxed(ndsr, nfc->regs + NDSR);
9918c2ecf20Sopenharmony_ci
9928c2ecf20Sopenharmony_ci		/*
9938c2ecf20Sopenharmony_ci		 * Do not increment ->ecc_stats.failed now, instead, return a
9948c2ecf20Sopenharmony_ci		 * non-zero value to indicate that this chunk was apparently
9958c2ecf20Sopenharmony_ci		 * bad, and it should be check to see if it empty or not. If
9968c2ecf20Sopenharmony_ci		 * the chunk (with ECC bytes) is not declared empty, the calling
9978c2ecf20Sopenharmony_ci		 * function must increment the failure count.
9988c2ecf20Sopenharmony_ci		 */
9998c2ecf20Sopenharmony_ci		return -EBADMSG;
10008c2ecf20Sopenharmony_ci	}
10018c2ecf20Sopenharmony_ci
10028c2ecf20Sopenharmony_ci	/* Check correctable error flag */
10038c2ecf20Sopenharmony_ci	if (ndsr & NDSR_CORERR) {
10048c2ecf20Sopenharmony_ci		writel_relaxed(ndsr, nfc->regs + NDSR);
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_ci		if (chip->ecc.algo == NAND_ECC_ALGO_BCH)
10078c2ecf20Sopenharmony_ci			bf = NDSR_ERRCNT(ndsr);
10088c2ecf20Sopenharmony_ci		else
10098c2ecf20Sopenharmony_ci			bf = 1;
10108c2ecf20Sopenharmony_ci	}
10118c2ecf20Sopenharmony_ci
10128c2ecf20Sopenharmony_ci	/* Update the stats and max_bitflips */
10138c2ecf20Sopenharmony_ci	mtd->ecc_stats.corrected += bf;
10148c2ecf20Sopenharmony_ci	*max_bitflips = max_t(unsigned int, *max_bitflips, bf);
10158c2ecf20Sopenharmony_ci
10168c2ecf20Sopenharmony_ci	return 0;
10178c2ecf20Sopenharmony_ci}
10188c2ecf20Sopenharmony_ci
10198c2ecf20Sopenharmony_ci/* Hamming read helpers */
10208c2ecf20Sopenharmony_cistatic int marvell_nfc_hw_ecc_hmg_do_read_page(struct nand_chip *chip,
10218c2ecf20Sopenharmony_ci					       u8 *data_buf, u8 *oob_buf,
10228c2ecf20Sopenharmony_ci					       bool raw, int page)
10238c2ecf20Sopenharmony_ci{
10248c2ecf20Sopenharmony_ci	struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
10258c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
10268c2ecf20Sopenharmony_ci	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
10278c2ecf20Sopenharmony_ci	struct marvell_nfc_op nfc_op = {
10288c2ecf20Sopenharmony_ci		.ndcb[0] = NDCB0_CMD_TYPE(TYPE_READ) |
10298c2ecf20Sopenharmony_ci			   NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
10308c2ecf20Sopenharmony_ci			   NDCB0_DBC |
10318c2ecf20Sopenharmony_ci			   NDCB0_CMD1(NAND_CMD_READ0) |
10328c2ecf20Sopenharmony_ci			   NDCB0_CMD2(NAND_CMD_READSTART),
10338c2ecf20Sopenharmony_ci		.ndcb[1] = NDCB1_ADDRS_PAGE(page),
10348c2ecf20Sopenharmony_ci		.ndcb[2] = NDCB2_ADDR5_PAGE(page),
10358c2ecf20Sopenharmony_ci	};
10368c2ecf20Sopenharmony_ci	unsigned int oob_bytes = lt->spare_bytes + (raw ? lt->ecc_bytes : 0);
10378c2ecf20Sopenharmony_ci	int ret;
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_ci	/* NFCv2 needs more information about the operation being executed */
10408c2ecf20Sopenharmony_ci	if (nfc->caps->is_nfcv2)
10418c2ecf20Sopenharmony_ci		nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_ci	ret = marvell_nfc_prepare_cmd(chip);
10448c2ecf20Sopenharmony_ci	if (ret)
10458c2ecf20Sopenharmony_ci		return ret;
10468c2ecf20Sopenharmony_ci
10478c2ecf20Sopenharmony_ci	marvell_nfc_send_cmd(chip, &nfc_op);
10488c2ecf20Sopenharmony_ci	ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
10498c2ecf20Sopenharmony_ci				  "RDDREQ while draining FIFO (data/oob)");
10508c2ecf20Sopenharmony_ci	if (ret)
10518c2ecf20Sopenharmony_ci		return ret;
10528c2ecf20Sopenharmony_ci
10538c2ecf20Sopenharmony_ci	/*
10548c2ecf20Sopenharmony_ci	 * Read the page then the OOB area. Unlike what is shown in current
10558c2ecf20Sopenharmony_ci	 * documentation, spare bytes are protected by the ECC engine, and must
10568c2ecf20Sopenharmony_ci	 * be at the beginning of the OOB area or running this driver on legacy
10578c2ecf20Sopenharmony_ci	 * systems will prevent the discovery of the BBM/BBT.
10588c2ecf20Sopenharmony_ci	 */
10598c2ecf20Sopenharmony_ci	if (nfc->use_dma) {
10608c2ecf20Sopenharmony_ci		marvell_nfc_xfer_data_dma(nfc, DMA_FROM_DEVICE,
10618c2ecf20Sopenharmony_ci					  lt->data_bytes + oob_bytes);
10628c2ecf20Sopenharmony_ci		memcpy(data_buf, nfc->dma_buf, lt->data_bytes);
10638c2ecf20Sopenharmony_ci		memcpy(oob_buf, nfc->dma_buf + lt->data_bytes, oob_bytes);
10648c2ecf20Sopenharmony_ci	} else {
10658c2ecf20Sopenharmony_ci		marvell_nfc_xfer_data_in_pio(nfc, data_buf, lt->data_bytes);
10668c2ecf20Sopenharmony_ci		marvell_nfc_xfer_data_in_pio(nfc, oob_buf, oob_bytes);
10678c2ecf20Sopenharmony_ci	}
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci	ret = marvell_nfc_wait_cmdd(chip);
10708c2ecf20Sopenharmony_ci	return ret;
10718c2ecf20Sopenharmony_ci}
10728c2ecf20Sopenharmony_ci
10738c2ecf20Sopenharmony_cistatic int marvell_nfc_hw_ecc_hmg_read_page_raw(struct nand_chip *chip, u8 *buf,
10748c2ecf20Sopenharmony_ci						int oob_required, int page)
10758c2ecf20Sopenharmony_ci{
10768c2ecf20Sopenharmony_ci	marvell_nfc_select_target(chip, chip->cur_cs);
10778c2ecf20Sopenharmony_ci	return marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi,
10788c2ecf20Sopenharmony_ci						   true, page);
10798c2ecf20Sopenharmony_ci}
10808c2ecf20Sopenharmony_ci
10818c2ecf20Sopenharmony_cistatic int marvell_nfc_hw_ecc_hmg_read_page(struct nand_chip *chip, u8 *buf,
10828c2ecf20Sopenharmony_ci					    int oob_required, int page)
10838c2ecf20Sopenharmony_ci{
10848c2ecf20Sopenharmony_ci	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
10858c2ecf20Sopenharmony_ci	unsigned int full_sz = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
10868c2ecf20Sopenharmony_ci	int max_bitflips = 0, ret;
10878c2ecf20Sopenharmony_ci	u8 *raw_buf;
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_ci	marvell_nfc_select_target(chip, chip->cur_cs);
10908c2ecf20Sopenharmony_ci	marvell_nfc_enable_hw_ecc(chip);
10918c2ecf20Sopenharmony_ci	marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi, false,
10928c2ecf20Sopenharmony_ci					    page);
10938c2ecf20Sopenharmony_ci	ret = marvell_nfc_hw_ecc_check_bitflips(chip, &max_bitflips);
10948c2ecf20Sopenharmony_ci	marvell_nfc_disable_hw_ecc(chip);
10958c2ecf20Sopenharmony_ci
10968c2ecf20Sopenharmony_ci	if (!ret)
10978c2ecf20Sopenharmony_ci		return max_bitflips;
10988c2ecf20Sopenharmony_ci
10998c2ecf20Sopenharmony_ci	/*
11008c2ecf20Sopenharmony_ci	 * When ECC failures are detected, check if the full page has been
11018c2ecf20Sopenharmony_ci	 * written or not. Ignore the failure if it is actually empty.
11028c2ecf20Sopenharmony_ci	 */
11038c2ecf20Sopenharmony_ci	raw_buf = kmalloc(full_sz, GFP_KERNEL);
11048c2ecf20Sopenharmony_ci	if (!raw_buf)
11058c2ecf20Sopenharmony_ci		return -ENOMEM;
11068c2ecf20Sopenharmony_ci
11078c2ecf20Sopenharmony_ci	marvell_nfc_hw_ecc_hmg_do_read_page(chip, raw_buf, raw_buf +
11088c2ecf20Sopenharmony_ci					    lt->data_bytes, true, page);
11098c2ecf20Sopenharmony_ci	marvell_nfc_check_empty_chunk(chip, raw_buf, full_sz, NULL, 0, NULL, 0,
11108c2ecf20Sopenharmony_ci				      &max_bitflips);
11118c2ecf20Sopenharmony_ci	kfree(raw_buf);
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_ci	return max_bitflips;
11148c2ecf20Sopenharmony_ci}
11158c2ecf20Sopenharmony_ci
11168c2ecf20Sopenharmony_ci/*
11178c2ecf20Sopenharmony_ci * Spare area in Hamming layouts is not protected by the ECC engine (even if
11188c2ecf20Sopenharmony_ci * it appears before the ECC bytes when reading), the ->read_oob_raw() function
11198c2ecf20Sopenharmony_ci * also stands for ->read_oob().
11208c2ecf20Sopenharmony_ci */
11218c2ecf20Sopenharmony_cistatic int marvell_nfc_hw_ecc_hmg_read_oob_raw(struct nand_chip *chip, int page)
11228c2ecf20Sopenharmony_ci{
11238c2ecf20Sopenharmony_ci	u8 *buf = nand_get_data_buf(chip);
11248c2ecf20Sopenharmony_ci
11258c2ecf20Sopenharmony_ci	marvell_nfc_select_target(chip, chip->cur_cs);
11268c2ecf20Sopenharmony_ci	return marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi,
11278c2ecf20Sopenharmony_ci						   true, page);
11288c2ecf20Sopenharmony_ci}
11298c2ecf20Sopenharmony_ci
11308c2ecf20Sopenharmony_ci/* Hamming write helpers */
11318c2ecf20Sopenharmony_cistatic int marvell_nfc_hw_ecc_hmg_do_write_page(struct nand_chip *chip,
11328c2ecf20Sopenharmony_ci						const u8 *data_buf,
11338c2ecf20Sopenharmony_ci						const u8 *oob_buf, bool raw,
11348c2ecf20Sopenharmony_ci						int page)
11358c2ecf20Sopenharmony_ci{
11368c2ecf20Sopenharmony_ci	const struct nand_sdr_timings *sdr =
11378c2ecf20Sopenharmony_ci		nand_get_sdr_timings(nand_get_interface_config(chip));
11388c2ecf20Sopenharmony_ci	struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
11398c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
11408c2ecf20Sopenharmony_ci	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
11418c2ecf20Sopenharmony_ci	struct marvell_nfc_op nfc_op = {
11428c2ecf20Sopenharmony_ci		.ndcb[0] = NDCB0_CMD_TYPE(TYPE_WRITE) |
11438c2ecf20Sopenharmony_ci			   NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
11448c2ecf20Sopenharmony_ci			   NDCB0_CMD1(NAND_CMD_SEQIN) |
11458c2ecf20Sopenharmony_ci			   NDCB0_CMD2(NAND_CMD_PAGEPROG) |
11468c2ecf20Sopenharmony_ci			   NDCB0_DBC,
11478c2ecf20Sopenharmony_ci		.ndcb[1] = NDCB1_ADDRS_PAGE(page),
11488c2ecf20Sopenharmony_ci		.ndcb[2] = NDCB2_ADDR5_PAGE(page),
11498c2ecf20Sopenharmony_ci	};
11508c2ecf20Sopenharmony_ci	unsigned int oob_bytes = lt->spare_bytes + (raw ? lt->ecc_bytes : 0);
11518c2ecf20Sopenharmony_ci	u8 status;
11528c2ecf20Sopenharmony_ci	int ret;
11538c2ecf20Sopenharmony_ci
11548c2ecf20Sopenharmony_ci	/* NFCv2 needs more information about the operation being executed */
11558c2ecf20Sopenharmony_ci	if (nfc->caps->is_nfcv2)
11568c2ecf20Sopenharmony_ci		nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
11578c2ecf20Sopenharmony_ci
11588c2ecf20Sopenharmony_ci	ret = marvell_nfc_prepare_cmd(chip);
11598c2ecf20Sopenharmony_ci	if (ret)
11608c2ecf20Sopenharmony_ci		return ret;
11618c2ecf20Sopenharmony_ci
11628c2ecf20Sopenharmony_ci	marvell_nfc_send_cmd(chip, &nfc_op);
11638c2ecf20Sopenharmony_ci	ret = marvell_nfc_end_cmd(chip, NDSR_WRDREQ,
11648c2ecf20Sopenharmony_ci				  "WRDREQ while loading FIFO (data)");
11658c2ecf20Sopenharmony_ci	if (ret)
11668c2ecf20Sopenharmony_ci		return ret;
11678c2ecf20Sopenharmony_ci
11688c2ecf20Sopenharmony_ci	/* Write the page then the OOB area */
11698c2ecf20Sopenharmony_ci	if (nfc->use_dma) {
11708c2ecf20Sopenharmony_ci		memcpy(nfc->dma_buf, data_buf, lt->data_bytes);
11718c2ecf20Sopenharmony_ci		memcpy(nfc->dma_buf + lt->data_bytes, oob_buf, oob_bytes);
11728c2ecf20Sopenharmony_ci		marvell_nfc_xfer_data_dma(nfc, DMA_TO_DEVICE, lt->data_bytes +
11738c2ecf20Sopenharmony_ci					  lt->ecc_bytes + lt->spare_bytes);
11748c2ecf20Sopenharmony_ci	} else {
11758c2ecf20Sopenharmony_ci		marvell_nfc_xfer_data_out_pio(nfc, data_buf, lt->data_bytes);
11768c2ecf20Sopenharmony_ci		marvell_nfc_xfer_data_out_pio(nfc, oob_buf, oob_bytes);
11778c2ecf20Sopenharmony_ci	}
11788c2ecf20Sopenharmony_ci
11798c2ecf20Sopenharmony_ci	ret = marvell_nfc_wait_cmdd(chip);
11808c2ecf20Sopenharmony_ci	if (ret)
11818c2ecf20Sopenharmony_ci		return ret;
11828c2ecf20Sopenharmony_ci
11838c2ecf20Sopenharmony_ci	ret = marvell_nfc_wait_op(chip,
11848c2ecf20Sopenharmony_ci				  PSEC_TO_MSEC(sdr->tPROG_max));
11858c2ecf20Sopenharmony_ci	if (ret)
11868c2ecf20Sopenharmony_ci		return ret;
11878c2ecf20Sopenharmony_ci
11888c2ecf20Sopenharmony_ci	/* Check write status on the chip side */
11898c2ecf20Sopenharmony_ci	ret = nand_status_op(chip, &status);
11908c2ecf20Sopenharmony_ci	if (ret)
11918c2ecf20Sopenharmony_ci		return ret;
11928c2ecf20Sopenharmony_ci
11938c2ecf20Sopenharmony_ci	if (status & NAND_STATUS_FAIL)
11948c2ecf20Sopenharmony_ci		return -EIO;
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_ci	return 0;
11978c2ecf20Sopenharmony_ci}
11988c2ecf20Sopenharmony_ci
11998c2ecf20Sopenharmony_cistatic int marvell_nfc_hw_ecc_hmg_write_page_raw(struct nand_chip *chip,
12008c2ecf20Sopenharmony_ci						 const u8 *buf,
12018c2ecf20Sopenharmony_ci						 int oob_required, int page)
12028c2ecf20Sopenharmony_ci{
12038c2ecf20Sopenharmony_ci	marvell_nfc_select_target(chip, chip->cur_cs);
12048c2ecf20Sopenharmony_ci	return marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi,
12058c2ecf20Sopenharmony_ci						    true, page);
12068c2ecf20Sopenharmony_ci}
12078c2ecf20Sopenharmony_ci
12088c2ecf20Sopenharmony_cistatic int marvell_nfc_hw_ecc_hmg_write_page(struct nand_chip *chip,
12098c2ecf20Sopenharmony_ci					     const u8 *buf,
12108c2ecf20Sopenharmony_ci					     int oob_required, int page)
12118c2ecf20Sopenharmony_ci{
12128c2ecf20Sopenharmony_ci	int ret;
12138c2ecf20Sopenharmony_ci
12148c2ecf20Sopenharmony_ci	marvell_nfc_select_target(chip, chip->cur_cs);
12158c2ecf20Sopenharmony_ci	marvell_nfc_enable_hw_ecc(chip);
12168c2ecf20Sopenharmony_ci	ret = marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi,
12178c2ecf20Sopenharmony_ci						   false, page);
12188c2ecf20Sopenharmony_ci	marvell_nfc_disable_hw_ecc(chip);
12198c2ecf20Sopenharmony_ci
12208c2ecf20Sopenharmony_ci	return ret;
12218c2ecf20Sopenharmony_ci}
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_ci/*
12248c2ecf20Sopenharmony_ci * Spare area in Hamming layouts is not protected by the ECC engine (even if
12258c2ecf20Sopenharmony_ci * it appears before the ECC bytes when reading), the ->write_oob_raw() function
12268c2ecf20Sopenharmony_ci * also stands for ->write_oob().
12278c2ecf20Sopenharmony_ci */
12288c2ecf20Sopenharmony_cistatic int marvell_nfc_hw_ecc_hmg_write_oob_raw(struct nand_chip *chip,
12298c2ecf20Sopenharmony_ci						int page)
12308c2ecf20Sopenharmony_ci{
12318c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
12328c2ecf20Sopenharmony_ci	u8 *buf = nand_get_data_buf(chip);
12338c2ecf20Sopenharmony_ci
12348c2ecf20Sopenharmony_ci	memset(buf, 0xFF, mtd->writesize);
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_ci	marvell_nfc_select_target(chip, chip->cur_cs);
12378c2ecf20Sopenharmony_ci	return marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi,
12388c2ecf20Sopenharmony_ci						    true, page);
12398c2ecf20Sopenharmony_ci}
12408c2ecf20Sopenharmony_ci
12418c2ecf20Sopenharmony_ci/* BCH read helpers */
12428c2ecf20Sopenharmony_cistatic int marvell_nfc_hw_ecc_bch_read_page_raw(struct nand_chip *chip, u8 *buf,
12438c2ecf20Sopenharmony_ci						int oob_required, int page)
12448c2ecf20Sopenharmony_ci{
12458c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
12468c2ecf20Sopenharmony_ci	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
12478c2ecf20Sopenharmony_ci	u8 *oob = chip->oob_poi;
12488c2ecf20Sopenharmony_ci	int chunk_size = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
12498c2ecf20Sopenharmony_ci	int ecc_offset = (lt->full_chunk_cnt * lt->spare_bytes) +
12508c2ecf20Sopenharmony_ci		lt->last_spare_bytes;
12518c2ecf20Sopenharmony_ci	int data_len = lt->data_bytes;
12528c2ecf20Sopenharmony_ci	int spare_len = lt->spare_bytes;
12538c2ecf20Sopenharmony_ci	int ecc_len = lt->ecc_bytes;
12548c2ecf20Sopenharmony_ci	int chunk;
12558c2ecf20Sopenharmony_ci
12568c2ecf20Sopenharmony_ci	marvell_nfc_select_target(chip, chip->cur_cs);
12578c2ecf20Sopenharmony_ci
12588c2ecf20Sopenharmony_ci	if (oob_required)
12598c2ecf20Sopenharmony_ci		memset(chip->oob_poi, 0xFF, mtd->oobsize);
12608c2ecf20Sopenharmony_ci
12618c2ecf20Sopenharmony_ci	nand_read_page_op(chip, page, 0, NULL, 0);
12628c2ecf20Sopenharmony_ci
12638c2ecf20Sopenharmony_ci	for (chunk = 0; chunk < lt->nchunks; chunk++) {
12648c2ecf20Sopenharmony_ci		/* Update last chunk length */
12658c2ecf20Sopenharmony_ci		if (chunk >= lt->full_chunk_cnt) {
12668c2ecf20Sopenharmony_ci			data_len = lt->last_data_bytes;
12678c2ecf20Sopenharmony_ci			spare_len = lt->last_spare_bytes;
12688c2ecf20Sopenharmony_ci			ecc_len = lt->last_ecc_bytes;
12698c2ecf20Sopenharmony_ci		}
12708c2ecf20Sopenharmony_ci
12718c2ecf20Sopenharmony_ci		/* Read data bytes*/
12728c2ecf20Sopenharmony_ci		nand_change_read_column_op(chip, chunk * chunk_size,
12738c2ecf20Sopenharmony_ci					   buf + (lt->data_bytes * chunk),
12748c2ecf20Sopenharmony_ci					   data_len, false);
12758c2ecf20Sopenharmony_ci
12768c2ecf20Sopenharmony_ci		/* Read spare bytes */
12778c2ecf20Sopenharmony_ci		nand_read_data_op(chip, oob + (lt->spare_bytes * chunk),
12788c2ecf20Sopenharmony_ci				  spare_len, false, false);
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_ci		/* Read ECC bytes */
12818c2ecf20Sopenharmony_ci		nand_read_data_op(chip, oob + ecc_offset +
12828c2ecf20Sopenharmony_ci				  (ALIGN(lt->ecc_bytes, 32) * chunk),
12838c2ecf20Sopenharmony_ci				  ecc_len, false, false);
12848c2ecf20Sopenharmony_ci	}
12858c2ecf20Sopenharmony_ci
12868c2ecf20Sopenharmony_ci	return 0;
12878c2ecf20Sopenharmony_ci}
12888c2ecf20Sopenharmony_ci
12898c2ecf20Sopenharmony_cistatic void marvell_nfc_hw_ecc_bch_read_chunk(struct nand_chip *chip, int chunk,
12908c2ecf20Sopenharmony_ci					      u8 *data, unsigned int data_len,
12918c2ecf20Sopenharmony_ci					      u8 *spare, unsigned int spare_len,
12928c2ecf20Sopenharmony_ci					      int page)
12938c2ecf20Sopenharmony_ci{
12948c2ecf20Sopenharmony_ci	struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
12958c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
12968c2ecf20Sopenharmony_ci	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
12978c2ecf20Sopenharmony_ci	int i, ret;
12988c2ecf20Sopenharmony_ci	struct marvell_nfc_op nfc_op = {
12998c2ecf20Sopenharmony_ci		.ndcb[0] = NDCB0_CMD_TYPE(TYPE_READ) |
13008c2ecf20Sopenharmony_ci			   NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
13018c2ecf20Sopenharmony_ci			   NDCB0_LEN_OVRD,
13028c2ecf20Sopenharmony_ci		.ndcb[1] = NDCB1_ADDRS_PAGE(page),
13038c2ecf20Sopenharmony_ci		.ndcb[2] = NDCB2_ADDR5_PAGE(page),
13048c2ecf20Sopenharmony_ci		.ndcb[3] = data_len + spare_len,
13058c2ecf20Sopenharmony_ci	};
13068c2ecf20Sopenharmony_ci
13078c2ecf20Sopenharmony_ci	ret = marvell_nfc_prepare_cmd(chip);
13088c2ecf20Sopenharmony_ci	if (ret)
13098c2ecf20Sopenharmony_ci		return;
13108c2ecf20Sopenharmony_ci
13118c2ecf20Sopenharmony_ci	if (chunk == 0)
13128c2ecf20Sopenharmony_ci		nfc_op.ndcb[0] |= NDCB0_DBC |
13138c2ecf20Sopenharmony_ci				  NDCB0_CMD1(NAND_CMD_READ0) |
13148c2ecf20Sopenharmony_ci				  NDCB0_CMD2(NAND_CMD_READSTART);
13158c2ecf20Sopenharmony_ci
13168c2ecf20Sopenharmony_ci	/*
13178c2ecf20Sopenharmony_ci	 * Trigger the monolithic read on the first chunk, then naked read on
13188c2ecf20Sopenharmony_ci	 * intermediate chunks and finally a last naked read on the last chunk.
13198c2ecf20Sopenharmony_ci	 */
13208c2ecf20Sopenharmony_ci	if (chunk == 0)
13218c2ecf20Sopenharmony_ci		nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
13228c2ecf20Sopenharmony_ci	else if (chunk < lt->nchunks - 1)
13238c2ecf20Sopenharmony_ci		nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_NAKED_RW);
13248c2ecf20Sopenharmony_ci	else
13258c2ecf20Sopenharmony_ci		nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
13268c2ecf20Sopenharmony_ci
13278c2ecf20Sopenharmony_ci	marvell_nfc_send_cmd(chip, &nfc_op);
13288c2ecf20Sopenharmony_ci
13298c2ecf20Sopenharmony_ci	/*
13308c2ecf20Sopenharmony_ci	 * According to the datasheet, when reading from NDDB
13318c2ecf20Sopenharmony_ci	 * with BCH enabled, after each 32 bytes reads, we
13328c2ecf20Sopenharmony_ci	 * have to make sure that the NDSR.RDDREQ bit is set.
13338c2ecf20Sopenharmony_ci	 *
13348c2ecf20Sopenharmony_ci	 * Drain the FIFO, 8 32-bit reads at a time, and skip
13358c2ecf20Sopenharmony_ci	 * the polling on the last read.
13368c2ecf20Sopenharmony_ci	 *
13378c2ecf20Sopenharmony_ci	 * Length is a multiple of 32 bytes, hence it is a multiple of 8 too.
13388c2ecf20Sopenharmony_ci	 */
13398c2ecf20Sopenharmony_ci	for (i = 0; i < data_len; i += FIFO_DEPTH * BCH_SEQ_READS) {
13408c2ecf20Sopenharmony_ci		marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
13418c2ecf20Sopenharmony_ci				    "RDDREQ while draining FIFO (data)");
13428c2ecf20Sopenharmony_ci		marvell_nfc_xfer_data_in_pio(nfc, data,
13438c2ecf20Sopenharmony_ci					     FIFO_DEPTH * BCH_SEQ_READS);
13448c2ecf20Sopenharmony_ci		data += FIFO_DEPTH * BCH_SEQ_READS;
13458c2ecf20Sopenharmony_ci	}
13468c2ecf20Sopenharmony_ci
13478c2ecf20Sopenharmony_ci	for (i = 0; i < spare_len; i += FIFO_DEPTH * BCH_SEQ_READS) {
13488c2ecf20Sopenharmony_ci		marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
13498c2ecf20Sopenharmony_ci				    "RDDREQ while draining FIFO (OOB)");
13508c2ecf20Sopenharmony_ci		marvell_nfc_xfer_data_in_pio(nfc, spare,
13518c2ecf20Sopenharmony_ci					     FIFO_DEPTH * BCH_SEQ_READS);
13528c2ecf20Sopenharmony_ci		spare += FIFO_DEPTH * BCH_SEQ_READS;
13538c2ecf20Sopenharmony_ci	}
13548c2ecf20Sopenharmony_ci}
13558c2ecf20Sopenharmony_ci
13568c2ecf20Sopenharmony_cistatic int marvell_nfc_hw_ecc_bch_read_page(struct nand_chip *chip,
13578c2ecf20Sopenharmony_ci					    u8 *buf, int oob_required,
13588c2ecf20Sopenharmony_ci					    int page)
13598c2ecf20Sopenharmony_ci{
13608c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
13618c2ecf20Sopenharmony_ci	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
13628c2ecf20Sopenharmony_ci	int data_len = lt->data_bytes, spare_len = lt->spare_bytes;
13638c2ecf20Sopenharmony_ci	u8 *data = buf, *spare = chip->oob_poi;
13648c2ecf20Sopenharmony_ci	int max_bitflips = 0;
13658c2ecf20Sopenharmony_ci	u32 failure_mask = 0;
13668c2ecf20Sopenharmony_ci	int chunk, ret;
13678c2ecf20Sopenharmony_ci
13688c2ecf20Sopenharmony_ci	marvell_nfc_select_target(chip, chip->cur_cs);
13698c2ecf20Sopenharmony_ci
13708c2ecf20Sopenharmony_ci	/*
13718c2ecf20Sopenharmony_ci	 * With BCH, OOB is not fully used (and thus not read entirely), not
13728c2ecf20Sopenharmony_ci	 * expected bytes could show up at the end of the OOB buffer if not
13738c2ecf20Sopenharmony_ci	 * explicitly erased.
13748c2ecf20Sopenharmony_ci	 */
13758c2ecf20Sopenharmony_ci	if (oob_required)
13768c2ecf20Sopenharmony_ci		memset(chip->oob_poi, 0xFF, mtd->oobsize);
13778c2ecf20Sopenharmony_ci
13788c2ecf20Sopenharmony_ci	marvell_nfc_enable_hw_ecc(chip);
13798c2ecf20Sopenharmony_ci
13808c2ecf20Sopenharmony_ci	for (chunk = 0; chunk < lt->nchunks; chunk++) {
13818c2ecf20Sopenharmony_ci		/* Update length for the last chunk */
13828c2ecf20Sopenharmony_ci		if (chunk >= lt->full_chunk_cnt) {
13838c2ecf20Sopenharmony_ci			data_len = lt->last_data_bytes;
13848c2ecf20Sopenharmony_ci			spare_len = lt->last_spare_bytes;
13858c2ecf20Sopenharmony_ci		}
13868c2ecf20Sopenharmony_ci
13878c2ecf20Sopenharmony_ci		/* Read the chunk and detect number of bitflips */
13888c2ecf20Sopenharmony_ci		marvell_nfc_hw_ecc_bch_read_chunk(chip, chunk, data, data_len,
13898c2ecf20Sopenharmony_ci						  spare, spare_len, page);
13908c2ecf20Sopenharmony_ci		ret = marvell_nfc_hw_ecc_check_bitflips(chip, &max_bitflips);
13918c2ecf20Sopenharmony_ci		if (ret)
13928c2ecf20Sopenharmony_ci			failure_mask |= BIT(chunk);
13938c2ecf20Sopenharmony_ci
13948c2ecf20Sopenharmony_ci		data += data_len;
13958c2ecf20Sopenharmony_ci		spare += spare_len;
13968c2ecf20Sopenharmony_ci	}
13978c2ecf20Sopenharmony_ci
13988c2ecf20Sopenharmony_ci	marvell_nfc_disable_hw_ecc(chip);
13998c2ecf20Sopenharmony_ci
14008c2ecf20Sopenharmony_ci	if (!failure_mask)
14018c2ecf20Sopenharmony_ci		return max_bitflips;
14028c2ecf20Sopenharmony_ci
14038c2ecf20Sopenharmony_ci	/*
14048c2ecf20Sopenharmony_ci	 * Please note that dumping the ECC bytes during a normal read with OOB
14058c2ecf20Sopenharmony_ci	 * area would add a significant overhead as ECC bytes are "consumed" by
14068c2ecf20Sopenharmony_ci	 * the controller in normal mode and must be re-read in raw mode. To
14078c2ecf20Sopenharmony_ci	 * avoid dropping the performances, we prefer not to include them. The
14088c2ecf20Sopenharmony_ci	 * user should re-read the page in raw mode if ECC bytes are required.
14098c2ecf20Sopenharmony_ci	 */
14108c2ecf20Sopenharmony_ci
14118c2ecf20Sopenharmony_ci	/*
14128c2ecf20Sopenharmony_ci	 * In case there is any subpage read error, we usually re-read only ECC
14138c2ecf20Sopenharmony_ci	 * bytes in raw mode and check if the whole page is empty. In this case,
14148c2ecf20Sopenharmony_ci	 * it is normal that the ECC check failed and we just ignore the error.
14158c2ecf20Sopenharmony_ci	 *
14168c2ecf20Sopenharmony_ci	 * However, it has been empirically observed that for some layouts (e.g
14178c2ecf20Sopenharmony_ci	 * 2k page, 8b strength per 512B chunk), the controller tries to correct
14188c2ecf20Sopenharmony_ci	 * bits and may create itself bitflips in the erased area. To overcome
14198c2ecf20Sopenharmony_ci	 * this strange behavior, the whole page is re-read in raw mode, not
14208c2ecf20Sopenharmony_ci	 * only the ECC bytes.
14218c2ecf20Sopenharmony_ci	 */
14228c2ecf20Sopenharmony_ci	for (chunk = 0; chunk < lt->nchunks; chunk++) {
14238c2ecf20Sopenharmony_ci		int data_off_in_page, spare_off_in_page, ecc_off_in_page;
14248c2ecf20Sopenharmony_ci		int data_off, spare_off, ecc_off;
14258c2ecf20Sopenharmony_ci		int data_len, spare_len, ecc_len;
14268c2ecf20Sopenharmony_ci
14278c2ecf20Sopenharmony_ci		/* No failure reported for this chunk, move to the next one */
14288c2ecf20Sopenharmony_ci		if (!(failure_mask & BIT(chunk)))
14298c2ecf20Sopenharmony_ci			continue;
14308c2ecf20Sopenharmony_ci
14318c2ecf20Sopenharmony_ci		data_off_in_page = chunk * (lt->data_bytes + lt->spare_bytes +
14328c2ecf20Sopenharmony_ci					    lt->ecc_bytes);
14338c2ecf20Sopenharmony_ci		spare_off_in_page = data_off_in_page +
14348c2ecf20Sopenharmony_ci			(chunk < lt->full_chunk_cnt ? lt->data_bytes :
14358c2ecf20Sopenharmony_ci						      lt->last_data_bytes);
14368c2ecf20Sopenharmony_ci		ecc_off_in_page = spare_off_in_page +
14378c2ecf20Sopenharmony_ci			(chunk < lt->full_chunk_cnt ? lt->spare_bytes :
14388c2ecf20Sopenharmony_ci						      lt->last_spare_bytes);
14398c2ecf20Sopenharmony_ci
14408c2ecf20Sopenharmony_ci		data_off = chunk * lt->data_bytes;
14418c2ecf20Sopenharmony_ci		spare_off = chunk * lt->spare_bytes;
14428c2ecf20Sopenharmony_ci		ecc_off = (lt->full_chunk_cnt * lt->spare_bytes) +
14438c2ecf20Sopenharmony_ci			  lt->last_spare_bytes +
14448c2ecf20Sopenharmony_ci			  (chunk * (lt->ecc_bytes + 2));
14458c2ecf20Sopenharmony_ci
14468c2ecf20Sopenharmony_ci		data_len = chunk < lt->full_chunk_cnt ? lt->data_bytes :
14478c2ecf20Sopenharmony_ci							lt->last_data_bytes;
14488c2ecf20Sopenharmony_ci		spare_len = chunk < lt->full_chunk_cnt ? lt->spare_bytes :
14498c2ecf20Sopenharmony_ci							 lt->last_spare_bytes;
14508c2ecf20Sopenharmony_ci		ecc_len = chunk < lt->full_chunk_cnt ? lt->ecc_bytes :
14518c2ecf20Sopenharmony_ci						       lt->last_ecc_bytes;
14528c2ecf20Sopenharmony_ci
14538c2ecf20Sopenharmony_ci		/*
14548c2ecf20Sopenharmony_ci		 * Only re-read the ECC bytes, unless we are using the 2k/8b
14558c2ecf20Sopenharmony_ci		 * layout which is buggy in the sense that the ECC engine will
14568c2ecf20Sopenharmony_ci		 * try to correct data bytes anyway, creating bitflips. In this
14578c2ecf20Sopenharmony_ci		 * case, re-read the entire page.
14588c2ecf20Sopenharmony_ci		 */
14598c2ecf20Sopenharmony_ci		if (lt->writesize == 2048 && lt->strength == 8) {
14608c2ecf20Sopenharmony_ci			nand_change_read_column_op(chip, data_off_in_page,
14618c2ecf20Sopenharmony_ci						   buf + data_off, data_len,
14628c2ecf20Sopenharmony_ci						   false);
14638c2ecf20Sopenharmony_ci			nand_change_read_column_op(chip, spare_off_in_page,
14648c2ecf20Sopenharmony_ci						   chip->oob_poi + spare_off, spare_len,
14658c2ecf20Sopenharmony_ci						   false);
14668c2ecf20Sopenharmony_ci		}
14678c2ecf20Sopenharmony_ci
14688c2ecf20Sopenharmony_ci		nand_change_read_column_op(chip, ecc_off_in_page,
14698c2ecf20Sopenharmony_ci					   chip->oob_poi + ecc_off, ecc_len,
14708c2ecf20Sopenharmony_ci					   false);
14718c2ecf20Sopenharmony_ci
14728c2ecf20Sopenharmony_ci		/* Check the entire chunk (data + spare + ecc) for emptyness */
14738c2ecf20Sopenharmony_ci		marvell_nfc_check_empty_chunk(chip, buf + data_off, data_len,
14748c2ecf20Sopenharmony_ci					      chip->oob_poi + spare_off, spare_len,
14758c2ecf20Sopenharmony_ci					      chip->oob_poi + ecc_off, ecc_len,
14768c2ecf20Sopenharmony_ci					      &max_bitflips);
14778c2ecf20Sopenharmony_ci	}
14788c2ecf20Sopenharmony_ci
14798c2ecf20Sopenharmony_ci	return max_bitflips;
14808c2ecf20Sopenharmony_ci}
14818c2ecf20Sopenharmony_ci
14828c2ecf20Sopenharmony_cistatic int marvell_nfc_hw_ecc_bch_read_oob_raw(struct nand_chip *chip, int page)
14838c2ecf20Sopenharmony_ci{
14848c2ecf20Sopenharmony_ci	u8 *buf = nand_get_data_buf(chip);
14858c2ecf20Sopenharmony_ci
14868c2ecf20Sopenharmony_ci	return chip->ecc.read_page_raw(chip, buf, true, page);
14878c2ecf20Sopenharmony_ci}
14888c2ecf20Sopenharmony_ci
14898c2ecf20Sopenharmony_cistatic int marvell_nfc_hw_ecc_bch_read_oob(struct nand_chip *chip, int page)
14908c2ecf20Sopenharmony_ci{
14918c2ecf20Sopenharmony_ci	u8 *buf = nand_get_data_buf(chip);
14928c2ecf20Sopenharmony_ci
14938c2ecf20Sopenharmony_ci	return chip->ecc.read_page(chip, buf, true, page);
14948c2ecf20Sopenharmony_ci}
14958c2ecf20Sopenharmony_ci
14968c2ecf20Sopenharmony_ci/* BCH write helpers */
14978c2ecf20Sopenharmony_cistatic int marvell_nfc_hw_ecc_bch_write_page_raw(struct nand_chip *chip,
14988c2ecf20Sopenharmony_ci						 const u8 *buf,
14998c2ecf20Sopenharmony_ci						 int oob_required, int page)
15008c2ecf20Sopenharmony_ci{
15018c2ecf20Sopenharmony_ci	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
15028c2ecf20Sopenharmony_ci	int full_chunk_size = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
15038c2ecf20Sopenharmony_ci	int data_len = lt->data_bytes;
15048c2ecf20Sopenharmony_ci	int spare_len = lt->spare_bytes;
15058c2ecf20Sopenharmony_ci	int ecc_len = lt->ecc_bytes;
15068c2ecf20Sopenharmony_ci	int spare_offset = 0;
15078c2ecf20Sopenharmony_ci	int ecc_offset = (lt->full_chunk_cnt * lt->spare_bytes) +
15088c2ecf20Sopenharmony_ci		lt->last_spare_bytes;
15098c2ecf20Sopenharmony_ci	int chunk;
15108c2ecf20Sopenharmony_ci
15118c2ecf20Sopenharmony_ci	marvell_nfc_select_target(chip, chip->cur_cs);
15128c2ecf20Sopenharmony_ci
15138c2ecf20Sopenharmony_ci	nand_prog_page_begin_op(chip, page, 0, NULL, 0);
15148c2ecf20Sopenharmony_ci
15158c2ecf20Sopenharmony_ci	for (chunk = 0; chunk < lt->nchunks; chunk++) {
15168c2ecf20Sopenharmony_ci		if (chunk >= lt->full_chunk_cnt) {
15178c2ecf20Sopenharmony_ci			data_len = lt->last_data_bytes;
15188c2ecf20Sopenharmony_ci			spare_len = lt->last_spare_bytes;
15198c2ecf20Sopenharmony_ci			ecc_len = lt->last_ecc_bytes;
15208c2ecf20Sopenharmony_ci		}
15218c2ecf20Sopenharmony_ci
15228c2ecf20Sopenharmony_ci		/* Point to the column of the next chunk */
15238c2ecf20Sopenharmony_ci		nand_change_write_column_op(chip, chunk * full_chunk_size,
15248c2ecf20Sopenharmony_ci					    NULL, 0, false);
15258c2ecf20Sopenharmony_ci
15268c2ecf20Sopenharmony_ci		/* Write the data */
15278c2ecf20Sopenharmony_ci		nand_write_data_op(chip, buf + (chunk * lt->data_bytes),
15288c2ecf20Sopenharmony_ci				   data_len, false);
15298c2ecf20Sopenharmony_ci
15308c2ecf20Sopenharmony_ci		if (!oob_required)
15318c2ecf20Sopenharmony_ci			continue;
15328c2ecf20Sopenharmony_ci
15338c2ecf20Sopenharmony_ci		/* Write the spare bytes */
15348c2ecf20Sopenharmony_ci		if (spare_len)
15358c2ecf20Sopenharmony_ci			nand_write_data_op(chip, chip->oob_poi + spare_offset,
15368c2ecf20Sopenharmony_ci					   spare_len, false);
15378c2ecf20Sopenharmony_ci
15388c2ecf20Sopenharmony_ci		/* Write the ECC bytes */
15398c2ecf20Sopenharmony_ci		if (ecc_len)
15408c2ecf20Sopenharmony_ci			nand_write_data_op(chip, chip->oob_poi + ecc_offset,
15418c2ecf20Sopenharmony_ci					   ecc_len, false);
15428c2ecf20Sopenharmony_ci
15438c2ecf20Sopenharmony_ci		spare_offset += spare_len;
15448c2ecf20Sopenharmony_ci		ecc_offset += ALIGN(ecc_len, 32);
15458c2ecf20Sopenharmony_ci	}
15468c2ecf20Sopenharmony_ci
15478c2ecf20Sopenharmony_ci	return nand_prog_page_end_op(chip);
15488c2ecf20Sopenharmony_ci}
15498c2ecf20Sopenharmony_ci
15508c2ecf20Sopenharmony_cistatic int
15518c2ecf20Sopenharmony_cimarvell_nfc_hw_ecc_bch_write_chunk(struct nand_chip *chip, int chunk,
15528c2ecf20Sopenharmony_ci				   const u8 *data, unsigned int data_len,
15538c2ecf20Sopenharmony_ci				   const u8 *spare, unsigned int spare_len,
15548c2ecf20Sopenharmony_ci				   int page)
15558c2ecf20Sopenharmony_ci{
15568c2ecf20Sopenharmony_ci	struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
15578c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
15588c2ecf20Sopenharmony_ci	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
15598c2ecf20Sopenharmony_ci	u32 xtype;
15608c2ecf20Sopenharmony_ci	int ret;
15618c2ecf20Sopenharmony_ci	struct marvell_nfc_op nfc_op = {
15628c2ecf20Sopenharmony_ci		.ndcb[0] = NDCB0_CMD_TYPE(TYPE_WRITE) | NDCB0_LEN_OVRD,
15638c2ecf20Sopenharmony_ci		.ndcb[3] = data_len + spare_len,
15648c2ecf20Sopenharmony_ci	};
15658c2ecf20Sopenharmony_ci
15668c2ecf20Sopenharmony_ci	/*
15678c2ecf20Sopenharmony_ci	 * First operation dispatches the CMD_SEQIN command, issue the address
15688c2ecf20Sopenharmony_ci	 * cycles and asks for the first chunk of data.
15698c2ecf20Sopenharmony_ci	 * All operations in the middle (if any) will issue a naked write and
15708c2ecf20Sopenharmony_ci	 * also ask for data.
15718c2ecf20Sopenharmony_ci	 * Last operation (if any) asks for the last chunk of data through a
15728c2ecf20Sopenharmony_ci	 * last naked write.
15738c2ecf20Sopenharmony_ci	 */
15748c2ecf20Sopenharmony_ci	if (chunk == 0) {
15758c2ecf20Sopenharmony_ci		if (lt->nchunks == 1)
15768c2ecf20Sopenharmony_ci			xtype = XTYPE_MONOLITHIC_RW;
15778c2ecf20Sopenharmony_ci		else
15788c2ecf20Sopenharmony_ci			xtype = XTYPE_WRITE_DISPATCH;
15798c2ecf20Sopenharmony_ci
15808c2ecf20Sopenharmony_ci		nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(xtype) |
15818c2ecf20Sopenharmony_ci				  NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
15828c2ecf20Sopenharmony_ci				  NDCB0_CMD1(NAND_CMD_SEQIN);
15838c2ecf20Sopenharmony_ci		nfc_op.ndcb[1] |= NDCB1_ADDRS_PAGE(page);
15848c2ecf20Sopenharmony_ci		nfc_op.ndcb[2] |= NDCB2_ADDR5_PAGE(page);
15858c2ecf20Sopenharmony_ci	} else if (chunk < lt->nchunks - 1) {
15868c2ecf20Sopenharmony_ci		nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_NAKED_RW);
15878c2ecf20Sopenharmony_ci	} else {
15888c2ecf20Sopenharmony_ci		nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
15898c2ecf20Sopenharmony_ci	}
15908c2ecf20Sopenharmony_ci
15918c2ecf20Sopenharmony_ci	/* Always dispatch the PAGEPROG command on the last chunk */
15928c2ecf20Sopenharmony_ci	if (chunk == lt->nchunks - 1)
15938c2ecf20Sopenharmony_ci		nfc_op.ndcb[0] |= NDCB0_CMD2(NAND_CMD_PAGEPROG) | NDCB0_DBC;
15948c2ecf20Sopenharmony_ci
15958c2ecf20Sopenharmony_ci	ret = marvell_nfc_prepare_cmd(chip);
15968c2ecf20Sopenharmony_ci	if (ret)
15978c2ecf20Sopenharmony_ci		return ret;
15988c2ecf20Sopenharmony_ci
15998c2ecf20Sopenharmony_ci	marvell_nfc_send_cmd(chip, &nfc_op);
16008c2ecf20Sopenharmony_ci	ret = marvell_nfc_end_cmd(chip, NDSR_WRDREQ,
16018c2ecf20Sopenharmony_ci				  "WRDREQ while loading FIFO (data)");
16028c2ecf20Sopenharmony_ci	if (ret)
16038c2ecf20Sopenharmony_ci		return ret;
16048c2ecf20Sopenharmony_ci
16058c2ecf20Sopenharmony_ci	/* Transfer the contents */
16068c2ecf20Sopenharmony_ci	iowrite32_rep(nfc->regs + NDDB, data, FIFO_REP(data_len));
16078c2ecf20Sopenharmony_ci	iowrite32_rep(nfc->regs + NDDB, spare, FIFO_REP(spare_len));
16088c2ecf20Sopenharmony_ci
16098c2ecf20Sopenharmony_ci	return 0;
16108c2ecf20Sopenharmony_ci}
16118c2ecf20Sopenharmony_ci
16128c2ecf20Sopenharmony_cistatic int marvell_nfc_hw_ecc_bch_write_page(struct nand_chip *chip,
16138c2ecf20Sopenharmony_ci					     const u8 *buf,
16148c2ecf20Sopenharmony_ci					     int oob_required, int page)
16158c2ecf20Sopenharmony_ci{
16168c2ecf20Sopenharmony_ci	const struct nand_sdr_timings *sdr =
16178c2ecf20Sopenharmony_ci		nand_get_sdr_timings(nand_get_interface_config(chip));
16188c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
16198c2ecf20Sopenharmony_ci	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
16208c2ecf20Sopenharmony_ci	const u8 *data = buf;
16218c2ecf20Sopenharmony_ci	const u8 *spare = chip->oob_poi;
16228c2ecf20Sopenharmony_ci	int data_len = lt->data_bytes;
16238c2ecf20Sopenharmony_ci	int spare_len = lt->spare_bytes;
16248c2ecf20Sopenharmony_ci	int chunk, ret;
16258c2ecf20Sopenharmony_ci	u8 status;
16268c2ecf20Sopenharmony_ci
16278c2ecf20Sopenharmony_ci	marvell_nfc_select_target(chip, chip->cur_cs);
16288c2ecf20Sopenharmony_ci
16298c2ecf20Sopenharmony_ci	/* Spare data will be written anyway, so clear it to avoid garbage */
16308c2ecf20Sopenharmony_ci	if (!oob_required)
16318c2ecf20Sopenharmony_ci		memset(chip->oob_poi, 0xFF, mtd->oobsize);
16328c2ecf20Sopenharmony_ci
16338c2ecf20Sopenharmony_ci	marvell_nfc_enable_hw_ecc(chip);
16348c2ecf20Sopenharmony_ci
16358c2ecf20Sopenharmony_ci	for (chunk = 0; chunk < lt->nchunks; chunk++) {
16368c2ecf20Sopenharmony_ci		if (chunk >= lt->full_chunk_cnt) {
16378c2ecf20Sopenharmony_ci			data_len = lt->last_data_bytes;
16388c2ecf20Sopenharmony_ci			spare_len = lt->last_spare_bytes;
16398c2ecf20Sopenharmony_ci		}
16408c2ecf20Sopenharmony_ci
16418c2ecf20Sopenharmony_ci		marvell_nfc_hw_ecc_bch_write_chunk(chip, chunk, data, data_len,
16428c2ecf20Sopenharmony_ci						   spare, spare_len, page);
16438c2ecf20Sopenharmony_ci		data += data_len;
16448c2ecf20Sopenharmony_ci		spare += spare_len;
16458c2ecf20Sopenharmony_ci
16468c2ecf20Sopenharmony_ci		/*
16478c2ecf20Sopenharmony_ci		 * Waiting only for CMDD or PAGED is not enough, ECC are
16488c2ecf20Sopenharmony_ci		 * partially written. No flag is set once the operation is
16498c2ecf20Sopenharmony_ci		 * really finished but the ND_RUN bit is cleared, so wait for it
16508c2ecf20Sopenharmony_ci		 * before stepping into the next command.
16518c2ecf20Sopenharmony_ci		 */
16528c2ecf20Sopenharmony_ci		marvell_nfc_wait_ndrun(chip);
16538c2ecf20Sopenharmony_ci	}
16548c2ecf20Sopenharmony_ci
16558c2ecf20Sopenharmony_ci	ret = marvell_nfc_wait_op(chip, PSEC_TO_MSEC(sdr->tPROG_max));
16568c2ecf20Sopenharmony_ci
16578c2ecf20Sopenharmony_ci	marvell_nfc_disable_hw_ecc(chip);
16588c2ecf20Sopenharmony_ci
16598c2ecf20Sopenharmony_ci	if (ret)
16608c2ecf20Sopenharmony_ci		return ret;
16618c2ecf20Sopenharmony_ci
16628c2ecf20Sopenharmony_ci	/* Check write status on the chip side */
16638c2ecf20Sopenharmony_ci	ret = nand_status_op(chip, &status);
16648c2ecf20Sopenharmony_ci	if (ret)
16658c2ecf20Sopenharmony_ci		return ret;
16668c2ecf20Sopenharmony_ci
16678c2ecf20Sopenharmony_ci	if (status & NAND_STATUS_FAIL)
16688c2ecf20Sopenharmony_ci		return -EIO;
16698c2ecf20Sopenharmony_ci
16708c2ecf20Sopenharmony_ci	return 0;
16718c2ecf20Sopenharmony_ci}
16728c2ecf20Sopenharmony_ci
16738c2ecf20Sopenharmony_cistatic int marvell_nfc_hw_ecc_bch_write_oob_raw(struct nand_chip *chip,
16748c2ecf20Sopenharmony_ci						int page)
16758c2ecf20Sopenharmony_ci{
16768c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
16778c2ecf20Sopenharmony_ci	u8 *buf = nand_get_data_buf(chip);
16788c2ecf20Sopenharmony_ci
16798c2ecf20Sopenharmony_ci	memset(buf, 0xFF, mtd->writesize);
16808c2ecf20Sopenharmony_ci
16818c2ecf20Sopenharmony_ci	return chip->ecc.write_page_raw(chip, buf, true, page);
16828c2ecf20Sopenharmony_ci}
16838c2ecf20Sopenharmony_ci
16848c2ecf20Sopenharmony_cistatic int marvell_nfc_hw_ecc_bch_write_oob(struct nand_chip *chip, int page)
16858c2ecf20Sopenharmony_ci{
16868c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
16878c2ecf20Sopenharmony_ci	u8 *buf = nand_get_data_buf(chip);
16888c2ecf20Sopenharmony_ci
16898c2ecf20Sopenharmony_ci	memset(buf, 0xFF, mtd->writesize);
16908c2ecf20Sopenharmony_ci
16918c2ecf20Sopenharmony_ci	return chip->ecc.write_page(chip, buf, true, page);
16928c2ecf20Sopenharmony_ci}
16938c2ecf20Sopenharmony_ci
16948c2ecf20Sopenharmony_ci/* NAND framework ->exec_op() hooks and related helpers */
16958c2ecf20Sopenharmony_cistatic void marvell_nfc_parse_instructions(struct nand_chip *chip,
16968c2ecf20Sopenharmony_ci					   const struct nand_subop *subop,
16978c2ecf20Sopenharmony_ci					   struct marvell_nfc_op *nfc_op)
16988c2ecf20Sopenharmony_ci{
16998c2ecf20Sopenharmony_ci	const struct nand_op_instr *instr = NULL;
17008c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
17018c2ecf20Sopenharmony_ci	bool first_cmd = true;
17028c2ecf20Sopenharmony_ci	unsigned int op_id;
17038c2ecf20Sopenharmony_ci	int i;
17048c2ecf20Sopenharmony_ci
17058c2ecf20Sopenharmony_ci	/* Reset the input structure as most of its fields will be OR'ed */
17068c2ecf20Sopenharmony_ci	memset(nfc_op, 0, sizeof(struct marvell_nfc_op));
17078c2ecf20Sopenharmony_ci
17088c2ecf20Sopenharmony_ci	for (op_id = 0; op_id < subop->ninstrs; op_id++) {
17098c2ecf20Sopenharmony_ci		unsigned int offset, naddrs;
17108c2ecf20Sopenharmony_ci		const u8 *addrs;
17118c2ecf20Sopenharmony_ci		int len;
17128c2ecf20Sopenharmony_ci
17138c2ecf20Sopenharmony_ci		instr = &subop->instrs[op_id];
17148c2ecf20Sopenharmony_ci
17158c2ecf20Sopenharmony_ci		switch (instr->type) {
17168c2ecf20Sopenharmony_ci		case NAND_OP_CMD_INSTR:
17178c2ecf20Sopenharmony_ci			if (first_cmd)
17188c2ecf20Sopenharmony_ci				nfc_op->ndcb[0] |=
17198c2ecf20Sopenharmony_ci					NDCB0_CMD1(instr->ctx.cmd.opcode);
17208c2ecf20Sopenharmony_ci			else
17218c2ecf20Sopenharmony_ci				nfc_op->ndcb[0] |=
17228c2ecf20Sopenharmony_ci					NDCB0_CMD2(instr->ctx.cmd.opcode) |
17238c2ecf20Sopenharmony_ci					NDCB0_DBC;
17248c2ecf20Sopenharmony_ci
17258c2ecf20Sopenharmony_ci			nfc_op->cle_ale_delay_ns = instr->delay_ns;
17268c2ecf20Sopenharmony_ci			first_cmd = false;
17278c2ecf20Sopenharmony_ci			break;
17288c2ecf20Sopenharmony_ci
17298c2ecf20Sopenharmony_ci		case NAND_OP_ADDR_INSTR:
17308c2ecf20Sopenharmony_ci			offset = nand_subop_get_addr_start_off(subop, op_id);
17318c2ecf20Sopenharmony_ci			naddrs = nand_subop_get_num_addr_cyc(subop, op_id);
17328c2ecf20Sopenharmony_ci			addrs = &instr->ctx.addr.addrs[offset];
17338c2ecf20Sopenharmony_ci
17348c2ecf20Sopenharmony_ci			nfc_op->ndcb[0] |= NDCB0_ADDR_CYC(naddrs);
17358c2ecf20Sopenharmony_ci
17368c2ecf20Sopenharmony_ci			for (i = 0; i < min_t(unsigned int, 4, naddrs); i++)
17378c2ecf20Sopenharmony_ci				nfc_op->ndcb[1] |= addrs[i] << (8 * i);
17388c2ecf20Sopenharmony_ci
17398c2ecf20Sopenharmony_ci			if (naddrs >= 5)
17408c2ecf20Sopenharmony_ci				nfc_op->ndcb[2] |= NDCB2_ADDR5_CYC(addrs[4]);
17418c2ecf20Sopenharmony_ci			if (naddrs >= 6)
17428c2ecf20Sopenharmony_ci				nfc_op->ndcb[3] |= NDCB3_ADDR6_CYC(addrs[5]);
17438c2ecf20Sopenharmony_ci			if (naddrs == 7)
17448c2ecf20Sopenharmony_ci				nfc_op->ndcb[3] |= NDCB3_ADDR7_CYC(addrs[6]);
17458c2ecf20Sopenharmony_ci
17468c2ecf20Sopenharmony_ci			nfc_op->cle_ale_delay_ns = instr->delay_ns;
17478c2ecf20Sopenharmony_ci			break;
17488c2ecf20Sopenharmony_ci
17498c2ecf20Sopenharmony_ci		case NAND_OP_DATA_IN_INSTR:
17508c2ecf20Sopenharmony_ci			nfc_op->data_instr = instr;
17518c2ecf20Sopenharmony_ci			nfc_op->data_instr_idx = op_id;
17528c2ecf20Sopenharmony_ci			nfc_op->ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ);
17538c2ecf20Sopenharmony_ci			if (nfc->caps->is_nfcv2) {
17548c2ecf20Sopenharmony_ci				nfc_op->ndcb[0] |=
17558c2ecf20Sopenharmony_ci					NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW) |
17568c2ecf20Sopenharmony_ci					NDCB0_LEN_OVRD;
17578c2ecf20Sopenharmony_ci				len = nand_subop_get_data_len(subop, op_id);
17588c2ecf20Sopenharmony_ci				nfc_op->ndcb[3] |= round_up(len, FIFO_DEPTH);
17598c2ecf20Sopenharmony_ci			}
17608c2ecf20Sopenharmony_ci			nfc_op->data_delay_ns = instr->delay_ns;
17618c2ecf20Sopenharmony_ci			break;
17628c2ecf20Sopenharmony_ci
17638c2ecf20Sopenharmony_ci		case NAND_OP_DATA_OUT_INSTR:
17648c2ecf20Sopenharmony_ci			nfc_op->data_instr = instr;
17658c2ecf20Sopenharmony_ci			nfc_op->data_instr_idx = op_id;
17668c2ecf20Sopenharmony_ci			nfc_op->ndcb[0] |= NDCB0_CMD_TYPE(TYPE_WRITE);
17678c2ecf20Sopenharmony_ci			if (nfc->caps->is_nfcv2) {
17688c2ecf20Sopenharmony_ci				nfc_op->ndcb[0] |=
17698c2ecf20Sopenharmony_ci					NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW) |
17708c2ecf20Sopenharmony_ci					NDCB0_LEN_OVRD;
17718c2ecf20Sopenharmony_ci				len = nand_subop_get_data_len(subop, op_id);
17728c2ecf20Sopenharmony_ci				nfc_op->ndcb[3] |= round_up(len, FIFO_DEPTH);
17738c2ecf20Sopenharmony_ci			}
17748c2ecf20Sopenharmony_ci			nfc_op->data_delay_ns = instr->delay_ns;
17758c2ecf20Sopenharmony_ci			break;
17768c2ecf20Sopenharmony_ci
17778c2ecf20Sopenharmony_ci		case NAND_OP_WAITRDY_INSTR:
17788c2ecf20Sopenharmony_ci			nfc_op->rdy_timeout_ms = instr->ctx.waitrdy.timeout_ms;
17798c2ecf20Sopenharmony_ci			nfc_op->rdy_delay_ns = instr->delay_ns;
17808c2ecf20Sopenharmony_ci			break;
17818c2ecf20Sopenharmony_ci		}
17828c2ecf20Sopenharmony_ci	}
17838c2ecf20Sopenharmony_ci}
17848c2ecf20Sopenharmony_ci
17858c2ecf20Sopenharmony_cistatic int marvell_nfc_xfer_data_pio(struct nand_chip *chip,
17868c2ecf20Sopenharmony_ci				     const struct nand_subop *subop,
17878c2ecf20Sopenharmony_ci				     struct marvell_nfc_op *nfc_op)
17888c2ecf20Sopenharmony_ci{
17898c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
17908c2ecf20Sopenharmony_ci	const struct nand_op_instr *instr = nfc_op->data_instr;
17918c2ecf20Sopenharmony_ci	unsigned int op_id = nfc_op->data_instr_idx;
17928c2ecf20Sopenharmony_ci	unsigned int len = nand_subop_get_data_len(subop, op_id);
17938c2ecf20Sopenharmony_ci	unsigned int offset = nand_subop_get_data_start_off(subop, op_id);
17948c2ecf20Sopenharmony_ci	bool reading = (instr->type == NAND_OP_DATA_IN_INSTR);
17958c2ecf20Sopenharmony_ci	int ret;
17968c2ecf20Sopenharmony_ci
17978c2ecf20Sopenharmony_ci	if (instr->ctx.data.force_8bit)
17988c2ecf20Sopenharmony_ci		marvell_nfc_force_byte_access(chip, true);
17998c2ecf20Sopenharmony_ci
18008c2ecf20Sopenharmony_ci	if (reading) {
18018c2ecf20Sopenharmony_ci		u8 *in = instr->ctx.data.buf.in + offset;
18028c2ecf20Sopenharmony_ci
18038c2ecf20Sopenharmony_ci		ret = marvell_nfc_xfer_data_in_pio(nfc, in, len);
18048c2ecf20Sopenharmony_ci	} else {
18058c2ecf20Sopenharmony_ci		const u8 *out = instr->ctx.data.buf.out + offset;
18068c2ecf20Sopenharmony_ci
18078c2ecf20Sopenharmony_ci		ret = marvell_nfc_xfer_data_out_pio(nfc, out, len);
18088c2ecf20Sopenharmony_ci	}
18098c2ecf20Sopenharmony_ci
18108c2ecf20Sopenharmony_ci	if (instr->ctx.data.force_8bit)
18118c2ecf20Sopenharmony_ci		marvell_nfc_force_byte_access(chip, false);
18128c2ecf20Sopenharmony_ci
18138c2ecf20Sopenharmony_ci	return ret;
18148c2ecf20Sopenharmony_ci}
18158c2ecf20Sopenharmony_ci
18168c2ecf20Sopenharmony_cistatic int marvell_nfc_monolithic_access_exec(struct nand_chip *chip,
18178c2ecf20Sopenharmony_ci					      const struct nand_subop *subop)
18188c2ecf20Sopenharmony_ci{
18198c2ecf20Sopenharmony_ci	struct marvell_nfc_op nfc_op;
18208c2ecf20Sopenharmony_ci	bool reading;
18218c2ecf20Sopenharmony_ci	int ret;
18228c2ecf20Sopenharmony_ci
18238c2ecf20Sopenharmony_ci	marvell_nfc_parse_instructions(chip, subop, &nfc_op);
18248c2ecf20Sopenharmony_ci	reading = (nfc_op.data_instr->type == NAND_OP_DATA_IN_INSTR);
18258c2ecf20Sopenharmony_ci
18268c2ecf20Sopenharmony_ci	ret = marvell_nfc_prepare_cmd(chip);
18278c2ecf20Sopenharmony_ci	if (ret)
18288c2ecf20Sopenharmony_ci		return ret;
18298c2ecf20Sopenharmony_ci
18308c2ecf20Sopenharmony_ci	marvell_nfc_send_cmd(chip, &nfc_op);
18318c2ecf20Sopenharmony_ci	ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ | NDSR_WRDREQ,
18328c2ecf20Sopenharmony_ci				  "RDDREQ/WRDREQ while draining raw data");
18338c2ecf20Sopenharmony_ci	if (ret)
18348c2ecf20Sopenharmony_ci		return ret;
18358c2ecf20Sopenharmony_ci
18368c2ecf20Sopenharmony_ci	cond_delay(nfc_op.cle_ale_delay_ns);
18378c2ecf20Sopenharmony_ci
18388c2ecf20Sopenharmony_ci	if (reading) {
18398c2ecf20Sopenharmony_ci		if (nfc_op.rdy_timeout_ms) {
18408c2ecf20Sopenharmony_ci			ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
18418c2ecf20Sopenharmony_ci			if (ret)
18428c2ecf20Sopenharmony_ci				return ret;
18438c2ecf20Sopenharmony_ci		}
18448c2ecf20Sopenharmony_ci
18458c2ecf20Sopenharmony_ci		cond_delay(nfc_op.rdy_delay_ns);
18468c2ecf20Sopenharmony_ci	}
18478c2ecf20Sopenharmony_ci
18488c2ecf20Sopenharmony_ci	marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
18498c2ecf20Sopenharmony_ci	ret = marvell_nfc_wait_cmdd(chip);
18508c2ecf20Sopenharmony_ci	if (ret)
18518c2ecf20Sopenharmony_ci		return ret;
18528c2ecf20Sopenharmony_ci
18538c2ecf20Sopenharmony_ci	cond_delay(nfc_op.data_delay_ns);
18548c2ecf20Sopenharmony_ci
18558c2ecf20Sopenharmony_ci	if (!reading) {
18568c2ecf20Sopenharmony_ci		if (nfc_op.rdy_timeout_ms) {
18578c2ecf20Sopenharmony_ci			ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
18588c2ecf20Sopenharmony_ci			if (ret)
18598c2ecf20Sopenharmony_ci				return ret;
18608c2ecf20Sopenharmony_ci		}
18618c2ecf20Sopenharmony_ci
18628c2ecf20Sopenharmony_ci		cond_delay(nfc_op.rdy_delay_ns);
18638c2ecf20Sopenharmony_ci	}
18648c2ecf20Sopenharmony_ci
18658c2ecf20Sopenharmony_ci	/*
18668c2ecf20Sopenharmony_ci	 * NDCR ND_RUN bit should be cleared automatically at the end of each
18678c2ecf20Sopenharmony_ci	 * operation but experience shows that the behavior is buggy when it
18688c2ecf20Sopenharmony_ci	 * comes to writes (with LEN_OVRD). Clear it by hand in this case.
18698c2ecf20Sopenharmony_ci	 */
18708c2ecf20Sopenharmony_ci	if (!reading) {
18718c2ecf20Sopenharmony_ci		struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
18728c2ecf20Sopenharmony_ci
18738c2ecf20Sopenharmony_ci		writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
18748c2ecf20Sopenharmony_ci			       nfc->regs + NDCR);
18758c2ecf20Sopenharmony_ci	}
18768c2ecf20Sopenharmony_ci
18778c2ecf20Sopenharmony_ci	return 0;
18788c2ecf20Sopenharmony_ci}
18798c2ecf20Sopenharmony_ci
18808c2ecf20Sopenharmony_cistatic int marvell_nfc_naked_access_exec(struct nand_chip *chip,
18818c2ecf20Sopenharmony_ci					 const struct nand_subop *subop)
18828c2ecf20Sopenharmony_ci{
18838c2ecf20Sopenharmony_ci	struct marvell_nfc_op nfc_op;
18848c2ecf20Sopenharmony_ci	int ret;
18858c2ecf20Sopenharmony_ci
18868c2ecf20Sopenharmony_ci	marvell_nfc_parse_instructions(chip, subop, &nfc_op);
18878c2ecf20Sopenharmony_ci
18888c2ecf20Sopenharmony_ci	/*
18898c2ecf20Sopenharmony_ci	 * Naked access are different in that they need to be flagged as naked
18908c2ecf20Sopenharmony_ci	 * by the controller. Reset the controller registers fields that inform
18918c2ecf20Sopenharmony_ci	 * on the type and refill them according to the ongoing operation.
18928c2ecf20Sopenharmony_ci	 */
18938c2ecf20Sopenharmony_ci	nfc_op.ndcb[0] &= ~(NDCB0_CMD_TYPE(TYPE_MASK) |
18948c2ecf20Sopenharmony_ci			    NDCB0_CMD_XTYPE(XTYPE_MASK));
18958c2ecf20Sopenharmony_ci	switch (subop->instrs[0].type) {
18968c2ecf20Sopenharmony_ci	case NAND_OP_CMD_INSTR:
18978c2ecf20Sopenharmony_ci		nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_CMD);
18988c2ecf20Sopenharmony_ci		break;
18998c2ecf20Sopenharmony_ci	case NAND_OP_ADDR_INSTR:
19008c2ecf20Sopenharmony_ci		nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_ADDR);
19018c2ecf20Sopenharmony_ci		break;
19028c2ecf20Sopenharmony_ci	case NAND_OP_DATA_IN_INSTR:
19038c2ecf20Sopenharmony_ci		nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ) |
19048c2ecf20Sopenharmony_ci				  NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
19058c2ecf20Sopenharmony_ci		break;
19068c2ecf20Sopenharmony_ci	case NAND_OP_DATA_OUT_INSTR:
19078c2ecf20Sopenharmony_ci		nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_WRITE) |
19088c2ecf20Sopenharmony_ci				  NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
19098c2ecf20Sopenharmony_ci		break;
19108c2ecf20Sopenharmony_ci	default:
19118c2ecf20Sopenharmony_ci		/* This should never happen */
19128c2ecf20Sopenharmony_ci		break;
19138c2ecf20Sopenharmony_ci	}
19148c2ecf20Sopenharmony_ci
19158c2ecf20Sopenharmony_ci	ret = marvell_nfc_prepare_cmd(chip);
19168c2ecf20Sopenharmony_ci	if (ret)
19178c2ecf20Sopenharmony_ci		return ret;
19188c2ecf20Sopenharmony_ci
19198c2ecf20Sopenharmony_ci	marvell_nfc_send_cmd(chip, &nfc_op);
19208c2ecf20Sopenharmony_ci
19218c2ecf20Sopenharmony_ci	if (!nfc_op.data_instr) {
19228c2ecf20Sopenharmony_ci		ret = marvell_nfc_wait_cmdd(chip);
19238c2ecf20Sopenharmony_ci		cond_delay(nfc_op.cle_ale_delay_ns);
19248c2ecf20Sopenharmony_ci		return ret;
19258c2ecf20Sopenharmony_ci	}
19268c2ecf20Sopenharmony_ci
19278c2ecf20Sopenharmony_ci	ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ | NDSR_WRDREQ,
19288c2ecf20Sopenharmony_ci				  "RDDREQ/WRDREQ while draining raw data");
19298c2ecf20Sopenharmony_ci	if (ret)
19308c2ecf20Sopenharmony_ci		return ret;
19318c2ecf20Sopenharmony_ci
19328c2ecf20Sopenharmony_ci	marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
19338c2ecf20Sopenharmony_ci	ret = marvell_nfc_wait_cmdd(chip);
19348c2ecf20Sopenharmony_ci	if (ret)
19358c2ecf20Sopenharmony_ci		return ret;
19368c2ecf20Sopenharmony_ci
19378c2ecf20Sopenharmony_ci	/*
19388c2ecf20Sopenharmony_ci	 * NDCR ND_RUN bit should be cleared automatically at the end of each
19398c2ecf20Sopenharmony_ci	 * operation but experience shows that the behavior is buggy when it
19408c2ecf20Sopenharmony_ci	 * comes to writes (with LEN_OVRD). Clear it by hand in this case.
19418c2ecf20Sopenharmony_ci	 */
19428c2ecf20Sopenharmony_ci	if (subop->instrs[0].type == NAND_OP_DATA_OUT_INSTR) {
19438c2ecf20Sopenharmony_ci		struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
19448c2ecf20Sopenharmony_ci
19458c2ecf20Sopenharmony_ci		writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
19468c2ecf20Sopenharmony_ci			       nfc->regs + NDCR);
19478c2ecf20Sopenharmony_ci	}
19488c2ecf20Sopenharmony_ci
19498c2ecf20Sopenharmony_ci	return 0;
19508c2ecf20Sopenharmony_ci}
19518c2ecf20Sopenharmony_ci
19528c2ecf20Sopenharmony_cistatic int marvell_nfc_naked_waitrdy_exec(struct nand_chip *chip,
19538c2ecf20Sopenharmony_ci					  const struct nand_subop *subop)
19548c2ecf20Sopenharmony_ci{
19558c2ecf20Sopenharmony_ci	struct marvell_nfc_op nfc_op;
19568c2ecf20Sopenharmony_ci	int ret;
19578c2ecf20Sopenharmony_ci
19588c2ecf20Sopenharmony_ci	marvell_nfc_parse_instructions(chip, subop, &nfc_op);
19598c2ecf20Sopenharmony_ci
19608c2ecf20Sopenharmony_ci	ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
19618c2ecf20Sopenharmony_ci	cond_delay(nfc_op.rdy_delay_ns);
19628c2ecf20Sopenharmony_ci
19638c2ecf20Sopenharmony_ci	return ret;
19648c2ecf20Sopenharmony_ci}
19658c2ecf20Sopenharmony_ci
19668c2ecf20Sopenharmony_cistatic int marvell_nfc_read_id_type_exec(struct nand_chip *chip,
19678c2ecf20Sopenharmony_ci					 const struct nand_subop *subop)
19688c2ecf20Sopenharmony_ci{
19698c2ecf20Sopenharmony_ci	struct marvell_nfc_op nfc_op;
19708c2ecf20Sopenharmony_ci	int ret;
19718c2ecf20Sopenharmony_ci
19728c2ecf20Sopenharmony_ci	marvell_nfc_parse_instructions(chip, subop, &nfc_op);
19738c2ecf20Sopenharmony_ci	nfc_op.ndcb[0] &= ~NDCB0_CMD_TYPE(TYPE_READ);
19748c2ecf20Sopenharmony_ci	nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ_ID);
19758c2ecf20Sopenharmony_ci
19768c2ecf20Sopenharmony_ci	ret = marvell_nfc_prepare_cmd(chip);
19778c2ecf20Sopenharmony_ci	if (ret)
19788c2ecf20Sopenharmony_ci		return ret;
19798c2ecf20Sopenharmony_ci
19808c2ecf20Sopenharmony_ci	marvell_nfc_send_cmd(chip, &nfc_op);
19818c2ecf20Sopenharmony_ci	ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
19828c2ecf20Sopenharmony_ci				  "RDDREQ while reading ID");
19838c2ecf20Sopenharmony_ci	if (ret)
19848c2ecf20Sopenharmony_ci		return ret;
19858c2ecf20Sopenharmony_ci
19868c2ecf20Sopenharmony_ci	cond_delay(nfc_op.cle_ale_delay_ns);
19878c2ecf20Sopenharmony_ci
19888c2ecf20Sopenharmony_ci	if (nfc_op.rdy_timeout_ms) {
19898c2ecf20Sopenharmony_ci		ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
19908c2ecf20Sopenharmony_ci		if (ret)
19918c2ecf20Sopenharmony_ci			return ret;
19928c2ecf20Sopenharmony_ci	}
19938c2ecf20Sopenharmony_ci
19948c2ecf20Sopenharmony_ci	cond_delay(nfc_op.rdy_delay_ns);
19958c2ecf20Sopenharmony_ci
19968c2ecf20Sopenharmony_ci	marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
19978c2ecf20Sopenharmony_ci	ret = marvell_nfc_wait_cmdd(chip);
19988c2ecf20Sopenharmony_ci	if (ret)
19998c2ecf20Sopenharmony_ci		return ret;
20008c2ecf20Sopenharmony_ci
20018c2ecf20Sopenharmony_ci	cond_delay(nfc_op.data_delay_ns);
20028c2ecf20Sopenharmony_ci
20038c2ecf20Sopenharmony_ci	return 0;
20048c2ecf20Sopenharmony_ci}
20058c2ecf20Sopenharmony_ci
20068c2ecf20Sopenharmony_cistatic int marvell_nfc_read_status_exec(struct nand_chip *chip,
20078c2ecf20Sopenharmony_ci					const struct nand_subop *subop)
20088c2ecf20Sopenharmony_ci{
20098c2ecf20Sopenharmony_ci	struct marvell_nfc_op nfc_op;
20108c2ecf20Sopenharmony_ci	int ret;
20118c2ecf20Sopenharmony_ci
20128c2ecf20Sopenharmony_ci	marvell_nfc_parse_instructions(chip, subop, &nfc_op);
20138c2ecf20Sopenharmony_ci	nfc_op.ndcb[0] &= ~NDCB0_CMD_TYPE(TYPE_READ);
20148c2ecf20Sopenharmony_ci	nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_STATUS);
20158c2ecf20Sopenharmony_ci
20168c2ecf20Sopenharmony_ci	ret = marvell_nfc_prepare_cmd(chip);
20178c2ecf20Sopenharmony_ci	if (ret)
20188c2ecf20Sopenharmony_ci		return ret;
20198c2ecf20Sopenharmony_ci
20208c2ecf20Sopenharmony_ci	marvell_nfc_send_cmd(chip, &nfc_op);
20218c2ecf20Sopenharmony_ci	ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
20228c2ecf20Sopenharmony_ci				  "RDDREQ while reading status");
20238c2ecf20Sopenharmony_ci	if (ret)
20248c2ecf20Sopenharmony_ci		return ret;
20258c2ecf20Sopenharmony_ci
20268c2ecf20Sopenharmony_ci	cond_delay(nfc_op.cle_ale_delay_ns);
20278c2ecf20Sopenharmony_ci
20288c2ecf20Sopenharmony_ci	if (nfc_op.rdy_timeout_ms) {
20298c2ecf20Sopenharmony_ci		ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
20308c2ecf20Sopenharmony_ci		if (ret)
20318c2ecf20Sopenharmony_ci			return ret;
20328c2ecf20Sopenharmony_ci	}
20338c2ecf20Sopenharmony_ci
20348c2ecf20Sopenharmony_ci	cond_delay(nfc_op.rdy_delay_ns);
20358c2ecf20Sopenharmony_ci
20368c2ecf20Sopenharmony_ci	marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
20378c2ecf20Sopenharmony_ci	ret = marvell_nfc_wait_cmdd(chip);
20388c2ecf20Sopenharmony_ci	if (ret)
20398c2ecf20Sopenharmony_ci		return ret;
20408c2ecf20Sopenharmony_ci
20418c2ecf20Sopenharmony_ci	cond_delay(nfc_op.data_delay_ns);
20428c2ecf20Sopenharmony_ci
20438c2ecf20Sopenharmony_ci	return 0;
20448c2ecf20Sopenharmony_ci}
20458c2ecf20Sopenharmony_ci
20468c2ecf20Sopenharmony_cistatic int marvell_nfc_reset_cmd_type_exec(struct nand_chip *chip,
20478c2ecf20Sopenharmony_ci					   const struct nand_subop *subop)
20488c2ecf20Sopenharmony_ci{
20498c2ecf20Sopenharmony_ci	struct marvell_nfc_op nfc_op;
20508c2ecf20Sopenharmony_ci	int ret;
20518c2ecf20Sopenharmony_ci
20528c2ecf20Sopenharmony_ci	marvell_nfc_parse_instructions(chip, subop, &nfc_op);
20538c2ecf20Sopenharmony_ci	nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_RESET);
20548c2ecf20Sopenharmony_ci
20558c2ecf20Sopenharmony_ci	ret = marvell_nfc_prepare_cmd(chip);
20568c2ecf20Sopenharmony_ci	if (ret)
20578c2ecf20Sopenharmony_ci		return ret;
20588c2ecf20Sopenharmony_ci
20598c2ecf20Sopenharmony_ci	marvell_nfc_send_cmd(chip, &nfc_op);
20608c2ecf20Sopenharmony_ci	ret = marvell_nfc_wait_cmdd(chip);
20618c2ecf20Sopenharmony_ci	if (ret)
20628c2ecf20Sopenharmony_ci		return ret;
20638c2ecf20Sopenharmony_ci
20648c2ecf20Sopenharmony_ci	cond_delay(nfc_op.cle_ale_delay_ns);
20658c2ecf20Sopenharmony_ci
20668c2ecf20Sopenharmony_ci	ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
20678c2ecf20Sopenharmony_ci	if (ret)
20688c2ecf20Sopenharmony_ci		return ret;
20698c2ecf20Sopenharmony_ci
20708c2ecf20Sopenharmony_ci	cond_delay(nfc_op.rdy_delay_ns);
20718c2ecf20Sopenharmony_ci
20728c2ecf20Sopenharmony_ci	return 0;
20738c2ecf20Sopenharmony_ci}
20748c2ecf20Sopenharmony_ci
20758c2ecf20Sopenharmony_cistatic int marvell_nfc_erase_cmd_type_exec(struct nand_chip *chip,
20768c2ecf20Sopenharmony_ci					   const struct nand_subop *subop)
20778c2ecf20Sopenharmony_ci{
20788c2ecf20Sopenharmony_ci	struct marvell_nfc_op nfc_op;
20798c2ecf20Sopenharmony_ci	int ret;
20808c2ecf20Sopenharmony_ci
20818c2ecf20Sopenharmony_ci	marvell_nfc_parse_instructions(chip, subop, &nfc_op);
20828c2ecf20Sopenharmony_ci	nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_ERASE);
20838c2ecf20Sopenharmony_ci
20848c2ecf20Sopenharmony_ci	ret = marvell_nfc_prepare_cmd(chip);
20858c2ecf20Sopenharmony_ci	if (ret)
20868c2ecf20Sopenharmony_ci		return ret;
20878c2ecf20Sopenharmony_ci
20888c2ecf20Sopenharmony_ci	marvell_nfc_send_cmd(chip, &nfc_op);
20898c2ecf20Sopenharmony_ci	ret = marvell_nfc_wait_cmdd(chip);
20908c2ecf20Sopenharmony_ci	if (ret)
20918c2ecf20Sopenharmony_ci		return ret;
20928c2ecf20Sopenharmony_ci
20938c2ecf20Sopenharmony_ci	cond_delay(nfc_op.cle_ale_delay_ns);
20948c2ecf20Sopenharmony_ci
20958c2ecf20Sopenharmony_ci	ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
20968c2ecf20Sopenharmony_ci	if (ret)
20978c2ecf20Sopenharmony_ci		return ret;
20988c2ecf20Sopenharmony_ci
20998c2ecf20Sopenharmony_ci	cond_delay(nfc_op.rdy_delay_ns);
21008c2ecf20Sopenharmony_ci
21018c2ecf20Sopenharmony_ci	return 0;
21028c2ecf20Sopenharmony_ci}
21038c2ecf20Sopenharmony_ci
21048c2ecf20Sopenharmony_cistatic const struct nand_op_parser marvell_nfcv2_op_parser = NAND_OP_PARSER(
21058c2ecf20Sopenharmony_ci	/* Monolithic reads/writes */
21068c2ecf20Sopenharmony_ci	NAND_OP_PARSER_PATTERN(
21078c2ecf20Sopenharmony_ci		marvell_nfc_monolithic_access_exec,
21088c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_CMD_ELEM(false),
21098c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_ADDR_ELEM(true, MAX_ADDRESS_CYC_NFCV2),
21108c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_CMD_ELEM(true),
21118c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
21128c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE)),
21138c2ecf20Sopenharmony_ci	NAND_OP_PARSER_PATTERN(
21148c2ecf20Sopenharmony_ci		marvell_nfc_monolithic_access_exec,
21158c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_CMD_ELEM(false),
21168c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2),
21178c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE),
21188c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_CMD_ELEM(true),
21198c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_WAITRDY_ELEM(true)),
21208c2ecf20Sopenharmony_ci	/* Naked commands */
21218c2ecf20Sopenharmony_ci	NAND_OP_PARSER_PATTERN(
21228c2ecf20Sopenharmony_ci		marvell_nfc_naked_access_exec,
21238c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_CMD_ELEM(false)),
21248c2ecf20Sopenharmony_ci	NAND_OP_PARSER_PATTERN(
21258c2ecf20Sopenharmony_ci		marvell_nfc_naked_access_exec,
21268c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2)),
21278c2ecf20Sopenharmony_ci	NAND_OP_PARSER_PATTERN(
21288c2ecf20Sopenharmony_ci		marvell_nfc_naked_access_exec,
21298c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE)),
21308c2ecf20Sopenharmony_ci	NAND_OP_PARSER_PATTERN(
21318c2ecf20Sopenharmony_ci		marvell_nfc_naked_access_exec,
21328c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE)),
21338c2ecf20Sopenharmony_ci	NAND_OP_PARSER_PATTERN(
21348c2ecf20Sopenharmony_ci		marvell_nfc_naked_waitrdy_exec,
21358c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
21368c2ecf20Sopenharmony_ci	);
21378c2ecf20Sopenharmony_ci
21388c2ecf20Sopenharmony_cistatic const struct nand_op_parser marvell_nfcv1_op_parser = NAND_OP_PARSER(
21398c2ecf20Sopenharmony_ci	/* Naked commands not supported, use a function for each pattern */
21408c2ecf20Sopenharmony_ci	NAND_OP_PARSER_PATTERN(
21418c2ecf20Sopenharmony_ci		marvell_nfc_read_id_type_exec,
21428c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_CMD_ELEM(false),
21438c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV1),
21448c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 8)),
21458c2ecf20Sopenharmony_ci	NAND_OP_PARSER_PATTERN(
21468c2ecf20Sopenharmony_ci		marvell_nfc_erase_cmd_type_exec,
21478c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_CMD_ELEM(false),
21488c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV1),
21498c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_CMD_ELEM(false),
21508c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
21518c2ecf20Sopenharmony_ci	NAND_OP_PARSER_PATTERN(
21528c2ecf20Sopenharmony_ci		marvell_nfc_read_status_exec,
21538c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_CMD_ELEM(false),
21548c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 1)),
21558c2ecf20Sopenharmony_ci	NAND_OP_PARSER_PATTERN(
21568c2ecf20Sopenharmony_ci		marvell_nfc_reset_cmd_type_exec,
21578c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_CMD_ELEM(false),
21588c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
21598c2ecf20Sopenharmony_ci	NAND_OP_PARSER_PATTERN(
21608c2ecf20Sopenharmony_ci		marvell_nfc_naked_waitrdy_exec,
21618c2ecf20Sopenharmony_ci		NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
21628c2ecf20Sopenharmony_ci	);
21638c2ecf20Sopenharmony_ci
21648c2ecf20Sopenharmony_cistatic int marvell_nfc_exec_op(struct nand_chip *chip,
21658c2ecf20Sopenharmony_ci			       const struct nand_operation *op,
21668c2ecf20Sopenharmony_ci			       bool check_only)
21678c2ecf20Sopenharmony_ci{
21688c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
21698c2ecf20Sopenharmony_ci
21708c2ecf20Sopenharmony_ci	if (!check_only)
21718c2ecf20Sopenharmony_ci		marvell_nfc_select_target(chip, op->cs);
21728c2ecf20Sopenharmony_ci
21738c2ecf20Sopenharmony_ci	if (nfc->caps->is_nfcv2)
21748c2ecf20Sopenharmony_ci		return nand_op_parser_exec_op(chip, &marvell_nfcv2_op_parser,
21758c2ecf20Sopenharmony_ci					      op, check_only);
21768c2ecf20Sopenharmony_ci	else
21778c2ecf20Sopenharmony_ci		return nand_op_parser_exec_op(chip, &marvell_nfcv1_op_parser,
21788c2ecf20Sopenharmony_ci					      op, check_only);
21798c2ecf20Sopenharmony_ci}
21808c2ecf20Sopenharmony_ci
21818c2ecf20Sopenharmony_ci/*
21828c2ecf20Sopenharmony_ci * Layouts were broken in old pxa3xx_nand driver, these are supposed to be
21838c2ecf20Sopenharmony_ci * usable.
21848c2ecf20Sopenharmony_ci */
21858c2ecf20Sopenharmony_cistatic int marvell_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
21868c2ecf20Sopenharmony_ci				      struct mtd_oob_region *oobregion)
21878c2ecf20Sopenharmony_ci{
21888c2ecf20Sopenharmony_ci	struct nand_chip *chip = mtd_to_nand(mtd);
21898c2ecf20Sopenharmony_ci	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
21908c2ecf20Sopenharmony_ci
21918c2ecf20Sopenharmony_ci	if (section)
21928c2ecf20Sopenharmony_ci		return -ERANGE;
21938c2ecf20Sopenharmony_ci
21948c2ecf20Sopenharmony_ci	oobregion->length = (lt->full_chunk_cnt * lt->ecc_bytes) +
21958c2ecf20Sopenharmony_ci			    lt->last_ecc_bytes;
21968c2ecf20Sopenharmony_ci	oobregion->offset = mtd->oobsize - oobregion->length;
21978c2ecf20Sopenharmony_ci
21988c2ecf20Sopenharmony_ci	return 0;
21998c2ecf20Sopenharmony_ci}
22008c2ecf20Sopenharmony_ci
22018c2ecf20Sopenharmony_cistatic int marvell_nand_ooblayout_free(struct mtd_info *mtd, int section,
22028c2ecf20Sopenharmony_ci				       struct mtd_oob_region *oobregion)
22038c2ecf20Sopenharmony_ci{
22048c2ecf20Sopenharmony_ci	struct nand_chip *chip = mtd_to_nand(mtd);
22058c2ecf20Sopenharmony_ci	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
22068c2ecf20Sopenharmony_ci
22078c2ecf20Sopenharmony_ci	if (section)
22088c2ecf20Sopenharmony_ci		return -ERANGE;
22098c2ecf20Sopenharmony_ci
22108c2ecf20Sopenharmony_ci	/*
22118c2ecf20Sopenharmony_ci	 * Bootrom looks in bytes 0 & 5 for bad blocks for the
22128c2ecf20Sopenharmony_ci	 * 4KB page / 4bit BCH combination.
22138c2ecf20Sopenharmony_ci	 */
22148c2ecf20Sopenharmony_ci	if (mtd->writesize == SZ_4K && lt->data_bytes == SZ_2K)
22158c2ecf20Sopenharmony_ci		oobregion->offset = 6;
22168c2ecf20Sopenharmony_ci	else
22178c2ecf20Sopenharmony_ci		oobregion->offset = 2;
22188c2ecf20Sopenharmony_ci
22198c2ecf20Sopenharmony_ci	oobregion->length = (lt->full_chunk_cnt * lt->spare_bytes) +
22208c2ecf20Sopenharmony_ci			    lt->last_spare_bytes - oobregion->offset;
22218c2ecf20Sopenharmony_ci
22228c2ecf20Sopenharmony_ci	return 0;
22238c2ecf20Sopenharmony_ci}
22248c2ecf20Sopenharmony_ci
22258c2ecf20Sopenharmony_cistatic const struct mtd_ooblayout_ops marvell_nand_ooblayout_ops = {
22268c2ecf20Sopenharmony_ci	.ecc = marvell_nand_ooblayout_ecc,
22278c2ecf20Sopenharmony_ci	.free = marvell_nand_ooblayout_free,
22288c2ecf20Sopenharmony_ci};
22298c2ecf20Sopenharmony_ci
22308c2ecf20Sopenharmony_cistatic int marvell_nand_hw_ecc_controller_init(struct mtd_info *mtd,
22318c2ecf20Sopenharmony_ci					       struct nand_ecc_ctrl *ecc)
22328c2ecf20Sopenharmony_ci{
22338c2ecf20Sopenharmony_ci	struct nand_chip *chip = mtd_to_nand(mtd);
22348c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
22358c2ecf20Sopenharmony_ci	const struct marvell_hw_ecc_layout *l;
22368c2ecf20Sopenharmony_ci	int i;
22378c2ecf20Sopenharmony_ci
22388c2ecf20Sopenharmony_ci	if (!nfc->caps->is_nfcv2 &&
22398c2ecf20Sopenharmony_ci	    (mtd->writesize + mtd->oobsize > MAX_CHUNK_SIZE)) {
22408c2ecf20Sopenharmony_ci		dev_err(nfc->dev,
22418c2ecf20Sopenharmony_ci			"NFCv1: writesize (%d) cannot be bigger than a chunk (%d)\n",
22428c2ecf20Sopenharmony_ci			mtd->writesize, MAX_CHUNK_SIZE - mtd->oobsize);
22438c2ecf20Sopenharmony_ci		return -ENOTSUPP;
22448c2ecf20Sopenharmony_ci	}
22458c2ecf20Sopenharmony_ci
22468c2ecf20Sopenharmony_ci	to_marvell_nand(chip)->layout = NULL;
22478c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(marvell_nfc_layouts); i++) {
22488c2ecf20Sopenharmony_ci		l = &marvell_nfc_layouts[i];
22498c2ecf20Sopenharmony_ci		if (mtd->writesize == l->writesize &&
22508c2ecf20Sopenharmony_ci		    ecc->size == l->chunk && ecc->strength == l->strength) {
22518c2ecf20Sopenharmony_ci			to_marvell_nand(chip)->layout = l;
22528c2ecf20Sopenharmony_ci			break;
22538c2ecf20Sopenharmony_ci		}
22548c2ecf20Sopenharmony_ci	}
22558c2ecf20Sopenharmony_ci
22568c2ecf20Sopenharmony_ci	if (!to_marvell_nand(chip)->layout ||
22578c2ecf20Sopenharmony_ci	    (!nfc->caps->is_nfcv2 && ecc->strength > 1)) {
22588c2ecf20Sopenharmony_ci		dev_err(nfc->dev,
22598c2ecf20Sopenharmony_ci			"ECC strength %d at page size %d is not supported\n",
22608c2ecf20Sopenharmony_ci			ecc->strength, mtd->writesize);
22618c2ecf20Sopenharmony_ci		return -ENOTSUPP;
22628c2ecf20Sopenharmony_ci	}
22638c2ecf20Sopenharmony_ci
22648c2ecf20Sopenharmony_ci	/* Special care for the layout 2k/8-bit/512B  */
22658c2ecf20Sopenharmony_ci	if (l->writesize == 2048 && l->strength == 8) {
22668c2ecf20Sopenharmony_ci		if (mtd->oobsize < 128) {
22678c2ecf20Sopenharmony_ci			dev_err(nfc->dev, "Requested layout needs at least 128 OOB bytes\n");
22688c2ecf20Sopenharmony_ci			return -ENOTSUPP;
22698c2ecf20Sopenharmony_ci		} else {
22708c2ecf20Sopenharmony_ci			chip->bbt_options |= NAND_BBT_NO_OOB_BBM;
22718c2ecf20Sopenharmony_ci		}
22728c2ecf20Sopenharmony_ci	}
22738c2ecf20Sopenharmony_ci
22748c2ecf20Sopenharmony_ci	mtd_set_ooblayout(mtd, &marvell_nand_ooblayout_ops);
22758c2ecf20Sopenharmony_ci	ecc->steps = l->nchunks;
22768c2ecf20Sopenharmony_ci	ecc->size = l->data_bytes;
22778c2ecf20Sopenharmony_ci
22788c2ecf20Sopenharmony_ci	if (ecc->strength == 1) {
22798c2ecf20Sopenharmony_ci		chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
22808c2ecf20Sopenharmony_ci		ecc->read_page_raw = marvell_nfc_hw_ecc_hmg_read_page_raw;
22818c2ecf20Sopenharmony_ci		ecc->read_page = marvell_nfc_hw_ecc_hmg_read_page;
22828c2ecf20Sopenharmony_ci		ecc->read_oob_raw = marvell_nfc_hw_ecc_hmg_read_oob_raw;
22838c2ecf20Sopenharmony_ci		ecc->read_oob = ecc->read_oob_raw;
22848c2ecf20Sopenharmony_ci		ecc->write_page_raw = marvell_nfc_hw_ecc_hmg_write_page_raw;
22858c2ecf20Sopenharmony_ci		ecc->write_page = marvell_nfc_hw_ecc_hmg_write_page;
22868c2ecf20Sopenharmony_ci		ecc->write_oob_raw = marvell_nfc_hw_ecc_hmg_write_oob_raw;
22878c2ecf20Sopenharmony_ci		ecc->write_oob = ecc->write_oob_raw;
22888c2ecf20Sopenharmony_ci	} else {
22898c2ecf20Sopenharmony_ci		chip->ecc.algo = NAND_ECC_ALGO_BCH;
22908c2ecf20Sopenharmony_ci		ecc->strength = 16;
22918c2ecf20Sopenharmony_ci		ecc->read_page_raw = marvell_nfc_hw_ecc_bch_read_page_raw;
22928c2ecf20Sopenharmony_ci		ecc->read_page = marvell_nfc_hw_ecc_bch_read_page;
22938c2ecf20Sopenharmony_ci		ecc->read_oob_raw = marvell_nfc_hw_ecc_bch_read_oob_raw;
22948c2ecf20Sopenharmony_ci		ecc->read_oob = marvell_nfc_hw_ecc_bch_read_oob;
22958c2ecf20Sopenharmony_ci		ecc->write_page_raw = marvell_nfc_hw_ecc_bch_write_page_raw;
22968c2ecf20Sopenharmony_ci		ecc->write_page = marvell_nfc_hw_ecc_bch_write_page;
22978c2ecf20Sopenharmony_ci		ecc->write_oob_raw = marvell_nfc_hw_ecc_bch_write_oob_raw;
22988c2ecf20Sopenharmony_ci		ecc->write_oob = marvell_nfc_hw_ecc_bch_write_oob;
22998c2ecf20Sopenharmony_ci	}
23008c2ecf20Sopenharmony_ci
23018c2ecf20Sopenharmony_ci	return 0;
23028c2ecf20Sopenharmony_ci}
23038c2ecf20Sopenharmony_ci
23048c2ecf20Sopenharmony_cistatic int marvell_nand_ecc_init(struct mtd_info *mtd,
23058c2ecf20Sopenharmony_ci				 struct nand_ecc_ctrl *ecc)
23068c2ecf20Sopenharmony_ci{
23078c2ecf20Sopenharmony_ci	struct nand_chip *chip = mtd_to_nand(mtd);
23088c2ecf20Sopenharmony_ci	const struct nand_ecc_props *requirements =
23098c2ecf20Sopenharmony_ci		nanddev_get_ecc_requirements(&chip->base);
23108c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
23118c2ecf20Sopenharmony_ci	int ret;
23128c2ecf20Sopenharmony_ci
23138c2ecf20Sopenharmony_ci	if (ecc->engine_type != NAND_ECC_ENGINE_TYPE_NONE &&
23148c2ecf20Sopenharmony_ci	    (!ecc->size || !ecc->strength)) {
23158c2ecf20Sopenharmony_ci		if (requirements->step_size && requirements->strength) {
23168c2ecf20Sopenharmony_ci			ecc->size = requirements->step_size;
23178c2ecf20Sopenharmony_ci			ecc->strength = requirements->strength;
23188c2ecf20Sopenharmony_ci		} else {
23198c2ecf20Sopenharmony_ci			dev_info(nfc->dev,
23208c2ecf20Sopenharmony_ci				 "No minimum ECC strength, using 1b/512B\n");
23218c2ecf20Sopenharmony_ci			ecc->size = 512;
23228c2ecf20Sopenharmony_ci			ecc->strength = 1;
23238c2ecf20Sopenharmony_ci		}
23248c2ecf20Sopenharmony_ci	}
23258c2ecf20Sopenharmony_ci
23268c2ecf20Sopenharmony_ci	switch (ecc->engine_type) {
23278c2ecf20Sopenharmony_ci	case NAND_ECC_ENGINE_TYPE_ON_HOST:
23288c2ecf20Sopenharmony_ci		ret = marvell_nand_hw_ecc_controller_init(mtd, ecc);
23298c2ecf20Sopenharmony_ci		if (ret)
23308c2ecf20Sopenharmony_ci			return ret;
23318c2ecf20Sopenharmony_ci		break;
23328c2ecf20Sopenharmony_ci	case NAND_ECC_ENGINE_TYPE_NONE:
23338c2ecf20Sopenharmony_ci	case NAND_ECC_ENGINE_TYPE_SOFT:
23348c2ecf20Sopenharmony_ci	case NAND_ECC_ENGINE_TYPE_ON_DIE:
23358c2ecf20Sopenharmony_ci		if (!nfc->caps->is_nfcv2 && mtd->writesize != SZ_512 &&
23368c2ecf20Sopenharmony_ci		    mtd->writesize != SZ_2K) {
23378c2ecf20Sopenharmony_ci			dev_err(nfc->dev, "NFCv1 cannot write %d bytes pages\n",
23388c2ecf20Sopenharmony_ci				mtd->writesize);
23398c2ecf20Sopenharmony_ci			return -EINVAL;
23408c2ecf20Sopenharmony_ci		}
23418c2ecf20Sopenharmony_ci		break;
23428c2ecf20Sopenharmony_ci	default:
23438c2ecf20Sopenharmony_ci		return -EINVAL;
23448c2ecf20Sopenharmony_ci	}
23458c2ecf20Sopenharmony_ci
23468c2ecf20Sopenharmony_ci	return 0;
23478c2ecf20Sopenharmony_ci}
23488c2ecf20Sopenharmony_ci
23498c2ecf20Sopenharmony_cistatic u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
23508c2ecf20Sopenharmony_cistatic u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
23518c2ecf20Sopenharmony_ci
23528c2ecf20Sopenharmony_cistatic struct nand_bbt_descr bbt_main_descr = {
23538c2ecf20Sopenharmony_ci	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
23548c2ecf20Sopenharmony_ci		   NAND_BBT_2BIT | NAND_BBT_VERSION,
23558c2ecf20Sopenharmony_ci	.offs =	8,
23568c2ecf20Sopenharmony_ci	.len = 6,
23578c2ecf20Sopenharmony_ci	.veroffs = 14,
23588c2ecf20Sopenharmony_ci	.maxblocks = 8,	/* Last 8 blocks in each chip */
23598c2ecf20Sopenharmony_ci	.pattern = bbt_pattern
23608c2ecf20Sopenharmony_ci};
23618c2ecf20Sopenharmony_ci
23628c2ecf20Sopenharmony_cistatic struct nand_bbt_descr bbt_mirror_descr = {
23638c2ecf20Sopenharmony_ci	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
23648c2ecf20Sopenharmony_ci		   NAND_BBT_2BIT | NAND_BBT_VERSION,
23658c2ecf20Sopenharmony_ci	.offs =	8,
23668c2ecf20Sopenharmony_ci	.len = 6,
23678c2ecf20Sopenharmony_ci	.veroffs = 14,
23688c2ecf20Sopenharmony_ci	.maxblocks = 8,	/* Last 8 blocks in each chip */
23698c2ecf20Sopenharmony_ci	.pattern = bbt_mirror_pattern
23708c2ecf20Sopenharmony_ci};
23718c2ecf20Sopenharmony_ci
23728c2ecf20Sopenharmony_cistatic int marvell_nfc_setup_interface(struct nand_chip *chip, int chipnr,
23738c2ecf20Sopenharmony_ci				       const struct nand_interface_config *conf)
23748c2ecf20Sopenharmony_ci{
23758c2ecf20Sopenharmony_ci	struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
23768c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
23778c2ecf20Sopenharmony_ci	unsigned int period_ns = 1000000000 / clk_get_rate(nfc->core_clk) * 2;
23788c2ecf20Sopenharmony_ci	const struct nand_sdr_timings *sdr;
23798c2ecf20Sopenharmony_ci	struct marvell_nfc_timings nfc_tmg;
23808c2ecf20Sopenharmony_ci	int read_delay;
23818c2ecf20Sopenharmony_ci
23828c2ecf20Sopenharmony_ci	sdr = nand_get_sdr_timings(conf);
23838c2ecf20Sopenharmony_ci	if (IS_ERR(sdr))
23848c2ecf20Sopenharmony_ci		return PTR_ERR(sdr);
23858c2ecf20Sopenharmony_ci
23868c2ecf20Sopenharmony_ci	/*
23878c2ecf20Sopenharmony_ci	 * SDR timings are given in pico-seconds while NFC timings must be
23888c2ecf20Sopenharmony_ci	 * expressed in NAND controller clock cycles, which is half of the
23898c2ecf20Sopenharmony_ci	 * frequency of the accessible ECC clock retrieved by clk_get_rate().
23908c2ecf20Sopenharmony_ci	 * This is not written anywhere in the datasheet but was observed
23918c2ecf20Sopenharmony_ci	 * with an oscilloscope.
23928c2ecf20Sopenharmony_ci	 *
23938c2ecf20Sopenharmony_ci	 * NFC datasheet gives equations from which thoses calculations
23948c2ecf20Sopenharmony_ci	 * are derived, they tend to be slightly more restrictives than the
23958c2ecf20Sopenharmony_ci	 * given core timings and may improve the overall speed.
23968c2ecf20Sopenharmony_ci	 */
23978c2ecf20Sopenharmony_ci	nfc_tmg.tRP = TO_CYCLES(DIV_ROUND_UP(sdr->tRC_min, 2), period_ns) - 1;
23988c2ecf20Sopenharmony_ci	nfc_tmg.tRH = nfc_tmg.tRP;
23998c2ecf20Sopenharmony_ci	nfc_tmg.tWP = TO_CYCLES(DIV_ROUND_UP(sdr->tWC_min, 2), period_ns) - 1;
24008c2ecf20Sopenharmony_ci	nfc_tmg.tWH = nfc_tmg.tWP;
24018c2ecf20Sopenharmony_ci	nfc_tmg.tCS = TO_CYCLES(sdr->tCS_min, period_ns);
24028c2ecf20Sopenharmony_ci	nfc_tmg.tCH = TO_CYCLES(sdr->tCH_min, period_ns) - 1;
24038c2ecf20Sopenharmony_ci	nfc_tmg.tADL = TO_CYCLES(sdr->tADL_min, period_ns);
24048c2ecf20Sopenharmony_ci	/*
24058c2ecf20Sopenharmony_ci	 * Read delay is the time of propagation from SoC pins to NFC internal
24068c2ecf20Sopenharmony_ci	 * logic. With non-EDO timings, this is MIN_RD_DEL_CNT clock cycles. In
24078c2ecf20Sopenharmony_ci	 * EDO mode, an additional delay of tRH must be taken into account so
24088c2ecf20Sopenharmony_ci	 * the data is sampled on the falling edge instead of the rising edge.
24098c2ecf20Sopenharmony_ci	 */
24108c2ecf20Sopenharmony_ci	read_delay = sdr->tRC_min >= 30000 ?
24118c2ecf20Sopenharmony_ci		MIN_RD_DEL_CNT : MIN_RD_DEL_CNT + nfc_tmg.tRH;
24128c2ecf20Sopenharmony_ci
24138c2ecf20Sopenharmony_ci	nfc_tmg.tAR = TO_CYCLES(sdr->tAR_min, period_ns);
24148c2ecf20Sopenharmony_ci	/*
24158c2ecf20Sopenharmony_ci	 * tWHR and tRHW are supposed to be read to write delays (and vice
24168c2ecf20Sopenharmony_ci	 * versa) but in some cases, ie. when doing a change column, they must
24178c2ecf20Sopenharmony_ci	 * be greater than that to be sure tCCS delay is respected.
24188c2ecf20Sopenharmony_ci	 */
24198c2ecf20Sopenharmony_ci	nfc_tmg.tWHR = TO_CYCLES(max_t(int, sdr->tWHR_min, sdr->tCCS_min),
24208c2ecf20Sopenharmony_ci				 period_ns) - 2,
24218c2ecf20Sopenharmony_ci	nfc_tmg.tRHW = TO_CYCLES(max_t(int, sdr->tRHW_min, sdr->tCCS_min),
24228c2ecf20Sopenharmony_ci				 period_ns);
24238c2ecf20Sopenharmony_ci
24248c2ecf20Sopenharmony_ci	/*
24258c2ecf20Sopenharmony_ci	 * NFCv2: Use WAIT_MODE (wait for RB line), do not rely only on delays.
24268c2ecf20Sopenharmony_ci	 * NFCv1: No WAIT_MODE, tR must be maximal.
24278c2ecf20Sopenharmony_ci	 */
24288c2ecf20Sopenharmony_ci	if (nfc->caps->is_nfcv2) {
24298c2ecf20Sopenharmony_ci		nfc_tmg.tR = TO_CYCLES(sdr->tWB_max, period_ns);
24308c2ecf20Sopenharmony_ci	} else {
24318c2ecf20Sopenharmony_ci		nfc_tmg.tR = TO_CYCLES64(sdr->tWB_max + sdr->tR_max,
24328c2ecf20Sopenharmony_ci					 period_ns);
24338c2ecf20Sopenharmony_ci		if (nfc_tmg.tR + 3 > nfc_tmg.tCH)
24348c2ecf20Sopenharmony_ci			nfc_tmg.tR = nfc_tmg.tCH - 3;
24358c2ecf20Sopenharmony_ci		else
24368c2ecf20Sopenharmony_ci			nfc_tmg.tR = 0;
24378c2ecf20Sopenharmony_ci	}
24388c2ecf20Sopenharmony_ci
24398c2ecf20Sopenharmony_ci	if (chipnr < 0)
24408c2ecf20Sopenharmony_ci		return 0;
24418c2ecf20Sopenharmony_ci
24428c2ecf20Sopenharmony_ci	marvell_nand->ndtr0 =
24438c2ecf20Sopenharmony_ci		NDTR0_TRP(nfc_tmg.tRP) |
24448c2ecf20Sopenharmony_ci		NDTR0_TRH(nfc_tmg.tRH) |
24458c2ecf20Sopenharmony_ci		NDTR0_ETRP(nfc_tmg.tRP) |
24468c2ecf20Sopenharmony_ci		NDTR0_TWP(nfc_tmg.tWP) |
24478c2ecf20Sopenharmony_ci		NDTR0_TWH(nfc_tmg.tWH) |
24488c2ecf20Sopenharmony_ci		NDTR0_TCS(nfc_tmg.tCS) |
24498c2ecf20Sopenharmony_ci		NDTR0_TCH(nfc_tmg.tCH);
24508c2ecf20Sopenharmony_ci
24518c2ecf20Sopenharmony_ci	marvell_nand->ndtr1 =
24528c2ecf20Sopenharmony_ci		NDTR1_TAR(nfc_tmg.tAR) |
24538c2ecf20Sopenharmony_ci		NDTR1_TWHR(nfc_tmg.tWHR) |
24548c2ecf20Sopenharmony_ci		NDTR1_TR(nfc_tmg.tR);
24558c2ecf20Sopenharmony_ci
24568c2ecf20Sopenharmony_ci	if (nfc->caps->is_nfcv2) {
24578c2ecf20Sopenharmony_ci		marvell_nand->ndtr0 |=
24588c2ecf20Sopenharmony_ci			NDTR0_RD_CNT_DEL(read_delay) |
24598c2ecf20Sopenharmony_ci			NDTR0_SELCNTR |
24608c2ecf20Sopenharmony_ci			NDTR0_TADL(nfc_tmg.tADL);
24618c2ecf20Sopenharmony_ci
24628c2ecf20Sopenharmony_ci		marvell_nand->ndtr1 |=
24638c2ecf20Sopenharmony_ci			NDTR1_TRHW(nfc_tmg.tRHW) |
24648c2ecf20Sopenharmony_ci			NDTR1_WAIT_MODE;
24658c2ecf20Sopenharmony_ci	}
24668c2ecf20Sopenharmony_ci
24678c2ecf20Sopenharmony_ci	/*
24688c2ecf20Sopenharmony_ci	 * Reset nfc->selected_chip so the next command will cause the timing
24698c2ecf20Sopenharmony_ci	 * registers to be updated in marvell_nfc_select_target().
24708c2ecf20Sopenharmony_ci	 */
24718c2ecf20Sopenharmony_ci	nfc->selected_chip = NULL;
24728c2ecf20Sopenharmony_ci
24738c2ecf20Sopenharmony_ci	return 0;
24748c2ecf20Sopenharmony_ci}
24758c2ecf20Sopenharmony_ci
24768c2ecf20Sopenharmony_cistatic int marvell_nand_attach_chip(struct nand_chip *chip)
24778c2ecf20Sopenharmony_ci{
24788c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
24798c2ecf20Sopenharmony_ci	struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
24808c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
24818c2ecf20Sopenharmony_ci	struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(nfc->dev);
24828c2ecf20Sopenharmony_ci	int ret;
24838c2ecf20Sopenharmony_ci
24848c2ecf20Sopenharmony_ci	if (pdata && pdata->flash_bbt)
24858c2ecf20Sopenharmony_ci		chip->bbt_options |= NAND_BBT_USE_FLASH;
24868c2ecf20Sopenharmony_ci
24878c2ecf20Sopenharmony_ci	if (chip->bbt_options & NAND_BBT_USE_FLASH) {
24888c2ecf20Sopenharmony_ci		/*
24898c2ecf20Sopenharmony_ci		 * We'll use a bad block table stored in-flash and don't
24908c2ecf20Sopenharmony_ci		 * allow writing the bad block marker to the flash.
24918c2ecf20Sopenharmony_ci		 */
24928c2ecf20Sopenharmony_ci		chip->bbt_options |= NAND_BBT_NO_OOB_BBM;
24938c2ecf20Sopenharmony_ci		chip->bbt_td = &bbt_main_descr;
24948c2ecf20Sopenharmony_ci		chip->bbt_md = &bbt_mirror_descr;
24958c2ecf20Sopenharmony_ci	}
24968c2ecf20Sopenharmony_ci
24978c2ecf20Sopenharmony_ci	/* Save the chip-specific fields of NDCR */
24988c2ecf20Sopenharmony_ci	marvell_nand->ndcr = NDCR_PAGE_SZ(mtd->writesize);
24998c2ecf20Sopenharmony_ci	if (chip->options & NAND_BUSWIDTH_16)
25008c2ecf20Sopenharmony_ci		marvell_nand->ndcr |= NDCR_DWIDTH_M | NDCR_DWIDTH_C;
25018c2ecf20Sopenharmony_ci
25028c2ecf20Sopenharmony_ci	/*
25038c2ecf20Sopenharmony_ci	 * On small page NANDs, only one cycle is needed to pass the
25048c2ecf20Sopenharmony_ci	 * column address.
25058c2ecf20Sopenharmony_ci	 */
25068c2ecf20Sopenharmony_ci	if (mtd->writesize <= 512) {
25078c2ecf20Sopenharmony_ci		marvell_nand->addr_cyc = 1;
25088c2ecf20Sopenharmony_ci	} else {
25098c2ecf20Sopenharmony_ci		marvell_nand->addr_cyc = 2;
25108c2ecf20Sopenharmony_ci		marvell_nand->ndcr |= NDCR_RA_START;
25118c2ecf20Sopenharmony_ci	}
25128c2ecf20Sopenharmony_ci
25138c2ecf20Sopenharmony_ci	/*
25148c2ecf20Sopenharmony_ci	 * Now add the number of cycles needed to pass the row
25158c2ecf20Sopenharmony_ci	 * address.
25168c2ecf20Sopenharmony_ci	 *
25178c2ecf20Sopenharmony_ci	 * Addressing a chip using CS 2 or 3 should also need the third row
25188c2ecf20Sopenharmony_ci	 * cycle but due to inconsistance in the documentation and lack of
25198c2ecf20Sopenharmony_ci	 * hardware to test this situation, this case is not supported.
25208c2ecf20Sopenharmony_ci	 */
25218c2ecf20Sopenharmony_ci	if (chip->options & NAND_ROW_ADDR_3)
25228c2ecf20Sopenharmony_ci		marvell_nand->addr_cyc += 3;
25238c2ecf20Sopenharmony_ci	else
25248c2ecf20Sopenharmony_ci		marvell_nand->addr_cyc += 2;
25258c2ecf20Sopenharmony_ci
25268c2ecf20Sopenharmony_ci	if (pdata) {
25278c2ecf20Sopenharmony_ci		chip->ecc.size = pdata->ecc_step_size;
25288c2ecf20Sopenharmony_ci		chip->ecc.strength = pdata->ecc_strength;
25298c2ecf20Sopenharmony_ci	}
25308c2ecf20Sopenharmony_ci
25318c2ecf20Sopenharmony_ci	ret = marvell_nand_ecc_init(mtd, &chip->ecc);
25328c2ecf20Sopenharmony_ci	if (ret) {
25338c2ecf20Sopenharmony_ci		dev_err(nfc->dev, "ECC init failed: %d\n", ret);
25348c2ecf20Sopenharmony_ci		return ret;
25358c2ecf20Sopenharmony_ci	}
25368c2ecf20Sopenharmony_ci
25378c2ecf20Sopenharmony_ci	if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST) {
25388c2ecf20Sopenharmony_ci		/*
25398c2ecf20Sopenharmony_ci		 * Subpage write not available with hardware ECC, prohibit also
25408c2ecf20Sopenharmony_ci		 * subpage read as in userspace subpage access would still be
25418c2ecf20Sopenharmony_ci		 * allowed and subpage write, if used, would lead to numerous
25428c2ecf20Sopenharmony_ci		 * uncorrectable ECC errors.
25438c2ecf20Sopenharmony_ci		 */
25448c2ecf20Sopenharmony_ci		chip->options |= NAND_NO_SUBPAGE_WRITE;
25458c2ecf20Sopenharmony_ci	}
25468c2ecf20Sopenharmony_ci
25478c2ecf20Sopenharmony_ci	if (pdata || nfc->caps->legacy_of_bindings) {
25488c2ecf20Sopenharmony_ci		/*
25498c2ecf20Sopenharmony_ci		 * We keep the MTD name unchanged to avoid breaking platforms
25508c2ecf20Sopenharmony_ci		 * where the MTD cmdline parser is used and the bootloader
25518c2ecf20Sopenharmony_ci		 * has not been updated to use the new naming scheme.
25528c2ecf20Sopenharmony_ci		 */
25538c2ecf20Sopenharmony_ci		mtd->name = "pxa3xx_nand-0";
25548c2ecf20Sopenharmony_ci	} else if (!mtd->name) {
25558c2ecf20Sopenharmony_ci		/*
25568c2ecf20Sopenharmony_ci		 * If the new bindings are used and the bootloader has not been
25578c2ecf20Sopenharmony_ci		 * updated to pass a new mtdparts parameter on the cmdline, you
25588c2ecf20Sopenharmony_ci		 * should define the following property in your NAND node, ie:
25598c2ecf20Sopenharmony_ci		 *
25608c2ecf20Sopenharmony_ci		 *	label = "main-storage";
25618c2ecf20Sopenharmony_ci		 *
25628c2ecf20Sopenharmony_ci		 * This way, mtd->name will be set by the core when
25638c2ecf20Sopenharmony_ci		 * nand_set_flash_node() is called.
25648c2ecf20Sopenharmony_ci		 */
25658c2ecf20Sopenharmony_ci		mtd->name = devm_kasprintf(nfc->dev, GFP_KERNEL,
25668c2ecf20Sopenharmony_ci					   "%s:nand.%d", dev_name(nfc->dev),
25678c2ecf20Sopenharmony_ci					   marvell_nand->sels[0].cs);
25688c2ecf20Sopenharmony_ci		if (!mtd->name) {
25698c2ecf20Sopenharmony_ci			dev_err(nfc->dev, "Failed to allocate mtd->name\n");
25708c2ecf20Sopenharmony_ci			return -ENOMEM;
25718c2ecf20Sopenharmony_ci		}
25728c2ecf20Sopenharmony_ci	}
25738c2ecf20Sopenharmony_ci
25748c2ecf20Sopenharmony_ci	return 0;
25758c2ecf20Sopenharmony_ci}
25768c2ecf20Sopenharmony_ci
25778c2ecf20Sopenharmony_cistatic const struct nand_controller_ops marvell_nand_controller_ops = {
25788c2ecf20Sopenharmony_ci	.attach_chip = marvell_nand_attach_chip,
25798c2ecf20Sopenharmony_ci	.exec_op = marvell_nfc_exec_op,
25808c2ecf20Sopenharmony_ci	.setup_interface = marvell_nfc_setup_interface,
25818c2ecf20Sopenharmony_ci};
25828c2ecf20Sopenharmony_ci
25838c2ecf20Sopenharmony_cistatic int marvell_nand_chip_init(struct device *dev, struct marvell_nfc *nfc,
25848c2ecf20Sopenharmony_ci				  struct device_node *np)
25858c2ecf20Sopenharmony_ci{
25868c2ecf20Sopenharmony_ci	struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(dev);
25878c2ecf20Sopenharmony_ci	struct marvell_nand_chip *marvell_nand;
25888c2ecf20Sopenharmony_ci	struct mtd_info *mtd;
25898c2ecf20Sopenharmony_ci	struct nand_chip *chip;
25908c2ecf20Sopenharmony_ci	int nsels, ret, i;
25918c2ecf20Sopenharmony_ci	u32 cs, rb;
25928c2ecf20Sopenharmony_ci
25938c2ecf20Sopenharmony_ci	/*
25948c2ecf20Sopenharmony_ci	 * The legacy "num-cs" property indicates the number of CS on the only
25958c2ecf20Sopenharmony_ci	 * chip connected to the controller (legacy bindings does not support
25968c2ecf20Sopenharmony_ci	 * more than one chip). The CS and RB pins are always the #0.
25978c2ecf20Sopenharmony_ci	 *
25988c2ecf20Sopenharmony_ci	 * When not using legacy bindings, a couple of "reg" and "nand-rb"
25998c2ecf20Sopenharmony_ci	 * properties must be filled. For each chip, expressed as a subnode,
26008c2ecf20Sopenharmony_ci	 * "reg" points to the CS lines and "nand-rb" to the RB line.
26018c2ecf20Sopenharmony_ci	 */
26028c2ecf20Sopenharmony_ci	if (pdata || nfc->caps->legacy_of_bindings) {
26038c2ecf20Sopenharmony_ci		nsels = 1;
26048c2ecf20Sopenharmony_ci	} else {
26058c2ecf20Sopenharmony_ci		nsels = of_property_count_elems_of_size(np, "reg", sizeof(u32));
26068c2ecf20Sopenharmony_ci		if (nsels <= 0) {
26078c2ecf20Sopenharmony_ci			dev_err(dev, "missing/invalid reg property\n");
26088c2ecf20Sopenharmony_ci			return -EINVAL;
26098c2ecf20Sopenharmony_ci		}
26108c2ecf20Sopenharmony_ci	}
26118c2ecf20Sopenharmony_ci
26128c2ecf20Sopenharmony_ci	/* Alloc the nand chip structure */
26138c2ecf20Sopenharmony_ci	marvell_nand = devm_kzalloc(dev,
26148c2ecf20Sopenharmony_ci				    struct_size(marvell_nand, sels, nsels),
26158c2ecf20Sopenharmony_ci				    GFP_KERNEL);
26168c2ecf20Sopenharmony_ci	if (!marvell_nand) {
26178c2ecf20Sopenharmony_ci		dev_err(dev, "could not allocate chip structure\n");
26188c2ecf20Sopenharmony_ci		return -ENOMEM;
26198c2ecf20Sopenharmony_ci	}
26208c2ecf20Sopenharmony_ci
26218c2ecf20Sopenharmony_ci	marvell_nand->nsels = nsels;
26228c2ecf20Sopenharmony_ci	marvell_nand->selected_die = -1;
26238c2ecf20Sopenharmony_ci
26248c2ecf20Sopenharmony_ci	for (i = 0; i < nsels; i++) {
26258c2ecf20Sopenharmony_ci		if (pdata || nfc->caps->legacy_of_bindings) {
26268c2ecf20Sopenharmony_ci			/*
26278c2ecf20Sopenharmony_ci			 * Legacy bindings use the CS lines in natural
26288c2ecf20Sopenharmony_ci			 * order (0, 1, ...)
26298c2ecf20Sopenharmony_ci			 */
26308c2ecf20Sopenharmony_ci			cs = i;
26318c2ecf20Sopenharmony_ci		} else {
26328c2ecf20Sopenharmony_ci			/* Retrieve CS id */
26338c2ecf20Sopenharmony_ci			ret = of_property_read_u32_index(np, "reg", i, &cs);
26348c2ecf20Sopenharmony_ci			if (ret) {
26358c2ecf20Sopenharmony_ci				dev_err(dev, "could not retrieve reg property: %d\n",
26368c2ecf20Sopenharmony_ci					ret);
26378c2ecf20Sopenharmony_ci				return ret;
26388c2ecf20Sopenharmony_ci			}
26398c2ecf20Sopenharmony_ci		}
26408c2ecf20Sopenharmony_ci
26418c2ecf20Sopenharmony_ci		if (cs >= nfc->caps->max_cs_nb) {
26428c2ecf20Sopenharmony_ci			dev_err(dev, "invalid reg value: %u (max CS = %d)\n",
26438c2ecf20Sopenharmony_ci				cs, nfc->caps->max_cs_nb);
26448c2ecf20Sopenharmony_ci			return -EINVAL;
26458c2ecf20Sopenharmony_ci		}
26468c2ecf20Sopenharmony_ci
26478c2ecf20Sopenharmony_ci		if (test_and_set_bit(cs, &nfc->assigned_cs)) {
26488c2ecf20Sopenharmony_ci			dev_err(dev, "CS %d already assigned\n", cs);
26498c2ecf20Sopenharmony_ci			return -EINVAL;
26508c2ecf20Sopenharmony_ci		}
26518c2ecf20Sopenharmony_ci
26528c2ecf20Sopenharmony_ci		/*
26538c2ecf20Sopenharmony_ci		 * The cs variable represents the chip select id, which must be
26548c2ecf20Sopenharmony_ci		 * converted in bit fields for NDCB0 and NDCB2 to select the
26558c2ecf20Sopenharmony_ci		 * right chip. Unfortunately, due to a lack of information on
26568c2ecf20Sopenharmony_ci		 * the subject and incoherent documentation, the user should not
26578c2ecf20Sopenharmony_ci		 * use CS1 and CS3 at all as asserting them is not supported in
26588c2ecf20Sopenharmony_ci		 * a reliable way (due to multiplexing inside ADDR5 field).
26598c2ecf20Sopenharmony_ci		 */
26608c2ecf20Sopenharmony_ci		marvell_nand->sels[i].cs = cs;
26618c2ecf20Sopenharmony_ci		switch (cs) {
26628c2ecf20Sopenharmony_ci		case 0:
26638c2ecf20Sopenharmony_ci		case 2:
26648c2ecf20Sopenharmony_ci			marvell_nand->sels[i].ndcb0_csel = 0;
26658c2ecf20Sopenharmony_ci			break;
26668c2ecf20Sopenharmony_ci		case 1:
26678c2ecf20Sopenharmony_ci		case 3:
26688c2ecf20Sopenharmony_ci			marvell_nand->sels[i].ndcb0_csel = NDCB0_CSEL;
26698c2ecf20Sopenharmony_ci			break;
26708c2ecf20Sopenharmony_ci		default:
26718c2ecf20Sopenharmony_ci			return -EINVAL;
26728c2ecf20Sopenharmony_ci		}
26738c2ecf20Sopenharmony_ci
26748c2ecf20Sopenharmony_ci		/* Retrieve RB id */
26758c2ecf20Sopenharmony_ci		if (pdata || nfc->caps->legacy_of_bindings) {
26768c2ecf20Sopenharmony_ci			/* Legacy bindings always use RB #0 */
26778c2ecf20Sopenharmony_ci			rb = 0;
26788c2ecf20Sopenharmony_ci		} else {
26798c2ecf20Sopenharmony_ci			ret = of_property_read_u32_index(np, "nand-rb", i,
26808c2ecf20Sopenharmony_ci							 &rb);
26818c2ecf20Sopenharmony_ci			if (ret) {
26828c2ecf20Sopenharmony_ci				dev_err(dev,
26838c2ecf20Sopenharmony_ci					"could not retrieve RB property: %d\n",
26848c2ecf20Sopenharmony_ci					ret);
26858c2ecf20Sopenharmony_ci				return ret;
26868c2ecf20Sopenharmony_ci			}
26878c2ecf20Sopenharmony_ci		}
26888c2ecf20Sopenharmony_ci
26898c2ecf20Sopenharmony_ci		if (rb >= nfc->caps->max_rb_nb) {
26908c2ecf20Sopenharmony_ci			dev_err(dev, "invalid reg value: %u (max RB = %d)\n",
26918c2ecf20Sopenharmony_ci				rb, nfc->caps->max_rb_nb);
26928c2ecf20Sopenharmony_ci			return -EINVAL;
26938c2ecf20Sopenharmony_ci		}
26948c2ecf20Sopenharmony_ci
26958c2ecf20Sopenharmony_ci		marvell_nand->sels[i].rb = rb;
26968c2ecf20Sopenharmony_ci	}
26978c2ecf20Sopenharmony_ci
26988c2ecf20Sopenharmony_ci	chip = &marvell_nand->chip;
26998c2ecf20Sopenharmony_ci	chip->controller = &nfc->controller;
27008c2ecf20Sopenharmony_ci	nand_set_flash_node(chip, np);
27018c2ecf20Sopenharmony_ci
27028c2ecf20Sopenharmony_ci	if (of_property_read_bool(np, "marvell,nand-keep-config"))
27038c2ecf20Sopenharmony_ci		chip->options |= NAND_KEEP_TIMINGS;
27048c2ecf20Sopenharmony_ci
27058c2ecf20Sopenharmony_ci	mtd = nand_to_mtd(chip);
27068c2ecf20Sopenharmony_ci	mtd->dev.parent = dev;
27078c2ecf20Sopenharmony_ci
27088c2ecf20Sopenharmony_ci	/*
27098c2ecf20Sopenharmony_ci	 * Default to HW ECC engine mode. If the nand-ecc-mode property is given
27108c2ecf20Sopenharmony_ci	 * in the DT node, this entry will be overwritten in nand_scan_ident().
27118c2ecf20Sopenharmony_ci	 */
27128c2ecf20Sopenharmony_ci	chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
27138c2ecf20Sopenharmony_ci
27148c2ecf20Sopenharmony_ci	/*
27158c2ecf20Sopenharmony_ci	 * Save a reference value for timing registers before
27168c2ecf20Sopenharmony_ci	 * ->setup_interface() is called.
27178c2ecf20Sopenharmony_ci	 */
27188c2ecf20Sopenharmony_ci	marvell_nand->ndtr0 = readl_relaxed(nfc->regs + NDTR0);
27198c2ecf20Sopenharmony_ci	marvell_nand->ndtr1 = readl_relaxed(nfc->regs + NDTR1);
27208c2ecf20Sopenharmony_ci
27218c2ecf20Sopenharmony_ci	chip->options |= NAND_BUSWIDTH_AUTO;
27228c2ecf20Sopenharmony_ci
27238c2ecf20Sopenharmony_ci	ret = nand_scan(chip, marvell_nand->nsels);
27248c2ecf20Sopenharmony_ci	if (ret) {
27258c2ecf20Sopenharmony_ci		dev_err(dev, "could not scan the nand chip\n");
27268c2ecf20Sopenharmony_ci		return ret;
27278c2ecf20Sopenharmony_ci	}
27288c2ecf20Sopenharmony_ci
27298c2ecf20Sopenharmony_ci	if (pdata)
27308c2ecf20Sopenharmony_ci		/* Legacy bindings support only one chip */
27318c2ecf20Sopenharmony_ci		ret = mtd_device_register(mtd, pdata->parts, pdata->nr_parts);
27328c2ecf20Sopenharmony_ci	else
27338c2ecf20Sopenharmony_ci		ret = mtd_device_register(mtd, NULL, 0);
27348c2ecf20Sopenharmony_ci	if (ret) {
27358c2ecf20Sopenharmony_ci		dev_err(dev, "failed to register mtd device: %d\n", ret);
27368c2ecf20Sopenharmony_ci		nand_cleanup(chip);
27378c2ecf20Sopenharmony_ci		return ret;
27388c2ecf20Sopenharmony_ci	}
27398c2ecf20Sopenharmony_ci
27408c2ecf20Sopenharmony_ci	list_add_tail(&marvell_nand->node, &nfc->chips);
27418c2ecf20Sopenharmony_ci
27428c2ecf20Sopenharmony_ci	return 0;
27438c2ecf20Sopenharmony_ci}
27448c2ecf20Sopenharmony_ci
27458c2ecf20Sopenharmony_cistatic void marvell_nand_chips_cleanup(struct marvell_nfc *nfc)
27468c2ecf20Sopenharmony_ci{
27478c2ecf20Sopenharmony_ci	struct marvell_nand_chip *entry, *temp;
27488c2ecf20Sopenharmony_ci	struct nand_chip *chip;
27498c2ecf20Sopenharmony_ci	int ret;
27508c2ecf20Sopenharmony_ci
27518c2ecf20Sopenharmony_ci	list_for_each_entry_safe(entry, temp, &nfc->chips, node) {
27528c2ecf20Sopenharmony_ci		chip = &entry->chip;
27538c2ecf20Sopenharmony_ci		ret = mtd_device_unregister(nand_to_mtd(chip));
27548c2ecf20Sopenharmony_ci		WARN_ON(ret);
27558c2ecf20Sopenharmony_ci		nand_cleanup(chip);
27568c2ecf20Sopenharmony_ci		list_del(&entry->node);
27578c2ecf20Sopenharmony_ci	}
27588c2ecf20Sopenharmony_ci}
27598c2ecf20Sopenharmony_ci
27608c2ecf20Sopenharmony_cistatic int marvell_nand_chips_init(struct device *dev, struct marvell_nfc *nfc)
27618c2ecf20Sopenharmony_ci{
27628c2ecf20Sopenharmony_ci	struct device_node *np = dev->of_node;
27638c2ecf20Sopenharmony_ci	struct device_node *nand_np;
27648c2ecf20Sopenharmony_ci	int max_cs = nfc->caps->max_cs_nb;
27658c2ecf20Sopenharmony_ci	int nchips;
27668c2ecf20Sopenharmony_ci	int ret;
27678c2ecf20Sopenharmony_ci
27688c2ecf20Sopenharmony_ci	if (!np)
27698c2ecf20Sopenharmony_ci		nchips = 1;
27708c2ecf20Sopenharmony_ci	else
27718c2ecf20Sopenharmony_ci		nchips = of_get_child_count(np);
27728c2ecf20Sopenharmony_ci
27738c2ecf20Sopenharmony_ci	if (nchips > max_cs) {
27748c2ecf20Sopenharmony_ci		dev_err(dev, "too many NAND chips: %d (max = %d CS)\n", nchips,
27758c2ecf20Sopenharmony_ci			max_cs);
27768c2ecf20Sopenharmony_ci		return -EINVAL;
27778c2ecf20Sopenharmony_ci	}
27788c2ecf20Sopenharmony_ci
27798c2ecf20Sopenharmony_ci	/*
27808c2ecf20Sopenharmony_ci	 * Legacy bindings do not use child nodes to exhibit NAND chip
27818c2ecf20Sopenharmony_ci	 * properties and layout. Instead, NAND properties are mixed with the
27828c2ecf20Sopenharmony_ci	 * controller ones, and partitions are defined as direct subnodes of the
27838c2ecf20Sopenharmony_ci	 * NAND controller node.
27848c2ecf20Sopenharmony_ci	 */
27858c2ecf20Sopenharmony_ci	if (nfc->caps->legacy_of_bindings) {
27868c2ecf20Sopenharmony_ci		ret = marvell_nand_chip_init(dev, nfc, np);
27878c2ecf20Sopenharmony_ci		return ret;
27888c2ecf20Sopenharmony_ci	}
27898c2ecf20Sopenharmony_ci
27908c2ecf20Sopenharmony_ci	for_each_child_of_node(np, nand_np) {
27918c2ecf20Sopenharmony_ci		ret = marvell_nand_chip_init(dev, nfc, nand_np);
27928c2ecf20Sopenharmony_ci		if (ret) {
27938c2ecf20Sopenharmony_ci			of_node_put(nand_np);
27948c2ecf20Sopenharmony_ci			goto cleanup_chips;
27958c2ecf20Sopenharmony_ci		}
27968c2ecf20Sopenharmony_ci	}
27978c2ecf20Sopenharmony_ci
27988c2ecf20Sopenharmony_ci	return 0;
27998c2ecf20Sopenharmony_ci
28008c2ecf20Sopenharmony_cicleanup_chips:
28018c2ecf20Sopenharmony_ci	marvell_nand_chips_cleanup(nfc);
28028c2ecf20Sopenharmony_ci
28038c2ecf20Sopenharmony_ci	return ret;
28048c2ecf20Sopenharmony_ci}
28058c2ecf20Sopenharmony_ci
28068c2ecf20Sopenharmony_cistatic int marvell_nfc_init_dma(struct marvell_nfc *nfc)
28078c2ecf20Sopenharmony_ci{
28088c2ecf20Sopenharmony_ci	struct platform_device *pdev = container_of(nfc->dev,
28098c2ecf20Sopenharmony_ci						    struct platform_device,
28108c2ecf20Sopenharmony_ci						    dev);
28118c2ecf20Sopenharmony_ci	struct dma_slave_config config = {};
28128c2ecf20Sopenharmony_ci	struct resource *r;
28138c2ecf20Sopenharmony_ci	int ret;
28148c2ecf20Sopenharmony_ci
28158c2ecf20Sopenharmony_ci	if (!IS_ENABLED(CONFIG_PXA_DMA)) {
28168c2ecf20Sopenharmony_ci		dev_warn(nfc->dev,
28178c2ecf20Sopenharmony_ci			 "DMA not enabled in configuration\n");
28188c2ecf20Sopenharmony_ci		return -ENOTSUPP;
28198c2ecf20Sopenharmony_ci	}
28208c2ecf20Sopenharmony_ci
28218c2ecf20Sopenharmony_ci	ret = dma_set_mask_and_coherent(nfc->dev, DMA_BIT_MASK(32));
28228c2ecf20Sopenharmony_ci	if (ret)
28238c2ecf20Sopenharmony_ci		return ret;
28248c2ecf20Sopenharmony_ci
28258c2ecf20Sopenharmony_ci	nfc->dma_chan =	dma_request_chan(nfc->dev, "data");
28268c2ecf20Sopenharmony_ci	if (IS_ERR(nfc->dma_chan)) {
28278c2ecf20Sopenharmony_ci		ret = PTR_ERR(nfc->dma_chan);
28288c2ecf20Sopenharmony_ci		nfc->dma_chan = NULL;
28298c2ecf20Sopenharmony_ci		return dev_err_probe(nfc->dev, ret, "DMA channel request failed\n");
28308c2ecf20Sopenharmony_ci	}
28318c2ecf20Sopenharmony_ci
28328c2ecf20Sopenharmony_ci	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
28338c2ecf20Sopenharmony_ci	if (!r) {
28348c2ecf20Sopenharmony_ci		ret = -ENXIO;
28358c2ecf20Sopenharmony_ci		goto release_channel;
28368c2ecf20Sopenharmony_ci	}
28378c2ecf20Sopenharmony_ci
28388c2ecf20Sopenharmony_ci	config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
28398c2ecf20Sopenharmony_ci	config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
28408c2ecf20Sopenharmony_ci	config.src_addr = r->start + NDDB;
28418c2ecf20Sopenharmony_ci	config.dst_addr = r->start + NDDB;
28428c2ecf20Sopenharmony_ci	config.src_maxburst = 32;
28438c2ecf20Sopenharmony_ci	config.dst_maxburst = 32;
28448c2ecf20Sopenharmony_ci	ret = dmaengine_slave_config(nfc->dma_chan, &config);
28458c2ecf20Sopenharmony_ci	if (ret < 0) {
28468c2ecf20Sopenharmony_ci		dev_err(nfc->dev, "Failed to configure DMA channel\n");
28478c2ecf20Sopenharmony_ci		goto release_channel;
28488c2ecf20Sopenharmony_ci	}
28498c2ecf20Sopenharmony_ci
28508c2ecf20Sopenharmony_ci	/*
28518c2ecf20Sopenharmony_ci	 * DMA must act on length multiple of 32 and this length may be
28528c2ecf20Sopenharmony_ci	 * bigger than the destination buffer. Use this buffer instead
28538c2ecf20Sopenharmony_ci	 * for DMA transfers and then copy the desired amount of data to
28548c2ecf20Sopenharmony_ci	 * the provided buffer.
28558c2ecf20Sopenharmony_ci	 */
28568c2ecf20Sopenharmony_ci	nfc->dma_buf = kmalloc(MAX_CHUNK_SIZE, GFP_KERNEL | GFP_DMA);
28578c2ecf20Sopenharmony_ci	if (!nfc->dma_buf) {
28588c2ecf20Sopenharmony_ci		ret = -ENOMEM;
28598c2ecf20Sopenharmony_ci		goto release_channel;
28608c2ecf20Sopenharmony_ci	}
28618c2ecf20Sopenharmony_ci
28628c2ecf20Sopenharmony_ci	nfc->use_dma = true;
28638c2ecf20Sopenharmony_ci
28648c2ecf20Sopenharmony_ci	return 0;
28658c2ecf20Sopenharmony_ci
28668c2ecf20Sopenharmony_cirelease_channel:
28678c2ecf20Sopenharmony_ci	dma_release_channel(nfc->dma_chan);
28688c2ecf20Sopenharmony_ci	nfc->dma_chan = NULL;
28698c2ecf20Sopenharmony_ci
28708c2ecf20Sopenharmony_ci	return ret;
28718c2ecf20Sopenharmony_ci}
28728c2ecf20Sopenharmony_ci
28738c2ecf20Sopenharmony_cistatic void marvell_nfc_reset(struct marvell_nfc *nfc)
28748c2ecf20Sopenharmony_ci{
28758c2ecf20Sopenharmony_ci	/*
28768c2ecf20Sopenharmony_ci	 * ECC operations and interruptions are only enabled when specifically
28778c2ecf20Sopenharmony_ci	 * needed. ECC shall not be activated in the early stages (fails probe).
28788c2ecf20Sopenharmony_ci	 * Arbiter flag, even if marked as "reserved", must be set (empirical).
28798c2ecf20Sopenharmony_ci	 * SPARE_EN bit must always be set or ECC bytes will not be at the same
28808c2ecf20Sopenharmony_ci	 * offset in the read page and this will fail the protection.
28818c2ecf20Sopenharmony_ci	 */
28828c2ecf20Sopenharmony_ci	writel_relaxed(NDCR_ALL_INT | NDCR_ND_ARB_EN | NDCR_SPARE_EN |
28838c2ecf20Sopenharmony_ci		       NDCR_RD_ID_CNT(NFCV1_READID_LEN), nfc->regs + NDCR);
28848c2ecf20Sopenharmony_ci	writel_relaxed(0xFFFFFFFF, nfc->regs + NDSR);
28858c2ecf20Sopenharmony_ci	writel_relaxed(0, nfc->regs + NDECCCTRL);
28868c2ecf20Sopenharmony_ci}
28878c2ecf20Sopenharmony_ci
28888c2ecf20Sopenharmony_cistatic int marvell_nfc_init(struct marvell_nfc *nfc)
28898c2ecf20Sopenharmony_ci{
28908c2ecf20Sopenharmony_ci	struct device_node *np = nfc->dev->of_node;
28918c2ecf20Sopenharmony_ci
28928c2ecf20Sopenharmony_ci	/*
28938c2ecf20Sopenharmony_ci	 * Some SoCs like A7k/A8k need to enable manually the NAND
28948c2ecf20Sopenharmony_ci	 * controller, gated clocks and reset bits to avoid being bootloader
28958c2ecf20Sopenharmony_ci	 * dependent. This is done through the use of the System Functions
28968c2ecf20Sopenharmony_ci	 * registers.
28978c2ecf20Sopenharmony_ci	 */
28988c2ecf20Sopenharmony_ci	if (nfc->caps->need_system_controller) {
28998c2ecf20Sopenharmony_ci		struct regmap *sysctrl_base =
29008c2ecf20Sopenharmony_ci			syscon_regmap_lookup_by_phandle(np,
29018c2ecf20Sopenharmony_ci							"marvell,system-controller");
29028c2ecf20Sopenharmony_ci
29038c2ecf20Sopenharmony_ci		if (IS_ERR(sysctrl_base))
29048c2ecf20Sopenharmony_ci			return PTR_ERR(sysctrl_base);
29058c2ecf20Sopenharmony_ci
29068c2ecf20Sopenharmony_ci		regmap_write(sysctrl_base, GENCONF_SOC_DEVICE_MUX,
29078c2ecf20Sopenharmony_ci			     GENCONF_SOC_DEVICE_MUX_NFC_EN |
29088c2ecf20Sopenharmony_ci			     GENCONF_SOC_DEVICE_MUX_ECC_CLK_RST |
29098c2ecf20Sopenharmony_ci			     GENCONF_SOC_DEVICE_MUX_ECC_CORE_RST |
29108c2ecf20Sopenharmony_ci			     GENCONF_SOC_DEVICE_MUX_NFC_INT_EN);
29118c2ecf20Sopenharmony_ci
29128c2ecf20Sopenharmony_ci		regmap_update_bits(sysctrl_base, GENCONF_CLK_GATING_CTRL,
29138c2ecf20Sopenharmony_ci				   GENCONF_CLK_GATING_CTRL_ND_GATE,
29148c2ecf20Sopenharmony_ci				   GENCONF_CLK_GATING_CTRL_ND_GATE);
29158c2ecf20Sopenharmony_ci	}
29168c2ecf20Sopenharmony_ci
29178c2ecf20Sopenharmony_ci	/* Configure the DMA if appropriate */
29188c2ecf20Sopenharmony_ci	if (!nfc->caps->is_nfcv2)
29198c2ecf20Sopenharmony_ci		marvell_nfc_init_dma(nfc);
29208c2ecf20Sopenharmony_ci
29218c2ecf20Sopenharmony_ci	marvell_nfc_reset(nfc);
29228c2ecf20Sopenharmony_ci
29238c2ecf20Sopenharmony_ci	return 0;
29248c2ecf20Sopenharmony_ci}
29258c2ecf20Sopenharmony_ci
29268c2ecf20Sopenharmony_cistatic int marvell_nfc_probe(struct platform_device *pdev)
29278c2ecf20Sopenharmony_ci{
29288c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
29298c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc;
29308c2ecf20Sopenharmony_ci	int ret;
29318c2ecf20Sopenharmony_ci	int irq;
29328c2ecf20Sopenharmony_ci
29338c2ecf20Sopenharmony_ci	nfc = devm_kzalloc(&pdev->dev, sizeof(struct marvell_nfc),
29348c2ecf20Sopenharmony_ci			   GFP_KERNEL);
29358c2ecf20Sopenharmony_ci	if (!nfc)
29368c2ecf20Sopenharmony_ci		return -ENOMEM;
29378c2ecf20Sopenharmony_ci
29388c2ecf20Sopenharmony_ci	nfc->dev = dev;
29398c2ecf20Sopenharmony_ci	nand_controller_init(&nfc->controller);
29408c2ecf20Sopenharmony_ci	nfc->controller.ops = &marvell_nand_controller_ops;
29418c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&nfc->chips);
29428c2ecf20Sopenharmony_ci
29438c2ecf20Sopenharmony_ci	nfc->regs = devm_platform_ioremap_resource(pdev, 0);
29448c2ecf20Sopenharmony_ci	if (IS_ERR(nfc->regs))
29458c2ecf20Sopenharmony_ci		return PTR_ERR(nfc->regs);
29468c2ecf20Sopenharmony_ci
29478c2ecf20Sopenharmony_ci	irq = platform_get_irq(pdev, 0);
29488c2ecf20Sopenharmony_ci	if (irq < 0)
29498c2ecf20Sopenharmony_ci		return irq;
29508c2ecf20Sopenharmony_ci
29518c2ecf20Sopenharmony_ci	nfc->core_clk = devm_clk_get(&pdev->dev, "core");
29528c2ecf20Sopenharmony_ci
29538c2ecf20Sopenharmony_ci	/* Managed the legacy case (when the first clock was not named) */
29548c2ecf20Sopenharmony_ci	if (nfc->core_clk == ERR_PTR(-ENOENT))
29558c2ecf20Sopenharmony_ci		nfc->core_clk = devm_clk_get(&pdev->dev, NULL);
29568c2ecf20Sopenharmony_ci
29578c2ecf20Sopenharmony_ci	if (IS_ERR(nfc->core_clk))
29588c2ecf20Sopenharmony_ci		return PTR_ERR(nfc->core_clk);
29598c2ecf20Sopenharmony_ci
29608c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(nfc->core_clk);
29618c2ecf20Sopenharmony_ci	if (ret)
29628c2ecf20Sopenharmony_ci		return ret;
29638c2ecf20Sopenharmony_ci
29648c2ecf20Sopenharmony_ci	nfc->reg_clk = devm_clk_get(&pdev->dev, "reg");
29658c2ecf20Sopenharmony_ci	if (IS_ERR(nfc->reg_clk)) {
29668c2ecf20Sopenharmony_ci		if (PTR_ERR(nfc->reg_clk) != -ENOENT) {
29678c2ecf20Sopenharmony_ci			ret = PTR_ERR(nfc->reg_clk);
29688c2ecf20Sopenharmony_ci			goto unprepare_core_clk;
29698c2ecf20Sopenharmony_ci		}
29708c2ecf20Sopenharmony_ci
29718c2ecf20Sopenharmony_ci		nfc->reg_clk = NULL;
29728c2ecf20Sopenharmony_ci	}
29738c2ecf20Sopenharmony_ci
29748c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(nfc->reg_clk);
29758c2ecf20Sopenharmony_ci	if (ret)
29768c2ecf20Sopenharmony_ci		goto unprepare_core_clk;
29778c2ecf20Sopenharmony_ci
29788c2ecf20Sopenharmony_ci	marvell_nfc_disable_int(nfc, NDCR_ALL_INT);
29798c2ecf20Sopenharmony_ci	marvell_nfc_clear_int(nfc, NDCR_ALL_INT);
29808c2ecf20Sopenharmony_ci	ret = devm_request_irq(dev, irq, marvell_nfc_isr,
29818c2ecf20Sopenharmony_ci			       0, "marvell-nfc", nfc);
29828c2ecf20Sopenharmony_ci	if (ret)
29838c2ecf20Sopenharmony_ci		goto unprepare_reg_clk;
29848c2ecf20Sopenharmony_ci
29858c2ecf20Sopenharmony_ci	/* Get NAND controller capabilities */
29868c2ecf20Sopenharmony_ci	if (pdev->id_entry)
29878c2ecf20Sopenharmony_ci		nfc->caps = (void *)pdev->id_entry->driver_data;
29888c2ecf20Sopenharmony_ci	else
29898c2ecf20Sopenharmony_ci		nfc->caps = of_device_get_match_data(&pdev->dev);
29908c2ecf20Sopenharmony_ci
29918c2ecf20Sopenharmony_ci	if (!nfc->caps) {
29928c2ecf20Sopenharmony_ci		dev_err(dev, "Could not retrieve NFC caps\n");
29938c2ecf20Sopenharmony_ci		ret = -EINVAL;
29948c2ecf20Sopenharmony_ci		goto unprepare_reg_clk;
29958c2ecf20Sopenharmony_ci	}
29968c2ecf20Sopenharmony_ci
29978c2ecf20Sopenharmony_ci	/* Init the controller and then probe the chips */
29988c2ecf20Sopenharmony_ci	ret = marvell_nfc_init(nfc);
29998c2ecf20Sopenharmony_ci	if (ret)
30008c2ecf20Sopenharmony_ci		goto unprepare_reg_clk;
30018c2ecf20Sopenharmony_ci
30028c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, nfc);
30038c2ecf20Sopenharmony_ci
30048c2ecf20Sopenharmony_ci	ret = marvell_nand_chips_init(dev, nfc);
30058c2ecf20Sopenharmony_ci	if (ret)
30068c2ecf20Sopenharmony_ci		goto release_dma;
30078c2ecf20Sopenharmony_ci
30088c2ecf20Sopenharmony_ci	return 0;
30098c2ecf20Sopenharmony_ci
30108c2ecf20Sopenharmony_cirelease_dma:
30118c2ecf20Sopenharmony_ci	if (nfc->use_dma)
30128c2ecf20Sopenharmony_ci		dma_release_channel(nfc->dma_chan);
30138c2ecf20Sopenharmony_ciunprepare_reg_clk:
30148c2ecf20Sopenharmony_ci	clk_disable_unprepare(nfc->reg_clk);
30158c2ecf20Sopenharmony_ciunprepare_core_clk:
30168c2ecf20Sopenharmony_ci	clk_disable_unprepare(nfc->core_clk);
30178c2ecf20Sopenharmony_ci
30188c2ecf20Sopenharmony_ci	return ret;
30198c2ecf20Sopenharmony_ci}
30208c2ecf20Sopenharmony_ci
30218c2ecf20Sopenharmony_cistatic int marvell_nfc_remove(struct platform_device *pdev)
30228c2ecf20Sopenharmony_ci{
30238c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = platform_get_drvdata(pdev);
30248c2ecf20Sopenharmony_ci
30258c2ecf20Sopenharmony_ci	marvell_nand_chips_cleanup(nfc);
30268c2ecf20Sopenharmony_ci
30278c2ecf20Sopenharmony_ci	if (nfc->use_dma) {
30288c2ecf20Sopenharmony_ci		dmaengine_terminate_all(nfc->dma_chan);
30298c2ecf20Sopenharmony_ci		dma_release_channel(nfc->dma_chan);
30308c2ecf20Sopenharmony_ci	}
30318c2ecf20Sopenharmony_ci
30328c2ecf20Sopenharmony_ci	clk_disable_unprepare(nfc->reg_clk);
30338c2ecf20Sopenharmony_ci	clk_disable_unprepare(nfc->core_clk);
30348c2ecf20Sopenharmony_ci
30358c2ecf20Sopenharmony_ci	return 0;
30368c2ecf20Sopenharmony_ci}
30378c2ecf20Sopenharmony_ci
30388c2ecf20Sopenharmony_cistatic int __maybe_unused marvell_nfc_suspend(struct device *dev)
30398c2ecf20Sopenharmony_ci{
30408c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = dev_get_drvdata(dev);
30418c2ecf20Sopenharmony_ci	struct marvell_nand_chip *chip;
30428c2ecf20Sopenharmony_ci
30438c2ecf20Sopenharmony_ci	list_for_each_entry(chip, &nfc->chips, node)
30448c2ecf20Sopenharmony_ci		marvell_nfc_wait_ndrun(&chip->chip);
30458c2ecf20Sopenharmony_ci
30468c2ecf20Sopenharmony_ci	clk_disable_unprepare(nfc->reg_clk);
30478c2ecf20Sopenharmony_ci	clk_disable_unprepare(nfc->core_clk);
30488c2ecf20Sopenharmony_ci
30498c2ecf20Sopenharmony_ci	return 0;
30508c2ecf20Sopenharmony_ci}
30518c2ecf20Sopenharmony_ci
30528c2ecf20Sopenharmony_cistatic int __maybe_unused marvell_nfc_resume(struct device *dev)
30538c2ecf20Sopenharmony_ci{
30548c2ecf20Sopenharmony_ci	struct marvell_nfc *nfc = dev_get_drvdata(dev);
30558c2ecf20Sopenharmony_ci	int ret;
30568c2ecf20Sopenharmony_ci
30578c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(nfc->core_clk);
30588c2ecf20Sopenharmony_ci	if (ret < 0)
30598c2ecf20Sopenharmony_ci		return ret;
30608c2ecf20Sopenharmony_ci
30618c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(nfc->reg_clk);
30628c2ecf20Sopenharmony_ci	if (ret < 0) {
30638c2ecf20Sopenharmony_ci		clk_disable_unprepare(nfc->core_clk);
30648c2ecf20Sopenharmony_ci		return ret;
30658c2ecf20Sopenharmony_ci	}
30668c2ecf20Sopenharmony_ci
30678c2ecf20Sopenharmony_ci	/*
30688c2ecf20Sopenharmony_ci	 * Reset nfc->selected_chip so the next command will cause the timing
30698c2ecf20Sopenharmony_ci	 * registers to be restored in marvell_nfc_select_target().
30708c2ecf20Sopenharmony_ci	 */
30718c2ecf20Sopenharmony_ci	nfc->selected_chip = NULL;
30728c2ecf20Sopenharmony_ci
30738c2ecf20Sopenharmony_ci	/* Reset registers that have lost their contents */
30748c2ecf20Sopenharmony_ci	marvell_nfc_reset(nfc);
30758c2ecf20Sopenharmony_ci
30768c2ecf20Sopenharmony_ci	return 0;
30778c2ecf20Sopenharmony_ci}
30788c2ecf20Sopenharmony_ci
30798c2ecf20Sopenharmony_cistatic const struct dev_pm_ops marvell_nfc_pm_ops = {
30808c2ecf20Sopenharmony_ci	SET_SYSTEM_SLEEP_PM_OPS(marvell_nfc_suspend, marvell_nfc_resume)
30818c2ecf20Sopenharmony_ci};
30828c2ecf20Sopenharmony_ci
30838c2ecf20Sopenharmony_cistatic const struct marvell_nfc_caps marvell_armada_8k_nfc_caps = {
30848c2ecf20Sopenharmony_ci	.max_cs_nb = 4,
30858c2ecf20Sopenharmony_ci	.max_rb_nb = 2,
30868c2ecf20Sopenharmony_ci	.need_system_controller = true,
30878c2ecf20Sopenharmony_ci	.is_nfcv2 = true,
30888c2ecf20Sopenharmony_ci};
30898c2ecf20Sopenharmony_ci
30908c2ecf20Sopenharmony_cistatic const struct marvell_nfc_caps marvell_armada370_nfc_caps = {
30918c2ecf20Sopenharmony_ci	.max_cs_nb = 4,
30928c2ecf20Sopenharmony_ci	.max_rb_nb = 2,
30938c2ecf20Sopenharmony_ci	.is_nfcv2 = true,
30948c2ecf20Sopenharmony_ci};
30958c2ecf20Sopenharmony_ci
30968c2ecf20Sopenharmony_cistatic const struct marvell_nfc_caps marvell_pxa3xx_nfc_caps = {
30978c2ecf20Sopenharmony_ci	.max_cs_nb = 2,
30988c2ecf20Sopenharmony_ci	.max_rb_nb = 1,
30998c2ecf20Sopenharmony_ci	.use_dma = true,
31008c2ecf20Sopenharmony_ci};
31018c2ecf20Sopenharmony_ci
31028c2ecf20Sopenharmony_cistatic const struct marvell_nfc_caps marvell_armada_8k_nfc_legacy_caps = {
31038c2ecf20Sopenharmony_ci	.max_cs_nb = 4,
31048c2ecf20Sopenharmony_ci	.max_rb_nb = 2,
31058c2ecf20Sopenharmony_ci	.need_system_controller = true,
31068c2ecf20Sopenharmony_ci	.legacy_of_bindings = true,
31078c2ecf20Sopenharmony_ci	.is_nfcv2 = true,
31088c2ecf20Sopenharmony_ci};
31098c2ecf20Sopenharmony_ci
31108c2ecf20Sopenharmony_cistatic const struct marvell_nfc_caps marvell_armada370_nfc_legacy_caps = {
31118c2ecf20Sopenharmony_ci	.max_cs_nb = 4,
31128c2ecf20Sopenharmony_ci	.max_rb_nb = 2,
31138c2ecf20Sopenharmony_ci	.legacy_of_bindings = true,
31148c2ecf20Sopenharmony_ci	.is_nfcv2 = true,
31158c2ecf20Sopenharmony_ci};
31168c2ecf20Sopenharmony_ci
31178c2ecf20Sopenharmony_cistatic const struct marvell_nfc_caps marvell_pxa3xx_nfc_legacy_caps = {
31188c2ecf20Sopenharmony_ci	.max_cs_nb = 2,
31198c2ecf20Sopenharmony_ci	.max_rb_nb = 1,
31208c2ecf20Sopenharmony_ci	.legacy_of_bindings = true,
31218c2ecf20Sopenharmony_ci	.use_dma = true,
31228c2ecf20Sopenharmony_ci};
31238c2ecf20Sopenharmony_ci
31248c2ecf20Sopenharmony_cistatic const struct platform_device_id marvell_nfc_platform_ids[] = {
31258c2ecf20Sopenharmony_ci	{
31268c2ecf20Sopenharmony_ci		.name = "pxa3xx-nand",
31278c2ecf20Sopenharmony_ci		.driver_data = (kernel_ulong_t)&marvell_pxa3xx_nfc_legacy_caps,
31288c2ecf20Sopenharmony_ci	},
31298c2ecf20Sopenharmony_ci	{ /* sentinel */ },
31308c2ecf20Sopenharmony_ci};
31318c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(platform, marvell_nfc_platform_ids);
31328c2ecf20Sopenharmony_ci
31338c2ecf20Sopenharmony_cistatic const struct of_device_id marvell_nfc_of_ids[] = {
31348c2ecf20Sopenharmony_ci	{
31358c2ecf20Sopenharmony_ci		.compatible = "marvell,armada-8k-nand-controller",
31368c2ecf20Sopenharmony_ci		.data = &marvell_armada_8k_nfc_caps,
31378c2ecf20Sopenharmony_ci	},
31388c2ecf20Sopenharmony_ci	{
31398c2ecf20Sopenharmony_ci		.compatible = "marvell,armada370-nand-controller",
31408c2ecf20Sopenharmony_ci		.data = &marvell_armada370_nfc_caps,
31418c2ecf20Sopenharmony_ci	},
31428c2ecf20Sopenharmony_ci	{
31438c2ecf20Sopenharmony_ci		.compatible = "marvell,pxa3xx-nand-controller",
31448c2ecf20Sopenharmony_ci		.data = &marvell_pxa3xx_nfc_caps,
31458c2ecf20Sopenharmony_ci	},
31468c2ecf20Sopenharmony_ci	/* Support for old/deprecated bindings: */
31478c2ecf20Sopenharmony_ci	{
31488c2ecf20Sopenharmony_ci		.compatible = "marvell,armada-8k-nand",
31498c2ecf20Sopenharmony_ci		.data = &marvell_armada_8k_nfc_legacy_caps,
31508c2ecf20Sopenharmony_ci	},
31518c2ecf20Sopenharmony_ci	{
31528c2ecf20Sopenharmony_ci		.compatible = "marvell,armada370-nand",
31538c2ecf20Sopenharmony_ci		.data = &marvell_armada370_nfc_legacy_caps,
31548c2ecf20Sopenharmony_ci	},
31558c2ecf20Sopenharmony_ci	{
31568c2ecf20Sopenharmony_ci		.compatible = "marvell,pxa3xx-nand",
31578c2ecf20Sopenharmony_ci		.data = &marvell_pxa3xx_nfc_legacy_caps,
31588c2ecf20Sopenharmony_ci	},
31598c2ecf20Sopenharmony_ci	{ /* sentinel */ },
31608c2ecf20Sopenharmony_ci};
31618c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, marvell_nfc_of_ids);
31628c2ecf20Sopenharmony_ci
31638c2ecf20Sopenharmony_cistatic struct platform_driver marvell_nfc_driver = {
31648c2ecf20Sopenharmony_ci	.driver	= {
31658c2ecf20Sopenharmony_ci		.name		= "marvell-nfc",
31668c2ecf20Sopenharmony_ci		.of_match_table = marvell_nfc_of_ids,
31678c2ecf20Sopenharmony_ci		.pm		= &marvell_nfc_pm_ops,
31688c2ecf20Sopenharmony_ci	},
31698c2ecf20Sopenharmony_ci	.id_table = marvell_nfc_platform_ids,
31708c2ecf20Sopenharmony_ci	.probe = marvell_nfc_probe,
31718c2ecf20Sopenharmony_ci	.remove	= marvell_nfc_remove,
31728c2ecf20Sopenharmony_ci};
31738c2ecf20Sopenharmony_cimodule_platform_driver(marvell_nfc_driver);
31748c2ecf20Sopenharmony_ci
31758c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
31768c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Marvell NAND controller driver");
3177