1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Bad Block Table support for the OneNAND driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright(c) 2005 Samsung Electronics
6*4882a593Smuzhiyun * Kyungmin Park <kyungmin.park@samsung.com>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Derived from nand_bbt.c
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * TODO:
11*4882a593Smuzhiyun * Split BBT core and chip specific BBT.
12*4882a593Smuzhiyun */
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include <linux/slab.h>
15*4882a593Smuzhiyun #include <linux/mtd/mtd.h>
16*4882a593Smuzhiyun #include <linux/mtd/onenand.h>
17*4882a593Smuzhiyun #include <linux/export.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun /**
20*4882a593Smuzhiyun * check_short_pattern - [GENERIC] check if a pattern is in the buffer
21*4882a593Smuzhiyun * @param buf the buffer to search
22*4882a593Smuzhiyun * @param len the length of buffer to search
23*4882a593Smuzhiyun * @param paglen the pagelength
24*4882a593Smuzhiyun * @param td search pattern descriptor
25*4882a593Smuzhiyun *
26*4882a593Smuzhiyun * Check for a pattern at the given place. Used to search bad block
27*4882a593Smuzhiyun * tables and good / bad block identifiers. Same as check_pattern, but
28*4882a593Smuzhiyun * no optional empty check and the pattern is expected to start
29*4882a593Smuzhiyun * at offset 0.
30*4882a593Smuzhiyun *
31*4882a593Smuzhiyun */
check_short_pattern(uint8_t * buf,int len,int paglen,struct nand_bbt_descr * td)32*4882a593Smuzhiyun static int check_short_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
33*4882a593Smuzhiyun {
34*4882a593Smuzhiyun int i;
35*4882a593Smuzhiyun uint8_t *p = buf;
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun /* Compare the pattern */
38*4882a593Smuzhiyun for (i = 0; i < td->len; i++) {
39*4882a593Smuzhiyun if (p[i] != td->pattern[i])
40*4882a593Smuzhiyun return -1;
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun return 0;
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun /**
46*4882a593Smuzhiyun * create_bbt - [GENERIC] Create a bad block table by scanning the device
47*4882a593Smuzhiyun * @param mtd MTD device structure
48*4882a593Smuzhiyun * @param buf temporary buffer
49*4882a593Smuzhiyun * @param bd descriptor for the good/bad block search pattern
50*4882a593Smuzhiyun * @param chip create the table for a specific chip, -1 read all chips.
51*4882a593Smuzhiyun * Applies only if NAND_BBT_PERCHIP option is set
52*4882a593Smuzhiyun *
53*4882a593Smuzhiyun * Create a bad block table by scanning the device
54*4882a593Smuzhiyun * for the given good/bad block identify pattern
55*4882a593Smuzhiyun */
create_bbt(struct mtd_info * mtd,uint8_t * buf,struct nand_bbt_descr * bd,int chip)56*4882a593Smuzhiyun static int create_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd, int chip)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun struct onenand_chip *this = mtd->priv;
59*4882a593Smuzhiyun struct bbm_info *bbm = this->bbm;
60*4882a593Smuzhiyun int i, j, numblocks, len, scanlen;
61*4882a593Smuzhiyun int startblock;
62*4882a593Smuzhiyun loff_t from;
63*4882a593Smuzhiyun size_t readlen, ooblen;
64*4882a593Smuzhiyun struct mtd_oob_ops ops;
65*4882a593Smuzhiyun int rgn;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun printk(KERN_INFO "Scanning device for bad blocks\n");
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun len = 2;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /* We need only read few bytes from the OOB area */
72*4882a593Smuzhiyun scanlen = ooblen = 0;
73*4882a593Smuzhiyun readlen = bd->len;
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun /* chip == -1 case only */
76*4882a593Smuzhiyun /* Note that numblocks is 2 * (real numblocks) here;
77*4882a593Smuzhiyun * see i += 2 below as it makses shifting and masking less painful
78*4882a593Smuzhiyun */
79*4882a593Smuzhiyun numblocks = this->chipsize >> (bbm->bbt_erase_shift - 1);
80*4882a593Smuzhiyun startblock = 0;
81*4882a593Smuzhiyun from = 0;
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun ops.mode = MTD_OPS_PLACE_OOB;
84*4882a593Smuzhiyun ops.ooblen = readlen;
85*4882a593Smuzhiyun ops.oobbuf = buf;
86*4882a593Smuzhiyun ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun for (i = startblock; i < numblocks; ) {
89*4882a593Smuzhiyun int ret;
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun for (j = 0; j < len; j++) {
92*4882a593Smuzhiyun /* No need to read pages fully,
93*4882a593Smuzhiyun * just read required OOB bytes */
94*4882a593Smuzhiyun ret = onenand_bbt_read_oob(mtd,
95*4882a593Smuzhiyun from + j * this->writesize + bd->offs, &ops);
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun /* If it is a initial bad block, just ignore it */
98*4882a593Smuzhiyun if (ret == ONENAND_BBT_READ_FATAL_ERROR)
99*4882a593Smuzhiyun return -EIO;
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun if (ret || check_short_pattern(&buf[j * scanlen],
102*4882a593Smuzhiyun scanlen, this->writesize, bd)) {
103*4882a593Smuzhiyun bbm->bbt[i >> 3] |= 0x03 << (i & 0x6);
104*4882a593Smuzhiyun printk(KERN_INFO "OneNAND eraseblock %d is an "
105*4882a593Smuzhiyun "initial bad block\n", i >> 1);
106*4882a593Smuzhiyun mtd->ecc_stats.badblocks++;
107*4882a593Smuzhiyun break;
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun i += 2;
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun if (FLEXONENAND(this)) {
113*4882a593Smuzhiyun rgn = flexonenand_region(mtd, from);
114*4882a593Smuzhiyun from += mtd->eraseregions[rgn].erasesize;
115*4882a593Smuzhiyun } else
116*4882a593Smuzhiyun from += (1 << bbm->bbt_erase_shift);
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun return 0;
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun /**
124*4882a593Smuzhiyun * onenand_memory_bbt - [GENERIC] create a memory based bad block table
125*4882a593Smuzhiyun * @param mtd MTD device structure
126*4882a593Smuzhiyun * @param bd descriptor for the good/bad block search pattern
127*4882a593Smuzhiyun *
128*4882a593Smuzhiyun * The function creates a memory based bbt by scanning the device
129*4882a593Smuzhiyun * for manufacturer / software marked good / bad blocks
130*4882a593Smuzhiyun */
onenand_memory_bbt(struct mtd_info * mtd,struct nand_bbt_descr * bd)131*4882a593Smuzhiyun static inline int onenand_memory_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun struct onenand_chip *this = mtd->priv;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun return create_bbt(mtd, this->page_buf, bd, -1);
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun /**
139*4882a593Smuzhiyun * onenand_isbad_bbt - [OneNAND Interface] Check if a block is bad
140*4882a593Smuzhiyun * @param mtd MTD device structure
141*4882a593Smuzhiyun * @param offs offset in the device
142*4882a593Smuzhiyun * @param allowbbt allow access to bad block table region
143*4882a593Smuzhiyun */
onenand_isbad_bbt(struct mtd_info * mtd,loff_t offs,int allowbbt)144*4882a593Smuzhiyun static int onenand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun struct onenand_chip *this = mtd->priv;
147*4882a593Smuzhiyun struct bbm_info *bbm = this->bbm;
148*4882a593Smuzhiyun int block;
149*4882a593Smuzhiyun uint8_t res;
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun /* Get block number * 2 */
152*4882a593Smuzhiyun block = (int) (onenand_block(this, offs) << 1);
153*4882a593Smuzhiyun res = (bbm->bbt[block >> 3] >> (block & 0x06)) & 0x03;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun pr_debug("onenand_isbad_bbt: bbt info for offs 0x%08x: (block %d) 0x%02x\n",
156*4882a593Smuzhiyun (unsigned int) offs, block >> 1, res);
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun switch ((int) res) {
159*4882a593Smuzhiyun case 0x00: return 0;
160*4882a593Smuzhiyun case 0x01: return 1;
161*4882a593Smuzhiyun case 0x02: return allowbbt ? 0 : 1;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun return 1;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun /**
168*4882a593Smuzhiyun * onenand_scan_bbt - [OneNAND Interface] scan, find, read and maybe create bad block table(s)
169*4882a593Smuzhiyun * @param mtd MTD device structure
170*4882a593Smuzhiyun * @param bd descriptor for the good/bad block search pattern
171*4882a593Smuzhiyun *
172*4882a593Smuzhiyun * The function checks, if a bad block table(s) is/are already
173*4882a593Smuzhiyun * available. If not it scans the device for manufacturer
174*4882a593Smuzhiyun * marked good / bad blocks and writes the bad block table(s) to
175*4882a593Smuzhiyun * the selected place.
176*4882a593Smuzhiyun *
177*4882a593Smuzhiyun * The bad block table memory is allocated here. It is freed
178*4882a593Smuzhiyun * by the onenand_release function.
179*4882a593Smuzhiyun *
180*4882a593Smuzhiyun */
onenand_scan_bbt(struct mtd_info * mtd,struct nand_bbt_descr * bd)181*4882a593Smuzhiyun static int onenand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun struct onenand_chip *this = mtd->priv;
184*4882a593Smuzhiyun struct bbm_info *bbm = this->bbm;
185*4882a593Smuzhiyun int len, ret = 0;
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun len = this->chipsize >> (this->erase_shift + 2);
188*4882a593Smuzhiyun /* Allocate memory (2bit per block) and clear the memory bad block table */
189*4882a593Smuzhiyun bbm->bbt = kzalloc(len, GFP_KERNEL);
190*4882a593Smuzhiyun if (!bbm->bbt)
191*4882a593Smuzhiyun return -ENOMEM;
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun /* Set erase shift */
194*4882a593Smuzhiyun bbm->bbt_erase_shift = this->erase_shift;
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun if (!bbm->isbad_bbt)
197*4882a593Smuzhiyun bbm->isbad_bbt = onenand_isbad_bbt;
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun /* Scan the device to build a memory based bad block table */
200*4882a593Smuzhiyun if ((ret = onenand_memory_bbt(mtd, bd))) {
201*4882a593Smuzhiyun printk(KERN_ERR "onenand_scan_bbt: Can't scan flash and build the RAM-based BBT\n");
202*4882a593Smuzhiyun kfree(bbm->bbt);
203*4882a593Smuzhiyun bbm->bbt = NULL;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun return ret;
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun /*
210*4882a593Smuzhiyun * Define some generic bad / good block scan pattern which are used
211*4882a593Smuzhiyun * while scanning a device for factory marked good / bad blocks.
212*4882a593Smuzhiyun */
213*4882a593Smuzhiyun static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun static struct nand_bbt_descr largepage_memorybased = {
216*4882a593Smuzhiyun .options = 0,
217*4882a593Smuzhiyun .offs = 0,
218*4882a593Smuzhiyun .len = 2,
219*4882a593Smuzhiyun .pattern = scan_ff_pattern,
220*4882a593Smuzhiyun };
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun /**
223*4882a593Smuzhiyun * onenand_default_bbt - [OneNAND Interface] Select a default bad block table for the device
224*4882a593Smuzhiyun * @param mtd MTD device structure
225*4882a593Smuzhiyun *
226*4882a593Smuzhiyun * This function selects the default bad block table
227*4882a593Smuzhiyun * support for the device and calls the onenand_scan_bbt function
228*4882a593Smuzhiyun */
onenand_default_bbt(struct mtd_info * mtd)229*4882a593Smuzhiyun int onenand_default_bbt(struct mtd_info *mtd)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun struct onenand_chip *this = mtd->priv;
232*4882a593Smuzhiyun struct bbm_info *bbm;
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun this->bbm = kzalloc(sizeof(struct bbm_info), GFP_KERNEL);
235*4882a593Smuzhiyun if (!this->bbm)
236*4882a593Smuzhiyun return -ENOMEM;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun bbm = this->bbm;
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun /* 1KB page has same configuration as 2KB page */
241*4882a593Smuzhiyun if (!bbm->badblock_pattern)
242*4882a593Smuzhiyun bbm->badblock_pattern = &largepage_memorybased;
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun return onenand_scan_bbt(mtd, bbm->badblock_pattern);
245*4882a593Smuzhiyun }
246