flc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /**
  2. * @file flc.h
  3. * @brief Flash Controler driver.
  4. * @details This driver can be used to operate on the embedded flash memory.
  5. */
  6. /* ****************************************************************************
  7. * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included
  17. * in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  22. * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
  23. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  24. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25. * OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. * Except as contained in this notice, the name of Maxim Integrated
  28. * Products, Inc. shall not be used except as stated in the Maxim Integrated
  29. * Products, Inc. Branding Policy.
  30. *
  31. * The mere transfer of this software does not imply any licenses
  32. * of trade secrets, proprietary technology, copyrights, patents,
  33. * trademarks, maskwork rights, or any other form of intellectual
  34. * property whatsoever. Maxim Integrated Products, Inc. retains all
  35. * ownership rights.
  36. *
  37. * $Date: 2019-06-05 16:53:29 -0500 (Wed, 05 Jun 2019) $
  38. * $Revision: 43696 $
  39. *
  40. *************************************************************************** */
  41. /* **** Includes **** */
  42. #include <string.h>
  43. #include "mxc_config.h"
  44. #include "mxc_sys.h"
  45. #include "flc.h"
  46. #include "flc_regs.h"
  47. /* **** Definitions **** */
  48. /* **** Globals **** */
  49. /* **** Functions **** */
  50. // *****************************************************************************
  51. #if defined (__ICCARM__)
  52. #pragma section=".flashprog"
  53. #endif
  54. #if defined ( __GNUC__ )
  55. __attribute__ ((section(".flashprog")))
  56. #endif
  57. static int prepare_flc(void)
  58. {
  59. // Set flash clock divider to generate a 1MHz clock from the APB clock
  60. MXC_FLC->clkdiv = SystemCoreClock / 1000000;
  61. /* Check if the flash controller is busy */
  62. if (FLC_Busy()) {
  63. return E_BUSY;
  64. }
  65. /* Clear stale errors */
  66. if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
  67. MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
  68. }
  69. /* Unlock flash */
  70. MXC_FLC->cn = (MXC_FLC->cn & ~MXC_F_FLC_CN_UNLOCK) | MXC_S_FLC_CN_UNLOCK_UNLOCKED;
  71. return E_NO_ERROR;
  72. }
  73. // *****************************************************************************
  74. #if defined (__ICCARM__)
  75. // IAR memory section declaration for the in-system flash programming functions to be loaded in RAM.
  76. #pragma section=".flashprog"
  77. #endif
  78. #if defined ( __GNUC__ )
  79. __attribute__ ((section(".flashprog")))
  80. #endif
  81. int FLC_Init(const sys_cfg_flc_t *sys_cfg)
  82. {
  83. SYS_FLC_Init(sys_cfg);
  84. return E_NO_ERROR;
  85. }
  86. // *****************************************************************************
  87. #if defined (__ICCARM__)
  88. // IAR memory section declaration for the in-system flash programming functions to be loaded in RAM.
  89. #pragma section=".flashprog"
  90. #endif
  91. #if defined ( __GNUC__ )
  92. __attribute__ ((section(".flashprog")))
  93. #endif
  94. int FLC_Busy(void)
  95. {
  96. return (MXC_FLC->cn & (MXC_F_FLC_CN_WR | MXC_F_FLC_CN_ME | MXC_F_FLC_CN_PGE));
  97. }
  98. // *****************************************************************************
  99. #if defined (__ICCARM__)
  100. #pragma section=".flashprog"
  101. #endif
  102. #if defined ( __GNUC__ )
  103. __attribute__ ((section(".flashprog")))
  104. #endif
  105. int FLC_MassErase(void)
  106. {
  107. int err;
  108. if ((err = prepare_flc()) != E_NO_ERROR)
  109. return err;
  110. /* Write mass erase code */
  111. MXC_FLC->cn = (MXC_FLC->cn & ~MXC_F_FLC_CN_ERASE_CODE) | MXC_S_FLC_CN_ERASE_CODE_ERASEALL;
  112. /* Issue mass erase command */
  113. MXC_FLC->cn |= MXC_F_FLC_CN_ME;
  114. /* Wait until flash operation is complete */
  115. while (FLC_Busy());
  116. /* Lock flash */
  117. MXC_FLC->cn &= ~MXC_F_FLC_CN_UNLOCK;
  118. /* Check access violations */
  119. if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
  120. MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
  121. return E_BAD_STATE;
  122. }
  123. SYS_Flash_Operation();
  124. return E_NO_ERROR;
  125. }
  126. // *****************************************************************************
  127. #if defined (__ICCARM__)
  128. #pragma section=".flashprog"
  129. #endif
  130. #if defined ( __GNUC__ )
  131. __attribute__ ((section(".flashprog")))
  132. #endif
  133. int FLC_PageErase(uint32_t address)
  134. {
  135. int err;
  136. if ((err = prepare_flc()) != E_NO_ERROR)
  137. return err;
  138. // Align address on page boundary
  139. address = address - (address % MXC_FLASH_PAGE_SIZE);
  140. /* Write page erase code */
  141. MXC_FLC->cn = (MXC_FLC->cn & ~MXC_F_FLC_CN_ERASE_CODE) | MXC_S_FLC_CN_ERASE_CODE_ERASEPAGE;
  142. /* Issue page erase command */
  143. MXC_FLC->addr = address;
  144. MXC_FLC->cn |= MXC_F_FLC_CN_PGE;
  145. /* Wait until flash operation is complete */
  146. while (FLC_Busy());
  147. /* Lock flash */
  148. MXC_FLC->cn &= ~MXC_F_FLC_CN_UNLOCK;
  149. /* Check access violations */
  150. if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
  151. MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
  152. return E_BAD_STATE;
  153. }
  154. SYS_Flash_Operation();
  155. return E_NO_ERROR;
  156. }
  157. // *****************************************************************************
  158. #if defined (__ICCARM__)
  159. #pragma section=".flashprog"
  160. #endif
  161. #if defined ( __GNUC__ )
  162. __attribute__ ((section(".flashprog")))
  163. #endif
  164. int FLC_Erase(uint32_t start, uint32_t end)
  165. {
  166. int retval;
  167. uint32_t addr;
  168. // Align start and end on page boundaries
  169. start = start - (start % MXC_FLASH_PAGE_SIZE);
  170. end = end - (end % MXC_FLASH_PAGE_SIZE);
  171. for (addr = start; addr <= end; addr += MXC_FLASH_PAGE_SIZE) {
  172. retval = FLC_PageErase(addr);
  173. if (retval != E_NO_ERROR) {
  174. return retval;
  175. }
  176. }
  177. return E_NO_ERROR;
  178. }
  179. // *****************************************************************************
  180. #if defined (__ICCARM__)
  181. #pragma section=".flashprog"
  182. #endif
  183. #if defined ( __GNUC__ )
  184. __attribute__ ((section(".flashprog")))
  185. #endif
  186. int FLC_BufferErase(uint32_t start, uint32_t end, uint8_t *buffer, unsigned length)
  187. {
  188. int retval;
  189. uint32_t start_align, start_len, end_align, end_len;
  190. // Align start and end on page boundaries, calculate length of data to buffer
  191. start_align = start - (start % MXC_FLASH_PAGE_SIZE);
  192. start_len = (start % MXC_FLASH_PAGE_SIZE);
  193. end_align = end - (end % MXC_FLASH_PAGE_SIZE);
  194. end_len = ((MXC_FLASH_PAGE_SIZE - (end % MXC_FLASH_PAGE_SIZE)) % MXC_FLASH_PAGE_SIZE);
  195. // Make sure the length of buffer is sufficient
  196. if ((length < start_len) || (length < end_len)) {
  197. return E_BAD_PARAM;
  198. }
  199. // Start and end address are in the same page
  200. if (start_align == end_align) {
  201. if (length < (start_len + end_len)) {
  202. return E_BAD_PARAM;
  203. }
  204. // Buffer first page data and last page data, erase and write
  205. memcpy(buffer, (void*)start_align, start_len);
  206. memcpy(&buffer[start_len], (void*)end, end_len);
  207. retval = FLC_PageErase(start_align);
  208. if (retval != E_NO_ERROR) {
  209. return retval;
  210. }
  211. retval = FLC_Write(start_align, start_len, buffer);
  212. if (retval != E_NO_ERROR) {
  213. return retval;
  214. }
  215. retval = FLC_Write(end, end_len, &buffer[start_len]);
  216. if (retval != E_NO_ERROR) {
  217. return retval;
  218. }
  219. return E_NO_ERROR;
  220. }
  221. // Buffer, erase, and write the data in the first page
  222. memcpy(buffer, (void*)start_align, start_len);
  223. retval = FLC_PageErase(start_align);
  224. if (retval != E_NO_ERROR) {
  225. return retval;
  226. }
  227. retval = FLC_Write(start_align, start_len, buffer);
  228. if (retval != E_NO_ERROR) {
  229. return retval;
  230. }
  231. // Buffer, erase, and write the data in the last page
  232. memcpy(buffer, (void*)end, end_len);
  233. retval = FLC_PageErase(end_align);
  234. if (retval != E_NO_ERROR) {
  235. return retval;
  236. }
  237. retval = FLC_Write(end, end_len, buffer);
  238. if (retval != E_NO_ERROR) {
  239. return retval;
  240. }
  241. // Erase the remaining pages
  242. if (start_align != end_align) {
  243. return FLC_Erase((start_align + MXC_FLASH_PAGE_SIZE), (end_align - MXC_FLASH_PAGE_SIZE));
  244. }
  245. return E_NO_ERROR;
  246. }
  247. // *****************************************************************************
  248. #if defined (__ICCARM__)
  249. #pragma section=".flashprog"
  250. #endif
  251. #if defined ( __GNUC__ )
  252. __attribute__ ((section(".flashprog")))
  253. #endif
  254. int FLC_Write32(uint32_t address, uint32_t data)
  255. {
  256. int err;
  257. // Address checked if it is byte addressable
  258. if (address & 0x3) {
  259. return E_BAD_PARAM;
  260. }
  261. if ((err = prepare_flc()) != E_NO_ERROR)
  262. return err;
  263. // write in 32-bit units
  264. MXC_FLC->cn |= MXC_F_FLC_CN_WDTH;
  265. MXC_FLC->cn &= ~MXC_F_FLC_CN_BRST;
  266. // write the data
  267. MXC_FLC->addr = address;
  268. MXC_FLC->data[0] = data;
  269. MXC_FLC->cn |= MXC_F_FLC_CN_WR;
  270. /* Wait until flash operation is complete */
  271. while (FLC_Busy()) {}
  272. /* Lock flash */
  273. MXC_FLC->cn &= ~MXC_F_FLC_CN_UNLOCK;
  274. /* Check access violations */
  275. if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
  276. MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
  277. return E_BAD_STATE;
  278. }
  279. SYS_Flash_Operation();
  280. return E_NO_ERROR;
  281. }
  282. // *****************************************************************************
  283. #if defined (__ICCARM__)
  284. #pragma section=".flashprog"
  285. #endif
  286. #if defined ( __GNUC__ )
  287. __attribute__ ((section(".flashprog")))
  288. #endif
  289. int FLC_Write128(uint32_t address, uint32_t *data)
  290. {
  291. int err;
  292. // Address checked if it is word addressable
  293. if (address & 0xF) {
  294. return E_BAD_PARAM;
  295. }
  296. if ((err = prepare_flc()) != E_NO_ERROR)
  297. return err;
  298. // write 128-bits
  299. MXC_FLC->cn &= ~MXC_F_FLC_CN_WDTH;
  300. // write the data
  301. MXC_FLC->addr = address;
  302. memcpy((void*)&MXC_FLC->data[0], data, 16);
  303. MXC_FLC->cn |= MXC_F_FLC_CN_WR;
  304. /* Wait until flash operation is complete */
  305. while (FLC_Busy());
  306. /* Lock flash */
  307. MXC_FLC->cn &= ~MXC_F_FLC_CN_UNLOCK;
  308. /* Check access violations */
  309. if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
  310. MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
  311. return E_BAD_STATE;
  312. }
  313. SYS_Flash_Operation();
  314. return E_NO_ERROR;
  315. }
  316. // *****************************************************************************
  317. #if defined (__ICCARM__)
  318. #pragma section=".flashprog"
  319. #endif
  320. #if defined ( __GNUC__ )
  321. __attribute__ ((section(".flashprog")))
  322. #endif
  323. int FLC_Write(uint32_t address, uint32_t length, uint8_t *buffer)
  324. {
  325. int err;
  326. uint32_t bytes_written;
  327. uint8_t current_data[4];
  328. if ((err = prepare_flc()) != E_NO_ERROR)
  329. return err;
  330. // write in 32-bit units until we are 128-bit aligned
  331. MXC_FLC->cn &= ~MXC_F_FLC_CN_BRST;
  332. MXC_FLC->cn |= MXC_F_FLC_CN_WDTH;
  333. // Align the address and read/write if we have to
  334. if (address & 0x3) {
  335. // Figure out how many bytes we have to write to round up the address
  336. bytes_written = 4 - (address & 0x3);
  337. // Save the data currently in the flash
  338. memcpy(current_data, (void*)(address & (~0x3)), 4);
  339. // Modify current_data to insert the data from buffer
  340. memcpy(&current_data[4-bytes_written], buffer, bytes_written);
  341. // Write the modified data
  342. MXC_FLC->addr = address - (address % 4);
  343. memcpy((void*)&MXC_FLC->data[0], &current_data, 4);
  344. MXC_FLC->cn |= MXC_F_FLC_CN_WR;
  345. /* Wait until flash operation is complete */
  346. while (FLC_Busy());
  347. address += bytes_written;
  348. length -= bytes_written;
  349. buffer += bytes_written;
  350. }
  351. while ( (length >= 4) && ((address & 0xF) != 0) ) {
  352. MXC_FLC->addr = address;
  353. memcpy((void*)&MXC_FLC->data[0], buffer, 4);
  354. MXC_FLC->cn |= MXC_F_FLC_CN_WR;
  355. /* Wait until flash operation is complete */
  356. while (FLC_Busy());
  357. address += 4;
  358. length -= 4;
  359. buffer += 4;
  360. }
  361. if (length >= 16) {
  362. // write in 128-bit bursts while we can
  363. MXC_FLC->cn &= ~MXC_F_FLC_CN_WDTH;
  364. while (length >= 16) {
  365. MXC_FLC->addr = address;
  366. memcpy((void*)&MXC_FLC->data[0], buffer, 16);
  367. MXC_FLC->cn |= MXC_F_FLC_CN_WR;
  368. /* Wait until flash operation is complete */
  369. while (FLC_Busy());
  370. address += 16;
  371. length -= 16;
  372. buffer += 16;
  373. }
  374. // Return to 32-bit writes.
  375. MXC_FLC->cn |= MXC_F_FLC_CN_WDTH;
  376. }
  377. while (length >= 4) {
  378. MXC_FLC->addr = address;
  379. memcpy((void*)&MXC_FLC->data[0], buffer, 4);
  380. MXC_FLC->cn |= MXC_F_FLC_CN_WR;
  381. /* Wait until flash operation is complete */
  382. while (FLC_Busy());
  383. address += 4;
  384. length -= 4;
  385. buffer += 4;
  386. }
  387. if (length > 0) {
  388. // Save the data currently in the flash
  389. memcpy(current_data, (void*)(address), 4);
  390. // Modify current_data to insert the data from buffer
  391. memcpy(current_data, buffer, length);
  392. MXC_FLC->addr = address;
  393. memcpy((void*)&MXC_FLC->data[0], current_data, 4);
  394. MXC_FLC->cn |= MXC_F_FLC_CN_WR;
  395. /* Wait until flash operation is complete */
  396. while (FLC_Busy());
  397. }
  398. /* Lock flash */
  399. MXC_FLC->cn &= ~MXC_F_FLC_CN_UNLOCK;
  400. /* Check access violations */
  401. if (MXC_FLC->intr & MXC_F_FLC_INTR_AF) {
  402. MXC_FLC->intr &= ~MXC_F_FLC_INTR_AF;
  403. return E_BAD_STATE;
  404. }
  405. SYS_Flash_Operation();
  406. return E_NO_ERROR;
  407. }
  408. int FLC_EnableInt(uint32_t mask)
  409. {
  410. uint32_t tmp;
  411. mask &= (MXC_F_FLC_INTR_DONEIE | MXC_F_FLC_INTR_AFIE);
  412. if (!mask) {
  413. /* No bits set? Wasn't something we can enable. */
  414. return E_BAD_PARAM;
  415. }
  416. /* Careful with access_fail bit, as it is W0C */
  417. tmp = MXC_FLC->intr | MXC_F_FLC_INTR_AF;
  418. /* Don't lose done flag */
  419. tmp &= ~(MXC_F_FLC_INTR_DONE);
  420. /* Apply enables and write back */
  421. MXC_FLC->intr = (tmp | mask);
  422. return E_NO_ERROR;
  423. }
  424. int FLC_DisableInt(uint32_t mask)
  425. {
  426. uint32_t tmp;
  427. mask &= (MXC_F_FLC_INTR_DONEIE | MXC_F_FLC_INTR_AFIE);
  428. if (!mask) {
  429. /* No bits set? Wasn't something we can disable. */
  430. return E_BAD_PARAM;
  431. }
  432. /* Careful with access_fail bit, as it is W0C */
  433. tmp = MXC_FLC->intr | MXC_F_FLC_INTR_AF;
  434. /* Don't lose done flag */
  435. tmp &= ~(MXC_F_FLC_INTR_DONE);
  436. /* Apply disables and write back */
  437. MXC_FLC->intr = (tmp & ~mask);
  438. return E_NO_ERROR;
  439. }
  440. int FLC_GetFlags(void)
  441. {
  442. return (MXC_FLC->intr & (MXC_F_FLC_INTR_DONE | MXC_F_FLC_INTR_AF));
  443. }
  444. int FLC_ClearFlags(uint32_t mask)
  445. {
  446. mask &= (MXC_F_FLC_INTR_DONE | MXC_F_FLC_INTR_AF);
  447. if (!mask) {
  448. /* No bits set? Wasn't something we can clear. */
  449. return E_BAD_PARAM;
  450. }
  451. // Both bits are write zero clear
  452. MXC_FLC->intr ^= mask;
  453. return E_NO_ERROR;
  454. }
  455. int FLC_UnlockInfoBlock()
  456. {
  457. MXC_FLC->acntl = 0x3a7f5ca3;
  458. MXC_FLC->acntl = 0xa1e34f20;
  459. MXC_FLC->acntl = 0x9608b2c1;
  460. return E_NO_ERROR;
  461. }
  462. int FLC_LockInfoBlock()
  463. {
  464. MXC_FLC->acntl = 0xDEADBEEF;
  465. return E_NO_ERROR;
  466. }