Kyungmin Park | 87590e2 | 2005-09-27 11:26:39 +0100 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/mtd/onenand/onenand_bbt.c |
| 3 | * |
| 4 | * Bad Block Table support for the OneNAND driver |
| 5 | * |
| 6 | * Copyright(c) 2005 Samsung Electronics |
| 7 | * Kyungmin Park <kyungmin.park@samsung.com> |
| 8 | * |
| 9 | * Derived from nand_bbt.c |
| 10 | * |
| 11 | * TODO: |
| 12 | * Split BBT core and chip specific BBT. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/mtd/mtd.h> |
| 17 | #include <linux/mtd/onenand.h> |
Paul Gortmaker | f3bcc01 | 2011-07-10 12:43:28 -0400 | [diff] [blame] | 18 | #include <linux/export.h> |
Kyungmin Park | 87590e2 | 2005-09-27 11:26:39 +0100 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * check_short_pattern - [GENERIC] check if a pattern is in the buffer |
| 22 | * @param buf the buffer to search |
| 23 | * @param len the length of buffer to search |
| 24 | * @param paglen the pagelength |
| 25 | * @param td search pattern descriptor |
| 26 | * |
| 27 | * Check for a pattern at the given place. Used to search bad block |
| 28 | * tables and good / bad block identifiers. Same as check_pattern, but |
| 29 | * no optional empty check and the pattern is expected to start |
| 30 | * at offset 0. |
| 31 | * |
| 32 | */ |
| 33 | static int check_short_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td) |
| 34 | { |
| 35 | int i; |
| 36 | uint8_t *p = buf; |
| 37 | |
| 38 | /* Compare the pattern */ |
| 39 | for (i = 0; i < td->len; i++) { |
| 40 | if (p[i] != td->pattern[i]) |
| 41 | return -1; |
| 42 | } |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * create_bbt - [GENERIC] Create a bad block table by scanning the device |
| 48 | * @param mtd MTD device structure |
| 49 | * @param buf temporary buffer |
| 50 | * @param bd descriptor for the good/bad block search pattern |
| 51 | * @param chip create the table for a specific chip, -1 read all chips. |
| 52 | * Applies only if NAND_BBT_PERCHIP option is set |
| 53 | * |
| 54 | * Create a bad block table by scanning the device |
| 55 | * for the given good/bad block identify pattern |
| 56 | */ |
| 57 | static int create_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd, int chip) |
| 58 | { |
| 59 | struct onenand_chip *this = mtd->priv; |
| 60 | struct bbm_info *bbm = this->bbm; |
| 61 | int i, j, numblocks, len, scanlen; |
| 62 | int startblock; |
| 63 | loff_t from; |
| 64 | size_t readlen, ooblen; |
Kyungmin Park | 211ac75 | 2007-02-07 12:15:01 +0900 | [diff] [blame] | 65 | struct mtd_oob_ops ops; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 66 | int rgn; |
Kyungmin Park | 87590e2 | 2005-09-27 11:26:39 +0100 | [diff] [blame] | 67 | |
| 68 | printk(KERN_INFO "Scanning device for bad blocks\n"); |
| 69 | |
Adrian Hunter | ec255e3 | 2007-01-22 21:30:31 +0900 | [diff] [blame] | 70 | len = 2; |
Kyungmin Park | 87590e2 | 2005-09-27 11:26:39 +0100 | [diff] [blame] | 71 | |
| 72 | /* We need only read few bytes from the OOB area */ |
| 73 | scanlen = ooblen = 0; |
| 74 | readlen = bd->len; |
| 75 | |
| 76 | /* chip == -1 case only */ |
| 77 | /* Note that numblocks is 2 * (real numblocks) here; |
| 78 | * see i += 2 below as it makses shifting and masking less painful |
| 79 | */ |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 80 | numblocks = this->chipsize >> (bbm->bbt_erase_shift - 1); |
Kyungmin Park | 87590e2 | 2005-09-27 11:26:39 +0100 | [diff] [blame] | 81 | startblock = 0; |
| 82 | from = 0; |
| 83 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 84 | ops.mode = MTD_OPS_PLACE_OOB; |
Kyungmin Park | 211ac75 | 2007-02-07 12:15:01 +0900 | [diff] [blame] | 85 | ops.ooblen = readlen; |
| 86 | ops.oobbuf = buf; |
| 87 | ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0; |
| 88 | |
Kyungmin Park | 87590e2 | 2005-09-27 11:26:39 +0100 | [diff] [blame] | 89 | for (i = startblock; i < numblocks; ) { |
| 90 | int ret; |
| 91 | |
| 92 | for (j = 0; j < len; j++) { |
Kyungmin Park | 87590e2 | 2005-09-27 11:26:39 +0100 | [diff] [blame] | 93 | /* No need to read pages fully, |
| 94 | * just read required OOB bytes */ |
Roman Tereshonkov | 01039e4 | 2010-12-02 15:28:38 +0200 | [diff] [blame] | 95 | ret = onenand_bbt_read_oob(mtd, |
| 96 | from + j * this->writesize + bd->offs, &ops); |
Kyungmin Park | 87590e2 | 2005-09-27 11:26:39 +0100 | [diff] [blame] | 97 | |
Kyungmin Park | f627248 | 2006-12-22 16:02:50 +0900 | [diff] [blame] | 98 | /* If it is a initial bad block, just ignore it */ |
Kyungmin Park | 211ac75 | 2007-02-07 12:15:01 +0900 | [diff] [blame] | 99 | if (ret == ONENAND_BBT_READ_FATAL_ERROR) |
| 100 | return -EIO; |
Kyungmin Park | 87590e2 | 2005-09-27 11:26:39 +0100 | [diff] [blame] | 101 | |
Roman Tereshonkov | 01039e4 | 2010-12-02 15:28:38 +0200 | [diff] [blame] | 102 | if (ret || check_short_pattern(&buf[j * scanlen], |
| 103 | scanlen, this->writesize, bd)) { |
Kyungmin Park | 87590e2 | 2005-09-27 11:26:39 +0100 | [diff] [blame] | 104 | bbm->bbt[i >> 3] |= 0x03 << (i & 0x6); |
Adrian Hunter | e0c1a92 | 2010-12-10 12:04:20 +0200 | [diff] [blame] | 105 | printk(KERN_INFO "OneNAND eraseblock %d is an " |
| 106 | "initial bad block\n", i >> 1); |
Kyungmin Park | f4f91ac | 2006-11-16 12:03:56 +0900 | [diff] [blame] | 107 | mtd->ecc_stats.badblocks++; |
Kyungmin Park | 87590e2 | 2005-09-27 11:26:39 +0100 | [diff] [blame] | 108 | break; |
| 109 | } |
| 110 | } |
| 111 | i += 2; |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 112 | |
| 113 | if (FLEXONENAND(this)) { |
| 114 | rgn = flexonenand_region(mtd, from); |
| 115 | from += mtd->eraseregions[rgn].erasesize; |
| 116 | } else |
| 117 | from += (1 << bbm->bbt_erase_shift); |
Kyungmin Park | 87590e2 | 2005-09-27 11:26:39 +0100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | /** |
| 125 | * onenand_memory_bbt - [GENERIC] create a memory based bad block table |
| 126 | * @param mtd MTD device structure |
| 127 | * @param bd descriptor for the good/bad block search pattern |
| 128 | * |
| 129 | * The function creates a memory based bbt by scanning the device |
| 130 | * for manufacturer / software marked good / bad blocks |
| 131 | */ |
| 132 | static inline int onenand_memory_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd) |
| 133 | { |
Kyungmin Park | 532a37c | 2005-12-16 11:17:29 +0900 | [diff] [blame] | 134 | struct onenand_chip *this = mtd->priv; |
Kyungmin Park | 87590e2 | 2005-09-27 11:26:39 +0100 | [diff] [blame] | 135 | |
Kyungmin Park | 532a37c | 2005-12-16 11:17:29 +0900 | [diff] [blame] | 136 | return create_bbt(mtd, this->page_buf, bd, -1); |
Kyungmin Park | 87590e2 | 2005-09-27 11:26:39 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
| 140 | * onenand_isbad_bbt - [OneNAND Interface] Check if a block is bad |
| 141 | * @param mtd MTD device structure |
| 142 | * @param offs offset in the device |
| 143 | * @param allowbbt allow access to bad block table region |
| 144 | */ |
| 145 | static int onenand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt) |
| 146 | { |
| 147 | struct onenand_chip *this = mtd->priv; |
| 148 | struct bbm_info *bbm = this->bbm; |
| 149 | int block; |
| 150 | uint8_t res; |
| 151 | |
| 152 | /* Get block number * 2 */ |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 153 | block = (int) (onenand_block(this, offs) << 1); |
Kyungmin Park | 87590e2 | 2005-09-27 11:26:39 +0100 | [diff] [blame] | 154 | res = (bbm->bbt[block >> 3] >> (block & 0x06)) & 0x03; |
| 155 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 156 | pr_debug("onenand_isbad_bbt: bbt info for offs 0x%08x: (block %d) 0x%02x\n", |
Kyungmin Park | 87590e2 | 2005-09-27 11:26:39 +0100 | [diff] [blame] | 157 | (unsigned int) offs, block >> 1, res); |
| 158 | |
| 159 | switch ((int) res) { |
| 160 | case 0x00: return 0; |
| 161 | case 0x01: return 1; |
| 162 | case 0x02: return allowbbt ? 0 : 1; |
| 163 | } |
| 164 | |
| 165 | return 1; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * onenand_scan_bbt - [OneNAND Interface] scan, find, read and maybe create bad block table(s) |
| 170 | * @param mtd MTD device structure |
| 171 | * @param bd descriptor for the good/bad block search pattern |
| 172 | * |
| 173 | * The function checks, if a bad block table(s) is/are already |
| 174 | * available. If not it scans the device for manufacturer |
| 175 | * marked good / bad blocks and writes the bad block table(s) to |
| 176 | * the selected place. |
| 177 | * |
Adrian Hunter | f00b004 | 2007-01-22 17:01:01 +0900 | [diff] [blame] | 178 | * The bad block table memory is allocated here. It is freed |
| 179 | * by the onenand_release function. |
Kyungmin Park | 87590e2 | 2005-09-27 11:26:39 +0100 | [diff] [blame] | 180 | * |
| 181 | */ |
| 182 | int onenand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd) |
| 183 | { |
| 184 | struct onenand_chip *this = mtd->priv; |
| 185 | struct bbm_info *bbm = this->bbm; |
| 186 | int len, ret = 0; |
| 187 | |
Rohit Hagargundgi | 5988af2 | 2009-05-12 13:46:57 -0700 | [diff] [blame] | 188 | len = this->chipsize >> (this->erase_shift + 2); |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 189 | /* Allocate memory (2bit per block) and clear the memory bad block table */ |
| 190 | bbm->bbt = kzalloc(len, GFP_KERNEL); |
Brian Norris | 0870066 | 2011-06-07 16:01:54 -0700 | [diff] [blame] | 191 | if (!bbm->bbt) |
Kyungmin Park | 87590e2 | 2005-09-27 11:26:39 +0100 | [diff] [blame] | 192 | return -ENOMEM; |
Kyungmin Park | 87590e2 | 2005-09-27 11:26:39 +0100 | [diff] [blame] | 193 | |
| 194 | /* Set the bad block position */ |
| 195 | bbm->badblockpos = ONENAND_BADBLOCK_POS; |
| 196 | |
| 197 | /* Set erase shift */ |
| 198 | bbm->bbt_erase_shift = this->erase_shift; |
| 199 | |
| 200 | if (!bbm->isbad_bbt) |
| 201 | bbm->isbad_bbt = onenand_isbad_bbt; |
| 202 | |
| 203 | /* Scan the device to build a memory based bad block table */ |
| 204 | if ((ret = onenand_memory_bbt(mtd, bd))) { |
| 205 | printk(KERN_ERR "onenand_scan_bbt: Can't scan flash and build the RAM-based BBT\n"); |
| 206 | kfree(bbm->bbt); |
| 207 | bbm->bbt = NULL; |
| 208 | } |
| 209 | |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Define some generic bad / good block scan pattern which are used |
| 215 | * while scanning a device for factory marked good / bad blocks. |
| 216 | */ |
| 217 | static uint8_t scan_ff_pattern[] = { 0xff, 0xff }; |
| 218 | |
| 219 | static struct nand_bbt_descr largepage_memorybased = { |
| 220 | .options = 0, |
| 221 | .offs = 0, |
| 222 | .len = 2, |
| 223 | .pattern = scan_ff_pattern, |
| 224 | }; |
| 225 | |
| 226 | /** |
| 227 | * onenand_default_bbt - [OneNAND Interface] Select a default bad block table for the device |
| 228 | * @param mtd MTD device structure |
| 229 | * |
| 230 | * This function selects the default bad block table |
| 231 | * support for the device and calls the onenand_scan_bbt function |
| 232 | */ |
| 233 | int onenand_default_bbt(struct mtd_info *mtd) |
| 234 | { |
| 235 | struct onenand_chip *this = mtd->priv; |
| 236 | struct bbm_info *bbm; |
| 237 | |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 238 | this->bbm = kzalloc(sizeof(struct bbm_info), GFP_KERNEL); |
Kyungmin Park | 87590e2 | 2005-09-27 11:26:39 +0100 | [diff] [blame] | 239 | if (!this->bbm) |
| 240 | return -ENOMEM; |
| 241 | |
| 242 | bbm = this->bbm; |
| 243 | |
Kyungmin Park | 87590e2 | 2005-09-27 11:26:39 +0100 | [diff] [blame] | 244 | /* 1KB page has same configuration as 2KB page */ |
| 245 | if (!bbm->badblock_pattern) |
| 246 | bbm->badblock_pattern = &largepage_memorybased; |
| 247 | |
| 248 | return onenand_scan_bbt(mtd, bbm->badblock_pattern); |
| 249 | } |
| 250 | |
| 251 | EXPORT_SYMBOL(onenand_scan_bbt); |
| 252 | EXPORT_SYMBOL(onenand_default_bbt); |