Merge branch 'msm-mmc_sdcc' of git://codeaurora.org/quic/kernel/dwalker/linux-msm
[pandora-kernel.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 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>
18
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  */
32 static 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  */
56 static 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;
64         struct mtd_oob_ops ops;
65         int rgn;
66
67         printk(KERN_INFO "Scanning device for bad blocks\n");
68
69         len = 2;
70
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          */
79         numblocks = this->chipsize >> (bbm->bbt_erase_shift - 1);
80         startblock = 0;
81         from = 0;
82
83         ops.mode = MTD_OOB_PLACE;
84         ops.ooblen = readlen;
85         ops.oobbuf = buf;
86         ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
87
88         for (i = startblock; i < numblocks; ) {
89                 int ret;
90
91                 for (j = 0; j < len; j++) {
92                         /* No need to read pages fully,
93                          * just read required OOB bytes */
94                         ret = onenand_bbt_read_oob(mtd, from + j * mtd->writesize + bd->offs, &ops);
95
96                         /* If it is a initial bad block, just ignore it */
97                         if (ret == ONENAND_BBT_READ_FATAL_ERROR)
98                                 return -EIO;
99
100                         if (ret || check_short_pattern(&buf[j * scanlen], scanlen, mtd->writesize, bd)) {
101                                 bbm->bbt[i >> 3] |= 0x03 << (i & 0x6);
102                                 printk(KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
103                                         i >> 1, (unsigned int) from);
104                                 mtd->ecc_stats.badblocks++;
105                                 break;
106                         }
107                 }
108                 i += 2;
109
110                 if (FLEXONENAND(this)) {
111                         rgn = flexonenand_region(mtd, from);
112                         from += mtd->eraseregions[rgn].erasesize;
113                 } else
114                         from += (1 << bbm->bbt_erase_shift);
115         }
116
117         return 0;
118 }
119
120
121 /**
122  * onenand_memory_bbt - [GENERIC] create a memory based bad block table
123  * @param mtd           MTD device structure
124  * @param bd            descriptor for the good/bad block search pattern
125  *
126  * The function creates a memory based bbt by scanning the device
127  * for manufacturer / software marked good / bad blocks
128  */
129 static inline int onenand_memory_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
130 {
131         struct onenand_chip *this = mtd->priv;
132
133         bd->options &= ~NAND_BBT_SCANEMPTY;
134         return create_bbt(mtd, this->page_buf, bd, -1);
135 }
136
137 /**
138  * onenand_isbad_bbt - [OneNAND Interface] Check if a block is bad
139  * @param mtd           MTD device structure
140  * @param offs          offset in the device
141  * @param allowbbt      allow access to bad block table region
142  */
143 static int onenand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
144 {
145         struct onenand_chip *this = mtd->priv;
146         struct bbm_info *bbm = this->bbm;
147         int block;
148         uint8_t res;
149
150         /* Get block number * 2 */
151         block = (int) (onenand_block(this, offs) << 1);
152         res = (bbm->bbt[block >> 3] >> (block & 0x06)) & 0x03;
153
154         DEBUG(MTD_DEBUG_LEVEL2, "onenand_isbad_bbt: bbt info for offs 0x%08x: (block %d) 0x%02x\n",
155                 (unsigned int) offs, block >> 1, res);
156
157         switch ((int) res) {
158         case 0x00:      return 0;
159         case 0x01:      return 1;
160         case 0x02:      return allowbbt ? 0 : 1;
161         }
162
163         return 1;
164 }
165
166 /**
167  * onenand_scan_bbt - [OneNAND Interface] scan, find, read and maybe create bad block table(s)
168  * @param mtd           MTD device structure
169  * @param bd            descriptor for the good/bad block search pattern
170  *
171  * The function checks, if a bad block table(s) is/are already
172  * available. If not it scans the device for manufacturer
173  * marked good / bad blocks and writes the bad block table(s) to
174  * the selected place.
175  *
176  * The bad block table memory is allocated here. It is freed
177  * by the onenand_release function.
178  *
179  */
180 int onenand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
181 {
182         struct onenand_chip *this = mtd->priv;
183         struct bbm_info *bbm = this->bbm;
184         int len, ret = 0;
185
186         len = this->chipsize >> (this->erase_shift + 2);
187         /* Allocate memory (2bit per block) and clear the memory bad block table */
188         bbm->bbt = kzalloc(len, GFP_KERNEL);
189         if (!bbm->bbt) {
190                 printk(KERN_ERR "onenand_scan_bbt: Out of memory\n");
191                 return -ENOMEM;
192         }
193
194         /* Set the bad block position */
195         bbm->badblockpos = ONENAND_BADBLOCK_POS;
196
197         /* Set erase shift */
198         bbm->bbt_erase_shift = this->erase_shift;
199
200         if (!bbm->isbad_bbt)
201                 bbm->isbad_bbt = onenand_isbad_bbt;
202
203         /* Scan the device to build a memory based bad block table */
204         if ((ret = onenand_memory_bbt(mtd, bd))) {
205                 printk(KERN_ERR "onenand_scan_bbt: Can't scan flash and build the RAM-based BBT\n");
206                 kfree(bbm->bbt);
207                 bbm->bbt = NULL;
208         }
209
210         return ret;
211 }
212
213 /*
214  * Define some generic bad / good block scan pattern which are used
215  * while scanning a device for factory marked good / bad blocks.
216  */
217 static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
218
219 static struct nand_bbt_descr largepage_memorybased = {
220         .options = 0,
221         .offs = 0,
222         .len = 2,
223         .pattern = scan_ff_pattern,
224 };
225
226 /**
227  * onenand_default_bbt - [OneNAND Interface] Select a default bad block table for the device
228  * @param mtd           MTD device structure
229  *
230  * This function selects the default bad block table
231  * support for the device and calls the onenand_scan_bbt function
232  */
233 int onenand_default_bbt(struct mtd_info *mtd)
234 {
235         struct onenand_chip *this = mtd->priv;
236         struct bbm_info *bbm;
237
238         this->bbm = kzalloc(sizeof(struct bbm_info), GFP_KERNEL);
239         if (!this->bbm)
240                 return -ENOMEM;
241
242         bbm = this->bbm;
243
244         /* 1KB page has same configuration as 2KB page */
245         if (!bbm->badblock_pattern)
246                 bbm->badblock_pattern = &largepage_memorybased;
247
248         return onenand_scan_bbt(mtd, bbm->badblock_pattern);
249 }
250
251 EXPORT_SYMBOL(onenand_scan_bbt);
252 EXPORT_SYMBOL(onenand_default_bbt);