/******************************************************************************

  Copyright (C), 2001-2012, Texas Instrument.

 ******************************************************************************
  File Name     : Robust_MNav_Init.c
  Version       : Initial Draft
  Author        : Brighton Feng
  Created       : 2012/10/28
  Last Modified :
  Description   : Initialization of Multicore Navigator on KeyStone DSP
                  for MPU test
  Function List :
              PktDMA_init
              QMSS_init
  History       :
  1.Date        : 2012/10/28
    Author      : Brighton Feng
    Modification: Created file

******************************************************************************/

#include <stdio.h>
#include <string.h>
#include <cslr_device.h>
#include <csl_qm_queue.h>
#include "Robust_MNav_init.h"

/*----------------------------------------------*
 * external variables                           *
 *----------------------------------------------*/

/*----------------------------------------------*
 * external routine prototypes                  *
 *----------------------------------------------*/

/*----------------------------------------------*
 * internal routine prototypes                  *
 *----------------------------------------------*/

/*----------------------------------------------*
 * project-wide global variables                *
 *----------------------------------------------*/

/*----------------------------------------------*
 * module-wide global variables                 *
 *----------------------------------------------*/
#pragma DATA_SECTION(hostDescriptor_SL2,"QMSS_Data.Descriptor_SL2")
#pragma DATA_ALIGN (hostDescriptor_SL2, 128)
Uint8 hostDescriptor_SL2[SL2_HOST_DESCRIPTOR_NUM][HOST_DESCRIPTOR_SIZE];

#pragma DATA_SECTION(packetBuffer_SL2_Size0,"PacketData.buffer_SL2")
#pragma DATA_SECTION(packetBuffer_SL2_Size1,"PacketData.buffer_SL2")
#pragma DATA_SECTION(packetBuffer_SL2_Size2,"PacketData.buffer_SL2")
#pragma DATA_SECTION(packetBuffer_SL2_Size3,"PacketData.buffer_SL2")
Uint8 packetBuffer_SL2_Size0[SL2_SIZE0_PKT_BUF_NUM][SL2_PACKET_BUFFER_SIZE0];
Uint8 packetBuffer_SL2_Size1[SL2_SIZE1_PKT_BUF_NUM][SL2_PACKET_BUFFER_SIZE1];
Uint8 packetBuffer_SL2_Size2[SL2_SIZE2_PKT_BUF_NUM][SL2_PACKET_BUFFER_SIZE2];
Uint8 packetBuffer_SL2_Size3[SL2_SIZE3_PKT_BUF_NUM][SL2_PACKET_BUFFER_SIZE3];

#pragma DATA_SECTION(monoDescriptor_SL2,"QMSS_Data.Descriptor_SL2")
#pragma DATA_ALIGN (monoDescriptor_SL2, 128)
Uint8 monoDescriptor_SL2[SL2_MONO_DESCRIPTOR_NUM][L2_MONO_DESCRIPTOR_SIZE];

/* descriptor memory region configuration.
descriptor Base address must be specified in ascending order,
no overlap is allowed between regions*/
Qmss_DescMemRegionCfg descMemRegionsCfg[]=
{   /*The base address of descriptor region     Size of each descriptor     Number of descriptors 
                                                16*n,1<=n<=8192             2^(5+n), 0<=n<=15*/
    {(Uint32)hostDescriptor_SL2,                HOST_DESCRIPTOR_SIZE,       SL2_HOST_DESC_LINK_ENTRY_NUM},/*host descriptor in SL2*/
    {(Uint32)monoDescriptor_SL2,                L2_MONO_DESCRIPTOR_SIZE,    SL2_MONO_DESC_LINK_ENTRY_NUM}/*Monolithic descriptor in SL2*/
};

