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
22 #include <linux/err.h>
23 #include <linux/compiler.h>
25 DECLARE_GLOBAL_DATA_PTR;
27 /* Generic PHY support and helper functions */
30 * genphy_config_advert - sanitize and advertise auto-negotation parameters
31 * @phydev: target phy_device struct
33 * Description: Writes MII_ADVERTISE with the appropriate values,
34 * after sanitizing the values to make sure we only advertise
35 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
36 * hasn't changed, and > 0 if it has changed.
38 static int genphy_config_advert(struct phy_device *phydev)
41 int oldadv, adv, bmsr;
44 /* Only allow advertising what this PHY supports */
45 phydev->advertising &= phydev->supported;
46 advertise = phydev->advertising;
48 /* Setup standard advertisement */
49 adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
55 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
56 ADVERTISE_PAUSE_ASYM);
57 if (advertise & ADVERTISED_10baseT_Half)
58 adv |= ADVERTISE_10HALF;
59 if (advertise & ADVERTISED_10baseT_Full)
60 adv |= ADVERTISE_10FULL;
61 if (advertise & ADVERTISED_100baseT_Half)
62 adv |= ADVERTISE_100HALF;
63 if (advertise & ADVERTISED_100baseT_Full)
64 adv |= ADVERTISE_100FULL;
65 if (advertise & ADVERTISED_Pause)
66 adv |= ADVERTISE_PAUSE_CAP;
67 if (advertise & ADVERTISED_Asym_Pause)
68 adv |= ADVERTISE_PAUSE_ASYM;
69 if (advertise & ADVERTISED_1000baseX_Half)
70 adv |= ADVERTISE_1000XHALF;
71 if (advertise & ADVERTISED_1000baseX_Full)
72 adv |= ADVERTISE_1000XFULL;
75 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv);
82 bmsr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
86 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
87 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
90 if (!(bmsr & BMSR_ESTATEN))
93 /* Configure gigabit if it's supported */
94 adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
100 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
102 if (phydev->supported & (SUPPORTED_1000baseT_Half |
103 SUPPORTED_1000baseT_Full)) {
104 if (advertise & SUPPORTED_1000baseT_Half)
105 adv |= ADVERTISE_1000HALF;
106 if (advertise & SUPPORTED_1000baseT_Full)
107 adv |= ADVERTISE_1000FULL;
113 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, adv);
122 * genphy_setup_forced - configures/forces speed/duplex from @phydev
123 * @phydev: target phy_device struct
125 * Description: Configures MII_BMCR to force speed/duplex
126 * to the values in phydev. Assumes that the values are valid.
128 static int genphy_setup_forced(struct phy_device *phydev)
133 phydev->pause = phydev->asym_pause = 0;
135 if (SPEED_1000 == phydev->speed)
136 ctl |= BMCR_SPEED1000;
137 else if (SPEED_100 == phydev->speed)
138 ctl |= BMCR_SPEED100;
140 if (DUPLEX_FULL == phydev->duplex)
141 ctl |= BMCR_FULLDPLX;
143 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
150 * genphy_restart_aneg - Enable and Restart Autonegotiation
151 * @phydev: target phy_device struct
153 int genphy_restart_aneg(struct phy_device *phydev)
157 ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
162 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
164 /* Don't isolate the PHY if we're negotiating */
165 ctl &= ~(BMCR_ISOLATE);
167 ctl = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
174 * genphy_config_aneg - restart auto-negotiation or write BMCR
175 * @phydev: target phy_device struct
177 * Description: If auto-negotiation is enabled, we configure the
178 * advertising, and then restart auto-negotiation. If it is not
179 * enabled, then we write the BMCR.
181 int genphy_config_aneg(struct phy_device *phydev)
185 if (AUTONEG_ENABLE != phydev->autoneg)
186 return genphy_setup_forced(phydev);
188 result = genphy_config_advert(phydev);
190 if (result < 0) /* error */
194 /* Advertisment hasn't changed, but maybe aneg was never on to
195 * begin with? Or maybe phy was isolated? */
196 int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
201 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
202 result = 1; /* do restart aneg */
205 /* Only restart aneg if we are advertising something different
206 * than we were before. */
208 result = genphy_restart_aneg(phydev);
214 * genphy_update_link - update link status in @phydev
215 * @phydev: target phy_device struct
217 * Description: Update the value in phydev->link to reflect the
218 * current link value. In order to do this, we need to read
219 * the status register twice, keeping the second value.
221 int genphy_update_link(struct phy_device *phydev)
223 unsigned int mii_reg;
226 * Wait if the link is up, and autonegotiation is in progress
227 * (ie - we're capable and it's not done)
229 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
232 * If we already saw the link up, and it hasn't gone down, then
233 * we don't need to wait for autoneg again
235 if (phydev->link && mii_reg & BMSR_LSTATUS)
238 if ((mii_reg & BMSR_ANEGCAPABLE) && !(mii_reg & BMSR_ANEGCOMPLETE)) {
241 printf("%s Waiting for PHY auto negotiation to complete",
243 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
247 if (i > PHY_ANEG_TIMEOUT) {
248 printf(" TIMEOUT !\n");
254 puts("user interrupt!\n");
259 if ((i++ % 500) == 0)
262 udelay(1000); /* 1 ms */
263 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);
271 if (mii_reg & BMSR_LSTATUS)
281 * Generic function which updates the speed and duplex. If
282 * autonegotiation is enabled, it uses the AND of the link
283 * partner's advertised capabilities and our advertised
284 * capabilities. If autonegotiation is disabled, we use the
285 * appropriate bits in the control register.
287 * Stolen from Linux's mii.c and phy_device.c
289 int genphy_parse_link(struct phy_device *phydev)
291 int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
293 /* We're using autonegotiation */
294 if (phydev->supported & SUPPORTED_Autoneg) {
299 /* Check for gigabit capability */
300 if (phydev->supported & (SUPPORTED_1000baseT_Full |
301 SUPPORTED_1000baseT_Half)) {
302 /* We want a list of states supported by
303 * both PHYs in the link
305 gblpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
307 debug("Could not read MII_STAT1000. Ignoring gigabit capability\n");
310 gblpa &= phy_read(phydev,
311 MDIO_DEVAD_NONE, MII_CTRL1000) << 2;
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 lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
332 lpa &= phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
334 if (lpa & (LPA_100FULL | LPA_100HALF)) {
335 phydev->speed = SPEED_100;
337 if (lpa & LPA_100FULL)
338 phydev->duplex = DUPLEX_FULL;
340 } else if (lpa & LPA_10FULL)
341 phydev->duplex = DUPLEX_FULL;
344 * Extended status may indicate that the PHY supports
345 * 1000BASE-T/X even though the 1000BASE-T registers
346 * are missing. In this case we can't tell whether the
347 * peer also supports it, so we only check extended
348 * status if the 1000BASE-T registers are actually
351 if ((mii_reg & BMSR_ESTATEN) && !(mii_reg & BMSR_ERCAP))
352 estatus = phy_read(phydev, MDIO_DEVAD_NONE,
355 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF |
356 ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) {
357 phydev->speed = SPEED_1000;
358 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL))
359 phydev->duplex = DUPLEX_FULL;
363 u32 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
365 phydev->speed = SPEED_10;
366 phydev->duplex = DUPLEX_HALF;
368 if (bmcr & BMCR_FULLDPLX)
369 phydev->duplex = DUPLEX_FULL;
371 if (bmcr & BMCR_SPEED1000)
372 phydev->speed = SPEED_1000;
373 else if (bmcr & BMCR_SPEED100)
374 phydev->speed = SPEED_100;
380 int genphy_config(struct phy_device *phydev)
385 features = (SUPPORTED_TP | SUPPORTED_MII
386 | SUPPORTED_AUI | SUPPORTED_FIBRE |
389 /* Do we support autonegotiation? */
390 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
395 if (val & BMSR_ANEGCAPABLE)
396 features |= SUPPORTED_Autoneg;
398 if (val & BMSR_100FULL)
399 features |= SUPPORTED_100baseT_Full;
400 if (val & BMSR_100HALF)
401 features |= SUPPORTED_100baseT_Half;
402 if (val & BMSR_10FULL)
403 features |= SUPPORTED_10baseT_Full;
404 if (val & BMSR_10HALF)
405 features |= SUPPORTED_10baseT_Half;
407 if (val & BMSR_ESTATEN) {
408 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS);
413 if (val & ESTATUS_1000_TFULL)
414 features |= SUPPORTED_1000baseT_Full;
415 if (val & ESTATUS_1000_THALF)
416 features |= SUPPORTED_1000baseT_Half;
417 if (val & ESTATUS_1000_XFULL)
418 features |= SUPPORTED_1000baseX_Full;
419 if (val & ESTATUS_1000_XHALF)
420 features |= SUPPORTED_1000baseX_Half;
423 phydev->supported &= features;
424 phydev->advertising &= features;
426 genphy_config_aneg(phydev);
431 int genphy_startup(struct phy_device *phydev)
433 genphy_update_link(phydev);
434 genphy_parse_link(phydev);
439 int genphy_shutdown(struct phy_device *phydev)
444 static struct phy_driver genphy_driver = {
447 .name = "Generic PHY",
448 .features = PHY_GBIT_FEATURES | SUPPORTED_MII |
449 SUPPORTED_AUI | SUPPORTED_FIBRE |
451 .config = genphy_config,
452 .startup = genphy_startup,
453 .shutdown = genphy_shutdown,
456 static LIST_HEAD(phy_drivers);
460 #ifdef CONFIG_PHY_AQUANTIA
463 #ifdef CONFIG_PHY_ATHEROS
466 #ifdef CONFIG_PHY_BROADCOM
469 #ifdef CONFIG_PHY_CORTINA
472 #ifdef CONFIG_PHY_DAVICOM
475 #ifdef CONFIG_PHY_ET1011C
478 #ifdef CONFIG_PHY_LXT
481 #ifdef CONFIG_PHY_MARVELL
484 #ifdef CONFIG_PHY_MICREL
487 #ifdef CONFIG_PHY_NATSEMI
490 #ifdef CONFIG_PHY_REALTEK
493 #ifdef CONFIG_PHY_SMSC
496 #ifdef CONFIG_PHY_TERANETICS
497 phy_teranetics_init();
502 #ifdef CONFIG_PHY_VITESSE
509 int phy_register(struct phy_driver *drv)
511 INIT_LIST_HEAD(&drv->list);
512 list_add_tail(&drv->list, &phy_drivers);
514 #ifdef CONFIG_NEEDS_MANUAL_RELOC
516 drv->probe += gd->reloc_off;
518 drv->config += gd->reloc_off;
520 drv->startup += gd->reloc_off;
522 drv->shutdown += gd->reloc_off;
524 drv->readext += gd->reloc_off;
526 drv->writeext += gd->reloc_off;
531 int phy_set_supported(struct phy_device *phydev, u32 max_speed)
533 /* The default values for phydev->supported are provided by the PHY
534 * driver "features" member, we want to reset to sane defaults first
535 * before supporting higher speeds.
537 phydev->supported &= PHY_DEFAULT_FEATURES;
543 phydev->supported |= PHY_1000BT_FEATURES;
546 phydev->supported |= PHY_100BT_FEATURES;
549 phydev->supported |= PHY_10BT_FEATURES;
555 static int phy_probe(struct phy_device *phydev)
559 phydev->advertising = phydev->supported = phydev->drv->features;
560 phydev->mmds = phydev->drv->mmds;
562 if (phydev->drv->probe)
563 err = phydev->drv->probe(phydev);
568 static struct phy_driver *generic_for_interface(phy_interface_t interface)
570 #ifdef CONFIG_PHYLIB_10G
571 if (is_10g_interface(interface))
572 return &gen10g_driver;
575 return &genphy_driver;
578 static struct phy_driver *get_phy_driver(struct phy_device *phydev,
579 phy_interface_t interface)
581 struct list_head *entry;
582 int phy_id = phydev->phy_id;
583 struct phy_driver *drv = NULL;
585 list_for_each(entry, &phy_drivers) {
586 drv = list_entry(entry, struct phy_driver, list);
587 if ((drv->uid & drv->mask) == (phy_id & drv->mask))
591 /* If we made it here, there's no driver for this PHY */
592 return generic_for_interface(interface);
595 static struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
597 phy_interface_t interface)
599 struct phy_device *dev;
601 /* We allocate the device, and initialize the
603 dev = malloc(sizeof(*dev));
605 printf("Failed to allocate PHY device for %s:%d\n",
610 memset(dev, 0, sizeof(*dev));
614 dev->interface = interface;
616 dev->autoneg = AUTONEG_ENABLE;
619 dev->phy_id = phy_id;
622 dev->drv = get_phy_driver(dev, interface);
626 bus->phymap[addr] = dev;
632 * get_phy_id - reads the specified addr for its ID.
633 * @bus: the target MII bus
634 * @addr: PHY address on the MII bus
635 * @phy_id: where to store the ID retrieved.
637 * Description: Reads the ID registers of the PHY at @addr on the
638 * @bus, stores it in @phy_id and returns zero on success.
640 int __weak get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
644 /* Grab the bits from PHYIR1, and put them
645 * in the upper half */
646 phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
651 *phy_id = (phy_reg & 0xffff) << 16;
653 /* Grab the bits from PHYIR2, and put them in the lower half */
654 phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
659 *phy_id |= (phy_reg & 0xffff);
664 static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
665 unsigned phy_mask, int devad, phy_interface_t interface)
667 u32 phy_id = 0xffffffff;
669 int addr = ffs(phy_mask) - 1;
670 int r = get_phy_id(bus, addr, devad, &phy_id);
671 /* If the PHY ID is mostly f's, we didn't find anything */
672 if (r == 0 && (phy_id & 0x1fffffff) != 0x1fffffff)
673 return phy_device_create(bus, addr, phy_id, interface);
674 phy_mask &= ~(1 << addr);
679 static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
680 unsigned phy_mask, phy_interface_t interface)
682 /* If we have one, return the existing device, with new interface */
684 int addr = ffs(phy_mask) - 1;
685 if (bus->phymap[addr]) {
686 bus->phymap[addr]->interface = interface;
687 return bus->phymap[addr];
689 phy_mask &= ~(1 << addr);
694 static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
695 unsigned phy_mask, phy_interface_t interface)
698 struct phy_device *phydev;
700 phydev = search_for_existing_phy(bus, phy_mask, interface);
703 /* Try Standard (ie Clause 22) access */
704 /* Otherwise we have to try Clause 45 */
705 for (i = 0; i < 5; i++) {
706 phydev = create_phy_by_mask(bus, phy_mask,
707 i ? i : MDIO_DEVAD_NONE, interface);
714 debug("\n%s PHY: ", bus->name);
716 int addr = ffs(phy_mask) - 1;
718 phy_mask &= ~(1 << addr);
720 debug("not found\n");
726 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
727 * @bus: the target MII bus
728 * @addr: PHY address on the MII bus
730 * Description: Reads the ID registers of the PHY at @addr on the
731 * @bus, then allocates and returns the phy_device to represent it.
733 static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
734 phy_interface_t interface)
736 return get_phy_device_by_mask(bus, 1 << addr, interface);
739 int phy_reset(struct phy_device *phydev)
743 int devad = MDIO_DEVAD_NONE;
745 #ifdef CONFIG_PHYLIB_10G
746 /* If it's 10G, we need to issue reset through one of the MMDs */
747 if (is_10g_interface(phydev->interface)) {
749 gen10g_discover_mmds(phydev);
751 devad = ffs(phydev->mmds) - 1;
755 if (phy_write(phydev, devad, MII_BMCR, BMCR_RESET) < 0) {
756 debug("PHY reset failed\n");
760 #ifdef CONFIG_PHY_RESET_DELAY
761 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
764 * Poll the control register for the reset bit to go to 0 (it is
765 * auto-clearing). This should happen within 0.5 seconds per the
768 reg = phy_read(phydev, devad, MII_BMCR);
769 while ((reg & BMCR_RESET) && timeout--) {
770 reg = phy_read(phydev, devad, MII_BMCR);
773 debug("PHY status read failed\n");
779 if (reg & BMCR_RESET) {
780 puts("PHY reset timed out\n");
787 int miiphy_reset(const char *devname, unsigned char addr)
789 struct mii_dev *bus = miiphy_get_dev_by_name(devname);
790 struct phy_device *phydev;
793 * miiphy_reset was only used on standard PHYs, so we'll fake it here.
794 * If later code tries to connect with the right interface, this will
795 * be corrected by get_phy_device in phy_connect()
797 phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII);
799 return phy_reset(phydev);
802 struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask,
803 phy_interface_t interface)
809 /* Wait 15ms to make sure the PHY has come out of hard reset */
813 return get_phy_device_by_mask(bus, phy_mask, interface);
817 void phy_connect_dev(struct phy_device *phydev, struct udevice *dev)
819 void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev)
822 /* Soft Reset the PHY */
824 if (phydev->dev && phydev->dev != dev) {
825 printf("%s:%d is connected to %s. Reconnecting to %s\n",
826 phydev->bus->name, phydev->addr,
827 phydev->dev->name, dev->name);
830 debug("%s connected to %s\n", dev->name, phydev->drv->name);
834 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
835 struct udevice *dev, phy_interface_t interface)
837 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
838 struct eth_device *dev, phy_interface_t interface)
841 struct phy_device *phydev;
843 phydev = phy_find_by_mask(bus, 1 << addr, interface);
845 phy_connect_dev(phydev, dev);
847 printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
852 * Start the PHY. Returns 0 on success, or a negative error code.
854 int phy_startup(struct phy_device *phydev)
856 if (phydev->drv->startup)
857 return phydev->drv->startup(phydev);
862 __weak int board_phy_config(struct phy_device *phydev)
864 if (phydev->drv->config)
865 return phydev->drv->config(phydev);
869 int phy_config(struct phy_device *phydev)
871 /* Invoke an optional board-specific helper */
872 board_phy_config(phydev);
877 int phy_shutdown(struct phy_device *phydev)
879 if (phydev->drv->shutdown)
880 phydev->drv->shutdown(phydev);
885 int phy_get_interface_by_name(const char *str)
889 for (i = 0; i < PHY_INTERFACE_MODE_COUNT; i++) {
890 if (!strcmp(str, phy_interface_strings[i]))