1 // SPDX-License-Identifier: GPL-2.0+
3 * Generic PHY Management code
5 * Copyright 2011 Freescale Semiconductor, Inc.
8 * Based loosely off of Linux's PHY Lib
21 #include <linux/err.h>
22 #include <linux/compiler.h>
24 DECLARE_GLOBAL_DATA_PTR;
26 /* Generic PHY support and helper functions */
29 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
30 * @phydev: target phy_device struct
32 * Description: Writes MII_ADVERTISE with the appropriate values,
33 * after sanitizing the values to make sure we only advertise
34 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
35 * hasn't changed, and > 0 if it has changed.
37 static int genphy_config_advert(struct phy_device *phydev)
40 int oldadv, adv, bmsr;
43 /* Only allow advertising what this PHY supports */
44 phydev->advertising &= phydev->supported;
45 advertise = phydev->advertising;
47 /* Setup standard advertisement */
48 adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
54 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
55 ADVERTISE_PAUSE_ASYM);
56 if (advertise & ADVERTISED_10baseT_Half)
57 adv |= ADVERTISE_10HALF;
58 if (advertise & ADVERTISED_10baseT_Full)
59 adv |= ADVERTISE_10FULL;
60 if (advertise & ADVERTISED_100baseT_Half)
61 adv |= ADVERTISE_100HALF;
62 if (advertise & ADVERTISED_100baseT_Full)
63 adv |= ADVERTISE_100FULL;
64 if (advertise & ADVERTISED_Pause)
65 adv |= ADVERTISE_PAUSE_CAP;
66 if (advertise & ADVERTISED_Asym_Pause)
67 adv |= ADVERTISE_PAUSE_ASYM;
68 if (advertise & ADVERTISED_1000baseX_Half)
69 adv |= ADVERTISE_1000XHALF;
70 if (advertise & ADVERTISED_1000baseX_Full)
71 adv |= ADVERTISE_1000XFULL;
74 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv);
81 bmsr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
85 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
86 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
89 if (!(bmsr & BMSR_ESTATEN))
92 /* Configure gigabit if it's supported */
93 adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
99 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
101 if (phydev->supported & (SUPPORTED_1000baseT_Half |
102 SUPPORTED_1000baseT_Full)) {
103 if (advertise & SUPPORTED_1000baseT_Half)
104 adv |= ADVERTISE_1000HALF;
105 if (advertise & SUPPORTED_1000baseT_Full)
106 adv |= ADVERTISE_1000FULL;
112 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, adv);
120 * genphy_setup_forced - configures/forces speed/duplex from @phydev
121 * @phydev: target phy_device struct
123 * Description: Configures MII_BMCR to force speed/duplex
124 * to the values in phydev. Assumes that the values are valid.
126 static int genphy_setup_forced(struct phy_device *phydev)
129 int ctl = BMCR_ANRESTART;
132 phydev->asym_pause = 0;
134 if (phydev->speed == SPEED_1000)
135 ctl |= BMCR_SPEED1000;
136 else if (phydev->speed == SPEED_100)
137 ctl |= BMCR_SPEED100;
139 if (phydev->duplex == DUPLEX_FULL)
140 ctl |= BMCR_FULLDPLX;
142 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
148 * genphy_restart_aneg - Enable and Restart Autonegotiation
149 * @phydev: target phy_device struct
151 int genphy_restart_aneg(struct phy_device *phydev)
155 ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
160 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
162 /* Don't isolate the PHY if we're negotiating */
163 ctl &= ~(BMCR_ISOLATE);
165 ctl = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
171 * genphy_config_aneg - restart auto-negotiation or write BMCR
172 * @phydev: target phy_device struct
174 * Description: If auto-negotiation is enabled, we configure the
175 * advertising, and then restart auto-negotiation. If it is not
176 * enabled, then we write the BMCR.
178 int genphy_config_aneg(struct phy_device *phydev)
182 if (phydev->autoneg != AUTONEG_ENABLE)
183 return genphy_setup_forced(phydev);
185 result = genphy_config_advert(phydev);
187 if (result < 0) /* error */
192 * Advertisment hasn't changed, but maybe aneg was never on to
193 * begin with? Or maybe phy was isolated?
195 int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
200 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
201 result = 1; /* do restart aneg */
205 * Only restart aneg if we are advertising something different
206 * than we were before.
209 result = genphy_restart_aneg(phydev);
215 * genphy_update_link - update link status in @phydev
216 * @phydev: target phy_device struct
218 * Description: Update the value in phydev->link to reflect the
219 * current link value. In order to do this, we need to read
220 * the status register twice, keeping the second value.
222 int genphy_update_link(struct phy_device *phydev)
224 unsigned int mii_reg;
227 * Wait if the link is up, and autonegotiation is in progress
228 * (ie - we're capable and it's not done)
230 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
233 * If we already saw the link up, and it hasn't gone down, then
234 * we don't need to wait for autoneg again
236 if (phydev->link && mii_reg & BMSR_LSTATUS)
239 if ((phydev->autoneg == AUTONEG_ENABLE) &&
240 !(mii_reg & BMSR_ANEGCOMPLETE)) {
243 printf("%s Waiting for PHY auto negotiation to complete",
245 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
249 if (i > PHY_ANEG_TIMEOUT) {
250 printf(" TIMEOUT !\n");
256 puts("user interrupt!\n");
261 if ((i++ % 500) == 0)
264 udelay(1000); /* 1 ms */
265 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
270 /* Read the link a second time to clear the latched state */
271 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
273 if (mii_reg & BMSR_LSTATUS)
283 * Generic function which updates the speed and duplex. If
284 * autonegotiation is enabled, it uses the AND of the link
285 * partner's advertised capabilities and our advertised
286 * capabilities. If autonegotiation is disabled, we use the
287 * appropriate bits in the control register.
289 * Stolen from Linux's mii.c and phy_device.c
291 int genphy_parse_link(struct phy_device *phydev)
293 int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
295 /* We're using autonegotiation */
296 if (phydev->autoneg == AUTONEG_ENABLE) {
301 /* Check for gigabit capability */
302 if (phydev->supported & (SUPPORTED_1000baseT_Full |
303 SUPPORTED_1000baseT_Half)) {
304 /* We want a list of states supported by
305 * both PHYs in the link
307 gblpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
309 debug("Could not read MII_STAT1000. ");
310 debug("Ignoring gigabit capability\n");
313 gblpa &= phy_read(phydev,
314 MDIO_DEVAD_NONE, MII_CTRL1000) << 2;
317 /* Set the baseline so we only have to set them
318 * if they're different
320 phydev->speed = SPEED_10;
321 phydev->duplex = DUPLEX_HALF;
323 /* Check the gigabit fields */
324 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
325 phydev->speed = SPEED_1000;
327 if (gblpa & PHY_1000BTSR_1000FD)
328 phydev->duplex = DUPLEX_FULL;
334 lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
335 lpa &= phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
337 if (lpa & (LPA_100FULL | LPA_100HALF)) {
338 phydev->speed = SPEED_100;
340 if (lpa & LPA_100FULL)
341 phydev->duplex = DUPLEX_FULL;
343 } else if (lpa & LPA_10FULL) {
344 phydev->duplex = DUPLEX_FULL;
348 * Extended status may indicate that the PHY supports
349 * 1000BASE-T/X even though the 1000BASE-T registers
350 * are missing. In this case we can't tell whether the
351 * peer also supports it, so we only check extended
352 * status if the 1000BASE-T registers are actually
355 if ((mii_reg & BMSR_ESTATEN) && !(mii_reg & BMSR_ERCAP))
356 estatus = phy_read(phydev, MDIO_DEVAD_NONE,
359 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF |
360 ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) {
361 phydev->speed = SPEED_1000;
362 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL))
363 phydev->duplex = DUPLEX_FULL;
367 u32 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
369 phydev->speed = SPEED_10;
370 phydev->duplex = DUPLEX_HALF;
372 if (bmcr & BMCR_FULLDPLX)
373 phydev->duplex = DUPLEX_FULL;
375 if (bmcr & BMCR_SPEED1000)
376 phydev->speed = SPEED_1000;
377 else if (bmcr & BMCR_SPEED100)
378 phydev->speed = SPEED_100;
384 int genphy_config(struct phy_device *phydev)
389 features = (SUPPORTED_TP | SUPPORTED_MII
390 | SUPPORTED_AUI | SUPPORTED_FIBRE |
393 /* Do we support autonegotiation? */
394 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
399 if (val & BMSR_ANEGCAPABLE)
400 features |= SUPPORTED_Autoneg;
402 if (val & BMSR_100FULL)
403 features |= SUPPORTED_100baseT_Full;
404 if (val & BMSR_100HALF)
405 features |= SUPPORTED_100baseT_Half;
406 if (val & BMSR_10FULL)
407 features |= SUPPORTED_10baseT_Full;
408 if (val & BMSR_10HALF)
409 features |= SUPPORTED_10baseT_Half;
411 if (val & BMSR_ESTATEN) {
412 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS);
417 if (val & ESTATUS_1000_TFULL)
418 features |= SUPPORTED_1000baseT_Full;
419 if (val & ESTATUS_1000_THALF)
420 features |= SUPPORTED_1000baseT_Half;
421 if (val & ESTATUS_1000_XFULL)
422 features |= SUPPORTED_1000baseX_Full;
423 if (val & ESTATUS_1000_XHALF)
424 features |= SUPPORTED_1000baseX_Half;
427 phydev->supported &= features;
428 phydev->advertising &= features;
430 genphy_config_aneg(phydev);
435 int genphy_startup(struct phy_device *phydev)
439 ret = genphy_update_link(phydev);
443 return genphy_parse_link(phydev);
446 int genphy_shutdown(struct phy_device *phydev)
451 static struct phy_driver genphy_driver = {
454 .name = "Generic PHY",
455 .features = PHY_GBIT_FEATURES | SUPPORTED_MII |
456 SUPPORTED_AUI | SUPPORTED_FIBRE |
458 .config = genphy_config,
459 .startup = genphy_startup,
460 .shutdown = genphy_shutdown,
463 static LIST_HEAD(phy_drivers);
467 #ifdef CONFIG_B53_SWITCH
470 #ifdef CONFIG_MV88E61XX_SWITCH
471 phy_mv88e61xx_init();
473 #ifdef CONFIG_PHY_AQUANTIA
476 #ifdef CONFIG_PHY_ATHEROS
479 #ifdef CONFIG_PHY_BROADCOM
482 #ifdef CONFIG_PHY_CORTINA
485 #ifdef CONFIG_PHY_DAVICOM
488 #ifdef CONFIG_PHY_ET1011C
491 #ifdef CONFIG_PHY_LXT
494 #ifdef CONFIG_PHY_MARVELL
497 #ifdef CONFIG_PHY_MICREL_KSZ8XXX
498 phy_micrel_ksz8xxx_init();
500 #ifdef CONFIG_PHY_MICREL_KSZ90X1
501 phy_micrel_ksz90x1_init();
503 #ifdef CONFIG_PHY_MESON_GXL
504 phy_meson_gxl_init();
506 #ifdef CONFIG_PHY_NATSEMI
509 #ifdef CONFIG_PHY_REALTEK
512 #ifdef CONFIG_PHY_SMSC
515 #ifdef CONFIG_PHY_TERANETICS
516 phy_teranetics_init();
521 #ifdef CONFIG_PHY_VITESSE
524 #ifdef CONFIG_PHY_XILINX
527 #ifdef CONFIG_PHY_MSCC
530 #ifdef CONFIG_PHY_FIXED
536 int phy_register(struct phy_driver *drv)
538 INIT_LIST_HEAD(&drv->list);
539 list_add_tail(&drv->list, &phy_drivers);
541 #ifdef CONFIG_NEEDS_MANUAL_RELOC
543 drv->probe += gd->reloc_off;
545 drv->config += gd->reloc_off;
547 drv->startup += gd->reloc_off;
549 drv->shutdown += gd->reloc_off;
551 drv->readext += gd->reloc_off;
553 drv->writeext += gd->reloc_off;
558 int phy_set_supported(struct phy_device *phydev, u32 max_speed)
560 /* The default values for phydev->supported are provided by the PHY
561 * driver "features" member, we want to reset to sane defaults first
562 * before supporting higher speeds.
564 phydev->supported &= PHY_DEFAULT_FEATURES;
570 phydev->supported |= PHY_1000BT_FEATURES;
573 phydev->supported |= PHY_100BT_FEATURES;
576 phydev->supported |= PHY_10BT_FEATURES;
582 static int phy_probe(struct phy_device *phydev)
586 phydev->advertising = phydev->drv->features;
587 phydev->supported = phydev->drv->features;
589 phydev->mmds = phydev->drv->mmds;
591 if (phydev->drv->probe)
592 err = phydev->drv->probe(phydev);
597 static struct phy_driver *generic_for_interface(phy_interface_t interface)
599 #ifdef CONFIG_PHYLIB_10G
600 if (is_10g_interface(interface))
601 return &gen10g_driver;
604 return &genphy_driver;
607 static struct phy_driver *get_phy_driver(struct phy_device *phydev,
608 phy_interface_t interface)
610 struct list_head *entry;
611 int phy_id = phydev->phy_id;
612 struct phy_driver *drv = NULL;
614 list_for_each(entry, &phy_drivers) {
615 drv = list_entry(entry, struct phy_driver, list);
616 if ((drv->uid & drv->mask) == (phy_id & drv->mask))
620 /* If we made it here, there's no driver for this PHY */
621 return generic_for_interface(interface);
624 static struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
626 phy_interface_t interface)
628 struct phy_device *dev;
631 * We allocate the device, and initialize the
634 dev = malloc(sizeof(*dev));
636 printf("Failed to allocate PHY device for %s:%d\n",
641 memset(dev, 0, sizeof(*dev));
645 dev->interface = interface;
647 dev->autoneg = AUTONEG_ENABLE;
650 dev->phy_id = phy_id;
653 dev->drv = get_phy_driver(dev, interface);
657 bus->phymap[addr] = dev;
663 * get_phy_id - reads the specified addr for its ID.
664 * @bus: the target MII bus
665 * @addr: PHY address on the MII bus
666 * @phy_id: where to store the ID retrieved.
668 * Description: Reads the ID registers of the PHY at @addr on the
669 * @bus, stores it in @phy_id and returns zero on success.
671 int __weak get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
676 * Grab the bits from PHYIR1, and put them
679 phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
684 *phy_id = (phy_reg & 0xffff) << 16;
686 /* Grab the bits from PHYIR2, and put them in the lower half */
687 phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
692 *phy_id |= (phy_reg & 0xffff);
697 static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
698 uint phy_mask, int devad,
699 phy_interface_t interface)
701 u32 phy_id = 0xffffffff;
704 int addr = ffs(phy_mask) - 1;
705 int r = get_phy_id(bus, addr, devad, &phy_id);
706 /* If the PHY ID is mostly f's, we didn't find anything */
707 if (r == 0 && (phy_id & 0x1fffffff) != 0x1fffffff)
708 return phy_device_create(bus, addr, phy_id, interface);
709 phy_mask &= ~(1 << addr);
714 static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
716 phy_interface_t interface)
718 /* If we have one, return the existing device, with new interface */
720 int addr = ffs(phy_mask) - 1;
722 if (bus->phymap[addr]) {
723 bus->phymap[addr]->interface = interface;
724 return bus->phymap[addr];
726 phy_mask &= ~(1 << addr);
731 static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
733 phy_interface_t interface)
736 struct phy_device *phydev;
738 phydev = search_for_existing_phy(bus, phy_mask, interface);
741 /* Try Standard (ie Clause 22) access */
742 /* Otherwise we have to try Clause 45 */
743 for (i = 0; i < 5; i++) {
744 phydev = create_phy_by_mask(bus, phy_mask,
745 i ? i : MDIO_DEVAD_NONE, interface);
752 debug("\n%s PHY: ", bus->name);
754 int addr = ffs(phy_mask) - 1;
757 phy_mask &= ~(1 << addr);
759 debug("not found\n");
765 * get_phy_device - reads the specified PHY device and returns its
767 * @bus: the target MII bus
768 * @addr: PHY address on the MII bus
770 * Description: Reads the ID registers of the PHY at @addr on the
771 * @bus, then allocates and returns the phy_device to represent it.
773 static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
774 phy_interface_t interface)
776 return get_phy_device_by_mask(bus, 1 << addr, interface);
779 int phy_reset(struct phy_device *phydev)
783 int devad = MDIO_DEVAD_NONE;
785 if (phydev->flags & PHY_FLAG_BROKEN_RESET)
788 #ifdef CONFIG_PHYLIB_10G
789 /* If it's 10G, we need to issue reset through one of the MMDs */
790 if (is_10g_interface(phydev->interface)) {
792 gen10g_discover_mmds(phydev);
794 devad = ffs(phydev->mmds) - 1;
798 if (phy_write(phydev, devad, MII_BMCR, BMCR_RESET) < 0) {
799 debug("PHY reset failed\n");
803 #ifdef CONFIG_PHY_RESET_DELAY
804 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
807 * Poll the control register for the reset bit to go to 0 (it is
808 * auto-clearing). This should happen within 0.5 seconds per the
811 reg = phy_read(phydev, devad, MII_BMCR);
812 while ((reg & BMCR_RESET) && timeout--) {
813 reg = phy_read(phydev, devad, MII_BMCR);
816 debug("PHY status read failed\n");
822 if (reg & BMCR_RESET) {
823 puts("PHY reset timed out\n");
830 int miiphy_reset(const char *devname, unsigned char addr)
832 struct mii_dev *bus = miiphy_get_dev_by_name(devname);
833 struct phy_device *phydev;
836 * miiphy_reset was only used on standard PHYs, so we'll fake it here.
837 * If later code tries to connect with the right interface, this will
838 * be corrected by get_phy_device in phy_connect()
840 phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII);
842 return phy_reset(phydev);
845 struct phy_device *phy_find_by_mask(struct mii_dev *bus, uint phy_mask,
846 phy_interface_t interface)
852 /* Wait 15ms to make sure the PHY has come out of hard reset */
856 return get_phy_device_by_mask(bus, phy_mask, interface);
860 void phy_connect_dev(struct phy_device *phydev, struct udevice *dev)
862 void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev)
865 /* Soft Reset the PHY */
867 if (phydev->dev && phydev->dev != dev) {
868 printf("%s:%d is connected to %s. Reconnecting to %s\n",
869 phydev->bus->name, phydev->addr,
870 phydev->dev->name, dev->name);
873 debug("%s connected to %s\n", dev->name, phydev->drv->name);
877 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
879 phy_interface_t interface)
881 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
882 struct eth_device *dev,
883 phy_interface_t interface)
886 struct phy_device *phydev = NULL;
887 #ifdef CONFIG_PHY_FIXED
891 sn = fdt_first_subnode(gd->fdt_blob, dev_of_offset(dev));
893 name = fdt_get_name(gd->fdt_blob, sn, NULL);
894 if (name && strcmp(name, "fixed-link") == 0) {
895 phydev = phy_device_create(bus,
896 sn, PHY_FIXED_ID, interface);
899 sn = fdt_next_subnode(gd->fdt_blob, sn);
903 phydev = phy_find_by_mask(bus, 1 << addr, interface);
906 phy_connect_dev(phydev, dev);
908 printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
913 * Start the PHY. Returns 0 on success, or a negative error code.
915 int phy_startup(struct phy_device *phydev)
917 if (phydev->drv->startup)
918 return phydev->drv->startup(phydev);
923 __weak int board_phy_config(struct phy_device *phydev)
925 if (phydev->drv->config)
926 return phydev->drv->config(phydev);
930 int phy_config(struct phy_device *phydev)
932 /* Invoke an optional board-specific helper */
933 return board_phy_config(phydev);
936 int phy_shutdown(struct phy_device *phydev)
938 if (phydev->drv->shutdown)
939 phydev->drv->shutdown(phydev);
944 int phy_get_interface_by_name(const char *str)
948 for (i = 0; i < PHY_INTERFACE_MODE_COUNT; i++) {
949 if (!strcmp(str, phy_interface_strings[i]))