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