fal_flash_port.c 7.9 KB

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