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