FreeHostQueueCfg freeHostQueueCfgTable[]=
{
    /*free queue number*/       /*address of first descriptor*/                                         /*size of descriptor*/  /*number of descriptors*/   /*address of first buffer*/                     /*size of each buffer*/ 
    {SL2_HOST_SIZE0_FDQ,        (Uint32)&hostDescriptor_SL2[0][0],                                      HOST_DESCRIPTOR_SIZE,   SL2_SIZE0_PKT_BUF_NUM,      (Uint32)packetBuffer_SL2_Size0,                 SL2_PACKET_BUFFER_SIZE0},/*SL2 Size0 host Free Descriptor Queue*/
    {SL2_HOST_SIZE1_FDQ,        (Uint32)&hostDescriptor_SL2[SL2_SIZE1_DESC_START_IDX][0],               HOST_DESCRIPTOR_SIZE,   SL2_SIZE1_PKT_BUF_NUM,      (Uint32)packetBuffer_SL2_Size1,                 SL2_PACKET_BUFFER_SIZE1},/*SL2 Size1 host Free Descriptor Queue*/
    {SL2_HOST_SIZE2_FDQ,        (Uint32)&hostDescriptor_SL2[SL2_SIZE2_DESC_START_IDX][0],               HOST_DESCRIPTOR_SIZE,   SL2_SIZE2_PKT_BUF_NUM,      (Uint32)packetBuffer_SL2_Size2,                 SL2_PACKET_BUFFER_SIZE2},/*SL2 Size2 host Free Descriptor Queue*/
    {SL2_HOST_SIZE3_FDQ,        (Uint32)&hostDescriptor_SL2[SL2_SIZE3_DESC_START_IDX][0],               HOST_DESCRIPTOR_SIZE,   SL2_SIZE3_PKT_BUF_NUM,      (Uint32)packetBuffer_SL2_Size3,                 SL2_PACKET_BUFFER_SIZE3},/*SL2 Size3 host Free Descriptor Queue*/
    {SL2_NO_BUF_HOST_FDQ,       (Uint32)&hostDescriptor_SL2[SL2_NO_BUF_DESC_START_IDX][0],              HOST_DESCRIPTOR_SIZE,   SL2_NO_BUF_DESCRIPTOR_NUM,  0,                                              0}         /*SL2 no buffer host Free Descriptor Queue*/
};

FreeMonoQueueCfg freeMonoQueueCfgTable[]=
{
    /*free queue number*/       /*address of first descriptor*/                                 /*size of descriptor*/      /*number of descriptors*/
    {SL2_MONOLITHIC_FDQ,        (Uint32)monoDescriptor_SL2,                                     L2_MONO_DESCRIPTOR_SIZE,    SL2_MONO_DESCRIPTOR_NUM}/*SL2 monolithic Free Descriptor Queue*/
};

PktDma_RxFlowCfg flowCfgTable[]=
{
    /*dest_qnum*/   /*sop_offset*/  /*ps_loc*/  /*desc_type*/   /*error retry*/ /*psinfo*/  /*einfo*/   /*size_thresh0(1,2)_en*/    /*size_thresh0*/    /*size_thresh1*/    /*size_thresh2*/    /*fdq0_sz0_qnum*/       /*fdq0_sz1_qnum*/           /*fdq0_sz2_qnum*/           /*fdq0_sz3_qnum*/           /*fdq1_qnum*/           /*fdq2_qnum*/           /*fdq3_qnum*/   /*dest_tag_lo*/ /*dest_tag_hi*/ /*src_tag_lo*/  /*src_tag_hi*/  /*dest_tag_lo_sel*/ /*dest_tag_hi_sel*/ /*src_tag_lo_sel*/  /*src_tag_hi_sel*/
    {PKT_DMA_TEST_DST_Q,    0,      0,      Cppi_DescType_HOST,     0,          0,          0,          1, 1, 1,    SL2_PACKET_BUFFER_SIZE0, SL2_PACKET_BUFFER_SIZE1, SL2_PACKET_BUFFER_SIZE2, SL2_HOST_SIZE0_FDQ,          SL2_HOST_SIZE1_FDQ,     SL2_HOST_SIZE2_FDQ,         SL2_HOST_SIZE3_FDQ,         SL2_HOST_SIZE3_FDQ,     SL2_HOST_SIZE3_FDQ,     SL2_HOST_SIZE3_FDQ,         0,      0,              0,              0,              0,                  0,                  0,                  0},
    {PKT_DMA_TEST_DST_Q,    12,     0,  Cppi_DescType_MONOLITHIC,   0,          0,          0,          0, 0, 0,                    0,                  0,                  0,                  SL2_MONOLITHIC_FDQ,     0,                          0,                          0,                          SL2_MONOLITHIC_FDQ,     SL2_MONOLITHIC_FDQ,     SL2_MONOLITHIC_FDQ,         0,      0,              0,              0,              0,                  0,                  0,                  0}
};

