fal_flash_port.c 7.9 KB

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