1 /*
2  * Freescale GPMI NAND Flash Driver
3  *
4  * Copyright (C) 2010-2015 Freescale Semiconductor, Inc.
5  * Copyright (C) 2008 Embedded Alley Solutions, Inc.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 #include <linux/clk.h>
22 #include <linux/slab.h>
23 #include <linux/sched/task_stack.h>
24 #include <linux/interrupt.h>
25 #include <linux/module.h>
26 #include <linux/mtd/partitions.h>
27 #include <linux/of.h>
28 #include <linux/of_device.h>
29 #include "gpmi-nand.h"
30 #include "bch-regs.h"
31 
32 /* Resource names for the GPMI NAND driver. */
33 #define GPMI_NAND_GPMI_REGS_ADDR_RES_NAME  "gpmi-nand"
34 #define GPMI_NAND_BCH_REGS_ADDR_RES_NAME   "bch"
35 #define GPMI_NAND_BCH_INTERRUPT_RES_NAME   "bch"
36 
37 /* add our owner bbt descriptor */
38 static uint8_t scan_ff_pattern[] = { 0xff };
39 static struct nand_bbt_descr gpmi_bbt_descr = {
40 	.options	= 0,
41 	.offs		= 0,
42 	.len		= 1,
43 	.pattern	= scan_ff_pattern
44 };
45 
46 /*
47  * We may change the layout if we can get the ECC info from the datasheet,
48  * else we will use all the (page + OOB).
49  */
50 static int gpmi_ooblayout_ecc(struct mtd_info *mtd, int section,
51 			      struct mtd_oob_region *oobregion)
52 {
53 	struct nand_chip *chip = mtd_to_nand(mtd);
54 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
55 	struct bch_geometry *geo = &this->bch_geometry;
56 
57 	if (section)
58 		return -ERANGE;
59 
60 	oobregion->offset = 0;
61 	oobregion->length = geo->page_size - mtd->writesize;
62 
63 	return 0;
64 }
65 
66 static int gpmi_ooblayout_free(struct mtd_info *mtd, int section,
67 			       struct mtd_oob_region *oobregion)
68 {
69 	struct nand_chip *chip = mtd_to_nand(mtd);
70 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
71 	struct bch_geometry *geo = &this->bch_geometry;
72 
73 	if (section)
74 		return -ERANGE;
75 
76 	/* The available oob size we have. */
77 	if (geo->page_size < mtd->writesize + mtd->oobsize) {
78 		oobregion->offset = geo->page_size - mtd->writesize;
79 		oobregion->length = mtd->oobsize - oobregion->offset;
80 	}
81 
82 	return 0;
83 }
84 
85 static const char * const gpmi_clks_for_mx2x[] = {
86 	"gpmi_io",
87 };
88 
89 static const struct mtd_ooblayout_ops gpmi_ooblayout_ops = {
90 	.ecc = gpmi_ooblayout_ecc,
91 	.free = gpmi_ooblayout_free,
92 };
93 
94 static const struct gpmi_devdata gpmi_devdata_imx23 = {
95 	.type = IS_MX23,
96 	.bch_max_ecc_strength = 20,
97 	.max_chain_delay = 16000,
98 	.clks = gpmi_clks_for_mx2x,
99 	.clks_count = ARRAY_SIZE(gpmi_clks_for_mx2x),
100 };
101 
102 static const struct gpmi_devdata gpmi_devdata_imx28 = {
103 	.type = IS_MX28,
104 	.bch_max_ecc_strength = 20,
105 	.max_chain_delay = 16000,
106 	.clks = gpmi_clks_for_mx2x,
107 	.clks_count = ARRAY_SIZE(gpmi_clks_for_mx2x),
108 };
109 
110 static const char * const gpmi_clks_for_mx6[] = {
111 	"gpmi_io", "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch",
112 };
113 
114 static const struct gpmi_devdata gpmi_devdata_imx6q = {
115 	.type = IS_MX6Q,
116 	.bch_max_ecc_strength = 40,
117 	.max_chain_delay = 12000,
118 	.clks = gpmi_clks_for_mx6,
119 	.clks_count = ARRAY_SIZE(gpmi_clks_for_mx6),
120 };
121 
122 static const struct gpmi_devdata gpmi_devdata_imx6sx = {
123 	.type = IS_MX6SX,
124 	.bch_max_ecc_strength = 62,
125 	.max_chain_delay = 12000,
126 	.clks = gpmi_clks_for_mx6,
127 	.clks_count = ARRAY_SIZE(gpmi_clks_for_mx6),
128 };
129 
130 static const char * const gpmi_clks_for_mx7d[] = {
131 	"gpmi_io", "gpmi_bch_apb",
132 };
133 
134 static const struct gpmi_devdata gpmi_devdata_imx7d = {
135 	.type = IS_MX7D,
136 	.bch_max_ecc_strength = 62,
137 	.max_chain_delay = 12000,
138 	.clks = gpmi_clks_for_mx7d,
139 	.clks_count = ARRAY_SIZE(gpmi_clks_for_mx7d),
140 };
141 
142 static irqreturn_t bch_irq(int irq, void *cookie)
143 {
144 	struct gpmi_nand_data *this = cookie;
145 
146 	gpmi_clear_bch(this);
147 	complete(&this->bch_done);
148 	return IRQ_HANDLED;
149 }
150 
151 /*
152  *  Calculate the ECC strength by hand:
153  *	E : The ECC strength.
154  *	G : the length of Galois Field.
155  *	N : The chunk count of per page.
156  *	O : the oobsize of the NAND chip.
157  *	M : the metasize of per page.
158  *
159  *	The formula is :
160  *		E * G * N
161  *	      ------------ <= (O - M)
162  *                  8
163  *
164  *      So, we get E by:
165  *                    (O - M) * 8
166  *              E <= -------------
167  *                       G * N
168  */
169 static inline int get_ecc_strength(struct gpmi_nand_data *this)
170 {
171 	struct bch_geometry *geo = &this->bch_geometry;
172 	struct mtd_info	*mtd = nand_to_mtd(&this->nand);
173 	int ecc_strength;
174 
175 	ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8)
176 			/ (geo->gf_len * geo->ecc_chunk_count);
177 
178 	/* We need the minor even number. */
179 	return round_down(ecc_strength, 2);
180 }
181 
182 static inline bool gpmi_check_ecc(struct gpmi_nand_data *this)
183 {
184 	struct bch_geometry *geo = &this->bch_geometry;
185 
186 	/* Do the sanity check. */
187 	if (GPMI_IS_MX23(this) || GPMI_IS_MX28(this)) {
188 		/* The mx23/mx28 only support the GF13. */
189 		if (geo->gf_len == 14)
190 			return false;
191 	}
192 	return geo->ecc_strength <= this->devdata->bch_max_ecc_strength;
193 }
194 
195 /*
196  * If we can get the ECC information from the nand chip, we do not
197  * need to calculate them ourselves.
198  *
199  * We may have available oob space in this case.
200  */
201 static int set_geometry_by_ecc_info(struct gpmi_nand_data *this)
202 {
203 	struct bch_geometry *geo = &this->bch_geometry;
204 	struct nand_chip *chip = &this->nand;
205 	struct mtd_info *mtd = nand_to_mtd(chip);
206 	unsigned int block_mark_bit_offset;
207 
208 	if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0))
209 		return -EINVAL;
210 
211 	switch (chip->ecc_step_ds) {
212 	case SZ_512:
213 		geo->gf_len = 13;
214 		break;
215 	case SZ_1K:
216 		geo->gf_len = 14;
217 		break;
218 	default:
219 		dev_err(this->dev,
220 			"unsupported nand chip. ecc bits : %d, ecc size : %d\n",
221 			chip->ecc_strength_ds, chip->ecc_step_ds);
222 		return -EINVAL;
223 	}
224 	geo->ecc_chunk_size = chip->ecc_step_ds;
225 	geo->ecc_strength = round_up(chip->ecc_strength_ds, 2);
226 	if (!gpmi_check_ecc(this))
227 		return -EINVAL;
228 
229 	/* Keep the C >= O */
230 	if (geo->ecc_chunk_size < mtd->oobsize) {
231 		dev_err(this->dev,
232 			"unsupported nand chip. ecc size: %d, oob size : %d\n",
233 			chip->ecc_step_ds, mtd->oobsize);
234 		return -EINVAL;
235 	}
236 
237 	/* The default value, see comment in the legacy_set_geometry(). */
238 	geo->metadata_size = 10;
239 
240 	geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
241 
242 	/*
243 	 * Now, the NAND chip with 2K page(data chunk is 512byte) shows below:
244 	 *
245 	 *    |                          P                            |
246 	 *    |<----------------------------------------------------->|
247 	 *    |                                                       |
248 	 *    |                                        (Block Mark)   |
249 	 *    |                      P'                      |      | |     |
250 	 *    |<-------------------------------------------->|  D   | |  O' |
251 	 *    |                                              |<---->| |<--->|
252 	 *    V                                              V      V V     V
253 	 *    +---+----------+-+----------+-+----------+-+----------+-+-----+
254 	 *    | M |   data   |E|   data   |E|   data   |E|   data   |E|     |
255 	 *    +---+----------+-+----------+-+----------+-+----------+-+-----+
256 	 *                                                   ^              ^
257 	 *                                                   |      O       |
258 	 *                                                   |<------------>|
259 	 *                                                   |              |
260 	 *
261 	 *	P : the page size for BCH module.
262 	 *	E : The ECC strength.
263 	 *	G : the length of Galois Field.
264 	 *	N : The chunk count of per page.
265 	 *	M : the metasize of per page.
266 	 *	C : the ecc chunk size, aka the "data" above.
267 	 *	P': the nand chip's page size.
268 	 *	O : the nand chip's oob size.
269 	 *	O': the free oob.
270 	 *
271 	 *	The formula for P is :
272 	 *
273 	 *	            E * G * N
274 	 *	       P = ------------ + P' + M
275 	 *                      8
276 	 *
277 	 * The position of block mark moves forward in the ECC-based view
278 	 * of page, and the delta is:
279 	 *
280 	 *                   E * G * (N - 1)
281 	 *             D = (---------------- + M)
282 	 *                          8
283 	 *
284 	 * Please see the comment in legacy_set_geometry().
285 	 * With the condition C >= O , we still can get same result.
286 	 * So the bit position of the physical block mark within the ECC-based
287 	 * view of the page is :
288 	 *             (P' - D) * 8
289 	 */
290 	geo->page_size = mtd->writesize + geo->metadata_size +
291 		(geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
292 
293 	geo->payload_size = mtd->writesize;
294 
295 	geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4);
296 	geo->auxiliary_size = ALIGN(geo->metadata_size, 4)
297 				+ ALIGN(geo->ecc_chunk_count, 4);
298 
299 	if (!this->swap_block_mark)
300 		return 0;
301 
302 	/* For bit swap. */
303 	block_mark_bit_offset = mtd->writesize * 8 -
304 		(geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
305 				+ geo->metadata_size * 8);
306 
307 	geo->block_mark_byte_offset = block_mark_bit_offset / 8;
308 	geo->block_mark_bit_offset  = block_mark_bit_offset % 8;
309 	return 0;
310 }
311 
312 static int legacy_set_geometry(struct gpmi_nand_data *this)
313 {
314 	struct bch_geometry *geo = &this->bch_geometry;
315 	struct mtd_info *mtd = nand_to_mtd(&this->nand);
316 	unsigned int metadata_size;
317 	unsigned int status_size;
318 	unsigned int block_mark_bit_offset;
319 
320 	/*
321 	 * The size of the metadata can be changed, though we set it to 10
322 	 * bytes now. But it can't be too large, because we have to save
323 	 * enough space for BCH.
324 	 */
325 	geo->metadata_size = 10;
326 
327 	/* The default for the length of Galois Field. */
328 	geo->gf_len = 13;
329 
330 	/* The default for chunk size. */
331 	geo->ecc_chunk_size = 512;
332 	while (geo->ecc_chunk_size < mtd->oobsize) {
333 		geo->ecc_chunk_size *= 2; /* keep C >= O */
334 		geo->gf_len = 14;
335 	}
336 
337 	geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
338 
339 	/* We use the same ECC strength for all chunks. */
340 	geo->ecc_strength = get_ecc_strength(this);
341 	if (!gpmi_check_ecc(this)) {
342 		dev_err(this->dev,
343 			"ecc strength: %d cannot be supported by the controller (%d)\n"
344 			"try to use minimum ecc strength that NAND chip required\n",
345 			geo->ecc_strength,
346 			this->devdata->bch_max_ecc_strength);
347 		return -EINVAL;
348 	}
349 
350 	geo->page_size = mtd->writesize + geo->metadata_size +
351 		(geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
352 	geo->payload_size = mtd->writesize;
353 
354 	/*
355 	 * The auxiliary buffer contains the metadata and the ECC status. The
356 	 * metadata is padded to the nearest 32-bit boundary. The ECC status
357 	 * contains one byte for every ECC chunk, and is also padded to the
358 	 * nearest 32-bit boundary.
359 	 */
360 	metadata_size = ALIGN(geo->metadata_size, 4);
361 	status_size   = ALIGN(geo->ecc_chunk_count, 4);
362 
363 	geo->auxiliary_size = metadata_size + status_size;
364 	geo->auxiliary_status_offset = metadata_size;
365 
366 	if (!this->swap_block_mark)
367 		return 0;
368 
369 	/*
370 	 * We need to compute the byte and bit offsets of
371 	 * the physical block mark within the ECC-based view of the page.
372 	 *
373 	 * NAND chip with 2K page shows below:
374 	 *                                             (Block Mark)
375 	 *                                                   |      |
376 	 *                                                   |  D   |
377 	 *                                                   |<---->|
378 	 *                                                   V      V
379 	 *    +---+----------+-+----------+-+----------+-+----------+-+
380 	 *    | M |   data   |E|   data   |E|   data   |E|   data   |E|
381 	 *    +---+----------+-+----------+-+----------+-+----------+-+
382 	 *
383 	 * The position of block mark moves forward in the ECC-based view
384 	 * of page, and the delta is:
385 	 *
386 	 *                   E * G * (N - 1)
387 	 *             D = (---------------- + M)
388 	 *                          8
389 	 *
390 	 * With the formula to compute the ECC strength, and the condition
391 	 *       : C >= O         (C is the ecc chunk size)
392 	 *
393 	 * It's easy to deduce to the following result:
394 	 *
395 	 *         E * G       (O - M)      C - M         C - M
396 	 *      ----------- <= ------- <=  --------  <  ---------
397 	 *           8            N           N          (N - 1)
398 	 *
399 	 *  So, we get:
400 	 *
401 	 *                   E * G * (N - 1)
402 	 *             D = (---------------- + M) < C
403 	 *                          8
404 	 *
405 	 *  The above inequality means the position of block mark
406 	 *  within the ECC-based view of the page is still in the data chunk,
407 	 *  and it's NOT in the ECC bits of the chunk.
408 	 *
409 	 *  Use the following to compute the bit position of the
410 	 *  physical block mark within the ECC-based view of the page:
411 	 *          (page_size - D) * 8
412 	 *
413 	 *  --Huang Shijie
414 	 */
415 	block_mark_bit_offset = mtd->writesize * 8 -
416 		(geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
417 				+ geo->metadata_size * 8);
418 
419 	geo->block_mark_byte_offset = block_mark_bit_offset / 8;
420 	geo->block_mark_bit_offset  = block_mark_bit_offset % 8;
421 	return 0;
422 }
423 
424 int common_nfc_set_geometry(struct gpmi_nand_data *this)
425 {
426 	if ((of_property_read_bool(this->dev->of_node, "fsl,use-minimum-ecc"))
427 				|| legacy_set_geometry(this))
428 		return set_geometry_by_ecc_info(this);
429 
430 	return 0;
431 }
432 
433 struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
434 {
435 	/* We use the DMA channel 0 to access all the nand chips. */
436 	return this->dma_chans[0];
437 }
438 
439 /* Can we use the upper's buffer directly for DMA? */
440 void prepare_data_dma(struct gpmi_nand_data *this, enum dma_data_direction dr)
441 {
442 	struct scatterlist *sgl = &this->data_sgl;
443 	int ret;
444 
445 	/* first try to map the upper buffer directly */
446 	if (virt_addr_valid(this->upper_buf) &&
447 		!object_is_on_stack(this->upper_buf)) {
448 		sg_init_one(sgl, this->upper_buf, this->upper_len);
449 		ret = dma_map_sg(this->dev, sgl, 1, dr);
450 		if (ret == 0)
451 			goto map_fail;
452 
453 		this->direct_dma_map_ok = true;
454 		return;
455 	}
456 
457 map_fail:
458 	/* We have to use our own DMA buffer. */
459 	sg_init_one(sgl, this->data_buffer_dma, this->upper_len);
460 
461 	if (dr == DMA_TO_DEVICE)
462 		memcpy(this->data_buffer_dma, this->upper_buf, this->upper_len);
463 
464 	dma_map_sg(this->dev, sgl, 1, dr);
465 
466 	this->direct_dma_map_ok = false;
467 }
468 
469 /* This will be called after the DMA operation is finished. */
470 static void dma_irq_callback(void *param)
471 {
472 	struct gpmi_nand_data *this = param;
473 	struct completion *dma_c = &this->dma_done;
474 
475 	switch (this->dma_type) {
476 	case DMA_FOR_COMMAND:
477 		dma_unmap_sg(this->dev, &this->cmd_sgl, 1, DMA_TO_DEVICE);
478 		break;
479 
480 	case DMA_FOR_READ_DATA:
481 		dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_FROM_DEVICE);
482 		if (this->direct_dma_map_ok == false)
483 			memcpy(this->upper_buf, this->data_buffer_dma,
484 				this->upper_len);
485 		break;
486 
487 	case DMA_FOR_WRITE_DATA:
488 		dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_TO_DEVICE);
489 		break;
490 
491 	case DMA_FOR_READ_ECC_PAGE:
492 	case DMA_FOR_WRITE_ECC_PAGE:
493 		/* We have to wait the BCH interrupt to finish. */
494 		break;
495 
496 	default:
497 		dev_err(this->dev, "in wrong DMA operation.\n");
498 	}
499 
500 	complete(dma_c);
501 }
502 
503 int start_dma_without_bch_irq(struct gpmi_nand_data *this,
504 				struct dma_async_tx_descriptor *desc)
505 {
506 	struct completion *dma_c = &this->dma_done;
507 	unsigned long timeout;
508 
509 	init_completion(dma_c);
510 
511 	desc->callback		= dma_irq_callback;
512 	desc->callback_param	= this;
513 	dmaengine_submit(desc);
514 	dma_async_issue_pending(get_dma_chan(this));
515 
516 	/* Wait for the interrupt from the DMA block. */
517 	timeout = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000));
518 	if (!timeout) {
519 		dev_err(this->dev, "DMA timeout, last DMA :%d\n",
520 			this->last_dma_type);
521 		gpmi_dump_info(this);
522 		return -ETIMEDOUT;
523 	}
524 	return 0;
525 }
526 
527 /*
528  * This function is used in BCH reading or BCH writing pages.
529  * It will wait for the BCH interrupt as long as ONE second.
530  * Actually, we must wait for two interrupts :
531  *	[1] firstly the DMA interrupt and
532  *	[2] secondly the BCH interrupt.
533  */
534 int start_dma_with_bch_irq(struct gpmi_nand_data *this,
535 			struct dma_async_tx_descriptor *desc)
536 {
537 	struct completion *bch_c = &this->bch_done;
538 	unsigned long timeout;
539 
540 	/* Prepare to receive an interrupt from the BCH block. */
541 	init_completion(bch_c);
542 
543 	/* start the DMA */
544 	start_dma_without_bch_irq(this, desc);
545 
546 	/* Wait for the interrupt from the BCH block. */
547 	timeout = wait_for_completion_timeout(bch_c, msecs_to_jiffies(1000));
548 	if (!timeout) {
549 		dev_err(this->dev, "BCH timeout, last DMA :%d\n",
550 			this->last_dma_type);
551 		gpmi_dump_info(this);
552 		return -ETIMEDOUT;
553 	}
554 	return 0;
555 }
556 
557 static int acquire_register_block(struct gpmi_nand_data *this,
558 				  const char *res_name)
559 {
560 	struct platform_device *pdev = this->pdev;
561 	struct resources *res = &this->resources;
562 	struct resource *r;
563 	void __iomem *p;
564 
565 	r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name);
566 	p = devm_ioremap_resource(&pdev->dev, r);
567 	if (IS_ERR(p))
568 		return PTR_ERR(p);
569 
570 	if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
571 		res->gpmi_regs = p;
572 	else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
573 		res->bch_regs = p;
574 	else
575 		dev_err(this->dev, "unknown resource name : %s\n", res_name);
576 
577 	return 0;
578 }
579 
580 static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
581 {
582 	struct platform_device *pdev = this->pdev;
583 	const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
584 	struct resource *r;
585 	int err;
586 
587 	r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name);
588 	if (!r) {
589 		dev_err(this->dev, "Can't get resource for %s\n", res_name);
590 		return -ENODEV;
591 	}
592 
593 	err = devm_request_irq(this->dev, r->start, irq_h, 0, res_name, this);
594 	if (err)
595 		dev_err(this->dev, "error requesting BCH IRQ\n");
596 
597 	return err;
598 }
599 
600 static void release_dma_channels(struct gpmi_nand_data *this)
601 {
602 	unsigned int i;
603 	for (i = 0; i < DMA_CHANS; i++)
604 		if (this->dma_chans[i]) {
605 			dma_release_channel(this->dma_chans[i]);
606 			this->dma_chans[i] = NULL;
607 		}
608 }
609 
610 static int acquire_dma_channels(struct gpmi_nand_data *this)
611 {
612 	struct platform_device *pdev = this->pdev;
613 	struct dma_chan *dma_chan;
614 
615 	/* request dma channel */
616 	dma_chan = dma_request_slave_channel(&pdev->dev, "rx-tx");
617 	if (!dma_chan) {
618 		dev_err(this->dev, "Failed to request DMA channel.\n");
619 		goto acquire_err;
620 	}
621 
622 	this->dma_chans[0] = dma_chan;
623 	return 0;
624 
625 acquire_err:
626 	release_dma_channels(this);
627 	return -EINVAL;
628 }
629 
630 static int gpmi_get_clks(struct gpmi_nand_data *this)
631 {
632 	struct resources *r = &this->resources;
633 	struct clk *clk;
634 	int err, i;
635 
636 	for (i = 0; i < this->devdata->clks_count; i++) {
637 		clk = devm_clk_get(this->dev, this->devdata->clks[i]);
638 		if (IS_ERR(clk)) {
639 			err = PTR_ERR(clk);
640 			goto err_clock;
641 		}
642 
643 		r->clock[i] = clk;
644 	}
645 
646 	if (GPMI_IS_MX6(this))
647 		/*
648 		 * Set the default value for the gpmi clock.
649 		 *
650 		 * If you want to use the ONFI nand which is in the
651 		 * Synchronous Mode, you should change the clock as you need.
652 		 */
653 		clk_set_rate(r->clock[0], 22000000);
654 
655 	return 0;
656 
657 err_clock:
658 	dev_dbg(this->dev, "failed in finding the clocks.\n");
659 	return err;
660 }
661 
662 static int acquire_resources(struct gpmi_nand_data *this)
663 {
664 	int ret;
665 
666 	ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
667 	if (ret)
668 		goto exit_regs;
669 
670 	ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
671 	if (ret)
672 		goto exit_regs;
673 
674 	ret = acquire_bch_irq(this, bch_irq);
675 	if (ret)
676 		goto exit_regs;
677 
678 	ret = acquire_dma_channels(this);
679 	if (ret)
680 		goto exit_regs;
681 
682 	ret = gpmi_get_clks(this);
683 	if (ret)
684 		goto exit_clock;
685 	return 0;
686 
687 exit_clock:
688 	release_dma_channels(this);
689 exit_regs:
690 	return ret;
691 }
692 
693 static void release_resources(struct gpmi_nand_data *this)
694 {
695 	release_dma_channels(this);
696 }
697 
698 static int read_page_prepare(struct gpmi_nand_data *this,
699 			void *destination, unsigned length,
700 			void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
701 			void **use_virt, dma_addr_t *use_phys)
702 {
703 	struct device *dev = this->dev;
704 
705 	if (virt_addr_valid(destination)) {
706 		dma_addr_t dest_phys;
707 
708 		dest_phys = dma_map_single(dev, destination,
709 						length, DMA_FROM_DEVICE);
710 		if (dma_mapping_error(dev, dest_phys)) {
711 			if (alt_size < length) {
712 				dev_err(dev, "Alternate buffer is too small\n");
713 				return -ENOMEM;
714 			}
715 			goto map_failed;
716 		}
717 		*use_virt = destination;
718 		*use_phys = dest_phys;
719 		this->direct_dma_map_ok = true;
720 		return 0;
721 	}
722 
723 map_failed:
724 	*use_virt = alt_virt;
725 	*use_phys = alt_phys;
726 	this->direct_dma_map_ok = false;
727 	return 0;
728 }
729 
730 static inline void read_page_end(struct gpmi_nand_data *this,
731 			void *destination, unsigned length,
732 			void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
733 			void *used_virt, dma_addr_t used_phys)
734 {
735 	if (this->direct_dma_map_ok)
736 		dma_unmap_single(this->dev, used_phys, length, DMA_FROM_DEVICE);
737 }
738 
739 static inline void read_page_swap_end(struct gpmi_nand_data *this,
740 			void *destination, unsigned length,
741 			void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
742 			void *used_virt, dma_addr_t used_phys)
743 {
744 	if (!this->direct_dma_map_ok)
745 		memcpy(destination, alt_virt, length);
746 }
747 
748 static int send_page_prepare(struct gpmi_nand_data *this,
749 			const void *source, unsigned length,
750 			void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
751 			const void **use_virt, dma_addr_t *use_phys)
752 {
753 	struct device *dev = this->dev;
754 
755 	if (virt_addr_valid(source)) {
756 		dma_addr_t source_phys;
757 
758 		source_phys = dma_map_single(dev, (void *)source, length,
759 						DMA_TO_DEVICE);
760 		if (dma_mapping_error(dev, source_phys)) {
761 			if (alt_size < length) {
762 				dev_err(dev, "Alternate buffer is too small\n");
763 				return -ENOMEM;
764 			}
765 			goto map_failed;
766 		}
767 		*use_virt = source;
768 		*use_phys = source_phys;
769 		return 0;
770 	}
771 map_failed:
772 	/*
773 	 * Copy the content of the source buffer into the alternate
774 	 * buffer and set up the return values accordingly.
775 	 */
776 	memcpy(alt_virt, source, length);
777 
778 	*use_virt = alt_virt;
779 	*use_phys = alt_phys;
780 	return 0;
781 }
782 
783 static void send_page_end(struct gpmi_nand_data *this,
784 			const void *source, unsigned length,
785 			void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
786 			const void *used_virt, dma_addr_t used_phys)
787 {
788 	struct device *dev = this->dev;
789 	if (used_virt == source)
790 		dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
791 }
792 
793 static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
794 {
795 	struct device *dev = this->dev;
796 
797 	if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
798 		dma_free_coherent(dev, this->page_buffer_size,
799 					this->page_buffer_virt,
800 					this->page_buffer_phys);
801 	kfree(this->cmd_buffer);
802 	kfree(this->data_buffer_dma);
803 	kfree(this->raw_buffer);
804 
805 	this->cmd_buffer	= NULL;
806 	this->data_buffer_dma	= NULL;
807 	this->raw_buffer	= NULL;
808 	this->page_buffer_virt	= NULL;
809 	this->page_buffer_size	=  0;
810 }
811 
812 /* Allocate the DMA buffers */
813 static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
814 {
815 	struct bch_geometry *geo = &this->bch_geometry;
816 	struct device *dev = this->dev;
817 	struct mtd_info *mtd = nand_to_mtd(&this->nand);
818 
819 	/* [1] Allocate a command buffer. PAGE_SIZE is enough. */
820 	this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
821 	if (this->cmd_buffer == NULL)
822 		goto error_alloc;
823 
824 	/*
825 	 * [2] Allocate a read/write data buffer.
826 	 *     The gpmi_alloc_dma_buffer can be called twice.
827 	 *     We allocate a PAGE_SIZE length buffer if gpmi_alloc_dma_buffer
828 	 *     is called before the nand_scan_ident; and we allocate a buffer
829 	 *     of the real NAND page size when the gpmi_alloc_dma_buffer is
830 	 *     called after the nand_scan_ident.
831 	 */
832 	this->data_buffer_dma = kzalloc(mtd->writesize ?: PAGE_SIZE,
833 					GFP_DMA | GFP_KERNEL);
834 	if (this->data_buffer_dma == NULL)
835 		goto error_alloc;
836 
837 	/*
838 	 * [3] Allocate the page buffer.
839 	 *
840 	 * Both the payload buffer and the auxiliary buffer must appear on
841 	 * 32-bit boundaries. We presume the size of the payload buffer is a
842 	 * power of two and is much larger than four, which guarantees the
843 	 * auxiliary buffer will appear on a 32-bit boundary.
844 	 */
845 	this->page_buffer_size = geo->payload_size + geo->auxiliary_size;
846 	this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
847 					&this->page_buffer_phys, GFP_DMA);
848 	if (!this->page_buffer_virt)
849 		goto error_alloc;
850 
851 	this->raw_buffer = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
852 	if (!this->raw_buffer)
853 		goto error_alloc;
854 
855 	/* Slice up the page buffer. */
856 	this->payload_virt = this->page_buffer_virt;
857 	this->payload_phys = this->page_buffer_phys;
858 	this->auxiliary_virt = this->payload_virt + geo->payload_size;
859 	this->auxiliary_phys = this->payload_phys + geo->payload_size;
860 	return 0;
861 
862 error_alloc:
863 	gpmi_free_dma_buffer(this);
864 	return -ENOMEM;
865 }
866 
867 static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
868 {
869 	struct nand_chip *chip = mtd_to_nand(mtd);
870 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
871 	int ret;
872 
873 	/*
874 	 * Every operation begins with a command byte and a series of zero or
875 	 * more address bytes. These are distinguished by either the Address
876 	 * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
877 	 * asserted. When MTD is ready to execute the command, it will deassert
878 	 * both latch enables.
879 	 *
880 	 * Rather than run a separate DMA operation for every single byte, we
881 	 * queue them up and run a single DMA operation for the entire series
882 	 * of command and data bytes. NAND_CMD_NONE means the END of the queue.
883 	 */
884 	if ((ctrl & (NAND_ALE | NAND_CLE))) {
885 		if (data != NAND_CMD_NONE)
886 			this->cmd_buffer[this->command_length++] = data;
887 		return;
888 	}
889 
890 	if (!this->command_length)
891 		return;
892 
893 	ret = gpmi_send_command(this);
894 	if (ret)
895 		dev_err(this->dev, "Chip: %u, Error %d\n",
896 			this->current_chip, ret);
897 
898 	this->command_length = 0;
899 }
900 
901 static int gpmi_dev_ready(struct mtd_info *mtd)
902 {
903 	struct nand_chip *chip = mtd_to_nand(mtd);
904 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
905 
906 	return gpmi_is_ready(this, this->current_chip);
907 }
908 
909 static void gpmi_select_chip(struct mtd_info *mtd, int chipnr)
910 {
911 	struct nand_chip *chip = mtd_to_nand(mtd);
912 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
913 	int ret;
914 
915 	/*
916 	 * For power consumption matters, disable/enable the clock each time a
917 	 * die is selected/unselected.
918 	 */
919 	if (this->current_chip < 0 && chipnr >= 0) {
920 		ret = gpmi_enable_clk(this);
921 		if (ret)
922 			dev_err(this->dev, "Failed to enable the clock\n");
923 	} else if (this->current_chip >= 0 && chipnr < 0) {
924 		ret = gpmi_disable_clk(this);
925 		if (ret)
926 			dev_err(this->dev, "Failed to disable the clock\n");
927 	}
928 
929 	/*
930 	 * This driver currently supports only one NAND chip. Plus, dies share
931 	 * the same configuration. So once timings have been applied on the
932 	 * controller side, they will not change anymore. When the time will
933 	 * come, the check on must_apply_timings will have to be dropped.
934 	 */
935 	if (chipnr >= 0 && this->hw.must_apply_timings) {
936 		this->hw.must_apply_timings = false;
937 		gpmi_nfc_apply_timings(this);
938 	}
939 
940 	this->current_chip = chipnr;
941 }
942 
943 static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
944 {
945 	struct nand_chip *chip = mtd_to_nand(mtd);
946 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
947 
948 	dev_dbg(this->dev, "len is %d\n", len);
949 	this->upper_buf	= buf;
950 	this->upper_len	= len;
951 
952 	gpmi_read_data(this);
953 }
954 
955 static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
956 {
957 	struct nand_chip *chip = mtd_to_nand(mtd);
958 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
959 
960 	dev_dbg(this->dev, "len is %d\n", len);
961 	this->upper_buf	= (uint8_t *)buf;
962 	this->upper_len	= len;
963 
964 	gpmi_send_data(this);
965 }
966 
967 static uint8_t gpmi_read_byte(struct mtd_info *mtd)
968 {
969 	struct nand_chip *chip = mtd_to_nand(mtd);
970 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
971 	uint8_t *buf = this->data_buffer_dma;
972 
973 	gpmi_read_buf(mtd, buf, 1);
974 	return buf[0];
975 }
976 
977 /*
978  * Handles block mark swapping.
979  * It can be called in swapping the block mark, or swapping it back,
980  * because the the operations are the same.
981  */
982 static void block_mark_swapping(struct gpmi_nand_data *this,
983 				void *payload, void *auxiliary)
984 {
985 	struct bch_geometry *nfc_geo = &this->bch_geometry;
986 	unsigned char *p;
987 	unsigned char *a;
988 	unsigned int  bit;
989 	unsigned char mask;
990 	unsigned char from_data;
991 	unsigned char from_oob;
992 
993 	if (!this->swap_block_mark)
994 		return;
995 
996 	/*
997 	 * If control arrives here, we're swapping. Make some convenience
998 	 * variables.
999 	 */
1000 	bit = nfc_geo->block_mark_bit_offset;
1001 	p   = payload + nfc_geo->block_mark_byte_offset;
1002 	a   = auxiliary;
1003 
1004 	/*
1005 	 * Get the byte from the data area that overlays the block mark. Since
1006 	 * the ECC engine applies its own view to the bits in the page, the
1007 	 * physical block mark won't (in general) appear on a byte boundary in
1008 	 * the data.
1009 	 */
1010 	from_data = (p[0] >> bit) | (p[1] << (8 - bit));
1011 
1012 	/* Get the byte from the OOB. */
1013 	from_oob = a[0];
1014 
1015 	/* Swap them. */
1016 	a[0] = from_data;
1017 
1018 	mask = (0x1 << bit) - 1;
1019 	p[0] = (p[0] & mask) | (from_oob << bit);
1020 
1021 	mask = ~0 << bit;
1022 	p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
1023 }
1024 
1025 static int gpmi_ecc_read_page_data(struct nand_chip *chip,
1026 				   uint8_t *buf, int oob_required,
1027 				   int page)
1028 {
1029 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
1030 	struct bch_geometry *nfc_geo = &this->bch_geometry;
1031 	struct mtd_info *mtd = nand_to_mtd(chip);
1032 	void          *payload_virt;
1033 	dma_addr_t    payload_phys;
1034 	void          *auxiliary_virt;
1035 	dma_addr_t    auxiliary_phys;
1036 	unsigned int  i;
1037 	unsigned char *status;
1038 	unsigned int  max_bitflips = 0;
1039 	int           ret;
1040 
1041 	dev_dbg(this->dev, "page number is : %d\n", page);
1042 	ret = read_page_prepare(this, buf, nfc_geo->payload_size,
1043 					this->payload_virt, this->payload_phys,
1044 					nfc_geo->payload_size,
1045 					&payload_virt, &payload_phys);
1046 	if (ret) {
1047 		dev_err(this->dev, "Inadequate DMA buffer\n");
1048 		ret = -ENOMEM;
1049 		return ret;
1050 	}
1051 	auxiliary_virt = this->auxiliary_virt;
1052 	auxiliary_phys = this->auxiliary_phys;
1053 
1054 	/* go! */
1055 	ret = gpmi_read_page(this, payload_phys, auxiliary_phys);
1056 	read_page_end(this, buf, nfc_geo->payload_size,
1057 			this->payload_virt, this->payload_phys,
1058 			nfc_geo->payload_size,
1059 			payload_virt, payload_phys);
1060 	if (ret) {
1061 		dev_err(this->dev, "Error in ECC-based read: %d\n", ret);
1062 		return ret;
1063 	}
1064 
1065 	/* Loop over status bytes, accumulating ECC status. */
1066 	status = auxiliary_virt + nfc_geo->auxiliary_status_offset;
1067 
1068 	read_page_swap_end(this, buf, nfc_geo->payload_size,
1069 			   this->payload_virt, this->payload_phys,
1070 			   nfc_geo->payload_size,
1071 			   payload_virt, payload_phys);
1072 
1073 	for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
1074 		if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
1075 			continue;
1076 
1077 		if (*status == STATUS_UNCORRECTABLE) {
1078 			int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
1079 			u8 *eccbuf = this->raw_buffer;
1080 			int offset, bitoffset;
1081 			int eccbytes;
1082 			int flips;
1083 
1084 			/* Read ECC bytes into our internal raw_buffer */
1085 			offset = nfc_geo->metadata_size * 8;
1086 			offset += ((8 * nfc_geo->ecc_chunk_size) + eccbits) * (i + 1);
1087 			offset -= eccbits;
1088 			bitoffset = offset % 8;
1089 			eccbytes = DIV_ROUND_UP(offset + eccbits, 8);
1090 			offset /= 8;
1091 			eccbytes -= offset;
1092 			nand_change_read_column_op(chip, offset, eccbuf,
1093 						   eccbytes, false);
1094 
1095 			/*
1096 			 * ECC data are not byte aligned and we may have
1097 			 * in-band data in the first and last byte of
1098 			 * eccbuf. Set non-eccbits to one so that
1099 			 * nand_check_erased_ecc_chunk() does not count them
1100 			 * as bitflips.
1101 			 */
1102 			if (bitoffset)
1103 				eccbuf[0] |= GENMASK(bitoffset - 1, 0);
1104 
1105 			bitoffset = (bitoffset + eccbits) % 8;
1106 			if (bitoffset)
1107 				eccbuf[eccbytes - 1] |= GENMASK(7, bitoffset);
1108 
1109 			/*
1110 			 * The ECC hardware has an uncorrectable ECC status
1111 			 * code in case we have bitflips in an erased page. As
1112 			 * nothing was written into this subpage the ECC is
1113 			 * obviously wrong and we can not trust it. We assume
1114 			 * at this point that we are reading an erased page and
1115 			 * try to correct the bitflips in buffer up to
1116 			 * ecc_strength bitflips. If this is a page with random
1117 			 * data, we exceed this number of bitflips and have a
1118 			 * ECC failure. Otherwise we use the corrected buffer.
1119 			 */
1120 			if (i == 0) {
1121 				/* The first block includes metadata */
1122 				flips = nand_check_erased_ecc_chunk(
1123 						buf + i * nfc_geo->ecc_chunk_size,
1124 						nfc_geo->ecc_chunk_size,
1125 						eccbuf, eccbytes,
1126 						auxiliary_virt,
1127 						nfc_geo->metadata_size,
1128 						nfc_geo->ecc_strength);
1129 			} else {
1130 				flips = nand_check_erased_ecc_chunk(
1131 						buf + i * nfc_geo->ecc_chunk_size,
1132 						nfc_geo->ecc_chunk_size,
1133 						eccbuf, eccbytes,
1134 						NULL, 0,
1135 						nfc_geo->ecc_strength);
1136 			}
1137 
1138 			if (flips > 0) {
1139 				max_bitflips = max_t(unsigned int, max_bitflips,
1140 						     flips);
1141 				mtd->ecc_stats.corrected += flips;
1142 				continue;
1143 			}
1144 
1145 			mtd->ecc_stats.failed++;
1146 			continue;
1147 		}
1148 
1149 		mtd->ecc_stats.corrected += *status;
1150 		max_bitflips = max_t(unsigned int, max_bitflips, *status);
1151 	}
1152 
1153 	/* handle the block mark swapping */
1154 	block_mark_swapping(this, buf, auxiliary_virt);
1155 
1156 	if (oob_required) {
1157 		/*
1158 		 * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
1159 		 * for details about our policy for delivering the OOB.
1160 		 *
1161 		 * We fill the caller's buffer with set bits, and then copy the
1162 		 * block mark to th caller's buffer. Note that, if block mark
1163 		 * swapping was necessary, it has already been done, so we can
1164 		 * rely on the first byte of the auxiliary buffer to contain
1165 		 * the block mark.
1166 		 */
1167 		memset(chip->oob_poi, ~0, mtd->oobsize);
1168 		chip->oob_poi[0] = ((uint8_t *) auxiliary_virt)[0];
1169 	}
1170 
1171 	return max_bitflips;
1172 }
1173 
1174 static int gpmi_ecc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1175 			      uint8_t *buf, int oob_required, int page)
1176 {
1177 	nand_read_page_op(chip, page, 0, NULL, 0);
1178 
1179 	return gpmi_ecc_read_page_data(chip, buf, oob_required, page);
1180 }
1181 
1182 /* Fake a virtual small page for the subpage read */
1183 static int gpmi_ecc_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1184 			uint32_t offs, uint32_t len, uint8_t *buf, int page)
1185 {
1186 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
1187 	void __iomem *bch_regs = this->resources.bch_regs;
1188 	struct bch_geometry old_geo = this->bch_geometry;
1189 	struct bch_geometry *geo = &this->bch_geometry;
1190 	int size = chip->ecc.size; /* ECC chunk size */
1191 	int meta, n, page_size;
1192 	u32 r1_old, r2_old, r1_new, r2_new;
1193 	unsigned int max_bitflips;
1194 	int first, last, marker_pos;
1195 	int ecc_parity_size;
1196 	int col = 0;
1197 	int old_swap_block_mark = this->swap_block_mark;
1198 
1199 	/* The size of ECC parity */
1200 	ecc_parity_size = geo->gf_len * geo->ecc_strength / 8;
1201 
1202 	/* Align it with the chunk size */
1203 	first = offs / size;
1204 	last = (offs + len - 1) / size;
1205 
1206 	if (this->swap_block_mark) {
1207 		/*
1208 		 * Find the chunk which contains the Block Marker.
1209 		 * If this chunk is in the range of [first, last],
1210 		 * we have to read out the whole page.
1211 		 * Why? since we had swapped the data at the position of Block
1212 		 * Marker to the metadata which is bound with the chunk 0.
1213 		 */
1214 		marker_pos = geo->block_mark_byte_offset / size;
1215 		if (last >= marker_pos && first <= marker_pos) {
1216 			dev_dbg(this->dev,
1217 				"page:%d, first:%d, last:%d, marker at:%d\n",
1218 				page, first, last, marker_pos);
1219 			return gpmi_ecc_read_page(mtd, chip, buf, 0, page);
1220 		}
1221 	}
1222 
1223 	meta = geo->metadata_size;
1224 	if (first) {
1225 		col = meta + (size + ecc_parity_size) * first;
1226 		meta = 0;
1227 		buf = buf + first * size;
1228 	}
1229 
1230 	nand_read_page_op(chip, page, col, NULL, 0);
1231 
1232 	/* Save the old environment */
1233 	r1_old = r1_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT0);
1234 	r2_old = r2_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT1);
1235 
1236 	/* change the BCH registers and bch_geometry{} */
1237 	n = last - first + 1;
1238 	page_size = meta + (size + ecc_parity_size) * n;
1239 
1240 	r1_new &= ~(BM_BCH_FLASH0LAYOUT0_NBLOCKS |
1241 			BM_BCH_FLASH0LAYOUT0_META_SIZE);
1242 	r1_new |= BF_BCH_FLASH0LAYOUT0_NBLOCKS(n - 1)
1243 			| BF_BCH_FLASH0LAYOUT0_META_SIZE(meta);
1244 	writel(r1_new, bch_regs + HW_BCH_FLASH0LAYOUT0);
1245 
1246 	r2_new &= ~BM_BCH_FLASH0LAYOUT1_PAGE_SIZE;
1247 	r2_new |= BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size);
1248 	writel(r2_new, bch_regs + HW_BCH_FLASH0LAYOUT1);
1249 
1250 	geo->ecc_chunk_count = n;
1251 	geo->payload_size = n * size;
1252 	geo->page_size = page_size;
1253 	geo->auxiliary_status_offset = ALIGN(meta, 4);
1254 
1255 	dev_dbg(this->dev, "page:%d(%d:%d)%d, chunk:(%d:%d), BCH PG size:%d\n",
1256 		page, offs, len, col, first, n, page_size);
1257 
1258 	/* Read the subpage now */
1259 	this->swap_block_mark = false;
1260 	max_bitflips = gpmi_ecc_read_page_data(chip, buf, 0, page);
1261 
1262 	/* Restore */
1263 	writel(r1_old, bch_regs + HW_BCH_FLASH0LAYOUT0);
1264 	writel(r2_old, bch_regs + HW_BCH_FLASH0LAYOUT1);
1265 	this->bch_geometry = old_geo;
1266 	this->swap_block_mark = old_swap_block_mark;
1267 
1268 	return max_bitflips;
1269 }
1270 
1271 static int gpmi_ecc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1272 				const uint8_t *buf, int oob_required, int page)
1273 {
1274 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
1275 	struct bch_geometry *nfc_geo = &this->bch_geometry;
1276 	const void *payload_virt;
1277 	dma_addr_t payload_phys;
1278 	const void *auxiliary_virt;
1279 	dma_addr_t auxiliary_phys;
1280 	int        ret;
1281 
1282 	dev_dbg(this->dev, "ecc write page.\n");
1283 
1284 	nand_prog_page_begin_op(chip, page, 0, NULL, 0);
1285 
1286 	if (this->swap_block_mark) {
1287 		/*
1288 		 * If control arrives here, we're doing block mark swapping.
1289 		 * Since we can't modify the caller's buffers, we must copy them
1290 		 * into our own.
1291 		 */
1292 		memcpy(this->payload_virt, buf, mtd->writesize);
1293 		payload_virt = this->payload_virt;
1294 		payload_phys = this->payload_phys;
1295 
1296 		memcpy(this->auxiliary_virt, chip->oob_poi,
1297 				nfc_geo->auxiliary_size);
1298 		auxiliary_virt = this->auxiliary_virt;
1299 		auxiliary_phys = this->auxiliary_phys;
1300 
1301 		/* Handle block mark swapping. */
1302 		block_mark_swapping(this,
1303 				(void *)payload_virt, (void *)auxiliary_virt);
1304 	} else {
1305 		/*
1306 		 * If control arrives here, we're not doing block mark swapping,
1307 		 * so we can to try and use the caller's buffers.
1308 		 */
1309 		ret = send_page_prepare(this,
1310 				buf, mtd->writesize,
1311 				this->payload_virt, this->payload_phys,
1312 				nfc_geo->payload_size,
1313 				&payload_virt, &payload_phys);
1314 		if (ret) {
1315 			dev_err(this->dev, "Inadequate payload DMA buffer\n");
1316 			return 0;
1317 		}
1318 
1319 		ret = send_page_prepare(this,
1320 				chip->oob_poi, mtd->oobsize,
1321 				this->auxiliary_virt, this->auxiliary_phys,
1322 				nfc_geo->auxiliary_size,
1323 				&auxiliary_virt, &auxiliary_phys);
1324 		if (ret) {
1325 			dev_err(this->dev, "Inadequate auxiliary DMA buffer\n");
1326 			goto exit_auxiliary;
1327 		}
1328 	}
1329 
1330 	/* Ask the NFC. */
1331 	ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
1332 	if (ret)
1333 		dev_err(this->dev, "Error in ECC-based write: %d\n", ret);
1334 
1335 	if (!this->swap_block_mark) {
1336 		send_page_end(this, chip->oob_poi, mtd->oobsize,
1337 				this->auxiliary_virt, this->auxiliary_phys,
1338 				nfc_geo->auxiliary_size,
1339 				auxiliary_virt, auxiliary_phys);
1340 exit_auxiliary:
1341 		send_page_end(this, buf, mtd->writesize,
1342 				this->payload_virt, this->payload_phys,
1343 				nfc_geo->payload_size,
1344 				payload_virt, payload_phys);
1345 	}
1346 
1347 	if (ret)
1348 		return ret;
1349 
1350 	return nand_prog_page_end_op(chip);
1351 }
1352 
1353 /*
1354  * There are several places in this driver where we have to handle the OOB and
1355  * block marks. This is the function where things are the most complicated, so
1356  * this is where we try to explain it all. All the other places refer back to
1357  * here.
1358  *
1359  * These are the rules, in order of decreasing importance:
1360  *
1361  * 1) Nothing the caller does can be allowed to imperil the block mark.
1362  *
1363  * 2) In read operations, the first byte of the OOB we return must reflect the
1364  *    true state of the block mark, no matter where that block mark appears in
1365  *    the physical page.
1366  *
1367  * 3) ECC-based read operations return an OOB full of set bits (since we never
1368  *    allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
1369  *    return).
1370  *
1371  * 4) "Raw" read operations return a direct view of the physical bytes in the
1372  *    page, using the conventional definition of which bytes are data and which
1373  *    are OOB. This gives the caller a way to see the actual, physical bytes
1374  *    in the page, without the distortions applied by our ECC engine.
1375  *
1376  *
1377  * What we do for this specific read operation depends on two questions:
1378  *
1379  * 1) Are we doing a "raw" read, or an ECC-based read?
1380  *
1381  * 2) Are we using block mark swapping or transcription?
1382  *
1383  * There are four cases, illustrated by the following Karnaugh map:
1384  *
1385  *                    |           Raw           |         ECC-based       |
1386  *       -------------+-------------------------+-------------------------+
1387  *                    | Read the conventional   |                         |
1388  *                    | OOB at the end of the   |                         |
1389  *       Swapping     | page and return it. It  |                         |
1390  *                    | contains exactly what   |                         |
1391  *                    | we want.                | Read the block mark and |
1392  *       -------------+-------------------------+ return it in a buffer   |
1393  *                    | Read the conventional   | full of set bits.       |
1394  *                    | OOB at the end of the   |                         |
1395  *                    | page and also the block |                         |
1396  *       Transcribing | mark in the metadata.   |                         |
1397  *                    | Copy the block mark     |                         |
1398  *                    | into the first byte of  |                         |
1399  *                    | the OOB.                |                         |
1400  *       -------------+-------------------------+-------------------------+
1401  *
1402  * Note that we break rule #4 in the Transcribing/Raw case because we're not
1403  * giving an accurate view of the actual, physical bytes in the page (we're
1404  * overwriting the block mark). That's OK because it's more important to follow
1405  * rule #2.
1406  *
1407  * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
1408  * easy. When reading a page, for example, the NAND Flash MTD code calls our
1409  * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
1410  * ECC-based or raw view of the page is implicit in which function it calls
1411  * (there is a similar pair of ECC-based/raw functions for writing).
1412  */
1413 static int gpmi_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1414 				int page)
1415 {
1416 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
1417 
1418 	dev_dbg(this->dev, "page number is %d\n", page);
1419 	/* clear the OOB buffer */
1420 	memset(chip->oob_poi, ~0, mtd->oobsize);
1421 
1422 	/* Read out the conventional OOB. */
1423 	nand_read_page_op(chip, page, mtd->writesize, NULL, 0);
1424 	chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1425 
1426 	/*
1427 	 * Now, we want to make sure the block mark is correct. In the
1428 	 * non-transcribing case (!GPMI_IS_MX23()), we already have it.
1429 	 * Otherwise, we need to explicitly read it.
1430 	 */
1431 	if (GPMI_IS_MX23(this)) {
1432 		/* Read the block mark into the first byte of the OOB buffer. */
1433 		nand_read_page_op(chip, page, 0, NULL, 0);
1434 		chip->oob_poi[0] = chip->read_byte(mtd);
1435 	}
1436 
1437 	return 0;
1438 }
1439 
1440 static int
1441 gpmi_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
1442 {
1443 	struct mtd_oob_region of = { };
1444 
1445 	/* Do we have available oob area? */
1446 	mtd_ooblayout_free(mtd, 0, &of);
1447 	if (!of.length)
1448 		return -EPERM;
1449 
1450 	if (!nand_is_slc(chip))
1451 		return -EPERM;
1452 
1453 	return nand_prog_page_op(chip, page, mtd->writesize + of.offset,
1454 				 chip->oob_poi + of.offset, of.length);
1455 }
1456 
1457 /*
1458  * This function reads a NAND page without involving the ECC engine (no HW
1459  * ECC correction).
1460  * The tricky part in the GPMI/BCH controller is that it stores ECC bits
1461  * inline (interleaved with payload DATA), and do not align data chunk on
1462  * byte boundaries.
1463  * We thus need to take care moving the payload data and ECC bits stored in the
1464  * page into the provided buffers, which is why we're using gpmi_copy_bits.
1465  *
1466  * See set_geometry_by_ecc_info inline comments to have a full description
1467  * of the layout used by the GPMI controller.
1468  */
1469 static int gpmi_ecc_read_page_raw(struct mtd_info *mtd,
1470 				  struct nand_chip *chip, uint8_t *buf,
1471 				  int oob_required, int page)
1472 {
1473 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
1474 	struct bch_geometry *nfc_geo = &this->bch_geometry;
1475 	int eccsize = nfc_geo->ecc_chunk_size;
1476 	int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
1477 	u8 *tmp_buf = this->raw_buffer;
1478 	size_t src_bit_off;
1479 	size_t oob_bit_off;
1480 	size_t oob_byte_off;
1481 	uint8_t *oob = chip->oob_poi;
1482 	int step;
1483 
1484 	nand_read_page_op(chip, page, 0, tmp_buf,
1485 			  mtd->writesize + mtd->oobsize);
1486 
1487 	/*
1488 	 * If required, swap the bad block marker and the data stored in the
1489 	 * metadata section, so that we don't wrongly consider a block as bad.
1490 	 *
1491 	 * See the layout description for a detailed explanation on why this
1492 	 * is needed.
1493 	 */
1494 	if (this->swap_block_mark)
1495 		swap(tmp_buf[0], tmp_buf[mtd->writesize]);
1496 
1497 	/*
1498 	 * Copy the metadata section into the oob buffer (this section is
1499 	 * guaranteed to be aligned on a byte boundary).
1500 	 */
1501 	if (oob_required)
1502 		memcpy(oob, tmp_buf, nfc_geo->metadata_size);
1503 
1504 	oob_bit_off = nfc_geo->metadata_size * 8;
1505 	src_bit_off = oob_bit_off;
1506 
1507 	/* Extract interleaved payload data and ECC bits */
1508 	for (step = 0; step < nfc_geo->ecc_chunk_count; step++) {
1509 		if (buf)
1510 			gpmi_copy_bits(buf, step * eccsize * 8,
1511 				       tmp_buf, src_bit_off,
1512 				       eccsize * 8);
1513 		src_bit_off += eccsize * 8;
1514 
1515 		/* Align last ECC block to align a byte boundary */
1516 		if (step == nfc_geo->ecc_chunk_count - 1 &&
1517 		    (oob_bit_off + eccbits) % 8)
1518 			eccbits += 8 - ((oob_bit_off + eccbits) % 8);
1519 
1520 		if (oob_required)
1521 			gpmi_copy_bits(oob, oob_bit_off,
1522 				       tmp_buf, src_bit_off,
1523 				       eccbits);
1524 
1525 		src_bit_off += eccbits;
1526 		oob_bit_off += eccbits;
1527 	}
1528 
1529 	if (oob_required) {
1530 		oob_byte_off = oob_bit_off / 8;
1531 
1532 		if (oob_byte_off < mtd->oobsize)
1533 			memcpy(oob + oob_byte_off,
1534 			       tmp_buf + mtd->writesize + oob_byte_off,
1535 			       mtd->oobsize - oob_byte_off);
1536 	}
1537 
1538 	return 0;
1539 }
1540 
1541 /*
1542  * This function writes a NAND page without involving the ECC engine (no HW
1543  * ECC generation).
1544  * The tricky part in the GPMI/BCH controller is that it stores ECC bits
1545  * inline (interleaved with payload DATA), and do not align data chunk on
1546  * byte boundaries.
1547  * We thus need to take care moving the OOB area at the right place in the
1548  * final page, which is why we're using gpmi_copy_bits.
1549  *
1550  * See set_geometry_by_ecc_info inline comments to have a full description
1551  * of the layout used by the GPMI controller.
1552  */
1553 static int gpmi_ecc_write_page_raw(struct mtd_info *mtd,
1554 				   struct nand_chip *chip,
1555 				   const uint8_t *buf,
1556 				   int oob_required, int page)
1557 {
1558 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
1559 	struct bch_geometry *nfc_geo = &this->bch_geometry;
1560 	int eccsize = nfc_geo->ecc_chunk_size;
1561 	int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
1562 	u8 *tmp_buf = this->raw_buffer;
1563 	uint8_t *oob = chip->oob_poi;
1564 	size_t dst_bit_off;
1565 	size_t oob_bit_off;
1566 	size_t oob_byte_off;
1567 	int step;
1568 
1569 	/*
1570 	 * Initialize all bits to 1 in case we don't have a buffer for the
1571 	 * payload or oob data in order to leave unspecified bits of data
1572 	 * to their initial state.
1573 	 */
1574 	if (!buf || !oob_required)
1575 		memset(tmp_buf, 0xff, mtd->writesize + mtd->oobsize);
1576 
1577 	/*
1578 	 * First copy the metadata section (stored in oob buffer) at the
1579 	 * beginning of the page, as imposed by the GPMI layout.
1580 	 */
1581 	memcpy(tmp_buf, oob, nfc_geo->metadata_size);
1582 	oob_bit_off = nfc_geo->metadata_size * 8;
1583 	dst_bit_off = oob_bit_off;
1584 
1585 	/* Interleave payload data and ECC bits */
1586 	for (step = 0; step < nfc_geo->ecc_chunk_count; step++) {
1587 		if (buf)
1588 			gpmi_copy_bits(tmp_buf, dst_bit_off,
1589 				       buf, step * eccsize * 8, eccsize * 8);
1590 		dst_bit_off += eccsize * 8;
1591 
1592 		/* Align last ECC block to align a byte boundary */
1593 		if (step == nfc_geo->ecc_chunk_count - 1 &&
1594 		    (oob_bit_off + eccbits) % 8)
1595 			eccbits += 8 - ((oob_bit_off + eccbits) % 8);
1596 
1597 		if (oob_required)
1598 			gpmi_copy_bits(tmp_buf, dst_bit_off,
1599 				       oob, oob_bit_off, eccbits);
1600 
1601 		dst_bit_off += eccbits;
1602 		oob_bit_off += eccbits;
1603 	}
1604 
1605 	oob_byte_off = oob_bit_off / 8;
1606 
1607 	if (oob_required && oob_byte_off < mtd->oobsize)
1608 		memcpy(tmp_buf + mtd->writesize + oob_byte_off,
1609 		       oob + oob_byte_off, mtd->oobsize - oob_byte_off);
1610 
1611 	/*
1612 	 * If required, swap the bad block marker and the first byte of the
1613 	 * metadata section, so that we don't modify the bad block marker.
1614 	 *
1615 	 * See the layout description for a detailed explanation on why this
1616 	 * is needed.
1617 	 */
1618 	if (this->swap_block_mark)
1619 		swap(tmp_buf[0], tmp_buf[mtd->writesize]);
1620 
1621 	return nand_prog_page_op(chip, page, 0, tmp_buf,
1622 				 mtd->writesize + mtd->oobsize);
1623 }
1624 
1625 static int gpmi_ecc_read_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
1626 				 int page)
1627 {
1628 	return gpmi_ecc_read_page_raw(mtd, chip, NULL, 1, page);
1629 }
1630 
1631 static int gpmi_ecc_write_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
1632 				 int page)
1633 {
1634 	return gpmi_ecc_write_page_raw(mtd, chip, NULL, 1, page);
1635 }
1636 
1637 static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs)
1638 {
1639 	struct nand_chip *chip = mtd_to_nand(mtd);
1640 	struct gpmi_nand_data *this = nand_get_controller_data(chip);
1641 	int ret = 0;
1642 	uint8_t *block_mark;
1643 	int column, page, chipnr;
1644 
1645 	chipnr = (int)(ofs >> chip->chip_shift);
1646 	chip->select_chip(mtd, chipnr);
1647 
1648 	column = !GPMI_IS_MX23(this) ? mtd->writesize : 0;
1649 
1650 	/* Write the block mark. */
1651 	block_mark = this->data_buffer_dma;
1652 	block_mark[0] = 0; /* bad block marker */
1653 
1654 	/* Shift to get page */
1655 	page = (int)(ofs >> chip->page_shift);
1656 
1657 	ret = nand_prog_page_op(chip, page, column, block_mark, 1);
1658 
1659 	chip->select_chip(mtd, -1);
1660 
1661 	return ret;
1662 }
1663 
1664 static int nand_boot_set_geometry(struct gpmi_nand_data *this)
1665 {
1666 	struct boot_rom_geometry *geometry = &this->rom_geometry;
1667 
1668 	/*
1669 	 * Set the boot block stride size.
1670 	 *
1671 	 * In principle, we should be reading this from the OTP bits, since
1672 	 * that's where the ROM is going to get it. In fact, we don't have any
1673 	 * way to read the OTP bits, so we go with the default and hope for the
1674 	 * best.
1675 	 */
1676 	geometry->stride_size_in_pages = 64;
1677 
1678 	/*
1679 	 * Set the search area stride exponent.
1680 	 *
1681 	 * In principle, we should be reading this from the OTP bits, since
1682 	 * that's where the ROM is going to get it. In fact, we don't have any
1683 	 * way to read the OTP bits, so we go with the default and hope for the
1684 	 * best.
1685 	 */
1686 	geometry->search_area_stride_exponent = 2;
1687 	return 0;
1688 }
1689 
1690 static const char  *fingerprint = "STMP";
1691 static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
1692 {
1693 	struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1694 	struct device *dev = this->dev;
1695 	struct nand_chip *chip = &this->nand;
1696 	struct mtd_info *mtd = nand_to_mtd(chip);
1697 	unsigned int search_area_size_in_strides;
1698 	unsigned int stride;
1699 	unsigned int page;
1700 	uint8_t *buffer = chip->data_buf;
1701 	int saved_chip_number;
1702 	int found_an_ncb_fingerprint = false;
1703 
1704 	/* Compute the number of strides in a search area. */
1705 	search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1706 
1707 	saved_chip_number = this->current_chip;
1708 	chip->select_chip(mtd, 0);
1709 
1710 	/*
1711 	 * Loop through the first search area, looking for the NCB fingerprint.
1712 	 */
1713 	dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
1714 
1715 	for (stride = 0; stride < search_area_size_in_strides; stride++) {
1716 		/* Compute the page addresses. */
1717 		page = stride * rom_geo->stride_size_in_pages;
1718 
1719 		dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
1720 
1721 		/*
1722 		 * Read the NCB fingerprint. The fingerprint is four bytes long
1723 		 * and starts in the 12th byte of the page.
1724 		 */
1725 		nand_read_page_op(chip, page, 12, NULL, 0);
1726 		chip->read_buf(mtd, buffer, strlen(fingerprint));
1727 
1728 		/* Look for the fingerprint. */
1729 		if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
1730 			found_an_ncb_fingerprint = true;
1731 			break;
1732 		}
1733 
1734 	}
1735 
1736 	chip->select_chip(mtd, saved_chip_number);
1737 
1738 	if (found_an_ncb_fingerprint)
1739 		dev_dbg(dev, "\tFound a fingerprint\n");
1740 	else
1741 		dev_dbg(dev, "\tNo fingerprint found\n");
1742 	return found_an_ncb_fingerprint;
1743 }
1744 
1745 /* Writes a transcription stamp. */
1746 static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
1747 {
1748 	struct device *dev = this->dev;
1749 	struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1750 	struct nand_chip *chip = &this->nand;
1751 	struct mtd_info *mtd = nand_to_mtd(chip);
1752 	unsigned int block_size_in_pages;
1753 	unsigned int search_area_size_in_strides;
1754 	unsigned int search_area_size_in_pages;
1755 	unsigned int search_area_size_in_blocks;
1756 	unsigned int block;
1757 	unsigned int stride;
1758 	unsigned int page;
1759 	uint8_t      *buffer = chip->data_buf;
1760 	int saved_chip_number;
1761 	int status;
1762 
1763 	/* Compute the search area geometry. */
1764 	block_size_in_pages = mtd->erasesize / mtd->writesize;
1765 	search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1766 	search_area_size_in_pages = search_area_size_in_strides *
1767 					rom_geo->stride_size_in_pages;
1768 	search_area_size_in_blocks =
1769 		  (search_area_size_in_pages + (block_size_in_pages - 1)) /
1770 				    block_size_in_pages;
1771 
1772 	dev_dbg(dev, "Search Area Geometry :\n");
1773 	dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
1774 	dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
1775 	dev_dbg(dev, "\tin Pages  : %u\n", search_area_size_in_pages);
1776 
1777 	/* Select chip 0. */
1778 	saved_chip_number = this->current_chip;
1779 	chip->select_chip(mtd, 0);
1780 
1781 	/* Loop over blocks in the first search area, erasing them. */
1782 	dev_dbg(dev, "Erasing the search area...\n");
1783 
1784 	for (block = 0; block < search_area_size_in_blocks; block++) {
1785 		/* Erase this block. */
1786 		dev_dbg(dev, "\tErasing block 0x%x\n", block);
1787 		status = nand_erase_op(chip, block);
1788 		if (status)
1789 			dev_err(dev, "[%s] Erase failed.\n", __func__);
1790 	}
1791 
1792 	/* Write the NCB fingerprint into the page buffer. */
1793 	memset(buffer, ~0, mtd->writesize);
1794 	memcpy(buffer + 12, fingerprint, strlen(fingerprint));
1795 
1796 	/* Loop through the first search area, writing NCB fingerprints. */
1797 	dev_dbg(dev, "Writing NCB fingerprints...\n");
1798 	for (stride = 0; stride < search_area_size_in_strides; stride++) {
1799 		/* Compute the page addresses. */
1800 		page = stride * rom_geo->stride_size_in_pages;
1801 
1802 		/* Write the first page of the current stride. */
1803 		dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
1804 
1805 		status = chip->ecc.write_page_raw(mtd, chip, buffer, 0, page);
1806 		if (status)
1807 			dev_err(dev, "[%s] Write failed.\n", __func__);
1808 	}
1809 
1810 	/* Deselect chip 0. */
1811 	chip->select_chip(mtd, saved_chip_number);
1812 	return 0;
1813 }
1814 
1815 static int mx23_boot_init(struct gpmi_nand_data  *this)
1816 {
1817 	struct device *dev = this->dev;
1818 	struct nand_chip *chip = &this->nand;
1819 	struct mtd_info *mtd = nand_to_mtd(chip);
1820 	unsigned int block_count;
1821 	unsigned int block;
1822 	int     chipnr;
1823 	int     page;
1824 	loff_t  byte;
1825 	uint8_t block_mark;
1826 	int     ret = 0;
1827 
1828 	/*
1829 	 * If control arrives here, we can't use block mark swapping, which
1830 	 * means we're forced to use transcription. First, scan for the
1831 	 * transcription stamp. If we find it, then we don't have to do
1832 	 * anything -- the block marks are already transcribed.
1833 	 */
1834 	if (mx23_check_transcription_stamp(this))
1835 		return 0;
1836 
1837 	/*
1838 	 * If control arrives here, we couldn't find a transcription stamp, so
1839 	 * so we presume the block marks are in the conventional location.
1840 	 */
1841 	dev_dbg(dev, "Transcribing bad block marks...\n");
1842 
1843 	/* Compute the number of blocks in the entire medium. */
1844 	block_count = chip->chipsize >> chip->phys_erase_shift;
1845 
1846 	/*
1847 	 * Loop over all the blocks in the medium, transcribing block marks as
1848 	 * we go.
1849 	 */
1850 	for (block = 0; block < block_count; block++) {
1851 		/*
1852 		 * Compute the chip, page and byte addresses for this block's
1853 		 * conventional mark.
1854 		 */
1855 		chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
1856 		page = block << (chip->phys_erase_shift - chip->page_shift);
1857 		byte = block <<  chip->phys_erase_shift;
1858 
1859 		/* Send the command to read the conventional block mark. */
1860 		chip->select_chip(mtd, chipnr);
1861 		nand_read_page_op(chip, page, mtd->writesize, NULL, 0);
1862 		block_mark = chip->read_byte(mtd);
1863 		chip->select_chip(mtd, -1);
1864 
1865 		/*
1866 		 * Check if the block is marked bad. If so, we need to mark it
1867 		 * again, but this time the result will be a mark in the
1868 		 * location where we transcribe block marks.
1869 		 */
1870 		if (block_mark != 0xff) {
1871 			dev_dbg(dev, "Transcribing mark in block %u\n", block);
1872 			ret = chip->block_markbad(mtd, byte);
1873 			if (ret)
1874 				dev_err(dev,
1875 					"Failed to mark block bad with ret %d\n",
1876 					ret);
1877 		}
1878 	}
1879 
1880 	/* Write the stamp that indicates we've transcribed the block marks. */
1881 	mx23_write_transcription_stamp(this);
1882 	return 0;
1883 }
1884 
1885 static int nand_boot_init(struct gpmi_nand_data  *this)
1886 {
1887 	nand_boot_set_geometry(this);
1888 
1889 	/* This is ROM arch-specific initilization before the BBT scanning. */
1890 	if (GPMI_IS_MX23(this))
1891 		return mx23_boot_init(this);
1892 	return 0;
1893 }
1894 
1895 static int gpmi_set_geometry(struct gpmi_nand_data *this)
1896 {
1897 	int ret;
1898 
1899 	/* Free the temporary DMA memory for reading ID. */
1900 	gpmi_free_dma_buffer(this);
1901 
1902 	/* Set up the NFC geometry which is used by BCH. */
1903 	ret = bch_set_geometry(this);
1904 	if (ret) {
1905 		dev_err(this->dev, "Error setting BCH geometry : %d\n", ret);
1906 		return ret;
1907 	}
1908 
1909 	/* Alloc the new DMA buffers according to the pagesize and oobsize */
1910 	return gpmi_alloc_dma_buffer(this);
1911 }
1912 
1913 static int gpmi_init_last(struct gpmi_nand_data *this)
1914 {
1915 	struct nand_chip *chip = &this->nand;
1916 	struct mtd_info *mtd = nand_to_mtd(chip);
1917 	struct nand_ecc_ctrl *ecc = &chip->ecc;
1918 	struct bch_geometry *bch_geo = &this->bch_geometry;
1919 	int ret;
1920 
1921 	/* Set up the medium geometry */
1922 	ret = gpmi_set_geometry(this);
1923 	if (ret)
1924 		return ret;
1925 
1926 	/* Init the nand_ecc_ctrl{} */
1927 	ecc->read_page	= gpmi_ecc_read_page;
1928 	ecc->write_page	= gpmi_ecc_write_page;
1929 	ecc->read_oob	= gpmi_ecc_read_oob;
1930 	ecc->write_oob	= gpmi_ecc_write_oob;
1931 	ecc->read_page_raw = gpmi_ecc_read_page_raw;
1932 	ecc->write_page_raw = gpmi_ecc_write_page_raw;
1933 	ecc->read_oob_raw = gpmi_ecc_read_oob_raw;
1934 	ecc->write_oob_raw = gpmi_ecc_write_oob_raw;
1935 	ecc->mode	= NAND_ECC_HW;
1936 	ecc->size	= bch_geo->ecc_chunk_size;
1937 	ecc->strength	= bch_geo->ecc_strength;
1938 	mtd_set_ooblayout(mtd, &gpmi_ooblayout_ops);
1939 
1940 	/*
1941 	 * We only enable the subpage read when:
1942 	 *  (1) the chip is imx6, and
1943 	 *  (2) the size of the ECC parity is byte aligned.
1944 	 */
1945 	if (GPMI_IS_MX6(this) &&
1946 		((bch_geo->gf_len * bch_geo->ecc_strength) % 8) == 0) {
1947 		ecc->read_subpage = gpmi_ecc_read_subpage;
1948 		chip->options |= NAND_SUBPAGE_READ;
1949 	}
1950 
1951 	return 0;
1952 }
1953 
1954 static int gpmi_nand_init(struct gpmi_nand_data *this)
1955 {
1956 	struct nand_chip *chip = &this->nand;
1957 	struct mtd_info  *mtd = nand_to_mtd(chip);
1958 	int ret;
1959 
1960 	/* init current chip */
1961 	this->current_chip	= -1;
1962 
1963 	/* init the MTD data structures */
1964 	mtd->name		= "gpmi-nand";
1965 	mtd->dev.parent		= this->dev;
1966 
1967 	/* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
1968 	nand_set_controller_data(chip, this);
1969 	nand_set_flash_node(chip, this->pdev->dev.of_node);
1970 	chip->select_chip	= gpmi_select_chip;
1971 	chip->setup_data_interface = gpmi_setup_data_interface;
1972 	chip->cmd_ctrl		= gpmi_cmd_ctrl;
1973 	chip->dev_ready		= gpmi_dev_ready;
1974 	chip->read_byte		= gpmi_read_byte;
1975 	chip->read_buf		= gpmi_read_buf;
1976 	chip->write_buf		= gpmi_write_buf;
1977 	chip->badblock_pattern	= &gpmi_bbt_descr;
1978 	chip->block_markbad	= gpmi_block_markbad;
1979 	chip->options		|= NAND_NO_SUBPAGE_WRITE;
1980 
1981 	/* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
1982 	this->swap_block_mark = !GPMI_IS_MX23(this);
1983 
1984 	/*
1985 	 * Allocate a temporary DMA buffer for reading ID in the
1986 	 * nand_scan_ident().
1987 	 */
1988 	this->bch_geometry.payload_size = 1024;
1989 	this->bch_geometry.auxiliary_size = 128;
1990 	ret = gpmi_alloc_dma_buffer(this);
1991 	if (ret)
1992 		goto err_out;
1993 
1994 	ret = nand_scan_ident(mtd, GPMI_IS_MX6(this) ? 2 : 1, NULL);
1995 	if (ret)
1996 		goto err_out;
1997 
1998 	if (chip->bbt_options & NAND_BBT_USE_FLASH) {
1999 		chip->bbt_options |= NAND_BBT_NO_OOB;
2000 
2001 		if (of_property_read_bool(this->dev->of_node,
2002 						"fsl,no-blockmark-swap"))
2003 			this->swap_block_mark = false;
2004 	}
2005 	dev_dbg(this->dev, "Blockmark swapping %sabled\n",
2006 		this->swap_block_mark ? "en" : "dis");
2007 
2008 	ret = gpmi_init_last(this);
2009 	if (ret)
2010 		goto err_out;
2011 
2012 	chip->options |= NAND_SKIP_BBTSCAN;
2013 	ret = nand_scan_tail(mtd);
2014 	if (ret)
2015 		goto err_out;
2016 
2017 	ret = nand_boot_init(this);
2018 	if (ret)
2019 		goto err_nand_cleanup;
2020 	ret = chip->scan_bbt(mtd);
2021 	if (ret)
2022 		goto err_nand_cleanup;
2023 
2024 	ret = mtd_device_register(mtd, NULL, 0);
2025 	if (ret)
2026 		goto err_nand_cleanup;
2027 	return 0;
2028 
2029 err_nand_cleanup:
2030 	nand_cleanup(chip);
2031 err_out:
2032 	gpmi_free_dma_buffer(this);
2033 	return ret;
2034 }
2035 
2036 static const struct of_device_id gpmi_nand_id_table[] = {
2037 	{
2038 		.compatible = "fsl,imx23-gpmi-nand",
2039 		.data = &gpmi_devdata_imx23,
2040 	}, {
2041 		.compatible = "fsl,imx28-gpmi-nand",
2042 		.data = &gpmi_devdata_imx28,
2043 	}, {
2044 		.compatible = "fsl,imx6q-gpmi-nand",
2045 		.data = &gpmi_devdata_imx6q,
2046 	}, {
2047 		.compatible = "fsl,imx6sx-gpmi-nand",
2048 		.data = &gpmi_devdata_imx6sx,
2049 	}, {
2050 		.compatible = "fsl,imx7d-gpmi-nand",
2051 		.data = &gpmi_devdata_imx7d,
2052 	}, {}
2053 };
2054 MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
2055 
2056 static int gpmi_nand_probe(struct platform_device *pdev)
2057 {
2058 	struct gpmi_nand_data *this;
2059 	const struct of_device_id *of_id;
2060 	int ret;
2061 
2062 	this = devm_kzalloc(&pdev->dev, sizeof(*this), GFP_KERNEL);
2063 	if (!this)
2064 		return -ENOMEM;
2065 
2066 	of_id = of_match_device(gpmi_nand_id_table, &pdev->dev);
2067 	if (of_id) {
2068 		this->devdata = of_id->data;
2069 	} else {
2070 		dev_err(&pdev->dev, "Failed to find the right device id.\n");
2071 		return -ENODEV;
2072 	}
2073 
2074 	platform_set_drvdata(pdev, this);
2075 	this->pdev  = pdev;
2076 	this->dev   = &pdev->dev;
2077 
2078 	ret = acquire_resources(this);
2079 	if (ret)
2080 		goto exit_acquire_resources;
2081 
2082 	ret = gpmi_init(this);
2083 	if (ret)
2084 		goto exit_nfc_init;
2085 
2086 	ret = gpmi_nand_init(this);
2087 	if (ret)
2088 		goto exit_nfc_init;
2089 
2090 	dev_info(this->dev, "driver registered.\n");
2091 
2092 	return 0;
2093 
2094 exit_nfc_init:
2095 	release_resources(this);
2096 exit_acquire_resources:
2097 
2098 	return ret;
2099 }
2100 
2101 static int gpmi_nand_remove(struct platform_device *pdev)
2102 {
2103 	struct gpmi_nand_data *this = platform_get_drvdata(pdev);
2104 
2105 	nand_release(nand_to_mtd(&this->nand));
2106 	gpmi_free_dma_buffer(this);
2107 	release_resources(this);
2108 	return 0;
2109 }
2110 
2111 #ifdef CONFIG_PM_SLEEP
2112 static int gpmi_pm_suspend(struct device *dev)
2113 {
2114 	struct gpmi_nand_data *this = dev_get_drvdata(dev);
2115 
2116 	release_dma_channels(this);
2117 	return 0;
2118 }
2119 
2120 static int gpmi_pm_resume(struct device *dev)
2121 {
2122 	struct gpmi_nand_data *this = dev_get_drvdata(dev);
2123 	int ret;
2124 
2125 	ret = acquire_dma_channels(this);
2126 	if (ret < 0)
2127 		return ret;
2128 
2129 	/* re-init the GPMI registers */
2130 	ret = gpmi_init(this);
2131 	if (ret) {
2132 		dev_err(this->dev, "Error setting GPMI : %d\n", ret);
2133 		return ret;
2134 	}
2135 
2136 	/* re-init the BCH registers */
2137 	ret = bch_set_geometry(this);
2138 	if (ret) {
2139 		dev_err(this->dev, "Error setting BCH : %d\n", ret);
2140 		return ret;
2141 	}
2142 
2143 	return 0;
2144 }
2145 #endif /* CONFIG_PM_SLEEP */
2146 
2147 static const struct dev_pm_ops gpmi_pm_ops = {
2148 	SET_SYSTEM_SLEEP_PM_OPS(gpmi_pm_suspend, gpmi_pm_resume)
2149 };
2150 
2151 static struct platform_driver gpmi_nand_driver = {
2152 	.driver = {
2153 		.name = "gpmi-nand",
2154 		.pm = &gpmi_pm_ops,
2155 		.of_match_table = gpmi_nand_id_table,
2156 	},
2157 	.probe   = gpmi_nand_probe,
2158 	.remove  = gpmi_nand_remove,
2159 };
2160 module_platform_driver(gpmi_nand_driver);
2161 
2162 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
2163 MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
2164 MODULE_LICENSE("GPL");
2165