• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Winner Microelectronics Co., Ltd. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @file    wm_internal_flash.h
18  *
19  * @brief  inter flash driver
20  *
21  * @author  dave
22  *
23  * @copyright (c) 2015 Winner Microelectronics Co., Ltd.
24  */
25 
26 #ifndef WM_INTERNAL_FLASH_H
27 #define WM_INTERNAL_FLASH_H
28 
29 #include "wm_type_def.h"
30 #include "wm_osal.h"
31 
32 typedef volatile unsigned char vu8;
33 typedef volatile unsigned short vu16;
34 typedef volatile unsigned long vu32;
35 
36 #define M8(adr)		(*((vu8 *) (adr)))
37 #define M16(adr)	(*((vu16*) (adr)))
38 #define M32(adr)	(*((vu32*) (adr)))
39 
40 #define INSIDE_FLS_SECTOR_SIZE	0x1000
41 #define INSIDE_FLS_PAGE_SIZE	256
42 
43 #define INSIDE_FLS_BASE_ADDR		0x8000000UL
44 #define INSIDE_FLS_SECBOOT_ADDR 	(INSIDE_FLS_BASE_ADDR  + 0x02000)
45 
46 enum TYPE_FLASH_ID {
47     SPIFLASH_MID_GD = 0xC8,
48     SPIFLASH_MID_ESMT = 0x1C,
49     SPIFLASH_MID_PUYA = 0x85,
50     SPIFLASH_MID_WINBOND = 0xEF,
51     SPIFLASH_MID_FUDANMICRO = 0xA1,
52     SPIFLASH_MID_BOYA       = 0x68,
53     SPIFLASH_MID_XMC        = 0x20,
54     SPIFLASH_MID_XTX        = 0x0B,
55     SPIFLASH_MID_TSINGTENG    = 0xEB, /* UNIGROUP TSINGTENG */
56 };
57 
58 typedef union {
59     struct {
60         uint32_t _reserved0 : 1;           /*!< bit:  0  Reserved */
61         uint32_t code_decrypt : 1;         /*!< bit:  1      read code from AHB decrypt flag */
62         uint32_t dbus_decrypt : 1;         /*!< bit:  2      read data from Flash register controller decrypt flag */
63         uint32_t data_decrypt : 1;         /*!< bit:  3      read data from AHB decrypt flag */
64         uint32_t prikey_sel : 3;           /*!< bit:  4.. 6   private key selection: 0 : first one; 1 : second one; */
65         uint32_t decrypt_start : 1;        /*!< bit:  7   write 1 to start RSA decryption operation */
66         uint32_t _reserved2 : 24;          /*!< bit:  8.. 31  Reserved */
67     } b;                                   /*!< Structure    Access by bit */
68     uint32_t w;                            /*!< Type         Access by whole register */
69 } FLASH_ENCRYPT_CTRL_Type;
70 
71 /**
72  * @typedef struct    Flash Registers
73  */
74 typedef struct {
75     vu32 ACR;                   /**< offset 0x000 */
76     vu32 KEYR;                 /**< offset 0x004 */
77     vu32 SR;                     /**< offset 0x008 */
78     vu32 CR;                     /**< offset 0x00C */
79     vu32 AR;                     /**< offset 0x010 */
80 } FLASH_TypeDef;
81 
82 #define FLASH_HS				0x00000001
83 
84  /** Flash Keys */
85 #define RDPRT_KEY       0x5AA5
86 #define FLASH_KEY1      0x57696E6E
87 #define FLASH_KEY2      0x65724D69
88 #define FLASH_KEY3			0x63726F21
89 
90  /** Flash Control Register definitions */
91 #define FLASH_PG        0x00000001
92 #define FLASH_PER       0x00000002
93 #define FLASH_MER       0x00000004
94 #define FLASH_STRT			0x00000008
95 #define FLASH_LOCK   	  0x00000020
96 #define FLASH_ERRIE     0x00000040
97 #define FLASH_EOPIE     0x00000080
98 
99  /** Flash Status Register definitions */
100 #define FLASH_BSY       0x00000001
101 #define FLASH_PGERR     0x00000002
102 #define FLASH_EOP       0x00000004
103 
104 #define TLS_FLS_STATUS_OK      (0)
105 #define TLS_FLS_STATUS_EINVAL      (1)
106 #define TLS_FLS_STATUS_EBUSY      (2)
107 #define TLS_FLS_STATUS_EPERM      (3)
108 #define TLS_FLS_STATUS_ENOSUPPORT      (4)
109 #define TLS_FLS_STATUS_EEXIST      (5)
110 #define TLS_FLS_STATUS_ENOMEM      (6)
111 #define TLS_FLS_STATUS_EOVERFLOW      (7)
112 #define TLS_FLS_STATUS_ENODEV      (8)
113 #define TLS_FLS_STATUS_EDEV      (9)
114 #define TLS_FLS_STATUS_EIO      (10)
115 #define TLS_FLS_STATUS_ENODRV      (11)
116 
117 #define TLS_FLS_PARAM_TYPE_ID      (0)
118 #define TLS_FLS_PARAM_TYPE_SIZE      (1)
119 #define TLS_FLS_PARAM_TYPE_PAGE_SIZE      (2)
120 #define TLS_FLS_PARAM_TYPE_PROG_SIZE      (3)
121 #define TLS_FLS_PARAM_TYPE_SECTOR_SIZE      (4)
122 
123 #define TLS_FLS_FLAG_UNDER_PROTECT      (1<<0)
124 #define TLS_FLS_FLAG_FAST_READ      (1<<1)
125 #define TLS_FLS_FLAG_AAAI      (1<<2)
126 
127 #define CMD_START_Pos                         8U                                          /*!< CMD start position */
128 #define CMD_START_Msk                         (1UL << CMD_START_Pos)                         /*!< CMD start Mask */
129 
130 typedef struct {
131     uint16_t eraseSize;
132     uint16_t pageSize;
133 } FLASH_OTP_WR_PARAM_ST;
134 
135 /**
136  * @struct tls_inside_fls
137  */
138 struct tls_inside_fls {
139     tls_os_sem_t *fls_lock;
140     unsigned char flashid;
141     unsigned int density;
142     FLASH_OTP_WR_PARAM_ST OTPWRParam;
143 };
144 
145 /**
146  * @defgroup Driver_APIs Driver APIs
147  * @brief Driver APIs
148  */
149 
150 /**
151  * @addtogroup Driver_APIs
152  * @{
153  */
154 
155 /**
156  * @defgroup INNER_FLASH_Driver_APIs INNER FLASH Driver APIs
157  * @brief INNER FLASH driver APIs
158  */
159 
160 /**
161  * @addtogroup INNER_FLASH_Driver_APIs
162  * @{
163  */
164 
165 /**
166  * @brief          This function is used to unlock flash protect area [0x0~0x2000].
167  *
168  * @param	       None
169  *
170  * @return         0-success,non-zero-failure
171  *
172  * @note           None
173  */
174 int tls_flash_unlock(void);
175 
176 /**
177  * @brief          This function is used to lock flash protect area [0x0~0x2000].
178  *
179  * @param	       None
180  *
181  * @return         0-success,non-zero-failure
182  *
183  * @note           None
184  */
185 int tls_flash_lock(void);
186 
187 /**
188  * @brief          This function is used to get the flash semaphore.
189  *
190  * @param	       None
191  *
192  * @return         None
193  *
194  * @note           None
195  */
196 void tls_fls_sem_lock(void);
197 
198 /**
199  * @brief          This function is used to release the flash semaphore.
200  *
201  * @param	       None
202  *
203  * @return         None
204  *
205  * @note           None
206  */
207 void tls_fls_sem_unlock(void);
208 
209 /**
210  * @brief          This function is used to read the unique id of the internal flash.
211  *
212  * @param[out]    uuid Specified the address to save the uuid, the length must be greater than or equals to 18 bytes.
213  *
214  * @retval         TLS_FLS_STATUS_OK	    if read sucsess
215  * @retval         TLS_FLS_STATUS_EIO	    if read fail
216  *
217  * @note           The uuid's length must be greater than or equals to 18 bytes.
218  */
219 int tls_fls_read_unique_id(unsigned  char *uuid);
220 
221 /**
222  * @brief          This function is used to initial flash module structer.
223  *
224  * @param[in]      None
225  *
226  * @retval         TLS_FLS_STATUS_OK	         if init sucsess
227  * @retval         TLS_FLS_STATUS_EBUSY          already inited
228  * @retval         TLS_FLS_STATUS_ENOMEM         memory error
229  *
230  * @note           None
231  */
232 int tls_fls_init(void);
233 
234 /**
235  * @brief          This function is used to read data from the flash.
236  *
237  * @param[in]      addr                 Specifies the starting address to read from.
238  * @param[in]      buf                  Specified the address to save the readback data.
239  * @param[in]      len                  Specifies the length of the data to read.
240  *
241  * @retval         TLS_FLS_STATUS_OK	    if read sucsess
242  * @retval         TLS_FLS_STATUS_EIO	    if read fail
243  *
244  * @note           None
245  */
246 int tls_fls_read(u32 addr, u8 *buf, u32 len);
247 
248 /**
249  * @brief          This function is used to write data into the flash.
250  *
251  * @param[in]      addr     Specifies the starting address to write to
252  * @param[in]      buf      Pointer to a byte array that is to be written
253  * @param[in]      len      Specifies the length of the data to be written
254  *
255  * @retval         TLS_FLS_STATUS_OK	        if write flash success
256  * @retval         TLS_FLS_STATUS_EPERM	        if flash struct point is null
257  * @retval         TLS_FLS_STATUS_ENODRV	    if flash driver is not installed
258  * @retval         TLS_FLS_STATUS_EINVAL	    if argument is invalid
259  * @retval         TLS_FLS_STATUS_EIO           if io error
260  *
261  * @note           None
262  */
263 int tls_fls_write(u32 addr, u8 *buf, u32 len);
264 
265 /**
266  * @brief          This function is used to write data into the flash without erase.
267  *
268  * @param[in]      addr     Specifies the starting address to write to
269  * @param[in]      buf      Pointer to a byte array that is to be written
270  * @param[in]      len      Specifies the length of the data to be written
271  *
272  * @retval         TLS_FLS_STATUS_OK	        if write flash success
273  * @retval         TLS_FLS_STATUS_EPERM	        if flash struct point is null
274  * @retval         TLS_FLS_STATUS_ENODRV	    if flash driver is not installed
275  * @retval         TLS_FLS_STATUS_EINVAL	    if argument is invalid
276  *
277  * @note           Erase action should be excuted by API tls_fls_erase in user layer.
278  */
279 int tls_fls_write_without_erase(u32 addr, u8 *buf, u32 len);
280 
281 /**
282  * @brief          	This function is used to erase the appointed sector
283  *
284  * @param[in]      	sector 	sector num of the flash, 4K bytes every sector
285  *
286  * @retval         	TLS_FLS_STATUS_OK	    	if read sucsess
287  * @retval         	other	    				if read fail
288  *
289  * @note           	None
290  */
291 int tls_fls_erase(u32 sector);
292 
293 /**
294  * @brief          	This function is used to initialize system parameter postion by flash density
295  *
296  * @param      	None
297  *
298  * @retval         	None
299  *
300  * @note           	must be called before tls_param_init
301  */
302 void tls_fls_sys_param_postion_init(void);
303 
304 /**
305  * @brief          This function is used to read data from the security registers.
306  *
307  * @param[in]      addr                 Specifies the starting address to read from.
308  * @param[in]      buf                  Specified the address to save the readback data.
309  * @param[in]      len                  Specifies the length of the data to read.
310  *
311  * @retval         TLS_FLS_STATUS_OK	    if read sucsess
312  * @retval         TLS_FLS_STATUS_EPERM	        if flash struct point is null
313  *
314  * @note           None
315  */
316 int tls_fls_otp_read(u32 addr, u8 *buf, u32 len);
317 
318 /**
319  * @brief          This function is used to write data into the security registers.
320  *
321  * @param[in]      addr     Specifies the starting address to write to
322  * @param[in]      buf      Pointer to a byte array that is to be written
323  * @param[in]      len      Specifies the length of the data to be written
324  *
325  * @retval         TLS_FLS_STATUS_OK	        if write the security registers success
326  * @retval         TLS_FLS_STATUS_EPERM	        if flash struct point is null
327  * @retval         TLS_FLS_STATUS_ENOSUPPORT	    if flash is not supported
328  * @retval         TLS_FLS_STATUS_EINVAL	    if argument is invalid
329  * @retval         TLS_FLS_STATUS_ENOMEN           if no memory
330  *
331  * @note           None
332  */
333 int tls_fls_otp_write(u32 addr, u8 *buf, u32 len);
334 
335 /**
336  * @brief          This function is used to lock the security registers.
337  *
338  * @param	       None
339  *
340  * @return         None
341  *
342  * @note           None
343  */
344 int tls_fls_otp_lock(void);
345 
346 #endif /* WM_INTERNAL_FLASH_H */
347