blob: 2ef15ef94525f14eb8ce1a6e72bf6da09a5d031d [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Overview:
4 * Bad block table support for the NAND driver
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00005 *
Fabio Estevamd159c4e2012-07-28 19:29:25 -03006 * Copyright © 2004 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Description:
9 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000010 * When nand_scan_bbt is called, then it tries to find the bad block table
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020011 * depending on the options in the BBT descriptor(s). If no flash based BBT
Brian Norrisbb9ebd42011-05-31 16:31:23 -070012 * (NAND_BBT_USE_FLASH) is specified then the device is scanned for factory
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020013 * marked good / bad blocks. This information is used to create a memory BBT.
14 * Once a new bad block is discovered then the "factory" information is updated
15 * on the device.
16 * If a flash based BBT is specified then the function first tries to find the
17 * BBT on flash. If a BBT is found then the contents are read and the memory
18 * based BBT is created. If a mirrored BBT is selected then the mirror is
19 * searched too and the versions are compared. If the mirror has a greater
Huang Shijie44ed0ff2012-07-03 16:44:15 +080020 * version number, then the mirror BBT is used to build the memory based BBT.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * If the tables are not versioned, then we "or" the bad block information.
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020022 * If one of the BBTs is out of date or does not exist it is (re)created.
23 * If no BBT exists at all then the device is scanned for factory marked
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000024 * good / bad blocks and the bad block tables are created.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 *
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020026 * For manufacturer created BBTs like the one found on M-SYS DOC devices
27 * the BBT is searched and read but never created
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 *
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020029 * The auto generated bad block table is located in the last good blocks
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000030 * of the device. The table is mirrored, so it can be updated eventually.
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020031 * The table is marked in the OOB area with an ident pattern and a version
32 * number which indicates which of both tables is more up to date. If the NAND
33 * controller needs the complete OOB area for the ECC information then the
Brian Norrisbb9ebd42011-05-31 16:31:23 -070034 * option NAND_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of
Brian Norrisa40f7342011-05-31 16:31:22 -070035 * course): it moves the ident pattern and the version byte into the data area
36 * and the OOB area will remain untouched.
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 *
38 * The table uses 2 bits per block
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020039 * 11b: block is good
40 * 00b: block is factory marked bad
41 * 01b, 10b: block is marked bad due to wear
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 *
43 * The memory bad block table uses the following scheme:
44 * 00b: block is good
45 * 01b: block is marked bad due to wear
46 * 10b: block is reserved (to protect the bbt area)
47 * 11b: block is factory marked bad
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000048 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * Multichip devices like DOC store the bad block info per floor.
50 *
51 * Following assumptions are made:
52 * - bbts start at a page boundary, if autolocated on a block boundary
David Woodhousee0c7d762006-05-13 18:07:53 +010053 * - the space necessary for a bbt in FLASH does not exceed a block boundary
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 */
55
56#include <linux/slab.h>
57#include <linux/types.h>
58#include <linux/mtd/mtd.h>
Richard Genoud61de9da2012-09-11 15:50:54 +020059#include <linux/mtd/bbm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/bitops.h>
61#include <linux/delay.h>
David Woodhousec3f8abf2006-05-13 04:03:42 +010062#include <linux/vmalloc.h>
Paul Gortmakerf3bcc012011-07-10 12:43:28 -040063#include <linux/export.h>
Brian Norris491ed062012-06-22 16:35:44 -070064#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Boris Brezillon348d56a2018-09-07 00:38:48 +020066#include "internals.h"
67
Brian Norris771c5682013-07-30 17:52:55 -070068#define BBT_BLOCK_GOOD 0x00
69#define BBT_BLOCK_WORN 0x01
70#define BBT_BLOCK_RESERVED 0x02
71#define BBT_BLOCK_FACTORY_BAD 0x03
72
73#define BBT_ENTRY_MASK 0x03
74#define BBT_ENTRY_SHIFT 2
75
76static inline uint8_t bbt_get_entry(struct nand_chip *chip, int block)
77{
78 uint8_t entry = chip->bbt[block >> BBT_ENTRY_SHIFT];
79 entry >>= (block & BBT_ENTRY_MASK) * 2;
80 return entry & BBT_ENTRY_MASK;
81}
82
83static inline void bbt_mark_entry(struct nand_chip *chip, int block,
84 uint8_t mark)
85{
86 uint8_t msk = (mark & BBT_ENTRY_MASK) << ((block & BBT_ENTRY_MASK) * 2);
87 chip->bbt[block >> BBT_ENTRY_SHIFT] |= msk;
88}
89
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020090static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
91{
Brian Norris718894a2012-06-22 16:35:43 -070092 if (memcmp(buf, td->pattern, td->len))
93 return -1;
94 return 0;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020095}
96
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000097/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * check_pattern - [GENERIC] check if a pattern is in the buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -070099 * @buf: the buffer to search
100 * @len: the length of buffer to search
101 * @paglen: the pagelength
102 * @td: search pattern descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700104 * Check for a pattern at the given place. Used to search bad block tables and
Brian Norrisdad22562013-07-30 17:53:00 -0700105 * good / bad block identifiers.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700106 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100107static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200109 if (td->options & NAND_BBT_NO_OOB)
110 return check_pattern_no_oob(buf, td);
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 /* Compare the pattern */
Brian Norrisdad22562013-07-30 17:53:00 -0700113 if (memcmp(buf + paglen + td->offs, td->pattern, td->len))
Brian Norris75b66d82011-09-07 13:13:41 -0700114 return -1;
Brian Norris58373ff2010-07-15 12:15:44 -0700115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return 0;
117}
118
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000119/**
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100120 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700121 * @buf: the buffer to search
122 * @td: search pattern descriptor
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100123 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700124 * Check for a pattern at the given place. Used to search bad block tables and
125 * good / bad block identifiers. Same as check_pattern, but no optional empty
126 * check.
127 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100128static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100129{
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100130 /* Compare the pattern */
Brian Norris491ed062012-06-22 16:35:44 -0700131 if (memcmp(buf + td->offs, td->pattern, td->len))
132 return -1;
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100133 return 0;
134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200137 * add_marker_len - compute the length of the marker in data area
Brian Norris8b6e50c2011-05-25 14:59:01 -0700138 * @td: BBT descriptor used for computation
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200139 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700140 * The length will be 0 if the marker is located in OOB area.
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200141 */
142static u32 add_marker_len(struct nand_bbt_descr *td)
143{
144 u32 len;
145
146 if (!(td->options & NAND_BBT_NO_OOB))
147 return 0;
148
149 len = td->len;
150 if (td->options & NAND_BBT_VERSION)
151 len++;
152 return len;
153}
154
155/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * read_bbt - [GENERIC] Read the bad block table starting from page
Randy Dunlap455e7b32019-01-27 18:21:42 -0800157 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700158 * @buf: temporary buffer
159 * @page: the starting page
160 * @num: the number of bbt descriptors to read
Brian Norris7854d3f2011-06-23 14:12:08 -0700161 * @td: the bbt describtion table
Brian Norrisb4d20d62013-07-30 17:52:56 -0700162 * @offs: block number offset in the table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 *
164 * Read the bad block table starting from page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 */
Boris Brezillon08136212018-11-11 08:55:03 +0100166static int read_bbt(struct nand_chip *this, uint8_t *buf, int page, int num,
167 struct nand_bbt_descr *td, int offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Boris Brezillon08136212018-11-11 08:55:03 +0100169 struct mtd_info *mtd = nand_to_mtd(this);
Brian Norris167a8d52011-09-20 18:35:08 -0700170 int res, ret = 0, i, j, act = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 size_t retlen, len, totlen;
172 loff_t from;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200173 int bits = td->options & NAND_BBT_NRBITS_MSK;
Brian Norris596d7452011-09-07 13:13:33 -0700174 uint8_t msk = (uint8_t)((1 << bits) - 1);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200175 u32 marker_len;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200176 int reserved_block_code = td->reserved_block_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 totlen = (num * bits) >> 3;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200179 marker_len = add_marker_len(td);
Brian Norris596d7452011-09-07 13:13:33 -0700180 from = ((loff_t)page) << this->page_shift;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 while (totlen) {
Brian Norris596d7452011-09-07 13:13:33 -0700183 len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200184 if (marker_len) {
185 /*
186 * In case the BBT marker is not in the OOB area it
187 * will be just in the first page.
188 */
189 len -= marker_len;
190 from += marker_len;
191 marker_len = 0;
192 }
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200193 res = mtd_read(mtd, from, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (res < 0) {
Brian Norris167a8d52011-09-20 18:35:08 -0700195 if (mtd_is_eccerr(res)) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200196 pr_info("nand_bbt: ECC error in BBT at 0x%012llx\n",
197 from & ~mtd->writesize);
Brian Norris167a8d52011-09-20 18:35:08 -0700198 return res;
199 } else if (mtd_is_bitflip(res)) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200200 pr_info("nand_bbt: corrected error in BBT at 0x%012llx\n",
201 from & ~mtd->writesize);
Brian Norris167a8d52011-09-20 18:35:08 -0700202 ret = res;
203 } else {
204 pr_info("nand_bbt: error reading BBT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return res;
206 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /* Analyse data */
210 for (i = 0; i < len; i++) {
211 uint8_t dat = buf[i];
Brian Norrisb4d20d62013-07-30 17:52:56 -0700212 for (j = 0; j < 8; j += bits, act++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 uint8_t tmp = (dat >> j) & msk;
214 if (tmp == msk)
215 continue;
David Woodhousee0c7d762006-05-13 18:07:53 +0100216 if (reserved_block_code && (tmp == reserved_block_code)) {
Brian Norrisd0370212011-07-19 10:06:08 -0700217 pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
Brian Norrisb4d20d62013-07-30 17:52:56 -0700218 (loff_t)(offs + act) <<
219 this->bbt_erase_shift);
220 bbt_mark_entry(this, offs + act,
Brian Norris771c5682013-07-30 17:52:55 -0700221 BBT_BLOCK_RESERVED);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200222 mtd->ecc_stats.bbtblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 continue;
224 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700225 /*
226 * Leave it for now, if it's matured we can
Brian Norrisa0f50802011-07-19 10:06:06 -0700227 * move this message to pr_debug.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700228 */
Brian Norrisd0370212011-07-19 10:06:08 -0700229 pr_info("nand_read_bbt: bad block at 0x%012llx\n",
Brian Norrisb4d20d62013-07-30 17:52:56 -0700230 (loff_t)(offs + act) <<
231 this->bbt_erase_shift);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700232 /* Factory marked bad or worn out? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (tmp == 0)
Brian Norrisb4d20d62013-07-30 17:52:56 -0700234 bbt_mark_entry(this, offs + act,
Brian Norris771c5682013-07-30 17:52:55 -0700235 BBT_BLOCK_FACTORY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 else
Brian Norrisb4d20d62013-07-30 17:52:56 -0700237 bbt_mark_entry(this, offs + act,
Brian Norris771c5682013-07-30 17:52:55 -0700238 BBT_BLOCK_WORN);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200239 mtd->ecc_stats.badblocks++;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242 totlen -= len;
243 from += len;
244 }
Brian Norris167a8d52011-09-20 18:35:08 -0700245 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
248/**
249 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
Boris Brezillon08136212018-11-11 08:55:03 +0100250 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700251 * @buf: temporary buffer
252 * @td: descriptor for the bad block table
Brian Norris596d7452011-09-07 13:13:33 -0700253 * @chip: read the table for a specific chip, -1 read all chips; applies only if
Brian Norris8b6e50c2011-05-25 14:59:01 -0700254 * NAND_BBT_PERCHIP option is set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700256 * Read the bad block table for all chips starting at a given page. We assume
257 * that the bbt bits are in consecutive order.
258 */
Boris Brezillon08136212018-11-11 08:55:03 +0100259static int read_abs_bbt(struct nand_chip *this, uint8_t *buf,
260 struct nand_bbt_descr *td, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Boris Brezillon08136212018-11-11 08:55:03 +0100262 struct mtd_info *mtd = nand_to_mtd(this);
Boris Brezillon6c836d52018-10-29 11:22:16 +0100263 u64 targetsize = nanddev_target_size(&this->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 int res = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (td->options & NAND_BBT_PERCHIP) {
267 int offs = 0;
Boris Brezillon32813e22018-10-29 11:58:29 +0100268 for (i = 0; i < nanddev_ntargets(&this->base); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (chip == -1 || chip == i)
Boris Brezillon08136212018-11-11 08:55:03 +0100270 res = read_bbt(this, buf, td->pages[i],
Boris Brezillon6c836d52018-10-29 11:22:16 +0100271 targetsize >> this->bbt_erase_shift,
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200272 td, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (res)
274 return res;
Boris Brezillon6c836d52018-10-29 11:22:16 +0100275 offs += targetsize >> this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277 } else {
Boris Brezillon08136212018-11-11 08:55:03 +0100278 res = read_bbt(this, buf, td->pages[0],
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200279 mtd->size >> this->bbt_erase_shift, td, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (res)
281 return res;
282 }
283 return 0;
284}
285
Brian Norris8b6e50c2011-05-25 14:59:01 -0700286/* BBT marker is in the first page, no OOB */
Boris Brezillon08136212018-11-11 08:55:03 +0100287static int scan_read_data(struct nand_chip *this, uint8_t *buf, loff_t offs,
288 struct nand_bbt_descr *td)
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200289{
Boris Brezillon08136212018-11-11 08:55:03 +0100290 struct mtd_info *mtd = nand_to_mtd(this);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200291 size_t retlen;
292 size_t len;
293
294 len = td->len;
295 if (td->options & NAND_BBT_VERSION)
296 len++;
297
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200298 return mtd_read(mtd, offs, len, &retlen, buf);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200299}
300
Brian Norrisa7e68832012-06-22 16:35:45 -0700301/**
Brian Norrisaf69dcd2012-06-22 16:35:42 -0700302 * scan_read_oob - [GENERIC] Scan data+OOB region to buffer
Boris Brezillon08136212018-11-11 08:55:03 +0100303 * @this: NAND chip object
Brian Norrisa7e68832012-06-22 16:35:45 -0700304 * @buf: temporary buffer
305 * @offs: offset at which to scan
306 * @len: length of data region to read
307 *
308 * Scan read data from data+OOB. May traverse multiple pages, interleaving
309 * page,OOB,page,OOB,... in buf. Completes transfer and returns the "strongest"
310 * ECC condition (error or bitflip). May quit on the first (non-ECC) error.
311 */
Boris Brezillon08136212018-11-11 08:55:03 +0100312static int scan_read_oob(struct nand_chip *this, uint8_t *buf, loff_t offs,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200313 size_t len)
314{
Boris Brezillon08136212018-11-11 08:55:03 +0100315 struct mtd_info *mtd = nand_to_mtd(this);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200316 struct mtd_oob_ops ops;
Brian Norrisa7e68832012-06-22 16:35:45 -0700317 int res, ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200318
Brian Norrisa7e68832012-06-22 16:35:45 -0700319 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200320 ops.ooboffs = 0;
321 ops.ooblen = mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200322
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200323 while (len > 0) {
Brian Norris105513c2011-09-07 13:13:28 -0700324 ops.datbuf = buf;
325 ops.len = min(len, (size_t)mtd->writesize);
326 ops.oobbuf = buf + ops.len;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200327
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200328 res = mtd_read_oob(mtd, offs, &ops);
Brian Norrisa7e68832012-06-22 16:35:45 -0700329 if (res) {
330 if (!mtd_is_bitflip_or_eccerr(res))
331 return res;
332 else if (mtd_is_eccerr(res) || !ret)
333 ret = res;
334 }
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200335
336 buf += mtd->oobsize + mtd->writesize;
337 len -= mtd->writesize;
Dmitry Maluka34a57042012-05-11 20:51:51 +0300338 offs += mtd->writesize;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200339 }
Brian Norrisa7e68832012-06-22 16:35:45 -0700340 return ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200341}
342
Boris Brezillon08136212018-11-11 08:55:03 +0100343static int scan_read(struct nand_chip *this, uint8_t *buf, loff_t offs,
344 size_t len, struct nand_bbt_descr *td)
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200345{
346 if (td->options & NAND_BBT_NO_OOB)
Boris Brezillon08136212018-11-11 08:55:03 +0100347 return scan_read_data(this, buf, offs, td);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200348 else
Boris Brezillon08136212018-11-11 08:55:03 +0100349 return scan_read_oob(this, buf, offs, len);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200350}
351
Brian Norris8b6e50c2011-05-25 14:59:01 -0700352/* Scan write data with oob to flash */
Boris Brezillon08136212018-11-11 08:55:03 +0100353static int scan_write_bbt(struct nand_chip *this, loff_t offs, size_t len,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200354 uint8_t *buf, uint8_t *oob)
355{
Boris Brezillon08136212018-11-11 08:55:03 +0100356 struct mtd_info *mtd = nand_to_mtd(this);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200357 struct mtd_oob_ops ops;
358
Brian Norris0612b9d2011-08-30 18:45:40 -0700359 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200360 ops.ooboffs = 0;
361 ops.ooblen = mtd->oobsize;
362 ops.datbuf = buf;
363 ops.oobbuf = oob;
364 ops.len = len;
365
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200366 return mtd_write_oob(mtd, offs, &ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200367}
368
Boris Brezillon08136212018-11-11 08:55:03 +0100369static u32 bbt_get_ver_offs(struct nand_chip *this, struct nand_bbt_descr *td)
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200370{
Boris Brezillon08136212018-11-11 08:55:03 +0100371 struct mtd_info *mtd = nand_to_mtd(this);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200372 u32 ver_offs = td->veroffs;
373
374 if (!(td->options & NAND_BBT_NO_OOB))
375 ver_offs += mtd->writesize;
376 return ver_offs;
377}
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379/**
380 * 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 +0100381 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700382 * @buf: temporary buffer
383 * @td: descriptor for the bad block table
384 * @md: descriptor for the bad block table mirror
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700386 * Read the bad block table(s) for all chips starting at a given page. We
387 * assume that the bbt bits are in consecutive order.
388 */
Boris Brezillon08136212018-11-11 08:55:03 +0100389static void read_abs_bbts(struct nand_chip *this, uint8_t *buf,
Brian Norris7b5a2d42012-06-22 16:35:41 -0700390 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Boris Brezillon08136212018-11-11 08:55:03 +0100392 struct mtd_info *mtd = nand_to_mtd(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000394 /* Read the primary version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (td->options & NAND_BBT_VERSION) {
Boris Brezillon08136212018-11-11 08:55:03 +0100396 scan_read(this, buf, (loff_t)td->pages[0] << this->page_shift,
397 mtd->writesize, td);
398 td->version[0] = buf[bbt_get_ver_offs(this, td)];
Brian Norris9a4d4d62011-07-19 10:06:07 -0700399 pr_info("Bad block table at page %d, version 0x%02X\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700400 td->pages[0], td->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000403 /* Read the mirror version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (md && (md->options & NAND_BBT_VERSION)) {
Boris Brezillon08136212018-11-11 08:55:03 +0100405 scan_read(this, buf, (loff_t)md->pages[0] << this->page_shift,
406 mtd->writesize, md);
407 md->version[0] = buf[bbt_get_ver_offs(this, md)];
Brian Norris9a4d4d62011-07-19 10:06:07 -0700408 pr_info("Bad block table at page %d, version 0x%02X\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700409 md->pages[0], md->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Brian Norris8b6e50c2011-05-25 14:59:01 -0700413/* Scan a given block partially */
Boris Brezillon08136212018-11-11 08:55:03 +0100414static int scan_block_fast(struct nand_chip *this, struct nand_bbt_descr *bd,
Frieder Schrempff90da782019-04-17 12:36:37 +0000415 loff_t offs, uint8_t *buf)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200416{
Boris Brezillon08136212018-11-11 08:55:03 +0100417 struct mtd_info *mtd = nand_to_mtd(this);
Frieder Schrempff90da782019-04-17 12:36:37 +0000418
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200419 struct mtd_oob_ops ops;
Frieder Schrempff90da782019-04-17 12:36:37 +0000420 int ret, page_offset;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200421
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200422 ops.ooblen = mtd->oobsize;
423 ops.oobbuf = buf;
424 ops.ooboffs = 0;
425 ops.datbuf = NULL;
Brian Norris0612b9d2011-08-30 18:45:40 -0700426 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200427
Frieder Schrempff90da782019-04-17 12:36:37 +0000428 page_offset = nand_bbm_get_next_page(this, 0);
429
430 while (page_offset >= 0) {
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 */
Frieder Schrempff90da782019-04-17 12:36:37 +0000435 ret = mtd_read_oob(mtd, offs + (page_offset * mtd->writesize),
436 &ops);
Brian Norris903cd062011-06-28 16:28:58 -0700437 /* Ignore ECC errors when checking for BBM */
Brian Norrisd57f40542011-09-20 18:34:25 -0700438 if (ret && !mtd_is_bitflip_or_eccerr(ret))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200439 return ret;
440
441 if (check_short_pattern(buf, bd))
442 return 1;
443
Frieder Schrempff90da782019-04-17 12:36:37 +0000444 page_offset = nand_bbm_get_next_page(this, page_offset + 1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200445 }
Frieder Schrempff90da782019-04-17 12:36:37 +0000446
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200447 return 0;
448}
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450/**
451 * create_bbt - [GENERIC] Create a bad block table by scanning the device
Boris Brezillon08136212018-11-11 08:55:03 +0100452 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700453 * @buf: temporary buffer
454 * @bd: descriptor for the good/bad block search pattern
455 * @chip: create the table for a specific chip, -1 read all chips; applies only
456 * if NAND_BBT_PERCHIP option is set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700458 * Create a bad block table by scanning the device for the given good/bad block
459 * identify pattern.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 */
Boris Brezillon08136212018-11-11 08:55:03 +0100461static int create_bbt(struct nand_chip *this, uint8_t *buf,
462 struct nand_bbt_descr *bd, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Boris Brezillon6c836d52018-10-29 11:22:16 +0100464 u64 targetsize = nanddev_target_size(&this->base);
Boris Brezillon08136212018-11-11 08:55:03 +0100465 struct mtd_info *mtd = nand_to_mtd(this);
Frieder Schrempff90da782019-04-17 12:36:37 +0000466 int i, numblocks, startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 loff_t from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Brian Norris9a4d4d62011-07-19 10:06:07 -0700469 pr_info("Scanning device for bad blocks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (chip == -1) {
Brian Norrisb4d20d62013-07-30 17:52:56 -0700472 numblocks = mtd->size >> this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 startblock = 0;
474 from = 0;
475 } else {
Boris Brezillon32813e22018-10-29 11:58:29 +0100476 if (chip >= nanddev_ntargets(&this->base)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -0700477 pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
Boris Brezillon32813e22018-10-29 11:58:29 +0100478 chip + 1, nanddev_ntargets(&this->base));
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000479 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
Boris Brezillon6c836d52018-10-29 11:22:16 +0100481 numblocks = targetsize >> this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 startblock = chip * numblocks;
483 numblocks += startblock;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700484 from = (loff_t)startblock << this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000486
Brian Norrisb4d20d62013-07-30 17:52:56 -0700487 for (i = startblock; i < numblocks; i++) {
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000488 int ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000489
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200490 BUG_ON(bd->options & NAND_BBT_NO_OOB);
491
Frieder Schrempff90da782019-04-17 12:36:37 +0000492 ret = scan_block_fast(this, bd, from, buf);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200493 if (ret < 0)
494 return ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000495
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200496 if (ret) {
Brian Norrisb4d20d62013-07-30 17:52:56 -0700497 bbt_mark_entry(this, i, BBT_BLOCK_FACTORY_BAD);
Brian Norris9a4d4d62011-07-19 10:06:07 -0700498 pr_warn("Bad eraseblock %d at 0x%012llx\n",
Brian Norrisb4d20d62013-07-30 17:52:56 -0700499 i, (unsigned long long)from);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200500 mtd->ecc_stats.badblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 from += (1 << this->bbt_erase_shift);
504 }
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
508/**
509 * search_bbt - [GENERIC] scan the device for a specific bad block table
Boris Brezillon08136212018-11-11 08:55:03 +0100510 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700511 * @buf: temporary buffer
512 * @td: descriptor for the bad block table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700514 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700521 * The bbt ident pattern resides in the oob area of the first page in a block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 */
Boris Brezillon08136212018-11-11 08:55:03 +0100523static int search_bbt(struct nand_chip *this, uint8_t *buf,
524 struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Boris Brezillon6c836d52018-10-29 11:22:16 +0100526 u64 targetsize = nanddev_target_size(&this->base);
Boris Brezillon08136212018-11-11 08:55:03 +0100527 struct mtd_info *mtd = nand_to_mtd(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 int i, chips;
Brian Norris930de532014-01-02 17:14:10 -0800529 int startblock, block, dir;
Joern Engel28318772006-05-22 23:18:05 +0200530 int scanlen = mtd->writesize + mtd->oobsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 int bbtblocks;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200532 int blocktopage = this->bbt_erase_shift - this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Brian Norris8b6e50c2011-05-25 14:59:01 -0700534 /* Search direction top -> down? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (td->options & NAND_BBT_LASTBLOCK) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100536 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 dir = -1;
538 } else {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000539 startblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000541 }
542
Brian Norris8b6e50c2011-05-25 14:59:01 -0700543 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (td->options & NAND_BBT_PERCHIP) {
Boris Brezillon32813e22018-10-29 11:58:29 +0100545 chips = nanddev_ntargets(&this->base);
Boris Brezillon6c836d52018-10-29 11:22:16 +0100546 bbtblocks = targetsize >> this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 startblock &= bbtblocks - 1;
548 } else {
549 chips = 1;
550 bbtblocks = mtd->size >> this->bbt_erase_shift;
551 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 for (i = 0; i < chips; i++) {
554 /* Reset version information */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000555 td->version[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 td->pages[i] = -1;
557 /* Scan the maximum number of blocks */
558 for (block = 0; block < td->maxblocks; block++) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 int actblock = startblock + dir * block;
Adrian Hunter69423d92008-12-10 13:37:21 +0000561 loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* Read first page */
Boris Brezillon08136212018-11-11 08:55:03 +0100564 scan_read(this, buf, offs, mtd->writesize, td);
Joern Engel28318772006-05-22 23:18:05 +0200565 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200566 td->pages[i] = actblock << blocktopage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (td->options & NAND_BBT_VERSION) {
Boris Brezillon08136212018-11-11 08:55:03 +0100568 offs = bbt_get_ver_offs(this, td);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200569 td->version[i] = buf[offs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571 break;
572 }
573 }
Boris Brezillon6c836d52018-10-29 11:22:16 +0100574 startblock += targetsize >> this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576 /* Check, if we found a bbt for each requested chip */
577 for (i = 0; i < chips; i++) {
578 if (td->pages[i] == -1)
Brian Norris9a4d4d62011-07-19 10:06:07 -0700579 pr_warn("Bad block table not found for chip %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 else
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200581 pr_info("Bad block table found at page %d, version 0x%02X\n",
582 td->pages[i], td->version[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000584 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
587/**
588 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
Boris Brezillon08136212018-11-11 08:55:03 +0100589 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700590 * @buf: temporary buffer
591 * @td: descriptor for the bad block table
592 * @md: descriptor for the bad block table mirror
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700594 * Search and read the bad block table(s).
595 */
Boris Brezillon08136212018-11-11 08:55:03 +0100596static void search_read_bbts(struct nand_chip *this, uint8_t *buf,
Brian Norris7b5a2d42012-06-22 16:35:41 -0700597 struct nand_bbt_descr *td,
598 struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 /* Search the primary table */
Boris Brezillon08136212018-11-11 08:55:03 +0100601 search_bbt(this, buf, td);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 /* Search the mirror table */
604 if (md)
Boris Brezillon08136212018-11-11 08:55:03 +0100605 search_bbt(this, buf, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000606}
607
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000608/**
Boris Brezillonc3baf272016-08-15 18:22:01 -0500609 * get_bbt_block - Get the first valid eraseblock suitable to store a BBT
610 * @this: the NAND device
611 * @td: the BBT description
612 * @md: the mirror BBT descriptor
613 * @chip: the CHIP selector
614 *
615 * This functions returns a positive block number pointing a valid eraseblock
616 * suitable to store a BBT (i.e. in the range reserved for BBT), or -ENOSPC if
617 * all blocks are already used of marked bad. If td->pages[chip] was already
618 * pointing to a valid block we re-use it, otherwise we search for the next
619 * valid one.
620 */
621static int get_bbt_block(struct nand_chip *this, struct nand_bbt_descr *td,
622 struct nand_bbt_descr *md, int chip)
623{
Boris Brezillon6c836d52018-10-29 11:22:16 +0100624 u64 targetsize = nanddev_target_size(&this->base);
Boris Brezillonc3baf272016-08-15 18:22:01 -0500625 int startblock, dir, page, numblocks, i;
626
627 /*
628 * There was already a version of the table, reuse the page. This
629 * applies for absolute placement too, as we have the page number in
630 * td->pages.
631 */
632 if (td->pages[chip] != -1)
633 return td->pages[chip] >>
634 (this->bbt_erase_shift - this->page_shift);
635
Boris Brezillon6c836d52018-10-29 11:22:16 +0100636 numblocks = (int)(targetsize >> this->bbt_erase_shift);
Boris Brezillonc3baf272016-08-15 18:22:01 -0500637 if (!(td->options & NAND_BBT_PERCHIP))
Boris Brezillon32813e22018-10-29 11:58:29 +0100638 numblocks *= nanddev_ntargets(&this->base);
Boris Brezillonc3baf272016-08-15 18:22:01 -0500639
640 /*
641 * Automatic placement of the bad block table. Search direction
642 * top -> down?
643 */
644 if (td->options & NAND_BBT_LASTBLOCK) {
645 startblock = numblocks * (chip + 1) - 1;
646 dir = -1;
647 } else {
648 startblock = chip * numblocks;
649 dir = 1;
650 }
651
652 for (i = 0; i < td->maxblocks; i++) {
653 int block = startblock + dir * i;
654
655 /* Check, if the block is bad */
656 switch (bbt_get_entry(this, block)) {
657 case BBT_BLOCK_WORN:
658 case BBT_BLOCK_FACTORY_BAD:
659 continue;
660 }
661
662 page = block << (this->bbt_erase_shift - this->page_shift);
663
664 /* Check, if the block is used by the mirror table */
665 if (!md || md->pages[chip] != page)
666 return block;
667 }
668
669 return -ENOSPC;
670}
671
672/**
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500673 * mark_bbt_block_bad - Mark one of the block reserved for BBT bad
674 * @this: the NAND device
675 * @td: the BBT description
676 * @chip: the CHIP selector
677 * @block: the BBT block to mark
678 *
679 * Blocks reserved for BBT can become bad. This functions is an helper to mark
680 * such blocks as bad. It takes care of updating the in-memory BBT, marking the
681 * block as bad using a bad block marker and invalidating the associated
682 * td->pages[] entry.
683 */
684static void mark_bbt_block_bad(struct nand_chip *this,
685 struct nand_bbt_descr *td,
686 int chip, int block)
687{
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500688 loff_t to;
689 int res;
690
691 bbt_mark_entry(this, block, BBT_BLOCK_WORN);
692
693 to = (loff_t)block << this->bbt_erase_shift;
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200694 res = nand_markbad_bbm(this, to);
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500695 if (res)
696 pr_warn("nand_bbt: error %d while marking block %d bad\n",
697 res, block);
698
699 td->pages[chip] = -1;
700}
701
702/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 * write_bbt - [GENERIC] (Re)write the bad block table
Boris Brezillon08136212018-11-11 08:55:03 +0100704 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700705 * @buf: temporary buffer
706 * @td: descriptor for the bad block table
707 * @md: descriptor for the bad block table mirror
708 * @chipsel: selector for a specific chip, -1 for all
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700710 * (Re)write the bad block table.
711 */
Boris Brezillon08136212018-11-11 08:55:03 +0100712static int write_bbt(struct nand_chip *this, uint8_t *buf,
Thomas Gleixner9223a452006-05-23 17:21:03 +0200713 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
714 int chipsel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Boris Brezillon6c836d52018-10-29 11:22:16 +0100716 u64 targetsize = nanddev_target_size(&this->base);
Boris Brezillon08136212018-11-11 08:55:03 +0100717 struct mtd_info *mtd = nand_to_mtd(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 struct erase_info einfo;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700719 int i, res, chip = 0;
Boris Brezillonc3baf272016-08-15 18:22:01 -0500720 int bits, page, offs, numblocks, sft, sftmsk;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700721 int nrchips, pageoffs, ooboffs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 uint8_t msk[4];
723 uint8_t rcode = td->reserved_block_code;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200724 size_t retlen, len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 loff_t to;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200726 struct mtd_oob_ops ops;
727
728 ops.ooblen = mtd->oobsize;
729 ops.ooboffs = 0;
730 ops.datbuf = NULL;
Brian Norris0612b9d2011-08-30 18:45:40 -0700731 ops.mode = MTD_OPS_PLACE_OOB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 if (!rcode)
734 rcode = 0xff;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700735 /* Write bad block table per chip rather than per device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (td->options & NAND_BBT_PERCHIP) {
Boris Brezillon6c836d52018-10-29 11:22:16 +0100737 numblocks = (int)(targetsize >> this->bbt_erase_shift);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700738 /* Full device write or specific chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (chipsel == -1) {
Boris Brezillon32813e22018-10-29 11:58:29 +0100740 nrchips = nanddev_ntargets(&this->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 } else {
742 nrchips = chipsel + 1;
743 chip = chipsel;
744 }
745 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +0100746 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 nrchips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000748 }
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 /* Loop through the chips */
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500751 while (chip < nrchips) {
Boris Brezillonc3baf272016-08-15 18:22:01 -0500752 int block;
753
754 block = get_bbt_block(this, td, md, chip);
755 if (block < 0) {
756 pr_err("No space left to write bad block table\n");
757 res = block;
758 goto outerr;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Brian Norris8b6e50c2011-05-25 14:59:01 -0700761 /*
Boris Brezillonc3baf272016-08-15 18:22:01 -0500762 * get_bbt_block() returns a block number, shift the value to
763 * get a page number.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700764 */
Boris Brezillonc3baf272016-08-15 18:22:01 -0500765 page = block << (this->bbt_erase_shift - this->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 /* Set up shift count and masks for the flash table */
768 bits = td->options & NAND_BBT_NRBITS_MSK;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200769 msk[2] = ~rcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 switch (bits) {
Thomas Gleixner9223a452006-05-23 17:21:03 +0200771 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
772 msk[3] = 0x01;
773 break;
774 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
775 msk[3] = 0x03;
776 break;
777 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
778 msk[3] = 0x0f;
779 break;
780 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
781 msk[3] = 0xff;
782 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 default: return -EINVAL;
784 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000785
Brian Norris596d7452011-09-07 13:13:33 -0700786 to = ((loff_t)page) << this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Brian Norris8b6e50c2011-05-25 14:59:01 -0700788 /* Must we save the block contents? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (td->options & NAND_BBT_SAVECONTENT) {
790 /* Make it block aligned */
Brian Norrisf5cd2ae2015-02-28 02:13:13 -0800791 to &= ~(((loff_t)1 << this->bbt_erase_shift) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 len = 1 << this->bbt_erase_shift;
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200793 res = mtd_read(mtd, to, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (res < 0) {
795 if (retlen != len) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200796 pr_info("nand_bbt: error reading block for writing the bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return res;
798 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200799 pr_warn("nand_bbt: ECC error while reading block for writing bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200801 /* Read oob data */
Vitaly Wool70145682006-11-03 18:20:38 +0300802 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200803 ops.oobbuf = &buf[len];
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200804 res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
Vitaly Wool70145682006-11-03 18:20:38 +0300805 if (res < 0 || ops.oobretlen != ops.ooblen)
Thomas Gleixner9223a452006-05-23 17:21:03 +0200806 goto outerr;
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 /* Calc the byte offset in the buffer */
809 pageoffs = page - (int)(to >> this->page_shift);
810 offs = pageoffs << this->page_shift;
811 /* Preset the bbt area with 0xff */
Brian Norris596d7452011-09-07 13:13:33 -0700812 memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
Thomas Gleixner9223a452006-05-23 17:21:03 +0200813 ooboffs = len + (pageoffs * mtd->oobsize);
814
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200815 } else if (td->options & NAND_BBT_NO_OOB) {
816 ooboffs = 0;
817 offs = td->len;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700818 /* The version byte */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200819 if (td->options & NAND_BBT_VERSION)
820 offs++;
821 /* Calc length */
Brian Norris596d7452011-09-07 13:13:33 -0700822 len = (size_t)(numblocks >> sft);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200823 len += offs;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700824 /* Make it page aligned! */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200825 len = ALIGN(len, mtd->writesize);
826 /* Preset the buffer with 0xff */
827 memset(buf, 0xff, len);
828 /* Pattern is located at the begin of first page */
829 memcpy(buf, td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 } else {
831 /* Calc length */
Brian Norris596d7452011-09-07 13:13:33 -0700832 len = (size_t)(numblocks >> sft);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700833 /* Make it page aligned! */
Sebastian Andrzej Siewiorcda32092010-09-29 19:43:50 +0200834 len = ALIGN(len, mtd->writesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 /* Preset the buffer with 0xff */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200836 memset(buf, 0xff, len +
837 (len >> this->page_shift)* mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 offs = 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200839 ooboffs = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 /* Pattern is located in oob area of first page */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200841 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000843
Thomas Gleixner9223a452006-05-23 17:21:03 +0200844 if (td->options & NAND_BBT_VERSION)
845 buf[ooboffs + td->veroffs] = td->version[chip];
846
Brian Norris8b6e50c2011-05-25 14:59:01 -0700847 /* Walk through the memory table */
Brian Norrisb4d20d62013-07-30 17:52:56 -0700848 for (i = 0; i < numblocks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 uint8_t dat;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700850 int sftcnt = (i << (3 - sft)) & sftmsk;
851 dat = bbt_get_entry(this, chip * numblocks + i);
852 /* Do not store the reserved bbt blocks! */
853 buf[offs + (i >> sft)] &= ~(msk[dat] << sftcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000855
David Woodhousee0c7d762006-05-13 18:07:53 +0100856 memset(&einfo, 0, sizeof(einfo));
Adrian Hunter69423d92008-12-10 13:37:21 +0000857 einfo.addr = to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 einfo.len = 1 << this->bbt_erase_shift;
Boris Brezillone4cdf9c2018-09-06 14:05:35 +0200859 res = nand_erase_nand(this, &einfo, 1);
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500860 if (res < 0) {
861 pr_warn("nand_bbt: error while erasing BBT block %d\n",
862 res);
863 mark_bbt_block_bad(this, td, chip, block);
864 continue;
865 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000866
Boris Brezillon08136212018-11-11 08:55:03 +0100867 res = scan_write_bbt(this, to, len, buf,
868 td->options & NAND_BBT_NO_OOB ?
869 NULL : &buf[len]);
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500870 if (res < 0) {
871 pr_warn("nand_bbt: error while writing BBT block %d\n",
872 res);
873 mark_bbt_block_bad(this, td, chip, block);
874 continue;
875 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200876
Brian Norrisd0370212011-07-19 10:06:08 -0700877 pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
878 (unsigned long long)to, td->version[chip]);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /* Mark it as used */
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500881 td->pages[chip++] = page;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200884
885 outerr:
Brian Norrisd0370212011-07-19 10:06:08 -0700886 pr_warn("nand_bbt: error while writing bad block table %d\n", res);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200887 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
890/**
891 * nand_memory_bbt - [GENERIC] create a memory based bad block table
Boris Brezillon08136212018-11-11 08:55:03 +0100892 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700893 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700895 * The function creates a memory based bbt by scanning the device for
896 * manufacturer / software marked good / bad blocks.
897 */
Boris Brezillon08136212018-11-11 08:55:03 +0100898static inline int nand_memory_bbt(struct nand_chip *this,
899 struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Boris Brezilloneeab7172018-10-28 15:27:55 +0100901 u8 *pagebuf = nand_get_data_buf(this);
902
903 return create_bbt(this, pagebuf, bd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
906/**
David Woodhousee0c7d762006-05-13 18:07:53 +0100907 * check_create - [GENERIC] create and write bbt(s) if necessary
Boris Brezillon08136212018-11-11 08:55:03 +0100908 * @this: the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700909 * @buf: temporary buffer
910 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700912 * The function checks the results of the previous call to read_bbt and creates
913 * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found
914 * for the chip/device. Update is necessary if one of the tables is missing or
915 * the version nr. of one table is less than the other.
916 */
Boris Brezillon08136212018-11-11 08:55:03 +0100917static int check_create(struct nand_chip *this, uint8_t *buf,
918 struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Brian Norris623978d2011-09-20 18:35:34 -0700920 int i, chips, writeops, create, chipsel, res, res2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 struct nand_bbt_descr *td = this->bbt_td;
922 struct nand_bbt_descr *md = this->bbt_md;
923 struct nand_bbt_descr *rd, *rd2;
924
Brian Norris8b6e50c2011-05-25 14:59:01 -0700925 /* Do we have a bbt per chip? */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000926 if (td->options & NAND_BBT_PERCHIP)
Boris Brezillon32813e22018-10-29 11:58:29 +0100927 chips = nanddev_ntargets(&this->base);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000928 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 chips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 for (i = 0; i < chips; i++) {
932 writeops = 0;
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700933 create = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 rd = NULL;
935 rd2 = NULL;
Brian Norris623978d2011-09-20 18:35:34 -0700936 res = res2 = 0;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700937 /* Per chip or per device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700939 /* Mirrored table available? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (md) {
941 if (td->pages[i] == -1 && md->pages[i] == -1) {
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700942 create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 writeops = 0x03;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700944 } else if (td->pages[i] == -1) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000945 rd = md;
Brian Norris596d7452011-09-07 13:13:33 -0700946 writeops = 0x01;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700947 } else if (md->pages[i] == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 rd = td;
Brian Norris596d7452011-09-07 13:13:33 -0700949 writeops = 0x02;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700950 } else if (td->version[i] == md->version[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 rd = td;
952 if (!(td->options & NAND_BBT_VERSION))
953 rd2 = md;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700954 } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 rd = td;
Brian Norris596d7452011-09-07 13:13:33 -0700956 writeops = 0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 } else {
958 rd = md;
Brian Norris596d7452011-09-07 13:13:33 -0700959 writeops = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 } else {
962 if (td->pages[i] == -1) {
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700963 create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 writeops = 0x01;
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700965 } else {
966 rd = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000969
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700970 if (create) {
971 /* Create the bad block table by scanning the device? */
972 if (!(td->options & NAND_BBT_CREATE))
973 continue;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000974
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700975 /* Create the table in memory by scanning the chip(s) */
976 if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
Boris Brezillon08136212018-11-11 08:55:03 +0100977 create_bbt(this, buf, bd, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700979 td->version[i] = 1;
980 if (md)
981 md->version[i] = 1;
982 }
983
Brian Norris8b6e50c2011-05-25 14:59:01 -0700984 /* Read back first? */
Brian Norris623978d2011-09-20 18:35:34 -0700985 if (rd) {
Boris Brezillon08136212018-11-11 08:55:03 +0100986 res = read_abs_bbt(this, buf, rd, chipsel);
Brian Norris623978d2011-09-20 18:35:34 -0700987 if (mtd_is_eccerr(res)) {
988 /* Mark table as invalid */
989 rd->pages[i] = -1;
Brian Norrisdadc17a2011-09-20 18:35:57 -0700990 rd->version[i] = 0;
Brian Norris623978d2011-09-20 18:35:34 -0700991 i--;
992 continue;
993 }
994 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700995 /* If they weren't versioned, read both */
Brian Norris623978d2011-09-20 18:35:34 -0700996 if (rd2) {
Boris Brezillon08136212018-11-11 08:55:03 +0100997 res2 = read_abs_bbt(this, buf, rd2, chipsel);
Brian Norris623978d2011-09-20 18:35:34 -0700998 if (mtd_is_eccerr(res2)) {
999 /* Mark table as invalid */
1000 rd2->pages[i] = -1;
Brian Norrisdadc17a2011-09-20 18:35:57 -07001001 rd2->version[i] = 0;
Brian Norris623978d2011-09-20 18:35:34 -07001002 i--;
1003 continue;
1004 }
1005 }
1006
1007 /* Scrub the flash table(s)? */
1008 if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
1009 writeops = 0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Brian Norrisdadc17a2011-09-20 18:35:57 -07001011 /* Update version numbers before writing */
1012 if (md) {
1013 td->version[i] = max(td->version[i], md->version[i]);
1014 md->version[i] = td->version[i];
1015 }
1016
Brian Norris8b6e50c2011-05-25 14:59:01 -07001017 /* Write the bad block table to the device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
Boris Brezillon08136212018-11-11 08:55:03 +01001019 res = write_bbt(this, buf, td, md, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (res < 0)
1021 return res;
1022 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001023
Brian Norris8b6e50c2011-05-25 14:59:01 -07001024 /* Write the mirror bad block table to the device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
Boris Brezillon08136212018-11-11 08:55:03 +01001026 res = write_bbt(this, buf, md, td, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (res < 0)
1028 return res;
1029 }
1030 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001031 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
1034/**
Boris Brezillon99f33512018-11-11 08:55:04 +01001035 * nand_update_bbt - update bad block table(s)
1036 * @this: the NAND device
1037 * @offs: the offset of the newly marked block
1038 *
1039 * The function updates the bad block table(s).
1040 */
1041static int nand_update_bbt(struct nand_chip *this, loff_t offs)
1042{
1043 struct mtd_info *mtd = nand_to_mtd(this);
1044 int len, res = 0;
1045 int chip, chipsel;
1046 uint8_t *buf;
1047 struct nand_bbt_descr *td = this->bbt_td;
1048 struct nand_bbt_descr *md = this->bbt_md;
1049
1050 if (!this->bbt || !td)
1051 return -EINVAL;
1052
1053 /* Allocate a temporary buffer for one eraseblock incl. oob */
1054 len = (1 << this->bbt_erase_shift);
1055 len += (len >> this->page_shift) * mtd->oobsize;
1056 buf = kmalloc(len, GFP_KERNEL);
1057 if (!buf)
1058 return -ENOMEM;
1059
1060 /* Do we have a bbt per chip? */
1061 if (td->options & NAND_BBT_PERCHIP) {
1062 chip = (int)(offs >> this->chip_shift);
1063 chipsel = chip;
1064 } else {
1065 chip = 0;
1066 chipsel = -1;
1067 }
1068
1069 td->version[chip]++;
1070 if (md)
1071 md->version[chip]++;
1072
1073 /* Write the bad block table to the device? */
1074 if (td->options & NAND_BBT_WRITE) {
1075 res = write_bbt(this, buf, td, md, chipsel);
1076 if (res < 0)
1077 goto out;
1078 }
1079 /* Write the mirror bad block table to the device? */
1080 if (md && (md->options & NAND_BBT_WRITE)) {
1081 res = write_bbt(this, buf, md, td, chipsel);
1082 }
1083
1084 out:
1085 kfree(buf);
1086 return res;
1087}
1088
1089/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001090 * mark_bbt_regions - [GENERIC] mark the bad block table regions
Boris Brezillon08136212018-11-11 08:55:03 +01001091 * @this: the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07001092 * @td: bad block table descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001094 * The bad block table regions are marked as "bad" to prevent accidental
1095 * erasures / writes. The regions are identified by the mark 0x02.
1096 */
Boris Brezillon08136212018-11-11 08:55:03 +01001097static void mark_bbt_region(struct nand_chip *this, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Boris Brezillon6c836d52018-10-29 11:22:16 +01001099 u64 targetsize = nanddev_target_size(&this->base);
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) {
Boris Brezillon32813e22018-10-29 11:58:29 +01001106 chips = nanddev_ntargets(&this->base);
Boris Brezillon6c836d52018-10-29 11:22:16 +01001107 nrblocks = (int)(targetsize >> this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 } 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 Brezillon6c836d52018-10-29 11:22:16 +01001160 u64 targetsize = nanddev_target_size(&this->base);
Boris Brezillon08136212018-11-11 08:55:03 +01001161 struct mtd_info *mtd = nand_to_mtd(this);
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001162 u32 pattern_len;
1163 u32 bits;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001164 u32 table_size;
1165
1166 if (!bd)
1167 return;
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001168
1169 pattern_len = bd->len;
1170 bits = bd->options & NAND_BBT_NRBITS_MSK;
1171
Brian Norrisa40f7342011-05-31 16:31:22 -07001172 BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001173 !(this->bbt_options & NAND_BBT_USE_FLASH));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001174 BUG_ON(!bits);
1175
1176 if (bd->options & NAND_BBT_VERSION)
1177 pattern_len++;
1178
1179 if (bd->options & NAND_BBT_NO_OOB) {
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001180 BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
Brian Norrisa40f7342011-05-31 16:31:22 -07001181 BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001182 BUG_ON(bd->offs);
1183 if (bd->options & NAND_BBT_VERSION)
1184 BUG_ON(bd->veroffs != bd->len);
1185 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1186 }
1187
1188 if (bd->options & NAND_BBT_PERCHIP)
Boris Brezillon6c836d52018-10-29 11:22:16 +01001189 table_size = targetsize >> this->bbt_erase_shift;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001190 else
1191 table_size = mtd->size >> this->bbt_erase_shift;
1192 table_size >>= 3;
1193 table_size *= bits;
1194 if (bd->options & NAND_BBT_NO_OOB)
1195 table_size += pattern_len;
1196 BUG_ON(table_size > (1 << this->bbt_erase_shift));
1197}
1198
1199/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
Boris Brezillon08136212018-11-11 08:55:03 +01001201 * @this: the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07001202 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001204 * The function checks, if a bad block table(s) is/are already available. If
1205 * not it scans the device for manufacturer marked good / bad blocks and writes
1206 * the bad block table(s) to the selected place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001208 * The bad block table memory is allocated here. It must be freed by calling
1209 * the nand_free_bbt function.
1210 */
Boris Brezillon08136212018-11-11 08:55:03 +01001211static int nand_scan_bbt(struct nand_chip *this, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Boris Brezillon08136212018-11-11 08:55:03 +01001213 struct mtd_info *mtd = nand_to_mtd(this);
Brian Norris83c59542015-02-28 02:13:12 -08001214 int len, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 uint8_t *buf;
1216 struct nand_bbt_descr *td = this->bbt_td;
1217 struct nand_bbt_descr *md = this->bbt_md;
1218
Sheng Yong192db1c2015-07-31 01:12:44 +00001219 len = (mtd->size >> (this->bbt_erase_shift + 2)) ? : 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07001220 /*
1221 * Allocate memory (2bit per block) and clear the memory bad block
1222 * table.
1223 */
Burman Yan95b93a02006-11-15 21:10:29 +02001224 this->bbt = kzalloc(len, GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001225 if (!this->bbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Brian Norris8b6e50c2011-05-25 14:59:01 -07001228 /*
1229 * If no primary table decriptor is given, scan the device to build a
1230 * memory based bad block table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 */
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001232 if (!td) {
Boris Brezillon08136212018-11-11 08:55:03 +01001233 if ((res = nand_memory_bbt(this, bd))) {
Brian Norrisd0370212011-07-19 10:06:08 -07001234 pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
Brian Norris83c59542015-02-28 02:13:12 -08001235 goto err;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001236 }
Brian Norris83c59542015-02-28 02:13:12 -08001237 return 0;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001238 }
Boris Brezillon08136212018-11-11 08:55:03 +01001239 verify_bbt_descr(this, td);
1240 verify_bbt_descr(this, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 /* Allocate a temporary buffer for one eraseblock incl. oob */
1243 len = (1 << this->bbt_erase_shift);
1244 len += (len >> this->page_shift) * mtd->oobsize;
David Woodhousec3f8abf2006-05-13 04:03:42 +01001245 buf = vmalloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (!buf) {
Brian Norris83c59542015-02-28 02:13:12 -08001247 res = -ENOMEM;
1248 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001250
Brian Norris8b6e50c2011-05-25 14:59:01 -07001251 /* Is the bbt at a given page? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (td->options & NAND_BBT_ABSPAGE) {
Boris Brezillon08136212018-11-11 08:55:03 +01001253 read_abs_bbts(this, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001254 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 /* Search the bad block table using a pattern in oob */
Boris Brezillon08136212018-11-11 08:55:03 +01001256 search_read_bbts(this, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Boris Brezillon08136212018-11-11 08:55:03 +01001259 res = check_create(this, buf, bd);
Brian Norris83c59542015-02-28 02:13:12 -08001260 if (res)
1261 goto err;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 /* Prevent the bbt regions from erasing / writing */
Boris Brezillon08136212018-11-11 08:55:03 +01001264 mark_bbt_region(this, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (md)
Boris Brezillon08136212018-11-11 08:55:03 +01001266 mark_bbt_region(this, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001267
David Woodhousee0c7d762006-05-13 18:07:53 +01001268 vfree(buf);
Brian Norris83c59542015-02-28 02:13:12 -08001269 return 0;
1270
1271err:
1272 kfree(this->bbt);
1273 this->bbt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return res;
1275}
1276
Brian Norris8b6e50c2011-05-25 14:59:01 -07001277/*
1278 * Define some generic bad / good block scan pattern which are used
1279 * while scanning a device for factory marked good / bad blocks.
1280 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1282
Brian Norris7854d3f2011-06-23 14:12:08 -07001283/* Generic flash bbt descriptors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1285static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1286
1287static struct nand_bbt_descr bbt_main_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001288 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1290 .offs = 8,
1291 .len = 4,
1292 .veroffs = 12,
Richard Genoud61de9da2012-09-11 15:50:54 +02001293 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 .pattern = bbt_pattern
1295};
1296
1297static struct nand_bbt_descr bbt_mirror_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001298 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1300 .offs = 8,
1301 .len = 4,
1302 .veroffs = 12,
Richard Genoud61de9da2012-09-11 15:50:54 +02001303 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 .pattern = mirror_pattern
1305};
1306
Brian Norris9fd6b372012-06-22 16:35:40 -07001307static struct nand_bbt_descr bbt_main_no_oob_descr = {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001308 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1309 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1310 | NAND_BBT_NO_OOB,
1311 .len = 4,
1312 .veroffs = 4,
Richard Genoud61de9da2012-09-11 15:50:54 +02001313 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001314 .pattern = bbt_pattern
1315};
1316
Brian Norris9fd6b372012-06-22 16:35:40 -07001317static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001318 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1319 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1320 | NAND_BBT_NO_OOB,
1321 .len = 4,
1322 .veroffs = 4,
Richard Genoud61de9da2012-09-11 15:50:54 +02001323 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001324 .pattern = mirror_pattern
1325};
1326
Brian Norris752ed6c2011-09-20 18:36:42 -07001327#define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
Brian Norris58373ff2010-07-15 12:15:44 -07001328/**
Brian Norris752ed6c2011-09-20 18:36:42 -07001329 * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001330 * @this: NAND chip to create descriptor for
Brian Norris58373ff2010-07-15 12:15:44 -07001331 *
1332 * This function allocates and initializes a nand_bbt_descr for BBM detection
Brian Norris752ed6c2011-09-20 18:36:42 -07001333 * based on the properties of @this. The new descriptor is stored in
Brian Norris58373ff2010-07-15 12:15:44 -07001334 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1335 * passed to this function.
Brian Norris58373ff2010-07-15 12:15:44 -07001336 */
Brian Norris752ed6c2011-09-20 18:36:42 -07001337static int nand_create_badblock_pattern(struct nand_chip *this)
Brian Norris58373ff2010-07-15 12:15:44 -07001338{
1339 struct nand_bbt_descr *bd;
1340 if (this->badblock_pattern) {
Brian Norris752ed6c2011-09-20 18:36:42 -07001341 pr_warn("Bad block pattern already allocated; not replacing\n");
Brian Norris58373ff2010-07-15 12:15:44 -07001342 return -EINVAL;
1343 }
1344 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001345 if (!bd)
Brian Norris58373ff2010-07-15 12:15:44 -07001346 return -ENOMEM;
Brian Norris752ed6c2011-09-20 18:36:42 -07001347 bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
Brian Norris58373ff2010-07-15 12:15:44 -07001348 bd->offs = this->badblockpos;
1349 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1350 bd->pattern = scan_ff_pattern;
1351 bd->options |= NAND_BBT_DYNAMICSTRUCT;
1352 this->badblock_pattern = bd;
1353 return 0;
1354}
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356/**
Boris Brezillon44b07b92018-07-05 12:27:30 +02001357 * nand_create_bbt - [NAND Interface] Select a default bad block table for the device
1358 * @this: NAND chip object
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001360 * This function selects the default bad block table support for the device and
1361 * calls the nand_scan_bbt function.
1362 */
Boris Brezillon44b07b92018-07-05 12:27:30 +02001363int nand_create_bbt(struct nand_chip *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Brian Norrisabb9cf72014-05-20 22:34:40 -07001365 int ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001366
Brian Norris8b6e50c2011-05-25 14:59:01 -07001367 /* Is a flash based bad block table requested? */
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001368 if (this->bbt_options & NAND_BBT_USE_FLASH) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001369 /* Use the default pattern descriptors */
1370 if (!this->bbt_td) {
Brian Norrisa40f7342011-05-31 16:31:22 -07001371 if (this->bbt_options & NAND_BBT_NO_OOB) {
Brian Norris9fd6b372012-06-22 16:35:40 -07001372 this->bbt_td = &bbt_main_no_oob_descr;
1373 this->bbt_md = &bbt_mirror_no_oob_descr;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001374 } else {
1375 this->bbt_td = &bbt_main_descr;
1376 this->bbt_md = &bbt_mirror_descr;
1377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 } else {
1380 this->bbt_td = NULL;
1381 this->bbt_md = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
Brian Norrisa2f812d2011-03-18 21:53:42 -07001383
Brian Norrisabb9cf72014-05-20 22:34:40 -07001384 if (!this->badblock_pattern) {
1385 ret = nand_create_badblock_pattern(this);
1386 if (ret)
1387 return ret;
1388 }
Brian Norrisa2f812d2011-03-18 21:53:42 -07001389
Boris Brezillon08136212018-11-11 08:55:03 +01001390 return nand_scan_bbt(this, this->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
Boris Brezillon44b07b92018-07-05 12:27:30 +02001392EXPORT_SYMBOL(nand_create_bbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394/**
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001395 * nand_isreserved_bbt - [NAND Interface] Check if a block is reserved
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001396 * @this: NAND chip object
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001397 * @offs: offset in the device
1398 */
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001399int nand_isreserved_bbt(struct nand_chip *this, loff_t offs)
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001400{
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001401 int block;
1402
1403 block = (int)(offs >> this->bbt_erase_shift);
1404 return bbt_get_entry(this, block) == BBT_BLOCK_RESERVED;
1405}
1406
1407/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001408 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001409 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07001410 * @offs: offset in the device
1411 * @allowbbt: allow access to bad block table region
1412 */
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001413int nand_isbad_bbt(struct nand_chip *this, loff_t offs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Brian Norris39dbb022013-07-30 17:52:57 -07001415 int block, res;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001416
Brian Norrisb4d20d62013-07-30 17:52:56 -07001417 block = (int)(offs >> this->bbt_erase_shift);
1418 res = bbt_get_entry(this, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02001420 pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1421 (unsigned int)offs, block, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Brian Norris39dbb022013-07-30 17:52:57 -07001423 switch (res) {
Brian Norris771c5682013-07-30 17:52:55 -07001424 case BBT_BLOCK_GOOD:
David Woodhousee0c7d762006-05-13 18:07:53 +01001425 return 0;
Brian Norris771c5682013-07-30 17:52:55 -07001426 case BBT_BLOCK_WORN:
David Woodhousee0c7d762006-05-13 18:07:53 +01001427 return 1;
Brian Norris771c5682013-07-30 17:52:55 -07001428 case BBT_BLOCK_RESERVED:
David Woodhousee0c7d762006-05-13 18:07:53 +01001429 return allowbbt ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
1431 return 1;
1432}
1433
Brian Norrisb32843b2013-07-30 17:52:59 -07001434/**
1435 * nand_markbad_bbt - [NAND Interface] Mark a block bad in the BBT
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001436 * @this: NAND chip object
Brian Norrisb32843b2013-07-30 17:52:59 -07001437 * @offs: offset of the bad block
1438 */
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001439int nand_markbad_bbt(struct nand_chip *this, loff_t offs)
Brian Norrisb32843b2013-07-30 17:52:59 -07001440{
Brian Norrisb32843b2013-07-30 17:52:59 -07001441 int block, ret = 0;
1442
1443 block = (int)(offs >> this->bbt_erase_shift);
1444
1445 /* Mark bad block in memory */
1446 bbt_mark_entry(this, block, BBT_BLOCK_WORN);
1447
1448 /* Update flash-based bad block table */
1449 if (this->bbt_options & NAND_BBT_USE_FLASH)
Boris Brezillon08136212018-11-11 08:55:03 +01001450 ret = nand_update_bbt(this, offs);
Brian Norrisb32843b2013-07-30 17:52:59 -07001451
1452 return ret;
1453}