nu_fmc.h 16 KB

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