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 | ac7eb8a | 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 | * |
| 9 | * $Id: nand_bbt.c,v 1.28 2004/11/13 10:19:09 gleixner Exp $ |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2 as |
| 13 | * published by the Free Software Foundation. |
| 14 | * |
| 15 | * Description: |
| 16 | * |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 17 | * When nand_scan_bbt is called, then it tries to find the bad block table |
| 18 | * depending on the options in the bbt descriptor(s). If a bbt is found |
| 19 | * then the contents are read and the memory based bbt is created. If a |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 20 | * mirrored bbt is selected then the mirror is searched too and the |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 21 | * versions are compared. If the mirror has a greater version number |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 22 | * than the mirror bbt is used to build the memory based bbt. |
| 23 | * If the tables are not versioned, then we "or" the bad block information. |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 24 | * If one of the bbt's is out of date or does not exist it is (re)created. |
| 25 | * If no bbt exists at all then the device is scanned for factory marked |
| 26 | * good / bad blocks and the bad block tables are created. |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 27 | * |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 28 | * For manufacturer created bbts like the one found on M-SYS DOC devices |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 29 | * the bbt is searched and read but never created |
| 30 | * |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 31 | * The autogenerated bad block table is located in the last good blocks |
| 32 | * of the device. The table is mirrored, so it can be updated eventually. |
| 33 | * The table is marked in the oob area with an ident pattern and a version |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 34 | * number which indicates which of both tables is more up to date. |
| 35 | * |
| 36 | * The table uses 2 bits per block |
| 37 | * 11b: block is good |
| 38 | * 00b: block is factory marked bad |
| 39 | * 01b, 10b: block is marked bad due to wear |
| 40 | * |
| 41 | * The memory bad block table uses the following scheme: |
| 42 | * 00b: block is good |
| 43 | * 01b: block is marked bad due to wear |
| 44 | * 10b: block is reserved (to protect the bbt area) |
| 45 | * 11b: block is factory marked bad |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 46 | * |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 47 | * Multichip devices like DOC store the bad block info per floor. |
| 48 | * |
| 49 | * Following assumptions are made: |
| 50 | * - bbts start at a page boundary, if autolocated on a block boundary |
| 51 | * - the space neccecary for a bbt in FLASH does not exceed a block boundary |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 52 | * |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 53 | */ |
| 54 | |
| 55 | #include <common.h> |
| 56 | |
Bartlomiej Sieka | addb2e1 | 2006-03-05 18:57:33 +0100 | [diff] [blame] | 57 | #ifdef CFG_NAND_LEGACY |
| 58 | #error CFG_NAND_LEGACY defined in a file not using the legacy NAND support! |
| 59 | #endif |
| 60 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 61 | #if (CONFIG_COMMANDS & CFG_CMD_NAND) |
| 62 | |
| 63 | #include <malloc.h> |
| 64 | #include <linux/mtd/compat.h> |
| 65 | #include <linux/mtd/mtd.h> |
| 66 | #include <linux/mtd/nand.h> |
| 67 | |
| 68 | #include <asm/errno.h> |
| 69 | |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 70 | /** |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 71 | * check_pattern - [GENERIC] check if a pattern is in the buffer |
| 72 | * @buf: the buffer to search |
| 73 | * @len: the length of buffer to search |
| 74 | * @paglen: the pagelength |
| 75 | * @td: search pattern descriptor |
| 76 | * |
| 77 | * Check for a pattern at the given place. Used to search bad block |
| 78 | * tables and good / bad block identifiers. |
| 79 | * If the SCAN_EMPTY option is set then check, if all bytes except the |
| 80 | * pattern area contain 0xff |
| 81 | * |
| 82 | */ |
| 83 | static int check_pattern (uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td) |
| 84 | { |
| 85 | int i, end; |
| 86 | uint8_t *p = buf; |
| 87 | |
| 88 | end = paglen + td->offs; |
| 89 | if (td->options & NAND_BBT_SCANEMPTY) { |
| 90 | for (i = 0; i < end; i++) { |
| 91 | if (p[i] != 0xff) |
| 92 | return -1; |
| 93 | } |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 94 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 95 | p += end; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 96 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 97 | /* Compare the pattern */ |
| 98 | for (i = 0; i < td->len; i++) { |
| 99 | if (p[i] != td->pattern[i]) |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | p += td->len; |
| 104 | end += td->len; |
| 105 | if (td->options & NAND_BBT_SCANEMPTY) { |
| 106 | for (i = end; i < len; i++) { |
| 107 | if (*p++ != 0xff) |
| 108 | return -1; |
| 109 | } |
| 110 | } |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * read_bbt - [GENERIC] Read the bad block table starting from page |
| 116 | * @mtd: MTD device structure |
| 117 | * @buf: temporary buffer |
| 118 | * @page: the starting page |
| 119 | * @num: the number of bbt descriptors to read |
| 120 | * @bits: number of bits per block |
| 121 | * @offs: offset in the memory table |
| 122 | * @reserved_block_code: Pattern to identify reserved blocks |
| 123 | * |
| 124 | * Read the bad block table starting from page. |
| 125 | * |
| 126 | */ |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 127 | static int read_bbt (struct mtd_info *mtd, uint8_t *buf, int page, int num, |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 128 | int bits, int offs, int reserved_block_code) |
| 129 | { |
| 130 | int res, i, j, act = 0; |
| 131 | struct nand_chip *this = mtd->priv; |
| 132 | size_t retlen, len, totlen; |
| 133 | loff_t from; |
| 134 | uint8_t msk = (uint8_t) ((1 << bits) - 1); |
| 135 | |
| 136 | totlen = (num * bits) >> 3; |
| 137 | from = ((loff_t)page) << this->page_shift; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 138 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 139 | while (totlen) { |
| 140 | len = min (totlen, (size_t) (1 << this->bbt_erase_shift)); |
| 141 | res = mtd->read_ecc (mtd, from, len, &retlen, buf, NULL, this->autooob); |
| 142 | if (res < 0) { |
| 143 | if (retlen != len) { |
| 144 | printk (KERN_INFO "nand_bbt: Error reading bad block table\n"); |
| 145 | return res; |
| 146 | } |
| 147 | printk (KERN_WARNING "nand_bbt: ECC error while reading bad block table\n"); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 148 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 149 | |
| 150 | /* Analyse data */ |
| 151 | for (i = 0; i < len; i++) { |
| 152 | uint8_t dat = buf[i]; |
| 153 | for (j = 0; j < 8; j += bits, act += 2) { |
| 154 | uint8_t tmp = (dat >> j) & msk; |
| 155 | if (tmp == msk) |
| 156 | continue; |
| 157 | if (reserved_block_code && |
| 158 | (tmp == reserved_block_code)) { |
| 159 | printk (KERN_DEBUG "nand_read_bbt: Reserved block at 0x%08x\n", |
| 160 | ((offs << 2) + (act >> 1)) << this->bbt_erase_shift); |
| 161 | this->bbt[offs + (act >> 3)] |= 0x2 << (act & 0x06); |
| 162 | continue; |
| 163 | } |
| 164 | /* Leave it for now, if its matured we can move this |
| 165 | * message to MTD_DEBUG_LEVEL0 */ |
| 166 | printk (KERN_DEBUG "nand_read_bbt: Bad block at 0x%08x\n", |
| 167 | ((offs << 2) + (act >> 1)) << this->bbt_erase_shift); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 168 | /* Factory marked bad or worn out ? */ |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 169 | if (tmp == 0) |
| 170 | this->bbt[offs + (act >> 3)] |= 0x3 << (act & 0x06); |
| 171 | else |
| 172 | this->bbt[offs + (act >> 3)] |= 0x1 << (act & 0x06); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 173 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 174 | } |
| 175 | totlen -= len; |
| 176 | from += len; |
| 177 | } |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page |
| 183 | * @mtd: MTD device structure |
| 184 | * @buf: temporary buffer |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 185 | * @td: descriptor for the bad block table |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 186 | * @chip: read the table for a specific chip, -1 read all chips. |
| 187 | * Applies only if NAND_BBT_PERCHIP option is set |
| 188 | * |
| 189 | * Read the bad block table for all chips starting at a given page |
| 190 | * We assume that the bbt bits are in consecutive order. |
| 191 | */ |
| 192 | static int read_abs_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip) |
| 193 | { |
| 194 | struct nand_chip *this = mtd->priv; |
| 195 | int res = 0, i; |
| 196 | int bits; |
| 197 | |
| 198 | bits = td->options & NAND_BBT_NRBITS_MSK; |
| 199 | if (td->options & NAND_BBT_PERCHIP) { |
| 200 | int offs = 0; |
| 201 | for (i = 0; i < this->numchips; i++) { |
| 202 | if (chip == -1 || chip == i) |
| 203 | res = read_bbt (mtd, buf, td->pages[i], this->chipsize >> this->bbt_erase_shift, bits, offs, td->reserved_block_code); |
| 204 | if (res) |
| 205 | return res; |
| 206 | offs += this->chipsize >> (this->bbt_erase_shift + 2); |
| 207 | } |
| 208 | } else { |
| 209 | res = read_bbt (mtd, buf, td->pages[0], mtd->size >> this->bbt_erase_shift, bits, 0, td->reserved_block_code); |
| 210 | if (res) |
| 211 | return res; |
| 212 | } |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page |
| 218 | * @mtd: MTD device structure |
| 219 | * @buf: temporary buffer |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 220 | * @td: descriptor for the bad block table |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 221 | * @md: descriptor for the bad block table mirror |
| 222 | * |
| 223 | * Read the bad block table(s) for all chips starting at a given page |
| 224 | * We assume that the bbt bits are in consecutive order. |
| 225 | * |
| 226 | */ |
| 227 | static int read_abs_bbts (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, |
| 228 | struct nand_bbt_descr *md) |
| 229 | { |
| 230 | struct nand_chip *this = mtd->priv; |
| 231 | |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 232 | /* Read the primary version, if available */ |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 233 | if (td->options & NAND_BBT_VERSION) { |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 234 | nand_read_raw (mtd, buf, td->pages[0] << this->page_shift, mtd->oobblock, mtd->oobsize); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 235 | td->version[0] = buf[mtd->oobblock + td->veroffs]; |
| 236 | printk (KERN_DEBUG "Bad block table at page %d, version 0x%02X\n", td->pages[0], td->version[0]); |
| 237 | } |
| 238 | |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 239 | /* Read the mirror version, if available */ |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 240 | if (md && (md->options & NAND_BBT_VERSION)) { |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 241 | nand_read_raw (mtd, buf, md->pages[0] << this->page_shift, mtd->oobblock, mtd->oobsize); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 242 | md->version[0] = buf[mtd->oobblock + md->veroffs]; |
| 243 | printk (KERN_DEBUG "Bad block table at page %d, version 0x%02X\n", md->pages[0], md->version[0]); |
| 244 | } |
| 245 | |
| 246 | return 1; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * create_bbt - [GENERIC] Create a bad block table by scanning the device |
| 251 | * @mtd: MTD device structure |
| 252 | * @buf: temporary buffer |
| 253 | * @bd: descriptor for the good/bad block search pattern |
| 254 | * @chip: create the table for a specific chip, -1 read all chips. |
| 255 | * Applies only if NAND_BBT_PERCHIP option is set |
| 256 | * |
| 257 | * Create a bad block table by scanning the device |
| 258 | * for the given good/bad block identify pattern |
| 259 | */ |
| 260 | static void create_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd, int chip) |
| 261 | { |
| 262 | struct nand_chip *this = mtd->priv; |
| 263 | int i, j, numblocks, len, scanlen; |
| 264 | int startblock; |
| 265 | loff_t from; |
| 266 | size_t readlen, ooblen; |
| 267 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 268 | if (bd->options & NAND_BBT_SCANALLPAGES) |
| 269 | len = 1 << (this->bbt_erase_shift - this->page_shift); |
| 270 | else { |
| 271 | if (bd->options & NAND_BBT_SCAN2NDPAGE) |
| 272 | len = 2; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 273 | else |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 274 | len = 1; |
| 275 | } |
| 276 | scanlen = mtd->oobblock + mtd->oobsize; |
| 277 | readlen = len * mtd->oobblock; |
| 278 | ooblen = len * mtd->oobsize; |
| 279 | |
| 280 | if (chip == -1) { |
| 281 | /* Note that numblocks is 2 * (real numblocks) here, see i+=2 below as it |
| 282 | * makes shifting and masking less painful */ |
| 283 | numblocks = mtd->size >> (this->bbt_erase_shift - 1); |
| 284 | startblock = 0; |
| 285 | from = 0; |
| 286 | } else { |
| 287 | if (chip >= this->numchips) { |
| 288 | printk (KERN_WARNING "create_bbt(): chipnr (%d) > available chips (%d)\n", |
| 289 | chip + 1, this->numchips); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 290 | return; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 291 | } |
| 292 | numblocks = this->chipsize >> (this->bbt_erase_shift - 1); |
| 293 | startblock = chip * numblocks; |
| 294 | numblocks += startblock; |
| 295 | from = startblock << (this->bbt_erase_shift - 1); |
| 296 | } |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 297 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 298 | for (i = startblock; i < numblocks;) { |
| 299 | nand_read_raw (mtd, buf, from, readlen, ooblen); |
| 300 | for (j = 0; j < len; j++) { |
| 301 | if (check_pattern (&buf[j * scanlen], scanlen, mtd->oobblock, bd)) { |
| 302 | this->bbt[i >> 3] |= 0x03 << (i & 0x6); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 303 | break; |
| 304 | } |
| 305 | } |
| 306 | i += 2; |
| 307 | from += (1 << this->bbt_erase_shift); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * search_bbt - [GENERIC] scan the device for a specific bad block table |
| 313 | * @mtd: MTD device structure |
| 314 | * @buf: temporary buffer |
| 315 | * @td: descriptor for the bad block table |
| 316 | * |
| 317 | * Read the bad block table by searching for a given ident pattern. |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 318 | * 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] | 319 | * the device downwards. The search starts always at the start of a |
| 320 | * block. |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 321 | * If the option NAND_BBT_PERCHIP is given, each chip is searched |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 322 | * for a bbt, which contains the bad block information of this chip. |
| 323 | * This is neccecary to provide support for certain DOC devices. |
| 324 | * |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 325 | * The bbt ident pattern resides in the oob area of the first page |
| 326 | * in a block. |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 327 | */ |
| 328 | static int search_bbt (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td) |
| 329 | { |
| 330 | struct nand_chip *this = mtd->priv; |
| 331 | int i, chips; |
| 332 | int bits, startblock, block, dir; |
| 333 | int scanlen = mtd->oobblock + mtd->oobsize; |
| 334 | int bbtblocks; |
| 335 | |
| 336 | /* Search direction top -> down ? */ |
| 337 | if (td->options & NAND_BBT_LASTBLOCK) { |
| 338 | startblock = (mtd->size >> this->bbt_erase_shift) -1; |
| 339 | dir = -1; |
| 340 | } else { |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 341 | startblock = 0; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 342 | dir = 1; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 343 | } |
| 344 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 345 | /* Do we have a bbt per chip ? */ |
| 346 | if (td->options & NAND_BBT_PERCHIP) { |
| 347 | chips = this->numchips; |
| 348 | bbtblocks = this->chipsize >> this->bbt_erase_shift; |
| 349 | startblock &= bbtblocks - 1; |
| 350 | } else { |
| 351 | chips = 1; |
| 352 | bbtblocks = mtd->size >> this->bbt_erase_shift; |
| 353 | } |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 354 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 355 | /* Number of bits for each erase block in the bbt */ |
| 356 | bits = td->options & NAND_BBT_NRBITS_MSK; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 357 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 358 | for (i = 0; i < chips; i++) { |
| 359 | /* Reset version information */ |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 360 | td->version[i] = 0; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 361 | td->pages[i] = -1; |
| 362 | /* Scan the maximum number of blocks */ |
| 363 | for (block = 0; block < td->maxblocks; block++) { |
| 364 | int actblock = startblock + dir * block; |
| 365 | /* Read first page */ |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 366 | nand_read_raw (mtd, buf, actblock << this->bbt_erase_shift, mtd->oobblock, mtd->oobsize); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 367 | if (!check_pattern(buf, scanlen, mtd->oobblock, td)) { |
| 368 | td->pages[i] = actblock << (this->bbt_erase_shift - this->page_shift); |
| 369 | if (td->options & NAND_BBT_VERSION) { |
| 370 | td->version[i] = buf[mtd->oobblock + td->veroffs]; |
| 371 | } |
| 372 | break; |
| 373 | } |
| 374 | } |
| 375 | startblock += this->chipsize >> this->bbt_erase_shift; |
| 376 | } |
| 377 | /* Check, if we found a bbt for each requested chip */ |
| 378 | for (i = 0; i < chips; i++) { |
| 379 | if (td->pages[i] == -1) |
| 380 | printk (KERN_WARNING "Bad block table not found for chip %d\n", i); |
| 381 | else |
| 382 | printk (KERN_DEBUG "Bad block table found at page %d, version 0x%02X\n", td->pages[i], td->version[i]); |
| 383 | } |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 384 | return 0; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /** |
| 388 | * search_read_bbts - [GENERIC] scan the device for bad block table(s) |
| 389 | * @mtd: MTD device structure |
| 390 | * @buf: temporary buffer |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 391 | * @td: descriptor for the bad block table |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 392 | * @md: descriptor for the bad block table mirror |
| 393 | * |
| 394 | * Search and read the bad block table(s) |
| 395 | */ |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 396 | static int search_read_bbts (struct mtd_info *mtd, uint8_t *buf, |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 397 | struct nand_bbt_descr *td, struct nand_bbt_descr *md) |
| 398 | { |
| 399 | /* Search the primary table */ |
| 400 | search_bbt (mtd, buf, td); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 401 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 402 | /* Search the mirror table */ |
| 403 | if (md) |
| 404 | search_bbt (mtd, buf, md); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 405 | |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 406 | /* Force result check */ |
| 407 | return 1; |
| 408 | } |
| 409 | |
| 410 | |
| 411 | /** |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 412 | * write_bbt - [GENERIC] (Re)write the bad block table |
| 413 | * |
| 414 | * @mtd: MTD device structure |
| 415 | * @buf: temporary buffer |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 416 | * @td: descriptor for the bad block table |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 417 | * @md: descriptor for the bad block table mirror |
| 418 | * @chipsel: selector for a specific chip, -1 for all |
| 419 | * |
| 420 | * (Re)write the bad block table |
| 421 | * |
| 422 | */ |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 423 | static int write_bbt (struct mtd_info *mtd, uint8_t *buf, |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 424 | struct nand_bbt_descr *td, struct nand_bbt_descr *md, int chipsel) |
| 425 | { |
| 426 | struct nand_chip *this = mtd->priv; |
| 427 | struct nand_oobinfo oobinfo; |
| 428 | struct erase_info einfo; |
| 429 | int i, j, res, chip = 0; |
| 430 | int bits, startblock, dir, page, offs, numblocks, sft, sftmsk; |
| 431 | int nrchips, bbtoffs, pageoffs; |
| 432 | uint8_t msk[4]; |
| 433 | uint8_t rcode = td->reserved_block_code; |
| 434 | size_t retlen, len = 0; |
| 435 | loff_t to; |
| 436 | |
| 437 | if (!rcode) |
| 438 | rcode = 0xff; |
| 439 | /* Write bad block table per chip rather than per device ? */ |
| 440 | if (td->options & NAND_BBT_PERCHIP) { |
| 441 | numblocks = (int) (this->chipsize >> this->bbt_erase_shift); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 442 | /* Full device write or specific chip ? */ |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 443 | if (chipsel == -1) { |
| 444 | nrchips = this->numchips; |
| 445 | } else { |
| 446 | nrchips = chipsel + 1; |
| 447 | chip = chipsel; |
| 448 | } |
| 449 | } else { |
| 450 | numblocks = (int) (mtd->size >> this->bbt_erase_shift); |
| 451 | nrchips = 1; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 454 | /* Loop through the chips */ |
| 455 | for (; chip < nrchips; chip++) { |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 456 | |
| 457 | /* There was already a version of the table, reuse the page |
| 458 | * This applies for absolute placement too, as we have the |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 459 | * page nr. in td->pages. |
| 460 | */ |
| 461 | if (td->pages[chip] != -1) { |
| 462 | page = td->pages[chip]; |
| 463 | goto write; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 464 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 465 | |
| 466 | /* Automatic placement of the bad block table */ |
| 467 | /* Search direction top -> down ? */ |
| 468 | if (td->options & NAND_BBT_LASTBLOCK) { |
| 469 | startblock = numblocks * (chip + 1) - 1; |
| 470 | dir = -1; |
| 471 | } else { |
| 472 | startblock = chip * numblocks; |
| 473 | dir = 1; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 474 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 475 | |
| 476 | for (i = 0; i < td->maxblocks; i++) { |
| 477 | int block = startblock + dir * i; |
| 478 | /* Check, if the block is bad */ |
| 479 | switch ((this->bbt[block >> 2] >> (2 * (block & 0x03))) & 0x03) { |
| 480 | case 0x01: |
| 481 | case 0x03: |
| 482 | continue; |
| 483 | } |
| 484 | page = block << (this->bbt_erase_shift - this->page_shift); |
| 485 | /* Check, if the block is used by the mirror table */ |
| 486 | if (!md || md->pages[chip] != page) |
| 487 | goto write; |
| 488 | } |
| 489 | printk (KERN_ERR "No space left to write bad block table\n"); |
| 490 | return -ENOSPC; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 491 | write: |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 492 | |
| 493 | /* Set up shift count and masks for the flash table */ |
| 494 | bits = td->options & NAND_BBT_NRBITS_MSK; |
| 495 | switch (bits) { |
| 496 | case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01; msk[2] = ~rcode; msk[3] = 0x01; break; |
| 497 | case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01; msk[2] = ~rcode; msk[3] = 0x03; break; |
| 498 | case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C; msk[2] = ~rcode; msk[3] = 0x0f; break; |
| 499 | case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F; msk[2] = ~rcode; msk[3] = 0xff; break; |
| 500 | default: return -EINVAL; |
| 501 | } |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 502 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 503 | bbtoffs = chip * (numblocks >> 2); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 504 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 505 | to = ((loff_t) page) << this->page_shift; |
| 506 | |
| 507 | memcpy (&oobinfo, this->autooob, sizeof(oobinfo)); |
| 508 | oobinfo.useecc = MTD_NANDECC_PLACEONLY; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 509 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 510 | /* Must we save the block contents ? */ |
| 511 | if (td->options & NAND_BBT_SAVECONTENT) { |
| 512 | /* Make it block aligned */ |
| 513 | to &= ~((loff_t) ((1 << this->bbt_erase_shift) - 1)); |
| 514 | len = 1 << this->bbt_erase_shift; |
| 515 | res = mtd->read_ecc (mtd, to, len, &retlen, buf, &buf[len], &oobinfo); |
| 516 | if (res < 0) { |
| 517 | if (retlen != len) { |
| 518 | printk (KERN_INFO "nand_bbt: Error reading block for writing the bad block table\n"); |
| 519 | return res; |
| 520 | } |
| 521 | printk (KERN_WARNING "nand_bbt: ECC error while reading block for writing bad block table\n"); |
| 522 | } |
| 523 | /* Calc the byte offset in the buffer */ |
| 524 | pageoffs = page - (int)(to >> this->page_shift); |
| 525 | offs = pageoffs << this->page_shift; |
| 526 | /* Preset the bbt area with 0xff */ |
| 527 | memset (&buf[offs], 0xff, (size_t)(numblocks >> sft)); |
| 528 | /* Preset the bbt's oob area with 0xff */ |
| 529 | memset (&buf[len + pageoffs * mtd->oobsize], 0xff, |
| 530 | ((len >> this->page_shift) - pageoffs) * mtd->oobsize); |
| 531 | if (td->options & NAND_BBT_VERSION) { |
| 532 | buf[len + (pageoffs * mtd->oobsize) + td->veroffs] = td->version[chip]; |
| 533 | } |
| 534 | } else { |
| 535 | /* Calc length */ |
| 536 | len = (size_t) (numblocks >> sft); |
| 537 | /* Make it page aligned ! */ |
| 538 | len = (len + (mtd->oobblock-1)) & ~(mtd->oobblock-1); |
| 539 | /* Preset the buffer with 0xff */ |
| 540 | memset (buf, 0xff, len + (len >> this->page_shift) * mtd->oobsize); |
| 541 | offs = 0; |
| 542 | /* Pattern is located in oob area of first page */ |
| 543 | memcpy (&buf[len + td->offs], td->pattern, td->len); |
| 544 | if (td->options & NAND_BBT_VERSION) { |
| 545 | buf[len + td->veroffs] = td->version[chip]; |
| 546 | } |
| 547 | } |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 548 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 549 | /* walk through the memory table */ |
| 550 | for (i = 0; i < numblocks; ) { |
| 551 | uint8_t dat; |
| 552 | dat = this->bbt[bbtoffs + (i >> 2)]; |
| 553 | for (j = 0; j < 4; j++ , i++) { |
| 554 | int sftcnt = (i << (3 - sft)) & sftmsk; |
| 555 | /* Do not store the reserved bbt blocks ! */ |
| 556 | buf[offs + (i >> sft)] &= ~(msk[dat & 0x03] << sftcnt); |
| 557 | dat >>= 2; |
| 558 | } |
| 559 | } |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 560 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 561 | memset (&einfo, 0, sizeof (einfo)); |
| 562 | einfo.mtd = mtd; |
| 563 | einfo.addr = (unsigned long) to; |
| 564 | einfo.len = 1 << this->bbt_erase_shift; |
| 565 | res = nand_erase_nand (mtd, &einfo, 1); |
| 566 | if (res < 0) { |
| 567 | printk (KERN_WARNING "nand_bbt: Error during block erase: %d\n", res); |
| 568 | return res; |
| 569 | } |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 570 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 571 | res = mtd->write_ecc (mtd, to, len, &retlen, buf, &buf[len], &oobinfo); |
| 572 | if (res < 0) { |
| 573 | printk (KERN_WARNING "nand_bbt: Error while writing bad block table %d\n", res); |
| 574 | return res; |
| 575 | } |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 576 | printk (KERN_DEBUG "Bad block table written to 0x%08x, version 0x%02X\n", |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 577 | (unsigned int) to, td->version[chip]); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 578 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 579 | /* Mark it as used */ |
| 580 | td->pages[chip] = page; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 581 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * nand_memory_bbt - [GENERIC] create a memory based bad block table |
| 587 | * @mtd: MTD device structure |
| 588 | * @bd: descriptor for the good/bad block search pattern |
| 589 | * |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 590 | * The function creates a memory based bbt by scanning the device |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 591 | * for manufacturer / software marked good / bad blocks |
| 592 | */ |
| 593 | static int nand_memory_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd) |
| 594 | { |
| 595 | struct nand_chip *this = mtd->priv; |
| 596 | |
| 597 | /* Ensure that we only scan for the pattern and nothing else */ |
| 598 | bd->options = 0; |
| 599 | create_bbt (mtd, this->data_buf, bd, -1); |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * check_create - [GENERIC] create and write bbt(s) if neccecary |
| 605 | * @mtd: MTD device structure |
| 606 | * @buf: temporary buffer |
| 607 | * @bd: descriptor for the good/bad block search pattern |
| 608 | * |
| 609 | * The function checks the results of the previous call to read_bbt |
| 610 | * and creates / updates the bbt(s) if neccecary |
| 611 | * Creation is neccecary if no bbt was found for the chip/device |
| 612 | * Update is neccecary if one of the tables is missing or the |
| 613 | * version nr. of one table is less than the other |
| 614 | */ |
| 615 | static int check_create (struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd) |
| 616 | { |
| 617 | int i, chips, writeops, chipsel, res; |
| 618 | struct nand_chip *this = mtd->priv; |
| 619 | struct nand_bbt_descr *td = this->bbt_td; |
| 620 | struct nand_bbt_descr *md = this->bbt_md; |
| 621 | struct nand_bbt_descr *rd, *rd2; |
| 622 | |
| 623 | /* Do we have a bbt per chip ? */ |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 624 | if (td->options & NAND_BBT_PERCHIP) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 625 | chips = this->numchips; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 626 | else |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 627 | chips = 1; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 628 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 629 | for (i = 0; i < chips; i++) { |
| 630 | writeops = 0; |
| 631 | rd = NULL; |
| 632 | rd2 = NULL; |
| 633 | /* Per chip or per device ? */ |
| 634 | chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1; |
| 635 | /* Mirrored table avilable ? */ |
| 636 | if (md) { |
| 637 | if (td->pages[i] == -1 && md->pages[i] == -1) { |
| 638 | writeops = 0x03; |
| 639 | goto create; |
| 640 | } |
| 641 | |
| 642 | if (td->pages[i] == -1) { |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 643 | rd = md; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 644 | td->version[i] = md->version[i]; |
| 645 | writeops = 1; |
| 646 | goto writecheck; |
| 647 | } |
| 648 | |
| 649 | if (md->pages[i] == -1) { |
| 650 | rd = td; |
| 651 | md->version[i] = td->version[i]; |
| 652 | writeops = 2; |
| 653 | goto writecheck; |
| 654 | } |
| 655 | |
| 656 | if (td->version[i] == md->version[i]) { |
| 657 | rd = td; |
| 658 | if (!(td->options & NAND_BBT_VERSION)) |
| 659 | rd2 = md; |
| 660 | goto writecheck; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 661 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 662 | |
| 663 | if (((int8_t) (td->version[i] - md->version[i])) > 0) { |
| 664 | rd = td; |
| 665 | md->version[i] = td->version[i]; |
| 666 | writeops = 2; |
| 667 | } else { |
| 668 | rd = md; |
| 669 | td->version[i] = md->version[i]; |
| 670 | writeops = 1; |
| 671 | } |
| 672 | |
| 673 | goto writecheck; |
| 674 | |
| 675 | } else { |
| 676 | if (td->pages[i] == -1) { |
| 677 | writeops = 0x01; |
| 678 | goto create; |
| 679 | } |
| 680 | rd = td; |
| 681 | goto writecheck; |
| 682 | } |
| 683 | create: |
| 684 | /* Create the bad block table by scanning the device ? */ |
| 685 | if (!(td->options & NAND_BBT_CREATE)) |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 686 | continue; |
| 687 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 688 | /* Create the table in memory by scanning the chip(s) */ |
| 689 | create_bbt (mtd, buf, bd, chipsel); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 690 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 691 | td->version[i] = 1; |
| 692 | if (md) |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 693 | md->version[i] = 1; |
| 694 | writecheck: |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 695 | /* read back first ? */ |
| 696 | if (rd) |
| 697 | read_abs_bbt (mtd, buf, rd, chipsel); |
| 698 | /* If they weren't versioned, read both. */ |
| 699 | if (rd2) |
| 700 | read_abs_bbt (mtd, buf, rd2, chipsel); |
| 701 | |
| 702 | /* Write the bad block table to the device ? */ |
| 703 | if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) { |
| 704 | res = write_bbt (mtd, buf, td, md, chipsel); |
| 705 | if (res < 0) |
| 706 | return res; |
| 707 | } |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 708 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 709 | /* Write the mirror bad block table to the device ? */ |
| 710 | if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) { |
| 711 | res = write_bbt (mtd, buf, md, td, chipsel); |
| 712 | if (res < 0) |
| 713 | return res; |
| 714 | } |
| 715 | } |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 716 | return 0; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | /** |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 720 | * mark_bbt_regions - [GENERIC] mark the bad block table regions |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 721 | * @mtd: MTD device structure |
| 722 | * @td: bad block table descriptor |
| 723 | * |
| 724 | * The bad block table regions are marked as "bad" to prevent |
| 725 | * accidental erasures / writes. The regions are identified by |
| 726 | * the mark 0x02. |
| 727 | */ |
| 728 | static void mark_bbt_region (struct mtd_info *mtd, struct nand_bbt_descr *td) |
| 729 | { |
| 730 | struct nand_chip *this = mtd->priv; |
| 731 | int i, j, chips, block, nrblocks, update; |
| 732 | uint8_t oldval, newval; |
| 733 | |
| 734 | /* Do we have a bbt per chip ? */ |
| 735 | if (td->options & NAND_BBT_PERCHIP) { |
| 736 | chips = this->numchips; |
| 737 | nrblocks = (int)(this->chipsize >> this->bbt_erase_shift); |
| 738 | } else { |
| 739 | chips = 1; |
| 740 | nrblocks = (int)(mtd->size >> this->bbt_erase_shift); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 741 | } |
| 742 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 743 | for (i = 0; i < chips; i++) { |
| 744 | if ((td->options & NAND_BBT_ABSPAGE) || |
| 745 | !(td->options & NAND_BBT_WRITE)) { |
| 746 | if (td->pages[i] == -1) continue; |
| 747 | block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 748 | block <<= 1; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 749 | oldval = this->bbt[(block >> 3)]; |
| 750 | newval = oldval | (0x2 << (block & 0x06)); |
| 751 | this->bbt[(block >> 3)] = newval; |
| 752 | if ((oldval != newval) && td->reserved_block_code) |
| 753 | nand_update_bbt(mtd, block << (this->bbt_erase_shift - 1)); |
| 754 | continue; |
| 755 | } |
| 756 | update = 0; |
| 757 | if (td->options & NAND_BBT_LASTBLOCK) |
| 758 | block = ((i + 1) * nrblocks) - td->maxblocks; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 759 | else |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 760 | block = i * nrblocks; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 761 | block <<= 1; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 762 | for (j = 0; j < td->maxblocks; j++) { |
| 763 | oldval = this->bbt[(block >> 3)]; |
| 764 | newval = oldval | (0x2 << (block & 0x06)); |
| 765 | this->bbt[(block >> 3)] = newval; |
| 766 | if (oldval != newval) update = 1; |
| 767 | block += 2; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 768 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 769 | /* If we want reserved blocks to be recorded to flash, and some |
| 770 | new ones have been marked, then we need to update the stored |
| 771 | bbts. This should only happen once. */ |
| 772 | if (update && td->reserved_block_code) |
| 773 | nand_update_bbt(mtd, (block - 2) << (this->bbt_erase_shift - 1)); |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | /** |
| 778 | * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s) |
| 779 | * @mtd: MTD device structure |
| 780 | * @bd: descriptor for the good/bad block search pattern |
| 781 | * |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 782 | * The function checks, if a bad block table(s) is/are already |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 783 | * available. If not it scans the device for manufacturer |
| 784 | * marked good / bad blocks and writes the bad block table(s) to |
| 785 | * the selected place. |
| 786 | * |
| 787 | * The bad block table memory is allocated here. It must be freed |
| 788 | * by calling the nand_free_bbt function. |
| 789 | * |
| 790 | */ |
| 791 | int nand_scan_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd) |
| 792 | { |
| 793 | struct nand_chip *this = mtd->priv; |
| 794 | int len, res = 0; |
| 795 | uint8_t *buf; |
| 796 | struct nand_bbt_descr *td = this->bbt_td; |
| 797 | struct nand_bbt_descr *md = this->bbt_md; |
| 798 | |
| 799 | len = mtd->size >> (this->bbt_erase_shift + 2); |
| 800 | /* Allocate memory (2bit per block) */ |
| 801 | this->bbt = kmalloc (len, GFP_KERNEL); |
| 802 | if (!this->bbt) { |
| 803 | printk (KERN_ERR "nand_scan_bbt: Out of memory\n"); |
| 804 | return -ENOMEM; |
| 805 | } |
| 806 | /* Clear the memory bad block table */ |
| 807 | memset (this->bbt, 0x00, len); |
| 808 | |
| 809 | /* If no primary table decriptor is given, scan the device |
| 810 | * to build a memory based bad block table |
| 811 | */ |
| 812 | if (!td) |
| 813 | return nand_memory_bbt(mtd, bd); |
| 814 | |
| 815 | /* Allocate a temporary buffer for one eraseblock incl. oob */ |
| 816 | len = (1 << this->bbt_erase_shift); |
| 817 | len += (len >> this->page_shift) * mtd->oobsize; |
| 818 | buf = kmalloc (len, GFP_KERNEL); |
| 819 | if (!buf) { |
| 820 | printk (KERN_ERR "nand_bbt: Out of memory\n"); |
| 821 | kfree (this->bbt); |
| 822 | this->bbt = NULL; |
| 823 | return -ENOMEM; |
| 824 | } |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 825 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 826 | /* Is the bbt at a given page ? */ |
| 827 | if (td->options & NAND_BBT_ABSPAGE) { |
| 828 | res = read_abs_bbts (mtd, buf, td, md); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 829 | } else { |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 830 | /* Search the bad block table using a pattern in oob */ |
| 831 | res = search_read_bbts (mtd, buf, td, md); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 832 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 833 | |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 834 | if (res) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 835 | res = check_create (mtd, buf, bd); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 836 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 837 | /* Prevent the bbt regions from erasing / writing */ |
| 838 | mark_bbt_region (mtd, td); |
| 839 | if (md) |
| 840 | mark_bbt_region (mtd, md); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 841 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 842 | kfree (buf); |
| 843 | return res; |
| 844 | } |
| 845 | |
| 846 | |
| 847 | /** |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 848 | * nand_update_bbt - [NAND Interface] update bad block table(s) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 849 | * @mtd: MTD device structure |
| 850 | * @offs: the offset of the newly marked block |
| 851 | * |
| 852 | * The function updates the bad block table(s) |
| 853 | */ |
| 854 | int nand_update_bbt (struct mtd_info *mtd, loff_t offs) |
| 855 | { |
| 856 | struct nand_chip *this = mtd->priv; |
| 857 | int len, res = 0, writeops = 0; |
| 858 | int chip, chipsel; |
| 859 | uint8_t *buf; |
| 860 | struct nand_bbt_descr *td = this->bbt_td; |
| 861 | struct nand_bbt_descr *md = this->bbt_md; |
| 862 | |
| 863 | if (!this->bbt || !td) |
| 864 | return -EINVAL; |
| 865 | |
| 866 | len = mtd->size >> (this->bbt_erase_shift + 2); |
| 867 | /* Allocate a temporary buffer for one eraseblock incl. oob */ |
| 868 | len = (1 << this->bbt_erase_shift); |
| 869 | len += (len >> this->page_shift) * mtd->oobsize; |
| 870 | buf = kmalloc (len, GFP_KERNEL); |
| 871 | if (!buf) { |
| 872 | printk (KERN_ERR "nand_update_bbt: Out of memory\n"); |
| 873 | return -ENOMEM; |
| 874 | } |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 875 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 876 | writeops = md != NULL ? 0x03 : 0x01; |
| 877 | |
| 878 | /* Do we have a bbt per chip ? */ |
| 879 | if (td->options & NAND_BBT_PERCHIP) { |
| 880 | chip = (int) (offs >> this->chip_shift); |
| 881 | chipsel = chip; |
| 882 | } else { |
| 883 | chip = 0; |
| 884 | chipsel = -1; |
| 885 | } |
| 886 | |
| 887 | td->version[chip]++; |
| 888 | if (md) |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 889 | md->version[chip]++; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 890 | |
| 891 | /* Write the bad block table to the device ? */ |
| 892 | if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) { |
| 893 | res = write_bbt (mtd, buf, td, md, chipsel); |
| 894 | if (res < 0) |
| 895 | goto out; |
| 896 | } |
| 897 | /* Write the mirror bad block table to the device ? */ |
| 898 | if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) { |
| 899 | res = write_bbt (mtd, buf, md, td, chipsel); |
| 900 | } |
| 901 | |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 902 | out: |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 903 | kfree (buf); |
| 904 | return res; |
| 905 | } |
| 906 | |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 907 | /* Define some generic bad / good block scan pattern which are used |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 908 | * while scanning a device for factory marked good / bad blocks |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 909 | * |
| 910 | * The memory based patterns just |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 911 | */ |
| 912 | static uint8_t scan_ff_pattern[] = { 0xff, 0xff }; |
| 913 | |
| 914 | static struct nand_bbt_descr smallpage_memorybased = { |
| 915 | .options = 0, |
| 916 | .offs = 5, |
| 917 | .len = 1, |
| 918 | .pattern = scan_ff_pattern |
| 919 | }; |
| 920 | |
| 921 | static struct nand_bbt_descr largepage_memorybased = { |
| 922 | .options = 0, |
| 923 | .offs = 0, |
| 924 | .len = 2, |
| 925 | .pattern = scan_ff_pattern |
| 926 | }; |
| 927 | |
| 928 | static struct nand_bbt_descr smallpage_flashbased = { |
| 929 | .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES, |
| 930 | .offs = 5, |
| 931 | .len = 1, |
| 932 | .pattern = scan_ff_pattern |
| 933 | }; |
| 934 | |
| 935 | static struct nand_bbt_descr largepage_flashbased = { |
| 936 | .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES, |
| 937 | .offs = 0, |
| 938 | .len = 2, |
| 939 | .pattern = scan_ff_pattern |
| 940 | }; |
| 941 | |
| 942 | static uint8_t scan_agand_pattern[] = { 0x1C, 0x71, 0xC7, 0x1C, 0x71, 0xC7 }; |
| 943 | |
| 944 | static struct nand_bbt_descr agand_flashbased = { |
| 945 | .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES, |
| 946 | .offs = 0x20, |
| 947 | .len = 6, |
| 948 | .pattern = scan_agand_pattern |
| 949 | }; |
| 950 | |
| 951 | /* Generic flash bbt decriptors |
| 952 | */ |
| 953 | static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' }; |
| 954 | static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' }; |
| 955 | |
| 956 | static struct nand_bbt_descr bbt_main_descr = { |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 957 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 958 | | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, |
| 959 | .offs = 8, |
| 960 | .len = 4, |
| 961 | .veroffs = 12, |
| 962 | .maxblocks = 4, |
| 963 | .pattern = bbt_pattern |
| 964 | }; |
| 965 | |
| 966 | static struct nand_bbt_descr bbt_mirror_descr = { |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 967 | .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 968 | | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP, |
| 969 | .offs = 8, |
| 970 | .len = 4, |
| 971 | .veroffs = 12, |
| 972 | .maxblocks = 4, |
| 973 | .pattern = mirror_pattern |
| 974 | }; |
| 975 | |
| 976 | /** |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 977 | * 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] | 978 | * @mtd: MTD device structure |
| 979 | * |
| 980 | * This function selects the default bad block table |
| 981 | * support for the device and calls the nand_scan_bbt function |
| 982 | * |
| 983 | */ |
| 984 | int nand_default_bbt (struct mtd_info *mtd) |
| 985 | { |
| 986 | struct nand_chip *this = mtd->priv; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 987 | |
| 988 | /* Default for AG-AND. We must use a flash based |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 989 | * bad block table as the devices have factory marked |
| 990 | * _good_ blocks. Erasing those blocks leads to loss |
| 991 | * of the good / bad information, so we _must_ store |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 992 | * this information in a good / bad table during |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 993 | * startup |
| 994 | */ |
| 995 | if (this->options & NAND_IS_AND) { |
| 996 | /* Use the default pattern descriptors */ |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 997 | if (!this->bbt_td) { |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 998 | this->bbt_td = &bbt_main_descr; |
| 999 | this->bbt_md = &bbt_mirror_descr; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1000 | } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1001 | this->options |= NAND_USE_FLASH_BBT; |
| 1002 | return nand_scan_bbt (mtd, &agand_flashbased); |
| 1003 | } |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1004 | |
| 1005 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1006 | /* Is a flash based bad block table requested ? */ |
| 1007 | if (this->options & NAND_USE_FLASH_BBT) { |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1008 | /* Use the default pattern descriptors */ |
| 1009 | if (!this->bbt_td) { |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1010 | this->bbt_td = &bbt_main_descr; |
| 1011 | this->bbt_md = &bbt_mirror_descr; |
| 1012 | } |
| 1013 | if (!this->badblock_pattern) { |
| 1014 | this->badblock_pattern = (mtd->oobblock > 512) ? |
| 1015 | &largepage_flashbased : &smallpage_flashbased; |
| 1016 | } |
| 1017 | } else { |
| 1018 | this->bbt_td = NULL; |
| 1019 | this->bbt_md = NULL; |
| 1020 | if (!this->badblock_pattern) { |
| 1021 | this->badblock_pattern = (mtd->oobblock > 512) ? |
| 1022 | &largepage_memorybased : &smallpage_memorybased; |
| 1023 | } |
| 1024 | } |
| 1025 | return nand_scan_bbt (mtd, this->badblock_pattern); |
| 1026 | } |
| 1027 | |
| 1028 | /** |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1029 | * nand_isbad_bbt - [NAND Interface] Check if a block is bad |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1030 | * @mtd: MTD device structure |
| 1031 | * @offs: offset in the device |
| 1032 | * @allowbbt: allow access to bad block table region |
| 1033 | * |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1034 | */ |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1035 | int nand_isbad_bbt (struct mtd_info *mtd, loff_t offs, int allowbbt) |
| 1036 | { |
| 1037 | struct nand_chip *this = mtd->priv; |
| 1038 | int block; |
| 1039 | uint8_t res; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1040 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1041 | /* Get block number * 2 */ |
| 1042 | block = (int) (offs >> (this->bbt_erase_shift - 1)); |
| 1043 | res = (this->bbt[block >> 3] >> (block & 0x06)) & 0x03; |
| 1044 | |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1045 | DEBUG (MTD_DEBUG_LEVEL2, "nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n", |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1046 | (unsigned int)offs, res, block >> 1); |
| 1047 | |
| 1048 | switch ((int)res) { |
| 1049 | case 0x00: return 0; |
| 1050 | case 0x01: return 1; |
| 1051 | case 0x02: return allowbbt ? 0 : 1; |
| 1052 | } |
| 1053 | return 1; |
| 1054 | } |
| 1055 | |
| 1056 | #endif |