1 /**************************************************************************//**
\r
5 * $Date: 16/07/29 3:11p $
\r
6 * @brief M2351 Series Flash Memory Controller(FMC) driver header file
\r
9 * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
\r
11 ******************************************************************************/
\r
21 /** @addtogroup Standard_Driver Standard Driver
\r
25 /** @addtogroup FMC_Driver FMC Driver
\r
29 /** @addtogroup FMC_EXPORTED_CONSTANTS FMC Exported Constants
\r
33 /*---------------------------------------------------------------------------------------------------------*/
\r
34 /* Global constant definitions */
\r
35 /*---------------------------------------------------------------------------------------------------------*/
\r
38 /*---------------------------------------------------------------------------------------------------------*/
\r
39 /* Define Base Address */
\r
40 /*---------------------------------------------------------------------------------------------------------*/
\r
41 #define FMC_APROM_BASE 0x00000000UL /*!< APROM Base Address */
\r
42 #define FMC_APROM_END 0x00080000UL /*!< APROM end address */
\r
43 #define FMC_APROM_BANK0_END (FMC_APROM_END/2UL) /*!< APROM bank0 end address */
\r
44 #define FMC_LDROM_BASE 0x00100000UL /*!< LDROM Base Address */
\r
45 #define FMC_LDROM_END 0x00101000UL /*!< LDROM end address */
\r
46 #define FMC_XOM_BASE 0x00200000UL /*!< XOM Base Address */
\r
47 #define FMC_XOMR0_BASE 0x00200000UL /*!< XOMR 0 Base Address */
\r
48 #define FMC_XOMR1_BASE 0x00200010UL /*!< XOMR 1 Base Address */
\r
49 #define FMC_XOMR2_BASE 0x00200020UL /*!< XOMR 2 Base Address */
\r
50 #define FMC_XOMR3_BASE 0x00200030UL /*!< XOMR 3 Base Address */
\r
51 #define FMC_NSCBA_BASE 0x00200800UL /*!< Non-Secure base address */
\r
52 #define FMC_SCRLOCK_BASE 0x00200804UL /*!< Secure Region Lock base address */
\r
53 #define FMC_ARLOCK_BASE 0x00210804UL /*!< All Region Lock base address */
\r
54 #define FMC_CONFIG_BASE 0x00300000UL /*!< CONFIG Base Address */
\r
55 #define FMC_USER_CONFIG_0 0x00300000UL /*!< CONFIG 0 Address */
\r
56 #define FMC_USER_CONFIG_1 0x00300004UL /*!< CONFIG 1 Address */
\r
57 #define FMC_USER_CONFIG_2 0x00300008UL /*!< CONFIG 2 Address */
\r
58 #define FMC_USER_CONFIG_3 0x0030000CUL /*!< CONFIG 3 Address */
\r
59 #define FMC_OTP_BASE 0x00310000UL /*!< OTP flash base address */
\r
60 #define FMC_KPROM_BASE 0x00311000UL /*!< Security ROM base address */
\r
62 #define FMC_FLASH_PAGE_SIZE 0x800UL /*!< Flash Page Size (2048 Bytes) */
\r
63 #define FMC_PAGE_ADDR_MASK 0xFFFFF800UL /*!< Flash page address mask */
\r
64 #define FMC_MULTI_WORD_PROG_LEN 512UL /*!< The maximum length of a multi-word program. */
\r
66 #define FMC_APROM_SIZE FMC_APROM_END /*!< APROM Size */
\r
67 #define FMC_BANK_SIZE (FMC_APROM_SIZE/2UL) /*!< APROM Bank Size */
\r
68 #define FMC_LDROM_SIZE 0x1000UL /*!< LDROM Size (4 Kbytes) */
\r
69 #define FMC_OTP_ENTRY_CNT 256UL /*!< OTP entry number */
\r
71 /*---------------------------------------------------------------------------------------------------------*/
\r
72 /* XOM region number constant definitions */
\r
73 /*---------------------------------------------------------------------------------------------------------*/
\r
74 #define XOMR0 0UL /*!< XOM region 0 */
\r
75 #define XOMR1 1UL /*!< XOM region 1 */
\r
76 #define XOMR2 2UL /*!< XOM region 2 */
\r
77 #define XOMR3 3UL /*!< XOM region 3 */
\r
79 /*---------------------------------------------------------------------------------------------------------*/
\r
80 /* ISPCTL constant definitions */
\r
81 /*---------------------------------------------------------------------------------------------------------*/
\r
82 #define IS_BOOT_FROM_LDROM 0x1UL /*!< ISPCTL setting to select to boot from LDROM */
\r
83 #define IS_BOOT_FROM_APROM 0x0UL /*!< ISPCTL setting to select to boot from APROM */
\r
85 /*---------------------------------------------------------------------------------------------------------*/
\r
86 /* ISPCMD constant definitions */
\r
87 /*---------------------------------------------------------------------------------------------------------*/
\r
88 #define FMC_ISPCMD_READ 0x00UL /*!< ISP Command: Read Flash */
\r
89 #define FMC_ISPCMD_READ_UID 0x04UL /*!< ISP Command: Read Unique ID */
\r
90 #define FMC_ISPCMD_READ_ALL1 0x08UL /*!< ISP Command: Read all-one result */
\r
91 #define FMC_ISPCMD_READ_CID 0x0BUL /*!< ISP Command: Read Company ID */
\r
92 #define FMC_ISPCMD_READ_DID 0x0CUL /*!< ISP Command: Read Device ID */
\r
93 #define FMC_ISPCMD_READ_CKS 0x0DUL /*!< ISP Command: Read Checksum */
\r
94 #define FMC_ISPCMD_PROGRAM 0x21UL /*!< ISP Command: 32-bit Program Flash */
\r
95 #define FMC_ISPCMD_PAGE_ERASE 0x22UL /*!< ISP Command: Page Erase Flash */
\r
96 #define FMC_ISPCMD_BANK_ERASE 0x23UL /*!< ISP Command: Erase Flash bank 0 or 1 */
\r
97 #define FMC_ISPCMD_BLOCK_ERASE 0x25UL /*!< ISP Command: Erase 4 pages alignment of APROM in bank 0 or 1 */
\r
98 #define FMC_ISPCMD_PROGRAM_MUL 0x27UL /*!< ISP Command: Flash Multi-Word Program */
\r
99 #define FMC_ISPCMD_RUN_ALL1 0x28UL /*!< ISP Command: Run all-one verification*/
\r
100 #define FMC_ISPCMD_RUN_CKS 0x2DUL /*!< ISP Command: Run Check Calculation */
\r
101 #define FMC_ISPCMD_VECMAP 0x2EUL /*!< ISP Command: Set vector mapping */
\r
102 #define FMC_ISPCMD_READ_64 0x40UL /*!< ISP Command: 64-bit read Flash */
\r
103 #define FMC_ISPCMD_PROGRAM_64 0x61UL /*!< ISP Command: 64-bit program Flash */
\r
105 #define READ_ALLONE_YES 0xA11FFFFFUL /*!< Check-all-one result is all one. */
\r
106 #define READ_ALLONE_NOT 0xA1100000UL /*!< Check-all-one result is not all one. */
\r
107 #define READ_ALLONE_CMD_FAIL 0xFFFFFFFFUL /*!< Check-all-one command failed. */
\r
109 /*@}*/ /* end of group FMC_EXPORTED_CONSTANTS */
\r
111 /** @addtogroup FMC_EXPORTED_FUNCTIONS FMC Exported Functions
\r
115 /*---------------------------------------------------------------------------------------------------------*/
\r
116 /* FMC Macro Definitions */
\r
117 /*---------------------------------------------------------------------------------------------------------*/
\r
119 * @brief Enable ISP Function
\r
125 * @details This function will set ISPEN bit of ISPCTL control register to enable ISP function.
\r
128 #define FMC_ENABLE_ISP() (FMC->ISPCTL |= FMC_ISPCTL_ISPEN_Msk) /*!< Enable ISP Function */
\r
131 * @brief Disable ISP Function
\r
137 * @details This function will clear ISPEN bit of ISPCTL control register to disable ISP function.
\r
140 #define FMC_DISABLE_ISP() (FMC->ISPCTL &= ~FMC_ISPCTL_ISPEN_Msk) /*!< Disable ISP Function */
\r
143 * @brief Enable LDROM Update Function
\r
149 * @details This function will set LDUEN bit of ISPCTL control register to enable LDROM update function.
\r
150 * User needs to set LDUEN bit before they can update LDROM.
\r
153 #define FMC_ENABLE_LD_UPDATE() (FMC->ISPCTL |= FMC_ISPCTL_LDUEN_Msk) /*!< Enable LDROM Update Function */
\r
156 * @brief Disable LDROM Update Function
\r
162 * @details This function will set ISPEN bit of ISPCTL control register to disable LDROM update function.
\r
165 #define FMC_DISABLE_LD_UPDATE() (FMC->ISPCTL &= ~FMC_ISPCTL_LDUEN_Msk) /*!< Disable LDROM Update Function */
\r
168 * @brief Enable User Configuration Update Function
\r
174 * @details This function will set CFGUEN bit of ISPCTL control register to enable User Configuration update function.
\r
175 * User needs to set CFGUEN bit before they can update User Configuration area.
\r
178 #define FMC_ENABLE_CFG_UPDATE() (FMC->ISPCTL |= FMC_ISPCTL_CFGUEN_Msk) /*!< Enable CONFIG Update Function */
\r
181 * @brief Disable User Configuration Update Function
\r
187 * @details This function will clear CFGUEN bit of ISPCTL control register to disable User Configuration update function.
\r
190 #define FMC_DISABLE_CFG_UPDATE() (FMC->ISPCTL &= ~FMC_ISPCTL_CFGUEN_Msk) /*!< Disable CONFIG Update Function */
\r
194 * @brief Enable APROM Update Function
\r
200 * @details This function will set APUEN bit of ISPCTL control register to enable APROM update function.
\r
201 * User needs to set APUEN bit before they can update APROM in APROM boot mode.
\r
204 #define FMC_ENABLE_AP_UPDATE() (FMC->ISPCTL |= FMC_ISPCTL_APUEN_Msk) /*!< Enable APROM Update Function */
\r
207 * @brief Disable APROM Update Function
\r
213 * @details This function will clear APUEN bit of ISPCTL control register to disable APROM update function.
\r
216 #define FMC_DISABLE_AP_UPDATE() (FMC->ISPCTL &= ~FMC_ISPCTL_APUEN_Msk) /*!< Disable APROM Update Function */
\r
219 * @brief Set Boot from APROM
\r
225 * @details This function is select booting from APROM.
\r
228 #define FMC_SET_APROM_BOOT() (FMC->ISPCTL &= ~FMC_ISPCTL_BS_Msk) /*!< Select booting from APROM */
\r
231 * @brief Set Boot from LDROM
\r
237 * @details This function is select booting from LDROM.
\r
240 #define FMC_SET_LDROM_BOOT() (FMC->ISPCTL |= FMC_ISPCTL_BS_Msk) /*!< Select booting from LDROM */
\r
243 * @brief Get ISP Fail Flag
\r
249 * @details This function is used to get ISP fail flag when do ISP actoin.
\r
252 #define FMC_GET_FAIL_FLAG() ((FMC->ISPCTL & FMC_ISPCTL_ISPFF_Msk) ? 1UL : 0UL) /*!< Get ISP fail flag */
\r
255 * @brief Clear ISP Fail Flag
\r
261 * @details This function is used to clear ISP fail flag when ISP fail flag set.
\r
264 #define FMC_CLR_FAIL_FLAG() (FMC->ISPCTL |= FMC_ISPCTL_ISPFF_Msk) /*!< Clear ISP fail flag */
\r
267 * @brief Enable ISP Interrupt
\r
273 * @details This function will enable ISP action interrupt.
\r
276 #define FMC_ENABLE_ISP_INT() (FMC->ISPCTL |= FMC_ISPCTL_INTEN_Msk) /*!< Enable ISP interrupt */
\r
279 * @brief Disable ISP Interrupt
\r
285 * @details This function will disable ISP action interrupt.
\r
288 #define FMC_DISABLE_ISP_INT() (FMC->ISPCTL &= ~FMC_ISPCTL_INTEN_Msk) /*!< Disable ISP interrupt */
\r
292 * @brief Get ISP Interrupt Flag
\r
298 * @details This function will get ISP action interrupt status
\r
301 #define FMC_GET_ISP_INT_FLAG() ((FMC->ISPSTS & FMC_ISPSTS_INTFLAG_Msk) ? 1UL : 0UL) /*!< Get ISP interrupt flag Status */
\r
305 * @brief Clear ISP Interrupt Flag
\r
311 * @details This function will clear ISP interrupt flag
\r
314 #define FMC_CLEAR_ISP_INT_FLAG() (FMC->ISPSTS = FMC_ISPSTS_INTFLAG_Msk) /*!< Clear ISP interrupt flag*/
\r
316 /*---------------------------------------------------------------------------------------------------------*/
\r
317 /* inline functions */
\r
318 /*---------------------------------------------------------------------------------------------------------*/
\r
319 __STATIC_INLINE uint32_t FMC_ReadCID(void);
\r
320 __STATIC_INLINE uint32_t FMC_ReadPID(void);
\r
321 __STATIC_INLINE uint32_t FMC_ReadUID(uint8_t u8Index);
\r
322 __STATIC_INLINE uint32_t FMC_ReadUCID(uint32_t u32Index);
\r
323 __STATIC_INLINE void FMC_SetVectorPageAddr(uint32_t u32PageAddr);
\r
324 __STATIC_INLINE uint32_t FMC_GetVECMAP(void);
\r
328 * @brief Get current vector mapping address.
\r
332 * @return The current vector mapping address.
\r
334 * @details To get VECMAP value which is the page address for remapping to vector page (0x0).
\r
337 __STATIC_INLINE uint32_t FMC_GetVECMAP(void)
\r
339 return (FMC->ISPSTS & FMC_ISPSTS_VECMAP_Msk);
\r
343 * @brief Read company ID
\r
347 * @return The company ID (32-bit)
\r
349 * @details The company ID of Nuvoton is fixed to be 0xDA
\r
351 __STATIC_INLINE uint32_t FMC_ReadCID(void)
\r
353 FMC->ISPCMD = FMC_ISPCMD_READ_CID; /* Set ISP Command Code */
\r
354 FMC->ISPADDR = 0x0u; /* Must keep 0x0 when read CID */
\r
355 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */
\r
358 #endif /* To make sure ISP/CPU be Synchronized */
\r
359 while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) {} /* Waiting for ISP Done */
\r
361 return FMC->ISPDAT;
\r
365 * @brief Read product ID
\r
369 * @return The product ID (32-bit)
\r
371 * @details This function is used to read product ID.
\r
373 __STATIC_INLINE uint32_t FMC_ReadPID(void)
\r
375 FMC->ISPCMD = FMC_ISPCMD_READ_DID; /* Set ISP Command Code */
\r
376 FMC->ISPADDR = 0x04u; /* Must keep 0x4 when read PID */
\r
377 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */
\r
380 #endif /* To make sure ISP/CPU be Synchronized */
\r
381 while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) {} /* Waiting for ISP Done */
\r
383 return FMC->ISPDAT;
\r
387 * @brief Read Unique ID
\r
389 * @param[in] u8Index UID index. 0 = UID[31:0], 1 = UID[63:32], 2 = UID[95:64]
\r
391 * @return The 32-bit unique ID data of specified UID index.
\r
393 * @details To read out 96-bit Unique ID.
\r
395 __STATIC_INLINE uint32_t FMC_ReadUID(uint8_t u8Index)
\r
397 FMC->ISPCMD = FMC_ISPCMD_READ_UID;
\r
398 FMC->ISPADDR = ((uint32_t)u8Index << 2u);
\r
400 FMC->ISPTRG = 0x1u;
\r
404 while(FMC->ISPTRG) {}
\r
406 return FMC->ISPDAT;
\r
410 * @brief To read UCID
\r
412 * @param[in] u32Index Index of the UCID to read. u32Index must be 0, 1, 2, or 3.
\r
414 * @return The UCID of specified index
\r
416 * @details This function is used to read unique chip ID (UCID).
\r
418 __STATIC_INLINE uint32_t FMC_ReadUCID(uint32_t u32Index)
\r
420 FMC->ISPCMD = FMC_ISPCMD_READ_UID; /* Set ISP Command Code */
\r
421 FMC->ISPADDR = (0x04u * u32Index) + 0x10u; /* The UCID is at offset 0x10 with word alignment. */
\r
422 FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */
\r
425 #endif /* To make sure ISP/CPU be Synchronized */
\r
426 while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) {} /* Waiting for ISP Done */
\r
428 return FMC->ISPDAT;
\r
432 * @brief Set vector mapping address
\r
434 * @param[in] u32PageAddr The page address to remap to address 0x0. The address must be page alignment.
\r
436 * @return To set VECMAP to remap specified page address to 0x0.
\r
438 * @details This function is used to set VECMAP to map specified page to vector page (0x0).
\r
440 __STATIC_INLINE void FMC_SetVectorPageAddr(uint32_t u32PageAddr)
\r
442 FMC->ISPCMD = FMC_ISPCMD_VECMAP; /* Set ISP Command Code */
\r
443 FMC->ISPADDR = u32PageAddr; /* The address of specified page which will be map to address 0x0. It must be page alignment. */
\r
444 FMC->ISPTRG = 0x1u; /* Trigger to start ISP procedure */
\r
447 #endif /* To make sure ISP/CPU be Synchronized */
\r
448 while(FMC->ISPTRG) {} /* Waiting for ISP Done */
\r
451 /*---------------------------------------------------------------------------------------------------------*/
\r
453 /*---------------------------------------------------------------------------------------------------------*/
\r
455 extern uint32_t FMC_CheckAllOne(uint32_t u32addr, uint32_t u32count);
\r
456 extern void FMC_Close(void);
\r
457 extern int32_t FMC_ConfigXOM(uint32_t xom_num, uint32_t xom_base, uint8_t xom_page);
\r
458 extern int32_t FMC_Erase(uint32_t u32PageAddr);
\r
459 extern int32_t FMC_Erase_Bank(uint32_t u32BankAddr);
\r
460 extern int32_t FMC_Erase_Block(uint32_t u32BlockAddr);
\r
461 extern int32_t FMC_EraseXOM(uint32_t xom_num);
\r
462 extern int32_t FMC_GetBootSource(void);
\r
463 extern uint32_t FMC_GetChkSum(uint32_t u32addr, uint32_t u32count);
\r
464 extern int32_t FMC_Is_OTP_Locked(uint32_t otp_num);
\r
465 extern int32_t FMC_GetXOMState(uint32_t xom_num);
\r
466 extern int32_t FMC_Lock_OTP(uint32_t otp_num);
\r
467 extern void FMC_Open(void);
\r
468 extern uint32_t FMC_Read(uint32_t u32Addr);
\r
469 extern int32_t FMC_Read_64(uint32_t u32addr, uint32_t * u32data0, uint32_t * u32data1);
\r
470 extern int32_t FMC_Read_OTP(uint32_t otp_num, uint32_t *low_word, uint32_t *high_word);
\r
471 extern int32_t FMC_ReadConfig(uint32_t u32Config[], uint32_t u32Count);
\r
472 extern void FMC_SetBootSource(int32_t i32BootSrc);
\r
473 extern int32_t FMC_CompareSPKey(uint32_t key[3]);
\r
474 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);
\r
475 extern void FMC_Write(uint32_t u32Addr, uint32_t u32Data);
\r
476 extern int32_t FMC_Write8Bytes(uint32_t u32addr, uint32_t u32data0, uint32_t u32data1);
\r
477 extern int32_t FMC_WriteConfig(uint32_t au32Config[], uint32_t u32Count);
\r
478 extern int32_t FMC_WriteMultiple(uint32_t u32Addr, uint32_t pu32Buf[], uint32_t u32Len);
\r
479 extern int32_t FMC_Write_OTP(uint32_t otp_num, uint32_t low_word, uint32_t high_word);
\r
480 extern int32_t FMC_WriteMultipleA(uint32_t u32Addr, uint32_t pu32Buf[], uint32_t u32Len);
\r
481 /*@}*/ /* end of group FMC_EXPORTED_FUNCTIONS */
\r
483 /*@}*/ /* end of group FMC_Driver */
\r
485 /*@}*/ /* end of group Standard_Driver */
\r
491 #endif /* __FMC_H__ */
\r
493 /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
\r