blob: fb86f1ddef9c5f4711e140b6fd6f0d12b4f32f4e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +09002/*
3 * linux/include/linux/mtd/bbm.h
4 *
5 * NAND family Bad Block Management (BBM) header file
6 * - Bad Block Table (BBT) implementation
7 *
Heiko Schocherf5895d12014-06-24 10:10:04 +02008 * Copyright © 2005 Samsung Electronics
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +09009 * Kyungmin Park <kyungmin.park@samsung.com>
10 *
Heiko Schocherf5895d12014-06-24 10:10:04 +020011 * Copyright © 2000-2005
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090012 * Thomas Gleixner <tglx@linuxtronix.de>
13 *
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090014 */
15#ifndef __LINUX_MTD_BBM_H
16#define __LINUX_MTD_BBM_H
17
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090018/**
19 * struct nand_bbt_descr - bad block table descriptor
Heiko Schocherf5895d12014-06-24 10:10:04 +020020 * @options: options for this descriptor
21 * @pages: the page(s) where we find the bbt, used with option BBT_ABSPAGE
22 * when bbt is searched, then we store the found bbts pages here.
23 * Its an array and supports up to 8 chips now
24 * @offs: offset of the pattern in the oob area of the page
25 * @veroffs: offset of the bbt version counter in the oob are of the page
26 * @version: version read from the bbt page during scan
27 * @len: length of the pattern, if 0 no pattern check is performed
28 * @maxblocks: maximum number of blocks to search for a bbt. This number of
29 * blocks is reserved at the end of the device where the tables are
30 * written.
31 * @reserved_block_code: if non-0, this pattern denotes a reserved (rather than
32 * bad) block in the stored bbt
33 * @pattern: pattern to identify bad block table or factory marked good /
34 * bad blocks, can be NULL, if len = 0
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090035 *
36 * Descriptor for the bad block table marker and the descriptor for the
37 * pattern which identifies good and bad blocks. The assumption is made
38 * that the pattern and the version count are always located in the oob area
39 * of the first block.
40 */
41struct nand_bbt_descr {
42 int options;
Wolfgang Grandegger03025502009-01-16 18:55:54 +010043 int pages[CONFIG_SYS_NAND_MAX_CHIPS];
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090044 int offs;
45 int veroffs;
Wolfgang Grandegger03025502009-01-16 18:55:54 +010046 uint8_t version[CONFIG_SYS_NAND_MAX_CHIPS];
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090047 int len;
48 int maxblocks;
49 int reserved_block_code;
50 uint8_t *pattern;
51};
52
53/* Options for the bad block table descriptors */
54
55/* The number of bits used per block in the bbt on the device */
56#define NAND_BBT_NRBITS_MSK 0x0000000F
57#define NAND_BBT_1BIT 0x00000001
58#define NAND_BBT_2BIT 0x00000002
59#define NAND_BBT_4BIT 0x00000004
60#define NAND_BBT_8BIT 0x00000008
61/* The bad block table is in the last good block of the device */
62#define NAND_BBT_LASTBLOCK 0x00000010
63/* The bbt is at the given page, else we must scan for the bbt */
64#define NAND_BBT_ABSPAGE 0x00000020
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090065/* bbt is stored per chip on multichip devices */
66#define NAND_BBT_PERCHIP 0x00000080
67/* bbt has a version counter at offset veroffs */
68#define NAND_BBT_VERSION 0x00000100
Christian Hitzb8a6b372011-10-12 09:32:02 +020069/* Create a bbt if none exists */
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090070#define NAND_BBT_CREATE 0x00000200
Sergey Lapin3a38a552013-01-14 03:46:50 +000071/*
72 * Create an empty BBT with no vendor information. Vendor's information may be
73 * unavailable, for example, if the NAND controller has a different data and OOB
74 * layout or if this information is already purged. Must be used in conjunction
75 * with NAND_BBT_CREATE.
76 */
77#define NAND_BBT_CREATE_EMPTY 0x00000400
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090078/* Write bbt if neccecary */
Sergey Lapin3a38a552013-01-14 03:46:50 +000079#define NAND_BBT_WRITE 0x00002000
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090080/* Read and write back block contents when writing bbt */
Sergey Lapin3a38a552013-01-14 03:46:50 +000081#define NAND_BBT_SAVECONTENT 0x00004000
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +090082/* Search good / bad pattern on the first and the second page */
Sergey Lapin3a38a552013-01-14 03:46:50 +000083#define NAND_BBT_SCAN2NDPAGE 0x00008000
Christian Hitzb8a6b372011-10-12 09:32:02 +020084/* Search good / bad pattern on the last page of the eraseblock */
Sergey Lapin3a38a552013-01-14 03:46:50 +000085#define NAND_BBT_SCANLASTPAGE 0x00010000
86/*
87 * Use a flash based bad block table. By default, OOB identifier is saved in
88 * OOB area. This option is passed to the default bad block table function.
89 */
90#define NAND_BBT_USE_FLASH 0x00020000
91/*
92 * Do not store flash based bad block table marker in the OOB area; store it
93 * in-band.
94 */
95#define NAND_BBT_NO_OOB 0x00040000
96/*
97 * Do not write new bad block markers to OOB; useful, e.g., when ECC covers
98 * entire spare area. Must be used with NAND_BBT_USE_FLASH.
99 */
100#define NAND_BBT_NO_OOB_BBM 0x00080000
101
102/*
103 * Flag set by nand_create_default_bbt_descr(), marking that the nand_bbt_descr
104 * was allocated dynamicaly and must be freed in nand_release(). Has no meaning
105 * in nand_chip.bbt_options.
106 */
107#define NAND_BBT_DYNAMICSTRUCT 0x80000000
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900108
109/* The maximum number of blocks to scan for a bbt */
110#define NAND_BBT_SCAN_MAXBLOCKS 4
111
112/*
113 * Constants for oob configuration
114 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200115#define NAND_SMALL_BADBLOCK_POS 5
116#define NAND_LARGE_BADBLOCK_POS 0
117#define ONENAND_BADBLOCK_POS 0
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900118
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900119/*
120 * Bad block scanning errors
121 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200122#define ONENAND_BBT_READ_ERROR 1
123#define ONENAND_BBT_READ_ECC_ERROR 2
124#define ONENAND_BBT_READ_FATAL_ERROR 4
Kyungmin Park5d7a01c2008-08-19 08:42:53 +0900125
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900126/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200127 * struct bbm_info - [GENERIC] Bad Block Table data structure
128 * @bbt_erase_shift: [INTERN] number of address bits in a bbt entry
129 * @badblockpos: [INTERN] position of the bad block marker in the oob area
130 * @options: options for this descriptor
131 * @bbt: [INTERN] bad block table pointer
132 * @isbad_bbt: function to determine if a block is bad
133 * @badblock_pattern: [REPLACEABLE] bad block scan pattern used for
134 * initial bad block scan
135 * @priv: [OPTIONAL] pointer to private bbm date
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900136 */
137struct bbm_info {
138 int bbt_erase_shift;
139 int badblockpos;
140 int options;
141
142 uint8_t *bbt;
143
Heiko Schocherf5895d12014-06-24 10:10:04 +0200144 int (*isbad_bbt)(struct mtd_info *mtd, loff_t ofs, int allowbbt);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900145
146 /* TODO Add more NAND specific fileds */
147 struct nand_bbt_descr *badblock_pattern;
148
149 void *priv;
150};
151
152/* OneNAND BBT interface */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200153extern int onenand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd);
154extern int onenand_default_bbt(struct mtd_info *mtd);
Kyungmin Parkaf3d11c2007-09-10 17:15:14 +0900155
Heiko Schocherf5895d12014-06-24 10:10:04 +0200156#endif /* __LINUX_MTD_BBM_H */