blob: 1b722fe9213c31ca56fedd21736eb1e7b678c9f2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Overview:
3 * Bad block table support for the NAND driver
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004 *
Fabio Estevamd159c4e2012-07-28 19:29:25 -03005 * Copyright © 2004 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000013 * When nand_scan_bbt is called, then it tries to find the bad block table
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020014 * depending on the options in the BBT descriptor(s). If no flash based BBT
Brian Norrisbb9ebd4e2011-05-31 16:31:23 -070015 * (NAND_BBT_USE_FLASH) is specified then the device is scanned for factory
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +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
Huang Shijie44ed0ff2012-07-03 16:44:15 +080023 * version number, then the mirror BBT is used to build the memory based BBT.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * If the tables are not versioned, then we "or" the bad block information.
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +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
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000027 * good / bad blocks and the bad block tables are created.
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 *
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020029 * For manufacturer created BBTs like the one found on M-SYS DOC devices
30 * the BBT is searched and read but never created
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 *
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020032 * The auto generated bad block table is located in the last good blocks
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000033 * of the device. The table is mirrored, so it can be updated eventually.
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +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
Brian Norrisbb9ebd4e2011-05-31 16:31:23 -070037 * option NAND_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of
Brian Norrisa40f7342011-05-31 16:31:22 -070038 * course): it moves the ident pattern and the version byte into the data area
39 * and the OOB area will remain untouched.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 *
41 * The table uses 2 bits per block
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020042 * 11b: block is good
43 * 00b: block is factory marked bad
44 * 01b, 10b: block is marked bad due to wear
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 *
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
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000051 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * 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
David Woodhousee0c7d762006-05-13 18:07:53 +010056 * - the space necessary for a bbt in FLASH does not exceed a block boundary
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000057 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 */
59
60#include <linux/slab.h>
61#include <linux/types.h>
62#include <linux/mtd/mtd.h>
Richard Genoud61de9da2012-09-11 15:50:54 +020063#include <linux/mtd/bbm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/bitops.h>
65#include <linux/delay.h>
David Woodhousec3f8abf2006-05-13 04:03:42 +010066#include <linux/vmalloc.h>
Paul Gortmakerf3bcc012011-07-10 12:43:28 -040067#include <linux/export.h>
Brian Norris491ed062012-06-22 16:35:44 -070068#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Boris Brezillon348d56a2018-09-07 00:38:48 +020070#include "internals.h"
71
Brian Norris771c5682013-07-30 17:52:55 -070072#define BBT_BLOCK_GOOD 0x00
73#define BBT_BLOCK_WORN 0x01
74#define BBT_BLOCK_RESERVED 0x02
75#define BBT_BLOCK_FACTORY_BAD 0x03
76
77#define BBT_ENTRY_MASK 0x03
78#define BBT_ENTRY_SHIFT 2
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}
93
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020094static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
95{
Brian Norris718894a2012-06-22 16:35:43 -070096 if (memcmp(buf, td->pattern, td->len))
97 return -1;
98 return 0;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020099}
100
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000101/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * check_pattern - [GENERIC] check if a pattern is in the buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700103 * @buf: the buffer to search
104 * @len: the length of buffer to search
105 * @paglen: the pagelength
106 * @td: search pattern descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700108 * Check for a pattern at the given place. Used to search bad block tables and
Brian Norrisdad22562013-07-30 17:53:00 -0700109 * good / bad block identifiers.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700110 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100111static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200113 if (td->options & NAND_BBT_NO_OOB)
114 return check_pattern_no_oob(buf, td);
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 /* Compare the pattern */
Brian Norrisdad22562013-07-30 17:53:00 -0700117 if (memcmp(buf + paglen + td->offs, td->pattern, td->len))
Brian Norris75b66d82011-09-07 13:13:41 -0700118 return -1;
Brian Norris58373ff2010-07-15 12:15:44 -0700119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return 0;
121}
122
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000123/**
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100124 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700125 * @buf: the buffer to search
126 * @td: search pattern descriptor
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100127 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700128 * 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 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100132static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100133{
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100134 /* Compare the pattern */
Brian Norris491ed062012-06-22 16:35:44 -0700135 if (memcmp(buf + td->offs, td->pattern, td->len))
136 return -1;
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100137 return 0;
138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200141 * add_marker_len - compute the length of the marker in data area
Brian Norris8b6e50c2011-05-25 14:59:01 -0700142 * @td: BBT descriptor used for computation
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200143 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700144 * The length will be 0 if the marker is located in OOB area.
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +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/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 * read_bbt - [GENERIC] Read the bad block table starting from page
Boris Brezillon08136212018-11-11 08:55:03 +0100161 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700162 * @buf: temporary buffer
163 * @page: the starting page
164 * @num: the number of bbt descriptors to read
Brian Norris7854d3f2011-06-23 14:12:08 -0700165 * @td: the bbt describtion table
Brian Norrisb4d20d62013-07-30 17:52:56 -0700166 * @offs: block number offset in the table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 *
168 * Read the bad block table starting from page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 */
Boris Brezillon08136212018-11-11 08:55:03 +0100170static int read_bbt(struct nand_chip *this, uint8_t *buf, int page, int num,
171 struct nand_bbt_descr *td, int offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Boris Brezillon08136212018-11-11 08:55:03 +0100173 struct mtd_info *mtd = nand_to_mtd(this);
Brian Norris167a8d52011-09-20 18:35:08 -0700174 int res, ret = 0, i, j, act = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 size_t retlen, len, totlen;
176 loff_t from;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200177 int bits = td->options & NAND_BBT_NRBITS_MSK;
Brian Norris596d7452011-09-07 13:13:33 -0700178 uint8_t msk = (uint8_t)((1 << bits) - 1);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200179 u32 marker_len;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200180 int reserved_block_code = td->reserved_block_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 totlen = (num * bits) >> 3;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200183 marker_len = add_marker_len(td);
Brian Norris596d7452011-09-07 13:13:33 -0700184 from = ((loff_t)page) << this->page_shift;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 while (totlen) {
Brian Norris596d7452011-09-07 13:13:33 -0700187 len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +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 }
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200197 res = mtd_read(mtd, from, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (res < 0) {
Brian Norris167a8d52011-09-20 18:35:08 -0700199 if (mtd_is_eccerr(res)) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200200 pr_info("nand_bbt: ECC error in BBT at 0x%012llx\n",
201 from & ~mtd->writesize);
Brian Norris167a8d52011-09-20 18:35:08 -0700202 return res;
203 } else if (mtd_is_bitflip(res)) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200204 pr_info("nand_bbt: corrected error in BBT at 0x%012llx\n",
205 from & ~mtd->writesize);
Brian Norris167a8d52011-09-20 18:35:08 -0700206 ret = res;
207 } else {
208 pr_info("nand_bbt: error reading BBT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return res;
210 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 /* Analyse data */
214 for (i = 0; i < len; i++) {
215 uint8_t dat = buf[i];
Brian Norrisb4d20d62013-07-30 17:52:56 -0700216 for (j = 0; j < 8; j += bits, act++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 uint8_t tmp = (dat >> j) & msk;
218 if (tmp == msk)
219 continue;
David Woodhousee0c7d762006-05-13 18:07:53 +0100220 if (reserved_block_code && (tmp == reserved_block_code)) {
Brian Norrisd0370212011-07-19 10:06:08 -0700221 pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
Brian Norrisb4d20d62013-07-30 17:52:56 -0700222 (loff_t)(offs + act) <<
223 this->bbt_erase_shift);
224 bbt_mark_entry(this, offs + act,
Brian Norris771c5682013-07-30 17:52:55 -0700225 BBT_BLOCK_RESERVED);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200226 mtd->ecc_stats.bbtblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 continue;
228 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700229 /*
230 * Leave it for now, if it's matured we can
Brian Norrisa0f50802011-07-19 10:06:06 -0700231 * move this message to pr_debug.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700232 */
Brian Norrisd0370212011-07-19 10:06:08 -0700233 pr_info("nand_read_bbt: bad block at 0x%012llx\n",
Brian Norrisb4d20d62013-07-30 17:52:56 -0700234 (loff_t)(offs + act) <<
235 this->bbt_erase_shift);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700236 /* Factory marked bad or worn out? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (tmp == 0)
Brian Norrisb4d20d62013-07-30 17:52:56 -0700238 bbt_mark_entry(this, offs + act,
Brian Norris771c5682013-07-30 17:52:55 -0700239 BBT_BLOCK_FACTORY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 else
Brian Norrisb4d20d62013-07-30 17:52:56 -0700241 bbt_mark_entry(this, offs + act,
Brian Norris771c5682013-07-30 17:52:55 -0700242 BBT_BLOCK_WORN);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200243 mtd->ecc_stats.badblocks++;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246 totlen -= len;
247 from += len;
248 }
Brian Norris167a8d52011-09-20 18:35:08 -0700249 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252/**
253 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
Boris Brezillon08136212018-11-11 08:55:03 +0100254 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700255 * @buf: temporary buffer
256 * @td: descriptor for the bad block table
Brian Norris596d7452011-09-07 13:13:33 -0700257 * @chip: read the table for a specific chip, -1 read all chips; applies only if
Brian Norris8b6e50c2011-05-25 14:59:01 -0700258 * NAND_BBT_PERCHIP option is set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700260 * 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 */
Boris Brezillon08136212018-11-11 08:55:03 +0100263static int read_abs_bbt(struct nand_chip *this, uint8_t *buf,
264 struct nand_bbt_descr *td, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Boris Brezillon08136212018-11-11 08:55:03 +0100266 struct mtd_info *mtd = nand_to_mtd(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 int res = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (td->options & NAND_BBT_PERCHIP) {
270 int offs = 0;
271 for (i = 0; i < this->numchips; i++) {
272 if (chip == -1 || chip == i)
Boris Brezillon08136212018-11-11 08:55:03 +0100273 res = read_bbt(this, buf, td->pages[i],
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200274 this->chipsize >> this->bbt_erase_shift,
275 td, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (res)
277 return res;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700278 offs += this->chipsize >> this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280 } else {
Boris Brezillon08136212018-11-11 08:55:03 +0100281 res = read_bbt(this, buf, td->pages[0],
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200282 mtd->size >> this->bbt_erase_shift, td, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (res)
284 return res;
285 }
286 return 0;
287}
288
Brian Norris8b6e50c2011-05-25 14:59:01 -0700289/* BBT marker is in the first page, no OOB */
Boris Brezillon08136212018-11-11 08:55:03 +0100290static int scan_read_data(struct nand_chip *this, uint8_t *buf, loff_t offs,
291 struct nand_bbt_descr *td)
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200292{
Boris Brezillon08136212018-11-11 08:55:03 +0100293 struct mtd_info *mtd = nand_to_mtd(this);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200294 size_t retlen;
295 size_t len;
296
297 len = td->len;
298 if (td->options & NAND_BBT_VERSION)
299 len++;
300
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200301 return mtd_read(mtd, offs, len, &retlen, buf);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200302}
303
Brian Norrisa7e68832012-06-22 16:35:45 -0700304/**
Brian Norrisaf69dcd2012-06-22 16:35:42 -0700305 * scan_read_oob - [GENERIC] Scan data+OOB region to buffer
Boris Brezillon08136212018-11-11 08:55:03 +0100306 * @this: NAND chip object
Brian Norrisa7e68832012-06-22 16:35:45 -0700307 * @buf: temporary buffer
308 * @offs: offset at which to scan
309 * @len: length of data region to read
310 *
311 * Scan read data from data+OOB. May traverse multiple pages, interleaving
312 * page,OOB,page,OOB,... in buf. Completes transfer and returns the "strongest"
313 * ECC condition (error or bitflip). May quit on the first (non-ECC) error.
314 */
Boris Brezillon08136212018-11-11 08:55:03 +0100315static int scan_read_oob(struct nand_chip *this, uint8_t *buf, loff_t offs,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200316 size_t len)
317{
Boris Brezillon08136212018-11-11 08:55:03 +0100318 struct mtd_info *mtd = nand_to_mtd(this);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200319 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
Boris Brezillon08136212018-11-11 08:55:03 +0100346static int scan_read(struct nand_chip *this, uint8_t *buf, loff_t offs,
347 size_t len, struct nand_bbt_descr *td)
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200348{
349 if (td->options & NAND_BBT_NO_OOB)
Boris Brezillon08136212018-11-11 08:55:03 +0100350 return scan_read_data(this, buf, offs, td);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200351 else
Boris Brezillon08136212018-11-11 08:55:03 +0100352 return scan_read_oob(this, 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 */
Boris Brezillon08136212018-11-11 08:55:03 +0100356static int scan_write_bbt(struct nand_chip *this, loff_t offs, size_t len,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200357 uint8_t *buf, uint8_t *oob)
358{
Boris Brezillon08136212018-11-11 08:55:03 +0100359 struct mtd_info *mtd = nand_to_mtd(this);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200360 struct mtd_oob_ops ops;
361
Brian Norris0612b9d2011-08-30 18:45:40 -0700362 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200363 ops.ooboffs = 0;
364 ops.ooblen = mtd->oobsize;
365 ops.datbuf = buf;
366 ops.oobbuf = oob;
367 ops.len = len;
368
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200369 return mtd_write_oob(mtd, offs, &ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200370}
371
Boris Brezillon08136212018-11-11 08:55:03 +0100372static u32 bbt_get_ver_offs(struct nand_chip *this, struct nand_bbt_descr *td)
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200373{
Boris Brezillon08136212018-11-11 08:55:03 +0100374 struct mtd_info *mtd = nand_to_mtd(this);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200375 u32 ver_offs = td->veroffs;
376
377 if (!(td->options & NAND_BBT_NO_OOB))
378 ver_offs += mtd->writesize;
379 return ver_offs;
380}
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382/**
383 * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
Boris Brezillon08136212018-11-11 08:55:03 +0100384 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700385 * @buf: temporary buffer
386 * @td: descriptor for the bad block table
387 * @md: descriptor for the bad block table mirror
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700389 * Read the bad block table(s) for all chips starting at a given page. We
390 * assume that the bbt bits are in consecutive order.
391 */
Boris Brezillon08136212018-11-11 08:55:03 +0100392static void read_abs_bbts(struct nand_chip *this, uint8_t *buf,
Brian Norris7b5a2d42012-06-22 16:35:41 -0700393 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Boris Brezillon08136212018-11-11 08:55:03 +0100395 struct mtd_info *mtd = nand_to_mtd(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000397 /* Read the primary version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (td->options & NAND_BBT_VERSION) {
Boris Brezillon08136212018-11-11 08:55:03 +0100399 scan_read(this, buf, (loff_t)td->pages[0] << this->page_shift,
400 mtd->writesize, td);
401 td->version[0] = buf[bbt_get_ver_offs(this, td)];
Brian Norris9a4d4d62011-07-19 10:06:07 -0700402 pr_info("Bad block table at page %d, version 0x%02X\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700403 td->pages[0], td->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000406 /* Read the mirror version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (md && (md->options & NAND_BBT_VERSION)) {
Boris Brezillon08136212018-11-11 08:55:03 +0100408 scan_read(this, buf, (loff_t)md->pages[0] << this->page_shift,
409 mtd->writesize, md);
410 md->version[0] = buf[bbt_get_ver_offs(this, md)];
Brian Norris9a4d4d62011-07-19 10:06:07 -0700411 pr_info("Bad block table at page %d, version 0x%02X\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700412 md->pages[0], md->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Brian Norris8b6e50c2011-05-25 14:59:01 -0700416/* Scan a given block partially */
Boris Brezillon08136212018-11-11 08:55:03 +0100417static int scan_block_fast(struct nand_chip *this, struct nand_bbt_descr *bd,
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300418 loff_t offs, uint8_t *buf, int numpages)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200419{
Boris Brezillon08136212018-11-11 08:55:03 +0100420 struct mtd_info *mtd = nand_to_mtd(this);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200421 struct mtd_oob_ops ops;
422 int j, ret;
423
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200424 ops.ooblen = mtd->oobsize;
425 ops.oobbuf = buf;
426 ops.ooboffs = 0;
427 ops.datbuf = NULL;
Brian Norris0612b9d2011-08-30 18:45:40 -0700428 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200429
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300430 for (j = 0; j < numpages; j++) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200431 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700432 * Read the full oob until read_oob is fixed to handle single
433 * byte reads for 16 bit buswidth.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200434 */
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200435 ret = mtd_read_oob(mtd, offs, &ops);
Brian Norris903cd062011-06-28 16:28:58 -0700436 /* Ignore ECC errors when checking for BBM */
Brian Norrisd57f40542011-09-20 18:34:25 -0700437 if (ret && !mtd_is_bitflip_or_eccerr(ret))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200438 return ret;
439
440 if (check_short_pattern(buf, bd))
441 return 1;
442
443 offs += mtd->writesize;
444 }
445 return 0;
446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448/**
449 * create_bbt - [GENERIC] Create a bad block table by scanning the device
Boris Brezillon08136212018-11-11 08:55:03 +0100450 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700451 * @buf: temporary buffer
452 * @bd: descriptor for the good/bad block search pattern
453 * @chip: create the table for a specific chip, -1 read all chips; applies only
454 * if NAND_BBT_PERCHIP option is set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700456 * Create a bad block table by scanning the device for the given good/bad block
457 * identify pattern.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 */
Boris Brezillon08136212018-11-11 08:55:03 +0100459static int create_bbt(struct nand_chip *this, uint8_t *buf,
460 struct nand_bbt_descr *bd, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Boris Brezillon08136212018-11-11 08:55:03 +0100462 struct mtd_info *mtd = nand_to_mtd(this);
Brian Norris5961ad22013-10-30 00:41:30 -0400463 int i, numblocks, numpages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 int startblock;
465 loff_t from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Brian Norris9a4d4d62011-07-19 10:06:07 -0700467 pr_info("Scanning device for bad blocks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Brian Norris5961ad22013-10-30 00:41:30 -0400469 if (bd->options & NAND_BBT_SCAN2NDPAGE)
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300470 numpages = 2;
Brian Norris58373ff2010-07-15 12:15:44 -0700471 else
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300472 numpages = 1;
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (chip == -1) {
Brian Norrisb4d20d62013-07-30 17:52:56 -0700475 numblocks = mtd->size >> this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 startblock = 0;
477 from = 0;
478 } else {
479 if (chip >= this->numchips) {
Brian Norris9a4d4d62011-07-19 10:06:07 -0700480 pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
David Woodhousee0c7d762006-05-13 18:07:53 +0100481 chip + 1, this->numchips);
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000482 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
Brian Norrisb4d20d62013-07-30 17:52:56 -0700484 numblocks = this->chipsize >> this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 startblock = chip * numblocks;
486 numblocks += startblock;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700487 from = (loff_t)startblock << this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000489
Brian Norris5fb15492011-05-31 16:31:21 -0700490 if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300491 from += mtd->erasesize - (mtd->writesize * numpages);
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700492
Brian Norrisb4d20d62013-07-30 17:52:56 -0700493 for (i = startblock; i < numblocks; i++) {
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000494 int ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000495
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200496 BUG_ON(bd->options & NAND_BBT_NO_OOB);
497
Boris Brezillon08136212018-11-11 08:55:03 +0100498 ret = scan_block_fast(this, bd, from, buf, numpages);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200499 if (ret < 0)
500 return ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000501
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200502 if (ret) {
Brian Norrisb4d20d62013-07-30 17:52:56 -0700503 bbt_mark_entry(this, i, BBT_BLOCK_FACTORY_BAD);
Brian Norris9a4d4d62011-07-19 10:06:07 -0700504 pr_warn("Bad eraseblock %d at 0x%012llx\n",
Brian Norrisb4d20d62013-07-30 17:52:56 -0700505 i, (unsigned long long)from);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200506 mtd->ecc_stats.badblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 from += (1 << this->bbt_erase_shift);
510 }
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
514/**
515 * search_bbt - [GENERIC] scan the device for a specific bad block table
Boris Brezillon08136212018-11-11 08:55:03 +0100516 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700517 * @buf: temporary buffer
518 * @td: descriptor for the bad block table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700520 * Read the bad block table by searching for a given ident pattern. Search is
521 * preformed either from the beginning up or from the end of the device
522 * downwards. The search starts always at the start of a block. If the option
523 * NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains
524 * the bad block information of this chip. This is necessary to provide support
525 * for certain DOC devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700527 * The bbt ident pattern resides in the oob area of the first page in a block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 */
Boris Brezillon08136212018-11-11 08:55:03 +0100529static int search_bbt(struct nand_chip *this, uint8_t *buf,
530 struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Boris Brezillon08136212018-11-11 08:55:03 +0100532 struct mtd_info *mtd = nand_to_mtd(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 int i, chips;
Brian Norris930de532014-01-02 17:14:10 -0800534 int startblock, block, dir;
Joern Engel28318772006-05-22 23:18:05 +0200535 int scanlen = mtd->writesize + mtd->oobsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 int bbtblocks;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200537 int blocktopage = this->bbt_erase_shift - this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Brian Norris8b6e50c2011-05-25 14:59:01 -0700539 /* Search direction top -> down? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (td->options & NAND_BBT_LASTBLOCK) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100541 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 dir = -1;
543 } else {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000544 startblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000546 }
547
Brian Norris8b6e50c2011-05-25 14:59:01 -0700548 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (td->options & NAND_BBT_PERCHIP) {
550 chips = this->numchips;
551 bbtblocks = this->chipsize >> this->bbt_erase_shift;
552 startblock &= bbtblocks - 1;
553 } else {
554 chips = 1;
555 bbtblocks = mtd->size >> this->bbt_erase_shift;
556 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 for (i = 0; i < chips; i++) {
559 /* Reset version information */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000560 td->version[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 td->pages[i] = -1;
562 /* Scan the maximum number of blocks */
563 for (block = 0; block < td->maxblocks; block++) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 int actblock = startblock + dir * block;
Adrian Hunter69423d92008-12-10 13:37:21 +0000566 loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* Read first page */
Boris Brezillon08136212018-11-11 08:55:03 +0100569 scan_read(this, buf, offs, mtd->writesize, td);
Joern Engel28318772006-05-22 23:18:05 +0200570 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200571 td->pages[i] = actblock << blocktopage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (td->options & NAND_BBT_VERSION) {
Boris Brezillon08136212018-11-11 08:55:03 +0100573 offs = bbt_get_ver_offs(this, td);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200574 td->version[i] = buf[offs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576 break;
577 }
578 }
579 startblock += this->chipsize >> this->bbt_erase_shift;
580 }
581 /* Check, if we found a bbt for each requested chip */
582 for (i = 0; i < chips; i++) {
583 if (td->pages[i] == -1)
Brian Norris9a4d4d62011-07-19 10:06:07 -0700584 pr_warn("Bad block table not found for chip %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 else
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200586 pr_info("Bad block table found at page %d, version 0x%02X\n",
587 td->pages[i], td->version[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
592/**
593 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
Boris Brezillon08136212018-11-11 08:55:03 +0100594 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700595 * @buf: temporary buffer
596 * @td: descriptor for the bad block table
597 * @md: descriptor for the bad block table mirror
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700599 * Search and read the bad block table(s).
600 */
Boris Brezillon08136212018-11-11 08:55:03 +0100601static void search_read_bbts(struct nand_chip *this, uint8_t *buf,
Brian Norris7b5a2d42012-06-22 16:35:41 -0700602 struct nand_bbt_descr *td,
603 struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 /* Search the primary table */
Boris Brezillon08136212018-11-11 08:55:03 +0100606 search_bbt(this, buf, td);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /* Search the mirror table */
609 if (md)
Boris Brezillon08136212018-11-11 08:55:03 +0100610 search_bbt(this, buf, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000611}
612
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000613/**
Boris Brezillonc3baf272016-08-15 18:22:01 -0500614 * get_bbt_block - Get the first valid eraseblock suitable to store a BBT
615 * @this: the NAND device
616 * @td: the BBT description
617 * @md: the mirror BBT descriptor
618 * @chip: the CHIP selector
619 *
620 * This functions returns a positive block number pointing a valid eraseblock
621 * suitable to store a BBT (i.e. in the range reserved for BBT), or -ENOSPC if
622 * all blocks are already used of marked bad. If td->pages[chip] was already
623 * pointing to a valid block we re-use it, otherwise we search for the next
624 * valid one.
625 */
626static int get_bbt_block(struct nand_chip *this, struct nand_bbt_descr *td,
627 struct nand_bbt_descr *md, int chip)
628{
629 int startblock, dir, page, numblocks, i;
630
631 /*
632 * There was already a version of the table, reuse the page. This
633 * applies for absolute placement too, as we have the page number in
634 * td->pages.
635 */
636 if (td->pages[chip] != -1)
637 return td->pages[chip] >>
638 (this->bbt_erase_shift - this->page_shift);
639
640 numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
641 if (!(td->options & NAND_BBT_PERCHIP))
642 numblocks *= this->numchips;
643
644 /*
645 * Automatic placement of the bad block table. Search direction
646 * top -> down?
647 */
648 if (td->options & NAND_BBT_LASTBLOCK) {
649 startblock = numblocks * (chip + 1) - 1;
650 dir = -1;
651 } else {
652 startblock = chip * numblocks;
653 dir = 1;
654 }
655
656 for (i = 0; i < td->maxblocks; i++) {
657 int block = startblock + dir * i;
658
659 /* Check, if the block is bad */
660 switch (bbt_get_entry(this, block)) {
661 case BBT_BLOCK_WORN:
662 case BBT_BLOCK_FACTORY_BAD:
663 continue;
664 }
665
666 page = block << (this->bbt_erase_shift - this->page_shift);
667
668 /* Check, if the block is used by the mirror table */
669 if (!md || md->pages[chip] != page)
670 return block;
671 }
672
673 return -ENOSPC;
674}
675
676/**
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500677 * mark_bbt_block_bad - Mark one of the block reserved for BBT bad
678 * @this: the NAND device
679 * @td: the BBT description
680 * @chip: the CHIP selector
681 * @block: the BBT block to mark
682 *
683 * Blocks reserved for BBT can become bad. This functions is an helper to mark
684 * such blocks as bad. It takes care of updating the in-memory BBT, marking the
685 * block as bad using a bad block marker and invalidating the associated
686 * td->pages[] entry.
687 */
688static void mark_bbt_block_bad(struct nand_chip *this,
689 struct nand_bbt_descr *td,
690 int chip, int block)
691{
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500692 loff_t to;
693 int res;
694
695 bbt_mark_entry(this, block, BBT_BLOCK_WORN);
696
697 to = (loff_t)block << this->bbt_erase_shift;
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200698 res = nand_markbad_bbm(this, to);
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500699 if (res)
700 pr_warn("nand_bbt: error %d while marking block %d bad\n",
701 res, block);
702
703 td->pages[chip] = -1;
704}
705
706/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 * write_bbt - [GENERIC] (Re)write the bad block table
Boris Brezillon08136212018-11-11 08:55:03 +0100708 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700709 * @buf: temporary buffer
710 * @td: descriptor for the bad block table
711 * @md: descriptor for the bad block table mirror
712 * @chipsel: selector for a specific chip, -1 for all
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700714 * (Re)write the bad block table.
715 */
Boris Brezillon08136212018-11-11 08:55:03 +0100716static int write_bbt(struct nand_chip *this, uint8_t *buf,
Thomas Gleixner9223a452006-05-23 17:21:03 +0200717 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
718 int chipsel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Boris Brezillon08136212018-11-11 08:55:03 +0100720 struct mtd_info *mtd = nand_to_mtd(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 struct erase_info einfo;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700722 int i, res, chip = 0;
Boris Brezillonc3baf272016-08-15 18:22:01 -0500723 int bits, page, offs, numblocks, sft, sftmsk;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700724 int nrchips, pageoffs, ooboffs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 uint8_t msk[4];
726 uint8_t rcode = td->reserved_block_code;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200727 size_t retlen, len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 loff_t to;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200729 struct mtd_oob_ops ops;
730
731 ops.ooblen = mtd->oobsize;
732 ops.ooboffs = 0;
733 ops.datbuf = NULL;
Brian Norris0612b9d2011-08-30 18:45:40 -0700734 ops.mode = MTD_OPS_PLACE_OOB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 if (!rcode)
737 rcode = 0xff;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700738 /* Write bad block table per chip rather than per device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (td->options & NAND_BBT_PERCHIP) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100740 numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700741 /* Full device write or specific chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (chipsel == -1) {
743 nrchips = this->numchips;
744 } else {
745 nrchips = chipsel + 1;
746 chip = chipsel;
747 }
748 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +0100749 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 nrchips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000751 }
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 /* Loop through the chips */
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500754 while (chip < nrchips) {
Boris Brezillonc3baf272016-08-15 18:22:01 -0500755 int block;
756
757 block = get_bbt_block(this, td, md, chip);
758 if (block < 0) {
759 pr_err("No space left to write bad block table\n");
760 res = block;
761 goto outerr;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Brian Norris8b6e50c2011-05-25 14:59:01 -0700764 /*
Boris Brezillonc3baf272016-08-15 18:22:01 -0500765 * get_bbt_block() returns a block number, shift the value to
766 * get a page number.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700767 */
Boris Brezillonc3baf272016-08-15 18:22:01 -0500768 page = block << (this->bbt_erase_shift - this->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 /* Set up shift count and masks for the flash table */
771 bits = td->options & NAND_BBT_NRBITS_MSK;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200772 msk[2] = ~rcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 switch (bits) {
Thomas Gleixner9223a452006-05-23 17:21:03 +0200774 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
775 msk[3] = 0x01;
776 break;
777 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
778 msk[3] = 0x03;
779 break;
780 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
781 msk[3] = 0x0f;
782 break;
783 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
784 msk[3] = 0xff;
785 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 default: return -EINVAL;
787 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000788
Brian Norris596d7452011-09-07 13:13:33 -0700789 to = ((loff_t)page) << this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Brian Norris8b6e50c2011-05-25 14:59:01 -0700791 /* Must we save the block contents? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (td->options & NAND_BBT_SAVECONTENT) {
793 /* Make it block aligned */
Brian Norrisf5cd2ae2015-02-28 02:13:13 -0800794 to &= ~(((loff_t)1 << this->bbt_erase_shift) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 len = 1 << this->bbt_erase_shift;
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200796 res = mtd_read(mtd, to, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (res < 0) {
798 if (retlen != len) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200799 pr_info("nand_bbt: error reading block for writing the bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return res;
801 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200802 pr_warn("nand_bbt: ECC error while reading block for writing bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200804 /* Read oob data */
Vitaly Wool70145682006-11-03 18:20:38 +0300805 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200806 ops.oobbuf = &buf[len];
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200807 res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
Vitaly Wool70145682006-11-03 18:20:38 +0300808 if (res < 0 || ops.oobretlen != ops.ooblen)
Thomas Gleixner9223a452006-05-23 17:21:03 +0200809 goto outerr;
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /* Calc the byte offset in the buffer */
812 pageoffs = page - (int)(to >> this->page_shift);
813 offs = pageoffs << this->page_shift;
814 /* Preset the bbt area with 0xff */
Brian Norris596d7452011-09-07 13:13:33 -0700815 memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
Thomas Gleixner9223a452006-05-23 17:21:03 +0200816 ooboffs = len + (pageoffs * mtd->oobsize);
817
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200818 } else if (td->options & NAND_BBT_NO_OOB) {
819 ooboffs = 0;
820 offs = td->len;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700821 /* The version byte */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200822 if (td->options & NAND_BBT_VERSION)
823 offs++;
824 /* Calc length */
Brian Norris596d7452011-09-07 13:13:33 -0700825 len = (size_t)(numblocks >> sft);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200826 len += offs;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700827 /* Make it page aligned! */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200828 len = ALIGN(len, mtd->writesize);
829 /* Preset the buffer with 0xff */
830 memset(buf, 0xff, len);
831 /* Pattern is located at the begin of first page */
832 memcpy(buf, td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 } else {
834 /* Calc length */
Brian Norris596d7452011-09-07 13:13:33 -0700835 len = (size_t)(numblocks >> sft);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700836 /* Make it page aligned! */
Sebastian Andrzej Siewiorcda32092010-09-29 19:43:50 +0200837 len = ALIGN(len, mtd->writesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /* Preset the buffer with 0xff */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200839 memset(buf, 0xff, len +
840 (len >> this->page_shift)* mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 offs = 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200842 ooboffs = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 /* Pattern is located in oob area of first page */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200844 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000846
Thomas Gleixner9223a452006-05-23 17:21:03 +0200847 if (td->options & NAND_BBT_VERSION)
848 buf[ooboffs + td->veroffs] = td->version[chip];
849
Brian Norris8b6e50c2011-05-25 14:59:01 -0700850 /* Walk through the memory table */
Brian Norrisb4d20d62013-07-30 17:52:56 -0700851 for (i = 0; i < numblocks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 uint8_t dat;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700853 int sftcnt = (i << (3 - sft)) & sftmsk;
854 dat = bbt_get_entry(this, chip * numblocks + i);
855 /* Do not store the reserved bbt blocks! */
856 buf[offs + (i >> sft)] &= ~(msk[dat] << sftcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000858
David Woodhousee0c7d762006-05-13 18:07:53 +0100859 memset(&einfo, 0, sizeof(einfo));
Adrian Hunter69423d92008-12-10 13:37:21 +0000860 einfo.addr = to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 einfo.len = 1 << this->bbt_erase_shift;
Boris Brezillone4cdf9c2018-09-06 14:05:35 +0200862 res = nand_erase_nand(this, &einfo, 1);
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500863 if (res < 0) {
864 pr_warn("nand_bbt: error while erasing BBT block %d\n",
865 res);
866 mark_bbt_block_bad(this, td, chip, block);
867 continue;
868 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000869
Boris Brezillon08136212018-11-11 08:55:03 +0100870 res = scan_write_bbt(this, to, len, buf,
871 td->options & NAND_BBT_NO_OOB ?
872 NULL : &buf[len]);
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500873 if (res < 0) {
874 pr_warn("nand_bbt: error while writing BBT block %d\n",
875 res);
876 mark_bbt_block_bad(this, td, chip, block);
877 continue;
878 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200879
Brian Norrisd0370212011-07-19 10:06:08 -0700880 pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
881 (unsigned long long)to, td->version[chip]);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 /* Mark it as used */
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500884 td->pages[chip++] = page;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200887
888 outerr:
Brian Norrisd0370212011-07-19 10:06:08 -0700889 pr_warn("nand_bbt: error while writing bad block table %d\n", res);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200890 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893/**
894 * nand_memory_bbt - [GENERIC] create a memory based bad block table
Boris Brezillon08136212018-11-11 08:55:03 +0100895 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700896 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700898 * The function creates a memory based bbt by scanning the device for
899 * manufacturer / software marked good / bad blocks.
900 */
Boris Brezillon08136212018-11-11 08:55:03 +0100901static inline int nand_memory_bbt(struct nand_chip *this,
902 struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Boris Brezillon08136212018-11-11 08:55:03 +0100904 return create_bbt(this, this->data_buf, bd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
907/**
David Woodhousee0c7d762006-05-13 18:07:53 +0100908 * check_create - [GENERIC] create and write bbt(s) if necessary
Boris Brezillon08136212018-11-11 08:55:03 +0100909 * @this: the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700910 * @buf: temporary buffer
911 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700913 * The function checks the results of the previous call to read_bbt and creates
914 * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found
915 * for the chip/device. Update is necessary if one of the tables is missing or
916 * the version nr. of one table is less than the other.
917 */
Boris Brezillon08136212018-11-11 08:55:03 +0100918static int check_create(struct nand_chip *this, uint8_t *buf,
919 struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Brian Norris623978d2011-09-20 18:35:34 -0700921 int i, chips, writeops, create, chipsel, res, res2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 struct nand_bbt_descr *td = this->bbt_td;
923 struct nand_bbt_descr *md = this->bbt_md;
924 struct nand_bbt_descr *rd, *rd2;
925
Brian Norris8b6e50c2011-05-25 14:59:01 -0700926 /* Do we have a bbt per chip? */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000927 if (td->options & NAND_BBT_PERCHIP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 chips = this->numchips;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000929 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 chips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 for (i = 0; i < chips; i++) {
933 writeops = 0;
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700934 create = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 rd = NULL;
936 rd2 = NULL;
Brian Norris623978d2011-09-20 18:35:34 -0700937 res = res2 = 0;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700938 /* Per chip or per device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700940 /* Mirrored table available? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (md) {
942 if (td->pages[i] == -1 && md->pages[i] == -1) {
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700943 create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 writeops = 0x03;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700945 } else if (td->pages[i] == -1) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000946 rd = md;
Brian Norris596d7452011-09-07 13:13:33 -0700947 writeops = 0x01;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700948 } else if (md->pages[i] == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 rd = td;
Brian Norris596d7452011-09-07 13:13:33 -0700950 writeops = 0x02;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700951 } else if (td->version[i] == md->version[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 rd = td;
953 if (!(td->options & NAND_BBT_VERSION))
954 rd2 = md;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700955 } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 rd = td;
Brian Norris596d7452011-09-07 13:13:33 -0700957 writeops = 0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 } else {
959 rd = md;
Brian Norris596d7452011-09-07 13:13:33 -0700960 writeops = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 } else {
963 if (td->pages[i] == -1) {
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700964 create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 writeops = 0x01;
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700966 } else {
967 rd = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000970
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700971 if (create) {
972 /* Create the bad block table by scanning the device? */
973 if (!(td->options & NAND_BBT_CREATE))
974 continue;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000975
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700976 /* Create the table in memory by scanning the chip(s) */
977 if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
Boris Brezillon08136212018-11-11 08:55:03 +0100978 create_bbt(this, buf, bd, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700980 td->version[i] = 1;
981 if (md)
982 md->version[i] = 1;
983 }
984
Brian Norris8b6e50c2011-05-25 14:59:01 -0700985 /* Read back first? */
Brian Norris623978d2011-09-20 18:35:34 -0700986 if (rd) {
Boris Brezillon08136212018-11-11 08:55:03 +0100987 res = read_abs_bbt(this, buf, rd, chipsel);
Brian Norris623978d2011-09-20 18:35:34 -0700988 if (mtd_is_eccerr(res)) {
989 /* Mark table as invalid */
990 rd->pages[i] = -1;
Brian Norrisdadc17a2011-09-20 18:35:57 -0700991 rd->version[i] = 0;
Brian Norris623978d2011-09-20 18:35:34 -0700992 i--;
993 continue;
994 }
995 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700996 /* If they weren't versioned, read both */
Brian Norris623978d2011-09-20 18:35:34 -0700997 if (rd2) {
Boris Brezillon08136212018-11-11 08:55:03 +0100998 res2 = read_abs_bbt(this, buf, rd2, chipsel);
Brian Norris623978d2011-09-20 18:35:34 -0700999 if (mtd_is_eccerr(res2)) {
1000 /* Mark table as invalid */
1001 rd2->pages[i] = -1;
Brian Norrisdadc17a2011-09-20 18:35:57 -07001002 rd2->version[i] = 0;
Brian Norris623978d2011-09-20 18:35:34 -07001003 i--;
1004 continue;
1005 }
1006 }
1007
1008 /* Scrub the flash table(s)? */
1009 if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
1010 writeops = 0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Brian Norrisdadc17a2011-09-20 18:35:57 -07001012 /* Update version numbers before writing */
1013 if (md) {
1014 td->version[i] = max(td->version[i], md->version[i]);
1015 md->version[i] = td->version[i];
1016 }
1017
Brian Norris8b6e50c2011-05-25 14:59:01 -07001018 /* Write the bad block table to the device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
Boris Brezillon08136212018-11-11 08:55:03 +01001020 res = write_bbt(this, buf, td, md, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (res < 0)
1022 return res;
1023 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001024
Brian Norris8b6e50c2011-05-25 14:59:01 -07001025 /* Write the mirror bad block table to the device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
Boris Brezillon08136212018-11-11 08:55:03 +01001027 res = write_bbt(this, buf, md, td, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (res < 0)
1029 return res;
1030 }
1031 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001032 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
1035/**
Boris Brezillon99f33512018-11-11 08:55:04 +01001036 * nand_update_bbt - update bad block table(s)
1037 * @this: the NAND device
1038 * @offs: the offset of the newly marked block
1039 *
1040 * The function updates the bad block table(s).
1041 */
1042static int nand_update_bbt(struct nand_chip *this, loff_t offs)
1043{
1044 struct mtd_info *mtd = nand_to_mtd(this);
1045 int len, res = 0;
1046 int chip, chipsel;
1047 uint8_t *buf;
1048 struct nand_bbt_descr *td = this->bbt_td;
1049 struct nand_bbt_descr *md = this->bbt_md;
1050
1051 if (!this->bbt || !td)
1052 return -EINVAL;
1053
1054 /* Allocate a temporary buffer for one eraseblock incl. oob */
1055 len = (1 << this->bbt_erase_shift);
1056 len += (len >> this->page_shift) * mtd->oobsize;
1057 buf = kmalloc(len, GFP_KERNEL);
1058 if (!buf)
1059 return -ENOMEM;
1060
1061 /* Do we have a bbt per chip? */
1062 if (td->options & NAND_BBT_PERCHIP) {
1063 chip = (int)(offs >> this->chip_shift);
1064 chipsel = chip;
1065 } else {
1066 chip = 0;
1067 chipsel = -1;
1068 }
1069
1070 td->version[chip]++;
1071 if (md)
1072 md->version[chip]++;
1073
1074 /* Write the bad block table to the device? */
1075 if (td->options & NAND_BBT_WRITE) {
1076 res = write_bbt(this, buf, td, md, chipsel);
1077 if (res < 0)
1078 goto out;
1079 }
1080 /* Write the mirror bad block table to the device? */
1081 if (md && (md->options & NAND_BBT_WRITE)) {
1082 res = write_bbt(this, buf, md, td, chipsel);
1083 }
1084
1085 out:
1086 kfree(buf);
1087 return res;
1088}
1089
1090/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001091 * mark_bbt_regions - [GENERIC] mark the bad block table regions
Boris Brezillon08136212018-11-11 08:55:03 +01001092 * @this: the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07001093 * @td: bad block table descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001095 * The bad block table regions are marked as "bad" to prevent accidental
1096 * erasures / writes. The regions are identified by the mark 0x02.
1097 */
Boris Brezillon08136212018-11-11 08:55:03 +01001098static void mark_bbt_region(struct nand_chip *this, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Boris Brezillon08136212018-11-11 08:55:03 +01001100 struct mtd_info *mtd = nand_to_mtd(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 int i, j, chips, block, nrblocks, update;
Brian Norris771c5682013-07-30 17:52:55 -07001102 uint8_t oldval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Brian Norris8b6e50c2011-05-25 14:59:01 -07001104 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (td->options & NAND_BBT_PERCHIP) {
1106 chips = this->numchips;
1107 nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
1108 } else {
1109 chips = 1;
1110 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001111 }
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 for (i = 0; i < chips; i++) {
1114 if ((td->options & NAND_BBT_ABSPAGE) ||
1115 !(td->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001116 if (td->pages[i] == -1)
1117 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
Brian Norrisb4d20d62013-07-30 17:52:56 -07001119 oldval = bbt_get_entry(this, block);
1120 bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
Brian Norris771c5682013-07-30 17:52:55 -07001121 if ((oldval != BBT_BLOCK_RESERVED) &&
1122 td->reserved_block_code)
Boris Brezillon08136212018-11-11 08:55:03 +01001123 nand_update_bbt(this, (loff_t)block <<
Brian Norrisb4d20d62013-07-30 17:52:56 -07001124 this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 continue;
1126 }
1127 update = 0;
1128 if (td->options & NAND_BBT_LASTBLOCK)
1129 block = ((i + 1) * nrblocks) - td->maxblocks;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001130 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 block = i * nrblocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 for (j = 0; j < td->maxblocks; j++) {
Brian Norrisb4d20d62013-07-30 17:52:56 -07001133 oldval = bbt_get_entry(this, block);
1134 bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
Brian Norris771c5682013-07-30 17:52:55 -07001135 if (oldval != BBT_BLOCK_RESERVED)
David Woodhousee0c7d762006-05-13 18:07:53 +01001136 update = 1;
Brian Norrisb4d20d62013-07-30 17:52:56 -07001137 block++;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001138 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07001139 /*
1140 * If we want reserved blocks to be recorded to flash, and some
1141 * new ones have been marked, then we need to update the stored
1142 * bbts. This should only happen once.
1143 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (update && td->reserved_block_code)
Boris Brezillon08136212018-11-11 08:55:03 +01001145 nand_update_bbt(this, (loff_t)(block - 1) <<
Brian Norrisb4d20d62013-07-30 17:52:56 -07001146 this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148}
1149
1150/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001151 * verify_bbt_descr - verify the bad block description
Boris Brezillon08136212018-11-11 08:55:03 +01001152 * @this: the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07001153 * @bd: the table to verify
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001154 *
1155 * This functions performs a few sanity checks on the bad block description
1156 * table.
1157 */
Boris Brezillon08136212018-11-11 08:55:03 +01001158static void verify_bbt_descr(struct nand_chip *this, struct nand_bbt_descr *bd)
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001159{
Boris Brezillon08136212018-11-11 08:55:03 +01001160 struct mtd_info *mtd = nand_to_mtd(this);
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001161 u32 pattern_len;
1162 u32 bits;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001163 u32 table_size;
1164
1165 if (!bd)
1166 return;
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001167
1168 pattern_len = bd->len;
1169 bits = bd->options & NAND_BBT_NRBITS_MSK;
1170
Brian Norrisa40f7342011-05-31 16:31:22 -07001171 BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
Brian Norrisbb9ebd4e2011-05-31 16:31:23 -07001172 !(this->bbt_options & NAND_BBT_USE_FLASH));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001173 BUG_ON(!bits);
1174
1175 if (bd->options & NAND_BBT_VERSION)
1176 pattern_len++;
1177
1178 if (bd->options & NAND_BBT_NO_OOB) {
Brian Norrisbb9ebd4e2011-05-31 16:31:23 -07001179 BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
Brian Norrisa40f7342011-05-31 16:31:22 -07001180 BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001181 BUG_ON(bd->offs);
1182 if (bd->options & NAND_BBT_VERSION)
1183 BUG_ON(bd->veroffs != bd->len);
1184 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1185 }
1186
1187 if (bd->options & NAND_BBT_PERCHIP)
1188 table_size = this->chipsize >> this->bbt_erase_shift;
1189 else
1190 table_size = mtd->size >> this->bbt_erase_shift;
1191 table_size >>= 3;
1192 table_size *= bits;
1193 if (bd->options & NAND_BBT_NO_OOB)
1194 table_size += pattern_len;
1195 BUG_ON(table_size > (1 << this->bbt_erase_shift));
1196}
1197
1198/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
Boris Brezillon08136212018-11-11 08:55:03 +01001200 * @this: the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07001201 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001203 * The function checks, if a bad block table(s) is/are already available. If
1204 * not it scans the device for manufacturer marked good / bad blocks and writes
1205 * the bad block table(s) to the selected place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001207 * The bad block table memory is allocated here. It must be freed by calling
1208 * the nand_free_bbt function.
1209 */
Boris Brezillon08136212018-11-11 08:55:03 +01001210static int nand_scan_bbt(struct nand_chip *this, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Boris Brezillon08136212018-11-11 08:55:03 +01001212 struct mtd_info *mtd = nand_to_mtd(this);
Brian Norris83c59542015-02-28 02:13:12 -08001213 int len, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 uint8_t *buf;
1215 struct nand_bbt_descr *td = this->bbt_td;
1216 struct nand_bbt_descr *md = this->bbt_md;
1217
Sheng Yong192db1c2015-07-31 01:12:44 +00001218 len = (mtd->size >> (this->bbt_erase_shift + 2)) ? : 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07001219 /*
1220 * Allocate memory (2bit per block) and clear the memory bad block
1221 * table.
1222 */
Burman Yan95b93a02006-11-15 21:10:29 +02001223 this->bbt = kzalloc(len, GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001224 if (!this->bbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Brian Norris8b6e50c2011-05-25 14:59:01 -07001227 /*
1228 * If no primary table decriptor is given, scan the device to build a
1229 * memory based bad block table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 */
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001231 if (!td) {
Boris Brezillon08136212018-11-11 08:55:03 +01001232 if ((res = nand_memory_bbt(this, bd))) {
Brian Norrisd0370212011-07-19 10:06:08 -07001233 pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
Brian Norris83c59542015-02-28 02:13:12 -08001234 goto err;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001235 }
Brian Norris83c59542015-02-28 02:13:12 -08001236 return 0;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001237 }
Boris Brezillon08136212018-11-11 08:55:03 +01001238 verify_bbt_descr(this, td);
1239 verify_bbt_descr(this, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 /* Allocate a temporary buffer for one eraseblock incl. oob */
1242 len = (1 << this->bbt_erase_shift);
1243 len += (len >> this->page_shift) * mtd->oobsize;
David Woodhousec3f8abf2006-05-13 04:03:42 +01001244 buf = vmalloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (!buf) {
Brian Norris83c59542015-02-28 02:13:12 -08001246 res = -ENOMEM;
1247 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001249
Brian Norris8b6e50c2011-05-25 14:59:01 -07001250 /* Is the bbt at a given page? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (td->options & NAND_BBT_ABSPAGE) {
Boris Brezillon08136212018-11-11 08:55:03 +01001252 read_abs_bbts(this, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001253 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 /* Search the bad block table using a pattern in oob */
Boris Brezillon08136212018-11-11 08:55:03 +01001255 search_read_bbts(this, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Boris Brezillon08136212018-11-11 08:55:03 +01001258 res = check_create(this, buf, bd);
Brian Norris83c59542015-02-28 02:13:12 -08001259 if (res)
1260 goto err;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 /* Prevent the bbt regions from erasing / writing */
Boris Brezillon08136212018-11-11 08:55:03 +01001263 mark_bbt_region(this, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if (md)
Boris Brezillon08136212018-11-11 08:55:03 +01001265 mark_bbt_region(this, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001266
David Woodhousee0c7d762006-05-13 18:07:53 +01001267 vfree(buf);
Brian Norris83c59542015-02-28 02:13:12 -08001268 return 0;
1269
1270err:
1271 kfree(this->bbt);
1272 this->bbt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return res;
1274}
1275
Brian Norris8b6e50c2011-05-25 14:59:01 -07001276/*
1277 * Define some generic bad / good block scan pattern which are used
1278 * while scanning a device for factory marked good / bad blocks.
1279 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1281
Brian Norris7854d3f2011-06-23 14:12:08 -07001282/* Generic flash bbt descriptors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1284static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1285
1286static struct nand_bbt_descr bbt_main_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001287 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1289 .offs = 8,
1290 .len = 4,
1291 .veroffs = 12,
Richard Genoud61de9da2012-09-11 15:50:54 +02001292 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 .pattern = bbt_pattern
1294};
1295
1296static struct nand_bbt_descr bbt_mirror_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001297 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1299 .offs = 8,
1300 .len = 4,
1301 .veroffs = 12,
Richard Genoud61de9da2012-09-11 15:50:54 +02001302 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 .pattern = mirror_pattern
1304};
1305
Brian Norris9fd6b372012-06-22 16:35:40 -07001306static struct nand_bbt_descr bbt_main_no_oob_descr = {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001307 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1308 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1309 | NAND_BBT_NO_OOB,
1310 .len = 4,
1311 .veroffs = 4,
Richard Genoud61de9da2012-09-11 15:50:54 +02001312 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001313 .pattern = bbt_pattern
1314};
1315
Brian Norris9fd6b372012-06-22 16:35:40 -07001316static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001317 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1318 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1319 | NAND_BBT_NO_OOB,
1320 .len = 4,
1321 .veroffs = 4,
Richard Genoud61de9da2012-09-11 15:50:54 +02001322 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001323 .pattern = mirror_pattern
1324};
1325
Brian Norris752ed6c2011-09-20 18:36:42 -07001326#define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
Brian Norris58373ff2010-07-15 12:15:44 -07001327/**
Brian Norris752ed6c2011-09-20 18:36:42 -07001328 * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001329 * @this: NAND chip to create descriptor for
Brian Norris58373ff2010-07-15 12:15:44 -07001330 *
1331 * This function allocates and initializes a nand_bbt_descr for BBM detection
Brian Norris752ed6c2011-09-20 18:36:42 -07001332 * based on the properties of @this. The new descriptor is stored in
Brian Norris58373ff2010-07-15 12:15:44 -07001333 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1334 * passed to this function.
Brian Norris58373ff2010-07-15 12:15:44 -07001335 */
Brian Norris752ed6c2011-09-20 18:36:42 -07001336static int nand_create_badblock_pattern(struct nand_chip *this)
Brian Norris58373ff2010-07-15 12:15:44 -07001337{
1338 struct nand_bbt_descr *bd;
1339 if (this->badblock_pattern) {
Brian Norris752ed6c2011-09-20 18:36:42 -07001340 pr_warn("Bad block pattern already allocated; not replacing\n");
Brian Norris58373ff2010-07-15 12:15:44 -07001341 return -EINVAL;
1342 }
1343 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001344 if (!bd)
Brian Norris58373ff2010-07-15 12:15:44 -07001345 return -ENOMEM;
Brian Norris752ed6c2011-09-20 18:36:42 -07001346 bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
Brian Norris58373ff2010-07-15 12:15:44 -07001347 bd->offs = this->badblockpos;
1348 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1349 bd->pattern = scan_ff_pattern;
1350 bd->options |= NAND_BBT_DYNAMICSTRUCT;
1351 this->badblock_pattern = bd;
1352 return 0;
1353}
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355/**
Boris Brezillon44b07b92018-07-05 12:27:30 +02001356 * nand_create_bbt - [NAND Interface] Select a default bad block table for the device
1357 * @this: NAND chip object
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001359 * This function selects the default bad block table support for the device and
1360 * calls the nand_scan_bbt function.
1361 */
Boris Brezillon44b07b92018-07-05 12:27:30 +02001362int nand_create_bbt(struct nand_chip *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Brian Norrisabb9cf72014-05-20 22:34:40 -07001364 int ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001365
Brian Norris8b6e50c2011-05-25 14:59:01 -07001366 /* Is a flash based bad block table requested? */
Brian Norrisbb9ebd4e2011-05-31 16:31:23 -07001367 if (this->bbt_options & NAND_BBT_USE_FLASH) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001368 /* Use the default pattern descriptors */
1369 if (!this->bbt_td) {
Brian Norrisa40f7342011-05-31 16:31:22 -07001370 if (this->bbt_options & NAND_BBT_NO_OOB) {
Brian Norris9fd6b372012-06-22 16:35:40 -07001371 this->bbt_td = &bbt_main_no_oob_descr;
1372 this->bbt_md = &bbt_mirror_no_oob_descr;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001373 } else {
1374 this->bbt_td = &bbt_main_descr;
1375 this->bbt_md = &bbt_mirror_descr;
1376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 } else {
1379 this->bbt_td = NULL;
1380 this->bbt_md = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
Brian Norrisa2f812d2011-03-18 21:53:42 -07001382
Brian Norrisabb9cf72014-05-20 22:34:40 -07001383 if (!this->badblock_pattern) {
1384 ret = nand_create_badblock_pattern(this);
1385 if (ret)
1386 return ret;
1387 }
Brian Norrisa2f812d2011-03-18 21:53:42 -07001388
Boris Brezillon08136212018-11-11 08:55:03 +01001389 return nand_scan_bbt(this, this->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
Boris Brezillon44b07b92018-07-05 12:27:30 +02001391EXPORT_SYMBOL(nand_create_bbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393/**
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001394 * nand_isreserved_bbt - [NAND Interface] Check if a block is reserved
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001395 * @this: NAND chip object
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001396 * @offs: offset in the device
1397 */
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001398int nand_isreserved_bbt(struct nand_chip *this, loff_t offs)
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001399{
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001400 int block;
1401
1402 block = (int)(offs >> this->bbt_erase_shift);
1403 return bbt_get_entry(this, block) == BBT_BLOCK_RESERVED;
1404}
1405
1406/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001407 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001408 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07001409 * @offs: offset in the device
1410 * @allowbbt: allow access to bad block table region
1411 */
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001412int nand_isbad_bbt(struct nand_chip *this, loff_t offs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Brian Norris39dbb022013-07-30 17:52:57 -07001414 int block, res;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001415
Brian Norrisb4d20d62013-07-30 17:52:56 -07001416 block = (int)(offs >> this->bbt_erase_shift);
1417 res = bbt_get_entry(this, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02001419 pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1420 (unsigned int)offs, block, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Brian Norris39dbb022013-07-30 17:52:57 -07001422 switch (res) {
Brian Norris771c5682013-07-30 17:52:55 -07001423 case BBT_BLOCK_GOOD:
David Woodhousee0c7d762006-05-13 18:07:53 +01001424 return 0;
Brian Norris771c5682013-07-30 17:52:55 -07001425 case BBT_BLOCK_WORN:
David Woodhousee0c7d762006-05-13 18:07:53 +01001426 return 1;
Brian Norris771c5682013-07-30 17:52:55 -07001427 case BBT_BLOCK_RESERVED:
David Woodhousee0c7d762006-05-13 18:07:53 +01001428 return allowbbt ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430 return 1;
1431}
1432
Brian Norrisb32843b2013-07-30 17:52:59 -07001433/**
1434 * nand_markbad_bbt - [NAND Interface] Mark a block bad in the BBT
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001435 * @this: NAND chip object
Brian Norrisb32843b2013-07-30 17:52:59 -07001436 * @offs: offset of the bad block
1437 */
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001438int nand_markbad_bbt(struct nand_chip *this, loff_t offs)
Brian Norrisb32843b2013-07-30 17:52:59 -07001439{
Brian Norrisb32843b2013-07-30 17:52:59 -07001440 int block, ret = 0;
1441
1442 block = (int)(offs >> this->bbt_erase_shift);
1443
1444 /* Mark bad block in memory */
1445 bbt_mark_entry(this, block, BBT_BLOCK_WORN);
1446
1447 /* Update flash-based bad block table */
1448 if (this->bbt_options & NAND_BBT_USE_FLASH)
Boris Brezillon08136212018-11-11 08:55:03 +01001449 ret = nand_update_bbt(this, offs);
Brian Norrisb32843b2013-07-30 17:52:59 -07001450
1451 return ret;
1452}