1 /**************************************************************************//**
2 * @file fmc.h
3 * @version V3.0
4 * $Revision: 2 $
5 * $Date: 16/07/29 3:11p $
6 * @brief M2351 Series Flash Memory Controller(FMC) driver header file
7 *
8 * @copyright SPDX-License-Identifier: Apache-2.0
9 * @copyright Copyright (C) 2016-2020 Nuvoton Technology Corp. All rights reserved.
10 ******************************************************************************/
11 #ifndef __FMC_H__
12 #define __FMC_H__
13
14 #ifdef __cplusplus
15 extern "C"
16 {
17 #endif
18
19
20 /** @addtogroup Standard_Driver Standard Driver
21 @{
22 */
23
24 /** @addtogroup FMC_Driver FMC Driver
25 @{
26 */
27
28 /** @addtogroup FMC_EXPORTED_CONSTANTS FMC Exported Constants
29 @{
30 */
31
32 /*---------------------------------------------------------------------------------------------------------*/
33 /* Global constant definitions */
34 /*---------------------------------------------------------------------------------------------------------*/
35 #define ISBEN 0UL
36
37 /*---------------------------------------------------------------------------------------------------------*/
38 /* Define Base Address */
39 /*---------------------------------------------------------------------------------------------------------*/
40 #define FMC_APROM_BASE 0x00000000UL /*!< APROM Base Address */
41 #define FMC_APROM_END 0x00080000UL /*!< APROM end address */
42 #define FMC_APROM_BANK0_END (FMC_APROM_END/2UL) /*!< APROM bank0 end address */
43 #define FMC_LDROM_BASE 0x00100000UL /*!< LDROM Base Address */
44 #define FMC_LDROM_END 0x00101000UL /*!< LDROM end address */
45 #define FMC_XOM_BASE 0x00200000UL /*!< XOM Base Address */
46 #define FMC_XOMR0_BASE 0x00200000UL /*!< XOMR 0 Base Address */
47 #define FMC_XOMR1_BASE 0x00200010UL /*!< XOMR 1 Base Address */
48 #define FMC_XOMR2_BASE 0x00200020UL /*!< XOMR 2 Base Address */
49 #define FMC_XOMR3_BASE 0x00200030UL /*!< XOMR 3 Base Address */
50 #define FMC_NSCBA_BASE 0x00200800UL /*!< Non-Secure base address */
51 #define FMC_SCRLOCK_BASE 0x00200804UL /*!< Secure Region Lock base address */
52 #define FMC_ARLOCK_BASE 0x00210804UL /*!< All Region Lock base address */
53 #define FMC_CONFIG_BASE 0x00300000UL /*!< CONFIG Base Address */
54 #define FMC_USER_CONFIG_0 0x00300000UL /*!< CONFIG 0 Address */
55 #define FMC_USER_CONFIG_1 0x00300004UL /*!< CONFIG 1 Address */
56 #define FMC_USER_CONFIG_2 0x00300008UL /*!< CONFIG 2 Address */
57 #define FMC_USER_CONFIG_3 0x0030000CUL /*!< CONFIG 3 Address */
58 #define FMC_OTP_BASE 0x00310000UL /*!< OTP flash base address */
59 #define FMC_KPROM_BASE 0x00311000UL /*!< Security ROM base address */
60
61 #define FMC_FLASH_PAGE_SIZE 0x800UL /*!< Flash Page Size (2048 Bytes) */
62 #define FMC_PAGE_ADDR_MASK 0xFFFFF800UL /*!< Flash page address mask */
63 #define FMC_MULTI_WORD_PROG_LEN 512UL /*!< The maximum length of a multi-word program. */
64
65 #define FMC_APROM_SIZE FMC_APROM_END /*!< APROM Size */
66 #define FMC_BANK_SIZE (FMC_APROM_SIZE/2UL) /*!< APROM Bank Size */
67 #define FMC_LDROM_SIZE 0x1000UL /*!< LDROM Size (4 Kbytes) */
68 #define FMC_OTP_ENTRY_CNT 256UL /*!< OTP entry number */
69
70 /*---------------------------------------------------------------------------------------------------------*/
71 /* XOM region number constant definitions */
72 /*---------------------------------------------------------------------------------------------------------*/
73 #define XOMR0 0UL /*!< XOM region 0 */
74 #define XOMR1 1UL /*!< XOM region 1 */
75 #define XOMR2 2UL /*!< XOM region 2 */
76 #define XOMR3 3UL /*!< XOM region 3 */
77
78 /*---------------------------------------------------------------------------------------------------------*/
79 /* ISPCTL constant definitions */
80 /*---------------------------------------------------------------------------------------------------------*/
81 #define IS_BOOT_FROM_LDROM 0x1UL /*!< ISPCTL setting to select to boot from LDROM */
82 #define IS_BOOT_FROM_APROM 0x0UL /*!< ISPCTL setting to select to boot from APROM */
83
84 /*---------------------------------------------------------------------------------------------------------*/
85 /* ISPCMD constant definitions */
86 /*---------------------------------------------------------------------------------------------------------*/
87 #define FMC_ISPCMD_READ 0x00UL /*!< ISP Command: Read Flash */
88 #define FMC_ISPCMD_READ_UID 0x04UL /*!< ISP Command: Read Unique ID */
89 #define FMC_ISPCMD_READ_ALL1 0x08UL /*!< ISP Command: Read all-one result */
90 #define FMC_ISPCMD_READ_CID 0x0BUL /*!< ISP Command: Read Company ID */
91 #define FMC_ISPCMD_READ_DID 0x0CUL /*!< ISP Command: Read Device ID */
92 #define FMC_ISPCMD_READ_CKS 0x0DUL /*!< ISP Command: Read Checksum */
93 #define FMC_ISPCMD_PROGRAM 0x21UL /*!< ISP Command: 32-bit Program Flash */
94 #define FMC_ISPCMD_PAGE_ERASE 0x22UL /*!< ISP Command: Page Erase Flash */
95 #define FMC_ISPCMD_BANK_ERASE 0x23UL /*!< ISP Command: Erase Flash bank 0 or 1 */
96 #define FMC_ISPCMD_BLOCK_ERASE 0x25UL /*!< ISP Command: Erase 4 pages alignment of APROM in bank 0 or 1 */
97 #define FMC_ISPCMD_PROGRAM_MUL 0x27UL /*!< ISP Command: Flash Multi-Word Program */
98 #define FMC_ISPCMD_RUN_ALL1 0x28UL /*!< ISP Command: Run all-one verification*/
99 #define FMC_ISPCMD_RUN_CKS 0x2DUL /*!< ISP Command: Run Check Calculation */
100 #define FMC_ISPCMD_VECMAP 0x2EUL /*!< ISP Command: Set vector mapping */
101 #define FMC_ISPCMD_READ_64 0x40UL /*!< ISP Command: 64-bit read Flash */
102 #define FMC_ISPCMD_PROGRAM_64 0x61UL /*!< ISP Command: 64-bit program Flash */
103
104 #define READ_ALLONE_YES 0xA11FFFFFUL /*!< Check-all-one result is all one. */
105 #define READ_ALLONE_NOT 0xA1100000UL /*!< Check-all-one result is not all one. */
106 #define READ_ALLONE_CMD_FAIL 0xFFFFFFFFUL /*!< Check-all-one command failed. */
107
108 /*@}*/ /* end of group FMC_EXPORTED_CONSTANTS */
109
110 /** @addtogroup FMC_EXPORTED_FUNCTIONS FMC Exported Functions
111 @{
112 */
113
114 /*---------------------------------------------------------------------------------------------------------*/
115 /* FMC Macro Definitions */
116 /*---------------------------------------------------------------------------------------------------------*/
117 /**
118 * @brief Enable ISP Function
119 *
120 * @param None
121 *
122 * @return None
123 *
124 * @details This function will set ISPEN bit of ISPCTL control register to enable ISP function.
125 *
126 */
127 #define FMC_ENABLE_ISP() (FMC->ISPCTL |= FMC_ISPCTL_ISPEN_Msk) /*!< Enable ISP Function */
128
129 /**
130 * @brief Disable ISP Function
131 *
132 * @param None
133 *
134 * @return None
135 *
136 * @details This function will clear ISPEN bit of ISPCTL control register to disable ISP function.
137 *
138 */
139 #define FMC_DISABLE_ISP() (FMC->ISPCTL &= ~FMC_ISPCTL_ISPEN_Msk) /*!< Disable ISP Function */
140
141 /**
142 * @brief Enable LDROM Update Function
143 *
144 * @param None
145 *
146 * @return None
147 *
148 * @details This function will set LDUEN bit of ISPCTL control register to enable LDROM update function.
149 * User needs to set LDUEN bit before they can update LDROM.
150 *
151 */
152 #define FMC_ENABLE_LD_UPDATE() (FMC->ISPCTL |= FMC_ISPCTL_LDUEN_Msk) /*!< Enable LDROM Update Function */
153
154 /**
155 * @brief Disable LDROM Update Function
156 *
157 * @param None
158 *
159 * @return None
160 *
161 * @details This function will set ISPEN bit of ISPCTL control register to disable LDROM update function.
162 *
163 */
164 #define FMC_DISABLE_LD_UPDATE() (FMC->ISPCTL &= ~FMC_ISPCTL_LDUEN_Msk) /*!< Disable LDROM Update Function */
165
166 /**
167 * @brief Enable User Configuration Update Function
168 *
169 * @param None
170 *
171 * @return None
172 *
173 * @details This function will set CFGUEN bit of ISPCTL control register to enable User Configuration update function.
174 * User needs to set CFGUEN bit before they can update User Configuration area.
175 *
176 */
177 #define FMC_ENABLE_CFG_UPDATE() (FMC->ISPCTL |= FMC_ISPCTL_CFGUEN_Msk) /*!< Enable CONFIG Update Function */
178
179 /**
180 * @brief Disable User Configuration Update Function
181 *
182 * @param None
183 *
184 * @return None
185 *
186 * @details This function will clear CFGUEN bit of ISPCTL control register to disable User Configuration update function.
187 *
188 */
189 #define FMC_DISABLE_CFG_UPDATE() (FMC->ISPCTL &= ~FMC_ISPCTL_CFGUEN_Msk) /*!< Disable CONFIG Update Function */
190
191
192 /**
193 * @brief Enable APROM Update Function
194 *
195 * @param None
196 *
197 * @return None
198 *
199 * @details This function will set APUEN bit of ISPCTL control register to enable APROM update function.
200 * User needs to set APUEN bit before they can update APROM in APROM boot mode.
201 *
202 */
203 #define FMC_ENABLE_AP_UPDATE() (FMC->ISPCTL |= FMC_ISPCTL_APUEN_Msk) /*!< Enable APROM Update Function */
204
205 /**
206 * @brief Disable APROM Update Function
207 *
208 * @param None
209 *
210 * @return None
211 *
212 * @details This function will clear APUEN bit of ISPCTL control register to disable APROM update function.
213 *
214 */
215 #define FMC_DISABLE_AP_UPDATE() (FMC->ISPCTL &= ~FMC_ISPCTL_APUEN_Msk) /*!< Disable APROM Update Function */
216
217 /**
218 * @brief Set Boot from APROM
219 *
220 * @param None
221 *
222 * @return None
223 *
224 * @details This function is select booting from APROM.
225 *
226 */
227 #define FMC_SET_APROM_BOOT() (FMC->ISPCTL &= ~FMC_ISPCTL_BS_Msk) /*!< Select booting from APROM */
228
229 /**
230 * @brief Set Boot from LDROM
231 *
232 * @param None
233 *
234 * @return None
235 *
236 * @details This function is select booting from LDROM.
237 *
238 */
239 #define FMC_SET_LDROM_BOOT() (FMC->ISPCTL |= FMC_ISPCTL_BS_Msk) /*!< Select booting from LDROM */
240
241 /**
242 * @brief Get ISP Fail Flag
243 *
244 * @param None
245 *
246 * @return None
247 *
248 * @details This function is used to get ISP fail flag when do ISP actoin.
249 *
250 */
251 #define FMC_GET_FAIL_FLAG() ((FMC->ISPCTL & FMC_ISPCTL_ISPFF_Msk) ? 1UL : 0UL) /*!< Get ISP fail flag */
252
253 /**
254 * @brief Clear ISP Fail Flag
255 *
256 * @param None
257 *
258 * @return None
259 *
260 * @details This function is used to clear ISP fail flag when ISP fail flag set.
261 *
262 */
263 #define FMC_CLR_FAIL_FLAG() (FMC->ISPCTL |= FMC_ISPCTL_ISPFF_Msk) /*!< Clear ISP fail flag */
264
265 /**
266 * @brief Enable ISP Interrupt
267 *
268 * @param None
269 *
270 * @return None
271 *
272 * @details This function will enable ISP action interrupt.
273 *
274 */
275 #define FMC_ENABLE_ISP_INT() (FMC->ISPCTL |= FMC_ISPCTL_INTEN_Msk) /*!< Enable ISP interrupt */
276
277 /**
278 * @brief Disable ISP Interrupt
279 *
280 * @param None
281 *
282 * @return None
283 *
284 * @details This function will disable ISP action interrupt.
285 *
286 */
287 #define FMC_DISABLE_ISP_INT() (FMC->ISPCTL &= ~FMC_ISPCTL_INTEN_Msk) /*!< Disable ISP interrupt */
288
289
290 /**
291 * @brief Get ISP Interrupt Flag
292 *
293 * @param None
294 *
295 * @return None
296 *
297 * @details This function will get ISP action interrupt status
298 *
299 */
300 #define FMC_GET_ISP_INT_FLAG() ((FMC->ISPSTS & FMC_ISPSTS_INTFLAG_Msk) ? 1UL : 0UL) /*!< Get ISP interrupt flag Status */
301
302
303 /**
304 * @brief Clear ISP Interrupt Flag
305 *
306 * @param None
307 *
308 * @return None
309 *
310 * @details This function will clear ISP interrupt flag
311 *
312 */
313 #define FMC_CLEAR_ISP_INT_FLAG() (FMC->ISPSTS = FMC_ISPSTS_INTFLAG_Msk) /*!< Clear ISP interrupt flag*/
314
315 /*---------------------------------------------------------------------------------------------------------*/
316 /* inline functions */
317 /*---------------------------------------------------------------------------------------------------------*/
318 __STATIC_INLINE uint32_t FMC_ReadCID(void);
319 __STATIC_INLINE uint32_t FMC_ReadPID(void);
320 __STATIC_INLINE uint32_t FMC_ReadUID(uint8_t u8Index);
321 __STATIC_INLINE uint32_t FMC_ReadUCID(uint32_t u32Index);
322 __STATIC_INLINE void FMC_SetVectorPageAddr(uint32_t u32PageAddr);
323 __STATIC_INLINE uint32_t FMC_GetVECMAP(void);
324
325
326 /**
327 * @brief Get current vector mapping address.
328 *
329 * @param None
330 *
331 * @return The current vector mapping address.
332 *
333 * @details To get VECMAP value which is the page address for remapping to vector page (0x0).
334 *
335 */
FMC_GetVECMAP(void)336 __STATIC_INLINE uint32_t FMC_GetVECMAP(void)
337 {
338 return (FMC->ISPSTS & FMC_ISPSTS_VECMAP_Msk);
339 }
340
341 /**
342 * @brief Read company ID
343 *
344 * @param None
345 *
346 * @return The company ID (32-bit)
347 *
348 * @details The company ID of Nuvoton is fixed to be 0xDA
349 */
FMC_ReadCID(void)350 __STATIC_INLINE uint32_t FMC_ReadCID(void)
351 {
352 FMC->ISPCMD = FMC_ISPCMD_READ_CID; /* Set ISP Command Code */
353 FMC->ISPADDR = 0x0u; /* Must keep 0x0 when read CID */
354 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */
355 #if ISBEN
356 __ISB();
357 #endif /* To make sure ISP/CPU be Synchronized */
358 while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) {} /* Waiting for ISP Done */
359
360 return FMC->ISPDAT;
361 }
362
363 /**
364 * @brief Read product ID
365 *
366 * @param None
367 *
368 * @return The product ID (32-bit)
369 *
370 * @details This function is used to read product ID.
371 */
FMC_ReadPID(void)372 __STATIC_INLINE uint32_t FMC_ReadPID(void)
373 {
374 FMC->ISPCMD = FMC_ISPCMD_READ_DID; /* Set ISP Command Code */
375 FMC->ISPADDR = 0x04u; /* Must keep 0x4 when read PID */
376 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */
377 #if ISBEN
378 __ISB();
379 #endif /* To make sure ISP/CPU be Synchronized */
380 while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) {} /* Waiting for ISP Done */
381
382 return FMC->ISPDAT;
383 }
384
385 /**
386 * @brief Read Unique ID
387 *
388 * @param[in] u8Index UID index. 0 = UID[31:0], 1 = UID[63:32], 2 = UID[95:64]
389 *
390 * @return The 32-bit unique ID data of specified UID index.
391 *
392 * @details To read out 96-bit Unique ID.
393 */
FMC_ReadUID(uint8_t u8Index)394 __STATIC_INLINE uint32_t FMC_ReadUID(uint8_t u8Index)
395 {
396 FMC->ISPCMD = FMC_ISPCMD_READ_UID;
397 FMC->ISPADDR = ((uint32_t)u8Index << 2u);
398 FMC->ISPDAT = 0u;
399 FMC->ISPTRG = 0x1u;
400 #if ISBEN
401 __ISB();
402 #endif
403 while(FMC->ISPTRG) {}
404
405 return FMC->ISPDAT;
406 }
407
408 /**
409 * @brief To read UCID
410 *
411 * @param[in] u32Index Index of the UCID to read. u32Index must be 0, 1, 2, or 3.
412 *
413 * @return The UCID of specified index
414 *
415 * @details This function is used to read unique chip ID (UCID).
416 */
FMC_ReadUCID(uint32_t u32Index)417 __STATIC_INLINE uint32_t FMC_ReadUCID(uint32_t u32Index)
418 {
419 FMC->ISPCMD = FMC_ISPCMD_READ_UID; /* Set ISP Command Code */
420 FMC->ISPADDR = (0x04u * u32Index) + 0x10u; /* The UCID is at offset 0x10 with word alignment. */
421 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */
422 #if ISBEN
423 __ISB();
424 #endif /* To make sure ISP/CPU be Synchronized */
425 while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) {} /* Waiting for ISP Done */
426
427 return FMC->ISPDAT;
428 }
429
430 /**
431 * @brief Set vector mapping address
432 *
433 * @param[in] u32PageAddr The page address to remap to address 0x0. The address must be page alignment.
434 *
435 * @return To set VECMAP to remap specified page address to 0x0.
436 *
437 * @details This function is used to set VECMAP to map specified page to vector page (0x0).
438 */
FMC_SetVectorPageAddr(uint32_t u32PageAddr)439 __STATIC_INLINE void FMC_SetVectorPageAddr(uint32_t u32PageAddr)
440 {
441 FMC->ISPCMD = FMC_ISPCMD_VECMAP; /* Set ISP Command Code */
442 FMC->ISPADDR = u32PageAddr; /* The address of specified page which will be map to address 0x0. It must be page alignment. */
443 FMC->ISPTRG = 0x1u; /* Trigger to start ISP procedure */
444 #if ISBEN
445 __ISB();
446 #endif /* To make sure ISP/CPU be Synchronized */
447 while(FMC->ISPTRG) {} /* Waiting for ISP Done */
448 }
449
450 /*---------------------------------------------------------------------------------------------------------*/
451 /* Functions */
452 /*---------------------------------------------------------------------------------------------------------*/
453
454 extern uint32_t FMC_CheckAllOne(uint32_t u32addr, uint32_t u32count);
455 extern void FMC_Close(void);
456 extern int32_t FMC_ConfigXOM(uint32_t xom_num, uint32_t xom_base, uint8_t xom_page);
457 extern int32_t FMC_Erase(uint32_t u32PageAddr);
458 extern int32_t FMC_Erase_Bank(uint32_t u32BankAddr);
459 extern int32_t FMC_Erase_Block(uint32_t u32BlockAddr);
460 extern int32_t FMC_EraseXOM(uint32_t xom_num);
461 extern int32_t FMC_GetBootSource(void);
462 extern uint32_t FMC_GetChkSum(uint32_t u32addr, uint32_t u32count);
463 extern int32_t FMC_Is_OTP_Locked(uint32_t otp_num);
464 extern int32_t FMC_GetXOMState(uint32_t xom_num);
465 extern int32_t FMC_Lock_OTP(uint32_t otp_num);
466 extern void FMC_Open(void);
467 extern uint32_t FMC_Read(uint32_t u32Addr);
468 extern int32_t FMC_Read_64(uint32_t u32addr, uint32_t * u32data0, uint32_t * u32data1);
469 extern int32_t FMC_Read_OTP(uint32_t otp_num, uint32_t *low_word, uint32_t *high_word);
470 extern int32_t FMC_ReadConfig(uint32_t u32Config[], uint32_t u32Count);
471 extern void FMC_SetBootSource(int32_t i32BootSrc);
472 extern int32_t FMC_CompareSPKey(uint32_t key[3]);
473 extern int32_t FMC_SetSPKey(uint32_t key[3], uint32_t kpmax, uint32_t kemax, const int32_t lock_CONFIG, const int32_t lock_SPROM);
474 extern void FMC_Write(uint32_t u32Addr, uint32_t u32Data);
475 extern int32_t FMC_Write8Bytes(uint32_t u32addr, uint32_t u32data0, uint32_t u32data1);
476 extern int32_t FMC_WriteConfig(uint32_t au32Config[], uint32_t u32Count);
477 extern int32_t FMC_WriteMultiple(uint32_t u32Addr, uint32_t pu32Buf[], uint32_t u32Len);
478 extern int32_t FMC_Write_OTP(uint32_t otp_num, uint32_t low_word, uint32_t high_word);
479 extern int32_t FMC_WriteMultipleA(uint32_t u32Addr, uint32_t pu32Buf[], uint32_t u32Len);
480 /*@}*/ /* end of group FMC_EXPORTED_FUNCTIONS */
481
482 /*@}*/ /* end of group FMC_Driver */
483
484 /*@}*/ /* end of group Standard_Driver */
485
486 #ifdef __cplusplus
487 }
488 #endif
489
490 #endif /* __FMC_H__ */
491
492