/**
 * @file  hv_drv_TypecInterrupts.c
 * @brief     UPD interrupt source file 
 *          This file contains functions to handle UPD interrupts.
 * @details This file provides the following functions: \n
 *          (1) \n
 *          (2) \n
 *          (3) \n
 *
 * @author HiView SoC Software Team
 * @version 1.0.0
 * @date 2022-08-25
 * @copyright Copyright(c),2022-8, Hiview Software. All rights reserved.
 * @par History:
 * <table>
 * <tr><th>Author                         <th>Date         <th>Change Description
 * <tr><td>Hiview SoC Software Team       <td>2022-08-25   <td>init
 * </table>
*/


#include <hv_drv_TypecStdInc.h>

void Hv_Drv_Typec_IrqHandler(UINT8 u8PortNum)
{
    do
    {
        HV_PSF_HOOK_DISABLE_GLOBAL_INTERRUPT();

        /*CC,PWR,VBUS interrupts are handled by the function "TypeC_HandleISR"*/
        TypeC_HandleISR (u8PortNum);

        /*Checking for PD MAC Interrupt*/
        PRL_HandleISR (u8PortNum);

        HV_PSF_HOOK_ENABLE_GLOBAL_INTERRUPT();    
    }
    while (HV_FALSE);
}

#if TYPEC_DEBUG_TRACE_EN
void UPDIntr_PollingHandler(UINT8 u8PortNum)
{
    if(HV_R32(0x110119FC))
    {
        HV_W32(0x110119FC, 0);

        HV_LOGI(" gasTypeCcontrol[u8PortNum].u8TypeCState = %d\n", gasTypeCcontrol[u8PortNum].u8TypeCState);
        HV_LOGI(" gasTypeCcontrol[u8PortNum].u8TypeCSubState = %d \n", gasTypeCcontrol[u8PortNum].u8TypeCSubState);
        HV_LOGI(" gasPolicyEngine[u8PortNum].ePEState = %d \n", gasPolicyEngine[u8PortNum].ePEState);
        HV_LOGI(" gasPolicyEngine[u8PortNum].ePETimeoutSubState = %d \n", gasPolicyEngine[u8PortNum].ePETimeoutSubState);
        HV_LOGI(" gasPolicyEngine[u8PortNum].ePESubState = %d \n", gasPolicyEngine[u8PortNum].ePESubState);
    }
    
    if(HV_R32(0x110119F8))
    {
        HV_W32(0x110119F8, 0);

        HV_W32_FIELD_EX(STANDBY_USB_TYPEC_TOP_00_04, 0x00, reg_00_sw_ctrl_rd_en, 1);
        HV_W32_FIELD_EX(STANDBY_USB_TYPEC_TOP_00_04, 0x00, reg_00_sw_ctrl_rp_en, 1);

        /* Simulate Typec Unplug*/
        HV_W32_FIELD_EX(STANDBY_USB_TYPEC_TOP_00_04, 0x00, reg_00_cc1_rd_en, 0);
        HV_W32_FIELD_EX(STANDBY_USB_TYPEC_TOP_00_04, 0x00, reg_00_cc2_rd_en, 0);
        HV_W32_FIELD_EX(STANDBY_USB_TYPEC_TOP_00_04, 0x00, reg_00_cc1_rp_en, 0);
        HV_W32_FIELD_EX(STANDBY_USB_TYPEC_TOP_00_04, 0x00, reg_00_cc2_rp_en, 0);

        Hv_Vos_MSleep(10);
        
        HV_W32_FIELD_EX(STANDBY_USB_TYPEC_TOP_00_04, 0x00, reg_00_cc1_rd_en, 1);
        HV_W32_FIELD_EX(STANDBY_USB_TYPEC_TOP_00_04, 0x00, reg_00_cc1_rd_en, 1);

        HV_W32_FIELD_EX(STANDBY_USB_TYPEC_TOP_00_04, 0x00, reg_00_sw_ctrl_rd_en, 0);
        HV_W32_FIELD_EX(STANDBY_USB_TYPEC_TOP_00_04, 0x00, reg_00_sw_ctrl_rp_en, 0);
    }

    if(HV_R32(0x110119F0))
    {
        HV_W32(0x110119F0, 0);
        Hv_Drv_Typec_SetTaskPriority(TYPEC_NORMAL_TASK_PRIORITY);
        PE_SendHardReset(0);
    }

    if(HV_R32(0x110119EC))
    {
        HV_W32(0x110119EC, 0);
        gasCfgStatusData.sVDMPerPortData[u8PortNum].u8SVDMIdentityFlag = HV_TRUE;
        //while(1);
    }

    if(HV_R32(0x110119E8))
    {
        HV_W32(0x110119E8, 0);
        HV_LOGI(" u32aPartnerPDIdentity[0] = 0x%08x \n", gasCfgStatusData.sVDMPerPortData[u8PortNum].u32aPartnerPDIdentity[0]);
        HV_LOGI(" u32aPartnerPDIdentity[1] = 0x%08x \n", gasCfgStatusData.sVDMPerPortData[u8PortNum].u32aPartnerPDIdentity[1]);
        HV_LOGI(" u32aPartnerPDIdentity[2] = 0x%08x \n", gasCfgStatusData.sVDMPerPortData[u8PortNum].u32aPartnerPDIdentity[2]);
        HV_LOGI(" u32aPartnerPDIdentity[3] = 0x%08x \n", gasCfgStatusData.sVDMPerPortData[u8PortNum].u32aPartnerPDIdentity[3]);
        HV_LOGI(" u32aPartnerPDIdentity[4] = 0x%08x \n", gasCfgStatusData.sVDMPerPortData[u8PortNum].u32aPartnerPDIdentity[4]);
        HV_LOGI(" u32aPartnerPDIdentity[5] = 0x%08x \n", gasCfgStatusData.sVDMPerPortData[u8PortNum].u32aPartnerPDIdentity[5]);        
        HV_LOGI(" u32aPartnerPDIdentity[5] = %d \n", gasCfgStatusData.sVDMPerPortData[u8PortNum].u8PartnerPDIdentityCnt);
    }

    if(HV_R32(0x110119E4))
    {
        HV_W32(0x110119E4, 0);
        HV_LOGI(" DPM_PORT_ATTACHED_STATUS = 0x%08x \n", gasCfgStatusData.sPerPortData[u8PortNum].u32PortConnectStatus & DPM_PORT_ATTACHED_STATUS);
        HV_LOGI(" DPM_PORT_ORIENTATION_FLIPPED_STATUS = 0x%08x \n", gasCfgStatusData.sPerPortData[u8PortNum].u32PortConnectStatus & DPM_PORT_ORIENTATION_FLIPPED_STATUS);
    }
}
#endif

