1 /**
2   ******************************************************************************
3   * @file    bl702_snflash.h
4   * @version V1.0
5   * @date
6   * @brief   This file is the standard driver header file
7   ******************************************************************************
8   * @attention
9   *
10   * <h2><center>&copy; COPYRIGHT(c) 2020 Bouffalo Lab</center></h2>
11   *
12   * Redistribution and use in source and binary forms, with or without modification,
13   * are permitted provided that the following conditions are met:
14   *   1. Redistributions of source code must retain the above copyright notice,
15   *      this list of conditions and the following disclaimer.
16   *   2. Redistributions in binary form must reproduce the above copyright notice,
17   *      this list of conditions and the following disclaimer in the documentation
18   *      and/or other materials provided with the distribution.
19   *   3. Neither the name of Bouffalo Lab nor the names of its contributors
20   *      may be used to endorse or promote products derived from this software
21   *      without specific prior written permission.
22   *
23   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
27   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   *
34   ******************************************************************************
35   */
36 #ifndef NANDFLASH_XT26G01A_H
37 #define NANDFLASH_XT26G01A_H
38 
39 #include "bl702_aon.h"
40 #include "bl702_common.h"
41 #include "bl702_glb.h"
42 #include "bl702_hbn.h"
43 #include "bl702_spi.h"
44 #include "bl702_gpio.h"
45 
46 #include "ffconf.h"
47 #include "ff.h"
48 #include "diskio.h"
49 
50 #define FLASH_WRITE_READ_SIZE (2560)
51 #define READ_PRINT_OPEN       (0)
52 
53 #define SPI_PIN_CLK  GLB_GPIO_PIN_11
54 #define SPI_PIN_MOSI GLB_GPIO_PIN_12
55 #define SPI_PIN_MISO GLB_GPIO_PIN_21
56 #define SPI_PIN_CS   GLB_GPIO_PIN_10
57 
58 #define NANDFLASH_WRITE_ENABLE             (0x06)
59 #define NANDFLASH_WRITE_DISABLE            (0x04)
60 #define NANDFLASH_GET_FEATURES             (0x0F)
61 #define NANDFLASH_SET_FEATURES             (0x1F)
62 #define NANDFLASH_PAGE_READ_TO_CACHE       (0x13)
63 #define NANDFLASH_READ_FROM_CACHE          (0x03)
64 #define NANDFLASH_READ_ID                  (0x9F)
65 #define NANDFLASH_PROGRAM_LOAD             (0x02)
66 #define NANDFLASH_PROGRAM_LOAD_RANDOM_DATA (0x84)
67 #define NANDFLASH_PROGRAM_EXECUTE          (0x10)
68 #define NANDFLASH_BLOCK_ERASE              (0xD8)
69 #define NANDFLASH_RESET                    (0xFF)
70 
71 #define NANDFLASH_BLOCK_LOCK_ADDR (0xA0)
72 #define NANDFLASH_FEATURE_ADDR    (0xB0)
73 #define NANDFLASH_STATUS_ADDR     (0xC0)
74 #define NANDFLASH_DUMMY_BYTE      (0x00)
75 #define NANDFLASH_PAGE_SIZE       (2048)
76 #define NANDFLASH_PAGE_TOTAL_SIZE (2048 + 64)
77 #define NANDFLASH_BLOCK_SIZE      (128 * 1024)
78 #define NANDFLASH_PAGES_PER_BLOCK (64)
79 
80 typedef struct
81 {
82     SPI_ID_Type spiNo;
83     SPI_CFG_Type spiCfg;
84     SPI_ClockCfg_Type clockCfg;
85 } NANDFLASH_CFG_Type;
86 
87 extern NANDFLASH_CFG_Type nfCfg;
88 
89 typedef struct
90 {
91     uint8_t Reserved0 : 1;
92     uint8_t CMP       : 1;
93     uint8_t INV       : 1;
94     uint8_t BP0       : 1;
95     uint8_t BP1       : 1;
96     uint8_t BP2       : 1;
97     uint8_t Reserved6 : 1;
98     uint8_t BRWD      : 1;
99 } NANDFLASH_BLOCK_LOCK_Type;
100 
101 typedef struct
102 {
103     uint8_t QE        : 1;
104     uint8_t Reserved1 : 1;
105     uint8_t Reserved2 : 1;
106     uint8_t Reserved3 : 1;
107     uint8_t ECC_EN    : 1;
108     uint8_t Reserved5 : 1;
109     uint8_t OTP_EN    : 1;
110     uint8_t OTP_PRT   : 1;
111 } NANDFLASH_FEATURE_Type;
112 
113 typedef struct
114 {
115     uint8_t OIP         : 1;
116     uint8_t WEL         : 1;
117     uint8_t EFAIL_ECCS0 : 1;
118     uint8_t PFAIL_ECCS1 : 1;
119     uint8_t ECCS2       : 1;
120     uint8_t ECCS3       : 1;
121     uint8_t Reserved6   : 1;
122     uint8_t Reserved7   : 1;
123 } NANDFLASH_STATUS_Type;
124 
125 typedef enum {
126     NO_BIT_ERROR = 0,
127     ONE_BIT_ERROR_DETECTED_AND_CORRECTED = 1,
128     TWO_BIT_ERROR_DETECTED_AND_CORRECTED = 2,
129     THREE_BIT_ERROR_DETECTED_AND_CORRECTED = 3,
130     FOUR_BIT_ERROR_DETECTED_AND_CORRECTED = 4,
131     FIVE_BIT_ERROR_DETECTED_AND_CORRECTED = 5,
132     SIX_BIT_ERROR_DETECTED_AND_CORRECTED = 6,
133     SEVEN_BIT_ERROR_DETECTED_AND_CORRECTED = 7,
134     BIT_ERROR_DETECTED_AND_NOT_CORRECTED = 8,
135     EIGHT_BIT_ERROR_DETECTED_AND_CORRECTED_MAX = 9
136 } NANDFLASH_ECC_Type;
137 
138 void ATTR_TCM_SECTION Nandflash_Psram_Cfg(void);
139 void Nandflash_Init(void);
140 
141 void Nandflash_Gpio_Init(GLB_GPIO_Type clk, GLB_GPIO_Type mosi, GLB_GPIO_Type miso);
142 void Nandflash_CS_High(void);
143 void Nandflash_CS_Low(void);
144 void Nandflash_Clock_Init(uint8_t enable, uint8_t div);
145 void Nandflash_SPI_Init(NANDFLASH_CFG_Type *pNandflashCfg);
146 
147 void Nandflash_WriteEnable(NANDFLASH_CFG_Type *pNandflashCfg);
148 void Nandflash_WriteDisable(NANDFLASH_CFG_Type *pNandflashCfg);
149 void Nandflash_Get_BlockLock(NANDFLASH_CFG_Type *pNandflashCfg, uint8_t *pBlockLock);
150 void Nandflash_Set_BlockLock(NANDFLASH_CFG_Type *pNandflashCfg, uint8_t *pBlockLock);
151 void Nandflash_Get_Feature(NANDFLASH_CFG_Type *pNandflashCfg, uint8_t *pFeature);
152 void Nandflash_Set_Feature(NANDFLASH_CFG_Type *pNandflashCfg, uint8_t *pFeature);
153 void Nandflash_Get_Status(NANDFLASH_CFG_Type *pNandflashCfg, uint8_t *pStatus);
154 void Nandflash_Set_Status(NANDFLASH_CFG_Type *pNandflashCfg, uint8_t *pStatus);
155 
156 NANDFLASH_ECC_Type Nandflash_CheckBadBlock(NANDFLASH_CFG_Type *pNandflashCfg, uint32_t blockNum, uint8_t *buff, uint32_t len);
157 
158 NANDFLASH_ECC_Type Nandflash_PagesRead(NANDFLASH_CFG_Type *pNandflashCfg, uint32_t addr, uint8_t *buff, uint32_t len);
159 NANDFLASH_ECC_Type Nandflash_PageRead(NANDFLASH_CFG_Type *pNandflashCfg, uint32_t addr, uint8_t *buff, uint32_t len);
160 NANDFLASH_ECC_Type Nandflash_PageReadToCache(NANDFLASH_CFG_Type *pNandflashCfg, uint32_t addr);
161 void Nandflash_ReadFromCache(NANDFLASH_CFG_Type *pNandflashCfg, uint16_t offset, uint8_t *buff, uint32_t len);
162 
163 void Nandflash_ReadID(NANDFLASH_CFG_Type *pNandflashCfg, uint8_t *pManufactureID, uint8_t *pDeviceID);
164 void Nandflash_PagesProgram(NANDFLASH_CFG_Type *pNandflashCfg, uint32_t addr, const uint8_t *buff, uint32_t len);
165 void Nandflash_PageProgram(NANDFLASH_CFG_Type *pNandflashCfg, uint32_t addr, const uint8_t *buff, uint32_t len);
166 void Nandflash_ProgramLoad(NANDFLASH_CFG_Type *pNandflashCfg, uint32_t pageOffset, const uint8_t *buff, uint32_t len);
167 void Nandflash_ProgramExecute(NANDFLASH_CFG_Type *pNandflashCfg, uint32_t addr);
168 void Nandflash_InternalDataMove(NANDFLASH_CFG_Type *pNandflashCfg);
169 void Nandflash_ProgramLoadRandomData(NANDFLASH_CFG_Type *pNandflashCfg, uint32_t addr, uint8_t *buff, uint32_t len);
170 void Nandflash_BlockErase(NANDFLASH_CFG_Type *pNandflashCfg, uint32_t addr);
171 void Nandflash_Reset(NANDFLASH_CFG_Type *pNandflashCfg);
172 
173 void Nandflash_WriteUnprotect(NANDFLASH_CFG_Type *pNandflashCfg);
174 NANDFLASH_ECC_Type Nandflash_Check_ECC_Status(NANDFLASH_CFG_Type *pNandflashCfg);
175 
176 int Nandflash_Disk_Status(void);
177 int Nandflash_Disk_Initialize(void);
178 int Nandflash_Disk_Read(BYTE *buff, LBA_t sector, UINT count);
179 int Nandflash_Disk_Write(const BYTE *buff, LBA_t sector, UINT count);
180 int Nandflash_Disk_Ioctl(BYTE cmd, void *buff);
181 DSTATUS Nandflash_Translate_Result_Code(int result);
182 
183 #endif
184