2 * Generic PHY Management code
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
7 * the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU 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., 59 Temple Place, Suite 330, Boston,
20 * Copyright 2011 Freescale Semiconductor, Inc.
23 * Based loosely off of Linux's PHY Lib
35 /* Generic PHY support and helper functions */
38 * genphy_config_advert - sanitize and advertise auto-negotation parameters
39 * @phydev: target phy_device struct
41 * Description: Writes MII_ADVERTISE with the appropriate values,
42 * after sanitizing the values to make sure we only advertise
43 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
44 * hasn't changed, and > 0 if it has changed.
46 static int genphy_config_advert(struct phy_device *phydev)
52 /* Only allow advertising what
53 * this PHY supports */
54 phydev->advertising &= phydev->supported;
55 advertise = phydev->advertising;
57 /* Setup standard advertisement */
58 oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
63 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
64 ADVERTISE_PAUSE_ASYM);
65 if (advertise & ADVERTISED_10baseT_Half)
66 adv |= ADVERTISE_10HALF;
67 if (advertise & ADVERTISED_10baseT_Full)
68 adv |= ADVERTISE_10FULL;
69 if (advertise & ADVERTISED_100baseT_Half)
70 adv |= ADVERTISE_100HALF;
71 if (advertise & ADVERTISED_100baseT_Full)
72 adv |= ADVERTISE_100FULL;
73 if (advertise & ADVERTISED_Pause)
74 adv |= ADVERTISE_PAUSE_CAP;
75 if (advertise & ADVERTISED_Asym_Pause)
76 adv |= ADVERTISE_PAUSE_ASYM;
79 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv);
86 /* Configure gigabit if it's supported */
87 if (phydev->supported & (SUPPORTED_1000baseT_Half |
88 SUPPORTED_1000baseT_Full)) {
89 oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
94 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
95 if (advertise & SUPPORTED_1000baseT_Half)
96 adv |= ADVERTISE_1000HALF;
97 if (advertise & SUPPORTED_1000baseT_Full)
98 adv |= ADVERTISE_1000FULL;
101 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000,
115 * genphy_setup_forced - configures/forces speed/duplex from @phydev
116 * @phydev: target phy_device struct
118 * Description: Configures MII_BMCR to force speed/duplex
119 * to the values in phydev. Assumes that the values are valid.
121 static int genphy_setup_forced(struct phy_device *phydev)
125 phydev->pause = phydev->asym_pause = 0;
127 if (SPEED_1000 == phydev->speed)
128 ctl |= BMCR_SPEED1000;
129 else if (SPEED_100 == phydev->speed)
130 ctl |= BMCR_SPEED100;
132 if (DUPLEX_FULL == phydev->duplex)
133 ctl |= BMCR_FULLDPLX;
135 return phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
140 * genphy_restart_aneg - Enable and Restart Autonegotiation
141 * @phydev: target phy_device struct
143 int genphy_restart_aneg(struct phy_device *phydev)
147 ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
152 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
154 /* Don't isolate the PHY if we're negotiating */
155 ctl &= ~(BMCR_ISOLATE);
157 return phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
162 * genphy_config_aneg - restart auto-negotiation or write BMCR
163 * @phydev: target phy_device struct
165 * Description: If auto-negotiation is enabled, we configure the
166 * advertising, and then restart auto-negotiation. If it is not
167 * enabled, then we write the BMCR.
169 int genphy_config_aneg(struct phy_device *phydev)
173 if (AUTONEG_ENABLE != phydev->autoneg)
174 return genphy_setup_forced(phydev);
176 result = genphy_config_advert(phydev);
178 if (result < 0) /* error */
182 /* Advertisment hasn't changed, but maybe aneg was never on to
183 * begin with? Or maybe phy was isolated? */
184 int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
189 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
190 result = 1; /* do restart aneg */
193 /* Only restart aneg if we are advertising something different
194 * than we were before. */
196 result = genphy_restart_aneg(phydev);
202 * genphy_update_link - update link status in @phydev
203 * @phydev: target phy_device struct
205 * Description: Update the value in phydev->link to reflect the
206 * current link value. In order to do this, we need to read
207 * the status register twice, keeping the second value.
209 int genphy_update_link(struct phy_device *phydev)
215 * Wait if the link is up, and autonegotiation is in progress
216 * (ie - we're capable and it's not done)
218 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
223 * If we already saw the link up, and it hasn't gone down, then
224 * we don't need to wait for autoneg again
226 if (phydev->link && mii_reg & BMSR_LSTATUS)
229 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
233 if (!(bmcr & BMCR_ANENABLE))
236 if ((mii_reg & BMSR_ANEGCAPABLE) && !(mii_reg & BMSR_ANEGCOMPLETE)) {
239 printf("%s Waiting for PHY auto negotiation to complete",
241 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
245 if (i > PHY_ANEG_TIMEOUT) {
246 printf(" TIMEOUT !\n");
252 puts("user interrupt!\n");
257 if ((i++ % 500) == 0)
260 udelay(1000); /* 1 ms */
261 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
268 /* Read the link a second time to clear the latched state */
269 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
273 if (mii_reg & BMSR_LSTATUS)
283 * Generic function which updates the speed and duplex. If
284 * autonegotiation is enabled, it uses the AND of the link
285 * partner's advertised capabilities and our advertised
286 * capabilities. If autonegotiation is disabled, we use the
287 * appropriate bits in the control register.
289 * Stolen from Linux's mii.c and phy_device.c
291 static int genphy_parse_link(struct phy_device *phydev)
293 int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
298 /* We're using autonegotiation */
299 if (mii_reg & BMSR_ANEGCAPABLE) {
304 /* Check for gigabit capability */
305 if (mii_reg & BMSR_ERCAP) {
306 /* We want a list of states supported by
307 * both PHYs in the link
309 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
314 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
320 /* Set the baseline so we only have to set them
321 * if they're different
323 phydev->speed = SPEED_10;
324 phydev->duplex = DUPLEX_HALF;
326 /* Check the gigabit fields */
327 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
328 phydev->speed = SPEED_1000;
330 if (gblpa & PHY_1000BTSR_1000FD)
331 phydev->duplex = DUPLEX_FULL;
337 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
342 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
347 if (lpa & (LPA_100FULL | LPA_100HALF)) {
348 phydev->speed = SPEED_100;
350 if (lpa & LPA_100FULL)
351 phydev->duplex = DUPLEX_FULL;
353 } else if (lpa & LPA_10FULL)
354 phydev->duplex = DUPLEX_FULL;
356 int bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
361 phydev->speed = SPEED_10;
362 phydev->duplex = DUPLEX_HALF;
364 if (bmcr & BMCR_FULLDPLX)
365 phydev->duplex = DUPLEX_FULL;
367 if (bmcr & BMCR_SPEED1000)
368 phydev->speed = SPEED_1000;
369 else if (bmcr & BMCR_SPEED100)
370 phydev->speed = SPEED_100;
376 int genphy_config(struct phy_device *phydev)
381 /* For now, I'll claim that the generic driver supports
382 * all possible port types */
383 features = (SUPPORTED_TP | SUPPORTED_MII
384 | SUPPORTED_AUI | SUPPORTED_FIBRE |
387 /* Do we support autonegotiation? */
388 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
393 if (val & BMSR_ANEGCAPABLE)
394 features |= SUPPORTED_Autoneg;
396 if (val & BMSR_100FULL)
397 features |= SUPPORTED_100baseT_Full;
398 if (val & BMSR_100HALF)
399 features |= SUPPORTED_100baseT_Half;
400 if (val & BMSR_10FULL)
401 features |= SUPPORTED_10baseT_Full;
402 if (val & BMSR_10HALF)
403 features |= SUPPORTED_10baseT_Half;
405 if (val & BMSR_ESTATEN) {
406 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS);
411 if (val & ESTATUS_1000_TFULL)
412 features |= SUPPORTED_1000baseT_Full;
413 if (val & ESTATUS_1000_THALF)
414 features |= SUPPORTED_1000baseT_Half;
417 phydev->supported = features;
418 phydev->advertising = features;
420 genphy_config_aneg(phydev);
425 int genphy_startup(struct phy_device *phydev)
427 genphy_update_link(phydev);
428 genphy_parse_link(phydev);
433 int genphy_shutdown(struct phy_device *phydev)
438 static struct phy_driver genphy_driver = {
441 .name = "Generic PHY",
443 .config = genphy_config,
444 .startup = genphy_startup,
445 .shutdown = genphy_shutdown,
448 static LIST_HEAD(phy_drivers);
452 #ifdef CONFIG_PHY_ATHEROS
455 #ifdef CONFIG_PHY_BROADCOM
458 #ifdef CONFIG_PHY_DAVICOM
461 #ifdef CONFIG_PHY_LXT
464 #ifdef CONFIG_PHY_MARVELL
467 #ifdef CONFIG_PHY_MICREL
470 #ifdef CONFIG_PHY_NATSEMI
473 #ifdef CONFIG_PHY_REALTEK
476 #ifdef CONFIG_PHY_SMSC
479 #ifdef CONFIG_PHY_TERANETICS
480 phy_teranetics_init();
482 #ifdef CONFIG_PHY_VITESSE
489 int phy_register(struct phy_driver *drv)
491 INIT_LIST_HEAD(&drv->list);
492 list_add_tail(&drv->list, &phy_drivers);
497 static int phy_probe(struct phy_device *phydev)
501 phydev->advertising = phydev->supported = phydev->drv->features;
502 phydev->mmds = phydev->drv->mmds;
504 if (phydev->drv->probe)
505 err = phydev->drv->probe(phydev);
510 static struct phy_driver *generic_for_interface(phy_interface_t interface)
512 #ifdef CONFIG_PHYLIB_10G
513 if (is_10g_interface(interface))
514 return &gen10g_driver;
517 return &genphy_driver;
520 static struct phy_driver *get_phy_driver(struct phy_device *phydev,
521 phy_interface_t interface)
523 struct list_head *entry;
524 int phy_id = phydev->phy_id;
525 struct phy_driver *drv = NULL;
527 list_for_each(entry, &phy_drivers) {
528 drv = list_entry(entry, struct phy_driver, list);
529 if ((drv->uid & drv->mask) == (phy_id & drv->mask))
533 /* If we made it here, there's no driver for this PHY */
534 return generic_for_interface(interface);
537 static int aneg_enabled(struct phy_device *phydev)
539 static const char *aneg = "_aneg";
540 char varname[strlen(phydev->bus->name) + strlen(aneg) + 1];
542 snprintf(varname, sizeof(varname), "%s%s", phydev->bus->name, aneg);
543 return getenv_yesno(varname);
546 static int phy_get_speed(struct phy_device *phydev)
549 static const char *aneg = "_speed";
550 char varname[strlen(phydev->bus->name) + strlen(aneg) + 1];
553 snprintf(varname, sizeof(varname), "%s%s", phydev->bus->name, aneg);
555 val = getenv_ulong(varname, 10, 100);
567 printf("Improper setting '%s' for %s; assuming 100\n",
568 getenv(varname), varname);
574 static int phy_get_duplex(struct phy_device *phydev)
576 int ret = DUPLEX_FULL;
577 static const char *aneg = "_duplex";
578 char varname[strlen(phydev->bus->name) + strlen(aneg) + 4];
581 snprintf(varname, sizeof(varname), "%s%d%s",
582 phydev->bus->name, phydev->addr, aneg);
584 val = getenv(varname);
586 if (strcasecmp(val, "full") != 0) {
587 if (strcasecmp(val, "half") == 0) {
590 printf("Improper setting '%s' for %s; assuming 'full'\n",
592 printf("Expected one of: 'full', 'half'\n");
600 static struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
602 phy_interface_t interface)
604 struct phy_device *dev;
606 /* We allocate the device, and initialize the
608 dev = malloc(sizeof(*dev));
610 printf("Failed to allocate PHY device for %s:%d\n",
615 memset(dev, 0, sizeof(*dev));
619 dev->interface = interface;
622 dev->phy_id = phy_id;
625 dev->drv = get_phy_driver(dev, interface);
627 if (aneg_enabled(dev)) {
628 dev->autoneg = AUTONEG_ENABLE;
630 dev->autoneg = AUTONEG_DISABLE;
631 dev->speed = phy_get_speed(dev);
632 dev->duplex = phy_get_duplex(dev);
634 switch (dev->speed) {
636 if (dev->duplex == DUPLEX_FULL)
637 dev->supported &= SUPPORTED_1000baseT_Full;
639 dev->supported &= SUPPORTED_1000baseT_Half;
642 if (dev->duplex == DUPLEX_FULL)
643 dev->supported &= SUPPORTED_100baseT_Full;
645 dev->supported &= SUPPORTED_100baseT_Half;
648 if (dev->duplex == DUPLEX_FULL)
649 dev->supported &= SUPPORTED_10baseT_Full;
651 dev->supported &= SUPPORTED_10baseT_Half;
654 printf("Unsupported speed: %d\n", dev->speed);
660 bus->phymap[addr] = dev;
666 * get_phy_id - reads the specified addr for its ID.
667 * @bus: the target MII bus
668 * @addr: PHY address on the MII bus
669 * @phy_id: where to store the ID retrieved.
671 * Description: Reads the ID registers of the PHY at @addr on the
672 * @bus, stores it in @phy_id and returns zero on success.
674 static int get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
678 /* Grab the bits from PHYIR1, and put them
679 * in the upper half */
680 phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
685 *phy_id = (phy_reg & 0xffff) << 16;
687 /* Grab the bits from PHYIR2, and put them in the lower half */
688 phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
693 *phy_id |= (phy_reg & 0xffff);
699 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
700 * @bus: the target MII bus
701 * @addr: PHY address on the MII bus
703 * Description: Reads the ID registers of the PHY at @addr on the
704 * @bus, then allocates and returns the phy_device to represent it.
706 static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
707 phy_interface_t interface)
709 u32 phy_id = 0x1fffffff;
713 /* If we have one, return the existing device, with new interface */
714 if (bus->phymap[addr]) {
715 bus->phymap[addr]->interface = interface;
717 return bus->phymap[addr];
720 /* Try Standard (ie Clause 22) access */
721 r = get_phy_id(bus, addr, MDIO_DEVAD_NONE, &phy_id);
725 /* If the PHY ID is mostly f's, we didn't find anything */
726 if ((phy_id & 0x1fffffff) != 0x1fffffff)
727 return phy_device_create(bus, addr, phy_id, interface);
729 /* Otherwise we have to try Clause 45 */
730 for (i = 1; i < 5; i++) {
731 r = get_phy_id(bus, addr, i, &phy_id);
735 /* If the phy_id is mostly Fs, there is no device there */
736 if ((phy_id & 0x1fffffff) != 0x1fffffff)
737 return phy_device_create(bus, addr, phy_id, interface);
742 int phy_reset(struct phy_device *phydev)
747 int devad = MDIO_DEVAD_NONE;
749 #ifdef CONFIG_PHYLIB_10G
750 /* If it's 10G, we need to issue reset through one of the MMDs */
751 if (is_10g_interface(phydev->interface)) {
753 gen10g_discover_mmds(phydev);
755 devad = ffs(phydev->mmds) - 1;
759 reg = phy_read(phydev, devad, MII_BMCR);
761 debug("PHY status read failed\n");
767 err = phy_write(phydev, devad, MII_BMCR, reg);
769 debug("PHY reset failed\n");
773 #ifdef CONFIG_PHY_RESET_DELAY
774 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
777 * Poll the control register for the reset bit to go to 0 (it is
778 * auto-clearing). This should happen within 0.5 seconds per the
781 while ((reg & BMCR_RESET) && timeout-- >= 0) {
782 reg = phy_read(phydev, devad, MII_BMCR);
785 debug("PHY status read failed\n");
791 if (reg & BMCR_RESET) {
792 puts("PHY reset timed out\n");
799 int miiphy_reset(const char *devname, unsigned char addr)
801 struct mii_dev *bus = miiphy_get_dev_by_name(devname);
802 struct phy_device *phydev;
805 * miiphy_reset was only used on standard PHYs, so we'll fake it here.
806 * If later code tries to connect with the right interface, this will
807 * be corrected by get_phy_device in phy_connect()
809 phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII);
811 return phy_reset(phydev);
814 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
815 struct eth_device *dev,
816 phy_interface_t interface)
818 struct phy_device *phydev;
824 /* Wait 15ms to make sure the PHY has come out of hard reset */
827 phydev = get_phy_device(bus, addr, interface);
830 printf("Could not get PHY for %s:%d\n", bus->name, addr);
835 /* Soft Reset the PHY */
839 printf("%s:%d is connected to %s. Reconnecting to %s\n",
840 bus->name, addr, phydev->dev->name, dev->name);
844 debug("%s connected to %s\n", dev->name, phydev->drv->name);
850 * Start the PHY. Returns 0 on success, or a negative error code.
852 int phy_startup(struct phy_device *phydev)
854 if (phydev->drv->startup)
855 return phydev->drv->startup(phydev);
860 static int __board_phy_config(struct phy_device *phydev)
862 if (phydev->drv->config)
863 return phydev->drv->config(phydev);
867 int board_phy_config(struct phy_device *phydev)
868 __attribute__((weak, alias("__board_phy_config")));
870 int phy_config(struct phy_device *phydev)
872 /* Invoke an optional board-specific helper */
873 board_phy_config(phydev);
878 int phy_shutdown(struct phy_device *phydev)
880 if (phydev->drv->shutdown)
881 phydev->drv->shutdown(phydev);