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