/* SPDX-License-Identifier: LGPL-2.1-only OR BSD-3-Clause */
/******************************************************************************
 *
 * This file is provided under a dual license.  When you use or
 * distribute this software, you may choose to be licensed under
 * version 2.1 of the GNU Lesser General Public License
 * ("LGPLv2.1 License") or BSD License.
 *
 * LGPLv2.1 License
 *
 * Copyright(C) 2019 MediaTek Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * 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 https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html for more
 * details.
 *
 * BSD LICENSE
 *
 * Copyright(C) 2019 MediaTek Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *****************************************************************************/

/*****************************************************************************
 *
 * Filename:
 * ---------
 *   $Workfile: ether_if.c $
 *
 * Project:
 * --------
 *
 *
 * Description:
 * ------------
 *   Ethernet driver interface header file
 *
 * Author:
 * -------
 *   Hs Huang
 *
 * Last changed:
 * -------------
 * $Author:
 *
 * $Modtime:
 *
 * $Revision:
****************************************************************************/

#ifndef X_ETHER_IF_H
#define X_ETHER_IF_H

/****************************************************************************
** Constant definitions
****************************************************************************/
/* ethernet message queue priority, 0: highest, 255: lowest */
typedef enum 
{
    ETHER_MSG_PRIORITY_TIMER = 10,	/* periodical timer task */
    ETHER_MSG_PRIORITY_IRQ, 		/* interrupt procedure */
    ETHER_MSG_PRIORITY_IOCTL		/* ioctl operation */
} ETHER_MSG_PRIORITY_T;

/* ethernet message queue command */
typedef enum 
{
    ETHER_MSG_RECV_IRQ = 0,
    ETHER_MSG_WAKEUP_IRQ,
    ETHER_MSG_TIMER_SRV,
    ETHER_MSG_OID_SET,
    ETHER_MSG_OID_GET
} ETHER_MSG_T;


/* Event group flags */
typedef enum 
{
    /* wake up thread flags */
    ETHER_EG_TX_COMP_IRQ      = (EV_GRP_EVENT_T)(0x1 << 0),     /* tx completion interrupt */
    ETHER_EG_RX_IRQ           = (EV_GRP_EVENT_T)(0x1 << 1),     /* rx interrupt */
    ETHER_EG_RECV_PKT         = (EV_GRP_EVENT_T)(0x1 << 2),     /* continue to service received packet */
    ETHER_EG_WAKEUP_IRQ       = (EV_GRP_EVENT_T)(0x1 << 3),     /* PMT wakeup interrupt */
    ETHER_EG_TIMER_SRV        = (EV_GRP_EVENT_T)(0x1 << 4),     /* timer jobs service */

    ETHER_EG_WAKEUP_THREAD    = (EV_GRP_EVENT_T)(ETHER_EG_TX_COMP_IRQ | ETHER_EG_RX_IRQ 
                                                | ETHER_EG_WAKEUP_IRQ | ETHER_EG_TIMER_SRV
                                                | ETHER_EG_RECV_PKT),
}WLAN_EVENT_T;


#define ETHER_TX_TIMEOUT              200		/* ethernet tx timeout value(ms) */ 

/****************************************************************************
** Structure and Type definitions
****************************************************************************/
/* ethernet driver message parameter structure */
typedef struct _ETHER_MSG_PARM_T_
{
    UINT32			u4Cmd1;
    UINT32			u4Cmd2;
    void			*pvArg;
} ETHER_MSG_PARM_T;

/****************************************************************************
** Macros
****************************************************************************/
#if CONFIG_ETHER_USE_SAM_INSTEAD_EV
extern HANDLE_T _hEtherTaskSema;
#define ETHER_SET_EVENT(u8Event)\
    CRIT_STATE_T  t_state;\
    t_state = x_crit_start();\
    prMac->u8TaskEvt |= u8Event;\
    x_crit_end(t_state);

#else
extern HANDLE_T _hEtherEvent;

/* The macro set ethernet group event flag */
#define ETHER_SET_EVENT(u8Event)  \
        {\
            VERIFY(x_ev_group_set_event(_hEtherEvent, u8Event, X_EV_OP_OR) == OSR_OK); \
        }    

/* The macro clear ethernet group event flag */
#define ETHER_CLR_EVENT(u8Event)  \
        {\
            VERIFY(x_ev_group_set_event(_hEtherEvent, ~u8Event, X_EV_OP_AND) == OSR_OK); \
        }    
#endif
/****************************************************************************
** Function Declarations
****************************************************************************/
NET_DRV_IF_T* prEtherGetDev(void);
INT32 i4EtherSendMsgQ(const PETHER_ADAPTER_T prMac, 
                             ETHER_MSG_T eEtherMsg, 
                             ETHER_MSG_PRIORITY_T ePriority );


