#include "ucp_wdt.h"

#include "dw_apb_wdt.h"
#include "typedef.h"
#include "ucp_utility.h"
#include "ucp_printf.h"
#include "ucp_param.h"
#include "ucp_sfr_c.h"
#include "typedef.h"

#ifdef APE_INT
#include "smartos.h"
#endif
#ifdef RFM_INT
#include "inter_vector.h"
#endif

int32_t gWdtIntCnt = 0;
void ucp_wdt0_init(int32_t mode)
{
	SMMU_WDT_REG  |= (1<<2); // WDT 在count 为0时产生中断和复位
	WDT0_TORR = 0x1;		// 0x1FFFF, 524us
	if (0 == mode)
	{
		WDT0_CR = 1;			
	}
	else
	{
		WDT0_CR = 3;			//1 IRQ THE RESET
	}
}

void ucp_wdt1_init(int32_t mode)
{
	SMMU_WDT_REG  |= (1<<5); // WDT 在count 为0时产生中断和复位
	WDT1_TORR = 0x0;		// 0xFFFF, 262us
	if (0 == mode)
	{
		WDT1_CR = 1;			
	}
	else
	{
		WDT1_CR = 3;			//1 IRQ THE RESET
	}
}

void ucp_wdt0_int_init()
{
	// APC_WDT0_INTR
	int32_t apeId = get_core_id();
	int32_t intNum = APC_WDT0_INTR;	// 314
	int32_t ret = smart_irq_request(intNum, isr_ucp_wdt0);
	if (0 != ret)
	{
		UCP_PRINT_ERROR("attach int num: 0x%x error. errno = 0x%x. \r\n", intNum, ret);
		debug_write(DBG_DDR_ERR_IDX(apeId, 1), ret);
	}
}

void ucp_wdt1_int_init()
{
	// APC_WDT0_INTR
	int32_t apeId = get_core_id();
	int32_t intNum = APC_WDT1_INTR;	// 315
	int32_t ret = smart_irq_request(intNum, isr_ucp_wdt1);
	if (0 != ret)
	{
		UCP_PRINT_ERROR("attach int num: 0x%x error. errno = 0x%x. \r\n", intNum, ret);
		debug_write(DBG_DDR_ERR_IDX(apeId, 1), ret);
	}
}

void isr_ucp_wdt0(void)
{
	uint32_t temp = WDT0_EOI;
	debug_write((DBG_DDR_IDX_DRV_BASE+112), temp);	// 0x1C0
	ucp_wdt0_feed();
}

void isr_ucp_wdt1(void)
{
	uint32_t temp = WDT0_EOI;
	debug_write((DBG_DDR_IDX_DRV_BASE+113), temp);	// 0x1C4
	ucp_wdt1_feed();
}

void ucp_wdt0_feed()
{
	int32_t apeId = get_core_id();
	WDT0_CRR = 0x76;
	gWdtIntCnt++;
	debug_write((DBG_DDR_IDX_DRV_BASE+116+apeId), gWdtIntCnt);	// 0x1C4
}

void ucp_wdt1_feed()
{
	int32_t apeId = get_core_id();
	WDT1_CRR = 0x76;
	gWdtIntCnt++;
	debug_write((DBG_DDR_IDX_DRV_BASE+116+apeId), gWdtIntCnt);	// 0x1C4
}

