blob: d68a315f197cbc78aa6c3f5671602f580ea643c9 [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 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * Description:
14 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020015 * When nand_scan_bbt is called, then it tries to find the bad block table
16 * depending on the options in the bbt descriptor(s). If a bbt is found
17 * then the contents are read and the memory based bbt is created. If a
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020018 * mirrored bbt is selected then the mirror is searched too and the
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020019 * versions are compared. If the mirror has a greater version number
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020020 * than the mirror bbt is used to build the memory based bbt.
21 * If the tables are not versioned, then we "or" the bad block information.
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020022 * If one of the bbt's is out of date or does not exist it is (re)created.
23 * If no bbt exists at all then the device is scanned for factory marked
24 * good / bad blocks and the bad block tables are created.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020025 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020026 * For manufacturer created bbts like the one found on M-SYS DOC devices
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020027 * the bbt is searched and read but never created
28 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020029 * The autogenerated bad block table is located in the last good blocks
30 * of the device. The table is mirrored, so it can be updated eventually.
31 * The table is marked in the oob area with an ident pattern and a version
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020032 * number which indicates which of both tables is more up to date.
33 *
34 * The table uses 2 bits per block
Wolfgang Denka1be4762008-05-20 16:00:29 +020035 * 11b: block is good
36 * 00b: block is factory marked bad
37 * 01b, 10b: block is marked bad due to wear
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020038 *
39 * The memory bad block table uses the following scheme:
40 * 00b: block is good
41 * 01b: block is marked bad due to wear
42 * 10b: block is reserved (to protect the bbt area)
43 * 11b: block is factory marked bad
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020044 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020045 * Multichip devices like DOC store the bad block info per floor.
46 *
47 * Following assumptions are made:
48 * - bbts start at a page boundary, if autolocated on a block boundary
William Juul52c07962007-10-31 13:53:06 +010049 * - the space necessary for a bbt in FLASH does not exceed a block boundary
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020050 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020051 */
52
53#include <common.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020054#include <malloc.h>
55#include <linux/mtd/compat.h>
56#include <linux/mtd/mtd.h>
57#include <linux/mtd/nand.h>
58
59#include <asm/errno.h>
60
William Juul52c07962007-10-31 13:53:06 +010061/* XXX U-BOOT XXX */
62#if 0
63#include <linux/slab.h>
64#include <linux/types.h>
65#include <linux/mtd/mtd.h>
66#include <linux/mtd/nand.h>
67#include <linux/mtd/nand_ecc.h>
68#include <linux/mtd/compatmac.h>
69#include <linux/bitops.h>
70#include <linux/delay.h>
71#include <linux/vmalloc.h>
72#endif
73
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020074/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020075 * check_pattern - [GENERIC] check if a pattern is in the buffer
76 * @buf: the buffer to search
77 * @len: the length of buffer to search
78 * @paglen: the pagelength
79 * @td: search pattern descriptor
80 *
81 * Check for a pattern at the given place. Used to search bad block
82 * tables and good / bad block identifiers.
83 * If the SCAN_EMPTY option is set then check, if all bytes except the
84 * pattern area contain 0xff
85 *
86*/
William Juul52c07962007-10-31 13:53:06 +010087static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020088{
William Juul52c07962007-10-31 13:53:06 +010089 int i, end = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020090 uint8_t *p = buf;
91
92 end = paglen + td->offs;
93 if (td->options & NAND_BBT_SCANEMPTY) {
94 for (i = 0; i < end; i++) {
95 if (p[i] != 0xff)
96 return -1;
97 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020098 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020099 p += end;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200100
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200101 /* Compare the pattern */
102 for (i = 0; i < td->len; i++) {
103 if (p[i] != td->pattern[i])
104 return -1;
105 }
106
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200107 if (td->options & NAND_BBT_SCANEMPTY) {
William Juul52c07962007-10-31 13:53:06 +0100108 p += td->len;
109 end += td->len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200110 for (i = end; i < len; i++) {
111 if (*p++ != 0xff)
112 return -1;
113 }
114 }
115 return 0;
116}
117
118/**
William Juul52c07962007-10-31 13:53:06 +0100119 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
120 * @buf: the buffer to search
121 * @td: search pattern descriptor
122 *
123 * Check for a pattern at the given place. Used to search bad block
124 * tables and good / bad block identifiers. Same as check_pattern, but
125 * no optional empty check
126 *
127*/
128static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
129{
130 int i;
131 uint8_t *p = buf;
132
133 /* Compare the pattern */
134 for (i = 0; i < td->len; i++) {
135 if (p[td->offs + i] != td->pattern[i])
136 return -1;
137 }
138 return 0;
139}
140
141/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200142 * read_bbt - [GENERIC] Read the bad block table starting from page
143 * @mtd: MTD device structure
144 * @buf: temporary buffer
145 * @page: the starting page
146 * @num: the number of bbt descriptors to read
147 * @bits: number of bits per block
148 * @offs: offset in the memory table
149 * @reserved_block_code: Pattern to identify reserved blocks
150 *
151 * Read the bad block table starting from page.
152 *
153 */
William Juul52c07962007-10-31 13:53:06 +0100154static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
155 int bits, int offs, int reserved_block_code)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200156{
157 int res, i, j, act = 0;
158 struct nand_chip *this = mtd->priv;
159 size_t retlen, len, totlen;
160 loff_t from;
161 uint8_t msk = (uint8_t) ((1 << bits) - 1);
162
163 totlen = (num * bits) >> 3;
William Juul52c07962007-10-31 13:53:06 +0100164 from = ((loff_t) page) << this->page_shift;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200165
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200166 while (totlen) {
William Juul52c07962007-10-31 13:53:06 +0100167 len = min(totlen, (size_t) (1 << this->bbt_erase_shift));
168 res = mtd->read(mtd, from, len, &retlen, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200169 if (res < 0) {
170 if (retlen != len) {
William Juul52c07962007-10-31 13:53:06 +0100171 printk(KERN_INFO "nand_bbt: Error reading bad block table\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200172 return res;
173 }
William Juul52c07962007-10-31 13:53:06 +0100174 printk(KERN_WARNING "nand_bbt: ECC error while reading bad block table\n");
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200175 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200176
177 /* Analyse data */
178 for (i = 0; i < len; i++) {
179 uint8_t dat = buf[i];
180 for (j = 0; j < 8; j += bits, act += 2) {
181 uint8_t tmp = (dat >> j) & msk;
182 if (tmp == msk)
183 continue;
William Juul52c07962007-10-31 13:53:06 +0100184 if (reserved_block_code && (tmp == reserved_block_code)) {
185 printk(KERN_DEBUG "nand_read_bbt: Reserved block at 0x%08x\n",
186 ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200187 this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
William Juul52c07962007-10-31 13:53:06 +0100188 mtd->ecc_stats.bbtblocks++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200189 continue;
190 }
191 /* Leave it for now, if its matured we can move this
192 * message to MTD_DEBUG_LEVEL0 */
William Juul52c07962007-10-31 13:53:06 +0100193 printk(KERN_DEBUG "nand_read_bbt: Bad block at 0x%08x\n",
194 ((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200195 /* Factory marked bad or worn out ? */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200196 if (tmp == 0)
197 this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
198 else
199 this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
William Juul52c07962007-10-31 13:53:06 +0100200 mtd->ecc_stats.badblocks++;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200201 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200202 }
203 totlen -= len;
204 from += len;
205 }
206 return 0;
207}
208
209/**
210 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
211 * @mtd: MTD device structure
212 * @buf: temporary buffer
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200213 * @td: descriptor for the bad block table
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200214 * @chip: read the table for a specific chip, -1 read all chips.
215 * Applies only if NAND_BBT_PERCHIP option is set
216 *
217 * Read the bad block table for all chips starting at a given page
218 * We assume that the bbt bits are in consecutive order.
219*/
William Juul52c07962007-10-31 13:53:06 +0100220static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200221{
222 struct nand_chip *this = mtd->priv;
223 int res = 0, i;
224 int bits;
225
226 bits = td->options & NAND_BBT_NRBITS_MSK;
227 if (td->options & NAND_BBT_PERCHIP) {
228 int offs = 0;
229 for (i = 0; i < this->numchips; i++) {
230 if (chip == -1 || chip == i)
231 res = read_bbt (mtd, buf, td->pages[i], this->chipsize >> this->bbt_erase_shift, bits, offs, td->reserved_block_code);
232 if (res)
233 return res;
234 offs += this->chipsize >> (this->bbt_erase_shift + 2);
235 }
236 } else {
237 res = read_bbt (mtd, buf, td->pages[0], mtd->size >> this->bbt_erase_shift, bits, 0, td->reserved_block_code);
238 if (res)
239 return res;
240 }
241 return 0;
242}
243
William Juul52c07962007-10-31 13:53:06 +0100244/*
245 * Scan read raw data from flash
246 */
247static int scan_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
248 size_t len)
249{
250 struct mtd_oob_ops ops;
251
252 ops.mode = MTD_OOB_RAW;
253 ops.ooboffs = 0;
254 ops.ooblen = mtd->oobsize;
255 ops.oobbuf = buf;
256 ops.datbuf = buf;
257 ops.len = len;
258
259 return mtd->read_oob(mtd, offs, &ops);
260}
261
262/*
263 * Scan write data with oob to flash
264 */
265static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
266 uint8_t *buf, uint8_t *oob)
267{
268 struct mtd_oob_ops ops;
269
270 ops.mode = MTD_OOB_PLACE;
271 ops.ooboffs = 0;
272 ops.ooblen = mtd->oobsize;
273 ops.datbuf = buf;
274 ops.oobbuf = oob;
275 ops.len = len;
276
277 return mtd->write_oob(mtd, offs, &ops);
278}
279
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200280/**
281 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
282 * @mtd: MTD device structure
283 * @buf: temporary buffer
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200284 * @td: descriptor for the bad block table
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200285 * @md: descriptor for the bad block table mirror
286 *
287 * Read the bad block table(s) for all chips starting at a given page
288 * We assume that the bbt bits are in consecutive order.
289 *
290*/
William Juul52c07962007-10-31 13:53:06 +0100291static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
292 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200293{
294 struct nand_chip *this = mtd->priv;
295
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200296 /* Read the primary version, if available */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200297 if (td->options & NAND_BBT_VERSION) {
William Juul52c07962007-10-31 13:53:06 +0100298 scan_read_raw(mtd, buf, td->pages[0] << this->page_shift,
299 mtd->writesize);
300 td->version[0] = buf[mtd->writesize + td->veroffs];
301 printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
302 td->pages[0], td->version[0]);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200303 }
304
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200305 /* Read the mirror version, if available */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200306 if (md && (md->options & NAND_BBT_VERSION)) {
William Juul52c07962007-10-31 13:53:06 +0100307 scan_read_raw(mtd, buf, md->pages[0] << this->page_shift,
308 mtd->writesize);
309 md->version[0] = buf[mtd->writesize + md->veroffs];
310 printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
311 md->pages[0], md->version[0]);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200312 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200313 return 1;
314}
315
William Juul52c07962007-10-31 13:53:06 +0100316/*
317 * Scan a given block full
318 */
319static int scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
320 loff_t offs, uint8_t *buf, size_t readlen,
321 int scanlen, int len)
322{
323 int ret, j;
324
325 ret = scan_read_raw(mtd, buf, offs, readlen);
326 if (ret)
327 return ret;
328
329 for (j = 0; j < len; j++, buf += scanlen) {
330 if (check_pattern(buf, scanlen, mtd->writesize, bd))
331 return 1;
332 }
333 return 0;
334}
335
336/*
337 * Scan a given block partially
338 */
339static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
340 loff_t offs, uint8_t *buf, int len)
341{
342 struct mtd_oob_ops ops;
343 int j, ret;
344
345 ops.ooblen = mtd->oobsize;
346 ops.oobbuf = buf;
347 ops.ooboffs = 0;
348 ops.datbuf = NULL;
349 ops.mode = MTD_OOB_PLACE;
350
351 for (j = 0; j < len; j++) {
352 /*
353 * Read the full oob until read_oob is fixed to
354 * handle single byte reads for 16 bit
355 * buswidth
356 */
357 ret = mtd->read_oob(mtd, offs, &ops);
358 if (ret)
359 return ret;
360
361 if (check_short_pattern(buf, bd))
362 return 1;
363
364 offs += mtd->writesize;
365 }
366 return 0;
367}
368
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200369/**
370 * create_bbt - [GENERIC] Create a bad block table by scanning the device
371 * @mtd: MTD device structure
372 * @buf: temporary buffer
373 * @bd: descriptor for the good/bad block search pattern
374 * @chip: create the table for a specific chip, -1 read all chips.
375 * Applies only if NAND_BBT_PERCHIP option is set
376 *
377 * Create a bad block table by scanning the device
378 * for the given good/bad block identify pattern
379 */
William Juul52c07962007-10-31 13:53:06 +0100380static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
381 struct nand_bbt_descr *bd, int chip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200382{
383 struct nand_chip *this = mtd->priv;
William Juul52c07962007-10-31 13:53:06 +0100384 int i, numblocks, len, scanlen;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200385 int startblock;
386 loff_t from;
William Juul52c07962007-10-31 13:53:06 +0100387 size_t readlen;
388
Stefan Roese4fe71432008-01-10 18:47:33 +0100389 MTDDEBUG (MTD_DEBUG_LEVEL0, "Scanning device for bad blocks\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200390
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200391 if (bd->options & NAND_BBT_SCANALLPAGES)
392 len = 1 << (this->bbt_erase_shift - this->page_shift);
393 else {
394 if (bd->options & NAND_BBT_SCAN2NDPAGE)
395 len = 2;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200396 else
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200397 len = 1;
398 }
William Juul52c07962007-10-31 13:53:06 +0100399
400 if (!(bd->options & NAND_BBT_SCANEMPTY)) {
401 /* We need only read few bytes from the OOB area */
402 scanlen = 0;
403 readlen = bd->len;
404 } else {
405 /* Full page content should be read */
406 scanlen = mtd->writesize + mtd->oobsize;
407 readlen = len * mtd->writesize;
408 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200409
410 if (chip == -1) {
William Juul52c07962007-10-31 13:53:06 +0100411 /* Note that numblocks is 2 * (real numblocks) here, see i+=2
412 * below as it makes shifting and masking less painful */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200413 numblocks = mtd->size >> (this->bbt_erase_shift - 1);
414 startblock = 0;
415 from = 0;
416 } else {
417 if (chip >= this->numchips) {
William Juul52c07962007-10-31 13:53:06 +0100418 printk(KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n",
419 chip + 1, this->numchips);
420 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200421 }
422 numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
423 startblock = chip * numblocks;
424 numblocks += startblock;
425 from = startblock << (this->bbt_erase_shift - 1);
426 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200427
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200428 for (i = startblock; i < numblocks;) {
William Juul52c07962007-10-31 13:53:06 +0100429 int ret;
430
431 if (bd->options & NAND_BBT_SCANALLPAGES)
432 ret = scan_block_full(mtd, bd, from, buf, readlen,
433 scanlen, len);
434 else
435 ret = scan_block_fast(mtd, bd, from, buf, len);
436
437 if (ret < 0)
438 return ret;
439
440 if (ret) {
441 this->bbt[i >> 3] |= 0x03 << (i & 0x6);
Stefan Roese4fe71432008-01-10 18:47:33 +0100442 MTDDEBUG (MTD_DEBUG_LEVEL0,
443 "Bad eraseblock %d at 0x%08x\n",
444 i >> 1, (unsigned int)from);
William Juul52c07962007-10-31 13:53:06 +0100445 mtd->ecc_stats.badblocks++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200446 }
William Juul52c07962007-10-31 13:53:06 +0100447
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200448 i += 2;
449 from += (1 << this->bbt_erase_shift);
450 }
William Juul52c07962007-10-31 13:53:06 +0100451 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200452}
453
454/**
455 * search_bbt - [GENERIC] scan the device for a specific bad block table
456 * @mtd: MTD device structure
457 * @buf: temporary buffer
458 * @td: descriptor for the bad block table
459 *
460 * Read the bad block table by searching for a given ident pattern.
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200461 * Search is preformed either from the beginning up or from the end of
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200462 * the device downwards. The search starts always at the start of a
463 * block.
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200464 * If the option NAND_BBT_PERCHIP is given, each chip is searched
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200465 * for a bbt, which contains the bad block information of this chip.
William Juul52c07962007-10-31 13:53:06 +0100466 * This is necessary to provide support for certain DOC devices.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200467 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200468 * The bbt ident pattern resides in the oob area of the first page
469 * in a block.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200470 */
William Juul52c07962007-10-31 13:53:06 +0100471static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200472{
473 struct nand_chip *this = mtd->priv;
474 int i, chips;
475 int bits, startblock, block, dir;
William Juul52c07962007-10-31 13:53:06 +0100476 int scanlen = mtd->writesize + mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200477 int bbtblocks;
William Juul52c07962007-10-31 13:53:06 +0100478 int blocktopage = this->bbt_erase_shift - this->page_shift;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200479
480 /* Search direction top -> down ? */
481 if (td->options & NAND_BBT_LASTBLOCK) {
William Juul52c07962007-10-31 13:53:06 +0100482 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200483 dir = -1;
484 } else {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200485 startblock = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200486 dir = 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200487 }
488
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200489 /* Do we have a bbt per chip ? */
490 if (td->options & NAND_BBT_PERCHIP) {
491 chips = this->numchips;
492 bbtblocks = this->chipsize >> this->bbt_erase_shift;
493 startblock &= bbtblocks - 1;
494 } else {
495 chips = 1;
496 bbtblocks = mtd->size >> this->bbt_erase_shift;
497 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200498
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200499 /* Number of bits for each erase block in the bbt */
500 bits = td->options & NAND_BBT_NRBITS_MSK;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200501
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200502 for (i = 0; i < chips; i++) {
503 /* Reset version information */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200504 td->version[i] = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200505 td->pages[i] = -1;
506 /* Scan the maximum number of blocks */
507 for (block = 0; block < td->maxblocks; block++) {
William Juul52c07962007-10-31 13:53:06 +0100508
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200509 int actblock = startblock + dir * block;
William Juul52c07962007-10-31 13:53:06 +0100510 loff_t offs = actblock << this->bbt_erase_shift;
511
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200512 /* Read first page */
William Juul52c07962007-10-31 13:53:06 +0100513 scan_read_raw(mtd, buf, offs, mtd->writesize);
514 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
515 td->pages[i] = actblock << blocktopage;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200516 if (td->options & NAND_BBT_VERSION) {
William Juul52c07962007-10-31 13:53:06 +0100517 td->version[i] = buf[mtd->writesize + td->veroffs];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200518 }
519 break;
520 }
521 }
522 startblock += this->chipsize >> this->bbt_erase_shift;
523 }
524 /* Check, if we found a bbt for each requested chip */
525 for (i = 0; i < chips; i++) {
526 if (td->pages[i] == -1)
William Juul52c07962007-10-31 13:53:06 +0100527 printk(KERN_WARNING "Bad block table not found for chip %d\n", i);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200528 else
William Juul52c07962007-10-31 13:53:06 +0100529 printk(KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n", td->pages[i],
530 td->version[i]);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200531 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200532 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200533}
534
535/**
536 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
537 * @mtd: MTD device structure
538 * @buf: temporary buffer
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200539 * @td: descriptor for the bad block table
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200540 * @md: descriptor for the bad block table mirror
541 *
542 * Search and read the bad block table(s)
543*/
William Juul52c07962007-10-31 13:53:06 +0100544static int search_read_bbts(struct mtd_info *mtd, uint8_t * buf, struct nand_bbt_descr *td, struct nand_bbt_descr *md)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200545{
546 /* Search the primary table */
William Juul52c07962007-10-31 13:53:06 +0100547 search_bbt(mtd, buf, td);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200548
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200549 /* Search the mirror table */
550 if (md)
William Juul52c07962007-10-31 13:53:06 +0100551 search_bbt(mtd, buf, md);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200552
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200553 /* Force result check */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200554 return 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200555}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200556
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200557/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200558 * write_bbt - [GENERIC] (Re)write the bad block table
559 *
560 * @mtd: MTD device structure
561 * @buf: temporary buffer
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200562 * @td: descriptor for the bad block table
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200563 * @md: descriptor for the bad block table mirror
564 * @chipsel: selector for a specific chip, -1 for all
565 *
566 * (Re)write the bad block table
567 *
568*/
William Juul52c07962007-10-31 13:53:06 +0100569static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
570 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
571 int chipsel)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200572{
573 struct nand_chip *this = mtd->priv;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200574 struct erase_info einfo;
575 int i, j, res, chip = 0;
576 int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
William Juul52c07962007-10-31 13:53:06 +0100577 int nrchips, bbtoffs, pageoffs, ooboffs;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200578 uint8_t msk[4];
579 uint8_t rcode = td->reserved_block_code;
580 size_t retlen, len = 0;
581 loff_t to;
William Juul52c07962007-10-31 13:53:06 +0100582 struct mtd_oob_ops ops;
583
584 ops.ooblen = mtd->oobsize;
585 ops.ooboffs = 0;
586 ops.datbuf = NULL;
587 ops.mode = MTD_OOB_PLACE;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200588
589 if (!rcode)
590 rcode = 0xff;
591 /* Write bad block table per chip rather than per device ? */
592 if (td->options & NAND_BBT_PERCHIP) {
William Juul52c07962007-10-31 13:53:06 +0100593 numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200594 /* Full device write or specific chip ? */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200595 if (chipsel == -1) {
596 nrchips = this->numchips;
597 } else {
598 nrchips = chipsel + 1;
599 chip = chipsel;
600 }
601 } else {
William Juul52c07962007-10-31 13:53:06 +0100602 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200603 nrchips = 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200604 }
605
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200606 /* Loop through the chips */
607 for (; chip < nrchips; chip++) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200608
609 /* There was already a version of the table, reuse the page
610 * This applies for absolute placement too, as we have the
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200611 * page nr. in td->pages.
612 */
613 if (td->pages[chip] != -1) {
614 page = td->pages[chip];
615 goto write;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200616 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200617
618 /* Automatic placement of the bad block table */
619 /* Search direction top -> down ? */
620 if (td->options & NAND_BBT_LASTBLOCK) {
621 startblock = numblocks * (chip + 1) - 1;
622 dir = -1;
623 } else {
624 startblock = chip * numblocks;
625 dir = 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200626 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200627
628 for (i = 0; i < td->maxblocks; i++) {
629 int block = startblock + dir * i;
630 /* Check, if the block is bad */
William Juul52c07962007-10-31 13:53:06 +0100631 switch ((this->bbt[block >> 2] >>
632 (2 * (block & 0x03))) & 0x03) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200633 case 0x01:
634 case 0x03:
635 continue;
636 }
William Juul52c07962007-10-31 13:53:06 +0100637 page = block <<
638 (this->bbt_erase_shift - this->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200639 /* Check, if the block is used by the mirror table */
640 if (!md || md->pages[chip] != page)
641 goto write;
642 }
William Juul52c07962007-10-31 13:53:06 +0100643 printk(KERN_ERR "No space left to write bad block table\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200644 return -ENOSPC;
William Juul52c07962007-10-31 13:53:06 +0100645 write:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200646
647 /* Set up shift count and masks for the flash table */
648 bits = td->options & NAND_BBT_NRBITS_MSK;
William Juul52c07962007-10-31 13:53:06 +0100649 msk[2] = ~rcode;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200650 switch (bits) {
William Juul52c07962007-10-31 13:53:06 +0100651 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
652 msk[3] = 0x01;
653 break;
654 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
655 msk[3] = 0x03;
656 break;
657 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
658 msk[3] = 0x0f;
659 break;
660 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
661 msk[3] = 0xff;
662 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200663 default: return -EINVAL;
664 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200665
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200666 bbtoffs = chip * (numblocks >> 2);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200667
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200668 to = ((loff_t) page) << this->page_shift;
669
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200670 /* Must we save the block contents ? */
671 if (td->options & NAND_BBT_SAVECONTENT) {
672 /* Make it block aligned */
673 to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1));
674 len = 1 << this->bbt_erase_shift;
William Juul52c07962007-10-31 13:53:06 +0100675 res = mtd->read(mtd, to, len, &retlen, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200676 if (res < 0) {
677 if (retlen != len) {
William Juul52c07962007-10-31 13:53:06 +0100678 printk(KERN_INFO "nand_bbt: Error "
679 "reading block for writing "
680 "the bad block table\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200681 return res;
682 }
William Juul52c07962007-10-31 13:53:06 +0100683 printk(KERN_WARNING "nand_bbt: ECC error "
684 "while reading block for writing "
685 "bad block table\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200686 }
William Juul52c07962007-10-31 13:53:06 +0100687 /* Read oob data */
688 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
689 ops.oobbuf = &buf[len];
690 res = mtd->read_oob(mtd, to + mtd->writesize, &ops);
691 if (res < 0 || ops.oobretlen != ops.ooblen)
692 goto outerr;
693
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200694 /* Calc the byte offset in the buffer */
695 pageoffs = page - (int)(to >> this->page_shift);
696 offs = pageoffs << this->page_shift;
697 /* Preset the bbt area with 0xff */
William Juul52c07962007-10-31 13:53:06 +0100698 memset(&buf[offs], 0xff, (size_t) (numblocks >> sft));
699 ooboffs = len + (pageoffs * mtd->oobsize);
700
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200701 } else {
702 /* Calc length */
703 len = (size_t) (numblocks >> sft);
704 /* Make it page aligned ! */
William Juul52c07962007-10-31 13:53:06 +0100705 len = (len + (mtd->writesize - 1)) &
706 ~(mtd->writesize - 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200707 /* Preset the buffer with 0xff */
William Juul52c07962007-10-31 13:53:06 +0100708 memset(buf, 0xff, len +
709 (len >> this->page_shift)* mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200710 offs = 0;
William Juul52c07962007-10-31 13:53:06 +0100711 ooboffs = len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200712 /* Pattern is located in oob area of first page */
William Juul52c07962007-10-31 13:53:06 +0100713 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200714 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200715
William Juul52c07962007-10-31 13:53:06 +0100716 if (td->options & NAND_BBT_VERSION)
717 buf[ooboffs + td->veroffs] = td->version[chip];
718
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200719 /* walk through the memory table */
William Juul52c07962007-10-31 13:53:06 +0100720 for (i = 0; i < numblocks;) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200721 uint8_t dat;
722 dat = this->bbt[bbtoffs + (i >> 2)];
William Juul52c07962007-10-31 13:53:06 +0100723 for (j = 0; j < 4; j++, i++) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200724 int sftcnt = (i << (3 - sft)) & sftmsk;
725 /* Do not store the reserved bbt blocks ! */
William Juul52c07962007-10-31 13:53:06 +0100726 buf[offs + (i >> sft)] &=
727 ~(msk[dat & 0x03] << sftcnt);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200728 dat >>= 2;
729 }
730 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200731
William Juul52c07962007-10-31 13:53:06 +0100732 memset(&einfo, 0, sizeof(einfo));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200733 einfo.mtd = mtd;
William Juul52c07962007-10-31 13:53:06 +0100734 einfo.addr = (unsigned long)to;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200735 einfo.len = 1 << this->bbt_erase_shift;
William Juul52c07962007-10-31 13:53:06 +0100736 res = nand_erase_nand(mtd, &einfo, 1);
737 if (res < 0)
738 goto outerr;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200739
William Juul52c07962007-10-31 13:53:06 +0100740 res = scan_write_bbt(mtd, to, len, buf, &buf[len]);
741 if (res < 0)
742 goto outerr;
743
744 printk(KERN_DEBUG "Bad block table written to 0x%08x, version "
745 "0x%02X\n", (unsigned int)to, td->version[chip]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200746
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200747 /* Mark it as used */
748 td->pages[chip] = page;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200749 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200750 return 0;
William Juul52c07962007-10-31 13:53:06 +0100751
752 outerr:
753 printk(KERN_WARNING
754 "nand_bbt: Error while writing bad block table %d\n", res);
755 return res;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200756}
757
758/**
759 * nand_memory_bbt - [GENERIC] create a memory based bad block table
760 * @mtd: MTD device structure
761 * @bd: descriptor for the good/bad block search pattern
762 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200763 * The function creates a memory based bbt by scanning the device
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200764 * for manufacturer / software marked good / bad blocks
765*/
William Juul52c07962007-10-31 13:53:06 +0100766static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200767{
768 struct nand_chip *this = mtd->priv;
769
William Juul52c07962007-10-31 13:53:06 +0100770 bd->options &= ~NAND_BBT_SCANEMPTY;
771 return create_bbt(mtd, this->buffers->databuf, bd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200772}
773
774/**
William Juul52c07962007-10-31 13:53:06 +0100775 * check_create - [GENERIC] create and write bbt(s) if necessary
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200776 * @mtd: MTD device structure
777 * @buf: temporary buffer
778 * @bd: descriptor for the good/bad block search pattern
779 *
780 * The function checks the results of the previous call to read_bbt
William Juul52c07962007-10-31 13:53:06 +0100781 * and creates / updates the bbt(s) if necessary
782 * Creation is necessary if no bbt was found for the chip/device
783 * Update is necessary if one of the tables is missing or the
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200784 * version nr. of one table is less than the other
785*/
William Juul52c07962007-10-31 13:53:06 +0100786static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200787{
788 int i, chips, writeops, chipsel, res;
789 struct nand_chip *this = mtd->priv;
790 struct nand_bbt_descr *td = this->bbt_td;
791 struct nand_bbt_descr *md = this->bbt_md;
792 struct nand_bbt_descr *rd, *rd2;
793
794 /* Do we have a bbt per chip ? */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200795 if (td->options & NAND_BBT_PERCHIP)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200796 chips = this->numchips;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200797 else
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200798 chips = 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200799
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200800 for (i = 0; i < chips; i++) {
801 writeops = 0;
802 rd = NULL;
803 rd2 = NULL;
804 /* Per chip or per device ? */
805 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
806 /* Mirrored table avilable ? */
807 if (md) {
808 if (td->pages[i] == -1 && md->pages[i] == -1) {
809 writeops = 0x03;
810 goto create;
811 }
812
813 if (td->pages[i] == -1) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200814 rd = md;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200815 td->version[i] = md->version[i];
816 writeops = 1;
817 goto writecheck;
818 }
819
820 if (md->pages[i] == -1) {
821 rd = td;
822 md->version[i] = td->version[i];
823 writeops = 2;
824 goto writecheck;
825 }
826
827 if (td->version[i] == md->version[i]) {
828 rd = td;
829 if (!(td->options & NAND_BBT_VERSION))
830 rd2 = md;
831 goto writecheck;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200832 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200833
834 if (((int8_t) (td->version[i] - md->version[i])) > 0) {
835 rd = td;
836 md->version[i] = td->version[i];
837 writeops = 2;
838 } else {
839 rd = md;
840 td->version[i] = md->version[i];
841 writeops = 1;
842 }
843
844 goto writecheck;
845
846 } else {
847 if (td->pages[i] == -1) {
848 writeops = 0x01;
849 goto create;
850 }
851 rd = td;
852 goto writecheck;
853 }
William Juul52c07962007-10-31 13:53:06 +0100854 create:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200855 /* Create the bad block table by scanning the device ? */
856 if (!(td->options & NAND_BBT_CREATE))
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200857 continue;
858
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200859 /* Create the table in memory by scanning the chip(s) */
William Juul52c07962007-10-31 13:53:06 +0100860 create_bbt(mtd, buf, bd, chipsel);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200861
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200862 td->version[i] = 1;
863 if (md)
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200864 md->version[i] = 1;
William Juul52c07962007-10-31 13:53:06 +0100865 writecheck:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200866 /* read back first ? */
867 if (rd)
William Juul52c07962007-10-31 13:53:06 +0100868 read_abs_bbt(mtd, buf, rd, chipsel);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200869 /* If they weren't versioned, read both. */
870 if (rd2)
William Juul52c07962007-10-31 13:53:06 +0100871 read_abs_bbt(mtd, buf, rd2, chipsel);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200872
873 /* Write the bad block table to the device ? */
874 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
William Juul52c07962007-10-31 13:53:06 +0100875 res = write_bbt(mtd, buf, td, md, chipsel);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200876 if (res < 0)
877 return res;
878 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200879
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200880 /* Write the mirror bad block table to the device ? */
881 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
William Juul52c07962007-10-31 13:53:06 +0100882 res = write_bbt(mtd, buf, md, td, chipsel);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200883 if (res < 0)
884 return res;
885 }
886 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200887 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200888}
889
890/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200891 * mark_bbt_regions - [GENERIC] mark the bad block table regions
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200892 * @mtd: MTD device structure
893 * @td: bad block table descriptor
894 *
895 * The bad block table regions are marked as "bad" to prevent
896 * accidental erasures / writes. The regions are identified by
897 * the mark 0x02.
898*/
William Juul52c07962007-10-31 13:53:06 +0100899static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200900{
901 struct nand_chip *this = mtd->priv;
902 int i, j, chips, block, nrblocks, update;
903 uint8_t oldval, newval;
904
905 /* Do we have a bbt per chip ? */
906 if (td->options & NAND_BBT_PERCHIP) {
907 chips = this->numchips;
908 nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
909 } else {
910 chips = 1;
911 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200912 }
913
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200914 for (i = 0; i < chips; i++) {
915 if ((td->options & NAND_BBT_ABSPAGE) ||
916 !(td->options & NAND_BBT_WRITE)) {
William Juul52c07962007-10-31 13:53:06 +0100917 if (td->pages[i] == -1)
918 continue;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200919 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200920 block <<= 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200921 oldval = this->bbt[(block >> 3)];
922 newval = oldval | (0x2 << (block & 0x06));
923 this->bbt[(block >> 3)] = newval;
924 if ((oldval != newval) && td->reserved_block_code)
925 nand_update_bbt(mtd, block << (this->bbt_erase_shift - 1));
926 continue;
927 }
928 update = 0;
929 if (td->options & NAND_BBT_LASTBLOCK)
930 block = ((i + 1) * nrblocks) - td->maxblocks;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200931 else
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200932 block = i * nrblocks;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200933 block <<= 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200934 for (j = 0; j < td->maxblocks; j++) {
935 oldval = this->bbt[(block >> 3)];
936 newval = oldval | (0x2 << (block & 0x06));
937 this->bbt[(block >> 3)] = newval;
William Juul52c07962007-10-31 13:53:06 +0100938 if (oldval != newval)
939 update = 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200940 block += 2;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200941 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200942 /* If we want reserved blocks to be recorded to flash, and some
943 new ones have been marked, then we need to update the stored
944 bbts. This should only happen once. */
945 if (update && td->reserved_block_code)
946 nand_update_bbt(mtd, (block - 2) << (this->bbt_erase_shift - 1));
947 }
948}
949
950/**
951 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
952 * @mtd: MTD device structure
953 * @bd: descriptor for the good/bad block search pattern
954 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200955 * The function checks, if a bad block table(s) is/are already
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200956 * available. If not it scans the device for manufacturer
957 * marked good / bad blocks and writes the bad block table(s) to
958 * the selected place.
959 *
960 * The bad block table memory is allocated here. It must be freed
961 * by calling the nand_free_bbt function.
962 *
963*/
William Juul52c07962007-10-31 13:53:06 +0100964int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200965{
966 struct nand_chip *this = mtd->priv;
967 int len, res = 0;
968 uint8_t *buf;
969 struct nand_bbt_descr *td = this->bbt_td;
970 struct nand_bbt_descr *md = this->bbt_md;
971
972 len = mtd->size >> (this->bbt_erase_shift + 2);
William Juul52c07962007-10-31 13:53:06 +0100973 /* Allocate memory (2bit per block) and clear the memory bad block table */
974 this->bbt = kzalloc(len, GFP_KERNEL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200975 if (!this->bbt) {
William Juul52c07962007-10-31 13:53:06 +0100976 printk(KERN_ERR "nand_scan_bbt: Out of memory\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200977 return -ENOMEM;
978 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200979
980 /* If no primary table decriptor is given, scan the device
981 * to build a memory based bad block table
982 */
William Juul52c07962007-10-31 13:53:06 +0100983 if (!td) {
984 if ((res = nand_memory_bbt(mtd, bd))) {
985 printk(KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n");
986 kfree(this->bbt);
987 this->bbt = NULL;
988 }
989 return res;
990 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200991
992 /* Allocate a temporary buffer for one eraseblock incl. oob */
993 len = (1 << this->bbt_erase_shift);
994 len += (len >> this->page_shift) * mtd->oobsize;
William Juul52c07962007-10-31 13:53:06 +0100995 buf = vmalloc(len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200996 if (!buf) {
William Juul52c07962007-10-31 13:53:06 +0100997 printk(KERN_ERR "nand_bbt: Out of memory\n");
998 kfree(this->bbt);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200999 this->bbt = NULL;
1000 return -ENOMEM;
1001 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001002
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001003 /* Is the bbt at a given page ? */
1004 if (td->options & NAND_BBT_ABSPAGE) {
William Juul52c07962007-10-31 13:53:06 +01001005 res = read_abs_bbts(mtd, buf, td, md);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001006 } else {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001007 /* Search the bad block table using a pattern in oob */
William Juul52c07962007-10-31 13:53:06 +01001008 res = search_read_bbts(mtd, buf, td, md);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001009 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001010
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001011 if (res)
William Juul52c07962007-10-31 13:53:06 +01001012 res = check_create(mtd, buf, bd);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001013
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001014 /* Prevent the bbt regions from erasing / writing */
William Juul52c07962007-10-31 13:53:06 +01001015 mark_bbt_region(mtd, td);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001016 if (md)
William Juul52c07962007-10-31 13:53:06 +01001017 mark_bbt_region(mtd, md);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001018
William Juul52c07962007-10-31 13:53:06 +01001019 vfree(buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001020 return res;
1021}
1022
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001023/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001024 * nand_update_bbt - [NAND Interface] update bad block table(s)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001025 * @mtd: MTD device structure
1026 * @offs: the offset of the newly marked block
1027 *
1028 * The function updates the bad block table(s)
1029*/
William Juul52c07962007-10-31 13:53:06 +01001030int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001031{
1032 struct nand_chip *this = mtd->priv;
1033 int len, res = 0, writeops = 0;
1034 int chip, chipsel;
1035 uint8_t *buf;
1036 struct nand_bbt_descr *td = this->bbt_td;
1037 struct nand_bbt_descr *md = this->bbt_md;
1038
1039 if (!this->bbt || !td)
1040 return -EINVAL;
1041
1042 len = mtd->size >> (this->bbt_erase_shift + 2);
1043 /* Allocate a temporary buffer for one eraseblock incl. oob */
1044 len = (1 << this->bbt_erase_shift);
1045 len += (len >> this->page_shift) * mtd->oobsize;
William Juul52c07962007-10-31 13:53:06 +01001046 buf = kmalloc(len, GFP_KERNEL);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001047 if (!buf) {
William Juul52c07962007-10-31 13:53:06 +01001048 printk(KERN_ERR "nand_update_bbt: Out of memory\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001049 return -ENOMEM;
1050 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001051
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001052 writeops = md != NULL ? 0x03 : 0x01;
1053
1054 /* Do we have a bbt per chip ? */
1055 if (td->options & NAND_BBT_PERCHIP) {
William Juul52c07962007-10-31 13:53:06 +01001056 chip = (int)(offs >> this->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001057 chipsel = chip;
1058 } else {
1059 chip = 0;
1060 chipsel = -1;
1061 }
1062
1063 td->version[chip]++;
1064 if (md)
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001065 md->version[chip]++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001066
1067 /* Write the bad block table to the device ? */
1068 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
William Juul52c07962007-10-31 13:53:06 +01001069 res = write_bbt(mtd, buf, td, md, chipsel);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001070 if (res < 0)
1071 goto out;
1072 }
1073 /* Write the mirror bad block table to the device ? */
1074 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
William Juul52c07962007-10-31 13:53:06 +01001075 res = write_bbt(mtd, buf, md, td, chipsel);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001076 }
1077
William Juul52c07962007-10-31 13:53:06 +01001078 out:
1079 kfree(buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001080 return res;
1081}
1082
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001083/* Define some generic bad / good block scan pattern which are used
William Juul52c07962007-10-31 13:53:06 +01001084 * while scanning a device for factory marked good / bad blocks. */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001085static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1086
1087static struct nand_bbt_descr smallpage_memorybased = {
William Juul52c07962007-10-31 13:53:06 +01001088 .options = NAND_BBT_SCAN2NDPAGE,
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001089 .offs = 5,
1090 .len = 1,
1091 .pattern = scan_ff_pattern
1092};
1093
1094static struct nand_bbt_descr largepage_memorybased = {
1095 .options = 0,
1096 .offs = 0,
1097 .len = 2,
1098 .pattern = scan_ff_pattern
1099};
1100
1101static struct nand_bbt_descr smallpage_flashbased = {
William Juul52c07962007-10-31 13:53:06 +01001102 .options = NAND_BBT_SCAN2NDPAGE,
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001103 .offs = 5,
1104 .len = 1,
1105 .pattern = scan_ff_pattern
1106};
1107
1108static struct nand_bbt_descr largepage_flashbased = {
William Juul52c07962007-10-31 13:53:06 +01001109 .options = NAND_BBT_SCAN2NDPAGE,
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001110 .offs = 0,
1111 .len = 2,
1112 .pattern = scan_ff_pattern
1113};
1114
1115static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
1116
1117static struct nand_bbt_descr agand_flashbased = {
1118 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
1119 .offs = 0x20,
1120 .len = 6,
1121 .pattern = scan_agand_pattern
1122};
1123
1124/* Generic flash bbt decriptors
1125*/
1126static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1127static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1128
1129static struct nand_bbt_descr bbt_main_descr = {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001130 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001131 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1132 .offs = 8,
1133 .len = 4,
1134 .veroffs = 12,
1135 .maxblocks = 4,
1136 .pattern = bbt_pattern
1137};
1138
1139static struct nand_bbt_descr bbt_mirror_descr = {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001140 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001141 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1142 .offs = 8,
1143 .len = 4,
1144 .veroffs = 12,
1145 .maxblocks = 4,
1146 .pattern = mirror_pattern
1147};
1148
1149/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001150 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001151 * @mtd: MTD device structure
1152 *
1153 * This function selects the default bad block table
1154 * support for the device and calls the nand_scan_bbt function
1155 *
1156*/
William Juul52c07962007-10-31 13:53:06 +01001157int nand_default_bbt(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001158{
1159 struct nand_chip *this = mtd->priv;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001160
1161 /* Default for AG-AND. We must use a flash based
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001162 * bad block table as the devices have factory marked
1163 * _good_ blocks. Erasing those blocks leads to loss
1164 * of the good / bad information, so we _must_ store
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001165 * this information in a good / bad table during
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001166 * startup
William Juul52c07962007-10-31 13:53:06 +01001167 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001168 if (this->options & NAND_IS_AND) {
1169 /* Use the default pattern descriptors */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001170 if (!this->bbt_td) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001171 this->bbt_td = &bbt_main_descr;
1172 this->bbt_md = &bbt_mirror_descr;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001173 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001174 this->options |= NAND_USE_FLASH_BBT;
William Juul52c07962007-10-31 13:53:06 +01001175 return nand_scan_bbt(mtd, &agand_flashbased);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001176 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001177
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001178 /* Is a flash based bad block table requested ? */
1179 if (this->options & NAND_USE_FLASH_BBT) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001180 /* Use the default pattern descriptors */
1181 if (!this->bbt_td) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001182 this->bbt_td = &bbt_main_descr;
1183 this->bbt_md = &bbt_mirror_descr;
1184 }
1185 if (!this->badblock_pattern) {
William Juul52c07962007-10-31 13:53:06 +01001186 this->badblock_pattern = (mtd->writesize > 512) ? &largepage_flashbased : &smallpage_flashbased;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001187 }
1188 } else {
1189 this->bbt_td = NULL;
1190 this->bbt_md = NULL;
1191 if (!this->badblock_pattern) {
William Juul52c07962007-10-31 13:53:06 +01001192 this->badblock_pattern = (mtd->writesize > 512) ?
1193 &largepage_memorybased : &smallpage_memorybased;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001194 }
1195 }
William Juul52c07962007-10-31 13:53:06 +01001196 return nand_scan_bbt(mtd, this->badblock_pattern);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001197}
1198
1199/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001200 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001201 * @mtd: MTD device structure
1202 * @offs: offset in the device
1203 * @allowbbt: allow access to bad block table region
1204 *
William Juul52c07962007-10-31 13:53:06 +01001205*/
1206int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001207{
1208 struct nand_chip *this = mtd->priv;
1209 int block;
William Juul52c07962007-10-31 13:53:06 +01001210 uint8_t res;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001211
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001212 /* Get block number * 2 */
William Juul52c07962007-10-31 13:53:06 +01001213 block = (int)(offs >> (this->bbt_erase_shift - 1));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001214 res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
1215
Scott Wooddf83c472008-06-20 12:38:57 -05001216 MTDDEBUG (MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: "
1217 "(block %d) 0x%02x\n", (unsigned int)offs, res, block >> 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001218
1219 switch ((int)res) {
William Juul52c07962007-10-31 13:53:06 +01001220 case 0x00:
1221 return 0;
1222 case 0x01:
1223 return 1;
1224 case 0x02:
1225 return allowbbt ? 0 : 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001226 }
1227 return 1;
1228}
1229
William Juul52c07962007-10-31 13:53:06 +01001230/* XXX U-BOOT XXX */
1231#if 0
1232EXPORT_SYMBOL(nand_scan_bbt);
1233EXPORT_SYMBOL(nand_default_bbt);
1234#endif