3 mii.c: MII interface library
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
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 static u32 mii_get_an(struct mii_if_info *mii, u16 addr)
40 advert = mii->mdio_read(mii->dev, mii->phy_id, addr);
42 return mii_lpa_to_ethtool_lpa_t(advert);
46 * mii_ethtool_gset - get settings that are specified in @ecmd
48 * @ecmd: requested ethtool_cmd
50 * The @ecmd parameter is expected to have been cleared before calling
53 * Returns 0 for success, negative on error.
55 int mii_ethtool_gset(struct mii_if_info *mii, struct ethtool_cmd *ecmd)
57 struct net_device *dev = mii->dev;
58 u16 bmcr, bmsr, ctrl1000 = 0, stat1000 = 0;
62 (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
63 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
64 SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII);
65 if (mii->supports_gmii)
66 ecmd->supported |= SUPPORTED_1000baseT_Half |
67 SUPPORTED_1000baseT_Full;
69 /* only supports twisted-pair */
70 ecmd->port = PORT_MII;
72 /* only supports internal transceiver */
73 ecmd->transceiver = XCVR_INTERNAL;
75 /* this isn't fully supported at higher layers */
76 ecmd->phy_address = mii->phy_id;
77 ecmd->mdio_support = ETH_MDIO_SUPPORTS_C22;
79 ecmd->advertising = ADVERTISED_TP | ADVERTISED_MII;
81 bmcr = mii->mdio_read(dev, mii->phy_id, MII_BMCR);
82 bmsr = mii->mdio_read(dev, mii->phy_id, MII_BMSR);
83 if (mii->supports_gmii) {
84 ctrl1000 = mii->mdio_read(dev, mii->phy_id, MII_CTRL1000);
85 stat1000 = mii->mdio_read(dev, mii->phy_id, MII_STAT1000);
88 ecmd->advertising |= mii_get_an(mii, MII_ADVERTISE);
89 if (mii->supports_gmii)
91 mii_ctrl1000_to_ethtool_adv_t(ctrl1000);
93 if (bmcr & BMCR_ANENABLE) {
94 ecmd->advertising |= ADVERTISED_Autoneg;
95 ecmd->autoneg = AUTONEG_ENABLE;
97 if (bmsr & BMSR_ANEGCOMPLETE) {
98 ecmd->lp_advertising = mii_get_an(mii, MII_LPA);
99 ecmd->lp_advertising |=
100 mii_stat1000_to_ethtool_lpa_t(stat1000);
102 ecmd->lp_advertising = 0;
105 nego = ecmd->advertising & ecmd->lp_advertising;
107 if (nego & (ADVERTISED_1000baseT_Full |
108 ADVERTISED_1000baseT_Half)) {
109 ethtool_cmd_speed_set(ecmd, SPEED_1000);
110 ecmd->duplex = !!(nego & ADVERTISED_1000baseT_Full);
111 } else if (nego & (ADVERTISED_100baseT_Full |
112 ADVERTISED_100baseT_Half)) {
113 ethtool_cmd_speed_set(ecmd, SPEED_100);
114 ecmd->duplex = !!(nego & ADVERTISED_100baseT_Full);
116 ethtool_cmd_speed_set(ecmd, SPEED_10);
117 ecmd->duplex = !!(nego & ADVERTISED_10baseT_Full);
120 ecmd->autoneg = AUTONEG_DISABLE;
122 ethtool_cmd_speed_set(ecmd,
123 ((bmcr & BMCR_SPEED1000 &&
124 (bmcr & BMCR_SPEED100) == 0) ?
126 ((bmcr & BMCR_SPEED100) ?
127 SPEED_100 : SPEED_10)));
128 ecmd->duplex = (bmcr & BMCR_FULLDPLX) ? DUPLEX_FULL : DUPLEX_HALF;
131 mii->full_duplex = ecmd->duplex;
133 /* ignore maxtxpkt, maxrxpkt for now */
139 * mii_ethtool_get_link_ksettings - get settings that are specified in @cmd
140 * @mii: MII interface
141 * @cmd: requested ethtool_link_ksettings
143 * The @cmd parameter is expected to have been cleared before calling
144 * mii_ethtool_get_link_ksettings().
146 void mii_ethtool_get_link_ksettings(struct mii_if_info *mii,
147 struct ethtool_link_ksettings *cmd)
149 struct net_device *dev = mii->dev;
150 u16 bmcr, bmsr, ctrl1000 = 0, stat1000 = 0;
151 u32 nego, supported, advertising, lp_advertising;
153 supported = (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
154 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
155 SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII);
156 if (mii->supports_gmii)
157 supported |= SUPPORTED_1000baseT_Half |
158 SUPPORTED_1000baseT_Full;
160 /* only supports twisted-pair */
161 cmd->base.port = PORT_MII;
163 /* this isn't fully supported at higher layers */
164 cmd->base.phy_address = mii->phy_id;
165 cmd->base.mdio_support = ETH_MDIO_SUPPORTS_C22;
167 advertising = ADVERTISED_TP | ADVERTISED_MII;
169 bmcr = mii->mdio_read(dev, mii->phy_id, MII_BMCR);
170 bmsr = mii->mdio_read(dev, mii->phy_id, MII_BMSR);
171 if (mii->supports_gmii) {
172 ctrl1000 = mii->mdio_read(dev, mii->phy_id, MII_CTRL1000);
173 stat1000 = mii->mdio_read(dev, mii->phy_id, MII_STAT1000);
176 advertising |= mii_get_an(mii, MII_ADVERTISE);
177 if (mii->supports_gmii)
178 advertising |= mii_ctrl1000_to_ethtool_adv_t(ctrl1000);
180 if (bmcr & BMCR_ANENABLE) {
181 advertising |= ADVERTISED_Autoneg;
182 cmd->base.autoneg = AUTONEG_ENABLE;
184 if (bmsr & BMSR_ANEGCOMPLETE) {
185 lp_advertising = mii_get_an(mii, MII_LPA);
187 mii_stat1000_to_ethtool_lpa_t(stat1000);
192 nego = advertising & lp_advertising;
194 if (nego & (ADVERTISED_1000baseT_Full |
195 ADVERTISED_1000baseT_Half)) {
196 cmd->base.speed = SPEED_1000;
197 cmd->base.duplex = !!(nego & ADVERTISED_1000baseT_Full);
198 } else if (nego & (ADVERTISED_100baseT_Full |
199 ADVERTISED_100baseT_Half)) {
200 cmd->base.speed = SPEED_100;
201 cmd->base.duplex = !!(nego & ADVERTISED_100baseT_Full);
203 cmd->base.speed = SPEED_10;
204 cmd->base.duplex = !!(nego & ADVERTISED_10baseT_Full);
207 cmd->base.autoneg = AUTONEG_DISABLE;
209 cmd->base.speed = ((bmcr & BMCR_SPEED1000 &&
210 (bmcr & BMCR_SPEED100) == 0) ?
212 ((bmcr & BMCR_SPEED100) ?
213 SPEED_100 : SPEED_10));
214 cmd->base.duplex = (bmcr & BMCR_FULLDPLX) ?
215 DUPLEX_FULL : DUPLEX_HALF;
220 mii->full_duplex = cmd->base.duplex;
222 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
224 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
226 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
229 /* ignore maxtxpkt, maxrxpkt for now */
233 * mii_ethtool_sset - set settings that are specified in @ecmd
234 * @mii: MII interface
235 * @ecmd: requested ethtool_cmd
237 * Returns 0 for success, negative on error.
239 int mii_ethtool_sset(struct mii_if_info *mii, struct ethtool_cmd *ecmd)
241 struct net_device *dev = mii->dev;
242 u32 speed = ethtool_cmd_speed(ecmd);
244 if (speed != SPEED_10 &&
245 speed != SPEED_100 &&
248 if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL)
250 if (ecmd->port != PORT_MII)
252 if (ecmd->transceiver != XCVR_INTERNAL)
254 if (ecmd->phy_address != mii->phy_id)
256 if (ecmd->autoneg != AUTONEG_DISABLE && ecmd->autoneg != AUTONEG_ENABLE)
258 if ((speed == SPEED_1000) && (!mii->supports_gmii))
261 /* ignore supported, maxtxpkt, maxrxpkt */
263 if (ecmd->autoneg == AUTONEG_ENABLE) {
264 u32 bmcr, advert, tmp;
265 u32 advert2 = 0, tmp2 = 0;
267 if ((ecmd->advertising & (ADVERTISED_10baseT_Half |
268 ADVERTISED_10baseT_Full |
269 ADVERTISED_100baseT_Half |
270 ADVERTISED_100baseT_Full |
271 ADVERTISED_1000baseT_Half |
272 ADVERTISED_1000baseT_Full)) == 0)
275 /* advertise only what has been requested */
276 advert = mii->mdio_read(dev, mii->phy_id, MII_ADVERTISE);
277 tmp = advert & ~(ADVERTISE_ALL | ADVERTISE_100BASE4);
278 if (mii->supports_gmii) {
279 advert2 = mii->mdio_read(dev, mii->phy_id, MII_CTRL1000);
280 tmp2 = advert2 & ~(ADVERTISE_1000HALF | ADVERTISE_1000FULL);
282 tmp |= ethtool_adv_to_mii_adv_t(ecmd->advertising);
284 if (mii->supports_gmii)
286 ethtool_adv_to_mii_ctrl1000_t(ecmd->advertising);
288 mii->mdio_write(dev, mii->phy_id, MII_ADVERTISE, tmp);
289 mii->advertising = tmp;
291 if ((mii->supports_gmii) && (advert2 != tmp2))
292 mii->mdio_write(dev, mii->phy_id, MII_CTRL1000, tmp2);
294 /* turn on autonegotiation, and force a renegotiate */
295 bmcr = mii->mdio_read(dev, mii->phy_id, MII_BMCR);
296 bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
297 mii->mdio_write(dev, mii->phy_id, MII_BMCR, bmcr);
299 mii->force_media = 0;
303 /* turn off auto negotiation, set speed and duplexity */
304 bmcr = mii->mdio_read(dev, mii->phy_id, MII_BMCR);
305 tmp = bmcr & ~(BMCR_ANENABLE | BMCR_SPEED100 |
306 BMCR_SPEED1000 | BMCR_FULLDPLX);
307 if (speed == SPEED_1000)
308 tmp |= BMCR_SPEED1000;
309 else if (speed == SPEED_100)
310 tmp |= BMCR_SPEED100;
311 if (ecmd->duplex == DUPLEX_FULL) {
312 tmp |= BMCR_FULLDPLX;
313 mii->full_duplex = 1;
315 mii->full_duplex = 0;
317 mii->mdio_write(dev, mii->phy_id, MII_BMCR, tmp);
319 mii->force_media = 1;
325 * mii_ethtool_set_link_ksettings - set settings that are specified in @cmd
326 * @mii: MII interfaces
327 * @cmd: requested ethtool_link_ksettings
329 * Returns 0 for success, negative on error.
331 int mii_ethtool_set_link_ksettings(struct mii_if_info *mii,
332 const struct ethtool_link_ksettings *cmd)
334 struct net_device *dev = mii->dev;
335 u32 speed = cmd->base.speed;
337 if (speed != SPEED_10 &&
338 speed != SPEED_100 &&
341 if (cmd->base.duplex != DUPLEX_HALF && cmd->base.duplex != DUPLEX_FULL)
343 if (cmd->base.port != PORT_MII)
345 if (cmd->base.phy_address != mii->phy_id)
347 if (cmd->base.autoneg != AUTONEG_DISABLE &&
348 cmd->base.autoneg != AUTONEG_ENABLE)
350 if ((speed == SPEED_1000) && (!mii->supports_gmii))
353 /* ignore supported, maxtxpkt, maxrxpkt */
355 if (cmd->base.autoneg == AUTONEG_ENABLE) {
356 u32 bmcr, advert, tmp;
357 u32 advert2 = 0, tmp2 = 0;
360 ethtool_convert_link_mode_to_legacy_u32(
361 &advertising, cmd->link_modes.advertising);
363 if ((advertising & (ADVERTISED_10baseT_Half |
364 ADVERTISED_10baseT_Full |
365 ADVERTISED_100baseT_Half |
366 ADVERTISED_100baseT_Full |
367 ADVERTISED_1000baseT_Half |
368 ADVERTISED_1000baseT_Full)) == 0)
371 /* advertise only what has been requested */
372 advert = mii->mdio_read(dev, mii->phy_id, MII_ADVERTISE);
373 tmp = advert & ~(ADVERTISE_ALL | ADVERTISE_100BASE4);
374 if (mii->supports_gmii) {
375 advert2 = mii->mdio_read(dev, mii->phy_id,
378 ~(ADVERTISE_1000HALF | ADVERTISE_1000FULL);
380 tmp |= ethtool_adv_to_mii_adv_t(advertising);
382 if (mii->supports_gmii)
383 tmp2 |= ethtool_adv_to_mii_ctrl1000_t(advertising);
385 mii->mdio_write(dev, mii->phy_id, MII_ADVERTISE, tmp);
386 mii->advertising = tmp;
388 if ((mii->supports_gmii) && (advert2 != tmp2))
389 mii->mdio_write(dev, mii->phy_id, MII_CTRL1000, tmp2);
391 /* turn on autonegotiation, and force a renegotiate */
392 bmcr = mii->mdio_read(dev, mii->phy_id, MII_BMCR);
393 bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
394 mii->mdio_write(dev, mii->phy_id, MII_BMCR, bmcr);
396 mii->force_media = 0;
400 /* turn off auto negotiation, set speed and duplexity */
401 bmcr = mii->mdio_read(dev, mii->phy_id, MII_BMCR);
402 tmp = bmcr & ~(BMCR_ANENABLE | BMCR_SPEED100 |
403 BMCR_SPEED1000 | BMCR_FULLDPLX);
404 if (speed == SPEED_1000)
405 tmp |= BMCR_SPEED1000;
406 else if (speed == SPEED_100)
407 tmp |= BMCR_SPEED100;
408 if (cmd->base.duplex == DUPLEX_FULL) {
409 tmp |= BMCR_FULLDPLX;
410 mii->full_duplex = 1;
412 mii->full_duplex = 0;
415 mii->mdio_write(dev, mii->phy_id, MII_BMCR, tmp);
417 mii->force_media = 1;
423 * mii_check_gmii_support - check if the MII supports Gb interfaces
424 * @mii: the MII interface
426 int mii_check_gmii_support(struct mii_if_info *mii)
430 reg = mii->mdio_read(mii->dev, mii->phy_id, MII_BMSR);
431 if (reg & BMSR_ESTATEN) {
432 reg = mii->mdio_read(mii->dev, mii->phy_id, MII_ESTATUS);
433 if (reg & (ESTATUS_1000_TFULL | ESTATUS_1000_THALF))
441 * mii_link_ok - is link status up/ok
442 * @mii: the MII interface
444 * Returns 1 if the MII reports link status up/ok, 0 otherwise.
446 int mii_link_ok (struct mii_if_info *mii)
448 /* first, a dummy read, needed to latch some MII phys */
449 mii->mdio_read(mii->dev, mii->phy_id, MII_BMSR);
450 if (mii->mdio_read(mii->dev, mii->phy_id, MII_BMSR) & BMSR_LSTATUS)
456 * mii_nway_restart - restart NWay (autonegotiation) for this interface
457 * @mii: the MII interface
459 * Returns 0 on success, negative on error.
461 int mii_nway_restart (struct mii_if_info *mii)
466 /* if autoneg is off, it's an error */
467 bmcr = mii->mdio_read(mii->dev, mii->phy_id, MII_BMCR);
469 if (bmcr & BMCR_ANENABLE) {
470 bmcr |= BMCR_ANRESTART;
471 mii->mdio_write(mii->dev, mii->phy_id, MII_BMCR, bmcr);
479 * mii_check_link - check MII link status
480 * @mii: MII interface
482 * If the link status changed (previous != current), call
483 * netif_carrier_on() if current link status is Up or call
484 * netif_carrier_off() if current link status is Down.
486 void mii_check_link (struct mii_if_info *mii)
488 int cur_link = mii_link_ok(mii);
489 int prev_link = netif_carrier_ok(mii->dev);
491 if (cur_link && !prev_link)
492 netif_carrier_on(mii->dev);
493 else if (prev_link && !cur_link)
494 netif_carrier_off(mii->dev);
498 * mii_check_media - check the MII interface for a carrier/speed/duplex change
499 * @mii: the MII interface
500 * @ok_to_print: OK to print link up/down messages
501 * @init_media: OK to save duplex mode in @mii
503 * Returns 1 if the duplex mode changed, 0 if not.
504 * If the media type is forced, always returns 0.
506 unsigned int mii_check_media (struct mii_if_info *mii,
507 unsigned int ok_to_print,
508 unsigned int init_media)
510 unsigned int old_carrier, new_carrier;
511 int advertise, lpa, media, duplex;
514 /* check current and old link status */
515 old_carrier = netif_carrier_ok(mii->dev) ? 1 : 0;
516 new_carrier = (unsigned int) mii_link_ok(mii);
518 /* if carrier state did not change, this is a "bounce",
519 * just exit as everything is already set correctly
521 if ((!init_media) && (old_carrier == new_carrier))
522 return 0; /* duplex did not change */
524 /* no carrier, nothing much to do */
526 netif_carrier_off(mii->dev);
528 netdev_info(mii->dev, "link down\n");
529 return 0; /* duplex did not change */
533 * we have carrier, see who's on the other end
535 netif_carrier_on(mii->dev);
537 if (mii->force_media) {
539 netdev_info(mii->dev, "link up\n");
540 return 0; /* duplex did not change */
543 /* get MII advertise and LPA values */
544 if ((!init_media) && (mii->advertising))
545 advertise = mii->advertising;
547 advertise = mii->mdio_read(mii->dev, mii->phy_id, MII_ADVERTISE);
548 mii->advertising = advertise;
550 lpa = mii->mdio_read(mii->dev, mii->phy_id, MII_LPA);
551 if (mii->supports_gmii)
552 lpa2 = mii->mdio_read(mii->dev, mii->phy_id, MII_STAT1000);
554 /* figure out media and duplex from advertise and LPA values */
555 media = mii_nway_result(lpa & advertise);
556 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
557 if (lpa2 & LPA_1000FULL)
561 netdev_info(mii->dev, "link up, %uMbps, %s-duplex, lpa 0x%04X\n",
562 lpa2 & (LPA_1000FULL | LPA_1000HALF) ? 1000 :
563 media & (ADVERTISE_100FULL | ADVERTISE_100HALF) ?
565 duplex ? "full" : "half",
568 if ((init_media) || (mii->full_duplex != duplex)) {
569 mii->full_duplex = duplex;
570 return 1; /* duplex changed */
573 return 0; /* duplex did not change */
577 * generic_mii_ioctl - main MII ioctl interface
578 * @mii_if: the MII interface
579 * @mii_data: MII ioctl data structure
580 * @cmd: MII ioctl command
581 * @duplex_chg_out: pointer to @duplex_changed status if there was no
584 * Returns 0 on success, negative on error.
586 int generic_mii_ioctl(struct mii_if_info *mii_if,
587 struct mii_ioctl_data *mii_data, int cmd,
588 unsigned int *duplex_chg_out)
591 unsigned int duplex_changed = 0;
596 mii_data->phy_id &= mii_if->phy_id_mask;
597 mii_data->reg_num &= mii_if->reg_num_mask;
601 mii_data->phy_id = mii_if->phy_id;
606 mii_if->mdio_read(mii_if->dev, mii_data->phy_id,
611 u16 val = mii_data->val_in;
613 if (mii_data->phy_id == mii_if->phy_id) {
614 switch(mii_data->reg_num) {
616 unsigned int new_duplex = 0;
617 if (val & (BMCR_RESET|BMCR_ANENABLE))
618 mii_if->force_media = 0;
620 mii_if->force_media = 1;
621 if (mii_if->force_media &&
622 (val & BMCR_FULLDPLX))
624 if (mii_if->full_duplex != new_duplex) {
626 mii_if->full_duplex = new_duplex;
631 mii_if->advertising = val;
639 mii_if->mdio_write(mii_if->dev, mii_data->phy_id,
640 mii_data->reg_num, val);
649 if ((rc == 0) && (duplex_chg_out) && (duplex_changed))
656 MODULE_DESCRIPTION ("MII hardware support library");
657 MODULE_LICENSE("GPL");
659 EXPORT_SYMBOL(mii_link_ok);
660 EXPORT_SYMBOL(mii_nway_restart);
661 EXPORT_SYMBOL(mii_ethtool_gset);
662 EXPORT_SYMBOL(mii_ethtool_get_link_ksettings);
663 EXPORT_SYMBOL(mii_ethtool_sset);
664 EXPORT_SYMBOL(mii_ethtool_set_link_ksettings);
665 EXPORT_SYMBOL(mii_check_link);
666 EXPORT_SYMBOL(mii_check_media);
667 EXPORT_SYMBOL(mii_check_gmii_support);
668 EXPORT_SYMBOL(generic_mii_ioctl);