hal_spi_flash.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* Copyright (C) 2018 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 _HAL_SPI_FLASH_H_
  13. #define _HAL_SPI_FLASH_H_
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <stddef.h>
  17. #include "drv_config.h"
  18. #include "osi_compiler.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /**
  23. * \brief flash instance
  24. *
  25. * The preperty is derived from manufacture ID. It is found that even
  26. * for the same manufacture ID, the property may be different, for
  27. * example, security register block size. In this case, user can
  28. * override the perperty.
  29. */
  30. typedef struct halSpiFlash
  31. {
  32. /** hardware controller register base */
  33. uintptr_t hwp;
  34. /** manufacture id */
  35. unsigned mid;
  36. /** capacity in bytes */
  37. unsigned capacity;
  38. /** security register block size, 0 for not support */
  39. uint16_t sreg_block_size;
  40. /** internal flash type */
  41. uint8_t type : 4;
  42. /** status register wp type */
  43. uint8_t wp_type : 4;
  44. /** read uid type */
  45. uint8_t uid_type : 4;
  46. /** whether cpid is supported */
  47. uint8_t cpid_type : 4;
  48. /** minimal security register number */
  49. uint8_t sreg_min_num : 4;
  50. /** maximum security register number */
  51. uint8_t sreg_max_num : 4;
  52. /** whether volatile sr is supported */
  53. bool volatile_sr_en : 1;
  54. /** whether suspend resume is supported */
  55. bool suspend_en : 1;
  56. /** whether 5AH to read SFDP supported */
  57. bool sfdp_en : 1;
  58. /** whether 01H can write both SR1 and SR2 */
  59. bool write_sr12 : 1;
  60. /** whether 35H can read SR2 */
  61. bool has_sr2 : 1;
  62. /** whether has GD_SR_SUS1 */
  63. bool has_sus1 : 1;
  64. /** whether has GD_SR_SUS2 */
  65. bool has_sus2 : 1;
  66. /** force 4K erase on first block */
  67. bool force_4k_first_block : 1;
  68. /** force 4K erase on last block */
  69. bool force_4k_last_block : 1;
  70. } halSpiFlash_t;
  71. /**
  72. * \brief write enable
  73. *
  74. * \param d hal spi flash instance
  75. */
  76. void halSpiFlashWriteEnable(const halSpiFlash_t *d);
  77. #if defined(CONFIG_SOC_8910) && defined(CONFIG_SUPPORT_LC_FLASH)
  78. void halSpiFlash4ByteAddressMode(const halSpiFlash_t *d);
  79. void halSpiFlashDisenable4ByteAddressMode(const halSpiFlash_t *d);
  80. #endif
  81. /**
  82. * \brief write disable
  83. *
  84. * \param d hal spi flash instance
  85. */
  86. void halSpiFlashWriteDisable(const halSpiFlash_t *d);
  87. /**
  88. * \brief program suspend
  89. *
  90. * \param d hal spi flash instance
  91. */
  92. void halSpiFlashProgramSuspend(const halSpiFlash_t *d);
  93. /**
  94. * \brief erase suspend
  95. *
  96. * \param d hal spi flash instance
  97. */
  98. void halSpiFlashEraseSuspend(const halSpiFlash_t *d);
  99. /**
  100. * \brief program resume
  101. *
  102. * \param d hal spi flash instance
  103. */
  104. void halSpiFlashProgramResume(const halSpiFlash_t *d);
  105. /**
  106. * \brief erase resume
  107. *
  108. * \param d hal spi flash instance
  109. */
  110. void halSpiFlashEraseResume(const halSpiFlash_t *d);
  111. /**
  112. * \brief chip erase
  113. *
  114. * \param d hal spi flash instance
  115. */
  116. void halSpiFlashChipErase(const halSpiFlash_t *d);
  117. /**
  118. * \brief reset enable
  119. *
  120. * \param d hal spi flash instance
  121. */
  122. void halSpiFlashResetEnable(const halSpiFlash_t *d);
  123. /**
  124. * \brief reset
  125. *
  126. * \param d hal spi flash instance
  127. */
  128. void halSpiFlashReset(const halSpiFlash_t *d);
  129. /**
  130. * \brief read status register
  131. *
  132. * When only one SR is supported, it will return the SR. When two SR are
  133. * supported, it will return 16bits SR, SR-1 at LSB and SR-2 at MSB.
  134. *
  135. * \param d hal spi flash instance
  136. * \return status register
  137. */
  138. uint16_t halSpiFlashReadSR(const halSpiFlash_t *d);
  139. /**
  140. * \brief write status register
  141. *
  142. * The implementation will depend on flash property, one status register,
  143. * two status registers with one command, or two status registers with
  144. * separated commands.
  145. *
  146. * Inside, it will send write enable command, and wait write finish.
  147. *
  148. * \param d hal spi flash instance
  149. * \param sr status register value
  150. */
  151. void halSpiFlashWriteSR(const halSpiFlash_t *d, uint16_t sr);
  152. /**
  153. * \brief page program
  154. *
  155. * \p data shouldn't be allocated in flash. \p size should be less than
  156. * hardware TX fifo size.
  157. *
  158. * It will only send program command, write enable and wait finish shall
  159. * be considered by caller.
  160. *
  161. * \param d hal spi flash instance
  162. * \param offset flash address
  163. * \param data data to be programmed
  164. * \param size data size
  165. */
  166. void halSpiFlashPageProgram(const halSpiFlash_t *d, uint32_t offset, const void *data, uint32_t size);
  167. /**
  168. * \brief 4K/32K/64K erase
  169. *
  170. * \p size can only be 4K, 32K or 64K. \p offset should be \p size aligned.
  171. *
  172. * \param d hal spi flash instance
  173. * \param offset flash address
  174. * \param size erase size
  175. */
  176. void halSpiFlashErase(const halSpiFlash_t *d, uint32_t offset, uint32_t size);
  177. /**
  178. * \brief read unique id number
  179. *
  180. * Not all flash supports unique id, and the unique id size is different even
  181. * supported. \p uid should be large enough.
  182. *
  183. * \param d hal spi flash instance
  184. * \param uid unique id
  185. * \return
  186. * - unique id size
  187. * - 0 if unique id is not supported
  188. */
  189. int halSpiFlashReadUniqueId(const halSpiFlash_t *d, uint8_t *uid);
  190. /**
  191. * \brief read cp id
  192. *
  193. * Not all flash support cp id.
  194. *
  195. * \param d hal spi flash instance
  196. * \return
  197. * - cp id
  198. * - 0 if not supported
  199. */
  200. uint16_t halSpiFlashReadCpId(const halSpiFlash_t *d);
  201. /**
  202. * \brief deep power down
  203. *
  204. * \param d hal spi flash instance
  205. */
  206. void halSpiFlashDeepPowerDown(const halSpiFlash_t *d);
  207. /**
  208. * \brief release from deep power down
  209. *
  210. * There are delay inside, to make sure flash is accessible at return.
  211. *
  212. * \param d hal spi flash instance
  213. */
  214. void halSpiFlashReleaseDeepPowerDown(const halSpiFlash_t *d);
  215. /**
  216. * \brief read SFDP
  217. *
  218. * \param d hal spi flash instance
  219. * \param address SFDP address
  220. * \param data data to be read
  221. * \param size read size
  222. * \return
  223. * - true on success
  224. * - false of the feature is not supported
  225. */
  226. bool halSpiFlashReadSFDP(const halSpiFlash_t *d, uint32_t address, void *data, uint32_t size);
  227. /**
  228. * \brief read status register and check WIP bit
  229. *
  230. * \param d hal spi flash instance
  231. * \return
  232. * - true if WIP is not set in status register
  233. * - false WIP is set in status register
  234. */
  235. bool halSpiFlashIsWipFinished(const halSpiFlash_t *d);
  236. /**
  237. * \brief wait WIP bit in status register unset
  238. *
  239. * \param d hal spi flash instance
  240. */
  241. void halSpiFlashWaitWipFinish(const halSpiFlash_t *d);
  242. /**
  243. * \brief read security register
  244. *
  245. * Not all flash supports security register, and the valid \p num and
  246. * \p address is different even supported.
  247. *
  248. * The maximum \p size is 4. When more bytes are needed, caller should
  249. * use loop for multiple read.
  250. *
  251. * \param d hal spi flash instance
  252. * \param num securerity register number
  253. * \param address security register address
  254. * \param data pointer to output data
  255. * \param size read size
  256. * \return
  257. * - true on success
  258. * - false on failed, invalid parameter or not support
  259. */
  260. bool halSpiFlashReadSecurityRegister(const halSpiFlash_t *d, uint8_t num, uint16_t address, void *data, uint32_t size);
  261. /**
  262. * \brief program security register
  263. *
  264. * Not all flash supports security register, and the valid \p num and
  265. * \p address is different even supported.
  266. *
  267. * \p data shouldn't be allocated in flash. \p size should be less than
  268. * (can't be equal to) hardware TX fifo size.
  269. *
  270. * \param d hal spi flash instance
  271. * \param num securerity register number
  272. * \param address security register address
  273. * \param data data to be programed
  274. * \param size program size
  275. * \return
  276. * - true on success
  277. * - false on failed, invalid parameter or not support
  278. */
  279. bool halSpiFlashProgramSecurityRegister(const halSpiFlash_t *d, uint8_t num, uint16_t address, const void *data, uint32_t size);
  280. /**
  281. * \brief erase security register
  282. *
  283. * Not all flash supports security register, and the valid \p num is
  284. * different even supported.
  285. *
  286. * \param d hal spi flash instance
  287. * \param num securerity register number
  288. * \return
  289. * - true on success
  290. * - false on failed, invalid parameter or not support
  291. */
  292. bool halSpiFlashEraseSecurityRegister(const halSpiFlash_t *d, uint8_t num);
  293. /**
  294. * \brief lock security register
  295. *
  296. * Not all flash supports security register, and the valid \p num is
  297. * different even supported.
  298. *
  299. * \param d hal spi flash instance
  300. * \param num securerity register number
  301. * \return
  302. * - true on success
  303. * - false on failed, invalid parameter or not support
  304. */
  305. bool halSpiFlashLockSecurityRegister(const halSpiFlash_t *d, uint8_t num);
  306. /**
  307. * \brief whether security register is locked
  308. *
  309. * Not all flash supports security register, and the valid \p num is
  310. * different even supported.
  311. *
  312. * \param d hal spi flash instance
  313. * \param num securerity register number
  314. * \return
  315. * - true if \p num is valid and locked
  316. * - false if not locked, or invalid parameter
  317. */
  318. bool halSpiFlashIsSecurityRegisterLocked(const halSpiFlash_t *d, uint8_t num);
  319. /**
  320. * \brief the actual write protected region
  321. *
  322. * Status register can't set write protection for arbitrary region. This returns
  323. * the real protected region.
  324. *
  325. * \param d hal spi flash instance
  326. * \param offset range start
  327. * \param size range size
  328. * \return real protected range
  329. */
  330. osiUintRange_t halSpiFlashWpRange(const halSpiFlash_t *prop, uint32_t offset, uint32_t size);
  331. /**
  332. * \brief handling before program/erase
  333. *
  334. * Check status register to ensure the range is not write protected, and
  335. * send write enable command.
  336. *
  337. * When volatile status register feature is not supported, it won't change
  338. * status register.
  339. *
  340. * \param d hal spi flash instance
  341. * \param offset range start to be program/erase
  342. * \param size range size to be program/erase
  343. */
  344. void halSpiFlashPrepareEraseProgram(const halSpiFlash_t *prop, uint32_t offset, uint32_t size);
  345. /**
  346. * \brief handling after program/erase is completed
  347. *
  348. * After program/erase is completed, status register will be set to
  349. * protect all.
  350. *
  351. * When volatile status register feature is not supported, it won't change
  352. * status register.
  353. *
  354. * \param d hal spi flash instance
  355. */
  356. void halSpiFlashFinishEraseProgram(const halSpiFlash_t *d);
  357. /**
  358. * \brief check status register
  359. *
  360. * Check status register:
  361. * - When there are suspend bits, reset
  362. * - QE should be 1
  363. * - reasonable WP setting
  364. *
  365. * \param d hal spi flash instance
  366. */
  367. void halSpiFlashStatusCheck(const halSpiFlash_t *d);
  368. /**
  369. * \brief initialize flash
  370. *
  371. * \p d->hwp should be valid flash controller base address. Inside, flash ID
  372. * will be read, and properties inside \p d will be set based on flash ID.
  373. *
  374. * If flash ID is unknown, it will panic inside.
  375. *
  376. * Also \p halSpiFlashStatusCheck will be called inside to ensure status
  377. * register is reasonable.
  378. *
  379. * It should be called before configure quad read in flash controller.
  380. *
  381. * \param d hal spi flash instance
  382. */
  383. void halSpiFlashInit(halSpiFlash_t *d);
  384. #ifdef __cplusplus
  385. }
  386. #endif
  387. #endif