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/gpmi-nand.h>
26#include <linux/mtd/partitions.h>
27#include "gpmi-nand.h"
28
29
30static uint8_t scan_ff_pattern[] = { 0xff };
31static struct nand_bbt_descr gpmi_bbt_descr = {
32 .options = 0,
33 .offs = 0,
34 .len = 1,
35 .pattern = scan_ff_pattern
36};
37
38
39static struct nand_ecclayout gpmi_hw_ecclayout = {
40 .eccbytes = 0,
41 .eccpos = { 0, },
42 .oobfree = { {.offset = 0, .length = 0} }
43};
44
45static irqreturn_t bch_irq(int irq, void *cookie)
46{
47 struct gpmi_nand_data *this = cookie;
48
49 gpmi_clear_bch(this);
50 complete(&this->bch_done);
51 return IRQ_HANDLED;
52}
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72static inline int get_ecc_strength(struct gpmi_nand_data *this)
73{
74 struct bch_geometry *geo = &this->bch_geometry;
75 struct mtd_info *mtd = &this->mtd;
76 int ecc_strength;
77
78 ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8)
79 / (geo->gf_len * geo->ecc_chunk_count);
80
81
82 return round_down(ecc_strength, 2);
83}
84
85int common_nfc_set_geometry(struct gpmi_nand_data *this)
86{
87 struct bch_geometry *geo = &this->bch_geometry;
88 struct mtd_info *mtd = &this->mtd;
89 unsigned int metadata_size;
90 unsigned int status_size;
91 unsigned int block_mark_bit_offset;
92
93
94
95
96
97
98 geo->metadata_size = 10;
99
100
101 geo->gf_len = 13;
102
103
104 geo->ecc_chunk_size = 512;
105 while (geo->ecc_chunk_size < mtd->oobsize)
106 geo->ecc_chunk_size *= 2;
107
108 geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
109
110
111 geo->ecc_strength = get_ecc_strength(this);
112 if (!geo->ecc_strength) {
113 pr_err("We get a wrong ECC strength.\n");
114 return -EINVAL;
115 }
116
117 geo->page_size = mtd->writesize + mtd->oobsize;
118 geo->payload_size = mtd->writesize;
119
120
121
122
123
124
125
126 metadata_size = ALIGN(geo->metadata_size, 4);
127 status_size = ALIGN(geo->ecc_chunk_count, 4);
128
129 geo->auxiliary_size = metadata_size + status_size;
130 geo->auxiliary_status_offset = metadata_size;
131
132 if (!this->swap_block_mark)
133 return 0;
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181 block_mark_bit_offset = mtd->writesize * 8 -
182 (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
183 + geo->metadata_size * 8);
184
185 geo->block_mark_byte_offset = block_mark_bit_offset / 8;
186 geo->block_mark_bit_offset = block_mark_bit_offset % 8;
187 return 0;
188}
189
190struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
191{
192 int chipnr = this->current_chip;
193
194 return this->dma_chans[chipnr];
195}
196
197
198void prepare_data_dma(struct gpmi_nand_data *this, enum dma_data_direction dr)
199{
200 struct scatterlist *sgl = &this->data_sgl;
201 int ret;
202
203 this->direct_dma_map_ok = true;
204
205
206 sg_init_one(sgl, this->upper_buf, this->upper_len);
207 ret = dma_map_sg(this->dev, sgl, 1, dr);
208 if (ret == 0) {
209
210 sg_init_one(sgl, this->data_buffer_dma, PAGE_SIZE);
211
212 if (dr == DMA_TO_DEVICE)
213 memcpy(this->data_buffer_dma, this->upper_buf,
214 this->upper_len);
215
216 ret = dma_map_sg(this->dev, sgl, 1, dr);
217 if (ret == 0)
218 pr_err("map failed.\n");
219
220 this->direct_dma_map_ok = false;
221 }
222}
223
224
225static void dma_irq_callback(void *param)
226{
227 struct gpmi_nand_data *this = param;
228 struct completion *dma_c = &this->dma_done;
229
230 complete(dma_c);
231
232 switch (this->dma_type) {
233 case DMA_FOR_COMMAND:
234 dma_unmap_sg(this->dev, &this->cmd_sgl, 1, DMA_TO_DEVICE);
235 break;
236
237 case DMA_FOR_READ_DATA:
238 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_FROM_DEVICE);
239 if (this->direct_dma_map_ok == false)
240 memcpy(this->upper_buf, this->data_buffer_dma,
241 this->upper_len);
242 break;
243
244 case DMA_FOR_WRITE_DATA:
245 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_TO_DEVICE);
246 break;
247
248 case DMA_FOR_READ_ECC_PAGE:
249 case DMA_FOR_WRITE_ECC_PAGE:
250
251 break;
252
253 default:
254 pr_err("in wrong DMA operation.\n");
255 }
256}
257
258int start_dma_without_bch_irq(struct gpmi_nand_data *this,
259 struct dma_async_tx_descriptor *desc)
260{
261 struct completion *dma_c = &this->dma_done;
262 int err;
263
264 init_completion(dma_c);
265
266 desc->callback = dma_irq_callback;
267 desc->callback_param = this;
268 dmaengine_submit(desc);
269
270
271 err = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000));
272 if (!err) {
273 pr_err("DMA timeout, last DMA :%d\n", this->last_dma_type);
274 gpmi_dump_info(this);
275 return -ETIMEDOUT;
276 }
277 return 0;
278}
279
280
281
282
283
284
285
286
287int start_dma_with_bch_irq(struct gpmi_nand_data *this,
288 struct dma_async_tx_descriptor *desc)
289{
290 struct completion *bch_c = &this->bch_done;
291 int err;
292
293
294 init_completion(bch_c);
295
296
297 start_dma_without_bch_irq(this, desc);
298
299
300 err = wait_for_completion_timeout(bch_c, msecs_to_jiffies(1000));
301 if (!err) {
302 pr_err("BCH timeout, last DMA :%d\n", this->last_dma_type);
303 gpmi_dump_info(this);
304 return -ETIMEDOUT;
305 }
306 return 0;
307}
308
309static int __devinit
310acquire_register_block(struct gpmi_nand_data *this, const char *res_name)
311{
312 struct platform_device *pdev = this->pdev;
313 struct resources *res = &this->resources;
314 struct resource *r;
315 void *p;
316
317 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name);
318 if (!r) {
319 pr_err("Can't get resource for %s\n", res_name);
320 return -ENXIO;
321 }
322
323 p = ioremap(r->start, resource_size(r));
324 if (!p) {
325 pr_err("Can't remap %s\n", res_name);
326 return -ENOMEM;
327 }
328
329 if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
330 res->gpmi_regs = p;
331 else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
332 res->bch_regs = p;
333 else
334 pr_err("unknown resource name : %s\n", res_name);
335
336 return 0;
337}
338
339static void release_register_block(struct gpmi_nand_data *this)
340{
341 struct resources *res = &this->resources;
342 if (res->gpmi_regs)
343 iounmap(res->gpmi_regs);
344 if (res->bch_regs)
345 iounmap(res->bch_regs);
346 res->gpmi_regs = NULL;
347 res->bch_regs = NULL;
348}
349
350static int __devinit
351acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
352{
353 struct platform_device *pdev = this->pdev;
354 struct resources *res = &this->resources;
355 const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
356 struct resource *r;
357 int err;
358
359 r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name);
360 if (!r) {
361 pr_err("Can't get resource for %s\n", res_name);
362 return -ENXIO;
363 }
364
365 err = request_irq(r->start, irq_h, 0, res_name, this);
366 if (err) {
367 pr_err("Can't own %s\n", res_name);
368 return err;
369 }
370
371 res->bch_low_interrupt = r->start;
372 res->bch_high_interrupt = r->end;
373 return 0;
374}
375
376static void release_bch_irq(struct gpmi_nand_data *this)
377{
378 struct resources *res = &this->resources;
379 int i = res->bch_low_interrupt;
380
381 for (; i <= res->bch_high_interrupt; i++)
382 free_irq(i, this);
383}
384
385static bool gpmi_dma_filter(struct dma_chan *chan, void *param)
386{
387 struct gpmi_nand_data *this = param;
388 struct resource *r = this->private;
389
390 if (!mxs_dma_is_apbh(chan))
391 return false;
392
393
394
395
396
397
398
399
400 if (r->start <= chan->chan_id && chan->chan_id <= r->end) {
401 chan->private = &this->dma_data;
402 return true;
403 }
404 return false;
405}
406
407static void release_dma_channels(struct gpmi_nand_data *this)
408{
409 unsigned int i;
410 for (i = 0; i < DMA_CHANS; i++)
411 if (this->dma_chans[i]) {
412 dma_release_channel(this->dma_chans[i]);
413 this->dma_chans[i] = NULL;
414 }
415}
416
417static int __devinit acquire_dma_channels(struct gpmi_nand_data *this)
418{
419 struct platform_device *pdev = this->pdev;
420 struct gpmi_nand_platform_data *pdata = this->pdata;
421 struct resources *res = &this->resources;
422 struct resource *r, *r_dma;
423 unsigned int i;
424
425 r = platform_get_resource_byname(pdev, IORESOURCE_DMA,
426 GPMI_NAND_DMA_CHANNELS_RES_NAME);
427 r_dma = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
428 GPMI_NAND_DMA_INTERRUPT_RES_NAME);
429 if (!r || !r_dma) {
430 pr_err("Can't get resource for DMA\n");
431 return -ENXIO;
432 }
433
434
435 this->private = r;
436
437 for (i = r->start; i <= r->end; i++) {
438 struct dma_chan *dma_chan;
439 dma_cap_mask_t mask;
440
441 if (i - r->start >= pdata->max_chip_count)
442 break;
443
444 dma_cap_zero(mask);
445 dma_cap_set(DMA_SLAVE, mask);
446
447
448 if (r_dma->start == r_dma->end) {
449
450 if (i == r->start)
451 this->dma_data.chan_irq = r_dma->start;
452 else
453 this->dma_data.chan_irq = NO_IRQ;
454 } else
455 this->dma_data.chan_irq = r_dma->start + (i - r->start);
456
457 dma_chan = dma_request_channel(mask, gpmi_dma_filter, this);
458 if (!dma_chan)
459 goto acquire_err;
460
461
462 this->dma_chans[i - r->start] = dma_chan;
463 }
464
465 res->dma_low_channel = r->start;
466 res->dma_high_channel = i;
467 return 0;
468
469acquire_err:
470 pr_err("Can't acquire DMA channel %u\n", i);
471 release_dma_channels(this);
472 return -EINVAL;
473}
474
475static int __devinit acquire_resources(struct gpmi_nand_data *this)
476{
477 struct resources *res = &this->resources;
478 int ret;
479
480 ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
481 if (ret)
482 goto exit_regs;
483
484 ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
485 if (ret)
486 goto exit_regs;
487
488 ret = acquire_bch_irq(this, bch_irq);
489 if (ret)
490 goto exit_regs;
491
492 ret = acquire_dma_channels(this);
493 if (ret)
494 goto exit_dma_channels;
495
496 res->clock = clk_get(&this->pdev->dev, NULL);
497 if (IS_ERR(res->clock)) {
498 pr_err("can not get the clock\n");
499 ret = -ENOENT;
500 goto exit_clock;
501 }
502 return 0;
503
504exit_clock:
505 release_dma_channels(this);
506exit_dma_channels:
507 release_bch_irq(this);
508exit_regs:
509 release_register_block(this);
510 return ret;
511}
512
513static void release_resources(struct gpmi_nand_data *this)
514{
515 struct resources *r = &this->resources;
516
517 clk_put(r->clock);
518 release_register_block(this);
519 release_bch_irq(this);
520 release_dma_channels(this);
521}
522
523static int __devinit init_hardware(struct gpmi_nand_data *this)
524{
525 int ret;
526
527
528
529
530
531
532 struct nand_timing safe_timing = {
533 .data_setup_in_ns = 80,
534 .data_hold_in_ns = 60,
535 .address_setup_in_ns = 25,
536 .gpmi_sample_delay_in_ns = 6,
537 .tREA_in_ns = -1,
538 .tRLOH_in_ns = -1,
539 .tRHOH_in_ns = -1,
540 };
541
542
543 ret = gpmi_init(this);
544 if (ret)
545 return ret;
546
547 this->timing = safe_timing;
548 return 0;
549}
550
551static int read_page_prepare(struct gpmi_nand_data *this,
552 void *destination, unsigned length,
553 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
554 void **use_virt, dma_addr_t *use_phys)
555{
556 struct device *dev = this->dev;
557
558 if (virt_addr_valid(destination)) {
559 dma_addr_t dest_phys;
560
561 dest_phys = dma_map_single(dev, destination,
562 length, DMA_FROM_DEVICE);
563 if (dma_mapping_error(dev, dest_phys)) {
564 if (alt_size < length) {
565 pr_err("Alternate buffer is too small\n");
566 return -ENOMEM;
567 }
568 goto map_failed;
569 }
570 *use_virt = destination;
571 *use_phys = dest_phys;
572 this->direct_dma_map_ok = true;
573 return 0;
574 }
575
576map_failed:
577 *use_virt = alt_virt;
578 *use_phys = alt_phys;
579 this->direct_dma_map_ok = false;
580 return 0;
581}
582
583static inline void read_page_end(struct gpmi_nand_data *this,
584 void *destination, unsigned length,
585 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
586 void *used_virt, dma_addr_t used_phys)
587{
588 if (this->direct_dma_map_ok)
589 dma_unmap_single(this->dev, used_phys, length, DMA_FROM_DEVICE);
590}
591
592static inline void read_page_swap_end(struct gpmi_nand_data *this,
593 void *destination, unsigned length,
594 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
595 void *used_virt, dma_addr_t used_phys)
596{
597 if (!this->direct_dma_map_ok)
598 memcpy(destination, alt_virt, length);
599}
600
601static int send_page_prepare(struct gpmi_nand_data *this,
602 const void *source, unsigned length,
603 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
604 const void **use_virt, dma_addr_t *use_phys)
605{
606 struct device *dev = this->dev;
607
608 if (virt_addr_valid(source)) {
609 dma_addr_t source_phys;
610
611 source_phys = dma_map_single(dev, (void *)source, length,
612 DMA_TO_DEVICE);
613 if (dma_mapping_error(dev, source_phys)) {
614 if (alt_size < length) {
615 pr_err("Alternate buffer is too small\n");
616 return -ENOMEM;
617 }
618 goto map_failed;
619 }
620 *use_virt = source;
621 *use_phys = source_phys;
622 return 0;
623 }
624map_failed:
625
626
627
628
629 memcpy(alt_virt, source, length);
630
631 *use_virt = alt_virt;
632 *use_phys = alt_phys;
633 return 0;
634}
635
636static void send_page_end(struct gpmi_nand_data *this,
637 const void *source, unsigned length,
638 void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
639 const void *used_virt, dma_addr_t used_phys)
640{
641 struct device *dev = this->dev;
642 if (used_virt == source)
643 dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
644}
645
646static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
647{
648 struct device *dev = this->dev;
649
650 if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
651 dma_free_coherent(dev, this->page_buffer_size,
652 this->page_buffer_virt,
653 this->page_buffer_phys);
654 kfree(this->cmd_buffer);
655 kfree(this->data_buffer_dma);
656
657 this->cmd_buffer = NULL;
658 this->data_buffer_dma = NULL;
659 this->page_buffer_virt = NULL;
660 this->page_buffer_size = 0;
661}
662
663
664static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
665{
666 struct bch_geometry *geo = &this->bch_geometry;
667 struct device *dev = this->dev;
668
669
670 this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA);
671 if (this->cmd_buffer == NULL)
672 goto error_alloc;
673
674
675 this->data_buffer_dma = kzalloc(PAGE_SIZE, GFP_DMA);
676 if (this->data_buffer_dma == NULL)
677 goto error_alloc;
678
679
680
681
682
683
684
685
686
687 this->page_buffer_size = geo->payload_size + geo->auxiliary_size;
688 this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
689 &this->page_buffer_phys, GFP_DMA);
690 if (!this->page_buffer_virt)
691 goto error_alloc;
692
693
694
695 this->payload_virt = this->page_buffer_virt;
696 this->payload_phys = this->page_buffer_phys;
697 this->auxiliary_virt = this->payload_virt + geo->payload_size;
698 this->auxiliary_phys = this->payload_phys + geo->payload_size;
699 return 0;
700
701error_alloc:
702 gpmi_free_dma_buffer(this);
703 pr_err("allocate DMA buffer ret!!\n");
704 return -ENOMEM;
705}
706
707static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
708{
709 struct nand_chip *chip = mtd->priv;
710 struct gpmi_nand_data *this = chip->priv;
711 int ret;
712
713
714
715
716
717
718
719
720
721
722
723
724 if ((ctrl & (NAND_ALE | NAND_CLE))) {
725 if (data != NAND_CMD_NONE)
726 this->cmd_buffer[this->command_length++] = data;
727 return;
728 }
729
730 if (!this->command_length)
731 return;
732
733 ret = gpmi_send_command(this);
734 if (ret)
735 pr_err("Chip: %u, Error %d\n", this->current_chip, ret);
736
737 this->command_length = 0;
738}
739
740static int gpmi_dev_ready(struct mtd_info *mtd)
741{
742 struct nand_chip *chip = mtd->priv;
743 struct gpmi_nand_data *this = chip->priv;
744
745 return gpmi_is_ready(this, this->current_chip);
746}
747
748static void gpmi_select_chip(struct mtd_info *mtd, int chipnr)
749{
750 struct nand_chip *chip = mtd->priv;
751 struct gpmi_nand_data *this = chip->priv;
752
753 if ((this->current_chip < 0) && (chipnr >= 0))
754 gpmi_begin(this);
755 else if ((this->current_chip >= 0) && (chipnr < 0))
756 gpmi_end(this);
757
758 this->current_chip = chipnr;
759}
760
761static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
762{
763 struct nand_chip *chip = mtd->priv;
764 struct gpmi_nand_data *this = chip->priv;
765
766 pr_debug("len is %d\n", len);
767 this->upper_buf = buf;
768 this->upper_len = len;
769
770 gpmi_read_data(this);
771}
772
773static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
774{
775 struct nand_chip *chip = mtd->priv;
776 struct gpmi_nand_data *this = chip->priv;
777
778 pr_debug("len is %d\n", len);
779 this->upper_buf = (uint8_t *)buf;
780 this->upper_len = len;
781
782 gpmi_send_data(this);
783}
784
785static uint8_t gpmi_read_byte(struct mtd_info *mtd)
786{
787 struct nand_chip *chip = mtd->priv;
788 struct gpmi_nand_data *this = chip->priv;
789 uint8_t *buf = this->data_buffer_dma;
790
791 gpmi_read_buf(mtd, buf, 1);
792 return buf[0];
793}
794
795
796
797
798
799
800static void block_mark_swapping(struct gpmi_nand_data *this,
801 void *payload, void *auxiliary)
802{
803 struct bch_geometry *nfc_geo = &this->bch_geometry;
804 unsigned char *p;
805 unsigned char *a;
806 unsigned int bit;
807 unsigned char mask;
808 unsigned char from_data;
809 unsigned char from_oob;
810
811 if (!this->swap_block_mark)
812 return;
813
814
815
816
817
818 bit = nfc_geo->block_mark_bit_offset;
819 p = payload + nfc_geo->block_mark_byte_offset;
820 a = auxiliary;
821
822
823
824
825
826
827
828 from_data = (p[0] >> bit) | (p[1] << (8 - bit));
829
830
831 from_oob = a[0];
832
833
834 a[0] = from_data;
835
836 mask = (0x1 << bit) - 1;
837 p[0] = (p[0] & mask) | (from_oob << bit);
838
839 mask = ~0 << bit;
840 p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
841}
842
843static int gpmi_ecc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
844 uint8_t *buf, int page)
845{
846 struct gpmi_nand_data *this = chip->priv;
847 struct bch_geometry *nfc_geo = &this->bch_geometry;
848 void *payload_virt;
849 dma_addr_t payload_phys;
850 void *auxiliary_virt;
851 dma_addr_t auxiliary_phys;
852 unsigned int i;
853 unsigned char *status;
854 unsigned int failed;
855 unsigned int corrected;
856 int ret;
857
858 pr_debug("page number is : %d\n", page);
859 ret = read_page_prepare(this, buf, mtd->writesize,
860 this->payload_virt, this->payload_phys,
861 nfc_geo->payload_size,
862 &payload_virt, &payload_phys);
863 if (ret) {
864 pr_err("Inadequate DMA buffer\n");
865 ret = -ENOMEM;
866 return ret;
867 }
868 auxiliary_virt = this->auxiliary_virt;
869 auxiliary_phys = this->auxiliary_phys;
870
871
872 ret = gpmi_read_page(this, payload_phys, auxiliary_phys);
873 read_page_end(this, buf, mtd->writesize,
874 this->payload_virt, this->payload_phys,
875 nfc_geo->payload_size,
876 payload_virt, payload_phys);
877 if (ret) {
878 pr_err("Error in ECC-based read: %d\n", ret);
879 goto exit_nfc;
880 }
881
882
883 block_mark_swapping(this, payload_virt, auxiliary_virt);
884
885
886 failed = 0;
887 corrected = 0;
888 status = auxiliary_virt + nfc_geo->auxiliary_status_offset;
889
890 for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
891 if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
892 continue;
893
894 if (*status == STATUS_UNCORRECTABLE) {
895 failed++;
896 continue;
897 }
898 corrected += *status;
899 }
900
901
902
903
904
905 if (failed || corrected >= (nfc_geo->ecc_strength - 1)) {
906 mtd->ecc_stats.failed += failed;
907 mtd->ecc_stats.corrected += corrected;
908 }
909
910
911
912
913
914
915
916
917
918
919 memset(chip->oob_poi, ~0, mtd->oobsize);
920 chip->oob_poi[0] = ((uint8_t *) auxiliary_virt)[0];
921
922 read_page_swap_end(this, buf, mtd->writesize,
923 this->payload_virt, this->payload_phys,
924 nfc_geo->payload_size,
925 payload_virt, payload_phys);
926exit_nfc:
927 return ret;
928}
929
930static void gpmi_ecc_write_page(struct mtd_info *mtd,
931 struct nand_chip *chip, const uint8_t *buf)
932{
933 struct gpmi_nand_data *this = chip->priv;
934 struct bch_geometry *nfc_geo = &this->bch_geometry;
935 const void *payload_virt;
936 dma_addr_t payload_phys;
937 const void *auxiliary_virt;
938 dma_addr_t auxiliary_phys;
939 int ret;
940
941 pr_debug("ecc write page.\n");
942 if (this->swap_block_mark) {
943
944
945
946
947
948 memcpy(this->payload_virt, buf, mtd->writesize);
949 payload_virt = this->payload_virt;
950 payload_phys = this->payload_phys;
951
952 memcpy(this->auxiliary_virt, chip->oob_poi,
953 nfc_geo->auxiliary_size);
954 auxiliary_virt = this->auxiliary_virt;
955 auxiliary_phys = this->auxiliary_phys;
956
957
958 block_mark_swapping(this,
959 (void *) payload_virt, (void *) auxiliary_virt);
960 } else {
961
962
963
964
965 ret = send_page_prepare(this,
966 buf, mtd->writesize,
967 this->payload_virt, this->payload_phys,
968 nfc_geo->payload_size,
969 &payload_virt, &payload_phys);
970 if (ret) {
971 pr_err("Inadequate payload DMA buffer\n");
972 return;
973 }
974
975 ret = send_page_prepare(this,
976 chip->oob_poi, mtd->oobsize,
977 this->auxiliary_virt, this->auxiliary_phys,
978 nfc_geo->auxiliary_size,
979 &auxiliary_virt, &auxiliary_phys);
980 if (ret) {
981 pr_err("Inadequate auxiliary DMA buffer\n");
982 goto exit_auxiliary;
983 }
984 }
985
986
987 ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
988 if (ret)
989 pr_err("Error in ECC-based write: %d\n", ret);
990
991 if (!this->swap_block_mark) {
992 send_page_end(this, chip->oob_poi, mtd->oobsize,
993 this->auxiliary_virt, this->auxiliary_phys,
994 nfc_geo->auxiliary_size,
995 auxiliary_virt, auxiliary_phys);
996exit_auxiliary:
997 send_page_end(this, buf, mtd->writesize,
998 this->payload_virt, this->payload_phys,
999 nfc_geo->payload_size,
1000 payload_virt, payload_phys);
1001 }
1002}
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069static int gpmi_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1070 int page, int sndcmd)
1071{
1072 struct gpmi_nand_data *this = chip->priv;
1073
1074 pr_debug("page number is %d\n", page);
1075
1076 memset(chip->oob_poi, ~0, mtd->oobsize);
1077
1078
1079 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1080 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1081
1082
1083
1084
1085
1086
1087 if (!this->swap_block_mark) {
1088
1089 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1090 chip->oob_poi[0] = chip->read_byte(mtd);
1091 }
1092
1093
1094
1095
1096
1097 return true;
1098}
1099
1100static int
1101gpmi_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
1102{
1103
1104
1105
1106
1107
1108
1109
1110 return -EPERM;
1111}
1112
1113static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs)
1114{
1115 struct nand_chip *chip = mtd->priv;
1116 struct gpmi_nand_data *this = chip->priv;
1117 int block, ret = 0;
1118 uint8_t *block_mark;
1119 int column, page, status, chipnr;
1120
1121
1122 block = (int)(ofs >> chip->bbt_erase_shift);
1123 if (chip->bbt)
1124 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
1125
1126
1127 if (chip->options & NAND_BBT_USE_FLASH)
1128 ret = nand_update_bbt(mtd, ofs);
1129 else {
1130 chipnr = (int)(ofs >> chip->chip_shift);
1131 chip->select_chip(mtd, chipnr);
1132
1133 column = this->swap_block_mark ? mtd->writesize : 0;
1134
1135
1136 block_mark = this->data_buffer_dma;
1137 block_mark[0] = 0;
1138
1139
1140 page = (int)(ofs >> chip->page_shift);
1141
1142 chip->cmdfunc(mtd, NAND_CMD_SEQIN, column, page);
1143 chip->write_buf(mtd, block_mark, 1);
1144 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1145
1146 status = chip->waitfunc(mtd, chip);
1147 if (status & NAND_STATUS_FAIL)
1148 ret = -EIO;
1149
1150 chip->select_chip(mtd, -1);
1151 }
1152 if (!ret)
1153 mtd->ecc_stats.badblocks++;
1154
1155 return ret;
1156}
1157
1158static int __devinit nand_boot_set_geometry(struct gpmi_nand_data *this)
1159{
1160 struct boot_rom_geometry *geometry = &this->rom_geometry;
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170 geometry->stride_size_in_pages = 64;
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180 geometry->search_area_stride_exponent = 2;
1181 return 0;
1182}
1183
1184static const char *fingerprint = "STMP";
1185static int __devinit mx23_check_transcription_stamp(struct gpmi_nand_data *this)
1186{
1187 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1188 struct device *dev = this->dev;
1189 struct mtd_info *mtd = &this->mtd;
1190 struct nand_chip *chip = &this->nand;
1191 unsigned int search_area_size_in_strides;
1192 unsigned int stride;
1193 unsigned int page;
1194 loff_t byte;
1195 uint8_t *buffer = chip->buffers->databuf;
1196 int saved_chip_number;
1197 int found_an_ncb_fingerprint = false;
1198
1199
1200 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1201
1202 saved_chip_number = this->current_chip;
1203 chip->select_chip(mtd, 0);
1204
1205
1206
1207
1208 dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
1209
1210 for (stride = 0; stride < search_area_size_in_strides; stride++) {
1211
1212 page = stride * rom_geo->stride_size_in_pages;
1213 byte = page * mtd->writesize;
1214
1215 dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
1216
1217
1218
1219
1220
1221 chip->cmdfunc(mtd, NAND_CMD_READ0, 12, page);
1222 chip->read_buf(mtd, buffer, strlen(fingerprint));
1223
1224
1225 if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
1226 found_an_ncb_fingerprint = true;
1227 break;
1228 }
1229
1230 }
1231
1232 chip->select_chip(mtd, saved_chip_number);
1233
1234 if (found_an_ncb_fingerprint)
1235 dev_dbg(dev, "\tFound a fingerprint\n");
1236 else
1237 dev_dbg(dev, "\tNo fingerprint found\n");
1238 return found_an_ncb_fingerprint;
1239}
1240
1241
1242static int __devinit mx23_write_transcription_stamp(struct gpmi_nand_data *this)
1243{
1244 struct device *dev = this->dev;
1245 struct boot_rom_geometry *rom_geo = &this->rom_geometry;
1246 struct mtd_info *mtd = &this->mtd;
1247 struct nand_chip *chip = &this->nand;
1248 unsigned int block_size_in_pages;
1249 unsigned int search_area_size_in_strides;
1250 unsigned int search_area_size_in_pages;
1251 unsigned int search_area_size_in_blocks;
1252 unsigned int block;
1253 unsigned int stride;
1254 unsigned int page;
1255 loff_t byte;
1256 uint8_t *buffer = chip->buffers->databuf;
1257 int saved_chip_number;
1258 int status;
1259
1260
1261 block_size_in_pages = mtd->erasesize / mtd->writesize;
1262 search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
1263 search_area_size_in_pages = search_area_size_in_strides *
1264 rom_geo->stride_size_in_pages;
1265 search_area_size_in_blocks =
1266 (search_area_size_in_pages + (block_size_in_pages - 1)) /
1267 block_size_in_pages;
1268
1269 dev_dbg(dev, "Search Area Geometry :\n");
1270 dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
1271 dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
1272 dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
1273
1274
1275 saved_chip_number = this->current_chip;
1276 chip->select_chip(mtd, 0);
1277
1278
1279 dev_dbg(dev, "Erasing the search area...\n");
1280
1281 for (block = 0; block < search_area_size_in_blocks; block++) {
1282
1283 page = block * block_size_in_pages;
1284
1285
1286 dev_dbg(dev, "\tErasing block 0x%x\n", block);
1287 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
1288 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
1289
1290
1291 status = chip->waitfunc(mtd, chip);
1292 if (status & NAND_STATUS_FAIL)
1293 dev_err(dev, "[%s] Erase failed.\n", __func__);
1294 }
1295
1296
1297 memset(buffer, ~0, mtd->writesize);
1298 memset(chip->oob_poi, ~0, mtd->oobsize);
1299 memcpy(buffer + 12, fingerprint, strlen(fingerprint));
1300
1301
1302 dev_dbg(dev, "Writing NCB fingerprints...\n");
1303 for (stride = 0; stride < search_area_size_in_strides; stride++) {
1304
1305 page = stride * rom_geo->stride_size_in_pages;
1306 byte = page * mtd->writesize;
1307
1308
1309 dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
1310 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1311 chip->ecc.write_page_raw(mtd, chip, buffer);
1312 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1313
1314
1315 status = chip->waitfunc(mtd, chip);
1316 if (status & NAND_STATUS_FAIL)
1317 dev_err(dev, "[%s] Write failed.\n", __func__);
1318 }
1319
1320
1321 chip->select_chip(mtd, saved_chip_number);
1322 return 0;
1323}
1324
1325static int __devinit mx23_boot_init(struct gpmi_nand_data *this)
1326{
1327 struct device *dev = this->dev;
1328 struct nand_chip *chip = &this->nand;
1329 struct mtd_info *mtd = &this->mtd;
1330 unsigned int block_count;
1331 unsigned int block;
1332 int chipnr;
1333 int page;
1334 loff_t byte;
1335 uint8_t block_mark;
1336 int ret = 0;
1337
1338
1339
1340
1341
1342
1343
1344 if (mx23_check_transcription_stamp(this))
1345 return 0;
1346
1347
1348
1349
1350
1351 dev_dbg(dev, "Transcribing bad block marks...\n");
1352
1353
1354 block_count = chip->chipsize >> chip->phys_erase_shift;
1355
1356
1357
1358
1359
1360 for (block = 0; block < block_count; block++) {
1361
1362
1363
1364
1365 chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
1366 page = block << (chip->phys_erase_shift - chip->page_shift);
1367 byte = block << chip->phys_erase_shift;
1368
1369
1370 chip->select_chip(mtd, chipnr);
1371 chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1372 block_mark = chip->read_byte(mtd);
1373 chip->select_chip(mtd, -1);
1374
1375
1376
1377
1378
1379
1380 if (block_mark != 0xff) {
1381 dev_dbg(dev, "Transcribing mark in block %u\n", block);
1382 ret = chip->block_markbad(mtd, byte);
1383 if (ret)
1384 dev_err(dev, "Failed to mark block bad with "
1385 "ret %d\n", ret);
1386 }
1387 }
1388
1389
1390 mx23_write_transcription_stamp(this);
1391 return 0;
1392}
1393
1394static int __devinit nand_boot_init(struct gpmi_nand_data *this)
1395{
1396 nand_boot_set_geometry(this);
1397
1398
1399 if (GPMI_IS_MX23(this))
1400 return mx23_boot_init(this);
1401 return 0;
1402}
1403
1404static int __devinit gpmi_set_geometry(struct gpmi_nand_data *this)
1405{
1406 int ret;
1407
1408
1409 gpmi_free_dma_buffer(this);
1410
1411
1412 ret = bch_set_geometry(this);
1413 if (ret) {
1414 pr_err("set geometry ret : %d\n", ret);
1415 return ret;
1416 }
1417
1418
1419 return gpmi_alloc_dma_buffer(this);
1420}
1421
1422static int gpmi_pre_bbt_scan(struct gpmi_nand_data *this)
1423{
1424 int ret;
1425
1426
1427 if (GPMI_IS_MX23(this))
1428 this->swap_block_mark = false;
1429 else
1430 this->swap_block_mark = true;
1431
1432
1433 ret = gpmi_set_geometry(this);
1434 if (ret)
1435 return ret;
1436
1437
1438 return nand_boot_init(this);
1439}
1440
1441static int gpmi_scan_bbt(struct mtd_info *mtd)
1442{
1443 struct nand_chip *chip = mtd->priv;
1444 struct gpmi_nand_data *this = chip->priv;
1445 int ret;
1446
1447
1448 ret = gpmi_pre_bbt_scan(this);
1449 if (ret)
1450 return ret;
1451
1452
1453 return nand_default_bbt(mtd);
1454}
1455
1456void gpmi_nfc_exit(struct gpmi_nand_data *this)
1457{
1458 nand_release(&this->mtd);
1459 gpmi_free_dma_buffer(this);
1460}
1461
1462static int __devinit gpmi_nfc_init(struct gpmi_nand_data *this)
1463{
1464 struct gpmi_nand_platform_data *pdata = this->pdata;
1465 struct mtd_info *mtd = &this->mtd;
1466 struct nand_chip *chip = &this->nand;
1467 int ret;
1468
1469
1470 this->current_chip = -1;
1471
1472
1473 mtd->priv = chip;
1474 mtd->name = "gpmi-nand";
1475 mtd->owner = THIS_MODULE;
1476
1477
1478 chip->priv = this;
1479 chip->select_chip = gpmi_select_chip;
1480 chip->cmd_ctrl = gpmi_cmd_ctrl;
1481 chip->dev_ready = gpmi_dev_ready;
1482 chip->read_byte = gpmi_read_byte;
1483 chip->read_buf = gpmi_read_buf;
1484 chip->write_buf = gpmi_write_buf;
1485 chip->ecc.read_page = gpmi_ecc_read_page;
1486 chip->ecc.write_page = gpmi_ecc_write_page;
1487 chip->ecc.read_oob = gpmi_ecc_read_oob;
1488 chip->ecc.write_oob = gpmi_ecc_write_oob;
1489 chip->scan_bbt = gpmi_scan_bbt;
1490 chip->badblock_pattern = &gpmi_bbt_descr;
1491 chip->block_markbad = gpmi_block_markbad;
1492 chip->options |= NAND_NO_SUBPAGE_WRITE;
1493 chip->ecc.mode = NAND_ECC_HW;
1494 chip->ecc.size = 1;
1495 chip->ecc.layout = &gpmi_hw_ecclayout;
1496
1497
1498 this->bch_geometry.payload_size = 1024;
1499 this->bch_geometry.auxiliary_size = 128;
1500 ret = gpmi_alloc_dma_buffer(this);
1501 if (ret)
1502 goto err_out;
1503
1504 ret = nand_scan(mtd, pdata->max_chip_count);
1505 if (ret) {
1506 pr_err("Chip scan failed\n");
1507 goto err_out;
1508 }
1509
1510 ret = mtd_device_parse_register(mtd, NULL, NULL,
1511 pdata->partitions, pdata->partition_count);
1512 if (ret)
1513 goto err_out;
1514 return 0;
1515
1516err_out:
1517 gpmi_nfc_exit(this);
1518 return ret;
1519}
1520
1521static int __devinit gpmi_nand_probe(struct platform_device *pdev)
1522{
1523 struct gpmi_nand_platform_data *pdata = pdev->dev.platform_data;
1524 struct gpmi_nand_data *this;
1525 int ret;
1526
1527 this = kzalloc(sizeof(*this), GFP_KERNEL);
1528 if (!this) {
1529 pr_err("Failed to allocate per-device memory\n");
1530 return -ENOMEM;
1531 }
1532
1533 platform_set_drvdata(pdev, this);
1534 this->pdev = pdev;
1535 this->dev = &pdev->dev;
1536 this->pdata = pdata;
1537
1538 if (pdata->platform_init) {
1539 ret = pdata->platform_init();
1540 if (ret)
1541 goto platform_init_error;
1542 }
1543
1544 ret = acquire_resources(this);
1545 if (ret)
1546 goto exit_acquire_resources;
1547
1548 ret = init_hardware(this);
1549 if (ret)
1550 goto exit_nfc_init;
1551
1552 ret = gpmi_nfc_init(this);
1553 if (ret)
1554 goto exit_nfc_init;
1555
1556 return 0;
1557
1558exit_nfc_init:
1559 release_resources(this);
1560platform_init_error:
1561exit_acquire_resources:
1562 platform_set_drvdata(pdev, NULL);
1563 kfree(this);
1564 return ret;
1565}
1566
1567static int __exit gpmi_nand_remove(struct platform_device *pdev)
1568{
1569 struct gpmi_nand_data *this = platform_get_drvdata(pdev);
1570
1571 gpmi_nfc_exit(this);
1572 release_resources(this);
1573 platform_set_drvdata(pdev, NULL);
1574 kfree(this);
1575 return 0;
1576}
1577
1578static const struct platform_device_id gpmi_ids[] = {
1579 {
1580 .name = "imx23-gpmi-nand",
1581 .driver_data = IS_MX23,
1582 }, {
1583 .name = "imx28-gpmi-nand",
1584 .driver_data = IS_MX28,
1585 }, {},
1586};
1587
1588static struct platform_driver gpmi_nand_driver = {
1589 .driver = {
1590 .name = "gpmi-nand",
1591 },
1592 .probe = gpmi_nand_probe,
1593 .remove = __exit_p(gpmi_nand_remove),
1594 .id_table = gpmi_ids,
1595};
1596
1597static int __init gpmi_nand_init(void)
1598{
1599 int err;
1600
1601 err = platform_driver_register(&gpmi_nand_driver);
1602 if (err == 0)
1603 printk(KERN_INFO "GPMI NAND driver registered. (IMX)\n");
1604 else
1605 pr_err("i.MX GPMI NAND driver registration failed\n");
1606 return err;
1607}
1608
1609static void __exit gpmi_nand_exit(void)
1610{
1611 platform_driver_unregister(&gpmi_nand_driver);
1612}
1613
1614module_init(gpmi_nand_init);
1615module_exit(gpmi_nand_exit);
1616
1617MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1618MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
1619MODULE_LICENSE("GPL");
1620