mtd: nand: rename NAND_USE_FLASH_BBT
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mtd / nand / nand_bbt.c
blobdfea9fd1d61cb04cca6a08bbacc45de052dbcf94
1 /*
2 * drivers/mtd/nand_bbt.c
4 * Overview:
5 * Bad block table support for the NAND driver
7 * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * Description:
15 * When nand_scan_bbt is called, then it tries to find the bad block table
16 * depending on the options in the BBT descriptor(s). If no flash based BBT
17 * (NAND_BBT_USE_FLASH) is specified then the device is scanned for factory
18 * marked good / bad blocks. This information is used to create a memory BBT.
19 * Once a new bad block is discovered then the "factory" information is updated
20 * on the device.
21 * If a flash based BBT is specified then the function first tries to find the
22 * BBT on flash. If a BBT is found then the contents are read and the memory
23 * based BBT is created. If a mirrored BBT is selected then the mirror is
24 * searched too and the versions are compared. If the mirror has a greater
25 * version number than the mirror BBT is used to build the memory based BBT.
26 * If the tables are not versioned, then we "or" the bad block information.
27 * If one of the BBTs is out of date or does not exist it is (re)created.
28 * If no BBT exists at all then the device is scanned for factory marked
29 * good / bad blocks and the bad block tables are created.
31 * For manufacturer created BBTs like the one found on M-SYS DOC devices
32 * the BBT is searched and read but never created
34 * The auto generated bad block table is located in the last good blocks
35 * of the device. The table is mirrored, so it can be updated eventually.
36 * The table is marked in the OOB area with an ident pattern and a version
37 * number which indicates which of both tables is more up to date. If the NAND
38 * controller needs the complete OOB area for the ECC information then the
39 * option NAND_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of
40 * course): it moves the ident pattern and the version byte into the data area
41 * and the OOB area will remain untouched.
43 * The table uses 2 bits per block
44 * 11b: block is good
45 * 00b: block is factory marked bad
46 * 01b, 10b: block is marked bad due to wear
48 * The memory bad block table uses the following scheme:
49 * 00b: block is good
50 * 01b: block is marked bad due to wear
51 * 10b: block is reserved (to protect the bbt area)
52 * 11b: block is factory marked bad
54 * Multichip devices like DOC store the bad block info per floor.
56 * Following assumptions are made:
57 * - bbts start at a page boundary, if autolocated on a block boundary
58 * - the space necessary for a bbt in FLASH does not exceed a block boundary
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>
71 static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
73 int ret;
75 ret = memcmp(buf, td->pattern, td->len);
76 if (!ret)
77 return ret;
78 return -1;
81 /**
82 * check_pattern - [GENERIC] check if a pattern is in the buffer
83 * @buf: the buffer to search
84 * @len: the length of buffer to search
85 * @paglen: the pagelength
86 * @td: search pattern descriptor
88 * Check for a pattern at the given place. Used to search bad block
89 * tables and good / bad block identifiers.
90 * If the SCAN_EMPTY option is set then check, if all bytes except the
91 * pattern area contain 0xff
94 static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
96 int i, end = 0;
97 uint8_t *p = buf;
99 if (td->options & NAND_BBT_NO_OOB)
100 return check_pattern_no_oob(buf, td);
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;
109 p += end;
111 /* Compare the pattern */
112 for (i = 0; i < td->len; i++) {
113 if (p[i] != td->pattern[i])
114 return -1;
117 if (td->options & NAND_BBT_SCANEMPTY) {
118 p += td->len;
119 end += td->len;
120 for (i = end; i < len; i++) {
121 if (*p++ != 0xff)
122 return -1;
125 return 0;
129 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
130 * @buf: the buffer to search
131 * @td: search pattern descriptor
133 * Check for a pattern at the given place. Used to search bad block
134 * tables and good / bad block identifiers. Same as check_pattern, but
135 * no optional empty check
138 static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
140 int i;
141 uint8_t *p = buf;
143 /* Compare the pattern */
144 for (i = 0; i < td->len; i++) {
145 if (p[td->offs + i] != td->pattern[i])
146 return -1;
148 return 0;
152 * add_marker_len - compute the length of the marker in data area
153 * @td: BBT descriptor used for computation
155 * The length will be 0 if the markeris located in OOB area.
157 static u32 add_marker_len(struct nand_bbt_descr *td)
159 u32 len;
161 if (!(td->options & NAND_BBT_NO_OOB))
162 return 0;
164 len = td->len;
165 if (td->options & NAND_BBT_VERSION)
166 len++;
167 return len;
171 * read_bbt - [GENERIC] Read the bad block table starting from page
172 * @mtd: MTD device structure
173 * @buf: temporary buffer
174 * @page: the starting page
175 * @num: the number of bbt descriptors to read
176 * @td: the bbt describtion table
177 * @offs: offset in the memory table
179 * Read the bad block table starting from page.
182 static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
183 struct nand_bbt_descr *td, int offs)
185 int res, i, j, act = 0;
186 struct nand_chip *this = mtd->priv;
187 size_t retlen, len, totlen;
188 loff_t from;
189 int bits = td->options & NAND_BBT_NRBITS_MSK;
190 uint8_t msk = (uint8_t) ((1 << bits) - 1);
191 u32 marker_len;
192 int reserved_block_code = td->reserved_block_code;
194 totlen = (num * bits) >> 3;
195 marker_len = add_marker_len(td);
196 from = ((loff_t) page) << this->page_shift;
198 while (totlen) {
199 len = min(totlen, (size_t) (1 << this->bbt_erase_shift));
200 if (marker_len) {
202 * In case the BBT marker is not in the OOB area it
203 * will be just in the first page.
205 len -= marker_len;
206 from += marker_len;
207 marker_len = 0;
209 res = mtd->read(mtd, from, len, &retlen, buf);
210 if (res < 0) {
211 if (retlen != len) {
212 printk(KERN_INFO "nand_bbt: Error reading bad block table\n");
213 return res;
215 printk(KERN_WARNING "nand_bbt: ECC error while reading bad block table\n");
218 /* Analyse data */
219 for (i = 0; i < len; i++) {
220 uint8_t dat = buf[i];
221 for (j = 0; j < 8; j += bits, act += 2) {
222 uint8_t tmp = (dat >> j) & msk;
223 if (tmp == msk)
224 continue;
225 if (reserved_block_code && (tmp == reserved_block_code)) {
226 printk(KERN_DEBUG "nand_read_bbt: Reserved block at 0x%012llx\n",
227 (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
228 this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
229 mtd->ecc_stats.bbtblocks++;
230 continue;
232 /* Leave it for now, if its matured we can move this
233 * message to MTD_DEBUG_LEVEL0 */
234 printk(KERN_DEBUG "nand_read_bbt: Bad block at 0x%012llx\n",
235 (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
236 /* Factory marked bad or worn out ? */
237 if (tmp == 0)
238 this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
239 else
240 this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
241 mtd->ecc_stats.badblocks++;
244 totlen -= len;
245 from += len;
247 return 0;
251 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
252 * @mtd: MTD device structure
253 * @buf: temporary buffer
254 * @td: descriptor for the bad block table
255 * @chip: read the table for a specific chip, -1 read all chips.
256 * Applies only if NAND_BBT_PERCHIP option is set
258 * Read the bad block table for all chips starting at a given page
259 * We assume that the bbt bits are in consecutive order.
261 static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
263 struct nand_chip *this = mtd->priv;
264 int res = 0, i;
266 if (td->options & NAND_BBT_PERCHIP) {
267 int offs = 0;
268 for (i = 0; i < this->numchips; i++) {
269 if (chip == -1 || chip == i)
270 res = read_bbt(mtd, buf, td->pages[i],
271 this->chipsize >> this->bbt_erase_shift,
272 td, offs);
273 if (res)
274 return res;
275 offs += this->chipsize >> (this->bbt_erase_shift + 2);
277 } else {
278 res = read_bbt(mtd, buf, td->pages[0],
279 mtd->size >> this->bbt_erase_shift, td, 0);
280 if (res)
281 return res;
283 return 0;
287 * BBT marker is in the first page, no OOB.
289 static int scan_read_raw_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
290 struct nand_bbt_descr *td)
292 size_t retlen;
293 size_t len;
295 len = td->len;
296 if (td->options & NAND_BBT_VERSION)
297 len++;
299 return mtd->read(mtd, offs, len, &retlen, buf);
303 * Scan read raw data from flash
305 static int scan_read_raw_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
306 size_t len)
308 struct mtd_oob_ops ops;
309 int res;
311 ops.mode = MTD_OOB_RAW;
312 ops.ooboffs = 0;
313 ops.ooblen = mtd->oobsize;
316 while (len > 0) {
317 if (len <= mtd->writesize) {
318 ops.oobbuf = buf + len;
319 ops.datbuf = buf;
320 ops.len = len;
321 return mtd->read_oob(mtd, offs, &ops);
322 } else {
323 ops.oobbuf = buf + mtd->writesize;
324 ops.datbuf = buf;
325 ops.len = mtd->writesize;
326 res = mtd->read_oob(mtd, offs, &ops);
328 if (res)
329 return res;
332 buf += mtd->oobsize + mtd->writesize;
333 len -= mtd->writesize;
335 return 0;
338 static int scan_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
339 size_t len, struct nand_bbt_descr *td)
341 if (td->options & NAND_BBT_NO_OOB)
342 return scan_read_raw_data(mtd, buf, offs, td);
343 else
344 return scan_read_raw_oob(mtd, buf, offs, len);
348 * Scan write data with oob to flash
350 static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
351 uint8_t *buf, uint8_t *oob)
353 struct mtd_oob_ops ops;
355 ops.mode = MTD_OOB_PLACE;
356 ops.ooboffs = 0;
357 ops.ooblen = mtd->oobsize;
358 ops.datbuf = buf;
359 ops.oobbuf = oob;
360 ops.len = len;
362 return mtd->write_oob(mtd, offs, &ops);
365 static u32 bbt_get_ver_offs(struct mtd_info *mtd, struct nand_bbt_descr *td)
367 u32 ver_offs = td->veroffs;
369 if (!(td->options & NAND_BBT_NO_OOB))
370 ver_offs += mtd->writesize;
371 return ver_offs;
375 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
376 * @mtd: MTD device structure
377 * @buf: temporary buffer
378 * @td: descriptor for the bad block table
379 * @md: descriptor for the bad block table mirror
381 * Read the bad block table(s) for all chips starting at a given page
382 * We assume that the bbt bits are in consecutive order.
385 static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
386 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
388 struct nand_chip *this = mtd->priv;
390 /* Read the primary version, if available */
391 if (td->options & NAND_BBT_VERSION) {
392 scan_read_raw(mtd, buf, (loff_t)td->pages[0] << this->page_shift,
393 mtd->writesize, td);
394 td->version[0] = buf[bbt_get_ver_offs(mtd, td)];
395 printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
396 td->pages[0], td->version[0]);
399 /* Read the mirror version, if available */
400 if (md && (md->options & NAND_BBT_VERSION)) {
401 scan_read_raw(mtd, buf, (loff_t)md->pages[0] << this->page_shift,
402 mtd->writesize, td);
403 md->version[0] = buf[bbt_get_ver_offs(mtd, md)];
404 printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
405 md->pages[0], md->version[0]);
407 return 1;
411 * Scan a given block full
413 static int scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
414 loff_t offs, uint8_t *buf, size_t readlen,
415 int scanlen, int len)
417 int ret, j;
419 ret = scan_read_raw_oob(mtd, buf, offs, readlen);
420 if (ret)
421 return ret;
423 for (j = 0; j < len; j++, buf += scanlen) {
424 if (check_pattern(buf, scanlen, mtd->writesize, bd))
425 return 1;
427 return 0;
431 * Scan a given block partially
433 static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
434 loff_t offs, uint8_t *buf, int len)
436 struct mtd_oob_ops ops;
437 int j, ret;
439 ops.ooblen = mtd->oobsize;
440 ops.oobbuf = buf;
441 ops.ooboffs = 0;
442 ops.datbuf = NULL;
443 ops.mode = MTD_OOB_PLACE;
445 for (j = 0; j < len; j++) {
447 * Read the full oob until read_oob is fixed to
448 * handle single byte reads for 16 bit
449 * buswidth
451 ret = mtd->read_oob(mtd, offs, &ops);
452 if (ret)
453 return ret;
455 if (check_short_pattern(buf, bd))
456 return 1;
458 offs += mtd->writesize;
460 return 0;
464 * create_bbt - [GENERIC] Create a bad block table by scanning the device
465 * @mtd: MTD device structure
466 * @buf: temporary buffer
467 * @bd: descriptor for the good/bad block search pattern
468 * @chip: create the table for a specific chip, -1 read all chips.
469 * Applies only if NAND_BBT_PERCHIP option is set
471 * Create a bad block table by scanning the device
472 * for the given good/bad block identify pattern
474 static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
475 struct nand_bbt_descr *bd, int chip)
477 struct nand_chip *this = mtd->priv;
478 int i, numblocks, len, scanlen;
479 int startblock;
480 loff_t from;
481 size_t readlen;
483 printk(KERN_INFO "Scanning device for bad blocks\n");
485 if (bd->options & NAND_BBT_SCANALLPAGES)
486 len = 1 << (this->bbt_erase_shift - this->page_shift);
487 else if (bd->options & NAND_BBT_SCAN2NDPAGE)
488 len = 2;
489 else
490 len = 1;
492 if (!(bd->options & NAND_BBT_SCANEMPTY)) {
493 /* We need only read few bytes from the OOB area */
494 scanlen = 0;
495 readlen = bd->len;
496 } else {
497 /* Full page content should be read */
498 scanlen = mtd->writesize + mtd->oobsize;
499 readlen = len * mtd->writesize;
502 if (chip == -1) {
503 /* Note that numblocks is 2 * (real numblocks) here, see i+=2
504 * below as it makes shifting and masking less painful */
505 numblocks = mtd->size >> (this->bbt_erase_shift - 1);
506 startblock = 0;
507 from = 0;
508 } else {
509 if (chip >= this->numchips) {
510 printk(KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n",
511 chip + 1, this->numchips);
512 return -EINVAL;
514 numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
515 startblock = chip * numblocks;
516 numblocks += startblock;
517 from = (loff_t)startblock << (this->bbt_erase_shift - 1);
520 if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
521 from += mtd->erasesize - (mtd->writesize * len);
523 for (i = startblock; i < numblocks;) {
524 int ret;
526 BUG_ON(bd->options & NAND_BBT_NO_OOB);
528 if (bd->options & NAND_BBT_SCANALLPAGES)
529 ret = scan_block_full(mtd, bd, from, buf, readlen,
530 scanlen, len);
531 else
532 ret = scan_block_fast(mtd, bd, from, buf, len);
534 if (ret < 0)
535 return ret;
537 if (ret) {
538 this->bbt[i >> 3] |= 0x03 << (i & 0x6);
539 printk(KERN_WARNING "Bad eraseblock %d at 0x%012llx\n",
540 i >> 1, (unsigned long long)from);
541 mtd->ecc_stats.badblocks++;
544 i += 2;
545 from += (1 << this->bbt_erase_shift);
547 return 0;
551 * search_bbt - [GENERIC] scan the device for a specific bad block table
552 * @mtd: MTD device structure
553 * @buf: temporary buffer
554 * @td: descriptor for the bad block table
556 * Read the bad block table by searching for a given ident pattern.
557 * Search is preformed either from the beginning up or from the end of
558 * the device downwards. The search starts always at the start of a
559 * block.
560 * If the option NAND_BBT_PERCHIP is given, each chip is searched
561 * for a bbt, which contains the bad block information of this chip.
562 * This is necessary to provide support for certain DOC devices.
564 * The bbt ident pattern resides in the oob area of the first page
565 * in a block.
567 static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
569 struct nand_chip *this = mtd->priv;
570 int i, chips;
571 int bits, startblock, block, dir;
572 int scanlen = mtd->writesize + mtd->oobsize;
573 int bbtblocks;
574 int blocktopage = this->bbt_erase_shift - this->page_shift;
576 /* Search direction top -> down ? */
577 if (td->options & NAND_BBT_LASTBLOCK) {
578 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
579 dir = -1;
580 } else {
581 startblock = 0;
582 dir = 1;
585 /* Do we have a bbt per chip ? */
586 if (td->options & NAND_BBT_PERCHIP) {
587 chips = this->numchips;
588 bbtblocks = this->chipsize >> this->bbt_erase_shift;
589 startblock &= bbtblocks - 1;
590 } else {
591 chips = 1;
592 bbtblocks = mtd->size >> this->bbt_erase_shift;
595 /* Number of bits for each erase block in the bbt */
596 bits = td->options & NAND_BBT_NRBITS_MSK;
598 for (i = 0; i < chips; i++) {
599 /* Reset version information */
600 td->version[i] = 0;
601 td->pages[i] = -1;
602 /* Scan the maximum number of blocks */
603 for (block = 0; block < td->maxblocks; block++) {
605 int actblock = startblock + dir * block;
606 loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
608 /* Read first page */
609 scan_read_raw(mtd, buf, offs, mtd->writesize, td);
610 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
611 td->pages[i] = actblock << blocktopage;
612 if (td->options & NAND_BBT_VERSION) {
613 offs = bbt_get_ver_offs(mtd, td);
614 td->version[i] = buf[offs];
616 break;
619 startblock += this->chipsize >> this->bbt_erase_shift;
621 /* Check, if we found a bbt for each requested chip */
622 for (i = 0; i < chips; i++) {
623 if (td->pages[i] == -1)
624 printk(KERN_WARNING "Bad block table not found for chip %d\n", i);
625 else
626 printk(KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n", td->pages[i],
627 td->version[i]);
629 return 0;
633 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
634 * @mtd: MTD device structure
635 * @buf: temporary buffer
636 * @td: descriptor for the bad block table
637 * @md: descriptor for the bad block table mirror
639 * Search and read the bad block table(s)
641 static int search_read_bbts(struct mtd_info *mtd, uint8_t * buf, struct nand_bbt_descr *td, struct nand_bbt_descr *md)
643 /* Search the primary table */
644 search_bbt(mtd, buf, td);
646 /* Search the mirror table */
647 if (md)
648 search_bbt(mtd, buf, md);
650 /* Force result check */
651 return 1;
655 * write_bbt - [GENERIC] (Re)write the bad block table
657 * @mtd: MTD device structure
658 * @buf: temporary buffer
659 * @td: descriptor for the bad block table
660 * @md: descriptor for the bad block table mirror
661 * @chipsel: selector for a specific chip, -1 for all
663 * (Re)write the bad block table
666 static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
667 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
668 int chipsel)
670 struct nand_chip *this = mtd->priv;
671 struct erase_info einfo;
672 int i, j, res, chip = 0;
673 int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
674 int nrchips, bbtoffs, pageoffs, ooboffs;
675 uint8_t msk[4];
676 uint8_t rcode = td->reserved_block_code;
677 size_t retlen, len = 0;
678 loff_t to;
679 struct mtd_oob_ops ops;
681 ops.ooblen = mtd->oobsize;
682 ops.ooboffs = 0;
683 ops.datbuf = NULL;
684 ops.mode = MTD_OOB_PLACE;
686 if (!rcode)
687 rcode = 0xff;
688 /* Write bad block table per chip rather than per device ? */
689 if (td->options & NAND_BBT_PERCHIP) {
690 numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
691 /* Full device write or specific chip ? */
692 if (chipsel == -1) {
693 nrchips = this->numchips;
694 } else {
695 nrchips = chipsel + 1;
696 chip = chipsel;
698 } else {
699 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
700 nrchips = 1;
703 /* Loop through the chips */
704 for (; chip < nrchips; chip++) {
706 /* There was already a version of the table, reuse the page
707 * This applies for absolute placement too, as we have the
708 * page nr. in td->pages.
710 if (td->pages[chip] != -1) {
711 page = td->pages[chip];
712 goto write;
715 /* Automatic placement of the bad block table */
716 /* Search direction top -> down ? */
717 if (td->options & NAND_BBT_LASTBLOCK) {
718 startblock = numblocks * (chip + 1) - 1;
719 dir = -1;
720 } else {
721 startblock = chip * numblocks;
722 dir = 1;
725 for (i = 0; i < td->maxblocks; i++) {
726 int block = startblock + dir * i;
727 /* Check, if the block is bad */
728 switch ((this->bbt[block >> 2] >>
729 (2 * (block & 0x03))) & 0x03) {
730 case 0x01:
731 case 0x03:
732 continue;
734 page = block <<
735 (this->bbt_erase_shift - this->page_shift);
736 /* Check, if the block is used by the mirror table */
737 if (!md || md->pages[chip] != page)
738 goto write;
740 printk(KERN_ERR "No space left to write bad block table\n");
741 return -ENOSPC;
742 write:
744 /* Set up shift count and masks for the flash table */
745 bits = td->options & NAND_BBT_NRBITS_MSK;
746 msk[2] = ~rcode;
747 switch (bits) {
748 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
749 msk[3] = 0x01;
750 break;
751 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
752 msk[3] = 0x03;
753 break;
754 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
755 msk[3] = 0x0f;
756 break;
757 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
758 msk[3] = 0xff;
759 break;
760 default: return -EINVAL;
763 bbtoffs = chip * (numblocks >> 2);
765 to = ((loff_t) page) << this->page_shift;
767 /* Must we save the block contents ? */
768 if (td->options & NAND_BBT_SAVECONTENT) {
769 /* Make it block aligned */
770 to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1));
771 len = 1 << this->bbt_erase_shift;
772 res = mtd->read(mtd, to, len, &retlen, buf);
773 if (res < 0) {
774 if (retlen != len) {
775 printk(KERN_INFO "nand_bbt: Error "
776 "reading block for writing "
777 "the bad block table\n");
778 return res;
780 printk(KERN_WARNING "nand_bbt: ECC error "
781 "while reading block for writing "
782 "bad block table\n");
784 /* Read oob data */
785 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
786 ops.oobbuf = &buf[len];
787 res = mtd->read_oob(mtd, to + mtd->writesize, &ops);
788 if (res < 0 || ops.oobretlen != ops.ooblen)
789 goto outerr;
791 /* Calc the byte offset in the buffer */
792 pageoffs = page - (int)(to >> this->page_shift);
793 offs = pageoffs << this->page_shift;
794 /* Preset the bbt area with 0xff */
795 memset(&buf[offs], 0xff, (size_t) (numblocks >> sft));
796 ooboffs = len + (pageoffs * mtd->oobsize);
798 } else if (td->options & NAND_BBT_NO_OOB) {
799 ooboffs = 0;
800 offs = td->len;
801 /* the version byte */
802 if (td->options & NAND_BBT_VERSION)
803 offs++;
804 /* Calc length */
805 len = (size_t) (numblocks >> sft);
806 len += offs;
807 /* Make it page aligned ! */
808 len = ALIGN(len, mtd->writesize);
809 /* Preset the buffer with 0xff */
810 memset(buf, 0xff, len);
811 /* Pattern is located at the begin of first page */
812 memcpy(buf, td->pattern, td->len);
813 } else {
814 /* Calc length */
815 len = (size_t) (numblocks >> sft);
816 /* Make it page aligned ! */
817 len = ALIGN(len, mtd->writesize);
818 /* Preset the buffer with 0xff */
819 memset(buf, 0xff, len +
820 (len >> this->page_shift)* mtd->oobsize);
821 offs = 0;
822 ooboffs = len;
823 /* Pattern is located in oob area of first page */
824 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
827 if (td->options & NAND_BBT_VERSION)
828 buf[ooboffs + td->veroffs] = td->version[chip];
830 /* walk through the memory table */
831 for (i = 0; i < numblocks;) {
832 uint8_t dat;
833 dat = this->bbt[bbtoffs + (i >> 2)];
834 for (j = 0; j < 4; j++, i++) {
835 int sftcnt = (i << (3 - sft)) & sftmsk;
836 /* Do not store the reserved bbt blocks ! */
837 buf[offs + (i >> sft)] &=
838 ~(msk[dat & 0x03] << sftcnt);
839 dat >>= 2;
843 memset(&einfo, 0, sizeof(einfo));
844 einfo.mtd = mtd;
845 einfo.addr = to;
846 einfo.len = 1 << this->bbt_erase_shift;
847 res = nand_erase_nand(mtd, &einfo, 1);
848 if (res < 0)
849 goto outerr;
851 res = scan_write_bbt(mtd, to, len, buf,
852 td->options & NAND_BBT_NO_OOB ? NULL :
853 &buf[len]);
854 if (res < 0)
855 goto outerr;
857 printk(KERN_DEBUG "Bad block table written to 0x%012llx, version "
858 "0x%02X\n", (unsigned long long)to, td->version[chip]);
860 /* Mark it as used */
861 td->pages[chip] = page;
863 return 0;
865 outerr:
866 printk(KERN_WARNING
867 "nand_bbt: Error while writing bad block table %d\n", res);
868 return res;
872 * nand_memory_bbt - [GENERIC] create a memory based bad block table
873 * @mtd: MTD device structure
874 * @bd: descriptor for the good/bad block search pattern
876 * The function creates a memory based bbt by scanning the device
877 * for manufacturer / software marked good / bad blocks
879 static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
881 struct nand_chip *this = mtd->priv;
883 bd->options &= ~NAND_BBT_SCANEMPTY;
884 return create_bbt(mtd, this->buffers->databuf, bd, -1);
888 * check_create - [GENERIC] create and write bbt(s) if necessary
889 * @mtd: MTD device structure
890 * @buf: temporary buffer
891 * @bd: descriptor for the good/bad block search pattern
893 * The function checks the results of the previous call to read_bbt
894 * and creates / updates the bbt(s) if necessary
895 * Creation is necessary if no bbt was found for the chip/device
896 * Update is necessary if one of the tables is missing or the
897 * version nr. of one table is less than the other
899 static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
901 int i, chips, writeops, chipsel, res;
902 struct nand_chip *this = mtd->priv;
903 struct nand_bbt_descr *td = this->bbt_td;
904 struct nand_bbt_descr *md = this->bbt_md;
905 struct nand_bbt_descr *rd, *rd2;
907 /* Do we have a bbt per chip ? */
908 if (td->options & NAND_BBT_PERCHIP)
909 chips = this->numchips;
910 else
911 chips = 1;
913 for (i = 0; i < chips; i++) {
914 writeops = 0;
915 rd = NULL;
916 rd2 = NULL;
917 /* Per chip or per device ? */
918 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
919 /* Mirrored table available ? */
920 if (md) {
921 if (td->pages[i] == -1 && md->pages[i] == -1) {
922 writeops = 0x03;
923 goto create;
926 if (td->pages[i] == -1) {
927 rd = md;
928 td->version[i] = md->version[i];
929 writeops = 1;
930 goto writecheck;
933 if (md->pages[i] == -1) {
934 rd = td;
935 md->version[i] = td->version[i];
936 writeops = 2;
937 goto writecheck;
940 if (td->version[i] == md->version[i]) {
941 rd = td;
942 if (!(td->options & NAND_BBT_VERSION))
943 rd2 = md;
944 goto writecheck;
947 if (((int8_t) (td->version[i] - md->version[i])) > 0) {
948 rd = td;
949 md->version[i] = td->version[i];
950 writeops = 2;
951 } else {
952 rd = md;
953 td->version[i] = md->version[i];
954 writeops = 1;
957 goto writecheck;
959 } else {
960 if (td->pages[i] == -1) {
961 writeops = 0x01;
962 goto create;
964 rd = td;
965 goto writecheck;
967 create:
968 /* Create the bad block table by scanning the device ? */
969 if (!(td->options & NAND_BBT_CREATE))
970 continue;
972 /* Create the table in memory by scanning the chip(s) */
973 if (!(this->options & NAND_CREATE_EMPTY_BBT))
974 create_bbt(mtd, buf, bd, chipsel);
976 td->version[i] = 1;
977 if (md)
978 md->version[i] = 1;
979 writecheck:
980 /* read back first ? */
981 if (rd)
982 read_abs_bbt(mtd, buf, rd, chipsel);
983 /* If they weren't versioned, read both. */
984 if (rd2)
985 read_abs_bbt(mtd, buf, rd2, chipsel);
987 /* Write the bad block table to the device ? */
988 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
989 res = write_bbt(mtd, buf, td, md, chipsel);
990 if (res < 0)
991 return res;
994 /* Write the mirror bad block table to the device ? */
995 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
996 res = write_bbt(mtd, buf, md, td, chipsel);
997 if (res < 0)
998 return res;
1001 return 0;
1005 * mark_bbt_regions - [GENERIC] mark the bad block table regions
1006 * @mtd: MTD device structure
1007 * @td: bad block table descriptor
1009 * The bad block table regions are marked as "bad" to prevent
1010 * accidental erasures / writes. The regions are identified by
1011 * the mark 0x02.
1013 static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
1015 struct nand_chip *this = mtd->priv;
1016 int i, j, chips, block, nrblocks, update;
1017 uint8_t oldval, newval;
1019 /* Do we have a bbt per chip ? */
1020 if (td->options & NAND_BBT_PERCHIP) {
1021 chips = this->numchips;
1022 nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
1023 } else {
1024 chips = 1;
1025 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
1028 for (i = 0; i < chips; i++) {
1029 if ((td->options & NAND_BBT_ABSPAGE) ||
1030 !(td->options & NAND_BBT_WRITE)) {
1031 if (td->pages[i] == -1)
1032 continue;
1033 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
1034 block <<= 1;
1035 oldval = this->bbt[(block >> 3)];
1036 newval = oldval | (0x2 << (block & 0x06));
1037 this->bbt[(block >> 3)] = newval;
1038 if ((oldval != newval) && td->reserved_block_code)
1039 nand_update_bbt(mtd, (loff_t)block << (this->bbt_erase_shift - 1));
1040 continue;
1042 update = 0;
1043 if (td->options & NAND_BBT_LASTBLOCK)
1044 block = ((i + 1) * nrblocks) - td->maxblocks;
1045 else
1046 block = i * nrblocks;
1047 block <<= 1;
1048 for (j = 0; j < td->maxblocks; j++) {
1049 oldval = this->bbt[(block >> 3)];
1050 newval = oldval | (0x2 << (block & 0x06));
1051 this->bbt[(block >> 3)] = newval;
1052 if (oldval != newval)
1053 update = 1;
1054 block += 2;
1056 /* If we want reserved blocks to be recorded to flash, and some
1057 new ones have been marked, then we need to update the stored
1058 bbts. This should only happen once. */
1059 if (update && td->reserved_block_code)
1060 nand_update_bbt(mtd, (loff_t)(block - 2) << (this->bbt_erase_shift - 1));
1065 * verify_bbt_descr - verify the bad block description
1066 * @mtd: MTD device structure
1067 * @bd: the table to verify
1069 * This functions performs a few sanity checks on the bad block description
1070 * table.
1072 static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
1074 struct nand_chip *this = mtd->priv;
1075 u32 pattern_len;
1076 u32 bits;
1077 u32 table_size;
1079 if (!bd)
1080 return;
1082 pattern_len = bd->len;
1083 bits = bd->options & NAND_BBT_NRBITS_MSK;
1085 BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
1086 !(this->bbt_options & NAND_BBT_USE_FLASH));
1087 BUG_ON(!bits);
1089 if (bd->options & NAND_BBT_VERSION)
1090 pattern_len++;
1092 if (bd->options & NAND_BBT_NO_OOB) {
1093 BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
1094 BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
1095 BUG_ON(bd->offs);
1096 if (bd->options & NAND_BBT_VERSION)
1097 BUG_ON(bd->veroffs != bd->len);
1098 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1101 if (bd->options & NAND_BBT_PERCHIP)
1102 table_size = this->chipsize >> this->bbt_erase_shift;
1103 else
1104 table_size = mtd->size >> this->bbt_erase_shift;
1105 table_size >>= 3;
1106 table_size *= bits;
1107 if (bd->options & NAND_BBT_NO_OOB)
1108 table_size += pattern_len;
1109 BUG_ON(table_size > (1 << this->bbt_erase_shift));
1113 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
1114 * @mtd: MTD device structure
1115 * @bd: descriptor for the good/bad block search pattern
1117 * The function checks, if a bad block table(s) is/are already
1118 * available. If not it scans the device for manufacturer
1119 * marked good / bad blocks and writes the bad block table(s) to
1120 * the selected place.
1122 * The bad block table memory is allocated here. It must be freed
1123 * by calling the nand_free_bbt function.
1126 int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
1128 struct nand_chip *this = mtd->priv;
1129 int len, res = 0;
1130 uint8_t *buf;
1131 struct nand_bbt_descr *td = this->bbt_td;
1132 struct nand_bbt_descr *md = this->bbt_md;
1134 len = mtd->size >> (this->bbt_erase_shift + 2);
1135 /* Allocate memory (2bit per block) and clear the memory bad block table */
1136 this->bbt = kzalloc(len, GFP_KERNEL);
1137 if (!this->bbt) {
1138 printk(KERN_ERR "nand_scan_bbt: Out of memory\n");
1139 return -ENOMEM;
1142 /* If no primary table decriptor is given, scan the device
1143 * to build a memory based bad block table
1145 if (!td) {
1146 if ((res = nand_memory_bbt(mtd, bd))) {
1147 printk(KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n");
1148 kfree(this->bbt);
1149 this->bbt = NULL;
1151 return res;
1153 verify_bbt_descr(mtd, td);
1154 verify_bbt_descr(mtd, md);
1156 /* Allocate a temporary buffer for one eraseblock incl. oob */
1157 len = (1 << this->bbt_erase_shift);
1158 len += (len >> this->page_shift) * mtd->oobsize;
1159 buf = vmalloc(len);
1160 if (!buf) {
1161 printk(KERN_ERR "nand_bbt: Out of memory\n");
1162 kfree(this->bbt);
1163 this->bbt = NULL;
1164 return -ENOMEM;
1167 /* Is the bbt at a given page ? */
1168 if (td->options & NAND_BBT_ABSPAGE) {
1169 res = read_abs_bbts(mtd, buf, td, md);
1170 } else {
1171 /* Search the bad block table using a pattern in oob */
1172 res = search_read_bbts(mtd, buf, td, md);
1175 if (res)
1176 res = check_create(mtd, buf, bd);
1178 /* Prevent the bbt regions from erasing / writing */
1179 mark_bbt_region(mtd, td);
1180 if (md)
1181 mark_bbt_region(mtd, md);
1183 vfree(buf);
1184 return res;
1188 * nand_update_bbt - [NAND Interface] update bad block table(s)
1189 * @mtd: MTD device structure
1190 * @offs: the offset of the newly marked block
1192 * The function updates the bad block table(s)
1194 int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
1196 struct nand_chip *this = mtd->priv;
1197 int len, res = 0, writeops = 0;
1198 int chip, chipsel;
1199 uint8_t *buf;
1200 struct nand_bbt_descr *td = this->bbt_td;
1201 struct nand_bbt_descr *md = this->bbt_md;
1203 if (!this->bbt || !td)
1204 return -EINVAL;
1206 /* Allocate a temporary buffer for one eraseblock incl. oob */
1207 len = (1 << this->bbt_erase_shift);
1208 len += (len >> this->page_shift) * mtd->oobsize;
1209 buf = kmalloc(len, GFP_KERNEL);
1210 if (!buf) {
1211 printk(KERN_ERR "nand_update_bbt: Out of memory\n");
1212 return -ENOMEM;
1215 writeops = md != NULL ? 0x03 : 0x01;
1217 /* Do we have a bbt per chip ? */
1218 if (td->options & NAND_BBT_PERCHIP) {
1219 chip = (int)(offs >> this->chip_shift);
1220 chipsel = chip;
1221 } else {
1222 chip = 0;
1223 chipsel = -1;
1226 td->version[chip]++;
1227 if (md)
1228 md->version[chip]++;
1230 /* Write the bad block table to the device ? */
1231 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
1232 res = write_bbt(mtd, buf, td, md, chipsel);
1233 if (res < 0)
1234 goto out;
1236 /* Write the mirror bad block table to the device ? */
1237 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
1238 res = write_bbt(mtd, buf, md, td, chipsel);
1241 out:
1242 kfree(buf);
1243 return res;
1246 /* Define some generic bad / good block scan pattern which are used
1247 * while scanning a device for factory marked good / bad blocks. */
1248 static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1250 static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
1252 static struct nand_bbt_descr agand_flashbased = {
1253 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
1254 .offs = 0x20,
1255 .len = 6,
1256 .pattern = scan_agand_pattern
1259 /* Generic flash bbt decriptors
1261 static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1262 static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1264 static struct nand_bbt_descr bbt_main_descr = {
1265 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1266 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1267 .offs = 8,
1268 .len = 4,
1269 .veroffs = 12,
1270 .maxblocks = 4,
1271 .pattern = bbt_pattern
1274 static struct nand_bbt_descr bbt_mirror_descr = {
1275 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1276 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1277 .offs = 8,
1278 .len = 4,
1279 .veroffs = 12,
1280 .maxblocks = 4,
1281 .pattern = mirror_pattern
1284 static struct nand_bbt_descr bbt_main_no_bbt_descr = {
1285 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1286 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1287 | NAND_BBT_NO_OOB,
1288 .len = 4,
1289 .veroffs = 4,
1290 .maxblocks = 4,
1291 .pattern = bbt_pattern
1294 static struct nand_bbt_descr bbt_mirror_no_bbt_descr = {
1295 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1296 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1297 | NAND_BBT_NO_OOB,
1298 .len = 4,
1299 .veroffs = 4,
1300 .maxblocks = 4,
1301 .pattern = mirror_pattern
1305 * nand_create_default_bbt_descr - [Internal] Creates a BBT descriptor structure
1306 * @this: NAND chip to create descriptor for
1308 * This function allocates and initializes a nand_bbt_descr for BBM detection
1309 * based on the properties of "this". The new descriptor is stored in
1310 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1311 * passed to this function.
1314 static int nand_create_default_bbt_descr(struct nand_chip *this)
1316 struct nand_bbt_descr *bd;
1317 if (this->badblock_pattern) {
1318 printk(KERN_WARNING "BBT descr already allocated; not replacing.\n");
1319 return -EINVAL;
1321 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
1322 if (!bd) {
1323 printk(KERN_ERR "nand_create_default_bbt_descr: Out of memory\n");
1324 return -ENOMEM;
1326 bd->options = this->bbt_options;
1327 bd->offs = this->badblockpos;
1328 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1329 bd->pattern = scan_ff_pattern;
1330 bd->options |= NAND_BBT_DYNAMICSTRUCT;
1331 this->badblock_pattern = bd;
1332 return 0;
1336 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
1337 * @mtd: MTD device structure
1339 * This function selects the default bad block table
1340 * support for the device and calls the nand_scan_bbt function
1343 int nand_default_bbt(struct mtd_info *mtd)
1345 struct nand_chip *this = mtd->priv;
1347 /* Default for AG-AND. We must use a flash based
1348 * bad block table as the devices have factory marked
1349 * _good_ blocks. Erasing those blocks leads to loss
1350 * of the good / bad information, so we _must_ store
1351 * this information in a good / bad table during
1352 * startup
1354 if (this->options & NAND_IS_AND) {
1355 /* Use the default pattern descriptors */
1356 if (!this->bbt_td) {
1357 this->bbt_td = &bbt_main_descr;
1358 this->bbt_md = &bbt_mirror_descr;
1360 this->bbt_options |= NAND_BBT_USE_FLASH;
1361 return nand_scan_bbt(mtd, &agand_flashbased);
1364 /* Is a flash based bad block table requested ? */
1365 if (this->bbt_options & NAND_BBT_USE_FLASH) {
1366 /* Use the default pattern descriptors */
1367 if (!this->bbt_td) {
1368 if (this->bbt_options & NAND_BBT_NO_OOB) {
1369 this->bbt_td = &bbt_main_no_bbt_descr;
1370 this->bbt_md = &bbt_mirror_no_bbt_descr;
1371 } else {
1372 this->bbt_td = &bbt_main_descr;
1373 this->bbt_md = &bbt_mirror_descr;
1376 } else {
1377 this->bbt_td = NULL;
1378 this->bbt_md = NULL;
1381 if (!this->badblock_pattern)
1382 nand_create_default_bbt_descr(this);
1384 return nand_scan_bbt(mtd, this->badblock_pattern);
1388 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
1389 * @mtd: MTD device structure
1390 * @offs: offset in the device
1391 * @allowbbt: allow access to bad block table region
1394 int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
1396 struct nand_chip *this = mtd->priv;
1397 int block;
1398 uint8_t res;
1400 /* Get block number * 2 */
1401 block = (int)(offs >> (this->bbt_erase_shift - 1));
1402 res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
1404 DEBUG(MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1405 (unsigned int)offs, block >> 1, res);
1407 switch ((int)res) {
1408 case 0x00:
1409 return 0;
1410 case 0x01:
1411 return 1;
1412 case 0x02:
1413 return allowbbt ? 0 : 1;
1415 return 1;
1418 EXPORT_SYMBOL(nand_scan_bbt);
1419 EXPORT_SYMBOL(nand_default_bbt);