fsl_spifi.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef _FSL_SPIFI_H_
  31. #define _FSL_SPIFI_H_
  32. #include "fsl_common.h"
  33. /*!
  34. * @addtogroup spifi
  35. * @{
  36. */
  37. /*******************************************************************************
  38. * Definitions
  39. ******************************************************************************/
  40. /*! @name Driver version */
  41. /*@{*/
  42. /*! @brief SPIFI driver version 2.0.0. */
  43. #define FSL_SPIFI_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
  44. /*@}*/
  45. /*! @brief Status structure of SPIFI.*/
  46. enum _status_t
  47. {
  48. kStatus_SPIFI_Idle = MAKE_STATUS(kStatusGroup_SPIFI, 0), /*!< SPIFI is in idle state */
  49. kStatus_SPIFI_Busy = MAKE_STATUS(kStatusGroup_SPIFI, 1), /*!< SPIFI is busy */
  50. kStatus_SPIFI_Error = MAKE_STATUS(kStatusGroup_SPIFI, 2), /*!< Error occurred during SPIFI transfer */
  51. };
  52. /*! @brief SPIFI interrupt source */
  53. typedef enum _spifi_interrupt_enable
  54. {
  55. kSPIFI_CommandFinishInterruptEnable = SPIFI_CTRL_INTEN_MASK, /*!< Interrupt while command finished */
  56. } spifi_interrupt_enable_t;
  57. /*! @brief SPIFI SPI mode select */
  58. typedef enum _spifi_spi_mode
  59. {
  60. kSPIFI_SPISckLow = 0x0U, /*!< SCK low after last bit of command, keeps low while CS high */
  61. kSPIFI_SPISckHigh = 0x1U /*!< SCK high after last bit of command and while CS high */
  62. } spifi_spi_mode_t;
  63. /*! @brief SPIFI dual mode select */
  64. typedef enum _spifi_dual_mode
  65. {
  66. kSPIFI_QuadMode = 0x0U, /*!< SPIFI uses IO3:0 */
  67. kSPIFI_DualMode = 0x1U /*!< SPIFI uses IO1:0 */
  68. } spifi_dual_mode_t;
  69. /*! @brief SPIFI data direction */
  70. typedef enum _spifi_data_direction
  71. {
  72. kSPIFI_DataInput = 0x0U, /*!< Data input from serial flash. */
  73. kSPIFI_DataOutput = 0x1U /*!< Data output to serial flash. */
  74. } spifi_data_direction_t;
  75. /*! @brief SPIFI command opcode format */
  76. typedef enum _spifi_command_format
  77. {
  78. kSPIFI_CommandAllSerial = 0x0, /*!< All fields of command are serial. */
  79. kSPIFI_CommandDataQuad = 0x1U, /*!< Only data field is dual/quad, others are serial. */
  80. kSPIFI_CommandOpcodeSerial = 0x2U, /*!< Only opcode field is serial, others are quad/dual. */
  81. kSPIFI_CommandAllQuad = 0x3U /*!< All fields of command are dual/quad mode. */
  82. } spifi_command_format_t;
  83. /*! @brief SPIFI command type */
  84. typedef enum _spifi_command_type
  85. {
  86. kSPIFI_CommandOpcodeOnly = 0x1U, /*!< Command only have opcode, no address field */
  87. kSPIFI_CommandOpcodeAddrOneByte = 0x2U, /*!< Command have opcode and also one byte address field */
  88. kSPIFI_CommandOpcodeAddrTwoBytes = 0x3U, /*!< Command have opcode and also two bytes address field */
  89. kSPIFI_CommandOpcodeAddrThreeBytes = 0x4U, /*!< Command have opcode and also three bytes address field. */
  90. kSPIFI_CommandOpcodeAddrFourBytes = 0x5U, /*!< Command have opcode and also four bytes address field */
  91. kSPIFI_CommandNoOpcodeAddrThreeBytes = 0x6U, /*!< Command have no opcode and three bytes address field */
  92. kSPIFI_CommandNoOpcodeAddrFourBytes = 0x7U /*!< Command have no opcode and four bytes address field */
  93. } spifi_command_type_t;
  94. /*! @brief SPIFI status flags */
  95. enum _spifi_status_flags
  96. {
  97. kSPIFI_MemoryCommandWriteFinished = SPIFI_STAT_MCINIT_MASK, /*!< Memory command write finished */
  98. kSPIFI_CommandWriteFinished = SPIFI_STAT_CMD_MASK, /*!< Command write finished */
  99. kSPIFI_InterruptRequest = SPIFI_STAT_INTRQ_MASK /*!< CMD flag from 1 to 0, means command execute finished */
  100. };
  101. /*! @brief SPIFI command structure */
  102. typedef struct _spifi_command
  103. {
  104. uint16_t dataLen; /*!< How many data bytes are needed in this command. */
  105. bool isPollMode; /*!< For command need to read data from serial flash */
  106. spifi_data_direction_t direction; /*!< Data direction of this command. */
  107. uint8_t intermediateBytes; /*!< How many intermediate bytes needed */
  108. spifi_command_format_t format; /*!< Command format */
  109. spifi_command_type_t type; /*!< Command type */
  110. uint8_t opcode; /*!< Command opcode value */
  111. } spifi_command_t;
  112. /*!
  113. * @brief SPIFI region configuration structure.
  114. */
  115. typedef struct _spifi_config
  116. {
  117. uint16_t timeout; /*!< SPI transfer timeout, the unit is SCK cycles */
  118. uint8_t csHighTime; /*!< CS high time cycles */
  119. bool disablePrefetch; /*!< True means SPIFI will not attempt a speculative prefetch. */
  120. bool disableCachePrefech; /*!< Disable prefetch of cache line */
  121. bool isFeedbackClock; /*!< Is data sample uses feedback clock. */
  122. spifi_spi_mode_t spiMode; /*!< SPIFI spi mode select */
  123. bool isReadFullClockCycle; /*!< If enable read full clock cycle. */
  124. spifi_dual_mode_t dualMode; /*!< SPIFI dual mode, dual or quad. */
  125. } spifi_config_t;
  126. /*! @brief Transfer structure for SPIFI */
  127. typedef struct _spifi_transfer
  128. {
  129. uint8_t *data; /*!< Pointer to data to transmit */
  130. size_t dataSize; /*!< Bytes to be transmit */
  131. } spifi_transfer_t;
  132. /*******************************************************************************
  133. * API
  134. ******************************************************************************/
  135. #if defined(__cplusplus)
  136. extern "C" {
  137. #endif /* _cplusplus */
  138. /*!
  139. * @name Initialization and deinitialization
  140. * @{
  141. */
  142. /*!
  143. * @brief Initializes the SPIFI with the user configuration structure.
  144. *
  145. * This function configures the SPIFI module with the user-defined configuration.
  146. *
  147. * @param base SPIFI peripheral base address.
  148. * @param config The pointer to the configuration structure.
  149. */
  150. void SPIFI_Init(SPIFI_Type *base, const spifi_config_t *config);
  151. /*!
  152. * @brief Get SPIFI default configure settings.
  153. *
  154. * @param config SPIFI config structure pointer.
  155. */
  156. void SPIFI_GetDefaultConfig(spifi_config_t *config);
  157. /*!
  158. * @brief Deinitializes the SPIFI regions.
  159. *
  160. * @param base SPIFI peripheral base address.
  161. */
  162. void SPIFI_Deinit(SPIFI_Type *base);
  163. /* @}*/
  164. /*!
  165. * @name Basic Control Operations
  166. * @{
  167. */
  168. /*!
  169. * @brief Set SPIFI flash command.
  170. *
  171. * @param base SPIFI peripheral base address.
  172. * @param cmd SPIFI command structure pointer.
  173. */
  174. void SPIFI_SetCommand(SPIFI_Type *base, spifi_command_t *cmd);
  175. /*!
  176. * @brief Set SPIFI command address.
  177. *
  178. * @param base SPIFI peripheral base address.
  179. * @param addr Address value for the command.
  180. */
  181. static inline void SPIFI_SetCommandAddress(SPIFI_Type *base, uint32_t addr)
  182. {
  183. base->ADDR = addr;
  184. }
  185. /*!
  186. * @brief Set SPIFI intermediate data.
  187. *
  188. * Before writing a command wihch needs specific intermediate value, users shall call this function to write it.
  189. * The main use of this function for current serial flash is to select no-opcode mode and cancelling this mode. As
  190. * dummy cycle do not care about the value, no need to call this function.
  191. *
  192. * @param base SPIFI peripheral base address.
  193. * @param val Intermediate data.
  194. */
  195. static inline void SPIFI_SetIntermediateData(SPIFI_Type *base, uint32_t val)
  196. {
  197. base->IDATA = val;
  198. }
  199. /*!
  200. * @brief Set SPIFI Cache limit value.
  201. *
  202. * SPIFI includes caching of prevously-accessed data to improve performance. Software can write an address to this
  203. * function, to prevent such caching at and above the address.
  204. *
  205. * @param base SPIFI peripheral base address.
  206. * @param val Zero-based upper limit of cacheable memory.
  207. */
  208. static inline void SPIFI_SetCacheLimit(SPIFI_Type *base, uint32_t val)
  209. {
  210. base->CLIMIT = val;
  211. }
  212. /*!
  213. * @brief Reset the command field of SPIFI.
  214. *
  215. * This function is used to abort the current command or memory mode.
  216. *
  217. * @param base SPIFI peripheral base address.
  218. */
  219. static inline void SPIFI_ResetCommand(SPIFI_Type *base)
  220. {
  221. base->STAT = SPIFI_STAT_RESET_MASK;
  222. /* Wait for the RESET flag cleared by HW */
  223. while (base->STAT & SPIFI_STAT_RESET_MASK)
  224. {
  225. }
  226. }
  227. /*!
  228. * @brief Set SPIFI flash AHB read command.
  229. *
  230. * Call this function means SPIFI enters to memory mode, while users need to use command, a SPIFI_ResetCommand shall
  231. * be called.
  232. *
  233. * @param base SPIFI peripheral base address.
  234. * @param cmd SPIFI command structure pointer.
  235. */
  236. void SPIFI_SetMemoryCommand(SPIFI_Type *base, spifi_command_t *cmd);
  237. /*!
  238. * @brief Enable SPIFI interrupt.
  239. *
  240. * The interrupt is triggered only in command mode, and it means the command now is finished.
  241. *
  242. * @param base SPIFI peripheral base address.
  243. * @param mask SPIFI interrupt enable mask. It is a logic OR of members the
  244. * enumeration :: spifi_interrupt_enable_t
  245. */
  246. static inline void SPIFI_EnableInterrupt(SPIFI_Type *base, uint32_t mask)
  247. {
  248. base->CTRL |= mask;
  249. }
  250. /*!
  251. * @brief Disable SPIFI interrupt.
  252. *
  253. * The interrupt is triggered only in command mode, and it means the command now is finished.
  254. *
  255. * @param base SPIFI peripheral base address.
  256. * @param mask SPIFI interrupt enable mask. It is a logic OR of members the
  257. * enumeration :: spifi_interrupt_enable_t
  258. */
  259. static inline void SPIFI_DisableInterrupt(SPIFI_Type *base, uint32_t mask)
  260. {
  261. base->CTRL &= ~mask;
  262. }
  263. /*!
  264. * @name Status
  265. * @{
  266. */
  267. /*!
  268. * @brief Get the status of all interrupt flags for SPIFI.
  269. *
  270. * @param base SPIFI peripheral base address.
  271. * @return SPIFI flag status
  272. */
  273. static inline uint32_t SPIFI_GetStatusFlag(SPIFI_Type *base)
  274. {
  275. return base->STAT;
  276. }
  277. /* @}*/
  278. /*!
  279. * @brief Enable or disable DMA request for SPIFI.
  280. *
  281. * @param base SPIFI peripheral base address.
  282. * @param enable True means enable DMA and false means disable DMA.
  283. */
  284. static inline void SPIFI_EnableDMA(SPIFI_Type *base, bool enable)
  285. {
  286. if (enable)
  287. {
  288. base->CTRL |= SPIFI_CTRL_DMAEN_MASK;
  289. }
  290. else
  291. {
  292. base->CTRL &= ~SPIFI_CTRL_DMAEN_MASK;
  293. }
  294. }
  295. /*!
  296. * @brief Gets the SPIFI data register address.
  297. *
  298. * This API is used to provide a transfer address for the SPIFI DMA transfer configuration.
  299. *
  300. * @param base SPIFI base pointer
  301. * @return data register address
  302. */
  303. static inline uint32_t SPIFI_GetDataRegisterAddress(SPIFI_Type *base)
  304. {
  305. return (uint32_t)(&(base->DATA));
  306. }
  307. /*!
  308. * @brief Write a word data in address of SPIFI.
  309. *
  310. * Users can write a page or at least a word data into SPIFI address.
  311. *
  312. * @param base SPIFI peripheral base address.
  313. * @param data Data need be write.
  314. */
  315. static inline void SPIFI_WriteData(SPIFI_Type *base, uint32_t data)
  316. {
  317. base->DATA = data;
  318. }
  319. /*!
  320. * @brief Read data from serial flash.
  321. *
  322. * Users should notice before call this function, the data length field in command register shall larger
  323. * than 4, otherwise a hardfault will happen.
  324. *
  325. * @param base SPIFI peripheral base address.
  326. * @return Data input from flash.
  327. */
  328. static inline uint32_t SPIFI_ReadData(SPIFI_Type *base)
  329. {
  330. return base->DATA;
  331. }
  332. /* @} */
  333. #if defined(__cplusplus)
  334. }
  335. #endif
  336. /*! @}*/
  337. #endif /* _FSL_SPIFI_H_ */