#include "lip/phy/lip_phy_cfg_defs.h"

#if LIP_PHY_SUPPORT_DP83848J || LIP_PHY_SUPPORT_DP83848C
#include "lip/phy/lip_phy_defs.h"
#include "lip/phy/lip_phy_mdio_regs.h"
#include "dp83848_regs.h"

static t_lip_errs f_spec_cfg(const t_lip_phy_mdio_acc *regs_acc, const t_lip_phy_cfg *cfg);
static t_lip_errs f_get_mode(const t_lip_phy_mdio_acc *regs_acc, t_lip_phy_mode *mode);


#if LIP_PHY_SUPPORT_DP83848J
    /* DP83848H, DP83848J, DP83848K, DP83848M, DP83848T */
    t_lip_phy_info lip_phy_dp83848j = {
        .name = "DP83848J",
        .model_id  =  LIP_PHY_MODEL_ID(DP83848_ID_VENDOR, DP83848J_ID_MODEL),
        .an_features = LIP_PHY_AN_FEAT_MODE_10_T_FD | LIP_PHY_AN_FEAT_MODE_10_T_HD
            | LIP_PHY_AN_FEAT_MODE_100_TX_FD | LIP_PHY_AN_FEAT_MODE_100_TX_HD
            | LIP_PHY_AN_FEAT_PAUSE_SYM | LIP_PHY_AN_FEAT_PAUSE_ASYM,
        .spec_features = 0,
        .spec_cfg = f_spec_cfg,
        .get_mode = f_get_mode,
    };
#endif
#if LIP_PHY_SUPPORT_DP83848C
    /* DP83848C/I/VYB/YB */
    t_lip_phy_info lip_phy_dp83848c = {
        "DP83848C",
        .model_id  =  LIP_PHY_MODEL_ID(DP83848_ID_VENDOR, DP83848C_ID_MODEL),
        .an_features = LIP_PHY_AN_FEAT_MODE_10_T_FD | LIP_PHY_AN_FEAT_MODE_10_T_HD
            | LIP_PHY_AN_FEAT_MODE_100_TX_FD | LIP_PHY_AN_FEAT_MODE_100_TX_HD
            | LIP_PHY_AN_FEAT_PAUSE_SYM | LIP_PHY_AN_FEAT_PAUSE_ASYM,
        .spec_features = 0,
        .spec_cfg = f_spec_cfg,
        .get_mode = f_get_mode,
    };
#endif



static t_lip_errs f_spec_cfg(const t_lip_phy_mdio_acc *regs_acc, const t_lip_phy_cfg *cfg) {
    t_lip_errs err = LIP_ERR_SUCCESS;
    /* разрешаем режим RMII */
    err = lip_phy_mdio_reg_write(regs_acc, DP83848_REGADDR_RBR, DP83848_REGFLD_RBR_RMII_MODE);
    return err;
}

static  t_lip_errs f_get_mode(const t_lip_phy_mdio_acc *regs_acc, t_lip_phy_mode *mode) {
    t_lip_errs err = LIP_ERR_SUCCESS;
    uint16_t phy_reg;

    err = lip_phy_mdio_reg_read(regs_acc, DP83848_REGADDR_PHYSTS, &phy_reg);
    if (err == LIP_ERR_SUCCESS) {
        mode->flags = 0;
        if (phy_reg & DP83848_REGFLD_PHYSTS_DUPLEX_STATUS) {
            mode->flags |= LIP_PHY_MODE_FLAG_DUPLEX_FULL;
        }
        mode->speed = phy_reg & DP83848_REGFLD_PHYSTS_SPEED_STATUS ?
                    LIP_PHY_SPEED_10 : LIP_PHY_SPEED_100;
    }
    return err;
}

#endif
