]> git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/mtd/nand/nand_bbt.c
Update MTD to that of Linux 2.6.22.1
[people/ms/u-boot.git] / drivers / mtd / nand / nand_bbt.c
1 /*
2 * drivers/mtd/nand_bbt.c
3 *
4 * Overview:
5 * Bad block table support for the NAND driver
6 *
7 * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
8 *
9 * $Id: nand_bbt.c,v 1.36 2005/11/07 11:14:30 gleixner Exp $
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 * Description:
16 *
17 * When nand_scan_bbt is called, then it tries to find the bad block table
18 * depending on the options in the bbt descriptor(s). If a bbt is found
19 * then the contents are read and the memory based bbt is created. If a
20 * mirrored bbt is selected then the mirror is searched too and the
21 * versions are compared. If the mirror has a greater version number
22 * than the mirror bbt is used to build the memory based bbt.
23 * If the tables are not versioned, then we "or" the bad block information.
24 * If one of the bbt's is out of date or does not exist it is (re)created.
25 * If no bbt exists at all then the device is scanned for factory marked
26 * good / bad blocks and the bad block tables are created.
27 *
28 * For manufacturer created bbts like the one found on M-SYS DOC devices
29 * the bbt is searched and read but never created
30 *
31 * The autogenerated bad block table is located in the last good blocks
32 * of the device. The table is mirrored, so it can be updated eventually.
33 * The table is marked in the oob area with an ident pattern and a version
34 * number which indicates which of both tables is more up to date.
35 *
36 * The table uses 2 bits per block
37 * 11b: block is good
38 * 00b: block is factory marked bad
39 * 01b, 10b: block is marked bad due to wear
40 *
41 * The memory bad block table uses the following scheme:
42 * 00b: block is good
43 * 01b: block is marked bad due to wear
44 * 10b: block is reserved (to protect the bbt area)
45 * 11b: block is factory marked bad
46 *
47 * Multichip devices like DOC store the bad block info per floor.
48 *
49 * Following assumptions are made:
50 * - bbts start at a page boundary, if autolocated on a block boundary
51 * - the space necessary for a bbt in FLASH does not exceed a block boundary
52 *
53 */
54
55 #include <common.h>
56
57 #if defined(CONFIG_CMD_NAND) && !defined(CFG_NAND_LEGACY)
58
59 #include <malloc.h>
60 #include <linux/mtd/compat.h>
61 #include <linux/mtd/mtd.h>
62 #include <linux/mtd/nand.h>
63
64 #include <asm/errno.h>
65
66 /* XXX U-BOOT XXX */
67 #if 0
68 #include <linux/slab.h>
69 #include <linux/types.h>
70 #include <linux/mtd/mtd.h>
71 #include <linux/mtd/nand.h>
72 #include <linux/mtd/nand_ecc.h>
73 #include <linux/mtd/compatmac.h>
74 #include <linux/bitops.h>
75 #include <linux/delay.h>
76 #include <linux/vmalloc.h>
77 #endif
78
79 /**
80 * check_pattern - [GENERIC] check if a pattern is in the buffer
81 * @buf: the buffer to search
82 * @len: the length of buffer to search
83 * @paglen: the pagelength
84 * @td: search pattern descriptor
85 *
86 * Check for a pattern at the given place. Used to search bad block
87 * tables and good / bad block identifiers.
88 * If the SCAN_EMPTY option is set then check, if all bytes except the
89 * pattern area contain 0xff
90 *
91 */
92 static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
93 {
94 int i, end = 0;
95 uint8_t *p = buf;
96
97 end = paglen + td->offs;
98 if (td->options & NAND_BBT_SCANEMPTY) {
99 for (i = 0; i < end; i++) {
100 if (p[i] != 0xff)
101 return -1;
102 }
103 }
104 p += end;
105
106 /* Compare the pattern */
107 for (i = 0; i < td->len; i++) {
108 if (p[i] != td->pattern[i])
109 return -1;
110 }
111
112 if (td->options & NAND_BBT_SCANEMPTY) {
113 p += td->len;
114 end += td->len;
115 for (i = end; i < len; i++) {
116 if (*p++ != 0xff)
117 return -1;
118 }
119 }
120 return 0;
121 }
122
123 /**
124 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
125 * @buf: the buffer to search
126 * @td: search pattern descriptor
127 *
128 * Check for a pattern at the given place. Used to search bad block
129 * tables and good / bad block identifiers. Same as check_pattern, but
130 * no optional empty check
131 *
132 */
133 static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
134 {
135 int i;
136 uint8_t *p = buf;
137
138 /* Compare the pattern */
139 for (i = 0; i < td->len; i++) {
140 if (p[td->offs + i] != td->pattern[i])
141 return -1;
142 }
143 return 0;
144 }
145
146 /**
147 * read_bbt - [GENERIC] Read the bad block table starting from page
148 * @mtd: MTD device structure
149 * @buf: temporary buffer
150 * @page: the starting page
151 * @num: the number of bbt descriptors to read
152 * @bits: number of bits per block
153 * @offs: offset in the memory table
154 * @reserved_block_code: Pattern to identify reserved blocks
155 *
156 * Read the bad block table starting from page.
157 *
158 */
159 static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
160 int bits, int offs, int reserved_block_code)
161 {
162 int res, i, j, act = 0;
163 struct nand_chip *this = mtd->priv;
164 size_t retlen, len, totlen;
165 loff_t from;
166 uint8_t msk = (uint8_t) ((1 << bits) - 1);
167
168 totlen = (num * bits) >> 3;
169 from = ((loff_t) page) << this->page_shift;
170
171 while (totlen) {
172 len = min(totlen, (size_t) (1 << this->bbt_erase_shift));
173 res = mtd->read(mtd, from, len, &retlen, buf);
174 if (res < 0) {
175 if (retlen != len) {
176 printk(KERN_INFO "nand_bbt: Error reading bad block table\n");
177 return res;
178 }
179 printk(KERN_WARNING "nand_bbt: ECC error while reading bad block table\n");
180 }
181
182 /* Analyse data */
183 for (i = 0; i < len; i++) {
184 uint8_t dat = buf[i];
185 for (j = 0; j < 8; j += bits, act += 2) {
186 uint8_t tmp = (dat >> j) & msk;
187 if (tmp == msk)
188 continue;
189 if (reserved_block_code && (tmp == reserved_block_code)) {
190 printk(KERN_DEBUG "nand_read_bbt: Reserved block at 0x%08x\n",
191 ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
192 this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
193 mtd->ecc_stats.bbtblocks++;
194 continue;
195 }
196 /* Leave it for now, if its matured we can move this
197 * message to MTD_DEBUG_LEVEL0 */
198 printk(KERN_DEBUG "nand_read_bbt: Bad block at 0x%08x\n",
199 ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
200 /* Factory marked bad or worn out ? */
201 if (tmp == 0)
202 this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
203 else
204 this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
205 mtd->ecc_stats.badblocks++;
206 }
207 }
208 totlen -= len;
209 from += len;
210 }
211 return 0;
212 }
213
214 /**
215 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
216 * @mtd: MTD device structure
217 * @buf: temporary buffer
218 * @td: descriptor for the bad block table
219 * @chip: read the table for a specific chip, -1 read all chips.
220 * Applies only if NAND_BBT_PERCHIP option is set
221 *
222 * Read the bad block table for all chips starting at a given page
223 * We assume that the bbt bits are in consecutive order.
224 */
225 static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
226 {
227 struct nand_chip *this = mtd->priv;
228 int res = 0, i;
229 int bits;
230
231 bits = td->options & NAND_BBT_NRBITS_MSK;
232 if (td->options & NAND_BBT_PERCHIP) {
233 int offs = 0;
234 for (i = 0; i < this->numchips; i++) {
235 if (chip == -1 || chip == i)
236 res = read_bbt (mtd, buf, td->pages[i], this->chipsize >> this->bbt_erase_shift, bits, offs, td->reserved_block_code);
237 if (res)
238 return res;
239 offs += this->chipsize >> (this->bbt_erase_shift + 2);
240 }
241 } else {
242 res = read_bbt (mtd, buf, td->pages[0], mtd->size >> this->bbt_erase_shift, bits, 0, td->reserved_block_code);
243 if (res)
244 return res;
245 }
246 return 0;
247 }
248
249 /*
250 * Scan read raw data from flash
251 */
252 static int scan_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
253 size_t len)
254 {
255 struct mtd_oob_ops ops;
256
257 ops.mode = MTD_OOB_RAW;
258 ops.ooboffs = 0;
259 ops.ooblen = mtd->oobsize;
260 ops.oobbuf = buf;
261 ops.datbuf = buf;
262 ops.len = len;
263
264 return mtd->read_oob(mtd, offs, &ops);
265 }
266
267 /*
268 * Scan write data with oob to flash
269 */
270 static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
271 uint8_t *buf, uint8_t *oob)
272 {
273 struct mtd_oob_ops ops;
274
275 ops.mode = MTD_OOB_PLACE;
276 ops.ooboffs = 0;
277 ops.ooblen = mtd->oobsize;
278 ops.datbuf = buf;
279 ops.oobbuf = oob;
280 ops.len = len;
281
282 return mtd->write_oob(mtd, offs, &ops);
283 }
284
285 /**
286 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
287 * @mtd: MTD device structure
288 * @buf: temporary buffer
289 * @td: descriptor for the bad block table
290 * @md: descriptor for the bad block table mirror
291 *
292 * Read the bad block table(s) for all chips starting at a given page
293 * We assume that the bbt bits are in consecutive order.
294 *
295 */
296 static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
297 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
298 {
299 struct nand_chip *this = mtd->priv;
300
301 /* Read the primary version, if available */
302 if (td->options & NAND_BBT_VERSION) {
303 scan_read_raw(mtd, buf, td->pages[0] << this->page_shift,
304 mtd->writesize);
305 td->version[0] = buf[mtd->writesize + td->veroffs];
306 printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
307 td->pages[0], td->version[0]);
308 }
309
310 /* Read the mirror version, if available */
311 if (md && (md->options & NAND_BBT_VERSION)) {
312 scan_read_raw(mtd, buf, md->pages[0] << this->page_shift,
313 mtd->writesize);
314 md->version[0] = buf[mtd->writesize + md->veroffs];
315 printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
316 md->pages[0], md->version[0]);
317 }
318 return 1;
319 }
320
321 /*
322 * Scan a given block full
323 */
324 static int scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
325 loff_t offs, uint8_t *buf, size_t readlen,
326 int scanlen, int len)
327 {
328 int ret, j;
329
330 ret = scan_read_raw(mtd, buf, offs, readlen);
331 if (ret)
332 return ret;
333
334 for (j = 0; j < len; j++, buf += scanlen) {
335 if (check_pattern(buf, scanlen, mtd->writesize, bd))
336 return 1;
337 }
338 return 0;
339 }
340
341 /*
342 * Scan a given block partially
343 */
344 static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
345 loff_t offs, uint8_t *buf, int len)
346 {
347 struct mtd_oob_ops ops;
348 int j, ret;
349
350 ops.ooblen = mtd->oobsize;
351 ops.oobbuf = buf;
352 ops.ooboffs = 0;
353 ops.datbuf = NULL;
354 ops.mode = MTD_OOB_PLACE;
355
356 for (j = 0; j < len; j++) {
357 /*
358 * Read the full oob until read_oob is fixed to
359 * handle single byte reads for 16 bit
360 * buswidth
361 */
362 ret = mtd->read_oob(mtd, offs, &ops);
363 if (ret)
364 return ret;
365
366 if (check_short_pattern(buf, bd))
367 return 1;
368
369 offs += mtd->writesize;
370 }
371 return 0;
372 }
373
374 /**
375 * create_bbt - [GENERIC] Create a bad block table by scanning the device
376 * @mtd: MTD device structure
377 * @buf: temporary buffer
378 * @bd: descriptor for the good/bad block search pattern
379 * @chip: create the table for a specific chip, -1 read all chips.
380 * Applies only if NAND_BBT_PERCHIP option is set
381 *
382 * Create a bad block table by scanning the device
383 * for the given good/bad block identify pattern
384 */
385 static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
386 struct nand_bbt_descr *bd, int chip)
387 {
388 struct nand_chip *this = mtd->priv;
389 int i, numblocks, len, scanlen;
390 int startblock;
391 loff_t from;
392 size_t readlen;
393
394 printk(KERN_INFO "Scanning device for bad blocks\n");
395
396 if (bd->options & NAND_BBT_SCANALLPAGES)
397 len = 1 << (this->bbt_erase_shift - this->page_shift);
398 else {
399 if (bd->options & NAND_BBT_SCAN2NDPAGE)
400 len = 2;
401 else
402 len = 1;
403 }
404
405 if (!(bd->options & NAND_BBT_SCANEMPTY)) {
406 /* We need only read few bytes from the OOB area */
407 scanlen = 0;
408 readlen = bd->len;
409 } else {
410 /* Full page content should be read */
411 scanlen = mtd->writesize + mtd->oobsize;
412 readlen = len * mtd->writesize;
413 }
414
415 if (chip == -1) {
416 /* Note that numblocks is 2 * (real numblocks) here, see i+=2
417 * below as it makes shifting and masking less painful */
418 numblocks = mtd->size >> (this->bbt_erase_shift - 1);
419 startblock = 0;
420 from = 0;
421 } else {
422 if (chip >= this->numchips) {
423 printk(KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n",
424 chip + 1, this->numchips);
425 return -EINVAL;
426 }
427 numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
428 startblock = chip * numblocks;
429 numblocks += startblock;
430 from = startblock << (this->bbt_erase_shift - 1);
431 }
432
433 for (i = startblock; i < numblocks;) {
434 int ret;
435
436 if (bd->options & NAND_BBT_SCANALLPAGES)
437 ret = scan_block_full(mtd, bd, from, buf, readlen,
438 scanlen, len);
439 else
440 ret = scan_block_fast(mtd, bd, from, buf, len);
441
442 if (ret < 0)
443 return ret;
444
445 if (ret) {
446 this->bbt[i >> 3] |= 0x03 << (i & 0x6);
447 printk(KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
448 i >> 1, (unsigned int)from);
449 mtd->ecc_stats.badblocks++;
450 }
451
452 i += 2;
453 from += (1 << this->bbt_erase_shift);
454 }
455 return 0;
456 }
457
458 /**
459 * search_bbt - [GENERIC] scan the device for a specific bad block table
460 * @mtd: MTD device structure
461 * @buf: temporary buffer
462 * @td: descriptor for the bad block table
463 *
464 * Read the bad block table by searching for a given ident pattern.
465 * Search is preformed either from the beginning up or from the end of
466 * the device downwards. The search starts always at the start of a
467 * block.
468 * If the option NAND_BBT_PERCHIP is given, each chip is searched
469 * for a bbt, which contains the bad block information of this chip.
470 * This is necessary to provide support for certain DOC devices.
471 *
472 * The bbt ident pattern resides in the oob area of the first page
473 * in a block.
474 */
475 static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
476 {
477 struct nand_chip *this = mtd->priv;
478 int i, chips;
479 int bits, startblock, block, dir;
480 int scanlen = mtd->writesize + mtd->oobsize;
481 int bbtblocks;
482 int blocktopage = this->bbt_erase_shift - this->page_shift;
483
484 /* Search direction top -> down ? */
485 if (td->options & NAND_BBT_LASTBLOCK) {
486 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
487 dir = -1;
488 } else {
489 startblock = 0;
490 dir = 1;
491 }
492
493 /* Do we have a bbt per chip ? */
494 if (td->options & NAND_BBT_PERCHIP) {
495 chips = this->numchips;
496 bbtblocks = this->chipsize >> this->bbt_erase_shift;
497 startblock &= bbtblocks - 1;
498 } else {
499 chips = 1;
500 bbtblocks = mtd->size >> this->bbt_erase_shift;
501 }
502
503 /* Number of bits for each erase block in the bbt */
504 bits = td->options & NAND_BBT_NRBITS_MSK;
505
506 for (i = 0; i < chips; i++) {
507 /* Reset version information */
508 td->version[i] = 0;
509 td->pages[i] = -1;
510 /* Scan the maximum number of blocks */
511 for (block = 0; block < td->maxblocks; block++) {
512
513 int actblock = startblock + dir * block;
514 loff_t offs = actblock << this->bbt_erase_shift;
515
516 /* Read first page */
517 scan_read_raw(mtd, buf, offs, mtd->writesize);
518 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
519 td->pages[i] = actblock << blocktopage;
520 if (td->options & NAND_BBT_VERSION) {
521 td->version[i] = buf[mtd->writesize + td->veroffs];
522 }
523 break;
524 }
525 }
526 startblock += this->chipsize >> this->bbt_erase_shift;
527 }
528 /* Check, if we found a bbt for each requested chip */
529 for (i = 0; i < chips; i++) {
530 if (td->pages[i] == -1)
531 printk(KERN_WARNING "Bad block table not found for chip %d\n", i);
532 else
533 printk(KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n", td->pages[i],
534 td->version[i]);
535 }
536 return 0;
537 }
538
539 /**
540 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
541 * @mtd: MTD device structure
542 * @buf: temporary buffer
543 * @td: descriptor for the bad block table
544 * @md: descriptor for the bad block table mirror
545 *
546 * Search and read the bad block table(s)
547 */
548 static int search_read_bbts(struct mtd_info *mtd, uint8_t * buf, struct nand_bbt_descr *td, struct nand_bbt_descr *md)
549 {
550 /* Search the primary table */
551 search_bbt(mtd, buf, td);
552
553 /* Search the mirror table */
554 if (md)
555 search_bbt(mtd, buf, md);
556
557 /* Force result check */
558 return 1;
559 }
560
561 /**
562 * write_bbt - [GENERIC] (Re)write the bad block table
563 *
564 * @mtd: MTD device structure
565 * @buf: temporary buffer
566 * @td: descriptor for the bad block table
567 * @md: descriptor for the bad block table mirror
568 * @chipsel: selector for a specific chip, -1 for all
569 *
570 * (Re)write the bad block table
571 *
572 */
573 static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
574 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
575 int chipsel)
576 {
577 struct nand_chip *this = mtd->priv;
578 struct erase_info einfo;
579 int i, j, res, chip = 0;
580 int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
581 int nrchips, bbtoffs, pageoffs, ooboffs;
582 uint8_t msk[4];
583 uint8_t rcode = td->reserved_block_code;
584 size_t retlen, len = 0;
585 loff_t to;
586 struct mtd_oob_ops ops;
587
588 ops.ooblen = mtd->oobsize;
589 ops.ooboffs = 0;
590 ops.datbuf = NULL;
591 ops.mode = MTD_OOB_PLACE;
592
593 if (!rcode)
594 rcode = 0xff;
595 /* Write bad block table per chip rather than per device ? */
596 if (td->options & NAND_BBT_PERCHIP) {
597 numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
598 /* Full device write or specific chip ? */
599 if (chipsel == -1) {
600 nrchips = this->numchips;
601 } else {
602 nrchips = chipsel + 1;
603 chip = chipsel;
604 }
605 } else {
606 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
607 nrchips = 1;
608 }
609
610 /* Loop through the chips */
611 for (; chip < nrchips; chip++) {
612
613 /* There was already a version of the table, reuse the page
614 * This applies for absolute placement too, as we have the
615 * page nr. in td->pages.
616 */
617 if (td->pages[chip] != -1) {
618 page = td->pages[chip];
619 goto write;
620 }
621
622 /* Automatic placement of the bad block table */
623 /* Search direction top -> down ? */
624 if (td->options & NAND_BBT_LASTBLOCK) {
625 startblock = numblocks * (chip + 1) - 1;
626 dir = -1;
627 } else {
628 startblock = chip * numblocks;
629 dir = 1;
630 }
631
632 for (i = 0; i < td->maxblocks; i++) {
633 int block = startblock + dir * i;
634 /* Check, if the block is bad */
635 switch ((this->bbt[block >> 2] >>
636 (2 * (block & 0x03))) & 0x03) {
637 case 0x01:
638 case 0x03:
639 continue;
640 }
641 page = block <<
642 (this->bbt_erase_shift - this->page_shift);
643 /* Check, if the block is used by the mirror table */
644 if (!md || md->pages[chip] != page)
645 goto write;
646 }
647 printk(KERN_ERR "No space left to write bad block table\n");
648 return -ENOSPC;
649 write:
650
651 /* Set up shift count and masks for the flash table */
652 bits = td->options & NAND_BBT_NRBITS_MSK;
653 msk[2] = ~rcode;
654 switch (bits) {
655 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
656 msk[3] = 0x01;
657 break;
658 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
659 msk[3] = 0x03;
660 break;
661 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
662 msk[3] = 0x0f;
663 break;
664 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
665 msk[3] = 0xff;
666 break;
667 default: return -EINVAL;
668 }
669
670 bbtoffs = chip * (numblocks >> 2);
671
672 to = ((loff_t) page) << this->page_shift;
673
674 /* Must we save the block contents ? */
675 if (td->options & NAND_BBT_SAVECONTENT) {
676 /* Make it block aligned */
677 to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1));
678 len = 1 << this->bbt_erase_shift;
679 res = mtd->read(mtd, to, len, &retlen, buf);
680 if (res < 0) {
681 if (retlen != len) {
682 printk(KERN_INFO "nand_bbt: Error "
683 "reading block for writing "
684 "the bad block table\n");
685 return res;
686 }
687 printk(KERN_WARNING "nand_bbt: ECC error "
688 "while reading block for writing "
689 "bad block table\n");
690 }
691 /* Read oob data */
692 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
693 ops.oobbuf = &buf[len];
694 res = mtd->read_oob(mtd, to + mtd->writesize, &ops);
695 if (res < 0 || ops.oobretlen != ops.ooblen)
696 goto outerr;
697
698 /* Calc the byte offset in the buffer */
699 pageoffs = page - (int)(to >> this->page_shift);
700 offs = pageoffs << this->page_shift;
701 /* Preset the bbt area with 0xff */
702 memset(&buf[offs], 0xff, (size_t) (numblocks >> sft));
703 ooboffs = len + (pageoffs * mtd->oobsize);
704
705 } else {
706 /* Calc length */
707 len = (size_t) (numblocks >> sft);
708 /* Make it page aligned ! */
709 len = (len + (mtd->writesize - 1)) &
710 ~(mtd->writesize - 1);
711 /* Preset the buffer with 0xff */
712 memset(buf, 0xff, len +
713 (len >> this->page_shift)* mtd->oobsize);
714 offs = 0;
715 ooboffs = len;
716 /* Pattern is located in oob area of first page */
717 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
718 }
719
720 if (td->options & NAND_BBT_VERSION)
721 buf[ooboffs + td->veroffs] = td->version[chip];
722
723 /* walk through the memory table */
724 for (i = 0; i < numblocks;) {
725 uint8_t dat;
726 dat = this->bbt[bbtoffs + (i >> 2)];
727 for (j = 0; j < 4; j++, i++) {
728 int sftcnt = (i << (3 - sft)) & sftmsk;
729 /* Do not store the reserved bbt blocks ! */
730 buf[offs + (i >> sft)] &=
731 ~(msk[dat & 0x03] << sftcnt);
732 dat >>= 2;
733 }
734 }
735
736 memset(&einfo, 0, sizeof(einfo));
737 einfo.mtd = mtd;
738 einfo.addr = (unsigned long)to;
739 einfo.len = 1 << this->bbt_erase_shift;
740 res = nand_erase_nand(mtd, &einfo, 1);
741 if (res < 0)
742 goto outerr;
743
744 res = scan_write_bbt(mtd, to, len, buf, &buf[len]);
745 if (res < 0)
746 goto outerr;
747
748 printk(KERN_DEBUG "Bad block table written to 0x%08x, version "
749 "0x%02X\n", (unsigned int)to, td->version[chip]);
750
751 /* Mark it as used */
752 td->pages[chip] = page;
753 }
754 return 0;
755
756 outerr:
757 printk(KERN_WARNING
758 "nand_bbt: Error while writing bad block table %d\n", res);
759 return res;
760 }
761
762 /**
763 * nand_memory_bbt - [GENERIC] create a memory based bad block table
764 * @mtd: MTD device structure
765 * @bd: descriptor for the good/bad block search pattern
766 *
767 * The function creates a memory based bbt by scanning the device
768 * for manufacturer / software marked good / bad blocks
769 */
770 static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
771 {
772 struct nand_chip *this = mtd->priv;
773
774 bd->options &= ~NAND_BBT_SCANEMPTY;
775 return create_bbt(mtd, this->buffers->databuf, bd, -1);
776 }
777
778 /**
779 * check_create - [GENERIC] create and write bbt(s) if necessary
780 * @mtd: MTD device structure
781 * @buf: temporary buffer
782 * @bd: descriptor for the good/bad block search pattern
783 *
784 * The function checks the results of the previous call to read_bbt
785 * and creates / updates the bbt(s) if necessary
786 * Creation is necessary if no bbt was found for the chip/device
787 * Update is necessary if one of the tables is missing or the
788 * version nr. of one table is less than the other
789 */
790 static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
791 {
792 int i, chips, writeops, chipsel, res;
793 struct nand_chip *this = mtd->priv;
794 struct nand_bbt_descr *td = this->bbt_td;
795 struct nand_bbt_descr *md = this->bbt_md;
796 struct nand_bbt_descr *rd, *rd2;
797
798 /* Do we have a bbt per chip ? */
799 if (td->options & NAND_BBT_PERCHIP)
800 chips = this->numchips;
801 else
802 chips = 1;
803
804 for (i = 0; i < chips; i++) {
805 writeops = 0;
806 rd = NULL;
807 rd2 = NULL;
808 /* Per chip or per device ? */
809 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
810 /* Mirrored table avilable ? */
811 if (md) {
812 if (td->pages[i] == -1 && md->pages[i] == -1) {
813 writeops = 0x03;
814 goto create;
815 }
816
817 if (td->pages[i] == -1) {
818 rd = md;
819 td->version[i] = md->version[i];
820 writeops = 1;
821 goto writecheck;
822 }
823
824 if (md->pages[i] == -1) {
825 rd = td;
826 md->version[i] = td->version[i];
827 writeops = 2;
828 goto writecheck;
829 }
830
831 if (td->version[i] == md->version[i]) {
832 rd = td;
833 if (!(td->options & NAND_BBT_VERSION))
834 rd2 = md;
835 goto writecheck;
836 }
837
838 if (((int8_t) (td->version[i] - md->version[i])) > 0) {
839 rd = td;
840 md->version[i] = td->version[i];
841 writeops = 2;
842 } else {
843 rd = md;
844 td->version[i] = md->version[i];
845 writeops = 1;
846 }
847
848 goto writecheck;
849
850 } else {
851 if (td->pages[i] == -1) {
852 writeops = 0x01;
853 goto create;
854 }
855 rd = td;
856 goto writecheck;
857 }
858 create:
859 /* Create the bad block table by scanning the device ? */
860 if (!(td->options & NAND_BBT_CREATE))
861 continue;
862
863 /* Create the table in memory by scanning the chip(s) */
864 create_bbt(mtd, buf, bd, chipsel);
865
866 td->version[i] = 1;
867 if (md)
868 md->version[i] = 1;
869 writecheck:
870 /* read back first ? */
871 if (rd)
872 read_abs_bbt(mtd, buf, rd, chipsel);
873 /* If they weren't versioned, read both. */
874 if (rd2)
875 read_abs_bbt(mtd, buf, rd2, chipsel);
876
877 /* Write the bad block table to the device ? */
878 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
879 res = write_bbt(mtd, buf, td, md, chipsel);
880 if (res < 0)
881 return res;
882 }
883
884 /* Write the mirror bad block table to the device ? */
885 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
886 res = write_bbt(mtd, buf, md, td, chipsel);
887 if (res < 0)
888 return res;
889 }
890 }
891 return 0;
892 }
893
894 /**
895 * mark_bbt_regions - [GENERIC] mark the bad block table regions
896 * @mtd: MTD device structure
897 * @td: bad block table descriptor
898 *
899 * The bad block table regions are marked as "bad" to prevent
900 * accidental erasures / writes. The regions are identified by
901 * the mark 0x02.
902 */
903 static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
904 {
905 struct nand_chip *this = mtd->priv;
906 int i, j, chips, block, nrblocks, update;
907 uint8_t oldval, newval;
908
909 /* Do we have a bbt per chip ? */
910 if (td->options & NAND_BBT_PERCHIP) {
911 chips = this->numchips;
912 nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
913 } else {
914 chips = 1;
915 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
916 }
917
918 for (i = 0; i < chips; i++) {
919 if ((td->options & NAND_BBT_ABSPAGE) ||
920 !(td->options & NAND_BBT_WRITE)) {
921 if (td->pages[i] == -1)
922 continue;
923 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
924 block <<= 1;
925 oldval = this->bbt[(block >> 3)];
926 newval = oldval | (0x2 << (block & 0x06));
927 this->bbt[(block >> 3)] = newval;
928 if ((oldval != newval) && td->reserved_block_code)
929 nand_update_bbt(mtd, block << (this->bbt_erase_shift - 1));
930 continue;
931 }
932 update = 0;
933 if (td->options & NAND_BBT_LASTBLOCK)
934 block = ((i + 1) * nrblocks) - td->maxblocks;
935 else
936 block = i * nrblocks;
937 block <<= 1;
938 for (j = 0; j < td->maxblocks; j++) {
939 oldval = this->bbt[(block >> 3)];
940 newval = oldval | (0x2 << (block & 0x06));
941 this->bbt[(block >> 3)] = newval;
942 if (oldval != newval)
943 update = 1;
944 block += 2;
945 }
946 /* If we want reserved blocks to be recorded to flash, and some
947 new ones have been marked, then we need to update the stored
948 bbts. This should only happen once. */
949 if (update && td->reserved_block_code)
950 nand_update_bbt(mtd, (block - 2) << (this->bbt_erase_shift - 1));
951 }
952 }
953
954 /**
955 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
956 * @mtd: MTD device structure
957 * @bd: descriptor for the good/bad block search pattern
958 *
959 * The function checks, if a bad block table(s) is/are already
960 * available. If not it scans the device for manufacturer
961 * marked good / bad blocks and writes the bad block table(s) to
962 * the selected place.
963 *
964 * The bad block table memory is allocated here. It must be freed
965 * by calling the nand_free_bbt function.
966 *
967 */
968 int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
969 {
970 struct nand_chip *this = mtd->priv;
971 int len, res = 0;
972 uint8_t *buf;
973 struct nand_bbt_descr *td = this->bbt_td;
974 struct nand_bbt_descr *md = this->bbt_md;
975
976 len = mtd->size >> (this->bbt_erase_shift + 2);
977 /* Allocate memory (2bit per block) and clear the memory bad block table */
978 this->bbt = kzalloc(len, GFP_KERNEL);
979 if (!this->bbt) {
980 printk(KERN_ERR "nand_scan_bbt: Out of memory\n");
981 return -ENOMEM;
982 }
983
984 /* If no primary table decriptor is given, scan the device
985 * to build a memory based bad block table
986 */
987 if (!td) {
988 if ((res = nand_memory_bbt(mtd, bd))) {
989 printk(KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n");
990 kfree(this->bbt);
991 this->bbt = NULL;
992 }
993 return res;
994 }
995
996 /* Allocate a temporary buffer for one eraseblock incl. oob */
997 len = (1 << this->bbt_erase_shift);
998 len += (len >> this->page_shift) * mtd->oobsize;
999 buf = vmalloc(len);
1000 if (!buf) {
1001 printk(KERN_ERR "nand_bbt: Out of memory\n");
1002 kfree(this->bbt);
1003 this->bbt = NULL;
1004 return -ENOMEM;
1005 }
1006
1007 /* Is the bbt at a given page ? */
1008 if (td->options & NAND_BBT_ABSPAGE) {
1009 res = read_abs_bbts(mtd, buf, td, md);
1010 } else {
1011 /* Search the bad block table using a pattern in oob */
1012 res = search_read_bbts(mtd, buf, td, md);
1013 }
1014
1015 if (res)
1016 res = check_create(mtd, buf, bd);
1017
1018 /* Prevent the bbt regions from erasing / writing */
1019 mark_bbt_region(mtd, td);
1020 if (md)
1021 mark_bbt_region(mtd, md);
1022
1023 vfree(buf);
1024 return res;
1025 }
1026
1027 /**
1028 * nand_update_bbt - [NAND Interface] update bad block table(s)
1029 * @mtd: MTD device structure
1030 * @offs: the offset of the newly marked block
1031 *
1032 * The function updates the bad block table(s)
1033 */
1034 int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
1035 {
1036 struct nand_chip *this = mtd->priv;
1037 int len, res = 0, writeops = 0;
1038 int chip, chipsel;
1039 uint8_t *buf;
1040 struct nand_bbt_descr *td = this->bbt_td;
1041 struct nand_bbt_descr *md = this->bbt_md;
1042
1043 if (!this->bbt || !td)
1044 return -EINVAL;
1045
1046 len = mtd->size >> (this->bbt_erase_shift + 2);
1047 /* Allocate a temporary buffer for one eraseblock incl. oob */
1048 len = (1 << this->bbt_erase_shift);
1049 len += (len >> this->page_shift) * mtd->oobsize;
1050 buf = kmalloc(len, GFP_KERNEL);
1051 if (!buf) {
1052 printk(KERN_ERR "nand_update_bbt: Out of memory\n");
1053 return -ENOMEM;
1054 }
1055
1056 writeops = md != NULL ? 0x03 : 0x01;
1057
1058 /* Do we have a bbt per chip ? */
1059 if (td->options & NAND_BBT_PERCHIP) {
1060 chip = (int)(offs >> this->chip_shift);
1061 chipsel = chip;
1062 } else {
1063 chip = 0;
1064 chipsel = -1;
1065 }
1066
1067 td->version[chip]++;
1068 if (md)
1069 md->version[chip]++;
1070
1071 /* Write the bad block table to the device ? */
1072 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
1073 res = write_bbt(mtd, buf, td, md, chipsel);
1074 if (res < 0)
1075 goto out;
1076 }
1077 /* Write the mirror bad block table to the device ? */
1078 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
1079 res = write_bbt(mtd, buf, md, td, chipsel);
1080 }
1081
1082 out:
1083 kfree(buf);
1084 return res;
1085 }
1086
1087 /* Define some generic bad / good block scan pattern which are used
1088 * while scanning a device for factory marked good / bad blocks. */
1089 static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1090
1091 static struct nand_bbt_descr smallpage_memorybased = {
1092 .options = NAND_BBT_SCAN2NDPAGE,
1093 .offs = 5,
1094 .len = 1,
1095 .pattern = scan_ff_pattern
1096 };
1097
1098 static struct nand_bbt_descr largepage_memorybased = {
1099 .options = 0,
1100 .offs = 0,
1101 .len = 2,
1102 .pattern = scan_ff_pattern
1103 };
1104
1105 static struct nand_bbt_descr smallpage_flashbased = {
1106 .options = NAND_BBT_SCAN2NDPAGE,
1107 .offs = 5,
1108 .len = 1,
1109 .pattern = scan_ff_pattern
1110 };
1111
1112 static struct nand_bbt_descr largepage_flashbased = {
1113 .options = NAND_BBT_SCAN2NDPAGE,
1114 .offs = 0,
1115 .len = 2,
1116 .pattern = scan_ff_pattern
1117 };
1118
1119 static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
1120
1121 static struct nand_bbt_descr agand_flashbased = {
1122 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
1123 .offs = 0x20,
1124 .len = 6,
1125 .pattern = scan_agand_pattern
1126 };
1127
1128 /* Generic flash bbt decriptors
1129 */
1130 static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1131 static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1132
1133 static struct nand_bbt_descr bbt_main_descr = {
1134 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1135 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1136 .offs = 8,
1137 .len = 4,
1138 .veroffs = 12,
1139 .maxblocks = 4,
1140 .pattern = bbt_pattern
1141 };
1142
1143 static struct nand_bbt_descr bbt_mirror_descr = {
1144 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1145 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1146 .offs = 8,
1147 .len = 4,
1148 .veroffs = 12,
1149 .maxblocks = 4,
1150 .pattern = mirror_pattern
1151 };
1152
1153 /**
1154 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
1155 * @mtd: MTD device structure
1156 *
1157 * This function selects the default bad block table
1158 * support for the device and calls the nand_scan_bbt function
1159 *
1160 */
1161 int nand_default_bbt(struct mtd_info *mtd)
1162 {
1163 struct nand_chip *this = mtd->priv;
1164
1165 /* Default for AG-AND. We must use a flash based
1166 * bad block table as the devices have factory marked
1167 * _good_ blocks. Erasing those blocks leads to loss
1168 * of the good / bad information, so we _must_ store
1169 * this information in a good / bad table during
1170 * startup
1171 */
1172 if (this->options & NAND_IS_AND) {
1173 /* Use the default pattern descriptors */
1174 if (!this->bbt_td) {
1175 this->bbt_td = &bbt_main_descr;
1176 this->bbt_md = &bbt_mirror_descr;
1177 }
1178 this->options |= NAND_USE_FLASH_BBT;
1179 return nand_scan_bbt(mtd, &agand_flashbased);
1180 }
1181
1182 /* Is a flash based bad block table requested ? */
1183 if (this->options & NAND_USE_FLASH_BBT) {
1184 /* Use the default pattern descriptors */
1185 if (!this->bbt_td) {
1186 this->bbt_td = &bbt_main_descr;
1187 this->bbt_md = &bbt_mirror_descr;
1188 }
1189 if (!this->badblock_pattern) {
1190 this->badblock_pattern = (mtd->writesize > 512) ? &largepage_flashbased : &smallpage_flashbased;
1191 }
1192 } else {
1193 this->bbt_td = NULL;
1194 this->bbt_md = NULL;
1195 if (!this->badblock_pattern) {
1196 this->badblock_pattern = (mtd->writesize > 512) ?
1197 &largepage_memorybased : &smallpage_memorybased;
1198 }
1199 }
1200 return nand_scan_bbt(mtd, this->badblock_pattern);
1201 }
1202
1203 /**
1204 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
1205 * @mtd: MTD device structure
1206 * @offs: offset in the device
1207 * @allowbbt: allow access to bad block table region
1208 *
1209 */
1210 int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
1211 {
1212 struct nand_chip *this = mtd->priv;
1213 int block;
1214 uint8_t res;
1215
1216 /* Get block number * 2 */
1217 block = (int)(offs >> (this->bbt_erase_shift - 1));
1218 res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
1219
1220 MTDDEBUG (MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: "
1221 "(block %d) 0x%02x\n", (unsigned int)offs, res, block >> 1);
1222
1223 switch ((int)res) {
1224 case 0x00:
1225 return 0;
1226 case 0x01:
1227 return 1;
1228 case 0x02:
1229 return allowbbt ? 0 : 1;
1230 }
1231 return 1;
1232 }
1233
1234 /* XXX U-BOOT XXX */
1235 #if 0
1236 EXPORT_SYMBOL(nand_scan_bbt);
1237 EXPORT_SYMBOL(nand_default_bbt);
1238 #endif
1239
1240 #endif