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 
to_marvell_nand(struct nand_chip * chip)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 
to_nand_sel(struct marvell_nand_chip * nand)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_controller 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 
to_marvell_nfc(struct nand_controller * ctrl)338 static inline struct marvell_nfc *to_marvell_nfc(struct nand_controller *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  */
cond_delay(unsigned int ns)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  */
marvell_nfc_disable_int(struct marvell_nfc * nfc,u32 int_mask)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 
marvell_nfc_enable_int(struct marvell_nfc * nfc,u32 int_mask)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 
marvell_nfc_clear_int(struct marvell_nfc * nfc,u32 int_mask)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 
marvell_nfc_force_byte_access(struct nand_chip * chip,bool force_8bit)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 
marvell_nfc_wait_ndrun(struct nand_chip * chip)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  */
marvell_nfc_prepare_cmd(struct nand_chip * chip)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 
marvell_nfc_send_cmd(struct nand_chip * chip,struct marvell_nfc_op * nfc_op)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 
marvell_nfc_end_cmd(struct nand_chip * chip,int flag,const char * label)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 
marvell_nfc_wait_cmdd(struct nand_chip * chip)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 
marvell_nfc_wait_op(struct nand_chip * chip,unsigned int timeout_ms)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 
marvell_nfc_select_chip(struct mtd_info * mtd,int die_nr)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 	writel_relaxed(marvell_nand->ndtr0, nfc->regs + NDTR0);
654 	writel_relaxed(marvell_nand->ndtr1, nfc->regs + NDTR1);
655 
656 	/*
657 	 * Reset the NDCR register to a clean state for this particular chip,
658 	 * also clear ND_RUN bit.
659 	 */
660 	ndcr_generic = readl_relaxed(nfc->regs + NDCR) &
661 		       NDCR_GENERIC_FIELDS_MASK & ~NDCR_ND_RUN;
662 	writel_relaxed(ndcr_generic | marvell_nand->ndcr, nfc->regs + NDCR);
663 
664 	/* Also reset the interrupt status register */
665 	marvell_nfc_clear_int(nfc, NDCR_ALL_INT);
666 
667 	nfc->selected_chip = chip;
668 	marvell_nand->selected_die = die_nr;
669 }
670 
marvell_nfc_isr(int irq,void * dev_id)671 static irqreturn_t marvell_nfc_isr(int irq, void *dev_id)
672 {
673 	struct marvell_nfc *nfc = dev_id;
674 	u32 st = readl_relaxed(nfc->regs + NDSR);
675 	u32 ien = (~readl_relaxed(nfc->regs + NDCR)) & NDCR_ALL_INT;
676 
677 	/*
678 	 * RDY interrupt mask is one bit in NDCR while there are two status
679 	 * bit in NDSR (RDY[cs0/cs2] and RDY[cs1/cs3]).
680 	 */
681 	if (st & NDSR_RDY(1))
682 		st |= NDSR_RDY(0);
683 
684 	if (!(st & ien))
685 		return IRQ_NONE;
686 
687 	marvell_nfc_disable_int(nfc, st & NDCR_ALL_INT);
688 
689 	if (!(st & (NDSR_RDDREQ | NDSR_WRDREQ | NDSR_WRCMDREQ)))
690 		complete(&nfc->complete);
691 
692 	return IRQ_HANDLED;
693 }
694 
695 /* HW ECC related functions */
marvell_nfc_enable_hw_ecc(struct nand_chip * chip)696 static void marvell_nfc_enable_hw_ecc(struct nand_chip *chip)
697 {
698 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
699 	u32 ndcr = readl_relaxed(nfc->regs + NDCR);
700 
701 	if (!(ndcr & NDCR_ECC_EN)) {
702 		writel_relaxed(ndcr | NDCR_ECC_EN, nfc->regs + NDCR);
703 
704 		/*
705 		 * When enabling BCH, set threshold to 0 to always know the
706 		 * number of corrected bitflips.
707 		 */
708 		if (chip->ecc.algo == NAND_ECC_BCH)
709 			writel_relaxed(NDECCCTRL_BCH_EN, nfc->regs + NDECCCTRL);
710 	}
711 }
712 
marvell_nfc_disable_hw_ecc(struct nand_chip * chip)713 static void marvell_nfc_disable_hw_ecc(struct nand_chip *chip)
714 {
715 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
716 	u32 ndcr = readl_relaxed(nfc->regs + NDCR);
717 
718 	if (ndcr & NDCR_ECC_EN) {
719 		writel_relaxed(ndcr & ~NDCR_ECC_EN, nfc->regs + NDCR);
720 		if (chip->ecc.algo == NAND_ECC_BCH)
721 			writel_relaxed(0, nfc->regs + NDECCCTRL);
722 	}
723 }
724 
725 /* DMA related helpers */
marvell_nfc_enable_dma(struct marvell_nfc * nfc)726 static void marvell_nfc_enable_dma(struct marvell_nfc *nfc)
727 {
728 	u32 reg;
729 
730 	reg = readl_relaxed(nfc->regs + NDCR);
731 	writel_relaxed(reg | NDCR_DMA_EN, nfc->regs + NDCR);
732 }
733 
marvell_nfc_disable_dma(struct marvell_nfc * nfc)734 static void marvell_nfc_disable_dma(struct marvell_nfc *nfc)
735 {
736 	u32 reg;
737 
738 	reg = readl_relaxed(nfc->regs + NDCR);
739 	writel_relaxed(reg & ~NDCR_DMA_EN, nfc->regs + NDCR);
740 }
741 
742 /* Read/write PIO/DMA accessors */
marvell_nfc_xfer_data_dma(struct marvell_nfc * nfc,enum dma_data_direction direction,unsigned int len)743 static int marvell_nfc_xfer_data_dma(struct marvell_nfc *nfc,
744 				     enum dma_data_direction direction,
745 				     unsigned int len)
746 {
747 	unsigned int dma_len = min_t(int, ALIGN(len, 32), MAX_CHUNK_SIZE);
748 	struct dma_async_tx_descriptor *tx;
749 	struct scatterlist sg;
750 	dma_cookie_t cookie;
751 	int ret;
752 
753 	marvell_nfc_enable_dma(nfc);
754 	/* Prepare the DMA transfer */
755 	sg_init_one(&sg, nfc->dma_buf, dma_len);
756 	dma_map_sg(nfc->dma_chan->device->dev, &sg, 1, direction);
757 	tx = dmaengine_prep_slave_sg(nfc->dma_chan, &sg, 1,
758 				     direction == DMA_FROM_DEVICE ?
759 				     DMA_DEV_TO_MEM : DMA_MEM_TO_DEV,
760 				     DMA_PREP_INTERRUPT);
761 	if (!tx) {
762 		dev_err(nfc->dev, "Could not prepare DMA S/G list\n");
763 		return -ENXIO;
764 	}
765 
766 	/* Do the task and wait for it to finish */
767 	cookie = dmaengine_submit(tx);
768 	ret = dma_submit_error(cookie);
769 	if (ret)
770 		return -EIO;
771 
772 	dma_async_issue_pending(nfc->dma_chan);
773 	ret = marvell_nfc_wait_cmdd(nfc->selected_chip);
774 	dma_unmap_sg(nfc->dma_chan->device->dev, &sg, 1, direction);
775 	marvell_nfc_disable_dma(nfc);
776 	if (ret) {
777 		dev_err(nfc->dev, "Timeout waiting for DMA (status: %d)\n",
778 			dmaengine_tx_status(nfc->dma_chan, cookie, NULL));
779 		dmaengine_terminate_all(nfc->dma_chan);
780 		return -ETIMEDOUT;
781 	}
782 
783 	return 0;
784 }
785 
marvell_nfc_xfer_data_in_pio(struct marvell_nfc * nfc,u8 * in,unsigned int len)786 static int marvell_nfc_xfer_data_in_pio(struct marvell_nfc *nfc, u8 *in,
787 					unsigned int len)
788 {
789 	unsigned int last_len = len % FIFO_DEPTH;
790 	unsigned int last_full_offset = round_down(len, FIFO_DEPTH);
791 	int i;
792 
793 	for (i = 0; i < last_full_offset; i += FIFO_DEPTH)
794 		ioread32_rep(nfc->regs + NDDB, in + i, FIFO_REP(FIFO_DEPTH));
795 
796 	if (last_len) {
797 		u8 tmp_buf[FIFO_DEPTH];
798 
799 		ioread32_rep(nfc->regs + NDDB, tmp_buf, FIFO_REP(FIFO_DEPTH));
800 		memcpy(in + last_full_offset, tmp_buf, last_len);
801 	}
802 
803 	return 0;
804 }
805 
marvell_nfc_xfer_data_out_pio(struct marvell_nfc * nfc,const u8 * out,unsigned int len)806 static int marvell_nfc_xfer_data_out_pio(struct marvell_nfc *nfc, const u8 *out,
807 					 unsigned int len)
808 {
809 	unsigned int last_len = len % FIFO_DEPTH;
810 	unsigned int last_full_offset = round_down(len, FIFO_DEPTH);
811 	int i;
812 
813 	for (i = 0; i < last_full_offset; i += FIFO_DEPTH)
814 		iowrite32_rep(nfc->regs + NDDB, out + i, FIFO_REP(FIFO_DEPTH));
815 
816 	if (last_len) {
817 		u8 tmp_buf[FIFO_DEPTH];
818 
819 		memcpy(tmp_buf, out + last_full_offset, last_len);
820 		iowrite32_rep(nfc->regs + NDDB, tmp_buf, FIFO_REP(FIFO_DEPTH));
821 	}
822 
823 	return 0;
824 }
825 
marvell_nfc_check_empty_chunk(struct nand_chip * chip,u8 * data,int data_len,u8 * spare,int spare_len,u8 * ecc,int ecc_len,unsigned int * max_bitflips)826 static void marvell_nfc_check_empty_chunk(struct nand_chip *chip,
827 					  u8 *data, int data_len,
828 					  u8 *spare, int spare_len,
829 					  u8 *ecc, int ecc_len,
830 					  unsigned int *max_bitflips)
831 {
832 	struct mtd_info *mtd = nand_to_mtd(chip);
833 	int bf;
834 
835 	/*
836 	 * Blank pages (all 0xFF) that have not been written may be recognized
837 	 * as bad if bitflips occur, so whenever an uncorrectable error occurs,
838 	 * check if the entire page (with ECC bytes) is actually blank or not.
839 	 */
840 	if (!data)
841 		data_len = 0;
842 	if (!spare)
843 		spare_len = 0;
844 	if (!ecc)
845 		ecc_len = 0;
846 
847 	bf = nand_check_erased_ecc_chunk(data, data_len, ecc, ecc_len,
848 					 spare, spare_len, chip->ecc.strength);
849 	if (bf < 0) {
850 		mtd->ecc_stats.failed++;
851 		return;
852 	}
853 
854 	/* Update the stats and max_bitflips */
855 	mtd->ecc_stats.corrected += bf;
856 	*max_bitflips = max_t(unsigned int, *max_bitflips, bf);
857 }
858 
859 /*
860  * Check a chunk is correct or not according to hardware ECC engine.
861  * mtd->ecc_stats.corrected is updated, as well as max_bitflips, however
862  * mtd->ecc_stats.failure is not, the function will instead return a non-zero
863  * value indicating that a check on the emptyness of the subpage must be
864  * performed before declaring the subpage corrupted.
865  */
marvell_nfc_hw_ecc_correct(struct nand_chip * chip,unsigned int * max_bitflips)866 static int marvell_nfc_hw_ecc_correct(struct nand_chip *chip,
867 				      unsigned int *max_bitflips)
868 {
869 	struct mtd_info *mtd = nand_to_mtd(chip);
870 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
871 	int bf = 0;
872 	u32 ndsr;
873 
874 	ndsr = readl_relaxed(nfc->regs + NDSR);
875 
876 	/* Check uncorrectable error flag */
877 	if (ndsr & NDSR_UNCERR) {
878 		writel_relaxed(ndsr, nfc->regs + NDSR);
879 
880 		/*
881 		 * Do not increment ->ecc_stats.failed now, instead, return a
882 		 * non-zero value to indicate that this chunk was apparently
883 		 * bad, and it should be check to see if it empty or not. If
884 		 * the chunk (with ECC bytes) is not declared empty, the calling
885 		 * function must increment the failure count.
886 		 */
887 		return -EBADMSG;
888 	}
889 
890 	/* Check correctable error flag */
891 	if (ndsr & NDSR_CORERR) {
892 		writel_relaxed(ndsr, nfc->regs + NDSR);
893 
894 		if (chip->ecc.algo == NAND_ECC_BCH)
895 			bf = NDSR_ERRCNT(ndsr);
896 		else
897 			bf = 1;
898 	}
899 
900 	/* Update the stats and max_bitflips */
901 	mtd->ecc_stats.corrected += bf;
902 	*max_bitflips = max_t(unsigned int, *max_bitflips, bf);
903 
904 	return 0;
905 }
906 
907 /* Hamming read helpers */
marvell_nfc_hw_ecc_hmg_do_read_page(struct nand_chip * chip,u8 * data_buf,u8 * oob_buf,bool raw,int page)908 static int marvell_nfc_hw_ecc_hmg_do_read_page(struct nand_chip *chip,
909 					       u8 *data_buf, u8 *oob_buf,
910 					       bool raw, int page)
911 {
912 	struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
913 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
914 	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
915 	struct marvell_nfc_op nfc_op = {
916 		.ndcb[0] = NDCB0_CMD_TYPE(TYPE_READ) |
917 			   NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
918 			   NDCB0_DBC |
919 			   NDCB0_CMD1(NAND_CMD_READ0) |
920 			   NDCB0_CMD2(NAND_CMD_READSTART),
921 		.ndcb[1] = NDCB1_ADDRS_PAGE(page),
922 		.ndcb[2] = NDCB2_ADDR5_PAGE(page),
923 	};
924 	unsigned int oob_bytes = lt->spare_bytes + (raw ? lt->ecc_bytes : 0);
925 	int ret;
926 
927 	/* NFCv2 needs more information about the operation being executed */
928 	if (nfc->caps->is_nfcv2)
929 		nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
930 
931 	ret = marvell_nfc_prepare_cmd(chip);
932 	if (ret)
933 		return ret;
934 
935 	marvell_nfc_send_cmd(chip, &nfc_op);
936 	ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
937 				  "RDDREQ while draining FIFO (data/oob)");
938 	if (ret)
939 		return ret;
940 
941 	/*
942 	 * Read the page then the OOB area. Unlike what is shown in current
943 	 * documentation, spare bytes are protected by the ECC engine, and must
944 	 * be at the beginning of the OOB area or running this driver on legacy
945 	 * systems will prevent the discovery of the BBM/BBT.
946 	 */
947 	if (nfc->use_dma) {
948 		marvell_nfc_xfer_data_dma(nfc, DMA_FROM_DEVICE,
949 					  lt->data_bytes + oob_bytes);
950 		memcpy(data_buf, nfc->dma_buf, lt->data_bytes);
951 		memcpy(oob_buf, nfc->dma_buf + lt->data_bytes, oob_bytes);
952 	} else {
953 		marvell_nfc_xfer_data_in_pio(nfc, data_buf, lt->data_bytes);
954 		marvell_nfc_xfer_data_in_pio(nfc, oob_buf, oob_bytes);
955 	}
956 
957 	ret = marvell_nfc_wait_cmdd(chip);
958 
959 	return ret;
960 }
961 
marvell_nfc_hw_ecc_hmg_read_page_raw(struct mtd_info * mtd,struct nand_chip * chip,u8 * buf,int oob_required,int page)962 static int marvell_nfc_hw_ecc_hmg_read_page_raw(struct mtd_info *mtd,
963 						struct nand_chip *chip, u8 *buf,
964 						int oob_required, int page)
965 {
966 	return marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi,
967 						   true, page);
968 }
969 
marvell_nfc_hw_ecc_hmg_read_page(struct mtd_info * mtd,struct nand_chip * chip,u8 * buf,int oob_required,int page)970 static int marvell_nfc_hw_ecc_hmg_read_page(struct mtd_info *mtd,
971 					    struct nand_chip *chip,
972 					    u8 *buf, int oob_required,
973 					    int page)
974 {
975 	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
976 	unsigned int full_sz = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
977 	int max_bitflips = 0, ret;
978 	u8 *raw_buf;
979 
980 	marvell_nfc_enable_hw_ecc(chip);
981 	marvell_nfc_hw_ecc_hmg_do_read_page(chip, buf, chip->oob_poi, false,
982 					    page);
983 	ret = marvell_nfc_hw_ecc_correct(chip, &max_bitflips);
984 	marvell_nfc_disable_hw_ecc(chip);
985 
986 	if (!ret)
987 		return max_bitflips;
988 
989 	/*
990 	 * When ECC failures are detected, check if the full page has been
991 	 * written or not. Ignore the failure if it is actually empty.
992 	 */
993 	raw_buf = kmalloc(full_sz, GFP_KERNEL);
994 	if (!raw_buf)
995 		return -ENOMEM;
996 
997 	marvell_nfc_hw_ecc_hmg_do_read_page(chip, raw_buf, raw_buf +
998 					    lt->data_bytes, true, page);
999 	marvell_nfc_check_empty_chunk(chip, raw_buf, full_sz, NULL, 0, NULL, 0,
1000 				      &max_bitflips);
1001 	kfree(raw_buf);
1002 
1003 	return max_bitflips;
1004 }
1005 
1006 /*
1007  * Spare area in Hamming layouts is not protected by the ECC engine (even if
1008  * it appears before the ECC bytes when reading), the ->read_oob_raw() function
1009  * also stands for ->read_oob().
1010  */
marvell_nfc_hw_ecc_hmg_read_oob_raw(struct mtd_info * mtd,struct nand_chip * chip,int page)1011 static int marvell_nfc_hw_ecc_hmg_read_oob_raw(struct mtd_info *mtd,
1012 					       struct nand_chip *chip, int page)
1013 {
1014 	/* Invalidate page cache */
1015 	chip->pagebuf = -1;
1016 
1017 	return marvell_nfc_hw_ecc_hmg_do_read_page(chip, chip->data_buf,
1018 						   chip->oob_poi, true, page);
1019 }
1020 
1021 /* Hamming write helpers */
marvell_nfc_hw_ecc_hmg_do_write_page(struct nand_chip * chip,const u8 * data_buf,const u8 * oob_buf,bool raw,int page)1022 static int marvell_nfc_hw_ecc_hmg_do_write_page(struct nand_chip *chip,
1023 						const u8 *data_buf,
1024 						const u8 *oob_buf, bool raw,
1025 						int page)
1026 {
1027 	struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
1028 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1029 	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1030 	struct marvell_nfc_op nfc_op = {
1031 		.ndcb[0] = NDCB0_CMD_TYPE(TYPE_WRITE) |
1032 			   NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
1033 			   NDCB0_CMD1(NAND_CMD_SEQIN) |
1034 			   NDCB0_CMD2(NAND_CMD_PAGEPROG) |
1035 			   NDCB0_DBC,
1036 		.ndcb[1] = NDCB1_ADDRS_PAGE(page),
1037 		.ndcb[2] = NDCB2_ADDR5_PAGE(page),
1038 	};
1039 	unsigned int oob_bytes = lt->spare_bytes + (raw ? lt->ecc_bytes : 0);
1040 	int ret;
1041 
1042 	/* NFCv2 needs more information about the operation being executed */
1043 	if (nfc->caps->is_nfcv2)
1044 		nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
1045 
1046 	ret = marvell_nfc_prepare_cmd(chip);
1047 	if (ret)
1048 		return ret;
1049 
1050 	marvell_nfc_send_cmd(chip, &nfc_op);
1051 	ret = marvell_nfc_end_cmd(chip, NDSR_WRDREQ,
1052 				  "WRDREQ while loading FIFO (data)");
1053 	if (ret)
1054 		return ret;
1055 
1056 	/* Write the page then the OOB area */
1057 	if (nfc->use_dma) {
1058 		memcpy(nfc->dma_buf, data_buf, lt->data_bytes);
1059 		memcpy(nfc->dma_buf + lt->data_bytes, oob_buf, oob_bytes);
1060 		marvell_nfc_xfer_data_dma(nfc, DMA_TO_DEVICE, lt->data_bytes +
1061 					  lt->ecc_bytes + lt->spare_bytes);
1062 	} else {
1063 		marvell_nfc_xfer_data_out_pio(nfc, data_buf, lt->data_bytes);
1064 		marvell_nfc_xfer_data_out_pio(nfc, oob_buf, oob_bytes);
1065 	}
1066 
1067 	ret = marvell_nfc_wait_cmdd(chip);
1068 	if (ret)
1069 		return ret;
1070 
1071 	ret = marvell_nfc_wait_op(chip,
1072 				  PSEC_TO_MSEC(chip->data_interface.timings.sdr.tPROG_max));
1073 	return ret;
1074 }
1075 
marvell_nfc_hw_ecc_hmg_write_page_raw(struct mtd_info * mtd,struct nand_chip * chip,const u8 * buf,int oob_required,int page)1076 static int marvell_nfc_hw_ecc_hmg_write_page_raw(struct mtd_info *mtd,
1077 						 struct nand_chip *chip,
1078 						 const u8 *buf,
1079 						 int oob_required, int page)
1080 {
1081 	return marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi,
1082 						    true, page);
1083 }
1084 
marvell_nfc_hw_ecc_hmg_write_page(struct mtd_info * mtd,struct nand_chip * chip,const u8 * buf,int oob_required,int page)1085 static int marvell_nfc_hw_ecc_hmg_write_page(struct mtd_info *mtd,
1086 					     struct nand_chip *chip,
1087 					     const u8 *buf,
1088 					     int oob_required, int page)
1089 {
1090 	int ret;
1091 
1092 	marvell_nfc_enable_hw_ecc(chip);
1093 	ret = marvell_nfc_hw_ecc_hmg_do_write_page(chip, buf, chip->oob_poi,
1094 						   false, page);
1095 	marvell_nfc_disable_hw_ecc(chip);
1096 
1097 	return ret;
1098 }
1099 
1100 /*
1101  * Spare area in Hamming layouts is not protected by the ECC engine (even if
1102  * it appears before the ECC bytes when reading), the ->write_oob_raw() function
1103  * also stands for ->write_oob().
1104  */
marvell_nfc_hw_ecc_hmg_write_oob_raw(struct mtd_info * mtd,struct nand_chip * chip,int page)1105 static int marvell_nfc_hw_ecc_hmg_write_oob_raw(struct mtd_info *mtd,
1106 						struct nand_chip *chip,
1107 						int page)
1108 {
1109 	/* Invalidate page cache */
1110 	chip->pagebuf = -1;
1111 
1112 	memset(chip->data_buf, 0xFF, mtd->writesize);
1113 
1114 	return marvell_nfc_hw_ecc_hmg_do_write_page(chip, chip->data_buf,
1115 						    chip->oob_poi, true, page);
1116 }
1117 
1118 /* BCH read helpers */
marvell_nfc_hw_ecc_bch_read_page_raw(struct mtd_info * mtd,struct nand_chip * chip,u8 * buf,int oob_required,int page)1119 static int marvell_nfc_hw_ecc_bch_read_page_raw(struct mtd_info *mtd,
1120 						struct nand_chip *chip, u8 *buf,
1121 						int oob_required, int page)
1122 {
1123 	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1124 	u8 *oob = chip->oob_poi;
1125 	int chunk_size = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
1126 	int ecc_offset = (lt->full_chunk_cnt * lt->spare_bytes) +
1127 		lt->last_spare_bytes;
1128 	int data_len = lt->data_bytes;
1129 	int spare_len = lt->spare_bytes;
1130 	int ecc_len = lt->ecc_bytes;
1131 	int chunk;
1132 
1133 	if (oob_required)
1134 		memset(chip->oob_poi, 0xFF, mtd->oobsize);
1135 
1136 	nand_read_page_op(chip, page, 0, NULL, 0);
1137 
1138 	for (chunk = 0; chunk < lt->nchunks; chunk++) {
1139 		/* Update last chunk length */
1140 		if (chunk >= lt->full_chunk_cnt) {
1141 			data_len = lt->last_data_bytes;
1142 			spare_len = lt->last_spare_bytes;
1143 			ecc_len = lt->last_ecc_bytes;
1144 		}
1145 
1146 		/* Read data bytes*/
1147 		nand_change_read_column_op(chip, chunk * chunk_size,
1148 					   buf + (lt->data_bytes * chunk),
1149 					   data_len, false);
1150 
1151 		/* Read spare bytes */
1152 		nand_read_data_op(chip, oob + (lt->spare_bytes * chunk),
1153 				  spare_len, false);
1154 
1155 		/* Read ECC bytes */
1156 		nand_read_data_op(chip, oob + ecc_offset +
1157 				  (ALIGN(lt->ecc_bytes, 32) * chunk),
1158 				  ecc_len, false);
1159 	}
1160 
1161 	return 0;
1162 }
1163 
marvell_nfc_hw_ecc_bch_read_chunk(struct nand_chip * chip,int chunk,u8 * data,unsigned int data_len,u8 * spare,unsigned int spare_len,int page)1164 static void marvell_nfc_hw_ecc_bch_read_chunk(struct nand_chip *chip, int chunk,
1165 					      u8 *data, unsigned int data_len,
1166 					      u8 *spare, unsigned int spare_len,
1167 					      int page)
1168 {
1169 	struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
1170 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1171 	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1172 	int i, ret;
1173 	struct marvell_nfc_op nfc_op = {
1174 		.ndcb[0] = NDCB0_CMD_TYPE(TYPE_READ) |
1175 			   NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
1176 			   NDCB0_LEN_OVRD,
1177 		.ndcb[1] = NDCB1_ADDRS_PAGE(page),
1178 		.ndcb[2] = NDCB2_ADDR5_PAGE(page),
1179 		.ndcb[3] = data_len + spare_len,
1180 	};
1181 
1182 	ret = marvell_nfc_prepare_cmd(chip);
1183 	if (ret)
1184 		return;
1185 
1186 	if (chunk == 0)
1187 		nfc_op.ndcb[0] |= NDCB0_DBC |
1188 				  NDCB0_CMD1(NAND_CMD_READ0) |
1189 				  NDCB0_CMD2(NAND_CMD_READSTART);
1190 
1191 	/*
1192 	 * Trigger the monolithic read on the first chunk, then naked read on
1193 	 * intermediate chunks and finally a last naked read on the last chunk.
1194 	 */
1195 	if (chunk == 0)
1196 		nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW);
1197 	else if (chunk < lt->nchunks - 1)
1198 		nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_NAKED_RW);
1199 	else
1200 		nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1201 
1202 	marvell_nfc_send_cmd(chip, &nfc_op);
1203 
1204 	/*
1205 	 * According to the datasheet, when reading from NDDB
1206 	 * with BCH enabled, after each 32 bytes reads, we
1207 	 * have to make sure that the NDSR.RDDREQ bit is set.
1208 	 *
1209 	 * Drain the FIFO, 8 32-bit reads at a time, and skip
1210 	 * the polling on the last read.
1211 	 *
1212 	 * Length is a multiple of 32 bytes, hence it is a multiple of 8 too.
1213 	 */
1214 	for (i = 0; i < data_len; i += FIFO_DEPTH * BCH_SEQ_READS) {
1215 		marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1216 				    "RDDREQ while draining FIFO (data)");
1217 		marvell_nfc_xfer_data_in_pio(nfc, data,
1218 					     FIFO_DEPTH * BCH_SEQ_READS);
1219 		data += FIFO_DEPTH * BCH_SEQ_READS;
1220 	}
1221 
1222 	for (i = 0; i < spare_len; i += FIFO_DEPTH * BCH_SEQ_READS) {
1223 		marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1224 				    "RDDREQ while draining FIFO (OOB)");
1225 		marvell_nfc_xfer_data_in_pio(nfc, spare,
1226 					     FIFO_DEPTH * BCH_SEQ_READS);
1227 		spare += FIFO_DEPTH * BCH_SEQ_READS;
1228 	}
1229 }
1230 
marvell_nfc_hw_ecc_bch_read_page(struct mtd_info * mtd,struct nand_chip * chip,u8 * buf,int oob_required,int page)1231 static int marvell_nfc_hw_ecc_bch_read_page(struct mtd_info *mtd,
1232 					    struct nand_chip *chip,
1233 					    u8 *buf, int oob_required,
1234 					    int page)
1235 {
1236 	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1237 	int data_len = lt->data_bytes, spare_len = lt->spare_bytes, ecc_len;
1238 	u8 *data = buf, *spare = chip->oob_poi, *ecc;
1239 	int max_bitflips = 0;
1240 	u32 failure_mask = 0;
1241 	int chunk, ecc_offset_in_page, ret;
1242 
1243 	/*
1244 	 * With BCH, OOB is not fully used (and thus not read entirely), not
1245 	 * expected bytes could show up at the end of the OOB buffer if not
1246 	 * explicitly erased.
1247 	 */
1248 	if (oob_required)
1249 		memset(chip->oob_poi, 0xFF, mtd->oobsize);
1250 
1251 	marvell_nfc_enable_hw_ecc(chip);
1252 
1253 	for (chunk = 0; chunk < lt->nchunks; chunk++) {
1254 		/* Update length for the last chunk */
1255 		if (chunk >= lt->full_chunk_cnt) {
1256 			data_len = lt->last_data_bytes;
1257 			spare_len = lt->last_spare_bytes;
1258 		}
1259 
1260 		/* Read the chunk and detect number of bitflips */
1261 		marvell_nfc_hw_ecc_bch_read_chunk(chip, chunk, data, data_len,
1262 						  spare, spare_len, page);
1263 		ret = marvell_nfc_hw_ecc_correct(chip, &max_bitflips);
1264 		if (ret)
1265 			failure_mask |= BIT(chunk);
1266 
1267 		data += data_len;
1268 		spare += spare_len;
1269 	}
1270 
1271 	marvell_nfc_disable_hw_ecc(chip);
1272 
1273 	if (!failure_mask)
1274 		return max_bitflips;
1275 
1276 	/*
1277 	 * Please note that dumping the ECC bytes during a normal read with OOB
1278 	 * area would add a significant overhead as ECC bytes are "consumed" by
1279 	 * the controller in normal mode and must be re-read in raw mode. To
1280 	 * avoid dropping the performances, we prefer not to include them. The
1281 	 * user should re-read the page in raw mode if ECC bytes are required.
1282 	 *
1283 	 * However, for any subpage read error reported by ->correct(), the ECC
1284 	 * bytes must be read in raw mode and the full subpage must be checked
1285 	 * to see if it is entirely empty of if there was an actual error.
1286 	 */
1287 	for (chunk = 0; chunk < lt->nchunks; chunk++) {
1288 		/* No failure reported for this chunk, move to the next one */
1289 		if (!(failure_mask & BIT(chunk)))
1290 			continue;
1291 
1292 		/* Derive ECC bytes positions (in page/buffer) and length */
1293 		ecc = chip->oob_poi +
1294 			(lt->full_chunk_cnt * lt->spare_bytes) +
1295 			lt->last_spare_bytes +
1296 			(chunk * ALIGN(lt->ecc_bytes, 32));
1297 		ecc_offset_in_page =
1298 			(chunk * (lt->data_bytes + lt->spare_bytes +
1299 				  lt->ecc_bytes)) +
1300 			(chunk < lt->full_chunk_cnt ?
1301 			 lt->data_bytes + lt->spare_bytes :
1302 			 lt->last_data_bytes + lt->last_spare_bytes);
1303 		ecc_len = chunk < lt->full_chunk_cnt ?
1304 			lt->ecc_bytes : lt->last_ecc_bytes;
1305 
1306 		/* Do the actual raw read of the ECC bytes */
1307 		nand_change_read_column_op(chip, ecc_offset_in_page,
1308 					   ecc, ecc_len, false);
1309 
1310 		/* Derive data/spare bytes positions (in buffer) and length */
1311 		data = buf + (chunk * lt->data_bytes);
1312 		data_len = chunk < lt->full_chunk_cnt ?
1313 			lt->data_bytes : lt->last_data_bytes;
1314 		spare = chip->oob_poi + (chunk * (lt->spare_bytes +
1315 						  lt->ecc_bytes));
1316 		spare_len = chunk < lt->full_chunk_cnt ?
1317 			lt->spare_bytes : lt->last_spare_bytes;
1318 
1319 		/* Check the entire chunk (data + spare + ecc) for emptyness */
1320 		marvell_nfc_check_empty_chunk(chip, data, data_len, spare,
1321 					      spare_len, ecc, ecc_len,
1322 					      &max_bitflips);
1323 	}
1324 
1325 	return max_bitflips;
1326 }
1327 
marvell_nfc_hw_ecc_bch_read_oob_raw(struct mtd_info * mtd,struct nand_chip * chip,int page)1328 static int marvell_nfc_hw_ecc_bch_read_oob_raw(struct mtd_info *mtd,
1329 					       struct nand_chip *chip, int page)
1330 {
1331 	/* Invalidate page cache */
1332 	chip->pagebuf = -1;
1333 
1334 	return chip->ecc.read_page_raw(mtd, chip, chip->data_buf, true, page);
1335 }
1336 
marvell_nfc_hw_ecc_bch_read_oob(struct mtd_info * mtd,struct nand_chip * chip,int page)1337 static int marvell_nfc_hw_ecc_bch_read_oob(struct mtd_info *mtd,
1338 					   struct nand_chip *chip, int page)
1339 {
1340 	/* Invalidate page cache */
1341 	chip->pagebuf = -1;
1342 
1343 	return chip->ecc.read_page(mtd, chip, chip->data_buf, true, page);
1344 }
1345 
1346 /* BCH write helpers */
marvell_nfc_hw_ecc_bch_write_page_raw(struct mtd_info * mtd,struct nand_chip * chip,const u8 * buf,int oob_required,int page)1347 static int marvell_nfc_hw_ecc_bch_write_page_raw(struct mtd_info *mtd,
1348 						 struct nand_chip *chip,
1349 						 const u8 *buf,
1350 						 int oob_required, int page)
1351 {
1352 	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1353 	int full_chunk_size = lt->data_bytes + lt->spare_bytes + lt->ecc_bytes;
1354 	int data_len = lt->data_bytes;
1355 	int spare_len = lt->spare_bytes;
1356 	int ecc_len = lt->ecc_bytes;
1357 	int spare_offset = 0;
1358 	int ecc_offset = (lt->full_chunk_cnt * lt->spare_bytes) +
1359 		lt->last_spare_bytes;
1360 	int chunk;
1361 
1362 	nand_prog_page_begin_op(chip, page, 0, NULL, 0);
1363 
1364 	for (chunk = 0; chunk < lt->nchunks; chunk++) {
1365 		if (chunk >= lt->full_chunk_cnt) {
1366 			data_len = lt->last_data_bytes;
1367 			spare_len = lt->last_spare_bytes;
1368 			ecc_len = lt->last_ecc_bytes;
1369 		}
1370 
1371 		/* Point to the column of the next chunk */
1372 		nand_change_write_column_op(chip, chunk * full_chunk_size,
1373 					    NULL, 0, false);
1374 
1375 		/* Write the data */
1376 		nand_write_data_op(chip, buf + (chunk * lt->data_bytes),
1377 				   data_len, false);
1378 
1379 		if (!oob_required)
1380 			continue;
1381 
1382 		/* Write the spare bytes */
1383 		if (spare_len)
1384 			nand_write_data_op(chip, chip->oob_poi + spare_offset,
1385 					   spare_len, false);
1386 
1387 		/* Write the ECC bytes */
1388 		if (ecc_len)
1389 			nand_write_data_op(chip, chip->oob_poi + ecc_offset,
1390 					   ecc_len, false);
1391 
1392 		spare_offset += spare_len;
1393 		ecc_offset += ALIGN(ecc_len, 32);
1394 	}
1395 
1396 	return nand_prog_page_end_op(chip);
1397 }
1398 
1399 static int
marvell_nfc_hw_ecc_bch_write_chunk(struct nand_chip * chip,int chunk,const u8 * data,unsigned int data_len,const u8 * spare,unsigned int spare_len,int page)1400 marvell_nfc_hw_ecc_bch_write_chunk(struct nand_chip *chip, int chunk,
1401 				   const u8 *data, unsigned int data_len,
1402 				   const u8 *spare, unsigned int spare_len,
1403 				   int page)
1404 {
1405 	struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
1406 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1407 	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1408 	u32 xtype;
1409 	int ret;
1410 	struct marvell_nfc_op nfc_op = {
1411 		.ndcb[0] = NDCB0_CMD_TYPE(TYPE_WRITE) | NDCB0_LEN_OVRD,
1412 		.ndcb[3] = data_len + spare_len,
1413 	};
1414 
1415 	/*
1416 	 * First operation dispatches the CMD_SEQIN command, issue the address
1417 	 * cycles and asks for the first chunk of data.
1418 	 * All operations in the middle (if any) will issue a naked write and
1419 	 * also ask for data.
1420 	 * Last operation (if any) asks for the last chunk of data through a
1421 	 * last naked write.
1422 	 */
1423 	if (chunk == 0) {
1424 		if (lt->nchunks == 1)
1425 			xtype = XTYPE_MONOLITHIC_RW;
1426 		else
1427 			xtype = XTYPE_WRITE_DISPATCH;
1428 
1429 		nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(xtype) |
1430 				  NDCB0_ADDR_CYC(marvell_nand->addr_cyc) |
1431 				  NDCB0_CMD1(NAND_CMD_SEQIN);
1432 		nfc_op.ndcb[1] |= NDCB1_ADDRS_PAGE(page);
1433 		nfc_op.ndcb[2] |= NDCB2_ADDR5_PAGE(page);
1434 	} else if (chunk < lt->nchunks - 1) {
1435 		nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_NAKED_RW);
1436 	} else {
1437 		nfc_op.ndcb[0] |= NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1438 	}
1439 
1440 	/* Always dispatch the PAGEPROG command on the last chunk */
1441 	if (chunk == lt->nchunks - 1)
1442 		nfc_op.ndcb[0] |= NDCB0_CMD2(NAND_CMD_PAGEPROG) | NDCB0_DBC;
1443 
1444 	ret = marvell_nfc_prepare_cmd(chip);
1445 	if (ret)
1446 		return ret;
1447 
1448 	marvell_nfc_send_cmd(chip, &nfc_op);
1449 	ret = marvell_nfc_end_cmd(chip, NDSR_WRDREQ,
1450 				  "WRDREQ while loading FIFO (data)");
1451 	if (ret)
1452 		return ret;
1453 
1454 	/* Transfer the contents */
1455 	iowrite32_rep(nfc->regs + NDDB, data, FIFO_REP(data_len));
1456 	iowrite32_rep(nfc->regs + NDDB, spare, FIFO_REP(spare_len));
1457 
1458 	return 0;
1459 }
1460 
marvell_nfc_hw_ecc_bch_write_page(struct mtd_info * mtd,struct nand_chip * chip,const u8 * buf,int oob_required,int page)1461 static int marvell_nfc_hw_ecc_bch_write_page(struct mtd_info *mtd,
1462 					     struct nand_chip *chip,
1463 					     const u8 *buf,
1464 					     int oob_required, int page)
1465 {
1466 	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
1467 	const u8 *data = buf;
1468 	const u8 *spare = chip->oob_poi;
1469 	int data_len = lt->data_bytes;
1470 	int spare_len = lt->spare_bytes;
1471 	int chunk, ret;
1472 
1473 	/* Spare data will be written anyway, so clear it to avoid garbage */
1474 	if (!oob_required)
1475 		memset(chip->oob_poi, 0xFF, mtd->oobsize);
1476 
1477 	marvell_nfc_enable_hw_ecc(chip);
1478 
1479 	for (chunk = 0; chunk < lt->nchunks; chunk++) {
1480 		if (chunk >= lt->full_chunk_cnt) {
1481 			data_len = lt->last_data_bytes;
1482 			spare_len = lt->last_spare_bytes;
1483 		}
1484 
1485 		marvell_nfc_hw_ecc_bch_write_chunk(chip, chunk, data, data_len,
1486 						   spare, spare_len, page);
1487 		data += data_len;
1488 		spare += spare_len;
1489 
1490 		/*
1491 		 * Waiting only for CMDD or PAGED is not enough, ECC are
1492 		 * partially written. No flag is set once the operation is
1493 		 * really finished but the ND_RUN bit is cleared, so wait for it
1494 		 * before stepping into the next command.
1495 		 */
1496 		marvell_nfc_wait_ndrun(chip);
1497 	}
1498 
1499 	ret = marvell_nfc_wait_op(chip,
1500 				  PSEC_TO_MSEC(chip->data_interface.timings.sdr.tPROG_max));
1501 
1502 	marvell_nfc_disable_hw_ecc(chip);
1503 
1504 	if (ret)
1505 		return ret;
1506 
1507 	return 0;
1508 }
1509 
marvell_nfc_hw_ecc_bch_write_oob_raw(struct mtd_info * mtd,struct nand_chip * chip,int page)1510 static int marvell_nfc_hw_ecc_bch_write_oob_raw(struct mtd_info *mtd,
1511 						struct nand_chip *chip,
1512 						int page)
1513 {
1514 	/* Invalidate page cache */
1515 	chip->pagebuf = -1;
1516 
1517 	memset(chip->data_buf, 0xFF, mtd->writesize);
1518 
1519 	return chip->ecc.write_page_raw(mtd, chip, chip->data_buf, true, page);
1520 }
1521 
marvell_nfc_hw_ecc_bch_write_oob(struct mtd_info * mtd,struct nand_chip * chip,int page)1522 static int marvell_nfc_hw_ecc_bch_write_oob(struct mtd_info *mtd,
1523 					    struct nand_chip *chip, int page)
1524 {
1525 	/* Invalidate page cache */
1526 	chip->pagebuf = -1;
1527 
1528 	memset(chip->data_buf, 0xFF, mtd->writesize);
1529 
1530 	return chip->ecc.write_page(mtd, chip, chip->data_buf, true, page);
1531 }
1532 
1533 /* NAND framework ->exec_op() hooks and related helpers */
marvell_nfc_parse_instructions(struct nand_chip * chip,const struct nand_subop * subop,struct marvell_nfc_op * nfc_op)1534 static void marvell_nfc_parse_instructions(struct nand_chip *chip,
1535 					   const struct nand_subop *subop,
1536 					   struct marvell_nfc_op *nfc_op)
1537 {
1538 	const struct nand_op_instr *instr = NULL;
1539 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1540 	bool first_cmd = true;
1541 	unsigned int op_id;
1542 	int i;
1543 
1544 	/* Reset the input structure as most of its fields will be OR'ed */
1545 	memset(nfc_op, 0, sizeof(struct marvell_nfc_op));
1546 
1547 	for (op_id = 0; op_id < subop->ninstrs; op_id++) {
1548 		unsigned int offset, naddrs;
1549 		const u8 *addrs;
1550 		int len;
1551 
1552 		instr = &subop->instrs[op_id];
1553 
1554 		switch (instr->type) {
1555 		case NAND_OP_CMD_INSTR:
1556 			if (first_cmd)
1557 				nfc_op->ndcb[0] |=
1558 					NDCB0_CMD1(instr->ctx.cmd.opcode);
1559 			else
1560 				nfc_op->ndcb[0] |=
1561 					NDCB0_CMD2(instr->ctx.cmd.opcode) |
1562 					NDCB0_DBC;
1563 
1564 			nfc_op->cle_ale_delay_ns = instr->delay_ns;
1565 			first_cmd = false;
1566 			break;
1567 
1568 		case NAND_OP_ADDR_INSTR:
1569 			offset = nand_subop_get_addr_start_off(subop, op_id);
1570 			naddrs = nand_subop_get_num_addr_cyc(subop, op_id);
1571 			addrs = &instr->ctx.addr.addrs[offset];
1572 
1573 			nfc_op->ndcb[0] |= NDCB0_ADDR_CYC(naddrs);
1574 
1575 			for (i = 0; i < min_t(unsigned int, 4, naddrs); i++)
1576 				nfc_op->ndcb[1] |= addrs[i] << (8 * i);
1577 
1578 			if (naddrs >= 5)
1579 				nfc_op->ndcb[2] |= NDCB2_ADDR5_CYC(addrs[4]);
1580 			if (naddrs >= 6)
1581 				nfc_op->ndcb[3] |= NDCB3_ADDR6_CYC(addrs[5]);
1582 			if (naddrs == 7)
1583 				nfc_op->ndcb[3] |= NDCB3_ADDR7_CYC(addrs[6]);
1584 
1585 			nfc_op->cle_ale_delay_ns = instr->delay_ns;
1586 			break;
1587 
1588 		case NAND_OP_DATA_IN_INSTR:
1589 			nfc_op->data_instr = instr;
1590 			nfc_op->data_instr_idx = op_id;
1591 			nfc_op->ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ);
1592 			if (nfc->caps->is_nfcv2) {
1593 				nfc_op->ndcb[0] |=
1594 					NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW) |
1595 					NDCB0_LEN_OVRD;
1596 				len = nand_subop_get_data_len(subop, op_id);
1597 				nfc_op->ndcb[3] |= round_up(len, FIFO_DEPTH);
1598 			}
1599 			nfc_op->data_delay_ns = instr->delay_ns;
1600 			break;
1601 
1602 		case NAND_OP_DATA_OUT_INSTR:
1603 			nfc_op->data_instr = instr;
1604 			nfc_op->data_instr_idx = op_id;
1605 			nfc_op->ndcb[0] |= NDCB0_CMD_TYPE(TYPE_WRITE);
1606 			if (nfc->caps->is_nfcv2) {
1607 				nfc_op->ndcb[0] |=
1608 					NDCB0_CMD_XTYPE(XTYPE_MONOLITHIC_RW) |
1609 					NDCB0_LEN_OVRD;
1610 				len = nand_subop_get_data_len(subop, op_id);
1611 				nfc_op->ndcb[3] |= round_up(len, FIFO_DEPTH);
1612 			}
1613 			nfc_op->data_delay_ns = instr->delay_ns;
1614 			break;
1615 
1616 		case NAND_OP_WAITRDY_INSTR:
1617 			nfc_op->rdy_timeout_ms = instr->ctx.waitrdy.timeout_ms;
1618 			nfc_op->rdy_delay_ns = instr->delay_ns;
1619 			break;
1620 		}
1621 	}
1622 }
1623 
marvell_nfc_xfer_data_pio(struct nand_chip * chip,const struct nand_subop * subop,struct marvell_nfc_op * nfc_op)1624 static int marvell_nfc_xfer_data_pio(struct nand_chip *chip,
1625 				     const struct nand_subop *subop,
1626 				     struct marvell_nfc_op *nfc_op)
1627 {
1628 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1629 	const struct nand_op_instr *instr = nfc_op->data_instr;
1630 	unsigned int op_id = nfc_op->data_instr_idx;
1631 	unsigned int len = nand_subop_get_data_len(subop, op_id);
1632 	unsigned int offset = nand_subop_get_data_start_off(subop, op_id);
1633 	bool reading = (instr->type == NAND_OP_DATA_IN_INSTR);
1634 	int ret;
1635 
1636 	if (instr->ctx.data.force_8bit)
1637 		marvell_nfc_force_byte_access(chip, true);
1638 
1639 	if (reading) {
1640 		u8 *in = instr->ctx.data.buf.in + offset;
1641 
1642 		ret = marvell_nfc_xfer_data_in_pio(nfc, in, len);
1643 	} else {
1644 		const u8 *out = instr->ctx.data.buf.out + offset;
1645 
1646 		ret = marvell_nfc_xfer_data_out_pio(nfc, out, len);
1647 	}
1648 
1649 	if (instr->ctx.data.force_8bit)
1650 		marvell_nfc_force_byte_access(chip, false);
1651 
1652 	return ret;
1653 }
1654 
marvell_nfc_monolithic_access_exec(struct nand_chip * chip,const struct nand_subop * subop)1655 static int marvell_nfc_monolithic_access_exec(struct nand_chip *chip,
1656 					      const struct nand_subop *subop)
1657 {
1658 	struct marvell_nfc_op nfc_op;
1659 	bool reading;
1660 	int ret;
1661 
1662 	marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1663 	reading = (nfc_op.data_instr->type == NAND_OP_DATA_IN_INSTR);
1664 
1665 	ret = marvell_nfc_prepare_cmd(chip);
1666 	if (ret)
1667 		return ret;
1668 
1669 	marvell_nfc_send_cmd(chip, &nfc_op);
1670 	ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ | NDSR_WRDREQ,
1671 				  "RDDREQ/WRDREQ while draining raw data");
1672 	if (ret)
1673 		return ret;
1674 
1675 	cond_delay(nfc_op.cle_ale_delay_ns);
1676 
1677 	if (reading) {
1678 		if (nfc_op.rdy_timeout_ms) {
1679 			ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1680 			if (ret)
1681 				return ret;
1682 		}
1683 
1684 		cond_delay(nfc_op.rdy_delay_ns);
1685 	}
1686 
1687 	marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1688 	ret = marvell_nfc_wait_cmdd(chip);
1689 	if (ret)
1690 		return ret;
1691 
1692 	cond_delay(nfc_op.data_delay_ns);
1693 
1694 	if (!reading) {
1695 		if (nfc_op.rdy_timeout_ms) {
1696 			ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1697 			if (ret)
1698 				return ret;
1699 		}
1700 
1701 		cond_delay(nfc_op.rdy_delay_ns);
1702 	}
1703 
1704 	/*
1705 	 * NDCR ND_RUN bit should be cleared automatically at the end of each
1706 	 * operation but experience shows that the behavior is buggy when it
1707 	 * comes to writes (with LEN_OVRD). Clear it by hand in this case.
1708 	 */
1709 	if (!reading) {
1710 		struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1711 
1712 		writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
1713 			       nfc->regs + NDCR);
1714 	}
1715 
1716 	return 0;
1717 }
1718 
marvell_nfc_naked_access_exec(struct nand_chip * chip,const struct nand_subop * subop)1719 static int marvell_nfc_naked_access_exec(struct nand_chip *chip,
1720 					 const struct nand_subop *subop)
1721 {
1722 	struct marvell_nfc_op nfc_op;
1723 	int ret;
1724 
1725 	marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1726 
1727 	/*
1728 	 * Naked access are different in that they need to be flagged as naked
1729 	 * by the controller. Reset the controller registers fields that inform
1730 	 * on the type and refill them according to the ongoing operation.
1731 	 */
1732 	nfc_op.ndcb[0] &= ~(NDCB0_CMD_TYPE(TYPE_MASK) |
1733 			    NDCB0_CMD_XTYPE(XTYPE_MASK));
1734 	switch (subop->instrs[0].type) {
1735 	case NAND_OP_CMD_INSTR:
1736 		nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_CMD);
1737 		break;
1738 	case NAND_OP_ADDR_INSTR:
1739 		nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_NAKED_ADDR);
1740 		break;
1741 	case NAND_OP_DATA_IN_INSTR:
1742 		nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ) |
1743 				  NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1744 		break;
1745 	case NAND_OP_DATA_OUT_INSTR:
1746 		nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_WRITE) |
1747 				  NDCB0_CMD_XTYPE(XTYPE_LAST_NAKED_RW);
1748 		break;
1749 	default:
1750 		/* This should never happen */
1751 		break;
1752 	}
1753 
1754 	ret = marvell_nfc_prepare_cmd(chip);
1755 	if (ret)
1756 		return ret;
1757 
1758 	marvell_nfc_send_cmd(chip, &nfc_op);
1759 
1760 	if (!nfc_op.data_instr) {
1761 		ret = marvell_nfc_wait_cmdd(chip);
1762 		cond_delay(nfc_op.cle_ale_delay_ns);
1763 		return ret;
1764 	}
1765 
1766 	ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ | NDSR_WRDREQ,
1767 				  "RDDREQ/WRDREQ while draining raw data");
1768 	if (ret)
1769 		return ret;
1770 
1771 	marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1772 	ret = marvell_nfc_wait_cmdd(chip);
1773 	if (ret)
1774 		return ret;
1775 
1776 	/*
1777 	 * NDCR ND_RUN bit should be cleared automatically at the end of each
1778 	 * operation but experience shows that the behavior is buggy when it
1779 	 * comes to writes (with LEN_OVRD). Clear it by hand in this case.
1780 	 */
1781 	if (subop->instrs[0].type == NAND_OP_DATA_OUT_INSTR) {
1782 		struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
1783 
1784 		writel_relaxed(readl(nfc->regs + NDCR) & ~NDCR_ND_RUN,
1785 			       nfc->regs + NDCR);
1786 	}
1787 
1788 	return 0;
1789 }
1790 
marvell_nfc_naked_waitrdy_exec(struct nand_chip * chip,const struct nand_subop * subop)1791 static int marvell_nfc_naked_waitrdy_exec(struct nand_chip *chip,
1792 					  const struct nand_subop *subop)
1793 {
1794 	struct marvell_nfc_op nfc_op;
1795 	int ret;
1796 
1797 	marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1798 
1799 	ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1800 	cond_delay(nfc_op.rdy_delay_ns);
1801 
1802 	return ret;
1803 }
1804 
marvell_nfc_read_id_type_exec(struct nand_chip * chip,const struct nand_subop * subop)1805 static int marvell_nfc_read_id_type_exec(struct nand_chip *chip,
1806 					 const struct nand_subop *subop)
1807 {
1808 	struct marvell_nfc_op nfc_op;
1809 	int ret;
1810 
1811 	marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1812 	nfc_op.ndcb[0] &= ~NDCB0_CMD_TYPE(TYPE_READ);
1813 	nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_READ_ID);
1814 
1815 	ret = marvell_nfc_prepare_cmd(chip);
1816 	if (ret)
1817 		return ret;
1818 
1819 	marvell_nfc_send_cmd(chip, &nfc_op);
1820 	ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1821 				  "RDDREQ while reading ID");
1822 	if (ret)
1823 		return ret;
1824 
1825 	cond_delay(nfc_op.cle_ale_delay_ns);
1826 
1827 	if (nfc_op.rdy_timeout_ms) {
1828 		ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1829 		if (ret)
1830 			return ret;
1831 	}
1832 
1833 	cond_delay(nfc_op.rdy_delay_ns);
1834 
1835 	marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1836 	ret = marvell_nfc_wait_cmdd(chip);
1837 	if (ret)
1838 		return ret;
1839 
1840 	cond_delay(nfc_op.data_delay_ns);
1841 
1842 	return 0;
1843 }
1844 
marvell_nfc_read_status_exec(struct nand_chip * chip,const struct nand_subop * subop)1845 static int marvell_nfc_read_status_exec(struct nand_chip *chip,
1846 					const struct nand_subop *subop)
1847 {
1848 	struct marvell_nfc_op nfc_op;
1849 	int ret;
1850 
1851 	marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1852 	nfc_op.ndcb[0] &= ~NDCB0_CMD_TYPE(TYPE_READ);
1853 	nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_STATUS);
1854 
1855 	ret = marvell_nfc_prepare_cmd(chip);
1856 	if (ret)
1857 		return ret;
1858 
1859 	marvell_nfc_send_cmd(chip, &nfc_op);
1860 	ret = marvell_nfc_end_cmd(chip, NDSR_RDDREQ,
1861 				  "RDDREQ while reading status");
1862 	if (ret)
1863 		return ret;
1864 
1865 	cond_delay(nfc_op.cle_ale_delay_ns);
1866 
1867 	if (nfc_op.rdy_timeout_ms) {
1868 		ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1869 		if (ret)
1870 			return ret;
1871 	}
1872 
1873 	cond_delay(nfc_op.rdy_delay_ns);
1874 
1875 	marvell_nfc_xfer_data_pio(chip, subop, &nfc_op);
1876 	ret = marvell_nfc_wait_cmdd(chip);
1877 	if (ret)
1878 		return ret;
1879 
1880 	cond_delay(nfc_op.data_delay_ns);
1881 
1882 	return 0;
1883 }
1884 
marvell_nfc_reset_cmd_type_exec(struct nand_chip * chip,const struct nand_subop * subop)1885 static int marvell_nfc_reset_cmd_type_exec(struct nand_chip *chip,
1886 					   const struct nand_subop *subop)
1887 {
1888 	struct marvell_nfc_op nfc_op;
1889 	int ret;
1890 
1891 	marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1892 	nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_RESET);
1893 
1894 	ret = marvell_nfc_prepare_cmd(chip);
1895 	if (ret)
1896 		return ret;
1897 
1898 	marvell_nfc_send_cmd(chip, &nfc_op);
1899 	ret = marvell_nfc_wait_cmdd(chip);
1900 	if (ret)
1901 		return ret;
1902 
1903 	cond_delay(nfc_op.cle_ale_delay_ns);
1904 
1905 	ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1906 	if (ret)
1907 		return ret;
1908 
1909 	cond_delay(nfc_op.rdy_delay_ns);
1910 
1911 	return 0;
1912 }
1913 
marvell_nfc_erase_cmd_type_exec(struct nand_chip * chip,const struct nand_subop * subop)1914 static int marvell_nfc_erase_cmd_type_exec(struct nand_chip *chip,
1915 					   const struct nand_subop *subop)
1916 {
1917 	struct marvell_nfc_op nfc_op;
1918 	int ret;
1919 
1920 	marvell_nfc_parse_instructions(chip, subop, &nfc_op);
1921 	nfc_op.ndcb[0] |= NDCB0_CMD_TYPE(TYPE_ERASE);
1922 
1923 	ret = marvell_nfc_prepare_cmd(chip);
1924 	if (ret)
1925 		return ret;
1926 
1927 	marvell_nfc_send_cmd(chip, &nfc_op);
1928 	ret = marvell_nfc_wait_cmdd(chip);
1929 	if (ret)
1930 		return ret;
1931 
1932 	cond_delay(nfc_op.cle_ale_delay_ns);
1933 
1934 	ret = marvell_nfc_wait_op(chip, nfc_op.rdy_timeout_ms);
1935 	if (ret)
1936 		return ret;
1937 
1938 	cond_delay(nfc_op.rdy_delay_ns);
1939 
1940 	return 0;
1941 }
1942 
1943 static const struct nand_op_parser marvell_nfcv2_op_parser = NAND_OP_PARSER(
1944 	/* Monolithic reads/writes */
1945 	NAND_OP_PARSER_PATTERN(
1946 		marvell_nfc_monolithic_access_exec,
1947 		NAND_OP_PARSER_PAT_CMD_ELEM(false),
1948 		NAND_OP_PARSER_PAT_ADDR_ELEM(true, MAX_ADDRESS_CYC_NFCV2),
1949 		NAND_OP_PARSER_PAT_CMD_ELEM(true),
1950 		NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
1951 		NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE)),
1952 	NAND_OP_PARSER_PATTERN(
1953 		marvell_nfc_monolithic_access_exec,
1954 		NAND_OP_PARSER_PAT_CMD_ELEM(false),
1955 		NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2),
1956 		NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE),
1957 		NAND_OP_PARSER_PAT_CMD_ELEM(true),
1958 		NAND_OP_PARSER_PAT_WAITRDY_ELEM(true)),
1959 	/* Naked commands */
1960 	NAND_OP_PARSER_PATTERN(
1961 		marvell_nfc_naked_access_exec,
1962 		NAND_OP_PARSER_PAT_CMD_ELEM(false)),
1963 	NAND_OP_PARSER_PATTERN(
1964 		marvell_nfc_naked_access_exec,
1965 		NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV2)),
1966 	NAND_OP_PARSER_PATTERN(
1967 		marvell_nfc_naked_access_exec,
1968 		NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, MAX_CHUNK_SIZE)),
1969 	NAND_OP_PARSER_PATTERN(
1970 		marvell_nfc_naked_access_exec,
1971 		NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_CHUNK_SIZE)),
1972 	NAND_OP_PARSER_PATTERN(
1973 		marvell_nfc_naked_waitrdy_exec,
1974 		NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
1975 	);
1976 
1977 static const struct nand_op_parser marvell_nfcv1_op_parser = NAND_OP_PARSER(
1978 	/* Naked commands not supported, use a function for each pattern */
1979 	NAND_OP_PARSER_PATTERN(
1980 		marvell_nfc_read_id_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_DATA_IN_ELEM(false, 8)),
1984 	NAND_OP_PARSER_PATTERN(
1985 		marvell_nfc_erase_cmd_type_exec,
1986 		NAND_OP_PARSER_PAT_CMD_ELEM(false),
1987 		NAND_OP_PARSER_PAT_ADDR_ELEM(false, MAX_ADDRESS_CYC_NFCV1),
1988 		NAND_OP_PARSER_PAT_CMD_ELEM(false),
1989 		NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
1990 	NAND_OP_PARSER_PATTERN(
1991 		marvell_nfc_read_status_exec,
1992 		NAND_OP_PARSER_PAT_CMD_ELEM(false),
1993 		NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 1)),
1994 	NAND_OP_PARSER_PATTERN(
1995 		marvell_nfc_reset_cmd_type_exec,
1996 		NAND_OP_PARSER_PAT_CMD_ELEM(false),
1997 		NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
1998 	NAND_OP_PARSER_PATTERN(
1999 		marvell_nfc_naked_waitrdy_exec,
2000 		NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
2001 	);
2002 
marvell_nfc_exec_op(struct nand_chip * chip,const struct nand_operation * op,bool check_only)2003 static int marvell_nfc_exec_op(struct nand_chip *chip,
2004 			       const struct nand_operation *op,
2005 			       bool check_only)
2006 {
2007 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2008 
2009 	if (nfc->caps->is_nfcv2)
2010 		return nand_op_parser_exec_op(chip, &marvell_nfcv2_op_parser,
2011 					      op, check_only);
2012 	else
2013 		return nand_op_parser_exec_op(chip, &marvell_nfcv1_op_parser,
2014 					      op, check_only);
2015 }
2016 
2017 /*
2018  * Layouts were broken in old pxa3xx_nand driver, these are supposed to be
2019  * usable.
2020  */
marvell_nand_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)2021 static int marvell_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
2022 				      struct mtd_oob_region *oobregion)
2023 {
2024 	struct nand_chip *chip = mtd_to_nand(mtd);
2025 	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
2026 
2027 	if (section)
2028 		return -ERANGE;
2029 
2030 	oobregion->length = (lt->full_chunk_cnt * lt->ecc_bytes) +
2031 			    lt->last_ecc_bytes;
2032 	oobregion->offset = mtd->oobsize - oobregion->length;
2033 
2034 	return 0;
2035 }
2036 
marvell_nand_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)2037 static int marvell_nand_ooblayout_free(struct mtd_info *mtd, int section,
2038 				       struct mtd_oob_region *oobregion)
2039 {
2040 	struct nand_chip *chip = mtd_to_nand(mtd);
2041 	const struct marvell_hw_ecc_layout *lt = to_marvell_nand(chip)->layout;
2042 
2043 	if (section)
2044 		return -ERANGE;
2045 
2046 	/*
2047 	 * Bootrom looks in bytes 0 & 5 for bad blocks for the
2048 	 * 4KB page / 4bit BCH combination.
2049 	 */
2050 	if (mtd->writesize == SZ_4K && lt->data_bytes == SZ_2K)
2051 		oobregion->offset = 6;
2052 	else
2053 		oobregion->offset = 2;
2054 
2055 	oobregion->length = (lt->full_chunk_cnt * lt->spare_bytes) +
2056 			    lt->last_spare_bytes - oobregion->offset;
2057 
2058 	return 0;
2059 }
2060 
2061 static const struct mtd_ooblayout_ops marvell_nand_ooblayout_ops = {
2062 	.ecc = marvell_nand_ooblayout_ecc,
2063 	.free = marvell_nand_ooblayout_free,
2064 };
2065 
marvell_nand_hw_ecc_ctrl_init(struct mtd_info * mtd,struct nand_ecc_ctrl * ecc)2066 static int marvell_nand_hw_ecc_ctrl_init(struct mtd_info *mtd,
2067 					 struct nand_ecc_ctrl *ecc)
2068 {
2069 	struct nand_chip *chip = mtd_to_nand(mtd);
2070 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2071 	const struct marvell_hw_ecc_layout *l;
2072 	int i;
2073 
2074 	if (!nfc->caps->is_nfcv2 &&
2075 	    (mtd->writesize + mtd->oobsize > MAX_CHUNK_SIZE)) {
2076 		dev_err(nfc->dev,
2077 			"NFCv1: writesize (%d) cannot be bigger than a chunk (%d)\n",
2078 			mtd->writesize, MAX_CHUNK_SIZE - mtd->oobsize);
2079 		return -ENOTSUPP;
2080 	}
2081 
2082 	to_marvell_nand(chip)->layout = NULL;
2083 	for (i = 0; i < ARRAY_SIZE(marvell_nfc_layouts); i++) {
2084 		l = &marvell_nfc_layouts[i];
2085 		if (mtd->writesize == l->writesize &&
2086 		    ecc->size == l->chunk && ecc->strength == l->strength) {
2087 			to_marvell_nand(chip)->layout = l;
2088 			break;
2089 		}
2090 	}
2091 
2092 	if (!to_marvell_nand(chip)->layout ||
2093 	    (!nfc->caps->is_nfcv2 && ecc->strength > 1)) {
2094 		dev_err(nfc->dev,
2095 			"ECC strength %d at page size %d is not supported\n",
2096 			ecc->strength, mtd->writesize);
2097 		return -ENOTSUPP;
2098 	}
2099 
2100 	mtd_set_ooblayout(mtd, &marvell_nand_ooblayout_ops);
2101 	ecc->steps = l->nchunks;
2102 	ecc->size = l->data_bytes;
2103 
2104 	if (ecc->strength == 1) {
2105 		chip->ecc.algo = NAND_ECC_HAMMING;
2106 		ecc->read_page_raw = marvell_nfc_hw_ecc_hmg_read_page_raw;
2107 		ecc->read_page = marvell_nfc_hw_ecc_hmg_read_page;
2108 		ecc->read_oob_raw = marvell_nfc_hw_ecc_hmg_read_oob_raw;
2109 		ecc->read_oob = ecc->read_oob_raw;
2110 		ecc->write_page_raw = marvell_nfc_hw_ecc_hmg_write_page_raw;
2111 		ecc->write_page = marvell_nfc_hw_ecc_hmg_write_page;
2112 		ecc->write_oob_raw = marvell_nfc_hw_ecc_hmg_write_oob_raw;
2113 		ecc->write_oob = ecc->write_oob_raw;
2114 	} else {
2115 		chip->ecc.algo = NAND_ECC_BCH;
2116 		ecc->strength = 16;
2117 		ecc->read_page_raw = marvell_nfc_hw_ecc_bch_read_page_raw;
2118 		ecc->read_page = marvell_nfc_hw_ecc_bch_read_page;
2119 		ecc->read_oob_raw = marvell_nfc_hw_ecc_bch_read_oob_raw;
2120 		ecc->read_oob = marvell_nfc_hw_ecc_bch_read_oob;
2121 		ecc->write_page_raw = marvell_nfc_hw_ecc_bch_write_page_raw;
2122 		ecc->write_page = marvell_nfc_hw_ecc_bch_write_page;
2123 		ecc->write_oob_raw = marvell_nfc_hw_ecc_bch_write_oob_raw;
2124 		ecc->write_oob = marvell_nfc_hw_ecc_bch_write_oob;
2125 	}
2126 
2127 	return 0;
2128 }
2129 
marvell_nand_ecc_init(struct mtd_info * mtd,struct nand_ecc_ctrl * ecc)2130 static int marvell_nand_ecc_init(struct mtd_info *mtd,
2131 				 struct nand_ecc_ctrl *ecc)
2132 {
2133 	struct nand_chip *chip = mtd_to_nand(mtd);
2134 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2135 	int ret;
2136 
2137 	if (ecc->mode != NAND_ECC_NONE && (!ecc->size || !ecc->strength)) {
2138 		if (chip->ecc_step_ds && chip->ecc_strength_ds) {
2139 			ecc->size = chip->ecc_step_ds;
2140 			ecc->strength = chip->ecc_strength_ds;
2141 		} else {
2142 			dev_info(nfc->dev,
2143 				 "No minimum ECC strength, using 1b/512B\n");
2144 			ecc->size = 512;
2145 			ecc->strength = 1;
2146 		}
2147 	}
2148 
2149 	switch (ecc->mode) {
2150 	case NAND_ECC_HW:
2151 		ret = marvell_nand_hw_ecc_ctrl_init(mtd, ecc);
2152 		if (ret)
2153 			return ret;
2154 		break;
2155 	case NAND_ECC_NONE:
2156 	case NAND_ECC_SOFT:
2157 	case NAND_ECC_ON_DIE:
2158 		if (!nfc->caps->is_nfcv2 && mtd->writesize != SZ_512 &&
2159 		    mtd->writesize != SZ_2K) {
2160 			dev_err(nfc->dev, "NFCv1 cannot write %d bytes pages\n",
2161 				mtd->writesize);
2162 			return -EINVAL;
2163 		}
2164 		break;
2165 	default:
2166 		return -EINVAL;
2167 	}
2168 
2169 	return 0;
2170 }
2171 
2172 static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
2173 static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
2174 
2175 static struct nand_bbt_descr bbt_main_descr = {
2176 	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
2177 		   NAND_BBT_2BIT | NAND_BBT_VERSION,
2178 	.offs =	8,
2179 	.len = 6,
2180 	.veroffs = 14,
2181 	.maxblocks = 8,	/* Last 8 blocks in each chip */
2182 	.pattern = bbt_pattern
2183 };
2184 
2185 static struct nand_bbt_descr bbt_mirror_descr = {
2186 	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
2187 		   NAND_BBT_2BIT | NAND_BBT_VERSION,
2188 	.offs =	8,
2189 	.len = 6,
2190 	.veroffs = 14,
2191 	.maxblocks = 8,	/* Last 8 blocks in each chip */
2192 	.pattern = bbt_mirror_pattern
2193 };
2194 
marvell_nfc_setup_data_interface(struct mtd_info * mtd,int chipnr,const struct nand_data_interface * conf)2195 static int marvell_nfc_setup_data_interface(struct mtd_info *mtd, int chipnr,
2196 					    const struct nand_data_interface
2197 					    *conf)
2198 {
2199 	struct nand_chip *chip = mtd_to_nand(mtd);
2200 	struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
2201 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2202 	unsigned int period_ns = 1000000000 / clk_get_rate(nfc->core_clk) * 2;
2203 	const struct nand_sdr_timings *sdr;
2204 	struct marvell_nfc_timings nfc_tmg;
2205 	int read_delay;
2206 
2207 	sdr = nand_get_sdr_timings(conf);
2208 	if (IS_ERR(sdr))
2209 		return PTR_ERR(sdr);
2210 
2211 	/*
2212 	 * SDR timings are given in pico-seconds while NFC timings must be
2213 	 * expressed in NAND controller clock cycles, which is half of the
2214 	 * frequency of the accessible ECC clock retrieved by clk_get_rate().
2215 	 * This is not written anywhere in the datasheet but was observed
2216 	 * with an oscilloscope.
2217 	 *
2218 	 * NFC datasheet gives equations from which thoses calculations
2219 	 * are derived, they tend to be slightly more restrictives than the
2220 	 * given core timings and may improve the overall speed.
2221 	 */
2222 	nfc_tmg.tRP = TO_CYCLES(DIV_ROUND_UP(sdr->tRC_min, 2), period_ns) - 1;
2223 	nfc_tmg.tRH = nfc_tmg.tRP;
2224 	nfc_tmg.tWP = TO_CYCLES(DIV_ROUND_UP(sdr->tWC_min, 2), period_ns) - 1;
2225 	nfc_tmg.tWH = nfc_tmg.tWP;
2226 	nfc_tmg.tCS = TO_CYCLES(sdr->tCS_min, period_ns);
2227 	nfc_tmg.tCH = TO_CYCLES(sdr->tCH_min, period_ns) - 1;
2228 	nfc_tmg.tADL = TO_CYCLES(sdr->tADL_min, period_ns);
2229 	/*
2230 	 * Read delay is the time of propagation from SoC pins to NFC internal
2231 	 * logic. With non-EDO timings, this is MIN_RD_DEL_CNT clock cycles. In
2232 	 * EDO mode, an additional delay of tRH must be taken into account so
2233 	 * the data is sampled on the falling edge instead of the rising edge.
2234 	 */
2235 	read_delay = sdr->tRC_min >= 30000 ?
2236 		MIN_RD_DEL_CNT : MIN_RD_DEL_CNT + nfc_tmg.tRH;
2237 
2238 	nfc_tmg.tAR = TO_CYCLES(sdr->tAR_min, period_ns);
2239 	/*
2240 	 * tWHR and tRHW are supposed to be read to write delays (and vice
2241 	 * versa) but in some cases, ie. when doing a change column, they must
2242 	 * be greater than that to be sure tCCS delay is respected.
2243 	 */
2244 	nfc_tmg.tWHR = TO_CYCLES(max_t(int, sdr->tWHR_min, sdr->tCCS_min),
2245 				 period_ns) - 2,
2246 	nfc_tmg.tRHW = TO_CYCLES(max_t(int, sdr->tRHW_min, sdr->tCCS_min),
2247 				 period_ns);
2248 
2249 	/*
2250 	 * NFCv2: Use WAIT_MODE (wait for RB line), do not rely only on delays.
2251 	 * NFCv1: No WAIT_MODE, tR must be maximal.
2252 	 */
2253 	if (nfc->caps->is_nfcv2) {
2254 		nfc_tmg.tR = TO_CYCLES(sdr->tWB_max, period_ns);
2255 	} else {
2256 		nfc_tmg.tR = TO_CYCLES64(sdr->tWB_max + sdr->tR_max,
2257 					 period_ns);
2258 		if (nfc_tmg.tR + 3 > nfc_tmg.tCH)
2259 			nfc_tmg.tR = nfc_tmg.tCH - 3;
2260 		else
2261 			nfc_tmg.tR = 0;
2262 	}
2263 
2264 	if (chipnr < 0)
2265 		return 0;
2266 
2267 	marvell_nand->ndtr0 =
2268 		NDTR0_TRP(nfc_tmg.tRP) |
2269 		NDTR0_TRH(nfc_tmg.tRH) |
2270 		NDTR0_ETRP(nfc_tmg.tRP) |
2271 		NDTR0_TWP(nfc_tmg.tWP) |
2272 		NDTR0_TWH(nfc_tmg.tWH) |
2273 		NDTR0_TCS(nfc_tmg.tCS) |
2274 		NDTR0_TCH(nfc_tmg.tCH);
2275 
2276 	marvell_nand->ndtr1 =
2277 		NDTR1_TAR(nfc_tmg.tAR) |
2278 		NDTR1_TWHR(nfc_tmg.tWHR) |
2279 		NDTR1_TR(nfc_tmg.tR);
2280 
2281 	if (nfc->caps->is_nfcv2) {
2282 		marvell_nand->ndtr0 |=
2283 			NDTR0_RD_CNT_DEL(read_delay) |
2284 			NDTR0_SELCNTR |
2285 			NDTR0_TADL(nfc_tmg.tADL);
2286 
2287 		marvell_nand->ndtr1 |=
2288 			NDTR1_TRHW(nfc_tmg.tRHW) |
2289 			NDTR1_WAIT_MODE;
2290 	}
2291 
2292 	return 0;
2293 }
2294 
marvell_nand_attach_chip(struct nand_chip * chip)2295 static int marvell_nand_attach_chip(struct nand_chip *chip)
2296 {
2297 	struct mtd_info *mtd = nand_to_mtd(chip);
2298 	struct marvell_nand_chip *marvell_nand = to_marvell_nand(chip);
2299 	struct marvell_nfc *nfc = to_marvell_nfc(chip->controller);
2300 	struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(nfc->dev);
2301 	int ret;
2302 
2303 	if (pdata && pdata->flash_bbt)
2304 		chip->bbt_options |= NAND_BBT_USE_FLASH;
2305 
2306 	if (chip->bbt_options & NAND_BBT_USE_FLASH) {
2307 		/*
2308 		 * We'll use a bad block table stored in-flash and don't
2309 		 * allow writing the bad block marker to the flash.
2310 		 */
2311 		chip->bbt_options |= NAND_BBT_NO_OOB_BBM;
2312 		chip->bbt_td = &bbt_main_descr;
2313 		chip->bbt_md = &bbt_mirror_descr;
2314 	}
2315 
2316 	/* Save the chip-specific fields of NDCR */
2317 	marvell_nand->ndcr = NDCR_PAGE_SZ(mtd->writesize);
2318 	if (chip->options & NAND_BUSWIDTH_16)
2319 		marvell_nand->ndcr |= NDCR_DWIDTH_M | NDCR_DWIDTH_C;
2320 
2321 	/*
2322 	 * On small page NANDs, only one cycle is needed to pass the
2323 	 * column address.
2324 	 */
2325 	if (mtd->writesize <= 512) {
2326 		marvell_nand->addr_cyc = 1;
2327 	} else {
2328 		marvell_nand->addr_cyc = 2;
2329 		marvell_nand->ndcr |= NDCR_RA_START;
2330 	}
2331 
2332 	/*
2333 	 * Now add the number of cycles needed to pass the row
2334 	 * address.
2335 	 *
2336 	 * Addressing a chip using CS 2 or 3 should also need the third row
2337 	 * cycle but due to inconsistance in the documentation and lack of
2338 	 * hardware to test this situation, this case is not supported.
2339 	 */
2340 	if (chip->options & NAND_ROW_ADDR_3)
2341 		marvell_nand->addr_cyc += 3;
2342 	else
2343 		marvell_nand->addr_cyc += 2;
2344 
2345 	if (pdata) {
2346 		chip->ecc.size = pdata->ecc_step_size;
2347 		chip->ecc.strength = pdata->ecc_strength;
2348 	}
2349 
2350 	ret = marvell_nand_ecc_init(mtd, &chip->ecc);
2351 	if (ret) {
2352 		dev_err(nfc->dev, "ECC init failed: %d\n", ret);
2353 		return ret;
2354 	}
2355 
2356 	if (chip->ecc.mode == NAND_ECC_HW) {
2357 		/*
2358 		 * Subpage write not available with hardware ECC, prohibit also
2359 		 * subpage read as in userspace subpage access would still be
2360 		 * allowed and subpage write, if used, would lead to numerous
2361 		 * uncorrectable ECC errors.
2362 		 */
2363 		chip->options |= NAND_NO_SUBPAGE_WRITE;
2364 	}
2365 
2366 	if (pdata || nfc->caps->legacy_of_bindings) {
2367 		/*
2368 		 * We keep the MTD name unchanged to avoid breaking platforms
2369 		 * where the MTD cmdline parser is used and the bootloader
2370 		 * has not been updated to use the new naming scheme.
2371 		 */
2372 		mtd->name = "pxa3xx_nand-0";
2373 	} else if (!mtd->name) {
2374 		/*
2375 		 * If the new bindings are used and the bootloader has not been
2376 		 * updated to pass a new mtdparts parameter on the cmdline, you
2377 		 * should define the following property in your NAND node, ie:
2378 		 *
2379 		 *	label = "main-storage";
2380 		 *
2381 		 * This way, mtd->name will be set by the core when
2382 		 * nand_set_flash_node() is called.
2383 		 */
2384 		mtd->name = devm_kasprintf(nfc->dev, GFP_KERNEL,
2385 					   "%s:nand.%d", dev_name(nfc->dev),
2386 					   marvell_nand->sels[0].cs);
2387 		if (!mtd->name) {
2388 			dev_err(nfc->dev, "Failed to allocate mtd->name\n");
2389 			return -ENOMEM;
2390 		}
2391 	}
2392 
2393 	return 0;
2394 }
2395 
2396 static const struct nand_controller_ops marvell_nand_controller_ops = {
2397 	.attach_chip = marvell_nand_attach_chip,
2398 };
2399 
marvell_nand_chip_init(struct device * dev,struct marvell_nfc * nfc,struct device_node * np)2400 static int marvell_nand_chip_init(struct device *dev, struct marvell_nfc *nfc,
2401 				  struct device_node *np)
2402 {
2403 	struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(dev);
2404 	struct marvell_nand_chip *marvell_nand;
2405 	struct mtd_info *mtd;
2406 	struct nand_chip *chip;
2407 	int nsels, ret, i;
2408 	u32 cs, rb;
2409 
2410 	/*
2411 	 * The legacy "num-cs" property indicates the number of CS on the only
2412 	 * chip connected to the controller (legacy bindings does not support
2413 	 * more than one chip). The CS and RB pins are always the #0.
2414 	 *
2415 	 * When not using legacy bindings, a couple of "reg" and "nand-rb"
2416 	 * properties must be filled. For each chip, expressed as a subnode,
2417 	 * "reg" points to the CS lines and "nand-rb" to the RB line.
2418 	 */
2419 	if (pdata || nfc->caps->legacy_of_bindings) {
2420 		nsels = 1;
2421 	} else {
2422 		nsels = of_property_count_elems_of_size(np, "reg", sizeof(u32));
2423 		if (nsels <= 0) {
2424 			dev_err(dev, "missing/invalid reg property\n");
2425 			return -EINVAL;
2426 		}
2427 	}
2428 
2429 	/* Alloc the nand chip structure */
2430 	marvell_nand = devm_kzalloc(dev, sizeof(*marvell_nand) +
2431 				    (nsels *
2432 				     sizeof(struct marvell_nand_chip_sel)),
2433 				    GFP_KERNEL);
2434 	if (!marvell_nand) {
2435 		dev_err(dev, "could not allocate chip structure\n");
2436 		return -ENOMEM;
2437 	}
2438 
2439 	marvell_nand->nsels = nsels;
2440 	marvell_nand->selected_die = -1;
2441 
2442 	for (i = 0; i < nsels; i++) {
2443 		if (pdata || nfc->caps->legacy_of_bindings) {
2444 			/*
2445 			 * Legacy bindings use the CS lines in natural
2446 			 * order (0, 1, ...)
2447 			 */
2448 			cs = i;
2449 		} else {
2450 			/* Retrieve CS id */
2451 			ret = of_property_read_u32_index(np, "reg", i, &cs);
2452 			if (ret) {
2453 				dev_err(dev, "could not retrieve reg property: %d\n",
2454 					ret);
2455 				return ret;
2456 			}
2457 		}
2458 
2459 		if (cs >= nfc->caps->max_cs_nb) {
2460 			dev_err(dev, "invalid reg value: %u (max CS = %d)\n",
2461 				cs, nfc->caps->max_cs_nb);
2462 			return -EINVAL;
2463 		}
2464 
2465 		if (test_and_set_bit(cs, &nfc->assigned_cs)) {
2466 			dev_err(dev, "CS %d already assigned\n", cs);
2467 			return -EINVAL;
2468 		}
2469 
2470 		/*
2471 		 * The cs variable represents the chip select id, which must be
2472 		 * converted in bit fields for NDCB0 and NDCB2 to select the
2473 		 * right chip. Unfortunately, due to a lack of information on
2474 		 * the subject and incoherent documentation, the user should not
2475 		 * use CS1 and CS3 at all as asserting them is not supported in
2476 		 * a reliable way (due to multiplexing inside ADDR5 field).
2477 		 */
2478 		marvell_nand->sels[i].cs = cs;
2479 		switch (cs) {
2480 		case 0:
2481 		case 2:
2482 			marvell_nand->sels[i].ndcb0_csel = 0;
2483 			break;
2484 		case 1:
2485 		case 3:
2486 			marvell_nand->sels[i].ndcb0_csel = NDCB0_CSEL;
2487 			break;
2488 		default:
2489 			return -EINVAL;
2490 		}
2491 
2492 		/* Retrieve RB id */
2493 		if (pdata || nfc->caps->legacy_of_bindings) {
2494 			/* Legacy bindings always use RB #0 */
2495 			rb = 0;
2496 		} else {
2497 			ret = of_property_read_u32_index(np, "nand-rb", i,
2498 							 &rb);
2499 			if (ret) {
2500 				dev_err(dev,
2501 					"could not retrieve RB property: %d\n",
2502 					ret);
2503 				return ret;
2504 			}
2505 		}
2506 
2507 		if (rb >= nfc->caps->max_rb_nb) {
2508 			dev_err(dev, "invalid reg value: %u (max RB = %d)\n",
2509 				rb, nfc->caps->max_rb_nb);
2510 			return -EINVAL;
2511 		}
2512 
2513 		marvell_nand->sels[i].rb = rb;
2514 	}
2515 
2516 	chip = &marvell_nand->chip;
2517 	chip->controller = &nfc->controller;
2518 	nand_set_flash_node(chip, np);
2519 
2520 	chip->exec_op = marvell_nfc_exec_op;
2521 	chip->select_chip = marvell_nfc_select_chip;
2522 	if (!of_property_read_bool(np, "marvell,nand-keep-config"))
2523 		chip->setup_data_interface = marvell_nfc_setup_data_interface;
2524 
2525 	mtd = nand_to_mtd(chip);
2526 	mtd->dev.parent = dev;
2527 
2528 	/*
2529 	 * Default to HW ECC engine mode. If the nand-ecc-mode property is given
2530 	 * in the DT node, this entry will be overwritten in nand_scan_ident().
2531 	 */
2532 	chip->ecc.mode = NAND_ECC_HW;
2533 
2534 	/*
2535 	 * Save a reference value for timing registers before
2536 	 * ->setup_data_interface() is called.
2537 	 */
2538 	marvell_nand->ndtr0 = readl_relaxed(nfc->regs + NDTR0);
2539 	marvell_nand->ndtr1 = readl_relaxed(nfc->regs + NDTR1);
2540 
2541 	chip->options |= NAND_BUSWIDTH_AUTO;
2542 
2543 	ret = nand_scan(mtd, marvell_nand->nsels);
2544 	if (ret) {
2545 		dev_err(dev, "could not scan the nand chip\n");
2546 		return ret;
2547 	}
2548 
2549 	if (pdata)
2550 		/* Legacy bindings support only one chip */
2551 		ret = mtd_device_register(mtd, pdata->parts, pdata->nr_parts);
2552 	else
2553 		ret = mtd_device_register(mtd, NULL, 0);
2554 	if (ret) {
2555 		dev_err(dev, "failed to register mtd device: %d\n", ret);
2556 		nand_release(mtd);
2557 		return ret;
2558 	}
2559 
2560 	list_add_tail(&marvell_nand->node, &nfc->chips);
2561 
2562 	return 0;
2563 }
2564 
marvell_nand_chips_init(struct device * dev,struct marvell_nfc * nfc)2565 static int marvell_nand_chips_init(struct device *dev, struct marvell_nfc *nfc)
2566 {
2567 	struct device_node *np = dev->of_node;
2568 	struct device_node *nand_np;
2569 	int max_cs = nfc->caps->max_cs_nb;
2570 	int nchips;
2571 	int ret;
2572 
2573 	if (!np)
2574 		nchips = 1;
2575 	else
2576 		nchips = of_get_child_count(np);
2577 
2578 	if (nchips > max_cs) {
2579 		dev_err(dev, "too many NAND chips: %d (max = %d CS)\n", nchips,
2580 			max_cs);
2581 		return -EINVAL;
2582 	}
2583 
2584 	/*
2585 	 * Legacy bindings do not use child nodes to exhibit NAND chip
2586 	 * properties and layout. Instead, NAND properties are mixed with the
2587 	 * controller ones, and partitions are defined as direct subnodes of the
2588 	 * NAND controller node.
2589 	 */
2590 	if (nfc->caps->legacy_of_bindings) {
2591 		ret = marvell_nand_chip_init(dev, nfc, np);
2592 		return ret;
2593 	}
2594 
2595 	for_each_child_of_node(np, nand_np) {
2596 		ret = marvell_nand_chip_init(dev, nfc, nand_np);
2597 		if (ret) {
2598 			of_node_put(nand_np);
2599 			return ret;
2600 		}
2601 	}
2602 
2603 	return 0;
2604 }
2605 
marvell_nand_chips_cleanup(struct marvell_nfc * nfc)2606 static void marvell_nand_chips_cleanup(struct marvell_nfc *nfc)
2607 {
2608 	struct marvell_nand_chip *entry, *temp;
2609 
2610 	list_for_each_entry_safe(entry, temp, &nfc->chips, node) {
2611 		nand_release(nand_to_mtd(&entry->chip));
2612 		list_del(&entry->node);
2613 	}
2614 }
2615 
marvell_nfc_init_dma(struct marvell_nfc * nfc)2616 static int marvell_nfc_init_dma(struct marvell_nfc *nfc)
2617 {
2618 	struct platform_device *pdev = container_of(nfc->dev,
2619 						    struct platform_device,
2620 						    dev);
2621 	struct dma_slave_config config = {};
2622 	struct resource *r;
2623 	int ret;
2624 
2625 	if (!IS_ENABLED(CONFIG_PXA_DMA)) {
2626 		dev_warn(nfc->dev,
2627 			 "DMA not enabled in configuration\n");
2628 		return -ENOTSUPP;
2629 	}
2630 
2631 	ret = dma_set_mask_and_coherent(nfc->dev, DMA_BIT_MASK(32));
2632 	if (ret)
2633 		return ret;
2634 
2635 	nfc->dma_chan =	dma_request_slave_channel(nfc->dev, "data");
2636 	if (!nfc->dma_chan) {
2637 		dev_err(nfc->dev,
2638 			"Unable to request data DMA channel\n");
2639 		return -ENODEV;
2640 	}
2641 
2642 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2643 	if (!r)
2644 		return -ENXIO;
2645 
2646 	config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2647 	config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
2648 	config.src_addr = r->start + NDDB;
2649 	config.dst_addr = r->start + NDDB;
2650 	config.src_maxburst = 32;
2651 	config.dst_maxburst = 32;
2652 	ret = dmaengine_slave_config(nfc->dma_chan, &config);
2653 	if (ret < 0) {
2654 		dev_err(nfc->dev, "Failed to configure DMA channel\n");
2655 		return ret;
2656 	}
2657 
2658 	/*
2659 	 * DMA must act on length multiple of 32 and this length may be
2660 	 * bigger than the destination buffer. Use this buffer instead
2661 	 * for DMA transfers and then copy the desired amount of data to
2662 	 * the provided buffer.
2663 	 */
2664 	nfc->dma_buf = kmalloc(MAX_CHUNK_SIZE, GFP_KERNEL | GFP_DMA);
2665 	if (!nfc->dma_buf)
2666 		return -ENOMEM;
2667 
2668 	nfc->use_dma = true;
2669 
2670 	return 0;
2671 }
2672 
marvell_nfc_reset(struct marvell_nfc * nfc)2673 static void marvell_nfc_reset(struct marvell_nfc *nfc)
2674 {
2675 	/*
2676 	 * ECC operations and interruptions are only enabled when specifically
2677 	 * needed. ECC shall not be activated in the early stages (fails probe).
2678 	 * Arbiter flag, even if marked as "reserved", must be set (empirical).
2679 	 * SPARE_EN bit must always be set or ECC bytes will not be at the same
2680 	 * offset in the read page and this will fail the protection.
2681 	 */
2682 	writel_relaxed(NDCR_ALL_INT | NDCR_ND_ARB_EN | NDCR_SPARE_EN |
2683 		       NDCR_RD_ID_CNT(NFCV1_READID_LEN), nfc->regs + NDCR);
2684 	writel_relaxed(0xFFFFFFFF, nfc->regs + NDSR);
2685 	writel_relaxed(0, nfc->regs + NDECCCTRL);
2686 }
2687 
marvell_nfc_init(struct marvell_nfc * nfc)2688 static int marvell_nfc_init(struct marvell_nfc *nfc)
2689 {
2690 	struct device_node *np = nfc->dev->of_node;
2691 
2692 	/*
2693 	 * Some SoCs like A7k/A8k need to enable manually the NAND
2694 	 * controller, gated clocks and reset bits to avoid being bootloader
2695 	 * dependent. This is done through the use of the System Functions
2696 	 * registers.
2697 	 */
2698 	if (nfc->caps->need_system_controller) {
2699 		struct regmap *sysctrl_base =
2700 			syscon_regmap_lookup_by_phandle(np,
2701 							"marvell,system-controller");
2702 		u32 reg;
2703 
2704 		if (IS_ERR(sysctrl_base))
2705 			return PTR_ERR(sysctrl_base);
2706 
2707 		reg = GENCONF_SOC_DEVICE_MUX_NFC_EN |
2708 		      GENCONF_SOC_DEVICE_MUX_ECC_CLK_RST |
2709 		      GENCONF_SOC_DEVICE_MUX_ECC_CORE_RST |
2710 		      GENCONF_SOC_DEVICE_MUX_NFC_INT_EN;
2711 		regmap_write(sysctrl_base, GENCONF_SOC_DEVICE_MUX, reg);
2712 
2713 		regmap_read(sysctrl_base, GENCONF_CLK_GATING_CTRL, &reg);
2714 		reg |= GENCONF_CLK_GATING_CTRL_ND_GATE;
2715 		regmap_write(sysctrl_base, GENCONF_CLK_GATING_CTRL, reg);
2716 
2717 		regmap_read(sysctrl_base, GENCONF_ND_CLK_CTRL, &reg);
2718 		reg |= GENCONF_ND_CLK_CTRL_EN;
2719 		regmap_write(sysctrl_base, GENCONF_ND_CLK_CTRL, reg);
2720 	}
2721 
2722 	/* Configure the DMA if appropriate */
2723 	if (!nfc->caps->is_nfcv2)
2724 		marvell_nfc_init_dma(nfc);
2725 
2726 	marvell_nfc_reset(nfc);
2727 
2728 	return 0;
2729 }
2730 
marvell_nfc_probe(struct platform_device * pdev)2731 static int marvell_nfc_probe(struct platform_device *pdev)
2732 {
2733 	struct device *dev = &pdev->dev;
2734 	struct resource *r;
2735 	struct marvell_nfc *nfc;
2736 	int ret;
2737 	int irq;
2738 
2739 	nfc = devm_kzalloc(&pdev->dev, sizeof(struct marvell_nfc),
2740 			   GFP_KERNEL);
2741 	if (!nfc)
2742 		return -ENOMEM;
2743 
2744 	nfc->dev = dev;
2745 	nand_controller_init(&nfc->controller);
2746 	nfc->controller.ops = &marvell_nand_controller_ops;
2747 	INIT_LIST_HEAD(&nfc->chips);
2748 
2749 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2750 	nfc->regs = devm_ioremap_resource(dev, r);
2751 	if (IS_ERR(nfc->regs))
2752 		return PTR_ERR(nfc->regs);
2753 
2754 	irq = platform_get_irq(pdev, 0);
2755 	if (irq < 0) {
2756 		dev_err(dev, "failed to retrieve irq\n");
2757 		return irq;
2758 	}
2759 
2760 	nfc->core_clk = devm_clk_get(&pdev->dev, "core");
2761 
2762 	/* Managed the legacy case (when the first clock was not named) */
2763 	if (nfc->core_clk == ERR_PTR(-ENOENT))
2764 		nfc->core_clk = devm_clk_get(&pdev->dev, NULL);
2765 
2766 	if (IS_ERR(nfc->core_clk))
2767 		return PTR_ERR(nfc->core_clk);
2768 
2769 	ret = clk_prepare_enable(nfc->core_clk);
2770 	if (ret)
2771 		return ret;
2772 
2773 	nfc->reg_clk = devm_clk_get(&pdev->dev, "reg");
2774 	if (IS_ERR(nfc->reg_clk)) {
2775 		if (PTR_ERR(nfc->reg_clk) != -ENOENT) {
2776 			ret = PTR_ERR(nfc->reg_clk);
2777 			goto unprepare_core_clk;
2778 		}
2779 
2780 		nfc->reg_clk = NULL;
2781 	}
2782 
2783 	ret = clk_prepare_enable(nfc->reg_clk);
2784 	if (ret)
2785 		goto unprepare_core_clk;
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 
marvell_nfc_remove(struct platform_device * pdev)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 
marvell_nfc_suspend(struct device * dev)2844 static int __maybe_unused marvell_nfc_suspend(struct device *dev)
2845 {
2846 	struct marvell_nfc *nfc = dev_get_drvdata(dev);
2847 	struct marvell_nand_chip *chip;
2848 
2849 	list_for_each_entry(chip, &nfc->chips, node)
2850 		marvell_nfc_wait_ndrun(&chip->chip);
2851 
2852 	clk_disable_unprepare(nfc->reg_clk);
2853 	clk_disable_unprepare(nfc->core_clk);
2854 
2855 	return 0;
2856 }
2857 
marvell_nfc_resume(struct device * dev)2858 static int __maybe_unused marvell_nfc_resume(struct device *dev)
2859 {
2860 	struct marvell_nfc *nfc = dev_get_drvdata(dev);
2861 	int ret;
2862 
2863 	ret = clk_prepare_enable(nfc->core_clk);
2864 	if (ret < 0)
2865 		return ret;
2866 
2867 	ret = clk_prepare_enable(nfc->reg_clk);
2868 	if (ret < 0)
2869 		return ret;
2870 
2871 	/*
2872 	 * Reset nfc->selected_chip so the next command will cause the timing
2873 	 * registers to be restored in marvell_nfc_select_chip().
2874 	 */
2875 	nfc->selected_chip = NULL;
2876 
2877 	/* Reset registers that have lost their contents */
2878 	marvell_nfc_reset(nfc);
2879 
2880 	return 0;
2881 }
2882 
2883 static const struct dev_pm_ops marvell_nfc_pm_ops = {
2884 	SET_SYSTEM_SLEEP_PM_OPS(marvell_nfc_suspend, marvell_nfc_resume)
2885 };
2886 
2887 static const struct marvell_nfc_caps marvell_armada_8k_nfc_caps = {
2888 	.max_cs_nb = 4,
2889 	.max_rb_nb = 2,
2890 	.need_system_controller = true,
2891 	.is_nfcv2 = true,
2892 };
2893 
2894 static const struct marvell_nfc_caps marvell_armada370_nfc_caps = {
2895 	.max_cs_nb = 4,
2896 	.max_rb_nb = 2,
2897 	.is_nfcv2 = true,
2898 };
2899 
2900 static const struct marvell_nfc_caps marvell_pxa3xx_nfc_caps = {
2901 	.max_cs_nb = 2,
2902 	.max_rb_nb = 1,
2903 	.use_dma = true,
2904 };
2905 
2906 static const struct marvell_nfc_caps marvell_armada_8k_nfc_legacy_caps = {
2907 	.max_cs_nb = 4,
2908 	.max_rb_nb = 2,
2909 	.need_system_controller = true,
2910 	.legacy_of_bindings = true,
2911 	.is_nfcv2 = true,
2912 };
2913 
2914 static const struct marvell_nfc_caps marvell_armada370_nfc_legacy_caps = {
2915 	.max_cs_nb = 4,
2916 	.max_rb_nb = 2,
2917 	.legacy_of_bindings = true,
2918 	.is_nfcv2 = true,
2919 };
2920 
2921 static const struct marvell_nfc_caps marvell_pxa3xx_nfc_legacy_caps = {
2922 	.max_cs_nb = 2,
2923 	.max_rb_nb = 1,
2924 	.legacy_of_bindings = true,
2925 	.use_dma = true,
2926 };
2927 
2928 static const struct platform_device_id marvell_nfc_platform_ids[] = {
2929 	{
2930 		.name = "pxa3xx-nand",
2931 		.driver_data = (kernel_ulong_t)&marvell_pxa3xx_nfc_legacy_caps,
2932 	},
2933 	{ /* sentinel */ },
2934 };
2935 MODULE_DEVICE_TABLE(platform, marvell_nfc_platform_ids);
2936 
2937 static const struct of_device_id marvell_nfc_of_ids[] = {
2938 	{
2939 		.compatible = "marvell,armada-8k-nand-controller",
2940 		.data = &marvell_armada_8k_nfc_caps,
2941 	},
2942 	{
2943 		.compatible = "marvell,armada370-nand-controller",
2944 		.data = &marvell_armada370_nfc_caps,
2945 	},
2946 	{
2947 		.compatible = "marvell,pxa3xx-nand-controller",
2948 		.data = &marvell_pxa3xx_nfc_caps,
2949 	},
2950 	/* Support for old/deprecated bindings: */
2951 	{
2952 		.compatible = "marvell,armada-8k-nand",
2953 		.data = &marvell_armada_8k_nfc_legacy_caps,
2954 	},
2955 	{
2956 		.compatible = "marvell,armada370-nand",
2957 		.data = &marvell_armada370_nfc_legacy_caps,
2958 	},
2959 	{
2960 		.compatible = "marvell,pxa3xx-nand",
2961 		.data = &marvell_pxa3xx_nfc_legacy_caps,
2962 	},
2963 	{ /* sentinel */ },
2964 };
2965 MODULE_DEVICE_TABLE(of, marvell_nfc_of_ids);
2966 
2967 static struct platform_driver marvell_nfc_driver = {
2968 	.driver	= {
2969 		.name		= "marvell-nfc",
2970 		.of_match_table = marvell_nfc_of_ids,
2971 		.pm		= &marvell_nfc_pm_ops,
2972 	},
2973 	.id_table = marvell_nfc_platform_ids,
2974 	.probe = marvell_nfc_probe,
2975 	.remove	= marvell_nfc_remove,
2976 };
2977 module_platform_driver(marvell_nfc_driver);
2978 
2979 MODULE_LICENSE("GPL");
2980 MODULE_DESCRIPTION("Marvell NAND controller driver");
2981