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 /* Generic PHY support and helper functions */
27 * genphy_config_advert - sanitize and advertise auto-negotation parameters
28 * @phydev: target phy_device struct
30 * Description: Writes MII_ADVERTISE with the appropriate values,
31 * after sanitizing the values to make sure we only advertise
32 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
33 * hasn't changed, and > 0 if it has changed.
35 static int genphy_config_advert(struct phy_device *phydev)
41 /* Only allow advertising what
42 * this PHY supports */
43 phydev->advertising &= phydev->supported;
44 advertise = phydev->advertising;
46 /* Setup standard advertisement */
47 oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
51 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
52 ADVERTISE_PAUSE_ASYM);
53 if (advertise & ADVERTISED_10baseT_Half)
54 adv |= ADVERTISE_10HALF;
55 if (advertise & ADVERTISED_10baseT_Full)
56 adv |= ADVERTISE_10FULL;
57 if (advertise & ADVERTISED_100baseT_Half)
58 adv |= ADVERTISE_100HALF;
59 if (advertise & ADVERTISED_100baseT_Full)
60 adv |= ADVERTISE_100FULL;
61 if (advertise & ADVERTISED_Pause)
62 adv |= ADVERTISE_PAUSE_CAP;
63 if (advertise & ADVERTISED_Asym_Pause)
64 adv |= ADVERTISE_PAUSE_ASYM;
65 if (advertise & ADVERTISED_1000baseX_Half)
66 adv |= ADVERTISE_1000XHALF;
67 if (advertise & ADVERTISED_1000baseX_Full)
68 adv |= ADVERTISE_1000XFULL;
71 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv);
78 /* Configure gigabit if it's supported */
79 if (phydev->supported & (SUPPORTED_1000baseT_Half |
80 SUPPORTED_1000baseT_Full)) {
81 oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
85 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
86 if (advertise & SUPPORTED_1000baseT_Half)
87 adv |= ADVERTISE_1000HALF;
88 if (advertise & SUPPORTED_1000baseT_Full)
89 adv |= ADVERTISE_1000FULL;
92 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000,
106 * genphy_setup_forced - configures/forces speed/duplex from @phydev
107 * @phydev: target phy_device struct
109 * Description: Configures MII_BMCR to force speed/duplex
110 * to the values in phydev. Assumes that the values are valid.
112 static int genphy_setup_forced(struct phy_device *phydev)
116 phydev->pause = phydev->asym_pause = 0;
118 if (SPEED_1000 == phydev->speed)
119 ctl |= BMCR_SPEED1000;
120 else if (SPEED_100 == phydev->speed)
121 ctl |= BMCR_SPEED100;
123 if (DUPLEX_FULL == phydev->duplex)
124 ctl |= BMCR_FULLDPLX;
126 return phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
131 * genphy_restart_aneg - Enable and Restart Autonegotiation
132 * @phydev: target phy_device struct
134 int genphy_restart_aneg(struct phy_device *phydev)
138 ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
142 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
144 /* Don't isolate the PHY if we're negotiating */
145 ctl &= ~(BMCR_ISOLATE);
147 return phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
152 * genphy_config_aneg - restart auto-negotiation or write BMCR
153 * @phydev: target phy_device struct
155 * Description: If auto-negotiation is enabled, we configure the
156 * advertising, and then restart auto-negotiation. If it is not
157 * enabled, then we write the BMCR.
159 int genphy_config_aneg(struct phy_device *phydev)
163 if (AUTONEG_ENABLE != phydev->autoneg)
164 return genphy_setup_forced(phydev);
166 result = genphy_config_advert(phydev);
168 if (result < 0) /* error */
172 /* Advertisment hasn't changed, but maybe aneg was never on to
173 * begin with? Or maybe phy was isolated? */
174 int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
179 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
180 result = 1; /* do restart aneg */
183 /* Only restart aneg if we are advertising something different
184 * than we were before. */
186 result = genphy_restart_aneg(phydev);
192 * genphy_update_link - update link status in @phydev
193 * @phydev: target phy_device struct
195 * Description: Update the value in phydev->link to reflect the
196 * current link value. In order to do this, we need to read
197 * the status register twice, keeping the second value.
199 int genphy_update_link(struct phy_device *phydev)
205 * Wait if the link is up, and autonegotiation is in progress
206 * (ie - we're capable and it's not done)
208 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
213 * If we already saw the link up, and it hasn't gone down, then
214 * we don't need to wait for autoneg again
216 if (phydev->link && mii_reg & BMSR_LSTATUS)
219 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
223 if (!(bmcr & BMCR_ANENABLE))
226 if ((mii_reg & BMSR_ANEGCAPABLE) && !(mii_reg & BMSR_ANEGCOMPLETE)) {
229 printf("%s Waiting for PHY auto negotiation to complete",
231 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
235 if (i > PHY_ANEG_TIMEOUT) {
236 printf(" TIMEOUT !\n");
242 puts("user interrupt!\n");
247 if ((i++ % 500) == 0)
250 udelay(1000); /* 1 ms */
251 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
258 /* Read the link a second time to clear the latched state */
259 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
263 if (mii_reg & BMSR_LSTATUS)
273 * Generic function which updates the speed and duplex. If
274 * autonegotiation is enabled, it uses the AND of the link
275 * partner's advertised capabilities and our advertised
276 * capabilities. If autonegotiation is disabled, we use the
277 * appropriate bits in the control register.
279 * Stolen from Linux's mii.c and phy_device.c
281 int genphy_parse_link(struct phy_device *phydev)
283 int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
288 /* We're using autonegotiation */
289 if (phydev->supported & SUPPORTED_Autoneg) {
295 /* Check for gigabit capability */
296 if (phydev->supported & (SUPPORTED_1000baseT_Full |
297 SUPPORTED_1000baseT_Half)) {
298 /* We want a list of states supported by
299 * both PHYs in the link
301 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
303 debug("Could not read MII_STAT1000. Ignoring gigabit capability\n");
308 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
314 /* Set the baseline so we only have to set them
315 * if they're different
317 phydev->speed = SPEED_10;
318 phydev->duplex = DUPLEX_HALF;
320 /* Check the gigabit fields */
321 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
322 phydev->speed = SPEED_1000;
324 if (gblpa & PHY_1000BTSR_1000FD)
325 phydev->duplex = DUPLEX_FULL;
331 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
336 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
341 if (lpa & (LPA_100FULL | LPA_100HALF)) {
342 phydev->speed = SPEED_100;
344 if (lpa & LPA_100FULL)
345 phydev->duplex = DUPLEX_FULL;
347 } else if (lpa & LPA_10FULL)
348 phydev->duplex = DUPLEX_FULL;
351 * Extended status may indicate that the PHY supports
352 * 1000BASE-T/X even though the 1000BASE-T registers
353 * are missing. In this case we can't tell whether the
354 * peer also supports it, so we only check extended
355 * status if the 1000BASE-T registers are actually
358 if ((mii_reg & BMSR_ESTATEN) && !(mii_reg & BMSR_ERCAP))
359 estatus = phy_read(phydev, MDIO_DEVAD_NONE,
364 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF |
365 ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) {
366 phydev->speed = SPEED_1000;
367 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL))
368 phydev->duplex = DUPLEX_FULL;
372 int bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
377 phydev->speed = SPEED_10;
378 phydev->duplex = DUPLEX_HALF;
380 if (bmcr & BMCR_FULLDPLX)
381 phydev->duplex = DUPLEX_FULL;
383 if (bmcr & BMCR_SPEED1000)
384 phydev->speed = SPEED_1000;
385 else if (bmcr & BMCR_SPEED100)
386 phydev->speed = SPEED_100;
392 int genphy_config(struct phy_device *phydev)
397 /* For now, I'll claim that the generic driver supports
398 * all possible port types */
399 features = (SUPPORTED_TP | SUPPORTED_MII
400 | SUPPORTED_AUI | SUPPORTED_FIBRE |
403 /* Do we support autonegotiation? */
404 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
408 if (val & BMSR_ANEGCAPABLE)
409 features |= SUPPORTED_Autoneg;
411 if (val & BMSR_100FULL)
412 features |= SUPPORTED_100baseT_Full;
413 if (val & BMSR_100HALF)
414 features |= SUPPORTED_100baseT_Half;
415 if (val & BMSR_10FULL)
416 features |= SUPPORTED_10baseT_Full;
417 if (val & BMSR_10HALF)
418 features |= SUPPORTED_10baseT_Half;
420 if (val & BMSR_ESTATEN) {
421 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS);
426 if (val & ESTATUS_1000_TFULL)
427 features |= SUPPORTED_1000baseT_Full;
428 if (val & ESTATUS_1000_THALF)
429 features |= SUPPORTED_1000baseT_Half;
430 if (val & ESTATUS_1000_XFULL)
431 features |= SUPPORTED_1000baseX_Full;
432 if (val & ESTATUS_1000_XHALF)
433 features |= SUPPORTED_1000baseX_Half;
436 phydev->supported = features;
437 phydev->advertising = features;
439 genphy_config_aneg(phydev);
444 int genphy_startup(struct phy_device *phydev)
446 genphy_update_link(phydev);
447 genphy_parse_link(phydev);
452 int genphy_shutdown(struct phy_device *phydev)
457 static struct phy_driver genphy_driver = {
460 .name = "Generic PHY",
462 .config = genphy_config,
463 .startup = genphy_startup,
464 .shutdown = genphy_shutdown,
467 static LIST_HEAD(phy_drivers);
471 #ifdef CONFIG_PHY_AQUANTIA
474 #ifdef CONFIG_PHY_ATHEROS
477 #ifdef CONFIG_PHY_BROADCOM
480 #ifdef CONFIG_PHY_CORTINA
483 #ifdef CONFIG_PHY_DAVICOM
486 #ifdef CONFIG_PHY_ET1011C
489 #ifdef CONFIG_PHY_LXT
492 #ifdef CONFIG_PHY_MARVELL
495 #ifdef CONFIG_PHY_MICREL
498 #ifdef CONFIG_PHY_NATSEMI
501 #ifdef CONFIG_PHY_REALTEK
504 #ifdef CONFIG_PHY_SMSC
507 #ifdef CONFIG_PHY_TERANETICS
508 phy_teranetics_init();
510 #ifdef CONFIG_PHY_VITESSE
517 int phy_register(struct phy_driver *drv)
519 INIT_LIST_HEAD(&drv->list);
520 list_add_tail(&drv->list, &phy_drivers);
525 static int phy_probe(struct phy_device *phydev)
529 phydev->advertising = phydev->supported = phydev->drv->features;
530 phydev->mmds = phydev->drv->mmds;
532 if (phydev->drv->probe)
533 err = phydev->drv->probe(phydev);
538 static struct phy_driver *generic_for_interface(phy_interface_t interface)
540 #ifdef CONFIG_PHYLIB_10G
541 if (is_10g_interface(interface))
542 return &gen10g_driver;
545 return &genphy_driver;
548 static struct phy_driver *get_phy_driver(struct phy_device *phydev,
549 phy_interface_t interface)
551 struct list_head *entry;
552 int phy_id = phydev->phy_id;
553 struct phy_driver *drv = NULL;
555 list_for_each(entry, &phy_drivers) {
556 drv = list_entry(entry, struct phy_driver, list);
557 if ((drv->uid & drv->mask) == (phy_id & drv->mask))
561 /* If we made it here, there's no driver for this PHY */
562 return generic_for_interface(interface);
565 static int aneg_enabled(struct phy_device *phydev)
567 static const char *aneg = "_aneg";
568 char varname[strlen(phydev->bus->name) + strlen(aneg) + 1];
570 snprintf(varname, sizeof(varname), "%s%s", phydev->bus->name, aneg);
571 return getenv_yesno(varname);
574 static int phy_get_speed(struct phy_device *phydev)
577 static const char *aneg = "_speed";
578 char varname[strlen(phydev->bus->name) + strlen(aneg) + 1];
581 snprintf(varname, sizeof(varname), "%s%s", phydev->bus->name, aneg);
583 val = getenv_ulong(varname, 10, 100);
595 printf("Improper setting '%s' for %s; assuming 100\n",
596 getenv(varname), varname);
602 static int phy_get_duplex(struct phy_device *phydev)
604 int ret = DUPLEX_FULL;
605 static const char *aneg = "_duplex";
606 char varname[strlen(phydev->bus->name) + strlen(aneg) + 4];
609 snprintf(varname, sizeof(varname), "%s%d%s",
610 phydev->bus->name, phydev->addr, aneg);
612 val = getenv(varname);
614 if (strcasecmp(val, "full") != 0) {
615 if (strcasecmp(val, "half") == 0) {
618 printf("Improper setting '%s' for %s; assuming 'full'\n",
620 printf("Expected one of: 'full', 'half'\n");
628 static struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
630 phy_interface_t interface)
632 struct phy_device *dev;
634 /* We allocate the device, and initialize the
636 dev = malloc(sizeof(*dev));
638 printf("Failed to allocate PHY device for %s:%d\n",
643 memset(dev, 0, sizeof(*dev));
647 dev->interface = interface;
650 dev->phy_id = phy_id;
653 dev->drv = get_phy_driver(dev, interface);
655 if (aneg_enabled(dev)) {
656 dev->autoneg = AUTONEG_ENABLE;
658 dev->autoneg = AUTONEG_DISABLE;
659 dev->speed = phy_get_speed(dev);
660 dev->duplex = phy_get_duplex(dev);
662 switch (dev->speed) {
664 if (dev->duplex == DUPLEX_FULL)
665 dev->supported &= SUPPORTED_1000baseT_Full;
667 dev->supported &= SUPPORTED_1000baseT_Half;
670 if (dev->duplex == DUPLEX_FULL)
671 dev->supported &= SUPPORTED_100baseT_Full;
673 dev->supported &= SUPPORTED_100baseT_Half;
676 if (dev->duplex == DUPLEX_FULL)
677 dev->supported &= SUPPORTED_10baseT_Full;
679 dev->supported &= SUPPORTED_10baseT_Half;
682 printf("Unsupported speed: %d\n", dev->speed);
688 bus->phymap[addr] = dev;
694 * get_phy_id - reads the specified addr for its ID.
695 * @bus: the target MII bus
696 * @addr: PHY address on the MII bus
697 * @phy_id: where to store the ID retrieved.
699 * Description: Reads the ID registers of the PHY at @addr on the
700 * @bus, stores it in @phy_id and returns zero on success.
702 int __weak get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
706 /* Grab the bits from PHYIR1, and put them
707 * in the upper half */
708 phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
713 *phy_id = (phy_reg & 0xffff) << 16;
715 /* Grab the bits from PHYIR2, and put them in the lower half */
716 phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
721 *phy_id |= (phy_reg & 0xffff);
726 static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
727 unsigned phy_mask, int devad, phy_interface_t interface)
729 u32 phy_id = 0xffffffff;
731 int addr = ffs(phy_mask) - 1;
732 int r = get_phy_id(bus, addr, devad, &phy_id);
733 /* If the PHY ID is mostly f's, we didn't find anything */
734 if (r == 0 && (phy_id & 0x1fffffff) != 0x1fffffff)
735 return phy_device_create(bus, addr, phy_id, interface);
736 phy_mask &= ~(1 << addr);
741 static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
742 unsigned phy_mask, phy_interface_t interface)
744 /* If we have one, return the existing device, with new interface */
746 int addr = ffs(phy_mask) - 1;
747 if (bus->phymap[addr]) {
748 bus->phymap[addr]->interface = interface;
749 return bus->phymap[addr];
751 phy_mask &= ~(1 << addr);
756 static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
757 unsigned phy_mask, phy_interface_t interface)
760 struct phy_device *phydev;
762 phydev = search_for_existing_phy(bus, phy_mask, interface);
765 /* Try Standard (ie Clause 22) access */
766 /* Otherwise we have to try Clause 45 */
767 for (i = 0; i < 5; i++) {
768 phydev = create_phy_by_mask(bus, phy_mask,
769 i ? i : MDIO_DEVAD_NONE, interface);
775 printf("Phy %d not found\n", ffs(phy_mask) - 1);
776 return phy_device_create(bus, ffs(phy_mask) - 1, 0xffffffff, interface);
780 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
781 * @bus: the target MII bus
782 * @addr: PHY address on the MII bus
784 * Description: Reads the ID registers of the PHY at @addr on the
785 * @bus, then allocates and returns the phy_device to represent it.
787 static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
788 phy_interface_t interface)
790 return get_phy_device_by_mask(bus, 1 << addr, interface);
793 int phy_reset(struct phy_device *phydev)
798 int devad = MDIO_DEVAD_NONE;
800 #ifdef CONFIG_PHYLIB_10G
801 /* If it's 10G, we need to issue reset through one of the MMDs */
802 if (is_10g_interface(phydev->interface)) {
804 gen10g_discover_mmds(phydev);
806 devad = ffs(phydev->mmds) - 1;
810 reg = phy_read(phydev, devad, MII_BMCR);
812 debug("PHY status read failed\n");
818 err = phy_write(phydev, devad, MII_BMCR, reg);
820 debug("PHY reset failed\n");
824 #ifdef CONFIG_PHY_RESET_DELAY
825 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
828 * Poll the control register for the reset bit to go to 0 (it is
829 * auto-clearing). This should happen within 0.5 seconds per the
832 while ((reg & BMCR_RESET) && timeout-- >= 0) {
833 reg = phy_read(phydev, devad, MII_BMCR);
836 debug("PHY status read failed\n");
842 if (reg & BMCR_RESET) {
843 puts("PHY reset timed out\n");
850 int miiphy_reset(const char *devname, unsigned char addr)
852 struct mii_dev *bus = miiphy_get_dev_by_name(devname);
853 struct phy_device *phydev;
856 * miiphy_reset was only used on standard PHYs, so we'll fake it here.
857 * If later code tries to connect with the right interface, this will
858 * be corrected by get_phy_device in phy_connect()
860 phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII);
862 return phy_reset(phydev);
865 struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask,
866 phy_interface_t interface)
872 /* Wait 15ms to make sure the PHY has come out of hard reset */
874 return get_phy_device_by_mask(bus, phy_mask, interface);
878 void phy_connect_dev(struct phy_device *phydev, struct udevice *dev)
880 void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev)
883 /* Soft Reset the PHY */
886 printf("%s:%d is connected to %s. Reconnecting to %s\n",
887 phydev->bus->name, phydev->addr,
888 phydev->dev->name, dev->name);
891 debug("%s connected to %s\n", dev->name, phydev->drv->name);
895 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
896 struct udevice *dev, phy_interface_t interface)
898 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
899 struct eth_device *dev, phy_interface_t interface)
902 struct phy_device *phydev;
904 phydev = phy_find_by_mask(bus, 1 << addr, interface);
906 phy_connect_dev(phydev, dev);
908 printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
913 * Start the PHY. Returns 0 on success, or a negative error code.
915 int phy_startup(struct phy_device *phydev)
917 if (phydev->drv->startup)
918 return phydev->drv->startup(phydev);
923 __weak int board_phy_config(struct phy_device *phydev)
925 if (phydev->drv->config)
926 return phydev->drv->config(phydev);
930 int phy_config(struct phy_device *phydev)
932 /* Invoke an optional board-specific helper */
933 board_phy_config(phydev);
938 int phy_shutdown(struct phy_device *phydev)
940 if (phydev->drv->shutdown)
941 phydev->drv->shutdown(phydev);
946 int phy_get_interface_by_name(const char *str)
950 for (i = 0; i < PHY_INTERFACE_MODE_COUNT; i++) {
951 if (!strcmp(str, phy_interface_strings[i]))