/*
DMA driver for memory test
Author: Brighton
Created on 2011-1-11
*/
#include <csl_edma3.h>
#include "mem_test_DMA_drv.h"
#include "KeyStone_common.h"

void EDMA_init_for_mem_test()
{
	int i;

	edma_Init();

	/*Initialize PaRAM for each channel/TC*/
	/*this test use channel 0 with TC0, channel 1 with TC1...*/
	for(i=0; i< CSL_EDMA3_TPCC0_NUM_TC; i++) 	
	{
		gEDMACC0Regs->PARAMSET[i].OPT= 
		CSL_EDMA3_OPT_MAKE(CSL_EDMA3_ITCCH_DIS, 
			CSL_EDMA3_TCCH_DIS, 
			CSL_EDMA3_ITCINT_DIS, 
			CSL_EDMA3_TCINT_EN,
			i,
			CSL_EDMA3_TCC_NORMAL,
			CSL_EDMA3_FIFOWIDTH_NONE, 
			CSL_EDMA3_STATIC_EN, 
			CSL_EDMA3_SYNC_A, 
			CSL_EDMA3_ADDRMODE_INCR, 
			CSL_EDMA3_ADDRMODE_INCR);
		gEDMACC0Regs->PARAMSET[i].SRC_DST_BIDX= 0;
		gEDMACC0Regs->PARAMSET[i].LINK_BCNTRLD= 0xFFFF;
		gEDMACC0Regs->PARAMSET[i].SRC_DST_CIDX= 0;
		gEDMACC0Regs->PARAMSET[i].CCNT= 1;
	}
	for(i=0; i< CSL_EDMA3_TPCC1_NUM_TC; i++) 	
	{
		gEDMACC1Regs->PARAMSET[i].OPT= 
		CSL_EDMA3_OPT_MAKE(CSL_EDMA3_ITCCH_DIS, 
			CSL_EDMA3_TCCH_DIS, 
			CSL_EDMA3_ITCINT_DIS, 
			CSL_EDMA3_TCINT_EN,
			i,
			CSL_EDMA3_TCC_NORMAL,
			CSL_EDMA3_FIFOWIDTH_NONE, 
			CSL_EDMA3_STATIC_EN, 
			CSL_EDMA3_SYNC_A, 
			CSL_EDMA3_ADDRMODE_INCR, 
			CSL_EDMA3_ADDRMODE_INCR);
		gEDMACC1Regs->PARAMSET[i].SRC_DST_BIDX= 0;
		gEDMACC1Regs->PARAMSET[i].LINK_BCNTRLD= 0xFFFF;
		gEDMACC1Regs->PARAMSET[i].SRC_DST_CIDX= 0;
		gEDMACC1Regs->PARAMSET[i].CCNT= 1;
	}
	for(i=0; i< CSL_EDMA3_TPCC2_NUM_TC; i++) 	
	{
		gEDMACC2Regs->PARAMSET[i].OPT= 
		CSL_EDMA3_OPT_MAKE(CSL_EDMA3_ITCCH_DIS, 
			CSL_EDMA3_TCCH_DIS, 
			CSL_EDMA3_ITCINT_DIS, 
			CSL_EDMA3_TCINT_EN,
			i,
			CSL_EDMA3_TCC_NORMAL,
			CSL_EDMA3_FIFOWIDTH_NONE, 
			CSL_EDMA3_STATIC_EN, 
			CSL_EDMA3_SYNC_A, 
			CSL_EDMA3_ADDRMODE_INCR, 
			CSL_EDMA3_ADDRMODE_INCR);
		gEDMACC2Regs->PARAMSET[i].SRC_DST_BIDX= 0;
		gEDMACC2Regs->PARAMSET[i].LINK_BCNTRLD= 0xFFFF;
		gEDMACC2Regs->PARAMSET[i].SRC_DST_CIDX= 0;
		gEDMACC2Regs->PARAMSET[i].CCNT= 1;
	}

}

void EDMA_Copy(unsigned int srcAddr, unsigned int dstAddr, 
	unsigned int byteCount, unsigned int TC_channel)
{
	CSL_TpccRegs*  EDMACCRegs;
	unsigned int uiTC_channel;

	/*use different TC to do test*/
	if(TC_channel<CSL_EDMA3_TPCC0_NUM_TC)
	{
		EDMACCRegs= gEDMACC0Regs;
		uiTC_channel = TC_channel;
	}
	else if(TC_channel<(CSL_EDMA3_TPCC0_NUM_TC+CSL_EDMA3_TPCC1_NUM_TC))
	{
		EDMACCRegs= gEDMACC1Regs;
		uiTC_channel = TC_channel- CSL_EDMA3_TPCC0_NUM_TC;
	}
	else if(TC_channel<EDMA_TC_NUM)
	{
		EDMACCRegs= gEDMACC2Regs;
		uiTC_channel = TC_channel- CSL_EDMA3_TPCC0_NUM_TC- CSL_EDMA3_TPCC1_NUM_TC;
	}
	else
		return;
		
	EDMACCRegs->PARAMSET[uiTC_channel].SRC= (srcAddr);
	EDMACCRegs->PARAMSET[uiTC_channel].A_B_CNT= (1<<16)|(byteCount&0xFFFF);
	EDMACCRegs->PARAMSET[uiTC_channel].DST= (dstAddr);
	
	/*Manually trigger the EDMA*/
	EDMACCRegs->TPCC_ESR= 1<<(uiTC_channel);

	/*wait for completion*/
	while(0==(EDMACCRegs->TPCC_IPR&(1<<(uiTC_channel))));

	/*clear completion flag*/
	EDMACCRegs->TPCC_ICR= 1<<(uiTC_channel);
}

