blob: 9d5da54708ec9ed9db9c83eff0b9939adb6d90bd [file] [log] [blame]
Kyungmin Park17aa2802007-09-10 17:14:34 +09001/*
2 * linux/drivers/mtd/onenand/onenand_bbt.c
3 *
4 * Bad Block Table support for the OneNAND driver
5 *
Kyungmin Parkef0921d2008-11-04 09:24:07 +09006 * Copyright(c) 2005-2008 Samsung Electronics
Kyungmin Park17aa2802007-09-10 17:14:34 +09007 * 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>
Mike Frysinger7b15e2b2012-04-09 13:39:55 +000018#include <linux/compat.h>
Kyungmin Park17aa2802007-09-10 17:14:34 +090019#include <linux/mtd/mtd.h>
20#include <linux/mtd/onenand.h>
21#include <malloc.h>
22
23#include <asm/errno.h>
24
25/**
26 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
27 * @param buf the buffer to search
28 * @param len the length of buffer to search
29 * @param paglen the pagelength
30 * @param td search pattern descriptor
31 *
32 * Check for a pattern at the given place. Used to search bad block
33 * tables and good / bad block identifiers. Same as check_pattern, but
34 * no optional empty check and the pattern is expected to start
35 * at offset 0.
36 */
37static int check_short_pattern(uint8_t * buf, int len, int paglen,
38 struct nand_bbt_descr *td)
39{
40 int i;
41 uint8_t *p = buf;
42
43 /* Compare the pattern */
44 for (i = 0; i < td->len; i++) {
45 if (p[i] != td->pattern[i])
46 return -1;
47 }
48 return 0;
49}
50
51/**
52 * create_bbt - [GENERIC] Create a bad block table by scanning the device
53 * @param mtd MTD device structure
54 * @param buf temporary buffer
55 * @param bd descriptor for the good/bad block search pattern
56 * @param chip create the table for a specific chip, -1 read all chips.
Kyungmin Parkef0921d2008-11-04 09:24:07 +090057 * Applies only if NAND_BBT_PERCHIP option is set
Kyungmin Park17aa2802007-09-10 17:14:34 +090058 *
59 * Create a bad block table by scanning the device
60 * for the given good/bad block identify pattern
61 */
62static int create_bbt(struct mtd_info *mtd, uint8_t * buf,
63 struct nand_bbt_descr *bd, int chip)
64{
65 struct onenand_chip *this = mtd->priv;
66 struct bbm_info *bbm = this->bbm;
67 int i, j, numblocks, len, scanlen;
68 int startblock;
69 loff_t from;
70 size_t readlen, ooblen;
Kyungmin Parkbfd7f382008-08-19 08:42:53 +090071 struct mtd_oob_ops ops;
Amul Kumar Sahacacbe912009-11-06 17:15:31 +053072 int rgn;
Kyungmin Park17aa2802007-09-10 17:14:34 +090073
74 printk(KERN_INFO "Scanning device for bad blocks\n");
75
76 len = 1;
77
78 /* We need only read few bytes from the OOB area */
79 scanlen = ooblen = 0;
80 readlen = bd->len;
81
82 /* chip == -1 case only */
83 /* Note that numblocks is 2 * (real numblocks) here;
84 * see i += 2 below as it makses shifting and masking less painful
85 */
Amul Kumar Sahacacbe912009-11-06 17:15:31 +053086 numblocks = this->chipsize >> (bbm->bbt_erase_shift - 1);
Kyungmin Park17aa2802007-09-10 17:14:34 +090087 startblock = 0;
88 from = 0;
89
Kyungmin Parkbfd7f382008-08-19 08:42:53 +090090 ops.mode = MTD_OOB_PLACE;
91 ops.ooblen = readlen;
92 ops.oobbuf = buf;
93 ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
94
Kyungmin Park17aa2802007-09-10 17:14:34 +090095 for (i = startblock; i < numblocks;) {
96 int ret;
97
98 for (j = 0; j < len; j++) {
Kyungmin Park17aa2802007-09-10 17:14:34 +090099 /* No need to read pages fully,
100 * just read required OOB bytes */
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900101 ret = onenand_bbt_read_oob(mtd,
Kyungmin Parkd438d502008-08-13 09:11:02 +0900102 from + j * mtd->writesize +
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900103 bd->offs, &ops);
Kyungmin Park17aa2802007-09-10 17:14:34 +0900104
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900105 /* If it is a initial bad block, just ignore it */
Wolfgang Denka49d10c2008-08-25 23:45:41 +0200106 if (ret == ONENAND_BBT_READ_FATAL_ERROR)
107 return -EIO;
Kyungmin Park17aa2802007-09-10 17:14:34 +0900108
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900109 if (ret || check_short_pattern
Kyungmin Parkd438d502008-08-13 09:11:02 +0900110 (&buf[j * scanlen], scanlen, mtd->writesize, bd)) {
Kyungmin Park17aa2802007-09-10 17:14:34 +0900111 bbm->bbt[i >> 3] |= 0x03 << (i & 0x6);
112 printk(KERN_WARNING
113 "Bad eraseblock %d at 0x%08x\n", i >> 1,
114 (unsigned int)from);
115 break;
116 }
117 }
118 i += 2;
Amul Kumar Sahacacbe912009-11-06 17:15:31 +0530119
120 if (FLEXONENAND(this)) {
121 rgn = flexonenand_region(mtd, from);
122 from += mtd->eraseregions[rgn].erasesize;
123 } else
124 from += (1 << bbm->bbt_erase_shift);
Kyungmin Park17aa2802007-09-10 17:14:34 +0900125 }
126
127 return 0;
128}
129
130/**
131 * onenand_memory_bbt - [GENERIC] create a memory based bad block table
132 * @param mtd MTD device structure
133 * @param bd descriptor for the good/bad block search pattern
134 *
135 * The function creates a memory based bbt by scanning the device
136 * for manufacturer / software marked good / bad blocks
137 */
138static inline int onenand_memory_bbt(struct mtd_info *mtd,
139 struct nand_bbt_descr *bd)
140{
141 unsigned char data_buf[MAX_ONENAND_PAGESIZE];
142
143 bd->options &= ~NAND_BBT_SCANEMPTY;
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 */
153static 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 Sahacacbe912009-11-06 17:15:31 +0530161 block = (int) (onenand_block(this, offs) << 1);
Kyungmin Park17aa2802007-09-10 17:14:34 +0900162 res = (bbm->bbt[block >> 3] >> (block & 0x06)) & 0x03;
163
Scott Wood3167c532008-06-20 12:38:57 -0500164 MTDDEBUG (MTD_DEBUG_LEVEL2,
Kyungmin Parkef0921d2008-11-04 09:24:07 +0900165 "onenand_isbad_bbt: bbt info for offs 0x%08x: (block %d) 0x%02x\n",
166 (unsigned int)offs, block >> 1, res);
Kyungmin Park17aa2802007-09-10 17:14:34 +0900167
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 */
194int 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 Sahacacbe912009-11-06 17:15:31 +0530200 len = this->chipsize >> (this->erase_shift + 2);
Kyungmin Park17aa2802007-09-10 17:14:34 +0900201 /* Allocate memory (2bit per block) */
202 bbm->bbt = malloc(len);
203 if (!bbm->bbt) {
204 printk(KERN_ERR "onenand_scan_bbt: Out of memory\n");
205 return -ENOMEM;
206 }
207 /* Clear the memory bad block table */
208 memset(bbm->bbt, 0x00, len);
209
210 /* Set the bad block position */
211 bbm->badblockpos = ONENAND_BADBLOCK_POS;
212
213 /* Set erase shift */
214 bbm->bbt_erase_shift = this->erase_shift;
215
216 if (!bbm->isbad_bbt)
217 bbm->isbad_bbt = onenand_isbad_bbt;
218
219 /* Scan the device to build a memory based bad block table */
220 if ((ret = onenand_memory_bbt(mtd, bd))) {
221 printk(KERN_ERR
222 "onenand_scan_bbt: Can't scan flash and build the RAM-based BBT\n");
223 free(bbm->bbt);
224 bbm->bbt = NULL;
225 }
226
227 return ret;
228}
229
230/*
231 * Define some generic bad / good block scan pattern which are used
232 * while scanning a device for factory marked good / bad blocks.
233 */
234static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
235
236static struct nand_bbt_descr largepage_memorybased = {
237 .options = 0,
238 .offs = 0,
239 .len = 2,
240 .pattern = scan_ff_pattern,
241};
242
243/**
244 * onenand_default_bbt - [OneNAND Interface] Select a default bad block table for the device
245 * @param mtd MTD device structure
246 *
247 * This function selects the default bad block table
248 * support for the device and calls the onenand_scan_bbt function
249 */
250int onenand_default_bbt(struct mtd_info *mtd)
251{
252 struct onenand_chip *this = mtd->priv;
253 struct bbm_info *bbm;
254
255 this->bbm = malloc(sizeof(struct bbm_info));
256 if (!this->bbm)
257 return -ENOMEM;
258
259 bbm = this->bbm;
260
261 memset(bbm, 0, sizeof(struct bbm_info));
262
263 /* 1KB page has same configuration as 2KB page */
264 if (!bbm->badblock_pattern)
265 bbm->badblock_pattern = &largepage_memorybased;
266
267 return onenand_scan_bbt(mtd, bbm->badblock_pattern);
268}