3 mii.c: MII interface library
5 Maintained by Jeff Garzik <jgarzik@pobox.com>
6 Copyright 2001,2002 Jeff Garzik
8 Various code came from myson803.c and other files by
9 Donald Becker. Copyright:
11 Written 1998-2002 by Donald Becker.
13 This software may be used and distributed according
14 to the terms of the GNU General Public License (GPL),
15 incorporated herein by reference. Drivers based on
16 or derived from this code fall under the GPL and must
17 retain the authorship, copyright and license notice.
18 This file is not a complete program and may only be
19 used when the entire operating system is licensed
22 The author may be reached as becker@scyld.com, or C/O
23 Scyld Computing Corporation
24 410 Severn Ave., Suite 210
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/netdevice.h>
33 #include <linux/ethtool.h>
34 #include <linux/mii.h>
36 int mii_ethtool_gset(struct mii_if_info *mii, struct ethtool_cmd *ecmd)
38 struct net_device *dev = mii->dev;
39 u32 advert, bmcr, lpa, nego;
40 u32 advert2 = 0, bmcr2 = 0, lpa2 = 0;
43 (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
44 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
45 SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII);
46 if (mii->supports_gmii)
47 ecmd->supported |= SUPPORTED_1000baseT_Half |
48 SUPPORTED_1000baseT_Full;
50 /* only supports twisted-pair */
51 ecmd->port = PORT_MII;
53 /* only supports internal transceiver */
54 ecmd->transceiver = XCVR_INTERNAL;
56 /* this isn't fully supported at higher layers */
57 ecmd->phy_address = mii->phy_id;
59 ecmd->advertising = ADVERTISED_TP | ADVERTISED_MII;
60 advert = mii->mdio_read(dev, mii->phy_id, MII_ADVERTISE);
61 if (mii->supports_gmii)
62 advert2 = mii->mdio_read(dev, mii->phy_id, MII_CTRL1000);
64 if (advert & ADVERTISE_10HALF)
65 ecmd->advertising |= ADVERTISED_10baseT_Half;
66 if (advert & ADVERTISE_10FULL)
67 ecmd->advertising |= ADVERTISED_10baseT_Full;
68 if (advert & ADVERTISE_100HALF)
69 ecmd->advertising |= ADVERTISED_100baseT_Half;
70 if (advert & ADVERTISE_100FULL)
71 ecmd->advertising |= ADVERTISED_100baseT_Full;
72 if (advert2 & ADVERTISE_1000HALF)
73 ecmd->advertising |= ADVERTISED_1000baseT_Half;
74 if (advert2 & ADVERTISE_1000FULL)
75 ecmd->advertising |= ADVERTISED_1000baseT_Full;
77 bmcr = mii->mdio_read(dev, mii->phy_id, MII_BMCR);
78 lpa = mii->mdio_read(dev, mii->phy_id, MII_LPA);
79 if (mii->supports_gmii) {
80 bmcr2 = mii->mdio_read(dev, mii->phy_id, MII_CTRL1000);
81 lpa2 = mii->mdio_read(dev, mii->phy_id, MII_STAT1000);
83 if (bmcr & BMCR_ANENABLE) {
84 ecmd->advertising |= ADVERTISED_Autoneg;
85 ecmd->autoneg = AUTONEG_ENABLE;
87 nego = mii_nway_result(advert & lpa);
88 if ((bmcr2 & (ADVERTISE_1000HALF | ADVERTISE_1000FULL)) &
90 ecmd->speed = SPEED_1000;
91 else if (nego == LPA_100FULL || nego == LPA_100HALF)
92 ecmd->speed = SPEED_100;
94 ecmd->speed = SPEED_10;
95 if ((lpa2 & LPA_1000FULL) || nego == LPA_100FULL ||
97 ecmd->duplex = DUPLEX_FULL;
100 ecmd->duplex = DUPLEX_HALF;
101 mii->full_duplex = 0;
104 ecmd->autoneg = AUTONEG_DISABLE;
106 ecmd->speed = ((bmcr & BMCR_SPEED1000 &&
107 (bmcr & BMCR_SPEED100) == 0) ? SPEED_1000 :
108 (bmcr & BMCR_SPEED100) ? SPEED_100 : SPEED_10);
109 ecmd->duplex = (bmcr & BMCR_FULLDPLX) ? DUPLEX_FULL : DUPLEX_HALF;
112 /* ignore maxtxpkt, maxrxpkt for now */
117 int mii_ethtool_sset(struct mii_if_info *mii, struct ethtool_cmd *ecmd)
119 struct net_device *dev = mii->dev;
121 if (ecmd->speed != SPEED_10 &&
122 ecmd->speed != SPEED_100 &&
123 ecmd->speed != SPEED_1000)
125 if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL)
127 if (ecmd->port != PORT_MII)
129 if (ecmd->transceiver != XCVR_INTERNAL)
131 if (ecmd->phy_address != mii->phy_id)
133 if (ecmd->autoneg != AUTONEG_DISABLE && ecmd->autoneg != AUTONEG_ENABLE)
135 if ((ecmd->speed == SPEED_1000) && (!mii->supports_gmii))
138 /* ignore supported, maxtxpkt, maxrxpkt */
140 if (ecmd->autoneg == AUTONEG_ENABLE) {
141 u32 bmcr, advert, tmp;
142 u32 advert2 = 0, tmp2 = 0;
144 if ((ecmd->advertising & (ADVERTISED_10baseT_Half |
145 ADVERTISED_10baseT_Full |
146 ADVERTISED_100baseT_Half |
147 ADVERTISED_100baseT_Full |
148 ADVERTISED_1000baseT_Half |
149 ADVERTISED_1000baseT_Full)) == 0)
152 /* advertise only what has been requested */
153 advert = mii->mdio_read(dev, mii->phy_id, MII_ADVERTISE);
154 tmp = advert & ~(ADVERTISE_ALL | ADVERTISE_100BASE4);
155 if (mii->supports_gmii) {
156 advert2 = mii->mdio_read(dev, mii->phy_id, MII_CTRL1000);
157 tmp2 = advert2 & ~(ADVERTISE_1000HALF | ADVERTISE_1000FULL);
159 if (ecmd->advertising & ADVERTISED_10baseT_Half)
160 tmp |= ADVERTISE_10HALF;
161 if (ecmd->advertising & ADVERTISED_10baseT_Full)
162 tmp |= ADVERTISE_10FULL;
163 if (ecmd->advertising & ADVERTISED_100baseT_Half)
164 tmp |= ADVERTISE_100HALF;
165 if (ecmd->advertising & ADVERTISED_100baseT_Full)
166 tmp |= ADVERTISE_100FULL;
167 if (mii->supports_gmii) {
168 if (ecmd->advertising & ADVERTISED_1000baseT_Half)
169 tmp2 |= ADVERTISE_1000HALF;
170 if (ecmd->advertising & ADVERTISED_1000baseT_Full)
171 tmp2 |= ADVERTISE_1000FULL;
174 mii->mdio_write(dev, mii->phy_id, MII_ADVERTISE, tmp);
175 mii->advertising = tmp;
177 if ((mii->supports_gmii) && (advert2 != tmp2))
178 mii->mdio_write(dev, mii->phy_id, MII_CTRL1000, tmp2);
180 /* turn on autonegotiation, and force a renegotiate */
181 bmcr = mii->mdio_read(dev, mii->phy_id, MII_BMCR);
182 bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
183 mii->mdio_write(dev, mii->phy_id, MII_BMCR, bmcr);
185 mii->force_media = 0;
189 /* turn off auto negotiation, set speed and duplexity */
190 bmcr = mii->mdio_read(dev, mii->phy_id, MII_BMCR);
191 tmp = bmcr & ~(BMCR_ANENABLE | BMCR_SPEED100 |
192 BMCR_SPEED1000 | BMCR_FULLDPLX);
193 if (ecmd->speed == SPEED_1000)
194 tmp |= BMCR_SPEED1000;
195 else if (ecmd->speed == SPEED_100)
196 tmp |= BMCR_SPEED100;
197 if (ecmd->duplex == DUPLEX_FULL) {
198 tmp |= BMCR_FULLDPLX;
199 mii->full_duplex = 1;
201 mii->full_duplex = 0;
203 mii->mdio_write(dev, mii->phy_id, MII_BMCR, tmp);
205 mii->force_media = 1;
210 int mii_link_ok (struct mii_if_info *mii)
212 /* first, a dummy read, needed to latch some MII phys */
213 mii->mdio_read(mii->dev, mii->phy_id, MII_BMSR);
214 if (mii->mdio_read(mii->dev, mii->phy_id, MII_BMSR) & BMSR_LSTATUS)
219 int mii_nway_restart (struct mii_if_info *mii)
224 /* if autoneg is off, it's an error */
225 bmcr = mii->mdio_read(mii->dev, mii->phy_id, MII_BMCR);
227 if (bmcr & BMCR_ANENABLE) {
228 bmcr |= BMCR_ANRESTART;
229 mii->mdio_write(mii->dev, mii->phy_id, MII_BMCR, bmcr);
236 void mii_check_link (struct mii_if_info *mii)
238 int cur_link = mii_link_ok(mii);
239 int prev_link = netif_carrier_ok(mii->dev);
241 if (cur_link && !prev_link)
242 netif_carrier_on(mii->dev);
243 else if (prev_link && !cur_link)
244 netif_carrier_off(mii->dev);
247 unsigned int mii_check_media (struct mii_if_info *mii,
248 unsigned int ok_to_print,
249 unsigned int init_media)
251 unsigned int old_carrier, new_carrier;
252 int advertise, lpa, media, duplex;
255 /* if forced media, go no further */
256 if (mii->force_media)
257 return 0; /* duplex did not change */
259 /* check current and old link status */
260 old_carrier = netif_carrier_ok(mii->dev) ? 1 : 0;
261 new_carrier = (unsigned int) mii_link_ok(mii);
263 /* if carrier state did not change, this is a "bounce",
264 * just exit as everything is already set correctly
266 if ((!init_media) && (old_carrier == new_carrier))
267 return 0; /* duplex did not change */
269 /* no carrier, nothing much to do */
271 netif_carrier_off(mii->dev);
273 printk(KERN_INFO "%s: link down\n", mii->dev->name);
274 return 0; /* duplex did not change */
278 * we have carrier, see who's on the other end
280 netif_carrier_on(mii->dev);
282 /* get MII advertise and LPA values */
283 if ((!init_media) && (mii->advertising))
284 advertise = mii->advertising;
286 advertise = mii->mdio_read(mii->dev, mii->phy_id, MII_ADVERTISE);
287 mii->advertising = advertise;
289 lpa = mii->mdio_read(mii->dev, mii->phy_id, MII_LPA);
290 if (mii->supports_gmii)
291 lpa2 = mii->mdio_read(mii->dev, mii->phy_id, MII_STAT1000);
293 /* figure out media and duplex from advertise and LPA values */
294 media = mii_nway_result(lpa & advertise);
295 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
296 if (lpa2 & LPA_1000FULL)
300 printk(KERN_INFO "%s: link up, %sMbps, %s-duplex, lpa 0x%04X\n",
302 lpa2 & (LPA_1000FULL | LPA_1000HALF) ? "1000" :
303 media & (ADVERTISE_100FULL | ADVERTISE_100HALF) ? "100" : "10",
304 duplex ? "full" : "half",
307 if ((init_media) || (mii->full_duplex != duplex)) {
308 mii->full_duplex = duplex;
309 return 1; /* duplex changed */
312 return 0; /* duplex did not change */
315 int generic_mii_ioctl(struct mii_if_info *mii_if,
316 struct mii_ioctl_data *mii_data, int cmd,
317 unsigned int *duplex_chg_out)
320 unsigned int duplex_changed = 0;
325 mii_data->phy_id &= mii_if->phy_id_mask;
326 mii_data->reg_num &= mii_if->reg_num_mask;
330 mii_data->phy_id = mii_if->phy_id;
335 mii_if->mdio_read(mii_if->dev, mii_data->phy_id,
340 u16 val = mii_data->val_in;
342 if (!capable(CAP_NET_ADMIN))
345 if (mii_data->phy_id == mii_if->phy_id) {
346 switch(mii_data->reg_num) {
348 unsigned int new_duplex = 0;
349 if (val & (BMCR_RESET|BMCR_ANENABLE))
350 mii_if->force_media = 0;
352 mii_if->force_media = 1;
353 if (mii_if->force_media &&
354 (val & BMCR_FULLDPLX))
356 if (mii_if->full_duplex != new_duplex) {
358 mii_if->full_duplex = new_duplex;
363 mii_if->advertising = val;
371 mii_if->mdio_write(mii_if->dev, mii_data->phy_id,
372 mii_data->reg_num, val);
381 if ((rc == 0) && (duplex_chg_out) && (duplex_changed))
387 MODULE_AUTHOR ("Jeff Garzik <jgarzik@pobox.com>");
388 MODULE_DESCRIPTION ("MII hardware support library");
389 MODULE_LICENSE("GPL");
391 EXPORT_SYMBOL(mii_link_ok);
392 EXPORT_SYMBOL(mii_nway_restart);
393 EXPORT_SYMBOL(mii_ethtool_gset);
394 EXPORT_SYMBOL(mii_ethtool_sset);
395 EXPORT_SYMBOL(mii_check_link);
396 EXPORT_SYMBOL(mii_check_media);
397 EXPORT_SYMBOL(generic_mii_ioctl);