blob: 5c9c0b2f09d4ad81b762059eb4795197fafc711e [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
Brian Norrisbb9ebd42011-05-31 16:31:23 -070017 * (NAND_BBT_USE_FLASH) is specified then the device is scanned for factory
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020018 * 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
Brian Norrisbb9ebd42011-05-31 16:31:23 -070039 * option NAND_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of
Brian Norrisa40f7342011-05-31 16:31:22 -070040 * course): it moves the ident pattern and the version byte into the data area
41 * and the OOB area will remain 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020071static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
72{
73 int ret;
74
75 ret = memcmp(buf, td->pattern, td->len);
76 if (!ret)
77 return ret;
78 return -1;
79}
80
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000081/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * check_pattern - [GENERIC] check if a pattern is in the buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -070083 * @buf: the buffer to search
84 * @len: the length of buffer to search
85 * @paglen: the pagelength
86 * @td: search pattern descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 *
Brian Norris8b6e50c2011-05-25 14:59:01 -070088 * Check for a pattern at the given place. Used to search bad block tables and
89 * good / bad block identifiers. If the SCAN_EMPTY option is set then check, if
90 * all bytes except the pattern area contain 0xff.
91 */
David Woodhousee0c7d762006-05-13 18:07:53 +010092static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +000094 int i, end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 uint8_t *p = buf;
96
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020097 if (td->options & NAND_BBT_NO_OOB)
98 return check_pattern_no_oob(buf, td);
99
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100100 end = paglen + td->offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (td->options & NAND_BBT_SCANEMPTY) {
102 for (i = 0; i < end; i++) {
103 if (p[i] != 0xff)
104 return -1;
105 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000106 }
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100107 p += end;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 /* Compare the pattern */
Brian Norris75b66d82011-09-07 13:13:41 -0700110 if (memcmp(p, td->pattern, td->len))
111 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (td->options & NAND_BBT_SCANEMPTY) {
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000114 p += td->len;
115 end += td->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 for (i = end; i < len; i++) {
117 if (*p++ != 0xff)
118 return -1;
119 }
120 }
121 return 0;
122}
123
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000124/**
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100125 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700126 * @buf: the buffer to search
127 * @td: search pattern descriptor
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100128 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700129 * Check for a pattern at the given place. Used to search bad block tables and
130 * good / bad block identifiers. Same as check_pattern, but no optional empty
131 * check.
132 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100133static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100134{
135 int i;
136 uint8_t *p = buf;
137
138 /* Compare the pattern */
139 for (i = 0; i < td->len; i++) {
Thomas Gleixner19870da2005-07-15 14:53:51 +0100140 if (p[td->offs + i] != td->pattern[i])
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100141 return -1;
142 }
143 return 0;
144}
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200147 * add_marker_len - compute the length of the marker in data area
Brian Norris8b6e50c2011-05-25 14:59:01 -0700148 * @td: BBT descriptor used for computation
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200149 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700150 * The length will be 0 if the marker is located in OOB area.
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200151 */
152static u32 add_marker_len(struct nand_bbt_descr *td)
153{
154 u32 len;
155
156 if (!(td->options & NAND_BBT_NO_OOB))
157 return 0;
158
159 len = td->len;
160 if (td->options & NAND_BBT_VERSION)
161 len++;
162 return len;
163}
164
165/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * read_bbt - [GENERIC] Read the bad block table starting from page
Brian Norris8b6e50c2011-05-25 14:59:01 -0700167 * @mtd: MTD device structure
168 * @buf: temporary buffer
169 * @page: the starting page
170 * @num: the number of bbt descriptors to read
Brian Norris7854d3f2011-06-23 14:12:08 -0700171 * @td: the bbt describtion table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700172 * @offs: offset in the memory table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 *
174 * Read the bad block table starting from page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100176static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200177 struct nand_bbt_descr *td, int offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Brian Norris167a8d52011-09-20 18:35:08 -0700179 int res, ret = 0, i, j, act = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 struct nand_chip *this = mtd->priv;
181 size_t retlen, len, totlen;
182 loff_t from;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200183 int bits = td->options & NAND_BBT_NRBITS_MSK;
Brian Norris596d7452011-09-07 13:13:33 -0700184 uint8_t msk = (uint8_t)((1 << bits) - 1);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200185 u32 marker_len;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200186 int reserved_block_code = td->reserved_block_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 totlen = (num * bits) >> 3;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200189 marker_len = add_marker_len(td);
Brian Norris596d7452011-09-07 13:13:33 -0700190 from = ((loff_t)page) << this->page_shift;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 while (totlen) {
Brian Norris596d7452011-09-07 13:13:33 -0700193 len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200194 if (marker_len) {
195 /*
196 * In case the BBT marker is not in the OOB area it
197 * will be just in the first page.
198 */
199 len -= marker_len;
200 from += marker_len;
201 marker_len = 0;
202 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200203 res = mtd->read(mtd, from, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (res < 0) {
Brian Norris167a8d52011-09-20 18:35:08 -0700205 if (mtd_is_eccerr(res)) {
206 pr_info("nand_bbt: ECC error in BBT at "
207 "0x%012llx\n", from & ~mtd->writesize);
208 return res;
209 } else if (mtd_is_bitflip(res)) {
210 pr_info("nand_bbt: corrected error in BBT at "
211 "0x%012llx\n", from & ~mtd->writesize);
212 ret = res;
213 } else {
214 pr_info("nand_bbt: error reading BBT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return res;
216 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 /* Analyse data */
220 for (i = 0; i < len; i++) {
221 uint8_t dat = buf[i];
222 for (j = 0; j < 8; j += bits, act += 2) {
223 uint8_t tmp = (dat >> j) & msk;
224 if (tmp == msk)
225 continue;
David Woodhousee0c7d762006-05-13 18:07:53 +0100226 if (reserved_block_code && (tmp == reserved_block_code)) {
Brian Norrisd0370212011-07-19 10:06:08 -0700227 pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
228 (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200230 mtd->ecc_stats.bbtblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 continue;
232 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700233 /*
234 * Leave it for now, if it's matured we can
Brian Norrisa0f50802011-07-19 10:06:06 -0700235 * move this message to pr_debug.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700236 */
Brian Norrisd0370212011-07-19 10:06:08 -0700237 pr_info("nand_read_bbt: bad block at 0x%012llx\n",
238 (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700239 /* Factory marked bad or worn out? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (tmp == 0)
241 this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
242 else
243 this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200244 mtd->ecc_stats.badblocks++;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247 totlen -= len;
248 from += len;
249 }
Brian Norris167a8d52011-09-20 18:35:08 -0700250 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253/**
254 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
Brian Norris8b6e50c2011-05-25 14:59:01 -0700255 * @mtd: MTD device structure
256 * @buf: temporary buffer
257 * @td: descriptor for the bad block table
Brian Norris596d7452011-09-07 13:13:33 -0700258 * @chip: read the table for a specific chip, -1 read all chips; applies only if
Brian Norris8b6e50c2011-05-25 14:59:01 -0700259 * NAND_BBT_PERCHIP option is set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700261 * Read the bad block table for all chips starting at a given page. We assume
262 * that the bbt bits are in consecutive order.
263 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100264static 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 -0700265{
266 struct nand_chip *this = mtd->priv;
267 int res = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (td->options & NAND_BBT_PERCHIP) {
270 int offs = 0;
271 for (i = 0; i < this->numchips; i++) {
272 if (chip == -1 || chip == i)
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200273 res = read_bbt(mtd, buf, td->pages[i],
274 this->chipsize >> this->bbt_erase_shift,
275 td, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (res)
277 return res;
278 offs += this->chipsize >> (this->bbt_erase_shift + 2);
279 }
280 } else {
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200281 res = read_bbt(mtd, buf, td->pages[0],
282 mtd->size >> this->bbt_erase_shift, td, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (res)
284 return res;
285 }
286 return 0;
287}
288
Brian Norris8b6e50c2011-05-25 14:59:01 -0700289/* BBT marker is in the first page, no OOB */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200290static int scan_read_raw_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
291 struct nand_bbt_descr *td)
292{
293 size_t retlen;
294 size_t len;
295
296 len = td->len;
297 if (td->options & NAND_BBT_VERSION)
298 len++;
299
300 return mtd->read(mtd, offs, len, &retlen, buf);
301}
302
Brian Norris8b6e50c2011-05-25 14:59:01 -0700303/* Scan read raw data from flash */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200304static int scan_read_raw_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200305 size_t len)
306{
307 struct mtd_oob_ops ops;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200308 int res;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200309
Brian Norris0612b9d2011-08-30 18:45:40 -0700310 ops.mode = MTD_OPS_RAW;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200311 ops.ooboffs = 0;
312 ops.ooblen = mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200313
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200314 while (len > 0) {
Brian Norris105513c2011-09-07 13:13:28 -0700315 ops.datbuf = buf;
316 ops.len = min(len, (size_t)mtd->writesize);
317 ops.oobbuf = buf + ops.len;
Brian Norris903cd062011-06-28 16:28:58 -0700318
Brian Norris105513c2011-09-07 13:13:28 -0700319 res = mtd->read_oob(mtd, offs, &ops);
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200320
Brian Norrisafa17de2011-09-07 13:13:29 -0700321 if (res)
Brian Norris105513c2011-09-07 13:13:28 -0700322 return res;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200323
324 buf += mtd->oobsize + mtd->writesize;
325 len -= mtd->writesize;
326 }
327 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200328}
329
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200330static int scan_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
331 size_t len, struct nand_bbt_descr *td)
332{
333 if (td->options & NAND_BBT_NO_OOB)
334 return scan_read_raw_data(mtd, buf, offs, td);
335 else
336 return scan_read_raw_oob(mtd, buf, offs, len);
337}
338
Brian Norris8b6e50c2011-05-25 14:59:01 -0700339/* Scan write data with oob to flash */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200340static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
341 uint8_t *buf, uint8_t *oob)
342{
343 struct mtd_oob_ops ops;
344
Brian Norris0612b9d2011-08-30 18:45:40 -0700345 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200346 ops.ooboffs = 0;
347 ops.ooblen = mtd->oobsize;
348 ops.datbuf = buf;
349 ops.oobbuf = oob;
350 ops.len = len;
351
352 return mtd->write_oob(mtd, offs, &ops);
353}
354
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200355static u32 bbt_get_ver_offs(struct mtd_info *mtd, struct nand_bbt_descr *td)
356{
357 u32 ver_offs = td->veroffs;
358
359 if (!(td->options & NAND_BBT_NO_OOB))
360 ver_offs += mtd->writesize;
361 return ver_offs;
362}
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364/**
365 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
Brian Norris8b6e50c2011-05-25 14:59:01 -0700366 * @mtd: MTD device structure
367 * @buf: temporary buffer
368 * @td: descriptor for the bad block table
369 * @md: descriptor for the bad block table mirror
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700371 * Read the bad block table(s) for all chips starting at a given page. We
372 * assume that the bbt bits are in consecutive order.
373 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200374static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
375 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 struct nand_chip *this = mtd->priv;
378
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000379 /* Read the primary version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (td->options & NAND_BBT_VERSION) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000381 scan_read_raw(mtd, buf, (loff_t)td->pages[0] << this->page_shift,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200382 mtd->writesize, td);
383 td->version[0] = buf[bbt_get_ver_offs(mtd, td)];
Brian Norris9a4d4d62011-07-19 10:06:07 -0700384 pr_info("Bad block table at page %d, version 0x%02X\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700385 td->pages[0], td->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000388 /* Read the mirror version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (md && (md->options & NAND_BBT_VERSION)) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000390 scan_read_raw(mtd, buf, (loff_t)md->pages[0] << this->page_shift,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200391 mtd->writesize, td);
392 md->version[0] = buf[bbt_get_ver_offs(mtd, md)];
Brian Norris9a4d4d62011-07-19 10:06:07 -0700393 pr_info("Bad block table at page %d, version 0x%02X\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700394 md->pages[0], md->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return 1;
397}
398
Brian Norris8b6e50c2011-05-25 14:59:01 -0700399/* Scan a given block full */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200400static int scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
401 loff_t offs, uint8_t *buf, size_t readlen,
402 int scanlen, int len)
403{
404 int ret, j;
405
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200406 ret = scan_read_raw_oob(mtd, buf, offs, readlen);
Brian Norrisafa17de2011-09-07 13:13:29 -0700407 /* Ignore ECC errors when checking for BBM */
Brian Norrisd57f40542011-09-20 18:34:25 -0700408 if (ret && !mtd_is_bitflip_or_eccerr(ret))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200409 return ret;
410
411 for (j = 0; j < len; j++, buf += scanlen) {
412 if (check_pattern(buf, scanlen, mtd->writesize, bd))
413 return 1;
414 }
415 return 0;
416}
417
Brian Norris8b6e50c2011-05-25 14:59:01 -0700418/* Scan a given block partially */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200419static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
420 loff_t offs, uint8_t *buf, int len)
421{
422 struct mtd_oob_ops ops;
423 int j, ret;
424
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200425 ops.ooblen = mtd->oobsize;
426 ops.oobbuf = buf;
427 ops.ooboffs = 0;
428 ops.datbuf = NULL;
Brian Norris0612b9d2011-08-30 18:45:40 -0700429 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200430
431 for (j = 0; j < len; j++) {
432 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700433 * Read the full oob until read_oob is fixed to handle single
434 * byte reads for 16 bit buswidth.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200435 */
436 ret = mtd->read_oob(mtd, offs, &ops);
Brian Norris903cd062011-06-28 16:28:58 -0700437 /* Ignore ECC errors when checking for BBM */
Brian Norrisd57f40542011-09-20 18:34:25 -0700438 if (ret && !mtd_is_bitflip_or_eccerr(ret))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200439 return ret;
440
441 if (check_short_pattern(buf, bd))
442 return 1;
443
444 offs += mtd->writesize;
445 }
446 return 0;
447}
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449/**
450 * create_bbt - [GENERIC] Create a bad block table by scanning the device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700451 * @mtd: MTD device structure
452 * @buf: temporary buffer
453 * @bd: descriptor for the good/bad block search pattern
454 * @chip: create the table for a specific chip, -1 read all chips; applies only
455 * if NAND_BBT_PERCHIP option is set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700457 * Create a bad block table by scanning the device for the given good/bad block
458 * identify pattern.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200460static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
461 struct nand_bbt_descr *bd, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 struct nand_chip *this = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200464 int i, numblocks, len, scanlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 int startblock;
466 loff_t from;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200467 size_t readlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Brian Norris9a4d4d62011-07-19 10:06:07 -0700469 pr_info("Scanning device for bad blocks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 if (bd->options & NAND_BBT_SCANALLPAGES)
472 len = 1 << (this->bbt_erase_shift - this->page_shift);
Brian Norris58373ff2010-07-15 12:15:44 -0700473 else if (bd->options & NAND_BBT_SCAN2NDPAGE)
474 len = 2;
475 else
476 len = 1;
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000477
478 if (!(bd->options & NAND_BBT_SCANEMPTY)) {
479 /* We need only read few bytes from the OOB area */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200480 scanlen = 0;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000481 readlen = bd->len;
482 } else {
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000483 /* Full page content should be read */
Joern Engel28318772006-05-22 23:18:05 +0200484 scanlen = mtd->writesize + mtd->oobsize;
485 readlen = len * mtd->writesize;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 if (chip == -1) {
Brian Norris8b6e50c2011-05-25 14:59:01 -0700489 /*
490 * Note that numblocks is 2 * (real numblocks) here, see i+=2
491 * below as it makes shifting and masking less painful
492 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 numblocks = mtd->size >> (this->bbt_erase_shift - 1);
494 startblock = 0;
495 from = 0;
496 } else {
497 if (chip >= this->numchips) {
Brian Norris9a4d4d62011-07-19 10:06:07 -0700498 pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
David Woodhousee0c7d762006-05-13 18:07:53 +0100499 chip + 1, this->numchips);
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000500 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502 numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
503 startblock = chip * numblocks;
504 numblocks += startblock;
Adrian Hunter69423d92008-12-10 13:37:21 +0000505 from = (loff_t)startblock << (this->bbt_erase_shift - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000507
Brian Norris5fb15492011-05-31 16:31:21 -0700508 if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700509 from += mtd->erasesize - (mtd->writesize * len);
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 for (i = startblock; i < numblocks;) {
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000512 int ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000513
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200514 BUG_ON(bd->options & NAND_BBT_NO_OOB);
515
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200516 if (bd->options & NAND_BBT_SCANALLPAGES)
517 ret = scan_block_full(mtd, bd, from, buf, readlen,
518 scanlen, len);
519 else
520 ret = scan_block_fast(mtd, bd, from, buf, len);
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000521
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200522 if (ret < 0)
523 return ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000524
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200525 if (ret) {
526 this->bbt[i >> 3] |= 0x03 << (i & 0x6);
Brian Norris9a4d4d62011-07-19 10:06:07 -0700527 pr_warn("Bad eraseblock %d at 0x%012llx\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700528 i >> 1, (unsigned long long)from);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200529 mtd->ecc_stats.badblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 i += 2;
533 from += (1 << this->bbt_erase_shift);
534 }
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000535 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
538/**
539 * search_bbt - [GENERIC] scan the device for a specific bad block table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700540 * @mtd: MTD device structure
541 * @buf: temporary buffer
542 * @td: descriptor for the bad block table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700544 * Read the bad block table by searching for a given ident pattern. Search is
545 * preformed either from the beginning up or from the end of the device
546 * downwards. The search starts always at the start of a block. If the option
547 * NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains
548 * the bad block information of this chip. This is necessary to provide support
549 * for certain DOC devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700551 * The bbt ident pattern resides in the oob area of the first page in a block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100553static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 struct nand_chip *this = mtd->priv;
556 int i, chips;
557 int bits, startblock, block, dir;
Joern Engel28318772006-05-22 23:18:05 +0200558 int scanlen = mtd->writesize + mtd->oobsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 int bbtblocks;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200560 int blocktopage = this->bbt_erase_shift - this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Brian Norris8b6e50c2011-05-25 14:59:01 -0700562 /* Search direction top -> down? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (td->options & NAND_BBT_LASTBLOCK) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100564 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 dir = -1;
566 } else {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000567 startblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000569 }
570
Brian Norris8b6e50c2011-05-25 14:59:01 -0700571 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (td->options & NAND_BBT_PERCHIP) {
573 chips = this->numchips;
574 bbtblocks = this->chipsize >> this->bbt_erase_shift;
575 startblock &= bbtblocks - 1;
576 } else {
577 chips = 1;
578 bbtblocks = mtd->size >> this->bbt_erase_shift;
579 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* Number of bits for each erase block in the bbt */
582 bits = td->options & NAND_BBT_NRBITS_MSK;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 for (i = 0; i < chips; i++) {
585 /* Reset version information */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000586 td->version[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 td->pages[i] = -1;
588 /* Scan the maximum number of blocks */
589 for (block = 0; block < td->maxblocks; block++) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 int actblock = startblock + dir * block;
Adrian Hunter69423d92008-12-10 13:37:21 +0000592 loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /* Read first page */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200595 scan_read_raw(mtd, buf, offs, mtd->writesize, td);
Joern Engel28318772006-05-22 23:18:05 +0200596 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200597 td->pages[i] = actblock << blocktopage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (td->options & NAND_BBT_VERSION) {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200599 offs = bbt_get_ver_offs(mtd, td);
600 td->version[i] = buf[offs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602 break;
603 }
604 }
605 startblock += this->chipsize >> this->bbt_erase_shift;
606 }
607 /* Check, if we found a bbt for each requested chip */
608 for (i = 0; i < chips; i++) {
609 if (td->pages[i] == -1)
Brian Norris9a4d4d62011-07-19 10:06:07 -0700610 pr_warn("Bad block table not found for chip %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 else
Brian Norrisd0370212011-07-19 10:06:08 -0700612 pr_info("Bad block table found at page %d, version "
613 "0x%02X\n", td->pages[i], td->version[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000615 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
618/**
619 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -0700620 * @mtd: MTD device structure
621 * @buf: temporary buffer
622 * @td: descriptor for the bad block table
623 * @md: descriptor for the bad block table mirror
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700625 * Search and read the bad block table(s).
626 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100627static 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 -0700628{
629 /* Search the primary table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100630 search_bbt(mtd, buf, td);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /* Search the mirror table */
633 if (md)
David Woodhousee0c7d762006-05-13 18:07:53 +0100634 search_bbt(mtd, buf, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000636 /* Force result check */
637 return 1;
638}
639
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000640/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 * write_bbt - [GENERIC] (Re)write the bad block table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700642 * @mtd: MTD device structure
643 * @buf: temporary buffer
644 * @td: descriptor for the bad block table
645 * @md: descriptor for the bad block table mirror
646 * @chipsel: selector for a specific chip, -1 for all
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700648 * (Re)write the bad block table.
649 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100650static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
Thomas Gleixner9223a452006-05-23 17:21:03 +0200651 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
652 int chipsel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654 struct nand_chip *this = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 struct erase_info einfo;
656 int i, j, res, chip = 0;
657 int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200658 int nrchips, bbtoffs, pageoffs, ooboffs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 uint8_t msk[4];
660 uint8_t rcode = td->reserved_block_code;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200661 size_t retlen, len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 loff_t to;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200663 struct mtd_oob_ops ops;
664
665 ops.ooblen = mtd->oobsize;
666 ops.ooboffs = 0;
667 ops.datbuf = NULL;
Brian Norris0612b9d2011-08-30 18:45:40 -0700668 ops.mode = MTD_OPS_PLACE_OOB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 if (!rcode)
671 rcode = 0xff;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700672 /* Write bad block table per chip rather than per device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (td->options & NAND_BBT_PERCHIP) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100674 numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700675 /* Full device write or specific chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (chipsel == -1) {
677 nrchips = this->numchips;
678 } else {
679 nrchips = chipsel + 1;
680 chip = chipsel;
681 }
682 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +0100683 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 nrchips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000685 }
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 /* Loop through the chips */
688 for (; chip < nrchips; chip++) {
Brian Norris8b6e50c2011-05-25 14:59:01 -0700689 /*
690 * There was already a version of the table, reuse the page
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000691 * This applies for absolute placement too, as we have the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 * page nr. in td->pages.
693 */
694 if (td->pages[chip] != -1) {
695 page = td->pages[chip];
696 goto write;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Brian Norris8b6e50c2011-05-25 14:59:01 -0700699 /*
700 * Automatic placement of the bad block table. Search direction
701 * top -> down?
702 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (td->options & NAND_BBT_LASTBLOCK) {
704 startblock = numblocks * (chip + 1) - 1;
705 dir = -1;
706 } else {
707 startblock = chip * numblocks;
708 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 for (i = 0; i < td->maxblocks; i++) {
712 int block = startblock + dir * i;
713 /* Check, if the block is bad */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200714 switch ((this->bbt[block >> 2] >>
715 (2 * (block & 0x03))) & 0x03) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 case 0x01:
717 case 0x03:
718 continue;
719 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200720 page = block <<
721 (this->bbt_erase_shift - this->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 /* Check, if the block is used by the mirror table */
723 if (!md || md->pages[chip] != page)
724 goto write;
725 }
Brian Norris9a4d4d62011-07-19 10:06:07 -0700726 pr_err("No space left to write bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return -ENOSPC;
David Woodhousee0c7d762006-05-13 18:07:53 +0100728 write:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 /* Set up shift count and masks for the flash table */
731 bits = td->options & NAND_BBT_NRBITS_MSK;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200732 msk[2] = ~rcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 switch (bits) {
Thomas Gleixner9223a452006-05-23 17:21:03 +0200734 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
735 msk[3] = 0x01;
736 break;
737 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
738 msk[3] = 0x03;
739 break;
740 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
741 msk[3] = 0x0f;
742 break;
743 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
744 msk[3] = 0xff;
745 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 default: return -EINVAL;
747 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 bbtoffs = chip * (numblocks >> 2);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000750
Brian Norris596d7452011-09-07 13:13:33 -0700751 to = ((loff_t)page) << this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Brian Norris8b6e50c2011-05-25 14:59:01 -0700753 /* Must we save the block contents? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (td->options & NAND_BBT_SAVECONTENT) {
755 /* Make it block aligned */
Brian Norris596d7452011-09-07 13:13:33 -0700756 to &= ~((loff_t)((1 << this->bbt_erase_shift) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 len = 1 << this->bbt_erase_shift;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200758 res = mtd->read(mtd, to, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (res < 0) {
760 if (retlen != len) {
Brian Norrisd0370212011-07-19 10:06:08 -0700761 pr_info("nand_bbt: error reading block "
762 "for writing the bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return res;
764 }
Brian Norrisd0370212011-07-19 10:06:08 -0700765 pr_warn("nand_bbt: ECC error while reading "
766 "block for writing bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200768 /* Read oob data */
Vitaly Wool70145682006-11-03 18:20:38 +0300769 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200770 ops.oobbuf = &buf[len];
771 res = mtd->read_oob(mtd, to + mtd->writesize, &ops);
Vitaly Wool70145682006-11-03 18:20:38 +0300772 if (res < 0 || ops.oobretlen != ops.ooblen)
Thomas Gleixner9223a452006-05-23 17:21:03 +0200773 goto outerr;
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 /* Calc the byte offset in the buffer */
776 pageoffs = page - (int)(to >> this->page_shift);
777 offs = pageoffs << this->page_shift;
778 /* Preset the bbt area with 0xff */
Brian Norris596d7452011-09-07 13:13:33 -0700779 memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
Thomas Gleixner9223a452006-05-23 17:21:03 +0200780 ooboffs = len + (pageoffs * mtd->oobsize);
781
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200782 } else if (td->options & NAND_BBT_NO_OOB) {
783 ooboffs = 0;
784 offs = td->len;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700785 /* The version byte */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200786 if (td->options & NAND_BBT_VERSION)
787 offs++;
788 /* Calc length */
Brian Norris596d7452011-09-07 13:13:33 -0700789 len = (size_t)(numblocks >> sft);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200790 len += offs;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700791 /* Make it page aligned! */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200792 len = ALIGN(len, mtd->writesize);
793 /* Preset the buffer with 0xff */
794 memset(buf, 0xff, len);
795 /* Pattern is located at the begin of first page */
796 memcpy(buf, td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 } else {
798 /* Calc length */
Brian Norris596d7452011-09-07 13:13:33 -0700799 len = (size_t)(numblocks >> sft);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700800 /* Make it page aligned! */
Sebastian Andrzej Siewiorcda32092010-09-29 19:43:50 +0200801 len = ALIGN(len, mtd->writesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 /* Preset the buffer with 0xff */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200803 memset(buf, 0xff, len +
804 (len >> this->page_shift)* mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 offs = 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200806 ooboffs = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 /* Pattern is located in oob area of first page */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200808 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000810
Thomas Gleixner9223a452006-05-23 17:21:03 +0200811 if (td->options & NAND_BBT_VERSION)
812 buf[ooboffs + td->veroffs] = td->version[chip];
813
Brian Norris8b6e50c2011-05-25 14:59:01 -0700814 /* Walk through the memory table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100815 for (i = 0; i < numblocks;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 uint8_t dat;
817 dat = this->bbt[bbtoffs + (i >> 2)];
David Woodhousee0c7d762006-05-13 18:07:53 +0100818 for (j = 0; j < 4; j++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 int sftcnt = (i << (3 - sft)) & sftmsk;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700820 /* Do not store the reserved bbt blocks! */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200821 buf[offs + (i >> sft)] &=
822 ~(msk[dat & 0x03] << sftcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 dat >>= 2;
824 }
825 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000826
David Woodhousee0c7d762006-05-13 18:07:53 +0100827 memset(&einfo, 0, sizeof(einfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 einfo.mtd = mtd;
Adrian Hunter69423d92008-12-10 13:37:21 +0000829 einfo.addr = to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 einfo.len = 1 << this->bbt_erase_shift;
David Woodhousee0c7d762006-05-13 18:07:53 +0100831 res = nand_erase_nand(mtd, &einfo, 1);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200832 if (res < 0)
833 goto outerr;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000834
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200835 res = scan_write_bbt(mtd, to, len, buf,
836 td->options & NAND_BBT_NO_OOB ? NULL :
837 &buf[len]);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200838 if (res < 0)
839 goto outerr;
840
Brian Norrisd0370212011-07-19 10:06:08 -0700841 pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
842 (unsigned long long)to, td->version[chip]);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /* Mark it as used */
845 td->pages[chip] = page;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200848
849 outerr:
Brian Norrisd0370212011-07-19 10:06:08 -0700850 pr_warn("nand_bbt: error while writing bad block table %d\n", res);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200851 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
854/**
855 * nand_memory_bbt - [GENERIC] create a memory based bad block table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700856 * @mtd: MTD device structure
857 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700859 * The function creates a memory based bbt by scanning the device for
860 * manufacturer / software marked good / bad blocks.
861 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100862static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864 struct nand_chip *this = mtd->priv;
865
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000866 bd->options &= ~NAND_BBT_SCANEMPTY;
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100867 return create_bbt(mtd, this->buffers->databuf, bd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
870/**
David Woodhousee0c7d762006-05-13 18:07:53 +0100871 * check_create - [GENERIC] create and write bbt(s) if necessary
Brian Norris8b6e50c2011-05-25 14:59:01 -0700872 * @mtd: MTD device structure
873 * @buf: temporary buffer
874 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700876 * The function checks the results of the previous call to read_bbt and creates
877 * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found
878 * for the chip/device. Update is necessary if one of the tables is missing or
879 * the version nr. of one table is less than the other.
880 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100881static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Brian Norris623978d2011-09-20 18:35:34 -0700883 int i, chips, writeops, create, chipsel, res, res2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 struct nand_chip *this = mtd->priv;
885 struct nand_bbt_descr *td = this->bbt_td;
886 struct nand_bbt_descr *md = this->bbt_md;
887 struct nand_bbt_descr *rd, *rd2;
888
Brian Norris8b6e50c2011-05-25 14:59:01 -0700889 /* Do we have a bbt per chip? */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000890 if (td->options & NAND_BBT_PERCHIP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 chips = this->numchips;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000892 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 chips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 for (i = 0; i < chips; i++) {
896 writeops = 0;
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700897 create = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 rd = NULL;
899 rd2 = NULL;
Brian Norris623978d2011-09-20 18:35:34 -0700900 res = res2 = 0;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700901 /* Per chip or per device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700903 /* Mirrored table available? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (md) {
905 if (td->pages[i] == -1 && md->pages[i] == -1) {
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700906 create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 writeops = 0x03;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700908 } else if (td->pages[i] == -1) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000909 rd = md;
Brian Norris596d7452011-09-07 13:13:33 -0700910 writeops = 0x01;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700911 } else if (md->pages[i] == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 rd = td;
Brian Norris596d7452011-09-07 13:13:33 -0700913 writeops = 0x02;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700914 } else if (td->version[i] == md->version[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 rd = td;
916 if (!(td->options & NAND_BBT_VERSION))
917 rd2 = md;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700918 } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 rd = td;
Brian Norris596d7452011-09-07 13:13:33 -0700920 writeops = 0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 } else {
922 rd = md;
Brian Norris596d7452011-09-07 13:13:33 -0700923 writeops = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 } else {
926 if (td->pages[i] == -1) {
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700927 create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 writeops = 0x01;
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700929 } else {
930 rd = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000933
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700934 if (create) {
935 /* Create the bad block table by scanning the device? */
936 if (!(td->options & NAND_BBT_CREATE))
937 continue;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000938
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700939 /* Create the table in memory by scanning the chip(s) */
940 if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
941 create_bbt(mtd, buf, bd, chipsel);
942
943 td->version[i] = 1;
944 if (md)
945 md->version[i] = 1;
946 }
947
Brian Norris8b6e50c2011-05-25 14:59:01 -0700948 /* Read back first? */
Brian Norris623978d2011-09-20 18:35:34 -0700949 if (rd) {
950 res = read_abs_bbt(mtd, buf, rd, chipsel);
951 if (mtd_is_eccerr(res)) {
952 /* Mark table as invalid */
953 rd->pages[i] = -1;
Brian Norrisdadc17a2011-09-20 18:35:57 -0700954 rd->version[i] = 0;
Brian Norris623978d2011-09-20 18:35:34 -0700955 i--;
956 continue;
957 }
958 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700959 /* If they weren't versioned, read both */
Brian Norris623978d2011-09-20 18:35:34 -0700960 if (rd2) {
961 res2 = read_abs_bbt(mtd, buf, rd2, chipsel);
962 if (mtd_is_eccerr(res2)) {
963 /* Mark table as invalid */
964 rd2->pages[i] = -1;
Brian Norrisdadc17a2011-09-20 18:35:57 -0700965 rd2->version[i] = 0;
Brian Norris623978d2011-09-20 18:35:34 -0700966 i--;
967 continue;
968 }
969 }
970
971 /* Scrub the flash table(s)? */
972 if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
973 writeops = 0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Brian Norrisdadc17a2011-09-20 18:35:57 -0700975 /* Update version numbers before writing */
976 if (md) {
977 td->version[i] = max(td->version[i], md->version[i]);
978 md->version[i] = td->version[i];
979 }
980
Brian Norris8b6e50c2011-05-25 14:59:01 -0700981 /* Write the bad block table to the device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100983 res = write_bbt(mtd, buf, td, md, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (res < 0)
985 return res;
986 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000987
Brian Norris8b6e50c2011-05-25 14:59:01 -0700988 /* Write the mirror bad block table to the device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100990 res = write_bbt(mtd, buf, md, td, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (res < 0)
992 return res;
993 }
994 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000995 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
998/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000999 * mark_bbt_regions - [GENERIC] mark the bad block table regions
Brian Norris8b6e50c2011-05-25 14:59:01 -07001000 * @mtd: MTD device structure
1001 * @td: bad block table descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001003 * The bad block table regions are marked as "bad" to prevent accidental
1004 * erasures / writes. The regions are identified by the mark 0x02.
1005 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001006static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008 struct nand_chip *this = mtd->priv;
1009 int i, j, chips, block, nrblocks, update;
1010 uint8_t oldval, newval;
1011
Brian Norris8b6e50c2011-05-25 14:59:01 -07001012 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if (td->options & NAND_BBT_PERCHIP) {
1014 chips = this->numchips;
1015 nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
1016 } else {
1017 chips = 1;
1018 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001019 }
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 for (i = 0; i < chips; i++) {
1022 if ((td->options & NAND_BBT_ABSPAGE) ||
1023 !(td->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001024 if (td->pages[i] == -1)
1025 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001027 block <<= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 oldval = this->bbt[(block >> 3)];
1029 newval = oldval | (0x2 << (block & 0x06));
1030 this->bbt[(block >> 3)] = newval;
1031 if ((oldval != newval) && td->reserved_block_code)
Adrian Hunter69423d92008-12-10 13:37:21 +00001032 nand_update_bbt(mtd, (loff_t)block << (this->bbt_erase_shift - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 continue;
1034 }
1035 update = 0;
1036 if (td->options & NAND_BBT_LASTBLOCK)
1037 block = ((i + 1) * nrblocks) - td->maxblocks;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001038 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 block = i * nrblocks;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001040 block <<= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 for (j = 0; j < td->maxblocks; j++) {
1042 oldval = this->bbt[(block >> 3)];
1043 newval = oldval | (0x2 << (block & 0x06));
1044 this->bbt[(block >> 3)] = newval;
David Woodhousee0c7d762006-05-13 18:07:53 +01001045 if (oldval != newval)
1046 update = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 block += 2;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001048 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07001049 /*
1050 * If we want reserved blocks to be recorded to flash, and some
1051 * new ones have been marked, then we need to update the stored
1052 * bbts. This should only happen once.
1053 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (update && td->reserved_block_code)
Adrian Hunter69423d92008-12-10 13:37:21 +00001055 nand_update_bbt(mtd, (loff_t)(block - 2) << (this->bbt_erase_shift - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057}
1058
1059/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001060 * verify_bbt_descr - verify the bad block description
Brian Norris8b6e50c2011-05-25 14:59:01 -07001061 * @mtd: MTD device structure
1062 * @bd: the table to verify
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001063 *
1064 * This functions performs a few sanity checks on the bad block description
1065 * table.
1066 */
1067static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
1068{
1069 struct nand_chip *this = mtd->priv;
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001070 u32 pattern_len;
1071 u32 bits;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001072 u32 table_size;
1073
1074 if (!bd)
1075 return;
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001076
1077 pattern_len = bd->len;
1078 bits = bd->options & NAND_BBT_NRBITS_MSK;
1079
Brian Norrisa40f7342011-05-31 16:31:22 -07001080 BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001081 !(this->bbt_options & NAND_BBT_USE_FLASH));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001082 BUG_ON(!bits);
1083
1084 if (bd->options & NAND_BBT_VERSION)
1085 pattern_len++;
1086
1087 if (bd->options & NAND_BBT_NO_OOB) {
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001088 BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
Brian Norrisa40f7342011-05-31 16:31:22 -07001089 BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001090 BUG_ON(bd->offs);
1091 if (bd->options & NAND_BBT_VERSION)
1092 BUG_ON(bd->veroffs != bd->len);
1093 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1094 }
1095
1096 if (bd->options & NAND_BBT_PERCHIP)
1097 table_size = this->chipsize >> this->bbt_erase_shift;
1098 else
1099 table_size = mtd->size >> this->bbt_erase_shift;
1100 table_size >>= 3;
1101 table_size *= bits;
1102 if (bd->options & NAND_BBT_NO_OOB)
1103 table_size += pattern_len;
1104 BUG_ON(table_size > (1 << this->bbt_erase_shift));
1105}
1106
1107/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07001109 * @mtd: MTD device structure
1110 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001112 * The function checks, if a bad block table(s) is/are already available. If
1113 * not it scans the device for manufacturer marked good / bad blocks and writes
1114 * the bad block table(s) to the selected place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001116 * The bad block table memory is allocated here. It must be freed by calling
1117 * the nand_free_bbt function.
1118 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001119int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 struct nand_chip *this = mtd->priv;
1122 int len, res = 0;
1123 uint8_t *buf;
1124 struct nand_bbt_descr *td = this->bbt_td;
1125 struct nand_bbt_descr *md = this->bbt_md;
1126
1127 len = mtd->size >> (this->bbt_erase_shift + 2);
Brian Norris8b6e50c2011-05-25 14:59:01 -07001128 /*
1129 * Allocate memory (2bit per block) and clear the memory bad block
1130 * table.
1131 */
Burman Yan95b93a02006-11-15 21:10:29 +02001132 this->bbt = kzalloc(len, GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001133 if (!this->bbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Brian Norris8b6e50c2011-05-25 14:59:01 -07001136 /*
1137 * If no primary table decriptor is given, scan the device to build a
1138 * memory based bad block table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 */
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001140 if (!td) {
1141 if ((res = nand_memory_bbt(mtd, bd))) {
Brian Norrisd0370212011-07-19 10:06:08 -07001142 pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
David Woodhousee0c7d762006-05-13 18:07:53 +01001143 kfree(this->bbt);
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001144 this->bbt = NULL;
1145 }
1146 return res;
1147 }
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001148 verify_bbt_descr(mtd, td);
1149 verify_bbt_descr(mtd, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 /* Allocate a temporary buffer for one eraseblock incl. oob */
1152 len = (1 << this->bbt_erase_shift);
1153 len += (len >> this->page_shift) * mtd->oobsize;
David Woodhousec3f8abf2006-05-13 04:03:42 +01001154 buf = vmalloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (!buf) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001156 kfree(this->bbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 this->bbt = NULL;
1158 return -ENOMEM;
1159 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001160
Brian Norris8b6e50c2011-05-25 14:59:01 -07001161 /* Is the bbt at a given page? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (td->options & NAND_BBT_ABSPAGE) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001163 res = read_abs_bbts(mtd, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001164 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /* Search the bad block table using a pattern in oob */
David Woodhousee0c7d762006-05-13 18:07:53 +01001166 res = search_read_bbts(mtd, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001169 if (res)
David Woodhousee0c7d762006-05-13 18:07:53 +01001170 res = check_create(mtd, buf, bd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 /* Prevent the bbt regions from erasing / writing */
David Woodhousee0c7d762006-05-13 18:07:53 +01001173 mark_bbt_region(mtd, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (md)
David Woodhousee0c7d762006-05-13 18:07:53 +01001175 mark_bbt_region(mtd, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001176
David Woodhousee0c7d762006-05-13 18:07:53 +01001177 vfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 return res;
1179}
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001182 * nand_update_bbt - [NAND Interface] update bad block table(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07001183 * @mtd: MTD device structure
1184 * @offs: the offset of the newly marked block
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001186 * The function updates the bad block table(s).
1187 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001188int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
1190 struct nand_chip *this = mtd->priv;
Brian Norris1196ac52011-09-07 13:13:32 -07001191 int len, res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 int chip, chipsel;
1193 uint8_t *buf;
1194 struct nand_bbt_descr *td = this->bbt_td;
1195 struct nand_bbt_descr *md = this->bbt_md;
1196
1197 if (!this->bbt || !td)
1198 return -EINVAL;
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 /* Allocate a temporary buffer for one eraseblock incl. oob */
1201 len = (1 << this->bbt_erase_shift);
1202 len += (len >> this->page_shift) * mtd->oobsize;
David Woodhousee0c7d762006-05-13 18:07:53 +01001203 buf = kmalloc(len, GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001204 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return -ENOMEM;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001206
Brian Norris8b6e50c2011-05-25 14:59:01 -07001207 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (td->options & NAND_BBT_PERCHIP) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001209 chip = (int)(offs >> this->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 chipsel = chip;
1211 } else {
1212 chip = 0;
1213 chipsel = -1;
1214 }
1215
1216 td->version[chip]++;
1217 if (md)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001218 md->version[chip]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Brian Norris8b6e50c2011-05-25 14:59:01 -07001220 /* Write the bad block table to the device? */
Brian Norris1196ac52011-09-07 13:13:32 -07001221 if (td->options & NAND_BBT_WRITE) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001222 res = write_bbt(mtd, buf, td, md, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (res < 0)
1224 goto out;
1225 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07001226 /* Write the mirror bad block table to the device? */
Brian Norris1196ac52011-09-07 13:13:32 -07001227 if (md && (md->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001228 res = write_bbt(mtd, buf, md, td, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
1230
David Woodhousee0c7d762006-05-13 18:07:53 +01001231 out:
1232 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return res;
1234}
1235
Brian Norris8b6e50c2011-05-25 14:59:01 -07001236/*
1237 * Define some generic bad / good block scan pattern which are used
1238 * while scanning a device for factory marked good / bad blocks.
1239 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
1243
1244static struct nand_bbt_descr agand_flashbased = {
1245 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
1246 .offs = 0x20,
1247 .len = 6,
1248 .pattern = scan_agand_pattern
1249};
1250
Brian Norris7854d3f2011-06-23 14:12:08 -07001251/* Generic flash bbt descriptors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1253static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1254
1255static struct nand_bbt_descr bbt_main_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001256 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1258 .offs = 8,
1259 .len = 4,
1260 .veroffs = 12,
1261 .maxblocks = 4,
1262 .pattern = bbt_pattern
1263};
1264
1265static struct nand_bbt_descr bbt_mirror_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001266 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1268 .offs = 8,
1269 .len = 4,
1270 .veroffs = 12,
1271 .maxblocks = 4,
1272 .pattern = mirror_pattern
1273};
1274
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001275static struct nand_bbt_descr bbt_main_no_bbt_descr = {
1276 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1277 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1278 | NAND_BBT_NO_OOB,
1279 .len = 4,
1280 .veroffs = 4,
1281 .maxblocks = 4,
1282 .pattern = bbt_pattern
1283};
1284
1285static struct nand_bbt_descr bbt_mirror_no_bbt_descr = {
1286 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1287 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1288 | NAND_BBT_NO_OOB,
1289 .len = 4,
1290 .veroffs = 4,
1291 .maxblocks = 4,
1292 .pattern = mirror_pattern
1293};
1294
Brian Norris752ed6c2011-09-20 18:36:42 -07001295#define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
Brian Norris58373ff2010-07-15 12:15:44 -07001296/**
Brian Norris752ed6c2011-09-20 18:36:42 -07001297 * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001298 * @this: NAND chip to create descriptor for
Brian Norris58373ff2010-07-15 12:15:44 -07001299 *
1300 * This function allocates and initializes a nand_bbt_descr for BBM detection
Brian Norris752ed6c2011-09-20 18:36:42 -07001301 * based on the properties of @this. The new descriptor is stored in
Brian Norris58373ff2010-07-15 12:15:44 -07001302 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1303 * passed to this function.
Brian Norris58373ff2010-07-15 12:15:44 -07001304 */
Brian Norris752ed6c2011-09-20 18:36:42 -07001305static int nand_create_badblock_pattern(struct nand_chip *this)
Brian Norris58373ff2010-07-15 12:15:44 -07001306{
1307 struct nand_bbt_descr *bd;
1308 if (this->badblock_pattern) {
Brian Norris752ed6c2011-09-20 18:36:42 -07001309 pr_warn("Bad block pattern already allocated; not replacing\n");
Brian Norris58373ff2010-07-15 12:15:44 -07001310 return -EINVAL;
1311 }
1312 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001313 if (!bd)
Brian Norris58373ff2010-07-15 12:15:44 -07001314 return -ENOMEM;
Brian Norris752ed6c2011-09-20 18:36:42 -07001315 bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
Brian Norris58373ff2010-07-15 12:15:44 -07001316 bd->offs = this->badblockpos;
1317 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1318 bd->pattern = scan_ff_pattern;
1319 bd->options |= NAND_BBT_DYNAMICSTRUCT;
1320 this->badblock_pattern = bd;
1321 return 0;
1322}
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001325 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
Brian Norris8b6e50c2011-05-25 14:59:01 -07001326 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001328 * This function selects the default bad block table support for the device and
1329 * calls the nand_scan_bbt function.
1330 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001331int nand_default_bbt(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
1333 struct nand_chip *this = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001334
Brian Norris8b6e50c2011-05-25 14:59:01 -07001335 /*
1336 * Default for AG-AND. We must use a flash based bad block table as the
1337 * devices have factory marked _good_ blocks. Erasing those blocks
1338 * leads to loss of the good / bad information, so we _must_ store this
1339 * information in a good / bad table during startup.
David Woodhousee0c7d762006-05-13 18:07:53 +01001340 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (this->options & NAND_IS_AND) {
1342 /* Use the default pattern descriptors */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001343 if (!this->bbt_td) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 this->bbt_td = &bbt_main_descr;
1345 this->bbt_md = &bbt_mirror_descr;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001346 }
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001347 this->bbt_options |= NAND_BBT_USE_FLASH;
David Woodhousee0c7d762006-05-13 18:07:53 +01001348 return nand_scan_bbt(mtd, &agand_flashbased);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001350
Brian Norris8b6e50c2011-05-25 14:59:01 -07001351 /* Is a flash based bad block table requested? */
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001352 if (this->bbt_options & NAND_BBT_USE_FLASH) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001353 /* Use the default pattern descriptors */
1354 if (!this->bbt_td) {
Brian Norrisa40f7342011-05-31 16:31:22 -07001355 if (this->bbt_options & NAND_BBT_NO_OOB) {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001356 this->bbt_td = &bbt_main_no_bbt_descr;
1357 this->bbt_md = &bbt_mirror_no_bbt_descr;
1358 } else {
1359 this->bbt_td = &bbt_main_descr;
1360 this->bbt_md = &bbt_mirror_descr;
1361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 } else {
1364 this->bbt_td = NULL;
1365 this->bbt_md = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
Brian Norrisa2f812d2011-03-18 21:53:42 -07001367
1368 if (!this->badblock_pattern)
Brian Norris752ed6c2011-09-20 18:36:42 -07001369 nand_create_badblock_pattern(this);
Brian Norrisa2f812d2011-03-18 21:53:42 -07001370
David Woodhousee0c7d762006-05-13 18:07:53 +01001371 return nand_scan_bbt(mtd, this->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372}
1373
1374/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001375 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07001376 * @mtd: MTD device structure
1377 * @offs: offset in the device
1378 * @allowbbt: allow access to bad block table region
1379 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001380int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 struct nand_chip *this = mtd->priv;
1383 int block;
David Woodhousee0c7d762006-05-13 18:07:53 +01001384 uint8_t res;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 /* Get block number * 2 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001387 block = (int)(offs >> (this->bbt_erase_shift - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
1389
Brian Norris289c0522011-07-19 10:06:09 -07001390 pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: "
1391 "(block %d) 0x%02x\n",
1392 (unsigned int)offs, block >> 1, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 switch ((int)res) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001395 case 0x00:
1396 return 0;
1397 case 0x01:
1398 return 1;
1399 case 0x02:
1400 return allowbbt ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
1402 return 1;
1403}
1404
David Woodhousee0c7d762006-05-13 18:07:53 +01001405EXPORT_SYMBOL(nand_scan_bbt);
1406EXPORT_SYMBOL(nand_default_bbt);