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);
52 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
53 ADVERTISE_PAUSE_ASYM);
54 if (advertise & ADVERTISED_10baseT_Half)
55 adv |= ADVERTISE_10HALF;
56 if (advertise & ADVERTISED_10baseT_Full)
57 adv |= ADVERTISE_10FULL;
58 if (advertise & ADVERTISED_100baseT_Half)
59 adv |= ADVERTISE_100HALF;
60 if (advertise & ADVERTISED_100baseT_Full)
61 adv |= ADVERTISE_100FULL;
62 if (advertise & ADVERTISED_Pause)
63 adv |= ADVERTISE_PAUSE_CAP;
64 if (advertise & ADVERTISED_Asym_Pause)
65 adv |= ADVERTISE_PAUSE_ASYM;
66 if (advertise & ADVERTISED_1000baseX_Half)
67 adv |= ADVERTISE_1000XHALF;
68 if (advertise & ADVERTISED_1000baseX_Full)
69 adv |= ADVERTISE_1000XFULL;
72 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv);
79 /* Configure gigabit if it's supported */
80 if (phydev->supported & (SUPPORTED_1000baseT_Half |
81 SUPPORTED_1000baseT_Full)) {
82 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)
119 phydev->pause = phydev->asym_pause = 0;
121 if (SPEED_1000 == phydev->speed)
122 ctl |= BMCR_SPEED1000;
123 else if (SPEED_100 == phydev->speed)
124 ctl |= BMCR_SPEED100;
126 if (DUPLEX_FULL == phydev->duplex)
127 ctl |= BMCR_FULLDPLX;
129 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
136 * genphy_restart_aneg - Enable and Restart Autonegotiation
137 * @phydev: target phy_device struct
139 int genphy_restart_aneg(struct phy_device *phydev)
143 ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
148 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
150 /* Don't isolate the PHY if we're negotiating */
151 ctl &= ~(BMCR_ISOLATE);
153 ctl = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
160 * genphy_config_aneg - restart auto-negotiation or write BMCR
161 * @phydev: target phy_device struct
163 * Description: If auto-negotiation is enabled, we configure the
164 * advertising, and then restart auto-negotiation. If it is not
165 * enabled, then we write the BMCR.
167 int genphy_config_aneg(struct phy_device *phydev)
171 if (AUTONEG_ENABLE != phydev->autoneg)
172 return genphy_setup_forced(phydev);
174 result = genphy_config_advert(phydev);
176 if (result < 0) /* error */
180 /* Advertisment hasn't changed, but maybe aneg was never on to
181 * begin with? Or maybe phy was isolated? */
182 int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
187 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
188 result = 1; /* do restart aneg */
191 /* Only restart aneg if we are advertising something different
192 * than we were before. */
194 result = genphy_restart_aneg(phydev);
200 * genphy_update_link - update link status in @phydev
201 * @phydev: target phy_device struct
203 * Description: Update the value in phydev->link to reflect the
204 * current link value. In order to do this, we need to read
205 * the status register twice, keeping the second value.
207 int genphy_update_link(struct phy_device *phydev)
209 unsigned int mii_reg;
212 * Wait if the link is up, and autonegotiation is in progress
213 * (ie - we're capable and it's not done)
215 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
218 * If we already saw the link up, and it hasn't gone down, then
219 * we don't need to wait for autoneg again
221 if (phydev->link && mii_reg & BMSR_LSTATUS)
224 if ((mii_reg & BMSR_ANEGCAPABLE) && !(mii_reg & BMSR_ANEGCOMPLETE)) {
227 printf("%s Waiting for PHY auto negotiation to complete",
229 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
233 if (i > PHY_ANEG_TIMEOUT) {
234 printf(" TIMEOUT !\n");
240 puts("user interrupt!\n");
245 if ((i++ % 500) == 0)
248 udelay(1000); /* 1 ms */
249 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
254 /* Read the link a second time to clear the latched state */
255 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
257 if (mii_reg & BMSR_LSTATUS)
267 * Generic function which updates the speed and duplex. If
268 * autonegotiation is enabled, it uses the AND of the link
269 * partner's advertised capabilities and our advertised
270 * capabilities. If autonegotiation is disabled, we use the
271 * appropriate bits in the control register.
273 * Stolen from Linux's mii.c and phy_device.c
275 int genphy_parse_link(struct phy_device *phydev)
277 int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
279 /* We're using autonegotiation */
280 if (phydev->supported & SUPPORTED_Autoneg) {
285 /* Check for gigabit capability */
286 if (phydev->supported & (SUPPORTED_1000baseT_Full |
287 SUPPORTED_1000baseT_Half)) {
288 /* We want a list of states supported by
289 * both PHYs in the link
291 gblpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
293 debug("Could not read MII_STAT1000. Ignoring gigabit capability\n");
296 gblpa &= phy_read(phydev,
297 MDIO_DEVAD_NONE, MII_CTRL1000) << 2;
300 /* Set the baseline so we only have to set them
301 * if they're different
303 phydev->speed = SPEED_10;
304 phydev->duplex = DUPLEX_HALF;
306 /* Check the gigabit fields */
307 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
308 phydev->speed = SPEED_1000;
310 if (gblpa & PHY_1000BTSR_1000FD)
311 phydev->duplex = DUPLEX_FULL;
317 lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
318 lpa &= phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
320 if (lpa & (LPA_100FULL | LPA_100HALF)) {
321 phydev->speed = SPEED_100;
323 if (lpa & LPA_100FULL)
324 phydev->duplex = DUPLEX_FULL;
326 } else if (lpa & LPA_10FULL)
327 phydev->duplex = DUPLEX_FULL;
330 * Extended status may indicate that the PHY supports
331 * 1000BASE-T/X even though the 1000BASE-T registers
332 * are missing. In this case we can't tell whether the
333 * peer also supports it, so we only check extended
334 * status if the 1000BASE-T registers are actually
337 if ((mii_reg & BMSR_ESTATEN) && !(mii_reg & BMSR_ERCAP))
338 estatus = phy_read(phydev, MDIO_DEVAD_NONE,
341 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF |
342 ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) {
343 phydev->speed = SPEED_1000;
344 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL))
345 phydev->duplex = DUPLEX_FULL;
349 u32 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
351 phydev->speed = SPEED_10;
352 phydev->duplex = DUPLEX_HALF;
354 if (bmcr & BMCR_FULLDPLX)
355 phydev->duplex = DUPLEX_FULL;
357 if (bmcr & BMCR_SPEED1000)
358 phydev->speed = SPEED_1000;
359 else if (bmcr & BMCR_SPEED100)
360 phydev->speed = SPEED_100;
366 int genphy_config(struct phy_device *phydev)
371 /* For now, I'll claim that the generic driver supports
372 * all possible port types */
373 features = (SUPPORTED_TP | SUPPORTED_MII
374 | SUPPORTED_AUI | SUPPORTED_FIBRE |
377 /* Do we support autonegotiation? */
378 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
383 if (val & BMSR_ANEGCAPABLE)
384 features |= SUPPORTED_Autoneg;
386 if (val & BMSR_100FULL)
387 features |= SUPPORTED_100baseT_Full;
388 if (val & BMSR_100HALF)
389 features |= SUPPORTED_100baseT_Half;
390 if (val & BMSR_10FULL)
391 features |= SUPPORTED_10baseT_Full;
392 if (val & BMSR_10HALF)
393 features |= SUPPORTED_10baseT_Half;
395 if (val & BMSR_ESTATEN) {
396 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS);
401 if (val & ESTATUS_1000_TFULL)
402 features |= SUPPORTED_1000baseT_Full;
403 if (val & ESTATUS_1000_THALF)
404 features |= SUPPORTED_1000baseT_Half;
405 if (val & ESTATUS_1000_XFULL)
406 features |= SUPPORTED_1000baseX_Full;
407 if (val & ESTATUS_1000_XHALF)
408 features |= SUPPORTED_1000baseX_Half;
411 phydev->supported = features;
412 phydev->advertising = features;
414 genphy_config_aneg(phydev);
419 int genphy_startup(struct phy_device *phydev)
421 genphy_update_link(phydev);
422 genphy_parse_link(phydev);
427 int genphy_shutdown(struct phy_device *phydev)
432 static struct phy_driver genphy_driver = {
435 .name = "Generic PHY",
437 .config = genphy_config,
438 .startup = genphy_startup,
439 .shutdown = genphy_shutdown,
442 static LIST_HEAD(phy_drivers);
446 #ifdef CONFIG_PHY_AQUANTIA
449 #ifdef CONFIG_PHY_ATHEROS
452 #ifdef CONFIG_PHY_BROADCOM
455 #ifdef CONFIG_PHY_CORTINA
458 #ifdef CONFIG_PHY_DAVICOM
461 #ifdef CONFIG_PHY_ET1011C
464 #ifdef CONFIG_PHY_LXT
467 #ifdef CONFIG_PHY_MARVELL
470 #ifdef CONFIG_PHY_MICREL
473 #ifdef CONFIG_PHY_NATSEMI
476 #ifdef CONFIG_PHY_REALTEK
479 #ifdef CONFIG_PHY_SMSC
482 #ifdef CONFIG_PHY_TERANETICS
483 phy_teranetics_init();
485 #ifdef CONFIG_PHY_VITESSE
492 int phy_register(struct phy_driver *drv)
494 INIT_LIST_HEAD(&drv->list);
495 list_add_tail(&drv->list, &phy_drivers);
500 static int phy_probe(struct phy_device *phydev)
504 phydev->advertising = phydev->supported = phydev->drv->features;
505 phydev->mmds = phydev->drv->mmds;
507 if (phydev->drv->probe)
508 err = phydev->drv->probe(phydev);
513 static struct phy_driver *generic_for_interface(phy_interface_t interface)
515 #ifdef CONFIG_PHYLIB_10G
516 if (is_10g_interface(interface))
517 return &gen10g_driver;
520 return &genphy_driver;
523 static struct phy_driver *get_phy_driver(struct phy_device *phydev,
524 phy_interface_t interface)
526 struct list_head *entry;
527 int phy_id = phydev->phy_id;
528 struct phy_driver *drv = NULL;
530 list_for_each(entry, &phy_drivers) {
531 drv = list_entry(entry, struct phy_driver, list);
532 if ((drv->uid & drv->mask) == (phy_id & drv->mask))
536 /* If we made it here, there's no driver for this PHY */
537 return generic_for_interface(interface);
540 static struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
542 phy_interface_t interface)
544 struct phy_device *dev;
546 /* We allocate the device, and initialize the
548 dev = malloc(sizeof(*dev));
550 printf("Failed to allocate PHY device for %s:%d\n",
555 memset(dev, 0, sizeof(*dev));
559 dev->interface = interface;
561 dev->autoneg = AUTONEG_ENABLE;
564 dev->phy_id = phy_id;
567 dev->drv = get_phy_driver(dev, interface);
571 bus->phymap[addr] = dev;
577 * get_phy_id - reads the specified addr for its ID.
578 * @bus: the target MII bus
579 * @addr: PHY address on the MII bus
580 * @phy_id: where to store the ID retrieved.
582 * Description: Reads the ID registers of the PHY at @addr on the
583 * @bus, stores it in @phy_id and returns zero on success.
585 int __weak get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
589 /* Grab the bits from PHYIR1, and put them
590 * in the upper half */
591 phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
596 *phy_id = (phy_reg & 0xffff) << 16;
598 /* Grab the bits from PHYIR2, and put them in the lower half */
599 phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
604 *phy_id |= (phy_reg & 0xffff);
609 static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
610 unsigned phy_mask, int devad, phy_interface_t interface)
612 u32 phy_id = 0xffffffff;
614 int addr = ffs(phy_mask) - 1;
615 int r = get_phy_id(bus, addr, devad, &phy_id);
616 /* If the PHY ID is mostly f's, we didn't find anything */
617 if (r == 0 && (phy_id & 0x1fffffff) != 0x1fffffff)
618 return phy_device_create(bus, addr, phy_id, interface);
619 phy_mask &= ~(1 << addr);
624 static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
625 unsigned phy_mask, phy_interface_t interface)
627 /* If we have one, return the existing device, with new interface */
629 int addr = ffs(phy_mask) - 1;
630 if (bus->phymap[addr]) {
631 bus->phymap[addr]->interface = interface;
632 return bus->phymap[addr];
634 phy_mask &= ~(1 << addr);
639 static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
640 unsigned phy_mask, phy_interface_t interface)
643 struct phy_device *phydev;
645 phydev = search_for_existing_phy(bus, phy_mask, interface);
648 /* Try Standard (ie Clause 22) access */
649 /* Otherwise we have to try Clause 45 */
650 for (i = 0; i < 5; i++) {
651 phydev = create_phy_by_mask(bus, phy_mask,
652 i ? i : MDIO_DEVAD_NONE, interface);
658 printf("Phy %d not found\n", ffs(phy_mask) - 1);
659 return phy_device_create(bus, ffs(phy_mask) - 1, 0xffffffff, interface);
663 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
664 * @bus: the target MII bus
665 * @addr: PHY address on the MII bus
667 * Description: Reads the ID registers of the PHY at @addr on the
668 * @bus, then allocates and returns the phy_device to represent it.
670 static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
671 phy_interface_t interface)
673 return get_phy_device_by_mask(bus, 1 << addr, interface);
676 int phy_reset(struct phy_device *phydev)
680 int devad = MDIO_DEVAD_NONE;
682 #ifdef CONFIG_PHYLIB_10G
683 /* If it's 10G, we need to issue reset through one of the MMDs */
684 if (is_10g_interface(phydev->interface)) {
686 gen10g_discover_mmds(phydev);
688 devad = ffs(phydev->mmds) - 1;
692 reg = phy_read(phydev, devad, MII_BMCR);
694 debug("PHY status read failed\n");
700 if (phy_write(phydev, devad, MII_BMCR, reg) < 0) {
701 debug("PHY reset failed\n");
705 #ifdef CONFIG_PHY_RESET_DELAY
706 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
709 * Poll the control register for the reset bit to go to 0 (it is
710 * auto-clearing). This should happen within 0.5 seconds per the
713 while ((reg & BMCR_RESET) && timeout--) {
714 reg = phy_read(phydev, devad, MII_BMCR);
717 debug("PHY status read failed\n");
723 if (reg & BMCR_RESET) {
724 puts("PHY reset timed out\n");
731 int miiphy_reset(const char *devname, unsigned char addr)
733 struct mii_dev *bus = miiphy_get_dev_by_name(devname);
734 struct phy_device *phydev;
737 * miiphy_reset was only used on standard PHYs, so we'll fake it here.
738 * If later code tries to connect with the right interface, this will
739 * be corrected by get_phy_device in phy_connect()
741 phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII);
743 return phy_reset(phydev);
746 struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask,
747 phy_interface_t interface)
753 /* Wait 15ms to make sure the PHY has come out of hard reset */
755 return get_phy_device_by_mask(bus, phy_mask, interface);
759 void phy_connect_dev(struct phy_device *phydev, struct udevice *dev)
761 void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev)
764 /* Soft Reset the PHY */
767 printf("%s:%d is connected to %s. Reconnecting to %s\n",
768 phydev->bus->name, phydev->addr,
769 phydev->dev->name, dev->name);
772 debug("%s connected to %s\n", dev->name, phydev->drv->name);
776 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
777 struct udevice *dev, phy_interface_t interface)
779 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
780 struct eth_device *dev, phy_interface_t interface)
783 struct phy_device *phydev;
785 phydev = phy_find_by_mask(bus, 1 << addr, interface);
787 phy_connect_dev(phydev, dev);
789 printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
794 * Start the PHY. Returns 0 on success, or a negative error code.
796 int phy_startup(struct phy_device *phydev)
798 if (phydev->drv->startup)
799 return phydev->drv->startup(phydev);
804 __weak int board_phy_config(struct phy_device *phydev)
806 if (phydev->drv->config)
807 return phydev->drv->config(phydev);
811 int phy_config(struct phy_device *phydev)
813 /* Invoke an optional board-specific helper */
814 board_phy_config(phydev);
819 int phy_shutdown(struct phy_device *phydev)
821 if (phydev->drv->shutdown)
822 phydev->drv->shutdown(phydev);
827 int phy_get_interface_by_name(const char *str)
831 for (i = 0; i < PHY_INTERFACE_MODE_COUNT; i++) {
832 if (!strcmp(str, phy_interface_strings[i]))