1 /* Framework for finding and configuring PHYs.
2 * Also contains generic PHY driver
6 * Copyright (c) 2004 Freescale Semiconductor, Inc.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
33 #include <linux/phy_led_triggers.h>
34 #include <linux/mdio.h>
36 #include <linux/uaccess.h>
41 MODULE_DESCRIPTION("PHY library");
42 MODULE_AUTHOR("Andy Fleming");
43 MODULE_LICENSE("GPL");
45 void phy_device_free(struct phy_device *phydev)
47 put_device(&phydev->mdio.dev);
49 EXPORT_SYMBOL(phy_device_free);
51 static void phy_mdio_device_free(struct mdio_device *mdiodev)
53 struct phy_device *phydev;
55 phydev = container_of(mdiodev, struct phy_device, mdio);
56 phy_device_free(phydev);
59 static void phy_device_release(struct device *dev)
61 kfree(to_phy_device(dev));
64 static void phy_mdio_device_remove(struct mdio_device *mdiodev)
66 struct phy_device *phydev;
68 phydev = container_of(mdiodev, struct phy_device, mdio);
69 phy_device_remove(phydev);
72 static struct phy_driver genphy_driver;
73 extern struct phy_driver genphy_10g_driver;
75 static LIST_HEAD(phy_fixup_list);
76 static DEFINE_MUTEX(phy_fixup_lock);
79 static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
81 struct device_driver *drv = phydev->mdio.dev.driver;
82 struct phy_driver *phydrv = to_phy_driver(drv);
83 struct net_device *netdev = phydev->attached_dev;
85 if (!drv || !phydrv->suspend)
88 /* PHY not attached? May suspend if the PHY has not already been
89 * suspended as part of a prior call to phy_disconnect() ->
90 * phy_detach() -> phy_suspend() because the parent netdev might be the
91 * MDIO bus driver and clock gated at this point.
96 if (netdev->wol_enabled)
99 /* As long as not all affected network drivers support the
100 * wol_enabled flag, let's check for hints that WoL is enabled.
101 * Don't suspend PHY if the attached netdev parent may wake up.
102 * The parent may point to a PCI device, as in tg3 driver.
104 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
107 /* Also don't suspend PHY if the netdev itself may wakeup. This
108 * is the case for devices w/o underlaying pwr. mgmt. aware bus,
111 if (device_may_wakeup(&netdev->dev))
115 return !phydev->suspended;
118 static int mdio_bus_phy_suspend(struct device *dev)
120 struct phy_device *phydev = to_phy_device(dev);
122 /* We must stop the state machine manually, otherwise it stops out of
123 * control, possibly with the phydev->lock held. Upon resume, netdev
124 * may call phy routines that try to grab the same lock, and that may
125 * lead to a deadlock.
127 if (phydev->attached_dev && phydev->adjust_link)
128 phy_stop_machine(phydev);
130 if (!mdio_bus_phy_may_suspend(phydev))
133 phydev->suspended_by_mdio_bus = 1;
135 return phy_suspend(phydev);
138 static int mdio_bus_phy_resume(struct device *dev)
140 struct phy_device *phydev = to_phy_device(dev);
143 if (!phydev->suspended_by_mdio_bus)
146 phydev->suspended_by_mdio_bus = 0;
148 ret = phy_resume(phydev);
153 if (phydev->attached_dev && phydev->adjust_link)
154 phy_start_machine(phydev);
159 static int mdio_bus_phy_restore(struct device *dev)
161 struct phy_device *phydev = to_phy_device(dev);
162 struct net_device *netdev = phydev->attached_dev;
168 ret = phy_init_hw(phydev);
172 if (phydev->attached_dev && phydev->adjust_link)
173 phy_start_machine(phydev);
178 static const struct dev_pm_ops mdio_bus_phy_pm_ops = {
179 .suspend = mdio_bus_phy_suspend,
180 .resume = mdio_bus_phy_resume,
181 .freeze = mdio_bus_phy_suspend,
182 .thaw = mdio_bus_phy_resume,
183 .restore = mdio_bus_phy_restore,
186 #define MDIO_BUS_PHY_PM_OPS (&mdio_bus_phy_pm_ops)
190 #define MDIO_BUS_PHY_PM_OPS NULL
192 #endif /* CONFIG_PM */
195 * phy_register_fixup - creates a new phy_fixup and adds it to the list
196 * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID)
197 * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
198 * It can also be PHY_ANY_UID
199 * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
201 * @run: The actual code to be run when a matching PHY is found
203 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
204 int (*run)(struct phy_device *))
206 struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
211 strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
212 fixup->phy_uid = phy_uid;
213 fixup->phy_uid_mask = phy_uid_mask;
216 mutex_lock(&phy_fixup_lock);
217 list_add_tail(&fixup->list, &phy_fixup_list);
218 mutex_unlock(&phy_fixup_lock);
222 EXPORT_SYMBOL(phy_register_fixup);
224 /* Registers a fixup to be run on any PHY with the UID in phy_uid */
225 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
226 int (*run)(struct phy_device *))
228 return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
230 EXPORT_SYMBOL(phy_register_fixup_for_uid);
232 /* Registers a fixup to be run on the PHY with id string bus_id */
233 int phy_register_fixup_for_id(const char *bus_id,
234 int (*run)(struct phy_device *))
236 return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
238 EXPORT_SYMBOL(phy_register_fixup_for_id);
241 * phy_unregister_fixup - remove a phy_fixup from the list
242 * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list
243 * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list
244 * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison
246 int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask)
248 struct list_head *pos, *n;
249 struct phy_fixup *fixup;
254 mutex_lock(&phy_fixup_lock);
255 list_for_each_safe(pos, n, &phy_fixup_list) {
256 fixup = list_entry(pos, struct phy_fixup, list);
258 if ((!strcmp(fixup->bus_id, bus_id)) &&
259 ((fixup->phy_uid & phy_uid_mask) ==
260 (phy_uid & phy_uid_mask))) {
261 list_del(&fixup->list);
267 mutex_unlock(&phy_fixup_lock);
271 EXPORT_SYMBOL(phy_unregister_fixup);
273 /* Unregisters a fixup of any PHY with the UID in phy_uid */
274 int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask)
276 return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask);
278 EXPORT_SYMBOL(phy_unregister_fixup_for_uid);
280 /* Unregisters a fixup of the PHY with id string bus_id */
281 int phy_unregister_fixup_for_id(const char *bus_id)
283 return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff);
285 EXPORT_SYMBOL(phy_unregister_fixup_for_id);
287 /* Returns 1 if fixup matches phydev in bus_id and phy_uid.
288 * Fixups can be set to match any in one or more fields.
290 static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
292 if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
293 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
296 if ((fixup->phy_uid & fixup->phy_uid_mask) !=
297 (phydev->phy_id & fixup->phy_uid_mask))
298 if (fixup->phy_uid != PHY_ANY_UID)
304 /* Runs any matching fixups for this phydev */
305 static int phy_scan_fixups(struct phy_device *phydev)
307 struct phy_fixup *fixup;
309 mutex_lock(&phy_fixup_lock);
310 list_for_each_entry(fixup, &phy_fixup_list, list) {
311 if (phy_needs_fixup(phydev, fixup)) {
312 int err = fixup->run(phydev);
315 mutex_unlock(&phy_fixup_lock);
318 phydev->has_fixups = true;
321 mutex_unlock(&phy_fixup_lock);
326 static int phy_bus_match(struct device *dev, struct device_driver *drv)
328 struct phy_device *phydev = to_phy_device(dev);
329 struct phy_driver *phydrv = to_phy_driver(drv);
330 const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
333 if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
336 if (phydrv->match_phy_device)
337 return phydrv->match_phy_device(phydev);
339 if (phydev->is_c45) {
340 for (i = 1; i < num_ids; i++) {
341 if (!(phydev->c45_ids.devices_in_package & (1 << i)))
344 if ((phydrv->phy_id & phydrv->phy_id_mask) ==
345 (phydev->c45_ids.device_ids[i] &
346 phydrv->phy_id_mask))
351 return (phydrv->phy_id & phydrv->phy_id_mask) ==
352 (phydev->phy_id & phydrv->phy_id_mask);
357 phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
359 struct phy_device *phydev = to_phy_device(dev);
361 return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
363 static DEVICE_ATTR_RO(phy_id);
366 phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
368 struct phy_device *phydev = to_phy_device(dev);
369 const char *mode = NULL;
371 if (phy_is_internal(phydev))
374 mode = phy_modes(phydev->interface);
376 return sprintf(buf, "%s\n", mode);
378 static DEVICE_ATTR_RO(phy_interface);
381 phy_has_fixups_show(struct device *dev, struct device_attribute *attr,
384 struct phy_device *phydev = to_phy_device(dev);
386 return sprintf(buf, "%d\n", phydev->has_fixups);
388 static DEVICE_ATTR_RO(phy_has_fixups);
390 static struct attribute *phy_dev_attrs[] = {
391 &dev_attr_phy_id.attr,
392 &dev_attr_phy_interface.attr,
393 &dev_attr_phy_has_fixups.attr,
396 ATTRIBUTE_GROUPS(phy_dev);
398 static const struct device_type mdio_bus_phy_type = {
400 .groups = phy_dev_groups,
401 .release = phy_device_release,
402 .pm = MDIO_BUS_PHY_PM_OPS,
405 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
407 struct phy_c45_device_ids *c45_ids)
409 struct phy_device *dev;
410 struct mdio_device *mdiodev;
412 /* We allocate the device, and initialize the default values */
413 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
415 return ERR_PTR(-ENOMEM);
417 mdiodev = &dev->mdio;
418 mdiodev->dev.parent = &bus->dev;
419 mdiodev->dev.bus = &mdio_bus_type;
420 mdiodev->dev.type = &mdio_bus_phy_type;
422 mdiodev->bus_match = phy_bus_match;
423 mdiodev->addr = addr;
424 mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
425 mdiodev->device_free = phy_mdio_device_free;
426 mdiodev->device_remove = phy_mdio_device_remove;
428 dev->speed = SPEED_UNKNOWN;
429 dev->duplex = DUPLEX_UNKNOWN;
433 dev->interface = PHY_INTERFACE_MODE_GMII;
435 dev->autoneg = AUTONEG_ENABLE;
437 dev->is_c45 = is_c45;
438 dev->phy_id = phy_id;
440 dev->c45_ids = *c45_ids;
441 dev->irq = bus->irq[addr];
442 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
444 dev->state = PHY_DOWN;
446 mutex_init(&dev->lock);
447 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
448 INIT_WORK(&dev->phy_queue, phy_change_work);
450 /* Request the appropriate module unconditionally; don't
451 * bother trying to do so only if it isn't already loaded,
452 * because that gets complicated. A hotplug event would have
453 * done an unconditional modprobe anyway.
454 * We don't do normal hotplug because it won't work for MDIO
455 * -- because it relies on the device staying around for long
456 * enough for the driver to get loaded. With MDIO, the NIC
457 * driver will get bored and give up as soon as it finds that
458 * there's no driver _already_ loaded.
460 request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id));
462 device_initialize(&mdiodev->dev);
466 EXPORT_SYMBOL(phy_device_create);
468 /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
469 * @bus: the target MII bus
470 * @addr: PHY address on the MII bus
471 * @dev_addr: MMD address in the PHY.
472 * @devices_in_package: where to store the devices in package information.
474 * Description: reads devices in package registers of a MMD at @dev_addr
475 * from PHY at @addr on @bus.
477 * Returns: 0 on success, -EIO on failure.
479 static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
480 u32 *devices_in_package)
482 int phy_reg, reg_addr;
484 reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS2;
485 phy_reg = mdiobus_read(bus, addr, reg_addr);
488 *devices_in_package = (phy_reg & 0xffff) << 16;
490 reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS1;
491 phy_reg = mdiobus_read(bus, addr, reg_addr);
494 *devices_in_package |= (phy_reg & 0xffff);
500 * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
501 * @bus: the target MII bus
502 * @addr: PHY address on the MII bus
503 * @phy_id: where to store the ID retrieved.
504 * @c45_ids: where to store the c45 ID information.
506 * If the PHY devices-in-package appears to be valid, it and the
507 * corresponding identifiers are stored in @c45_ids, zero is stored
508 * in @phy_id. Otherwise 0xffffffff is stored in @phy_id. Returns
512 static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,
513 struct phy_c45_device_ids *c45_ids) {
516 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
517 u32 *devs = &c45_ids->devices_in_package;
519 /* Find first non-zero Devices In package. Device zero is reserved
520 * for 802.3 c45 complied PHYs, so don't probe it at first.
522 for (i = 1; i < num_ids && *devs == 0; i++) {
523 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, devs);
527 if ((*devs & 0x1fffffff) == 0x1fffffff) {
528 /* If mostly Fs, there is no device there,
529 * then let's continue to probe more, as some
530 * 10G PHYs have zero Devices In package,
531 * e.g. Cortina CS4315/CS4340 PHY.
533 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, devs);
536 /* no device there, let's get out of here */
537 if ((*devs & 0x1fffffff) == 0x1fffffff) {
538 *phy_id = 0xffffffff;
546 /* Now probe Device Identifiers for each device present. */
547 for (i = 1; i < num_ids; i++) {
548 if (!(c45_ids->devices_in_package & (1 << i)))
551 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1;
552 phy_reg = mdiobus_read(bus, addr, reg_addr);
555 c45_ids->device_ids[i] = (phy_reg & 0xffff) << 16;
557 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2;
558 phy_reg = mdiobus_read(bus, addr, reg_addr);
561 c45_ids->device_ids[i] |= (phy_reg & 0xffff);
568 * get_phy_id - reads the specified addr for its ID.
569 * @bus: the target MII bus
570 * @addr: PHY address on the MII bus
571 * @phy_id: where to store the ID retrieved.
572 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
573 * @c45_ids: where to store the c45 ID information.
575 * Description: In the case of a 802.3-c22 PHY, reads the ID registers
576 * of the PHY at @addr on the @bus, stores it in @phy_id and returns
579 * In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and
580 * its return value is in turn returned.
583 static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
584 bool is_c45, struct phy_c45_device_ids *c45_ids)
589 return get_phy_c45_ids(bus, addr, phy_id, c45_ids);
591 /* Grab the bits from PHYIR1, and put them in the upper half */
592 phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
594 /* if there is no device, return without an error so scanning
595 * the bus works properly
597 if (phy_reg == -EIO || phy_reg == -ENODEV) {
598 *phy_id = 0xffffffff;
605 *phy_id = (phy_reg & 0xffff) << 16;
607 /* Grab the bits from PHYIR2, and put them in the lower half */
608 phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
610 /* returning -ENODEV doesn't stop bus scanning */
611 return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
614 *phy_id |= (phy_reg & 0xffff);
620 * get_phy_device - reads the specified PHY device and returns its @phy_device
622 * @bus: the target MII bus
623 * @addr: PHY address on the MII bus
624 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
626 * Description: Reads the ID registers of the PHY at @addr on the
627 * @bus, then allocates and returns the phy_device to represent it.
629 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
631 struct phy_c45_device_ids c45_ids = {0};
635 r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids);
639 /* If the phy_id is mostly Fs, there is no device there */
640 if ((phy_id & 0x1fffffff) == 0x1fffffff)
641 return ERR_PTR(-ENODEV);
643 return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
645 EXPORT_SYMBOL(get_phy_device);
648 * phy_device_register - Register the phy device on the MDIO bus
649 * @phydev: phy_device structure to be added to the MDIO bus
651 int phy_device_register(struct phy_device *phydev)
655 err = mdiobus_register_device(&phydev->mdio);
659 /* Deassert the reset signal */
660 phy_device_reset(phydev, 0);
662 /* Run all of the fixups for this PHY */
663 err = phy_scan_fixups(phydev);
665 pr_err("PHY %d failed to initialize\n", phydev->mdio.addr);
669 err = device_add(&phydev->mdio.dev);
671 pr_err("PHY %d failed to add\n", phydev->mdio.addr);
678 /* Assert the reset signal */
679 phy_device_reset(phydev, 1);
681 mdiobus_unregister_device(&phydev->mdio);
684 EXPORT_SYMBOL(phy_device_register);
687 * phy_device_remove - Remove a previously registered phy device from the MDIO bus
688 * @phydev: phy_device structure to remove
690 * This doesn't free the phy_device itself, it merely reverses the effects
691 * of phy_device_register(). Use phy_device_free() to free the device
692 * after calling this function.
694 void phy_device_remove(struct phy_device *phydev)
696 device_del(&phydev->mdio.dev);
698 /* Assert the reset signal */
699 phy_device_reset(phydev, 1);
701 mdiobus_unregister_device(&phydev->mdio);
703 EXPORT_SYMBOL(phy_device_remove);
706 * phy_find_first - finds the first PHY device on the bus
707 * @bus: the target MII bus
709 struct phy_device *phy_find_first(struct mii_bus *bus)
711 struct phy_device *phydev;
714 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
715 phydev = mdiobus_get_phy(bus, addr);
721 EXPORT_SYMBOL(phy_find_first);
723 static void phy_link_change(struct phy_device *phydev, bool up, bool do_carrier)
725 struct net_device *netdev = phydev->attached_dev;
729 netif_carrier_on(netdev);
731 netif_carrier_off(netdev);
733 phydev->adjust_link(netdev);
737 * phy_prepare_link - prepares the PHY layer to monitor link status
738 * @phydev: target phy_device struct
739 * @handler: callback function for link status change notifications
741 * Description: Tells the PHY infrastructure to handle the
742 * gory details on monitoring link status (whether through
743 * polling or an interrupt), and to call back to the
744 * connected device driver when the link status changes.
745 * If you want to monitor your own link state, don't call
748 static void phy_prepare_link(struct phy_device *phydev,
749 void (*handler)(struct net_device *))
751 phydev->adjust_link = handler;
755 * phy_connect_direct - connect an ethernet device to a specific phy_device
756 * @dev: the network device to connect
757 * @phydev: the pointer to the phy device
758 * @handler: callback function for state change notifications
759 * @interface: PHY device's interface
761 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
762 void (*handler)(struct net_device *),
763 phy_interface_t interface)
770 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
774 phy_prepare_link(phydev, handler);
775 phy_start_machine(phydev);
777 phy_start_interrupts(phydev);
781 EXPORT_SYMBOL(phy_connect_direct);
784 * phy_connect - connect an ethernet device to a PHY device
785 * @dev: the network device to connect
786 * @bus_id: the id string of the PHY device to connect
787 * @handler: callback function for state change notifications
788 * @interface: PHY device's interface
790 * Description: Convenience function for connecting ethernet
791 * devices to PHY devices. The default behavior is for
792 * the PHY infrastructure to handle everything, and only notify
793 * the connected driver when the link status changes. If you
794 * don't want, or can't use the provided functionality, you may
795 * choose to call only the subset of functions which provide
796 * the desired functionality.
798 struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
799 void (*handler)(struct net_device *),
800 phy_interface_t interface)
802 struct phy_device *phydev;
806 /* Search the list of PHY devices on the mdio bus for the
807 * PHY with the requested name
809 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
811 pr_err("PHY %s not found\n", bus_id);
812 return ERR_PTR(-ENODEV);
814 phydev = to_phy_device(d);
816 rc = phy_connect_direct(dev, phydev, handler, interface);
823 EXPORT_SYMBOL(phy_connect);
826 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
828 * @phydev: target phy_device struct
830 void phy_disconnect(struct phy_device *phydev)
833 phy_stop_interrupts(phydev);
835 phy_stop_machine(phydev);
837 phydev->adjust_link = NULL;
841 EXPORT_SYMBOL(phy_disconnect);
844 * phy_poll_reset - Safely wait until a PHY reset has properly completed
845 * @phydev: The PHY device to poll
847 * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
848 * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR
849 * register must be polled until the BMCR_RESET bit clears.
851 * Furthermore, any attempts to write to PHY registers may have no effect
852 * or even generate MDIO bus errors until this is complete.
854 * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
855 * standard and do not fully reset after the BMCR_RESET bit is set, and may
856 * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an
857 * effort to support such broken PHYs, this function is separate from the
858 * standard phy_init_hw() which will zero all the other bits in the BMCR
859 * and reapply all driver-specific and board-specific fixups.
861 static int phy_poll_reset(struct phy_device *phydev)
863 /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
864 unsigned int retries = 12;
869 ret = phy_read(phydev, MII_BMCR);
872 } while (ret & BMCR_RESET && --retries);
873 if (ret & BMCR_RESET)
876 /* Some chips (smsc911x) may still need up to another 1ms after the
877 * BMCR_RESET bit is cleared before they are usable.
883 int phy_init_hw(struct phy_device *phydev)
887 /* Deassert the reset signal */
888 phy_device_reset(phydev, 0);
890 if (!phydev->drv || !phydev->drv->config_init)
893 if (phydev->drv->soft_reset)
894 ret = phydev->drv->soft_reset(phydev);
896 ret = genphy_soft_reset(phydev);
901 ret = phy_scan_fixups(phydev);
905 return phydev->drv->config_init(phydev);
907 EXPORT_SYMBOL(phy_init_hw);
909 void phy_attached_info(struct phy_device *phydev)
911 phy_attached_print(phydev, NULL);
913 EXPORT_SYMBOL(phy_attached_info);
915 #define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%s)"
916 void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
918 const char *drv_name = phydev->drv ? phydev->drv->name : "unbound";
922 switch(phydev->irq) {
926 case PHY_IGNORE_INTERRUPT:
930 snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
937 dev_info(&phydev->mdio.dev, ATTACHED_FMT "\n",
938 drv_name, phydev_name(phydev),
943 dev_info(&phydev->mdio.dev, ATTACHED_FMT,
944 drv_name, phydev_name(phydev),
952 EXPORT_SYMBOL(phy_attached_print);
955 * phy_attach_direct - attach a network device to a given PHY device pointer
956 * @dev: network device to attach
957 * @phydev: Pointer to phy_device to attach
958 * @flags: PHY device's dev_flags
959 * @interface: PHY device's interface
961 * Description: Called by drivers to attach to a particular PHY
962 * device. The phy_device is found, and properly hooked up
963 * to the phy_driver. If no driver is attached, then a
964 * generic driver is used. The phy_device is given a ptr to
965 * the attaching device, and given a callback for link status
966 * change. The phy_device is returned to the attaching driver.
967 * This function takes a reference on the phy device.
969 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
970 u32 flags, phy_interface_t interface)
972 struct module *ndev_owner = dev->dev.parent->driver->owner;
973 struct mii_bus *bus = phydev->mdio.bus;
974 struct device *d = &phydev->mdio.dev;
975 bool using_genphy = false;
978 /* For Ethernet device drivers that register their own MDIO bus, we
979 * will have bus->owner match ndev_mod, so we do not want to increment
980 * our own module->refcnt here, otherwise we would not be able to
983 if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
984 dev_err(&dev->dev, "failed to get the bus module\n");
990 /* Assume that if there is no driver, that it doesn't
991 * exist, and we should use the genphy driver.
995 d->driver = &genphy_10g_driver.mdiodrv.driver;
997 d->driver = &genphy_driver.mdiodrv.driver;
1002 if (!try_module_get(d->driver->owner)) {
1003 dev_err(&dev->dev, "failed to get the device driver module\n");
1005 goto error_put_device;
1009 err = d->driver->probe(d);
1011 err = device_bind_driver(d);
1014 goto error_module_put;
1017 if (phydev->attached_dev) {
1018 dev_err(&dev->dev, "PHY already attached\n");
1023 phydev->phy_link_change = phy_link_change;
1024 phydev->attached_dev = dev;
1025 dev->phydev = phydev;
1027 /* Some Ethernet drivers try to connect to a PHY device before
1028 * calling register_netdevice() -> netdev_register_kobject() and
1029 * does the dev->dev.kobj initialization. Here we only check for
1030 * success which indicates that the network device kobject is
1031 * ready. Once we do that we still need to keep track of whether
1032 * links were successfully set up or not for phy_detach() to
1033 * remove them accordingly.
1035 phydev->sysfs_links = false;
1037 err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
1040 err = sysfs_create_link_nowarn(&dev->dev.kobj,
1041 &phydev->mdio.dev.kobj,
1044 dev_err(&dev->dev, "could not add device link to %s err %d\n",
1045 kobject_name(&phydev->mdio.dev.kobj),
1047 /* non-fatal - some net drivers can use one netdevice
1048 * with more then one phy
1052 phydev->sysfs_links = true;
1055 phydev->dev_flags = flags;
1057 phydev->interface = interface;
1059 phydev->state = PHY_READY;
1061 /* Initial carrier state is off as the phy is about to be
1064 netif_carrier_off(phydev->attached_dev);
1066 /* Do initial configuration here, now that
1067 * we have certain key parameters
1068 * (dev_flags and interface)
1070 err = phy_init_hw(phydev);
1075 phy_led_triggers_register(phydev);
1080 /* phy_detach() does all of the cleanup below */
1085 module_put(d->driver->owner);
1089 if (ndev_owner != bus->owner)
1090 module_put(bus->owner);
1093 EXPORT_SYMBOL(phy_attach_direct);
1096 * phy_attach - attach a network device to a particular PHY device
1097 * @dev: network device to attach
1098 * @bus_id: Bus ID of PHY device to attach
1099 * @interface: PHY device's interface
1101 * Description: Same as phy_attach_direct() except that a PHY bus_id
1102 * string is passed instead of a pointer to a struct phy_device.
1104 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1105 phy_interface_t interface)
1107 struct bus_type *bus = &mdio_bus_type;
1108 struct phy_device *phydev;
1113 return ERR_PTR(-EINVAL);
1115 /* Search the list of PHY devices on the mdio bus for the
1116 * PHY with the requested name
1118 d = bus_find_device_by_name(bus, NULL, bus_id);
1120 pr_err("PHY %s not found\n", bus_id);
1121 return ERR_PTR(-ENODEV);
1123 phydev = to_phy_device(d);
1125 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
1132 EXPORT_SYMBOL(phy_attach);
1135 * phy_detach - detach a PHY device from its network device
1136 * @phydev: target phy_device struct
1138 * This detaches the phy device from its network device and the phy
1139 * driver, and drops the reference count taken in phy_attach_direct().
1141 void phy_detach(struct phy_device *phydev)
1143 struct net_device *dev = phydev->attached_dev;
1144 struct module *ndev_owner = dev->dev.parent->driver->owner;
1145 struct mii_bus *bus;
1147 if (phydev->sysfs_links) {
1148 sysfs_remove_link(&dev->dev.kobj, "phydev");
1149 sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
1151 phy_suspend(phydev);
1152 phydev->attached_dev->phydev = NULL;
1153 phydev->attached_dev = NULL;
1154 phydev->phylink = NULL;
1156 phy_led_triggers_unregister(phydev);
1158 if (phydev->mdio.dev.driver)
1159 module_put(phydev->mdio.dev.driver->owner);
1161 /* If the device had no specific driver before (i.e. - it
1162 * was using the generic driver), we unbind the device
1163 * from the generic driver so that there's a chance a
1164 * real driver could be loaded
1166 if (phydev->mdio.dev.driver == &genphy_10g_driver.mdiodrv.driver ||
1167 phydev->mdio.dev.driver == &genphy_driver.mdiodrv.driver)
1168 device_release_driver(&phydev->mdio.dev);
1170 /* Assert the reset signal */
1171 phy_device_reset(phydev, 1);
1174 * The phydev might go away on the put_device() below, so avoid
1175 * a use-after-free bug by reading the underlying bus first.
1177 bus = phydev->mdio.bus;
1179 put_device(&phydev->mdio.dev);
1180 if (ndev_owner != bus->owner)
1181 module_put(bus->owner);
1183 EXPORT_SYMBOL(phy_detach);
1185 int phy_suspend(struct phy_device *phydev)
1187 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1188 struct net_device *netdev = phydev->attached_dev;
1189 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1192 /* If the device has WOL enabled, we cannot suspend the PHY */
1193 phy_ethtool_get_wol(phydev, &wol);
1194 if (wol.wolopts || (netdev && netdev->wol_enabled))
1197 if (phydev->drv && phydrv->suspend)
1198 ret = phydrv->suspend(phydev);
1203 phydev->suspended = true;
1207 EXPORT_SYMBOL(phy_suspend);
1209 int __phy_resume(struct phy_device *phydev)
1211 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1214 WARN_ON(!mutex_is_locked(&phydev->lock));
1216 if (phydev->drv && phydrv->resume)
1217 ret = phydrv->resume(phydev);
1222 phydev->suspended = false;
1226 EXPORT_SYMBOL(__phy_resume);
1228 int phy_resume(struct phy_device *phydev)
1232 mutex_lock(&phydev->lock);
1233 ret = __phy_resume(phydev);
1234 mutex_unlock(&phydev->lock);
1238 EXPORT_SYMBOL(phy_resume);
1240 int phy_loopback(struct phy_device *phydev, bool enable)
1242 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1245 mutex_lock(&phydev->lock);
1247 if (enable && phydev->loopback_enabled) {
1252 if (!enable && !phydev->loopback_enabled) {
1257 if (phydev->drv && phydrv->set_loopback)
1258 ret = phydrv->set_loopback(phydev, enable);
1265 phydev->loopback_enabled = enable;
1268 mutex_unlock(&phydev->lock);
1271 EXPORT_SYMBOL(phy_loopback);
1274 * phy_reset_after_clk_enable - perform a PHY reset if needed
1275 * @phydev: target phy_device struct
1277 * Description: Some PHYs are known to need a reset after their refclk was
1278 * enabled. This function evaluates the flags and perform the reset if it's
1279 * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy
1282 int phy_reset_after_clk_enable(struct phy_device *phydev)
1284 if (!phydev || !phydev->drv)
1287 if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) {
1288 phy_device_reset(phydev, 1);
1289 phy_device_reset(phydev, 0);
1295 EXPORT_SYMBOL(phy_reset_after_clk_enable);
1297 /* Generic PHY support and helper functions */
1300 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
1301 * @phydev: target phy_device struct
1303 * Description: Writes MII_ADVERTISE with the appropriate values,
1304 * after sanitizing the values to make sure we only advertise
1305 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
1306 * hasn't changed, and > 0 if it has changed.
1308 static int genphy_config_advert(struct phy_device *phydev)
1311 int oldadv, adv, bmsr;
1312 int err, changed = 0;
1314 /* Only allow advertising what this PHY supports */
1315 phydev->advertising &= phydev->supported;
1316 advertise = phydev->advertising;
1318 /* Setup standard advertisement */
1319 adv = phy_read(phydev, MII_ADVERTISE);
1324 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
1325 ADVERTISE_PAUSE_ASYM);
1326 adv |= ethtool_adv_to_mii_adv_t(advertise);
1328 if (adv != oldadv) {
1329 err = phy_write(phydev, MII_ADVERTISE, adv);
1336 bmsr = phy_read(phydev, MII_BMSR);
1340 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
1341 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
1344 if (!(bmsr & BMSR_ESTATEN))
1347 /* Configure gigabit if it's supported */
1348 adv = phy_read(phydev, MII_CTRL1000);
1353 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1355 if (phydev->supported & (SUPPORTED_1000baseT_Half |
1356 SUPPORTED_1000baseT_Full)) {
1357 adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
1363 err = phy_write(phydev, MII_CTRL1000, adv);
1371 * genphy_config_eee_advert - disable unwanted eee mode advertisement
1372 * @phydev: target phy_device struct
1374 * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy
1375 * efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't
1376 * changed, and 1 if it has changed.
1378 static int genphy_config_eee_advert(struct phy_device *phydev)
1380 int broken = phydev->eee_broken_modes;
1383 /* Nothing to disable */
1387 /* If the following call fails, we assume that EEE is not
1388 * supported by the phy. If we read 0, EEE is not advertised
1389 * In both case, we don't need to continue
1391 adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1398 /* Advertising remains unchanged with the broken mask */
1402 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
1408 * genphy_setup_forced - configures/forces speed/duplex from @phydev
1409 * @phydev: target phy_device struct
1411 * Description: Configures MII_BMCR to force speed/duplex
1412 * to the values in phydev. Assumes that the values are valid.
1413 * Please see phy_sanitize_settings().
1415 int genphy_setup_forced(struct phy_device *phydev)
1420 phydev->asym_pause = 0;
1422 if (SPEED_1000 == phydev->speed)
1423 ctl |= BMCR_SPEED1000;
1424 else if (SPEED_100 == phydev->speed)
1425 ctl |= BMCR_SPEED100;
1427 if (DUPLEX_FULL == phydev->duplex)
1428 ctl |= BMCR_FULLDPLX;
1430 return phy_modify(phydev, MII_BMCR,
1431 ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
1433 EXPORT_SYMBOL(genphy_setup_forced);
1436 * genphy_restart_aneg - Enable and Restart Autonegotiation
1437 * @phydev: target phy_device struct
1439 int genphy_restart_aneg(struct phy_device *phydev)
1441 /* Don't isolate the PHY if we're negotiating */
1442 return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
1443 BMCR_ANENABLE | BMCR_ANRESTART);
1445 EXPORT_SYMBOL(genphy_restart_aneg);
1448 * genphy_config_aneg - restart auto-negotiation or write BMCR
1449 * @phydev: target phy_device struct
1451 * Description: If auto-negotiation is enabled, we configure the
1452 * advertising, and then restart auto-negotiation. If it is not
1453 * enabled, then we write the BMCR.
1455 int genphy_config_aneg(struct phy_device *phydev)
1459 changed = genphy_config_eee_advert(phydev);
1461 if (AUTONEG_ENABLE != phydev->autoneg)
1462 return genphy_setup_forced(phydev);
1464 err = genphy_config_advert(phydev);
1465 if (err < 0) /* error */
1471 /* Advertisement hasn't changed, but maybe aneg was never on to
1472 * begin with? Or maybe phy was isolated?
1474 int ctl = phy_read(phydev, MII_BMCR);
1479 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
1480 changed = 1; /* do restart aneg */
1483 /* Only restart aneg if we are advertising something different
1484 * than we were before.
1487 return genphy_restart_aneg(phydev);
1491 EXPORT_SYMBOL(genphy_config_aneg);
1494 * genphy_aneg_done - return auto-negotiation status
1495 * @phydev: target phy_device struct
1497 * Description: Reads the status register and returns 0 either if
1498 * auto-negotiation is incomplete, or if there was an error.
1499 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
1501 int genphy_aneg_done(struct phy_device *phydev)
1503 int retval = phy_read(phydev, MII_BMSR);
1505 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1507 EXPORT_SYMBOL(genphy_aneg_done);
1510 * genphy_update_link - update link status in @phydev
1511 * @phydev: target phy_device struct
1513 * Description: Update the value in phydev->link to reflect the
1514 * current link value. In order to do this, we need to read
1515 * the status register twice, keeping the second value.
1517 int genphy_update_link(struct phy_device *phydev)
1521 /* The link state is latched low so that momentary link
1522 * drops can be detected. Do not double-read the status
1523 * in polling mode to detect such short link drops.
1525 if (!phy_polling_mode(phydev)) {
1526 status = phy_read(phydev, MII_BMSR);
1531 /* Read link and autonegotiation status */
1532 status = phy_read(phydev, MII_BMSR);
1536 if ((status & BMSR_LSTATUS) == 0)
1543 EXPORT_SYMBOL(genphy_update_link);
1546 * genphy_read_status - check the link status and update current link state
1547 * @phydev: target phy_device struct
1549 * Description: Check the link, then figure out the current state
1550 * by comparing what we advertise with what the link partner
1551 * advertises. Start by checking the gigabit possibilities,
1552 * then move on to 10/100.
1554 int genphy_read_status(struct phy_device *phydev)
1561 int common_adv_gb = 0;
1563 /* Update the link, but return if there was an error */
1564 err = genphy_update_link(phydev);
1568 phydev->lp_advertising = 0;
1570 if (AUTONEG_ENABLE == phydev->autoneg) {
1571 if (phydev->supported & (SUPPORTED_1000baseT_Half
1572 | SUPPORTED_1000baseT_Full)) {
1573 lpagb = phy_read(phydev, MII_STAT1000);
1577 adv = phy_read(phydev, MII_CTRL1000);
1581 if (lpagb & LPA_1000MSFAIL) {
1582 if (adv & CTL1000_ENABLE_MASTER)
1583 phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
1585 phydev_err(phydev, "Master/Slave resolution failed\n");
1589 phydev->lp_advertising =
1590 mii_stat1000_to_ethtool_lpa_t(lpagb);
1591 common_adv_gb = lpagb & adv << 2;
1594 lpa = phy_read(phydev, MII_LPA);
1598 phydev->lp_advertising |= mii_lpa_to_ethtool_lpa_t(lpa);
1600 adv = phy_read(phydev, MII_ADVERTISE);
1604 common_adv = lpa & adv;
1606 phydev->speed = SPEED_10;
1607 phydev->duplex = DUPLEX_HALF;
1609 phydev->asym_pause = 0;
1611 if (common_adv_gb & (LPA_1000FULL | LPA_1000HALF)) {
1612 phydev->speed = SPEED_1000;
1614 if (common_adv_gb & LPA_1000FULL)
1615 phydev->duplex = DUPLEX_FULL;
1616 } else if (common_adv & (LPA_100FULL | LPA_100HALF)) {
1617 phydev->speed = SPEED_100;
1619 if (common_adv & LPA_100FULL)
1620 phydev->duplex = DUPLEX_FULL;
1622 if (common_adv & LPA_10FULL)
1623 phydev->duplex = DUPLEX_FULL;
1625 if (phydev->duplex == DUPLEX_FULL) {
1626 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
1627 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
1630 int bmcr = phy_read(phydev, MII_BMCR);
1635 if (bmcr & BMCR_FULLDPLX)
1636 phydev->duplex = DUPLEX_FULL;
1638 phydev->duplex = DUPLEX_HALF;
1640 if (bmcr & BMCR_SPEED1000)
1641 phydev->speed = SPEED_1000;
1642 else if (bmcr & BMCR_SPEED100)
1643 phydev->speed = SPEED_100;
1645 phydev->speed = SPEED_10;
1648 phydev->asym_pause = 0;
1653 EXPORT_SYMBOL(genphy_read_status);
1656 * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
1657 * @phydev: target phy_device struct
1659 * Description: Perform a software PHY reset using the standard
1660 * BMCR_RESET bit and poll for the reset bit to be cleared.
1662 * Returns: 0 on success, < 0 on failure
1664 int genphy_soft_reset(struct phy_device *phydev)
1668 ret = phy_set_bits(phydev, MII_BMCR, BMCR_RESET);
1672 return phy_poll_reset(phydev);
1674 EXPORT_SYMBOL(genphy_soft_reset);
1676 int genphy_config_init(struct phy_device *phydev)
1681 features = (SUPPORTED_TP | SUPPORTED_MII
1682 | SUPPORTED_AUI | SUPPORTED_FIBRE |
1683 SUPPORTED_BNC | SUPPORTED_Pause | SUPPORTED_Asym_Pause);
1685 /* Do we support autonegotiation? */
1686 val = phy_read(phydev, MII_BMSR);
1690 if (val & BMSR_ANEGCAPABLE)
1691 features |= SUPPORTED_Autoneg;
1693 if (val & BMSR_100FULL)
1694 features |= SUPPORTED_100baseT_Full;
1695 if (val & BMSR_100HALF)
1696 features |= SUPPORTED_100baseT_Half;
1697 if (val & BMSR_10FULL)
1698 features |= SUPPORTED_10baseT_Full;
1699 if (val & BMSR_10HALF)
1700 features |= SUPPORTED_10baseT_Half;
1702 if (val & BMSR_ESTATEN) {
1703 val = phy_read(phydev, MII_ESTATUS);
1707 if (val & ESTATUS_1000_TFULL)
1708 features |= SUPPORTED_1000baseT_Full;
1709 if (val & ESTATUS_1000_THALF)
1710 features |= SUPPORTED_1000baseT_Half;
1713 phydev->supported &= features;
1714 phydev->advertising &= features;
1718 EXPORT_SYMBOL(genphy_config_init);
1720 /* This is used for the phy device which doesn't support the MMD extended
1721 * register access, but it does have side effect when we are trying to access
1722 * the MMD register via indirect method.
1724 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum)
1728 EXPORT_SYMBOL(genphy_read_mmd_unsupported);
1730 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
1731 u16 regnum, u16 val)
1735 EXPORT_SYMBOL(genphy_write_mmd_unsupported);
1737 int genphy_suspend(struct phy_device *phydev)
1739 return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN);
1741 EXPORT_SYMBOL(genphy_suspend);
1743 int genphy_resume(struct phy_device *phydev)
1745 return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN);
1747 EXPORT_SYMBOL(genphy_resume);
1749 int genphy_loopback(struct phy_device *phydev, bool enable)
1751 return phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK,
1752 enable ? BMCR_LOOPBACK : 0);
1754 EXPORT_SYMBOL(genphy_loopback);
1756 static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
1758 switch (max_speed) {
1760 phydev->supported &= ~PHY_100BT_FEATURES;
1763 phydev->supported &= ~PHY_1000BT_FEATURES;
1774 int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
1778 err = __set_phy_supported(phydev, max_speed);
1782 phydev->advertising = phydev->supported;
1786 EXPORT_SYMBOL(phy_set_max_speed);
1788 static void of_set_phy_supported(struct phy_device *phydev)
1790 struct device_node *node = phydev->mdio.dev.of_node;
1793 if (!IS_ENABLED(CONFIG_OF_MDIO))
1799 if (!of_property_read_u32(node, "max-speed", &max_speed))
1800 __set_phy_supported(phydev, max_speed);
1803 static void of_set_phy_eee_broken(struct phy_device *phydev)
1805 struct device_node *node = phydev->mdio.dev.of_node;
1808 if (!IS_ENABLED(CONFIG_OF_MDIO))
1814 if (of_property_read_bool(node, "eee-broken-100tx"))
1815 broken |= MDIO_EEE_100TX;
1816 if (of_property_read_bool(node, "eee-broken-1000t"))
1817 broken |= MDIO_EEE_1000T;
1818 if (of_property_read_bool(node, "eee-broken-10gt"))
1819 broken |= MDIO_EEE_10GT;
1820 if (of_property_read_bool(node, "eee-broken-1000kx"))
1821 broken |= MDIO_EEE_1000KX;
1822 if (of_property_read_bool(node, "eee-broken-10gkx4"))
1823 broken |= MDIO_EEE_10GKX4;
1824 if (of_property_read_bool(node, "eee-broken-10gkr"))
1825 broken |= MDIO_EEE_10GKR;
1827 phydev->eee_broken_modes = broken;
1831 * phy_probe - probe and init a PHY device
1832 * @dev: device to probe and init
1834 * Description: Take care of setting up the phy_device structure,
1835 * set the state to READY (the driver's init function should
1836 * set it to STARTING if needed).
1838 static int phy_probe(struct device *dev)
1840 struct phy_device *phydev = to_phy_device(dev);
1841 struct device_driver *drv = phydev->mdio.dev.driver;
1842 struct phy_driver *phydrv = to_phy_driver(drv);
1845 phydev->drv = phydrv;
1847 /* Disable the interrupt if the PHY doesn't support it
1848 * but the interrupt is still a valid one
1850 if (!(phydrv->flags & PHY_HAS_INTERRUPT) &&
1851 phy_interrupt_is_valid(phydev))
1852 phydev->irq = PHY_POLL;
1854 if (phydrv->flags & PHY_IS_INTERNAL)
1855 phydev->is_internal = true;
1857 mutex_lock(&phydev->lock);
1859 /* Start out supporting everything. Eventually,
1860 * a controller will attach, and may modify one
1861 * or both of these values
1863 phydev->supported = phydrv->features;
1864 of_set_phy_supported(phydev);
1865 phydev->advertising = phydev->supported;
1867 /* Get the EEE modes we want to prohibit. We will ask
1868 * the PHY stop advertising these mode later on
1870 of_set_phy_eee_broken(phydev);
1872 /* The Pause Frame bits indicate that the PHY can support passing
1873 * pause frames. During autonegotiation, the PHYs will determine if
1874 * they should allow pause frames to pass. The MAC driver should then
1875 * use that result to determine whether to enable flow control via
1878 * Normally, PHY drivers should not set the Pause bits, and instead
1879 * allow phylib to do that. However, there may be some situations
1880 * (e.g. hardware erratum) where the driver wants to set only one
1883 if (phydrv->features & (SUPPORTED_Pause | SUPPORTED_Asym_Pause)) {
1884 phydev->supported &= ~(SUPPORTED_Pause | SUPPORTED_Asym_Pause);
1885 phydev->supported |= phydrv->features &
1886 (SUPPORTED_Pause | SUPPORTED_Asym_Pause);
1888 phydev->supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
1891 /* Set the state to READY by default */
1892 phydev->state = PHY_READY;
1894 if (phydev->drv->probe) {
1895 /* Deassert the reset signal */
1896 phy_device_reset(phydev, 0);
1898 err = phydev->drv->probe(phydev);
1900 /* Assert the reset signal */
1901 phy_device_reset(phydev, 1);
1905 mutex_unlock(&phydev->lock);
1910 static int phy_remove(struct device *dev)
1912 struct phy_device *phydev = to_phy_device(dev);
1914 cancel_delayed_work_sync(&phydev->state_queue);
1916 mutex_lock(&phydev->lock);
1917 phydev->state = PHY_DOWN;
1918 mutex_unlock(&phydev->lock);
1920 if (phydev->drv && phydev->drv->remove) {
1921 phydev->drv->remove(phydev);
1923 /* Assert the reset signal */
1924 phy_device_reset(phydev, 1);
1932 * phy_driver_register - register a phy_driver with the PHY layer
1933 * @new_driver: new phy_driver to register
1934 * @owner: module owning this PHY
1936 int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
1940 new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
1941 new_driver->mdiodrv.driver.name = new_driver->name;
1942 new_driver->mdiodrv.driver.bus = &mdio_bus_type;
1943 new_driver->mdiodrv.driver.probe = phy_probe;
1944 new_driver->mdiodrv.driver.remove = phy_remove;
1945 new_driver->mdiodrv.driver.owner = owner;
1947 /* The following works around an issue where the PHY driver doesn't bind
1948 * to the device, resulting in the genphy driver being used instead of
1949 * the dedicated driver. The root cause of the issue isn't known yet
1950 * and seems to be in the base driver core. Once this is fixed we may
1951 * remove this workaround.
1953 new_driver->mdiodrv.driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
1955 retval = driver_register(&new_driver->mdiodrv.driver);
1957 pr_err("%s: Error %d in registering driver\n",
1958 new_driver->name, retval);
1963 pr_debug("%s: Registered new driver\n", new_driver->name);
1967 EXPORT_SYMBOL(phy_driver_register);
1969 int phy_drivers_register(struct phy_driver *new_driver, int n,
1970 struct module *owner)
1974 for (i = 0; i < n; i++) {
1975 ret = phy_driver_register(new_driver + i, owner);
1978 phy_driver_unregister(new_driver + i);
1984 EXPORT_SYMBOL(phy_drivers_register);
1986 void phy_driver_unregister(struct phy_driver *drv)
1988 driver_unregister(&drv->mdiodrv.driver);
1990 EXPORT_SYMBOL(phy_driver_unregister);
1992 void phy_drivers_unregister(struct phy_driver *drv, int n)
1996 for (i = 0; i < n; i++)
1997 phy_driver_unregister(drv + i);
1999 EXPORT_SYMBOL(phy_drivers_unregister);
2001 static struct phy_driver genphy_driver = {
2002 .phy_id = 0xffffffff,
2003 .phy_id_mask = 0xffffffff,
2004 .name = "Generic PHY",
2005 .soft_reset = genphy_no_soft_reset,
2006 .config_init = genphy_config_init,
2007 .features = PHY_GBIT_FEATURES | SUPPORTED_MII |
2008 SUPPORTED_AUI | SUPPORTED_FIBRE |
2010 .aneg_done = genphy_aneg_done,
2011 .suspend = genphy_suspend,
2012 .resume = genphy_resume,
2013 .set_loopback = genphy_loopback,
2016 static int __init phy_init(void)
2020 rc = mdio_bus_init();
2024 rc = phy_driver_register(&genphy_10g_driver, THIS_MODULE);
2028 rc = phy_driver_register(&genphy_driver, THIS_MODULE);
2030 phy_driver_unregister(&genphy_10g_driver);
2038 static void __exit phy_exit(void)
2040 phy_driver_unregister(&genphy_10g_driver);
2041 phy_driver_unregister(&genphy_driver);
2045 subsys_initcall(phy_init);
2046 module_exit(phy_exit);