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;
80 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv);
87 /* Configure gigabit if it's supported */
88 if (phydev->supported & (SUPPORTED_1000baseT_Half |
89 SUPPORTED_1000baseT_Full)) {
90 oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
95 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
96 if (advertise & SUPPORTED_1000baseT_Half)
97 adv |= ADVERTISE_1000HALF;
98 if (advertise & SUPPORTED_1000baseT_Full)
99 adv |= ADVERTISE_1000FULL;
102 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000,
116 * genphy_setup_forced - configures/forces speed/duplex from @phydev
117 * @phydev: target phy_device struct
119 * Description: Configures MII_BMCR to force speed/duplex
120 * to the values in phydev. Assumes that the values are valid.
122 static int genphy_setup_forced(struct phy_device *phydev)
127 phydev->pause = phydev->asym_pause = 0;
129 if (SPEED_1000 == phydev->speed)
130 ctl |= BMCR_SPEED1000;
131 else if (SPEED_100 == phydev->speed)
132 ctl |= BMCR_SPEED100;
134 if (DUPLEX_FULL == phydev->duplex)
135 ctl |= BMCR_FULLDPLX;
137 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
144 * genphy_restart_aneg - Enable and Restart Autonegotiation
145 * @phydev: target phy_device struct
147 int genphy_restart_aneg(struct phy_device *phydev)
151 ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
156 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
158 /* Don't isolate the PHY if we're negotiating */
159 ctl &= ~(BMCR_ISOLATE);
161 ctl = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
168 * genphy_config_aneg - restart auto-negotiation or write BMCR
169 * @phydev: target phy_device struct
171 * Description: If auto-negotiation is enabled, we configure the
172 * advertising, and then restart auto-negotiation. If it is not
173 * enabled, then we write the BMCR.
175 int genphy_config_aneg(struct phy_device *phydev)
179 if (AUTONEG_ENABLE != phydev->autoneg)
180 return genphy_setup_forced(phydev);
182 result = genphy_config_advert(phydev);
184 if (result < 0) /* error */
188 /* Advertisment hasn't changed, but maybe aneg was never on to
189 * begin with? Or maybe phy was isolated? */
190 int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
195 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
196 result = 1; /* do restart aneg */
199 /* Only restart aneg if we are advertising something different
200 * than we were before. */
202 result = genphy_restart_aneg(phydev);
208 * genphy_update_link - update link status in @phydev
209 * @phydev: target phy_device struct
211 * Description: Update the value in phydev->link to reflect the
212 * current link value. In order to do this, we need to read
213 * the status register twice, keeping the second value.
215 int genphy_update_link(struct phy_device *phydev)
217 unsigned int mii_reg;
220 * Wait if the link is up, and autonegotiation is in progress
221 * (ie - we're capable and it's not done)
223 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
226 * If we already saw the link up, and it hasn't gone down, then
227 * we don't need to wait for autoneg again
229 if (phydev->link && mii_reg & BMSR_LSTATUS)
232 if ((mii_reg & BMSR_ANEGCAPABLE) && !(mii_reg & BMSR_ANEGCOMPLETE)) {
235 printf("%s Waiting for PHY auto negotiation to complete",
237 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
241 if (i > PHY_ANEG_TIMEOUT) {
242 printf(" TIMEOUT !\n");
248 puts("user interrupt!\n");
253 if ((i++ % 500) == 0)
256 udelay(1000); /* 1 ms */
257 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
262 /* Read the link a second time to clear the latched state */
263 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
265 if (mii_reg & BMSR_LSTATUS)
275 * Generic function which updates the speed and duplex. If
276 * autonegotiation is enabled, it uses the AND of the link
277 * partner's advertised capabilities and our advertised
278 * capabilities. If autonegotiation is disabled, we use the
279 * appropriate bits in the control register.
281 * Stolen from Linux's mii.c and phy_device.c
283 static int genphy_parse_link(struct phy_device *phydev)
285 int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
287 /* We're using autonegotiation */
288 if (mii_reg & BMSR_ANEGCAPABLE) {
292 /* Check for gigabit capability */
293 if (mii_reg & BMSR_ERCAP) {
294 /* We want a list of states supported by
295 * both PHYs in the link
297 gblpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
298 gblpa &= phy_read(phydev,
299 MDIO_DEVAD_NONE, MII_CTRL1000) << 2;
302 /* Set the baseline so we only have to set them
303 * if they're different
305 phydev->speed = SPEED_10;
306 phydev->duplex = DUPLEX_HALF;
308 /* Check the gigabit fields */
309 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
310 phydev->speed = SPEED_1000;
312 if (gblpa & PHY_1000BTSR_1000FD)
313 phydev->duplex = DUPLEX_FULL;
319 lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
320 lpa &= phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
322 if (lpa & (LPA_100FULL | LPA_100HALF)) {
323 phydev->speed = SPEED_100;
325 if (lpa & LPA_100FULL)
326 phydev->duplex = DUPLEX_FULL;
328 } else if (lpa & LPA_10FULL)
329 phydev->duplex = DUPLEX_FULL;
331 u32 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
333 phydev->speed = SPEED_10;
334 phydev->duplex = DUPLEX_HALF;
336 if (bmcr & BMCR_FULLDPLX)
337 phydev->duplex = DUPLEX_FULL;
339 if (bmcr & BMCR_SPEED1000)
340 phydev->speed = SPEED_1000;
341 else if (bmcr & BMCR_SPEED100)
342 phydev->speed = SPEED_100;
348 int genphy_config(struct phy_device *phydev)
353 /* For now, I'll claim that the generic driver supports
354 * all possible port types */
355 features = (SUPPORTED_TP | SUPPORTED_MII
356 | SUPPORTED_AUI | SUPPORTED_FIBRE |
359 /* Do we support autonegotiation? */
360 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
365 if (val & BMSR_ANEGCAPABLE)
366 features |= SUPPORTED_Autoneg;
368 if (val & BMSR_100FULL)
369 features |= SUPPORTED_100baseT_Full;
370 if (val & BMSR_100HALF)
371 features |= SUPPORTED_100baseT_Half;
372 if (val & BMSR_10FULL)
373 features |= SUPPORTED_10baseT_Full;
374 if (val & BMSR_10HALF)
375 features |= SUPPORTED_10baseT_Half;
377 if (val & BMSR_ESTATEN) {
378 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS);
383 if (val & ESTATUS_1000_TFULL)
384 features |= SUPPORTED_1000baseT_Full;
385 if (val & ESTATUS_1000_THALF)
386 features |= SUPPORTED_1000baseT_Half;
389 phydev->supported = features;
390 phydev->advertising = features;
392 genphy_config_aneg(phydev);
397 int genphy_startup(struct phy_device *phydev)
399 genphy_update_link(phydev);
400 genphy_parse_link(phydev);
405 int genphy_shutdown(struct phy_device *phydev)
410 static struct phy_driver genphy_driver = {
413 .name = "Generic PHY",
415 .config = genphy_config,
416 .startup = genphy_startup,
417 .shutdown = genphy_shutdown,
420 static LIST_HEAD(phy_drivers);
424 #ifdef CONFIG_PHY_ATHEROS
427 #ifdef CONFIG_PHY_BROADCOM
430 #ifdef CONFIG_PHY_DAVICOM
433 #ifdef CONFIG_PHY_ET1011C
436 #ifdef CONFIG_PHY_LXT
439 #ifdef CONFIG_PHY_MARVELL
442 #ifdef CONFIG_PHY_MICREL
445 #ifdef CONFIG_PHY_NATSEMI
448 #ifdef CONFIG_PHY_REALTEK
451 #ifdef CONFIG_PHY_SMSC
454 #ifdef CONFIG_PHY_TERANETICS
455 phy_teranetics_init();
457 #ifdef CONFIG_PHY_VITESSE
464 int phy_register(struct phy_driver *drv)
466 INIT_LIST_HEAD(&drv->list);
467 list_add_tail(&drv->list, &phy_drivers);
472 static int phy_probe(struct phy_device *phydev)
476 phydev->advertising = phydev->supported = phydev->drv->features;
477 phydev->mmds = phydev->drv->mmds;
479 if (phydev->drv->probe)
480 err = phydev->drv->probe(phydev);
485 static struct phy_driver *generic_for_interface(phy_interface_t interface)
487 #ifdef CONFIG_PHYLIB_10G
488 if (is_10g_interface(interface))
489 return &gen10g_driver;
492 return &genphy_driver;
495 static struct phy_driver *get_phy_driver(struct phy_device *phydev,
496 phy_interface_t interface)
498 struct list_head *entry;
499 int phy_id = phydev->phy_id;
500 struct phy_driver *drv = NULL;
502 list_for_each(entry, &phy_drivers) {
503 drv = list_entry(entry, struct phy_driver, list);
504 if ((drv->uid & drv->mask) == (phy_id & drv->mask))
508 /* If we made it here, there's no driver for this PHY */
509 return generic_for_interface(interface);
512 static struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
514 phy_interface_t interface)
516 struct phy_device *dev;
518 /* We allocate the device, and initialize the
520 dev = malloc(sizeof(*dev));
522 printf("Failed to allocate PHY device for %s:%d\n",
527 memset(dev, 0, sizeof(*dev));
531 dev->interface = interface;
533 dev->autoneg = AUTONEG_ENABLE;
536 dev->phy_id = phy_id;
539 dev->drv = get_phy_driver(dev, interface);
543 bus->phymap[addr] = dev;
549 * get_phy_id - reads the specified addr for its ID.
550 * @bus: the target MII bus
551 * @addr: PHY address on the MII bus
552 * @phy_id: where to store the ID retrieved.
554 * Description: Reads the ID registers of the PHY at @addr on the
555 * @bus, stores it in @phy_id and returns zero on success.
557 static int get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
561 /* Grab the bits from PHYIR1, and put them
562 * in the upper half */
563 phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
568 *phy_id = (phy_reg & 0xffff) << 16;
570 /* Grab the bits from PHYIR2, and put them in the lower half */
571 phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
576 *phy_id |= (phy_reg & 0xffff);
581 static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
582 unsigned phy_mask, int devad, phy_interface_t interface)
584 u32 phy_id = 0xffffffff;
586 int addr = ffs(phy_mask) - 1;
587 int r = get_phy_id(bus, addr, devad, &phy_id);
590 /* If the PHY ID is mostly f's, we didn't find anything */
591 if ((phy_id & 0x1fffffff) != 0x1fffffff)
592 return phy_device_create(bus, addr, phy_id, interface);
593 phy_mask &= ~(1 << addr);
598 static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
599 unsigned phy_mask, phy_interface_t interface)
601 /* If we have one, return the existing device, with new interface */
603 int addr = ffs(phy_mask) - 1;
604 if (bus->phymap[addr]) {
605 bus->phymap[addr]->interface = interface;
606 return bus->phymap[addr];
608 phy_mask &= ~(1 << addr);
613 static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
614 unsigned phy_mask, phy_interface_t interface)
617 struct phy_device *phydev;
619 phydev = search_for_existing_phy(bus, phy_mask, interface);
622 /* Try Standard (ie Clause 22) access */
623 /* Otherwise we have to try Clause 45 */
624 for (i = 0; i < 5; i++) {
625 phydev = create_phy_by_mask(bus, phy_mask,
626 i ? i : MDIO_DEVAD_NONE, interface);
632 printf("Phy not found\n");
633 return phy_device_create(bus, ffs(phy_mask) - 1, 0xffffffff, interface);
637 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
638 * @bus: the target MII bus
639 * @addr: PHY address on the MII bus
641 * Description: Reads the ID registers of the PHY at @addr on the
642 * @bus, then allocates and returns the phy_device to represent it.
644 static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
645 phy_interface_t interface)
647 return get_phy_device_by_mask(bus, 1 << addr, interface);
650 int phy_reset(struct phy_device *phydev)
654 int devad = MDIO_DEVAD_NONE;
656 #ifdef CONFIG_PHYLIB_10G
657 /* If it's 10G, we need to issue reset through one of the MMDs */
658 if (is_10g_interface(phydev->interface)) {
660 gen10g_discover_mmds(phydev);
662 devad = ffs(phydev->mmds) - 1;
666 reg = phy_read(phydev, devad, MII_BMCR);
668 debug("PHY status read failed\n");
674 if (phy_write(phydev, devad, MII_BMCR, reg) < 0) {
675 debug("PHY reset failed\n");
679 #ifdef CONFIG_PHY_RESET_DELAY
680 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
683 * Poll the control register for the reset bit to go to 0 (it is
684 * auto-clearing). This should happen within 0.5 seconds per the
687 while ((reg & BMCR_RESET) && timeout--) {
688 reg = phy_read(phydev, devad, MII_BMCR);
691 debug("PHY status read failed\n");
697 if (reg & BMCR_RESET) {
698 puts("PHY reset timed out\n");
705 int miiphy_reset(const char *devname, unsigned char addr)
707 struct mii_dev *bus = miiphy_get_dev_by_name(devname);
708 struct phy_device *phydev;
711 * miiphy_reset was only used on standard PHYs, so we'll fake it here.
712 * If later code tries to connect with the right interface, this will
713 * be corrected by get_phy_device in phy_connect()
715 phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII);
717 return phy_reset(phydev);
720 struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask,
721 phy_interface_t interface)
727 /* Wait 15ms to make sure the PHY has come out of hard reset */
729 return get_phy_device_by_mask(bus, phy_mask, interface);
732 void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev)
734 /* Soft Reset the PHY */
737 printf("%s:%d is connected to %s. Reconnecting to %s\n",
738 phydev->bus->name, phydev->addr,
739 phydev->dev->name, dev->name);
742 debug("%s connected to %s\n", dev->name, phydev->drv->name);
745 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
746 struct eth_device *dev, phy_interface_t interface)
748 struct phy_device *phydev;
750 phydev = phy_find_by_mask(bus, 1 << addr, interface);
752 phy_connect_dev(phydev, dev);
754 printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
759 * Start the PHY. Returns 0 on success, or a negative error code.
761 int phy_startup(struct phy_device *phydev)
763 if (phydev->drv->startup)
764 return phydev->drv->startup(phydev);
769 static int __board_phy_config(struct phy_device *phydev)
771 if (phydev->drv->config)
772 return phydev->drv->config(phydev);
776 int board_phy_config(struct phy_device *phydev)
777 __attribute__((weak, alias("__board_phy_config")));
779 int phy_config(struct phy_device *phydev)
781 /* Invoke an optional board-specific helper */
782 board_phy_config(phydev);
787 int phy_shutdown(struct phy_device *phydev)
789 if (phydev->drv->shutdown)
790 phydev->drv->shutdown(phydev);