/* This function sets the value of the PHY registers through the MII interface.  */
int imx_fec_mii_write(volatile hw_fec_t *fec_reg, unsigned char phy_addr, unsigned char reg_addr, unsigned short int value)
/* This function sets the value of the PHY registers through the MII interface.  */
int imx_fec_mii_write(volatile hw_fec_t *fec_reg, unsigned char phy_addr, unsigned char reg_addr, unsigned short int value)
{
    unsigned long waiting = FEC_MII_TIMEOUT;
    if (fec_reg->EIR.U & FEC_EVENT_MII) {
        fec_reg->EIR.U = FEC_EVENT_MII;
    }
    fec_reg->MMFR.U = FEC_MII_WRITE(phy_addr, reg_addr, value);
    while (1) {
        if (fec_reg->EIR.U & FEC_EVENT_MII) {
            fec_reg->EIR.U = FEC_EVENT_MII;
            break;
        }
        if ((--waiting) == 0)
            return -1;
        hal_delay_us(FEC_MII_TICK);
    }
    return 0;
}