/**
* @file hsr_prp_soc.c
*
* @brief Contains AM335x SoC specific intialization functions for HSR PRP protocol
*
* \par
* Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
* \par
*
*/
#include "hsr_prp_soc.h"
#include <examples/board/include/board_spi.h>
#include <ti/drv/spi/SPI.h>
#include <ti/board/board.h>
#include <examples/board/include/board_tlkphy.h>
#include <ti/drv/pruss/soc/pruicss_v1.h>
#include "ti/starterware/include/soc_control.h"
#include "ti/starterware/include/chipdb.h"

SPI_Handle handle;
#define MCSPI_INSTANCE         (2U)
extern ICSS_EmacHandle appEmacHandle;
extern PRUICSS_Handle prusshandle;

void HSR_PRP_configureInterrupts(ICSSEMAC_InitConfig *switchEmacCfg)
{
    /* Interrupt configuration */
    switchEmacCfg->rxIntNum = HSR_PRP_RX_INT_NUM;
    switchEmacCfg->txIntNum = 0;
    switchEmacCfg->linkIntNum = HSR_PRP_LINK_INT_NUM;
}

void HSR_PRP_socHwinit(uint8_t instance)
{
    Board_init(BOARD_INIT_MODULE_CLOCK |
               BOARD_INIT_ICSS_PINMUX | BOARD_INIT_UART_STDIO | BOARD_INIT_ICSS_ETH_PHY);

    PRCMModuleEnable(CHIPDB_MOD_ID_PWMSS, 0, 0);

    /* Clock source selection */
    SOCCtrlTimerClkSrcSelect(HSR_PRP_TIMER_ID,
                             SOC_CTRL_DMTIMER_CLK_SRC_M_OSC_24M);
    PRCMModuleEnable(CHIPDB_MOD_ID_DMTIMER, HSR_PRP_TIMER_ID , FALSE);
    PRCMModuleEnable(CHIPDB_MOD_ID_DMTIMER, HSR_PRP_TIMER_ID + 2, FALSE);

    /*Pinmux for LatchO in AM437x IDK*/
    HWREG(0x44E10978) = 0x00050006;

    Board_phyReset(2);

}

void HSR_PRP_socInit()
{
    SPI_init();
    /* HVS SPI Init */
    SPI_Params spiParams;                /* SPI params structure */
    SPI_Params_init(&spiParams);
    spiParams.frameFormat  = SPI_POL1_PHA0;
    handle = SPI_open((MCSPI_INSTANCE - 1U), &spiParams);

    /* Flash Init */
    Board_flashOpen(NULL, NULL, NULL);

    Board_i2cLedInit();
}

void HSR_PRP_socgetMACAddress(uint8_t *lclMac)
{
    SOCCtrlGetPortMacAddr(1, lclMac);
}
