blob: a6e6e0ef6d75c879dd7264b909e99840c4ac87e4 [file] [log] [blame]
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001/*
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02002 * Overview:
3 * Bad block table support for the NAND driver
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02004 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00005 * Copyright © 2004 Thomas Gleixner (tglx@linutronix.de)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02006 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Description:
12 *
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020013 * When nand_scan_bbt is called, then it tries to find the bad block table
Christian Hitz24e6ea42011-10-12 09:32:04 +020014 * depending on the options in the BBT descriptor(s). If no flash based BBT
Sergey Lapin3a38a552013-01-14 03:46:50 +000015 * (NAND_BBT_USE_FLASH) is specified then the device is scanned for factory
Christian Hitz24e6ea42011-10-12 09:32:04 +020016 * marked good / bad blocks. This information is used to create a memory BBT.
17 * Once a new bad block is discovered then the "factory" information is updated
18 * on the device.
19 * If a flash based BBT is specified then the function first tries to find the
20 * BBT on flash. If a BBT is found then the contents are read and the memory
21 * based BBT is created. If a mirrored BBT is selected then the mirror is
22 * searched too and the versions are compared. If the mirror has a greater
Sergey Lapin3a38a552013-01-14 03:46:50 +000023 * version number, then the mirror BBT is used to build the memory based BBT.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020024 * If the tables are not versioned, then we "or" the bad block information.
Christian Hitz24e6ea42011-10-12 09:32:04 +020025 * If one of the BBTs is out of date or does not exist it is (re)created.
26 * If no BBT exists at all then the device is scanned for factory marked
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020027 * good / bad blocks and the bad block tables are created.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020028 *
Christian Hitz24e6ea42011-10-12 09:32:04 +020029 * For manufacturer created BBTs like the one found on M-SYS DOC devices
30 * the BBT is searched and read but never created
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020031 *
Christian Hitz24e6ea42011-10-12 09:32:04 +020032 * The auto generated bad block table is located in the last good blocks
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020033 * of the device. The table is mirrored, so it can be updated eventually.
Christian Hitz24e6ea42011-10-12 09:32:04 +020034 * The table is marked in the OOB area with an ident pattern and a version
35 * number which indicates which of both tables is more up to date. If the NAND
36 * controller needs the complete OOB area for the ECC information then the
Sergey Lapin3a38a552013-01-14 03:46:50 +000037 * option NAND_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of
38 * course): it moves the ident pattern and the version byte into the data area
39 * and the OOB area will remain untouched.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020040 *
41 * The table uses 2 bits per block
Christian Hitz24e6ea42011-10-12 09:32:04 +020042 * 11b: block is good
43 * 00b: block is factory marked bad
Wolfgang Denka1be4762008-05-20 16:00:29 +020044 * 01b, 10b: block is marked bad due to wear
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020045 *
46 * The memory bad block table uses the following scheme:
47 * 00b: block is good
48 * 01b: block is marked bad due to wear
49 * 10b: block is reserved (to protect the bbt area)
50 * 11b: block is factory marked bad
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020051 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020052 * Multichip devices like DOC store the bad block info per floor.
53 *
54 * Following assumptions are made:
55 * - bbts start at a page boundary, if autolocated on a block boundary
William Juul52c07962007-10-31 13:53:06 +010056 * - the space necessary for a bbt in FLASH does not exceed a block boundary
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +020057 *
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020058 */
59
Scott Wood831bbad2015-06-22 22:38:32 -050060#include <common.h>
61#include <malloc.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070062#include <dm/devres.h>
Scott Wood831bbad2015-06-22 22:38:32 -050063#include <linux/compat.h>
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020064#include <linux/mtd/mtd.h>
Sergey Lapin3a38a552013-01-14 03:46:50 +000065#include <linux/mtd/bbm.h>
Masahiro Yamada2b7a8732017-11-30 13:45:24 +090066#include <linux/mtd/rawnand.h>
Christian Hitz24e6ea42011-10-12 09:32:04 +020067#include <linux/bitops.h>
Sergey Lapin3a38a552013-01-14 03:46:50 +000068#include <linux/string.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020069
70#define BBT_BLOCK_GOOD 0x00
71#define BBT_BLOCK_WORN 0x01
72#define BBT_BLOCK_RESERVED 0x02
73#define BBT_BLOCK_FACTORY_BAD 0x03
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020074
Heiko Schocherf5895d12014-06-24 10:10:04 +020075#define BBT_ENTRY_MASK 0x03
76#define BBT_ENTRY_SHIFT 2
77
78static int nand_update_bbt(struct mtd_info *mtd, loff_t offs);
79
80static inline uint8_t bbt_get_entry(struct nand_chip *chip, int block)
81{
82 uint8_t entry = chip->bbt[block >> BBT_ENTRY_SHIFT];
83 entry >>= (block & BBT_ENTRY_MASK) * 2;
84 return entry & BBT_ENTRY_MASK;
85}
86
87static inline void bbt_mark_entry(struct nand_chip *chip, int block,
88 uint8_t mark)
89{
90 uint8_t msk = (mark & BBT_ENTRY_MASK) << ((block & BBT_ENTRY_MASK) * 2);
91 chip->bbt[block >> BBT_ENTRY_SHIFT] |= msk;
92}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +020093
Christian Hitz24e6ea42011-10-12 09:32:04 +020094static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
95{
Sergey Lapin3a38a552013-01-14 03:46:50 +000096 if (memcmp(buf, td->pattern, td->len))
97 return -1;
98 return 0;
Christian Hitz24e6ea42011-10-12 09:32:04 +020099}
100
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200101/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200102 * check_pattern - [GENERIC] check if a pattern is in the buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000103 * @buf: the buffer to search
104 * @len: the length of buffer to search
105 * @paglen: the pagelength
106 * @td: search pattern descriptor
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200107 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000108 * Check for a pattern at the given place. Used to search bad block tables and
Heiko Schocherf5895d12014-06-24 10:10:04 +0200109 * good / bad block identifiers.
Sergey Lapin3a38a552013-01-14 03:46:50 +0000110 */
William Juul52c07962007-10-31 13:53:06 +0100111static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200112{
Christian Hitz24e6ea42011-10-12 09:32:04 +0200113 if (td->options & NAND_BBT_NO_OOB)
114 return check_pattern_no_oob(buf, td);
115
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200116 /* Compare the pattern */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200117 if (memcmp(buf + paglen + td->offs, td->pattern, td->len))
Sergey Lapin3a38a552013-01-14 03:46:50 +0000118 return -1;
Christian Hitz24e6ea42011-10-12 09:32:04 +0200119
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200120 return 0;
121}
122
123/**
William Juul52c07962007-10-31 13:53:06 +0100124 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
Sergey Lapin3a38a552013-01-14 03:46:50 +0000125 * @buf: the buffer to search
126 * @td: search pattern descriptor
William Juul52c07962007-10-31 13:53:06 +0100127 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000128 * Check for a pattern at the given place. Used to search bad block tables and
129 * good / bad block identifiers. Same as check_pattern, but no optional empty
130 * check.
131 */
William Juul52c07962007-10-31 13:53:06 +0100132static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
133{
William Juul52c07962007-10-31 13:53:06 +0100134 /* Compare the pattern */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000135 if (memcmp(buf + td->offs, td->pattern, td->len))
136 return -1;
William Juul52c07962007-10-31 13:53:06 +0100137 return 0;
138}
139
140/**
Christian Hitz24e6ea42011-10-12 09:32:04 +0200141 * add_marker_len - compute the length of the marker in data area
Sergey Lapin3a38a552013-01-14 03:46:50 +0000142 * @td: BBT descriptor used for computation
Christian Hitz24e6ea42011-10-12 09:32:04 +0200143 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000144 * The length will be 0 if the marker is located in OOB area.
Christian Hitz24e6ea42011-10-12 09:32:04 +0200145 */
146static u32 add_marker_len(struct nand_bbt_descr *td)
147{
148 u32 len;
149
150 if (!(td->options & NAND_BBT_NO_OOB))
151 return 0;
152
153 len = td->len;
154 if (td->options & NAND_BBT_VERSION)
155 len++;
156 return len;
157}
158
159/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200160 * read_bbt - [GENERIC] Read the bad block table starting from page
Sergey Lapin3a38a552013-01-14 03:46:50 +0000161 * @mtd: MTD device structure
162 * @buf: temporary buffer
163 * @page: the starting page
164 * @num: the number of bbt descriptors to read
165 * @td: the bbt describtion table
Heiko Schocherf5895d12014-06-24 10:10:04 +0200166 * @offs: block number offset in the table
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200167 *
168 * Read the bad block table starting from page.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200169 */
William Juul52c07962007-10-31 13:53:06 +0100170static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
Christian Hitz24e6ea42011-10-12 09:32:04 +0200171 struct nand_bbt_descr *td, int offs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200172{
Sergey Lapin3a38a552013-01-14 03:46:50 +0000173 int res, ret = 0, i, j, act = 0;
Scott Wood17fed142016-05-30 13:57:56 -0500174 struct nand_chip *this = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200175 size_t retlen, len, totlen;
176 loff_t from;
Christian Hitz24e6ea42011-10-12 09:32:04 +0200177 int bits = td->options & NAND_BBT_NRBITS_MSK;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000178 uint8_t msk = (uint8_t)((1 << bits) - 1);
Christian Hitz24e6ea42011-10-12 09:32:04 +0200179 u32 marker_len;
180 int reserved_block_code = td->reserved_block_code;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200181
182 totlen = (num * bits) >> 3;
Christian Hitz24e6ea42011-10-12 09:32:04 +0200183 marker_len = add_marker_len(td);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000184 from = ((loff_t)page) << this->page_shift;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200185
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200186 while (totlen) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000187 len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
Christian Hitz24e6ea42011-10-12 09:32:04 +0200188 if (marker_len) {
189 /*
190 * In case the BBT marker is not in the OOB area it
191 * will be just in the first page.
192 */
193 len -= marker_len;
194 from += marker_len;
195 marker_len = 0;
196 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000197 res = mtd_read(mtd, from, len, &retlen, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200198 if (res < 0) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000199 if (mtd_is_eccerr(res)) {
Scott Wood3ea94ed2015-06-26 19:03:26 -0500200 pr_info("nand_bbt: ECC error in BBT at 0x%012llx\n",
201 from & ~mtd->writesize);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000202 return res;
203 } else if (mtd_is_bitflip(res)) {
Scott Wood3ea94ed2015-06-26 19:03:26 -0500204 pr_info("nand_bbt: corrected error in BBT at 0x%012llx\n",
205 from & ~mtd->writesize);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000206 ret = res;
207 } else {
208 pr_info("nand_bbt: error reading BBT\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200209 return res;
210 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200211 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200212
213 /* Analyse data */
214 for (i = 0; i < len; i++) {
215 uint8_t dat = buf[i];
Heiko Schocherf5895d12014-06-24 10:10:04 +0200216 for (j = 0; j < 8; j += bits, act++) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200217 uint8_t tmp = (dat >> j) & msk;
218 if (tmp == msk)
219 continue;
William Juul52c07962007-10-31 13:53:06 +0100220 if (reserved_block_code && (tmp == reserved_block_code)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000221 pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200222 (loff_t)(offs + act) <<
223 this->bbt_erase_shift);
224 bbt_mark_entry(this, offs + act,
225 BBT_BLOCK_RESERVED);
William Juul52c07962007-10-31 13:53:06 +0100226 mtd->ecc_stats.bbtblocks++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200227 continue;
228 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200229 /*
230 * Leave it for now, if it's matured we can
231 * move this message to pr_debug.
232 */
233 pr_info("nand_read_bbt: bad block at 0x%012llx\n",
234 (loff_t)(offs + act) <<
235 this->bbt_erase_shift);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000236 /* Factory marked bad or worn out? */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200237 if (tmp == 0)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200238 bbt_mark_entry(this, offs + act,
239 BBT_BLOCK_FACTORY_BAD);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200240 else
Heiko Schocherf5895d12014-06-24 10:10:04 +0200241 bbt_mark_entry(this, offs + act,
242 BBT_BLOCK_WORN);
William Juul52c07962007-10-31 13:53:06 +0100243 mtd->ecc_stats.badblocks++;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200244 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200245 }
246 totlen -= len;
247 from += len;
248 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000249 return ret;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200250}
251
252/**
253 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
Sergey Lapin3a38a552013-01-14 03:46:50 +0000254 * @mtd: MTD device structure
255 * @buf: temporary buffer
256 * @td: descriptor for the bad block table
257 * @chip: read the table for a specific chip, -1 read all chips; applies only if
258 * NAND_BBT_PERCHIP option is set
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200259 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000260 * Read the bad block table for all chips starting at a given page. We assume
261 * that the bbt bits are in consecutive order.
262 */
William Juul52c07962007-10-31 13:53:06 +0100263static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200264{
Scott Wood17fed142016-05-30 13:57:56 -0500265 struct nand_chip *this = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200266 int res = 0, i;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200267
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200268 if (td->options & NAND_BBT_PERCHIP) {
269 int offs = 0;
270 for (i = 0; i < this->numchips; i++) {
271 if (chip == -1 || chip == i)
Christian Hitz24e6ea42011-10-12 09:32:04 +0200272 res = read_bbt(mtd, buf, td->pages[i],
273 this->chipsize >> this->bbt_erase_shift,
274 td, offs);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200275 if (res)
276 return res;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200277 offs += this->chipsize >> this->bbt_erase_shift;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200278 }
279 } else {
Christian Hitz24e6ea42011-10-12 09:32:04 +0200280 res = read_bbt(mtd, buf, td->pages[0],
281 mtd->size >> this->bbt_erase_shift, td, 0);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200282 if (res)
283 return res;
284 }
285 return 0;
286}
287
Sergey Lapin3a38a552013-01-14 03:46:50 +0000288/* BBT marker is in the first page, no OOB */
289static int scan_read_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
Christian Hitz24e6ea42011-10-12 09:32:04 +0200290 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
Sergey Lapin3a38a552013-01-14 03:46:50 +0000299 return mtd_read(mtd, offs, len, &retlen, buf);
Christian Hitz24e6ea42011-10-12 09:32:04 +0200300}
301
Sergey Lapin3a38a552013-01-14 03:46:50 +0000302/**
303 * scan_read_oob - [GENERIC] Scan data+OOB region to buffer
304 * @mtd: MTD device structure
305 * @buf: temporary buffer
306 * @offs: offset at which to scan
307 * @len: length of data region to read
308 *
309 * Scan read data from data+OOB. May traverse multiple pages, interleaving
310 * page,OOB,page,OOB,... in buf. Completes transfer and returns the "strongest"
311 * ECC condition (error or bitflip). May quit on the first (non-ECC) error.
William Juul52c07962007-10-31 13:53:06 +0100312 */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000313static int scan_read_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
William Juul52c07962007-10-31 13:53:06 +0100314 size_t len)
315{
316 struct mtd_oob_ops ops;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000317 int res, ret = 0;
William Juul52c07962007-10-31 13:53:06 +0100318
Sergey Lapin3a38a552013-01-14 03:46:50 +0000319 ops.mode = MTD_OPS_PLACE_OOB;
William Juul52c07962007-10-31 13:53:06 +0100320 ops.ooboffs = 0;
321 ops.ooblen = mtd->oobsize;
Christian Hitz24e6ea42011-10-12 09:32:04 +0200322
Christian Hitz24e6ea42011-10-12 09:32:04 +0200323 while (len > 0) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000324 ops.datbuf = buf;
325 ops.len = min(len, (size_t)mtd->writesize);
326 ops.oobbuf = buf + ops.len;
Christian Hitz24e6ea42011-10-12 09:32:04 +0200327
Sergey Lapin3a38a552013-01-14 03:46:50 +0000328 res = mtd_read_oob(mtd, offs, &ops);
329 if (res) {
330 if (!mtd_is_bitflip_or_eccerr(res))
Christian Hitz24e6ea42011-10-12 09:32:04 +0200331 return res;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000332 else if (mtd_is_eccerr(res) || !ret)
333 ret = res;
Christian Hitz24e6ea42011-10-12 09:32:04 +0200334 }
335
336 buf += mtd->oobsize + mtd->writesize;
337 len -= mtd->writesize;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000338 offs += mtd->writesize;
Christian Hitz24e6ea42011-10-12 09:32:04 +0200339 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000340 return ret;
Christian Hitz24e6ea42011-10-12 09:32:04 +0200341}
William Juul52c07962007-10-31 13:53:06 +0100342
Sergey Lapin3a38a552013-01-14 03:46:50 +0000343static int scan_read(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
Christian Hitz24e6ea42011-10-12 09:32:04 +0200344 size_t len, struct nand_bbt_descr *td)
345{
346 if (td->options & NAND_BBT_NO_OOB)
Sergey Lapin3a38a552013-01-14 03:46:50 +0000347 return scan_read_data(mtd, buf, offs, td);
Christian Hitz24e6ea42011-10-12 09:32:04 +0200348 else
Sergey Lapin3a38a552013-01-14 03:46:50 +0000349 return scan_read_oob(mtd, buf, offs, len);
William Juul52c07962007-10-31 13:53:06 +0100350}
351
Sergey Lapin3a38a552013-01-14 03:46:50 +0000352/* Scan write data with oob to flash */
William Juul52c07962007-10-31 13:53:06 +0100353static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
354 uint8_t *buf, uint8_t *oob)
355{
356 struct mtd_oob_ops ops;
357
Sergey Lapin3a38a552013-01-14 03:46:50 +0000358 ops.mode = MTD_OPS_PLACE_OOB;
William Juul52c07962007-10-31 13:53:06 +0100359 ops.ooboffs = 0;
360 ops.ooblen = mtd->oobsize;
361 ops.datbuf = buf;
362 ops.oobbuf = oob;
363 ops.len = len;
364
Sergey Lapin3a38a552013-01-14 03:46:50 +0000365 return mtd_write_oob(mtd, offs, &ops);
William Juul52c07962007-10-31 13:53:06 +0100366}
367
Christian Hitz24e6ea42011-10-12 09:32:04 +0200368static u32 bbt_get_ver_offs(struct mtd_info *mtd, struct nand_bbt_descr *td)
369{
370 u32 ver_offs = td->veroffs;
371
372 if (!(td->options & NAND_BBT_NO_OOB))
373 ver_offs += mtd->writesize;
374 return ver_offs;
375}
376
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200377/**
378 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
Sergey Lapin3a38a552013-01-14 03:46:50 +0000379 * @mtd: MTD device structure
380 * @buf: temporary buffer
381 * @td: descriptor for the bad block table
382 * @md: descriptor for the bad block table mirror
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200383 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000384 * Read the bad block table(s) for all chips starting at a given page. We
385 * assume that the bbt bits are in consecutive order.
386 */
387static void read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
388 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200389{
Scott Wood17fed142016-05-30 13:57:56 -0500390 struct nand_chip *this = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200391
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200392 /* Read the primary version, if available */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200393 if (td->options & NAND_BBT_VERSION) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000394 scan_read(mtd, buf, (loff_t)td->pages[0] << this->page_shift,
Christian Hitz24e6ea42011-10-12 09:32:04 +0200395 mtd->writesize, td);
396 td->version[0] = buf[bbt_get_ver_offs(mtd, td)];
Sergey Lapin3a38a552013-01-14 03:46:50 +0000397 pr_info("Bad block table at page %d, version 0x%02X\n",
398 td->pages[0], td->version[0]);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200399 }
400
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200401 /* Read the mirror version, if available */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200402 if (md && (md->options & NAND_BBT_VERSION)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000403 scan_read(mtd, buf, (loff_t)md->pages[0] << this->page_shift,
404 mtd->writesize, md);
Christian Hitz24e6ea42011-10-12 09:32:04 +0200405 md->version[0] = buf[bbt_get_ver_offs(mtd, md)];
Sergey Lapin3a38a552013-01-14 03:46:50 +0000406 pr_info("Bad block table at page %d, version 0x%02X\n",
407 md->pages[0], md->version[0]);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200408 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200409}
410
Sergey Lapin3a38a552013-01-14 03:46:50 +0000411/* Scan a given block partially */
William Juul52c07962007-10-31 13:53:06 +0100412static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000413 loff_t offs, uint8_t *buf, int numpages)
William Juul52c07962007-10-31 13:53:06 +0100414{
415 struct mtd_oob_ops ops;
416 int j, ret;
417
418 ops.ooblen = mtd->oobsize;
419 ops.oobbuf = buf;
420 ops.ooboffs = 0;
421 ops.datbuf = NULL;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000422 ops.mode = MTD_OPS_PLACE_OOB;
William Juul52c07962007-10-31 13:53:06 +0100423
Sergey Lapin3a38a552013-01-14 03:46:50 +0000424 for (j = 0; j < numpages; j++) {
William Juul52c07962007-10-31 13:53:06 +0100425 /*
Sergey Lapin3a38a552013-01-14 03:46:50 +0000426 * Read the full oob until read_oob is fixed to handle single
427 * byte reads for 16 bit buswidth.
William Juul52c07962007-10-31 13:53:06 +0100428 */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000429 ret = mtd_read_oob(mtd, offs, &ops);
430 /* Ignore ECC errors when checking for BBM */
431 if (ret && !mtd_is_bitflip_or_eccerr(ret))
William Juul52c07962007-10-31 13:53:06 +0100432 return ret;
433
434 if (check_short_pattern(buf, bd))
435 return 1;
436
437 offs += mtd->writesize;
438 }
439 return 0;
440}
441
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200442/**
443 * create_bbt - [GENERIC] Create a bad block table by scanning the device
Sergey Lapin3a38a552013-01-14 03:46:50 +0000444 * @mtd: MTD device structure
445 * @buf: temporary buffer
446 * @bd: descriptor for the good/bad block search pattern
447 * @chip: create the table for a specific chip, -1 read all chips; applies only
448 * if NAND_BBT_PERCHIP option is set
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200449 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000450 * Create a bad block table by scanning the device for the given good/bad block
451 * identify pattern.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200452 */
William Juul52c07962007-10-31 13:53:06 +0100453static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
454 struct nand_bbt_descr *bd, int chip)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200455{
Scott Wood17fed142016-05-30 13:57:56 -0500456 struct nand_chip *this = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200457 int i, numblocks, numpages;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200458 int startblock;
459 loff_t from;
William Juul52c07962007-10-31 13:53:06 +0100460
Sergey Lapin3a38a552013-01-14 03:46:50 +0000461 pr_info("Scanning device for bad blocks\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200462
Heiko Schocherf5895d12014-06-24 10:10:04 +0200463 if (bd->options & NAND_BBT_SCAN2NDPAGE)
Sergey Lapin3a38a552013-01-14 03:46:50 +0000464 numpages = 2;
Christian Hitz24e6ea42011-10-12 09:32:04 +0200465 else
Sergey Lapin3a38a552013-01-14 03:46:50 +0000466 numpages = 1;
William Juul52c07962007-10-31 13:53:06 +0100467
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200468 if (chip == -1) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200469 numblocks = mtd->size >> this->bbt_erase_shift;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200470 startblock = 0;
471 from = 0;
472 } else {
473 if (chip >= this->numchips) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000474 pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
William Juul52c07962007-10-31 13:53:06 +0100475 chip + 1, this->numchips);
476 return -EINVAL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200477 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200478 numblocks = this->chipsize >> this->bbt_erase_shift;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200479 startblock = chip * numblocks;
480 numblocks += startblock;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200481 from = (loff_t)startblock << this->bbt_erase_shift;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200482 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200483
Sergey Lapin3a38a552013-01-14 03:46:50 +0000484 if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
485 from += mtd->erasesize - (mtd->writesize * numpages);
Christian Hitz24e6ea42011-10-12 09:32:04 +0200486
Heiko Schocherf5895d12014-06-24 10:10:04 +0200487 for (i = startblock; i < numblocks; i++) {
William Juul52c07962007-10-31 13:53:06 +0100488 int ret;
489
Christian Hitz24e6ea42011-10-12 09:32:04 +0200490 BUG_ON(bd->options & NAND_BBT_NO_OOB);
491
Heiko Schocherf5895d12014-06-24 10:10:04 +0200492 ret = scan_block_fast(mtd, bd, from, buf, numpages);
William Juul52c07962007-10-31 13:53:06 +0100493 if (ret < 0)
494 return ret;
495
496 if (ret) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200497 bbt_mark_entry(this, i, BBT_BLOCK_FACTORY_BAD);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000498 pr_warn("Bad eraseblock %d at 0x%012llx\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200499 i, (unsigned long long)from);
William Juul52c07962007-10-31 13:53:06 +0100500 mtd->ecc_stats.badblocks++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200501 }
William Juul52c07962007-10-31 13:53:06 +0100502
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200503 from += (1 << this->bbt_erase_shift);
504 }
William Juul52c07962007-10-31 13:53:06 +0100505 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200506}
507
508/**
509 * search_bbt - [GENERIC] scan the device for a specific bad block table
Sergey Lapin3a38a552013-01-14 03:46:50 +0000510 * @mtd: MTD device structure
511 * @buf: temporary buffer
512 * @td: descriptor for the bad block table
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200513 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000514 * Read the bad block table by searching for a given ident pattern. Search is
515 * preformed either from the beginning up or from the end of the device
516 * downwards. The search starts always at the start of a block. If the option
517 * NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains
518 * the bad block information of this chip. This is necessary to provide support
519 * for certain DOC devices.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200520 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000521 * The bbt ident pattern resides in the oob area of the first page in a block.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200522 */
William Juul52c07962007-10-31 13:53:06 +0100523static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200524{
Scott Wood17fed142016-05-30 13:57:56 -0500525 struct nand_chip *this = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200526 int i, chips;
Marek Vasut15324ad2011-09-30 12:13:22 +0200527 int startblock, block, dir;
William Juul52c07962007-10-31 13:53:06 +0100528 int scanlen = mtd->writesize + mtd->oobsize;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200529 int bbtblocks;
William Juul52c07962007-10-31 13:53:06 +0100530 int blocktopage = this->bbt_erase_shift - this->page_shift;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200531
Sergey Lapin3a38a552013-01-14 03:46:50 +0000532 /* Search direction top -> down? */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200533 if (td->options & NAND_BBT_LASTBLOCK) {
William Juul52c07962007-10-31 13:53:06 +0100534 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200535 dir = -1;
536 } else {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200537 startblock = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200538 dir = 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200539 }
540
Sergey Lapin3a38a552013-01-14 03:46:50 +0000541 /* Do we have a bbt per chip? */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200542 if (td->options & NAND_BBT_PERCHIP) {
543 chips = this->numchips;
544 bbtblocks = this->chipsize >> this->bbt_erase_shift;
545 startblock &= bbtblocks - 1;
546 } else {
547 chips = 1;
548 bbtblocks = mtd->size >> this->bbt_erase_shift;
549 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200550
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200551 for (i = 0; i < chips; i++) {
552 /* Reset version information */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200553 td->version[i] = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200554 td->pages[i] = -1;
555 /* Scan the maximum number of blocks */
556 for (block = 0; block < td->maxblocks; block++) {
William Juul52c07962007-10-31 13:53:06 +0100557
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200558 int actblock = startblock + dir * block;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -0400559 loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
William Juul52c07962007-10-31 13:53:06 +0100560
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200561 /* Read first page */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000562 scan_read(mtd, buf, offs, mtd->writesize, td);
William Juul52c07962007-10-31 13:53:06 +0100563 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
564 td->pages[i] = actblock << blocktopage;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200565 if (td->options & NAND_BBT_VERSION) {
Christian Hitz24e6ea42011-10-12 09:32:04 +0200566 offs = bbt_get_ver_offs(mtd, td);
567 td->version[i] = buf[offs];
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200568 }
569 break;
570 }
571 }
572 startblock += this->chipsize >> this->bbt_erase_shift;
573 }
574 /* Check, if we found a bbt for each requested chip */
575 for (i = 0; i < chips; i++) {
576 if (td->pages[i] == -1)
Sergey Lapin3a38a552013-01-14 03:46:50 +0000577 pr_warn("Bad block table not found for chip %d\n", i);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200578 else
Scott Wood3ea94ed2015-06-26 19:03:26 -0500579 pr_info("Bad block table found at page %d, version 0x%02X\n",
580 td->pages[i], td->version[i]);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200581 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200582 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200583}
584
585/**
586 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
Sergey Lapin3a38a552013-01-14 03:46:50 +0000587 * @mtd: MTD device structure
588 * @buf: temporary buffer
589 * @td: descriptor for the bad block table
590 * @md: descriptor for the bad block table mirror
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200591 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000592 * Search and read the bad block table(s).
593 */
594static void search_read_bbts(struct mtd_info *mtd, uint8_t *buf,
595 struct nand_bbt_descr *td,
596 struct nand_bbt_descr *md)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200597{
598 /* Search the primary table */
William Juul52c07962007-10-31 13:53:06 +0100599 search_bbt(mtd, buf, td);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200600
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200601 /* Search the mirror table */
602 if (md)
William Juul52c07962007-10-31 13:53:06 +0100603 search_bbt(mtd, buf, md);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200604}
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200605
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200606/**
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200607 * write_bbt - [GENERIC] (Re)write the bad block table
Sergey Lapin3a38a552013-01-14 03:46:50 +0000608 * @mtd: MTD device structure
609 * @buf: temporary buffer
610 * @td: descriptor for the bad block table
611 * @md: descriptor for the bad block table mirror
612 * @chipsel: selector for a specific chip, -1 for all
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200613 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000614 * (Re)write the bad block table.
615 */
William Juul52c07962007-10-31 13:53:06 +0100616static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
617 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
618 int chipsel)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200619{
Scott Wood17fed142016-05-30 13:57:56 -0500620 struct nand_chip *this = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200621 struct erase_info einfo;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200622 int i, res, chip = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200623 int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200624 int nrchips, pageoffs, ooboffs;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200625 uint8_t msk[4];
626 uint8_t rcode = td->reserved_block_code;
627 size_t retlen, len = 0;
628 loff_t to;
William Juul52c07962007-10-31 13:53:06 +0100629 struct mtd_oob_ops ops;
630
631 ops.ooblen = mtd->oobsize;
632 ops.ooboffs = 0;
633 ops.datbuf = NULL;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000634 ops.mode = MTD_OPS_PLACE_OOB;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200635
636 if (!rcode)
637 rcode = 0xff;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000638 /* Write bad block table per chip rather than per device? */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200639 if (td->options & NAND_BBT_PERCHIP) {
William Juul52c07962007-10-31 13:53:06 +0100640 numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000641 /* Full device write or specific chip? */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200642 if (chipsel == -1) {
643 nrchips = this->numchips;
644 } else {
645 nrchips = chipsel + 1;
646 chip = chipsel;
647 }
648 } else {
William Juul52c07962007-10-31 13:53:06 +0100649 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200650 nrchips = 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200651 }
652
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200653 /* Loop through the chips */
654 for (; chip < nrchips; chip++) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000655 /*
656 * There was already a version of the table, reuse the page
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200657 * This applies for absolute placement too, as we have the
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200658 * page nr. in td->pages.
659 */
660 if (td->pages[chip] != -1) {
661 page = td->pages[chip];
662 goto write;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200663 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200664
Sergey Lapin3a38a552013-01-14 03:46:50 +0000665 /*
666 * Automatic placement of the bad block table. Search direction
667 * top -> down?
668 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200669 if (td->options & NAND_BBT_LASTBLOCK) {
670 startblock = numblocks * (chip + 1) - 1;
671 dir = -1;
672 } else {
673 startblock = chip * numblocks;
674 dir = 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200675 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200676
677 for (i = 0; i < td->maxblocks; i++) {
678 int block = startblock + dir * i;
679 /* Check, if the block is bad */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200680 switch (bbt_get_entry(this, block)) {
681 case BBT_BLOCK_WORN:
682 case BBT_BLOCK_FACTORY_BAD:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200683 continue;
684 }
William Juul52c07962007-10-31 13:53:06 +0100685 page = block <<
686 (this->bbt_erase_shift - this->page_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200687 /* Check, if the block is used by the mirror table */
688 if (!md || md->pages[chip] != page)
689 goto write;
690 }
Sergey Lapin3a38a552013-01-14 03:46:50 +0000691 pr_err("No space left to write bad block table\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200692 return -ENOSPC;
William Juul52c07962007-10-31 13:53:06 +0100693 write:
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200694
695 /* Set up shift count and masks for the flash table */
696 bits = td->options & NAND_BBT_NRBITS_MSK;
William Juul52c07962007-10-31 13:53:06 +0100697 msk[2] = ~rcode;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200698 switch (bits) {
William Juul52c07962007-10-31 13:53:06 +0100699 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
700 msk[3] = 0x01;
701 break;
702 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
703 msk[3] = 0x03;
704 break;
705 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
706 msk[3] = 0x0f;
707 break;
708 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
709 msk[3] = 0xff;
710 break;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200711 default: return -EINVAL;
712 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200713
Sergey Lapin3a38a552013-01-14 03:46:50 +0000714 to = ((loff_t)page) << this->page_shift;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200715
Sergey Lapin3a38a552013-01-14 03:46:50 +0000716 /* Must we save the block contents? */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200717 if (td->options & NAND_BBT_SAVECONTENT) {
718 /* Make it block aligned */
Scott Wood52ab7ce2016-05-30 13:57:58 -0500719 to &= ~(((loff_t)1 << this->bbt_erase_shift) - 1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200720 len = 1 << this->bbt_erase_shift;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000721 res = mtd_read(mtd, to, len, &retlen, buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200722 if (res < 0) {
723 if (retlen != len) {
Scott Wood3ea94ed2015-06-26 19:03:26 -0500724 pr_info("nand_bbt: error reading block for writing the bad block table\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200725 return res;
726 }
Scott Wood3ea94ed2015-06-26 19:03:26 -0500727 pr_warn("nand_bbt: ECC error while reading block for writing bad block table\n");
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200728 }
William Juul52c07962007-10-31 13:53:06 +0100729 /* Read oob data */
730 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
731 ops.oobbuf = &buf[len];
Sergey Lapin3a38a552013-01-14 03:46:50 +0000732 res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
William Juul52c07962007-10-31 13:53:06 +0100733 if (res < 0 || ops.oobretlen != ops.ooblen)
734 goto outerr;
735
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200736 /* Calc the byte offset in the buffer */
737 pageoffs = page - (int)(to >> this->page_shift);
738 offs = pageoffs << this->page_shift;
739 /* Preset the bbt area with 0xff */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000740 memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
William Juul52c07962007-10-31 13:53:06 +0100741 ooboffs = len + (pageoffs * mtd->oobsize);
742
Christian Hitz24e6ea42011-10-12 09:32:04 +0200743 } else if (td->options & NAND_BBT_NO_OOB) {
744 ooboffs = 0;
745 offs = td->len;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000746 /* The version byte */
Christian Hitz24e6ea42011-10-12 09:32:04 +0200747 if (td->options & NAND_BBT_VERSION)
748 offs++;
749 /* Calc length */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000750 len = (size_t)(numblocks >> sft);
Christian Hitz24e6ea42011-10-12 09:32:04 +0200751 len += offs;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000752 /* Make it page aligned! */
Christian Hitz24e6ea42011-10-12 09:32:04 +0200753 len = ALIGN(len, mtd->writesize);
754 /* Preset the buffer with 0xff */
755 memset(buf, 0xff, len);
756 /* Pattern is located at the begin of first page */
757 memcpy(buf, td->pattern, td->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200758 } else {
759 /* Calc length */
Sergey Lapin3a38a552013-01-14 03:46:50 +0000760 len = (size_t)(numblocks >> sft);
761 /* Make it page aligned! */
Christian Hitz24e6ea42011-10-12 09:32:04 +0200762 len = ALIGN(len, mtd->writesize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200763 /* Preset the buffer with 0xff */
William Juul52c07962007-10-31 13:53:06 +0100764 memset(buf, 0xff, len +
765 (len >> this->page_shift)* mtd->oobsize);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200766 offs = 0;
William Juul52c07962007-10-31 13:53:06 +0100767 ooboffs = len;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200768 /* Pattern is located in oob area of first page */
William Juul52c07962007-10-31 13:53:06 +0100769 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200770 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200771
William Juul52c07962007-10-31 13:53:06 +0100772 if (td->options & NAND_BBT_VERSION)
773 buf[ooboffs + td->veroffs] = td->version[chip];
774
Sergey Lapin3a38a552013-01-14 03:46:50 +0000775 /* Walk through the memory table */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200776 for (i = 0; i < numblocks; i++) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200777 uint8_t dat;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200778 int sftcnt = (i << (3 - sft)) & sftmsk;
779 dat = bbt_get_entry(this, chip * numblocks + i);
780 /* Do not store the reserved bbt blocks! */
781 buf[offs + (i >> sft)] &= ~(msk[dat] << sftcnt);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200782 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200783
William Juul52c07962007-10-31 13:53:06 +0100784 memset(&einfo, 0, sizeof(einfo));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200785 einfo.mtd = mtd;
Sandeep Paulrajeab580c2009-10-30 13:51:23 -0400786 einfo.addr = to;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200787 einfo.len = 1 << this->bbt_erase_shift;
William Juul52c07962007-10-31 13:53:06 +0100788 res = nand_erase_nand(mtd, &einfo, 1);
789 if (res < 0)
790 goto outerr;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200791
Christian Hitz24e6ea42011-10-12 09:32:04 +0200792 res = scan_write_bbt(mtd, to, len, buf,
793 td->options & NAND_BBT_NO_OOB ? NULL :
794 &buf[len]);
William Juul52c07962007-10-31 13:53:06 +0100795 if (res < 0)
796 goto outerr;
797
Sergey Lapin3a38a552013-01-14 03:46:50 +0000798 pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
799 (unsigned long long)to, td->version[chip]);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200800
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200801 /* Mark it as used */
802 td->pages[chip] = page;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200803 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200804 return 0;
William Juul52c07962007-10-31 13:53:06 +0100805
806 outerr:
Sergey Lapin3a38a552013-01-14 03:46:50 +0000807 pr_warn("nand_bbt: error while writing bad block table %d\n", res);
William Juul52c07962007-10-31 13:53:06 +0100808 return res;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200809}
810
811/**
812 * nand_memory_bbt - [GENERIC] create a memory based bad block table
Sergey Lapin3a38a552013-01-14 03:46:50 +0000813 * @mtd: MTD device structure
814 * @bd: descriptor for the good/bad block search pattern
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200815 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000816 * The function creates a memory based bbt by scanning the device for
817 * manufacturer / software marked good / bad blocks.
818 */
William Juul52c07962007-10-31 13:53:06 +0100819static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200820{
Scott Wood17fed142016-05-30 13:57:56 -0500821 struct nand_chip *this = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200822
William Juul52c07962007-10-31 13:53:06 +0100823 return create_bbt(mtd, this->buffers->databuf, bd, -1);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200824}
825
826/**
William Juul52c07962007-10-31 13:53:06 +0100827 * check_create - [GENERIC] create and write bbt(s) if necessary
Sergey Lapin3a38a552013-01-14 03:46:50 +0000828 * @mtd: MTD device structure
829 * @buf: temporary buffer
830 * @bd: descriptor for the good/bad block search pattern
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200831 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000832 * The function checks the results of the previous call to read_bbt and creates
833 * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found
834 * for the chip/device. Update is necessary if one of the tables is missing or
835 * the version nr. of one table is less than the other.
836 */
William Juul52c07962007-10-31 13:53:06 +0100837static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200838{
Sergey Lapin3a38a552013-01-14 03:46:50 +0000839 int i, chips, writeops, create, chipsel, res, res2;
Scott Wood17fed142016-05-30 13:57:56 -0500840 struct nand_chip *this = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200841 struct nand_bbt_descr *td = this->bbt_td;
842 struct nand_bbt_descr *md = this->bbt_md;
843 struct nand_bbt_descr *rd, *rd2;
844
Sergey Lapin3a38a552013-01-14 03:46:50 +0000845 /* Do we have a bbt per chip? */
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200846 if (td->options & NAND_BBT_PERCHIP)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200847 chips = this->numchips;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200848 else
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200849 chips = 1;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200850
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200851 for (i = 0; i < chips; i++) {
852 writeops = 0;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000853 create = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200854 rd = NULL;
855 rd2 = NULL;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000856 res = res2 = 0;
857 /* Per chip or per device? */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200858 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000859 /* Mirrored table available? */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200860 if (md) {
861 if (td->pages[i] == -1 && md->pages[i] == -1) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000862 create = 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200863 writeops = 0x03;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000864 } else if (td->pages[i] == -1) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200865 rd = md;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000866 writeops = 0x01;
867 } else if (md->pages[i] == -1) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200868 rd = td;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000869 writeops = 0x02;
870 } else if (td->version[i] == md->version[i]) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200871 rd = td;
872 if (!(td->options & NAND_BBT_VERSION))
873 rd2 = md;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000874 } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200875 rd = td;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000876 writeops = 0x02;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200877 } else {
878 rd = md;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000879 writeops = 0x01;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200880 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200881 } else {
882 if (td->pages[i] == -1) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000883 create = 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200884 writeops = 0x01;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000885 } else {
886 rd = td;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200887 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200888 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200889
Sergey Lapin3a38a552013-01-14 03:46:50 +0000890 if (create) {
891 /* Create the bad block table by scanning the device? */
892 if (!(td->options & NAND_BBT_CREATE))
893 continue;
894
895 /* Create the table in memory by scanning the chip(s) */
896 if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
897 create_bbt(mtd, buf, bd, chipsel);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200898
Sergey Lapin3a38a552013-01-14 03:46:50 +0000899 td->version[i] = 1;
900 if (md)
901 md->version[i] = 1;
902 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200903
Sergey Lapin3a38a552013-01-14 03:46:50 +0000904 /* Read back first? */
905 if (rd) {
906 res = read_abs_bbt(mtd, buf, rd, chipsel);
907 if (mtd_is_eccerr(res)) {
908 /* Mark table as invalid */
909 rd->pages[i] = -1;
910 rd->version[i] = 0;
911 i--;
912 continue;
913 }
914 }
915 /* If they weren't versioned, read both */
916 if (rd2) {
917 res2 = read_abs_bbt(mtd, buf, rd2, chipsel);
918 if (mtd_is_eccerr(res2)) {
919 /* Mark table as invalid */
920 rd2->pages[i] = -1;
921 rd2->version[i] = 0;
922 i--;
923 continue;
924 }
925 }
926
927 /* Scrub the flash table(s)? */
928 if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
929 writeops = 0x03;
930
931 /* Update version numbers before writing */
932 if (md) {
933 td->version[i] = max(td->version[i], md->version[i]);
934 md->version[i] = td->version[i];
935 }
936
937 /* Write the bad block table to the device? */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200938 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
William Juul52c07962007-10-31 13:53:06 +0100939 res = write_bbt(mtd, buf, td, md, chipsel);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200940 if (res < 0)
941 return res;
942 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200943
Sergey Lapin3a38a552013-01-14 03:46:50 +0000944 /* Write the mirror bad block table to the device? */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200945 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
William Juul52c07962007-10-31 13:53:06 +0100946 res = write_bbt(mtd, buf, md, td, chipsel);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200947 if (res < 0)
948 return res;
949 }
950 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200951 return 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200952}
953
954/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200955 * mark_bbt_regions - [GENERIC] mark the bad block table regions
Sergey Lapin3a38a552013-01-14 03:46:50 +0000956 * @mtd: MTD device structure
957 * @td: bad block table descriptor
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200958 *
Sergey Lapin3a38a552013-01-14 03:46:50 +0000959 * The bad block table regions are marked as "bad" to prevent accidental
960 * erasures / writes. The regions are identified by the mark 0x02.
961 */
William Juul52c07962007-10-31 13:53:06 +0100962static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200963{
Scott Wood17fed142016-05-30 13:57:56 -0500964 struct nand_chip *this = mtd_to_nand(mtd);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200965 int i, j, chips, block, nrblocks, update;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200966 uint8_t oldval;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200967
Sergey Lapin3a38a552013-01-14 03:46:50 +0000968 /* Do we have a bbt per chip? */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200969 if (td->options & NAND_BBT_PERCHIP) {
970 chips = this->numchips;
971 nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
972 } else {
973 chips = 1;
974 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200975 }
976
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200977 for (i = 0; i < chips; i++) {
978 if ((td->options & NAND_BBT_ABSPAGE) ||
979 !(td->options & NAND_BBT_WRITE)) {
William Juul52c07962007-10-31 13:53:06 +0100980 if (td->pages[i] == -1)
981 continue;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200982 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200983 oldval = bbt_get_entry(this, block);
984 bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
985 if ((oldval != BBT_BLOCK_RESERVED) &&
986 td->reserved_block_code)
987 nand_update_bbt(mtd, (loff_t)block <<
988 this->bbt_erase_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200989 continue;
990 }
991 update = 0;
992 if (td->options & NAND_BBT_LASTBLOCK)
993 block = ((i + 1) * nrblocks) - td->maxblocks;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +0200994 else
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200995 block = i * nrblocks;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +0200996 for (j = 0; j < td->maxblocks; j++) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200997 oldval = bbt_get_entry(this, block);
998 bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
999 if (oldval != BBT_BLOCK_RESERVED)
William Juul52c07962007-10-31 13:53:06 +01001000 update = 1;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001001 block++;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001002 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00001003 /*
1004 * If we want reserved blocks to be recorded to flash, and some
1005 * new ones have been marked, then we need to update the stored
1006 * bbts. This should only happen once.
1007 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001008 if (update && td->reserved_block_code)
Heiko Schocherf5895d12014-06-24 10:10:04 +02001009 nand_update_bbt(mtd, (loff_t)(block - 1) <<
1010 this->bbt_erase_shift);
Christian Hitz24e6ea42011-10-12 09:32:04 +02001011 }
1012}
1013
1014/**
1015 * verify_bbt_descr - verify the bad block description
Sergey Lapin3a38a552013-01-14 03:46:50 +00001016 * @mtd: MTD device structure
1017 * @bd: the table to verify
Christian Hitz24e6ea42011-10-12 09:32:04 +02001018 *
1019 * This functions performs a few sanity checks on the bad block description
1020 * table.
1021 */
1022static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
1023{
Scott Wood17fed142016-05-30 13:57:56 -05001024 struct nand_chip *this = mtd_to_nand(mtd);
Christian Hitz24e6ea42011-10-12 09:32:04 +02001025 u32 pattern_len;
1026 u32 bits;
1027 u32 table_size;
1028
1029 if (!bd)
1030 return;
1031
1032 pattern_len = bd->len;
1033 bits = bd->options & NAND_BBT_NRBITS_MSK;
1034
Sergey Lapin3a38a552013-01-14 03:46:50 +00001035 BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
1036 !(this->bbt_options & NAND_BBT_USE_FLASH));
Christian Hitz24e6ea42011-10-12 09:32:04 +02001037 BUG_ON(!bits);
1038
1039 if (bd->options & NAND_BBT_VERSION)
1040 pattern_len++;
1041
1042 if (bd->options & NAND_BBT_NO_OOB) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001043 BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
1044 BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
Christian Hitz24e6ea42011-10-12 09:32:04 +02001045 BUG_ON(bd->offs);
1046 if (bd->options & NAND_BBT_VERSION)
1047 BUG_ON(bd->veroffs != bd->len);
1048 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001049 }
Christian Hitz24e6ea42011-10-12 09:32:04 +02001050
1051 if (bd->options & NAND_BBT_PERCHIP)
1052 table_size = this->chipsize >> this->bbt_erase_shift;
1053 else
1054 table_size = mtd->size >> this->bbt_erase_shift;
1055 table_size >>= 3;
1056 table_size *= bits;
1057 if (bd->options & NAND_BBT_NO_OOB)
1058 table_size += pattern_len;
1059 BUG_ON(table_size > (1 << this->bbt_erase_shift));
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001060}
1061
1062/**
1063 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
Sergey Lapin3a38a552013-01-14 03:46:50 +00001064 * @mtd: MTD device structure
1065 * @bd: descriptor for the good/bad block search pattern
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001066 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001067 * The function checks, if a bad block table(s) is/are already available. If
1068 * not it scans the device for manufacturer marked good / bad blocks and writes
1069 * the bad block table(s) to the selected place.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001070 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001071 * The bad block table memory is allocated here. It must be freed by calling
1072 * the nand_free_bbt function.
1073 */
Scott Wood52ab7ce2016-05-30 13:57:58 -05001074static int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001075{
Scott Wood17fed142016-05-30 13:57:56 -05001076 struct nand_chip *this = mtd_to_nand(mtd);
Scott Wood52ab7ce2016-05-30 13:57:58 -05001077 int len, res;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001078 uint8_t *buf;
1079 struct nand_bbt_descr *td = this->bbt_td;
1080 struct nand_bbt_descr *md = this->bbt_md;
1081
Scott Wood52ab7ce2016-05-30 13:57:58 -05001082 len = (mtd->size >> (this->bbt_erase_shift + 2)) ? : 1;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001083 /*
1084 * Allocate memory (2bit per block) and clear the memory bad block
1085 * table.
1086 */
William Juul52c07962007-10-31 13:53:06 +01001087 this->bbt = kzalloc(len, GFP_KERNEL);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001088 if (!this->bbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001089 return -ENOMEM;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001090
Sergey Lapin3a38a552013-01-14 03:46:50 +00001091 /*
1092 * If no primary table decriptor is given, scan the device to build a
1093 * memory based bad block table.
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001094 */
William Juul52c07962007-10-31 13:53:06 +01001095 if (!td) {
1096 if ((res = nand_memory_bbt(mtd, bd))) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001097 pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
Scott Wood52ab7ce2016-05-30 13:57:58 -05001098 goto err;
William Juul52c07962007-10-31 13:53:06 +01001099 }
Scott Wood52ab7ce2016-05-30 13:57:58 -05001100 return 0;
William Juul52c07962007-10-31 13:53:06 +01001101 }
Christian Hitz24e6ea42011-10-12 09:32:04 +02001102 verify_bbt_descr(mtd, td);
1103 verify_bbt_descr(mtd, md);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001104
1105 /* Allocate a temporary buffer for one eraseblock incl. oob */
1106 len = (1 << this->bbt_erase_shift);
1107 len += (len >> this->page_shift) * mtd->oobsize;
William Juul52c07962007-10-31 13:53:06 +01001108 buf = vmalloc(len);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001109 if (!buf) {
Scott Wood52ab7ce2016-05-30 13:57:58 -05001110 res = -ENOMEM;
1111 goto err;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001112 }
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001113
Sergey Lapin3a38a552013-01-14 03:46:50 +00001114 /* Is the bbt at a given page? */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001115 if (td->options & NAND_BBT_ABSPAGE) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001116 read_abs_bbts(mtd, buf, td, md);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001117 } else {
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001118 /* Search the bad block table using a pattern in oob */
Sergey Lapin3a38a552013-01-14 03:46:50 +00001119 search_read_bbts(mtd, buf, td, md);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001120 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001121
Sergey Lapin3a38a552013-01-14 03:46:50 +00001122 res = check_create(mtd, buf, bd);
Scott Wood52ab7ce2016-05-30 13:57:58 -05001123 if (res)
1124 goto err;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001125
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001126 /* Prevent the bbt regions from erasing / writing */
William Juul52c07962007-10-31 13:53:06 +01001127 mark_bbt_region(mtd, td);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001128 if (md)
William Juul52c07962007-10-31 13:53:06 +01001129 mark_bbt_region(mtd, md);
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001130
William Juul52c07962007-10-31 13:53:06 +01001131 vfree(buf);
Scott Wood52ab7ce2016-05-30 13:57:58 -05001132 return 0;
1133
1134err:
1135 kfree(this->bbt);
1136 this->bbt = NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001137 return res;
1138}
1139
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001140/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001141 * nand_update_bbt - update bad block table(s)
Sergey Lapin3a38a552013-01-14 03:46:50 +00001142 * @mtd: MTD device structure
1143 * @offs: the offset of the newly marked block
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001144 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001145 * The function updates the bad block table(s).
1146 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02001147static int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001148{
Scott Wood17fed142016-05-30 13:57:56 -05001149 struct nand_chip *this = mtd_to_nand(mtd);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001150 int len, res = 0;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001151 int chip, chipsel;
1152 uint8_t *buf;
1153 struct nand_bbt_descr *td = this->bbt_td;
1154 struct nand_bbt_descr *md = this->bbt_md;
1155
1156 if (!this->bbt || !td)
1157 return -EINVAL;
1158
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001159 /* Allocate a temporary buffer for one eraseblock incl. oob */
1160 len = (1 << this->bbt_erase_shift);
1161 len += (len >> this->page_shift) * mtd->oobsize;
William Juul52c07962007-10-31 13:53:06 +01001162 buf = kmalloc(len, GFP_KERNEL);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001163 if (!buf)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001164 return -ENOMEM;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001165
Sergey Lapin3a38a552013-01-14 03:46:50 +00001166 /* Do we have a bbt per chip? */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001167 if (td->options & NAND_BBT_PERCHIP) {
William Juul52c07962007-10-31 13:53:06 +01001168 chip = (int)(offs >> this->chip_shift);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001169 chipsel = chip;
1170 } else {
1171 chip = 0;
1172 chipsel = -1;
1173 }
1174
1175 td->version[chip]++;
1176 if (md)
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001177 md->version[chip]++;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001178
Sergey Lapin3a38a552013-01-14 03:46:50 +00001179 /* Write the bad block table to the device? */
1180 if (td->options & NAND_BBT_WRITE) {
William Juul52c07962007-10-31 13:53:06 +01001181 res = write_bbt(mtd, buf, td, md, chipsel);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001182 if (res < 0)
1183 goto out;
1184 }
Sergey Lapin3a38a552013-01-14 03:46:50 +00001185 /* Write the mirror bad block table to the device? */
1186 if (md && (md->options & NAND_BBT_WRITE)) {
William Juul52c07962007-10-31 13:53:06 +01001187 res = write_bbt(mtd, buf, md, td, chipsel);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001188 }
1189
William Juul52c07962007-10-31 13:53:06 +01001190 out:
1191 kfree(buf);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001192 return res;
1193}
1194
Sergey Lapin3a38a552013-01-14 03:46:50 +00001195/*
1196 * Define some generic bad / good block scan pattern which are used
1197 * while scanning a device for factory marked good / bad blocks.
1198 */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001199static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1200
Sergey Lapin3a38a552013-01-14 03:46:50 +00001201/* Generic flash bbt descriptors */
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001202static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1203static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1204
1205static struct nand_bbt_descr bbt_main_descr = {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001206 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001207 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1208 .offs = 8,
1209 .len = 4,
1210 .veroffs = 12,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001211 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001212 .pattern = bbt_pattern
1213};
1214
1215static struct nand_bbt_descr bbt_mirror_descr = {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001216 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001217 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1218 .offs = 8,
1219 .len = 4,
1220 .veroffs = 12,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001221 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001222 .pattern = mirror_pattern
1223};
1224
Sergey Lapin3a38a552013-01-14 03:46:50 +00001225static struct nand_bbt_descr bbt_main_no_oob_descr = {
Christian Hitz24e6ea42011-10-12 09:32:04 +02001226 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1227 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1228 | NAND_BBT_NO_OOB,
1229 .len = 4,
1230 .veroffs = 4,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001231 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Christian Hitz24e6ea42011-10-12 09:32:04 +02001232 .pattern = bbt_pattern
1233};
1234
Sergey Lapin3a38a552013-01-14 03:46:50 +00001235static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
Christian Hitz24e6ea42011-10-12 09:32:04 +02001236 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1237 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1238 | NAND_BBT_NO_OOB,
1239 .len = 4,
1240 .veroffs = 4,
Sergey Lapin3a38a552013-01-14 03:46:50 +00001241 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Christian Hitz24e6ea42011-10-12 09:32:04 +02001242 .pattern = mirror_pattern
1243};
1244
Sergey Lapin3a38a552013-01-14 03:46:50 +00001245#define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
Christian Hitz24e6ea42011-10-12 09:32:04 +02001246/**
Sergey Lapin3a38a552013-01-14 03:46:50 +00001247 * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
1248 * @this: NAND chip to create descriptor for
Christian Hitz24e6ea42011-10-12 09:32:04 +02001249 *
1250 * This function allocates and initializes a nand_bbt_descr for BBM detection
Sergey Lapin3a38a552013-01-14 03:46:50 +00001251 * based on the properties of @this. The new descriptor is stored in
Christian Hitz24e6ea42011-10-12 09:32:04 +02001252 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1253 * passed to this function.
Christian Hitz24e6ea42011-10-12 09:32:04 +02001254 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00001255static int nand_create_badblock_pattern(struct nand_chip *this)
Christian Hitz24e6ea42011-10-12 09:32:04 +02001256{
1257 struct nand_bbt_descr *bd;
1258 if (this->badblock_pattern) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001259 pr_warn("Bad block pattern already allocated; not replacing\n");
Christian Hitz24e6ea42011-10-12 09:32:04 +02001260 return -EINVAL;
1261 }
1262 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
Sergey Lapin3a38a552013-01-14 03:46:50 +00001263 if (!bd)
Christian Hitz24e6ea42011-10-12 09:32:04 +02001264 return -ENOMEM;
Sergey Lapin3a38a552013-01-14 03:46:50 +00001265 bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
Christian Hitz24e6ea42011-10-12 09:32:04 +02001266 bd->offs = this->badblockpos;
1267 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1268 bd->pattern = scan_ff_pattern;
1269 bd->options |= NAND_BBT_DYNAMICSTRUCT;
1270 this->badblock_pattern = bd;
1271 return 0;
1272}
1273
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001274/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001275 * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
Sergey Lapin3a38a552013-01-14 03:46:50 +00001276 * @mtd: MTD device structure
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001277 *
Sergey Lapin3a38a552013-01-14 03:46:50 +00001278 * This function selects the default bad block table support for the device and
1279 * calls the nand_scan_bbt function.
1280 */
William Juul52c07962007-10-31 13:53:06 +01001281int nand_default_bbt(struct mtd_info *mtd)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001282{
Scott Wood17fed142016-05-30 13:57:56 -05001283 struct nand_chip *this = mtd_to_nand(mtd);
Scott Wood3ea94ed2015-06-26 19:03:26 -05001284 int ret;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001285
Sergey Lapin3a38a552013-01-14 03:46:50 +00001286 /* Is a flash based bad block table requested? */
1287 if (this->bbt_options & NAND_BBT_USE_FLASH) {
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001288 /* Use the default pattern descriptors */
1289 if (!this->bbt_td) {
Sergey Lapin3a38a552013-01-14 03:46:50 +00001290 if (this->bbt_options & NAND_BBT_NO_OOB) {
1291 this->bbt_td = &bbt_main_no_oob_descr;
1292 this->bbt_md = &bbt_mirror_no_oob_descr;
Christian Hitz24e6ea42011-10-12 09:32:04 +02001293 } else {
1294 this->bbt_td = &bbt_main_descr;
1295 this->bbt_md = &bbt_mirror_descr;
1296 }
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001297 }
1298 } else {
1299 this->bbt_td = NULL;
1300 this->bbt_md = NULL;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001301 }
Christian Hitz24e6ea42011-10-12 09:32:04 +02001302
Scott Wood3ea94ed2015-06-26 19:03:26 -05001303 if (!this->badblock_pattern) {
1304 ret = nand_create_badblock_pattern(this);
1305 if (ret)
1306 return ret;
1307 }
Christian Hitz24e6ea42011-10-12 09:32:04 +02001308
William Juul52c07962007-10-31 13:53:06 +01001309 return nand_scan_bbt(mtd, this->badblock_pattern);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001310}
1311
1312/**
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03001313 * nand_isreserved_bbt - [NAND Interface] Check if a block is reserved
1314 * @mtd: MTD device structure
1315 * @offs: offset in the device
1316 */
1317int nand_isreserved_bbt(struct mtd_info *mtd, loff_t offs)
1318{
Scott Wood17fed142016-05-30 13:57:56 -05001319 struct nand_chip *this = mtd_to_nand(mtd);
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -03001320 int block;
1321
1322 block = (int)(offs >> this->bbt_erase_shift);
1323 return bbt_get_entry(this, block) == BBT_BLOCK_RESERVED;
1324}
1325
1326/**
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001327 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
Sergey Lapin3a38a552013-01-14 03:46:50 +00001328 * @mtd: MTD device structure
1329 * @offs: offset in the device
1330 * @allowbbt: allow access to bad block table region
1331 */
William Juul52c07962007-10-31 13:53:06 +01001332int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001333{
Scott Wood17fed142016-05-30 13:57:56 -05001334 struct nand_chip *this = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001335 int block, res;
Wolfgang Denk7b9bc3a2005-09-14 23:53:32 +02001336
Heiko Schocherf5895d12014-06-24 10:10:04 +02001337 block = (int)(offs >> this->bbt_erase_shift);
1338 res = bbt_get_entry(this, block);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001339
Scott Wood3ea94ed2015-06-26 19:03:26 -05001340 pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1341 (unsigned int)offs, block, res);
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001342
Heiko Schocherf5895d12014-06-24 10:10:04 +02001343 switch (res) {
1344 case BBT_BLOCK_GOOD:
William Juul52c07962007-10-31 13:53:06 +01001345 return 0;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001346 case BBT_BLOCK_WORN:
William Juul52c07962007-10-31 13:53:06 +01001347 return 1;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001348 case BBT_BLOCK_RESERVED:
William Juul52c07962007-10-31 13:53:06 +01001349 return allowbbt ? 0 : 1;
Wolfgang Denk2f9b7e42005-08-17 12:55:25 +02001350 }
1351 return 1;
1352}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001353
1354/**
1355 * nand_markbad_bbt - [NAND Interface] Mark a block bad in the BBT
1356 * @mtd: MTD device structure
1357 * @offs: offset of the bad block
1358 */
1359int nand_markbad_bbt(struct mtd_info *mtd, loff_t offs)
1360{
Scott Wood17fed142016-05-30 13:57:56 -05001361 struct nand_chip *this = mtd_to_nand(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001362 int block, ret = 0;
1363
1364 block = (int)(offs >> this->bbt_erase_shift);
1365
1366 /* Mark bad block in memory */
1367 bbt_mark_entry(this, block, BBT_BLOCK_WORN);
1368
1369 /* Update flash-based bad block table */
1370 if (this->bbt_options & NAND_BBT_USE_FLASH)
1371 ret = nand_update_bbt(mtd, offs);
1372
1373 return ret;
1374}