blob: dc6c6480e58bbed1ab272b38645347e0a9346b89 [file] [log] [blame]
Wolfgang Denk932394a2005-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 Denkac7eb8a2005-09-14 23:53:32 +02006 *
Wolfgang Denk932394a2005-08-17 12:55:25 +02007 * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
8 *
Wolfgang Denk932394a2005-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 Denkac7eb8a2005-09-14 23:53:32 +020015 * When nand_scan_bbt is called, then it tries to find the bad block table
Christian Hitzff8a8a72011-10-12 09:32:04 +020016 * depending on the options in the BBT descriptor(s). If no flash based BBT
17 * (NAND_USE_FLASH_BBT) is specified then the device is scanned for factory
18 * marked good / bad blocks. This information is used to create a memory BBT.
19 * Once a new bad block is discovered then the "factory" information is updated
20 * on the device.
21 * If a flash based BBT is specified then the function first tries to find the
22 * BBT on flash. If a BBT is found then the contents are read and the memory
23 * based BBT is created. If a mirrored BBT is selected then the mirror is
24 * searched too and the versions are compared. If the mirror has a greater
25 * version number than the mirror BBT is used to build the memory based BBT.
Wolfgang Denk932394a2005-08-17 12:55:25 +020026 * If the tables are not versioned, then we "or" the bad block information.
Christian Hitzff8a8a72011-10-12 09:32:04 +020027 * If one of the BBTs is out of date or does not exist it is (re)created.
28 * If no BBT exists at all then the device is scanned for factory marked
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +020029 * good / bad blocks and the bad block tables are created.
Wolfgang Denk932394a2005-08-17 12:55:25 +020030 *
Christian Hitzff8a8a72011-10-12 09:32:04 +020031 * For manufacturer created BBTs like the one found on M-SYS DOC devices
32 * the BBT is searched and read but never created
Wolfgang Denk932394a2005-08-17 12:55:25 +020033 *
Christian Hitzff8a8a72011-10-12 09:32:04 +020034 * The auto generated bad block table is located in the last good blocks
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +020035 * of the device. The table is mirrored, so it can be updated eventually.
Christian Hitzff8a8a72011-10-12 09:32:04 +020036 * The table is marked in the OOB area with an ident pattern and a version
37 * number which indicates which of both tables is more up to date. If the NAND
38 * controller needs the complete OOB area for the ECC information then the
39 * option NAND_USE_FLASH_BBT_NO_OOB should be used: it moves the ident pattern
40 * and the version byte into the data area and the OOB area will remain
41 * untouched.
Wolfgang Denk932394a2005-08-17 12:55:25 +020042 *
43 * The table uses 2 bits per block
Christian Hitzff8a8a72011-10-12 09:32:04 +020044 * 11b: block is good
45 * 00b: block is factory marked bad
Wolfgang Denk53677ef2008-05-20 16:00:29 +020046 * 01b, 10b: block is marked bad due to wear
Wolfgang Denk932394a2005-08-17 12:55:25 +020047 *
48 * The memory bad block table uses the following scheme:
49 * 00b: block is good
50 * 01b: block is marked bad due to wear
51 * 10b: block is reserved (to protect the bbt area)
52 * 11b: block is factory marked bad
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +020053 *
Wolfgang Denk932394a2005-08-17 12:55:25 +020054 * Multichip devices like DOC store the bad block info per floor.
55 *
56 * Following assumptions are made:
57 * - bbts start at a page boundary, if autolocated on a block boundary
William Juulcfa460a2007-10-31 13:53:06 +010058 * - the space necessary for a bbt in FLASH does not exceed a block boundary
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +020059 *
Wolfgang Denk932394a2005-08-17 12:55:25 +020060 */
61
62#include <common.h>
Wolfgang Denk932394a2005-08-17 12:55:25 +020063#include <malloc.h>
Mike Frysinger7b15e2b2012-04-09 13:39:55 +000064#include <linux/compat.h>
Wolfgang Denk932394a2005-08-17 12:55:25 +020065#include <linux/mtd/mtd.h>
66#include <linux/mtd/nand.h>
Christian Hitzff8a8a72011-10-12 09:32:04 +020067#include <linux/mtd/nand_ecc.h>
68#include <linux/bitops.h>
Wolfgang Denk932394a2005-08-17 12:55:25 +020069
70#include <asm/errno.h>
71
Christian Hitzff8a8a72011-10-12 09:32:04 +020072static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
73{
74 int ret;
75
76 ret = memcmp(buf, td->pattern, td->len);
77 if (!ret)
78 return ret;
79 return -1;
80}
81
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +020082/**
Wolfgang Denk932394a2005-08-17 12:55:25 +020083 * check_pattern - [GENERIC] check if a pattern is in the buffer
84 * @buf: the buffer to search
85 * @len: the length of buffer to search
86 * @paglen: the pagelength
87 * @td: search pattern descriptor
88 *
89 * Check for a pattern at the given place. Used to search bad block
90 * tables and good / bad block identifiers.
91 * If the SCAN_EMPTY option is set then check, if all bytes except the
92 * pattern area contain 0xff
93 *
94*/
William Juulcfa460a2007-10-31 13:53:06 +010095static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
Wolfgang Denk932394a2005-08-17 12:55:25 +020096{
William Juulcfa460a2007-10-31 13:53:06 +010097 int i, end = 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +020098 uint8_t *p = buf;
99
Christian Hitzff8a8a72011-10-12 09:32:04 +0200100 if (td->options & NAND_BBT_NO_OOB)
101 return check_pattern_no_oob(buf, td);
102
Wolfgang Denk932394a2005-08-17 12:55:25 +0200103 end = paglen + td->offs;
104 if (td->options & NAND_BBT_SCANEMPTY) {
105 for (i = 0; i < end; i++) {
106 if (p[i] != 0xff)
107 return -1;
108 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200109 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200110 p += end;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200111
Wolfgang Denk932394a2005-08-17 12:55:25 +0200112 /* Compare the pattern */
113 for (i = 0; i < td->len; i++) {
114 if (p[i] != td->pattern[i])
115 return -1;
116 }
117
Christian Hitzff8a8a72011-10-12 09:32:04 +0200118 /* Check both positions 1 and 6 for pattern? */
119 if (td->options & NAND_BBT_SCANBYTE1AND6) {
120 if (td->options & NAND_BBT_SCANEMPTY) {
121 p += td->len;
122 end += NAND_SMALL_BADBLOCK_POS - td->offs;
123 /* Check region between positions 1 and 6 */
124 for (i = 0; i < NAND_SMALL_BADBLOCK_POS - td->offs - td->len;
125 i++) {
126 if (*p++ != 0xff)
127 return -1;
128 }
129 }
130 else {
131 p += NAND_SMALL_BADBLOCK_POS - td->offs;
132 }
133 /* Compare the pattern */
134 for (i = 0; i < td->len; i++) {
135 if (p[i] != td->pattern[i])
136 return -1;
137 }
138 }
139
Wolfgang Denk932394a2005-08-17 12:55:25 +0200140 if (td->options & NAND_BBT_SCANEMPTY) {
William Juulcfa460a2007-10-31 13:53:06 +0100141 p += td->len;
142 end += td->len;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200143 for (i = end; i < len; i++) {
144 if (*p++ != 0xff)
145 return -1;
146 }
147 }
148 return 0;
149}
150
151/**
William Juulcfa460a2007-10-31 13:53:06 +0100152 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
153 * @buf: the buffer to search
154 * @td: search pattern descriptor
155 *
156 * Check for a pattern at the given place. Used to search bad block
157 * tables and good / bad block identifiers. Same as check_pattern, but
158 * no optional empty check
159 *
160*/
161static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
162{
163 int i;
164 uint8_t *p = buf;
165
166 /* Compare the pattern */
167 for (i = 0; i < td->len; i++) {
168 if (p[td->offs + i] != td->pattern[i])
169 return -1;
170 }
Christian Hitzff8a8a72011-10-12 09:32:04 +0200171 /* Need to check location 1 AND 6? */
172 if (td->options & NAND_BBT_SCANBYTE1AND6) {
173 for (i = 0; i < td->len; i++) {
174 if (p[NAND_SMALL_BADBLOCK_POS + i] != td->pattern[i])
175 return -1;
176 }
177 }
William Juulcfa460a2007-10-31 13:53:06 +0100178 return 0;
179}
180
181/**
Christian Hitzff8a8a72011-10-12 09:32:04 +0200182 * add_marker_len - compute the length of the marker in data area
183 * @td: BBT descriptor used for computation
184 *
185 * The length will be 0 if the markeris located in OOB area.
186 */
187static u32 add_marker_len(struct nand_bbt_descr *td)
188{
189 u32 len;
190
191 if (!(td->options & NAND_BBT_NO_OOB))
192 return 0;
193
194 len = td->len;
195 if (td->options & NAND_BBT_VERSION)
196 len++;
197 return len;
198}
199
200/**
Wolfgang Denk932394a2005-08-17 12:55:25 +0200201 * read_bbt - [GENERIC] Read the bad block table starting from page
202 * @mtd: MTD device structure
203 * @buf: temporary buffer
204 * @page: the starting page
205 * @num: the number of bbt descriptors to read
Christian Hitzff8a8a72011-10-12 09:32:04 +0200206 * @td: the bbt describtion table
Wolfgang Denk932394a2005-08-17 12:55:25 +0200207 * @offs: offset in the memory table
Wolfgang Denk932394a2005-08-17 12:55:25 +0200208 *
209 * Read the bad block table starting from page.
210 *
211 */
William Juulcfa460a2007-10-31 13:53:06 +0100212static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
Christian Hitzff8a8a72011-10-12 09:32:04 +0200213 struct nand_bbt_descr *td, int offs)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200214{
215 int res, i, j, act = 0;
216 struct nand_chip *this = mtd->priv;
217 size_t retlen, len, totlen;
218 loff_t from;
Christian Hitzff8a8a72011-10-12 09:32:04 +0200219 int bits = td->options & NAND_BBT_NRBITS_MSK;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200220 uint8_t msk = (uint8_t) ((1 << bits) - 1);
Christian Hitzff8a8a72011-10-12 09:32:04 +0200221 u32 marker_len;
222 int reserved_block_code = td->reserved_block_code;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200223
224 totlen = (num * bits) >> 3;
Christian Hitzff8a8a72011-10-12 09:32:04 +0200225 marker_len = add_marker_len(td);
William Juulcfa460a2007-10-31 13:53:06 +0100226 from = ((loff_t) page) << this->page_shift;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200227
Wolfgang Denk932394a2005-08-17 12:55:25 +0200228 while (totlen) {
William Juulcfa460a2007-10-31 13:53:06 +0100229 len = min(totlen, (size_t) (1 << this->bbt_erase_shift));
Christian Hitzff8a8a72011-10-12 09:32:04 +0200230 if (marker_len) {
231 /*
232 * In case the BBT marker is not in the OOB area it
233 * will be just in the first page.
234 */
235 len -= marker_len;
236 from += marker_len;
237 marker_len = 0;
238 }
William Juulcfa460a2007-10-31 13:53:06 +0100239 res = mtd->read(mtd, from, len, &retlen, buf);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200240 if (res < 0) {
241 if (retlen != len) {
William Juulcfa460a2007-10-31 13:53:06 +0100242 printk(KERN_INFO "nand_bbt: Error reading bad block table\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +0200243 return res;
244 }
William Juulcfa460a2007-10-31 13:53:06 +0100245 printk(KERN_WARNING "nand_bbt: ECC error while reading bad block table\n");
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200246 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200247
248 /* Analyse data */
249 for (i = 0; i < len; i++) {
250 uint8_t dat = buf[i];
251 for (j = 0; j < 8; j += bits, act += 2) {
252 uint8_t tmp = (dat >> j) & msk;
253 if (tmp == msk)
254 continue;
William Juulcfa460a2007-10-31 13:53:06 +0100255 if (reserved_block_code && (tmp == reserved_block_code)) {
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -0400256 printk(KERN_DEBUG "nand_read_bbt: Reserved block at 0x%012llx\n",
Christian Hitzff8a8a72011-10-12 09:32:04 +0200257 (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200258 this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
William Juulcfa460a2007-10-31 13:53:06 +0100259 mtd->ecc_stats.bbtblocks++;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200260 continue;
261 }
262 /* Leave it for now, if its matured we can move this
263 * message to MTD_DEBUG_LEVEL0 */
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -0400264 printk(KERN_DEBUG "nand_read_bbt: Bad block at 0x%012llx\n",
Christian Hitzff8a8a72011-10-12 09:32:04 +0200265 (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200266 /* Factory marked bad or worn out ? */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200267 if (tmp == 0)
268 this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
269 else
270 this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
William Juulcfa460a2007-10-31 13:53:06 +0100271 mtd->ecc_stats.badblocks++;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200272 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200273 }
274 totlen -= len;
275 from += len;
276 }
277 return 0;
278}
279
280/**
281 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
282 * @mtd: MTD device structure
283 * @buf: temporary buffer
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200284 * @td: descriptor for the bad block table
Wolfgang Denk932394a2005-08-17 12:55:25 +0200285 * @chip: read the table for a specific chip, -1 read all chips.
286 * Applies only if NAND_BBT_PERCHIP option is set
287 *
288 * Read the bad block table for all chips starting at a given page
289 * We assume that the bbt bits are in consecutive order.
290*/
William Juulcfa460a2007-10-31 13:53:06 +0100291static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200292{
293 struct nand_chip *this = mtd->priv;
294 int res = 0, i;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200295
Wolfgang Denk932394a2005-08-17 12:55:25 +0200296 if (td->options & NAND_BBT_PERCHIP) {
297 int offs = 0;
298 for (i = 0; i < this->numchips; i++) {
299 if (chip == -1 || chip == i)
Christian Hitzff8a8a72011-10-12 09:32:04 +0200300 res = read_bbt(mtd, buf, td->pages[i],
301 this->chipsize >> this->bbt_erase_shift,
302 td, offs);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200303 if (res)
304 return res;
305 offs += this->chipsize >> (this->bbt_erase_shift + 2);
306 }
307 } else {
Christian Hitzff8a8a72011-10-12 09:32:04 +0200308 res = read_bbt(mtd, buf, td->pages[0],
309 mtd->size >> this->bbt_erase_shift, td, 0);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200310 if (res)
311 return res;
312 }
313 return 0;
314}
315
William Juulcfa460a2007-10-31 13:53:06 +0100316/*
Christian Hitzff8a8a72011-10-12 09:32:04 +0200317 * BBT marker is in the first page, no OOB.
318 */
319static int scan_read_raw_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
320 struct nand_bbt_descr *td)
321{
322 size_t retlen;
323 size_t len;
324
325 len = td->len;
326 if (td->options & NAND_BBT_VERSION)
327 len++;
328
329 return mtd->read(mtd, offs, len, &retlen, buf);
330}
331
332/*
William Juulcfa460a2007-10-31 13:53:06 +0100333 * Scan read raw data from flash
334 */
Christian Hitzff8a8a72011-10-12 09:32:04 +0200335static int scan_read_raw_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
William Juulcfa460a2007-10-31 13:53:06 +0100336 size_t len)
337{
338 struct mtd_oob_ops ops;
Christian Hitzff8a8a72011-10-12 09:32:04 +0200339 int res;
William Juulcfa460a2007-10-31 13:53:06 +0100340
341 ops.mode = MTD_OOB_RAW;
342 ops.ooboffs = 0;
343 ops.ooblen = mtd->oobsize;
William Juulcfa460a2007-10-31 13:53:06 +0100344
Christian Hitzff8a8a72011-10-12 09:32:04 +0200345
346 while (len > 0) {
347 if (len <= mtd->writesize) {
348 ops.oobbuf = buf + len;
349 ops.datbuf = buf;
350 ops.len = len;
351 return mtd->read_oob(mtd, offs, &ops);
352 } else {
353 ops.oobbuf = buf + mtd->writesize;
354 ops.datbuf = buf;
355 ops.len = mtd->writesize;
356 res = mtd->read_oob(mtd, offs, &ops);
357
358 if (res)
359 return res;
360 }
361
362 buf += mtd->oobsize + mtd->writesize;
363 len -= mtd->writesize;
364 }
365 return 0;
366}
367
368static int scan_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
369 size_t len, struct nand_bbt_descr *td)
370{
371 if (td->options & NAND_BBT_NO_OOB)
372 return scan_read_raw_data(mtd, buf, offs, td);
373 else
374 return scan_read_raw_oob(mtd, buf, offs, len);
William Juulcfa460a2007-10-31 13:53:06 +0100375}
376
377/*
378 * Scan write data with oob to flash
379 */
380static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
381 uint8_t *buf, uint8_t *oob)
382{
383 struct mtd_oob_ops ops;
384
385 ops.mode = MTD_OOB_PLACE;
386 ops.ooboffs = 0;
387 ops.ooblen = mtd->oobsize;
388 ops.datbuf = buf;
389 ops.oobbuf = oob;
390 ops.len = len;
391
392 return mtd->write_oob(mtd, offs, &ops);
393}
394
Christian Hitzff8a8a72011-10-12 09:32:04 +0200395static u32 bbt_get_ver_offs(struct mtd_info *mtd, struct nand_bbt_descr *td)
396{
397 u32 ver_offs = td->veroffs;
398
399 if (!(td->options & NAND_BBT_NO_OOB))
400 ver_offs += mtd->writesize;
401 return ver_offs;
402}
403
Wolfgang Denk932394a2005-08-17 12:55:25 +0200404/**
405 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
406 * @mtd: MTD device structure
407 * @buf: temporary buffer
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200408 * @td: descriptor for the bad block table
Wolfgang Denk932394a2005-08-17 12:55:25 +0200409 * @md: descriptor for the bad block table mirror
410 *
411 * Read the bad block table(s) for all chips starting at a given page
412 * We assume that the bbt bits are in consecutive order.
413 *
414*/
William Juulcfa460a2007-10-31 13:53:06 +0100415static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
416 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200417{
418 struct nand_chip *this = mtd->priv;
419
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200420 /* Read the primary version, if available */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200421 if (td->options & NAND_BBT_VERSION) {
Christian Hitzff8a8a72011-10-12 09:32:04 +0200422 scan_read_raw(mtd, buf, (loff_t)td->pages[0] << this->page_shift,
423 mtd->writesize, td);
424 td->version[0] = buf[bbt_get_ver_offs(mtd, td)];
William Juulcfa460a2007-10-31 13:53:06 +0100425 printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
426 td->pages[0], td->version[0]);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200427 }
428
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200429 /* Read the mirror version, if available */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200430 if (md && (md->options & NAND_BBT_VERSION)) {
Christian Hitzff8a8a72011-10-12 09:32:04 +0200431 scan_read_raw(mtd, buf, (loff_t)md->pages[0] << this->page_shift,
432 mtd->writesize, td);
433 md->version[0] = buf[bbt_get_ver_offs(mtd, md)];
William Juulcfa460a2007-10-31 13:53:06 +0100434 printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
435 md->pages[0], md->version[0]);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200436 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200437 return 1;
438}
439
William Juulcfa460a2007-10-31 13:53:06 +0100440/*
441 * Scan a given block full
442 */
443static int scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
444 loff_t offs, uint8_t *buf, size_t readlen,
445 int scanlen, int len)
446{
447 int ret, j;
448
Christian Hitzff8a8a72011-10-12 09:32:04 +0200449 ret = scan_read_raw_oob(mtd, buf, offs, readlen);
William Juulcfa460a2007-10-31 13:53:06 +0100450 if (ret)
451 return ret;
452
453 for (j = 0; j < len; j++, buf += scanlen) {
454 if (check_pattern(buf, scanlen, mtd->writesize, bd))
455 return 1;
456 }
457 return 0;
458}
459
460/*
461 * Scan a given block partially
462 */
463static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
464 loff_t offs, uint8_t *buf, int len)
465{
466 struct mtd_oob_ops ops;
467 int j, ret;
468
469 ops.ooblen = mtd->oobsize;
470 ops.oobbuf = buf;
471 ops.ooboffs = 0;
472 ops.datbuf = NULL;
473 ops.mode = MTD_OOB_PLACE;
474
475 for (j = 0; j < len; j++) {
476 /*
477 * Read the full oob until read_oob is fixed to
478 * handle single byte reads for 16 bit
479 * buswidth
480 */
481 ret = mtd->read_oob(mtd, offs, &ops);
482 if (ret)
483 return ret;
484
485 if (check_short_pattern(buf, bd))
486 return 1;
487
488 offs += mtd->writesize;
489 }
490 return 0;
491}
492
Wolfgang Denk932394a2005-08-17 12:55:25 +0200493/**
494 * create_bbt - [GENERIC] Create a bad block table by scanning the device
495 * @mtd: MTD device structure
496 * @buf: temporary buffer
497 * @bd: descriptor for the good/bad block search pattern
498 * @chip: create the table for a specific chip, -1 read all chips.
499 * Applies only if NAND_BBT_PERCHIP option is set
500 *
501 * Create a bad block table by scanning the device
502 * for the given good/bad block identify pattern
503 */
William Juulcfa460a2007-10-31 13:53:06 +0100504static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
505 struct nand_bbt_descr *bd, int chip)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200506{
507 struct nand_chip *this = mtd->priv;
William Juulcfa460a2007-10-31 13:53:06 +0100508 int i, numblocks, len, scanlen;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200509 int startblock;
510 loff_t from;
William Juulcfa460a2007-10-31 13:53:06 +0100511 size_t readlen;
512
Christian Hitzff8a8a72011-10-12 09:32:04 +0200513 MTDDEBUG(MTD_DEBUG_LEVEL0, "Scanning device for bad blocks\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +0200514
Wolfgang Denk932394a2005-08-17 12:55:25 +0200515 if (bd->options & NAND_BBT_SCANALLPAGES)
516 len = 1 << (this->bbt_erase_shift - this->page_shift);
Christian Hitzff8a8a72011-10-12 09:32:04 +0200517 else if (bd->options & NAND_BBT_SCAN2NDPAGE)
518 len = 2;
519 else
520 len = 1;
William Juulcfa460a2007-10-31 13:53:06 +0100521
522 if (!(bd->options & NAND_BBT_SCANEMPTY)) {
523 /* We need only read few bytes from the OOB area */
524 scanlen = 0;
525 readlen = bd->len;
526 } else {
527 /* Full page content should be read */
528 scanlen = mtd->writesize + mtd->oobsize;
529 readlen = len * mtd->writesize;
530 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200531
532 if (chip == -1) {
William Juulcfa460a2007-10-31 13:53:06 +0100533 /* Note that numblocks is 2 * (real numblocks) here, see i+=2
534 * below as it makes shifting and masking less painful */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200535 numblocks = mtd->size >> (this->bbt_erase_shift - 1);
536 startblock = 0;
537 from = 0;
538 } else {
539 if (chip >= this->numchips) {
William Juulcfa460a2007-10-31 13:53:06 +0100540 printk(KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n",
541 chip + 1, this->numchips);
542 return -EINVAL;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200543 }
544 numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
545 startblock = chip * numblocks;
546 numblocks += startblock;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -0400547 from = (loff_t)startblock << (this->bbt_erase_shift - 1);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200548 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200549
Christian Hitzff8a8a72011-10-12 09:32:04 +0200550 if (this->options & NAND_BBT_SCANLASTPAGE)
551 from += mtd->erasesize - (mtd->writesize * len);
552
Wolfgang Denk932394a2005-08-17 12:55:25 +0200553 for (i = startblock; i < numblocks;) {
William Juulcfa460a2007-10-31 13:53:06 +0100554 int ret;
555
Christian Hitzff8a8a72011-10-12 09:32:04 +0200556 BUG_ON(bd->options & NAND_BBT_NO_OOB);
557
William Juulcfa460a2007-10-31 13:53:06 +0100558 if (bd->options & NAND_BBT_SCANALLPAGES)
559 ret = scan_block_full(mtd, bd, from, buf, readlen,
560 scanlen, len);
561 else
562 ret = scan_block_fast(mtd, bd, from, buf, len);
563
564 if (ret < 0)
565 return ret;
566
567 if (ret) {
568 this->bbt[i >> 3] |= 0x03 << (i & 0x6);
Christian Hitzff8a8a72011-10-12 09:32:04 +0200569 MTDDEBUG(MTD_DEBUG_LEVEL0,
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -0400570 "Bad eraseblock %d at 0x%012llx\n",
571 i >> 1, (unsigned long long)from);
William Juulcfa460a2007-10-31 13:53:06 +0100572 mtd->ecc_stats.badblocks++;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200573 }
William Juulcfa460a2007-10-31 13:53:06 +0100574
Wolfgang Denk932394a2005-08-17 12:55:25 +0200575 i += 2;
576 from += (1 << this->bbt_erase_shift);
577 }
William Juulcfa460a2007-10-31 13:53:06 +0100578 return 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200579}
580
581/**
582 * search_bbt - [GENERIC] scan the device for a specific bad block table
583 * @mtd: MTD device structure
584 * @buf: temporary buffer
585 * @td: descriptor for the bad block table
586 *
587 * Read the bad block table by searching for a given ident pattern.
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200588 * Search is preformed either from the beginning up or from the end of
Wolfgang Denk932394a2005-08-17 12:55:25 +0200589 * the device downwards. The search starts always at the start of a
590 * block.
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200591 * If the option NAND_BBT_PERCHIP is given, each chip is searched
Wolfgang Denk932394a2005-08-17 12:55:25 +0200592 * for a bbt, which contains the bad block information of this chip.
William Juulcfa460a2007-10-31 13:53:06 +0100593 * This is necessary to provide support for certain DOC devices.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200594 *
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200595 * The bbt ident pattern resides in the oob area of the first page
596 * in a block.
Wolfgang Denk932394a2005-08-17 12:55:25 +0200597 */
William Juulcfa460a2007-10-31 13:53:06 +0100598static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200599{
600 struct nand_chip *this = mtd->priv;
601 int i, chips;
Marek Vasut89131e92011-09-30 12:13:22 +0200602 int startblock, block, dir;
William Juulcfa460a2007-10-31 13:53:06 +0100603 int scanlen = mtd->writesize + mtd->oobsize;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200604 int bbtblocks;
William Juulcfa460a2007-10-31 13:53:06 +0100605 int blocktopage = this->bbt_erase_shift - this->page_shift;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200606
607 /* Search direction top -> down ? */
608 if (td->options & NAND_BBT_LASTBLOCK) {
William Juulcfa460a2007-10-31 13:53:06 +0100609 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200610 dir = -1;
611 } else {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200612 startblock = 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200613 dir = 1;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200614 }
615
Wolfgang Denk932394a2005-08-17 12:55:25 +0200616 /* Do we have a bbt per chip ? */
617 if (td->options & NAND_BBT_PERCHIP) {
618 chips = this->numchips;
619 bbtblocks = this->chipsize >> this->bbt_erase_shift;
620 startblock &= bbtblocks - 1;
621 } else {
622 chips = 1;
623 bbtblocks = mtd->size >> this->bbt_erase_shift;
624 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200625
Wolfgang Denk932394a2005-08-17 12:55:25 +0200626 for (i = 0; i < chips; i++) {
627 /* Reset version information */
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200628 td->version[i] = 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200629 td->pages[i] = -1;
630 /* Scan the maximum number of blocks */
631 for (block = 0; block < td->maxblocks; block++) {
William Juulcfa460a2007-10-31 13:53:06 +0100632
Wolfgang Denk932394a2005-08-17 12:55:25 +0200633 int actblock = startblock + dir * block;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -0400634 loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
William Juulcfa460a2007-10-31 13:53:06 +0100635
Wolfgang Denk932394a2005-08-17 12:55:25 +0200636 /* Read first page */
Christian Hitzff8a8a72011-10-12 09:32:04 +0200637 scan_read_raw(mtd, buf, offs, mtd->writesize, td);
William Juulcfa460a2007-10-31 13:53:06 +0100638 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
639 td->pages[i] = actblock << blocktopage;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200640 if (td->options & NAND_BBT_VERSION) {
Christian Hitzff8a8a72011-10-12 09:32:04 +0200641 offs = bbt_get_ver_offs(mtd, td);
642 td->version[i] = buf[offs];
Wolfgang Denk932394a2005-08-17 12:55:25 +0200643 }
644 break;
645 }
646 }
647 startblock += this->chipsize >> this->bbt_erase_shift;
648 }
649 /* Check, if we found a bbt for each requested chip */
650 for (i = 0; i < chips; i++) {
651 if (td->pages[i] == -1)
William Juulcfa460a2007-10-31 13:53:06 +0100652 printk(KERN_WARNING "Bad block table not found for chip %d\n", i);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200653 else
William Juulcfa460a2007-10-31 13:53:06 +0100654 printk(KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n", td->pages[i],
655 td->version[i]);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200656 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200657 return 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200658}
659
660/**
661 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
662 * @mtd: MTD device structure
663 * @buf: temporary buffer
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200664 * @td: descriptor for the bad block table
Wolfgang Denk932394a2005-08-17 12:55:25 +0200665 * @md: descriptor for the bad block table mirror
666 *
667 * Search and read the bad block table(s)
668*/
William Juulcfa460a2007-10-31 13:53:06 +0100669static int search_read_bbts(struct mtd_info *mtd, uint8_t * buf, struct nand_bbt_descr *td, struct nand_bbt_descr *md)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200670{
671 /* Search the primary table */
William Juulcfa460a2007-10-31 13:53:06 +0100672 search_bbt(mtd, buf, td);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200673
Wolfgang Denk932394a2005-08-17 12:55:25 +0200674 /* Search the mirror table */
675 if (md)
William Juulcfa460a2007-10-31 13:53:06 +0100676 search_bbt(mtd, buf, md);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200677
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200678 /* Force result check */
679 return 1;
680}
681
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200682/**
Wolfgang Denk932394a2005-08-17 12:55:25 +0200683 * write_bbt - [GENERIC] (Re)write the bad block table
684 *
685 * @mtd: MTD device structure
686 * @buf: temporary buffer
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200687 * @td: descriptor for the bad block table
Wolfgang Denk932394a2005-08-17 12:55:25 +0200688 * @md: descriptor for the bad block table mirror
689 * @chipsel: selector for a specific chip, -1 for all
690 *
691 * (Re)write the bad block table
692 *
693*/
William Juulcfa460a2007-10-31 13:53:06 +0100694static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
695 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
696 int chipsel)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200697{
698 struct nand_chip *this = mtd->priv;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200699 struct erase_info einfo;
700 int i, j, res, chip = 0;
701 int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
William Juulcfa460a2007-10-31 13:53:06 +0100702 int nrchips, bbtoffs, pageoffs, ooboffs;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200703 uint8_t msk[4];
704 uint8_t rcode = td->reserved_block_code;
705 size_t retlen, len = 0;
706 loff_t to;
William Juulcfa460a2007-10-31 13:53:06 +0100707 struct mtd_oob_ops ops;
708
709 ops.ooblen = mtd->oobsize;
710 ops.ooboffs = 0;
711 ops.datbuf = NULL;
712 ops.mode = MTD_OOB_PLACE;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200713
714 if (!rcode)
715 rcode = 0xff;
716 /* Write bad block table per chip rather than per device ? */
717 if (td->options & NAND_BBT_PERCHIP) {
William Juulcfa460a2007-10-31 13:53:06 +0100718 numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200719 /* Full device write or specific chip ? */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200720 if (chipsel == -1) {
721 nrchips = this->numchips;
722 } else {
723 nrchips = chipsel + 1;
724 chip = chipsel;
725 }
726 } else {
William Juulcfa460a2007-10-31 13:53:06 +0100727 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200728 nrchips = 1;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200729 }
730
Wolfgang Denk932394a2005-08-17 12:55:25 +0200731 /* Loop through the chips */
732 for (; chip < nrchips; chip++) {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200733
734 /* There was already a version of the table, reuse the page
735 * This applies for absolute placement too, as we have the
Wolfgang Denk932394a2005-08-17 12:55:25 +0200736 * page nr. in td->pages.
737 */
738 if (td->pages[chip] != -1) {
739 page = td->pages[chip];
740 goto write;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200741 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200742
743 /* Automatic placement of the bad block table */
744 /* Search direction top -> down ? */
745 if (td->options & NAND_BBT_LASTBLOCK) {
746 startblock = numblocks * (chip + 1) - 1;
747 dir = -1;
748 } else {
749 startblock = chip * numblocks;
750 dir = 1;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200751 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200752
753 for (i = 0; i < td->maxblocks; i++) {
754 int block = startblock + dir * i;
755 /* Check, if the block is bad */
William Juulcfa460a2007-10-31 13:53:06 +0100756 switch ((this->bbt[block >> 2] >>
757 (2 * (block & 0x03))) & 0x03) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200758 case 0x01:
759 case 0x03:
760 continue;
761 }
William Juulcfa460a2007-10-31 13:53:06 +0100762 page = block <<
763 (this->bbt_erase_shift - this->page_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200764 /* Check, if the block is used by the mirror table */
765 if (!md || md->pages[chip] != page)
766 goto write;
767 }
William Juulcfa460a2007-10-31 13:53:06 +0100768 printk(KERN_ERR "No space left to write bad block table\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +0200769 return -ENOSPC;
William Juulcfa460a2007-10-31 13:53:06 +0100770 write:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200771
772 /* Set up shift count and masks for the flash table */
773 bits = td->options & NAND_BBT_NRBITS_MSK;
William Juulcfa460a2007-10-31 13:53:06 +0100774 msk[2] = ~rcode;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200775 switch (bits) {
William Juulcfa460a2007-10-31 13:53:06 +0100776 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
777 msk[3] = 0x01;
778 break;
779 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
780 msk[3] = 0x03;
781 break;
782 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
783 msk[3] = 0x0f;
784 break;
785 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
786 msk[3] = 0xff;
787 break;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200788 default: return -EINVAL;
789 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200790
Wolfgang Denk932394a2005-08-17 12:55:25 +0200791 bbtoffs = chip * (numblocks >> 2);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200792
Wolfgang Denk932394a2005-08-17 12:55:25 +0200793 to = ((loff_t) page) << this->page_shift;
794
Wolfgang Denk932394a2005-08-17 12:55:25 +0200795 /* Must we save the block contents ? */
796 if (td->options & NAND_BBT_SAVECONTENT) {
797 /* Make it block aligned */
798 to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1));
799 len = 1 << this->bbt_erase_shift;
William Juulcfa460a2007-10-31 13:53:06 +0100800 res = mtd->read(mtd, to, len, &retlen, buf);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200801 if (res < 0) {
802 if (retlen != len) {
William Juulcfa460a2007-10-31 13:53:06 +0100803 printk(KERN_INFO "nand_bbt: Error "
804 "reading block for writing "
805 "the bad block table\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +0200806 return res;
807 }
William Juulcfa460a2007-10-31 13:53:06 +0100808 printk(KERN_WARNING "nand_bbt: ECC error "
809 "while reading block for writing "
810 "bad block table\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +0200811 }
William Juulcfa460a2007-10-31 13:53:06 +0100812 /* Read oob data */
813 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
814 ops.oobbuf = &buf[len];
815 res = mtd->read_oob(mtd, to + mtd->writesize, &ops);
816 if (res < 0 || ops.oobretlen != ops.ooblen)
817 goto outerr;
818
Wolfgang Denk932394a2005-08-17 12:55:25 +0200819 /* Calc the byte offset in the buffer */
820 pageoffs = page - (int)(to >> this->page_shift);
821 offs = pageoffs << this->page_shift;
822 /* Preset the bbt area with 0xff */
William Juulcfa460a2007-10-31 13:53:06 +0100823 memset(&buf[offs], 0xff, (size_t) (numblocks >> sft));
824 ooboffs = len + (pageoffs * mtd->oobsize);
825
Christian Hitzff8a8a72011-10-12 09:32:04 +0200826 } else if (td->options & NAND_BBT_NO_OOB) {
827 ooboffs = 0;
828 offs = td->len;
829 /* the version byte */
830 if (td->options & NAND_BBT_VERSION)
831 offs++;
832 /* Calc length */
833 len = (size_t) (numblocks >> sft);
834 len += offs;
835 /* Make it page aligned ! */
836 len = ALIGN(len, mtd->writesize);
837 /* Preset the buffer with 0xff */
838 memset(buf, 0xff, len);
839 /* Pattern is located at the begin of first page */
840 memcpy(buf, td->pattern, td->len);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200841 } else {
842 /* Calc length */
843 len = (size_t) (numblocks >> sft);
844 /* Make it page aligned ! */
Christian Hitzff8a8a72011-10-12 09:32:04 +0200845 len = ALIGN(len, mtd->writesize);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200846 /* Preset the buffer with 0xff */
William Juulcfa460a2007-10-31 13:53:06 +0100847 memset(buf, 0xff, len +
848 (len >> this->page_shift)* mtd->oobsize);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200849 offs = 0;
William Juulcfa460a2007-10-31 13:53:06 +0100850 ooboffs = len;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200851 /* Pattern is located in oob area of first page */
William Juulcfa460a2007-10-31 13:53:06 +0100852 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200853 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200854
William Juulcfa460a2007-10-31 13:53:06 +0100855 if (td->options & NAND_BBT_VERSION)
856 buf[ooboffs + td->veroffs] = td->version[chip];
857
Wolfgang Denk932394a2005-08-17 12:55:25 +0200858 /* walk through the memory table */
William Juulcfa460a2007-10-31 13:53:06 +0100859 for (i = 0; i < numblocks;) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200860 uint8_t dat;
861 dat = this->bbt[bbtoffs + (i >> 2)];
William Juulcfa460a2007-10-31 13:53:06 +0100862 for (j = 0; j < 4; j++, i++) {
Wolfgang Denk932394a2005-08-17 12:55:25 +0200863 int sftcnt = (i << (3 - sft)) & sftmsk;
864 /* Do not store the reserved bbt blocks ! */
William Juulcfa460a2007-10-31 13:53:06 +0100865 buf[offs + (i >> sft)] &=
866 ~(msk[dat & 0x03] << sftcnt);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200867 dat >>= 2;
868 }
869 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200870
William Juulcfa460a2007-10-31 13:53:06 +0100871 memset(&einfo, 0, sizeof(einfo));
Wolfgang Denk932394a2005-08-17 12:55:25 +0200872 einfo.mtd = mtd;
Sandeep Paulrajaaa8eec2009-10-30 13:51:23 -0400873 einfo.addr = to;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200874 einfo.len = 1 << this->bbt_erase_shift;
William Juulcfa460a2007-10-31 13:53:06 +0100875 res = nand_erase_nand(mtd, &einfo, 1);
876 if (res < 0)
877 goto outerr;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200878
Christian Hitzff8a8a72011-10-12 09:32:04 +0200879 res = scan_write_bbt(mtd, to, len, buf,
880 td->options & NAND_BBT_NO_OOB ? NULL :
881 &buf[len]);
William Juulcfa460a2007-10-31 13:53:06 +0100882 if (res < 0)
883 goto outerr;
884
Christian Hitzff8a8a72011-10-12 09:32:04 +0200885 printk(KERN_DEBUG "Bad block table written to 0x%012llx, version "
886 "0x%02X\n", (unsigned long long)to, td->version[chip]);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200887
Wolfgang Denk932394a2005-08-17 12:55:25 +0200888 /* Mark it as used */
889 td->pages[chip] = page;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200890 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200891 return 0;
William Juulcfa460a2007-10-31 13:53:06 +0100892
893 outerr:
894 printk(KERN_WARNING
895 "nand_bbt: Error while writing bad block table %d\n", res);
896 return res;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200897}
898
899/**
900 * nand_memory_bbt - [GENERIC] create a memory based bad block table
901 * @mtd: MTD device structure
902 * @bd: descriptor for the good/bad block search pattern
903 *
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200904 * The function creates a memory based bbt by scanning the device
Wolfgang Denk932394a2005-08-17 12:55:25 +0200905 * for manufacturer / software marked good / bad blocks
906*/
William Juulcfa460a2007-10-31 13:53:06 +0100907static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200908{
909 struct nand_chip *this = mtd->priv;
910
William Juulcfa460a2007-10-31 13:53:06 +0100911 bd->options &= ~NAND_BBT_SCANEMPTY;
912 return create_bbt(mtd, this->buffers->databuf, bd, -1);
Wolfgang Denk932394a2005-08-17 12:55:25 +0200913}
914
915/**
William Juulcfa460a2007-10-31 13:53:06 +0100916 * check_create - [GENERIC] create and write bbt(s) if necessary
Wolfgang Denk932394a2005-08-17 12:55:25 +0200917 * @mtd: MTD device structure
918 * @buf: temporary buffer
919 * @bd: descriptor for the good/bad block search pattern
920 *
921 * The function checks the results of the previous call to read_bbt
William Juulcfa460a2007-10-31 13:53:06 +0100922 * and creates / updates the bbt(s) if necessary
923 * Creation is necessary if no bbt was found for the chip/device
924 * Update is necessary if one of the tables is missing or the
Wolfgang Denk932394a2005-08-17 12:55:25 +0200925 * version nr. of one table is less than the other
926*/
William Juulcfa460a2007-10-31 13:53:06 +0100927static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200928{
929 int i, chips, writeops, chipsel, res;
930 struct nand_chip *this = mtd->priv;
931 struct nand_bbt_descr *td = this->bbt_td;
932 struct nand_bbt_descr *md = this->bbt_md;
933 struct nand_bbt_descr *rd, *rd2;
934
935 /* Do we have a bbt per chip ? */
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200936 if (td->options & NAND_BBT_PERCHIP)
Wolfgang Denk932394a2005-08-17 12:55:25 +0200937 chips = this->numchips;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200938 else
Wolfgang Denk932394a2005-08-17 12:55:25 +0200939 chips = 1;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200940
Wolfgang Denk932394a2005-08-17 12:55:25 +0200941 for (i = 0; i < chips; i++) {
942 writeops = 0;
943 rd = NULL;
944 rd2 = NULL;
945 /* Per chip or per device ? */
946 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
Christian Hitzff8a8a72011-10-12 09:32:04 +0200947 /* Mirrored table available ? */
Wolfgang Denk932394a2005-08-17 12:55:25 +0200948 if (md) {
949 if (td->pages[i] == -1 && md->pages[i] == -1) {
950 writeops = 0x03;
951 goto create;
952 }
953
954 if (td->pages[i] == -1) {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200955 rd = md;
Wolfgang Denk932394a2005-08-17 12:55:25 +0200956 td->version[i] = md->version[i];
957 writeops = 1;
958 goto writecheck;
959 }
960
961 if (md->pages[i] == -1) {
962 rd = td;
963 md->version[i] = td->version[i];
964 writeops = 2;
965 goto writecheck;
966 }
967
968 if (td->version[i] == md->version[i]) {
969 rd = td;
970 if (!(td->options & NAND_BBT_VERSION))
971 rd2 = md;
972 goto writecheck;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200973 }
Wolfgang Denk932394a2005-08-17 12:55:25 +0200974
975 if (((int8_t) (td->version[i] - md->version[i])) > 0) {
976 rd = td;
977 md->version[i] = td->version[i];
978 writeops = 2;
979 } else {
980 rd = md;
981 td->version[i] = md->version[i];
982 writeops = 1;
983 }
984
985 goto writecheck;
986
987 } else {
988 if (td->pages[i] == -1) {
989 writeops = 0x01;
990 goto create;
991 }
992 rd = td;
993 goto writecheck;
994 }
William Juulcfa460a2007-10-31 13:53:06 +0100995 create:
Wolfgang Denk932394a2005-08-17 12:55:25 +0200996 /* Create the bad block table by scanning the device ? */
997 if (!(td->options & NAND_BBT_CREATE))
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +0200998 continue;
999
Wolfgang Denk932394a2005-08-17 12:55:25 +02001000 /* Create the table in memory by scanning the chip(s) */
Christian Hitzff8a8a72011-10-12 09:32:04 +02001001 if (!(this->options & NAND_CREATE_EMPTY_BBT))
1002 create_bbt(mtd, buf, bd, chipsel);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001003
Wolfgang Denk932394a2005-08-17 12:55:25 +02001004 td->version[i] = 1;
1005 if (md)
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001006 md->version[i] = 1;
William Juulcfa460a2007-10-31 13:53:06 +01001007 writecheck:
Wolfgang Denk932394a2005-08-17 12:55:25 +02001008 /* read back first ? */
1009 if (rd)
William Juulcfa460a2007-10-31 13:53:06 +01001010 read_abs_bbt(mtd, buf, rd, chipsel);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001011 /* If they weren't versioned, read both. */
1012 if (rd2)
William Juulcfa460a2007-10-31 13:53:06 +01001013 read_abs_bbt(mtd, buf, rd2, chipsel);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001014
1015 /* Write the bad block table to the device ? */
1016 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
William Juulcfa460a2007-10-31 13:53:06 +01001017 res = write_bbt(mtd, buf, td, md, chipsel);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001018 if (res < 0)
1019 return res;
1020 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001021
Wolfgang Denk932394a2005-08-17 12:55:25 +02001022 /* Write the mirror bad block table to the device ? */
1023 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
William Juulcfa460a2007-10-31 13:53:06 +01001024 res = write_bbt(mtd, buf, md, td, chipsel);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001025 if (res < 0)
1026 return res;
1027 }
1028 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001029 return 0;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001030}
1031
1032/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001033 * mark_bbt_regions - [GENERIC] mark the bad block table regions
Wolfgang Denk932394a2005-08-17 12:55:25 +02001034 * @mtd: MTD device structure
1035 * @td: bad block table descriptor
1036 *
1037 * The bad block table regions are marked as "bad" to prevent
1038 * accidental erasures / writes. The regions are identified by
1039 * the mark 0x02.
1040*/
William Juulcfa460a2007-10-31 13:53:06 +01001041static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001042{
1043 struct nand_chip *this = mtd->priv;
1044 int i, j, chips, block, nrblocks, update;
1045 uint8_t oldval, newval;
1046
1047 /* Do we have a bbt per chip ? */
1048 if (td->options & NAND_BBT_PERCHIP) {
1049 chips = this->numchips;
1050 nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
1051 } else {
1052 chips = 1;
1053 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001054 }
1055
Wolfgang Denk932394a2005-08-17 12:55:25 +02001056 for (i = 0; i < chips; i++) {
1057 if ((td->options & NAND_BBT_ABSPAGE) ||
1058 !(td->options & NAND_BBT_WRITE)) {
William Juulcfa460a2007-10-31 13:53:06 +01001059 if (td->pages[i] == -1)
1060 continue;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001061 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001062 block <<= 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001063 oldval = this->bbt[(block >> 3)];
1064 newval = oldval | (0x2 << (block & 0x06));
1065 this->bbt[(block >> 3)] = newval;
1066 if ((oldval != newval) && td->reserved_block_code)
Christian Hitzff8a8a72011-10-12 09:32:04 +02001067 nand_update_bbt(mtd, (loff_t)block << (this->bbt_erase_shift - 1));
Wolfgang Denk932394a2005-08-17 12:55:25 +02001068 continue;
1069 }
1070 update = 0;
1071 if (td->options & NAND_BBT_LASTBLOCK)
1072 block = ((i + 1) * nrblocks) - td->maxblocks;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001073 else
Wolfgang Denk932394a2005-08-17 12:55:25 +02001074 block = i * nrblocks;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001075 block <<= 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001076 for (j = 0; j < td->maxblocks; j++) {
1077 oldval = this->bbt[(block >> 3)];
1078 newval = oldval | (0x2 << (block & 0x06));
1079 this->bbt[(block >> 3)] = newval;
William Juulcfa460a2007-10-31 13:53:06 +01001080 if (oldval != newval)
1081 update = 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001082 block += 2;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001083 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001084 /* If we want reserved blocks to be recorded to flash, and some
1085 new ones have been marked, then we need to update the stored
1086 bbts. This should only happen once. */
1087 if (update && td->reserved_block_code)
Christian Hitzff8a8a72011-10-12 09:32:04 +02001088 nand_update_bbt(mtd, (loff_t)(block - 2) << (this->bbt_erase_shift - 1));
Wolfgang Denk932394a2005-08-17 12:55:25 +02001089 }
1090}
1091
1092/**
Christian Hitzff8a8a72011-10-12 09:32:04 +02001093 * verify_bbt_descr - verify the bad block description
1094 * @mtd: MTD device structure
1095 * @bd: the table to verify
1096 *
1097 * This functions performs a few sanity checks on the bad block description
1098 * table.
1099 */
1100static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
1101{
1102 struct nand_chip *this = mtd->priv;
1103 u32 pattern_len;
1104 u32 bits;
1105 u32 table_size;
1106
1107 if (!bd)
1108 return;
1109
1110 pattern_len = bd->len;
1111 bits = bd->options & NAND_BBT_NRBITS_MSK;
1112
1113 BUG_ON((this->options & NAND_USE_FLASH_BBT_NO_OOB) &&
1114 !(this->options & NAND_USE_FLASH_BBT));
1115 BUG_ON(!bits);
1116
1117 if (bd->options & NAND_BBT_VERSION)
1118 pattern_len++;
1119
1120 if (bd->options & NAND_BBT_NO_OOB) {
1121 BUG_ON(!(this->options & NAND_USE_FLASH_BBT));
1122 BUG_ON(!(this->options & NAND_USE_FLASH_BBT_NO_OOB));
1123 BUG_ON(bd->offs);
1124 if (bd->options & NAND_BBT_VERSION)
1125 BUG_ON(bd->veroffs != bd->len);
1126 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1127 }
1128
1129 if (bd->options & NAND_BBT_PERCHIP)
1130 table_size = this->chipsize >> this->bbt_erase_shift;
1131 else
1132 table_size = mtd->size >> this->bbt_erase_shift;
1133 table_size >>= 3;
1134 table_size *= bits;
1135 if (bd->options & NAND_BBT_NO_OOB)
1136 table_size += pattern_len;
1137 BUG_ON(table_size > (1 << this->bbt_erase_shift));
1138}
1139
1140/**
Wolfgang Denk932394a2005-08-17 12:55:25 +02001141 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
1142 * @mtd: MTD device structure
1143 * @bd: descriptor for the good/bad block search pattern
1144 *
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001145 * The function checks, if a bad block table(s) is/are already
Wolfgang Denk932394a2005-08-17 12:55:25 +02001146 * available. If not it scans the device for manufacturer
1147 * marked good / bad blocks and writes the bad block table(s) to
1148 * the selected place.
1149 *
1150 * The bad block table memory is allocated here. It must be freed
1151 * by calling the nand_free_bbt function.
1152 *
1153*/
William Juulcfa460a2007-10-31 13:53:06 +01001154int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001155{
1156 struct nand_chip *this = mtd->priv;
1157 int len, res = 0;
1158 uint8_t *buf;
1159 struct nand_bbt_descr *td = this->bbt_td;
1160 struct nand_bbt_descr *md = this->bbt_md;
1161
1162 len = mtd->size >> (this->bbt_erase_shift + 2);
William Juulcfa460a2007-10-31 13:53:06 +01001163 /* Allocate memory (2bit per block) and clear the memory bad block table */
1164 this->bbt = kzalloc(len, GFP_KERNEL);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001165 if (!this->bbt) {
William Juulcfa460a2007-10-31 13:53:06 +01001166 printk(KERN_ERR "nand_scan_bbt: Out of memory\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02001167 return -ENOMEM;
1168 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001169
1170 /* If no primary table decriptor is given, scan the device
1171 * to build a memory based bad block table
1172 */
William Juulcfa460a2007-10-31 13:53:06 +01001173 if (!td) {
1174 if ((res = nand_memory_bbt(mtd, bd))) {
1175 printk(KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n");
1176 kfree(this->bbt);
1177 this->bbt = NULL;
1178 }
1179 return res;
1180 }
Christian Hitzff8a8a72011-10-12 09:32:04 +02001181 verify_bbt_descr(mtd, td);
1182 verify_bbt_descr(mtd, md);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001183
1184 /* Allocate a temporary buffer for one eraseblock incl. oob */
1185 len = (1 << this->bbt_erase_shift);
1186 len += (len >> this->page_shift) * mtd->oobsize;
William Juulcfa460a2007-10-31 13:53:06 +01001187 buf = vmalloc(len);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001188 if (!buf) {
William Juulcfa460a2007-10-31 13:53:06 +01001189 printk(KERN_ERR "nand_bbt: Out of memory\n");
1190 kfree(this->bbt);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001191 this->bbt = NULL;
1192 return -ENOMEM;
1193 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001194
Wolfgang Denk932394a2005-08-17 12:55:25 +02001195 /* Is the bbt at a given page ? */
1196 if (td->options & NAND_BBT_ABSPAGE) {
William Juulcfa460a2007-10-31 13:53:06 +01001197 res = read_abs_bbts(mtd, buf, td, md);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001198 } else {
Wolfgang Denk932394a2005-08-17 12:55:25 +02001199 /* Search the bad block table using a pattern in oob */
William Juulcfa460a2007-10-31 13:53:06 +01001200 res = search_read_bbts(mtd, buf, td, md);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001201 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001202
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001203 if (res)
William Juulcfa460a2007-10-31 13:53:06 +01001204 res = check_create(mtd, buf, bd);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001205
Wolfgang Denk932394a2005-08-17 12:55:25 +02001206 /* Prevent the bbt regions from erasing / writing */
William Juulcfa460a2007-10-31 13:53:06 +01001207 mark_bbt_region(mtd, td);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001208 if (md)
William Juulcfa460a2007-10-31 13:53:06 +01001209 mark_bbt_region(mtd, md);
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001210
William Juulcfa460a2007-10-31 13:53:06 +01001211 vfree(buf);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001212 return res;
1213}
1214
Wolfgang Denk932394a2005-08-17 12:55:25 +02001215/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001216 * nand_update_bbt - [NAND Interface] update bad block table(s)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001217 * @mtd: MTD device structure
1218 * @offs: the offset of the newly marked block
1219 *
1220 * The function updates the bad block table(s)
1221*/
William Juulcfa460a2007-10-31 13:53:06 +01001222int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001223{
1224 struct nand_chip *this = mtd->priv;
1225 int len, res = 0, writeops = 0;
1226 int chip, chipsel;
1227 uint8_t *buf;
1228 struct nand_bbt_descr *td = this->bbt_td;
1229 struct nand_bbt_descr *md = this->bbt_md;
1230
1231 if (!this->bbt || !td)
1232 return -EINVAL;
1233
Wolfgang Denk932394a2005-08-17 12:55:25 +02001234 /* Allocate a temporary buffer for one eraseblock incl. oob */
1235 len = (1 << this->bbt_erase_shift);
1236 len += (len >> this->page_shift) * mtd->oobsize;
William Juulcfa460a2007-10-31 13:53:06 +01001237 buf = kmalloc(len, GFP_KERNEL);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001238 if (!buf) {
William Juulcfa460a2007-10-31 13:53:06 +01001239 printk(KERN_ERR "nand_update_bbt: Out of memory\n");
Wolfgang Denk932394a2005-08-17 12:55:25 +02001240 return -ENOMEM;
1241 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001242
Wolfgang Denk932394a2005-08-17 12:55:25 +02001243 writeops = md != NULL ? 0x03 : 0x01;
1244
1245 /* Do we have a bbt per chip ? */
1246 if (td->options & NAND_BBT_PERCHIP) {
William Juulcfa460a2007-10-31 13:53:06 +01001247 chip = (int)(offs >> this->chip_shift);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001248 chipsel = chip;
1249 } else {
1250 chip = 0;
1251 chipsel = -1;
1252 }
1253
1254 td->version[chip]++;
1255 if (md)
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001256 md->version[chip]++;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001257
1258 /* Write the bad block table to the device ? */
1259 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
William Juulcfa460a2007-10-31 13:53:06 +01001260 res = write_bbt(mtd, buf, td, md, chipsel);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001261 if (res < 0)
1262 goto out;
1263 }
1264 /* Write the mirror bad block table to the device ? */
1265 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
William Juulcfa460a2007-10-31 13:53:06 +01001266 res = write_bbt(mtd, buf, md, td, chipsel);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001267 }
1268
William Juulcfa460a2007-10-31 13:53:06 +01001269 out:
1270 kfree(buf);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001271 return res;
1272}
1273
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001274/* Define some generic bad / good block scan pattern which are used
William Juulcfa460a2007-10-31 13:53:06 +01001275 * while scanning a device for factory marked good / bad blocks. */
Wolfgang Denk932394a2005-08-17 12:55:25 +02001276static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1277
Wolfgang Denk932394a2005-08-17 12:55:25 +02001278static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
1279
1280static struct nand_bbt_descr agand_flashbased = {
1281 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
1282 .offs = 0x20,
1283 .len = 6,
1284 .pattern = scan_agand_pattern
1285};
1286
1287/* Generic flash bbt decriptors
1288*/
1289static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1290static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1291
1292static struct nand_bbt_descr bbt_main_descr = {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001293 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Wolfgang Denk932394a2005-08-17 12:55:25 +02001294 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1295 .offs = 8,
1296 .len = 4,
1297 .veroffs = 12,
1298 .maxblocks = 4,
1299 .pattern = bbt_pattern
1300};
1301
1302static struct nand_bbt_descr bbt_mirror_descr = {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001303 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Wolfgang Denk932394a2005-08-17 12:55:25 +02001304 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1305 .offs = 8,
1306 .len = 4,
1307 .veroffs = 12,
1308 .maxblocks = 4,
1309 .pattern = mirror_pattern
1310};
1311
Christian Hitzff8a8a72011-10-12 09:32:04 +02001312static struct nand_bbt_descr bbt_main_no_bbt_descr = {
1313 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1314 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1315 | NAND_BBT_NO_OOB,
1316 .len = 4,
1317 .veroffs = 4,
1318 .maxblocks = 4,
1319 .pattern = bbt_pattern
1320};
1321
1322static struct nand_bbt_descr bbt_mirror_no_bbt_descr = {
1323 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1324 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1325 | NAND_BBT_NO_OOB,
1326 .len = 4,
1327 .veroffs = 4,
1328 .maxblocks = 4,
1329 .pattern = mirror_pattern
1330};
1331
1332#define BBT_SCAN_OPTIONS (NAND_BBT_SCANLASTPAGE | NAND_BBT_SCAN2NDPAGE | \
1333 NAND_BBT_SCANBYTE1AND6)
1334/**
1335 * nand_create_default_bbt_descr - [Internal] Creates a BBT descriptor structure
1336 * @this: NAND chip to create descriptor for
1337 *
1338 * This function allocates and initializes a nand_bbt_descr for BBM detection
1339 * based on the properties of "this". The new descriptor is stored in
1340 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1341 * passed to this function.
1342 *
1343 */
1344static int nand_create_default_bbt_descr(struct nand_chip *this)
1345{
1346 struct nand_bbt_descr *bd;
1347 if (this->badblock_pattern) {
1348 printk(KERN_WARNING "BBT descr already allocated; not replacing.\n");
1349 return -EINVAL;
1350 }
1351 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
1352 if (!bd) {
1353 printk(KERN_ERR "nand_create_default_bbt_descr: Out of memory\n");
1354 return -ENOMEM;
1355 }
1356 bd->options = this->options & BBT_SCAN_OPTIONS;
1357 bd->offs = this->badblockpos;
1358 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1359 bd->pattern = scan_ff_pattern;
1360 bd->options |= NAND_BBT_DYNAMICSTRUCT;
1361 this->badblock_pattern = bd;
1362 return 0;
1363}
1364
Wolfgang Denk932394a2005-08-17 12:55:25 +02001365/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001366 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
Wolfgang Denk932394a2005-08-17 12:55:25 +02001367 * @mtd: MTD device structure
1368 *
1369 * This function selects the default bad block table
1370 * support for the device and calls the nand_scan_bbt function
1371 *
1372*/
William Juulcfa460a2007-10-31 13:53:06 +01001373int nand_default_bbt(struct mtd_info *mtd)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001374{
1375 struct nand_chip *this = mtd->priv;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001376
1377 /* Default for AG-AND. We must use a flash based
Wolfgang Denk932394a2005-08-17 12:55:25 +02001378 * bad block table as the devices have factory marked
1379 * _good_ blocks. Erasing those blocks leads to loss
1380 * of the good / bad information, so we _must_ store
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001381 * this information in a good / bad table during
Wolfgang Denk932394a2005-08-17 12:55:25 +02001382 * startup
William Juulcfa460a2007-10-31 13:53:06 +01001383 */
Wolfgang Denk932394a2005-08-17 12:55:25 +02001384 if (this->options & NAND_IS_AND) {
1385 /* Use the default pattern descriptors */
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001386 if (!this->bbt_td) {
Wolfgang Denk932394a2005-08-17 12:55:25 +02001387 this->bbt_td = &bbt_main_descr;
1388 this->bbt_md = &bbt_mirror_descr;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001389 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001390 this->options |= NAND_USE_FLASH_BBT;
William Juulcfa460a2007-10-31 13:53:06 +01001391 return nand_scan_bbt(mtd, &agand_flashbased);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001392 }
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001393
Wolfgang Denk932394a2005-08-17 12:55:25 +02001394 /* Is a flash based bad block table requested ? */
1395 if (this->options & NAND_USE_FLASH_BBT) {
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001396 /* Use the default pattern descriptors */
1397 if (!this->bbt_td) {
Christian Hitzff8a8a72011-10-12 09:32:04 +02001398 if (this->options & NAND_USE_FLASH_BBT_NO_OOB) {
1399 this->bbt_td = &bbt_main_no_bbt_descr;
1400 this->bbt_md = &bbt_mirror_no_bbt_descr;
1401 } else {
1402 this->bbt_td = &bbt_main_descr;
1403 this->bbt_md = &bbt_mirror_descr;
1404 }
Wolfgang Denk932394a2005-08-17 12:55:25 +02001405 }
1406 } else {
1407 this->bbt_td = NULL;
1408 this->bbt_md = NULL;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001409 }
Christian Hitzff8a8a72011-10-12 09:32:04 +02001410
1411 if (!this->badblock_pattern)
1412 nand_create_default_bbt_descr(this);
1413
William Juulcfa460a2007-10-31 13:53:06 +01001414 return nand_scan_bbt(mtd, this->badblock_pattern);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001415}
1416
1417/**
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001418 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
Wolfgang Denk932394a2005-08-17 12:55:25 +02001419 * @mtd: MTD device structure
1420 * @offs: offset in the device
1421 * @allowbbt: allow access to bad block table region
1422 *
William Juulcfa460a2007-10-31 13:53:06 +01001423*/
1424int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
Wolfgang Denk932394a2005-08-17 12:55:25 +02001425{
1426 struct nand_chip *this = mtd->priv;
1427 int block;
William Juulcfa460a2007-10-31 13:53:06 +01001428 uint8_t res;
Wolfgang Denkac7eb8a2005-09-14 23:53:32 +02001429
Wolfgang Denk932394a2005-08-17 12:55:25 +02001430 /* Get block number * 2 */
William Juulcfa460a2007-10-31 13:53:06 +01001431 block = (int)(offs >> (this->bbt_erase_shift - 1));
Wolfgang Denk932394a2005-08-17 12:55:25 +02001432 res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
1433
Christian Hitzff8a8a72011-10-12 09:32:04 +02001434 MTDDEBUG(MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1435 (unsigned int)offs, block >> 1, res);
Wolfgang Denk932394a2005-08-17 12:55:25 +02001436
1437 switch ((int)res) {
William Juulcfa460a2007-10-31 13:53:06 +01001438 case 0x00:
1439 return 0;
1440 case 0x01:
1441 return 1;
1442 case 0x02:
1443 return allowbbt ? 0 : 1;
Wolfgang Denk932394a2005-08-17 12:55:25 +02001444 }
1445 return 1;
1446}