1
2
3
4
5
6
7
8#include <linux/clk.h>
9#include <linux/slab.h>
10#include <linux/sched/task_stack.h>
11#include <linux/interrupt.h>
12#include <linux/module.h>
13#include <linux/mtd/partitions.h>
14#include <linux/of.h>
15#include <linux/of_device.h>
16#include "gpmi-nand.h"
17#include "bch-regs.h"
18
19
20#define GPMI_NAND_GPMI_REGS_ADDR_RES_NAME "gpmi-nand"
21#define GPMI_NAND_BCH_REGS_ADDR_RES_NAME "bch"
22#define GPMI_NAND_BCH_INTERRUPT_RES_NAME "bch"
23
24
25static uint8_t scan_ff_pattern[] = { 0xff };
26static struct nand_bbt_descr gpmi_bbt_descr = {
27 .options = 0,
28 .offs = 0,
29 .len = 1,
30 .pattern = scan_ff_pattern
31};
32
33
34
35
36
37static int gpmi_ooblayout_ecc(struct mtd_info *mtd, int section,
38 struct mtd_oob_region *oobregion)
39{
40 struct nand_chip *chip = mtd_to_nand(mtd);
41 struct gpmi_nand_data *this = nand_get_controller_data(chip);
42 struct bch_geometry *geo = &this->bch_geometry;
43
44 if (section)
45 return -ERANGE;
46
47 oobregion->offset = 0;
48 oobregion->length = geo->page_size - mtd->writesize;
49
50 return 0;
51}
52
53static int gpmi_ooblayout_free(struct mtd_info *mtd, int section,
54 struct mtd_oob_region *oobregion)
55{
56 struct nand_chip *chip = mtd_to_nand(mtd);
57 struct gpmi_nand_data *this = nand_get_controller_data(chip);
58 struct bch_geometry *geo = &this->bch_geometry;
59
60 if (section)
61 return -ERANGE;
62
63
64 if (geo->page_size < mtd->writesize + mtd->oobsize) {
65 oobregion->offset = geo->page_size - mtd->writesize;
66 oobregion->length = mtd->oobsize - oobregion->offset;
67 }
68
69 return 0;
70}
71
72static const char * const gpmi_clks_for_mx2x[] = {
73 "gpmi_io",
74};
75
76static const struct mtd_ooblayout_ops gpmi_ooblayout_ops = {
77 .ecc = gpmi_ooblayout_ecc,
78 .free = gpmi_ooblayout_free,
79};
80
81static const struct gpmi_devdata gpmi_devdata_imx23 = {
82 .type = IS_MX23,
83 .bch_max_ecc_strength = 20,
84 .max_chain_delay = 16000,
85 .clks = gpmi_clks_for_mx2x,
86 .clks_count = ARRAY_SIZE(gpmi_clks_for_mx2x),
87};
88
89static const struct gpmi_devdata gpmi_devdata_imx28 = {
90 .type = IS_MX28,
91 .bch_max_ecc_strength = 20,
92 .max_chain_delay = 16000,
93 .clks = gpmi_clks_for_mx2x,
94 .clks_count = ARRAY_SIZE(gpmi_clks_for_mx2x),
95};
96
97static const char * const gpmi_clks_for_mx6[] = {
98 "gpmi_io", "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch",
99};
100
101static const struct gpmi_devdata gpmi_devdata_imx6q = {
102 .type = IS_MX6Q,
103 .bch_max_ecc_strength = 40,
104 .max_chain_delay = 12000,
105 .clks = gpmi_clks_for_mx6,
106 .clks_count = ARRAY_SIZE(gpmi_clks_for_mx6),
107};
108
109static const struct gpmi_devdata gpmi_devdata_imx6sx = {
110 .type = IS_MX6SX,
111 .bch_max_ecc_strength = 62,
112 .max_chain_delay = 12000,
113 .clks = gpmi_clks_for_mx6,
114 .clks_count = ARRAY_SIZE(gpmi_clks_for_mx6),
115};
116
117static const char * const gpmi_clks_for_mx7d[] = {
118 "gpmi_io", "gpmi_bch_apb",
119};
120
121static const struct gpmi_devdata gpmi_devdata_imx7d = {
122 .type = IS_MX7D,
123 .bch_max_ecc_strength = 62,
124 .max_chain_delay = 12000,
125 .clks = gpmi_clks_for_mx7d,
126 .clks_count = ARRAY_SIZE(gpmi_clks_for_mx7d),
127};
128
129static irqreturn_t bch_irq(int irq, void *cookie)
130{
131 struct gpmi_nand_data *this = cookie;
132
133 gpmi_clear_bch(this);
134 complete(&this->bch_done);
135 return IRQ_HANDLED;
136}
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156static inline int get_ecc_strength(struct gpmi_nand_data *this)
157{
158 struct bch_geometry *geo = &this->bch_geometry;
159 struct mtd_info *mtd = nand_to_mtd(&this->nand);
160 int ecc_strength;
161
162 ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8)
163 / (geo->gf_len * geo->ecc_chunk_count);
164
165
166 return round_down(ecc_strength, 2);
167}
168
169static inline bool gpmi_check_ecc(struct gpmi_nand_data *this)
170{
171 struct bch_geometry *geo = &this->bch_geometry;
172
173
174 if (GPMI_IS_MX23(this) || GPMI_IS_MX28(this)) {
175
176 if (geo->gf_len == 14)
177 return false;
178 }
179 return geo->ecc_strength <= this->devdata->bch_max_ecc_strength;
180}
181
182
183
184
185
186
187
188static int set_geometry_by_ecc_info(struct gpmi_nand_data *this,
189 unsigned int ecc_strength,
190 unsigned int ecc_step)
191{
192 struct bch_geometry *geo = &this->bch_geometry;
193 struct nand_chip *chip = &this->nand;
194 struct mtd_info *mtd = nand_to_mtd(chip);
195 unsigned int block_mark_bit_offset;
196
197 switch (ecc_step) {
198 case SZ_512:
199 geo->gf_len = 13;
200 break;
201 case SZ_1K:
202 geo->gf_len = 14;
203 break;
204 default:
205 dev_err(this->dev,
206 "unsupported nand chip. ecc bits : %d, ecc size : %d\n",
207 chip->ecc_strength_ds, chip->ecc_step_ds);
208 return -EINVAL;
209 }
210 geo->ecc_chunk_size = ecc_step;
211 geo->ecc_strength = round_up(ecc_strength, 2);
212 if (!gpmi_check_ecc(this))
213 return -EINVAL;
214
215
216 if (geo->ecc_chunk_size < mtd->oobsize) {
217 dev_err(this->dev,
218 "unsupported nand chip. ecc size: %d, oob size : %d\n",
219 ecc_step, mtd->oobsize);
220 return -EINVAL;
221 }
222
223
224 geo->metadata_size = 10;
225
226 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276 geo->page_size = mtd->writesize + geo->metadata_size +
277 (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
278
279 geo->payload_size = mtd->writesize;
280
281 geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4);
282 geo->auxiliary_size = ALIGN(geo->metadata_size, 4)
283 + ALIGN(geo->ecc_chunk_count, 4);
284
285 if (!this->swap_block_mark)
286 return 0;
287
288
289 block_mark_bit_offset = mtd->writesize * 8 -
290 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
291 + geo->metadata_size * 8);
292
293 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
294 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
295 return 0;
296}
297
298static int legacy_set_geometry(struct gpmi_nand_data *this)
299{
300 struct bch_geometry *geo = &this->bch_geometry;
301 struct mtd_info *mtd = nand_to_mtd(&this->nand);
302 unsigned int metadata_size;
303 unsigned int status_size;
304 unsigned int block_mark_bit_offset;
305
306
307
308
309
310
311 geo->metadata_size = 10;
312
313
314 geo->gf_len = 13;
315
316
317 geo->ecc_chunk_size = 512;
318 while (geo->ecc_chunk_size < mtd->oobsize) {
319 geo->ecc_chunk_size *= 2;
320 geo->gf_len = 14;
321 }
322
323 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
324
325
326 geo->ecc_strength = get_ecc_strength(this);
327 if (!gpmi_check_ecc(this)) {
328 dev_err(this->dev,
329 "ecc strength: %d cannot be supported by the controller (%d)\n"
330 "try to use minimum ecc strength that NAND chip required\n",
331 geo->ecc_strength,
332 this->devdata->bch_max_ecc_strength);
333 return -EINVAL;
334 }
335
336 geo->page_size = mtd->writesize + geo->metadata_size +
337 (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
338 geo->payload_size = mtd->writesize;
339
340
341
342
343
344
345
346 metadata_size = ALIGN(geo->metadata_size, 4);
347 status_size = ALIGN(geo->ecc_chunk_count, 4);
348
349 geo->auxiliary_size = metadata_size + status_size;
350 geo->auxiliary_status_offset = metadata_size;
351
352 if (!this->swap_block_mark)
353 return 0;
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
399
400
401 block_mark_bit_offset = mtd->writesize * 8 -
402 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
403 + geo->metadata_size * 8);
404
405 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
406 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
407 return 0;
408}
409
410int common_nfc_set_geometry(struct gpmi_nand_data *this)
411{
412 struct nand_chip *chip = &this->nand;
413
414 if (chip->ecc.strength > 0 && chip->ecc.size > 0)
415 return set_geometry_by_ecc_info(this, chip->ecc.strength,
416 chip->ecc.size);
417
418 if ((of_property_read_bool(this->dev->of_node, "fsl,use-minimum-ecc"))
419 || legacy_set_geometry(this)) {
420 if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0))
421 return -EINVAL;
422
423 return set_geometry_by_ecc_info(this, chip->ecc_strength_ds,
424 chip->ecc_step_ds);
425 }
426
427 return 0;
428}
429
430struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
431{
432
433 return this->dma_chans[0];
434}
435
436
437bool prepare_data_dma(struct gpmi_nand_data *this, const void *buf, int len,
438 enum dma_data_direction dr)
439{
440 struct scatterlist *sgl = &this->data_sgl;
441 int ret;
442
443
444 if (virt_addr_valid(buf) && !object_is_on_stack(buf)) {
445 sg_init_one(sgl, buf, len);
446 ret = dma_map_sg(this->dev, sgl, 1, dr);
447 if (ret == 0)
448 goto map_fail;
449
450 return true;
451 }
452
453map_fail:
454
455 sg_init_one(sgl, this->data_buffer_dma, len);
456
457 if (dr == DMA_TO_DEVICE)
458 memcpy(this->data_buffer_dma, buf, len);
459
460 dma_map_sg(this->dev, sgl, 1, dr);
461
462 return false;
463}
464
465
466static void dma_irq_callback(void *param)
467{
468 struct gpmi_nand_data *this = param;
469 struct completion *dma_c = &this->dma_done;
470
471 complete(dma_c);
472}
473
474int start_dma_without_bch_irq(struct gpmi_nand_data *this,
475 struct dma_async_tx_descriptor *desc)
476{
477 struct completion *dma_c = &this->dma_done;
478 unsigned long timeout;
479
480 init_completion(dma_c);
481
482 desc->callback = dma_irq_callback;
483 desc->callback_param = this;
484 dmaengine_submit(desc);
485 dma_async_issue_pending(get_dma_chan(this));
486
487
488 timeout = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000));
489 if (!timeout) {
490 dev_err(this->dev, "DMA timeout, last DMA\n");
491 gpmi_dump_info(this);
492 return -ETIMEDOUT;
493 }
494 return 0;
495}
496
497
498
499
500
501
502
503
504int start_dma_with_bch_irq(struct gpmi_nand_data *this,
505 struct dma_async_tx_descriptor *desc)
506{
507 struct completion *bch_c = &this->bch_done;
508 unsigned long timeout;
509
510
511 init_completion(bch_c);
512
513
514 start_dma_without_bch_irq(this, desc);
515
516
517 timeout = wait_for_completion_timeout(bch_c, msecs_to_jiffies(1000));
518 if (!timeout) {
519 dev_err(this->dev, "BCH timeout\n");
520 gpmi_dump_info(this);
521 return -ETIMEDOUT;
522 }
523 return 0;
524}
525
526static int acquire_register_block(struct gpmi_nand_data *this,
527 const char *res_name)
528{
529 struct platform_device *pdev = this->pdev;
530 struct resources *res = &this->resources;
531 struct resource *r;
532 void __iomem *p;
533
534 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name);
535 p = devm_ioremap_resource(&pdev->dev, r);
536 if (IS_ERR(p))
537 return PTR_ERR(p);
538
539 if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
540 res->gpmi_regs = p;
541 else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
542 res->bch_regs = p;
543 else
544 dev_err(this->dev, "unknown resource name : %s\n", res_name);
545
546 return 0;
547}
548
549static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
550{
551 struct platform_device *pdev = this->pdev;
552 const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
553 struct resource *r;
554 int err;
555
556 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name);
557 if (!r) {
558 dev_err(this->dev, "Can't get resource for %s\n", res_name);
559 return -ENODEV;
560 }
561
562 err = devm_request_irq(this->dev, r->start, irq_h, 0, res_name, this);
563 if (err)
564 dev_err(this->dev, "error requesting BCH IRQ\n");
565
566 return err;
567}
568
569static void release_dma_channels(struct gpmi_nand_data *this)
570{
571 unsigned int i;
572 for (i = 0; i < DMA_CHANS; i++)
573 if (this->dma_chans[i]) {
574 dma_release_channel(this->dma_chans[i]);
575 this->dma_chans[i] = NULL;
576 }
577}
578
579static int acquire_dma_channels(struct gpmi_nand_data *this)
580{
581 struct platform_device *pdev = this->pdev;
582 struct dma_chan *dma_chan;
583
584
585 dma_chan = dma_request_slave_channel(&pdev->dev, "rx-tx");
586 if (!dma_chan) {
587 dev_err(this->dev, "Failed to request DMA channel.\n");
588 goto acquire_err;
589 }
590
591 this->dma_chans[0] = dma_chan;
592 return 0;
593
594acquire_err:
595 release_dma_channels(this);
596 return -EINVAL;
597}
598
599static int gpmi_get_clks(struct gpmi_nand_data *this)
600{
601 struct resources *r = &this->resources;
602 struct clk *clk;
603 int err, i;
604
605 for (i = 0; i < this->devdata->clks_count; i++) {
606 clk = devm_clk_get(this->dev, this->devdata->clks[i]);
607 if (IS_ERR(clk)) {
608 err = PTR_ERR(clk);
609 goto err_clock;
610 }
611
612 r->clock[i] = clk;
613 }
614
615 if (GPMI_IS_MX6(this))
616
617
618
619
620
621
622 clk_set_rate(r->clock[0], 22000000);
623
624 return 0;
625
626err_clock:
627 dev_dbg(this->dev, "failed in finding the clocks.\n");
628 return err;
629}
630
631static int acquire_resources(struct gpmi_nand_data *this)
632{
633 int ret;
634
635 ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
636 if (ret)
637 goto exit_regs;
638
639 ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
640 if (ret)
641 goto exit_regs;
642
643 ret = acquire_bch_irq(this, bch_irq);
644 if (ret)
645 goto exit_regs;
646
647 ret = acquire_dma_channels(this);
648 if (ret)
649 goto exit_regs;
650
651 ret = gpmi_get_clks(this);
652 if (ret)
653 goto exit_clock;
654 return 0;
655
656exit_clock:
657 release_dma_channels(this);
658exit_regs:
659 return ret;
660}
661
662static void release_resources(struct gpmi_nand_data *this)
663{
664 release_dma_channels(this);
665}
666
667static int send_page_prepare(struct gpmi_nand_data *this,
668 const void *source, unsigned length,
669 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
670 const void **use_virt, dma_addr_t *use_phys)
671{
672 struct device *dev = this->dev;
673
674 if (virt_addr_valid(source)) {
675 dma_addr_t source_phys;
676
677 source_phys = dma_map_single(dev, (void *)source, length,
678 DMA_TO_DEVICE);
679 if (dma_mapping_error(dev, source_phys)) {
680 if (alt_size < length) {
681 dev_err(dev, "Alternate buffer is too small\n");
682 return -ENOMEM;
683 }
684 goto map_failed;
685 }
686 *use_virt = source;
687 *use_phys = source_phys;
688 return 0;
689 }
690map_failed:
691
692
693
694
695 memcpy(alt_virt, source, length);
696
697 *use_virt = alt_virt;
698 *use_phys = alt_phys;
699 return 0;
700}
701
702static void send_page_end(struct gpmi_nand_data *this,
703 const void *source, unsigned length,
704 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
705 const void *used_virt, dma_addr_t used_phys)
706{
707 struct device *dev = this->dev;
708 if (used_virt == source)
709 dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
710}
711
712static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
713{
714 struct device *dev = this->dev;
715
716 if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
717 dma_free_coherent(dev, this->page_buffer_size,
718 this->page_buffer_virt,
719 this->page_buffer_phys);
720 kfree(this->cmd_buffer);
721 kfree(this->data_buffer_dma);
722 kfree(this->raw_buffer);
723
724 this->cmd_buffer = NULL;
725 this->data_buffer_dma = NULL;
726 this->raw_buffer = NULL;
727 this->page_buffer_virt = NULL;
728 this->page_buffer_size = 0;
729}
730
731
732static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
733{
734 struct bch_geometry *geo = &this->bch_geometry;
735 struct device *dev = this->dev;
736 struct mtd_info *mtd = nand_to_mtd(&this->nand);
737
738
739 this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
740 if (this->cmd_buffer == NULL)
741 goto error_alloc;
742
743
744
745
746
747
748
749
750
751 this->data_buffer_dma = kzalloc(mtd->writesize ?: PAGE_SIZE,
752 GFP_DMA | GFP_KERNEL);
753 if (this->data_buffer_dma == NULL)
754 goto error_alloc;
755
756
757
758
759
760
761
762
763
764 this->page_buffer_size = geo->payload_size + geo->auxiliary_size;
765 this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
766 &this->page_buffer_phys, GFP_DMA);
767 if (!this->page_buffer_virt)
768 goto error_alloc;
769
770 this->raw_buffer = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL);
771 if (!this->raw_buffer)
772 goto error_alloc;
773
774
775 this->payload_virt = this->page_buffer_virt;
776 this->payload_phys = this->page_buffer_phys;
777 this->auxiliary_virt = this->payload_virt + geo->payload_size;
778 this->auxiliary_phys = this->payload_phys + geo->payload_size;
779 return 0;
780
781error_alloc:
782 gpmi_free_dma_buffer(this);
783 return -ENOMEM;
784}
785
786static void gpmi_cmd_ctrl(struct nand_chip *chip, int data, unsigned int ctrl)
787{
788 struct gpmi_nand_data *this = nand_get_controller_data(chip);
789 int ret;
790
791
792
793
794
795
796
797
798
799
800
801
802 if ((ctrl & (NAND_ALE | NAND_CLE))) {
803 if (data != NAND_CMD_NONE)
804 this->cmd_buffer[this->command_length++] = data;
805 return;
806 }
807
808 if (!this->command_length)
809 return;
810
811 ret = gpmi_send_command(this);
812 if (ret)
813 dev_err(this->dev, "Chip: %u, Error %d\n",
814 this->current_chip, ret);
815
816 this->command_length = 0;
817}
818
819static int gpmi_dev_ready(struct nand_chip *chip)
820{
821 struct gpmi_nand_data *this = nand_get_controller_data(chip);
822
823 return gpmi_is_ready(this, this->current_chip);
824}
825
826static void gpmi_select_chip(struct nand_chip *chip, int chipnr)
827{
828 struct gpmi_nand_data *this = nand_get_controller_data(chip);
829 int ret;
830
831
832
833
834
835 if (this->current_chip < 0 && chipnr >= 0) {
836 ret = gpmi_enable_clk(this);
837 if (ret)
838 dev_err(this->dev, "Failed to enable the clock\n");
839 } else if (this->current_chip >= 0 && chipnr < 0) {
840 ret = gpmi_disable_clk(this);
841 if (ret)
842 dev_err(this->dev, "Failed to disable the clock\n");
843 }
844
845
846
847
848
849
850
851 if (chipnr >= 0 && this->hw.must_apply_timings) {
852 this->hw.must_apply_timings = false;
853 gpmi_nfc_apply_timings(this);
854 }
855
856 this->current_chip = chipnr;
857}
858
859static void gpmi_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
860{
861 struct gpmi_nand_data *this = nand_get_controller_data(chip);
862
863 dev_dbg(this->dev, "len is %d\n", len);
864
865 gpmi_read_data(this, buf, len);
866}
867
868static void gpmi_write_buf(struct nand_chip *chip, const uint8_t *buf, int len)
869{
870 struct gpmi_nand_data *this = nand_get_controller_data(chip);
871
872 dev_dbg(this->dev, "len is %d\n", len);
873
874 gpmi_send_data(this, buf, len);
875}
876
877static uint8_t gpmi_read_byte(struct nand_chip *chip)
878{
879 struct gpmi_nand_data *this = nand_get_controller_data(chip);
880 uint8_t *buf = this->data_buffer_dma;
881
882 gpmi_read_buf(chip, buf, 1);
883 return buf[0];
884}
885
886
887
888
889
890
891static void block_mark_swapping(struct gpmi_nand_data *this,
892 void *payload, void *auxiliary)
893{
894 struct bch_geometry *nfc_geo = &this->bch_geometry;
895 unsigned char *p;
896 unsigned char *a;
897 unsigned int bit;
898 unsigned char mask;
899 unsigned char from_data;
900 unsigned char from_oob;
901
902 if (!this->swap_block_mark)
903 return;
904
905
906
907
908
909 bit = nfc_geo->block_mark_bit_offset;
910 p = payload + nfc_geo->block_mark_byte_offset;
911 a = auxiliary;
912
913
914
915
916
917
918
919 from_data = (p[0] >> bit) | (p[1] << (8 - bit));
920
921
922 from_oob = a[0];
923
924
925 a[0] = from_data;
926
927 mask = (0x1 << bit) - 1;
928 p[0] = (p[0] & mask) | (from_oob << bit);
929
930 mask = ~0 << bit;
931 p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
932}
933
934static int gpmi_ecc_read_page_data(struct nand_chip *chip,
935 uint8_t *buf, int oob_required,
936 int page)
937{
938 struct gpmi_nand_data *this = nand_get_controller_data(chip);
939 struct bch_geometry *nfc_geo = &this->bch_geometry;
940 struct mtd_info *mtd = nand_to_mtd(chip);
941 dma_addr_t payload_phys;
942 unsigned int i;
943 unsigned char *status;
944 unsigned int max_bitflips = 0;
945 int ret;
946 bool direct = false;
947
948 dev_dbg(this->dev, "page number is : %d\n", page);
949
950 payload_phys = this->payload_phys;
951
952 if (virt_addr_valid(buf)) {
953 dma_addr_t dest_phys;
954
955 dest_phys = dma_map_single(this->dev, buf, nfc_geo->payload_size,
956 DMA_FROM_DEVICE);
957 if (!dma_mapping_error(this->dev, dest_phys)) {
958 payload_phys = dest_phys;
959 direct = true;
960 }
961 }
962
963
964 ret = gpmi_read_page(this, payload_phys, this->auxiliary_phys);
965
966 if (direct)
967 dma_unmap_single(this->dev, payload_phys, nfc_geo->payload_size,
968 DMA_FROM_DEVICE);
969
970 if (ret) {
971 dev_err(this->dev, "Error in ECC-based read: %d\n", ret);
972 return ret;
973 }
974
975
976 status = this->auxiliary_virt + nfc_geo->auxiliary_status_offset;
977
978 if (!direct)
979 memcpy(buf, this->payload_virt, nfc_geo->payload_size);
980
981 for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
982 if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
983 continue;
984
985 if (*status == STATUS_UNCORRECTABLE) {
986 int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
987 u8 *eccbuf = this->raw_buffer;
988 int offset, bitoffset;
989 int eccbytes;
990 int flips;
991
992
993 offset = nfc_geo->metadata_size * 8;
994 offset += ((8 * nfc_geo->ecc_chunk_size) + eccbits) * (i + 1);
995 offset -= eccbits;
996 bitoffset = offset % 8;
997 eccbytes = DIV_ROUND_UP(offset + eccbits, 8);
998 offset /= 8;
999 eccbytes -= offset;
1000 nand_change_read_column_op(chip, offset, eccbuf,
1001 eccbytes, false);
1002
1003
1004
1005
1006
1007
1008
1009
1010 if (bitoffset)
1011 eccbuf[0] |= GENMASK(bitoffset - 1, 0);
1012
1013 bitoffset = (bitoffset + eccbits) % 8;
1014 if (bitoffset)
1015 eccbuf[eccbytes - 1] |= GENMASK(7, bitoffset);
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028 if (i == 0) {
1029
1030 flips = nand_check_erased_ecc_chunk(
1031 buf + i * nfc_geo->ecc_chunk_size,
1032 nfc_geo->ecc_chunk_size,
1033 eccbuf, eccbytes,
1034 this->auxiliary_virt,
1035 nfc_geo->metadata_size,
1036 nfc_geo->ecc_strength);
1037 } else {
1038 flips = nand_check_erased_ecc_chunk(
1039 buf + i * nfc_geo->ecc_chunk_size,
1040 nfc_geo->ecc_chunk_size,
1041 eccbuf, eccbytes,
1042 NULL, 0,
1043 nfc_geo->ecc_strength);
1044 }
1045
1046 if (flips > 0) {
1047 max_bitflips = max_t(unsigned int, max_bitflips,
1048 flips);
1049 mtd->ecc_stats.corrected += flips;
1050 continue;
1051 }
1052
1053 mtd->ecc_stats.failed++;
1054 continue;
1055 }
1056
1057 mtd->ecc_stats.corrected += *status;
1058 max_bitflips = max_t(unsigned int, max_bitflips, *status);
1059 }
1060
1061
1062 block_mark_swapping(this, buf, this->auxiliary_virt);
1063
1064 if (oob_required) {
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075 memset(chip->oob_poi, ~0, mtd->oobsize);
1076 chip->oob_poi[0] = ((uint8_t *)this->auxiliary_virt)[0];
1077 }
1078
1079 return max_bitflips;
1080}
1081
1082static int gpmi_ecc_read_page(struct nand_chip *chip, uint8_t *buf,
1083 int oob_required, int page)
1084{
1085 nand_read_page_op(chip, page, 0, NULL, 0);
1086
1087 return gpmi_ecc_read_page_data(chip, buf, oob_required, page);
1088}
1089
1090
1091static int gpmi_ecc_read_subpage(struct nand_chip *chip, uint32_t offs,
1092 uint32_t len, uint8_t *buf, int page)
1093{
1094 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1095 void __iomem *bch_regs = this->resources.bch_regs;
1096 struct bch_geometry old_geo = this->bch_geometry;
1097 struct bch_geometry *geo = &this->bch_geometry;
1098 int size = chip->ecc.size;
1099 int meta, n, page_size;
1100 u32 r1_old, r2_old, r1_new, r2_new;
1101 unsigned int max_bitflips;
1102 int first, last, marker_pos;
1103 int ecc_parity_size;
1104 int col = 0;
1105 int old_swap_block_mark = this->swap_block_mark;
1106
1107
1108 ecc_parity_size = geo->gf_len * geo->ecc_strength / 8;
1109
1110
1111 first = offs / size;
1112 last = (offs + len - 1) / size;
1113
1114 if (this->swap_block_mark) {
1115
1116
1117
1118
1119
1120
1121
1122 marker_pos = geo->block_mark_byte_offset / size;
1123 if (last >= marker_pos && first <= marker_pos) {
1124 dev_dbg(this->dev,
1125 "page:%d, first:%d, last:%d, marker at:%d\n",
1126 page, first, last, marker_pos);
1127 return gpmi_ecc_read_page(chip, buf, 0, page);
1128 }
1129 }
1130
1131 meta = geo->metadata_size;
1132 if (first) {
1133 col = meta + (size + ecc_parity_size) * first;
1134 meta = 0;
1135 buf = buf + first * size;
1136 }
1137
1138 nand_read_page_op(chip, page, col, NULL, 0);
1139
1140
1141 r1_old = r1_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT0);
1142 r2_old = r2_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT1);
1143
1144
1145 n = last - first + 1;
1146 page_size = meta + (size + ecc_parity_size) * n;
1147
1148 r1_new &= ~(BM_BCH_FLASH0LAYOUT0_NBLOCKS |
1149 BM_BCH_FLASH0LAYOUT0_META_SIZE);
1150 r1_new |= BF_BCH_FLASH0LAYOUT0_NBLOCKS(n - 1)
1151 | BF_BCH_FLASH0LAYOUT0_META_SIZE(meta);
1152 writel(r1_new, bch_regs + HW_BCH_FLASH0LAYOUT0);
1153
1154 r2_new &= ~BM_BCH_FLASH0LAYOUT1_PAGE_SIZE;
1155 r2_new |= BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size);
1156 writel(r2_new, bch_regs + HW_BCH_FLASH0LAYOUT1);
1157
1158 geo->ecc_chunk_count = n;
1159 geo->payload_size = n * size;
1160 geo->page_size = page_size;
1161 geo->auxiliary_status_offset = ALIGN(meta, 4);
1162
1163 dev_dbg(this->dev, "page:%d(%d:%d)%d, chunk:(%d:%d), BCH PG size:%d\n",
1164 page, offs, len, col, first, n, page_size);
1165
1166
1167 this->swap_block_mark = false;
1168 max_bitflips = gpmi_ecc_read_page_data(chip, buf, 0, page);
1169
1170
1171 writel(r1_old, bch_regs + HW_BCH_FLASH0LAYOUT0);
1172 writel(r2_old, bch_regs + HW_BCH_FLASH0LAYOUT1);
1173 this->bch_geometry = old_geo;
1174 this->swap_block_mark = old_swap_block_mark;
1175
1176 return max_bitflips;
1177}
1178
1179static int gpmi_ecc_write_page(struct nand_chip *chip, const uint8_t *buf,
1180 int oob_required, int page)
1181{
1182 struct mtd_info *mtd = nand_to_mtd(chip);
1183 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1184 struct bch_geometry *nfc_geo = &this->bch_geometry;
1185 const void *payload_virt;
1186 dma_addr_t payload_phys;
1187 const void *auxiliary_virt;
1188 dma_addr_t auxiliary_phys;
1189 int ret;
1190
1191 dev_dbg(this->dev, "ecc write page.\n");
1192
1193 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
1194
1195 if (this->swap_block_mark) {
1196
1197
1198
1199
1200
1201 memcpy(this->payload_virt, buf, mtd->writesize);
1202 payload_virt = this->payload_virt;
1203 payload_phys = this->payload_phys;
1204
1205 memcpy(this->auxiliary_virt, chip->oob_poi,
1206 nfc_geo->auxiliary_size);
1207 auxiliary_virt = this->auxiliary_virt;
1208 auxiliary_phys = this->auxiliary_phys;
1209
1210
1211 block_mark_swapping(this,
1212 (void *)payload_virt, (void *)auxiliary_virt);
1213 } else {
1214
1215
1216
1217
1218 ret = send_page_prepare(this,
1219 buf, mtd->writesize,
1220 this->payload_virt, this->payload_phys,
1221 nfc_geo->payload_size,
1222 &payload_virt, &payload_phys);
1223 if (ret) {
1224 dev_err(this->dev, "Inadequate payload DMA buffer\n");
1225 return 0;
1226 }
1227
1228 ret = send_page_prepare(this,
1229 chip->oob_poi, mtd->oobsize,
1230 this->auxiliary_virt, this->auxiliary_phys,
1231 nfc_geo->auxiliary_size,
1232 &auxiliary_virt, &auxiliary_phys);
1233 if (ret) {
1234 dev_err(this->dev, "Inadequate auxiliary DMA buffer\n");
1235 goto exit_auxiliary;
1236 }
1237 }
1238
1239
1240 ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
1241 if (ret)
1242 dev_err(this->dev, "Error in ECC-based write: %d\n", ret);
1243
1244 if (!this->swap_block_mark) {
1245 send_page_end(this, chip->oob_poi, mtd->oobsize,
1246 this->auxiliary_virt, this->auxiliary_phys,
1247 nfc_geo->auxiliary_size,
1248 auxiliary_virt, auxiliary_phys);
1249exit_auxiliary:
1250 send_page_end(this, buf, mtd->writesize,
1251 this->payload_virt, this->payload_phys,
1252 nfc_geo->payload_size,
1253 payload_virt, payload_phys);
1254 }
1255
1256 if (ret)
1257 return ret;
1258
1259 return nand_prog_page_end_op(chip);
1260}
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322static int gpmi_ecc_read_oob(struct nand_chip *chip, int page)
1323{
1324 struct mtd_info *mtd = nand_to_mtd(chip);
1325 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1326
1327 dev_dbg(this->dev, "page number is %d\n", page);
1328
1329 memset(chip->oob_poi, ~0, mtd->oobsize);
1330
1331
1332 nand_read_page_op(chip, page, mtd->writesize, NULL, 0);
1333 chip->legacy.read_buf(chip, chip->oob_poi, mtd->oobsize);
1334
1335
1336
1337
1338
1339
1340 if (GPMI_IS_MX23(this)) {
1341
1342 nand_read_page_op(chip, page, 0, NULL, 0);
1343 chip->oob_poi[0] = chip->legacy.read_byte(chip);
1344 }
1345
1346 return 0;
1347}
1348
1349static int gpmi_ecc_write_oob(struct nand_chip *chip, int page)
1350{
1351 struct mtd_info *mtd = nand_to_mtd(chip);
1352 struct mtd_oob_region of = { };
1353
1354
1355 mtd_ooblayout_free(mtd, 0, &of);
1356 if (!of.length)
1357 return -EPERM;
1358
1359 if (!nand_is_slc(chip))
1360 return -EPERM;
1361
1362 return nand_prog_page_op(chip, page, mtd->writesize + of.offset,
1363 chip->oob_poi + of.offset, of.length);
1364}
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378static int gpmi_ecc_read_page_raw(struct nand_chip *chip, uint8_t *buf,
1379 int oob_required, int page)
1380{
1381 struct mtd_info *mtd = nand_to_mtd(chip);
1382 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1383 struct bch_geometry *nfc_geo = &this->bch_geometry;
1384 int eccsize = nfc_geo->ecc_chunk_size;
1385 int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
1386 u8 *tmp_buf = this->raw_buffer;
1387 size_t src_bit_off;
1388 size_t oob_bit_off;
1389 size_t oob_byte_off;
1390 uint8_t *oob = chip->oob_poi;
1391 int step;
1392
1393 nand_read_page_op(chip, page, 0, tmp_buf,
1394 mtd->writesize + mtd->oobsize);
1395
1396
1397
1398
1399
1400
1401
1402
1403 if (this->swap_block_mark)
1404 swap(tmp_buf[0], tmp_buf[mtd->writesize]);
1405
1406
1407
1408
1409
1410 if (oob_required)
1411 memcpy(oob, tmp_buf, nfc_geo->metadata_size);
1412
1413 oob_bit_off = nfc_geo->metadata_size * 8;
1414 src_bit_off = oob_bit_off;
1415
1416
1417 for (step = 0; step < nfc_geo->ecc_chunk_count; step++) {
1418 if (buf)
1419 gpmi_copy_bits(buf, step * eccsize * 8,
1420 tmp_buf, src_bit_off,
1421 eccsize * 8);
1422 src_bit_off += eccsize * 8;
1423
1424
1425 if (step == nfc_geo->ecc_chunk_count - 1 &&
1426 (oob_bit_off + eccbits) % 8)
1427 eccbits += 8 - ((oob_bit_off + eccbits) % 8);
1428
1429 if (oob_required)
1430 gpmi_copy_bits(oob, oob_bit_off,
1431 tmp_buf, src_bit_off,
1432 eccbits);
1433
1434 src_bit_off += eccbits;
1435 oob_bit_off += eccbits;
1436 }
1437
1438 if (oob_required) {
1439 oob_byte_off = oob_bit_off / 8;
1440
1441 if (oob_byte_off < mtd->oobsize)
1442 memcpy(oob + oob_byte_off,
1443 tmp_buf + mtd->writesize + oob_byte_off,
1444 mtd->oobsize - oob_byte_off);
1445 }
1446
1447 return 0;
1448}
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462static int gpmi_ecc_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
1463 int oob_required, int page)
1464{
1465 struct mtd_info *mtd = nand_to_mtd(chip);
1466 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1467 struct bch_geometry *nfc_geo = &this->bch_geometry;
1468 int eccsize = nfc_geo->ecc_chunk_size;
1469 int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len;
1470 u8 *tmp_buf = this->raw_buffer;
1471 uint8_t *oob = chip->oob_poi;
1472 size_t dst_bit_off;
1473 size_t oob_bit_off;
1474 size_t oob_byte_off;
1475 int step;
1476
1477
1478
1479
1480
1481
1482 if (!buf || !oob_required)
1483 memset(tmp_buf, 0xff, mtd->writesize + mtd->oobsize);
1484
1485
1486
1487
1488
1489 memcpy(tmp_buf, oob, nfc_geo->metadata_size);
1490 oob_bit_off = nfc_geo->metadata_size * 8;
1491 dst_bit_off = oob_bit_off;
1492
1493
1494 for (step = 0; step < nfc_geo->ecc_chunk_count; step++) {
1495 if (buf)
1496 gpmi_copy_bits(tmp_buf, dst_bit_off,
1497 buf, step * eccsize * 8, eccsize * 8);
1498 dst_bit_off += eccsize * 8;
1499
1500
1501 if (step == nfc_geo->ecc_chunk_count - 1 &&
1502 (oob_bit_off + eccbits) % 8)
1503 eccbits += 8 - ((oob_bit_off + eccbits) % 8);
1504
1505 if (oob_required)
1506 gpmi_copy_bits(tmp_buf, dst_bit_off,
1507 oob, oob_bit_off, eccbits);
1508
1509 dst_bit_off += eccbits;
1510 oob_bit_off += eccbits;
1511 }
1512
1513 oob_byte_off = oob_bit_off / 8;
1514
1515 if (oob_required && oob_byte_off < mtd->oobsize)
1516 memcpy(tmp_buf + mtd->writesize + oob_byte_off,
1517 oob + oob_byte_off, mtd->oobsize - oob_byte_off);
1518
1519
1520
1521
1522
1523
1524
1525
1526 if (this->swap_block_mark)
1527 swap(tmp_buf[0], tmp_buf[mtd->writesize]);
1528
1529 return nand_prog_page_op(chip, page, 0, tmp_buf,
1530 mtd->writesize + mtd->oobsize);
1531}
1532
1533static int gpmi_ecc_read_oob_raw(struct nand_chip *chip, int page)
1534{
1535 return gpmi_ecc_read_page_raw(chip, NULL, 1, page);
1536}
1537
1538static int gpmi_ecc_write_oob_raw(struct nand_chip *chip, int page)
1539{
1540 return gpmi_ecc_write_page_raw(chip, NULL, 1, page);
1541}
1542
1543static int gpmi_block_markbad(struct nand_chip *chip, loff_t ofs)
1544{
1545 struct mtd_info *mtd = nand_to_mtd(chip);
1546 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1547 int ret = 0;
1548 uint8_t *block_mark;
1549 int column, page, chipnr;
1550
1551 chipnr = (int)(ofs >> chip->chip_shift);
1552 chip->select_chip(chip, chipnr);
1553
1554 column = !GPMI_IS_MX23(this) ? mtd->writesize : 0;
1555
1556
1557 block_mark = this->data_buffer_dma;
1558 block_mark[0] = 0;
1559
1560
1561 page = (int)(ofs >> chip->page_shift);
1562
1563 ret = nand_prog_page_op(chip, page, column, block_mark, 1);
1564
1565 chip->select_chip(chip, -1);
1566
1567 return ret;
1568}
1569
1570static int nand_boot_set_geometry(struct gpmi_nand_data *this)
1571{
1572 struct boot_rom_geometry *geometry = &this->rom_geometry;
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582 geometry->stride_size_in_pages = 64;
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592 geometry->search_area_stride_exponent = 2;
1593 return 0;
1594}
1595
1596static const char *fingerprint = "STMP";
1597static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
1598{
1599 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1600 struct device *dev = this->dev;
1601 struct nand_chip *chip = &this->nand;
1602 unsigned int search_area_size_in_strides;
1603 unsigned int stride;
1604 unsigned int page;
1605 uint8_t *buffer = chip->data_buf;
1606 int saved_chip_number;
1607 int found_an_ncb_fingerprint = false;
1608
1609
1610 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1611
1612 saved_chip_number = this->current_chip;
1613 chip->select_chip(chip, 0);
1614
1615
1616
1617
1618 dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
1619
1620 for (stride = 0; stride < search_area_size_in_strides; stride++) {
1621
1622 page = stride * rom_geo->stride_size_in_pages;
1623
1624 dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
1625
1626
1627
1628
1629
1630 nand_read_page_op(chip, page, 12, NULL, 0);
1631 chip->legacy.read_buf(chip, buffer, strlen(fingerprint));
1632
1633
1634 if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
1635 found_an_ncb_fingerprint = true;
1636 break;
1637 }
1638
1639 }
1640
1641 chip->select_chip(chip, saved_chip_number);
1642
1643 if (found_an_ncb_fingerprint)
1644 dev_dbg(dev, "\tFound a fingerprint\n");
1645 else
1646 dev_dbg(dev, "\tNo fingerprint found\n");
1647 return found_an_ncb_fingerprint;
1648}
1649
1650
1651static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
1652{
1653 struct device *dev = this->dev;
1654 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1655 struct nand_chip *chip = &this->nand;
1656 struct mtd_info *mtd = nand_to_mtd(chip);
1657 unsigned int block_size_in_pages;
1658 unsigned int search_area_size_in_strides;
1659 unsigned int search_area_size_in_pages;
1660 unsigned int search_area_size_in_blocks;
1661 unsigned int block;
1662 unsigned int stride;
1663 unsigned int page;
1664 uint8_t *buffer = chip->data_buf;
1665 int saved_chip_number;
1666 int status;
1667
1668
1669 block_size_in_pages = mtd->erasesize / mtd->writesize;
1670 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1671 search_area_size_in_pages = search_area_size_in_strides *
1672 rom_geo->stride_size_in_pages;
1673 search_area_size_in_blocks =
1674 (search_area_size_in_pages + (block_size_in_pages - 1)) /
1675 block_size_in_pages;
1676
1677 dev_dbg(dev, "Search Area Geometry :\n");
1678 dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
1679 dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
1680 dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
1681
1682
1683 saved_chip_number = this->current_chip;
1684 chip->select_chip(chip, 0);
1685
1686
1687 dev_dbg(dev, "Erasing the search area...\n");
1688
1689 for (block = 0; block < search_area_size_in_blocks; block++) {
1690
1691 dev_dbg(dev, "\tErasing block 0x%x\n", block);
1692 status = nand_erase_op(chip, block);
1693 if (status)
1694 dev_err(dev, "[%s] Erase failed.\n", __func__);
1695 }
1696
1697
1698 memset(buffer, ~0, mtd->writesize);
1699 memcpy(buffer + 12, fingerprint, strlen(fingerprint));
1700
1701
1702 dev_dbg(dev, "Writing NCB fingerprints...\n");
1703 for (stride = 0; stride < search_area_size_in_strides; stride++) {
1704
1705 page = stride * rom_geo->stride_size_in_pages;
1706
1707
1708 dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
1709
1710 status = chip->ecc.write_page_raw(chip, buffer, 0, page);
1711 if (status)
1712 dev_err(dev, "[%s] Write failed.\n", __func__);
1713 }
1714
1715
1716 chip->select_chip(chip, saved_chip_number);
1717 return 0;
1718}
1719
1720static int mx23_boot_init(struct gpmi_nand_data *this)
1721{
1722 struct device *dev = this->dev;
1723 struct nand_chip *chip = &this->nand;
1724 struct mtd_info *mtd = nand_to_mtd(chip);
1725 unsigned int block_count;
1726 unsigned int block;
1727 int chipnr;
1728 int page;
1729 loff_t byte;
1730 uint8_t block_mark;
1731 int ret = 0;
1732
1733
1734
1735
1736
1737
1738
1739 if (mx23_check_transcription_stamp(this))
1740 return 0;
1741
1742
1743
1744
1745
1746 dev_dbg(dev, "Transcribing bad block marks...\n");
1747
1748
1749 block_count = chip->chipsize >> chip->phys_erase_shift;
1750
1751
1752
1753
1754
1755 for (block = 0; block < block_count; block++) {
1756
1757
1758
1759
1760 chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
1761 page = block << (chip->phys_erase_shift - chip->page_shift);
1762 byte = block << chip->phys_erase_shift;
1763
1764
1765 chip->select_chip(chip, chipnr);
1766 nand_read_page_op(chip, page, mtd->writesize, NULL, 0);
1767 block_mark = chip->legacy.read_byte(chip);
1768 chip->select_chip(chip, -1);
1769
1770
1771
1772
1773
1774
1775 if (block_mark != 0xff) {
1776 dev_dbg(dev, "Transcribing mark in block %u\n", block);
1777 ret = chip->legacy.block_markbad(chip, byte);
1778 if (ret)
1779 dev_err(dev,
1780 "Failed to mark block bad with ret %d\n",
1781 ret);
1782 }
1783 }
1784
1785
1786 mx23_write_transcription_stamp(this);
1787 return 0;
1788}
1789
1790static int nand_boot_init(struct gpmi_nand_data *this)
1791{
1792 nand_boot_set_geometry(this);
1793
1794
1795 if (GPMI_IS_MX23(this))
1796 return mx23_boot_init(this);
1797 return 0;
1798}
1799
1800static int gpmi_set_geometry(struct gpmi_nand_data *this)
1801{
1802 int ret;
1803
1804
1805 gpmi_free_dma_buffer(this);
1806
1807
1808 ret = bch_set_geometry(this);
1809 if (ret) {
1810 dev_err(this->dev, "Error setting BCH geometry : %d\n", ret);
1811 return ret;
1812 }
1813
1814
1815 return gpmi_alloc_dma_buffer(this);
1816}
1817
1818static int gpmi_init_last(struct gpmi_nand_data *this)
1819{
1820 struct nand_chip *chip = &this->nand;
1821 struct mtd_info *mtd = nand_to_mtd(chip);
1822 struct nand_ecc_ctrl *ecc = &chip->ecc;
1823 struct bch_geometry *bch_geo = &this->bch_geometry;
1824 int ret;
1825
1826
1827 ret = gpmi_set_geometry(this);
1828 if (ret)
1829 return ret;
1830
1831
1832 ecc->read_page = gpmi_ecc_read_page;
1833 ecc->write_page = gpmi_ecc_write_page;
1834 ecc->read_oob = gpmi_ecc_read_oob;
1835 ecc->write_oob = gpmi_ecc_write_oob;
1836 ecc->read_page_raw = gpmi_ecc_read_page_raw;
1837 ecc->write_page_raw = gpmi_ecc_write_page_raw;
1838 ecc->read_oob_raw = gpmi_ecc_read_oob_raw;
1839 ecc->write_oob_raw = gpmi_ecc_write_oob_raw;
1840 ecc->mode = NAND_ECC_HW;
1841 ecc->size = bch_geo->ecc_chunk_size;
1842 ecc->strength = bch_geo->ecc_strength;
1843 mtd_set_ooblayout(mtd, &gpmi_ooblayout_ops);
1844
1845
1846
1847
1848
1849
1850 if (GPMI_IS_MX6(this) &&
1851 ((bch_geo->gf_len * bch_geo->ecc_strength) % 8) == 0) {
1852 ecc->read_subpage = gpmi_ecc_read_subpage;
1853 chip->options |= NAND_SUBPAGE_READ;
1854 }
1855
1856 return 0;
1857}
1858
1859static int gpmi_nand_attach_chip(struct nand_chip *chip)
1860{
1861 struct gpmi_nand_data *this = nand_get_controller_data(chip);
1862 int ret;
1863
1864 if (chip->bbt_options & NAND_BBT_USE_FLASH) {
1865 chip->bbt_options |= NAND_BBT_NO_OOB;
1866
1867 if (of_property_read_bool(this->dev->of_node,
1868 "fsl,no-blockmark-swap"))
1869 this->swap_block_mark = false;
1870 }
1871 dev_dbg(this->dev, "Blockmark swapping %sabled\n",
1872 this->swap_block_mark ? "en" : "dis");
1873
1874 ret = gpmi_init_last(this);
1875 if (ret)
1876 return ret;
1877
1878 chip->options |= NAND_SKIP_BBTSCAN;
1879
1880 return 0;
1881}
1882
1883static const struct nand_controller_ops gpmi_nand_controller_ops = {
1884 .attach_chip = gpmi_nand_attach_chip,
1885};
1886
1887static int gpmi_nand_init(struct gpmi_nand_data *this)
1888{
1889 struct nand_chip *chip = &this->nand;
1890 struct mtd_info *mtd = nand_to_mtd(chip);
1891 int ret;
1892
1893
1894 this->current_chip = -1;
1895
1896
1897 mtd->name = "gpmi-nand";
1898 mtd->dev.parent = this->dev;
1899
1900
1901 nand_set_controller_data(chip, this);
1902 nand_set_flash_node(chip, this->pdev->dev.of_node);
1903 chip->select_chip = gpmi_select_chip;
1904 chip->setup_data_interface = gpmi_setup_data_interface;
1905 chip->legacy.cmd_ctrl = gpmi_cmd_ctrl;
1906 chip->legacy.dev_ready = gpmi_dev_ready;
1907 chip->legacy.read_byte = gpmi_read_byte;
1908 chip->legacy.read_buf = gpmi_read_buf;
1909 chip->legacy.write_buf = gpmi_write_buf;
1910 chip->badblock_pattern = &gpmi_bbt_descr;
1911 chip->legacy.block_markbad = gpmi_block_markbad;
1912 chip->options |= NAND_NO_SUBPAGE_WRITE;
1913
1914
1915 this->swap_block_mark = !GPMI_IS_MX23(this);
1916
1917
1918
1919
1920
1921 this->bch_geometry.payload_size = 1024;
1922 this->bch_geometry.auxiliary_size = 128;
1923 ret = gpmi_alloc_dma_buffer(this);
1924 if (ret)
1925 goto err_out;
1926
1927 chip->dummy_controller.ops = &gpmi_nand_controller_ops;
1928 ret = nand_scan(chip, GPMI_IS_MX6(this) ? 2 : 1);
1929 if (ret)
1930 goto err_out;
1931
1932 ret = nand_boot_init(this);
1933 if (ret)
1934 goto err_nand_cleanup;
1935 ret = nand_create_bbt(chip);
1936 if (ret)
1937 goto err_nand_cleanup;
1938
1939 ret = mtd_device_register(mtd, NULL, 0);
1940 if (ret)
1941 goto err_nand_cleanup;
1942 return 0;
1943
1944err_nand_cleanup:
1945 nand_cleanup(chip);
1946err_out:
1947 gpmi_free_dma_buffer(this);
1948 return ret;
1949}
1950
1951static const struct of_device_id gpmi_nand_id_table[] = {
1952 {
1953 .compatible = "fsl,imx23-gpmi-nand",
1954 .data = &gpmi_devdata_imx23,
1955 }, {
1956 .compatible = "fsl,imx28-gpmi-nand",
1957 .data = &gpmi_devdata_imx28,
1958 }, {
1959 .compatible = "fsl,imx6q-gpmi-nand",
1960 .data = &gpmi_devdata_imx6q,
1961 }, {
1962 .compatible = "fsl,imx6sx-gpmi-nand",
1963 .data = &gpmi_devdata_imx6sx,
1964 }, {
1965 .compatible = "fsl,imx7d-gpmi-nand",
1966 .data = &gpmi_devdata_imx7d,
1967 }, {}
1968};
1969MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
1970
1971static int gpmi_nand_probe(struct platform_device *pdev)
1972{
1973 struct gpmi_nand_data *this;
1974 const struct of_device_id *of_id;
1975 int ret;
1976
1977 this = devm_kzalloc(&pdev->dev, sizeof(*this), GFP_KERNEL);
1978 if (!this)
1979 return -ENOMEM;
1980
1981 of_id = of_match_device(gpmi_nand_id_table, &pdev->dev);
1982 if (of_id) {
1983 this->devdata = of_id->data;
1984 } else {
1985 dev_err(&pdev->dev, "Failed to find the right device id.\n");
1986 return -ENODEV;
1987 }
1988
1989 platform_set_drvdata(pdev, this);
1990 this->pdev = pdev;
1991 this->dev = &pdev->dev;
1992
1993 ret = acquire_resources(this);
1994 if (ret)
1995 goto exit_acquire_resources;
1996
1997 ret = gpmi_init(this);
1998 if (ret)
1999 goto exit_nfc_init;
2000
2001 ret = gpmi_nand_init(this);
2002 if (ret)
2003 goto exit_nfc_init;
2004
2005 dev_info(this->dev, "driver registered.\n");
2006
2007 return 0;
2008
2009exit_nfc_init:
2010 release_resources(this);
2011exit_acquire_resources:
2012
2013 return ret;
2014}
2015
2016static int gpmi_nand_remove(struct platform_device *pdev)
2017{
2018 struct gpmi_nand_data *this = platform_get_drvdata(pdev);
2019
2020 nand_release(&this->nand);
2021 gpmi_free_dma_buffer(this);
2022 release_resources(this);
2023 return 0;
2024}
2025
2026#ifdef CONFIG_PM_SLEEP
2027static int gpmi_pm_suspend(struct device *dev)
2028{
2029 struct gpmi_nand_data *this = dev_get_drvdata(dev);
2030
2031 release_dma_channels(this);
2032 return 0;
2033}
2034
2035static int gpmi_pm_resume(struct device *dev)
2036{
2037 struct gpmi_nand_data *this = dev_get_drvdata(dev);
2038 int ret;
2039
2040 ret = acquire_dma_channels(this);
2041 if (ret < 0)
2042 return ret;
2043
2044
2045 ret = gpmi_init(this);
2046 if (ret) {
2047 dev_err(this->dev, "Error setting GPMI : %d\n", ret);
2048 return ret;
2049 }
2050
2051
2052 ret = bch_set_geometry(this);
2053 if (ret) {
2054 dev_err(this->dev, "Error setting BCH : %d\n", ret);
2055 return ret;
2056 }
2057
2058 return 0;
2059}
2060#endif
2061
2062static const struct dev_pm_ops gpmi_pm_ops = {
2063 SET_SYSTEM_SLEEP_PM_OPS(gpmi_pm_suspend, gpmi_pm_resume)
2064};
2065
2066static struct platform_driver gpmi_nand_driver = {
2067 .driver = {
2068 .name = "gpmi-nand",
2069 .pm = &gpmi_pm_ops,
2070 .of_match_table = gpmi_nand_id_table,
2071 },
2072 .probe = gpmi_nand_probe,
2073 .remove = gpmi_nand_remove,
2074};
2075module_platform_driver(gpmi_nand_driver);
2076
2077MODULE_AUTHOR("Freescale Semiconductor, Inc.");
2078MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
2079MODULE_LICENSE("GPL");
2080