Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 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 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 7 | * Copyright © 2005 Samsung Electronics |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 8 | * Kyungmin Park <kyungmin.park@samsung.com> |
| 9 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 10 | * Copyright © 2000-2005 |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 11 | * Thomas Gleixner <tglx@linuxtronix.de> |
| 12 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 13 | * SPDX-License-Identifier: GPL-2.0+ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 14 | * |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 15 | */ |
| 16 | #ifndef __LINUX_MTD_BBM_H |
| 17 | #define __LINUX_MTD_BBM_H |
| 18 | |
| 19 | /* The maximum number of NAND chips in an array */ |
Wolfgang Grandegger | 0302550 | 2009-01-16 18:55:54 +0100 | [diff] [blame] | 20 | #ifndef CONFIG_SYS_NAND_MAX_CHIPS |
| 21 | #define CONFIG_SYS_NAND_MAX_CHIPS 1 |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 22 | #endif |
| 23 | |
| 24 | /** |
| 25 | * struct nand_bbt_descr - bad block table descriptor |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 26 | * @options: options for this descriptor |
| 27 | * @pages: the page(s) where we find the bbt, used with option BBT_ABSPAGE |
| 28 | * when bbt is searched, then we store the found bbts pages here. |
| 29 | * Its an array and supports up to 8 chips now |
| 30 | * @offs: offset of the pattern in the oob area of the page |
| 31 | * @veroffs: offset of the bbt version counter in the oob are of the page |
| 32 | * @version: version read from the bbt page during scan |
| 33 | * @len: length of the pattern, if 0 no pattern check is performed |
| 34 | * @maxblocks: maximum number of blocks to search for a bbt. This number of |
| 35 | * blocks is reserved at the end of the device where the tables are |
| 36 | * written. |
| 37 | * @reserved_block_code: if non-0, this pattern denotes a reserved (rather than |
| 38 | * bad) block in the stored bbt |
| 39 | * @pattern: pattern to identify bad block table or factory marked good / |
| 40 | * bad blocks, can be NULL, if len = 0 |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 41 | * |
| 42 | * Descriptor for the bad block table marker and the descriptor for the |
| 43 | * pattern which identifies good and bad blocks. The assumption is made |
| 44 | * that the pattern and the version count are always located in the oob area |
| 45 | * of the first block. |
| 46 | */ |
| 47 | struct nand_bbt_descr { |
| 48 | int options; |
Wolfgang Grandegger | 0302550 | 2009-01-16 18:55:54 +0100 | [diff] [blame] | 49 | int pages[CONFIG_SYS_NAND_MAX_CHIPS]; |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 50 | int offs; |
| 51 | int veroffs; |
Wolfgang Grandegger | 0302550 | 2009-01-16 18:55:54 +0100 | [diff] [blame] | 52 | uint8_t version[CONFIG_SYS_NAND_MAX_CHIPS]; |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 53 | int len; |
| 54 | int maxblocks; |
| 55 | int reserved_block_code; |
| 56 | uint8_t *pattern; |
| 57 | }; |
| 58 | |
| 59 | /* Options for the bad block table descriptors */ |
| 60 | |
| 61 | /* The number of bits used per block in the bbt on the device */ |
| 62 | #define NAND_BBT_NRBITS_MSK 0x0000000F |
| 63 | #define NAND_BBT_1BIT 0x00000001 |
| 64 | #define NAND_BBT_2BIT 0x00000002 |
| 65 | #define NAND_BBT_4BIT 0x00000004 |
| 66 | #define NAND_BBT_8BIT 0x00000008 |
| 67 | /* The bad block table is in the last good block of the device */ |
| 68 | #define NAND_BBT_LASTBLOCK 0x00000010 |
| 69 | /* The bbt is at the given page, else we must scan for the bbt */ |
| 70 | #define NAND_BBT_ABSPAGE 0x00000020 |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 71 | /* bbt is stored per chip on multichip devices */ |
| 72 | #define NAND_BBT_PERCHIP 0x00000080 |
| 73 | /* bbt has a version counter at offset veroffs */ |
| 74 | #define NAND_BBT_VERSION 0x00000100 |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 75 | /* Create a bbt if none exists */ |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 76 | #define NAND_BBT_CREATE 0x00000200 |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 77 | /* |
| 78 | * Create an empty BBT with no vendor information. Vendor's information may be |
| 79 | * unavailable, for example, if the NAND controller has a different data and OOB |
| 80 | * layout or if this information is already purged. Must be used in conjunction |
| 81 | * with NAND_BBT_CREATE. |
| 82 | */ |
| 83 | #define NAND_BBT_CREATE_EMPTY 0x00000400 |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 84 | /* Write bbt if neccecary */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 85 | #define NAND_BBT_WRITE 0x00002000 |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 86 | /* Read and write back block contents when writing bbt */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 87 | #define NAND_BBT_SAVECONTENT 0x00004000 |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 88 | /* Search good / bad pattern on the first and the second page */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 89 | #define NAND_BBT_SCAN2NDPAGE 0x00008000 |
Christian Hitz | b8a6b37 | 2011-10-12 09:32:02 +0200 | [diff] [blame] | 90 | /* Search good / bad pattern on the last page of the eraseblock */ |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 91 | #define NAND_BBT_SCANLASTPAGE 0x00010000 |
| 92 | /* |
| 93 | * Use a flash based bad block table. By default, OOB identifier is saved in |
| 94 | * OOB area. This option is passed to the default bad block table function. |
| 95 | */ |
| 96 | #define NAND_BBT_USE_FLASH 0x00020000 |
| 97 | /* |
| 98 | * Do not store flash based bad block table marker in the OOB area; store it |
| 99 | * in-band. |
| 100 | */ |
| 101 | #define NAND_BBT_NO_OOB 0x00040000 |
| 102 | /* |
| 103 | * Do not write new bad block markers to OOB; useful, e.g., when ECC covers |
| 104 | * entire spare area. Must be used with NAND_BBT_USE_FLASH. |
| 105 | */ |
| 106 | #define NAND_BBT_NO_OOB_BBM 0x00080000 |
| 107 | |
| 108 | /* |
| 109 | * Flag set by nand_create_default_bbt_descr(), marking that the nand_bbt_descr |
| 110 | * was allocated dynamicaly and must be freed in nand_release(). Has no meaning |
| 111 | * in nand_chip.bbt_options. |
| 112 | */ |
| 113 | #define NAND_BBT_DYNAMICSTRUCT 0x80000000 |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 114 | |
| 115 | /* The maximum number of blocks to scan for a bbt */ |
| 116 | #define NAND_BBT_SCAN_MAXBLOCKS 4 |
| 117 | |
| 118 | /* |
| 119 | * Constants for oob configuration |
| 120 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 121 | #define NAND_SMALL_BADBLOCK_POS 5 |
| 122 | #define NAND_LARGE_BADBLOCK_POS 0 |
| 123 | #define ONENAND_BADBLOCK_POS 0 |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 124 | |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 125 | /* |
| 126 | * Bad block scanning errors |
| 127 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 128 | #define ONENAND_BBT_READ_ERROR 1 |
| 129 | #define ONENAND_BBT_READ_ECC_ERROR 2 |
| 130 | #define ONENAND_BBT_READ_FATAL_ERROR 4 |
Kyungmin Park | 5d7a01c | 2008-08-19 08:42:53 +0900 | [diff] [blame] | 131 | |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 132 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 133 | * struct bbm_info - [GENERIC] Bad Block Table data structure |
| 134 | * @bbt_erase_shift: [INTERN] number of address bits in a bbt entry |
| 135 | * @badblockpos: [INTERN] position of the bad block marker in the oob area |
| 136 | * @options: options for this descriptor |
| 137 | * @bbt: [INTERN] bad block table pointer |
| 138 | * @isbad_bbt: function to determine if a block is bad |
| 139 | * @badblock_pattern: [REPLACEABLE] bad block scan pattern used for |
| 140 | * initial bad block scan |
| 141 | * @priv: [OPTIONAL] pointer to private bbm date |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 142 | */ |
| 143 | struct bbm_info { |
| 144 | int bbt_erase_shift; |
| 145 | int badblockpos; |
| 146 | int options; |
| 147 | |
| 148 | uint8_t *bbt; |
| 149 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 150 | int (*isbad_bbt)(struct mtd_info *mtd, loff_t ofs, int allowbbt); |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 151 | |
| 152 | /* TODO Add more NAND specific fileds */ |
| 153 | struct nand_bbt_descr *badblock_pattern; |
| 154 | |
| 155 | void *priv; |
| 156 | }; |
| 157 | |
| 158 | /* OneNAND BBT interface */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 159 | extern int onenand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd); |
| 160 | extern int onenand_default_bbt(struct mtd_info *mtd); |
Kyungmin Park | af3d11c | 2007-09-10 17:15:14 +0900 | [diff] [blame] | 161 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 162 | #endif /* __LINUX_MTD_BBM_H */ |