blob: ca286999fe08ab3b39e89f6f2963bc708b440569 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/mtd/nand_bbt.c
3 *
4 * Overview:
5 * Bad block table support for the NAND driver
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
8 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00009 * $Id: nand_bbt.c,v 1.36 2005/11/07 11:14:30 gleixner Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
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 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000017 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * mirrored bbt is selected then the mirror is searched too and the
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000021 * versions are compared. If the mirror has a greater version number
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * 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.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000024 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000028 * For manufacturer created bbts like the one found on M-SYS DOC devices
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * the bbt is searched and read but never created
30 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000031 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * 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
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000046 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * 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
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000052 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 */
54
55#include <linux/slab.h>
56#include <linux/types.h>
57#include <linux/mtd/mtd.h>
58#include <linux/mtd/nand.h>
59#include <linux/mtd/nand_ecc.h>
60#include <linux/mtd/compatmac.h>
61#include <linux/bitops.h>
62#include <linux/delay.h>
63
64
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000065/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * check_pattern - [GENERIC] check if a pattern is in the buffer
67 * @buf: the buffer to search
68 * @len: the length of buffer to search
69 * @paglen: the pagelength
70 * @td: search pattern descriptor
71 *
72 * Check for a pattern at the given place. Used to search bad block
73 * tables and good / bad block identifiers.
74 * If the SCAN_EMPTY option is set then check, if all bytes except the
75 * pattern area contain 0xff
76 *
77*/
78static int check_pattern (uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
79{
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +000080 int i, end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 uint8_t *p = buf;
82
Thomas Gleixnerc9e053652005-06-14 16:39:57 +010083 end = paglen + td->offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (td->options & NAND_BBT_SCANEMPTY) {
85 for (i = 0; i < end; i++) {
86 if (p[i] != 0xff)
87 return -1;
88 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000089 }
Thomas Gleixnerc9e053652005-06-14 16:39:57 +010090 p += end;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 /* Compare the pattern */
93 for (i = 0; i < td->len; i++) {
94 if (p[i] != td->pattern[i])
95 return -1;
96 }
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 if (td->options & NAND_BBT_SCANEMPTY) {
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +000099 p += td->len;
100 end += td->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 for (i = end; i < len; i++) {
102 if (*p++ != 0xff)
103 return -1;
104 }
105 }
106 return 0;
107}
108
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000109/**
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100110 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
111 * @buf: the buffer to search
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100112 * @td: search pattern descriptor
113 *
114 * Check for a pattern at the given place. Used to search bad block
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000115 * tables and good / bad block identifiers. Same as check_pattern, but
Thomas Gleixner19870da2005-07-15 14:53:51 +0100116 * no optional empty check
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100117 *
118*/
Thomas Gleixner19870da2005-07-15 14:53:51 +0100119static int check_short_pattern (uint8_t *buf, struct nand_bbt_descr *td)
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100120{
121 int i;
122 uint8_t *p = buf;
123
124 /* Compare the pattern */
125 for (i = 0; i < td->len; i++) {
Thomas Gleixner19870da2005-07-15 14:53:51 +0100126 if (p[td->offs + i] != td->pattern[i])
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100127 return -1;
128 }
129 return 0;
130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/**
133 * read_bbt - [GENERIC] Read the bad block table starting from page
134 * @mtd: MTD device structure
135 * @buf: temporary buffer
136 * @page: the starting page
137 * @num: the number of bbt descriptors to read
138 * @bits: number of bits per block
139 * @offs: offset in the memory table
140 * @reserved_block_code: Pattern to identify reserved blocks
141 *
142 * Read the bad block table starting from page.
143 *
144 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000145static int read_bbt (struct mtd_info *mtd, uint8_t *buf, int page, int num,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 int bits, int offs, int reserved_block_code)
147{
148 int res, i, j, act = 0;
149 struct nand_chip *this = mtd->priv;
150 size_t retlen, len, totlen;
151 loff_t from;
152 uint8_t msk = (uint8_t) ((1 << bits) - 1);
153
154 totlen = (num * bits) >> 3;
155 from = ((loff_t)page) << this->page_shift;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 while (totlen) {
158 len = min (totlen, (size_t) (1 << this->bbt_erase_shift));
159 res = mtd->read_ecc (mtd, from, len, &retlen, buf, NULL, this->autooob);
160 if (res < 0) {
161 if (retlen != len) {
162 printk (KERN_INFO "nand_bbt: Error reading bad block table\n");
163 return res;
164 }
165 printk (KERN_WARNING "nand_bbt: ECC error while reading bad block table\n");
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 /* Analyse data */
169 for (i = 0; i < len; i++) {
170 uint8_t dat = buf[i];
171 for (j = 0; j < 8; j += bits, act += 2) {
172 uint8_t tmp = (dat >> j) & msk;
173 if (tmp == msk)
174 continue;
175 if (reserved_block_code &&
176 (tmp == reserved_block_code)) {
177 printk (KERN_DEBUG "nand_read_bbt: Reserved block at 0x%08x\n",
178 ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
179 this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
180 continue;
181 }
182 /* Leave it for now, if its matured we can move this
183 * message to MTD_DEBUG_LEVEL0 */
184 printk (KERN_DEBUG "nand_read_bbt: Bad block at 0x%08x\n",
185 ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000186 /* Factory marked bad or worn out ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (tmp == 0)
188 this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
189 else
190 this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193 totlen -= len;
194 from += len;
195 }
196 return 0;
197}
198
199/**
200 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
201 * @mtd: MTD device structure
202 * @buf: temporary buffer
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000203 * @td: descriptor for the bad block table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * @chip: read the table for a specific chip, -1 read all chips.
205 * Applies only if NAND_BBT_PERCHIP option is set
206 *
207 * Read the bad block table for all chips starting at a given page
208 * We assume that the bbt bits are in consecutive order.
209*/
210static int read_abs_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
211{
212 struct nand_chip *this = mtd->priv;
213 int res = 0, i;
214 int bits;
215
216 bits = td->options & NAND_BBT_NRBITS_MSK;
217 if (td->options & NAND_BBT_PERCHIP) {
218 int offs = 0;
219 for (i = 0; i < this->numchips; i++) {
220 if (chip == -1 || chip == i)
221 res = read_bbt (mtd, buf, td->pages[i], this->chipsize >> this->bbt_erase_shift, bits, offs, td->reserved_block_code);
222 if (res)
223 return res;
224 offs += this->chipsize >> (this->bbt_erase_shift + 2);
225 }
226 } else {
227 res = read_bbt (mtd, buf, td->pages[0], mtd->size >> this->bbt_erase_shift, bits, 0, td->reserved_block_code);
228 if (res)
229 return res;
230 }
231 return 0;
232}
233
234/**
235 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
236 * @mtd: MTD device structure
237 * @buf: temporary buffer
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000238 * @td: descriptor for the bad block table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * @md: descriptor for the bad block table mirror
240 *
241 * Read the bad block table(s) for all chips starting at a given page
242 * We assume that the bbt bits are in consecutive order.
243 *
244*/
245static int read_abs_bbts (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td,
246 struct nand_bbt_descr *md)
247{
248 struct nand_chip *this = mtd->priv;
249
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000250 /* Read the primary version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (td->options & NAND_BBT_VERSION) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000252 nand_read_raw (mtd, buf, td->pages[0] << this->page_shift, mtd->oobblock, mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 td->version[0] = buf[mtd->oobblock + td->veroffs];
254 printk (KERN_DEBUG "Bad block table at page %d, version 0x%02X\n", td->pages[0], td->version[0]);
255 }
256
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000257 /* Read the mirror version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (md && (md->options & NAND_BBT_VERSION)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000259 nand_read_raw (mtd, buf, md->pages[0] << this->page_shift, mtd->oobblock, mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 md->version[0] = buf[mtd->oobblock + md->veroffs];
261 printk (KERN_DEBUG "Bad block table at page %d, version 0x%02X\n", md->pages[0], md->version[0]);
262 }
263
264 return 1;
265}
266
267/**
268 * create_bbt - [GENERIC] Create a bad block table by scanning the device
269 * @mtd: MTD device structure
270 * @buf: temporary buffer
271 * @bd: descriptor for the good/bad block search pattern
272 * @chip: create the table for a specific chip, -1 read all chips.
273 * Applies only if NAND_BBT_PERCHIP option is set
274 *
275 * Create a bad block table by scanning the device
276 * for the given good/bad block identify pattern
277 */
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000278static int create_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 struct nand_chip *this = mtd->priv;
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000281 int i, j, numblocks, len, scanlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 int startblock;
283 loff_t from;
284 size_t readlen, ooblen;
285
286 printk (KERN_INFO "Scanning device for bad blocks\n");
287
288 if (bd->options & NAND_BBT_SCANALLPAGES)
289 len = 1 << (this->bbt_erase_shift - this->page_shift);
290 else {
291 if (bd->options & NAND_BBT_SCAN2NDPAGE)
292 len = 2;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000293 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 len = 1;
295 }
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000296
297 if (!(bd->options & NAND_BBT_SCANEMPTY)) {
298 /* We need only read few bytes from the OOB area */
299 scanlen = ooblen = 0;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000300 readlen = bd->len;
301 } else {
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000302 /* Full page content should be read */
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000303 scanlen = mtd->oobblock + mtd->oobsize;
304 readlen = len * mtd->oobblock;
305 ooblen = len * mtd->oobsize;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 if (chip == -1) {
309 /* Note that numblocks is 2 * (real numblocks) here, see i+=2 below as it
310 * makes shifting and masking less painful */
311 numblocks = mtd->size >> (this->bbt_erase_shift - 1);
312 startblock = 0;
313 from = 0;
314 } else {
315 if (chip >= this->numchips) {
316 printk (KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n",
317 chip + 1, this->numchips);
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000318 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320 numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
321 startblock = chip * numblocks;
322 numblocks += startblock;
323 from = startblock << (this->bbt_erase_shift - 1);
324 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 for (i = startblock; i < numblocks;) {
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000327 int ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000328
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000329 if (bd->options & NAND_BBT_SCANEMPTY)
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000330 if ((ret = nand_read_raw (mtd, buf, from, readlen, ooblen)))
331 return ret;
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 for (j = 0; j < len; j++) {
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000334 if (!(bd->options & NAND_BBT_SCANEMPTY)) {
335 size_t retlen;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000336
337 /* Read the full oob until read_oob is fixed to
Thomas Gleixner19870da2005-07-15 14:53:51 +0100338 * handle single byte reads for 16 bit buswidth */
339 ret = mtd->read_oob(mtd, from + j * mtd->oobblock,
340 mtd->oobsize, &retlen, buf);
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000341 if (ret)
342 return ret;
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100343
Thomas Gleixner19870da2005-07-15 14:53:51 +0100344 if (check_short_pattern (buf, bd)) {
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100345 this->bbt[i >> 3] |= 0x03 << (i & 0x6);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000346 printk (KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100347 i >> 1, (unsigned int) from);
348 break;
349 }
350 } else {
351 if (check_pattern (&buf[j * scanlen], scanlen, mtd->oobblock, bd)) {
352 this->bbt[i >> 3] |= 0x03 << (i & 0x6);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000353 printk (KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100354 i >> 1, (unsigned int) from);
355 break;
356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358 }
359 i += 2;
360 from += (1 << this->bbt_erase_shift);
361 }
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365/**
366 * search_bbt - [GENERIC] scan the device for a specific bad block table
367 * @mtd: MTD device structure
368 * @buf: temporary buffer
369 * @td: descriptor for the bad block table
370 *
371 * Read the bad block table by searching for a given ident pattern.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000372 * Search is preformed either from the beginning up or from the end of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 * the device downwards. The search starts always at the start of a
374 * block.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000375 * If the option NAND_BBT_PERCHIP is given, each chip is searched
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 * for a bbt, which contains the bad block information of this chip.
377 * This is neccecary to provide support for certain DOC devices.
378 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000379 * The bbt ident pattern resides in the oob area of the first page
380 * in a block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 */
382static int search_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
383{
384 struct nand_chip *this = mtd->priv;
385 int i, chips;
386 int bits, startblock, block, dir;
387 int scanlen = mtd->oobblock + mtd->oobsize;
388 int bbtblocks;
389
390 /* Search direction top -> down ? */
391 if (td->options & NAND_BBT_LASTBLOCK) {
392 startblock = (mtd->size >> this->bbt_erase_shift) -1;
393 dir = -1;
394 } else {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000395 startblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000397 }
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /* Do we have a bbt per chip ? */
400 if (td->options & NAND_BBT_PERCHIP) {
401 chips = this->numchips;
402 bbtblocks = this->chipsize >> this->bbt_erase_shift;
403 startblock &= bbtblocks - 1;
404 } else {
405 chips = 1;
406 bbtblocks = mtd->size >> this->bbt_erase_shift;
407 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /* Number of bits for each erase block in the bbt */
410 bits = td->options & NAND_BBT_NRBITS_MSK;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 for (i = 0; i < chips; i++) {
413 /* Reset version information */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000414 td->version[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 td->pages[i] = -1;
416 /* Scan the maximum number of blocks */
417 for (block = 0; block < td->maxblocks; block++) {
418 int actblock = startblock + dir * block;
419 /* Read first page */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000420 nand_read_raw (mtd, buf, actblock << this->bbt_erase_shift, mtd->oobblock, mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (!check_pattern(buf, scanlen, mtd->oobblock, td)) {
422 td->pages[i] = actblock << (this->bbt_erase_shift - this->page_shift);
423 if (td->options & NAND_BBT_VERSION) {
424 td->version[i] = buf[mtd->oobblock + td->veroffs];
425 }
426 break;
427 }
428 }
429 startblock += this->chipsize >> this->bbt_erase_shift;
430 }
431 /* Check, if we found a bbt for each requested chip */
432 for (i = 0; i < chips; i++) {
433 if (td->pages[i] == -1)
434 printk (KERN_WARNING "Bad block table not found for chip %d\n", i);
435 else
436 printk (KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n", td->pages[i], td->version[i]);
437 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000438 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
441/**
442 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
443 * @mtd: MTD device structure
444 * @buf: temporary buffer
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000445 * @td: descriptor for the bad block table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * @md: descriptor for the bad block table mirror
447 *
448 * Search and read the bad block table(s)
449*/
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000450static int search_read_bbts (struct mtd_info *mtd, uint8_t *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
452{
453 /* Search the primary table */
454 search_bbt (mtd, buf, td);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /* Search the mirror table */
457 if (md)
458 search_bbt (mtd, buf, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000460 /* Force result check */
461 return 1;
462}
463
464
465/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 * write_bbt - [GENERIC] (Re)write the bad block table
467 *
468 * @mtd: MTD device structure
469 * @buf: temporary buffer
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000470 * @td: descriptor for the bad block table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 * @md: descriptor for the bad block table mirror
472 * @chipsel: selector for a specific chip, -1 for all
473 *
474 * (Re)write the bad block table
475 *
476*/
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000477static int write_bbt (struct mtd_info *mtd, uint8_t *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 struct nand_bbt_descr *td, struct nand_bbt_descr *md, int chipsel)
479{
480 struct nand_chip *this = mtd->priv;
481 struct nand_oobinfo oobinfo;
482 struct erase_info einfo;
483 int i, j, res, chip = 0;
484 int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
485 int nrchips, bbtoffs, pageoffs;
486 uint8_t msk[4];
487 uint8_t rcode = td->reserved_block_code;
488 size_t retlen, len = 0;
489 loff_t to;
490
491 if (!rcode)
492 rcode = 0xff;
493 /* Write bad block table per chip rather than per device ? */
494 if (td->options & NAND_BBT_PERCHIP) {
495 numblocks = (int) (this->chipsize >> this->bbt_erase_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000496 /* Full device write or specific chip ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (chipsel == -1) {
498 nrchips = this->numchips;
499 } else {
500 nrchips = chipsel + 1;
501 chip = chipsel;
502 }
503 } else {
504 numblocks = (int) (mtd->size >> this->bbt_erase_shift);
505 nrchips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000506 }
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 /* Loop through the chips */
509 for (; chip < nrchips; chip++) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000510
511 /* There was already a version of the table, reuse the page
512 * This applies for absolute placement too, as we have the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 * page nr. in td->pages.
514 */
515 if (td->pages[chip] != -1) {
516 page = td->pages[chip];
517 goto write;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 /* Automatic placement of the bad block table */
521 /* Search direction top -> down ? */
522 if (td->options & NAND_BBT_LASTBLOCK) {
523 startblock = numblocks * (chip + 1) - 1;
524 dir = -1;
525 } else {
526 startblock = chip * numblocks;
527 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 for (i = 0; i < td->maxblocks; i++) {
531 int block = startblock + dir * i;
532 /* Check, if the block is bad */
533 switch ((this->bbt[block >> 2] >> (2 * (block & 0x03))) & 0x03) {
534 case 0x01:
535 case 0x03:
536 continue;
537 }
538 page = block << (this->bbt_erase_shift - this->page_shift);
539 /* Check, if the block is used by the mirror table */
540 if (!md || md->pages[chip] != page)
541 goto write;
542 }
543 printk (KERN_ERR "No space left to write bad block table\n");
544 return -ENOSPC;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000545write:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 /* Set up shift count and masks for the flash table */
548 bits = td->options & NAND_BBT_NRBITS_MSK;
549 switch (bits) {
550 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01; msk[2] = ~rcode; msk[3] = 0x01; break;
551 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01; msk[2] = ~rcode; msk[3] = 0x03; break;
552 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C; msk[2] = ~rcode; msk[3] = 0x0f; break;
553 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F; msk[2] = ~rcode; msk[3] = 0xff; break;
554 default: return -EINVAL;
555 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 bbtoffs = chip * (numblocks >> 2);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 to = ((loff_t) page) << this->page_shift;
560
561 memcpy (&oobinfo, this->autooob, sizeof(oobinfo));
562 oobinfo.useecc = MTD_NANDECC_PLACEONLY;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /* Must we save the block contents ? */
565 if (td->options & NAND_BBT_SAVECONTENT) {
566 /* Make it block aligned */
567 to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1));
568 len = 1 << this->bbt_erase_shift;
569 res = mtd->read_ecc (mtd, to, len, &retlen, buf, &buf[len], &oobinfo);
570 if (res < 0) {
571 if (retlen != len) {
572 printk (KERN_INFO "nand_bbt: Error reading block for writing the bad block table\n");
573 return res;
574 }
575 printk (KERN_WARNING "nand_bbt: ECC error while reading block for writing bad block table\n");
576 }
577 /* Calc the byte offset in the buffer */
578 pageoffs = page - (int)(to >> this->page_shift);
579 offs = pageoffs << this->page_shift;
580 /* Preset the bbt area with 0xff */
581 memset (&buf[offs], 0xff, (size_t)(numblocks >> sft));
582 /* Preset the bbt's oob area with 0xff */
583 memset (&buf[len + pageoffs * mtd->oobsize], 0xff,
584 ((len >> this->page_shift) - pageoffs) * mtd->oobsize);
585 if (td->options & NAND_BBT_VERSION) {
586 buf[len + (pageoffs * mtd->oobsize) + td->veroffs] = td->version[chip];
587 }
588 } else {
589 /* Calc length */
590 len = (size_t) (numblocks >> sft);
591 /* Make it page aligned ! */
592 len = (len + (mtd->oobblock-1)) & ~(mtd->oobblock-1);
593 /* Preset the buffer with 0xff */
594 memset (buf, 0xff, len + (len >> this->page_shift) * mtd->oobsize);
595 offs = 0;
596 /* Pattern is located in oob area of first page */
597 memcpy (&buf[len + td->offs], td->pattern, td->len);
598 if (td->options & NAND_BBT_VERSION) {
599 buf[len + td->veroffs] = td->version[chip];
600 }
601 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 /* walk through the memory table */
604 for (i = 0; i < numblocks; ) {
605 uint8_t dat;
606 dat = this->bbt[bbtoffs + (i >> 2)];
607 for (j = 0; j < 4; j++ , i++) {
608 int sftcnt = (i << (3 - sft)) & sftmsk;
609 /* Do not store the reserved bbt blocks ! */
610 buf[offs + (i >> sft)] &= ~(msk[dat & 0x03] << sftcnt);
611 dat >>= 2;
612 }
613 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 memset (&einfo, 0, sizeof (einfo));
616 einfo.mtd = mtd;
617 einfo.addr = (unsigned long) to;
618 einfo.len = 1 << this->bbt_erase_shift;
619 res = nand_erase_nand (mtd, &einfo, 1);
620 if (res < 0) {
621 printk (KERN_WARNING "nand_bbt: Error during block erase: %d\n", res);
622 return res;
623 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 res = mtd->write_ecc (mtd, to, len, &retlen, buf, &buf[len], &oobinfo);
626 if (res < 0) {
627 printk (KERN_WARNING "nand_bbt: Error while writing bad block table %d\n", res);
628 return res;
629 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000630 printk (KERN_DEBUG "Bad block table written to 0x%08x, version 0x%02X\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 (unsigned int) to, td->version[chip]);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /* Mark it as used */
634 td->pages[chip] = page;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return 0;
637}
638
639/**
640 * nand_memory_bbt - [GENERIC] create a memory based bad block table
641 * @mtd: MTD device structure
642 * @bd: descriptor for the good/bad block search pattern
643 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000644 * The function creates a memory based bbt by scanning the device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 * for manufacturer / software marked good / bad blocks
646*/
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000647static inline int nand_memory_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 struct nand_chip *this = mtd->priv;
650
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000651 bd->options &= ~NAND_BBT_SCANEMPTY;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000652 return create_bbt (mtd, this->data_buf, bd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
655/**
656 * check_create - [GENERIC] create and write bbt(s) if neccecary
657 * @mtd: MTD device structure
658 * @buf: temporary buffer
659 * @bd: descriptor for the good/bad block search pattern
660 *
661 * The function checks the results of the previous call to read_bbt
662 * and creates / updates the bbt(s) if neccecary
663 * Creation is neccecary if no bbt was found for the chip/device
664 * Update is neccecary if one of the tables is missing or the
665 * version nr. of one table is less than the other
666*/
667static int check_create (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
668{
669 int i, chips, writeops, chipsel, res;
670 struct nand_chip *this = mtd->priv;
671 struct nand_bbt_descr *td = this->bbt_td;
672 struct nand_bbt_descr *md = this->bbt_md;
673 struct nand_bbt_descr *rd, *rd2;
674
675 /* Do we have a bbt per chip ? */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000676 if (td->options & NAND_BBT_PERCHIP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 chips = this->numchips;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000678 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 chips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 for (i = 0; i < chips; i++) {
682 writeops = 0;
683 rd = NULL;
684 rd2 = NULL;
685 /* Per chip or per device ? */
686 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
687 /* Mirrored table avilable ? */
688 if (md) {
689 if (td->pages[i] == -1 && md->pages[i] == -1) {
690 writeops = 0x03;
691 goto create;
692 }
693
694 if (td->pages[i] == -1) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000695 rd = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 td->version[i] = md->version[i];
697 writeops = 1;
698 goto writecheck;
699 }
700
701 if (md->pages[i] == -1) {
702 rd = td;
703 md->version[i] = td->version[i];
704 writeops = 2;
705 goto writecheck;
706 }
707
708 if (td->version[i] == md->version[i]) {
709 rd = td;
710 if (!(td->options & NAND_BBT_VERSION))
711 rd2 = md;
712 goto writecheck;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 if (((int8_t) (td->version[i] - md->version[i])) > 0) {
716 rd = td;
717 md->version[i] = td->version[i];
718 writeops = 2;
719 } else {
720 rd = md;
721 td->version[i] = md->version[i];
722 writeops = 1;
723 }
724
725 goto writecheck;
726
727 } else {
728 if (td->pages[i] == -1) {
729 writeops = 0x01;
730 goto create;
731 }
732 rd = td;
733 goto writecheck;
734 }
735create:
736 /* Create the bad block table by scanning the device ? */
737 if (!(td->options & NAND_BBT_CREATE))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000738 continue;
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 /* Create the table in memory by scanning the chip(s) */
741 create_bbt (mtd, buf, bd, chipsel);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 td->version[i] = 1;
744 if (md)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000745 md->version[i] = 1;
746writecheck:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 /* read back first ? */
748 if (rd)
749 read_abs_bbt (mtd, buf, rd, chipsel);
750 /* If they weren't versioned, read both. */
751 if (rd2)
752 read_abs_bbt (mtd, buf, rd2, chipsel);
753
754 /* Write the bad block table to the device ? */
755 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
756 res = write_bbt (mtd, buf, td, md, chipsel);
757 if (res < 0)
758 return res;
759 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /* Write the mirror bad block table to the device ? */
762 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
763 res = write_bbt (mtd, buf, md, td, chipsel);
764 if (res < 0)
765 return res;
766 }
767 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000768 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
771/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000772 * mark_bbt_regions - [GENERIC] mark the bad block table regions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 * @mtd: MTD device structure
774 * @td: bad block table descriptor
775 *
776 * The bad block table regions are marked as "bad" to prevent
777 * accidental erasures / writes. The regions are identified by
778 * the mark 0x02.
779*/
780static void mark_bbt_region (struct mtd_info *mtd, struct nand_bbt_descr *td)
781{
782 struct nand_chip *this = mtd->priv;
783 int i, j, chips, block, nrblocks, update;
784 uint8_t oldval, newval;
785
786 /* Do we have a bbt per chip ? */
787 if (td->options & NAND_BBT_PERCHIP) {
788 chips = this->numchips;
789 nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
790 } else {
791 chips = 1;
792 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000793 }
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 for (i = 0; i < chips; i++) {
796 if ((td->options & NAND_BBT_ABSPAGE) ||
797 !(td->options & NAND_BBT_WRITE)) {
798 if (td->pages[i] == -1) continue;
799 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000800 block <<= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 oldval = this->bbt[(block >> 3)];
802 newval = oldval | (0x2 << (block & 0x06));
803 this->bbt[(block >> 3)] = newval;
804 if ((oldval != newval) && td->reserved_block_code)
805 nand_update_bbt(mtd, block << (this->bbt_erase_shift - 1));
806 continue;
807 }
808 update = 0;
809 if (td->options & NAND_BBT_LASTBLOCK)
810 block = ((i + 1) * nrblocks) - td->maxblocks;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000811 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 block = i * nrblocks;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000813 block <<= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 for (j = 0; j < td->maxblocks; j++) {
815 oldval = this->bbt[(block >> 3)];
816 newval = oldval | (0x2 << (block & 0x06));
817 this->bbt[(block >> 3)] = newval;
818 if (oldval != newval) update = 1;
819 block += 2;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 /* If we want reserved blocks to be recorded to flash, and some
822 new ones have been marked, then we need to update the stored
823 bbts. This should only happen once. */
824 if (update && td->reserved_block_code)
825 nand_update_bbt(mtd, (block - 2) << (this->bbt_erase_shift - 1));
826 }
827}
828
829/**
830 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
831 * @mtd: MTD device structure
832 * @bd: descriptor for the good/bad block search pattern
833 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000834 * The function checks, if a bad block table(s) is/are already
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 * available. If not it scans the device for manufacturer
836 * marked good / bad blocks and writes the bad block table(s) to
837 * the selected place.
838 *
839 * The bad block table memory is allocated here. It must be freed
840 * by calling the nand_free_bbt function.
841 *
842*/
843int nand_scan_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
844{
845 struct nand_chip *this = mtd->priv;
846 int len, res = 0;
847 uint8_t *buf;
848 struct nand_bbt_descr *td = this->bbt_td;
849 struct nand_bbt_descr *md = this->bbt_md;
850
851 len = mtd->size >> (this->bbt_erase_shift + 2);
852 /* Allocate memory (2bit per block) */
853 this->bbt = kmalloc (len, GFP_KERNEL);
854 if (!this->bbt) {
855 printk (KERN_ERR "nand_scan_bbt: Out of memory\n");
856 return -ENOMEM;
857 }
858 /* Clear the memory bad block table */
859 memset (this->bbt, 0x00, len);
860
861 /* If no primary table decriptor is given, scan the device
862 * to build a memory based bad block table
863 */
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000864 if (!td) {
865 if ((res = nand_memory_bbt(mtd, bd))) {
866 printk (KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n");
867 kfree (this->bbt);
868 this->bbt = NULL;
869 }
870 return res;
871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 /* Allocate a temporary buffer for one eraseblock incl. oob */
874 len = (1 << this->bbt_erase_shift);
875 len += (len >> this->page_shift) * mtd->oobsize;
876 buf = kmalloc (len, GFP_KERNEL);
877 if (!buf) {
878 printk (KERN_ERR "nand_bbt: Out of memory\n");
879 kfree (this->bbt);
880 this->bbt = NULL;
881 return -ENOMEM;
882 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 /* Is the bbt at a given page ? */
885 if (td->options & NAND_BBT_ABSPAGE) {
886 res = read_abs_bbts (mtd, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000887 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 /* Search the bad block table using a pattern in oob */
889 res = search_read_bbts (mtd, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000892 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 res = check_create (mtd, buf, bd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 /* Prevent the bbt regions from erasing / writing */
896 mark_bbt_region (mtd, td);
897 if (md)
898 mark_bbt_region (mtd, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 kfree (buf);
901 return res;
902}
903
904
905/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000906 * nand_update_bbt - [NAND Interface] update bad block table(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 * @mtd: MTD device structure
908 * @offs: the offset of the newly marked block
909 *
910 * The function updates the bad block table(s)
911*/
912int nand_update_bbt (struct mtd_info *mtd, loff_t offs)
913{
914 struct nand_chip *this = mtd->priv;
915 int len, res = 0, writeops = 0;
916 int chip, chipsel;
917 uint8_t *buf;
918 struct nand_bbt_descr *td = this->bbt_td;
919 struct nand_bbt_descr *md = this->bbt_md;
920
921 if (!this->bbt || !td)
922 return -EINVAL;
923
924 len = mtd->size >> (this->bbt_erase_shift + 2);
925 /* Allocate a temporary buffer for one eraseblock incl. oob */
926 len = (1 << this->bbt_erase_shift);
927 len += (len >> this->page_shift) * mtd->oobsize;
928 buf = kmalloc (len, GFP_KERNEL);
929 if (!buf) {
930 printk (KERN_ERR "nand_update_bbt: Out of memory\n");
931 return -ENOMEM;
932 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 writeops = md != NULL ? 0x03 : 0x01;
935
936 /* Do we have a bbt per chip ? */
937 if (td->options & NAND_BBT_PERCHIP) {
938 chip = (int) (offs >> this->chip_shift);
939 chipsel = chip;
940 } else {
941 chip = 0;
942 chipsel = -1;
943 }
944
945 td->version[chip]++;
946 if (md)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000947 md->version[chip]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 /* Write the bad block table to the device ? */
950 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
951 res = write_bbt (mtd, buf, td, md, chipsel);
952 if (res < 0)
953 goto out;
954 }
955 /* Write the mirror bad block table to the device ? */
956 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
957 res = write_bbt (mtd, buf, md, td, chipsel);
958 }
959
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000960out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 kfree (buf);
962 return res;
963}
964
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000965/* Define some generic bad / good block scan pattern which are used
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000966 * while scanning a device for factory marked good / bad blocks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
968
969static struct nand_bbt_descr smallpage_memorybased = {
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000970 .options = NAND_BBT_SCAN2NDPAGE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 .offs = 5,
972 .len = 1,
973 .pattern = scan_ff_pattern
974};
975
976static struct nand_bbt_descr largepage_memorybased = {
977 .options = 0,
978 .offs = 0,
979 .len = 2,
980 .pattern = scan_ff_pattern
981};
982
983static struct nand_bbt_descr smallpage_flashbased = {
984 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
985 .offs = 5,
986 .len = 1,
987 .pattern = scan_ff_pattern
988};
989
990static struct nand_bbt_descr largepage_flashbased = {
991 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
992 .offs = 0,
993 .len = 2,
994 .pattern = scan_ff_pattern
995};
996
997static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
998
999static struct nand_bbt_descr agand_flashbased = {
1000 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
1001 .offs = 0x20,
1002 .len = 6,
1003 .pattern = scan_agand_pattern
1004};
1005
1006/* Generic flash bbt decriptors
1007*/
1008static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1009static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1010
1011static struct nand_bbt_descr bbt_main_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001012 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1014 .offs = 8,
1015 .len = 4,
1016 .veroffs = 12,
1017 .maxblocks = 4,
1018 .pattern = bbt_pattern
1019};
1020
1021static struct nand_bbt_descr bbt_mirror_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001022 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1024 .offs = 8,
1025 .len = 4,
1026 .veroffs = 12,
1027 .maxblocks = 4,
1028 .pattern = mirror_pattern
1029};
1030
1031/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001032 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 * @mtd: MTD device structure
1034 *
1035 * This function selects the default bad block table
1036 * support for the device and calls the nand_scan_bbt function
1037 *
1038*/
1039int nand_default_bbt (struct mtd_info *mtd)
1040{
1041 struct nand_chip *this = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001042
1043 /* Default for AG-AND. We must use a flash based
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 * bad block table as the devices have factory marked
1045 * _good_ blocks. Erasing those blocks leads to loss
1046 * of the good / bad information, so we _must_ store
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001047 * this information in a good / bad table during
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 * startup
1049 */
1050 if (this->options & NAND_IS_AND) {
1051 /* Use the default pattern descriptors */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001052 if (!this->bbt_td) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 this->bbt_td = &bbt_main_descr;
1054 this->bbt_md = &bbt_mirror_descr;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 this->options |= NAND_USE_FLASH_BBT;
1057 return nand_scan_bbt (mtd, &agand_flashbased);
1058 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001059
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 /* Is a flash based bad block table requested ? */
1062 if (this->options & NAND_USE_FLASH_BBT) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001063 /* Use the default pattern descriptors */
1064 if (!this->bbt_td) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 this->bbt_td = &bbt_main_descr;
1066 this->bbt_md = &bbt_mirror_descr;
1067 }
1068 if (!this->badblock_pattern) {
1069 this->badblock_pattern = (mtd->oobblock > 512) ?
1070 &largepage_flashbased : &smallpage_flashbased;
1071 }
1072 } else {
1073 this->bbt_td = NULL;
1074 this->bbt_md = NULL;
1075 if (!this->badblock_pattern) {
1076 this->badblock_pattern = (mtd->oobblock > 512) ?
1077 &largepage_memorybased : &smallpage_memorybased;
1078 }
1079 }
1080 return nand_scan_bbt (mtd, this->badblock_pattern);
1081}
1082
1083/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001084 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 * @mtd: MTD device structure
1086 * @offs: offset in the device
1087 * @allowbbt: allow access to bad block table region
1088 *
1089*/
1090int nand_isbad_bbt (struct mtd_info *mtd, loff_t offs, int allowbbt)
1091{
1092 struct nand_chip *this = mtd->priv;
1093 int block;
1094 uint8_t res;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 /* Get block number * 2 */
1097 block = (int) (offs >> (this->bbt_erase_shift - 1));
1098 res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
1099
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001100 DEBUG (MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001101 (unsigned int)offs, block >> 1, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 switch ((int)res) {
1104 case 0x00: return 0;
1105 case 0x01: return 1;
1106 case 0x02: return allowbbt ? 0 : 1;
1107 }
1108 return 1;
1109}
1110
1111EXPORT_SYMBOL (nand_scan_bbt);
1112EXPORT_SYMBOL (nand_default_bbt);