blob: 53e17586fbedd60744bab79e15e4eed96945e02e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/mtd/nand_bbt.c
3 *
4 * Overview:
5 * Bad block table support for the NAND driver
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006 *
Fabio Estevamd159c4e2012-07-28 19:29:25 -03007 * Copyright © 2004 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * Description:
14 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000015 * When nand_scan_bbt is called, then it tries to find the bad block table
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020016 * depending on the options in the BBT descriptor(s). If no flash based BBT
Brian Norrisbb9ebd42011-05-31 16:31:23 -070017 * (NAND_BBT_USE_FLASH) is specified then the device is scanned for factory
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020018 * marked good / bad blocks. This information is used to create a memory BBT.
19 * Once a new bad block is discovered then the "factory" information is updated
20 * on the device.
21 * If a flash based BBT is specified then the function first tries to find the
22 * BBT on flash. If a BBT is found then the contents are read and the memory
23 * based BBT is created. If a mirrored BBT is selected then the mirror is
24 * searched too and the versions are compared. If the mirror has a greater
Huang Shijie44ed0ff2012-07-03 16:44:15 +080025 * version number, then the mirror BBT is used to build the memory based BBT.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * If the tables are not versioned, then we "or" the bad block information.
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020027 * If one of the BBTs is out of date or does not exist it is (re)created.
28 * If no BBT exists at all then the device is scanned for factory marked
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000029 * good / bad blocks and the bad block tables are created.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020031 * For manufacturer created BBTs like the one found on M-SYS DOC devices
32 * the BBT is searched and read but never created
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 *
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020034 * The auto generated bad block table is located in the last good blocks
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000035 * of the device. The table is mirrored, so it can be updated eventually.
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020036 * The table is marked in the OOB area with an ident pattern and a version
37 * number which indicates which of both tables is more up to date. If the NAND
38 * controller needs the complete OOB area for the ECC information then the
Brian Norrisbb9ebd42011-05-31 16:31:23 -070039 * option NAND_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of
Brian Norrisa40f7342011-05-31 16:31:22 -070040 * course): it moves the ident pattern and the version byte into the data area
41 * and the OOB area will remain untouched.
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 *
43 * The table uses 2 bits per block
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020044 * 11b: block is good
45 * 00b: block is factory marked bad
46 * 01b, 10b: block is marked bad due to wear
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 *
48 * The memory bad block table uses the following scheme:
49 * 00b: block is good
50 * 01b: block is marked bad due to wear
51 * 10b: block is reserved (to protect the bbt area)
52 * 11b: block is factory marked bad
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000053 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * Multichip devices like DOC store the bad block info per floor.
55 *
56 * Following assumptions are made:
57 * - bbts start at a page boundary, if autolocated on a block boundary
David Woodhousee0c7d762006-05-13 18:07:53 +010058 * - the space necessary for a bbt in FLASH does not exceed a block boundary
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000059 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 */
61
62#include <linux/slab.h>
63#include <linux/types.h>
64#include <linux/mtd/mtd.h>
Richard Genoud61de9da2012-09-11 15:50:54 +020065#include <linux/mtd/bbm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/mtd/nand.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/bitops.h>
68#include <linux/delay.h>
David Woodhousec3f8abf2006-05-13 04:03:42 +010069#include <linux/vmalloc.h>
Paul Gortmakerf3bcc012011-07-10 12:43:28 -040070#include <linux/export.h>
Brian Norris491ed062012-06-22 16:35:44 -070071#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Brian Norris771c5682013-07-30 17:52:55 -070073#define BBT_BLOCK_GOOD 0x00
74#define BBT_BLOCK_WORN 0x01
75#define BBT_BLOCK_RESERVED 0x02
76#define BBT_BLOCK_FACTORY_BAD 0x03
77
78#define BBT_ENTRY_MASK 0x03
79#define BBT_ENTRY_SHIFT 2
80
Brian Norrisb32843b2013-07-30 17:52:59 -070081static int nand_update_bbt(struct mtd_info *mtd, loff_t offs);
82
Brian Norris771c5682013-07-30 17:52:55 -070083static inline uint8_t bbt_get_entry(struct nand_chip *chip, int block)
84{
85 uint8_t entry = chip->bbt[block >> BBT_ENTRY_SHIFT];
86 entry >>= (block & BBT_ENTRY_MASK) * 2;
87 return entry & BBT_ENTRY_MASK;
88}
89
90static inline void bbt_mark_entry(struct nand_chip *chip, int block,
91 uint8_t mark)
92{
93 uint8_t msk = (mark & BBT_ENTRY_MASK) << ((block & BBT_ENTRY_MASK) * 2);
94 chip->bbt[block >> BBT_ENTRY_SHIFT] |= msk;
95}
96
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020097static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
98{
Brian Norris718894a2012-06-22 16:35:43 -070099 if (memcmp(buf, td->pattern, td->len))
100 return -1;
101 return 0;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200102}
103
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000104/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * check_pattern - [GENERIC] check if a pattern is in the buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700106 * @buf: the buffer to search
107 * @len: the length of buffer to search
108 * @paglen: the pagelength
109 * @td: search pattern descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700111 * Check for a pattern at the given place. Used to search bad block tables and
Brian Norrisdad22562013-07-30 17:53:00 -0700112 * good / bad block identifiers.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700113 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100114static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200116 if (td->options & NAND_BBT_NO_OOB)
117 return check_pattern_no_oob(buf, td);
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 /* Compare the pattern */
Brian Norrisdad22562013-07-30 17:53:00 -0700120 if (memcmp(buf + paglen + td->offs, td->pattern, td->len))
Brian Norris75b66d82011-09-07 13:13:41 -0700121 return -1;
Brian Norris58373ff2010-07-15 12:15:44 -0700122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 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{
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100137 /* Compare the pattern */
Brian Norris491ed062012-06-22 16:35:44 -0700138 if (memcmp(buf + td->offs, td->pattern, td->len))
139 return -1;
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100140 return 0;
141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200144 * add_marker_len - compute the length of the marker in data area
Brian Norris8b6e50c2011-05-25 14:59:01 -0700145 * @td: BBT descriptor used for computation
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200146 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700147 * The length will be 0 if the marker is located in OOB area.
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200148 */
149static u32 add_marker_len(struct nand_bbt_descr *td)
150{
151 u32 len;
152
153 if (!(td->options & NAND_BBT_NO_OOB))
154 return 0;
155
156 len = td->len;
157 if (td->options & NAND_BBT_VERSION)
158 len++;
159 return len;
160}
161
162/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 * read_bbt - [GENERIC] Read the bad block table starting from page
Brian Norris8b6e50c2011-05-25 14:59:01 -0700164 * @mtd: MTD device structure
165 * @buf: temporary buffer
166 * @page: the starting page
167 * @num: the number of bbt descriptors to read
Brian Norris7854d3f2011-06-23 14:12:08 -0700168 * @td: the bbt describtion table
Brian Norrisb4d20d62013-07-30 17:52:56 -0700169 * @offs: block number offset in the table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 *
171 * Read the bad block table starting from page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100173static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200174 struct nand_bbt_descr *td, int offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Brian Norris167a8d52011-09-20 18:35:08 -0700176 int res, ret = 0, i, j, act = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 struct nand_chip *this = mtd->priv;
178 size_t retlen, len, totlen;
179 loff_t from;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200180 int bits = td->options & NAND_BBT_NRBITS_MSK;
Brian Norris596d7452011-09-07 13:13:33 -0700181 uint8_t msk = (uint8_t)((1 << bits) - 1);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200182 u32 marker_len;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200183 int reserved_block_code = td->reserved_block_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 totlen = (num * bits) >> 3;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200186 marker_len = add_marker_len(td);
Brian Norris596d7452011-09-07 13:13:33 -0700187 from = ((loff_t)page) << this->page_shift;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 while (totlen) {
Brian Norris596d7452011-09-07 13:13:33 -0700190 len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200191 if (marker_len) {
192 /*
193 * In case the BBT marker is not in the OOB area it
194 * will be just in the first page.
195 */
196 len -= marker_len;
197 from += marker_len;
198 marker_len = 0;
199 }
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200200 res = mtd_read(mtd, from, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (res < 0) {
Brian Norris167a8d52011-09-20 18:35:08 -0700202 if (mtd_is_eccerr(res)) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200203 pr_info("nand_bbt: ECC error in BBT at 0x%012llx\n",
204 from & ~mtd->writesize);
Brian Norris167a8d52011-09-20 18:35:08 -0700205 return res;
206 } else if (mtd_is_bitflip(res)) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200207 pr_info("nand_bbt: corrected error in BBT at 0x%012llx\n",
208 from & ~mtd->writesize);
Brian Norris167a8d52011-09-20 18:35:08 -0700209 ret = res;
210 } else {
211 pr_info("nand_bbt: error reading BBT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return res;
213 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 /* Analyse data */
217 for (i = 0; i < len; i++) {
218 uint8_t dat = buf[i];
Brian Norrisb4d20d62013-07-30 17:52:56 -0700219 for (j = 0; j < 8; j += bits, act++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 uint8_t tmp = (dat >> j) & msk;
221 if (tmp == msk)
222 continue;
David Woodhousee0c7d762006-05-13 18:07:53 +0100223 if (reserved_block_code && (tmp == reserved_block_code)) {
Brian Norrisd0370212011-07-19 10:06:08 -0700224 pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
Brian Norrisb4d20d62013-07-30 17:52:56 -0700225 (loff_t)(offs + act) <<
226 this->bbt_erase_shift);
227 bbt_mark_entry(this, offs + act,
Brian Norris771c5682013-07-30 17:52:55 -0700228 BBT_BLOCK_RESERVED);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200229 mtd->ecc_stats.bbtblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 continue;
231 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700232 /*
233 * Leave it for now, if it's matured we can
Brian Norrisa0f50802011-07-19 10:06:06 -0700234 * move this message to pr_debug.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700235 */
Brian Norrisd0370212011-07-19 10:06:08 -0700236 pr_info("nand_read_bbt: bad block at 0x%012llx\n",
Brian Norrisb4d20d62013-07-30 17:52:56 -0700237 (loff_t)(offs + act) <<
238 this->bbt_erase_shift);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700239 /* Factory marked bad or worn out? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (tmp == 0)
Brian Norrisb4d20d62013-07-30 17:52:56 -0700241 bbt_mark_entry(this, offs + act,
Brian Norris771c5682013-07-30 17:52:55 -0700242 BBT_BLOCK_FACTORY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 else
Brian Norrisb4d20d62013-07-30 17:52:56 -0700244 bbt_mark_entry(this, offs + act,
Brian Norris771c5682013-07-30 17:52:55 -0700245 BBT_BLOCK_WORN);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200246 mtd->ecc_stats.badblocks++;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249 totlen -= len;
250 from += len;
251 }
Brian Norris167a8d52011-09-20 18:35:08 -0700252 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255/**
256 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
Brian Norris8b6e50c2011-05-25 14:59:01 -0700257 * @mtd: MTD device structure
258 * @buf: temporary buffer
259 * @td: descriptor for the bad block table
Brian Norris596d7452011-09-07 13:13:33 -0700260 * @chip: read the table for a specific chip, -1 read all chips; applies only if
Brian Norris8b6e50c2011-05-25 14:59:01 -0700261 * NAND_BBT_PERCHIP option is set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700263 * Read the bad block table for all chips starting at a given page. We assume
264 * that the bbt bits are in consecutive order.
265 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100266static 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 -0700267{
268 struct nand_chip *this = mtd->priv;
269 int res = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (td->options & NAND_BBT_PERCHIP) {
272 int offs = 0;
273 for (i = 0; i < this->numchips; i++) {
274 if (chip == -1 || chip == i)
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200275 res = read_bbt(mtd, buf, td->pages[i],
276 this->chipsize >> this->bbt_erase_shift,
277 td, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (res)
279 return res;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700280 offs += this->chipsize >> this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282 } else {
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200283 res = read_bbt(mtd, buf, td->pages[0],
284 mtd->size >> this->bbt_erase_shift, td, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (res)
286 return res;
287 }
288 return 0;
289}
290
Brian Norris8b6e50c2011-05-25 14:59:01 -0700291/* BBT marker is in the first page, no OOB */
Brian Norrisaf69dcd2012-06-22 16:35:42 -0700292static int scan_read_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200293 struct nand_bbt_descr *td)
294{
295 size_t retlen;
296 size_t len;
297
298 len = td->len;
299 if (td->options & NAND_BBT_VERSION)
300 len++;
301
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200302 return mtd_read(mtd, offs, len, &retlen, buf);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200303}
304
Brian Norrisa7e68832012-06-22 16:35:45 -0700305/**
Brian Norrisaf69dcd2012-06-22 16:35:42 -0700306 * scan_read_oob - [GENERIC] Scan data+OOB region to buffer
Brian Norrisa7e68832012-06-22 16:35:45 -0700307 * @mtd: MTD device structure
308 * @buf: temporary buffer
309 * @offs: offset at which to scan
310 * @len: length of data region to read
311 *
312 * Scan read data from data+OOB. May traverse multiple pages, interleaving
313 * page,OOB,page,OOB,... in buf. Completes transfer and returns the "strongest"
314 * ECC condition (error or bitflip). May quit on the first (non-ECC) error.
315 */
Brian Norrisaf69dcd2012-06-22 16:35:42 -0700316static int scan_read_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200317 size_t len)
318{
319 struct mtd_oob_ops ops;
Brian Norrisa7e68832012-06-22 16:35:45 -0700320 int res, ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200321
Brian Norrisa7e68832012-06-22 16:35:45 -0700322 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200323 ops.ooboffs = 0;
324 ops.ooblen = mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200325
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200326 while (len > 0) {
Brian Norris105513c2011-09-07 13:13:28 -0700327 ops.datbuf = buf;
328 ops.len = min(len, (size_t)mtd->writesize);
329 ops.oobbuf = buf + ops.len;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200330
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200331 res = mtd_read_oob(mtd, offs, &ops);
Brian Norrisa7e68832012-06-22 16:35:45 -0700332 if (res) {
333 if (!mtd_is_bitflip_or_eccerr(res))
334 return res;
335 else if (mtd_is_eccerr(res) || !ret)
336 ret = res;
337 }
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200338
339 buf += mtd->oobsize + mtd->writesize;
340 len -= mtd->writesize;
Dmitry Maluka34a57042012-05-11 20:51:51 +0300341 offs += mtd->writesize;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200342 }
Brian Norrisa7e68832012-06-22 16:35:45 -0700343 return ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200344}
345
Brian Norrisaf69dcd2012-06-22 16:35:42 -0700346static int scan_read(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200347 size_t len, struct nand_bbt_descr *td)
348{
349 if (td->options & NAND_BBT_NO_OOB)
Brian Norrisaf69dcd2012-06-22 16:35:42 -0700350 return scan_read_data(mtd, buf, offs, td);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200351 else
Brian Norrisaf69dcd2012-06-22 16:35:42 -0700352 return scan_read_oob(mtd, buf, offs, len);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200353}
354
Brian Norris8b6e50c2011-05-25 14:59:01 -0700355/* Scan write data with oob to flash */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200356static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
357 uint8_t *buf, uint8_t *oob)
358{
359 struct mtd_oob_ops ops;
360
Brian Norris0612b9d2011-08-30 18:45:40 -0700361 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200362 ops.ooboffs = 0;
363 ops.ooblen = mtd->oobsize;
364 ops.datbuf = buf;
365 ops.oobbuf = oob;
366 ops.len = len;
367
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200368 return mtd_write_oob(mtd, offs, &ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200369}
370
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200371static u32 bbt_get_ver_offs(struct mtd_info *mtd, struct nand_bbt_descr *td)
372{
373 u32 ver_offs = td->veroffs;
374
375 if (!(td->options & NAND_BBT_NO_OOB))
376 ver_offs += mtd->writesize;
377 return ver_offs;
378}
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380/**
381 * 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 -0700382 * @mtd: MTD device structure
383 * @buf: temporary buffer
384 * @td: descriptor for the bad block table
385 * @md: descriptor for the bad block table mirror
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700387 * Read the bad block table(s) for all chips starting at a given page. We
388 * assume that the bbt bits are in consecutive order.
389 */
Brian Norris7b5a2d42012-06-22 16:35:41 -0700390static void read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
391 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 struct nand_chip *this = mtd->priv;
394
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000395 /* Read the primary version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (td->options & NAND_BBT_VERSION) {
Brian Norrisaf69dcd2012-06-22 16:35:42 -0700397 scan_read(mtd, buf, (loff_t)td->pages[0] << this->page_shift,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200398 mtd->writesize, td);
399 td->version[0] = buf[bbt_get_ver_offs(mtd, td)];
Brian Norris9a4d4d62011-07-19 10:06:07 -0700400 pr_info("Bad block table at page %d, version 0x%02X\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700401 td->pages[0], td->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000404 /* Read the mirror version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (md && (md->options & NAND_BBT_VERSION)) {
Brian Norrisaf69dcd2012-06-22 16:35:42 -0700406 scan_read(mtd, buf, (loff_t)md->pages[0] << this->page_shift,
Shmulik Ladkani7bb9c752012-06-10 13:58:12 +0300407 mtd->writesize, md);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200408 md->version[0] = buf[bbt_get_ver_offs(mtd, md)];
Brian Norris9a4d4d62011-07-19 10:06:07 -0700409 pr_info("Bad block table at page %d, version 0x%02X\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700410 md->pages[0], md->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Brian Norris8b6e50c2011-05-25 14:59:01 -0700414/* Scan a given block partially */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200415static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300416 loff_t offs, uint8_t *buf, int numpages)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200417{
418 struct mtd_oob_ops ops;
419 int j, ret;
420
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200421 ops.ooblen = mtd->oobsize;
422 ops.oobbuf = buf;
423 ops.ooboffs = 0;
424 ops.datbuf = NULL;
Brian Norris0612b9d2011-08-30 18:45:40 -0700425 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200426
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300427 for (j = 0; j < numpages; j++) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200428 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700429 * Read the full oob until read_oob is fixed to handle single
430 * byte reads for 16 bit buswidth.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200431 */
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200432 ret = mtd_read_oob(mtd, offs, &ops);
Brian Norris903cd062011-06-28 16:28:58 -0700433 /* Ignore ECC errors when checking for BBM */
Brian Norrisd57f40542011-09-20 18:34:25 -0700434 if (ret && !mtd_is_bitflip_or_eccerr(ret))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200435 return ret;
436
437 if (check_short_pattern(buf, bd))
438 return 1;
439
440 offs += mtd->writesize;
441 }
442 return 0;
443}
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445/**
446 * create_bbt - [GENERIC] Create a bad block table by scanning the device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700447 * @mtd: MTD device structure
448 * @buf: temporary buffer
449 * @bd: descriptor for the good/bad block search pattern
450 * @chip: create the table for a specific chip, -1 read all chips; applies only
451 * if NAND_BBT_PERCHIP option is set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700453 * Create a bad block table by scanning the device for the given good/bad block
454 * identify pattern.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200456static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
457 struct nand_bbt_descr *bd, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 struct nand_chip *this = mtd->priv;
Brian Norris5961ad22013-10-30 00:41:30 -0400460 int i, numblocks, numpages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 int startblock;
462 loff_t from;
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
Brian Norris5961ad22013-10-30 00:41:30 -0400466 if (bd->options & NAND_BBT_SCAN2NDPAGE)
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300467 numpages = 2;
Brian Norris58373ff2010-07-15 12:15:44 -0700468 else
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300469 numpages = 1;
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (chip == -1) {
Brian Norrisb4d20d62013-07-30 17:52:56 -0700472 numblocks = mtd->size >> this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 startblock = 0;
474 from = 0;
475 } else {
476 if (chip >= this->numchips) {
Brian Norris9a4d4d62011-07-19 10:06:07 -0700477 pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
David Woodhousee0c7d762006-05-13 18:07:53 +0100478 chip + 1, this->numchips);
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000479 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
Brian Norrisb4d20d62013-07-30 17:52:56 -0700481 numblocks = this->chipsize >> this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 startblock = chip * numblocks;
483 numblocks += startblock;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700484 from = (loff_t)startblock << this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000486
Brian Norris5fb15492011-05-31 16:31:21 -0700487 if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300488 from += mtd->erasesize - (mtd->writesize * numpages);
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700489
Brian Norrisb4d20d62013-07-30 17:52:56 -0700490 for (i = startblock; i < numblocks; i++) {
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000491 int ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000492
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200493 BUG_ON(bd->options & NAND_BBT_NO_OOB);
494
Brian Norris5961ad22013-10-30 00:41:30 -0400495 ret = scan_block_fast(mtd, bd, from, buf, numpages);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200496 if (ret < 0)
497 return ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000498
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200499 if (ret) {
Brian Norrisb4d20d62013-07-30 17:52:56 -0700500 bbt_mark_entry(this, i, BBT_BLOCK_FACTORY_BAD);
Brian Norris9a4d4d62011-07-19 10:06:07 -0700501 pr_warn("Bad eraseblock %d at 0x%012llx\n",
Brian Norrisb4d20d62013-07-30 17:52:56 -0700502 i, (unsigned long long)from);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200503 mtd->ecc_stats.badblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 from += (1 << this->bbt_erase_shift);
507 }
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000508 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
511/**
512 * search_bbt - [GENERIC] scan the device for a specific bad block table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700513 * @mtd: MTD device structure
514 * @buf: temporary buffer
515 * @td: descriptor for the bad block table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700517 * Read the bad block table by searching for a given ident pattern. Search is
518 * preformed either from the beginning up or from the end of the device
519 * downwards. The search starts always at the start of a block. If the option
520 * NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains
521 * the bad block information of this chip. This is necessary to provide support
522 * for certain DOC devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700524 * The bbt ident pattern resides in the oob area of the first page in a block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100526static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 struct nand_chip *this = mtd->priv;
529 int i, chips;
Brian Norris930de532014-01-02 17:14:10 -0800530 int startblock, block, dir;
Joern Engel28318772006-05-22 23:18:05 +0200531 int scanlen = mtd->writesize + mtd->oobsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 int bbtblocks;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200533 int blocktopage = this->bbt_erase_shift - this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Brian Norris8b6e50c2011-05-25 14:59:01 -0700535 /* Search direction top -> down? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (td->options & NAND_BBT_LASTBLOCK) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100537 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 dir = -1;
539 } else {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000540 startblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000542 }
543
Brian Norris8b6e50c2011-05-25 14:59:01 -0700544 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (td->options & NAND_BBT_PERCHIP) {
546 chips = this->numchips;
547 bbtblocks = this->chipsize >> this->bbt_erase_shift;
548 startblock &= bbtblocks - 1;
549 } else {
550 chips = 1;
551 bbtblocks = mtd->size >> this->bbt_erase_shift;
552 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 for (i = 0; i < chips; i++) {
555 /* Reset version information */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000556 td->version[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 td->pages[i] = -1;
558 /* Scan the maximum number of blocks */
559 for (block = 0; block < td->maxblocks; block++) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 int actblock = startblock + dir * block;
Adrian Hunter69423d92008-12-10 13:37:21 +0000562 loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /* Read first page */
Brian Norrisaf69dcd2012-06-22 16:35:42 -0700565 scan_read(mtd, buf, offs, mtd->writesize, td);
Joern Engel28318772006-05-22 23:18:05 +0200566 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200567 td->pages[i] = actblock << blocktopage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (td->options & NAND_BBT_VERSION) {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200569 offs = bbt_get_ver_offs(mtd, td);
570 td->version[i] = buf[offs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572 break;
573 }
574 }
575 startblock += this->chipsize >> this->bbt_erase_shift;
576 }
577 /* Check, if we found a bbt for each requested chip */
578 for (i = 0; i < chips; i++) {
579 if (td->pages[i] == -1)
Brian Norris9a4d4d62011-07-19 10:06:07 -0700580 pr_warn("Bad block table not found for chip %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 else
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200582 pr_info("Bad block table found at page %d, version 0x%02X\n",
583 td->pages[i], td->version[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000585 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
588/**
589 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -0700590 * @mtd: MTD device structure
591 * @buf: temporary buffer
592 * @td: descriptor for the bad block table
593 * @md: descriptor for the bad block table mirror
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700595 * Search and read the bad block table(s).
596 */
Brian Norris7b5a2d42012-06-22 16:35:41 -0700597static void search_read_bbts(struct mtd_info *mtd, uint8_t *buf,
598 struct nand_bbt_descr *td,
599 struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 /* Search the primary table */
David Woodhousee0c7d762006-05-13 18:07:53 +0100602 search_bbt(mtd, buf, td);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /* Search the mirror table */
605 if (md)
David Woodhousee0c7d762006-05-13 18:07:53 +0100606 search_bbt(mtd, buf, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000607}
608
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000609/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 * write_bbt - [GENERIC] (Re)write the bad block table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700611 * @mtd: MTD device structure
612 * @buf: temporary buffer
613 * @td: descriptor for the bad block table
614 * @md: descriptor for the bad block table mirror
615 * @chipsel: selector for a specific chip, -1 for all
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700617 * (Re)write the bad block table.
618 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100619static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
Thomas Gleixner9223a452006-05-23 17:21:03 +0200620 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
621 int chipsel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 struct nand_chip *this = mtd->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 struct erase_info einfo;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700625 int i, res, chip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700627 int nrchips, pageoffs, ooboffs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 uint8_t msk[4];
629 uint8_t rcode = td->reserved_block_code;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200630 size_t retlen, len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 loff_t to;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200632 struct mtd_oob_ops ops;
633
634 ops.ooblen = mtd->oobsize;
635 ops.ooboffs = 0;
636 ops.datbuf = NULL;
Brian Norris0612b9d2011-08-30 18:45:40 -0700637 ops.mode = MTD_OPS_PLACE_OOB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 if (!rcode)
640 rcode = 0xff;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700641 /* Write bad block table per chip rather than per device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (td->options & NAND_BBT_PERCHIP) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100643 numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700644 /* Full device write or specific chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (chipsel == -1) {
646 nrchips = this->numchips;
647 } else {
648 nrchips = chipsel + 1;
649 chip = chipsel;
650 }
651 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +0100652 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 nrchips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000654 }
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /* Loop through the chips */
657 for (; chip < nrchips; chip++) {
Brian Norris8b6e50c2011-05-25 14:59:01 -0700658 /*
659 * There was already a version of the table, reuse the page
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000660 * This applies for absolute placement too, as we have the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 * page nr. in td->pages.
662 */
663 if (td->pages[chip] != -1) {
664 page = td->pages[chip];
665 goto write;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Brian Norris8b6e50c2011-05-25 14:59:01 -0700668 /*
669 * Automatic placement of the bad block table. Search direction
670 * top -> down?
671 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (td->options & NAND_BBT_LASTBLOCK) {
673 startblock = numblocks * (chip + 1) - 1;
674 dir = -1;
675 } else {
676 startblock = chip * numblocks;
677 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 for (i = 0; i < td->maxblocks; i++) {
681 int block = startblock + dir * i;
682 /* Check, if the block is bad */
Brian Norris771c5682013-07-30 17:52:55 -0700683 switch (bbt_get_entry(this, block)) {
684 case BBT_BLOCK_WORN:
685 case BBT_BLOCK_FACTORY_BAD:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 continue;
687 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200688 page = block <<
689 (this->bbt_erase_shift - this->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /* Check, if the block is used by the mirror table */
691 if (!md || md->pages[chip] != page)
692 goto write;
693 }
Brian Norris9a4d4d62011-07-19 10:06:07 -0700694 pr_err("No space left to write bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return -ENOSPC;
David Woodhousee0c7d762006-05-13 18:07:53 +0100696 write:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 /* Set up shift count and masks for the flash table */
699 bits = td->options & NAND_BBT_NRBITS_MSK;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200700 msk[2] = ~rcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 switch (bits) {
Thomas Gleixner9223a452006-05-23 17:21:03 +0200702 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
703 msk[3] = 0x01;
704 break;
705 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
706 msk[3] = 0x03;
707 break;
708 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
709 msk[3] = 0x0f;
710 break;
711 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
712 msk[3] = 0xff;
713 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 default: return -EINVAL;
715 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000716
Brian Norris596d7452011-09-07 13:13:33 -0700717 to = ((loff_t)page) << this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Brian Norris8b6e50c2011-05-25 14:59:01 -0700719 /* Must we save the block contents? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (td->options & NAND_BBT_SAVECONTENT) {
721 /* Make it block aligned */
Brian Norris596d7452011-09-07 13:13:33 -0700722 to &= ~((loff_t)((1 << this->bbt_erase_shift) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 len = 1 << this->bbt_erase_shift;
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200724 res = mtd_read(mtd, to, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (res < 0) {
726 if (retlen != len) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200727 pr_info("nand_bbt: error reading block for writing the bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return res;
729 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200730 pr_warn("nand_bbt: ECC error while reading block for writing bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200732 /* Read oob data */
Vitaly Wool70145682006-11-03 18:20:38 +0300733 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200734 ops.oobbuf = &buf[len];
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200735 res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
Vitaly Wool70145682006-11-03 18:20:38 +0300736 if (res < 0 || ops.oobretlen != ops.ooblen)
Thomas Gleixner9223a452006-05-23 17:21:03 +0200737 goto outerr;
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 /* Calc the byte offset in the buffer */
740 pageoffs = page - (int)(to >> this->page_shift);
741 offs = pageoffs << this->page_shift;
742 /* Preset the bbt area with 0xff */
Brian Norris596d7452011-09-07 13:13:33 -0700743 memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
Thomas Gleixner9223a452006-05-23 17:21:03 +0200744 ooboffs = len + (pageoffs * mtd->oobsize);
745
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200746 } else if (td->options & NAND_BBT_NO_OOB) {
747 ooboffs = 0;
748 offs = td->len;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700749 /* The version byte */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200750 if (td->options & NAND_BBT_VERSION)
751 offs++;
752 /* Calc length */
Brian Norris596d7452011-09-07 13:13:33 -0700753 len = (size_t)(numblocks >> sft);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200754 len += offs;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700755 /* Make it page aligned! */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200756 len = ALIGN(len, mtd->writesize);
757 /* Preset the buffer with 0xff */
758 memset(buf, 0xff, len);
759 /* Pattern is located at the begin of first page */
760 memcpy(buf, td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 } else {
762 /* Calc length */
Brian Norris596d7452011-09-07 13:13:33 -0700763 len = (size_t)(numblocks >> sft);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700764 /* Make it page aligned! */
Sebastian Andrzej Siewiorcda32092010-09-29 19:43:50 +0200765 len = ALIGN(len, mtd->writesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 /* Preset the buffer with 0xff */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200767 memset(buf, 0xff, len +
768 (len >> this->page_shift)* mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 offs = 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200770 ooboffs = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 /* Pattern is located in oob area of first page */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200772 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000774
Thomas Gleixner9223a452006-05-23 17:21:03 +0200775 if (td->options & NAND_BBT_VERSION)
776 buf[ooboffs + td->veroffs] = td->version[chip];
777
Brian Norris8b6e50c2011-05-25 14:59:01 -0700778 /* Walk through the memory table */
Brian Norrisb4d20d62013-07-30 17:52:56 -0700779 for (i = 0; i < numblocks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 uint8_t dat;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700781 int sftcnt = (i << (3 - sft)) & sftmsk;
782 dat = bbt_get_entry(this, chip * numblocks + i);
783 /* Do not store the reserved bbt blocks! */
784 buf[offs + (i >> sft)] &= ~(msk[dat] << sftcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000786
David Woodhousee0c7d762006-05-13 18:07:53 +0100787 memset(&einfo, 0, sizeof(einfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 einfo.mtd = mtd;
Adrian Hunter69423d92008-12-10 13:37:21 +0000789 einfo.addr = to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 einfo.len = 1 << this->bbt_erase_shift;
David Woodhousee0c7d762006-05-13 18:07:53 +0100791 res = nand_erase_nand(mtd, &einfo, 1);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200792 if (res < 0)
793 goto outerr;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000794
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200795 res = scan_write_bbt(mtd, to, len, buf,
796 td->options & NAND_BBT_NO_OOB ? NULL :
797 &buf[len]);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200798 if (res < 0)
799 goto outerr;
800
Brian Norrisd0370212011-07-19 10:06:08 -0700801 pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
802 (unsigned long long)to, td->version[chip]);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /* Mark it as used */
805 td->pages[chip] = page;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200808
809 outerr:
Brian Norrisd0370212011-07-19 10:06:08 -0700810 pr_warn("nand_bbt: error while writing bad block table %d\n", res);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200811 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
814/**
815 * nand_memory_bbt - [GENERIC] create a memory based bad block table
Brian Norris8b6e50c2011-05-25 14:59:01 -0700816 * @mtd: MTD device structure
817 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700819 * The function creates a memory based bbt by scanning the device for
820 * manufacturer / software marked good / bad blocks.
821 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100822static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 struct nand_chip *this = mtd->priv;
825
David Woodhouse4bf63fc2006-09-25 17:08:04 +0100826 return create_bbt(mtd, this->buffers->databuf, bd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
829/**
David Woodhousee0c7d762006-05-13 18:07:53 +0100830 * check_create - [GENERIC] create and write bbt(s) if necessary
Brian Norris8b6e50c2011-05-25 14:59:01 -0700831 * @mtd: MTD device structure
832 * @buf: temporary buffer
833 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700835 * The function checks the results of the previous call to read_bbt and creates
836 * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found
837 * for the chip/device. Update is necessary if one of the tables is missing or
838 * the version nr. of one table is less than the other.
839 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100840static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Brian Norris623978d2011-09-20 18:35:34 -0700842 int i, chips, writeops, create, chipsel, res, res2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 struct nand_chip *this = mtd->priv;
844 struct nand_bbt_descr *td = this->bbt_td;
845 struct nand_bbt_descr *md = this->bbt_md;
846 struct nand_bbt_descr *rd, *rd2;
847
Brian Norris8b6e50c2011-05-25 14:59:01 -0700848 /* Do we have a bbt per chip? */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000849 if (td->options & NAND_BBT_PERCHIP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 chips = this->numchips;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000851 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 chips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 for (i = 0; i < chips; i++) {
855 writeops = 0;
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700856 create = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 rd = NULL;
858 rd2 = NULL;
Brian Norris623978d2011-09-20 18:35:34 -0700859 res = res2 = 0;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700860 /* Per chip or per device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700862 /* Mirrored table available? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (md) {
864 if (td->pages[i] == -1 && md->pages[i] == -1) {
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700865 create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 writeops = 0x03;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700867 } else if (td->pages[i] == -1) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000868 rd = md;
Brian Norris596d7452011-09-07 13:13:33 -0700869 writeops = 0x01;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700870 } else if (md->pages[i] == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 rd = td;
Brian Norris596d7452011-09-07 13:13:33 -0700872 writeops = 0x02;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700873 } else if (td->version[i] == md->version[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 rd = td;
875 if (!(td->options & NAND_BBT_VERSION))
876 rd2 = md;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700877 } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 rd = td;
Brian Norris596d7452011-09-07 13:13:33 -0700879 writeops = 0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 } else {
881 rd = md;
Brian Norris596d7452011-09-07 13:13:33 -0700882 writeops = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 } else {
885 if (td->pages[i] == -1) {
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700886 create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 writeops = 0x01;
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700888 } else {
889 rd = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000892
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700893 if (create) {
894 /* Create the bad block table by scanning the device? */
895 if (!(td->options & NAND_BBT_CREATE))
896 continue;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000897
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700898 /* Create the table in memory by scanning the chip(s) */
899 if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
900 create_bbt(mtd, buf, bd, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700902 td->version[i] = 1;
903 if (md)
904 md->version[i] = 1;
905 }
906
Brian Norris8b6e50c2011-05-25 14:59:01 -0700907 /* Read back first? */
Brian Norris623978d2011-09-20 18:35:34 -0700908 if (rd) {
909 res = read_abs_bbt(mtd, buf, rd, chipsel);
910 if (mtd_is_eccerr(res)) {
911 /* Mark table as invalid */
912 rd->pages[i] = -1;
Brian Norrisdadc17a2011-09-20 18:35:57 -0700913 rd->version[i] = 0;
Brian Norris623978d2011-09-20 18:35:34 -0700914 i--;
915 continue;
916 }
917 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700918 /* If they weren't versioned, read both */
Brian Norris623978d2011-09-20 18:35:34 -0700919 if (rd2) {
920 res2 = read_abs_bbt(mtd, buf, rd2, chipsel);
921 if (mtd_is_eccerr(res2)) {
922 /* Mark table as invalid */
923 rd2->pages[i] = -1;
Brian Norrisdadc17a2011-09-20 18:35:57 -0700924 rd2->version[i] = 0;
Brian Norris623978d2011-09-20 18:35:34 -0700925 i--;
926 continue;
927 }
928 }
929
930 /* Scrub the flash table(s)? */
931 if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
932 writeops = 0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Brian Norrisdadc17a2011-09-20 18:35:57 -0700934 /* Update version numbers before writing */
935 if (md) {
936 td->version[i] = max(td->version[i], md->version[i]);
937 md->version[i] = td->version[i];
938 }
939
Brian Norris8b6e50c2011-05-25 14:59:01 -0700940 /* Write the bad block table to the device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100942 res = write_bbt(mtd, buf, td, md, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (res < 0)
944 return res;
945 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000946
Brian Norris8b6e50c2011-05-25 14:59:01 -0700947 /* Write the mirror bad block table to the device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100949 res = write_bbt(mtd, buf, md, td, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (res < 0)
951 return res;
952 }
953 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000954 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
957/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000958 * mark_bbt_regions - [GENERIC] mark the bad block table regions
Brian Norris8b6e50c2011-05-25 14:59:01 -0700959 * @mtd: MTD device structure
960 * @td: bad block table descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700962 * The bad block table regions are marked as "bad" to prevent accidental
963 * erasures / writes. The regions are identified by the mark 0x02.
964 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100965static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
967 struct nand_chip *this = mtd->priv;
968 int i, j, chips, block, nrblocks, update;
Brian Norris771c5682013-07-30 17:52:55 -0700969 uint8_t oldval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Brian Norris8b6e50c2011-05-25 14:59:01 -0700971 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (td->options & NAND_BBT_PERCHIP) {
973 chips = this->numchips;
974 nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
975 } else {
976 chips = 1;
977 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000978 }
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 for (i = 0; i < chips; i++) {
981 if ((td->options & NAND_BBT_ABSPAGE) ||
982 !(td->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100983 if (td->pages[i] == -1)
984 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
Brian Norrisb4d20d62013-07-30 17:52:56 -0700986 oldval = bbt_get_entry(this, block);
987 bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
Brian Norris771c5682013-07-30 17:52:55 -0700988 if ((oldval != BBT_BLOCK_RESERVED) &&
989 td->reserved_block_code)
Brian Norrisb4d20d62013-07-30 17:52:56 -0700990 nand_update_bbt(mtd, (loff_t)block <<
991 this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 continue;
993 }
994 update = 0;
995 if (td->options & NAND_BBT_LASTBLOCK)
996 block = ((i + 1) * nrblocks) - td->maxblocks;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000997 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 block = i * nrblocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 for (j = 0; j < td->maxblocks; j++) {
Brian Norrisb4d20d62013-07-30 17:52:56 -07001000 oldval = bbt_get_entry(this, block);
1001 bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
Brian Norris771c5682013-07-30 17:52:55 -07001002 if (oldval != BBT_BLOCK_RESERVED)
David Woodhousee0c7d762006-05-13 18:07:53 +01001003 update = 1;
Brian Norrisb4d20d62013-07-30 17:52:56 -07001004 block++;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001005 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07001006 /*
1007 * If we want reserved blocks to be recorded to flash, and some
1008 * new ones have been marked, then we need to update the stored
1009 * bbts. This should only happen once.
1010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (update && td->reserved_block_code)
Brian Norrisb4d20d62013-07-30 17:52:56 -07001012 nand_update_bbt(mtd, (loff_t)(block - 1) <<
1013 this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015}
1016
1017/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001018 * verify_bbt_descr - verify the bad block description
Brian Norris8b6e50c2011-05-25 14:59:01 -07001019 * @mtd: MTD device structure
1020 * @bd: the table to verify
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001021 *
1022 * This functions performs a few sanity checks on the bad block description
1023 * table.
1024 */
1025static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
1026{
1027 struct nand_chip *this = mtd->priv;
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001028 u32 pattern_len;
1029 u32 bits;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001030 u32 table_size;
1031
1032 if (!bd)
1033 return;
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001034
1035 pattern_len = bd->len;
1036 bits = bd->options & NAND_BBT_NRBITS_MSK;
1037
Brian Norrisa40f7342011-05-31 16:31:22 -07001038 BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001039 !(this->bbt_options & NAND_BBT_USE_FLASH));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001040 BUG_ON(!bits);
1041
1042 if (bd->options & NAND_BBT_VERSION)
1043 pattern_len++;
1044
1045 if (bd->options & NAND_BBT_NO_OOB) {
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001046 BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
Brian Norrisa40f7342011-05-31 16:31:22 -07001047 BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001048 BUG_ON(bd->offs);
1049 if (bd->options & NAND_BBT_VERSION)
1050 BUG_ON(bd->veroffs != bd->len);
1051 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1052 }
1053
1054 if (bd->options & NAND_BBT_PERCHIP)
1055 table_size = this->chipsize >> this->bbt_erase_shift;
1056 else
1057 table_size = mtd->size >> this->bbt_erase_shift;
1058 table_size >>= 3;
1059 table_size *= bits;
1060 if (bd->options & NAND_BBT_NO_OOB)
1061 table_size += pattern_len;
1062 BUG_ON(table_size > (1 << this->bbt_erase_shift));
1063}
1064
1065/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07001067 * @mtd: MTD device structure
1068 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001070 * The function checks, if a bad block table(s) is/are already available. If
1071 * not it scans the device for manufacturer marked good / bad blocks and writes
1072 * the bad block table(s) to the selected place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001074 * The bad block table memory is allocated here. It must be freed by calling
1075 * the nand_free_bbt function.
1076 */
Brian Norris17799352015-02-28 02:13:11 -08001077static int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
1079 struct nand_chip *this = mtd->priv;
1080 int len, res = 0;
1081 uint8_t *buf;
1082 struct nand_bbt_descr *td = this->bbt_td;
1083 struct nand_bbt_descr *md = this->bbt_md;
1084
1085 len = mtd->size >> (this->bbt_erase_shift + 2);
Brian Norris8b6e50c2011-05-25 14:59:01 -07001086 /*
1087 * Allocate memory (2bit per block) and clear the memory bad block
1088 * table.
1089 */
Burman Yan95b93a02006-11-15 21:10:29 +02001090 this->bbt = kzalloc(len, GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001091 if (!this->bbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Brian Norris8b6e50c2011-05-25 14:59:01 -07001094 /*
1095 * If no primary table decriptor is given, scan the device to build a
1096 * memory based bad block table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 */
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001098 if (!td) {
1099 if ((res = nand_memory_bbt(mtd, bd))) {
Brian Norrisd0370212011-07-19 10:06:08 -07001100 pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
David Woodhousee0c7d762006-05-13 18:07:53 +01001101 kfree(this->bbt);
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001102 this->bbt = NULL;
1103 }
1104 return res;
1105 }
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001106 verify_bbt_descr(mtd, td);
1107 verify_bbt_descr(mtd, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 /* Allocate a temporary buffer for one eraseblock incl. oob */
1110 len = (1 << this->bbt_erase_shift);
1111 len += (len >> this->page_shift) * mtd->oobsize;
David Woodhousec3f8abf2006-05-13 04:03:42 +01001112 buf = vmalloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (!buf) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001114 kfree(this->bbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 this->bbt = NULL;
1116 return -ENOMEM;
1117 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001118
Brian Norris8b6e50c2011-05-25 14:59:01 -07001119 /* Is the bbt at a given page? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (td->options & NAND_BBT_ABSPAGE) {
Brian Norris7b5a2d42012-06-22 16:35:41 -07001121 read_abs_bbts(mtd, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001122 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 /* Search the bad block table using a pattern in oob */
Brian Norris7b5a2d42012-06-22 16:35:41 -07001124 search_read_bbts(mtd, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Brian Norris7b5a2d42012-06-22 16:35:41 -07001127 res = check_create(mtd, buf, bd);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 /* Prevent the bbt regions from erasing / writing */
David Woodhousee0c7d762006-05-13 18:07:53 +01001130 mark_bbt_region(mtd, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (md)
David Woodhousee0c7d762006-05-13 18:07:53 +01001132 mark_bbt_region(mtd, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001133
David Woodhousee0c7d762006-05-13 18:07:53 +01001134 vfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return res;
1136}
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138/**
Brian Norrisb32843b2013-07-30 17:52:59 -07001139 * nand_update_bbt - update bad block table(s)
Brian Norris8b6e50c2011-05-25 14:59:01 -07001140 * @mtd: MTD device structure
1141 * @offs: the offset of the newly marked block
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001143 * The function updates the bad block table(s).
1144 */
Brian Norrisb32843b2013-07-30 17:52:59 -07001145static int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 struct nand_chip *this = mtd->priv;
Brian Norris1196ac52011-09-07 13:13:32 -07001148 int len, res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 int chip, chipsel;
1150 uint8_t *buf;
1151 struct nand_bbt_descr *td = this->bbt_td;
1152 struct nand_bbt_descr *md = this->bbt_md;
1153
1154 if (!this->bbt || !td)
1155 return -EINVAL;
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 /* Allocate a temporary buffer for one eraseblock incl. oob */
1158 len = (1 << this->bbt_erase_shift);
1159 len += (len >> this->page_shift) * mtd->oobsize;
David Woodhousee0c7d762006-05-13 18:07:53 +01001160 buf = kmalloc(len, GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001161 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return -ENOMEM;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001163
Brian Norris8b6e50c2011-05-25 14:59:01 -07001164 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (td->options & NAND_BBT_PERCHIP) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001166 chip = (int)(offs >> this->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 chipsel = chip;
1168 } else {
1169 chip = 0;
1170 chipsel = -1;
1171 }
1172
1173 td->version[chip]++;
1174 if (md)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001175 md->version[chip]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Brian Norris8b6e50c2011-05-25 14:59:01 -07001177 /* Write the bad block table to the device? */
Brian Norris1196ac52011-09-07 13:13:32 -07001178 if (td->options & NAND_BBT_WRITE) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001179 res = write_bbt(mtd, buf, td, md, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (res < 0)
1181 goto out;
1182 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07001183 /* Write the mirror bad block table to the device? */
Brian Norris1196ac52011-09-07 13:13:32 -07001184 if (md && (md->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001185 res = write_bbt(mtd, buf, md, td, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187
David Woodhousee0c7d762006-05-13 18:07:53 +01001188 out:
1189 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return res;
1191}
1192
Brian Norris8b6e50c2011-05-25 14:59:01 -07001193/*
1194 * Define some generic bad / good block scan pattern which are used
1195 * while scanning a device for factory marked good / bad blocks.
1196 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1198
Brian Norris7854d3f2011-06-23 14:12:08 -07001199/* Generic flash bbt descriptors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1201static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1202
1203static struct nand_bbt_descr bbt_main_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001204 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1206 .offs = 8,
1207 .len = 4,
1208 .veroffs = 12,
Richard Genoud61de9da2012-09-11 15:50:54 +02001209 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 .pattern = bbt_pattern
1211};
1212
1213static struct nand_bbt_descr bbt_mirror_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001214 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1216 .offs = 8,
1217 .len = 4,
1218 .veroffs = 12,
Richard Genoud61de9da2012-09-11 15:50:54 +02001219 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 .pattern = mirror_pattern
1221};
1222
Brian Norris9fd6b372012-06-22 16:35:40 -07001223static struct nand_bbt_descr bbt_main_no_oob_descr = {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001224 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1225 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1226 | NAND_BBT_NO_OOB,
1227 .len = 4,
1228 .veroffs = 4,
Richard Genoud61de9da2012-09-11 15:50:54 +02001229 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001230 .pattern = bbt_pattern
1231};
1232
Brian Norris9fd6b372012-06-22 16:35:40 -07001233static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001234 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1235 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1236 | NAND_BBT_NO_OOB,
1237 .len = 4,
1238 .veroffs = 4,
Richard Genoud61de9da2012-09-11 15:50:54 +02001239 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001240 .pattern = mirror_pattern
1241};
1242
Brian Norris752ed6c2011-09-20 18:36:42 -07001243#define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
Brian Norris58373ff2010-07-15 12:15:44 -07001244/**
Brian Norris752ed6c2011-09-20 18:36:42 -07001245 * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001246 * @this: NAND chip to create descriptor for
Brian Norris58373ff2010-07-15 12:15:44 -07001247 *
1248 * This function allocates and initializes a nand_bbt_descr for BBM detection
Brian Norris752ed6c2011-09-20 18:36:42 -07001249 * based on the properties of @this. The new descriptor is stored in
Brian Norris58373ff2010-07-15 12:15:44 -07001250 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1251 * passed to this function.
Brian Norris58373ff2010-07-15 12:15:44 -07001252 */
Brian Norris752ed6c2011-09-20 18:36:42 -07001253static int nand_create_badblock_pattern(struct nand_chip *this)
Brian Norris58373ff2010-07-15 12:15:44 -07001254{
1255 struct nand_bbt_descr *bd;
1256 if (this->badblock_pattern) {
Brian Norris752ed6c2011-09-20 18:36:42 -07001257 pr_warn("Bad block pattern already allocated; not replacing\n");
Brian Norris58373ff2010-07-15 12:15:44 -07001258 return -EINVAL;
1259 }
1260 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001261 if (!bd)
Brian Norris58373ff2010-07-15 12:15:44 -07001262 return -ENOMEM;
Brian Norris752ed6c2011-09-20 18:36:42 -07001263 bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
Brian Norris58373ff2010-07-15 12:15:44 -07001264 bd->offs = this->badblockpos;
1265 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1266 bd->pattern = scan_ff_pattern;
1267 bd->options |= NAND_BBT_DYNAMICSTRUCT;
1268 this->badblock_pattern = bd;
1269 return 0;
1270}
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001273 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
Brian Norris8b6e50c2011-05-25 14:59:01 -07001274 * @mtd: MTD device structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001276 * This function selects the default bad block table support for the device and
1277 * calls the nand_scan_bbt function.
1278 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001279int nand_default_bbt(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 struct nand_chip *this = mtd->priv;
Brian Norrisabb9cf72014-05-20 22:34:40 -07001282 int ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001283
Brian Norris8b6e50c2011-05-25 14:59:01 -07001284 /* Is a flash based bad block table requested? */
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001285 if (this->bbt_options & NAND_BBT_USE_FLASH) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001286 /* Use the default pattern descriptors */
1287 if (!this->bbt_td) {
Brian Norrisa40f7342011-05-31 16:31:22 -07001288 if (this->bbt_options & NAND_BBT_NO_OOB) {
Brian Norris9fd6b372012-06-22 16:35:40 -07001289 this->bbt_td = &bbt_main_no_oob_descr;
1290 this->bbt_md = &bbt_mirror_no_oob_descr;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001291 } else {
1292 this->bbt_td = &bbt_main_descr;
1293 this->bbt_md = &bbt_mirror_descr;
1294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 } else {
1297 this->bbt_td = NULL;
1298 this->bbt_md = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
Brian Norrisa2f812d2011-03-18 21:53:42 -07001300
Brian Norrisabb9cf72014-05-20 22:34:40 -07001301 if (!this->badblock_pattern) {
1302 ret = nand_create_badblock_pattern(this);
1303 if (ret)
1304 return ret;
1305 }
Brian Norrisa2f812d2011-03-18 21:53:42 -07001306
David Woodhousee0c7d762006-05-13 18:07:53 +01001307 return nand_scan_bbt(mtd, this->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
1310/**
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001311 * nand_isreserved_bbt - [NAND Interface] Check if a block is reserved
1312 * @mtd: MTD device structure
1313 * @offs: offset in the device
1314 */
1315int nand_isreserved_bbt(struct mtd_info *mtd, loff_t offs)
1316{
1317 struct nand_chip *this = mtd->priv;
1318 int block;
1319
1320 block = (int)(offs >> this->bbt_erase_shift);
1321 return bbt_get_entry(this, block) == BBT_BLOCK_RESERVED;
1322}
1323
1324/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001325 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
Brian Norris8b6e50c2011-05-25 14:59:01 -07001326 * @mtd: MTD device structure
1327 * @offs: offset in the device
1328 * @allowbbt: allow access to bad block table region
1329 */
David Woodhousee0c7d762006-05-13 18:07:53 +01001330int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
1332 struct nand_chip *this = mtd->priv;
Brian Norris39dbb022013-07-30 17:52:57 -07001333 int block, res;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001334
Brian Norrisb4d20d62013-07-30 17:52:56 -07001335 block = (int)(offs >> this->bbt_erase_shift);
1336 res = bbt_get_entry(this, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02001338 pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1339 (unsigned int)offs, block, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Brian Norris39dbb022013-07-30 17:52:57 -07001341 switch (res) {
Brian Norris771c5682013-07-30 17:52:55 -07001342 case BBT_BLOCK_GOOD:
David Woodhousee0c7d762006-05-13 18:07:53 +01001343 return 0;
Brian Norris771c5682013-07-30 17:52:55 -07001344 case BBT_BLOCK_WORN:
David Woodhousee0c7d762006-05-13 18:07:53 +01001345 return 1;
Brian Norris771c5682013-07-30 17:52:55 -07001346 case BBT_BLOCK_RESERVED:
David Woodhousee0c7d762006-05-13 18:07:53 +01001347 return allowbbt ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349 return 1;
1350}
1351
Brian Norrisb32843b2013-07-30 17:52:59 -07001352/**
1353 * nand_markbad_bbt - [NAND Interface] Mark a block bad in the BBT
1354 * @mtd: MTD device structure
1355 * @offs: offset of the bad block
1356 */
1357int nand_markbad_bbt(struct mtd_info *mtd, loff_t offs)
1358{
1359 struct nand_chip *this = mtd->priv;
1360 int block, ret = 0;
1361
1362 block = (int)(offs >> this->bbt_erase_shift);
1363
1364 /* Mark bad block in memory */
1365 bbt_mark_entry(this, block, BBT_BLOCK_WORN);
1366
1367 /* Update flash-based bad block table */
1368 if (this->bbt_options & NAND_BBT_USE_FLASH)
1369 ret = nand_update_bbt(mtd, offs);
1370
1371 return ret;
1372}
1373
David Woodhousee0c7d762006-05-13 18:07:53 +01001374EXPORT_SYMBOL(nand_scan_bbt);