yincheng.zhong
2024-08-20 7744fffacb03dc81cc9dbaf9f5d86a0f21e79c03
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * Copyright (c) 2019-2023 Beijing Hanwei Innovation Technology Ltd. Co. and
 * its subsidiaries and affiliates (collectly called MKSEMI).
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into an MKSEMI
 *    integrated circuit in a product or a software update for such product,
 *    must reproduce the above copyright notice, this list of conditions and
 *    the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of MKSEMI nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    MKSEMI integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be
 *    reverse engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY MKSEMI "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL MKSEMI OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
#ifndef MK_FLASH_H_
#define MK_FLASH_H_
 
#include "mk_common.h"
#include "mk_dma.h"
#include "mk_misc.h"
 
#ifndef FLASH_INT_MODE_EN
#define FLASH_INT_MODE_EN (1)
#endif
 
#ifndef FLASH_DMA_MODE_EN
#define FLASH_DMA_MODE_EN (1)
#endif
 
#ifndef FLASH_WRITE_EN
#define FLASH_WRITE_EN (1)
#endif
 
/**
 * @addtogroup MK8000_Flash
 * @{
 */
 
#define FLASH_PAGE_SIZE 0x100U
#define FLASH_SECTOR_SIZE 0x1000U
#define FLASH_BLOCK_SIZE 0x10000U
 
#define FLASH_OP_ERASE 1
#define FLASH_OP_WRITE 2
#define FLASH_OP_READ 3
 
// Flash status check timeout
#define FLASH_OP_ERASE_SECTOR_TIMEOUT __MS_TO_TICKS(600)
#define FLASH_OP_ERASE_BLOCK_TIMEOUT __MS_TO_TICKS(4000)
#define FLASH_OP_WRITE_PAGE_TIMEOUT __MS_TO_TICKS(6)
#define FLASH_OP_WRITE_STATUS_TIMEOUT __MS_TO_TICKS(40)
 
/** FLASH device instance */
enum FLASH_DEV_T
{
    FLASH_ID0 = 0,
    FLASH_MAX_NUM
};
 
/** FLASH operation code */
enum FLASH_OPCODE_T
{
    FLASH_OPCODE_READ = 0,
    FLASH_OPCODE_PROGRAM_PAGE,
    FLASH_OPCODE_GET_STATUS,
    FLASH_OPCODE_GET_STATUS1,
    FLASH_OPCODE_ERASE_SECTOR,
    FLASH_OPCODE_ERASE_BLOCK,
    FLASH_OPCODE_WRITE_ENABLE,
    FLASH_OPCODE_WRITE_REGISTER,
    FLASH_OPCODE_RDID,
    FLASH_OPCODE_POWER_DOWN,
    FLASH_OPCODE_RELEASE_POWER_DOWN,
    FLASH_OPCODE_NUM,
};
 
/** FLASH operation state */
enum FLASH_STATE_T
{
    FLASH_STATE_RESET = 0,
    FLASH_STATE_READY,
    FLASH_STATE_BUSY_READ,
    FLASH_STATE_BUSY_ERASE,
    FLASH_STATE_BUSY_WRITE,
    FLASH_STATE_TIMEOUT,
    FLASH_STATE_ERROR
};
 
/*! @brief FLASH data direction */
enum FLASH_DATA_DIR_TYPE_T
{
    FLASH_DATA_IN = 0x0U, /*!< Data input from serial flash. */
    FLASH_DATA_OUT = 0x1U /*!< Data output to serial flash. */
};
 
/*! @brief FLASH command opcode format */
enum FLASH_CMD_FORMAT_T
{
    FLASH_CMD_ALL_SERIAL = 0x0,      /*!< All fields of command are serial. */
    FLASH_CMD_DATA_QUAD_DUAL = 0x1U, /*!< Only data field is dual/quad, others are serial. */
    FLASH_CMD_OP_SERIAL = 0x2U,      /*!< Only opcode field is serial, others are quad/dual. */
    FLASH_CMD_ALL_QUAD_DUAL = 0x3U   /*!< All fields of command are dual/quad mode. */
};
 
/*! @brief FLASH command type */
enum FLASH_CMD_TYPE_T
{
    FLASH_CMD_OP_ONLY = 0x1U,       /*!< Command only have opcode, no address field */
    FLASH_CMD_OP_ADDR_1B = 0x2U,    /*!< Command have opcode and also one byte address field */
    FLASH_CMD_OP_ADDR_2B = 0x3U,    /*!< Command have opcode and also two bytes address field */
    FLASH_CMD_OP_ADDR_3B = 0x4U,    /*!< Command have opcode and also three bytes address field. */
    FLASH_CMD_OP_ADDR_4B = 0x5U,    /*!< Command have opcode and also four bytes address field */
    FLASH_CMD_NO_OP_ADDR_3B = 0x6U, /*!< Command have no opcode and three bytes address field */
    FLASH_CMD_NO_OP_ADDR_4B = 0x7U  /*!< Command have no opcode and four bytes address field */
};
 
