blob: fc2c16a0fd1cd4fbf4e76e33ac87578aaf0b38d7 [file] [log] [blame]
Kyungmin Park87590e22005-09-27 11:26:39 +01001/*
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>
Kyungmin Park87590e22005-09-27 11:26:39 +010018
19/**
20 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
21 * @param buf the buffer to search
22 * @param len the length of buffer to search
23 * @param paglen the pagelength
24 * @param td search pattern descriptor
25 *
26 * Check for a pattern at the given place. Used to search bad block
27 * tables and good / bad block identifiers. Same as check_pattern, but
28 * no optional empty check and the pattern is expected to start
29 * at offset 0.
30 *
31 */
32static int check_short_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
33{
34 int i;
35 uint8_t *p = buf;
36
37 /* Compare the pattern */
38 for (i = 0; i < td->len; i++) {
39 if (p[i] != td->pattern[i])
40 return -1;
41 }
42 return 0;
43}
44
45/**
46 * create_bbt - [GENERIC] Create a bad block table by scanning the device
47 * @param mtd MTD device structure
48 * @param buf temporary buffer
49 * @param bd descriptor for the good/bad block search pattern
50 * @param chip create the table for a specific chip, -1 read all chips.
51 * Applies only if NAND_BBT_PERCHIP option is set
52 *
53 * Create a bad block table by scanning the device
54 * for the given good/bad block identify pattern
55 */
56static int create_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd, int chip)
57{
58 struct onenand_chip *this = mtd->priv;
59 struct bbm_info *bbm = this->bbm;
60 int i, j, numblocks, len, scanlen;
61 int startblock;
62 loff_t from;
63 size_t readlen, ooblen;
Kyungmin Park211ac752007-02-07 12:15:01 +090064 struct mtd_oob_ops ops;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -070065 int rgn;
Kyungmin Park87590e22005-09-27 11:26:39 +010066
67 printk(KERN_INFO "Scanning device for bad blocks\n");
68
Adrian Hunterec255e32007-01-22 21:30:31 +090069 len = 2;
Kyungmin Park87590e22005-09-27 11:26:39 +010070
71 /* We need only read few bytes from the OOB area */
72 scanlen = ooblen = 0;
73 readlen = bd->len;
74
75 /* chip == -1 case only */
76 /* Note that numblocks is 2 * (real numblocks) here;
77 * see i += 2 below as it makses shifting and masking less painful
78 */
Rohit Hagargundgi5988af22009-05-12 13:46:57 -070079 numblocks = this->chipsize >> (bbm->bbt_erase_shift - 1);
Kyungmin Park87590e22005-09-27 11:26:39 +010080 startblock = 0;
81 from = 0;
82
Kyungmin Park211ac752007-02-07 12:15:01 +090083 ops.mode = MTD_OOB_PLACE;
84 ops.ooblen = readlen;
85 ops.oobbuf = buf;
86 ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
87
Kyungmin Park87590e22005-09-27 11:26:39 +010088 for (i = startblock; i < numblocks; ) {
89 int ret;
90
91 for (j = 0; j < len; j++) {
Kyungmin Park87590e22005-09-27 11:26:39 +010092 /* No need to read pages fully,
93 * just read required OOB bytes */
Roman Tereshonkov01039e42010-12-02 15:28:38 +020094 ret = onenand_bbt_read_oob(mtd,
95 from + j * this->writesize + bd->offs, &ops);
Kyungmin Park87590e22005-09-27 11:26:39 +010096
Kyungmin Parkf6272482006-12-22 16:02:50 +090097 /* If it is a initial bad block, just ignore it */
Kyungmin Park211ac752007-02-07 12:15:01 +090098 if (ret == ONENAND_BBT_READ_FATAL_ERROR)
99 return -EIO;
Kyungmin Park87590e22005-09-27 11:26:39 +0100100
Roman Tereshonkov01039e42010-12-02 15:28:38 +0200101 if (ret || check_short_pattern(&buf[j * scanlen],
102 scanlen, this->writesize, bd)) {
Kyungmin Park87590e22005-09-27 11:26:39 +0100103 bbm->bbt[i >> 3] |= 0x03 << (i & 0x6);
Adrian Huntere0c1a922010-12-10 12:04:20 +0200104 printk(KERN_INFO "OneNAND eraseblock %d is an "
105 "initial bad block\n", i >> 1);
Kyungmin Parkf4f91ac2006-11-16 12:03:56 +0900106 mtd->ecc_stats.badblocks++;
Kyungmin Park87590e22005-09-27 11:26:39 +0100107 break;
108 }
109 }
110 i += 2;
Rohit Hagargundgi5988af22009-05-12 13:46:57 -0700111
112 if (FLEXONENAND(this)) {
113 rgn = flexonenand_region(mtd, from);
114 from += mtd->eraseregions[rgn].erasesize;
115 } else
116 from += (1 << bbm->bbt_erase_shift);
Kyungmin Park87590e22005-09-27 11:26:39 +0100117 }
118
119 return 0;
120}
121
122
123/**
124 * onenand_memory_bbt - [GENERIC] create a memory based bad block table
125 * @param mtd MTD device structure
126 * @param bd descriptor for the good/bad block search pattern
127 *
128 * The function creates a memory based bbt by scanning the device
129 * for manufacturer / software marked good / bad blocks
130 */
131static inline int onenand_memory_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
132{
Kyungmin Park532a37c2005-12-16 11:17:29 +0900133 struct onenand_chip *this = mtd->priv;
Kyungmin Park87590e22005-09-27 11:26:39 +0100134
135 bd->options &= ~NAND_BBT_SCANEMPTY;
Kyungmin Park532a37c2005-12-16 11:17:29 +0900136 return create_bbt(mtd, this->page_buf, bd, -1);
Kyungmin Park87590e22005-09-27 11:26:39 +0100137}
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 */
145static 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 Hagargundgi5988af22009-05-12 13:46:57 -0700153 block = (int) (onenand_block(this, offs) << 1);
Kyungmin Park87590e22005-09-27 11:26:39 +0100154 res = (bbm->bbt[block >> 3] >> (block & 0x06)) & 0x03;
155
156 DEBUG(MTD_DEBUG_LEVEL2, "onenand_isbad_bbt: bbt info for offs 0x%08x: (block %d) 0x%02x\n",
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 Hunterf00b0042007-01-22 17:01:01 +0900178 * The bad block table memory is allocated here. It is freed
179 * by the onenand_release function.
Kyungmin Park87590e22005-09-27 11:26:39 +0100180 *
181 */
182int 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 Hagargundgi5988af22009-05-12 13:46:57 -0700188 len = this->chipsize >> (this->erase_shift + 2);
Burman Yan95b93a02006-11-15 21:10:29 +0200189 /* Allocate memory (2bit per block) and clear the memory bad block table */
190 bbm->bbt = kzalloc(len, GFP_KERNEL);
Kyungmin Park87590e22005-09-27 11:26:39 +0100191 if (!bbm->bbt) {
192 printk(KERN_ERR "onenand_scan_bbt: Out of memory\n");
193 return -ENOMEM;
194 }
Kyungmin Park87590e22005-09-27 11:26:39 +0100195
196 /* Set the bad block position */
197 bbm->badblockpos = ONENAND_BADBLOCK_POS;
198
199 /* Set erase shift */
200 bbm->bbt_erase_shift = this->erase_shift;
201
202 if (!bbm->isbad_bbt)
203 bbm->isbad_bbt = onenand_isbad_bbt;
204
205 /* Scan the device to build a memory based bad block table */
206 if ((ret = onenand_memory_bbt(mtd, bd))) {
207 printk(KERN_ERR "onenand_scan_bbt: Can't scan flash and build the RAM-based BBT\n");
208 kfree(bbm->bbt);
209 bbm->bbt = NULL;
210 }
211
212 return ret;
213}
214
215/*
216 * Define some generic bad / good block scan pattern which are used
217 * while scanning a device for factory marked good / bad blocks.
218 */
219static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
220
221static struct nand_bbt_descr largepage_memorybased = {
222 .options = 0,
223 .offs = 0,
224 .len = 2,
225 .pattern = scan_ff_pattern,
226};
227
228/**
229 * onenand_default_bbt - [OneNAND Interface] Select a default bad block table for the device
230 * @param mtd MTD device structure
231 *
232 * This function selects the default bad block table
233 * support for the device and calls the onenand_scan_bbt function
234 */
235int onenand_default_bbt(struct mtd_info *mtd)
236{
237 struct onenand_chip *this = mtd->priv;
238 struct bbm_info *bbm;
239
Burman Yan95b93a02006-11-15 21:10:29 +0200240 this->bbm = kzalloc(sizeof(struct bbm_info), GFP_KERNEL);
Kyungmin Park87590e22005-09-27 11:26:39 +0100241 if (!this->bbm)
242 return -ENOMEM;
243
244 bbm = this->bbm;
245
Kyungmin Park87590e22005-09-27 11:26:39 +0100246 /* 1KB page has same configuration as 2KB page */
247 if (!bbm->badblock_pattern)
248 bbm->badblock_pattern = &largepage_memorybased;
249
250 return onenand_scan_bbt(mtd, bbm->badblock_pattern);
251}
252
253EXPORT_SYMBOL(onenand_scan_bbt);
254EXPORT_SYMBOL(onenand_default_bbt);