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