1
2
3
4
5
6
7
8#include <linux/clk.h>
9#include <linux/delay.h>
10#include <linux/slab.h>
11#include <linux/sched/task_stack.h>
12#include <linux/interrupt.h>
13#include <linux/module.h>
14#include <linux/mtd/partitions.h>
15#include <linux/of.h>
16#include <linux/of_device.h>
17#include <linux/pm_runtime.h>
18#include <linux/dma/mxs-dma.h>
19#include "gpmi-nand.h"
20#include "gpmi-regs.h"
21#include "bch-regs.h"
22
23
24#define GPMI_NAND_GPMI_REGS_ADDR_RES_NAME "gpmi-nand"
25#define GPMI_NAND_BCH_REGS_ADDR_RES_NAME "bch"
26#define GPMI_NAND_BCH_INTERRUPT_RES_NAME "bch"
27
28
29#define TO_CYCLES(duration, period) DIV_ROUND_UP_ULL(duration, period)
30
31#define MXS_SET_ADDR 0x4
32#define MXS_CLR_ADDR 0x8
33
34
35
36
37
38static int clear_poll_bit(void __iomem *addr, u32 mask)
39{
40 int timeout = 0x400;
41
42
43 writel(mask, addr + MXS_CLR_ADDR);
44
45
46
47
48
49 udelay(1);
50
51
52 while ((readl(addr) & mask) && --timeout)
53 ;
54
55 return !timeout;
56}
57
58#define MODULE_CLKGATE (1 << 30)
59#define MODULE_SFTRST (1 << 31)
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77static int gpmi_reset_block(void __iomem *reset_addr, bool just_enable)
78{
79 int ret;
80 int timeout = 0x400;
81
82
83 ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
84 if (unlikely(ret))
85 goto error;
86
87
88 writel(MODULE_CLKGATE, reset_addr + MXS_CLR_ADDR);
89
90 if (!just_enable) {
91
92 writel(MODULE_SFTRST, reset_addr + MXS_SET_ADDR);
93 udelay(1);
94
95
96 while ((!(readl(reset_addr) & MODULE_CLKGATE)) && --timeout)
97 ;
98 if (unlikely(!timeout))
99 goto error;
100 }
101
102
103 ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
104 if (unlikely(ret))
105 goto error;
106
107
108 ret = clear_poll_bit(reset_addr, MODULE_CLKGATE);
109 if (unlikely(ret))
110 goto error;
111
112 return 0;
113
114error:
115 pr_err("%s(%p): module reset timeout\n", __func__, reset_addr);
116 return -ETIMEDOUT;
117}
118
119static int __gpmi_enable_clk(struct gpmi_nand_data *this, bool v)
120{
121 struct clk *clk;
122 int ret;
123 int i;
124
125 for (i = 0; i < GPMI_CLK_MAX; i++) {
126 clk = this->resources.clock[i];
127 if (!clk)
128 break;
129
130 if (v) {
131 ret = clk_prepare_enable(clk);
132 if (ret)
133 goto err_clk;
134 } else {
135 clk_disable_unprepare(clk);
136 }
137 }
138 return 0;
139
140err_clk:
141 for (; i > 0; i--)
142 clk_disable_unprepare(this->resources.clock[i - 1]);
143 return ret;
144}
145
146static int gpmi_init(struct gpmi_nand_data *this)
147{
148 struct resources *r = &this->resources;
149 int ret;
150
151 ret = pm_runtime_get_sync(this->dev);
152 if (ret < 0) {
153 pm_runtime_put_noidle(this->dev);
154 return ret;
155 }
156
157 ret = gpmi_reset_block(r->gpmi_regs, false);
158 if (ret)
159 goto err_out;
160
161
162
163
164
165 ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MXS(this));
166 if (ret)
167 goto err_out;
168
169
170 writel(BM_GPMI_CTRL1_GPMI_MODE, r->gpmi_regs + HW_GPMI_CTRL1_CLR);
171
172
173 writel(BM_GPMI_CTRL1_ATA_IRQRDY_POLARITY,
174 r->gpmi_regs + HW_GPMI_CTRL1_SET);
175
176
177 writel(BM_GPMI_CTRL1_DEV_RESET, r->gpmi_regs + HW_GPMI_CTRL1_SET);
178
179
180 writel(BM_GPMI_CTRL1_BCH_MODE, r->gpmi_regs + HW_GPMI_CTRL1_SET);
181
182
183
184
185
186
187 writel(BM_GPMI_CTRL1_DECOUPLE_CS | BM_GPMI_CTRL1_GANGED_RDYBUSY,
188 r->gpmi_regs + HW_GPMI_CTRL1_SET);
189
190err_out:
191 pm_runtime_mark_last_busy(this->dev);
192 pm_runtime_put_autosuspend(this->dev);
193 return ret;
194}
195
196
197static void gpmi_dump_info(struct gpmi_nand_data *this)
198{
199 struct resources *r = &this->resources;
200 struct bch_geometry *geo = &this->bch_geometry;
201 u32 reg;
202 int i;
203
204 dev_err(this->dev, "Show GPMI registers :\n");
205 for (i = 0; i <= HW_GPMI_DEBUG / 0x10 + 1; i++) {
206 reg = readl(r->gpmi_regs + i * 0x10);
207 dev_err(this->dev, "offset 0x%.3x : 0x%.8x\n", i * 0x10, reg);
208 }
209
210
211 dev_err(this->dev, "Show BCH registers :\n");
212 for (i = 0; i <= HW_BCH_VERSION / 0x10 + 1; i++) {
213 reg = readl(r->bch_regs + i * 0x10);
214 dev_err(this->dev, "offset 0x%.3x : 0x%.8x\n", i * 0x10, reg);
215 }
216 dev_err(this->dev, "BCH Geometry :\n"
217 "GF length : %u\n"
218 "ECC Strength : %u\n"
219 "Page Size in Bytes : %u\n"
220 "Metadata Size in Bytes : %u\n"
221 "ECC0 Chunk Size in Bytes: %u\n"
222 "ECCn Chunk Size in Bytes: %u\n"
223 "ECC Chunk Count : %u\n"
224 "Payload Size in Bytes : %u\n"
225 "Auxiliary Size in Bytes: %u\n"
226 "Auxiliary Status Offset: %u\n"
227 "Block Mark Byte Offset : %u\n"
228 "Block Mark Bit Offset : %u\n",
229 geo->gf_len,
230 geo->ecc_strength,
231 geo->page_size,
232 geo->metadata_size,
233 geo->ecc0_chunk_size,
234 geo->eccn_chunk_size,
235 geo->ecc_chunk_count,
236 geo->payload_size,
237 geo->auxiliary_size,
238 geo->auxiliary_status_offset,
239 geo->block_mark_byte_offset,
240 geo->block_mark_bit_offset);
241}
242
243static bool gpmi_check_ecc(struct gpmi_nand_data *this)
244{
245 struct nand_chip *chip = &this->nand;
246 struct bch_geometry *geo = &this->bch_geometry;
247 struct nand_device *nand = &chip->base;
248 struct nand_ecc_props *conf = &nand->ecc.ctx.conf;
249
250 conf->step_size = geo->eccn_chunk_size;
251 conf->strength = geo->ecc_strength;
252
253
254 if (GPMI_IS_MXS(this)) {
255
256 if (geo->gf_len == 14)
257 return false;
258 }
259
260 if (geo->ecc_strength > this->devdata->bch_max_ecc_strength)
261 return false;
262
263 if (!nand_ecc_is_strong_enough(nand))
264 return false;
265
266 return true;
267}
268
269
270static bool bbm_in_data_chunk(struct gpmi_nand_data *this,
271 unsigned int *chunk_num)
272{
273 struct bch_geometry *geo = &this->bch_geometry;
274 struct nand_chip *chip = &this->nand;
275 struct mtd_info *mtd = nand_to_mtd(chip);
276 unsigned int i, j;
277
278 if (geo->ecc0_chunk_size != geo->eccn_chunk_size) {
279 dev_err(this->dev,
280 "The size of ecc0_chunk must equal to eccn_chunk\n");
281 return false;
282 }
283
284 i = (mtd->writesize * 8 - geo->metadata_size * 8) /
285 (geo->gf_len * geo->ecc_strength +
286 geo->eccn_chunk_size * 8);
287
288 j = (mtd->writesize * 8 - geo->metadata_size * 8) -
289 (geo->gf_len * geo->ecc_strength +
290 geo->eccn_chunk_size * 8) * i;
291
292 if (j < geo->eccn_chunk_size * 8) {
293 *chunk_num = i+1;
294 dev_dbg(this->dev, "Set ecc to %d and bbm in chunk %d\n",
295 geo->ecc_strength, *chunk_num);
296 return true;
297 }
298
299 return false;
300}
301
302
303
304
305
306
307
308static int set_geometry_by_ecc_info(struct gpmi_nand_data *this,
309 unsigned int ecc_strength,
310 unsigned int ecc_step)
311{
312 struct bch_geometry *geo = &this->bch_geometry;
313 struct nand_chip *chip = &this->nand;
314 struct mtd_info *mtd = nand_to_mtd(chip);
315 unsigned int block_mark_bit_offset;
316
317 switch (ecc_step) {
318 case SZ_512:
319 geo->gf_len = 13;
320 break;
321 case SZ_1K:
322 geo->gf_len = 14;
323 break;
324 default:
325 dev_err(this->dev,
326 "unsupported nand chip. ecc bits : %d, ecc size : %d\n",
327 nanddev_get_ecc_requirements(&chip->base)->strength,
328 nanddev_get_ecc_requirements(&chip->base)->step_size);
329 return -EINVAL;
330 }
331 geo->ecc0_chunk_size = ecc_step;
332 geo->eccn_chunk_size = ecc_step;
333 geo->ecc_strength = round_up(ecc_strength, 2);
334 if (!gpmi_check_ecc(this))
335 return -EINVAL;
336
337
338 if (geo->eccn_chunk_size < mtd->oobsize) {
339 dev_err(this->dev,
340 "unsupported nand chip. ecc size: %d, oob size : %d\n",
341 ecc_step, mtd->oobsize);
342 return -EINVAL;
343 }
344
345
346 geo->metadata_size = 10;
347
348 geo->ecc_chunk_count = mtd->writesize / geo->eccn_chunk_size;
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398 geo->page_size = mtd->writesize + geo->metadata_size +
399 (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
400
401 geo->payload_size = mtd->writesize;
402
403 geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4);
404 geo->auxiliary_size = ALIGN(geo->metadata_size, 4)
405 + ALIGN(geo->ecc_chunk_count, 4);
406
407 if (!this->swap_block_mark)
408 return 0;
409
410
411 block_mark_bit_offset = mtd->writesize * 8 -
412 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
413 + geo->metadata_size * 8);
414
415 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
416 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
417 return 0;
418}
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438static inline int get_ecc_strength(struct gpmi_nand_data *this)
439{
440 struct bch_geometry *geo = &this->bch_geometry;
441 struct mtd_info *mtd = nand_to_mtd(&this->nand);
442 int ecc_strength;
443
444 ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8)
445 / (geo->gf_len * geo->ecc_chunk_count);
446
447
448 return round_down(ecc_strength, 2);
449}
450
451static int set_geometry_for_large_oob(struct gpmi_nand_data *this)
452{
453 struct bch_geometry *geo = &this->bch_geometry;
454 struct nand_chip *chip = &this->nand;
455 struct mtd_info *mtd = nand_to_mtd(chip);
456 const struct nand_ecc_props *requirements =
457 nanddev_get_ecc_requirements(&chip->base);
458 unsigned int block_mark_bit_offset;
459 unsigned int max_ecc;
460 unsigned int bbm_chunk;
461 unsigned int i;
462
463
464 if (!(requirements->strength > 0 &&
465 requirements->step_size > 0))
466 return -EINVAL;
467 geo->ecc_strength = requirements->strength;
468
469
470 if (!gpmi_check_ecc(this)) {
471 dev_err(this->dev,
472 "unsupported NAND chip, minimum ecc required %d\n",
473 geo->ecc_strength);
474 return -EINVAL;
475 }
476
477
478 geo->metadata_size = 10;
479 geo->gf_len = 14;
480 geo->ecc0_chunk_size = 1024;
481 geo->eccn_chunk_size = 1024;
482 geo->ecc_chunk_count = mtd->writesize / geo->eccn_chunk_size;
483 max_ecc = min(get_ecc_strength(this),
484 this->devdata->bch_max_ecc_strength);
485
486
487
488
489
490 geo->ecc_strength = max_ecc;
491 while (!(geo->ecc_strength < requirements->strength)) {
492 if (bbm_in_data_chunk(this, &bbm_chunk))
493 goto geo_setting;
494 geo->ecc_strength -= 2;
495 }
496
497
498
499 geo->ecc_strength = requirements->strength;
500
501 geo->ecc0_chunk_size = 0;
502 geo->ecc_chunk_count = (mtd->writesize / geo->eccn_chunk_size) + 1;
503 geo->ecc_for_meta = 1;
504
505 if (mtd->oobsize * 8 < geo->metadata_size * 8 +
506 geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) {
507 dev_err(this->dev, "unsupported NAND chip with new layout\n");
508 return -EINVAL;
509 }
510
511
512 bbm_chunk = (mtd->writesize * 8 - geo->metadata_size * 8 -
513 geo->gf_len * geo->ecc_strength) /
514 (geo->gf_len * geo->ecc_strength +
515 geo->eccn_chunk_size * 8) + 1;
516
517geo_setting:
518
519 geo->page_size = mtd->writesize + geo->metadata_size +
520 (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
521 geo->payload_size = mtd->writesize;
522
523
524
525
526
527
528
529 geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4);
530 geo->auxiliary_size = ALIGN(geo->metadata_size, 4)
531 + ALIGN(geo->ecc_chunk_count, 4);
532
533 if (!this->swap_block_mark)
534 return 0;
535
536
537 i = (mtd->writesize / geo->eccn_chunk_size) - bbm_chunk + 1;
538
539 block_mark_bit_offset = mtd->writesize * 8 -
540 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - i)
541 + geo->metadata_size * 8);
542
543 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
544 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
545
546 dev_dbg(this->dev, "BCH Geometry :\n"
547 "GF length : %u\n"
548 "ECC Strength : %u\n"
549 "Page Size in Bytes : %u\n"
550 "Metadata Size in Bytes : %u\n"
551 "ECC0 Chunk Size in Bytes: %u\n"
552 "ECCn Chunk Size in Bytes: %u\n"
553 "ECC Chunk Count : %u\n"
554 "Payload Size in Bytes : %u\n"
555 "Auxiliary Size in Bytes: %u\n"
556 "Auxiliary Status Offset: %u\n"
557 "Block Mark Byte Offset : %u\n"
558 "Block Mark Bit Offset : %u\n"
559 "Block Mark in chunk : %u\n"
560 "Ecc for Meta data : %u\n",
561 geo->gf_len,
562 geo->ecc_strength,
563 geo->page_size,
564 geo->metadata_size,
565 geo->ecc0_chunk_size,
566 geo->eccn_chunk_size,
567 geo->ecc_chunk_count,
568 geo->payload_size,
569 geo->auxiliary_size,
570 geo->auxiliary_status_offset,
571 geo->block_mark_byte_offset,
572 geo->block_mark_bit_offset,
573 bbm_chunk,
574 geo->ecc_for_meta);
575
576 return 0;
577}
578
579static int legacy_set_geometry(struct gpmi_nand_data *this)
580{
581 struct bch_geometry *geo = &this->bch_geometry;
582 struct mtd_info *mtd = nand_to_mtd(&this->nand);
583 unsigned int metadata_size;
584 unsigned int status_size;
585 unsigned int block_mark_bit_offset;
586
587
588
589
590
591
592 geo->metadata_size = 10;
593
594
595 geo->gf_len = 13;
596
597
598 geo->ecc0_chunk_size = 512;
599 geo->eccn_chunk_size = 512;
600 while (geo->eccn_chunk_size < mtd->oobsize) {
601 geo->ecc0_chunk_size *= 2;
602 geo->eccn_chunk_size *= 2;
603 geo->gf_len = 14;
604 }
605
606 geo->ecc_chunk_count = mtd->writesize / geo->eccn_chunk_size;
607
608
609 geo->ecc_strength = get_ecc_strength(this);
610 if (!gpmi_check_ecc(this)) {
611 dev_err(this->dev,
612 "ecc strength: %d cannot be supported by the controller (%d)\n"
613 "try to use minimum ecc strength that NAND chip required\n",
614 geo->ecc_strength,
615 this->devdata->bch_max_ecc_strength);
616 return -EINVAL;
617 }
618
619 geo->page_size = mtd->writesize + geo->metadata_size +
620 (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
621 geo->payload_size = mtd->writesize;
622
623
624
625
626
627
628
629 metadata_size = ALIGN(geo->metadata_size, 4);
630 status_size = ALIGN(geo->ecc_chunk_count, 4);
631
632 geo->auxiliary_size = metadata_size + status_size;
633 geo->auxiliary_status_offset = metadata_size;
634
635 if (!this->swap_block_mark)
636 return 0;
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684 block_mark_bit_offset = mtd->writesize * 8 -
685 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
686 + geo->metadata_size * 8);
687
688 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
689 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
690 return 0;
691}
692
693static int common_nfc_set_geometry(struct gpmi_nand_data *this)
694{
695 struct nand_chip *chip = &this->nand;
696 struct mtd_info *mtd = nand_to_mtd(&this->nand);
697 const struct nand_ecc_props *requirements =
698 nanddev_get_ecc_requirements(&chip->base);
699 bool use_minimun_ecc;
700 int err;
701
702 use_minimun_ecc = of_property_read_bool(this->dev->of_node,
703 "fsl,use-minimum-ecc");
704
705
706 if ((!use_minimun_ecc && mtd->oobsize < 1024) ||
707 !(requirements->strength > 0 && requirements->step_size > 0)) {
708 dev_dbg(this->dev, "use legacy bch geometry\n");
709 err = legacy_set_geometry(this);
710 if (!err)
711 return 0;
712 }
713
714
715 if (mtd->oobsize > 1024) {
716 dev_dbg(this->dev, "use large oob bch geometry\n");
717 err = set_geometry_for_large_oob(this);
718 if (!err)
719 return 0;
720 }
721
722
723 dev_dbg(this->dev, "use minimum ecc bch geometry\n");
724 err = set_geometry_by_ecc_info(this, requirements->strength,
725 requirements->step_size);
726 if (err)
727 dev_err(this->dev, "none of the bch geometry setting works\n");
728
729 return err;
730}
731
732
733static int bch_set_geometry(struct gpmi_nand_data *this)
734{
735 struct resources *r = &this->resources;
736 int ret;
737
738 ret = common_nfc_set_geometry(this);
739 if (ret)
740 return ret;
741
742 ret = pm_runtime_get_sync(this->dev);
743 if (ret < 0) {
744 pm_runtime_put_autosuspend(this->dev);
745 return ret;
746 }
747
748
749
750
751
752
753 ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MXS(this));
754 if (ret)
755 goto err_out;
756
757
758 writel(0, r->bch_regs + HW_BCH_LAYOUTSELECT);
759
760 ret = 0;
761err_out:
762 pm_runtime_mark_last_busy(this->dev);
763 pm_runtime_put_autosuspend(this->dev);
764
765 return ret;
766}
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842static int gpmi_nfc_compute_timings(struct gpmi_nand_data *this,
843 const struct nand_sdr_timings *sdr)
844{
845 struct gpmi_nfc_hardware_timing *hw = &this->hw;
846 struct resources *r = &this->resources;
847 unsigned int dll_threshold_ps = this->devdata->max_chain_delay;
848 unsigned int period_ps, reference_period_ps;
849 unsigned int data_setup_cycles, data_hold_cycles, addr_setup_cycles;
850 unsigned int tRP_ps;
851 bool use_half_period;
852 int sample_delay_ps, sample_delay_factor;
853 unsigned int busy_timeout_cycles;
854 u8 wrn_dly_sel;
855 unsigned long clk_rate, min_rate;
856 u64 busy_timeout_ps;
857
858 if (sdr->tRC_min >= 30000) {
859
860 hw->clk_rate = 22000000;
861 min_rate = 0;
862 wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_4_TO_8NS;
863 } else if (sdr->tRC_min >= 25000) {
864
865 hw->clk_rate = 80000000;
866 min_rate = 22000000;
867 wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY;
868 } else {
869
870 hw->clk_rate = 100000000;
871 min_rate = 80000000;
872 wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY;
873 }
874
875 clk_rate = clk_round_rate(r->clock[0], hw->clk_rate);
876 if (clk_rate <= min_rate) {
877 dev_err(this->dev, "clock setting: expected %ld, got %ld\n",
878 hw->clk_rate, clk_rate);
879 return -ENOTSUPP;
880 }
881
882 hw->clk_rate = clk_rate;
883
884 period_ps = div_u64((u64)NSEC_PER_SEC * 1000, hw->clk_rate);
885
886 addr_setup_cycles = TO_CYCLES(sdr->tALS_min, period_ps);
887 data_setup_cycles = TO_CYCLES(sdr->tDS_min, period_ps);
888 data_hold_cycles = TO_CYCLES(sdr->tDH_min, period_ps);
889 busy_timeout_ps = max(sdr->tBERS_max, sdr->tPROG_max);
890 busy_timeout_cycles = TO_CYCLES(busy_timeout_ps, period_ps);
891
892 hw->timing0 = BF_GPMI_TIMING0_ADDRESS_SETUP(addr_setup_cycles) |
893 BF_GPMI_TIMING0_DATA_HOLD(data_hold_cycles) |
894 BF_GPMI_TIMING0_DATA_SETUP(data_setup_cycles);
895 hw->timing1 = BF_GPMI_TIMING1_BUSY_TIMEOUT(DIV_ROUND_UP(busy_timeout_cycles, 4096));
896
897
898
899
900
901
902
903
904 if (period_ps > dll_threshold_ps) {
905 use_half_period = true;
906 reference_period_ps = period_ps / 2;
907 } else {
908 use_half_period = false;
909 reference_period_ps = period_ps;
910 }
911
912 tRP_ps = data_setup_cycles * period_ps;
913 sample_delay_ps = (sdr->tREA_max + 4000 - tRP_ps) * 8;
914 if (sample_delay_ps > 0)
915 sample_delay_factor = sample_delay_ps / reference_period_ps;
916 else
917 sample_delay_factor = 0;
918
919 hw->ctrl1n = BF_GPMI_CTRL1_WRN_DLY_SEL(wrn_dly_sel);
920 if (sample_delay_factor)
921 hw->ctrl1n |= BF_GPMI_CTRL1_RDN_DELAY(sample_delay_factor) |
922 BM_GPMI_CTRL1_DLL_ENABLE |
923 (use_half_period ? BM_GPMI_CTRL1_HALF_PERIOD : 0);
924 return 0;
925}
926
927static int gpmi_nfc_apply_timings(struct gpmi_nand_data *this)
928{
929 struct gpmi_nfc_hardware_timing *hw = &this->hw;
930 struct resources *r = &this->resources;
931 void __iomem *gpmi_regs = r->gpmi_regs;
932 unsigned int dll_wait_time_us;
933 int ret;
934
935
936
937
938
939 if (GPMI_IS_MX6Q(this) || GPMI_IS_MX6SX(this))
940 clk_disable_unprepare(r->clock[0]);
941
942 ret = clk_set_rate(r->clock[0], hw->clk_rate);
943 if (ret) {
944 dev_err(this->dev, "cannot set clock rate to %lu Hz: %d\n", hw->clk_rate, ret);
945 return ret;
946 }
947
948 if (GPMI_IS_MX6Q(this) || GPMI_IS_MX6SX(this)) {
949 ret = clk_prepare_enable(r->clock[0]);
950 if (ret)
951 return ret;
952 }
953
954 writel(hw->timing0, gpmi_regs + HW_GPMI_TIMING0);
955 writel(hw->timing1, gpmi_regs + HW_GPMI_TIMING1);
956
957
958
959
960
961 writel(BM_GPMI_CTRL1_CLEAR_MASK, gpmi_regs + HW_GPMI_CTRL1_CLR);
962 writel(hw->ctrl1n, gpmi_regs + HW_GPMI_CTRL1_SET);
963
964
965 dll_wait_time_us = USEC_PER_SEC / hw->clk_rate * 64;
966 if (!dll_wait_time_us)
967 dll_wait_time_us = 1;
968
969
970 udelay(dll_wait_time_us);
971
972 return 0;
973}
974
975static int gpmi_setup_interface(struct nand_chip *chip, int chipnr,
976 const struct nand_interface_config *conf)
977{
978 struct gpmi_nand_data *this = nand_get_controller_data(chip);
979 const struct nand_sdr_timings *sdr;
980 int ret;
981
982
983 sdr = nand_get_sdr_timings(conf);
984 if (IS_ERR(sdr))
985 return PTR_ERR(sdr);
986
987
988 if (sdr->tRC_min <= 25000 && !GPMI_IS_MX28(this) && !GPMI_IS_MX6(this))
989 return -ENOTSUPP;
990
991
992 if (chipnr < 0)
993 return 0;
994
995
996 ret = gpmi_nfc_compute_timings(this, sdr);
997 if (ret)
998 return ret;
999
1000 this->hw.must_apply_timings = true;
1001
1002 return 0;
1003}
1004
1005
1006static void gpmi_clear_bch(struct gpmi_nand_data *this)
1007{
1008 struct resources *r = &this->resources;
1009 writel(BM_BCH_CTRL_COMPLETE_IRQ, r->bch_regs + HW_BCH_CTRL_CLR);
1010}
1011
1012static struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
1013{
1014
1015 return this->dma_chans[0];
1016}
1017
1018
1019static void dma_irq_callback(void *param)
1020{
1021 struct gpmi_nand_data *this = param;
1022 struct completion *dma_c = &this->dma_done;
1023
1024 complete(dma_c);
1025}
1026
1027static irqreturn_t bch_irq(int irq, void *cookie)
1028{
1029 struct gpmi_nand_data *this = cookie;
1030
1031 gpmi_clear_bch(this);
1032 complete(&this->bch_done);
1033 return IRQ_HANDLED;
1034}
1035
1036static int gpmi_raw_len_to_len(struct gpmi_nand_data *this, int raw_len)
1037{
1038
1039
1040
1041
1042 if (this->bch)
1043 return ALIGN_DOWN(raw_len, this->bch_geometry.eccn_chunk_size);
1044 else
1045 return raw_len;
1046}
1047
1048
1049static bool prepare_data_dma(struct gpmi_nand_data *this, const void *buf,
1050 int raw_len, struct scatterlist *sgl,
1051 enum dma_data_direction dr)
1052{
1053 int ret;
1054 int len = gpmi_raw_len_to_len(this, raw_len);
1055
1056
1057 if (virt_addr_valid(buf) && !object_is_on_stack(buf)) {
1058 sg_init_one(sgl, buf, len);
1059 ret = dma_map_sg(this->dev, sgl, 1, dr);
1060 if (ret == 0)
1061 goto map_fail;
1062
1063 return true;
1064 }
1065
1066map_fail:
1067
1068 sg_init_one(sgl, this->data_buffer_dma, len);
1069
1070 if (dr == DMA_TO_DEVICE && buf != this->data_buffer_dma)
1071 memcpy(this->data_buffer_dma, buf, len);
1072
1073 dma_map_sg(this->dev, sgl, 1, dr);
1074
1075 return false;
1076}
1077
1078
1079static uint8_t scan_ff_pattern[] = { 0xff };
1080static struct nand_bbt_descr gpmi_bbt_descr = {
1081 .options = 0,
1082 .offs = 0,
1083 .len = 1,
1084 .pattern = scan_ff_pattern
1085};
1086
1087
1088
1089
1090
1091static int gpmi_ooblayout_ecc(struct mtd_info *mtd, int section,
1092 struct mtd_oob_region *oobregion)
1093{
1094 struct nand_chip *chip = mtd_to_nand(mtd);
1095 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1096 struct bch_geometry *geo = &this->bch_geometry;
1097
1098 if (section)
1099 return -ERANGE;
1100
1101 oobregion->offset = 0;
1102 oobregion->length = geo->page_size - mtd->writesize;
1103
1104 return 0;
1105}
1106
1107static int gpmi_ooblayout_free(struct mtd_info *mtd, int section,
1108 struct mtd_oob_region *oobregion)
1109{
1110 struct nand_chip *chip = mtd_to_nand(mtd);
1111 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1112 struct bch_geometry *geo = &this->bch_geometry;
1113
1114 if (section)
1115 return -ERANGE;
1116
1117
1118 if (geo->page_size < mtd->writesize + mtd->oobsize) {
1119 oobregion->offset = geo->page_size - mtd->writesize;
1120 oobregion->length = mtd->oobsize - oobregion->offset;
1121 }
1122
1123 return 0;
1124}
1125
1126static const char * const gpmi_clks_for_mx2x[] = {
1127 "gpmi_io",
1128};
1129
1130static const struct mtd_ooblayout_ops gpmi_ooblayout_ops = {
1131 .ecc = gpmi_ooblayout_ecc,
1132 .free = gpmi_ooblayout_free,
1133};
1134
1135static const struct gpmi_devdata gpmi_devdata_imx23 = {
1136 .type = IS_MX23,
1137 .bch_max_ecc_strength = 20,
1138 .max_chain_delay = 16000,
1139 .clks = gpmi_clks_for_mx2x,
1140 .clks_count = ARRAY_SIZE(gpmi_clks_for_mx2x),
1141};
1142
1143static const struct gpmi_devdata gpmi_devdata_imx28 = {
1144 .type = IS_MX28,
1145 .bch_max_ecc_strength = 20,
1146 .max_chain_delay = 16000,
1147 .clks = gpmi_clks_for_mx2x,
1148 .clks_count = ARRAY_SIZE(gpmi_clks_for_mx2x),
1149};
1150
1151static const char * const gpmi_clks_for_mx6[] = {
1152 "gpmi_io", "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch",
1153};
1154
1155static const struct gpmi_devdata gpmi_devdata_imx6q = {
1156 .type = IS_MX6Q,
1157 .bch_max_ecc_strength = 40,
1158 .max_chain_delay = 12000,
1159 .clks = gpmi_clks_for_mx6,
1160 .clks_count = ARRAY_SIZE(gpmi_clks_for_mx6),
1161};
1162
1163static const struct gpmi_devdata gpmi_devdata_imx6sx = {
1164 .type = IS_MX6SX,
1165 .bch_max_ecc_strength = 62,
1166 .max_chain_delay = 12000,
1167 .clks = gpmi_clks_for_mx6,
1168 .clks_count = ARRAY_SIZE(gpmi_clks_for_mx6),
1169};
1170
1171static const char * const gpmi_clks_for_mx7d[] = {
1172 "gpmi_io", "gpmi_bch_apb",
1173};
1174
1175static const struct gpmi_devdata gpmi_devdata_imx7d = {
1176 .type = IS_MX7D,
1177 .bch_max_ecc_strength = 62,
1178 .max_chain_delay = 12000,
1179 .clks = gpmi_clks_for_mx7d,
1180 .clks_count = ARRAY_SIZE(gpmi_clks_for_mx7d),
1181};
1182
1183static int acquire_register_block(struct gpmi_nand_data *this,
1184 const char *res_name)
1185{
1186 struct platform_device *pdev = this->pdev;
1187 struct resources *res = &this->resources;
1188 void __iomem *p;
1189
1190 p = devm_platform_ioremap_resource_byname(pdev, res_name);
1191 if (IS_ERR(p))
1192 return PTR_ERR(p);
1193
1194 if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
1195 res->gpmi_regs = p;
1196 else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
1197 res->bch_regs = p;
1198 else
1199 dev_err(this->dev, "unknown resource name : %s\n", res_name);
1200
1201 return 0;
1202}
1203
1204static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
1205{
1206 struct platform_device *pdev = this->pdev;
1207 const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
1208 int err;
1209
1210 err = platform_get_irq_byname(pdev, res_name);
1211 if (err < 0)
1212 return err;
1213
1214 err = devm_request_irq(this->dev, err, irq_h, 0, res_name, this);
1215 if (err)
1216 dev_err(this->dev, "error requesting BCH IRQ\n");
1217
1218 return err;
1219}
1220
1221static void release_dma_channels(struct gpmi_nand_data *this)
1222{
1223 unsigned int i;
1224 for (i = 0; i < DMA_CHANS; i++)
1225 if (this->dma_chans[i]) {
1226 dma_release_channel(this->dma_chans[i]);
1227 this->dma_chans[i] = NULL;
1228 }
1229}
1230
1231static int acquire_dma_channels(struct gpmi_nand_data *this)
1232{
1233 struct platform_device *pdev = this->pdev;
1234 struct dma_chan *dma_chan;
1235 int ret = 0;
1236
1237
1238 dma_chan = dma_request_chan(&pdev->dev, "rx-tx");
1239 if (IS_ERR(dma_chan)) {
1240 ret = dev_err_probe(this->dev, PTR_ERR(dma_chan),
1241 "DMA channel request failed\n");
1242 release_dma_channels(this);
1243 } else {
1244 this->dma_chans[0] = dma_chan;
1245 }
1246
1247 return ret;
1248}
1249
1250static int gpmi_get_clks(struct gpmi_nand_data *this)
1251{
1252 struct resources *r = &this->resources;
1253 struct clk *clk;
1254 int err, i;
1255
1256 for (i = 0; i < this->devdata->clks_count; i++) {
1257 clk = devm_clk_get(this->dev, this->devdata->clks[i]);
1258 if (IS_ERR(clk)) {
1259 err = PTR_ERR(clk);
1260 goto err_clock;
1261 }
1262
1263 r->clock[i] = clk;
1264 }
1265
1266 return 0;
1267
1268err_clock:
1269 dev_dbg(this->dev, "failed in finding the clocks.\n");
1270 return err;
1271}
1272
1273static int acquire_resources(struct gpmi_nand_data *this)
1274{
1275 int ret;
1276
1277 ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
1278 if (ret)
1279 goto exit_regs;
1280
1281 ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
1282 if (ret)
1283 goto exit_regs;
1284
1285 ret = acquire_bch_irq(this, bch_irq);
1286 if (ret)
1287 goto exit_regs;
1288
1289 ret = acquire_dma_channels(this);
1290 if (ret)
1291 goto exit_regs;
1292
1293 ret = gpmi_get_clks(this);
1294 if (ret)
1295 goto exit_clock;
1296 return 0;
1297
1298exit_clock:
1299 release_dma_channels(this);
1300exit_regs:
1301 return ret;
1302}
1303
1304static void release_resources(struct gpmi_nand_data *this)
1305{
1306 release_dma_channels(this);
1307}
1308
1309static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
1310{
1311 struct device *dev = this->dev;
1312 struct bch_geometry *geo = &this->bch_geometry;
1313
1314 if (this->auxiliary_virt && virt_addr_valid(this->auxiliary_virt))
1315 dma_free_coherent(dev, geo->auxiliary_size,
1316 this->auxiliary_virt,
1317 this->auxiliary_phys);
1318 kfree(this->data_buffer_dma);
1319 kfree(this->raw_buffer);
1320
1321 this->data_buffer_dma = NULL;
1322 this->raw_buffer = NULL;
1323}
1324
1325
1326static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
1327{
1328 struct bch_geometry *geo = &this->bch_geometry;
1329 struct device *dev = this->dev;
1330 struct mtd_info *mtd = nand_to_mtd(&this->nand);
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340 this->data_buffer_dma = kzalloc(mtd->writesize ?: PAGE_SIZE,
1341 GFP_DMA | GFP_KERNEL);
1342 if (this->data_buffer_dma == NULL)
1343 goto error_alloc;
1344
1345 this->auxiliary_virt = dma_alloc_coherent(dev, geo->auxiliary_size,
1346 &this->auxiliary_phys, GFP_DMA);
1347 if (!this->auxiliary_virt)
1348 goto error_alloc;
1349
1350 this->raw_buffer = kzalloc((mtd->writesize ?: PAGE_SIZE) + mtd->oobsize, GFP_KERNEL);
1351 if (!this->raw_buffer)
1352 goto error_alloc;
1353
1354 return 0;
1355
1356error_alloc:
1357 gpmi_free_dma_buffer(this);
1358 return -ENOMEM;
1359}
1360
1361
1362
1363
1364
1365
1366static void block_mark_swapping(struct gpmi_nand_data *this,
1367 void *payload, void *auxiliary)
1368{
1369 struct bch_geometry *nfc_geo = &this->bch_geometry;
1370 unsigned char *p;
1371 unsigned char *a;
1372 unsigned int bit;
1373 unsigned char mask;
1374 unsigned char from_data;
1375 unsigned char from_oob;
1376
1377 if (!this->swap_block_mark)
1378 return;
1379
1380
1381
1382
1383
1384 bit = nfc_geo->block_mark_bit_offset;
1385 p = payload + nfc_geo->block_mark_byte_offset;
1386 a = auxiliary;
1387
1388
1389
1390
1391
1392
1393
1394 from_data = (p[0] >> bit) | (p[1] << (8 - bit));
1395
1396
1397 from_oob = a[0];
1398
1399
1400 a[0] = from_data;
1401
1402 mask = (0x1 << bit) - 1;
1403 p[0] = (p[0] & mask) | (from_oob << bit);
1404
1405 mask = ~0 << bit;
1406 p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
1407}
1408
1409static int gpmi_count_bitflips(struct nand_chip *chip, void *buf, int first,
1410 int last, int meta)
1411{
1412 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1413 struct bch_geometry *nfc_geo = &this->bch_geometry;
1414 struct mtd_info *mtd = nand_to_mtd(chip);
1415 int i;
1416 unsigned char *status;
1417 unsigned int max_bitflips = 0;
1418
1419
1420 status = this->auxiliary_virt + ALIGN(meta, 4);
1421
1422 for (i = first; i < last; i++, status++) {
1423 if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
1424 continue;
1425
1426 if (*status == STATUS_UNCORRECTABLE) {
1427 int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
1428 u8 *eccbuf = this->raw_buffer;
1429 int offset, bitoffset;
1430 int eccbytes;
1431 int flips;
1432
1433
1434 offset = nfc_geo->metadata_size * 8;
1435 offset += ((8 * nfc_geo->eccn_chunk_size) + eccbits) * (i + 1);
1436 offset -= eccbits;
1437 bitoffset = offset % 8;
1438 eccbytes = DIV_ROUND_UP(offset + eccbits, 8);
1439 offset /= 8;
1440 eccbytes -= offset;
1441 nand_change_read_column_op(chip, offset, eccbuf,
1442 eccbytes, false);
1443
1444
1445
1446
1447
1448
1449
1450
1451 if (bitoffset)
1452 eccbuf[0] |= GENMASK(bitoffset - 1, 0);
1453
1454 bitoffset = (bitoffset + eccbits) % 8;
1455 if (bitoffset)
1456 eccbuf[eccbytes - 1] |= GENMASK(7, bitoffset);
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469 if (i == 0) {
1470
1471 flips = nand_check_erased_ecc_chunk(
1472 buf + i * nfc_geo->eccn_chunk_size,
1473 nfc_geo->eccn_chunk_size,
1474 eccbuf, eccbytes,
1475 this->auxiliary_virt,
1476 nfc_geo->metadata_size,
1477 nfc_geo->ecc_strength);
1478 } else {
1479 flips = nand_check_erased_ecc_chunk(
1480 buf + i * nfc_geo->eccn_chunk_size,
1481 nfc_geo->eccn_chunk_size,
1482 eccbuf, eccbytes,
1483 NULL, 0,
1484 nfc_geo->ecc_strength);
1485 }
1486
1487 if (flips > 0) {
1488 max_bitflips = max_t(unsigned int, max_bitflips,
1489 flips);
1490 mtd->ecc_stats.corrected += flips;
1491 continue;
1492 }
1493
1494 mtd->ecc_stats.failed++;
1495 continue;
1496 }
1497
1498 mtd->ecc_stats.corrected += *status;
1499 max_bitflips = max_t(unsigned int, max_bitflips, *status);
1500 }
1501
1502 return max_bitflips;
1503}
1504
1505static void gpmi_bch_layout_std(struct gpmi_nand_data *this)
1506{
1507 struct bch_geometry *geo = &this->bch_geometry;
1508 unsigned int ecc_strength = geo->ecc_strength >> 1;
1509 unsigned int gf_len = geo->gf_len;
1510 unsigned int block0_size = geo->ecc0_chunk_size;
1511 unsigned int blockn_size = geo->eccn_chunk_size;
1512
1513 this->bch_flashlayout0 =
1514 BF_BCH_FLASH0LAYOUT0_NBLOCKS(geo->ecc_chunk_count - 1) |
1515 BF_BCH_FLASH0LAYOUT0_META_SIZE(geo->metadata_size) |
1516 BF_BCH_FLASH0LAYOUT0_ECC0(ecc_strength, this) |
1517 BF_BCH_FLASH0LAYOUT0_GF(gf_len, this) |
1518 BF_BCH_FLASH0LAYOUT0_DATA0_SIZE(block0_size, this);
1519
1520 this->bch_flashlayout1 =
1521 BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(geo->page_size) |
1522 BF_BCH_FLASH0LAYOUT1_ECCN(ecc_strength, this) |
1523 BF_BCH_FLASH0LAYOUT1_GF(gf_len, this) |
1524 BF_BCH_FLASH0LAYOUT1_DATAN_SIZE(blockn_size, this);
1525}
1526
1527static int gpmi_ecc_read_page(struct nand_chip *chip, uint8_t *buf,
1528 int oob_required, int page)
1529{
1530 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1531 struct mtd_info *mtd = nand_to_mtd(chip);
1532 struct bch_geometry *geo = &this->bch_geometry;
1533 unsigned int max_bitflips;
1534 int ret;
1535
1536 gpmi_bch_layout_std(this);
1537 this->bch = true;
1538
1539 ret = nand_read_page_op(chip, page, 0, buf, geo->page_size);
1540 if (ret)
1541 return ret;
1542
1543 max_bitflips = gpmi_count_bitflips(chip, buf, 0,
1544 geo->ecc_chunk_count,
1545 geo->auxiliary_status_offset);
1546
1547
1548 block_mark_swapping(this, buf, this->auxiliary_virt);
1549
1550 if (oob_required) {
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561 memset(chip->oob_poi, ~0, mtd->oobsize);
1562 chip->oob_poi[0] = ((uint8_t *)this->auxiliary_virt)[0];
1563 }
1564
1565 return max_bitflips;
1566}
1567
1568
1569static int gpmi_ecc_read_subpage(struct nand_chip *chip, uint32_t offs,
1570 uint32_t len, uint8_t *buf, int page)
1571{
1572 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1573 struct bch_geometry *geo = &this->bch_geometry;
1574 int size = chip->ecc.size;
1575 int meta, n, page_size;
1576 unsigned int max_bitflips;
1577 unsigned int ecc_strength;
1578 int first, last, marker_pos;
1579 int ecc_parity_size;
1580 int col = 0;
1581 int ret;
1582
1583
1584 ecc_parity_size = geo->gf_len * geo->ecc_strength / 8;
1585
1586
1587 first = offs / size;
1588 last = (offs + len - 1) / size;
1589
1590 if (this->swap_block_mark) {
1591
1592
1593
1594
1595
1596
1597
1598 marker_pos = geo->block_mark_byte_offset / size;
1599 if (last >= marker_pos && first <= marker_pos) {
1600 dev_dbg(this->dev,
1601 "page:%d, first:%d, last:%d, marker at:%d\n",
1602 page, first, last, marker_pos);
1603 return gpmi_ecc_read_page(chip, buf, 0, page);
1604 }
1605 }
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615 meta = geo->metadata_size;
1616 if (first) {
1617 if (geo->ecc_for_meta)
1618 col = meta + ecc_parity_size
1619 + (size + ecc_parity_size) * first;
1620 else
1621 col = meta + (size + ecc_parity_size) * first;
1622
1623 meta = 0;
1624 buf = buf + first * size;
1625 }
1626
1627 ecc_parity_size = geo->gf_len * geo->ecc_strength / 8;
1628 n = last - first + 1;
1629
1630 if (geo->ecc_for_meta && meta)
1631 page_size = meta + ecc_parity_size
1632 + (size + ecc_parity_size) * n;
1633 else
1634 page_size = meta + (size + ecc_parity_size) * n;
1635
1636 ecc_strength = geo->ecc_strength >> 1;
1637
1638 this->bch_flashlayout0 = BF_BCH_FLASH0LAYOUT0_NBLOCKS(
1639 (geo->ecc_for_meta ? n : n - 1)) |
1640 BF_BCH_FLASH0LAYOUT0_META_SIZE(meta) |
1641 BF_BCH_FLASH0LAYOUT0_ECC0(ecc_strength, this) |
1642 BF_BCH_FLASH0LAYOUT0_GF(geo->gf_len, this) |
1643 BF_BCH_FLASH0LAYOUT0_DATA0_SIZE((geo->ecc_for_meta ?
1644 0 : geo->ecc0_chunk_size), this);
1645
1646 this->bch_flashlayout1 = BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size) |
1647 BF_BCH_FLASH0LAYOUT1_ECCN(ecc_strength, this) |
1648 BF_BCH_FLASH0LAYOUT1_GF(geo->gf_len, this) |
1649 BF_BCH_FLASH0LAYOUT1_DATAN_SIZE(geo->eccn_chunk_size, this);
1650
1651 this->bch = true;
1652
1653 ret = nand_read_page_op(chip, page, col, buf, page_size);
1654 if (ret)
1655 return ret;
1656
1657 dev_dbg(this->dev, "page:%d(%d:%d)%d, chunk:(%d:%d), BCH PG size:%d\n",
1658 page, offs, len, col, first, n, page_size);
1659
1660 max_bitflips = gpmi_count_bitflips(chip, buf, first, last, meta);
1661
1662 return max_bitflips;
1663}
1664
1665static int gpmi_ecc_write_page(struct nand_chip *chip, const uint8_t *buf,
1666 int oob_required, int page)
1667{
1668 struct mtd_info *mtd = nand_to_mtd(chip);
1669 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1670 struct bch_geometry *nfc_geo = &this->bch_geometry;
1671
1672 dev_dbg(this->dev, "ecc write page.\n");
1673
1674 gpmi_bch_layout_std(this);
1675 this->bch = true;
1676
1677 memcpy(this->auxiliary_virt, chip->oob_poi, nfc_geo->auxiliary_size);
1678
1679 if (this->swap_block_mark) {
1680
1681
1682
1683
1684 memcpy(this->data_buffer_dma, buf, mtd->writesize);
1685 buf = this->data_buffer_dma;
1686 block_mark_swapping(this, this->data_buffer_dma,
1687 this->auxiliary_virt);
1688 }
1689
1690 return nand_prog_page_op(chip, page, 0, buf, nfc_geo->page_size);
1691}
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753static int gpmi_ecc_read_oob(struct nand_chip *chip, int page)
1754{
1755 struct mtd_info *mtd = nand_to_mtd(chip);
1756 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1757 int ret;
1758
1759
1760 memset(chip->oob_poi, ~0, mtd->oobsize);
1761
1762
1763 ret = nand_read_page_op(chip, page, mtd->writesize, chip->oob_poi,
1764 mtd->oobsize);
1765 if (ret)
1766 return ret;
1767
1768
1769
1770
1771
1772
1773 if (GPMI_IS_MX23(this)) {
1774
1775 ret = nand_read_page_op(chip, page, 0, chip->oob_poi, 1);
1776 if (ret)
1777 return ret;
1778 }
1779
1780 return 0;
1781}
1782
1783static int gpmi_ecc_write_oob(struct nand_chip *chip, int page)
1784{
1785 struct mtd_info *mtd = nand_to_mtd(chip);
1786 struct mtd_oob_region of = { };
1787
1788
1789 mtd_ooblayout_free(mtd, 0, &of);
1790 if (!of.length)
1791 return -EPERM;
1792
1793 if (!nand_is_slc(chip))
1794 return -EPERM;
1795
1796 return nand_prog_page_op(chip, page, mtd->writesize + of.offset,
1797 chip->oob_poi + of.offset, of.length);
1798}
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812static int gpmi_ecc_read_page_raw(struct nand_chip *chip, uint8_t *buf,
1813 int oob_required, int page)
1814{
1815 struct mtd_info *mtd = nand_to_mtd(chip);
1816 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1817 struct bch_geometry *nfc_geo = &this->bch_geometry;
1818 int eccsize = nfc_geo->eccn_chunk_size;
1819 int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
1820 u8 *tmp_buf = this->raw_buffer;
1821 size_t src_bit_off;
1822 size_t oob_bit_off;
1823 size_t oob_byte_off;
1824 uint8_t *oob = chip->oob_poi;
1825 int step;
1826 int ret;
1827
1828 ret = nand_read_page_op(chip, page, 0, tmp_buf,
1829 mtd->writesize + mtd->oobsize);
1830 if (ret)
1831 return ret;
1832
1833
1834
1835
1836
1837
1838
1839
1840 if (this->swap_block_mark)
1841 swap(tmp_buf[0], tmp_buf[mtd->writesize]);
1842
1843
1844
1845
1846
1847 if (oob_required)
1848 memcpy(oob, tmp_buf, nfc_geo->metadata_size);
1849
1850 oob_bit_off = nfc_geo->metadata_size * 8;
1851 src_bit_off = oob_bit_off;
1852
1853
1854 for (step = 0; step < nfc_geo->ecc_chunk_count; step++) {
1855 if (buf)
1856 nand_extract_bits(buf, step * eccsize * 8, tmp_buf,
1857 src_bit_off, eccsize * 8);
1858 src_bit_off += eccsize * 8;
1859
1860
1861 if (step == nfc_geo->ecc_chunk_count - 1 &&
1862 (oob_bit_off + eccbits) % 8)
1863 eccbits += 8 - ((oob_bit_off + eccbits) % 8);
1864
1865 if (oob_required)
1866 nand_extract_bits(oob, oob_bit_off, tmp_buf,
1867 src_bit_off, eccbits);
1868
1869 src_bit_off += eccbits;
1870 oob_bit_off += eccbits;
1871 }
1872
1873 if (oob_required) {
1874 oob_byte_off = oob_bit_off / 8;
1875
1876 if (oob_byte_off < mtd->oobsize)
1877 memcpy(oob + oob_byte_off,
1878 tmp_buf + mtd->writesize + oob_byte_off,
1879 mtd->oobsize - oob_byte_off);
1880 }
1881
1882 return 0;
1883}
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897static int gpmi_ecc_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
1898 int oob_required, int page)
1899{
1900 struct mtd_info *mtd = nand_to_mtd(chip);
1901 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1902 struct bch_geometry *nfc_geo = &this->bch_geometry;
1903 int eccsize = nfc_geo->eccn_chunk_size;
1904 int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
1905 u8 *tmp_buf = this->raw_buffer;
1906 uint8_t *oob = chip->oob_poi;
1907 size_t dst_bit_off;
1908 size_t oob_bit_off;
1909 size_t oob_byte_off;
1910 int step;
1911
1912
1913
1914
1915
1916
1917 if (!buf || !oob_required)
1918 memset(tmp_buf, 0xff, mtd->writesize + mtd->oobsize);
1919
1920
1921
1922
1923
1924 memcpy(tmp_buf, oob, nfc_geo->metadata_size);
1925 oob_bit_off = nfc_geo->metadata_size * 8;
1926 dst_bit_off = oob_bit_off;
1927
1928
1929 for (step = 0; step < nfc_geo->ecc_chunk_count; step++) {
1930 if (buf)
1931 nand_extract_bits(tmp_buf, dst_bit_off, buf,
1932 step * eccsize * 8, eccsize * 8);
1933 dst_bit_off += eccsize * 8;
1934
1935
1936 if (step == nfc_geo->ecc_chunk_count - 1 &&
1937 (oob_bit_off + eccbits) % 8)
1938 eccbits += 8 - ((oob_bit_off + eccbits) % 8);
1939
1940 if (oob_required)
1941 nand_extract_bits(tmp_buf, dst_bit_off, oob,
1942 oob_bit_off, eccbits);
1943
1944 dst_bit_off += eccbits;
1945 oob_bit_off += eccbits;
1946 }
1947
1948 oob_byte_off = oob_bit_off / 8;
1949
1950 if (oob_required && oob_byte_off < mtd->oobsize)
1951 memcpy(tmp_buf + mtd->writesize + oob_byte_off,
1952 oob + oob_byte_off, mtd->oobsize - oob_byte_off);
1953
1954
1955
1956
1957
1958
1959
1960
1961 if (this->swap_block_mark)
1962 swap(tmp_buf[0], tmp_buf[mtd->writesize]);
1963
1964 return nand_prog_page_op(chip, page, 0, tmp_buf,
1965 mtd->writesize + mtd->oobsize);
1966}
1967
1968static int gpmi_ecc_read_oob_raw(struct nand_chip *chip, int page)
1969{
1970 return gpmi_ecc_read_page_raw(chip, NULL, 1, page);
1971}
1972
1973static int gpmi_ecc_write_oob_raw(struct nand_chip *chip, int page)
1974{
1975 return gpmi_ecc_write_page_raw(chip, NULL, 1, page);
1976}
1977
1978static int gpmi_block_markbad(struct nand_chip *chip, loff_t ofs)
1979{
1980 struct mtd_info *mtd = nand_to_mtd(chip);
1981 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1982 int ret = 0;
1983 uint8_t *block_mark;
1984 int column, page, chipnr;
1985
1986 chipnr = (int)(ofs >> chip->chip_shift);
1987 nand_select_target(chip, chipnr);
1988
1989 column = !GPMI_IS_MX23(this) ? mtd->writesize : 0;
1990
1991
1992 block_mark = this->data_buffer_dma;
1993 block_mark[0] = 0;
1994
1995
1996 page = (int)(ofs >> chip->page_shift);
1997
1998 ret = nand_prog_page_op(chip, page, column, block_mark, 1);
1999
2000 nand_deselect_target(chip);
2001
2002 return ret;
2003}
2004
2005static int nand_boot_set_geometry(struct gpmi_nand_data *this)
2006{
2007 struct boot_rom_geometry *geometry = &this->rom_geometry;
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017 geometry->stride_size_in_pages = 64;
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027 geometry->search_area_stride_exponent = 2;
2028 return 0;
2029}
2030
2031static const char *fingerprint = "STMP";
2032static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
2033{
2034 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
2035 struct device *dev = this->dev;
2036 struct nand_chip *chip = &this->nand;
2037 unsigned int search_area_size_in_strides;
2038 unsigned int stride;
2039 unsigned int page;
2040 u8 *buffer = nand_get_data_buf(chip);
2041 int found_an_ncb_fingerprint = false;
2042 int ret;
2043
2044
2045 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
2046
2047 nand_select_target(chip, 0);
2048
2049
2050
2051
2052 dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
2053
2054 for (stride = 0; stride < search_area_size_in_strides; stride++) {
2055
2056 page = stride * rom_geo->stride_size_in_pages;
2057
2058 dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
2059
2060
2061
2062
2063
2064 ret = nand_read_page_op(chip, page, 12, buffer,
2065 strlen(fingerprint));
2066 if (ret)
2067 continue;
2068
2069
2070 if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
2071 found_an_ncb_fingerprint = true;
2072 break;
2073 }
2074
2075 }
2076
2077 nand_deselect_target(chip);
2078
2079 if (found_an_ncb_fingerprint)
2080 dev_dbg(dev, "\tFound a fingerprint\n");
2081 else
2082 dev_dbg(dev, "\tNo fingerprint found\n");
2083 return found_an_ncb_fingerprint;
2084}
2085
2086
2087static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
2088{
2089 struct device *dev = this->dev;
2090 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
2091 struct nand_chip *chip = &this->nand;
2092 struct mtd_info *mtd = nand_to_mtd(chip);
2093 unsigned int block_size_in_pages;
2094 unsigned int search_area_size_in_strides;
2095 unsigned int search_area_size_in_pages;
2096 unsigned int search_area_size_in_blocks;
2097 unsigned int block;
2098 unsigned int stride;
2099 unsigned int page;
2100 u8 *buffer = nand_get_data_buf(chip);
2101 int status;
2102
2103
2104 block_size_in_pages = mtd->erasesize / mtd->writesize;
2105 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
2106 search_area_size_in_pages = search_area_size_in_strides *
2107 rom_geo->stride_size_in_pages;
2108 search_area_size_in_blocks =
2109 (search_area_size_in_pages + (block_size_in_pages - 1)) /
2110 block_size_in_pages;
2111
2112 dev_dbg(dev, "Search Area Geometry :\n");
2113 dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
2114 dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
2115 dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
2116
2117 nand_select_target(chip, 0);
2118
2119
2120 dev_dbg(dev, "Erasing the search area...\n");
2121
2122 for (block = 0; block < search_area_size_in_blocks; block++) {
2123
2124 dev_dbg(dev, "\tErasing block 0x%x\n", block);
2125 status = nand_erase_op(chip, block);
2126 if (status)
2127 dev_err(dev, "[%s] Erase failed.\n", __func__);
2128 }
2129
2130
2131 memset(buffer, ~0, mtd->writesize);
2132 memcpy(buffer + 12, fingerprint, strlen(fingerprint));
2133
2134
2135 dev_dbg(dev, "Writing NCB fingerprints...\n");
2136 for (stride = 0; stride < search_area_size_in_strides; stride++) {
2137
2138 page = stride * rom_geo->stride_size_in_pages;
2139
2140
2141 dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
2142
2143 status = chip->ecc.write_page_raw(chip, buffer, 0, page);
2144 if (status)
2145 dev_err(dev, "[%s] Write failed.\n", __func__);
2146 }
2147
2148 nand_deselect_target(chip);
2149
2150 return 0;
2151}
2152
2153static int mx23_boot_init(struct gpmi_nand_data *this)
2154{
2155 struct device *dev = this->dev;
2156 struct nand_chip *chip = &this->nand;
2157 struct mtd_info *mtd = nand_to_mtd(chip);
2158 unsigned int block_count;
2159 unsigned int block;
2160 int chipnr;
2161 int page;
2162 loff_t byte;
2163 uint8_t block_mark;
2164 int ret = 0;
2165
2166
2167
2168
2169
2170
2171
2172 if (mx23_check_transcription_stamp(this))
2173 return 0;
2174
2175
2176
2177
2178
2179 dev_dbg(dev, "Transcribing bad block marks...\n");
2180
2181
2182 block_count = nanddev_eraseblocks_per_target(&chip->base);
2183
2184
2185
2186
2187
2188 for (block = 0; block < block_count; block++) {
2189
2190
2191
2192
2193 chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
2194 page = block << (chip->phys_erase_shift - chip->page_shift);
2195 byte = block << chip->phys_erase_shift;
2196
2197
2198 nand_select_target(chip, chipnr);
2199 ret = nand_read_page_op(chip, page, mtd->writesize, &block_mark,
2200 1);
2201 nand_deselect_target(chip);
2202
2203 if (ret)
2204 continue;
2205
2206
2207
2208
2209
2210
2211 if (block_mark != 0xff) {
2212 dev_dbg(dev, "Transcribing mark in block %u\n", block);
2213 ret = chip->legacy.block_markbad(chip, byte);
2214 if (ret)
2215 dev_err(dev,
2216 "Failed to mark block bad with ret %d\n",
2217 ret);
2218 }
2219 }
2220
2221
2222 mx23_write_transcription_stamp(this);
2223 return 0;
2224}
2225
2226static int nand_boot_init(struct gpmi_nand_data *this)
2227{
2228 nand_boot_set_geometry(this);
2229
2230
2231 if (GPMI_IS_MX23(this))
2232 return mx23_boot_init(this);
2233 return 0;
2234}
2235
2236static int gpmi_set_geometry(struct gpmi_nand_data *this)
2237{
2238 int ret;
2239
2240
2241 gpmi_free_dma_buffer(this);
2242
2243
2244 ret = bch_set_geometry(this);
2245 if (ret) {
2246 dev_err(this->dev, "Error setting BCH geometry : %d\n", ret);
2247 return ret;
2248 }
2249
2250
2251 return gpmi_alloc_dma_buffer(this);
2252}
2253
2254static int gpmi_init_last(struct gpmi_nand_data *this)
2255{
2256 struct nand_chip *chip = &this->nand;
2257 struct mtd_info *mtd = nand_to_mtd(chip);
2258 struct nand_ecc_ctrl *ecc = &chip->ecc;
2259 struct bch_geometry *bch_geo = &this->bch_geometry;
2260 int ret;
2261
2262
2263 ret = gpmi_set_geometry(this);
2264 if (ret)
2265 return ret;
2266
2267
2268 ecc->read_page = gpmi_ecc_read_page;
2269 ecc->write_page = gpmi_ecc_write_page;
2270 ecc->read_oob = gpmi_ecc_read_oob;
2271 ecc->write_oob = gpmi_ecc_write_oob;
2272 ecc->read_page_raw = gpmi_ecc_read_page_raw;
2273 ecc->write_page_raw = gpmi_ecc_write_page_raw;
2274 ecc->read_oob_raw = gpmi_ecc_read_oob_raw;
2275 ecc->write_oob_raw = gpmi_ecc_write_oob_raw;
2276 ecc->engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
2277 ecc->size = bch_geo->eccn_chunk_size;
2278 ecc->strength = bch_geo->ecc_strength;
2279 mtd_set_ooblayout(mtd, &gpmi_ooblayout_ops);
2280
2281
2282
2283
2284
2285
2286 if (GPMI_IS_MX6(this) &&
2287 ((bch_geo->gf_len * bch_geo->ecc_strength) % 8) == 0) {
2288 ecc->read_subpage = gpmi_ecc_read_subpage;
2289 chip->options |= NAND_SUBPAGE_READ;
2290 }
2291
2292 return 0;
2293}
2294
2295static int gpmi_nand_attach_chip(struct nand_chip *chip)
2296{
2297 struct gpmi_nand_data *this = nand_get_controller_data(chip);
2298 int ret;
2299
2300 if (chip->bbt_options & NAND_BBT_USE_FLASH) {
2301 chip->bbt_options |= NAND_BBT_NO_OOB;
2302
2303 if (of_property_read_bool(this->dev->of_node,
2304 "fsl,no-blockmark-swap"))
2305 this->swap_block_mark = false;
2306 }
2307 dev_dbg(this->dev, "Blockmark swapping %sabled\n",
2308 this->swap_block_mark ? "en" : "dis");
2309
2310 ret = gpmi_init_last(this);
2311 if (ret)
2312 return ret;
2313
2314 chip->options |= NAND_SKIP_BBTSCAN;
2315
2316 return 0;
2317}
2318
2319static struct gpmi_transfer *get_next_transfer(struct gpmi_nand_data *this)
2320{
2321 struct gpmi_transfer *transfer = &this->transfers[this->ntransfers];
2322
2323 this->ntransfers++;
2324
2325 if (this->ntransfers == GPMI_MAX_TRANSFERS)
2326 return NULL;
2327
2328 return transfer;
2329}
2330
2331static struct dma_async_tx_descriptor *gpmi_chain_command(
2332 struct gpmi_nand_data *this, u8 cmd, const u8 *addr, int naddr)
2333{
2334 struct dma_chan *channel = get_dma_chan(this);
2335 struct dma_async_tx_descriptor *desc;
2336 struct gpmi_transfer *transfer;
2337 int chip = this->nand.cur_cs;
2338 u32 pio[3];
2339
2340
2341 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WRITE)
2342 | BM_GPMI_CTRL0_WORD_LENGTH
2343 | BF_GPMI_CTRL0_CS(chip, this)
2344 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
2345 | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_CLE)
2346 | BM_GPMI_CTRL0_ADDRESS_INCREMENT
2347 | BF_GPMI_CTRL0_XFER_COUNT(naddr + 1);
2348 pio[1] = 0;
2349 pio[2] = 0;
2350 desc = mxs_dmaengine_prep_pio(channel, pio, ARRAY_SIZE(pio),
2351 DMA_TRANS_NONE, 0);
2352 if (!desc)
2353 return NULL;
2354
2355 transfer = get_next_transfer(this);
2356 if (!transfer)
2357 return NULL;
2358
2359 transfer->cmdbuf[0] = cmd;
2360 if (naddr)
2361 memcpy(&transfer->cmdbuf[1], addr, naddr);
2362
2363 sg_init_one(&transfer->sgl, transfer->cmdbuf, naddr + 1);
2364 dma_map_sg(this->dev, &transfer->sgl, 1, DMA_TO_DEVICE);
2365
2366 transfer->direction = DMA_TO_DEVICE;
2367
2368 desc = dmaengine_prep_slave_sg(channel, &transfer->sgl, 1, DMA_MEM_TO_DEV,
2369 MXS_DMA_CTRL_WAIT4END);
2370 return desc;
2371}
2372
2373static struct dma_async_tx_descriptor *gpmi_chain_wait_ready(
2374 struct gpmi_nand_data *this)
2375{
2376 struct dma_chan *channel = get_dma_chan(this);
2377 u32 pio[2];
2378
2379 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY)
2380 | BM_GPMI_CTRL0_WORD_LENGTH
2381 | BF_GPMI_CTRL0_CS(this->nand.cur_cs, this)
2382 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
2383 | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA)
2384 | BF_GPMI_CTRL0_XFER_COUNT(0);
2385 pio[1] = 0;
2386
2387 return mxs_dmaengine_prep_pio(channel, pio, 2, DMA_TRANS_NONE,
2388 MXS_DMA_CTRL_WAIT4END | MXS_DMA_CTRL_WAIT4RDY);
2389}
2390
2391static struct dma_async_tx_descriptor *gpmi_chain_data_read(
2392 struct gpmi_nand_data *this, void *buf, int raw_len, bool *direct)
2393{
2394 struct dma_async_tx_descriptor *desc;
2395 struct dma_chan *channel = get_dma_chan(this);
2396 struct gpmi_transfer *transfer;
2397 u32 pio[6] = {};
2398
2399 transfer = get_next_transfer(this);
2400 if (!transfer)
2401 return NULL;
2402
2403 transfer->direction = DMA_FROM_DEVICE;
2404
2405 *direct = prepare_data_dma(this, buf, raw_len, &transfer->sgl,
2406 DMA_FROM_DEVICE);
2407
2408 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__READ)
2409 | BM_GPMI_CTRL0_WORD_LENGTH
2410 | BF_GPMI_CTRL0_CS(this->nand.cur_cs, this)
2411 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
2412 | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA)
2413 | BF_GPMI_CTRL0_XFER_COUNT(raw_len);
2414
2415 if (this->bch) {
2416 pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC
2417 | BF_GPMI_ECCCTRL_ECC_CMD(BV_GPMI_ECCCTRL_ECC_CMD__BCH_DECODE)
2418 | BF_GPMI_ECCCTRL_BUFFER_MASK(BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE
2419 | BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY);
2420 pio[3] = raw_len;
2421 pio[4] = transfer->sgl.dma_address;
2422 pio[5] = this->auxiliary_phys;
2423 }
2424
2425 desc = mxs_dmaengine_prep_pio(channel, pio, ARRAY_SIZE(pio),
2426 DMA_TRANS_NONE, 0);
2427 if (!desc)
2428 return NULL;
2429
2430 if (!this->bch)
2431 desc = dmaengine_prep_slave_sg(channel, &transfer->sgl, 1,
2432 DMA_DEV_TO_MEM,
2433 MXS_DMA_CTRL_WAIT4END);
2434
2435 return desc;
2436}
2437
2438static struct dma_async_tx_descriptor *gpmi_chain_data_write(
2439 struct gpmi_nand_data *this, const void *buf, int raw_len)
2440{
2441 struct dma_chan *channel = get_dma_chan(this);
2442 struct dma_async_tx_descriptor *desc;
2443 struct gpmi_transfer *transfer;
2444 u32 pio[6] = {};
2445
2446 transfer = get_next_transfer(this);
2447 if (!transfer)
2448 return NULL;
2449
2450 transfer->direction = DMA_TO_DEVICE;
2451
2452 prepare_data_dma(this, buf, raw_len, &transfer->sgl, DMA_TO_DEVICE);
2453
2454 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WRITE)
2455 | BM_GPMI_CTRL0_WORD_LENGTH
2456 | BF_GPMI_CTRL0_CS(this->nand.cur_cs, this)
2457 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
2458 | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA)
2459 | BF_GPMI_CTRL0_XFER_COUNT(raw_len);
2460
2461 if (this->bch) {
2462 pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC
2463 | BF_GPMI_ECCCTRL_ECC_CMD(BV_GPMI_ECCCTRL_ECC_CMD__BCH_ENCODE)
2464 | BF_GPMI_ECCCTRL_BUFFER_MASK(BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE |
2465 BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY);
2466 pio[3] = raw_len;
2467 pio[4] = transfer->sgl.dma_address;
2468 pio[5] = this->auxiliary_phys;
2469 }
2470
2471 desc = mxs_dmaengine_prep_pio(channel, pio, ARRAY_SIZE(pio),
2472 DMA_TRANS_NONE,
2473 (this->bch ? MXS_DMA_CTRL_WAIT4END : 0));
2474 if (!desc)
2475 return NULL;
2476
2477 if (!this->bch)
2478 desc = dmaengine_prep_slave_sg(channel, &transfer->sgl, 1,
2479 DMA_MEM_TO_DEV,
2480 MXS_DMA_CTRL_WAIT4END);
2481
2482 return desc;
2483}
2484
2485static int gpmi_nfc_exec_op(struct nand_chip *chip,
2486 const struct nand_operation *op,
2487 bool check_only)
2488{
2489 const struct nand_op_instr *instr;
2490 struct gpmi_nand_data *this = nand_get_controller_data(chip);
2491 struct dma_async_tx_descriptor *desc = NULL;
2492 int i, ret, buf_len = 0, nbufs = 0;
2493 u8 cmd = 0;
2494 void *buf_read = NULL;
2495 const void *buf_write = NULL;
2496 bool direct = false;
2497 struct completion *dma_completion, *bch_completion;
2498 unsigned long to;
2499
2500 if (check_only)
2501 return 0;
2502
2503 this->ntransfers = 0;
2504 for (i = 0; i < GPMI_MAX_TRANSFERS; i++)
2505 this->transfers[i].direction = DMA_NONE;
2506
2507 ret = pm_runtime_get_sync(this->dev);
2508 if (ret < 0) {
2509 pm_runtime_put_noidle(this->dev);
2510 return ret;
2511 }
2512
2513
2514
2515
2516
2517
2518
2519 if (this->hw.must_apply_timings) {
2520 this->hw.must_apply_timings = false;
2521 ret = gpmi_nfc_apply_timings(this);
2522 if (ret)
2523 goto out_pm;
2524 }
2525
2526 dev_dbg(this->dev, "%s: %d instructions\n", __func__, op->ninstrs);
2527
2528 for (i = 0; i < op->ninstrs; i++) {
2529 instr = &op->instrs[i];
2530
2531 nand_op_trace(" ", instr);
2532
2533 switch (instr->type) {
2534 case NAND_OP_WAITRDY_INSTR:
2535 desc = gpmi_chain_wait_ready(this);
2536 break;
2537 case NAND_OP_CMD_INSTR:
2538 cmd = instr->ctx.cmd.opcode;
2539
2540
2541
2542
2543
2544 if (i + 1 != op->ninstrs &&
2545 op->instrs[i + 1].type == NAND_OP_ADDR_INSTR)
2546 continue;
2547
2548 desc = gpmi_chain_command(this, cmd, NULL, 0);
2549
2550 break;
2551 case NAND_OP_ADDR_INSTR:
2552 desc = gpmi_chain_command(this, cmd, instr->ctx.addr.addrs,
2553 instr->ctx.addr.naddrs);
2554 break;
2555 case NAND_OP_DATA_OUT_INSTR:
2556 buf_write = instr->ctx.data.buf.out;
2557 buf_len = instr->ctx.data.len;
2558 nbufs++;
2559
2560 desc = gpmi_chain_data_write(this, buf_write, buf_len);
2561
2562 break;
2563 case NAND_OP_DATA_IN_INSTR:
2564 if (!instr->ctx.data.len)
2565 break;
2566 buf_read = instr->ctx.data.buf.in;
2567 buf_len = instr->ctx.data.len;
2568 nbufs++;
2569
2570 desc = gpmi_chain_data_read(this, buf_read, buf_len,
2571 &direct);
2572 break;
2573 }
2574
2575 if (!desc) {
2576 ret = -ENXIO;
2577 goto unmap;
2578 }
2579 }
2580
2581 dev_dbg(this->dev, "%s setup done\n", __func__);
2582
2583 if (nbufs > 1) {
2584 dev_err(this->dev, "Multiple data instructions not supported\n");
2585 ret = -EINVAL;
2586 goto unmap;
2587 }
2588
2589 if (this->bch) {
2590 writel(this->bch_flashlayout0,
2591 this->resources.bch_regs + HW_BCH_FLASH0LAYOUT0);
2592 writel(this->bch_flashlayout1,
2593 this->resources.bch_regs + HW_BCH_FLASH0LAYOUT1);
2594 }
2595
2596 desc->callback = dma_irq_callback;
2597 desc->callback_param = this;
2598 dma_completion = &this->dma_done;
2599 bch_completion = NULL;
2600
2601 init_completion(dma_completion);
2602
2603 if (this->bch && buf_read) {
2604 writel(BM_BCH_CTRL_COMPLETE_IRQ_EN,
2605 this->resources.bch_regs + HW_BCH_CTRL_SET);
2606 bch_completion = &this->bch_done;
2607 init_completion(bch_completion);
2608 }
2609
2610 dmaengine_submit(desc);
2611 dma_async_issue_pending(get_dma_chan(this));
2612
2613 to = wait_for_completion_timeout(dma_completion, msecs_to_jiffies(1000));
2614 if (!to) {
2615 dev_err(this->dev, "DMA timeout, last DMA\n");
2616 gpmi_dump_info(this);
2617 ret = -ETIMEDOUT;
2618 goto unmap;
2619 }
2620
2621 if (this->bch && buf_read) {
2622 to = wait_for_completion_timeout(bch_completion, msecs_to_jiffies(1000));
2623 if (!to) {
2624 dev_err(this->dev, "BCH timeout, last DMA\n");
2625 gpmi_dump_info(this);
2626 ret = -ETIMEDOUT;
2627 goto unmap;
2628 }
2629 }
2630
2631 writel(BM_BCH_CTRL_COMPLETE_IRQ_EN,
2632 this->resources.bch_regs + HW_BCH_CTRL_CLR);
2633 gpmi_clear_bch(this);
2634
2635 ret = 0;
2636
2637unmap:
2638 for (i = 0; i < this->ntransfers; i++) {
2639 struct gpmi_transfer *transfer = &this->transfers[i];
2640
2641 if (transfer->direction != DMA_NONE)
2642 dma_unmap_sg(this->dev, &transfer->sgl, 1,
2643 transfer->direction);
2644 }
2645
2646 if (!ret && buf_read && !direct)
2647 memcpy(buf_read, this->data_buffer_dma,
2648 gpmi_raw_len_to_len(this, buf_len));
2649
2650 this->bch = false;
2651
2652out_pm:
2653 pm_runtime_mark_last_busy(this->dev);
2654 pm_runtime_put_autosuspend(this->dev);
2655
2656 return ret;
2657}
2658
2659static const struct nand_controller_ops gpmi_nand_controller_ops = {
2660 .attach_chip = gpmi_nand_attach_chip,
2661 .setup_interface = gpmi_setup_interface,
2662 .exec_op = gpmi_nfc_exec_op,
2663};
2664
2665static int gpmi_nand_init(struct gpmi_nand_data *this)
2666{
2667 struct nand_chip *chip = &this->nand;
2668 struct mtd_info *mtd = nand_to_mtd(chip);
2669 int ret;
2670
2671
2672 mtd->name = "gpmi-nand";
2673 mtd->dev.parent = this->dev;
2674
2675
2676 nand_set_controller_data(chip, this);
2677 nand_set_flash_node(chip, this->pdev->dev.of_node);
2678 chip->legacy.block_markbad = gpmi_block_markbad;
2679 chip->badblock_pattern = &gpmi_bbt_descr;
2680 chip->options |= NAND_NO_SUBPAGE_WRITE;
2681
2682
2683 this->swap_block_mark = !GPMI_IS_MX23(this);
2684
2685
2686
2687
2688
2689 this->bch_geometry.payload_size = 1024;
2690 this->bch_geometry.auxiliary_size = 128;
2691 ret = gpmi_alloc_dma_buffer(this);
2692 if (ret)
2693 return ret;
2694
2695 nand_controller_init(&this->base);
2696 this->base.ops = &gpmi_nand_controller_ops;
2697 chip->controller = &this->base;
2698
2699 ret = nand_scan(chip, GPMI_IS_MX6(this) ? 2 : 1);
2700 if (ret)
2701 goto err_out;
2702
2703 ret = nand_boot_init(this);
2704 if (ret)
2705 goto err_nand_cleanup;
2706 ret = nand_create_bbt(chip);
2707 if (ret)
2708 goto err_nand_cleanup;
2709
2710 ret = mtd_device_register(mtd, NULL, 0);
2711 if (ret)
2712 goto err_nand_cleanup;
2713 return 0;
2714
2715err_nand_cleanup:
2716 nand_cleanup(chip);
2717err_out:
2718 gpmi_free_dma_buffer(this);
2719 return ret;
2720}
2721
2722static const struct of_device_id gpmi_nand_id_table[] = {
2723 { .compatible = "fsl,imx23-gpmi-nand", .data = &gpmi_devdata_imx23, },
2724 { .compatible = "fsl,imx28-gpmi-nand", .data = &gpmi_devdata_imx28, },
2725 { .compatible = "fsl,imx6q-gpmi-nand", .data = &gpmi_devdata_imx6q, },
2726 { .compatible = "fsl,imx6sx-gpmi-nand", .data = &gpmi_devdata_imx6sx, },
2727 { .compatible = "fsl,imx7d-gpmi-nand", .data = &gpmi_devdata_imx7d,},
2728 {}
2729};
2730MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
2731
2732static int gpmi_nand_probe(struct platform_device *pdev)
2733{
2734 struct gpmi_nand_data *this;
2735 int ret;
2736
2737 this = devm_kzalloc(&pdev->dev, sizeof(*this), GFP_KERNEL);
2738 if (!this)
2739 return -ENOMEM;
2740
2741 this->devdata = of_device_get_match_data(&pdev->dev);
2742 platform_set_drvdata(pdev, this);
2743 this->pdev = pdev;
2744 this->dev = &pdev->dev;
2745
2746 ret = acquire_resources(this);
2747 if (ret)
2748 goto exit_acquire_resources;
2749
2750 ret = __gpmi_enable_clk(this, true);
2751 if (ret)
2752 goto exit_acquire_resources;
2753
2754 pm_runtime_set_autosuspend_delay(&pdev->dev, 500);
2755 pm_runtime_use_autosuspend(&pdev->dev);
2756 pm_runtime_set_active(&pdev->dev);
2757 pm_runtime_enable(&pdev->dev);
2758 pm_runtime_get_sync(&pdev->dev);
2759
2760 ret = gpmi_init(this);
2761 if (ret)
2762 goto exit_nfc_init;
2763
2764 ret = gpmi_nand_init(this);
2765 if (ret)
2766 goto exit_nfc_init;
2767
2768 pm_runtime_mark_last_busy(&pdev->dev);
2769 pm_runtime_put_autosuspend(&pdev->dev);
2770
2771 dev_info(this->dev, "driver registered.\n");
2772
2773 return 0;
2774
2775exit_nfc_init:
2776 pm_runtime_put(&pdev->dev);
2777 pm_runtime_disable(&pdev->dev);
2778 release_resources(this);
2779exit_acquire_resources:
2780
2781 return ret;
2782}
2783
2784static int gpmi_nand_remove(struct platform_device *pdev)
2785{
2786 struct gpmi_nand_data *this = platform_get_drvdata(pdev);
2787 struct nand_chip *chip = &this->nand;
2788 int ret;
2789
2790 pm_runtime_put_sync(&pdev->dev);
2791 pm_runtime_disable(&pdev->dev);
2792
2793 ret = mtd_device_unregister(nand_to_mtd(chip));
2794 WARN_ON(ret);
2795 nand_cleanup(chip);
2796 gpmi_free_dma_buffer(this);
2797 release_resources(this);
2798 return 0;
2799}
2800
2801#ifdef CONFIG_PM_SLEEP
2802static int gpmi_pm_suspend(struct device *dev)
2803{
2804 struct gpmi_nand_data *this = dev_get_drvdata(dev);
2805
2806 release_dma_channels(this);
2807 return 0;
2808}
2809
2810static int gpmi_pm_resume(struct device *dev)
2811{
2812 struct gpmi_nand_data *this = dev_get_drvdata(dev);
2813 int ret;
2814
2815 ret = acquire_dma_channels(this);
2816 if (ret < 0)
2817 return ret;
2818
2819
2820 ret = gpmi_init(this);
2821 if (ret) {
2822 dev_err(this->dev, "Error setting GPMI : %d\n", ret);
2823 return ret;
2824 }
2825
2826
2827 if (this->hw.clk_rate)
2828 this->hw.must_apply_timings = true;
2829
2830
2831 ret = bch_set_geometry(this);
2832 if (ret) {
2833 dev_err(this->dev, "Error setting BCH : %d\n", ret);
2834 return ret;
2835 }
2836
2837 return 0;
2838}
2839#endif
2840
2841static int __maybe_unused gpmi_runtime_suspend(struct device *dev)
2842{
2843 struct gpmi_nand_data *this = dev_get_drvdata(dev);
2844
2845 return __gpmi_enable_clk(this, false);
2846}
2847
2848static int __maybe_unused gpmi_runtime_resume(struct device *dev)
2849{
2850 struct gpmi_nand_data *this = dev_get_drvdata(dev);
2851
2852 return __gpmi_enable_clk(this, true);
2853}
2854
2855static const struct dev_pm_ops gpmi_pm_ops = {
2856 SET_SYSTEM_SLEEP_PM_OPS(gpmi_pm_suspend, gpmi_pm_resume)
2857 SET_RUNTIME_PM_OPS(gpmi_runtime_suspend, gpmi_runtime_resume, NULL)
2858};
2859
2860static struct platform_driver gpmi_nand_driver = {
2861 .driver = {
2862 .name = "gpmi-nand",
2863 .pm = &gpmi_pm_ops,
2864 .of_match_table = gpmi_nand_id_table,
2865 },
2866 .probe = gpmi_nand_probe,
2867 .remove = gpmi_nand_remove,
2868};
2869module_platform_driver(gpmi_nand_driver);
2870
2871MODULE_AUTHOR("Freescale Semiconductor, Inc.");
2872MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
2873MODULE_LICENSE("GPL");
2874