1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Overview:
3*4882a593Smuzhiyun * Bad block table support for the NAND driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright © 2004 Thomas Gleixner (tglx@linutronix.de)
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify
8*4882a593Smuzhiyun * it under the terms of the GNU General Public License version 2 as
9*4882a593Smuzhiyun * published by the Free Software Foundation.
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * Description:
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * When nand_scan_bbt is called, then it tries to find the bad block table
14*4882a593Smuzhiyun * depending on the options in the BBT descriptor(s). If no flash based BBT
15*4882a593Smuzhiyun * (NAND_BBT_USE_FLASH) is specified then the device is scanned for factory
16*4882a593Smuzhiyun * marked good / bad blocks. This information is used to create a memory BBT.
17*4882a593Smuzhiyun * Once a new bad block is discovered then the "factory" information is updated
18*4882a593Smuzhiyun * on the device.
19*4882a593Smuzhiyun * If a flash based BBT is specified then the function first tries to find the
20*4882a593Smuzhiyun * BBT on flash. If a BBT is found then the contents are read and the memory
21*4882a593Smuzhiyun * based BBT is created. If a mirrored BBT is selected then the mirror is
22*4882a593Smuzhiyun * searched too and the versions are compared. If the mirror has a greater
23*4882a593Smuzhiyun * version number, then the mirror BBT is used to build the memory based BBT.
24*4882a593Smuzhiyun * If the tables are not versioned, then we "or" the bad block information.
25*4882a593Smuzhiyun * If one of the BBTs is out of date or does not exist it is (re)created.
26*4882a593Smuzhiyun * If no BBT exists at all then the device is scanned for factory marked
27*4882a593Smuzhiyun * good / bad blocks and the bad block tables are created.
28*4882a593Smuzhiyun *
29*4882a593Smuzhiyun * For manufacturer created BBTs like the one found on M-SYS DOC devices
30*4882a593Smuzhiyun * the BBT is searched and read but never created
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * The auto generated bad block table is located in the last good blocks
33*4882a593Smuzhiyun * of the device. The table is mirrored, so it can be updated eventually.
34*4882a593Smuzhiyun * The table is marked in the OOB area with an ident pattern and a version
35*4882a593Smuzhiyun * number which indicates which of both tables is more up to date. If the NAND
36*4882a593Smuzhiyun * controller needs the complete OOB area for the ECC information then the
37*4882a593Smuzhiyun * option NAND_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of
38*4882a593Smuzhiyun * course): it moves the ident pattern and the version byte into the data area
39*4882a593Smuzhiyun * and the OOB area will remain untouched.
40*4882a593Smuzhiyun *
41*4882a593Smuzhiyun * The table uses 2 bits per block
42*4882a593Smuzhiyun * 11b: block is good
43*4882a593Smuzhiyun * 00b: block is factory marked bad
44*4882a593Smuzhiyun * 01b, 10b: block is marked bad due to wear
45*4882a593Smuzhiyun *
46*4882a593Smuzhiyun * The memory bad block table uses the following scheme:
47*4882a593Smuzhiyun * 00b: block is good
48*4882a593Smuzhiyun * 01b: block is marked bad due to wear
49*4882a593Smuzhiyun * 10b: block is reserved (to protect the bbt area)
50*4882a593Smuzhiyun * 11b: block is factory marked bad
51*4882a593Smuzhiyun *
52*4882a593Smuzhiyun * Multichip devices like DOC store the bad block info per floor.
53*4882a593Smuzhiyun *
54*4882a593Smuzhiyun * Following assumptions are made:
55*4882a593Smuzhiyun * - bbts start at a page boundary, if autolocated on a block boundary
56*4882a593Smuzhiyun * - the space necessary for a bbt in FLASH does not exceed a block boundary
57*4882a593Smuzhiyun *
58*4882a593Smuzhiyun */
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun #include <common.h>
61*4882a593Smuzhiyun #include <malloc.h>
62*4882a593Smuzhiyun #include <linux/compat.h>
63*4882a593Smuzhiyun #include <linux/mtd/mtd.h>
64*4882a593Smuzhiyun #include <linux/mtd/bbm.h>
65*4882a593Smuzhiyun #include <linux/mtd/rawnand.h>
66*4882a593Smuzhiyun #include <linux/bitops.h>
67*4882a593Smuzhiyun #include <linux/string.h>
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun #define BBT_BLOCK_GOOD 0x00
70*4882a593Smuzhiyun #define BBT_BLOCK_WORN 0x01
71*4882a593Smuzhiyun #define BBT_BLOCK_RESERVED 0x02
72*4882a593Smuzhiyun #define BBT_BLOCK_FACTORY_BAD 0x03
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun #define BBT_ENTRY_MASK 0x03
75*4882a593Smuzhiyun #define BBT_ENTRY_SHIFT 2
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun static int nand_update_bbt(struct mtd_info *mtd, loff_t offs);
78*4882a593Smuzhiyun
bbt_get_entry(struct nand_chip * chip,int block)79*4882a593Smuzhiyun static inline uint8_t bbt_get_entry(struct nand_chip *chip, int block)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun uint8_t entry = chip->bbt[block >> BBT_ENTRY_SHIFT];
82*4882a593Smuzhiyun entry >>= (block & BBT_ENTRY_MASK) * 2;
83*4882a593Smuzhiyun return entry & BBT_ENTRY_MASK;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun
bbt_mark_entry(struct nand_chip * chip,int block,uint8_t mark)86*4882a593Smuzhiyun static inline void bbt_mark_entry(struct nand_chip *chip, int block,
87*4882a593Smuzhiyun uint8_t mark)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun uint8_t msk = (mark & BBT_ENTRY_MASK) << ((block & BBT_ENTRY_MASK) * 2);
90*4882a593Smuzhiyun chip->bbt[block >> BBT_ENTRY_SHIFT] |= msk;
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun
check_pattern_no_oob(uint8_t * buf,struct nand_bbt_descr * td)93*4882a593Smuzhiyun static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun if (memcmp(buf, td->pattern, td->len))
96*4882a593Smuzhiyun return -1;
97*4882a593Smuzhiyun return 0;
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /**
101*4882a593Smuzhiyun * check_pattern - [GENERIC] check if a pattern is in the buffer
102*4882a593Smuzhiyun * @buf: the buffer to search
103*4882a593Smuzhiyun * @len: the length of buffer to search
104*4882a593Smuzhiyun * @paglen: the pagelength
105*4882a593Smuzhiyun * @td: search pattern descriptor
106*4882a593Smuzhiyun *
107*4882a593Smuzhiyun * Check for a pattern at the given place. Used to search bad block tables and
108*4882a593Smuzhiyun * good / bad block identifiers.
109*4882a593Smuzhiyun */
check_pattern(uint8_t * buf,int len,int paglen,struct nand_bbt_descr * td)110*4882a593Smuzhiyun static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun if (td->options & NAND_BBT_NO_OOB)
113*4882a593Smuzhiyun return check_pattern_no_oob(buf, td);
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun /* Compare the pattern */
116*4882a593Smuzhiyun if (memcmp(buf + paglen + td->offs, td->pattern, td->len))
117*4882a593Smuzhiyun return -1;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun return 0;
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun /**
123*4882a593Smuzhiyun * check_short_pattern - [GENERIC] check if a pattern is in the buffer
124*4882a593Smuzhiyun * @buf: the buffer to search
125*4882a593Smuzhiyun * @td: search pattern descriptor
126*4882a593Smuzhiyun *
127*4882a593Smuzhiyun * Check for a pattern at the given place. Used to search bad block tables and
128*4882a593Smuzhiyun * good / bad block identifiers. Same as check_pattern, but no optional empty
129*4882a593Smuzhiyun * check.
130*4882a593Smuzhiyun */
check_short_pattern(uint8_t * buf,struct nand_bbt_descr * td)131*4882a593Smuzhiyun static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun /* Compare the pattern */
134*4882a593Smuzhiyun if (memcmp(buf + td->offs, td->pattern, td->len))
135*4882a593Smuzhiyun return -1;
136*4882a593Smuzhiyun return 0;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun /**
140*4882a593Smuzhiyun * add_marker_len - compute the length of the marker in data area
141*4882a593Smuzhiyun * @td: BBT descriptor used for computation
142*4882a593Smuzhiyun *
143*4882a593Smuzhiyun * The length will be 0 if the marker is located in OOB area.
144*4882a593Smuzhiyun */
add_marker_len(struct nand_bbt_descr * td)145*4882a593Smuzhiyun static u32 add_marker_len(struct nand_bbt_descr *td)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun u32 len;
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun if (!(td->options & NAND_BBT_NO_OOB))
150*4882a593Smuzhiyun return 0;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun len = td->len;
153*4882a593Smuzhiyun if (td->options & NAND_BBT_VERSION)
154*4882a593Smuzhiyun len++;
155*4882a593Smuzhiyun return len;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun /**
159*4882a593Smuzhiyun * read_bbt - [GENERIC] Read the bad block table starting from page
160*4882a593Smuzhiyun * @mtd: MTD device structure
161*4882a593Smuzhiyun * @buf: temporary buffer
162*4882a593Smuzhiyun * @page: the starting page
163*4882a593Smuzhiyun * @num: the number of bbt descriptors to read
164*4882a593Smuzhiyun * @td: the bbt describtion table
165*4882a593Smuzhiyun * @offs: block number offset in the table
166*4882a593Smuzhiyun *
167*4882a593Smuzhiyun * Read the bad block table starting from page.
168*4882a593Smuzhiyun */
read_bbt(struct mtd_info * mtd,uint8_t * buf,int page,int num,struct nand_bbt_descr * td,int offs)169*4882a593Smuzhiyun static int read_bbt(struct mtd_info *mtd, uint8_t *buf, int page, int num,
170*4882a593Smuzhiyun struct nand_bbt_descr *td, int offs)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun int res, ret = 0, i, j, act = 0;
173*4882a593Smuzhiyun struct nand_chip *this = mtd_to_nand(mtd);
174*4882a593Smuzhiyun size_t retlen, len, totlen;
175*4882a593Smuzhiyun loff_t from;
176*4882a593Smuzhiyun int bits = td->options & NAND_BBT_NRBITS_MSK;
177*4882a593Smuzhiyun uint8_t msk = (uint8_t)((1 << bits) - 1);
178*4882a593Smuzhiyun u32 marker_len;
179*4882a593Smuzhiyun int reserved_block_code = td->reserved_block_code;
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun totlen = (num * bits) >> 3;
182*4882a593Smuzhiyun marker_len = add_marker_len(td);
183*4882a593Smuzhiyun from = ((loff_t)page) << this->page_shift;
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun while (totlen) {
186*4882a593Smuzhiyun len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
187*4882a593Smuzhiyun if (marker_len) {
188*4882a593Smuzhiyun /*
189*4882a593Smuzhiyun * In case the BBT marker is not in the OOB area it
190*4882a593Smuzhiyun * will be just in the first page.
191*4882a593Smuzhiyun */
192*4882a593Smuzhiyun len -= marker_len;
193*4882a593Smuzhiyun from += marker_len;
194*4882a593Smuzhiyun marker_len = 0;
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun res = mtd_read(mtd, from, len, &retlen, buf);
197*4882a593Smuzhiyun if (res < 0) {
198*4882a593Smuzhiyun if (mtd_is_eccerr(res)) {
199*4882a593Smuzhiyun pr_info("nand_bbt: ECC error in BBT at 0x%012llx\n",
200*4882a593Smuzhiyun from & ~mtd->writesize);
201*4882a593Smuzhiyun return res;
202*4882a593Smuzhiyun } else if (mtd_is_bitflip(res)) {
203*4882a593Smuzhiyun pr_info("nand_bbt: corrected error in BBT at 0x%012llx\n",
204*4882a593Smuzhiyun from & ~mtd->writesize);
205*4882a593Smuzhiyun ret = res;
206*4882a593Smuzhiyun } else {
207*4882a593Smuzhiyun pr_info("nand_bbt: error reading BBT\n");
208*4882a593Smuzhiyun return res;
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun /* Analyse data */
213*4882a593Smuzhiyun for (i = 0; i < len; i++) {
214*4882a593Smuzhiyun uint8_t dat = buf[i];
215*4882a593Smuzhiyun for (j = 0; j < 8; j += bits, act++) {
216*4882a593Smuzhiyun uint8_t tmp = (dat >> j) & msk;
217*4882a593Smuzhiyun if (tmp == msk)
218*4882a593Smuzhiyun continue;
219*4882a593Smuzhiyun if (reserved_block_code && (tmp == reserved_block_code)) {
220*4882a593Smuzhiyun pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
221*4882a593Smuzhiyun (loff_t)(offs + act) <<
222*4882a593Smuzhiyun this->bbt_erase_shift);
223*4882a593Smuzhiyun bbt_mark_entry(this, offs + act,
224*4882a593Smuzhiyun BBT_BLOCK_RESERVED);
225*4882a593Smuzhiyun mtd->ecc_stats.bbtblocks++;
226*4882a593Smuzhiyun continue;
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun /*
229*4882a593Smuzhiyun * Leave it for now, if it's matured we can
230*4882a593Smuzhiyun * move this message to pr_debug.
231*4882a593Smuzhiyun */
232*4882a593Smuzhiyun pr_info("nand_read_bbt: bad block at 0x%012llx\n",
233*4882a593Smuzhiyun (loff_t)(offs + act) <<
234*4882a593Smuzhiyun this->bbt_erase_shift);
235*4882a593Smuzhiyun /* Factory marked bad or worn out? */
236*4882a593Smuzhiyun if (tmp == 0)
237*4882a593Smuzhiyun bbt_mark_entry(this, offs + act,
238*4882a593Smuzhiyun BBT_BLOCK_FACTORY_BAD);
239*4882a593Smuzhiyun else
240*4882a593Smuzhiyun bbt_mark_entry(this, offs + act,
241*4882a593Smuzhiyun BBT_BLOCK_WORN);
242*4882a593Smuzhiyun mtd->ecc_stats.badblocks++;
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun totlen -= len;
246*4882a593Smuzhiyun from += len;
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun return ret;
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun /**
252*4882a593Smuzhiyun * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
253*4882a593Smuzhiyun * @mtd: MTD device structure
254*4882a593Smuzhiyun * @buf: temporary buffer
255*4882a593Smuzhiyun * @td: descriptor for the bad block table
256*4882a593Smuzhiyun * @chip: read the table for a specific chip, -1 read all chips; applies only if
257*4882a593Smuzhiyun * NAND_BBT_PERCHIP option is set
258*4882a593Smuzhiyun *
259*4882a593Smuzhiyun * Read the bad block table for all chips starting at a given page. We assume
260*4882a593Smuzhiyun * that the bbt bits are in consecutive order.
261*4882a593Smuzhiyun */
read_abs_bbt(struct mtd_info * mtd,uint8_t * buf,struct nand_bbt_descr * td,int chip)262*4882a593Smuzhiyun static int read_abs_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td, int chip)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun struct nand_chip *this = mtd_to_nand(mtd);
265*4882a593Smuzhiyun int res = 0, i;
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun if (td->options & NAND_BBT_PERCHIP) {
268*4882a593Smuzhiyun int offs = 0;
269*4882a593Smuzhiyun for (i = 0; i < this->numchips; i++) {
270*4882a593Smuzhiyun if (chip == -1 || chip == i)
271*4882a593Smuzhiyun res = read_bbt(mtd, buf, td->pages[i],
272*4882a593Smuzhiyun this->chipsize >> this->bbt_erase_shift,
273*4882a593Smuzhiyun td, offs);
274*4882a593Smuzhiyun if (res)
275*4882a593Smuzhiyun return res;
276*4882a593Smuzhiyun offs += this->chipsize >> this->bbt_erase_shift;
277*4882a593Smuzhiyun }
278*4882a593Smuzhiyun } else {
279*4882a593Smuzhiyun res = read_bbt(mtd, buf, td->pages[0],
280*4882a593Smuzhiyun mtd->size >> this->bbt_erase_shift, td, 0);
281*4882a593Smuzhiyun if (res)
282*4882a593Smuzhiyun return res;
283*4882a593Smuzhiyun }
284*4882a593Smuzhiyun return 0;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun /* BBT marker is in the first page, no OOB */
scan_read_data(struct mtd_info * mtd,uint8_t * buf,loff_t offs,struct nand_bbt_descr * td)288*4882a593Smuzhiyun static int scan_read_data(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
289*4882a593Smuzhiyun struct nand_bbt_descr *td)
290*4882a593Smuzhiyun {
291*4882a593Smuzhiyun size_t retlen;
292*4882a593Smuzhiyun size_t len;
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun len = td->len;
295*4882a593Smuzhiyun if (td->options & NAND_BBT_VERSION)
296*4882a593Smuzhiyun len++;
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun return mtd_read(mtd, offs, len, &retlen, buf);
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun /**
302*4882a593Smuzhiyun * scan_read_oob - [GENERIC] Scan data+OOB region to buffer
303*4882a593Smuzhiyun * @mtd: MTD device structure
304*4882a593Smuzhiyun * @buf: temporary buffer
305*4882a593Smuzhiyun * @offs: offset at which to scan
306*4882a593Smuzhiyun * @len: length of data region to read
307*4882a593Smuzhiyun *
308*4882a593Smuzhiyun * Scan read data from data+OOB. May traverse multiple pages, interleaving
309*4882a593Smuzhiyun * page,OOB,page,OOB,... in buf. Completes transfer and returns the "strongest"
310*4882a593Smuzhiyun * ECC condition (error or bitflip). May quit on the first (non-ECC) error.
311*4882a593Smuzhiyun */
scan_read_oob(struct mtd_info * mtd,uint8_t * buf,loff_t offs,size_t len)312*4882a593Smuzhiyun static int scan_read_oob(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
313*4882a593Smuzhiyun size_t len)
314*4882a593Smuzhiyun {
315*4882a593Smuzhiyun struct mtd_oob_ops ops;
316*4882a593Smuzhiyun int res, ret = 0;
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun ops.mode = MTD_OPS_PLACE_OOB;
319*4882a593Smuzhiyun ops.ooboffs = 0;
320*4882a593Smuzhiyun ops.ooblen = mtd->oobsize;
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun while (len > 0) {
323*4882a593Smuzhiyun ops.datbuf = buf;
324*4882a593Smuzhiyun ops.len = min(len, (size_t)mtd->writesize);
325*4882a593Smuzhiyun ops.oobbuf = buf + ops.len;
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun res = mtd_read_oob(mtd, offs, &ops);
328*4882a593Smuzhiyun if (res) {
329*4882a593Smuzhiyun if (!mtd_is_bitflip_or_eccerr(res))
330*4882a593Smuzhiyun return res;
331*4882a593Smuzhiyun else if (mtd_is_eccerr(res) || !ret)
332*4882a593Smuzhiyun ret = res;
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun buf += mtd->oobsize + mtd->writesize;
336*4882a593Smuzhiyun len -= mtd->writesize;
337*4882a593Smuzhiyun offs += mtd->writesize;
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun return ret;
340*4882a593Smuzhiyun }
341*4882a593Smuzhiyun
scan_read(struct mtd_info * mtd,uint8_t * buf,loff_t offs,size_t len,struct nand_bbt_descr * td)342*4882a593Smuzhiyun static int scan_read(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
343*4882a593Smuzhiyun size_t len, struct nand_bbt_descr *td)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun if (td->options & NAND_BBT_NO_OOB)
346*4882a593Smuzhiyun return scan_read_data(mtd, buf, offs, td);
347*4882a593Smuzhiyun else
348*4882a593Smuzhiyun return scan_read_oob(mtd, buf, offs, len);
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun /* Scan write data with oob to flash */
scan_write_bbt(struct mtd_info * mtd,loff_t offs,size_t len,uint8_t * buf,uint8_t * oob)352*4882a593Smuzhiyun static int scan_write_bbt(struct mtd_info *mtd, loff_t offs, size_t len,
353*4882a593Smuzhiyun uint8_t *buf, uint8_t *oob)
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun struct mtd_oob_ops ops;
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun ops.mode = MTD_OPS_PLACE_OOB;
358*4882a593Smuzhiyun ops.ooboffs = 0;
359*4882a593Smuzhiyun ops.ooblen = mtd->oobsize;
360*4882a593Smuzhiyun ops.datbuf = buf;
361*4882a593Smuzhiyun ops.oobbuf = oob;
362*4882a593Smuzhiyun ops.len = len;
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun return mtd_write_oob(mtd, offs, &ops);
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun
bbt_get_ver_offs(struct mtd_info * mtd,struct nand_bbt_descr * td)367*4882a593Smuzhiyun static u32 bbt_get_ver_offs(struct mtd_info *mtd, struct nand_bbt_descr *td)
368*4882a593Smuzhiyun {
369*4882a593Smuzhiyun u32 ver_offs = td->veroffs;
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun if (!(td->options & NAND_BBT_NO_OOB))
372*4882a593Smuzhiyun ver_offs += mtd->writesize;
373*4882a593Smuzhiyun return ver_offs;
374*4882a593Smuzhiyun }
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun /**
377*4882a593Smuzhiyun * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
378*4882a593Smuzhiyun * @mtd: MTD device structure
379*4882a593Smuzhiyun * @buf: temporary buffer
380*4882a593Smuzhiyun * @td: descriptor for the bad block table
381*4882a593Smuzhiyun * @md: descriptor for the bad block table mirror
382*4882a593Smuzhiyun *
383*4882a593Smuzhiyun * Read the bad block table(s) for all chips starting at a given page. We
384*4882a593Smuzhiyun * assume that the bbt bits are in consecutive order.
385*4882a593Smuzhiyun */
read_abs_bbts(struct mtd_info * mtd,uint8_t * buf,struct nand_bbt_descr * td,struct nand_bbt_descr * md)386*4882a593Smuzhiyun static void read_abs_bbts(struct mtd_info *mtd, uint8_t *buf,
387*4882a593Smuzhiyun struct nand_bbt_descr *td, struct nand_bbt_descr *md)
388*4882a593Smuzhiyun {
389*4882a593Smuzhiyun struct nand_chip *this = mtd_to_nand(mtd);
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun /* Read the primary version, if available */
392*4882a593Smuzhiyun if (td->options & NAND_BBT_VERSION) {
393*4882a593Smuzhiyun scan_read(mtd, buf, (loff_t)td->pages[0] << this->page_shift,
394*4882a593Smuzhiyun mtd->writesize, td);
395*4882a593Smuzhiyun td->version[0] = buf[bbt_get_ver_offs(mtd, td)];
396*4882a593Smuzhiyun pr_info("Bad block table at page %d, version 0x%02X\n",
397*4882a593Smuzhiyun td->pages[0], td->version[0]);
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun /* Read the mirror version, if available */
401*4882a593Smuzhiyun if (md && (md->options & NAND_BBT_VERSION)) {
402*4882a593Smuzhiyun scan_read(mtd, buf, (loff_t)md->pages[0] << this->page_shift,
403*4882a593Smuzhiyun mtd->writesize, md);
404*4882a593Smuzhiyun md->version[0] = buf[bbt_get_ver_offs(mtd, md)];
405*4882a593Smuzhiyun pr_info("Bad block table at page %d, version 0x%02X\n",
406*4882a593Smuzhiyun md->pages[0], md->version[0]);
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun /* Scan a given block partially */
scan_block_fast(struct mtd_info * mtd,struct nand_bbt_descr * bd,loff_t offs,uint8_t * buf,int numpages)411*4882a593Smuzhiyun static int scan_block_fast(struct mtd_info *mtd, struct nand_bbt_descr *bd,
412*4882a593Smuzhiyun loff_t offs, uint8_t *buf, int numpages)
413*4882a593Smuzhiyun {
414*4882a593Smuzhiyun struct mtd_oob_ops ops;
415*4882a593Smuzhiyun int j, ret;
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun ops.ooblen = mtd->oobsize;
418*4882a593Smuzhiyun ops.oobbuf = buf;
419*4882a593Smuzhiyun ops.ooboffs = 0;
420*4882a593Smuzhiyun ops.datbuf = NULL;
421*4882a593Smuzhiyun ops.mode = MTD_OPS_PLACE_OOB;
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun for (j = 0; j < numpages; j++) {
424*4882a593Smuzhiyun /*
425*4882a593Smuzhiyun * Read the full oob until read_oob is fixed to handle single
426*4882a593Smuzhiyun * byte reads for 16 bit buswidth.
427*4882a593Smuzhiyun */
428*4882a593Smuzhiyun ret = mtd_read_oob(mtd, offs, &ops);
429*4882a593Smuzhiyun /* Ignore ECC errors when checking for BBM */
430*4882a593Smuzhiyun if (ret && !mtd_is_bitflip_or_eccerr(ret))
431*4882a593Smuzhiyun return ret;
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun if (check_short_pattern(buf, bd))
434*4882a593Smuzhiyun return 1;
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun offs += mtd->writesize;
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun return 0;
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun /**
442*4882a593Smuzhiyun * create_bbt - [GENERIC] Create a bad block table by scanning the device
443*4882a593Smuzhiyun * @mtd: MTD device structure
444*4882a593Smuzhiyun * @buf: temporary buffer
445*4882a593Smuzhiyun * @bd: descriptor for the good/bad block search pattern
446*4882a593Smuzhiyun * @chip: create the table for a specific chip, -1 read all chips; applies only
447*4882a593Smuzhiyun * if NAND_BBT_PERCHIP option is set
448*4882a593Smuzhiyun *
449*4882a593Smuzhiyun * Create a bad block table by scanning the device for the given good/bad block
450*4882a593Smuzhiyun * identify pattern.
451*4882a593Smuzhiyun */
create_bbt(struct mtd_info * mtd,uint8_t * buf,struct nand_bbt_descr * bd,int chip)452*4882a593Smuzhiyun static int create_bbt(struct mtd_info *mtd, uint8_t *buf,
453*4882a593Smuzhiyun struct nand_bbt_descr *bd, int chip)
454*4882a593Smuzhiyun {
455*4882a593Smuzhiyun struct nand_chip *this = mtd_to_nand(mtd);
456*4882a593Smuzhiyun int i, numblocks, numpages;
457*4882a593Smuzhiyun int startblock;
458*4882a593Smuzhiyun loff_t from;
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun pr_info("Scanning device for bad blocks\n");
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun if (bd->options & NAND_BBT_SCAN2NDPAGE)
463*4882a593Smuzhiyun numpages = 2;
464*4882a593Smuzhiyun else
465*4882a593Smuzhiyun numpages = 1;
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun if (chip == -1) {
468*4882a593Smuzhiyun numblocks = mtd->size >> this->bbt_erase_shift;
469*4882a593Smuzhiyun startblock = 0;
470*4882a593Smuzhiyun from = 0;
471*4882a593Smuzhiyun } else {
472*4882a593Smuzhiyun if (chip >= this->numchips) {
473*4882a593Smuzhiyun pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
474*4882a593Smuzhiyun chip + 1, this->numchips);
475*4882a593Smuzhiyun return -EINVAL;
476*4882a593Smuzhiyun }
477*4882a593Smuzhiyun numblocks = this->chipsize >> this->bbt_erase_shift;
478*4882a593Smuzhiyun startblock = chip * numblocks;
479*4882a593Smuzhiyun numblocks += startblock;
480*4882a593Smuzhiyun from = (loff_t)startblock << this->bbt_erase_shift;
481*4882a593Smuzhiyun }
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun if (this->bbt_options & NAND_BBT_SCANLASTPAGE)
484*4882a593Smuzhiyun from += mtd->erasesize - (mtd->writesize * numpages);
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun for (i = startblock; i < numblocks; i++) {
487*4882a593Smuzhiyun int ret;
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun BUG_ON(bd->options & NAND_BBT_NO_OOB);
490*4882a593Smuzhiyun if (this->block_bad)
491*4882a593Smuzhiyun ret = this->block_bad(mtd, from);
492*4882a593Smuzhiyun else
493*4882a593Smuzhiyun ret = scan_block_fast(mtd, bd, from, buf, numpages);
494*4882a593Smuzhiyun if (ret < 0)
495*4882a593Smuzhiyun return ret;
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun if (ret) {
498*4882a593Smuzhiyun bbt_mark_entry(this, i, BBT_BLOCK_FACTORY_BAD);
499*4882a593Smuzhiyun pr_warn("Bad eraseblock %d at 0x%012llx\n",
500*4882a593Smuzhiyun i, (unsigned long long)from);
501*4882a593Smuzhiyun mtd->ecc_stats.badblocks++;
502*4882a593Smuzhiyun }
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun from += (1 << this->bbt_erase_shift);
505*4882a593Smuzhiyun }
506*4882a593Smuzhiyun return 0;
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun /**
510*4882a593Smuzhiyun * search_bbt - [GENERIC] scan the device for a specific bad block table
511*4882a593Smuzhiyun * @mtd: MTD device structure
512*4882a593Smuzhiyun * @buf: temporary buffer
513*4882a593Smuzhiyun * @td: descriptor for the bad block table
514*4882a593Smuzhiyun *
515*4882a593Smuzhiyun * Read the bad block table by searching for a given ident pattern. Search is
516*4882a593Smuzhiyun * preformed either from the beginning up or from the end of the device
517*4882a593Smuzhiyun * downwards. The search starts always at the start of a block. If the option
518*4882a593Smuzhiyun * NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains
519*4882a593Smuzhiyun * the bad block information of this chip. This is necessary to provide support
520*4882a593Smuzhiyun * for certain DOC devices.
521*4882a593Smuzhiyun *
522*4882a593Smuzhiyun * The bbt ident pattern resides in the oob area of the first page in a block.
523*4882a593Smuzhiyun */
search_bbt(struct mtd_info * mtd,uint8_t * buf,struct nand_bbt_descr * td)524*4882a593Smuzhiyun static int search_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *td)
525*4882a593Smuzhiyun {
526*4882a593Smuzhiyun struct nand_chip *this = mtd_to_nand(mtd);
527*4882a593Smuzhiyun int i, chips;
528*4882a593Smuzhiyun int startblock, block, dir;
529*4882a593Smuzhiyun int scanlen = mtd->writesize + mtd->oobsize;
530*4882a593Smuzhiyun int bbtblocks;
531*4882a593Smuzhiyun int blocktopage = this->bbt_erase_shift - this->page_shift;
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun /* Search direction top -> down? */
534*4882a593Smuzhiyun if (td->options & NAND_BBT_LASTBLOCK) {
535*4882a593Smuzhiyun startblock = (mtd->size >> this->bbt_erase_shift) - 1;
536*4882a593Smuzhiyun dir = -1;
537*4882a593Smuzhiyun } else {
538*4882a593Smuzhiyun startblock = 0;
539*4882a593Smuzhiyun dir = 1;
540*4882a593Smuzhiyun }
541*4882a593Smuzhiyun
542*4882a593Smuzhiyun /* Do we have a bbt per chip? */
543*4882a593Smuzhiyun if (td->options & NAND_BBT_PERCHIP) {
544*4882a593Smuzhiyun chips = this->numchips;
545*4882a593Smuzhiyun bbtblocks = this->chipsize >> this->bbt_erase_shift;
546*4882a593Smuzhiyun startblock &= bbtblocks - 1;
547*4882a593Smuzhiyun } else {
548*4882a593Smuzhiyun chips = 1;
549*4882a593Smuzhiyun bbtblocks = mtd->size >> this->bbt_erase_shift;
550*4882a593Smuzhiyun }
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun for (i = 0; i < chips; i++) {
553*4882a593Smuzhiyun /* Reset version information */
554*4882a593Smuzhiyun td->version[i] = 0;
555*4882a593Smuzhiyun td->pages[i] = -1;
556*4882a593Smuzhiyun /* Scan the maximum number of blocks */
557*4882a593Smuzhiyun for (block = 0; block < td->maxblocks; block++) {
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun int actblock = startblock + dir * block;
560*4882a593Smuzhiyun loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun /* Read first page */
563*4882a593Smuzhiyun scan_read(mtd, buf, offs, mtd->writesize, td);
564*4882a593Smuzhiyun if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
565*4882a593Smuzhiyun td->pages[i] = actblock << blocktopage;
566*4882a593Smuzhiyun if (td->options & NAND_BBT_VERSION) {
567*4882a593Smuzhiyun offs = bbt_get_ver_offs(mtd, td);
568*4882a593Smuzhiyun td->version[i] = buf[offs];
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun break;
571*4882a593Smuzhiyun }
572*4882a593Smuzhiyun }
573*4882a593Smuzhiyun startblock += this->chipsize >> this->bbt_erase_shift;
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun /* Check, if we found a bbt for each requested chip */
576*4882a593Smuzhiyun for (i = 0; i < chips; i++) {
577*4882a593Smuzhiyun if (td->pages[i] == -1)
578*4882a593Smuzhiyun pr_warn("Bad block table not found for chip %d\n", i);
579*4882a593Smuzhiyun else
580*4882a593Smuzhiyun pr_info("Bad block table found at page %d, version 0x%02X\n",
581*4882a593Smuzhiyun td->pages[i], td->version[i]);
582*4882a593Smuzhiyun }
583*4882a593Smuzhiyun return 0;
584*4882a593Smuzhiyun }
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun /**
587*4882a593Smuzhiyun * search_read_bbts - [GENERIC] scan the device for bad block table(s)
588*4882a593Smuzhiyun * @mtd: MTD device structure
589*4882a593Smuzhiyun * @buf: temporary buffer
590*4882a593Smuzhiyun * @td: descriptor for the bad block table
591*4882a593Smuzhiyun * @md: descriptor for the bad block table mirror
592*4882a593Smuzhiyun *
593*4882a593Smuzhiyun * Search and read the bad block table(s).
594*4882a593Smuzhiyun */
search_read_bbts(struct mtd_info * mtd,uint8_t * buf,struct nand_bbt_descr * td,struct nand_bbt_descr * md)595*4882a593Smuzhiyun static void search_read_bbts(struct mtd_info *mtd, uint8_t *buf,
596*4882a593Smuzhiyun struct nand_bbt_descr *td,
597*4882a593Smuzhiyun struct nand_bbt_descr *md)
598*4882a593Smuzhiyun {
599*4882a593Smuzhiyun /* Search the primary table */
600*4882a593Smuzhiyun search_bbt(mtd, buf, td);
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun /* Search the mirror table */
603*4882a593Smuzhiyun if (md)
604*4882a593Smuzhiyun search_bbt(mtd, buf, md);
605*4882a593Smuzhiyun }
606*4882a593Smuzhiyun
607*4882a593Smuzhiyun /**
608*4882a593Smuzhiyun * write_bbt - [GENERIC] (Re)write the bad block table
609*4882a593Smuzhiyun * @mtd: MTD device structure
610*4882a593Smuzhiyun * @buf: temporary buffer
611*4882a593Smuzhiyun * @td: descriptor for the bad block table
612*4882a593Smuzhiyun * @md: descriptor for the bad block table mirror
613*4882a593Smuzhiyun * @chipsel: selector for a specific chip, -1 for all
614*4882a593Smuzhiyun *
615*4882a593Smuzhiyun * (Re)write the bad block table.
616*4882a593Smuzhiyun */
write_bbt(struct mtd_info * mtd,uint8_t * buf,struct nand_bbt_descr * td,struct nand_bbt_descr * md,int chipsel)617*4882a593Smuzhiyun static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
618*4882a593Smuzhiyun struct nand_bbt_descr *td, struct nand_bbt_descr *md,
619*4882a593Smuzhiyun int chipsel)
620*4882a593Smuzhiyun {
621*4882a593Smuzhiyun struct nand_chip *this = mtd_to_nand(mtd);
622*4882a593Smuzhiyun struct erase_info einfo;
623*4882a593Smuzhiyun int i, res, chip = 0;
624*4882a593Smuzhiyun int bits, startblock, dir, page, offs, numblocks, sft, sftmsk;
625*4882a593Smuzhiyun int nrchips, pageoffs, ooboffs;
626*4882a593Smuzhiyun uint8_t msk[4];
627*4882a593Smuzhiyun uint8_t rcode = td->reserved_block_code;
628*4882a593Smuzhiyun size_t retlen, len = 0;
629*4882a593Smuzhiyun loff_t to;
630*4882a593Smuzhiyun struct mtd_oob_ops ops;
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun ops.ooblen = mtd->oobsize;
633*4882a593Smuzhiyun ops.ooboffs = 0;
634*4882a593Smuzhiyun ops.datbuf = NULL;
635*4882a593Smuzhiyun ops.mode = MTD_OPS_PLACE_OOB;
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun if (!rcode)
638*4882a593Smuzhiyun rcode = 0xff;
639*4882a593Smuzhiyun /* Write bad block table per chip rather than per device? */
640*4882a593Smuzhiyun if (td->options & NAND_BBT_PERCHIP) {
641*4882a593Smuzhiyun numblocks = (int)(this->chipsize >> this->bbt_erase_shift);
642*4882a593Smuzhiyun /* Full device write or specific chip? */
643*4882a593Smuzhiyun if (chipsel == -1) {
644*4882a593Smuzhiyun nrchips = this->numchips;
645*4882a593Smuzhiyun } else {
646*4882a593Smuzhiyun nrchips = chipsel + 1;
647*4882a593Smuzhiyun chip = chipsel;
648*4882a593Smuzhiyun }
649*4882a593Smuzhiyun } else {
650*4882a593Smuzhiyun numblocks = (int)(mtd->size >> this->bbt_erase_shift);
651*4882a593Smuzhiyun nrchips = 1;
652*4882a593Smuzhiyun }
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun /* Loop through the chips */
655*4882a593Smuzhiyun for (; chip < nrchips; chip++) {
656*4882a593Smuzhiyun /*
657*4882a593Smuzhiyun * There was already a version of the table, reuse the page
658*4882a593Smuzhiyun * This applies for absolute placement too, as we have the
659*4882a593Smuzhiyun * page nr. in td->pages.
660*4882a593Smuzhiyun */
661*4882a593Smuzhiyun if (td->pages[chip] != -1) {
662*4882a593Smuzhiyun page = td->pages[chip];
663*4882a593Smuzhiyun goto write;
664*4882a593Smuzhiyun }
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun /*
667*4882a593Smuzhiyun * Automatic placement of the bad block table. Search direction
668*4882a593Smuzhiyun * top -> down?
669*4882a593Smuzhiyun */
670*4882a593Smuzhiyun if (td->options & NAND_BBT_LASTBLOCK) {
671*4882a593Smuzhiyun startblock = numblocks * (chip + 1) - 1;
672*4882a593Smuzhiyun dir = -1;
673*4882a593Smuzhiyun } else {
674*4882a593Smuzhiyun startblock = chip * numblocks;
675*4882a593Smuzhiyun dir = 1;
676*4882a593Smuzhiyun }
677*4882a593Smuzhiyun
678*4882a593Smuzhiyun for (i = 0; i < td->maxblocks; i++) {
679*4882a593Smuzhiyun int block = startblock + dir * i;
680*4882a593Smuzhiyun /* Check, if the block is bad */
681*4882a593Smuzhiyun switch (bbt_get_entry(this, block)) {
682*4882a593Smuzhiyun case BBT_BLOCK_WORN:
683*4882a593Smuzhiyun case BBT_BLOCK_FACTORY_BAD:
684*4882a593Smuzhiyun continue;
685*4882a593Smuzhiyun }
686*4882a593Smuzhiyun page = block <<
687*4882a593Smuzhiyun (this->bbt_erase_shift - this->page_shift);
688*4882a593Smuzhiyun /* Check, if the block is used by the mirror table */
689*4882a593Smuzhiyun if (!md || md->pages[chip] != page)
690*4882a593Smuzhiyun goto write;
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun pr_err("No space left to write bad block table\n");
693*4882a593Smuzhiyun return -ENOSPC;
694*4882a593Smuzhiyun write:
695*4882a593Smuzhiyun
696*4882a593Smuzhiyun /* Set up shift count and masks for the flash table */
697*4882a593Smuzhiyun bits = td->options & NAND_BBT_NRBITS_MSK;
698*4882a593Smuzhiyun msk[2] = ~rcode;
699*4882a593Smuzhiyun switch (bits) {
700*4882a593Smuzhiyun case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
701*4882a593Smuzhiyun msk[3] = 0x01;
702*4882a593Smuzhiyun break;
703*4882a593Smuzhiyun case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
704*4882a593Smuzhiyun msk[3] = 0x03;
705*4882a593Smuzhiyun break;
706*4882a593Smuzhiyun case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
707*4882a593Smuzhiyun msk[3] = 0x0f;
708*4882a593Smuzhiyun break;
709*4882a593Smuzhiyun case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
710*4882a593Smuzhiyun msk[3] = 0xff;
711*4882a593Smuzhiyun break;
712*4882a593Smuzhiyun default: return -EINVAL;
713*4882a593Smuzhiyun }
714*4882a593Smuzhiyun
715*4882a593Smuzhiyun to = ((loff_t)page) << this->page_shift;
716*4882a593Smuzhiyun
717*4882a593Smuzhiyun /* Must we save the block contents? */
718*4882a593Smuzhiyun if (td->options & NAND_BBT_SAVECONTENT) {
719*4882a593Smuzhiyun /* Make it block aligned */
720*4882a593Smuzhiyun to &= ~(((loff_t)1 << this->bbt_erase_shift) - 1);
721*4882a593Smuzhiyun len = 1 << this->bbt_erase_shift;
722*4882a593Smuzhiyun res = mtd_read(mtd, to, len, &retlen, buf);
723*4882a593Smuzhiyun if (res < 0) {
724*4882a593Smuzhiyun if (retlen != len) {
725*4882a593Smuzhiyun pr_info("nand_bbt: error reading block for writing the bad block table\n");
726*4882a593Smuzhiyun return res;
727*4882a593Smuzhiyun }
728*4882a593Smuzhiyun pr_warn("nand_bbt: ECC error while reading block for writing bad block table\n");
729*4882a593Smuzhiyun }
730*4882a593Smuzhiyun /* Read oob data */
731*4882a593Smuzhiyun ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
732*4882a593Smuzhiyun ops.oobbuf = &buf[len];
733*4882a593Smuzhiyun res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
734*4882a593Smuzhiyun if (res < 0 || ops.oobretlen != ops.ooblen)
735*4882a593Smuzhiyun goto outerr;
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun /* Calc the byte offset in the buffer */
738*4882a593Smuzhiyun pageoffs = page - (int)(to >> this->page_shift);
739*4882a593Smuzhiyun offs = pageoffs << this->page_shift;
740*4882a593Smuzhiyun /* Preset the bbt area with 0xff */
741*4882a593Smuzhiyun memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
742*4882a593Smuzhiyun ooboffs = len + (pageoffs * mtd->oobsize);
743*4882a593Smuzhiyun
744*4882a593Smuzhiyun } else if (td->options & NAND_BBT_NO_OOB) {
745*4882a593Smuzhiyun ooboffs = 0;
746*4882a593Smuzhiyun offs = td->len;
747*4882a593Smuzhiyun /* The version byte */
748*4882a593Smuzhiyun if (td->options & NAND_BBT_VERSION)
749*4882a593Smuzhiyun offs++;
750*4882a593Smuzhiyun /* Calc length */
751*4882a593Smuzhiyun len = (size_t)(numblocks >> sft);
752*4882a593Smuzhiyun len += offs;
753*4882a593Smuzhiyun /* Make it page aligned! */
754*4882a593Smuzhiyun len = ALIGN(len, mtd->writesize);
755*4882a593Smuzhiyun /* Preset the buffer with 0xff */
756*4882a593Smuzhiyun memset(buf, 0xff, len);
757*4882a593Smuzhiyun /* Pattern is located at the begin of first page */
758*4882a593Smuzhiyun memcpy(buf, td->pattern, td->len);
759*4882a593Smuzhiyun } else {
760*4882a593Smuzhiyun /* Calc length */
761*4882a593Smuzhiyun len = (size_t)(numblocks >> sft);
762*4882a593Smuzhiyun /* Make it page aligned! */
763*4882a593Smuzhiyun len = ALIGN(len, mtd->writesize);
764*4882a593Smuzhiyun /* Preset the buffer with 0xff */
765*4882a593Smuzhiyun memset(buf, 0xff, len +
766*4882a593Smuzhiyun (len >> this->page_shift)* mtd->oobsize);
767*4882a593Smuzhiyun offs = 0;
768*4882a593Smuzhiyun ooboffs = len;
769*4882a593Smuzhiyun /* Pattern is located in oob area of first page */
770*4882a593Smuzhiyun memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
771*4882a593Smuzhiyun }
772*4882a593Smuzhiyun
773*4882a593Smuzhiyun if (td->options & NAND_BBT_VERSION)
774*4882a593Smuzhiyun buf[ooboffs + td->veroffs] = td->version[chip];
775*4882a593Smuzhiyun
776*4882a593Smuzhiyun /* Walk through the memory table */
777*4882a593Smuzhiyun for (i = 0; i < numblocks; i++) {
778*4882a593Smuzhiyun uint8_t dat;
779*4882a593Smuzhiyun int sftcnt = (i << (3 - sft)) & sftmsk;
780*4882a593Smuzhiyun dat = bbt_get_entry(this, chip * numblocks + i);
781*4882a593Smuzhiyun /* Do not store the reserved bbt blocks! */
782*4882a593Smuzhiyun buf[offs + (i >> sft)] &= ~(msk[dat] << sftcnt);
783*4882a593Smuzhiyun }
784*4882a593Smuzhiyun
785*4882a593Smuzhiyun memset(&einfo, 0, sizeof(einfo));
786*4882a593Smuzhiyun einfo.mtd = mtd;
787*4882a593Smuzhiyun einfo.addr = to;
788*4882a593Smuzhiyun einfo.len = 1 << this->bbt_erase_shift;
789*4882a593Smuzhiyun res = nand_erase_nand(mtd, &einfo, 1);
790*4882a593Smuzhiyun if (res < 0)
791*4882a593Smuzhiyun goto outerr;
792*4882a593Smuzhiyun
793*4882a593Smuzhiyun res = scan_write_bbt(mtd, to, len, buf,
794*4882a593Smuzhiyun td->options & NAND_BBT_NO_OOB ? NULL :
795*4882a593Smuzhiyun &buf[len]);
796*4882a593Smuzhiyun if (res < 0)
797*4882a593Smuzhiyun goto outerr;
798*4882a593Smuzhiyun
799*4882a593Smuzhiyun pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
800*4882a593Smuzhiyun (unsigned long long)to, td->version[chip]);
801*4882a593Smuzhiyun
802*4882a593Smuzhiyun /* Mark it as used */
803*4882a593Smuzhiyun td->pages[chip] = page;
804*4882a593Smuzhiyun }
805*4882a593Smuzhiyun return 0;
806*4882a593Smuzhiyun
807*4882a593Smuzhiyun outerr:
808*4882a593Smuzhiyun pr_warn("nand_bbt: error while writing bad block table %d\n", res);
809*4882a593Smuzhiyun return res;
810*4882a593Smuzhiyun }
811*4882a593Smuzhiyun
812*4882a593Smuzhiyun /**
813*4882a593Smuzhiyun * nand_memory_bbt - [GENERIC] create a memory based bad block table
814*4882a593Smuzhiyun * @mtd: MTD device structure
815*4882a593Smuzhiyun * @bd: descriptor for the good/bad block search pattern
816*4882a593Smuzhiyun *
817*4882a593Smuzhiyun * The function creates a memory based bbt by scanning the device for
818*4882a593Smuzhiyun * manufacturer / software marked good / bad blocks.
819*4882a593Smuzhiyun */
nand_memory_bbt(struct mtd_info * mtd,struct nand_bbt_descr * bd)820*4882a593Smuzhiyun static inline int nand_memory_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
821*4882a593Smuzhiyun {
822*4882a593Smuzhiyun struct nand_chip *this = mtd_to_nand(mtd);
823*4882a593Smuzhiyun
824*4882a593Smuzhiyun return create_bbt(mtd, this->buffers->databuf, bd, -1);
825*4882a593Smuzhiyun }
826*4882a593Smuzhiyun
827*4882a593Smuzhiyun /**
828*4882a593Smuzhiyun * check_create - [GENERIC] create and write bbt(s) if necessary
829*4882a593Smuzhiyun * @mtd: MTD device structure
830*4882a593Smuzhiyun * @buf: temporary buffer
831*4882a593Smuzhiyun * @bd: descriptor for the good/bad block search pattern
832*4882a593Smuzhiyun *
833*4882a593Smuzhiyun * The function checks the results of the previous call to read_bbt and creates
834*4882a593Smuzhiyun * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found
835*4882a593Smuzhiyun * for the chip/device. Update is necessary if one of the tables is missing or
836*4882a593Smuzhiyun * the version nr. of one table is less than the other.
837*4882a593Smuzhiyun */
check_create(struct mtd_info * mtd,uint8_t * buf,struct nand_bbt_descr * bd)838*4882a593Smuzhiyun static int check_create(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd)
839*4882a593Smuzhiyun {
840*4882a593Smuzhiyun int i, chips, writeops, create, chipsel, res, res2;
841*4882a593Smuzhiyun struct nand_chip *this = mtd_to_nand(mtd);
842*4882a593Smuzhiyun struct nand_bbt_descr *td = this->bbt_td;
843*4882a593Smuzhiyun struct nand_bbt_descr *md = this->bbt_md;
844*4882a593Smuzhiyun struct nand_bbt_descr *rd, *rd2;
845*4882a593Smuzhiyun
846*4882a593Smuzhiyun /* Do we have a bbt per chip? */
847*4882a593Smuzhiyun if (td->options & NAND_BBT_PERCHIP)
848*4882a593Smuzhiyun chips = this->numchips;
849*4882a593Smuzhiyun else
850*4882a593Smuzhiyun chips = 1;
851*4882a593Smuzhiyun
852*4882a593Smuzhiyun for (i = 0; i < chips; i++) {
853*4882a593Smuzhiyun writeops = 0;
854*4882a593Smuzhiyun create = 0;
855*4882a593Smuzhiyun rd = NULL;
856*4882a593Smuzhiyun rd2 = NULL;
857*4882a593Smuzhiyun res = res2 = 0;
858*4882a593Smuzhiyun /* Per chip or per device? */
859*4882a593Smuzhiyun chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
860*4882a593Smuzhiyun /* Mirrored table available? */
861*4882a593Smuzhiyun if (md) {
862*4882a593Smuzhiyun if (td->pages[i] == -1 && md->pages[i] == -1) {
863*4882a593Smuzhiyun create = 1;
864*4882a593Smuzhiyun writeops = 0x03;
865*4882a593Smuzhiyun } else if (td->pages[i] == -1) {
866*4882a593Smuzhiyun rd = md;
867*4882a593Smuzhiyun writeops = 0x01;
868*4882a593Smuzhiyun } else if (md->pages[i] == -1) {
869*4882a593Smuzhiyun rd = td;
870*4882a593Smuzhiyun writeops = 0x02;
871*4882a593Smuzhiyun } else if (td->version[i] == md->version[i]) {
872*4882a593Smuzhiyun rd = td;
873*4882a593Smuzhiyun if (!(td->options & NAND_BBT_VERSION))
874*4882a593Smuzhiyun rd2 = md;
875*4882a593Smuzhiyun } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
876*4882a593Smuzhiyun rd = td;
877*4882a593Smuzhiyun writeops = 0x02;
878*4882a593Smuzhiyun } else {
879*4882a593Smuzhiyun rd = md;
880*4882a593Smuzhiyun writeops = 0x01;
881*4882a593Smuzhiyun }
882*4882a593Smuzhiyun } else {
883*4882a593Smuzhiyun if (td->pages[i] == -1) {
884*4882a593Smuzhiyun create = 1;
885*4882a593Smuzhiyun writeops = 0x01;
886*4882a593Smuzhiyun } else {
887*4882a593Smuzhiyun rd = td;
888*4882a593Smuzhiyun }
889*4882a593Smuzhiyun }
890*4882a593Smuzhiyun
891*4882a593Smuzhiyun if (create) {
892*4882a593Smuzhiyun /* Create the bad block table by scanning the device? */
893*4882a593Smuzhiyun if (!(td->options & NAND_BBT_CREATE))
894*4882a593Smuzhiyun continue;
895*4882a593Smuzhiyun
896*4882a593Smuzhiyun /* Create the table in memory by scanning the chip(s) */
897*4882a593Smuzhiyun if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
898*4882a593Smuzhiyun create_bbt(mtd, buf, bd, chipsel);
899*4882a593Smuzhiyun
900*4882a593Smuzhiyun td->version[i] = 1;
901*4882a593Smuzhiyun if (md)
902*4882a593Smuzhiyun md->version[i] = 1;
903*4882a593Smuzhiyun }
904*4882a593Smuzhiyun
905*4882a593Smuzhiyun /* Read back first? */
906*4882a593Smuzhiyun if (rd) {
907*4882a593Smuzhiyun res = read_abs_bbt(mtd, buf, rd, chipsel);
908*4882a593Smuzhiyun if (mtd_is_eccerr(res)) {
909*4882a593Smuzhiyun /* Mark table as invalid */
910*4882a593Smuzhiyun rd->pages[i] = -1;
911*4882a593Smuzhiyun rd->version[i] = 0;
912*4882a593Smuzhiyun i--;
913*4882a593Smuzhiyun continue;
914*4882a593Smuzhiyun }
915*4882a593Smuzhiyun }
916*4882a593Smuzhiyun /* If they weren't versioned, read both */
917*4882a593Smuzhiyun if (rd2) {
918*4882a593Smuzhiyun res2 = read_abs_bbt(mtd, buf, rd2, chipsel);
919*4882a593Smuzhiyun if (mtd_is_eccerr(res2)) {
920*4882a593Smuzhiyun /* Mark table as invalid */
921*4882a593Smuzhiyun rd2->pages[i] = -1;
922*4882a593Smuzhiyun rd2->version[i] = 0;
923*4882a593Smuzhiyun i--;
924*4882a593Smuzhiyun continue;
925*4882a593Smuzhiyun }
926*4882a593Smuzhiyun }
927*4882a593Smuzhiyun
928*4882a593Smuzhiyun /* Scrub the flash table(s)? */
929*4882a593Smuzhiyun if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
930*4882a593Smuzhiyun writeops = 0x03;
931*4882a593Smuzhiyun
932*4882a593Smuzhiyun /* Update version numbers before writing */
933*4882a593Smuzhiyun if (md) {
934*4882a593Smuzhiyun td->version[i] = max(td->version[i], md->version[i]);
935*4882a593Smuzhiyun md->version[i] = td->version[i];
936*4882a593Smuzhiyun }
937*4882a593Smuzhiyun
938*4882a593Smuzhiyun /* Write the bad block table to the device? */
939*4882a593Smuzhiyun if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
940*4882a593Smuzhiyun res = write_bbt(mtd, buf, td, md, chipsel);
941*4882a593Smuzhiyun if (res < 0)
942*4882a593Smuzhiyun return res;
943*4882a593Smuzhiyun }
944*4882a593Smuzhiyun
945*4882a593Smuzhiyun /* Write the mirror bad block table to the device? */
946*4882a593Smuzhiyun if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
947*4882a593Smuzhiyun res = write_bbt(mtd, buf, md, td, chipsel);
948*4882a593Smuzhiyun if (res < 0)
949*4882a593Smuzhiyun return res;
950*4882a593Smuzhiyun }
951*4882a593Smuzhiyun }
952*4882a593Smuzhiyun return 0;
953*4882a593Smuzhiyun }
954*4882a593Smuzhiyun
955*4882a593Smuzhiyun /**
956*4882a593Smuzhiyun * mark_bbt_regions - [GENERIC] mark the bad block table regions
957*4882a593Smuzhiyun * @mtd: MTD device structure
958*4882a593Smuzhiyun * @td: bad block table descriptor
959*4882a593Smuzhiyun *
960*4882a593Smuzhiyun * The bad block table regions are marked as "bad" to prevent accidental
961*4882a593Smuzhiyun * erasures / writes. The regions are identified by the mark 0x02.
962*4882a593Smuzhiyun */
mark_bbt_region(struct mtd_info * mtd,struct nand_bbt_descr * td)963*4882a593Smuzhiyun static void mark_bbt_region(struct mtd_info *mtd, struct nand_bbt_descr *td)
964*4882a593Smuzhiyun {
965*4882a593Smuzhiyun struct nand_chip *this = mtd_to_nand(mtd);
966*4882a593Smuzhiyun int i, j, chips, block, nrblocks, update;
967*4882a593Smuzhiyun uint8_t oldval;
968*4882a593Smuzhiyun
969*4882a593Smuzhiyun /* Do we have a bbt per chip? */
970*4882a593Smuzhiyun if (td->options & NAND_BBT_PERCHIP) {
971*4882a593Smuzhiyun chips = this->numchips;
972*4882a593Smuzhiyun nrblocks = (int)(this->chipsize >> this->bbt_erase_shift);
973*4882a593Smuzhiyun } else {
974*4882a593Smuzhiyun chips = 1;
975*4882a593Smuzhiyun nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
976*4882a593Smuzhiyun }
977*4882a593Smuzhiyun
978*4882a593Smuzhiyun for (i = 0; i < chips; i++) {
979*4882a593Smuzhiyun if ((td->options & NAND_BBT_ABSPAGE) ||
980*4882a593Smuzhiyun !(td->options & NAND_BBT_WRITE)) {
981*4882a593Smuzhiyun if (td->pages[i] == -1)
982*4882a593Smuzhiyun continue;
983*4882a593Smuzhiyun block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
984*4882a593Smuzhiyun oldval = bbt_get_entry(this, block);
985*4882a593Smuzhiyun bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
986*4882a593Smuzhiyun if ((oldval != BBT_BLOCK_RESERVED) &&
987*4882a593Smuzhiyun td->reserved_block_code)
988*4882a593Smuzhiyun nand_update_bbt(mtd, (loff_t)block <<
989*4882a593Smuzhiyun this->bbt_erase_shift);
990*4882a593Smuzhiyun continue;
991*4882a593Smuzhiyun }
992*4882a593Smuzhiyun update = 0;
993*4882a593Smuzhiyun if (td->options & NAND_BBT_LASTBLOCK)
994*4882a593Smuzhiyun block = ((i + 1) * nrblocks) - td->maxblocks;
995*4882a593Smuzhiyun else
996*4882a593Smuzhiyun block = i * nrblocks;
997*4882a593Smuzhiyun for (j = 0; j < td->maxblocks; j++) {
998*4882a593Smuzhiyun oldval = bbt_get_entry(this, block);
999*4882a593Smuzhiyun bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
1000*4882a593Smuzhiyun if (oldval != BBT_BLOCK_RESERVED)
1001*4882a593Smuzhiyun update = 1;
1002*4882a593Smuzhiyun block++;
1003*4882a593Smuzhiyun }
1004*4882a593Smuzhiyun /*
1005*4882a593Smuzhiyun * If we want reserved blocks to be recorded to flash, and some
1006*4882a593Smuzhiyun * new ones have been marked, then we need to update the stored
1007*4882a593Smuzhiyun * bbts. This should only happen once.
1008*4882a593Smuzhiyun */
1009*4882a593Smuzhiyun if (update && td->reserved_block_code)
1010*4882a593Smuzhiyun nand_update_bbt(mtd, (loff_t)(block - 1) <<
1011*4882a593Smuzhiyun this->bbt_erase_shift);
1012*4882a593Smuzhiyun }
1013*4882a593Smuzhiyun }
1014*4882a593Smuzhiyun
1015*4882a593Smuzhiyun /**
1016*4882a593Smuzhiyun * verify_bbt_descr - verify the bad block description
1017*4882a593Smuzhiyun * @mtd: MTD device structure
1018*4882a593Smuzhiyun * @bd: the table to verify
1019*4882a593Smuzhiyun *
1020*4882a593Smuzhiyun * This functions performs a few sanity checks on the bad block description
1021*4882a593Smuzhiyun * table.
1022*4882a593Smuzhiyun */
verify_bbt_descr(struct mtd_info * mtd,struct nand_bbt_descr * bd)1023*4882a593Smuzhiyun static void verify_bbt_descr(struct mtd_info *mtd, struct nand_bbt_descr *bd)
1024*4882a593Smuzhiyun {
1025*4882a593Smuzhiyun struct nand_chip *this = mtd_to_nand(mtd);
1026*4882a593Smuzhiyun u32 pattern_len;
1027*4882a593Smuzhiyun u32 bits;
1028*4882a593Smuzhiyun u32 table_size;
1029*4882a593Smuzhiyun
1030*4882a593Smuzhiyun if (!bd)
1031*4882a593Smuzhiyun return;
1032*4882a593Smuzhiyun
1033*4882a593Smuzhiyun pattern_len = bd->len;
1034*4882a593Smuzhiyun bits = bd->options & NAND_BBT_NRBITS_MSK;
1035*4882a593Smuzhiyun
1036*4882a593Smuzhiyun BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
1037*4882a593Smuzhiyun !(this->bbt_options & NAND_BBT_USE_FLASH));
1038*4882a593Smuzhiyun BUG_ON(!bits);
1039*4882a593Smuzhiyun
1040*4882a593Smuzhiyun if (bd->options & NAND_BBT_VERSION)
1041*4882a593Smuzhiyun pattern_len++;
1042*4882a593Smuzhiyun
1043*4882a593Smuzhiyun if (bd->options & NAND_BBT_NO_OOB) {
1044*4882a593Smuzhiyun BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
1045*4882a593Smuzhiyun BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
1046*4882a593Smuzhiyun BUG_ON(bd->offs);
1047*4882a593Smuzhiyun if (bd->options & NAND_BBT_VERSION)
1048*4882a593Smuzhiyun BUG_ON(bd->veroffs != bd->len);
1049*4882a593Smuzhiyun BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1050*4882a593Smuzhiyun }
1051*4882a593Smuzhiyun
1052*4882a593Smuzhiyun if (bd->options & NAND_BBT_PERCHIP)
1053*4882a593Smuzhiyun table_size = this->chipsize >> this->bbt_erase_shift;
1054*4882a593Smuzhiyun else
1055*4882a593Smuzhiyun table_size = mtd->size >> this->bbt_erase_shift;
1056*4882a593Smuzhiyun table_size >>= 3;
1057*4882a593Smuzhiyun table_size *= bits;
1058*4882a593Smuzhiyun if (bd->options & NAND_BBT_NO_OOB)
1059*4882a593Smuzhiyun table_size += pattern_len;
1060*4882a593Smuzhiyun BUG_ON(table_size > (1 << this->bbt_erase_shift));
1061*4882a593Smuzhiyun }
1062*4882a593Smuzhiyun
1063*4882a593Smuzhiyun /**
1064*4882a593Smuzhiyun * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
1065*4882a593Smuzhiyun * @mtd: MTD device structure
1066*4882a593Smuzhiyun * @bd: descriptor for the good/bad block search pattern
1067*4882a593Smuzhiyun *
1068*4882a593Smuzhiyun * The function checks, if a bad block table(s) is/are already available. If
1069*4882a593Smuzhiyun * not it scans the device for manufacturer marked good / bad blocks and writes
1070*4882a593Smuzhiyun * the bad block table(s) to the selected place.
1071*4882a593Smuzhiyun *
1072*4882a593Smuzhiyun * The bad block table memory is allocated here. It must be freed by calling
1073*4882a593Smuzhiyun * the nand_free_bbt function.
1074*4882a593Smuzhiyun */
nand_scan_bbt(struct mtd_info * mtd,struct nand_bbt_descr * bd)1075*4882a593Smuzhiyun static int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
1076*4882a593Smuzhiyun {
1077*4882a593Smuzhiyun struct nand_chip *this = mtd_to_nand(mtd);
1078*4882a593Smuzhiyun int len, res;
1079*4882a593Smuzhiyun uint8_t *buf;
1080*4882a593Smuzhiyun struct nand_bbt_descr *td = this->bbt_td;
1081*4882a593Smuzhiyun struct nand_bbt_descr *md = this->bbt_md;
1082*4882a593Smuzhiyun
1083*4882a593Smuzhiyun len = (mtd->size >> (this->bbt_erase_shift + 2)) ? : 1;
1084*4882a593Smuzhiyun /*
1085*4882a593Smuzhiyun * Allocate memory (2bit per block) and clear the memory bad block
1086*4882a593Smuzhiyun * table.
1087*4882a593Smuzhiyun */
1088*4882a593Smuzhiyun this->bbt = kzalloc(len, GFP_KERNEL);
1089*4882a593Smuzhiyun if (!this->bbt)
1090*4882a593Smuzhiyun return -ENOMEM;
1091*4882a593Smuzhiyun
1092*4882a593Smuzhiyun /*
1093*4882a593Smuzhiyun * If no primary table decriptor is given, scan the device to build a
1094*4882a593Smuzhiyun * memory based bad block table.
1095*4882a593Smuzhiyun */
1096*4882a593Smuzhiyun if (!td) {
1097*4882a593Smuzhiyun if ((res = nand_memory_bbt(mtd, bd))) {
1098*4882a593Smuzhiyun pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
1099*4882a593Smuzhiyun goto err;
1100*4882a593Smuzhiyun }
1101*4882a593Smuzhiyun return 0;
1102*4882a593Smuzhiyun }
1103*4882a593Smuzhiyun verify_bbt_descr(mtd, td);
1104*4882a593Smuzhiyun verify_bbt_descr(mtd, md);
1105*4882a593Smuzhiyun
1106*4882a593Smuzhiyun /* Allocate a temporary buffer for one eraseblock incl. oob */
1107*4882a593Smuzhiyun len = (1 << this->bbt_erase_shift);
1108*4882a593Smuzhiyun len += (len >> this->page_shift) * mtd->oobsize;
1109*4882a593Smuzhiyun buf = vmalloc(len);
1110*4882a593Smuzhiyun if (!buf) {
1111*4882a593Smuzhiyun res = -ENOMEM;
1112*4882a593Smuzhiyun goto err;
1113*4882a593Smuzhiyun }
1114*4882a593Smuzhiyun
1115*4882a593Smuzhiyun /* Is the bbt at a given page? */
1116*4882a593Smuzhiyun if (td->options & NAND_BBT_ABSPAGE) {
1117*4882a593Smuzhiyun read_abs_bbts(mtd, buf, td, md);
1118*4882a593Smuzhiyun } else {
1119*4882a593Smuzhiyun /* Search the bad block table using a pattern in oob */
1120*4882a593Smuzhiyun search_read_bbts(mtd, buf, td, md);
1121*4882a593Smuzhiyun }
1122*4882a593Smuzhiyun
1123*4882a593Smuzhiyun res = check_create(mtd, buf, bd);
1124*4882a593Smuzhiyun if (res)
1125*4882a593Smuzhiyun goto err;
1126*4882a593Smuzhiyun
1127*4882a593Smuzhiyun /* Prevent the bbt regions from erasing / writing */
1128*4882a593Smuzhiyun mark_bbt_region(mtd, td);
1129*4882a593Smuzhiyun if (md)
1130*4882a593Smuzhiyun mark_bbt_region(mtd, md);
1131*4882a593Smuzhiyun
1132*4882a593Smuzhiyun vfree(buf);
1133*4882a593Smuzhiyun return 0;
1134*4882a593Smuzhiyun
1135*4882a593Smuzhiyun err:
1136*4882a593Smuzhiyun kfree(this->bbt);
1137*4882a593Smuzhiyun this->bbt = NULL;
1138*4882a593Smuzhiyun return res;
1139*4882a593Smuzhiyun }
1140*4882a593Smuzhiyun
1141*4882a593Smuzhiyun /**
1142*4882a593Smuzhiyun * nand_update_bbt - update bad block table(s)
1143*4882a593Smuzhiyun * @mtd: MTD device structure
1144*4882a593Smuzhiyun * @offs: the offset of the newly marked block
1145*4882a593Smuzhiyun *
1146*4882a593Smuzhiyun * The function updates the bad block table(s).
1147*4882a593Smuzhiyun */
nand_update_bbt(struct mtd_info * mtd,loff_t offs)1148*4882a593Smuzhiyun static int nand_update_bbt(struct mtd_info *mtd, loff_t offs)
1149*4882a593Smuzhiyun {
1150*4882a593Smuzhiyun struct nand_chip *this = mtd_to_nand(mtd);
1151*4882a593Smuzhiyun int len, res = 0;
1152*4882a593Smuzhiyun int chip, chipsel;
1153*4882a593Smuzhiyun uint8_t *buf;
1154*4882a593Smuzhiyun struct nand_bbt_descr *td = this->bbt_td;
1155*4882a593Smuzhiyun struct nand_bbt_descr *md = this->bbt_md;
1156*4882a593Smuzhiyun
1157*4882a593Smuzhiyun if (!this->bbt || !td)
1158*4882a593Smuzhiyun return -EINVAL;
1159*4882a593Smuzhiyun
1160*4882a593Smuzhiyun /* Allocate a temporary buffer for one eraseblock incl. oob */
1161*4882a593Smuzhiyun len = (1 << this->bbt_erase_shift);
1162*4882a593Smuzhiyun len += (len >> this->page_shift) * mtd->oobsize;
1163*4882a593Smuzhiyun buf = kmalloc(len, GFP_KERNEL);
1164*4882a593Smuzhiyun if (!buf)
1165*4882a593Smuzhiyun return -ENOMEM;
1166*4882a593Smuzhiyun
1167*4882a593Smuzhiyun /* Do we have a bbt per chip? */
1168*4882a593Smuzhiyun if (td->options & NAND_BBT_PERCHIP) {
1169*4882a593Smuzhiyun chip = (int)(offs >> this->chip_shift);
1170*4882a593Smuzhiyun chipsel = chip;
1171*4882a593Smuzhiyun } else {
1172*4882a593Smuzhiyun chip = 0;
1173*4882a593Smuzhiyun chipsel = -1;
1174*4882a593Smuzhiyun }
1175*4882a593Smuzhiyun
1176*4882a593Smuzhiyun td->version[chip]++;
1177*4882a593Smuzhiyun if (md)
1178*4882a593Smuzhiyun md->version[chip]++;
1179*4882a593Smuzhiyun
1180*4882a593Smuzhiyun /* Write the bad block table to the device? */
1181*4882a593Smuzhiyun if (td->options & NAND_BBT_WRITE) {
1182*4882a593Smuzhiyun res = write_bbt(mtd, buf, td, md, chipsel);
1183*4882a593Smuzhiyun if (res < 0)
1184*4882a593Smuzhiyun goto out;
1185*4882a593Smuzhiyun }
1186*4882a593Smuzhiyun /* Write the mirror bad block table to the device? */
1187*4882a593Smuzhiyun if (md && (md->options & NAND_BBT_WRITE)) {
1188*4882a593Smuzhiyun res = write_bbt(mtd, buf, md, td, chipsel);
1189*4882a593Smuzhiyun }
1190*4882a593Smuzhiyun
1191*4882a593Smuzhiyun out:
1192*4882a593Smuzhiyun kfree(buf);
1193*4882a593Smuzhiyun return res;
1194*4882a593Smuzhiyun }
1195*4882a593Smuzhiyun
1196*4882a593Smuzhiyun /*
1197*4882a593Smuzhiyun * Define some generic bad / good block scan pattern which are used
1198*4882a593Smuzhiyun * while scanning a device for factory marked good / bad blocks.
1199*4882a593Smuzhiyun */
1200*4882a593Smuzhiyun static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1201*4882a593Smuzhiyun
1202*4882a593Smuzhiyun /* Generic flash bbt descriptors */
1203*4882a593Smuzhiyun static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1204*4882a593Smuzhiyun static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1205*4882a593Smuzhiyun
1206*4882a593Smuzhiyun static struct nand_bbt_descr bbt_main_descr = {
1207*4882a593Smuzhiyun .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1208*4882a593Smuzhiyun | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1209*4882a593Smuzhiyun .offs = 8,
1210*4882a593Smuzhiyun .len = 4,
1211*4882a593Smuzhiyun .veroffs = 12,
1212*4882a593Smuzhiyun .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1213*4882a593Smuzhiyun .pattern = bbt_pattern
1214*4882a593Smuzhiyun };
1215*4882a593Smuzhiyun
1216*4882a593Smuzhiyun static struct nand_bbt_descr bbt_mirror_descr = {
1217*4882a593Smuzhiyun .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1218*4882a593Smuzhiyun | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1219*4882a593Smuzhiyun .offs = 8,
1220*4882a593Smuzhiyun .len = 4,
1221*4882a593Smuzhiyun .veroffs = 12,
1222*4882a593Smuzhiyun .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1223*4882a593Smuzhiyun .pattern = mirror_pattern
1224*4882a593Smuzhiyun };
1225*4882a593Smuzhiyun
1226*4882a593Smuzhiyun static struct nand_bbt_descr bbt_main_no_oob_descr = {
1227*4882a593Smuzhiyun .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1228*4882a593Smuzhiyun | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1229*4882a593Smuzhiyun | NAND_BBT_NO_OOB,
1230*4882a593Smuzhiyun .len = 4,
1231*4882a593Smuzhiyun .veroffs = 4,
1232*4882a593Smuzhiyun .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1233*4882a593Smuzhiyun .pattern = bbt_pattern
1234*4882a593Smuzhiyun };
1235*4882a593Smuzhiyun
1236*4882a593Smuzhiyun static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
1237*4882a593Smuzhiyun .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1238*4882a593Smuzhiyun | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1239*4882a593Smuzhiyun | NAND_BBT_NO_OOB,
1240*4882a593Smuzhiyun .len = 4,
1241*4882a593Smuzhiyun .veroffs = 4,
1242*4882a593Smuzhiyun .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1243*4882a593Smuzhiyun .pattern = mirror_pattern
1244*4882a593Smuzhiyun };
1245*4882a593Smuzhiyun
1246*4882a593Smuzhiyun #define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
1247*4882a593Smuzhiyun /**
1248*4882a593Smuzhiyun * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
1249*4882a593Smuzhiyun * @this: NAND chip to create descriptor for
1250*4882a593Smuzhiyun *
1251*4882a593Smuzhiyun * This function allocates and initializes a nand_bbt_descr for BBM detection
1252*4882a593Smuzhiyun * based on the properties of @this. The new descriptor is stored in
1253*4882a593Smuzhiyun * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1254*4882a593Smuzhiyun * passed to this function.
1255*4882a593Smuzhiyun */
nand_create_badblock_pattern(struct nand_chip * this)1256*4882a593Smuzhiyun static int nand_create_badblock_pattern(struct nand_chip *this)
1257*4882a593Smuzhiyun {
1258*4882a593Smuzhiyun struct nand_bbt_descr *bd;
1259*4882a593Smuzhiyun if (this->badblock_pattern) {
1260*4882a593Smuzhiyun pr_warn("Bad block pattern already allocated; not replacing\n");
1261*4882a593Smuzhiyun return -EINVAL;
1262*4882a593Smuzhiyun }
1263*4882a593Smuzhiyun bd = kzalloc(sizeof(*bd), GFP_KERNEL);
1264*4882a593Smuzhiyun if (!bd)
1265*4882a593Smuzhiyun return -ENOMEM;
1266*4882a593Smuzhiyun bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
1267*4882a593Smuzhiyun bd->offs = this->badblockpos;
1268*4882a593Smuzhiyun bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1269*4882a593Smuzhiyun bd->pattern = scan_ff_pattern;
1270*4882a593Smuzhiyun bd->options |= NAND_BBT_DYNAMICSTRUCT;
1271*4882a593Smuzhiyun this->badblock_pattern = bd;
1272*4882a593Smuzhiyun return 0;
1273*4882a593Smuzhiyun }
1274*4882a593Smuzhiyun
1275*4882a593Smuzhiyun /**
1276*4882a593Smuzhiyun * nand_default_bbt - [NAND Interface] Select a default bad block table for the device
1277*4882a593Smuzhiyun * @mtd: MTD device structure
1278*4882a593Smuzhiyun *
1279*4882a593Smuzhiyun * This function selects the default bad block table support for the device and
1280*4882a593Smuzhiyun * calls the nand_scan_bbt function.
1281*4882a593Smuzhiyun */
nand_default_bbt(struct mtd_info * mtd)1282*4882a593Smuzhiyun int nand_default_bbt(struct mtd_info *mtd)
1283*4882a593Smuzhiyun {
1284*4882a593Smuzhiyun struct nand_chip *this = mtd_to_nand(mtd);
1285*4882a593Smuzhiyun int ret;
1286*4882a593Smuzhiyun
1287*4882a593Smuzhiyun /* Is a flash based bad block table requested? */
1288*4882a593Smuzhiyun if (this->bbt_options & NAND_BBT_USE_FLASH) {
1289*4882a593Smuzhiyun /* Use the default pattern descriptors */
1290*4882a593Smuzhiyun if (!this->bbt_td) {
1291*4882a593Smuzhiyun if (this->bbt_options & NAND_BBT_NO_OOB) {
1292*4882a593Smuzhiyun this->bbt_td = &bbt_main_no_oob_descr;
1293*4882a593Smuzhiyun this->bbt_md = &bbt_mirror_no_oob_descr;
1294*4882a593Smuzhiyun } else {
1295*4882a593Smuzhiyun this->bbt_td = &bbt_main_descr;
1296*4882a593Smuzhiyun this->bbt_md = &bbt_mirror_descr;
1297*4882a593Smuzhiyun }
1298*4882a593Smuzhiyun }
1299*4882a593Smuzhiyun } else {
1300*4882a593Smuzhiyun this->bbt_td = NULL;
1301*4882a593Smuzhiyun this->bbt_md = NULL;
1302*4882a593Smuzhiyun }
1303*4882a593Smuzhiyun
1304*4882a593Smuzhiyun if (!this->badblock_pattern) {
1305*4882a593Smuzhiyun ret = nand_create_badblock_pattern(this);
1306*4882a593Smuzhiyun if (ret)
1307*4882a593Smuzhiyun return ret;
1308*4882a593Smuzhiyun }
1309*4882a593Smuzhiyun
1310*4882a593Smuzhiyun return nand_scan_bbt(mtd, this->badblock_pattern);
1311*4882a593Smuzhiyun }
1312*4882a593Smuzhiyun
1313*4882a593Smuzhiyun /**
1314*4882a593Smuzhiyun * nand_isreserved_bbt - [NAND Interface] Check if a block is reserved
1315*4882a593Smuzhiyun * @mtd: MTD device structure
1316*4882a593Smuzhiyun * @offs: offset in the device
1317*4882a593Smuzhiyun */
nand_isreserved_bbt(struct mtd_info * mtd,loff_t offs)1318*4882a593Smuzhiyun int nand_isreserved_bbt(struct mtd_info *mtd, loff_t offs)
1319*4882a593Smuzhiyun {
1320*4882a593Smuzhiyun struct nand_chip *this = mtd_to_nand(mtd);
1321*4882a593Smuzhiyun int block;
1322*4882a593Smuzhiyun
1323*4882a593Smuzhiyun block = (int)(offs >> this->bbt_erase_shift);
1324*4882a593Smuzhiyun return bbt_get_entry(this, block) == BBT_BLOCK_RESERVED;
1325*4882a593Smuzhiyun }
1326*4882a593Smuzhiyun
1327*4882a593Smuzhiyun /**
1328*4882a593Smuzhiyun * nand_isbad_bbt - [NAND Interface] Check if a block is bad
1329*4882a593Smuzhiyun * @mtd: MTD device structure
1330*4882a593Smuzhiyun * @offs: offset in the device
1331*4882a593Smuzhiyun * @allowbbt: allow access to bad block table region
1332*4882a593Smuzhiyun */
nand_isbad_bbt(struct mtd_info * mtd,loff_t offs,int allowbbt)1333*4882a593Smuzhiyun int nand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
1334*4882a593Smuzhiyun {
1335*4882a593Smuzhiyun struct nand_chip *this = mtd_to_nand(mtd);
1336*4882a593Smuzhiyun int block, res;
1337*4882a593Smuzhiyun
1338*4882a593Smuzhiyun block = (int)(offs >> this->bbt_erase_shift);
1339*4882a593Smuzhiyun res = bbt_get_entry(this, block);
1340*4882a593Smuzhiyun
1341*4882a593Smuzhiyun pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1342*4882a593Smuzhiyun (unsigned int)offs, block, res);
1343*4882a593Smuzhiyun
1344*4882a593Smuzhiyun switch (res) {
1345*4882a593Smuzhiyun case BBT_BLOCK_GOOD:
1346*4882a593Smuzhiyun return 0;
1347*4882a593Smuzhiyun case BBT_BLOCK_WORN:
1348*4882a593Smuzhiyun return 1;
1349*4882a593Smuzhiyun case BBT_BLOCK_RESERVED:
1350*4882a593Smuzhiyun return allowbbt ? 0 : 1;
1351*4882a593Smuzhiyun }
1352*4882a593Smuzhiyun return 1;
1353*4882a593Smuzhiyun }
1354*4882a593Smuzhiyun
1355*4882a593Smuzhiyun /**
1356*4882a593Smuzhiyun * nand_markbad_bbt - [NAND Interface] Mark a block bad in the BBT
1357*4882a593Smuzhiyun * @mtd: MTD device structure
1358*4882a593Smuzhiyun * @offs: offset of the bad block
1359*4882a593Smuzhiyun */
nand_markbad_bbt(struct mtd_info * mtd,loff_t offs)1360*4882a593Smuzhiyun int nand_markbad_bbt(struct mtd_info *mtd, loff_t offs)
1361*4882a593Smuzhiyun {
1362*4882a593Smuzhiyun struct nand_chip *this = mtd_to_nand(mtd);
1363*4882a593Smuzhiyun int block, ret = 0;
1364*4882a593Smuzhiyun
1365*4882a593Smuzhiyun block = (int)(offs >> this->bbt_erase_shift);
1366*4882a593Smuzhiyun
1367*4882a593Smuzhiyun /* Mark bad block in memory */
1368*4882a593Smuzhiyun bbt_mark_entry(this, block, BBT_BLOCK_WORN);
1369*4882a593Smuzhiyun
1370*4882a593Smuzhiyun /* Update flash-based bad block table */
1371*4882a593Smuzhiyun if (this->bbt_options & NAND_BBT_USE_FLASH)
1372*4882a593Smuzhiyun ret = nand_update_bbt(mtd, offs);
1373*4882a593Smuzhiyun
1374*4882a593Smuzhiyun return ret;
1375*4882a593Smuzhiyun }
1376