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