iPXE
Data Structures | Defines | Functions
mii.h File Reference

Media Independent Interface. More...

#include <mii.h>
#include <ipxe/netdevice.h>

Go to the source code of this file.

Data Structures

struct  mii_operations
 MII interface operations. More...
struct  mii_interface
 An MII interface. More...
struct  mii_device
 An MII device. More...

Defines

#define MII_RESET_MAX_WAIT_MS   500
 Maximum time to wait for a reset, in milliseconds.
#define MII_MAX_PHY_ADDRESS   31
 Maximum PHY address.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static void mdio_init (struct mii_interface *mdio, struct mii_operations *op)
 Initialise MII interface.
static void mii_init (struct mii_device *mii, struct mii_interface *mdio, unsigned int address)
 Initialise MII device.
static int mii_read (struct mii_device *mii, unsigned int reg)
 Read from MII register.
static int mii_write (struct mii_device *mii, unsigned int reg, unsigned int data)
 Write to MII register.
static void mii_dump (struct mii_device *mii)
 Dump MII registers (for debugging)
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.h.


Define Documentation

#define MII_RESET_MAX_WAIT_MS   500

Maximum time to wait for a reset, in milliseconds.

Definition at line 142 of file mii.h.

Referenced by mii_reset().

#define MII_MAX_PHY_ADDRESS   31

Maximum PHY address.

Definition at line 145 of file mii.h.

Referenced by mii_find().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static void mdio_init ( struct mii_interface mdio,
struct mii_operations op 
) [inline, static]

Initialise MII interface.

Parameters:
mdioMII interface
opMII interface operations

Definition at line 63 of file mii.h.

References op.

Referenced by init_mii_bit_basher(), realtek_probe(), rhine_probe(), and velocity_probe().

                                                                    {
        mdio->op = op;
}
static void mii_init ( struct mii_device mii,
struct mii_interface mdio,
unsigned int  address 
) [inline, static]

Initialise MII device.

Parameters:
miiMII device
mdioMII interface
addressPHY address

Definition at line 75 of file mii.h.

References address.

Referenced by icplus_probe(), realtek_probe(), rhine_probe(), and velocity_probe().

                                  {
        mii->mdio = mdio;
        mii->address = address;
}
static int mii_read ( struct mii_device mii,
unsigned int  reg 
) [inline, static]

Read from MII register.

Parameters:
miiMII device
regRegister address
Return values:
dataData read, or negative error

Definition at line 89 of file mii.h.

References mii_interface::op, and mii_operations::read.

Referenced by mii_dump().

                                                      {
        struct mii_interface *mdio = mii->mdio;

        return mdio->op->read ( mdio, mii->address, reg );
}
static int mii_write ( struct mii_device mii,
unsigned int  reg,
unsigned int  data 
) [inline, static]

Write to MII register.

Parameters:
miiMII device
regRegister address
dataData to write
Return values:
rcReturn status code

Definition at line 104 of file mii.h.

References mii_interface::op, and mii_operations::write.

Referenced by icplus_init_phy(), mii_reset(), mii_restart(), realtek_phy_speed(), smscusb_mii_check_link(), smscusb_mii_open(), and velocity_set_link().

                                                                          {
        struct mii_interface *mdio = mii->mdio;

        return mdio->op->write ( mdio, mii->address, reg, data );
}
static void mii_dump ( struct mii_device mii) [inline, static]

Dump MII registers (for debugging)

Parameters:
miiMII device

Definition at line 116 of file mii.h.

References data, DBG_LOG, DBGC, and mii_read().

Referenced by realtek_check_link().

                                    {
        unsigned int i;
        int data;

        /* Do nothing unless debug output is enabled */
        if ( ! DBG_LOG )
                return;

        /* Dump basic MII register set */
        for ( i = 0 ; i < 16 ; i++ ) {
                if ( ( i % 8 ) == 0 ) {
                        DBGC ( mii, "MII %p registers %02x-%02x:",
                               mii, i, ( i + 7 ) );
                }
                data = mii_read ( mii, i );
                if ( data >= 0 ) {
                        DBGC ( mii, " %04x", data );
                } else {
                        DBGC ( mii, " XXXX" );
                }
                if ( ( i % 8 ) == 7 )
                        DBGC ( mii, "\n" );
        }
}
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;
}