aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyungmin Park <kyungmin.park@samsung.com>2005-09-27 06:26:39 -0400
committerThomas Gleixner <tglx@mtd.linutronix.de>2005-11-06 16:39:23 -0500
commit87590e26ff4e7d57dfdaa81780b1b0d9e9970a4c (patch)
treeebc1fc55e3bfb46115e198d78ddb914afc5801d5
parent0255fc1b081cf92b56dfe5e1f3a824d050326614 (diff)
[MTD] OneNAND: Add missing files
Simple bad block table source and header files Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
-rw-r--r--drivers/mtd/onenand/onenand_bbt.c246
-rw-r--r--include/linux/mtd/bbm.h122
2 files changed, 368 insertions, 0 deletions
diff --git a/drivers/mtd/onenand/onenand_bbt.c b/drivers/mtd/onenand/onenand_bbt.c
new file mode 100644
index 000000000000..f40190f499e1
--- /dev/null
+++ b/drivers/mtd/onenand/onenand_bbt.c
@@ -0,0 +1,246 @@
1/*
2 * linux/drivers/mtd/onenand/onenand_bbt.c
3 *
4 * Bad Block Table support for the OneNAND driver
5 *
6 * Copyright(c) 2005 Samsung Electronics
7 * Kyungmin Park <kyungmin.park@samsung.com>
8 *
9 * Derived from nand_bbt.c
10 *
11 * TODO:
12 * Split BBT core and chip specific BBT.
13 */
14
15#include <linux/slab.h>
16#include <linux/mtd/mtd.h>
17#include <linux/mtd/onenand.h>
18#include <linux/mtd/compatmac.h>
19
20/**
21 * check_short_pattern - [GENERIC] check if a pattern is in the buffer
22 * @param buf the buffer to search
23 * @param len the length of buffer to search
24 * @param paglen the pagelength
25 * @param td search pattern descriptor
26 *
27 * Check for a pattern at the given place. Used to search bad block
28 * tables and good / bad block identifiers. Same as check_pattern, but
29 * no optional empty check and the pattern is expected to start
30 * at offset 0.
31 *
32 */
33static int check_short_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
34{
35 int i;
36 uint8_t *p = buf;
37
38 /* Compare the pattern */
39 for (i = 0; i < td->len; i++) {
40 if (p[i] != td->pattern[i])
41 return -1;
42 }
43 return 0;
44}
45
46/**
47 * create_bbt - [GENERIC] Create a bad block table by scanning the device
48 * @param mtd MTD device structure
49 * @param buf temporary buffer
50 * @param bd descriptor for the good/bad block search pattern
51 * @param chip create the table for a specific chip, -1 read all chips.
52 * Applies only if NAND_BBT_PERCHIP option is set
53 *
54 * Create a bad block table by scanning the device
55 * for the given good/bad block identify pattern
56 */
57static int create_bbt(struct mtd_info *mtd, uint8_t *buf, struct nand_bbt_descr *bd, int chip)
58{
59 struct onenand_chip *this = mtd->priv;
60 struct bbm_info *bbm = this->bbm;
61 int i, j, numblocks, len, scanlen;
62 int startblock;
63 loff_t from;
64 size_t readlen, ooblen;
65
66 printk(KERN_INFO "Scanning device for bad blocks\n");
67
68 len = 1;
69
70 /* We need only read few bytes from the OOB area */
71 scanlen = ooblen = 0;
72 readlen = bd->len;
73
74 /* chip == -1 case only */
75 /* Note that numblocks is 2 * (real numblocks) here;
76 * see i += 2 below as it makses shifting and masking less painful
77 */
78 numblocks = mtd->size >> (bbm->bbt_erase_shift - 1);
79 startblock = 0;
80 from = 0;
81
82 for (i = startblock; i < numblocks; ) {
83 int ret;
84
85 for (j = 0; j < len; j++) {
86 size_t retlen;
87
88 /* No need to read pages fully,
89 * just read required OOB bytes */
90 ret = mtd->read_oob(mtd, from + j * mtd->oobblock + bd->offs,
91 readlen, &retlen, &buf[0]);
92
93 if (ret)
94 return ret;
95
96 if (check_short_pattern(&buf[j * scanlen], scanlen, mtd->oobblock, bd)) {
97 bbm->bbt[i >> 3] |= 0x03 << (i & 0x6);
98 printk(KERN_WARNING "Bad eraseblock %d at 0x%08x\n",
99 i >> 1, (unsigned int) from);
100 break;
101 }
102 }
103 i += 2;
104 from += (1 << bbm->bbt_erase_shift);
105 }
106
107 return 0;
108}
109
110
111/**
112 * onenand_memory_bbt - [GENERIC] create a memory based bad block table
113 * @param mtd MTD device structure
114 * @param bd descriptor for the good/bad block search pattern
115 *
116 * The function creates a memory based bbt by scanning the device
117 * for manufacturer / software marked good / bad blocks
118 */
119static inline int onenand_memory_bbt (struct mtd_info *mtd, struct nand_bbt_descr *bd)
120{
121 unsigned char data_buf[MAX_ONENAND_PAGESIZE];
122
123 bd->options &= ~NAND_BBT_SCANEMPTY;
124 return create_bbt(mtd, data_buf, bd, -1);
125}
126
127/**
128 * onenand_isbad_bbt - [OneNAND Interface] Check if a block is bad
129 * @param mtd MTD device structure
130 * @param offs offset in the device
131 * @param allowbbt allow access to bad block table region
132 */
133static int onenand_isbad_bbt(struct mtd_info *mtd, loff_t offs, int allowbbt)
134{
135 struct onenand_chip *this = mtd->priv;
136 struct bbm_info *bbm = this->bbm;
137 int block;
138 uint8_t res;
139
140 /* Get block number * 2 */
141 block = (int) (offs >> (bbm->bbt_erase_shift - 1));
142 res = (bbm->bbt[block >> 3] >> (block & 0x06)) & 0x03;
143
144 DEBUG(MTD_DEBUG_LEVEL2, "onenand_isbad_bbt: bbt info for offs 0x%08x: (block %d) 0x%02x\n",
145 (unsigned int) offs, block >> 1, res);
146
147 switch ((int) res) {
148 case 0x00: return 0;
149 case 0x01: return 1;
150 case 0x02: return allowbbt ? 0 : 1;
151 }
152
153 return 1;
154}
155
156/**
157 * onenand_scan_bbt - [OneNAND Interface] scan, find, read and maybe create bad block table(s)
158 * @param mtd MTD device structure
159 * @param bd descriptor for the good/bad block search pattern
160 *
161 * The function checks, if a bad block table(s) is/are already
162 * available. If not it scans the device for manufacturer
163 * marked good / bad blocks and writes the bad block table(s) to
164 * the selected place.
165 *
166 * The bad block table memory is allocated here. It must be freed
167 * by calling the onenand_free_bbt function.
168 *
169 */
170int onenand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd)
171{
172 struct onenand_chip *this = mtd->priv;
173 struct bbm_info *bbm = this->bbm;
174 int len, ret = 0;
175
176 len = mtd->size >> (this->erase_shift + 2);
177 /* Allocate memory (2bit per block) */
178 bbm->bbt = kmalloc(len, GFP_KERNEL);
179 if (!bbm->bbt) {
180 printk(KERN_ERR "onenand_scan_bbt: Out of memory\n");
181 return -ENOMEM;
182 }
183 /* Clear the memory bad block table */
184 memset(bbm->bbt, 0x00, len);
185
186 /* Set the bad block position */
187 bbm->badblockpos = ONENAND_BADBLOCK_POS;
188
189 /* Set erase shift */
190 bbm->bbt_erase_shift = this->erase_shift;
191
192 if (!bbm->isbad_bbt)
193 bbm->isbad_bbt = onenand_isbad_bbt;
194
195 /* Scan the device to build a memory based bad block table */
196 if ((ret = onenand_memory_bbt(mtd, bd))) {
197 printk(KERN_ERR "onenand_scan_bbt: Can't scan flash and build the RAM-based BBT\n");
198 kfree(bbm->bbt);
199 bbm->bbt = NULL;
200 }
201
202 return ret;
203}
204
205/*
206 * Define some generic bad / good block scan pattern which are used
207 * while scanning a device for factory marked good / bad blocks.
208 */
209static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
210
211static struct nand_bbt_descr largepage_memorybased = {
212 .options = 0,
213 .offs = 0,
214 .len = 2,
215 .pattern = scan_ff_pattern,
216};
217
218/**
219 * onenand_default_bbt - [OneNAND Interface] Select a default bad block table for the device
220 * @param mtd MTD device structure
221 *
222 * This function selects the default bad block table
223 * support for the device and calls the onenand_scan_bbt function
224 */
225int onenand_default_bbt(struct mtd_info *mtd)
226{
227 struct onenand_chip *this = mtd->priv;
228 struct bbm_info *bbm;
229
230 this->bbm = kmalloc(sizeof(struct bbm_info), GFP_KERNEL);
231 if (!this->bbm)
232 return -ENOMEM;
233
234 bbm = this->bbm;
235
236 memset(bbm, 0, sizeof(struct bbm_info));
237
238 /* 1KB page has same configuration as 2KB page */
239 if (!bbm->badblock_pattern)
240 bbm->badblock_pattern = &largepage_memorybased;
241
242 return onenand_scan_bbt(mtd, bbm->badblock_pattern);
243}
244
245EXPORT_SYMBOL(onenand_scan_bbt);
246EXPORT_SYMBOL(onenand_default_bbt);
diff --git a/include/linux/mtd/bbm.h b/include/linux/mtd/bbm.h
new file mode 100644
index 000000000000..92b42cb7ed2e
--- /dev/null
+++ b/include/linux/mtd/bbm.h
@@ -0,0 +1,122 @@
1/*
2 * linux/include/linux/mtd/bbm.h
3 *
4 * NAND family Bad Block Management (BBM) header file
5 * - Bad Block Table (BBT) implementation
6 *
7 * Copyright (c) 2005 Samsung Electronics
8 * Kyungmin Park <kyungmin.park@samsung.com>
9 *
10 * Copyright (c) 2000-2005
11 * Thomas Gleixner <tglx@linuxtronix.de>
12 *
13 */
14#ifndef __LINUX_MTD_BBM_H
15#define __LINUX_MTD_BBM_H
16
17/* The maximum number of NAND chips in an array */
18#define NAND_MAX_CHIPS 8
19
20/**
21 * struct nand_bbt_descr - bad block table descriptor
22 * @param options options for this descriptor
23 * @param pages the page(s) where we find the bbt, used with
24 * option BBT_ABSPAGE when bbt is searched,
25 * then we store the found bbts pages here.
26 * Its an array and supports up to 8 chips now
27 * @param offs offset of the pattern in the oob area of the page
28 * @param veroffs offset of the bbt version counter in the oob are of the page
29 * @param version version read from the bbt page during scan
30 * @param len length of the pattern, if 0 no pattern check is performed
31 * @param maxblocks maximum number of blocks to search for a bbt. This number of
32 * blocks is reserved at the end of the device
33 * where the tables are written.
34 * @param reserved_block_code if non-0, this pattern denotes a reserved
35 * (rather than bad) block in the stored bbt
36 * @param pattern pattern to identify bad block table or factory marked
37 * good / bad blocks, can be NULL, if len = 0
38 *
39 * Descriptor for the bad block table marker and the descriptor for the
40 * pattern which identifies good and bad blocks. The assumption is made
41 * that the pattern and the version count are always located in the oob area
42 * of the first block.
43 */
44struct nand_bbt_descr {
45 int options;
46 int pages[NAND_MAX_CHIPS];
47 int offs;
48 int veroffs;
49 uint8_t version[NAND_MAX_CHIPS];
50 int len;
51 int maxblocks;
52 int reserved_block_code;
53 uint8_t *pattern;
54};
55
56/* Options for the bad block table descriptors */
57
58/* The number of bits used per block in the bbt on the device */
59#define NAND_BBT_NRBITS_MSK 0x0000000F
60#define NAND_BBT_1BIT 0x00000001
61#define NAND_BBT_2BIT 0x00000002
62#define NAND_BBT_4BIT 0x00000004
63#define NAND_BBT_8BIT 0x00000008
64/* The bad block table is in the last good block of the device */
65#define NAND_BBT_LASTBLOCK 0x00000010
66/* The bbt is at the given page, else we must scan for the bbt */
67#define NAND_BBT_ABSPAGE 0x00000020
68/* The bbt is at the given page, else we must scan for the bbt */
69#define NAND_BBT_SEARCH 0x00000040
70/* bbt is stored per chip on multichip devices */
71#define NAND_BBT_PERCHIP 0x00000080
72/* bbt has a version counter at offset veroffs */
73#define NAND_BBT_VERSION 0x00000100
74/* Create a bbt if none axists */
75#define NAND_BBT_CREATE 0x00000200
76/* Search good / bad pattern through all pages of a block */
77#define NAND_BBT_SCANALLPAGES 0x00000400
78/* Scan block empty during good / bad block scan */
79#define NAND_BBT_SCANEMPTY 0x00000800
80/* Write bbt if neccecary */
81#define NAND_BBT_WRITE 0x00001000
82/* Read and write back block contents when writing bbt */
83#define NAND_BBT_SAVECONTENT 0x00002000
84/* Search good / bad pattern on the first and the second page */
85#define NAND_BBT_SCAN2NDPAGE 0x00004000
86
87/* The maximum number of blocks to scan for a bbt */
88#define NAND_BBT_SCAN_MAXBLOCKS 4
89
90/*
91 * Constants for oob configuration
92 */
93#define ONENAND_BADBLOCK_POS 0
94
95/**
96 * struct bbt_info - [GENERIC] Bad Block Table data structure
97 * @param bbt_erase_shift [INTERN] number of address bits in a bbt entry
98 * @param badblockpos [INTERN] position of the bad block marker in the oob area
99 * @param bbt [INTERN] bad block table pointer
100 * @param badblock_pattern [REPLACEABLE] bad block scan pattern used for initial bad block scan
101 * @param priv [OPTIONAL] pointer to private bbm date
102 */
103struct bbm_info {
104 int bbt_erase_shift;
105 int badblockpos;
106 int options;
107
108 uint8_t *bbt;
109
110 int (*isbad_bbt)(struct mtd_info *mtd, loff_t ofs, int allowbbt);
111
112 /* TODO Add more NAND specific fileds */
113 struct nand_bbt_descr *badblock_pattern;
114
115 void *priv;
116};
117
118/* OneNAND BBT interface */
119extern int onenand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd);
120extern int onenand_default_bbt(struct mtd_info *mtd);
121
122#endif /* __LINUX_MTD_BBM_H */