1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
32#include <linux/module.h>
33#include <linux/delay.h>
34#include <linux/errno.h>
35#include <linux/err.h>
36#include <linux/sched.h>
37#include <linux/slab.h>
38#include <linux/mm.h>
39#include <linux/nmi.h>
40#include <linux/types.h>
41#include <linux/mtd/mtd.h>
42#include <linux/mtd/nand.h>
43#include <linux/mtd/nand_ecc.h>
44#include <linux/mtd/nand_bch.h>
45#include <linux/interrupt.h>
46#include <linux/bitops.h>
47#include <linux/io.h>
48#include <linux/mtd/partitions.h>
49#include <linux/of.h>
50
51static int nand_get_device(struct mtd_info *mtd, int new_state);
52
53static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
54 struct mtd_oob_ops *ops);
55
56
57static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section,
58 struct mtd_oob_region *oobregion)
59{
60 struct nand_chip *chip = mtd_to_nand(mtd);
61 struct nand_ecc_ctrl *ecc = &chip->ecc;
62
63 if (section > 1)
64 return -ERANGE;
65
66 if (!section) {
67 oobregion->offset = 0;
68 if (mtd->oobsize == 16)
69 oobregion->length = 4;
70 else
71 oobregion->length = 3;
72 } else {
73 if (mtd->oobsize == 8)
74 return -ERANGE;
75
76 oobregion->offset = 6;
77 oobregion->length = ecc->total - 4;
78 }
79
80 return 0;
81}
82
83static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section,
84 struct mtd_oob_region *oobregion)
85{
86 if (section > 1)
87 return -ERANGE;
88
89 if (mtd->oobsize == 16) {
90 if (section)
91 return -ERANGE;
92
93 oobregion->length = 8;
94 oobregion->offset = 8;
95 } else {
96 oobregion->length = 2;
97 if (!section)
98 oobregion->offset = 3;
99 else
100 oobregion->offset = 6;
101 }
102
103 return 0;
104}
105
106const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = {
107 .ecc = nand_ooblayout_ecc_sp,
108 .free = nand_ooblayout_free_sp,
109};
110EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops);
111
112static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section,
113 struct mtd_oob_region *oobregion)
114{
115 struct nand_chip *chip = mtd_to_nand(mtd);
116 struct nand_ecc_ctrl *ecc = &chip->ecc;
117
118 if (section)
119 return -ERANGE;
120
121 oobregion->length = ecc->total;
122 oobregion->offset = mtd->oobsize - oobregion->length;
123
124 return 0;
125}
126
127static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section,
128 struct mtd_oob_region *oobregion)
129{
130 struct nand_chip *chip = mtd_to_nand(mtd);
131 struct nand_ecc_ctrl *ecc = &chip->ecc;
132
133 if (section)
134 return -ERANGE;
135
136 oobregion->length = mtd->oobsize - ecc->total - 2;
137 oobregion->offset = 2;
138
139 return 0;
140}
141
142const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = {
143 .ecc = nand_ooblayout_ecc_lp,
144 .free = nand_ooblayout_free_lp,
145};
146EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops);
147
148
149
150
151
152static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section,
153 struct mtd_oob_region *oobregion)
154{
155 struct nand_chip *chip = mtd_to_nand(mtd);
156 struct nand_ecc_ctrl *ecc = &chip->ecc;
157
158 if (section)
159 return -ERANGE;
160
161 switch (mtd->oobsize) {
162 case 64:
163 oobregion->offset = 40;
164 break;
165 case 128:
166 oobregion->offset = 80;
167 break;
168 default:
169 return -EINVAL;
170 }
171
172 oobregion->length = ecc->total;
173 if (oobregion->offset + oobregion->length > mtd->oobsize)
174 return -ERANGE;
175
176 return 0;
177}
178
179static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section,
180 struct mtd_oob_region *oobregion)
181{
182 struct nand_chip *chip = mtd_to_nand(mtd);
183 struct nand_ecc_ctrl *ecc = &chip->ecc;
184 int ecc_offset = 0;
185
186 if (section < 0 || section > 1)
187 return -ERANGE;
188
189 switch (mtd->oobsize) {
190 case 64:
191 ecc_offset = 40;
192 break;
193 case 128:
194 ecc_offset = 80;
195 break;
196 default:
197 return -EINVAL;
198 }
199
200 if (section == 0) {
201 oobregion->offset = 2;
202 oobregion->length = ecc_offset - 2;
203 } else {
204 oobregion->offset = ecc_offset + ecc->total;
205 oobregion->length = mtd->oobsize - oobregion->offset;
206 }
207
208 return 0;
209}
210
211static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = {
212 .ecc = nand_ooblayout_ecc_lp_hamming,
213 .free = nand_ooblayout_free_lp_hamming,
214};
215
216static int check_offs_len(struct mtd_info *mtd,
217 loff_t ofs, uint64_t len)
218{
219 struct nand_chip *chip = mtd_to_nand(mtd);
220 int ret = 0;
221
222
223 if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) {
224 pr_debug("%s: unaligned address\n", __func__);
225 ret = -EINVAL;
226 }
227
228
229 if (len & ((1ULL << chip->phys_erase_shift) - 1)) {
230 pr_debug("%s: length not block aligned\n", __func__);
231 ret = -EINVAL;
232 }
233
234 return ret;
235}
236
237
238
239
240
241
242
243static void nand_release_device(struct mtd_info *mtd)
244{
245 struct nand_chip *chip = mtd_to_nand(mtd);
246
247
248 spin_lock(&chip->controller->lock);
249 chip->controller->active = NULL;
250 chip->state = FL_READY;
251 wake_up(&chip->controller->wq);
252 spin_unlock(&chip->controller->lock);
253}
254
255
256
257
258
259
260
261static uint8_t nand_read_byte(struct mtd_info *mtd)
262{
263 struct nand_chip *chip = mtd_to_nand(mtd);
264 return readb(chip->IO_ADDR_R);
265}
266
267
268
269
270
271
272
273
274static uint8_t nand_read_byte16(struct mtd_info *mtd)
275{
276 struct nand_chip *chip = mtd_to_nand(mtd);
277 return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R));
278}
279
280
281
282
283
284
285
286static u16 nand_read_word(struct mtd_info *mtd)
287{
288 struct nand_chip *chip = mtd_to_nand(mtd);
289 return readw(chip->IO_ADDR_R);
290}
291
292
293
294
295
296
297
298
299static void nand_select_chip(struct mtd_info *mtd, int chipnr)
300{
301 struct nand_chip *chip = mtd_to_nand(mtd);
302
303 switch (chipnr) {
304 case -1:
305 chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
306 break;
307 case 0:
308 break;
309
310 default:
311 BUG();
312 }
313}
314
315
316
317
318
319
320
321
322static void nand_write_byte(struct mtd_info *mtd, uint8_t byte)
323{
324 struct nand_chip *chip = mtd_to_nand(mtd);
325
326 chip->write_buf(mtd, &byte, 1);
327}
328
329
330
331
332
333
334
335
336static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte)
337{
338 struct nand_chip *chip = mtd_to_nand(mtd);
339 uint16_t word = byte;
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357 chip->write_buf(mtd, (uint8_t *)&word, 2);
358}
359
360
361
362
363
364
365
366
367
368static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
369{
370 struct nand_chip *chip = mtd_to_nand(mtd);
371
372 iowrite8_rep(chip->IO_ADDR_W, buf, len);
373}
374
375
376
377
378
379
380
381
382
383static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
384{
385 struct nand_chip *chip = mtd_to_nand(mtd);
386
387 ioread8_rep(chip->IO_ADDR_R, buf, len);
388}
389
390
391
392
393
394
395
396
397
398static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
399{
400 struct nand_chip *chip = mtd_to_nand(mtd);
401 u16 *p = (u16 *) buf;
402
403 iowrite16_rep(chip->IO_ADDR_W, p, len >> 1);
404}
405
406
407
408
409
410
411
412
413
414static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len)
415{
416 struct nand_chip *chip = mtd_to_nand(mtd);
417 u16 *p = (u16 *) buf;
418
419 ioread16_rep(chip->IO_ADDR_R, p, len >> 1);
420}
421
422
423
424
425
426
427
428
429static int nand_block_bad(struct mtd_info *mtd, loff_t ofs)
430{
431 int page, page_end, res;
432 struct nand_chip *chip = mtd_to_nand(mtd);
433 u8 bad;
434
435 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
436 ofs += mtd->erasesize - mtd->writesize;
437
438 page = (int)(ofs >> chip->page_shift) & chip->pagemask;
439 page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1);
440
441 for (; page < page_end; page++) {
442 res = chip->ecc.read_oob(mtd, chip, page);
443 if (res)
444 return res;
445
446 bad = chip->oob_poi[chip->badblockpos];
447
448 if (likely(chip->badblockbits == 8))
449 res = bad != 0xFF;
450 else
451 res = hweight8(bad) < chip->badblockbits;
452 if (res)
453 return res;
454 }
455
456 return 0;
457}
458
459
460
461
462
463
464
465
466
467
468static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
469{
470 struct nand_chip *chip = mtd_to_nand(mtd);
471 struct mtd_oob_ops ops;
472 uint8_t buf[2] = { 0, 0 };
473 int ret = 0, res, i = 0;
474
475 memset(&ops, 0, sizeof(ops));
476 ops.oobbuf = buf;
477 ops.ooboffs = chip->badblockpos;
478 if (chip->options & NAND_BUSWIDTH_16) {
479 ops.ooboffs &= ~0x01;
480 ops.len = ops.ooblen = 2;
481 } else {
482 ops.len = ops.ooblen = 1;
483 }
484 ops.mode = MTD_OPS_PLACE_OOB;
485
486
487 if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
488 ofs += mtd->erasesize - mtd->writesize;
489 do {
490 res = nand_do_write_oob(mtd, ofs, &ops);
491 if (!ret)
492 ret = res;
493
494 i++;
495 ofs += mtd->writesize;
496 } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
497
498 return ret;
499}
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs)
521{
522 struct nand_chip *chip = mtd_to_nand(mtd);
523 int res, ret = 0;
524
525 if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) {
526 struct erase_info einfo;
527
528
529 memset(&einfo, 0, sizeof(einfo));
530 einfo.mtd = mtd;
531 einfo.addr = ofs;
532 einfo.len = 1ULL << chip->phys_erase_shift;
533 nand_erase_nand(mtd, &einfo, 0);
534
535
536 nand_get_device(mtd, FL_WRITING);
537 ret = chip->block_markbad(mtd, ofs);
538 nand_release_device(mtd);
539 }
540
541
542 if (chip->bbt) {
543 res = nand_markbad_bbt(mtd, ofs);
544 if (!ret)
545 ret = res;
546 }
547
548 if (!ret)
549 mtd->ecc_stats.badblocks++;
550
551 return ret;
552}
553
554
555
556
557
558
559
560
561static int nand_check_wp(struct mtd_info *mtd)
562{
563 struct nand_chip *chip = mtd_to_nand(mtd);
564
565
566 if (chip->options & NAND_BROKEN_XD)
567 return 0;
568
569
570 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
571 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
572}
573
574
575
576
577
578
579
580
581static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs)
582{
583 struct nand_chip *chip = mtd_to_nand(mtd);
584
585 if (!chip->bbt)
586 return 0;
587
588 return nand_isreserved_bbt(mtd, ofs);
589}
590
591
592
593
594
595
596
597
598
599
600static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt)
601{
602 struct nand_chip *chip = mtd_to_nand(mtd);
603
604 if (!chip->bbt)
605 return chip->block_bad(mtd, ofs);
606
607
608 return nand_isbad_bbt(mtd, ofs, allowbbt);
609}
610
611
612
613
614
615
616
617
618
619static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo)
620{
621 struct nand_chip *chip = mtd_to_nand(mtd);
622 int i;
623
624
625 for (i = 0; i < timeo; i++) {
626 if (chip->dev_ready(mtd))
627 break;
628 touch_softlockup_watchdog();
629 mdelay(1);
630 }
631}
632
633
634
635
636
637
638
639void nand_wait_ready(struct mtd_info *mtd)
640{
641 struct nand_chip *chip = mtd_to_nand(mtd);
642 unsigned long timeo = 400;
643
644 if (in_interrupt() || oops_in_progress)
645 return panic_nand_wait_ready(mtd, timeo);
646
647
648 timeo = jiffies + msecs_to_jiffies(timeo);
649 do {
650 if (chip->dev_ready(mtd))
651 return;
652 cond_resched();
653 } while (time_before(jiffies, timeo));
654
655 if (!chip->dev_ready(mtd))
656 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
657}
658EXPORT_SYMBOL_GPL(nand_wait_ready);
659
660
661
662
663
664
665
666
667static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo)
668{
669 register struct nand_chip *chip = mtd_to_nand(mtd);
670
671 timeo = jiffies + msecs_to_jiffies(timeo);
672 do {
673 if ((chip->read_byte(mtd) & NAND_STATUS_READY))
674 break;
675 touch_softlockup_watchdog();
676 } while (time_before(jiffies, timeo));
677};
678
679
680
681
682
683
684
685
686
687
688
689static void nand_command(struct mtd_info *mtd, unsigned int command,
690 int column, int page_addr)
691{
692 register struct nand_chip *chip = mtd_to_nand(mtd);
693 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
694
695
696 if (command == NAND_CMD_SEQIN) {
697 int readcmd;
698
699 if (column >= mtd->writesize) {
700
701 column -= mtd->writesize;
702 readcmd = NAND_CMD_READOOB;
703 } else if (column < 256) {
704
705 readcmd = NAND_CMD_READ0;
706 } else {
707 column -= 256;
708 readcmd = NAND_CMD_READ1;
709 }
710 chip->cmd_ctrl(mtd, readcmd, ctrl);
711 ctrl &= ~NAND_CTRL_CHANGE;
712 }
713 chip->cmd_ctrl(mtd, command, ctrl);
714
715
716 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
717
718 if (column != -1) {
719
720 if (chip->options & NAND_BUSWIDTH_16 &&
721 !nand_opcode_8bits(command))
722 column >>= 1;
723 chip->cmd_ctrl(mtd, column, ctrl);
724 ctrl &= ~NAND_CTRL_CHANGE;
725 }
726 if (page_addr != -1) {
727 chip->cmd_ctrl(mtd, page_addr, ctrl);
728 ctrl &= ~NAND_CTRL_CHANGE;
729 chip->cmd_ctrl(mtd, page_addr >> 8, ctrl);
730
731 if (chip->chipsize > (32 << 20))
732 chip->cmd_ctrl(mtd, page_addr >> 16, ctrl);
733 }
734 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
735
736
737
738
739
740 switch (command) {
741
742 case NAND_CMD_PAGEPROG:
743 case NAND_CMD_ERASE1:
744 case NAND_CMD_ERASE2:
745 case NAND_CMD_SEQIN:
746 case NAND_CMD_STATUS:
747 case NAND_CMD_READID:
748 case NAND_CMD_SET_FEATURES:
749 return;
750
751 case NAND_CMD_RESET:
752 if (chip->dev_ready)
753 break;
754 udelay(chip->chip_delay);
755 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
756 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
757 chip->cmd_ctrl(mtd,
758 NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
759
760 nand_wait_status_ready(mtd, 250);
761 return;
762
763
764 case NAND_CMD_READ0:
765
766
767
768
769
770
771 if (column == -1 && page_addr == -1)
772 return;
773
774 default:
775
776
777
778
779 if (!chip->dev_ready) {
780 udelay(chip->chip_delay);
781 return;
782 }
783 }
784
785
786
787
788 ndelay(100);
789
790 nand_wait_ready(mtd);
791}
792
793static void nand_ccs_delay(struct nand_chip *chip)
794{
795
796
797
798
799 if (!(chip->options & NAND_WAIT_TCCS))
800 return;
801
802
803
804
805
806 if (chip->data_interface && chip->data_interface->timings.sdr.tCCS_min)
807 ndelay(chip->data_interface->timings.sdr.tCCS_min / 1000);
808 else
809 ndelay(500);
810}
811
812
813
814
815
816
817
818
819
820
821
822
823static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
824 int column, int page_addr)
825{
826 register struct nand_chip *chip = mtd_to_nand(mtd);
827
828
829 if (command == NAND_CMD_READOOB) {
830 column += mtd->writesize;
831 command = NAND_CMD_READ0;
832 }
833
834
835 chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
836
837 if (column != -1 || page_addr != -1) {
838 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
839
840
841 if (column != -1) {
842
843 if (chip->options & NAND_BUSWIDTH_16 &&
844 !nand_opcode_8bits(command))
845 column >>= 1;
846 chip->cmd_ctrl(mtd, column, ctrl);
847 ctrl &= ~NAND_CTRL_CHANGE;
848
849
850 if (!nand_opcode_8bits(command))
851 chip->cmd_ctrl(mtd, column >> 8, ctrl);
852 }
853 if (page_addr != -1) {
854 chip->cmd_ctrl(mtd, page_addr, ctrl);
855 chip->cmd_ctrl(mtd, page_addr >> 8,
856 NAND_NCE | NAND_ALE);
857
858 if (chip->chipsize > (128 << 20))
859 chip->cmd_ctrl(mtd, page_addr >> 16,
860 NAND_NCE | NAND_ALE);
861 }
862 }
863 chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
864
865
866
867
868
869 switch (command) {
870
871 case NAND_CMD_CACHEDPROG:
872 case NAND_CMD_PAGEPROG:
873 case NAND_CMD_ERASE1:
874 case NAND_CMD_ERASE2:
875 case NAND_CMD_SEQIN:
876 case NAND_CMD_STATUS:
877 case NAND_CMD_READID:
878 case NAND_CMD_SET_FEATURES:
879 return;
880
881 case NAND_CMD_RNDIN:
882 nand_ccs_delay(chip);
883 return;
884
885 case NAND_CMD_RESET:
886 if (chip->dev_ready)
887 break;
888 udelay(chip->chip_delay);
889 chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
890 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
891 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
892 NAND_NCE | NAND_CTRL_CHANGE);
893
894 nand_wait_status_ready(mtd, 250);
895 return;
896
897 case NAND_CMD_RNDOUT:
898
899 chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
900 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
901 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
902 NAND_NCE | NAND_CTRL_CHANGE);
903
904 nand_ccs_delay(chip);
905 return;
906
907 case NAND_CMD_READ0:
908
909
910
911
912
913
914 if (column == -1 && page_addr == -1)
915 return;
916
917 chip->cmd_ctrl(mtd, NAND_CMD_READSTART,
918 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
919 chip->cmd_ctrl(mtd, NAND_CMD_NONE,
920 NAND_NCE | NAND_CTRL_CHANGE);
921
922
923 default:
924
925
926
927
928 if (!chip->dev_ready) {
929 udelay(chip->chip_delay);
930 return;
931 }
932 }
933
934
935
936
937
938 ndelay(100);
939
940 nand_wait_ready(mtd);
941}
942
943
944
945
946
947
948
949
950
951static void panic_nand_get_device(struct nand_chip *chip,
952 struct mtd_info *mtd, int new_state)
953{
954
955 chip->controller->active = chip;
956 chip->state = new_state;
957}
958
959
960
961
962
963
964
965
966static int
967nand_get_device(struct mtd_info *mtd, int new_state)
968{
969 struct nand_chip *chip = mtd_to_nand(mtd);
970 spinlock_t *lock = &chip->controller->lock;
971 wait_queue_head_t *wq = &chip->controller->wq;
972 DECLARE_WAITQUEUE(wait, current);
973retry:
974 spin_lock(lock);
975
976
977 if (!chip->controller->active)
978 chip->controller->active = chip;
979
980 if (chip->controller->active == chip && chip->state == FL_READY) {
981 chip->state = new_state;
982 spin_unlock(lock);
983 return 0;
984 }
985 if (new_state == FL_PM_SUSPENDED) {
986 if (chip->controller->active->state == FL_PM_SUSPENDED) {
987 chip->state = FL_PM_SUSPENDED;
988 spin_unlock(lock);
989 return 0;
990 }
991 }
992 set_current_state(TASK_UNINTERRUPTIBLE);
993 add_wait_queue(wq, &wait);
994 spin_unlock(lock);
995 schedule();
996 remove_wait_queue(wq, &wait);
997 goto retry;
998}
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip,
1011 unsigned long timeo)
1012{
1013 int i;
1014 for (i = 0; i < timeo; i++) {
1015 if (chip->dev_ready) {
1016 if (chip->dev_ready(mtd))
1017 break;
1018 } else {
1019 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1020 break;
1021 }
1022 mdelay(1);
1023 }
1024}
1025
1026
1027
1028
1029
1030
1031
1032
1033static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
1034{
1035
1036 int status;
1037 unsigned long timeo = 400;
1038
1039
1040
1041
1042
1043 ndelay(100);
1044
1045 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
1046
1047 if (in_interrupt() || oops_in_progress)
1048 panic_nand_wait(mtd, chip, timeo);
1049 else {
1050 timeo = jiffies + msecs_to_jiffies(timeo);
1051 do {
1052 if (chip->dev_ready) {
1053 if (chip->dev_ready(mtd))
1054 break;
1055 } else {
1056 if (chip->read_byte(mtd) & NAND_STATUS_READY)
1057 break;
1058 }
1059 cond_resched();
1060 } while (time_before(jiffies, timeo));
1061 }
1062
1063 status = (int)chip->read_byte(mtd);
1064
1065 WARN_ON(!(status & NAND_STATUS_READY));
1066 return status;
1067}
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078static int nand_reset_data_interface(struct nand_chip *chip, int chipnr)
1079{
1080 struct mtd_info *mtd = nand_to_mtd(chip);
1081 const struct nand_data_interface *conf;
1082 int ret;
1083
1084 if (!chip->setup_data_interface)
1085 return 0;
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101 conf = nand_get_default_data_interface();
1102 ret = chip->setup_data_interface(mtd, chipnr, conf);
1103 if (ret)
1104 pr_err("Failed to configure data interface to SDR timing mode 0\n");
1105
1106 return ret;
1107}
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122static int nand_setup_data_interface(struct nand_chip *chip, int chipnr)
1123{
1124 struct mtd_info *mtd = nand_to_mtd(chip);
1125 int ret;
1126
1127 if (!chip->setup_data_interface || !chip->data_interface)
1128 return 0;
1129
1130
1131
1132
1133
1134 if (chip->onfi_version &&
1135 (le16_to_cpu(chip->onfi_params.opt_cmd) &
1136 ONFI_OPT_CMD_SET_GET_FEATURES)) {
1137 u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = {
1138 chip->onfi_timing_mode_default,
1139 };
1140
1141 ret = chip->onfi_set_features(mtd, chip,
1142 ONFI_FEATURE_ADDR_TIMING_MODE,
1143 tmode_param);
1144 if (ret)
1145 goto err;
1146 }
1147
1148 ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface);
1149err:
1150 return ret;
1151}
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167static int nand_init_data_interface(struct nand_chip *chip)
1168{
1169 struct mtd_info *mtd = nand_to_mtd(chip);
1170 int modes, mode, ret;
1171
1172 if (!chip->setup_data_interface)
1173 return 0;
1174
1175
1176
1177
1178
1179
1180 modes = onfi_get_async_timing_mode(chip);
1181 if (modes == ONFI_TIMING_MODE_UNKNOWN) {
1182 if (!chip->onfi_timing_mode_default)
1183 return 0;
1184
1185 modes = GENMASK(chip->onfi_timing_mode_default, 0);
1186 }
1187
1188 chip->data_interface = kzalloc(sizeof(*chip->data_interface),
1189 GFP_KERNEL);
1190 if (!chip->data_interface)
1191 return -ENOMEM;
1192
1193 for (mode = fls(modes) - 1; mode >= 0; mode--) {
1194 ret = onfi_init_data_interface(chip, chip->data_interface,
1195 NAND_SDR_IFACE, mode);
1196 if (ret)
1197 continue;
1198
1199
1200 ret = chip->setup_data_interface(mtd,
1201 NAND_DATA_IFACE_CHECK_ONLY,
1202 chip->data_interface);
1203 if (!ret) {
1204 chip->onfi_timing_mode_default = mode;
1205 break;
1206 }
1207 }
1208
1209 return 0;
1210}
1211
1212static void nand_release_data_interface(struct nand_chip *chip)
1213{
1214 kfree(chip->data_interface);
1215}
1216
1217
1218
1219
1220
1221
1222
1223
1224int nand_reset(struct nand_chip *chip, int chipnr)
1225{
1226 struct mtd_info *mtd = nand_to_mtd(chip);
1227 int ret;
1228
1229 ret = nand_reset_data_interface(chip, chipnr);
1230 if (ret)
1231 return ret;
1232
1233
1234
1235
1236
1237 chip->select_chip(mtd, chipnr);
1238 chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1239 chip->select_chip(mtd, -1);
1240
1241 chip->select_chip(mtd, chipnr);
1242 ret = nand_setup_data_interface(chip, chipnr);
1243 chip->select_chip(mtd, -1);
1244 if (ret)
1245 return ret;
1246
1247 return 0;
1248}
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
1264 uint64_t len, int invert)
1265{
1266 int ret = 0;
1267 int status, page;
1268 struct nand_chip *chip = mtd_to_nand(mtd);
1269
1270
1271 page = ofs >> chip->page_shift;
1272 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
1273
1274
1275 page = (ofs + len) >> chip->page_shift;
1276 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
1277 (page | invert) & chip->pagemask);
1278
1279
1280 status = chip->waitfunc(mtd, chip);
1281
1282 if (status & NAND_STATUS_FAIL) {
1283 pr_debug("%s: error status = 0x%08x\n",
1284 __func__, status);
1285 ret = -EIO;
1286 }
1287
1288 return ret;
1289}
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1300{
1301 int ret = 0;
1302 int chipnr;
1303 struct nand_chip *chip = mtd_to_nand(mtd);
1304
1305 pr_debug("%s: start = 0x%012llx, len = %llu\n",
1306 __func__, (unsigned long long)ofs, len);
1307
1308 if (check_offs_len(mtd, ofs, len))
1309 return -EINVAL;
1310
1311
1312 if (ofs + len == mtd->size)
1313 len -= mtd->erasesize;
1314
1315 nand_get_device(mtd, FL_UNLOCKING);
1316
1317
1318 chipnr = ofs >> chip->chip_shift;
1319
1320
1321
1322
1323
1324
1325
1326
1327 nand_reset(chip, chipnr);
1328
1329 chip->select_chip(mtd, chipnr);
1330
1331
1332 if (nand_check_wp(mtd)) {
1333 pr_debug("%s: device is write protected!\n",
1334 __func__);
1335 ret = -EIO;
1336 goto out;
1337 }
1338
1339 ret = __nand_unlock(mtd, ofs, len, 0);
1340
1341out:
1342 chip->select_chip(mtd, -1);
1343 nand_release_device(mtd);
1344
1345 return ret;
1346}
1347EXPORT_SYMBOL(nand_unlock);
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1363{
1364 int ret = 0;
1365 int chipnr, status, page;
1366 struct nand_chip *chip = mtd_to_nand(mtd);
1367
1368 pr_debug("%s: start = 0x%012llx, len = %llu\n",
1369 __func__, (unsigned long long)ofs, len);
1370
1371 if (check_offs_len(mtd, ofs, len))
1372 return -EINVAL;
1373
1374 nand_get_device(mtd, FL_LOCKING);
1375
1376
1377 chipnr = ofs >> chip->chip_shift;
1378
1379
1380
1381
1382
1383
1384
1385
1386 nand_reset(chip, chipnr);
1387
1388 chip->select_chip(mtd, chipnr);
1389
1390
1391 if (nand_check_wp(mtd)) {
1392 pr_debug("%s: device is write protected!\n",
1393 __func__);
1394 status = MTD_ERASE_FAILED;
1395 ret = -EIO;
1396 goto out;
1397 }
1398
1399
1400 page = ofs >> chip->page_shift;
1401 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1402
1403
1404 status = chip->waitfunc(mtd, chip);
1405
1406 if (status & NAND_STATUS_FAIL) {
1407 pr_debug("%s: error status = 0x%08x\n",
1408 __func__, status);
1409 ret = -EIO;
1410 goto out;
1411 }
1412
1413 ret = __nand_unlock(mtd, ofs, len, 0x1);
1414
1415out:
1416 chip->select_chip(mtd, -1);
1417 nand_release_device(mtd);
1418
1419 return ret;
1420}
1421EXPORT_SYMBOL(nand_lock);
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold)
1443{
1444 const unsigned char *bitmap = buf;
1445 int bitflips = 0;
1446 int weight;
1447
1448 for (; len && ((uintptr_t)bitmap) % sizeof(long);
1449 len--, bitmap++) {
1450 weight = hweight8(*bitmap);
1451 bitflips += BITS_PER_BYTE - weight;
1452 if (unlikely(bitflips > bitflips_threshold))
1453 return -EBADMSG;
1454 }
1455
1456 for (; len >= sizeof(long);
1457 len -= sizeof(long), bitmap += sizeof(long)) {
1458 unsigned long d = *((unsigned long *)bitmap);
1459 if (d == ~0UL)
1460 continue;
1461 weight = hweight_long(d);
1462 bitflips += BITS_PER_LONG - weight;
1463 if (unlikely(bitflips > bitflips_threshold))
1464 return -EBADMSG;
1465 }
1466
1467 for (; len > 0; len--, bitmap++) {
1468 weight = hweight8(*bitmap);
1469 bitflips += BITS_PER_BYTE - weight;
1470 if (unlikely(bitflips > bitflips_threshold))
1471 return -EBADMSG;
1472 }
1473
1474 return bitflips;
1475}
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516int nand_check_erased_ecc_chunk(void *data, int datalen,
1517 void *ecc, int ecclen,
1518 void *extraoob, int extraooblen,
1519 int bitflips_threshold)
1520{
1521 int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0;
1522
1523 data_bitflips = nand_check_erased_buf(data, datalen,
1524 bitflips_threshold);
1525 if (data_bitflips < 0)
1526 return data_bitflips;
1527
1528 bitflips_threshold -= data_bitflips;
1529
1530 ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold);
1531 if (ecc_bitflips < 0)
1532 return ecc_bitflips;
1533
1534 bitflips_threshold -= ecc_bitflips;
1535
1536 extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen,
1537 bitflips_threshold);
1538 if (extraoob_bitflips < 0)
1539 return extraoob_bitflips;
1540
1541 if (data_bitflips)
1542 memset(data, 0xff, datalen);
1543
1544 if (ecc_bitflips)
1545 memset(ecc, 0xff, ecclen);
1546
1547 if (extraoob_bitflips)
1548 memset(extraoob, 0xff, extraooblen);
1549
1550 return data_bitflips + ecc_bitflips + extraoob_bitflips;
1551}
1552EXPORT_SYMBOL(nand_check_erased_ecc_chunk);
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1565 uint8_t *buf, int oob_required, int page)
1566{
1567 chip->read_buf(mtd, buf, mtd->writesize);
1568 if (oob_required)
1569 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1570 return 0;
1571}
1572EXPORT_SYMBOL(nand_read_page_raw);
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584static int nand_read_page_raw_syndrome(struct mtd_info *mtd,
1585 struct nand_chip *chip, uint8_t *buf,
1586 int oob_required, int page)
1587{
1588 int eccsize = chip->ecc.size;
1589 int eccbytes = chip->ecc.bytes;
1590 uint8_t *oob = chip->oob_poi;
1591 int steps, size;
1592
1593 for (steps = chip->ecc.steps; steps > 0; steps--) {
1594 chip->read_buf(mtd, buf, eccsize);
1595 buf += eccsize;
1596
1597 if (chip->ecc.prepad) {
1598 chip->read_buf(mtd, oob, chip->ecc.prepad);
1599 oob += chip->ecc.prepad;
1600 }
1601
1602 chip->read_buf(mtd, oob, eccbytes);
1603 oob += eccbytes;
1604
1605 if (chip->ecc.postpad) {
1606 chip->read_buf(mtd, oob, chip->ecc.postpad);
1607 oob += chip->ecc.postpad;
1608 }
1609 }
1610
1611 size = mtd->oobsize - (oob - chip->oob_poi);
1612 if (size)
1613 chip->read_buf(mtd, oob, size);
1614
1615 return 0;
1616}
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1627 uint8_t *buf, int oob_required, int page)
1628{
1629 int i, eccsize = chip->ecc.size, ret;
1630 int eccbytes = chip->ecc.bytes;
1631 int eccsteps = chip->ecc.steps;
1632 uint8_t *p = buf;
1633 uint8_t *ecc_calc = chip->buffers->ecccalc;
1634 uint8_t *ecc_code = chip->buffers->ecccode;
1635 unsigned int max_bitflips = 0;
1636
1637 chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
1638
1639 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1640 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1641
1642 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1643 chip->ecc.total);
1644 if (ret)
1645 return ret;
1646
1647 eccsteps = chip->ecc.steps;
1648 p = buf;
1649
1650 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1651 int stat;
1652
1653 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1654 if (stat < 0) {
1655 mtd->ecc_stats.failed++;
1656 } else {
1657 mtd->ecc_stats.corrected += stat;
1658 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1659 }
1660 }
1661 return max_bitflips;
1662}
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
1674 uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
1675 int page)
1676{
1677 int start_step, end_step, num_steps, ret;
1678 uint8_t *p;
1679 int data_col_addr, i, gaps = 0;
1680 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1681 int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1;
1682 int index, section = 0;
1683 unsigned int max_bitflips = 0;
1684 struct mtd_oob_region oobregion = { };
1685
1686
1687 start_step = data_offs / chip->ecc.size;
1688 end_step = (data_offs + readlen - 1) / chip->ecc.size;
1689 num_steps = end_step - start_step + 1;
1690 index = start_step * chip->ecc.bytes;
1691
1692
1693 datafrag_len = num_steps * chip->ecc.size;
1694 eccfrag_len = num_steps * chip->ecc.bytes;
1695
1696 data_col_addr = start_step * chip->ecc.size;
1697
1698 if (data_col_addr != 0)
1699 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1);
1700
1701 p = bufpoi + data_col_addr;
1702 chip->read_buf(mtd, p, datafrag_len);
1703
1704
1705 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size)
1706 chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]);
1707
1708
1709
1710
1711
1712 ret = mtd_ooblayout_find_eccregion(mtd, index, §ion, &oobregion);
1713 if (ret)
1714 return ret;
1715
1716 if (oobregion.length < eccfrag_len)
1717 gaps = 1;
1718
1719 if (gaps) {
1720 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1721 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1722 } else {
1723
1724
1725
1726
1727 aligned_pos = oobregion.offset & ~(busw - 1);
1728 aligned_len = eccfrag_len;
1729 if (oobregion.offset & (busw - 1))
1730 aligned_len++;
1731 if ((oobregion.offset + (num_steps * chip->ecc.bytes)) &
1732 (busw - 1))
1733 aligned_len++;
1734
1735 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1736 mtd->writesize + aligned_pos, -1);
1737 chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len);
1738 }
1739
1740 ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode,
1741 chip->oob_poi, index, eccfrag_len);
1742 if (ret)
1743 return ret;
1744
1745 p = bufpoi + data_col_addr;
1746 for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) {
1747 int stat;
1748
1749 stat = chip->ecc.correct(mtd, p,
1750 &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]);
1751 if (stat == -EBADMSG &&
1752 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1753
1754 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1755 &chip->buffers->ecccode[i],
1756 chip->ecc.bytes,
1757 NULL, 0,
1758 chip->ecc.strength);
1759 }
1760
1761 if (stat < 0) {
1762 mtd->ecc_stats.failed++;
1763 } else {
1764 mtd->ecc_stats.corrected += stat;
1765 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1766 }
1767 }
1768 return max_bitflips;
1769}
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1782 uint8_t *buf, int oob_required, int page)
1783{
1784 int i, eccsize = chip->ecc.size, ret;
1785 int eccbytes = chip->ecc.bytes;
1786 int eccsteps = chip->ecc.steps;
1787 uint8_t *p = buf;
1788 uint8_t *ecc_calc = chip->buffers->ecccalc;
1789 uint8_t *ecc_code = chip->buffers->ecccode;
1790 unsigned int max_bitflips = 0;
1791
1792 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1793 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1794 chip->read_buf(mtd, p, eccsize);
1795 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1796 }
1797 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1798
1799 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1800 chip->ecc.total);
1801 if (ret)
1802 return ret;
1803
1804 eccsteps = chip->ecc.steps;
1805 p = buf;
1806
1807 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1808 int stat;
1809
1810 stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1811 if (stat == -EBADMSG &&
1812 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1813
1814 stat = nand_check_erased_ecc_chunk(p, eccsize,
1815 &ecc_code[i], eccbytes,
1816 NULL, 0,
1817 chip->ecc.strength);
1818 }
1819
1820 if (stat < 0) {
1821 mtd->ecc_stats.failed++;
1822 } else {
1823 mtd->ecc_stats.corrected += stat;
1824 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1825 }
1826 }
1827 return max_bitflips;
1828}
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd,
1845 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
1846{
1847 int i, eccsize = chip->ecc.size, ret;
1848 int eccbytes = chip->ecc.bytes;
1849 int eccsteps = chip->ecc.steps;
1850 uint8_t *p = buf;
1851 uint8_t *ecc_code = chip->buffers->ecccode;
1852 uint8_t *ecc_calc = chip->buffers->ecccalc;
1853 unsigned int max_bitflips = 0;
1854
1855
1856 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
1857 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1858 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1859
1860 ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0,
1861 chip->ecc.total);
1862 if (ret)
1863 return ret;
1864
1865 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1866 int stat;
1867
1868 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1869 chip->read_buf(mtd, p, eccsize);
1870 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
1871
1872 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
1873 if (stat == -EBADMSG &&
1874 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1875
1876 stat = nand_check_erased_ecc_chunk(p, eccsize,
1877 &ecc_code[i], eccbytes,
1878 NULL, 0,
1879 chip->ecc.strength);
1880 }
1881
1882 if (stat < 0) {
1883 mtd->ecc_stats.failed++;
1884 } else {
1885 mtd->ecc_stats.corrected += stat;
1886 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1887 }
1888 }
1889 return max_bitflips;
1890}
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
1904 uint8_t *buf, int oob_required, int page)
1905{
1906 int i, eccsize = chip->ecc.size;
1907 int eccbytes = chip->ecc.bytes;
1908 int eccsteps = chip->ecc.steps;
1909 int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
1910 uint8_t *p = buf;
1911 uint8_t *oob = chip->oob_poi;
1912 unsigned int max_bitflips = 0;
1913
1914 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1915 int stat;
1916
1917 chip->ecc.hwctl(mtd, NAND_ECC_READ);
1918 chip->read_buf(mtd, p, eccsize);
1919
1920 if (chip->ecc.prepad) {
1921 chip->read_buf(mtd, oob, chip->ecc.prepad);
1922 oob += chip->ecc.prepad;
1923 }
1924
1925 chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
1926 chip->read_buf(mtd, oob, eccbytes);
1927 stat = chip->ecc.correct(mtd, p, oob, NULL);
1928
1929 oob += eccbytes;
1930
1931 if (chip->ecc.postpad) {
1932 chip->read_buf(mtd, oob, chip->ecc.postpad);
1933 oob += chip->ecc.postpad;
1934 }
1935
1936 if (stat == -EBADMSG &&
1937 (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) {
1938
1939 stat = nand_check_erased_ecc_chunk(p, chip->ecc.size,
1940 oob - eccpadbytes,
1941 eccpadbytes,
1942 NULL, 0,
1943 chip->ecc.strength);
1944 }
1945
1946 if (stat < 0) {
1947 mtd->ecc_stats.failed++;
1948 } else {
1949 mtd->ecc_stats.corrected += stat;
1950 max_bitflips = max_t(unsigned int, max_bitflips, stat);
1951 }
1952 }
1953
1954
1955 i = mtd->oobsize - (oob - chip->oob_poi);
1956 if (i)
1957 chip->read_buf(mtd, oob, i);
1958
1959 return max_bitflips;
1960}
1961
1962
1963
1964
1965
1966
1967
1968
1969static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob,
1970 struct mtd_oob_ops *ops, size_t len)
1971{
1972 struct nand_chip *chip = mtd_to_nand(mtd);
1973 int ret;
1974
1975 switch (ops->mode) {
1976
1977 case MTD_OPS_PLACE_OOB:
1978 case MTD_OPS_RAW:
1979 memcpy(oob, chip->oob_poi + ops->ooboffs, len);
1980 return oob + len;
1981
1982 case MTD_OPS_AUTO_OOB:
1983 ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi,
1984 ops->ooboffs, len);
1985 BUG_ON(ret);
1986 return oob + len;
1987
1988 default:
1989 BUG();
1990 }
1991 return NULL;
1992}
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode)
2004{
2005 struct nand_chip *chip = mtd_to_nand(mtd);
2006
2007 pr_debug("setting READ RETRY mode %d\n", retry_mode);
2008
2009 if (retry_mode >= chip->read_retries)
2010 return -EINVAL;
2011
2012 if (!chip->setup_read_retry)
2013 return -EOPNOTSUPP;
2014
2015 return chip->setup_read_retry(mtd, retry_mode);
2016}
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
2027 struct mtd_oob_ops *ops)
2028{
2029 int chipnr, page, realpage, col, bytes, aligned, oob_required;
2030 struct nand_chip *chip = mtd_to_nand(mtd);
2031 int ret = 0;
2032 uint32_t readlen = ops->len;
2033 uint32_t oobreadlen = ops->ooblen;
2034 uint32_t max_oobsize = mtd_oobavail(mtd, ops);
2035
2036 uint8_t *bufpoi, *oob, *buf;
2037 int use_bufpoi;
2038 unsigned int max_bitflips = 0;
2039 int retry_mode = 0;
2040 bool ecc_fail = false;
2041
2042 chipnr = (int)(from >> chip->chip_shift);
2043 chip->select_chip(mtd, chipnr);
2044
2045 realpage = (int)(from >> chip->page_shift);
2046 page = realpage & chip->pagemask;
2047
2048 col = (int)(from & (mtd->writesize - 1));
2049
2050 buf = ops->datbuf;
2051 oob = ops->oobbuf;
2052 oob_required = oob ? 1 : 0;
2053
2054 while (1) {
2055 unsigned int ecc_failures = mtd->ecc_stats.failed;
2056
2057 bytes = min(mtd->writesize - col, readlen);
2058 aligned = (bytes == mtd->writesize);
2059
2060 if (!aligned)
2061 use_bufpoi = 1;
2062 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2063 use_bufpoi = !virt_addr_valid(buf) ||
2064 !IS_ALIGNED((unsigned long)buf,
2065 chip->buf_align);
2066 else
2067 use_bufpoi = 0;
2068
2069
2070 if (realpage != chip->pagebuf || oob) {
2071 bufpoi = use_bufpoi ? chip->buffers->databuf : buf;
2072
2073 if (use_bufpoi && aligned)
2074 pr_debug("%s: using read bounce buffer for buf@%p\n",
2075 __func__, buf);
2076
2077read_retry:
2078 if (nand_standard_page_accessors(&chip->ecc))
2079 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
2080
2081
2082
2083
2084
2085 if (unlikely(ops->mode == MTD_OPS_RAW))
2086 ret = chip->ecc.read_page_raw(mtd, chip, bufpoi,
2087 oob_required,
2088 page);
2089 else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) &&
2090 !oob)
2091 ret = chip->ecc.read_subpage(mtd, chip,
2092 col, bytes, bufpoi,
2093 page);
2094 else
2095 ret = chip->ecc.read_page(mtd, chip, bufpoi,
2096 oob_required, page);
2097 if (ret < 0) {
2098 if (use_bufpoi)
2099
2100 chip->pagebuf = -1;
2101 break;
2102 }
2103
2104
2105 if (use_bufpoi) {
2106 if (!NAND_HAS_SUBPAGE_READ(chip) && !oob &&
2107 !(mtd->ecc_stats.failed - ecc_failures) &&
2108 (ops->mode != MTD_OPS_RAW)) {
2109 chip->pagebuf = realpage;
2110 chip->pagebuf_bitflips = ret;
2111 } else {
2112
2113 chip->pagebuf = -1;
2114 }
2115 memcpy(buf, chip->buffers->databuf + col, bytes);
2116 }
2117
2118 if (unlikely(oob)) {
2119 int toread = min(oobreadlen, max_oobsize);
2120
2121 if (toread) {
2122 oob = nand_transfer_oob(mtd,
2123 oob, ops, toread);
2124 oobreadlen -= toread;
2125 }
2126 }
2127
2128 if (chip->options & NAND_NEED_READRDY) {
2129
2130 if (!chip->dev_ready)
2131 udelay(chip->chip_delay);
2132 else
2133 nand_wait_ready(mtd);
2134 }
2135
2136 if (mtd->ecc_stats.failed - ecc_failures) {
2137 if (retry_mode + 1 < chip->read_retries) {
2138 retry_mode++;
2139 ret = nand_setup_read_retry(mtd,
2140 retry_mode);
2141 if (ret < 0)
2142 break;
2143
2144
2145 mtd->ecc_stats.failed = ecc_failures;
2146 goto read_retry;
2147 } else {
2148
2149 ecc_fail = true;
2150 }
2151 }
2152
2153 buf += bytes;
2154 max_bitflips = max_t(unsigned int, max_bitflips, ret);
2155 } else {
2156 memcpy(buf, chip->buffers->databuf + col, bytes);
2157 buf += bytes;
2158 max_bitflips = max_t(unsigned int, max_bitflips,
2159 chip->pagebuf_bitflips);
2160 }
2161
2162 readlen -= bytes;
2163
2164
2165 if (retry_mode) {
2166 ret = nand_setup_read_retry(mtd, 0);
2167 if (ret < 0)
2168 break;
2169 retry_mode = 0;
2170 }
2171
2172 if (!readlen)
2173 break;
2174
2175
2176 col = 0;
2177
2178 realpage++;
2179
2180 page = realpage & chip->pagemask;
2181
2182 if (!page) {
2183 chipnr++;
2184 chip->select_chip(mtd, -1);
2185 chip->select_chip(mtd, chipnr);
2186 }
2187 }
2188 chip->select_chip(mtd, -1);
2189
2190 ops->retlen = ops->len - (size_t) readlen;
2191 if (oob)
2192 ops->oobretlen = ops->ooblen - oobreadlen;
2193
2194 if (ret < 0)
2195 return ret;
2196
2197 if (ecc_fail)
2198 return -EBADMSG;
2199
2200 return max_bitflips;
2201}
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
2214 size_t *retlen, uint8_t *buf)
2215{
2216 struct mtd_oob_ops ops;
2217 int ret;
2218
2219 nand_get_device(mtd, FL_READING);
2220 memset(&ops, 0, sizeof(ops));
2221 ops.len = len;
2222 ops.datbuf = buf;
2223 ops.mode = MTD_OPS_PLACE_OOB;
2224 ret = nand_do_read_ops(mtd, from, &ops);
2225 *retlen = ops.retlen;
2226 nand_release_device(mtd);
2227 return ret;
2228}
2229
2230
2231
2232
2233
2234
2235
2236int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
2237{
2238 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
2239 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
2240 return 0;
2241}
2242EXPORT_SYMBOL(nand_read_oob_std);
2243
2244
2245
2246
2247
2248
2249
2250
2251int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2252 int page)
2253{
2254 int length = mtd->oobsize;
2255 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2256 int eccsize = chip->ecc.size;
2257 uint8_t *bufpoi = chip->oob_poi;
2258 int i, toread, sndrnd = 0, pos;
2259
2260 chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page);
2261 for (i = 0; i < chip->ecc.steps; i++) {
2262 if (sndrnd) {
2263 pos = eccsize + i * (eccsize + chunk);
2264 if (mtd->writesize > 512)
2265 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
2266 else
2267 chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
2268 } else
2269 sndrnd = 1;
2270 toread = min_t(int, length, chunk);
2271 chip->read_buf(mtd, bufpoi, toread);
2272 bufpoi += toread;
2273 length -= toread;
2274 }
2275 if (length > 0)
2276 chip->read_buf(mtd, bufpoi, length);
2277
2278 return 0;
2279}
2280EXPORT_SYMBOL(nand_read_oob_syndrome);
2281
2282
2283
2284
2285
2286
2287
2288int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page)
2289{
2290 int status = 0;
2291 const uint8_t *buf = chip->oob_poi;
2292 int length = mtd->oobsize;
2293
2294 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
2295 chip->write_buf(mtd, buf, length);
2296
2297 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2298
2299 status = chip->waitfunc(mtd, chip);
2300
2301 return status & NAND_STATUS_FAIL ? -EIO : 0;
2302}
2303EXPORT_SYMBOL(nand_write_oob_std);
2304
2305
2306
2307
2308
2309
2310
2311
2312int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
2313 int page)
2314{
2315 int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
2316 int eccsize = chip->ecc.size, length = mtd->oobsize;
2317 int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps;
2318 const uint8_t *bufpoi = chip->oob_poi;
2319
2320
2321
2322
2323
2324
2325 if (!chip->ecc.prepad && !chip->ecc.postpad) {
2326 pos = steps * (eccsize + chunk);
2327 steps = 0;
2328 } else
2329 pos = eccsize;
2330
2331 chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page);
2332 for (i = 0; i < steps; i++) {
2333 if (sndcmd) {
2334 if (mtd->writesize <= 512) {
2335 uint32_t fill = 0xFFFFFFFF;
2336
2337 len = eccsize;
2338 while (len > 0) {
2339 int num = min_t(int, len, 4);
2340 chip->write_buf(mtd, (uint8_t *)&fill,
2341 num);
2342 len -= num;
2343 }
2344 } else {
2345 pos = eccsize + i * (eccsize + chunk);
2346 chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1);
2347 }
2348 } else
2349 sndcmd = 1;
2350 len = min_t(int, length, chunk);
2351 chip->write_buf(mtd, bufpoi, len);
2352 bufpoi += len;
2353 length -= len;
2354 }
2355 if (length > 0)
2356 chip->write_buf(mtd, bufpoi, length);
2357
2358 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2359 status = chip->waitfunc(mtd, chip);
2360
2361 return status & NAND_STATUS_FAIL ? -EIO : 0;
2362}
2363EXPORT_SYMBOL(nand_write_oob_syndrome);
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
2374 struct mtd_oob_ops *ops)
2375{
2376 int page, realpage, chipnr;
2377 struct nand_chip *chip = mtd_to_nand(mtd);
2378 struct mtd_ecc_stats stats;
2379 int readlen = ops->ooblen;
2380 int len;
2381 uint8_t *buf = ops->oobbuf;
2382 int ret = 0;
2383
2384 pr_debug("%s: from = 0x%08Lx, len = %i\n",
2385 __func__, (unsigned long long)from, readlen);
2386
2387 stats = mtd->ecc_stats;
2388
2389 len = mtd_oobavail(mtd, ops);
2390
2391 if (unlikely(ops->ooboffs >= len)) {
2392 pr_debug("%s: attempt to start read outside oob\n",
2393 __func__);
2394 return -EINVAL;
2395 }
2396
2397
2398 if (unlikely(from >= mtd->size ||
2399 ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) -
2400 (from >> chip->page_shift)) * len)) {
2401 pr_debug("%s: attempt to read beyond end of device\n",
2402 __func__);
2403 return -EINVAL;
2404 }
2405
2406 chipnr = (int)(from >> chip->chip_shift);
2407 chip->select_chip(mtd, chipnr);
2408
2409
2410 realpage = (int)(from >> chip->page_shift);
2411 page = realpage & chip->pagemask;
2412
2413 while (1) {
2414 if (ops->mode == MTD_OPS_RAW)
2415 ret = chip->ecc.read_oob_raw(mtd, chip, page);
2416 else
2417 ret = chip->ecc.read_oob(mtd, chip, page);
2418
2419 if (ret < 0)
2420 break;
2421
2422 len = min(len, readlen);
2423 buf = nand_transfer_oob(mtd, buf, ops, len);
2424
2425 if (chip->options & NAND_NEED_READRDY) {
2426
2427 if (!chip->dev_ready)
2428 udelay(chip->chip_delay);
2429 else
2430 nand_wait_ready(mtd);
2431 }
2432
2433 readlen -= len;
2434 if (!readlen)
2435 break;
2436
2437
2438 realpage++;
2439
2440 page = realpage & chip->pagemask;
2441
2442 if (!page) {
2443 chipnr++;
2444 chip->select_chip(mtd, -1);
2445 chip->select_chip(mtd, chipnr);
2446 }
2447 }
2448 chip->select_chip(mtd, -1);
2449
2450 ops->oobretlen = ops->ooblen - readlen;
2451
2452 if (ret < 0)
2453 return ret;
2454
2455 if (mtd->ecc_stats.failed - stats.failed)
2456 return -EBADMSG;
2457
2458 return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
2459}
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469static int nand_read_oob(struct mtd_info *mtd, loff_t from,
2470 struct mtd_oob_ops *ops)
2471{
2472 int ret;
2473
2474 ops->retlen = 0;
2475
2476
2477 if (ops->datbuf && (from + ops->len) > mtd->size) {
2478 pr_debug("%s: attempt to read beyond end of device\n",
2479 __func__);
2480 return -EINVAL;
2481 }
2482
2483 if (ops->mode != MTD_OPS_PLACE_OOB &&
2484 ops->mode != MTD_OPS_AUTO_OOB &&
2485 ops->mode != MTD_OPS_RAW)
2486 return -ENOTSUPP;
2487
2488 nand_get_device(mtd, FL_READING);
2489
2490 if (!ops->datbuf)
2491 ret = nand_do_read_oob(mtd, from, ops);
2492 else
2493 ret = nand_do_read_ops(mtd, from, ops);
2494
2495 nand_release_device(mtd);
2496 return ret;
2497}
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
2511 const uint8_t *buf, int oob_required, int page)
2512{
2513 chip->write_buf(mtd, buf, mtd->writesize);
2514 if (oob_required)
2515 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2516
2517 return 0;
2518}
2519EXPORT_SYMBOL(nand_write_page_raw);
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531static int nand_write_page_raw_syndrome(struct mtd_info *mtd,
2532 struct nand_chip *chip,
2533 const uint8_t *buf, int oob_required,
2534 int page)
2535{
2536 int eccsize = chip->ecc.size;
2537 int eccbytes = chip->ecc.bytes;
2538 uint8_t *oob = chip->oob_poi;
2539 int steps, size;
2540
2541 for (steps = chip->ecc.steps; steps > 0; steps--) {
2542 chip->write_buf(mtd, buf, eccsize);
2543 buf += eccsize;
2544
2545 if (chip->ecc.prepad) {
2546 chip->write_buf(mtd, oob, chip->ecc.prepad);
2547 oob += chip->ecc.prepad;
2548 }
2549
2550 chip->write_buf(mtd, oob, eccbytes);
2551 oob += eccbytes;
2552
2553 if (chip->ecc.postpad) {
2554 chip->write_buf(mtd, oob, chip->ecc.postpad);
2555 oob += chip->ecc.postpad;
2556 }
2557 }
2558
2559 size = mtd->oobsize - (oob - chip->oob_poi);
2560 if (size)
2561 chip->write_buf(mtd, oob, size);
2562
2563 return 0;
2564}
2565
2566
2567
2568
2569
2570
2571
2572
2573static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
2574 const uint8_t *buf, int oob_required,
2575 int page)
2576{
2577 int i, eccsize = chip->ecc.size, ret;
2578 int eccbytes = chip->ecc.bytes;
2579 int eccsteps = chip->ecc.steps;
2580 uint8_t *ecc_calc = chip->buffers->ecccalc;
2581 const uint8_t *p = buf;
2582
2583
2584 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
2585 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2586
2587 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2588 chip->ecc.total);
2589 if (ret)
2590 return ret;
2591
2592 return chip->ecc.write_page_raw(mtd, chip, buf, 1, page);
2593}
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
2604 const uint8_t *buf, int oob_required,
2605 int page)
2606{
2607 int i, eccsize = chip->ecc.size, ret;
2608 int eccbytes = chip->ecc.bytes;
2609 int eccsteps = chip->ecc.steps;
2610 uint8_t *ecc_calc = chip->buffers->ecccalc;
2611 const uint8_t *p = buf;
2612
2613 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2614 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2615 chip->write_buf(mtd, p, eccsize);
2616 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
2617 }
2618
2619 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2620 chip->ecc.total);
2621 if (ret)
2622 return ret;
2623
2624 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2625
2626 return 0;
2627}
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640static int nand_write_subpage_hwecc(struct mtd_info *mtd,
2641 struct nand_chip *chip, uint32_t offset,
2642 uint32_t data_len, const uint8_t *buf,
2643 int oob_required, int page)
2644{
2645 uint8_t *oob_buf = chip->oob_poi;
2646 uint8_t *ecc_calc = chip->buffers->ecccalc;
2647 int ecc_size = chip->ecc.size;
2648 int ecc_bytes = chip->ecc.bytes;
2649 int ecc_steps = chip->ecc.steps;
2650 uint32_t start_step = offset / ecc_size;
2651 uint32_t end_step = (offset + data_len - 1) / ecc_size;
2652 int oob_bytes = mtd->oobsize / ecc_steps;
2653 int step, ret;
2654
2655 for (step = 0; step < ecc_steps; step++) {
2656
2657 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2658
2659
2660 chip->write_buf(mtd, buf, ecc_size);
2661
2662
2663 if ((step < start_step) || (step > end_step))
2664 memset(ecc_calc, 0xff, ecc_bytes);
2665 else
2666 chip->ecc.calculate(mtd, buf, ecc_calc);
2667
2668
2669
2670 if (!oob_required || (step < start_step) || (step > end_step))
2671 memset(oob_buf, 0xff, oob_bytes);
2672
2673 buf += ecc_size;
2674 ecc_calc += ecc_bytes;
2675 oob_buf += oob_bytes;
2676 }
2677
2678
2679
2680 ecc_calc = chip->buffers->ecccalc;
2681 ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
2682 chip->ecc.total);
2683 if (ret)
2684 return ret;
2685
2686
2687 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
2688
2689 return 0;
2690}
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704static int nand_write_page_syndrome(struct mtd_info *mtd,
2705 struct nand_chip *chip,
2706 const uint8_t *buf, int oob_required,
2707 int page)
2708{
2709 int i, eccsize = chip->ecc.size;
2710 int eccbytes = chip->ecc.bytes;
2711 int eccsteps = chip->ecc.steps;
2712 const uint8_t *p = buf;
2713 uint8_t *oob = chip->oob_poi;
2714
2715 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2716
2717 chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
2718 chip->write_buf(mtd, p, eccsize);
2719
2720 if (chip->ecc.prepad) {
2721 chip->write_buf(mtd, oob, chip->ecc.prepad);
2722 oob += chip->ecc.prepad;
2723 }
2724
2725 chip->ecc.calculate(mtd, p, oob);
2726 chip->write_buf(mtd, oob, eccbytes);
2727 oob += eccbytes;
2728
2729 if (chip->ecc.postpad) {
2730 chip->write_buf(mtd, oob, chip->ecc.postpad);
2731 oob += chip->ecc.postpad;
2732 }
2733 }
2734
2735
2736 i = mtd->oobsize - (oob - chip->oob_poi);
2737 if (i)
2738 chip->write_buf(mtd, oob, i);
2739
2740 return 0;
2741}
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
2755 uint32_t offset, int data_len, const uint8_t *buf,
2756 int oob_required, int page, int raw)
2757{
2758 int status, subpage;
2759
2760 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) &&
2761 chip->ecc.write_subpage)
2762 subpage = offset || (data_len < mtd->writesize);
2763 else
2764 subpage = 0;
2765
2766 if (nand_standard_page_accessors(&chip->ecc))
2767 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
2768
2769 if (unlikely(raw))
2770 status = chip->ecc.write_page_raw(mtd, chip, buf,
2771 oob_required, page);
2772 else if (subpage)
2773 status = chip->ecc.write_subpage(mtd, chip, offset, data_len,
2774 buf, oob_required, page);
2775 else
2776 status = chip->ecc.write_page(mtd, chip, buf, oob_required,
2777 page);
2778
2779 if (status < 0)
2780 return status;
2781
2782 if (nand_standard_page_accessors(&chip->ecc)) {
2783 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
2784
2785 status = chip->waitfunc(mtd, chip);
2786 if (status & NAND_STATUS_FAIL)
2787 return -EIO;
2788 }
2789
2790 return 0;
2791}
2792
2793
2794
2795
2796
2797
2798
2799
2800static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len,
2801 struct mtd_oob_ops *ops)
2802{
2803 struct nand_chip *chip = mtd_to_nand(mtd);
2804 int ret;
2805
2806
2807
2808
2809
2810 memset(chip->oob_poi, 0xff, mtd->oobsize);
2811
2812 switch (ops->mode) {
2813
2814 case MTD_OPS_PLACE_OOB:
2815 case MTD_OPS_RAW:
2816 memcpy(chip->oob_poi + ops->ooboffs, oob, len);
2817 return oob + len;
2818
2819 case MTD_OPS_AUTO_OOB:
2820 ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi,
2821 ops->ooboffs, len);
2822 BUG_ON(ret);
2823 return oob + len;
2824
2825 default:
2826 BUG();
2827 }
2828 return NULL;
2829}
2830
2831#define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
2842 struct mtd_oob_ops *ops)
2843{
2844 int chipnr, realpage, page, blockmask, column;
2845 struct nand_chip *chip = mtd_to_nand(mtd);
2846 uint32_t writelen = ops->len;
2847
2848 uint32_t oobwritelen = ops->ooblen;
2849 uint32_t oobmaxlen = mtd_oobavail(mtd, ops);
2850
2851 uint8_t *oob = ops->oobbuf;
2852 uint8_t *buf = ops->datbuf;
2853 int ret;
2854 int oob_required = oob ? 1 : 0;
2855
2856 ops->retlen = 0;
2857 if (!writelen)
2858 return 0;
2859
2860
2861 if (NOTALIGNED(to) || NOTALIGNED(ops->len)) {
2862 pr_notice("%s: attempt to write non page aligned data\n",
2863 __func__);
2864 return -EINVAL;
2865 }
2866
2867 column = to & (mtd->writesize - 1);
2868
2869 chipnr = (int)(to >> chip->chip_shift);
2870 chip->select_chip(mtd, chipnr);
2871
2872
2873 if (nand_check_wp(mtd)) {
2874 ret = -EIO;
2875 goto err_out;
2876 }
2877
2878 realpage = (int)(to >> chip->page_shift);
2879 page = realpage & chip->pagemask;
2880 blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
2881
2882
2883 if (to <= ((loff_t)chip->pagebuf << chip->page_shift) &&
2884 ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len))
2885 chip->pagebuf = -1;
2886
2887
2888 if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) {
2889 ret = -EINVAL;
2890 goto err_out;
2891 }
2892
2893 while (1) {
2894 int bytes = mtd->writesize;
2895 uint8_t *wbuf = buf;
2896 int use_bufpoi;
2897 int part_pagewr = (column || writelen < mtd->writesize);
2898
2899 if (part_pagewr)
2900 use_bufpoi = 1;
2901 else if (chip->options & NAND_USE_BOUNCE_BUFFER)
2902 use_bufpoi = !virt_addr_valid(buf) ||
2903 !IS_ALIGNED((unsigned long)buf,
2904 chip->buf_align);
2905 else
2906 use_bufpoi = 0;
2907
2908
2909 if (use_bufpoi) {
2910 pr_debug("%s: using write bounce buffer for buf@%p\n",
2911 __func__, buf);
2912 if (part_pagewr)
2913 bytes = min_t(int, bytes - column, writelen);
2914 chip->pagebuf = -1;
2915 memset(chip->buffers->databuf, 0xff, mtd->writesize);
2916 memcpy(&chip->buffers->databuf[column], buf, bytes);
2917 wbuf = chip->buffers->databuf;
2918 }
2919
2920 if (unlikely(oob)) {
2921 size_t len = min(oobwritelen, oobmaxlen);
2922 oob = nand_fill_oob(mtd, oob, len, ops);
2923 oobwritelen -= len;
2924 } else {
2925
2926 memset(chip->oob_poi, 0xff, mtd->oobsize);
2927 }
2928
2929 ret = nand_write_page(mtd, chip, column, bytes, wbuf,
2930 oob_required, page,
2931 (ops->mode == MTD_OPS_RAW));
2932 if (ret)
2933 break;
2934
2935 writelen -= bytes;
2936 if (!writelen)
2937 break;
2938
2939 column = 0;
2940 buf += bytes;
2941 realpage++;
2942
2943 page = realpage & chip->pagemask;
2944
2945 if (!page) {
2946 chipnr++;
2947 chip->select_chip(mtd, -1);
2948 chip->select_chip(mtd, chipnr);
2949 }
2950 }
2951
2952 ops->retlen = ops->len - writelen;
2953 if (unlikely(oob))
2954 ops->oobretlen = ops->ooblen;
2955
2956err_out:
2957 chip->select_chip(mtd, -1);
2958 return ret;
2959}
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
2973 size_t *retlen, const uint8_t *buf)
2974{
2975 struct nand_chip *chip = mtd_to_nand(mtd);
2976 struct mtd_oob_ops ops;
2977 int ret;
2978
2979
2980 panic_nand_wait(mtd, chip, 400);
2981
2982
2983 panic_nand_get_device(chip, mtd, FL_WRITING);
2984
2985 memset(&ops, 0, sizeof(ops));
2986 ops.len = len;
2987 ops.datbuf = (uint8_t *)buf;
2988 ops.mode = MTD_OPS_PLACE_OOB;
2989
2990 ret = nand_do_write_ops(mtd, to, &ops);
2991
2992 *retlen = ops.retlen;
2993 return ret;
2994}
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
3007 size_t *retlen, const uint8_t *buf)
3008{
3009 struct mtd_oob_ops ops;
3010 int ret;
3011
3012 nand_get_device(mtd, FL_WRITING);
3013 memset(&ops, 0, sizeof(ops));
3014 ops.len = len;
3015 ops.datbuf = (uint8_t *)buf;
3016 ops.mode = MTD_OPS_PLACE_OOB;
3017 ret = nand_do_write_ops(mtd, to, &ops);
3018 *retlen = ops.retlen;
3019 nand_release_device(mtd);
3020 return ret;
3021}
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
3032 struct mtd_oob_ops *ops)
3033{
3034 int chipnr, page, status, len;
3035 struct nand_chip *chip = mtd_to_nand(mtd);
3036
3037 pr_debug("%s: to = 0x%08x, len = %i\n",
3038 __func__, (unsigned int)to, (int)ops->ooblen);
3039
3040 len = mtd_oobavail(mtd, ops);
3041
3042
3043 if ((ops->ooboffs + ops->ooblen) > len) {
3044 pr_debug("%s: attempt to write past end of page\n",
3045 __func__);
3046 return -EINVAL;
3047 }
3048
3049 if (unlikely(ops->ooboffs >= len)) {
3050 pr_debug("%s: attempt to start write outside oob\n",
3051 __func__);
3052 return -EINVAL;
3053 }
3054
3055
3056 if (unlikely(to >= mtd->size ||
3057 ops->ooboffs + ops->ooblen >
3058 ((mtd->size >> chip->page_shift) -
3059 (to >> chip->page_shift)) * len)) {
3060 pr_debug("%s: attempt to write beyond end of device\n",
3061 __func__);
3062 return -EINVAL;
3063 }
3064
3065 chipnr = (int)(to >> chip->chip_shift);
3066
3067
3068
3069
3070
3071
3072
3073 nand_reset(chip, chipnr);
3074
3075 chip->select_chip(mtd, chipnr);
3076
3077
3078 page = (int)(to >> chip->page_shift);
3079
3080
3081 if (nand_check_wp(mtd)) {
3082 chip->select_chip(mtd, -1);
3083 return -EROFS;
3084 }
3085
3086
3087 if (page == chip->pagebuf)
3088 chip->pagebuf = -1;
3089
3090 nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops);
3091
3092 if (ops->mode == MTD_OPS_RAW)
3093 status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask);
3094 else
3095 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
3096
3097 chip->select_chip(mtd, -1);
3098
3099 if (status)
3100 return status;
3101
3102 ops->oobretlen = ops->ooblen;
3103
3104 return 0;
3105}
3106
3107
3108
3109
3110
3111
3112
3113static int nand_write_oob(struct mtd_info *mtd, loff_t to,
3114 struct mtd_oob_ops *ops)
3115{
3116 int ret = -ENOTSUPP;
3117
3118 ops->retlen = 0;
3119
3120
3121 if (ops->datbuf && (to + ops->len) > mtd->size) {
3122 pr_debug("%s: attempt to write beyond end of device\n",
3123 __func__);
3124 return -EINVAL;
3125 }
3126
3127 nand_get_device(mtd, FL_WRITING);
3128
3129 switch (ops->mode) {
3130 case MTD_OPS_PLACE_OOB:
3131 case MTD_OPS_AUTO_OOB:
3132 case MTD_OPS_RAW:
3133 break;
3134
3135 default:
3136 goto out;
3137 }
3138
3139 if (!ops->datbuf)
3140 ret = nand_do_write_oob(mtd, to, ops);
3141 else
3142 ret = nand_do_write_ops(mtd, to, ops);
3143
3144out:
3145 nand_release_device(mtd);
3146 return ret;
3147}
3148
3149
3150
3151
3152
3153
3154
3155
3156static int single_erase(struct mtd_info *mtd, int page)
3157{
3158 struct nand_chip *chip = mtd_to_nand(mtd);
3159
3160 chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
3161 chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
3162
3163 return chip->waitfunc(mtd, chip);
3164}
3165
3166
3167
3168
3169
3170
3171
3172
3173static int nand_erase(struct mtd_info *mtd, struct erase_info *instr)
3174{
3175 return nand_erase_nand(mtd, instr, 0);
3176}
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
3187 int allowbbt)
3188{
3189 int page, status, pages_per_block, ret, chipnr;
3190 struct nand_chip *chip = mtd_to_nand(mtd);
3191 loff_t len;
3192
3193 pr_debug("%s: start = 0x%012llx, len = %llu\n",
3194 __func__, (unsigned long long)instr->addr,
3195 (unsigned long long)instr->len);
3196
3197 if (check_offs_len(mtd, instr->addr, instr->len))
3198 return -EINVAL;
3199
3200
3201 nand_get_device(mtd, FL_ERASING);
3202
3203
3204 page = (int)(instr->addr >> chip->page_shift);
3205 chipnr = (int)(instr->addr >> chip->chip_shift);
3206
3207
3208 pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift);
3209
3210
3211 chip->select_chip(mtd, chipnr);
3212
3213
3214 if (nand_check_wp(mtd)) {
3215 pr_debug("%s: device is write protected!\n",
3216 __func__);
3217 instr->state = MTD_ERASE_FAILED;
3218 goto erase_exit;
3219 }
3220
3221
3222 len = instr->len;
3223
3224 instr->state = MTD_ERASING;
3225
3226 while (len) {
3227
3228 if (nand_block_checkbad(mtd, ((loff_t) page) <<
3229 chip->page_shift, allowbbt)) {
3230 pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
3231 __func__, page);
3232 instr->state = MTD_ERASE_FAILED;
3233 goto erase_exit;
3234 }
3235
3236
3237
3238
3239
3240 if (page <= chip->pagebuf && chip->pagebuf <
3241 (page + pages_per_block))
3242 chip->pagebuf = -1;
3243
3244 status = chip->erase(mtd, page & chip->pagemask);
3245
3246
3247 if (status & NAND_STATUS_FAIL) {
3248 pr_debug("%s: failed erase, page 0x%08x\n",
3249 __func__, page);
3250 instr->state = MTD_ERASE_FAILED;
3251 instr->fail_addr =
3252 ((loff_t)page << chip->page_shift);
3253 goto erase_exit;
3254 }
3255
3256
3257 len -= (1ULL << chip->phys_erase_shift);
3258 page += pages_per_block;
3259
3260
3261 if (len && !(page & chip->pagemask)) {
3262 chipnr++;
3263 chip->select_chip(mtd, -1);
3264 chip->select_chip(mtd, chipnr);
3265 }
3266 }
3267 instr->state = MTD_ERASE_DONE;
3268
3269erase_exit:
3270
3271 ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO;
3272
3273
3274 chip->select_chip(mtd, -1);
3275 nand_release_device(mtd);
3276
3277
3278 if (!ret)
3279 mtd_erase_callback(instr);
3280
3281
3282 return ret;
3283}
3284
3285
3286
3287
3288
3289
3290
3291static void nand_sync(struct mtd_info *mtd)
3292{
3293 pr_debug("%s: called\n", __func__);
3294
3295
3296 nand_get_device(mtd, FL_SYNCING);
3297
3298 nand_release_device(mtd);
3299}
3300
3301
3302
3303
3304
3305
3306static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
3307{
3308 struct nand_chip *chip = mtd_to_nand(mtd);
3309 int chipnr = (int)(offs >> chip->chip_shift);
3310 int ret;
3311
3312
3313 nand_get_device(mtd, FL_READING);
3314 chip->select_chip(mtd, chipnr);
3315
3316 ret = nand_block_checkbad(mtd, offs, 0);
3317
3318 chip->select_chip(mtd, -1);
3319 nand_release_device(mtd);
3320
3321 return ret;
3322}
3323
3324
3325
3326
3327
3328
3329static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
3330{
3331 int ret;
3332
3333 ret = nand_block_isbad(mtd, ofs);
3334 if (ret) {
3335
3336 if (ret > 0)
3337 return 0;
3338 return ret;
3339 }
3340
3341 return nand_block_markbad_lowlevel(mtd, ofs);
3342}
3343
3344
3345
3346
3347
3348
3349
3350static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
3351{
3352 struct nand_chip *chip = mtd_to_nand(mtd);
3353 u32 part_start_block;
3354 u32 part_end_block;
3355 u32 part_start_die;
3356 u32 part_end_die;
3357
3358
3359
3360
3361
3362 if (!chip->max_bb_per_die || !chip->blocks_per_die)
3363 return -ENOTSUPP;
3364
3365
3366 part_start_block = mtd_div_by_eb(ofs, mtd);
3367 part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1;
3368
3369
3370 part_start_die = part_start_block / chip->blocks_per_die;
3371 part_end_die = part_end_block / chip->blocks_per_die;
3372
3373
3374
3375
3376
3377 return chip->max_bb_per_die * (part_end_die - part_start_die + 1);
3378}
3379
3380
3381
3382
3383
3384
3385
3386
3387static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip,
3388 int addr, uint8_t *subfeature_param)
3389{
3390 int status;
3391 int i;
3392
3393 if (!chip->onfi_version ||
3394 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3395 & ONFI_OPT_CMD_SET_GET_FEATURES))
3396 return -EINVAL;
3397
3398 chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1);
3399 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3400 chip->write_byte(mtd, subfeature_param[i]);
3401
3402 status = chip->waitfunc(mtd, chip);
3403 if (status & NAND_STATUS_FAIL)
3404 return -EIO;
3405 return 0;
3406}
3407
3408
3409
3410
3411
3412
3413
3414
3415static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip,
3416 int addr, uint8_t *subfeature_param)
3417{
3418 int i;
3419
3420 if (!chip->onfi_version ||
3421 !(le16_to_cpu(chip->onfi_params.opt_cmd)
3422 & ONFI_OPT_CMD_SET_GET_FEATURES))
3423 return -EINVAL;
3424
3425 chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1);
3426 for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
3427 *subfeature_param++ = chip->read_byte(mtd);
3428 return 0;
3429}
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442int nand_onfi_get_set_features_notsupp(struct mtd_info *mtd,
3443 struct nand_chip *chip, int addr,
3444 u8 *subfeature_param)
3445{
3446 return -ENOTSUPP;
3447}
3448EXPORT_SYMBOL(nand_onfi_get_set_features_notsupp);
3449
3450
3451
3452
3453
3454static int nand_suspend(struct mtd_info *mtd)
3455{
3456 return nand_get_device(mtd, FL_PM_SUSPENDED);
3457}
3458
3459
3460
3461
3462
3463static void nand_resume(struct mtd_info *mtd)
3464{
3465 struct nand_chip *chip = mtd_to_nand(mtd);
3466
3467 if (chip->state == FL_PM_SUSPENDED)
3468 nand_release_device(mtd);
3469 else
3470 pr_err("%s called for a chip which is not in suspended state\n",
3471 __func__);
3472}
3473
3474
3475
3476
3477
3478
3479static void nand_shutdown(struct mtd_info *mtd)
3480{
3481 nand_get_device(mtd, FL_PM_SUSPENDED);
3482}
3483
3484
3485static void nand_set_defaults(struct nand_chip *chip)
3486{
3487 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
3488
3489
3490 if (!chip->chip_delay)
3491 chip->chip_delay = 20;
3492
3493
3494 if (chip->cmdfunc == NULL)
3495 chip->cmdfunc = nand_command;
3496
3497
3498 if (chip->waitfunc == NULL)
3499 chip->waitfunc = nand_wait;
3500
3501 if (!chip->select_chip)
3502 chip->select_chip = nand_select_chip;
3503
3504
3505 if (!chip->onfi_set_features)
3506 chip->onfi_set_features = nand_onfi_set_features;
3507 if (!chip->onfi_get_features)
3508 chip->onfi_get_features = nand_onfi_get_features;
3509
3510
3511 if (!chip->read_byte || chip->read_byte == nand_read_byte)
3512 chip->read_byte = busw ? nand_read_byte16 : nand_read_byte;
3513 if (!chip->read_word)
3514 chip->read_word = nand_read_word;
3515 if (!chip->block_bad)
3516 chip->block_bad = nand_block_bad;
3517 if (!chip->block_markbad)
3518 chip->block_markbad = nand_default_block_markbad;
3519 if (!chip->write_buf || chip->write_buf == nand_write_buf)
3520 chip->write_buf = busw ? nand_write_buf16 : nand_write_buf;
3521 if (!chip->write_byte || chip->write_byte == nand_write_byte)
3522 chip->write_byte = busw ? nand_write_byte16 : nand_write_byte;
3523 if (!chip->read_buf || chip->read_buf == nand_read_buf)
3524 chip->read_buf = busw ? nand_read_buf16 : nand_read_buf;
3525 if (!chip->scan_bbt)
3526 chip->scan_bbt = nand_default_bbt;
3527
3528 if (!chip->controller) {
3529 chip->controller = &chip->hwcontrol;
3530 nand_hw_control_init(chip->controller);
3531 }
3532
3533 if (!chip->buf_align)
3534 chip->buf_align = 1;
3535}
3536
3537
3538static void sanitize_string(uint8_t *s, size_t len)
3539{
3540 ssize_t i;
3541
3542
3543 s[len - 1] = 0;
3544
3545
3546 for (i = 0; i < len - 1; i++) {
3547 if (s[i] < ' ' || s[i] > 127)
3548 s[i] = '?';
3549 }
3550
3551
3552 strim(s);
3553}
3554
3555static u16 onfi_crc16(u16 crc, u8 const *p, size_t len)
3556{
3557 int i;
3558 while (len--) {
3559 crc ^= *p++ << 8;
3560 for (i = 0; i < 8; i++)
3561 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
3562 }
3563
3564 return crc;
3565}
3566
3567
3568static int nand_flash_detect_ext_param_page(struct nand_chip *chip,
3569 struct nand_onfi_params *p)
3570{
3571 struct mtd_info *mtd = nand_to_mtd(chip);
3572 struct onfi_ext_param_page *ep;
3573 struct onfi_ext_section *s;
3574 struct onfi_ext_ecc_info *ecc;
3575 uint8_t *cursor;
3576 int ret = -EINVAL;
3577 int len;
3578 int i;
3579
3580 len = le16_to_cpu(p->ext_param_page_length) * 16;
3581 ep = kmalloc(len, GFP_KERNEL);
3582 if (!ep)
3583 return -ENOMEM;
3584
3585
3586 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3587
3588
3589 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
3590 sizeof(*p) * p->num_of_param_pages , -1);
3591
3592
3593 chip->read_buf(mtd, (uint8_t *)ep, len);
3594 if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2)
3595 != le16_to_cpu(ep->crc))) {
3596 pr_debug("fail in the CRC.\n");
3597 goto ext_out;
3598 }
3599
3600
3601
3602
3603
3604 if (strncmp(ep->sig, "EPPS", 4)) {
3605 pr_debug("The signature is invalid.\n");
3606 goto ext_out;
3607 }
3608
3609
3610 cursor = (uint8_t *)(ep + 1);
3611 for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) {
3612 s = ep->sections + i;
3613 if (s->type == ONFI_SECTION_TYPE_2)
3614 break;
3615 cursor += s->length * 16;
3616 }
3617 if (i == ONFI_EXT_SECTION_MAX) {
3618 pr_debug("We can not find the ECC section.\n");
3619 goto ext_out;
3620 }
3621
3622
3623 ecc = (struct onfi_ext_ecc_info *)cursor;
3624
3625 if (!ecc->codeword_size) {
3626 pr_debug("Invalid codeword size\n");
3627 goto ext_out;
3628 }
3629
3630 chip->ecc_strength_ds = ecc->ecc_bits;
3631 chip->ecc_step_ds = 1 << ecc->codeword_size;
3632 ret = 0;
3633
3634ext_out:
3635 kfree(ep);
3636 return ret;
3637}
3638
3639
3640
3641
3642static int nand_flash_detect_onfi(struct nand_chip *chip)
3643{
3644 struct mtd_info *mtd = nand_to_mtd(chip);
3645 struct nand_onfi_params *p = &chip->onfi_params;
3646 int i, j;
3647 int val;
3648
3649
3650 chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1);
3651 if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' ||
3652 chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
3653 return 0;
3654
3655 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
3656 for (i = 0; i < 3; i++) {
3657 for (j = 0; j < sizeof(*p); j++)
3658 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3659 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
3660 le16_to_cpu(p->crc)) {
3661 break;
3662 }
3663 }
3664
3665 if (i == 3) {
3666 pr_err("Could not find valid ONFI parameter page; aborting\n");
3667 return 0;
3668 }
3669
3670
3671 val = le16_to_cpu(p->revision);
3672 if (val & (1 << 5))
3673 chip->onfi_version = 23;
3674 else if (val & (1 << 4))
3675 chip->onfi_version = 22;
3676 else if (val & (1 << 3))
3677 chip->onfi_version = 21;
3678 else if (val & (1 << 2))
3679 chip->onfi_version = 20;
3680 else if (val & (1 << 1))
3681 chip->onfi_version = 10;
3682
3683 if (!chip->onfi_version) {
3684 pr_info("unsupported ONFI version: %d\n", val);
3685 return 0;
3686 }
3687
3688 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3689 sanitize_string(p->model, sizeof(p->model));
3690 if (!mtd->name)
3691 mtd->name = p->model;
3692
3693 mtd->writesize = le32_to_cpu(p->byte_per_page);
3694
3695
3696
3697
3698
3699
3700 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3701 mtd->erasesize *= mtd->writesize;
3702
3703 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3704
3705
3706 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3707 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3708 chip->bits_per_cell = p->bits_per_cell;
3709
3710 chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun);
3711 chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun);
3712
3713 if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS)
3714 chip->options |= NAND_BUSWIDTH_16;
3715
3716 if (p->ecc_bits != 0xff) {
3717 chip->ecc_strength_ds = p->ecc_bits;
3718 chip->ecc_step_ds = 512;
3719 } else if (chip->onfi_version >= 21 &&
3720 (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) {
3721
3722
3723
3724
3725
3726
3727
3728 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
3729 chip->cmdfunc = nand_command_lp;
3730
3731
3732 if (nand_flash_detect_ext_param_page(chip, p))
3733 pr_warn("Failed to detect ONFI extended param page\n");
3734 } else {
3735 pr_warn("Could not retrieve ONFI ECC requirements\n");
3736 }
3737
3738 return 1;
3739}
3740
3741
3742
3743
3744static int nand_flash_detect_jedec(struct nand_chip *chip)
3745{
3746 struct mtd_info *mtd = nand_to_mtd(chip);
3747 struct nand_jedec_params *p = &chip->jedec_params;
3748 struct jedec_ecc_info *ecc;
3749 int val;
3750 int i, j;
3751
3752
3753 chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1);
3754 if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' ||
3755 chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' ||
3756 chip->read_byte(mtd) != 'C')
3757 return 0;
3758
3759 chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1);
3760 for (i = 0; i < 3; i++) {
3761 for (j = 0; j < sizeof(*p); j++)
3762 ((uint8_t *)p)[j] = chip->read_byte(mtd);
3763
3764 if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) ==
3765 le16_to_cpu(p->crc))
3766 break;
3767 }
3768
3769 if (i == 3) {
3770 pr_err("Could not find valid JEDEC parameter page; aborting\n");
3771 return 0;
3772 }
3773
3774
3775 val = le16_to_cpu(p->revision);
3776 if (val & (1 << 2))
3777 chip->jedec_version = 10;
3778 else if (val & (1 << 1))
3779 chip->jedec_version = 1;
3780
3781 if (!chip->jedec_version) {
3782 pr_info("unsupported JEDEC version: %d\n", val);
3783 return 0;
3784 }
3785
3786 sanitize_string(p->manufacturer, sizeof(p->manufacturer));
3787 sanitize_string(p->model, sizeof(p->model));
3788 if (!mtd->name)
3789 mtd->name = p->model;
3790
3791 mtd->writesize = le32_to_cpu(p->byte_per_page);
3792
3793
3794 mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
3795 mtd->erasesize *= mtd->writesize;
3796
3797 mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
3798
3799
3800 chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);
3801 chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
3802 chip->bits_per_cell = p->bits_per_cell;
3803
3804 if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS)
3805 chip->options |= NAND_BUSWIDTH_16;
3806
3807
3808 ecc = &p->ecc_info[0];
3809
3810 if (ecc->codeword_size >= 9) {
3811 chip->ecc_strength_ds = ecc->ecc_bits;
3812 chip->ecc_step_ds = 1 << ecc->codeword_size;
3813 } else {
3814 pr_warn("Invalid codeword size\n");
3815 }
3816
3817 return 1;
3818}
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831static int nand_id_has_period(u8 *id_data, int arrlen, int period)
3832{
3833 int i, j;
3834 for (i = 0; i < period; i++)
3835 for (j = i + period; j < arrlen; j += period)
3836 if (id_data[i] != id_data[j])
3837 return 0;
3838 return 1;
3839}
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849static int nand_id_len(u8 *id_data, int arrlen)
3850{
3851 int last_nonzero, period;
3852
3853
3854 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
3855 if (id_data[last_nonzero])
3856 break;
3857
3858
3859 if (last_nonzero < 0)
3860 return 0;
3861
3862
3863 for (period = 1; period < arrlen; period++)
3864 if (nand_id_has_period(id_data, arrlen, period))
3865 break;
3866
3867
3868 if (period < arrlen)
3869 return period;
3870
3871
3872 if (last_nonzero < arrlen - 1)
3873 return last_nonzero + 1;
3874
3875
3876 return arrlen;
3877}
3878
3879
3880static int nand_get_bits_per_cell(u8 cellinfo)
3881{
3882 int bits;
3883
3884 bits = cellinfo & NAND_CI_CELLTYPE_MSK;
3885 bits >>= NAND_CI_CELLTYPE_SHIFT;
3886 return bits + 1;
3887}
3888
3889
3890
3891
3892
3893
3894void nand_decode_ext_id(struct nand_chip *chip)
3895{
3896 struct mtd_info *mtd = nand_to_mtd(chip);
3897 int extid;
3898 u8 *id_data = chip->id.data;
3899
3900 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
3901
3902 extid = id_data[3];
3903
3904
3905 mtd->writesize = 1024 << (extid & 0x03);
3906 extid >>= 2;
3907
3908 mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9);
3909 extid >>= 2;
3910
3911 mtd->erasesize = (64 * 1024) << (extid & 0x03);
3912 extid >>= 2;
3913
3914 if (extid & 0x1)
3915 chip->options |= NAND_BUSWIDTH_16;
3916}
3917EXPORT_SYMBOL_GPL(nand_decode_ext_id);
3918
3919
3920
3921
3922
3923
3924static void nand_decode_id(struct nand_chip *chip, struct nand_flash_dev *type)
3925{
3926 struct mtd_info *mtd = nand_to_mtd(chip);
3927
3928 mtd->erasesize = type->erasesize;
3929 mtd->writesize = type->pagesize;
3930 mtd->oobsize = mtd->writesize / 32;
3931
3932
3933 chip->bits_per_cell = 1;
3934}
3935
3936
3937
3938
3939
3940
3941static void nand_decode_bbm_options(struct nand_chip *chip)
3942{
3943 struct mtd_info *mtd = nand_to_mtd(chip);
3944
3945
3946 if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16))
3947 chip->badblockpos = NAND_LARGE_BADBLOCK_POS;
3948 else
3949 chip->badblockpos = NAND_SMALL_BADBLOCK_POS;
3950}
3951
3952static inline bool is_full_id_nand(struct nand_flash_dev *type)
3953{
3954 return type->id_len;
3955}
3956
3957static bool find_full_id_nand(struct nand_chip *chip,
3958 struct nand_flash_dev *type)
3959{
3960 struct mtd_info *mtd = nand_to_mtd(chip);
3961 u8 *id_data = chip->id.data;
3962
3963 if (!strncmp(type->id, id_data, type->id_len)) {
3964 mtd->writesize = type->pagesize;
3965 mtd->erasesize = type->erasesize;
3966 mtd->oobsize = type->oobsize;
3967
3968 chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]);
3969 chip->chipsize = (uint64_t)type->chipsize << 20;
3970 chip->options |= type->options;
3971 chip->ecc_strength_ds = NAND_ECC_STRENGTH(type);
3972 chip->ecc_step_ds = NAND_ECC_STEP(type);
3973 chip->onfi_timing_mode_default =
3974 type->onfi_timing_mode_default;
3975
3976 if (!mtd->name)
3977 mtd->name = type->name;
3978
3979 return true;
3980 }
3981 return false;
3982}
3983
3984
3985
3986
3987
3988
3989static void nand_manufacturer_detect(struct nand_chip *chip)
3990{
3991
3992
3993
3994
3995 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
3996 chip->manufacturer.desc->ops->detect)
3997 chip->manufacturer.desc->ops->detect(chip);
3998 else
3999 nand_decode_ext_id(chip);
4000}
4001
4002
4003
4004
4005
4006
4007
4008static int nand_manufacturer_init(struct nand_chip *chip)
4009{
4010 if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
4011 !chip->manufacturer.desc->ops->init)
4012 return 0;
4013
4014 return chip->manufacturer.desc->ops->init(chip);
4015}
4016
4017
4018
4019
4020
4021
4022
4023static void nand_manufacturer_cleanup(struct nand_chip *chip)
4024{
4025
4026 if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
4027 chip->manufacturer.desc->ops->cleanup)
4028 chip->manufacturer.desc->ops->cleanup(chip);
4029}
4030
4031
4032
4033
4034static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
4035{
4036 const struct nand_manufacturer *manufacturer;
4037 struct mtd_info *mtd = nand_to_mtd(chip);
4038 int busw;
4039 int i, ret;
4040 u8 *id_data = chip->id.data;
4041 u8 maf_id, dev_id;
4042
4043
4044
4045
4046
4047 nand_reset(chip, 0);
4048
4049
4050 chip->select_chip(mtd, 0);
4051
4052
4053 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
4054
4055
4056 maf_id = chip->read_byte(mtd);
4057 dev_id = chip->read_byte(mtd);
4058
4059
4060
4061
4062
4063
4064
4065
4066 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
4067
4068
4069 for (i = 0; i < 8; i++)
4070 id_data[i] = chip->read_byte(mtd);
4071
4072 if (id_data[0] != maf_id || id_data[1] != dev_id) {
4073 pr_info("second ID read did not match %02x,%02x against %02x,%02x\n",
4074 maf_id, dev_id, id_data[0], id_data[1]);
4075 return -ENODEV;
4076 }
4077
4078 chip->id.len = nand_id_len(id_data, 8);
4079
4080
4081 manufacturer = nand_get_manufacturer(maf_id);
4082 chip->manufacturer.desc = manufacturer;
4083
4084 if (!type)
4085 type = nand_flash_ids;
4086
4087
4088
4089
4090
4091
4092
4093
4094 busw = chip->options & NAND_BUSWIDTH_16;
4095
4096
4097
4098
4099
4100 chip->options &= ~NAND_BUSWIDTH_16;
4101
4102 for (; type->name != NULL; type++) {
4103 if (is_full_id_nand(type)) {
4104 if (find_full_id_nand(chip, type))
4105 goto ident_done;
4106 } else if (dev_id == type->dev_id) {
4107 break;
4108 }
4109 }
4110
4111 chip->onfi_version = 0;
4112 if (!type->name || !type->pagesize) {
4113
4114 if (nand_flash_detect_onfi(chip))
4115 goto ident_done;
4116
4117
4118 if (nand_flash_detect_jedec(chip))
4119 goto ident_done;
4120 }
4121
4122 if (!type->name)
4123 return -ENODEV;
4124
4125 if (!mtd->name)
4126 mtd->name = type->name;
4127
4128 chip->chipsize = (uint64_t)type->chipsize << 20;
4129
4130 if (!type->pagesize)
4131 nand_manufacturer_detect(chip);
4132 else
4133 nand_decode_id(chip, type);
4134
4135
4136 chip->options |= type->options;
4137
4138ident_done:
4139
4140 if (chip->options & NAND_BUSWIDTH_AUTO) {
4141 WARN_ON(busw & NAND_BUSWIDTH_16);
4142 nand_set_defaults(chip);
4143 } else if (busw != (chip->options & NAND_BUSWIDTH_16)) {
4144
4145
4146
4147
4148 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4149 maf_id, dev_id);
4150 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4151 mtd->name);
4152 pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
4153 (chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
4154 return -EINVAL;
4155 }
4156
4157 nand_decode_bbm_options(chip);
4158
4159
4160 chip->page_shift = ffs(mtd->writesize) - 1;
4161
4162 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
4163
4164 chip->bbt_erase_shift = chip->phys_erase_shift =
4165 ffs(mtd->erasesize) - 1;
4166 if (chip->chipsize & 0xffffffff)
4167 chip->chip_shift = ffs((unsigned)chip->chipsize) - 1;
4168 else {
4169 chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32));
4170 chip->chip_shift += 32 - 1;
4171 }
4172
4173 chip->badblockbits = 8;
4174 chip->erase = single_erase;
4175
4176
4177 if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
4178 chip->cmdfunc = nand_command_lp;
4179
4180 ret = nand_manufacturer_init(chip);
4181 if (ret)
4182 return ret;
4183
4184 pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
4185 maf_id, dev_id);
4186
4187 if (chip->onfi_version)
4188 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4189 chip->onfi_params.model);
4190 else if (chip->jedec_version)
4191 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4192 chip->jedec_params.model);
4193 else
4194 pr_info("%s %s\n", nand_manufacturer_name(manufacturer),
4195 type->name);
4196
4197 pr_info("%d MiB, %s, erase size: %d KiB, page size: %d, OOB size: %d\n",
4198 (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
4199 mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
4200 return 0;
4201}
4202
4203static const char * const nand_ecc_modes[] = {
4204 [NAND_ECC_NONE] = "none",
4205 [NAND_ECC_SOFT] = "soft",
4206 [NAND_ECC_HW] = "hw",
4207 [NAND_ECC_HW_SYNDROME] = "hw_syndrome",
4208 [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first",
4209 [NAND_ECC_ON_DIE] = "on-die",
4210};
4211
4212static int of_get_nand_ecc_mode(struct device_node *np)
4213{
4214 const char *pm;
4215 int err, i;
4216
4217 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4218 if (err < 0)
4219 return err;
4220
4221 for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++)
4222 if (!strcasecmp(pm, nand_ecc_modes[i]))
4223 return i;
4224
4225
4226
4227
4228
4229
4230 if (!strcasecmp(pm, "soft_bch"))
4231 return NAND_ECC_SOFT;
4232
4233 return -ENODEV;
4234}
4235
4236static const char * const nand_ecc_algos[] = {
4237 [NAND_ECC_HAMMING] = "hamming",
4238 [NAND_ECC_BCH] = "bch",
4239};
4240
4241static int of_get_nand_ecc_algo(struct device_node *np)
4242{
4243 const char *pm;
4244 int err, i;
4245
4246 err = of_property_read_string(np, "nand-ecc-algo", &pm);
4247 if (!err) {
4248 for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++)
4249 if (!strcasecmp(pm, nand_ecc_algos[i]))
4250 return i;
4251 return -ENODEV;
4252 }
4253
4254
4255
4256
4257
4258 err = of_property_read_string(np, "nand-ecc-mode", &pm);
4259 if (err < 0)
4260 return err;
4261
4262 if (!strcasecmp(pm, "soft"))
4263 return NAND_ECC_HAMMING;
4264 else if (!strcasecmp(pm, "soft_bch"))
4265 return NAND_ECC_BCH;
4266
4267 return -ENODEV;
4268}
4269
4270static int of_get_nand_ecc_step_size(struct device_node *np)
4271{
4272 int ret;
4273 u32 val;
4274
4275 ret = of_property_read_u32(np, "nand-ecc-step-size", &val);
4276 return ret ? ret : val;
4277}
4278
4279static int of_get_nand_ecc_strength(struct device_node *np)
4280{
4281 int ret;
4282 u32 val;
4283
4284 ret = of_property_read_u32(np, "nand-ecc-strength", &val);
4285 return ret ? ret : val;
4286}
4287
4288static int of_get_nand_bus_width(struct device_node *np)
4289{
4290 u32 val;
4291
4292 if (of_property_read_u32(np, "nand-bus-width", &val))
4293 return 8;
4294
4295 switch (val) {
4296 case 8:
4297 case 16:
4298 return val;
4299 default:
4300 return -EIO;
4301 }
4302}
4303
4304static bool of_get_nand_on_flash_bbt(struct device_node *np)
4305{
4306 return of_property_read_bool(np, "nand-on-flash-bbt");
4307}
4308
4309static int nand_dt_init(struct nand_chip *chip)
4310{
4311 struct device_node *dn = nand_get_flash_node(chip);
4312 int ecc_mode, ecc_algo, ecc_strength, ecc_step;
4313
4314 if (!dn)
4315 return 0;
4316
4317 if (of_get_nand_bus_width(dn) == 16)
4318 chip->options |= NAND_BUSWIDTH_16;
4319
4320 if (of_get_nand_on_flash_bbt(dn))
4321 chip->bbt_options |= NAND_BBT_USE_FLASH;
4322
4323 ecc_mode = of_get_nand_ecc_mode(dn);
4324 ecc_algo = of_get_nand_ecc_algo(dn);
4325 ecc_strength = of_get_nand_ecc_strength(dn);
4326 ecc_step = of_get_nand_ecc_step_size(dn);
4327
4328 if (ecc_mode >= 0)
4329 chip->ecc.mode = ecc_mode;
4330
4331 if (ecc_algo >= 0)
4332 chip->ecc.algo = ecc_algo;
4333
4334 if (ecc_strength >= 0)
4335 chip->ecc.strength = ecc_strength;
4336
4337 if (ecc_step > 0)
4338 chip->ecc.size = ecc_step;
4339
4340 if (of_property_read_bool(dn, "nand-ecc-maximize"))
4341 chip->ecc.options |= NAND_ECC_MAXIMIZE;
4342
4343 return 0;
4344}
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356int nand_scan_ident(struct mtd_info *mtd, int maxchips,
4357 struct nand_flash_dev *table)
4358{
4359 int i, nand_maf_id, nand_dev_id;
4360 struct nand_chip *chip = mtd_to_nand(mtd);
4361 int ret;
4362
4363 ret = nand_dt_init(chip);
4364 if (ret)
4365 return ret;
4366
4367 if (!mtd->name && mtd->dev.parent)
4368 mtd->name = dev_name(mtd->dev.parent);
4369
4370 if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
4371
4372
4373
4374
4375
4376 pr_err("chip.cmd_ctrl() callback is not provided");
4377 return -EINVAL;
4378 }
4379
4380 nand_set_defaults(chip);
4381
4382
4383 ret = nand_detect(chip, table);
4384 if (ret) {
4385 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
4386 pr_warn("No NAND device found\n");
4387 chip->select_chip(mtd, -1);
4388 return ret;
4389 }
4390
4391
4392 ret = nand_init_data_interface(chip);
4393 if (ret)
4394 goto err_nand_init;
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404 ret = nand_setup_data_interface(chip, 0);
4405 if (ret)
4406 goto err_nand_init;
4407
4408 nand_maf_id = chip->id.data[0];
4409 nand_dev_id = chip->id.data[1];
4410
4411 chip->select_chip(mtd, -1);
4412
4413
4414 for (i = 1; i < maxchips; i++) {
4415
4416 nand_reset(chip, i);
4417
4418 chip->select_chip(mtd, i);
4419
4420 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
4421
4422 if (nand_maf_id != chip->read_byte(mtd) ||
4423 nand_dev_id != chip->read_byte(mtd)) {
4424 chip->select_chip(mtd, -1);
4425 break;
4426 }
4427 chip->select_chip(mtd, -1);
4428 }
4429 if (i > 1)
4430 pr_info("%d chips detected\n", i);
4431
4432
4433 chip->numchips = i;
4434 mtd->size = i * chip->chipsize;
4435
4436 return 0;
4437
4438err_nand_init:
4439
4440 nand_manufacturer_cleanup(chip);
4441
4442 return ret;
4443}
4444EXPORT_SYMBOL(nand_scan_ident);
4445
4446static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
4447{
4448 struct nand_chip *chip = mtd_to_nand(mtd);
4449 struct nand_ecc_ctrl *ecc = &chip->ecc;
4450
4451 if (WARN_ON(ecc->mode != NAND_ECC_SOFT))
4452 return -EINVAL;
4453
4454 switch (ecc->algo) {
4455 case NAND_ECC_HAMMING:
4456 ecc->calculate = nand_calculate_ecc;
4457 ecc->correct = nand_correct_data;
4458 ecc->read_page = nand_read_page_swecc;
4459 ecc->read_subpage = nand_read_subpage;
4460 ecc->write_page = nand_write_page_swecc;
4461 ecc->read_page_raw = nand_read_page_raw;
4462 ecc->write_page_raw = nand_write_page_raw;
4463 ecc->read_oob = nand_read_oob_std;
4464 ecc->write_oob = nand_write_oob_std;
4465 if (!ecc->size)
4466 ecc->size = 256;
4467 ecc->bytes = 3;
4468 ecc->strength = 1;
4469 return 0;
4470 case NAND_ECC_BCH:
4471 if (!mtd_nand_has_bch()) {
4472 WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n");
4473 return -EINVAL;
4474 }
4475 ecc->calculate = nand_bch_calculate_ecc;
4476 ecc->correct = nand_bch_correct_data;
4477 ecc->read_page = nand_read_page_swecc;
4478 ecc->read_subpage = nand_read_subpage;
4479 ecc->write_page = nand_write_page_swecc;
4480 ecc->read_page_raw = nand_read_page_raw;
4481 ecc->write_page_raw = nand_write_page_raw;
4482 ecc->read_oob = nand_read_oob_std;
4483 ecc->write_oob = nand_write_oob_std;
4484
4485
4486
4487
4488
4489
4490 if (!ecc->size && (mtd->oobsize >= 64)) {
4491 ecc->size = 512;
4492 ecc->strength = 4;
4493 }
4494
4495
4496
4497
4498
4499 if (!mtd->ooblayout) {
4500
4501 if (mtd->oobsize < 64) {
4502 WARN(1, "OOB layout is required when using software BCH on small pages\n");
4503 return -EINVAL;
4504 }
4505
4506 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
4507
4508 }
4509
4510
4511
4512
4513
4514
4515 if (mtd->ooblayout == &nand_ooblayout_lp_ops &&
4516 ecc->options & NAND_ECC_MAXIMIZE) {
4517 int steps, bytes;
4518
4519
4520 ecc->size = 1024;
4521 steps = mtd->writesize / ecc->size;
4522
4523
4524 bytes = (mtd->oobsize - 2) / steps;
4525 ecc->strength = bytes * 8 / fls(8 * ecc->size);
4526 }
4527
4528
4529 ecc->bytes = 0;
4530 ecc->priv = nand_bch_init(mtd);
4531 if (!ecc->priv) {
4532 WARN(1, "BCH ECC initialization failed!\n");
4533 return -EINVAL;
4534 }
4535 return 0;
4536 default:
4537 WARN(1, "Unsupported ECC algorithm!\n");
4538 return -EINVAL;
4539 }
4540}
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552int nand_check_ecc_caps(struct nand_chip *chip,
4553 const struct nand_ecc_caps *caps, int oobavail)
4554{
4555 struct mtd_info *mtd = nand_to_mtd(chip);
4556 const struct nand_ecc_step_info *stepinfo;
4557 int preset_step = chip->ecc.size;
4558 int preset_strength = chip->ecc.strength;
4559 int nsteps, ecc_bytes;
4560 int i, j;
4561
4562 if (WARN_ON(oobavail < 0))
4563 return -EINVAL;
4564
4565 if (!preset_step || !preset_strength)
4566 return -ENODATA;
4567
4568 nsteps = mtd->writesize / preset_step;
4569
4570 for (i = 0; i < caps->nstepinfos; i++) {
4571 stepinfo = &caps->stepinfos[i];
4572
4573 if (stepinfo->stepsize != preset_step)
4574 continue;
4575
4576 for (j = 0; j < stepinfo->nstrengths; j++) {
4577 if (stepinfo->strengths[j] != preset_strength)
4578 continue;
4579
4580 ecc_bytes = caps->calc_ecc_bytes(preset_step,
4581 preset_strength);
4582 if (WARN_ON_ONCE(ecc_bytes < 0))
4583 return ecc_bytes;
4584
4585 if (ecc_bytes * nsteps > oobavail) {
4586 pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB",
4587 preset_step, preset_strength);
4588 return -ENOSPC;
4589 }
4590
4591 chip->ecc.bytes = ecc_bytes;
4592
4593 return 0;
4594 }
4595 }
4596
4597 pr_err("ECC (step, strength) = (%d, %d) not supported on this controller",
4598 preset_step, preset_strength);
4599
4600 return -ENOTSUPP;
4601}
4602EXPORT_SYMBOL_GPL(nand_check_ecc_caps);
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614int nand_match_ecc_req(struct nand_chip *chip,
4615 const struct nand_ecc_caps *caps, int oobavail)
4616{
4617 struct mtd_info *mtd = nand_to_mtd(chip);
4618 const struct nand_ecc_step_info *stepinfo;
4619 int req_step = chip->ecc_step_ds;
4620 int req_strength = chip->ecc_strength_ds;
4621 int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total;
4622 int best_step, best_strength, best_ecc_bytes;
4623 int best_ecc_bytes_total = INT_MAX;
4624 int i, j;
4625
4626 if (WARN_ON(oobavail < 0))
4627 return -EINVAL;
4628
4629
4630 if (!req_step || !req_strength)
4631 return -ENOTSUPP;
4632
4633
4634 req_corr = mtd->writesize / req_step * req_strength;
4635
4636 for (i = 0; i < caps->nstepinfos; i++) {
4637 stepinfo = &caps->stepinfos[i];
4638 step_size = stepinfo->stepsize;
4639
4640 for (j = 0; j < stepinfo->nstrengths; j++) {
4641 strength = stepinfo->strengths[j];
4642
4643
4644
4645
4646
4647
4648 if (step_size < req_step && strength < req_strength)
4649 continue;
4650
4651 if (mtd->writesize % step_size)
4652 continue;
4653
4654 nsteps = mtd->writesize / step_size;
4655
4656 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4657 if (WARN_ON_ONCE(ecc_bytes < 0))
4658 continue;
4659 ecc_bytes_total = ecc_bytes * nsteps;
4660
4661 if (ecc_bytes_total > oobavail ||
4662 strength * nsteps < req_corr)
4663 continue;
4664
4665
4666
4667
4668
4669 if (ecc_bytes_total < best_ecc_bytes_total) {
4670 best_ecc_bytes_total = ecc_bytes_total;
4671 best_step = step_size;
4672 best_strength = strength;
4673 best_ecc_bytes = ecc_bytes;
4674 }
4675 }
4676 }
4677
4678 if (best_ecc_bytes_total == INT_MAX)
4679 return -ENOTSUPP;
4680
4681 chip->ecc.size = best_step;
4682 chip->ecc.strength = best_strength;
4683 chip->ecc.bytes = best_ecc_bytes;
4684
4685 return 0;
4686}
4687EXPORT_SYMBOL_GPL(nand_match_ecc_req);
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698int nand_maximize_ecc(struct nand_chip *chip,
4699 const struct nand_ecc_caps *caps, int oobavail)
4700{
4701 struct mtd_info *mtd = nand_to_mtd(chip);
4702 const struct nand_ecc_step_info *stepinfo;
4703 int step_size, strength, nsteps, ecc_bytes, corr;
4704 int best_corr = 0;
4705 int best_step = 0;
4706 int best_strength, best_ecc_bytes;
4707 int i, j;
4708
4709 if (WARN_ON(oobavail < 0))
4710 return -EINVAL;
4711
4712 for (i = 0; i < caps->nstepinfos; i++) {
4713 stepinfo = &caps->stepinfos[i];
4714 step_size = stepinfo->stepsize;
4715
4716
4717 if (chip->ecc.size && step_size != chip->ecc.size)
4718 continue;
4719
4720 for (j = 0; j < stepinfo->nstrengths; j++) {
4721 strength = stepinfo->strengths[j];
4722
4723 if (mtd->writesize % step_size)
4724 continue;
4725
4726 nsteps = mtd->writesize / step_size;
4727
4728 ecc_bytes = caps->calc_ecc_bytes(step_size, strength);
4729 if (WARN_ON_ONCE(ecc_bytes < 0))
4730 continue;
4731
4732 if (ecc_bytes * nsteps > oobavail)
4733 continue;
4734
4735 corr = strength * nsteps;
4736
4737
4738
4739
4740
4741 if (corr > best_corr ||
4742 (corr == best_corr && step_size > best_step)) {
4743 best_corr = corr;
4744 best_step = step_size;
4745 best_strength = strength;
4746 best_ecc_bytes = ecc_bytes;
4747 }
4748 }
4749 }
4750
4751 if (!best_corr)
4752 return -ENOTSUPP;
4753
4754 chip->ecc.size = best_step;
4755 chip->ecc.strength = best_strength;
4756 chip->ecc.bytes = best_ecc_bytes;
4757
4758 return 0;
4759}
4760EXPORT_SYMBOL_GPL(nand_maximize_ecc);
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776static bool nand_ecc_strength_good(struct mtd_info *mtd)
4777{
4778 struct nand_chip *chip = mtd_to_nand(mtd);
4779 struct nand_ecc_ctrl *ecc = &chip->ecc;
4780 int corr, ds_corr;
4781
4782 if (ecc->size == 0 || chip->ecc_step_ds == 0)
4783
4784 return true;
4785
4786
4787
4788
4789
4790 corr = (mtd->writesize * ecc->strength) / ecc->size;
4791 ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds;
4792
4793 return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds;
4794}
4795
4796static bool invalid_ecc_page_accessors(struct nand_chip *chip)
4797{
4798 struct nand_ecc_ctrl *ecc = &chip->ecc;
4799
4800 if (nand_standard_page_accessors(ecc))
4801 return false;
4802
4803
4804
4805
4806
4807
4808
4809 return (!ecc->read_page || !ecc->write_page ||
4810 !ecc->read_page_raw || !ecc->write_page_raw ||
4811 (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) ||
4812 (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage &&
4813 ecc->hwctl && ecc->calculate));
4814}
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824int nand_scan_tail(struct mtd_info *mtd)
4825{
4826 struct nand_chip *chip = mtd_to_nand(mtd);
4827 struct nand_ecc_ctrl *ecc = &chip->ecc;
4828 struct nand_buffers *nbuf = NULL;
4829 int ret;
4830
4831
4832 if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
4833 !(chip->bbt_options & NAND_BBT_USE_FLASH))) {
4834 ret = -EINVAL;
4835 goto err_ident;
4836 }
4837
4838 if (invalid_ecc_page_accessors(chip)) {
4839 pr_err("Invalid ECC page accessors setup\n");
4840 ret = -EINVAL;
4841 goto err_ident;
4842 }
4843
4844 if (!(chip->options & NAND_OWN_BUFFERS)) {
4845 nbuf = kzalloc(sizeof(*nbuf), GFP_KERNEL);
4846 if (!nbuf) {
4847 ret = -ENOMEM;
4848 goto err_ident;
4849 }
4850
4851 nbuf->ecccalc = kmalloc(mtd->oobsize, GFP_KERNEL);
4852 if (!nbuf->ecccalc) {
4853 ret = -ENOMEM;
4854 goto err_free;
4855 }
4856
4857 nbuf->ecccode = kmalloc(mtd->oobsize, GFP_KERNEL);
4858 if (!nbuf->ecccode) {
4859 ret = -ENOMEM;
4860 goto err_free;
4861 }
4862
4863 nbuf->databuf = kmalloc(mtd->writesize + mtd->oobsize,
4864 GFP_KERNEL);
4865 if (!nbuf->databuf) {
4866 ret = -ENOMEM;
4867 goto err_free;
4868 }
4869
4870 chip->buffers = nbuf;
4871 } else {
4872 if (!chip->buffers) {
4873 ret = -ENOMEM;
4874 goto err_ident;
4875 }
4876 }
4877
4878
4879 chip->oob_poi = chip->buffers->databuf + mtd->writesize;
4880
4881
4882
4883
4884 if (!mtd->ooblayout &&
4885 !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) {
4886 switch (mtd->oobsize) {
4887 case 8:
4888 case 16:
4889 mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops);
4890 break;
4891 case 64:
4892 case 128:
4893 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops);
4894 break;
4895 default:
4896 WARN(1, "No oob scheme defined for oobsize %d\n",
4897 mtd->oobsize);
4898 ret = -EINVAL;
4899 goto err_free;
4900 }
4901 }
4902
4903
4904
4905
4906
4907
4908 switch (ecc->mode) {
4909 case NAND_ECC_HW_OOB_FIRST:
4910
4911 if (!ecc->calculate || !ecc->correct || !ecc->hwctl) {
4912 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4913 ret = -EINVAL;
4914 goto err_free;
4915 }
4916 if (!ecc->read_page)
4917 ecc->read_page = nand_read_page_hwecc_oob_first;
4918
4919 case NAND_ECC_HW:
4920
4921 if (!ecc->read_page)
4922 ecc->read_page = nand_read_page_hwecc;
4923 if (!ecc->write_page)
4924 ecc->write_page = nand_write_page_hwecc;
4925 if (!ecc->read_page_raw)
4926 ecc->read_page_raw = nand_read_page_raw;
4927 if (!ecc->write_page_raw)
4928 ecc->write_page_raw = nand_write_page_raw;
4929 if (!ecc->read_oob)
4930 ecc->read_oob = nand_read_oob_std;
4931 if (!ecc->write_oob)
4932 ecc->write_oob = nand_write_oob_std;
4933 if (!ecc->read_subpage)
4934 ecc->read_subpage = nand_read_subpage;
4935 if (!ecc->write_subpage && ecc->hwctl && ecc->calculate)
4936 ecc->write_subpage = nand_write_subpage_hwecc;
4937
4938 case NAND_ECC_HW_SYNDROME:
4939 if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) &&
4940 (!ecc->read_page ||
4941 ecc->read_page == nand_read_page_hwecc ||
4942 !ecc->write_page ||
4943 ecc->write_page == nand_write_page_hwecc)) {
4944 WARN(1, "No ECC functions supplied; hardware ECC not possible\n");
4945 ret = -EINVAL;
4946 goto err_free;
4947 }
4948
4949 if (!ecc->read_page)
4950 ecc->read_page = nand_read_page_syndrome;
4951 if (!ecc->write_page)
4952 ecc->write_page = nand_write_page_syndrome;
4953 if (!ecc->read_page_raw)
4954 ecc->read_page_raw = nand_read_page_raw_syndrome;
4955 if (!ecc->write_page_raw)
4956 ecc->write_page_raw = nand_write_page_raw_syndrome;
4957 if (!ecc->read_oob)
4958 ecc->read_oob = nand_read_oob_syndrome;
4959 if (!ecc->write_oob)
4960 ecc->write_oob = nand_write_oob_syndrome;
4961
4962 if (mtd->writesize >= ecc->size) {
4963 if (!ecc->strength) {
4964 WARN(1, "Driver must set ecc.strength when using hardware ECC\n");
4965 ret = -EINVAL;
4966 goto err_free;
4967 }
4968 break;
4969 }
4970 pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n",
4971 ecc->size, mtd->writesize);
4972 ecc->mode = NAND_ECC_SOFT;
4973 ecc->algo = NAND_ECC_HAMMING;
4974
4975 case NAND_ECC_SOFT:
4976 ret = nand_set_ecc_soft_ops(mtd);
4977 if (ret) {
4978 ret = -EINVAL;
4979 goto err_free;
4980 }
4981 break;
4982
4983 case NAND_ECC_ON_DIE:
4984 if (!ecc->read_page || !ecc->write_page) {
4985 WARN(1, "No ECC functions supplied; on-die ECC not possible\n");
4986 ret = -EINVAL;
4987 goto err_free;
4988 }
4989 if (!ecc->read_oob)
4990 ecc->read_oob = nand_read_oob_std;
4991 if (!ecc->write_oob)
4992 ecc->write_oob = nand_write_oob_std;
4993 break;
4994
4995 case NAND_ECC_NONE:
4996 pr_warn("NAND_ECC_NONE selected by board driver. This is not recommended!\n");
4997 ecc->read_page = nand_read_page_raw;
4998 ecc->write_page = nand_write_page_raw;
4999 ecc->read_oob = nand_read_oob_std;
5000 ecc->read_page_raw = nand_read_page_raw;
5001 ecc->write_page_raw = nand_write_page_raw;
5002 ecc->write_oob = nand_write_oob_std;
5003 ecc->size = mtd->writesize;
5004 ecc->bytes = 0;
5005 ecc->strength = 0;
5006 break;
5007
5008 default:
5009 WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode);
5010 ret = -EINVAL;
5011 goto err_free;
5012 }
5013
5014
5015 if (!ecc->read_oob_raw)
5016 ecc->read_oob_raw = ecc->read_oob;
5017 if (!ecc->write_oob_raw)
5018 ecc->write_oob_raw = ecc->write_oob;
5019
5020
5021 mtd->ecc_strength = ecc->strength;
5022 mtd->ecc_step_size = ecc->size;
5023
5024
5025
5026
5027
5028 ecc->steps = mtd->writesize / ecc->size;
5029 if (ecc->steps * ecc->size != mtd->writesize) {
5030 WARN(1, "Invalid ECC parameters\n");
5031 ret = -EINVAL;
5032 goto err_free;
5033 }
5034 ecc->total = ecc->steps * ecc->bytes;
5035 if (ecc->total > mtd->oobsize) {
5036 WARN(1, "Total number of ECC bytes exceeded oobsize\n");
5037 ret = -EINVAL;
5038 goto err_free;
5039 }
5040
5041
5042
5043
5044
5045 ret = mtd_ooblayout_count_freebytes(mtd);
5046 if (ret < 0)
5047 ret = 0;
5048
5049 mtd->oobavail = ret;
5050
5051
5052 if (!nand_ecc_strength_good(mtd))
5053 pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n",
5054 mtd->name);
5055
5056
5057 if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) {
5058 switch (ecc->steps) {
5059 case 2:
5060 mtd->subpage_sft = 1;
5061 break;
5062 case 4:
5063 case 8:
5064 case 16:
5065 mtd->subpage_sft = 2;
5066 break;
5067 }
5068 }
5069 chip->subpagesize = mtd->writesize >> mtd->subpage_sft;
5070
5071
5072 chip->state = FL_READY;
5073
5074
5075 chip->pagebuf = -1;
5076
5077
5078 switch (ecc->mode) {
5079 case NAND_ECC_SOFT:
5080 if (chip->page_shift > 9)
5081 chip->options |= NAND_SUBPAGE_READ;
5082 break;
5083
5084 default:
5085 break;
5086 }
5087
5088
5089 mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH;
5090 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
5091 MTD_CAP_NANDFLASH;
5092 mtd->_erase = nand_erase;
5093 mtd->_point = NULL;
5094 mtd->_unpoint = NULL;
5095 mtd->_read = nand_read;
5096 mtd->_write = nand_write;
5097 mtd->_panic_write = panic_nand_write;
5098 mtd->_read_oob = nand_read_oob;
5099 mtd->_write_oob = nand_write_oob;
5100 mtd->_sync = nand_sync;
5101 mtd->_lock = NULL;
5102 mtd->_unlock = NULL;
5103 mtd->_suspend = nand_suspend;
5104 mtd->_resume = nand_resume;
5105 mtd->_reboot = nand_shutdown;
5106 mtd->_block_isreserved = nand_block_isreserved;
5107 mtd->_block_isbad = nand_block_isbad;
5108 mtd->_block_markbad = nand_block_markbad;
5109 mtd->_max_bad_blocks = nand_max_bad_blocks;
5110 mtd->writebufsize = mtd->writesize;
5111
5112
5113
5114
5115
5116
5117 if (!mtd->bitflip_threshold)
5118 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
5119
5120
5121 if (chip->options & NAND_SKIP_BBTSCAN)
5122 return 0;
5123
5124
5125 ret = chip->scan_bbt(mtd);
5126 if (ret)
5127 goto err_free;
5128 return 0;
5129
5130err_free:
5131 if (nbuf) {
5132 kfree(nbuf->databuf);
5133 kfree(nbuf->ecccode);
5134 kfree(nbuf->ecccalc);
5135 kfree(nbuf);
5136 }
5137
5138err_ident:
5139
5140
5141
5142 nand_manufacturer_cleanup(chip);
5143
5144 return ret;
5145}
5146EXPORT_SYMBOL(nand_scan_tail);
5147
5148
5149
5150
5151
5152
5153#ifdef MODULE
5154#define caller_is_module() (1)
5155#else
5156#define caller_is_module() \
5157 is_module_text_address((unsigned long)__builtin_return_address(0))
5158#endif
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169int nand_scan(struct mtd_info *mtd, int maxchips)
5170{
5171 int ret;
5172
5173 ret = nand_scan_ident(mtd, maxchips, NULL);
5174 if (!ret)
5175 ret = nand_scan_tail(mtd);
5176 return ret;
5177}
5178EXPORT_SYMBOL(nand_scan);
5179
5180
5181
5182
5183
5184void nand_cleanup(struct nand_chip *chip)
5185{
5186 if (chip->ecc.mode == NAND_ECC_SOFT &&
5187 chip->ecc.algo == NAND_ECC_BCH)
5188 nand_bch_free((struct nand_bch_control *)chip->ecc.priv);
5189
5190 nand_release_data_interface(chip);
5191
5192
5193 kfree(chip->bbt);
5194 if (!(chip->options & NAND_OWN_BUFFERS) && chip->buffers) {
5195 kfree(chip->buffers->databuf);
5196 kfree(chip->buffers->ecccode);
5197 kfree(chip->buffers->ecccalc);
5198 kfree(chip->buffers);
5199 }
5200
5201
5202 if (chip->badblock_pattern && chip->badblock_pattern->options
5203 & NAND_BBT_DYNAMICSTRUCT)
5204 kfree(chip->badblock_pattern);
5205
5206
5207 nand_manufacturer_cleanup(chip);
5208}
5209EXPORT_SYMBOL_GPL(nand_cleanup);
5210
5211
5212
5213
5214
5215
5216void nand_release(struct mtd_info *mtd)
5217{
5218 mtd_device_unregister(mtd);
5219 nand_cleanup(mtd_to_nand(mtd));
5220}
5221EXPORT_SYMBOL_GPL(nand_release);
5222
5223MODULE_LICENSE("GPL");
5224MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>");
5225MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
5226MODULE_DESCRIPTION("Generic NAND flash driver code");
5227