1 /**************************************************************************//**
2  * @file     fmc.h
3  * @version  V3.00
4  * @brief    M460 Series Flash Memory Controller Driver Header File
5  *
6  * @copyright SPDX-License-Identifier: Apache-2.0
7  * @copyright Copyright (C) 2021 Nuvoton Technology Corp. All rights reserved.
8  ******************************************************************************/
9 #ifndef __FMC_H__
10 #define __FMC_H__
11 
12 #ifdef __cplusplus
13 extern "C"
14 {
15 #endif
16 
17 /** @addtogroup Standard_Driver Standard Driver
18   @{
19 */
20 
21 /** @addtogroup FMC_Driver FMC Driver
22   @{
23 */
24 
25 
26 /** @addtogroup FMC_EXPORTED_CONSTANTS FMC Exported Constants
27   @{
28 */
29 
30 
31 /*---------------------------------------------------------------------------------------------------------*/
32 /* Define Base Address                                                                                     */
33 /*---------------------------------------------------------------------------------------------------------*/
34 #define FMC_APROM_BASE          0x00000000UL    /*!< APROM base address          \hideinitializer */
35 #define FMC_APROM_END           0x00100000UL    /*!< APROM end address           \hideinitializer */
36 #define FMC_APROM_BANK0_END     (FMC_APROM_END/2UL)  /*!< APROM bank0 end address     \hideinitializer */
37 #define FMC_LDROM_BASE          0x0F100000UL    /*!< LDROM base address          \hideinitializer */
38 #define FMC_LDROM_END           0x0F102000UL    /*!< LDROM end address           \hideinitializer */
39 #define FMC_XOM_BASE            0x0F200000UL    /*!< XOM  Base Address           \hideinitializer */
40 #define FMC_XOMR0_BASE          0x0F200000UL    /*!< XOMR 0 Base Address         \hideinitializer */
41 #define FMC_XOMR1_BASE          0x0F200010UL    /*!< XOMR 1 Base Address         \hideinitializer */
42 #define FMC_XOMR2_BASE          0x0F200020UL    /*!< XOMR 2 Base Address         \hideinitializer */
43 #define FMC_XOMR3_BASE          0x0F200030UL    /*!< XOMR 3 Base Address         \hideinitializer */
44 #define FMC_CONFIG_BASE         0x0F300000UL    /*!< User Configuration address  \hideinitializer */
45 #define FMC_USER_CONFIG_0       0x0F300000UL    /*!< User Config 0 address       \hideinitializer */
46 #define FMC_USER_CONFIG_1       0x0F300004UL    /*!< User Config 1 address       \hideinitializer */
47 #define FMC_USER_CONFIG_2       0x0F300008UL    /*!< User Config 2 address       \hideinitializer */
48 #define FMC_USER_CONFIG_3       0x0F30000CUL    /*!< User Config 2 address       \hideinitializer */
49 #define FMC_OTP_BASE            0x0F310000UL    /*!< OTP flash base address      \hideinitializer */
50 #define FMC_REMAPCFG_BASE       0x0F320000UL    /*!< User Configuration address  \hideinitializer */
51 
52 #define FMC_FLASH_PAGE_SIZE     0x1000UL        /*!< Flash Page Size (4K bytes)  \hideinitializer */
53 #define FMC_PAGE_ADDR_MASK      0xFFFFF000UL    /*!< Flash page address mask     \hideinitializer */
54 #define FMC_MULTI_WORD_PROG_LEN 512             /*!< The maximum length of a multi-word program.  */
55 #define FMC_APPROT_BLOCK_SIZE   0x8000UL        /*!< APROM APPROT size (32K bytes)  \hideinitializer */
56 
57 
58 #define FMC_APROM_SIZE          FMC_APROM_END   /*!< APROM Size                  \hideinitializer */
59 #define FMC_BANK_SIZE           (FMC_APROM_SIZE/2UL) /*!< APROM Bank Size        \hideinitializer */
60 #define FMC_LDROM_SIZE          0x2000UL        /*!< LDROM Size (8 Kbytes)       \hideinitializer */
61 #define FMC_OTP_ENTRY_CNT       256UL           /*!< OTP entry number            \hideinitializer */
62 
63 /*---------------------------------------------------------------------------------------------------------*/
64 /*  XOM region number constant definitions                                                                 */
65 /*---------------------------------------------------------------------------------------------------------*/
66 #define XOMR0   0UL                             /*!< XOM region 0     */
67 #define XOMR1   1UL                             /*!< XOM region 1     */
68 #define XOMR2   2UL                             /*!< XOM region 2     */
69 #define XOMR3   3UL                             /*!< XOM region 3     */
70 
71 /*---------------------------------------------------------------------------------------------------------*/
72 /*  ISPCTL constant definitions                                                                            */
73 /*---------------------------------------------------------------------------------------------------------*/
74 #define IS_BOOT_FROM_LDROM      0x1UL           /*!< ISPCTL setting to select to boot from LDROM */
75 #define IS_BOOT_FROM_APROM      0x0UL           /*!< ISPCTL setting to select to boot from APROM */
76 
77 /*---------------------------------------------------------------------------------------------------------*/
78 /*  ISPCMD constant definitions                                                                            */
79 /*---------------------------------------------------------------------------------------------------------*/
80 #define FMC_ISPCMD_READ         0x00UL          /*!< ISP Command: Read flash word         \hideinitializer */
81 #define FMC_ISPCMD_READ_UID     0x04UL          /*!< ISP Command: Read Unique ID          \hideinitializer */
82 #define FMC_ISPCMD_READ_ALL1    0x08UL          /*!< ISP Command: Read all-one result     \hideinitializer */
83 #define FMC_ISPCMD_READ_CID     0x0BUL          /*!< ISP Command: Read Company ID         \hideinitializer */
84 #define FMC_ISPCMD_READ_DID     0x0CUL          /*!< ISP Command: Read Device ID          \hideinitializer */
85 #define FMC_ISPCMD_READ_CKS     0x0DUL          /*!< ISP Command: Read checksum           \hideinitializer */
86 #define FMC_ISPCMD_PROGRAM      0x21UL          /*!< ISP Command: Write flash word        \hideinitializer */
87 #define FMC_ISPCMD_PAGE_ERASE   0x22UL          /*!< ISP Command: Page Erase Flash        \hideinitializer */
88 #define FMC_ISPCMD_BANK_ERASE   0x23UL          /*!< ISP Command: Erase Flash bank 0 or 1 \hideinitializer */
89 #define FMC_ISPCMD_BLOCK_ERASE  0x25UL          /*!< ISP Command: Erase 4 pages alignment of APROM in bank 0 or 1  \hideinitializer */
90 #define FMC_ISPCMD_PROGRAM_MUL  0x27UL          /*!< ISP Command: Multuple word program   \hideinitializer */
91 #define FMC_ISPCMD_RUN_ALL1     0x28UL          /*!< ISP Command: Run all-one verification \hideinitializer */
92 #define FMC_ISPCMD_BANK_REMAP   0x2CUL          /*!< ISP Command: Bank Remap              \hideinitializer */
93 #define FMC_ISPCMD_RUN_CKS      0x2DUL          /*!< ISP Command: Run checksum calculation \hideinitializer */
94 #define FMC_ISPCMD_VECMAP       0x2EUL          /*!< ISP Command: Vector Page Remap       \hideinitializer */
95 #define FMC_ISPCMD_READ_64      0x40UL          /*!< ISP Command: Read double flash word  \hideinitializer */
96 #define FMC_ISPCMD_PROGRAM_64   0x61UL          /*!< ISP Command: Write double flash word \hideinitializer */
97 
98 
99 #define READ_ALLONE_YES         0xA11FFFFFUL    /*!< Check-all-one result is all one.     \hideinitializer */
100 #define READ_ALLONE_NOT         0xA1100000UL    /*!< Check-all-one result is not all one. \hideinitializer */
101 #define READ_ALLONE_CMD_FAIL    0xFFFFFFFFUL    /*!< Check-all-one command failed.        \hideinitializer */
102 
103 /*---------------------------------------------------------------------------------------------------------*/
104 /* FMC Time-out Handler Constant Definitions                                                               */
105 /*---------------------------------------------------------------------------------------------------------*/
106 #define FMC_TIMEOUT_READ            (SystemCoreClock>>3) /*!< Read command time-out 125 ms        \hideinitializer */
107 #define FMC_TIMEOUT_WRITE           (SystemCoreClock>>3) /*!< Write command time-out 125 ms       \hideinitializer */
108 #define FMC_TIMEOUT_ERASE           (SystemCoreClock>>2) /*!< Erase command time-out 250 ms       \hideinitializer */
109 #define FMC_TIMEOUT_CHKSUM          (SystemCoreClock<<1) /*!< Get checksum command time-out 2 s   \hideinitializer */
110 #define FMC_TIMEOUT_CHKALLONE       (SystemCoreClock<<1) /*!< Check-all-one command time-out 2 s  \hideinitializer */
111 
112 /*@}*/ /* end of group FMC_EXPORTED_CONSTANTS */
113 
114 
115 /** @addtogroup FMC_EXPORTED_MACROS FMC Exported Macros
116   @{
117 */
118 
119 
120 /*---------------------------------------------------------------------------------------------------------*/
121 /*  Macros                                                                                                 */
122 /*---------------------------------------------------------------------------------------------------------*/
123 
124 #define FMC_SET_APROM_BOOT()        (FMC->ISPCTL &= ~FMC_ISPCTL_BS_Msk)                       /*!< Select booting from APROM  \hideinitializer */
125 #define FMC_SET_LDROM_BOOT()        (FMC->ISPCTL |= FMC_ISPCTL_BS_Msk)                        /*!< Select booting from LDROM  \hideinitializer */
126 #define FMC_ENABLE_AP_UPDATE()      (FMC->ISPCTL |=  FMC_ISPCTL_APUEN_Msk)                    /*!< Enable APROM update        \hideinitializer */
127 #define FMC_DISABLE_AP_UPDATE()     (FMC->ISPCTL &= ~FMC_ISPCTL_APUEN_Msk)                    /*!< Disable APROM update       \hideinitializer */
128 #define FMC_ENABLE_CFG_UPDATE()     (FMC->ISPCTL |=  FMC_ISPCTL_CFGUEN_Msk)                   /*!< Enable User Config update  \hideinitializer */
129 #define FMC_DISABLE_CFG_UPDATE()    (FMC->ISPCTL &= ~FMC_ISPCTL_CFGUEN_Msk)                   /*!< Disable User Config update \hideinitializer */
130 #define FMC_ENABLE_LD_UPDATE()      (FMC->ISPCTL |=  FMC_ISPCTL_LDUEN_Msk)                    /*!< Enable LDROM update        \hideinitializer */
131 #define FMC_DISABLE_LD_UPDATE()     (FMC->ISPCTL &= ~FMC_ISPCTL_LDUEN_Msk)                    /*!< Disable LDROM update       \hideinitializer */
132 #define FMC_DISABLE_ISP()           (FMC->ISPCTL &= ~FMC_ISPCTL_ISPEN_Msk)                    /*!< Disable ISP function       \hideinitializer */
133 #define FMC_ENABLE_ISP()            (FMC->ISPCTL |=  FMC_ISPCTL_ISPEN_Msk)                    /*!< Enable ISP function        \hideinitializer */
134 #define FMC_GET_FAIL_FLAG()         ((FMC->ISPCTL & FMC_ISPCTL_ISPFF_Msk) ? 1UL : 0UL)        /*!< Get ISP fail flag          \hideinitializer */
135 #define FMC_CLR_FAIL_FLAG()         (FMC->ISPCTL |= FMC_ISPCTL_ISPFF_Msk)                     /*!< Clear ISP fail flag        \hideinitializer */
136 #define FMC_ENABLE_APPROT(u8Block)  (FMC->APPROT |= (1ul << u8Block))                         /*!< Enable APPROT Block        \hideinitializer */
137 #define FMC_DISABLE_APPROT(u8Block) (FMC->APPROT &= ~(1ul << u8Block))                        /*!< Disable APPROT Block       \hideinitializer */
138 /*@}*/ /* end of group FMC_EXPORTED_MACROS */
139 
140 /*---------------------------------------------------------------------------------------------------------*/
141 /*  Global variables                                                                                       */
142 /*---------------------------------------------------------------------------------------------------------*/
143 extern int32_t  g_FMC_i32ErrCode;
144 
145 /** @addtogroup FMC_EXPORTED_FUNCTIONS FMC Exported Functions
146   @{
147 */
148 
149 /*---------------------------------------------------------------------------------------------------------*/
150 /* inline functions                                                                                        */
151 /*---------------------------------------------------------------------------------------------------------*/
152 
153 __STATIC_INLINE uint32_t FMC_ReadCID(void);
154 __STATIC_INLINE uint32_t FMC_ReadPID(void);
155 __STATIC_INLINE uint32_t FMC_ReadUID(uint8_t u8Index);
156 __STATIC_INLINE uint32_t FMC_ReadUCID(uint32_t u32Index);
157 __STATIC_INLINE int32_t FMC_SetVectorPageAddr(uint32_t u32PageAddr);
158 __STATIC_INLINE uint32_t FMC_GetVECMAP(void);
159 
160 /**
161  * @brief       Get current vector mapping address.
162  * @param       None
163  * @return      The current vector mapping address.
164  * @details     To get VECMAP value which is the page address for remapping to vector page (0x0).
165  * @note
166  *              VECMAP only valid when new IAP function is enabled. (CBS = 10'b or 00'b)
167  */
FMC_GetVECMAP(void)168 __STATIC_INLINE uint32_t FMC_GetVECMAP(void)
169 {
170     return (FMC->ISPSTS & FMC_ISPSTS_VECMAP_Msk);
171 }
172 
173 /**
174   * @brief    Read company ID
175   * @param    None
176   * @return   The company ID (32-bit). 0xFFFFFFFF means read failed.
177   * @details  The company ID of Nuvoton is fixed to be 0x530000DA
178   *
179   * @note     Global error code g_FMC_i32ErrCode
180   *           -1  Read time-out
181   */
FMC_ReadCID(void)182 __STATIC_INLINE uint32_t FMC_ReadCID(void)
183 {
184     int32_t i32TimeOutCnt = FMC_TIMEOUT_READ;
185 
186     g_FMC_i32ErrCode = 0;
187 
188     FMC->ISPCMD = FMC_ISPCMD_READ_CID;           /* Set ISP Command Code */
189     FMC->ISPADDR = 0x0u;                         /* Must keep 0x0 when read CID */
190     FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;          /* Trigger to start ISP procedure */
191     while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)    /* Waiting for ISP Done */
192     {
193         if( i32TimeOutCnt-- <= 0)
194         {
195             g_FMC_i32ErrCode = -1;
196             return 0xFFFFFFFF;
197         }
198     }
199 
200     return FMC->ISPDAT;
201 }
202 
203 /**
204   * @brief    Read product ID
205   * @param    None
206   * @return   The product ID (32-bit). 0xFFFFFFFF means read failed.
207   * @details  This function is used to read product ID.
208   *
209   * @note     Global error code g_FMC_i32ErrCode
210   *           -1  Read time-out
211   */
FMC_ReadPID(void)212 __STATIC_INLINE uint32_t FMC_ReadPID(void)
213 {
214     int32_t i32TimeOutCnt = FMC_TIMEOUT_READ;
215 
216     g_FMC_i32ErrCode = 0;
217 
218     FMC->ISPCMD = FMC_ISPCMD_READ_DID;          /* Set ISP Command Code */
219     FMC->ISPADDR = 0x04u;                       /* Must keep 0x4 when read PID */
220     FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;         /* Trigger to start ISP procedure */
221     while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)   /* Waiting for ISP Done */
222     {
223         if( i32TimeOutCnt-- <= 0)
224         {
225             g_FMC_i32ErrCode = -1;
226             return 0xFFFFFFFF;
227         }
228     }
229 
230     return FMC->ISPDAT;
231 }
232 
233 /**
234  * @brief       Read Unique ID
235  * @param[in]   u8Index  UID index. 0 = UID[31:0], 1 = UID[63:32], 2 = UID[95:64]
236  * @return      The 32-bit unique ID data of specified UID index. 0xFFFFFFFF means read failed.
237  * @details     To read out 96-bit Unique ID.
238  *
239  * @note        Global error code g_FMC_i32ErrCode
240  *              -1  Read time-out
241  */
FMC_ReadUID(uint8_t u8Index)242 __STATIC_INLINE uint32_t FMC_ReadUID(uint8_t u8Index)
243 {
244     int32_t i32TimeOutCnt = FMC_TIMEOUT_READ;
245 
246     g_FMC_i32ErrCode = 0;
247 
248     FMC->ISPCMD = FMC_ISPCMD_READ_UID;
249     FMC->ISPADDR = ((uint32_t)u8Index << 2u);
250     FMC->ISPDAT = 0u;
251     FMC->ISPTRG = 0x1u;
252     while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)   /* Waiting for ISP Done */
253     {
254         if( i32TimeOutCnt-- <= 0)
255         {
256             g_FMC_i32ErrCode = -1;
257             return 0xFFFFFFFF;
258         }
259     }
260 
261     return FMC->ISPDAT;
262 }
263 
264 /**
265   * @brief      To read UCID
266   * @param[in]  u32Index    Index of the UCID to read. u32Index must be 0, 1, 2, or 3.
267   * @return     The UCID of specified index
268   * @details    This function is used to read unique chip ID (UCID). 0xFFFFFFFF means read failed.
269   *
270   * @note       Global error code g_FMC_i32ErrCode
271   *             -1  Read time-out
272   */
FMC_ReadUCID(uint32_t u32Index)273 __STATIC_INLINE uint32_t FMC_ReadUCID(uint32_t u32Index)
274 {
275     int32_t i32TimeOutCnt = FMC_TIMEOUT_READ;
276 
277     g_FMC_i32ErrCode = 0;
278 
279     FMC->ISPCMD = FMC_ISPCMD_READ_UID;            /* Set ISP Command Code */
280     FMC->ISPADDR = (0x04u * u32Index) + 0x10u;    /* The UCID is at offset 0x10 with word alignment. */
281     FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;           /* Trigger to start ISP procedure */
282     while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk)     /* Waiting for ISP Done */
283     {
284         if( i32TimeOutCnt-- <= 0)
285         {
286             g_FMC_i32ErrCode = -1;
287             return 0xFFFFFFFF;
288         }
289     }
290 
291     return FMC->ISPDAT;
292 }
293 
294 /**
295  * @brief       Set vector mapping address
296  * @param[in]   u32PageAddr  The page address to remap to address 0x0. The address must be page alignment.
297  * @return      To set VECMAP to remap specified page address to 0x0.
298  * @details     This function is used to set VECMAP to map specified page to vector page (0x0).
299  * @retval      0   Success
300  * @retval      -1  Failed
301  * @note
302  *              VECMAP only valid when new IAP function is enabled. (CBS = 10'b or 00'b)
303  *
304  * @note        Global error code g_FMC_i32ErrCode
305  *              -1  Command time-out
306  */
FMC_SetVectorPageAddr(uint32_t u32PageAddr)307 __STATIC_INLINE int32_t FMC_SetVectorPageAddr(uint32_t u32PageAddr)
308 {
309     int32_t i32TimeOutCnt = FMC_TIMEOUT_WRITE;
310 
311     g_FMC_i32ErrCode = 0;
312 
313     FMC->ISPCMD = FMC_ISPCMD_VECMAP;  /* Set ISP Command Code */
314     FMC->ISPADDR = u32PageAddr;       /* The address of specified page which will be map to address 0x0. It must be page alignment. */
315     FMC->ISPTRG = 0x1u;               /* Trigger to start ISP procedure */
316     while(FMC->ISPTRG)                /* Waiting for ISP Done */
317     {
318         if( i32TimeOutCnt-- <= 0)
319         {
320             g_FMC_i32ErrCode = -1;
321             return -1;
322         }
323     }
324 
325     return 0;
326 }
327 
328 
329 /*---------------------------------------------------------------------------------------------------------*/
330 /*  Functions                                                                                              */
331 /*---------------------------------------------------------------------------------------------------------*/
332 
333 extern void     FMC_Close(void);
334 extern int32_t  FMC_ConfigXOM(uint32_t xom_num, uint32_t xom_base, uint8_t xom_page);
335 extern int32_t  FMC_Erase(uint32_t u32PageAddr);
336 extern int32_t  FMC_Erase_Bank(uint32_t u32BankAddr);
337 extern int32_t  FMC_EraseXOM(uint32_t xom_num);
338 extern int32_t  FMC_GetXOMState(uint32_t xom_num);
339 extern int32_t  FMC_GetBootSource(void);
340 extern void     FMC_Open(void);
341 extern uint32_t FMC_Read(uint32_t u32Addr);
342 extern int32_t  FMC_Read_64(uint32_t u32addr, uint32_t * u32data0, uint32_t * u32data1);
343 extern uint32_t FMC_ReadDataFlashBaseAddr(void);
344 extern void     FMC_SetBootSource(int32_t i32BootSrc);
345 extern int32_t  FMC_Write(uint32_t u32Addr, uint32_t u32Data);
346 extern int32_t  FMC_Write8Bytes(uint32_t u32addr, uint32_t u32data0, uint32_t u32data1);
347 extern int32_t  FMC_WriteMultiple(uint32_t u32Addr, uint32_t pu32Buf[], uint32_t u32Len);
348 extern int32_t  FMC_WriteOTP(uint32_t otp_num, uint32_t low_word, uint32_t high_word);
349 extern int32_t  FMC_ReadOTP(uint32_t otp_num, uint32_t *low_word, uint32_t *high_word);
350 extern int32_t  FMC_LockOTP(uint32_t otp_num);
351 extern int32_t  FMC_IsOTPLocked(uint32_t otp_num);
352 extern int32_t  FMC_ReadConfig(uint32_t u32Config[], uint32_t u32Count);
353 extern int32_t  FMC_WriteConfig(uint32_t u32Config[], uint32_t u32Count);
354 extern uint32_t FMC_GetChkSum(uint32_t u32addr, uint32_t u32count);
355 extern uint32_t FMC_CheckAllOne(uint32_t u32addr, uint32_t u32count);
356 extern int32_t  FMC_RemapBank(uint32_t u32Bank);
357 
358 
359 /*@}*/ /* end of group FMC_EXPORTED_FUNCTIONS */
360 
361 /*@}*/ /* end of group FMC_Driver */
362 
363 /*@}*/ /* end of group Standard_Driver */
364 
365 #ifdef __cplusplus
366 }
367 #endif
368 
369 #endif   /* __FMC_H__ */
370