ql_spi6_nor_flash.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*================================================================
  2. Copyright (c) 2021, Quectel Wireless Solutions Co., Ltd. All rights reserved.
  3. Quectel Wireless Solutions Proprietary and Confidential.
  4. =================================================================*/
  5. /*=================================================================
  6. EDIT HISTORY FOR MODULE
  7. This section contains comments describing changes made to the module.
  8. Notice that changes are listed in reverse chronological order.
  9. WHEN WHO WHAT, WHERE, WHY
  10. ------------ ------- -------------------------------------------------------------------------------
  11. =================================================================*/
  12. /*===========================================================================
  13. * include files
  14. ===========================================================================*/
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "ql_log.h"
  19. #include "drv_spi_flash.h"
  20. #include "ql_spi6_nor_flash.h"
  21. #include "ql_type.h"
  22. #include "osi_api.h"
  23. #include "hal_chip.h"
  24. #include "hal_spi_flash_defs.h"
  25. #define SPI6_NOR_FLASH QL_LOG_LEVEL_INFO
  26. #define QL_SPI6_NOR_FLASH_LOG(msg, ...) QL_LOG(SPI6_NOR_FLASH, "ql_spi6_nor", msg, ##__VA_ARGS__)
  27. #define QL_BOOT_FLASH_END_ADDR CONFIG_BOOT_FLASH_ADDRESS+CONFIG_BOOT_FLASH_SIZE
  28. #define QL_APP_FLASH_END_ADDR CONFIG_APP_FLASH_ADDRESS+CONFIG_APP_FLASH_SIZE
  29. #define QL_APPIMG_FLASH_END_ADDR CONFIG_APPIMG_FLASH_ADDRESS+CONFIG_APPIMG_FLASH_SIZE
  30. #define QL_FS_SYS_FLASH_END_ADDR CONFIG_FS_SYS_FLASH_ADDRESS+CONFIG_FS_SYS_FLASH_SIZE
  31. #if (defined CONFIG_EFS_SYS_FLASH2_ADDRESS) && (CONFIG_EFS_SYS_FLASH2_ADDRESS >= CONFIG_NOR_EXT_PHY_ADDRESS)
  32. #if (defined CONFIG_EFS_SYS_FLASH2_SIZE) && (CONFIG_EFS_SYS_FLASH2_SIZE != 0)
  33. #define QL_EFS_SYS_FLASH2_END_ADDR CONFIG_EFS_SYS_FLASH2_ADDRESS+CONFIG_EFS_SYS_FLASH2_SIZE
  34. #endif
  35. #endif
  36. static bool ql_chk_spi6_embed_flash_addr(uint32 addr,size_t size)
  37. {
  38. if(addr<QL_BOOT_FLASH_END_ADDR)
  39. {
  40. return false;
  41. }
  42. if(addr>=CONFIG_FS_MODEM_FLASH_ADDRESS)
  43. {
  44. return false;
  45. }
  46. if(addr>=CONFIG_APP_FLASH_ADDRESS&&addr<QL_APP_FLASH_END_ADDR) /****CONFIG_APP_FLASH_ADDRESS<=addr<QL_APP_FLASH_END_ADDR*****/
  47. {
  48. return false;
  49. }
  50. if(addr>=CONFIG_APPIMG_FLASH_ADDRESS&&addr<QL_APPIMG_FLASH_END_ADDR) /****CONFIG_APPIMG_FLASH_ADDRESS<=addr<QL_APPIMG_FLASH_END_ADDR*****/
  51. {
  52. return false;
  53. }
  54. if(addr>=CONFIG_FS_SYS_FLASH_ADDRESS&&addr<QL_FS_SYS_FLASH_END_ADDR) /****CONFIG_FS_SYS_FLASH_ADDRESS<=addr<QL_FS_SYS_FLASH_END_ADDR*****/
  55. {
  56. return false;
  57. }
  58. addr=addr+size-1;
  59. if(addr>=CONFIG_APP_FLASH_ADDRESS&&addr<QL_APP_FLASH_END_ADDR) /****CONFIG_APP_FLASH_ADDRESS<=addr<QL_APP_FLASH_END_ADDR*****/
  60. {
  61. return false;
  62. }
  63. if(addr>=CONFIG_APPIMG_FLASH_ADDRESS&&addr<QL_APPIMG_FLASH_END_ADDR) /****CONFIG_APPIMG_FLASH_ADDRESS<=addr<QL_APPIMG_FLASH_END_ADDR*****/
  64. {
  65. return false;
  66. }
  67. if(addr>=CONFIG_FS_SYS_FLASH_ADDRESS&&addr<QL_FS_SYS_FLASH_END_ADDR) /****CONFIG_FS_SYS_FLASH_ADDRESS<=addr<QL_FS_SYS_FLASH_END_ADDR*****/
  68. {
  69. return false;
  70. }
  71. return true;
  72. }
  73. extern unsigned int quec_nor_ext_mount_start_addr;
  74. static bool ql_chk_spi6_ext_flash_addr(uint32 addr,size_t size)
  75. {
  76. if(addr < CONFIG_NOR_EXT_PHY_ADDRESS)
  77. {
  78. return false;
  79. }
  80. #if (CONFIG_APP_FLASH_ADDRESS >= CONFIG_NOR_EXT_PHY_ADDRESS)
  81. if(addr>=CONFIG_APP_FLASH_ADDRESS&&addr<QL_APP_FLASH_END_ADDR) /****CONFIG_APP_FLASH_ADDRESS<=addr<QL_APP_FLASH_END_ADDR*****/
  82. {
  83. return false;
  84. }
  85. #endif
  86. #if (CONFIG_APPIMG_FLASH_ADDRESS >= CONFIG_NOR_EXT_PHY_ADDRESS)
  87. if(addr>=CONFIG_APPIMG_FLASH_ADDRESS&&addr<QL_APPIMG_FLASH_END_ADDR) /****CONFIG_APPIMG_FLASH_ADDRESS<=addr<QL_APPIMG_FLASH_END_ADDR*****/
  88. {
  89. return false;
  90. }
  91. #endif
  92. #if (CONFIG_FS_SYS_FLASH_ADDRESS >= CONFIG_NOR_EXT_PHY_ADDRESS)
  93. if(addr>=CONFIG_FS_SYS_FLASH_ADDRESS&&addr<QL_FS_SYS_FLASH_END_ADDR) /****CONFIG_FS_SYS_FLASH_ADDRESS<=addr<QL_FS_SYS_FLASH_END_ADDR*****/
  94. {
  95. return false;
  96. }
  97. #endif
  98. #if (defined CONFIG_EFS_SYS_FLASH2_ADDRESS) && (CONFIG_EFS_SYS_FLASH2_ADDRESS >= CONFIG_NOR_EXT_PHY_ADDRESS)
  99. #if (defined CONFIG_EFS_SYS_FLASH2_SIZE) && (CONFIG_EFS_SYS_FLASH2_SIZE != 0)
  100. if(addr>=CONFIG_EFS_SYS_FLASH2_ADDRESS&&addr<QL_EFS_SYS_FLASH2_END_ADDR) /****CONFIG_EFS_SYS_FLASH2_ADDRESS<=addr<QL_EFS_SYS_FLASH2_END_ADDR*****/
  101. {
  102. return false;
  103. }
  104. #endif
  105. #endif
  106. addr=addr+size-1;
  107. #if (CONFIG_APP_FLASH_ADDRESS >= CONFIG_NOR_EXT_PHY_ADDRESS)
  108. if(addr>=CONFIG_APP_FLASH_ADDRESS&&addr<QL_APP_FLASH_END_ADDR) /****CONFIG_APP_FLASH_ADDRESS<=addr<QL_APP_FLASH_END_ADDR*****/
  109. {
  110. return false;
  111. }
  112. #endif
  113. #if (CONFIG_APPIMG_FLASH_ADDRESS >= CONFIG_NOR_EXT_PHY_ADDRESS)
  114. if(addr>=CONFIG_APPIMG_FLASH_ADDRESS&&addr<QL_APPIMG_FLASH_END_ADDR) /****CONFIG_APPIMG_FLASH_ADDRESS<=addr<QL_APPIMG_FLASH_END_ADDR*****/
  115. {
  116. return false;
  117. }
  118. #endif
  119. #if (CONFIG_FS_SYS_FLASH_ADDRESS >= CONFIG_NOR_EXT_PHY_ADDRESS)
  120. if(addr>=CONFIG_FS_SYS_FLASH_ADDRESS&&addr<QL_FS_SYS_FLASH_END_ADDR) /****CONFIG_FS_SYS_FLASH_ADDRESS<=addr<QL_FS_SYS_FLASH_END_ADDR*****/
  121. {
  122. return false;
  123. }
  124. #endif
  125. #if (defined CONFIG_EFS_SYS_FLASH2_ADDRESS) && (CONFIG_EFS_SYS_FLASH2_ADDRESS >= CONFIG_NOR_EXT_PHY_ADDRESS)
  126. #if (defined CONFIG_EFS_SYS_FLASH2_SIZE) && (CONFIG_EFS_SYS_FLASH2_SIZE != 0)
  127. if(addr>=CONFIG_EFS_SYS_FLASH2_ADDRESS&&addr<QL_EFS_SYS_FLASH2_END_ADDR) /****CONFIG_EFS_SYS_FLASH2_ADDRESS<=addr<QL_EFS_SYS_FLASH2_END_ADDR*****/
  128. {
  129. return false;
  130. }
  131. #endif
  132. #endif
  133. return true;
  134. }
  135. /*判断flash地址合法性,是否在已分配的分区外*/
  136. bool ql_chk_spi6_flash_addr(uint32 name,uint32 addr,size_t size)
  137. {
  138. if(name != DRV_NAME_SPI_FLASH && name != DRV_NAME_SPI_FLASH_EXT)
  139. {
  140. return false;
  141. }
  142. if(DRV_NAME_SPI_FLASH == name)
  143. {
  144. return ql_chk_spi6_embed_flash_addr(addr, size);
  145. }
  146. return ql_chk_spi6_ext_flash_addr(addr, size);
  147. }
  148. ql_spi6_nor_flash_e ql_spi6_nor_flash_write(uint32 name,uint32 write_addr,void *data,size_t size)
  149. {
  150. bool ret=0;
  151. if(name != DRV_NAME_SPI_FLASH && name != DRV_NAME_SPI_FLASH_EXT)
  152. {
  153. return QL_SPI6_NOR_FLASH_NAME_ERR;
  154. }
  155. drvSpiFlash_t *d_flash = drvSpiFlashOpen(name);
  156. if(d_flash==NULL)
  157. {
  158. return QL_SPI6_NOR_FLASH_WRITE_ERR;
  159. }
  160. if(data==NULL)
  161. {
  162. return QL_SPI6_NOR_FLASH_WRITE_ERR;
  163. }
  164. if(!ql_chk_spi6_flash_addr(name,write_addr,size))
  165. {
  166. QL_SPI6_NOR_FLASH_LOG("wirte address/size illegal");
  167. return QL_SPI6_NOR_FLASH_ADDRESS_ERR;
  168. }
  169. write_addr=HAL_FLASH_OFFSET(write_addr);
  170. ret=drvSpiFlashWrite(d_flash,write_addr,data,size);
  171. if(ret==true)
  172. {
  173. return QL_SPI6_NOR_FLASH_SUCCESS;
  174. }
  175. else
  176. {
  177. return QL_SPI6_NOR_FLASH_WRITE_ERR;
  178. }
  179. }
  180. ql_spi6_nor_flash_e ql_spi6_nor_flash_read(uint32 name,uint32 read_addr,void *data,size_t size)
  181. {
  182. bool ret=0;
  183. if(name != DRV_NAME_SPI_FLASH && name != DRV_NAME_SPI_FLASH_EXT)
  184. {
  185. return QL_SPI6_NOR_FLASH_NAME_ERR;
  186. }
  187. drvSpiFlash_t *d_flash = drvSpiFlashOpen(name);
  188. if(d_flash==NULL)
  189. {
  190. return QL_SPI6_NOR_FLASH_READ_ERR;
  191. }
  192. if(data==NULL)
  193. {
  194. return QL_SPI6_NOR_FLASH_READ_ERR;
  195. }
  196. if(!ql_chk_spi6_flash_addr(name,read_addr,size))
  197. {
  198. QL_SPI6_NOR_FLASH_LOG("read address/size illegal");
  199. return QL_SPI6_NOR_FLASH_ADDRESS_ERR;
  200. }
  201. read_addr=HAL_FLASH_OFFSET(read_addr);
  202. ret=drvSpiFlashRead(d_flash,read_addr,data,size);
  203. if(ret==true)
  204. {
  205. return QL_SPI6_NOR_FLASH_SUCCESS;
  206. }
  207. else
  208. {
  209. return QL_SPI6_NOR_FLASH_READ_ERR;
  210. }
  211. }
  212. /*擦除预留扇区块,每次擦除最小4K erase_addr,size需4K对齐 */
  213. ql_spi6_nor_flash_e ql_spi6_nor_flash_erase(uint32 name,uint32 erase_addr,size_t size)
  214. {
  215. bool ret=0;
  216. if(name != DRV_NAME_SPI_FLASH && name != DRV_NAME_SPI_FLASH_EXT)
  217. {
  218. return QL_SPI6_NOR_FLASH_NAME_ERR;
  219. }
  220. uint32 is_align_4K=0;
  221. drvSpiFlash_t *d_flash = drvSpiFlashOpen(name);
  222. if(d_flash==NULL)
  223. {
  224. return QL_SPI6_NOR_FLASH_ERASE_ERR;
  225. }
  226. if(!ql_chk_spi6_flash_addr(name,erase_addr,size))
  227. {
  228. QL_SPI6_NOR_FLASH_LOG("erase address/size illegal");
  229. return QL_SPI6_NOR_FLASH_ADDRESS_ERR;
  230. }
  231. is_align_4K=erase_addr%SIZE_4K;
  232. if(is_align_4K!=0)
  233. {
  234. return QL_SPI6_NOR_FLASH_OPERATE_ERR;
  235. }
  236. is_align_4K=size%SIZE_4K;
  237. if(is_align_4K!=0)
  238. {
  239. return QL_SPI6_NOR_FLASH_OPERATE_ERR;
  240. }
  241. erase_addr=HAL_FLASH_OFFSET(erase_addr);
  242. ret=drvSpiFlashErase(d_flash,erase_addr,size);
  243. if(ret==true)
  244. {
  245. return QL_SPI6_NOR_FLASH_SUCCESS;
  246. }
  247. else
  248. {
  249. return QL_SPI6_NOR_FLASH_ERASE_ERR;
  250. }
  251. }