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