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