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