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