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>
22 /* Generic PHY support and helper functions */
25 * genphy_config_advert - sanitize and advertise auto-negotation parameters
26 * @phydev: target phy_device struct
28 * Description: Writes MII_ADVERTISE with the appropriate values,
29 * after sanitizing the values to make sure we only advertise
30 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
31 * hasn't changed, and > 0 if it has changed.
33 static int genphy_config_advert(struct phy_device *phydev)
39 /* Only allow advertising what
40 * this PHY supports */
41 phydev->advertising &= phydev->supported;
42 advertise = phydev->advertising;
44 /* Setup standard advertisement */
45 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);
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);
143 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
145 /* Don't isolate the PHY if we're negotiating */
146 ctl &= ~(BMCR_ISOLATE);
148 return phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
153 * genphy_config_aneg - restart auto-negotiation or write BMCR
154 * @phydev: target phy_device struct
156 * Description: If auto-negotiation is enabled, we configure the
157 * advertising, and then restart auto-negotiation. If it is not
158 * enabled, then we write the BMCR.
160 int genphy_config_aneg(struct phy_device *phydev)
164 if (AUTONEG_ENABLE != phydev->autoneg)
165 return genphy_setup_forced(phydev);
167 result = genphy_config_advert(phydev);
169 if (result < 0) /* error */
173 /* Advertisment hasn't changed, but maybe aneg was never on to
174 * begin with? Or maybe phy was isolated? */
175 int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
180 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
181 result = 1; /* do restart aneg */
184 /* Only restart aneg if we are advertising something different
185 * than we were before. */
187 result = genphy_restart_aneg(phydev);
193 * genphy_update_link - update link status in @phydev
194 * @phydev: target phy_device struct
196 * Description: Update the value in phydev->link to reflect the
197 * current link value. In order to do this, we need to read
198 * the status register twice, keeping the second value.
200 int genphy_update_link(struct phy_device *phydev)
206 * Wait if the link is up, and autonegotiation is in progress
207 * (ie - we're capable and it's not done)
209 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
214 * If we already saw the link up, and it hasn't gone down, then
215 * we don't need to wait for autoneg again
217 if (phydev->link && mii_reg & BMSR_LSTATUS)
220 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
224 if (!(bmcr & BMCR_ANENABLE))
227 if ((mii_reg & BMSR_ANEGCAPABLE) && !(mii_reg & BMSR_ANEGCOMPLETE)) {
230 printf("%s Waiting for PHY auto negotiation to complete",
232 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
236 if (i > PHY_ANEG_TIMEOUT) {
237 printf(" TIMEOUT !\n");
243 puts("user interrupt!\n");
248 if ((i++ % 500) == 0)
251 udelay(1000); /* 1 ms */
252 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
259 /* Read the link a second time to clear the latched state */
260 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
264 if (mii_reg & BMSR_LSTATUS)
274 * Generic function which updates the speed and duplex. If
275 * autonegotiation is enabled, it uses the AND of the link
276 * partner's advertised capabilities and our advertised
277 * capabilities. If autonegotiation is disabled, we use the
278 * appropriate bits in the control register.
280 * Stolen from Linux's mii.c and phy_device.c
282 int genphy_parse_link(struct phy_device *phydev)
284 int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
289 /* We're using autonegotiation */
290 if (mii_reg & BMSR_ANEGCAPABLE) {
296 /* Check for gigabit capability */
297 if (mii_reg & BMSR_ERCAP) {
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);
306 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
312 /* Set the baseline so we only have to set them
313 * if they're different
315 phydev->speed = SPEED_10;
316 phydev->duplex = DUPLEX_HALF;
318 /* Check the gigabit fields */
319 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
320 phydev->speed = SPEED_1000;
322 if (gblpa & PHY_1000BTSR_1000FD)
323 phydev->duplex = DUPLEX_FULL;
329 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
334 ret = phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
339 if (lpa & (LPA_100FULL | LPA_100HALF)) {
340 phydev->speed = SPEED_100;
342 if (lpa & LPA_100FULL)
343 phydev->duplex = DUPLEX_FULL;
345 } else if (lpa & LPA_10FULL)
346 phydev->duplex = DUPLEX_FULL;
349 * Extended status may indicate that the PHY supports
350 * 1000BASE-T/X even though the 1000BASE-T registers
351 * are missing. In this case we can't tell whether the
352 * peer also supports it, so we only check extended
353 * status if the 1000BASE-T registers are actually
356 if ((mii_reg & BMSR_ESTATEN) && !(mii_reg & BMSR_ERCAP))
357 estatus = phy_read(phydev, MDIO_DEVAD_NONE,
362 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF |
363 ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) {
364 phydev->speed = SPEED_1000;
365 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL))
366 phydev->duplex = DUPLEX_FULL;
370 int bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
375 phydev->speed = SPEED_10;
376 phydev->duplex = DUPLEX_HALF;
378 if (bmcr & BMCR_FULLDPLX)
379 phydev->duplex = DUPLEX_FULL;
381 if (bmcr & BMCR_SPEED1000)
382 phydev->speed = SPEED_1000;
383 else if (bmcr & BMCR_SPEED100)
384 phydev->speed = SPEED_100;
390 int genphy_config(struct phy_device *phydev)
395 /* For now, I'll claim that the generic driver supports
396 * all possible port types */
397 features = (SUPPORTED_TP | SUPPORTED_MII
398 | SUPPORTED_AUI | SUPPORTED_FIBRE |
401 /* Do we support autonegotiation? */
402 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_DAVICOM
479 #ifdef CONFIG_PHY_ET1011C
482 #ifdef CONFIG_PHY_ICPLUS
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 struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
563 phy_interface_t interface)
565 struct phy_device *dev;
567 /* We allocate the device, and initialize the
569 dev = malloc(sizeof(*dev));
571 printf("Failed to allocate PHY device for %s:%d\n",
576 memset(dev, 0, sizeof(*dev));
580 dev->interface = interface;
582 dev->autoneg = AUTONEG_ENABLE;
585 dev->phy_id = phy_id;
588 dev->drv = get_phy_driver(dev, interface);
592 bus->phymap[addr] = dev;
598 * get_phy_id - reads the specified addr for its ID.
599 * @bus: the target MII bus
600 * @addr: PHY address on the MII bus
601 * @phy_id: where to store the ID retrieved.
603 * Description: Reads the ID registers of the PHY at @addr on the
604 * @bus, stores it in @phy_id and returns zero on success.
606 static int get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
610 /* Grab the bits from PHYIR1, and put them
611 * in the upper half */
612 phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
617 *phy_id = (phy_reg & 0xffff) << 16;
619 /* Grab the bits from PHYIR2, and put them in the lower half */
620 phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
625 *phy_id |= (phy_reg & 0xffff);
630 static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
631 unsigned phy_mask, int devad, phy_interface_t interface)
633 u32 phy_id = 0xffffffff;
635 int addr = ffs(phy_mask) - 1;
636 int r = get_phy_id(bus, addr, devad, &phy_id);
639 /* If the PHY ID is mostly f's, we didn't find anything */
640 if ((phy_id & 0x1fffffff) != 0x1fffffff)
641 return phy_device_create(bus, addr, phy_id, interface);
642 phy_mask &= ~(1 << addr);
647 static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
648 unsigned phy_mask, phy_interface_t interface)
650 /* If we have one, return the existing device, with new interface */
652 int addr = ffs(phy_mask) - 1;
653 if (bus->phymap[addr]) {
654 bus->phymap[addr]->interface = interface;
655 return bus->phymap[addr];
657 phy_mask &= ~(1 << addr);
662 static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
663 unsigned phy_mask, phy_interface_t interface)
666 struct phy_device *phydev;
668 phydev = search_for_existing_phy(bus, phy_mask, interface);
671 /* Try Standard (ie Clause 22) access */
672 /* Otherwise we have to try Clause 45 */
673 for (i = 0; i < 5; i++) {
674 phydev = create_phy_by_mask(bus, phy_mask,
675 i ? i : MDIO_DEVAD_NONE, interface);
681 printf("Phy not found\n");
682 return phy_device_create(bus, ffs(phy_mask) - 1, 0xffffffff, interface);
686 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
687 * @bus: the target MII bus
688 * @addr: PHY address on the MII bus
690 * Description: Reads the ID registers of the PHY at @addr on the
691 * @bus, then allocates and returns the phy_device to represent it.
693 static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
694 phy_interface_t interface)
696 return get_phy_device_by_mask(bus, 1 << addr, interface);
699 int phy_reset(struct phy_device *phydev)
704 int devad = MDIO_DEVAD_NONE;
706 #ifdef CONFIG_PHYLIB_10G
707 /* If it's 10G, we need to issue reset through one of the MMDs */
708 if (is_10g_interface(phydev->interface)) {
710 gen10g_discover_mmds(phydev);
712 devad = ffs(phydev->mmds) - 1;
716 reg = phy_read(phydev, devad, MII_BMCR);
718 debug("PHY status read failed\n");
724 err = phy_write(phydev, devad, MII_BMCR, reg);
726 debug("PHY reset failed\n");
730 #ifdef CONFIG_PHY_RESET_DELAY
731 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
734 * Poll the control register for the reset bit to go to 0 (it is
735 * auto-clearing). This should happen within 0.5 seconds per the
738 while ((reg & BMCR_RESET) && timeout-- >= 0) {
739 reg = phy_read(phydev, devad, MII_BMCR);
742 debug("PHY status read failed\n");
748 if (reg & BMCR_RESET) {
749 puts("PHY reset timed out\n");
756 int miiphy_reset(const char *devname, unsigned char addr)
758 struct mii_dev *bus = miiphy_get_dev_by_name(devname);
759 struct phy_device *phydev;
762 * miiphy_reset was only used on standard PHYs, so we'll fake it here.
763 * If later code tries to connect with the right interface, this will
764 * be corrected by get_phy_device in phy_connect()
766 phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII);
768 return phy_reset(phydev);
771 struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask,
772 phy_interface_t interface)
778 /* Wait 15ms to make sure the PHY has come out of hard reset */
780 return get_phy_device_by_mask(bus, phy_mask, interface);
783 void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev)
785 /* Soft Reset the PHY */
788 printf("%s:%d is connected to %s. Reconnecting to %s\n",
789 phydev->bus->name, phydev->addr,
790 phydev->dev->name, dev->name);
793 debug("%s connected to %s\n", dev->name, phydev->drv->name);
796 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
797 struct eth_device *dev, phy_interface_t interface)
799 struct phy_device *phydev;
801 phydev = phy_find_by_mask(bus, 1 << addr, interface);
803 phy_connect_dev(phydev, dev);
805 printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
810 * Start the PHY. Returns 0 on success, or a negative error code.
812 int phy_startup(struct phy_device *phydev)
814 if (phydev->drv->startup)
815 return phydev->drv->startup(phydev);
820 static int __board_phy_config(struct phy_device *phydev)
822 if (phydev->drv->config)
823 return phydev->drv->config(phydev);
827 int board_phy_config(struct phy_device *phydev)
828 __attribute__((weak, alias("__board_phy_config")));
830 int phy_config(struct phy_device *phydev)
832 /* Invoke an optional board-specific helper */
833 board_phy_config(phydev);
838 int phy_shutdown(struct phy_device *phydev)
840 if (phydev->drv->shutdown)
841 phydev->drv->shutdown(phydev);