blob: 4165857752ca267de3efef0a917eec42dc484b00 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/mtd/nand_bbt.c
3 *
4 * Overview:
5 * Bad block table support for the NAND driver
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000015 * When nand_scan_bbt is called, then it tries to find the bad block table
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * If the tables are not versioned, then we "or" the bad block information.
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +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
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000029 * good / bad blocks and the bad block tables are created.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020031 * For manufacturer created BBTs like the one found on M-SYS DOC devices
32 * the BBT is searched and read but never created
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 *
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020034 * The auto generated bad block table is located in the last good blocks
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000035 * of the device. The table is mirrored, so it can be updated eventually.
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 *
43 * The table uses 2 bits per block
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020044 * 11b: block is good
45 * 00b: block is factory marked bad
46 * 01b, 10b: block is marked bad due to wear
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 *
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
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000053 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * 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
David Woodhousee0c7d762006-05-13 18:07:53 +010058 * - the space necessary for a bbt in FLASH does not exceed a block boundary
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000059 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 */
61
62#include <linux/slab.h>
63#include <linux/types.h>
64#include <linux/mtd/mtd.h>
65#include <linux/mtd/nand.h>
66#include <linux/mtd/nand_ecc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/bitops.h>
68#include <linux/delay.h>
David Woodhousec3f8abf2006-05-13 04:03:42 +010069#include <linux/vmalloc.h>
Paul Gortmakerf3bcc012011-07-10 12:43:28 -040070#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +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
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000082/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 * 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*/
David Woodhousee0c7d762006-05-13 18:07:53 +010095static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +000097 int i, end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 uint8_t *p = buf;
99
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200100 if (td->options & NAND_BBT_NO_OOB)
101 return check_pattern_no_oob(buf, td);
102
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100103 end = paglen + td->offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (td->options & NAND_BBT_SCANEMPTY) {
105 for (i = 0; i < end; i++) {
106 if (p[i] != 0xff)
107 return -1;
108 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000109 }
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100110 p += end;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 /* Compare the pattern */
113 for (i = 0; i < td->len; i++) {
114 if (p[i] != td->pattern[i])
115 return -1;
116 }
117
Brian Norris58373ff2010-07-15 12:15:44 -0700118 /* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (td->options & NAND_BBT_SCANEMPTY) {
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000141 p += td->len;
142 end += td->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 for (i = end; i < len; i++) {
144 if (*p++ != 0xff)
145 return -1;
146 }
147 }
148 return 0;
149}
150
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000151/**
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100152 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
153 * @buf: the buffer to search
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100154 * @td: search pattern descriptor
155 *
156 * Check for a pattern at the given place. Used to search bad block
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000157 * tables and good / bad block identifiers. Same as check_pattern, but
Thomas Gleixner19870da2005-07-15 14:53:51 +0100158 * no optional empty check
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100159 *
160*/
David Woodhousee0c7d762006-05-13 18:07:53 +0100161static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100162{
163 int i;
164 uint8_t *p = buf;
165
166 /* Compare the pattern */
167 for (i = 0; i < td->len; i++) {
Thomas Gleixner19870da2005-07-15 14:53:51 +0100168 if (p[td->offs + i] != td->pattern[i])
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100169 return -1;
170 }
Brian Norris58373ff2010-07-15 12:15:44 -0700171 /* 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 }
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100178 return 0;
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +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/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * 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
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200206 * @td: the bbt describtion table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * @offs: offset in the memory table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 *
209 * Read the bad block table starting from page.
210 *
211 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100212static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200213 struct nand_bbt_descr *td, int offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 int res, i, j, act = 0;
216 struct nand_chip *this = mtd->priv;
217 size_t retlen, len, totlen;
218 loff_t from;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200219 int bits = td->options & NAND_BBT_NRBITS_MSK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 uint8_t msk = (uint8_t) ((1 << bits) - 1);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200221 u32 marker_len;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200222 int reserved_block_code = td->reserved_block_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 totlen = (num * bits) >> 3;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200225 marker_len = add_marker_len(td);
David Woodhousee0c7d762006-05-13 18:07:53 +0100226 from = ((loff_t) page) << this->page_shift;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 while (totlen) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100229 len = min(totlen, (size_t) (1 << this->bbt_erase_shift));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +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 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200239 res = mtd->read(mtd, from, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (res < 0) {
241 if (retlen != len) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100242 printk(KERN_INFO "nand_bbt: Error reading bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return res;
244 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100245 printk(KERN_WARNING "nand_bbt: ECC error while reading bad block table\n");
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
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;
David Woodhousee0c7d762006-05-13 18:07:53 +0100255 if (reserved_block_code && (tmp == reserved_block_code)) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000256 printk(KERN_DEBUG "nand_read_bbt: Reserved block at 0x%012llx\n",
257 (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200259 mtd->ecc_stats.bbtblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 continue;
261 }
262 /* Leave it for now, if its matured we can move this
263 * message to MTD_DEBUG_LEVEL0 */
Adrian Hunter69423d92008-12-10 13:37:21 +0000264 printk(KERN_DEBUG "nand_read_bbt: Bad block at 0x%012llx\n",
265 (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000266 /* Factory marked bad or worn out ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (tmp == 0)
268 this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
269 else
270 this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200271 mtd->ecc_stats.badblocks++;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
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
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000284 * @td: descriptor for the bad block table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 * @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*/
David Woodhousee0c7d762006-05-13 18:07:53 +0100291static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 struct nand_chip *this = mtd->priv;
294 int res = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (td->options & NAND_BBT_PERCHIP) {
297 int offs = 0;
298 for (i = 0; i < this->numchips; i++) {
299 if (chip == -1 || chip == i)
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200300 res = read_bbt(mtd, buf, td->pages[i],
301 this->chipsize >> this->bbt_erase_shift,
302 td, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (res)
304 return res;
305 offs += this->chipsize >> (this->bbt_erase_shift + 2);
306 }
307 } else {
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200308 res = read_bbt(mtd, buf, td->pages[0],
309 mtd->size >> this->bbt_erase_shift, td, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (res)
311 return res;
312 }
313 return 0;
314}
315
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200316/*
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +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/*
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200333 * Scan read raw data from flash
334 */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200335static int scan_read_raw_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200336 size_t len)
337{
338 struct mtd_oob_ops ops;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200339 int res;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200340
341 ops.mode = MTD_OOB_RAW;
342 ops.ooboffs = 0;
343 ops.ooblen = mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200344
Maxim Levitskyb64d39d2010-02-22 20:39:37 +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;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200366}
367
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200368static 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);
375}
376
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200377/*
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
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404/**
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
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000408 * @td: descriptor for the bad block table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 * @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*/
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200415static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
416 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 struct nand_chip *this = mtd->priv;
419
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000420 /* Read the primary version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (td->options & NAND_BBT_VERSION) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000422 scan_read_raw(mtd, buf, (loff_t)td->pages[0] << this->page_shift,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200423 mtd->writesize, td);
424 td->version[0] = buf[bbt_get_ver_offs(mtd, td)];
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200425 printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
426 td->pages[0], td->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000429 /* Read the mirror version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (md && (md->options & NAND_BBT_VERSION)) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000431 scan_read_raw(mtd, buf, (loff_t)md->pages[0] << this->page_shift,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200432 mtd->writesize, td);
433 md->version[0] = buf[bbt_get_ver_offs(mtd, md)];
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200434 printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
435 md->pages[0], md->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return 1;
438}
439
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200440/*
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
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200449 ret = scan_read_raw_oob(mtd, buf, offs, readlen);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200450 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
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200469 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493/**
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 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200504static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
505 struct nand_bbt_descr *bd, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 struct nand_chip *this = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200508 int i, numblocks, len, scanlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 int startblock;
510 loff_t from;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200511 size_t readlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
David Woodhousee0c7d762006-05-13 18:07:53 +0100513 printk(KERN_INFO "Scanning device for bad blocks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 if (bd->options & NAND_BBT_SCANALLPAGES)
516 len = 1 << (this->bbt_erase_shift - this->page_shift);
Brian Norris58373ff2010-07-15 12:15:44 -0700517 else if (bd->options & NAND_BBT_SCAN2NDPAGE)
518 len = 2;
519 else
520 len = 1;
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000521
522 if (!(bd->options & NAND_BBT_SCANEMPTY)) {
523 /* We need only read few bytes from the OOB area */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200524 scanlen = 0;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000525 readlen = bd->len;
526 } else {
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000527 /* Full page content should be read */
Joern Engel28318772006-05-22 23:18:05 +0200528 scanlen = mtd->writesize + mtd->oobsize;
529 readlen = len * mtd->writesize;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 if (chip == -1) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200533 /* Note that numblocks is 2 * (real numblocks) here, see i+=2
534 * below as it makes shifting and masking less painful */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 numblocks = mtd->size >> (this->bbt_erase_shift - 1);
536 startblock = 0;
537 from = 0;
538 } else {
539 if (chip >= this->numchips) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100540 printk(KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n",
541 chip + 1, this->numchips);
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000542 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
544 numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
545 startblock = chip * numblocks;
546 numblocks += startblock;
Adrian Hunter69423d92008-12-10 13:37:21 +0000547 from = (loff_t)startblock << (this->bbt_erase_shift - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000549
Brian Norris30fe8112010-06-23 13:36:02 -0700550 if (this->options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700551 from += mtd->erasesize - (mtd->writesize * len);
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 for (i = startblock; i < numblocks;) {
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000554 int ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000555
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200556 BUG_ON(bd->options & NAND_BBT_NO_OOB);
557
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200558 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);
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000563
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200564 if (ret < 0)
565 return ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000566
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200567 if (ret) {
568 this->bbt[i >> 3] |= 0x03 << (i & 0x6);
Adrian Hunter69423d92008-12-10 13:37:21 +0000569 printk(KERN_WARNING "Bad eraseblock %d at 0x%012llx\n",
570 i >> 1, (unsigned long long)from);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200571 mtd->ecc_stats.badblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 i += 2;
575 from += (1 << this->bbt_erase_shift);
576 }
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000577 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
580/**
581 * search_bbt - [GENERIC] scan the device for a specific bad block table
582 * @mtd: MTD device structure
583 * @buf: temporary buffer
584 * @td: descriptor for the bad block table
585 *
586 * Read the bad block table by searching for a given ident pattern.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000587 * Search is preformed either from the beginning up or from the end of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 * the device downwards. The search starts always at the start of a
589 * block.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000590 * If the option NAND_BBT_PERCHIP is given, each chip is searched
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 * for a bbt, which contains the bad block information of this chip.
David Woodhousee0c7d762006-05-13 18:07:53 +0100592 * This is necessary to provide support for certain DOC devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000594 * The bbt ident pattern resides in the oob area of the first page
595 * in a block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100597static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 struct nand_chip *this = mtd->priv;
600 int i, chips;
601 int bits, startblock, block, dir;
Joern Engel28318772006-05-22 23:18:05 +0200602 int scanlen = mtd->writesize + mtd->oobsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 int bbtblocks;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200604 int blocktopage = this->bbt_erase_shift - this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 /* Search direction top -> down ? */
607 if (td->options & NAND_BBT_LASTBLOCK) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100608 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 dir = -1;
610 } else {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000611 startblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000613 }
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /* Do we have a bbt per chip ? */
616 if (td->options & NAND_BBT_PERCHIP) {
617 chips = this->numchips;
618 bbtblocks = this->chipsize >> this->bbt_erase_shift;
619 startblock &= bbtblocks - 1;
620 } else {
621 chips = 1;
622 bbtblocks = mtd->size >> this->bbt_erase_shift;
623 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 /* Number of bits for each erase block in the bbt */
626 bits = td->options & NAND_BBT_NRBITS_MSK;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 for (i = 0; i < chips; i++) {
629 /* Reset version information */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000630 td->version[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 td->pages[i] = -1;
632 /* Scan the maximum number of blocks */
633 for (block = 0; block < td->maxblocks; block++) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 int actblock = startblock + dir * block;
Adrian Hunter69423d92008-12-10 13:37:21 +0000636 loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /* Read first page */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200639 scan_read_raw(mtd, buf, offs, mtd->writesize, td);
Joern Engel28318772006-05-22 23:18:05 +0200640 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200641 td->pages[i] = actblock << blocktopage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (td->options & NAND_BBT_VERSION) {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200643 offs = bbt_get_ver_offs(mtd, td);
644 td->version[i] = buf[offs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646 break;
647 }
648 }
649 startblock += this->chipsize >> this->bbt_erase_shift;
650 }
651 /* Check, if we found a bbt for each requested chip */
652 for (i = 0; i < chips; i++) {
653 if (td->pages[i] == -1)
David Woodhousee0c7d762006-05-13 18:07:53 +0100654 printk(KERN_WARNING "Bad block table not found for chip %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 else
David Woodhousee0c7d762006-05-13 18:07:53 +0100656 printk(KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n", td->pages[i],
657 td->version[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000659 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
662/**
663 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
664 * @mtd: MTD device structure
665 * @buf: temporary buffer
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000666 * @td: descriptor for the bad block table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 * @md: descriptor for the bad block table mirror
668 *
669 * Search and read the bad block table(s)
670*/
David Woodhousee0c7d762006-05-13 18:07:53 +0100671static int search_read_bbts(struct mtd_info *mtd, uint8_t * buf, struct nand_bbt_descr *td, struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 /* Search the primary table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100674 search_bbt(mtd, buf, td);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /* Search the mirror table */
677 if (md)
David Woodhousee0c7d762006-05-13 18:07:53 +0100678 search_bbt(mtd, buf, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000680 /* Force result check */
681 return 1;
682}
683
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000684/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 * write_bbt - [GENERIC] (Re)write the bad block table
686 *
687 * @mtd: MTD device structure
688 * @buf: temporary buffer
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000689 * @td: descriptor for the bad block table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 * @md: descriptor for the bad block table mirror
691 * @chipsel: selector for a specific chip, -1 for all
692 *
693 * (Re)write the bad block table
694 *
695*/
David Woodhousee0c7d762006-05-13 18:07:53 +0100696static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
Thomas Gleixner9223a452006-05-23 17:21:03 +0200697 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
698 int chipsel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 struct nand_chip *this = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 struct erase_info einfo;
702 int i, j, res, chip = 0;
703 int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200704 int nrchips, bbtoffs, pageoffs, ooboffs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 uint8_t msk[4];
706 uint8_t rcode = td->reserved_block_code;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200707 size_t retlen, len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 loff_t to;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200709 struct mtd_oob_ops ops;
710
711 ops.ooblen = mtd->oobsize;
712 ops.ooboffs = 0;
713 ops.datbuf = NULL;
714 ops.mode = MTD_OOB_PLACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 if (!rcode)
717 rcode = 0xff;
718 /* Write bad block table per chip rather than per device ? */
719 if (td->options & NAND_BBT_PERCHIP) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100720 numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000721 /* Full device write or specific chip ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (chipsel == -1) {
723 nrchips = this->numchips;
724 } else {
725 nrchips = chipsel + 1;
726 chip = chipsel;
727 }
728 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +0100729 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 nrchips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000731 }
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /* Loop through the chips */
734 for (; chip < nrchips; chip++) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000735
736 /* There was already a version of the table, reuse the page
737 * This applies for absolute placement too, as we have the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 * page nr. in td->pages.
739 */
740 if (td->pages[chip] != -1) {
741 page = td->pages[chip];
742 goto write;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 /* Automatic placement of the bad block table */
746 /* Search direction top -> down ? */
747 if (td->options & NAND_BBT_LASTBLOCK) {
748 startblock = numblocks * (chip + 1) - 1;
749 dir = -1;
750 } else {
751 startblock = chip * numblocks;
752 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 for (i = 0; i < td->maxblocks; i++) {
756 int block = startblock + dir * i;
757 /* Check, if the block is bad */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200758 switch ((this->bbt[block >> 2] >>
759 (2 * (block & 0x03))) & 0x03) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 case 0x01:
761 case 0x03:
762 continue;
763 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200764 page = block <<
765 (this->bbt_erase_shift - this->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 /* Check, if the block is used by the mirror table */
767 if (!md || md->pages[chip] != page)
768 goto write;
769 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100770 printk(KERN_ERR "No space left to write bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return -ENOSPC;
David Woodhousee0c7d762006-05-13 18:07:53 +0100772 write:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 /* Set up shift count and masks for the flash table */
775 bits = td->options & NAND_BBT_NRBITS_MSK;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200776 msk[2] = ~rcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 switch (bits) {
Thomas Gleixner9223a452006-05-23 17:21:03 +0200778 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
779 msk[3] = 0x01;
780 break;
781 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
782 msk[3] = 0x03;
783 break;
784 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
785 msk[3] = 0x0f;
786 break;
787 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
788 msk[3] = 0xff;
789 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 default: return -EINVAL;
791 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 bbtoffs = chip * (numblocks >> 2);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 to = ((loff_t) page) << this->page_shift;
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 /* Must we save the block contents ? */
798 if (td->options & NAND_BBT_SAVECONTENT) {
799 /* Make it block aligned */
800 to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1));
801 len = 1 << this->bbt_erase_shift;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200802 res = mtd->read(mtd, to, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (res < 0) {
804 if (retlen != len) {
Thomas Gleixner9223a452006-05-23 17:21:03 +0200805 printk(KERN_INFO "nand_bbt: Error "
806 "reading block for writing "
807 "the bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return res;
809 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200810 printk(KERN_WARNING "nand_bbt: ECC error "
811 "while reading block for writing "
812 "bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200814 /* Read oob data */
Vitaly Wool70145682006-11-03 18:20:38 +0300815 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200816 ops.oobbuf = &buf[len];
817 res = mtd->read_oob(mtd, to + mtd->writesize, &ops);
Vitaly Wool70145682006-11-03 18:20:38 +0300818 if (res < 0 || ops.oobretlen != ops.ooblen)
Thomas Gleixner9223a452006-05-23 17:21:03 +0200819 goto outerr;
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 /* Calc the byte offset in the buffer */
822 pageoffs = page - (int)(to >> this->page_shift);
823 offs = pageoffs << this->page_shift;
824 /* Preset the bbt area with 0xff */
David Woodhousee0c7d762006-05-13 18:07:53 +0100825 memset(&buf[offs], 0xff, (size_t) (numblocks >> sft));
Thomas Gleixner9223a452006-05-23 17:21:03 +0200826 ooboffs = len + (pageoffs * mtd->oobsize);
827
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200828 } else if (td->options & NAND_BBT_NO_OOB) {
829 ooboffs = 0;
830 offs = td->len;
831 /* the version byte */
832 if (td->options & NAND_BBT_VERSION)
833 offs++;
834 /* Calc length */
835 len = (size_t) (numblocks >> sft);
836 len += offs;
837 /* Make it page aligned ! */
838 len = ALIGN(len, mtd->writesize);
839 /* Preset the buffer with 0xff */
840 memset(buf, 0xff, len);
841 /* Pattern is located at the begin of first page */
842 memcpy(buf, td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 } else {
844 /* Calc length */
845 len = (size_t) (numblocks >> sft);
846 /* Make it page aligned ! */
Sebastian Andrzej Siewiorcda32092010-09-29 19:43:50 +0200847 len = ALIGN(len, mtd->writesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 /* Preset the buffer with 0xff */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200849 memset(buf, 0xff, len +
850 (len >> this->page_shift)* mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 offs = 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200852 ooboffs = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /* Pattern is located in oob area of first page */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200854 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000856
Thomas Gleixner9223a452006-05-23 17:21:03 +0200857 if (td->options & NAND_BBT_VERSION)
858 buf[ooboffs + td->veroffs] = td->version[chip];
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 /* walk through the memory table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100861 for (i = 0; i < numblocks;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 uint8_t dat;
863 dat = this->bbt[bbtoffs + (i >> 2)];
David Woodhousee0c7d762006-05-13 18:07:53 +0100864 for (j = 0; j < 4; j++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 int sftcnt = (i << (3 - sft)) & sftmsk;
866 /* Do not store the reserved bbt blocks ! */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200867 buf[offs + (i >> sft)] &=
868 ~(msk[dat & 0x03] << sftcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 dat >>= 2;
870 }
871 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000872
David Woodhousee0c7d762006-05-13 18:07:53 +0100873 memset(&einfo, 0, sizeof(einfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 einfo.mtd = mtd;
Adrian Hunter69423d92008-12-10 13:37:21 +0000875 einfo.addr = to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 einfo.len = 1 << this->bbt_erase_shift;
David Woodhousee0c7d762006-05-13 18:07:53 +0100877 res = nand_erase_nand(mtd, &einfo, 1);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200878 if (res < 0)
879 goto outerr;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000880
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200881 res = scan_write_bbt(mtd, to, len, buf,
882 td->options & NAND_BBT_NO_OOB ? NULL :
883 &buf[len]);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200884 if (res < 0)
885 goto outerr;
886
Adrian Hunter69423d92008-12-10 13:37:21 +0000887 printk(KERN_DEBUG "Bad block table written to 0x%012llx, version "
888 "0x%02X\n", (unsigned long long)to, td->version[chip]);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 /* Mark it as used */
891 td->pages[chip] = page;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200894
895 outerr:
896 printk(KERN_WARNING
897 "nand_bbt: Error while writing bad block table %d\n", res);
898 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
901/**
902 * nand_memory_bbt - [GENERIC] create a memory based bad block table
903 * @mtd: MTD device structure
904 * @bd: descriptor for the good/bad block search pattern
905 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000906 * The function creates a memory based bbt by scanning the device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 * for manufacturer / software marked good / bad blocks
908*/
David Woodhousee0c7d762006-05-13 18:07:53 +0100909static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 struct nand_chip *this = mtd->priv;
912
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000913 bd->options &= ~NAND_BBT_SCANEMPTY;
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100914 return create_bbt(mtd, this->buffers->databuf, bd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
917/**
David Woodhousee0c7d762006-05-13 18:07:53 +0100918 * check_create - [GENERIC] create and write bbt(s) if necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 * @mtd: MTD device structure
920 * @buf: temporary buffer
921 * @bd: descriptor for the good/bad block search pattern
922 *
923 * The function checks the results of the previous call to read_bbt
David Woodhousee0c7d762006-05-13 18:07:53 +0100924 * and creates / updates the bbt(s) if necessary
925 * Creation is necessary if no bbt was found for the chip/device
926 * Update is necessary if one of the tables is missing or the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 * version nr. of one table is less than the other
928*/
David Woodhousee0c7d762006-05-13 18:07:53 +0100929static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
931 int i, chips, writeops, chipsel, res;
932 struct nand_chip *this = mtd->priv;
933 struct nand_bbt_descr *td = this->bbt_td;
934 struct nand_bbt_descr *md = this->bbt_md;
935 struct nand_bbt_descr *rd, *rd2;
936
937 /* Do we have a bbt per chip ? */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000938 if (td->options & NAND_BBT_PERCHIP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 chips = this->numchips;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000940 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 chips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 for (i = 0; i < chips; i++) {
944 writeops = 0;
945 rd = NULL;
946 rd2 = NULL;
947 /* Per chip or per device ? */
948 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300949 /* Mirrored table available ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (md) {
951 if (td->pages[i] == -1 && md->pages[i] == -1) {
952 writeops = 0x03;
953 goto create;
954 }
955
956 if (td->pages[i] == -1) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000957 rd = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 td->version[i] = md->version[i];
959 writeops = 1;
960 goto writecheck;
961 }
962
963 if (md->pages[i] == -1) {
964 rd = td;
965 md->version[i] = td->version[i];
966 writeops = 2;
967 goto writecheck;
968 }
969
970 if (td->version[i] == md->version[i]) {
971 rd = td;
972 if (!(td->options & NAND_BBT_VERSION))
973 rd2 = md;
974 goto writecheck;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 if (((int8_t) (td->version[i] - md->version[i])) > 0) {
978 rd = td;
979 md->version[i] = td->version[i];
980 writeops = 2;
981 } else {
982 rd = md;
983 td->version[i] = md->version[i];
984 writeops = 1;
985 }
986
987 goto writecheck;
988
989 } else {
990 if (td->pages[i] == -1) {
991 writeops = 0x01;
992 goto create;
993 }
994 rd = td;
995 goto writecheck;
996 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100997 create:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 /* Create the bad block table by scanning the device ? */
999 if (!(td->options & NAND_BBT_CREATE))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001000 continue;
1001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 /* Create the table in memory by scanning the chip(s) */
Sebastian Andrzej Siewior453281a2010-10-01 21:37:37 +02001003 if (!(this->options & NAND_CREATE_EMPTY_BBT))
1004 create_bbt(mtd, buf, bd, chipsel);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 td->version[i] = 1;
1007 if (md)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001008 md->version[i] = 1;
David Woodhousee0c7d762006-05-13 18:07:53 +01001009 writecheck:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 /* read back first ? */
1011 if (rd)
David Woodhousee0c7d762006-05-13 18:07:53 +01001012 read_abs_bbt(mtd, buf, rd, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 /* If they weren't versioned, read both. */
1014 if (rd2)
David Woodhousee0c7d762006-05-13 18:07:53 +01001015 read_abs_bbt(mtd, buf, rd2, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 /* Write the bad block table to the device ? */
1018 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001019 res = write_bbt(mtd, buf, td, md, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (res < 0)
1021 return res;
1022 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 /* Write the mirror bad block table to the device ? */
1025 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001026 res = write_bbt(mtd, buf, md, td, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (res < 0)
1028 return res;
1029 }
1030 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001031 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
1034/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001035 * mark_bbt_regions - [GENERIC] mark the bad block table regions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 * @mtd: MTD device structure
1037 * @td: bad block table descriptor
1038 *
1039 * The bad block table regions are marked as "bad" to prevent
1040 * accidental erasures / writes. The regions are identified by
1041 * the mark 0x02.
1042*/
David Woodhousee0c7d762006-05-13 18:07:53 +01001043static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 struct nand_chip *this = mtd->priv;
1046 int i, j, chips, block, nrblocks, update;
1047 uint8_t oldval, newval;
1048
1049 /* Do we have a bbt per chip ? */
1050 if (td->options & NAND_BBT_PERCHIP) {
1051 chips = this->numchips;
1052 nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
1053 } else {
1054 chips = 1;
1055 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001056 }
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 for (i = 0; i < chips; i++) {
1059 if ((td->options & NAND_BBT_ABSPAGE) ||
1060 !(td->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001061 if (td->pages[i] == -1)
1062 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001064 block <<= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 oldval = this->bbt[(block >> 3)];
1066 newval = oldval | (0x2 << (block & 0x06));
1067 this->bbt[(block >> 3)] = newval;
1068 if ((oldval != newval) && td->reserved_block_code)
Adrian Hunter69423d92008-12-10 13:37:21 +00001069 nand_update_bbt(mtd, (loff_t)block << (this->bbt_erase_shift - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 continue;
1071 }
1072 update = 0;
1073 if (td->options & NAND_BBT_LASTBLOCK)
1074 block = ((i + 1) * nrblocks) - td->maxblocks;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001075 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 block = i * nrblocks;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001077 block <<= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 for (j = 0; j < td->maxblocks; j++) {
1079 oldval = this->bbt[(block >> 3)];
1080 newval = oldval | (0x2 << (block & 0x06));
1081 this->bbt[(block >> 3)] = newval;
David Woodhousee0c7d762006-05-13 18:07:53 +01001082 if (oldval != newval)
1083 update = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 block += 2;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 /* If we want reserved blocks to be recorded to flash, and some
1087 new ones have been marked, then we need to update the stored
1088 bbts. This should only happen once. */
1089 if (update && td->reserved_block_code)
Adrian Hunter69423d92008-12-10 13:37:21 +00001090 nand_update_bbt(mtd, (loff_t)(block - 2) << (this->bbt_erase_shift - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092}
1093
1094/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001095 * verify_bbt_descr - verify the bad block description
Randy Dunlap1534b8b2010-11-18 15:02:21 -08001096 * @mtd: MTD device structure
1097 * @bd: the table to verify
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001098 *
1099 * This functions performs a few sanity checks on the bad block description
1100 * table.
1101 */
1102static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
1103{
1104 struct nand_chip *this = mtd->priv;
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001105 u32 pattern_len;
1106 u32 bits;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001107 u32 table_size;
1108
1109 if (!bd)
1110 return;
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001111
1112 pattern_len = bd->len;
1113 bits = bd->options & NAND_BBT_NRBITS_MSK;
1114
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001115 BUG_ON((this->options & NAND_USE_FLASH_BBT_NO_OOB) &&
1116 !(this->options & NAND_USE_FLASH_BBT));
1117 BUG_ON(!bits);
1118
1119 if (bd->options & NAND_BBT_VERSION)
1120 pattern_len++;
1121
1122 if (bd->options & NAND_BBT_NO_OOB) {
1123 BUG_ON(!(this->options & NAND_USE_FLASH_BBT));
1124 BUG_ON(!(this->options & NAND_USE_FLASH_BBT_NO_OOB));
1125 BUG_ON(bd->offs);
1126 if (bd->options & NAND_BBT_VERSION)
1127 BUG_ON(bd->veroffs != bd->len);
1128 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1129 }
1130
1131 if (bd->options & NAND_BBT_PERCHIP)
1132 table_size = this->chipsize >> this->bbt_erase_shift;
1133 else
1134 table_size = mtd->size >> this->bbt_erase_shift;
1135 table_size >>= 3;
1136 table_size *= bits;
1137 if (bd->options & NAND_BBT_NO_OOB)
1138 table_size += pattern_len;
1139 BUG_ON(table_size > (1 << this->bbt_erase_shift));
1140}
1141
1142/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
1144 * @mtd: MTD device structure
1145 * @bd: descriptor for the good/bad block search pattern
1146 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001147 * The function checks, if a bad block table(s) is/are already
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 * available. If not it scans the device for manufacturer
1149 * marked good / bad blocks and writes the bad block table(s) to
1150 * the selected place.
1151 *
1152 * The bad block table memory is allocated here. It must be freed
1153 * by calling the nand_free_bbt function.
1154 *
1155*/
David Woodhousee0c7d762006-05-13 18:07:53 +01001156int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
1158 struct nand_chip *this = mtd->priv;
1159 int len, res = 0;
1160 uint8_t *buf;
1161 struct nand_bbt_descr *td = this->bbt_td;
1162 struct nand_bbt_descr *md = this->bbt_md;
1163
1164 len = mtd->size >> (this->bbt_erase_shift + 2);
Burman Yan95b93a02006-11-15 21:10:29 +02001165 /* Allocate memory (2bit per block) and clear the memory bad block table */
1166 this->bbt = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (!this->bbt) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001168 printk(KERN_ERR "nand_scan_bbt: Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return -ENOMEM;
1170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 /* If no primary table decriptor is given, scan the device
1173 * to build a memory based bad block table
1174 */
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001175 if (!td) {
1176 if ((res = nand_memory_bbt(mtd, bd))) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001177 printk(KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n");
1178 kfree(this->bbt);
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001179 this->bbt = NULL;
1180 }
1181 return res;
1182 }
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001183 verify_bbt_descr(mtd, td);
1184 verify_bbt_descr(mtd, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 /* Allocate a temporary buffer for one eraseblock incl. oob */
1187 len = (1 << this->bbt_erase_shift);
1188 len += (len >> this->page_shift) * mtd->oobsize;
David Woodhousec3f8abf2006-05-13 04:03:42 +01001189 buf = vmalloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (!buf) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001191 printk(KERN_ERR "nand_bbt: Out of memory\n");
1192 kfree(this->bbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 this->bbt = NULL;
1194 return -ENOMEM;
1195 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 /* Is the bbt at a given page ? */
1198 if (td->options & NAND_BBT_ABSPAGE) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001199 res = read_abs_bbts(mtd, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001200 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 /* Search the bad block table using a pattern in oob */
David Woodhousee0c7d762006-05-13 18:07:53 +01001202 res = search_read_bbts(mtd, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001205 if (res)
David Woodhousee0c7d762006-05-13 18:07:53 +01001206 res = check_create(mtd, buf, bd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 /* Prevent the bbt regions from erasing / writing */
David Woodhousee0c7d762006-05-13 18:07:53 +01001209 mark_bbt_region(mtd, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (md)
David Woodhousee0c7d762006-05-13 18:07:53 +01001211 mark_bbt_region(mtd, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001212
David Woodhousee0c7d762006-05-13 18:07:53 +01001213 vfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 return res;
1215}
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001218 * nand_update_bbt - [NAND Interface] update bad block table(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 * @mtd: MTD device structure
1220 * @offs: the offset of the newly marked block
1221 *
1222 * The function updates the bad block table(s)
1223*/
David Woodhousee0c7d762006-05-13 18:07:53 +01001224int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
1226 struct nand_chip *this = mtd->priv;
1227 int len, res = 0, writeops = 0;
1228 int chip, chipsel;
1229 uint8_t *buf;
1230 struct nand_bbt_descr *td = this->bbt_td;
1231 struct nand_bbt_descr *md = this->bbt_md;
1232
1233 if (!this->bbt || !td)
1234 return -EINVAL;
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 /* Allocate a temporary buffer for one eraseblock incl. oob */
1237 len = (1 << this->bbt_erase_shift);
1238 len += (len >> this->page_shift) * mtd->oobsize;
David Woodhousee0c7d762006-05-13 18:07:53 +01001239 buf = kmalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (!buf) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001241 printk(KERN_ERR "nand_update_bbt: Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return -ENOMEM;
1243 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 writeops = md != NULL ? 0x03 : 0x01;
1246
1247 /* Do we have a bbt per chip ? */
1248 if (td->options & NAND_BBT_PERCHIP) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001249 chip = (int)(offs >> this->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 chipsel = chip;
1251 } else {
1252 chip = 0;
1253 chipsel = -1;
1254 }
1255
1256 td->version[chip]++;
1257 if (md)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001258 md->version[chip]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 /* Write the bad block table to the device ? */
1261 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001262 res = write_bbt(mtd, buf, td, md, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (res < 0)
1264 goto out;
1265 }
1266 /* Write the mirror bad block table to the device ? */
1267 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001268 res = write_bbt(mtd, buf, md, td, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
1270
David Woodhousee0c7d762006-05-13 18:07:53 +01001271 out:
1272 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return res;
1274}
1275
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001276/* Define some generic bad / good block scan pattern which are used
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +00001277 * while scanning a device for factory marked good / bad blocks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
1281
1282static struct nand_bbt_descr agand_flashbased = {
1283 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
1284 .offs = 0x20,
1285 .len = 6,
1286 .pattern = scan_agand_pattern
1287};
1288
1289/* Generic flash bbt decriptors
1290*/
1291static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1292static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1293
1294static struct nand_bbt_descr bbt_main_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001295 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1297 .offs = 8,
1298 .len = 4,
1299 .veroffs = 12,
1300 .maxblocks = 4,
1301 .pattern = bbt_pattern
1302};
1303
1304static struct nand_bbt_descr bbt_mirror_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001305 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1307 .offs = 8,
1308 .len = 4,
1309 .veroffs = 12,
1310 .maxblocks = 4,
1311 .pattern = mirror_pattern
1312};
1313
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001314static struct nand_bbt_descr bbt_main_no_bbt_descr = {
1315 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1316 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1317 | NAND_BBT_NO_OOB,
1318 .len = 4,
1319 .veroffs = 4,
1320 .maxblocks = 4,
1321 .pattern = bbt_pattern
1322};
1323
1324static struct nand_bbt_descr bbt_mirror_no_bbt_descr = {
1325 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1326 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1327 | NAND_BBT_NO_OOB,
1328 .len = 4,
1329 .veroffs = 4,
1330 .maxblocks = 4,
1331 .pattern = mirror_pattern
1332};
1333
Brian Norris58373ff2010-07-15 12:15:44 -07001334#define BBT_SCAN_OPTIONS (NAND_BBT_SCANLASTPAGE | NAND_BBT_SCAN2NDPAGE | \
1335 NAND_BBT_SCANBYTE1AND6)
1336/**
1337 * nand_create_default_bbt_descr - [Internal] Creates a BBT descriptor structure
1338 * @this: NAND chip to create descriptor for
1339 *
1340 * This function allocates and initializes a nand_bbt_descr for BBM detection
1341 * based on the properties of "this". The new descriptor is stored in
1342 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1343 * passed to this function.
1344 *
Brian Norris58373ff2010-07-15 12:15:44 -07001345 */
1346static int nand_create_default_bbt_descr(struct nand_chip *this)
1347{
1348 struct nand_bbt_descr *bd;
1349 if (this->badblock_pattern) {
1350 printk(KERN_WARNING "BBT descr already allocated; not replacing.\n");
1351 return -EINVAL;
1352 }
1353 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
1354 if (!bd) {
1355 printk(KERN_ERR "nand_create_default_bbt_descr: Out of memory\n");
1356 return -ENOMEM;
1357 }
1358 bd->options = this->options & BBT_SCAN_OPTIONS;
1359 bd->offs = this->badblockpos;
1360 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1361 bd->pattern = scan_ff_pattern;
1362 bd->options |= NAND_BBT_DYNAMICSTRUCT;
1363 this->badblock_pattern = bd;
1364 return 0;
1365}
1366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001368 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 * @mtd: MTD device structure
1370 *
1371 * This function selects the default bad block table
1372 * support for the device and calls the nand_scan_bbt function
1373 *
1374*/
David Woodhousee0c7d762006-05-13 18:07:53 +01001375int nand_default_bbt(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 struct nand_chip *this = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001378
1379 /* Default for AG-AND. We must use a flash based
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 * bad block table as the devices have factory marked
1381 * _good_ blocks. Erasing those blocks leads to loss
1382 * of the good / bad information, so we _must_ store
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001383 * this information in a good / bad table during
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 * startup
David Woodhousee0c7d762006-05-13 18:07:53 +01001385 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (this->options & NAND_IS_AND) {
1387 /* Use the default pattern descriptors */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001388 if (!this->bbt_td) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 this->bbt_td = &bbt_main_descr;
1390 this->bbt_md = &bbt_mirror_descr;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 this->options |= NAND_USE_FLASH_BBT;
David Woodhousee0c7d762006-05-13 18:07:53 +01001393 return nand_scan_bbt(mtd, &agand_flashbased);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 /* Is a flash based bad block table requested ? */
1397 if (this->options & NAND_USE_FLASH_BBT) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001398 /* Use the default pattern descriptors */
1399 if (!this->bbt_td) {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001400 if (this->options & NAND_USE_FLASH_BBT_NO_OOB) {
1401 this->bbt_td = &bbt_main_no_bbt_descr;
1402 this->bbt_md = &bbt_mirror_no_bbt_descr;
1403 } else {
1404 this->bbt_td = &bbt_main_descr;
1405 this->bbt_md = &bbt_mirror_descr;
1406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 } else {
1409 this->bbt_td = NULL;
1410 this->bbt_md = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
Brian Norrisa2f812d2011-03-18 21:53:42 -07001412
1413 if (!this->badblock_pattern)
1414 nand_create_default_bbt_descr(this);
1415
David Woodhousee0c7d762006-05-13 18:07:53 +01001416 return nand_scan_bbt(mtd, this->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
1419/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001420 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 * @mtd: MTD device structure
1422 * @offs: offset in the device
1423 * @allowbbt: allow access to bad block table region
1424 *
1425*/
David Woodhousee0c7d762006-05-13 18:07:53 +01001426int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
1428 struct nand_chip *this = mtd->priv;
1429 int block;
David Woodhousee0c7d762006-05-13 18:07:53 +01001430 uint8_t res;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 /* Get block number * 2 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001433 block = (int)(offs >> (this->bbt_erase_shift - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
1435
David Woodhousee0c7d762006-05-13 18:07:53 +01001436 DEBUG(MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1437 (unsigned int)offs, block >> 1, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 switch ((int)res) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001440 case 0x00:
1441 return 0;
1442 case 0x01:
1443 return 1;
1444 case 0x02:
1445 return allowbbt ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
1447 return 1;
1448}
1449
David Woodhousee0c7d762006-05-13 18:07:53 +01001450EXPORT_SYMBOL(nand_scan_bbt);
1451EXPORT_SYMBOL(nand_default_bbt);