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
20 FILE_LICENCE ( GPL2_OR_LATER );
29 * Media Independent Interface
34 * Restart autonegotiation
36 * @v mii MII interface
37 * @ret rc Return status code
39 int mii_restart ( struct mii_interface *mii ) {
44 bmcr = mii_read ( mii, MII_BMCR );
47 DBGC ( mii, "MII %p could not read BMCR: %s\n",
48 mii, strerror ( rc ) );
52 /* Enable and restart autonegotiation */
53 bmcr |= ( BMCR_ANENABLE | BMCR_ANRESTART );
54 if ( ( rc = mii_write ( mii, MII_BMCR, bmcr ) ) != 0 ) {
55 DBGC ( mii, "MII %p could not write BMCR: %s\n",
56 mii, strerror ( rc ) );
60 DBGC ( mii, "MII %p restarted autonegotiation\n", mii );
67 * @v mii MII interface
68 * @ret rc Return status code
70 int mii_reset ( struct mii_interface *mii ) {
75 /* Power-up, enable autonegotiation and initiate reset */
76 if ( ( rc = mii_write ( mii, MII_BMCR,
77 ( BMCR_RESET | BMCR_ANENABLE ) ) ) != 0 ) {
78 DBGC ( mii, "MII %p could not write BMCR: %s\n",
79 mii, strerror ( rc ) );
83 /* Wait for reset to complete */
84 for ( i = 0 ; i < MII_RESET_MAX_WAIT_MS ; i++ ) {
86 /* Check if reset has completed */
87 bmcr = mii_read ( mii, MII_BMCR );
90 DBGC ( mii, "MII %p could not read BMCR: %s\n",
91 mii, strerror ( rc ) );
95 /* If reset is not complete, delay 1ms and retry */
96 if ( bmcr & BMCR_RESET ) {
101 /* Force autonegotation on again, in case it was
102 * cleared by the reset.
104 if ( ( rc = mii_restart ( mii ) ) != 0 )
107 DBGC ( mii, "MII %p reset after %dms\n", mii, i );
111 DBGC ( mii, "MII %p timed out waiting for reset\n", mii );