blob: 69148ae3bf58aaf806be617894725eb72d1483f1 [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>
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
Brian Norris8b6e50c2011-05-25 14:59:01 -070084 * @buf: the buffer to search
85 * @len: the length of buffer to search
86 * @paglen: the pagelength
87 * @td: search pattern descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 *
Brian Norris8b6e50c2011-05-25 14:59:01 -070089 * Check for a pattern at the given place. Used to search bad block tables and
90 * good / bad block identifiers. If the SCAN_EMPTY option is set then check, if
91 * all bytes except the pattern area contain 0xff.
92 */
David Woodhousee0c7d762006-05-13 18:07:53 +010093static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +000095 int i, end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 uint8_t *p = buf;
97
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020098 if (td->options & NAND_BBT_NO_OOB)
99 return check_pattern_no_oob(buf, td);
100
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100101 end = paglen + td->offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (td->options & NAND_BBT_SCANEMPTY) {
103 for (i = 0; i < end; i++) {
104 if (p[i] != 0xff)
105 return -1;
106 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000107 }
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100108 p += end;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 /* Compare the pattern */
Brian Norris75b66d82011-09-07 13:13:41 -0700111 if (memcmp(p, td->pattern, td->len))
112 return -1;
Brian Norris58373ff2010-07-15 12:15:44 -0700113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (td->options & NAND_BBT_SCANEMPTY) {
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000115 p += td->len;
116 end += td->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 for (i = end; i < len; i++) {
118 if (*p++ != 0xff)
119 return -1;
120 }
121 }
122 return 0;
123}
124
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000125/**
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100126 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700127 * @buf: the buffer to search
128 * @td: search pattern descriptor
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100129 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700130 * Check for a pattern at the given place. Used to search bad block tables and
131 * good / bad block identifiers. Same as check_pattern, but no optional empty
132 * check.
133 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100134static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100135{
136 int i;
137 uint8_t *p = buf;
138
139 /* Compare the pattern */
140 for (i = 0; i < td->len; i++) {
Thomas Gleixner19870da2005-07-15 14:53:51 +0100141 if (p[td->offs + i] != td->pattern[i])
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100142 return -1;
143 }
144 return 0;
145}
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200148 * add_marker_len - compute the length of the marker in data area
Brian Norris8b6e50c2011-05-25 14:59:01 -0700149 * @td: BBT descriptor used for computation
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200150 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700151 * The length will be 0 if the marker is located in OOB area.
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200152 */
153static u32 add_marker_len(struct nand_bbt_descr *td)
154{
155 u32 len;
156
157 if (!(td->options & NAND_BBT_NO_OOB))
158 return 0;
159
160 len = td->len;
161 if (td->options & NAND_BBT_VERSION)
162 len++;
163 return len;
164}
165
166/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 * read_bbt - [GENERIC] Read the bad block table starting from page
Brian Norris8b6e50c2011-05-25 14:59:01 -0700168 * @mtd: MTD device structure
169 * @buf: temporary buffer
170 * @page: the starting page
171 * @num: the number of bbt descriptors to read
Brian Norris7854d3f2011-06-23 14:12:08 -0700172 * @td: the bbt describtion table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700173 * @offs: offset in the memory table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 *
175 * Read the bad block table starting from page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100177static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200178 struct nand_bbt_descr *td, int offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Brian Norris167a8d52011-09-20 18:35:08 -0700180 int res, ret = 0, i, j, act = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 struct nand_chip *this = mtd->priv;
182 size_t retlen, len, totlen;
183 loff_t from;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200184 int bits = td->options & NAND_BBT_NRBITS_MSK;
Brian Norris596d7452011-09-07 13:13:33 -0700185 uint8_t msk = (uint8_t)((1 << bits) - 1);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200186 u32 marker_len;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200187 int reserved_block_code = td->reserved_block_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 totlen = (num * bits) >> 3;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200190 marker_len = add_marker_len(td);
Brian Norris596d7452011-09-07 13:13:33 -0700191 from = ((loff_t)page) << this->page_shift;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 while (totlen) {
Brian Norris596d7452011-09-07 13:13:33 -0700194 len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200195 if (marker_len) {
196 /*
197 * In case the BBT marker is not in the OOB area it
198 * will be just in the first page.
199 */
200 len -= marker_len;
201 from += marker_len;
202 marker_len = 0;
203 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200204 res = mtd->read(mtd, from, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (res < 0) {
Brian Norris167a8d52011-09-20 18:35:08 -0700206 if (mtd_is_eccerr(res)) {
207 pr_info("nand_bbt: ECC error in BBT at "
208 "0x%012llx\n", from & ~mtd->writesize);
209 return res;
210 } else if (mtd_is_bitflip(res)) {
211 pr_info("nand_bbt: corrected error in BBT at "
212 "0x%012llx\n", from & ~mtd->writesize);
213 ret = res;
214 } else {
215 pr_info("nand_bbt: error reading BBT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return res;
217 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 /* Analyse data */
221 for (i = 0; i < len; i++) {
222 uint8_t dat = buf[i];
223 for (j = 0; j < 8; j += bits, act += 2) {
224 uint8_t tmp = (dat >> j) & msk;
225 if (tmp == msk)
226 continue;
David Woodhousee0c7d762006-05-13 18:07:53 +0100227 if (reserved_block_code && (tmp == reserved_block_code)) {
Brian Norrisd0370212011-07-19 10:06:08 -0700228 pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
229 (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200231 mtd->ecc_stats.bbtblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 continue;
233 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700234 /*
235 * Leave it for now, if it's matured we can
Brian Norrisa0f50802011-07-19 10:06:06 -0700236 * move this message to pr_debug.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700237 */
Brian Norrisd0370212011-07-19 10:06:08 -0700238 pr_info("nand_read_bbt: bad block at 0x%012llx\n",
239 (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700240 /* Factory marked bad or worn out? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (tmp == 0)
242 this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
243 else
244 this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200245 mtd->ecc_stats.badblocks++;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248 totlen -= len;
249 from += len;
250 }
Brian Norris167a8d52011-09-20 18:35:08 -0700251 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254/**
255 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
Brian Norris8b6e50c2011-05-25 14:59:01 -0700256 * @mtd: MTD device structure
257 * @buf: temporary buffer
258 * @td: descriptor for the bad block table
Brian Norris596d7452011-09-07 13:13:33 -0700259 * @chip: read the table for a specific chip, -1 read all chips; applies only if
Brian Norris8b6e50c2011-05-25 14:59:01 -0700260 * NAND_BBT_PERCHIP option is set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700262 * Read the bad block table for all chips starting at a given page. We assume
263 * that the bbt bits are in consecutive order.
264 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100265static 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 -0700266{
267 struct nand_chip *this = mtd->priv;
268 int res = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (td->options & NAND_BBT_PERCHIP) {
271 int offs = 0;
272 for (i = 0; i < this->numchips; i++) {
273 if (chip == -1 || chip == i)
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200274 res = read_bbt(mtd, buf, td->pages[i],
275 this->chipsize >> this->bbt_erase_shift,
276 td, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (res)
278 return res;
279 offs += this->chipsize >> (this->bbt_erase_shift + 2);
280 }
281 } else {
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200282 res = read_bbt(mtd, buf, td->pages[0],
283 mtd->size >> this->bbt_erase_shift, td, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (res)
285 return res;
286 }
287 return 0;
288}
289
Brian Norris8b6e50c2011-05-25 14:59:01 -0700290/* BBT marker is in the first page, no OOB */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200291static int scan_read_raw_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
292 struct nand_bbt_descr *td)
293{
294 size_t retlen;
295 size_t len;
296
297 len = td->len;
298 if (td->options & NAND_BBT_VERSION)
299 len++;
300
301 return mtd->read(mtd, offs, len, &retlen, buf);
302}
303
Brian Norris8b6e50c2011-05-25 14:59:01 -0700304/* Scan read raw data from flash */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200305static int scan_read_raw_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200306 size_t len)
307{
308 struct mtd_oob_ops ops;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200309 int res;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200310
Brian Norris0612b9d2011-08-30 18:45:40 -0700311 ops.mode = MTD_OPS_RAW;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200312 ops.ooboffs = 0;
313 ops.ooblen = mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200314
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200315 while (len > 0) {
Brian Norris105513c2011-09-07 13:13:28 -0700316 ops.datbuf = buf;
317 ops.len = min(len, (size_t)mtd->writesize);
318 ops.oobbuf = buf + ops.len;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200319
Brian Norris105513c2011-09-07 13:13:28 -0700320 res = mtd->read_oob(mtd, offs, &ops);
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200321
Brian Norrisafa17de2011-09-07 13:13:29 -0700322 if (res)
Brian Norris105513c2011-09-07 13:13:28 -0700323 return res;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200324
325 buf += mtd->oobsize + mtd->writesize;
326 len -= mtd->writesize;
327 }
328 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200329}
330
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200331static int scan_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
332 size_t len, struct nand_bbt_descr *td)
333{
334 if (td->options & NAND_BBT_NO_OOB)
335 return scan_read_raw_data(mtd, buf, offs, td);
336 else
337 return scan_read_raw_oob(mtd, buf, offs, len);
338}
339
Brian Norris8b6e50c2011-05-25 14:59:01 -0700340/* Scan write data with oob to flash */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200341static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
342 uint8_t *buf, uint8_t *oob)
343{
344 struct mtd_oob_ops ops;
345
Brian Norris0612b9d2011-08-30 18:45:40 -0700346 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200347 ops.ooboffs = 0;
348 ops.ooblen = mtd->oobsize;
349 ops.datbuf = buf;
350 ops.oobbuf = oob;
351 ops.len = len;
352
353 return mtd->write_oob(mtd, offs, &ops);
354}
355
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200356static u32 bbt_get_ver_offs(struct mtd_info *mtd, struct nand_bbt_descr *td)
357{
358 u32 ver_offs = td->veroffs;
359
360 if (!(td->options & NAND_BBT_NO_OOB))
361 ver_offs += mtd->writesize;
362 return ver_offs;
363}
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365/**
366 * 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 -0700367 * @mtd: MTD device structure
368 * @buf: temporary buffer
369 * @td: descriptor for the bad block table
370 * @md: descriptor for the bad block table mirror
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700372 * Read the bad block table(s) for all chips starting at a given page. We
373 * assume that the bbt bits are in consecutive order.
374 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200375static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
376 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 struct nand_chip *this = mtd->priv;
379
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000380 /* Read the primary version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (td->options & NAND_BBT_VERSION) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000382 scan_read_raw(mtd, buf, (loff_t)td->pages[0] << this->page_shift,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200383 mtd->writesize, td);
384 td->version[0] = buf[bbt_get_ver_offs(mtd, td)];
Brian Norris9a4d4d62011-07-19 10:06:07 -0700385 pr_info("Bad block table at page %d, version 0x%02X\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700386 td->pages[0], td->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000389 /* Read the mirror version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (md && (md->options & NAND_BBT_VERSION)) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000391 scan_read_raw(mtd, buf, (loff_t)md->pages[0] << this->page_shift,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200392 mtd->writesize, td);
393 md->version[0] = buf[bbt_get_ver_offs(mtd, md)];
Brian Norris9a4d4d62011-07-19 10:06:07 -0700394 pr_info("Bad block table at page %d, version 0x%02X\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700395 md->pages[0], md->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return 1;
398}
399
Brian Norris8b6e50c2011-05-25 14:59:01 -0700400/* Scan a given block full */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200401static int scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
402 loff_t offs, uint8_t *buf, size_t readlen,
403 int scanlen, int len)
404{
405 int ret, j;
406
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200407 ret = scan_read_raw_oob(mtd, buf, offs, readlen);
Brian Norrisafa17de2011-09-07 13:13:29 -0700408 /* Ignore ECC errors when checking for BBM */
Brian Norrisd57f40542011-09-20 18:34:25 -0700409 if (ret && !mtd_is_bitflip_or_eccerr(ret))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200410 return ret;
411
412 for (j = 0; j < len; j++, buf += scanlen) {
413 if (check_pattern(buf, scanlen, mtd->writesize, bd))
414 return 1;
415 }
416 return 0;
417}
418
Brian Norris8b6e50c2011-05-25 14:59:01 -0700419/* Scan a given block partially */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200420static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
421 loff_t offs, uint8_t *buf, int len)
422{
423 struct mtd_oob_ops ops;
424 int j, ret;
425
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200426 ops.ooblen = mtd->oobsize;
427 ops.oobbuf = buf;
428 ops.ooboffs = 0;
429 ops.datbuf = NULL;
Brian Norris0612b9d2011-08-30 18:45:40 -0700430 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200431
432 for (j = 0; j < len; j++) {
433 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700434 * Read the full oob until read_oob is fixed to handle single
435 * byte reads for 16 bit buswidth.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200436 */
437 ret = mtd->read_oob(mtd, offs, &ops);
Brian Norris903cd062011-06-28 16:28:58 -0700438 /* Ignore ECC errors when checking for BBM */
Brian Norrisd57f40542011-09-20 18:34:25 -0700439 if (ret && !mtd_is_bitflip_or_eccerr(ret))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200440 return ret;
441
442 if (check_short_pattern(buf, bd))
443 return 1;
444
445 offs += mtd->writesize;
446 }
447 return 0;
448}
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450/**
451 * create_bbt - [GENERIC] Create a bad block table by scanning the device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700452 * @mtd: MTD device structure
453 * @buf: temporary buffer
454 * @bd: descriptor for the good/bad block search pattern
455 * @chip: create the table for a specific chip, -1 read all chips; applies only
456 * if NAND_BBT_PERCHIP option is set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700458 * Create a bad block table by scanning the device for the given good/bad block
459 * identify pattern.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200461static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
462 struct nand_bbt_descr *bd, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 struct nand_chip *this = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200465 int i, numblocks, len, scanlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 int startblock;
467 loff_t from;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200468 size_t readlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Brian Norris9a4d4d62011-07-19 10:06:07 -0700470 pr_info("Scanning device for bad blocks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 if (bd->options & NAND_BBT_SCANALLPAGES)
473 len = 1 << (this->bbt_erase_shift - this->page_shift);
Brian Norris58373ff2010-07-15 12:15:44 -0700474 else if (bd->options & NAND_BBT_SCAN2NDPAGE)
475 len = 2;
476 else
477 len = 1;
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000478
479 if (!(bd->options & NAND_BBT_SCANEMPTY)) {
480 /* We need only read few bytes from the OOB area */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200481 scanlen = 0;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000482 readlen = bd->len;
483 } else {
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000484 /* Full page content should be read */
Joern Engel28318772006-05-22 23:18:05 +0200485 scanlen = mtd->writesize + mtd->oobsize;
486 readlen = len * mtd->writesize;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 if (chip == -1) {
Brian Norris8b6e50c2011-05-25 14:59:01 -0700490 /*
491 * Note that numblocks is 2 * (real numblocks) here, see i+=2
492 * below as it makes shifting and masking less painful
493 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 numblocks = mtd->size >> (this->bbt_erase_shift - 1);
495 startblock = 0;
496 from = 0;
497 } else {
498 if (chip >= this->numchips) {
Brian Norris9a4d4d62011-07-19 10:06:07 -0700499 pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
David Woodhousee0c7d762006-05-13 18:07:53 +0100500 chip + 1, this->numchips);
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000501 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503 numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
504 startblock = chip * numblocks;
505 numblocks += startblock;
Adrian Hunter69423d92008-12-10 13:37:21 +0000506 from = (loff_t)startblock << (this->bbt_erase_shift - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000508
Brian Norris5fb15492011-05-31 16:31:21 -0700509 if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700510 from += mtd->erasesize - (mtd->writesize * len);
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 for (i = startblock; i < numblocks;) {
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000513 int ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000514
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200515 BUG_ON(bd->options & NAND_BBT_NO_OOB);
516
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200517 if (bd->options & NAND_BBT_SCANALLPAGES)
518 ret = scan_block_full(mtd, bd, from, buf, readlen,
519 scanlen, len);
520 else
521 ret = scan_block_fast(mtd, bd, from, buf, len);
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000522
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200523 if (ret < 0)
524 return ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000525
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200526 if (ret) {
527 this->bbt[i >> 3] |= 0x03 << (i & 0x6);
Brian Norris9a4d4d62011-07-19 10:06:07 -0700528 pr_warn("Bad eraseblock %d at 0x%012llx\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700529 i >> 1, (unsigned long long)from);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200530 mtd->ecc_stats.badblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 i += 2;
534 from += (1 << this->bbt_erase_shift);
535 }
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000536 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539/**
540 * search_bbt - [GENERIC] scan the device for a specific bad block table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700541 * @mtd: MTD device structure
542 * @buf: temporary buffer
543 * @td: descriptor for the bad block table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700545 * Read the bad block table by searching for a given ident pattern. Search is
546 * preformed either from the beginning up or from the end of the device
547 * downwards. The search starts always at the start of a block. If the option
548 * NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains
549 * the bad block information of this chip. This is necessary to provide support
550 * for certain DOC devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700552 * The bbt ident pattern resides in the oob area of the first page in a block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100554static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 struct nand_chip *this = mtd->priv;
557 int i, chips;
558 int bits, startblock, block, dir;
Joern Engel28318772006-05-22 23:18:05 +0200559 int scanlen = mtd->writesize + mtd->oobsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 int bbtblocks;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200561 int blocktopage = this->bbt_erase_shift - this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Brian Norris8b6e50c2011-05-25 14:59:01 -0700563 /* Search direction top -> down? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (td->options & NAND_BBT_LASTBLOCK) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100565 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 dir = -1;
567 } else {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000568 startblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000570 }
571
Brian Norris8b6e50c2011-05-25 14:59:01 -0700572 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (td->options & NAND_BBT_PERCHIP) {
574 chips = this->numchips;
575 bbtblocks = this->chipsize >> this->bbt_erase_shift;
576 startblock &= bbtblocks - 1;
577 } else {
578 chips = 1;
579 bbtblocks = mtd->size >> this->bbt_erase_shift;
580 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* Number of bits for each erase block in the bbt */
583 bits = td->options & NAND_BBT_NRBITS_MSK;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 for (i = 0; i < chips; i++) {
586 /* Reset version information */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000587 td->version[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 td->pages[i] = -1;
589 /* Scan the maximum number of blocks */
590 for (block = 0; block < td->maxblocks; block++) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 int actblock = startblock + dir * block;
Adrian Hunter69423d92008-12-10 13:37:21 +0000593 loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 /* Read first page */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200596 scan_read_raw(mtd, buf, offs, mtd->writesize, td);
Joern Engel28318772006-05-22 23:18:05 +0200597 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200598 td->pages[i] = actblock << blocktopage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (td->options & NAND_BBT_VERSION) {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200600 offs = bbt_get_ver_offs(mtd, td);
601 td->version[i] = buf[offs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603 break;
604 }
605 }
606 startblock += this->chipsize >> this->bbt_erase_shift;
607 }
608 /* Check, if we found a bbt for each requested chip */
609 for (i = 0; i < chips; i++) {
610 if (td->pages[i] == -1)
Brian Norris9a4d4d62011-07-19 10:06:07 -0700611 pr_warn("Bad block table not found for chip %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 else
Brian Norrisd0370212011-07-19 10:06:08 -0700613 pr_info("Bad block table found at page %d, version "
614 "0x%02X\n", td->pages[i], td->version[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000616 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
619/**
620 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -0700621 * @mtd: MTD device structure
622 * @buf: temporary buffer
623 * @td: descriptor for the bad block table
624 * @md: descriptor for the bad block table mirror
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700626 * Search and read the bad block table(s).
627 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100628static 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 -0700629{
630 /* Search the primary table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100631 search_bbt(mtd, buf, td);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /* Search the mirror table */
634 if (md)
David Woodhousee0c7d762006-05-13 18:07:53 +0100635 search_bbt(mtd, buf, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000637 /* Force result check */
638 return 1;
639}
640
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000641/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 * write_bbt - [GENERIC] (Re)write the bad block table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700643 * @mtd: MTD device structure
644 * @buf: temporary buffer
645 * @td: descriptor for the bad block table
646 * @md: descriptor for the bad block table mirror
647 * @chipsel: selector for a specific chip, -1 for all
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700649 * (Re)write the bad block table.
650 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100651static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
Thomas Gleixner9223a452006-05-23 17:21:03 +0200652 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
653 int chipsel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 struct nand_chip *this = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 struct erase_info einfo;
657 int i, j, res, chip = 0;
658 int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200659 int nrchips, bbtoffs, pageoffs, ooboffs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 uint8_t msk[4];
661 uint8_t rcode = td->reserved_block_code;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200662 size_t retlen, len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 loff_t to;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200664 struct mtd_oob_ops ops;
665
666 ops.ooblen = mtd->oobsize;
667 ops.ooboffs = 0;
668 ops.datbuf = NULL;
Brian Norris0612b9d2011-08-30 18:45:40 -0700669 ops.mode = MTD_OPS_PLACE_OOB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 if (!rcode)
672 rcode = 0xff;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700673 /* Write bad block table per chip rather than per device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (td->options & NAND_BBT_PERCHIP) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100675 numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700676 /* Full device write or specific chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (chipsel == -1) {
678 nrchips = this->numchips;
679 } else {
680 nrchips = chipsel + 1;
681 chip = chipsel;
682 }
683 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +0100684 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 nrchips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000686 }
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /* Loop through the chips */
689 for (; chip < nrchips; chip++) {
Brian Norris8b6e50c2011-05-25 14:59:01 -0700690 /*
691 * There was already a version of the table, reuse the page
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000692 * This applies for absolute placement too, as we have the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 * page nr. in td->pages.
694 */
695 if (td->pages[chip] != -1) {
696 page = td->pages[chip];
697 goto write;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Brian Norris8b6e50c2011-05-25 14:59:01 -0700700 /*
701 * Automatic placement of the bad block table. Search direction
702 * top -> down?
703 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (td->options & NAND_BBT_LASTBLOCK) {
705 startblock = numblocks * (chip + 1) - 1;
706 dir = -1;
707 } else {
708 startblock = chip * numblocks;
709 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 for (i = 0; i < td->maxblocks; i++) {
713 int block = startblock + dir * i;
714 /* Check, if the block is bad */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200715 switch ((this->bbt[block >> 2] >>
716 (2 * (block & 0x03))) & 0x03) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 case 0x01:
718 case 0x03:
719 continue;
720 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200721 page = block <<
722 (this->bbt_erase_shift - this->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 /* Check, if the block is used by the mirror table */
724 if (!md || md->pages[chip] != page)
725 goto write;
726 }
Brian Norris9a4d4d62011-07-19 10:06:07 -0700727 pr_err("No space left to write bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return -ENOSPC;
David Woodhousee0c7d762006-05-13 18:07:53 +0100729 write:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 /* Set up shift count and masks for the flash table */
732 bits = td->options & NAND_BBT_NRBITS_MSK;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200733 msk[2] = ~rcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 switch (bits) {
Thomas Gleixner9223a452006-05-23 17:21:03 +0200735 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
736 msk[3] = 0x01;
737 break;
738 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
739 msk[3] = 0x03;
740 break;
741 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
742 msk[3] = 0x0f;
743 break;
744 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
745 msk[3] = 0xff;
746 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 default: return -EINVAL;
748 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 bbtoffs = chip * (numblocks >> 2);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000751
Brian Norris596d7452011-09-07 13:13:33 -0700752 to = ((loff_t)page) << this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Brian Norris8b6e50c2011-05-25 14:59:01 -0700754 /* Must we save the block contents? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (td->options & NAND_BBT_SAVECONTENT) {
756 /* Make it block aligned */
Brian Norris596d7452011-09-07 13:13:33 -0700757 to &= ~((loff_t)((1 << this->bbt_erase_shift) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 len = 1 << this->bbt_erase_shift;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200759 res = mtd->read(mtd, to, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (res < 0) {
761 if (retlen != len) {
Brian Norrisd0370212011-07-19 10:06:08 -0700762 pr_info("nand_bbt: error reading block "
763 "for writing the bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return res;
765 }
Brian Norrisd0370212011-07-19 10:06:08 -0700766 pr_warn("nand_bbt: ECC error while reading "
767 "block for writing bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200769 /* Read oob data */
Vitaly Wool70145682006-11-03 18:20:38 +0300770 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200771 ops.oobbuf = &buf[len];
772 res = mtd->read_oob(mtd, to + mtd->writesize, &ops);
Vitaly Wool70145682006-11-03 18:20:38 +0300773 if (res < 0 || ops.oobretlen != ops.ooblen)
Thomas Gleixner9223a452006-05-23 17:21:03 +0200774 goto outerr;
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /* Calc the byte offset in the buffer */
777 pageoffs = page - (int)(to >> this->page_shift);
778 offs = pageoffs << this->page_shift;
779 /* Preset the bbt area with 0xff */
Brian Norris596d7452011-09-07 13:13:33 -0700780 memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
Thomas Gleixner9223a452006-05-23 17:21:03 +0200781 ooboffs = len + (pageoffs * mtd->oobsize);
782
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200783 } else if (td->options & NAND_BBT_NO_OOB) {
784 ooboffs = 0;
785 offs = td->len;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700786 /* The version byte */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200787 if (td->options & NAND_BBT_VERSION)
788 offs++;
789 /* Calc length */
Brian Norris596d7452011-09-07 13:13:33 -0700790 len = (size_t)(numblocks >> sft);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200791 len += offs;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700792 /* Make it page aligned! */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200793 len = ALIGN(len, mtd->writesize);
794 /* Preset the buffer with 0xff */
795 memset(buf, 0xff, len);
796 /* Pattern is located at the begin of first page */
797 memcpy(buf, td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 } else {
799 /* Calc length */
Brian Norris596d7452011-09-07 13:13:33 -0700800 len = (size_t)(numblocks >> sft);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700801 /* Make it page aligned! */
Sebastian Andrzej Siewiorcda32092010-09-29 19:43:50 +0200802 len = ALIGN(len, mtd->writesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 /* Preset the buffer with 0xff */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200804 memset(buf, 0xff, len +
805 (len >> this->page_shift)* mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 offs = 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200807 ooboffs = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 /* Pattern is located in oob area of first page */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200809 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000811
Thomas Gleixner9223a452006-05-23 17:21:03 +0200812 if (td->options & NAND_BBT_VERSION)
813 buf[ooboffs + td->veroffs] = td->version[chip];
814
Brian Norris8b6e50c2011-05-25 14:59:01 -0700815 /* Walk through the memory table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100816 for (i = 0; i < numblocks;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 uint8_t dat;
818 dat = this->bbt[bbtoffs + (i >> 2)];
David Woodhousee0c7d762006-05-13 18:07:53 +0100819 for (j = 0; j < 4; j++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 int sftcnt = (i << (3 - sft)) & sftmsk;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700821 /* Do not store the reserved bbt blocks! */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200822 buf[offs + (i >> sft)] &=
823 ~(msk[dat & 0x03] << sftcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 dat >>= 2;
825 }
826 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000827
David Woodhousee0c7d762006-05-13 18:07:53 +0100828 memset(&einfo, 0, sizeof(einfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 einfo.mtd = mtd;
Adrian Hunter69423d92008-12-10 13:37:21 +0000830 einfo.addr = to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 einfo.len = 1 << this->bbt_erase_shift;
David Woodhousee0c7d762006-05-13 18:07:53 +0100832 res = nand_erase_nand(mtd, &einfo, 1);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200833 if (res < 0)
834 goto outerr;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000835
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200836 res = scan_write_bbt(mtd, to, len, buf,
837 td->options & NAND_BBT_NO_OOB ? NULL :
838 &buf[len]);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200839 if (res < 0)
840 goto outerr;
841
Brian Norrisd0370212011-07-19 10:06:08 -0700842 pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
843 (unsigned long long)to, td->version[chip]);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 /* Mark it as used */
846 td->pages[chip] = page;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200849
850 outerr:
Brian Norrisd0370212011-07-19 10:06:08 -0700851 pr_warn("nand_bbt: error while writing bad block table %d\n", res);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200852 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
855/**
856 * nand_memory_bbt - [GENERIC] create a memory based bad block table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700857 * @mtd: MTD device structure
858 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700860 * The function creates a memory based bbt by scanning the device for
861 * manufacturer / software marked good / bad blocks.
862 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100863static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 struct nand_chip *this = mtd->priv;
866
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000867 bd->options &= ~NAND_BBT_SCANEMPTY;
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100868 return create_bbt(mtd, this->buffers->databuf, bd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
871/**
David Woodhousee0c7d762006-05-13 18:07:53 +0100872 * check_create - [GENERIC] create and write bbt(s) if necessary
Brian Norris8b6e50c2011-05-25 14:59:01 -0700873 * @mtd: MTD device structure
874 * @buf: temporary buffer
875 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700877 * The function checks the results of the previous call to read_bbt and creates
878 * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found
879 * for the chip/device. Update is necessary if one of the tables is missing or
880 * the version nr. of one table is less than the other.
881 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100882static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Brian Norris623978d2011-09-20 18:35:34 -0700884 int i, chips, writeops, create, chipsel, res, res2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 struct nand_chip *this = mtd->priv;
886 struct nand_bbt_descr *td = this->bbt_td;
887 struct nand_bbt_descr *md = this->bbt_md;
888 struct nand_bbt_descr *rd, *rd2;
889
Brian Norris8b6e50c2011-05-25 14:59:01 -0700890 /* Do we have a bbt per chip? */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000891 if (td->options & NAND_BBT_PERCHIP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 chips = this->numchips;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000893 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 chips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 for (i = 0; i < chips; i++) {
897 writeops = 0;
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700898 create = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 rd = NULL;
900 rd2 = NULL;
Brian Norris623978d2011-09-20 18:35:34 -0700901 res = res2 = 0;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700902 /* Per chip or per device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700904 /* Mirrored table available? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (md) {
906 if (td->pages[i] == -1 && md->pages[i] == -1) {
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700907 create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 writeops = 0x03;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700909 } else if (td->pages[i] == -1) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000910 rd = md;
Brian Norris596d7452011-09-07 13:13:33 -0700911 writeops = 0x01;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700912 } else if (md->pages[i] == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 rd = td;
Brian Norris596d7452011-09-07 13:13:33 -0700914 writeops = 0x02;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700915 } else if (td->version[i] == md->version[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 rd = td;
917 if (!(td->options & NAND_BBT_VERSION))
918 rd2 = md;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700919 } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 rd = td;
Brian Norris596d7452011-09-07 13:13:33 -0700921 writeops = 0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 } else {
923 rd = md;
Brian Norris596d7452011-09-07 13:13:33 -0700924 writeops = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 } else {
927 if (td->pages[i] == -1) {
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700928 create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 writeops = 0x01;
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700930 } else {
931 rd = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000934
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700935 if (create) {
936 /* Create the bad block table by scanning the device? */
937 if (!(td->options & NAND_BBT_CREATE))
938 continue;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000939
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700940 /* Create the table in memory by scanning the chip(s) */
941 if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
942 create_bbt(mtd, buf, bd, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700944 td->version[i] = 1;
945 if (md)
946 md->version[i] = 1;
947 }
948
Brian Norris8b6e50c2011-05-25 14:59:01 -0700949 /* Read back first? */
Brian Norris623978d2011-09-20 18:35:34 -0700950 if (rd) {
951 res = read_abs_bbt(mtd, buf, rd, chipsel);
952 if (mtd_is_eccerr(res)) {
953 /* Mark table as invalid */
954 rd->pages[i] = -1;
Brian Norrisdadc17a2011-09-20 18:35:57 -0700955 rd->version[i] = 0;
Brian Norris623978d2011-09-20 18:35:34 -0700956 i--;
957 continue;
958 }
959 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700960 /* If they weren't versioned, read both */
Brian Norris623978d2011-09-20 18:35:34 -0700961 if (rd2) {
962 res2 = read_abs_bbt(mtd, buf, rd2, chipsel);
963 if (mtd_is_eccerr(res2)) {
964 /* Mark table as invalid */
965 rd2->pages[i] = -1;
Brian Norrisdadc17a2011-09-20 18:35:57 -0700966 rd2->version[i] = 0;
Brian Norris623978d2011-09-20 18:35:34 -0700967 i--;
968 continue;
969 }
970 }
971
972 /* Scrub the flash table(s)? */
973 if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
974 writeops = 0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Brian Norrisdadc17a2011-09-20 18:35:57 -0700976 /* Update version numbers before writing */
977 if (md) {
978 td->version[i] = max(td->version[i], md->version[i]);
979 md->version[i] = td->version[i];
980 }
981
Brian Norris8b6e50c2011-05-25 14:59:01 -0700982 /* Write the bad block table to the device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100984 res = write_bbt(mtd, buf, td, md, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (res < 0)
986 return res;
987 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000988
Brian Norris8b6e50c2011-05-25 14:59:01 -0700989 /* Write the mirror bad block table to the device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100991 res = write_bbt(mtd, buf, md, td, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (res < 0)
993 return res;
994 }
995 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000996 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
999/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001000 * mark_bbt_regions - [GENERIC] mark the bad block table regions
Brian Norris8b6e50c2011-05-25 14:59:01 -07001001 * @mtd: MTD device structure
1002 * @td: bad block table descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001004 * The bad block table regions are marked as "bad" to prevent accidental
1005 * erasures / writes. The regions are identified by the mark 0x02.
1006 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001007static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
1009 struct nand_chip *this = mtd->priv;
1010 int i, j, chips, block, nrblocks, update;
1011 uint8_t oldval, newval;
1012
Brian Norris8b6e50c2011-05-25 14:59:01 -07001013 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (td->options & NAND_BBT_PERCHIP) {
1015 chips = this->numchips;
1016 nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
1017 } else {
1018 chips = 1;
1019 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001020 }
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 for (i = 0; i < chips; i++) {
1023 if ((td->options & NAND_BBT_ABSPAGE) ||
1024 !(td->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001025 if (td->pages[i] == -1)
1026 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001028 block <<= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 oldval = this->bbt[(block >> 3)];
1030 newval = oldval | (0x2 << (block & 0x06));
1031 this->bbt[(block >> 3)] = newval;
1032 if ((oldval != newval) && td->reserved_block_code)
Adrian Hunter69423d92008-12-10 13:37:21 +00001033 nand_update_bbt(mtd, (loff_t)block << (this->bbt_erase_shift - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 continue;
1035 }
1036 update = 0;
1037 if (td->options & NAND_BBT_LASTBLOCK)
1038 block = ((i + 1) * nrblocks) - td->maxblocks;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001039 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 block = i * nrblocks;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001041 block <<= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 for (j = 0; j < td->maxblocks; j++) {
1043 oldval = this->bbt[(block >> 3)];
1044 newval = oldval | (0x2 << (block & 0x06));
1045 this->bbt[(block >> 3)] = newval;
David Woodhousee0c7d762006-05-13 18:07:53 +01001046 if (oldval != newval)
1047 update = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 block += 2;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001049 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07001050 /*
1051 * If we want reserved blocks to be recorded to flash, and some
1052 * new ones have been marked, then we need to update the stored
1053 * bbts. This should only happen once.
1054 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (update && td->reserved_block_code)
Adrian Hunter69423d92008-12-10 13:37:21 +00001056 nand_update_bbt(mtd, (loff_t)(block - 2) << (this->bbt_erase_shift - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058}
1059
1060/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001061 * verify_bbt_descr - verify the bad block description
Brian Norris8b6e50c2011-05-25 14:59:01 -07001062 * @mtd: MTD device structure
1063 * @bd: the table to verify
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001064 *
1065 * This functions performs a few sanity checks on the bad block description
1066 * table.
1067 */
1068static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
1069{
1070 struct nand_chip *this = mtd->priv;
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001071 u32 pattern_len;
1072 u32 bits;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001073 u32 table_size;
1074
1075 if (!bd)
1076 return;
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001077
1078 pattern_len = bd->len;
1079 bits = bd->options & NAND_BBT_NRBITS_MSK;
1080
Brian Norrisa40f7342011-05-31 16:31:22 -07001081 BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001082 !(this->bbt_options & NAND_BBT_USE_FLASH));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001083 BUG_ON(!bits);
1084
1085 if (bd->options & NAND_BBT_VERSION)
1086 pattern_len++;
1087
1088 if (bd->options & NAND_BBT_NO_OOB) {
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001089 BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
Brian Norrisa40f7342011-05-31 16:31:22 -07001090 BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001091 BUG_ON(bd->offs);
1092 if (bd->options & NAND_BBT_VERSION)
1093 BUG_ON(bd->veroffs != bd->len);
1094 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1095 }
1096
1097 if (bd->options & NAND_BBT_PERCHIP)
1098 table_size = this->chipsize >> this->bbt_erase_shift;
1099 else
1100 table_size = mtd->size >> this->bbt_erase_shift;
1101 table_size >>= 3;
1102 table_size *= bits;
1103 if (bd->options & NAND_BBT_NO_OOB)
1104 table_size += pattern_len;
1105 BUG_ON(table_size > (1 << this->bbt_erase_shift));
1106}
1107
1108/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07001110 * @mtd: MTD device structure
1111 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001113 * The function checks, if a bad block table(s) is/are already available. If
1114 * not it scans the device for manufacturer marked good / bad blocks and writes
1115 * the bad block table(s) to the selected place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001117 * The bad block table memory is allocated here. It must be freed by calling
1118 * the nand_free_bbt function.
1119 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001120int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122 struct nand_chip *this = mtd->priv;
1123 int len, res = 0;
1124 uint8_t *buf;
1125 struct nand_bbt_descr *td = this->bbt_td;
1126 struct nand_bbt_descr *md = this->bbt_md;
1127
1128 len = mtd->size >> (this->bbt_erase_shift + 2);
Brian Norris8b6e50c2011-05-25 14:59:01 -07001129 /*
1130 * Allocate memory (2bit per block) and clear the memory bad block
1131 * table.
1132 */
Burman Yan95b93a02006-11-15 21:10:29 +02001133 this->bbt = kzalloc(len, GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001134 if (!this->bbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Brian Norris8b6e50c2011-05-25 14:59:01 -07001137 /*
1138 * If no primary table decriptor is given, scan the device to build a
1139 * memory based bad block table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 */
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001141 if (!td) {
1142 if ((res = nand_memory_bbt(mtd, bd))) {
Brian Norrisd0370212011-07-19 10:06:08 -07001143 pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
David Woodhousee0c7d762006-05-13 18:07:53 +01001144 kfree(this->bbt);
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001145 this->bbt = NULL;
1146 }
1147 return res;
1148 }
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001149 verify_bbt_descr(mtd, td);
1150 verify_bbt_descr(mtd, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 /* Allocate a temporary buffer for one eraseblock incl. oob */
1153 len = (1 << this->bbt_erase_shift);
1154 len += (len >> this->page_shift) * mtd->oobsize;
David Woodhousec3f8abf2006-05-13 04:03:42 +01001155 buf = vmalloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (!buf) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001157 kfree(this->bbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 this->bbt = NULL;
1159 return -ENOMEM;
1160 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001161
Brian Norris8b6e50c2011-05-25 14:59:01 -07001162 /* Is the bbt at a given page? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (td->options & NAND_BBT_ABSPAGE) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001164 res = read_abs_bbts(mtd, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001165 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 /* Search the bad block table using a pattern in oob */
David Woodhousee0c7d762006-05-13 18:07:53 +01001167 res = search_read_bbts(mtd, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001170 if (res)
David Woodhousee0c7d762006-05-13 18:07:53 +01001171 res = check_create(mtd, buf, bd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 /* Prevent the bbt regions from erasing / writing */
David Woodhousee0c7d762006-05-13 18:07:53 +01001174 mark_bbt_region(mtd, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (md)
David Woodhousee0c7d762006-05-13 18:07:53 +01001176 mark_bbt_region(mtd, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001177
David Woodhousee0c7d762006-05-13 18:07:53 +01001178 vfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return res;
1180}
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001183 * nand_update_bbt - [NAND Interface] update bad block table(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07001184 * @mtd: MTD device structure
1185 * @offs: the offset of the newly marked block
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001187 * The function updates the bad block table(s).
1188 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001189int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 struct nand_chip *this = mtd->priv;
Brian Norris1196ac52011-09-07 13:13:32 -07001192 int len, res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 int chip, chipsel;
1194 uint8_t *buf;
1195 struct nand_bbt_descr *td = this->bbt_td;
1196 struct nand_bbt_descr *md = this->bbt_md;
1197
1198 if (!this->bbt || !td)
1199 return -EINVAL;
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 /* Allocate a temporary buffer for one eraseblock incl. oob */
1202 len = (1 << this->bbt_erase_shift);
1203 len += (len >> this->page_shift) * mtd->oobsize;
David Woodhousee0c7d762006-05-13 18:07:53 +01001204 buf = kmalloc(len, GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001205 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return -ENOMEM;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001207
Brian Norris8b6e50c2011-05-25 14:59:01 -07001208 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (td->options & NAND_BBT_PERCHIP) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001210 chip = (int)(offs >> this->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 chipsel = chip;
1212 } else {
1213 chip = 0;
1214 chipsel = -1;
1215 }
1216
1217 td->version[chip]++;
1218 if (md)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001219 md->version[chip]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Brian Norris8b6e50c2011-05-25 14:59:01 -07001221 /* Write the bad block table to the device? */
Brian Norris1196ac52011-09-07 13:13:32 -07001222 if (td->options & NAND_BBT_WRITE) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001223 res = write_bbt(mtd, buf, td, md, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (res < 0)
1225 goto out;
1226 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07001227 /* Write the mirror bad block table to the device? */
Brian Norris1196ac52011-09-07 13:13:32 -07001228 if (md && (md->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001229 res = write_bbt(mtd, buf, md, td, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231
David Woodhousee0c7d762006-05-13 18:07:53 +01001232 out:
1233 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return res;
1235}
1236
Brian Norris8b6e50c2011-05-25 14:59:01 -07001237/*
1238 * Define some generic bad / good block scan pattern which are used
1239 * while scanning a device for factory marked good / bad blocks.
1240 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
1244
1245static struct nand_bbt_descr agand_flashbased = {
1246 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
1247 .offs = 0x20,
1248 .len = 6,
1249 .pattern = scan_agand_pattern
1250};
1251
Brian Norris7854d3f2011-06-23 14:12:08 -07001252/* Generic flash bbt descriptors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1254static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1255
1256static struct nand_bbt_descr bbt_main_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001257 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1259 .offs = 8,
1260 .len = 4,
1261 .veroffs = 12,
1262 .maxblocks = 4,
1263 .pattern = bbt_pattern
1264};
1265
1266static struct nand_bbt_descr bbt_mirror_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001267 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1269 .offs = 8,
1270 .len = 4,
1271 .veroffs = 12,
1272 .maxblocks = 4,
1273 .pattern = mirror_pattern
1274};
1275
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001276static struct nand_bbt_descr bbt_main_no_bbt_descr = {
1277 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1278 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1279 | NAND_BBT_NO_OOB,
1280 .len = 4,
1281 .veroffs = 4,
1282 .maxblocks = 4,
1283 .pattern = bbt_pattern
1284};
1285
1286static struct nand_bbt_descr bbt_mirror_no_bbt_descr = {
1287 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1288 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1289 | NAND_BBT_NO_OOB,
1290 .len = 4,
1291 .veroffs = 4,
1292 .maxblocks = 4,
1293 .pattern = mirror_pattern
1294};
1295
Brian Norris752ed6c2011-09-20 18:36:42 -07001296#define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
Brian Norris58373ff2010-07-15 12:15:44 -07001297/**
Brian Norris752ed6c2011-09-20 18:36:42 -07001298 * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001299 * @this: NAND chip to create descriptor for
Brian Norris58373ff2010-07-15 12:15:44 -07001300 *
1301 * This function allocates and initializes a nand_bbt_descr for BBM detection
Brian Norris752ed6c2011-09-20 18:36:42 -07001302 * based on the properties of @this. The new descriptor is stored in
Brian Norris58373ff2010-07-15 12:15:44 -07001303 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1304 * passed to this function.
Brian Norris58373ff2010-07-15 12:15:44 -07001305 */
Brian Norris752ed6c2011-09-20 18:36:42 -07001306static int nand_create_badblock_pattern(struct nand_chip *this)
Brian Norris58373ff2010-07-15 12:15:44 -07001307{
1308 struct nand_bbt_descr *bd;
1309 if (this->badblock_pattern) {
Brian Norris752ed6c2011-09-20 18:36:42 -07001310 pr_warn("Bad block pattern already allocated; not replacing\n");
Brian Norris58373ff2010-07-15 12:15:44 -07001311 return -EINVAL;
1312 }
1313 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001314 if (!bd)
Brian Norris58373ff2010-07-15 12:15:44 -07001315 return -ENOMEM;
Brian Norris752ed6c2011-09-20 18:36:42 -07001316 bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
Brian Norris58373ff2010-07-15 12:15:44 -07001317 bd->offs = this->badblockpos;
1318 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1319 bd->pattern = scan_ff_pattern;
1320 bd->options |= NAND_BBT_DYNAMICSTRUCT;
1321 this->badblock_pattern = bd;
1322 return 0;
1323}
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001326 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
Brian Norris8b6e50c2011-05-25 14:59:01 -07001327 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001329 * This function selects the default bad block table support for the device and
1330 * calls the nand_scan_bbt function.
1331 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001332int nand_default_bbt(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 struct nand_chip *this = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001335
Brian Norris8b6e50c2011-05-25 14:59:01 -07001336 /*
1337 * Default for AG-AND. We must use a flash based bad block table as the
1338 * devices have factory marked _good_ blocks. Erasing those blocks
1339 * leads to loss of the good / bad information, so we _must_ store this
1340 * information in a good / bad table during startup.
David Woodhousee0c7d762006-05-13 18:07:53 +01001341 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (this->options & NAND_IS_AND) {
1343 /* Use the default pattern descriptors */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001344 if (!this->bbt_td) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 this->bbt_td = &bbt_main_descr;
1346 this->bbt_md = &bbt_mirror_descr;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001347 }
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001348 this->bbt_options |= NAND_BBT_USE_FLASH;
David Woodhousee0c7d762006-05-13 18:07:53 +01001349 return nand_scan_bbt(mtd, &agand_flashbased);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001351
Brian Norris8b6e50c2011-05-25 14:59:01 -07001352 /* Is a flash based bad block table requested? */
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001353 if (this->bbt_options & NAND_BBT_USE_FLASH) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001354 /* Use the default pattern descriptors */
1355 if (!this->bbt_td) {
Brian Norrisa40f7342011-05-31 16:31:22 -07001356 if (this->bbt_options & NAND_BBT_NO_OOB) {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001357 this->bbt_td = &bbt_main_no_bbt_descr;
1358 this->bbt_md = &bbt_mirror_no_bbt_descr;
1359 } else {
1360 this->bbt_td = &bbt_main_descr;
1361 this->bbt_md = &bbt_mirror_descr;
1362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 } else {
1365 this->bbt_td = NULL;
1366 this->bbt_md = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
Brian Norrisa2f812d2011-03-18 21:53:42 -07001368
1369 if (!this->badblock_pattern)
Brian Norris752ed6c2011-09-20 18:36:42 -07001370 nand_create_badblock_pattern(this);
Brian Norrisa2f812d2011-03-18 21:53:42 -07001371
David Woodhousee0c7d762006-05-13 18:07:53 +01001372 return nand_scan_bbt(mtd, this->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
1375/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001376 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07001377 * @mtd: MTD device structure
1378 * @offs: offset in the device
1379 * @allowbbt: allow access to bad block table region
1380 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001381int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 struct nand_chip *this = mtd->priv;
1384 int block;
David Woodhousee0c7d762006-05-13 18:07:53 +01001385 uint8_t res;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 /* Get block number * 2 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001388 block = (int)(offs >> (this->bbt_erase_shift - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
1390
Brian Norris289c0522011-07-19 10:06:09 -07001391 pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: "
1392 "(block %d) 0x%02x\n",
1393 (unsigned int)offs, block >> 1, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 switch ((int)res) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001396 case 0x00:
1397 return 0;
1398 case 0x01:
1399 return 1;
1400 case 0x02:
1401 return allowbbt ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403 return 1;
1404}
1405
David Woodhousee0c7d762006-05-13 18:07:53 +01001406EXPORT_SYMBOL(nand_scan_bbt);
1407EXPORT_SYMBOL(nand_default_bbt);