/****************************************************************************
** External ethernet API
****************************************************************************/

/*******************************************************************************
**  i4EtherDrv_Init
**
**  @brief  This routine is called to initialize ethernet driver and creat thread
**
**  @param  prDev - pointer to net device structure
**  @result
**  @return NET_DRV_OK - driver init OK
**          NET_DRV_ERROR - driver init fail
**  @note
*******************************************************************************/
INT32 i4EtherDrv_Init (NET_DRV_IF_T *prDev);                                        

/*******************************************************************************
**  i4EtherDrv_DeInit
**
**  @brief  This routine is called to de-initialize ethernet driver and thread
**
**  @param  prDev - pointer to net device structure
**  @result
**  @return 0 if success
**          negative if fail
**  @note
*******************************************************************************/
INT32 i4EtherDrv_DeInit(NET_DRV_IF_T *prDev);

/*******************************************************************************
**  i4EtherDrv_Start
**
**  @brief  This routine is called to start ethernet driver processing
**
**  @param  prDev - pointer to net device structure
**  @result
**  @return 0 if success
**          negative if fail
**  @note
*******************************************************************************/
INT32 i4EtherDrv_Start(NET_DRV_IF_T *prDev);

/*******************************************************************************
**  i4EtherDrv_Stop
**
**  @brief  This routine is called to stop ethernet driver processing
**
**  @param  prDev - pointer to net device structure
**  @result
**  @return 0 if success
**          negative if fail
**  @note
*******************************************************************************/
INT32 i4EtherDrv_Stop(NET_DRV_IF_T *prDev);

/*******************************************************************************
**  i4EtherDrv_Txmit
**
**  @brief  This routine is called to transmit packet through ethernet driver 
**
**  @param  prDev - pointer to net device structure
**  @result
**  @return 0 if success
**          negative if fail
**  @note   ethernet driver will send the packet immediately and wait transmission 
**          completed sema, then rerun the fucntion call
*******************************************************************************/
INT32 i4EtherDrv_Txmit(const NET_DRV_IF_T *prDev, PKT_BUFF_T *prPkt);

/*******************************************************************************
**  i4EtherDrv_Set
**
**  @brief  This routine is called to set ethernet driver OIDs information
**
**  @param  prDev - pointer to net device structure
**          i4Cmd - OIDs command
**          pvParam - OIDs parameter
**  @result
**  @return 0 if success
**          negative if fail
**  @note
*******************************************************************************/
INT32 i4EtherDrv_Set(NET_DRV_IF_T *prDev, INT32 i4Cmd, VOID *pvParam);

/*******************************************************************************
**  i4EtherDrv_Get
**
**  @brief  This routine is called to get ethernet driver OIDs information
**
**  @param  prDev - pointer to net device structure
**          i4Cmd - OIDs command
**          pvParam - OIDs parameter
**  @result
**  @return 0 if success
**          negative if fail
**  @note
*******************************************************************************/
INT32 i4EtherDrv_Get(NET_DRV_IF_T *prDev, INT32 i4Cmd, VOID *pvParam);


//for PHY setting CLI
void vSet100MAmp(UINT32 chipId, UINT32 val);
UINT32 u1Get100MAmp(UINT32 chipId);
void vSet10MAmp(UINT32 chipId, UINT32 val);
UINT32 u1Get10MAmp(UINT32 chipId);
void vSetSlewRate(UINT32 chipId, UINT32 val);
UINT32 u1GetSlewRate(UINT32 chipId);
void vSetInputBias(UINT32 chipId, UINT32 val);
UINT32 u1GetInputBias(UINT32 chipId);
void vSetOutputBias(UINT32 chipId, UINT32 val);
UINT32 u1GetOutputBias(UINT32 chipId);
void vSet50percentBW(UINT32 chipId, UINT32 val);
UINT32 u1Get50percentBW(UINT32 chipId);
void vSetFeedbackCap(UINT32 chipId, UINT32 val);
UINT32 u1GetFeedbackCap(UINT32 chipId);
void vSetEyeOpen(UINT32 chipId, UINT32 val);
UINT32 u1GetEyeOpen(UINT32 chipId);
void vSetPhyLoopOn(UINT32 chipId, UINT32 val);
void vSetForceMdiCrossover(UINT32 chipId, UINT32 val);
void vSetSpeed(UINT32 chipId, UINT32 val);
void vSetPhyEPG(UINT32 chipId, UINT32 val);

#endif /* X_ETHER_IF_H */
