xref: /linux/drivers/mtd/nand/raw/gpmi-nand/gpmi-nand.c (revision 34f7c6e7d4396090692a09789db231e12cb4762b)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Freescale GPMI NAND Flash Driver
4  *
5  * Copyright (C) 2010-2015 Freescale Semiconductor, Inc.
6  * Copyright (C) 2008 Embedded Alley Solutions, Inc.
7  */
8 #include <linux/clk.h>
9 #include <linux/delay.h>
10 #include <linux/slab.h>
11 #include <linux/sched/task_stack.h>
12 #include <linux/interrupt.h>
13 #include <linux/module.h>
14 #include <linux/mtd/partitions.h>
15 #include <linux/of.h>
16 #include <linux/of_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/dma/mxs-dma.h>
19 #include "gpmi-nand.h"
20 #include "gpmi-regs.h"
21 #include "bch-regs.h"
22 
23 /* Resource names for the GPMI NAND driver. */
24 #define GPMI_NAND_GPMI_REGS_ADDR_RES_NAME  "gpmi-nand"
25 #define GPMI_NAND_BCH_REGS_ADDR_RES_NAME   "bch"
26 #define GPMI_NAND_BCH_INTERRUPT_RES_NAME   "bch"
27 
28 /* Converts time to clock cycles */
29 #define TO_CYCLES(duration, period) DIV_ROUND_UP_ULL(duration, period)
30 
31 #define MXS_SET_ADDR		0x4
32 #define MXS_CLR_ADDR		0x8
33 /*
34  * Clear the bit and poll it cleared.  This is usually called with
35  * a reset address and mask being either SFTRST(bit 31) or CLKGATE
36  * (bit 30).
37  */
38 static int clear_poll_bit(void __iomem *addr, u32 mask)
39 {
40 	int timeout = 0x400;
41 
42 	/* clear the bit */
43 	writel(mask, addr + MXS_CLR_ADDR);
44 
45 	/*
46 	 * SFTRST needs 3 GPMI clocks to settle, the reference manual
47 	 * recommends to wait 1us.
48 	 */
49 	udelay(1);
50 
51 	/* poll the bit becoming clear */
52 	while ((readl(addr) & mask) && --timeout)
53 		/* nothing */;
54 
55 	return !timeout;
56 }
57 
58 #define MODULE_CLKGATE		(1 << 30)
59 #define MODULE_SFTRST		(1 << 31)
60 /*
61  * The current mxs_reset_block() will do two things:
62  *  [1] enable the module.
63  *  [2] reset the module.
64  *
65  * In most of the cases, it's ok.
66  * But in MX23, there is a hardware bug in the BCH block (see erratum #2847).
67  * If you try to soft reset the BCH block, it becomes unusable until
68  * the next hard reset. This case occurs in the NAND boot mode. When the board
69  * boots by NAND, the ROM of the chip will initialize the BCH blocks itself.
70  * So If the driver tries to reset the BCH again, the BCH will not work anymore.
71  * You will see a DMA timeout in this case. The bug has been fixed
72  * in the following chips, such as MX28.
73  *
74  * To avoid this bug, just add a new parameter `just_enable` for
75  * the mxs_reset_block(), and rewrite it here.
76  */
77 static int gpmi_reset_block(void __iomem *reset_addr, bool just_enable)
78 {
79 	int ret;
80 	int timeout = 0x400;
81 
82 	/* clear and poll SFTRST */
83 	ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
84 	if (unlikely(ret))
85 		goto error;
86 
87 	/* clear CLKGATE */
88 	writel(MODULE_CLKGATE, reset_addr + MXS_CLR_ADDR);
89 
90 	if (!just_enable) {
91 		/* set SFTRST to reset the block */
92 		writel(MODULE_SFTRST, reset_addr + MXS_SET_ADDR);
93 		udelay(1);
94 
95 		/* poll CLKGATE becoming set */
96 		while ((!(readl(reset_addr) & MODULE_CLKGATE)) && --timeout)
97 			/* nothing */;
98 		if (unlikely(!timeout))
99 			goto error;
100 	}
101 
102 	/* clear and poll SFTRST */
103 	ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
104 	if (unlikely(ret))
105 		goto error;
106 
107 	/* clear and poll CLKGATE */
108 	ret = clear_poll_bit(reset_addr, MODULE_CLKGATE);
109 	if (unlikely(ret))
110 		goto error;
111 
112 	return 0;
113 
114 error:
115 	pr_err("%s(%p): module reset timeout\n", __func__, reset_addr);
116 	return -ETIMEDOUT;
117 }
118 
119 static int __gpmi_enable_clk(struct gpmi_nand_data *this, bool v)
120 {
121 	struct clk *clk;
122 	int ret;
123 	int i;
124 
125 	for (i = 0; i < GPMI_CLK_MAX; i++) {
126 		clk = this->resources.clock[i];
127 		if (!clk)
128 			break;
129 
130 		if (v) {
131 			ret = clk_prepare_enable(clk);
132 			if (ret)
133 				goto err_clk;
134 		} else {
135 			clk_disable_unprepare(clk);
136 		}
137 	}
138 	return 0;
139 
140 err_clk:
141 	for (; i > 0; i--)
142 		clk_disable_unprepare(this->resources.clock[i - 1]);
143 	return ret;
144 }
145 
146 static int gpmi_init(struct gpmi_nand_data *this)
147 {
148 	struct resources *r = &this->resources;
149 	int ret;
150 
151 	ret = pm_runtime_get_sync(this->dev);
152 	if (ret < 0) {
153 		pm_runtime_put_noidle(this->dev);
154 		return ret;
155 	}
156 
157 	ret = gpmi_reset_block(r->gpmi_regs, false);
158 	if (ret)
159 		goto err_out;
160 
161 	/*
162 	 * Reset BCH here, too. We got failures otherwise :(
163 	 * See later BCH reset for explanation of MX23 and MX28 handling
164 	 */
165 	ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MXS(this));
166 	if (ret)
167 		goto err_out;
168 
169 	/* Choose NAND mode. */
170 	writel(BM_GPMI_CTRL1_GPMI_MODE, r->gpmi_regs + HW_GPMI_CTRL1_CLR);
171 
172 	/* Set the IRQ polarity. */
173 	writel(BM_GPMI_CTRL1_ATA_IRQRDY_POLARITY,
174 				r->gpmi_regs + HW_GPMI_CTRL1_SET);
175 
176 	/* Disable Write-Protection. */
177 	writel(BM_GPMI_CTRL1_DEV_RESET, r->gpmi_regs + HW_GPMI_CTRL1_SET);
178 
179 	/* Select BCH ECC. */
180 	writel(BM_GPMI_CTRL1_BCH_MODE, r->gpmi_regs + HW_GPMI_CTRL1_SET);
181 
182 	/*
183 	 * Decouple the chip select from dma channel. We use dma0 for all
184 	 * the chips, force all NAND RDY_BUSY inputs to be sourced from
185 	 * RDY_BUSY0.
186 	 */
187 	writel(BM_GPMI_CTRL1_DECOUPLE_CS | BM_GPMI_CTRL1_GANGED_RDYBUSY,
188 	       r->gpmi_regs + HW_GPMI_CTRL1_SET);
189 
190 err_out:
191 	pm_runtime_mark_last_busy(this->dev);
192 	pm_runtime_put_autosuspend(this->dev);
193 	return ret;
194 }
195 
196 /* This function is very useful. It is called only when the bug occur. */
197 static void gpmi_dump_info(struct gpmi_nand_data *this)
198 {
199 	struct resources *r = &this->resources;
200 	struct bch_geometry *geo = &this->bch_geometry;
201 	u32 reg;
202 	int i;
203 
204 	dev_err(this->dev, "Show GPMI registers :\n");
205 	for (i = 0; i <= HW_GPMI_DEBUG / 0x10 + 1; i++) {
206 		reg = readl(r->gpmi_regs + i * 0x10);
207 		dev_err(this->dev, "offset 0x%.3x : 0x%.8x\n", i * 0x10, reg);
208 	}
209 
210 	/* start to print out the BCH info */
211 	dev_err(this->dev, "Show BCH registers :\n");
212 	for (i = 0; i <= HW_BCH_VERSION / 0x10 + 1; i++) {
213 		reg = readl(r->bch_regs + i * 0x10);
214 		dev_err(this->dev, "offset 0x%.3x : 0x%.8x\n", i * 0x10, reg);
215 	}
216 	dev_err(this->dev, "BCH Geometry :\n"
217 		"GF length              : %u\n"
218 		"ECC Strength           : %u\n"
219 		"Page Size in Bytes     : %u\n"
220 		"Metadata Size in Bytes : %u\n"
221 		"ECC Chunk Size in Bytes: %u\n"
222 		"ECC Chunk Count        : %u\n"
223 		"Payload Size in Bytes  : %u\n"
224 		"Auxiliary Size in Bytes: %u\n"
225 		"Auxiliary Status Offset: %u\n"
226 		"Block Mark Byte Offset : %u\n"
227 		"Block Mark Bit Offset  : %u\n",
228 		geo->gf_len,
229 		geo->ecc_strength,
230 		geo->page_size,
231 		geo->metadata_size,
232 		geo->ecc_chunk_size,
233 		geo->ecc_chunk_count,
234 		geo->payload_size,
235 		geo->auxiliary_size,
236 		geo->auxiliary_status_offset,
237 		geo->block_mark_byte_offset,
238 		geo->block_mark_bit_offset);
239 }
240 
241 static inline bool gpmi_check_ecc(struct gpmi_nand_data *this)
242 {
243 	struct bch_geometry *geo = &this->bch_geometry;
244 
245 	/* Do the sanity check. */
246 	if (GPMI_IS_MXS(this)) {
247 		/* The mx23/mx28 only support the GF13. */
248 		if (geo->gf_len == 14)
249 			return false;
250 	}
251 	return geo->ecc_strength <= this->devdata->bch_max_ecc_strength;
252 }
253 
254 /*
255  * If we can get the ECC information from the nand chip, we do not
256  * need to calculate them ourselves.
257  *
258  * We may have available oob space in this case.
259  */
260 static int set_geometry_by_ecc_info(struct gpmi_nand_data *this,
261 				    unsigned int ecc_strength,
262 				    unsigned int ecc_step)
263 {
264 	struct bch_geometry *geo = &this->bch_geometry;
265 	struct nand_chip *chip = &this->nand;
266 	struct mtd_info *mtd = nand_to_mtd(chip);
267 	unsigned int block_mark_bit_offset;
268 
269 	switch (ecc_step) {
270 	case SZ_512:
271 		geo->gf_len = 13;
272 		break;
273 	case SZ_1K:
274 		geo->gf_len = 14;
275 		break;
276 	default:
277 		dev_err(this->dev,
278 			"unsupported nand chip. ecc bits : %d, ecc size : %d\n",
279 			nanddev_get_ecc_requirements(&chip->base)->strength,
280 			nanddev_get_ecc_requirements(&chip->base)->step_size);
281 		return -EINVAL;
282 	}
283 	geo->ecc_chunk_size = ecc_step;
284 	geo->ecc_strength = round_up(ecc_strength, 2);
285 	if (!gpmi_check_ecc(this))
286 		return -EINVAL;
287 
288 	/* Keep the C >= O */
289 	if (geo->ecc_chunk_size < mtd->oobsize) {
290 		dev_err(this->dev,
291 			"unsupported nand chip. ecc size: %d, oob size : %d\n",
292 			ecc_step, mtd->oobsize);
293 		return -EINVAL;
294 	}
295 
296 	/* The default value, see comment in the legacy_set_geometry(). */
297 	geo->metadata_size = 10;
298 
299 	geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
300 
301 	/*
302 	 * Now, the NAND chip with 2K page(data chunk is 512byte) shows below:
303 	 *
304 	 *    |                          P                            |
305 	 *    |<----------------------------------------------------->|
306 	 *    |                                                       |
307 	 *    |                                        (Block Mark)   |
308 	 *    |                      P'                      |      | |     |
309 	 *    |<-------------------------------------------->|  D   | |  O' |
310 	 *    |                                              |<---->| |<--->|
311 	 *    V                                              V      V V     V
312 	 *    +---+----------+-+----------+-+----------+-+----------+-+-----+
313 	 *    | M |   data   |E|   data   |E|   data   |E|   data   |E|     |
314 	 *    +---+----------+-+----------+-+----------+-+----------+-+-----+
315 	 *                                                   ^              ^
316 	 *                                                   |      O       |
317 	 *                                                   |<------------>|
318 	 *                                                   |              |
319 	 *
320 	 *	P : the page size for BCH module.
321 	 *	E : The ECC strength.
322 	 *	G : the length of Galois Field.
323 	 *	N : The chunk count of per page.
324 	 *	M : the metasize of per page.
325 	 *	C : the ecc chunk size, aka the "data" above.
326 	 *	P': the nand chip's page size.
327 	 *	O : the nand chip's oob size.
328 	 *	O': the free oob.
329 	 *
330 	 *	The formula for P is :
331 	 *
332 	 *	            E * G * N
333 	 *	       P = ------------ + P' + M
334 	 *                      8
335 	 *
336 	 * The position of block mark moves forward in the ECC-based view
337 	 * of page, and the delta is:
338 	 *
339 	 *                   E * G * (N - 1)
340 	 *             D = (---------------- + M)
341 	 *                          8
342 	 *
343 	 * Please see the comment in legacy_set_geometry().
344 	 * With the condition C >= O , we still can get same result.
345 	 * So the bit position of the physical block mark within the ECC-based
346 	 * view of the page is :
347 	 *             (P' - D) * 8
348 	 */
349 	geo->page_size = mtd->writesize + geo->metadata_size +
350 		(geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
351 
352 	geo->payload_size = mtd->writesize;
353 
354 	geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4);
355 	geo->auxiliary_size = ALIGN(geo->metadata_size, 4)
356 				+ ALIGN(geo->ecc_chunk_count, 4);
357 
358 	if (!this->swap_block_mark)
359 		return 0;
360 
361 	/* For bit swap. */
362 	block_mark_bit_offset = mtd->writesize * 8 -
363 		(geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
364 				+ geo->metadata_size * 8);
365 
366 	geo->block_mark_byte_offset = block_mark_bit_offset / 8;
367 	geo->block_mark_bit_offset  = block_mark_bit_offset % 8;
368 	return 0;
369 }
370 
371 /*
372  *  Calculate the ECC strength by hand:
373  *	E : The ECC strength.
374  *	G : the length of Galois Field.
375  *	N : The chunk count of per page.
376  *	O : the oobsize of the NAND chip.
377  *	M : the metasize of per page.
378  *
379  *	The formula is :
380  *		E * G * N
381  *	      ------------ <= (O - M)
382  *                  8
383  *
384  *      So, we get E by:
385  *                    (O - M) * 8
386  *              E <= -------------
387  *                       G * N
388  */
389 static inline int get_ecc_strength(struct gpmi_nand_data *this)
390 {
391 	struct bch_geometry *geo = &this->bch_geometry;
392 	struct mtd_info	*mtd = nand_to_mtd(&this->nand);
393 	int ecc_strength;
394 
395 	ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8)
396 			/ (geo->gf_len * geo->ecc_chunk_count);
397 
398 	/* We need the minor even number. */
399 	return round_down(ecc_strength, 2);
400 }
401 
402 static int legacy_set_geometry(struct gpmi_nand_data *this)
403 {
404 	struct bch_geometry *geo = &this->bch_geometry;
405 	struct mtd_info *mtd = nand_to_mtd(&this->nand);
406 	unsigned int metadata_size;
407 	unsigned int status_size;
408 	unsigned int block_mark_bit_offset;
409 
410 	/*
411 	 * The size of the metadata can be changed, though we set it to 10
412 	 * bytes now. But it can't be too large, because we have to save
413 	 * enough space for BCH.
414 	 */
415 	geo->metadata_size = 10;
416 
417 	/* The default for the length of Galois Field. */
418 	geo->gf_len = 13;
419 
420 	/* The default for chunk size. */
421 	geo->ecc_chunk_size = 512;
422 	while (geo->ecc_chunk_size < mtd->oobsize) {
423 		geo->ecc_chunk_size *= 2; /* keep C >= O */
424 		geo->gf_len = 14;
425 	}
426 
427 	geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
428 
429 	/* We use the same ECC strength for all chunks. */
430 	geo->ecc_strength = get_ecc_strength(this);
431 	if (!gpmi_check_ecc(this)) {
432 		dev_err(this->dev,
433 			"ecc strength: %d cannot be supported by the controller (%d)\n"
434 			"try to use minimum ecc strength that NAND chip required\n",
435 			geo->ecc_strength,
436 			this->devdata->bch_max_ecc_strength);
437 		return -EINVAL;
438 	}
439 
440 	geo->page_size = mtd->writesize + geo->metadata_size +
441 		(geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
442 	geo->payload_size = mtd->writesize;
443 
444 	/*
445 	 * The auxiliary buffer contains the metadata and the ECC status. The
446 	 * metadata is padded to the nearest 32-bit boundary. The ECC status
447 	 * contains one byte for every ECC chunk, and is also padded to the
448 	 * nearest 32-bit boundary.
449 	 */
450 	metadata_size = ALIGN(geo->metadata_size, 4);
451 	status_size   = ALIGN(geo->ecc_chunk_count, 4);
452 
453 	geo->auxiliary_size = metadata_size + status_size;
454 	geo->auxiliary_status_offset = metadata_size;
455 
456 	if (!this->swap_block_mark)
457 		return 0;
458 
459 	/*
460 	 * We need to compute the byte and bit offsets of
461 	 * the physical block mark within the ECC-based view of the page.
462 	 *
463 	 * NAND chip with 2K page shows below:
464 	 *                                             (Block Mark)
465 	 *                                                   |      |
466 	 *                                                   |  D   |
467 	 *                                                   |<---->|
468 	 *                                                   V      V
469 	 *    +---+----------+-+----------+-+----------+-+----------+-+
470 	 *    | M |   data   |E|   data   |E|   data   |E|   data   |E|
471 	 *    +---+----------+-+----------+-+----------+-+----------+-+
472 	 *
473 	 * The position of block mark moves forward in the ECC-based view
474 	 * of page, and the delta is:
475 	 *
476 	 *                   E * G * (N - 1)
477 	 *             D = (---------------- + M)
478 	 *                          8
479 	 *
480 	 * With the formula to compute the ECC strength, and the condition
481 	 *       : C >= O         (C is the ecc chunk size)
482 	 *
483 	 * It's easy to deduce to the following result:
484 	 *
485 	 *         E * G       (O - M)      C - M         C - M
486 	 *      ----------- <= ------- <=  --------  <  ---------
487 	 *           8            N           N          (N - 1)
488 	 *
489 	 *  So, we get:
490 	 *
491 	 *                   E * G * (N - 1)
492 	 *             D = (---------------- + M) < C
493 	 *                          8
494 	 *
495 	 *  The above inequality means the position of block mark
496 	 *  within the ECC-based view of the page is still in the data chunk,
497 	 *  and it's NOT in the ECC bits of the chunk.
498 	 *
499 	 *  Use the following to compute the bit position of the
500 	 *  physical block mark within the ECC-based view of the page:
501 	 *          (page_size - D) * 8
502 	 *
503 	 *  --Huang Shijie
504 	 */
505 	block_mark_bit_offset = mtd->writesize * 8 -
506 		(geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
507 				+ geo->metadata_size * 8);
508 
509 	geo->block_mark_byte_offset = block_mark_bit_offset / 8;
510 	geo->block_mark_bit_offset  = block_mark_bit_offset % 8;
511 	return 0;
512 }
513 
514 static int common_nfc_set_geometry(struct gpmi_nand_data *this)
515 {
516 	struct nand_chip *chip = &this->nand;
517 	const struct nand_ecc_props *requirements =
518 		nanddev_get_ecc_requirements(&chip->base);
519 
520 	if (chip->ecc.strength > 0 && chip->ecc.size > 0)
521 		return set_geometry_by_ecc_info(this, chip->ecc.strength,
522 						chip->ecc.size);
523 
524 	if ((of_property_read_bool(this->dev->of_node, "fsl,use-minimum-ecc"))
525 				|| legacy_set_geometry(this)) {
526 		if (!(requirements->strength > 0 && requirements->step_size > 0))
527 			return -EINVAL;
528 
529 		return set_geometry_by_ecc_info(this,
530 						requirements->strength,
531 						requirements->step_size);
532 	}
533 
534 	return 0;
535 }
536 
537 /* Configures the geometry for BCH.  */
538 static int bch_set_geometry(struct gpmi_nand_data *this)
539 {
540 	struct resources *r = &this->resources;
541 	int ret;
542 
543 	ret = common_nfc_set_geometry(this);
544 	if (ret)
545 		return ret;
546 
547 	ret = pm_runtime_get_sync(this->dev);
548 	if (ret < 0) {
549 		pm_runtime_put_autosuspend(this->dev);
550 		return ret;
551 	}
552 
553 	/*
554 	* Due to erratum #2847 of the MX23, the BCH cannot be soft reset on this
555 	* chip, otherwise it will lock up. So we skip resetting BCH on the MX23.
556 	* and MX28.
557 	*/
558 	ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MXS(this));
559 	if (ret)
560 		goto err_out;
561 
562 	/* Set *all* chip selects to use layout 0. */
563 	writel(0, r->bch_regs + HW_BCH_LAYOUTSELECT);
564 
565 	ret = 0;
566 err_out:
567 	pm_runtime_mark_last_busy(this->dev);
568 	pm_runtime_put_autosuspend(this->dev);
569 
570 	return ret;
571 }
572 
573 /*
574  * <1> Firstly, we should know what's the GPMI-clock means.
575  *     The GPMI-clock is the internal clock in the gpmi nand controller.
576  *     If you set 100MHz to gpmi nand controller, the GPMI-clock's period
577  *     is 10ns. Mark the GPMI-clock's period as GPMI-clock-period.
578  *
579  * <2> Secondly, we should know what's the frequency on the nand chip pins.
580  *     The frequency on the nand chip pins is derived from the GPMI-clock.
581  *     We can get it from the following equation:
582  *
583  *         F = G / (DS + DH)
584  *
585  *         F  : the frequency on the nand chip pins.
586  *         G  : the GPMI clock, such as 100MHz.
587  *         DS : GPMI_HW_GPMI_TIMING0:DATA_SETUP
588  *         DH : GPMI_HW_GPMI_TIMING0:DATA_HOLD
589  *
590  * <3> Thirdly, when the frequency on the nand chip pins is above 33MHz,
591  *     the nand EDO(extended Data Out) timing could be applied.
592  *     The GPMI implements a feedback read strobe to sample the read data.
593  *     The feedback read strobe can be delayed to support the nand EDO timing
594  *     where the read strobe may deasserts before the read data is valid, and
595  *     read data is valid for some time after read strobe.
596  *
597  *     The following figure illustrates some aspects of a NAND Flash read:
598  *
599  *                   |<---tREA---->|
600  *                   |             |
601  *                   |         |   |
602  *                   |<--tRP-->|   |
603  *                   |         |   |
604  *                  __          ___|__________________________________
605  *     RDN            \________/   |
606  *                                 |
607  *                                 /---------\
608  *     Read Data    --------------<           >---------
609  *                                 \---------/
610  *                                |     |
611  *                                |<-D->|
612  *     FeedbackRDN  ________             ____________
613  *                          \___________/
614  *
615  *          D stands for delay, set in the HW_GPMI_CTRL1:RDN_DELAY.
616  *
617  *
618  * <4> Now, we begin to describe how to compute the right RDN_DELAY.
619  *
620  *  4.1) From the aspect of the nand chip pins:
621  *        Delay = (tREA + C - tRP)               {1}
622  *
623  *        tREA : the maximum read access time.
624  *        C    : a constant to adjust the delay. default is 4000ps.
625  *        tRP  : the read pulse width, which is exactly:
626  *                   tRP = (GPMI-clock-period) * DATA_SETUP
627  *
628  *  4.2) From the aspect of the GPMI nand controller:
629  *         Delay = RDN_DELAY * 0.125 * RP        {2}
630  *
631  *         RP   : the DLL reference period.
632  *            if (GPMI-clock-period > DLL_THRETHOLD)
633  *                   RP = GPMI-clock-period / 2;
634  *            else
635  *                   RP = GPMI-clock-period;
636  *
637  *            Set the HW_GPMI_CTRL1:HALF_PERIOD if GPMI-clock-period
638  *            is greater DLL_THRETHOLD. In other SOCs, the DLL_THRETHOLD
639  *            is 16000ps, but in mx6q, we use 12000ps.
640  *
641  *  4.3) since {1} equals {2}, we get:
642  *
643  *                     (tREA + 4000 - tRP) * 8
644  *         RDN_DELAY = -----------------------     {3}
645  *                           RP
646  */
647 static int gpmi_nfc_compute_timings(struct gpmi_nand_data *this,
648 				    const struct nand_sdr_timings *sdr)
649 {
650 	struct gpmi_nfc_hardware_timing *hw = &this->hw;
651 	struct resources *r = &this->resources;
652 	unsigned int dll_threshold_ps = this->devdata->max_chain_delay;
653 	unsigned int period_ps, reference_period_ps;
654 	unsigned int data_setup_cycles, data_hold_cycles, addr_setup_cycles;
655 	unsigned int tRP_ps;
656 	bool use_half_period;
657 	int sample_delay_ps, sample_delay_factor;
658 	u16 busy_timeout_cycles;
659 	u8 wrn_dly_sel;
660 	unsigned long clk_rate, min_rate;
661 
662 	if (sdr->tRC_min >= 30000) {
663 		/* ONFI non-EDO modes [0-3] */
664 		hw->clk_rate = 22000000;
665 		min_rate = 0;
666 		wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_4_TO_8NS;
667 	} else if (sdr->tRC_min >= 25000) {
668 		/* ONFI EDO mode 4 */
669 		hw->clk_rate = 80000000;
670 		min_rate = 22000000;
671 		wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY;
672 	} else {
673 		/* ONFI EDO mode 5 */
674 		hw->clk_rate = 100000000;
675 		min_rate = 80000000;
676 		wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY;
677 	}
678 
679 	clk_rate = clk_round_rate(r->clock[0], hw->clk_rate);
680 	if (clk_rate <= min_rate) {
681 		dev_err(this->dev, "clock setting: expected %ld, got %ld\n",
682 			hw->clk_rate, clk_rate);
683 		return -ENOTSUPP;
684 	}
685 
686 	hw->clk_rate = clk_rate;
687 	/* SDR core timings are given in picoseconds */
688 	period_ps = div_u64((u64)NSEC_PER_SEC * 1000, hw->clk_rate);
689 
690 	addr_setup_cycles = TO_CYCLES(sdr->tALS_min, period_ps);
691 	data_setup_cycles = TO_CYCLES(sdr->tDS_min, period_ps);
692 	data_hold_cycles = TO_CYCLES(sdr->tDH_min, period_ps);
693 	busy_timeout_cycles = TO_CYCLES(sdr->tWB_max + sdr->tR_max, period_ps);
694 
695 	hw->timing0 = BF_GPMI_TIMING0_ADDRESS_SETUP(addr_setup_cycles) |
696 		      BF_GPMI_TIMING0_DATA_HOLD(data_hold_cycles) |
697 		      BF_GPMI_TIMING0_DATA_SETUP(data_setup_cycles);
698 	hw->timing1 = BF_GPMI_TIMING1_BUSY_TIMEOUT(busy_timeout_cycles * 4096);
699 
700 	/*
701 	 * Derive NFC ideal delay from {3}:
702 	 *
703 	 *                     (tREA + 4000 - tRP) * 8
704 	 *         RDN_DELAY = -----------------------
705 	 *                                RP
706 	 */
707 	if (period_ps > dll_threshold_ps) {
708 		use_half_period = true;
709 		reference_period_ps = period_ps / 2;
710 	} else {
711 		use_half_period = false;
712 		reference_period_ps = period_ps;
713 	}
714 
715 	tRP_ps = data_setup_cycles * period_ps;
716 	sample_delay_ps = (sdr->tREA_max + 4000 - tRP_ps) * 8;
717 	if (sample_delay_ps > 0)
718 		sample_delay_factor = sample_delay_ps / reference_period_ps;
719 	else
720 		sample_delay_factor = 0;
721 
722 	hw->ctrl1n = BF_GPMI_CTRL1_WRN_DLY_SEL(wrn_dly_sel);
723 	if (sample_delay_factor)
724 		hw->ctrl1n |= BF_GPMI_CTRL1_RDN_DELAY(sample_delay_factor) |
725 			      BM_GPMI_CTRL1_DLL_ENABLE |
726 			      (use_half_period ? BM_GPMI_CTRL1_HALF_PERIOD : 0);
727 	return 0;
728 }
729 
730 static int gpmi_nfc_apply_timings(struct gpmi_nand_data *this)
731 {
732 	struct gpmi_nfc_hardware_timing *hw = &this->hw;
733 	struct resources *r = &this->resources;
734 	void __iomem *gpmi_regs = r->gpmi_regs;
735 	unsigned int dll_wait_time_us;
736 	int ret;
737 
738 	/* Clock dividers do NOT guarantee a clean clock signal on its output
739 	 * during the change of the divide factor on i.MX6Q/UL/SX. On i.MX7/8,
740 	 * all clock dividers provide these guarantee.
741 	 */
742 	if (GPMI_IS_MX6Q(this) || GPMI_IS_MX6SX(this))
743 		clk_disable_unprepare(r->clock[0]);
744 
745 	ret = clk_set_rate(r->clock[0], hw->clk_rate);
746 	if (ret) {
747 		dev_err(this->dev, "cannot set clock rate to %lu Hz: %d\n", hw->clk_rate, ret);
748 		return ret;
749 	}
750 
751 	if (GPMI_IS_MX6Q(this) || GPMI_IS_MX6SX(this)) {
752 		ret = clk_prepare_enable(r->clock[0]);
753 		if (ret)
754 			return ret;
755 	}
756 
757 	writel(hw->timing0, gpmi_regs + HW_GPMI_TIMING0);
758 	writel(hw->timing1, gpmi_regs + HW_GPMI_TIMING1);
759 
760 	/*
761 	 * Clear several CTRL1 fields, DLL must be disabled when setting
762 	 * RDN_DELAY or HALF_PERIOD.
763 	 */
764 	writel(BM_GPMI_CTRL1_CLEAR_MASK, gpmi_regs + HW_GPMI_CTRL1_CLR);
765 	writel(hw->ctrl1n, gpmi_regs + HW_GPMI_CTRL1_SET);
766 
767 	/* Wait 64 clock cycles before using the GPMI after enabling the DLL */
768 	dll_wait_time_us = USEC_PER_SEC / hw->clk_rate * 64;
769 	if (!dll_wait_time_us)
770 		dll_wait_time_us = 1;
771 
772 	/* Wait for the DLL to settle. */
773 	udelay(dll_wait_time_us);
774 
775 	return 0;
776 }
777 
778 static int gpmi_setup_interface(struct nand_chip *chip, int chipnr,
779 				const struct nand_interface_config *conf)
780 {
781 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
782 	const struct nand_sdr_timings *sdr;
783 	int ret;
784 
785 	/* Retrieve required NAND timings */
786 	sdr = nand_get_sdr_timings(conf);
787 	if (IS_ERR(sdr))
788 		return PTR_ERR(sdr);
789 
790 	/* Only MX28/MX6 GPMI controller can reach EDO timings */
791 	if (sdr->tRC_min <= 25000 && !GPMI_IS_MX28(this) && !GPMI_IS_MX6(this))
792 		return -ENOTSUPP;
793 
794 	/* Stop here if this call was just a check */
795 	if (chipnr < 0)
796 		return 0;
797 
798 	/* Do the actual derivation of the controller timings */
799 	ret = gpmi_nfc_compute_timings(this, sdr);
800 	if (ret)
801 		return ret;
802 
803 	this->hw.must_apply_timings = true;
804 
805 	return 0;
806 }
807 
808 /* Clears a BCH interrupt. */
809 static void gpmi_clear_bch(struct gpmi_nand_data *this)
810 {
811 	struct resources *r = &this->resources;
812 	writel(BM_BCH_CTRL_COMPLETE_IRQ, r->bch_regs + HW_BCH_CTRL_CLR);
813 }
814 
815 static struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
816 {
817 	/* We use the DMA channel 0 to access all the nand chips. */
818 	return this->dma_chans[0];
819 }
820 
821 /* This will be called after the DMA operation is finished. */
822 static void dma_irq_callback(void *param)
823 {
824 	struct gpmi_nand_data *this = param;
825 	struct completion *dma_c = &this->dma_done;
826 
827 	complete(dma_c);
828 }
829 
830 static irqreturn_t bch_irq(int irq, void *cookie)
831 {
832 	struct gpmi_nand_data *this = cookie;
833 
834 	gpmi_clear_bch(this);
835 	complete(&this->bch_done);
836 	return IRQ_HANDLED;
837 }
838 
839 static int gpmi_raw_len_to_len(struct gpmi_nand_data *this, int raw_len)
840 {
841 	/*
842 	 * raw_len is the length to read/write including bch data which
843 	 * we are passed in exec_op. Calculate the data length from it.
844 	 */
845 	if (this->bch)
846 		return ALIGN_DOWN(raw_len, this->bch_geometry.ecc_chunk_size);
847 	else
848 		return raw_len;
849 }
850 
851 /* Can we use the upper's buffer directly for DMA? */
852 static bool prepare_data_dma(struct gpmi_nand_data *this, const void *buf,
853 			     int raw_len, struct scatterlist *sgl,
854 			     enum dma_data_direction dr)
855 {
856 	int ret;
857 	int len = gpmi_raw_len_to_len(this, raw_len);
858 
859 	/* first try to map the upper buffer directly */
860 	if (virt_addr_valid(buf) && !object_is_on_stack(buf)) {
861 		sg_init_one(sgl, buf, len);
862 		ret = dma_map_sg(this->dev, sgl, 1, dr);
863 		if (ret == 0)
864 			goto map_fail;
865 
866 		return true;
867 	}
868 
869 map_fail:
870 	/* We have to use our own DMA buffer. */
871 	sg_init_one(sgl, this->data_buffer_dma, len);
872 
873 	if (dr == DMA_TO_DEVICE && buf != this->data_buffer_dma)
874 		memcpy(this->data_buffer_dma, buf, len);
875 
876 	dma_map_sg(this->dev, sgl, 1, dr);
877 
878 	return false;
879 }
880 
881 /* add our owner bbt descriptor */
882 static uint8_t scan_ff_pattern[] = { 0xff };
883 static struct nand_bbt_descr gpmi_bbt_descr = {
884 	.options	= 0,
885 	.offs		= 0,
886 	.len		= 1,
887 	.pattern	= scan_ff_pattern
888 };
889 
890 /*
891  * We may change the layout if we can get the ECC info from the datasheet,
892  * else we will use all the (page + OOB).
893  */
894 static int gpmi_ooblayout_ecc(struct mtd_info *mtd, int section,
895 			      struct mtd_oob_region *oobregion)
896 {
897 	struct nand_chip *chip = mtd_to_nand(mtd);
898 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
899 	struct bch_geometry *geo = &this->bch_geometry;
900 
901 	if (section)
902 		return -ERANGE;
903 
904 	oobregion->offset = 0;
905 	oobregion->length = geo->page_size - mtd->writesize;
906 
907 	return 0;
908 }
909 
910 static int gpmi_ooblayout_free(struct mtd_info *mtd, int section,
911 			       struct mtd_oob_region *oobregion)
912 {
913 	struct nand_chip *chip = mtd_to_nand(mtd);
914 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
915 	struct bch_geometry *geo = &this->bch_geometry;
916 
917 	if (section)
918 		return -ERANGE;
919 
920 	/* The available oob size we have. */
921 	if (geo->page_size < mtd->writesize + mtd->oobsize) {
922 		oobregion->offset = geo->page_size - mtd->writesize;
923 		oobregion->length = mtd->oobsize - oobregion->offset;
924 	}
925 
926 	return 0;
927 }
928 
929 static const char * const gpmi_clks_for_mx2x[] = {
930 	"gpmi_io",
931 };
932 
933 static const struct mtd_ooblayout_ops gpmi_ooblayout_ops = {
934 	.ecc = gpmi_ooblayout_ecc,
935 	.free = gpmi_ooblayout_free,
936 };
937 
938 static const struct gpmi_devdata gpmi_devdata_imx23 = {
939 	.type = IS_MX23,
940 	.bch_max_ecc_strength = 20,
941 	.max_chain_delay = 16000,
942 	.clks = gpmi_clks_for_mx2x,
943 	.clks_count = ARRAY_SIZE(gpmi_clks_for_mx2x),
944 };
945 
946 static const struct gpmi_devdata gpmi_devdata_imx28 = {
947 	.type = IS_MX28,
948 	.bch_max_ecc_strength = 20,
949 	.max_chain_delay = 16000,
950 	.clks = gpmi_clks_for_mx2x,
951 	.clks_count = ARRAY_SIZE(gpmi_clks_for_mx2x),
952 };
953 
954 static const char * const gpmi_clks_for_mx6[] = {
955 	"gpmi_io", "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch",
956 };
957 
958 static const struct gpmi_devdata gpmi_devdata_imx6q = {
959 	.type = IS_MX6Q,
960 	.bch_max_ecc_strength = 40,
961 	.max_chain_delay = 12000,
962 	.clks = gpmi_clks_for_mx6,
963 	.clks_count = ARRAY_SIZE(gpmi_clks_for_mx6),
964 };
965 
966 static const struct gpmi_devdata gpmi_devdata_imx6sx = {
967 	.type = IS_MX6SX,
968 	.bch_max_ecc_strength = 62,
969 	.max_chain_delay = 12000,
970 	.clks = gpmi_clks_for_mx6,
971 	.clks_count = ARRAY_SIZE(gpmi_clks_for_mx6),
972 };
973 
974 static const char * const gpmi_clks_for_mx7d[] = {
975 	"gpmi_io", "gpmi_bch_apb",
976 };
977 
978 static const struct gpmi_devdata gpmi_devdata_imx7d = {
979 	.type = IS_MX7D,
980 	.bch_max_ecc_strength = 62,
981 	.max_chain_delay = 12000,
982 	.clks = gpmi_clks_for_mx7d,
983 	.clks_count = ARRAY_SIZE(gpmi_clks_for_mx7d),
984 };
985 
986 static int acquire_register_block(struct gpmi_nand_data *this,
987 				  const char *res_name)
988 {
989 	struct platform_device *pdev = this->pdev;
990 	struct resources *res = &this->resources;
991 	void __iomem *p;
992 
993 	p = devm_platform_ioremap_resource_byname(pdev, res_name);
994 	if (IS_ERR(p))
995 		return PTR_ERR(p);
996 
997 	if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
998 		res->gpmi_regs = p;
999 	else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
1000 		res->bch_regs = p;
1001 	else
1002 		dev_err(this->dev, "unknown resource name : %s\n", res_name);
1003 
1004 	return 0;
1005 }
1006 
1007 static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
1008 {
1009 	struct platform_device *pdev = this->pdev;
1010 	const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
1011 	int err;
1012 
1013 	err = platform_get_irq_byname(pdev, res_name);
1014 	if (err < 0)
1015 		return err;
1016 
1017 	err = devm_request_irq(this->dev, err, irq_h, 0, res_name, this);
1018 	if (err)
1019 		dev_err(this->dev, "error requesting BCH IRQ\n");
1020 
1021 	return err;
1022 }
1023 
1024 static void release_dma_channels(struct gpmi_nand_data *this)
1025 {
1026 	unsigned int i;
1027 	for (i = 0; i < DMA_CHANS; i++)
1028 		if (this->dma_chans[i]) {
1029 			dma_release_channel(this->dma_chans[i]);
1030 			this->dma_chans[i] = NULL;
1031 		}
1032 }
1033 
1034 static int acquire_dma_channels(struct gpmi_nand_data *this)
1035 {
1036 	struct platform_device *pdev = this->pdev;
1037 	struct dma_chan *dma_chan;
1038 	int ret = 0;
1039 
1040 	/* request dma channel */
1041 	dma_chan = dma_request_chan(&pdev->dev, "rx-tx");
1042 	if (IS_ERR(dma_chan)) {
1043 		ret = dev_err_probe(this->dev, PTR_ERR(dma_chan),
1044 				    "DMA channel request failed\n");
1045 		release_dma_channels(this);
1046 	} else {
1047 		this->dma_chans[0] = dma_chan;
1048 	}
1049 
1050 	return ret;
1051 }
1052 
1053 static int gpmi_get_clks(struct gpmi_nand_data *this)
1054 {
1055 	struct resources *r = &this->resources;
1056 	struct clk *clk;
1057 	int err, i;
1058 
1059 	for (i = 0; i < this->devdata->clks_count; i++) {
1060 		clk = devm_clk_get(this->dev, this->devdata->clks[i]);
1061 		if (IS_ERR(clk)) {
1062 			err = PTR_ERR(clk);
1063 			goto err_clock;
1064 		}
1065 
1066 		r->clock[i] = clk;
1067 	}
1068 
1069 	return 0;
1070 
1071 err_clock:
1072 	dev_dbg(this->dev, "failed in finding the clocks.\n");
1073 	return err;
1074 }
1075 
1076 static int acquire_resources(struct gpmi_nand_data *this)
1077 {
1078 	int ret;
1079 
1080 	ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
1081 	if (ret)
1082 		goto exit_regs;
1083 
1084 	ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
1085 	if (ret)
1086 		goto exit_regs;
1087 
1088 	ret = acquire_bch_irq(this, bch_irq);
1089 	if (ret)
1090 		goto exit_regs;
1091 
1092 	ret = acquire_dma_channels(this);
1093 	if (ret)
1094 		goto exit_regs;
1095 
1096 	ret = gpmi_get_clks(this);
1097 	if (ret)
1098 		goto exit_clock;
1099 	return 0;
1100 
1101 exit_clock:
1102 	release_dma_channels(this);
1103 exit_regs:
1104 	return ret;
1105 }
1106 
1107 static void release_resources(struct gpmi_nand_data *this)
1108 {
1109 	release_dma_channels(this);
1110 }
1111 
1112 static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
1113 {
1114 	struct device *dev = this->dev;
1115 	struct bch_geometry *geo = &this->bch_geometry;
1116 
1117 	if (this->auxiliary_virt && virt_addr_valid(this->auxiliary_virt))
1118 		dma_free_coherent(dev, geo->auxiliary_size,
1119 					this->auxiliary_virt,
1120 					this->auxiliary_phys);
1121 	kfree(this->data_buffer_dma);
1122 	kfree(this->raw_buffer);
1123 
1124 	this->data_buffer_dma	= NULL;
1125 	this->raw_buffer	= NULL;
1126 }
1127 
1128 /* Allocate the DMA buffers */
1129 static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
1130 {
1131 	struct bch_geometry *geo = &this->bch_geometry;
1132 	struct device *dev = this->dev;
1133 	struct mtd_info *mtd = nand_to_mtd(&this->nand);
1134 
1135 	/*
1136 	 * [2] Allocate a read/write data buffer.
1137 	 *     The gpmi_alloc_dma_buffer can be called twice.
1138 	 *     We allocate a PAGE_SIZE length buffer if gpmi_alloc_dma_buffer
1139 	 *     is called before the NAND identification; and we allocate a
1140 	 *     buffer of the real NAND page size when the gpmi_alloc_dma_buffer
1141 	 *     is called after.
1142 	 */
1143 	this->data_buffer_dma = kzalloc(mtd->writesize ?: PAGE_SIZE,
1144 					GFP_DMA | GFP_KERNEL);
1145 	if (this->data_buffer_dma == NULL)
1146 		goto error_alloc;
1147 
1148 	this->auxiliary_virt = dma_alloc_coherent(dev, geo->auxiliary_size,
1149 					&this->auxiliary_phys, GFP_DMA);
1150 	if (!this->auxiliary_virt)
1151 		goto error_alloc;
1152 
1153 	this->raw_buffer = kzalloc((mtd->writesize ?: PAGE_SIZE) + mtd->oobsize, GFP_KERNEL);
1154 	if (!this->raw_buffer)
1155 		goto error_alloc;
1156 
1157 	return 0;
1158 
1159 error_alloc:
1160 	gpmi_free_dma_buffer(this);
1161 	return -ENOMEM;
1162 }
1163 
1164 /*
1165  * Handles block mark swapping.
1166  * It can be called in swapping the block mark, or swapping it back,
1167  * because the the operations are the same.
1168  */
1169 static void block_mark_swapping(struct gpmi_nand_data *this,
1170 				void *payload, void *auxiliary)
1171 {
1172 	struct bch_geometry *nfc_geo = &this->bch_geometry;
1173 	unsigned char *p;
1174 	unsigned char *a;
1175 	unsigned int  bit;
1176 	unsigned char mask;
1177 	unsigned char from_data;
1178 	unsigned char from_oob;
1179 
1180 	if (!this->swap_block_mark)
1181 		return;
1182 
1183 	/*
1184 	 * If control arrives here, we're swapping. Make some convenience
1185 	 * variables.
1186 	 */
1187 	bit = nfc_geo->block_mark_bit_offset;
1188 	p   = payload + nfc_geo->block_mark_byte_offset;
1189 	a   = auxiliary;
1190 
1191 	/*
1192 	 * Get the byte from the data area that overlays the block mark. Since
1193 	 * the ECC engine applies its own view to the bits in the page, the
1194 	 * physical block mark won't (in general) appear on a byte boundary in
1195 	 * the data.
1196 	 */
1197 	from_data = (p[0] >> bit) | (p[1] << (8 - bit));
1198 
1199 	/* Get the byte from the OOB. */
1200 	from_oob = a[0];
1201 
1202 	/* Swap them. */
1203 	a[0] = from_data;
1204 
1205 	mask = (0x1 << bit) - 1;
1206 	p[0] = (p[0] & mask) | (from_oob << bit);
1207 
1208 	mask = ~0 << bit;
1209 	p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
1210 }
1211 
1212 static int gpmi_count_bitflips(struct nand_chip *chip, void *buf, int first,
1213 			       int last, int meta)
1214 {
1215 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
1216 	struct bch_geometry *nfc_geo = &this->bch_geometry;
1217 	struct mtd_info *mtd = nand_to_mtd(chip);
1218 	int i;
1219 	unsigned char *status;
1220 	unsigned int max_bitflips = 0;
1221 
1222 	/* Loop over status bytes, accumulating ECC status. */
1223 	status = this->auxiliary_virt + ALIGN(meta, 4);
1224 
1225 	for (i = first; i < last; i++, status++) {
1226 		if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
1227 			continue;
1228 
1229 		if (*status == STATUS_UNCORRECTABLE) {
1230 			int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
1231 			u8 *eccbuf = this->raw_buffer;
1232 			int offset, bitoffset;
1233 			int eccbytes;
1234 			int flips;
1235 
1236 			/* Read ECC bytes into our internal raw_buffer */
1237 			offset = nfc_geo->metadata_size * 8;
1238 			offset += ((8 * nfc_geo->ecc_chunk_size) + eccbits) * (i + 1);
1239 			offset -= eccbits;
1240 			bitoffset = offset % 8;
1241 			eccbytes = DIV_ROUND_UP(offset + eccbits, 8);
1242 			offset /= 8;
1243 			eccbytes -= offset;
1244 			nand_change_read_column_op(chip, offset, eccbuf,
1245 						   eccbytes, false);
1246 
1247 			/*
1248 			 * ECC data are not byte aligned and we may have
1249 			 * in-band data in the first and last byte of
1250 			 * eccbuf. Set non-eccbits to one so that
1251 			 * nand_check_erased_ecc_chunk() does not count them
1252 			 * as bitflips.
1253 			 */
1254 			if (bitoffset)
1255 				eccbuf[0] |= GENMASK(bitoffset - 1, 0);
1256 
1257 			bitoffset = (bitoffset + eccbits) % 8;
1258 			if (bitoffset)
1259 				eccbuf[eccbytes - 1] |= GENMASK(7, bitoffset);
1260 
1261 			/*
1262 			 * The ECC hardware has an uncorrectable ECC status
1263 			 * code in case we have bitflips in an erased page. As
1264 			 * nothing was written into this subpage the ECC is
1265 			 * obviously wrong and we can not trust it. We assume
1266 			 * at this point that we are reading an erased page and
1267 			 * try to correct the bitflips in buffer up to
1268 			 * ecc_strength bitflips. If this is a page with random
1269 			 * data, we exceed this number of bitflips and have a
1270 			 * ECC failure. Otherwise we use the corrected buffer.
1271 			 */
1272 			if (i == 0) {
1273 				/* The first block includes metadata */
1274 				flips = nand_check_erased_ecc_chunk(
1275 						buf + i * nfc_geo->ecc_chunk_size,
1276 						nfc_geo->ecc_chunk_size,
1277 						eccbuf, eccbytes,
1278 						this->auxiliary_virt,
1279 						nfc_geo->metadata_size,
1280 						nfc_geo->ecc_strength);
1281 			} else {
1282 				flips = nand_check_erased_ecc_chunk(
1283 						buf + i * nfc_geo->ecc_chunk_size,
1284 						nfc_geo->ecc_chunk_size,
1285 						eccbuf, eccbytes,
1286 						NULL, 0,
1287 						nfc_geo->ecc_strength);
1288 			}
1289 
1290 			if (flips > 0) {
1291 				max_bitflips = max_t(unsigned int, max_bitflips,
1292 						     flips);
1293 				mtd->ecc_stats.corrected += flips;
1294 				continue;
1295 			}
1296 
1297 			mtd->ecc_stats.failed++;
1298 			continue;
1299 		}
1300 
1301 		mtd->ecc_stats.corrected += *status;
1302 		max_bitflips = max_t(unsigned int, max_bitflips, *status);
1303 	}
1304 
1305 	return max_bitflips;
1306 }
1307 
1308 static void gpmi_bch_layout_std(struct gpmi_nand_data *this)
1309 {
1310 	struct bch_geometry *geo = &this->bch_geometry;
1311 	unsigned int ecc_strength = geo->ecc_strength >> 1;
1312 	unsigned int gf_len = geo->gf_len;
1313 	unsigned int block_size = geo->ecc_chunk_size;
1314 
1315 	this->bch_flashlayout0 =
1316 		BF_BCH_FLASH0LAYOUT0_NBLOCKS(geo->ecc_chunk_count - 1) |
1317 		BF_BCH_FLASH0LAYOUT0_META_SIZE(geo->metadata_size) |
1318 		BF_BCH_FLASH0LAYOUT0_ECC0(ecc_strength, this) |
1319 		BF_BCH_FLASH0LAYOUT0_GF(gf_len, this) |
1320 		BF_BCH_FLASH0LAYOUT0_DATA0_SIZE(block_size, this);
1321 
1322 	this->bch_flashlayout1 =
1323 		BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(geo->page_size) |
1324 		BF_BCH_FLASH0LAYOUT1_ECCN(ecc_strength, this) |
1325 		BF_BCH_FLASH0LAYOUT1_GF(gf_len, this) |
1326 		BF_BCH_FLASH0LAYOUT1_DATAN_SIZE(block_size, this);
1327 }
1328 
1329 static int gpmi_ecc_read_page(struct nand_chip *chip, uint8_t *buf,
1330 			      int oob_required, int page)
1331 {
1332 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
1333 	struct mtd_info *mtd = nand_to_mtd(chip);
1334 	struct bch_geometry *geo = &this->bch_geometry;
1335 	unsigned int max_bitflips;
1336 	int ret;
1337 
1338 	gpmi_bch_layout_std(this);
1339 	this->bch = true;
1340 
1341 	ret = nand_read_page_op(chip, page, 0, buf, geo->page_size);
1342 	if (ret)
1343 		return ret;
1344 
1345 	max_bitflips = gpmi_count_bitflips(chip, buf, 0,
1346 					   geo->ecc_chunk_count,
1347 					   geo->auxiliary_status_offset);
1348 
1349 	/* handle the block mark swapping */
1350 	block_mark_swapping(this, buf, this->auxiliary_virt);
1351 
1352 	if (oob_required) {
1353 		/*
1354 		 * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
1355 		 * for details about our policy for delivering the OOB.
1356 		 *
1357 		 * We fill the caller's buffer with set bits, and then copy the
1358 		 * block mark to th caller's buffer. Note that, if block mark
1359 		 * swapping was necessary, it has already been done, so we can
1360 		 * rely on the first byte of the auxiliary buffer to contain
1361 		 * the block mark.
1362 		 */
1363 		memset(chip->oob_poi, ~0, mtd->oobsize);
1364 		chip->oob_poi[0] = ((uint8_t *)this->auxiliary_virt)[0];
1365 	}
1366 
1367 	return max_bitflips;
1368 }
1369 
1370 /* Fake a virtual small page for the subpage read */
1371 static int gpmi_ecc_read_subpage(struct nand_chip *chip, uint32_t offs,
1372 				 uint32_t len, uint8_t *buf, int page)
1373 {
1374 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
1375 	struct bch_geometry *geo = &this->bch_geometry;
1376 	int size = chip->ecc.size; /* ECC chunk size */
1377 	int meta, n, page_size;
1378 	unsigned int max_bitflips;
1379 	unsigned int ecc_strength;
1380 	int first, last, marker_pos;
1381 	int ecc_parity_size;
1382 	int col = 0;
1383 	int ret;
1384 
1385 	/* The size of ECC parity */
1386 	ecc_parity_size = geo->gf_len * geo->ecc_strength / 8;
1387 
1388 	/* Align it with the chunk size */
1389 	first = offs / size;
1390 	last = (offs + len - 1) / size;
1391 
1392 	if (this->swap_block_mark) {
1393 		/*
1394 		 * Find the chunk which contains the Block Marker.
1395 		 * If this chunk is in the range of [first, last],
1396 		 * we have to read out the whole page.
1397 		 * Why? since we had swapped the data at the position of Block
1398 		 * Marker to the metadata which is bound with the chunk 0.
1399 		 */
1400 		marker_pos = geo->block_mark_byte_offset / size;
1401 		if (last >= marker_pos && first <= marker_pos) {
1402 			dev_dbg(this->dev,
1403 				"page:%d, first:%d, last:%d, marker at:%d\n",
1404 				page, first, last, marker_pos);
1405 			return gpmi_ecc_read_page(chip, buf, 0, page);
1406 		}
1407 	}
1408 
1409 	meta = geo->metadata_size;
1410 	if (first) {
1411 		col = meta + (size + ecc_parity_size) * first;
1412 		meta = 0;
1413 		buf = buf + first * size;
1414 	}
1415 
1416 	ecc_parity_size = geo->gf_len * geo->ecc_strength / 8;
1417 
1418 	n = last - first + 1;
1419 	page_size = meta + (size + ecc_parity_size) * n;
1420 	ecc_strength = geo->ecc_strength >> 1;
1421 
1422 	this->bch_flashlayout0 = BF_BCH_FLASH0LAYOUT0_NBLOCKS(n - 1) |
1423 		BF_BCH_FLASH0LAYOUT0_META_SIZE(meta) |
1424 		BF_BCH_FLASH0LAYOUT0_ECC0(ecc_strength, this) |
1425 		BF_BCH_FLASH0LAYOUT0_GF(geo->gf_len, this) |
1426 		BF_BCH_FLASH0LAYOUT0_DATA0_SIZE(geo->ecc_chunk_size, this);
1427 
1428 	this->bch_flashlayout1 = BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size) |
1429 		BF_BCH_FLASH0LAYOUT1_ECCN(ecc_strength, this) |
1430 		BF_BCH_FLASH0LAYOUT1_GF(geo->gf_len, this) |
1431 		BF_BCH_FLASH0LAYOUT1_DATAN_SIZE(geo->ecc_chunk_size, this);
1432 
1433 	this->bch = true;
1434 
1435 	ret = nand_read_page_op(chip, page, col, buf, page_size);
1436 	if (ret)
1437 		return ret;
1438 
1439 	dev_dbg(this->dev, "page:%d(%d:%d)%d, chunk:(%d:%d), BCH PG size:%d\n",
1440 		page, offs, len, col, first, n, page_size);
1441 
1442 	max_bitflips = gpmi_count_bitflips(chip, buf, first, last, meta);
1443 
1444 	return max_bitflips;
1445 }
1446 
1447 static int gpmi_ecc_write_page(struct nand_chip *chip, const uint8_t *buf,
1448 			       int oob_required, int page)
1449 {
1450 	struct mtd_info *mtd = nand_to_mtd(chip);
1451 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
1452 	struct bch_geometry *nfc_geo = &this->bch_geometry;
1453 
1454 	dev_dbg(this->dev, "ecc write page.\n");
1455 
1456 	gpmi_bch_layout_std(this);
1457 	this->bch = true;
1458 
1459 	memcpy(this->auxiliary_virt, chip->oob_poi, nfc_geo->auxiliary_size);
1460 
1461 	if (this->swap_block_mark) {
1462 		/*
1463 		 * When doing bad block marker swapping we must always copy the
1464 		 * input buffer as we can't modify the const buffer.
1465 		 */
1466 		memcpy(this->data_buffer_dma, buf, mtd->writesize);
1467 		buf = this->data_buffer_dma;
1468 		block_mark_swapping(this, this->data_buffer_dma,
1469 				    this->auxiliary_virt);
1470 	}
1471 
1472 	return nand_prog_page_op(chip, page, 0, buf, nfc_geo->page_size);
1473 }
1474 
1475 /*
1476  * There are several places in this driver where we have to handle the OOB and
1477  * block marks. This is the function where things are the most complicated, so
1478  * this is where we try to explain it all. All the other places refer back to
1479  * here.
1480  *
1481  * These are the rules, in order of decreasing importance:
1482  *
1483  * 1) Nothing the caller does can be allowed to imperil the block mark.
1484  *
1485  * 2) In read operations, the first byte of the OOB we return must reflect the
1486  *    true state of the block mark, no matter where that block mark appears in
1487  *    the physical page.
1488  *
1489  * 3) ECC-based read operations return an OOB full of set bits (since we never
1490  *    allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
1491  *    return).
1492  *
1493  * 4) "Raw" read operations return a direct view of the physical bytes in the
1494  *    page, using the conventional definition of which bytes are data and which
1495  *    are OOB. This gives the caller a way to see the actual, physical bytes
1496  *    in the page, without the distortions applied by our ECC engine.
1497  *
1498  *
1499  * What we do for this specific read operation depends on two questions:
1500  *
1501  * 1) Are we doing a "raw" read, or an ECC-based read?
1502  *
1503  * 2) Are we using block mark swapping or transcription?
1504  *
1505  * There are four cases, illustrated by the following Karnaugh map:
1506  *
1507  *                    |           Raw           |         ECC-based       |
1508  *       -------------+-------------------------+-------------------------+
1509  *                    | Read the conventional   |                         |
1510  *                    | OOB at the end of the   |                         |
1511  *       Swapping     | page and return it. It  |                         |
1512  *                    | contains exactly what   |                         |
1513  *                    | we want.                | Read the block mark and |
1514  *       -------------+-------------------------+ return it in a buffer   |
1515  *                    | Read the conventional   | full of set bits.       |
1516  *                    | OOB at the end of the   |                         |
1517  *                    | page and also the block |                         |
1518  *       Transcribing | mark in the metadata.   |                         |
1519  *                    | Copy the block mark     |                         |
1520  *                    | into the first byte of  |                         |
1521  *                    | the OOB.                |                         |
1522  *       -------------+-------------------------+-------------------------+
1523  *
1524  * Note that we break rule #4 in the Transcribing/Raw case because we're not
1525  * giving an accurate view of the actual, physical bytes in the page (we're
1526  * overwriting the block mark). That's OK because it's more important to follow
1527  * rule #2.
1528  *
1529  * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
1530  * easy. When reading a page, for example, the NAND Flash MTD code calls our
1531  * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
1532  * ECC-based or raw view of the page is implicit in which function it calls
1533  * (there is a similar pair of ECC-based/raw functions for writing).
1534  */
1535 static int gpmi_ecc_read_oob(struct nand_chip *chip, int page)
1536 {
1537 	struct mtd_info *mtd = nand_to_mtd(chip);
1538 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
1539 	int ret;
1540 
1541 	/* clear the OOB buffer */
1542 	memset(chip->oob_poi, ~0, mtd->oobsize);
1543 
1544 	/* Read out the conventional OOB. */
1545 	ret = nand_read_page_op(chip, page, mtd->writesize, chip->oob_poi,
1546 				mtd->oobsize);
1547 	if (ret)
1548 		return ret;
1549 
1550 	/*
1551 	 * Now, we want to make sure the block mark is correct. In the
1552 	 * non-transcribing case (!GPMI_IS_MX23()), we already have it.
1553 	 * Otherwise, we need to explicitly read it.
1554 	 */
1555 	if (GPMI_IS_MX23(this)) {
1556 		/* Read the block mark into the first byte of the OOB buffer. */
1557 		ret = nand_read_page_op(chip, page, 0, chip->oob_poi, 1);
1558 		if (ret)
1559 			return ret;
1560 	}
1561 
1562 	return 0;
1563 }
1564 
1565 static int gpmi_ecc_write_oob(struct nand_chip *chip, int page)
1566 {
1567 	struct mtd_info *mtd = nand_to_mtd(chip);
1568 	struct mtd_oob_region of = { };
1569 
1570 	/* Do we have available oob area? */
1571 	mtd_ooblayout_free(mtd, 0, &of);
1572 	if (!of.length)
1573 		return -EPERM;
1574 
1575 	if (!nand_is_slc(chip))
1576 		return -EPERM;
1577 
1578 	return nand_prog_page_op(chip, page, mtd->writesize + of.offset,
1579 				 chip->oob_poi + of.offset, of.length);
1580 }
1581 
1582 /*
1583  * This function reads a NAND page without involving the ECC engine (no HW
1584  * ECC correction).
1585  * The tricky part in the GPMI/BCH controller is that it stores ECC bits
1586  * inline (interleaved with payload DATA), and do not align data chunk on
1587  * byte boundaries.
1588  * We thus need to take care moving the payload data and ECC bits stored in the
1589  * page into the provided buffers, which is why we're using nand_extract_bits().
1590  *
1591  * See set_geometry_by_ecc_info inline comments to have a full description
1592  * of the layout used by the GPMI controller.
1593  */
1594 static int gpmi_ecc_read_page_raw(struct nand_chip *chip, uint8_t *buf,
1595 				  int oob_required, int page)
1596 {
1597 	struct mtd_info *mtd = nand_to_mtd(chip);
1598 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
1599 	struct bch_geometry *nfc_geo = &this->bch_geometry;
1600 	int eccsize = nfc_geo->ecc_chunk_size;
1601 	int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
1602 	u8 *tmp_buf = this->raw_buffer;
1603 	size_t src_bit_off;
1604 	size_t oob_bit_off;
1605 	size_t oob_byte_off;
1606 	uint8_t *oob = chip->oob_poi;
1607 	int step;
1608 	int ret;
1609 
1610 	ret = nand_read_page_op(chip, page, 0, tmp_buf,
1611 				mtd->writesize + mtd->oobsize);
1612 	if (ret)
1613 		return ret;
1614 
1615 	/*
1616 	 * If required, swap the bad block marker and the data stored in the
1617 	 * metadata section, so that we don't wrongly consider a block as bad.
1618 	 *
1619 	 * See the layout description for a detailed explanation on why this
1620 	 * is needed.
1621 	 */
1622 	if (this->swap_block_mark)
1623 		swap(tmp_buf[0], tmp_buf[mtd->writesize]);
1624 
1625 	/*
1626 	 * Copy the metadata section into the oob buffer (this section is
1627 	 * guaranteed to be aligned on a byte boundary).
1628 	 */
1629 	if (oob_required)
1630 		memcpy(oob, tmp_buf, nfc_geo->metadata_size);
1631 
1632 	oob_bit_off = nfc_geo->metadata_size * 8;
1633 	src_bit_off = oob_bit_off;
1634 
1635 	/* Extract interleaved payload data and ECC bits */
1636 	for (step = 0; step < nfc_geo->ecc_chunk_count; step++) {
1637 		if (buf)
1638 			nand_extract_bits(buf, step * eccsize * 8, tmp_buf,
1639 					  src_bit_off, eccsize * 8);
1640 		src_bit_off += eccsize * 8;
1641 
1642 		/* Align last ECC block to align a byte boundary */
1643 		if (step == nfc_geo->ecc_chunk_count - 1 &&
1644 		    (oob_bit_off + eccbits) % 8)
1645 			eccbits += 8 - ((oob_bit_off + eccbits) % 8);
1646 
1647 		if (oob_required)
1648 			nand_extract_bits(oob, oob_bit_off, tmp_buf,
1649 					  src_bit_off, eccbits);
1650 
1651 		src_bit_off += eccbits;
1652 		oob_bit_off += eccbits;
1653 	}
1654 
1655 	if (oob_required) {
1656 		oob_byte_off = oob_bit_off / 8;
1657 
1658 		if (oob_byte_off < mtd->oobsize)
1659 			memcpy(oob + oob_byte_off,
1660 			       tmp_buf + mtd->writesize + oob_byte_off,
1661 			       mtd->oobsize - oob_byte_off);
1662 	}
1663 
1664 	return 0;
1665 }
1666 
1667 /*
1668  * This function writes a NAND page without involving the ECC engine (no HW
1669  * ECC generation).
1670  * The tricky part in the GPMI/BCH controller is that it stores ECC bits
1671  * inline (interleaved with payload DATA), and do not align data chunk on
1672  * byte boundaries.
1673  * We thus need to take care moving the OOB area at the right place in the
1674  * final page, which is why we're using nand_extract_bits().
1675  *
1676  * See set_geometry_by_ecc_info inline comments to have a full description
1677  * of the layout used by the GPMI controller.
1678  */
1679 static int gpmi_ecc_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
1680 				   int oob_required, int page)
1681 {
1682 	struct mtd_info *mtd = nand_to_mtd(chip);
1683 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
1684 	struct bch_geometry *nfc_geo = &this->bch_geometry;
1685 	int eccsize = nfc_geo->ecc_chunk_size;
1686 	int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
1687 	u8 *tmp_buf = this->raw_buffer;
1688 	uint8_t *oob = chip->oob_poi;
1689 	size_t dst_bit_off;
1690 	size_t oob_bit_off;
1691 	size_t oob_byte_off;
1692 	int step;
1693 
1694 	/*
1695 	 * Initialize all bits to 1 in case we don't have a buffer for the
1696 	 * payload or oob data in order to leave unspecified bits of data
1697 	 * to their initial state.
1698 	 */
1699 	if (!buf || !oob_required)
1700 		memset(tmp_buf, 0xff, mtd->writesize + mtd->oobsize);
1701 
1702 	/*
1703 	 * First copy the metadata section (stored in oob buffer) at the
1704 	 * beginning of the page, as imposed by the GPMI layout.
1705 	 */
1706 	memcpy(tmp_buf, oob, nfc_geo->metadata_size);
1707 	oob_bit_off = nfc_geo->metadata_size * 8;
1708 	dst_bit_off = oob_bit_off;
1709 
1710 	/* Interleave payload data and ECC bits */
1711 	for (step = 0; step < nfc_geo->ecc_chunk_count; step++) {
1712 		if (buf)
1713 			nand_extract_bits(tmp_buf, dst_bit_off, buf,
1714 					  step * eccsize * 8, eccsize * 8);
1715 		dst_bit_off += eccsize * 8;
1716 
1717 		/* Align last ECC block to align a byte boundary */
1718 		if (step == nfc_geo->ecc_chunk_count - 1 &&
1719 		    (oob_bit_off + eccbits) % 8)
1720 			eccbits += 8 - ((oob_bit_off + eccbits) % 8);
1721 
1722 		if (oob_required)
1723 			nand_extract_bits(tmp_buf, dst_bit_off, oob,
1724 					  oob_bit_off, eccbits);
1725 
1726 		dst_bit_off += eccbits;
1727 		oob_bit_off += eccbits;
1728 	}
1729 
1730 	oob_byte_off = oob_bit_off / 8;
1731 
1732 	if (oob_required && oob_byte_off < mtd->oobsize)
1733 		memcpy(tmp_buf + mtd->writesize + oob_byte_off,
1734 		       oob + oob_byte_off, mtd->oobsize - oob_byte_off);
1735 
1736 	/*
1737 	 * If required, swap the bad block marker and the first byte of the
1738 	 * metadata section, so that we don't modify the bad block marker.
1739 	 *
1740 	 * See the layout description for a detailed explanation on why this
1741 	 * is needed.
1742 	 */
1743 	if (this->swap_block_mark)
1744 		swap(tmp_buf[0], tmp_buf[mtd->writesize]);
1745 
1746 	return nand_prog_page_op(chip, page, 0, tmp_buf,
1747 				 mtd->writesize + mtd->oobsize);
1748 }
1749 
1750 static int gpmi_ecc_read_oob_raw(struct nand_chip *chip, int page)
1751 {
1752 	return gpmi_ecc_read_page_raw(chip, NULL, 1, page);
1753 }
1754 
1755 static int gpmi_ecc_write_oob_raw(struct nand_chip *chip, int page)
1756 {
1757 	return gpmi_ecc_write_page_raw(chip, NULL, 1, page);
1758 }
1759 
1760 static int gpmi_block_markbad(struct nand_chip *chip, loff_t ofs)
1761 {
1762 	struct mtd_info *mtd = nand_to_mtd(chip);
1763 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
1764 	int ret = 0;
1765 	uint8_t *block_mark;
1766 	int column, page, chipnr;
1767 
1768 	chipnr = (int)(ofs >> chip->chip_shift);
1769 	nand_select_target(chip, chipnr);
1770 
1771 	column = !GPMI_IS_MX23(this) ? mtd->writesize : 0;
1772 
1773 	/* Write the block mark. */
1774 	block_mark = this->data_buffer_dma;
1775 	block_mark[0] = 0; /* bad block marker */
1776 
1777 	/* Shift to get page */
1778 	page = (int)(ofs >> chip->page_shift);
1779 
1780 	ret = nand_prog_page_op(chip, page, column, block_mark, 1);
1781 
1782 	nand_deselect_target(chip);
1783 
1784 	return ret;
1785 }
1786 
1787 static int nand_boot_set_geometry(struct gpmi_nand_data *this)
1788 {
1789 	struct boot_rom_geometry *geometry = &this->rom_geometry;
1790 
1791 	/*
1792 	 * Set the boot block stride size.
1793 	 *
1794 	 * In principle, we should be reading this from the OTP bits, since
1795 	 * that's where the ROM is going to get it. In fact, we don't have any
1796 	 * way to read the OTP bits, so we go with the default and hope for the
1797 	 * best.
1798 	 */
1799 	geometry->stride_size_in_pages = 64;
1800 
1801 	/*
1802 	 * Set the search area stride exponent.
1803 	 *
1804 	 * In principle, we should be reading this from the OTP bits, since
1805 	 * that's where the ROM is going to get it. In fact, we don't have any
1806 	 * way to read the OTP bits, so we go with the default and hope for the
1807 	 * best.
1808 	 */
1809 	geometry->search_area_stride_exponent = 2;
1810 	return 0;
1811 }
1812 
1813 static const char  *fingerprint = "STMP";
1814 static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
1815 {
1816 	struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1817 	struct device *dev = this->dev;
1818 	struct nand_chip *chip = &this->nand;
1819 	unsigned int search_area_size_in_strides;
1820 	unsigned int stride;
1821 	unsigned int page;
1822 	u8 *buffer = nand_get_data_buf(chip);
1823 	int found_an_ncb_fingerprint = false;
1824 	int ret;
1825 
1826 	/* Compute the number of strides in a search area. */
1827 	search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1828 
1829 	nand_select_target(chip, 0);
1830 
1831 	/*
1832 	 * Loop through the first search area, looking for the NCB fingerprint.
1833 	 */
1834 	dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
1835 
1836 	for (stride = 0; stride < search_area_size_in_strides; stride++) {
1837 		/* Compute the page addresses. */
1838 		page = stride * rom_geo->stride_size_in_pages;
1839 
1840 		dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
1841 
1842 		/*
1843 		 * Read the NCB fingerprint. The fingerprint is four bytes long
1844 		 * and starts in the 12th byte of the page.
1845 		 */
1846 		ret = nand_read_page_op(chip, page, 12, buffer,
1847 					strlen(fingerprint));
1848 		if (ret)
1849 			continue;
1850 
1851 		/* Look for the fingerprint. */
1852 		if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
1853 			found_an_ncb_fingerprint = true;
1854 			break;
1855 		}
1856 
1857 	}
1858 
1859 	nand_deselect_target(chip);
1860 
1861 	if (found_an_ncb_fingerprint)
1862 		dev_dbg(dev, "\tFound a fingerprint\n");
1863 	else
1864 		dev_dbg(dev, "\tNo fingerprint found\n");
1865 	return found_an_ncb_fingerprint;
1866 }
1867 
1868 /* Writes a transcription stamp. */
1869 static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
1870 {
1871 	struct device *dev = this->dev;
1872 	struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1873 	struct nand_chip *chip = &this->nand;
1874 	struct mtd_info *mtd = nand_to_mtd(chip);
1875 	unsigned int block_size_in_pages;
1876 	unsigned int search_area_size_in_strides;
1877 	unsigned int search_area_size_in_pages;
1878 	unsigned int search_area_size_in_blocks;
1879 	unsigned int block;
1880 	unsigned int stride;
1881 	unsigned int page;
1882 	u8 *buffer = nand_get_data_buf(chip);
1883 	int status;
1884 
1885 	/* Compute the search area geometry. */
1886 	block_size_in_pages = mtd->erasesize / mtd->writesize;
1887 	search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1888 	search_area_size_in_pages = search_area_size_in_strides *
1889 					rom_geo->stride_size_in_pages;
1890 	search_area_size_in_blocks =
1891 		  (search_area_size_in_pages + (block_size_in_pages - 1)) /
1892 				    block_size_in_pages;
1893 
1894 	dev_dbg(dev, "Search Area Geometry :\n");
1895 	dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
1896 	dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
1897 	dev_dbg(dev, "\tin Pages  : %u\n", search_area_size_in_pages);
1898 
1899 	nand_select_target(chip, 0);
1900 
1901 	/* Loop over blocks in the first search area, erasing them. */
1902 	dev_dbg(dev, "Erasing the search area...\n");
1903 
1904 	for (block = 0; block < search_area_size_in_blocks; block++) {
1905 		/* Erase this block. */
1906 		dev_dbg(dev, "\tErasing block 0x%x\n", block);
1907 		status = nand_erase_op(chip, block);
1908 		if (status)
1909 			dev_err(dev, "[%s] Erase failed.\n", __func__);
1910 	}
1911 
1912 	/* Write the NCB fingerprint into the page buffer. */
1913 	memset(buffer, ~0, mtd->writesize);
1914 	memcpy(buffer + 12, fingerprint, strlen(fingerprint));
1915 
1916 	/* Loop through the first search area, writing NCB fingerprints. */
1917 	dev_dbg(dev, "Writing NCB fingerprints...\n");
1918 	for (stride = 0; stride < search_area_size_in_strides; stride++) {
1919 		/* Compute the page addresses. */
1920 		page = stride * rom_geo->stride_size_in_pages;
1921 
1922 		/* Write the first page of the current stride. */
1923 		dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
1924 
1925 		status = chip->ecc.write_page_raw(chip, buffer, 0, page);
1926 		if (status)
1927 			dev_err(dev, "[%s] Write failed.\n", __func__);
1928 	}
1929 
1930 	nand_deselect_target(chip);
1931 
1932 	return 0;
1933 }
1934 
1935 static int mx23_boot_init(struct gpmi_nand_data  *this)
1936 {
1937 	struct device *dev = this->dev;
1938 	struct nand_chip *chip = &this->nand;
1939 	struct mtd_info *mtd = nand_to_mtd(chip);
1940 	unsigned int block_count;
1941 	unsigned int block;
1942 	int     chipnr;
1943 	int     page;
1944 	loff_t  byte;
1945 	uint8_t block_mark;
1946 	int     ret = 0;
1947 
1948 	/*
1949 	 * If control arrives here, we can't use block mark swapping, which
1950 	 * means we're forced to use transcription. First, scan for the
1951 	 * transcription stamp. If we find it, then we don't have to do
1952 	 * anything -- the block marks are already transcribed.
1953 	 */
1954 	if (mx23_check_transcription_stamp(this))
1955 		return 0;
1956 
1957 	/*
1958 	 * If control arrives here, we couldn't find a transcription stamp, so
1959 	 * so we presume the block marks are in the conventional location.
1960 	 */
1961 	dev_dbg(dev, "Transcribing bad block marks...\n");
1962 
1963 	/* Compute the number of blocks in the entire medium. */
1964 	block_count = nanddev_eraseblocks_per_target(&chip->base);
1965 
1966 	/*
1967 	 * Loop over all the blocks in the medium, transcribing block marks as
1968 	 * we go.
1969 	 */
1970 	for (block = 0; block < block_count; block++) {
1971 		/*
1972 		 * Compute the chip, page and byte addresses for this block's
1973 		 * conventional mark.
1974 		 */
1975 		chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
1976 		page = block << (chip->phys_erase_shift - chip->page_shift);
1977 		byte = block <<  chip->phys_erase_shift;
1978 
1979 		/* Send the command to read the conventional block mark. */
1980 		nand_select_target(chip, chipnr);
1981 		ret = nand_read_page_op(chip, page, mtd->writesize, &block_mark,
1982 					1);
1983 		nand_deselect_target(chip);
1984 
1985 		if (ret)
1986 			continue;
1987 
1988 		/*
1989 		 * Check if the block is marked bad. If so, we need to mark it
1990 		 * again, but this time the result will be a mark in the
1991 		 * location where we transcribe block marks.
1992 		 */
1993 		if (block_mark != 0xff) {
1994 			dev_dbg(dev, "Transcribing mark in block %u\n", block);
1995 			ret = chip->legacy.block_markbad(chip, byte);
1996 			if (ret)
1997 				dev_err(dev,
1998 					"Failed to mark block bad with ret %d\n",
1999 					ret);
2000 		}
2001 	}
2002 
2003 	/* Write the stamp that indicates we've transcribed the block marks. */
2004 	mx23_write_transcription_stamp(this);
2005 	return 0;
2006 }
2007 
2008 static int nand_boot_init(struct gpmi_nand_data  *this)
2009 {
2010 	nand_boot_set_geometry(this);
2011 
2012 	/* This is ROM arch-specific initilization before the BBT scanning. */
2013 	if (GPMI_IS_MX23(this))
2014 		return mx23_boot_init(this);
2015 	return 0;
2016 }
2017 
2018 static int gpmi_set_geometry(struct gpmi_nand_data *this)
2019 {
2020 	int ret;
2021 
2022 	/* Free the temporary DMA memory for reading ID. */
2023 	gpmi_free_dma_buffer(this);
2024 
2025 	/* Set up the NFC geometry which is used by BCH. */
2026 	ret = bch_set_geometry(this);
2027 	if (ret) {
2028 		dev_err(this->dev, "Error setting BCH geometry : %d\n", ret);
2029 		return ret;
2030 	}
2031 
2032 	/* Alloc the new DMA buffers according to the pagesize and oobsize */
2033 	return gpmi_alloc_dma_buffer(this);
2034 }
2035 
2036 static int gpmi_init_last(struct gpmi_nand_data *this)
2037 {
2038 	struct nand_chip *chip = &this->nand;
2039 	struct mtd_info *mtd = nand_to_mtd(chip);
2040 	struct nand_ecc_ctrl *ecc = &chip->ecc;
2041 	struct bch_geometry *bch_geo = &this->bch_geometry;
2042 	int ret;
2043 
2044 	/* Set up the medium geometry */
2045 	ret = gpmi_set_geometry(this);
2046 	if (ret)
2047 		return ret;
2048 
2049 	/* Init the nand_ecc_ctrl{} */
2050 	ecc->read_page	= gpmi_ecc_read_page;
2051 	ecc->write_page	= gpmi_ecc_write_page;
2052 	ecc->read_oob	= gpmi_ecc_read_oob;
2053 	ecc->write_oob	= gpmi_ecc_write_oob;
2054 	ecc->read_page_raw = gpmi_ecc_read_page_raw;
2055 	ecc->write_page_raw = gpmi_ecc_write_page_raw;
2056 	ecc->read_oob_raw = gpmi_ecc_read_oob_raw;
2057 	ecc->write_oob_raw = gpmi_ecc_write_oob_raw;
2058 	ecc->engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
2059 	ecc->size	= bch_geo->ecc_chunk_size;
2060 	ecc->strength	= bch_geo->ecc_strength;
2061 	mtd_set_ooblayout(mtd, &gpmi_ooblayout_ops);
2062 
2063 	/*
2064 	 * We only enable the subpage read when:
2065 	 *  (1) the chip is imx6, and
2066 	 *  (2) the size of the ECC parity is byte aligned.
2067 	 */
2068 	if (GPMI_IS_MX6(this) &&
2069 		((bch_geo->gf_len * bch_geo->ecc_strength) % 8) == 0) {
2070 		ecc->read_subpage = gpmi_ecc_read_subpage;
2071 		chip->options |= NAND_SUBPAGE_READ;
2072 	}
2073 
2074 	return 0;
2075 }
2076 
2077 static int gpmi_nand_attach_chip(struct nand_chip *chip)
2078 {
2079 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
2080 	int ret;
2081 
2082 	if (chip->bbt_options & NAND_BBT_USE_FLASH) {
2083 		chip->bbt_options |= NAND_BBT_NO_OOB;
2084 
2085 		if (of_property_read_bool(this->dev->of_node,
2086 					  "fsl,no-blockmark-swap"))
2087 			this->swap_block_mark = false;
2088 	}
2089 	dev_dbg(this->dev, "Blockmark swapping %sabled\n",
2090 		this->swap_block_mark ? "en" : "dis");
2091 
2092 	ret = gpmi_init_last(this);
2093 	if (ret)
2094 		return ret;
2095 
2096 	chip->options |= NAND_SKIP_BBTSCAN;
2097 
2098 	return 0;
2099 }
2100 
2101 static struct gpmi_transfer *get_next_transfer(struct gpmi_nand_data *this)
2102 {
2103 	struct gpmi_transfer *transfer = &this->transfers[this->ntransfers];
2104 
2105 	this->ntransfers++;
2106 
2107 	if (this->ntransfers == GPMI_MAX_TRANSFERS)
2108 		return NULL;
2109 
2110 	return transfer;
2111 }
2112 
2113 static struct dma_async_tx_descriptor *gpmi_chain_command(
2114 	struct gpmi_nand_data *this, u8 cmd, const u8 *addr, int naddr)
2115 {
2116 	struct dma_chan *channel = get_dma_chan(this);
2117 	struct dma_async_tx_descriptor *desc;
2118 	struct gpmi_transfer *transfer;
2119 	int chip = this->nand.cur_cs;
2120 	u32 pio[3];
2121 
2122 	/* [1] send out the PIO words */
2123 	pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WRITE)
2124 		| BM_GPMI_CTRL0_WORD_LENGTH
2125 		| BF_GPMI_CTRL0_CS(chip, this)
2126 		| BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
2127 		| BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_CLE)
2128 		| BM_GPMI_CTRL0_ADDRESS_INCREMENT
2129 		| BF_GPMI_CTRL0_XFER_COUNT(naddr + 1);
2130 	pio[1] = 0;
2131 	pio[2] = 0;
2132 	desc = mxs_dmaengine_prep_pio(channel, pio, ARRAY_SIZE(pio),
2133 				      DMA_TRANS_NONE, 0);
2134 	if (!desc)
2135 		return NULL;
2136 
2137 	transfer = get_next_transfer(this);
2138 	if (!transfer)
2139 		return NULL;
2140 
2141 	transfer->cmdbuf[0] = cmd;
2142 	if (naddr)
2143 		memcpy(&transfer->cmdbuf[1], addr, naddr);
2144 
2145 	sg_init_one(&transfer->sgl, transfer->cmdbuf, naddr + 1);
2146 	dma_map_sg(this->dev, &transfer->sgl, 1, DMA_TO_DEVICE);
2147 
2148 	transfer->direction = DMA_TO_DEVICE;
2149 
2150 	desc = dmaengine_prep_slave_sg(channel, &transfer->sgl, 1, DMA_MEM_TO_DEV,
2151 				       MXS_DMA_CTRL_WAIT4END);
2152 	return desc;
2153 }
2154 
2155 static struct dma_async_tx_descriptor *gpmi_chain_wait_ready(
2156 	struct gpmi_nand_data *this)
2157 {
2158 	struct dma_chan *channel = get_dma_chan(this);
2159 	u32 pio[2];
2160 
2161 	pio[0] =  BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY)
2162 		| BM_GPMI_CTRL0_WORD_LENGTH
2163 		| BF_GPMI_CTRL0_CS(this->nand.cur_cs, this)
2164 		| BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
2165 		| BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA)
2166 		| BF_GPMI_CTRL0_XFER_COUNT(0);
2167 	pio[1] = 0;
2168 
2169 	return mxs_dmaengine_prep_pio(channel, pio, 2, DMA_TRANS_NONE,
2170 				MXS_DMA_CTRL_WAIT4END | MXS_DMA_CTRL_WAIT4RDY);
2171 }
2172 
2173 static struct dma_async_tx_descriptor *gpmi_chain_data_read(
2174 	struct gpmi_nand_data *this, void *buf, int raw_len, bool *direct)
2175 {
2176 	struct dma_async_tx_descriptor *desc;
2177 	struct dma_chan *channel = get_dma_chan(this);
2178 	struct gpmi_transfer *transfer;
2179 	u32 pio[6] = {};
2180 
2181 	transfer = get_next_transfer(this);
2182 	if (!transfer)
2183 		return NULL;
2184 
2185 	transfer->direction = DMA_FROM_DEVICE;
2186 
2187 	*direct = prepare_data_dma(this, buf, raw_len, &transfer->sgl,
2188 				   DMA_FROM_DEVICE);
2189 
2190 	pio[0] =  BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__READ)
2191 		| BM_GPMI_CTRL0_WORD_LENGTH
2192 		| BF_GPMI_CTRL0_CS(this->nand.cur_cs, this)
2193 		| BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
2194 		| BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA)
2195 		| BF_GPMI_CTRL0_XFER_COUNT(raw_len);
2196 
2197 	if (this->bch) {
2198 		pio[2] =  BM_GPMI_ECCCTRL_ENABLE_ECC
2199 			| BF_GPMI_ECCCTRL_ECC_CMD(BV_GPMI_ECCCTRL_ECC_CMD__BCH_DECODE)
2200 			| BF_GPMI_ECCCTRL_BUFFER_MASK(BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE
2201 				| BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY);
2202 		pio[3] = raw_len;
2203 		pio[4] = transfer->sgl.dma_address;
2204 		pio[5] = this->auxiliary_phys;
2205 	}
2206 
2207 	desc = mxs_dmaengine_prep_pio(channel, pio, ARRAY_SIZE(pio),
2208 				      DMA_TRANS_NONE, 0);
2209 	if (!desc)
2210 		return NULL;
2211 
2212 	if (!this->bch)
2213 		desc = dmaengine_prep_slave_sg(channel, &transfer->sgl, 1,
2214 					     DMA_DEV_TO_MEM,
2215 					     MXS_DMA_CTRL_WAIT4END);
2216 
2217 	return desc;
2218 }
2219 
2220 static struct dma_async_tx_descriptor *gpmi_chain_data_write(
2221 	struct gpmi_nand_data *this, const void *buf, int raw_len)
2222 {
2223 	struct dma_chan *channel = get_dma_chan(this);
2224 	struct dma_async_tx_descriptor *desc;
2225 	struct gpmi_transfer *transfer;
2226 	u32 pio[6] = {};
2227 
2228 	transfer = get_next_transfer(this);
2229 	if (!transfer)
2230 		return NULL;
2231 
2232 	transfer->direction = DMA_TO_DEVICE;
2233 
2234 	prepare_data_dma(this, buf, raw_len, &transfer->sgl, DMA_TO_DEVICE);
2235 
2236 	pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WRITE)
2237 		| BM_GPMI_CTRL0_WORD_LENGTH
2238 		| BF_GPMI_CTRL0_CS(this->nand.cur_cs, this)
2239 		| BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
2240 		| BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA)
2241 		| BF_GPMI_CTRL0_XFER_COUNT(raw_len);
2242 
2243 	if (this->bch) {
2244 		pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC
2245 			| BF_GPMI_ECCCTRL_ECC_CMD(BV_GPMI_ECCCTRL_ECC_CMD__BCH_ENCODE)
2246 			| BF_GPMI_ECCCTRL_BUFFER_MASK(BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE |
2247 					BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY);
2248 		pio[3] = raw_len;
2249 		pio[4] = transfer->sgl.dma_address;
2250 		pio[5] = this->auxiliary_phys;
2251 	}
2252 
2253 	desc = mxs_dmaengine_prep_pio(channel, pio, ARRAY_SIZE(pio),
2254 				      DMA_TRANS_NONE,
2255 				      (this->bch ? MXS_DMA_CTRL_WAIT4END : 0));
2256 	if (!desc)
2257 		return NULL;
2258 
2259 	if (!this->bch)
2260 		desc = dmaengine_prep_slave_sg(channel, &transfer->sgl, 1,
2261 					       DMA_MEM_TO_DEV,
2262 					       MXS_DMA_CTRL_WAIT4END);
2263 
2264 	return desc;
2265 }
2266 
2267 static int gpmi_nfc_exec_op(struct nand_chip *chip,
2268 			     const struct nand_operation *op,
2269 			     bool check_only)
2270 {
2271 	const struct nand_op_instr *instr;
2272 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
2273 	struct dma_async_tx_descriptor *desc = NULL;
2274 	int i, ret, buf_len = 0, nbufs = 0;
2275 	u8 cmd = 0;
2276 	void *buf_read = NULL;
2277 	const void *buf_write = NULL;
2278 	bool direct = false;
2279 	struct completion *dma_completion, *bch_completion;
2280 	unsigned long to;
2281 
2282 	if (check_only)
2283 		return 0;
2284 
2285 	this->ntransfers = 0;
2286 	for (i = 0; i < GPMI_MAX_TRANSFERS; i++)
2287 		this->transfers[i].direction = DMA_NONE;
2288 
2289 	ret = pm_runtime_get_sync(this->dev);
2290 	if (ret < 0) {
2291 		pm_runtime_put_noidle(this->dev);
2292 		return ret;
2293 	}
2294 
2295 	/*
2296 	 * This driver currently supports only one NAND chip. Plus, dies share
2297 	 * the same configuration. So once timings have been applied on the
2298 	 * controller side, they will not change anymore. When the time will
2299 	 * come, the check on must_apply_timings will have to be dropped.
2300 	 */
2301 	if (this->hw.must_apply_timings) {
2302 		this->hw.must_apply_timings = false;
2303 		ret = gpmi_nfc_apply_timings(this);
2304 		if (ret)
2305 			goto out_pm;
2306 	}
2307 
2308 	dev_dbg(this->dev, "%s: %d instructions\n", __func__, op->ninstrs);
2309 
2310 	for (i = 0; i < op->ninstrs; i++) {
2311 		instr = &op->instrs[i];
2312 
2313 		nand_op_trace("  ", instr);
2314 
2315 		switch (instr->type) {
2316 		case NAND_OP_WAITRDY_INSTR:
2317 			desc = gpmi_chain_wait_ready(this);
2318 			break;
2319 		case NAND_OP_CMD_INSTR:
2320 			cmd = instr->ctx.cmd.opcode;
2321 
2322 			/*
2323 			 * When this command has an address cycle chain it
2324 			 * together with the address cycle
2325 			 */
2326 			if (i + 1 != op->ninstrs &&
2327 			    op->instrs[i + 1].type == NAND_OP_ADDR_INSTR)
2328 				continue;
2329 
2330 			desc = gpmi_chain_command(this, cmd, NULL, 0);
2331 
2332 			break;
2333 		case NAND_OP_ADDR_INSTR:
2334 			desc = gpmi_chain_command(this, cmd, instr->ctx.addr.addrs,
2335 						  instr->ctx.addr.naddrs);
2336 			break;
2337 		case NAND_OP_DATA_OUT_INSTR:
2338 			buf_write = instr->ctx.data.buf.out;
2339 			buf_len = instr->ctx.data.len;
2340 			nbufs++;
2341 
2342 			desc = gpmi_chain_data_write(this, buf_write, buf_len);
2343 
2344 			break;
2345 		case NAND_OP_DATA_IN_INSTR:
2346 			if (!instr->ctx.data.len)
2347 				break;
2348 			buf_read = instr->ctx.data.buf.in;
2349 			buf_len = instr->ctx.data.len;
2350 			nbufs++;
2351 
2352 			desc = gpmi_chain_data_read(this, buf_read, buf_len,
2353 						   &direct);
2354 			break;
2355 		}
2356 
2357 		if (!desc) {
2358 			ret = -ENXIO;
2359 			goto unmap;
2360 		}
2361 	}
2362 
2363 	dev_dbg(this->dev, "%s setup done\n", __func__);
2364 
2365 	if (nbufs > 1) {
2366 		dev_err(this->dev, "Multiple data instructions not supported\n");
2367 		ret = -EINVAL;
2368 		goto unmap;
2369 	}
2370 
2371 	if (this->bch) {
2372 		writel(this->bch_flashlayout0,
2373 		       this->resources.bch_regs + HW_BCH_FLASH0LAYOUT0);
2374 		writel(this->bch_flashlayout1,
2375 		       this->resources.bch_regs + HW_BCH_FLASH0LAYOUT1);
2376 	}
2377 
2378 	desc->callback = dma_irq_callback;
2379 	desc->callback_param = this;
2380 	dma_completion = &this->dma_done;
2381 	bch_completion = NULL;
2382 
2383 	init_completion(dma_completion);
2384 
2385 	if (this->bch && buf_read) {
2386 		writel(BM_BCH_CTRL_COMPLETE_IRQ_EN,
2387 		       this->resources.bch_regs + HW_BCH_CTRL_SET);
2388 		bch_completion = &this->bch_done;
2389 		init_completion(bch_completion);
2390 	}
2391 
2392 	dmaengine_submit(desc);
2393 	dma_async_issue_pending(get_dma_chan(this));
2394 
2395 	to = wait_for_completion_timeout(dma_completion, msecs_to_jiffies(1000));
2396 	if (!to) {
2397 		dev_err(this->dev, "DMA timeout, last DMA\n");
2398 		gpmi_dump_info(this);
2399 		ret = -ETIMEDOUT;
2400 		goto unmap;
2401 	}
2402 
2403 	if (this->bch && buf_read) {
2404 		to = wait_for_completion_timeout(bch_completion, msecs_to_jiffies(1000));
2405 		if (!to) {
2406 			dev_err(this->dev, "BCH timeout, last DMA\n");
2407 			gpmi_dump_info(this);
2408 			ret = -ETIMEDOUT;
2409 			goto unmap;
2410 		}
2411 	}
2412 
2413 	writel(BM_BCH_CTRL_COMPLETE_IRQ_EN,
2414 	       this->resources.bch_regs + HW_BCH_CTRL_CLR);
2415 	gpmi_clear_bch(this);
2416 
2417 	ret = 0;
2418 
2419 unmap:
2420 	for (i = 0; i < this->ntransfers; i++) {
2421 		struct gpmi_transfer *transfer = &this->transfers[i];
2422 
2423 		if (transfer->direction != DMA_NONE)
2424 			dma_unmap_sg(this->dev, &transfer->sgl, 1,
2425 				     transfer->direction);
2426 	}
2427 
2428 	if (!ret && buf_read && !direct)
2429 		memcpy(buf_read, this->data_buffer_dma,
2430 		       gpmi_raw_len_to_len(this, buf_len));
2431 
2432 	this->bch = false;
2433 
2434 out_pm:
2435 	pm_runtime_mark_last_busy(this->dev);
2436 	pm_runtime_put_autosuspend(this->dev);
2437 
2438 	return ret;
2439 }
2440 
2441 static const struct nand_controller_ops gpmi_nand_controller_ops = {
2442 	.attach_chip = gpmi_nand_attach_chip,
2443 	.setup_interface = gpmi_setup_interface,
2444 	.exec_op = gpmi_nfc_exec_op,
2445 };
2446 
2447 static int gpmi_nand_init(struct gpmi_nand_data *this)
2448 {
2449 	struct nand_chip *chip = &this->nand;
2450 	struct mtd_info  *mtd = nand_to_mtd(chip);
2451 	int ret;
2452 
2453 	/* init the MTD data structures */
2454 	mtd->name		= "gpmi-nand";
2455 	mtd->dev.parent		= this->dev;
2456 
2457 	/* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
2458 	nand_set_controller_data(chip, this);
2459 	nand_set_flash_node(chip, this->pdev->dev.of_node);
2460 	chip->legacy.block_markbad = gpmi_block_markbad;
2461 	chip->badblock_pattern	= &gpmi_bbt_descr;
2462 	chip->options		|= NAND_NO_SUBPAGE_WRITE;
2463 
2464 	/* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
2465 	this->swap_block_mark = !GPMI_IS_MX23(this);
2466 
2467 	/*
2468 	 * Allocate a temporary DMA buffer for reading ID in the
2469 	 * nand_scan_ident().
2470 	 */
2471 	this->bch_geometry.payload_size = 1024;
2472 	this->bch_geometry.auxiliary_size = 128;
2473 	ret = gpmi_alloc_dma_buffer(this);
2474 	if (ret)
2475 		return ret;
2476 
2477 	nand_controller_init(&this->base);
2478 	this->base.ops = &gpmi_nand_controller_ops;
2479 	chip->controller = &this->base;
2480 
2481 	ret = nand_scan(chip, GPMI_IS_MX6(this) ? 2 : 1);
2482 	if (ret)
2483 		goto err_out;
2484 
2485 	ret = nand_boot_init(this);
2486 	if (ret)
2487 		goto err_nand_cleanup;
2488 	ret = nand_create_bbt(chip);
2489 	if (ret)
2490 		goto err_nand_cleanup;
2491 
2492 	ret = mtd_device_register(mtd, NULL, 0);
2493 	if (ret)
2494 		goto err_nand_cleanup;
2495 	return 0;
2496 
2497 err_nand_cleanup:
2498 	nand_cleanup(chip);
2499 err_out:
2500 	gpmi_free_dma_buffer(this);
2501 	return ret;
2502 }
2503 
2504 static const struct of_device_id gpmi_nand_id_table[] = {
2505 	{ .compatible = "fsl,imx23-gpmi-nand", .data = &gpmi_devdata_imx23, },
2506 	{ .compatible = "fsl,imx28-gpmi-nand", .data = &gpmi_devdata_imx28, },
2507 	{ .compatible = "fsl,imx6q-gpmi-nand", .data = &gpmi_devdata_imx6q, },
2508 	{ .compatible = "fsl,imx6sx-gpmi-nand", .data = &gpmi_devdata_imx6sx, },
2509 	{ .compatible = "fsl,imx7d-gpmi-nand", .data = &gpmi_devdata_imx7d,},
2510 	{}
2511 };
2512 MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
2513 
2514 static int gpmi_nand_probe(struct platform_device *pdev)
2515 {
2516 	struct gpmi_nand_data *this;
2517 	int ret;
2518 
2519 	this = devm_kzalloc(&pdev->dev, sizeof(*this), GFP_KERNEL);
2520 	if (!this)
2521 		return -ENOMEM;
2522 
2523 	this->devdata = of_device_get_match_data(&pdev->dev);
2524 	platform_set_drvdata(pdev, this);
2525 	this->pdev  = pdev;
2526 	this->dev   = &pdev->dev;
2527 
2528 	ret = acquire_resources(this);
2529 	if (ret)
2530 		goto exit_acquire_resources;
2531 
2532 	ret = __gpmi_enable_clk(this, true);
2533 	if (ret)
2534 		goto exit_acquire_resources;
2535 
2536 	pm_runtime_set_autosuspend_delay(&pdev->dev, 500);
2537 	pm_runtime_use_autosuspend(&pdev->dev);
2538 	pm_runtime_set_active(&pdev->dev);
2539 	pm_runtime_enable(&pdev->dev);
2540 	pm_runtime_get_sync(&pdev->dev);
2541 
2542 	ret = gpmi_init(this);
2543 	if (ret)
2544 		goto exit_nfc_init;
2545 
2546 	ret = gpmi_nand_init(this);
2547 	if (ret)
2548 		goto exit_nfc_init;
2549 
2550 	pm_runtime_mark_last_busy(&pdev->dev);
2551 	pm_runtime_put_autosuspend(&pdev->dev);
2552 
2553 	dev_info(this->dev, "driver registered.\n");
2554 
2555 	return 0;
2556 
2557 exit_nfc_init:
2558 	pm_runtime_put(&pdev->dev);
2559 	pm_runtime_disable(&pdev->dev);
2560 	release_resources(this);
2561 exit_acquire_resources:
2562 
2563 	return ret;
2564 }
2565 
2566 static int gpmi_nand_remove(struct platform_device *pdev)
2567 {
2568 	struct gpmi_nand_data *this = platform_get_drvdata(pdev);
2569 	struct nand_chip *chip = &this->nand;
2570 	int ret;
2571 
2572 	pm_runtime_put_sync(&pdev->dev);
2573 	pm_runtime_disable(&pdev->dev);
2574 
2575 	ret = mtd_device_unregister(nand_to_mtd(chip));
2576 	WARN_ON(ret);
2577 	nand_cleanup(chip);
2578 	gpmi_free_dma_buffer(this);
2579 	release_resources(this);
2580 	return 0;
2581 }
2582 
2583 #ifdef CONFIG_PM_SLEEP
2584 static int gpmi_pm_suspend(struct device *dev)
2585 {
2586 	struct gpmi_nand_data *this = dev_get_drvdata(dev);
2587 
2588 	release_dma_channels(this);
2589 	return 0;
2590 }
2591 
2592 static int gpmi_pm_resume(struct device *dev)
2593 {
2594 	struct gpmi_nand_data *this = dev_get_drvdata(dev);
2595 	int ret;
2596 
2597 	ret = acquire_dma_channels(this);
2598 	if (ret < 0)
2599 		return ret;
2600 
2601 	/* re-init the GPMI registers */
2602 	ret = gpmi_init(this);
2603 	if (ret) {
2604 		dev_err(this->dev, "Error setting GPMI : %d\n", ret);
2605 		return ret;
2606 	}
2607 
2608 	/* Set flag to get timing setup restored for next exec_op */
2609 	if (this->hw.clk_rate)
2610 		this->hw.must_apply_timings = true;
2611 
2612 	/* re-init the BCH registers */
2613 	ret = bch_set_geometry(this);
2614 	if (ret) {
2615 		dev_err(this->dev, "Error setting BCH : %d\n", ret);
2616 		return ret;
2617 	}
2618 
2619 	return 0;
2620 }
2621 #endif /* CONFIG_PM_SLEEP */
2622 
2623 static int __maybe_unused gpmi_runtime_suspend(struct device *dev)
2624 {
2625 	struct gpmi_nand_data *this = dev_get_drvdata(dev);
2626 
2627 	return __gpmi_enable_clk(this, false);
2628 }
2629 
2630 static int __maybe_unused gpmi_runtime_resume(struct device *dev)
2631 {
2632 	struct gpmi_nand_data *this = dev_get_drvdata(dev);
2633 
2634 	return __gpmi_enable_clk(this, true);
2635 }
2636 
2637 static const struct dev_pm_ops gpmi_pm_ops = {
2638 	SET_SYSTEM_SLEEP_PM_OPS(gpmi_pm_suspend, gpmi_pm_resume)
2639 	SET_RUNTIME_PM_OPS(gpmi_runtime_suspend, gpmi_runtime_resume, NULL)
2640 };
2641 
2642 static struct platform_driver gpmi_nand_driver = {
2643 	.driver = {
2644 		.name = "gpmi-nand",
2645 		.pm = &gpmi_pm_ops,
2646 		.of_match_table = gpmi_nand_id_table,
2647 	},
2648 	.probe   = gpmi_nand_probe,
2649 	.remove  = gpmi_nand_remove,
2650 };
2651 module_platform_driver(gpmi_nand_driver);
2652 
2653 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
2654 MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
2655 MODULE_LICENSE("GPL");
2656