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