blob: d890805f549444e73cdbb278724becfd9f1bbd16 [file] [log] [blame]
Thomas Gleixnerfd534e92019-05-23 11:14:39 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Kyungmin Park87590e22005-09-27 11:26:39 +01002/*
Kyungmin Park87590e22005-09-27 11:26:39 +01003 * NAND family Bad Block Management (BBM) header file
4 * - Bad Block Table (BBT) implementation
5 *
David Woodhousea1452a32010-08-08 20:58:20 +01006 * Copyright © 2005 Samsung Electronics
Kyungmin Park87590e22005-09-27 11:26:39 +01007 * Kyungmin Park <kyungmin.park@samsung.com>
8 *
David Woodhousea1452a32010-08-08 20:58:20 +01009 * Copyright © 2000-2005
Kyungmin Park87590e22005-09-27 11:26:39 +010010 * Thomas Gleixner <tglx@linuxtronix.de>
Kyungmin Park87590e22005-09-27 11:26:39 +010011 */
12#ifndef __LINUX_MTD_BBM_H
13#define __LINUX_MTD_BBM_H
14
15/* The maximum number of NAND chips in an array */
16#define NAND_MAX_CHIPS 8
17
18/**
19 * struct nand_bbt_descr - bad block table descriptor
Alessandro Rubinic62d81b2009-09-20 23:28: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 Park87590e22005-09-27 11:26:39 +010035 *
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;
43 int pages[NAND_MAX_CHIPS];
44 int offs;
45 int veroffs;
46 uint8_t version[NAND_MAX_CHIPS];
47 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 Park87590e22005-09-27 11:26:39 +010065/* 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
Sebastian Andrzej Siewior453281a2010-10-01 21:37:37 +020069/* Create a bbt if none exists */
Kyungmin Park87590e22005-09-27 11:26:39 +010070#define NAND_BBT_CREATE 0x00000200
Brian Norrisb8f80682011-05-31 16:31:24 -070071/*
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 */
Brian Norrisb4dc53e2011-05-31 16:31:26 -070077#define NAND_BBT_CREATE_EMPTY 0x00000400
Kyungmin Park87590e22005-09-27 11:26:39 +010078/* Write bbt if neccecary */
Brian Norrisb4dc53e2011-05-31 16:31:26 -070079#define NAND_BBT_WRITE 0x00002000
Kyungmin Park87590e22005-09-27 11:26:39 +010080/* Read and write back block contents when writing bbt */
Brian Norrisb4dc53e2011-05-31 16:31:26 -070081#define NAND_BBT_SAVECONTENT 0x00004000
Frieder Schrempfc9024672019-04-17 12:36:35 +000082
Brian Norrisa40f7342011-05-31 16:31:22 -070083/*
84 * Use a flash based bad block table. By default, OOB identifier is saved in
85 * OOB area. This option is passed to the default bad block table function.
86 */
Brian Norrisb4dc53e2011-05-31 16:31:26 -070087#define NAND_BBT_USE_FLASH 0x00020000
Shmulik Ladkanif3bae3d2012-06-26 17:28:28 +030088/*
89 * Do not store flash based bad block table marker in the OOB area; store it
90 * in-band.
91 */
Brian Norrisb4dc53e2011-05-31 16:31:26 -070092#define NAND_BBT_NO_OOB 0x00040000
Brian Norrise2414f42012-02-06 13:44:00 -080093/*
94 * Do not write new bad block markers to OOB; useful, e.g., when ECC covers
95 * entire spare area. Must be used with NAND_BBT_USE_FLASH.
96 */
97#define NAND_BBT_NO_OOB_BBM 0x00080000
Brian Norrisb4dc53e2011-05-31 16:31:26 -070098
Brian Norris9eeff822011-05-31 16:31:27 -070099/*
100 * Flag set by nand_create_default_bbt_descr(), marking that the nand_bbt_descr
Miquel Raynal9630a052020-05-19 15:00:35 +0200101 * was allocated dynamicaly and must be freed in nand_cleanup(). Has no meaning
Brian Norris9eeff822011-05-31 16:31:27 -0700102 * in nand_chip.bbt_options.
103 */
Brian Norrisb4dc53e2011-05-31 16:31:26 -0700104#define NAND_BBT_DYNAMICSTRUCT 0x80000000
Kyungmin Park87590e22005-09-27 11:26:39 +0100105
106/* The maximum number of blocks to scan for a bbt */
107#define NAND_BBT_SCAN_MAXBLOCKS 4
108
109/*
Kyungmin Park211ac752007-02-07 12:15:01 +0900110 * Bad block scanning errors
111 */
112#define ONENAND_BBT_READ_ERROR 1
113#define ONENAND_BBT_READ_ECC_ERROR 2
114#define ONENAND_BBT_READ_FATAL_ERROR 4
115
Kyungmin Park87590e22005-09-27 11:26:39 +0100116/**
Randy Dunlapea9b6dc2006-06-28 21:48:38 -0700117 * struct bbm_info - [GENERIC] Bad Block Table data structure
118 * @bbt_erase_shift: [INTERN] number of address bits in a bbt entry
Randy Dunlapea9b6dc2006-06-28 21:48:38 -0700119 * @options: options for this descriptor
120 * @bbt: [INTERN] bad block table pointer
121 * @isbad_bbt: function to determine if a block is bad
122 * @badblock_pattern: [REPLACEABLE] bad block scan pattern used for
123 * initial bad block scan
124 * @priv: [OPTIONAL] pointer to private bbm date
Kyungmin Park87590e22005-09-27 11:26:39 +0100125 */
126struct bbm_info {
127 int bbt_erase_shift;
Kyungmin Park87590e22005-09-27 11:26:39 +0100128 int options;
129
130 uint8_t *bbt;
131
132 int (*isbad_bbt)(struct mtd_info *mtd, loff_t ofs, int allowbbt);
133
134 /* TODO Add more NAND specific fileds */
135 struct nand_bbt_descr *badblock_pattern;
136
137 void *priv;
138};
139
140/* OneNAND BBT interface */
Kyungmin Park87590e22005-09-27 11:26:39 +0100141extern int onenand_default_bbt(struct mtd_info *mtd);
142
143#endif /* __LINUX_MTD_BBM_H */