blob: dde11ae45f76b7a8a89701a141a0f6c0d279c22e [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 *
6 * Copyright(c) 2005-2007 Samsung Electronics
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>
Kyungmin Park17aa2802007-09-10 17:14:34 +090018#include <linux/mtd/compat.h>
19#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.
57 * Applies only if NAND_BBT_PERCHIP option is set
58 *
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;
Kyungmin Park17aa2802007-09-10 17:14:34 +090072
73 printk(KERN_INFO "Scanning device for bad blocks\n");
74
75 len = 1;
76
77 /* We need only read few bytes from the OOB area */
78 scanlen = ooblen = 0;
79 readlen = bd->len;
80
81 /* chip == -1 case only */
82 /* Note that numblocks is 2 * (real numblocks) here;
83 * see i += 2 below as it makses shifting and masking less painful
84 */
85 numblocks = mtd->size >> (bbm->bbt_erase_shift - 1);
86 startblock = 0;
87 from = 0;
88
Kyungmin Parkbfd7f382008-08-19 08:42:53 +090089 ops.mode = MTD_OOB_PLACE;
90 ops.ooblen = readlen;
91 ops.oobbuf = buf;
92 ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
93
Kyungmin Park17aa2802007-09-10 17:14:34 +090094 for (i = startblock; i < numblocks;) {
95 int ret;
96
97 for (j = 0; j < len; j++) {
Kyungmin Park17aa2802007-09-10 17:14:34 +090098 /* No need to read pages fully,
99 * just read required OOB bytes */
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900100 ret = onenand_bbt_read_oob(mtd,
Kyungmin Parkd438d502008-08-13 09:11:02 +0900101 from + j * mtd->writesize +
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900102 bd->offs, &ops);
Kyungmin Park17aa2802007-09-10 17:14:34 +0900103
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900104 /* If it is a initial bad block, just ignore it */
105 if (ret == ONENAND_BBT_READ_FATAL_ERROR)
106 return -EIO;
Kyungmin Park17aa2802007-09-10 17:14:34 +0900107
Kyungmin Parkbfd7f382008-08-19 08:42:53 +0900108 if (ret || check_short_pattern
Kyungmin Parkd438d502008-08-13 09:11:02 +0900109 (&buf[j * scanlen], scanlen, mtd->writesize, bd)) {
Kyungmin Park17aa2802007-09-10 17:14:34 +0900110 bbm->bbt[i >> 3] |= 0x03 << (i & 0x6);
111 printk(KERN_WARNING
112 "Bad eraseblock %d at 0x%08x\n", i >> 1,
113 (unsigned int)from);
114 break;
115 }
116 }
117 i += 2;
118 from += (1 << bbm->bbt_erase_shift);
119 }
120
121 return 0;
122}
123
124/**
125 * onenand_memory_bbt - [GENERIC] create a memory based bad block table
126 * @param mtd MTD device structure
127 * @param bd descriptor for the good/bad block search pattern
128 *
129 * The function creates a memory based bbt by scanning the device
130 * for manufacturer / software marked good / bad blocks
131 */
132static inline int onenand_memory_bbt(struct mtd_info *mtd,
133 struct nand_bbt_descr *bd)
134{
135 unsigned char data_buf[MAX_ONENAND_PAGESIZE];
136
137 bd->options &= ~NAND_BBT_SCANEMPTY;
138 return create_bbt(mtd, data_buf, bd, -1);
139}
140
141/**
142 * onenand_isbad_bbt - [OneNAND Interface] Check if a block is bad
143 * @param mtd MTD device structure
144 * @param offs offset in the device
145 * @param allowbbt allow access to bad block table region
146 */
147static int onenand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
148{
149 struct onenand_chip *this = mtd->priv;
150 struct bbm_info *bbm = this->bbm;
151 int block;
152 uint8_t res;
153
154 /* Get block number * 2 */
155 block = (int)(offs >> (bbm->bbt_erase_shift - 1));
156 res = (bbm->bbt[block >> 3] >> (block & 0x06)) & 0x03;
157
Scott Wood3167c532008-06-20 12:38:57 -0500158 MTDDEBUG (MTD_DEBUG_LEVEL2,
159 "onenand_isbad_bbt: bbt info for offs 0x%08x: (block %d) 0x%02x\n",
160 (unsigned int)offs, block >> 1, res);
Kyungmin Park17aa2802007-09-10 17:14:34 +0900161
162 switch ((int)res) {
163 case 0x00:
164 return 0;
165 case 0x01:
166 return 1;
167 case 0x02:
168 return allowbbt ? 0 : 1;
169 }
170
171 return 1;
172}
173
174/**
175 * onenand_scan_bbt - [OneNAND Interface] scan, find, read and maybe create bad block table(s)
176 * @param mtd MTD device structure
177 * @param bd descriptor for the good/bad block search pattern
178 *
179 * The function checks, if a bad block table(s) is/are already
180 * available. If not it scans the device for manufacturer
181 * marked good / bad blocks and writes the bad block table(s) to
182 * the selected place.
183 *
184 * The bad block table memory is allocated here. It must be freed
185 * by calling the onenand_free_bbt function.
186 *
187 */
188int onenand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
189{
190 struct onenand_chip *this = mtd->priv;
191 struct bbm_info *bbm = this->bbm;
192 int len, ret = 0;
193
194 len = mtd->size >> (this->erase_shift + 2);
195 /* Allocate memory (2bit per block) */
196 bbm->bbt = malloc(len);
197 if (!bbm->bbt) {
198 printk(KERN_ERR "onenand_scan_bbt: Out of memory\n");
199 return -ENOMEM;
200 }
201 /* Clear the memory bad block table */
202 memset(bbm->bbt, 0x00, len);
203
204 /* Set the bad block position */
205 bbm->badblockpos = ONENAND_BADBLOCK_POS;
206
207 /* Set erase shift */
208 bbm->bbt_erase_shift = this->erase_shift;
209
210 if (!bbm->isbad_bbt)
211 bbm->isbad_bbt = onenand_isbad_bbt;
212
213 /* Scan the device to build a memory based bad block table */
214 if ((ret = onenand_memory_bbt(mtd, bd))) {
215 printk(KERN_ERR
216 "onenand_scan_bbt: Can't scan flash and build the RAM-based BBT\n");
217 free(bbm->bbt);
218 bbm->bbt = NULL;
219 }
220
221 return ret;
222}
223
224/*
225 * Define some generic bad / good block scan pattern which are used
226 * while scanning a device for factory marked good / bad blocks.
227 */
228static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
229
230static struct nand_bbt_descr largepage_memorybased = {
231 .options = 0,
232 .offs = 0,
233 .len = 2,
234 .pattern = scan_ff_pattern,
235};
236
237/**
238 * onenand_default_bbt - [OneNAND Interface] Select a default bad block table for the device
239 * @param mtd MTD device structure
240 *
241 * This function selects the default bad block table
242 * support for the device and calls the onenand_scan_bbt function
243 */
244int onenand_default_bbt(struct mtd_info *mtd)
245{
246 struct onenand_chip *this = mtd->priv;
247 struct bbm_info *bbm;
248
249 this->bbm = malloc(sizeof(struct bbm_info));
250 if (!this->bbm)
251 return -ENOMEM;
252
253 bbm = this->bbm;
254
255 memset(bbm, 0, sizeof(struct bbm_info));
256
257 /* 1KB page has same configuration as 2KB page */
258 if (!bbm->badblock_pattern)
259 bbm->badblock_pattern = &largepage_memorybased;
260
261 return onenand_scan_bbt(mtd, bbm->badblock_pattern);
262}