1 /******************************************************************************
2  * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************/
18 
19 /********************************************************************************************************
20  * @file	flash.h
21  *
22  * @brief	This is the header file for B91
23  *
24  * @author	Driver Group
25  *
26  *******************************************************************************************************/
27 #pragma once
28 
29 #include "mspi.h"
30 #include "compiler.h"
31 
32 
33 #define PAGE_SIZE 256
34 
35 /**
36  * @brief     flash command definition
37  */
38 typedef enum
39 {
40 	FLASH_WRITE_STATUS_CMD		=	0x01,
41 	FLASH_WRITE_CMD				=	0x02,
42 	FLASH_READ_CMD				=	0x03,
43 
44 	FLASH_WRITE_DISABLE_CMD 	= 	0x04,
45 	FLASH_READ_STATUS_CMD		=	0x05,
46 	FLASH_WRITE_ENABLE_CMD 		= 	0x06,
47 
48 	FLASH_CHIP_ERASE_CMD		=	0x60,   //or 0xc7
49 
50 	FLASH_PES_CMD				=	0x75,
51 	FLASH_PER_CMD				=	0x7A,
52 	FLASH_QUAD_PAGE_PROGRAM_CMD	=	0x32,
53 	FLASH_READ_DEVICE_ID_CMD	=	0x90,
54 
55 	FLASH_FAST_READ_CMD			=	0x0B,
56 	FLASH_X2READ_CMD			=	0xBB,
57 	FLASH_DREAD_CMD				=	0x3B,
58 	FLASH_X4READ_CMD			=	0xEB,
59 	FLASH_QREAD_CMD				=	0x6B,
60 
61 	FLASH_SECT_ERASE_CMD		=	0x20,   //sector size = 4KBytes
62 	FLASH_32KBLK_ERASE_CMD		=	0x52,
63 	FLASH_64KBLK_ERASE_CMD		=	0xD8,
64 	FLASH_GD_PUYA_READ_UID_CMD	=	0x4B,	//Flash Type = GD/PUYA
65 	FLASH_XTX_READ_UID_CMD		=	0x5A,	//Flash Type = XTX
66 	FLASH_PAGE_ERASE_CMD		=	0x81,   //caution: only P25Q40L support this function
67 
68 	FLASH_POWER_DOWN			=	0xB9,
69 	FLASH_POWER_DOWN_RELEASE	=	0xAB,
70 	FLASH_GET_JEDEC_ID			=	0x9F,
71 	FLASH_READ_STATUS_1_CMD		=	0x35,
72 
73 	FLASH_VOLATILE_SR_WRITE_CMD	=	0x50,
74 	FLASH_SET_BURST_WITH_WRAP_CMD	=	0x77,
75 	FLASH_ENABLE_SO_TO_OUTPUT_CMD	=	0x70,
76 	FLASH_READ_DEVICE_ID_DUAL_CME	=	0x92,
77 	RLASH_READ_DEVICE_ID_QUAD_CMD	=	0x94,
78 	FLASH_ERASE_SECURITY_REGISTERS_CMD	=	0x44,
79 	FLASH_PROGRAM_SECURITY_REGISTERS_CMD	=	0x42,
80 	FLASH_READ_SECURITY_REGISTERS_CMD	=	0x48,
81 	FLASH_ENABLE_RESET_CMD	=	0x99,
82 
83 	FLASH_ENABLE_RESET	=	0x66,
84 	FLASH_DISABLE_SO_TO_OUTPUT	=	0x80,
85 }flash_command_e;
86 
87 /**
88  * @brief     flash type definition
89  */
90 typedef enum{
91 	FLASH_TYPE_PUYA	= 0,
92 }flash_type_e;
93 
94 /**
95  * @brief   flash capacity definition
96  * 			Call flash_read_mid function to get the size of flash capacity.
97  * 			Example is as follows:
98  * 			unsigned char temp_buf[4];
99  * 			flash_read_mid(temp_buf);
100  * 			The value of temp_buf[2] reflects flash capacity.
101  */
102 typedef enum {
103     FLASH_SIZE_64K     = 0x10,
104     FLASH_SIZE_128K    = 0x11,
105     FLASH_SIZE_256K    = 0x12,
106     FLASH_SIZE_512K    = 0x13,
107     FLASH_SIZE_1M      = 0x14,
108     FLASH_SIZE_2M      = 0x15,
109     FLASH_SIZE_4M      = 0x16,
110     FLASH_SIZE_8M      = 0x17,
111 } flash_capacity_e;
112 
113 typedef struct{
114 	unsigned char  flash_read_cmd;			/**< xip read command */
115 	unsigned char  flash_read_dummy:4;		/**< dummy cycle = flash_read_dummy + 1 */
116 	unsigned char  flash_read_data_line:2;	/**< 0:single line;  1: dual line;  2:quad line; 3:quad line */
117 	unsigned char  flash_read_addr_line:1;	/**< 0:single line;  1:the same to dat_line_h */
118 	unsigned char  flash_read_cmd_line:1; 	/**< 0:single line;  1:the same to dat_line_h */
119 }flash_xip_config_t;
120 /**
121  * @brief     	This function serves to erase a page(256 bytes).
122  * @param[in] 	addr	- the start address of the page needs to erase.
123  * @return    	none.
124  */
125 _attribute_text_sec_ void flash_erase_page(unsigned int addr);
126 
127 /**
128  * @brief 		This function serves to erase a sector.
129  * @param[in]   addr	- the start address of the sector needs to erase.
130  * @return 		none.
131  */
132 _attribute_text_sec_ void flash_erase_sector(unsigned long addr);
133 
134 /**
135  * @brief 		This function serves to erase a block(32k).
136  * @param[in]   addr	- the start address of the block needs to erase.
137  * @return 		none.
138  */
139 _attribute_text_sec_ void flash_erase_32kblock(unsigned int addr);
140 
141 /**
142  * @brief 		This function serves to erase a block(64k).
143  * @param[in]   addr	- the start address of the block needs to erase.
144  * @return 		none.
145  */
146 _attribute_text_sec_ void flash_erase_64kblock(unsigned int addr);
147 
148 /**
149  * @brief     	This function serves to erase a chip.
150  * @return    	none.
151  */
152 _attribute_text_sec_ void flash_erase_chip(void);
153 
154 /**
155  * @brief 		This function writes the buffer's content to a page.
156  * @param[in]   addr	- the start address of the page.
157  * @param[in]   len		- the length(in byte) of content needs to write into the page.
158  * @param[in]   buf		- the start address of the content needs to write into.
159  * @return 		none.
160  */
161 _attribute_text_sec_ void flash_write_page(unsigned long addr, unsigned long len, unsigned char *buf);
162 
163 /**
164  * @brief 		This function reads the content from a page to the buf.
165  * @param[in]   addr	- the start address of the page.
166  * @param[in]   len		- the length(in byte) of content needs to read out from the page.
167  * @param[out]  buf		- the start address of the buffer.
168  * @return 		none.
169  */
170 _attribute_text_sec_ void flash_read_page(unsigned long addr, unsigned long len, unsigned char *buf);
171 
172 /**
173  * @brief 		This function write the status of flash.
174  * @param[in]  	data	- the value of status.
175  * @return 		none.
176  */
177 _attribute_text_sec_ void flash_write_status(unsigned short data);
178 
179 /**
180  * @brief 		This function reads the status of flash.
181  * @return 		the value of status.
182  */
183 _attribute_text_sec_ unsigned short flash_read_status(void);
184 
185 /**
186  * @brief		Deep Power Down mode to put the device in the lowest consumption mode
187  * 				it can be used as an extra software protection mechanism,while the device
188  * 				is not in active use,since in the mode,  all write,Program and Erase commands
189  * 				are ignored,except the Release from Deep Power-Down and Read Device ID(RDI)
190  * 				command.This release the device from this mode
191  * @return 		none.
192  */
193 _attribute_text_sec_ void flash_deep_powerdown(void);
194 
195 /**
196  * @brief		The Release from Power-Down or High Performance Mode/Device ID command is a
197  * 				Multi-purpose command.it can be used to release the device from the power-Down
198  * 				State or High Performance Mode or obtain the devices electronic identification
199  * 				(ID)number.Release from Power-Down will take the time duration of tRES1 before
200  * 				the device will resume normal operation and other command are accepted.The CS#
201  * 				pin must remain high during the tRES1(8us) time duration.
202  * @return      none.
203  */
204 _attribute_text_sec_ void flash_release_deep_powerdown(void);
205 
206 /**
207  * @brief	  	This function serves to read MID of flash(MAC id). Before reading UID of flash,
208  * 				you must read MID of flash. and then you can look up the related table to select
209  * 				the idcmd and read UID of flash
210  * @return    	MID of the flash(4 bytes).
211  */
212 _attribute_text_sec_ unsigned int flash_read_mid(void);
213 
214 /**
215  * @brief	  	This function serves to read UID of flash
216  * @param[in] 	idcmd	- different flash vendor have different read-uid command. E.g: GD/PUYA:0x4B; XTX: 0x5A
217  * @param[in] 	buf		- store UID of flash
218  * @return    	none.
219  */
220 _attribute_text_sec_ void flash_read_uid(unsigned char idcmd, unsigned char *buf);
221 
222 /**
223  * @brief		This function serves to read flash mid and uid,and check the correctness of mid and uid.
224  * @param[out]	flash_mid	- Flash Manufacturer ID
225  * @param[out]	flash_uid	- Flash Unique ID
226  * @return		0: flash no uid or not a known flash model 	 1:the flash model is known and the uid is read.
227  */
228 _attribute_text_sec_ int flash_read_mid_uid_with_check( unsigned int *flash_mid ,unsigned char *flash_uid);
229 
230 /**
231  * @brief 		This function serves to set the protection area of the flash.
232  * @param[in]   type	- flash type include Puya.
233  * @param[in]   data	- refer to Driver API Doc.
234  * @return 		none.
235  */
236 _attribute_text_sec_ void flash_lock(flash_type_e type, unsigned short data);
237 
238 /**
239  * @brief 		This function serves to flash release protection.
240  * @param[in]   type	- flash type include Puya.
241  * @return 		none.
242  */
243 _attribute_text_sec_ void flash_unlock(flash_type_e type);
244 
245 /**
246  * @brief 		This function serves to set priority threshold. when the interrupt priority > Threshold flash process will disturb by interrupt.
247  * @param[in]   preempt_en	- 1 can disturb by interrupt, 0 can disturb by interrupt.
248  * @param[in]	threshold	- priority Threshold.
249  * @return    	none.
250  */
251 _attribute_text_sec_ void flash_plic_preempt_config(unsigned char preempt_en, unsigned char threshold);
252 /**
253  * @brief 		This function is used to update the configuration parameters of xip(eXecute In Place),
254  * 				this configuration will affect the speed of MCU fetching,
255  * 				this parameter needs to be consistent with the corresponding parameters in the flash datasheet.
256  * @param[in]	config	- xip configuration,reference structure flash_xip_config_t
257  * @return none
258  */
259 _attribute_text_sec_ void flash_set_xip_config(flash_xip_config_t config);
260 /**
261  * @brief		This function serves to set flash write command.This function interface is only used internally by flash,
262  * 				and is currently included in the H file for compatibility with other SDKs. When using this interface,
263  * 				please ensure that you understand the precautions of flash before using it.
264  * @param[in]	cmd	- set command.
265  * @return		none.
266  */
267 _attribute_ram_code_sec_noinline_ void flash_send_cmd(unsigned char cmd);
268 
269