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);
51 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
52 ADVERTISE_PAUSE_ASYM);
53 if (advertise & ADVERTISED_10baseT_Half)
54 adv |= ADVERTISE_10HALF;
55 if (advertise & ADVERTISED_10baseT_Full)
56 adv |= ADVERTISE_10FULL;
57 if (advertise & ADVERTISED_100baseT_Half)
58 adv |= ADVERTISE_100HALF;
59 if (advertise & ADVERTISED_100baseT_Full)
60 adv |= ADVERTISE_100FULL;
61 if (advertise & ADVERTISED_Pause)
62 adv |= ADVERTISE_PAUSE_CAP;
63 if (advertise & ADVERTISED_Asym_Pause)
64 adv |= ADVERTISE_PAUSE_ASYM;
65 if (advertise & ADVERTISED_1000baseX_Half)
66 adv |= ADVERTISE_1000XHALF;
67 if (advertise & ADVERTISED_1000baseX_Full)
68 adv |= ADVERTISE_1000XFULL;
71 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv);
78 /* Configure gigabit if it's supported */
79 if (phydev->supported & (SUPPORTED_1000baseT_Half |
80 SUPPORTED_1000baseT_Full)) {
81 oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
86 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
87 if (advertise & SUPPORTED_1000baseT_Half)
88 adv |= ADVERTISE_1000HALF;
89 if (advertise & SUPPORTED_1000baseT_Full)
90 adv |= ADVERTISE_1000FULL;
93 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000,
107 * genphy_setup_forced - configures/forces speed/duplex from @phydev
108 * @phydev: target phy_device struct
110 * Description: Configures MII_BMCR to force speed/duplex
111 * to the values in phydev. Assumes that the values are valid.
113 static int genphy_setup_forced(struct phy_device *phydev)
118 phydev->pause = phydev->asym_pause = 0;
120 if (SPEED_1000 == phydev->speed)
121 ctl |= BMCR_SPEED1000;
122 else if (SPEED_100 == phydev->speed)
123 ctl |= BMCR_SPEED100;
125 if (DUPLEX_FULL == phydev->duplex)
126 ctl |= BMCR_FULLDPLX;
128 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
135 * genphy_restart_aneg - Enable and Restart Autonegotiation
136 * @phydev: target phy_device struct
138 int genphy_restart_aneg(struct phy_device *phydev)
142 ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
147 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
149 /* Don't isolate the PHY if we're negotiating */
150 ctl &= ~(BMCR_ISOLATE);
152 ctl = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
159 * genphy_config_aneg - restart auto-negotiation or write BMCR
160 * @phydev: target phy_device struct
162 * Description: If auto-negotiation is enabled, we configure the
163 * advertising, and then restart auto-negotiation. If it is not
164 * enabled, then we write the BMCR.
166 int genphy_config_aneg(struct phy_device *phydev)
170 if (AUTONEG_ENABLE != phydev->autoneg)
171 return genphy_setup_forced(phydev);
173 result = genphy_config_advert(phydev);
175 if (result < 0) /* error */
179 /* Advertisment hasn't changed, but maybe aneg was never on to
180 * begin with? Or maybe phy was isolated? */
181 int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
186 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
187 result = 1; /* do restart aneg */
190 /* Only restart aneg if we are advertising something different
191 * than we were before. */
193 result = genphy_restart_aneg(phydev);
199 * genphy_update_link - update link status in @phydev
200 * @phydev: target phy_device struct
202 * Description: Update the value in phydev->link to reflect the
203 * current link value. In order to do this, we need to read
204 * the status register twice, keeping the second value.
206 int genphy_update_link(struct phy_device *phydev)
208 unsigned int mii_reg;
211 * Wait if the link is up, and autonegotiation is in progress
212 * (ie - we're capable and it's not done)
214 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
217 * If we already saw the link up, and it hasn't gone down, then
218 * we don't need to wait for autoneg again
220 if (phydev->link && mii_reg & BMSR_LSTATUS)
223 if ((mii_reg & BMSR_ANEGCAPABLE) && !(mii_reg & BMSR_ANEGCOMPLETE)) {
226 printf("%s Waiting for PHY auto negotiation to complete",
228 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
232 if (i > PHY_ANEG_TIMEOUT) {
233 printf(" TIMEOUT !\n");
239 puts("user interrupt!\n");
244 if ((i++ % 500) == 0)
247 udelay(1000); /* 1 ms */
248 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
253 /* Read the link a second time to clear the latched state */
254 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
256 if (mii_reg & BMSR_LSTATUS)
266 * Generic function which updates the speed and duplex. If
267 * autonegotiation is enabled, it uses the AND of the link
268 * partner's advertised capabilities and our advertised
269 * capabilities. If autonegotiation is disabled, we use the
270 * appropriate bits in the control register.
272 * Stolen from Linux's mii.c and phy_device.c
274 int genphy_parse_link(struct phy_device *phydev)
276 int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
278 /* We're using autonegotiation */
279 if (phydev->supported & SUPPORTED_Autoneg) {
284 /* Check for gigabit capability */
285 if (phydev->supported & (SUPPORTED_1000baseT_Full |
286 SUPPORTED_1000baseT_Half)) {
287 /* We want a list of states supported by
288 * both PHYs in the link
290 gblpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
292 debug("Could not read MII_STAT1000. Ignoring gigabit capability\n");
295 gblpa &= phy_read(phydev,
296 MDIO_DEVAD_NONE, MII_CTRL1000) << 2;
299 /* Set the baseline so we only have to set them
300 * if they're different
302 phydev->speed = SPEED_10;
303 phydev->duplex = DUPLEX_HALF;
305 /* Check the gigabit fields */
306 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
307 phydev->speed = SPEED_1000;
309 if (gblpa & PHY_1000BTSR_1000FD)
310 phydev->duplex = DUPLEX_FULL;
316 lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
317 lpa &= phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
319 if (lpa & (LPA_100FULL | LPA_100HALF)) {
320 phydev->speed = SPEED_100;
322 if (lpa & LPA_100FULL)
323 phydev->duplex = DUPLEX_FULL;
325 } else if (lpa & LPA_10FULL)
326 phydev->duplex = DUPLEX_FULL;
329 * Extended status may indicate that the PHY supports
330 * 1000BASE-T/X even though the 1000BASE-T registers
331 * are missing. In this case we can't tell whether the
332 * peer also supports it, so we only check extended
333 * status if the 1000BASE-T registers are actually
336 if ((mii_reg & BMSR_ESTATEN) && !(mii_reg & BMSR_ERCAP))
337 estatus = phy_read(phydev, MDIO_DEVAD_NONE,
340 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF |
341 ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) {
342 phydev->speed = SPEED_1000;
343 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL))
344 phydev->duplex = DUPLEX_FULL;
348 u32 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
350 phydev->speed = SPEED_10;
351 phydev->duplex = DUPLEX_HALF;
353 if (bmcr & BMCR_FULLDPLX)
354 phydev->duplex = DUPLEX_FULL;
356 if (bmcr & BMCR_SPEED1000)
357 phydev->speed = SPEED_1000;
358 else if (bmcr & BMCR_SPEED100)
359 phydev->speed = SPEED_100;
365 int genphy_config(struct phy_device *phydev)
370 /* For now, I'll claim that the generic driver supports
371 * all possible port types */
372 features = (SUPPORTED_TP | SUPPORTED_MII
373 | SUPPORTED_AUI | SUPPORTED_FIBRE |
376 /* Do we support autonegotiation? */
377 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
382 if (val & BMSR_ANEGCAPABLE)
383 features |= SUPPORTED_Autoneg;
385 if (val & BMSR_100FULL)
386 features |= SUPPORTED_100baseT_Full;
387 if (val & BMSR_100HALF)
388 features |= SUPPORTED_100baseT_Half;
389 if (val & BMSR_10FULL)
390 features |= SUPPORTED_10baseT_Full;
391 if (val & BMSR_10HALF)
392 features |= SUPPORTED_10baseT_Half;
394 if (val & BMSR_ESTATEN) {
395 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS);
400 if (val & ESTATUS_1000_TFULL)
401 features |= SUPPORTED_1000baseT_Full;
402 if (val & ESTATUS_1000_THALF)
403 features |= SUPPORTED_1000baseT_Half;
404 if (val & ESTATUS_1000_XFULL)
405 features |= SUPPORTED_1000baseX_Full;
406 if (val & ESTATUS_1000_XHALF)
407 features |= SUPPORTED_1000baseX_Half;
410 phydev->supported = features;
411 phydev->advertising = features;
413 genphy_config_aneg(phydev);
418 int genphy_startup(struct phy_device *phydev)
420 genphy_update_link(phydev);
421 genphy_parse_link(phydev);
426 int genphy_shutdown(struct phy_device *phydev)
431 static struct phy_driver genphy_driver = {
434 .name = "Generic PHY",
436 .config = genphy_config,
437 .startup = genphy_startup,
438 .shutdown = genphy_shutdown,
441 static LIST_HEAD(phy_drivers);
445 #ifdef CONFIG_PHY_ATHEROS
448 #ifdef CONFIG_PHY_BROADCOM
451 #ifdef CONFIG_PHY_DAVICOM
454 #ifdef CONFIG_PHY_ET1011C
457 #ifdef CONFIG_PHY_ICPLUS
460 #ifdef CONFIG_PHY_LXT
463 #ifdef CONFIG_PHY_MARVELL
466 #ifdef CONFIG_PHY_MICREL
469 #ifdef CONFIG_PHY_NATSEMI
472 #ifdef CONFIG_PHY_REALTEK
475 #ifdef CONFIG_PHY_SMSC
478 #ifdef CONFIG_PHY_TERANETICS
479 phy_teranetics_init();
481 #ifdef CONFIG_PHY_VITESSE
488 int phy_register(struct phy_driver *drv)
490 INIT_LIST_HEAD(&drv->list);
491 list_add_tail(&drv->list, &phy_drivers);
496 static int phy_probe(struct phy_device *phydev)
500 phydev->advertising = phydev->supported = phydev->drv->features;
501 phydev->mmds = phydev->drv->mmds;
503 if (phydev->drv->probe)
504 err = phydev->drv->probe(phydev);
509 static struct phy_driver *generic_for_interface(phy_interface_t interface)
511 #ifdef CONFIG_PHYLIB_10G
512 if (is_10g_interface(interface))
513 return &gen10g_driver;
516 return &genphy_driver;
519 static struct phy_driver *get_phy_driver(struct phy_device *phydev,
520 phy_interface_t interface)
522 struct list_head *entry;
523 int phy_id = phydev->phy_id;
524 struct phy_driver *drv = NULL;
526 list_for_each(entry, &phy_drivers) {
527 drv = list_entry(entry, struct phy_driver, list);
528 if ((drv->uid & drv->mask) == (phy_id & drv->mask))
532 /* If we made it here, there's no driver for this PHY */
533 return generic_for_interface(interface);
536 static struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
538 phy_interface_t interface)
540 struct phy_device *dev;
542 /* We allocate the device, and initialize the
544 dev = malloc(sizeof(*dev));
546 printf("Failed to allocate PHY device for %s:%d\n",
551 memset(dev, 0, sizeof(*dev));
555 dev->interface = interface;
557 dev->autoneg = AUTONEG_ENABLE;
560 dev->phy_id = phy_id;
563 dev->drv = get_phy_driver(dev, interface);
567 bus->phymap[addr] = dev;
573 * get_phy_id - reads the specified addr for its ID.
574 * @bus: the target MII bus
575 * @addr: PHY address on the MII bus
576 * @phy_id: where to store the ID retrieved.
578 * Description: Reads the ID registers of the PHY at @addr on the
579 * @bus, stores it in @phy_id and returns zero on success.
581 int __weak get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
585 /* Grab the bits from PHYIR1, and put them
586 * in the upper half */
587 phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
592 *phy_id = (phy_reg & 0xffff) << 16;
594 /* Grab the bits from PHYIR2, and put them in the lower half */
595 phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
600 *phy_id |= (phy_reg & 0xffff);
605 static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
606 unsigned phy_mask, int devad, phy_interface_t interface)
608 u32 phy_id = 0xffffffff;
610 int addr = ffs(phy_mask) - 1;
611 int r = get_phy_id(bus, addr, devad, &phy_id);
612 /* If the PHY ID is mostly f's, we didn't find anything */
613 if (r == 0 && (phy_id & 0x1fffffff) != 0x1fffffff)
614 return phy_device_create(bus, addr, phy_id, interface);
615 phy_mask &= ~(1 << addr);
620 static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
621 unsigned phy_mask, phy_interface_t interface)
623 /* If we have one, return the existing device, with new interface */
625 int addr = ffs(phy_mask) - 1;
626 if (bus->phymap[addr]) {
627 bus->phymap[addr]->interface = interface;
628 return bus->phymap[addr];
630 phy_mask &= ~(1 << addr);
635 static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
636 unsigned phy_mask, phy_interface_t interface)
639 struct phy_device *phydev;
641 phydev = search_for_existing_phy(bus, phy_mask, interface);
644 /* Try Standard (ie Clause 22) access */
645 /* Otherwise we have to try Clause 45 */
646 for (i = 0; i < 5; i++) {
647 phydev = create_phy_by_mask(bus, phy_mask,
648 i ? i : MDIO_DEVAD_NONE, interface);
654 printf("Phy not found\n");
655 return phy_device_create(bus, ffs(phy_mask) - 1, 0xffffffff, interface);
659 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
660 * @bus: the target MII bus
661 * @addr: PHY address on the MII bus
663 * Description: Reads the ID registers of the PHY at @addr on the
664 * @bus, then allocates and returns the phy_device to represent it.
666 static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
667 phy_interface_t interface)
669 return get_phy_device_by_mask(bus, 1 << addr, interface);
672 int phy_reset(struct phy_device *phydev)
676 int devad = MDIO_DEVAD_NONE;
678 #ifdef CONFIG_PHYLIB_10G
679 /* If it's 10G, we need to issue reset through one of the MMDs */
680 if (is_10g_interface(phydev->interface)) {
682 gen10g_discover_mmds(phydev);
684 devad = ffs(phydev->mmds) - 1;
688 reg = phy_read(phydev, devad, MII_BMCR);
690 debug("PHY status read failed\n");
696 if (phy_write(phydev, devad, MII_BMCR, reg) < 0) {
697 debug("PHY reset failed\n");
701 #ifdef CONFIG_PHY_RESET_DELAY
702 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
705 * Poll the control register for the reset bit to go to 0 (it is
706 * auto-clearing). This should happen within 0.5 seconds per the
709 while ((reg & BMCR_RESET) && timeout--) {
710 reg = phy_read(phydev, devad, MII_BMCR);
713 debug("PHY status read failed\n");
719 if (reg & BMCR_RESET) {
720 puts("PHY reset timed out\n");
727 int miiphy_reset(const char *devname, unsigned char addr)
729 struct mii_dev *bus = miiphy_get_dev_by_name(devname);
730 struct phy_device *phydev;
733 * miiphy_reset was only used on standard PHYs, so we'll fake it here.
734 * If later code tries to connect with the right interface, this will
735 * be corrected by get_phy_device in phy_connect()
737 phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII);
739 return phy_reset(phydev);
742 struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask,
743 phy_interface_t interface)
749 /* Wait 15ms to make sure the PHY has come out of hard reset */
751 return get_phy_device_by_mask(bus, phy_mask, interface);
754 void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev)
756 /* Soft Reset the PHY */
759 printf("%s:%d is connected to %s. Reconnecting to %s\n",
760 phydev->bus->name, phydev->addr,
761 phydev->dev->name, dev->name);
764 debug("%s connected to %s\n", dev->name, phydev->drv->name);
767 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
768 struct eth_device *dev, phy_interface_t interface)
770 struct phy_device *phydev;
772 phydev = phy_find_by_mask(bus, 1 << addr, interface);
774 phy_connect_dev(phydev, dev);
776 printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
781 * Start the PHY. Returns 0 on success, or a negative error code.
783 int phy_startup(struct phy_device *phydev)
785 if (phydev->drv->startup)
786 return phydev->drv->startup(phydev);
791 static int __board_phy_config(struct phy_device *phydev)
793 if (phydev->drv->config)
794 return phydev->drv->config(phydev);
798 int board_phy_config(struct phy_device *phydev)
799 __attribute__((weak, alias("__board_phy_config")));
801 int phy_config(struct phy_device *phydev)
803 /* Invoke an optional board-specific helper */
804 board_phy_config(phydev);
809 int phy_shutdown(struct phy_device *phydev)
811 if (phydev->drv->shutdown)
812 phydev->drv->shutdown(phydev);