1 /**************************************************************************//**
2  * @file     fmc.h
3  * @version  V1.00
4  * @brief    M480 Series Flash Memory Controller Driver Header File
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  * @copyright (C) 2016-2020 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           0x00080000UL    /*!< APROM end address           \hideinitializer */
36 #define FMC_APROM_BANK0_END     (FMC_APROM_END/2UL)  /*!< APROM bank0 end address     \hideinitializer */
37 #define FMC_LDROM_BASE          0x00100000UL    /*!< LDROM base address          \hideinitializer */
38 #define FMC_LDROM_END           0x00101000UL    /*!< LDROM end address           \hideinitializer */
39 #define FMC_SPROM_BASE          0x00200000UL    /*!< SPROM base address          \hideinitializer */
40 #define FMC_SPROM_END           0x00201000UL    /*!< SPROM end address           \hideinitializer */
41 #define FMC_XOM_BASE            0x00200000UL    /*!< XOM  Base Address           \hideinitializer */
42 #define FMC_XOMR0_BASE          0x00200000UL    /*!< XOMR 0 Base Address         \hideinitializer */
43 #define FMC_XOMR1_BASE          0x00200010UL    /*!< XOMR 1 Base Address         \hideinitializer */
44 #define FMC_XOMR2_BASE          0x00200020UL    /*!< XOMR 2 Base Address         \hideinitializer */
45 #define FMC_XOMR3_BASE          0x00200030UL    /*!< XOMR 3 Base Address         \hideinitializer */
46 #define FMC_CONFIG_BASE         0x00300000UL    /*!< User Configuration address  \hideinitializer */
47 #define FMC_USER_CONFIG_0       0x00300000UL    /*!< User Config 0 address       \hideinitializer */
48 #define FMC_USER_CONFIG_1       0x00300004UL    /*!< User Config 1 address       \hideinitializer */
49 #define FMC_USER_CONFIG_2       0x00300008UL    /*!< User Config 2 address       \hideinitializer */
50 #define FMC_KPROM_BASE          0x00301000UL    /*!< Security ROM base address   \hideinitializer */
51 #define FMC_OTP_BASE            0x00310000UL    /*!< OTP flash base address      \hideinitializer */
52 
53 #define FMC_FLASH_PAGE_SIZE     0x1000UL        /*!< Flash Page Size (4K bytes)  \hideinitializer */
54 #define FMC_PAGE_ADDR_MASK      0xFFFFF000UL    /*!< Flash page address mask     \hideinitializer */
55 #define FMC_MULTI_WORD_PROG_LEN 512             /*!< The maximum length of a multi-word program.  */
56 
57 #define FMC_APROM_SIZE          FMC_APROM_END   /*!< APROM Size                  \hideinitializer */
58 #define FMC_BANK_SIZE           (FMC_APROM_SIZE/2UL) /*!< APROM Bank Size        \hideinitializer */
59 #define FMC_LDROM_SIZE          0x1000UL        /*!< LDROM Size (4 Kbytes)       \hideinitializer */
60 #define FMC_SPROM_SIZE          0x1000UL        /*!< SPROM Size (4 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_RUN_CKS      0x2DUL          /*!< ISP Command: Run checksum calculation \hideinitializer */
93 #define FMC_ISPCMD_VECMAP       0x2EUL          /*!< ISP Command: Vector Page Remap       \hideinitializer */
94 #define FMC_ISPCMD_READ_64      0x40UL          /*!< ISP Command: Read double flash word  \hideinitializer */
95 #define FMC_ISPCMD_PROGRAM_64   0x61UL          /*!< ISP Command: Write double flash word \hideinitializer */
96 
97 #define READ_ALLONE_YES         0xA11FFFFFUL    /*!< Check-all-one result is all one.     \hideinitializer */
98 #define READ_ALLONE_NOT         0xA1100000UL    /*!< Check-all-one result is not all one. \hideinitializer */
99 #define READ_ALLONE_CMD_FAIL    0xFFFFFFFFUL    /*!< Check-all-one command failed.        \hideinitializer */
100 
101 
102 /*@}*/ /* end of group FMC_EXPORTED_CONSTANTS */
103 
104 
105 /** @addtogroup FMC_EXPORTED_MACROS FMC Exported Macros
106   @{
107 */
108 
109 
110 /*---------------------------------------------------------------------------------------------------------*/
111 /*  Macros                                                                                                 */
112 /*---------------------------------------------------------------------------------------------------------*/
113 
114 #define FMC_SET_APROM_BOOT()        (FMC->ISPCTL &= ~FMC_ISPCTL_BS_Msk)         /*!< Select booting from APROM  \hideinitializer */
115 #define FMC_SET_LDROM_BOOT()        (FMC->ISPCTL |= FMC_ISPCTL_BS_Msk)          /*!< Select booting from LDROM  \hideinitializer */
116 #define FMC_ENABLE_AP_UPDATE()      (FMC->ISPCTL |=  FMC_ISPCTL_APUEN_Msk)      /*!< Enable APROM update        \hideinitializer */
117 #define FMC_DISABLE_AP_UPDATE()     (FMC->ISPCTL &= ~FMC_ISPCTL_APUEN_Msk)      /*!< Disable APROM update       \hideinitializer */
118 #define FMC_ENABLE_CFG_UPDATE()     (FMC->ISPCTL |=  FMC_ISPCTL_CFGUEN_Msk)     /*!< Enable User Config update  \hideinitializer */
119 #define FMC_DISABLE_CFG_UPDATE()    (FMC->ISPCTL &= ~FMC_ISPCTL_CFGUEN_Msk)     /*!< Disable User Config update \hideinitializer */
120 #define FMC_ENABLE_LD_UPDATE()      (FMC->ISPCTL |=  FMC_ISPCTL_LDUEN_Msk)      /*!< Enable LDROM update        \hideinitializer */
121 #define FMC_DISABLE_LD_UPDATE()     (FMC->ISPCTL &= ~FMC_ISPCTL_LDUEN_Msk)      /*!< Disable LDROM update       \hideinitializer */
122 #define FMC_ENABLE_SP_UPDATE()      (FMC->ISPCTL |=  FMC_ISPCTL_SPUEN_Msk)      /*!< Enable SPROM update        \hideinitializer */
123 #define FMC_DISABLE_SP_UPDATE()     (FMC->ISPCTL &= ~FMC_ISPCTL_SPUEN_Msk)      /*!< Disable SPROM update       \hideinitializer */
124 #define FMC_DISABLE_ISP()           (FMC->ISPCTL &= ~FMC_ISPCTL_ISPEN_Msk)      /*!< Disable ISP function       \hideinitializer */
125 #define FMC_ENABLE_ISP()            (FMC->ISPCTL |=  FMC_ISPCTL_ISPEN_Msk)      /*!< Enable ISP function        \hideinitializer */
126 #define FMC_GET_FAIL_FLAG()         ((FMC->ISPCTL & FMC_ISPCTL_ISPFF_Msk) ? 1UL : 0UL)  /*!< Get ISP fail flag  \hideinitializer */
127 #define FMC_CLR_FAIL_FLAG()         (FMC->ISPCTL |= FMC_ISPCTL_ISPFF_Msk)       /*!< Clear ISP fail flag        \hideinitializer */
128 
129 /*@}*/ /* end of group FMC_EXPORTED_MACROS */
130 
131 
132 /** @addtogroup FMC_EXPORTED_FUNCTIONS FMC Exported Functions
133   @{
134 */
135 
136 /*---------------------------------------------------------------------------------------------------------*/
137 /* inline functions                                                                                        */
138 /*---------------------------------------------------------------------------------------------------------*/
139 
140 __STATIC_INLINE uint32_t FMC_ReadCID(void);
141 __STATIC_INLINE uint32_t FMC_ReadPID(void);
142 __STATIC_INLINE uint32_t FMC_ReadUID(uint8_t u8Index);
143 __STATIC_INLINE uint32_t FMC_ReadUCID(uint32_t u32Index);
144 __STATIC_INLINE void FMC_SetVectorPageAddr(uint32_t u32PageAddr);
145 __STATIC_INLINE uint32_t FMC_GetVECMAP(void);
146 
147 /**
148  * @brief       Get current vector mapping address.
149  * @param       None
150  * @return      The current vector mapping address.
151  * @details     To get VECMAP value which is the page address for remapping to vector page (0x0).
152  * @note
153  *              VECMAP only valid when new IAP function is enabled. (CBS = 10'b or 00'b)
154  */
FMC_GetVECMAP(void)155 __STATIC_INLINE uint32_t FMC_GetVECMAP(void)
156 {
157     return (FMC->ISPSTS & FMC_ISPSTS_VECMAP_Msk);
158 }
159 
160 /**
161   * @brief    Read company ID
162   * @param    None
163   * @return   The company ID (32-bit)
164   * @details  The company ID of Nuvoton is fixed to be 0xDA
165   */
FMC_ReadCID(void)166 __STATIC_INLINE uint32_t FMC_ReadCID(void)
167 {
168     FMC->ISPCMD = FMC_ISPCMD_READ_CID;           /* Set ISP Command Code */
169     FMC->ISPADDR = 0x0u;                         /* Must keep 0x0 when read CID */
170     FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;          /* Trigger to start ISP procedure */
171 #if ISBEN
172     __ISB();
173 #endif                                           /* To make sure ISP/CPU be Synchronized */
174     while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) {} /* Waiting for ISP Done */
175 
176     return FMC->ISPDAT;
177 }
178 
179 /**
180   * @brief    Read product ID
181   * @param    None
182   * @return   The product ID (32-bit)
183   * @details  This function is used to read product ID.
184   */
FMC_ReadPID(void)185 __STATIC_INLINE uint32_t FMC_ReadPID(void)
186 {
187     FMC->ISPCMD = FMC_ISPCMD_READ_DID;          /* Set ISP Command Code */
188     FMC->ISPADDR = 0x04u;                       /* Must keep 0x4 when read PID */
189     FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;         /* Trigger to start ISP procedure */
190 #if ISBEN
191     __ISB();
192 #endif                                          /* To make sure ISP/CPU be Synchronized */
193     while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) {} /* Waiting for ISP Done */
194 
195     return FMC->ISPDAT;
196 }
197 
198 /**
199  * @brief       Read Unique ID
200  * @param[in]   u8Index  UID index. 0 = UID[31:0], 1 = UID[63:32], 2 = UID[95:64]
201  * @return      The 32-bit unique ID data of specified UID index.
202  * @details     To read out 96-bit Unique ID.
203  */
FMC_ReadUID(uint8_t u8Index)204 __STATIC_INLINE uint32_t FMC_ReadUID(uint8_t u8Index)
205 {
206     FMC->ISPCMD = FMC_ISPCMD_READ_UID;
207     FMC->ISPADDR = ((uint32_t)u8Index << 2u);
208     FMC->ISPDAT = 0u;
209     FMC->ISPTRG = 0x1u;
210 #if ISBEN
211     __ISB();
212 #endif
213     while(FMC->ISPTRG) {}
214 
215     return FMC->ISPDAT;
216 }
217 
218 /**
219   * @brief      To read UCID
220   * @param[in]  u32Index    Index of the UCID to read. u32Index must be 0, 1, 2, or 3.
221   * @return     The UCID of specified index
222   * @details    This function is used to read unique chip ID (UCID).
223   */
FMC_ReadUCID(uint32_t u32Index)224 __STATIC_INLINE uint32_t FMC_ReadUCID(uint32_t u32Index)
225 {
226     FMC->ISPCMD = FMC_ISPCMD_READ_UID;            /* Set ISP Command Code */
227     FMC->ISPADDR = (0x04u * u32Index) + 0x10u;    /* The UCID is at offset 0x10 with word alignment. */
228     FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;           /* Trigger to start ISP procedure */
229 #if ISBEN
230     __ISB();
231 #endif                                            /* To make sure ISP/CPU be Synchronized */
232     while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) {}  /* Waiting for ISP Done */
233 
234     return FMC->ISPDAT;
235 }
236 
237 /**
238  * @brief       Set vector mapping address
239  * @param[in]   u32PageAddr  The page address to remap to address 0x0. The address must be page alignment.
240  * @return      To set VECMAP to remap specified page address to 0x0.
241  * @details     This function is used to set VECMAP to map specified page to vector page (0x0).
242  * @note
243  *              VECMAP only valid when new IAP function is enabled. (CBS = 10'b or 00'b)
244  */
FMC_SetVectorPageAddr(uint32_t u32PageAddr)245 __STATIC_INLINE void FMC_SetVectorPageAddr(uint32_t u32PageAddr)
246 {
247     FMC->ISPCMD = FMC_ISPCMD_VECMAP;  /* Set ISP Command Code */
248     FMC->ISPADDR = u32PageAddr;       /* The address of specified page which will be map to address 0x0. It must be page alignment. */
249     FMC->ISPTRG = 0x1u;               /* Trigger to start ISP procedure */
250 #if ISBEN
251     __ISB();
252 #endif                                /* To make sure ISP/CPU be Synchronized */
253     while(FMC->ISPTRG) {}             /* Waiting for ISP Done */
254 }
255 
256 
257 /*---------------------------------------------------------------------------------------------------------*/
258 /*  Functions                                                                                              */
259 /*---------------------------------------------------------------------------------------------------------*/
260 
261 extern void     FMC_Close(void);
262 extern int32_t  FMC_ConfigXOM(uint32_t xom_num, uint32_t xom_base, uint8_t xom_page);
263 extern int32_t  FMC_Erase(uint32_t u32PageAddr);
264 extern int32_t  FMC_Erase_SPROM(void);
265 extern int32_t  FMC_Erase_Block(uint32_t u32BlockAddr);
266 extern int32_t  FMC_Erase_Bank(uint32_t u32BankAddr);
267 extern int32_t  FMC_EraseXOM(uint32_t xom_num);
268 extern int32_t  FMC_GetXOMState(uint32_t xom_num);
269 extern int32_t  FMC_GetBootSource(void);
270 extern void     FMC_Open(void);
271 extern uint32_t FMC_Read(uint32_t u32Addr);
272 extern int32_t  FMC_Read_64(uint32_t u32addr, uint32_t * u32data0, uint32_t * u32data1);
273 extern uint32_t FMC_ReadDataFlashBaseAddr(void);
274 extern void     FMC_SetBootSource(int32_t i32BootSrc);
275 extern void     FMC_Write(uint32_t u32Addr, uint32_t u32Data);
276 extern int32_t  FMC_Write8Bytes(uint32_t u32addr, uint32_t u32data0, uint32_t u32data1);
277 extern int32_t  FMC_WriteMultiple(uint32_t u32Addr, uint32_t pu32Buf[], uint32_t u32Len);
278 extern int32_t  FMC_Write_OTP(uint32_t otp_num, uint32_t low_word, uint32_t high_word);
279 extern int32_t  FMC_Read_OTP(uint32_t otp_num, uint32_t *low_word, uint32_t *high_word);
280 extern int32_t  FMC_Lock_OTP(uint32_t otp_num);
281 extern int32_t  FMC_Is_OTP_Locked(uint32_t otp_num);
282 extern int32_t  FMC_ReadConfig(uint32_t u32Config[], uint32_t u32Count);
283 extern int32_t  FMC_WriteConfig(uint32_t u32Config[], uint32_t u32Count);
284 extern uint32_t FMC_GetChkSum(uint32_t u32addr, uint32_t u32count);
285 extern uint32_t FMC_CheckAllOne(uint32_t u32addr, uint32_t u32count);
286 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);
287 extern int32_t  FMC_CompareSPKey(uint32_t key[3]);
288 
289 
290 /*@}*/ /* end of group FMC_EXPORTED_FUNCTIONS */
291 
292 /*@}*/ /* end of group FMC_Driver */
293 
294 /*@}*/ /* end of group Standard_Driver */
295 
296 #ifdef __cplusplus
297 }
298 #endif
299 
300 #endif   /* __FMC_H__ */
301 
302 /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
303