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