GNU Linux-libre 4.19.268-gnu1
[releases.git] / include / linux / mtd / bbm.h
1 /*
2  *  NAND family Bad Block Management (BBM) header file
3  *    - Bad Block Table (BBT) implementation
4  *
5  *  Copyright © 2005 Samsung Electronics
6  *  Kyungmin Park <kyungmin.park@samsung.com>
7  *
8  *  Copyright © 2000-2005
9  *  Thomas Gleixner <tglx@linuxtronix.de>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24  *
25  */
26 #ifndef __LINUX_MTD_BBM_H
27 #define __LINUX_MTD_BBM_H
28
29 /* The maximum number of NAND chips in an array */
30 #define NAND_MAX_CHIPS          8
31
32 /**
33  * struct nand_bbt_descr - bad block table descriptor
34  * @options:    options for this descriptor
35  * @pages:      the page(s) where we find the bbt, used with option BBT_ABSPAGE
36  *              when bbt is searched, then we store the found bbts pages here.
37  *              Its an array and supports up to 8 chips now
38  * @offs:       offset of the pattern in the oob area of the page
39  * @veroffs:    offset of the bbt version counter in the oob are of the page
40  * @version:    version read from the bbt page during scan
41  * @len:        length of the pattern, if 0 no pattern check is performed
42  * @maxblocks:  maximum number of blocks to search for a bbt. This number of
43  *              blocks is reserved at the end of the device where the tables are
44  *              written.
45  * @reserved_block_code: if non-0, this pattern denotes a reserved (rather than
46  *              bad) block in the stored bbt
47  * @pattern:    pattern to identify bad block table or factory marked good /
48  *              bad blocks, can be NULL, if len = 0
49  *
50  * Descriptor for the bad block table marker and the descriptor for the
51  * pattern which identifies good and bad blocks. The assumption is made
52  * that the pattern and the version count are always located in the oob area
53  * of the first block.
54  */
55 struct nand_bbt_descr {
56         int options;
57         int pages[NAND_MAX_CHIPS];
58         int offs;
59         int veroffs;
60         uint8_t version[NAND_MAX_CHIPS];
61         int len;
62         int maxblocks;
63         int reserved_block_code;
64         uint8_t *pattern;
65 };
66
67 /* Options for the bad block table descriptors */
68
69 /* The number of bits used per block in the bbt on the device */
70 #define NAND_BBT_NRBITS_MSK     0x0000000F
71 #define NAND_BBT_1BIT           0x00000001
72 #define NAND_BBT_2BIT           0x00000002
73 #define NAND_BBT_4BIT           0x00000004
74 #define NAND_BBT_8BIT           0x00000008
75 /* The bad block table is in the last good block of the device */
76 #define NAND_BBT_LASTBLOCK      0x00000010
77 /* The bbt is at the given page, else we must scan for the bbt */
78 #define NAND_BBT_ABSPAGE        0x00000020
79 /* bbt is stored per chip on multichip devices */
80 #define NAND_BBT_PERCHIP        0x00000080
81 /* bbt has a version counter at offset veroffs */
82 #define NAND_BBT_VERSION        0x00000100
83 /* Create a bbt if none exists */
84 #define NAND_BBT_CREATE         0x00000200
85 /*
86  * Create an empty BBT with no vendor information. Vendor's information may be
87  * unavailable, for example, if the NAND controller has a different data and OOB
88  * layout or if this information is already purged. Must be used in conjunction
89  * with NAND_BBT_CREATE.
90  */
91 #define NAND_BBT_CREATE_EMPTY   0x00000400
92 /* Write bbt if neccecary */
93 #define NAND_BBT_WRITE          0x00002000
94 /* Read and write back block contents when writing bbt */
95 #define NAND_BBT_SAVECONTENT    0x00004000
96 /* Search good / bad pattern on the first and the second page */
97 #define NAND_BBT_SCAN2NDPAGE    0x00008000
98 /* Search good / bad pattern on the last page of the eraseblock */
99 #define NAND_BBT_SCANLASTPAGE   0x00010000
100 /*
101  * Use a flash based bad block table. By default, OOB identifier is saved in
102  * OOB area. This option is passed to the default bad block table function.
103  */
104 #define NAND_BBT_USE_FLASH      0x00020000
105 /*
106  * Do not store flash based bad block table marker in the OOB area; store it
107  * in-band.
108  */
109 #define NAND_BBT_NO_OOB         0x00040000
110 /*
111  * Do not write new bad block markers to OOB; useful, e.g., when ECC covers
112  * entire spare area. Must be used with NAND_BBT_USE_FLASH.
113  */
114 #define NAND_BBT_NO_OOB_BBM     0x00080000
115
116 /*
117  * Flag set by nand_create_default_bbt_descr(), marking that the nand_bbt_descr
118  * was allocated dynamicaly and must be freed in nand_release(). Has no meaning
119  * in nand_chip.bbt_options.
120  */
121 #define NAND_BBT_DYNAMICSTRUCT  0x80000000
122
123 /* The maximum number of blocks to scan for a bbt */
124 #define NAND_BBT_SCAN_MAXBLOCKS 4
125
126 /*
127  * Constants for oob configuration
128  */
129 #define NAND_SMALL_BADBLOCK_POS         5
130 #define NAND_LARGE_BADBLOCK_POS         0
131 #define ONENAND_BADBLOCK_POS            0
132
133 /*
134  * Bad block scanning errors
135  */
136 #define ONENAND_BBT_READ_ERROR          1
137 #define ONENAND_BBT_READ_ECC_ERROR      2
138 #define ONENAND_BBT_READ_FATAL_ERROR    4
139
140 /**
141  * struct bbm_info - [GENERIC] Bad Block Table data structure
142  * @bbt_erase_shift:    [INTERN] number of address bits in a bbt entry
143  * @badblockpos:        [INTERN] position of the bad block marker in the oob area
144  * @options:            options for this descriptor
145  * @bbt:                [INTERN] bad block table pointer
146  * @isbad_bbt:          function to determine if a block is bad
147  * @badblock_pattern:   [REPLACEABLE] bad block scan pattern used for
148  *                      initial bad block scan
149  * @priv:               [OPTIONAL] pointer to private bbm date
150  */
151 struct bbm_info {
152         int bbt_erase_shift;
153         int badblockpos;
154         int options;
155
156         uint8_t *bbt;
157
158         int (*isbad_bbt)(struct mtd_info *mtd, loff_t ofs, int allowbbt);
159
160         /* TODO Add more NAND specific fileds */
161         struct nand_bbt_descr *badblock_pattern;
162
163         void *priv;
164 };
165
166 /* OneNAND BBT interface */
167 extern int onenand_default_bbt(struct mtd_info *mtd);
168
169 #endif  /* __LINUX_MTD_BBM_H */