boot_spi_flash.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* Copyright (C) 2017 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _BOOT_SPI_FLASH_H_
  13. #define _BOOT_SPI_FLASH_H_
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <stddef.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /**
  21. * opaque data structure for SPI NOR flash
  22. */
  23. typedef struct bootSpiFlash bootSpiFlash_t;
  24. /**
  25. * \brief open SPI flash
  26. *
  27. * When \p name is DRV_NAME_SPI_FLASH_EXT, even CONFIG_BOARD_WITH_EXT_FLASH
  28. * isn't defined, this will initialize external flash.
  29. *
  30. * \param name SPI flash device name
  31. * \return SPI flash instance
  32. */
  33. bootSpiFlash_t *bootSpiFlashOpen(unsigned name);
  34. /**
  35. * \brief prohibit flash erase/program for a certain range
  36. *
  37. * By default, flash erase/program is not prohibited. If it is known that
  38. * certain range will never be changed, this can be called. This range will
  39. * be used to check erase and program parameters. When the erase or program
  40. * address is located in the range, erase or program API will return false.
  41. *
  42. * For multiple non-coninuous ranges, this can be called multple times.
  43. *
  44. * Inside, there is a flag for each block (64KB). So, the check unit is 64KB
  45. * block. Also, \p start will be aligned up, \p end will be aligned down.
  46. *
  47. * \param d SPI flash instance
  48. * \param start range start (inclusive)
  49. * \param end range end (exclusive)
  50. */
  51. void bootSpiFlashSetRangeWriteProhibit(bootSpiFlash_t *d, uint32_t start, uint32_t end);
  52. /**
  53. * \brief clear range write prohibit flag
  54. *
  55. * This is not recommended, just for completeness.
  56. *
  57. * \param d SPI flash instance
  58. * \param start range start (inclusive)
  59. * \param end range end (exclusive)
  60. */
  61. void bootSpiFlashClearRangeWriteProhibit(bootSpiFlash_t *d, uint32_t start, uint32_t end);
  62. /**
  63. * \brief check whether flash offset offset is valid
  64. *
  65. * \param d SPI flash instance
  66. * \param offset flash offset
  67. * \return
  68. * - true if flash offset is valid
  69. * - false if flash offset is invalid
  70. */
  71. bool bootSpiFlashOffsetValid(bootSpiFlash_t *d, uint32_t offset);
  72. /**
  73. * \brief check whether the address is valid map address
  74. *
  75. * \param d SPI flash instance
  76. * \param address address to be checked
  77. * \return
  78. * - true if the address is valid map address
  79. * - false if the address is not valid map address
  80. */
  81. bool bootSpiFlashMapAddressValid(bootSpiFlash_t *d, const void *address);
  82. /**
  83. * \brief mapped address for flash offset
  84. *
  85. * The mapped address is the physical address.
  86. *
  87. * \param d SPI flash instance
  88. * \param offset flash offset
  89. * \return pointer of the mapped address
  90. */
  91. const void *bootSpiFlashMapAddress(bootSpiFlash_t *d, uint32_t offset);
  92. /**
  93. * \brief flash ID
  94. *
  95. * The format of the ID:
  96. * [31:24] 0
  97. * [23:16] capacity bits
  98. * [15:8]
  99. * [7:0] manufacture ID
  100. *
  101. * \param d SPI flash instance
  102. * \return flash ID
  103. */
  104. uint32_t bootSpiFlashID(bootSpiFlash_t *d);
  105. /**
  106. * \brief flash capacity in bytes
  107. *
  108. * \param d SPI flash instance
  109. * \return capacity in bytes
  110. */
  111. uint32_t bootSpiFlashCapacity(bootSpiFlash_t *d);
  112. /**
  113. * \brief wait erase or program in progress finish
  114. *
  115. * \param d SPI flash instance
  116. */
  117. void bootSpiFlashWaitDone(bootSpiFlash_t *d);
  118. /**
  119. * \brief whether erase or program in progress finish
  120. *
  121. * \param d SPI flash instance
  122. * \return
  123. * - true if there are no pending erase or program
  124. * - false if not
  125. */
  126. bool bootSpiFlashIsDone(bootSpiFlash_t *d);
  127. /**
  128. * \brief flash chip erase
  129. *
  130. * This function will wait erase finish.
  131. *
  132. * \param d SPI flash instance
  133. */
  134. void bootSpiFlashChipErase(bootSpiFlash_t *d);
  135. /**
  136. * \brief erase block or sector of flash
  137. *
  138. * 64K/32K/4K erase will be chosen based on offset alignment and size.
  139. *
  140. * This function WON'T wait erase finish.
  141. *
  142. * \param d SPI flash instance
  143. * \param offset flash offset
  144. * \param size flash erase size
  145. * \return
  146. * - the actual erase size
  147. * - -1 for invalid parameters
  148. */
  149. int bootSpiFlashEraseNoWait(bootSpiFlash_t *d, uint32_t offset, size_t size);
  150. /**
  151. * \brief write data to flash
  152. *
  153. * Flash write won't cross page boundary.
  154. *
  155. * This function WON'T wait erase finish.
  156. *
  157. * \param d SPI flash instance
  158. * \param offset flash offset
  159. * \param size flash program size
  160. * \return
  161. * - the actual program size
  162. * - -1 for invalid parameters
  163. */
  164. int bootSpiFlashWriteNoWait(bootSpiFlash_t *d, uint32_t offset, const void *data, size_t size);
  165. /**
  166. * \brief erase region of flash
  167. *
  168. * This function will loop to erase the whole flash region, and wait erase
  169. * done.
  170. *
  171. * \param d SPI flash instance
  172. * \param offset flash offset
  173. * \param size flash erase size
  174. * \return
  175. * - true on success
  176. * - false on error, invalid parameters
  177. */
  178. bool bootSpiFlashErase(bootSpiFlash_t *d, uint32_t offset, size_t size);
  179. /**
  180. * \brief write all data to flash
  181. *
  182. * This function will loop to write the whole flash region, and wait
  183. * program finish. Caller should ensure \a data is not located in the
  184. * same flash.
  185. *
  186. * \param d SPI flash instance
  187. * \param offset flash offset
  188. * \param data data to be written
  189. * \param size flash erase size
  190. * \return
  191. * - true on success
  192. * - false on error, invalid parameters
  193. */
  194. bool bootSpiFlashWrite(bootSpiFlash_t *d, uint32_t offset, const void *data, size_t size);
  195. /**
  196. * \brief read data from flash
  197. *
  198. * \param d SPI flash instance
  199. * \param offset flash offset
  200. * \param data memory for read
  201. * \param size read size
  202. * \return
  203. * - true on success
  204. * - false on error, invalid parameters
  205. */
  206. bool bootSpiFlashRead(bootSpiFlash_t *d, uint32_t offset, void *data, uint32_t size);
  207. /**
  208. * \brief read data from flash, and check with provided data
  209. *
  210. * It is close to \p bootSpiFlashRead with \p memcmp. And it can return false
  211. * earlier at mismatch. Also, it can save another extra buffer.
  212. *
  213. * \param d SPI flash instance
  214. * \param offset flash offset
  215. * \param data memory for read
  216. * \param size read size
  217. * \return
  218. * - true on success
  219. * - false on error, invalid parameters
  220. */
  221. bool bootSpiFlashReadCheck(bootSpiFlash_t *d, uint32_t offset, const void *data, uint32_t size);
  222. /**
  223. * \brief read unique id number
  224. *
  225. * The currently known largest uid length is 16 bytes.
  226. *
  227. * \param d SPI flash instance pointer, must be valid
  228. * \param id unique id, enough to hold uid
  229. * \return
  230. * - uid length
  231. * - -1 on error, invalid parameters or not support
  232. */
  233. int bootSpiFlashReadUniqueId(bootSpiFlash_t *d, uint8_t *id);
  234. #ifdef __cplusplus
  235. }
  236. #endif
  237. #endif