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