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
20 #include <linux/err.h>
21 #include <linux/compiler.h>
23 /* Generic PHY support and helper functions */
26 * genphy_config_advert - sanitize and advertise auto-negotation parameters
27 * @phydev: target phy_device struct
29 * Description: Writes MII_ADVERTISE with the appropriate values,
30 * after sanitizing the values to make sure we only advertise
31 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
32 * hasn't changed, and > 0 if it has changed.
34 static int genphy_config_advert(struct phy_device *phydev)
40 /* Only allow advertising what
41 * this PHY supports */
42 phydev->advertising &= phydev->supported;
43 advertise = phydev->advertising;
45 /* Setup standard advertisement */
46 oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
50 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
51 ADVERTISE_PAUSE_ASYM);
52 if (advertise & ADVERTISED_10baseT_Half)
53 adv |= ADVERTISE_10HALF;
54 if (advertise & ADVERTISED_10baseT_Full)
55 adv |= ADVERTISE_10FULL;
56 if (advertise & ADVERTISED_100baseT_Half)
57 adv |= ADVERTISE_100HALF;
58 if (advertise & ADVERTISED_100baseT_Full)
59 adv |= ADVERTISE_100FULL;
60 if (advertise & ADVERTISED_Pause)
61 adv |= ADVERTISE_PAUSE_CAP;
62 if (advertise & ADVERTISED_Asym_Pause)
63 adv |= ADVERTISE_PAUSE_ASYM;
64 if (advertise & ADVERTISED_1000baseX_Half)
65 adv |= ADVERTISE_1000XHALF;
66 if (advertise & ADVERTISED_1000baseX_Full)
67 adv |= ADVERTISE_1000XFULL;
70 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv);
77 /* Configure gigabit if it's supported */
78 if (phydev->supported & (SUPPORTED_1000baseT_Half |
79 SUPPORTED_1000baseT_Full)) {
80 oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
84 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
85 if (advertise & SUPPORTED_1000baseT_Half)
86 adv |= ADVERTISE_1000HALF;
87 if (advertise & SUPPORTED_1000baseT_Full)
88 adv |= ADVERTISE_1000FULL;
91 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000,
105 * genphy_setup_forced - configures/forces speed/duplex from @phydev
106 * @phydev: target phy_device struct
108 * Description: Configures MII_BMCR to force speed/duplex
109 * to the values in phydev. Assumes that the values are valid.
111 static int genphy_setup_forced(struct phy_device *phydev)
115 phydev->pause = phydev->asym_pause = 0;
117 if (SPEED_1000 == phydev->speed)
118 ctl |= BMCR_SPEED1000;
119 else if (SPEED_100 == phydev->speed)
120 ctl |= BMCR_SPEED100;
122 if (DUPLEX_FULL == phydev->duplex)
123 ctl |= BMCR_FULLDPLX;
125 return phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
130 * genphy_restart_aneg - Enable and Restart Autonegotiation
131 * @phydev: target phy_device struct
133 int genphy_restart_aneg(struct phy_device *phydev)
137 ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
141 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
143 /* Don't isolate the PHY if we're negotiating */
144 ctl &= ~(BMCR_ISOLATE);
146 return phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
151 * genphy_config_aneg - restart auto-negotiation or write BMCR
152 * @phydev: target phy_device struct
154 * Description: If auto-negotiation is enabled, we configure the
155 * advertising, and then restart auto-negotiation. If it is not
156 * enabled, then we write the BMCR.
158 int genphy_config_aneg(struct phy_device *phydev)
162 if (AUTONEG_ENABLE != phydev->autoneg)
163 return genphy_setup_forced(phydev);
165 result = genphy_config_advert(phydev);
167 if (result < 0) /* error */
171 /* Advertisment hasn't changed, but maybe aneg was never on to
172 * begin with? Or maybe phy was isolated? */
173 int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
178 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
179 result = 1; /* do restart aneg */
182 /* Only restart aneg if we are advertising something different
183 * than we were before. */
185 result = genphy_restart_aneg(phydev);
191 * genphy_update_link - update link status in @phydev
192 * @phydev: target phy_device struct
194 * Description: Update the value in phydev->link to reflect the
195 * current link value. In order to do this, we need to read
196 * the status register twice, keeping the second value.
198 int genphy_update_link(struct phy_device *phydev)
204 * Wait if the link is up, and autonegotiation is in progress
205 * (ie - we're capable and it's not done)
207 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
212 * If we already saw the link up, and it hasn't gone down, then
213 * we don't need to wait for autoneg again
215 if (phydev->link && mii_reg & BMSR_LSTATUS)
218 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
222 if (!(bmcr & BMCR_ANENABLE))
225 if ((mii_reg & BMSR_ANEGCAPABLE) && !(mii_reg & BMSR_ANEGCOMPLETE)) {
228 printf("%s Waiting for PHY auto negotiation to complete",
230 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
234 if (i > PHY_ANEG_TIMEOUT) {
235 printf(" TIMEOUT !\n");
241 puts("user interrupt!\n");
246 if ((i++ % 500) == 0)
249 udelay(1000); /* 1 ms */
250 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
257 /* Read the link a second time to clear the latched state */
258 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
262 if (mii_reg & BMSR_LSTATUS)
272 * Generic function which updates the speed and duplex. If
273 * autonegotiation is enabled, it uses the AND of the link
274 * partner's advertised capabilities and our advertised
275 * capabilities. If autonegotiation is disabled, we use the
276 * appropriate bits in the control register.
278 * Stolen from Linux's mii.c and phy_device.c
280 int genphy_parse_link(struct phy_device *phydev)
282 int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
287 /* We're using autonegotiation */
288 if (phydev->supported & SUPPORTED_Autoneg) {
294 /* Check for gigabit capability */
295 if (phydev->supported & (SUPPORTED_1000baseT_Full |
296 SUPPORTED_1000baseT_Half)) {
297 /* We want a list of states supported by
298 * both PHYs in the link
300 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
302 debug("Could not read MII_STAT1000. Ignoring gigabit capability\n");
307 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
313 /* Set the baseline so we only have to set them
314 * if they're different
316 phydev->speed = SPEED_10;
317 phydev->duplex = DUPLEX_HALF;
319 /* Check the gigabit fields */
320 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
321 phydev->speed = SPEED_1000;
323 if (gblpa & PHY_1000BTSR_1000FD)
324 phydev->duplex = DUPLEX_FULL;
330 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
335 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
340 if (lpa & (LPA_100FULL | LPA_100HALF)) {
341 phydev->speed = SPEED_100;
343 if (lpa & LPA_100FULL)
344 phydev->duplex = DUPLEX_FULL;
346 } else if (lpa & LPA_10FULL)
347 phydev->duplex = DUPLEX_FULL;
350 * Extended status may indicate that the PHY supports
351 * 1000BASE-T/X even though the 1000BASE-T registers
352 * are missing. In this case we can't tell whether the
353 * peer also supports it, so we only check extended
354 * status if the 1000BASE-T registers are actually
357 if ((mii_reg & BMSR_ESTATEN) && !(mii_reg & BMSR_ERCAP))
358 estatus = phy_read(phydev, MDIO_DEVAD_NONE,
363 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF |
364 ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) {
365 phydev->speed = SPEED_1000;
366 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL))
367 phydev->duplex = DUPLEX_FULL;
371 int bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
376 phydev->speed = SPEED_10;
377 phydev->duplex = DUPLEX_HALF;
379 if (bmcr & BMCR_FULLDPLX)
380 phydev->duplex = DUPLEX_FULL;
382 if (bmcr & BMCR_SPEED1000)
383 phydev->speed = SPEED_1000;
384 else if (bmcr & BMCR_SPEED100)
385 phydev->speed = SPEED_100;
391 int genphy_config(struct phy_device *phydev)
396 /* For now, I'll claim that the generic driver supports
397 * all possible port types */
398 features = (SUPPORTED_TP | SUPPORTED_MII
399 | SUPPORTED_AUI | SUPPORTED_FIBRE |
402 /* Do we support autonegotiation? */
403 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
407 if (val & BMSR_ANEGCAPABLE)
408 features |= SUPPORTED_Autoneg;
410 if (val & BMSR_100FULL)
411 features |= SUPPORTED_100baseT_Full;
412 if (val & BMSR_100HALF)
413 features |= SUPPORTED_100baseT_Half;
414 if (val & BMSR_10FULL)
415 features |= SUPPORTED_10baseT_Full;
416 if (val & BMSR_10HALF)
417 features |= SUPPORTED_10baseT_Half;
419 if (val & BMSR_ESTATEN) {
420 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS);
425 if (val & ESTATUS_1000_TFULL)
426 features |= SUPPORTED_1000baseT_Full;
427 if (val & ESTATUS_1000_THALF)
428 features |= SUPPORTED_1000baseT_Half;
429 if (val & ESTATUS_1000_XFULL)
430 features |= SUPPORTED_1000baseX_Full;
431 if (val & ESTATUS_1000_XHALF)
432 features |= SUPPORTED_1000baseX_Half;
435 phydev->supported = features;
436 phydev->advertising = features;
438 genphy_config_aneg(phydev);
443 int genphy_startup(struct phy_device *phydev)
445 genphy_update_link(phydev);
446 genphy_parse_link(phydev);
451 int genphy_shutdown(struct phy_device *phydev)
456 static struct phy_driver genphy_driver = {
459 .name = "Generic PHY",
461 .config = genphy_config,
462 .startup = genphy_startup,
463 .shutdown = genphy_shutdown,
466 static LIST_HEAD(phy_drivers);
470 #ifdef CONFIG_PHY_ATHEROS
473 #ifdef CONFIG_PHY_BROADCOM
476 #ifdef CONFIG_PHY_CORTINA
479 #ifdef CONFIG_PHY_DAVICOM
482 #ifdef CONFIG_PHY_ET1011C
485 #ifdef CONFIG_PHY_LXT
488 #ifdef CONFIG_PHY_MARVELL
491 #ifdef CONFIG_PHY_MICREL
494 #ifdef CONFIG_PHY_NATSEMI
497 #ifdef CONFIG_PHY_REALTEK
500 #ifdef CONFIG_PHY_SMSC
503 #ifdef CONFIG_PHY_TERANETICS
504 phy_teranetics_init();
506 #ifdef CONFIG_PHY_VITESSE
513 int phy_register(struct phy_driver *drv)
515 INIT_LIST_HEAD(&drv->list);
516 list_add_tail(&drv->list, &phy_drivers);
521 static int phy_probe(struct phy_device *phydev)
525 phydev->advertising = phydev->supported = phydev->drv->features;
526 phydev->mmds = phydev->drv->mmds;
528 if (phydev->drv->probe)
529 err = phydev->drv->probe(phydev);
534 static struct phy_driver *generic_for_interface(phy_interface_t interface)
536 #ifdef CONFIG_PHYLIB_10G
537 if (is_10g_interface(interface))
538 return &gen10g_driver;
541 return &genphy_driver;
544 static struct phy_driver *get_phy_driver(struct phy_device *phydev,
545 phy_interface_t interface)
547 struct list_head *entry;
548 int phy_id = phydev->phy_id;
549 struct phy_driver *drv = NULL;
551 list_for_each(entry, &phy_drivers) {
552 drv = list_entry(entry, struct phy_driver, list);
553 if ((drv->uid & drv->mask) == (phy_id & drv->mask))
557 /* If we made it here, there's no driver for this PHY */
558 return generic_for_interface(interface);
561 static int aneg_enabled(struct phy_device *phydev)
563 static const char *aneg = "_aneg";
564 char varname[strlen(phydev->bus->name) + strlen(aneg) + 1];
566 snprintf(varname, sizeof(varname), "%s%s", phydev->bus->name, aneg);
567 return getenv_yesno(varname);
570 static int phy_get_speed(struct phy_device *phydev)
573 static const char *aneg = "_speed";
574 char varname[strlen(phydev->bus->name) + strlen(aneg) + 1];
577 snprintf(varname, sizeof(varname), "%s%s", phydev->bus->name, aneg);
579 val = getenv_ulong(varname, 10, 100);
591 printf("Improper setting '%s' for %s; assuming 100\n",
592 getenv(varname), varname);
598 static int phy_get_duplex(struct phy_device *phydev)
600 int ret = DUPLEX_FULL;
601 static const char *aneg = "_duplex";
602 char varname[strlen(phydev->bus->name) + strlen(aneg) + 4];
605 snprintf(varname, sizeof(varname), "%s%d%s",
606 phydev->bus->name, phydev->addr, aneg);
608 val = getenv(varname);
610 if (strcasecmp(val, "full") != 0) {
611 if (strcasecmp(val, "half") == 0) {
614 printf("Improper setting '%s' for %s; assuming 'full'\n",
616 printf("Expected one of: 'full', 'half'\n");
624 static struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
626 phy_interface_t interface)
628 struct phy_device *dev;
630 /* We allocate the device, and initialize the
632 dev = malloc(sizeof(*dev));
634 printf("Failed to allocate PHY device for %s:%d\n",
639 memset(dev, 0, sizeof(*dev));
643 dev->interface = interface;
646 dev->phy_id = phy_id;
649 dev->drv = get_phy_driver(dev, interface);
651 if (aneg_enabled(dev)) {
652 dev->autoneg = AUTONEG_ENABLE;
654 dev->autoneg = AUTONEG_DISABLE;
655 dev->speed = phy_get_speed(dev);
656 dev->duplex = phy_get_duplex(dev);
658 switch (dev->speed) {
660 if (dev->duplex == DUPLEX_FULL)
661 dev->supported &= SUPPORTED_1000baseT_Full;
663 dev->supported &= SUPPORTED_1000baseT_Half;
666 if (dev->duplex == DUPLEX_FULL)
667 dev->supported &= SUPPORTED_100baseT_Full;
669 dev->supported &= SUPPORTED_100baseT_Half;
672 if (dev->duplex == DUPLEX_FULL)
673 dev->supported &= SUPPORTED_10baseT_Full;
675 dev->supported &= SUPPORTED_10baseT_Half;
678 printf("Unsupported speed: %d\n", dev->speed);
684 bus->phymap[addr] = dev;
690 * get_phy_id - reads the specified addr for its ID.
691 * @bus: the target MII bus
692 * @addr: PHY address on the MII bus
693 * @phy_id: where to store the ID retrieved.
695 * Description: Reads the ID registers of the PHY at @addr on the
696 * @bus, stores it in @phy_id and returns zero on success.
698 static int get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
702 /* Grab the bits from PHYIR1, and put them
703 * in the upper half */
704 phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
709 *phy_id = (phy_reg & 0xffff) << 16;
711 /* Grab the bits from PHYIR2, and put them in the lower half */
712 phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
717 *phy_id |= (phy_reg & 0xffff);
722 static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
723 unsigned phy_mask, int devad, phy_interface_t interface)
725 u32 phy_id = 0xffffffff;
727 int addr = ffs(phy_mask) - 1;
728 int r = get_phy_id(bus, addr, devad, &phy_id);
729 /* If the PHY ID is mostly f's, we didn't find anything */
730 if (r == 0 && (phy_id & 0x1fffffff) != 0x1fffffff)
731 return phy_device_create(bus, addr, phy_id, interface);
732 phy_mask &= ~(1 << addr);
737 static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
738 unsigned phy_mask, phy_interface_t interface)
740 /* If we have one, return the existing device, with new interface */
742 int addr = ffs(phy_mask) - 1;
743 if (bus->phymap[addr]) {
744 bus->phymap[addr]->interface = interface;
745 return bus->phymap[addr];
747 phy_mask &= ~(1 << addr);
752 static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
753 unsigned phy_mask, phy_interface_t interface)
756 struct phy_device *phydev;
758 phydev = search_for_existing_phy(bus, phy_mask, interface);
761 /* Try Standard (ie Clause 22) access */
762 /* Otherwise we have to try Clause 45 */
763 for (i = 0; i < 5; i++) {
764 phydev = create_phy_by_mask(bus, phy_mask,
765 i ? i : MDIO_DEVAD_NONE, interface);
771 printf("Phy %d not found\n", ffs(phy_mask) - 1);
772 return phy_device_create(bus, ffs(phy_mask) - 1, 0xffffffff, interface);
776 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
777 * @bus: the target MII bus
778 * @addr: PHY address on the MII bus
780 * Description: Reads the ID registers of the PHY at @addr on the
781 * @bus, then allocates and returns the phy_device to represent it.
783 static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
784 phy_interface_t interface)
786 return get_phy_device_by_mask(bus, 1 << addr, interface);
789 int phy_reset(struct phy_device *phydev)
794 int devad = MDIO_DEVAD_NONE;
796 #ifdef CONFIG_PHYLIB_10G
797 /* If it's 10G, we need to issue reset through one of the MMDs */
798 if (is_10g_interface(phydev->interface)) {
800 gen10g_discover_mmds(phydev);
802 devad = ffs(phydev->mmds) - 1;
806 reg = phy_read(phydev, devad, MII_BMCR);
808 debug("PHY status read failed\n");
814 err = phy_write(phydev, devad, MII_BMCR, reg);
816 debug("PHY reset failed\n");
820 #ifdef CONFIG_PHY_RESET_DELAY
821 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
824 * Poll the control register for the reset bit to go to 0 (it is
825 * auto-clearing). This should happen within 0.5 seconds per the
828 while ((reg & BMCR_RESET) && timeout-- >= 0) {
829 reg = phy_read(phydev, devad, MII_BMCR);
832 debug("PHY status read failed\n");
838 if (reg & BMCR_RESET) {
839 puts("PHY reset timed out\n");
846 int miiphy_reset(const char *devname, unsigned char addr)
848 struct mii_dev *bus = miiphy_get_dev_by_name(devname);
849 struct phy_device *phydev;
852 * miiphy_reset was only used on standard PHYs, so we'll fake it here.
853 * If later code tries to connect with the right interface, this will
854 * be corrected by get_phy_device in phy_connect()
856 phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII);
858 return phy_reset(phydev);
861 struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask,
862 phy_interface_t interface)
868 /* Wait 15ms to make sure the PHY has come out of hard reset */
870 return get_phy_device_by_mask(bus, phy_mask, interface);
873 void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev)
875 /* Soft Reset the PHY */
878 printf("%s:%d is connected to %s. Reconnecting to %s\n",
879 phydev->bus->name, phydev->addr,
880 phydev->dev->name, dev->name);
883 debug("%s connected to %s\n", dev->name, phydev->drv->name);
886 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
887 struct eth_device *dev, phy_interface_t interface)
889 struct phy_device *phydev;
891 phydev = phy_find_by_mask(bus, 1 << addr, interface);
893 phy_connect_dev(phydev, dev);
895 printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
900 * Start the PHY. Returns 0 on success, or a negative error code.
902 int phy_startup(struct phy_device *phydev)
904 if (phydev->drv->startup)
905 return phydev->drv->startup(phydev);
910 __weak int board_phy_config(struct phy_device *phydev)
912 if (phydev->drv->config)
913 return phydev->drv->config(phydev);
917 int phy_config(struct phy_device *phydev)
919 /* Invoke an optional board-specific helper */
920 board_phy_config(phydev);
925 int phy_shutdown(struct phy_device *phydev)
927 if (phydev->drv->shutdown)
928 phydev->drv->shutdown(phydev);