1fd534e9bSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */ 287590e26SKyungmin Park /* 387590e26SKyungmin Park * NAND family Bad Block Management (BBM) header file 487590e26SKyungmin Park * - Bad Block Table (BBT) implementation 587590e26SKyungmin Park * 6a1452a37SDavid Woodhouse * Copyright © 2005 Samsung Electronics 787590e26SKyungmin Park * Kyungmin Park <[email protected]> 887590e26SKyungmin Park * 9a1452a37SDavid Woodhouse * Copyright © 2000-2005 1087590e26SKyungmin Park * Thomas Gleixner <[email protected]> 1187590e26SKyungmin Park */ 1287590e26SKyungmin Park #ifndef __LINUX_MTD_BBM_H 1387590e26SKyungmin Park #define __LINUX_MTD_BBM_H 1487590e26SKyungmin Park 1587590e26SKyungmin Park /* The maximum number of NAND chips in an array */ 1687590e26SKyungmin Park #define NAND_MAX_CHIPS 8 1787590e26SKyungmin Park 1887590e26SKyungmin Park /** 1987590e26SKyungmin Park * struct nand_bbt_descr - bad block table descriptor 20ea9b6dccSRandy Dunlap * @options: options for this descriptor 21c62d81bcSAlessandro Rubini * @pages: the page(s) where we find the bbt, used with option BBT_ABSPAGE 22c62d81bcSAlessandro Rubini * when bbt is searched, then we store the found bbts pages here. 2387590e26SKyungmin Park * Its an array and supports up to 8 chips now 24ea9b6dccSRandy Dunlap * @offs: offset of the pattern in the oob area of the page 25c62d81bcSAlessandro Rubini * @veroffs: offset of the bbt version counter in the oob are of the page 26ea9b6dccSRandy Dunlap * @version: version read from the bbt page during scan 27ea9b6dccSRandy Dunlap * @len: length of the pattern, if 0 no pattern check is performed 28c62d81bcSAlessandro Rubini * @maxblocks: maximum number of blocks to search for a bbt. This number of 29c62d81bcSAlessandro Rubini * blocks is reserved at the end of the device where the tables are 30c62d81bcSAlessandro Rubini * written. 31c62d81bcSAlessandro Rubini * @reserved_block_code: if non-0, this pattern denotes a reserved (rather than 32c62d81bcSAlessandro Rubini * bad) block in the stored bbt 33c62d81bcSAlessandro Rubini * @pattern: pattern to identify bad block table or factory marked good / 34c62d81bcSAlessandro Rubini * bad blocks, can be NULL, if len = 0 3587590e26SKyungmin Park * 3687590e26SKyungmin Park * Descriptor for the bad block table marker and the descriptor for the 3787590e26SKyungmin Park * pattern which identifies good and bad blocks. The assumption is made 3887590e26SKyungmin Park * that the pattern and the version count are always located in the oob area 3987590e26SKyungmin Park * of the first block. 4087590e26SKyungmin Park */ 4187590e26SKyungmin Park struct nand_bbt_descr { 4287590e26SKyungmin Park int options; 4387590e26SKyungmin Park int pages[NAND_MAX_CHIPS]; 4487590e26SKyungmin Park int offs; 4587590e26SKyungmin Park int veroffs; 4687590e26SKyungmin Park uint8_t version[NAND_MAX_CHIPS]; 4787590e26SKyungmin Park int len; 4887590e26SKyungmin Park int maxblocks; 4987590e26SKyungmin Park int reserved_block_code; 5087590e26SKyungmin Park uint8_t *pattern; 5187590e26SKyungmin Park }; 5287590e26SKyungmin Park 5387590e26SKyungmin Park /* Options for the bad block table descriptors */ 5487590e26SKyungmin Park 5587590e26SKyungmin Park /* The number of bits used per block in the bbt on the device */ 5687590e26SKyungmin Park #define NAND_BBT_NRBITS_MSK 0x0000000F 5787590e26SKyungmin Park #define NAND_BBT_1BIT 0x00000001 5887590e26SKyungmin Park #define NAND_BBT_2BIT 0x00000002 5987590e26SKyungmin Park #define NAND_BBT_4BIT 0x00000004 6087590e26SKyungmin Park #define NAND_BBT_8BIT 0x00000008 6187590e26SKyungmin Park /* The bad block table is in the last good block of the device */ 6287590e26SKyungmin Park #define NAND_BBT_LASTBLOCK 0x00000010 6387590e26SKyungmin Park /* The bbt is at the given page, else we must scan for the bbt */ 6487590e26SKyungmin Park #define NAND_BBT_ABSPAGE 0x00000020 6587590e26SKyungmin Park /* bbt is stored per chip on multichip devices */ 6687590e26SKyungmin Park #define NAND_BBT_PERCHIP 0x00000080 6787590e26SKyungmin Park /* bbt has a version counter at offset veroffs */ 6887590e26SKyungmin Park #define NAND_BBT_VERSION 0x00000100 69453281a9SSebastian Andrzej Siewior /* Create a bbt if none exists */ 7087590e26SKyungmin Park #define NAND_BBT_CREATE 0x00000200 71b8f80684SBrian Norris /* 72b8f80684SBrian Norris * Create an empty BBT with no vendor information. Vendor's information may be 73b8f80684SBrian Norris * unavailable, for example, if the NAND controller has a different data and OOB 74b8f80684SBrian Norris * layout or if this information is already purged. Must be used in conjunction 75b8f80684SBrian Norris * with NAND_BBT_CREATE. 76b8f80684SBrian Norris */ 77b4dc53e1SBrian Norris #define NAND_BBT_CREATE_EMPTY 0x00000400 7887590e26SKyungmin Park /* Write bbt if neccecary */ 79b4dc53e1SBrian Norris #define NAND_BBT_WRITE 0x00002000 8087590e26SKyungmin Park /* Read and write back block contents when writing bbt */ 81b4dc53e1SBrian Norris #define NAND_BBT_SAVECONTENT 0x00004000 82c902467cSFrieder Schrempf 83a40f7341SBrian Norris /* 84a40f7341SBrian Norris * Use a flash based bad block table. By default, OOB identifier is saved in 85a40f7341SBrian Norris * OOB area. This option is passed to the default bad block table function. 86a40f7341SBrian Norris */ 87b4dc53e1SBrian Norris #define NAND_BBT_USE_FLASH 0x00020000 88f3bae3dfSShmulik Ladkani /* 89f3bae3dfSShmulik Ladkani * Do not store flash based bad block table marker in the OOB area; store it 90f3bae3dfSShmulik Ladkani * in-band. 91f3bae3dfSShmulik Ladkani */ 92b4dc53e1SBrian Norris #define NAND_BBT_NO_OOB 0x00040000 93e2414f4cSBrian Norris /* 94e2414f4cSBrian Norris * Do not write new bad block markers to OOB; useful, e.g., when ECC covers 95e2414f4cSBrian Norris * entire spare area. Must be used with NAND_BBT_USE_FLASH. 96e2414f4cSBrian Norris */ 97e2414f4cSBrian Norris #define NAND_BBT_NO_OOB_BBM 0x00080000 98b4dc53e1SBrian Norris 999eeff824SBrian Norris /* 1009eeff824SBrian Norris * Flag set by nand_create_default_bbt_descr(), marking that the nand_bbt_descr 101*9630a055SMiquel Raynal * was allocated dynamicaly and must be freed in nand_cleanup(). Has no meaning 1029eeff824SBrian Norris * in nand_chip.bbt_options. 1039eeff824SBrian Norris */ 104b4dc53e1SBrian Norris #define NAND_BBT_DYNAMICSTRUCT 0x80000000 10587590e26SKyungmin Park 10687590e26SKyungmin Park /* The maximum number of blocks to scan for a bbt */ 10787590e26SKyungmin Park #define NAND_BBT_SCAN_MAXBLOCKS 4 10887590e26SKyungmin Park 10987590e26SKyungmin Park /* 110211ac75fSKyungmin Park * Bad block scanning errors 111211ac75fSKyungmin Park */ 112211ac75fSKyungmin Park #define ONENAND_BBT_READ_ERROR 1 113211ac75fSKyungmin Park #define ONENAND_BBT_READ_ECC_ERROR 2 114211ac75fSKyungmin Park #define ONENAND_BBT_READ_FATAL_ERROR 4 115211ac75fSKyungmin Park 11687590e26SKyungmin Park /** 117ea9b6dccSRandy Dunlap * struct bbm_info - [GENERIC] Bad Block Table data structure 118ea9b6dccSRandy Dunlap * @bbt_erase_shift: [INTERN] number of address bits in a bbt entry 119ea9b6dccSRandy Dunlap * @options: options for this descriptor 120ea9b6dccSRandy Dunlap * @bbt: [INTERN] bad block table pointer 121ea9b6dccSRandy Dunlap * @isbad_bbt: function to determine if a block is bad 122ea9b6dccSRandy Dunlap * @badblock_pattern: [REPLACEABLE] bad block scan pattern used for 123ea9b6dccSRandy Dunlap * initial bad block scan 124ea9b6dccSRandy Dunlap * @priv: [OPTIONAL] pointer to private bbm date 12587590e26SKyungmin Park */ 12687590e26SKyungmin Park struct bbm_info { 12787590e26SKyungmin Park int bbt_erase_shift; 12887590e26SKyungmin Park int options; 12987590e26SKyungmin Park 13087590e26SKyungmin Park uint8_t *bbt; 13187590e26SKyungmin Park 13287590e26SKyungmin Park int (*isbad_bbt)(struct mtd_info *mtd, loff_t ofs, int allowbbt); 13387590e26SKyungmin Park 13487590e26SKyungmin Park /* TODO Add more NAND specific fileds */ 13587590e26SKyungmin Park struct nand_bbt_descr *badblock_pattern; 13687590e26SKyungmin Park 13787590e26SKyungmin Park void *priv; 13887590e26SKyungmin Park }; 13987590e26SKyungmin Park 14087590e26SKyungmin Park /* OneNAND BBT interface */ 14187590e26SKyungmin Park extern int onenand_default_bbt(struct mtd_info *mtd); 14287590e26SKyungmin Park 14387590e26SKyungmin Park #endif /* __LINUX_MTD_BBM_H */ 144