Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Overview: |
| 3 | * Bad block table support for the NAND driver |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4 | * |
Fabio Estevam | d159c4e | 2012-07-28 19:29:25 -0300 | [diff] [blame] | 5 | * Copyright © 2004 Thomas Gleixner (tglx@linutronix.de) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * 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 Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 13 | * When nand_scan_bbt is called, then it tries to find the bad block table |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 14 | * depending on the options in the BBT descriptor(s). If no flash based BBT |
Brian Norris | bb9ebd4 | 2011-05-31 16:31:23 -0700 | [diff] [blame] | 15 | * (NAND_BBT_USE_FLASH) is specified then the device is scanned for factory |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 16 | * 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 Shijie | 44ed0ff | 2012-07-03 16:44:15 +0800 | [diff] [blame] | 23 | * version number, then the mirror BBT is used to build the memory based BBT. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * If the tables are not versioned, then we "or" the bad block information. |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 25 | * 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 Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 27 | * good / bad blocks and the bad block tables are created. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | * |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 29 | * For manufacturer created BBTs like the one found on M-SYS DOC devices |
| 30 | * the BBT is searched and read but never created |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | * |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 32 | * The auto generated bad block table is located in the last good blocks |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 33 | * of the device. The table is mirrored, so it can be updated eventually. |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 34 | * 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 Norris | bb9ebd4 | 2011-05-31 16:31:23 -0700 | [diff] [blame] | 37 | * option NAND_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of |
Brian Norris | a40f734 | 2011-05-31 16:31:22 -0700 | [diff] [blame] | 38 | * course): it moves the ident pattern and the version byte into the data area |
| 39 | * and the OOB area will remain untouched. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | * |
| 41 | * The table uses 2 bits per block |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 42 | * 11b: block is good |
| 43 | * 00b: block is factory marked bad |
| 44 | * 01b, 10b: block is marked bad due to wear |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | * |
| 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 Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 51 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | * 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 Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 56 | * - the space necessary for a bbt in FLASH does not exceed a block boundary |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 57 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | */ |
| 59 | |
| 60 | #include <linux/slab.h> |
| 61 | #include <linux/types.h> |
| 62 | #include <linux/mtd/mtd.h> |
Richard Genoud | 61de9da | 2012-09-11 15:50:54 +0200 | [diff] [blame] | 63 | #include <linux/mtd/bbm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | #include <linux/bitops.h> |
| 65 | #include <linux/delay.h> |
David Woodhouse | c3f8abf | 2006-05-13 04:03:42 +0100 | [diff] [blame] | 66 | #include <linux/vmalloc.h> |
Paul Gortmaker | f3bcc01 | 2011-07-10 12:43:28 -0400 | [diff] [blame] | 67 | #include <linux/export.h> |
Brian Norris | 491ed06 | 2012-06-22 16:35:44 -0700 | [diff] [blame] | 68 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Boris Brezillon | 348d56a | 2018-09-07 00:38:48 +0200 | [diff] [blame] | 70 | #include "internals.h" |
| 71 | |
Brian Norris | 771c568 | 2013-07-30 17:52:55 -0700 | [diff] [blame] | 72 | #define BBT_BLOCK_GOOD 0x00 |
| 73 | #define BBT_BLOCK_WORN 0x01 |
| 74 | #define BBT_BLOCK_RESERVED 0x02 |
| 75 | #define BBT_BLOCK_FACTORY_BAD 0x03 |
| 76 | |
| 77 | #define BBT_ENTRY_MASK 0x03 |
| 78 | #define BBT_ENTRY_SHIFT 2 |
| 79 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 80 | static int nand_update_bbt(struct nand_chip *chip, loff_t offs); |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 81 | |
Brian Norris | 771c568 | 2013-07-30 17:52:55 -0700 | [diff] [blame] | 82 | static inline uint8_t bbt_get_entry(struct nand_chip *chip, int block) |
| 83 | { |
| 84 | uint8_t entry = chip->bbt[block >> BBT_ENTRY_SHIFT]; |
| 85 | entry >>= (block & BBT_ENTRY_MASK) * 2; |
| 86 | return entry & BBT_ENTRY_MASK; |
| 87 | } |
| 88 | |
| 89 | static inline void bbt_mark_entry(struct nand_chip *chip, int block, |
| 90 | uint8_t mark) |
| 91 | { |
| 92 | uint8_t msk = (mark & BBT_ENTRY_MASK) << ((block & BBT_ENTRY_MASK) * 2); |
| 93 | chip->bbt[block >> BBT_ENTRY_SHIFT] |= msk; |
| 94 | } |
| 95 | |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 96 | static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td) |
| 97 | { |
Brian Norris | 718894a | 2012-06-22 16:35:43 -0700 | [diff] [blame] | 98 | if (memcmp(buf, td->pattern, td->len)) |
| 99 | return -1; |
| 100 | return 0; |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 103 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | * check_pattern - [GENERIC] check if a pattern is in the buffer |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 105 | * @buf: the buffer to search |
| 106 | * @len: the length of buffer to search |
| 107 | * @paglen: the pagelength |
| 108 | * @td: search pattern descriptor |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 110 | * Check for a pattern at the given place. Used to search bad block tables and |
Brian Norris | dad2256 | 2013-07-30 17:53:00 -0700 | [diff] [blame] | 111 | * good / bad block identifiers. |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 112 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 113 | static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 115 | if (td->options & NAND_BBT_NO_OOB) |
| 116 | return check_pattern_no_oob(buf, td); |
| 117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | /* Compare the pattern */ |
Brian Norris | dad2256 | 2013-07-30 17:53:00 -0700 | [diff] [blame] | 119 | if (memcmp(buf + paglen + td->offs, td->pattern, td->len)) |
Brian Norris | 75b66d8 | 2011-09-07 13:13:41 -0700 | [diff] [blame] | 120 | return -1; |
Brian Norris | 58373ff | 2010-07-15 12:15:44 -0700 | [diff] [blame] | 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 125 | /** |
Thomas Gleixner | c9e05365 | 2005-06-14 16:39:57 +0100 | [diff] [blame] | 126 | * check_short_pattern - [GENERIC] check if a pattern is in the buffer |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 127 | * @buf: the buffer to search |
| 128 | * @td: search pattern descriptor |
Thomas Gleixner | c9e05365 | 2005-06-14 16:39:57 +0100 | [diff] [blame] | 129 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 130 | * Check for a pattern at the given place. Used to search bad block tables and |
| 131 | * good / bad block identifiers. Same as check_pattern, but no optional empty |
| 132 | * check. |
| 133 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 134 | static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td) |
Thomas Gleixner | c9e05365 | 2005-06-14 16:39:57 +0100 | [diff] [blame] | 135 | { |
Thomas Gleixner | c9e05365 | 2005-06-14 16:39:57 +0100 | [diff] [blame] | 136 | /* Compare the pattern */ |
Brian Norris | 491ed06 | 2012-06-22 16:35:44 -0700 | [diff] [blame] | 137 | if (memcmp(buf + td->offs, td->pattern, td->len)) |
| 138 | return -1; |
Thomas Gleixner | c9e05365 | 2005-06-14 16:39:57 +0100 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | /** |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 143 | * add_marker_len - compute the length of the marker in data area |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 144 | * @td: BBT descriptor used for computation |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 145 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 146 | * The length will be 0 if the marker is located in OOB area. |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 147 | */ |
| 148 | static u32 add_marker_len(struct nand_bbt_descr *td) |
| 149 | { |
| 150 | u32 len; |
| 151 | |
| 152 | if (!(td->options & NAND_BBT_NO_OOB)) |
| 153 | return 0; |
| 154 | |
| 155 | len = td->len; |
| 156 | if (td->options & NAND_BBT_VERSION) |
| 157 | len++; |
| 158 | return len; |
| 159 | } |
| 160 | |
| 161 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | * read_bbt - [GENERIC] Read the bad block table starting from page |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 163 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 164 | * @buf: temporary buffer |
| 165 | * @page: the starting page |
| 166 | * @num: the number of bbt descriptors to read |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 167 | * @td: the bbt describtion table |
Brian Norris | b4d20d6 | 2013-07-30 17:52:56 -0700 | [diff] [blame] | 168 | * @offs: block number offset in the table |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | * |
| 170 | * Read the bad block table starting from page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 172 | static int read_bbt(struct nand_chip *this, uint8_t *buf, int page, int num, |
| 173 | struct nand_bbt_descr *td, int offs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 175 | struct mtd_info *mtd = nand_to_mtd(this); |
Brian Norris | 167a8d5 | 2011-09-20 18:35:08 -0700 | [diff] [blame] | 176 | int res, ret = 0, i, j, act = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | size_t retlen, len, totlen; |
| 178 | loff_t from; |
Sebastian Andrzej Siewior | df5b4e3 | 2010-09-29 19:43:51 +0200 | [diff] [blame] | 179 | int bits = td->options & NAND_BBT_NRBITS_MSK; |
Brian Norris | 596d745 | 2011-09-07 13:13:33 -0700 | [diff] [blame] | 180 | uint8_t msk = (uint8_t)((1 << bits) - 1); |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 181 | u32 marker_len; |
Sebastian Andrzej Siewior | df5b4e3 | 2010-09-29 19:43:51 +0200 | [diff] [blame] | 182 | int reserved_block_code = td->reserved_block_code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
| 184 | totlen = (num * bits) >> 3; |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 185 | marker_len = add_marker_len(td); |
Brian Norris | 596d745 | 2011-09-07 13:13:33 -0700 | [diff] [blame] | 186 | from = ((loff_t)page) << this->page_shift; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | while (totlen) { |
Brian Norris | 596d745 | 2011-09-07 13:13:33 -0700 | [diff] [blame] | 189 | len = min(totlen, (size_t)(1 << this->bbt_erase_shift)); |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 190 | if (marker_len) { |
| 191 | /* |
| 192 | * In case the BBT marker is not in the OOB area it |
| 193 | * will be just in the first page. |
| 194 | */ |
| 195 | len -= marker_len; |
| 196 | from += marker_len; |
| 197 | marker_len = 0; |
| 198 | } |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 199 | res = mtd_read(mtd, from, len, &retlen, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | if (res < 0) { |
Brian Norris | 167a8d5 | 2011-09-20 18:35:08 -0700 | [diff] [blame] | 201 | if (mtd_is_eccerr(res)) { |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 202 | pr_info("nand_bbt: ECC error in BBT at 0x%012llx\n", |
| 203 | from & ~mtd->writesize); |
Brian Norris | 167a8d5 | 2011-09-20 18:35:08 -0700 | [diff] [blame] | 204 | return res; |
| 205 | } else if (mtd_is_bitflip(res)) { |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 206 | pr_info("nand_bbt: corrected error in BBT at 0x%012llx\n", |
| 207 | from & ~mtd->writesize); |
Brian Norris | 167a8d5 | 2011-09-20 18:35:08 -0700 | [diff] [blame] | 208 | ret = res; |
| 209 | } else { |
| 210 | pr_info("nand_bbt: error reading BBT\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | return res; |
| 212 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 213 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
| 215 | /* Analyse data */ |
| 216 | for (i = 0; i < len; i++) { |
| 217 | uint8_t dat = buf[i]; |
Brian Norris | b4d20d6 | 2013-07-30 17:52:56 -0700 | [diff] [blame] | 218 | for (j = 0; j < 8; j += bits, act++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | uint8_t tmp = (dat >> j) & msk; |
| 220 | if (tmp == msk) |
| 221 | continue; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 222 | if (reserved_block_code && (tmp == reserved_block_code)) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 223 | pr_info("nand_read_bbt: reserved block at 0x%012llx\n", |
Brian Norris | b4d20d6 | 2013-07-30 17:52:56 -0700 | [diff] [blame] | 224 | (loff_t)(offs + act) << |
| 225 | this->bbt_erase_shift); |
| 226 | bbt_mark_entry(this, offs + act, |
Brian Norris | 771c568 | 2013-07-30 17:52:55 -0700 | [diff] [blame] | 227 | BBT_BLOCK_RESERVED); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 228 | mtd->ecc_stats.bbtblocks++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | continue; |
| 230 | } |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 231 | /* |
| 232 | * Leave it for now, if it's matured we can |
Brian Norris | a0f5080 | 2011-07-19 10:06:06 -0700 | [diff] [blame] | 233 | * move this message to pr_debug. |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 234 | */ |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 235 | pr_info("nand_read_bbt: bad block at 0x%012llx\n", |
Brian Norris | b4d20d6 | 2013-07-30 17:52:56 -0700 | [diff] [blame] | 236 | (loff_t)(offs + act) << |
| 237 | this->bbt_erase_shift); |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 238 | /* Factory marked bad or worn out? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | if (tmp == 0) |
Brian Norris | b4d20d6 | 2013-07-30 17:52:56 -0700 | [diff] [blame] | 240 | bbt_mark_entry(this, offs + act, |
Brian Norris | 771c568 | 2013-07-30 17:52:55 -0700 | [diff] [blame] | 241 | BBT_BLOCK_FACTORY_BAD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | else |
Brian Norris | b4d20d6 | 2013-07-30 17:52:56 -0700 | [diff] [blame] | 243 | bbt_mark_entry(this, offs + act, |
Brian Norris | 771c568 | 2013-07-30 17:52:55 -0700 | [diff] [blame] | 244 | BBT_BLOCK_WORN); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 245 | mtd->ecc_stats.badblocks++; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 246 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } |
| 248 | totlen -= len; |
| 249 | from += len; |
| 250 | } |
Brian Norris | 167a8d5 | 2011-09-20 18:35:08 -0700 | [diff] [blame] | 251 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /** |
| 255 | * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 256 | * @this: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 257 | * @buf: temporary buffer |
| 258 | * @td: descriptor for the bad block table |
Brian Norris | 596d745 | 2011-09-07 13:13:33 -0700 | [diff] [blame] | 259 | * @chip: read the table for a specific chip, -1 read all chips; applies only if |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 260 | * NAND_BBT_PERCHIP option is set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 262 | * Read the bad block table for all chips starting at a given page. We assume |
| 263 | * that the bbt bits are in consecutive order. |
| 264 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 265 | static int read_abs_bbt(struct nand_chip *this, uint8_t *buf, |
| 266 | struct nand_bbt_descr *td, int chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 268 | struct mtd_info *mtd = nand_to_mtd(this); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | int res = 0, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | if (td->options & NAND_BBT_PERCHIP) { |
| 272 | int offs = 0; |
| 273 | for (i = 0; i < this->numchips; i++) { |
| 274 | if (chip == -1 || chip == i) |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 275 | res = read_bbt(this, buf, td->pages[i], |
Sebastian Andrzej Siewior | df5b4e3 | 2010-09-29 19:43:51 +0200 | [diff] [blame] | 276 | this->chipsize >> this->bbt_erase_shift, |
| 277 | td, offs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | if (res) |
| 279 | return res; |
Brian Norris | b4d20d6 | 2013-07-30 17:52:56 -0700 | [diff] [blame] | 280 | offs += this->chipsize >> this->bbt_erase_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | } |
| 282 | } else { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 283 | res = read_bbt(this, buf, td->pages[0], |
Sebastian Andrzej Siewior | df5b4e3 | 2010-09-29 19:43:51 +0200 | [diff] [blame] | 284 | mtd->size >> this->bbt_erase_shift, td, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | if (res) |
| 286 | return res; |
| 287 | } |
| 288 | return 0; |
| 289 | } |
| 290 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 291 | /* BBT marker is in the first page, no OOB */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 292 | static int scan_read_data(struct nand_chip *this, uint8_t *buf, loff_t offs, |
| 293 | struct nand_bbt_descr *td) |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 294 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 295 | struct mtd_info *mtd = nand_to_mtd(this); |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 296 | size_t retlen; |
| 297 | size_t len; |
| 298 | |
| 299 | len = td->len; |
| 300 | if (td->options & NAND_BBT_VERSION) |
| 301 | len++; |
| 302 | |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 303 | return mtd_read(mtd, offs, len, &retlen, buf); |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 304 | } |
| 305 | |
Brian Norris | a7e6883 | 2012-06-22 16:35:45 -0700 | [diff] [blame] | 306 | /** |
Brian Norris | af69dcd | 2012-06-22 16:35:42 -0700 | [diff] [blame] | 307 | * scan_read_oob - [GENERIC] Scan data+OOB region to buffer |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 308 | * @this: NAND chip object |
Brian Norris | a7e6883 | 2012-06-22 16:35:45 -0700 | [diff] [blame] | 309 | * @buf: temporary buffer |
| 310 | * @offs: offset at which to scan |
| 311 | * @len: length of data region to read |
| 312 | * |
| 313 | * Scan read data from data+OOB. May traverse multiple pages, interleaving |
| 314 | * page,OOB,page,OOB,... in buf. Completes transfer and returns the "strongest" |
| 315 | * ECC condition (error or bitflip). May quit on the first (non-ECC) error. |
| 316 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 317 | static int scan_read_oob(struct nand_chip *this, uint8_t *buf, loff_t offs, |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 318 | size_t len) |
| 319 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 320 | struct mtd_info *mtd = nand_to_mtd(this); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 321 | struct mtd_oob_ops ops; |
Brian Norris | a7e6883 | 2012-06-22 16:35:45 -0700 | [diff] [blame] | 322 | int res, ret = 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 323 | |
Brian Norris | a7e6883 | 2012-06-22 16:35:45 -0700 | [diff] [blame] | 324 | ops.mode = MTD_OPS_PLACE_OOB; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 325 | ops.ooboffs = 0; |
| 326 | ops.ooblen = mtd->oobsize; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 327 | |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 328 | while (len > 0) { |
Brian Norris | 105513c | 2011-09-07 13:13:28 -0700 | [diff] [blame] | 329 | ops.datbuf = buf; |
| 330 | ops.len = min(len, (size_t)mtd->writesize); |
| 331 | ops.oobbuf = buf + ops.len; |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 332 | |
Artem Bityutskiy | fd2819b | 2011-12-23 18:27:05 +0200 | [diff] [blame] | 333 | res = mtd_read_oob(mtd, offs, &ops); |
Brian Norris | a7e6883 | 2012-06-22 16:35:45 -0700 | [diff] [blame] | 334 | if (res) { |
| 335 | if (!mtd_is_bitflip_or_eccerr(res)) |
| 336 | return res; |
| 337 | else if (mtd_is_eccerr(res) || !ret) |
| 338 | ret = res; |
| 339 | } |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 340 | |
| 341 | buf += mtd->oobsize + mtd->writesize; |
| 342 | len -= mtd->writesize; |
Dmitry Maluka | 34a5704 | 2012-05-11 20:51:51 +0300 | [diff] [blame] | 343 | offs += mtd->writesize; |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 344 | } |
Brian Norris | a7e6883 | 2012-06-22 16:35:45 -0700 | [diff] [blame] | 345 | return ret; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 346 | } |
| 347 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 348 | static int scan_read(struct nand_chip *this, uint8_t *buf, loff_t offs, |
| 349 | size_t len, struct nand_bbt_descr *td) |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 350 | { |
| 351 | if (td->options & NAND_BBT_NO_OOB) |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 352 | return scan_read_data(this, buf, offs, td); |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 353 | else |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 354 | return scan_read_oob(this, buf, offs, len); |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 355 | } |
| 356 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 357 | /* Scan write data with oob to flash */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 358 | static int scan_write_bbt(struct nand_chip *this, loff_t offs, size_t len, |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 359 | uint8_t *buf, uint8_t *oob) |
| 360 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 361 | struct mtd_info *mtd = nand_to_mtd(this); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 362 | struct mtd_oob_ops ops; |
| 363 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 364 | ops.mode = MTD_OPS_PLACE_OOB; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 365 | ops.ooboffs = 0; |
| 366 | ops.ooblen = mtd->oobsize; |
| 367 | ops.datbuf = buf; |
| 368 | ops.oobbuf = oob; |
| 369 | ops.len = len; |
| 370 | |
Artem Bityutskiy | a2cc5ba | 2011-12-23 18:29:55 +0200 | [diff] [blame] | 371 | return mtd_write_oob(mtd, offs, &ops); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 372 | } |
| 373 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 374 | static u32 bbt_get_ver_offs(struct nand_chip *this, struct nand_bbt_descr *td) |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 375 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 376 | struct mtd_info *mtd = nand_to_mtd(this); |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 377 | u32 ver_offs = td->veroffs; |
| 378 | |
| 379 | if (!(td->options & NAND_BBT_NO_OOB)) |
| 380 | ver_offs += mtd->writesize; |
| 381 | return ver_offs; |
| 382 | } |
| 383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | /** |
| 385 | * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 386 | * @this: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 387 | * @buf: temporary buffer |
| 388 | * @td: descriptor for the bad block table |
| 389 | * @md: descriptor for the bad block table mirror |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 391 | * Read the bad block table(s) for all chips starting at a given page. We |
| 392 | * assume that the bbt bits are in consecutive order. |
| 393 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 394 | static void read_abs_bbts(struct nand_chip *this, uint8_t *buf, |
Brian Norris | 7b5a2d4 | 2012-06-22 16:35:41 -0700 | [diff] [blame] | 395 | struct nand_bbt_descr *td, struct nand_bbt_descr *md) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 397 | struct mtd_info *mtd = nand_to_mtd(this); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 399 | /* Read the primary version, if available */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | if (td->options & NAND_BBT_VERSION) { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 401 | scan_read(this, buf, (loff_t)td->pages[0] << this->page_shift, |
| 402 | mtd->writesize, td); |
| 403 | td->version[0] = buf[bbt_get_ver_offs(this, td)]; |
Brian Norris | 9a4d4d6 | 2011-07-19 10:06:07 -0700 | [diff] [blame] | 404 | pr_info("Bad block table at page %d, version 0x%02X\n", |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 405 | td->pages[0], td->version[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 408 | /* Read the mirror version, if available */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | if (md && (md->options & NAND_BBT_VERSION)) { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 410 | scan_read(this, buf, (loff_t)md->pages[0] << this->page_shift, |
| 411 | mtd->writesize, md); |
| 412 | md->version[0] = buf[bbt_get_ver_offs(this, md)]; |
Brian Norris | 9a4d4d6 | 2011-07-19 10:06:07 -0700 | [diff] [blame] | 413 | pr_info("Bad block table at page %d, version 0x%02X\n", |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 414 | md->pages[0], md->version[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 418 | /* Scan a given block partially */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 419 | static int scan_block_fast(struct nand_chip *this, struct nand_bbt_descr *bd, |
Shmulik Ladkani | eceb84b | 2012-08-20 16:29:15 +0300 | [diff] [blame] | 420 | loff_t offs, uint8_t *buf, int numpages) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 421 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 422 | struct mtd_info *mtd = nand_to_mtd(this); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 423 | struct mtd_oob_ops ops; |
| 424 | int j, ret; |
| 425 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 426 | ops.ooblen = mtd->oobsize; |
| 427 | ops.oobbuf = buf; |
| 428 | ops.ooboffs = 0; |
| 429 | ops.datbuf = NULL; |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 430 | ops.mode = MTD_OPS_PLACE_OOB; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 431 | |
Shmulik Ladkani | eceb84b | 2012-08-20 16:29:15 +0300 | [diff] [blame] | 432 | for (j = 0; j < numpages; j++) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 433 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 434 | * Read the full oob until read_oob is fixed to handle single |
| 435 | * byte reads for 16 bit buswidth. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 436 | */ |
Artem Bityutskiy | fd2819b | 2011-12-23 18:27:05 +0200 | [diff] [blame] | 437 | ret = mtd_read_oob(mtd, offs, &ops); |
Brian Norris | 903cd06 | 2011-06-28 16:28:58 -0700 | [diff] [blame] | 438 | /* Ignore ECC errors when checking for BBM */ |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 439 | if (ret && !mtd_is_bitflip_or_eccerr(ret)) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 440 | return ret; |
| 441 | |
| 442 | if (check_short_pattern(buf, bd)) |
| 443 | return 1; |
| 444 | |
| 445 | offs += mtd->writesize; |
| 446 | } |
| 447 | return 0; |
| 448 | } |
| 449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | /** |
| 451 | * create_bbt - [GENERIC] Create a bad block table by scanning the device |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 452 | * @this: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 453 | * @buf: temporary buffer |
| 454 | * @bd: descriptor for the good/bad block search pattern |
| 455 | * @chip: create the table for a specific chip, -1 read all chips; applies only |
| 456 | * if NAND_BBT_PERCHIP option is set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 458 | * Create a bad block table by scanning the device for the given good/bad block |
| 459 | * identify pattern. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 461 | static int create_bbt(struct nand_chip *this, uint8_t *buf, |
| 462 | struct nand_bbt_descr *bd, int chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 464 | struct mtd_info *mtd = nand_to_mtd(this); |
Brian Norris | 5961ad2 | 2013-10-30 00:41:30 -0400 | [diff] [blame] | 465 | int i, numblocks, numpages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | int startblock; |
| 467 | loff_t from; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | |
Brian Norris | 9a4d4d6 | 2011-07-19 10:06:07 -0700 | [diff] [blame] | 469 | pr_info("Scanning device for bad blocks\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
Brian Norris | 5961ad2 | 2013-10-30 00:41:30 -0400 | [diff] [blame] | 471 | if (bd->options & NAND_BBT_SCAN2NDPAGE) |
Shmulik Ladkani | eceb84b | 2012-08-20 16:29:15 +0300 | [diff] [blame] | 472 | numpages = 2; |
Brian Norris | 58373ff | 2010-07-15 12:15:44 -0700 | [diff] [blame] | 473 | else |
Shmulik Ladkani | eceb84b | 2012-08-20 16:29:15 +0300 | [diff] [blame] | 474 | numpages = 1; |
Artem B. Bityuckiy | 171650a | 2005-02-16 17:09:39 +0000 | [diff] [blame] | 475 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | if (chip == -1) { |
Brian Norris | b4d20d6 | 2013-07-30 17:52:56 -0700 | [diff] [blame] | 477 | numblocks = mtd->size >> this->bbt_erase_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | startblock = 0; |
| 479 | from = 0; |
| 480 | } else { |
| 481 | if (chip >= this->numchips) { |
Brian Norris | 9a4d4d6 | 2011-07-19 10:06:07 -0700 | [diff] [blame] | 482 | pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n", |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 483 | chip + 1, this->numchips); |
Artem B. Bityuckiy | 171650a | 2005-02-16 17:09:39 +0000 | [diff] [blame] | 484 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | } |
Brian Norris | b4d20d6 | 2013-07-30 17:52:56 -0700 | [diff] [blame] | 486 | numblocks = this->chipsize >> this->bbt_erase_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | startblock = chip * numblocks; |
| 488 | numblocks += startblock; |
Brian Norris | b4d20d6 | 2013-07-30 17:52:56 -0700 | [diff] [blame] | 489 | from = (loff_t)startblock << this->bbt_erase_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 491 | |
Brian Norris | 5fb1549 | 2011-05-31 16:31:21 -0700 | [diff] [blame] | 492 | if (this->bbt_options & NAND_BBT_SCANLASTPAGE) |
Shmulik Ladkani | eceb84b | 2012-08-20 16:29:15 +0300 | [diff] [blame] | 493 | from += mtd->erasesize - (mtd->writesize * numpages); |
Kevin Cernekee | b60b08b | 2010-05-04 20:58:10 -0700 | [diff] [blame] | 494 | |
Brian Norris | b4d20d6 | 2013-07-30 17:52:56 -0700 | [diff] [blame] | 495 | for (i = startblock; i < numblocks; i++) { |
Artem B. Bityuckiy | eeada24 | 2005-02-11 10:14:15 +0000 | [diff] [blame] | 496 | int ret; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 497 | |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 498 | BUG_ON(bd->options & NAND_BBT_NO_OOB); |
| 499 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 500 | ret = scan_block_fast(this, bd, from, buf, numpages); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 501 | if (ret < 0) |
| 502 | return ret; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 503 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 504 | if (ret) { |
Brian Norris | b4d20d6 | 2013-07-30 17:52:56 -0700 | [diff] [blame] | 505 | bbt_mark_entry(this, i, BBT_BLOCK_FACTORY_BAD); |
Brian Norris | 9a4d4d6 | 2011-07-19 10:06:07 -0700 | [diff] [blame] | 506 | pr_warn("Bad eraseblock %d at 0x%012llx\n", |
Brian Norris | b4d20d6 | 2013-07-30 17:52:56 -0700 | [diff] [blame] | 507 | i, (unsigned long long)from); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 508 | mtd->ecc_stats.badblocks++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 510 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | from += (1 << this->bbt_erase_shift); |
| 512 | } |
Artem B. Bityuckiy | eeada24 | 2005-02-11 10:14:15 +0000 | [diff] [blame] | 513 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | /** |
| 517 | * search_bbt - [GENERIC] scan the device for a specific bad block table |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 518 | * @this: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 519 | * @buf: temporary buffer |
| 520 | * @td: descriptor for the bad block table |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 522 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 529 | * The bbt ident pattern resides in the oob area of the first page in a block. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 531 | static int search_bbt(struct nand_chip *this, uint8_t *buf, |
| 532 | struct nand_bbt_descr *td) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 534 | struct mtd_info *mtd = nand_to_mtd(this); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | int i, chips; |
Brian Norris | 930de53 | 2014-01-02 17:14:10 -0800 | [diff] [blame] | 536 | int startblock, block, dir; |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 537 | int scanlen = mtd->writesize + mtd->oobsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | int bbtblocks; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 539 | int blocktopage = this->bbt_erase_shift - this->page_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 541 | /* Search direction top -> down? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | if (td->options & NAND_BBT_LASTBLOCK) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 543 | startblock = (mtd->size >> this->bbt_erase_shift) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | dir = -1; |
| 545 | } else { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 546 | startblock = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | dir = 1; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 550 | /* Do we have a bbt per chip? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | if (td->options & NAND_BBT_PERCHIP) { |
| 552 | chips = this->numchips; |
| 553 | bbtblocks = this->chipsize >> this->bbt_erase_shift; |
| 554 | startblock &= bbtblocks - 1; |
| 555 | } else { |
| 556 | chips = 1; |
| 557 | bbtblocks = mtd->size >> this->bbt_erase_shift; |
| 558 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | for (i = 0; i < chips; i++) { |
| 561 | /* Reset version information */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 562 | td->version[i] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | td->pages[i] = -1; |
| 564 | /* Scan the maximum number of blocks */ |
| 565 | for (block = 0; block < td->maxblocks; block++) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | int actblock = startblock + dir * block; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 568 | loff_t offs = (loff_t)actblock << this->bbt_erase_shift; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | /* Read first page */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 571 | scan_read(this, buf, offs, mtd->writesize, td); |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 572 | if (!check_pattern(buf, scanlen, mtd->writesize, td)) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 573 | td->pages[i] = actblock << blocktopage; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | if (td->options & NAND_BBT_VERSION) { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 575 | offs = bbt_get_ver_offs(this, td); |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 576 | td->version[i] = buf[offs]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | } |
| 578 | break; |
| 579 | } |
| 580 | } |
| 581 | startblock += this->chipsize >> this->bbt_erase_shift; |
| 582 | } |
| 583 | /* Check, if we found a bbt for each requested chip */ |
| 584 | for (i = 0; i < chips; i++) { |
| 585 | if (td->pages[i] == -1) |
Brian Norris | 9a4d4d6 | 2011-07-19 10:06:07 -0700 | [diff] [blame] | 586 | pr_warn("Bad block table not found for chip %d\n", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | else |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 588 | pr_info("Bad block table found at page %d, version 0x%02X\n", |
| 589 | td->pages[i], td->version[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 591 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /** |
| 595 | * search_read_bbts - [GENERIC] scan the device for bad block table(s) |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 596 | * @this: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 597 | * @buf: temporary buffer |
| 598 | * @td: descriptor for the bad block table |
| 599 | * @md: descriptor for the bad block table mirror |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 601 | * Search and read the bad block table(s). |
| 602 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 603 | static void search_read_bbts(struct nand_chip *this, uint8_t *buf, |
Brian Norris | 7b5a2d4 | 2012-06-22 16:35:41 -0700 | [diff] [blame] | 604 | struct nand_bbt_descr *td, |
| 605 | struct nand_bbt_descr *md) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | { |
| 607 | /* Search the primary table */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 608 | search_bbt(this, buf, td); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | /* Search the mirror table */ |
| 611 | if (md) |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 612 | search_bbt(this, buf, md); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 615 | /** |
Boris Brezillon | c3baf27 | 2016-08-15 18:22:01 -0500 | [diff] [blame] | 616 | * get_bbt_block - Get the first valid eraseblock suitable to store a BBT |
| 617 | * @this: the NAND device |
| 618 | * @td: the BBT description |
| 619 | * @md: the mirror BBT descriptor |
| 620 | * @chip: the CHIP selector |
| 621 | * |
| 622 | * This functions returns a positive block number pointing a valid eraseblock |
| 623 | * suitable to store a BBT (i.e. in the range reserved for BBT), or -ENOSPC if |
| 624 | * all blocks are already used of marked bad. If td->pages[chip] was already |
| 625 | * pointing to a valid block we re-use it, otherwise we search for the next |
| 626 | * valid one. |
| 627 | */ |
| 628 | static int get_bbt_block(struct nand_chip *this, struct nand_bbt_descr *td, |
| 629 | struct nand_bbt_descr *md, int chip) |
| 630 | { |
| 631 | int startblock, dir, page, numblocks, i; |
| 632 | |
| 633 | /* |
| 634 | * There was already a version of the table, reuse the page. This |
| 635 | * applies for absolute placement too, as we have the page number in |
| 636 | * td->pages. |
| 637 | */ |
| 638 | if (td->pages[chip] != -1) |
| 639 | return td->pages[chip] >> |
| 640 | (this->bbt_erase_shift - this->page_shift); |
| 641 | |
| 642 | numblocks = (int)(this->chipsize >> this->bbt_erase_shift); |
| 643 | if (!(td->options & NAND_BBT_PERCHIP)) |
| 644 | numblocks *= this->numchips; |
| 645 | |
| 646 | /* |
| 647 | * Automatic placement of the bad block table. Search direction |
| 648 | * top -> down? |
| 649 | */ |
| 650 | if (td->options & NAND_BBT_LASTBLOCK) { |
| 651 | startblock = numblocks * (chip + 1) - 1; |
| 652 | dir = -1; |
| 653 | } else { |
| 654 | startblock = chip * numblocks; |
| 655 | dir = 1; |
| 656 | } |
| 657 | |
| 658 | for (i = 0; i < td->maxblocks; i++) { |
| 659 | int block = startblock + dir * i; |
| 660 | |
| 661 | /* Check, if the block is bad */ |
| 662 | switch (bbt_get_entry(this, block)) { |
| 663 | case BBT_BLOCK_WORN: |
| 664 | case BBT_BLOCK_FACTORY_BAD: |
| 665 | continue; |
| 666 | } |
| 667 | |
| 668 | page = block << (this->bbt_erase_shift - this->page_shift); |
| 669 | |
| 670 | /* Check, if the block is used by the mirror table */ |
| 671 | if (!md || md->pages[chip] != page) |
| 672 | return block; |
| 673 | } |
| 674 | |
| 675 | return -ENOSPC; |
| 676 | } |
| 677 | |
| 678 | /** |
Kyle Roeschley | 10ffd57 | 2016-08-15 18:22:02 -0500 | [diff] [blame] | 679 | * mark_bbt_block_bad - Mark one of the block reserved for BBT bad |
| 680 | * @this: the NAND device |
| 681 | * @td: the BBT description |
| 682 | * @chip: the CHIP selector |
| 683 | * @block: the BBT block to mark |
| 684 | * |
| 685 | * Blocks reserved for BBT can become bad. This functions is an helper to mark |
| 686 | * such blocks as bad. It takes care of updating the in-memory BBT, marking the |
| 687 | * block as bad using a bad block marker and invalidating the associated |
| 688 | * td->pages[] entry. |
| 689 | */ |
| 690 | static void mark_bbt_block_bad(struct nand_chip *this, |
| 691 | struct nand_bbt_descr *td, |
| 692 | int chip, int block) |
| 693 | { |
Kyle Roeschley | 10ffd57 | 2016-08-15 18:22:02 -0500 | [diff] [blame] | 694 | loff_t to; |
| 695 | int res; |
| 696 | |
| 697 | bbt_mark_entry(this, block, BBT_BLOCK_WORN); |
| 698 | |
| 699 | to = (loff_t)block << this->bbt_erase_shift; |
Boris Brezillon | cdc784c | 2018-09-07 00:38:38 +0200 | [diff] [blame] | 700 | res = nand_markbad_bbm(this, to); |
Kyle Roeschley | 10ffd57 | 2016-08-15 18:22:02 -0500 | [diff] [blame] | 701 | if (res) |
| 702 | pr_warn("nand_bbt: error %d while marking block %d bad\n", |
| 703 | res, block); |
| 704 | |
| 705 | td->pages[chip] = -1; |
| 706 | } |
| 707 | |
| 708 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | * write_bbt - [GENERIC] (Re)write the bad block table |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 710 | * @this: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 711 | * @buf: temporary buffer |
| 712 | * @td: descriptor for the bad block table |
| 713 | * @md: descriptor for the bad block table mirror |
| 714 | * @chipsel: selector for a specific chip, -1 for all |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 716 | * (Re)write the bad block table. |
| 717 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 718 | static int write_bbt(struct nand_chip *this, uint8_t *buf, |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 719 | struct nand_bbt_descr *td, struct nand_bbt_descr *md, |
| 720 | int chipsel) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 722 | struct mtd_info *mtd = nand_to_mtd(this); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | struct erase_info einfo; |
Brian Norris | b4d20d6 | 2013-07-30 17:52:56 -0700 | [diff] [blame] | 724 | int i, res, chip = 0; |
Boris Brezillon | c3baf27 | 2016-08-15 18:22:01 -0500 | [diff] [blame] | 725 | int bits, page, offs, numblocks, sft, sftmsk; |
Brian Norris | b4d20d6 | 2013-07-30 17:52:56 -0700 | [diff] [blame] | 726 | int nrchips, pageoffs, ooboffs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | uint8_t msk[4]; |
| 728 | uint8_t rcode = td->reserved_block_code; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 729 | size_t retlen, len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | loff_t to; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 731 | struct mtd_oob_ops ops; |
| 732 | |
| 733 | ops.ooblen = mtd->oobsize; |
| 734 | ops.ooboffs = 0; |
| 735 | ops.datbuf = NULL; |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 736 | ops.mode = MTD_OPS_PLACE_OOB; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | |
| 738 | if (!rcode) |
| 739 | rcode = 0xff; |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 740 | /* Write bad block table per chip rather than per device? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | if (td->options & NAND_BBT_PERCHIP) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 742 | numblocks = (int)(this->chipsize >> this->bbt_erase_shift); |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 743 | /* Full device write or specific chip? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | if (chipsel == -1) { |
| 745 | nrchips = this->numchips; |
| 746 | } else { |
| 747 | nrchips = chipsel + 1; |
| 748 | chip = chipsel; |
| 749 | } |
| 750 | } else { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 751 | numblocks = (int)(mtd->size >> this->bbt_erase_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | nrchips = 1; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | /* Loop through the chips */ |
Kyle Roeschley | 10ffd57 | 2016-08-15 18:22:02 -0500 | [diff] [blame] | 756 | while (chip < nrchips) { |
Boris Brezillon | c3baf27 | 2016-08-15 18:22:01 -0500 | [diff] [blame] | 757 | int block; |
| 758 | |
| 759 | block = get_bbt_block(this, td, md, chip); |
| 760 | if (block < 0) { |
| 761 | pr_err("No space left to write bad block table\n"); |
| 762 | res = block; |
| 763 | goto outerr; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 764 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 766 | /* |
Boris Brezillon | c3baf27 | 2016-08-15 18:22:01 -0500 | [diff] [blame] | 767 | * get_bbt_block() returns a block number, shift the value to |
| 768 | * get a page number. |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 769 | */ |
Boris Brezillon | c3baf27 | 2016-08-15 18:22:01 -0500 | [diff] [blame] | 770 | page = block << (this->bbt_erase_shift - this->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | |
| 772 | /* Set up shift count and masks for the flash table */ |
| 773 | bits = td->options & NAND_BBT_NRBITS_MSK; |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 774 | msk[2] = ~rcode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | switch (bits) { |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 776 | case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01; |
| 777 | msk[3] = 0x01; |
| 778 | break; |
| 779 | case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01; |
| 780 | msk[3] = 0x03; |
| 781 | break; |
| 782 | case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C; |
| 783 | msk[3] = 0x0f; |
| 784 | break; |
| 785 | case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F; |
| 786 | msk[3] = 0xff; |
| 787 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | default: return -EINVAL; |
| 789 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 790 | |
Brian Norris | 596d745 | 2011-09-07 13:13:33 -0700 | [diff] [blame] | 791 | to = ((loff_t)page) << this->page_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 793 | /* Must we save the block contents? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | if (td->options & NAND_BBT_SAVECONTENT) { |
| 795 | /* Make it block aligned */ |
Brian Norris | f5cd2ae | 2015-02-28 02:13:13 -0800 | [diff] [blame] | 796 | to &= ~(((loff_t)1 << this->bbt_erase_shift) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | len = 1 << this->bbt_erase_shift; |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 798 | res = mtd_read(mtd, to, len, &retlen, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | if (res < 0) { |
| 800 | if (retlen != len) { |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 801 | pr_info("nand_bbt: error reading block for writing the bad block table\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | return res; |
| 803 | } |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 804 | pr_warn("nand_bbt: ECC error while reading block for writing bad block table\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | } |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 806 | /* Read oob data */ |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 807 | ops.ooblen = (len >> this->page_shift) * mtd->oobsize; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 808 | ops.oobbuf = &buf[len]; |
Artem Bityutskiy | fd2819b | 2011-12-23 18:27:05 +0200 | [diff] [blame] | 809 | res = mtd_read_oob(mtd, to + mtd->writesize, &ops); |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 810 | if (res < 0 || ops.oobretlen != ops.ooblen) |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 811 | goto outerr; |
| 812 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | /* Calc the byte offset in the buffer */ |
| 814 | pageoffs = page - (int)(to >> this->page_shift); |
| 815 | offs = pageoffs << this->page_shift; |
| 816 | /* Preset the bbt area with 0xff */ |
Brian Norris | 596d745 | 2011-09-07 13:13:33 -0700 | [diff] [blame] | 817 | memset(&buf[offs], 0xff, (size_t)(numblocks >> sft)); |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 818 | ooboffs = len + (pageoffs * mtd->oobsize); |
| 819 | |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 820 | } else if (td->options & NAND_BBT_NO_OOB) { |
| 821 | ooboffs = 0; |
| 822 | offs = td->len; |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 823 | /* The version byte */ |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 824 | if (td->options & NAND_BBT_VERSION) |
| 825 | offs++; |
| 826 | /* Calc length */ |
Brian Norris | 596d745 | 2011-09-07 13:13:33 -0700 | [diff] [blame] | 827 | len = (size_t)(numblocks >> sft); |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 828 | len += offs; |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 829 | /* Make it page aligned! */ |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 830 | len = ALIGN(len, mtd->writesize); |
| 831 | /* Preset the buffer with 0xff */ |
| 832 | memset(buf, 0xff, len); |
| 833 | /* Pattern is located at the begin of first page */ |
| 834 | memcpy(buf, td->pattern, td->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | } else { |
| 836 | /* Calc length */ |
Brian Norris | 596d745 | 2011-09-07 13:13:33 -0700 | [diff] [blame] | 837 | len = (size_t)(numblocks >> sft); |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 838 | /* Make it page aligned! */ |
Sebastian Andrzej Siewior | cda3209 | 2010-09-29 19:43:50 +0200 | [diff] [blame] | 839 | len = ALIGN(len, mtd->writesize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | /* Preset the buffer with 0xff */ |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 841 | memset(buf, 0xff, len + |
| 842 | (len >> this->page_shift)* mtd->oobsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | offs = 0; |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 844 | ooboffs = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | /* Pattern is located in oob area of first page */ |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 846 | memcpy(&buf[ooboffs + td->offs], td->pattern, td->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 848 | |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 849 | if (td->options & NAND_BBT_VERSION) |
| 850 | buf[ooboffs + td->veroffs] = td->version[chip]; |
| 851 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 852 | /* Walk through the memory table */ |
Brian Norris | b4d20d6 | 2013-07-30 17:52:56 -0700 | [diff] [blame] | 853 | for (i = 0; i < numblocks; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | uint8_t dat; |
Brian Norris | b4d20d6 | 2013-07-30 17:52:56 -0700 | [diff] [blame] | 855 | int sftcnt = (i << (3 - sft)) & sftmsk; |
| 856 | dat = bbt_get_entry(this, chip * numblocks + i); |
| 857 | /* Do not store the reserved bbt blocks! */ |
| 858 | buf[offs + (i >> sft)] &= ~(msk[dat] << sftcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 860 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 861 | memset(&einfo, 0, sizeof(einfo)); |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 862 | einfo.addr = to; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | einfo.len = 1 << this->bbt_erase_shift; |
Boris Brezillon | e4cdf9c | 2018-09-06 14:05:35 +0200 | [diff] [blame] | 864 | res = nand_erase_nand(this, &einfo, 1); |
Kyle Roeschley | 10ffd57 | 2016-08-15 18:22:02 -0500 | [diff] [blame] | 865 | if (res < 0) { |
| 866 | pr_warn("nand_bbt: error while erasing BBT block %d\n", |
| 867 | res); |
| 868 | mark_bbt_block_bad(this, td, chip, block); |
| 869 | continue; |
| 870 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 871 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 872 | res = scan_write_bbt(this, to, len, buf, |
| 873 | td->options & NAND_BBT_NO_OOB ? |
| 874 | NULL : &buf[len]); |
Kyle Roeschley | 10ffd57 | 2016-08-15 18:22:02 -0500 | [diff] [blame] | 875 | if (res < 0) { |
| 876 | pr_warn("nand_bbt: error while writing BBT block %d\n", |
| 877 | res); |
| 878 | mark_bbt_block_bad(this, td, chip, block); |
| 879 | continue; |
| 880 | } |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 881 | |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 882 | pr_info("Bad block table written to 0x%012llx, version 0x%02X\n", |
| 883 | (unsigned long long)to, td->version[chip]); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | /* Mark it as used */ |
Kyle Roeschley | 10ffd57 | 2016-08-15 18:22:02 -0500 | [diff] [blame] | 886 | td->pages[chip++] = page; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 887 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | return 0; |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 889 | |
| 890 | outerr: |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 891 | pr_warn("nand_bbt: error while writing bad block table %d\n", res); |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 892 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | /** |
| 896 | * nand_memory_bbt - [GENERIC] create a memory based bad block table |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 897 | * @this: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 898 | * @bd: descriptor for the good/bad block search pattern |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 900 | * The function creates a memory based bbt by scanning the device for |
| 901 | * manufacturer / software marked good / bad blocks. |
| 902 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 903 | static inline int nand_memory_bbt(struct nand_chip *this, |
| 904 | struct nand_bbt_descr *bd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 906 | return create_bbt(this, this->data_buf, bd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | /** |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 910 | * check_create - [GENERIC] create and write bbt(s) if necessary |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 911 | * @this: the NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 912 | * @buf: temporary buffer |
| 913 | * @bd: descriptor for the good/bad block search pattern |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 915 | * The function checks the results of the previous call to read_bbt and creates |
| 916 | * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found |
| 917 | * for the chip/device. Update is necessary if one of the tables is missing or |
| 918 | * the version nr. of one table is less than the other. |
| 919 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 920 | static int check_create(struct nand_chip *this, uint8_t *buf, |
| 921 | struct nand_bbt_descr *bd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | { |
Brian Norris | 623978d | 2011-09-20 18:35:34 -0700 | [diff] [blame] | 923 | int i, chips, writeops, create, chipsel, res, res2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | struct nand_bbt_descr *td = this->bbt_td; |
| 925 | struct nand_bbt_descr *md = this->bbt_md; |
| 926 | struct nand_bbt_descr *rd, *rd2; |
| 927 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 928 | /* Do we have a bbt per chip? */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 929 | if (td->options & NAND_BBT_PERCHIP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | chips = this->numchips; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 931 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | chips = 1; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 933 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | for (i = 0; i < chips; i++) { |
| 935 | writeops = 0; |
Brian Norris | b61bf5b | 2011-09-07 13:13:35 -0700 | [diff] [blame] | 936 | create = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | rd = NULL; |
| 938 | rd2 = NULL; |
Brian Norris | 623978d | 2011-09-20 18:35:34 -0700 | [diff] [blame] | 939 | res = res2 = 0; |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 940 | /* Per chip or per device? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1; |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 942 | /* Mirrored table available? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | if (md) { |
| 944 | if (td->pages[i] == -1 && md->pages[i] == -1) { |
Brian Norris | b61bf5b | 2011-09-07 13:13:35 -0700 | [diff] [blame] | 945 | create = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | writeops = 0x03; |
Brian Norris | c5e8ef9 | 2011-09-07 13:13:34 -0700 | [diff] [blame] | 947 | } else if (td->pages[i] == -1) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 948 | rd = md; |
Brian Norris | 596d745 | 2011-09-07 13:13:33 -0700 | [diff] [blame] | 949 | writeops = 0x01; |
Brian Norris | c5e8ef9 | 2011-09-07 13:13:34 -0700 | [diff] [blame] | 950 | } else if (md->pages[i] == -1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | rd = td; |
Brian Norris | 596d745 | 2011-09-07 13:13:33 -0700 | [diff] [blame] | 952 | writeops = 0x02; |
Brian Norris | c5e8ef9 | 2011-09-07 13:13:34 -0700 | [diff] [blame] | 953 | } else if (td->version[i] == md->version[i]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | rd = td; |
| 955 | if (!(td->options & NAND_BBT_VERSION)) |
| 956 | rd2 = md; |
Brian Norris | c5e8ef9 | 2011-09-07 13:13:34 -0700 | [diff] [blame] | 957 | } else if (((int8_t)(td->version[i] - md->version[i])) > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | rd = td; |
Brian Norris | 596d745 | 2011-09-07 13:13:33 -0700 | [diff] [blame] | 959 | writeops = 0x02; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | } else { |
| 961 | rd = md; |
Brian Norris | 596d745 | 2011-09-07 13:13:33 -0700 | [diff] [blame] | 962 | writeops = 0x01; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | } else { |
| 965 | if (td->pages[i] == -1) { |
Brian Norris | b61bf5b | 2011-09-07 13:13:35 -0700 | [diff] [blame] | 966 | create = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | writeops = 0x01; |
Brian Norris | b61bf5b | 2011-09-07 13:13:35 -0700 | [diff] [blame] | 968 | } else { |
| 969 | rd = td; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 972 | |
Brian Norris | b61bf5b | 2011-09-07 13:13:35 -0700 | [diff] [blame] | 973 | if (create) { |
| 974 | /* Create the bad block table by scanning the device? */ |
| 975 | if (!(td->options & NAND_BBT_CREATE)) |
| 976 | continue; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 977 | |
Brian Norris | b61bf5b | 2011-09-07 13:13:35 -0700 | [diff] [blame] | 978 | /* Create the table in memory by scanning the chip(s) */ |
| 979 | if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY)) |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 980 | create_bbt(this, buf, bd, chipsel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | |
Brian Norris | b61bf5b | 2011-09-07 13:13:35 -0700 | [diff] [blame] | 982 | td->version[i] = 1; |
| 983 | if (md) |
| 984 | md->version[i] = 1; |
| 985 | } |
| 986 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 987 | /* Read back first? */ |
Brian Norris | 623978d | 2011-09-20 18:35:34 -0700 | [diff] [blame] | 988 | if (rd) { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 989 | res = read_abs_bbt(this, buf, rd, chipsel); |
Brian Norris | 623978d | 2011-09-20 18:35:34 -0700 | [diff] [blame] | 990 | if (mtd_is_eccerr(res)) { |
| 991 | /* Mark table as invalid */ |
| 992 | rd->pages[i] = -1; |
Brian Norris | dadc17a | 2011-09-20 18:35:57 -0700 | [diff] [blame] | 993 | rd->version[i] = 0; |
Brian Norris | 623978d | 2011-09-20 18:35:34 -0700 | [diff] [blame] | 994 | i--; |
| 995 | continue; |
| 996 | } |
| 997 | } |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 998 | /* If they weren't versioned, read both */ |
Brian Norris | 623978d | 2011-09-20 18:35:34 -0700 | [diff] [blame] | 999 | if (rd2) { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1000 | res2 = read_abs_bbt(this, buf, rd2, chipsel); |
Brian Norris | 623978d | 2011-09-20 18:35:34 -0700 | [diff] [blame] | 1001 | if (mtd_is_eccerr(res2)) { |
| 1002 | /* Mark table as invalid */ |
| 1003 | rd2->pages[i] = -1; |
Brian Norris | dadc17a | 2011-09-20 18:35:57 -0700 | [diff] [blame] | 1004 | rd2->version[i] = 0; |
Brian Norris | 623978d | 2011-09-20 18:35:34 -0700 | [diff] [blame] | 1005 | i--; |
| 1006 | continue; |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | /* Scrub the flash table(s)? */ |
| 1011 | if (mtd_is_bitflip(res) || mtd_is_bitflip(res2)) |
| 1012 | writeops = 0x03; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | |
Brian Norris | dadc17a | 2011-09-20 18:35:57 -0700 | [diff] [blame] | 1014 | /* Update version numbers before writing */ |
| 1015 | if (md) { |
| 1016 | td->version[i] = max(td->version[i], md->version[i]); |
| 1017 | md->version[i] = td->version[i]; |
| 1018 | } |
| 1019 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1020 | /* Write the bad block table to the device? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1022 | res = write_bbt(this, buf, td, md, chipsel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | if (res < 0) |
| 1024 | return res; |
| 1025 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1026 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1027 | /* Write the mirror bad block table to the device? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1029 | res = write_bbt(this, buf, md, td, chipsel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | if (res < 0) |
| 1031 | return res; |
| 1032 | } |
| 1033 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1034 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1038 | * mark_bbt_regions - [GENERIC] mark the bad block table regions |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1039 | * @this: the NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1040 | * @td: bad block table descriptor |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1042 | * The bad block table regions are marked as "bad" to prevent accidental |
| 1043 | * erasures / writes. The regions are identified by the mark 0x02. |
| 1044 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1045 | static void mark_bbt_region(struct nand_chip *this, struct nand_bbt_descr *td) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1047 | struct mtd_info *mtd = nand_to_mtd(this); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | int i, j, chips, block, nrblocks, update; |
Brian Norris | 771c568 | 2013-07-30 17:52:55 -0700 | [diff] [blame] | 1049 | uint8_t oldval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1051 | /* Do we have a bbt per chip? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | if (td->options & NAND_BBT_PERCHIP) { |
| 1053 | chips = this->numchips; |
| 1054 | nrblocks = (int)(this->chipsize >> this->bbt_erase_shift); |
| 1055 | } else { |
| 1056 | chips = 1; |
| 1057 | nrblocks = (int)(mtd->size >> this->bbt_erase_shift); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | for (i = 0; i < chips; i++) { |
| 1061 | if ((td->options & NAND_BBT_ABSPAGE) || |
| 1062 | !(td->options & NAND_BBT_WRITE)) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1063 | if (td->pages[i] == -1) |
| 1064 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift); |
Brian Norris | b4d20d6 | 2013-07-30 17:52:56 -0700 | [diff] [blame] | 1066 | oldval = bbt_get_entry(this, block); |
| 1067 | bbt_mark_entry(this, block, BBT_BLOCK_RESERVED); |
Brian Norris | 771c568 | 2013-07-30 17:52:55 -0700 | [diff] [blame] | 1068 | if ((oldval != BBT_BLOCK_RESERVED) && |
| 1069 | td->reserved_block_code) |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1070 | nand_update_bbt(this, (loff_t)block << |
Brian Norris | b4d20d6 | 2013-07-30 17:52:56 -0700 | [diff] [blame] | 1071 | this->bbt_erase_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | continue; |
| 1073 | } |
| 1074 | update = 0; |
| 1075 | if (td->options & NAND_BBT_LASTBLOCK) |
| 1076 | block = ((i + 1) * nrblocks) - td->maxblocks; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1077 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | block = i * nrblocks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | for (j = 0; j < td->maxblocks; j++) { |
Brian Norris | b4d20d6 | 2013-07-30 17:52:56 -0700 | [diff] [blame] | 1080 | oldval = bbt_get_entry(this, block); |
| 1081 | bbt_mark_entry(this, block, BBT_BLOCK_RESERVED); |
Brian Norris | 771c568 | 2013-07-30 17:52:55 -0700 | [diff] [blame] | 1082 | if (oldval != BBT_BLOCK_RESERVED) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1083 | update = 1; |
Brian Norris | b4d20d6 | 2013-07-30 17:52:56 -0700 | [diff] [blame] | 1084 | block++; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1085 | } |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1086 | /* |
| 1087 | * If we want reserved blocks to be recorded to flash, and some |
| 1088 | * new ones have been marked, then we need to update the stored |
| 1089 | * bbts. This should only happen once. |
| 1090 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | if (update && td->reserved_block_code) |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1092 | nand_update_bbt(this, (loff_t)(block - 1) << |
Brian Norris | b4d20d6 | 2013-07-30 17:52:56 -0700 | [diff] [blame] | 1093 | this->bbt_erase_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | /** |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 1098 | * verify_bbt_descr - verify the bad block description |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1099 | * @this: the NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1100 | * @bd: the table to verify |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 1101 | * |
| 1102 | * This functions performs a few sanity checks on the bad block description |
| 1103 | * table. |
| 1104 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1105 | static void verify_bbt_descr(struct nand_chip *this, struct nand_bbt_descr *bd) |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 1106 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1107 | struct mtd_info *mtd = nand_to_mtd(this); |
Stanislav Fomichev | 7912a5e | 2011-02-07 23:48:25 +0300 | [diff] [blame] | 1108 | u32 pattern_len; |
| 1109 | u32 bits; |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 1110 | u32 table_size; |
| 1111 | |
| 1112 | if (!bd) |
| 1113 | return; |
Stanislav Fomichev | 7912a5e | 2011-02-07 23:48:25 +0300 | [diff] [blame] | 1114 | |
| 1115 | pattern_len = bd->len; |
| 1116 | bits = bd->options & NAND_BBT_NRBITS_MSK; |
| 1117 | |
Brian Norris | a40f734 | 2011-05-31 16:31:22 -0700 | [diff] [blame] | 1118 | BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) && |
Brian Norris | bb9ebd4 | 2011-05-31 16:31:23 -0700 | [diff] [blame] | 1119 | !(this->bbt_options & NAND_BBT_USE_FLASH)); |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 1120 | BUG_ON(!bits); |
| 1121 | |
| 1122 | if (bd->options & NAND_BBT_VERSION) |
| 1123 | pattern_len++; |
| 1124 | |
| 1125 | if (bd->options & NAND_BBT_NO_OOB) { |
Brian Norris | bb9ebd4 | 2011-05-31 16:31:23 -0700 | [diff] [blame] | 1126 | BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH)); |
Brian Norris | a40f734 | 2011-05-31 16:31:22 -0700 | [diff] [blame] | 1127 | BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB)); |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 1128 | BUG_ON(bd->offs); |
| 1129 | if (bd->options & NAND_BBT_VERSION) |
| 1130 | BUG_ON(bd->veroffs != bd->len); |
| 1131 | BUG_ON(bd->options & NAND_BBT_SAVECONTENT); |
| 1132 | } |
| 1133 | |
| 1134 | if (bd->options & NAND_BBT_PERCHIP) |
| 1135 | table_size = this->chipsize >> this->bbt_erase_shift; |
| 1136 | else |
| 1137 | table_size = mtd->size >> this->bbt_erase_shift; |
| 1138 | table_size >>= 3; |
| 1139 | table_size *= bits; |
| 1140 | if (bd->options & NAND_BBT_NO_OOB) |
| 1141 | table_size += pattern_len; |
| 1142 | BUG_ON(table_size > (1 << this->bbt_erase_shift)); |
| 1143 | } |
| 1144 | |
| 1145 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s) |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1147 | * @this: the NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1148 | * @bd: descriptor for the good/bad block search pattern |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1150 | * The function checks, if a bad block table(s) is/are already available. If |
| 1151 | * not it scans the device for manufacturer marked good / bad blocks and writes |
| 1152 | * the bad block table(s) to the selected place. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1154 | * The bad block table memory is allocated here. It must be freed by calling |
| 1155 | * the nand_free_bbt function. |
| 1156 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1157 | static int nand_scan_bbt(struct nand_chip *this, struct nand_bbt_descr *bd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1159 | struct mtd_info *mtd = nand_to_mtd(this); |
Brian Norris | 83c5954 | 2015-02-28 02:13:12 -0800 | [diff] [blame] | 1160 | int len, res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | uint8_t *buf; |
| 1162 | struct nand_bbt_descr *td = this->bbt_td; |
| 1163 | struct nand_bbt_descr *md = this->bbt_md; |
| 1164 | |
Sheng Yong | 192db1c | 2015-07-31 01:12:44 +0000 | [diff] [blame] | 1165 | len = (mtd->size >> (this->bbt_erase_shift + 2)) ? : 1; |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1166 | /* |
| 1167 | * Allocate memory (2bit per block) and clear the memory bad block |
| 1168 | * table. |
| 1169 | */ |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 1170 | this->bbt = kzalloc(len, GFP_KERNEL); |
Brian Norris | 0870066 | 2011-06-07 16:01:54 -0700 | [diff] [blame] | 1171 | if (!this->bbt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1174 | /* |
| 1175 | * If no primary table decriptor is given, scan the device to build a |
| 1176 | * memory based bad block table. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | */ |
Artem B. Bityuckiy | eeada24 | 2005-02-11 10:14:15 +0000 | [diff] [blame] | 1178 | if (!td) { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1179 | if ((res = nand_memory_bbt(this, bd))) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 1180 | pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n"); |
Brian Norris | 83c5954 | 2015-02-28 02:13:12 -0800 | [diff] [blame] | 1181 | goto err; |
Artem B. Bityuckiy | eeada24 | 2005-02-11 10:14:15 +0000 | [diff] [blame] | 1182 | } |
Brian Norris | 83c5954 | 2015-02-28 02:13:12 -0800 | [diff] [blame] | 1183 | return 0; |
Artem B. Bityuckiy | eeada24 | 2005-02-11 10:14:15 +0000 | [diff] [blame] | 1184 | } |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1185 | verify_bbt_descr(this, td); |
| 1186 | verify_bbt_descr(this, md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | |
| 1188 | /* Allocate a temporary buffer for one eraseblock incl. oob */ |
| 1189 | len = (1 << this->bbt_erase_shift); |
| 1190 | len += (len >> this->page_shift) * mtd->oobsize; |
David Woodhouse | c3f8abf | 2006-05-13 04:03:42 +0100 | [diff] [blame] | 1191 | buf = vmalloc(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | if (!buf) { |
Brian Norris | 83c5954 | 2015-02-28 02:13:12 -0800 | [diff] [blame] | 1193 | res = -ENOMEM; |
| 1194 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1196 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1197 | /* Is the bbt at a given page? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | if (td->options & NAND_BBT_ABSPAGE) { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1199 | read_abs_bbts(this, buf, td, md); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1200 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | /* Search the bad block table using a pattern in oob */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1202 | search_read_bbts(this, buf, td, md); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1203 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1205 | res = check_create(this, buf, bd); |
Brian Norris | 83c5954 | 2015-02-28 02:13:12 -0800 | [diff] [blame] | 1206 | if (res) |
| 1207 | goto err; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | /* Prevent the bbt regions from erasing / writing */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1210 | mark_bbt_region(this, td); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | if (md) |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1212 | mark_bbt_region(this, md); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1213 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1214 | vfree(buf); |
Brian Norris | 83c5954 | 2015-02-28 02:13:12 -0800 | [diff] [blame] | 1215 | return 0; |
| 1216 | |
| 1217 | err: |
| 1218 | kfree(this->bbt); |
| 1219 | this->bbt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | return res; |
| 1221 | } |
| 1222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | /** |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 1224 | * nand_update_bbt - update bad block table(s) |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1225 | * @this: the NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1226 | * @offs: the offset of the newly marked block |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1228 | * The function updates the bad block table(s). |
| 1229 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1230 | static int nand_update_bbt(struct nand_chip *this, loff_t offs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1232 | struct mtd_info *mtd = nand_to_mtd(this); |
Brian Norris | 1196ac5 | 2011-09-07 13:13:32 -0700 | [diff] [blame] | 1233 | int len, res = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | int chip, chipsel; |
| 1235 | uint8_t *buf; |
| 1236 | struct nand_bbt_descr *td = this->bbt_td; |
| 1237 | struct nand_bbt_descr *md = this->bbt_md; |
| 1238 | |
| 1239 | if (!this->bbt || !td) |
| 1240 | return -EINVAL; |
| 1241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | /* Allocate a temporary buffer for one eraseblock incl. oob */ |
| 1243 | len = (1 << this->bbt_erase_shift); |
| 1244 | len += (len >> this->page_shift) * mtd->oobsize; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1245 | buf = kmalloc(len, GFP_KERNEL); |
Brian Norris | 0870066 | 2011-06-07 16:01:54 -0700 | [diff] [blame] | 1246 | if (!buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | return -ENOMEM; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1248 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1249 | /* Do we have a bbt per chip? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | if (td->options & NAND_BBT_PERCHIP) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1251 | chip = (int)(offs >> this->chip_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | chipsel = chip; |
| 1253 | } else { |
| 1254 | chip = 0; |
| 1255 | chipsel = -1; |
| 1256 | } |
| 1257 | |
| 1258 | td->version[chip]++; |
| 1259 | if (md) |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1260 | md->version[chip]++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1262 | /* Write the bad block table to the device? */ |
Brian Norris | 1196ac5 | 2011-09-07 13:13:32 -0700 | [diff] [blame] | 1263 | if (td->options & NAND_BBT_WRITE) { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1264 | res = write_bbt(this, buf, td, md, chipsel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | if (res < 0) |
| 1266 | goto out; |
| 1267 | } |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1268 | /* Write the mirror bad block table to the device? */ |
Brian Norris | 1196ac5 | 2011-09-07 13:13:32 -0700 | [diff] [blame] | 1269 | if (md && (md->options & NAND_BBT_WRITE)) { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1270 | res = write_bbt(this, buf, md, td, chipsel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1273 | out: |
| 1274 | kfree(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | return res; |
| 1276 | } |
| 1277 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1278 | /* |
| 1279 | * Define some generic bad / good block scan pattern which are used |
| 1280 | * while scanning a device for factory marked good / bad blocks. |
| 1281 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | static uint8_t scan_ff_pattern[] = { 0xff, 0xff }; |
| 1283 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1284 | /* Generic flash bbt descriptors */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' }; |
| 1286 | static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' }; |
| 1287 | |
| 1288 | static struct nand_bbt_descr bbt_main_descr = { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1289 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, |
| 1291 | .offs = 8, |
| 1292 | .len = 4, |
| 1293 | .veroffs = 12, |
Richard Genoud | 61de9da | 2012-09-11 15:50:54 +0200 | [diff] [blame] | 1294 | .maxblocks = NAND_BBT_SCAN_MAXBLOCKS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | .pattern = bbt_pattern |
| 1296 | }; |
| 1297 | |
| 1298 | static struct nand_bbt_descr bbt_mirror_descr = { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1299 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, |
| 1301 | .offs = 8, |
| 1302 | .len = 4, |
| 1303 | .veroffs = 12, |
Richard Genoud | 61de9da | 2012-09-11 15:50:54 +0200 | [diff] [blame] | 1304 | .maxblocks = NAND_BBT_SCAN_MAXBLOCKS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | .pattern = mirror_pattern |
| 1306 | }; |
| 1307 | |
Brian Norris | 9fd6b37 | 2012-06-22 16:35:40 -0700 | [diff] [blame] | 1308 | static struct nand_bbt_descr bbt_main_no_oob_descr = { |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 1309 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
| 1310 | | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP |
| 1311 | | NAND_BBT_NO_OOB, |
| 1312 | .len = 4, |
| 1313 | .veroffs = 4, |
Richard Genoud | 61de9da | 2012-09-11 15:50:54 +0200 | [diff] [blame] | 1314 | .maxblocks = NAND_BBT_SCAN_MAXBLOCKS, |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 1315 | .pattern = bbt_pattern |
| 1316 | }; |
| 1317 | |
Brian Norris | 9fd6b37 | 2012-06-22 16:35:40 -0700 | [diff] [blame] | 1318 | static struct nand_bbt_descr bbt_mirror_no_oob_descr = { |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 1319 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
| 1320 | | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP |
| 1321 | | NAND_BBT_NO_OOB, |
| 1322 | .len = 4, |
| 1323 | .veroffs = 4, |
Richard Genoud | 61de9da | 2012-09-11 15:50:54 +0200 | [diff] [blame] | 1324 | .maxblocks = NAND_BBT_SCAN_MAXBLOCKS, |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 1325 | .pattern = mirror_pattern |
| 1326 | }; |
| 1327 | |
Brian Norris | 752ed6c | 2011-09-20 18:36:42 -0700 | [diff] [blame] | 1328 | #define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB) |
Brian Norris | 58373ff | 2010-07-15 12:15:44 -0700 | [diff] [blame] | 1329 | /** |
Brian Norris | 752ed6c | 2011-09-20 18:36:42 -0700 | [diff] [blame] | 1330 | * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1331 | * @this: NAND chip to create descriptor for |
Brian Norris | 58373ff | 2010-07-15 12:15:44 -0700 | [diff] [blame] | 1332 | * |
| 1333 | * This function allocates and initializes a nand_bbt_descr for BBM detection |
Brian Norris | 752ed6c | 2011-09-20 18:36:42 -0700 | [diff] [blame] | 1334 | * based on the properties of @this. The new descriptor is stored in |
Brian Norris | 58373ff | 2010-07-15 12:15:44 -0700 | [diff] [blame] | 1335 | * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when |
| 1336 | * passed to this function. |
Brian Norris | 58373ff | 2010-07-15 12:15:44 -0700 | [diff] [blame] | 1337 | */ |
Brian Norris | 752ed6c | 2011-09-20 18:36:42 -0700 | [diff] [blame] | 1338 | static int nand_create_badblock_pattern(struct nand_chip *this) |
Brian Norris | 58373ff | 2010-07-15 12:15:44 -0700 | [diff] [blame] | 1339 | { |
| 1340 | struct nand_bbt_descr *bd; |
| 1341 | if (this->badblock_pattern) { |
Brian Norris | 752ed6c | 2011-09-20 18:36:42 -0700 | [diff] [blame] | 1342 | pr_warn("Bad block pattern already allocated; not replacing\n"); |
Brian Norris | 58373ff | 2010-07-15 12:15:44 -0700 | [diff] [blame] | 1343 | return -EINVAL; |
| 1344 | } |
| 1345 | bd = kzalloc(sizeof(*bd), GFP_KERNEL); |
Brian Norris | 0870066 | 2011-06-07 16:01:54 -0700 | [diff] [blame] | 1346 | if (!bd) |
Brian Norris | 58373ff | 2010-07-15 12:15:44 -0700 | [diff] [blame] | 1347 | return -ENOMEM; |
Brian Norris | 752ed6c | 2011-09-20 18:36:42 -0700 | [diff] [blame] | 1348 | bd->options = this->bbt_options & BADBLOCK_SCAN_MASK; |
Brian Norris | 58373ff | 2010-07-15 12:15:44 -0700 | [diff] [blame] | 1349 | bd->offs = this->badblockpos; |
| 1350 | bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1; |
| 1351 | bd->pattern = scan_ff_pattern; |
| 1352 | bd->options |= NAND_BBT_DYNAMICSTRUCT; |
| 1353 | this->badblock_pattern = bd; |
| 1354 | return 0; |
| 1355 | } |
| 1356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | /** |
Boris Brezillon | 44b07b9 | 2018-07-05 12:27:30 +0200 | [diff] [blame] | 1358 | * nand_create_bbt - [NAND Interface] Select a default bad block table for the device |
| 1359 | * @this: NAND chip object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1361 | * This function selects the default bad block table support for the device and |
| 1362 | * calls the nand_scan_bbt function. |
| 1363 | */ |
Boris Brezillon | 44b07b9 | 2018-07-05 12:27:30 +0200 | [diff] [blame] | 1364 | int nand_create_bbt(struct nand_chip *this) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | { |
Brian Norris | abb9cf7 | 2014-05-20 22:34:40 -0700 | [diff] [blame] | 1366 | int ret; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1367 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1368 | /* Is a flash based bad block table requested? */ |
Brian Norris | bb9ebd4 | 2011-05-31 16:31:23 -0700 | [diff] [blame] | 1369 | if (this->bbt_options & NAND_BBT_USE_FLASH) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1370 | /* Use the default pattern descriptors */ |
| 1371 | if (!this->bbt_td) { |
Brian Norris | a40f734 | 2011-05-31 16:31:22 -0700 | [diff] [blame] | 1372 | if (this->bbt_options & NAND_BBT_NO_OOB) { |
Brian Norris | 9fd6b37 | 2012-06-22 16:35:40 -0700 | [diff] [blame] | 1373 | this->bbt_td = &bbt_main_no_oob_descr; |
| 1374 | this->bbt_md = &bbt_mirror_no_oob_descr; |
Sebastian Andrzej Siewior | 7cba7b1 | 2010-09-30 21:28:01 +0200 | [diff] [blame] | 1375 | } else { |
| 1376 | this->bbt_td = &bbt_main_descr; |
| 1377 | this->bbt_md = &bbt_mirror_descr; |
| 1378 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | } else { |
| 1381 | this->bbt_td = NULL; |
| 1382 | this->bbt_md = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | } |
Brian Norris | a2f812d | 2011-03-18 21:53:42 -0700 | [diff] [blame] | 1384 | |
Brian Norris | abb9cf7 | 2014-05-20 22:34:40 -0700 | [diff] [blame] | 1385 | if (!this->badblock_pattern) { |
| 1386 | ret = nand_create_badblock_pattern(this); |
| 1387 | if (ret) |
| 1388 | return ret; |
| 1389 | } |
Brian Norris | a2f812d | 2011-03-18 21:53:42 -0700 | [diff] [blame] | 1390 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1391 | return nand_scan_bbt(this, this->badblock_pattern); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | } |
Boris Brezillon | 44b07b9 | 2018-07-05 12:27:30 +0200 | [diff] [blame] | 1393 | EXPORT_SYMBOL(nand_create_bbt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | |
| 1395 | /** |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 1396 | * nand_isreserved_bbt - [NAND Interface] Check if a block is reserved |
Boris Brezillon | 5740d4c | 2018-09-06 14:05:34 +0200 | [diff] [blame] | 1397 | * @this: NAND chip object |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 1398 | * @offs: offset in the device |
| 1399 | */ |
Boris Brezillon | 5740d4c | 2018-09-06 14:05:34 +0200 | [diff] [blame] | 1400 | int nand_isreserved_bbt(struct nand_chip *this, loff_t offs) |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 1401 | { |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 1402 | int block; |
| 1403 | |
| 1404 | block = (int)(offs >> this->bbt_erase_shift); |
| 1405 | return bbt_get_entry(this, block) == BBT_BLOCK_RESERVED; |
| 1406 | } |
| 1407 | |
| 1408 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1409 | * nand_isbad_bbt - [NAND Interface] Check if a block is bad |
Boris Brezillon | 5740d4c | 2018-09-06 14:05:34 +0200 | [diff] [blame] | 1410 | * @this: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1411 | * @offs: offset in the device |
| 1412 | * @allowbbt: allow access to bad block table region |
| 1413 | */ |
Boris Brezillon | 5740d4c | 2018-09-06 14:05:34 +0200 | [diff] [blame] | 1414 | int nand_isbad_bbt(struct nand_chip *this, loff_t offs, int allowbbt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | { |
Brian Norris | 39dbb02 | 2013-07-30 17:52:57 -0700 | [diff] [blame] | 1416 | int block, res; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1417 | |
Brian Norris | b4d20d6 | 2013-07-30 17:52:56 -0700 | [diff] [blame] | 1418 | block = (int)(offs >> this->bbt_erase_shift); |
| 1419 | res = bbt_get_entry(this, block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 1421 | pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n", |
| 1422 | (unsigned int)offs, block, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | |
Brian Norris | 39dbb02 | 2013-07-30 17:52:57 -0700 | [diff] [blame] | 1424 | switch (res) { |
Brian Norris | 771c568 | 2013-07-30 17:52:55 -0700 | [diff] [blame] | 1425 | case BBT_BLOCK_GOOD: |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1426 | return 0; |
Brian Norris | 771c568 | 2013-07-30 17:52:55 -0700 | [diff] [blame] | 1427 | case BBT_BLOCK_WORN: |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1428 | return 1; |
Brian Norris | 771c568 | 2013-07-30 17:52:55 -0700 | [diff] [blame] | 1429 | case BBT_BLOCK_RESERVED: |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1430 | return allowbbt ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | } |
| 1432 | return 1; |
| 1433 | } |
| 1434 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 1435 | /** |
| 1436 | * nand_markbad_bbt - [NAND Interface] Mark a block bad in the BBT |
Boris Brezillon | 5740d4c | 2018-09-06 14:05:34 +0200 | [diff] [blame] | 1437 | * @this: NAND chip object |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 1438 | * @offs: offset of the bad block |
| 1439 | */ |
Boris Brezillon | 5740d4c | 2018-09-06 14:05:34 +0200 | [diff] [blame] | 1440 | int nand_markbad_bbt(struct nand_chip *this, loff_t offs) |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 1441 | { |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 1442 | int block, ret = 0; |
| 1443 | |
| 1444 | block = (int)(offs >> this->bbt_erase_shift); |
| 1445 | |
| 1446 | /* Mark bad block in memory */ |
| 1447 | bbt_mark_entry(this, block, BBT_BLOCK_WORN); |
| 1448 | |
| 1449 | /* Update flash-based bad block table */ |
| 1450 | if (this->bbt_options & NAND_BBT_USE_FLASH) |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 1451 | ret = nand_update_bbt(this, offs); |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 1452 | |
| 1453 | return ret; |
| 1454 | } |