blob: e23115be079e74e9d3a5b68ab6032ba0a9310f72 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020072static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
73{
Brian Norris718894a2012-06-22 16:35:43 -070074 if (memcmp(buf, td->pattern, td->len))
75 return -1;
76 return 0;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020077}
78
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000079/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * check_pattern - [GENERIC] check if a pattern is in the buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -070081 * @buf: the buffer to search
82 * @len: the length of buffer to search
83 * @paglen: the pagelength
84 * @td: search pattern descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 *
Brian Norris8b6e50c2011-05-25 14:59:01 -070086 * Check for a pattern at the given place. Used to search bad block tables and
87 * good / bad block identifiers. If the SCAN_EMPTY option is set then check, if
88 * all bytes except the pattern area contain 0xff.
89 */
David Woodhousee0c7d762006-05-13 18:07:53 +010090static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +000092 int i, end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 uint8_t *p = buf;
94
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020095 if (td->options & NAND_BBT_NO_OOB)
96 return check_pattern_no_oob(buf, td);
97
Thomas Gleixnerc9e053652005-06-14 16:39:57 +010098 end = paglen + td->offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (td->options & NAND_BBT_SCANEMPTY) {
100 for (i = 0; i < end; i++) {
101 if (p[i] != 0xff)
102 return -1;
103 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000104 }
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100105 p += end;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 /* Compare the pattern */
Brian Norris75b66d82011-09-07 13:13:41 -0700108 if (memcmp(p, td->pattern, td->len))
109 return -1;
Brian Norris58373ff2010-07-15 12:15:44 -0700110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (td->options & NAND_BBT_SCANEMPTY) {
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000112 p += td->len;
113 end += td->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 for (i = end; i < len; i++) {
115 if (*p++ != 0xff)
116 return -1;
117 }
118 }
119 return 0;
120}
121
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000122/**
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100123 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700124 * @buf: the buffer to search
125 * @td: search pattern descriptor
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100126 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700127 * Check for a pattern at the given place. Used to search bad block tables and
128 * good / bad block identifiers. Same as check_pattern, but no optional empty
129 * check.
130 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100131static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100132{
133 int i;
134 uint8_t *p = buf;
135
136 /* Compare the pattern */
137 for (i = 0; i < td->len; i++) {
Thomas Gleixner19870da2005-07-15 14:53:51 +0100138 if (p[td->offs + i] != td->pattern[i])
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100139 return -1;
140 }
141 return 0;
142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200145 * add_marker_len - compute the length of the marker in data area
Brian Norris8b6e50c2011-05-25 14:59:01 -0700146 * @td: BBT descriptor used for computation
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200147 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700148 * The length will be 0 if the marker is located in OOB area.
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200149 */
150static u32 add_marker_len(struct nand_bbt_descr *td)
151{
152 u32 len;
153
154 if (!(td->options & NAND_BBT_NO_OOB))
155 return 0;
156
157 len = td->len;
158 if (td->options & NAND_BBT_VERSION)
159 len++;
160 return len;
161}
162
163/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 * read_bbt - [GENERIC] Read the bad block table starting from page
Brian Norris8b6e50c2011-05-25 14:59:01 -0700165 * @mtd: MTD device structure
166 * @buf: temporary buffer
167 * @page: the starting page
168 * @num: the number of bbt descriptors to read
Brian Norris7854d3f2011-06-23 14:12:08 -0700169 * @td: the bbt describtion table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700170 * @offs: offset in the memory table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 *
172 * Read the bad block table starting from page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100174static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200175 struct nand_bbt_descr *td, int offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Brian Norris167a8d52011-09-20 18:35:08 -0700177 int res, ret = 0, i, j, act = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 struct nand_chip *this = mtd->priv;
179 size_t retlen, len, totlen;
180 loff_t from;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200181 int bits = td->options & NAND_BBT_NRBITS_MSK;
Brian Norris596d7452011-09-07 13:13:33 -0700182 uint8_t msk = (uint8_t)((1 << bits) - 1);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200183 u32 marker_len;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200184 int reserved_block_code = td->reserved_block_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 totlen = (num * bits) >> 3;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200187 marker_len = add_marker_len(td);
Brian Norris596d7452011-09-07 13:13:33 -0700188 from = ((loff_t)page) << this->page_shift;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 while (totlen) {
Brian Norris596d7452011-09-07 13:13:33 -0700191 len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200192 if (marker_len) {
193 /*
194 * In case the BBT marker is not in the OOB area it
195 * will be just in the first page.
196 */
197 len -= marker_len;
198 from += marker_len;
199 marker_len = 0;
200 }
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200201 res = mtd_read(mtd, from, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (res < 0) {
Brian Norris167a8d52011-09-20 18:35:08 -0700203 if (mtd_is_eccerr(res)) {
204 pr_info("nand_bbt: ECC error in BBT at "
205 "0x%012llx\n", from & ~mtd->writesize);
206 return res;
207 } else if (mtd_is_bitflip(res)) {
208 pr_info("nand_bbt: corrected error in BBT at "
209 "0x%012llx\n", from & ~mtd->writesize);
210 ret = res;
211 } else {
212 pr_info("nand_bbt: error reading BBT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return res;
214 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 /* Analyse data */
218 for (i = 0; i < len; i++) {
219 uint8_t dat = buf[i];
220 for (j = 0; j < 8; j += bits, act += 2) {
221 uint8_t tmp = (dat >> j) & msk;
222 if (tmp == msk)
223 continue;
David Woodhousee0c7d762006-05-13 18:07:53 +0100224 if (reserved_block_code && (tmp == reserved_block_code)) {
Brian Norrisd0370212011-07-19 10:06:08 -0700225 pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
226 (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200228 mtd->ecc_stats.bbtblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 continue;
230 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700231 /*
232 * Leave it for now, if it's matured we can
Brian Norrisa0f50802011-07-19 10:06:06 -0700233 * move this message to pr_debug.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700234 */
Brian Norrisd0370212011-07-19 10:06:08 -0700235 pr_info("nand_read_bbt: bad block at 0x%012llx\n",
236 (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700237 /* Factory marked bad or worn out? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (tmp == 0)
239 this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
240 else
241 this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200242 mtd->ecc_stats.badblocks++;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245 totlen -= len;
246 from += len;
247 }
Brian Norris167a8d52011-09-20 18:35:08 -0700248 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251/**
252 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
Brian Norris8b6e50c2011-05-25 14:59:01 -0700253 * @mtd: MTD device structure
254 * @buf: temporary buffer
255 * @td: descriptor for the bad block table
Brian Norris596d7452011-09-07 13:13:33 -0700256 * @chip: read the table for a specific chip, -1 read all chips; applies only if
Brian Norris8b6e50c2011-05-25 14:59:01 -0700257 * NAND_BBT_PERCHIP option is set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700259 * Read the bad block table for all chips starting at a given page. We assume
260 * that the bbt bits are in consecutive order.
261 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100262static 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 -0700263{
264 struct nand_chip *this = mtd->priv;
265 int res = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (td->options & NAND_BBT_PERCHIP) {
268 int offs = 0;
269 for (i = 0; i < this->numchips; i++) {
270 if (chip == -1 || chip == i)
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200271 res = read_bbt(mtd, buf, td->pages[i],
272 this->chipsize >> this->bbt_erase_shift,
273 td, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (res)
275 return res;
276 offs += this->chipsize >> (this->bbt_erase_shift + 2);
277 }
278 } else {
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200279 res = read_bbt(mtd, buf, td->pages[0],
280 mtd->size >> this->bbt_erase_shift, td, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (res)
282 return res;
283 }
284 return 0;
285}
286
Brian Norris8b6e50c2011-05-25 14:59:01 -0700287/* BBT marker is in the first page, no OOB */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200288static int scan_read_raw_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
289 struct nand_bbt_descr *td)
290{
291 size_t retlen;
292 size_t len;
293
294 len = td->len;
295 if (td->options & NAND_BBT_VERSION)
296 len++;
297
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200298 return mtd_read(mtd, offs, len, &retlen, buf);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200299}
300
Brian Norris8b6e50c2011-05-25 14:59:01 -0700301/* Scan read raw data from flash */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200302static int scan_read_raw_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200303 size_t len)
304{
305 struct mtd_oob_ops ops;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200306 int res;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200307
Brian Norris0612b9d2011-08-30 18:45:40 -0700308 ops.mode = MTD_OPS_RAW;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200309 ops.ooboffs = 0;
310 ops.ooblen = mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200311
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200312 while (len > 0) {
Brian Norris105513c2011-09-07 13:13:28 -0700313 ops.datbuf = buf;
314 ops.len = min(len, (size_t)mtd->writesize);
315 ops.oobbuf = buf + ops.len;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200316
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200317 res = mtd_read_oob(mtd, offs, &ops);
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200318
Brian Norrisafa17de2011-09-07 13:13:29 -0700319 if (res)
Brian Norris105513c2011-09-07 13:13:28 -0700320 return res;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200321
322 buf += mtd->oobsize + mtd->writesize;
323 len -= mtd->writesize;
Dmitry Maluka34a57042012-05-11 20:51:51 +0300324 offs += mtd->writesize;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200325 }
326 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200327}
328
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200329static int scan_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
330 size_t len, struct nand_bbt_descr *td)
331{
332 if (td->options & NAND_BBT_NO_OOB)
333 return scan_read_raw_data(mtd, buf, offs, td);
334 else
335 return scan_read_raw_oob(mtd, buf, offs, len);
336}
337
Brian Norris8b6e50c2011-05-25 14:59:01 -0700338/* Scan write data with oob to flash */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200339static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
340 uint8_t *buf, uint8_t *oob)
341{
342 struct mtd_oob_ops ops;
343
Brian Norris0612b9d2011-08-30 18:45:40 -0700344 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200345 ops.ooboffs = 0;
346 ops.ooblen = mtd->oobsize;
347 ops.datbuf = buf;
348 ops.oobbuf = oob;
349 ops.len = len;
350
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200351 return mtd_write_oob(mtd, offs, &ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200352}
353
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200354static u32 bbt_get_ver_offs(struct mtd_info *mtd, struct nand_bbt_descr *td)
355{
356 u32 ver_offs = td->veroffs;
357
358 if (!(td->options & NAND_BBT_NO_OOB))
359 ver_offs += mtd->writesize;
360 return ver_offs;
361}
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363/**
364 * 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 -0700365 * @mtd: MTD device structure
366 * @buf: temporary buffer
367 * @td: descriptor for the bad block table
368 * @md: descriptor for the bad block table mirror
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700370 * Read the bad block table(s) for all chips starting at a given page. We
371 * assume that the bbt bits are in consecutive order.
372 */
Brian Norris7b5a2d42012-06-22 16:35:41 -0700373static void read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
374 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 struct nand_chip *this = mtd->priv;
377
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000378 /* Read the primary version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (td->options & NAND_BBT_VERSION) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000380 scan_read_raw(mtd, buf, (loff_t)td->pages[0] << this->page_shift,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200381 mtd->writesize, td);
382 td->version[0] = buf[bbt_get_ver_offs(mtd, td)];
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 td->pages[0], td->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000387 /* Read the mirror version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (md && (md->options & NAND_BBT_VERSION)) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000389 scan_read_raw(mtd, buf, (loff_t)md->pages[0] << this->page_shift,
Shmulik Ladkani7bb9c752012-06-10 13:58:12 +0300390 mtd->writesize, md);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200391 md->version[0] = buf[bbt_get_ver_offs(mtd, md)];
Brian Norris9a4d4d62011-07-19 10:06:07 -0700392 pr_info("Bad block table at page %d, version 0x%02X\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700393 md->pages[0], md->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
Brian Norris8b6e50c2011-05-25 14:59:01 -0700397/* Scan a given block full */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200398static int scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
399 loff_t offs, uint8_t *buf, size_t readlen,
400 int scanlen, int len)
401{
402 int ret, j;
403
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200404 ret = scan_read_raw_oob(mtd, buf, offs, readlen);
Brian Norrisafa17de2011-09-07 13:13:29 -0700405 /* Ignore ECC errors when checking for BBM */
Brian Norrisd57f40542011-09-20 18:34:25 -0700406 if (ret && !mtd_is_bitflip_or_eccerr(ret))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200407 return ret;
408
409 for (j = 0; j < len; j++, buf += scanlen) {
410 if (check_pattern(buf, scanlen, mtd->writesize, bd))
411 return 1;
412 }
413 return 0;
414}
415
Brian Norris8b6e50c2011-05-25 14:59:01 -0700416/* Scan a given block partially */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200417static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
418 loff_t offs, uint8_t *buf, int len)
419{
420 struct mtd_oob_ops ops;
421 int j, ret;
422
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200423 ops.ooblen = mtd->oobsize;
424 ops.oobbuf = buf;
425 ops.ooboffs = 0;
426 ops.datbuf = NULL;
Brian Norris0612b9d2011-08-30 18:45:40 -0700427 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200428
429 for (j = 0; j < len; j++) {
430 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700431 * Read the full oob until read_oob is fixed to handle single
432 * byte reads for 16 bit buswidth.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200433 */
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200434 ret = mtd_read_oob(mtd, offs, &ops);
Brian Norris903cd062011-06-28 16:28:58 -0700435 /* Ignore ECC errors when checking for BBM */
Brian Norrisd57f40542011-09-20 18:34:25 -0700436 if (ret && !mtd_is_bitflip_or_eccerr(ret))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200437 return ret;
438
439 if (check_short_pattern(buf, bd))
440 return 1;
441
442 offs += mtd->writesize;
443 }
444 return 0;
445}
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447/**
448 * create_bbt - [GENERIC] Create a bad block table by scanning the device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700449 * @mtd: MTD device structure
450 * @buf: temporary buffer
451 * @bd: descriptor for the good/bad block search pattern
452 * @chip: create the table for a specific chip, -1 read all chips; applies only
453 * if NAND_BBT_PERCHIP option is set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700455 * Create a bad block table by scanning the device for the given good/bad block
456 * identify pattern.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200458static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
459 struct nand_bbt_descr *bd, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 struct nand_chip *this = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200462 int i, numblocks, len, scanlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 int startblock;
464 loff_t from;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200465 size_t readlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Brian Norris9a4d4d62011-07-19 10:06:07 -0700467 pr_info("Scanning device for bad blocks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 if (bd->options & NAND_BBT_SCANALLPAGES)
470 len = 1 << (this->bbt_erase_shift - this->page_shift);
Brian Norris58373ff2010-07-15 12:15:44 -0700471 else if (bd->options & NAND_BBT_SCAN2NDPAGE)
472 len = 2;
473 else
474 len = 1;
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000475
476 if (!(bd->options & NAND_BBT_SCANEMPTY)) {
477 /* We need only read few bytes from the OOB area */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200478 scanlen = 0;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000479 readlen = bd->len;
480 } else {
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000481 /* Full page content should be read */
Joern Engel28318772006-05-22 23:18:05 +0200482 scanlen = mtd->writesize + mtd->oobsize;
483 readlen = len * mtd->writesize;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 if (chip == -1) {
Brian Norris8b6e50c2011-05-25 14:59:01 -0700487 /*
488 * Note that numblocks is 2 * (real numblocks) here, see i+=2
489 * below as it makes shifting and masking less painful
490 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 numblocks = mtd->size >> (this->bbt_erase_shift - 1);
492 startblock = 0;
493 from = 0;
494 } else {
495 if (chip >= this->numchips) {
Brian Norris9a4d4d62011-07-19 10:06:07 -0700496 pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
David Woodhousee0c7d762006-05-13 18:07:53 +0100497 chip + 1, this->numchips);
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000498 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500 numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
501 startblock = chip * numblocks;
502 numblocks += startblock;
Adrian Hunter69423d92008-12-10 13:37:21 +0000503 from = (loff_t)startblock << (this->bbt_erase_shift - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000505
Brian Norris5fb15492011-05-31 16:31:21 -0700506 if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700507 from += mtd->erasesize - (mtd->writesize * len);
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 for (i = startblock; i < numblocks;) {
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000510 int ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000511
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200512 BUG_ON(bd->options & NAND_BBT_NO_OOB);
513
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200514 if (bd->options & NAND_BBT_SCANALLPAGES)
515 ret = scan_block_full(mtd, bd, from, buf, readlen,
516 scanlen, len);
517 else
518 ret = scan_block_fast(mtd, bd, from, buf, len);
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000519
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200520 if (ret < 0)
521 return ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000522
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200523 if (ret) {
524 this->bbt[i >> 3] |= 0x03 << (i & 0x6);
Brian Norris9a4d4d62011-07-19 10:06:07 -0700525 pr_warn("Bad eraseblock %d at 0x%012llx\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700526 i >> 1, (unsigned long long)from);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200527 mtd->ecc_stats.badblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 i += 2;
531 from += (1 << this->bbt_erase_shift);
532 }
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536/**
537 * search_bbt - [GENERIC] scan the device for a specific bad block table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700538 * @mtd: MTD device structure
539 * @buf: temporary buffer
540 * @td: descriptor for the bad block table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700542 * Read the bad block table by searching for a given ident pattern. Search is
543 * preformed either from the beginning up or from the end of the device
544 * downwards. The search starts always at the start of a block. If the option
545 * NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains
546 * the bad block information of this chip. This is necessary to provide support
547 * for certain DOC devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700549 * The bbt ident pattern resides in the oob area of the first page in a block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100551static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 struct nand_chip *this = mtd->priv;
554 int i, chips;
555 int bits, startblock, block, dir;
Joern Engel28318772006-05-22 23:18:05 +0200556 int scanlen = mtd->writesize + mtd->oobsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 int bbtblocks;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200558 int blocktopage = this->bbt_erase_shift - this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Brian Norris8b6e50c2011-05-25 14:59:01 -0700560 /* Search direction top -> down? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (td->options & NAND_BBT_LASTBLOCK) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100562 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 dir = -1;
564 } else {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000565 startblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000567 }
568
Brian Norris8b6e50c2011-05-25 14:59:01 -0700569 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (td->options & NAND_BBT_PERCHIP) {
571 chips = this->numchips;
572 bbtblocks = this->chipsize >> this->bbt_erase_shift;
573 startblock &= bbtblocks - 1;
574 } else {
575 chips = 1;
576 bbtblocks = mtd->size >> this->bbt_erase_shift;
577 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* Number of bits for each erase block in the bbt */
580 bits = td->options & NAND_BBT_NRBITS_MSK;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 for (i = 0; i < chips; i++) {
583 /* Reset version information */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000584 td->version[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 td->pages[i] = -1;
586 /* Scan the maximum number of blocks */
587 for (block = 0; block < td->maxblocks; block++) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 int actblock = startblock + dir * block;
Adrian Hunter69423d92008-12-10 13:37:21 +0000590 loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 /* Read first page */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200593 scan_read_raw(mtd, buf, offs, mtd->writesize, td);
Joern Engel28318772006-05-22 23:18:05 +0200594 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200595 td->pages[i] = actblock << blocktopage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (td->options & NAND_BBT_VERSION) {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200597 offs = bbt_get_ver_offs(mtd, td);
598 td->version[i] = buf[offs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600 break;
601 }
602 }
603 startblock += this->chipsize >> this->bbt_erase_shift;
604 }
605 /* Check, if we found a bbt for each requested chip */
606 for (i = 0; i < chips; i++) {
607 if (td->pages[i] == -1)
Brian Norris9a4d4d62011-07-19 10:06:07 -0700608 pr_warn("Bad block table not found for chip %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 else
Brian Norrisd0370212011-07-19 10:06:08 -0700610 pr_info("Bad block table found at page %d, version "
611 "0x%02X\n", td->pages[i], td->version[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000613 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616/**
617 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -0700618 * @mtd: MTD device structure
619 * @buf: temporary buffer
620 * @td: descriptor for the bad block table
621 * @md: descriptor for the bad block table mirror
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700623 * Search and read the bad block table(s).
624 */
Brian Norris7b5a2d42012-06-22 16:35:41 -0700625static void search_read_bbts(struct mtd_info *mtd, uint8_t *buf,
626 struct nand_bbt_descr *td,
627 struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 /* Search the primary table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100630 search_bbt(mtd, buf, td);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /* Search the mirror table */
633 if (md)
David Woodhousee0c7d762006-05-13 18:07:53 +0100634 search_bbt(mtd, buf, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000635}
636
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000637/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 * write_bbt - [GENERIC] (Re)write the bad block table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700639 * @mtd: MTD device structure
640 * @buf: temporary buffer
641 * @td: descriptor for the bad block table
642 * @md: descriptor for the bad block table mirror
643 * @chipsel: selector for a specific chip, -1 for all
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700645 * (Re)write the bad block table.
646 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100647static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
Thomas Gleixner9223a452006-05-23 17:21:03 +0200648 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
649 int chipsel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 struct nand_chip *this = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 struct erase_info einfo;
653 int i, j, res, chip = 0;
654 int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200655 int nrchips, bbtoffs, pageoffs, ooboffs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 uint8_t msk[4];
657 uint8_t rcode = td->reserved_block_code;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200658 size_t retlen, len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 loff_t to;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200660 struct mtd_oob_ops ops;
661
662 ops.ooblen = mtd->oobsize;
663 ops.ooboffs = 0;
664 ops.datbuf = NULL;
Brian Norris0612b9d2011-08-30 18:45:40 -0700665 ops.mode = MTD_OPS_PLACE_OOB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 if (!rcode)
668 rcode = 0xff;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700669 /* Write bad block table per chip rather than per device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (td->options & NAND_BBT_PERCHIP) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100671 numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700672 /* Full device write or specific chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (chipsel == -1) {
674 nrchips = this->numchips;
675 } else {
676 nrchips = chipsel + 1;
677 chip = chipsel;
678 }
679 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +0100680 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 nrchips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000682 }
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 /* Loop through the chips */
685 for (; chip < nrchips; chip++) {
Brian Norris8b6e50c2011-05-25 14:59:01 -0700686 /*
687 * There was already a version of the table, reuse the page
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000688 * This applies for absolute placement too, as we have the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 * page nr. in td->pages.
690 */
691 if (td->pages[chip] != -1) {
692 page = td->pages[chip];
693 goto write;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Brian Norris8b6e50c2011-05-25 14:59:01 -0700696 /*
697 * Automatic placement of the bad block table. Search direction
698 * top -> down?
699 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (td->options & NAND_BBT_LASTBLOCK) {
701 startblock = numblocks * (chip + 1) - 1;
702 dir = -1;
703 } else {
704 startblock = chip * numblocks;
705 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 for (i = 0; i < td->maxblocks; i++) {
709 int block = startblock + dir * i;
710 /* Check, if the block is bad */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200711 switch ((this->bbt[block >> 2] >>
712 (2 * (block & 0x03))) & 0x03) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 case 0x01:
714 case 0x03:
715 continue;
716 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200717 page = block <<
718 (this->bbt_erase_shift - this->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /* Check, if the block is used by the mirror table */
720 if (!md || md->pages[chip] != page)
721 goto write;
722 }
Brian Norris9a4d4d62011-07-19 10:06:07 -0700723 pr_err("No space left to write bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return -ENOSPC;
David Woodhousee0c7d762006-05-13 18:07:53 +0100725 write:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 /* Set up shift count and masks for the flash table */
728 bits = td->options & NAND_BBT_NRBITS_MSK;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200729 msk[2] = ~rcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 switch (bits) {
Thomas Gleixner9223a452006-05-23 17:21:03 +0200731 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
732 msk[3] = 0x01;
733 break;
734 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
735 msk[3] = 0x03;
736 break;
737 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
738 msk[3] = 0x0f;
739 break;
740 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
741 msk[3] = 0xff;
742 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 default: return -EINVAL;
744 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 bbtoffs = chip * (numblocks >> 2);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000747
Brian Norris596d7452011-09-07 13:13:33 -0700748 to = ((loff_t)page) << this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Brian Norris8b6e50c2011-05-25 14:59:01 -0700750 /* Must we save the block contents? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (td->options & NAND_BBT_SAVECONTENT) {
752 /* Make it block aligned */
Brian Norris596d7452011-09-07 13:13:33 -0700753 to &= ~((loff_t)((1 << this->bbt_erase_shift) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 len = 1 << this->bbt_erase_shift;
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200755 res = mtd_read(mtd, to, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (res < 0) {
757 if (retlen != len) {
Brian Norrisd0370212011-07-19 10:06:08 -0700758 pr_info("nand_bbt: error reading block "
759 "for writing the bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return res;
761 }
Brian Norrisd0370212011-07-19 10:06:08 -0700762 pr_warn("nand_bbt: ECC error while reading "
763 "block for writing bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200765 /* Read oob data */
Vitaly Wool70145682006-11-03 18:20:38 +0300766 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200767 ops.oobbuf = &buf[len];
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200768 res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
Vitaly Wool70145682006-11-03 18:20:38 +0300769 if (res < 0 || ops.oobretlen != ops.ooblen)
Thomas Gleixner9223a452006-05-23 17:21:03 +0200770 goto outerr;
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /* Calc the byte offset in the buffer */
773 pageoffs = page - (int)(to >> this->page_shift);
774 offs = pageoffs << this->page_shift;
775 /* Preset the bbt area with 0xff */
Brian Norris596d7452011-09-07 13:13:33 -0700776 memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
Thomas Gleixner9223a452006-05-23 17:21:03 +0200777 ooboffs = len + (pageoffs * mtd->oobsize);
778
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200779 } else if (td->options & NAND_BBT_NO_OOB) {
780 ooboffs = 0;
781 offs = td->len;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700782 /* The version byte */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200783 if (td->options & NAND_BBT_VERSION)
784 offs++;
785 /* Calc length */
Brian Norris596d7452011-09-07 13:13:33 -0700786 len = (size_t)(numblocks >> sft);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200787 len += offs;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700788 /* Make it page aligned! */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200789 len = ALIGN(len, mtd->writesize);
790 /* Preset the buffer with 0xff */
791 memset(buf, 0xff, len);
792 /* Pattern is located at the begin of first page */
793 memcpy(buf, td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 } else {
795 /* Calc length */
Brian Norris596d7452011-09-07 13:13:33 -0700796 len = (size_t)(numblocks >> sft);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700797 /* Make it page aligned! */
Sebastian Andrzej Siewiorcda32092010-09-29 19:43:50 +0200798 len = ALIGN(len, mtd->writesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 /* Preset the buffer with 0xff */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200800 memset(buf, 0xff, len +
801 (len >> this->page_shift)* mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 offs = 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200803 ooboffs = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /* Pattern is located in oob area of first page */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200805 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000807
Thomas Gleixner9223a452006-05-23 17:21:03 +0200808 if (td->options & NAND_BBT_VERSION)
809 buf[ooboffs + td->veroffs] = td->version[chip];
810
Brian Norris8b6e50c2011-05-25 14:59:01 -0700811 /* Walk through the memory table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100812 for (i = 0; i < numblocks;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 uint8_t dat;
814 dat = this->bbt[bbtoffs + (i >> 2)];
David Woodhousee0c7d762006-05-13 18:07:53 +0100815 for (j = 0; j < 4; j++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 int sftcnt = (i << (3 - sft)) & sftmsk;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700817 /* Do not store the reserved bbt blocks! */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200818 buf[offs + (i >> sft)] &=
819 ~(msk[dat & 0x03] << sftcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 dat >>= 2;
821 }
822 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000823
David Woodhousee0c7d762006-05-13 18:07:53 +0100824 memset(&einfo, 0, sizeof(einfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 einfo.mtd = mtd;
Adrian Hunter69423d92008-12-10 13:37:21 +0000826 einfo.addr = to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 einfo.len = 1 << this->bbt_erase_shift;
David Woodhousee0c7d762006-05-13 18:07:53 +0100828 res = nand_erase_nand(mtd, &einfo, 1);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200829 if (res < 0)
830 goto outerr;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000831
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200832 res = scan_write_bbt(mtd, to, len, buf,
833 td->options & NAND_BBT_NO_OOB ? NULL :
834 &buf[len]);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200835 if (res < 0)
836 goto outerr;
837
Brian Norrisd0370212011-07-19 10:06:08 -0700838 pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
839 (unsigned long long)to, td->version[chip]);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 /* Mark it as used */
842 td->pages[chip] = page;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200845
846 outerr:
Brian Norrisd0370212011-07-19 10:06:08 -0700847 pr_warn("nand_bbt: error while writing bad block table %d\n", res);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200848 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
851/**
852 * nand_memory_bbt - [GENERIC] create a memory based bad block table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700853 * @mtd: MTD device structure
854 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700856 * The function creates a memory based bbt by scanning the device for
857 * manufacturer / software marked good / bad blocks.
858 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100859static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 struct nand_chip *this = mtd->priv;
862
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000863 bd->options &= ~NAND_BBT_SCANEMPTY;
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100864 return create_bbt(mtd, this->buffers->databuf, bd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
867/**
David Woodhousee0c7d762006-05-13 18:07:53 +0100868 * check_create - [GENERIC] create and write bbt(s) if necessary
Brian Norris8b6e50c2011-05-25 14:59:01 -0700869 * @mtd: MTD device structure
870 * @buf: temporary buffer
871 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700873 * The function checks the results of the previous call to read_bbt and creates
874 * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found
875 * for the chip/device. Update is necessary if one of the tables is missing or
876 * the version nr. of one table is less than the other.
877 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100878static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Brian Norris623978d2011-09-20 18:35:34 -0700880 int i, chips, writeops, create, chipsel, res, res2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 struct nand_chip *this = mtd->priv;
882 struct nand_bbt_descr *td = this->bbt_td;
883 struct nand_bbt_descr *md = this->bbt_md;
884 struct nand_bbt_descr *rd, *rd2;
885
Brian Norris8b6e50c2011-05-25 14:59:01 -0700886 /* Do we have a bbt per chip? */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000887 if (td->options & NAND_BBT_PERCHIP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 chips = this->numchips;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000889 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 chips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 for (i = 0; i < chips; i++) {
893 writeops = 0;
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700894 create = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 rd = NULL;
896 rd2 = NULL;
Brian Norris623978d2011-09-20 18:35:34 -0700897 res = res2 = 0;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700898 /* Per chip or per device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700900 /* Mirrored table available? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (md) {
902 if (td->pages[i] == -1 && md->pages[i] == -1) {
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700903 create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 writeops = 0x03;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700905 } else if (td->pages[i] == -1) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000906 rd = md;
Brian Norris596d7452011-09-07 13:13:33 -0700907 writeops = 0x01;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700908 } else if (md->pages[i] == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 rd = td;
Brian Norris596d7452011-09-07 13:13:33 -0700910 writeops = 0x02;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700911 } else if (td->version[i] == md->version[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 rd = td;
913 if (!(td->options & NAND_BBT_VERSION))
914 rd2 = md;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700915 } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 rd = td;
Brian Norris596d7452011-09-07 13:13:33 -0700917 writeops = 0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 } else {
919 rd = md;
Brian Norris596d7452011-09-07 13:13:33 -0700920 writeops = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 } else {
923 if (td->pages[i] == -1) {
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700924 create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 writeops = 0x01;
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700926 } else {
927 rd = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000930
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700931 if (create) {
932 /* Create the bad block table by scanning the device? */
933 if (!(td->options & NAND_BBT_CREATE))
934 continue;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000935
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700936 /* Create the table in memory by scanning the chip(s) */
937 if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
938 create_bbt(mtd, buf, bd, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700940 td->version[i] = 1;
941 if (md)
942 md->version[i] = 1;
943 }
944
Brian Norris8b6e50c2011-05-25 14:59:01 -0700945 /* Read back first? */
Brian Norris623978d2011-09-20 18:35:34 -0700946 if (rd) {
947 res = read_abs_bbt(mtd, buf, rd, chipsel);
948 if (mtd_is_eccerr(res)) {
949 /* Mark table as invalid */
950 rd->pages[i] = -1;
Brian Norrisdadc17a2011-09-20 18:35:57 -0700951 rd->version[i] = 0;
Brian Norris623978d2011-09-20 18:35:34 -0700952 i--;
953 continue;
954 }
955 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700956 /* If they weren't versioned, read both */
Brian Norris623978d2011-09-20 18:35:34 -0700957 if (rd2) {
958 res2 = read_abs_bbt(mtd, buf, rd2, chipsel);
959 if (mtd_is_eccerr(res2)) {
960 /* Mark table as invalid */
961 rd2->pages[i] = -1;
Brian Norrisdadc17a2011-09-20 18:35:57 -0700962 rd2->version[i] = 0;
Brian Norris623978d2011-09-20 18:35:34 -0700963 i--;
964 continue;
965 }
966 }
967
968 /* Scrub the flash table(s)? */
969 if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
970 writeops = 0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Brian Norrisdadc17a2011-09-20 18:35:57 -0700972 /* Update version numbers before writing */
973 if (md) {
974 td->version[i] = max(td->version[i], md->version[i]);
975 md->version[i] = td->version[i];
976 }
977
Brian Norris8b6e50c2011-05-25 14:59:01 -0700978 /* Write the bad block table to the device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100980 res = write_bbt(mtd, buf, td, md, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (res < 0)
982 return res;
983 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000984
Brian Norris8b6e50c2011-05-25 14:59:01 -0700985 /* Write the mirror bad block table to the device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100987 res = write_bbt(mtd, buf, md, td, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (res < 0)
989 return res;
990 }
991 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000992 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
995/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000996 * mark_bbt_regions - [GENERIC] mark the bad block table regions
Brian Norris8b6e50c2011-05-25 14:59:01 -0700997 * @mtd: MTD device structure
998 * @td: bad block table descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001000 * The bad block table regions are marked as "bad" to prevent accidental
1001 * erasures / writes. The regions are identified by the mark 0x02.
1002 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001003static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
1005 struct nand_chip *this = mtd->priv;
1006 int i, j, chips, block, nrblocks, update;
1007 uint8_t oldval, newval;
1008
Brian Norris8b6e50c2011-05-25 14:59:01 -07001009 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (td->options & NAND_BBT_PERCHIP) {
1011 chips = this->numchips;
1012 nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
1013 } else {
1014 chips = 1;
1015 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001016 }
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 for (i = 0; i < chips; i++) {
1019 if ((td->options & NAND_BBT_ABSPAGE) ||
1020 !(td->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001021 if (td->pages[i] == -1)
1022 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001024 block <<= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 oldval = this->bbt[(block >> 3)];
1026 newval = oldval | (0x2 << (block & 0x06));
1027 this->bbt[(block >> 3)] = newval;
1028 if ((oldval != newval) && td->reserved_block_code)
Adrian Hunter69423d92008-12-10 13:37:21 +00001029 nand_update_bbt(mtd, (loff_t)block << (this->bbt_erase_shift - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 continue;
1031 }
1032 update = 0;
1033 if (td->options & NAND_BBT_LASTBLOCK)
1034 block = ((i + 1) * nrblocks) - td->maxblocks;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001035 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 block = i * nrblocks;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001037 block <<= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 for (j = 0; j < td->maxblocks; j++) {
1039 oldval = this->bbt[(block >> 3)];
1040 newval = oldval | (0x2 << (block & 0x06));
1041 this->bbt[(block >> 3)] = newval;
David Woodhousee0c7d762006-05-13 18:07:53 +01001042 if (oldval != newval)
1043 update = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 block += 2;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001045 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07001046 /*
1047 * If we want reserved blocks to be recorded to flash, and some
1048 * new ones have been marked, then we need to update the stored
1049 * bbts. This should only happen once.
1050 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (update && td->reserved_block_code)
Adrian Hunter69423d92008-12-10 13:37:21 +00001052 nand_update_bbt(mtd, (loff_t)(block - 2) << (this->bbt_erase_shift - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054}
1055
1056/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001057 * verify_bbt_descr - verify the bad block description
Brian Norris8b6e50c2011-05-25 14:59:01 -07001058 * @mtd: MTD device structure
1059 * @bd: the table to verify
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001060 *
1061 * This functions performs a few sanity checks on the bad block description
1062 * table.
1063 */
1064static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
1065{
1066 struct nand_chip *this = mtd->priv;
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001067 u32 pattern_len;
1068 u32 bits;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001069 u32 table_size;
1070
1071 if (!bd)
1072 return;
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001073
1074 pattern_len = bd->len;
1075 bits = bd->options & NAND_BBT_NRBITS_MSK;
1076
Brian Norrisa40f7342011-05-31 16:31:22 -07001077 BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001078 !(this->bbt_options & NAND_BBT_USE_FLASH));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001079 BUG_ON(!bits);
1080
1081 if (bd->options & NAND_BBT_VERSION)
1082 pattern_len++;
1083
1084 if (bd->options & NAND_BBT_NO_OOB) {
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001085 BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
Brian Norrisa40f7342011-05-31 16:31:22 -07001086 BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001087 BUG_ON(bd->offs);
1088 if (bd->options & NAND_BBT_VERSION)
1089 BUG_ON(bd->veroffs != bd->len);
1090 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1091 }
1092
1093 if (bd->options & NAND_BBT_PERCHIP)
1094 table_size = this->chipsize >> this->bbt_erase_shift;
1095 else
1096 table_size = mtd->size >> this->bbt_erase_shift;
1097 table_size >>= 3;
1098 table_size *= bits;
1099 if (bd->options & NAND_BBT_NO_OOB)
1100 table_size += pattern_len;
1101 BUG_ON(table_size > (1 << this->bbt_erase_shift));
1102}
1103
1104/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07001106 * @mtd: MTD device structure
1107 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001109 * The function checks, if a bad block table(s) is/are already available. If
1110 * not it scans the device for manufacturer marked good / bad blocks and writes
1111 * the bad block table(s) to the selected place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001113 * The bad block table memory is allocated here. It must be freed by calling
1114 * the nand_free_bbt function.
1115 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001116int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
1118 struct nand_chip *this = mtd->priv;
1119 int len, res = 0;
1120 uint8_t *buf;
1121 struct nand_bbt_descr *td = this->bbt_td;
1122 struct nand_bbt_descr *md = this->bbt_md;
1123
1124 len = mtd->size >> (this->bbt_erase_shift + 2);
Brian Norris8b6e50c2011-05-25 14:59:01 -07001125 /*
1126 * Allocate memory (2bit per block) and clear the memory bad block
1127 * table.
1128 */
Burman Yan95b93a02006-11-15 21:10:29 +02001129 this->bbt = kzalloc(len, GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001130 if (!this->bbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Brian Norris8b6e50c2011-05-25 14:59:01 -07001133 /*
1134 * If no primary table decriptor is given, scan the device to build a
1135 * memory based bad block table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 */
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001137 if (!td) {
1138 if ((res = nand_memory_bbt(mtd, bd))) {
Brian Norrisd0370212011-07-19 10:06:08 -07001139 pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
David Woodhousee0c7d762006-05-13 18:07:53 +01001140 kfree(this->bbt);
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001141 this->bbt = NULL;
1142 }
1143 return res;
1144 }
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001145 verify_bbt_descr(mtd, td);
1146 verify_bbt_descr(mtd, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 /* Allocate a temporary buffer for one eraseblock incl. oob */
1149 len = (1 << this->bbt_erase_shift);
1150 len += (len >> this->page_shift) * mtd->oobsize;
David Woodhousec3f8abf2006-05-13 04:03:42 +01001151 buf = vmalloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (!buf) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001153 kfree(this->bbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 this->bbt = NULL;
1155 return -ENOMEM;
1156 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001157
Brian Norris8b6e50c2011-05-25 14:59:01 -07001158 /* Is the bbt at a given page? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (td->options & NAND_BBT_ABSPAGE) {
Brian Norris7b5a2d42012-06-22 16:35:41 -07001160 read_abs_bbts(mtd, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001161 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 /* Search the bad block table using a pattern in oob */
Brian Norris7b5a2d42012-06-22 16:35:41 -07001163 search_read_bbts(mtd, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Brian Norris7b5a2d42012-06-22 16:35:41 -07001166 res = check_create(mtd, buf, bd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 /* Prevent the bbt regions from erasing / writing */
David Woodhousee0c7d762006-05-13 18:07:53 +01001169 mark_bbt_region(mtd, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (md)
David Woodhousee0c7d762006-05-13 18:07:53 +01001171 mark_bbt_region(mtd, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001172
David Woodhousee0c7d762006-05-13 18:07:53 +01001173 vfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return res;
1175}
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001178 * nand_update_bbt - [NAND Interface] update bad block table(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07001179 * @mtd: MTD device structure
1180 * @offs: the offset of the newly marked block
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001182 * The function updates the bad block table(s).
1183 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001184int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
1186 struct nand_chip *this = mtd->priv;
Brian Norris1196ac52011-09-07 13:13:32 -07001187 int len, res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 int chip, chipsel;
1189 uint8_t *buf;
1190 struct nand_bbt_descr *td = this->bbt_td;
1191 struct nand_bbt_descr *md = this->bbt_md;
1192
1193 if (!this->bbt || !td)
1194 return -EINVAL;
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 /* Allocate a temporary buffer for one eraseblock incl. oob */
1197 len = (1 << this->bbt_erase_shift);
1198 len += (len >> this->page_shift) * mtd->oobsize;
David Woodhousee0c7d762006-05-13 18:07:53 +01001199 buf = kmalloc(len, GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001200 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return -ENOMEM;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001202
Brian Norris8b6e50c2011-05-25 14:59:01 -07001203 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (td->options & NAND_BBT_PERCHIP) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001205 chip = (int)(offs >> this->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 chipsel = chip;
1207 } else {
1208 chip = 0;
1209 chipsel = -1;
1210 }
1211
1212 td->version[chip]++;
1213 if (md)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001214 md->version[chip]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Brian Norris8b6e50c2011-05-25 14:59:01 -07001216 /* Write the bad block table to the device? */
Brian Norris1196ac52011-09-07 13:13:32 -07001217 if (td->options & NAND_BBT_WRITE) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001218 res = write_bbt(mtd, buf, td, md, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (res < 0)
1220 goto out;
1221 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07001222 /* Write the mirror bad block table to the device? */
Brian Norris1196ac52011-09-07 13:13:32 -07001223 if (md && (md->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001224 res = write_bbt(mtd, buf, md, td, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
1226
David Woodhousee0c7d762006-05-13 18:07:53 +01001227 out:
1228 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return res;
1230}
1231
Brian Norris8b6e50c2011-05-25 14:59:01 -07001232/*
1233 * Define some generic bad / good block scan pattern which are used
1234 * while scanning a device for factory marked good / bad blocks.
1235 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
1239
1240static struct nand_bbt_descr agand_flashbased = {
1241 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
1242 .offs = 0x20,
1243 .len = 6,
1244 .pattern = scan_agand_pattern
1245};
1246
Brian Norris7854d3f2011-06-23 14:12:08 -07001247/* Generic flash bbt descriptors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1249static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1250
1251static struct nand_bbt_descr bbt_main_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001252 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1254 .offs = 8,
1255 .len = 4,
1256 .veroffs = 12,
1257 .maxblocks = 4,
1258 .pattern = bbt_pattern
1259};
1260
1261static struct nand_bbt_descr bbt_mirror_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001262 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1264 .offs = 8,
1265 .len = 4,
1266 .veroffs = 12,
1267 .maxblocks = 4,
1268 .pattern = mirror_pattern
1269};
1270
Brian Norris9fd6b372012-06-22 16:35:40 -07001271static struct nand_bbt_descr bbt_main_no_oob_descr = {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001272 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1273 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1274 | NAND_BBT_NO_OOB,
1275 .len = 4,
1276 .veroffs = 4,
1277 .maxblocks = 4,
1278 .pattern = bbt_pattern
1279};
1280
Brian Norris9fd6b372012-06-22 16:35:40 -07001281static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001282 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1283 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1284 | NAND_BBT_NO_OOB,
1285 .len = 4,
1286 .veroffs = 4,
1287 .maxblocks = 4,
1288 .pattern = mirror_pattern
1289};
1290
Brian Norris752ed6c2011-09-20 18:36:42 -07001291#define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
Brian Norris58373ff2010-07-15 12:15:44 -07001292/**
Brian Norris752ed6c2011-09-20 18:36:42 -07001293 * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001294 * @this: NAND chip to create descriptor for
Brian Norris58373ff2010-07-15 12:15:44 -07001295 *
1296 * This function allocates and initializes a nand_bbt_descr for BBM detection
Brian Norris752ed6c2011-09-20 18:36:42 -07001297 * based on the properties of @this. The new descriptor is stored in
Brian Norris58373ff2010-07-15 12:15:44 -07001298 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1299 * passed to this function.
Brian Norris58373ff2010-07-15 12:15:44 -07001300 */
Brian Norris752ed6c2011-09-20 18:36:42 -07001301static int nand_create_badblock_pattern(struct nand_chip *this)
Brian Norris58373ff2010-07-15 12:15:44 -07001302{
1303 struct nand_bbt_descr *bd;
1304 if (this->badblock_pattern) {
Brian Norris752ed6c2011-09-20 18:36:42 -07001305 pr_warn("Bad block pattern already allocated; not replacing\n");
Brian Norris58373ff2010-07-15 12:15:44 -07001306 return -EINVAL;
1307 }
1308 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001309 if (!bd)
Brian Norris58373ff2010-07-15 12:15:44 -07001310 return -ENOMEM;
Brian Norris752ed6c2011-09-20 18:36:42 -07001311 bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
Brian Norris58373ff2010-07-15 12:15:44 -07001312 bd->offs = this->badblockpos;
1313 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1314 bd->pattern = scan_ff_pattern;
1315 bd->options |= NAND_BBT_DYNAMICSTRUCT;
1316 this->badblock_pattern = bd;
1317 return 0;
1318}
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001321 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
Brian Norris8b6e50c2011-05-25 14:59:01 -07001322 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001324 * This function selects the default bad block table support for the device and
1325 * calls the nand_scan_bbt function.
1326 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001327int nand_default_bbt(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 struct nand_chip *this = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001330
Brian Norris8b6e50c2011-05-25 14:59:01 -07001331 /*
1332 * Default for AG-AND. We must use a flash based bad block table as the
1333 * devices have factory marked _good_ blocks. Erasing those blocks
1334 * leads to loss of the good / bad information, so we _must_ store this
1335 * information in a good / bad table during startup.
David Woodhousee0c7d762006-05-13 18:07:53 +01001336 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (this->options & NAND_IS_AND) {
1338 /* Use the default pattern descriptors */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001339 if (!this->bbt_td) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 this->bbt_td = &bbt_main_descr;
1341 this->bbt_md = &bbt_mirror_descr;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001342 }
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001343 this->bbt_options |= NAND_BBT_USE_FLASH;
David Woodhousee0c7d762006-05-13 18:07:53 +01001344 return nand_scan_bbt(mtd, &agand_flashbased);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001346
Brian Norris8b6e50c2011-05-25 14:59:01 -07001347 /* Is a flash based bad block table requested? */
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001348 if (this->bbt_options & NAND_BBT_USE_FLASH) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001349 /* Use the default pattern descriptors */
1350 if (!this->bbt_td) {
Brian Norrisa40f7342011-05-31 16:31:22 -07001351 if (this->bbt_options & NAND_BBT_NO_OOB) {
Brian Norris9fd6b372012-06-22 16:35:40 -07001352 this->bbt_td = &bbt_main_no_oob_descr;
1353 this->bbt_md = &bbt_mirror_no_oob_descr;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001354 } else {
1355 this->bbt_td = &bbt_main_descr;
1356 this->bbt_md = &bbt_mirror_descr;
1357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 } else {
1360 this->bbt_td = NULL;
1361 this->bbt_md = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
Brian Norrisa2f812d2011-03-18 21:53:42 -07001363
1364 if (!this->badblock_pattern)
Brian Norris752ed6c2011-09-20 18:36:42 -07001365 nand_create_badblock_pattern(this);
Brian Norrisa2f812d2011-03-18 21:53:42 -07001366
David Woodhousee0c7d762006-05-13 18:07:53 +01001367 return nand_scan_bbt(mtd, this->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
1370/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001371 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07001372 * @mtd: MTD device structure
1373 * @offs: offset in the device
1374 * @allowbbt: allow access to bad block table region
1375 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001376int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 struct nand_chip *this = mtd->priv;
1379 int block;
David Woodhousee0c7d762006-05-13 18:07:53 +01001380 uint8_t res;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 /* Get block number * 2 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001383 block = (int)(offs >> (this->bbt_erase_shift - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
1385
Brian Norris289c0522011-07-19 10:06:09 -07001386 pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: "
1387 "(block %d) 0x%02x\n",
1388 (unsigned int)offs, block >> 1, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 switch ((int)res) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001391 case 0x00:
1392 return 0;
1393 case 0x01:
1394 return 1;
1395 case 0x02:
1396 return allowbbt ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398 return 1;
1399}
1400
David Woodhousee0c7d762006-05-13 18:07:53 +01001401EXPORT_SYMBOL(nand_scan_bbt);
1402EXPORT_SYMBOL(nand_default_bbt);