fal_flash_port.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright (c) 2022-2023 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-03-09 hpmicro First implementation
  9. * 2022-08-01 hpmicro Fixed random crashing during kvdb_init
  10. * 2022-08-03 hpmicro Improved erase speed
  11. * 2023-05-15 hpmicro Disable global interrupt during FLASH operation for FLASH build
  12. *
  13. */
  14. #include <rtthread.h>
  15. #include <rthw.h>
  16. #ifdef RT_USING_FAL
  17. #include "fal.h"
  18. #include "hpm_romapi.h"
  19. #include "board.h"
  20. #include "hpm_l1c_drv.h"
  21. #if defined(FLASH_XIP) && (FLASH_XIP == 1)
  22. static rt_base_t s_interrupt_level;
  23. #define FAL_ENTER_CRITICAL() do {\
  24. rt_enter_critical(); \
  25. fencei();\
  26. s_interrupt_level = rt_hw_interrupt_disable();\
  27. }while(0)
  28. #define FAL_EXIT_CRITICAL() do {\
  29. ROM_API_TABLE_ROOT->xpi_driver_if->software_reset(BOARD_APP_XPI_NOR_XPI_BASE);\
  30. fencei();\
  31. rt_exit_critical();\
  32. rt_hw_interrupt_enable(s_interrupt_level);\
  33. }while(0)
  34. #define FAL_RAMFUNC __attribute__((section(".isr_vector")))
  35. #else
  36. #define FAL_ENTER_CRITICAL() rt_enter_critical()
  37. #define FAL_EXIT_CRITICAL() rt_exit_critical()
  38. #define FAL_RAMFUNC
  39. #endif
  40. /***************************************************************************************************
  41. * FAL Porting Guide
  42. *
  43. * 1. Most FLASH devices do not support RWW (Read-while-Write), the codes to access the FLASH
  44. * must be placed at RAM or ROM code
  45. * 2. During FLASH erase/program, it is recommended to disable the interrupt, or place the
  46. * interrupt related codes to RAM
  47. *
  48. ***************************************************************************************************/
  49. static int init(void);
  50. static int read(long offset, rt_uint8_t *buf, rt_size_t size);
  51. static int write(long offset, const rt_uint8_t *buf, rt_size_t size);
  52. static int erase(long offset, rt_size_t size);
  53. static xpi_nor_config_t s_flashcfg;
  54. /**
  55. * @brief FAL Flash device context
  56. */
  57. struct fal_flash_dev nor_flash0 =
  58. {
  59. .name = NOR_FLASH_DEV_NAME,
  60. /* If porting this code to the device with FLASH connected to XPI1, the address must be changed to 0x90000000 */
  61. .addr = NOR_FLASH_MEM_BASE,
  62. .len = 8 * 1024 * 1024,
  63. .blk_size = 4096,
  64. .ops = { .init = init, .read = read, .write = write, .erase = erase },
  65. .write_gran = 1
  66. };
  67. /**
  68. * @brief FAL initialization
  69. * This function probes the FLASH using the ROM API
  70. */
  71. FAL_RAMFUNC static int init(void)
  72. {
  73. int ret = RT_EOK;
  74. xpi_nor_config_option_t cfg_option;
  75. cfg_option.header.U = BOARD_APP_XPI_NOR_CFG_OPT_HDR;
  76. cfg_option.option0.U = BOARD_APP_XPI_NOR_CFG_OPT_OPT0;
  77. cfg_option.option1.U = BOARD_APP_XPI_NOR_CFG_OPT_OPT1;
  78. FAL_ENTER_CRITICAL();
  79. hpm_stat_t status = rom_xpi_nor_auto_config(BOARD_APP_XPI_NOR_XPI_BASE, &s_flashcfg, &cfg_option);
  80. FAL_EXIT_CRITICAL();
  81. if (status != status_success)
  82. {
  83. ret = -RT_ERROR;
  84. }
  85. else
  86. {
  87. /* update the flash chip information */
  88. rt_uint32_t sector_size;
  89. rom_xpi_nor_get_property(BOARD_APP_XPI_NOR_XPI_BASE, &s_flashcfg, xpi_nor_property_sector_size, &sector_size);
  90. rt_uint32_t flash_size;
  91. rom_xpi_nor_get_property(BOARD_APP_XPI_NOR_XPI_BASE, &s_flashcfg, xpi_nor_property_total_size, &flash_size);
  92. nor_flash0.blk_size = sector_size;
  93. nor_flash0.len = flash_size;
  94. }
  95. return ret;
  96. }
  97. /**
  98. * @brief FAL read function
  99. * Read data from FLASH
  100. * @param offset FLASH offset
  101. * @param buf Buffer to hold data read by this API
  102. * @param size Size of data to be read
  103. * @return actual read bytes
  104. */
  105. FAL_RAMFUNC static int read(long offset, rt_uint8_t *buf, rt_size_t size)
  106. {
  107. rt_uint32_t flash_addr = nor_flash0.addr + offset;
  108. rt_uint32_t aligned_start = HPM_L1C_CACHELINE_ALIGN_DOWN(flash_addr);
  109. rt_uint32_t aligned_end = HPM_L1C_CACHELINE_ALIGN_UP(flash_addr + size);
  110. rt_uint32_t aligned_size = aligned_end - aligned_start;
  111. rt_base_t level = rt_hw_interrupt_disable();
  112. l1c_dc_invalidate(aligned_start, aligned_size);
  113. rt_hw_interrupt_enable(level);
  114. (void) rt_memcpy(buf, (void*) flash_addr, size);
  115. return size;
  116. }
  117. /**
  118. * @brief Write unaligned data to the page
  119. * @param offset FLASH offset
  120. * @param buf Data buffer
  121. * @param size Size of data to be written
  122. * @return actual size of written data or error code
  123. */
  124. FAL_RAMFUNC static int write_unaligned_page_data(long offset, const rt_uint32_t *buf, rt_size_t size)
  125. {
  126. hpm_stat_t status;
  127. FAL_ENTER_CRITICAL();
  128. status = rom_xpi_nor_program(BOARD_APP_XPI_NOR_XPI_BASE, xpi_xfer_channel_auto, &s_flashcfg, buf, offset, size);
  129. FAL_EXIT_CRITICAL();
  130. if (status != status_success)
  131. {
  132. return -RT_ERROR;
  133. rt_kprintf("write failed, status=%d\n", status);
  134. }
  135. return size;
  136. }
  137. /**
  138. * @brief FAL write function
  139. * Write data to specified FLASH address
  140. * @param offset FLASH offset
  141. * @param buf Data buffer
  142. * @param size Size of data to be written
  143. * @return actual size of written data or error code
  144. */
  145. FAL_RAMFUNC static int write(long offset, const rt_uint8_t *buf, rt_size_t size)
  146. {
  147. rt_uint32_t *src = NULL;
  148. rt_uint32_t buf_32[64];
  149. rt_uint32_t write_size;
  150. rt_size_t remaining_size = size;
  151. int ret = (int)size;
  152. rt_uint32_t page_size;
  153. rom_xpi_nor_get_property(BOARD_APP_XPI_NOR_XPI_BASE, &s_flashcfg, xpi_nor_property_page_size, &page_size);
  154. rt_uint32_t offset_in_page = offset % page_size;
  155. if (offset_in_page != 0)
  156. {
  157. rt_uint32_t write_size_in_page = page_size - offset_in_page;
  158. rt_uint32_t write_page_size = MIN(write_size_in_page, size);
  159. (void) rt_memcpy(buf_32, buf, write_page_size);
  160. write_size = write_unaligned_page_data(offset, buf_32, write_page_size);
  161. if (write_size < 0)
  162. {
  163. ret = -RT_ERROR;
  164. goto write_quit;
  165. }
  166. remaining_size -= write_page_size;
  167. offset += write_page_size;
  168. buf += write_page_size;
  169. }
  170. while (remaining_size > 0)
  171. {
  172. write_size = MIN(remaining_size, sizeof(buf_32));
  173. rt_memcpy(buf_32, buf, write_size);
  174. src = &buf_32[0];
  175. FAL_ENTER_CRITICAL();
  176. hpm_stat_t status = rom_xpi_nor_program(BOARD_APP_XPI_NOR_XPI_BASE, xpi_xfer_channel_auto, &s_flashcfg, src,
  177. offset, write_size);
  178. FAL_EXIT_CRITICAL();
  179. if (status != status_success)
  180. {
  181. ret = -RT_ERROR;
  182. rt_kprintf("write failed, status=%d\n", status);
  183. break;
  184. }
  185. remaining_size -= write_size;
  186. buf += write_size;
  187. offset += write_size;
  188. }
  189. write_quit:
  190. return ret;
  191. }
  192. /**
  193. * @brief FAL erase function
  194. * Erase specified FLASH region
  195. * @param offset the start FLASH address to be erased
  196. * @param size size of the region to be erased
  197. * @ret RT_EOK Erase operation is successful
  198. * @retval -RT_ERROR Erase operation failed
  199. */
  200. FAL_RAMFUNC static int erase(long offset, rt_size_t size)
  201. {
  202. rt_uint32_t aligned_size = (size + nor_flash0.blk_size - 1U) & ~(nor_flash0.blk_size - 1U);
  203. hpm_stat_t status;
  204. int ret = (int)size;
  205. rt_uint32_t block_size;
  206. rt_uint32_t sector_size;
  207. (void) rom_xpi_nor_get_property(BOARD_APP_XPI_NOR_XPI_BASE, &s_flashcfg, xpi_nor_property_sector_size, &sector_size);
  208. (void) rom_xpi_nor_get_property(BOARD_APP_XPI_NOR_XPI_BASE, &s_flashcfg, xpi_nor_property_block_size, &block_size);
  209. rt_uint32_t erase_unit;
  210. while (aligned_size > 0)
  211. {
  212. FAL_ENTER_CRITICAL();
  213. if ((offset % block_size == 0) && (aligned_size >= block_size))
  214. {
  215. erase_unit = block_size;
  216. status = rom_xpi_nor_erase_block(BOARD_APP_XPI_NOR_XPI_BASE, xpi_xfer_channel_auto, &s_flashcfg, offset);
  217. }
  218. else
  219. {
  220. erase_unit = sector_size;
  221. status = rom_xpi_nor_erase_sector(BOARD_APP_XPI_NOR_XPI_BASE, xpi_xfer_channel_auto, &s_flashcfg, offset);
  222. }
  223. FAL_EXIT_CRITICAL();
  224. if (status != status_success)
  225. {
  226. ret = -RT_ERROR;
  227. break;
  228. }
  229. offset += erase_unit;
  230. aligned_size -= erase_unit;
  231. }
  232. return ret;
  233. }
  234. #endif /* RT_USING_FAL */