fsl_flexspi.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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_FLEXSPI_H_
  31. #define __FSL_FLEXSPI_H_
  32. #include <stddef.h>
  33. #include "fsl_device_registers.h"
  34. #include "fsl_common.h"
  35. /*!
  36. * @addtogroup flexspi
  37. * @{
  38. */
  39. /*******************************************************************************
  40. * Definitions
  41. ******************************************************************************/
  42. /*! @name Driver version */
  43. /*@{*/
  44. /*! @brief FLEXSPI driver version 2.0.1. */
  45. #define FSL_FLEXSPI_DRIVER_VERSION (MAKE_VERSION(2, 0, 1))
  46. /*@}*/
  47. #define FSL_FEATURE_FLEXSPI_AHB_BUFFER_COUNT FSL_FEATURE_FLEXSPI_AHB_BUFFER_COUNTn(0)
  48. /*! @breif Formula to form FLEXSPI instructions in LUT table. */
  49. #define FLEXSPI_LUT_SEQ(cmd0, pad0, op0, cmd1, pad1, op1) \
  50. (FLEXSPI_LUT_OPERAND0(op0) | FLEXSPI_LUT_NUM_PADS0(pad0) | FLEXSPI_LUT_OPCODE0(cmd0) | FLEXSPI_LUT_OPERAND1(op1) | \
  51. FLEXSPI_LUT_NUM_PADS1(pad1) | FLEXSPI_LUT_OPCODE1(cmd1))
  52. /*! @brief Status structure of FLEXSPI.*/
  53. enum _flexspi_status
  54. {
  55. kStatus_FLEXSPI_Busy = MAKE_STATUS(kStatusGroup_FLEXSPI, 0), /*!< FLEXSPI is busy */
  56. kStatus_FLEXSPI_SequenceExecutionTimeout = MAKE_STATUS(kStatusGroup_FLEXSPI, 1), /*!< Sequence execution timeout
  57. error occurred during FLEXSPI transfer. */
  58. kStatus_FLEXSPI_IpCommandSequenceError = MAKE_STATUS(kStatusGroup_FLEXSPI, 2), /*!< IP command Sequence execution
  59. timeout error occurred during FLEXSPI transfer. */
  60. kStatus_FLEXSPI_IpCommandGrantTimeout = MAKE_STATUS(kStatusGroup_FLEXSPI, 3), /*!< IP command grant timeout error
  61. occurred during FLEXSPI transfer. */
  62. };
  63. /*! @brief CMD definition of FLEXSPI, use to form LUT instruction. */
  64. enum _flexspi_command
  65. {
  66. kFLEXSPI_Command_STOP = 0x00U, /*!< Stop execution, deassert CS. */
  67. kFLEXSPI_Command_SDR = 0x01U, /*!< Transmit Command code to Flash, using SDR mode. */
  68. kFLEXSPI_Command_RADDR_SDR = 0x02U, /*!< Transmit Row Address to Flash, using SDR mode. */
  69. kFLEXSPI_Command_CADDR_SDR = 0x03U, /*!< Transmit Column Address to Flash, using SDR mode. */
  70. kFLEXSPI_Command_MODE1_SDR = 0x04U, /*!< Transmit 1-bit Mode bits to Flash, using SDR mode. */
  71. kFLEXSPI_Command_MODE2_SDR = 0x05U, /*!< Transmit 2-bit Mode bits to Flash, using SDR mode. */
  72. kFLEXSPI_Command_MODE4_SDR = 0x06U, /*!< Transmit 4-bit Mode bits to Flash, using SDR mode. */
  73. kFLEXSPI_Command_MODE8_SDR = 0x07U, /*!< Transmit 8-bit Mode bits to Flash, using SDR mode. */
  74. kFLEXSPI_Command_WRITE_SDR = 0x08U, /*!< Transmit Programming Data to Flash, using SDR mode. */
  75. kFLEXSPI_Command_READ_SDR = 0x09U, /*!< Receive Read Data from Flash, using SDR mode. */
  76. kFLEXSPI_Command_LEARN_SDR = 0x0AU, /*!< Receive Read Data or Preamble bit from Flash, SDR mode. */
  77. kFLEXSPI_Command_DATSZ_SDR = 0x0BU, /*!< Transmit Read/Program Data size (byte) to Flash, SDR mode. */
  78. kFLEXSPI_Command_DUMMY_SDR = 0x0CU, /*!< Leave data lines undriven by FlexSPI controller.*/
  79. kFLEXSPI_Command_DUMMY_RWDS_SDR = 0x0DU, /*!< Leave data lines undriven by FlexSPI controller,
  80. dummy cycles decided by RWDS. */
  81. kFLEXSPI_Command_DDR = 0x21U, /*!< Transmit Command code to Flash, using DDR mode. */
  82. kFLEXSPI_Command_RADDR_DDR = 0x22U, /*!< Transmit Row Address to Flash, using DDR mode. */
  83. kFLEXSPI_Command_CADDR_DDR = 0x23U, /*!< Transmit Column Address to Flash, using DDR mode. */
  84. kFLEXSPI_Command_MODE1_DDR = 0x24U, /*!< Transmit 1-bit Mode bits to Flash, using DDR mode. */
  85. kFLEXSPI_Command_MODE2_DDR = 0x25U, /*!< Transmit 2-bit Mode bits to Flash, using DDR mode. */
  86. kFLEXSPI_Command_MODE4_DDR = 0x26U, /*!< Transmit 4-bit Mode bits to Flash, using DDR mode. */
  87. kFLEXSPI_Command_MODE8_DDR = 0x27U, /*!< Transmit 8-bit Mode bits to Flash, using DDR mode. */
  88. kFLEXSPI_Command_WRITE_DDR = 0x28U, /*!< Transmit Programming Data to Flash, using DDR mode. */
  89. kFLEXSPI_Command_READ_DDR = 0x29U, /*!< Receive Read Data from Flash, using DDR mode. */
  90. kFLEXSPI_Command_LEARN_DDR = 0x2AU, /*!< Receive Read Data or Preamble bit from Flash, DDR mode. */
  91. kFLEXSPI_Command_DATSZ_DDR = 0x2BU, /*!< Transmit Read/Program Data size (byte) to Flash, DDR mode. */
  92. kFLEXSPI_Command_DUMMY_DDR = 0x2CU, /*!< Leave data lines undriven by FlexSPI controller.*/
  93. kFLEXSPI_Command_DUMMY_RWDS_DDR = 0x2DU, /*!< Leave data lines undriven by FlexSPI controller,
  94. dummy cycles decided by RWDS. */
  95. kFLEXSPI_Command_JUMP_ON_CS = 0x1FU, /*!< Stop execution, deassert CS and save operand[7:0] as the
  96. instruction start pointer for next sequence */
  97. };
  98. /*! @brief pad definition of FLEXSPI, use to form LUT instruction. */
  99. enum _flexspi_pad
  100. {
  101. kFLEXSPI_1PAD = 0x00U, /*!< Transmit command/address and transmit/receive data only through DATA0/DATA1. */
  102. kFLEXSPI_2PAD = 0x01U, /*!< Transmit command/address and transmit/receive data only through DATA[1:0]. */
  103. kFLEXSPI_4PAD = 0x02U, /*!< Transmit command/address and transmit/receive data only through DATA[3:0]. */
  104. kFLEXSPI_8PAD = 0x03U, /*!< Transmit command/address and transmit/receive data only through DATA[7:0]. */
  105. };
  106. /*! @brief FLEXSPI interrupt status flags.*/
  107. typedef enum _flexspi_flags
  108. {
  109. kFLEXSPI_SequenceExecutionTimeoutFlag = FLEXSPI_INTEN_SEQTIMEOUTEN_MASK, /*!< Sequence execution timeout. */
  110. kFLEXSPI_AhbBusTimeoutFlag = FLEXSPI_INTEN_AHBBUSTIMEOUTEN_MASK, /*!< AHB Bus timeout. */
  111. kFLEXSPI_SckStoppedBecauseTxEmptyFlag =
  112. FLEXSPI_INTEN_SCKSTOPBYWREN_MASK, /*!< SCK is stopped during command
  113. sequence because Async TX FIFO empty. */
  114. kFLEXSPI_SckStoppedBecauseRxFullFlag =
  115. FLEXSPI_INTEN_SCKSTOPBYRDEN_MASK, /*!< SCK is stopped during command
  116. sequence because Async RX FIFO full. */
  117. #if !((defined(FSL_FEATURE_FLEXSPI_HAS_NO_DATA_LEARN)) && (FSL_FEATURE_FLEXSPI_HAS_NO_DATA_LEARN))
  118. kFLEXSPI_DataLearningFailedFlag = FLEXSPI_INTEN_DATALEARNFAILEN_MASK, /*!< Data learning failed. */
  119. #endif
  120. kFLEXSPI_IpTxFifoWatermarkEmpltyFlag = FLEXSPI_INTEN_IPTXWEEN_MASK, /*!< IP TX FIFO WaterMark empty. */
  121. kFLEXSPI_IpRxFifoWatermarkAvailableFlag = FLEXSPI_INTEN_IPRXWAEN_MASK, /*!< IP RX FIFO WaterMark available. */
  122. kFLEXSPI_AhbCommandSequenceErrorFlag =
  123. FLEXSPI_INTEN_AHBCMDERREN_MASK, /*!< AHB triggered Command Sequences Error. */
  124. kFLEXSPI_IpCommandSequenceErrorFlag = FLEXSPI_INTEN_IPCMDERREN_MASK, /*!< IP triggered Command Sequences Error. */
  125. kFLEXSPI_AhbCommandGrantTimeoutFlag =
  126. FLEXSPI_INTEN_AHBCMDGEEN_MASK, /*!< AHB triggered Command Sequences Grant Timeout. */
  127. kFLEXSPI_IpCommandGrantTimeoutFlag =
  128. FLEXSPI_INTEN_IPCMDGEEN_MASK, /*!< IP triggered Command Sequences Grant Timeout. */
  129. kFLEXSPI_IpCommandExcutionDoneFlag =
  130. FLEXSPI_INTEN_IPCMDDONEEN_MASK, /*!< IP triggered Command Sequences Execution finished. */
  131. kFLEXSPI_AllInterruptFlags = 0xFFFU, /*!< All flags. */
  132. } flexspi_flags_t;
  133. /*! @brief FLEXSPI sample clock source selection for Flash Reading.*/
  134. typedef enum _flexspi_read_sample_clock
  135. {
  136. kFLEXSPI_ReadSampleClkLoopbackInternally = 0x0U, /*!< Dummy Read strobe generated by FlexSPI Controller
  137. and loopback internally. */
  138. kFLEXSPI_ReadSampleClkLoopbackFromDqsPad = 0x1U, /*!< Dummy Read strobe generated by FlexSPI Controller
  139. and loopback from DQS pad. */
  140. kFLEXSPI_ReadSampleClkLoopbackFromSckPad = 0x2U, /*!< SCK output clock and loopback from SCK pad. */
  141. kFLEXSPI_ReadSampleClkExternalInputFromDqsPad = 0x3U, /*!< Flash provided Read strobe and input from DQS pad. */
  142. } flexspi_read_sample_clock_t;
  143. /*! @brief FLEXSPI interval unit for flash device select.*/
  144. typedef enum _flexspi_cs_interval_cycle_unit
  145. {
  146. kFLEXSPI_CsIntervalUnit1SckCycle = 0x0U, /*!< Chip selection interval: CSINTERVAL * 1 serial clock cycle. */
  147. kFLEXSPI_CsIntervalUnit256SckCycle = 0x1U, /*!< Chip selection interval: CSINTERVAL * 256 serial clock cycle. */
  148. } flexspi_cs_interval_cycle_unit_t;
  149. /*! @brief FLEXSPI AHB wait interval unit for writting.*/
  150. typedef enum _flexspi_ahb_write_wait_unit
  151. {
  152. kFLEXSPI_AhbWriteWaitUnit2AhbCycle = 0x0U, /*!< AWRWAIT unit is 2 ahb clock cycle. */
  153. kFLEXSPI_AhbWriteWaitUnit8AhbCycle = 0x1U, /*!< AWRWAIT unit is 8 ahb clock cycle. */
  154. kFLEXSPI_AhbWriteWaitUnit32AhbCycle = 0x2U, /*!< AWRWAIT unit is 32 ahb clock cycle. */
  155. kFLEXSPI_AhbWriteWaitUnit128AhbCycle = 0x3U, /*!< AWRWAIT unit is 128 ahb clock cycle. */
  156. kFLEXSPI_AhbWriteWaitUnit512AhbCycle = 0x4U, /*!< AWRWAIT unit is 512 ahb clock cycle. */
  157. kFLEXSPI_AhbWriteWaitUnit2048AhbCycle = 0x5U, /*!< AWRWAIT unit is 2048 ahb clock cycle. */
  158. kFLEXSPI_AhbWriteWaitUnit8192AhbCycle = 0x6U, /*!< AWRWAIT unit is 8192 ahb clock cycle. */
  159. kFLEXSPI_AhbWriteWaitUnit32768AhbCycle = 0x7U, /*!< AWRWAIT unit is 32768 ahb clock cycle. */
  160. } flexspi_ahb_write_wait_unit_t;
  161. /*! @brief Error Code when IP command Error detected.*/
  162. typedef enum _flexspi_ip_error_code
  163. {
  164. kFLEXSPI_IpCmdErrorNoError = 0x0U, /*!< No error. */
  165. kFLEXSPI_IpCmdErrorJumpOnCsInIpCmd = 0x2U, /*!< IP command with JMP_ON_CS instruction used. */
  166. kFLEXSPI_IpCmdErrorUnknownOpCode = 0x3U, /*!< Unknown instruction opcode in the sequence. */
  167. kFLEXSPI_IpCmdErrorSdrDummyInDdrSequence = 0x4U, /*!< Instruction DUMMY_SDR/DUMMY_RWDS_SDR
  168. used in DDR sequence. */
  169. kFLEXSPI_IpCmdErrorDdrDummyInSdrSequence = 0x5U, /*!< Instruction DUMMY_DDR/DUMMY_RWDS_DDR
  170. used in SDR sequence. */
  171. kFLEXSPI_IpCmdErrorInvalidAddress = 0x6U, /*!< Flash access start address exceed the whole
  172. flash address range (A1/A2/B1/B2). */
  173. kFLEXSPI_IpCmdErrorSequenceExecutionTimeout = 0xEU, /*!< Sequence execution timeout. */
  174. kFLEXSPI_IpCmdErrorFlashBoundaryAcrosss = 0xFU, /*!< Flash boundary crossed. */
  175. } flexspi_ip_error_code_t;
  176. /*! @brief Error Code when AHB command Error detected.*/
  177. typedef enum _flexspi_ahb_error_code
  178. {
  179. kFLEXSPI_AhbCmdErrorNoError = 0x0U, /*!< No error. */
  180. kFLEXSPI_AhbCmdErrorJumpOnCsInWriteCmd = 0x2U, /*!< AHB Write command with JMP_ON_CS instruction
  181. used in the sequence. */
  182. kFLEXSPI_AhbCmdErrorUnknownOpCode = 0x3U, /*!< Unknown instruction opcode in the sequence. */
  183. kFLEXSPI_AhbCmdErrorSdrDummyInDdrSequence = 0x4U, /*!< Instruction DUMMY_SDR/DUMMY_RWDS_SDR used
  184. in DDR sequence. */
  185. kFLEXSPI_AhbCmdErrorDdrDummyInSdrSequence = 0x5U, /*!< Instruction DUMMY_DDR/DUMMY_RWDS_DDR
  186. used in SDR sequence. */
  187. kFLEXSPI_AhbCmdSequenceExecutionTimeout = 0x6U, /*!< Sequence execution timeout. */
  188. } flexspi_ahb_error_code_t;
  189. /*! @brief FLEXSPI operation port select.*/
  190. typedef enum _flexspi_port
  191. {
  192. kFLEXSPI_PortA1 = 0x0U, /*!< Access flash on A1 port. */
  193. kFLEXSPI_PortA2 = 0x1U, /*!< Access flash on A2 port. */
  194. kFLEXSPI_PortB1 = 0x2U, /*!< Access flash on B1 port. */
  195. kFLEXSPI_PortB2 = 0x3U, /*!< Access flash on B2 port. */
  196. } flexspi_port_t;
  197. /*! @brief Trigger source of current command sequence granted by arbitrator.*/
  198. typedef enum _flexspi_arb_command_source
  199. {
  200. kFLEXSPI_AhbReadCommand = 0x0U,
  201. kFLEXSPI_AhbWriteCommand = 0x1U,
  202. kFLEXSPI_IpCommand = 0x2U,
  203. kFLEXSPI_SuspendedCommand = 0x3U,
  204. } flexspi_arb_command_source_t;
  205. typedef enum _flexspi_command_type
  206. {
  207. kFLEXSPI_Command, /*!< FlexSPI operation: Only command, both TX and Rx buffer are ignored. */
  208. kFLEXSPI_Config, /*!< FlexSPI operation: Configure device mode, the TX fifo size is fixed in LUT. */
  209. kFLEXSPI_Read, /* /!< FlexSPI operation: Read, only Rx Buffer is effective. */
  210. kFLEXSPI_Write, /* /!< FlexSPI operation: Read, only Tx Buffer is effective. */
  211. } flexspi_command_type_t;
  212. typedef struct _flexspi_ahbBuffer_config
  213. {
  214. uint8_t priority;
  215. uint8_t masterIndex;
  216. uint16_t bufferSize;
  217. } flexspi_ahbBuffer_config_t;
  218. /*! @brief FLEXSPI configuration structure. */
  219. typedef struct _flexspi_config
  220. {
  221. flexspi_read_sample_clock_t rxSampleClock; /*!< Sample Clock source selection for Flash Reading. */
  222. bool enableSckFreeRunning; /*!< Enable/disable SCK output free-running. */
  223. bool enableCombination; /*!< Enable/disable combining PORT A and B Data Pins
  224. (SIOA[3:0] and SIOB[3:0]) to support Flash Octal mode. */
  225. bool enableDoze; /*!< Enable/disable doze mode support. */
  226. bool enableHalfSpeedAccess; /*!< Enable/disable divide by 2 of the clock for half
  227. speed commands. */
  228. bool enableSckBDiffOpt; /*!< Enable/disable SCKB pad use as SCKA differential clock
  229. output, when enable, Port B flash access is not available. */
  230. bool enableSameConfigForAll; /*!< Enable/disable same configuration for all connected devices
  231. when enabled, same configuration in FLASHA1CRx is applied to all. */
  232. uint16_t seqTimeoutCycle; /*!< Timeout wait cycle for command sequence execution,
  233. timeout after ahbGrantTimeoutCyle*1024 serial root clock cycles. */
  234. uint8_t ipGrantTimeoutCycle; /*!< Timeout wait cycle for IP command grant, timeout after
  235. ipGrantTimeoutCycle*1024 AHB clock cycles. */
  236. uint8_t txWatermark; /*!< FLEXSPI IP transmit watermark value. */
  237. uint8_t rxWatermark; /*!< FLEXSPI receive watermark value. */
  238. struct
  239. {
  240. bool enableAHBWriteIpTxFifo; /*!< Enable AHB bus write access to IP TX FIFO. */
  241. bool enableAHBWriteIpRxFifo; /*!< Enable AHB bus write access to IP RX FIFO. */
  242. uint8_t ahbGrantTimeoutCycle; /*!< Timeout wait cycle for AHB command grant,
  243. timeout after ahbGrantTimeoutCyle*1024 AHB clock cycles. */
  244. uint16_t ahbBusTimeoutCycle; /*!< Timeout wait cycle for AHB read/write access,
  245. timeout after ahbBusTimeoutCycle*1024 AHB clock cycles. */
  246. uint8_t resumeWaitCycle; /*!< Wait cycle for idle state before suspended command sequence
  247. resume, timeout after ahbBusTimeoutCycle AHB clock cycles. */
  248. flexspi_ahbBuffer_config_t buffer[FSL_FEATURE_FLEXSPI_AHB_BUFFER_COUNT]; /*!< AHB buffer size. */
  249. bool enableClearAHBBufferOpt; /*!< Enable/disable automatically clean AHB RX Buffer and TX Buffer
  250. when FLEXSPI returns STOP mode ACK. */
  251. bool enableAHBPrefetch; /*!< Enable/disable AHB read prefetch feature, when enabled, FLEXSPI
  252. will fetch more data than current AHB burst. */
  253. bool enableAHBBufferable; /*!< Enable/disable AHB bufferable write access support, when enabled,
  254. FLEXSPI return before waiting for command excution finished. */
  255. bool enableAHBCachable; /*!< Enable AHB bus cachable read access support. */
  256. } ahbConfig;
  257. } flexspi_config_t;
  258. /*! @brief External device configuration items. */
  259. typedef struct _flexspi_device_config
  260. {
  261. uint32_t flexspiRootClk; /*!< FLEXSPI serial root clock. */
  262. bool isSck2Enabled; /*!< FLEXSPI use SCK2. */
  263. uint32_t flashSize; /*!< Flash size in KByte. */
  264. flexspi_cs_interval_cycle_unit_t CSIntervalUnit; /*!< CS interval unit, 1 or 256 cycle. */
  265. uint16_t CSInterval; /*!< CS line assert interval, mutiply CS interval unit to
  266. get the CS line assert interval cycles. */
  267. uint8_t CSHoldTime; /*!< CS line hold time. */
  268. uint8_t CSSetupTime; /*!< CS line setup time. */
  269. uint8_t dataValidTime; /*!< Data valid time for external device. */
  270. uint8_t columnspace; /*!< Column space size. */
  271. bool enableWordAddress; /*!< If enable word address.*/
  272. uint8_t AWRSeqIndex; /*!< Sequence ID for AHB write command. */
  273. uint8_t AWRSeqNumber; /*!< Sequence number for AHB write command. */
  274. uint8_t ARDSeqIndex; /*!< Sequence ID for AHB read command. */
  275. uint8_t ARDSeqNumber; /*!< Sequence number for AHB read command. */
  276. flexspi_ahb_write_wait_unit_t AHBWriteWaitUnit; /*!< AHB write wait unit. */
  277. uint16_t AHBWriteWaitInterval; /*!< AHB write wait interval, mutiply AHB write interval
  278. unit to get the AHB write wait cycles. */
  279. bool enableWriteMask; /*!< Enable/Disable FLEXSPI drive DQS pin as write mask
  280. when writing to external device. */
  281. } flexspi_device_config_t;
  282. /*! @brief Transfer structure for FLEXSPI. */
  283. typedef struct _flexspi_transfer
  284. {
  285. uint32_t deviceAddress; /*!< Operation device address. */
  286. flexspi_port_t port; /*!< Operation port. */
  287. flexspi_command_type_t cmdType; /*!< Execution command type. */
  288. uint8_t seqIndex; /*!< Sequence ID for command. */
  289. uint8_t SeqNumber; /*!< Sequence number for command. */
  290. uint32_t *data; /*!< Data buffer. */
  291. size_t dataSize; /*!< Data size in bytes. */
  292. } flexspi_transfer_t;
  293. /* Forward declaration of the handle typedef. */
  294. typedef struct _flexspi_handle flexspi_handle_t;
  295. /*! @brief FLEXSPI transfer callback function. */
  296. typedef void (*flexspi_transfer_callback_t)(FLEXSPI_Type *base,
  297. flexspi_handle_t *handle,
  298. status_t status,
  299. void *userData);
  300. /*! @brief Transfer handle structure for FLEXSPI. */
  301. struct _flexspi_handle
  302. {
  303. uint32_t state; /*!< Internal state for FLEXSPI transfer */
  304. uint32_t *data; /*!< Data buffer. */
  305. size_t dataSize; /*!< Remaining Data size in bytes. */
  306. size_t transferTotalSize; /*!< Total Data size in bytes. */
  307. flexspi_transfer_callback_t completionCallback; /*!< Callback for users while transfer finish or error occurred */
  308. void *userData; /*!< FLEXSPI callback function parameter.*/
  309. };
  310. /*******************************************************************************
  311. * API
  312. ******************************************************************************/
  313. #if defined(__cplusplus)
  314. extern "C" {
  315. #endif /*_cplusplus. */
  316. /*!
  317. * @name Initialization and deinitialization
  318. * @{
  319. */
  320. /*!
  321. * @brief Initializes the FLEXSPI module and internal state.
  322. *
  323. * This function enables the clock for FLEXSPI and also configures the FLEXSPI with the
  324. * input configure parameters. Users should call this function before any FLEXSPI operations.
  325. *
  326. * @param base FLEXSPI peripheral base address.
  327. * @param config FLEXSPI configure structure.
  328. */
  329. void FLEXSPI_Init(FLEXSPI_Type *base, const flexspi_config_t *config);
  330. /*!
  331. * @brief Gets default settings for FLEXSPI.
  332. *
  333. * @param config FLEXSPI configuration structure.
  334. */
  335. void FLEXSPI_GetDefaultConfig(flexspi_config_t *config);
  336. /*!
  337. * @brief Deinitializes the FLEXSPI module.
  338. *
  339. * Clears the FLEXSPI state and FLEXSPI module registers.
  340. * @param base FLEXSPI peripheral base address.
  341. */
  342. void FLEXSPI_Deinit(FLEXSPI_Type *base);
  343. /*!
  344. * @brief Configures the connected device parameter.
  345. *
  346. * This function configures the connected device relevant parameters, such as the size, command, and so on.
  347. * The flash configuration value cannot have a default value. The user needs to configure it according to the
  348. * connected device.
  349. *
  350. * @param base FLEXSPI peripheral base address.
  351. * @param config Flash configuration parameters.
  352. * @param port FLEXSPI Operation port.
  353. */
  354. void FLEXSPI_SetFlashConfig(FLEXSPI_Type *base, flexspi_device_config_t *config, flexspi_port_t port);
  355. /*!
  356. * @brief Software reset for the FLEXSPI logic.
  357. *
  358. * This function sets the software reset flags for both AHB and buffer domain and
  359. * resets both AHB buffer and also IP FIFOs.
  360. *
  361. * @param base FLEXSPI peripheral base address.
  362. */
  363. static inline void FLEXSPI_SoftwareReset(FLEXSPI_Type *base)
  364. {
  365. base->MCR0 |= FLEXSPI_MCR0_SWRESET_MASK;
  366. while (base->MCR0 & FLEXSPI_MCR0_SWRESET_MASK)
  367. {
  368. }
  369. }
  370. /*!
  371. * @brief Enables or disables the FLEXSPI module.
  372. *
  373. * @param base FLEXSPI peripheral base address.
  374. * @param enable True means enable FLEXSPI, false means disable.
  375. */
  376. static inline void FLEXSPI_Enable(FLEXSPI_Type *base, bool enable)
  377. {
  378. if (enable)
  379. {
  380. base->MCR0 &= ~FLEXSPI_MCR0_MDIS_MASK;
  381. }
  382. else
  383. {
  384. base->MCR0 |= FLEXSPI_MCR0_MDIS_MASK;
  385. }
  386. }
  387. /* @} */
  388. /*!
  389. * @name Interrupts
  390. * @{
  391. */
  392. /*!
  393. * @brief Enables the FLEXSPI interrupts.
  394. *
  395. * @param base FLEXSPI peripheral base address.
  396. * @param mask FLEXSPI interrupt source.
  397. */
  398. static inline void FLEXSPI_EnableInterrupts(FLEXSPI_Type *base, uint32_t mask)
  399. {
  400. base->INTEN |= mask;
  401. }
  402. /*!
  403. * @brief Disable the FLEXSPI interrupts.
  404. *
  405. * @param base FLEXSPI peripheral base address.
  406. * @param mask FLEXSPI interrupt source.
  407. */
  408. static inline void FLEXSPI_DisableInterrupts(FLEXSPI_Type *base, uint32_t mask)
  409. {
  410. base->INTEN &= ~mask;
  411. }
  412. /* @} */
  413. /*! @name DMA control */
  414. /*@{*/
  415. /*!
  416. * @brief Enables or disables FLEXSPI IP Tx FIFO DMA requests.
  417. *
  418. * @param base FLEXSPI peripheral base address.
  419. * @param enable Enable flag for transmit DMA request. Pass true for enable, false for disable.
  420. */
  421. static inline void FLEXSPI_EnableTxDMA(FLEXSPI_Type *base, bool enable)
  422. {
  423. if (enable)
  424. {
  425. base->IPTXFCR |= FLEXSPI_IPTXFCR_TXDMAEN_MASK;
  426. }
  427. else
  428. {
  429. base->IPTXFCR &= ~FLEXSPI_IPTXFCR_TXDMAEN_MASK;
  430. }
  431. }
  432. /*!
  433. * @brief Enables or disables FLEXSPI IP Rx FIFO DMA requests.
  434. *
  435. * @param base FLEXSPI peripheral base address.
  436. * @param enable Enable flag for receive DMA request. Pass true for enable, false for disable.
  437. */
  438. static inline void FLEXSPI_EnableRxDMA(FLEXSPI_Type *base, bool enable)
  439. {
  440. if (enable)
  441. {
  442. base->IPRXFCR |= FLEXSPI_IPRXFCR_RXDMAEN_MASK;
  443. }
  444. else
  445. {
  446. base->IPRXFCR &= ~FLEXSPI_IPRXFCR_RXDMAEN_MASK;
  447. }
  448. }
  449. /*!
  450. * @brief Gets FLEXSPI IP tx fifo address for DMA transfer.
  451. *
  452. * @param base FLEXSPI peripheral base address.
  453. * @retval The tx fifo address.
  454. */
  455. static inline uint32_t FLEXSPI_GetTxFifoAddress(FLEXSPI_Type *base)
  456. {
  457. return (uint32_t)&base->TFDR[0];
  458. }
  459. /*!
  460. * @brief Gets FLEXSPI IP rx fifo address for DMA transfer.
  461. *
  462. * @param base FLEXSPI peripheral base address.
  463. * @retval The rx fifo address.
  464. */
  465. static inline uint32_t FLEXSPI_GetRxFifoAddress(FLEXSPI_Type *base)
  466. {
  467. return (uint32_t)&base->RFDR[0];
  468. }
  469. /*@}*/
  470. /*! @name FIFO control */
  471. /*@{*/
  472. /*! @brief Clears the FLEXSPI IP FIFO logic.
  473. *
  474. * @param base FLEXSPI peripheral base address.
  475. * @param txFifo Pass true to reset TX FIFO.
  476. * @param rxFifo Pass true to reset RX FIFO.
  477. */
  478. static inline void FLEXSPI_ResetFifos(FLEXSPI_Type *base, bool txFifo, bool rxFifo)
  479. {
  480. if (txFifo)
  481. {
  482. base->IPTXFCR |= FLEXSPI_IPTXFCR_CLRIPTXF_MASK;
  483. }
  484. if (rxFifo)
  485. {
  486. base->IPRXFCR |= FLEXSPI_IPRXFCR_CLRIPRXF_MASK;
  487. }
  488. }
  489. /*!
  490. * @brief Gets the valid data entries in the FLEXSPI FIFOs.
  491. *
  492. * @param base FLEXSPI peripheral base address.
  493. * @param[out] txCount Pointer through which the current number of bytes in the transmit FIFO is returned.
  494. * Pass NULL if this value is not required.
  495. * @param[out] rxCount Pointer through which the current number of bytes in the receive FIFO is returned.
  496. * Pass NULL if this value is not required.
  497. */
  498. static inline void FLEXSPI_GetFifoCounts(FLEXSPI_Type *base, size_t *txCount, size_t *rxCount)
  499. {
  500. if (txCount)
  501. {
  502. *txCount = (((base->IPTXFSTS) & FLEXSPI_IPTXFSTS_FILL_MASK) >> FLEXSPI_IPTXFSTS_FILL_SHIFT) * 8U;
  503. }
  504. if (rxCount)
  505. {
  506. *rxCount = (((base->IPRXFSTS) & FLEXSPI_IPRXFSTS_FILL_MASK) >> FLEXSPI_IPRXFSTS_FILL_SHIFT) * 8U;
  507. }
  508. }
  509. /*@}*/
  510. /*!
  511. * @name Status
  512. * @{
  513. */
  514. /*!
  515. * @brief Get the FLEXSPI interrupt status flags.
  516. *
  517. * @param base FLEXSPI peripheral base address.
  518. * @retval interrupt status flag, use status flag to AND #flexspi_flags_t could get the related status.
  519. */
  520. static inline uint32_t FLEXSPI_GetInterruptStatusFlags(FLEXSPI_Type *base)
  521. {
  522. return base->INTR;
  523. }
  524. /*!
  525. * @brief Get the FLEXSPI interrupt status flags.
  526. *
  527. * @param base FLEXSPI peripheral base address.
  528. * @param interrupt status flag.
  529. */
  530. static inline void FLEXSPI_ClearInterruptStatusFlags(FLEXSPI_Type *base, uint32_t mask)
  531. {
  532. base->INTR |= mask;
  533. }
  534. #if !((defined(FSL_FEATURE_FLEXSPI_HAS_NO_DATA_LEARN)) && (FSL_FEATURE_FLEXSPI_HAS_NO_DATA_LEARN))
  535. /*! @brief Gets the sampling clock phase selection after Data Learning.
  536. *
  537. * @param base FLEXSPI peripheral base address.
  538. * @param portAPhase Pointer to a uint8_t type variable to receive the selected clock phase on PORTA.
  539. * @param portBPhase Pointer to a uint8_t type variable to receive the selected clock phase on PORTB.
  540. */
  541. static inline void FLEXSPI_GetDataLearningPhase(FLEXSPI_Type *base, uint8_t *portAPhase, uint8_t *portBPhase)
  542. {
  543. if (portAPhase)
  544. {
  545. *portAPhase = (base->STS0 & FLEXSPI_STS0_DATALEARNPHASEA_MASK) >> FLEXSPI_STS0_DATALEARNPHASEA_SHIFT;
  546. }
  547. if (portBPhase)
  548. {
  549. *portBPhase = (base->STS0 & FLEXSPI_STS0_DATALEARNPHASEB_MASK) >> FLEXSPI_STS0_DATALEARNPHASEB_SHIFT;
  550. }
  551. }
  552. #endif
  553. /*! @brief Gets the trigger source of current command sequence granted by arbitrator.
  554. *
  555. * @param base FLEXSPI peripheral base address.
  556. * @retval trigger source of current command sequence.
  557. */
  558. static inline flexspi_arb_command_source_t FLEXSPI_GetArbitratorCommandSource(FLEXSPI_Type *base)
  559. {
  560. return (flexspi_arb_command_source_t)((base->STS0 & FLEXSPI_STS0_ARBCMDSRC_MASK) >> FLEXSPI_STS0_ARBCMDSRC_SHIFT);
  561. }
  562. /*! @brief Gets the error code when IP command error detected.
  563. *
  564. * @param base FLEXSPI peripheral base address.
  565. * @param index Pointer to a uint8_t type variable to receive the sequence index when error detected.
  566. * @retval error code when IP command error detected.
  567. */
  568. static inline flexspi_ip_error_code_t FLEXSPI_GetIPCommandErrorCode(FLEXSPI_Type *base, uint8_t *index)
  569. {
  570. *index = (base->STS1 & FLEXSPI_STS1_IPCMDERRID_MASK) >> FLEXSPI_STS1_IPCMDERRID_SHIFT;
  571. return (flexspi_ip_error_code_t)((base->STS1 & FLEXSPI_STS1_IPCMDERRCODE_MASK) >> FLEXSPI_STS1_IPCMDERRCODE_SHIFT);
  572. }
  573. /*! @brief Gets the error code when AHB command error detected.
  574. *
  575. * @param base FLEXSPI peripheral base address.
  576. * @param index Pointer to a uint8_t type variable to receive the sequence index when error detected.
  577. * @retval error code when AHB command error detected.
  578. */
  579. static inline flexspi_ahb_error_code_t FLEXSPI_GetAHBCommandErrorCode(FLEXSPI_Type *base, uint8_t *index)
  580. {
  581. *index = (base->STS1 & FLEXSPI_STS1_AHBCMDERRID_MASK) >> FLEXSPI_STS1_AHBCMDERRID_SHIFT;
  582. return (flexspi_ahb_error_code_t)((base->STS1 & FLEXSPI_STS1_AHBCMDERRCODE_MASK) >>
  583. FLEXSPI_STS1_AHBCMDERRCODE_SHIFT);
  584. }
  585. /*! @brief Returns whether the bus is idle.
  586. *
  587. * @param base FLEXSPI peripheral base address.
  588. * @retval true Bus is idle.
  589. * @retval false Bus is busy.
  590. */
  591. static inline bool FLEXSPI_GetBusIdleStatus(FLEXSPI_Type *base)
  592. {
  593. return (base->STS0 & FLEXSPI_STS0_ARBIDLE_MASK) && (base->STS0 & FLEXSPI_STS0_SEQIDLE_MASK);
  594. }
  595. /*@}*/
  596. /*!
  597. * @name Bus Operations
  598. * @{
  599. */
  600. /*! @brief Enables/disables the FLEXSPI IP command parallel mode.
  601. *
  602. * @param base FLEXSPI peripheral base address.
  603. * @param enable True means enable parallel mode, false means disable parallel mode.
  604. */
  605. static inline void FLEXSPI_EnableIPParallelMode(FLEXSPI_Type *base, bool enable)
  606. {
  607. if (enable)
  608. {
  609. base->IPCR1 |= FLEXSPI_IPCR1_IPAREN_MASK;
  610. }
  611. else
  612. {
  613. base->IPCR1 &= ~FLEXSPI_IPCR1_IPAREN_MASK;
  614. }
  615. }
  616. /*! @brief Enables/disables the FLEXSPI AHB command parallel mode.
  617. *
  618. * @param base FLEXSPI peripheral base address.
  619. * @param enable True means enable parallel mode, false means disable parallel mode.
  620. */
  621. static inline void FLEXSPI_EnableAHBParallelMode(FLEXSPI_Type *base, bool enable)
  622. {
  623. if (enable)
  624. {
  625. base->AHBCR |= FLEXSPI_AHBCR_APAREN_MASK;
  626. }
  627. else
  628. {
  629. base->AHBCR &= ~FLEXSPI_AHBCR_APAREN_MASK;
  630. }
  631. }
  632. /*! @brief Updates the LUT table.
  633. *
  634. * @param base FLEXSPI peripheral base address.
  635. * @param index From which index start to update. It could be any index of the LUT table, which
  636. * also allows user to update command content inside a command. Each command consists of up to
  637. * 8 instructions and occupy 4*32-bit memory.
  638. * @param cmd Command sequence array.
  639. * @param count Number of sequences.
  640. */
  641. void FLEXSPI_UpdateLUT(FLEXSPI_Type *base, uint32_t index, const uint32_t *cmd, uint32_t count);
  642. /*!
  643. * @brief Writes data into FIFO.
  644. *
  645. * @param base FLEXSPI peripheral base address
  646. * @param data The data bytes to send
  647. * @param fifoIndex Destination fifo index.
  648. */
  649. static inline void FLEXSPI_WriteData(FLEXSPI_Type *base, uint32_t data, uint8_t fifoIndex)
  650. {
  651. base->TFDR[fifoIndex] = data;
  652. }
  653. /*!
  654. * @brief Receives data from data FIFO.
  655. *
  656. * @param base FLEXSPI peripheral base address
  657. * @param fifoIndex Source fifo index.
  658. * @return The data in the FIFO.
  659. */
  660. static inline uint32_t FLEXSPI_ReadData(FLEXSPI_Type *base, uint8_t fifoIndex)
  661. {
  662. return base->RFDR[fifoIndex];
  663. }
  664. /*!
  665. * @brief Sends a buffer of data bytes using blocking method.
  666. * @note This function blocks via polling until all bytes have been sent.
  667. * @param base FLEXSPI peripheral base address
  668. * @param buffer The data bytes to send
  669. * @param size The number of data bytes to send
  670. * @retval kStatus_Success write success without error
  671. * @retval kStatus_FLEXSPI_SequenceExecutionTimeout sequence execution timeout
  672. * @retval kStatus_FLEXSPI_IpCommandSequenceError IP command sequencen error detected
  673. * @retval kStatus_FLEXSPI_IpCommandGrantTimeout IP command grant timeout detected
  674. */
  675. status_t FLEXSPI_WriteBlocking(FLEXSPI_Type *base, uint32_t *buffer, size_t size);
  676. /*!
  677. * @brief Receives a buffer of data bytes using a blocking method.
  678. * @note This function blocks via polling until all bytes have been sent.
  679. * @param base FLEXSPI peripheral base address
  680. * @param buffer The data bytes to send
  681. * @param size The number of data bytes to receive
  682. * @retval kStatus_Success read success without error
  683. * @retval kStatus_FLEXSPI_SequenceExecutionTimeout sequence execution timeout
  684. * @retval kStatus_FLEXSPI_IpCommandSequenceError IP command sequencen error detected
  685. * @retval kStatus_FLEXSPI_IpCommandGrantTimeout IP command grant timeout detected
  686. */
  687. status_t FLEXSPI_ReadBlocking(FLEXSPI_Type *base, uint32_t *buffer, size_t size);
  688. /*!
  689. * @brief Execute command to transfer a buffer data bytes using a blocking method.
  690. * @param base FLEXSPI peripheral base address
  691. * @param xfer pointer to the transfer structure.
  692. * @retval kStatus_Success command transfer success without error
  693. * @retval kStatus_FLEXSPI_SequenceExecutionTimeout sequence execution timeout
  694. * @retval kStatus_FLEXSPI_IpCommandSequenceError IP command sequencen error detected
  695. * @retval kStatus_FLEXSPI_IpCommandGrantTimeout IP command grant timeout detected
  696. */
  697. status_t FLEXSPI_TransferBlocking(FLEXSPI_Type *base, flexspi_transfer_t *xfer);
  698. /*! @} */
  699. /*!
  700. * @name Transactional
  701. * @{
  702. */
  703. /*!
  704. * @brief Initializes the FLEXSPI handle which is used in transactional functions.
  705. *
  706. * @param base FLEXSPI peripheral base address.
  707. * @param handle pointer to flexspi_handle_t structure to store the transfer state.
  708. * @param callback pointer to user callback function.
  709. * @param userData user parameter passed to the callback function.
  710. */
  711. void FLEXSPI_TransferCreateHandle(FLEXSPI_Type *base,
  712. flexspi_handle_t *handle,
  713. flexspi_transfer_callback_t callback,
  714. void *userData);
  715. /*!
  716. * @brief Performs a interrupt non-blocking transfer on the FLEXSPI bus.
  717. *
  718. * @note Calling the API returns immediately after transfer initiates. The user needs
  719. * to call FLEXSPI_GetTransferCount to poll the transfer status to check whether
  720. * the transfer is finished. If the return status is not kStatus_FLEXSPI_Busy, the transfer
  721. * is finished. For FLEXSPI_Read, the dataSize should be multiple of rx watermark levle, or
  722. * FLEXSPI could not read data properly.
  723. *
  724. * @param base FLEXSPI peripheral base address.
  725. * @param handle pointer to flexspi_handle_t structure which stores the transfer state.
  726. * @param xfer pointer to flexspi_transfer_t structure.
  727. * @retval kStatus_Success Successfully start the data transmission.
  728. * @retval kStatus_FLEXSPI_Busy Previous transmission still not finished.
  729. */
  730. status_t FLEXSPI_TransferNonBlocking(FLEXSPI_Type *base, flexspi_handle_t *handle, flexspi_transfer_t *xfer);
  731. /*!
  732. * @brief Gets the master transfer status during a interrupt non-blocking transfer.
  733. *
  734. * @param base FLEXSPI peripheral base address.
  735. * @param handle pointer to flexspi_handle_t structure which stores the transfer state.
  736. * @param count Number of bytes transferred so far by the non-blocking transaction.
  737. * @retval kStatus_InvalidArgument count is Invalid.
  738. * @retval kStatus_Success Successfully return the count.
  739. */
  740. status_t FLEXSPI_TransferGetCount(FLEXSPI_Type *base, flexspi_handle_t *handle, size_t *count);
  741. /*!
  742. * @brief Aborts an interrupt non-blocking transfer early.
  743. *
  744. * @note This API can be called at any time when an interrupt non-blocking transfer initiates
  745. * to abort the transfer early.
  746. *
  747. * @param base FLEXSPI peripheral base address.
  748. * @param handle pointer to flexspi_handle_t structure which stores the transfer state
  749. */
  750. void FLEXSPI_TransferAbort(FLEXSPI_Type *base, flexspi_handle_t *handle);
  751. /*!
  752. * @brief Master interrupt handler.
  753. *
  754. * @param base FLEXSPI peripheral base address.
  755. * @param handle pointer to flexspi_handle_t structure.
  756. */
  757. void FLEXSPI_TransferHandleIRQ(FLEXSPI_Type *base, flexspi_handle_t *handle);
  758. /*! @} */
  759. #if defined(__cplusplus)
  760. }
  761. #endif /*_cplusplus. */
  762. /*@}*/
  763. #endif /* __FSL_FLEXSPI_H_ */