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