xref: /openbmc/linux/drivers/mtd/nand/raw/marvell_nand.c (revision 9c6d26df1fae6ad4718d51c48e6517913304ed27)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Marvell NAND flash controller driver
4  *
5  * Copyright (C) 2017 Marvell
6  * Author: Miquel RAYNAL <miquel.raynal@free-electrons.com>
7  *
8  */
9 
10 #include <linux/module.h>
11 #include <linux/clk.h>
12 #include <linux/mtd/rawnand.h>
13 #include <linux/of_platform.h>
14 #include <linux/iopoll.h>
15 #include <linux/interrupt.h>
16 #include <linux/slab.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/regmap.h>
19 #include <asm/unaligned.h>
20 
21 #include <linux/dmaengine.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/dma/pxa-dma.h>
24 #include <linux/platform_data/mtd-nand-pxa3xx.h>
25 
26 /* Data FIFO granularity, FIFO reads/writes must be a multiple of this length */
27 #define FIFO_DEPTH		8
28 #define FIFO_REP(x)		(x / sizeof(u32))
29 #define BCH_SEQ_READS		(32 / FIFO_DEPTH)
30 /* NFC does not support transfers of larger chunks at a time */
31 #define MAX_CHUNK_SIZE		2112
32 /* NFCv1 cannot read more that 7 bytes of ID */
33 #define NFCV1_READID_LEN	7
34 /* Polling is done at a pace of POLL_PERIOD us until POLL_TIMEOUT is reached */
35 #define POLL_PERIOD		0
36 #define POLL_TIMEOUT		100000
37 /* Interrupt maximum wait period in ms */
38 #define IRQ_TIMEOUT		1000
39 /* Latency in clock cycles between SoC pins and NFC logic */
40 #define MIN_RD_DEL_CNT		3
41 /* Maximum number of contiguous address cycles */
42 #define MAX_ADDRESS_CYC_NFCV1	5
43 #define MAX_ADDRESS_CYC_NFCV2	7
44 /* System control registers/bits to enable the NAND controller on some SoCs */
45 #define GENCONF_SOC_DEVICE_MUX	0x208
46 #define GENCONF_SOC_DEVICE_MUX_NFC_EN BIT(0)
47 #define GENCONF_SOC_DEVICE_MUX_ECC_CLK_RST BIT(20)
48 #define GENCONF_SOC_DEVICE_MUX_ECC_CORE_RST BIT(21)
49 #define GENCONF_SOC_DEVICE_MUX_NFC_INT_EN BIT(25)
50 #define GENCONF_CLK_GATING_CTRL	0x220
51 #define GENCONF_CLK_GATING_CTRL_ND_GATE BIT(2)
52 #define GENCONF_ND_CLK_CTRL	0x700
53 #define GENCONF_ND_CLK_CTRL_EN	BIT(0)
54 
55 /* NAND controller data flash control register */
56 #define NDCR			0x00
57 #define NDCR_ALL_INT		GENMASK(11, 0)
58 #define NDCR_CS1_CMDDM		BIT(7)
59 #define NDCR_CS0_CMDDM		BIT(8)
60 #define NDCR_RDYM		BIT(11)
61 #define NDCR_ND_ARB_EN		BIT(12)
62 #define NDCR_RA_START		BIT(15)
63 #define NDCR_RD_ID_CNT(x)	(min_t(unsigned int, x, 0x7) << 16)
64 #define NDCR_PAGE_SZ(x)		(x >= 2048 ? BIT(24) : 0)
65 #define NDCR_DWIDTH_M		BIT(26)
66 #define NDCR_DWIDTH_C		BIT(27)
67 #define NDCR_ND_RUN		BIT(28)
68 #define NDCR_DMA_EN		BIT(29)
69 #define NDCR_ECC_EN		BIT(30)
70 #define NDCR_SPARE_EN		BIT(31)
71 #define NDCR_GENERIC_FIELDS_MASK (~(NDCR_RA_START | NDCR_PAGE_SZ(2048) | \
72 				    NDCR_DWIDTH_M | NDCR_DWIDTH_C))
73 
74 /* NAND interface timing parameter 0 register */
75 #define NDTR0			0x04
76 #define NDTR0_TRP(x)		((min_t(unsigned int, x, 0xF) & 0x7) << 0)
77 #define NDTR0_TRH(x)		(min_t(unsigned int, x, 0x7) << 3)
78 #define NDTR0_ETRP(x)		((min_t(unsigned int, x, 0xF) & 0x8) << 3)
79 #define NDTR0_SEL_NRE_EDGE	BIT(7)
80 #define NDTR0_TWP(x)		(min_t(unsigned int, x, 0x7) << 8)
81 #define NDTR0_TWH(x)		(min_t(unsigned int, x, 0x7) << 11)
82 #define NDTR0_TCS(x)		(min_t(unsigned int, x, 0x7) << 16)
83 #define NDTR0_TCH(x)		(min_t(unsigned int, x, 0x7) << 19)
84 #define NDTR0_RD_CNT_DEL(x)	(min_t(unsigned int, x, 0xF) << 22)
85 #define NDTR0_SELCNTR		BIT(26)
86 #define NDTR0_TADL(x)		(min_t(unsigned int, x, 0x1F) << 27)
87 
88 /* NAND interface timing parameter 1 register */
89 #define NDTR1			0x0C
90 #define NDTR1_TAR(x)		(min_t(unsigned int, x, 0xF) << 0)
91 #define NDTR1_TWHR(x)		(min_t(unsigned int, x, 0xF) << 4)
92 #define NDTR1_TRHW(x)		(min_t(unsigned int, x / 16, 0x3) << 8)
93 #define NDTR1_PRESCALE		BIT(14)
94 #define NDTR1_WAIT_MODE		BIT(15)
95 #define NDTR1_TR(x)		(min_t(unsigned int, x, 0xFFFF) << 16)
96 
97 /* NAND controller status register */
98 #define NDSR			0x14
99 #define NDSR_WRCMDREQ		BIT(0)
100 #define NDSR_RDDREQ		BIT(1)
101 #define NDSR_WRDREQ		BIT(2)
102 #define NDSR_CORERR		BIT(3)
103 #define NDSR_UNCERR		BIT(4)
104 #define NDSR_CMDD(cs)		BIT(8 - cs)
105 #define NDSR_RDY(rb)		BIT(11 + rb)
106 #define NDSR_ERRCNT(x)		((x >> 16) & 0x1F)
107 
108 /* NAND ECC control register */
109 #define NDECCCTRL		0x28
110 #define NDECCCTRL_BCH_EN	BIT(0)
111 
112 /* NAND controller data buffer register */
113 #define NDDB			0x40
114 
115 /* NAND controller command buffer 0 register */
116 #define NDCB0			0x48
117 #define NDCB0_CMD1(x)		((x & 0xFF) << 0)
118 #define NDCB0_CMD2(x)		((x & 0xFF) << 8)
119 #define NDCB0_ADDR_CYC(x)	((x & 0x7) << 16)
120 #define NDCB0_ADDR_GET_NUM_CYC(x) (((x) >> 16) & 0x7)
121 #define NDCB0_DBC		BIT(19)
122 #define NDCB0_CMD_TYPE(x)	((x & 0x7) << 21)
123 #define NDCB0_CSEL		BIT(24)
124 #define NDCB0_RDY_BYP		BIT(27)
125 #define NDCB0_LEN_OVRD		BIT(28)
126 #define NDCB0_CMD_XTYPE(x)	((x & 0x7) << 29)
127 
128 /* NAND controller command buffer 1 register */
129 #define NDCB1			0x4C
130 #define NDCB1_COLS(x)		((x & 0xFFFF) << 0)
131 #define NDCB1_ADDRS_PAGE(x)	(x << 16)
132 
133 /* NAND controller command buffer 2 register */
134 #define NDCB2			0x50
135 #define NDCB2_ADDR5_PAGE(x)	(((x >> 16) & 0xFF) << 0)
136 #define NDCB2_ADDR5_CYC(x)	((x & 0xFF) << 0)
137 
138 /* NAND controller command buffer 3 register */
139 #define NDCB3			0x54
140 #define NDCB3_ADDR6_CYC(x)	((x & 0xFF) << 16)
141 #define NDCB3_ADDR7_CYC(x)	((x & 0xFF) << 24)
142 
143 /* NAND controller command buffer 0 register 'type' and 'xtype' fields */
144 #define TYPE_READ		0
145 #define TYPE_WRITE		1
146 #define TYPE_ERASE		2
147 #define TYPE_READ_ID		3
148 #define TYPE_STATUS		4
149 #define TYPE_RESET		5
150 #define TYPE_NAKED_CMD		6
151 #define TYPE_NAKED_ADDR		7
152 #define TYPE_MASK		7
153 #define XTYPE_MONOLITHIC_RW	0
154 #define XTYPE_LAST_NAKED_RW	1
155 #define XTYPE_FINAL_COMMAND	3
156 #define XTYPE_READ		4
157 #define XTYPE_WRITE_DISPATCH	4
158 #define XTYPE_NAKED_RW		5
159 #define XTYPE_COMMAND_DISPATCH	6
160 #define XTYPE_MASK		7
161 
162 /**
163  * Marvell ECC engine works differently than the others, in order to limit the
164  * size of the IP, hardware engineers chose to set a fixed strength at 16 bits
165  * per subpage, and depending on a the desired strength needed by the NAND chip,
166  * a particular layout mixing data/spare/ecc is defined, with a possible last
167  * chunk smaller that the others.
168  *
169  * @writesize:		Full page size on which the layout applies
170  * @chunk:		Desired ECC chunk size on which the layout applies
171  * @strength:		Desired ECC strength (per chunk size bytes) on which the
172  *			layout applies
173  * @nchunks:		Total number of chunks
174  * @full_chunk_cnt:	Number of full-sized chunks, which is the number of
175  *			repetitions of the pattern:
176  *			(data_bytes + spare_bytes + ecc_bytes).
177  * @data_bytes:		Number of data bytes per chunk
178  * @spare_bytes:	Number of spare bytes per chunk
179  * @ecc_bytes:		Number of ecc bytes per chunk
180  * @last_data_bytes:	Number of data bytes in the last chunk
181  * @last_spare_bytes:	Number of spare bytes in the last chunk
182  * @last_ecc_bytes:	Number of ecc bytes in the last chunk
183  */
184 struct marvell_hw_ecc_layout {
185 	/* Constraints */
186 	int writesize;
187 	int chunk;
188 	int strength;
189 	/* Corresponding layout */
190 	int nchunks;
191 	int full_chunk_cnt;
192 	int data_bytes;
193 	int spare_bytes;
194 	int ecc_bytes;
195 	int last_data_bytes;
196 	int last_spare_bytes;
197 	int last_ecc_bytes;
198 };
199 
200 #define MARVELL_LAYOUT(ws, dc, ds, nc, fcc, db, sb, eb, ldb, lsb, leb)	\
201 	{								\
202 		.writesize = ws,					\
203 		.chunk = dc,						\
204 		.strength = ds,						\
205 		.nchunks = nc,						\
206 		.full_chunk_cnt = fcc,					\
207 		.data_bytes = db,					\
208 		.spare_bytes = sb,					\
209 		.ecc_bytes = eb,					\
210 		.last_data_bytes = ldb,					\
211 		.last_spare_bytes = lsb,				\
212 		.last_ecc_bytes = leb,					\
213 	}
214 
215 /* Layouts explained in AN-379_Marvell_SoC_NFC_ECC */
216 static const struct marvell_hw_ecc_layout marvell_nfc_layouts[] = {
217 	MARVELL_LAYOUT(  512,   512,  1,  1,  1,  512,  8,  8,  0,  0,  0),
218 	MARVELL_LAYOUT( 2048,   512,  1,  1,  1, 2048, 40, 24,  0,  0,  0),
219 	MARVELL_LAYOUT( 2048,   512,  4,  1,  1, 2048, 32, 30,  0,  0,  0),
220 	MARVELL_LAYOUT( 4096,   512,  4,  2,  2, 2048, 32, 30,  0,  0,  0),
221 	MARVELL_LAYOUT( 4096,   512,  8,  5,  4, 1024,  0, 30,  0, 64, 30),
222 };
223 
224 /**
225  * The Nand Flash Controller has up to 4 CE and 2 RB pins. The CE selection
226  * is made by a field in NDCB0 register, and in another field in NDCB2 register.
227  * The datasheet describes the logic with an error: ADDR5 field is once
228  * declared at the beginning of NDCB2, and another time at its end. Because the
229  * ADDR5 field of NDCB2 may be used by other bytes, it would be more logical
230  * to use the last bit of this field instead of the first ones.
231  *
232  * @cs:			Wanted CE lane.
233  * @ndcb0_csel:		Value of the NDCB0 register with or without the flag
234  *			selecting the wanted CE lane. This is set once when
235  *			the Device Tree is probed.
236  * @rb:			Ready/Busy pin for the flash chip
237  */
238 struct marvell_nand_chip_sel {
239 	unsigned int cs;
240 	u32 ndcb0_csel;
241 	unsigned int rb;
242 };
243 
244 /**
245  * NAND chip structure: stores NAND chip device related information
246  *
247  * @chip:		Base NAND chip structure
248  * @node:		Used to store NAND chips into a list
249  * @layout		NAND layout when using hardware ECC
250  * @ndcr:		Controller register value for this NAND chip
251  * @ndtr0:		Timing registers 0 value for this NAND chip
252  * @ndtr1:		Timing registers 1 value for this NAND chip
253  * @selected_die:	Current active CS
254  * @nsels:		Number of CS lines required by the NAND chip
255  * @sels:		Array of CS lines descriptions
256  */
257 struct marvell_nand_chip {
258 	struct nand_chip chip;
259 	struct list_head node;
260 	const struct marvell_hw_ecc_layout *layout;
261 	u32 ndcr;
262 	u32 ndtr0;
263 	u32 ndtr1;
264 	int addr_cyc;
265 	int selected_die;
266 	unsigned int nsels;
267 	struct marvell_nand_chip_sel sels[0];
268 };
269 
270 static inline struct marvell_nand_chip *to_marvell_nand(struct nand_chip *chip)
271 {
272 	return container_of(chip, struct marvell_nand_chip, chip);
273 }
274 
275 static inline struct marvell_nand_chip_sel *to_nand_sel(struct marvell_nand_chip
276 							*nand)
277 {
278 	return &nand->sels[nand->selected_die];
279 }
280 
281 /**
282  * NAND controller capabilities for distinction between compatible strings
283  *
284  * @max_cs_nb:		Number of Chip Select lines available
285  * @max_rb_nb:		Number of Ready/Busy lines available
286  * @need_system_controller: Indicates if the SoC needs to have access to the
287  *                      system controller (ie. to enable the NAND controller)
288  * @legacy_of_bindings:	Indicates if DT parsing must be done using the old
289  *			fashion way
290  * @is_nfcv2:		NFCv2 has numerous enhancements compared to NFCv1, ie.
291  *			BCH error detection and correction algorithm,
292  *			NDCB3 register has been added
293  * @use_dma:		Use dma for data transfers
294  */
295 struct marvell_nfc_caps {
296 	unsigned int max_cs_nb;
297 	unsigned int max_rb_nb;
298 	bool need_system_controller;
299 	bool legacy_of_bindings;
300 	bool is_nfcv2;
301 	bool use_dma;
302 };
303 
304 /**
305  * NAND controller structure: stores Marvell NAND controller information
306  *
307  * @controller:		Base controller structure
308  * @dev:		Parent device (used to print error messages)
309  * @regs:		NAND controller registers
310  * @core_clk:		Core clock
311  * @reg_clk:		Regiters clock
312  * @complete:		Completion object to wait for NAND controller events
313  * @assigned_cs:	Bitmask describing already assigned CS lines
314  * @chips:		List containing all the NAND chips attached to
315  *			this NAND controller
316  * @caps:		NAND controller capabilities for each compatible string
317  * @dma_chan:		DMA channel (NFCv1 only)
318  * @dma_buf:		32-bit aligned buffer for DMA transfers (NFCv1 only)
319  */
320 struct marvell_nfc {
321 	struct nand_hw_control controller;
322 	struct device *dev;
323 	void __iomem *regs;
324 	struct clk *core_clk;
325 	struct clk *reg_clk;
326 	struct completion complete;
327 	unsigned long assigned_cs;
328 	struct list_head chips;
329 	struct nand_chip *selected_chip;
330 	const struct marvell_nfc_caps *caps;
331 
332 	/* DMA (NFCv1 only) */
333 	bool use_dma;
334 	struct dma_chan *dma_chan;
335 	u8 *dma_buf;
336 };
337 
338 static inline struct marvell_nfc *to_marvell_nfc(struct nand_hw_control *ctrl)
339 {
340 	return container_of(ctrl, struct marvell_nfc, controller);
341 }
342 
343 /**
344  * NAND controller timings expressed in NAND Controller clock cycles
345  *
346  * @tRP:		ND_nRE pulse width
347  * @tRH:		ND_nRE high duration
348  * @tWP:		ND_nWE pulse time
349  * @tWH:		ND_nWE high duration
350  * @tCS:		Enable signal setup time
351  * @tCH:		Enable signal hold time
352  * @tADL:		Address to write data delay
353  * @tAR:		ND_ALE low to ND_nRE low delay
354  * @tWHR:		ND_nWE high to ND_nRE low for status read
355  * @tRHW:		ND_nRE high duration, read to write delay
356  * @tR:			ND_nWE high to ND_nRE low for read
357  */
358 struct marvell_nfc_timings {
359 	/* NDTR0 fields */
360 	unsigned int tRP;
361 	unsigned int tRH;
362 	unsigned int tWP;
363 	unsigned int tWH;
364 	unsigned int tCS;
365 	unsigned int tCH;
366 	unsigned int tADL;
367 	/* NDTR1 fields */
368 	unsigned int tAR;
369 	unsigned int tWHR;
370 	unsigned int tRHW;
371 	unsigned int tR;
372 };
373 
374 /**
375  * Derives a duration in numbers of clock cycles.
376  *
377  * @ps: Duration in pico-seconds
378  * @period_ns:  Clock period in nano-seconds
379  *
380  * Convert the duration in nano-seconds, then divide by the period and
381  * return the number of clock periods.
382  */
383 #define TO_CYCLES(ps, period_ns) (DIV_ROUND_UP(ps / 1000, period_ns))
384 #define TO_CYCLES64(ps, period_ns) (DIV_ROUND_UP_ULL(div_u64(ps, 1000), \
385 						     period_ns))
386 
387 /**
388  * NAND driver structure filled during the parsing of the ->exec_op() subop
389  * subset of instructions.
390  *
391  * @ndcb:		Array of values written to NDCBx registers
392  * @cle_ale_delay_ns:	Optional delay after the last CMD or ADDR cycle
393  * @rdy_timeout_ms:	Timeout for waits on Ready/Busy pin
394  * @rdy_delay_ns:	Optional delay after waiting for the RB pin
395  * @data_delay_ns:	Optional delay after the data xfer
396  * @data_instr_idx:	Index of the data instruction in the subop
397  * @data_instr:		Pointer to the data instruction in the subop
398  */
399 struct marvell_nfc_op {
400 	u32 ndcb[4];
401 	unsigned int cle_ale_delay_ns;
402 	unsigned int rdy_timeout_ms;
403 	unsigned int rdy_delay_ns;
404 	unsigned int data_delay_ns;
405 	unsigned int data_instr_idx;
406 	const struct nand_op_instr *data_instr;
407 };
408 
409 /*
410  * Internal helper to conditionnally apply a delay (from the above structure,
411  * most of the time).
412  */
413 static void cond_delay(unsigned int ns)
414 {
415 	if (!ns)
416 		return;
417 
418 	if (ns < 10000)
419 		ndelay(ns);
420 	else
421 		udelay(DIV_ROUND_UP(ns, 1000));
422 }
423 
424 /*
425  * The controller has many flags that could generate interrupts, most of them
426  * are disabled and polling is used. For the very slow signals, using interrupts
427  * may relax the CPU charge.
428  */
429 static void marvell_nfc_disable_int(struct marvell_nfc *nfc, u32 int_mask)
430 {
431 	u32 reg;
432 
433 	/* Writing 1 disables the interrupt */
434 	reg = readl_relaxed(nfc->regs + NDCR);
435 	writel_relaxed(reg | int_mask, nfc->regs + NDCR);
436 }
437 
438 static void marvell_nfc_enable_int(struct marvell_nfc *nfc, u32 int_mask)
439 {
440 	u32 reg;
441 
442 	/* Writing 0 enables the interrupt */
443 	reg = readl_relaxed(nfc->regs + NDCR);
444 	writel_relaxed(reg & ~int_mask, nfc->regs + NDCR);
445 }
446 
447 static void marvell_nfc_clear_int(struct marvell_nfc *nfc, u32 int_mask)
448 {
449 	writel_relaxed(int_mask, nfc->regs + NDSR);
450 }
451 
452 static void marvell_nfc_force_byte_access(struct nand_chip *chip,
453 					  bool force_8bit)
454 {
455 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
456 	u32 ndcr;
457 
458 	/*
459 	 * Callers of this function do not verify if the NAND is using a 16-bit
460 	 * an 8-bit bus for normal operations, so we need to take care of that
461 	 * here by leaving the configuration unchanged if the NAND does not have
462 	 * the NAND_BUSWIDTH_16 flag set.
463 	 */
464 	if (!(chip->options & NAND_BUSWIDTH_16))
465 		return;
466 
467 	ndcr = readl_relaxed(nfc->regs + NDCR);
468 
469 	if (force_8bit)
470 		ndcr &= ~(NDCR_DWIDTH_M | NDCR_DWIDTH_C);
471 	else
472 		ndcr |= NDCR_DWIDTH_M | NDCR_DWIDTH_C;
473 
474 	writel_relaxed(ndcr, nfc->regs + NDCR);
475 }
476 
477 static int marvell_nfc_wait_ndrun(struct nand_chip *chip)
478 {
479 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
480 	u32 val;
481 	int ret;
482 
483 	/*
484 	 * The command is being processed, wait for the ND_RUN bit to be
485 	 * cleared by the NFC. If not, we must clear it by hand.
486 	 */
487 	ret = readl_relaxed_poll_timeout(nfc->regs + NDCR, val,
488 					 (val & NDCR_ND_RUN) == 0,
489 					 POLL_PERIOD, POLL_TIMEOUT);
490 	if (ret) {
491 		dev_err(nfc->dev, "Timeout on NAND controller run mode\n");
492 		writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
493 			       nfc->regs + NDCR);
494 		return ret;
495 	}
496 
497 	return 0;
498 }
499 
500 /*
501  * Any time a command has to be sent to the controller, the following sequence
502  * has to be followed:
503  * - call marvell_nfc_prepare_cmd()
504  *      -> activate the ND_RUN bit that will kind of 'start a job'
505  *      -> wait the signal indicating the NFC is waiting for a command
506  * - send the command (cmd and address cycles)
507  * - enventually send or receive the data
508  * - call marvell_nfc_end_cmd() with the corresponding flag
509  *      -> wait the flag to be triggered or cancel the job with a timeout
510  *
511  * The following helpers are here to factorize the code a bit so that
512  * specialized functions responsible for executing the actual NAND
513  * operations do not have to replicate the same code blocks.
514  */
515 static int marvell_nfc_prepare_cmd(struct nand_chip *chip)
516 {
517 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
518 	u32 ndcr, val;
519 	int ret;
520 
521 	/* Poll ND_RUN and clear NDSR before issuing any command */
522 	ret = marvell_nfc_wait_ndrun(chip);
523 	if (ret) {
524 		dev_err(nfc->dev, "Last operation did not succeed\n");
525 		return ret;
526 	}
527 
528 	ndcr = readl_relaxed(nfc->regs + NDCR);
529 	writel_relaxed(readl(nfc->regs + NDSR), nfc->regs + NDSR);
530 
531 	/* Assert ND_RUN bit and wait the NFC to be ready */
532 	writel_relaxed(ndcr | NDCR_ND_RUN, nfc->regs + NDCR);
533 	ret = readl_relaxed_poll_timeout(nfc->regs + NDSR, val,
534 					 val & NDSR_WRCMDREQ,
535 					 POLL_PERIOD, POLL_TIMEOUT);
536 	if (ret) {
537 		dev_err(nfc->dev, "Timeout on WRCMDRE\n");
538 		return -ETIMEDOUT;
539 	}
540 
541 	/* Command may be written, clear WRCMDREQ status bit */
542 	writel_relaxed(NDSR_WRCMDREQ, nfc->regs + NDSR);
543 
544 	return 0;
545 }
546 
547 static void marvell_nfc_send_cmd(struct nand_chip *chip,
548 				 struct marvell_nfc_op *nfc_op)
549 {
550 	struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
551 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
552 
553 	dev_dbg(nfc->dev, "\nNDCR:  0x%08x\n"
554 		"NDCB0: 0x%08x\nNDCB1: 0x%08x\nNDCB2: 0x%08x\nNDCB3: 0x%08x\n",
555 		(u32)readl_relaxed(nfc->regs + NDCR), nfc_op->ndcb[0],
556 		nfc_op->ndcb[1], nfc_op->ndcb[2], nfc_op->ndcb[3]);
557 
558 	writel_relaxed(to_nand_sel(marvell_nand)->ndcb0_csel | nfc_op->ndcb[0],
559 		       nfc->regs + NDCB0);
560 	writel_relaxed(nfc_op->ndcb[1], nfc->regs + NDCB0);
561 	writel(nfc_op->ndcb[2], nfc->regs + NDCB0);
562 
563 	/*
564 	 * Write NDCB0 four times only if LEN_OVRD is set or if ADDR6 or ADDR7
565 	 * fields are used (only available on NFCv2).
566 	 */
567 	if (nfc_op->ndcb[0] & NDCB0_LEN_OVRD ||
568 	    NDCB0_ADDR_GET_NUM_CYC(nfc_op->ndcb[0]) >= 6) {
569 		if (!WARN_ON_ONCE(!nfc->caps->is_nfcv2))
570 			writel(nfc_op->ndcb[3], nfc->regs + NDCB0);
571 	}
572 }
573 
574 static int marvell_nfc_end_cmd(struct nand_chip *chip, int flag,
575 			       const char *label)
576 {
577 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
578 	u32 val;
579 	int ret;
580 
581 	ret = readl_relaxed_poll_timeout(nfc->regs + NDSR, val,
582 					 val & flag,
583 					 POLL_PERIOD, POLL_TIMEOUT);
584 
585 	if (ret) {
586 		dev_err(nfc->dev, "Timeout on %s (NDSR: 0x%08x)\n",
587 			label, val);
588 		if (nfc->dma_chan)
589 			dmaengine_terminate_all(nfc->dma_chan);
590 		return ret;
591 	}
592 
593 	/*
594 	 * DMA function uses this helper to poll on CMDD bits without wanting
595 	 * them to be cleared.
596 	 */
597 	if (nfc->use_dma && (readl_relaxed(nfc->regs + NDCR) & NDCR_DMA_EN))
598 		return 0;
599 
600 	writel_relaxed(flag, nfc->regs + NDSR);
601 
602 	return 0;
603 }
604 
605 static int marvell_nfc_wait_cmdd(struct nand_chip *chip)
606 {
607 	struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
608 	int cs_flag = NDSR_CMDD(to_nand_sel(marvell_nand)->ndcb0_csel);
609 
610 	return marvell_nfc_end_cmd(chip, cs_flag, "CMDD");
611 }
612 
613 static int marvell_nfc_wait_op(struct nand_chip *chip, unsigned int timeout_ms)
614 {
615 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
616 	int ret;
617 
618 	/* Timeout is expressed in ms */
619 	if (!timeout_ms)
620 		timeout_ms = IRQ_TIMEOUT;
621 
622 	init_completion(&nfc->complete);
623 
624 	marvell_nfc_enable_int(nfc, NDCR_RDYM);
625 	ret = wait_for_completion_timeout(&nfc->complete,
626 					  msecs_to_jiffies(timeout_ms));
627 	marvell_nfc_disable_int(nfc, NDCR_RDYM);
628 	marvell_nfc_clear_int(nfc, NDSR_RDY(0) | NDSR_RDY(1));
629 	if (!ret) {
630 		dev_err(nfc->dev, "Timeout waiting for RB signal\n");
631 		return -ETIMEDOUT;
632 	}
633 
634 	return 0;
635 }
636 
637 static void marvell_nfc_select_chip(struct mtd_info *mtd, int die_nr)
638 {
639 	struct nand_chip *chip = mtd_to_nand(mtd);
640 	struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
641 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
642 	u32 ndcr_generic;
643 
644 	if (chip == nfc->selected_chip && die_nr == marvell_nand->selected_die)
645 		return;
646 
647 	if (die_nr < 0 || die_nr >= marvell_nand->nsels) {
648 		nfc->selected_chip = NULL;
649 		marvell_nand->selected_die = -1;
650 		return;
651 	}
652 
653 	/*
654 	 * Do not change the timing registers when using the DT property
655 	 * marvell,nand-keep-config; in that case ->ndtr0 and ->ndtr1 from the
656 	 * marvell_nand structure are supposedly empty.
657 	 */
658 	writel_relaxed(marvell_nand->ndtr0, nfc->regs + NDTR0);
659 	writel_relaxed(marvell_nand->ndtr1, nfc->regs + NDTR1);
660 
661 	/*
662 	 * Reset the NDCR register to a clean state for this particular chip,
663 	 * also clear ND_RUN bit.
664 	 */
665 	ndcr_generic = readl_relaxed(nfc->regs + NDCR) &
666 		       NDCR_GENERIC_FIELDS_MASK & ~NDCR_ND_RUN;
667 	writel_relaxed(ndcr_generic | marvell_nand->ndcr, nfc->regs + NDCR);
668 
669 	/* Also reset the interrupt status register */
670 	marvell_nfc_clear_int(nfc, NDCR_ALL_INT);
671 
672 	nfc->selected_chip = chip;
673 	marvell_nand->selected_die = die_nr;
674 }
675 
676 static irqreturn_t marvell_nfc_isr(int irq, void *dev_id)
677 {
678 	struct marvell_nfc *nfc = dev_id;
679 	u32 st = readl_relaxed(nfc->regs + NDSR);
680 	u32 ien = (~readl_relaxed(nfc->regs + NDCR)) & NDCR_ALL_INT;
681 
682 	/*
683 	 * RDY interrupt mask is one bit in NDCR while there are two status
684 	 * bit in NDSR (RDY[cs0/cs2] and RDY[cs1/cs3]).
685 	 */
686 	if (st & NDSR_RDY(1))
687 		st |= NDSR_RDY(0);
688 
689 	if (!(st & ien))
690 		return IRQ_NONE;
691 
692 	marvell_nfc_disable_int(nfc, st & NDCR_ALL_INT);
693 
694 	if (!(st & (NDSR_RDDREQ | NDSR_WRDREQ | NDSR_WRCMDREQ)))
695 		complete(&nfc->complete);
696 
697 	return IRQ_HANDLED;
698 }
699 
700 /* HW ECC related functions */
701 static void marvell_nfc_enable_hw_ecc(struct nand_chip *chip)
702 {
703 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
704 	u32 ndcr = readl_relaxed(nfc->regs + NDCR);
705 
706 	if (!(ndcr & NDCR_ECC_EN)) {
707 		writel_relaxed(ndcr | NDCR_ECC_EN, nfc->regs + NDCR);
708 
709 		/*
710 		 * When enabling BCH, set threshold to 0 to always know the
711 		 * number of corrected bitflips.
712 		 */
713 		if (chip->ecc.algo == NAND_ECC_BCH)
714 			writel_relaxed(NDECCCTRL_BCH_EN, nfc->regs + NDECCCTRL);
715 	}
716 }
717 
718 static void marvell_nfc_disable_hw_ecc(struct nand_chip *chip)
719 {
720 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
721 	u32 ndcr = readl_relaxed(nfc->regs + NDCR);
722 
723 	if (ndcr & NDCR_ECC_EN) {
724 		writel_relaxed(ndcr & ~NDCR_ECC_EN, nfc->regs + NDCR);
725 		if (chip->ecc.algo == NAND_ECC_BCH)
726 			writel_relaxed(0, nfc->regs + NDECCCTRL);
727 	}
728 }
729 
730 /* DMA related helpers */
731 static void marvell_nfc_enable_dma(struct marvell_nfc *nfc)
732 {
733 	u32 reg;
734 
735 	reg = readl_relaxed(nfc->regs + NDCR);
736 	writel_relaxed(reg | NDCR_DMA_EN, nfc->regs + NDCR);
737 }
738 
739 static void marvell_nfc_disable_dma(struct marvell_nfc *nfc)
740 {
741 	u32 reg;
742 
743 	reg = readl_relaxed(nfc->regs + NDCR);
744 	writel_relaxed(reg & ~NDCR_DMA_EN, nfc->regs + NDCR);
745 }
746 
747 /* Read/write PIO/DMA accessors */
748 static int marvell_nfc_xfer_data_dma(struct marvell_nfc *nfc,
749 				     enum dma_data_direction direction,
750 				     unsigned int len)
751 {
752 	unsigned int dma_len = min_t(int, ALIGN(len, 32), MAX_CHUNK_SIZE);
753 	struct dma_async_tx_descriptor *tx;
754 	struct scatterlist sg;
755 	dma_cookie_t cookie;
756 	int ret;
757 
758 	marvell_nfc_enable_dma(nfc);
759 	/* Prepare the DMA transfer */
760 	sg_init_one(&sg, nfc->dma_buf, dma_len);
761 	dma_map_sg(nfc->dma_chan->device->dev, &sg, 1, direction);
762 	tx = dmaengine_prep_slave_sg(nfc->dma_chan, &sg, 1,
763 				     direction == DMA_FROM_DEVICE ?
764 				     DMA_DEV_TO_MEM : DMA_MEM_TO_DEV,
765 				     DMA_PREP_INTERRUPT);
766 	if (!tx) {
767 		dev_err(nfc->dev, "Could not prepare DMA S/G list\n");
768 		return -ENXIO;
769 	}
770 
771 	/* Do the task and wait for it to finish */
772 	cookie = dmaengine_submit(tx);
773 	ret = dma_submit_error(cookie);
774 	if (ret)
775 		return -EIO;
776 
777 	dma_async_issue_pending(nfc->dma_chan);
778 	ret = marvell_nfc_wait_cmdd(nfc->selected_chip);
779 	dma_unmap_sg(nfc->dma_chan->device->dev, &sg, 1, direction);
780 	marvell_nfc_disable_dma(nfc);
781 	if (ret) {
782 		dev_err(nfc->dev, "Timeout waiting for DMA (status: %d)\n",
783 			dmaengine_tx_status(nfc->dma_chan, cookie, NULL));
784 		dmaengine_terminate_all(nfc->dma_chan);
785 		return -ETIMEDOUT;
786 	}
787 
788 	return 0;
789 }
790 
791 static int marvell_nfc_xfer_data_in_pio(struct marvell_nfc *nfc, u8 *in,
792 					unsigned int len)
793 {
794 	unsigned int last_len = len % FIFO_DEPTH;
795 	unsigned int last_full_offset = round_down(len, FIFO_DEPTH);
796 	int i;
797 
798 	for (i = 0; i < last_full_offset; i += FIFO_DEPTH)
799 		ioread32_rep(nfc->regs + NDDB, in + i, FIFO_REP(FIFO_DEPTH));
800 
801 	if (last_len) {
802 		u8 tmp_buf[FIFO_DEPTH];
803 
804 		ioread32_rep(nfc->regs + NDDB, tmp_buf, FIFO_REP(FIFO_DEPTH));
805 		memcpy(in + last_full_offset, tmp_buf, last_len);
806 	}
807 
808 	return 0;
809 }
810 
811 static int marvell_nfc_xfer_data_out_pio(struct marvell_nfc *nfc, const u8 *out,
812 					 unsigned int len)
813 {
814 	unsigned int last_len = len % FIFO_DEPTH;
815 	unsigned int last_full_offset = round_down(len, FIFO_DEPTH);
816 	int i;
817 
818 	for (i = 0; i < last_full_offset; i += FIFO_DEPTH)
819 		iowrite32_rep(nfc->regs + NDDB, out + i, FIFO_REP(FIFO_DEPTH));
820 
821 	if (last_len) {
822 		u8 tmp_buf[FIFO_DEPTH];
823 
824 		memcpy(tmp_buf, out + last_full_offset, last_len);
825 		iowrite32_rep(nfc->regs + NDDB, tmp_buf, FIFO_REP(FIFO_DEPTH));
826 	}
827 
828 	return 0;
829 }
830 
831 static void marvell_nfc_check_empty_chunk(struct nand_chip *chip,
832 					  u8 *data, int data_len,
833 					  u8 *spare, int spare_len,
834 					  u8 *ecc, int ecc_len,
835 					  unsigned int *max_bitflips)
836 {
837 	struct mtd_info *mtd = nand_to_mtd(chip);
838 	int bf;
839 
840 	/*
841 	 * Blank pages (all 0xFF) that have not been written may be recognized
842 	 * as bad if bitflips occur, so whenever an uncorrectable error occurs,
843 	 * check if the entire page (with ECC bytes) is actually blank or not.
844 	 */
845 	if (!data)
846 		data_len = 0;
847 	if (!spare)
848 		spare_len = 0;
849 	if (!ecc)
850 		ecc_len = 0;
851 
852 	bf = nand_check_erased_ecc_chunk(data, data_len, ecc, ecc_len,
853 					 spare, spare_len, chip->ecc.strength);
854 	if (bf < 0) {
855 		mtd->ecc_stats.failed++;
856 		return;
857 	}
858 
859 	/* Update the stats and max_bitflips */
860 	mtd->ecc_stats.corrected += bf;
861 	*max_bitflips = max_t(unsigned int, *max_bitflips, bf);
862 }
863 
864 /*
865  * Check a chunk is correct or not according to hardware ECC engine.
866  * mtd->ecc_stats.corrected is updated, as well as max_bitflips, however
867  * mtd->ecc_stats.failure is not, the function will instead return a non-zero
868  * value indicating that a check on the emptyness of the subpage must be
869  * performed before declaring the subpage corrupted.
870  */
871 static int marvell_nfc_hw_ecc_correct(struct nand_chip *chip,
872 				      unsigned int *max_bitflips)
873 {
874 	struct mtd_info *mtd = nand_to_mtd(chip);
875 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
876 	int bf = 0;
877 	u32 ndsr;
878 
879 	ndsr = readl_relaxed(nfc->regs + NDSR);
880 
881 	/* Check uncorrectable error flag */
882 	if (ndsr & NDSR_UNCERR) {
883 		writel_relaxed(ndsr, nfc->regs + NDSR);
884 
885 		/*
886 		 * Do not increment ->ecc_stats.failed now, instead, return a
887 		 * non-zero value to indicate that this chunk was apparently
888 		 * bad, and it should be check to see if it empty or not. If
889 		 * the chunk (with ECC bytes) is not declared empty, the calling
890 		 * function must increment the failure count.
891 		 */
892 		return -EBADMSG;
893 	}
894 
895 	/* Check correctable error flag */
896 	if (ndsr & NDSR_CORERR) {
897 		writel_relaxed(ndsr, nfc->regs + NDSR);
898 
899 		if (chip->ecc.algo == NAND_ECC_BCH)
900 			bf = NDSR_ERRCNT(ndsr);
901 		else
902 			bf = 1;
903 	}
904 
905 	/* Update the stats and max_bitflips */
906 	mtd->ecc_stats.corrected += bf;
907 	*max_bitflips = max_t(unsigned int, *max_bitflips, bf);
908 
909 	return 0;
910 }
911 
912 /* Hamming read helpers */
913 static int marvell_nfc_hw_ecc_hmg_do_read_page(struct nand_chip *chip,
914 					       u8 *data_buf, u8 *oob_buf,
915 					       bool raw, int page)
916 {
917 	struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
918 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
919 	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
920 	struct marvell_nfc_op nfc_op = {
921 		.ndcb[0] = NDCB0_CMD_TYPE(TYPE_READ) |
922 			   NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
923 			   NDCB0_DBC |
924 			   NDCB0_CMD1(NAND_CMD_READ0) |
925 			   NDCB0_CMD2(NAND_CMD_READSTART),
926 		.ndcb[1] = NDCB1_ADDRS_PAGE(page),
927 		.ndcb[2] = NDCB2_ADDR5_PAGE(page),
928 	};
929 	unsigned int oob_bytes = lt->spare_bytes + (raw ? lt->ecc_bytes : 0);
930 	int ret;
931 
932 	/* NFCv2 needs more information about the operation being executed */
933 	if (nfc->caps->is_nfcv2)
934 		nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
935 
936 	ret = marvell_nfc_prepare_cmd(chip);
937 	if (ret)
938 		return ret;
939 
940 	marvell_nfc_send_cmd(chip, &nfc_op);
941 	ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
942 				  "RDDREQ while draining FIFO (data/oob)");
943 	if (ret)
944 		return ret;
945 
946 	/*
947 	 * Read the page then the OOB area. Unlike what is shown in current
948 	 * documentation, spare bytes are protected by the ECC engine, and must
949 	 * be at the beginning of the OOB area or running this driver on legacy
950 	 * systems will prevent the discovery of the BBM/BBT.
951 	 */
952 	if (nfc->use_dma) {
953 		marvell_nfc_xfer_data_dma(nfc, DMA_FROM_DEVICE,
954 					  lt->data_bytes + oob_bytes);
955 		memcpy(data_buf, nfc->dma_buf, lt->data_bytes);
956 		memcpy(oob_buf, nfc->dma_buf + lt->data_bytes, oob_bytes);
957 	} else {
958 		marvell_nfc_xfer_data_in_pio(nfc, data_buf, lt->data_bytes);
959 		marvell_nfc_xfer_data_in_pio(nfc, oob_buf, oob_bytes);
960 	}
961 
962 	ret = marvell_nfc_wait_cmdd(chip);
963 
964 	return ret;
965 }
966 
967 static int marvell_nfc_hw_ecc_hmg_read_page_raw(struct mtd_info *mtd,
968 						struct nand_chip *chip, u8 *buf,
969 						int oob_required, int page)
970 {
971 	return marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi,
972 						   true, page);
973 }
974 
975 static int marvell_nfc_hw_ecc_hmg_read_page(struct mtd_info *mtd,
976 					    struct nand_chip *chip,
977 					    u8 *buf, int oob_required,
978 					    int page)
979 {
980 	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
981 	unsigned int full_sz = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
982 	int max_bitflips = 0, ret;
983 	u8 *raw_buf;
984 
985 	marvell_nfc_enable_hw_ecc(chip);
986 	marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi, false,
987 					    page);
988 	ret = marvell_nfc_hw_ecc_correct(chip, &max_bitflips);
989 	marvell_nfc_disable_hw_ecc(chip);
990 
991 	if (!ret)
992 		return max_bitflips;
993 
994 	/*
995 	 * When ECC failures are detected, check if the full page has been
996 	 * written or not. Ignore the failure if it is actually empty.
997 	 */
998 	raw_buf = kmalloc(full_sz, GFP_KERNEL);
999 	if (!raw_buf)
1000 		return -ENOMEM;
1001 
1002 	marvell_nfc_hw_ecc_hmg_do_read_page(chip, raw_buf, raw_buf +
1003 					    lt->data_bytes, true, page);
1004 	marvell_nfc_check_empty_chunk(chip, raw_buf, full_sz, NULL, 0, NULL, 0,
1005 				      &max_bitflips);
1006 	kfree(raw_buf);
1007 
1008 	return max_bitflips;
1009 }
1010 
1011 /*
1012  * Spare area in Hamming layouts is not protected by the ECC engine (even if
1013  * it appears before the ECC bytes when reading), the ->read_oob_raw() function
1014  * also stands for ->read_oob().
1015  */
1016 static int marvell_nfc_hw_ecc_hmg_read_oob_raw(struct mtd_info *mtd,
1017 					       struct nand_chip *chip, int page)
1018 {
1019 	/* Invalidate page cache */
1020 	chip->pagebuf = -1;
1021 
1022 	return marvell_nfc_hw_ecc_hmg_do_read_page(chip, chip->data_buf,
1023 						   chip->oob_poi, true, page);
1024 }
1025 
1026 /* Hamming write helpers */
1027 static int marvell_nfc_hw_ecc_hmg_do_write_page(struct nand_chip *chip,
1028 						const u8 *data_buf,
1029 						const u8 *oob_buf, bool raw,
1030 						int page)
1031 {
1032 	struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
1033 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1034 	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1035 	struct marvell_nfc_op nfc_op = {
1036 		.ndcb[0] = NDCB0_CMD_TYPE(TYPE_WRITE) |
1037 			   NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
1038 			   NDCB0_CMD1(NAND_CMD_SEQIN) |
1039 			   NDCB0_CMD2(NAND_CMD_PAGEPROG) |
1040 			   NDCB0_DBC,
1041 		.ndcb[1] = NDCB1_ADDRS_PAGE(page),
1042 		.ndcb[2] = NDCB2_ADDR5_PAGE(page),
1043 	};
1044 	unsigned int oob_bytes = lt->spare_bytes + (raw ? lt->ecc_bytes : 0);
1045 	int ret;
1046 
1047 	/* NFCv2 needs more information about the operation being executed */
1048 	if (nfc->caps->is_nfcv2)
1049 		nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
1050 
1051 	ret = marvell_nfc_prepare_cmd(chip);
1052 	if (ret)
1053 		return ret;
1054 
1055 	marvell_nfc_send_cmd(chip, &nfc_op);
1056 	ret = marvell_nfc_end_cmd(chip, NDSR_WRDREQ,
1057 				  "WRDREQ while loading FIFO (data)");
1058 	if (ret)
1059 		return ret;
1060 
1061 	/* Write the page then the OOB area */
1062 	if (nfc->use_dma) {
1063 		memcpy(nfc->dma_buf, data_buf, lt->data_bytes);
1064 		memcpy(nfc->dma_buf + lt->data_bytes, oob_buf, oob_bytes);
1065 		marvell_nfc_xfer_data_dma(nfc, DMA_TO_DEVICE, lt->data_bytes +
1066 					  lt->ecc_bytes + lt->spare_bytes);
1067 	} else {
1068 		marvell_nfc_xfer_data_out_pio(nfc, data_buf, lt->data_bytes);
1069 		marvell_nfc_xfer_data_out_pio(nfc, oob_buf, oob_bytes);
1070 	}
1071 
1072 	ret = marvell_nfc_wait_cmdd(chip);
1073 	if (ret)
1074 		return ret;
1075 
1076 	ret = marvell_nfc_wait_op(chip,
1077 				  chip->data_interface.timings.sdr.tPROG_max);
1078 	return ret;
1079 }
1080 
1081 static int marvell_nfc_hw_ecc_hmg_write_page_raw(struct mtd_info *mtd,
1082 						 struct nand_chip *chip,
1083 						 const u8 *buf,
1084 						 int oob_required, int page)
1085 {
1086 	return marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi,
1087 						    true, page);
1088 }
1089 
1090 static int marvell_nfc_hw_ecc_hmg_write_page(struct mtd_info *mtd,
1091 					     struct nand_chip *chip,
1092 					     const u8 *buf,
1093 					     int oob_required, int page)
1094 {
1095 	int ret;
1096 
1097 	marvell_nfc_enable_hw_ecc(chip);
1098 	ret = marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi,
1099 						   false, page);
1100 	marvell_nfc_disable_hw_ecc(chip);
1101 
1102 	return ret;
1103 }
1104 
1105 /*
1106  * Spare area in Hamming layouts is not protected by the ECC engine (even if
1107  * it appears before the ECC bytes when reading), the ->write_oob_raw() function
1108  * also stands for ->write_oob().
1109  */
1110 static int marvell_nfc_hw_ecc_hmg_write_oob_raw(struct mtd_info *mtd,
1111 						struct nand_chip *chip,
1112 						int page)
1113 {
1114 	/* Invalidate page cache */
1115 	chip->pagebuf = -1;
1116 
1117 	memset(chip->data_buf, 0xFF, mtd->writesize);
1118 
1119 	return marvell_nfc_hw_ecc_hmg_do_write_page(chip, chip->data_buf,
1120 						    chip->oob_poi, true, page);
1121 }
1122 
1123 /* BCH read helpers */
1124 static int marvell_nfc_hw_ecc_bch_read_page_raw(struct mtd_info *mtd,
1125 						struct nand_chip *chip, u8 *buf,
1126 						int oob_required, int page)
1127 {
1128 	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1129 	u8 *oob = chip->oob_poi;
1130 	int chunk_size = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
1131 	int ecc_offset = (lt->full_chunk_cnt * lt->spare_bytes) +
1132 		lt->last_spare_bytes;
1133 	int data_len = lt->data_bytes;
1134 	int spare_len = lt->spare_bytes;
1135 	int ecc_len = lt->ecc_bytes;
1136 	int chunk;
1137 
1138 	if (oob_required)
1139 		memset(chip->oob_poi, 0xFF, mtd->oobsize);
1140 
1141 	nand_read_page_op(chip, page, 0, NULL, 0);
1142 
1143 	for (chunk = 0; chunk < lt->nchunks; chunk++) {
1144 		/* Update last chunk length */
1145 		if (chunk >= lt->full_chunk_cnt) {
1146 			data_len = lt->last_data_bytes;
1147 			spare_len = lt->last_spare_bytes;
1148 			ecc_len = lt->last_ecc_bytes;
1149 		}
1150 
1151 		/* Read data bytes*/
1152 		nand_change_read_column_op(chip, chunk * chunk_size,
1153 					   buf + (lt->data_bytes * chunk),
1154 					   data_len, false);
1155 
1156 		/* Read spare bytes */
1157 		nand_read_data_op(chip, oob + (lt->spare_bytes * chunk),
1158 				  spare_len, false);
1159 
1160 		/* Read ECC bytes */
1161 		nand_read_data_op(chip, oob + ecc_offset +
1162 				  (ALIGN(lt->ecc_bytes, 32) * chunk),
1163 				  ecc_len, false);
1164 	}
1165 
1166 	return 0;
1167 }
1168 
1169 static void marvell_nfc_hw_ecc_bch_read_chunk(struct nand_chip *chip, int chunk,
1170 					      u8 *data, unsigned int data_len,
1171 					      u8 *spare, unsigned int spare_len,
1172 					      int page)
1173 {
1174 	struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
1175 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1176 	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1177 	int i, ret;
1178 	struct marvell_nfc_op nfc_op = {
1179 		.ndcb[0] = NDCB0_CMD_TYPE(TYPE_READ) |
1180 			   NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
1181 			   NDCB0_LEN_OVRD,
1182 		.ndcb[1] = NDCB1_ADDRS_PAGE(page),
1183 		.ndcb[2] = NDCB2_ADDR5_PAGE(page),
1184 		.ndcb[3] = data_len + spare_len,
1185 	};
1186 
1187 	ret = marvell_nfc_prepare_cmd(chip);
1188 	if (ret)
1189 		return;
1190 
1191 	if (chunk == 0)
1192 		nfc_op.ndcb[0] |= NDCB0_DBC |
1193 				  NDCB0_CMD1(NAND_CMD_READ0) |
1194 				  NDCB0_CMD2(NAND_CMD_READSTART);
1195 
1196 	/*
1197 	 * Trigger the naked read operation only on the last chunk.
1198 	 * Otherwise, use monolithic read.
1199 	 */
1200 	if (lt->nchunks == 1 || (chunk < lt->nchunks - 1))
1201 		nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
1202 	else
1203 		nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1204 
1205 	marvell_nfc_send_cmd(chip, &nfc_op);
1206 
1207 	/*
1208 	 * According to the datasheet, when reading from NDDB
1209 	 * with BCH enabled, after each 32 bytes reads, we
1210 	 * have to make sure that the NDSR.RDDREQ bit is set.
1211 	 *
1212 	 * Drain the FIFO, 8 32-bit reads at a time, and skip
1213 	 * the polling on the last read.
1214 	 *
1215 	 * Length is a multiple of 32 bytes, hence it is a multiple of 8 too.
1216 	 */
1217 	for (i = 0; i < data_len; i += FIFO_DEPTH * BCH_SEQ_READS) {
1218 		marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1219 				    "RDDREQ while draining FIFO (data)");
1220 		marvell_nfc_xfer_data_in_pio(nfc, data,
1221 					     FIFO_DEPTH * BCH_SEQ_READS);
1222 		data += FIFO_DEPTH * BCH_SEQ_READS;
1223 	}
1224 
1225 	for (i = 0; i < spare_len; i += FIFO_DEPTH * BCH_SEQ_READS) {
1226 		marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1227 				    "RDDREQ while draining FIFO (OOB)");
1228 		marvell_nfc_xfer_data_in_pio(nfc, spare,
1229 					     FIFO_DEPTH * BCH_SEQ_READS);
1230 		spare += FIFO_DEPTH * BCH_SEQ_READS;
1231 	}
1232 }
1233 
1234 static int marvell_nfc_hw_ecc_bch_read_page(struct mtd_info *mtd,
1235 					    struct nand_chip *chip,
1236 					    u8 *buf, int oob_required,
1237 					    int page)
1238 {
1239 	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1240 	int data_len = lt->data_bytes, spare_len = lt->spare_bytes, ecc_len;
1241 	u8 *data = buf, *spare = chip->oob_poi, *ecc;
1242 	int max_bitflips = 0;
1243 	u32 failure_mask = 0;
1244 	int chunk, ecc_offset_in_page, ret;
1245 
1246 	/*
1247 	 * With BCH, OOB is not fully used (and thus not read entirely), not
1248 	 * expected bytes could show up at the end of the OOB buffer if not
1249 	 * explicitly erased.
1250 	 */
1251 	if (oob_required)
1252 		memset(chip->oob_poi, 0xFF, mtd->oobsize);
1253 
1254 	marvell_nfc_enable_hw_ecc(chip);
1255 
1256 	for (chunk = 0; chunk < lt->nchunks; chunk++) {
1257 		/* Update length for the last chunk */
1258 		if (chunk >= lt->full_chunk_cnt) {
1259 			data_len = lt->last_data_bytes;
1260 			spare_len = lt->last_spare_bytes;
1261 		}
1262 
1263 		/* Read the chunk and detect number of bitflips */
1264 		marvell_nfc_hw_ecc_bch_read_chunk(chip, chunk, data, data_len,
1265 						  spare, spare_len, page);
1266 		ret = marvell_nfc_hw_ecc_correct(chip, &max_bitflips);
1267 		if (ret)
1268 			failure_mask |= BIT(chunk);
1269 
1270 		data += data_len;
1271 		spare += spare_len;
1272 	}
1273 
1274 	marvell_nfc_disable_hw_ecc(chip);
1275 
1276 	if (!failure_mask)
1277 		return max_bitflips;
1278 
1279 	/*
1280 	 * Please note that dumping the ECC bytes during a normal read with OOB
1281 	 * area would add a significant overhead as ECC bytes are "consumed" by
1282 	 * the controller in normal mode and must be re-read in raw mode. To
1283 	 * avoid dropping the performances, we prefer not to include them. The
1284 	 * user should re-read the page in raw mode if ECC bytes are required.
1285 	 *
1286 	 * However, for any subpage read error reported by ->correct(), the ECC
1287 	 * bytes must be read in raw mode and the full subpage must be checked
1288 	 * to see if it is entirely empty of if there was an actual error.
1289 	 */
1290 	for (chunk = 0; chunk < lt->nchunks; chunk++) {
1291 		/* No failure reported for this chunk, move to the next one */
1292 		if (!(failure_mask & BIT(chunk)))
1293 			continue;
1294 
1295 		/* Derive ECC bytes positions (in page/buffer) and length */
1296 		ecc = chip->oob_poi +
1297 			(lt->full_chunk_cnt * lt->spare_bytes) +
1298 			lt->last_spare_bytes +
1299 			(chunk * ALIGN(lt->ecc_bytes, 32));
1300 		ecc_offset_in_page =
1301 			(chunk * (lt->data_bytes + lt->spare_bytes +
1302 				  lt->ecc_bytes)) +
1303 			(chunk < lt->full_chunk_cnt ?
1304 			 lt->data_bytes + lt->spare_bytes :
1305 			 lt->last_data_bytes + lt->last_spare_bytes);
1306 		ecc_len = chunk < lt->full_chunk_cnt ?
1307 			lt->ecc_bytes : lt->last_ecc_bytes;
1308 
1309 		/* Do the actual raw read of the ECC bytes */
1310 		nand_change_read_column_op(chip, ecc_offset_in_page,
1311 					   ecc, ecc_len, false);
1312 
1313 		/* Derive data/spare bytes positions (in buffer) and length */
1314 		data = buf + (chunk * lt->data_bytes);
1315 		data_len = chunk < lt->full_chunk_cnt ?
1316 			lt->data_bytes : lt->last_data_bytes;
1317 		spare = chip->oob_poi + (chunk * (lt->spare_bytes +
1318 						  lt->ecc_bytes));
1319 		spare_len = chunk < lt->full_chunk_cnt ?
1320 			lt->spare_bytes : lt->last_spare_bytes;
1321 
1322 		/* Check the entire chunk (data + spare + ecc) for emptyness */
1323 		marvell_nfc_check_empty_chunk(chip, data, data_len, spare,
1324 					      spare_len, ecc, ecc_len,
1325 					      &max_bitflips);
1326 	}
1327 
1328 	return max_bitflips;
1329 }
1330 
1331 static int marvell_nfc_hw_ecc_bch_read_oob_raw(struct mtd_info *mtd,
1332 					       struct nand_chip *chip, int page)
1333 {
1334 	/* Invalidate page cache */
1335 	chip->pagebuf = -1;
1336 
1337 	return chip->ecc.read_page_raw(mtd, chip, chip->data_buf, true, page);
1338 }
1339 
1340 static int marvell_nfc_hw_ecc_bch_read_oob(struct mtd_info *mtd,
1341 					   struct nand_chip *chip, int page)
1342 {
1343 	/* Invalidate page cache */
1344 	chip->pagebuf = -1;
1345 
1346 	return chip->ecc.read_page(mtd, chip, chip->data_buf, true, page);
1347 }
1348 
1349 /* BCH write helpers */
1350 static int marvell_nfc_hw_ecc_bch_write_page_raw(struct mtd_info *mtd,
1351 						 struct nand_chip *chip,
1352 						 const u8 *buf,
1353 						 int oob_required, int page)
1354 {
1355 	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1356 	int full_chunk_size = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
1357 	int data_len = lt->data_bytes;
1358 	int spare_len = lt->spare_bytes;
1359 	int ecc_len = lt->ecc_bytes;
1360 	int spare_offset = 0;
1361 	int ecc_offset = (lt->full_chunk_cnt * lt->spare_bytes) +
1362 		lt->last_spare_bytes;
1363 	int chunk;
1364 
1365 	nand_prog_page_begin_op(chip, page, 0, NULL, 0);
1366 
1367 	for (chunk = 0; chunk < lt->nchunks; chunk++) {
1368 		if (chunk >= lt->full_chunk_cnt) {
1369 			data_len = lt->last_data_bytes;
1370 			spare_len = lt->last_spare_bytes;
1371 			ecc_len = lt->last_ecc_bytes;
1372 		}
1373 
1374 		/* Point to the column of the next chunk */
1375 		nand_change_write_column_op(chip, chunk * full_chunk_size,
1376 					    NULL, 0, false);
1377 
1378 		/* Write the data */
1379 		nand_write_data_op(chip, buf + (chunk * lt->data_bytes),
1380 				   data_len, false);
1381 
1382 		if (!oob_required)
1383 			continue;
1384 
1385 		/* Write the spare bytes */
1386 		if (spare_len)
1387 			nand_write_data_op(chip, chip->oob_poi + spare_offset,
1388 					   spare_len, false);
1389 
1390 		/* Write the ECC bytes */
1391 		if (ecc_len)
1392 			nand_write_data_op(chip, chip->oob_poi + ecc_offset,
1393 					   ecc_len, false);
1394 
1395 		spare_offset += spare_len;
1396 		ecc_offset += ALIGN(ecc_len, 32);
1397 	}
1398 
1399 	return nand_prog_page_end_op(chip);
1400 }
1401 
1402 static int
1403 marvell_nfc_hw_ecc_bch_write_chunk(struct nand_chip *chip, int chunk,
1404 				   const u8 *data, unsigned int data_len,
1405 				   const u8 *spare, unsigned int spare_len,
1406 				   int page)
1407 {
1408 	struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
1409 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1410 	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1411 	int ret;
1412 	struct marvell_nfc_op nfc_op = {
1413 		.ndcb[0] = NDCB0_CMD_TYPE(TYPE_WRITE) | NDCB0_LEN_OVRD,
1414 		.ndcb[3] = data_len + spare_len,
1415 	};
1416 
1417 	/*
1418 	 * First operation dispatches the CMD_SEQIN command, issue the address
1419 	 * cycles and asks for the first chunk of data.
1420 	 * All operations in the middle (if any) will issue a naked write and
1421 	 * also ask for data.
1422 	 * Last operation (if any) asks for the last chunk of data through a
1423 	 * last naked write.
1424 	 */
1425 	if (chunk == 0) {
1426 		nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_WRITE_DISPATCH) |
1427 				  NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
1428 				  NDCB0_CMD1(NAND_CMD_SEQIN);
1429 		nfc_op.ndcb[1] |= NDCB1_ADDRS_PAGE(page);
1430 		nfc_op.ndcb[2] |= NDCB2_ADDR5_PAGE(page);
1431 	} else if (chunk < lt->nchunks - 1) {
1432 		nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_NAKED_RW);
1433 	} else {
1434 		nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1435 	}
1436 
1437 	/* Always dispatch the PAGEPROG command on the last chunk */
1438 	if (chunk == lt->nchunks - 1)
1439 		nfc_op.ndcb[0] |= NDCB0_CMD2(NAND_CMD_PAGEPROG) | NDCB0_DBC;
1440 
1441 	ret = marvell_nfc_prepare_cmd(chip);
1442 	if (ret)
1443 		return ret;
1444 
1445 	marvell_nfc_send_cmd(chip, &nfc_op);
1446 	ret = marvell_nfc_end_cmd(chip, NDSR_WRDREQ,
1447 				  "WRDREQ while loading FIFO (data)");
1448 	if (ret)
1449 		return ret;
1450 
1451 	/* Transfer the contents */
1452 	iowrite32_rep(nfc->regs + NDDB, data, FIFO_REP(data_len));
1453 	iowrite32_rep(nfc->regs + NDDB, spare, FIFO_REP(spare_len));
1454 
1455 	return 0;
1456 }
1457 
1458 static int marvell_nfc_hw_ecc_bch_write_page(struct mtd_info *mtd,
1459 					     struct nand_chip *chip,
1460 					     const u8 *buf,
1461 					     int oob_required, int page)
1462 {
1463 	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1464 	const u8 *data = buf;
1465 	const u8 *spare = chip->oob_poi;
1466 	int data_len = lt->data_bytes;
1467 	int spare_len = lt->spare_bytes;
1468 	int chunk, ret;
1469 
1470 	/* Spare data will be written anyway, so clear it to avoid garbage */
1471 	if (!oob_required)
1472 		memset(chip->oob_poi, 0xFF, mtd->oobsize);
1473 
1474 	marvell_nfc_enable_hw_ecc(chip);
1475 
1476 	for (chunk = 0; chunk < lt->nchunks; chunk++) {
1477 		if (chunk >= lt->full_chunk_cnt) {
1478 			data_len = lt->last_data_bytes;
1479 			spare_len = lt->last_spare_bytes;
1480 		}
1481 
1482 		marvell_nfc_hw_ecc_bch_write_chunk(chip, chunk, data, data_len,
1483 						   spare, spare_len, page);
1484 		data += data_len;
1485 		spare += spare_len;
1486 
1487 		/*
1488 		 * Waiting only for CMDD or PAGED is not enough, ECC are
1489 		 * partially written. No flag is set once the operation is
1490 		 * really finished but the ND_RUN bit is cleared, so wait for it
1491 		 * before stepping into the next command.
1492 		 */
1493 		marvell_nfc_wait_ndrun(chip);
1494 	}
1495 
1496 	ret = marvell_nfc_wait_op(chip,
1497 				  chip->data_interface.timings.sdr.tPROG_max);
1498 
1499 	marvell_nfc_disable_hw_ecc(chip);
1500 
1501 	if (ret)
1502 		return ret;
1503 
1504 	return 0;
1505 }
1506 
1507 static int marvell_nfc_hw_ecc_bch_write_oob_raw(struct mtd_info *mtd,
1508 						struct nand_chip *chip,
1509 						int page)
1510 {
1511 	/* Invalidate page cache */
1512 	chip->pagebuf = -1;
1513 
1514 	memset(chip->data_buf, 0xFF, mtd->writesize);
1515 
1516 	return chip->ecc.write_page_raw(mtd, chip, chip->data_buf, true, page);
1517 }
1518 
1519 static int marvell_nfc_hw_ecc_bch_write_oob(struct mtd_info *mtd,
1520 					    struct nand_chip *chip, int page)
1521 {
1522 	/* Invalidate page cache */
1523 	chip->pagebuf = -1;
1524 
1525 	memset(chip->data_buf, 0xFF, mtd->writesize);
1526 
1527 	return chip->ecc.write_page(mtd, chip, chip->data_buf, true, page);
1528 }
1529 
1530 /* NAND framework ->exec_op() hooks and related helpers */
1531 static void marvell_nfc_parse_instructions(struct nand_chip *chip,
1532 					   const struct nand_subop *subop,
1533 					   struct marvell_nfc_op *nfc_op)
1534 {
1535 	const struct nand_op_instr *instr = NULL;
1536 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1537 	bool first_cmd = true;
1538 	unsigned int op_id;
1539 	int i;
1540 
1541 	/* Reset the input structure as most of its fields will be OR'ed */
1542 	memset(nfc_op, 0, sizeof(struct marvell_nfc_op));
1543 
1544 	for (op_id = 0; op_id < subop->ninstrs; op_id++) {
1545 		unsigned int offset, naddrs;
1546 		const u8 *addrs;
1547 		int len = nand_subop_get_data_len(subop, op_id);
1548 
1549 		instr = &subop->instrs[op_id];
1550 
1551 		switch (instr->type) {
1552 		case NAND_OP_CMD_INSTR:
1553 			if (first_cmd)
1554 				nfc_op->ndcb[0] |=
1555 					NDCB0_CMD1(instr->ctx.cmd.opcode);
1556 			else
1557 				nfc_op->ndcb[0] |=
1558 					NDCB0_CMD2(instr->ctx.cmd.opcode) |
1559 					NDCB0_DBC;
1560 
1561 			nfc_op->cle_ale_delay_ns = instr->delay_ns;
1562 			first_cmd = false;
1563 			break;
1564 
1565 		case NAND_OP_ADDR_INSTR:
1566 			offset = nand_subop_get_addr_start_off(subop, op_id);
1567 			naddrs = nand_subop_get_num_addr_cyc(subop, op_id);
1568 			addrs = &instr->ctx.addr.addrs[offset];
1569 
1570 			nfc_op->ndcb[0] |= NDCB0_ADDR_CYC(naddrs);
1571 
1572 			for (i = 0; i < min_t(unsigned int, 4, naddrs); i++)
1573 				nfc_op->ndcb[1] |= addrs[i] << (8 * i);
1574 
1575 			if (naddrs >= 5)
1576 				nfc_op->ndcb[2] |= NDCB2_ADDR5_CYC(addrs[4]);
1577 			if (naddrs >= 6)
1578 				nfc_op->ndcb[3] |= NDCB3_ADDR6_CYC(addrs[5]);
1579 			if (naddrs == 7)
1580 				nfc_op->ndcb[3] |= NDCB3_ADDR7_CYC(addrs[6]);
1581 
1582 			nfc_op->cle_ale_delay_ns = instr->delay_ns;
1583 			break;
1584 
1585 		case NAND_OP_DATA_IN_INSTR:
1586 			nfc_op->data_instr = instr;
1587 			nfc_op->data_instr_idx = op_id;
1588 			nfc_op->ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ);
1589 			if (nfc->caps->is_nfcv2) {
1590 				nfc_op->ndcb[0] |=
1591 					NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW) |
1592 					NDCB0_LEN_OVRD;
1593 				nfc_op->ndcb[3] |= round_up(len, FIFO_DEPTH);
1594 			}
1595 			nfc_op->data_delay_ns = instr->delay_ns;
1596 			break;
1597 
1598 		case NAND_OP_DATA_OUT_INSTR:
1599 			nfc_op->data_instr = instr;
1600 			nfc_op->data_instr_idx = op_id;
1601 			nfc_op->ndcb[0] |= NDCB0_CMD_TYPE(TYPE_WRITE);
1602 			if (nfc->caps->is_nfcv2) {
1603 				nfc_op->ndcb[0] |=
1604 					NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW) |
1605 					NDCB0_LEN_OVRD;
1606 				nfc_op->ndcb[3] |= round_up(len, FIFO_DEPTH);
1607 			}
1608 			nfc_op->data_delay_ns = instr->delay_ns;
1609 			break;
1610 
1611 		case NAND_OP_WAITRDY_INSTR:
1612 			nfc_op->rdy_timeout_ms = instr->ctx.waitrdy.timeout_ms;
1613 			nfc_op->rdy_delay_ns = instr->delay_ns;
1614 			break;
1615 		}
1616 	}
1617 }
1618 
1619 static int marvell_nfc_xfer_data_pio(struct nand_chip *chip,
1620 				     const struct nand_subop *subop,
1621 				     struct marvell_nfc_op *nfc_op)
1622 {
1623 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1624 	const struct nand_op_instr *instr = nfc_op->data_instr;
1625 	unsigned int op_id = nfc_op->data_instr_idx;
1626 	unsigned int len = nand_subop_get_data_len(subop, op_id);
1627 	unsigned int offset = nand_subop_get_data_start_off(subop, op_id);
1628 	bool reading = (instr->type == NAND_OP_DATA_IN_INSTR);
1629 	int ret;
1630 
1631 	if (instr->ctx.data.force_8bit)
1632 		marvell_nfc_force_byte_access(chip, true);
1633 
1634 	if (reading) {
1635 		u8 *in = instr->ctx.data.buf.in + offset;
1636 
1637 		ret = marvell_nfc_xfer_data_in_pio(nfc, in, len);
1638 	} else {
1639 		const u8 *out = instr->ctx.data.buf.out + offset;
1640 
1641 		ret = marvell_nfc_xfer_data_out_pio(nfc, out, len);
1642 	}
1643 
1644 	if (instr->ctx.data.force_8bit)
1645 		marvell_nfc_force_byte_access(chip, false);
1646 
1647 	return ret;
1648 }
1649 
1650 static int marvell_nfc_monolithic_access_exec(struct nand_chip *chip,
1651 					      const struct nand_subop *subop)
1652 {
1653 	struct marvell_nfc_op nfc_op;
1654 	bool reading;
1655 	int ret;
1656 
1657 	marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1658 	reading = (nfc_op.data_instr->type == NAND_OP_DATA_IN_INSTR);
1659 
1660 	ret = marvell_nfc_prepare_cmd(chip);
1661 	if (ret)
1662 		return ret;
1663 
1664 	marvell_nfc_send_cmd(chip, &nfc_op);
1665 	ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ | NDSR_WRDREQ,
1666 				  "RDDREQ/WRDREQ while draining raw data");
1667 	if (ret)
1668 		return ret;
1669 
1670 	cond_delay(nfc_op.cle_ale_delay_ns);
1671 
1672 	if (reading) {
1673 		if (nfc_op.rdy_timeout_ms) {
1674 			ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1675 			if (ret)
1676 				return ret;
1677 		}
1678 
1679 		cond_delay(nfc_op.rdy_delay_ns);
1680 	}
1681 
1682 	marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1683 	ret = marvell_nfc_wait_cmdd(chip);
1684 	if (ret)
1685 		return ret;
1686 
1687 	cond_delay(nfc_op.data_delay_ns);
1688 
1689 	if (!reading) {
1690 		if (nfc_op.rdy_timeout_ms) {
1691 			ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1692 			if (ret)
1693 				return ret;
1694 		}
1695 
1696 		cond_delay(nfc_op.rdy_delay_ns);
1697 	}
1698 
1699 	/*
1700 	 * NDCR ND_RUN bit should be cleared automatically at the end of each
1701 	 * operation but experience shows that the behavior is buggy when it
1702 	 * comes to writes (with LEN_OVRD). Clear it by hand in this case.
1703 	 */
1704 	if (!reading) {
1705 		struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1706 
1707 		writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
1708 			       nfc->regs + NDCR);
1709 	}
1710 
1711 	return 0;
1712 }
1713 
1714 static int marvell_nfc_naked_access_exec(struct nand_chip *chip,
1715 					 const struct nand_subop *subop)
1716 {
1717 	struct marvell_nfc_op nfc_op;
1718 	int ret;
1719 
1720 	marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1721 
1722 	/*
1723 	 * Naked access are different in that they need to be flagged as naked
1724 	 * by the controller. Reset the controller registers fields that inform
1725 	 * on the type and refill them according to the ongoing operation.
1726 	 */
1727 	nfc_op.ndcb[0] &= ~(NDCB0_CMD_TYPE(TYPE_MASK) |
1728 			    NDCB0_CMD_XTYPE(XTYPE_MASK));
1729 	switch (subop->instrs[0].type) {
1730 	case NAND_OP_CMD_INSTR:
1731 		nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_CMD);
1732 		break;
1733 	case NAND_OP_ADDR_INSTR:
1734 		nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_ADDR);
1735 		break;
1736 	case NAND_OP_DATA_IN_INSTR:
1737 		nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ) |
1738 				  NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1739 		break;
1740 	case NAND_OP_DATA_OUT_INSTR:
1741 		nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_WRITE) |
1742 				  NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1743 		break;
1744 	default:
1745 		/* This should never happen */
1746 		break;
1747 	}
1748 
1749 	ret = marvell_nfc_prepare_cmd(chip);
1750 	if (ret)
1751 		return ret;
1752 
1753 	marvell_nfc_send_cmd(chip, &nfc_op);
1754 
1755 	if (!nfc_op.data_instr) {
1756 		ret = marvell_nfc_wait_cmdd(chip);
1757 		cond_delay(nfc_op.cle_ale_delay_ns);
1758 		return ret;
1759 	}
1760 
1761 	ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ | NDSR_WRDREQ,
1762 				  "RDDREQ/WRDREQ while draining raw data");
1763 	if (ret)
1764 		return ret;
1765 
1766 	marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1767 	ret = marvell_nfc_wait_cmdd(chip);
1768 	if (ret)
1769 		return ret;
1770 
1771 	/*
1772 	 * NDCR ND_RUN bit should be cleared automatically at the end of each
1773 	 * operation but experience shows that the behavior is buggy when it
1774 	 * comes to writes (with LEN_OVRD). Clear it by hand in this case.
1775 	 */
1776 	if (subop->instrs[0].type == NAND_OP_DATA_OUT_INSTR) {
1777 		struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1778 
1779 		writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
1780 			       nfc->regs + NDCR);
1781 	}
1782 
1783 	return 0;
1784 }
1785 
1786 static int marvell_nfc_naked_waitrdy_exec(struct nand_chip *chip,
1787 					  const struct nand_subop *subop)
1788 {
1789 	struct marvell_nfc_op nfc_op;
1790 	int ret;
1791 
1792 	marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1793 
1794 	ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1795 	cond_delay(nfc_op.rdy_delay_ns);
1796 
1797 	return ret;
1798 }
1799 
1800 static int marvell_nfc_read_id_type_exec(struct nand_chip *chip,
1801 					 const struct nand_subop *subop)
1802 {
1803 	struct marvell_nfc_op nfc_op;
1804 	int ret;
1805 
1806 	marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1807 	nfc_op.ndcb[0] &= ~NDCB0_CMD_TYPE(TYPE_READ);
1808 	nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ_ID);
1809 
1810 	ret = marvell_nfc_prepare_cmd(chip);
1811 	if (ret)
1812 		return ret;
1813 
1814 	marvell_nfc_send_cmd(chip, &nfc_op);
1815 	ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1816 				  "RDDREQ while reading ID");
1817 	if (ret)
1818 		return ret;
1819 
1820 	cond_delay(nfc_op.cle_ale_delay_ns);
1821 
1822 	if (nfc_op.rdy_timeout_ms) {
1823 		ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1824 		if (ret)
1825 			return ret;
1826 	}
1827 
1828 	cond_delay(nfc_op.rdy_delay_ns);
1829 
1830 	marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1831 	ret = marvell_nfc_wait_cmdd(chip);
1832 	if (ret)
1833 		return ret;
1834 
1835 	cond_delay(nfc_op.data_delay_ns);
1836 
1837 	return 0;
1838 }
1839 
1840 static int marvell_nfc_read_status_exec(struct nand_chip *chip,
1841 					const struct nand_subop *subop)
1842 {
1843 	struct marvell_nfc_op nfc_op;
1844 	int ret;
1845 
1846 	marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1847 	nfc_op.ndcb[0] &= ~NDCB0_CMD_TYPE(TYPE_READ);
1848 	nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_STATUS);
1849 
1850 	ret = marvell_nfc_prepare_cmd(chip);
1851 	if (ret)
1852 		return ret;
1853 
1854 	marvell_nfc_send_cmd(chip, &nfc_op);
1855 	ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1856 				  "RDDREQ while reading status");
1857 	if (ret)
1858 		return ret;
1859 
1860 	cond_delay(nfc_op.cle_ale_delay_ns);
1861 
1862 	if (nfc_op.rdy_timeout_ms) {
1863 		ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1864 		if (ret)
1865 			return ret;
1866 	}
1867 
1868 	cond_delay(nfc_op.rdy_delay_ns);
1869 
1870 	marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1871 	ret = marvell_nfc_wait_cmdd(chip);
1872 	if (ret)
1873 		return ret;
1874 
1875 	cond_delay(nfc_op.data_delay_ns);
1876 
1877 	return 0;
1878 }
1879 
1880 static int marvell_nfc_reset_cmd_type_exec(struct nand_chip *chip,
1881 					   const struct nand_subop *subop)
1882 {
1883 	struct marvell_nfc_op nfc_op;
1884 	int ret;
1885 
1886 	marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1887 	nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_RESET);
1888 
1889 	ret = marvell_nfc_prepare_cmd(chip);
1890 	if (ret)
1891 		return ret;
1892 
1893 	marvell_nfc_send_cmd(chip, &nfc_op);
1894 	ret = marvell_nfc_wait_cmdd(chip);
1895 	if (ret)
1896 		return ret;
1897 
1898 	cond_delay(nfc_op.cle_ale_delay_ns);
1899 
1900 	ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1901 	if (ret)
1902 		return ret;
1903 
1904 	cond_delay(nfc_op.rdy_delay_ns);
1905 
1906 	return 0;
1907 }
1908 
1909 static int marvell_nfc_erase_cmd_type_exec(struct nand_chip *chip,
1910 					   const struct nand_subop *subop)
1911 {
1912 	struct marvell_nfc_op nfc_op;
1913 	int ret;
1914 
1915 	marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1916 	nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_ERASE);
1917 
1918 	ret = marvell_nfc_prepare_cmd(chip);
1919 	if (ret)
1920 		return ret;
1921 
1922 	marvell_nfc_send_cmd(chip, &nfc_op);
1923 	ret = marvell_nfc_wait_cmdd(chip);
1924 	if (ret)
1925 		return ret;
1926 
1927 	cond_delay(nfc_op.cle_ale_delay_ns);
1928 
1929 	ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1930 	if (ret)
1931 		return ret;
1932 
1933 	cond_delay(nfc_op.rdy_delay_ns);
1934 
1935 	return 0;
1936 }
1937 
1938 static const struct nand_op_parser marvell_nfcv2_op_parser = NAND_OP_PARSER(
1939 	/* Monolithic reads/writes */
1940 	NAND_OP_PARSER_PATTERN(
1941 		marvell_nfc_monolithic_access_exec,
1942 		NAND_OP_PARSER_PAT_CMD_ELEM(false),
1943 		NAND_OP_PARSER_PAT_ADDR_ELEM(true, MAX_ADDRESS_CYC_NFCV2),
1944 		NAND_OP_PARSER_PAT_CMD_ELEM(true),
1945 		NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
1946 		NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE)),
1947 	NAND_OP_PARSER_PATTERN(
1948 		marvell_nfc_monolithic_access_exec,
1949 		NAND_OP_PARSER_PAT_CMD_ELEM(false),
1950 		NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2),
1951 		NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE),
1952 		NAND_OP_PARSER_PAT_CMD_ELEM(true),
1953 		NAND_OP_PARSER_PAT_WAITRDY_ELEM(true)),
1954 	/* Naked commands */
1955 	NAND_OP_PARSER_PATTERN(
1956 		marvell_nfc_naked_access_exec,
1957 		NAND_OP_PARSER_PAT_CMD_ELEM(false)),
1958 	NAND_OP_PARSER_PATTERN(
1959 		marvell_nfc_naked_access_exec,
1960 		NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2)),
1961 	NAND_OP_PARSER_PATTERN(
1962 		marvell_nfc_naked_access_exec,
1963 		NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE)),
1964 	NAND_OP_PARSER_PATTERN(
1965 		marvell_nfc_naked_access_exec,
1966 		NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE)),
1967 	NAND_OP_PARSER_PATTERN(
1968 		marvell_nfc_naked_waitrdy_exec,
1969 		NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
1970 	);
1971 
1972 static const struct nand_op_parser marvell_nfcv1_op_parser = NAND_OP_PARSER(
1973 	/* Naked commands not supported, use a function for each pattern */
1974 	NAND_OP_PARSER_PATTERN(
1975 		marvell_nfc_read_id_type_exec,
1976 		NAND_OP_PARSER_PAT_CMD_ELEM(false),
1977 		NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV1),
1978 		NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 8)),
1979 	NAND_OP_PARSER_PATTERN(
1980 		marvell_nfc_erase_cmd_type_exec,
1981 		NAND_OP_PARSER_PAT_CMD_ELEM(false),
1982 		NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV1),
1983 		NAND_OP_PARSER_PAT_CMD_ELEM(false),
1984 		NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
1985 	NAND_OP_PARSER_PATTERN(
1986 		marvell_nfc_read_status_exec,
1987 		NAND_OP_PARSER_PAT_CMD_ELEM(false),
1988 		NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 1)),
1989 	NAND_OP_PARSER_PATTERN(
1990 		marvell_nfc_reset_cmd_type_exec,
1991 		NAND_OP_PARSER_PAT_CMD_ELEM(false),
1992 		NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
1993 	NAND_OP_PARSER_PATTERN(
1994 		marvell_nfc_naked_waitrdy_exec,
1995 		NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
1996 	);
1997 
1998 static int marvell_nfc_exec_op(struct nand_chip *chip,
1999 			       const struct nand_operation *op,
2000 			       bool check_only)
2001 {
2002 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2003 
2004 	if (nfc->caps->is_nfcv2)
2005 		return nand_op_parser_exec_op(chip, &marvell_nfcv2_op_parser,
2006 					      op, check_only);
2007 	else
2008 		return nand_op_parser_exec_op(chip, &marvell_nfcv1_op_parser,
2009 					      op, check_only);
2010 }
2011 
2012 /*
2013  * Layouts were broken in old pxa3xx_nand driver, these are supposed to be
2014  * usable.
2015  */
2016 static int marvell_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
2017 				      struct mtd_oob_region *oobregion)
2018 {
2019 	struct nand_chip *chip = mtd_to_nand(mtd);
2020 	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
2021 
2022 	if (section)
2023 		return -ERANGE;
2024 
2025 	oobregion->length = (lt->full_chunk_cnt * lt->ecc_bytes) +
2026 			    lt->last_ecc_bytes;
2027 	oobregion->offset = mtd->oobsize - oobregion->length;
2028 
2029 	return 0;
2030 }
2031 
2032 static int marvell_nand_ooblayout_free(struct mtd_info *mtd, int section,
2033 				       struct mtd_oob_region *oobregion)
2034 {
2035 	struct nand_chip *chip = mtd_to_nand(mtd);
2036 	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
2037 
2038 	if (section)
2039 		return -ERANGE;
2040 
2041 	/*
2042 	 * Bootrom looks in bytes 0 & 5 for bad blocks for the
2043 	 * 4KB page / 4bit BCH combination.
2044 	 */
2045 	if (mtd->writesize == SZ_4K && lt->data_bytes == SZ_2K)
2046 		oobregion->offset = 6;
2047 	else
2048 		oobregion->offset = 2;
2049 
2050 	oobregion->length = (lt->full_chunk_cnt * lt->spare_bytes) +
2051 			    lt->last_spare_bytes - oobregion->offset;
2052 
2053 	return 0;
2054 }
2055 
2056 static const struct mtd_ooblayout_ops marvell_nand_ooblayout_ops = {
2057 	.ecc = marvell_nand_ooblayout_ecc,
2058 	.free = marvell_nand_ooblayout_free,
2059 };
2060 
2061 static int marvell_nand_hw_ecc_ctrl_init(struct mtd_info *mtd,
2062 					 struct nand_ecc_ctrl *ecc)
2063 {
2064 	struct nand_chip *chip = mtd_to_nand(mtd);
2065 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2066 	const struct marvell_hw_ecc_layout *l;
2067 	int i;
2068 
2069 	if (!nfc->caps->is_nfcv2 &&
2070 	    (mtd->writesize + mtd->oobsize > MAX_CHUNK_SIZE)) {
2071 		dev_err(nfc->dev,
2072 			"NFCv1: writesize (%d) cannot be bigger than a chunk (%d)\n",
2073 			mtd->writesize, MAX_CHUNK_SIZE - mtd->oobsize);
2074 		return -ENOTSUPP;
2075 	}
2076 
2077 	to_marvell_nand(chip)->layout = NULL;
2078 	for (i = 0; i < ARRAY_SIZE(marvell_nfc_layouts); i++) {
2079 		l = &marvell_nfc_layouts[i];
2080 		if (mtd->writesize == l->writesize &&
2081 		    ecc->size == l->chunk && ecc->strength == l->strength) {
2082 			to_marvell_nand(chip)->layout = l;
2083 			break;
2084 		}
2085 	}
2086 
2087 	if (!to_marvell_nand(chip)->layout ||
2088 	    (!nfc->caps->is_nfcv2 && ecc->strength > 1)) {
2089 		dev_err(nfc->dev,
2090 			"ECC strength %d at page size %d is not supported\n",
2091 			ecc->strength, mtd->writesize);
2092 		return -ENOTSUPP;
2093 	}
2094 
2095 	mtd_set_ooblayout(mtd, &marvell_nand_ooblayout_ops);
2096 	ecc->steps = l->nchunks;
2097 	ecc->size = l->data_bytes;
2098 
2099 	if (ecc->strength == 1) {
2100 		chip->ecc.algo = NAND_ECC_HAMMING;
2101 		ecc->read_page_raw = marvell_nfc_hw_ecc_hmg_read_page_raw;
2102 		ecc->read_page = marvell_nfc_hw_ecc_hmg_read_page;
2103 		ecc->read_oob_raw = marvell_nfc_hw_ecc_hmg_read_oob_raw;
2104 		ecc->read_oob = ecc->read_oob_raw;
2105 		ecc->write_page_raw = marvell_nfc_hw_ecc_hmg_write_page_raw;
2106 		ecc->write_page = marvell_nfc_hw_ecc_hmg_write_page;
2107 		ecc->write_oob_raw = marvell_nfc_hw_ecc_hmg_write_oob_raw;
2108 		ecc->write_oob = ecc->write_oob_raw;
2109 	} else {
2110 		chip->ecc.algo = NAND_ECC_BCH;
2111 		ecc->strength = 16;
2112 		ecc->read_page_raw = marvell_nfc_hw_ecc_bch_read_page_raw;
2113 		ecc->read_page = marvell_nfc_hw_ecc_bch_read_page;
2114 		ecc->read_oob_raw = marvell_nfc_hw_ecc_bch_read_oob_raw;
2115 		ecc->read_oob = marvell_nfc_hw_ecc_bch_read_oob;
2116 		ecc->write_page_raw = marvell_nfc_hw_ecc_bch_write_page_raw;
2117 		ecc->write_page = marvell_nfc_hw_ecc_bch_write_page;
2118 		ecc->write_oob_raw = marvell_nfc_hw_ecc_bch_write_oob_raw;
2119 		ecc->write_oob = marvell_nfc_hw_ecc_bch_write_oob;
2120 	}
2121 
2122 	return 0;
2123 }
2124 
2125 static int marvell_nand_ecc_init(struct mtd_info *mtd,
2126 				 struct nand_ecc_ctrl *ecc)
2127 {
2128 	struct nand_chip *chip = mtd_to_nand(mtd);
2129 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2130 	int ret;
2131 
2132 	if (ecc->mode != NAND_ECC_NONE && (!ecc->size || !ecc->strength)) {
2133 		if (chip->ecc_step_ds && chip->ecc_strength_ds) {
2134 			ecc->size = chip->ecc_step_ds;
2135 			ecc->strength = chip->ecc_strength_ds;
2136 		} else {
2137 			dev_info(nfc->dev,
2138 				 "No minimum ECC strength, using 1b/512B\n");
2139 			ecc->size = 512;
2140 			ecc->strength = 1;
2141 		}
2142 	}
2143 
2144 	switch (ecc->mode) {
2145 	case NAND_ECC_HW:
2146 		ret = marvell_nand_hw_ecc_ctrl_init(mtd, ecc);
2147 		if (ret)
2148 			return ret;
2149 		break;
2150 	case NAND_ECC_NONE:
2151 	case NAND_ECC_SOFT:
2152 		if (!nfc->caps->is_nfcv2 && mtd->writesize != SZ_512 &&
2153 		    mtd->writesize != SZ_2K) {
2154 			dev_err(nfc->dev, "NFCv1 cannot write %d bytes pages\n",
2155 				mtd->writesize);
2156 			return -EINVAL;
2157 		}
2158 		break;
2159 	default:
2160 		return -EINVAL;
2161 	}
2162 
2163 	return 0;
2164 }
2165 
2166 static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
2167 static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
2168 
2169 static struct nand_bbt_descr bbt_main_descr = {
2170 	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
2171 		   NAND_BBT_2BIT | NAND_BBT_VERSION,
2172 	.offs =	8,
2173 	.len = 6,
2174 	.veroffs = 14,
2175 	.maxblocks = 8,	/* Last 8 blocks in each chip */
2176 	.pattern = bbt_pattern
2177 };
2178 
2179 static struct nand_bbt_descr bbt_mirror_descr = {
2180 	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
2181 		   NAND_BBT_2BIT | NAND_BBT_VERSION,
2182 	.offs =	8,
2183 	.len = 6,
2184 	.veroffs = 14,
2185 	.maxblocks = 8,	/* Last 8 blocks in each chip */
2186 	.pattern = bbt_mirror_pattern
2187 };
2188 
2189 static int marvell_nfc_setup_data_interface(struct mtd_info *mtd, int chipnr,
2190 					    const struct nand_data_interface
2191 					    *conf)
2192 {
2193 	struct nand_chip *chip = mtd_to_nand(mtd);
2194 	struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
2195 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2196 	unsigned int period_ns = 1000000000 / clk_get_rate(nfc->core_clk) * 2;
2197 	const struct nand_sdr_timings *sdr;
2198 	struct marvell_nfc_timings nfc_tmg;
2199 	int read_delay;
2200 
2201 	sdr = nand_get_sdr_timings(conf);
2202 	if (IS_ERR(sdr))
2203 		return PTR_ERR(sdr);
2204 
2205 	/*
2206 	 * SDR timings are given in pico-seconds while NFC timings must be
2207 	 * expressed in NAND controller clock cycles, which is half of the
2208 	 * frequency of the accessible ECC clock retrieved by clk_get_rate().
2209 	 * This is not written anywhere in the datasheet but was observed
2210 	 * with an oscilloscope.
2211 	 *
2212 	 * NFC datasheet gives equations from which thoses calculations
2213 	 * are derived, they tend to be slightly more restrictives than the
2214 	 * given core timings and may improve the overall speed.
2215 	 */
2216 	nfc_tmg.tRP = TO_CYCLES(DIV_ROUND_UP(sdr->tRC_min, 2), period_ns) - 1;
2217 	nfc_tmg.tRH = nfc_tmg.tRP;
2218 	nfc_tmg.tWP = TO_CYCLES(DIV_ROUND_UP(sdr->tWC_min, 2), period_ns) - 1;
2219 	nfc_tmg.tWH = nfc_tmg.tWP;
2220 	nfc_tmg.tCS = TO_CYCLES(sdr->tCS_min, period_ns);
2221 	nfc_tmg.tCH = TO_CYCLES(sdr->tCH_min, period_ns) - 1;
2222 	nfc_tmg.tADL = TO_CYCLES(sdr->tADL_min, period_ns);
2223 	/*
2224 	 * Read delay is the time of propagation from SoC pins to NFC internal
2225 	 * logic. With non-EDO timings, this is MIN_RD_DEL_CNT clock cycles. In
2226 	 * EDO mode, an additional delay of tRH must be taken into account so
2227 	 * the data is sampled on the falling edge instead of the rising edge.
2228 	 */
2229 	read_delay = sdr->tRC_min >= 30000 ?
2230 		MIN_RD_DEL_CNT : MIN_RD_DEL_CNT + nfc_tmg.tRH;
2231 
2232 	nfc_tmg.tAR = TO_CYCLES(sdr->tAR_min, period_ns);
2233 	/*
2234 	 * tWHR and tRHW are supposed to be read to write delays (and vice
2235 	 * versa) but in some cases, ie. when doing a change column, they must
2236 	 * be greater than that to be sure tCCS delay is respected.
2237 	 */
2238 	nfc_tmg.tWHR = TO_CYCLES(max_t(int, sdr->tWHR_min, sdr->tCCS_min),
2239 				 period_ns) - 2,
2240 	nfc_tmg.tRHW = TO_CYCLES(max_t(int, sdr->tRHW_min, sdr->tCCS_min),
2241 				 period_ns);
2242 
2243 	/*
2244 	 * NFCv2: Use WAIT_MODE (wait for RB line), do not rely only on delays.
2245 	 * NFCv1: No WAIT_MODE, tR must be maximal.
2246 	 */
2247 	if (nfc->caps->is_nfcv2) {
2248 		nfc_tmg.tR = TO_CYCLES(sdr->tWB_max, period_ns);
2249 	} else {
2250 		nfc_tmg.tR = TO_CYCLES64(sdr->tWB_max + sdr->tR_max,
2251 					 period_ns);
2252 		if (nfc_tmg.tR + 3 > nfc_tmg.tCH)
2253 			nfc_tmg.tR = nfc_tmg.tCH - 3;
2254 		else
2255 			nfc_tmg.tR = 0;
2256 	}
2257 
2258 	if (chipnr < 0)
2259 		return 0;
2260 
2261 	marvell_nand->ndtr0 =
2262 		NDTR0_TRP(nfc_tmg.tRP) |
2263 		NDTR0_TRH(nfc_tmg.tRH) |
2264 		NDTR0_ETRP(nfc_tmg.tRP) |
2265 		NDTR0_TWP(nfc_tmg.tWP) |
2266 		NDTR0_TWH(nfc_tmg.tWH) |
2267 		NDTR0_TCS(nfc_tmg.tCS) |
2268 		NDTR0_TCH(nfc_tmg.tCH);
2269 
2270 	marvell_nand->ndtr1 =
2271 		NDTR1_TAR(nfc_tmg.tAR) |
2272 		NDTR1_TWHR(nfc_tmg.tWHR) |
2273 		NDTR1_TR(nfc_tmg.tR);
2274 
2275 	if (nfc->caps->is_nfcv2) {
2276 		marvell_nand->ndtr0 |=
2277 			NDTR0_RD_CNT_DEL(read_delay) |
2278 			NDTR0_SELCNTR |
2279 			NDTR0_TADL(nfc_tmg.tADL);
2280 
2281 		marvell_nand->ndtr1 |=
2282 			NDTR1_TRHW(nfc_tmg.tRHW) |
2283 			NDTR1_WAIT_MODE;
2284 	}
2285 
2286 	return 0;
2287 }
2288 
2289 static int marvell_nand_chip_init(struct device *dev, struct marvell_nfc *nfc,
2290 				  struct device_node *np)
2291 {
2292 	struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(dev);
2293 	struct marvell_nand_chip *marvell_nand;
2294 	struct mtd_info *mtd;
2295 	struct nand_chip *chip;
2296 	int nsels, ret, i;
2297 	u32 cs, rb;
2298 
2299 	/*
2300 	 * The legacy "num-cs" property indicates the number of CS on the only
2301 	 * chip connected to the controller (legacy bindings does not support
2302 	 * more than one chip). CS are only incremented one by one while the RB
2303 	 * pin is always the #0.
2304 	 *
2305 	 * When not using legacy bindings, a couple of "reg" and "nand-rb"
2306 	 * properties must be filled. For each chip, expressed as a subnode,
2307 	 * "reg" points to the CS lines and "nand-rb" to the RB line.
2308 	 */
2309 	if (pdata) {
2310 		nsels = 1;
2311 	} else if (nfc->caps->legacy_of_bindings &&
2312 		   !of_get_property(np, "num-cs", &nsels)) {
2313 		dev_err(dev, "missing num-cs property\n");
2314 		return -EINVAL;
2315 	} else if (!of_get_property(np, "reg", &nsels)) {
2316 		dev_err(dev, "missing reg property\n");
2317 		return -EINVAL;
2318 	}
2319 
2320 	if (!pdata)
2321 		nsels /= sizeof(u32);
2322 	if (!nsels) {
2323 		dev_err(dev, "invalid reg property size\n");
2324 		return -EINVAL;
2325 	}
2326 
2327 	/* Alloc the nand chip structure */
2328 	marvell_nand = devm_kzalloc(dev, sizeof(*marvell_nand) +
2329 				    (nsels *
2330 				     sizeof(struct marvell_nand_chip_sel)),
2331 				    GFP_KERNEL);
2332 	if (!marvell_nand) {
2333 		dev_err(dev, "could not allocate chip structure\n");
2334 		return -ENOMEM;
2335 	}
2336 
2337 	marvell_nand->nsels = nsels;
2338 	marvell_nand->selected_die = -1;
2339 
2340 	for (i = 0; i < nsels; i++) {
2341 		if (pdata || nfc->caps->legacy_of_bindings) {
2342 			/*
2343 			 * Legacy bindings use the CS lines in natural
2344 			 * order (0, 1, ...)
2345 			 */
2346 			cs = i;
2347 		} else {
2348 			/* Retrieve CS id */
2349 			ret = of_property_read_u32_index(np, "reg", i, &cs);
2350 			if (ret) {
2351 				dev_err(dev, "could not retrieve reg property: %d\n",
2352 					ret);
2353 				return ret;
2354 			}
2355 		}
2356 
2357 		if (cs >= nfc->caps->max_cs_nb) {
2358 			dev_err(dev, "invalid reg value: %u (max CS = %d)\n",
2359 				cs, nfc->caps->max_cs_nb);
2360 			return -EINVAL;
2361 		}
2362 
2363 		if (test_and_set_bit(cs, &nfc->assigned_cs)) {
2364 			dev_err(dev, "CS %d already assigned\n", cs);
2365 			return -EINVAL;
2366 		}
2367 
2368 		/*
2369 		 * The cs variable represents the chip select id, which must be
2370 		 * converted in bit fields for NDCB0 and NDCB2 to select the
2371 		 * right chip. Unfortunately, due to a lack of information on
2372 		 * the subject and incoherent documentation, the user should not
2373 		 * use CS1 and CS3 at all as asserting them is not supported in
2374 		 * a reliable way (due to multiplexing inside ADDR5 field).
2375 		 */
2376 		marvell_nand->sels[i].cs = cs;
2377 		switch (cs) {
2378 		case 0:
2379 		case 2:
2380 			marvell_nand->sels[i].ndcb0_csel = 0;
2381 			break;
2382 		case 1:
2383 		case 3:
2384 			marvell_nand->sels[i].ndcb0_csel = NDCB0_CSEL;
2385 			break;
2386 		default:
2387 			return -EINVAL;
2388 		}
2389 
2390 		/* Retrieve RB id */
2391 		if (pdata || nfc->caps->legacy_of_bindings) {
2392 			/* Legacy bindings always use RB #0 */
2393 			rb = 0;
2394 		} else {
2395 			ret = of_property_read_u32_index(np, "nand-rb", i,
2396 							 &rb);
2397 			if (ret) {
2398 				dev_err(dev,
2399 					"could not retrieve RB property: %d\n",
2400 					ret);
2401 				return ret;
2402 			}
2403 		}
2404 
2405 		if (rb >= nfc->caps->max_rb_nb) {
2406 			dev_err(dev, "invalid reg value: %u (max RB = %d)\n",
2407 				rb, nfc->caps->max_rb_nb);
2408 			return -EINVAL;
2409 		}
2410 
2411 		marvell_nand->sels[i].rb = rb;
2412 	}
2413 
2414 	chip = &marvell_nand->chip;
2415 	chip->controller = &nfc->controller;
2416 	nand_set_flash_node(chip, np);
2417 
2418 	chip->exec_op = marvell_nfc_exec_op;
2419 	chip->select_chip = marvell_nfc_select_chip;
2420 	if (!of_property_read_bool(np, "marvell,nand-keep-config"))
2421 		chip->setup_data_interface = marvell_nfc_setup_data_interface;
2422 
2423 	mtd = nand_to_mtd(chip);
2424 	mtd->dev.parent = dev;
2425 
2426 	/*
2427 	 * Default to HW ECC engine mode. If the nand-ecc-mode property is given
2428 	 * in the DT node, this entry will be overwritten in nand_scan_ident().
2429 	 */
2430 	chip->ecc.mode = NAND_ECC_HW;
2431 
2432 	/*
2433 	 * Save a reference value for timing registers before
2434 	 * ->setup_data_interface() is called.
2435 	 */
2436 	marvell_nand->ndtr0 = readl_relaxed(nfc->regs + NDTR0);
2437 	marvell_nand->ndtr1 = readl_relaxed(nfc->regs + NDTR1);
2438 
2439 	chip->options |= NAND_BUSWIDTH_AUTO;
2440 	ret = nand_scan_ident(mtd, marvell_nand->nsels, NULL);
2441 	if (ret) {
2442 		dev_err(dev, "could not identify the nand chip\n");
2443 		return ret;
2444 	}
2445 
2446 	if (pdata && pdata->flash_bbt)
2447 		chip->bbt_options |= NAND_BBT_USE_FLASH;
2448 
2449 	if (chip->bbt_options & NAND_BBT_USE_FLASH) {
2450 		/*
2451 		 * We'll use a bad block table stored in-flash and don't
2452 		 * allow writing the bad block marker to the flash.
2453 		 */
2454 		chip->bbt_options |= NAND_BBT_NO_OOB_BBM;
2455 		chip->bbt_td = &bbt_main_descr;
2456 		chip->bbt_md = &bbt_mirror_descr;
2457 	}
2458 
2459 	/* Save the chip-specific fields of NDCR */
2460 	marvell_nand->ndcr = NDCR_PAGE_SZ(mtd->writesize);
2461 	if (chip->options & NAND_BUSWIDTH_16)
2462 		marvell_nand->ndcr |= NDCR_DWIDTH_M | NDCR_DWIDTH_C;
2463 
2464 	/*
2465 	 * On small page NANDs, only one cycle is needed to pass the
2466 	 * column address.
2467 	 */
2468 	if (mtd->writesize <= 512) {
2469 		marvell_nand->addr_cyc = 1;
2470 	} else {
2471 		marvell_nand->addr_cyc = 2;
2472 		marvell_nand->ndcr |= NDCR_RA_START;
2473 	}
2474 
2475 	/*
2476 	 * Now add the number of cycles needed to pass the row
2477 	 * address.
2478 	 *
2479 	 * Addressing a chip using CS 2 or 3 should also need the third row
2480 	 * cycle but due to inconsistance in the documentation and lack of
2481 	 * hardware to test this situation, this case is not supported.
2482 	 */
2483 	if (chip->options & NAND_ROW_ADDR_3)
2484 		marvell_nand->addr_cyc += 3;
2485 	else
2486 		marvell_nand->addr_cyc += 2;
2487 
2488 	if (pdata) {
2489 		chip->ecc.size = pdata->ecc_step_size;
2490 		chip->ecc.strength = pdata->ecc_strength;
2491 	}
2492 
2493 	ret = marvell_nand_ecc_init(mtd, &chip->ecc);
2494 	if (ret) {
2495 		dev_err(dev, "ECC init failed: %d\n", ret);
2496 		return ret;
2497 	}
2498 
2499 	if (chip->ecc.mode == NAND_ECC_HW) {
2500 		/*
2501 		 * Subpage write not available with hardware ECC, prohibit also
2502 		 * subpage read as in userspace subpage access would still be
2503 		 * allowed and subpage write, if used, would lead to numerous
2504 		 * uncorrectable ECC errors.
2505 		 */
2506 		chip->options |= NAND_NO_SUBPAGE_WRITE;
2507 	}
2508 
2509 	if (pdata || nfc->caps->legacy_of_bindings) {
2510 		/*
2511 		 * We keep the MTD name unchanged to avoid breaking platforms
2512 		 * where the MTD cmdline parser is used and the bootloader
2513 		 * has not been updated to use the new naming scheme.
2514 		 */
2515 		mtd->name = "pxa3xx_nand-0";
2516 	} else if (!mtd->name) {
2517 		/*
2518 		 * If the new bindings are used and the bootloader has not been
2519 		 * updated to pass a new mtdparts parameter on the cmdline, you
2520 		 * should define the following property in your NAND node, ie:
2521 		 *
2522 		 *	label = "main-storage";
2523 		 *
2524 		 * This way, mtd->name will be set by the core when
2525 		 * nand_set_flash_node() is called.
2526 		 */
2527 		mtd->name = devm_kasprintf(nfc->dev, GFP_KERNEL,
2528 					   "%s:nand.%d", dev_name(nfc->dev),
2529 					   marvell_nand->sels[0].cs);
2530 		if (!mtd->name) {
2531 			dev_err(nfc->dev, "Failed to allocate mtd->name\n");
2532 			return -ENOMEM;
2533 		}
2534 	}
2535 
2536 	ret = nand_scan_tail(mtd);
2537 	if (ret) {
2538 		dev_err(dev, "nand_scan_tail failed: %d\n", ret);
2539 		return ret;
2540 	}
2541 
2542 	if (pdata)
2543 		/* Legacy bindings support only one chip */
2544 		ret = mtd_device_register(mtd, pdata->parts, pdata->nr_parts);
2545 	else
2546 		ret = mtd_device_register(mtd, NULL, 0);
2547 	if (ret) {
2548 		dev_err(dev, "failed to register mtd device: %d\n", ret);
2549 		nand_release(mtd);
2550 		return ret;
2551 	}
2552 
2553 	list_add_tail(&marvell_nand->node, &nfc->chips);
2554 
2555 	return 0;
2556 }
2557 
2558 static int marvell_nand_chips_init(struct device *dev, struct marvell_nfc *nfc)
2559 {
2560 	struct device_node *np = dev->of_node;
2561 	struct device_node *nand_np;
2562 	int max_cs = nfc->caps->max_cs_nb;
2563 	int nchips;
2564 	int ret;
2565 
2566 	if (!np)
2567 		nchips = 1;
2568 	else
2569 		nchips = of_get_child_count(np);
2570 
2571 	if (nchips > max_cs) {
2572 		dev_err(dev, "too many NAND chips: %d (max = %d CS)\n", nchips,
2573 			max_cs);
2574 		return -EINVAL;
2575 	}
2576 
2577 	/*
2578 	 * Legacy bindings do not use child nodes to exhibit NAND chip
2579 	 * properties and layout. Instead, NAND properties are mixed with the
2580 	 * controller ones, and partitions are defined as direct subnodes of the
2581 	 * NAND controller node.
2582 	 */
2583 	if (nfc->caps->legacy_of_bindings) {
2584 		ret = marvell_nand_chip_init(dev, nfc, np);
2585 		return ret;
2586 	}
2587 
2588 	for_each_child_of_node(np, nand_np) {
2589 		ret = marvell_nand_chip_init(dev, nfc, nand_np);
2590 		if (ret) {
2591 			of_node_put(nand_np);
2592 			return ret;
2593 		}
2594 	}
2595 
2596 	return 0;
2597 }
2598 
2599 static void marvell_nand_chips_cleanup(struct marvell_nfc *nfc)
2600 {
2601 	struct marvell_nand_chip *entry, *temp;
2602 
2603 	list_for_each_entry_safe(entry, temp, &nfc->chips, node) {
2604 		nand_release(nand_to_mtd(&entry->chip));
2605 		list_del(&entry->node);
2606 	}
2607 }
2608 
2609 static int marvell_nfc_init_dma(struct marvell_nfc *nfc)
2610 {
2611 	struct platform_device *pdev = container_of(nfc->dev,
2612 						    struct platform_device,
2613 						    dev);
2614 	struct dma_slave_config config = {};
2615 	struct resource *r;
2616 	dma_cap_mask_t mask;
2617 	struct pxad_param param;
2618 	int ret;
2619 
2620 	if (!IS_ENABLED(CONFIG_PXA_DMA)) {
2621 		dev_warn(nfc->dev,
2622 			 "DMA not enabled in configuration\n");
2623 		return -ENOTSUPP;
2624 	}
2625 
2626 	ret = dma_set_mask_and_coherent(nfc->dev, DMA_BIT_MASK(32));
2627 	if (ret)
2628 		return ret;
2629 
2630 	r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
2631 	if (!r) {
2632 		dev_err(nfc->dev, "No resource defined for data DMA\n");
2633 		return -ENXIO;
2634 	}
2635 
2636 	param.drcmr = r->start;
2637 	param.prio = PXAD_PRIO_LOWEST;
2638 	dma_cap_zero(mask);
2639 	dma_cap_set(DMA_SLAVE, mask);
2640 	nfc->dma_chan =
2641 		dma_request_slave_channel_compat(mask, pxad_filter_fn,
2642 						 &param, nfc->dev,
2643 						 "data");
2644 	if (!nfc->dma_chan) {
2645 		dev_err(nfc->dev,
2646 			"Unable to request data DMA channel\n");
2647 		return -ENODEV;
2648 	}
2649 
2650 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2651 	if (!r)
2652 		return -ENXIO;
2653 
2654 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2655 	config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2656 	config.src_addr = r->start + NDDB;
2657 	config.dst_addr = r->start + NDDB;
2658 	config.src_maxburst = 32;
2659 	config.dst_maxburst = 32;
2660 	ret = dmaengine_slave_config(nfc->dma_chan, &config);
2661 	if (ret < 0) {
2662 		dev_err(nfc->dev, "Failed to configure DMA channel\n");
2663 		return ret;
2664 	}
2665 
2666 	/*
2667 	 * DMA must act on length multiple of 32 and this length may be
2668 	 * bigger than the destination buffer. Use this buffer instead
2669 	 * for DMA transfers and then copy the desired amount of data to
2670 	 * the provided buffer.
2671 	 */
2672 	nfc->dma_buf = kmalloc(MAX_CHUNK_SIZE, GFP_KERNEL | GFP_DMA);
2673 	if (!nfc->dma_buf)
2674 		return -ENOMEM;
2675 
2676 	nfc->use_dma = true;
2677 
2678 	return 0;
2679 }
2680 
2681 static int marvell_nfc_init(struct marvell_nfc *nfc)
2682 {
2683 	struct device_node *np = nfc->dev->of_node;
2684 
2685 	/*
2686 	 * Some SoCs like A7k/A8k need to enable manually the NAND
2687 	 * controller, gated clocks and reset bits to avoid being bootloader
2688 	 * dependent. This is done through the use of the System Functions
2689 	 * registers.
2690 	 */
2691 	if (nfc->caps->need_system_controller) {
2692 		struct regmap *sysctrl_base =
2693 			syscon_regmap_lookup_by_phandle(np,
2694 							"marvell,system-controller");
2695 		u32 reg;
2696 
2697 		if (IS_ERR(sysctrl_base))
2698 			return PTR_ERR(sysctrl_base);
2699 
2700 		reg = GENCONF_SOC_DEVICE_MUX_NFC_EN |
2701 		      GENCONF_SOC_DEVICE_MUX_ECC_CLK_RST |
2702 		      GENCONF_SOC_DEVICE_MUX_ECC_CORE_RST |
2703 		      GENCONF_SOC_DEVICE_MUX_NFC_INT_EN;
2704 		regmap_write(sysctrl_base, GENCONF_SOC_DEVICE_MUX, reg);
2705 
2706 		regmap_read(sysctrl_base, GENCONF_CLK_GATING_CTRL, &reg);
2707 		reg |= GENCONF_CLK_GATING_CTRL_ND_GATE;
2708 		regmap_write(sysctrl_base, GENCONF_CLK_GATING_CTRL, reg);
2709 
2710 		regmap_read(sysctrl_base, GENCONF_ND_CLK_CTRL, &reg);
2711 		reg |= GENCONF_ND_CLK_CTRL_EN;
2712 		regmap_write(sysctrl_base, GENCONF_ND_CLK_CTRL, reg);
2713 	}
2714 
2715 	/* Configure the DMA if appropriate */
2716 	if (!nfc->caps->is_nfcv2)
2717 		marvell_nfc_init_dma(nfc);
2718 
2719 	/*
2720 	 * ECC operations and interruptions are only enabled when specifically
2721 	 * needed. ECC shall not be activated in the early stages (fails probe).
2722 	 * Arbiter flag, even if marked as "reserved", must be set (empirical).
2723 	 * SPARE_EN bit must always be set or ECC bytes will not be at the same
2724 	 * offset in the read page and this will fail the protection.
2725 	 */
2726 	writel_relaxed(NDCR_ALL_INT | NDCR_ND_ARB_EN | NDCR_SPARE_EN |
2727 		       NDCR_RD_ID_CNT(NFCV1_READID_LEN), nfc->regs + NDCR);
2728 	writel_relaxed(0xFFFFFFFF, nfc->regs + NDSR);
2729 	writel_relaxed(0, nfc->regs + NDECCCTRL);
2730 
2731 	return 0;
2732 }
2733 
2734 static int marvell_nfc_probe(struct platform_device *pdev)
2735 {
2736 	struct device *dev = &pdev->dev;
2737 	struct resource *r;
2738 	struct marvell_nfc *nfc;
2739 	int ret;
2740 	int irq;
2741 
2742 	nfc = devm_kzalloc(&pdev->dev, sizeof(struct marvell_nfc),
2743 			   GFP_KERNEL);
2744 	if (!nfc)
2745 		return -ENOMEM;
2746 
2747 	nfc->dev = dev;
2748 	nand_hw_control_init(&nfc->controller);
2749 	INIT_LIST_HEAD(&nfc->chips);
2750 
2751 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2752 	nfc->regs = devm_ioremap_resource(dev, r);
2753 	if (IS_ERR(nfc->regs))
2754 		return PTR_ERR(nfc->regs);
2755 
2756 	irq = platform_get_irq(pdev, 0);
2757 	if (irq < 0) {
2758 		dev_err(dev, "failed to retrieve irq\n");
2759 		return irq;
2760 	}
2761 
2762 	nfc->core_clk = devm_clk_get(&pdev->dev, "core");
2763 
2764 	/* Managed the legacy case (when the first clock was not named) */
2765 	if (nfc->core_clk == ERR_PTR(-ENOENT))
2766 		nfc->core_clk = devm_clk_get(&pdev->dev, NULL);
2767 
2768 	if (IS_ERR(nfc->core_clk))
2769 		return PTR_ERR(nfc->core_clk);
2770 
2771 	ret = clk_prepare_enable(nfc->core_clk);
2772 	if (ret)
2773 		return ret;
2774 
2775 	nfc->reg_clk = devm_clk_get(&pdev->dev, "reg");
2776 	if (PTR_ERR(nfc->reg_clk) != -ENOENT) {
2777 		if (!IS_ERR(nfc->reg_clk)) {
2778 			ret = clk_prepare_enable(nfc->reg_clk);
2779 			if (ret)
2780 				goto unprepare_core_clk;
2781 		} else {
2782 			ret = PTR_ERR(nfc->reg_clk);
2783 			goto unprepare_core_clk;
2784 		}
2785 	}
2786 
2787 	marvell_nfc_disable_int(nfc, NDCR_ALL_INT);
2788 	marvell_nfc_clear_int(nfc, NDCR_ALL_INT);
2789 	ret = devm_request_irq(dev, irq, marvell_nfc_isr,
2790 			       0, "marvell-nfc", nfc);
2791 	if (ret)
2792 		goto unprepare_reg_clk;
2793 
2794 	/* Get NAND controller capabilities */
2795 	if (pdev->id_entry)
2796 		nfc->caps = (void *)pdev->id_entry->driver_data;
2797 	else
2798 		nfc->caps = of_device_get_match_data(&pdev->dev);
2799 
2800 	if (!nfc->caps) {
2801 		dev_err(dev, "Could not retrieve NFC caps\n");
2802 		ret = -EINVAL;
2803 		goto unprepare_reg_clk;
2804 	}
2805 
2806 	/* Init the controller and then probe the chips */
2807 	ret = marvell_nfc_init(nfc);
2808 	if (ret)
2809 		goto unprepare_reg_clk;
2810 
2811 	platform_set_drvdata(pdev, nfc);
2812 
2813 	ret = marvell_nand_chips_init(dev, nfc);
2814 	if (ret)
2815 		goto unprepare_reg_clk;
2816 
2817 	return 0;
2818 
2819 unprepare_reg_clk:
2820 	clk_disable_unprepare(nfc->reg_clk);
2821 unprepare_core_clk:
2822 	clk_disable_unprepare(nfc->core_clk);
2823 
2824 	return ret;
2825 }
2826 
2827 static int marvell_nfc_remove(struct platform_device *pdev)
2828 {
2829 	struct marvell_nfc *nfc = platform_get_drvdata(pdev);
2830 
2831 	marvell_nand_chips_cleanup(nfc);
2832 
2833 	if (nfc->use_dma) {
2834 		dmaengine_terminate_all(nfc->dma_chan);
2835 		dma_release_channel(nfc->dma_chan);
2836 	}
2837 
2838 	clk_disable_unprepare(nfc->reg_clk);
2839 	clk_disable_unprepare(nfc->core_clk);
2840 
2841 	return 0;
2842 }
2843 
2844 static const struct marvell_nfc_caps marvell_armada_8k_nfc_caps = {
2845 	.max_cs_nb = 4,
2846 	.max_rb_nb = 2,
2847 	.need_system_controller = true,
2848 	.is_nfcv2 = true,
2849 };
2850 
2851 static const struct marvell_nfc_caps marvell_armada370_nfc_caps = {
2852 	.max_cs_nb = 4,
2853 	.max_rb_nb = 2,
2854 	.is_nfcv2 = true,
2855 };
2856 
2857 static const struct marvell_nfc_caps marvell_pxa3xx_nfc_caps = {
2858 	.max_cs_nb = 2,
2859 	.max_rb_nb = 1,
2860 	.use_dma = true,
2861 };
2862 
2863 static const struct marvell_nfc_caps marvell_armada_8k_nfc_legacy_caps = {
2864 	.max_cs_nb = 4,
2865 	.max_rb_nb = 2,
2866 	.need_system_controller = true,
2867 	.legacy_of_bindings = true,
2868 	.is_nfcv2 = true,
2869 };
2870 
2871 static const struct marvell_nfc_caps marvell_armada370_nfc_legacy_caps = {
2872 	.max_cs_nb = 4,
2873 	.max_rb_nb = 2,
2874 	.legacy_of_bindings = true,
2875 	.is_nfcv2 = true,
2876 };
2877 
2878 static const struct marvell_nfc_caps marvell_pxa3xx_nfc_legacy_caps = {
2879 	.max_cs_nb = 2,
2880 	.max_rb_nb = 1,
2881 	.legacy_of_bindings = true,
2882 	.use_dma = true,
2883 };
2884 
2885 static const struct platform_device_id marvell_nfc_platform_ids[] = {
2886 	{
2887 		.name = "pxa3xx-nand",
2888 		.driver_data = (kernel_ulong_t)&marvell_pxa3xx_nfc_legacy_caps,
2889 	},
2890 	{ /* sentinel */ },
2891 };
2892 MODULE_DEVICE_TABLE(platform, marvell_nfc_platform_ids);
2893 
2894 static const struct of_device_id marvell_nfc_of_ids[] = {
2895 	{
2896 		.compatible = "marvell,armada-8k-nand-controller",
2897 		.data = &marvell_armada_8k_nfc_caps,
2898 	},
2899 	{
2900 		.compatible = "marvell,armada370-nand-controller",
2901 		.data = &marvell_armada370_nfc_caps,
2902 	},
2903 	{
2904 		.compatible = "marvell,pxa3xx-nand-controller",
2905 		.data = &marvell_pxa3xx_nfc_caps,
2906 	},
2907 	/* Support for old/deprecated bindings: */
2908 	{
2909 		.compatible = "marvell,armada-8k-nand",
2910 		.data = &marvell_armada_8k_nfc_legacy_caps,
2911 	},
2912 	{
2913 		.compatible = "marvell,armada370-nand",
2914 		.data = &marvell_armada370_nfc_legacy_caps,
2915 	},
2916 	{
2917 		.compatible = "marvell,pxa3xx-nand",
2918 		.data = &marvell_pxa3xx_nfc_legacy_caps,
2919 	},
2920 	{ /* sentinel */ },
2921 };
2922 MODULE_DEVICE_TABLE(of, marvell_nfc_of_ids);
2923 
2924 static struct platform_driver marvell_nfc_driver = {
2925 	.driver	= {
2926 		.name		= "marvell-nfc",
2927 		.of_match_table = marvell_nfc_of_ids,
2928 	},
2929 	.id_table = marvell_nfc_platform_ids,
2930 	.probe = marvell_nfc_probe,
2931 	.remove	= marvell_nfc_remove,
2932 };
2933 module_platform_driver(marvell_nfc_driver);
2934 
2935 MODULE_LICENSE("GPL");
2936 MODULE_DESCRIPTION("Marvell NAND controller driver");
2937