Uint32 uiUsedDescRegionNum;
Uint32 uiInitialHostFdqNum;
Uint32 uiInitialMonoFdqNum;
Uint32 uiUsedRxFlowNum;

/*----------------------------------------------*
 * constants                                    *
 *----------------------------------------------*/

/*----------------------------------------------*
 * macros                                       *
 *----------------------------------------------*/

/*----------------------------------------------*
 * routines' implementations                    *
 *----------------------------------------------*/


/*****************************************************************************
 Prototype    : QMSS_init
 Description  : Initialize the QMSS for MPU test
 Input        : None
 Output       : None
 Return Value : 
 
  History        :
  1.Date         : 2012/10/28
    Author       : Brighton Feng
    Modification : Created function

*****************************************************************************/
void QMSS_init()
{
	uiUsedDescRegionNum= sizeof(descMemRegionsCfg)/sizeof(Qmss_DescMemRegionCfg);
	uiInitialHostFdqNum= sizeof(freeHostQueueCfgTable)/sizeof(FreeHostQueueCfg);
	uiInitialMonoFdqNum= sizeof(freeMonoQueueCfgTable)/sizeof(FreeMonoQueueCfg);

	/*-----------------QMSS queue configuration------------------*/
	KeyStone_QMSS_Linking_RAM_init(NULL, 0);

	KeyStone_QMSS_Descriptor_Regions_init(descMemRegionsCfg, uiUsedDescRegionNum);

	KeyStone_Host_Descriptor_Queues_init(freeHostQueueCfgTable, uiInitialHostFdqNum);
	KeyStone_Mono_Descriptor_Queues_init(freeMonoQueueCfgTable, uiInitialMonoFdqNum);

	gpQM_INTD_regs->STATUS_CLR_REG0= 0xFFFFFFFF;
	gpQM_INTD_regs->STATUS_CLR_REG1= 0xFFFF;
}
/*****************************************************************************
 Prototype    : PktDMA_init
 Description  : Initialize the PktDMA for MPU test
 Input        : None
 Output       : None
 Return Value : 
 
  History        :
  1.Date         : 2012/10/28
    Author       : Brighton Feng
    Modification : Created function

*****************************************************************************/
void PktDMA_init()
{
	int i;
	
	uiUsedRxFlowNum= sizeof(flowCfgTable)/sizeof(PktDma_RxFlowCfg);

	/*-----------------QMSS PktDMA configuration------------------*/
	KeyStone_pktDma_Global_Control(gpQM_DMA_CfgRegs, 256, 5, 5);

	for(i=0; i<uiUsedRxFlowNum; i++)
	{
		KeyStone_pktDma_RxCh_enable(gpQM_DMA_RxChCfgRegs, i);
		KeyStone_pktDma_TxCh_enable(gpQM_DMA_TxChCfgRegs, i);
	}
	
	KeyStone_pktDma_configureRxFlow(
		gpQM_DMA_RxFlowCfgRegs->RX_FLOW_CONFIG, flowCfgTable, uiUsedRxFlowNum);

}
