2 * drivers/net/phy/phy_device.c
4 * Framework for finding and configuring PHYs.
5 * Also contains generic PHY driver
9 * Copyright (c) 2004 Freescale Semiconductor, Inc.
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
17 #include <linux/kernel.h>
18 #include <linux/string.h>
19 #include <linux/errno.h>
20 #include <linux/unistd.h>
21 #include <linux/slab.h>
22 #include <linux/interrupt.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/skbuff.h>
29 #include <linux/module.h>
30 #include <linux/mii.h>
31 #include <linux/ethtool.h>
32 #include <linux/phy.h>
36 #include <asm/uaccess.h>
38 MODULE_DESCRIPTION("PHY library");
39 MODULE_AUTHOR("Andy Fleming");
40 MODULE_LICENSE("GPL");
42 static struct phy_driver genphy_driver;
43 extern int mdio_bus_init(void);
44 extern void mdio_bus_exit(void);
46 void phy_device_free(struct phy_device *phydev)
51 static void phy_device_release(struct device *dev)
53 phy_device_free(to_phy_device(dev));
56 static LIST_HEAD(phy_fixup_list);
57 static DEFINE_MUTEX(phy_fixup_lock);
60 * Creates a new phy_fixup and adds it to the list
61 * @bus_id: A string which matches phydev->dev.bus_id (or PHY_ANY_ID)
62 * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
63 * It can also be PHY_ANY_UID
64 * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
66 * @run: The actual code to be run when a matching PHY is found
68 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
69 int (*run)(struct phy_device *))
71 struct phy_fixup *fixup;
73 fixup = kzalloc(sizeof(struct phy_fixup), GFP_KERNEL);
77 strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
78 fixup->phy_uid = phy_uid;
79 fixup->phy_uid_mask = phy_uid_mask;
82 mutex_lock(&phy_fixup_lock);
83 list_add_tail(&fixup->list, &phy_fixup_list);
84 mutex_unlock(&phy_fixup_lock);
88 EXPORT_SYMBOL(phy_register_fixup);
90 /* Registers a fixup to be run on any PHY with the UID in phy_uid */
91 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
92 int (*run)(struct phy_device *))
94 return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
96 EXPORT_SYMBOL(phy_register_fixup_for_uid);
98 /* Registers a fixup to be run on the PHY with id string bus_id */
99 int phy_register_fixup_for_id(const char *bus_id,
100 int (*run)(struct phy_device *))
102 return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
104 EXPORT_SYMBOL(phy_register_fixup_for_id);
107 * Returns 1 if fixup matches phydev in bus_id and phy_uid.
108 * Fixups can be set to match any in one or more fields.
110 static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
112 if (strcmp(fixup->bus_id, dev_name(&phydev->dev)) != 0)
113 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
116 if ((fixup->phy_uid & fixup->phy_uid_mask) !=
117 (phydev->phy_id & fixup->phy_uid_mask))
118 if (fixup->phy_uid != PHY_ANY_UID)
124 /* Runs any matching fixups for this phydev */
125 int phy_scan_fixups(struct phy_device *phydev)
127 struct phy_fixup *fixup;
129 mutex_lock(&phy_fixup_lock);
130 list_for_each_entry(fixup, &phy_fixup_list, list) {
131 if (phy_needs_fixup(phydev, fixup)) {
134 err = fixup->run(phydev);
140 mutex_unlock(&phy_fixup_lock);
144 EXPORT_SYMBOL(phy_scan_fixups);
146 struct phy_device* phy_device_create(struct mii_bus *bus, int addr, int phy_id)
148 struct phy_device *dev;
149 /* We allocate the device, and initialize the
151 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
154 return (struct phy_device*) PTR_ERR((void*)-ENOMEM);
156 dev->dev.release = phy_device_release;
160 dev->pause = dev->asym_pause = 0;
162 dev->interface = PHY_INTERFACE_MODE_GMII;
164 dev->autoneg = AUTONEG_ENABLE;
167 dev->phy_id = phy_id;
170 dev->state = PHY_DOWN;
172 mutex_init(&dev->lock);
176 EXPORT_SYMBOL(phy_device_create);
179 * get_phy_id - reads the specified addr for its ID.
180 * @bus: the target MII bus
181 * @addr: PHY address on the MII bus
182 * @phy_id: where to store the ID retrieved.
184 * Description: Reads the ID registers of the PHY at @addr on the
185 * @bus, stores it in @phy_id and returns zero on success.
187 int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id)
191 /* Grab the bits from PHYIR1, and put them
192 * in the upper half */
193 phy_reg = bus->read(bus, addr, MII_PHYSID1);
198 *phy_id = (phy_reg & 0xffff) << 16;
200 /* Grab the bits from PHYIR2, and put them in the lower half */
201 phy_reg = bus->read(bus, addr, MII_PHYSID2);
206 *phy_id |= (phy_reg & 0xffff);
210 EXPORT_SYMBOL(get_phy_id);
213 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
214 * @bus: the target MII bus
215 * @addr: PHY address on the MII bus
217 * Description: Reads the ID registers of the PHY at @addr on the
218 * @bus, then allocates and returns the phy_device to represent it.
220 struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
222 struct phy_device *dev = NULL;
226 r = get_phy_id(bus, addr, &phy_id);
230 /* If the phy_id is mostly Fs, there is no device there */
231 if ((phy_id & 0x1fffffff) == 0x1fffffff)
235 * Broken hardware is sometimes missing the pull down resistor on the
236 * MDIO line, which results in reads to non-existent devices returning
237 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
243 dev = phy_device_create(bus, addr, phy_id);
249 * phy_prepare_link - prepares the PHY layer to monitor link status
250 * @phydev: target phy_device struct
251 * @handler: callback function for link status change notifications
253 * Description: Tells the PHY infrastructure to handle the
254 * gory details on monitoring link status (whether through
255 * polling or an interrupt), and to call back to the
256 * connected device driver when the link status changes.
257 * If you want to monitor your own link state, don't call
260 void phy_prepare_link(struct phy_device *phydev,
261 void (*handler)(struct net_device *))
263 phydev->adjust_link = handler;
267 * phy_connect - connect an ethernet device to a PHY device
268 * @dev: the network device to connect
269 * @bus_id: the id string of the PHY device to connect
270 * @handler: callback function for state change notifications
271 * @flags: PHY device's dev_flags
272 * @interface: PHY device's interface
274 * Description: Convenience function for connecting ethernet
275 * devices to PHY devices. The default behavior is for
276 * the PHY infrastructure to handle everything, and only notify
277 * the connected driver when the link status changes. If you
278 * don't want, or can't use the provided functionality, you may
279 * choose to call only the subset of functions which provide
280 * the desired functionality.
282 struct phy_device * phy_connect(struct net_device *dev, const char *bus_id,
283 void (*handler)(struct net_device *), u32 flags,
284 phy_interface_t interface)
286 struct phy_device *phydev;
288 phydev = phy_attach(dev, bus_id, flags, interface);
293 phy_prepare_link(phydev, handler);
295 phy_start_machine(phydev, NULL);
298 phy_start_interrupts(phydev);
302 EXPORT_SYMBOL(phy_connect);
305 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY device
306 * @phydev: target phy_device struct
308 void phy_disconnect(struct phy_device *phydev)
311 phy_stop_interrupts(phydev);
313 phy_stop_machine(phydev);
315 phydev->adjust_link = NULL;
319 EXPORT_SYMBOL(phy_disconnect);
322 * phy_attach - attach a network device to a particular PHY device
323 * @dev: network device to attach
324 * @bus_id: PHY device to attach
325 * @flags: PHY device's dev_flags
326 * @interface: PHY device's interface
328 * Description: Called by drivers to attach to a particular PHY
329 * device. The phy_device is found, and properly hooked up
330 * to the phy_driver. If no driver is attached, then the
331 * genphy_driver is used. The phy_device is given a ptr to
332 * the attaching device, and given a callback for link status
333 * change. The phy_device is returned to the attaching driver.
335 struct phy_device *phy_attach(struct net_device *dev,
336 const char *bus_id, u32 flags, phy_interface_t interface)
338 struct bus_type *bus = &mdio_bus_type;
339 struct phy_device *phydev;
342 /* Search the list of PHY devices on the mdio bus for the
343 * PHY with the requested name */
344 d = bus_find_device_by_name(bus, NULL, bus_id);
346 phydev = to_phy_device(d);
348 printk(KERN_ERR "%s not found\n", bus_id);
349 return ERR_PTR(-ENODEV);
352 /* Assume that if there is no driver, that it doesn't
353 * exist, and we should use the genphy driver. */
354 if (NULL == d->driver) {
356 d->driver = &genphy_driver.driver;
358 err = d->driver->probe(d);
360 err = device_bind_driver(d);
366 if (phydev->attached_dev) {
367 printk(KERN_ERR "%s: %s already attached\n",
369 return ERR_PTR(-EBUSY);
372 phydev->attached_dev = dev;
374 phydev->dev_flags = flags;
376 phydev->interface = interface;
378 /* Do initial configuration here, now that
379 * we have certain key parameters
380 * (dev_flags and interface) */
381 if (phydev->drv->config_init) {
384 err = phy_scan_fixups(phydev);
389 err = phydev->drv->config_init(phydev);
397 EXPORT_SYMBOL(phy_attach);
400 * phy_detach - detach a PHY device from its network device
401 * @phydev: target phy_device struct
403 void phy_detach(struct phy_device *phydev)
405 phydev->attached_dev = NULL;
407 /* If the device had no specific driver before (i.e. - it
408 * was using the generic driver), we unbind the device
409 * from the generic driver so that there's a chance a
410 * real driver could be loaded */
411 if (phydev->dev.driver == &genphy_driver.driver)
412 device_release_driver(&phydev->dev);
414 EXPORT_SYMBOL(phy_detach);
417 /* Generic PHY support and helper functions */
420 * genphy_config_advert - sanitize and advertise auto-negotation parameters
421 * @phydev: target phy_device struct
423 * Description: Writes MII_ADVERTISE with the appropriate values,
424 * after sanitizing the values to make sure we only advertise
425 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
426 * hasn't changed, and > 0 if it has changed.
428 int genphy_config_advert(struct phy_device *phydev)
432 int err, changed = 0;
434 /* Only allow advertising what
435 * this PHY supports */
436 phydev->advertising &= phydev->supported;
437 advertise = phydev->advertising;
439 /* Setup standard advertisement */
440 oldadv = adv = phy_read(phydev, MII_ADVERTISE);
445 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
446 ADVERTISE_PAUSE_ASYM);
447 if (advertise & ADVERTISED_10baseT_Half)
448 adv |= ADVERTISE_10HALF;
449 if (advertise & ADVERTISED_10baseT_Full)
450 adv |= ADVERTISE_10FULL;
451 if (advertise & ADVERTISED_100baseT_Half)
452 adv |= ADVERTISE_100HALF;
453 if (advertise & ADVERTISED_100baseT_Full)
454 adv |= ADVERTISE_100FULL;
455 if (advertise & ADVERTISED_Pause)
456 adv |= ADVERTISE_PAUSE_CAP;
457 if (advertise & ADVERTISED_Asym_Pause)
458 adv |= ADVERTISE_PAUSE_ASYM;
461 err = phy_write(phydev, MII_ADVERTISE, adv);
468 /* Configure gigabit if it's supported */
469 if (phydev->supported & (SUPPORTED_1000baseT_Half |
470 SUPPORTED_1000baseT_Full)) {
471 oldadv = adv = phy_read(phydev, MII_CTRL1000);
476 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
477 if (advertise & SUPPORTED_1000baseT_Half)
478 adv |= ADVERTISE_1000HALF;
479 if (advertise & SUPPORTED_1000baseT_Full)
480 adv |= ADVERTISE_1000FULL;
483 err = phy_write(phydev, MII_CTRL1000, adv);
493 EXPORT_SYMBOL(genphy_config_advert);
496 * genphy_setup_forced - configures/forces speed/duplex from @phydev
497 * @phydev: target phy_device struct
499 * Description: Configures MII_BMCR to force speed/duplex
500 * to the values in phydev. Assumes that the values are valid.
501 * Please see phy_sanitize_settings().
503 int genphy_setup_forced(struct phy_device *phydev)
508 phydev->pause = phydev->asym_pause = 0;
510 if (SPEED_1000 == phydev->speed)
511 ctl |= BMCR_SPEED1000;
512 else if (SPEED_100 == phydev->speed)
513 ctl |= BMCR_SPEED100;
515 if (DUPLEX_FULL == phydev->duplex)
516 ctl |= BMCR_FULLDPLX;
518 err = phy_write(phydev, MII_BMCR, ctl);
525 * genphy_restart_aneg - Enable and Restart Autonegotiation
526 * @phydev: target phy_device struct
528 int genphy_restart_aneg(struct phy_device *phydev)
532 ctl = phy_read(phydev, MII_BMCR);
537 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
539 /* Don't isolate the PHY if we're negotiating */
540 ctl &= ~(BMCR_ISOLATE);
542 ctl = phy_write(phydev, MII_BMCR, ctl);
546 EXPORT_SYMBOL(genphy_restart_aneg);
550 * genphy_config_aneg - restart auto-negotiation or write BMCR
551 * @phydev: target phy_device struct
553 * Description: If auto-negotiation is enabled, we configure the
554 * advertising, and then restart auto-negotiation. If it is not
555 * enabled, then we write the BMCR.
557 int genphy_config_aneg(struct phy_device *phydev)
561 if (AUTONEG_ENABLE != phydev->autoneg)
562 return genphy_setup_forced(phydev);
564 result = genphy_config_advert(phydev);
566 if (result < 0) /* error */
570 /* Advertisment hasn't changed, but maybe aneg was never on to
571 * begin with? Or maybe phy was isolated? */
572 int ctl = phy_read(phydev, MII_BMCR);
577 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
578 result = 1; /* do restart aneg */
581 /* Only restart aneg if we are advertising something different
582 * than we were before. */
584 result = genphy_restart_aneg(phydev);
588 EXPORT_SYMBOL(genphy_config_aneg);
591 * genphy_update_link - update link status in @phydev
592 * @phydev: target phy_device struct
594 * Description: Update the value in phydev->link to reflect the
595 * current link value. In order to do this, we need to read
596 * the status register twice, keeping the second value.
598 int genphy_update_link(struct phy_device *phydev)
603 status = phy_read(phydev, MII_BMSR);
608 /* Read link and autonegotiation status */
609 status = phy_read(phydev, MII_BMSR);
614 if ((status & BMSR_LSTATUS) == 0)
621 EXPORT_SYMBOL(genphy_update_link);
624 * genphy_read_status - check the link status and update current link state
625 * @phydev: target phy_device struct
627 * Description: Check the link, then figure out the current state
628 * by comparing what we advertise with what the link partner
629 * advertises. Start by checking the gigabit possibilities,
630 * then move on to 10/100.
632 int genphy_read_status(struct phy_device *phydev)
639 /* Update the link, but return if there
641 err = genphy_update_link(phydev);
645 if (AUTONEG_ENABLE == phydev->autoneg) {
646 if (phydev->supported & (SUPPORTED_1000baseT_Half
647 | SUPPORTED_1000baseT_Full)) {
648 lpagb = phy_read(phydev, MII_STAT1000);
653 adv = phy_read(phydev, MII_CTRL1000);
661 lpa = phy_read(phydev, MII_LPA);
666 adv = phy_read(phydev, MII_ADVERTISE);
673 phydev->speed = SPEED_10;
674 phydev->duplex = DUPLEX_HALF;
675 phydev->pause = phydev->asym_pause = 0;
677 if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
678 phydev->speed = SPEED_1000;
680 if (lpagb & LPA_1000FULL)
681 phydev->duplex = DUPLEX_FULL;
682 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
683 phydev->speed = SPEED_100;
685 if (lpa & LPA_100FULL)
686 phydev->duplex = DUPLEX_FULL;
688 if (lpa & LPA_10FULL)
689 phydev->duplex = DUPLEX_FULL;
691 if (phydev->duplex == DUPLEX_FULL){
692 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
693 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
696 int bmcr = phy_read(phydev, MII_BMCR);
700 if (bmcr & BMCR_FULLDPLX)
701 phydev->duplex = DUPLEX_FULL;
703 phydev->duplex = DUPLEX_HALF;
705 if (bmcr & BMCR_SPEED1000)
706 phydev->speed = SPEED_1000;
707 else if (bmcr & BMCR_SPEED100)
708 phydev->speed = SPEED_100;
710 phydev->speed = SPEED_10;
712 phydev->pause = phydev->asym_pause = 0;
717 EXPORT_SYMBOL(genphy_read_status);
719 static int genphy_config_init(struct phy_device *phydev)
724 /* For now, I'll claim that the generic driver supports
725 * all possible port types */
726 features = (SUPPORTED_TP | SUPPORTED_MII
727 | SUPPORTED_AUI | SUPPORTED_FIBRE |
730 /* Do we support autonegotiation? */
731 val = phy_read(phydev, MII_BMSR);
736 if (val & BMSR_ANEGCAPABLE)
737 features |= SUPPORTED_Autoneg;
739 if (val & BMSR_100FULL)
740 features |= SUPPORTED_100baseT_Full;
741 if (val & BMSR_100HALF)
742 features |= SUPPORTED_100baseT_Half;
743 if (val & BMSR_10FULL)
744 features |= SUPPORTED_10baseT_Full;
745 if (val & BMSR_10HALF)
746 features |= SUPPORTED_10baseT_Half;
748 if (val & BMSR_ESTATEN) {
749 val = phy_read(phydev, MII_ESTATUS);
754 if (val & ESTATUS_1000_TFULL)
755 features |= SUPPORTED_1000baseT_Full;
756 if (val & ESTATUS_1000_THALF)
757 features |= SUPPORTED_1000baseT_Half;
760 phydev->supported = features;
761 phydev->advertising = features;
765 int genphy_suspend(struct phy_device *phydev)
769 mutex_lock(&phydev->lock);
771 value = phy_read(phydev, MII_BMCR);
772 phy_write(phydev, MII_BMCR, (value | BMCR_PDOWN));
774 mutex_unlock(&phydev->lock);
778 EXPORT_SYMBOL(genphy_suspend);
780 int genphy_resume(struct phy_device *phydev)
784 mutex_lock(&phydev->lock);
786 value = phy_read(phydev, MII_BMCR);
787 phy_write(phydev, MII_BMCR, (value & ~BMCR_PDOWN));
789 mutex_unlock(&phydev->lock);
793 EXPORT_SYMBOL(genphy_resume);
796 * phy_probe - probe and init a PHY device
797 * @dev: device to probe and init
799 * Description: Take care of setting up the phy_device structure,
800 * set the state to READY (the driver's init function should
801 * set it to STARTING if needed).
803 static int phy_probe(struct device *dev)
805 struct phy_device *phydev;
806 struct phy_driver *phydrv;
807 struct device_driver *drv;
810 phydev = to_phy_device(dev);
812 /* Make sure the driver is held.
813 * XXX -- Is this correct? */
814 drv = get_driver(phydev->dev.driver);
815 phydrv = to_phy_driver(drv);
816 phydev->drv = phydrv;
818 /* Disable the interrupt if the PHY doesn't support it */
819 if (!(phydrv->flags & PHY_HAS_INTERRUPT))
820 phydev->irq = PHY_POLL;
822 mutex_lock(&phydev->lock);
824 /* Start out supporting everything. Eventually,
825 * a controller will attach, and may modify one
826 * or both of these values */
827 phydev->supported = phydrv->features;
828 phydev->advertising = phydrv->features;
830 /* Set the state to READY by default */
831 phydev->state = PHY_READY;
833 if (phydev->drv->probe)
834 err = phydev->drv->probe(phydev);
836 mutex_unlock(&phydev->lock);
842 static int phy_remove(struct device *dev)
844 struct phy_device *phydev;
846 phydev = to_phy_device(dev);
848 mutex_lock(&phydev->lock);
849 phydev->state = PHY_DOWN;
850 mutex_unlock(&phydev->lock);
852 if (phydev->drv->remove)
853 phydev->drv->remove(phydev);
855 put_driver(dev->driver);
862 * phy_driver_register - register a phy_driver with the PHY layer
863 * @new_driver: new phy_driver to register
865 int phy_driver_register(struct phy_driver *new_driver)
869 new_driver->driver.name = new_driver->name;
870 new_driver->driver.bus = &mdio_bus_type;
871 new_driver->driver.probe = phy_probe;
872 new_driver->driver.remove = phy_remove;
874 retval = driver_register(&new_driver->driver);
877 printk(KERN_ERR "%s: Error %d in registering driver\n",
878 new_driver->name, retval);
883 pr_debug("%s: Registered new driver\n", new_driver->name);
887 EXPORT_SYMBOL(phy_driver_register);
889 void phy_driver_unregister(struct phy_driver *drv)
891 driver_unregister(&drv->driver);
893 EXPORT_SYMBOL(phy_driver_unregister);
895 static struct phy_driver genphy_driver = {
896 .phy_id = 0xffffffff,
897 .phy_id_mask = 0xffffffff,
898 .name = "Generic PHY",
899 .config_init = genphy_config_init,
901 .config_aneg = genphy_config_aneg,
902 .read_status = genphy_read_status,
903 .suspend = genphy_suspend,
904 .resume = genphy_resume,
905 .driver = {.owner= THIS_MODULE, },
908 static int __init phy_init(void)
912 rc = mdio_bus_init();
916 rc = phy_driver_register(&genphy_driver);
923 static void __exit phy_exit(void)
925 phy_driver_unregister(&genphy_driver);
929 subsys_initcall(phy_init);
930 module_exit(phy_exit);