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