2 * Generic PHY Management code
4 * SPDX-License-Identifier: GPL-2.0+
6 * Copyright 2011 Freescale Semiconductor, Inc.
9 * Based loosely off of Linux's PHY Lib
21 #include <linux/err.h>
22 #include <linux/compiler.h>
24 DECLARE_GLOBAL_DATA_PTR;
26 /* Generic PHY support and helper functions */
29 * genphy_config_advert - sanitize and advertise auto-negotation parameters
30 * @phydev: target phy_device struct
32 * Description: Writes MII_ADVERTISE with the appropriate values,
33 * after sanitizing the values to make sure we only advertise
34 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
35 * hasn't changed, and > 0 if it has changed.
37 static int genphy_config_advert(struct phy_device *phydev)
43 /* Only allow advertising what
44 * this PHY supports */
45 phydev->advertising &= phydev->supported;
46 advertise = phydev->advertising;
48 /* Setup standard advertisement */
49 oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
53 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
54 ADVERTISE_PAUSE_ASYM);
55 if (advertise & ADVERTISED_10baseT_Half)
56 adv |= ADVERTISE_10HALF;
57 if (advertise & ADVERTISED_10baseT_Full)
58 adv |= ADVERTISE_10FULL;
59 if (advertise & ADVERTISED_100baseT_Half)
60 adv |= ADVERTISE_100HALF;
61 if (advertise & ADVERTISED_100baseT_Full)
62 adv |= ADVERTISE_100FULL;
63 if (advertise & ADVERTISED_Pause)
64 adv |= ADVERTISE_PAUSE_CAP;
65 if (advertise & ADVERTISED_Asym_Pause)
66 adv |= ADVERTISE_PAUSE_ASYM;
67 if (advertise & ADVERTISED_1000baseX_Half)
68 adv |= ADVERTISE_1000XHALF;
69 if (advertise & ADVERTISED_1000baseX_Full)
70 adv |= ADVERTISE_1000XFULL;
73 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv);
80 /* Configure gigabit if it's supported */
81 if (phydev->supported & (SUPPORTED_1000baseT_Half |
82 SUPPORTED_1000baseT_Full)) {
83 oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
87 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
88 if (advertise & SUPPORTED_1000baseT_Half)
89 adv |= ADVERTISE_1000HALF;
90 if (advertise & SUPPORTED_1000baseT_Full)
91 adv |= ADVERTISE_1000FULL;
94 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000,
108 * genphy_setup_forced - configures/forces speed/duplex from @phydev
109 * @phydev: target phy_device struct
111 * Description: Configures MII_BMCR to force speed/duplex
112 * to the values in phydev. Assumes that the values are valid.
114 static int genphy_setup_forced(struct phy_device *phydev)
118 phydev->pause = phydev->asym_pause = 0;
120 if (SPEED_1000 == phydev->speed)
121 ctl |= BMCR_SPEED1000;
122 else if (SPEED_100 == phydev->speed)
123 ctl |= BMCR_SPEED100;
125 if (DUPLEX_FULL == phydev->duplex)
126 ctl |= BMCR_FULLDPLX;
128 return phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
133 * genphy_restart_aneg - Enable and Restart Autonegotiation
134 * @phydev: target phy_device struct
136 int genphy_restart_aneg(struct phy_device *phydev)
140 ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
144 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
146 /* Don't isolate the PHY if we're negotiating */
147 ctl &= ~(BMCR_ISOLATE);
149 return phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
154 * genphy_config_aneg - restart auto-negotiation or write BMCR
155 * @phydev: target phy_device struct
157 * Description: If auto-negotiation is enabled, we configure the
158 * advertising, and then restart auto-negotiation. If it is not
159 * enabled, then we write the BMCR.
161 int genphy_config_aneg(struct phy_device *phydev)
165 if (AUTONEG_ENABLE != phydev->autoneg)
166 return genphy_setup_forced(phydev);
168 result = genphy_config_advert(phydev);
170 if (result < 0) /* error */
174 /* Advertisment hasn't changed, but maybe aneg was never on to
175 * begin with? Or maybe phy was isolated? */
176 int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
181 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
182 result = 1; /* do restart aneg */
185 /* Only restart aneg if we are advertising something different
186 * than we were before. */
188 result = genphy_restart_aneg(phydev);
194 * genphy_update_link - update link status in @phydev
195 * @phydev: target phy_device struct
197 * Description: Update the value in phydev->link to reflect the
198 * current link value. In order to do this, we need to read
199 * the status register twice, keeping the second value.
201 int genphy_update_link(struct phy_device *phydev)
207 * Wait if the link is up, and autonegotiation is in progress
208 * (ie - we're capable and it's not done)
210 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
215 * If we already saw the link up, and it hasn't gone down, then
216 * we don't need to wait for autoneg again
218 if (phydev->link && mii_reg & BMSR_LSTATUS)
221 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
225 if (!(bmcr & BMCR_ANENABLE))
228 if ((mii_reg & BMSR_ANEGCAPABLE) && !(mii_reg & BMSR_ANEGCOMPLETE)) {
231 printf("%s Waiting for PHY auto negotiation to complete",
233 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
237 if (i > PHY_ANEG_TIMEOUT) {
238 printf(" TIMEOUT !\n");
244 puts("user interrupt!\n");
249 if ((i++ % 500) == 0)
252 udelay(1000); /* 1 ms */
253 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
260 /* Read the link a second time to clear the latched state */
261 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
265 if (mii_reg & BMSR_LSTATUS)
275 * Generic function which updates the speed and duplex. If
276 * autonegotiation is enabled, it uses the AND of the link
277 * partner's advertised capabilities and our advertised
278 * capabilities. If autonegotiation is disabled, we use the
279 * appropriate bits in the control register.
281 * Stolen from Linux's mii.c and phy_device.c
283 int genphy_parse_link(struct phy_device *phydev)
285 int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
290 /* We're using autonegotiation */
291 if (phydev->supported & SUPPORTED_Autoneg) {
297 /* Check for gigabit capability */
298 if (phydev->supported & (SUPPORTED_1000baseT_Full |
299 SUPPORTED_1000baseT_Half)) {
300 /* We want a list of states supported by
301 * both PHYs in the link
303 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
305 debug("Could not read MII_STAT1000. Ignoring gigabit capability\n");
310 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
316 /* Set the baseline so we only have to set them
317 * if they're different
319 phydev->speed = SPEED_10;
320 phydev->duplex = DUPLEX_HALF;
322 /* Check the gigabit fields */
323 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
324 phydev->speed = SPEED_1000;
326 if (gblpa & PHY_1000BTSR_1000FD)
327 phydev->duplex = DUPLEX_FULL;
333 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
338 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
343 if (lpa & (LPA_100FULL | LPA_100HALF)) {
344 phydev->speed = SPEED_100;
346 if (lpa & LPA_100FULL)
347 phydev->duplex = DUPLEX_FULL;
349 } else if (lpa & LPA_10FULL)
350 phydev->duplex = DUPLEX_FULL;
353 * Extended status may indicate that the PHY supports
354 * 1000BASE-T/X even though the 1000BASE-T registers
355 * are missing. In this case we can't tell whether the
356 * peer also supports it, so we only check extended
357 * status if the 1000BASE-T registers are actually
360 if ((mii_reg & BMSR_ESTATEN) && !(mii_reg & BMSR_ERCAP))
361 estatus = phy_read(phydev, MDIO_DEVAD_NONE,
366 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF |
367 ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) {
368 phydev->speed = SPEED_1000;
369 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL))
370 phydev->duplex = DUPLEX_FULL;
374 int bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
379 phydev->speed = SPEED_10;
380 phydev->duplex = DUPLEX_HALF;
382 if (bmcr & BMCR_FULLDPLX)
383 phydev->duplex = DUPLEX_FULL;
385 if (bmcr & BMCR_SPEED1000)
386 phydev->speed = SPEED_1000;
387 else if (bmcr & BMCR_SPEED100)
388 phydev->speed = SPEED_100;
394 int genphy_config(struct phy_device *phydev)
399 /* For now, I'll claim that the generic driver supports
400 * all possible port types */
401 features = (SUPPORTED_TP | SUPPORTED_MII
402 | SUPPORTED_AUI | SUPPORTED_FIBRE |
405 /* Do we support autonegotiation? */
406 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
410 if (val & BMSR_ANEGCAPABLE)
411 features |= SUPPORTED_Autoneg;
413 if (val & BMSR_100FULL)
414 features |= SUPPORTED_100baseT_Full;
415 if (val & BMSR_100HALF)
416 features |= SUPPORTED_100baseT_Half;
417 if (val & BMSR_10FULL)
418 features |= SUPPORTED_10baseT_Full;
419 if (val & BMSR_10HALF)
420 features |= SUPPORTED_10baseT_Half;
422 if (val & BMSR_ESTATEN) {
423 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS);
428 if (val & ESTATUS_1000_TFULL)
429 features |= SUPPORTED_1000baseT_Full;
430 if (val & ESTATUS_1000_THALF)
431 features |= SUPPORTED_1000baseT_Half;
432 if (val & ESTATUS_1000_XFULL)
433 features |= SUPPORTED_1000baseX_Full;
434 if (val & ESTATUS_1000_XHALF)
435 features |= SUPPORTED_1000baseX_Half;
438 phydev->supported = features;
439 phydev->advertising = features;
441 genphy_config_aneg(phydev);
446 int genphy_startup(struct phy_device *phydev)
448 genphy_update_link(phydev);
449 genphy_parse_link(phydev);
454 int genphy_shutdown(struct phy_device *phydev)
459 static struct phy_driver genphy_driver = {
462 .name = "Generic PHY",
464 .config = genphy_config,
465 .startup = genphy_startup,
466 .shutdown = genphy_shutdown,
469 static LIST_HEAD(phy_drivers);
473 #ifdef CONFIG_PHY_AQUANTIA
476 #ifdef CONFIG_PHY_ATHEROS
479 #ifdef CONFIG_PHY_BROADCOM
482 #ifdef CONFIG_PHY_CORTINA
485 #ifdef CONFIG_PHY_DAVICOM
488 #ifdef CONFIG_PHY_ET1011C
491 #ifdef CONFIG_PHY_LXT
494 #ifdef CONFIG_PHY_MARVELL
497 #ifdef CONFIG_PHY_MICREL
500 #ifdef CONFIG_PHY_NATSEMI
503 #ifdef CONFIG_PHY_REALTEK
506 #ifdef CONFIG_PHY_SMSC
509 #ifdef CONFIG_PHY_TERANETICS
510 phy_teranetics_init();
512 #ifdef CONFIG_PHY_VITESSE
519 int phy_register(struct phy_driver *drv)
521 INIT_LIST_HEAD(&drv->list);
522 list_add_tail(&drv->list, &phy_drivers);
524 #ifdef CONFIG_NEEDS_MANUAL_RELOC
526 drv->probe += gd->reloc_off;
528 drv->config += gd->reloc_off;
530 drv->startup += gd->reloc_off;
532 drv->shutdown += gd->reloc_off;
534 drv->readext += gd->reloc_off;
536 drv->writeext += gd->reloc_off;
541 static int phy_probe(struct phy_device *phydev)
545 phydev->advertising = phydev->supported = phydev->drv->features;
546 phydev->mmds = phydev->drv->mmds;
548 if (phydev->drv->probe)
549 err = phydev->drv->probe(phydev);
554 static struct phy_driver *generic_for_interface(phy_interface_t interface)
556 #ifdef CONFIG_PHYLIB_10G
557 if (is_10g_interface(interface))
558 return &gen10g_driver;
561 return &genphy_driver;
564 static struct phy_driver *get_phy_driver(struct phy_device *phydev,
565 phy_interface_t interface)
567 struct list_head *entry;
568 int phy_id = phydev->phy_id;
569 struct phy_driver *drv = NULL;
571 list_for_each(entry, &phy_drivers) {
572 drv = list_entry(entry, struct phy_driver, list);
573 if ((drv->uid & drv->mask) == (phy_id & drv->mask))
577 /* If we made it here, there's no driver for this PHY */
578 return generic_for_interface(interface);
581 static int aneg_enabled(struct phy_device *phydev)
583 static const char *aneg = "_aneg";
584 char varname[strlen(phydev->bus->name) + strlen(aneg) + 1];
586 snprintf(varname, sizeof(varname), "%s%s", phydev->bus->name, aneg);
587 return getenv_yesno(varname);
590 static int phy_get_speed(struct phy_device *phydev)
593 static const char *aneg = "_speed";
594 char varname[strlen(phydev->bus->name) + strlen(aneg) + 1];
597 snprintf(varname, sizeof(varname), "%s%s", phydev->bus->name, aneg);
599 val = getenv_ulong(varname, 10, 100);
611 printf("Improper setting '%s' for %s; assuming 100\n",
612 getenv(varname), varname);
618 static int phy_get_duplex(struct phy_device *phydev)
620 int ret = DUPLEX_FULL;
621 static const char *aneg = "_duplex";
622 char varname[strlen(phydev->bus->name) + strlen(aneg) + 4];
625 snprintf(varname, sizeof(varname), "%s%d%s",
626 phydev->bus->name, phydev->addr, aneg);
628 val = getenv(varname);
630 if (strcasecmp(val, "full") != 0) {
631 if (strcasecmp(val, "half") == 0) {
634 printf("Improper setting '%s' for %s; assuming 'full'\n",
636 printf("Expected one of: 'full', 'half'\n");
644 static struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
646 phy_interface_t interface)
648 struct phy_device *dev;
650 /* We allocate the device, and initialize the
652 dev = malloc(sizeof(*dev));
654 printf("Failed to allocate PHY device for %s:%d\n",
659 memset(dev, 0, sizeof(*dev));
663 dev->interface = interface;
666 dev->phy_id = phy_id;
669 dev->drv = get_phy_driver(dev, interface);
671 if (aneg_enabled(dev)) {
672 dev->autoneg = AUTONEG_ENABLE;
674 dev->autoneg = AUTONEG_DISABLE;
675 dev->speed = phy_get_speed(dev);
676 dev->duplex = phy_get_duplex(dev);
678 switch (dev->speed) {
680 if (dev->duplex == DUPLEX_FULL)
681 dev->supported &= SUPPORTED_1000baseT_Full;
683 dev->supported &= SUPPORTED_1000baseT_Half;
686 if (dev->duplex == DUPLEX_FULL)
687 dev->supported &= SUPPORTED_100baseT_Full;
689 dev->supported &= SUPPORTED_100baseT_Half;
692 if (dev->duplex == DUPLEX_FULL)
693 dev->supported &= SUPPORTED_10baseT_Full;
695 dev->supported &= SUPPORTED_10baseT_Half;
698 printf("Unsupported speed: %d\n", dev->speed);
704 bus->phymap[addr] = dev;
710 * get_phy_id - reads the specified addr for its ID.
711 * @bus: the target MII bus
712 * @addr: PHY address on the MII bus
713 * @phy_id: where to store the ID retrieved.
715 * Description: Reads the ID registers of the PHY at @addr on the
716 * @bus, stores it in @phy_id and returns zero on success.
718 int __weak get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
722 /* Grab the bits from PHYIR1, and put them
723 * in the upper half */
724 phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
729 *phy_id = (phy_reg & 0xffff) << 16;
731 /* Grab the bits from PHYIR2, and put them in the lower half */
732 phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
737 *phy_id |= (phy_reg & 0xffff);
742 static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
743 unsigned phy_mask, int devad, phy_interface_t interface)
745 u32 phy_id = 0xffffffff;
747 int addr = ffs(phy_mask) - 1;
748 int r = get_phy_id(bus, addr, devad, &phy_id);
749 /* If the PHY ID is mostly f's, we didn't find anything */
750 if (r == 0 && (phy_id & 0x1fffffff) != 0x1fffffff)
751 return phy_device_create(bus, addr, phy_id, interface);
752 phy_mask &= ~(1 << addr);
757 static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
758 unsigned phy_mask, phy_interface_t interface)
760 /* If we have one, return the existing device, with new interface */
762 int addr = ffs(phy_mask) - 1;
763 if (bus->phymap[addr]) {
764 bus->phymap[addr]->interface = interface;
765 return bus->phymap[addr];
767 phy_mask &= ~(1 << addr);
772 static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
773 unsigned phy_mask, phy_interface_t interface)
776 struct phy_device *phydev;
778 phydev = search_for_existing_phy(bus, phy_mask, interface);
781 /* Try Standard (ie Clause 22) access */
782 /* Otherwise we have to try Clause 45 */
783 for (i = 0; i < 5; i++) {
784 phydev = create_phy_by_mask(bus, phy_mask,
785 i ? i : MDIO_DEVAD_NONE, interface);
791 printf("Phy %d not found\n", ffs(phy_mask) - 1);
792 return phy_device_create(bus, ffs(phy_mask) - 1, 0xffffffff, interface);
796 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
797 * @bus: the target MII bus
798 * @addr: PHY address on the MII bus
800 * Description: Reads the ID registers of the PHY at @addr on the
801 * @bus, then allocates and returns the phy_device to represent it.
803 static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
804 phy_interface_t interface)
806 return get_phy_device_by_mask(bus, 1 << addr, interface);
809 int phy_reset(struct phy_device *phydev)
814 int devad = MDIO_DEVAD_NONE;
816 #ifdef CONFIG_PHYLIB_10G
817 /* If it's 10G, we need to issue reset through one of the MMDs */
818 if (is_10g_interface(phydev->interface)) {
820 gen10g_discover_mmds(phydev);
822 devad = ffs(phydev->mmds) - 1;
826 reg = phy_read(phydev, devad, MII_BMCR);
828 debug("PHY status read failed\n");
834 err = phy_write(phydev, devad, MII_BMCR, reg);
836 debug("PHY reset failed\n");
840 #ifdef CONFIG_PHY_RESET_DELAY
841 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
844 * Poll the control register for the reset bit to go to 0 (it is
845 * auto-clearing). This should happen within 0.5 seconds per the
848 while ((reg & BMCR_RESET) && timeout-- >= 0) {
849 reg = phy_read(phydev, devad, MII_BMCR);
852 debug("PHY status read failed\n");
858 if (reg & BMCR_RESET) {
859 puts("PHY reset timed out\n");
866 int miiphy_reset(const char *devname, unsigned char addr)
868 struct mii_dev *bus = miiphy_get_dev_by_name(devname);
869 struct phy_device *phydev;
872 * miiphy_reset was only used on standard PHYs, so we'll fake it here.
873 * If later code tries to connect with the right interface, this will
874 * be corrected by get_phy_device in phy_connect()
876 phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII);
878 return phy_reset(phydev);
881 struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask,
882 phy_interface_t interface)
888 /* Wait 15ms to make sure the PHY has come out of hard reset */
890 return get_phy_device_by_mask(bus, phy_mask, interface);
894 void phy_connect_dev(struct phy_device *phydev, struct udevice *dev)
896 void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev)
899 /* Soft Reset the PHY */
902 printf("%s:%d is connected to %s. Reconnecting to %s\n",
903 phydev->bus->name, phydev->addr,
904 phydev->dev->name, dev->name);
907 debug("%s connected to %s\n", dev->name, phydev->drv->name);
911 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
912 struct udevice *dev, phy_interface_t interface)
914 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
915 struct eth_device *dev, phy_interface_t interface)
918 struct phy_device *phydev;
920 phydev = phy_find_by_mask(bus, 1 << addr, interface);
922 phy_connect_dev(phydev, dev);
924 printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
929 * Start the PHY. Returns 0 on success, or a negative error code.
931 int phy_startup(struct phy_device *phydev)
933 if (phydev->drv->startup)
934 return phydev->drv->startup(phydev);
939 __weak int board_phy_config(struct phy_device *phydev)
941 if (phydev->drv->config)
942 return phydev->drv->config(phydev);
946 int phy_config(struct phy_device *phydev)
948 /* Invoke an optional board-specific helper */
949 board_phy_config(phydev);
954 int phy_shutdown(struct phy_device *phydev)
956 if (phydev->drv->shutdown)
957 phydev->drv->shutdown(phydev);
962 int phy_get_interface_by_name(const char *str)
966 for (i = 0; i < PHY_INTERFACE_MODE_COUNT; i++) {
967 if (!strcmp(str, phy_interface_strings[i]))