/*! @brief FLASH command structure */
struct FLASH_CMD_T
{
    uint32_t polling_mode;               /*!< For command need to read data from serial flash */
    enum FLASH_DATA_DIR_TYPE_T data_dir; /*!< Data direction of this command. */
    enum FLASH_CMD_FORMAT_T format;      /*!< Command format */
    enum FLASH_CMD_TYPE_T type;          /*!< Command type */
    uint16_t data_len;                   /*!< How many data bytes are needed in this command. */
    uint8_t dummy_len;                   /*!< How many intermediate bytes needed */
    uint8_t opcode;                      /*!< Command opcode value */
};
 
/*!
 * @brief FLASH configuration structure.
 */
struct FLASH_CFG_T
{
    uint16_t timeout;                 /*!< SPI transfer timeout, the unit is SCK cycles */
    uint8_t cs_high_time;             /*!< CS high time cycles */
    bool dual_mode : 1;               /*!< true: dual mode, false: quad mode */
    bool prefetch_dis : 1;            /*!< True means FLASH will not attempt a speculative prefetch */
    bool cache_prefetch_dis : 1;      /*!< Disable prefetch of cache line */
    bool feedback_clk_sel : 1;        /*!< Is data sample uses feedback clock */
    bool spi_sck_high : 1;            /*!< FLASH spi mode select */
    bool spi_falling_edge_active : 1; /*!< If enable read full clock cycle. */
    bool dma_en : 1;
    bool int_en : 1;
    uint32_t start_addr;
    uint32_t total_size;
    uint32_t block_size;
    uint16_t sector_size;
    uint16_t page_size;
};
 
struct FLASH_DMA_OP_CFG_T
{
    uint32_t start_addr;
    const uint8_t *buf;
    uint32_t len;
    uint32_t count;
};
 
struct FLASH_WRITE_NBYTES_CFG_T
{
    const uint8_t *src_addr;
    uint32_t write_start_addr;
    uint32_t write_end_addr;
    uint32_t offset_addr;
    uint32_t page_start;
    uint32_t page_end;
    uint32_t page_count;
    uint32_t write_num;
    uint32_t write_count;
    uint32_t start_page_inc_data_len;
    uint32_t end_page_inc_data_len;
    uint32_t src_buf_pos;
    __IOM uint32_t dest_tmp_addr;
};
 
struct FLASH_HANDLE_T
{
    FLASH_CTRL_TypeDef *const base;
    const IRQn_Type irq;
    enum DMA_CH_T dma_wr_ch;
    enum DMA_CH_T dma_rd_ch;
    enum FLASH_STATE_T state;
    struct FLASH_CFG_T config;
    struct FLASH_WRITE_NBYTES_CFG_T wr_nbyte_cfg;
    struct FLASH_DMA_OP_CFG_T dma_op;
    uint32_t code; // reserved | err_code | int_flag | op_code
    drv_callback_t callback;
    uint8_t flash_type;
    uint8_t wrsr_type;
    uint8_t work_mode;
    uint8_t promote_fclk;
    uint8_t is_open;
};
 
#ifdef __cplusplus
extern "C" {
#endif
 
extern struct FLASH_HANDLE_T flash_handle[FLASH_MAX_NUM];
 
int flash_open(enum FLASH_DEV_T id, struct FLASH_CFG_T *cfg);
int flash_close(enum FLASH_DEV_T id);
 
void RAM_FUNC flash_power_up(enum FLASH_DEV_T id);
void RAM_FUNC flash_power_down(enum FLASH_DEV_T id);
 
int flash_sector_erase(enum FLASH_DEV_T id, uint32_t sector);
int flash_block_erase(enum FLASH_DEV_T id, uint32_t block);
 
int flash_erase(enum FLASH_DEV_T id, uint32_t start_addr, uint32_t len);
 
int flash_write_nbytes(enum FLASH_DEV_T id, uint32_t start_addr, const uint8_t *buf, uint32_t len);
int flash_write(enum FLASH_DEV_T id, uint32_t start_addr, const uint8_t *buf, uint32_t len);
int flash_read(enum FLASH_DEV_T id, uint32_t start_addr, uint8_t *buf, uint32_t len);
 
void FLASH_CTRL_IRQHandler(void);
 
bool flash_check_busy(enum FLASH_DEV_T id);
 
int RAM_FUNC flash_open_for_xip(enum FLASH_DEV_T id);
 
#ifdef __cplusplus
}
#endif
 
/**
 * @}
 */
 
#endif /* MK_FLASH_H_ */