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)
131 int ctl = BMCR_ANRESTART;
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 ((phydev->autoneg == AUTONEG_ENABLE) &&
239 !(mii_reg & BMSR_ANEGCOMPLETE)) {
242 printf("%s Waiting for PHY auto negotiation to complete",
244 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
248 if (i > PHY_ANEG_TIMEOUT) {
249 printf(" TIMEOUT !\n");
255 puts("user interrupt!\n");
260 if ((i++ % 500) == 0)
263 udelay(1000); /* 1 ms */
264 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
269 /* Read the link a second time to clear the latched state */
270 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
272 if (mii_reg & BMSR_LSTATUS)
282 * Generic function which updates the speed and duplex. If
283 * autonegotiation is enabled, it uses the AND of the link
284 * partner's advertised capabilities and our advertised
285 * capabilities. If autonegotiation is disabled, we use the
286 * appropriate bits in the control register.
288 * Stolen from Linux's mii.c and phy_device.c
290 int genphy_parse_link(struct phy_device *phydev)
292 int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
294 /* We're using autonegotiation */
295 if (phydev->autoneg == AUTONEG_ENABLE) {
300 /* Check for gigabit capability */
301 if (phydev->supported & (SUPPORTED_1000baseT_Full |
302 SUPPORTED_1000baseT_Half)) {
303 /* We want a list of states supported by
304 * both PHYs in the link
306 gblpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
308 debug("Could not read MII_STAT1000. Ignoring gigabit capability\n");
311 gblpa &= phy_read(phydev,
312 MDIO_DEVAD_NONE, MII_CTRL1000) << 2;
315 /* Set the baseline so we only have to set them
316 * if they're different
318 phydev->speed = SPEED_10;
319 phydev->duplex = DUPLEX_HALF;
321 /* Check the gigabit fields */
322 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
323 phydev->speed = SPEED_1000;
325 if (gblpa & PHY_1000BTSR_1000FD)
326 phydev->duplex = DUPLEX_FULL;
332 lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
333 lpa &= phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
335 if (lpa & (LPA_100FULL | LPA_100HALF)) {
336 phydev->speed = SPEED_100;
338 if (lpa & LPA_100FULL)
339 phydev->duplex = DUPLEX_FULL;
341 } else if (lpa & LPA_10FULL)
342 phydev->duplex = DUPLEX_FULL;
345 * Extended status may indicate that the PHY supports
346 * 1000BASE-T/X even though the 1000BASE-T registers
347 * are missing. In this case we can't tell whether the
348 * peer also supports it, so we only check extended
349 * status if the 1000BASE-T registers are actually
352 if ((mii_reg & BMSR_ESTATEN) && !(mii_reg & BMSR_ERCAP))
353 estatus = phy_read(phydev, MDIO_DEVAD_NONE,
356 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF |
357 ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) {
358 phydev->speed = SPEED_1000;
359 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL))
360 phydev->duplex = DUPLEX_FULL;
364 u32 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
366 phydev->speed = SPEED_10;
367 phydev->duplex = DUPLEX_HALF;
369 if (bmcr & BMCR_FULLDPLX)
370 phydev->duplex = DUPLEX_FULL;
372 if (bmcr & BMCR_SPEED1000)
373 phydev->speed = SPEED_1000;
374 else if (bmcr & BMCR_SPEED100)
375 phydev->speed = SPEED_100;
381 int genphy_config(struct phy_device *phydev)
386 features = (SUPPORTED_TP | SUPPORTED_MII
387 | SUPPORTED_AUI | SUPPORTED_FIBRE |
390 /* Do we support autonegotiation? */
391 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
396 if (val & BMSR_ANEGCAPABLE)
397 features |= SUPPORTED_Autoneg;
399 if (val & BMSR_100FULL)
400 features |= SUPPORTED_100baseT_Full;
401 if (val & BMSR_100HALF)
402 features |= SUPPORTED_100baseT_Half;
403 if (val & BMSR_10FULL)
404 features |= SUPPORTED_10baseT_Full;
405 if (val & BMSR_10HALF)
406 features |= SUPPORTED_10baseT_Half;
408 if (val & BMSR_ESTATEN) {
409 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS);
414 if (val & ESTATUS_1000_TFULL)
415 features |= SUPPORTED_1000baseT_Full;
416 if (val & ESTATUS_1000_THALF)
417 features |= SUPPORTED_1000baseT_Half;
418 if (val & ESTATUS_1000_XFULL)
419 features |= SUPPORTED_1000baseX_Full;
420 if (val & ESTATUS_1000_XHALF)
421 features |= SUPPORTED_1000baseX_Half;
424 phydev->supported &= features;
425 phydev->advertising &= features;
427 genphy_config_aneg(phydev);
432 int genphy_startup(struct phy_device *phydev)
434 genphy_update_link(phydev);
435 genphy_parse_link(phydev);
440 int genphy_shutdown(struct phy_device *phydev)
445 static struct phy_driver genphy_driver = {
448 .name = "Generic PHY",
449 .features = PHY_GBIT_FEATURES | SUPPORTED_MII |
450 SUPPORTED_AUI | SUPPORTED_FIBRE |
452 .config = genphy_config,
453 .startup = genphy_startup,
454 .shutdown = genphy_shutdown,
457 static LIST_HEAD(phy_drivers);
461 #ifdef CONFIG_PHY_AQUANTIA
464 #ifdef CONFIG_PHY_ATHEROS
467 #ifdef CONFIG_PHY_BROADCOM
470 #ifdef CONFIG_PHY_CORTINA
473 #ifdef CONFIG_PHY_DAVICOM
476 #ifdef CONFIG_PHY_ET1011C
479 #ifdef CONFIG_PHY_LXT
482 #ifdef CONFIG_PHY_MARVELL
485 #ifdef CONFIG_PHY_MICREL
488 #ifdef CONFIG_PHY_NATSEMI
491 #ifdef CONFIG_PHY_REALTEK
494 #ifdef CONFIG_PHY_SMSC
497 #ifdef CONFIG_PHY_TERANETICS
498 phy_teranetics_init();
503 #ifdef CONFIG_PHY_VITESSE
506 #ifdef CONFIG_PHY_XILINX
513 int phy_register(struct phy_driver *drv)
515 INIT_LIST_HEAD(&drv->list);
516 list_add_tail(&drv->list, &phy_drivers);
518 #ifdef CONFIG_NEEDS_MANUAL_RELOC
520 drv->probe += gd->reloc_off;
522 drv->config += gd->reloc_off;
524 drv->startup += gd->reloc_off;
526 drv->shutdown += gd->reloc_off;
528 drv->readext += gd->reloc_off;
530 drv->writeext += gd->reloc_off;
535 int phy_set_supported(struct phy_device *phydev, u32 max_speed)
537 /* The default values for phydev->supported are provided by the PHY
538 * driver "features" member, we want to reset to sane defaults first
539 * before supporting higher speeds.
541 phydev->supported &= PHY_DEFAULT_FEATURES;
547 phydev->supported |= PHY_1000BT_FEATURES;
550 phydev->supported |= PHY_100BT_FEATURES;
553 phydev->supported |= PHY_10BT_FEATURES;
559 static int phy_probe(struct phy_device *phydev)
563 phydev->advertising = phydev->supported = phydev->drv->features;
564 phydev->mmds = phydev->drv->mmds;
566 if (phydev->drv->probe)
567 err = phydev->drv->probe(phydev);
572 static struct phy_driver *generic_for_interface(phy_interface_t interface)
574 #ifdef CONFIG_PHYLIB_10G
575 if (is_10g_interface(interface))
576 return &gen10g_driver;
579 return &genphy_driver;
582 static struct phy_driver *get_phy_driver(struct phy_device *phydev,
583 phy_interface_t interface)
585 struct list_head *entry;
586 int phy_id = phydev->phy_id;
587 struct phy_driver *drv = NULL;
589 list_for_each(entry, &phy_drivers) {
590 drv = list_entry(entry, struct phy_driver, list);
591 if ((drv->uid & drv->mask) == (phy_id & drv->mask))
595 /* If we made it here, there's no driver for this PHY */
596 return generic_for_interface(interface);
599 static struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
601 phy_interface_t interface)
603 struct phy_device *dev;
605 /* We allocate the device, and initialize the
607 dev = malloc(sizeof(*dev));
609 printf("Failed to allocate PHY device for %s:%d\n",
614 memset(dev, 0, sizeof(*dev));
618 dev->interface = interface;
620 dev->autoneg = AUTONEG_ENABLE;
623 dev->phy_id = phy_id;
626 dev->drv = get_phy_driver(dev, interface);
630 bus->phymap[addr] = dev;
636 * get_phy_id - reads the specified addr for its ID.
637 * @bus: the target MII bus
638 * @addr: PHY address on the MII bus
639 * @phy_id: where to store the ID retrieved.
641 * Description: Reads the ID registers of the PHY at @addr on the
642 * @bus, stores it in @phy_id and returns zero on success.
644 int __weak get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
648 /* Grab the bits from PHYIR1, and put them
649 * in the upper half */
650 phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
655 *phy_id = (phy_reg & 0xffff) << 16;
657 /* Grab the bits from PHYIR2, and put them in the lower half */
658 phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
663 *phy_id |= (phy_reg & 0xffff);
668 static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
669 unsigned phy_mask, int devad, phy_interface_t interface)
671 u32 phy_id = 0xffffffff;
673 int addr = ffs(phy_mask) - 1;
674 int r = get_phy_id(bus, addr, devad, &phy_id);
675 /* If the PHY ID is mostly f's, we didn't find anything */
676 if (r == 0 && (phy_id & 0x1fffffff) != 0x1fffffff)
677 return phy_device_create(bus, addr, phy_id, interface);
678 phy_mask &= ~(1 << addr);
683 static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
684 unsigned phy_mask, phy_interface_t interface)
686 /* If we have one, return the existing device, with new interface */
688 int addr = ffs(phy_mask) - 1;
689 if (bus->phymap[addr]) {
690 bus->phymap[addr]->interface = interface;
691 return bus->phymap[addr];
693 phy_mask &= ~(1 << addr);
698 static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
699 unsigned phy_mask, phy_interface_t interface)
702 struct phy_device *phydev;
704 phydev = search_for_existing_phy(bus, phy_mask, interface);
707 /* Try Standard (ie Clause 22) access */
708 /* Otherwise we have to try Clause 45 */
709 for (i = 0; i < 5; i++) {
710 phydev = create_phy_by_mask(bus, phy_mask,
711 i ? i : MDIO_DEVAD_NONE, interface);
718 debug("\n%s PHY: ", bus->name);
720 int addr = ffs(phy_mask) - 1;
722 phy_mask &= ~(1 << addr);
724 debug("not found\n");
730 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
731 * @bus: the target MII bus
732 * @addr: PHY address on the MII bus
734 * Description: Reads the ID registers of the PHY at @addr on the
735 * @bus, then allocates and returns the phy_device to represent it.
737 static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
738 phy_interface_t interface)
740 return get_phy_device_by_mask(bus, 1 << addr, interface);
743 int phy_reset(struct phy_device *phydev)
747 int devad = MDIO_DEVAD_NONE;
749 if (phydev->flags & PHY_FLAG_BROKEN_RESET)
752 #ifdef CONFIG_PHYLIB_10G
753 /* If it's 10G, we need to issue reset through one of the MMDs */
754 if (is_10g_interface(phydev->interface)) {
756 gen10g_discover_mmds(phydev);
758 devad = ffs(phydev->mmds) - 1;
762 if (phy_write(phydev, devad, MII_BMCR, BMCR_RESET) < 0) {
763 debug("PHY reset failed\n");
767 #ifdef CONFIG_PHY_RESET_DELAY
768 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
771 * Poll the control register for the reset bit to go to 0 (it is
772 * auto-clearing). This should happen within 0.5 seconds per the
775 reg = phy_read(phydev, devad, MII_BMCR);
776 while ((reg & BMCR_RESET) && timeout--) {
777 reg = phy_read(phydev, devad, MII_BMCR);
780 debug("PHY status read failed\n");
786 if (reg & BMCR_RESET) {
787 puts("PHY reset timed out\n");
794 int miiphy_reset(const char *devname, unsigned char addr)
796 struct mii_dev *bus = miiphy_get_dev_by_name(devname);
797 struct phy_device *phydev;
800 * miiphy_reset was only used on standard PHYs, so we'll fake it here.
801 * If later code tries to connect with the right interface, this will
802 * be corrected by get_phy_device in phy_connect()
804 phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII);
806 return phy_reset(phydev);
809 struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask,
810 phy_interface_t interface)
816 /* Wait 15ms to make sure the PHY has come out of hard reset */
820 return get_phy_device_by_mask(bus, phy_mask, interface);
824 void phy_connect_dev(struct phy_device *phydev, struct udevice *dev)
826 void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev)
829 /* Soft Reset the PHY */
831 if (phydev->dev && phydev->dev != dev) {
832 printf("%s:%d is connected to %s. Reconnecting to %s\n",
833 phydev->bus->name, phydev->addr,
834 phydev->dev->name, dev->name);
837 debug("%s connected to %s\n", dev->name, phydev->drv->name);
841 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
842 struct udevice *dev, phy_interface_t interface)
844 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
845 struct eth_device *dev, phy_interface_t interface)
848 struct phy_device *phydev;
850 phydev = phy_find_by_mask(bus, 1 << addr, interface);
852 phy_connect_dev(phydev, dev);
854 printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
859 * Start the PHY. Returns 0 on success, or a negative error code.
861 int phy_startup(struct phy_device *phydev)
863 if (phydev->drv->startup)
864 return phydev->drv->startup(phydev);
869 __weak int board_phy_config(struct phy_device *phydev)
871 if (phydev->drv->config)
872 return phydev->drv->config(phydev);
876 int phy_config(struct phy_device *phydev)
878 /* Invoke an optional board-specific helper */
879 board_phy_config(phydev);
884 int phy_shutdown(struct phy_device *phydev)
886 if (phydev->drv->shutdown)
887 phydev->drv->shutdown(phydev);
892 int phy_get_interface_by_name(const char *str)
896 for (i = 0; i < PHY_INTERFACE_MODE_COUNT; i++) {
897 if (!strcmp(str, phy_interface_strings[i]))