blob: fd3c10216eda0bbf9ecf0f434f85bed7204df232 [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
80static inline uint8_t bbt_get_entry(struct nand_chip *chip, int block)
81{
82 uint8_t entry = chip->bbt[block >> BBT_ENTRY_SHIFT];
83 entry >>= (block & BBT_ENTRY_MASK) * 2;
84 return entry & BBT_ENTRY_MASK;
85}
86
87static inline void bbt_mark_entry(struct nand_chip *chip, int block,
88 uint8_t mark)
89{
90 uint8_t msk = (mark & BBT_ENTRY_MASK) << ((block & BBT_ENTRY_MASK) * 2);
91 chip->bbt[block >> BBT_ENTRY_SHIFT] |= msk;
92}
93
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020094static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
95{
Brian Norris718894a2012-06-22 16:35:43 -070096 if (memcmp(buf, td->pattern, td->len))
97 return -1;
98 return 0;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020099}
100
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000101/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * check_pattern - [GENERIC] check if a pattern is in the buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700103 * @buf: the buffer to search
104 * @len: the length of buffer to search
105 * @paglen: the pagelength
106 * @td: search pattern descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700108 * Check for a pattern at the given place. Used to search bad block tables and
Brian Norrisdad22562013-07-30 17:53:00 -0700109 * good / bad block identifiers.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700110 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100111static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200113 if (td->options & NAND_BBT_NO_OOB)
114 return check_pattern_no_oob(buf, td);
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 /* Compare the pattern */
Brian Norrisdad22562013-07-30 17:53:00 -0700117 if (memcmp(buf + paglen + td->offs, td->pattern, td->len))
Brian Norris75b66d82011-09-07 13:13:41 -0700118 return -1;
Brian Norris58373ff2010-07-15 12:15:44 -0700119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return 0;
121}
122
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000123/**
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100124 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700125 * @buf: the buffer to search
126 * @td: search pattern descriptor
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100127 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700128 * Check for a pattern at the given place. Used to search bad block tables and
129 * good / bad block identifiers. Same as check_pattern, but no optional empty
130 * check.
131 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100132static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100133{
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100134 /* Compare the pattern */
Brian Norris491ed062012-06-22 16:35:44 -0700135 if (memcmp(buf + td->offs, td->pattern, td->len))
136 return -1;
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100137 return 0;
138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200141 * add_marker_len - compute the length of the marker in data area
Brian Norris8b6e50c2011-05-25 14:59:01 -0700142 * @td: BBT descriptor used for computation
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200143 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700144 * The length will be 0 if the marker is located in OOB area.
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200145 */
146static u32 add_marker_len(struct nand_bbt_descr *td)
147{
148 u32 len;
149
150 if (!(td->options & NAND_BBT_NO_OOB))
151 return 0;
152
153 len = td->len;
154 if (td->options & NAND_BBT_VERSION)
155 len++;
156 return len;
157}
158
159/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 * read_bbt - [GENERIC] Read the bad block table starting from page
Randy Dunlap455e7b32019-01-27 18:21:42 -0800161 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700162 * @buf: temporary buffer
163 * @page: the starting page
164 * @num: the number of bbt descriptors to read
Brian Norris7854d3f2011-06-23 14:12:08 -0700165 * @td: the bbt describtion table
Brian Norrisb4d20d62013-07-30 17:52:56 -0700166 * @offs: block number offset in the table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 *
168 * Read the bad block table starting from page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 */
Boris Brezillon08136212018-11-11 08:55:03 +0100170static int read_bbt(struct nand_chip *this, uint8_t *buf, int page, int num,
171 struct nand_bbt_descr *td, int offs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Boris Brezillon08136212018-11-11 08:55:03 +0100173 struct mtd_info *mtd = nand_to_mtd(this);
Brian Norris167a8d52011-09-20 18:35:08 -0700174 int res, ret = 0, i, j, act = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 size_t retlen, len, totlen;
176 loff_t from;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200177 int bits = td->options & NAND_BBT_NRBITS_MSK;
Brian Norris596d7452011-09-07 13:13:33 -0700178 uint8_t msk = (uint8_t)((1 << bits) - 1);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200179 u32 marker_len;
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200180 int reserved_block_code = td->reserved_block_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 totlen = (num * bits) >> 3;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200183 marker_len = add_marker_len(td);
Brian Norris596d7452011-09-07 13:13:33 -0700184 from = ((loff_t)page) << this->page_shift;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 while (totlen) {
Brian Norris596d7452011-09-07 13:13:33 -0700187 len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200188 if (marker_len) {
189 /*
190 * In case the BBT marker is not in the OOB area it
191 * will be just in the first page.
192 */
193 len -= marker_len;
194 from += marker_len;
195 marker_len = 0;
196 }
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200197 res = mtd_read(mtd, from, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (res < 0) {
Brian Norris167a8d52011-09-20 18:35:08 -0700199 if (mtd_is_eccerr(res)) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200200 pr_info("nand_bbt: ECC error in BBT at 0x%012llx\n",
201 from & ~mtd->writesize);
Brian Norris167a8d52011-09-20 18:35:08 -0700202 return res;
203 } else if (mtd_is_bitflip(res)) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200204 pr_info("nand_bbt: corrected error in BBT at 0x%012llx\n",
205 from & ~mtd->writesize);
Brian Norris167a8d52011-09-20 18:35:08 -0700206 ret = res;
207 } else {
208 pr_info("nand_bbt: error reading BBT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return res;
210 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 /* Analyse data */
214 for (i = 0; i < len; i++) {
215 uint8_t dat = buf[i];
Brian Norrisb4d20d62013-07-30 17:52:56 -0700216 for (j = 0; j < 8; j += bits, act++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 uint8_t tmp = (dat >> j) & msk;
218 if (tmp == msk)
219 continue;
David Woodhousee0c7d762006-05-13 18:07:53 +0100220 if (reserved_block_code && (tmp == reserved_block_code)) {
Brian Norrisd0370212011-07-19 10:06:08 -0700221 pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
Brian Norrisb4d20d62013-07-30 17:52:56 -0700222 (loff_t)(offs + act) <<
223 this->bbt_erase_shift);
224 bbt_mark_entry(this, offs + act,
Brian Norris771c5682013-07-30 17:52:55 -0700225 BBT_BLOCK_RESERVED);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200226 mtd->ecc_stats.bbtblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 continue;
228 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700229 /*
230 * Leave it for now, if it's matured we can
Brian Norrisa0f50802011-07-19 10:06:06 -0700231 * move this message to pr_debug.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700232 */
Brian Norrisd0370212011-07-19 10:06:08 -0700233 pr_info("nand_read_bbt: bad block at 0x%012llx\n",
Brian Norrisb4d20d62013-07-30 17:52:56 -0700234 (loff_t)(offs + act) <<
235 this->bbt_erase_shift);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700236 /* Factory marked bad or worn out? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (tmp == 0)
Brian Norrisb4d20d62013-07-30 17:52:56 -0700238 bbt_mark_entry(this, offs + act,
Brian Norris771c5682013-07-30 17:52:55 -0700239 BBT_BLOCK_FACTORY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 else
Brian Norrisb4d20d62013-07-30 17:52:56 -0700241 bbt_mark_entry(this, offs + act,
Brian Norris771c5682013-07-30 17:52:55 -0700242 BBT_BLOCK_WORN);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200243 mtd->ecc_stats.badblocks++;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246 totlen -= len;
247 from += len;
248 }
Brian Norris167a8d52011-09-20 18:35:08 -0700249 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252/**
253 * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
Boris Brezillon08136212018-11-11 08:55:03 +0100254 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700255 * @buf: temporary buffer
256 * @td: descriptor for the bad block table
Brian Norris596d7452011-09-07 13:13:33 -0700257 * @chip: read the table for a specific chip, -1 read all chips; applies only if
Brian Norris8b6e50c2011-05-25 14:59:01 -0700258 * NAND_BBT_PERCHIP option is set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700260 * Read the bad block table for all chips starting at a given page. We assume
261 * that the bbt bits are in consecutive order.
262 */
Boris Brezillon08136212018-11-11 08:55:03 +0100263static int read_abs_bbt(struct nand_chip *this, uint8_t *buf,
264 struct nand_bbt_descr *td, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Boris Brezillon08136212018-11-11 08:55:03 +0100266 struct mtd_info *mtd = nand_to_mtd(this);
Boris Brezillon6c836d52018-10-29 11:22:16 +0100267 u64 targetsize = nanddev_target_size(&this->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 int res = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (td->options & NAND_BBT_PERCHIP) {
271 int offs = 0;
Boris Brezillon32813e22018-10-29 11:58:29 +0100272 for (i = 0; i < nanddev_ntargets(&this->base); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (chip == -1 || chip == i)
Boris Brezillon08136212018-11-11 08:55:03 +0100274 res = read_bbt(this, buf, td->pages[i],
Boris Brezillon6c836d52018-10-29 11:22:16 +0100275 targetsize >> this->bbt_erase_shift,
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200276 td, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (res)
278 return res;
Boris Brezillon6c836d52018-10-29 11:22:16 +0100279 offs += targetsize >> this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281 } else {
Boris Brezillon08136212018-11-11 08:55:03 +0100282 res = read_bbt(this, buf, td->pages[0],
Sebastian Andrzej Siewiordf5b4e32010-09-29 19:43:51 +0200283 mtd->size >> this->bbt_erase_shift, td, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (res)
285 return res;
286 }
287 return 0;
288}
289
Brian Norris8b6e50c2011-05-25 14:59:01 -0700290/* BBT marker is in the first page, no OOB */
Boris Brezillon08136212018-11-11 08:55:03 +0100291static int scan_read_data(struct nand_chip *this, uint8_t *buf, loff_t offs,
292 struct nand_bbt_descr *td)
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200293{
Boris Brezillon08136212018-11-11 08:55:03 +0100294 struct mtd_info *mtd = nand_to_mtd(this);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200295 size_t retlen;
296 size_t len;
297
298 len = td->len;
299 if (td->options & NAND_BBT_VERSION)
300 len++;
301
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200302 return mtd_read(mtd, offs, len, &retlen, buf);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200303}
304
Brian Norrisa7e68832012-06-22 16:35:45 -0700305/**
Brian Norrisaf69dcd2012-06-22 16:35:42 -0700306 * scan_read_oob - [GENERIC] Scan data+OOB region to buffer
Boris Brezillon08136212018-11-11 08:55:03 +0100307 * @this: NAND chip object
Brian Norrisa7e68832012-06-22 16:35:45 -0700308 * @buf: temporary buffer
309 * @offs: offset at which to scan
310 * @len: length of data region to read
311 *
312 * Scan read data from data+OOB. May traverse multiple pages, interleaving
313 * page,OOB,page,OOB,... in buf. Completes transfer and returns the "strongest"
314 * ECC condition (error or bitflip). May quit on the first (non-ECC) error.
315 */
Boris Brezillon08136212018-11-11 08:55:03 +0100316static int scan_read_oob(struct nand_chip *this, uint8_t *buf, loff_t offs,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200317 size_t len)
318{
Boris Brezillon08136212018-11-11 08:55:03 +0100319 struct mtd_info *mtd = nand_to_mtd(this);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200320 struct mtd_oob_ops ops;
Brian Norrisa7e68832012-06-22 16:35:45 -0700321 int res, ret = 0;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200322
Brian Norrisa7e68832012-06-22 16:35:45 -0700323 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200324 ops.ooboffs = 0;
325 ops.ooblen = mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200326
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200327 while (len > 0) {
Brian Norris105513c2011-09-07 13:13:28 -0700328 ops.datbuf = buf;
329 ops.len = min(len, (size_t)mtd->writesize);
330 ops.oobbuf = buf + ops.len;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200331
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200332 res = mtd_read_oob(mtd, offs, &ops);
Brian Norrisa7e68832012-06-22 16:35:45 -0700333 if (res) {
334 if (!mtd_is_bitflip_or_eccerr(res))
335 return res;
336 else if (mtd_is_eccerr(res) || !ret)
337 ret = res;
338 }
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200339
340 buf += mtd->oobsize + mtd->writesize;
341 len -= mtd->writesize;
Dmitry Maluka34a57042012-05-11 20:51:51 +0300342 offs += mtd->writesize;
Maxim Levitskyb64d39d2010-02-22 20:39:37 +0200343 }
Brian Norrisa7e68832012-06-22 16:35:45 -0700344 return ret;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200345}
346
Boris Brezillon08136212018-11-11 08:55:03 +0100347static int scan_read(struct nand_chip *this, uint8_t *buf, loff_t offs,
348 size_t len, struct nand_bbt_descr *td)
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200349{
350 if (td->options & NAND_BBT_NO_OOB)
Boris Brezillon08136212018-11-11 08:55:03 +0100351 return scan_read_data(this, buf, offs, td);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200352 else
Boris Brezillon08136212018-11-11 08:55:03 +0100353 return scan_read_oob(this, buf, offs, len);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200354}
355
Brian Norris8b6e50c2011-05-25 14:59:01 -0700356/* Scan write data with oob to flash */
Boris Brezillon08136212018-11-11 08:55:03 +0100357static int scan_write_bbt(struct nand_chip *this, loff_t offs, size_t len,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200358 uint8_t *buf, uint8_t *oob)
359{
Boris Brezillon08136212018-11-11 08:55:03 +0100360 struct mtd_info *mtd = nand_to_mtd(this);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200361 struct mtd_oob_ops ops;
362
Brian Norris0612b9d2011-08-30 18:45:40 -0700363 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200364 ops.ooboffs = 0;
365 ops.ooblen = mtd->oobsize;
366 ops.datbuf = buf;
367 ops.oobbuf = oob;
368 ops.len = len;
369
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200370 return mtd_write_oob(mtd, offs, &ops);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200371}
372
Boris Brezillon08136212018-11-11 08:55:03 +0100373static u32 bbt_get_ver_offs(struct nand_chip *this, struct nand_bbt_descr *td)
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200374{
Boris Brezillon08136212018-11-11 08:55:03 +0100375 struct mtd_info *mtd = nand_to_mtd(this);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200376 u32 ver_offs = td->veroffs;
377
378 if (!(td->options & NAND_BBT_NO_OOB))
379 ver_offs += mtd->writesize;
380 return ver_offs;
381}
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383/**
384 * 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 +0100385 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700386 * @buf: temporary buffer
387 * @td: descriptor for the bad block table
388 * @md: descriptor for the bad block table mirror
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700390 * Read the bad block table(s) for all chips starting at a given page. We
391 * assume that the bbt bits are in consecutive order.
392 */
Boris Brezillon08136212018-11-11 08:55:03 +0100393static void read_abs_bbts(struct nand_chip *this, uint8_t *buf,
Brian Norris7b5a2d42012-06-22 16:35:41 -0700394 struct nand_bbt_descr *td, struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Boris Brezillon08136212018-11-11 08:55:03 +0100396 struct mtd_info *mtd = nand_to_mtd(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000398 /* Read the primary version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (td->options & NAND_BBT_VERSION) {
Boris Brezillon08136212018-11-11 08:55:03 +0100400 scan_read(this, buf, (loff_t)td->pages[0] << this->page_shift,
401 mtd->writesize, td);
402 td->version[0] = buf[bbt_get_ver_offs(this, td)];
Brian Norris9a4d4d62011-07-19 10:06:07 -0700403 pr_info("Bad block table at page %d, version 0x%02X\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700404 td->pages[0], td->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000407 /* Read the mirror version, if available */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (md && (md->options & NAND_BBT_VERSION)) {
Boris Brezillon08136212018-11-11 08:55:03 +0100409 scan_read(this, buf, (loff_t)md->pages[0] << this->page_shift,
410 mtd->writesize, md);
411 md->version[0] = buf[bbt_get_ver_offs(this, md)];
Brian Norris9a4d4d62011-07-19 10:06:07 -0700412 pr_info("Bad block table at page %d, version 0x%02X\n",
Brian Norrisd0370212011-07-19 10:06:08 -0700413 md->pages[0], md->version[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Brian Norris8b6e50c2011-05-25 14:59:01 -0700417/* Scan a given block partially */
Boris Brezillon08136212018-11-11 08:55:03 +0100418static int scan_block_fast(struct nand_chip *this, struct nand_bbt_descr *bd,
Frieder Schrempff90da782019-04-17 12:36:37 +0000419 loff_t offs, uint8_t *buf)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200420{
Boris Brezillon08136212018-11-11 08:55:03 +0100421 struct mtd_info *mtd = nand_to_mtd(this);
Frieder Schrempff90da782019-04-17 12:36:37 +0000422
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200423 struct mtd_oob_ops ops;
Frieder Schrempff90da782019-04-17 12:36:37 +0000424 int ret, page_offset;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200425
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
Frieder Schrempff90da782019-04-17 12:36:37 +0000432 page_offset = nand_bbm_get_next_page(this, 0);
433
434 while (page_offset >= 0) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200435 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700436 * Read the full oob until read_oob is fixed to handle single
437 * byte reads for 16 bit buswidth.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200438 */
Frieder Schrempff90da782019-04-17 12:36:37 +0000439 ret = mtd_read_oob(mtd, offs + (page_offset * mtd->writesize),
440 &ops);
Brian Norris903cd062011-06-28 16:28:58 -0700441 /* Ignore ECC errors when checking for BBM */
Brian Norrisd57f40542011-09-20 18:34:25 -0700442 if (ret && !mtd_is_bitflip_or_eccerr(ret))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200443 return ret;
444
445 if (check_short_pattern(buf, bd))
446 return 1;
447
Frieder Schrempff90da782019-04-17 12:36:37 +0000448 page_offset = nand_bbm_get_next_page(this, page_offset + 1);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200449 }
Frieder Schrempff90da782019-04-17 12:36:37 +0000450
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200451 return 0;
452}
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454/**
455 * create_bbt - [GENERIC] Create a bad block table by scanning the device
Boris Brezillon08136212018-11-11 08:55:03 +0100456 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700457 * @buf: temporary buffer
458 * @bd: descriptor for the good/bad block search pattern
459 * @chip: create the table for a specific chip, -1 read all chips; applies only
460 * if NAND_BBT_PERCHIP option is set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700462 * Create a bad block table by scanning the device for the given good/bad block
463 * identify pattern.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 */
Boris Brezillon08136212018-11-11 08:55:03 +0100465static int create_bbt(struct nand_chip *this, uint8_t *buf,
466 struct nand_bbt_descr *bd, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Boris Brezillon6c836d52018-10-29 11:22:16 +0100468 u64 targetsize = nanddev_target_size(&this->base);
Boris Brezillon08136212018-11-11 08:55:03 +0100469 struct mtd_info *mtd = nand_to_mtd(this);
Frieder Schrempff90da782019-04-17 12:36:37 +0000470 int i, numblocks, startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 loff_t from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Brian Norris9a4d4d62011-07-19 10:06:07 -0700473 pr_info("Scanning device for bad blocks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (chip == -1) {
Brian Norrisb4d20d62013-07-30 17:52:56 -0700476 numblocks = mtd->size >> this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 startblock = 0;
478 from = 0;
479 } else {
Boris Brezillon32813e22018-10-29 11:58:29 +0100480 if (chip >= nanddev_ntargets(&this->base)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -0700481 pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
Boris Brezillon32813e22018-10-29 11:58:29 +0100482 chip + 1, nanddev_ntargets(&this->base));
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000483 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
Boris Brezillon6c836d52018-10-29 11:22:16 +0100485 numblocks = targetsize >> this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 startblock = chip * numblocks;
487 numblocks += startblock;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700488 from = (loff_t)startblock << this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000490
Brian Norrisb4d20d62013-07-30 17:52:56 -0700491 for (i = startblock; i < numblocks; i++) {
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000492 int ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000493
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200494 BUG_ON(bd->options & NAND_BBT_NO_OOB);
495
Frieder Schrempff90da782019-04-17 12:36:37 +0000496 ret = scan_block_fast(this, bd, from, buf);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200497 if (ret < 0)
498 return ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000499
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200500 if (ret) {
Brian Norrisb4d20d62013-07-30 17:52:56 -0700501 bbt_mark_entry(this, i, BBT_BLOCK_FACTORY_BAD);
Brian Norris9a4d4d62011-07-19 10:06:07 -0700502 pr_warn("Bad eraseblock %d at 0x%012llx\n",
Brian Norrisb4d20d62013-07-30 17:52:56 -0700503 i, (unsigned long long)from);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200504 mtd->ecc_stats.badblocks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 from += (1 << this->bbt_erase_shift);
508 }
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +0000509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
512/**
513 * search_bbt - [GENERIC] scan the device for a specific bad block table
Boris Brezillon08136212018-11-11 08:55:03 +0100514 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700515 * @buf: temporary buffer
516 * @td: descriptor for the bad block table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700518 * Read the bad block table by searching for a given ident pattern. Search is
519 * preformed either from the beginning up or from the end of the device
520 * downwards. The search starts always at the start of a block. If the option
521 * NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains
522 * the bad block information of this chip. This is necessary to provide support
523 * for certain DOC devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700525 * The bbt ident pattern resides in the oob area of the first page in a block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 */
Boris Brezillon08136212018-11-11 08:55:03 +0100527static int search_bbt(struct nand_chip *this, uint8_t *buf,
528 struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Boris Brezillon6c836d52018-10-29 11:22:16 +0100530 u64 targetsize = nanddev_target_size(&this->base);
Boris Brezillon08136212018-11-11 08:55:03 +0100531 struct mtd_info *mtd = nand_to_mtd(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 int i, chips;
Brian Norris930de532014-01-02 17:14:10 -0800533 int startblock, block, dir;
Joern Engel28318772006-05-22 23:18:05 +0200534 int scanlen = mtd->writesize + mtd->oobsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 int bbtblocks;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200536 int blocktopage = this->bbt_erase_shift - this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Brian Norris8b6e50c2011-05-25 14:59:01 -0700538 /* Search direction top -> down? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (td->options & NAND_BBT_LASTBLOCK) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100540 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 dir = -1;
542 } else {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000543 startblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000545 }
546
Brian Norris8b6e50c2011-05-25 14:59:01 -0700547 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (td->options & NAND_BBT_PERCHIP) {
Boris Brezillon32813e22018-10-29 11:58:29 +0100549 chips = nanddev_ntargets(&this->base);
Boris Brezillon6c836d52018-10-29 11:22:16 +0100550 bbtblocks = targetsize >> this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 startblock &= bbtblocks - 1;
552 } else {
553 chips = 1;
554 bbtblocks = mtd->size >> this->bbt_erase_shift;
555 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 for (i = 0; i < chips; i++) {
558 /* Reset version information */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000559 td->version[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 td->pages[i] = -1;
561 /* Scan the maximum number of blocks */
562 for (block = 0; block < td->maxblocks; block++) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 int actblock = startblock + dir * block;
Adrian Hunter69423d92008-12-10 13:37:21 +0000565 loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /* Read first page */
Boris Brezillon08136212018-11-11 08:55:03 +0100568 scan_read(this, buf, offs, mtd->writesize, td);
Joern Engel28318772006-05-22 23:18:05 +0200569 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200570 td->pages[i] = actblock << blocktopage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (td->options & NAND_BBT_VERSION) {
Boris Brezillon08136212018-11-11 08:55:03 +0100572 offs = bbt_get_ver_offs(this, td);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200573 td->version[i] = buf[offs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575 break;
576 }
577 }
Boris Brezillon6c836d52018-10-29 11:22:16 +0100578 startblock += targetsize >> this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580 /* Check, if we found a bbt for each requested chip */
581 for (i = 0; i < chips; i++) {
582 if (td->pages[i] == -1)
Brian Norris9a4d4d62011-07-19 10:06:07 -0700583 pr_warn("Bad block table not found for chip %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 else
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200585 pr_info("Bad block table found at page %d, version 0x%02X\n",
586 td->pages[i], td->version[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
591/**
592 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
Boris Brezillon08136212018-11-11 08:55:03 +0100593 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700594 * @buf: temporary buffer
595 * @td: descriptor for the bad block table
596 * @md: descriptor for the bad block table mirror
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700598 * Search and read the bad block table(s).
599 */
Boris Brezillon08136212018-11-11 08:55:03 +0100600static void search_read_bbts(struct nand_chip *this, uint8_t *buf,
Brian Norris7b5a2d42012-06-22 16:35:41 -0700601 struct nand_bbt_descr *td,
602 struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 /* Search the primary table */
Boris Brezillon08136212018-11-11 08:55:03 +0100605 search_bbt(this, buf, td);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* Search the mirror table */
608 if (md)
Boris Brezillon08136212018-11-11 08:55:03 +0100609 search_bbt(this, buf, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000610}
611
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000612/**
Boris Brezillonc3baf272016-08-15 18:22:01 -0500613 * get_bbt_block - Get the first valid eraseblock suitable to store a BBT
614 * @this: the NAND device
615 * @td: the BBT description
616 * @md: the mirror BBT descriptor
617 * @chip: the CHIP selector
618 *
619 * This functions returns a positive block number pointing a valid eraseblock
620 * suitable to store a BBT (i.e. in the range reserved for BBT), or -ENOSPC if
621 * all blocks are already used of marked bad. If td->pages[chip] was already
622 * pointing to a valid block we re-use it, otherwise we search for the next
623 * valid one.
624 */
625static int get_bbt_block(struct nand_chip *this, struct nand_bbt_descr *td,
626 struct nand_bbt_descr *md, int chip)
627{
Boris Brezillon6c836d52018-10-29 11:22:16 +0100628 u64 targetsize = nanddev_target_size(&this->base);
Boris Brezillonc3baf272016-08-15 18:22:01 -0500629 int startblock, dir, page, numblocks, i;
630
631 /*
632 * There was already a version of the table, reuse the page. This
633 * applies for absolute placement too, as we have the page number in
634 * td->pages.
635 */
636 if (td->pages[chip] != -1)
637 return td->pages[chip] >>
638 (this->bbt_erase_shift - this->page_shift);
639
Boris Brezillon6c836d52018-10-29 11:22:16 +0100640 numblocks = (int)(targetsize >> this->bbt_erase_shift);
Boris Brezillonc3baf272016-08-15 18:22:01 -0500641 if (!(td->options & NAND_BBT_PERCHIP))
Boris Brezillon32813e22018-10-29 11:58:29 +0100642 numblocks *= nanddev_ntargets(&this->base);
Boris Brezillonc3baf272016-08-15 18:22:01 -0500643
644 /*
645 * Automatic placement of the bad block table. Search direction
646 * top -> down?
647 */
648 if (td->options & NAND_BBT_LASTBLOCK) {
649 startblock = numblocks * (chip + 1) - 1;
650 dir = -1;
651 } else {
652 startblock = chip * numblocks;
653 dir = 1;
654 }
655
656 for (i = 0; i < td->maxblocks; i++) {
657 int block = startblock + dir * i;
658
659 /* Check, if the block is bad */
660 switch (bbt_get_entry(this, block)) {
661 case BBT_BLOCK_WORN:
662 case BBT_BLOCK_FACTORY_BAD:
663 continue;
664 }
665
666 page = block << (this->bbt_erase_shift - this->page_shift);
667
668 /* Check, if the block is used by the mirror table */
669 if (!md || md->pages[chip] != page)
670 return block;
671 }
672
673 return -ENOSPC;
674}
675
676/**
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500677 * mark_bbt_block_bad - Mark one of the block reserved for BBT bad
678 * @this: the NAND device
679 * @td: the BBT description
680 * @chip: the CHIP selector
681 * @block: the BBT block to mark
682 *
683 * Blocks reserved for BBT can become bad. This functions is an helper to mark
684 * such blocks as bad. It takes care of updating the in-memory BBT, marking the
685 * block as bad using a bad block marker and invalidating the associated
686 * td->pages[] entry.
687 */
688static void mark_bbt_block_bad(struct nand_chip *this,
689 struct nand_bbt_descr *td,
690 int chip, int block)
691{
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500692 loff_t to;
693 int res;
694
695 bbt_mark_entry(this, block, BBT_BLOCK_WORN);
696
697 to = (loff_t)block << this->bbt_erase_shift;
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200698 res = nand_markbad_bbm(this, to);
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500699 if (res)
700 pr_warn("nand_bbt: error %d while marking block %d bad\n",
701 res, block);
702
703 td->pages[chip] = -1;
704}
705
706/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 * write_bbt - [GENERIC] (Re)write the bad block table
Boris Brezillon08136212018-11-11 08:55:03 +0100708 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700709 * @buf: temporary buffer
710 * @td: descriptor for the bad block table
711 * @md: descriptor for the bad block table mirror
712 * @chipsel: selector for a specific chip, -1 for all
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700714 * (Re)write the bad block table.
715 */
Boris Brezillon08136212018-11-11 08:55:03 +0100716static int write_bbt(struct nand_chip *this, uint8_t *buf,
Thomas Gleixner9223a452006-05-23 17:21:03 +0200717 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
718 int chipsel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Boris Brezillon6c836d52018-10-29 11:22:16 +0100720 u64 targetsize = nanddev_target_size(&this->base);
Boris Brezillon08136212018-11-11 08:55:03 +0100721 struct mtd_info *mtd = nand_to_mtd(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 struct erase_info einfo;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700723 int i, res, chip = 0;
Boris Brezillonc3baf272016-08-15 18:22:01 -0500724 int bits, page, offs, numblocks, sft, sftmsk;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700725 int nrchips, pageoffs, ooboffs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 uint8_t msk[4];
727 uint8_t rcode = td->reserved_block_code;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200728 size_t retlen, len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 loff_t to;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200730 struct mtd_oob_ops ops;
731
732 ops.ooblen = mtd->oobsize;
733 ops.ooboffs = 0;
734 ops.datbuf = NULL;
Brian Norris0612b9d2011-08-30 18:45:40 -0700735 ops.mode = MTD_OPS_PLACE_OOB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 if (!rcode)
738 rcode = 0xff;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700739 /* Write bad block table per chip rather than per device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (td->options & NAND_BBT_PERCHIP) {
Boris Brezillon6c836d52018-10-29 11:22:16 +0100741 numblocks = (int)(targetsize >> this->bbt_erase_shift);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700742 /* Full device write or specific chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (chipsel == -1) {
Boris Brezillon32813e22018-10-29 11:58:29 +0100744 nrchips = nanddev_ntargets(&this->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 } else {
746 nrchips = chipsel + 1;
747 chip = chipsel;
748 }
749 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +0100750 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 nrchips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000752 }
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 /* Loop through the chips */
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500755 while (chip < nrchips) {
Boris Brezillonc3baf272016-08-15 18:22:01 -0500756 int block;
757
758 block = get_bbt_block(this, td, md, chip);
759 if (block < 0) {
760 pr_err("No space left to write bad block table\n");
761 res = block;
762 goto outerr;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Brian Norris8b6e50c2011-05-25 14:59:01 -0700765 /*
Boris Brezillonc3baf272016-08-15 18:22:01 -0500766 * get_bbt_block() returns a block number, shift the value to
767 * get a page number.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700768 */
Boris Brezillonc3baf272016-08-15 18:22:01 -0500769 page = block << (this->bbt_erase_shift - this->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 /* Set up shift count and masks for the flash table */
772 bits = td->options & NAND_BBT_NRBITS_MSK;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200773 msk[2] = ~rcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 switch (bits) {
Thomas Gleixner9223a452006-05-23 17:21:03 +0200775 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
776 msk[3] = 0x01;
777 break;
778 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
779 msk[3] = 0x03;
780 break;
781 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
782 msk[3] = 0x0f;
783 break;
784 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
785 msk[3] = 0xff;
786 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 default: return -EINVAL;
788 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000789
Brian Norris596d7452011-09-07 13:13:33 -0700790 to = ((loff_t)page) << this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Brian Norris8b6e50c2011-05-25 14:59:01 -0700792 /* Must we save the block contents? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (td->options & NAND_BBT_SAVECONTENT) {
794 /* Make it block aligned */
Brian Norrisf5cd2ae2015-02-28 02:13:13 -0800795 to &= ~(((loff_t)1 << this->bbt_erase_shift) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 len = 1 << this->bbt_erase_shift;
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200797 res = mtd_read(mtd, to, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (res < 0) {
799 if (retlen != len) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200800 pr_info("nand_bbt: error reading block for writing the bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return res;
802 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200803 pr_warn("nand_bbt: ECC error while reading block for writing bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200805 /* Read oob data */
Vitaly Wool70145682006-11-03 18:20:38 +0300806 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200807 ops.oobbuf = &buf[len];
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200808 res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
Vitaly Wool70145682006-11-03 18:20:38 +0300809 if (res < 0 || ops.oobretlen != ops.ooblen)
Thomas Gleixner9223a452006-05-23 17:21:03 +0200810 goto outerr;
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 /* Calc the byte offset in the buffer */
813 pageoffs = page - (int)(to >> this->page_shift);
814 offs = pageoffs << this->page_shift;
815 /* Preset the bbt area with 0xff */
Brian Norris596d7452011-09-07 13:13:33 -0700816 memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
Thomas Gleixner9223a452006-05-23 17:21:03 +0200817 ooboffs = len + (pageoffs * mtd->oobsize);
818
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200819 } else if (td->options & NAND_BBT_NO_OOB) {
820 ooboffs = 0;
821 offs = td->len;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700822 /* The version byte */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200823 if (td->options & NAND_BBT_VERSION)
824 offs++;
825 /* Calc length */
Brian Norris596d7452011-09-07 13:13:33 -0700826 len = (size_t)(numblocks >> sft);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200827 len += offs;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700828 /* Make it page aligned! */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200829 len = ALIGN(len, mtd->writesize);
830 /* Preset the buffer with 0xff */
831 memset(buf, 0xff, len);
832 /* Pattern is located at the begin of first page */
833 memcpy(buf, td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 } else {
835 /* Calc length */
Brian Norris596d7452011-09-07 13:13:33 -0700836 len = (size_t)(numblocks >> sft);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700837 /* Make it page aligned! */
Sebastian Andrzej Siewiorcda32092010-09-29 19:43:50 +0200838 len = ALIGN(len, mtd->writesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 /* Preset the buffer with 0xff */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200840 memset(buf, 0xff, len +
841 (len >> this->page_shift)* mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 offs = 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200843 ooboffs = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /* Pattern is located in oob area of first page */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200845 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000847
Thomas Gleixner9223a452006-05-23 17:21:03 +0200848 if (td->options & NAND_BBT_VERSION)
849 buf[ooboffs + td->veroffs] = td->version[chip];
850
Brian Norris8b6e50c2011-05-25 14:59:01 -0700851 /* Walk through the memory table */
Brian Norrisb4d20d62013-07-30 17:52:56 -0700852 for (i = 0; i < numblocks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 uint8_t dat;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700854 int sftcnt = (i << (3 - sft)) & sftmsk;
855 dat = bbt_get_entry(this, chip * numblocks + i);
856 /* Do not store the reserved bbt blocks! */
857 buf[offs + (i >> sft)] &= ~(msk[dat] << sftcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000859
David Woodhousee0c7d762006-05-13 18:07:53 +0100860 memset(&einfo, 0, sizeof(einfo));
Adrian Hunter69423d92008-12-10 13:37:21 +0000861 einfo.addr = to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 einfo.len = 1 << this->bbt_erase_shift;
Boris Brezillone4cdf9c2018-09-06 14:05:35 +0200863 res = nand_erase_nand(this, &einfo, 1);
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500864 if (res < 0) {
865 pr_warn("nand_bbt: error while erasing BBT block %d\n",
866 res);
867 mark_bbt_block_bad(this, td, chip, block);
868 continue;
869 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000870
Boris Brezillon08136212018-11-11 08:55:03 +0100871 res = scan_write_bbt(this, to, len, buf,
872 td->options & NAND_BBT_NO_OOB ?
873 NULL : &buf[len]);
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500874 if (res < 0) {
875 pr_warn("nand_bbt: error while writing BBT block %d\n",
876 res);
877 mark_bbt_block_bad(this, td, chip, block);
878 continue;
879 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200880
Brian Norrisd0370212011-07-19 10:06:08 -0700881 pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
882 (unsigned long long)to, td->version[chip]);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 /* Mark it as used */
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500885 td->pages[chip++] = page;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200888
889 outerr:
Brian Norrisd0370212011-07-19 10:06:08 -0700890 pr_warn("nand_bbt: error while writing bad block table %d\n", res);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200891 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
894/**
895 * nand_memory_bbt - [GENERIC] create a memory based bad block table
Boris Brezillon08136212018-11-11 08:55:03 +0100896 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700897 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700899 * The function creates a memory based bbt by scanning the device for
900 * manufacturer / software marked good / bad blocks.
901 */
Boris Brezillon08136212018-11-11 08:55:03 +0100902static inline int nand_memory_bbt(struct nand_chip *this,
903 struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Boris Brezilloneeab7172018-10-28 15:27:55 +0100905 u8 *pagebuf = nand_get_data_buf(this);
906
907 return create_bbt(this, pagebuf, bd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
910/**
David Woodhousee0c7d762006-05-13 18:07:53 +0100911 * check_create - [GENERIC] create and write bbt(s) if necessary
Boris Brezillon08136212018-11-11 08:55:03 +0100912 * @this: the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700913 * @buf: temporary buffer
914 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700916 * The function checks the results of the previous call to read_bbt and creates
917 * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found
918 * for the chip/device. Update is necessary if one of the tables is missing or
919 * the version nr. of one table is less than the other.
920 */
Boris Brezillon08136212018-11-11 08:55:03 +0100921static int check_create(struct nand_chip *this, uint8_t *buf,
922 struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Brian Norris623978d2011-09-20 18:35:34 -0700924 int i, chips, writeops, create, chipsel, res, res2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 struct nand_bbt_descr *td = this->bbt_td;
926 struct nand_bbt_descr *md = this->bbt_md;
927 struct nand_bbt_descr *rd, *rd2;
928
Brian Norris8b6e50c2011-05-25 14:59:01 -0700929 /* Do we have a bbt per chip? */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000930 if (td->options & NAND_BBT_PERCHIP)
Boris Brezillon32813e22018-10-29 11:58:29 +0100931 chips = nanddev_ntargets(&this->base);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000932 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 chips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 for (i = 0; i < chips; i++) {
936 writeops = 0;
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700937 create = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 rd = NULL;
939 rd2 = NULL;
Brian Norris623978d2011-09-20 18:35:34 -0700940 res = res2 = 0;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700941 /* Per chip or per device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700943 /* Mirrored table available? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 if (md) {
945 if (td->pages[i] == -1 && md->pages[i] == -1) {
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700946 create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 writeops = 0x03;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700948 } else if (td->pages[i] == -1) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000949 rd = md;
Brian Norris596d7452011-09-07 13:13:33 -0700950 writeops = 0x01;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700951 } else if (md->pages[i] == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 rd = td;
Brian Norris596d7452011-09-07 13:13:33 -0700953 writeops = 0x02;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700954 } else if (td->version[i] == md->version[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 rd = td;
956 if (!(td->options & NAND_BBT_VERSION))
957 rd2 = md;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700958 } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 rd = td;
Brian Norris596d7452011-09-07 13:13:33 -0700960 writeops = 0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 } else {
962 rd = md;
Brian Norris596d7452011-09-07 13:13:33 -0700963 writeops = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 } else {
966 if (td->pages[i] == -1) {
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700967 create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 writeops = 0x01;
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700969 } else {
970 rd = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000973
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700974 if (create) {
975 /* Create the bad block table by scanning the device? */
976 if (!(td->options & NAND_BBT_CREATE))
977 continue;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000978
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700979 /* Create the table in memory by scanning the chip(s) */
980 if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
Boris Brezillon08136212018-11-11 08:55:03 +0100981 create_bbt(this, buf, bd, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700983 td->version[i] = 1;
984 if (md)
985 md->version[i] = 1;
986 }
987
Brian Norris8b6e50c2011-05-25 14:59:01 -0700988 /* Read back first? */
Brian Norris623978d2011-09-20 18:35:34 -0700989 if (rd) {
Boris Brezillon08136212018-11-11 08:55:03 +0100990 res = read_abs_bbt(this, buf, rd, chipsel);
Brian Norris623978d2011-09-20 18:35:34 -0700991 if (mtd_is_eccerr(res)) {
992 /* Mark table as invalid */
993 rd->pages[i] = -1;
Brian Norrisdadc17a2011-09-20 18:35:57 -0700994 rd->version[i] = 0;
Brian Norris623978d2011-09-20 18:35:34 -0700995 i--;
996 continue;
997 }
998 }
Brian Norris8b6e50c2011-05-25 14:59:01 -0700999 /* If they weren't versioned, read both */
Brian Norris623978d2011-09-20 18:35:34 -07001000 if (rd2) {
Boris Brezillon08136212018-11-11 08:55:03 +01001001 res2 = read_abs_bbt(this, buf, rd2, chipsel);
Brian Norris623978d2011-09-20 18:35:34 -07001002 if (mtd_is_eccerr(res2)) {
1003 /* Mark table as invalid */
1004 rd2->pages[i] = -1;
Brian Norrisdadc17a2011-09-20 18:35:57 -07001005 rd2->version[i] = 0;
Brian Norris623978d2011-09-20 18:35:34 -07001006 i--;
1007 continue;
1008 }
1009 }
1010
1011 /* Scrub the flash table(s)? */
1012 if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
1013 writeops = 0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Brian Norrisdadc17a2011-09-20 18:35:57 -07001015 /* Update version numbers before writing */
1016 if (md) {
1017 td->version[i] = max(td->version[i], md->version[i]);
1018 md->version[i] = td->version[i];
1019 }
1020
Brian Norris8b6e50c2011-05-25 14:59:01 -07001021 /* Write the bad block table to the device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
Boris Brezillon08136212018-11-11 08:55:03 +01001023 res = write_bbt(this, buf, td, md, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (res < 0)
1025 return res;
1026 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001027
Brian Norris8b6e50c2011-05-25 14:59:01 -07001028 /* Write the mirror bad block table to the device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
Boris Brezillon08136212018-11-11 08:55:03 +01001030 res = write_bbt(this, buf, md, td, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (res < 0)
1032 return res;
1033 }
1034 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001035 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
1038/**
Boris Brezillon99f33512018-11-11 08:55:04 +01001039 * nand_update_bbt - update bad block table(s)
1040 * @this: the NAND device
1041 * @offs: the offset of the newly marked block
1042 *
1043 * The function updates the bad block table(s).
1044 */
1045static int nand_update_bbt(struct nand_chip *this, loff_t offs)
1046{
1047 struct mtd_info *mtd = nand_to_mtd(this);
1048 int len, res = 0;
1049 int chip, chipsel;
1050 uint8_t *buf;
1051 struct nand_bbt_descr *td = this->bbt_td;
1052 struct nand_bbt_descr *md = this->bbt_md;
1053
1054 if (!this->bbt || !td)
1055 return -EINVAL;
1056
1057 /* Allocate a temporary buffer for one eraseblock incl. oob */
1058 len = (1 << this->bbt_erase_shift);
1059 len += (len >> this->page_shift) * mtd->oobsize;
1060 buf = kmalloc(len, GFP_KERNEL);
1061 if (!buf)
1062 return -ENOMEM;
1063
1064 /* Do we have a bbt per chip? */
1065 if (td->options & NAND_BBT_PERCHIP) {
1066 chip = (int)(offs >> this->chip_shift);
1067 chipsel = chip;
1068 } else {
1069 chip = 0;
1070 chipsel = -1;
1071 }
1072
1073 td->version[chip]++;
1074 if (md)
1075 md->version[chip]++;
1076
1077 /* Write the bad block table to the device? */
1078 if (td->options & NAND_BBT_WRITE) {
1079 res = write_bbt(this, buf, td, md, chipsel);
1080 if (res < 0)
1081 goto out;
1082 }
1083 /* Write the mirror bad block table to the device? */
1084 if (md && (md->options & NAND_BBT_WRITE)) {
1085 res = write_bbt(this, buf, md, td, chipsel);
1086 }
1087
1088 out:
1089 kfree(buf);
1090 return res;
1091}
1092
1093/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001094 * mark_bbt_regions - [GENERIC] mark the bad block table regions
Boris Brezillon08136212018-11-11 08:55:03 +01001095 * @this: the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07001096 * @td: bad block table descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001098 * The bad block table regions are marked as "bad" to prevent accidental
1099 * erasures / writes. The regions are identified by the mark 0x02.
1100 */
Boris Brezillon08136212018-11-11 08:55:03 +01001101static void mark_bbt_region(struct nand_chip *this, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Boris Brezillon6c836d52018-10-29 11:22:16 +01001103 u64 targetsize = nanddev_target_size(&this->base);
Boris Brezillon08136212018-11-11 08:55:03 +01001104 struct mtd_info *mtd = nand_to_mtd(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 int i, j, chips, block, nrblocks, update;
Brian Norris771c5682013-07-30 17:52:55 -07001106 uint8_t oldval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Brian Norris8b6e50c2011-05-25 14:59:01 -07001108 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (td->options & NAND_BBT_PERCHIP) {
Boris Brezillon32813e22018-10-29 11:58:29 +01001110 chips = nanddev_ntargets(&this->base);
Boris Brezillon6c836d52018-10-29 11:22:16 +01001111 nrblocks = (int)(targetsize >> this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 } else {
1113 chips = 1;
1114 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001115 }
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 for (i = 0; i < chips; i++) {
1118 if ((td->options & NAND_BBT_ABSPAGE) ||
1119 !(td->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001120 if (td->pages[i] == -1)
1121 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
Brian Norrisb4d20d62013-07-30 17:52:56 -07001123 oldval = bbt_get_entry(this, block);
1124 bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
Brian Norris771c5682013-07-30 17:52:55 -07001125 if ((oldval != BBT_BLOCK_RESERVED) &&
1126 td->reserved_block_code)
Boris Brezillon08136212018-11-11 08:55:03 +01001127 nand_update_bbt(this, (loff_t)block <<
Brian Norrisb4d20d62013-07-30 17:52:56 -07001128 this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 continue;
1130 }
1131 update = 0;
1132 if (td->options & NAND_BBT_LASTBLOCK)
1133 block = ((i + 1) * nrblocks) - td->maxblocks;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001134 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 block = i * nrblocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 for (j = 0; j < td->maxblocks; j++) {
Brian Norrisb4d20d62013-07-30 17:52:56 -07001137 oldval = bbt_get_entry(this, block);
1138 bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
Brian Norris771c5682013-07-30 17:52:55 -07001139 if (oldval != BBT_BLOCK_RESERVED)
David Woodhousee0c7d762006-05-13 18:07:53 +01001140 update = 1;
Brian Norrisb4d20d62013-07-30 17:52:56 -07001141 block++;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001142 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07001143 /*
1144 * If we want reserved blocks to be recorded to flash, and some
1145 * new ones have been marked, then we need to update the stored
1146 * bbts. This should only happen once.
1147 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (update && td->reserved_block_code)
Boris Brezillon08136212018-11-11 08:55:03 +01001149 nand_update_bbt(this, (loff_t)(block - 1) <<
Brian Norrisb4d20d62013-07-30 17:52:56 -07001150 this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152}
1153
1154/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001155 * verify_bbt_descr - verify the bad block description
Boris Brezillon08136212018-11-11 08:55:03 +01001156 * @this: the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07001157 * @bd: the table to verify
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001158 *
1159 * This functions performs a few sanity checks on the bad block description
1160 * table.
1161 */
Boris Brezillon08136212018-11-11 08:55:03 +01001162static void verify_bbt_descr(struct nand_chip *this, struct nand_bbt_descr *bd)
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001163{
Boris Brezillon6c836d52018-10-29 11:22:16 +01001164 u64 targetsize = nanddev_target_size(&this->base);
Boris Brezillon08136212018-11-11 08:55:03 +01001165 struct mtd_info *mtd = nand_to_mtd(this);
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001166 u32 pattern_len;
1167 u32 bits;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001168 u32 table_size;
1169
1170 if (!bd)
1171 return;
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001172
1173 pattern_len = bd->len;
1174 bits = bd->options & NAND_BBT_NRBITS_MSK;
1175
Brian Norrisa40f7342011-05-31 16:31:22 -07001176 BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001177 !(this->bbt_options & NAND_BBT_USE_FLASH));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001178 BUG_ON(!bits);
1179
1180 if (bd->options & NAND_BBT_VERSION)
1181 pattern_len++;
1182
1183 if (bd->options & NAND_BBT_NO_OOB) {
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001184 BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
Brian Norrisa40f7342011-05-31 16:31:22 -07001185 BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001186 BUG_ON(bd->offs);
1187 if (bd->options & NAND_BBT_VERSION)
1188 BUG_ON(bd->veroffs != bd->len);
1189 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1190 }
1191
1192 if (bd->options & NAND_BBT_PERCHIP)
Boris Brezillon6c836d52018-10-29 11:22:16 +01001193 table_size = targetsize >> this->bbt_erase_shift;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001194 else
1195 table_size = mtd->size >> this->bbt_erase_shift;
1196 table_size >>= 3;
1197 table_size *= bits;
1198 if (bd->options & NAND_BBT_NO_OOB)
1199 table_size += pattern_len;
1200 BUG_ON(table_size > (1 << this->bbt_erase_shift));
1201}
1202
1203/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
Boris Brezillon08136212018-11-11 08:55:03 +01001205 * @this: the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07001206 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001208 * The function checks, if a bad block table(s) is/are already available. If
1209 * not it scans the device for manufacturer marked good / bad blocks and writes
1210 * the bad block table(s) to the selected place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001212 * The bad block table memory is allocated here. It must be freed by calling
1213 * the nand_free_bbt function.
1214 */
Boris Brezillon08136212018-11-11 08:55:03 +01001215static int nand_scan_bbt(struct nand_chip *this, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Boris Brezillon08136212018-11-11 08:55:03 +01001217 struct mtd_info *mtd = nand_to_mtd(this);
Brian Norris83c59542015-02-28 02:13:12 -08001218 int len, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 uint8_t *buf;
1220 struct nand_bbt_descr *td = this->bbt_td;
1221 struct nand_bbt_descr *md = this->bbt_md;
1222
Sheng Yong192db1c2015-07-31 01:12:44 +00001223 len = (mtd->size >> (this->bbt_erase_shift + 2)) ? : 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07001224 /*
1225 * Allocate memory (2bit per block) and clear the memory bad block
1226 * table.
1227 */
Burman Yan95b93a02006-11-15 21:10:29 +02001228 this->bbt = kzalloc(len, GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001229 if (!this->bbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Brian Norris8b6e50c2011-05-25 14:59:01 -07001232 /*
1233 * If no primary table decriptor is given, scan the device to build a
1234 * memory based bad block table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 */
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001236 if (!td) {
Boris Brezillon08136212018-11-11 08:55:03 +01001237 if ((res = nand_memory_bbt(this, bd))) {
Brian Norrisd0370212011-07-19 10:06:08 -07001238 pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
Brian Norris83c59542015-02-28 02:13:12 -08001239 goto err;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001240 }
Brian Norris83c59542015-02-28 02:13:12 -08001241 return 0;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001242 }
Boris Brezillon08136212018-11-11 08:55:03 +01001243 verify_bbt_descr(this, td);
1244 verify_bbt_descr(this, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 /* Allocate a temporary buffer for one eraseblock incl. oob */
1247 len = (1 << this->bbt_erase_shift);
1248 len += (len >> this->page_shift) * mtd->oobsize;
David Woodhousec3f8abf2006-05-13 04:03:42 +01001249 buf = vmalloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (!buf) {
Brian Norris83c59542015-02-28 02:13:12 -08001251 res = -ENOMEM;
1252 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001254
Brian Norris8b6e50c2011-05-25 14:59:01 -07001255 /* Is the bbt at a given page? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (td->options & NAND_BBT_ABSPAGE) {
Boris Brezillon08136212018-11-11 08:55:03 +01001257 read_abs_bbts(this, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001258 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 /* Search the bad block table using a pattern in oob */
Boris Brezillon08136212018-11-11 08:55:03 +01001260 search_read_bbts(this, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Boris Brezillon08136212018-11-11 08:55:03 +01001263 res = check_create(this, buf, bd);
Brian Norris83c59542015-02-28 02:13:12 -08001264 if (res)
1265 goto err;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 /* Prevent the bbt regions from erasing / writing */
Boris Brezillon08136212018-11-11 08:55:03 +01001268 mark_bbt_region(this, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (md)
Boris Brezillon08136212018-11-11 08:55:03 +01001270 mark_bbt_region(this, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001271
David Woodhousee0c7d762006-05-13 18:07:53 +01001272 vfree(buf);
Brian Norris83c59542015-02-28 02:13:12 -08001273 return 0;
1274
1275err:
1276 kfree(this->bbt);
1277 this->bbt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return res;
1279}
1280
Brian Norris8b6e50c2011-05-25 14:59:01 -07001281/*
1282 * Define some generic bad / good block scan pattern which are used
1283 * while scanning a device for factory marked good / bad blocks.
1284 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1286
Brian Norris7854d3f2011-06-23 14:12:08 -07001287/* Generic flash bbt descriptors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1289static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1290
1291static struct nand_bbt_descr bbt_main_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001292 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1294 .offs = 8,
1295 .len = 4,
1296 .veroffs = 12,
Richard Genoud61de9da2012-09-11 15:50:54 +02001297 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 .pattern = bbt_pattern
1299};
1300
1301static struct nand_bbt_descr bbt_mirror_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001302 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1304 .offs = 8,
1305 .len = 4,
1306 .veroffs = 12,
Richard Genoud61de9da2012-09-11 15:50:54 +02001307 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 .pattern = mirror_pattern
1309};
1310
Brian Norris9fd6b372012-06-22 16:35:40 -07001311static struct nand_bbt_descr bbt_main_no_oob_descr = {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001312 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1313 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1314 | NAND_BBT_NO_OOB,
1315 .len = 4,
1316 .veroffs = 4,
Richard Genoud61de9da2012-09-11 15:50:54 +02001317 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001318 .pattern = bbt_pattern
1319};
1320
Brian Norris9fd6b372012-06-22 16:35:40 -07001321static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001322 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1323 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1324 | NAND_BBT_NO_OOB,
1325 .len = 4,
1326 .veroffs = 4,
Richard Genoud61de9da2012-09-11 15:50:54 +02001327 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001328 .pattern = mirror_pattern
1329};
1330
Brian Norris752ed6c2011-09-20 18:36:42 -07001331#define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
Brian Norris58373ff2010-07-15 12:15:44 -07001332/**
Brian Norris752ed6c2011-09-20 18:36:42 -07001333 * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001334 * @this: NAND chip to create descriptor for
Brian Norris58373ff2010-07-15 12:15:44 -07001335 *
1336 * This function allocates and initializes a nand_bbt_descr for BBM detection
Brian Norris752ed6c2011-09-20 18:36:42 -07001337 * based on the properties of @this. The new descriptor is stored in
Brian Norris58373ff2010-07-15 12:15:44 -07001338 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1339 * passed to this function.
Brian Norris58373ff2010-07-15 12:15:44 -07001340 */
Brian Norris752ed6c2011-09-20 18:36:42 -07001341static int nand_create_badblock_pattern(struct nand_chip *this)
Brian Norris58373ff2010-07-15 12:15:44 -07001342{
1343 struct nand_bbt_descr *bd;
1344 if (this->badblock_pattern) {
Brian Norris752ed6c2011-09-20 18:36:42 -07001345 pr_warn("Bad block pattern already allocated; not replacing\n");
Brian Norris58373ff2010-07-15 12:15:44 -07001346 return -EINVAL;
1347 }
1348 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001349 if (!bd)
Brian Norris58373ff2010-07-15 12:15:44 -07001350 return -ENOMEM;
Brian Norris752ed6c2011-09-20 18:36:42 -07001351 bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
Brian Norris58373ff2010-07-15 12:15:44 -07001352 bd->offs = this->badblockpos;
1353 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1354 bd->pattern = scan_ff_pattern;
1355 bd->options |= NAND_BBT_DYNAMICSTRUCT;
1356 this->badblock_pattern = bd;
1357 return 0;
1358}
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360/**
Boris Brezillon44b07b92018-07-05 12:27:30 +02001361 * nand_create_bbt - [NAND Interface] Select a default bad block table for the device
1362 * @this: NAND chip object
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001364 * This function selects the default bad block table support for the device and
1365 * calls the nand_scan_bbt function.
1366 */
Boris Brezillon44b07b92018-07-05 12:27:30 +02001367int nand_create_bbt(struct nand_chip *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Brian Norrisabb9cf72014-05-20 22:34:40 -07001369 int ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001370
Brian Norris8b6e50c2011-05-25 14:59:01 -07001371 /* Is a flash based bad block table requested? */
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001372 if (this->bbt_options & NAND_BBT_USE_FLASH) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001373 /* Use the default pattern descriptors */
1374 if (!this->bbt_td) {
Brian Norrisa40f7342011-05-31 16:31:22 -07001375 if (this->bbt_options & NAND_BBT_NO_OOB) {
Brian Norris9fd6b372012-06-22 16:35:40 -07001376 this->bbt_td = &bbt_main_no_oob_descr;
1377 this->bbt_md = &bbt_mirror_no_oob_descr;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001378 } else {
1379 this->bbt_td = &bbt_main_descr;
1380 this->bbt_md = &bbt_mirror_descr;
1381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 } else {
1384 this->bbt_td = NULL;
1385 this->bbt_md = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
Brian Norrisa2f812d2011-03-18 21:53:42 -07001387
Brian Norrisabb9cf72014-05-20 22:34:40 -07001388 if (!this->badblock_pattern) {
1389 ret = nand_create_badblock_pattern(this);
1390 if (ret)
1391 return ret;
1392 }
Brian Norrisa2f812d2011-03-18 21:53:42 -07001393
Boris Brezillon08136212018-11-11 08:55:03 +01001394 return nand_scan_bbt(this, this->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
Boris Brezillon44b07b92018-07-05 12:27:30 +02001396EXPORT_SYMBOL(nand_create_bbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398/**
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001399 * nand_isreserved_bbt - [NAND Interface] Check if a block is reserved
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001400 * @this: NAND chip object
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001401 * @offs: offset in the device
1402 */
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001403int nand_isreserved_bbt(struct nand_chip *this, loff_t offs)
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001404{
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001405 int block;
1406
1407 block = (int)(offs >> this->bbt_erase_shift);
1408 return bbt_get_entry(this, block) == BBT_BLOCK_RESERVED;
1409}
1410
1411/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001412 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001413 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07001414 * @offs: offset in the device
1415 * @allowbbt: allow access to bad block table region
1416 */
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001417int nand_isbad_bbt(struct nand_chip *this, loff_t offs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Brian Norris39dbb022013-07-30 17:52:57 -07001419 int block, res;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001420
Brian Norrisb4d20d62013-07-30 17:52:56 -07001421 block = (int)(offs >> this->bbt_erase_shift);
1422 res = bbt_get_entry(this, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02001424 pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1425 (unsigned int)offs, block, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Brian Norris39dbb022013-07-30 17:52:57 -07001427 switch (res) {
Brian Norris771c5682013-07-30 17:52:55 -07001428 case BBT_BLOCK_GOOD:
David Woodhousee0c7d762006-05-13 18:07:53 +01001429 return 0;
Brian Norris771c5682013-07-30 17:52:55 -07001430 case BBT_BLOCK_WORN:
David Woodhousee0c7d762006-05-13 18:07:53 +01001431 return 1;
Brian Norris771c5682013-07-30 17:52:55 -07001432 case BBT_BLOCK_RESERVED:
David Woodhousee0c7d762006-05-13 18:07:53 +01001433 return allowbbt ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435 return 1;
1436}
1437
Brian Norrisb32843b2013-07-30 17:52:59 -07001438/**
1439 * nand_markbad_bbt - [NAND Interface] Mark a block bad in the BBT
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001440 * @this: NAND chip object
Brian Norrisb32843b2013-07-30 17:52:59 -07001441 * @offs: offset of the bad block
1442 */
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001443int nand_markbad_bbt(struct nand_chip *this, loff_t offs)
Brian Norrisb32843b2013-07-30 17:52:59 -07001444{
Brian Norrisb32843b2013-07-30 17:52:59 -07001445 int block, ret = 0;
1446
1447 block = (int)(offs >> this->bbt_erase_shift);
1448
1449 /* Mark bad block in memory */
1450 bbt_mark_entry(this, block, BBT_BLOCK_WORN);
1451
1452 /* Update flash-based bad block table */
1453 if (this->bbt_options & NAND_BBT_USE_FLASH)
Boris Brezillon08136212018-11-11 08:55:03 +01001454 ret = nand_update_bbt(this, offs);
Brian Norrisb32843b2013-07-30 17:52:59 -07001455
1456 return ret;
1457}