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