These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / drivers / net / mii.c
1 /*
2  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  *
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.
22  */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <string.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <ipxe/mii.h>
30
31 /** @file
32  *
33  * Media Independent Interface
34  *
35  */
36
37 /**
38  * Restart autonegotiation
39  *
40  * @v mii               MII interface
41  * @ret rc              Return status code
42  */
43 int mii_restart ( struct mii_interface *mii ) {
44         int bmcr;
45         int rc;
46
47         /* Read BMCR */
48         bmcr = mii_read ( mii, MII_BMCR );
49         if ( bmcr < 0 ) {
50                 rc = bmcr;
51                 DBGC ( mii, "MII %p could not read BMCR: %s\n",
52                        mii, strerror ( rc ) );
53                 return rc;
54         }
55
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 ) );
61                 return rc;
62         }
63
64         DBGC ( mii, "MII %p restarted autonegotiation\n", mii );
65         return 0;
66 }
67
68 /**
69  * Reset MII interface
70  *
71  * @v mii               MII interface
72  * @ret rc              Return status code
73  */
74 int mii_reset ( struct mii_interface *mii ) {
75         unsigned int i;
76         int bmcr;
77         int rc;
78
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 ) );
84                 return rc;
85         }
86
87         /* Wait for reset to complete */
88         for ( i = 0 ; i < MII_RESET_MAX_WAIT_MS ; i++ ) {
89
90                 /* Check if reset has completed */
91                 bmcr = mii_read ( mii, MII_BMCR );
92                 if ( bmcr < 0 ) {
93                         rc = bmcr;
94                         DBGC ( mii, "MII %p could not read BMCR: %s\n",
95                                mii, strerror ( rc ) );
96                         return rc;
97                 }
98
99                 /* If reset is not complete, delay 1ms and retry */
100                 if ( bmcr & BMCR_RESET ) {
101                         mdelay ( 1 );
102                         continue;
103                 }
104
105                 /* Force autonegotation on again, in case it was
106                  * cleared by the reset.
107                  */
108                 if ( ( rc = mii_restart ( mii ) ) != 0 )
109                         return rc;
110
111                 DBGC ( mii, "MII %p reset after %dms\n", mii, i );
112                 return 0;
113         }
114
115         DBGC ( mii, "MII %p timed out waiting for reset\n", mii );
116         return -ETIMEDOUT;
117 }
118
119 /**
120  * Update link status via MII
121  *
122  * @v mii               MII interface
123  * @v netdev            Network device
124  * @ret rc              Return status code
125  */
126 int mii_check_link ( struct mii_interface *mii, struct net_device *netdev ) {
127         int bmsr;
128         int link;
129         int rc;
130
131         /* Read BMSR */
132         bmsr = mii_read ( mii, MII_BMSR );
133         if ( bmsr < 0 ) {
134                 rc = bmsr;
135                 return rc;
136         }
137
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 );
142         if ( link ) {
143                 netdev_link_up ( netdev );
144         } else {
145                 netdev_link_down ( netdev );
146         }
147
148         return 0;
149 }