Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:42

0001 /******************************************************************************
0002 * Copyright (C) 2015 - 2022 Xilinx, Inc.  All rights reserved.
0003 * SPDX-License-Identifier: MIT
0004 ******************************************************************************/
0005 
0006 /*****************************************************************************/
0007 /**
0008 *
0009 * @file xnandpsu_bbm.h
0010 * @addtogroup Overview
0011 * @{
0012 *
0013 * This file implements the Bad Block Management(BBM) functionality. This is
0014 * similar to the Bad Block Management which is a part of the MTD subsystem in
0015 * Linux.  The factory marked bad blocks are scanned initially and a Bad Block
0016 * Table(BBT) is created in the memory.  This table is also written to the flash
0017 * so that upon reboot, the BBT is read back from the flash and loaded into the
0018 * memory instead of scanning every time. The Bad Block Table(BBT) is written
0019 * into one of the the last four blocks in the flash memory. The last four
0020 * blocks are marked as Reserved so that user can't erase/program those blocks.
0021 *
0022 * There are two bad block tables, a primary table and a mirror table. The
0023 * tables are versioned and incrementing version number is used to detect and
0024 * recover from interrupted updates. Each table is stored in a separate block,
0025 * beginning in the first page of that block. Only two blocks would be necessary
0026 * in the absence of bad blocks within the last four; the range of four provides
0027 * a little slack in case one or two of those blocks is bad. These blocks are
0028 * marked as reserved and cannot be programmed by the user. A NAND Flash device
0029 * with 3 or more factory bad blocks in the last 4 cannot be used. The bad block
0030 * table signature is written into the spare data area of the pages containing
0031 * bad block table so that upon rebooting the bad block table signature is
0032 * searched and the bad block table is loaded into RAM. The signature is "Bbt0"
0033 * for primary Bad Block Table and "1tbB" for Mirror Bad Block Table. The
0034 * version offset follows the signature offset in the spare data area. The
0035 * version number increments on every update to the bad block table and the
0036 * version wraps at 0xff.
0037 *
0038 * Each block in the Bad Block Table(BBT) is represented by 2 bits.
0039 * The two bits are encoded as follows in RAM BBT.
0040 * 0'b00 -> Good Block
0041 * 0'b01 -> Block is bad due to wear
0042 * 0'b10 -> Reserved block
0043 * 0'b11 -> Factory marked bad block
0044 *
0045 * While writing to the flash the two bits are encoded as follows.
0046 * 0'b00 -> Factory marked bad block
0047 * 0'b01 -> Reserved block
0048 * 0'b10 -> Block is bad due to wear
0049 * 0'b11 -> Good Block
0050 *
0051 * The user can check for the validity of the block using the API
0052 * XNandPsu_IsBlockBad and take the action based on the return value. Also user
0053 * can update the bad block table using XNandPsu_MarkBlockBad API.
0054 *
0055 * @note     None
0056 *
0057 * <pre>
0058 * MODIFICATION HISTORY:
0059 *
0060 * Ver   Who    Date        Changes
0061 * ----- ----   ----------  -----------------------------------------------
0062 * 1.0   nm     05/06/2014  First release
0063 * 2.0   sb     01/12/2015  Added support for writing BBT signature and version
0064 *              in page section by enabling XNANDPSU_BBT_NO_OOB.
0065 *              Modified Bbt Signature and Version Offset value for
0066 *              Oob and No-Oob region.
0067 * </pre>
0068 *
0069 ******************************************************************************/
0070 #ifndef XNANDPSU_BBM_H      /* prevent circular inclusions */
0071 #define XNANDPSU_BBM_H      /* by using protection macros */
0072 
0073 #ifdef __cplusplus
0074 extern "C" {
0075 #endif
0076 
0077 /***************************** Include Files *********************************/
0078 #include "xnandpsu.h"
0079 
0080 /************************** Constant Definitions *****************************/
0081 /* Block definitions for RAM based Bad Block Table (BBT) */
0082 #define XNANDPSU_BLOCK_GOOD         0x0U    /**< Block is good */
0083 #define XNANDPSU_BLOCK_BAD          0x1U    /**< Block is bad */
0084 #define XNANDPSU_BLOCK_RESERVED         0x2U    /**< Reserved block */
0085 #define XNANDPSU_BLOCK_FACTORY_BAD      0x3U    /**< Factory marked bad
0086                               block */
0087 /* Block definitions for FLASH based Bad Block Table (BBT) */
0088 #define XNANDPSU_FLASH_BLOCK_GOOD       0x3U    /**< Block is good */
0089 #define XNANDPSU_FLASH_BLOCK_BAD        0x2U    /**< Block is bad */
0090 #define XNANDPSU_FLASH_BLOCK_RESERVED       0x1U    /**< Reserved block */
0091 #define XNANDPSU_FLASH_BLOCK_FAC_BAD    0x0U    /**< Factory marked bad
0092                               block */
0093 
0094 #define XNANDPSU_BBT_SCAN_2ND_PAGE      0x00000001U /**< Scan the
0095                                   second page
0096                                   for bad block
0097                                   information
0098                                   */
0099 #define XNANDPSU_BBT_DESC_PAGE_OFFSET       0U  /**< Page offset of Bad
0100                               Block Table Desc */
0101 #define XNANDPSU_BBT_DESC_SIG_OFFSET        8U  /**< Bad Block Table
0102                               signature offset */
0103 #define XNANDPSU_BBT_DESC_VER_OFFSET        12U /**< Bad block Table
0104                               version offset */
0105 #define XNANDPSU_NO_OOB_BBT_DESC_SIG_OFFSET 0U  /**< Bad Block Table
0106                               signature offset in
0107                               page memory */
0108 #define XNANDPSU_NO_OOB_BBT_DESC_VER_OFFSET 4U  /**< Bad block Table
0109                               version offset in
0110                               page memory */
0111 #define XNANDPSU_BBT_DESC_SIG_LEN       4U  /**< Bad block Table
0112                               signature length */
0113 #define XNANDPSU_BBT_DESC_MAX_BLOCKS        64U /**< Bad block Table
0114                               max blocks */
0115 
0116 #define XNANDPSU_BBT_BLOCK_SHIFT        2U  /**< Block shift value
0117                               for a block in BBT */
0118 #define XNANDPSU_BBT_ENTRY_NUM_BLOCKS       4U  /**< Num of blocks in
0119                               one BBT entry */
0120 #define XNANDPSU_BB_PTRN_OFF_SML_PAGE   5U  /**< Bad block pattern
0121                               offset in a page */
0122 #define XNANDPSU_BB_PTRN_LEN_SML_PAGE   1U  /**< Bad block pattern
0123                               length */
0124 #define XNANDPSU_BB_PTRN_OFF_LARGE_PAGE 0U  /**< Bad block pattern
0125                               offset in a large
0126                               page */
0127 #define XNANDPSU_BB_PTRN_LEN_LARGE_PAGE 2U  /**< Bad block pattern
0128                               length */
0129 #define XNANDPSU_BB_PATTERN         0xFFU   /**< Bad block pattern
0130                               to search in a page
0131                               */
0132 #define XNANDPSU_BLOCK_TYPE_MASK        0x03U   /**< Block type mask */
0133 #define XNANDPSU_BLOCK_SHIFT_MASK       0x06U   /**< Block shift mask
0134                               for a Bad Block Table
0135                               entry byte */
0136 
0137 #define XNANDPSU_ONDIE_SIG_OFFSET       0x4U
0138 #define XNANDPSU_ONDIE_VER_OFFSET       0x14U
0139 
0140 #define XNANDPSU_BBT_VERSION_LENGTH 1U
0141 #define XNANDPSU_BBT_SIG_LENGTH     4U
0142 
0143 #define XNANDPSU_BBT_BUF_LENGTH     ((XNANDPSU_MAX_BLOCKS >>        \
0144                      XNANDPSU_BBT_BLOCK_SHIFT) +    \
0145                     (XNANDPSU_BBT_DESC_SIG_OFFSET + \
0146                      XNANDPSU_BBT_SIG_LENGTH +  \
0147                      XNANDPSU_BBT_VERSION_LENGTH))
0148 /**************************** Type Definitions *******************************/
0149 
0150 /***************** Macros (Inline Functions) Definitions *********************/
0151 
0152 /****************************************************************************/
0153 /**
0154 *
0155 * This macro returns the Block shift value corresponding to a Block.
0156 *
0157 * @param        Block is the block number.
0158 *
0159 * @return       Block shift value
0160 *
0161 * @note         None.
0162 *
0163 *****************************************************************************/
0164 #define XNandPsu_BbtBlockShift(Block) \
0165             (u8)(((Block) * 2U) & XNANDPSU_BLOCK_SHIFT_MASK)
0166 
0167 /************************** Variable Definitions *****************************/
0168 
0169 /************************** Function Prototypes ******************************/
0170 
0171 void XNandPsu_InitBbtDesc(XNandPsu *InstancePtr);
0172 
0173 s32 XNandPsu_IsBlockBad(XNandPsu *InstancePtr, u32 Block);
0174 
0175 #ifdef __cplusplus
0176 }
0177 #endif
0178 
0179 #endif /* end of protection macro */
0180 /** @} */