fsl_qspi_edma.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "fsl_qspi_edma.h"
  35. /*******************************************************************************
  36. * Definitions
  37. ******************************************************************************/
  38. /* Component ID definition, used by tools. */
  39. #ifndef FSL_COMPONENT_ID
  40. #define FSL_COMPONENT_ID "platform.drivers.qspi_edma"
  41. #endif
  42. /*<! Structure definition for qspi_edma_private_handle_t. The structure is private. */
  43. typedef struct _qspi_edma_private_handle
  44. {
  45. QuadSPI_Type *base;
  46. qspi_edma_handle_t *handle;
  47. } qspi_edma_private_handle_t;
  48. /* QSPI EDMA transfer handle. */
  49. enum _qspi_edma_tansfer_states
  50. {
  51. kQSPI_Idle, /* TX idle. */
  52. kQSPI_BusBusy /* RX busy. */
  53. };
  54. /*******************************************************************************
  55. * Variables
  56. ******************************************************************************/
  57. /*<! Private handle only used for internally. */
  58. static qspi_edma_private_handle_t s_edmaPrivateHandle[FSL_FEATURE_SOC_QuadSPI_COUNT][2];
  59. /*******************************************************************************
  60. * Prototypes
  61. ******************************************************************************/
  62. /*!
  63. * @brief QSPI EDMA send finished callback function.
  64. *
  65. * This function is called when QSPI EDMA send finished. It disables the QSPI
  66. * TX EDMA request and sends @ref kStatus_QSPI_TxIdle to QSPI callback.
  67. *
  68. * @param handle The EDMA handle.
  69. * @param param Callback function parameter.
  70. */
  71. static void QSPI_SendEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds);
  72. /*!
  73. * @brief QSPI EDMA receive finished callback function.
  74. *
  75. * This function is called when QSPI EDMA receive finished. It disables the QSPI
  76. * RX EDMA request and sends @ref kStatus_QSPI_RxIdle to QSPI callback.
  77. *
  78. * @param handle The EDMA handle.
  79. * @param param Callback function parameter.
  80. */
  81. static void QSPI_ReceiveEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds);
  82. /*******************************************************************************
  83. * Code
  84. ******************************************************************************/
  85. static void QSPI_SendEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds)
  86. {
  87. qspi_edma_private_handle_t *qspiPrivateHandle = (qspi_edma_private_handle_t *)param;
  88. /* Avoid the warning for unused variables. */
  89. handle = handle;
  90. tcds = tcds;
  91. if (transferDone)
  92. {
  93. QSPI_TransferAbortSendEDMA(qspiPrivateHandle->base, qspiPrivateHandle->handle);
  94. if (qspiPrivateHandle->handle->callback)
  95. {
  96. qspiPrivateHandle->handle->callback(qspiPrivateHandle->base, qspiPrivateHandle->handle, kStatus_QSPI_Idle,
  97. qspiPrivateHandle->handle->userData);
  98. }
  99. }
  100. }
  101. static void QSPI_ReceiveEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds)
  102. {
  103. qspi_edma_private_handle_t *qspiPrivateHandle = (qspi_edma_private_handle_t *)param;
  104. /* Avoid warning for unused parameters. */
  105. handle = handle;
  106. tcds = tcds;
  107. if (transferDone)
  108. {
  109. /* Disable transfer. */
  110. QSPI_TransferAbortReceiveEDMA(qspiPrivateHandle->base, qspiPrivateHandle->handle);
  111. if (qspiPrivateHandle->handle->callback)
  112. {
  113. qspiPrivateHandle->handle->callback(qspiPrivateHandle->base, qspiPrivateHandle->handle, kStatus_QSPI_Idle,
  114. qspiPrivateHandle->handle->userData);
  115. }
  116. }
  117. }
  118. void QSPI_TransferTxCreateHandleEDMA(QuadSPI_Type *base,
  119. qspi_edma_handle_t *handle,
  120. qspi_edma_callback_t callback,
  121. void *userData,
  122. edma_handle_t *dmaHandle)
  123. {
  124. assert(handle);
  125. uint32_t instance = QSPI_GetInstance(base);
  126. s_edmaPrivateHandle[instance][0].base = base;
  127. s_edmaPrivateHandle[instance][0].handle = handle;
  128. memset(handle, 0, sizeof(*handle));
  129. handle->state = kQSPI_Idle;
  130. handle->dmaHandle = dmaHandle;
  131. handle->callback = callback;
  132. handle->userData = userData;
  133. /* Get the watermark value */
  134. handle->count = base->TBCT + 1;
  135. /* Configure TX edma callback */
  136. EDMA_SetCallback(handle->dmaHandle, QSPI_SendEDMACallback, &s_edmaPrivateHandle[instance][0]);
  137. }
  138. void QSPI_TransferRxCreateHandleEDMA(QuadSPI_Type *base,
  139. qspi_edma_handle_t *handle,
  140. qspi_edma_callback_t callback,
  141. void *userData,
  142. edma_handle_t *dmaHandle)
  143. {
  144. assert(handle);
  145. uint32_t instance = QSPI_GetInstance(base);
  146. s_edmaPrivateHandle[instance][1].base = base;
  147. s_edmaPrivateHandle[instance][1].handle = handle;
  148. memset(handle, 0, sizeof(*handle));
  149. handle->state = kQSPI_Idle;
  150. handle->dmaHandle = dmaHandle;
  151. handle->callback = callback;
  152. handle->userData = userData;
  153. /* Get the watermark value */
  154. handle->count = (base->RBCT & QuadSPI_RBCT_WMRK_MASK) + 1;
  155. /* Configure RX edma callback */
  156. EDMA_SetCallback(handle->dmaHandle, QSPI_ReceiveEDMACallback, &s_edmaPrivateHandle[instance][1]);
  157. }
  158. status_t QSPI_TransferSendEDMA(QuadSPI_Type *base, qspi_edma_handle_t *handle, qspi_transfer_t *xfer)
  159. {
  160. assert(handle && (handle->dmaHandle));
  161. edma_transfer_config_t xferConfig;
  162. status_t status;
  163. /* If previous TX not finished. */
  164. if (kQSPI_BusBusy == handle->state)
  165. {
  166. status = kStatus_QSPI_Busy;
  167. }
  168. else
  169. {
  170. handle->state = kQSPI_BusBusy;
  171. /* Prepare transfer. */
  172. EDMA_PrepareTransfer(&xferConfig, xfer->data, sizeof(uint32_t), (void *)QSPI_GetTxDataRegisterAddress(base),
  173. sizeof(uint32_t), (sizeof(uint32_t) * handle->count), xfer->dataSize,
  174. kEDMA_MemoryToPeripheral);
  175. /* Store the initially configured eDMA minor byte transfer count into the QSPI handle */
  176. handle->nbytes = (sizeof(uint32_t) * handle->count);
  177. /* Submit transfer. */
  178. EDMA_SubmitTransfer(handle->dmaHandle, &xferConfig);
  179. EDMA_StartTransfer(handle->dmaHandle);
  180. /* Enable QSPI TX EDMA. */
  181. QSPI_EnableDMA(base, kQSPI_TxBufferFillDMAEnable, true);
  182. status = kStatus_Success;
  183. }
  184. return status;
  185. }
  186. status_t QSPI_TransferReceiveEDMA(QuadSPI_Type *base, qspi_edma_handle_t *handle, qspi_transfer_t *xfer)
  187. {
  188. assert(handle && (handle->dmaHandle));
  189. edma_transfer_config_t xferConfig;
  190. status_t status;
  191. /* If previous TX not finished. */
  192. if (kQSPI_BusBusy == handle->state)
  193. {
  194. status = kStatus_QSPI_Busy;
  195. }
  196. else
  197. {
  198. handle->state = kQSPI_BusBusy;
  199. /* Prepare transfer. */
  200. EDMA_PrepareTransfer(&xferConfig, (void *)QSPI_GetRxDataRegisterAddress(base), sizeof(uint32_t), xfer->data,
  201. sizeof(uint32_t), (sizeof(uint32_t) * handle->count), xfer->dataSize,
  202. kEDMA_MemoryToMemory);
  203. /* Store the initially configured eDMA minor byte transfer count into the QSPI handle */
  204. handle->nbytes = (sizeof(uint32_t) * handle->count);
  205. /* Submit transfer. */
  206. EDMA_SubmitTransfer(handle->dmaHandle, &xferConfig);
  207. handle->dmaHandle->base->TCD[handle->dmaHandle->channel].ATTR |= DMA_ATTR_SMOD(0x5U);
  208. EDMA_StartTransfer(handle->dmaHandle);
  209. /* Enable QSPI TX EDMA. */
  210. QSPI_EnableDMA(base, kQSPI_RxBufferDrainDMAEnable, true);
  211. status = kStatus_Success;
  212. }
  213. return status;
  214. }
  215. void QSPI_TransferAbortSendEDMA(QuadSPI_Type *base, qspi_edma_handle_t *handle)
  216. {
  217. assert(handle && (handle->dmaHandle));
  218. /* Disable QSPI TX EDMA. */
  219. QSPI_EnableDMA(base, kQSPI_TxBufferFillDMAEnable, false);
  220. /* Stop transfer. */
  221. EDMA_AbortTransfer(handle->dmaHandle);
  222. handle->state = kQSPI_Idle;
  223. }
  224. void QSPI_TransferAbortReceiveEDMA(QuadSPI_Type *base, qspi_edma_handle_t *handle)
  225. {
  226. assert(handle && (handle->dmaHandle));
  227. /* Disable QSPI RX EDMA. */
  228. QSPI_EnableDMA(base, kQSPI_RxBufferDrainDMAEnable, false);
  229. /* Stop transfer. */
  230. EDMA_AbortTransfer(handle->dmaHandle);
  231. handle->state = kQSPI_Idle;
  232. }
  233. status_t QSPI_TransferGetSendCountEDMA(QuadSPI_Type *base, qspi_edma_handle_t *handle, size_t *count)
  234. {
  235. assert(handle);
  236. status_t status = kStatus_Success;
  237. if (handle->state != kQSPI_BusBusy)
  238. {
  239. status = kStatus_NoTransferInProgress;
  240. }
  241. else
  242. {
  243. *count = (handle->transferSize -
  244. (uint32_t)handle->nbytes *
  245. EDMA_GetRemainingMajorLoopCount(handle->dmaHandle->base, handle->dmaHandle->channel));
  246. }
  247. return status;
  248. }
  249. status_t QSPI_TransferGetReceiveCountEDMA(QuadSPI_Type *base, qspi_edma_handle_t *handle, size_t *count)
  250. {
  251. assert(handle);
  252. status_t status = kStatus_Success;
  253. if (handle->state != kQSPI_BusBusy)
  254. {
  255. status = kStatus_NoTransferInProgress;
  256. }
  257. else
  258. {
  259. *count = (handle->transferSize -
  260. (uint32_t)handle->nbytes *
  261. EDMA_GetRemainingMajorLoopCount(handle->dmaHandle->base, handle->dmaHandle->channel));
  262. }
  263. return status;
  264. }