1 /******************************************************************************
3 * Copyright (C) 2015 Xilinx, Inc. All rights reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * Use of the Software is limited solely to applications:
16 * (a) running on a Xilinx device, or
17 * (b) that interact with a Xilinx device through a bus or interconnect.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * Except as contained in this notice, the name of the Xilinx shall not be used
28 * in advertising or otherwise to promote the sale, use or other dealings in
29 * this Software without prior written authorization from Xilinx.
31 ******************************************************************************/
32 /*****************************************************************************/
35 * @file xnandpsu_bbm.h
37 * This file implements the Bad Block Management(BBM) functionality. This is
38 * similar to the Bad Block Management which is a part of the MTD subsystem in
39 * Linux. The factory marked bad blocks are scanned initially and a Bad Block
40 * Table(BBT) is created in the memory. This table is also written to the flash
41 * so that upon reboot, the BBT is read back from the flash and loaded into the
42 * memory instead of scanning every time. The Bad Block Table(BBT) is written
43 * into one of the the last four blocks in the flash memory. The last four
44 * blocks are marked as Reserved so that user can't erase/program those blocks.
46 * There are two bad block tables, a primary table and a mirror table. The
47 * tables are versioned and incrementing version number is used to detect and
48 * recover from interrupted updates. Each table is stored in a separate block,
49 * beginning in the first page of that block. Only two blocks would be necessary
50 * in the absence of bad blocks within the last four; the range of four provides
51 * a little slack in case one or two of those blocks is bad. These blocks are
52 * marked as reserved and cannot be programmed by the user. A NAND Flash device
53 * with 3 or more factory bad blocks in the last 4 cannot be used. The bad block
54 * table signature is written into the spare data area of the pages containing
55 * bad block table so that upon rebooting the bad block table signature is
56 * searched and the bad block table is loaded into RAM. The signature is "Bbt0"
57 * for primary Bad Block Table and "1tbB" for Mirror Bad Block Table. The
58 * version offset follows the signature offset in the spare data area. The
59 * version number increments on every update to the bad block table and the
60 * version wraps at 0xff.
62 * Each block in the Bad Block Table(BBT) is represented by 2 bits.
63 * The two bits are encoded as follows in RAM BBT.
65 * 0'b01 -> Block is bad due to wear
66 * 0'b10 -> Reserved block
67 * 0'b11 -> Factory marked bad block
69 * While writing to the flash the two bits are encoded as follows.
70 * 0'b00 -> Factory marked bad block
71 * 0'b01 -> Reserved block
72 * 0'b10 -> Block is bad due to wear
75 * The user can check for the validity of the block using the API
76 * XNandPsu_IsBlockBad and take the action based on the return value. Also user
77 * can update the bad block table using XNandPsu_MarkBlockBad API.
82 * MODIFICATION HISTORY:
84 * Ver Who Date Changes
85 * ----- ---- ---------- -----------------------------------------------
86 * 1.0 nm 05/06/2014 First release
87 * 2.0 sb 01/12/2015 Added support for writing BBT signature and version
88 * in page section by enabling XNANDPSU_BBT_NO_OOB.
89 * Modified Bbt Signature and Version Offset value for
90 * Oob and No-Oob region.
93 ******************************************************************************/
94 #ifndef XNANDPSU_BBM_H /* prevent circular inclusions */
95 #define XNANDPSU_BBM_H /* by using protection macros */
101 /***************************** Include Files *********************************/
102 #include "xnandpsu.h"
104 /************************** Constant Definitions *****************************/
106 * Block definitions for RAM based Bad Block Table (BBT)
108 #define XNANDPSU_BLOCK_GOOD 0x0U /**< Block is good */
109 #define XNANDPSU_BLOCK_BAD 0x1U /**< Block is bad */
110 #define XNANDPSU_BLOCK_RESERVED 0x2U /**< Reserved block */
111 #define XNANDPSU_BLOCK_FACTORY_BAD 0x3U /**< Factory marked bad
114 * Block definitions for FLASH based Bad Block Table (BBT)
116 #define XNANDPSU_FLASH_BLOCK_GOOD 0x3U /**< Block is good */
117 #define XNANDPSU_FLASH_BLOCK_BAD 0x2U /**< Block is bad */
118 #define XNANDPSU_FLASH_BLOCK_RESERVED 0x1U /**< Reserved block */
119 #define XNANDPSU_FLASH_BLOCK_FAC_BAD 0x0U /**< Factory marked bad
122 #define XNANDPSU_BBT_SCAN_2ND_PAGE 0x00000001U /**< Scan the
127 #define XNANDPSU_BBT_DESC_PAGE_OFFSET 0U /**< Page offset of Bad
129 #define XNANDPSU_BBT_DESC_SIG_OFFSET 8U /**< Bad Block Table
131 #define XNANDPSU_BBT_DESC_VER_OFFSET 12U /**< Bad block Table
133 #define XNANDPSU_NO_OOB_BBT_DESC_SIG_OFFSET 0U /**< Bad Block Table
136 #define XNANDPSU_NO_OOB_BBT_DESC_VER_OFFSET 4U /**< Bad block Table
139 #define XNANDPSU_BBT_DESC_SIG_LEN 4U /**< Bad block Table
141 #define XNANDPSU_BBT_DESC_MAX_BLOCKS 64U /**< Bad block Table
144 #define XNANDPSU_BBT_BLOCK_SHIFT 2U /**< Block shift value
145 for a block in BBT */
146 #define XNANDPSU_BBT_ENTRY_NUM_BLOCKS 4U /**< Num of blocks in
148 #define XNANDPSU_BB_PTRN_OFF_SML_PAGE 5U /**< Bad block pattern
150 #define XNANDPSU_BB_PTRN_LEN_SML_PAGE 1U /**< Bad block pattern
152 #define XNANDPSU_BB_PTRN_OFF_LARGE_PAGE 0U /**< Bad block pattern
155 #define XNANDPSU_BB_PTRN_LEN_LARGE_PAGE 2U /**< Bad block pattern
157 #define XNANDPSU_BB_PATTERN 0xFFU /**< Bad block pattern
160 #define XNANDPSU_BLOCK_TYPE_MASK 0x03U /**< Block type mask */
161 #define XNANDPSU_BLOCK_SHIFT_MASK 0x06U /**< Block shift mask
162 for a Bad Block Table
165 #define XNANDPSU_ONDIE_SIG_OFFSET 0x4U
166 #define XNANDPSU_ONDIE_VER_OFFSET 0x14U
168 #define XNANDPSU_BBT_VERSION_LENGTH 1U
169 #define XNANDPSU_BBT_SIG_LENGTH 4U
171 #define XNANDPSU_BBT_BUF_LENGTH ((XNANDPSU_MAX_BLOCKS >> \
172 XNANDPSU_BBT_BLOCK_SHIFT) + \
173 (XNANDPSU_BBT_DESC_SIG_OFFSET + \
174 XNANDPSU_BBT_SIG_LENGTH + \
175 XNANDPSU_BBT_VERSION_LENGTH))
176 /**************************** Type Definitions *******************************/
178 /***************** Macros (Inline Functions) Definitions *********************/
180 /****************************************************************************/
183 * This macro returns the Block shift value corresponding to a Block.
185 * @param Block is the block number.
187 * @return Block shift value
191 *****************************************************************************/
192 #define XNandPsu_BbtBlockShift(Block) \
193 ((u8)(((Block) * 2U) & XNANDPSU_BLOCK_SHIFT_MASK))
195 /************************** Variable Definitions *****************************/
197 /************************** Function Prototypes ******************************/
199 void XNandPsu_InitBbtDesc(XNandPsu *InstancePtr);
201 s32 XNandPsu_ScanBbt(XNandPsu *InstancePtr);
203 s32 XNandPsu_IsBlockBad(XNandPsu *InstancePtr, u32 Block);
205 s32 XNandPsu_MarkBlockBad(XNandPsu *InstancePtr, u32 Block);
211 #endif /* end of protection macro */