nu_fmc.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /**************************************************************************//**
  2. * @file nu_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 __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 0x00100000UL /*!< APROM end address \hideinitializer */
  29. #define FMC_APROM_BANK0_END (FMC_APROM_END/2UL) /*!< APROM bank0 end address \hideinitializer */
  30. #define FMC_LDROM_BASE 0x0F100000UL /*!< LDROM base address \hideinitializer */
  31. #define FMC_LDROM_END 0x0F102000UL /*!< LDROM end address \hideinitializer */
  32. #define FMC_XOM_BASE 0x0F200000UL /*!< XOM Base Address \hideinitializer */
  33. #define FMC_XOMR0_BASE 0x0F200000UL /*!< XOMR 0 Base Address \hideinitializer */
  34. #define FMC_XOMR1_BASE 0x0F200010UL /*!< XOMR 1 Base Address \hideinitializer */
  35. #define FMC_XOMR2_BASE 0x0F200020UL /*!< XOMR 2 Base Address \hideinitializer */
  36. #define FMC_XOMR3_BASE 0x0F200030UL /*!< XOMR 3 Base Address \hideinitializer */
  37. #define FMC_CONFIG_BASE 0x0F300000UL /*!< User Configuration address \hideinitializer */
  38. #define FMC_USER_CONFIG_0 0x0F300000UL /*!< User Config 0 address \hideinitializer */
  39. #define FMC_USER_CONFIG_1 0x0F300004UL /*!< User Config 1 address \hideinitializer */
  40. #define FMC_USER_CONFIG_2 0x0F300008UL /*!< User Config 2 address \hideinitializer */
  41. #define FMC_USER_CONFIG_3 0x0F30000CUL /*!< User Config 2 address \hideinitializer */
  42. #define FMC_OTP_BASE 0x0F310000UL /*!< OTP flash base address \hideinitializer */
  43. #define FMC_REMAPCFG_BASE 0x0F320000UL /*!< User Configuration address \hideinitializer */
  44. #define FMC_FLASH_PAGE_SIZE 0x1000UL /*!< Flash Page Size (4K bytes) \hideinitializer */
  45. #define FMC_PAGE_ADDR_MASK 0xFFFFF000UL /*!< Flash page address mask \hideinitializer */
  46. #define FMC_MULTI_WORD_PROG_LEN 512 /*!< The maximum length of a multi-word program. */
  47. #define FMC_APPROT_BLOCK_SIZE 0x8000UL /*!< APROM APPROT size (32K bytes) \hideinitializer */
  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 0x2000UL /*!< LDROM Size (8 Kbytes) \hideinitializer */
  51. #define FMC_OTP_ENTRY_CNT 256UL /*!< OTP entry number \hideinitializer */
  52. /*---------------------------------------------------------------------------------------------------------*/
  53. /* XOM region number constant definitions */
  54. /*---------------------------------------------------------------------------------------------------------*/
  55. #define XOMR0 0UL /*!< XOM region 0 */
  56. #define XOMR1 1UL /*!< XOM region 1 */
  57. #define XOMR2 2UL /*!< XOM region 2 */
  58. #define XOMR3 3UL /*!< XOM region 3 */
  59. /*---------------------------------------------------------------------------------------------------------*/
  60. /* ISPCTL constant definitions */
  61. /*---------------------------------------------------------------------------------------------------------*/
  62. #define IS_BOOT_FROM_LDROM 0x1UL /*!< ISPCTL setting to select to boot from LDROM */
  63. #define IS_BOOT_FROM_APROM 0x0UL /*!< ISPCTL setting to select to boot from APROM */
  64. /*---------------------------------------------------------------------------------------------------------*/
  65. /* ISPCMD constant definitions */
  66. /*---------------------------------------------------------------------------------------------------------*/
  67. #define FMC_ISPCMD_READ 0x00UL /*!< ISP Command: Read flash word \hideinitializer */
  68. #define FMC_ISPCMD_READ_UID 0x04UL /*!< ISP Command: Read Unique ID \hideinitializer */
  69. #define FMC_ISPCMD_READ_ALL1 0x08UL /*!< ISP Command: Read all-one result \hideinitializer */
  70. #define FMC_ISPCMD_READ_CID 0x0BUL /*!< ISP Command: Read Company ID \hideinitializer */
  71. #define FMC_ISPCMD_READ_DID 0x0CUL /*!< ISP Command: Read Device ID \hideinitializer */
  72. #define FMC_ISPCMD_READ_CKS 0x0DUL /*!< ISP Command: Read checksum \hideinitializer */
  73. #define FMC_ISPCMD_PROGRAM 0x21UL /*!< ISP Command: Write flash word \hideinitializer */
  74. #define FMC_ISPCMD_PAGE_ERASE 0x22UL /*!< ISP Command: Page Erase Flash \hideinitializer */
  75. #define FMC_ISPCMD_BANK_ERASE 0x23UL /*!< ISP Command: Erase Flash bank 0 or 1 \hideinitializer */
  76. #define FMC_ISPCMD_BLOCK_ERASE 0x25UL /*!< ISP Command: Erase 4 pages alignment of APROM in bank 0 or 1 \hideinitializer */
  77. #define FMC_ISPCMD_PROGRAM_MUL 0x27UL /*!< ISP Command: Multuple word program \hideinitializer */
  78. #define FMC_ISPCMD_RUN_ALL1 0x28UL /*!< ISP Command: Run all-one verification \hideinitializer */
  79. #define FMC_ISPCMD_BANK_REMAP 0x2CUL /*!< ISP Command: Bank Remap \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. /*---------------------------------------------------------------------------------------------------------*/
  88. /* FMC Time-out Handler Constant Definitions */
  89. /*---------------------------------------------------------------------------------------------------------*/
  90. #define FMC_TIMEOUT_READ (SystemCoreClock>>3) /*!< Read command time-out 125 ms \hideinitializer */
  91. #define FMC_TIMEOUT_WRITE (SystemCoreClock>>3) /*!< Write command time-out 125 ms \hideinitializer */
  92. #define FMC_TIMEOUT_ERASE (SystemCoreClock>>2) /*!< Erase command time-out 250 ms \hideinitializer */
  93. #define FMC_TIMEOUT_CHKSUM (SystemCoreClock<<1) /*!< Get checksum command time-out 2 s \hideinitializer */
  94. #define FMC_TIMEOUT_CHKALLONE (SystemCoreClock<<1) /*!< Check-all-one command time-out 2 s \hideinitializer */
  95. /*@}*/ /* end of group FMC_EXPORTED_CONSTANTS */
  96. /** @addtogroup FMC_EXPORTED_MACROS FMC Exported Macros
  97. @{
  98. */
  99. /*---------------------------------------------------------------------------------------------------------*/
  100. /* Macros */
  101. /*---------------------------------------------------------------------------------------------------------*/
  102. #define FMC_SET_APROM_BOOT() (FMC->ISPCTL &= ~FMC_ISPCTL_BS_Msk) /*!< Select booting from APROM \hideinitializer */
  103. #define FMC_SET_LDROM_BOOT() (FMC->ISPCTL |= FMC_ISPCTL_BS_Msk) /*!< Select booting from LDROM \hideinitializer */
  104. #define FMC_ENABLE_AP_UPDATE() (FMC->ISPCTL |= FMC_ISPCTL_APUEN_Msk) /*!< Enable APROM update \hideinitializer */
  105. #define FMC_DISABLE_AP_UPDATE() (FMC->ISPCTL &= ~FMC_ISPCTL_APUEN_Msk) /*!< Disable APROM update \hideinitializer */
  106. #define FMC_ENABLE_CFG_UPDATE() (FMC->ISPCTL |= FMC_ISPCTL_CFGUEN_Msk) /*!< Enable User Config update \hideinitializer */
  107. #define FMC_DISABLE_CFG_UPDATE() (FMC->ISPCTL &= ~FMC_ISPCTL_CFGUEN_Msk) /*!< Disable User Config update \hideinitializer */
  108. #define FMC_ENABLE_LD_UPDATE() (FMC->ISPCTL |= FMC_ISPCTL_LDUEN_Msk) /*!< Enable LDROM update \hideinitializer */
  109. #define FMC_DISABLE_LD_UPDATE() (FMC->ISPCTL &= ~FMC_ISPCTL_LDUEN_Msk) /*!< Disable LDROM update \hideinitializer */
  110. #define FMC_DISABLE_ISP() (FMC->ISPCTL &= ~FMC_ISPCTL_ISPEN_Msk) /*!< Disable ISP function \hideinitializer */
  111. #define FMC_ENABLE_ISP() (FMC->ISPCTL |= FMC_ISPCTL_ISPEN_Msk) /*!< Enable ISP function \hideinitializer */
  112. #define FMC_GET_FAIL_FLAG() ((FMC->ISPCTL & FMC_ISPCTL_ISPFF_Msk) ? 1UL : 0UL) /*!< Get ISP fail flag \hideinitializer */
  113. #define FMC_CLR_FAIL_FLAG() (FMC->ISPCTL |= FMC_ISPCTL_ISPFF_Msk) /*!< Clear ISP fail flag \hideinitializer */
  114. #define FMC_ENABLE_APPROT(u8Block) (FMC->APPROT |= (1ul << u8Block)) /*!< Enable APPROT Block \hideinitializer */
  115. #define FMC_DISABLE_APPROT(u8Block) (FMC->APPROT &= ~(1ul << u8Block)) /*!< Disable APPROT Block \hideinitializer */
  116. /*@}*/ /* end of group FMC_EXPORTED_MACROS */
  117. /*---------------------------------------------------------------------------------------------------------*/
  118. /* Global variables */
  119. /*---------------------------------------------------------------------------------------------------------*/
  120. extern int32_t g_FMC_i32ErrCode;
  121. /** @addtogroup FMC_EXPORTED_FUNCTIONS FMC Exported Functions
  122. @{
  123. */
  124. /*---------------------------------------------------------------------------------------------------------*/
  125. /* inline functions */
  126. /*---------------------------------------------------------------------------------------------------------*/
  127. __STATIC_INLINE uint32_t FMC_ReadCID(void);
  128. __STATIC_INLINE uint32_t FMC_ReadPID(void);
  129. __STATIC_INLINE uint32_t FMC_ReadUID(uint8_t u8Index);
  130. __STATIC_INLINE uint32_t FMC_ReadUCID(uint32_t u32Index);
  131. __STATIC_INLINE int32_t FMC_SetVectorPageAddr(uint32_t u32PageAddr);
  132. __STATIC_INLINE uint32_t FMC_GetVECMAP(void);
  133. /**
  134. * @brief Get current vector mapping address.
  135. * @param None
  136. * @return The current vector mapping address.
  137. * @details To get VECMAP value which is the page address for remapping to vector page (0x0).
  138. * @note
  139. * VECMAP only valid when new IAP function is enabled. (CBS = 10'b or 00'b)
  140. */
  141. __STATIC_INLINE uint32_t FMC_GetVECMAP(void)
  142. {
  143. return (FMC->ISPSTS & FMC_ISPSTS_VECMAP_Msk);
  144. }
  145. /**
  146. * @brief Read company ID
  147. * @param None
  148. * @return The company ID (32-bit). 0xFFFFFFFF means read failed.
  149. * @details The company ID of Nuvoton is fixed to be 0xDA
  150. *
  151. * @note Global error code g_FMC_i32ErrCode
  152. * -1 Read time-out
  153. */
  154. __STATIC_INLINE uint32_t FMC_ReadCID(void)
  155. {
  156. int32_t i32TimeOutCnt = FMC_TIMEOUT_READ;
  157. g_FMC_i32ErrCode = 0;
  158. FMC->ISPCMD = FMC_ISPCMD_READ_CID; /* Set ISP Command Code */
  159. FMC->ISPADDR = 0x0u; /* Must keep 0x0 when read CID */
  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. {
  166. if (i32TimeOutCnt-- <= 0)
  167. {
  168. g_FMC_i32ErrCode = -1;
  169. return 0xFFFFFFFF;
  170. }
  171. }
  172. return FMC->ISPDAT;
  173. }
  174. /**
  175. * @brief Read product ID
  176. * @param None
  177. * @return The product ID (32-bit). 0xFFFFFFFF means read failed.
  178. * @details This function is used to read product ID.
  179. *
  180. * @note Global error code g_FMC_i32ErrCode
  181. * -1 Read time-out
  182. */
  183. __STATIC_INLINE uint32_t FMC_ReadPID(void)
  184. {
  185. int32_t i32TimeOutCnt = FMC_TIMEOUT_READ;
  186. g_FMC_i32ErrCode = 0;
  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. if (i32TimeOutCnt-- <= 0)
  196. {
  197. g_FMC_i32ErrCode = -1;
  198. return 0xFFFFFFFF;
  199. }
  200. }
  201. return FMC->ISPDAT;
  202. }
  203. /**
  204. * @brief Read Unique ID
  205. * @param[in] u8Index UID index. 0 = UID[31:0], 1 = UID[63:32], 2 = UID[95:64]
  206. * @return The 32-bit unique ID data of specified UID index. 0xFFFFFFFF means read failed.
  207. * @details To read out 96-bit Unique ID.
  208. *
  209. * @note Global error code g_FMC_i32ErrCode
  210. * -1 Read time-out
  211. */
  212. __STATIC_INLINE uint32_t FMC_ReadUID(uint8_t u8Index)
  213. {
  214. int32_t i32TimeOutCnt = FMC_TIMEOUT_READ;
  215. g_FMC_i32ErrCode = 0;
  216. FMC->ISPCMD = FMC_ISPCMD_READ_UID;
  217. FMC->ISPADDR = ((uint32_t)u8Index << 2u);
  218. FMC->ISPDAT = 0u;
  219. FMC->ISPTRG = 0x1u;
  220. #if ISBEN
  221. __ISB();
  222. #endif
  223. while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) /* Waiting for ISP Done */
  224. {
  225. if (i32TimeOutCnt-- <= 0)
  226. {
  227. g_FMC_i32ErrCode = -1;
  228. return 0xFFFFFFFF;
  229. }
  230. }
  231. return FMC->ISPDAT;
  232. }
  233. /**
  234. * @brief To read UCID
  235. * @param[in] u32Index Index of the UCID to read. u32Index must be 0, 1, 2, or 3.
  236. * @return The UCID of specified index
  237. * @details This function is used to read unique chip ID (UCID). 0xFFFFFFFF means read failed.
  238. *
  239. * @note Global error code g_FMC_i32ErrCode
  240. * -1 Read time-out
  241. */
  242. __STATIC_INLINE uint32_t FMC_ReadUCID(uint32_t u32Index)
  243. {
  244. int32_t i32TimeOutCnt = FMC_TIMEOUT_READ;
  245. g_FMC_i32ErrCode = 0;
  246. FMC->ISPCMD = FMC_ISPCMD_READ_UID; /* Set ISP Command Code */
  247. FMC->ISPADDR = (0x04u * u32Index) + 0x10u; /* The UCID is at offset 0x10 with word alignment. */
  248. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */
  249. #if ISBEN
  250. __ISB();
  251. #endif /* To make sure ISP/CPU be Synchronized */
  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. return FMC->ISPDAT;
  261. }
  262. /**
  263. * @brief Set vector mapping address
  264. * @param[in] u32PageAddr The page address to remap to address 0x0. The address must be page alignment.
  265. * @return To set VECMAP to remap specified page address to 0x0.
  266. * @details This function is used to set VECMAP to map specified page to vector page (0x0).
  267. * @retval 0 Success
  268. * @retval -1 Failed
  269. * @note
  270. * VECMAP only valid when new IAP function is enabled. (CBS = 10'b or 00'b)
  271. *
  272. * @note Global error code g_FMC_i32ErrCode
  273. * -1 Command time-out
  274. */
  275. __STATIC_INLINE int32_t FMC_SetVectorPageAddr(uint32_t u32PageAddr)
  276. {
  277. int32_t i32TimeOutCnt = FMC_TIMEOUT_WRITE;
  278. g_FMC_i32ErrCode = 0;
  279. FMC->ISPCMD = FMC_ISPCMD_VECMAP; /* Set ISP Command Code */
  280. FMC->ISPADDR = u32PageAddr; /* The address of specified page which will be map to address 0x0. It must be page alignment. */
  281. FMC->ISPTRG = 0x1u; /* Trigger to start ISP procedure */
  282. #if ISBEN
  283. __ISB();
  284. #endif /* To make sure ISP/CPU be Synchronized */
  285. while (FMC->ISPTRG) /* Waiting for ISP Done */
  286. {
  287. if (i32TimeOutCnt-- <= 0)
  288. {
  289. g_FMC_i32ErrCode = -1;
  290. return -1;
  291. }
  292. }
  293. return 0;
  294. }
  295. /*---------------------------------------------------------------------------------------------------------*/
  296. /* Functions */
  297. /*---------------------------------------------------------------------------------------------------------*/
  298. extern void FMC_Close(void);
  299. extern int32_t FMC_ConfigXOM(uint32_t xom_num, uint32_t xom_base, uint8_t xom_page);
  300. extern int32_t FMC_Erase(uint32_t u32PageAddr);
  301. extern int32_t FMC_Erase_Bank(uint32_t u32BankAddr);
  302. extern int32_t FMC_EraseXOM(uint32_t xom_num);
  303. extern int32_t FMC_GetXOMState(uint32_t xom_num);
  304. extern int32_t FMC_GetBootSource(void);
  305. extern void FMC_Open(void);
  306. extern uint32_t FMC_Read(uint32_t u32Addr);
  307. extern int32_t FMC_Read_64(uint32_t u32addr, uint32_t *u32data0, uint32_t *u32data1);
  308. extern uint32_t FMC_ReadDataFlashBaseAddr(void);
  309. extern void FMC_SetBootSource(int32_t i32BootSrc);
  310. extern int32_t FMC_Write(uint32_t u32Addr, uint32_t u32Data);
  311. extern int32_t FMC_Write8Bytes(uint32_t u32addr, uint32_t u32data0, uint32_t u32data1);
  312. extern int32_t FMC_WriteMultiple(uint32_t u32Addr, uint32_t pu32Buf[], uint32_t u32Len);
  313. extern int32_t FMC_WriteOTP(uint32_t otp_num, uint32_t low_word, uint32_t high_word);
  314. extern int32_t FMC_ReadOTP(uint32_t otp_num, uint32_t *low_word, uint32_t *high_word);
  315. extern int32_t FMC_LockOTP(uint32_t otp_num);
  316. extern int32_t FMC_IsOTPLocked(uint32_t otp_num);
  317. extern int32_t FMC_ReadConfig(uint32_t u32Config[], uint32_t u32Count);
  318. extern int32_t FMC_WriteConfig(uint32_t u32Config[], uint32_t u32Count);
  319. extern uint32_t FMC_GetChkSum(uint32_t u32addr, uint32_t u32count);
  320. extern uint32_t FMC_CheckAllOne(uint32_t u32addr, uint32_t u32count);
  321. extern int32_t FMC_RemapBank(uint32_t u32Bank);
  322. /*@}*/ /* end of group FMC_EXPORTED_FUNCTIONS */
  323. /*@}*/ /* end of group FMC_Driver */
  324. /*@}*/ /* end of group Standard_Driver */
  325. #ifdef __cplusplus
  326. }
  327. #endif
  328. #endif /* __NU_FMC_H__ */