blob: f5839f06cece60db9ce08228b37080e7182961b7 [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
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 */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200279static int scan_read_raw_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
280 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 Norris8b6e50c2011-05-25 14:59:01 -0700292/* Scan read raw data from flash */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200293static int scan_read_raw_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200294 size_t len)
295{
296 struct mtd_oob_ops ops;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200297 int res;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200298
Brian Norris0612b9d2011-08-30 18:45:40 -0700299 ops.mode = MTD_OPS_RAW;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200300 ops.ooboffs = 0;
301 ops.ooblen = mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200302
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200303 while (len > 0) {
Brian Norris105513c2011-09-07 13:13:28 -0700304 ops.datbuf = buf;
305 ops.len = min(len, (size_t)mtd->writesize);
306 ops.oobbuf = buf + ops.len;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200307
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200308 res = mtd_read_oob(mtd, offs, &ops);
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200309
Brian Norrisafa17de2011-09-07 13:13:29 -0700310 if (res)
Brian Norris105513c2011-09-07 13:13:28 -0700311 return res;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200312
313 buf += mtd->oobsize + mtd->writesize;
314 len -= mtd->writesize;
Dmitry Maluka34a57042012-05-11 20:51:51 +0300315 offs += mtd->writesize;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200316 }
317 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200318}
319
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200320static int scan_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
321 size_t len, struct nand_bbt_descr *td)
322{
323 if (td->options & NAND_BBT_NO_OOB)
324 return scan_read_raw_data(mtd, buf, offs, td);
325 else
326 return scan_read_raw_oob(mtd, buf, offs, len);
327}
328
Brian Norris8b6e50c2011-05-25 14:59:01 -0700329/* Scan write data with oob to flash */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200330static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
331 uint8_t *buf, uint8_t *oob)
332{
333 struct mtd_oob_ops ops;
334
Brian Norris0612b9d2011-08-30 18:45:40 -0700335 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200336 ops.ooboffs = 0;
337 ops.ooblen = mtd->oobsize;
338 ops.datbuf = buf;
339 ops.oobbuf = oob;
340 ops.len = len;
341
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200342 return mtd_write_oob(mtd, offs, &ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200343}
344
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200345static u32 bbt_get_ver_offs(struct mtd_info *mtd, struct nand_bbt_descr *td)
346{
347 u32 ver_offs = td->veroffs;
348
349 if (!(td->options & NAND_BBT_NO_OOB))
350 ver_offs += mtd->writesize;
351 return ver_offs;
352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354/**
355 * 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 -0700356 * @mtd: MTD device structure
357 * @buf: temporary buffer
358 * @td: descriptor for the bad block table
359 * @md: descriptor for the bad block table mirror
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700361 * Read the bad block table(s) for all chips starting at a given page. We
362 * assume that the bbt bits are in consecutive order.
363 */
Brian Norris7b5a2d42012-06-22 16:35:41 -0700364static void read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
365 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 struct nand_chip *this = mtd->priv;
368
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000369 /* Read the primary version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (td->options & NAND_BBT_VERSION) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000371 scan_read_raw(mtd, buf, (loff_t)td->pages[0] << this->page_shift,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200372 mtd->writesize, td);
373 td->version[0] = buf[bbt_get_ver_offs(mtd, td)];
Brian Norris9a4d4d62011-07-19 10:06:07 -0700374 pr_info("Bad block table at page %d, version 0x%02X\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700375 td->pages[0], td->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000378 /* Read the mirror version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (md && (md->options & NAND_BBT_VERSION)) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000380 scan_read_raw(mtd, buf, (loff_t)md->pages[0] << this->page_shift,
Shmulik Ladkani7bb9c752012-06-10 13:58:12 +0300381 mtd->writesize, md);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200382 md->version[0] = buf[bbt_get_ver_offs(mtd, md)];
Brian Norris9a4d4d62011-07-19 10:06:07 -0700383 pr_info("Bad block table at page %d, version 0x%02X\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700384 md->pages[0], md->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Brian Norris8b6e50c2011-05-25 14:59:01 -0700388/* Scan a given block full */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200389static int scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
390 loff_t offs, uint8_t *buf, size_t readlen,
391 int scanlen, int len)
392{
393 int ret, j;
394
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200395 ret = scan_read_raw_oob(mtd, buf, offs, readlen);
Brian Norrisafa17de2011-09-07 13:13:29 -0700396 /* Ignore ECC errors when checking for BBM */
Brian Norrisd57f40542011-09-20 18:34:25 -0700397 if (ret && !mtd_is_bitflip_or_eccerr(ret))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200398 return ret;
399
400 for (j = 0; j < len; j++, buf += scanlen) {
401 if (check_pattern(buf, scanlen, mtd->writesize, bd))
402 return 1;
403 }
404 return 0;
405}
406
Brian Norris8b6e50c2011-05-25 14:59:01 -0700407/* Scan a given block partially */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200408static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
409 loff_t offs, uint8_t *buf, int len)
410{
411 struct mtd_oob_ops ops;
412 int j, ret;
413
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200414 ops.ooblen = mtd->oobsize;
415 ops.oobbuf = buf;
416 ops.ooboffs = 0;
417 ops.datbuf = NULL;
Brian Norris0612b9d2011-08-30 18:45:40 -0700418 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200419
420 for (j = 0; j < len; j++) {
421 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700422 * Read the full oob until read_oob is fixed to handle single
423 * byte reads for 16 bit buswidth.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200424 */
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200425 ret = mtd_read_oob(mtd, offs, &ops);
Brian Norris903cd062011-06-28 16:28:58 -0700426 /* Ignore ECC errors when checking for BBM */
Brian Norrisd57f40542011-09-20 18:34:25 -0700427 if (ret && !mtd_is_bitflip_or_eccerr(ret))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200428 return ret;
429
430 if (check_short_pattern(buf, bd))
431 return 1;
432
433 offs += mtd->writesize;
434 }
435 return 0;
436}
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438/**
439 * create_bbt - [GENERIC] Create a bad block table by scanning the device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700440 * @mtd: MTD device structure
441 * @buf: temporary buffer
442 * @bd: descriptor for the good/bad block search pattern
443 * @chip: create the table for a specific chip, -1 read all chips; applies only
444 * if NAND_BBT_PERCHIP option is set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700446 * Create a bad block table by scanning the device for the given good/bad block
447 * identify pattern.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200449static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
450 struct nand_bbt_descr *bd, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 struct nand_chip *this = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200453 int i, numblocks, len, scanlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 int startblock;
455 loff_t from;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200456 size_t readlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Brian Norris9a4d4d62011-07-19 10:06:07 -0700458 pr_info("Scanning device for bad blocks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 if (bd->options & NAND_BBT_SCANALLPAGES)
461 len = 1 << (this->bbt_erase_shift - this->page_shift);
Brian Norris58373ff2010-07-15 12:15:44 -0700462 else if (bd->options & NAND_BBT_SCAN2NDPAGE)
463 len = 2;
464 else
465 len = 1;
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000466
467 if (!(bd->options & NAND_BBT_SCANEMPTY)) {
468 /* We need only read few bytes from the OOB area */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200469 scanlen = 0;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000470 readlen = bd->len;
471 } else {
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000472 /* Full page content should be read */
Joern Engel28318772006-05-22 23:18:05 +0200473 scanlen = mtd->writesize + mtd->oobsize;
474 readlen = len * mtd->writesize;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 if (chip == -1) {
Brian Norris8b6e50c2011-05-25 14:59:01 -0700478 /*
479 * Note that numblocks is 2 * (real numblocks) here, see i+=2
480 * below as it makes shifting and masking less painful
481 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 numblocks = mtd->size >> (this->bbt_erase_shift - 1);
483 startblock = 0;
484 from = 0;
485 } else {
486 if (chip >= this->numchips) {
Brian Norris9a4d4d62011-07-19 10:06:07 -0700487 pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
David Woodhousee0c7d762006-05-13 18:07:53 +0100488 chip + 1, this->numchips);
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000489 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491 numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
492 startblock = chip * numblocks;
493 numblocks += startblock;
Adrian Hunter69423d92008-12-10 13:37:21 +0000494 from = (loff_t)startblock << (this->bbt_erase_shift - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000496
Brian Norris5fb15492011-05-31 16:31:21 -0700497 if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700498 from += mtd->erasesize - (mtd->writesize * len);
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 for (i = startblock; i < numblocks;) {
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000501 int ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000502
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200503 BUG_ON(bd->options & NAND_BBT_NO_OOB);
504
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200505 if (bd->options & NAND_BBT_SCANALLPAGES)
506 ret = scan_block_full(mtd, bd, from, buf, readlen,
507 scanlen, len);
508 else
509 ret = scan_block_fast(mtd, bd, from, buf, len);
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000510
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200511 if (ret < 0)
512 return ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000513
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200514 if (ret) {
515 this->bbt[i >> 3] |= 0x03 << (i & 0x6);
Brian Norris9a4d4d62011-07-19 10:06:07 -0700516 pr_warn("Bad eraseblock %d at 0x%012llx\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700517 i >> 1, (unsigned long long)from);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200518 mtd->ecc_stats.badblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 i += 2;
522 from += (1 << this->bbt_erase_shift);
523 }
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
527/**
528 * search_bbt - [GENERIC] scan the device for a specific bad block table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700529 * @mtd: MTD device structure
530 * @buf: temporary buffer
531 * @td: descriptor for the bad block table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700533 * Read the bad block table by searching for a given ident pattern. Search is
534 * preformed either from the beginning up or from the end of the device
535 * downwards. The search starts always at the start of a block. If the option
536 * NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains
537 * the bad block information of this chip. This is necessary to provide support
538 * for certain DOC devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700540 * The bbt ident pattern resides in the oob area of the first page in a block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100542static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 struct nand_chip *this = mtd->priv;
545 int i, chips;
546 int bits, startblock, block, dir;
Joern Engel28318772006-05-22 23:18:05 +0200547 int scanlen = mtd->writesize + mtd->oobsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 int bbtblocks;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200549 int blocktopage = this->bbt_erase_shift - this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Brian Norris8b6e50c2011-05-25 14:59:01 -0700551 /* Search direction top -> down? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (td->options & NAND_BBT_LASTBLOCK) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100553 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 dir = -1;
555 } else {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000556 startblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000558 }
559
Brian Norris8b6e50c2011-05-25 14:59:01 -0700560 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (td->options & NAND_BBT_PERCHIP) {
562 chips = this->numchips;
563 bbtblocks = this->chipsize >> this->bbt_erase_shift;
564 startblock &= bbtblocks - 1;
565 } else {
566 chips = 1;
567 bbtblocks = mtd->size >> this->bbt_erase_shift;
568 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /* Number of bits for each erase block in the bbt */
571 bits = td->options & NAND_BBT_NRBITS_MSK;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 for (i = 0; i < chips; i++) {
574 /* Reset version information */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000575 td->version[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 td->pages[i] = -1;
577 /* Scan the maximum number of blocks */
578 for (block = 0; block < td->maxblocks; block++) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 int actblock = startblock + dir * block;
Adrian Hunter69423d92008-12-10 13:37:21 +0000581 loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 /* Read first page */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200584 scan_read_raw(mtd, buf, offs, mtd->writesize, td);
Joern Engel28318772006-05-22 23:18:05 +0200585 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200586 td->pages[i] = actblock << blocktopage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (td->options & NAND_BBT_VERSION) {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200588 offs = bbt_get_ver_offs(mtd, td);
589 td->version[i] = buf[offs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591 break;
592 }
593 }
594 startblock += this->chipsize >> this->bbt_erase_shift;
595 }
596 /* Check, if we found a bbt for each requested chip */
597 for (i = 0; i < chips; i++) {
598 if (td->pages[i] == -1)
Brian Norris9a4d4d62011-07-19 10:06:07 -0700599 pr_warn("Bad block table not found for chip %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 else
Brian Norrisd0370212011-07-19 10:06:08 -0700601 pr_info("Bad block table found at page %d, version "
602 "0x%02X\n", td->pages[i], td->version[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000604 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607/**
608 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -0700609 * @mtd: MTD device structure
610 * @buf: temporary buffer
611 * @td: descriptor for the bad block table
612 * @md: descriptor for the bad block table mirror
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700614 * Search and read the bad block table(s).
615 */
Brian Norris7b5a2d42012-06-22 16:35:41 -0700616static void search_read_bbts(struct mtd_info *mtd, uint8_t *buf,
617 struct nand_bbt_descr *td,
618 struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 /* Search the primary table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100621 search_bbt(mtd, buf, td);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 /* Search the mirror table */
624 if (md)
David Woodhousee0c7d762006-05-13 18:07:53 +0100625 search_bbt(mtd, buf, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000626}
627
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000628/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 * write_bbt - [GENERIC] (Re)write the bad block table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700630 * @mtd: MTD device structure
631 * @buf: temporary buffer
632 * @td: descriptor for the bad block table
633 * @md: descriptor for the bad block table mirror
634 * @chipsel: selector for a specific chip, -1 for all
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700636 * (Re)write the bad block table.
637 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100638static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
Thomas Gleixner9223a452006-05-23 17:21:03 +0200639 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
640 int chipsel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
642 struct nand_chip *this = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 struct erase_info einfo;
644 int i, j, res, chip = 0;
645 int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200646 int nrchips, bbtoffs, pageoffs, ooboffs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 uint8_t msk[4];
648 uint8_t rcode = td->reserved_block_code;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200649 size_t retlen, len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 loff_t to;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200651 struct mtd_oob_ops ops;
652
653 ops.ooblen = mtd->oobsize;
654 ops.ooboffs = 0;
655 ops.datbuf = NULL;
Brian Norris0612b9d2011-08-30 18:45:40 -0700656 ops.mode = MTD_OPS_PLACE_OOB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 if (!rcode)
659 rcode = 0xff;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700660 /* Write bad block table per chip rather than per device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (td->options & NAND_BBT_PERCHIP) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100662 numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700663 /* Full device write or specific chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (chipsel == -1) {
665 nrchips = this->numchips;
666 } else {
667 nrchips = chipsel + 1;
668 chip = chipsel;
669 }
670 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +0100671 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 nrchips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000673 }
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /* Loop through the chips */
676 for (; chip < nrchips; chip++) {
Brian Norris8b6e50c2011-05-25 14:59:01 -0700677 /*
678 * There was already a version of the table, reuse the page
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000679 * This applies for absolute placement too, as we have the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 * page nr. in td->pages.
681 */
682 if (td->pages[chip] != -1) {
683 page = td->pages[chip];
684 goto write;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Brian Norris8b6e50c2011-05-25 14:59:01 -0700687 /*
688 * Automatic placement of the bad block table. Search direction
689 * top -> down?
690 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (td->options & NAND_BBT_LASTBLOCK) {
692 startblock = numblocks * (chip + 1) - 1;
693 dir = -1;
694 } else {
695 startblock = chip * numblocks;
696 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 for (i = 0; i < td->maxblocks; i++) {
700 int block = startblock + dir * i;
701 /* Check, if the block is bad */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200702 switch ((this->bbt[block >> 2] >>
703 (2 * (block & 0x03))) & 0x03) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 case 0x01:
705 case 0x03:
706 continue;
707 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200708 page = block <<
709 (this->bbt_erase_shift - this->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /* Check, if the block is used by the mirror table */
711 if (!md || md->pages[chip] != page)
712 goto write;
713 }
Brian Norris9a4d4d62011-07-19 10:06:07 -0700714 pr_err("No space left to write bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return -ENOSPC;
David Woodhousee0c7d762006-05-13 18:07:53 +0100716 write:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 /* Set up shift count and masks for the flash table */
719 bits = td->options & NAND_BBT_NRBITS_MSK;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200720 msk[2] = ~rcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 switch (bits) {
Thomas Gleixner9223a452006-05-23 17:21:03 +0200722 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
723 msk[3] = 0x01;
724 break;
725 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
726 msk[3] = 0x03;
727 break;
728 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
729 msk[3] = 0x0f;
730 break;
731 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
732 msk[3] = 0xff;
733 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 default: return -EINVAL;
735 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 bbtoffs = chip * (numblocks >> 2);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000738
Brian Norris596d7452011-09-07 13:13:33 -0700739 to = ((loff_t)page) << this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Brian Norris8b6e50c2011-05-25 14:59:01 -0700741 /* Must we save the block contents? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (td->options & NAND_BBT_SAVECONTENT) {
743 /* Make it block aligned */
Brian Norris596d7452011-09-07 13:13:33 -0700744 to &= ~((loff_t)((1 << this->bbt_erase_shift) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 len = 1 << this->bbt_erase_shift;
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200746 res = mtd_read(mtd, to, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (res < 0) {
748 if (retlen != len) {
Brian Norrisd0370212011-07-19 10:06:08 -0700749 pr_info("nand_bbt: error reading block "
750 "for writing the bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return res;
752 }
Brian Norrisd0370212011-07-19 10:06:08 -0700753 pr_warn("nand_bbt: ECC error while reading "
754 "block for writing bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200756 /* Read oob data */
Vitaly Wool70145682006-11-03 18:20:38 +0300757 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200758 ops.oobbuf = &buf[len];
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200759 res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
Vitaly Wool70145682006-11-03 18:20:38 +0300760 if (res < 0 || ops.oobretlen != ops.ooblen)
Thomas Gleixner9223a452006-05-23 17:21:03 +0200761 goto outerr;
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 /* Calc the byte offset in the buffer */
764 pageoffs = page - (int)(to >> this->page_shift);
765 offs = pageoffs << this->page_shift;
766 /* Preset the bbt area with 0xff */
Brian Norris596d7452011-09-07 13:13:33 -0700767 memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
Thomas Gleixner9223a452006-05-23 17:21:03 +0200768 ooboffs = len + (pageoffs * mtd->oobsize);
769
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200770 } else if (td->options & NAND_BBT_NO_OOB) {
771 ooboffs = 0;
772 offs = td->len;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700773 /* The version byte */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200774 if (td->options & NAND_BBT_VERSION)
775 offs++;
776 /* Calc length */
Brian Norris596d7452011-09-07 13:13:33 -0700777 len = (size_t)(numblocks >> sft);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200778 len += offs;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700779 /* Make it page aligned! */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200780 len = ALIGN(len, mtd->writesize);
781 /* Preset the buffer with 0xff */
782 memset(buf, 0xff, len);
783 /* Pattern is located at the begin of first page */
784 memcpy(buf, td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 } else {
786 /* Calc length */
Brian Norris596d7452011-09-07 13:13:33 -0700787 len = (size_t)(numblocks >> sft);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700788 /* Make it page aligned! */
Sebastian Andrzej Siewiorcda32092010-09-29 19:43:50 +0200789 len = ALIGN(len, mtd->writesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 /* Preset the buffer with 0xff */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200791 memset(buf, 0xff, len +
792 (len >> this->page_shift)* mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 offs = 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200794 ooboffs = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /* Pattern is located in oob area of first page */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200796 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000798
Thomas Gleixner9223a452006-05-23 17:21:03 +0200799 if (td->options & NAND_BBT_VERSION)
800 buf[ooboffs + td->veroffs] = td->version[chip];
801
Brian Norris8b6e50c2011-05-25 14:59:01 -0700802 /* Walk through the memory table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100803 for (i = 0; i < numblocks;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 uint8_t dat;
805 dat = this->bbt[bbtoffs + (i >> 2)];
David Woodhousee0c7d762006-05-13 18:07:53 +0100806 for (j = 0; j < 4; j++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 int sftcnt = (i << (3 - sft)) & sftmsk;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700808 /* Do not store the reserved bbt blocks! */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200809 buf[offs + (i >> sft)] &=
810 ~(msk[dat & 0x03] << sftcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 dat >>= 2;
812 }
813 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000814
David Woodhousee0c7d762006-05-13 18:07:53 +0100815 memset(&einfo, 0, sizeof(einfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 einfo.mtd = mtd;
Adrian Hunter69423d92008-12-10 13:37:21 +0000817 einfo.addr = to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 einfo.len = 1 << this->bbt_erase_shift;
David Woodhousee0c7d762006-05-13 18:07:53 +0100819 res = nand_erase_nand(mtd, &einfo, 1);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200820 if (res < 0)
821 goto outerr;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000822
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200823 res = scan_write_bbt(mtd, to, len, buf,
824 td->options & NAND_BBT_NO_OOB ? NULL :
825 &buf[len]);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200826 if (res < 0)
827 goto outerr;
828
Brian Norrisd0370212011-07-19 10:06:08 -0700829 pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
830 (unsigned long long)to, td->version[chip]);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /* Mark it as used */
833 td->pages[chip] = page;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200836
837 outerr:
Brian Norrisd0370212011-07-19 10:06:08 -0700838 pr_warn("nand_bbt: error while writing bad block table %d\n", res);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200839 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
842/**
843 * nand_memory_bbt - [GENERIC] create a memory based bad block table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700844 * @mtd: MTD device structure
845 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700847 * The function creates a memory based bbt by scanning the device for
848 * manufacturer / software marked good / bad blocks.
849 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100850static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
852 struct nand_chip *this = mtd->priv;
853
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000854 bd->options &= ~NAND_BBT_SCANEMPTY;
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100855 return create_bbt(mtd, this->buffers->databuf, bd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
858/**
David Woodhousee0c7d762006-05-13 18:07:53 +0100859 * check_create - [GENERIC] create and write bbt(s) if necessary
Brian Norris8b6e50c2011-05-25 14:59:01 -0700860 * @mtd: MTD device structure
861 * @buf: temporary buffer
862 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700864 * The function checks the results of the previous call to read_bbt and creates
865 * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found
866 * for the chip/device. Update is necessary if one of the tables is missing or
867 * the version nr. of one table is less than the other.
868 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100869static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Brian Norris623978d2011-09-20 18:35:34 -0700871 int i, chips, writeops, create, chipsel, res, res2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 struct nand_chip *this = mtd->priv;
873 struct nand_bbt_descr *td = this->bbt_td;
874 struct nand_bbt_descr *md = this->bbt_md;
875 struct nand_bbt_descr *rd, *rd2;
876
Brian Norris8b6e50c2011-05-25 14:59:01 -0700877 /* Do we have a bbt per chip? */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000878 if (td->options & NAND_BBT_PERCHIP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 chips = this->numchips;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000880 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 chips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 for (i = 0; i < chips; i++) {
884 writeops = 0;
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700885 create = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 rd = NULL;
887 rd2 = NULL;
Brian Norris623978d2011-09-20 18:35:34 -0700888 res = res2 = 0;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700889 /* Per chip or per device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700891 /* Mirrored table available? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (md) {
893 if (td->pages[i] == -1 && md->pages[i] == -1) {
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700894 create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 writeops = 0x03;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700896 } else if (td->pages[i] == -1) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000897 rd = md;
Brian Norris596d7452011-09-07 13:13:33 -0700898 writeops = 0x01;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700899 } else if (md->pages[i] == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 rd = td;
Brian Norris596d7452011-09-07 13:13:33 -0700901 writeops = 0x02;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700902 } else if (td->version[i] == md->version[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 rd = td;
904 if (!(td->options & NAND_BBT_VERSION))
905 rd2 = md;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700906 } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 rd = td;
Brian Norris596d7452011-09-07 13:13:33 -0700908 writeops = 0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 } else {
910 rd = md;
Brian Norris596d7452011-09-07 13:13:33 -0700911 writeops = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 } else {
914 if (td->pages[i] == -1) {
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700915 create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 writeops = 0x01;
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700917 } else {
918 rd = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000921
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700922 if (create) {
923 /* Create the bad block table by scanning the device? */
924 if (!(td->options & NAND_BBT_CREATE))
925 continue;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000926
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700927 /* Create the table in memory by scanning the chip(s) */
928 if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
929 create_bbt(mtd, buf, bd, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700931 td->version[i] = 1;
932 if (md)
933 md->version[i] = 1;
934 }
935
Brian Norris8b6e50c2011-05-25 14:59:01 -0700936 /* Read back first? */
Brian Norris623978d2011-09-20 18:35:34 -0700937 if (rd) {
938 res = read_abs_bbt(mtd, buf, rd, chipsel);
939 if (mtd_is_eccerr(res)) {
940 /* Mark table as invalid */
941 rd->pages[i] = -1;
Brian Norrisdadc17a2011-09-20 18:35:57 -0700942 rd->version[i] = 0;
Brian Norris623978d2011-09-20 18:35:34 -0700943 i--;
944 continue;
945 }
946 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700947 /* If they weren't versioned, read both */
Brian Norris623978d2011-09-20 18:35:34 -0700948 if (rd2) {
949 res2 = read_abs_bbt(mtd, buf, rd2, chipsel);
950 if (mtd_is_eccerr(res2)) {
951 /* Mark table as invalid */
952 rd2->pages[i] = -1;
Brian Norrisdadc17a2011-09-20 18:35:57 -0700953 rd2->version[i] = 0;
Brian Norris623978d2011-09-20 18:35:34 -0700954 i--;
955 continue;
956 }
957 }
958
959 /* Scrub the flash table(s)? */
960 if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
961 writeops = 0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Brian Norrisdadc17a2011-09-20 18:35:57 -0700963 /* Update version numbers before writing */
964 if (md) {
965 td->version[i] = max(td->version[i], md->version[i]);
966 md->version[i] = td->version[i];
967 }
968
Brian Norris8b6e50c2011-05-25 14:59:01 -0700969 /* Write the bad block table to the device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100971 res = write_bbt(mtd, buf, td, md, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (res < 0)
973 return res;
974 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000975
Brian Norris8b6e50c2011-05-25 14:59:01 -0700976 /* Write the mirror bad block table to the device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100978 res = write_bbt(mtd, buf, md, td, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (res < 0)
980 return res;
981 }
982 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000983 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
986/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000987 * mark_bbt_regions - [GENERIC] mark the bad block table regions
Brian Norris8b6e50c2011-05-25 14:59:01 -0700988 * @mtd: MTD device structure
989 * @td: bad block table descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700991 * The bad block table regions are marked as "bad" to prevent accidental
992 * erasures / writes. The regions are identified by the mark 0x02.
993 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100994static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 struct nand_chip *this = mtd->priv;
997 int i, j, chips, block, nrblocks, update;
998 uint8_t oldval, newval;
999
Brian Norris8b6e50c2011-05-25 14:59:01 -07001000 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (td->options & NAND_BBT_PERCHIP) {
1002 chips = this->numchips;
1003 nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
1004 } else {
1005 chips = 1;
1006 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001007 }
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 for (i = 0; i < chips; i++) {
1010 if ((td->options & NAND_BBT_ABSPAGE) ||
1011 !(td->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001012 if (td->pages[i] == -1)
1013 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001015 block <<= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 oldval = this->bbt[(block >> 3)];
1017 newval = oldval | (0x2 << (block & 0x06));
1018 this->bbt[(block >> 3)] = newval;
1019 if ((oldval != newval) && td->reserved_block_code)
Adrian Hunter69423d92008-12-10 13:37:21 +00001020 nand_update_bbt(mtd, (loff_t)block << (this->bbt_erase_shift - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 continue;
1022 }
1023 update = 0;
1024 if (td->options & NAND_BBT_LASTBLOCK)
1025 block = ((i + 1) * nrblocks) - td->maxblocks;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001026 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 block = i * nrblocks;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001028 block <<= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 for (j = 0; j < td->maxblocks; j++) {
1030 oldval = this->bbt[(block >> 3)];
1031 newval = oldval | (0x2 << (block & 0x06));
1032 this->bbt[(block >> 3)] = newval;
David Woodhousee0c7d762006-05-13 18:07:53 +01001033 if (oldval != newval)
1034 update = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 block += 2;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001036 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07001037 /*
1038 * If we want reserved blocks to be recorded to flash, and some
1039 * new ones have been marked, then we need to update the stored
1040 * bbts. This should only happen once.
1041 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (update && td->reserved_block_code)
Adrian Hunter69423d92008-12-10 13:37:21 +00001043 nand_update_bbt(mtd, (loff_t)(block - 2) << (this->bbt_erase_shift - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045}
1046
1047/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001048 * verify_bbt_descr - verify the bad block description
Brian Norris8b6e50c2011-05-25 14:59:01 -07001049 * @mtd: MTD device structure
1050 * @bd: the table to verify
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001051 *
1052 * This functions performs a few sanity checks on the bad block description
1053 * table.
1054 */
1055static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
1056{
1057 struct nand_chip *this = mtd->priv;
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001058 u32 pattern_len;
1059 u32 bits;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001060 u32 table_size;
1061
1062 if (!bd)
1063 return;
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001064
1065 pattern_len = bd->len;
1066 bits = bd->options & NAND_BBT_NRBITS_MSK;
1067
Brian Norrisa40f7342011-05-31 16:31:22 -07001068 BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001069 !(this->bbt_options & NAND_BBT_USE_FLASH));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001070 BUG_ON(!bits);
1071
1072 if (bd->options & NAND_BBT_VERSION)
1073 pattern_len++;
1074
1075 if (bd->options & NAND_BBT_NO_OOB) {
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001076 BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
Brian Norrisa40f7342011-05-31 16:31:22 -07001077 BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001078 BUG_ON(bd->offs);
1079 if (bd->options & NAND_BBT_VERSION)
1080 BUG_ON(bd->veroffs != bd->len);
1081 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1082 }
1083
1084 if (bd->options & NAND_BBT_PERCHIP)
1085 table_size = this->chipsize >> this->bbt_erase_shift;
1086 else
1087 table_size = mtd->size >> this->bbt_erase_shift;
1088 table_size >>= 3;
1089 table_size *= bits;
1090 if (bd->options & NAND_BBT_NO_OOB)
1091 table_size += pattern_len;
1092 BUG_ON(table_size > (1 << this->bbt_erase_shift));
1093}
1094
1095/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07001097 * @mtd: MTD device structure
1098 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001100 * The function checks, if a bad block table(s) is/are already available. If
1101 * not it scans the device for manufacturer marked good / bad blocks and writes
1102 * the bad block table(s) to the selected place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001104 * The bad block table memory is allocated here. It must be freed by calling
1105 * the nand_free_bbt function.
1106 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001107int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
1109 struct nand_chip *this = mtd->priv;
1110 int len, res = 0;
1111 uint8_t *buf;
1112 struct nand_bbt_descr *td = this->bbt_td;
1113 struct nand_bbt_descr *md = this->bbt_md;
1114
1115 len = mtd->size >> (this->bbt_erase_shift + 2);
Brian Norris8b6e50c2011-05-25 14:59:01 -07001116 /*
1117 * Allocate memory (2bit per block) and clear the memory bad block
1118 * table.
1119 */
Burman Yan95b93a02006-11-15 21:10:29 +02001120 this->bbt = kzalloc(len, GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001121 if (!this->bbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Brian Norris8b6e50c2011-05-25 14:59:01 -07001124 /*
1125 * If no primary table decriptor is given, scan the device to build a
1126 * memory based bad block table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 */
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001128 if (!td) {
1129 if ((res = nand_memory_bbt(mtd, bd))) {
Brian Norrisd0370212011-07-19 10:06:08 -07001130 pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
David Woodhousee0c7d762006-05-13 18:07:53 +01001131 kfree(this->bbt);
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001132 this->bbt = NULL;
1133 }
1134 return res;
1135 }
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001136 verify_bbt_descr(mtd, td);
1137 verify_bbt_descr(mtd, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 /* Allocate a temporary buffer for one eraseblock incl. oob */
1140 len = (1 << this->bbt_erase_shift);
1141 len += (len >> this->page_shift) * mtd->oobsize;
David Woodhousec3f8abf2006-05-13 04:03:42 +01001142 buf = vmalloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (!buf) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001144 kfree(this->bbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 this->bbt = NULL;
1146 return -ENOMEM;
1147 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001148
Brian Norris8b6e50c2011-05-25 14:59:01 -07001149 /* Is the bbt at a given page? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (td->options & NAND_BBT_ABSPAGE) {
Brian Norris7b5a2d42012-06-22 16:35:41 -07001151 read_abs_bbts(mtd, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001152 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /* Search the bad block table using a pattern in oob */
Brian Norris7b5a2d42012-06-22 16:35:41 -07001154 search_read_bbts(mtd, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Brian Norris7b5a2d42012-06-22 16:35:41 -07001157 res = check_create(mtd, buf, bd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 /* Prevent the bbt regions from erasing / writing */
David Woodhousee0c7d762006-05-13 18:07:53 +01001160 mark_bbt_region(mtd, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (md)
David Woodhousee0c7d762006-05-13 18:07:53 +01001162 mark_bbt_region(mtd, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001163
David Woodhousee0c7d762006-05-13 18:07:53 +01001164 vfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return res;
1166}
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001169 * nand_update_bbt - [NAND Interface] update bad block table(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07001170 * @mtd: MTD device structure
1171 * @offs: the offset of the newly marked block
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001173 * The function updates the bad block table(s).
1174 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001175int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 struct nand_chip *this = mtd->priv;
Brian Norris1196ac52011-09-07 13:13:32 -07001178 int len, res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 int chip, chipsel;
1180 uint8_t *buf;
1181 struct nand_bbt_descr *td = this->bbt_td;
1182 struct nand_bbt_descr *md = this->bbt_md;
1183
1184 if (!this->bbt || !td)
1185 return -EINVAL;
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 /* Allocate a temporary buffer for one eraseblock incl. oob */
1188 len = (1 << this->bbt_erase_shift);
1189 len += (len >> this->page_shift) * mtd->oobsize;
David Woodhousee0c7d762006-05-13 18:07:53 +01001190 buf = kmalloc(len, GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001191 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return -ENOMEM;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001193
Brian Norris8b6e50c2011-05-25 14:59:01 -07001194 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (td->options & NAND_BBT_PERCHIP) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001196 chip = (int)(offs >> this->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 chipsel = chip;
1198 } else {
1199 chip = 0;
1200 chipsel = -1;
1201 }
1202
1203 td->version[chip]++;
1204 if (md)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001205 md->version[chip]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Brian Norris8b6e50c2011-05-25 14:59:01 -07001207 /* Write the bad block table to the device? */
Brian Norris1196ac52011-09-07 13:13:32 -07001208 if (td->options & NAND_BBT_WRITE) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001209 res = write_bbt(mtd, buf, td, md, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (res < 0)
1211 goto out;
1212 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07001213 /* Write the mirror bad block table to the device? */
Brian Norris1196ac52011-09-07 13:13:32 -07001214 if (md && (md->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001215 res = write_bbt(mtd, buf, md, td, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217
David Woodhousee0c7d762006-05-13 18:07:53 +01001218 out:
1219 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return res;
1221}
1222
Brian Norris8b6e50c2011-05-25 14:59:01 -07001223/*
1224 * Define some generic bad / good block scan pattern which are used
1225 * while scanning a device for factory marked good / bad blocks.
1226 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
1230
1231static struct nand_bbt_descr agand_flashbased = {
1232 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
1233 .offs = 0x20,
1234 .len = 6,
1235 .pattern = scan_agand_pattern
1236};
1237
Brian Norris7854d3f2011-06-23 14:12:08 -07001238/* Generic flash bbt descriptors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1240static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1241
1242static struct nand_bbt_descr bbt_main_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001243 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1245 .offs = 8,
1246 .len = 4,
1247 .veroffs = 12,
1248 .maxblocks = 4,
1249 .pattern = bbt_pattern
1250};
1251
1252static struct nand_bbt_descr bbt_mirror_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001253 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1255 .offs = 8,
1256 .len = 4,
1257 .veroffs = 12,
1258 .maxblocks = 4,
1259 .pattern = mirror_pattern
1260};
1261
Brian Norris9fd6b372012-06-22 16:35:40 -07001262static struct nand_bbt_descr bbt_main_no_oob_descr = {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001263 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1264 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1265 | NAND_BBT_NO_OOB,
1266 .len = 4,
1267 .veroffs = 4,
1268 .maxblocks = 4,
1269 .pattern = bbt_pattern
1270};
1271
Brian Norris9fd6b372012-06-22 16:35:40 -07001272static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001273 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1274 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1275 | NAND_BBT_NO_OOB,
1276 .len = 4,
1277 .veroffs = 4,
1278 .maxblocks = 4,
1279 .pattern = mirror_pattern
1280};
1281
Brian Norris752ed6c2011-09-20 18:36:42 -07001282#define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
Brian Norris58373ff2010-07-15 12:15:44 -07001283/**
Brian Norris752ed6c2011-09-20 18:36:42 -07001284 * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001285 * @this: NAND chip to create descriptor for
Brian Norris58373ff2010-07-15 12:15:44 -07001286 *
1287 * This function allocates and initializes a nand_bbt_descr for BBM detection
Brian Norris752ed6c2011-09-20 18:36:42 -07001288 * based on the properties of @this. The new descriptor is stored in
Brian Norris58373ff2010-07-15 12:15:44 -07001289 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1290 * passed to this function.
Brian Norris58373ff2010-07-15 12:15:44 -07001291 */
Brian Norris752ed6c2011-09-20 18:36:42 -07001292static int nand_create_badblock_pattern(struct nand_chip *this)
Brian Norris58373ff2010-07-15 12:15:44 -07001293{
1294 struct nand_bbt_descr *bd;
1295 if (this->badblock_pattern) {
Brian Norris752ed6c2011-09-20 18:36:42 -07001296 pr_warn("Bad block pattern already allocated; not replacing\n");
Brian Norris58373ff2010-07-15 12:15:44 -07001297 return -EINVAL;
1298 }
1299 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001300 if (!bd)
Brian Norris58373ff2010-07-15 12:15:44 -07001301 return -ENOMEM;
Brian Norris752ed6c2011-09-20 18:36:42 -07001302 bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
Brian Norris58373ff2010-07-15 12:15:44 -07001303 bd->offs = this->badblockpos;
1304 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1305 bd->pattern = scan_ff_pattern;
1306 bd->options |= NAND_BBT_DYNAMICSTRUCT;
1307 this->badblock_pattern = bd;
1308 return 0;
1309}
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001312 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
Brian Norris8b6e50c2011-05-25 14:59:01 -07001313 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001315 * This function selects the default bad block table support for the device and
1316 * calls the nand_scan_bbt function.
1317 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001318int nand_default_bbt(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 struct nand_chip *this = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001321
Brian Norris8b6e50c2011-05-25 14:59:01 -07001322 /*
1323 * Default for AG-AND. We must use a flash based bad block table as the
1324 * devices have factory marked _good_ blocks. Erasing those blocks
1325 * leads to loss of the good / bad information, so we _must_ store this
1326 * information in a good / bad table during startup.
David Woodhousee0c7d762006-05-13 18:07:53 +01001327 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (this->options & NAND_IS_AND) {
1329 /* Use the default pattern descriptors */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001330 if (!this->bbt_td) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 this->bbt_td = &bbt_main_descr;
1332 this->bbt_md = &bbt_mirror_descr;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001333 }
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001334 this->bbt_options |= NAND_BBT_USE_FLASH;
David Woodhousee0c7d762006-05-13 18:07:53 +01001335 return nand_scan_bbt(mtd, &agand_flashbased);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001337
Brian Norris8b6e50c2011-05-25 14:59:01 -07001338 /* Is a flash based bad block table requested? */
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001339 if (this->bbt_options & NAND_BBT_USE_FLASH) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001340 /* Use the default pattern descriptors */
1341 if (!this->bbt_td) {
Brian Norrisa40f7342011-05-31 16:31:22 -07001342 if (this->bbt_options & NAND_BBT_NO_OOB) {
Brian Norris9fd6b372012-06-22 16:35:40 -07001343 this->bbt_td = &bbt_main_no_oob_descr;
1344 this->bbt_md = &bbt_mirror_no_oob_descr;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001345 } else {
1346 this->bbt_td = &bbt_main_descr;
1347 this->bbt_md = &bbt_mirror_descr;
1348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 } else {
1351 this->bbt_td = NULL;
1352 this->bbt_md = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
Brian Norrisa2f812d2011-03-18 21:53:42 -07001354
1355 if (!this->badblock_pattern)
Brian Norris752ed6c2011-09-20 18:36:42 -07001356 nand_create_badblock_pattern(this);
Brian Norrisa2f812d2011-03-18 21:53:42 -07001357
David Woodhousee0c7d762006-05-13 18:07:53 +01001358 return nand_scan_bbt(mtd, this->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
1360
1361/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001362 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07001363 * @mtd: MTD device structure
1364 * @offs: offset in the device
1365 * @allowbbt: allow access to bad block table region
1366 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001367int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 struct nand_chip *this = mtd->priv;
1370 int block;
David Woodhousee0c7d762006-05-13 18:07:53 +01001371 uint8_t res;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 /* Get block number * 2 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001374 block = (int)(offs >> (this->bbt_erase_shift - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
1376
Brian Norris289c0522011-07-19 10:06:09 -07001377 pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: "
1378 "(block %d) 0x%02x\n",
1379 (unsigned int)offs, block >> 1, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 switch ((int)res) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001382 case 0x00:
1383 return 0;
1384 case 0x01:
1385 return 1;
1386 case 0x02:
1387 return allowbbt ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389 return 1;
1390}
1391
David Woodhousee0c7d762006-05-13 18:07:53 +01001392EXPORT_SYMBOL(nand_scan_bbt);
1393EXPORT_SYMBOL(nand_default_bbt);