2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
33 * Media Independent Interface
38 * Restart autonegotiation
40 * @v mii MII interface
41 * @ret rc Return status code
43 int mii_restart ( struct mii_interface *mii ) {
48 bmcr = mii_read ( mii, MII_BMCR );
51 DBGC ( mii, "MII %p could not read BMCR: %s\n",
52 mii, strerror ( rc ) );
56 /* Enable and restart autonegotiation */
57 bmcr |= ( BMCR_ANENABLE | BMCR_ANRESTART );
58 if ( ( rc = mii_write ( mii, MII_BMCR, bmcr ) ) != 0 ) {
59 DBGC ( mii, "MII %p could not write BMCR: %s\n",
60 mii, strerror ( rc ) );
64 DBGC ( mii, "MII %p restarted autonegotiation\n", mii );
71 * @v mii MII interface
72 * @ret rc Return status code
74 int mii_reset ( struct mii_interface *mii ) {
79 /* Power-up, enable autonegotiation and initiate reset */
80 if ( ( rc = mii_write ( mii, MII_BMCR,
81 ( BMCR_RESET | BMCR_ANENABLE ) ) ) != 0 ) {
82 DBGC ( mii, "MII %p could not write BMCR: %s\n",
83 mii, strerror ( rc ) );
87 /* Wait for reset to complete */
88 for ( i = 0 ; i < MII_RESET_MAX_WAIT_MS ; i++ ) {
90 /* Check if reset has completed */
91 bmcr = mii_read ( mii, MII_BMCR );
94 DBGC ( mii, "MII %p could not read BMCR: %s\n",
95 mii, strerror ( rc ) );
99 /* If reset is not complete, delay 1ms and retry */
100 if ( bmcr & BMCR_RESET ) {
105 /* Force autonegotation on again, in case it was
106 * cleared by the reset.
108 if ( ( rc = mii_restart ( mii ) ) != 0 )
111 DBGC ( mii, "MII %p reset after %dms\n", mii, i );
115 DBGC ( mii, "MII %p timed out waiting for reset\n", mii );
120 * Update link status via MII
122 * @v mii MII interface
123 * @v netdev Network device
124 * @ret rc Return status code
126 int mii_check_link ( struct mii_interface *mii, struct net_device *netdev ) {
132 bmsr = mii_read ( mii, MII_BMSR );
138 /* Report link status */
139 link = ( bmsr & BMSR_LSTATUS );
140 DBGC ( mii, "MII %p link %s (BMSR %#04x)\n",
141 mii, ( link ? "up" : "down" ), bmsr );
143 netdev_link_up ( netdev );
145 netdev_link_down ( netdev );