Path: blob/master/arch/arm/mach-bcmring/csp/dmac/dmacHw_extra.c
10820 views
/*****************************************************************************1* Copyright 2003 - 2008 Broadcom Corporation. All rights reserved.2*3* Unless you and Broadcom execute a separate written software license4* agreement governing use of this software, this software is licensed to you5* under the terms of the GNU General Public License version 2, available at6* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").7*8* Notwithstanding the above, under no circumstances may you combine this9* software in any way with any other Broadcom software provided under a10* license other than the GPL, without Broadcom's express prior written11* consent.12*****************************************************************************/1314/****************************************************************************/15/**16* @file dmacHw_extra.c17*18* @brief Extra Low level DMA controller driver routines19*20* @note21*22* These routines provide basic DMA functionality only.23*/24/****************************************************************************/2526/* ---- Include Files ---------------------------------------------------- */2728#include <csp/stdint.h>29#include <stddef.h>3031#include <csp/dmacHw.h>32#include <mach/csp/dmacHw_reg.h>33#include <mach/csp/dmacHw_priv.h>3435extern dmacHw_CBLK_t dmacHw_gCblk[dmacHw_MAX_CHANNEL_COUNT]; /* Declared in dmacHw.c */3637/* ---- External Function Prototypes ------------------------------------- */3839/* ---- Internal Use Function Prototypes --------------------------------- */40/****************************************************************************/41/**42* @brief Overwrites data length in the descriptor43*44* This function overwrites data length in the descriptor45*46*47* @return void48*49* @note50* This is only used for PCM channel51*/52/****************************************************************************/53void dmacHw_setDataLength(dmacHw_CONFIG_t *pConfig, /* [ IN ] Configuration settings */54void *pDescriptor, /* [ IN ] Descriptor buffer */55size_t dataLen /* [ IN ] Data length in bytes */56);5758/****************************************************************************/59/**60* @brief Helper function to display DMA registers61*62* @return void63*64*65* @note66* None67*/68/****************************************************************************/69static void DisplayRegisterContents(int module, /* [ IN ] DMA Controller unit (0-1) */70int channel, /* [ IN ] DMA Channel (0-7) / -1(all) */71int (*fpPrint) (const char *, ...) /* [ IN ] Callback to the print function */72) {73int chan;7475(*fpPrint) ("Displaying register content \n\n");76(*fpPrint) ("Module %d: Interrupt raw transfer 0x%X\n",77module, (uint32_t) (dmacHw_REG_INT_RAW_TRAN(module)));78(*fpPrint) ("Module %d: Interrupt raw block 0x%X\n",79module, (uint32_t) (dmacHw_REG_INT_RAW_BLOCK(module)));80(*fpPrint) ("Module %d: Interrupt raw src transfer 0x%X\n",81module, (uint32_t) (dmacHw_REG_INT_RAW_STRAN(module)));82(*fpPrint) ("Module %d: Interrupt raw dst transfer 0x%X\n",83module, (uint32_t) (dmacHw_REG_INT_RAW_DTRAN(module)));84(*fpPrint) ("Module %d: Interrupt raw error 0x%X\n",85module, (uint32_t) (dmacHw_REG_INT_RAW_ERROR(module)));86(*fpPrint) ("--------------------------------------------------\n");87(*fpPrint) ("Module %d: Interrupt stat transfer 0x%X\n",88module, (uint32_t) (dmacHw_REG_INT_STAT_TRAN(module)));89(*fpPrint) ("Module %d: Interrupt stat block 0x%X\n",90module, (uint32_t) (dmacHw_REG_INT_STAT_BLOCK(module)));91(*fpPrint) ("Module %d: Interrupt stat src transfer 0x%X\n",92module, (uint32_t) (dmacHw_REG_INT_STAT_STRAN(module)));93(*fpPrint) ("Module %d: Interrupt stat dst transfer 0x%X\n",94module, (uint32_t) (dmacHw_REG_INT_STAT_DTRAN(module)));95(*fpPrint) ("Module %d: Interrupt stat error 0x%X\n",96module, (uint32_t) (dmacHw_REG_INT_STAT_ERROR(module)));97(*fpPrint) ("--------------------------------------------------\n");98(*fpPrint) ("Module %d: Interrupt mask transfer 0x%X\n",99module, (uint32_t) (dmacHw_REG_INT_MASK_TRAN(module)));100(*fpPrint) ("Module %d: Interrupt mask block 0x%X\n",101module, (uint32_t) (dmacHw_REG_INT_MASK_BLOCK(module)));102(*fpPrint) ("Module %d: Interrupt mask src transfer 0x%X\n",103module, (uint32_t) (dmacHw_REG_INT_MASK_STRAN(module)));104(*fpPrint) ("Module %d: Interrupt mask dst transfer 0x%X\n",105module, (uint32_t) (dmacHw_REG_INT_MASK_DTRAN(module)));106(*fpPrint) ("Module %d: Interrupt mask error 0x%X\n",107module, (uint32_t) (dmacHw_REG_INT_MASK_ERROR(module)));108(*fpPrint) ("--------------------------------------------------\n");109(*fpPrint) ("Module %d: Interrupt clear transfer 0x%X\n",110module, (uint32_t) (dmacHw_REG_INT_CLEAR_TRAN(module)));111(*fpPrint) ("Module %d: Interrupt clear block 0x%X\n",112module, (uint32_t) (dmacHw_REG_INT_CLEAR_BLOCK(module)));113(*fpPrint) ("Module %d: Interrupt clear src transfer 0x%X\n",114module, (uint32_t) (dmacHw_REG_INT_CLEAR_STRAN(module)));115(*fpPrint) ("Module %d: Interrupt clear dst transfer 0x%X\n",116module, (uint32_t) (dmacHw_REG_INT_CLEAR_DTRAN(module)));117(*fpPrint) ("Module %d: Interrupt clear error 0x%X\n",118module, (uint32_t) (dmacHw_REG_INT_CLEAR_ERROR(module)));119(*fpPrint) ("--------------------------------------------------\n");120(*fpPrint) ("Module %d: SW source req 0x%X\n",121module, (uint32_t) (dmacHw_REG_SW_HS_SRC_REQ(module)));122(*fpPrint) ("Module %d: SW dest req 0x%X\n",123module, (uint32_t) (dmacHw_REG_SW_HS_DST_REQ(module)));124(*fpPrint) ("Module %d: SW source signal 0x%X\n",125module, (uint32_t) (dmacHw_REG_SW_HS_SRC_SGL_REQ(module)));126(*fpPrint) ("Module %d: SW dest signal 0x%X\n",127module, (uint32_t) (dmacHw_REG_SW_HS_DST_SGL_REQ(module)));128(*fpPrint) ("Module %d: SW source last 0x%X\n",129module, (uint32_t) (dmacHw_REG_SW_HS_SRC_LST_REQ(module)));130(*fpPrint) ("Module %d: SW dest last 0x%X\n",131module, (uint32_t) (dmacHw_REG_SW_HS_DST_LST_REQ(module)));132(*fpPrint) ("--------------------------------------------------\n");133(*fpPrint) ("Module %d: misc config 0x%X\n",134module, (uint32_t) (dmacHw_REG_MISC_CFG(module)));135(*fpPrint) ("Module %d: misc channel enable 0x%X\n",136module, (uint32_t) (dmacHw_REG_MISC_CH_ENABLE(module)));137(*fpPrint) ("Module %d: misc ID 0x%X\n",138module, (uint32_t) (dmacHw_REG_MISC_ID(module)));139(*fpPrint) ("Module %d: misc test 0x%X\n",140module, (uint32_t) (dmacHw_REG_MISC_TEST(module)));141142if (channel == -1) {143for (chan = 0; chan < 8; chan++) {144(*fpPrint)145("--------------------------------------------------\n");146(*fpPrint)147("Module %d: Channel %d Source 0x%X\n",148module, chan,149(uint32_t) (dmacHw_REG_SAR(module, chan)));150(*fpPrint)151("Module %d: Channel %d Destination 0x%X\n",152module, chan,153(uint32_t) (dmacHw_REG_DAR(module, chan)));154(*fpPrint)155("Module %d: Channel %d LLP 0x%X\n",156module, chan,157(uint32_t) (dmacHw_REG_LLP(module, chan)));158(*fpPrint)159("Module %d: Channel %d Control (LO) 0x%X\n",160module, chan,161(uint32_t) (dmacHw_REG_CTL_LO(module, chan)));162(*fpPrint)163("Module %d: Channel %d Control (HI) 0x%X\n",164module, chan,165(uint32_t) (dmacHw_REG_CTL_HI(module, chan)));166(*fpPrint)167("Module %d: Channel %d Source Stats 0x%X\n",168module, chan,169(uint32_t) (dmacHw_REG_SSTAT(module, chan)));170(*fpPrint)171("Module %d: Channel %d Dest Stats 0x%X\n",172module, chan,173(uint32_t) (dmacHw_REG_DSTAT(module, chan)));174(*fpPrint)175("Module %d: Channel %d Source Stats Addr 0x%X\n",176module, chan,177(uint32_t) (dmacHw_REG_SSTATAR(module, chan)));178(*fpPrint)179("Module %d: Channel %d Dest Stats Addr 0x%X\n",180module, chan,181(uint32_t) (dmacHw_REG_DSTATAR(module, chan)));182(*fpPrint)183("Module %d: Channel %d Config (LO) 0x%X\n",184module, chan,185(uint32_t) (dmacHw_REG_CFG_LO(module, chan)));186(*fpPrint)187("Module %d: Channel %d Config (HI) 0x%X\n",188module, chan,189(uint32_t) (dmacHw_REG_CFG_HI(module, chan)));190}191} else {192chan = channel;193(*fpPrint)194("--------------------------------------------------\n");195(*fpPrint)196("Module %d: Channel %d Source 0x%X\n",197module, chan, (uint32_t) (dmacHw_REG_SAR(module, chan)));198(*fpPrint)199("Module %d: Channel %d Destination 0x%X\n",200module, chan, (uint32_t) (dmacHw_REG_DAR(module, chan)));201(*fpPrint)202("Module %d: Channel %d LLP 0x%X\n",203module, chan, (uint32_t) (dmacHw_REG_LLP(module, chan)));204(*fpPrint)205("Module %d: Channel %d Control (LO) 0x%X\n",206module, chan,207(uint32_t) (dmacHw_REG_CTL_LO(module, chan)));208(*fpPrint)209("Module %d: Channel %d Control (HI) 0x%X\n",210module, chan,211(uint32_t) (dmacHw_REG_CTL_HI(module, chan)));212(*fpPrint)213("Module %d: Channel %d Source Stats 0x%X\n",214module, chan, (uint32_t) (dmacHw_REG_SSTAT(module, chan)));215(*fpPrint)216("Module %d: Channel %d Dest Stats 0x%X\n",217module, chan, (uint32_t) (dmacHw_REG_DSTAT(module, chan)));218(*fpPrint)219("Module %d: Channel %d Source Stats Addr 0x%X\n",220module, chan,221(uint32_t) (dmacHw_REG_SSTATAR(module, chan)));222(*fpPrint)223("Module %d: Channel %d Dest Stats Addr 0x%X\n",224module, chan,225(uint32_t) (dmacHw_REG_DSTATAR(module, chan)));226(*fpPrint)227("Module %d: Channel %d Config (LO) 0x%X\n",228module, chan,229(uint32_t) (dmacHw_REG_CFG_LO(module, chan)));230(*fpPrint)231("Module %d: Channel %d Config (HI) 0x%X\n",232module, chan,233(uint32_t) (dmacHw_REG_CFG_HI(module, chan)));234}235}236237/****************************************************************************/238/**239* @brief Helper function to display descriptor ring240*241* @return void242*243*244* @note245* None246*/247/****************************************************************************/248static void DisplayDescRing(void *pDescriptor, /* [ IN ] Descriptor buffer */249int (*fpPrint) (const char *, ...) /* [ IN ] Callback to the print function */250) {251dmacHw_DESC_RING_t *pRing = dmacHw_GET_DESC_RING(pDescriptor);252dmacHw_DESC_t *pStart;253254if (pRing->pHead == NULL) {255return;256}257258pStart = pRing->pHead;259260while ((dmacHw_DESC_t *) pStart->llp != pRing->pHead) {261if (pStart == pRing->pHead) {262(*fpPrint) ("Head\n");263}264if (pStart == pRing->pTail) {265(*fpPrint) ("Tail\n");266}267if (pStart == pRing->pProg) {268(*fpPrint) ("Prog\n");269}270if (pStart == pRing->pEnd) {271(*fpPrint) ("End\n");272}273if (pStart == pRing->pFree) {274(*fpPrint) ("Free\n");275}276(*fpPrint) ("0x%X:\n", (uint32_t) pStart);277(*fpPrint) ("sar 0x%0X\n", pStart->sar);278(*fpPrint) ("dar 0x%0X\n", pStart->dar);279(*fpPrint) ("llp 0x%0X\n", pStart->llp);280(*fpPrint) ("ctl.lo 0x%0X\n", pStart->ctl.lo);281(*fpPrint) ("ctl.hi 0x%0X\n", pStart->ctl.hi);282(*fpPrint) ("sstat 0x%0X\n", pStart->sstat);283(*fpPrint) ("dstat 0x%0X\n", pStart->dstat);284(*fpPrint) ("devCtl 0x%0X\n", pStart->devCtl);285286pStart = (dmacHw_DESC_t *) pStart->llp;287}288if (pStart == pRing->pHead) {289(*fpPrint) ("Head\n");290}291if (pStart == pRing->pTail) {292(*fpPrint) ("Tail\n");293}294if (pStart == pRing->pProg) {295(*fpPrint) ("Prog\n");296}297if (pStart == pRing->pEnd) {298(*fpPrint) ("End\n");299}300if (pStart == pRing->pFree) {301(*fpPrint) ("Free\n");302}303(*fpPrint) ("0x%X:\n", (uint32_t) pStart);304(*fpPrint) ("sar 0x%0X\n", pStart->sar);305(*fpPrint) ("dar 0x%0X\n", pStart->dar);306(*fpPrint) ("llp 0x%0X\n", pStart->llp);307(*fpPrint) ("ctl.lo 0x%0X\n", pStart->ctl.lo);308(*fpPrint) ("ctl.hi 0x%0X\n", pStart->ctl.hi);309(*fpPrint) ("sstat 0x%0X\n", pStart->sstat);310(*fpPrint) ("dstat 0x%0X\n", pStart->dstat);311(*fpPrint) ("devCtl 0x%0X\n", pStart->devCtl);312}313314/****************************************************************************/315/**316* @brief Check if DMA channel is the flow controller317*318* @return 1 : If DMA is a flow controller319* 0 : Peripheral is the flow controller320*321* @note322* None323*/324/****************************************************************************/325static inline int DmaIsFlowController(void *pDescriptor /* [ IN ] Descriptor buffer */326) {327uint32_t ttfc =328(dmacHw_GET_DESC_RING(pDescriptor))->pTail->ctl.329lo & dmacHw_REG_CTL_TTFC_MASK;330331switch (ttfc) {332case dmacHw_REG_CTL_TTFC_MM_DMAC:333case dmacHw_REG_CTL_TTFC_MP_DMAC:334case dmacHw_REG_CTL_TTFC_PM_DMAC:335case dmacHw_REG_CTL_TTFC_PP_DMAC:336return 1;337}338339return 0;340}341342/****************************************************************************/343/**344* @brief Overwrites data length in the descriptor345*346* This function overwrites data length in the descriptor347*348*349* @return void350*351* @note352* This is only used for PCM channel353*/354/****************************************************************************/355void dmacHw_setDataLength(dmacHw_CONFIG_t *pConfig, /* [ IN ] Configuration settings */356void *pDescriptor, /* [ IN ] Descriptor buffer */357size_t dataLen /* [ IN ] Data length in bytes */358) {359dmacHw_DESC_t *pProg;360dmacHw_DESC_t *pHead;361int srcTs = 0;362int srcTrSize = 0;363364pHead = (dmacHw_GET_DESC_RING(pDescriptor))->pHead;365pProg = pHead;366367srcTrSize = dmacHw_GetTrWidthInBytes(pConfig->srcMaxTransactionWidth);368srcTs = dataLen / srcTrSize;369do {370pProg->ctl.hi = srcTs & dmacHw_REG_CTL_BLOCK_TS_MASK;371pProg = (dmacHw_DESC_t *) pProg->llp;372} while (pProg != pHead);373}374375/****************************************************************************/376/**377* @brief Clears the interrupt378*379* This function clears the DMA channel specific interrupt380*381*382* @return void383*384* @note385* Must be called under the context of ISR386*/387/****************************************************************************/388void dmacHw_clearInterrupt(dmacHw_HANDLE_t handle /* [ IN ] DMA Channel handle */389) {390dmacHw_CBLK_t *pCblk = dmacHw_HANDLE_TO_CBLK(handle);391392dmacHw_TRAN_INT_CLEAR(pCblk->module, pCblk->channel);393dmacHw_BLOCK_INT_CLEAR(pCblk->module, pCblk->channel);394dmacHw_ERROR_INT_CLEAR(pCblk->module, pCblk->channel);395}396397/****************************************************************************/398/**399* @brief Returns the cause of channel specific DMA interrupt400*401* This function returns the cause of interrupt402*403* @return Interrupt status, each bit representing a specific type of interrupt404*405* @note406* Should be called under the context of ISR407*/408/****************************************************************************/409dmacHw_INTERRUPT_STATUS_e dmacHw_getInterruptStatus(dmacHw_HANDLE_t handle /* [ IN ] DMA Channel handle */410) {411dmacHw_CBLK_t *pCblk = dmacHw_HANDLE_TO_CBLK(handle);412dmacHw_INTERRUPT_STATUS_e status = dmacHw_INTERRUPT_STATUS_NONE;413414if (dmacHw_REG_INT_STAT_TRAN(pCblk->module) &415((0x00000001 << pCblk->channel))) {416status |= dmacHw_INTERRUPT_STATUS_TRANS;417}418if (dmacHw_REG_INT_STAT_BLOCK(pCblk->module) &419((0x00000001 << pCblk->channel))) {420status |= dmacHw_INTERRUPT_STATUS_BLOCK;421}422if (dmacHw_REG_INT_STAT_ERROR(pCblk->module) &423((0x00000001 << pCblk->channel))) {424status |= dmacHw_INTERRUPT_STATUS_ERROR;425}426427return status;428}429430/****************************************************************************/431/**432* @brief Indentifies a DMA channel causing interrupt433*434* This functions returns a channel causing interrupt of type dmacHw_INTERRUPT_STATUS_e435*436* @return NULL : No channel causing DMA interrupt437* ! NULL : Handle to a channel causing DMA interrupt438* @note439* dmacHw_clearInterrupt() must be called with a valid handle after calling this function440*/441/****************************************************************************/442dmacHw_HANDLE_t dmacHw_getInterruptSource(void)443{444uint32_t i;445446for (i = 0; i < dmaChannelCount_0 + dmaChannelCount_1; i++) {447if ((dmacHw_REG_INT_STAT_TRAN(dmacHw_gCblk[i].module) &448((0x00000001 << dmacHw_gCblk[i].channel)))449|| (dmacHw_REG_INT_STAT_BLOCK(dmacHw_gCblk[i].module) &450((0x00000001 << dmacHw_gCblk[i].channel)))451|| (dmacHw_REG_INT_STAT_ERROR(dmacHw_gCblk[i].module) &452((0x00000001 << dmacHw_gCblk[i].channel)))453) {454return dmacHw_CBLK_TO_HANDLE(&dmacHw_gCblk[i]);455}456}457return dmacHw_CBLK_TO_HANDLE(NULL);458}459460/****************************************************************************/461/**462* @brief Estimates number of descriptor needed to perform certain DMA transfer463*464*465* @return On failure : -1466* On success : Number of descriptor count467*468*469*/470/****************************************************************************/471int dmacHw_calculateDescriptorCount(dmacHw_CONFIG_t *pConfig, /* [ IN ] Configuration settings */472void *pSrcAddr, /* [ IN ] Source (Peripheral/Memory) address */473void *pDstAddr, /* [ IN ] Destination (Peripheral/Memory) address */474size_t dataLen /* [ IN ] Data length in bytes */475) {476int srcTs = 0;477int oddSize = 0;478int descCount = 0;479int dstTrSize = 0;480int srcTrSize = 0;481uint32_t maxBlockSize = dmacHw_MAX_BLOCKSIZE;482dmacHw_TRANSACTION_WIDTH_e dstTrWidth;483dmacHw_TRANSACTION_WIDTH_e srcTrWidth;484485dstTrSize = dmacHw_GetTrWidthInBytes(pConfig->dstMaxTransactionWidth);486srcTrSize = dmacHw_GetTrWidthInBytes(pConfig->srcMaxTransactionWidth);487488/* Skip Tx if buffer is NULL or length is unknown */489if ((pSrcAddr == NULL) || (pDstAddr == NULL) || (dataLen == 0)) {490/* Do not initiate transfer */491return -1;492}493494/* Ensure scatter and gather are transaction aligned */495if (pConfig->srcGatherWidth % srcTrSize496|| pConfig->dstScatterWidth % dstTrSize) {497return -1;498}499500/*501Background 1: DMAC can not perform DMA if source and destination addresses are502not properly aligned with the channel's transaction width. So, for successful503DMA transfer, transaction width must be set according to the alignment of the504source and destination address.505*/506507/* Adjust destination transaction width if destination address is not aligned properly */508dstTrWidth = pConfig->dstMaxTransactionWidth;509while (dmacHw_ADDRESS_MASK(dstTrSize) & (uint32_t) pDstAddr) {510dstTrWidth = dmacHw_GetNextTrWidth(dstTrWidth);511dstTrSize = dmacHw_GetTrWidthInBytes(dstTrWidth);512}513514/* Adjust source transaction width if source address is not aligned properly */515srcTrWidth = pConfig->srcMaxTransactionWidth;516while (dmacHw_ADDRESS_MASK(srcTrSize) & (uint32_t) pSrcAddr) {517srcTrWidth = dmacHw_GetNextTrWidth(srcTrWidth);518srcTrSize = dmacHw_GetTrWidthInBytes(srcTrWidth);519}520521/* Find the maximum transaction per descriptor */522if (pConfig->maxDataPerBlock523&& ((pConfig->maxDataPerBlock / srcTrSize) <524dmacHw_MAX_BLOCKSIZE)) {525maxBlockSize = pConfig->maxDataPerBlock / srcTrSize;526}527528/* Find number of source transactions needed to complete the DMA transfer */529srcTs = dataLen / srcTrSize;530/* Find the odd number of bytes that need to be transferred as single byte transaction width */531if (srcTs && (dstTrSize > srcTrSize)) {532oddSize = dataLen % dstTrSize;533/* Adjust source transaction count due to "oddSize" */534srcTs = srcTs - (oddSize / srcTrSize);535} else {536oddSize = dataLen % srcTrSize;537}538/* Adjust "descCount" due to "oddSize" */539if (oddSize) {540descCount++;541}542543/* Find the number of descriptor needed for total "srcTs" */544if (srcTs) {545descCount += ((srcTs - 1) / maxBlockSize) + 1;546}547548return descCount;549}550551/****************************************************************************/552/**553* @brief Check the existence of pending descriptor554*555* This function confirmes if there is any pending descriptor in the chain556* to program the channel557*558* @return 1 : Channel need to be programmed with pending descriptor559* 0 : No more pending descriptor to programe the channel560*561* @note562* - This function should be called from ISR in case there are pending563* descriptor to program the channel.564*565* Example:566*567* dmac_isr ()568* {569* ...570* if (dmacHw_descriptorPending (handle))571* {572* dmacHw_initiateTransfer (handle);573* }574* }575*576*/577/****************************************************************************/578uint32_t dmacHw_descriptorPending(dmacHw_HANDLE_t handle, /* [ IN ] DMA Channel handle */579void *pDescriptor /* [ IN ] Descriptor buffer */580) {581dmacHw_CBLK_t *pCblk = dmacHw_HANDLE_TO_CBLK(handle);582dmacHw_DESC_RING_t *pRing = dmacHw_GET_DESC_RING(pDescriptor);583584/* Make sure channel is not busy */585if (!CHANNEL_BUSY(pCblk->module, pCblk->channel)) {586/* Check if pEnd is not processed */587if (pRing->pEnd) {588/* Something left for processing */589return 1;590}591}592return 0;593}594595/****************************************************************************/596/**597* @brief Program channel register to stop transfer598*599* Ensures the channel is not doing any transfer after calling this function600*601* @return void602*603*/604/****************************************************************************/605void dmacHw_stopTransfer(dmacHw_HANDLE_t handle /* [ IN ] DMA Channel handle */606) {607dmacHw_CBLK_t *pCblk;608609pCblk = dmacHw_HANDLE_TO_CBLK(handle);610611/* Stop the channel */612dmacHw_DMA_STOP(pCblk->module, pCblk->channel);613}614615/****************************************************************************/616/**617* @brief Deallocates source or destination memory, allocated618*619* This function can be called to deallocate data memory that was DMAed successfully620*621* @return On failure : -1622* On success : Number of buffer freed623*624* @note625* This function will be called ONLY, when source OR destination address is pointing626* to dynamic memory627*/628/****************************************************************************/629int dmacHw_freeMem(dmacHw_CONFIG_t *pConfig, /* [ IN ] Configuration settings */630void *pDescriptor, /* [ IN ] Descriptor buffer */631void (*fpFree) (void *) /* [ IN ] Function pointer to free data memory */632) {633dmacHw_DESC_RING_t *pRing = dmacHw_GET_DESC_RING(pDescriptor);634uint32_t count = 0;635636if (fpFree == NULL) {637return -1;638}639640while ((pRing->pFree != pRing->pTail)641&& (pRing->pFree->ctl.lo & dmacHw_DESC_FREE)) {642if (pRing->pFree->devCtl == dmacHw_FREE_USER_MEMORY) {643/* Identify, which memory to free */644if (dmacHw_DST_IS_MEMORY(pConfig->transferType)) {645(*fpFree) ((void *)pRing->pFree->dar);646} else {647/* Destination was a peripheral */648(*fpFree) ((void *)pRing->pFree->sar);649}650/* Unmark user memory to indicate it is freed */651pRing->pFree->devCtl = ~dmacHw_FREE_USER_MEMORY;652}653dmacHw_NEXT_DESC(pRing, pFree);654655count++;656}657658return count;659}660661/****************************************************************************/662/**663* @brief Prepares descriptor ring, when source peripheral working as a flow controller664*665* This function will update the discriptor ring by allocating buffers, when source peripheral666* has to work as a flow controller to transfer data from:667* - Peripheral to memory.668*669* @return On failure : -1670* On success : Number of descriptor updated671*672*673* @note674* Channel must be configured for peripheral to memory transfer675*676*/677/****************************************************************************/678int dmacHw_setVariableDataDescriptor(dmacHw_HANDLE_t handle, /* [ IN ] DMA Channel handle */679dmacHw_CONFIG_t *pConfig, /* [ IN ] Configuration settings */680void *pDescriptor, /* [ IN ] Descriptor buffer */681uint32_t srcAddr, /* [ IN ] Source peripheral address */682void *(*fpAlloc) (int len), /* [ IN ] Function pointer that provides destination memory */683int len, /* [ IN ] Number of bytes "fpAlloc" will allocate for destination */684int num /* [ IN ] Number of descriptor to set */685) {686dmacHw_CBLK_t *pCblk = dmacHw_HANDLE_TO_CBLK(handle);687dmacHw_DESC_t *pProg = NULL;688dmacHw_DESC_t *pLast = NULL;689dmacHw_DESC_RING_t *pRing = dmacHw_GET_DESC_RING(pDescriptor);690uint32_t dstAddr;691uint32_t controlParam;692int i;693694dmacHw_ASSERT(pConfig->transferType ==695dmacHw_TRANSFER_TYPE_PERIPHERAL_TO_MEM);696697if (num > pRing->num) {698return -1;699}700701pLast = pRing->pEnd; /* Last descriptor updated */702pProg = pRing->pHead; /* First descriptor in the new list */703704controlParam = pConfig->srcUpdate |705pConfig->dstUpdate |706pConfig->srcMaxTransactionWidth |707pConfig->dstMaxTransactionWidth |708pConfig->srcMasterInterface |709pConfig->dstMasterInterface |710pConfig->srcMaxBurstWidth |711pConfig->dstMaxBurstWidth |712dmacHw_REG_CTL_TTFC_PM_PERI |713dmacHw_REG_CTL_LLP_DST_EN |714dmacHw_REG_CTL_LLP_SRC_EN | dmacHw_REG_CTL_INT_EN;715716for (i = 0; i < num; i++) {717/* Allocate Rx buffer only for idle descriptor */718if (((pRing->pHead->ctl.hi & dmacHw_DESC_FREE) == 0) ||719((dmacHw_DESC_t *) pRing->pHead->llp == pRing->pTail)720) {721/* Rx descriptor is not idle */722break;723}724/* Set source address */725pRing->pHead->sar = srcAddr;726if (fpAlloc) {727/* Allocate memory for buffer in descriptor */728dstAddr = (uint32_t) (*fpAlloc) (len);729/* Check the destination address */730if (dstAddr == 0) {731if (i == 0) {732/* Not a single descriptor is available */733return -1;734}735break;736}737/* Set destination address */738pRing->pHead->dar = dstAddr;739}740/* Set control information */741pRing->pHead->ctl.lo = controlParam;742/* Use "devCtl" to mark the memory that need to be freed later */743pRing->pHead->devCtl = dmacHw_FREE_USER_MEMORY;744/* Descriptor is now owned by the channel */745pRing->pHead->ctl.hi = 0;746/* Remember the descriptor last updated */747pRing->pEnd = pRing->pHead;748/* Update next descriptor */749dmacHw_NEXT_DESC(pRing, pHead);750}751752/* Mark the end of the list */753pRing->pEnd->ctl.lo &=754~(dmacHw_REG_CTL_LLP_DST_EN | dmacHw_REG_CTL_LLP_SRC_EN);755/* Connect the list */756if (pLast != pProg) {757pLast->ctl.lo |=758dmacHw_REG_CTL_LLP_DST_EN | dmacHw_REG_CTL_LLP_SRC_EN;759}760/* Mark the descriptors are updated */761pCblk->descUpdated = 1;762if (!pCblk->varDataStarted) {763/* LLP must be pointing to the first descriptor */764dmacHw_SET_LLP(pCblk->module, pCblk->channel,765(uint32_t) pProg - pRing->virt2PhyOffset);766/* Channel, handling variable data started */767pCblk->varDataStarted = 1;768}769770return i;771}772773/****************************************************************************/774/**775* @brief Read data DMAed to memory776*777* This function will read data that has been DMAed to memory while transferring from:778* - Memory to memory779* - Peripheral to memory780*781* @param handle -782* @param ppBbuf -783* @param pLen -784*785* @return 0 - No more data is available to read786* 1 - More data might be available to read787*788*/789/****************************************************************************/790int dmacHw_readTransferredData(dmacHw_HANDLE_t handle, /* [ IN ] DMA Channel handle */791dmacHw_CONFIG_t *pConfig, /* [ IN ] Configuration settings */792void *pDescriptor, /* [ IN ] Descriptor buffer */793void **ppBbuf, /* [ OUT ] Data received */794size_t *pLlen /* [ OUT ] Length of the data received */795) {796dmacHw_DESC_RING_t *pRing = dmacHw_GET_DESC_RING(pDescriptor);797798(void)handle;799800if (pConfig->transferMode != dmacHw_TRANSFER_MODE_CONTINUOUS) {801if (((pRing->pTail->ctl.hi & dmacHw_DESC_FREE) == 0) ||802(pRing->pTail == pRing->pHead)803) {804/* No receive data available */805*ppBbuf = (char *)NULL;806*pLlen = 0;807808return 0;809}810}811812/* Return read buffer and length */813*ppBbuf = (char *)pRing->pTail->dar;814815/* Extract length of the received data */816if (DmaIsFlowController(pDescriptor)) {817uint32_t srcTrSize = 0;818819switch (pRing->pTail->ctl.lo & dmacHw_REG_CTL_SRC_TR_WIDTH_MASK) {820case dmacHw_REG_CTL_SRC_TR_WIDTH_8:821srcTrSize = 1;822break;823case dmacHw_REG_CTL_SRC_TR_WIDTH_16:824srcTrSize = 2;825break;826case dmacHw_REG_CTL_SRC_TR_WIDTH_32:827srcTrSize = 4;828break;829case dmacHw_REG_CTL_SRC_TR_WIDTH_64:830srcTrSize = 8;831break;832default:833dmacHw_ASSERT(0);834}835/* Calculate length from the block size */836*pLlen =837(pRing->pTail->ctl.hi & dmacHw_REG_CTL_BLOCK_TS_MASK) *838srcTrSize;839} else {840/* Extract length from the source peripheral */841*pLlen = pRing->pTail->sstat;842}843844/* Advance tail to next descriptor */845dmacHw_NEXT_DESC(pRing, pTail);846847return 1;848}849850/****************************************************************************/851/**852* @brief Set descriptor carrying control information853*854* This function will be used to send specific control information to the device855* using the DMA channel856*857*858* @return -1 - On failure859* 0 - On success860*861* @note862* None863*/864/****************************************************************************/865int dmacHw_setControlDescriptor(dmacHw_CONFIG_t *pConfig, /* [ IN ] Configuration settings */866void *pDescriptor, /* [ IN ] Descriptor buffer */867uint32_t ctlAddress, /* [ IN ] Address of the device control register */868uint32_t control /* [ IN ] Device control information */869) {870dmacHw_DESC_RING_t *pRing = dmacHw_GET_DESC_RING(pDescriptor);871872if (ctlAddress == 0) {873return -1;874}875876/* Check the availability of descriptors in the ring */877if ((pRing->pHead->ctl.hi & dmacHw_DESC_FREE) == 0) {878return -1;879}880/* Set control information */881pRing->pHead->devCtl = control;882/* Set source and destination address */883pRing->pHead->sar = (uint32_t) &pRing->pHead->devCtl;884pRing->pHead->dar = ctlAddress;885/* Set control parameters */886if (pConfig->flowControler == dmacHw_FLOW_CONTROL_DMA) {887pRing->pHead->ctl.lo = pConfig->transferType |888dmacHw_SRC_ADDRESS_UPDATE_MODE_INC |889dmacHw_DST_ADDRESS_UPDATE_MODE_INC |890dmacHw_SRC_TRANSACTION_WIDTH_32 |891pConfig->dstMaxTransactionWidth |892dmacHw_SRC_BURST_WIDTH_0 |893dmacHw_DST_BURST_WIDTH_0 |894pConfig->srcMasterInterface |895pConfig->dstMasterInterface | dmacHw_REG_CTL_INT_EN;896} else {897uint32_t transferType = 0;898switch (pConfig->transferType) {899case dmacHw_TRANSFER_TYPE_PERIPHERAL_TO_MEM:900transferType = dmacHw_REG_CTL_TTFC_PM_PERI;901break;902case dmacHw_TRANSFER_TYPE_MEM_TO_PERIPHERAL:903transferType = dmacHw_REG_CTL_TTFC_MP_PERI;904break;905default:906dmacHw_ASSERT(0);907}908pRing->pHead->ctl.lo = transferType |909dmacHw_SRC_ADDRESS_UPDATE_MODE_INC |910dmacHw_DST_ADDRESS_UPDATE_MODE_INC |911dmacHw_SRC_TRANSACTION_WIDTH_32 |912pConfig->dstMaxTransactionWidth |913dmacHw_SRC_BURST_WIDTH_0 |914dmacHw_DST_BURST_WIDTH_0 |915pConfig->srcMasterInterface |916pConfig->dstMasterInterface |917pConfig->flowControler | dmacHw_REG_CTL_INT_EN;918}919920/* Set block transaction size to one 32 bit transaction */921pRing->pHead->ctl.hi = dmacHw_REG_CTL_BLOCK_TS_MASK & 1;922923/* Remember the descriptor to initialize the registers */924if (pRing->pProg == dmacHw_DESC_INIT) {925pRing->pProg = pRing->pHead;926}927pRing->pEnd = pRing->pHead;928929/* Advance the descriptor */930dmacHw_NEXT_DESC(pRing, pHead);931932/* Update Tail pointer if destination is a peripheral */933if (!dmacHw_DST_IS_MEMORY(pConfig->transferType)) {934pRing->pTail = pRing->pHead;935}936return 0;937}938939/****************************************************************************/940/**941* @brief Sets channel specific user data942*943* This function associates user data to a specific DMA channel944*945*/946/****************************************************************************/947void dmacHw_setChannelUserData(dmacHw_HANDLE_t handle, /* [ IN ] DMA Channel handle */948void *userData /* [ IN ] User data */949) {950dmacHw_CBLK_t *pCblk = dmacHw_HANDLE_TO_CBLK(handle);951952pCblk->userData = userData;953}954955/****************************************************************************/956/**957* @brief Gets channel specific user data958*959* This function returns user data specific to a DMA channel960*961* @return user data962*/963/****************************************************************************/964void *dmacHw_getChannelUserData(dmacHw_HANDLE_t handle /* [ IN ] DMA Channel handle */965) {966dmacHw_CBLK_t *pCblk = dmacHw_HANDLE_TO_CBLK(handle);967968return pCblk->userData;969}970971/****************************************************************************/972/**973* @brief Resets descriptor control information974*975* @return void976*/977/****************************************************************************/978void dmacHw_resetDescriptorControl(void *pDescriptor /* [ IN ] Descriptor buffer */979) {980int i;981dmacHw_DESC_RING_t *pRing;982dmacHw_DESC_t *pDesc;983984pRing = dmacHw_GET_DESC_RING(pDescriptor);985pDesc = pRing->pHead;986987for (i = 0; i < pRing->num; i++) {988/* Mark descriptor is ready to use */989pDesc->ctl.hi = dmacHw_DESC_FREE;990/* Look into next link list item */991pDesc++;992}993pRing->pFree = pRing->pTail = pRing->pEnd = pRing->pHead;994pRing->pProg = dmacHw_DESC_INIT;995}996997/****************************************************************************/998/**999* @brief Displays channel specific registers and other control parameters1000*1001* @return void1002*1003*1004* @note1005* None1006*/1007/****************************************************************************/1008void dmacHw_printDebugInfo(dmacHw_HANDLE_t handle, /* [ IN ] DMA Channel handle */1009void *pDescriptor, /* [ IN ] Descriptor buffer */1010int (*fpPrint) (const char *, ...) /* [ IN ] Print callback function */1011) {1012dmacHw_CBLK_t *pCblk = dmacHw_HANDLE_TO_CBLK(handle);10131014DisplayRegisterContents(pCblk->module, pCblk->channel, fpPrint);1015DisplayDescRing(pDescriptor, fpPrint);1016}101710181019