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