/*  ===========================================================================
 *   Copyright (c) Texas Instruments Inc 2002, 2003, 2004, 2005
 *
 *   Use of this software is controlled by the terms and conditions found in 
 *   the license agreement under which this software has been supplied.
 *   ==========================================================================
 */

/** @file csl_dmaxGetHwSetup.c
 *
 *    @brief    File for functional layer of CSL API @a CSL_dmaxGetHwSetup()
 *
 *  Description
 *    - The @a CSL_dmaxGetHwSetup() function definition & it's associated
 *      functions
 *
 *  Modification 1
 *    - Modified on: 11/March/2005
 *    - Reason: created the sources
 * 
 *    @author asr.
 */

#include <csl_dmax.h>


/** ===========================================================================
 *   @n@b CSL_dmaxGetHwSetup
 *
 *   @b Description
 *   @n It retrives the hardware setup parameters of the DMAX
 *      specified by the given handle.
 *
 *   @b Arguments
 *   @verbatim
 *
 *          hDmax       Handle to the DMAX  
 *          setup       Pointer to the harware setup structure
 *
 *   @endverbatim
 *
 *   <b> Return Value </b>  CSL_Status
 *   @li                    CSL_SOK             - Retrieving the hardware setup
 *                                                parameters is successful
 *   @li                    CSL_ESYS_BADHANDLE  - The handle is passed is
 *                                                invalid
 *   @li                    CSL_ESYS_INVPARAMS  - Invalid parameter
 *
 *   <b> Pre Condition </b>
 *   @n  dmax must have opened properly
 *
 *   <b> Post Condition </b>
 *   @n  The hardware setup structure is populated with the hardware setup
 *       parameters
 *
 *   @b Modifies
 *   @n setup variable
 *
 *   @b Example
 *   @verbatim
            CSL_DmaxHandle   hDmax;
            CSL_DmaxHwSetup  hwSetup;

            ...

            status = CSL_dmaxGetHwSetup(hDmax, &hwSetup);

            ...

     @endverbatim
 * ===========================================================================
 */
#pragma CODE_SECTION (CSL_dmaxGetHwSetup, ".text:csl_section:dmax");

