drv_spi_flash.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 _DRV_SPI_FLASH_H_
  13. #define _DRV_SPI_FLASH_H_
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <stddef.h>
  17. #include "drv_names.h"
  18. #include "drv_config.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /**
  23. * \brief opaque data structure for SPI flash
  24. */
  25. typedef struct drvSpiFlash drvSpiFlash_t;
  26. /**
  27. * \brief open SPI flash
  28. *
  29. * SPI flash instance is singleton for each flash. So, for each device name
  30. * the returned instance pointer is the same. And there are no \p close
  31. * API.
  32. *
  33. * At the first open, the flash will be initialized (for faster speed).
  34. *
  35. * When \p name is DRV_NAME_SPI_FLASH_EXT, even CONFIG_BOARD_WITH_EXT_FLASH
  36. * isn't defined, this will initialize external flash.
  37. *
  38. * \param name SPI flash device name
  39. * \return
  40. * - SPI flash instance pointer
  41. * - NULL if the name is invalid
  42. */
  43. drvSpiFlash_t *drvSpiFlashOpen(uint32_t name);
  44. /**
  45. * \brief check SPI flash SR
  46. *
  47. * wait SPI flash wip finish in 30s(hardcode)
  48. *
  49. * \param name SPI flash device name
  50. */
  51. void drvSpiFlashSRCheck(drvSpiFlash_t *d);
  52. /**
  53. * \brief set whether XIP is enabled
  54. *
  55. * When XIP is enabled, flash operation protect the conflict of code
  56. * execution on flash.
  57. *
  58. * The typical case is external flash for file system only. In this,
  59. * set XIP to false can make flash erase/write faster.
  60. *
  61. * By default XIP protection is enabled. The flash on primary flash
  62. * controller can't be changed.
  63. *
  64. * \param d SPI flash instance pointer, must be valid
  65. * \param enable XIP protection enable
  66. * \return
  67. * - true on success
  68. * - false on invalid prameters
  69. */
  70. bool drvSpiFlashSetXipEnabled(drvSpiFlash_t *d, bool enable);
  71. /**
  72. * \brief set whether to enable suspend
  73. *
  74. * By default, flash driver will suspend erase or program when there are
  75. * pending interrupt, and if the flash supports suspend. In case, it is
  76. * known that erase and program will be called in critical section, it is
  77. * needed to call this to disable suspend temporally. Otherwise, erase or
  78. * program may consume very long time, or never finish.
  79. *
  80. * In normal application, it should be avoided to call flash driver inside
  81. * interrupt service or critical section. So, most likely it is not needed
  82. * to call this. The main purpose is for debugging.
  83. *
  84. * \param d SPI flash instance pointer, must be valid
  85. * \param enable suspend enabled
  86. * \return
  87. * - true on success
  88. * - false on invalid prameters
  89. */
  90. bool drvSpiFlashSetSuspendEnabled(drvSpiFlash_t *d, bool enable);
  91. /**
  92. * \brief prohibit flash erase/program for a certain range
  93. *
  94. * By default, flash erase/program is not prohibited. If it is known that
  95. * certain range will never be changed, this can be called. This range will
  96. * be used to check erase and program parameters. When the erase or program
  97. * address is located in the range, erase or program API will return false.
  98. * For example, it the first block (usually it is bootloader) will never be
  99. * modified:
  100. *
  101. * \code{.cpp}
  102. * drvSpiFlashSetRangeWriteProhibit(flash, 0, 0x10000);
  103. * \endcode
  104. *
  105. * For multiple non-coninuous ranges, this can be called multple times.
  106. *
  107. * Inside, there is a flag for each block (64KB). So, the check unit is 64KB
  108. * block. Also, \p start will be aligned up, \p end will be aligned down.
  109. * So, the followings won't set any prohibit.
  110. *
  111. * \code{.cpp}
  112. * drvSpiFlashSetRangeWriteProhibit(flash, 0x10000, 0x18000);
  113. * drvSpiFlashSetRangeWriteProhibit(flash, 0x18000, 0x20000);
  114. * \endcode
  115. *
  116. * \param d SPI flash instance pointer, must be valid
  117. * \param start range start (inclusive)
  118. * \param end range end (exclusive)
  119. */
  120. void drvSpiFlashSetRangeWriteProhibit(drvSpiFlash_t *d, uint32_t start, uint32_t end);
  121. /**
  122. * \brief clear range write prohibit flag
  123. *
  124. * This is not recommended, just for completeness.
  125. *
  126. * \param d SPI flash instance pointer, must be valid
  127. * \param start range start (inclusive)
  128. * \param end range end (exclusive)
  129. */
  130. void drvSpiFlashClearRangeWriteProhibit(drvSpiFlash_t *d, uint32_t start, uint32_t end);
  131. /**
  132. * \brief get flash properties
  133. *
  134. * \p halSpiFlash_t is defined in \p hal_spi_flash.h
  135. *
  136. * The returned property pointer is the property used by the driver.
  137. * It is designed not to be const. If you really know that the default
  138. * property from manufacture ID doesn't match you flash, you can
  139. * change it.
  140. *
  141. * \param d SPI flash instance pointer, must be valid
  142. * \return
  143. * - flash properties
  144. */
  145. struct halSpiFlash *drvSpiFlashGetProp(drvSpiFlash_t *d);
  146. /**
  147. * \brief get flash manufactor id
  148. *
  149. * The contents of the id:
  150. * - id[23:16]: capacity
  151. * - id[15:8]: memory type
  152. * - id[7:0]: manufacturer
  153. *
  154. * \param d SPI flash instance pointer, must be valid
  155. * \return
  156. * - flash ID
  157. */
  158. uint32_t drvSpiFlashGetID(drvSpiFlash_t *d);
  159. /**
  160. * \brief get flash capacity in byte
  161. *
  162. * \param d SPI flash instance pointer, must be valid
  163. * \return
  164. * - flash capacity in byte
  165. */
  166. uint32_t drvSpiFlashCapacity(drvSpiFlash_t *d);
  167. /**
  168. * \brief map flash offset to accessible address
  169. *
  170. * The returned pointer should be accessed as read only.
  171. *
  172. * This is incompatible with flash not on flash controller. So,
  173. * \p drvSpiFlashRead and \p drvSpiFlashReadCheck is recommended.
  174. *
  175. * \param d SPI flash instance pointer, must be valid
  176. * \param offset flash offset
  177. * \return
  178. * - accessible pointer
  179. * - NULL on error, invalid parameters
  180. */
  181. const void *drvSpiFlashMapAddress(drvSpiFlash_t *d, uint32_t offset);
  182. /**
  183. * \brief read data from flash
  184. *
  185. * \param d SPI flash instance pointer, must be valid
  186. * \param offset flash offset
  187. * \param data memory for read
  188. * \param size read size
  189. * \return
  190. * - true on success
  191. * - false on error, invalid parameters
  192. */
  193. bool drvSpiFlashRead(drvSpiFlash_t *d, uint32_t offset, void *data, uint32_t size);
  194. /**
  195. * \brief read data from flash, and check with provided data
  196. *
  197. * It is close to \p drvSpiFlashRead with \p memcmp. And it can return false
  198. * earlier at mismatch. Also, it can save another extra buffer.
  199. *
  200. * \param d SPI flash instance pointer, must be valid
  201. * \param offset flash offset
  202. * \param data memory for read
  203. * \param size read size
  204. * \return
  205. * - true on success
  206. * - false on error, invalid parameters
  207. */
  208. bool drvSpiFlashReadCheck(drvSpiFlash_t *d, uint32_t offset, const void *data, uint32_t size);
  209. /**
  210. * \brief write to flash
  211. *
  212. * This will just use flash PROGRAM command to write. Caller should ensure
  213. * the write sector or block is erased before. Otherwise, the data on
  214. * flash may be different from the input data.
  215. *
  216. * \p offset and \p size are not needed to be sector or block aligned.
  217. *
  218. * It will wait flash write finish. On success return, all data are written
  219. * to flash.
  220. *
  221. * It won't read back to verify the result.
  222. *
  223. * Caller should take care about the location of \p data. When \p data is on
  224. * flash of the driver, it may cause hang up.
  225. *
  226. * \param d SPI flash instance pointer, must be valid
  227. * \param offset flash offset
  228. * \param data memory for write
  229. * \param size write size
  230. * \return
  231. * - true on success
  232. * - false on invalid parameters, or soft write protected
  233. */
  234. bool drvSpiFlashWrite(drvSpiFlash_t *d, uint32_t offset, const void *data, size_t size);
  235. /**
  236. * \brief erase flash sectors or blocks
  237. *
  238. * Both \p offset and \p size must be sector (4KB) aligned.
  239. *
  240. * When \p size is larger than sector or block, it will loop inside until
  241. * all requested sector or block are erased.
  242. *
  243. * It will wait flash erase finish. On success return, the whole region is
  244. * erased.
  245. *
  246. * It won't read back to verify the result.
  247. *
  248. * \param d SPI flash instance pointer, must be valid
  249. * \param offset flash offset
  250. * \param size erase size
  251. * \return
  252. * - true on success
  253. * - false on invalid parameters, or soft write protected
  254. */
  255. bool drvSpiFlashErase(drvSpiFlash_t *d, uint32_t offset, size_t size);
  256. /**
  257. * \brief erase the whole flash chip
  258. *
  259. * In normal case, it shouldn't be used by application.
  260. *
  261. * \param d SPI flash instance pointer, must be valid
  262. * \return
  263. * - true on success
  264. * - false on invalid parameters, or soft write protected
  265. */
  266. bool drvSpiFlashChipErase(drvSpiFlash_t *d);
  267. /**
  268. * \brief read flash status register
  269. *
  270. * When flash support (at least) two status registers, this will return two
  271. * status registers. Otherwise, it will return only one status register.
  272. *
  273. * \param d SPI flash instance pointer, must be valid
  274. * \return
  275. * - flash status register
  276. */
  277. uint16_t drvSpiFlashReadSR(drvSpiFlash_t *d);
  278. /**
  279. * \brief write flash status register
  280. *
  281. * When flash support (at least) two status registers, this will write two
  282. * status registers. Otherwise, it will write only one status register.
  283. *
  284. * \param d SPI flash instance pointer, must be valid
  285. * \param sr value to be written to flash status register
  286. */
  287. void drvSpiFlashWriteSR(drvSpiFlash_t *d, uint16_t sr);
  288. /**
  289. * \brief enter deep power down mode
  290. *
  291. * When XIP is enabled, it must be called with interrupt disabled.
  292. *
  293. * After enter deep power down mode, \p drvSpiFlashReleaseDeepPowerDown must
  294. * be called before access.
  295. *
  296. * \param d SPI flash instance pointer, must be valid
  297. */
  298. void drvSpiFlashDeepPowerDown(drvSpiFlash_t *d);
  299. /**
  300. * \brief release from deep power down mode
  301. *
  302. * When XIP is enabled, it must be called with interrupt disabled.
  303. *
  304. * \param d SPI flash instance pointer, must be valid
  305. */
  306. void drvSpiFlashReleaseDeepPowerDown(drvSpiFlash_t *d);
  307. /**
  308. * \brief read unique id number
  309. *
  310. * The currently known largest uid length is 16 bytes.
  311. *
  312. * \param d SPI flash instance pointer, must be valid
  313. * \param id unique id, enough to hold uid
  314. * \return
  315. * - uid length
  316. * - -1 on error, invalid parameters or not support
  317. */
  318. int drvSpiFlashReadUniqueId(drvSpiFlash_t *d, uint8_t *id);
  319. /**
  320. * \brief read cp id
  321. *
  322. * \param d SPI flash instance pointer, must be valid
  323. * \return
  324. * - cp id
  325. * - 0 if not supported
  326. */
  327. uint16_t drvSpiFlashReadCpId(drvSpiFlash_t *d);
  328. /**
  329. * \brief read security register
  330. *
  331. * \param d SPI flash instance pointer, must be valid
  332. * \param num security register number
  333. * \param address address inside security register
  334. * \param data pointer to output data
  335. * \param size read size
  336. * \return
  337. * - true on success
  338. * - false on invalid parameter, or not support
  339. */
  340. bool drvSpiFlashReadSecurityRegister(drvSpiFlash_t *d, uint8_t num, uint16_t address, void *data, uint32_t size);
  341. /**
  342. * \brief program security register
  343. *
  344. * \param d SPI flash instance pointer, must be valid
  345. * \param num security register number
  346. * \param address address inside security register
  347. * \param data data to be programmed
  348. * \param size program size
  349. * \return
  350. * - true on success
  351. * - false on invalid parameter, or not support
  352. */
  353. bool drvSpiFlashProgramSecurityRegister(drvSpiFlash_t *d, uint8_t num, uint16_t address, const void *data, uint32_t size);
  354. /**
  355. * \brief program security register
  356. *
  357. * \param d SPI flash instance pointer, must be valid
  358. * \param num security register number
  359. * \return
  360. * - true on success
  361. * - false on invalid parameter, or not support
  362. */
  363. bool drvSpiFlashEraseSecurityRegister(drvSpiFlash_t *d, uint8_t num);
  364. /**
  365. * \brief lock security register
  366. *
  367. * Lock are OTP bits in status register. Once it is locked, there are
  368. * no ways to erase/program the security register.
  369. *
  370. * \param d SPI flash instance pointer, must be valid
  371. * \param num security register number
  372. * \return
  373. * - true on success
  374. * - false on invalid parameter, or not support
  375. */
  376. bool drvSpiFlashLockSecurityRegister(drvSpiFlash_t *d, uint8_t num);
  377. /**
  378. * \brief whether security register is locked
  379. *
  380. * \param d SPI flash instance pointer, must be valid
  381. * \param num security register number
  382. * \return
  383. * - true if \p num is valid and locked
  384. * - false if not locked, or invalid parameter
  385. */
  386. bool drvSpiFlashIsSecurityRegisterLocked(drvSpiFlash_t *d, uint8_t num);
  387. /**
  388. * \brief read SFDP
  389. *
  390. * \param d SPI flash instance pointer, must be valid
  391. * \param address SFDP address
  392. * \param data data to be read
  393. * \param size read size
  394. * \return
  395. * - true on success
  396. * - false on fail, invalid parameters or not support
  397. */
  398. bool drvSpiFlashReadSFDP(drvSpiFlash_t *d, unsigned address, void *data, unsigned size);
  399. /**
  400. * \brief unique id length
  401. *
  402. * \param d SPI flash instance pointer, must be valid
  403. * \return unique id length, 0 for not support
  404. */
  405. int drvSpiFlashUidLength(drvSpiFlash_t *d);
  406. #if defined(CONFIG_SOC_8910) && defined(CONFIG_SUPPORT_LC_FLASH)
  407. /**
  408. * \brief enter 4-byte address mode(EN4B)
  409. *
  410. * \param d SPI flash instance pointer, must be valid
  411. * \param offset flash offset to address
  412. */
  413. void drvSpiFlash4ByteAddressMode(drvSpiFlash_t *d, uint32_t offset);
  414. /**
  415. * \brief exit 4-byte address mode(EX4B)
  416. *
  417. * \param d SPI flash instance pointer, must be valid
  418. * \param offset flash offset to address
  419. */
  420. void drvSpiFlashDisenable4ByteAddressMode(drvSpiFlash_t *d, uint32_t offset);
  421. #endif
  422. #ifdef __cplusplus
  423. }
  424. #endif
  425. #endif