blob: aaa9400e54d501bd8bea6bbc3b813d06080b28e1 [file] [log] [blame]
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001/*
2 * drivers/mtd/nand_bbt.c
3 *
4 * Overview:
5 * Bad block table support for the NAND driver
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02006 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02007 * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
8 *
9 * $Id: nand_bbt.c,v 1.28 2004/11/13 10:19:09 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 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020017 * 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
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020020 * mirrored bbt is selected then the mirror is searched too and the
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020021 * versions are compared. If the mirror has a greater version number
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020022 * 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.
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020024 * 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.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020027 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020028 * For manufacturer created bbts like the one found on M-SYS DOC devices
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020029 * the bbt is searched and read but never created
30 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020031 * 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
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020034 * 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
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020046 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020047 * 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 neccecary for a bbt in FLASH does not exceed a block boundary
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020052 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020053 */
54
55#include <common.h>
56
Marian Balakowicz6a076752006-04-08 19:08:06 +020057#if (CONFIG_COMMANDS & CFG_CMD_NAND) && !defined(CFG_NAND_LEGACY)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020058
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
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020066/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020067 * check_pattern - [GENERIC] check if a pattern is in the buffer
68 * @buf: the buffer to search
69 * @len: the length of buffer to search
70 * @paglen: the pagelength
71 * @td: search pattern descriptor
72 *
73 * Check for a pattern at the given place. Used to search bad block
74 * tables and good / bad block identifiers.
75 * If the SCAN_EMPTY option is set then check, if all bytes except the
76 * pattern area contain 0xff
77 *
78*/
79static int check_pattern (uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
80{
81 int i, end;
82 uint8_t *p = buf;
83
84 end = paglen + td->offs;
85 if (td->options & NAND_BBT_SCANEMPTY) {
86 for (i = 0; i < end; i++) {
87 if (p[i] != 0xff)
88 return -1;
89 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020090 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020091 p += end;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020092
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020093 /* Compare the pattern */
94 for (i = 0; i < td->len; i++) {
95 if (p[i] != td->pattern[i])
96 return -1;
97 }
98
99 p += td->len;
100 end += td->len;
101 if (td->options & NAND_BBT_SCANEMPTY) {
102 for (i = end; i < len; i++) {
103 if (*p++ != 0xff)
104 return -1;
105 }
106 }
107 return 0;
108}
109
110/**
111 * read_bbt - [GENERIC] Read the bad block table starting from page
112 * @mtd: MTD device structure
113 * @buf: temporary buffer
114 * @page: the starting page
115 * @num: the number of bbt descriptors to read
116 * @bits: number of bits per block
117 * @offs: offset in the memory table
118 * @reserved_block_code: Pattern to identify reserved blocks
119 *
120 * Read the bad block table starting from page.
121 *
122 */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200123static int read_bbt (struct mtd_info *mtd, uint8_t *buf, int page, int num,
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200124 int bits, int offs, int reserved_block_code)
125{
126 int res, i, j, act = 0;
127 struct nand_chip *this = mtd->priv;
128 size_t retlen, len, totlen;
129 loff_t from;
130 uint8_t msk = (uint8_t) ((1 << bits) - 1);
131
132 totlen = (num * bits) >> 3;
133 from = ((loff_t)page) << this->page_shift;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200134
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200135 while (totlen) {
136 len = min (totlen, (size_t) (1 << this->bbt_erase_shift));
137 res = mtd->read_ecc (mtd, from, len, &retlen, buf, NULL, this->autooob);
138 if (res < 0) {
139 if (retlen != len) {
140 printk (KERN_INFO "nand_bbt: Error reading bad block table\n");
141 return res;
142 }
143 printk (KERN_WARNING "nand_bbt: ECC error while reading bad block table\n");
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200144 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200145
146 /* Analyse data */
147 for (i = 0; i < len; i++) {
148 uint8_t dat = buf[i];
149 for (j = 0; j < 8; j += bits, act += 2) {
150 uint8_t tmp = (dat >> j) & msk;
151 if (tmp == msk)
152 continue;
153 if (reserved_block_code &&
154 (tmp == reserved_block_code)) {
155 printk (KERN_DEBUG "nand_read_bbt: Reserved block at 0x%08x\n",
156 ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
157 this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
158 continue;
159 }
160 /* Leave it for now, if its matured we can move this
161 * message to MTD_DEBUG_LEVEL0 */
162 printk (KERN_DEBUG "nand_read_bbt: Bad block at 0x%08x\n",
163 ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200164 /* Factory marked bad or worn out ? */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200165 if (tmp == 0)
166 this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
167 else
168 this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200169 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200170 }
171 totlen -= len;
172 from += len;
173 }
174 return 0;
175}
176
177/**
178 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
179 * @mtd: MTD device structure
180 * @buf: temporary buffer
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200181 * @td: descriptor for the bad block table
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200182 * @chip: read the table for a specific chip, -1 read all chips.
183 * Applies only if NAND_BBT_PERCHIP option is set
184 *
185 * Read the bad block table for all chips starting at a given page
186 * We assume that the bbt bits are in consecutive order.
187*/
188static int read_abs_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
189{
190 struct nand_chip *this = mtd->priv;
191 int res = 0, i;
192 int bits;
193
194 bits = td->options & NAND_BBT_NRBITS_MSK;
195 if (td->options & NAND_BBT_PERCHIP) {
196 int offs = 0;
197 for (i = 0; i < this->numchips; i++) {
198 if (chip == -1 || chip == i)
199 res = read_bbt (mtd, buf, td->pages[i], this->chipsize >> this->bbt_erase_shift, bits, offs, td->reserved_block_code);
200 if (res)
201 return res;
202 offs += this->chipsize >> (this->bbt_erase_shift + 2);
203 }
204 } else {
205 res = read_bbt (mtd, buf, td->pages[0], mtd->size >> this->bbt_erase_shift, bits, 0, td->reserved_block_code);
206 if (res)
207 return res;
208 }
209 return 0;
210}
211
212/**
213 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
214 * @mtd: MTD device structure
215 * @buf: temporary buffer
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200216 * @td: descriptor for the bad block table
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200217 * @md: descriptor for the bad block table mirror
218 *
219 * Read the bad block table(s) for all chips starting at a given page
220 * We assume that the bbt bits are in consecutive order.
221 *
222*/
223static int read_abs_bbts (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td,
224 struct nand_bbt_descr *md)
225{
226 struct nand_chip *this = mtd->priv;
227
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200228 /* Read the primary version, if available */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200229 if (td->options & NAND_BBT_VERSION) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200230 nand_read_raw (mtd, buf, td->pages[0] << this->page_shift, mtd->oobblock, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200231 td->version[0] = buf[mtd->oobblock + td->veroffs];
232 printk (KERN_DEBUG "Bad block table at page %d, version 0x%02X\n", td->pages[0], td->version[0]);
233 }
234
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200235 /* Read the mirror version, if available */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200236 if (md && (md->options & NAND_BBT_VERSION)) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200237 nand_read_raw (mtd, buf, md->pages[0] << this->page_shift, mtd->oobblock, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200238 md->version[0] = buf[mtd->oobblock + md->veroffs];
239 printk (KERN_DEBUG "Bad block table at page %d, version 0x%02X\n", md->pages[0], md->version[0]);
240 }
241
242 return 1;
243}
244
245/**
246 * create_bbt - [GENERIC] Create a bad block table by scanning the device
247 * @mtd: MTD device structure
248 * @buf: temporary buffer
249 * @bd: descriptor for the good/bad block search pattern
250 * @chip: create the table for a specific chip, -1 read all chips.
251 * Applies only if NAND_BBT_PERCHIP option is set
252 *
253 * Create a bad block table by scanning the device
254 * for the given good/bad block identify pattern
255 */
256static void create_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd, int chip)
257{
258 struct nand_chip *this = mtd->priv;
259 int i, j, numblocks, len, scanlen;
260 int startblock;
261 loff_t from;
262 size_t readlen, ooblen;
263
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200264 if (bd->options & NAND_BBT_SCANALLPAGES)
265 len = 1 << (this->bbt_erase_shift - this->page_shift);
266 else {
267 if (bd->options & NAND_BBT_SCAN2NDPAGE)
268 len = 2;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200269 else
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200270 len = 1;
271 }
272 scanlen = mtd->oobblock + mtd->oobsize;
273 readlen = len * mtd->oobblock;
274 ooblen = len * mtd->oobsize;
275
276 if (chip == -1) {
277 /* Note that numblocks is 2 * (real numblocks) here, see i+=2 below as it
278 * makes shifting and masking less painful */
279 numblocks = mtd->size >> (this->bbt_erase_shift - 1);
280 startblock = 0;
281 from = 0;
282 } else {
283 if (chip >= this->numchips) {
284 printk (KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n",
285 chip + 1, this->numchips);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200286 return;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200287 }
288 numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
289 startblock = chip * numblocks;
290 numblocks += startblock;
291 from = startblock << (this->bbt_erase_shift - 1);
292 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200293
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200294 for (i = startblock; i < numblocks;) {
295 nand_read_raw (mtd, buf, from, readlen, ooblen);
296 for (j = 0; j < len; j++) {
297 if (check_pattern (&buf[j * scanlen], scanlen, mtd->oobblock, bd)) {
298 this->bbt[i >> 3] |= 0x03 << (i & 0x6);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200299 break;
300 }
301 }
302 i += 2;
303 from += (1 << this->bbt_erase_shift);
304 }
305}
306
307/**
308 * search_bbt - [GENERIC] scan the device for a specific bad block table
309 * @mtd: MTD device structure
310 * @buf: temporary buffer
311 * @td: descriptor for the bad block table
312 *
313 * Read the bad block table by searching for a given ident pattern.
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200314 * Search is preformed either from the beginning up or from the end of
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200315 * the device downwards. The search starts always at the start of a
316 * block.
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200317 * If the option NAND_BBT_PERCHIP is given, each chip is searched
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200318 * for a bbt, which contains the bad block information of this chip.
319 * This is neccecary to provide support for certain DOC devices.
320 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200321 * The bbt ident pattern resides in the oob area of the first page
322 * in a block.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200323 */
324static int search_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
325{
326 struct nand_chip *this = mtd->priv;
327 int i, chips;
328 int bits, startblock, block, dir;
329 int scanlen = mtd->oobblock + mtd->oobsize;
330 int bbtblocks;
331
332 /* Search direction top -> down ? */
333 if (td->options & NAND_BBT_LASTBLOCK) {
334 startblock = (mtd->size >> this->bbt_erase_shift) -1;
335 dir = -1;
336 } else {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200337 startblock = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200338 dir = 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200339 }
340
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200341 /* Do we have a bbt per chip ? */
342 if (td->options & NAND_BBT_PERCHIP) {
343 chips = this->numchips;
344 bbtblocks = this->chipsize >> this->bbt_erase_shift;
345 startblock &= bbtblocks - 1;
346 } else {
347 chips = 1;
348 bbtblocks = mtd->size >> this->bbt_erase_shift;
349 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200350
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200351 /* Number of bits for each erase block in the bbt */
352 bits = td->options & NAND_BBT_NRBITS_MSK;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200353
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200354 for (i = 0; i < chips; i++) {
355 /* Reset version information */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200356 td->version[i] = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200357 td->pages[i] = -1;
358 /* Scan the maximum number of blocks */
359 for (block = 0; block < td->maxblocks; block++) {
360 int actblock = startblock + dir * block;
361 /* Read first page */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200362 nand_read_raw (mtd, buf, actblock << this->bbt_erase_shift, mtd->oobblock, mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200363 if (!check_pattern(buf, scanlen, mtd->oobblock, td)) {
364 td->pages[i] = actblock << (this->bbt_erase_shift - this->page_shift);
365 if (td->options & NAND_BBT_VERSION) {
366 td->version[i] = buf[mtd->oobblock + td->veroffs];
367 }
368 break;
369 }
370 }
371 startblock += this->chipsize >> this->bbt_erase_shift;
372 }
373 /* Check, if we found a bbt for each requested chip */
374 for (i = 0; i < chips; i++) {
375 if (td->pages[i] == -1)
376 printk (KERN_WARNING "Bad block table not found for chip %d\n", i);
377 else
378 printk (KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n", td->pages[i], td->version[i]);
379 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200380 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200381}
382
383/**
384 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
385 * @mtd: MTD device structure
386 * @buf: temporary buffer
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200387 * @td: descriptor for the bad block table
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200388 * @md: descriptor for the bad block table mirror
389 *
390 * Search and read the bad block table(s)
391*/
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200392static int search_read_bbts (struct mtd_info *mtd, uint8_t *buf,
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200393 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
394{
395 /* Search the primary table */
396 search_bbt (mtd, buf, td);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200397
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200398 /* Search the mirror table */
399 if (md)
400 search_bbt (mtd, buf, md);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200401
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200402 /* Force result check */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200403 return 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200404}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200405
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200406
407/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200408 * write_bbt - [GENERIC] (Re)write the bad block table
409 *
410 * @mtd: MTD device structure
411 * @buf: temporary buffer
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200412 * @td: descriptor for the bad block table
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200413 * @md: descriptor for the bad block table mirror
414 * @chipsel: selector for a specific chip, -1 for all
415 *
416 * (Re)write the bad block table
417 *
418*/
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200419static int write_bbt (struct mtd_info *mtd, uint8_t *buf,
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200420 struct nand_bbt_descr *td, struct nand_bbt_descr *md, int chipsel)
421{
422 struct nand_chip *this = mtd->priv;
423 struct nand_oobinfo oobinfo;
424 struct erase_info einfo;
425 int i, j, res, chip = 0;
426 int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
427 int nrchips, bbtoffs, pageoffs;
428 uint8_t msk[4];
429 uint8_t rcode = td->reserved_block_code;
430 size_t retlen, len = 0;
431 loff_t to;
432
433 if (!rcode)
434 rcode = 0xff;
435 /* Write bad block table per chip rather than per device ? */
436 if (td->options & NAND_BBT_PERCHIP) {
437 numblocks = (int) (this->chipsize >> this->bbt_erase_shift);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200438 /* Full device write or specific chip ? */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200439 if (chipsel == -1) {
440 nrchips = this->numchips;
441 } else {
442 nrchips = chipsel + 1;
443 chip = chipsel;
444 }
445 } else {
446 numblocks = (int) (mtd->size >> this->bbt_erase_shift);
447 nrchips = 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200448 }
449
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200450 /* Loop through the chips */
451 for (; chip < nrchips; chip++) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200452
453 /* There was already a version of the table, reuse the page
454 * This applies for absolute placement too, as we have the
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200455 * page nr. in td->pages.
456 */
457 if (td->pages[chip] != -1) {
458 page = td->pages[chip];
459 goto write;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200460 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200461
462 /* Automatic placement of the bad block table */
463 /* Search direction top -> down ? */
464 if (td->options & NAND_BBT_LASTBLOCK) {
465 startblock = numblocks * (chip + 1) - 1;
466 dir = -1;
467 } else {
468 startblock = chip * numblocks;
469 dir = 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200470 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200471
472 for (i = 0; i < td->maxblocks; i++) {
473 int block = startblock + dir * i;
474 /* Check, if the block is bad */
475 switch ((this->bbt[block >> 2] >> (2 * (block & 0x03))) & 0x03) {
476 case 0x01:
477 case 0x03:
478 continue;
479 }
480 page = block << (this->bbt_erase_shift - this->page_shift);
481 /* Check, if the block is used by the mirror table */
482 if (!md || md->pages[chip] != page)
483 goto write;
484 }
485 printk (KERN_ERR "No space left to write bad block table\n");
486 return -ENOSPC;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200487write:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200488
489 /* Set up shift count and masks for the flash table */
490 bits = td->options & NAND_BBT_NRBITS_MSK;
491 switch (bits) {
492 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01; msk[2] = ~rcode; msk[3] = 0x01; break;
493 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01; msk[2] = ~rcode; msk[3] = 0x03; break;
494 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C; msk[2] = ~rcode; msk[3] = 0x0f; break;
495 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F; msk[2] = ~rcode; msk[3] = 0xff; break;
496 default: return -EINVAL;
497 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200498
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200499 bbtoffs = chip * (numblocks >> 2);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200500
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200501 to = ((loff_t) page) << this->page_shift;
502
503 memcpy (&oobinfo, this->autooob, sizeof(oobinfo));
504 oobinfo.useecc = MTD_NANDECC_PLACEONLY;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200505
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200506 /* Must we save the block contents ? */
507 if (td->options & NAND_BBT_SAVECONTENT) {
508 /* Make it block aligned */
509 to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1));
510 len = 1 << this->bbt_erase_shift;
511 res = mtd->read_ecc (mtd, to, len, &retlen, buf, &buf[len], &oobinfo);
512 if (res < 0) {
513 if (retlen != len) {
514 printk (KERN_INFO "nand_bbt: Error reading block for writing the bad block table\n");
515 return res;
516 }
517 printk (KERN_WARNING "nand_bbt: ECC error while reading block for writing bad block table\n");
518 }
519 /* Calc the byte offset in the buffer */
520 pageoffs = page - (int)(to >> this->page_shift);
521 offs = pageoffs << this->page_shift;
522 /* Preset the bbt area with 0xff */
523 memset (&buf[offs], 0xff, (size_t)(numblocks >> sft));
524 /* Preset the bbt's oob area with 0xff */
525 memset (&buf[len + pageoffs * mtd->oobsize], 0xff,
526 ((len >> this->page_shift) - pageoffs) * mtd->oobsize);
527 if (td->options & NAND_BBT_VERSION) {
528 buf[len + (pageoffs * mtd->oobsize) + td->veroffs] = td->version[chip];
529 }
530 } else {
531 /* Calc length */
532 len = (size_t) (numblocks >> sft);
533 /* Make it page aligned ! */
534 len = (len + (mtd->oobblock-1)) & ~(mtd->oobblock-1);
535 /* Preset the buffer with 0xff */
536 memset (buf, 0xff, len + (len >> this->page_shift) * mtd->oobsize);
537 offs = 0;
538 /* Pattern is located in oob area of first page */
539 memcpy (&buf[len + td->offs], td->pattern, td->len);
540 if (td->options & NAND_BBT_VERSION) {
541 buf[len + td->veroffs] = td->version[chip];
542 }
543 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200544
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200545 /* walk through the memory table */
546 for (i = 0; i < numblocks; ) {
547 uint8_t dat;
548 dat = this->bbt[bbtoffs + (i >> 2)];
549 for (j = 0; j < 4; j++ , i++) {
550 int sftcnt = (i << (3 - sft)) & sftmsk;
551 /* Do not store the reserved bbt blocks ! */
552 buf[offs + (i >> sft)] &= ~(msk[dat & 0x03] << sftcnt);
553 dat >>= 2;
554 }
555 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200556
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200557 memset (&einfo, 0, sizeof (einfo));
558 einfo.mtd = mtd;
559 einfo.addr = (unsigned long) to;
560 einfo.len = 1 << this->bbt_erase_shift;
561 res = nand_erase_nand (mtd, &einfo, 1);
562 if (res < 0) {
563 printk (KERN_WARNING "nand_bbt: Error during block erase: %d\n", res);
564 return res;
565 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200566
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200567 res = mtd->write_ecc (mtd, to, len, &retlen, buf, &buf[len], &oobinfo);
568 if (res < 0) {
569 printk (KERN_WARNING "nand_bbt: Error while writing bad block table %d\n", res);
570 return res;
571 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200572 printk (KERN_DEBUG "Bad block table written to 0x%08x, version 0x%02X\n",
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200573 (unsigned int) to, td->version[chip]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200574
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200575 /* Mark it as used */
576 td->pages[chip] = page;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200577 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200578 return 0;
579}
580
581/**
582 * nand_memory_bbt - [GENERIC] create a memory based bad block table
583 * @mtd: MTD device structure
584 * @bd: descriptor for the good/bad block search pattern
585 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200586 * The function creates a memory based bbt by scanning the device
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200587 * for manufacturer / software marked good / bad blocks
588*/
589static int nand_memory_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
590{
591 struct nand_chip *this = mtd->priv;
592
593 /* Ensure that we only scan for the pattern and nothing else */
594 bd->options = 0;
595 create_bbt (mtd, this->data_buf, bd, -1);
596 return 0;
597}
598
599/**
600 * check_create - [GENERIC] create and write bbt(s) if neccecary
601 * @mtd: MTD device structure
602 * @buf: temporary buffer
603 * @bd: descriptor for the good/bad block search pattern
604 *
605 * The function checks the results of the previous call to read_bbt
606 * and creates / updates the bbt(s) if neccecary
607 * Creation is neccecary if no bbt was found for the chip/device
608 * Update is neccecary if one of the tables is missing or the
609 * version nr. of one table is less than the other
610*/
611static int check_create (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
612{
613 int i, chips, writeops, chipsel, res;
614 struct nand_chip *this = mtd->priv;
615 struct nand_bbt_descr *td = this->bbt_td;
616 struct nand_bbt_descr *md = this->bbt_md;
617 struct nand_bbt_descr *rd, *rd2;
618
619 /* Do we have a bbt per chip ? */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200620 if (td->options & NAND_BBT_PERCHIP)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200621 chips = this->numchips;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200622 else
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200623 chips = 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200624
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200625 for (i = 0; i < chips; i++) {
626 writeops = 0;
627 rd = NULL;
628 rd2 = NULL;
629 /* Per chip or per device ? */
630 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
631 /* Mirrored table avilable ? */
632 if (md) {
633 if (td->pages[i] == -1 && md->pages[i] == -1) {
634 writeops = 0x03;
635 goto create;
636 }
637
638 if (td->pages[i] == -1) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200639 rd = md;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200640 td->version[i] = md->version[i];
641 writeops = 1;
642 goto writecheck;
643 }
644
645 if (md->pages[i] == -1) {
646 rd = td;
647 md->version[i] = td->version[i];
648 writeops = 2;
649 goto writecheck;
650 }
651
652 if (td->version[i] == md->version[i]) {
653 rd = td;
654 if (!(td->options & NAND_BBT_VERSION))
655 rd2 = md;
656 goto writecheck;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200657 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200658
659 if (((int8_t) (td->version[i] - md->version[i])) > 0) {
660 rd = td;
661 md->version[i] = td->version[i];
662 writeops = 2;
663 } else {
664 rd = md;
665 td->version[i] = md->version[i];
666 writeops = 1;
667 }
668
669 goto writecheck;
670
671 } else {
672 if (td->pages[i] == -1) {
673 writeops = 0x01;
674 goto create;
675 }
676 rd = td;
677 goto writecheck;
678 }
679create:
680 /* Create the bad block table by scanning the device ? */
681 if (!(td->options & NAND_BBT_CREATE))
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200682 continue;
683
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200684 /* Create the table in memory by scanning the chip(s) */
685 create_bbt (mtd, buf, bd, chipsel);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200686
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200687 td->version[i] = 1;
688 if (md)
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200689 md->version[i] = 1;
690writecheck:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200691 /* read back first ? */
692 if (rd)
693 read_abs_bbt (mtd, buf, rd, chipsel);
694 /* If they weren't versioned, read both. */
695 if (rd2)
696 read_abs_bbt (mtd, buf, rd2, chipsel);
697
698 /* Write the bad block table to the device ? */
699 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
700 res = write_bbt (mtd, buf, td, md, chipsel);
701 if (res < 0)
702 return res;
703 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200704
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200705 /* Write the mirror bad block table to the device ? */
706 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
707 res = write_bbt (mtd, buf, md, td, chipsel);
708 if (res < 0)
709 return res;
710 }
711 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200712 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200713}
714
715/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200716 * mark_bbt_regions - [GENERIC] mark the bad block table regions
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200717 * @mtd: MTD device structure
718 * @td: bad block table descriptor
719 *
720 * The bad block table regions are marked as "bad" to prevent
721 * accidental erasures / writes. The regions are identified by
722 * the mark 0x02.
723*/
724static void mark_bbt_region (struct mtd_info *mtd, struct nand_bbt_descr *td)
725{
726 struct nand_chip *this = mtd->priv;
727 int i, j, chips, block, nrblocks, update;
728 uint8_t oldval, newval;
729
730 /* Do we have a bbt per chip ? */
731 if (td->options & NAND_BBT_PERCHIP) {
732 chips = this->numchips;
733 nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
734 } else {
735 chips = 1;
736 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200737 }
738
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200739 for (i = 0; i < chips; i++) {
740 if ((td->options & NAND_BBT_ABSPAGE) ||
741 !(td->options & NAND_BBT_WRITE)) {
742 if (td->pages[i] == -1) continue;
743 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200744 block <<= 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200745 oldval = this->bbt[(block >> 3)];
746 newval = oldval | (0x2 << (block & 0x06));
747 this->bbt[(block >> 3)] = newval;
748 if ((oldval != newval) && td->reserved_block_code)
749 nand_update_bbt(mtd, block << (this->bbt_erase_shift - 1));
750 continue;
751 }
752 update = 0;
753 if (td->options & NAND_BBT_LASTBLOCK)
754 block = ((i + 1) * nrblocks) - td->maxblocks;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200755 else
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200756 block = i * nrblocks;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200757 block <<= 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200758 for (j = 0; j < td->maxblocks; j++) {
759 oldval = this->bbt[(block >> 3)];
760 newval = oldval | (0x2 << (block & 0x06));
761 this->bbt[(block >> 3)] = newval;
762 if (oldval != newval) update = 1;
763 block += 2;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200764 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200765 /* If we want reserved blocks to be recorded to flash, and some
766 new ones have been marked, then we need to update the stored
767 bbts. This should only happen once. */
768 if (update && td->reserved_block_code)
769 nand_update_bbt(mtd, (block - 2) << (this->bbt_erase_shift - 1));
770 }
771}
772
773/**
774 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
775 * @mtd: MTD device structure
776 * @bd: descriptor for the good/bad block search pattern
777 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200778 * The function checks, if a bad block table(s) is/are already
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200779 * available. If not it scans the device for manufacturer
780 * marked good / bad blocks and writes the bad block table(s) to
781 * the selected place.
782 *
783 * The bad block table memory is allocated here. It must be freed
784 * by calling the nand_free_bbt function.
785 *
786*/
787int nand_scan_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
788{
789 struct nand_chip *this = mtd->priv;
790 int len, res = 0;
791 uint8_t *buf;
792 struct nand_bbt_descr *td = this->bbt_td;
793 struct nand_bbt_descr *md = this->bbt_md;
794
795 len = mtd->size >> (this->bbt_erase_shift + 2);
796 /* Allocate memory (2bit per block) */
797 this->bbt = kmalloc (len, GFP_KERNEL);
798 if (!this->bbt) {
799 printk (KERN_ERR "nand_scan_bbt: Out of memory\n");
800 return -ENOMEM;
801 }
802 /* Clear the memory bad block table */
803 memset (this->bbt, 0x00, len);
804
805 /* If no primary table decriptor is given, scan the device
806 * to build a memory based bad block table
807 */
808 if (!td)
809 return nand_memory_bbt(mtd, bd);
810
811 /* Allocate a temporary buffer for one eraseblock incl. oob */
812 len = (1 << this->bbt_erase_shift);
813 len += (len >> this->page_shift) * mtd->oobsize;
814 buf = kmalloc (len, GFP_KERNEL);
815 if (!buf) {
816 printk (KERN_ERR "nand_bbt: Out of memory\n");
817 kfree (this->bbt);
818 this->bbt = NULL;
819 return -ENOMEM;
820 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200821
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200822 /* Is the bbt at a given page ? */
823 if (td->options & NAND_BBT_ABSPAGE) {
824 res = read_abs_bbts (mtd, buf, td, md);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200825 } else {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200826 /* Search the bad block table using a pattern in oob */
827 res = search_read_bbts (mtd, buf, td, md);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200828 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200829
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200830 if (res)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200831 res = check_create (mtd, buf, bd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200832
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200833 /* Prevent the bbt regions from erasing / writing */
834 mark_bbt_region (mtd, td);
835 if (md)
836 mark_bbt_region (mtd, md);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200837
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200838 kfree (buf);
839 return res;
840}
841
842
843/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200844 * nand_update_bbt - [NAND Interface] update bad block table(s)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200845 * @mtd: MTD device structure
846 * @offs: the offset of the newly marked block
847 *
848 * The function updates the bad block table(s)
849*/
850int nand_update_bbt (struct mtd_info *mtd, loff_t offs)
851{
852 struct nand_chip *this = mtd->priv;
853 int len, res = 0, writeops = 0;
854 int chip, chipsel;
855 uint8_t *buf;
856 struct nand_bbt_descr *td = this->bbt_td;
857 struct nand_bbt_descr *md = this->bbt_md;
858
859 if (!this->bbt || !td)
860 return -EINVAL;
861
862 len = mtd->size >> (this->bbt_erase_shift + 2);
863 /* Allocate a temporary buffer for one eraseblock incl. oob */
864 len = (1 << this->bbt_erase_shift);
865 len += (len >> this->page_shift) * mtd->oobsize;
866 buf = kmalloc (len, GFP_KERNEL);
867 if (!buf) {
868 printk (KERN_ERR "nand_update_bbt: Out of memory\n");
869 return -ENOMEM;
870 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200871
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200872 writeops = md != NULL ? 0x03 : 0x01;
873
874 /* Do we have a bbt per chip ? */
875 if (td->options & NAND_BBT_PERCHIP) {
876 chip = (int) (offs >> this->chip_shift);
877 chipsel = chip;
878 } else {
879 chip = 0;
880 chipsel = -1;
881 }
882
883 td->version[chip]++;
884 if (md)
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200885 md->version[chip]++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200886
887 /* Write the bad block table to the device ? */
888 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
889 res = write_bbt (mtd, buf, td, md, chipsel);
890 if (res < 0)
891 goto out;
892 }
893 /* Write the mirror bad block table to the device ? */
894 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
895 res = write_bbt (mtd, buf, md, td, chipsel);
896 }
897
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200898out:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200899 kfree (buf);
900 return res;
901}
902
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200903/* Define some generic bad / good block scan pattern which are used
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200904 * while scanning a device for factory marked good / bad blocks
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200905 *
906 * The memory based patterns just
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200907 */
908static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
909
910static struct nand_bbt_descr smallpage_memorybased = {
911 .options = 0,
912 .offs = 5,
913 .len = 1,
914 .pattern = scan_ff_pattern
915};
916
917static struct nand_bbt_descr largepage_memorybased = {
918 .options = 0,
919 .offs = 0,
920 .len = 2,
921 .pattern = scan_ff_pattern
922};
923
924static struct nand_bbt_descr smallpage_flashbased = {
925 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
926 .offs = 5,
927 .len = 1,
928 .pattern = scan_ff_pattern
929};
930
931static struct nand_bbt_descr largepage_flashbased = {
932 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
933 .offs = 0,
934 .len = 2,
935 .pattern = scan_ff_pattern
936};
937
938static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
939
940static struct nand_bbt_descr agand_flashbased = {
941 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
942 .offs = 0x20,
943 .len = 6,
944 .pattern = scan_agand_pattern
945};
946
947/* Generic flash bbt decriptors
948*/
949static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
950static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
951
952static struct nand_bbt_descr bbt_main_descr = {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200953 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200954 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
955 .offs = 8,
956 .len = 4,
957 .veroffs = 12,
958 .maxblocks = 4,
959 .pattern = bbt_pattern
960};
961
962static struct nand_bbt_descr bbt_mirror_descr = {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200963 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200964 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
965 .offs = 8,
966 .len = 4,
967 .veroffs = 12,
968 .maxblocks = 4,
969 .pattern = mirror_pattern
970};
971
972/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200973 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200974 * @mtd: MTD device structure
975 *
976 * This function selects the default bad block table
977 * support for the device and calls the nand_scan_bbt function
978 *
979*/
980int nand_default_bbt (struct mtd_info *mtd)
981{
982 struct nand_chip *this = mtd->priv;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200983
984 /* Default for AG-AND. We must use a flash based
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200985 * bad block table as the devices have factory marked
986 * _good_ blocks. Erasing those blocks leads to loss
987 * of the good / bad information, so we _must_ store
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200988 * this information in a good / bad table during
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200989 * startup
990 */
991 if (this->options & NAND_IS_AND) {
992 /* Use the default pattern descriptors */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200993 if (!this->bbt_td) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200994 this->bbt_td = &bbt_main_descr;
995 this->bbt_md = &bbt_mirror_descr;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200996 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200997 this->options |= NAND_USE_FLASH_BBT;
998 return nand_scan_bbt (mtd, &agand_flashbased);
999 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001000
1001
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001002 /* Is a flash based bad block table requested ? */
1003 if (this->options & NAND_USE_FLASH_BBT) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001004 /* Use the default pattern descriptors */
1005 if (!this->bbt_td) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001006 this->bbt_td = &bbt_main_descr;
1007 this->bbt_md = &bbt_mirror_descr;
1008 }
1009 if (!this->badblock_pattern) {
1010 this->badblock_pattern = (mtd->oobblock > 512) ?
1011 &largepage_flashbased : &smallpage_flashbased;
1012 }
1013 } else {
1014 this->bbt_td = NULL;
1015 this->bbt_md = NULL;
1016 if (!this->badblock_pattern) {
1017 this->badblock_pattern = (mtd->oobblock > 512) ?
1018 &largepage_memorybased : &smallpage_memorybased;
1019 }
1020 }
1021 return nand_scan_bbt (mtd, this->badblock_pattern);
1022}
1023
1024/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001025 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001026 * @mtd: MTD device structure
1027 * @offs: offset in the device
1028 * @allowbbt: allow access to bad block table region
1029 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001030 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001031int nand_isbad_bbt (struct mtd_info *mtd, loff_t offs, int allowbbt)
1032{
1033 struct nand_chip *this = mtd->priv;
1034 int block;
1035 uint8_t res;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001036
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001037 /* Get block number * 2 */
1038 block = (int) (offs >> (this->bbt_erase_shift - 1));
1039 res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
1040
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001041 DEBUG (MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001042 (unsigned int)offs, res, block >> 1);
1043
1044 switch ((int)res) {
1045 case 0x00: return 0;
1046 case 0x01: return 1;
1047 case 0x02: return allowbbt ? 0 : 1;
1048 }
1049 return 1;
1050}
1051
1052#endif