CSL_Status CSL_dmaxGetHwSetup (
    CSL_DmaxHandle 	hDmax,
    CSL_DmaxHwSetup *setup
)
{
    Uint16                   etype;
    Uint16                   flag;
    Uint32                   mask;
    Uint32                   eventCtl;
    Uint8                   *tmpPtr;
    CSL_DmaxParameterEntry  *pEntry;
    CSL_DmaxGPTransferEventSetup *gpXfrEvent;
    CSL_DmaxFifoTransferEventSetup *fifoXfrEvent;
    CSL_DmaxCpuintEventSetup *cpuEvent;
    CSL_Status               status = CSL_SOK;

    if (hDmax == NULL)
        return CSL_ESYS_BADHANDLE;

    if (setup == NULL)
        return CSL_ESYS_INVPARAMS;

    setup->polarity = hDmax->regs->DEPR;
    flag = hDmax->eventUid & 0xFFFF;
    mask = 1u << flag;
    if (hDmax->regs->DEHPR & mask) {
        setup->priority = hDmax->regs->DEHPR;
        tmpPtr = (Uint8 *) (&(hDmax->regs->HiMaxParam));
    }
    else {
        setup->priority = hDmax->regs->DELPR;
        tmpPtr = (Uint8 *) (&(hDmax->regs->LoMaxParam));
    }

    if (*(hDmax->hiTableEventEntryPtr) != 0) {
        eventCtl = *(hDmax->hiTableEventEntryPtr);
    }
    else {
        eventCtl = *(hDmax->loTableEventEntryPtr);
    }

    etype = (eventCtl & CSL_DMAX_EVENT0_ETYPE_MASK)
        >> CSL_DMAX_EVENT0_ETYPE_SHIFT;

    switch (etype) {
	    case CSL_DMAX_EVENT0_ETYPE_GENERALXFR:
	        {
	            gpXfrEvent = (CSL_DmaxGPTransferEventSetup *) setup->eventSetup;
	            gpXfrEvent->qtsl = CSL_FEXT (eventCtl, DMAX_EVENT0_QTSL);
	            gpXfrEvent->sync = CSL_FEXT (eventCtl, DMAX_EVENT0_SYNC);
	            gpXfrEvent->tcc = CSL_FEXT (eventCtl, DMAX_EVENT0_TCC);
	            gpXfrEvent->atcint = CSL_FEXT (eventCtl, DMAX_EVENT0_ATCINT);
	            gpXfrEvent->tcint = CSL_FEXT (eventCtl, DMAX_EVENT0_TCINT);
	            gpXfrEvent->rload = CSL_FEXT (eventCtl, DMAX_EVENT0_RLOAD);
	            gpXfrEvent->cc = CSL_FEXT (eventCtl, DMAX_EVENT0_CC);
	            gpXfrEvent->esize = CSL_FEXT (eventCtl, DMAX_EVENT0_ESIZE);
	            gpXfrEvent->pte = CSL_FEXT (eventCtl, DMAX_EVENT0_PTE);
	            gpXfrEvent->etype = CSL_FEXT (eventCtl, DMAX_EVENT0_ETYPE);
	            break;
	        }
	    case CSL_DMAX_EVENT0_ETYPE_FIFOWRITE:
	    case CSL_DMAX_EVENT0_ETYPE_FIFOREAD:
	        {
	            fifoXfrEvent = (CSL_DmaxFifoTransferEventSetup *) 
	            										setup->eventSetup;
	
	            fifoXfrEvent->qtsl = CSL_FEXT (eventCtl, DMAX_EVENT0_QTSL);
	            fifoXfrEvent->sync = CSL_FEXT (eventCtl, DMAX_EVENT0_SYNC);
	            fifoXfrEvent->tcc = CSL_FEXT (eventCtl, DMAX_EVENT0_TCC);
	            fifoXfrEvent->atcint = CSL_FEXT (eventCtl, DMAX_EVENT0_ATCINT);
	            fifoXfrEvent->tcint = CSL_FEXT (eventCtl, DMAX_EVENT0_TCINT);
	            fifoXfrEvent->rload = CSL_FEXT (eventCtl, DMAX_EVENT0_RLOAD);
	            fifoXfrEvent->pte = CSL_FEXT (eventCtl, DMAX_EVENT0_PTE);
	            fifoXfrEvent->etype = CSL_FEXT (eventCtl, DMAX_EVENT0_ETYPE);
	            break;
	        }
	    case CSL_DMAX_EVENT30_ETYPE_CPUINT:
	        {
	            cpuEvent = (CSL_DmaxCpuintEventSetup *) setup->eventSetup;
	            cpuEvent->cpuInt = CSL_FEXT (eventCtl, DMAX_EVENT0_INT);
	            cpuEvent->etype = CSL_FEXT (eventCtl, DMAX_EVENT0_ETYPE);
	            break;
	        }
	    default:
	        {
	            status = CSL_EDMAX_BAD_ETYPE;
	        }
    }

    tmpPtr += (hDmax->paramPtr) * CSL_DMAX_WORD_SIZE;
    pEntry = (CSL_DmaxParameterEntry *) tmpPtr;

    if (etype == CSL_DMAX_EVENT0_ETYPE_GENERALXFR) {
        CSL_dmaxGetHwSetupGenXFRParamEntry (pEntry, gpXfrEvent->cc,
            gpXfrEvent->paramSetup);
    }
    if ((etype == CSL_DMAX_EVENT0_ETYPE_FIFOREAD)
        || (etype == CSL_DMAX_EVENT0_ETYPE_FIFOWRITE)) {
        CSL_dmaxGetHwSetupFifoXFRParamEntry (pEntry, fifoXfrEvent->paramSetup);
    }
    else {
        status = CSL_ESYS_BADHANDLE;
    }
    return status;
}
