nu_fmc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. /**************************************************************************//**
  2. * @file fmc.c
  3. * @version V1.00
  4. * @brief M480 series FMC driver source file
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. * @copyright (C) 2016-2020 Nuvoton Technology Corp. All rights reserved.
  8. *****************************************************************************/
  9. #include <stdio.h>
  10. #include "NuMicro.h"
  11. /** @addtogroup Standard_Driver Standard Driver
  12. @{
  13. */
  14. /** @addtogroup FMC_Driver FMC Driver
  15. @{
  16. */
  17. /** @addtogroup FMC_EXPORTED_FUNCTIONS FMC Exported Functions
  18. @{
  19. */
  20. /**
  21. * @brief Disable FMC ISP function.
  22. * @return None
  23. */
  24. void FMC_Close(void)
  25. {
  26. FMC->ISPCTL &= ~FMC_ISPCTL_ISPEN_Msk;
  27. }
  28. /**
  29. * @brief Config XOM Region
  30. * @param[in] u32XomNum The XOM number(0~3)
  31. * @param[in] u32XomBase The XOM region base address.
  32. * @param[in] u8XomPage The XOM page number of region size.
  33. *
  34. * @retval 0 Success
  35. * @retval 1 XOM is has already actived.
  36. * @retval -1 Program failed.
  37. * @retval -2 Invalid XOM number.
  38. *
  39. * @details Program XOM base address and XOM size(page)
  40. */
  41. int32_t FMC_ConfigXOM(uint32_t u32XomNum, uint32_t u32XomBase, uint8_t u8XomPage)
  42. {
  43. int32_t ret = 0;
  44. if(u32XomNum >= 4UL)
  45. {
  46. ret = -2;
  47. }
  48. if(ret == 0)
  49. {
  50. ret = FMC_GetXOMState(u32XomNum);
  51. }
  52. if(ret == 0)
  53. {
  54. FMC->ISPCMD = FMC_ISPCMD_PROGRAM;
  55. FMC->ISPADDR = FMC_XOM_BASE + (u32XomNum * 0x10u);
  56. FMC->ISPDAT = u32XomBase;
  57. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
  58. while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) {}
  59. if(FMC->ISPSTS & FMC_ISPSTS_ISPFF_Msk)
  60. {
  61. FMC->ISPSTS |= FMC_ISPSTS_ISPFF_Msk;
  62. ret = -1;
  63. }
  64. }
  65. if(ret == 0)
  66. {
  67. FMC->ISPCMD = FMC_ISPCMD_PROGRAM;
  68. FMC->ISPADDR = FMC_XOM_BASE + (u32XomNum * 0x10u + 0x04u);
  69. FMC->ISPDAT = u8XomPage;
  70. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
  71. while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) {}
  72. if(FMC->ISPSTS & FMC_ISPSTS_ISPFF_Msk)
  73. {
  74. FMC->ISPSTS |= FMC_ISPSTS_ISPFF_Msk;
  75. ret = -1;
  76. }
  77. }
  78. if(ret == 0)
  79. {
  80. FMC->ISPCMD = FMC_ISPCMD_PROGRAM;
  81. FMC->ISPADDR = FMC_XOM_BASE + (u32XomNum * 0x10u + 0x08u);
  82. FMC->ISPDAT = 0u;
  83. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
  84. while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) {}
  85. if(FMC->ISPSTS & FMC_ISPSTS_ISPFF_Msk)
  86. {
  87. FMC->ISPSTS |= FMC_ISPSTS_ISPFF_Msk;
  88. ret = -1;
  89. }
  90. }
  91. return ret;
  92. }
  93. /**
  94. * @brief Execute FMC_ISPCMD_PAGE_ERASE command to erase a flash page. The page size is 4096 bytes.
  95. * @param[in] u32PageAddr Address of the flash page to be erased.
  96. * It must be a 4096 bytes aligned address.
  97. * @return ISP page erase success or not.
  98. * @retval 0 Success
  99. * @retval -1 Erase failed
  100. */
  101. int32_t FMC_Erase(uint32_t u32PageAddr)
  102. {
  103. int32_t ret = 0;
  104. if (u32PageAddr == FMC_SPROM_BASE)
  105. {
  106. ret = FMC_Erase_SPROM();
  107. }
  108. if (ret == 0)
  109. {
  110. FMC->ISPCMD = FMC_ISPCMD_PAGE_ERASE;
  111. FMC->ISPADDR = u32PageAddr;
  112. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
  113. while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) { }
  114. if (FMC->ISPCTL & FMC_ISPCTL_ISPFF_Msk)
  115. {
  116. FMC->ISPCTL |= FMC_ISPCTL_ISPFF_Msk;
  117. ret = -1;
  118. }
  119. }
  120. return ret;
  121. }
  122. /**
  123. * @brief Execute FMC_ISPCMD_PAGE_ERASE command to erase SPROM. The page size is 4096 bytes.
  124. * @return SPROM page erase success or not.
  125. * @retval 0 Success
  126. * @retval -1 Erase failed
  127. */
  128. int32_t FMC_Erase_SPROM(void)
  129. {
  130. int32_t ret = 0;
  131. FMC->ISPCMD = FMC_ISPCMD_PAGE_ERASE;
  132. FMC->ISPADDR = FMC_SPROM_BASE;
  133. FMC->ISPDAT = 0x0055AA03UL;
  134. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
  135. while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) { }
  136. if (FMC->ISPCTL & FMC_ISPCTL_ISPFF_Msk)
  137. {
  138. FMC->ISPCTL |= FMC_ISPCTL_ISPFF_Msk;
  139. ret = -1;
  140. }
  141. return ret;
  142. }
  143. /**
  144. * @brief Execute FMC_ISPCMD_BLOCK_ERASE command to erase a flash block. The block size is 4 pages.
  145. * @param[in] u32BlockAddr Address of the flash block to be erased.
  146. * It must be a 4 pages aligned address.
  147. * @return ISP page erase success or not.
  148. * @retval 0 Success
  149. * @retval -1 Erase failed
  150. */
  151. int32_t FMC_Erase_Block(uint32_t u32BlockAddr)
  152. {
  153. int32_t ret = 0;
  154. FMC->ISPCMD = FMC_ISPCMD_BLOCK_ERASE;
  155. FMC->ISPADDR = u32BlockAddr;
  156. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
  157. while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) { }
  158. if (FMC->ISPCTL & FMC_ISPCTL_ISPFF_Msk)
  159. {
  160. FMC->ISPCTL |= FMC_ISPCTL_ISPFF_Msk;
  161. ret = -1;
  162. }
  163. return ret;
  164. }
  165. /**
  166. * @brief Execute FMC_ISPCMD_BANK_ERASE command to erase a flash block.
  167. * @param[in] u32BankAddr Base address of the flash bank to be erased.
  168. * @return ISP page erase success or not.
  169. * @retval 0 Success
  170. * @retval -1 Erase failed
  171. */
  172. int32_t FMC_Erase_Bank(uint32_t u32BankAddr)
  173. {
  174. int32_t ret = 0;
  175. FMC->ISPCMD = FMC_ISPCMD_BANK_ERASE;
  176. FMC->ISPADDR = u32BankAddr;
  177. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
  178. while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) { }
  179. if (FMC->ISPCTL & FMC_ISPCTL_ISPFF_Msk)
  180. {
  181. FMC->ISPCTL |= FMC_ISPCTL_ISPFF_Msk;
  182. ret = -1;
  183. }
  184. return ret;
  185. }
  186. /**
  187. * @brief Execute Erase XOM Region
  188. *
  189. * @param[in] u32XomNum The XOMRn(n=0~3)
  190. *
  191. * @return XOM erase success or not.
  192. * @retval 0 Success
  193. * @retval -1 Erase failed
  194. * @retval -2 Invalid XOM number.
  195. *
  196. * @details Execute FMC_ISPCMD_PAGE_ERASE command to erase XOM.
  197. */
  198. int32_t FMC_EraseXOM(uint32_t u32XomNum)
  199. {
  200. uint32_t u32Addr;
  201. int32_t i32Active, err = 0;
  202. if(u32XomNum >= 4UL)
  203. {
  204. err = -2;
  205. }
  206. if(err == 0)
  207. {
  208. i32Active = FMC_GetXOMState(u32XomNum);
  209. if(i32Active)
  210. {
  211. switch(u32XomNum)
  212. {
  213. case 0u:
  214. u32Addr = (FMC->XOMR0STS & 0xFFFFFF00u) >> 8u;
  215. break;
  216. case 1u:
  217. u32Addr = (FMC->XOMR1STS & 0xFFFFFF00u) >> 8u;
  218. break;
  219. case 2u:
  220. u32Addr = (FMC->XOMR2STS & 0xFFFFFF00u) >> 8u;
  221. break;
  222. case 3u:
  223. u32Addr = (FMC->XOMR3STS & 0xFFFFFF00u) >> 8u;
  224. break;
  225. default:
  226. break;
  227. }
  228. FMC->ISPCMD = FMC_ISPCMD_PAGE_ERASE;
  229. FMC->ISPADDR = u32Addr;
  230. FMC->ISPDAT = 0x55aa03u;
  231. FMC->ISPTRG = 0x1u;
  232. #if ISBEN
  233. __ISB();
  234. #endif
  235. while(FMC->ISPTRG) {}
  236. /* Check ISPFF flag to know whether erase OK or fail. */
  237. if(FMC->ISPCTL & FMC_ISPCTL_ISPFF_Msk)
  238. {
  239. FMC->ISPCTL |= FMC_ISPCTL_ISPFF_Msk;
  240. err = -1;
  241. }
  242. }
  243. else
  244. {
  245. err = -1;
  246. }
  247. }
  248. return err;
  249. }
  250. /**
  251. * @brief Check the XOM is actived or not.
  252. *
  253. * @param[in] u32XomNum The xom number(0~3).
  254. *
  255. * @retval 1 XOM is actived.
  256. * @retval 0 XOM is not actived.
  257. * @retval -2 Invalid XOM number.
  258. *
  259. * @details To get specify XOMRn(n=0~3) active status
  260. */
  261. int32_t FMC_GetXOMState(uint32_t u32XomNum)
  262. {
  263. uint32_t u32act;
  264. int32_t ret = 0;
  265. if(u32XomNum >= 4UL)
  266. {
  267. ret = -2;
  268. }
  269. if(ret >= 0)
  270. {
  271. u32act = (((FMC->XOMSTS) & 0xful) & (1ul << u32XomNum)) >> u32XomNum;
  272. ret = (int32_t)u32act;
  273. }
  274. return ret;
  275. }
  276. /**
  277. * @brief Get the current boot source.
  278. * @return The current boot source.
  279. * @retval 0 Is boot from APROM.
  280. * @retval 1 Is boot from LDROM.
  281. * @retval 2 Is boot from Boot Loader.
  282. */
  283. int32_t FMC_GetBootSource (void)
  284. {
  285. if (FMC->ISPCTL & FMC_ISPCTL_BL_Msk)
  286. {
  287. return 2;
  288. }
  289. if (FMC->ISPCTL & FMC_ISPCTL_BS_Msk)
  290. {
  291. return 1;
  292. }
  293. return 0;
  294. }
  295. /**
  296. * @brief Enable FMC ISP function
  297. * @return None
  298. */
  299. void FMC_Open(void)
  300. {
  301. FMC->ISPCTL |= FMC_ISPCTL_ISPEN_Msk;
  302. }
  303. /**
  304. * @brief Execute FMC_ISPCMD_READ command to read a word from flash.
  305. * @param[in] u32Addr Address of the flash location to be read.
  306. * It must be a word aligned address.
  307. * @return The word data read from specified flash address.
  308. */
  309. uint32_t FMC_Read(uint32_t u32Addr)
  310. {
  311. FMC->ISPCMD = FMC_ISPCMD_READ;
  312. FMC->ISPADDR = u32Addr;
  313. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
  314. while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) { }
  315. return FMC->ISPDAT;
  316. }
  317. /**
  318. * @brief Execute FMC_ISPCMD_READ_64 command to read a double-word from flash.
  319. * @param[in] u32addr Address of the flash location to be read.
  320. * It must be a double-word aligned address.
  321. * @param[out] u32data0 Place holder of word 0 read from flash address u32addr.
  322. * @param[out] u32data1 Place holder of word 0 read from flash address u32addr+4.
  323. * @return 0 Success
  324. * @return -1 Failed
  325. */
  326. int32_t FMC_Read_64(uint32_t u32addr, uint32_t * u32data0, uint32_t * u32data1)
  327. {
  328. int32_t ret = 0;
  329. FMC->ISPCMD = FMC_ISPCMD_READ_64;
  330. FMC->ISPADDR = u32addr;
  331. FMC->ISPDAT = 0x0UL;
  332. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
  333. while (FMC->ISPSTS & FMC_ISPSTS_ISPBUSY_Msk) { }
  334. if (FMC->ISPSTS & FMC_ISPSTS_ISPFF_Msk)
  335. {
  336. FMC->ISPSTS |= FMC_ISPSTS_ISPFF_Msk;
  337. ret = -1;
  338. }
  339. else
  340. {
  341. *u32data0 = FMC->MPDAT0;
  342. *u32data1 = FMC->MPDAT1;
  343. }
  344. return ret;
  345. }
  346. /**
  347. * @brief Get the base address of Data Flash if enabled.
  348. * @retval The base address of Data Flash
  349. */
  350. uint32_t FMC_ReadDataFlashBaseAddr(void)
  351. {
  352. return FMC->DFBA;
  353. }
  354. /**
  355. * @brief Set boot source from LDROM or APROM after next software reset
  356. * @param[in] i32BootSrc
  357. * 1: Boot from LDROM
  358. * 0: Boot from APROM
  359. * @return None
  360. * @details This function is used to switch APROM boot or LDROM boot. User need to call
  361. * FMC_SetBootSource to select boot source first, then use CPU reset or
  362. * System Reset Request to reset system.
  363. */
  364. void FMC_SetBootSource(int32_t i32BootSrc)
  365. {
  366. if(i32BootSrc)
  367. {
  368. FMC->ISPCTL |= FMC_ISPCTL_BS_Msk; /* Boot from LDROM */
  369. }
  370. else
  371. {
  372. FMC->ISPCTL &= ~FMC_ISPCTL_BS_Msk;/* Boot from APROM */
  373. }
  374. }
  375. /**
  376. * @brief Execute ISP FMC_ISPCMD_PROGRAM to program a word to flash.
  377. * @param[in] u32Addr Address of the flash location to be programmed.
  378. * It must be a word aligned address.
  379. * @param[in] u32Data The word data to be programmed.
  380. * @return None
  381. */
  382. void FMC_Write(uint32_t u32Addr, uint32_t u32Data)
  383. {
  384. FMC->ISPCMD = FMC_ISPCMD_PROGRAM;
  385. FMC->ISPADDR = u32Addr;
  386. FMC->ISPDAT = u32Data;
  387. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
  388. while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) { }
  389. }
  390. /**
  391. * @brief Execute ISP FMC_ISPCMD_PROGRAM_64 to program a double-word to flash.
  392. * @param[in] u32addr Address of the flash location to be programmed.
  393. * It must be a double-word aligned address.
  394. * @param[in] u32data0 The word data to be programmed to flash address u32addr.
  395. * @param[in] u32data1 The word data to be programmed to flash address u32addr+4.
  396. * @return 0 Success
  397. * @return -1 Failed
  398. */
  399. int32_t FMC_Write8Bytes(uint32_t u32addr, uint32_t u32data0, uint32_t u32data1)
  400. {
  401. int32_t ret = 0;
  402. FMC->ISPCMD = FMC_ISPCMD_PROGRAM_64;
  403. FMC->ISPADDR = u32addr;
  404. FMC->MPDAT0 = u32data0;
  405. FMC->MPDAT1 = u32data1;
  406. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
  407. while (FMC->ISPSTS & FMC_ISPSTS_ISPBUSY_Msk) { }
  408. if (FMC->ISPSTS & FMC_ISPSTS_ISPFF_Msk)
  409. {
  410. FMC->ISPSTS |= FMC_ISPSTS_ISPFF_Msk;
  411. ret = -1;
  412. }
  413. return ret;
  414. }
  415. /**
  416. * @brief Program Multi-Word data into specified address of flash.
  417. * @param[in] u32Addr Start flash address in APROM where the data chunk to be programmed into.
  418. * This address must be 8-bytes aligned to flash address.
  419. * @param[in] pu32Buf Buffer that carry the data chunk.
  420. * @param[in] u32Len Length of the data chunk in bytes.
  421. * @retval >=0 Number of data bytes were programmed.
  422. * @return -1 Invalid address.
  423. */
  424. int32_t FMC_WriteMultiple(uint32_t u32Addr, uint32_t pu32Buf[], uint32_t u32Len)
  425. {
  426. int i, idx, retval = 0;
  427. if ((u32Addr >= FMC_APROM_END) || ((u32Addr % 8) != 0))
  428. {
  429. return -1;
  430. }
  431. u32Len = u32Len - (u32Len % 8); /* u32Len must be multiple of 8. */
  432. idx = 0;
  433. while (u32Len >= 8)
  434. {
  435. FMC->ISPADDR = u32Addr;
  436. FMC->MPDAT0 = pu32Buf[idx++];
  437. FMC->MPDAT1 = pu32Buf[idx++];
  438. FMC->MPDAT2 = pu32Buf[idx++];
  439. FMC->MPDAT3 = pu32Buf[idx++];
  440. FMC->ISPCMD = FMC_ISPCMD_PROGRAM_MUL;
  441. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
  442. for (i = 16; i < FMC_MULTI_WORD_PROG_LEN; )
  443. {
  444. while (FMC->MPSTS & (FMC_MPSTS_D0_Msk | FMC_MPSTS_D1_Msk))
  445. ;
  446. retval += 8;
  447. u32Len -= 8;
  448. if (u32Len < 8)
  449. {
  450. return retval;
  451. }
  452. if (!(FMC->MPSTS & FMC_MPSTS_MPBUSY_Msk))
  453. {
  454. /* printf(" [WARNING] busy cleared after D0D1 cleared!\n"); */
  455. i += 8;
  456. break;
  457. }
  458. FMC->MPDAT0 = pu32Buf[idx++];
  459. FMC->MPDAT1 = pu32Buf[idx++];
  460. if (i == FMC_MULTI_WORD_PROG_LEN/4)
  461. break; // done
  462. while (FMC->MPSTS & (FMC_MPSTS_D2_Msk | FMC_MPSTS_D3_Msk))
  463. ;
  464. retval += 8;
  465. u32Len -= 8;
  466. if (u32Len < 8)
  467. {
  468. return retval;
  469. }
  470. if (!(FMC->MPSTS & FMC_MPSTS_MPBUSY_Msk))
  471. {
  472. /* printf(" [WARNING] busy cleared after D2D3 cleared!\n"); */
  473. i += 8;
  474. break;
  475. }
  476. FMC->MPDAT2 = pu32Buf[idx++];
  477. FMC->MPDAT3 = pu32Buf[idx++];
  478. }
  479. if (i != FMC_MULTI_WORD_PROG_LEN)
  480. {
  481. /* printf(" [WARNING] Multi-word program interrupted at 0x%x !!\n", i); */
  482. return retval;
  483. }
  484. while (FMC->MPSTS & FMC_MPSTS_MPBUSY_Msk) ;
  485. u32Addr += FMC_MULTI_WORD_PROG_LEN;
  486. }
  487. return retval;
  488. }
  489. /**
  490. * @brief Program a 64-bits data to the specified OTP.
  491. * @param[in] otp_num The OTP number.
  492. * @param[in] low_word Low word of the 64-bits data.
  493. * @param[in] high_word Low word of the 64-bits data.
  494. * @retval 0 Success
  495. * @retval -1 Program failed.
  496. * @retval -2 Invalid OTP number.
  497. */
  498. int32_t FMC_Write_OTP(uint32_t otp_num, uint32_t low_word, uint32_t high_word)
  499. {
  500. int32_t ret = 0;
  501. if (otp_num > 255UL)
  502. {
  503. ret = -2;
  504. }
  505. if (ret == 0)
  506. {
  507. FMC->ISPCMD = FMC_ISPCMD_PROGRAM;
  508. FMC->ISPADDR = FMC_OTP_BASE + otp_num * 8UL;
  509. FMC->ISPDAT = low_word;
  510. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
  511. while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) { }
  512. if (FMC->ISPSTS & FMC_ISPSTS_ISPFF_Msk)
  513. {
  514. FMC->ISPSTS |= FMC_ISPSTS_ISPFF_Msk;
  515. ret = -1;
  516. }
  517. }
  518. if (ret == 0)
  519. {
  520. FMC->ISPCMD = FMC_ISPCMD_PROGRAM;
  521. FMC->ISPADDR = FMC_OTP_BASE + otp_num * 8UL + 4UL;
  522. FMC->ISPDAT = high_word;
  523. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
  524. while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) { }
  525. if (FMC->ISPSTS & FMC_ISPSTS_ISPFF_Msk)
  526. {
  527. FMC->ISPSTS |= FMC_ISPSTS_ISPFF_Msk;
  528. ret = -1;
  529. }
  530. }
  531. return ret;
  532. }
  533. /**
  534. * @brief Read the 64-bits data from the specified OTP.
  535. * @param[in] otp_num The OTP number.
  536. * @param[in] low_word Low word of the 64-bits data.
  537. * @param[in] high_word Low word of the 64-bits data.
  538. * @retval 0 Success
  539. * @retval -1 Read failed.
  540. * @retval -2 Invalid OTP number.
  541. */
  542. int32_t FMC_Read_OTP(uint32_t otp_num, uint32_t *low_word, uint32_t *high_word)
  543. {
  544. int32_t ret = 0;
  545. if (otp_num > 255UL)
  546. {
  547. ret = -2;
  548. }
  549. if (ret == 0)
  550. {
  551. FMC->ISPCMD = FMC_ISPCMD_READ_64;
  552. FMC->ISPADDR = FMC_OTP_BASE + otp_num * 8UL ;
  553. FMC->ISPDAT = 0x0UL;
  554. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
  555. while (FMC->ISPSTS & FMC_ISPSTS_ISPBUSY_Msk) { }
  556. if (FMC->ISPSTS & FMC_ISPSTS_ISPFF_Msk)
  557. {
  558. FMC->ISPSTS |= FMC_ISPSTS_ISPFF_Msk;
  559. ret = -1;
  560. }
  561. else
  562. {
  563. *low_word = FMC->MPDAT0;
  564. *high_word = FMC->MPDAT1;
  565. }
  566. }
  567. return ret;
  568. }
  569. /**
  570. * @brief Lock the specified OTP.
  571. * @param[in] otp_num The OTP number.
  572. * @retval 0 Success
  573. * @retval -1 Failed to write OTP lock bits.
  574. * @retval -2 Invalid OTP number.
  575. */
  576. int32_t FMC_Lock_OTP(uint32_t otp_num)
  577. {
  578. int32_t ret = 0;
  579. if (otp_num > 255UL)
  580. {
  581. ret = -2;
  582. }
  583. if (ret == 0)
  584. {
  585. FMC->ISPCMD = FMC_ISPCMD_PROGRAM;
  586. FMC->ISPADDR = FMC_OTP_BASE + 0x800UL + otp_num * 4UL;
  587. FMC->ISPDAT = 0UL;
  588. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
  589. while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) { }
  590. if (FMC->ISPSTS & FMC_ISPSTS_ISPFF_Msk)
  591. {
  592. FMC->ISPSTS |= FMC_ISPSTS_ISPFF_Msk;
  593. ret = -1;
  594. }
  595. }
  596. return ret;
  597. }
  598. /**
  599. * @brief Check the OTP is locked or not.
  600. * @param[in] otp_num The OTP number.
  601. * @retval 1 OTP is locked.
  602. * @retval 0 OTP is not locked.
  603. * @retval -1 Failed to read OTP lock bits.
  604. * @retval -2 Invalid OTP number.
  605. */
  606. int32_t FMC_Is_OTP_Locked(uint32_t otp_num)
  607. {
  608. int32_t ret = 0;
  609. if (otp_num > 255UL)
  610. {
  611. ret = -2;
  612. }
  613. if (ret == 0)
  614. {
  615. FMC->ISPCMD = FMC_ISPCMD_READ;
  616. FMC->ISPADDR = FMC_OTP_BASE + 0x800UL + otp_num * 4UL;
  617. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
  618. while (FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) { }
  619. if (FMC->ISPSTS & FMC_ISPSTS_ISPFF_Msk)
  620. {
  621. FMC->ISPSTS |= FMC_ISPSTS_ISPFF_Msk;
  622. ret = -1;
  623. }
  624. else
  625. {
  626. if (FMC->ISPDAT != 0xFFFFFFFFUL)
  627. {
  628. ret = 1; /* Lock work was progrmmed. OTP was locked. */
  629. }
  630. }
  631. }
  632. return ret;
  633. }
  634. /**
  635. * @brief Execute FMC_ISPCMD_READ command to read User Configuration.
  636. * @param[out] u32Config A two-word array.
  637. * u32Config[0] holds CONFIG0, while u32Config[1] holds CONFIG1.
  638. * @param[in] u32Count Available word count in u32Config.
  639. * @return Success or not.
  640. * @retval 0 Success.
  641. * @retval -1 Invalid parameter.
  642. */
  643. int32_t FMC_ReadConfig(uint32_t u32Config[], uint32_t u32Count)
  644. {
  645. int32_t ret = 0;
  646. u32Config[0] = FMC_Read(FMC_CONFIG_BASE);
  647. if (u32Count < 2UL)
  648. {
  649. ret = -1;
  650. }
  651. else
  652. {
  653. u32Config[1] = FMC_Read(FMC_CONFIG_BASE+4UL);
  654. }
  655. return ret;
  656. }
  657. /**
  658. * @brief Execute ISP commands to erase then write User Configuration.
  659. * @param[in] u32Config A two-word array.
  660. * u32Config[0] holds CONFIG0, while u32Config[1] holds CONFIG1.
  661. * @param[in] u32Count The number of User Configuration words to be written.
  662. * @return Success or not.
  663. * @retval 0 Success
  664. * @retval -1 Failed
  665. */
  666. int32_t FMC_WriteConfig(uint32_t u32Config[], uint32_t u32Count)
  667. {
  668. int i;
  669. FMC_ENABLE_CFG_UPDATE();
  670. FMC_Erase(FMC_CONFIG_BASE);
  671. if ((FMC_Read(FMC_CONFIG_BASE) != 0xFFFFFFFF) || (FMC_Read(FMC_CONFIG_BASE+4) != 0xFFFFFFFF) ||
  672. (FMC_Read(FMC_CONFIG_BASE+8) != 0xFFFF5A5A))
  673. {
  674. FMC_DISABLE_CFG_UPDATE();
  675. return -1;
  676. }
  677. for (i = 0; i < u32Count; i++)
  678. {
  679. FMC_Write(FMC_CONFIG_BASE+i*4UL, u32Config[i]);
  680. if (FMC_Read(FMC_CONFIG_BASE+i*4UL) != u32Config[i])
  681. {
  682. FMC_DISABLE_CFG_UPDATE();
  683. return -1;
  684. }
  685. }
  686. FMC_DISABLE_CFG_UPDATE();
  687. return 0;
  688. }
  689. /**
  690. * @brief Run CRC32 checksum calculation and get result.
  691. * @param[in] u32addr Starting flash address. It must be a page aligned address.
  692. * @param[in] u32count Byte count of flash to be calculated. It must be multiple of 512 bytes.
  693. * @return Success or not.
  694. * @retval 0 Success.
  695. * @retval 0xFFFFFFFF Invalid parameter.
  696. */
  697. uint32_t FMC_GetChkSum(uint32_t u32addr, uint32_t u32count)
  698. {
  699. uint32_t ret;
  700. if ((u32addr % 512UL) || (u32count % 512UL))
  701. {
  702. ret = 0xFFFFFFFF;
  703. }
  704. else
  705. {
  706. FMC->ISPCMD = FMC_ISPCMD_RUN_CKS;
  707. FMC->ISPADDR = u32addr;
  708. FMC->ISPDAT = u32count;
  709. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
  710. while (FMC->ISPSTS & FMC_ISPSTS_ISPBUSY_Msk) { }
  711. FMC->ISPCMD = FMC_ISPCMD_READ_CKS;
  712. FMC->ISPADDR = u32addr;
  713. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
  714. while (FMC->ISPSTS & FMC_ISPSTS_ISPBUSY_Msk) { }
  715. ret = FMC->ISPDAT;
  716. }
  717. return ret;
  718. }
  719. /**
  720. * @brief Run flash all one verification and get result.
  721. * @param[in] u32addr Starting flash address. It must be a page aligned address.
  722. * @param[in] u32count Byte count of flash to be calculated. It must be multiple of 512 bytes.
  723. * @retval READ_ALLONE_YES The contents of verified flash area are 0xFFFFFFFF.
  724. * @retval READ_ALLONE_NOT Some contents of verified flash area are not 0xFFFFFFFF.
  725. * @retval READ_ALLONE_CMD_FAIL Unexpected error occurred.
  726. */
  727. uint32_t FMC_CheckAllOne(uint32_t u32addr, uint32_t u32count)
  728. {
  729. uint32_t ret = READ_ALLONE_CMD_FAIL;
  730. FMC->ISPSTS = 0x80UL; /* clear check all one bit */
  731. FMC->ISPCMD = FMC_ISPCMD_RUN_ALL1;
  732. FMC->ISPADDR = u32addr;
  733. FMC->ISPDAT = u32count;
  734. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
  735. while (FMC->ISPSTS & FMC_ISPSTS_ISPBUSY_Msk) { }
  736. do
  737. {
  738. FMC->ISPCMD = FMC_ISPCMD_READ_ALL1;
  739. FMC->ISPADDR = u32addr;
  740. FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk;
  741. while (FMC->ISPSTS & FMC_ISPSTS_ISPBUSY_Msk) { }
  742. }
  743. while (FMC->ISPDAT == 0UL);
  744. if (FMC->ISPDAT == READ_ALLONE_YES)
  745. {
  746. ret = FMC->ISPDAT;
  747. }
  748. if (FMC->ISPDAT == READ_ALLONE_NOT)
  749. {
  750. ret = FMC->ISPDAT;
  751. }
  752. return ret;
  753. }
  754. /**
  755. * @brief Setup security key.
  756. * @param[in] key Key 0~2 to be setup.
  757. * @param[in] kpmax Maximum unmatched power-on counting number.
  758. * @param[in] kemax Maximum unmatched counting number.
  759. * @param[in] lock_CONFIG 1: Security key lock CONFIG to write-protect. 0: Don't lock CONFIG.
  760. * @param[in] lock_SPROM 1: Security key lock SPROM to write-protect. 0: Don't lock SPROM.
  761. * @retval 0 Success.
  762. * @retval -1 Key is locked. Cannot overwrite the current key.
  763. * @retval -2 Failed to erase flash.
  764. * @retval -3 Failed to program key.
  765. * @retval -4 Key lock function failed.
  766. * @retval -5 CONFIG lock function failed.
  767. * @retval -6 SPROM lock function failed.
  768. * @retval -7 KPMAX function failed.
  769. * @retval -8 KEMAX function failed.
  770. */
  771. int32_t FMC_SetSPKey(uint32_t key[3], uint32_t kpmax, uint32_t kemax,
  772. const int32_t lock_CONFIG, const int32_t lock_SPROM)
  773. {
  774. uint32_t lock_ctrl = 0UL;
  775. uint32_t u32KeySts;
  776. int32_t ret = 0;
  777. if (FMC->KPKEYSTS != 0x200UL)
  778. {
  779. ret = -1;
  780. }
  781. if (FMC_Erase(FMC_KPROM_BASE))
  782. {
  783. ret = -2;
  784. }
  785. if (FMC_Erase(FMC_KPROM_BASE+0x200UL))
  786. {
  787. ret = -3;
  788. }
  789. if (!lock_CONFIG)
  790. {
  791. lock_ctrl |= 0x1UL;
  792. }
  793. if (!lock_SPROM)
  794. {
  795. lock_ctrl |= 0x2UL;
  796. }
  797. if (ret == 0)
  798. {
  799. FMC_Write(FMC_KPROM_BASE, key[0]);
  800. FMC_Write(FMC_KPROM_BASE+0x4UL, key[1]);
  801. FMC_Write(FMC_KPROM_BASE+0x8UL, key[2]);
  802. FMC_Write(FMC_KPROM_BASE+0xCUL, kpmax);
  803. FMC_Write(FMC_KPROM_BASE+0x10UL, kemax);
  804. FMC_Write(FMC_KPROM_BASE+0x14UL, lock_ctrl);
  805. while (FMC->KPKEYSTS & FMC_KPKEYSTS_KEYBUSY_Msk) { }
  806. u32KeySts = FMC->KPKEYSTS;
  807. if (!(u32KeySts & FMC_KPKEYSTS_KEYLOCK_Msk))
  808. {
  809. /* Security key lock failed! */
  810. ret = -4;
  811. }
  812. else if ((lock_CONFIG && (!(u32KeySts & FMC_KPKEYSTS_CFGFLAG_Msk))) ||
  813. ((!lock_CONFIG) && (u32KeySts & FMC_KPKEYSTS_CFGFLAG_Msk)))
  814. {
  815. /* CONFIG lock failed! */
  816. ret = -5;
  817. }
  818. else if ((lock_SPROM && (!(u32KeySts & FMC_KPKEYSTS_SPFLAG_Msk))) ||
  819. ((!lock_SPROM) && (u32KeySts & FMC_KPKEYSTS_SPFLAG_Msk)))
  820. {
  821. /* CONFIG lock failed! */
  822. ret = -6;
  823. }
  824. else if (((FMC->KPCNT & FMC_KPCNT_KPMAX_Msk) >> FMC_KPCNT_KPMAX_Pos) != kpmax)
  825. {
  826. /* KPMAX failed! */
  827. ret = -7;
  828. }
  829. else if (((FMC->KPKEYCNT & FMC_KPKEYCNT_KPKEMAX_Msk) >> FMC_KPKEYCNT_KPKEMAX_Pos) != kemax)
  830. {
  831. /* KEMAX failed! */
  832. ret = -8;
  833. }
  834. }
  835. return ret;
  836. }
  837. /**
  838. * @brief Execute security key comparison.
  839. * @param[in] key Key 0~2 to be compared.
  840. * @retval 0 Key matched.
  841. * @retval -1 Forbidden. Times of key comparison mismatch reach the maximum count.
  842. * @retval -2 Key mismatched.
  843. * @retval -3 No security key lock. Key comparison is not required.
  844. */
  845. int32_t FMC_CompareSPKey(uint32_t key[3])
  846. {
  847. uint32_t u32KeySts;
  848. int32_t ret = 0;
  849. if (FMC->KPKEYSTS & FMC_KPKEYSTS_FORBID_Msk)
  850. {
  851. /* FMC_CompareSPKey - FORBID! */
  852. ret = -1;
  853. }
  854. if (!(FMC->KPKEYSTS & FMC_KPKEYSTS_KEYLOCK_Msk))
  855. {
  856. /* FMC_CompareSPKey - key is not locked! */
  857. ret = -3;
  858. }
  859. if (ret == 0)
  860. {
  861. FMC->KPKEY0 = key[0];
  862. FMC->KPKEY1 = key[1];
  863. FMC->KPKEY2 = key[2];
  864. FMC->KPKEYTRG = FMC_KPKEYTRG_KPKEYGO_Msk | FMC_KPKEYTRG_TCEN_Msk;
  865. while (FMC->KPKEYSTS & FMC_KPKEYSTS_KEYBUSY_Msk) { }
  866. u32KeySts = FMC->KPKEYSTS;
  867. if (!(u32KeySts & FMC_KPKEYSTS_KEYMATCH_Msk))
  868. {
  869. /* Key mismatched! */
  870. ret = -2;
  871. }
  872. else if (u32KeySts & FMC_KPKEYSTS_KEYLOCK_Msk)
  873. {
  874. /* Key matched, but still be locked! */
  875. ret = -2;
  876. }
  877. }
  878. return ret;
  879. }
  880. /*@}*/ /* end of group FMC_EXPORTED_FUNCTIONS */
  881. /*@}*/ /* end of group FMC_Driver */
  882. /*@}*/ /* end of group Standard_Driver */
  883. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/