blob: 9a7855839f81d1e59531d65838ffb3f346b847ac [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Overview:
3 * Bad block table support for the NAND driver
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00004 *
Fabio Estevamd159c4e2012-07-28 19:29:25 -03005 * Copyright © 2004 Thomas Gleixner (tglx@linutronix.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Description:
12 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000013 * When nand_scan_bbt is called, then it tries to find the bad block table
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020014 * depending on the options in the BBT descriptor(s). If no flash based BBT
Brian Norrisbb9ebd4e2011-05-31 16:31:23 -070015 * (NAND_BBT_USE_FLASH) is specified then the device is scanned for factory
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020016 * marked good / bad blocks. This information is used to create a memory BBT.
17 * Once a new bad block is discovered then the "factory" information is updated
18 * on the device.
19 * If a flash based BBT is specified then the function first tries to find the
20 * BBT on flash. If a BBT is found then the contents are read and the memory
21 * based BBT is created. If a mirrored BBT is selected then the mirror is
22 * searched too and the versions are compared. If the mirror has a greater
Huang Shijie44ed0ff2012-07-03 16:44:15 +080023 * version number, then the mirror BBT is used to build the memory based BBT.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * If the tables are not versioned, then we "or" the bad block information.
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020025 * If one of the BBTs is out of date or does not exist it is (re)created.
26 * If no BBT exists at all then the device is scanned for factory marked
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000027 * good / bad blocks and the bad block tables are created.
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 *
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020029 * For manufacturer created BBTs like the one found on M-SYS DOC devices
30 * the BBT is searched and read but never created
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 *
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020032 * The auto generated bad block table is located in the last good blocks
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000033 * of the device. The table is mirrored, so it can be updated eventually.
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020034 * The table is marked in the OOB area with an ident pattern and a version
35 * number which indicates which of both tables is more up to date. If the NAND
36 * controller needs the complete OOB area for the ECC information then the
Brian Norrisbb9ebd4e2011-05-31 16:31:23 -070037 * option NAND_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of
Brian Norrisa40f7342011-05-31 16:31:22 -070038 * course): it moves the ident pattern and the version byte into the data area
39 * and the OOB area will remain untouched.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 *
41 * The table uses 2 bits per block
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020042 * 11b: block is good
43 * 00b: block is factory marked bad
44 * 01b, 10b: block is marked bad due to wear
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 *
46 * The memory bad block table uses the following scheme:
47 * 00b: block is good
48 * 01b: block is marked bad due to wear
49 * 10b: block is reserved (to protect the bbt area)
50 * 11b: block is factory marked bad
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000051 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * Multichip devices like DOC store the bad block info per floor.
53 *
54 * Following assumptions are made:
55 * - bbts start at a page boundary, if autolocated on a block boundary
David Woodhousee0c7d762006-05-13 18:07:53 +010056 * - the space necessary for a bbt in FLASH does not exceed a block boundary
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000057 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 */
59
60#include <linux/slab.h>
61#include <linux/types.h>
62#include <linux/mtd/mtd.h>
Richard Genoud61de9da2012-09-11 15:50:54 +020063#include <linux/mtd/bbm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/bitops.h>
65#include <linux/delay.h>
David Woodhousec3f8abf2006-05-13 04:03:42 +010066#include <linux/vmalloc.h>
Paul Gortmakerf3bcc012011-07-10 12:43:28 -040067#include <linux/export.h>
Brian Norris491ed062012-06-22 16:35:44 -070068#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Boris Brezillon348d56a2018-09-07 00:38:48 +020070#include "internals.h"
71
Brian Norris771c5682013-07-30 17:52:55 -070072#define BBT_BLOCK_GOOD 0x00
73#define BBT_BLOCK_WORN 0x01
74#define BBT_BLOCK_RESERVED 0x02
75#define BBT_BLOCK_FACTORY_BAD 0x03
76
77#define BBT_ENTRY_MASK 0x03
78#define BBT_ENTRY_SHIFT 2
79
80static inline uint8_t bbt_get_entry(struct nand_chip *chip, int block)
81{
82 uint8_t entry = chip->bbt[block >> BBT_ENTRY_SHIFT];
83 entry >>= (block & BBT_ENTRY_MASK) * 2;
84 return entry & BBT_ENTRY_MASK;
85}
86
87static inline void bbt_mark_entry(struct nand_chip *chip, int block,
88 uint8_t mark)
89{
90 uint8_t msk = (mark & BBT_ENTRY_MASK) << ((block & BBT_ENTRY_MASK) * 2);
91 chip->bbt[block >> BBT_ENTRY_SHIFT] |= msk;
92}
93
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020094static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
95{
Brian Norris718894a2012-06-22 16:35:43 -070096 if (memcmp(buf, td->pattern, td->len))
97 return -1;
98 return 0;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +020099}
100
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000101/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * check_pattern - [GENERIC] check if a pattern is in the buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700103 * @buf: the buffer to search
104 * @len: the length of buffer to search
105 * @paglen: the pagelength
106 * @td: search pattern descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700108 * Check for a pattern at the given place. Used to search bad block tables and
Brian Norrisdad22562013-07-30 17:53:00 -0700109 * good / bad block identifiers.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700110 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100111static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200113 if (td->options & NAND_BBT_NO_OOB)
114 return check_pattern_no_oob(buf, td);
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 /* Compare the pattern */
Brian Norrisdad22562013-07-30 17:53:00 -0700117 if (memcmp(buf + paglen + td->offs, td->pattern, td->len))
Brian Norris75b66d82011-09-07 13:13:41 -0700118 return -1;
Brian Norris58373ff2010-07-15 12:15:44 -0700119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return 0;
121}
122
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000123/**
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100124 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
Brian Norris8b6e50c2011-05-25 14:59:01 -0700125 * @buf: the buffer to search
126 * @td: search pattern descriptor
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100127 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700128 * Check for a pattern at the given place. Used to search bad block tables and
129 * good / bad block identifiers. Same as check_pattern, but no optional empty
130 * check.
131 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100132static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100133{
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100134 /* Compare the pattern */
Brian Norris491ed062012-06-22 16:35:44 -0700135 if (memcmp(buf + td->offs, td->pattern, td->len))
136 return -1;
Thomas Gleixnerc9e053652005-06-14 16:39:57 +0100137 return 0;
138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200141 * add_marker_len - compute the length of the marker in data area
Brian Norris8b6e50c2011-05-25 14:59:01 -0700142 * @td: BBT descriptor used for computation
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200143 *
Brian Norris7854d3f2011-06-23 14:12:08 -0700144 * The length will be 0 if the marker is located in OOB area.
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200145 */
146static u32 add_marker_len(struct nand_bbt_descr *td)
147{
148 u32 len;
149
150 if (!(td->options & NAND_BBT_NO_OOB))
151 return 0;
152
153 len = td->len;
154 if (td->options & NAND_BBT_VERSION)
155 len++;
156 return len;
157}
158
159/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 * read_bbt - [GENERIC] Read the bad block table starting from page
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,
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300419 loff_t offs, uint8_t *buf, int numpages)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200420{
Boris Brezillon08136212018-11-11 08:55:03 +0100421 struct mtd_info *mtd = nand_to_mtd(this);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200422 struct mtd_oob_ops ops;
423 int j, ret;
424
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200425 ops.ooblen = mtd->oobsize;
426 ops.oobbuf = buf;
427 ops.ooboffs = 0;
428 ops.datbuf = NULL;
Brian Norris0612b9d2011-08-30 18:45:40 -0700429 ops.mode = MTD_OPS_PLACE_OOB;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200430
Shmulik Ladkanieceb84b2012-08-20 16:29:15 +0300431 for (j = 0; j < numpages; j++) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200432 /*
Brian Norris8b6e50c2011-05-25 14:59:01 -0700433 * Read the full oob until read_oob is fixed to handle single
434 * byte reads for 16 bit buswidth.
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200435 */
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200436 ret = mtd_read_oob(mtd, offs, &ops);
Brian Norris903cd062011-06-28 16:28:58 -0700437 /* Ignore ECC errors when checking for BBM */
Brian Norrisd57f40542011-09-20 18:34:25 -0700438 if (ret && !mtd_is_bitflip_or_eccerr(ret))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200439 return ret;
440
441 if (check_short_pattern(buf, bd))
442 return 1;
443
444 offs += mtd->writesize;
445 }
446 return 0;
447}
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449/**
450 * create_bbt - [GENERIC] Create a bad block table by scanning the device
Boris Brezillon08136212018-11-11 08:55:03 +0100451 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700452 * @buf: temporary buffer
453 * @bd: descriptor for the good/bad block search pattern
454 * @chip: create the table for a specific chip, -1 read all chips; applies only
455 * if NAND_BBT_PERCHIP option is set
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700457 * Create a bad block table by scanning the device for the given good/bad block
458 * identify pattern.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 */
Boris Brezillon08136212018-11-11 08:55:03 +0100460static int create_bbt(struct nand_chip *this, uint8_t *buf,
461 struct nand_bbt_descr *bd, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Boris Brezillon6c836d52018-10-29 11:22:16 +0100463 u64 targetsize = nanddev_target_size(&this->base);
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 {
Boris Brezillon32813e22018-10-29 11:58:29 +0100481 if (chip >= nanddev_ntargets(&this->base)) {
Brian Norris9a4d4d62011-07-19 10:06:07 -0700482 pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
Boris Brezillon32813e22018-10-29 11:58:29 +0100483 chip + 1, nanddev_ntargets(&this->base));
Artem B. Bityuckiy171650a2005-02-16 17:09:39 +0000484 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
Boris Brezillon6c836d52018-10-29 11:22:16 +0100486 numblocks = targetsize >> 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 Brezillon6c836d52018-10-29 11:22:16 +0100534 u64 targetsize = nanddev_target_size(&this->base);
Boris Brezillon08136212018-11-11 08:55:03 +0100535 struct mtd_info *mtd = nand_to_mtd(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 int i, chips;
Brian Norris930de532014-01-02 17:14:10 -0800537 int startblock, block, dir;
Joern Engel28318772006-05-22 23:18:05 +0200538 int scanlen = mtd->writesize + mtd->oobsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 int bbtblocks;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200540 int blocktopage = this->bbt_erase_shift - this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Brian Norris8b6e50c2011-05-25 14:59:01 -0700542 /* Search direction top -> down? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (td->options & NAND_BBT_LASTBLOCK) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100544 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 dir = -1;
546 } else {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000547 startblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 dir = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000549 }
550
Brian Norris8b6e50c2011-05-25 14:59:01 -0700551 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (td->options & NAND_BBT_PERCHIP) {
Boris Brezillon32813e22018-10-29 11:58:29 +0100553 chips = nanddev_ntargets(&this->base);
Boris Brezillon6c836d52018-10-29 11:22:16 +0100554 bbtblocks = targetsize >> this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 startblock &= bbtblocks - 1;
556 } else {
557 chips = 1;
558 bbtblocks = mtd->size >> this->bbt_erase_shift;
559 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 for (i = 0; i < chips; i++) {
562 /* Reset version information */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000563 td->version[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 td->pages[i] = -1;
565 /* Scan the maximum number of blocks */
566 for (block = 0; block < td->maxblocks; block++) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 int actblock = startblock + dir * block;
Adrian Hunter69423d92008-12-10 13:37:21 +0000569 loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* Read first page */
Boris Brezillon08136212018-11-11 08:55:03 +0100572 scan_read(this, buf, offs, mtd->writesize, td);
Joern Engel28318772006-05-22 23:18:05 +0200573 if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200574 td->pages[i] = actblock << blocktopage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (td->options & NAND_BBT_VERSION) {
Boris Brezillon08136212018-11-11 08:55:03 +0100576 offs = bbt_get_ver_offs(this, td);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200577 td->version[i] = buf[offs];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579 break;
580 }
581 }
Boris Brezillon6c836d52018-10-29 11:22:16 +0100582 startblock += targetsize >> this->bbt_erase_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584 /* Check, if we found a bbt for each requested chip */
585 for (i = 0; i < chips; i++) {
586 if (td->pages[i] == -1)
Brian Norris9a4d4d62011-07-19 10:06:07 -0700587 pr_warn("Bad block table not found for chip %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 else
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200589 pr_info("Bad block table found at page %d, version 0x%02X\n",
590 td->pages[i], td->version[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000592 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
595/**
596 * search_read_bbts - [GENERIC] scan the device for bad block table(s)
Boris Brezillon08136212018-11-11 08:55:03 +0100597 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700598 * @buf: temporary buffer
599 * @td: descriptor for the bad block table
600 * @md: descriptor for the bad block table mirror
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700602 * Search and read the bad block table(s).
603 */
Boris Brezillon08136212018-11-11 08:55:03 +0100604static void search_read_bbts(struct nand_chip *this, uint8_t *buf,
Brian Norris7b5a2d42012-06-22 16:35:41 -0700605 struct nand_bbt_descr *td,
606 struct nand_bbt_descr *md)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
608 /* Search the primary table */
Boris Brezillon08136212018-11-11 08:55:03 +0100609 search_bbt(this, buf, td);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /* Search the mirror table */
612 if (md)
Boris Brezillon08136212018-11-11 08:55:03 +0100613 search_bbt(this, buf, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000614}
615
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000616/**
Boris Brezillonc3baf272016-08-15 18:22:01 -0500617 * get_bbt_block - Get the first valid eraseblock suitable to store a BBT
618 * @this: the NAND device
619 * @td: the BBT description
620 * @md: the mirror BBT descriptor
621 * @chip: the CHIP selector
622 *
623 * This functions returns a positive block number pointing a valid eraseblock
624 * suitable to store a BBT (i.e. in the range reserved for BBT), or -ENOSPC if
625 * all blocks are already used of marked bad. If td->pages[chip] was already
626 * pointing to a valid block we re-use it, otherwise we search for the next
627 * valid one.
628 */
629static int get_bbt_block(struct nand_chip *this, struct nand_bbt_descr *td,
630 struct nand_bbt_descr *md, int chip)
631{
Boris Brezillon6c836d52018-10-29 11:22:16 +0100632 u64 targetsize = nanddev_target_size(&this->base);
Boris Brezillonc3baf272016-08-15 18:22:01 -0500633 int startblock, dir, page, numblocks, i;
634
635 /*
636 * There was already a version of the table, reuse the page. This
637 * applies for absolute placement too, as we have the page number in
638 * td->pages.
639 */
640 if (td->pages[chip] != -1)
641 return td->pages[chip] >>
642 (this->bbt_erase_shift - this->page_shift);
643
Boris Brezillon6c836d52018-10-29 11:22:16 +0100644 numblocks = (int)(targetsize >> this->bbt_erase_shift);
Boris Brezillonc3baf272016-08-15 18:22:01 -0500645 if (!(td->options & NAND_BBT_PERCHIP))
Boris Brezillon32813e22018-10-29 11:58:29 +0100646 numblocks *= nanddev_ntargets(&this->base);
Boris Brezillonc3baf272016-08-15 18:22:01 -0500647
648 /*
649 * Automatic placement of the bad block table. Search direction
650 * top -> down?
651 */
652 if (td->options & NAND_BBT_LASTBLOCK) {
653 startblock = numblocks * (chip + 1) - 1;
654 dir = -1;
655 } else {
656 startblock = chip * numblocks;
657 dir = 1;
658 }
659
660 for (i = 0; i < td->maxblocks; i++) {
661 int block = startblock + dir * i;
662
663 /* Check, if the block is bad */
664 switch (bbt_get_entry(this, block)) {
665 case BBT_BLOCK_WORN:
666 case BBT_BLOCK_FACTORY_BAD:
667 continue;
668 }
669
670 page = block << (this->bbt_erase_shift - this->page_shift);
671
672 /* Check, if the block is used by the mirror table */
673 if (!md || md->pages[chip] != page)
674 return block;
675 }
676
677 return -ENOSPC;
678}
679
680/**
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500681 * mark_bbt_block_bad - Mark one of the block reserved for BBT bad
682 * @this: the NAND device
683 * @td: the BBT description
684 * @chip: the CHIP selector
685 * @block: the BBT block to mark
686 *
687 * Blocks reserved for BBT can become bad. This functions is an helper to mark
688 * such blocks as bad. It takes care of updating the in-memory BBT, marking the
689 * block as bad using a bad block marker and invalidating the associated
690 * td->pages[] entry.
691 */
692static void mark_bbt_block_bad(struct nand_chip *this,
693 struct nand_bbt_descr *td,
694 int chip, int block)
695{
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500696 loff_t to;
697 int res;
698
699 bbt_mark_entry(this, block, BBT_BLOCK_WORN);
700
701 to = (loff_t)block << this->bbt_erase_shift;
Boris Brezilloncdc784c2018-09-07 00:38:38 +0200702 res = nand_markbad_bbm(this, to);
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500703 if (res)
704 pr_warn("nand_bbt: error %d while marking block %d bad\n",
705 res, block);
706
707 td->pages[chip] = -1;
708}
709
710/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 * write_bbt - [GENERIC] (Re)write the bad block table
Boris Brezillon08136212018-11-11 08:55:03 +0100712 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700713 * @buf: temporary buffer
714 * @td: descriptor for the bad block table
715 * @md: descriptor for the bad block table mirror
716 * @chipsel: selector for a specific chip, -1 for all
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700718 * (Re)write the bad block table.
719 */
Boris Brezillon08136212018-11-11 08:55:03 +0100720static int write_bbt(struct nand_chip *this, uint8_t *buf,
Thomas Gleixner9223a452006-05-23 17:21:03 +0200721 struct nand_bbt_descr *td, struct nand_bbt_descr *md,
722 int chipsel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Boris Brezillon6c836d52018-10-29 11:22:16 +0100724 u64 targetsize = nanddev_target_size(&this->base);
Boris Brezillon08136212018-11-11 08:55:03 +0100725 struct mtd_info *mtd = nand_to_mtd(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct erase_info einfo;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700727 int i, res, chip = 0;
Boris Brezillonc3baf272016-08-15 18:22:01 -0500728 int bits, page, offs, numblocks, sft, sftmsk;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700729 int nrchips, pageoffs, ooboffs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 uint8_t msk[4];
731 uint8_t rcode = td->reserved_block_code;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200732 size_t retlen, len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 loff_t to;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200734 struct mtd_oob_ops ops;
735
736 ops.ooblen = mtd->oobsize;
737 ops.ooboffs = 0;
738 ops.datbuf = NULL;
Brian Norris0612b9d2011-08-30 18:45:40 -0700739 ops.mode = MTD_OPS_PLACE_OOB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 if (!rcode)
742 rcode = 0xff;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700743 /* Write bad block table per chip rather than per device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (td->options & NAND_BBT_PERCHIP) {
Boris Brezillon6c836d52018-10-29 11:22:16 +0100745 numblocks = (int)(targetsize >> this->bbt_erase_shift);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700746 /* Full device write or specific chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (chipsel == -1) {
Boris Brezillon32813e22018-10-29 11:58:29 +0100748 nrchips = nanddev_ntargets(&this->base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 } else {
750 nrchips = chipsel + 1;
751 chip = chipsel;
752 }
753 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +0100754 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 nrchips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000756 }
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 /* Loop through the chips */
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500759 while (chip < nrchips) {
Boris Brezillonc3baf272016-08-15 18:22:01 -0500760 int block;
761
762 block = get_bbt_block(this, td, md, chip);
763 if (block < 0) {
764 pr_err("No space left to write bad block table\n");
765 res = block;
766 goto outerr;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Brian Norris8b6e50c2011-05-25 14:59:01 -0700769 /*
Boris Brezillonc3baf272016-08-15 18:22:01 -0500770 * get_bbt_block() returns a block number, shift the value to
771 * get a page number.
Brian Norris8b6e50c2011-05-25 14:59:01 -0700772 */
Boris Brezillonc3baf272016-08-15 18:22:01 -0500773 page = block << (this->bbt_erase_shift - this->page_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 /* Set up shift count and masks for the flash table */
776 bits = td->options & NAND_BBT_NRBITS_MSK;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200777 msk[2] = ~rcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 switch (bits) {
Thomas Gleixner9223a452006-05-23 17:21:03 +0200779 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
780 msk[3] = 0x01;
781 break;
782 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
783 msk[3] = 0x03;
784 break;
785 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
786 msk[3] = 0x0f;
787 break;
788 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
789 msk[3] = 0xff;
790 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 default: return -EINVAL;
792 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000793
Brian Norris596d7452011-09-07 13:13:33 -0700794 to = ((loff_t)page) << this->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Brian Norris8b6e50c2011-05-25 14:59:01 -0700796 /* Must we save the block contents? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (td->options & NAND_BBT_SAVECONTENT) {
798 /* Make it block aligned */
Brian Norrisf5cd2ae2015-02-28 02:13:13 -0800799 to &= ~(((loff_t)1 << this->bbt_erase_shift) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 len = 1 << this->bbt_erase_shift;
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200801 res = mtd_read(mtd, to, len, &retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (res < 0) {
803 if (retlen != len) {
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200804 pr_info("nand_bbt: error reading block for writing the bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return res;
806 }
Rafał Miłecki2ac63d92014-08-19 13:55:34 +0200807 pr_warn("nand_bbt: ECC error while reading block for writing bad block table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200809 /* Read oob data */
Vitaly Wool70145682006-11-03 18:20:38 +0300810 ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200811 ops.oobbuf = &buf[len];
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200812 res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
Vitaly Wool70145682006-11-03 18:20:38 +0300813 if (res < 0 || ops.oobretlen != ops.ooblen)
Thomas Gleixner9223a452006-05-23 17:21:03 +0200814 goto outerr;
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /* Calc the byte offset in the buffer */
817 pageoffs = page - (int)(to >> this->page_shift);
818 offs = pageoffs << this->page_shift;
819 /* Preset the bbt area with 0xff */
Brian Norris596d7452011-09-07 13:13:33 -0700820 memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
Thomas Gleixner9223a452006-05-23 17:21:03 +0200821 ooboffs = len + (pageoffs * mtd->oobsize);
822
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200823 } else if (td->options & NAND_BBT_NO_OOB) {
824 ooboffs = 0;
825 offs = td->len;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700826 /* The version byte */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200827 if (td->options & NAND_BBT_VERSION)
828 offs++;
829 /* Calc length */
Brian Norris596d7452011-09-07 13:13:33 -0700830 len = (size_t)(numblocks >> sft);
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200831 len += offs;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700832 /* Make it page aligned! */
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +0200833 len = ALIGN(len, mtd->writesize);
834 /* Preset the buffer with 0xff */
835 memset(buf, 0xff, len);
836 /* Pattern is located at the begin of first page */
837 memcpy(buf, td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 } else {
839 /* Calc length */
Brian Norris596d7452011-09-07 13:13:33 -0700840 len = (size_t)(numblocks >> sft);
Brian Norris8b6e50c2011-05-25 14:59:01 -0700841 /* Make it page aligned! */
Sebastian Andrzej Siewiorcda32092010-09-29 19:43:50 +0200842 len = ALIGN(len, mtd->writesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 /* Preset the buffer with 0xff */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200844 memset(buf, 0xff, len +
845 (len >> this->page_shift)* mtd->oobsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 offs = 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200847 ooboffs = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 /* Pattern is located in oob area of first page */
Thomas Gleixner9223a452006-05-23 17:21:03 +0200849 memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000851
Thomas Gleixner9223a452006-05-23 17:21:03 +0200852 if (td->options & NAND_BBT_VERSION)
853 buf[ooboffs + td->veroffs] = td->version[chip];
854
Brian Norris8b6e50c2011-05-25 14:59:01 -0700855 /* Walk through the memory table */
Brian Norrisb4d20d62013-07-30 17:52:56 -0700856 for (i = 0; i < numblocks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 uint8_t dat;
Brian Norrisb4d20d62013-07-30 17:52:56 -0700858 int sftcnt = (i << (3 - sft)) & sftmsk;
859 dat = bbt_get_entry(this, chip * numblocks + i);
860 /* Do not store the reserved bbt blocks! */
861 buf[offs + (i >> sft)] &= ~(msk[dat] << sftcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000863
David Woodhousee0c7d762006-05-13 18:07:53 +0100864 memset(&einfo, 0, sizeof(einfo));
Adrian Hunter69423d92008-12-10 13:37:21 +0000865 einfo.addr = to;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 einfo.len = 1 << this->bbt_erase_shift;
Boris Brezillone4cdf9c2018-09-06 14:05:35 +0200867 res = nand_erase_nand(this, &einfo, 1);
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500868 if (res < 0) {
869 pr_warn("nand_bbt: error while erasing BBT block %d\n",
870 res);
871 mark_bbt_block_bad(this, td, chip, block);
872 continue;
873 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000874
Boris Brezillon08136212018-11-11 08:55:03 +0100875 res = scan_write_bbt(this, to, len, buf,
876 td->options & NAND_BBT_NO_OOB ?
877 NULL : &buf[len]);
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500878 if (res < 0) {
879 pr_warn("nand_bbt: error while writing BBT block %d\n",
880 res);
881 mark_bbt_block_bad(this, td, chip, block);
882 continue;
883 }
Thomas Gleixner9223a452006-05-23 17:21:03 +0200884
Brian Norrisd0370212011-07-19 10:06:08 -0700885 pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
886 (unsigned long long)to, td->version[chip]);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 /* Mark it as used */
Kyle Roeschley10ffd572016-08-15 18:22:02 -0500889 td->pages[chip++] = page;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return 0;
Thomas Gleixner9223a452006-05-23 17:21:03 +0200892
893 outerr:
Brian Norrisd0370212011-07-19 10:06:08 -0700894 pr_warn("nand_bbt: error while writing bad block table %d\n", res);
Thomas Gleixner9223a452006-05-23 17:21:03 +0200895 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897
898/**
899 * nand_memory_bbt - [GENERIC] create a memory based bad block table
Boris Brezillon08136212018-11-11 08:55:03 +0100900 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -0700901 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700903 * The function creates a memory based bbt by scanning the device for
904 * manufacturer / software marked good / bad blocks.
905 */
Boris Brezillon08136212018-11-11 08:55:03 +0100906static inline int nand_memory_bbt(struct nand_chip *this,
907 struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Boris Brezilloneeab7172018-10-28 15:27:55 +0100909 u8 *pagebuf = nand_get_data_buf(this);
910
911 return create_bbt(this, pagebuf, bd, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
914/**
David Woodhousee0c7d762006-05-13 18:07:53 +0100915 * check_create - [GENERIC] create and write bbt(s) if necessary
Boris Brezillon08136212018-11-11 08:55:03 +0100916 * @this: the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -0700917 * @buf: temporary buffer
918 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 *
Brian Norris8b6e50c2011-05-25 14:59:01 -0700920 * The function checks the results of the previous call to read_bbt and creates
921 * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found
922 * for the chip/device. Update is necessary if one of the tables is missing or
923 * the version nr. of one table is less than the other.
924 */
Boris Brezillon08136212018-11-11 08:55:03 +0100925static int check_create(struct nand_chip *this, uint8_t *buf,
926 struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Brian Norris623978d2011-09-20 18:35:34 -0700928 int i, chips, writeops, create, chipsel, res, res2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 struct nand_bbt_descr *td = this->bbt_td;
930 struct nand_bbt_descr *md = this->bbt_md;
931 struct nand_bbt_descr *rd, *rd2;
932
Brian Norris8b6e50c2011-05-25 14:59:01 -0700933 /* Do we have a bbt per chip? */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000934 if (td->options & NAND_BBT_PERCHIP)
Boris Brezillon32813e22018-10-29 11:58:29 +0100935 chips = nanddev_ntargets(&this->base);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000936 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 chips = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 for (i = 0; i < chips; i++) {
940 writeops = 0;
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700941 create = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 rd = NULL;
943 rd2 = NULL;
Brian Norris623978d2011-09-20 18:35:34 -0700944 res = res2 = 0;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700945 /* Per chip or per device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
Brian Norris8b6e50c2011-05-25 14:59:01 -0700947 /* Mirrored table available? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (md) {
949 if (td->pages[i] == -1 && md->pages[i] == -1) {
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700950 create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 writeops = 0x03;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700952 } else if (td->pages[i] == -1) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000953 rd = md;
Brian Norris596d7452011-09-07 13:13:33 -0700954 writeops = 0x01;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700955 } else if (md->pages[i] == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 rd = td;
Brian Norris596d7452011-09-07 13:13:33 -0700957 writeops = 0x02;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700958 } else if (td->version[i] == md->version[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 rd = td;
960 if (!(td->options & NAND_BBT_VERSION))
961 rd2 = md;
Brian Norrisc5e8ef92011-09-07 13:13:34 -0700962 } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 rd = td;
Brian Norris596d7452011-09-07 13:13:33 -0700964 writeops = 0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 } else {
966 rd = md;
Brian Norris596d7452011-09-07 13:13:33 -0700967 writeops = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 } else {
970 if (td->pages[i] == -1) {
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700971 create = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 writeops = 0x01;
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700973 } else {
974 rd = td;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000977
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700978 if (create) {
979 /* Create the bad block table by scanning the device? */
980 if (!(td->options & NAND_BBT_CREATE))
981 continue;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000982
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700983 /* Create the table in memory by scanning the chip(s) */
984 if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
Boris Brezillon08136212018-11-11 08:55:03 +0100985 create_bbt(this, buf, bd, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Brian Norrisb61bf5b2011-09-07 13:13:35 -0700987 td->version[i] = 1;
988 if (md)
989 md->version[i] = 1;
990 }
991
Brian Norris8b6e50c2011-05-25 14:59:01 -0700992 /* Read back first? */
Brian Norris623978d2011-09-20 18:35:34 -0700993 if (rd) {
Boris Brezillon08136212018-11-11 08:55:03 +0100994 res = read_abs_bbt(this, buf, rd, chipsel);
Brian Norris623978d2011-09-20 18:35:34 -0700995 if (mtd_is_eccerr(res)) {
996 /* Mark table as invalid */
997 rd->pages[i] = -1;
Brian Norrisdadc17a2011-09-20 18:35:57 -0700998 rd->version[i] = 0;
Brian Norris623978d2011-09-20 18:35:34 -0700999 i--;
1000 continue;
1001 }
1002 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07001003 /* If they weren't versioned, read both */
Brian Norris623978d2011-09-20 18:35:34 -07001004 if (rd2) {
Boris Brezillon08136212018-11-11 08:55:03 +01001005 res2 = read_abs_bbt(this, buf, rd2, chipsel);
Brian Norris623978d2011-09-20 18:35:34 -07001006 if (mtd_is_eccerr(res2)) {
1007 /* Mark table as invalid */
1008 rd2->pages[i] = -1;
Brian Norrisdadc17a2011-09-20 18:35:57 -07001009 rd2->version[i] = 0;
Brian Norris623978d2011-09-20 18:35:34 -07001010 i--;
1011 continue;
1012 }
1013 }
1014
1015 /* Scrub the flash table(s)? */
1016 if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
1017 writeops = 0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Brian Norrisdadc17a2011-09-20 18:35:57 -07001019 /* Update version numbers before writing */
1020 if (md) {
1021 td->version[i] = max(td->version[i], md->version[i]);
1022 md->version[i] = td->version[i];
1023 }
1024
Brian Norris8b6e50c2011-05-25 14:59:01 -07001025 /* Write the bad block table to the device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
Boris Brezillon08136212018-11-11 08:55:03 +01001027 res = write_bbt(this, buf, td, md, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (res < 0)
1029 return res;
1030 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001031
Brian Norris8b6e50c2011-05-25 14:59:01 -07001032 /* Write the mirror bad block table to the device? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
Boris Brezillon08136212018-11-11 08:55:03 +01001034 res = write_bbt(this, buf, md, td, chipsel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (res < 0)
1036 return res;
1037 }
1038 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001039 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
1042/**
Boris Brezillon99f33512018-11-11 08:55:04 +01001043 * nand_update_bbt - update bad block table(s)
1044 * @this: the NAND device
1045 * @offs: the offset of the newly marked block
1046 *
1047 * The function updates the bad block table(s).
1048 */
1049static int nand_update_bbt(struct nand_chip *this, loff_t offs)
1050{
1051 struct mtd_info *mtd = nand_to_mtd(this);
1052 int len, res = 0;
1053 int chip, chipsel;
1054 uint8_t *buf;
1055 struct nand_bbt_descr *td = this->bbt_td;
1056 struct nand_bbt_descr *md = this->bbt_md;
1057
1058 if (!this->bbt || !td)
1059 return -EINVAL;
1060
1061 /* Allocate a temporary buffer for one eraseblock incl. oob */
1062 len = (1 << this->bbt_erase_shift);
1063 len += (len >> this->page_shift) * mtd->oobsize;
1064 buf = kmalloc(len, GFP_KERNEL);
1065 if (!buf)
1066 return -ENOMEM;
1067
1068 /* Do we have a bbt per chip? */
1069 if (td->options & NAND_BBT_PERCHIP) {
1070 chip = (int)(offs >> this->chip_shift);
1071 chipsel = chip;
1072 } else {
1073 chip = 0;
1074 chipsel = -1;
1075 }
1076
1077 td->version[chip]++;
1078 if (md)
1079 md->version[chip]++;
1080
1081 /* Write the bad block table to the device? */
1082 if (td->options & NAND_BBT_WRITE) {
1083 res = write_bbt(this, buf, td, md, chipsel);
1084 if (res < 0)
1085 goto out;
1086 }
1087 /* Write the mirror bad block table to the device? */
1088 if (md && (md->options & NAND_BBT_WRITE)) {
1089 res = write_bbt(this, buf, md, td, chipsel);
1090 }
1091
1092 out:
1093 kfree(buf);
1094 return res;
1095}
1096
1097/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001098 * mark_bbt_regions - [GENERIC] mark the bad block table regions
Boris Brezillon08136212018-11-11 08:55:03 +01001099 * @this: the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07001100 * @td: bad block table descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001102 * The bad block table regions are marked as "bad" to prevent accidental
1103 * erasures / writes. The regions are identified by the mark 0x02.
1104 */
Boris Brezillon08136212018-11-11 08:55:03 +01001105static void mark_bbt_region(struct nand_chip *this, struct nand_bbt_descr *td)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Boris Brezillon6c836d52018-10-29 11:22:16 +01001107 u64 targetsize = nanddev_target_size(&this->base);
Boris Brezillon08136212018-11-11 08:55:03 +01001108 struct mtd_info *mtd = nand_to_mtd(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 int i, j, chips, block, nrblocks, update;
Brian Norris771c5682013-07-30 17:52:55 -07001110 uint8_t oldval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Brian Norris8b6e50c2011-05-25 14:59:01 -07001112 /* Do we have a bbt per chip? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (td->options & NAND_BBT_PERCHIP) {
Boris Brezillon32813e22018-10-29 11:58:29 +01001114 chips = nanddev_ntargets(&this->base);
Boris Brezillon6c836d52018-10-29 11:22:16 +01001115 nrblocks = (int)(targetsize >> this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 } else {
1117 chips = 1;
1118 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001119 }
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 for (i = 0; i < chips; i++) {
1122 if ((td->options & NAND_BBT_ABSPAGE) ||
1123 !(td->options & NAND_BBT_WRITE)) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001124 if (td->pages[i] == -1)
1125 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
Brian Norrisb4d20d62013-07-30 17:52:56 -07001127 oldval = bbt_get_entry(this, block);
1128 bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
Brian Norris771c5682013-07-30 17:52:55 -07001129 if ((oldval != BBT_BLOCK_RESERVED) &&
1130 td->reserved_block_code)
Boris Brezillon08136212018-11-11 08:55:03 +01001131 nand_update_bbt(this, (loff_t)block <<
Brian Norrisb4d20d62013-07-30 17:52:56 -07001132 this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 continue;
1134 }
1135 update = 0;
1136 if (td->options & NAND_BBT_LASTBLOCK)
1137 block = ((i + 1) * nrblocks) - td->maxblocks;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001138 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 block = i * nrblocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 for (j = 0; j < td->maxblocks; j++) {
Brian Norrisb4d20d62013-07-30 17:52:56 -07001141 oldval = bbt_get_entry(this, block);
1142 bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
Brian Norris771c5682013-07-30 17:52:55 -07001143 if (oldval != BBT_BLOCK_RESERVED)
David Woodhousee0c7d762006-05-13 18:07:53 +01001144 update = 1;
Brian Norrisb4d20d62013-07-30 17:52:56 -07001145 block++;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001146 }
Brian Norris8b6e50c2011-05-25 14:59:01 -07001147 /*
1148 * If we want reserved blocks to be recorded to flash, and some
1149 * new ones have been marked, then we need to update the stored
1150 * bbts. This should only happen once.
1151 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (update && td->reserved_block_code)
Boris Brezillon08136212018-11-11 08:55:03 +01001153 nand_update_bbt(this, (loff_t)(block - 1) <<
Brian Norrisb4d20d62013-07-30 17:52:56 -07001154 this->bbt_erase_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156}
1157
1158/**
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001159 * verify_bbt_descr - verify the bad block description
Boris Brezillon08136212018-11-11 08:55:03 +01001160 * @this: the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07001161 * @bd: the table to verify
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001162 *
1163 * This functions performs a few sanity checks on the bad block description
1164 * table.
1165 */
Boris Brezillon08136212018-11-11 08:55:03 +01001166static void verify_bbt_descr(struct nand_chip *this, struct nand_bbt_descr *bd)
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001167{
Boris Brezillon6c836d52018-10-29 11:22:16 +01001168 u64 targetsize = nanddev_target_size(&this->base);
Boris Brezillon08136212018-11-11 08:55:03 +01001169 struct mtd_info *mtd = nand_to_mtd(this);
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001170 u32 pattern_len;
1171 u32 bits;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001172 u32 table_size;
1173
1174 if (!bd)
1175 return;
Stanislav Fomichev7912a5e2011-02-07 23:48:25 +03001176
1177 pattern_len = bd->len;
1178 bits = bd->options & NAND_BBT_NRBITS_MSK;
1179
Brian Norrisa40f7342011-05-31 16:31:22 -07001180 BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
Brian Norrisbb9ebd4e2011-05-31 16:31:23 -07001181 !(this->bbt_options & NAND_BBT_USE_FLASH));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001182 BUG_ON(!bits);
1183
1184 if (bd->options & NAND_BBT_VERSION)
1185 pattern_len++;
1186
1187 if (bd->options & NAND_BBT_NO_OOB) {
Brian Norrisbb9ebd4e2011-05-31 16:31:23 -07001188 BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
Brian Norrisa40f7342011-05-31 16:31:22 -07001189 BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001190 BUG_ON(bd->offs);
1191 if (bd->options & NAND_BBT_VERSION)
1192 BUG_ON(bd->veroffs != bd->len);
1193 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1194 }
1195
1196 if (bd->options & NAND_BBT_PERCHIP)
Boris Brezillon6c836d52018-10-29 11:22:16 +01001197 table_size = targetsize >> this->bbt_erase_shift;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001198 else
1199 table_size = mtd->size >> this->bbt_erase_shift;
1200 table_size >>= 3;
1201 table_size *= bits;
1202 if (bd->options & NAND_BBT_NO_OOB)
1203 table_size += pattern_len;
1204 BUG_ON(table_size > (1 << this->bbt_erase_shift));
1205}
1206
1207/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
Boris Brezillon08136212018-11-11 08:55:03 +01001209 * @this: the NAND device
Brian Norris8b6e50c2011-05-25 14:59:01 -07001210 * @bd: descriptor for the good/bad block search pattern
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001212 * The function checks, if a bad block table(s) is/are already available. If
1213 * not it scans the device for manufacturer marked good / bad blocks and writes
1214 * the bad block table(s) to the selected place.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001216 * The bad block table memory is allocated here. It must be freed by calling
1217 * the nand_free_bbt function.
1218 */
Boris Brezillon08136212018-11-11 08:55:03 +01001219static int nand_scan_bbt(struct nand_chip *this, struct nand_bbt_descr *bd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Boris Brezillon08136212018-11-11 08:55:03 +01001221 struct mtd_info *mtd = nand_to_mtd(this);
Brian Norris83c59542015-02-28 02:13:12 -08001222 int len, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 uint8_t *buf;
1224 struct nand_bbt_descr *td = this->bbt_td;
1225 struct nand_bbt_descr *md = this->bbt_md;
1226
Sheng Yong192db1c2015-07-31 01:12:44 +00001227 len = (mtd->size >> (this->bbt_erase_shift + 2)) ? : 1;
Brian Norris8b6e50c2011-05-25 14:59:01 -07001228 /*
1229 * Allocate memory (2bit per block) and clear the memory bad block
1230 * table.
1231 */
Burman Yan95b93a02006-11-15 21:10:29 +02001232 this->bbt = kzalloc(len, GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001233 if (!this->bbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Brian Norris8b6e50c2011-05-25 14:59:01 -07001236 /*
1237 * If no primary table decriptor is given, scan the device to build a
1238 * memory based bad block table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 */
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001240 if (!td) {
Boris Brezillon08136212018-11-11 08:55:03 +01001241 if ((res = nand_memory_bbt(this, bd))) {
Brian Norrisd0370212011-07-19 10:06:08 -07001242 pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
Brian Norris83c59542015-02-28 02:13:12 -08001243 goto err;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001244 }
Brian Norris83c59542015-02-28 02:13:12 -08001245 return 0;
Artem B. Bityuckiyeeada242005-02-11 10:14:15 +00001246 }
Boris Brezillon08136212018-11-11 08:55:03 +01001247 verify_bbt_descr(this, td);
1248 verify_bbt_descr(this, md);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 /* Allocate a temporary buffer for one eraseblock incl. oob */
1251 len = (1 << this->bbt_erase_shift);
1252 len += (len >> this->page_shift) * mtd->oobsize;
David Woodhousec3f8abf2006-05-13 04:03:42 +01001253 buf = vmalloc(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (!buf) {
Brian Norris83c59542015-02-28 02:13:12 -08001255 res = -ENOMEM;
1256 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001258
Brian Norris8b6e50c2011-05-25 14:59:01 -07001259 /* Is the bbt at a given page? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (td->options & NAND_BBT_ABSPAGE) {
Boris Brezillon08136212018-11-11 08:55:03 +01001261 read_abs_bbts(this, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001262 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 /* Search the bad block table using a pattern in oob */
Boris Brezillon08136212018-11-11 08:55:03 +01001264 search_read_bbts(this, buf, td, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Boris Brezillon08136212018-11-11 08:55:03 +01001267 res = check_create(this, buf, bd);
Brian Norris83c59542015-02-28 02:13:12 -08001268 if (res)
1269 goto err;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 /* Prevent the bbt regions from erasing / writing */
Boris Brezillon08136212018-11-11 08:55:03 +01001272 mark_bbt_region(this, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (md)
Boris Brezillon08136212018-11-11 08:55:03 +01001274 mark_bbt_region(this, md);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001275
David Woodhousee0c7d762006-05-13 18:07:53 +01001276 vfree(buf);
Brian Norris83c59542015-02-28 02:13:12 -08001277 return 0;
1278
1279err:
1280 kfree(this->bbt);
1281 this->bbt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return res;
1283}
1284
Brian Norris8b6e50c2011-05-25 14:59:01 -07001285/*
1286 * Define some generic bad / good block scan pattern which are used
1287 * while scanning a device for factory marked good / bad blocks.
1288 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1290
Brian Norris7854d3f2011-06-23 14:12:08 -07001291/* Generic flash bbt descriptors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1293static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1294
1295static struct nand_bbt_descr bbt_main_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001296 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1298 .offs = 8,
1299 .len = 4,
1300 .veroffs = 12,
Richard Genoud61de9da2012-09-11 15:50:54 +02001301 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 .pattern = bbt_pattern
1303};
1304
1305static struct nand_bbt_descr bbt_mirror_descr = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001306 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1308 .offs = 8,
1309 .len = 4,
1310 .veroffs = 12,
Richard Genoud61de9da2012-09-11 15:50:54 +02001311 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 .pattern = mirror_pattern
1313};
1314
Brian Norris9fd6b372012-06-22 16:35:40 -07001315static struct nand_bbt_descr bbt_main_no_oob_descr = {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001316 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1317 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1318 | NAND_BBT_NO_OOB,
1319 .len = 4,
1320 .veroffs = 4,
Richard Genoud61de9da2012-09-11 15:50:54 +02001321 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001322 .pattern = bbt_pattern
1323};
1324
Brian Norris9fd6b372012-06-22 16:35:40 -07001325static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001326 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1327 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1328 | NAND_BBT_NO_OOB,
1329 .len = 4,
1330 .veroffs = 4,
Richard Genoud61de9da2012-09-11 15:50:54 +02001331 .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001332 .pattern = mirror_pattern
1333};
1334
Brian Norris752ed6c2011-09-20 18:36:42 -07001335#define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
Brian Norris58373ff2010-07-15 12:15:44 -07001336/**
Brian Norris752ed6c2011-09-20 18:36:42 -07001337 * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
Brian Norris8b6e50c2011-05-25 14:59:01 -07001338 * @this: NAND chip to create descriptor for
Brian Norris58373ff2010-07-15 12:15:44 -07001339 *
1340 * This function allocates and initializes a nand_bbt_descr for BBM detection
Brian Norris752ed6c2011-09-20 18:36:42 -07001341 * based on the properties of @this. The new descriptor is stored in
Brian Norris58373ff2010-07-15 12:15:44 -07001342 * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1343 * passed to this function.
Brian Norris58373ff2010-07-15 12:15:44 -07001344 */
Brian Norris752ed6c2011-09-20 18:36:42 -07001345static int nand_create_badblock_pattern(struct nand_chip *this)
Brian Norris58373ff2010-07-15 12:15:44 -07001346{
1347 struct nand_bbt_descr *bd;
1348 if (this->badblock_pattern) {
Brian Norris752ed6c2011-09-20 18:36:42 -07001349 pr_warn("Bad block pattern already allocated; not replacing\n");
Brian Norris58373ff2010-07-15 12:15:44 -07001350 return -EINVAL;
1351 }
1352 bd = kzalloc(sizeof(*bd), GFP_KERNEL);
Brian Norris08700662011-06-07 16:01:54 -07001353 if (!bd)
Brian Norris58373ff2010-07-15 12:15:44 -07001354 return -ENOMEM;
Brian Norris752ed6c2011-09-20 18:36:42 -07001355 bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
Brian Norris58373ff2010-07-15 12:15:44 -07001356 bd->offs = this->badblockpos;
1357 bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1358 bd->pattern = scan_ff_pattern;
1359 bd->options |= NAND_BBT_DYNAMICSTRUCT;
1360 this->badblock_pattern = bd;
1361 return 0;
1362}
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364/**
Boris Brezillon44b07b92018-07-05 12:27:30 +02001365 * nand_create_bbt - [NAND Interface] Select a default bad block table for the device
1366 * @this: NAND chip object
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 *
Brian Norris8b6e50c2011-05-25 14:59:01 -07001368 * This function selects the default bad block table support for the device and
1369 * calls the nand_scan_bbt function.
1370 */
Boris Brezillon44b07b92018-07-05 12:27:30 +02001371int nand_create_bbt(struct nand_chip *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Brian Norrisabb9cf72014-05-20 22:34:40 -07001373 int ret;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001374
Brian Norris8b6e50c2011-05-25 14:59:01 -07001375 /* Is a flash based bad block table requested? */
Brian Norrisbb9ebd4e2011-05-31 16:31:23 -07001376 if (this->bbt_options & NAND_BBT_USE_FLASH) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001377 /* Use the default pattern descriptors */
1378 if (!this->bbt_td) {
Brian Norrisa40f7342011-05-31 16:31:22 -07001379 if (this->bbt_options & NAND_BBT_NO_OOB) {
Brian Norris9fd6b372012-06-22 16:35:40 -07001380 this->bbt_td = &bbt_main_no_oob_descr;
1381 this->bbt_md = &bbt_mirror_no_oob_descr;
Sebastian Andrzej Siewior7cba7b12010-09-30 21:28:01 +02001382 } else {
1383 this->bbt_td = &bbt_main_descr;
1384 this->bbt_md = &bbt_mirror_descr;
1385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 } else {
1388 this->bbt_td = NULL;
1389 this->bbt_md = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
Brian Norrisa2f812d2011-03-18 21:53:42 -07001391
Brian Norrisabb9cf72014-05-20 22:34:40 -07001392 if (!this->badblock_pattern) {
1393 ret = nand_create_badblock_pattern(this);
1394 if (ret)
1395 return ret;
1396 }
Brian Norrisa2f812d2011-03-18 21:53:42 -07001397
Boris Brezillon08136212018-11-11 08:55:03 +01001398 return nand_scan_bbt(this, this->badblock_pattern);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
Boris Brezillon44b07b92018-07-05 12:27:30 +02001400EXPORT_SYMBOL(nand_create_bbt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402/**
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001403 * nand_isreserved_bbt - [NAND Interface] Check if a block is reserved
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001404 * @this: NAND chip object
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001405 * @offs: offset in the device
1406 */
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001407int nand_isreserved_bbt(struct nand_chip *this, loff_t offs)
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001408{
Ezequiel Garcia8471bb72014-05-21 19:06:12 -03001409 int block;
1410
1411 block = (int)(offs >> this->bbt_erase_shift);
1412 return bbt_get_entry(this, block) == BBT_BLOCK_RESERVED;
1413}
1414
1415/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001416 * nand_isbad_bbt - [NAND Interface] Check if a block is bad
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001417 * @this: NAND chip object
Brian Norris8b6e50c2011-05-25 14:59:01 -07001418 * @offs: offset in the device
1419 * @allowbbt: allow access to bad block table region
1420 */
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001421int nand_isbad_bbt(struct nand_chip *this, loff_t offs, int allowbbt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Brian Norris39dbb022013-07-30 17:52:57 -07001423 int block, res;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001424
Brian Norrisb4d20d62013-07-30 17:52:56 -07001425 block = (int)(offs >> this->bbt_erase_shift);
1426 res = bbt_get_entry(this, block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Rafał Miłecki2ac63d92014-08-19 13:55:34 +02001428 pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1429 (unsigned int)offs, block, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Brian Norris39dbb022013-07-30 17:52:57 -07001431 switch (res) {
Brian Norris771c5682013-07-30 17:52:55 -07001432 case BBT_BLOCK_GOOD:
David Woodhousee0c7d762006-05-13 18:07:53 +01001433 return 0;
Brian Norris771c5682013-07-30 17:52:55 -07001434 case BBT_BLOCK_WORN:
David Woodhousee0c7d762006-05-13 18:07:53 +01001435 return 1;
Brian Norris771c5682013-07-30 17:52:55 -07001436 case BBT_BLOCK_RESERVED:
David Woodhousee0c7d762006-05-13 18:07:53 +01001437 return allowbbt ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
1439 return 1;
1440}
1441
Brian Norrisb32843b2013-07-30 17:52:59 -07001442/**
1443 * nand_markbad_bbt - [NAND Interface] Mark a block bad in the BBT
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001444 * @this: NAND chip object
Brian Norrisb32843b2013-07-30 17:52:59 -07001445 * @offs: offset of the bad block
1446 */
Boris Brezillon5740d4c2018-09-06 14:05:34 +02001447int nand_markbad_bbt(struct nand_chip *this, loff_t offs)
Brian Norrisb32843b2013-07-30 17:52:59 -07001448{
Brian Norrisb32843b2013-07-30 17:52:59 -07001449 int block, ret = 0;
1450
1451 block = (int)(offs >> this->bbt_erase_shift);
1452
1453 /* Mark bad block in memory */
1454 bbt_mark_entry(this, block, BBT_BLOCK_WORN);
1455
1456 /* Update flash-based bad block table */
1457 if (this->bbt_options & NAND_BBT_USE_FLASH)
Boris Brezillon08136212018-11-11 08:55:03 +01001458 ret = nand_update_bbt(this, offs);
Brian Norrisb32843b2013-07-30 17:52:59 -07001459
1460 return ret;
1461}