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