blob: 66f93e2ac16b7217facf44378b4d00e19fb99ce9 [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
17 * (NAND_USE_FLASH_BBT) is specified then the device is scanned for factory
18 * 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 Norrisa40f7342011-05-31 16:31:22 -070039 * option NAND_BBT_NO_OOB should be used (along with NAND_USE_FLASH_BBT, of
40 * 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
83 * @buf: the buffer to search
84 * @len: the length of buffer to search
85 * @paglen: the pagelength
86 * @td: search pattern descriptor
87 *
88 * Check for a pattern at the given place. Used to search bad block
89 * tables and good / bad block identifiers.
90 * If the SCAN_EMPTY option is set then check, if all bytes except the
91 * pattern area contain 0xff
92 *
93*/
David Woodhousee0c7d762006-05-13 18:07:53 +010094static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +000096 int i, end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 uint8_t *p = buf;
98
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020099 if (td->options & NAND_BBT_NO_OOB)
100 return check_pattern_no_oob(buf, td);
101
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100102 end = paglen + td->offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (td->options & NAND_BBT_SCANEMPTY) {
104 for (i = 0; i < end; i++) {
105 if (p[i] != 0xff)
106 return -1;
107 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000108 }
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100109 p += end;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 /* Compare the pattern */
112 for (i = 0; i < td->len; i++) {
113 if (p[i] != td->pattern[i])
114 return -1;
115 }
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (td->options & NAND_BBT_SCANEMPTY) {
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000118 p += td->len;
119 end += td->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 for (i = end; i < len; i++) {
121 if (*p++ != 0xff)
122 return -1;
123 }
124 }
125 return 0;
126}
127
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000128/**
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100129 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
130 * @buf: the buffer to search
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100131 * @td: search pattern descriptor
132 *
133 * Check for a pattern at the given place. Used to search bad block
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000134 * tables and good / bad block identifiers. Same as check_pattern, but
Thomas Gleixner19870da2005-07-15 14:53:51 +0100135 * no optional empty check
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100136 *
137*/
David Woodhousee0c7d762006-05-13 18:07:53 +0100138static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100139{
140 int i;
141 uint8_t *p = buf;
142
143 /* Compare the pattern */
144 for (i = 0; i < td->len; i++) {
Thomas Gleixner19870da2005-07-15 14:53:51 +0100145 if (p[td->offs + i] != td->pattern[i])
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100146 return -1;
147 }
148 return 0;
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200152 * add_marker_len - compute the length of the marker in data area
153 * @td: BBT descriptor used for computation
154 *
155 * The length will be 0 if the markeris located in OOB area.
156 */
157static u32 add_marker_len(struct nand_bbt_descr *td)
158{
159 u32 len;
160
161 if (!(td->options & NAND_BBT_NO_OOB))
162 return 0;
163
164 len = td->len;
165 if (td->options & NAND_BBT_VERSION)
166 len++;
167 return len;
168}
169
170/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * read_bbt - [GENERIC] Read the bad block table starting from page
172 * @mtd: MTD device structure
173 * @buf: temporary buffer
174 * @page: the starting page
175 * @num: the number of bbt descriptors to read
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200176 * @td: the bbt describtion table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 * @offs: offset in the memory table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 *
179 * Read the bad block table starting from page.
180 *
181 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100182static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200183 struct nand_bbt_descr *td, int offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 int res, i, j, act = 0;
186 struct nand_chip *this = mtd->priv;
187 size_t retlen, len, totlen;
188 loff_t from;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200189 int bits = td->options & NAND_BBT_NRBITS_MSK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 uint8_t msk = (uint8_t) ((1 << bits) - 1);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200191 u32 marker_len;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200192 int reserved_block_code = td->reserved_block_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 totlen = (num * bits) >> 3;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200195 marker_len = add_marker_len(td);
David Woodhousee0c7d762006-05-13 18:07:53 +0100196 from = ((loff_t) page) << this->page_shift;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 while (totlen) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100199 len = min(totlen, (size_t) (1 << this->bbt_erase_shift));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200200 if (marker_len) {
201 /*
202 * In case the BBT marker is not in the OOB area it
203 * will be just in the first page.
204 */
205 len -= marker_len;
206 from += marker_len;
207 marker_len = 0;
208 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200209 res = mtd->read(mtd, from, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (res < 0) {
211 if (retlen != len) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100212 printk(KERN_INFO "nand_bbt: Error reading bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return res;
214 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100215 printk(KERN_WARNING "nand_bbt: ECC error while reading bad block table\n");
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 /* Analyse data */
219 for (i = 0; i < len; i++) {
220 uint8_t dat = buf[i];
221 for (j = 0; j < 8; j += bits, act += 2) {
222 uint8_t tmp = (dat >> j) & msk;
223 if (tmp == msk)
224 continue;
David Woodhousee0c7d762006-05-13 18:07:53 +0100225 if (reserved_block_code && (tmp == reserved_block_code)) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000226 printk(KERN_DEBUG "nand_read_bbt: Reserved block at 0x%012llx\n",
227 (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200229 mtd->ecc_stats.bbtblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 continue;
231 }
232 /* Leave it for now, if its matured we can move this
233 * message to MTD_DEBUG_LEVEL0 */
Adrian Hunter69423d92008-12-10 13:37:21 +0000234 printk(KERN_DEBUG "nand_read_bbt: Bad block at 0x%012llx\n",
235 (loff_t)((offs << 2) + (act >> 1)) << this->bbt_erase_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000236 /* Factory marked bad or worn out ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (tmp == 0)
238 this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06);
239 else
240 this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200241 mtd->ecc_stats.badblocks++;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244 totlen -= len;
245 from += len;
246 }
247 return 0;
248}
249
250/**
251 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
252 * @mtd: MTD device structure
253 * @buf: temporary buffer
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000254 * @td: descriptor for the bad block table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 * @chip: read the table for a specific chip, -1 read all chips.
256 * Applies only if NAND_BBT_PERCHIP option is set
257 *
258 * Read the bad block table for all chips starting at a given page
259 * We assume that the bbt bits are in consecutive order.
260*/
David Woodhousee0c7d762006-05-13 18:07:53 +0100261static 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 -0700262{
263 struct nand_chip *this = mtd->priv;
264 int res = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (td->options & NAND_BBT_PERCHIP) {
267 int offs = 0;
268 for (i = 0; i < this->numchips; i++) {
269 if (chip == -1 || chip == i)
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200270 res = read_bbt(mtd, buf, td->pages[i],
271 this->chipsize >> this->bbt_erase_shift,
272 td, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (res)
274 return res;
275 offs += this->chipsize >> (this->bbt_erase_shift + 2);
276 }
277 } else {
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200278 res = read_bbt(mtd, buf, td->pages[0],
279 mtd->size >> this->bbt_erase_shift, td, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (res)
281 return res;
282 }
283 return 0;
284}
285
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200286/*
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200287 * BBT marker is in the first page, no OOB.
288 */
289static int scan_read_raw_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
290 struct nand_bbt_descr *td)
291{
292 size_t retlen;
293 size_t len;
294
295 len = td->len;
296 if (td->options & NAND_BBT_VERSION)
297 len++;
298
299 return mtd->read(mtd, offs, len, &retlen, buf);
300}
301
302/*
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200303 * Scan read raw data from flash
304 */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200305static int scan_read_raw_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200306 size_t len)
307{
308 struct mtd_oob_ops ops;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200309 int res;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200310
311 ops.mode = MTD_OOB_RAW;
312 ops.ooboffs = 0;
313 ops.ooblen = mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200314
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200315
316 while (len > 0) {
317 if (len <= mtd->writesize) {
318 ops.oobbuf = buf + len;
319 ops.datbuf = buf;
320 ops.len = len;
321 return mtd->read_oob(mtd, offs, &ops);
322 } else {
323 ops.oobbuf = buf + mtd->writesize;
324 ops.datbuf = buf;
325 ops.len = mtd->writesize;
326 res = mtd->read_oob(mtd, offs, &ops);
327
328 if (res)
329 return res;
330 }
331
332 buf += mtd->oobsize + mtd->writesize;
333 len -= mtd->writesize;
334 }
335 return 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200336}
337
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200338static int scan_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
339 size_t len, struct nand_bbt_descr *td)
340{
341 if (td->options & NAND_BBT_NO_OOB)
342 return scan_read_raw_data(mtd, buf, offs, td);
343 else
344 return scan_read_raw_oob(mtd, buf, offs, len);
345}
346
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200347/*
348 * Scan write data with oob to flash
349 */
350static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
351 uint8_t *buf, uint8_t *oob)
352{
353 struct mtd_oob_ops ops;
354
355 ops.mode = MTD_OOB_PLACE;
356 ops.ooboffs = 0;
357 ops.ooblen = mtd->oobsize;
358 ops.datbuf = buf;
359 ops.oobbuf = oob;
360 ops.len = len;
361
362 return mtd->write_oob(mtd, offs, &ops);
363}
364
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200365static u32 bbt_get_ver_offs(struct mtd_info *mtd, struct nand_bbt_descr *td)
366{
367 u32 ver_offs = td->veroffs;
368
369 if (!(td->options & NAND_BBT_NO_OOB))
370 ver_offs += mtd->writesize;
371 return ver_offs;
372}
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374/**
375 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
376 * @mtd: MTD device structure
377 * @buf: temporary buffer
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000378 * @td: descriptor for the bad block table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 * @md: descriptor for the bad block table mirror
380 *
381 * Read the bad block table(s) for all chips starting at a given page
382 * We assume that the bbt bits are in consecutive order.
383 *
384*/
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200385static int read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
386 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 struct nand_chip *this = mtd->priv;
389
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000390 /* Read the primary version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (td->options & NAND_BBT_VERSION) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000392 scan_read_raw(mtd, buf, (loff_t)td->pages[0] << this->page_shift,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200393 mtd->writesize, td);
394 td->version[0] = buf[bbt_get_ver_offs(mtd, td)];
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200395 printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
396 td->pages[0], td->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000399 /* Read the mirror version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (md && (md->options & NAND_BBT_VERSION)) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000401 scan_read_raw(mtd, buf, (loff_t)md->pages[0] << this->page_shift,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200402 mtd->writesize, td);
403 md->version[0] = buf[bbt_get_ver_offs(mtd, md)];
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200404 printk(KERN_DEBUG "Bad block table at page %d, version 0x%02X\n",
405 md->pages[0], md->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return 1;
408}
409
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200410/*
411 * Scan a given block full
412 */
413static int scan_block_full(struct mtd_info *mtd, struct nand_bbt_descr *bd,
414 loff_t offs, uint8_t *buf, size_t readlen,
415 int scanlen, int len)
416{
417 int ret, j;
418
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200419 ret = scan_read_raw_oob(mtd, buf, offs, readlen);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200420 if (ret)
421 return ret;
422
423 for (j = 0; j < len; j++, buf += scanlen) {
424 if (check_pattern(buf, scanlen, mtd->writesize, bd))
425 return 1;
426 }
427 return 0;
428}
429
430/*
431 * Scan a given block partially
432 */
433static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
434 loff_t offs, uint8_t *buf, int len)
435{
436 struct mtd_oob_ops ops;
437 int j, ret;
438
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200439 ops.ooblen = mtd->oobsize;
440 ops.oobbuf = buf;
441 ops.ooboffs = 0;
442 ops.datbuf = NULL;
443 ops.mode = MTD_OOB_PLACE;
444
445 for (j = 0; j < len; j++) {
446 /*
447 * Read the full oob until read_oob is fixed to
448 * handle single byte reads for 16 bit
449 * buswidth
450 */
451 ret = mtd->read_oob(mtd, offs, &ops);
452 if (ret)
453 return ret;
454
455 if (check_short_pattern(buf, bd))
456 return 1;
457
458 offs += mtd->writesize;
459 }
460 return 0;
461}
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463/**
464 * create_bbt - [GENERIC] Create a bad block table by scanning the device
465 * @mtd: MTD device structure
466 * @buf: temporary buffer
467 * @bd: descriptor for the good/bad block search pattern
468 * @chip: create the table for a specific chip, -1 read all chips.
469 * Applies only if NAND_BBT_PERCHIP option is set
470 *
471 * Create a bad block table by scanning the device
472 * for the given good/bad block identify pattern
473 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200474static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
475 struct nand_bbt_descr *bd, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 struct nand_chip *this = mtd->priv;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200478 int i, numblocks, len, scanlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 int startblock;
480 loff_t from;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200481 size_t readlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
David Woodhousee0c7d762006-05-13 18:07:53 +0100483 printk(KERN_INFO "Scanning device for bad blocks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 if (bd->options & NAND_BBT_SCANALLPAGES)
486 len = 1 << (this->bbt_erase_shift - this->page_shift);
Brian Norris58373ff2010-07-15 12:15:44 -0700487 else if (bd->options & NAND_BBT_SCAN2NDPAGE)
488 len = 2;
489 else
490 len = 1;
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000491
492 if (!(bd->options & NAND_BBT_SCANEMPTY)) {
493 /* We need only read few bytes from the OOB area */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200494 scanlen = 0;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000495 readlen = bd->len;
496 } else {
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000497 /* Full page content should be read */
Joern Engel28318772006-05-22 23:18:05 +0200498 scanlen = mtd->writesize + mtd->oobsize;
499 readlen = len * mtd->writesize;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 if (chip == -1) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200503 /* Note that numblocks is 2 * (real numblocks) here, see i+=2
504 * below as it makes shifting and masking less painful */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 numblocks = mtd->size >> (this->bbt_erase_shift - 1);
506 startblock = 0;
507 from = 0;
508 } else {
509 if (chip >= this->numchips) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100510 printk(KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n",
511 chip + 1, this->numchips);
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000512 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514 numblocks = this->chipsize >> (this->bbt_erase_shift - 1);
515 startblock = chip * numblocks;
516 numblocks += startblock;
Adrian Hunter69423d92008-12-10 13:37:21 +0000517 from = (loff_t)startblock << (this->bbt_erase_shift - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000519
Brian Norris5fb15492011-05-31 16:31:21 -0700520 if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700521 from += mtd->erasesize - (mtd->writesize * len);
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 for (i = startblock; i < numblocks;) {
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000524 int ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000525
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200526 BUG_ON(bd->options & NAND_BBT_NO_OOB);
527
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200528 if (bd->options & NAND_BBT_SCANALLPAGES)
529 ret = scan_block_full(mtd, bd, from, buf, readlen,
530 scanlen, len);
531 else
532 ret = scan_block_fast(mtd, bd, from, buf, len);
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000533
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200534 if (ret < 0)
535 return ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000536
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200537 if (ret) {
538 this->bbt[i >> 3] |= 0x03 << (i & 0x6);
Adrian Hunter69423d92008-12-10 13:37:21 +0000539 printk(KERN_WARNING "Bad eraseblock %d at 0x%012llx\n",
540 i >> 1, (unsigned long long)from);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200541 mtd->ecc_stats.badblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 i += 2;
545 from += (1 << this->bbt_erase_shift);
546 }
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550/**
551 * search_bbt - [GENERIC] scan the device for a specific bad block table
552 * @mtd: MTD device structure
553 * @buf: temporary buffer
554 * @td: descriptor for the bad block table
555 *
556 * Read the bad block table by searching for a given ident pattern.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000557 * Search is preformed either from the beginning up or from the end of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 * the device downwards. The search starts always at the start of a
559 * block.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000560 * If the option NAND_BBT_PERCHIP is given, each chip is searched
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 * for a bbt, which contains the bad block information of this chip.
David Woodhousee0c7d762006-05-13 18:07:53 +0100562 * This is necessary to provide support for certain DOC devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000564 * The bbt ident pattern resides in the oob area of the first page
565 * in a block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100567static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 struct nand_chip *this = mtd->priv;
570 int i, chips;
571 int bits, startblock, block, dir;
Joern Engel28318772006-05-22 23:18:05 +0200572 int scanlen = mtd->writesize + mtd->oobsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 int bbtblocks;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200574 int blocktopage = this->bbt_erase_shift - this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 /* Search direction top -> down ? */
577 if (td->options & NAND_BBT_LASTBLOCK) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100578 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 dir = -1;
580 } else {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000581 startblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000583 }
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /* Do we have a bbt per chip ? */
586 if (td->options & NAND_BBT_PERCHIP) {
587 chips = this->numchips;
588 bbtblocks = this->chipsize >> this->bbt_erase_shift;
589 startblock &= bbtblocks - 1;
590 } else {
591 chips = 1;
592 bbtblocks = mtd->size >> this->bbt_erase_shift;
593 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 /* Number of bits for each erase block in the bbt */
596 bits = td->options & NAND_BBT_NRBITS_MSK;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 for (i = 0; i < chips; i++) {
599 /* Reset version information */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000600 td->version[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 td->pages[i] = -1;
602 /* Scan the maximum number of blocks */
603 for (block = 0; block < td->maxblocks; block++) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 int actblock = startblock + dir * block;
Adrian Hunter69423d92008-12-10 13:37:21 +0000606 loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /* Read first page */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200609 scan_read_raw(mtd, buf, offs, mtd->writesize, td);
Joern Engel28318772006-05-22 23:18:05 +0200610 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200611 td->pages[i] = actblock << blocktopage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (td->options & NAND_BBT_VERSION) {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200613 offs = bbt_get_ver_offs(mtd, td);
614 td->version[i] = buf[offs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616 break;
617 }
618 }
619 startblock += this->chipsize >> this->bbt_erase_shift;
620 }
621 /* Check, if we found a bbt for each requested chip */
622 for (i = 0; i < chips; i++) {
623 if (td->pages[i] == -1)
David Woodhousee0c7d762006-05-13 18:07:53 +0100624 printk(KERN_WARNING "Bad block table not found for chip %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 else
David Woodhousee0c7d762006-05-13 18:07:53 +0100626 printk(KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n", td->pages[i],
627 td->version[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000629 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
632/**
633 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
634 * @mtd: MTD device structure
635 * @buf: temporary buffer
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000636 * @td: descriptor for the bad block table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 * @md: descriptor for the bad block table mirror
638 *
639 * Search and read the bad block table(s)
640*/
David Woodhousee0c7d762006-05-13 18:07:53 +0100641static 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 -0700642{
643 /* Search the primary table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100644 search_bbt(mtd, buf, td);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 /* Search the mirror table */
647 if (md)
David Woodhousee0c7d762006-05-13 18:07:53 +0100648 search_bbt(mtd, buf, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000650 /* Force result check */
651 return 1;
652}
653
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000654/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 * write_bbt - [GENERIC] (Re)write the bad block table
656 *
657 * @mtd: MTD device structure
658 * @buf: temporary buffer
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000659 * @td: descriptor for the bad block table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 * @md: descriptor for the bad block table mirror
661 * @chipsel: selector for a specific chip, -1 for all
662 *
663 * (Re)write the bad block table
664 *
665*/
David Woodhousee0c7d762006-05-13 18:07:53 +0100666static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
Thomas Gleixner9223a452006-05-23 17:21:03 +0200667 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
668 int chipsel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 struct nand_chip *this = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 struct erase_info einfo;
672 int i, j, res, chip = 0;
673 int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200674 int nrchips, bbtoffs, pageoffs, ooboffs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 uint8_t msk[4];
676 uint8_t rcode = td->reserved_block_code;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200677 size_t retlen, len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 loff_t to;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200679 struct mtd_oob_ops ops;
680
681 ops.ooblen = mtd->oobsize;
682 ops.ooboffs = 0;
683 ops.datbuf = NULL;
684 ops.mode = MTD_OOB_PLACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 if (!rcode)
687 rcode = 0xff;
688 /* Write bad block table per chip rather than per device ? */
689 if (td->options & NAND_BBT_PERCHIP) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100690 numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000691 /* Full device write or specific chip ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (chipsel == -1) {
693 nrchips = this->numchips;
694 } else {
695 nrchips = chipsel + 1;
696 chip = chipsel;
697 }
698 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +0100699 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 nrchips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000701 }
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 /* Loop through the chips */
704 for (; chip < nrchips; chip++) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000705
706 /* There was already a version of the table, reuse the page
707 * This applies for absolute placement too, as we have the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 * page nr. in td->pages.
709 */
710 if (td->pages[chip] != -1) {
711 page = td->pages[chip];
712 goto write;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 /* Automatic placement of the bad block table */
716 /* Search direction top -> down ? */
717 if (td->options & NAND_BBT_LASTBLOCK) {
718 startblock = numblocks * (chip + 1) - 1;
719 dir = -1;
720 } else {
721 startblock = chip * numblocks;
722 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 for (i = 0; i < td->maxblocks; i++) {
726 int block = startblock + dir * i;
727 /* Check, if the block is bad */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200728 switch ((this->bbt[block >> 2] >>
729 (2 * (block & 0x03))) & 0x03) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 case 0x01:
731 case 0x03:
732 continue;
733 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200734 page = block <<
735 (this->bbt_erase_shift - this->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /* Check, if the block is used by the mirror table */
737 if (!md || md->pages[chip] != page)
738 goto write;
739 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100740 printk(KERN_ERR "No space left to write bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return -ENOSPC;
David Woodhousee0c7d762006-05-13 18:07:53 +0100742 write:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 /* Set up shift count and masks for the flash table */
745 bits = td->options & NAND_BBT_NRBITS_MSK;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200746 msk[2] = ~rcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 switch (bits) {
Thomas Gleixner9223a452006-05-23 17:21:03 +0200748 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
749 msk[3] = 0x01;
750 break;
751 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
752 msk[3] = 0x03;
753 break;
754 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
755 msk[3] = 0x0f;
756 break;
757 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
758 msk[3] = 0xff;
759 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 default: return -EINVAL;
761 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 bbtoffs = chip * (numblocks >> 2);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 to = ((loff_t) page) << this->page_shift;
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /* Must we save the block contents ? */
768 if (td->options & NAND_BBT_SAVECONTENT) {
769 /* Make it block aligned */
770 to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1));
771 len = 1 << this->bbt_erase_shift;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200772 res = mtd->read(mtd, to, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (res < 0) {
774 if (retlen != len) {
Thomas Gleixner9223a452006-05-23 17:21:03 +0200775 printk(KERN_INFO "nand_bbt: Error "
776 "reading block for writing "
777 "the bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return res;
779 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200780 printk(KERN_WARNING "nand_bbt: ECC error "
781 "while reading block for writing "
782 "bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200784 /* Read oob data */
Vitaly Wool70145682006-11-03 18:20:38 +0300785 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200786 ops.oobbuf = &buf[len];
787 res = mtd->read_oob(mtd, to + mtd->writesize, &ops);
Vitaly Wool70145682006-11-03 18:20:38 +0300788 if (res < 0 || ops.oobretlen != ops.ooblen)
Thomas Gleixner9223a452006-05-23 17:21:03 +0200789 goto outerr;
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 /* Calc the byte offset in the buffer */
792 pageoffs = page - (int)(to >> this->page_shift);
793 offs = pageoffs << this->page_shift;
794 /* Preset the bbt area with 0xff */
David Woodhousee0c7d762006-05-13 18:07:53 +0100795 memset(&buf[offs], 0xff, (size_t) (numblocks >> sft));
Thomas Gleixner9223a452006-05-23 17:21:03 +0200796 ooboffs = len + (pageoffs * mtd->oobsize);
797
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200798 } else if (td->options & NAND_BBT_NO_OOB) {
799 ooboffs = 0;
800 offs = td->len;
801 /* the version byte */
802 if (td->options & NAND_BBT_VERSION)
803 offs++;
804 /* Calc length */
805 len = (size_t) (numblocks >> sft);
806 len += offs;
807 /* Make it page aligned ! */
808 len = ALIGN(len, mtd->writesize);
809 /* Preset the buffer with 0xff */
810 memset(buf, 0xff, len);
811 /* Pattern is located at the begin of first page */
812 memcpy(buf, td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 } else {
814 /* Calc length */
815 len = (size_t) (numblocks >> sft);
816 /* Make it page aligned ! */
Sebastian Andrzej Siewiorcda32092010-09-29 19:43:50 +0200817 len = ALIGN(len, mtd->writesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 /* Preset the buffer with 0xff */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200819 memset(buf, 0xff, len +
820 (len >> this->page_shift)* mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 offs = 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200822 ooboffs = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /* Pattern is located in oob area of first page */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200824 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000826
Thomas Gleixner9223a452006-05-23 17:21:03 +0200827 if (td->options & NAND_BBT_VERSION)
828 buf[ooboffs + td->veroffs] = td->version[chip];
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 /* walk through the memory table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100831 for (i = 0; i < numblocks;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 uint8_t dat;
833 dat = this->bbt[bbtoffs + (i >> 2)];
David Woodhousee0c7d762006-05-13 18:07:53 +0100834 for (j = 0; j < 4; j++, i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 int sftcnt = (i << (3 - sft)) & sftmsk;
836 /* Do not store the reserved bbt blocks ! */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200837 buf[offs + (i >> sft)] &=
838 ~(msk[dat & 0x03] << sftcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 dat >>= 2;
840 }
841 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000842
David Woodhousee0c7d762006-05-13 18:07:53 +0100843 memset(&einfo, 0, sizeof(einfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 einfo.mtd = mtd;
Adrian Hunter69423d92008-12-10 13:37:21 +0000845 einfo.addr = to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 einfo.len = 1 << this->bbt_erase_shift;
David Woodhousee0c7d762006-05-13 18:07:53 +0100847 res = nand_erase_nand(mtd, &einfo, 1);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200848 if (res < 0)
849 goto outerr;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000850
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200851 res = scan_write_bbt(mtd, to, len, buf,
852 td->options & NAND_BBT_NO_OOB ? NULL :
853 &buf[len]);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200854 if (res < 0)
855 goto outerr;
856
Adrian Hunter69423d92008-12-10 13:37:21 +0000857 printk(KERN_DEBUG "Bad block table written to 0x%012llx, version "
858 "0x%02X\n", (unsigned long long)to, td->version[chip]);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 /* Mark it as used */
861 td->pages[chip] = page;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200864
865 outerr:
866 printk(KERN_WARNING
867 "nand_bbt: Error while writing bad block table %d\n", res);
868 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
871/**
872 * nand_memory_bbt - [GENERIC] create a memory based bad block table
873 * @mtd: MTD device structure
874 * @bd: descriptor for the good/bad block search pattern
875 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000876 * The function creates a memory based bbt by scanning the device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 * for manufacturer / software marked good / bad blocks
878*/
David Woodhousee0c7d762006-05-13 18:07:53 +0100879static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
881 struct nand_chip *this = mtd->priv;
882
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000883 bd->options &= ~NAND_BBT_SCANEMPTY;
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100884 return create_bbt(mtd, this->buffers->databuf, bd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
887/**
David Woodhousee0c7d762006-05-13 18:07:53 +0100888 * check_create - [GENERIC] create and write bbt(s) if necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 * @mtd: MTD device structure
890 * @buf: temporary buffer
891 * @bd: descriptor for the good/bad block search pattern
892 *
893 * The function checks the results of the previous call to read_bbt
David Woodhousee0c7d762006-05-13 18:07:53 +0100894 * and creates / updates the bbt(s) if necessary
895 * Creation is necessary if no bbt was found for the chip/device
896 * Update is necessary if one of the tables is missing or the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 * version nr. of one table is less than the other
898*/
David Woodhousee0c7d762006-05-13 18:07:53 +0100899static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 int i, chips, writeops, chipsel, res;
902 struct nand_chip *this = mtd->priv;
903 struct nand_bbt_descr *td = this->bbt_td;
904 struct nand_bbt_descr *md = this->bbt_md;
905 struct nand_bbt_descr *rd, *rd2;
906
907 /* Do we have a bbt per chip ? */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000908 if (td->options & NAND_BBT_PERCHIP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 chips = this->numchips;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000910 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 chips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 for (i = 0; i < chips; i++) {
914 writeops = 0;
915 rd = NULL;
916 rd2 = NULL;
917 /* Per chip or per device ? */
918 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300919 /* Mirrored table available ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (md) {
921 if (td->pages[i] == -1 && md->pages[i] == -1) {
922 writeops = 0x03;
923 goto create;
924 }
925
926 if (td->pages[i] == -1) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000927 rd = md;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 td->version[i] = md->version[i];
929 writeops = 1;
930 goto writecheck;
931 }
932
933 if (md->pages[i] == -1) {
934 rd = td;
935 md->version[i] = td->version[i];
936 writeops = 2;
937 goto writecheck;
938 }
939
940 if (td->version[i] == md->version[i]) {
941 rd = td;
942 if (!(td->options & NAND_BBT_VERSION))
943 rd2 = md;
944 goto writecheck;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 if (((int8_t) (td->version[i] - md->version[i])) > 0) {
948 rd = td;
949 md->version[i] = td->version[i];
950 writeops = 2;
951 } else {
952 rd = md;
953 td->version[i] = md->version[i];
954 writeops = 1;
955 }
956
957 goto writecheck;
958
959 } else {
960 if (td->pages[i] == -1) {
961 writeops = 0x01;
962 goto create;
963 }
964 rd = td;
965 goto writecheck;
966 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100967 create:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 /* Create the bad block table by scanning the device ? */
969 if (!(td->options & NAND_BBT_CREATE))
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000970 continue;
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* Create the table in memory by scanning the chip(s) */
Sebastian Andrzej Siewior453281a2010-10-01 21:37:37 +0200973 if (!(this->options & NAND_CREATE_EMPTY_BBT))
974 create_bbt(mtd, buf, bd, chipsel);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 td->version[i] = 1;
977 if (md)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000978 md->version[i] = 1;
David Woodhousee0c7d762006-05-13 18:07:53 +0100979 writecheck:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 /* read back first ? */
981 if (rd)
David Woodhousee0c7d762006-05-13 18:07:53 +0100982 read_abs_bbt(mtd, buf, rd, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 /* If they weren't versioned, read both. */
984 if (rd2)
David Woodhousee0c7d762006-05-13 18:07:53 +0100985 read_abs_bbt(mtd, buf, rd2, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 /* Write the bad block table to the device ? */
988 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100989 res = write_bbt(mtd, buf, td, md, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (res < 0)
991 return res;
992 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /* Write the mirror bad block table to the device ? */
995 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100996 res = write_bbt(mtd, buf, md, td, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (res < 0)
998 return res;
999 }
1000 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001001 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
1004/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001005 * mark_bbt_regions - [GENERIC] mark the bad block table regions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 * @mtd: MTD device structure
1007 * @td: bad block table descriptor
1008 *
1009 * The bad block table regions are marked as "bad" to prevent
1010 * accidental erasures / writes. The regions are identified by
1011 * the mark 0x02.
1012*/
David Woodhousee0c7d762006-05-13 18:07:53 +01001013static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015 struct nand_chip *this = mtd->priv;
1016 int i, j, chips, block, nrblocks, update;
1017 uint8_t oldval, newval;
1018
1019 /* Do we have a bbt per chip ? */
1020 if (td->options & NAND_BBT_PERCHIP) {
1021 chips = this->numchips;
1022 nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
1023 } else {
1024 chips = 1;
1025 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001026 }
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 for (i = 0; i < chips; i++) {
1029 if ((td->options & NAND_BBT_ABSPAGE) ||
1030 !(td->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001031 if (td->pages[i] == -1)
1032 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001034 block <<= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 oldval = this->bbt[(block >> 3)];
1036 newval = oldval | (0x2 << (block & 0x06));
1037 this->bbt[(block >> 3)] = newval;
1038 if ((oldval != newval) && td->reserved_block_code)
Adrian Hunter69423d92008-12-10 13:37:21 +00001039 nand_update_bbt(mtd, (loff_t)block << (this->bbt_erase_shift - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 continue;
1041 }
1042 update = 0;
1043 if (td->options & NAND_BBT_LASTBLOCK)
1044 block = ((i + 1) * nrblocks) - td->maxblocks;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001045 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 block = i * nrblocks;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001047 block <<= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 for (j = 0; j < td->maxblocks; j++) {
1049 oldval = this->bbt[(block >> 3)];
1050 newval = oldval | (0x2 << (block & 0x06));
1051 this->bbt[(block >> 3)] = newval;
David Woodhousee0c7d762006-05-13 18:07:53 +01001052 if (oldval != newval)
1053 update = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 block += 2;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 /* If we want reserved blocks to be recorded to flash, and some
1057 new ones have been marked, then we need to update the stored
1058 bbts. This should only happen once. */
1059 if (update && td->reserved_block_code)
Adrian Hunter69423d92008-12-10 13:37:21 +00001060 nand_update_bbt(mtd, (loff_t)(block - 2) << (this->bbt_erase_shift - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062}
1063
1064/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001065 * verify_bbt_descr - verify the bad block description
Randy Dunlap1534b8b2010-11-18 15:02:21 -08001066 * @mtd: MTD device structure
1067 * @bd: the table to verify
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001068 *
1069 * This functions performs a few sanity checks on the bad block description
1070 * table.
1071 */
1072static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
1073{
1074 struct nand_chip *this = mtd->priv;
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001075 u32 pattern_len;
1076 u32 bits;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001077 u32 table_size;
1078
1079 if (!bd)
1080 return;
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001081
1082 pattern_len = bd->len;
1083 bits = bd->options & NAND_BBT_NRBITS_MSK;
1084
Brian Norrisa40f7342011-05-31 16:31:22 -07001085 BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
1086 !(this->bbt_options & NAND_USE_FLASH_BBT));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001087 BUG_ON(!bits);
1088
1089 if (bd->options & NAND_BBT_VERSION)
1090 pattern_len++;
1091
1092 if (bd->options & NAND_BBT_NO_OOB) {
Brian Norrisa40f7342011-05-31 16:31:22 -07001093 BUG_ON(!(this->bbt_options & NAND_USE_FLASH_BBT));
1094 BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001095 BUG_ON(bd->offs);
1096 if (bd->options & NAND_BBT_VERSION)
1097 BUG_ON(bd->veroffs != bd->len);
1098 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1099 }
1100
1101 if (bd->options & NAND_BBT_PERCHIP)
1102 table_size = this->chipsize >> this->bbt_erase_shift;
1103 else
1104 table_size = mtd->size >> this->bbt_erase_shift;
1105 table_size >>= 3;
1106 table_size *= bits;
1107 if (bd->options & NAND_BBT_NO_OOB)
1108 table_size += pattern_len;
1109 BUG_ON(table_size > (1 << this->bbt_erase_shift));
1110}
1111
1112/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
1114 * @mtd: MTD device structure
1115 * @bd: descriptor for the good/bad block search pattern
1116 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001117 * The function checks, if a bad block table(s) is/are already
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 * available. If not it scans the device for manufacturer
1119 * marked good / bad blocks and writes the bad block table(s) to
1120 * the selected place.
1121 *
1122 * The bad block table memory is allocated here. It must be freed
1123 * by calling the nand_free_bbt function.
1124 *
1125*/
David Woodhousee0c7d762006-05-13 18:07:53 +01001126int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
1128 struct nand_chip *this = mtd->priv;
1129 int len, res = 0;
1130 uint8_t *buf;
1131 struct nand_bbt_descr *td = this->bbt_td;
1132 struct nand_bbt_descr *md = this->bbt_md;
1133
1134 len = mtd->size >> (this->bbt_erase_shift + 2);
Burman Yan95b93a02006-11-15 21:10:29 +02001135 /* Allocate memory (2bit per block) and clear the memory bad block table */
1136 this->bbt = kzalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (!this->bbt) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001138 printk(KERN_ERR "nand_scan_bbt: Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return -ENOMEM;
1140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 /* If no primary table decriptor is given, scan the device
1143 * to build a memory based bad block table
1144 */
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001145 if (!td) {
1146 if ((res = nand_memory_bbt(mtd, bd))) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001147 printk(KERN_ERR "nand_bbt: Can't scan flash and build the RAM-based BBT\n");
1148 kfree(this->bbt);
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001149 this->bbt = NULL;
1150 }
1151 return res;
1152 }
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001153 verify_bbt_descr(mtd, td);
1154 verify_bbt_descr(mtd, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 /* Allocate a temporary buffer for one eraseblock incl. oob */
1157 len = (1 << this->bbt_erase_shift);
1158 len += (len >> this->page_shift) * mtd->oobsize;
David Woodhousec3f8abf2006-05-13 04:03:42 +01001159 buf = vmalloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (!buf) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001161 printk(KERN_ERR "nand_bbt: Out of memory\n");
1162 kfree(this->bbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 this->bbt = NULL;
1164 return -ENOMEM;
1165 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 /* Is the bbt at a given page ? */
1168 if (td->options & NAND_BBT_ABSPAGE) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001169 res = read_abs_bbts(mtd, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001170 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 /* Search the bad block table using a pattern in oob */
David Woodhousee0c7d762006-05-13 18:07:53 +01001172 res = search_read_bbts(mtd, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001175 if (res)
David Woodhousee0c7d762006-05-13 18:07:53 +01001176 res = check_create(mtd, buf, bd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 /* Prevent the bbt regions from erasing / writing */
David Woodhousee0c7d762006-05-13 18:07:53 +01001179 mark_bbt_region(mtd, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (md)
David Woodhousee0c7d762006-05-13 18:07:53 +01001181 mark_bbt_region(mtd, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001182
David Woodhousee0c7d762006-05-13 18:07:53 +01001183 vfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return res;
1185}
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001188 * nand_update_bbt - [NAND Interface] update bad block table(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 * @mtd: MTD device structure
1190 * @offs: the offset of the newly marked block
1191 *
1192 * The function updates the bad block table(s)
1193*/
David Woodhousee0c7d762006-05-13 18:07:53 +01001194int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
1196 struct nand_chip *this = mtd->priv;
1197 int len, res = 0, writeops = 0;
1198 int chip, chipsel;
1199 uint8_t *buf;
1200 struct nand_bbt_descr *td = this->bbt_td;
1201 struct nand_bbt_descr *md = this->bbt_md;
1202
1203 if (!this->bbt || !td)
1204 return -EINVAL;
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /* Allocate a temporary buffer for one eraseblock incl. oob */
1207 len = (1 << this->bbt_erase_shift);
1208 len += (len >> this->page_shift) * mtd->oobsize;
David Woodhousee0c7d762006-05-13 18:07:53 +01001209 buf = kmalloc(len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (!buf) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001211 printk(KERN_ERR "nand_update_bbt: Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return -ENOMEM;
1213 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 writeops = md != NULL ? 0x03 : 0x01;
1216
1217 /* Do we have a bbt per chip ? */
1218 if (td->options & NAND_BBT_PERCHIP) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001219 chip = (int)(offs >> this->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 chipsel = chip;
1221 } else {
1222 chip = 0;
1223 chipsel = -1;
1224 }
1225
1226 td->version[chip]++;
1227 if (md)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001228 md->version[chip]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 /* Write the bad block table to the device ? */
1231 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001232 res = write_bbt(mtd, buf, td, md, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (res < 0)
1234 goto out;
1235 }
1236 /* Write the mirror bad block table to the device ? */
1237 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001238 res = write_bbt(mtd, buf, md, td, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
1240
David Woodhousee0c7d762006-05-13 18:07:53 +01001241 out:
1242 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return res;
1244}
1245
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001246/* Define some generic bad / good block scan pattern which are used
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +00001247 * while scanning a device for factory marked good / bad blocks. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 };
1251
1252static struct nand_bbt_descr agand_flashbased = {
1253 .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
1254 .offs = 0x20,
1255 .len = 6,
1256 .pattern = scan_agand_pattern
1257};
1258
1259/* Generic flash bbt decriptors
1260*/
1261static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1262static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1263
1264static struct nand_bbt_descr bbt_main_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001265 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1267 .offs = 8,
1268 .len = 4,
1269 .veroffs = 12,
1270 .maxblocks = 4,
1271 .pattern = bbt_pattern
1272};
1273
1274static struct nand_bbt_descr bbt_mirror_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001275 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1277 .offs = 8,
1278 .len = 4,
1279 .veroffs = 12,
1280 .maxblocks = 4,
1281 .pattern = mirror_pattern
1282};
1283
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001284static struct nand_bbt_descr bbt_main_no_bbt_descr = {
1285 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1286 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1287 | NAND_BBT_NO_OOB,
1288 .len = 4,
1289 .veroffs = 4,
1290 .maxblocks = 4,
1291 .pattern = bbt_pattern
1292};
1293
1294static struct nand_bbt_descr bbt_mirror_no_bbt_descr = {
1295 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1296 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1297 | NAND_BBT_NO_OOB,
1298 .len = 4,
1299 .veroffs = 4,
1300 .maxblocks = 4,
1301 .pattern = mirror_pattern
1302};
1303
Brian Norris58373ff2010-07-15 12:15:44 -07001304/**
1305 * nand_create_default_bbt_descr - [Internal] Creates a BBT descriptor structure
1306 * @this: NAND chip to create descriptor for
1307 *
1308 * This function allocates and initializes a nand_bbt_descr for BBM detection
1309 * based on the properties of "this". The new descriptor is stored in
1310 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1311 * passed to this function.
1312 *
Brian Norris58373ff2010-07-15 12:15:44 -07001313 */
1314static int nand_create_default_bbt_descr(struct nand_chip *this)
1315{
1316 struct nand_bbt_descr *bd;
1317 if (this->badblock_pattern) {
1318 printk(KERN_WARNING "BBT descr already allocated; not replacing.\n");
1319 return -EINVAL;
1320 }
1321 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
1322 if (!bd) {
1323 printk(KERN_ERR "nand_create_default_bbt_descr: Out of memory\n");
1324 return -ENOMEM;
1325 }
Brian Norris5fb15492011-05-31 16:31:21 -07001326 bd->options = this->bbt_options;
Brian Norris58373ff2010-07-15 12:15:44 -07001327 bd->offs = this->badblockpos;
1328 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1329 bd->pattern = scan_ff_pattern;
1330 bd->options |= NAND_BBT_DYNAMICSTRUCT;
1331 this->badblock_pattern = bd;
1332 return 0;
1333}
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001336 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 * @mtd: MTD device structure
1338 *
1339 * This function selects the default bad block table
1340 * support for the device and calls the nand_scan_bbt function
1341 *
1342*/
David Woodhousee0c7d762006-05-13 18:07:53 +01001343int nand_default_bbt(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
1345 struct nand_chip *this = mtd->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001346
1347 /* Default for AG-AND. We must use a flash based
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 * bad block table as the devices have factory marked
1349 * _good_ blocks. Erasing those blocks leads to loss
1350 * of the good / bad information, so we _must_ store
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001351 * this information in a good / bad table during
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 * startup
David Woodhousee0c7d762006-05-13 18:07:53 +01001353 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (this->options & NAND_IS_AND) {
1355 /* Use the default pattern descriptors */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001356 if (!this->bbt_td) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 this->bbt_td = &bbt_main_descr;
1358 this->bbt_md = &bbt_mirror_descr;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001359 }
Brian Norrisa40f7342011-05-31 16:31:22 -07001360 this->bbt_options |= NAND_USE_FLASH_BBT;
David Woodhousee0c7d762006-05-13 18:07:53 +01001361 return nand_scan_bbt(mtd, &agand_flashbased);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 /* Is a flash based bad block table requested ? */
Brian Norrisa40f7342011-05-31 16:31:22 -07001365 if (this->bbt_options & NAND_USE_FLASH_BBT) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001366 /* Use the default pattern descriptors */
1367 if (!this->bbt_td) {
Brian Norrisa40f7342011-05-31 16:31:22 -07001368 if (this->bbt_options & NAND_BBT_NO_OOB) {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001369 this->bbt_td = &bbt_main_no_bbt_descr;
1370 this->bbt_md = &bbt_mirror_no_bbt_descr;
1371 } else {
1372 this->bbt_td = &bbt_main_descr;
1373 this->bbt_md = &bbt_mirror_descr;
1374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 } else {
1377 this->bbt_td = NULL;
1378 this->bbt_md = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
Brian Norrisa2f812d2011-03-18 21:53:42 -07001380
1381 if (!this->badblock_pattern)
1382 nand_create_default_bbt_descr(this);
1383
David Woodhousee0c7d762006-05-13 18:07:53 +01001384 return nand_scan_bbt(mtd, this->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385}
1386
1387/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001388 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 * @mtd: MTD device structure
1390 * @offs: offset in the device
1391 * @allowbbt: allow access to bad block table region
1392 *
1393*/
David Woodhousee0c7d762006-05-13 18:07:53 +01001394int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 struct nand_chip *this = mtd->priv;
1397 int block;
David Woodhousee0c7d762006-05-13 18:07:53 +01001398 uint8_t res;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 /* Get block number * 2 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001401 block = (int)(offs >> (this->bbt_erase_shift - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03;
1403
David Woodhousee0c7d762006-05-13 18:07:53 +01001404 DEBUG(MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1405 (unsigned int)offs, block >> 1, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 switch ((int)res) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001408 case 0x00:
1409 return 0;
1410 case 0x01:
1411 return 1;
1412 case 0x02:
1413 return allowbbt ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
1415 return 1;
1416}
1417
David Woodhousee0c7d762006-05-13 18:07:53 +01001418EXPORT_SYMBOL(nand_scan_bbt);
1419EXPORT_SYMBOL(nand_default_bbt);