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