xref: /linux-6.15/include/linux/mtd/bbm.h (revision 71ccc212)
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 © 2005 Samsung Electronics
8  *  Kyungmin Park <[email protected]>
9  *
10  *  Copyright © 2000-2005
11  *  Thomas Gleixner <[email protected]>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
26  *
27  */
28 #ifndef __LINUX_MTD_BBM_H
29 #define __LINUX_MTD_BBM_H
30 
31 /* The maximum number of NAND chips in an array */
32 #define NAND_MAX_CHIPS		8
33 
34 /**
35  * struct nand_bbt_descr - bad block table descriptor
36  * @options:	options for this descriptor
37  * @pages:	the page(s) where we find the bbt, used with option BBT_ABSPAGE
38  *		when bbt is searched, then we store the found bbts pages here.
39  *		Its an array and supports up to 8 chips now
40  * @offs:	offset of the pattern in the oob area of the page
41  * @veroffs:	offset of the bbt version counter in the oob are of the page
42  * @version:	version read from the bbt page during scan
43  * @len:	length of the pattern, if 0 no pattern check is performed
44  * @maxblocks:	maximum number of blocks to search for a bbt. This number of
45  *		blocks is reserved at the end of the device where the tables are
46  *		written.
47  * @reserved_block_code: if non-0, this pattern denotes a reserved (rather than
48  *              bad) block in the stored bbt
49  * @pattern:	pattern to identify bad block table or factory marked good /
50  *		bad blocks, can be NULL, if len = 0
51  *
52  * Descriptor for the bad block table marker and the descriptor for the
53  * pattern which identifies good and bad blocks. The assumption is made
54  * that the pattern and the version count are always located in the oob area
55  * of the first block.
56  */
57 struct nand_bbt_descr {
58 	int options;
59 	int pages[NAND_MAX_CHIPS];
60 	int offs;
61 	int veroffs;
62 	uint8_t version[NAND_MAX_CHIPS];
63 	int len;
64 	int maxblocks;
65 	int reserved_block_code;
66 	uint8_t *pattern;
67 };
68 
69 /* Options for the bad block table descriptors */
70 
71 /* The number of bits used per block in the bbt on the device */
72 #define NAND_BBT_NRBITS_MSK	0x0000000F
73 #define NAND_BBT_1BIT		0x00000001
74 #define NAND_BBT_2BIT		0x00000002
75 #define NAND_BBT_4BIT		0x00000004
76 #define NAND_BBT_8BIT		0x00000008
77 /* The bad block table is in the last good block of the device */
78 #define NAND_BBT_LASTBLOCK	0x00000010
79 /* The bbt is at the given page, else we must scan for the bbt */
80 #define NAND_BBT_ABSPAGE	0x00000020
81 /* The bbt is at the given page, else we must scan for the bbt */
82 #define NAND_BBT_SEARCH		0x00000040
83 /* bbt is stored per chip on multichip devices */
84 #define NAND_BBT_PERCHIP	0x00000080
85 /* bbt has a version counter at offset veroffs */
86 #define NAND_BBT_VERSION	0x00000100
87 /* Create a bbt if none axists */
88 #define NAND_BBT_CREATE		0x00000200
89 /* Search good / bad pattern through all pages of a block */
90 #define NAND_BBT_SCANALLPAGES	0x00000400
91 /* Scan block empty during good / bad block scan */
92 #define NAND_BBT_SCANEMPTY	0x00000800
93 /* Write bbt if neccecary */
94 #define NAND_BBT_WRITE		0x00001000
95 /* Read and write back block contents when writing bbt */
96 #define NAND_BBT_SAVECONTENT	0x00002000
97 /* Search good / bad pattern on the first and the second page */
98 #define NAND_BBT_SCAN2NDPAGE	0x00004000
99 /* Search good / bad pattern on the last page of the eraseblock */
100 #define NAND_BBT_SCANLASTPAGE	0x00008000
101 /* Chip stores bad block marker on BOTH 1st and 6th bytes of OOB */
102 #define NAND_BBT_SCANBYTE1AND6 0x00100000
103 /* The nand_bbt_descr was created dynamicaly and must be freed */
104 #define NAND_BBT_DYNAMICSTRUCT 0x00200000
105 
106 /* The maximum number of blocks to scan for a bbt */
107 #define NAND_BBT_SCAN_MAXBLOCKS	4
108 
109 /*
110  * Constants for oob configuration
111  */
112 #define NAND_SMALL_BADBLOCK_POS		5
113 #define NAND_LARGE_BADBLOCK_POS		0
114 #define ONENAND_BADBLOCK_POS		0
115 
116 /*
117  * Bad block scanning errors
118  */
119 #define ONENAND_BBT_READ_ERROR		1
120 #define ONENAND_BBT_READ_ECC_ERROR	2
121 #define ONENAND_BBT_READ_FATAL_ERROR	4
122 
123 /**
124  * struct bbm_info - [GENERIC] Bad Block Table data structure
125  * @bbt_erase_shift:	[INTERN] number of address bits in a bbt entry
126  * @badblockpos:	[INTERN] position of the bad block marker in the oob area
127  * @options:		options for this descriptor
128  * @bbt:		[INTERN] bad block table pointer
129  * @isbad_bbt:		function to determine if a block is bad
130  * @badblock_pattern:	[REPLACEABLE] bad block scan pattern used for
131  *			initial bad block scan
132  * @priv:		[OPTIONAL] pointer to private bbm date
133  */
134 struct bbm_info {
135 	int bbt_erase_shift;
136 	int badblockpos;
137 	int options;
138 
139 	uint8_t *bbt;
140 
141 	int (*isbad_bbt)(struct mtd_info *mtd, loff_t ofs, int allowbbt);
142 
143 	/* TODO Add more NAND specific fileds */
144 	struct nand_bbt_descr *badblock_pattern;
145 
146 	void *priv;
147 };
148 
149 /* OneNAND BBT interface */
150 extern int onenand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd);
151 extern int onenand_default_bbt(struct mtd_info *mtd);
152 
153 #endif	/* __LINUX_MTD_BBM_H */
154