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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56#include <linux/slab.h>
57#include <linux/types.h>
58#include <linux/mtd/mtd.h>
59#include <linux/mtd/bbm.h>
60#include <linux/bitops.h>
61#include <linux/delay.h>
62#include <linux/vmalloc.h>
63#include <linux/export.h>
64#include <linux/string.h>
65
66#include "internals.h"
67
68#define BBT_BLOCK_GOOD 0x00
69#define BBT_BLOCK_WORN 0x01
70#define BBT_BLOCK_RESERVED 0x02
71#define BBT_BLOCK_FACTORY_BAD 0x03
72
73#define BBT_ENTRY_MASK 0x03
74#define BBT_ENTRY_SHIFT 2
75
76static inline uint8_t bbt_get_entry(struct nand_chip *chip, int block)
77{
78 uint8_t entry = chip->bbt[block >> BBT_ENTRY_SHIFT];
79 entry >>= (block & BBT_ENTRY_MASK) * 2;
80 return entry & BBT_ENTRY_MASK;
81}
82
83static inline void bbt_mark_entry(struct nand_chip *chip, int block,
84 uint8_t mark)
85{
86 uint8_t msk = (mark & BBT_ENTRY_MASK) << ((block & BBT_ENTRY_MASK) * 2);
87 chip->bbt[block >> BBT_ENTRY_SHIFT] |= msk;
88}
89
90static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
91{
92 if (memcmp(buf, td->pattern, td->len))
93 return -1;
94 return 0;
95}
96
97
98
99
100
101
102
103
104
105
106
107static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
108{
109 if (td->options & NAND_BBT_NO_OOB)
110 return check_pattern_no_oob(buf, td);
111
112
113 if (memcmp(buf + paglen + td->offs, td->pattern, td->len))
114 return -1;
115
116 return 0;
117}
118
119
120
121
122
123
124
125
126
127
128static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
129{
130
131 if (memcmp(buf + td->offs, td->pattern, td->len))
132 return -1;
133 return 0;
134}
135
136
137
138
139
140
141
142static u32 add_marker_len(struct nand_bbt_descr *td)
143{
144 u32 len;
145
146 if (!(td->options & NAND_BBT_NO_OOB))
147 return 0;
148
149 len = td->len;
150 if (td->options & NAND_BBT_VERSION)
151 len++;
152 return len;
153}
154
155
156
157
158
159
160
161
162
163
164
165
166static int read_bbt(struct nand_chip *this, uint8_t *buf, int page, int num,
167 struct nand_bbt_descr *td, int offs)
168{
169 struct mtd_info *mtd = nand_to_mtd(this);
170 int res, ret = 0, i, j, act = 0;
171 size_t retlen, len, totlen;
172 loff_t from;
173 int bits = td->options & NAND_BBT_NRBITS_MSK;
174 uint8_t msk = (uint8_t)((1 << bits) - 1);
175 u32 marker_len;
176 int reserved_block_code = td->reserved_block_code;
177
178 totlen = (num * bits) >> 3;
179 marker_len = add_marker_len(td);
180 from = ((loff_t)page) << this->page_shift;
181
182 while (totlen) {
183 len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
184 if (marker_len) {
185
186
187
188
189 len -= marker_len;
190 from += marker_len;
191 marker_len = 0;
192 }
193 res = mtd_read(mtd, from, len, &retlen, buf);
194 if (res < 0) {
195 if (mtd_is_eccerr(res)) {
196 pr_info("nand_bbt: ECC error in BBT at 0x%012llx\n",
197 from & ~mtd->writesize);
198 return res;
199 } else if (mtd_is_bitflip(res)) {
200 pr_info("nand_bbt: corrected error in BBT at 0x%012llx\n",
201 from & ~mtd->writesize);
202 ret = res;
203 } else {
204 pr_info("nand_bbt: error reading BBT\n");
205 return res;
206 }
207 }
208
209
210 for (i = 0; i < len; i++) {
211 uint8_t dat = buf[i];
212 for (j = 0; j < 8; j += bits, act++) {
213 uint8_t tmp = (dat >> j) & msk;
214 if (tmp == msk)
215 continue;
216 if (reserved_block_code && (tmp == reserved_block_code)) {
217 pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
218 (loff_t)(offs + act) <<
219 this->bbt_erase_shift);
220 bbt_mark_entry(this, offs + act,
221 BBT_BLOCK_RESERVED);
222 mtd->ecc_stats.bbtblocks++;
223 continue;
224 }
225
226
227
228
229 pr_info("nand_read_bbt: bad block at 0x%012llx\n",
230 (loff_t)(offs + act) <<
231 this->bbt_erase_shift);
232
233 if (tmp == 0)
234 bbt_mark_entry(this, offs + act,
235 BBT_BLOCK_FACTORY_BAD);
236 else
237 bbt_mark_entry(this, offs + act,
238 BBT_BLOCK_WORN);
239 mtd->ecc_stats.badblocks++;
240 }
241 }
242 totlen -= len;
243 from += len;
244 }
245 return ret;
246}
247
248
249
250
251
252
253
254
255
256
257
258
259static int read_abs_bbt(struct nand_chip *this, uint8_t *buf,
260 struct nand_bbt_descr *td, int chip)
261{
262 struct mtd_info *mtd = nand_to_mtd(this);
263 u64 targetsize = nanddev_target_size(&this->base);
264 int res = 0, i;
265
266 if (td->options & NAND_BBT_PERCHIP) {
267 int offs = 0;
268 for (i = 0; i < nanddev_ntargets(&this->base); i++) {
269 if (chip == -1 || chip == i)
270 res = read_bbt(this, buf, td->pages[i],
271 targetsize >> this->bbt_erase_shift,
272 td, offs);
273 if (res)
274 return res;
275 offs += targetsize >> this->bbt_erase_shift;
276 }
277 } else {
278 res = read_bbt(this, buf, td->pages[0],
279 mtd->size >> this->bbt_erase_shift, td, 0);
280 if (res)
281 return res;
282 }
283 return 0;
284}
285
286
287static int scan_read_data(struct nand_chip *this, uint8_t *buf, loff_t offs,
288 struct nand_bbt_descr *td)
289{
290 struct mtd_info *mtd = nand_to_mtd(this);
291 size_t retlen;
292 size_t len;
293
294 len = td->len;
295 if (td->options & NAND_BBT_VERSION)
296 len++;
297
298 return mtd_read(mtd, offs, len, &retlen, buf);
299}
300
301
302
303
304
305
306
307
308
309
310
311
312static int scan_read_oob(struct nand_chip *this, uint8_t *buf, loff_t offs,
313 size_t len)
314{
315 struct mtd_info *mtd = nand_to_mtd(this);
316 struct mtd_oob_ops ops;
317 int res, ret = 0;
318
319 ops.mode = MTD_OPS_PLACE_OOB;
320 ops.ooboffs = 0;
321 ops.ooblen = mtd->oobsize;
322
323 while (len > 0) {
324 ops.datbuf = buf;
325 ops.len = min(len, (size_t)mtd->writesize);
326 ops.oobbuf = buf + ops.len;
327
328 res = mtd_read_oob(mtd, offs, &ops);
329 if (res) {
330 if (!mtd_is_bitflip_or_eccerr(res))
331 return res;
332 else if (mtd_is_eccerr(res) || !ret)
333 ret = res;
334 }
335
336 buf += mtd->oobsize + mtd->writesize;
337 len -= mtd->writesize;
338 offs += mtd->writesize;
339 }
340 return ret;
341}
342
343static int scan_read(struct nand_chip *this, uint8_t *buf, loff_t offs,
344 size_t len, struct nand_bbt_descr *td)
345{
346 if (td->options & NAND_BBT_NO_OOB)
347 return scan_read_data(this, buf, offs, td);
348 else
349 return scan_read_oob(this, buf, offs, len);
350}
351
352
353static int scan_write_bbt(struct nand_chip *this, loff_t offs, size_t len,
354 uint8_t *buf, uint8_t *oob)
355{
356 struct mtd_info *mtd = nand_to_mtd(this);
357 struct mtd_oob_ops ops;
358
359 ops.mode = MTD_OPS_PLACE_OOB;
360 ops.ooboffs = 0;
361 ops.ooblen = mtd->oobsize;
362 ops.datbuf = buf;
363 ops.oobbuf = oob;
364 ops.len = len;
365
366 return mtd_write_oob(mtd, offs, &ops);
367}
368
369static u32 bbt_get_ver_offs(struct nand_chip *this, struct nand_bbt_descr *td)
370{
371 struct mtd_info *mtd = nand_to_mtd(this);
372 u32 ver_offs = td->veroffs;
373
374 if (!(td->options & NAND_BBT_NO_OOB))
375 ver_offs += mtd->writesize;
376 return ver_offs;
377}
378
379
380
381
382
383
384
385
386
387
388
389static void read_abs_bbts(struct nand_chip *this, uint8_t *buf,
390 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
391{
392 struct mtd_info *mtd = nand_to_mtd(this);
393
394
395 if (td->options & NAND_BBT_VERSION) {
396 scan_read(this, buf, (loff_t)td->pages[0] << this->page_shift,
397 mtd->writesize, td);
398 td->version[0] = buf[bbt_get_ver_offs(this, td)];
399 pr_info("Bad block table at page %d, version 0x%02X\n",
400 td->pages[0], td->version[0]);
401 }
402
403
404 if (md && (md->options & NAND_BBT_VERSION)) {
405 scan_read(this, buf, (loff_t)md->pages[0] << this->page_shift,
406 mtd->writesize, md);
407 md->version[0] = buf[bbt_get_ver_offs(this, md)];
408 pr_info("Bad block table at page %d, version 0x%02X\n",
409 md->pages[0], md->version[0]);
410 }
411}
412
413
414static int scan_block_fast(struct nand_chip *this, struct nand_bbt_descr *bd,
415 loff_t offs, uint8_t *buf)
416{
417 struct mtd_info *mtd = nand_to_mtd(this);
418
419 struct mtd_oob_ops ops;
420 int ret, page_offset;
421
422 ops.ooblen = mtd->oobsize;
423 ops.oobbuf = buf;
424 ops.ooboffs = 0;
425 ops.datbuf = NULL;
426 ops.mode = MTD_OPS_PLACE_OOB;
427
428 page_offset = nand_bbm_get_next_page(this, 0);
429
430 while (page_offset >= 0) {
431
432
433
434
435 ret = mtd_read_oob(mtd, offs + (page_offset * mtd->writesize),
436 &ops);
437
438 if (ret && !mtd_is_bitflip_or_eccerr(ret))
439 return ret;
440
441 if (check_short_pattern(buf, bd))
442 return 1;
443
444 page_offset = nand_bbm_get_next_page(this, page_offset + 1);
445 }
446
447 return 0;
448}
449
450
451static int bbt_block_checkbad(struct nand_chip *this, struct nand_bbt_descr *td,
452 loff_t offs, uint8_t *buf)
453{
454 struct nand_bbt_descr *bd = this->badblock_pattern;
455
456
457
458
459
460
461 if (!(td->options & NAND_BBT_NO_OOB) &&
462 td->offs >= bd->offs && td->offs < bd->offs + bd->len)
463 return 0;
464
465
466
467
468
469 if (this->bbt_options & NAND_BBT_NO_OOB_BBM ||
470 this->options & NAND_NO_BBM_QUIRK)
471 return 0;
472
473 if (scan_block_fast(this, bd, offs, buf) > 0)
474 return 1;
475
476 return 0;
477}
478
479
480
481
482
483
484
485
486
487
488
489
490static int create_bbt(struct nand_chip *this, uint8_t *buf,
491 struct nand_bbt_descr *bd, int chip)
492{
493 u64 targetsize = nanddev_target_size(&this->base);
494 struct mtd_info *mtd = nand_to_mtd(this);
495 int i, numblocks, startblock;
496 loff_t from;
497
498 pr_info("Scanning device for bad blocks\n");
499
500 if (chip == -1) {
501 numblocks = mtd->size >> this->bbt_erase_shift;
502 startblock = 0;
503 from = 0;
504 } else {
505 if (chip >= nanddev_ntargets(&this->base)) {
506 pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
507 chip + 1, nanddev_ntargets(&this->base));
508 return -EINVAL;
509 }
510 numblocks = targetsize >> this->bbt_erase_shift;
511 startblock = chip * numblocks;
512 numblocks += startblock;
513 from = (loff_t)startblock << this->bbt_erase_shift;
514 }
515
516 for (i = startblock; i < numblocks; i++) {
517 int ret;
518
519 BUG_ON(bd->options & NAND_BBT_NO_OOB);
520
521 ret = scan_block_fast(this, bd, from, buf);
522 if (ret < 0)
523 return ret;
524
525 if (ret) {
526 bbt_mark_entry(this, i, BBT_BLOCK_FACTORY_BAD);
527 pr_warn("Bad eraseblock %d at 0x%012llx\n",
528 i, (unsigned long long)from);
529 mtd->ecc_stats.badblocks++;
530 }
531
532 from += (1 << this->bbt_erase_shift);
533 }
534 return 0;
535}
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552static int search_bbt(struct nand_chip *this, uint8_t *buf,
553 struct nand_bbt_descr *td)
554{
555 u64 targetsize = nanddev_target_size(&this->base);
556 struct mtd_info *mtd = nand_to_mtd(this);
557 int i, chips;
558 int startblock, block, dir;
559 int scanlen = mtd->writesize + mtd->oobsize;
560 int bbtblocks;
561 int blocktopage = this->bbt_erase_shift - this->page_shift;
562
563
564 if (td->options & NAND_BBT_LASTBLOCK) {
565 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
566 dir = -1;
567 } else {
568 startblock = 0;
569 dir = 1;
570 }
571
572
573 if (td->options & NAND_BBT_PERCHIP) {
574 chips = nanddev_ntargets(&this->base);
575 bbtblocks = targetsize >> this->bbt_erase_shift;
576 startblock &= bbtblocks - 1;
577 } else {
578 chips = 1;
579 bbtblocks = mtd->size >> this->bbt_erase_shift;
580 }
581
582 for (i = 0; i < chips; i++) {
583
584 td->version[i] = 0;
585 td->pages[i] = -1;
586
587 for (block = 0; block < td->maxblocks; block++) {
588
589 int actblock = startblock + dir * block;
590 loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
591
592
593 if (bbt_block_checkbad(this, td, offs, buf))
594 continue;
595
596
597 scan_read(this, buf, offs, mtd->writesize, td);
598 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
599 td->pages[i] = actblock << blocktopage;
600 if (td->options & NAND_BBT_VERSION) {
601 offs = bbt_get_ver_offs(this, td);
602 td->version[i] = buf[offs];
603 }
604 break;
605 }
606 }
607 startblock += targetsize >> this->bbt_erase_shift;
608 }
609
610 for (i = 0; i < chips; i++) {
611 if (td->pages[i] == -1)
612 pr_warn("Bad block table not found for chip %d\n", i);
613 else
614 pr_info("Bad block table found at page %d, version 0x%02X\n",
615 td->pages[i], td->version[i]);
616 }
617 return 0;
618}
619
620
621
622
623
624
625
626
627
628
629static void search_read_bbts(struct nand_chip *this, uint8_t *buf,
630 struct nand_bbt_descr *td,
631 struct nand_bbt_descr *md)
632{
633
634 search_bbt(this, buf, td);
635
636
637 if (md)
638 search_bbt(this, buf, md);
639}
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654static int get_bbt_block(struct nand_chip *this, struct nand_bbt_descr *td,
655 struct nand_bbt_descr *md, int chip)
656{
657 u64 targetsize = nanddev_target_size(&this->base);
658 int startblock, dir, page, numblocks, i;
659
660
661
662
663
664
665 if (td->pages[chip] != -1)
666 return td->pages[chip] >>
667 (this->bbt_erase_shift - this->page_shift);
668
669 numblocks = (int)(targetsize >> this->bbt_erase_shift);
670 if (!(td->options & NAND_BBT_PERCHIP))
671 numblocks *= nanddev_ntargets(&this->base);
672
673
674
675
676
677 if (td->options & NAND_BBT_LASTBLOCK) {
678 startblock = numblocks * (chip + 1) - 1;
679 dir = -1;
680 } else {
681 startblock = chip * numblocks;
682 dir = 1;
683 }
684
685 for (i = 0; i < td->maxblocks; i++) {
686 int block = startblock + dir * i;
687
688
689 switch (bbt_get_entry(this, block)) {
690 case BBT_BLOCK_WORN:
691 case BBT_BLOCK_FACTORY_BAD:
692 continue;
693 }
694
695 page = block << (this->bbt_erase_shift - this->page_shift);
696
697
698 if (!md || md->pages[chip] != page)
699 return block;
700 }
701
702 return -ENOSPC;
703}
704
705
706
707
708
709
710
711
712
713
714
715
716
717static void mark_bbt_block_bad(struct nand_chip *this,
718 struct nand_bbt_descr *td,
719 int chip, int block)
720{
721 loff_t to;
722 int res;
723
724 bbt_mark_entry(this, block, BBT_BLOCK_WORN);
725
726 to = (loff_t)block << this->bbt_erase_shift;
727 res = nand_markbad_bbm(this, to);
728 if (res)
729 pr_warn("nand_bbt: error %d while marking block %d bad\n",
730 res, block);
731
732 td->pages[chip] = -1;
733}
734
735
736
737
738
739
740
741
742
743
744
745static int write_bbt(struct nand_chip *this, uint8_t *buf,
746 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
747 int chipsel)
748{
749 u64 targetsize = nanddev_target_size(&this->base);
750 struct mtd_info *mtd = nand_to_mtd(this);
751 struct erase_info einfo;
752 int i, res, chip = 0;
753 int bits, page, offs, numblocks, sft, sftmsk;
754 int nrchips, pageoffs, ooboffs;
755 uint8_t msk[4];
756 uint8_t rcode = td->reserved_block_code;
757 size_t retlen, len = 0;
758 loff_t to;
759 struct mtd_oob_ops ops;
760
761 ops.ooblen = mtd->oobsize;
762 ops.ooboffs = 0;
763 ops.datbuf = NULL;
764 ops.mode = MTD_OPS_PLACE_OOB;
765
766 if (!rcode)
767 rcode = 0xff;
768
769 if (td->options & NAND_BBT_PERCHIP) {
770 numblocks = (int)(targetsize >> this->bbt_erase_shift);
771
772 if (chipsel == -1) {
773 nrchips = nanddev_ntargets(&this->base);
774 } else {
775 nrchips = chipsel + 1;
776 chip = chipsel;
777 }
778 } else {
779 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
780 nrchips = 1;
781 }
782
783
784 while (chip < nrchips) {
785 int block;
786
787 block = get_bbt_block(this, td, md, chip);
788 if (block < 0) {
789 pr_err("No space left to write bad block table\n");
790 res = block;
791 goto outerr;
792 }
793
794
795
796
797
798 page = block << (this->bbt_erase_shift - this->page_shift);
799
800
801 bits = td->options & NAND_BBT_NRBITS_MSK;
802 msk[2] = ~rcode;
803 switch (bits) {
804 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
805 msk[3] = 0x01;
806 break;
807 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
808 msk[3] = 0x03;
809 break;
810 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
811 msk[3] = 0x0f;
812 break;
813 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
814 msk[3] = 0xff;
815 break;
816 default: return -EINVAL;
817 }
818
819 to = ((loff_t)page) << this->page_shift;
820
821
822 if (td->options & NAND_BBT_SAVECONTENT) {
823
824 to &= ~(((loff_t)1 << this->bbt_erase_shift) - 1);
825 len = 1 << this->bbt_erase_shift;
826 res = mtd_read(mtd, to, len, &retlen, buf);
827 if (res < 0) {
828 if (retlen != len) {
829 pr_info("nand_bbt: error reading block for writing the bad block table\n");
830 return res;
831 }
832 pr_warn("nand_bbt: ECC error while reading block for writing bad block table\n");
833 }
834
835 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
836 ops.oobbuf = &buf[len];
837 res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
838 if (res < 0 || ops.oobretlen != ops.ooblen)
839 goto outerr;
840
841
842 pageoffs = page - (int)(to >> this->page_shift);
843 offs = pageoffs << this->page_shift;
844
845 memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
846 ooboffs = len + (pageoffs * mtd->oobsize);
847
848 } else if (td->options & NAND_BBT_NO_OOB) {
849 ooboffs = 0;
850 offs = td->len;
851
852 if (td->options & NAND_BBT_VERSION)
853 offs++;
854
855 len = (size_t)(numblocks >> sft);
856 len += offs;
857
858 len = ALIGN(len, mtd->writesize);
859
860 memset(buf, 0xff, len);
861
862 memcpy(buf, td->pattern, td->len);
863 } else {
864
865 len = (size_t)(numblocks >> sft);
866
867 len = ALIGN(len, mtd->writesize);
868
869 memset(buf, 0xff, len +
870 (len >> this->page_shift)* mtd->oobsize);
871 offs = 0;
872 ooboffs = len;
873
874 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
875 }
876
877 if (td->options & NAND_BBT_VERSION)
878 buf[ooboffs + td->veroffs] = td->version[chip];
879
880
881 for (i = 0; i < numblocks; i++) {
882 uint8_t dat;
883 int sftcnt = (i << (3 - sft)) & sftmsk;
884 dat = bbt_get_entry(this, chip * numblocks + i);
885
886 buf[offs + (i >> sft)] &= ~(msk[dat] << sftcnt);
887 }
888
889 memset(&einfo, 0, sizeof(einfo));
890 einfo.addr = to;
891 einfo.len = 1 << this->bbt_erase_shift;
892 res = nand_erase_nand(this, &einfo, 1);
893 if (res < 0) {
894 pr_warn("nand_bbt: error while erasing BBT block %d\n",
895 res);
896 mark_bbt_block_bad(this, td, chip, block);
897 continue;
898 }
899
900 res = scan_write_bbt(this, to, len, buf,
901 td->options & NAND_BBT_NO_OOB ?
902 NULL : &buf[len]);
903 if (res < 0) {
904 pr_warn("nand_bbt: error while writing BBT block %d\n",
905 res);
906 mark_bbt_block_bad(this, td, chip, block);
907 continue;
908 }
909
910 pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
911 (unsigned long long)to, td->version[chip]);
912
913
914 td->pages[chip++] = page;
915 }
916 return 0;
917
918 outerr:
919 pr_warn("nand_bbt: error while writing bad block table %d\n", res);
920 return res;
921}
922
923
924
925
926
927
928
929
930
931static inline int nand_memory_bbt(struct nand_chip *this,
932 struct nand_bbt_descr *bd)
933{
934 u8 *pagebuf = nand_get_data_buf(this);
935
936 return create_bbt(this, pagebuf, bd, -1);
937}
938
939
940
941
942
943
944
945
946
947
948
949
950static int check_create(struct nand_chip *this, uint8_t *buf,
951 struct nand_bbt_descr *bd)
952{
953 int i, chips, writeops, create, chipsel, res, res2;
954 struct nand_bbt_descr *td = this->bbt_td;
955 struct nand_bbt_descr *md = this->bbt_md;
956 struct nand_bbt_descr *rd, *rd2;
957
958
959 if (td->options & NAND_BBT_PERCHIP)
960 chips = nanddev_ntargets(&this->base);
961 else
962 chips = 1;
963
964 for (i = 0; i < chips; i++) {
965 writeops = 0;
966 create = 0;
967 rd = NULL;
968 rd2 = NULL;
969 res = res2 = 0;
970
971 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
972
973 if (md) {
974 if (td->pages[i] == -1 && md->pages[i] == -1) {
975 create = 1;
976 writeops = 0x03;
977 } else if (td->pages[i] == -1) {
978 rd = md;
979 writeops = 0x01;
980 } else if (md->pages[i] == -1) {
981 rd = td;
982 writeops = 0x02;
983 } else if (td->version[i] == md->version[i]) {
984 rd = td;
985 if (!(td->options & NAND_BBT_VERSION))
986 rd2 = md;
987 } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
988 rd = td;
989 writeops = 0x02;
990 } else {
991 rd = md;
992 writeops = 0x01;
993 }
994 } else {
995 if (td->pages[i] == -1) {
996 create = 1;
997 writeops = 0x01;
998 } else {
999 rd = td;
1000 }
1001 }
1002
1003 if (create) {
1004
1005 if (!(td->options & NAND_BBT_CREATE))
1006 continue;
1007
1008
1009 if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
1010 create_bbt(this, buf, bd, chipsel);
1011
1012 td->version[i] = 1;
1013 if (md)
1014 md->version[i] = 1;
1015 }
1016
1017
1018 if (rd) {
1019 res = read_abs_bbt(this, buf, rd, chipsel);
1020 if (mtd_is_eccerr(res)) {
1021
1022 rd->pages[i] = -1;
1023 rd->version[i] = 0;
1024 i--;
1025 continue;
1026 }
1027 }
1028
1029 if (rd2) {
1030 res2 = read_abs_bbt(this, buf, rd2, chipsel);
1031 if (mtd_is_eccerr(res2)) {
1032
1033 rd2->pages[i] = -1;
1034 rd2->version[i] = 0;
1035 i--;
1036 continue;
1037 }
1038 }
1039
1040
1041 if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
1042 writeops = 0x03;
1043
1044
1045 if (md) {
1046 td->version[i] = max(td->version[i], md->version[i]);
1047 md->version[i] = td->version[i];
1048 }
1049
1050
1051 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
1052 res = write_bbt(this, buf, td, md, chipsel);
1053 if (res < 0)
1054 return res;
1055 }
1056
1057
1058 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
1059 res = write_bbt(this, buf, md, td, chipsel);
1060 if (res < 0)
1061 return res;
1062 }
1063 }
1064 return 0;
1065}
1066
1067
1068
1069
1070
1071
1072
1073
1074static int nand_update_bbt(struct nand_chip *this, loff_t offs)
1075{
1076 struct mtd_info *mtd = nand_to_mtd(this);
1077 int len, res = 0;
1078 int chip, chipsel;
1079 uint8_t *buf;
1080 struct nand_bbt_descr *td = this->bbt_td;
1081 struct nand_bbt_descr *md = this->bbt_md;
1082
1083 if (!this->bbt || !td)
1084 return -EINVAL;
1085
1086
1087 len = (1 << this->bbt_erase_shift);
1088 len += (len >> this->page_shift) * mtd->oobsize;
1089 buf = kmalloc(len, GFP_KERNEL);
1090 if (!buf)
1091 return -ENOMEM;
1092
1093
1094 if (td->options & NAND_BBT_PERCHIP) {
1095 chip = (int)(offs >> this->chip_shift);
1096 chipsel = chip;
1097 } else {
1098 chip = 0;
1099 chipsel = -1;
1100 }
1101
1102 td->version[chip]++;
1103 if (md)
1104 md->version[chip]++;
1105
1106
1107 if (td->options & NAND_BBT_WRITE) {
1108 res = write_bbt(this, buf, td, md, chipsel);
1109 if (res < 0)
1110 goto out;
1111 }
1112
1113 if (md && (md->options & NAND_BBT_WRITE)) {
1114 res = write_bbt(this, buf, md, td, chipsel);
1115 }
1116
1117 out:
1118 kfree(buf);
1119 return res;
1120}
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130static void mark_bbt_region(struct nand_chip *this, struct nand_bbt_descr *td)
1131{
1132 u64 targetsize = nanddev_target_size(&this->base);
1133 struct mtd_info *mtd = nand_to_mtd(this);
1134 int i, j, chips, block, nrblocks, update;
1135 uint8_t oldval;
1136
1137
1138 if (td->options & NAND_BBT_PERCHIP) {
1139 chips = nanddev_ntargets(&this->base);
1140 nrblocks = (int)(targetsize >> this->bbt_erase_shift);
1141 } else {
1142 chips = 1;
1143 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
1144 }
1145
1146 for (i = 0; i < chips; i++) {
1147 if ((td->options & NAND_BBT_ABSPAGE) ||
1148 !(td->options & NAND_BBT_WRITE)) {
1149 if (td->pages[i] == -1)
1150 continue;
1151 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
1152 oldval = bbt_get_entry(this, block);
1153 bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
1154 if ((oldval != BBT_BLOCK_RESERVED) &&
1155 td->reserved_block_code)
1156 nand_update_bbt(this, (loff_t)block <<
1157 this->bbt_erase_shift);
1158 continue;
1159 }
1160 update = 0;
1161 if (td->options & NAND_BBT_LASTBLOCK)
1162 block = ((i + 1) * nrblocks) - td->maxblocks;
1163 else
1164 block = i * nrblocks;
1165 for (j = 0; j < td->maxblocks; j++) {
1166 oldval = bbt_get_entry(this, block);
1167 bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
1168 if (oldval != BBT_BLOCK_RESERVED)
1169 update = 1;
1170 block++;
1171 }
1172
1173
1174
1175
1176
1177 if (update && td->reserved_block_code)
1178 nand_update_bbt(this, (loff_t)(block - 1) <<
1179 this->bbt_erase_shift);
1180 }
1181}
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191static void verify_bbt_descr(struct nand_chip *this, struct nand_bbt_descr *bd)
1192{
1193 u64 targetsize = nanddev_target_size(&this->base);
1194 struct mtd_info *mtd = nand_to_mtd(this);
1195 u32 pattern_len;
1196 u32 bits;
1197 u32 table_size;
1198
1199 if (!bd)
1200 return;
1201
1202 pattern_len = bd->len;
1203 bits = bd->options & NAND_BBT_NRBITS_MSK;
1204
1205 BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
1206 !(this->bbt_options & NAND_BBT_USE_FLASH));
1207 BUG_ON(!bits);
1208
1209 if (bd->options & NAND_BBT_VERSION)
1210 pattern_len++;
1211
1212 if (bd->options & NAND_BBT_NO_OOB) {
1213 BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
1214 BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
1215 BUG_ON(bd->offs);
1216 if (bd->options & NAND_BBT_VERSION)
1217 BUG_ON(bd->veroffs != bd->len);
1218 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1219 }
1220
1221 if (bd->options & NAND_BBT_PERCHIP)
1222 table_size = targetsize >> this->bbt_erase_shift;
1223 else
1224 table_size = mtd->size >> this->bbt_erase_shift;
1225 table_size >>= 3;
1226 table_size *= bits;
1227 if (bd->options & NAND_BBT_NO_OOB)
1228 table_size += pattern_len;
1229 BUG_ON(table_size > (1 << this->bbt_erase_shift));
1230}
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244static int nand_scan_bbt(struct nand_chip *this, struct nand_bbt_descr *bd)
1245{
1246 struct mtd_info *mtd = nand_to_mtd(this);
1247 int len, res;
1248 uint8_t *buf;
1249 struct nand_bbt_descr *td = this->bbt_td;
1250 struct nand_bbt_descr *md = this->bbt_md;
1251
1252 len = (mtd->size >> (this->bbt_erase_shift + 2)) ? : 1;
1253
1254
1255
1256
1257 this->bbt = kzalloc(len, GFP_KERNEL);
1258 if (!this->bbt)
1259 return -ENOMEM;
1260
1261
1262
1263
1264
1265 if (!td) {
1266 if ((res = nand_memory_bbt(this, bd))) {
1267 pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
1268 goto err_free_bbt;
1269 }
1270 return 0;
1271 }
1272 verify_bbt_descr(this, td);
1273 verify_bbt_descr(this, md);
1274
1275
1276 len = (1 << this->bbt_erase_shift);
1277 len += (len >> this->page_shift) * mtd->oobsize;
1278 buf = vmalloc(len);
1279 if (!buf) {
1280 res = -ENOMEM;
1281 goto err_free_bbt;
1282 }
1283
1284
1285 if (td->options & NAND_BBT_ABSPAGE) {
1286 read_abs_bbts(this, buf, td, md);
1287 } else {
1288
1289 search_read_bbts(this, buf, td, md);
1290 }
1291
1292 res = check_create(this, buf, bd);
1293 if (res)
1294 goto err_free_buf;
1295
1296
1297 mark_bbt_region(this, td);
1298 if (md)
1299 mark_bbt_region(this, md);
1300
1301 vfree(buf);
1302 return 0;
1303
1304err_free_buf:
1305 vfree(buf);
1306err_free_bbt:
1307 kfree(this->bbt);
1308 this->bbt = NULL;
1309 return res;
1310}
1311
1312
1313
1314
1315
1316static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1317
1318
1319static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1320static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1321
1322static struct nand_bbt_descr bbt_main_descr = {
1323 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1324 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1325 .offs = 8,
1326 .len = 4,
1327 .veroffs = 12,
1328 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1329 .pattern = bbt_pattern
1330};
1331
1332static struct nand_bbt_descr bbt_mirror_descr = {
1333 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1334 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1335 .offs = 8,
1336 .len = 4,
1337 .veroffs = 12,
1338 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1339 .pattern = mirror_pattern
1340};
1341
1342static struct nand_bbt_descr bbt_main_no_oob_descr = {
1343 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1344 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1345 | NAND_BBT_NO_OOB,
1346 .len = 4,
1347 .veroffs = 4,
1348 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1349 .pattern = bbt_pattern
1350};
1351
1352static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
1353 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1354 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1355 | NAND_BBT_NO_OOB,
1356 .len = 4,
1357 .veroffs = 4,
1358 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1359 .pattern = mirror_pattern
1360};
1361
1362#define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372static int nand_create_badblock_pattern(struct nand_chip *this)
1373{
1374 struct nand_bbt_descr *bd;
1375 if (this->badblock_pattern) {
1376 pr_warn("Bad block pattern already allocated; not replacing\n");
1377 return -EINVAL;
1378 }
1379 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
1380 if (!bd)
1381 return -ENOMEM;
1382 bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
1383 bd->offs = this->badblockpos;
1384 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1385 bd->pattern = scan_ff_pattern;
1386 bd->options |= NAND_BBT_DYNAMICSTRUCT;
1387 this->badblock_pattern = bd;
1388 return 0;
1389}
1390
1391
1392
1393
1394
1395
1396
1397
1398int nand_create_bbt(struct nand_chip *this)
1399{
1400 int ret;
1401
1402
1403 if (this->bbt_options & NAND_BBT_USE_FLASH) {
1404
1405 if (!this->bbt_td) {
1406 if (this->bbt_options & NAND_BBT_NO_OOB) {
1407 this->bbt_td = &bbt_main_no_oob_descr;
1408 this->bbt_md = &bbt_mirror_no_oob_descr;
1409 } else {
1410 this->bbt_td = &bbt_main_descr;
1411 this->bbt_md = &bbt_mirror_descr;
1412 }
1413 }
1414 } else {
1415 this->bbt_td = NULL;
1416 this->bbt_md = NULL;
1417 }
1418
1419 if (!this->badblock_pattern) {
1420 ret = nand_create_badblock_pattern(this);
1421 if (ret)
1422 return ret;
1423 }
1424
1425 return nand_scan_bbt(this, this->badblock_pattern);
1426}
1427EXPORT_SYMBOL(nand_create_bbt);
1428
1429
1430
1431
1432
1433
1434int nand_isreserved_bbt(struct nand_chip *this, loff_t offs)
1435{
1436 int block;
1437
1438 block = (int)(offs >> this->bbt_erase_shift);
1439 return bbt_get_entry(this, block) == BBT_BLOCK_RESERVED;
1440}
1441
1442
1443
1444
1445
1446
1447
1448int nand_isbad_bbt(struct nand_chip *this, loff_t offs, int allowbbt)
1449{
1450 int block, res;
1451
1452 block = (int)(offs >> this->bbt_erase_shift);
1453 res = bbt_get_entry(this, block);
1454
1455 pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1456 (unsigned int)offs, block, res);
1457
1458 switch (res) {
1459 case BBT_BLOCK_GOOD:
1460 return 0;
1461 case BBT_BLOCK_WORN:
1462 return 1;
1463 case BBT_BLOCK_RESERVED:
1464 return allowbbt ? 0 : 1;
1465 }
1466 return 1;
1467}
1468
1469
1470
1471
1472
1473
1474int nand_markbad_bbt(struct nand_chip *this, loff_t offs)
1475{
1476 int block, ret = 0;
1477
1478 block = (int)(offs >> this->bbt_erase_shift);
1479
1480
1481 bbt_mark_entry(this, block, BBT_BLOCK_WORN);
1482
1483
1484 if (this->bbt_options & NAND_BBT_USE_FLASH)
1485 ret = nand_update_bbt(this, offs);
1486
1487 return ret;
1488}
1489