fmc.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /**************************************************************************//**
  2. * @file fmc.h
  3. * @version V1.00
  4. * @brief M480 Series Flash Memory Controller Driver Header File
  5. *
  6. * @copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  7. ******************************************************************************/
  8. #ifndef __FMC_H__
  9. #define __FMC_H__
  10. #ifdef __cplusplus
  11. extern "C"
  12. {
  13. #endif
  14. /** @addtogroup Standard_Driver Standard Driver
  15. @{
  16. */
  17. /** @addtogroup FMC_Driver FMC Driver
  18. @{
  19. */
  20. /** @addtogroup FMC_EXPORTED_CONSTANTS FMC Exported Constants
  21. @{
  22. */
  23. /*---------------------------------------------------------------------------------------------------------*/
  24. /* Define Base Address */
  25. /*---------------------------------------------------------------------------------------------------------*/
  26. #define FMC_APROM_BASE 0x00000000UL /*!< APROM base address \hideinitializer */
  27. #define FMC_APROM_END 0x00080000UL /*!< APROM end address \hideinitializer */
  28. #define FMC_APROM_BANK0_END (FMC_APROM_END/2UL) /*!< APROM bank0 end address \hideinitializer */
  29. #define FMC_LDROM_BASE 0x00100000UL /*!< LDROM base address \hideinitializer */
  30. #define FMC_LDROM_END 0x00101000UL /*!< LDROM end address \hideinitializer */
  31. #define FMC_SPROM_BASE 0x00200000UL /*!< SPROM base address \hideinitializer */
  32. #define FMC_SPROM_END 0x00201000UL /*!< SPROM end address \hideinitializer */
  33. #define FMC_CONFIG_BASE 0x00300000UL /*!< User Configuration address \hideinitializer */
  34. #define FMC_USER_CONFIG_0 0x00300000UL /*!< User Config 0 address \hideinitializer */
  35. #define FMC_USER_CONFIG_1 0x00300004UL /*!< User Config 1 address \hideinitializer */
  36. #define FMC_USER_CONFIG_2 0x00300008UL /*!< User Config 2 address \hideinitializer */
  37. #define FMC_KPROM_BASE 0x00301000UL /*!< Security ROM base address \hideinitializer */
  38. #define FMC_OTP_BASE 0x00310000UL /*!< OTP flash base address \hideinitializer */
  39. #define FMC_FLASH_PAGE_SIZE 0x1000UL /*!< Flash Page Size (4K bytes) \hideinitializer */
  40. #define FMC_PAGE_ADDR_MASK 0xFFFFF000UL /*!< Flash page address mask \hideinitializer */
  41. #define FMC_MULTI_WORD_PROG_LEN 512 /*!< The maximum length of a multi-word program. */
  42. #define FMC_APROM_SIZE FMC_APROM_END /*!< APROM Size \hideinitializer */
  43. #define FMC_BANK_SIZE (FMC_APROM_SIZE/2UL) /*!< APROM Bank Size \hideinitializer */
  44. #define FMC_LDROM_SIZE 0x1000UL /*!< LDROM Size (4 Kbytes) \hideinitializer */
  45. #define FMC_SPROM_SIZE 0x1000UL /*!< SPROM Size (4 Kbytes) \hideinitializer */
  46. #define FMC_OTP_ENTRY_CNT 256UL /*!< OTP entry number \hideinitializer */
  47. /*---------------------------------------------------------------------------------------------------------*/
  48. /* ISPCMD constant definitions */
  49. /*---------------------------------------------------------------------------------------------------------*/
  50. #define FMC_ISPCMD_READ 0x00UL /*!< ISP Command: Read flash word \hideinitializer */
  51. #define FMC_ISPCMD_READ_UID 0x04UL /*!< ISP Command: Read Unique ID \hideinitializer */
  52. #define FMC_ISPCMD_READ_ALL1 0x08UL /*!< ISP Command: Read all-one result \hideinitializer */
  53. #define FMC_ISPCMD_READ_CID 0x0BUL /*!< ISP Command: Read Company ID \hideinitializer */
  54. #define FMC_ISPCMD_READ_DID 0x0CUL /*!< ISP Command: Read Device ID \hideinitializer */
  55. #define FMC_ISPCMD_READ_CKS 0x0DUL /*!< ISP Command: Read checksum \hideinitializer */
  56. #define FMC_ISPCMD_PROGRAM 0x21UL /*!< ISP Command: Write flash word \hideinitializer */
  57. #define FMC_ISPCMD_PAGE_ERASE 0x22UL /*!< ISP Command: Page Erase Flash \hideinitializer */
  58. #define FMC_ISPCMD_BANK_ERASE 0x23UL /*!< ISP Command: Erase Flash bank 0 or 1 \hideinitializer */
  59. #define FMC_ISPCMD_BLOCK_ERASE 0x25UL /*!< ISP Command: Erase 4 pages alignment of APROM in bank 0 or 1 \hideinitializer */
  60. #define FMC_ISPCMD_PROGRAM_MUL 0x27UL /*!< ISP Command: Multuple word program \hideinitializer */
  61. #define FMC_ISPCMD_RUN_ALL1 0x28UL /*!< ISP Command: Run all-one verification \hideinitializer */
  62. #define FMC_ISPCMD_RUN_CKS 0x2DUL /*!< ISP Command: Run checksum calculation \hideinitializer */
  63. #define FMC_ISPCMD_VECMAP 0x2EUL /*!< ISP Command: Vector Page Remap \hideinitializer */
  64. #define FMC_ISPCMD_READ_64 0x40UL /*!< ISP Command: Read double flash word \hideinitializer */
  65. #define FMC_ISPCMD_PROGRAM_64 0x61UL /*!< ISP Command: Write double flash word \hideinitializer */
  66. #define IS_BOOT_FROM_APROM 0UL /*!< Is booting from APROM \hideinitializer */
  67. #define IS_BOOT_FROM_LDROM 1UL /*!< Is booting from LDROM \hideinitializer */
  68. #define READ_ALLONE_YES 0xA11FFFFFUL /*!< Check-all-one result is all one. \hideinitializer */
  69. #define READ_ALLONE_NOT 0xA1100000UL /*!< Check-all-one result is not all one. \hideinitializer */
  70. #define READ_ALLONE_CMD_FAIL 0xFFFFFFFFUL /*!< Check-all-one command failed. \hideinitializer */
  71. /*@}*/ /* end of group FMC_EXPORTED_CONSTANTS */
  72. /** @addtogroup FMC_EXPORTED_MACROS FMC Exported Macros
  73. @{
  74. */
  75. /*---------------------------------------------------------------------------------------------------------*/
  76. /* Macros */
  77. /*---------------------------------------------------------------------------------------------------------*/
  78. #define FMC_SET_APROM_BOOT() (FMC->ISPCTL &= ~FMC_ISPCTL_BS_Msk) /*!< Select booting from APROM \hideinitializer */
  79. #define FMC_SET_LDROM_BOOT() (FMC->ISPCTL |= FMC_ISPCTL_BS_Msk) /*!< Select booting from LDROM \hideinitializer */
  80. #define FMC_ENABLE_AP_UPDATE() (FMC->ISPCTL |= FMC_ISPCTL_APUEN_Msk) /*!< Enable APROM update \hideinitializer */
  81. #define FMC_DISABLE_AP_UPDATE() (FMC->ISPCTL &= ~FMC_ISPCTL_APUEN_Msk) /*!< Disable APROM update \hideinitializer */
  82. #define FMC_ENABLE_CFG_UPDATE() (FMC->ISPCTL |= FMC_ISPCTL_CFGUEN_Msk) /*!< Enable User Config update \hideinitializer */
  83. #define FMC_DISABLE_CFG_UPDATE() (FMC->ISPCTL &= ~FMC_ISPCTL_CFGUEN_Msk) /*!< Disable User Config update \hideinitializer */
  84. #define FMC_ENABLE_LD_UPDATE() (FMC->ISPCTL |= FMC_ISPCTL_LDUEN_Msk) /*!< Enable LDROM update \hideinitializer */
  85. #define FMC_DISABLE_LD_UPDATE() (FMC->ISPCTL &= ~FMC_ISPCTL_LDUEN_Msk) /*!< Disable LDROM update \hideinitializer */
  86. #define FMC_ENABLE_SP_UPDATE() (FMC->ISPCTL |= FMC_ISPCTL_SPUEN_Msk) /*!< Enable SPROM update \hideinitializer */
  87. #define FMC_DISABLE_SP_UPDATE() (FMC->ISPCTL &= ~FMC_ISPCTL_SPUEN_Msk) /*!< Disable SPROM update \hideinitializer */
  88. #define FMC_DISABLE_ISP() (FMC->ISPCTL &= ~FMC_ISPCTL_ISPEN_Msk) /*!< Disable ISP function \hideinitializer */
  89. #define FMC_ENABLE_ISP() (FMC->ISPCTL |= FMC_ISPCTL_ISPEN_Msk) /*!< Enable ISP function \hideinitializer */
  90. #define FMC_GET_FAIL_FLAG() ((FMC->ISPCTL & FMC_ISPCTL_ISPFF_Msk) ? 1UL : 0UL) /*!< Get ISP fail flag \hideinitializer */
  91. #define FMC_CLR_FAIL_FLAG() (FMC->ISPCTL |= FMC_ISPCTL_ISPFF_Msk) /*!< Clear ISP fail flag \hideinitializer */
  92. /*@}*/ /* end of group FMC_EXPORTED_MACROS */
  93. /** @addtogroup FMC_EXPORTED_FUNCTIONS FMC Exported Functions
  94. @{
  95. */
  96. /*---------------------------------------------------------------------------------------------------------*/
  97. /* inline functions */
  98. /*---------------------------------------------------------------------------------------------------------*/
  99. __STATIC_INLINE uint32_t FMC_ReadCID(void);
  100. __STATIC_INLINE uint32_t FMC_ReadPID(void);
  101. __STATIC_INLINE uint32_t FMC_ReadUID(uint8_t u8Index);
  102. __STATIC_INLINE uint32_t FMC_ReadUCID(uint32_t u32Index);
  103. __STATIC_INLINE void FMC_SetVectorPageAddr(uint32_t u32PageAddr);
  104. __STATIC_INLINE uint32_t FMC_GetVECMAP(void);
  105. /**
  106. * @brief Get current vector mapping address.
  107. * @param None
  108. * @return The current vector mapping address.
  109. * @details To get VECMAP value which is the page address for remapping to vector page (0x0).
  110. * @note
  111. * VECMAP only valid when new IAP function is enabled. (CBS = 10'b or 00'b)
  112. */
  113. __STATIC_INLINE uint32_t FMC_GetVECMAP(void)
  114. {
  115. return (FMC->ISPSTS & FMC_ISPSTS_VECMAP_Msk);
  116. }
  117. /**
  118. * @brief Read company ID
  119. * @param None
  120. * @return The company ID (32-bit)
  121. * @details The company ID of Nuvoton is fixed to be 0xDA
  122. */
  123. __STATIC_INLINE uint32_t FMC_ReadCID(void)
  124. {
  125. FMC->ISPCMD = FMC_ISPCMD_READ_CID; /* Set ISP Command Code */
  126. FMC->ISPADDR = 0x0u; /* Must keep 0x0 when read CID */
  127. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */
  128. #if ISBEN
  129. __ISB();
  130. #endif /* To make sure ISP/CPU be Synchronized */
  131. while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) {} /* Waiting for ISP Done */
  132. return FMC->ISPDAT;
  133. }
  134. /**
  135. * @brief Read product ID
  136. * @param None
  137. * @return The product ID (32-bit)
  138. * @details This function is used to read product ID.
  139. */
  140. __STATIC_INLINE uint32_t FMC_ReadPID(void)
  141. {
  142. FMC->ISPCMD = FMC_ISPCMD_READ_DID; /* Set ISP Command Code */
  143. FMC->ISPADDR = 0x04u; /* Must keep 0x4 when read PID */
  144. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */
  145. #if ISBEN
  146. __ISB();
  147. #endif /* To make sure ISP/CPU be Synchronized */
  148. while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) {} /* Waiting for ISP Done */
  149. return FMC->ISPDAT;
  150. }
  151. /**
  152. * @brief Read Unique ID
  153. * @param[in] u8Index UID index. 0 = UID[31:0], 1 = UID[63:32], 2 = UID[95:64]
  154. * @return The 32-bit unique ID data of specified UID index.
  155. * @details To read out 96-bit Unique ID.
  156. */
  157. __STATIC_INLINE uint32_t FMC_ReadUID(uint8_t u8Index)
  158. {
  159. FMC->ISPCMD = FMC_ISPCMD_READ_UID;
  160. FMC->ISPADDR = ((uint32_t)u8Index << 2u);
  161. FMC->ISPDAT = 0u;
  162. FMC->ISPTRG = 0x1u;
  163. #if ISBEN
  164. __ISB();
  165. #endif
  166. while(FMC->ISPTRG) {}
  167. return FMC->ISPDAT;
  168. }
  169. /**
  170. * @brief To read UCID
  171. * @param[in] u32Index Index of the UCID to read. u32Index must be 0, 1, 2, or 3.
  172. * @return The UCID of specified index
  173. * @details This function is used to read unique chip ID (UCID).
  174. */
  175. __STATIC_INLINE uint32_t FMC_ReadUCID(uint32_t u32Index)
  176. {
  177. FMC->ISPCMD = FMC_ISPCMD_READ_UID; /* Set ISP Command Code */
  178. FMC->ISPADDR = (0x04u * u32Index) + 0x10u; /* The UCID is at offset 0x10 with word alignment. */
  179. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */
  180. #if ISBEN
  181. __ISB();
  182. #endif /* To make sure ISP/CPU be Synchronized */
  183. while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) {} /* Waiting for ISP Done */
  184. return FMC->ISPDAT;
  185. }
  186. /**
  187. * @brief Set vector mapping address
  188. * @param[in] u32PageAddr The page address to remap to address 0x0. The address must be page alignment.
  189. * @return To set VECMAP to remap specified page address to 0x0.
  190. * @details This function is used to set VECMAP to map specified page to vector page (0x0).
  191. * @note
  192. * VECMAP only valid when new IAP function is enabled. (CBS = 10'b or 00'b)
  193. */
  194. __STATIC_INLINE void FMC_SetVectorPageAddr(uint32_t u32PageAddr)
  195. {
  196. FMC->ISPCMD = FMC_ISPCMD_VECMAP; /* Set ISP Command Code */
  197. FMC->ISPADDR = u32PageAddr; /* The address of specified page which will be map to address 0x0. It must be page alignment. */
  198. FMC->ISPTRG = 0x1u; /* Trigger to start ISP procedure */
  199. #if ISBEN
  200. __ISB();
  201. #endif /* To make sure ISP/CPU be Synchronized */
  202. while(FMC->ISPTRG) {} /* Waiting for ISP Done */
  203. }
  204. /*---------------------------------------------------------------------------------------------------------*/
  205. /* Functions */
  206. /*---------------------------------------------------------------------------------------------------------*/
  207. extern void FMC_Close(void);
  208. extern int32_t FMC_Erase(uint32_t u32PageAddr);
  209. extern int32_t FMC_Erase_SPROM(void);
  210. extern int32_t FMC_Erase_Block(uint32_t u32BlockAddr);
  211. extern int32_t FMC_Erase_Bank(uint32_t u32BankAddr);
  212. extern int32_t FMC_GetBootSource(void);
  213. extern void FMC_Open(void);
  214. extern uint32_t FMC_Read(uint32_t u32Addr);
  215. extern int32_t FMC_Read_64(uint32_t u32addr, uint32_t * u32data0, uint32_t * u32data1);
  216. extern uint32_t FMC_ReadDataFlashBaseAddr(void);
  217. extern void FMC_SetBootSource(int32_t i32BootSrc);
  218. extern void FMC_Write(uint32_t u32Addr, uint32_t u32Data);
  219. extern int32_t FMC_Write8Bytes(uint32_t u32addr, uint32_t u32data0, uint32_t u32data1);
  220. extern int32_t FMC_WriteMultiple(uint32_t u32Addr, uint32_t pu32Buf[], uint32_t u32Len);
  221. extern int32_t FMC_Write_OTP(uint32_t otp_num, uint32_t low_word, uint32_t high_word);
  222. extern int32_t FMC_Read_OTP(uint32_t otp_num, uint32_t *low_word, uint32_t *high_word);
  223. extern int32_t FMC_Lock_OTP(uint32_t otp_num);
  224. extern int32_t FMC_Is_OTP_Locked(uint32_t otp_num);
  225. extern int32_t FMC_ReadConfig(uint32_t u32Config[], uint32_t u32Count);
  226. extern int32_t FMC_WriteConfig(uint32_t u32Config[], uint32_t u32Count);
  227. extern uint32_t FMC_GetChkSum(uint32_t u32addr, uint32_t u32count);
  228. extern uint32_t FMC_CheckAllOne(uint32_t u32addr, uint32_t u32count);
  229. 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);
  230. extern int32_t FMC_CompareSPKey(uint32_t key[3]);
  231. /*@}*/ /* end of group FMC_EXPORTED_FUNCTIONS */
  232. /*@}*/ /* end of group FMC_Driver */
  233. /*@}*/ /* end of group Standard_Driver */
  234. #ifdef __cplusplus
  235. }
  236. #endif
  237. #endif /* __FMC_H__ */
  238. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/