// SPDX-License-Identifier: GPL-2.0-only /* * Overview: * Bad block table support for the NAND driver * * Copyright © 2004 Thomas Gleixner ([email protected]) * * Description: * * When nand_scan_bbt is called, then it tries to find the bad block table * depending on the options in the BBT descriptor(s). If no flash based BBT * (NAND_BBT_USE_FLASH) is specified then the device is scanned for factory * marked good / bad blocks. This information is used to create a memory BBT. * Once a new bad block is discovered then the "factory" information is updated * on the device. * If a flash based BBT is specified then the function first tries to find the * BBT on flash. If a BBT is found then the contents are read and the memory * based BBT is created. If a mirrored BBT is selected then the mirror is * searched too and the versions are compared. If the mirror has a greater * version number, then the mirror BBT is used to build the memory based BBT. * If the tables are not versioned, then we "or" the bad block information. * If one of the BBTs is out of date or does not exist it is (re)created. * If no BBT exists at all then the device is scanned for factory marked * good / bad blocks and the bad block tables are created. * * For manufacturer created BBTs like the one found on M-SYS DOC devices * the BBT is searched and read but never created * * The auto generated bad block table is located in the last good blocks * of the device. The table is mirrored, so it can be updated eventually. * The table is marked in the OOB area with an ident pattern and a version * number which indicates which of both tables is more up to date. If the NAND * controller needs the complete OOB area for the ECC information then the * option NAND_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of * course): it moves the ident pattern and the version byte into the data area * and the OOB area will remain untouched. * * The table uses 2 bits per block * 11b: block is good * 00b: block is factory marked bad * 01b, 10b: block is marked bad due to wear * * The memory bad block table uses the following scheme: * 00b: block is good * 01b: block is marked bad due to wear * 10b: block is reserved (to protect the bbt area) * 11b: block is factory marked bad * * Multichip devices like DOC store the bad block info per floor. * * Following assumptions are made: * - bbts start at a page boundary, if autolocated on a block boundary * - the space necessary for a bbt in FLASH does not exceed a block boundary */ #include <linux/slab.h> #include <linux/types.h> #include <linux/mtd/mtd.h> #include <linux/mtd/bbm.h> #include <linux/bitops.h> #include <linux/delay.h> #include <linux/vmalloc.h> #include <linux/export.h> #include <linux/string.h> #include "internals.h" #define BBT_BLOCK_GOOD … #define BBT_BLOCK_WORN … #define BBT_BLOCK_RESERVED … #define BBT_BLOCK_FACTORY_BAD … #define BBT_ENTRY_MASK … #define BBT_ENTRY_SHIFT … static inline uint8_t bbt_get_entry(struct nand_chip *chip, int block) { … } static inline void bbt_mark_entry(struct nand_chip *chip, int block, uint8_t mark) { … } static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td) { … } /** * check_pattern - [GENERIC] check if a pattern is in the buffer * @buf: the buffer to search * @len: the length of buffer to search * @paglen: the pagelength * @td: search pattern descriptor * * Check for a pattern at the given place. Used to search bad block tables and * good / bad block identifiers. */ static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td) { … } /** * check_short_pattern - [GENERIC] check if a pattern is in the buffer * @buf: the buffer to search * @td: search pattern descriptor * * Check for a pattern at the given place. Used to search bad block tables and * good / bad block identifiers. Same as check_pattern, but no optional empty * check. */ static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td) { … } /** * add_marker_len - compute the length of the marker in data area * @td: BBT descriptor used for computation * * The length will be 0 if the marker is located in OOB area. */ static u32 add_marker_len(struct nand_bbt_descr *td) { … } /** * read_bbt - [GENERIC] Read the bad block table starting from page * @this: NAND chip object * @buf: temporary buffer * @page: the starting page * @num: the number of bbt descriptors to read * @td: the bbt describtion table * @offs: block number offset in the table * * Read the bad block table starting from page. */ static int read_bbt(struct nand_chip *this, uint8_t *buf, int page, int num, struct nand_bbt_descr *td, int offs) { … } /** * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page * @this: NAND chip object * @buf: temporary buffer * @td: descriptor for the bad block table * @chip: read the table for a specific chip, -1 read all chips; applies only if * NAND_BBT_PERCHIP option is set * * Read the bad block table for all chips starting at a given page. We assume * that the bbt bits are in consecutive order. */ static int read_abs_bbt(struct nand_chip *this, uint8_t *buf, struct nand_bbt_descr *td, int chip) { … } /* BBT marker is in the first page, no OOB */ static int scan_read_data(struct nand_chip *this, uint8_t *buf, loff_t offs, struct nand_bbt_descr *td) { … } /** * scan_read_oob - [GENERIC] Scan data+OOB region to buffer * @this: NAND chip object * @buf: temporary buffer * @offs: offset at which to scan * @len: length of data region to read * * Scan read data from data+OOB. May traverse multiple pages, interleaving * page,OOB,page,OOB,... in buf. Completes transfer and returns the "strongest" * ECC condition (error or bitflip). May quit on the first (non-ECC) error. */ static int scan_read_oob(struct nand_chip *this, uint8_t *buf, loff_t offs, size_t len) { … } static int scan_read(struct nand_chip *this, uint8_t *buf, loff_t offs, size_t len, struct nand_bbt_descr *td) { … } /* Scan write data with oob to flash */ static int scan_write_bbt(struct nand_chip *this, loff_t offs, size_t len, uint8_t *buf, uint8_t *oob) { … } static u32 bbt_get_ver_offs(struct nand_chip *this, struct nand_bbt_descr *td) { … } /** * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page * @this: NAND chip object * @buf: temporary buffer * @td: descriptor for the bad block table * @md: descriptor for the bad block table mirror * * Read the bad block table(s) for all chips starting at a given page. We * assume that the bbt bits are in consecutive order. */ static void read_abs_bbts(struct nand_chip *this, uint8_t *buf, struct nand_bbt_descr *td, struct nand_bbt_descr *md) { … } /* Scan a given block partially */ static int scan_block_fast(struct nand_chip *this, struct nand_bbt_descr *bd, loff_t offs, uint8_t *buf) { … } /* Check if a potential BBT block is marked as bad */ static int bbt_block_checkbad(struct nand_chip *this, struct nand_bbt_descr *td, loff_t offs, uint8_t *buf) { … } /** * create_bbt - [GENERIC] Create a bad block table by scanning the device * @this: NAND chip object * @buf: temporary buffer * @bd: descriptor for the good/bad block search pattern * @chip: create the table for a specific chip, -1 read all chips; applies only * if NAND_BBT_PERCHIP option is set * * Create a bad block table by scanning the device for the given good/bad block * identify pattern. */ static int create_bbt(struct nand_chip *this, uint8_t *buf, struct nand_bbt_descr *bd, int chip) { … } /** * search_bbt - [GENERIC] scan the device for a specific bad block table * @this: NAND chip object * @buf: temporary buffer * @td: descriptor for the bad block table * * Read the bad block table by searching for a given ident pattern. Search is * preformed either from the beginning up or from the end of the device * downwards. The search starts always at the start of a block. If the option * NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains * the bad block information of this chip. This is necessary to provide support * for certain DOC devices. * * The bbt ident pattern resides in the oob area of the first page in a block. */ static int search_bbt(struct nand_chip *this, uint8_t *buf, struct nand_bbt_descr *td) { … } /** * search_read_bbts - [GENERIC] scan the device for bad block table(s) * @this: NAND chip object * @buf: temporary buffer * @td: descriptor for the bad block table * @md: descriptor for the bad block table mirror * * Search and read the bad block table(s). */ static void search_read_bbts(struct nand_chip *this, uint8_t *buf, struct nand_bbt_descr *td, struct nand_bbt_descr *md) { … } /** * get_bbt_block - Get the first valid eraseblock suitable to store a BBT * @this: the NAND device * @td: the BBT description * @md: the mirror BBT descriptor * @chip: the CHIP selector * * This functions returns a positive block number pointing a valid eraseblock * suitable to store a BBT (i.e. in the range reserved for BBT), or -ENOSPC if * all blocks are already used of marked bad. If td->pages[chip] was already * pointing to a valid block we re-use it, otherwise we search for the next * valid one. */ static int get_bbt_block(struct nand_chip *this, struct nand_bbt_descr *td, struct nand_bbt_descr *md, int chip) { … } /** * mark_bbt_block_bad - Mark one of the block reserved for BBT bad * @this: the NAND device * @td: the BBT description * @chip: the CHIP selector * @block: the BBT block to mark * * Blocks reserved for BBT can become bad. This functions is an helper to mark * such blocks as bad. It takes care of updating the in-memory BBT, marking the * block as bad using a bad block marker and invalidating the associated * td->pages[] entry. */ static void mark_bbt_block_bad(struct nand_chip *this, struct nand_bbt_descr *td, int chip, int block) { … } /** * write_bbt - [GENERIC] (Re)write the bad block table * @this: NAND chip object * @buf: temporary buffer * @td: descriptor for the bad block table * @md: descriptor for the bad block table mirror * @chipsel: selector for a specific chip, -1 for all * * (Re)write the bad block table. */ static int write_bbt(struct nand_chip *this, uint8_t *buf, struct nand_bbt_descr *td, struct nand_bbt_descr *md, int chipsel) { … } /** * nand_memory_bbt - [GENERIC] create a memory based bad block table * @this: NAND chip object * @bd: descriptor for the good/bad block search pattern * * The function creates a memory based bbt by scanning the device for * manufacturer / software marked good / bad blocks. */ static inline int nand_memory_bbt(struct nand_chip *this, struct nand_bbt_descr *bd) { … } /** * check_create - [GENERIC] create and write bbt(s) if necessary * @this: the NAND device * @buf: temporary buffer * @bd: descriptor for the good/bad block search pattern * * The function checks the results of the previous call to read_bbt and creates * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found * for the chip/device. Update is necessary if one of the tables is missing or * the version nr. of one table is less than the other. */ static int check_create(struct nand_chip *this, uint8_t *buf, struct nand_bbt_descr *bd) { … } /** * nand_update_bbt - update bad block table(s) * @this: the NAND device * @offs: the offset of the newly marked block * * The function updates the bad block table(s). */ static int nand_update_bbt(struct nand_chip *this, loff_t offs) { … } /** * mark_bbt_region - [GENERIC] mark the bad block table regions * @this: the NAND device * @td: bad block table descriptor * * The bad block table regions are marked as "bad" to prevent accidental * erasures / writes. The regions are identified by the mark 0x02. */ static void mark_bbt_region(struct nand_chip *this, struct nand_bbt_descr *td) { … } /** * verify_bbt_descr - verify the bad block description * @this: the NAND device * @bd: the table to verify * * This functions performs a few sanity checks on the bad block description * table. */ static void verify_bbt_descr(struct nand_chip *this, struct nand_bbt_descr *bd) { … } /** * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s) * @this: the NAND device * @bd: descriptor for the good/bad block search pattern * * The function checks, if a bad block table(s) is/are already available. If * not it scans the device for manufacturer marked good / bad blocks and writes * the bad block table(s) to the selected place. * * The bad block table memory is allocated here. It must be freed by calling * the nand_free_bbt function. */ static int nand_scan_bbt(struct nand_chip *this, struct nand_bbt_descr *bd) { … } /* * Define some generic bad / good block scan pattern which are used * while scanning a device for factory marked good / bad blocks. */ static uint8_t scan_ff_pattern[] = …; /* Generic flash bbt descriptors */ static uint8_t bbt_pattern[] = …; static uint8_t mirror_pattern[] = …; static struct nand_bbt_descr bbt_main_descr = …; static struct nand_bbt_descr bbt_mirror_descr = …; static struct nand_bbt_descr bbt_main_no_oob_descr = …; static struct nand_bbt_descr bbt_mirror_no_oob_descr = …; #define BADBLOCK_SCAN_MASK … /** * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure * @this: NAND chip to create descriptor for * * This function allocates and initializes a nand_bbt_descr for BBM detection * based on the properties of @this. The new descriptor is stored in * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when * passed to this function. */ static int nand_create_badblock_pattern(struct nand_chip *this) { … } /** * nand_create_bbt - [NAND Interface] Select a default bad block table for the device * @this: NAND chip object * * This function selects the default bad block table support for the device and * calls the nand_scan_bbt function. */ int nand_create_bbt(struct nand_chip *this) { … } EXPORT_SYMBOL(…); /** * nand_isreserved_bbt - [NAND Interface] Check if a block is reserved * @this: NAND chip object * @offs: offset in the device */ int nand_isreserved_bbt(struct nand_chip *this, loff_t offs) { … } /** * nand_isbad_bbt - [NAND Interface] Check if a block is bad * @this: NAND chip object * @offs: offset in the device * @allowbbt: allow access to bad block table region */ int nand_isbad_bbt(struct nand_chip *this, loff_t offs, int allowbbt) { … } /** * nand_markbad_bbt - [NAND Interface] Mark a block bad in the BBT * @this: NAND chip object * @offs: offset of the bad block */ int nand_markbad_bbt(struct nand_chip *this, loff_t offs) { … }