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