fsl_sai_edma.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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_sai_edma.h"
  35. /* Component ID definition, used by tools. */
  36. #ifndef FSL_COMPONENT_ID
  37. #define FSL_COMPONENT_ID "platform.drivers.sai_edma"
  38. #endif
  39. /*******************************************************************************
  40. * Definitations
  41. ******************************************************************************/
  42. /* Used for 32byte aligned */
  43. #define STCD_ADDR(address) (edma_tcd_t *)(((uint32_t)(address) + 32) & ~0x1FU)
  44. static I2S_Type *const s_saiBases[] = I2S_BASE_PTRS;
  45. /*<! Structure definition for uart_edma_private_handle_t. The structure is private. */
  46. typedef struct _sai_edma_private_handle
  47. {
  48. I2S_Type *base;
  49. sai_edma_handle_t *handle;
  50. } sai_edma_private_handle_t;
  51. enum _sai_edma_transfer_state
  52. {
  53. kSAI_Busy = 0x0U, /*!< SAI is busy */
  54. kSAI_Idle, /*!< Transfer is done. */
  55. };
  56. /*<! Private handle only used for internally. */
  57. static sai_edma_private_handle_t s_edmaPrivateHandle[ARRAY_SIZE(s_saiBases)][2];
  58. /*******************************************************************************
  59. * Prototypes
  60. ******************************************************************************/
  61. /*!
  62. * @brief Get the instance number for SAI.
  63. *
  64. * @param base SAI base pointer.
  65. */
  66. static uint32_t SAI_GetInstance(I2S_Type *base);
  67. /*!
  68. * @brief SAI EDMA callback for send.
  69. *
  70. * @param handle pointer to sai_edma_handle_t structure which stores the transfer state.
  71. * @param userData Parameter for user callback.
  72. * @param done If the DMA transfer finished.
  73. * @param tcds The TCD index.
  74. */
  75. static void SAI_TxEDMACallback(edma_handle_t *handle, void *userData, bool done, uint32_t tcds);
  76. /*!
  77. * @brief SAI EDMA callback for receive.
  78. *
  79. * @param handle pointer to sai_edma_handle_t structure which stores the transfer state.
  80. * @param userData Parameter for user callback.
  81. * @param done If the DMA transfer finished.
  82. * @param tcds The TCD index.
  83. */
  84. static void SAI_RxEDMACallback(edma_handle_t *handle, void *userData, bool done, uint32_t tcds);
  85. /*******************************************************************************
  86. * Code
  87. ******************************************************************************/
  88. static uint32_t SAI_GetInstance(I2S_Type *base)
  89. {
  90. uint32_t instance;
  91. /* Find the instance index from base address mappings. */
  92. for (instance = 0; instance < ARRAY_SIZE(s_saiBases); instance++)
  93. {
  94. if (s_saiBases[instance] == base)
  95. {
  96. break;
  97. }
  98. }
  99. assert(instance < ARRAY_SIZE(s_saiBases));
  100. return instance;
  101. }
  102. static void SAI_TxEDMACallback(edma_handle_t *handle, void *userData, bool done, uint32_t tcds)
  103. {
  104. sai_edma_private_handle_t *privHandle = (sai_edma_private_handle_t *)userData;
  105. sai_edma_handle_t *saiHandle = privHandle->handle;
  106. /* If finished a blcok, call the callback function */
  107. memset(&saiHandle->saiQueue[saiHandle->queueDriver], 0, sizeof(sai_transfer_t));
  108. saiHandle->queueDriver = (saiHandle->queueDriver + 1) % SAI_XFER_QUEUE_SIZE;
  109. if (saiHandle->callback)
  110. {
  111. (saiHandle->callback)(privHandle->base, saiHandle, kStatus_SAI_TxIdle, saiHandle->userData);
  112. }
  113. /* If all data finished, just stop the transfer */
  114. if (saiHandle->saiQueue[saiHandle->queueDriver].data == NULL)
  115. {
  116. /* Disable DMA enable bit */
  117. SAI_TxEnableDMA(privHandle->base, kSAI_FIFORequestDMAEnable, false);
  118. EDMA_AbortTransfer(handle);
  119. }
  120. }
  121. static void SAI_RxEDMACallback(edma_handle_t *handle, void *userData, bool done, uint32_t tcds)
  122. {
  123. sai_edma_private_handle_t *privHandle = (sai_edma_private_handle_t *)userData;
  124. sai_edma_handle_t *saiHandle = privHandle->handle;
  125. /* If finished a blcok, call the callback function */
  126. memset(&saiHandle->saiQueue[saiHandle->queueDriver], 0, sizeof(sai_transfer_t));
  127. saiHandle->queueDriver = (saiHandle->queueDriver + 1) % SAI_XFER_QUEUE_SIZE;
  128. if (saiHandle->callback)
  129. {
  130. (saiHandle->callback)(privHandle->base, saiHandle, kStatus_SAI_RxIdle, saiHandle->userData);
  131. }
  132. /* If all data finished, just stop the transfer */
  133. if (saiHandle->saiQueue[saiHandle->queueDriver].data == NULL)
  134. {
  135. /* Disable DMA enable bit */
  136. SAI_RxEnableDMA(privHandle->base, kSAI_FIFORequestDMAEnable, false);
  137. EDMA_AbortTransfer(handle);
  138. }
  139. }
  140. void SAI_TransferTxCreateHandleEDMA(
  141. I2S_Type *base, sai_edma_handle_t *handle, sai_edma_callback_t callback, void *userData, edma_handle_t *dmaHandle)
  142. {
  143. assert(handle && dmaHandle);
  144. uint32_t instance = SAI_GetInstance(base);
  145. /* Zero the handle */
  146. memset(handle, 0, sizeof(*handle));
  147. /* Set sai base to handle */
  148. handle->dmaHandle = dmaHandle;
  149. handle->callback = callback;
  150. handle->userData = userData;
  151. /* Set SAI state to idle */
  152. handle->state = kSAI_Idle;
  153. s_edmaPrivateHandle[instance][0].base = base;
  154. s_edmaPrivateHandle[instance][0].handle = handle;
  155. /* Need to use scatter gather */
  156. EDMA_InstallTCDMemory(dmaHandle, (edma_tcd_t *)(STCD_ADDR(handle->tcd)), SAI_XFER_QUEUE_SIZE);
  157. /* Install callback for Tx dma channel */
  158. EDMA_SetCallback(dmaHandle, SAI_TxEDMACallback, &s_edmaPrivateHandle[instance][0]);
  159. }
  160. void SAI_TransferRxCreateHandleEDMA(
  161. I2S_Type *base, sai_edma_handle_t *handle, sai_edma_callback_t callback, void *userData, edma_handle_t *dmaHandle)
  162. {
  163. assert(handle && dmaHandle);
  164. uint32_t instance = SAI_GetInstance(base);
  165. /* Zero the handle */
  166. memset(handle, 0, sizeof(*handle));
  167. /* Set sai base to handle */
  168. handle->dmaHandle = dmaHandle;
  169. handle->callback = callback;
  170. handle->userData = userData;
  171. /* Set SAI state to idle */
  172. handle->state = kSAI_Idle;
  173. s_edmaPrivateHandle[instance][1].base = base;
  174. s_edmaPrivateHandle[instance][1].handle = handle;
  175. /* Need to use scatter gather */
  176. EDMA_InstallTCDMemory(dmaHandle, STCD_ADDR(handle->tcd), SAI_XFER_QUEUE_SIZE);
  177. /* Install callback for Tx dma channel */
  178. EDMA_SetCallback(dmaHandle, SAI_RxEDMACallback, &s_edmaPrivateHandle[instance][1]);
  179. }
  180. void SAI_TransferTxSetFormatEDMA(I2S_Type *base,
  181. sai_edma_handle_t *handle,
  182. sai_transfer_format_t *format,
  183. uint32_t mclkSourceClockHz,
  184. uint32_t bclkSourceClockHz)
  185. {
  186. assert(handle && format);
  187. /* Configure the audio format to SAI registers */
  188. SAI_TxSetFormat(base, format, mclkSourceClockHz, bclkSourceClockHz);
  189. /* Get the tranfer size from format, this should be used in EDMA configuration */
  190. if (format->bitWidth == 24U)
  191. {
  192. handle->bytesPerFrame = 4U;
  193. }
  194. else
  195. {
  196. handle->bytesPerFrame = format->bitWidth / 8U;
  197. }
  198. /* Update the data channel SAI used */
  199. handle->channel = format->channel;
  200. /* Clear the channel enable bits unitl do a send/receive */
  201. base->TCR3 &= ~I2S_TCR3_TCE_MASK;
  202. #if defined(FSL_FEATURE_SAI_FIFO_COUNT) && (FSL_FEATURE_SAI_FIFO_COUNT > 1)
  203. handle->count = FSL_FEATURE_SAI_FIFO_COUNT - format->watermark;
  204. #else
  205. handle->count = 1U;
  206. #endif /* FSL_FEATURE_SAI_FIFO_COUNT */
  207. }
  208. void SAI_TransferRxSetFormatEDMA(I2S_Type *base,
  209. sai_edma_handle_t *handle,
  210. sai_transfer_format_t *format,
  211. uint32_t mclkSourceClockHz,
  212. uint32_t bclkSourceClockHz)
  213. {
  214. assert(handle && format);
  215. /* Configure the audio format to SAI registers */
  216. SAI_RxSetFormat(base, format, mclkSourceClockHz, bclkSourceClockHz);
  217. /* Get the tranfer size from format, this should be used in EDMA configuration */
  218. if (format->bitWidth == 24U)
  219. {
  220. handle->bytesPerFrame = 4U;
  221. }
  222. else
  223. {
  224. handle->bytesPerFrame = format->bitWidth / 8U;
  225. }
  226. /* Update the data channel SAI used */
  227. handle->channel = format->channel;
  228. /* Clear the channel enable bits unitl do a send/receive */
  229. base->RCR3 &= ~I2S_RCR3_RCE_MASK;
  230. #if defined(FSL_FEATURE_SAI_FIFO_COUNT) && (FSL_FEATURE_SAI_FIFO_COUNT > 1)
  231. handle->count = format->watermark;
  232. #else
  233. handle->count = 1U;
  234. #endif /* FSL_FEATURE_SAI_FIFO_COUNT */
  235. }
  236. status_t SAI_TransferSendEDMA(I2S_Type *base, sai_edma_handle_t *handle, sai_transfer_t *xfer)
  237. {
  238. assert(handle && xfer);
  239. edma_transfer_config_t config = {0};
  240. uint32_t destAddr = SAI_TxGetDataRegisterAddress(base, handle->channel);
  241. /* Check if input parameter invalid */
  242. if ((xfer->data == NULL) || (xfer->dataSize == 0U))
  243. {
  244. return kStatus_InvalidArgument;
  245. }
  246. if (handle->saiQueue[handle->queueUser].data)
  247. {
  248. return kStatus_SAI_QueueFull;
  249. }
  250. /* Change the state of handle */
  251. handle->state = kSAI_Busy;
  252. /* Update the queue state */
  253. handle->transferSize[handle->queueUser] = xfer->dataSize;
  254. handle->saiQueue[handle->queueUser].data = xfer->data;
  255. handle->saiQueue[handle->queueUser].dataSize = xfer->dataSize;
  256. handle->queueUser = (handle->queueUser + 1) % SAI_XFER_QUEUE_SIZE;
  257. /* Prepare edma configure */
  258. EDMA_PrepareTransfer(&config, xfer->data, handle->bytesPerFrame, (void *)destAddr, handle->bytesPerFrame,
  259. handle->count * handle->bytesPerFrame, xfer->dataSize, kEDMA_MemoryToPeripheral);
  260. /* Store the initially configured eDMA minor byte transfer count into the SAI handle */
  261. handle->nbytes = handle->count * handle->bytesPerFrame;
  262. EDMA_SubmitTransfer(handle->dmaHandle, &config);
  263. /* Start DMA transfer */
  264. EDMA_StartTransfer(handle->dmaHandle);
  265. /* Enable DMA enable bit */
  266. SAI_TxEnableDMA(base, kSAI_FIFORequestDMAEnable, true);
  267. /* Enable SAI Tx clock */
  268. SAI_TxEnable(base, true);
  269. /* Enable the channel FIFO */
  270. base->TCR3 |= I2S_TCR3_TCE(1U << handle->channel);
  271. return kStatus_Success;
  272. }
  273. status_t SAI_TransferReceiveEDMA(I2S_Type *base, sai_edma_handle_t *handle, sai_transfer_t *xfer)
  274. {
  275. assert(handle && xfer);
  276. edma_transfer_config_t config = {0};
  277. uint32_t srcAddr = SAI_RxGetDataRegisterAddress(base, handle->channel);
  278. /* Check if input parameter invalid */
  279. if ((xfer->data == NULL) || (xfer->dataSize == 0U))
  280. {
  281. return kStatus_InvalidArgument;
  282. }
  283. if (handle->saiQueue[handle->queueUser].data)
  284. {
  285. return kStatus_SAI_QueueFull;
  286. }
  287. /* Change the state of handle */
  288. handle->state = kSAI_Busy;
  289. /* Update queue state */
  290. handle->transferSize[handle->queueUser] = xfer->dataSize;
  291. handle->saiQueue[handle->queueUser].data = xfer->data;
  292. handle->saiQueue[handle->queueUser].dataSize = xfer->dataSize;
  293. handle->queueUser = (handle->queueUser + 1) % SAI_XFER_QUEUE_SIZE;
  294. /* Prepare edma configure */
  295. EDMA_PrepareTransfer(&config, (void *)srcAddr, handle->bytesPerFrame, xfer->data, handle->bytesPerFrame,
  296. handle->count * handle->bytesPerFrame, xfer->dataSize, kEDMA_PeripheralToMemory);
  297. /* Store the initially configured eDMA minor byte transfer count into the SAI handle */
  298. handle->nbytes = handle->count * handle->bytesPerFrame;
  299. EDMA_SubmitTransfer(handle->dmaHandle, &config);
  300. /* Start DMA transfer */
  301. EDMA_StartTransfer(handle->dmaHandle);
  302. /* Enable DMA enable bit */
  303. SAI_RxEnableDMA(base, kSAI_FIFORequestDMAEnable, true);
  304. /* Enable the channel FIFO */
  305. base->RCR3 |= I2S_RCR3_RCE(1U << handle->channel);
  306. /* Enable SAI Rx clock */
  307. SAI_RxEnable(base, true);
  308. return kStatus_Success;
  309. }
  310. void SAI_TransferAbortSendEDMA(I2S_Type *base, sai_edma_handle_t *handle)
  311. {
  312. assert(handle);
  313. /* Disable dma */
  314. EDMA_AbortTransfer(handle->dmaHandle);
  315. /* Disable the channel FIFO */
  316. base->TCR3 &= ~I2S_TCR3_TCE_MASK;
  317. /* Disable DMA enable bit */
  318. SAI_TxEnableDMA(base, kSAI_FIFORequestDMAEnable, false);
  319. /* Disable Tx */
  320. SAI_TxEnable(base, false);
  321. /* Reset the FIFO pointer, at the same time clear all error flags if set */
  322. base->TCSR |= (I2S_TCSR_FR_MASK | I2S_TCSR_SR_MASK);
  323. base->TCSR &= ~I2S_TCSR_SR_MASK;
  324. /* Handle the queue index */
  325. memset(&handle->saiQueue[handle->queueDriver], 0, sizeof(sai_transfer_t));
  326. handle->queueDriver = (handle->queueDriver + 1) % SAI_XFER_QUEUE_SIZE;
  327. /* Set the handle state */
  328. handle->state = kSAI_Idle;
  329. }
  330. void SAI_TransferAbortReceiveEDMA(I2S_Type *base, sai_edma_handle_t *handle)
  331. {
  332. assert(handle);
  333. /* Disable dma */
  334. EDMA_AbortTransfer(handle->dmaHandle);
  335. /* Disable the channel FIFO */
  336. base->RCR3 &= ~I2S_RCR3_RCE_MASK;
  337. /* Disable DMA enable bit */
  338. SAI_RxEnableDMA(base, kSAI_FIFORequestDMAEnable, false);
  339. /* Disable Rx */
  340. SAI_RxEnable(base, false);
  341. /* Reset the FIFO pointer, at the same time clear all error flags if set */
  342. base->RCSR |= (I2S_RCSR_FR_MASK | I2S_RCSR_SR_MASK);
  343. base->RCSR &= ~I2S_RCSR_SR_MASK;
  344. /* Handle the queue index */
  345. memset(&handle->saiQueue[handle->queueDriver], 0, sizeof(sai_transfer_t));
  346. handle->queueDriver = (handle->queueDriver + 1) % SAI_XFER_QUEUE_SIZE;
  347. /* Set the handle state */
  348. handle->state = kSAI_Idle;
  349. }
  350. void SAI_TransferTerminateSendEDMA(I2S_Type *base, sai_edma_handle_t *handle)
  351. {
  352. assert(handle);
  353. /* Abort the current transfer */
  354. SAI_TransferAbortSendEDMA(base, handle);
  355. /* Clear all the internal information */
  356. memset(handle->tcd, 0U, sizeof(handle->tcd));
  357. memset(handle->saiQueue, 0U, sizeof(handle->saiQueue));
  358. memset(handle->transferSize, 0U, sizeof(handle->transferSize));
  359. handle->queueUser = 0U;
  360. handle->queueDriver = 0U;
  361. }
  362. void SAI_TransferTerminateReceiveEDMA(I2S_Type *base, sai_edma_handle_t *handle)
  363. {
  364. assert(handle);
  365. /* Abort the current transfer */
  366. SAI_TransferAbortReceiveEDMA(base, handle);
  367. /* Clear all the internal information */
  368. memset(handle->tcd, 0U, sizeof(handle->tcd));
  369. memset(handle->saiQueue, 0U, sizeof(handle->saiQueue));
  370. memset(handle->transferSize, 0U, sizeof(handle->transferSize));
  371. handle->queueUser = 0U;
  372. handle->queueDriver = 0U;
  373. }
  374. status_t SAI_TransferGetSendCountEDMA(I2S_Type *base, sai_edma_handle_t *handle, size_t *count)
  375. {
  376. assert(handle);
  377. status_t status = kStatus_Success;
  378. if (handle->state != kSAI_Busy)
  379. {
  380. status = kStatus_NoTransferInProgress;
  381. }
  382. else
  383. {
  384. *count = (handle->transferSize[handle->queueDriver] -
  385. (uint32_t)handle->nbytes *
  386. EDMA_GetRemainingMajorLoopCount(handle->dmaHandle->base, handle->dmaHandle->channel));
  387. }
  388. return status;
  389. }
  390. status_t SAI_TransferGetReceiveCountEDMA(I2S_Type *base, sai_edma_handle_t *handle, size_t *count)
  391. {
  392. assert(handle);
  393. status_t status = kStatus_Success;
  394. if (handle->state != kSAI_Busy)
  395. {
  396. status = kStatus_NoTransferInProgress;
  397. }
  398. else
  399. {
  400. *count = (handle->transferSize[handle->queueDriver] -
  401. (uint32_t)handle->nbytes *
  402. EDMA_GetRemainingMajorLoopCount(handle->dmaHandle->base, handle->dmaHandle->channel));
  403. }
  404. return status;
  405. }