blob: 2f744dd907409fdf4d9182e415cc5bd7709052f9 [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 *
Fabio Estevamd159c4e2012-07-28 19:29:25 -03007 * Copyright © 2004 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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
Huang Shijie44ed0ff2012-07-03 16:44:15 +080025 * version number, then 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>
Brian Norris491ed062012-06-22 16:35:44 -070071#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020073static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
74{
Brian Norris718894a2012-06-22 16:35:43 -070075 if (memcmp(buf, td->pattern, td->len))
76 return -1;
77 return 0;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020078}
79
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000080/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * check_pattern - [GENERIC] check if a pattern is in the buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -070082 * @buf: the buffer to search
83 * @len: the length of buffer to search
84 * @paglen: the pagelength
85 * @td: search pattern descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 *
Brian Norris8b6e50c2011-05-25 14:59:01 -070087 * Check for a pattern at the given place. Used to search bad block tables and
88 * good / bad block identifiers. If the SCAN_EMPTY option is set then check, if
89 * all bytes except the pattern area contain 0xff.
90 */
David Woodhousee0c7d762006-05-13 18:07:53 +010091static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Brian Norris491ed062012-06-22 16:35:44 -070093 int end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 uint8_t *p = buf;
95
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020096 if (td->options & NAND_BBT_NO_OOB)
97 return check_pattern_no_oob(buf, td);
98
Thomas Gleixnerc9e05362005-06-14 16:39:57 +010099 end = paglen + td->offs;
Brian Norris491ed062012-06-22 16:35:44 -0700100 if (td->options & NAND_BBT_SCANEMPTY)
101 if (memchr_inv(p, 0xff, end))
102 return -1;
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100103 p += end;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 /* Compare the pattern */
Brian Norris75b66d82011-09-07 13:13:41 -0700106 if (memcmp(p, td->pattern, td->len))
107 return -1;
Brian Norris58373ff2010-07-15 12:15:44 -0700108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (td->options & NAND_BBT_SCANEMPTY) {
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000110 p += td->len;
111 end += td->len;
Brian Norris491ed062012-06-22 16:35:44 -0700112 if (memchr_inv(p, 0xff, len - end))
113 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
115 return 0;
116}
117
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000118/**
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100119 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700120 * @buf: the buffer to search
121 * @td: search pattern descriptor
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100122 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700123 * Check for a pattern at the given place. Used to search bad block tables and
124 * good / bad block identifiers. Same as check_pattern, but no optional empty
125 * check.
126 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100127static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100128{
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100129 /* Compare the pattern */
Brian Norris491ed062012-06-22 16:35:44 -0700130 if (memcmp(buf + td->offs, td->pattern, td->len))
131 return -1;
Thomas Gleixnerc9e05362005-06-14 16:39:57 +0100132 return 0;
133}
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200136 * add_marker_len - compute the length of the marker in data area
Brian Norris8b6e50c2011-05-25 14:59:01 -0700137 * @td: BBT descriptor used for computation
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200138 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700139 * The length will be 0 if the marker is located in OOB area.
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200140 */
141static u32 add_marker_len(struct nand_bbt_descr *td)
142{
143 u32 len;
144
145 if (!(td->options & NAND_BBT_NO_OOB))
146 return 0;
147
148 len = td->len;
149 if (td->options & NAND_BBT_VERSION)
150 len++;
151 return len;
152}
153
154/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 * read_bbt - [GENERIC] Read the bad block table starting from page
Brian Norris8b6e50c2011-05-25 14:59:01 -0700156 * @mtd: MTD device structure
157 * @buf: temporary buffer
158 * @page: the starting page
159 * @num: the number of bbt descriptors to read
Brian Norris7854d3f2011-06-23 14:12:08 -0700160 * @td: the bbt describtion table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700161 * @offs: offset in the memory table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 *
163 * Read the bad block table starting from page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100165static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200166 struct nand_bbt_descr *td, int offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Brian Norris167a8d52011-09-20 18:35:08 -0700168 int res, ret = 0, i, j, act = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 struct nand_chip *this = mtd->priv;
170 size_t retlen, len, totlen;
171 loff_t from;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200172 int bits = td->options & NAND_BBT_NRBITS_MSK;
Brian Norris596d7452011-09-07 13:13:33 -0700173 uint8_t msk = (uint8_t)((1 << bits) - 1);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200174 u32 marker_len;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200175 int reserved_block_code = td->reserved_block_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 totlen = (num * bits) >> 3;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200178 marker_len = add_marker_len(td);
Brian Norris596d7452011-09-07 13:13:33 -0700179 from = ((loff_t)page) << this->page_shift;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 while (totlen) {
Brian Norris596d7452011-09-07 13:13:33 -0700182 len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200183 if (marker_len) {
184 /*
185 * In case the BBT marker is not in the OOB area it
186 * will be just in the first page.
187 */
188 len -= marker_len;
189 from += marker_len;
190 marker_len = 0;
191 }
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200192 res = mtd_read(mtd, from, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (res < 0) {
Brian Norris167a8d52011-09-20 18:35:08 -0700194 if (mtd_is_eccerr(res)) {
195 pr_info("nand_bbt: ECC error in BBT at "
196 "0x%012llx\n", from & ~mtd->writesize);
197 return res;
198 } else if (mtd_is_bitflip(res)) {
199 pr_info("nand_bbt: corrected error in BBT at "
200 "0x%012llx\n", from & ~mtd->writesize);
201 ret = res;
202 } else {
203 pr_info("nand_bbt: error reading BBT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return res;
205 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 /* Analyse data */
209 for (i = 0; i < len; i++) {
210 uint8_t dat = buf[i];
211 for (j = 0; j < 8; j += bits, act += 2) {
212 uint8_t tmp = (dat >> j) & msk;
213 if (tmp == msk)
214 continue;
David Woodhousee0c7d762006-05-13 18:07:53 +0100215 if (reserved_block_code && (tmp == reserved_block_code)) {
Brian Norrisd0370212011-07-19 10:06:08 -0700216 pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
217 (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200219 mtd->ecc_stats.bbtblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 continue;
221 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700222 /*
223 * Leave it for now, if it's matured we can
Brian Norrisa0f50802011-07-19 10:06:06 -0700224 * move this message to pr_debug.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700225 */
Brian Norrisd0370212011-07-19 10:06:08 -0700226 pr_info("nand_read_bbt: bad block at 0x%012llx\n",
227 (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700228 /* Factory marked bad or worn out? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (tmp == 0)
230 this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
231 else
232 this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200233 mtd->ecc_stats.badblocks++;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236 totlen -= len;
237 from += len;
238 }
Brian Norris167a8d52011-09-20 18:35:08 -0700239 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242/**
243 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
Brian Norris8b6e50c2011-05-25 14:59:01 -0700244 * @mtd: MTD device structure
245 * @buf: temporary buffer
246 * @td: descriptor for the bad block table
Brian Norris596d7452011-09-07 13:13:33 -0700247 * @chip: read the table for a specific chip, -1 read all chips; applies only if
Brian Norris8b6e50c2011-05-25 14:59:01 -0700248 * NAND_BBT_PERCHIP option is set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700250 * Read the bad block table for all chips starting at a given page. We assume
251 * that the bbt bits are in consecutive order.
252 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100253static 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 -0700254{
255 struct nand_chip *this = mtd->priv;
256 int res = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (td->options & NAND_BBT_PERCHIP) {
259 int offs = 0;
260 for (i = 0; i < this->numchips; i++) {
261 if (chip == -1 || chip == i)
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200262 res = read_bbt(mtd, buf, td->pages[i],
263 this->chipsize >> this->bbt_erase_shift,
264 td, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (res)
266 return res;
267 offs += this->chipsize >> (this->bbt_erase_shift + 2);
268 }
269 } else {
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200270 res = read_bbt(mtd, buf, td->pages[0],
271 mtd->size >> this->bbt_erase_shift, td, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (res)
273 return res;
274 }
275 return 0;
276}
277
Brian Norris8b6e50c2011-05-25 14:59:01 -0700278/* BBT marker is in the first page, no OOB */
Brian Norrisaf69dcd2012-06-22 16:35:42 -0700279static int scan_read_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200280 struct nand_bbt_descr *td)
281{
282 size_t retlen;
283 size_t len;
284
285 len = td->len;
286 if (td->options & NAND_BBT_VERSION)
287 len++;
288
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200289 return mtd_read(mtd, offs, len, &retlen, buf);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200290}
291
Brian Norrisa7e68832012-06-22 16:35:45 -0700292/**
Brian Norrisaf69dcd2012-06-22 16:35:42 -0700293 * scan_read_oob - [GENERIC] Scan data+OOB region to buffer
Brian Norrisa7e68832012-06-22 16:35:45 -0700294 * @mtd: MTD device structure
295 * @buf: temporary buffer
296 * @offs: offset at which to scan
297 * @len: length of data region to read
298 *
299 * Scan read data from data+OOB. May traverse multiple pages, interleaving
300 * page,OOB,page,OOB,... in buf. Completes transfer and returns the "strongest"
301 * ECC condition (error or bitflip). May quit on the first (non-ECC) error.
302 */
Brian Norrisaf69dcd2012-06-22 16:35:42 -0700303static int scan_read_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200304 size_t len)
305{
306 struct mtd_oob_ops ops;
Brian Norrisa7e68832012-06-22 16:35:45 -0700307 int res, ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200308
Brian Norrisa7e68832012-06-22 16:35:45 -0700309 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200310 ops.ooboffs = 0;
311 ops.ooblen = mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200312
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200313 while (len > 0) {
Brian Norris105513c2011-09-07 13:13:28 -0700314 ops.datbuf = buf;
315 ops.len = min(len, (size_t)mtd->writesize);
316 ops.oobbuf = buf + ops.len;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200317
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200318 res = mtd_read_oob(mtd, offs, &ops);
Brian Norrisa7e68832012-06-22 16:35:45 -0700319 if (res) {
320 if (!mtd_is_bitflip_or_eccerr(res))
321 return res;
322 else if (mtd_is_eccerr(res) || !ret)
323 ret = res;
324 }
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200325
326 buf += mtd->oobsize + mtd->writesize;
327 len -= mtd->writesize;
Dmitry Maluka34a57042012-05-11 20:51:51 +0300328 offs += mtd->writesize;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200329 }
Brian Norrisa7e68832012-06-22 16:35:45 -0700330 return ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200331}
332
Brian Norrisaf69dcd2012-06-22 16:35:42 -0700333static int scan_read(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200334 size_t len, struct nand_bbt_descr *td)
335{
336 if (td->options & NAND_BBT_NO_OOB)
Brian Norrisaf69dcd2012-06-22 16:35:42 -0700337 return scan_read_data(mtd, buf, offs, td);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200338 else
Brian Norrisaf69dcd2012-06-22 16:35:42 -0700339 return scan_read_oob(mtd, buf, offs, len);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200340}
341
Brian Norris8b6e50c2011-05-25 14:59:01 -0700342/* Scan write data with oob to flash */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200343static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
344 uint8_t *buf, uint8_t *oob)
345{
346 struct mtd_oob_ops ops;
347
Brian Norris0612b9d2011-08-30 18:45:40 -0700348 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200349 ops.ooboffs = 0;
350 ops.ooblen = mtd->oobsize;
351 ops.datbuf = buf;
352 ops.oobbuf = oob;
353 ops.len = len;
354
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200355 return mtd_write_oob(mtd, offs, &ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200356}
357
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200358static u32 bbt_get_ver_offs(struct mtd_info *mtd, struct nand_bbt_descr *td)
359{
360 u32 ver_offs = td->veroffs;
361
362 if (!(td->options & NAND_BBT_NO_OOB))
363 ver_offs += mtd->writesize;
364 return ver_offs;
365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367/**
368 * 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 -0700369 * @mtd: MTD device structure
370 * @buf: temporary buffer
371 * @td: descriptor for the bad block table
372 * @md: descriptor for the bad block table mirror
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700374 * Read the bad block table(s) for all chips starting at a given page. We
375 * assume that the bbt bits are in consecutive order.
376 */
Brian Norris7b5a2d42012-06-22 16:35:41 -0700377static void read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
378 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 struct nand_chip *this = mtd->priv;
381
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000382 /* Read the primary version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (td->options & NAND_BBT_VERSION) {
Brian Norrisaf69dcd2012-06-22 16:35:42 -0700384 scan_read(mtd, buf, (loff_t)td->pages[0] << this->page_shift,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200385 mtd->writesize, td);
386 td->version[0] = buf[bbt_get_ver_offs(mtd, td)];
Brian Norris9a4d4d62011-07-19 10:06:07 -0700387 pr_info("Bad block table at page %d, version 0x%02X\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700388 td->pages[0], td->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000391 /* Read the mirror version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (md && (md->options & NAND_BBT_VERSION)) {
Brian Norrisaf69dcd2012-06-22 16:35:42 -0700393 scan_read(mtd, buf, (loff_t)md->pages[0] << this->page_shift,
Shmulik Ladkani7bb9c752012-06-10 13:58:12 +0300394 mtd->writesize, md);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200395 md->version[0] = buf[bbt_get_ver_offs(mtd, md)];
Brian Norris9a4d4d62011-07-19 10:06:07 -0700396 pr_info("Bad block table at page %d, version 0x%02X\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700397 md->pages[0], md->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Brian Norris8b6e50c2011-05-25 14:59:01 -0700401/* Scan a given block full */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200402static int scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
403 loff_t offs, uint8_t *buf, size_t readlen,
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300404 int scanlen, int numpages)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200405{
406 int ret, j;
407
Brian Norrisaf69dcd2012-06-22 16:35:42 -0700408 ret = scan_read_oob(mtd, buf, offs, readlen);
Brian Norrisafa17de2011-09-07 13:13:29 -0700409 /* Ignore ECC errors when checking for BBM */
Brian Norrisd57f40542011-09-20 18:34:25 -0700410 if (ret && !mtd_is_bitflip_or_eccerr(ret))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200411 return ret;
412
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300413 for (j = 0; j < numpages; j++, buf += scanlen) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200414 if (check_pattern(buf, scanlen, mtd->writesize, bd))
415 return 1;
416 }
417 return 0;
418}
419
Brian Norris8b6e50c2011-05-25 14:59:01 -0700420/* Scan a given block partially */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200421static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300422 loff_t offs, uint8_t *buf, int numpages)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200423{
424 struct mtd_oob_ops ops;
425 int j, ret;
426
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200427 ops.ooblen = mtd->oobsize;
428 ops.oobbuf = buf;
429 ops.ooboffs = 0;
430 ops.datbuf = NULL;
Brian Norris0612b9d2011-08-30 18:45:40 -0700431 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200432
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300433 for (j = 0; j < numpages; j++) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200434 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700435 * Read the full oob until read_oob is fixed to handle single
436 * byte reads for 16 bit buswidth.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200437 */
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200438 ret = mtd_read_oob(mtd, offs, &ops);
Brian Norris903cd062011-06-28 16:28:58 -0700439 /* Ignore ECC errors when checking for BBM */
Brian Norrisd57f40542011-09-20 18:34:25 -0700440 if (ret && !mtd_is_bitflip_or_eccerr(ret))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200441 return ret;
442
443 if (check_short_pattern(buf, bd))
444 return 1;
445
446 offs += mtd->writesize;
447 }
448 return 0;
449}
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/**
452 * create_bbt - [GENERIC] Create a bad block table by scanning the device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700453 * @mtd: MTD device structure
454 * @buf: temporary buffer
455 * @bd: descriptor for the good/bad block search pattern
456 * @chip: create the table for a specific chip, -1 read all chips; applies only
457 * if NAND_BBT_PERCHIP option is set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700459 * Create a bad block table by scanning the device for the given good/bad block
460 * identify pattern.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200462static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
463 struct nand_bbt_descr *bd, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 struct nand_chip *this = mtd->priv;
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300466 int i, numblocks, numpages, scanlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 int startblock;
468 loff_t from;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200469 size_t readlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Brian Norris9a4d4d62011-07-19 10:06:07 -0700471 pr_info("Scanning device for bad blocks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 if (bd->options & NAND_BBT_SCANALLPAGES)
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300474 numpages = 1 << (this->bbt_erase_shift - this->page_shift);
Brian Norris58373ff2010-07-15 12:15:44 -0700475 else if (bd->options & NAND_BBT_SCAN2NDPAGE)
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300476 numpages = 2;
Brian Norris58373ff2010-07-15 12:15:44 -0700477 else
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300478 numpages = 1;
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000479
480 if (!(bd->options & NAND_BBT_SCANEMPTY)) {
481 /* We need only read few bytes from the OOB area */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200482 scanlen = 0;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000483 readlen = bd->len;
484 } else {
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000485 /* Full page content should be read */
Joern Engel28318772006-05-22 23:18:05 +0200486 scanlen = mtd->writesize + mtd->oobsize;
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300487 readlen = numpages * mtd->writesize;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 if (chip == -1) {
Brian Norris8b6e50c2011-05-25 14:59:01 -0700491 /*
492 * Note that numblocks is 2 * (real numblocks) here, see i+=2
493 * below as it makes shifting and masking less painful
494 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 numblocks = mtd->size >> (this->bbt_erase_shift - 1);
496 startblock = 0;
497 from = 0;
498 } else {
499 if (chip >= this->numchips) {
Brian Norris9a4d4d62011-07-19 10:06:07 -0700500 pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
David Woodhousee0c7d762006-05-13 18:07:53 +0100501 chip + 1, this->numchips);
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000502 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504 numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
505 startblock = chip * numblocks;
506 numblocks += startblock;
Adrian Hunter69423d92008-12-10 13:37:21 +0000507 from = (loff_t)startblock << (this->bbt_erase_shift - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000509
Brian Norris5fb15492011-05-31 16:31:21 -0700510 if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300511 from += mtd->erasesize - (mtd->writesize * numpages);
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 for (i = startblock; i < numblocks;) {
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000514 int ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000515
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200516 BUG_ON(bd->options & NAND_BBT_NO_OOB);
517
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200518 if (bd->options & NAND_BBT_SCANALLPAGES)
519 ret = scan_block_full(mtd, bd, from, buf, readlen,
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300520 scanlen, numpages);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200521 else
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300522 ret = scan_block_fast(mtd, bd, from, buf, numpages);
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000523
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200524 if (ret < 0)
525 return ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000526
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200527 if (ret) {
528 this->bbt[i >> 3] |= 0x03 << (i & 0x6);
Brian Norris9a4d4d62011-07-19 10:06:07 -0700529 pr_warn("Bad eraseblock %d at 0x%012llx\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700530 i >> 1, (unsigned long long)from);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200531 mtd->ecc_stats.badblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 i += 2;
535 from += (1 << this->bbt_erase_shift);
536 }
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000537 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
540/**
541 * search_bbt - [GENERIC] scan the device for a specific bad block table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700542 * @mtd: MTD device structure
543 * @buf: temporary buffer
544 * @td: descriptor for the bad block table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700546 * Read the bad block table by searching for a given ident pattern. Search is
547 * preformed either from the beginning up or from the end of the device
548 * downwards. The search starts always at the start of a block. If the option
549 * NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains
550 * the bad block information of this chip. This is necessary to provide support
551 * for certain DOC devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700553 * The bbt ident pattern resides in the oob area of the first page in a block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100555static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 struct nand_chip *this = mtd->priv;
558 int i, chips;
559 int bits, startblock, block, dir;
Joern Engel28318772006-05-22 23:18:05 +0200560 int scanlen = mtd->writesize + mtd->oobsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 int bbtblocks;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200562 int blocktopage = this->bbt_erase_shift - this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Brian Norris8b6e50c2011-05-25 14:59:01 -0700564 /* Search direction top -> down? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (td->options & NAND_BBT_LASTBLOCK) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100566 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 dir = -1;
568 } else {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000569 startblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000571 }
572
Brian Norris8b6e50c2011-05-25 14:59:01 -0700573 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (td->options & NAND_BBT_PERCHIP) {
575 chips = this->numchips;
576 bbtblocks = this->chipsize >> this->bbt_erase_shift;
577 startblock &= bbtblocks - 1;
578 } else {
579 chips = 1;
580 bbtblocks = mtd->size >> this->bbt_erase_shift;
581 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 /* Number of bits for each erase block in the bbt */
584 bits = td->options & NAND_BBT_NRBITS_MSK;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 for (i = 0; i < chips; i++) {
587 /* Reset version information */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000588 td->version[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 td->pages[i] = -1;
590 /* Scan the maximum number of blocks */
591 for (block = 0; block < td->maxblocks; block++) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 int actblock = startblock + dir * block;
Adrian Hunter69423d92008-12-10 13:37:21 +0000594 loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /* Read first page */
Brian Norrisaf69dcd2012-06-22 16:35:42 -0700597 scan_read(mtd, buf, offs, mtd->writesize, td);
Joern Engel28318772006-05-22 23:18:05 +0200598 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200599 td->pages[i] = actblock << blocktopage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (td->options & NAND_BBT_VERSION) {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200601 offs = bbt_get_ver_offs(mtd, td);
602 td->version[i] = buf[offs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604 break;
605 }
606 }
607 startblock += this->chipsize >> this->bbt_erase_shift;
608 }
609 /* Check, if we found a bbt for each requested chip */
610 for (i = 0; i < chips; i++) {
611 if (td->pages[i] == -1)
Brian Norris9a4d4d62011-07-19 10:06:07 -0700612 pr_warn("Bad block table not found for chip %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 else
Brian Norrisd0370212011-07-19 10:06:08 -0700614 pr_info("Bad block table found at page %d, version "
615 "0x%02X\n", td->pages[i], td->version[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000617 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
620/**
621 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -0700622 * @mtd: MTD device structure
623 * @buf: temporary buffer
624 * @td: descriptor for the bad block table
625 * @md: descriptor for the bad block table mirror
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700627 * Search and read the bad block table(s).
628 */
Brian Norris7b5a2d42012-06-22 16:35:41 -0700629static void search_read_bbts(struct mtd_info *mtd, uint8_t *buf,
630 struct nand_bbt_descr *td,
631 struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 /* Search the primary table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100634 search_bbt(mtd, buf, td);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 /* Search the mirror table */
637 if (md)
David Woodhousee0c7d762006-05-13 18:07:53 +0100638 search_bbt(mtd, buf, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000639}
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;
Artem Bityutskiy329ad392011-12-23 17:30:16 +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];
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200772 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) {
Brian Norris7b5a2d42012-06-22 16:35:41 -07001164 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 */
Brian Norris7b5a2d42012-06-22 16:35:41 -07001167 search_read_bbts(mtd, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Brian Norris7b5a2d42012-06-22 16:35:41 -07001170 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
Brian Norris9fd6b372012-06-22 16:35:40 -07001275static struct nand_bbt_descr bbt_main_no_oob_descr = {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001276 .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
Brian Norris9fd6b372012-06-22 16:35:40 -07001285static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001286 .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) {
Brian Norris9fd6b372012-06-22 16:35:40 -07001356 this->bbt_td = &bbt_main_no_oob_descr;
1357 this->bbt_md = &bbt_mirror_no_oob_descr;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001358 } 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);
Fabio Estevamd159c4e2012-07-28 19:29:25 -03001407EXPORT_SYMBOL_GPL(nand_update_bbt);