2 * Generic PHY Management code
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * Copyright 2011 Freescale Semiconductor, Inc.
23 * Based loosely off of Linux's PHY Lib
34 #include <linux/err.h>
36 /* Generic PHY support and helper functions */
39 * genphy_config_advert - sanitize and advertise auto-negotation parameters
40 * @phydev: target phy_device struct
42 * Description: Writes MII_ADVERTISE with the appropriate values,
43 * after sanitizing the values to make sure we only advertise
44 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
45 * hasn't changed, and > 0 if it has changed.
47 static int genphy_config_advert(struct phy_device *phydev)
53 /* Only allow advertising what
54 * this PHY supports */
55 phydev->advertising &= phydev->supported;
56 advertise = phydev->advertising;
58 /* Setup standard advertisement */
59 oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
64 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
65 ADVERTISE_PAUSE_ASYM);
66 if (advertise & ADVERTISED_10baseT_Half)
67 adv |= ADVERTISE_10HALF;
68 if (advertise & ADVERTISED_10baseT_Full)
69 adv |= ADVERTISE_10FULL;
70 if (advertise & ADVERTISED_100baseT_Half)
71 adv |= ADVERTISE_100HALF;
72 if (advertise & ADVERTISED_100baseT_Full)
73 adv |= ADVERTISE_100FULL;
74 if (advertise & ADVERTISED_Pause)
75 adv |= ADVERTISE_PAUSE_CAP;
76 if (advertise & ADVERTISED_Asym_Pause)
77 adv |= ADVERTISE_PAUSE_ASYM;
78 if (advertise & ADVERTISED_1000baseX_Half)
79 adv |= ADVERTISE_1000XHALF;
80 if (advertise & ADVERTISED_1000baseX_Full)
81 adv |= ADVERTISE_1000XFULL;
84 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv);
91 /* Configure gigabit if it's supported */
92 if (phydev->supported & (SUPPORTED_1000baseT_Half |
93 SUPPORTED_1000baseT_Full)) {
94 oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
99 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
100 if (advertise & SUPPORTED_1000baseT_Half)
101 adv |= ADVERTISE_1000HALF;
102 if (advertise & SUPPORTED_1000baseT_Full)
103 adv |= ADVERTISE_1000FULL;
106 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000,
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)
131 phydev->pause = phydev->asym_pause = 0;
133 if (SPEED_1000 == phydev->speed)
134 ctl |= BMCR_SPEED1000;
135 else if (SPEED_100 == phydev->speed)
136 ctl |= BMCR_SPEED100;
138 if (DUPLEX_FULL == phydev->duplex)
139 ctl |= BMCR_FULLDPLX;
141 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);
172 * genphy_config_aneg - restart auto-negotiation or write BMCR
173 * @phydev: target phy_device struct
175 * Description: If auto-negotiation is enabled, we configure the
176 * advertising, and then restart auto-negotiation. If it is not
177 * enabled, then we write the BMCR.
179 int genphy_config_aneg(struct phy_device *phydev)
183 if (AUTONEG_ENABLE != phydev->autoneg)
184 return genphy_setup_forced(phydev);
186 result = genphy_config_advert(phydev);
188 if (result < 0) /* error */
192 /* Advertisment hasn't changed, but maybe aneg was never on to
193 * begin with? Or maybe phy was isolated? */
194 int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
199 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
200 result = 1; /* do restart aneg */
203 /* Only restart aneg if we are advertising something different
204 * than we were before. */
206 result = genphy_restart_aneg(phydev);
212 * genphy_update_link - update link status in @phydev
213 * @phydev: target phy_device struct
215 * Description: Update the value in phydev->link to reflect the
216 * current link value. In order to do this, we need to read
217 * the status register twice, keeping the second value.
219 int genphy_update_link(struct phy_device *phydev)
221 unsigned int mii_reg;
224 * Wait if the link is up, and autonegotiation is in progress
225 * (ie - we're capable and it's not done)
227 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
230 * If we already saw the link up, and it hasn't gone down, then
231 * we don't need to wait for autoneg again
233 if (phydev->link && mii_reg & BMSR_LSTATUS)
236 if ((mii_reg & BMSR_ANEGCAPABLE) && !(mii_reg & BMSR_ANEGCOMPLETE)) {
239 printf("%s Waiting for PHY auto negotiation to complete",
241 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
245 if (i > PHY_ANEG_TIMEOUT) {
246 printf(" TIMEOUT !\n");
252 puts("user interrupt!\n");
257 if ((i++ % 500) == 0)
260 udelay(1000); /* 1 ms */
261 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
266 /* Read the link a second time to clear the latched state */
267 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
269 if (mii_reg & BMSR_LSTATUS)
279 * Generic function which updates the speed and duplex. If
280 * autonegotiation is enabled, it uses the AND of the link
281 * partner's advertised capabilities and our advertised
282 * capabilities. If autonegotiation is disabled, we use the
283 * appropriate bits in the control register.
285 * Stolen from Linux's mii.c and phy_device.c
287 int genphy_parse_link(struct phy_device *phydev)
289 int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
291 /* We're using autonegotiation */
292 if (mii_reg & BMSR_ANEGCAPABLE) {
297 /* Check for gigabit capability */
298 if (mii_reg & BMSR_ERCAP) {
299 /* We want a list of states supported by
300 * both PHYs in the link
302 gblpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
303 gblpa &= phy_read(phydev,
304 MDIO_DEVAD_NONE, MII_CTRL1000) << 2;
307 /* Set the baseline so we only have to set them
308 * if they're different
310 phydev->speed = SPEED_10;
311 phydev->duplex = DUPLEX_HALF;
313 /* Check the gigabit fields */
314 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
315 phydev->speed = SPEED_1000;
317 if (gblpa & PHY_1000BTSR_1000FD)
318 phydev->duplex = DUPLEX_FULL;
324 lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
325 lpa &= phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
327 if (lpa & (LPA_100FULL | LPA_100HALF)) {
328 phydev->speed = SPEED_100;
330 if (lpa & LPA_100FULL)
331 phydev->duplex = DUPLEX_FULL;
333 } else if (lpa & LPA_10FULL)
334 phydev->duplex = DUPLEX_FULL;
336 if (mii_reg & BMSR_ESTATEN)
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_Full;
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 static int 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);
614 /* If the PHY ID is mostly f's, we didn't find anything */
615 if ((phy_id & 0x1fffffff) != 0x1fffffff)
616 return phy_device_create(bus, addr, phy_id, interface);
617 phy_mask &= ~(1 << addr);
622 static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
623 unsigned phy_mask, phy_interface_t interface)
625 /* If we have one, return the existing device, with new interface */
627 int addr = ffs(phy_mask) - 1;
628 if (bus->phymap[addr]) {
629 bus->phymap[addr]->interface = interface;
630 return bus->phymap[addr];
632 phy_mask &= ~(1 << addr);
637 static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
638 unsigned phy_mask, phy_interface_t interface)
641 struct phy_device *phydev;
643 phydev = search_for_existing_phy(bus, phy_mask, interface);
646 /* Try Standard (ie Clause 22) access */
647 /* Otherwise we have to try Clause 45 */
648 for (i = 0; i < 5; i++) {
649 phydev = create_phy_by_mask(bus, phy_mask,
650 i ? i : MDIO_DEVAD_NONE, interface);
656 printf("Phy not found\n");
657 return phy_device_create(bus, ffs(phy_mask) - 1, 0xffffffff, interface);
661 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
662 * @bus: the target MII bus
663 * @addr: PHY address on the MII bus
665 * Description: Reads the ID registers of the PHY at @addr on the
666 * @bus, then allocates and returns the phy_device to represent it.
668 static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
669 phy_interface_t interface)
671 return get_phy_device_by_mask(bus, 1 << addr, interface);
674 int phy_reset(struct phy_device *phydev)
678 int devad = MDIO_DEVAD_NONE;
680 #ifdef CONFIG_PHYLIB_10G
681 /* If it's 10G, we need to issue reset through one of the MMDs */
682 if (is_10g_interface(phydev->interface)) {
684 gen10g_discover_mmds(phydev);
686 devad = ffs(phydev->mmds) - 1;
690 reg = phy_read(phydev, devad, MII_BMCR);
692 debug("PHY status read failed\n");
698 if (phy_write(phydev, devad, MII_BMCR, reg) < 0) {
699 debug("PHY reset failed\n");
703 #ifdef CONFIG_PHY_RESET_DELAY
704 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
707 * Poll the control register for the reset bit to go to 0 (it is
708 * auto-clearing). This should happen within 0.5 seconds per the
711 while ((reg & BMCR_RESET) && timeout--) {
712 reg = phy_read(phydev, devad, MII_BMCR);
715 debug("PHY status read failed\n");
721 if (reg & BMCR_RESET) {
722 puts("PHY reset timed out\n");
729 int miiphy_reset(const char *devname, unsigned char addr)
731 struct mii_dev *bus = miiphy_get_dev_by_name(devname);
732 struct phy_device *phydev;
735 * miiphy_reset was only used on standard PHYs, so we'll fake it here.
736 * If later code tries to connect with the right interface, this will
737 * be corrected by get_phy_device in phy_connect()
739 phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII);
741 return phy_reset(phydev);
744 struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask,
745 phy_interface_t interface)
751 /* Wait 15ms to make sure the PHY has come out of hard reset */
753 return get_phy_device_by_mask(bus, phy_mask, interface);
756 void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev)
758 /* Soft Reset the PHY */
761 printf("%s:%d is connected to %s. Reconnecting to %s\n",
762 phydev->bus->name, phydev->addr,
763 phydev->dev->name, dev->name);
766 debug("%s connected to %s\n", dev->name, phydev->drv->name);
769 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
770 struct eth_device *dev, phy_interface_t interface)
772 struct phy_device *phydev;
774 phydev = phy_find_by_mask(bus, 1 << addr, interface);
776 phy_connect_dev(phydev, dev);
778 printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
783 * Start the PHY. Returns 0 on success, or a negative error code.
785 int phy_startup(struct phy_device *phydev)
787 if (phydev->drv->startup)
788 return phydev->drv->startup(phydev);
793 static int __board_phy_config(struct phy_device *phydev)
795 if (phydev->drv->config)
796 return phydev->drv->config(phydev);
800 int board_phy_config(struct phy_device *phydev)
801 __attribute__((weak, alias("__board_phy_config")));
803 int phy_config(struct phy_device *phydev)
805 /* Invoke an optional board-specific helper */
806 board_phy_config(phydev);
811 int phy_shutdown(struct phy_device *phydev)
813 if (phydev->drv->shutdown)
814 phydev->drv->shutdown(phydev);