blob: 584bdcb3c61af981e6a081a97adfc4a8b106a357 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/mtd/nand_bbt.c
3 *
4 * Overview:
5 * Bad block table support for the NAND driver
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * Description:
14 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000015 * When nand_scan_bbt is called, then it tries to find the bad block table
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020016 * depending on the options in the BBT descriptor(s). If no flash based BBT
Brian Norrisbb9ebd42011-05-31 16:31:23 -070017 * (NAND_BBT_USE_FLASH) is specified then the device is scanned for factory
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020018 * marked good / bad blocks. This information is used to create a memory BBT.
19 * Once a new bad block is discovered then the "factory" information is updated
20 * on the device.
21 * If a flash based BBT is specified then the function first tries to find the
22 * BBT on flash. If a BBT is found then the contents are read and the memory
23 * based BBT is created. If a mirrored BBT is selected then the mirror is
24 * searched too and the versions are compared. If the mirror has a greater
25 * version number than the mirror BBT is used to build the memory based BBT.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * If the tables are not versioned, then we "or" the bad block information.
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020027 * If one of the BBTs is out of date or does not exist it is (re)created.
28 * If no BBT exists at all then the device is scanned for factory marked
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000029 * good / bad blocks and the bad block tables are created.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020031 * For manufacturer created BBTs like the one found on M-SYS DOC devices
32 * the BBT is searched and read but never created
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 *
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020034 * The auto generated bad block table is located in the last good blocks
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000035 * of the device. The table is mirrored, so it can be updated eventually.
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020036 * The table is marked in the OOB area with an ident pattern and a version
37 * number which indicates which of both tables is more up to date. If the NAND
38 * controller needs the complete OOB area for the ECC information then the
Brian Norrisbb9ebd42011-05-31 16:31:23 -070039 * option NAND_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of
Brian Norrisa40f7342011-05-31 16:31:22 -070040 * course): it moves the ident pattern and the version byte into the data area
41 * and the OOB area will remain untouched.
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 *
43 * The table uses 2 bits per block
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020044 * 11b: block is good
45 * 00b: block is factory marked bad
46 * 01b, 10b: block is marked bad due to wear
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 *
48 * The memory bad block table uses the following scheme:
49 * 00b: block is good
50 * 01b: block is marked bad due to wear
51 * 10b: block is reserved (to protect the bbt area)
52 * 11b: block is factory marked bad
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000053 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * Multichip devices like DOC store the bad block info per floor.
55 *
56 * Following assumptions are made:
57 * - bbts start at a page boundary, if autolocated on a block boundary
David Woodhousee0c7d762006-05-13 18:07:53 +010058 * - the space necessary for a bbt in FLASH does not exceed a block boundary
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000059 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 */
61
62#include <linux/slab.h>
63#include <linux/types.h>
64#include <linux/mtd/mtd.h>
65#include <linux/mtd/nand.h>
66#include <linux/mtd/nand_ecc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/bitops.h>
68#include <linux/delay.h>
David Woodhousec3f8abf2006-05-13 04:03:42 +010069#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020071static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
72{
73 int ret;
74
75 ret = memcmp(buf, td->pattern, td->len);
76 if (!ret)
77 return ret;
78 return -1;
79}
80
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000081/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * check_pattern - [GENERIC] check if a pattern is in the buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -070083 * @buf: the buffer to search
84 * @len: the length of buffer to search
85 * @paglen: the pagelength
86 * @td: search pattern descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 *
Brian Norris8b6e50c2011-05-25 14:59:01 -070088 * Check for a pattern at the given place. Used to search bad block tables and
89 * good / bad block identifiers. If the SCAN_EMPTY option is set then check, if
90 * all bytes except the pattern area contain 0xff.
91 */
David Woodhousee0c7d762006-05-13 18:07:53 +010092static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +000094 int i, end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 uint8_t *p = buf;
96
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020097 if (td->options & NAND_BBT_NO_OOB)
98 return check_pattern_no_oob(buf, td);
99
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100100 end = paglen + td->offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (td->options & NAND_BBT_SCANEMPTY) {
102 for (i = 0; i < end; i++) {
103 if (p[i] != 0xff)
104 return -1;
105 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000106 }
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100107 p += end;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 /* Compare the pattern */
110 for (i = 0; i < td->len; i++) {
111 if (p[i] != td->pattern[i])
112 return -1;
113 }
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (td->options & NAND_BBT_SCANEMPTY) {
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000116 p += td->len;
117 end += td->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 for (i = end; i < len; i++) {
119 if (*p++ != 0xff)
120 return -1;
121 }
122 }
123 return 0;
124}
125
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000126/**
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100127 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700128 * @buf: the buffer to search
129 * @td: search pattern descriptor
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100130 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700131 * Check for a pattern at the given place. Used to search bad block tables and
132 * good / bad block identifiers. Same as check_pattern, but no optional empty
133 * check.
134 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100135static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100136{
137 int i;
138 uint8_t *p = buf;
139
140 /* Compare the pattern */
141 for (i = 0; i < td->len; i++) {
Thomas Gleixner19870da2005-07-15 14:53:51 +0100142 if (p[td->offs + i] != td->pattern[i])
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100143 return -1;
144 }
145 return 0;
146}
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200149 * add_marker_len - compute the length of the marker in data area
Brian Norris8b6e50c2011-05-25 14:59:01 -0700150 * @td: BBT descriptor used for computation
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200151 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700152 * The length will be 0 if the marker is located in OOB area.
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200153 */
154static u32 add_marker_len(struct nand_bbt_descr *td)
155{
156 u32 len;
157
158 if (!(td->options & NAND_BBT_NO_OOB))
159 return 0;
160
161 len = td->len;
162 if (td->options & NAND_BBT_VERSION)
163 len++;
164 return len;
165}
166
167/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * read_bbt - [GENERIC] Read the bad block table starting from page
Brian Norris8b6e50c2011-05-25 14:59:01 -0700169 * @mtd: MTD device structure
170 * @buf: temporary buffer
171 * @page: the starting page
172 * @num: the number of bbt descriptors to read
Brian Norris7854d3f2011-06-23 14:12:08 -0700173 * @td: the bbt describtion table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700174 * @offs: offset in the memory table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 *
176 * Read the bad block table starting from page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100178static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200179 struct nand_bbt_descr *td, int offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 int res, i, j, act = 0;
182 struct nand_chip *this = mtd->priv;
183 size_t retlen, len, totlen;
184 loff_t from;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200185 int bits = td->options & NAND_BBT_NRBITS_MSK;
Brian Norris596d7452011-09-07 13:13:33 -0700186 uint8_t msk = (uint8_t)((1 << bits) - 1);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200187 u32 marker_len;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200188 int reserved_block_code = td->reserved_block_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 totlen = (num * bits) >> 3;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200191 marker_len = add_marker_len(td);
Brian Norris596d7452011-09-07 13:13:33 -0700192 from = ((loff_t)page) << this->page_shift;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 while (totlen) {
Brian Norris596d7452011-09-07 13:13:33 -0700195 len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200196 if (marker_len) {
197 /*
198 * In case the BBT marker is not in the OOB area it
199 * will be just in the first page.
200 */
201 len -= marker_len;
202 from += marker_len;
203 marker_len = 0;
204 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200205 res = mtd->read(mtd, from, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (res < 0) {
207 if (retlen != len) {
Brian Norrisd0370212011-07-19 10:06:08 -0700208 pr_info("nand_bbt: error reading bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return res;
210 }
Brian Norris9a4d4d62011-07-19 10:06:07 -0700211 pr_warn("nand_bbt: ECC error while reading bad block table\n");
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /* Analyse data */
215 for (i = 0; i < len; i++) {
216 uint8_t dat = buf[i];
217 for (j = 0; j < 8; j += bits, act += 2) {
218 uint8_t tmp = (dat >> j) & msk;
219 if (tmp == msk)
220 continue;
David Woodhousee0c7d762006-05-13 18:07:53 +0100221 if (reserved_block_code && (tmp == reserved_block_code)) {
Brian Norrisd0370212011-07-19 10:06:08 -0700222 pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
223 (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200225 mtd->ecc_stats.bbtblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 continue;
227 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700228 /*
229 * Leave it for now, if it's matured we can
Brian Norrisa0f50802011-07-19 10:06:06 -0700230 * move this message to pr_debug.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700231 */
Brian Norrisd0370212011-07-19 10:06:08 -0700232 pr_info("nand_read_bbt: bad block at 0x%012llx\n",
233 (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700234 /* Factory marked bad or worn out? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (tmp == 0)
236 this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
237 else
238 this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200239 mtd->ecc_stats.badblocks++;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242 totlen -= len;
243 from += len;
244 }
245 return 0;
246}
247
248/**
249 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
Brian Norris8b6e50c2011-05-25 14:59:01 -0700250 * @mtd: MTD device structure
251 * @buf: temporary buffer
252 * @td: descriptor for the bad block table
Brian Norris596d7452011-09-07 13:13:33 -0700253 * @chip: read the table for a specific chip, -1 read all chips; applies only if
Brian Norris8b6e50c2011-05-25 14:59:01 -0700254 * NAND_BBT_PERCHIP option is set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700256 * Read the bad block table for all chips starting at a given page. We assume
257 * that the bbt bits are in consecutive order.
258 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100259static 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 -0700260{
261 struct nand_chip *this = mtd->priv;
262 int res = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (td->options & NAND_BBT_PERCHIP) {
265 int offs = 0;
266 for (i = 0; i < this->numchips; i++) {
267 if (chip == -1 || chip == i)
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200268 res = read_bbt(mtd, buf, td->pages[i],
269 this->chipsize >> this->bbt_erase_shift,
270 td, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (res)
272 return res;
273 offs += this->chipsize >> (this->bbt_erase_shift + 2);
274 }
275 } else {
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200276 res = read_bbt(mtd, buf, td->pages[0],
277 mtd->size >> this->bbt_erase_shift, td, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (res)
279 return res;
280 }
281 return 0;
282}
283
Brian Norris8b6e50c2011-05-25 14:59:01 -0700284/* BBT marker is in the first page, no OOB */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200285static int scan_read_raw_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
286 struct nand_bbt_descr *td)
287{
288 size_t retlen;
289 size_t len;
290
291 len = td->len;
292 if (td->options & NAND_BBT_VERSION)
293 len++;
294
295 return mtd->read(mtd, offs, len, &retlen, buf);
296}
297
Brian Norris8b6e50c2011-05-25 14:59:01 -0700298/* Scan read raw data from flash */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200299static int scan_read_raw_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200300 size_t len)
301{
302 struct mtd_oob_ops ops;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200303 int res;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200304
Brian Norris0612b9d2011-08-30 18:45:40 -0700305 ops.mode = MTD_OPS_RAW;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200306 ops.ooboffs = 0;
307 ops.ooblen = mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200308
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200309 while (len > 0) {
Brian Norris105513c2011-09-07 13:13:28 -0700310 ops.datbuf = buf;
311 ops.len = min(len, (size_t)mtd->writesize);
312 ops.oobbuf = buf + ops.len;
Brian Norris903cd062011-06-28 16:28:58 -0700313
Brian Norris105513c2011-09-07 13:13:28 -0700314 res = mtd->read_oob(mtd, offs, &ops);
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200315
Brian Norrisafa17de2011-09-07 13:13:29 -0700316 if (res)
Brian Norris105513c2011-09-07 13:13:28 -0700317 return res;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200318
319 buf += mtd->oobsize + mtd->writesize;
320 len -= mtd->writesize;
321 }
322 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200323}
324
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200325static int scan_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
326 size_t len, struct nand_bbt_descr *td)
327{
328 if (td->options & NAND_BBT_NO_OOB)
329 return scan_read_raw_data(mtd, buf, offs, td);
330 else
331 return scan_read_raw_oob(mtd, buf, offs, len);
332}
333
Brian Norris8b6e50c2011-05-25 14:59:01 -0700334/* Scan write data with oob to flash */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200335static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
336 uint8_t *buf, uint8_t *oob)
337{
338 struct mtd_oob_ops ops;
339
Brian Norris0612b9d2011-08-30 18:45:40 -0700340 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200341 ops.ooboffs = 0;
342 ops.ooblen = mtd->oobsize;
343 ops.datbuf = buf;
344 ops.oobbuf = oob;
345 ops.len = len;
346
347 return mtd->write_oob(mtd, offs, &ops);
348}
349
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200350static u32 bbt_get_ver_offs(struct mtd_info *mtd, struct nand_bbt_descr *td)
351{
352 u32 ver_offs = td->veroffs;
353
354 if (!(td->options & NAND_BBT_NO_OOB))
355 ver_offs += mtd->writesize;
356 return ver_offs;
357}
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359/**
360 * 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 -0700361 * @mtd: MTD device structure
362 * @buf: temporary buffer
363 * @td: descriptor for the bad block table
364 * @md: descriptor for the bad block table mirror
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700366 * Read the bad block table(s) for all chips starting at a given page. We
367 * assume that the bbt bits are in consecutive order.
368 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200369static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
370 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 struct nand_chip *this = mtd->priv;
373
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000374 /* Read the primary version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (td->options & NAND_BBT_VERSION) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000376 scan_read_raw(mtd, buf, (loff_t)td->pages[0] << this->page_shift,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200377 mtd->writesize, td);
378 td->version[0] = buf[bbt_get_ver_offs(mtd, td)];
Brian Norris9a4d4d62011-07-19 10:06:07 -0700379 pr_info("Bad block table at page %d, version 0x%02X\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700380 td->pages[0], td->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000383 /* Read the mirror version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (md && (md->options & NAND_BBT_VERSION)) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000385 scan_read_raw(mtd, buf, (loff_t)md->pages[0] << this->page_shift,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200386 mtd->writesize, td);
387 md->version[0] = buf[bbt_get_ver_offs(mtd, md)];
Brian Norris9a4d4d62011-07-19 10:06:07 -0700388 pr_info("Bad block table at page %d, version 0x%02X\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700389 md->pages[0], md->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return 1;
392}
393
Brian Norris8b6e50c2011-05-25 14:59:01 -0700394/* Scan a given block full */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200395static int scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
396 loff_t offs, uint8_t *buf, size_t readlen,
397 int scanlen, int len)
398{
399 int ret, j;
400
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200401 ret = scan_read_raw_oob(mtd, buf, offs, readlen);
Brian Norrisafa17de2011-09-07 13:13:29 -0700402 /* Ignore ECC errors when checking for BBM */
Brian Norrisd57f40542011-09-20 18:34:25 -0700403 if (ret && !mtd_is_bitflip_or_eccerr(ret))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200404 return ret;
405
406 for (j = 0; j < len; j++, buf += scanlen) {
407 if (check_pattern(buf, scanlen, mtd->writesize, bd))
408 return 1;
409 }
410 return 0;
411}
412
Brian Norris8b6e50c2011-05-25 14:59:01 -0700413/* Scan a given block partially */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200414static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
415 loff_t offs, uint8_t *buf, int len)
416{
417 struct mtd_oob_ops ops;
418 int j, ret;
419
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200420 ops.ooblen = mtd->oobsize;
421 ops.oobbuf = buf;
422 ops.ooboffs = 0;
423 ops.datbuf = NULL;
Brian Norris0612b9d2011-08-30 18:45:40 -0700424 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200425
426 for (j = 0; j < len; j++) {
427 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700428 * Read the full oob until read_oob is fixed to handle single
429 * byte reads for 16 bit buswidth.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200430 */
431 ret = mtd->read_oob(mtd, offs, &ops);
Brian Norris903cd062011-06-28 16:28:58 -0700432 /* Ignore ECC errors when checking for BBM */
Brian Norrisd57f40542011-09-20 18:34:25 -0700433 if (ret && !mtd_is_bitflip_or_eccerr(ret))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200434 return ret;
435
436 if (check_short_pattern(buf, bd))
437 return 1;
438
439 offs += mtd->writesize;
440 }
441 return 0;
442}
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/**
445 * create_bbt - [GENERIC] Create a bad block table by scanning the device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700446 * @mtd: MTD device structure
447 * @buf: temporary buffer
448 * @bd: descriptor for the good/bad block search pattern
449 * @chip: create the table for a specific chip, -1 read all chips; applies only
450 * if NAND_BBT_PERCHIP option is set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700452 * Create a bad block table by scanning the device for the given good/bad block
453 * identify pattern.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200455static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
456 struct nand_bbt_descr *bd, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 struct nand_chip *this = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200459 int i, numblocks, len, scanlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 int startblock;
461 loff_t from;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200462 size_t readlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Brian Norris9a4d4d62011-07-19 10:06:07 -0700464 pr_info("Scanning device for bad blocks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 if (bd->options & NAND_BBT_SCANALLPAGES)
467 len = 1 << (this->bbt_erase_shift - this->page_shift);
Brian Norris58373ff2010-07-15 12:15:44 -0700468 else if (bd->options & NAND_BBT_SCAN2NDPAGE)
469 len = 2;
470 else
471 len = 1;
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000472
473 if (!(bd->options & NAND_BBT_SCANEMPTY)) {
474 /* We need only read few bytes from the OOB area */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200475 scanlen = 0;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000476 readlen = bd->len;
477 } else {
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000478 /* Full page content should be read */
Joern Engel28318772006-05-22 23:18:05 +0200479 scanlen = mtd->writesize + mtd->oobsize;
480 readlen = len * mtd->writesize;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 if (chip == -1) {
Brian Norris8b6e50c2011-05-25 14:59:01 -0700484 /*
485 * Note that numblocks is 2 * (real numblocks) here, see i+=2
486 * below as it makes shifting and masking less painful
487 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 numblocks = mtd->size >> (this->bbt_erase_shift - 1);
489 startblock = 0;
490 from = 0;
491 } else {
492 if (chip >= this->numchips) {
Brian Norris9a4d4d62011-07-19 10:06:07 -0700493 pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
David Woodhousee0c7d762006-05-13 18:07:53 +0100494 chip + 1, this->numchips);
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000495 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497 numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
498 startblock = chip * numblocks;
499 numblocks += startblock;
Adrian Hunter69423d92008-12-10 13:37:21 +0000500 from = (loff_t)startblock << (this->bbt_erase_shift - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000502
Brian Norris5fb15492011-05-31 16:31:21 -0700503 if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700504 from += mtd->erasesize - (mtd->writesize * len);
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 for (i = startblock; i < numblocks;) {
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000507 int ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000508
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200509 BUG_ON(bd->options & NAND_BBT_NO_OOB);
510
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200511 if (bd->options & NAND_BBT_SCANALLPAGES)
512 ret = scan_block_full(mtd, bd, from, buf, readlen,
513 scanlen, len);
514 else
515 ret = scan_block_fast(mtd, bd, from, buf, len);
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000516
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200517 if (ret < 0)
518 return ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000519
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200520 if (ret) {
521 this->bbt[i >> 3] |= 0x03 << (i & 0x6);
Brian Norris9a4d4d62011-07-19 10:06:07 -0700522 pr_warn("Bad eraseblock %d at 0x%012llx\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700523 i >> 1, (unsigned long long)from);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200524 mtd->ecc_stats.badblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 i += 2;
528 from += (1 << this->bbt_erase_shift);
529 }
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000530 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
533/**
534 * search_bbt - [GENERIC] scan the device for a specific bad block table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700535 * @mtd: MTD device structure
536 * @buf: temporary buffer
537 * @td: descriptor for the bad block table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700539 * Read the bad block table by searching for a given ident pattern. Search is
540 * preformed either from the beginning up or from the end of the device
541 * downwards. The search starts always at the start of a block. If the option
542 * NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains
543 * the bad block information of this chip. This is necessary to provide support
544 * for certain DOC devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700546 * The bbt ident pattern resides in the oob area of the first page in a block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100548static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 struct nand_chip *this = mtd->priv;
551 int i, chips;
552 int bits, startblock, block, dir;
Joern Engel28318772006-05-22 23:18:05 +0200553 int scanlen = mtd->writesize + mtd->oobsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 int bbtblocks;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200555 int blocktopage = this->bbt_erase_shift - this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Brian Norris8b6e50c2011-05-25 14:59:01 -0700557 /* Search direction top -> down? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (td->options & NAND_BBT_LASTBLOCK) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100559 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 dir = -1;
561 } else {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000562 startblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000564 }
565
Brian Norris8b6e50c2011-05-25 14:59:01 -0700566 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (td->options & NAND_BBT_PERCHIP) {
568 chips = this->numchips;
569 bbtblocks = this->chipsize >> this->bbt_erase_shift;
570 startblock &= bbtblocks - 1;
571 } else {
572 chips = 1;
573 bbtblocks = mtd->size >> this->bbt_erase_shift;
574 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 /* Number of bits for each erase block in the bbt */
577 bits = td->options & NAND_BBT_NRBITS_MSK;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 for (i = 0; i < chips; i++) {
580 /* Reset version information */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000581 td->version[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 td->pages[i] = -1;
583 /* Scan the maximum number of blocks */
584 for (block = 0; block < td->maxblocks; block++) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 int actblock = startblock + dir * block;
Adrian Hunter69423d92008-12-10 13:37:21 +0000587 loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* Read first page */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200590 scan_read_raw(mtd, buf, offs, mtd->writesize, td);
Joern Engel28318772006-05-22 23:18:05 +0200591 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200592 td->pages[i] = actblock << blocktopage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (td->options & NAND_BBT_VERSION) {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200594 offs = bbt_get_ver_offs(mtd, td);
595 td->version[i] = buf[offs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597 break;
598 }
599 }
600 startblock += this->chipsize >> this->bbt_erase_shift;
601 }
602 /* Check, if we found a bbt for each requested chip */
603 for (i = 0; i < chips; i++) {
604 if (td->pages[i] == -1)
Brian Norris9a4d4d62011-07-19 10:06:07 -0700605 pr_warn("Bad block table not found for chip %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 else
Brian Norrisd0370212011-07-19 10:06:08 -0700607 pr_info("Bad block table found at page %d, version "
608 "0x%02X\n", td->pages[i], td->version[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000610 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
613/**
614 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -0700615 * @mtd: MTD device structure
616 * @buf: temporary buffer
617 * @td: descriptor for the bad block table
618 * @md: descriptor for the bad block table mirror
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700620 * Search and read the bad block table(s).
621 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100622static int search_read_bbts(struct mtd_info *mtd, uint8_t * buf, struct nand_bbt_descr *td, struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 /* Search the primary table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100625 search_bbt(mtd, buf, td);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /* Search the mirror table */
628 if (md)
David Woodhousee0c7d762006-05-13 18:07:53 +0100629 search_bbt(mtd, buf, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000631 /* Force result check */
632 return 1;
633}
634
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000635/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 * write_bbt - [GENERIC] (Re)write the bad block table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700637 * @mtd: MTD device structure
638 * @buf: temporary buffer
639 * @td: descriptor for the bad block table
640 * @md: descriptor for the bad block table mirror
641 * @chipsel: selector for a specific chip, -1 for all
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700643 * (Re)write the bad block table.
644 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100645static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
Thomas Gleixner9223a452006-05-23 17:21:03 +0200646 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
647 int chipsel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 struct nand_chip *this = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 struct erase_info einfo;
651 int i, j, res, chip = 0;
652 int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200653 int nrchips, bbtoffs, pageoffs, ooboffs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 uint8_t msk[4];
655 uint8_t rcode = td->reserved_block_code;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200656 size_t retlen, len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 loff_t to;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200658 struct mtd_oob_ops ops;
659
660 ops.ooblen = mtd->oobsize;
661 ops.ooboffs = 0;
662 ops.datbuf = NULL;
Brian Norris0612b9d2011-08-30 18:45:40 -0700663 ops.mode = MTD_OPS_PLACE_OOB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 if (!rcode)
666 rcode = 0xff;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700667 /* Write bad block table per chip rather than per device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (td->options & NAND_BBT_PERCHIP) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100669 numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700670 /* Full device write or specific chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (chipsel == -1) {
672 nrchips = this->numchips;
673 } else {
674 nrchips = chipsel + 1;
675 chip = chipsel;
676 }
677 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +0100678 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 nrchips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000680 }
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /* Loop through the chips */
683 for (; chip < nrchips; chip++) {
Brian Norris8b6e50c2011-05-25 14:59:01 -0700684 /*
685 * There was already a version of the table, reuse the page
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000686 * This applies for absolute placement too, as we have the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 * page nr. in td->pages.
688 */
689 if (td->pages[chip] != -1) {
690 page = td->pages[chip];
691 goto write;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Brian Norris8b6e50c2011-05-25 14:59:01 -0700694 /*
695 * Automatic placement of the bad block table. Search direction
696 * top -> down?
697 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (td->options & NAND_BBT_LASTBLOCK) {
699 startblock = numblocks * (chip + 1) - 1;
700 dir = -1;
701 } else {
702 startblock = chip * numblocks;
703 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 for (i = 0; i < td->maxblocks; i++) {
707 int block = startblock + dir * i;
708 /* Check, if the block is bad */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200709 switch ((this->bbt[block >> 2] >>
710 (2 * (block & 0x03))) & 0x03) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 case 0x01:
712 case 0x03:
713 continue;
714 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200715 page = block <<
716 (this->bbt_erase_shift - this->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /* Check, if the block is used by the mirror table */
718 if (!md || md->pages[chip] != page)
719 goto write;
720 }
Brian Norris9a4d4d62011-07-19 10:06:07 -0700721 pr_err("No space left to write bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return -ENOSPC;
David Woodhousee0c7d762006-05-13 18:07:53 +0100723 write:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 /* Set up shift count and masks for the flash table */
726 bits = td->options & NAND_BBT_NRBITS_MSK;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200727 msk[2] = ~rcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 switch (bits) {
Thomas Gleixner9223a452006-05-23 17:21:03 +0200729 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
730 msk[3] = 0x01;
731 break;
732 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
733 msk[3] = 0x03;
734 break;
735 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
736 msk[3] = 0x0f;
737 break;
738 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
739 msk[3] = 0xff;
740 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 default: return -EINVAL;
742 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 bbtoffs = chip * (numblocks >> 2);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000745
Brian Norris596d7452011-09-07 13:13:33 -0700746 to = ((loff_t)page) << this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Brian Norris8b6e50c2011-05-25 14:59:01 -0700748 /* Must we save the block contents? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (td->options & NAND_BBT_SAVECONTENT) {
750 /* Make it block aligned */
Brian Norris596d7452011-09-07 13:13:33 -0700751 to &= ~((loff_t)((1 << this->bbt_erase_shift) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 len = 1 << this->bbt_erase_shift;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200753 res = mtd->read(mtd, to, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (res < 0) {
755 if (retlen != len) {
Brian Norrisd0370212011-07-19 10:06:08 -0700756 pr_info("nand_bbt: error reading block "
757 "for writing the bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return res;
759 }
Brian Norrisd0370212011-07-19 10:06:08 -0700760 pr_warn("nand_bbt: ECC error while reading "
761 "block for writing bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200763 /* Read oob data */
Vitaly Wool70145682006-11-03 18:20:38 +0300764 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200765 ops.oobbuf = &buf[len];
766 res = mtd->read_oob(mtd, to + mtd->writesize, &ops);
Vitaly Wool70145682006-11-03 18:20:38 +0300767 if (res < 0 || ops.oobretlen != ops.ooblen)
Thomas Gleixner9223a452006-05-23 17:21:03 +0200768 goto outerr;
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 /* Calc the byte offset in the buffer */
771 pageoffs = page - (int)(to >> this->page_shift);
772 offs = pageoffs << this->page_shift;
773 /* Preset the bbt area with 0xff */
Brian Norris596d7452011-09-07 13:13:33 -0700774 memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
Thomas Gleixner9223a452006-05-23 17:21:03 +0200775 ooboffs = len + (pageoffs * mtd->oobsize);
776
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200777 } else if (td->options & NAND_BBT_NO_OOB) {
778 ooboffs = 0;
779 offs = td->len;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700780 /* The version byte */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200781 if (td->options & NAND_BBT_VERSION)
782 offs++;
783 /* Calc length */
Brian Norris596d7452011-09-07 13:13:33 -0700784 len = (size_t)(numblocks >> sft);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200785 len += offs;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700786 /* Make it page aligned! */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200787 len = ALIGN(len, mtd->writesize);
788 /* Preset the buffer with 0xff */
789 memset(buf, 0xff, len);
790 /* Pattern is located at the begin of first page */
791 memcpy(buf, td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 } else {
793 /* Calc length */
Brian Norris596d7452011-09-07 13:13:33 -0700794 len = (size_t)(numblocks >> sft);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700795 /* Make it page aligned! */
Sebastian Andrzej Siewiorcda32092010-09-29 19:43:50 +0200796 len = ALIGN(len, mtd->writesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 /* Preset the buffer with 0xff */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200798 memset(buf, 0xff, len +
799 (len >> this->page_shift)* mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 offs = 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200801 ooboffs = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 /* Pattern is located in oob area of first page */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200803 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000805
Thomas Gleixner9223a452006-05-23 17:21:03 +0200806 if (td->options & NAND_BBT_VERSION)
807 buf[ooboffs + td->veroffs] = td->version[chip];
808
Brian Norris8b6e50c2011-05-25 14:59:01 -0700809 /* Walk through the memory table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100810 for (i = 0; i < numblocks;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 uint8_t dat;
812 dat = this->bbt[bbtoffs + (i >> 2)];
David Woodhousee0c7d762006-05-13 18:07:53 +0100813 for (j = 0; j < 4; j++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 int sftcnt = (i << (3 - sft)) & sftmsk;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700815 /* Do not store the reserved bbt blocks! */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200816 buf[offs + (i >> sft)] &=
817 ~(msk[dat & 0x03] << sftcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 dat >>= 2;
819 }
820 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000821
David Woodhousee0c7d762006-05-13 18:07:53 +0100822 memset(&einfo, 0, sizeof(einfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 einfo.mtd = mtd;
Adrian Hunter69423d92008-12-10 13:37:21 +0000824 einfo.addr = to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 einfo.len = 1 << this->bbt_erase_shift;
David Woodhousee0c7d762006-05-13 18:07:53 +0100826 res = nand_erase_nand(mtd, &einfo, 1);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200827 if (res < 0)
828 goto outerr;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000829
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200830 res = scan_write_bbt(mtd, to, len, buf,
831 td->options & NAND_BBT_NO_OOB ? NULL :
832 &buf[len]);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200833 if (res < 0)
834 goto outerr;
835
Brian Norrisd0370212011-07-19 10:06:08 -0700836 pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
837 (unsigned long long)to, td->version[chip]);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 /* Mark it as used */
840 td->pages[chip] = page;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200843
844 outerr:
Brian Norrisd0370212011-07-19 10:06:08 -0700845 pr_warn("nand_bbt: error while writing bad block table %d\n", res);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200846 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
849/**
850 * nand_memory_bbt - [GENERIC] create a memory based bad block table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700851 * @mtd: MTD device structure
852 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700854 * The function creates a memory based bbt by scanning the device for
855 * manufacturer / software marked good / bad blocks.
856 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100857static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859 struct nand_chip *this = mtd->priv;
860
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000861 bd->options &= ~NAND_BBT_SCANEMPTY;
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100862 return create_bbt(mtd, this->buffers->databuf, bd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
865/**
David Woodhousee0c7d762006-05-13 18:07:53 +0100866 * check_create - [GENERIC] create and write bbt(s) if necessary
Brian Norris8b6e50c2011-05-25 14:59:01 -0700867 * @mtd: MTD device structure
868 * @buf: temporary buffer
869 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700871 * The function checks the results of the previous call to read_bbt and creates
872 * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found
873 * for the chip/device. Update is necessary if one of the tables is missing or
874 * the version nr. of one table is less than the other.
875 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100876static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700878 int i, chips, writeops, create, chipsel, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 struct nand_chip *this = mtd->priv;
880 struct nand_bbt_descr *td = this->bbt_td;
881 struct nand_bbt_descr *md = this->bbt_md;
882 struct nand_bbt_descr *rd, *rd2;
883
Brian Norris8b6e50c2011-05-25 14:59:01 -0700884 /* Do we have a bbt per chip? */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000885 if (td->options & NAND_BBT_PERCHIP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 chips = this->numchips;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000887 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 chips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 for (i = 0; i < chips; i++) {
891 writeops = 0;
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700892 create = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 rd = NULL;
894 rd2 = NULL;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700895 /* Per chip or per device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700897 /* Mirrored table available? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (md) {
899 if (td->pages[i] == -1 && md->pages[i] == -1) {
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700900 create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 writeops = 0x03;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700902 } else if (td->pages[i] == -1) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000903 rd = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 td->version[i] = md->version[i];
Brian Norris596d7452011-09-07 13:13:33 -0700905 writeops = 0x01;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700906 } else if (md->pages[i] == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 rd = td;
908 md->version[i] = td->version[i];
Brian Norris596d7452011-09-07 13:13:33 -0700909 writeops = 0x02;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700910 } else if (td->version[i] == md->version[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 rd = td;
912 if (!(td->options & NAND_BBT_VERSION))
913 rd2 = md;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700914 } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 rd = td;
916 md->version[i] = td->version[i];
Brian Norris596d7452011-09-07 13:13:33 -0700917 writeops = 0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 } else {
919 rd = md;
920 td->version[i] = md->version[i];
Brian Norris596d7452011-09-07 13:13:33 -0700921 writeops = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 } else {
924 if (td->pages[i] == -1) {
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700925 create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 writeops = 0x01;
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700927 } else {
928 rd = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000931
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700932 if (create) {
933 /* Create the bad block table by scanning the device? */
934 if (!(td->options & NAND_BBT_CREATE))
935 continue;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000936
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700937 /* Create the table in memory by scanning the chip(s) */
938 if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
939 create_bbt(mtd, buf, bd, chipsel);
940
941 td->version[i] = 1;
942 if (md)
943 md->version[i] = 1;
944 }
945
Brian Norris8b6e50c2011-05-25 14:59:01 -0700946 /* Read back first? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (rd)
David Woodhousee0c7d762006-05-13 18:07:53 +0100948 read_abs_bbt(mtd, buf, rd, chipsel);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700949 /* If they weren't versioned, read both */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (rd2)
David Woodhousee0c7d762006-05-13 18:07:53 +0100951 read_abs_bbt(mtd, buf, rd2, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Brian Norris8b6e50c2011-05-25 14:59:01 -0700953 /* Write the bad block table to the device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100955 res = write_bbt(mtd, buf, td, md, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (res < 0)
957 return res;
958 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000959
Brian Norris8b6e50c2011-05-25 14:59:01 -0700960 /* Write the mirror bad block table to the device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100962 res = write_bbt(mtd, buf, md, td, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (res < 0)
964 return res;
965 }
966 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000967 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
970/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000971 * mark_bbt_regions - [GENERIC] mark the bad block table regions
Brian Norris8b6e50c2011-05-25 14:59:01 -0700972 * @mtd: MTD device structure
973 * @td: bad block table descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700975 * The bad block table regions are marked as "bad" to prevent accidental
976 * erasures / writes. The regions are identified by the mark 0x02.
977 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100978static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 struct nand_chip *this = mtd->priv;
981 int i, j, chips, block, nrblocks, update;
982 uint8_t oldval, newval;
983
Brian Norris8b6e50c2011-05-25 14:59:01 -0700984 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (td->options & NAND_BBT_PERCHIP) {
986 chips = this->numchips;
987 nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
988 } else {
989 chips = 1;
990 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000991 }
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 for (i = 0; i < chips; i++) {
994 if ((td->options & NAND_BBT_ABSPAGE) ||
995 !(td->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100996 if (td->pages[i] == -1)
997 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000999 block <<= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 oldval = this->bbt[(block >> 3)];
1001 newval = oldval | (0x2 << (block & 0x06));
1002 this->bbt[(block >> 3)] = newval;
1003 if ((oldval != newval) && td->reserved_block_code)
Adrian Hunter69423d92008-12-10 13:37:21 +00001004 nand_update_bbt(mtd, (loff_t)block << (this->bbt_erase_shift - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 continue;
1006 }
1007 update = 0;
1008 if (td->options & NAND_BBT_LASTBLOCK)
1009 block = ((i + 1) * nrblocks) - td->maxblocks;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001010 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 block = i * nrblocks;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001012 block <<= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 for (j = 0; j < td->maxblocks; j++) {
1014 oldval = this->bbt[(block >> 3)];
1015 newval = oldval | (0x2 << (block & 0x06));
1016 this->bbt[(block >> 3)] = newval;
David Woodhousee0c7d762006-05-13 18:07:53 +01001017 if (oldval != newval)
1018 update = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 block += 2;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001020 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07001021 /*
1022 * If we want reserved blocks to be recorded to flash, and some
1023 * new ones have been marked, then we need to update the stored
1024 * bbts. This should only happen once.
1025 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (update && td->reserved_block_code)
Adrian Hunter69423d92008-12-10 13:37:21 +00001027 nand_update_bbt(mtd, (loff_t)(block - 2) << (this->bbt_erase_shift - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029}
1030
1031/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001032 * verify_bbt_descr - verify the bad block description
Brian Norris8b6e50c2011-05-25 14:59:01 -07001033 * @mtd: MTD device structure
1034 * @bd: the table to verify
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001035 *
1036 * This functions performs a few sanity checks on the bad block description
1037 * table.
1038 */
1039static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
1040{
1041 struct nand_chip *this = mtd->priv;
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001042 u32 pattern_len;
1043 u32 bits;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001044 u32 table_size;
1045
1046 if (!bd)
1047 return;
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001048
1049 pattern_len = bd->len;
1050 bits = bd->options & NAND_BBT_NRBITS_MSK;
1051
Brian Norrisa40f7342011-05-31 16:31:22 -07001052 BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001053 !(this->bbt_options & NAND_BBT_USE_FLASH));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001054 BUG_ON(!bits);
1055
1056 if (bd->options & NAND_BBT_VERSION)
1057 pattern_len++;
1058
1059 if (bd->options & NAND_BBT_NO_OOB) {
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001060 BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
Brian Norrisa40f7342011-05-31 16:31:22 -07001061 BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001062 BUG_ON(bd->offs);
1063 if (bd->options & NAND_BBT_VERSION)
1064 BUG_ON(bd->veroffs != bd->len);
1065 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1066 }
1067
1068 if (bd->options & NAND_BBT_PERCHIP)
1069 table_size = this->chipsize >> this->bbt_erase_shift;
1070 else
1071 table_size = mtd->size >> this->bbt_erase_shift;
1072 table_size >>= 3;
1073 table_size *= bits;
1074 if (bd->options & NAND_BBT_NO_OOB)
1075 table_size += pattern_len;
1076 BUG_ON(table_size > (1 << this->bbt_erase_shift));
1077}
1078
1079/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07001081 * @mtd: MTD device structure
1082 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001084 * The function checks, if a bad block table(s) is/are already available. If
1085 * not it scans the device for manufacturer marked good / bad blocks and writes
1086 * the bad block table(s) to the selected place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001088 * The bad block table memory is allocated here. It must be freed by calling
1089 * the nand_free_bbt function.
1090 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001091int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
1093 struct nand_chip *this = mtd->priv;
1094 int len, res = 0;
1095 uint8_t *buf;
1096 struct nand_bbt_descr *td = this->bbt_td;
1097 struct nand_bbt_descr *md = this->bbt_md;
1098
1099 len = mtd->size >> (this->bbt_erase_shift + 2);
Brian Norris8b6e50c2011-05-25 14:59:01 -07001100 /*
1101 * Allocate memory (2bit per block) and clear the memory bad block
1102 * table.
1103 */
Burman Yan95b93a02006-11-15 21:10:29 +02001104 this->bbt = kzalloc(len, GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001105 if (!this->bbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Brian Norris8b6e50c2011-05-25 14:59:01 -07001108 /*
1109 * If no primary table decriptor is given, scan the device to build a
1110 * memory based bad block table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 */
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001112 if (!td) {
1113 if ((res = nand_memory_bbt(mtd, bd))) {
Brian Norrisd0370212011-07-19 10:06:08 -07001114 pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
David Woodhousee0c7d762006-05-13 18:07:53 +01001115 kfree(this->bbt);
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001116 this->bbt = NULL;
1117 }
1118 return res;
1119 }
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001120 verify_bbt_descr(mtd, td);
1121 verify_bbt_descr(mtd, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 /* Allocate a temporary buffer for one eraseblock incl. oob */
1124 len = (1 << this->bbt_erase_shift);
1125 len += (len >> this->page_shift) * mtd->oobsize;
David Woodhousec3f8abf2006-05-13 04:03:42 +01001126 buf = vmalloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (!buf) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001128 kfree(this->bbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 this->bbt = NULL;
1130 return -ENOMEM;
1131 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001132
Brian Norris8b6e50c2011-05-25 14:59:01 -07001133 /* Is the bbt at a given page? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (td->options & NAND_BBT_ABSPAGE) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001135 res = read_abs_bbts(mtd, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001136 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 /* Search the bad block table using a pattern in oob */
David Woodhousee0c7d762006-05-13 18:07:53 +01001138 res = search_read_bbts(mtd, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001141 if (res)
David Woodhousee0c7d762006-05-13 18:07:53 +01001142 res = check_create(mtd, buf, bd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 /* Prevent the bbt regions from erasing / writing */
David Woodhousee0c7d762006-05-13 18:07:53 +01001145 mark_bbt_region(mtd, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (md)
David Woodhousee0c7d762006-05-13 18:07:53 +01001147 mark_bbt_region(mtd, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001148
David Woodhousee0c7d762006-05-13 18:07:53 +01001149 vfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return res;
1151}
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001154 * nand_update_bbt - [NAND Interface] update bad block table(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07001155 * @mtd: MTD device structure
1156 * @offs: the offset of the newly marked block
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001158 * The function updates the bad block table(s).
1159 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001160int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
1162 struct nand_chip *this = mtd->priv;
Brian Norris1196ac52011-09-07 13:13:32 -07001163 int len, res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 int chip, chipsel;
1165 uint8_t *buf;
1166 struct nand_bbt_descr *td = this->bbt_td;
1167 struct nand_bbt_descr *md = this->bbt_md;
1168
1169 if (!this->bbt || !td)
1170 return -EINVAL;
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 /* Allocate a temporary buffer for one eraseblock incl. oob */
1173 len = (1 << this->bbt_erase_shift);
1174 len += (len >> this->page_shift) * mtd->oobsize;
David Woodhousee0c7d762006-05-13 18:07:53 +01001175 buf = kmalloc(len, GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001176 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return -ENOMEM;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001178
Brian Norris8b6e50c2011-05-25 14:59:01 -07001179 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (td->options & NAND_BBT_PERCHIP) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001181 chip = (int)(offs >> this->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 chipsel = chip;
1183 } else {
1184 chip = 0;
1185 chipsel = -1;
1186 }
1187
1188 td->version[chip]++;
1189 if (md)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001190 md->version[chip]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Brian Norris8b6e50c2011-05-25 14:59:01 -07001192 /* Write the bad block table to the device? */
Brian Norris1196ac52011-09-07 13:13:32 -07001193 if (td->options & NAND_BBT_WRITE) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001194 res = write_bbt(mtd, buf, td, md, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (res < 0)
1196 goto out;
1197 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07001198 /* Write the mirror bad block table to the device? */
Brian Norris1196ac52011-09-07 13:13:32 -07001199 if (md && (md->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001200 res = write_bbt(mtd, buf, md, td, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
1202
David Woodhousee0c7d762006-05-13 18:07:53 +01001203 out:
1204 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return res;
1206}
1207
Brian Norris8b6e50c2011-05-25 14:59:01 -07001208/*
1209 * Define some generic bad / good block scan pattern which are used
1210 * while scanning a device for factory marked good / bad blocks.
1211 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
1215
1216static struct nand_bbt_descr agand_flashbased = {
1217 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
1218 .offs = 0x20,
1219 .len = 6,
1220 .pattern = scan_agand_pattern
1221};
1222
Brian Norris7854d3f2011-06-23 14:12:08 -07001223/* Generic flash bbt descriptors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1225static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1226
1227static struct nand_bbt_descr bbt_main_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001228 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1230 .offs = 8,
1231 .len = 4,
1232 .veroffs = 12,
1233 .maxblocks = 4,
1234 .pattern = bbt_pattern
1235};
1236
1237static struct nand_bbt_descr bbt_mirror_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001238 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1240 .offs = 8,
1241 .len = 4,
1242 .veroffs = 12,
1243 .maxblocks = 4,
1244 .pattern = mirror_pattern
1245};
1246
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001247static struct nand_bbt_descr bbt_main_no_bbt_descr = {
1248 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1249 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1250 | NAND_BBT_NO_OOB,
1251 .len = 4,
1252 .veroffs = 4,
1253 .maxblocks = 4,
1254 .pattern = bbt_pattern
1255};
1256
1257static struct nand_bbt_descr bbt_mirror_no_bbt_descr = {
1258 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1259 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1260 | NAND_BBT_NO_OOB,
1261 .len = 4,
1262 .veroffs = 4,
1263 .maxblocks = 4,
1264 .pattern = mirror_pattern
1265};
1266
Brian Norris58373ff2010-07-15 12:15:44 -07001267/**
Brian Norris7854d3f2011-06-23 14:12:08 -07001268 * nand_create_default_bbt_descr - [INTERN] Creates a BBT descriptor structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001269 * @this: NAND chip to create descriptor for
Brian Norris58373ff2010-07-15 12:15:44 -07001270 *
1271 * This function allocates and initializes a nand_bbt_descr for BBM detection
1272 * based on the properties of "this". The new descriptor is stored in
1273 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1274 * passed to this function.
Brian Norris58373ff2010-07-15 12:15:44 -07001275 */
1276static int nand_create_default_bbt_descr(struct nand_chip *this)
1277{
1278 struct nand_bbt_descr *bd;
1279 if (this->badblock_pattern) {
Brian Norrisd0370212011-07-19 10:06:08 -07001280 pr_warn("BBT descr already allocated; not replacing\n");
Brian Norris58373ff2010-07-15 12:15:44 -07001281 return -EINVAL;
1282 }
1283 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001284 if (!bd)
Brian Norris58373ff2010-07-15 12:15:44 -07001285 return -ENOMEM;
Brian Norris5fb15492011-05-31 16:31:21 -07001286 bd->options = this->bbt_options;
Brian Norris58373ff2010-07-15 12:15:44 -07001287 bd->offs = this->badblockpos;
1288 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1289 bd->pattern = scan_ff_pattern;
1290 bd->options |= NAND_BBT_DYNAMICSTRUCT;
1291 this->badblock_pattern = bd;
1292 return 0;
1293}
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001296 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
Brian Norris8b6e50c2011-05-25 14:59:01 -07001297 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001299 * This function selects the default bad block table support for the device and
1300 * calls the nand_scan_bbt function.
1301 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001302int nand_default_bbt(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 struct nand_chip *this = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001305
Brian Norris8b6e50c2011-05-25 14:59:01 -07001306 /*
1307 * Default for AG-AND. We must use a flash based bad block table as the
1308 * devices have factory marked _good_ blocks. Erasing those blocks
1309 * leads to loss of the good / bad information, so we _must_ store this
1310 * information in a good / bad table during startup.
David Woodhousee0c7d762006-05-13 18:07:53 +01001311 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (this->options & NAND_IS_AND) {
1313 /* Use the default pattern descriptors */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001314 if (!this->bbt_td) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 this->bbt_td = &bbt_main_descr;
1316 this->bbt_md = &bbt_mirror_descr;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001317 }
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001318 this->bbt_options |= NAND_BBT_USE_FLASH;
David Woodhousee0c7d762006-05-13 18:07:53 +01001319 return nand_scan_bbt(mtd, &agand_flashbased);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001321
Brian Norris8b6e50c2011-05-25 14:59:01 -07001322 /* Is a flash based bad block table requested? */
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001323 if (this->bbt_options & NAND_BBT_USE_FLASH) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001324 /* Use the default pattern descriptors */
1325 if (!this->bbt_td) {
Brian Norrisa40f7342011-05-31 16:31:22 -07001326 if (this->bbt_options & NAND_BBT_NO_OOB) {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001327 this->bbt_td = &bbt_main_no_bbt_descr;
1328 this->bbt_md = &bbt_mirror_no_bbt_descr;
1329 } else {
1330 this->bbt_td = &bbt_main_descr;
1331 this->bbt_md = &bbt_mirror_descr;
1332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 } else {
1335 this->bbt_td = NULL;
1336 this->bbt_md = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
Brian Norrisa2f812d2011-03-18 21:53:42 -07001338
1339 if (!this->badblock_pattern)
1340 nand_create_default_bbt_descr(this);
1341
David Woodhousee0c7d762006-05-13 18:07:53 +01001342 return nand_scan_bbt(mtd, this->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
1345/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001346 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07001347 * @mtd: MTD device structure
1348 * @offs: offset in the device
1349 * @allowbbt: allow access to bad block table region
1350 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001351int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 struct nand_chip *this = mtd->priv;
1354 int block;
David Woodhousee0c7d762006-05-13 18:07:53 +01001355 uint8_t res;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 /* Get block number * 2 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001358 block = (int)(offs >> (this->bbt_erase_shift - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
1360
Brian Norris289c0522011-07-19 10:06:09 -07001361 pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: "
1362 "(block %d) 0x%02x\n",
1363 (unsigned int)offs, block >> 1, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 switch ((int)res) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001366 case 0x00:
1367 return 0;
1368 case 0x01:
1369 return 1;
1370 case 0x02:
1371 return allowbbt ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
1373 return 1;
1374}
1375
David Woodhousee0c7d762006-05-13 18:07:53 +01001376EXPORT_SYMBOL(nand_scan_bbt);
1377EXPORT_SYMBOL(nand_default_bbt);