fgmac_intr.c 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
/*
 * Copyright : (C) 2022 Phytium Information Technology, Inc.
 * All Rights Reserved.
 *
 * This program is OPEN SOURCE software: you can redistribute it and/or modify it
 * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
 * either version 1.0 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the Phytium Public License for more details.
 *
 *
 * FilePath: fgmac_intr.c
 * Date: 2022-04-06 14:46:52
 * LastEditTime: 2022-04-06 14:46:58
 * Description:  This file is for
 *
 * Modify History:
 *  Ver   Who        Date         Changes
 * ----- ------     --------    --------------------------------------
 */

/***************************** Include Files *********************************/

#include "fgmac.h"
#include "fgmac_hw.h"

/************************** Constant Definitions *****************************/

/**************************** Type Definitions *******************************/

/***************** Macros (Inline Functions) Definitions *********************/
#define FGMAC_CALL_EVT_HANDLER(instance_p, evt)    \
    {\
        if (NULL != (instance_p)->evt_handler[(evt)]) \
        {                                        \
            (instance_p)->evt_handler[evt]((void *)(instance_p));  \
        }\
    }

/************************** Variable Definitions *****************************/

/************************** Function Prototypes ******************************/

/**
 * @name: FGmacInterruptHandler
 * @msg: FGMAC中断处理函数
 * @return {*}
 * @param {s32} vector, 中断向量号,此处没有用到
          {void} *param, 中断输入参数,此处传入的是FGMAC的驱动控制数据
 * @note 此函数运行在中断上下文
 */
void FGmacInterruptHandler(s32 vector, void *param)
{
    FASSERT(param);
    FGmac *instance_p = (FGmac *)param;
    uintptr base_addr = instance_p->config.base_addr;
    u32 status = 0;

    /* dma interrupt */
    status = FGMAC_READ_REG32(base_addr, FGMAC_DMA_STATUS_OFFSET);

    if (FGMAC_DMA_STATUS_GLI & status)
    {
        FGMAC_CALL_EVT_HANDLER(instance_p, FGMAC_PHY_STATUS_EVT);
    }

    if (FGMAC_DMA_STATUS_RI & status)
    {
        FGMAC_CALL_EVT_HANDLER(instance_p, FGMAC_RX_COMPLETE_EVT);
        FGMAC_SET_REG32(base_addr, FGMAC_DMA_STATUS_OFFSET, FGMAC_DMA_STATUS_RI); /* write to clear */
    }

    if (FGMAC_DMA_STATUS_TI & status)
    {
        FGMAC_CALL_EVT_HANDLER(instance_p, FGMAC_TX_COMPLETE_EVT);
        FGMAC_SET_REG32(base_addr, FGMAC_DMA_STATUS_OFFSET, FGMAC_DMA_STATUS_TI); /* write to clear */
    }

    if (FGMAC_DMA_STATUS_AIS & status)
    {
        FGMAC_CALL_EVT_HANDLER(instance_p, FGMAC_DMA_ERR_EVT);
        FGMAC_SET_REG32(base_addr, FGMAC_DMA_STATUS_OFFSET, FGMAC_DMA_STATUS_CLR_ABLE);
    }

    FGMAC_WRITE_REG32(base_addr, FGMAC_DMA_STATUS_OFFSET, status); /* write to clear */

    /* RGMII/SGMII Interrupt */
    status = FGMAC_READ_REG32(base_addr, FGMAC_INTR_STATUS_OFFSET);
    if (status & FGMAC_ISR_STATUS_RSIS)
    {
        /* status changed, read SGMII register to clear */
        FGMAC_READ_REG32(base_addr, FGMAC_MAC_PHY_STATUS);
    }

    return;
}

/**
 * @name: FGmacRegisterEvtHandler
 * @msg: 注册FGMAC中断事件响应函数
 * @return {*}
 * @param {FGmac} *instance_p 驱动控制数据
 * @param {u32} evt 中断事件类型
 * @param {FGmacEvtHandler} handler 中断事件响应函数
 * @note 注册的函数handler会在中断上下文执行
 */
void FGmacRegisterEvtHandler(FGmac *instance_p, u32 evt, FGmacEvtHandler handler)
{
    FASSERT((NULL != instance_p) && (FGMAC_INTR_EVT_NUM > evt));
    instance_p->evt_handler[evt] = handler;
}

/**
 * @name: FGmacSetInterruptMask
 * @msg: 屏蔽FGMAC中断
 * @return {*}
 * @param {FGmac} *instance_p 驱动控制数据
 * @param {u32} intr_type 中断类型 GMAC中断/DMA中断
 * @param {u32} mask 中断屏蔽位
 * @note 在FGMAC驱动初始化成功后调用此函数
 */
void FGmacSetInterruptMask(FGmac *instance_p, u32 intr_type, u32 mask)
{
    FASSERT(instance_p);
    FASSERT(FGMAC_MAX_INTR_TYPE > intr_type);
    u32 cur_mask = 0;
    uintptr base_addr = instance_p->config.base_addr;

    if (FGMAC_CTRL_INTR == intr_type)
    {
        cur_mask = FGMAC_READ_REG32(base_addr, FGMAC_INTR_MASK_OFFSET);
        cur_mask |= mask;
        FGMAC_WRITE_REG32(base_addr, FGMAC_INTR_MASK_OFFSET, cur_mask);
    }
    else
    {
        cur_mask = FGMAC_READ_REG32(base_addr, FGMAC_DMA_INTR_OFFSET);
        cur_mask &= (~mask);
        FGMAC_WRITE_REG32(base_addr, FGMAC_DMA_INTR_OFFSET, cur_mask);
    }

    return;
}

/**
 * @name: FGmacSetInterruptUmask
 * @msg: 使能FGMAC中断
 * @return {*}
 * @param {FGmac} *instance_p 驱动控制数据
 * @param {u32} intr_type 中断类型 GMAC中断/DMA中断
 * @param {u32} mask 中断使能标志位
 * @note 在FGMAC驱动初始化成功后调用此函数
 */
void FGmacSetInterruptUmask(FGmac *instance_p, u32 intr_type, u32 mask)
{
    FASSERT(instance_p);
    FASSERT(FGMAC_MAX_INTR_TYPE > intr_type);
    u32 cur_mask = 0;
    uintptr base_addr = instance_p->config.base_addr;

    if (FGMAC_CTRL_INTR == intr_type)
    {
        cur_mask = FGMAC_READ_REG32(base_addr, FGMAC_INTR_MASK_OFFSET);
        cur_mask &= (~mask);
        FGMAC_WRITE_REG32(base_addr, FGMAC_INTR_MASK_OFFSET, cur_mask);
    }
    else
    {
        cur_mask = FGMAC_READ_REG32(base_addr, FGMAC_DMA_INTR_OFFSET);
        cur_mask |= mask;
        FGMAC_WRITE_REG32(base_addr, FGMAC_DMA_INTR_OFFSET, cur_mask);
    }

    return;
}