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