blob: b5e3b54cc767d28eb3eb50596a3c3fa7612893fd [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 Norrisbb9ebd42011-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 Norrisbb9ebd42011-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
Boris Brezillon08136212018-11-11 08:55:03 +010080static int nand_update_bbt(struct nand_chip *chip, loff_t offs);
Brian Norrisb32843b2013-07-30 17:52:59 -070081
Brian Norris771c5682013-07-30 17:52:55 -070082static inline uint8_t bbt_get_entry(struct nand_chip *chip, int block)
83{
84 uint8_t entry = chip->bbt[block >> BBT_ENTRY_SHIFT];
85 entry >>= (block & BBT_ENTRY_MASK) * 2;
86 return entry & BBT_ENTRY_MASK;
87}
88
89static inline void bbt_mark_entry(struct nand_chip *chip, int block,
90 uint8_t mark)
91{
92 uint8_t msk = (mark & BBT_ENTRY_MASK) << ((block & BBT_ENTRY_MASK) * 2);
93 chip->bbt[block >> BBT_ENTRY_SHIFT] |= msk;
94}
95
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020096static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
97{
Brian Norris718894a2012-06-22 16:35:43 -070098 if (memcmp(buf, td->pattern, td->len))
99 return -1;
100 return 0;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200101}
102
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000103/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * check_pattern - [GENERIC] check if a pattern is in the buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700105 * @buf: the buffer to search
106 * @len: the length of buffer to search
107 * @paglen: the pagelength
108 * @td: search pattern descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700110 * Check for a pattern at the given place. Used to search bad block tables and
Brian Norrisdad22562013-07-30 17:53:00 -0700111 * good / bad block identifiers.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700112 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100113static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200115 if (td->options & NAND_BBT_NO_OOB)
116 return check_pattern_no_oob(buf, td);
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 /* Compare the pattern */
Brian Norrisdad22562013-07-30 17:53:00 -0700119 if (memcmp(buf + paglen + td->offs, td->pattern, td->len))
Brian Norris75b66d82011-09-07 13:13:41 -0700120 return -1;
Brian Norris58373ff2010-07-15 12:15:44 -0700121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return 0;
123}
124
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000125/**
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100126 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700127 * @buf: the buffer to search
128 * @td: search pattern descriptor
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100129 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700130 * Check for a pattern at the given place. Used to search bad block tables and
131 * good / bad block identifiers. Same as check_pattern, but no optional empty
132 * check.
133 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100134static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100135{
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100136 /* Compare the pattern */
Brian Norris491ed062012-06-22 16:35:44 -0700137 if (memcmp(buf + td->offs, td->pattern, td->len))
138 return -1;
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100139 return 0;
140}
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200143 * add_marker_len - compute the length of the marker in data area
Brian Norris8b6e50c2011-05-25 14:59:01 -0700144 * @td: BBT descriptor used for computation
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200145 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700146 * The length will be 0 if the marker is located in OOB area.
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200147 */
148static u32 add_marker_len(struct nand_bbt_descr *td)
149{
150 u32 len;
151
152 if (!(td->options & NAND_BBT_NO_OOB))
153 return 0;
154
155 len = td->len;
156 if (td->options & NAND_BBT_VERSION)
157 len++;
158 return len;
159}
160
161/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * read_bbt - [GENERIC] Read the bad block table starting from page
Boris Brezillon08136212018-11-11 08:55:03 +0100163 * @chip: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700164 * @buf: temporary buffer
165 * @page: the starting page
166 * @num: the number of bbt descriptors to read
Brian Norris7854d3f2011-06-23 14:12:08 -0700167 * @td: the bbt describtion table
Brian Norrisb4d20d62013-07-30 17:52:56 -0700168 * @offs: block number offset in the table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 *
170 * Read the bad block table starting from page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 */
Boris Brezillon08136212018-11-11 08:55:03 +0100172static int read_bbt(struct nand_chip *this, uint8_t *buf, int page, int num,
173 struct nand_bbt_descr *td, int offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Boris Brezillon08136212018-11-11 08:55:03 +0100175 struct mtd_info *mtd = nand_to_mtd(this);
Brian Norris167a8d52011-09-20 18:35:08 -0700176 int res, ret = 0, i, j, act = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 size_t retlen, len, totlen;
178 loff_t from;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200179 int bits = td->options & NAND_BBT_NRBITS_MSK;
Brian Norris596d7452011-09-07 13:13:33 -0700180 uint8_t msk = (uint8_t)((1 << bits) - 1);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200181 u32 marker_len;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200182 int reserved_block_code = td->reserved_block_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 totlen = (num * bits) >> 3;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200185 marker_len = add_marker_len(td);
Brian Norris596d7452011-09-07 13:13:33 -0700186 from = ((loff_t)page) << this->page_shift;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 while (totlen) {
Brian Norris596d7452011-09-07 13:13:33 -0700189 len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200190 if (marker_len) {
191 /*
192 * In case the BBT marker is not in the OOB area it
193 * will be just in the first page.
194 */
195 len -= marker_len;
196 from += marker_len;
197 marker_len = 0;
198 }
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200199 res = mtd_read(mtd, from, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (res < 0) {
Brian Norris167a8d52011-09-20 18:35:08 -0700201 if (mtd_is_eccerr(res)) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200202 pr_info("nand_bbt: ECC error in BBT at 0x%012llx\n",
203 from & ~mtd->writesize);
Brian Norris167a8d52011-09-20 18:35:08 -0700204 return res;
205 } else if (mtd_is_bitflip(res)) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200206 pr_info("nand_bbt: corrected error in BBT at 0x%012llx\n",
207 from & ~mtd->writesize);
Brian Norris167a8d52011-09-20 18:35:08 -0700208 ret = res;
209 } else {
210 pr_info("nand_bbt: error reading BBT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return res;
212 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 /* Analyse data */
216 for (i = 0; i < len; i++) {
217 uint8_t dat = buf[i];
Brian Norrisb4d20d62013-07-30 17:52:56 -0700218 for (j = 0; j < 8; j += bits, act++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 uint8_t tmp = (dat >> j) & msk;
220 if (tmp == msk)
221 continue;
David Woodhousee0c7d762006-05-13 18:07:53 +0100222 if (reserved_block_code && (tmp == reserved_block_code)) {
Brian Norrisd0370212011-07-19 10:06:08 -0700223 pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
Brian Norrisb4d20d62013-07-30 17:52:56 -0700224 (loff_t)(offs + act) <<
225 this->bbt_erase_shift);
226 bbt_mark_entry(this, offs + act,
Brian Norris771c5682013-07-30 17:52:55 -0700227 BBT_BLOCK_RESERVED);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200228 mtd->ecc_stats.bbtblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 continue;
230 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700231 /*
232 * Leave it for now, if it's matured we can
Brian Norrisa0f50802011-07-19 10:06:06 -0700233 * move this message to pr_debug.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700234 */
Brian Norrisd0370212011-07-19 10:06:08 -0700235 pr_info("nand_read_bbt: bad block at 0x%012llx\n",
Brian Norrisb4d20d62013-07-30 17:52:56 -0700236 (loff_t)(offs + act) <<
237 this->bbt_erase_shift);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700238 /* Factory marked bad or worn out? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (tmp == 0)
Brian Norrisb4d20d62013-07-30 17:52:56 -0700240 bbt_mark_entry(this, offs + act,
Brian Norris771c5682013-07-30 17:52:55 -0700241 BBT_BLOCK_FACTORY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 else
Brian Norrisb4d20d62013-07-30 17:52:56 -0700243 bbt_mark_entry(this, offs + act,
Brian Norris771c5682013-07-30 17:52:55 -0700244 BBT_BLOCK_WORN);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200245 mtd->ecc_stats.badblocks++;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248 totlen -= len;
249 from += len;
250 }
Brian Norris167a8d52011-09-20 18:35:08 -0700251 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254/**
255 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
Boris Brezillon08136212018-11-11 08:55:03 +0100256 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700257 * @buf: temporary buffer
258 * @td: descriptor for the bad block table
Brian Norris596d7452011-09-07 13:13:33 -0700259 * @chip: read the table for a specific chip, -1 read all chips; applies only if
Brian Norris8b6e50c2011-05-25 14:59:01 -0700260 * NAND_BBT_PERCHIP option is set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700262 * Read the bad block table for all chips starting at a given page. We assume
263 * that the bbt bits are in consecutive order.
264 */
Boris Brezillon08136212018-11-11 08:55:03 +0100265static int read_abs_bbt(struct nand_chip *this, uint8_t *buf,
266 struct nand_bbt_descr *td, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Boris Brezillon08136212018-11-11 08:55:03 +0100268 struct mtd_info *mtd = nand_to_mtd(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 int res = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (td->options & NAND_BBT_PERCHIP) {
272 int offs = 0;
273 for (i = 0; i < this->numchips; i++) {
274 if (chip == -1 || chip == i)
Boris Brezillon08136212018-11-11 08:55:03 +0100275 res = read_bbt(this, buf, td->pages[i],
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200276 this->chipsize >> this->bbt_erase_shift,
277 td, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (res)
279 return res;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700280 offs += this->chipsize >> this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282 } else {
Boris Brezillon08136212018-11-11 08:55:03 +0100283 res = read_bbt(this, buf, td->pages[0],
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200284 mtd->size >> this->bbt_erase_shift, td, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (res)
286 return res;
287 }
288 return 0;
289}
290
Brian Norris8b6e50c2011-05-25 14:59:01 -0700291/* BBT marker is in the first page, no OOB */
Boris Brezillon08136212018-11-11 08:55:03 +0100292static int scan_read_data(struct nand_chip *this, uint8_t *buf, loff_t offs,
293 struct nand_bbt_descr *td)
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200294{
Boris Brezillon08136212018-11-11 08:55:03 +0100295 struct mtd_info *mtd = nand_to_mtd(this);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200296 size_t retlen;
297 size_t len;
298
299 len = td->len;
300 if (td->options & NAND_BBT_VERSION)
301 len++;
302
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200303 return mtd_read(mtd, offs, len, &retlen, buf);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200304}
305
Brian Norrisa7e68832012-06-22 16:35:45 -0700306/**
Brian Norrisaf69dcd2012-06-22 16:35:42 -0700307 * scan_read_oob - [GENERIC] Scan data+OOB region to buffer
Boris Brezillon08136212018-11-11 08:55:03 +0100308 * @this: NAND chip object
Brian Norrisa7e68832012-06-22 16:35:45 -0700309 * @buf: temporary buffer
310 * @offs: offset at which to scan
311 * @len: length of data region to read
312 *
313 * Scan read data from data+OOB. May traverse multiple pages, interleaving
314 * page,OOB,page,OOB,... in buf. Completes transfer and returns the "strongest"
315 * ECC condition (error or bitflip). May quit on the first (non-ECC) error.
316 */
Boris Brezillon08136212018-11-11 08:55:03 +0100317static int scan_read_oob(struct nand_chip *this, uint8_t *buf, loff_t offs,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200318 size_t len)
319{
Boris Brezillon08136212018-11-11 08:55:03 +0100320 struct mtd_info *mtd = nand_to_mtd(this);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200321 struct mtd_oob_ops ops;
Brian Norrisa7e68832012-06-22 16:35:45 -0700322 int res, ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200323
Brian Norrisa7e68832012-06-22 16:35:45 -0700324 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200325 ops.ooboffs = 0;
326 ops.ooblen = mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200327
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200328 while (len > 0) {
Brian Norris105513c2011-09-07 13:13:28 -0700329 ops.datbuf = buf;
330 ops.len = min(len, (size_t)mtd->writesize);
331 ops.oobbuf = buf + ops.len;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200332
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200333 res = mtd_read_oob(mtd, offs, &ops);
Brian Norrisa7e68832012-06-22 16:35:45 -0700334 if (res) {
335 if (!mtd_is_bitflip_or_eccerr(res))
336 return res;
337 else if (mtd_is_eccerr(res) || !ret)
338 ret = res;
339 }
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200340
341 buf += mtd->oobsize + mtd->writesize;
342 len -= mtd->writesize;
Dmitry Maluka34a57042012-05-11 20:51:51 +0300343 offs += mtd->writesize;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200344 }
Brian Norrisa7e68832012-06-22 16:35:45 -0700345 return ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200346}
347
Boris Brezillon08136212018-11-11 08:55:03 +0100348static int scan_read(struct nand_chip *this, uint8_t *buf, loff_t offs,
349 size_t len, struct nand_bbt_descr *td)
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200350{
351 if (td->options & NAND_BBT_NO_OOB)
Boris Brezillon08136212018-11-11 08:55:03 +0100352 return scan_read_data(this, buf, offs, td);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200353 else
Boris Brezillon08136212018-11-11 08:55:03 +0100354 return scan_read_oob(this, buf, offs, len);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200355}
356
Brian Norris8b6e50c2011-05-25 14:59:01 -0700357/* Scan write data with oob to flash */
Boris Brezillon08136212018-11-11 08:55:03 +0100358static int scan_write_bbt(struct nand_chip *this, loff_t offs, size_t len,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200359 uint8_t *buf, uint8_t *oob)
360{
Boris Brezillon08136212018-11-11 08:55:03 +0100361 struct mtd_info *mtd = nand_to_mtd(this);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200362 struct mtd_oob_ops ops;
363
Brian Norris0612b9d2011-08-30 18:45:40 -0700364 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200365 ops.ooboffs = 0;
366 ops.ooblen = mtd->oobsize;
367 ops.datbuf = buf;
368 ops.oobbuf = oob;
369 ops.len = len;
370
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200371 return mtd_write_oob(mtd, offs, &ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200372}
373
Boris Brezillon08136212018-11-11 08:55:03 +0100374static u32 bbt_get_ver_offs(struct nand_chip *this, struct nand_bbt_descr *td)
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200375{
Boris Brezillon08136212018-11-11 08:55:03 +0100376 struct mtd_info *mtd = nand_to_mtd(this);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200377 u32 ver_offs = td->veroffs;
378
379 if (!(td->options & NAND_BBT_NO_OOB))
380 ver_offs += mtd->writesize;
381 return ver_offs;
382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384/**
385 * 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 +0100386 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700387 * @buf: temporary buffer
388 * @td: descriptor for the bad block table
389 * @md: descriptor for the bad block table mirror
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700391 * Read the bad block table(s) for all chips starting at a given page. We
392 * assume that the bbt bits are in consecutive order.
393 */
Boris Brezillon08136212018-11-11 08:55:03 +0100394static void read_abs_bbts(struct nand_chip *this, uint8_t *buf,
Brian Norris7b5a2d42012-06-22 16:35:41 -0700395 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Boris Brezillon08136212018-11-11 08:55:03 +0100397 struct mtd_info *mtd = nand_to_mtd(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000399 /* Read the primary version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (td->options & NAND_BBT_VERSION) {
Boris Brezillon08136212018-11-11 08:55:03 +0100401 scan_read(this, buf, (loff_t)td->pages[0] << this->page_shift,
402 mtd->writesize, td);
403 td->version[0] = buf[bbt_get_ver_offs(this, td)];
Brian Norris9a4d4d62011-07-19 10:06:07 -0700404 pr_info("Bad block table at page %d, version 0x%02X\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700405 td->pages[0], td->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000408 /* Read the mirror version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (md && (md->options & NAND_BBT_VERSION)) {
Boris Brezillon08136212018-11-11 08:55:03 +0100410 scan_read(this, buf, (loff_t)md->pages[0] << this->page_shift,
411 mtd->writesize, md);
412 md->version[0] = buf[bbt_get_ver_offs(this, md)];
Brian Norris9a4d4d62011-07-19 10:06:07 -0700413 pr_info("Bad block table at page %d, version 0x%02X\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700414 md->pages[0], md->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Brian Norris8b6e50c2011-05-25 14:59:01 -0700418/* Scan a given block partially */
Boris Brezillon08136212018-11-11 08:55:03 +0100419static int scan_block_fast(struct nand_chip *this, struct nand_bbt_descr *bd,
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300420 loff_t offs, uint8_t *buf, int numpages)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200421{
Boris Brezillon08136212018-11-11 08:55:03 +0100422 struct mtd_info *mtd = nand_to_mtd(this);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200423 struct mtd_oob_ops ops;
424 int j, ret;
425
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200426 ops.ooblen = mtd->oobsize;
427 ops.oobbuf = buf;
428 ops.ooboffs = 0;
429 ops.datbuf = NULL;
Brian Norris0612b9d2011-08-30 18:45:40 -0700430 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200431
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300432 for (j = 0; j < numpages; j++) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200433 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700434 * Read the full oob until read_oob is fixed to handle single
435 * byte reads for 16 bit buswidth.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200436 */
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200437 ret = mtd_read_oob(mtd, offs, &ops);
Brian Norris903cd062011-06-28 16:28:58 -0700438 /* Ignore ECC errors when checking for BBM */
Brian Norrisd57f40542011-09-20 18:34:25 -0700439 if (ret && !mtd_is_bitflip_or_eccerr(ret))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200440 return ret;
441
442 if (check_short_pattern(buf, bd))
443 return 1;
444
445 offs += mtd->writesize;
446 }
447 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 Brezillon08136212018-11-11 08:55:03 +0100464 struct mtd_info *mtd = nand_to_mtd(this);
Brian Norris5961ad22013-10-30 00:41:30 -0400465 int i, numblocks, numpages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 int startblock;
467 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
Brian Norris5961ad22013-10-30 00:41:30 -0400471 if (bd->options & NAND_BBT_SCAN2NDPAGE)
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300472 numpages = 2;
Brian Norris58373ff2010-07-15 12:15:44 -0700473 else
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300474 numpages = 1;
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (chip == -1) {
Brian Norrisb4d20d62013-07-30 17:52:56 -0700477 numblocks = mtd->size >> this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 startblock = 0;
479 from = 0;
480 } else {
481 if (chip >= this->numchips) {
Brian Norris9a4d4d62011-07-19 10:06:07 -0700482 pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
David Woodhousee0c7d762006-05-13 18:07:53 +0100483 chip + 1, this->numchips);
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000484 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
Brian Norrisb4d20d62013-07-30 17:52:56 -0700486 numblocks = this->chipsize >> this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 startblock = chip * numblocks;
488 numblocks += startblock;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700489 from = (loff_t)startblock << this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000491
Brian Norris5fb15492011-05-31 16:31:21 -0700492 if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300493 from += mtd->erasesize - (mtd->writesize * numpages);
Kevin Cernekeeb60b08b2010-05-04 20:58:10 -0700494
Brian Norrisb4d20d62013-07-30 17:52:56 -0700495 for (i = startblock; i < numblocks; i++) {
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000496 int ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000497
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200498 BUG_ON(bd->options & NAND_BBT_NO_OOB);
499
Boris Brezillon08136212018-11-11 08:55:03 +0100500 ret = scan_block_fast(this, bd, from, buf, numpages);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200501 if (ret < 0)
502 return ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000503
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200504 if (ret) {
Brian Norrisb4d20d62013-07-30 17:52:56 -0700505 bbt_mark_entry(this, i, BBT_BLOCK_FACTORY_BAD);
Brian Norris9a4d4d62011-07-19 10:06:07 -0700506 pr_warn("Bad eraseblock %d at 0x%012llx\n",
Brian Norrisb4d20d62013-07-30 17:52:56 -0700507 i, (unsigned long long)from);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200508 mtd->ecc_stats.badblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 from += (1 << this->bbt_erase_shift);
512 }
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000513 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
516/**
517 * search_bbt - [GENERIC] scan the device for a specific bad block table
Boris Brezillon08136212018-11-11 08:55:03 +0100518 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700519 * @buf: temporary buffer
520 * @td: descriptor for the bad block table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700522 * Read the bad block table by searching for a given ident pattern. Search is
523 * preformed either from the beginning up or from the end of the device
524 * downwards. The search starts always at the start of a block. If the option
525 * NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains
526 * the bad block information of this chip. This is necessary to provide support
527 * for certain DOC devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700529 * The bbt ident pattern resides in the oob area of the first page in a block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 */
Boris Brezillon08136212018-11-11 08:55:03 +0100531static int search_bbt(struct nand_chip *this, uint8_t *buf,
532 struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Boris Brezillon08136212018-11-11 08:55:03 +0100534 struct mtd_info *mtd = nand_to_mtd(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 int i, chips;
Brian Norris930de532014-01-02 17:14:10 -0800536 int startblock, block, dir;
Joern Engel28318772006-05-22 23:18:05 +0200537 int scanlen = mtd->writesize + mtd->oobsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 int bbtblocks;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200539 int blocktopage = this->bbt_erase_shift - this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Brian Norris8b6e50c2011-05-25 14:59:01 -0700541 /* Search direction top -> down? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (td->options & NAND_BBT_LASTBLOCK) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100543 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 dir = -1;
545 } else {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000546 startblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000548 }
549
Brian Norris8b6e50c2011-05-25 14:59:01 -0700550 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (td->options & NAND_BBT_PERCHIP) {
552 chips = this->numchips;
553 bbtblocks = this->chipsize >> this->bbt_erase_shift;
554 startblock &= bbtblocks - 1;
555 } else {
556 chips = 1;
557 bbtblocks = mtd->size >> this->bbt_erase_shift;
558 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 for (i = 0; i < chips; i++) {
561 /* Reset version information */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000562 td->version[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 td->pages[i] = -1;
564 /* Scan the maximum number of blocks */
565 for (block = 0; block < td->maxblocks; block++) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 int actblock = startblock + dir * block;
Adrian Hunter69423d92008-12-10 13:37:21 +0000568 loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /* Read first page */
Boris Brezillon08136212018-11-11 08:55:03 +0100571 scan_read(this, buf, offs, mtd->writesize, td);
Joern Engel28318772006-05-22 23:18:05 +0200572 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200573 td->pages[i] = actblock << blocktopage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (td->options & NAND_BBT_VERSION) {
Boris Brezillon08136212018-11-11 08:55:03 +0100575 offs = bbt_get_ver_offs(this, td);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200576 td->version[i] = buf[offs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578 break;
579 }
580 }
581 startblock += this->chipsize >> this->bbt_erase_shift;
582 }
583 /* Check, if we found a bbt for each requested chip */
584 for (i = 0; i < chips; i++) {
585 if (td->pages[i] == -1)
Brian Norris9a4d4d62011-07-19 10:06:07 -0700586 pr_warn("Bad block table not found for chip %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 else
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200588 pr_info("Bad block table found at page %d, version 0x%02X\n",
589 td->pages[i], td->version[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594/**
595 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
Boris Brezillon08136212018-11-11 08:55:03 +0100596 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700597 * @buf: temporary buffer
598 * @td: descriptor for the bad block table
599 * @md: descriptor for the bad block table mirror
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700601 * Search and read the bad block table(s).
602 */
Boris Brezillon08136212018-11-11 08:55:03 +0100603static void search_read_bbts(struct nand_chip *this, uint8_t *buf,
Brian Norris7b5a2d42012-06-22 16:35:41 -0700604 struct nand_bbt_descr *td,
605 struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 /* Search the primary table */
Boris Brezillon08136212018-11-11 08:55:03 +0100608 search_bbt(this, buf, td);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /* Search the mirror table */
611 if (md)
Boris Brezillon08136212018-11-11 08:55:03 +0100612 search_bbt(this, buf, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000613}
614
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000615/**
Boris Brezillonc3baf272016-08-15 18:22:01 -0500616 * get_bbt_block - Get the first valid eraseblock suitable to store a BBT
617 * @this: the NAND device
618 * @td: the BBT description
619 * @md: the mirror BBT descriptor
620 * @chip: the CHIP selector
621 *
622 * This functions returns a positive block number pointing a valid eraseblock
623 * suitable to store a BBT (i.e. in the range reserved for BBT), or -ENOSPC if
624 * all blocks are already used of marked bad. If td->pages[chip] was already
625 * pointing to a valid block we re-use it, otherwise we search for the next
626 * valid one.
627 */
628static int get_bbt_block(struct nand_chip *this, struct nand_bbt_descr *td,
629 struct nand_bbt_descr *md, int chip)
630{
631 int startblock, dir, page, numblocks, i;
632
633 /*
634 * There was already a version of the table, reuse the page. This
635 * applies for absolute placement too, as we have the page number in
636 * td->pages.
637 */
638 if (td->pages[chip] != -1)
639 return td->pages[chip] >>
640 (this->bbt_erase_shift - this->page_shift);
641
642 numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
643 if (!(td->options & NAND_BBT_PERCHIP))
644 numblocks *= this->numchips;
645
646 /*
647 * Automatic placement of the bad block table. Search direction
648 * top -> down?
649 */
650 if (td->options & NAND_BBT_LASTBLOCK) {
651 startblock = numblocks * (chip + 1) - 1;
652 dir = -1;
653 } else {
654 startblock = chip * numblocks;
655 dir = 1;
656 }
657
658 for (i = 0; i < td->maxblocks; i++) {
659 int block = startblock + dir * i;
660
661 /* Check, if the block is bad */
662 switch (bbt_get_entry(this, block)) {
663 case BBT_BLOCK_WORN:
664 case BBT_BLOCK_FACTORY_BAD:
665 continue;
666 }
667
668 page = block << (this->bbt_erase_shift - this->page_shift);
669
670 /* Check, if the block is used by the mirror table */
671 if (!md || md->pages[chip] != page)
672 return block;
673 }
674
675 return -ENOSPC;
676}
677
678/**
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500679 * mark_bbt_block_bad - Mark one of the block reserved for BBT bad
680 * @this: the NAND device
681 * @td: the BBT description
682 * @chip: the CHIP selector
683 * @block: the BBT block to mark
684 *
685 * Blocks reserved for BBT can become bad. This functions is an helper to mark
686 * such blocks as bad. It takes care of updating the in-memory BBT, marking the
687 * block as bad using a bad block marker and invalidating the associated
688 * td->pages[] entry.
689 */
690static void mark_bbt_block_bad(struct nand_chip *this,
691 struct nand_bbt_descr *td,
692 int chip, int block)
693{
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500694 loff_t to;
695 int res;
696
697 bbt_mark_entry(this, block, BBT_BLOCK_WORN);
698
699 to = (loff_t)block << this->bbt_erase_shift;
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200700 res = nand_markbad_bbm(this, to);
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500701 if (res)
702 pr_warn("nand_bbt: error %d while marking block %d bad\n",
703 res, block);
704
705 td->pages[chip] = -1;
706}
707
708/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 * write_bbt - [GENERIC] (Re)write the bad block table
Boris Brezillon08136212018-11-11 08:55:03 +0100710 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700711 * @buf: temporary buffer
712 * @td: descriptor for the bad block table
713 * @md: descriptor for the bad block table mirror
714 * @chipsel: selector for a specific chip, -1 for all
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700716 * (Re)write the bad block table.
717 */
Boris Brezillon08136212018-11-11 08:55:03 +0100718static int write_bbt(struct nand_chip *this, uint8_t *buf,
Thomas Gleixner9223a452006-05-23 17:21:03 +0200719 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
720 int chipsel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Boris Brezillon08136212018-11-11 08:55:03 +0100722 struct mtd_info *mtd = nand_to_mtd(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 struct erase_info einfo;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700724 int i, res, chip = 0;
Boris Brezillonc3baf272016-08-15 18:22:01 -0500725 int bits, page, offs, numblocks, sft, sftmsk;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700726 int nrchips, pageoffs, ooboffs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 uint8_t msk[4];
728 uint8_t rcode = td->reserved_block_code;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200729 size_t retlen, len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 loff_t to;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200731 struct mtd_oob_ops ops;
732
733 ops.ooblen = mtd->oobsize;
734 ops.ooboffs = 0;
735 ops.datbuf = NULL;
Brian Norris0612b9d2011-08-30 18:45:40 -0700736 ops.mode = MTD_OPS_PLACE_OOB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 if (!rcode)
739 rcode = 0xff;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700740 /* Write bad block table per chip rather than per device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (td->options & NAND_BBT_PERCHIP) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100742 numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700743 /* Full device write or specific chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (chipsel == -1) {
745 nrchips = this->numchips;
746 } else {
747 nrchips = chipsel + 1;
748 chip = chipsel;
749 }
750 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +0100751 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 nrchips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000753 }
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 /* Loop through the chips */
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500756 while (chip < nrchips) {
Boris Brezillonc3baf272016-08-15 18:22:01 -0500757 int block;
758
759 block = get_bbt_block(this, td, md, chip);
760 if (block < 0) {
761 pr_err("No space left to write bad block table\n");
762 res = block;
763 goto outerr;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Brian Norris8b6e50c2011-05-25 14:59:01 -0700766 /*
Boris Brezillonc3baf272016-08-15 18:22:01 -0500767 * get_bbt_block() returns a block number, shift the value to
768 * get a page number.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700769 */
Boris Brezillonc3baf272016-08-15 18:22:01 -0500770 page = block << (this->bbt_erase_shift - this->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 /* Set up shift count and masks for the flash table */
773 bits = td->options & NAND_BBT_NRBITS_MSK;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200774 msk[2] = ~rcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 switch (bits) {
Thomas Gleixner9223a452006-05-23 17:21:03 +0200776 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
777 msk[3] = 0x01;
778 break;
779 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
780 msk[3] = 0x03;
781 break;
782 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
783 msk[3] = 0x0f;
784 break;
785 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
786 msk[3] = 0xff;
787 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 default: return -EINVAL;
789 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000790
Brian Norris596d7452011-09-07 13:13:33 -0700791 to = ((loff_t)page) << this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Brian Norris8b6e50c2011-05-25 14:59:01 -0700793 /* Must we save the block contents? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (td->options & NAND_BBT_SAVECONTENT) {
795 /* Make it block aligned */
Brian Norrisf5cd2ae2015-02-28 02:13:13 -0800796 to &= ~(((loff_t)1 << this->bbt_erase_shift) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 len = 1 << this->bbt_erase_shift;
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200798 res = mtd_read(mtd, to, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (res < 0) {
800 if (retlen != len) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200801 pr_info("nand_bbt: error reading block for writing the bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return res;
803 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200804 pr_warn("nand_bbt: ECC error while reading block for writing bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200806 /* Read oob data */
Vitaly Wool70145682006-11-03 18:20:38 +0300807 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200808 ops.oobbuf = &buf[len];
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200809 res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
Vitaly Wool70145682006-11-03 18:20:38 +0300810 if (res < 0 || ops.oobretlen != ops.ooblen)
Thomas Gleixner9223a452006-05-23 17:21:03 +0200811 goto outerr;
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /* Calc the byte offset in the buffer */
814 pageoffs = page - (int)(to >> this->page_shift);
815 offs = pageoffs << this->page_shift;
816 /* Preset the bbt area with 0xff */
Brian Norris596d7452011-09-07 13:13:33 -0700817 memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
Thomas Gleixner9223a452006-05-23 17:21:03 +0200818 ooboffs = len + (pageoffs * mtd->oobsize);
819
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200820 } else if (td->options & NAND_BBT_NO_OOB) {
821 ooboffs = 0;
822 offs = td->len;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700823 /* The version byte */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200824 if (td->options & NAND_BBT_VERSION)
825 offs++;
826 /* Calc length */
Brian Norris596d7452011-09-07 13:13:33 -0700827 len = (size_t)(numblocks >> sft);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200828 len += offs;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700829 /* Make it page aligned! */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200830 len = ALIGN(len, mtd->writesize);
831 /* Preset the buffer with 0xff */
832 memset(buf, 0xff, len);
833 /* Pattern is located at the begin of first page */
834 memcpy(buf, td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 } else {
836 /* Calc length */
Brian Norris596d7452011-09-07 13:13:33 -0700837 len = (size_t)(numblocks >> sft);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700838 /* Make it page aligned! */
Sebastian Andrzej Siewiorcda32092010-09-29 19:43:50 +0200839 len = ALIGN(len, mtd->writesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 /* Preset the buffer with 0xff */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200841 memset(buf, 0xff, len +
842 (len >> this->page_shift)* mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 offs = 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200844 ooboffs = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 /* Pattern is located in oob area of first page */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200846 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000848
Thomas Gleixner9223a452006-05-23 17:21:03 +0200849 if (td->options & NAND_BBT_VERSION)
850 buf[ooboffs + td->veroffs] = td->version[chip];
851
Brian Norris8b6e50c2011-05-25 14:59:01 -0700852 /* Walk through the memory table */
Brian Norrisb4d20d62013-07-30 17:52:56 -0700853 for (i = 0; i < numblocks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 uint8_t dat;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700855 int sftcnt = (i << (3 - sft)) & sftmsk;
856 dat = bbt_get_entry(this, chip * numblocks + i);
857 /* Do not store the reserved bbt blocks! */
858 buf[offs + (i >> sft)] &= ~(msk[dat] << sftcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000860
David Woodhousee0c7d762006-05-13 18:07:53 +0100861 memset(&einfo, 0, sizeof(einfo));
Adrian Hunter69423d92008-12-10 13:37:21 +0000862 einfo.addr = to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 einfo.len = 1 << this->bbt_erase_shift;
Boris Brezillone4cdf9c2018-09-06 14:05:35 +0200864 res = nand_erase_nand(this, &einfo, 1);
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500865 if (res < 0) {
866 pr_warn("nand_bbt: error while erasing BBT block %d\n",
867 res);
868 mark_bbt_block_bad(this, td, chip, block);
869 continue;
870 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000871
Boris Brezillon08136212018-11-11 08:55:03 +0100872 res = scan_write_bbt(this, to, len, buf,
873 td->options & NAND_BBT_NO_OOB ?
874 NULL : &buf[len]);
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500875 if (res < 0) {
876 pr_warn("nand_bbt: error while writing BBT block %d\n",
877 res);
878 mark_bbt_block_bad(this, td, chip, block);
879 continue;
880 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200881
Brian Norrisd0370212011-07-19 10:06:08 -0700882 pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
883 (unsigned long long)to, td->version[chip]);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 /* Mark it as used */
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500886 td->pages[chip++] = page;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200889
890 outerr:
Brian Norrisd0370212011-07-19 10:06:08 -0700891 pr_warn("nand_bbt: error while writing bad block table %d\n", res);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200892 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
895/**
896 * nand_memory_bbt - [GENERIC] create a memory based bad block table
Boris Brezillon08136212018-11-11 08:55:03 +0100897 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700898 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700900 * The function creates a memory based bbt by scanning the device for
901 * manufacturer / software marked good / bad blocks.
902 */
Boris Brezillon08136212018-11-11 08:55:03 +0100903static inline int nand_memory_bbt(struct nand_chip *this,
904 struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Boris Brezillon08136212018-11-11 08:55:03 +0100906 return create_bbt(this, this->data_buf, bd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
909/**
David Woodhousee0c7d762006-05-13 18:07:53 +0100910 * check_create - [GENERIC] create and write bbt(s) if necessary
Boris Brezillon08136212018-11-11 08:55:03 +0100911 * @this: the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700912 * @buf: temporary buffer
913 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700915 * The function checks the results of the previous call to read_bbt and creates
916 * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found
917 * for the chip/device. Update is necessary if one of the tables is missing or
918 * the version nr. of one table is less than the other.
919 */
Boris Brezillon08136212018-11-11 08:55:03 +0100920static int check_create(struct nand_chip *this, uint8_t *buf,
921 struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Brian Norris623978d2011-09-20 18:35:34 -0700923 int i, chips, writeops, create, chipsel, res, res2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 struct nand_bbt_descr *td = this->bbt_td;
925 struct nand_bbt_descr *md = this->bbt_md;
926 struct nand_bbt_descr *rd, *rd2;
927
Brian Norris8b6e50c2011-05-25 14:59:01 -0700928 /* Do we have a bbt per chip? */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000929 if (td->options & NAND_BBT_PERCHIP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 chips = this->numchips;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000931 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 chips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 for (i = 0; i < chips; i++) {
935 writeops = 0;
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700936 create = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 rd = NULL;
938 rd2 = NULL;
Brian Norris623978d2011-09-20 18:35:34 -0700939 res = res2 = 0;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700940 /* Per chip or per device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700942 /* Mirrored table available? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (md) {
944 if (td->pages[i] == -1 && md->pages[i] == -1) {
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700945 create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 writeops = 0x03;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700947 } else if (td->pages[i] == -1) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000948 rd = md;
Brian Norris596d7452011-09-07 13:13:33 -0700949 writeops = 0x01;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700950 } else if (md->pages[i] == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 rd = td;
Brian Norris596d7452011-09-07 13:13:33 -0700952 writeops = 0x02;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700953 } else if (td->version[i] == md->version[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 rd = td;
955 if (!(td->options & NAND_BBT_VERSION))
956 rd2 = md;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700957 } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 rd = td;
Brian Norris596d7452011-09-07 13:13:33 -0700959 writeops = 0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 } else {
961 rd = md;
Brian Norris596d7452011-09-07 13:13:33 -0700962 writeops = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 } else {
965 if (td->pages[i] == -1) {
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700966 create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 writeops = 0x01;
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700968 } else {
969 rd = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000972
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700973 if (create) {
974 /* Create the bad block table by scanning the device? */
975 if (!(td->options & NAND_BBT_CREATE))
976 continue;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000977
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700978 /* Create the table in memory by scanning the chip(s) */
979 if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
Boris Brezillon08136212018-11-11 08:55:03 +0100980 create_bbt(this, buf, bd, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700982 td->version[i] = 1;
983 if (md)
984 md->version[i] = 1;
985 }
986
Brian Norris8b6e50c2011-05-25 14:59:01 -0700987 /* Read back first? */
Brian Norris623978d2011-09-20 18:35:34 -0700988 if (rd) {
Boris Brezillon08136212018-11-11 08:55:03 +0100989 res = read_abs_bbt(this, buf, rd, chipsel);
Brian Norris623978d2011-09-20 18:35:34 -0700990 if (mtd_is_eccerr(res)) {
991 /* Mark table as invalid */
992 rd->pages[i] = -1;
Brian Norrisdadc17a2011-09-20 18:35:57 -0700993 rd->version[i] = 0;
Brian Norris623978d2011-09-20 18:35:34 -0700994 i--;
995 continue;
996 }
997 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700998 /* If they weren't versioned, read both */
Brian Norris623978d2011-09-20 18:35:34 -0700999 if (rd2) {
Boris Brezillon08136212018-11-11 08:55:03 +01001000 res2 = read_abs_bbt(this, buf, rd2, chipsel);
Brian Norris623978d2011-09-20 18:35:34 -07001001 if (mtd_is_eccerr(res2)) {
1002 /* Mark table as invalid */
1003 rd2->pages[i] = -1;
Brian Norrisdadc17a2011-09-20 18:35:57 -07001004 rd2->version[i] = 0;
Brian Norris623978d2011-09-20 18:35:34 -07001005 i--;
1006 continue;
1007 }
1008 }
1009
1010 /* Scrub the flash table(s)? */
1011 if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
1012 writeops = 0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Brian Norrisdadc17a2011-09-20 18:35:57 -07001014 /* Update version numbers before writing */
1015 if (md) {
1016 td->version[i] = max(td->version[i], md->version[i]);
1017 md->version[i] = td->version[i];
1018 }
1019
Brian Norris8b6e50c2011-05-25 14:59:01 -07001020 /* Write the bad block table to the device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
Boris Brezillon08136212018-11-11 08:55:03 +01001022 res = write_bbt(this, buf, td, md, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (res < 0)
1024 return res;
1025 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001026
Brian Norris8b6e50c2011-05-25 14:59:01 -07001027 /* Write the mirror bad block table to the device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
Boris Brezillon08136212018-11-11 08:55:03 +01001029 res = write_bbt(this, buf, md, td, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (res < 0)
1031 return res;
1032 }
1033 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001034 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
1037/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001038 * mark_bbt_regions - [GENERIC] mark the bad block table regions
Boris Brezillon08136212018-11-11 08:55:03 +01001039 * @this: the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07001040 * @td: bad block table descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001042 * The bad block table regions are marked as "bad" to prevent accidental
1043 * erasures / writes. The regions are identified by the mark 0x02.
1044 */
Boris Brezillon08136212018-11-11 08:55:03 +01001045static void mark_bbt_region(struct nand_chip *this, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Boris Brezillon08136212018-11-11 08:55:03 +01001047 struct mtd_info *mtd = nand_to_mtd(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 int i, j, chips, block, nrblocks, update;
Brian Norris771c5682013-07-30 17:52:55 -07001049 uint8_t oldval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Brian Norris8b6e50c2011-05-25 14:59:01 -07001051 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (td->options & NAND_BBT_PERCHIP) {
1053 chips = this->numchips;
1054 nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
1055 } else {
1056 chips = 1;
1057 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001058 }
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 for (i = 0; i < chips; i++) {
1061 if ((td->options & NAND_BBT_ABSPAGE) ||
1062 !(td->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001063 if (td->pages[i] == -1)
1064 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
Brian Norrisb4d20d62013-07-30 17:52:56 -07001066 oldval = bbt_get_entry(this, block);
1067 bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
Brian Norris771c5682013-07-30 17:52:55 -07001068 if ((oldval != BBT_BLOCK_RESERVED) &&
1069 td->reserved_block_code)
Boris Brezillon08136212018-11-11 08:55:03 +01001070 nand_update_bbt(this, (loff_t)block <<
Brian Norrisb4d20d62013-07-30 17:52:56 -07001071 this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 continue;
1073 }
1074 update = 0;
1075 if (td->options & NAND_BBT_LASTBLOCK)
1076 block = ((i + 1) * nrblocks) - td->maxblocks;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001077 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 block = i * nrblocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 for (j = 0; j < td->maxblocks; j++) {
Brian Norrisb4d20d62013-07-30 17:52:56 -07001080 oldval = bbt_get_entry(this, block);
1081 bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
Brian Norris771c5682013-07-30 17:52:55 -07001082 if (oldval != BBT_BLOCK_RESERVED)
David Woodhousee0c7d762006-05-13 18:07:53 +01001083 update = 1;
Brian Norrisb4d20d62013-07-30 17:52:56 -07001084 block++;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001085 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07001086 /*
1087 * If we want reserved blocks to be recorded to flash, and some
1088 * new ones have been marked, then we need to update the stored
1089 * bbts. This should only happen once.
1090 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (update && td->reserved_block_code)
Boris Brezillon08136212018-11-11 08:55:03 +01001092 nand_update_bbt(this, (loff_t)(block - 1) <<
Brian Norrisb4d20d62013-07-30 17:52:56 -07001093 this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095}
1096
1097/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001098 * verify_bbt_descr - verify the bad block description
Boris Brezillon08136212018-11-11 08:55:03 +01001099 * @this: the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07001100 * @bd: the table to verify
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001101 *
1102 * This functions performs a few sanity checks on the bad block description
1103 * table.
1104 */
Boris Brezillon08136212018-11-11 08:55:03 +01001105static void verify_bbt_descr(struct nand_chip *this, struct nand_bbt_descr *bd)
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001106{
Boris Brezillon08136212018-11-11 08:55:03 +01001107 struct mtd_info *mtd = nand_to_mtd(this);
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001108 u32 pattern_len;
1109 u32 bits;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001110 u32 table_size;
1111
1112 if (!bd)
1113 return;
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001114
1115 pattern_len = bd->len;
1116 bits = bd->options & NAND_BBT_NRBITS_MSK;
1117
Brian Norrisa40f7342011-05-31 16:31:22 -07001118 BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001119 !(this->bbt_options & NAND_BBT_USE_FLASH));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001120 BUG_ON(!bits);
1121
1122 if (bd->options & NAND_BBT_VERSION)
1123 pattern_len++;
1124
1125 if (bd->options & NAND_BBT_NO_OOB) {
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001126 BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
Brian Norrisa40f7342011-05-31 16:31:22 -07001127 BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001128 BUG_ON(bd->offs);
1129 if (bd->options & NAND_BBT_VERSION)
1130 BUG_ON(bd->veroffs != bd->len);
1131 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1132 }
1133
1134 if (bd->options & NAND_BBT_PERCHIP)
1135 table_size = this->chipsize >> this->bbt_erase_shift;
1136 else
1137 table_size = mtd->size >> this->bbt_erase_shift;
1138 table_size >>= 3;
1139 table_size *= bits;
1140 if (bd->options & NAND_BBT_NO_OOB)
1141 table_size += pattern_len;
1142 BUG_ON(table_size > (1 << this->bbt_erase_shift));
1143}
1144
1145/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
Boris Brezillon08136212018-11-11 08:55:03 +01001147 * @this: the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07001148 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001150 * The function checks, if a bad block table(s) is/are already available. If
1151 * not it scans the device for manufacturer marked good / bad blocks and writes
1152 * the bad block table(s) to the selected place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001154 * The bad block table memory is allocated here. It must be freed by calling
1155 * the nand_free_bbt function.
1156 */
Boris Brezillon08136212018-11-11 08:55:03 +01001157static int nand_scan_bbt(struct nand_chip *this, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Boris Brezillon08136212018-11-11 08:55:03 +01001159 struct mtd_info *mtd = nand_to_mtd(this);
Brian Norris83c59542015-02-28 02:13:12 -08001160 int len, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 uint8_t *buf;
1162 struct nand_bbt_descr *td = this->bbt_td;
1163 struct nand_bbt_descr *md = this->bbt_md;
1164
Sheng Yong192db1c2015-07-31 01:12:44 +00001165 len = (mtd->size >> (this->bbt_erase_shift + 2)) ? : 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07001166 /*
1167 * Allocate memory (2bit per block) and clear the memory bad block
1168 * table.
1169 */
Burman Yan95b93a02006-11-15 21:10:29 +02001170 this->bbt = kzalloc(len, GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001171 if (!this->bbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Brian Norris8b6e50c2011-05-25 14:59:01 -07001174 /*
1175 * If no primary table decriptor is given, scan the device to build a
1176 * memory based bad block table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 */
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001178 if (!td) {
Boris Brezillon08136212018-11-11 08:55:03 +01001179 if ((res = nand_memory_bbt(this, bd))) {
Brian Norrisd0370212011-07-19 10:06:08 -07001180 pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
Brian Norris83c59542015-02-28 02:13:12 -08001181 goto err;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001182 }
Brian Norris83c59542015-02-28 02:13:12 -08001183 return 0;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001184 }
Boris Brezillon08136212018-11-11 08:55:03 +01001185 verify_bbt_descr(this, td);
1186 verify_bbt_descr(this, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 /* Allocate a temporary buffer for one eraseblock incl. oob */
1189 len = (1 << this->bbt_erase_shift);
1190 len += (len >> this->page_shift) * mtd->oobsize;
David Woodhousec3f8abf2006-05-13 04:03:42 +01001191 buf = vmalloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (!buf) {
Brian Norris83c59542015-02-28 02:13:12 -08001193 res = -ENOMEM;
1194 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001196
Brian Norris8b6e50c2011-05-25 14:59:01 -07001197 /* Is the bbt at a given page? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (td->options & NAND_BBT_ABSPAGE) {
Boris Brezillon08136212018-11-11 08:55:03 +01001199 read_abs_bbts(this, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001200 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 /* Search the bad block table using a pattern in oob */
Boris Brezillon08136212018-11-11 08:55:03 +01001202 search_read_bbts(this, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Boris Brezillon08136212018-11-11 08:55:03 +01001205 res = check_create(this, buf, bd);
Brian Norris83c59542015-02-28 02:13:12 -08001206 if (res)
1207 goto err;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 /* Prevent the bbt regions from erasing / writing */
Boris Brezillon08136212018-11-11 08:55:03 +01001210 mark_bbt_region(this, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (md)
Boris Brezillon08136212018-11-11 08:55:03 +01001212 mark_bbt_region(this, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001213
David Woodhousee0c7d762006-05-13 18:07:53 +01001214 vfree(buf);
Brian Norris83c59542015-02-28 02:13:12 -08001215 return 0;
1216
1217err:
1218 kfree(this->bbt);
1219 this->bbt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return res;
1221}
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223/**
Brian Norrisb32843b2013-07-30 17:52:59 -07001224 * nand_update_bbt - update bad block table(s)
Boris Brezillon08136212018-11-11 08:55:03 +01001225 * @this: the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07001226 * @offs: the offset of the newly marked block
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001228 * The function updates the bad block table(s).
1229 */
Boris Brezillon08136212018-11-11 08:55:03 +01001230static int nand_update_bbt(struct nand_chip *this, loff_t offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Boris Brezillon08136212018-11-11 08:55:03 +01001232 struct mtd_info *mtd = nand_to_mtd(this);
Brian Norris1196ac52011-09-07 13:13:32 -07001233 int len, res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 int chip, chipsel;
1235 uint8_t *buf;
1236 struct nand_bbt_descr *td = this->bbt_td;
1237 struct nand_bbt_descr *md = this->bbt_md;
1238
1239 if (!this->bbt || !td)
1240 return -EINVAL;
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 /* 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 Woodhousee0c7d762006-05-13 18:07:53 +01001245 buf = kmalloc(len, GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001246 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return -ENOMEM;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001248
Brian Norris8b6e50c2011-05-25 14:59:01 -07001249 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (td->options & NAND_BBT_PERCHIP) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001251 chip = (int)(offs >> this->chip_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 chipsel = chip;
1253 } else {
1254 chip = 0;
1255 chipsel = -1;
1256 }
1257
1258 td->version[chip]++;
1259 if (md)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001260 md->version[chip]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Brian Norris8b6e50c2011-05-25 14:59:01 -07001262 /* Write the bad block table to the device? */
Brian Norris1196ac52011-09-07 13:13:32 -07001263 if (td->options & NAND_BBT_WRITE) {
Boris Brezillon08136212018-11-11 08:55:03 +01001264 res = write_bbt(this, buf, td, md, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (res < 0)
1266 goto out;
1267 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07001268 /* Write the mirror bad block table to the device? */
Brian Norris1196ac52011-09-07 13:13:32 -07001269 if (md && (md->options & NAND_BBT_WRITE)) {
Boris Brezillon08136212018-11-11 08:55:03 +01001270 res = write_bbt(this, buf, md, td, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
1272
David Woodhousee0c7d762006-05-13 18:07:53 +01001273 out:
1274 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return res;
1276}
1277
Brian Norris8b6e50c2011-05-25 14:59:01 -07001278/*
1279 * Define some generic bad / good block scan pattern which are used
1280 * while scanning a device for factory marked good / bad blocks.
1281 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1283
Brian Norris7854d3f2011-06-23 14:12:08 -07001284/* Generic flash bbt descriptors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1286static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1287
1288static struct nand_bbt_descr bbt_main_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001289 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1291 .offs = 8,
1292 .len = 4,
1293 .veroffs = 12,
Richard Genoud61de9da2012-09-11 15:50:54 +02001294 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 .pattern = bbt_pattern
1296};
1297
1298static struct nand_bbt_descr bbt_mirror_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001299 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1301 .offs = 8,
1302 .len = 4,
1303 .veroffs = 12,
Richard Genoud61de9da2012-09-11 15:50:54 +02001304 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 .pattern = mirror_pattern
1306};
1307
Brian Norris9fd6b372012-06-22 16:35:40 -07001308static struct nand_bbt_descr bbt_main_no_oob_descr = {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001309 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1310 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1311 | NAND_BBT_NO_OOB,
1312 .len = 4,
1313 .veroffs = 4,
Richard Genoud61de9da2012-09-11 15:50:54 +02001314 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001315 .pattern = bbt_pattern
1316};
1317
Brian Norris9fd6b372012-06-22 16:35:40 -07001318static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001319 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1320 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1321 | NAND_BBT_NO_OOB,
1322 .len = 4,
1323 .veroffs = 4,
Richard Genoud61de9da2012-09-11 15:50:54 +02001324 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001325 .pattern = mirror_pattern
1326};
1327
Brian Norris752ed6c2011-09-20 18:36:42 -07001328#define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
Brian Norris58373ff2010-07-15 12:15:44 -07001329/**
Brian Norris752ed6c2011-09-20 18:36:42 -07001330 * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001331 * @this: NAND chip to create descriptor for
Brian Norris58373ff2010-07-15 12:15:44 -07001332 *
1333 * This function allocates and initializes a nand_bbt_descr for BBM detection
Brian Norris752ed6c2011-09-20 18:36:42 -07001334 * based on the properties of @this. The new descriptor is stored in
Brian Norris58373ff2010-07-15 12:15:44 -07001335 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1336 * passed to this function.
Brian Norris58373ff2010-07-15 12:15:44 -07001337 */
Brian Norris752ed6c2011-09-20 18:36:42 -07001338static int nand_create_badblock_pattern(struct nand_chip *this)
Brian Norris58373ff2010-07-15 12:15:44 -07001339{
1340 struct nand_bbt_descr *bd;
1341 if (this->badblock_pattern) {
Brian Norris752ed6c2011-09-20 18:36:42 -07001342 pr_warn("Bad block pattern already allocated; not replacing\n");
Brian Norris58373ff2010-07-15 12:15:44 -07001343 return -EINVAL;
1344 }
1345 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001346 if (!bd)
Brian Norris58373ff2010-07-15 12:15:44 -07001347 return -ENOMEM;
Brian Norris752ed6c2011-09-20 18:36:42 -07001348 bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
Brian Norris58373ff2010-07-15 12:15:44 -07001349 bd->offs = this->badblockpos;
1350 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1351 bd->pattern = scan_ff_pattern;
1352 bd->options |= NAND_BBT_DYNAMICSTRUCT;
1353 this->badblock_pattern = bd;
1354 return 0;
1355}
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357/**
Boris Brezillon44b07b92018-07-05 12:27:30 +02001358 * nand_create_bbt - [NAND Interface] Select a default bad block table for the device
1359 * @this: NAND chip object
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001361 * This function selects the default bad block table support for the device and
1362 * calls the nand_scan_bbt function.
1363 */
Boris Brezillon44b07b92018-07-05 12:27:30 +02001364int nand_create_bbt(struct nand_chip *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Brian Norrisabb9cf72014-05-20 22:34:40 -07001366 int ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001367
Brian Norris8b6e50c2011-05-25 14:59:01 -07001368 /* Is a flash based bad block table requested? */
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001369 if (this->bbt_options & NAND_BBT_USE_FLASH) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001370 /* Use the default pattern descriptors */
1371 if (!this->bbt_td) {
Brian Norrisa40f7342011-05-31 16:31:22 -07001372 if (this->bbt_options & NAND_BBT_NO_OOB) {
Brian Norris9fd6b372012-06-22 16:35:40 -07001373 this->bbt_td = &bbt_main_no_oob_descr;
1374 this->bbt_md = &bbt_mirror_no_oob_descr;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001375 } else {
1376 this->bbt_td = &bbt_main_descr;
1377 this->bbt_md = &bbt_mirror_descr;
1378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 } else {
1381 this->bbt_td = NULL;
1382 this->bbt_md = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
Brian Norrisa2f812d2011-03-18 21:53:42 -07001384
Brian Norrisabb9cf72014-05-20 22:34:40 -07001385 if (!this->badblock_pattern) {
1386 ret = nand_create_badblock_pattern(this);
1387 if (ret)
1388 return ret;
1389 }
Brian Norrisa2f812d2011-03-18 21:53:42 -07001390
Boris Brezillon08136212018-11-11 08:55:03 +01001391 return nand_scan_bbt(this, this->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392}
Boris Brezillon44b07b92018-07-05 12:27:30 +02001393EXPORT_SYMBOL(nand_create_bbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395/**
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001396 * nand_isreserved_bbt - [NAND Interface] Check if a block is reserved
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001397 * @this: NAND chip object
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001398 * @offs: offset in the device
1399 */
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001400int nand_isreserved_bbt(struct nand_chip *this, loff_t offs)
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001401{
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001402 int block;
1403
1404 block = (int)(offs >> this->bbt_erase_shift);
1405 return bbt_get_entry(this, block) == BBT_BLOCK_RESERVED;
1406}
1407
1408/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001409 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001410 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07001411 * @offs: offset in the device
1412 * @allowbbt: allow access to bad block table region
1413 */
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001414int nand_isbad_bbt(struct nand_chip *this, loff_t offs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Brian Norris39dbb022013-07-30 17:52:57 -07001416 int block, res;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001417
Brian Norrisb4d20d62013-07-30 17:52:56 -07001418 block = (int)(offs >> this->bbt_erase_shift);
1419 res = bbt_get_entry(this, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02001421 pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1422 (unsigned int)offs, block, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Brian Norris39dbb022013-07-30 17:52:57 -07001424 switch (res) {
Brian Norris771c5682013-07-30 17:52:55 -07001425 case BBT_BLOCK_GOOD:
David Woodhousee0c7d762006-05-13 18:07:53 +01001426 return 0;
Brian Norris771c5682013-07-30 17:52:55 -07001427 case BBT_BLOCK_WORN:
David Woodhousee0c7d762006-05-13 18:07:53 +01001428 return 1;
Brian Norris771c5682013-07-30 17:52:55 -07001429 case BBT_BLOCK_RESERVED:
David Woodhousee0c7d762006-05-13 18:07:53 +01001430 return allowbbt ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
1432 return 1;
1433}
1434
Brian Norrisb32843b2013-07-30 17:52:59 -07001435/**
1436 * nand_markbad_bbt - [NAND Interface] Mark a block bad in the BBT
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001437 * @this: NAND chip object
Brian Norrisb32843b2013-07-30 17:52:59 -07001438 * @offs: offset of the bad block
1439 */
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001440int nand_markbad_bbt(struct nand_chip *this, loff_t offs)
Brian Norrisb32843b2013-07-30 17:52:59 -07001441{
Brian Norrisb32843b2013-07-30 17:52:59 -07001442 int block, ret = 0;
1443
1444 block = (int)(offs >> this->bbt_erase_shift);
1445
1446 /* Mark bad block in memory */
1447 bbt_mark_entry(this, block, BBT_BLOCK_WORN);
1448
1449 /* Update flash-based bad block table */
1450 if (this->bbt_options & NAND_BBT_USE_FLASH)
Boris Brezillon08136212018-11-11 08:55:03 +01001451 ret = nand_update_bbt(this, offs);
Brian Norrisb32843b2013-07-30 17:52:59 -07001452
1453 return ret;
1454}