iPXE
Functions
mii.c File Reference

Media Independent Interface. More...

#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <ipxe/mii.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
int mii_restart (struct mii_device *mii)
 Restart autonegotiation.
int mii_reset (struct mii_device *mii)
 Reset MII device.
int mii_check_link (struct mii_device *mii, struct net_device *netdev)
 Update link status via MII.
int mii_find (struct mii_device *mii)
 Find PHY address.

Detailed Description

Media Independent Interface.

Definition in file mii.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
int mii_restart ( struct mii_device mii)

Restart autonegotiation.

Parameters:
miiMII device
Return values:
rcReturn status code

Definition at line 43 of file mii.c.

References BMCR_ANENABLE, BMCR_ANRESTART, DBGC, MII_BMCR, mii_read(), mii_write(), rc, and strerror().

Referenced by mii_reset(), and realtek_phy_reset().

                                           {
        int bmcr;
        int rc;

        /* Read BMCR */
        bmcr = mii_read ( mii, MII_BMCR );
        if ( bmcr < 0 ) {
                rc = bmcr;
                DBGC ( mii, "MII %p could not read BMCR: %s\n",
                       mii, strerror ( rc ) );
                return rc;
        }

        /* Enable and restart autonegotiation */
        bmcr |= ( BMCR_ANENABLE | BMCR_ANRESTART );
        if ( ( rc = mii_write ( mii, MII_BMCR, bmcr ) ) != 0 ) {
                DBGC ( mii, "MII %p could not write BMCR: %s\n",
                       mii, strerror ( rc ) );
                return rc;
        }

        DBGC ( mii, "MII %p restarted autonegotiation\n", mii );
        return 0;
}
int mii_reset ( struct mii_device mii)

Reset MII device.

Parameters:
miiMII device
Return values:
rcReturn status code

Definition at line 74 of file mii.c.

References BMCR_ANENABLE, BMCR_RESET, DBGC, ETIMEDOUT, mdelay(), MII_BMCR, mii_read(), MII_RESET_MAX_WAIT_MS, mii_restart(), mii_write(), rc, and strerror().

Referenced by icplus_init_phy(), realtek_phy_reset(), rhine_probe(), and velocity_probe().

                                         {
        unsigned int i;
        int bmcr;
        int rc;

        /* Power-up, enable autonegotiation and initiate reset */
        if ( ( rc = mii_write ( mii, MII_BMCR,
                                ( BMCR_RESET | BMCR_ANENABLE ) ) ) != 0 ) {
                DBGC ( mii, "MII %p could not write BMCR: %s\n",
                       mii, strerror ( rc ) );
                return rc;
        }

        /* Wait for reset to complete */
        for ( i = 0 ; i < MII_RESET_MAX_WAIT_MS ; i++ ) {

                /* Check if reset has completed */
                bmcr = mii_read ( mii, MII_BMCR );
                if ( bmcr < 0 ) {
                        rc = bmcr;
                        DBGC ( mii, "MII %p could not read BMCR: %s\n",
                               mii, strerror ( rc ) );
                        return rc;
                }

                /* If reset is not complete, delay 1ms and retry */
                if ( bmcr & BMCR_RESET ) {
                        mdelay ( 1 );
                        continue;
                }

                /* Force autonegotation on again, in case it was
                 * cleared by the reset.
                 */
                if ( ( rc = mii_restart ( mii ) ) != 0 )
                        return rc;

                DBGC ( mii, "MII %p reset after %dms\n", mii, i );
                return 0;
        }

        DBGC ( mii, "MII %p timed out waiting for reset\n", mii );
        return -ETIMEDOUT;
}
int mii_check_link ( struct mii_device mii,
struct net_device netdev 
)

Update link status via MII.

Parameters:
miiMII device
netdevNetwork device
Return values:
rcReturn status code

Definition at line 126 of file mii.c.

References BMSR_LSTATUS, DBGC, link, MII_BMSR, mii_read(), netdev_link_down(), netdev_link_up(), and rc.

Referenced by smscusb_mii_check_link().

                                                                         {
        int bmsr;
        int link;
        int rc;

        /* Read BMSR */
        bmsr = mii_read ( mii, MII_BMSR );
        if ( bmsr < 0 ) {
                rc = bmsr;
                return rc;
        }

        /* Report link status */
        link = ( bmsr & BMSR_LSTATUS );
        DBGC ( mii, "MII %p link %s (BMSR %#04x)\n",
               mii, ( link ? "up" : "down" ), bmsr );
        if ( link ) {
                netdev_link_up ( netdev );
        } else {
                netdev_link_down ( netdev );
        }

        return 0;
}
int mii_find ( struct mii_device mii)

Find PHY address.

Parameters:
miiMII device
Return values:
rcReturn status code

Definition at line 157 of file mii.c.

References mii_device::address, address, DBGC, ENOENT, id, MII_MAX_PHY_ADDRESS, MII_PHYSID1, and mii_read().

Referenced by icplus_init_phy().

                                        {
        unsigned int address;
        int id;

        /* Try all possible PHY addresses */
        for ( address = 0 ; address <= MII_MAX_PHY_ADDRESS ; address++ ) {
                mii->address = address;
                id = mii_read ( mii, MII_PHYSID1 );
                if ( ( id > 0x0000 ) && ( id < 0xffff ) ) {
                        DBGC ( mii, "MII %p found PHY at address %d\n",
                               mii, address );
                        return 0;
                }
        }

        DBGC ( mii, "MII %p failed to find an address\n", mii );
        return -ENOENT;
}