]> git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/mtd/onenand/onenand_bbt.c
Merge branch 'master' of git://git.denx.de/u-boot-nand-flash
[people/ms/u-boot.git] / drivers / mtd / onenand / onenand_bbt.c
1 /*
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>
18 #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 */
37 static 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 */
62 static 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;
71
72 printk(KERN_INFO "Scanning device for bad blocks\n");
73
74 len = 1;
75
76 /* We need only read few bytes from the OOB area */
77 scanlen = ooblen = 0;
78 readlen = bd->len;
79
80 /* chip == -1 case only */
81 /* Note that numblocks is 2 * (real numblocks) here;
82 * see i += 2 below as it makses shifting and masking less painful
83 */
84 numblocks = mtd->size >> (bbm->bbt_erase_shift - 1);
85 startblock = 0;
86 from = 0;
87
88 for (i = startblock; i < numblocks;) {
89 int ret;
90
91 for (j = 0; j < len; j++) {
92 size_t retlen;
93
94 /* No need to read pages fully,
95 * just read required OOB bytes */
96 ret = onenand_read_oob(mtd,
97 from + j * mtd->writesize +
98 bd->offs, readlen, &retlen,
99 &buf[0]);
100
101 if (ret && ret != -EAGAIN) {
102 printk("ret = %d\n", ret);
103 return ret;
104 }
105
106 if (check_short_pattern
107 (&buf[j * scanlen], scanlen, mtd->writesize, bd)) {
108 bbm->bbt[i >> 3] |= 0x03 << (i & 0x6);
109 printk(KERN_WARNING
110 "Bad eraseblock %d at 0x%08x\n", i >> 1,
111 (unsigned int)from);
112 break;
113 }
114 }
115 i += 2;
116 from += (1 << bbm->bbt_erase_shift);
117 }
118
119 return 0;
120 }
121
122 /**
123 * onenand_memory_bbt - [GENERIC] create a memory based bad block table
124 * @param mtd MTD device structure
125 * @param bd descriptor for the good/bad block search pattern
126 *
127 * The function creates a memory based bbt by scanning the device
128 * for manufacturer / software marked good / bad blocks
129 */
130 static inline int onenand_memory_bbt(struct mtd_info *mtd,
131 struct nand_bbt_descr *bd)
132 {
133 unsigned char data_buf[MAX_ONENAND_PAGESIZE];
134
135 bd->options &= ~NAND_BBT_SCANEMPTY;
136 return create_bbt(mtd, data_buf, bd, -1);
137 }
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 */
145 static 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 */
153 block = (int)(offs >> (bbm->bbt_erase_shift - 1));
154 res = (bbm->bbt[block >> 3] >> (block & 0x06)) & 0x03;
155
156 MTDDEBUG (MTD_DEBUG_LEVEL2,
157 "onenand_isbad_bbt: bbt info for offs 0x%08x: (block %d) 0x%02x\n",
158 (unsigned int)offs, block >> 1, res);
159
160 switch ((int)res) {
161 case 0x00:
162 return 0;
163 case 0x01:
164 return 1;
165 case 0x02:
166 return allowbbt ? 0 : 1;
167 }
168
169 return 1;
170 }
171
172 /**
173 * onenand_scan_bbt - [OneNAND Interface] scan, find, read and maybe create bad block table(s)
174 * @param mtd MTD device structure
175 * @param bd descriptor for the good/bad block search pattern
176 *
177 * The function checks, if a bad block table(s) is/are already
178 * available. If not it scans the device for manufacturer
179 * marked good / bad blocks and writes the bad block table(s) to
180 * the selected place.
181 *
182 * The bad block table memory is allocated here. It must be freed
183 * by calling the onenand_free_bbt function.
184 *
185 */
186 int onenand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
187 {
188 struct onenand_chip *this = mtd->priv;
189 struct bbm_info *bbm = this->bbm;
190 int len, ret = 0;
191
192 len = mtd->size >> (this->erase_shift + 2);
193 /* Allocate memory (2bit per block) */
194 bbm->bbt = malloc(len);
195 if (!bbm->bbt) {
196 printk(KERN_ERR "onenand_scan_bbt: Out of memory\n");
197 return -ENOMEM;
198 }
199 /* Clear the memory bad block table */
200 memset(bbm->bbt, 0x00, len);
201
202 /* Set the bad block position */
203 bbm->badblockpos = ONENAND_BADBLOCK_POS;
204
205 /* Set erase shift */
206 bbm->bbt_erase_shift = this->erase_shift;
207
208 if (!bbm->isbad_bbt)
209 bbm->isbad_bbt = onenand_isbad_bbt;
210
211 /* Scan the device to build a memory based bad block table */
212 if ((ret = onenand_memory_bbt(mtd, bd))) {
213 printk(KERN_ERR
214 "onenand_scan_bbt: Can't scan flash and build the RAM-based BBT\n");
215 free(bbm->bbt);
216 bbm->bbt = NULL;
217 }
218
219 return ret;
220 }
221
222 /*
223 * Define some generic bad / good block scan pattern which are used
224 * while scanning a device for factory marked good / bad blocks.
225 */
226 static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
227
228 static struct nand_bbt_descr largepage_memorybased = {
229 .options = 0,
230 .offs = 0,
231 .len = 2,
232 .pattern = scan_ff_pattern,
233 };
234
235 /**
236 * onenand_default_bbt - [OneNAND Interface] Select a default bad block table for the device
237 * @param mtd MTD device structure
238 *
239 * This function selects the default bad block table
240 * support for the device and calls the onenand_scan_bbt function
241 */
242 int onenand_default_bbt(struct mtd_info *mtd)
243 {
244 struct onenand_chip *this = mtd->priv;
245 struct bbm_info *bbm;
246
247 this->bbm = malloc(sizeof(struct bbm_info));
248 if (!this->bbm)
249 return -ENOMEM;
250
251 bbm = this->bbm;
252
253 memset(bbm, 0, sizeof(struct bbm_info));
254
255 /* 1KB page has same configuration as 2KB page */
256 if (!bbm->badblock_pattern)
257 bbm->badblock_pattern = &largepage_memorybased;
258
259 return onenand_scan_bbt(mtd, bbm->badblock_pattern);
260 }