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);
1088 if (ndev_owner != bus->owner)
1089 module_put(bus->owner);
1092 EXPORT_SYMBOL(phy_attach_direct);
1095 * phy_attach - attach a network device to a particular PHY device
1096 * @dev: network device to attach
1097 * @bus_id: Bus ID of PHY device to attach
1098 * @interface: PHY device's interface
1100 * Description: Same as phy_attach_direct() except that a PHY bus_id
1101 * string is passed instead of a pointer to a struct phy_device.
1103 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1104 phy_interface_t interface)
1106 struct bus_type *bus = &mdio_bus_type;
1107 struct phy_device *phydev;
1112 return ERR_PTR(-EINVAL);
1114 /* Search the list of PHY devices on the mdio bus for the
1115 * PHY with the requested name
1117 d = bus_find_device_by_name(bus, NULL, bus_id);
1119 pr_err("PHY %s not found\n", bus_id);
1120 return ERR_PTR(-ENODEV);
1122 phydev = to_phy_device(d);
1124 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
1131 EXPORT_SYMBOL(phy_attach);
1134 * phy_detach - detach a PHY device from its network device
1135 * @phydev: target phy_device struct
1137 * This detaches the phy device from its network device and the phy
1138 * driver, and drops the reference count taken in phy_attach_direct().
1140 void phy_detach(struct phy_device *phydev)
1142 struct net_device *dev = phydev->attached_dev;
1143 struct module *ndev_owner = dev->dev.parent->driver->owner;
1144 struct mii_bus *bus;
1146 if (phydev->sysfs_links) {
1147 sysfs_remove_link(&dev->dev.kobj, "phydev");
1148 sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
1150 phy_suspend(phydev);
1151 phydev->attached_dev->phydev = NULL;
1152 phydev->attached_dev = NULL;
1153 phydev->phylink = NULL;
1155 phy_led_triggers_unregister(phydev);
1157 if (phydev->mdio.dev.driver)
1158 module_put(phydev->mdio.dev.driver->owner);
1160 /* If the device had no specific driver before (i.e. - it
1161 * was using the generic driver), we unbind the device
1162 * from the generic driver so that there's a chance a
1163 * real driver could be loaded
1165 if (phydev->mdio.dev.driver == &genphy_10g_driver.mdiodrv.driver ||
1166 phydev->mdio.dev.driver == &genphy_driver.mdiodrv.driver)
1167 device_release_driver(&phydev->mdio.dev);
1169 /* Assert the reset signal */
1170 phy_device_reset(phydev, 1);
1173 * The phydev might go away on the put_device() below, so avoid
1174 * a use-after-free bug by reading the underlying bus first.
1176 bus = phydev->mdio.bus;
1178 put_device(&phydev->mdio.dev);
1179 if (ndev_owner != bus->owner)
1180 module_put(bus->owner);
1182 EXPORT_SYMBOL(phy_detach);
1184 int phy_suspend(struct phy_device *phydev)
1186 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1187 struct net_device *netdev = phydev->attached_dev;
1188 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1191 /* If the device has WOL enabled, we cannot suspend the PHY */
1192 phy_ethtool_get_wol(phydev, &wol);
1193 if (wol.wolopts || (netdev && netdev->wol_enabled))
1196 if (phydev->drv && phydrv->suspend)
1197 ret = phydrv->suspend(phydev);
1202 phydev->suspended = true;
1206 EXPORT_SYMBOL(phy_suspend);
1208 int __phy_resume(struct phy_device *phydev)
1210 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1213 WARN_ON(!mutex_is_locked(&phydev->lock));
1215 if (phydev->drv && phydrv->resume)
1216 ret = phydrv->resume(phydev);
1221 phydev->suspended = false;
1225 EXPORT_SYMBOL(__phy_resume);
1227 int phy_resume(struct phy_device *phydev)
1231 mutex_lock(&phydev->lock);
1232 ret = __phy_resume(phydev);
1233 mutex_unlock(&phydev->lock);
1237 EXPORT_SYMBOL(phy_resume);
1239 int phy_loopback(struct phy_device *phydev, bool enable)
1241 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1244 mutex_lock(&phydev->lock);
1246 if (enable && phydev->loopback_enabled) {
1251 if (!enable && !phydev->loopback_enabled) {
1256 if (phydev->drv && phydrv->set_loopback)
1257 ret = phydrv->set_loopback(phydev, enable);
1264 phydev->loopback_enabled = enable;
1267 mutex_unlock(&phydev->lock);
1270 EXPORT_SYMBOL(phy_loopback);
1273 * phy_reset_after_clk_enable - perform a PHY reset if needed
1274 * @phydev: target phy_device struct
1276 * Description: Some PHYs are known to need a reset after their refclk was
1277 * enabled. This function evaluates the flags and perform the reset if it's
1278 * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy
1281 int phy_reset_after_clk_enable(struct phy_device *phydev)
1283 if (!phydev || !phydev->drv)
1286 if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) {
1287 phy_device_reset(phydev, 1);
1288 phy_device_reset(phydev, 0);
1294 EXPORT_SYMBOL(phy_reset_after_clk_enable);
1296 /* Generic PHY support and helper functions */
1299 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
1300 * @phydev: target phy_device struct
1302 * Description: Writes MII_ADVERTISE with the appropriate values,
1303 * after sanitizing the values to make sure we only advertise
1304 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
1305 * hasn't changed, and > 0 if it has changed.
1307 static int genphy_config_advert(struct phy_device *phydev)
1310 int oldadv, adv, bmsr;
1311 int err, changed = 0;
1313 /* Only allow advertising what this PHY supports */
1314 phydev->advertising &= phydev->supported;
1315 advertise = phydev->advertising;
1317 /* Setup standard advertisement */
1318 adv = phy_read(phydev, MII_ADVERTISE);
1323 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
1324 ADVERTISE_PAUSE_ASYM);
1325 adv |= ethtool_adv_to_mii_adv_t(advertise);
1327 if (adv != oldadv) {
1328 err = phy_write(phydev, MII_ADVERTISE, adv);
1335 bmsr = phy_read(phydev, MII_BMSR);
1339 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
1340 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
1343 if (!(bmsr & BMSR_ESTATEN))
1346 /* Configure gigabit if it's supported */
1347 adv = phy_read(phydev, MII_CTRL1000);
1352 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1354 if (phydev->supported & (SUPPORTED_1000baseT_Half |
1355 SUPPORTED_1000baseT_Full)) {
1356 adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
1362 err = phy_write(phydev, MII_CTRL1000, adv);
1370 * genphy_config_eee_advert - disable unwanted eee mode advertisement
1371 * @phydev: target phy_device struct
1373 * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy
1374 * efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't
1375 * changed, and 1 if it has changed.
1377 static int genphy_config_eee_advert(struct phy_device *phydev)
1379 int broken = phydev->eee_broken_modes;
1382 /* Nothing to disable */
1386 /* If the following call fails, we assume that EEE is not
1387 * supported by the phy. If we read 0, EEE is not advertised
1388 * In both case, we don't need to continue
1390 adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1397 /* Advertising remains unchanged with the broken mask */
1401 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
1407 * genphy_setup_forced - configures/forces speed/duplex from @phydev
1408 * @phydev: target phy_device struct
1410 * Description: Configures MII_BMCR to force speed/duplex
1411 * to the values in phydev. Assumes that the values are valid.
1412 * Please see phy_sanitize_settings().
1414 int genphy_setup_forced(struct phy_device *phydev)
1419 phydev->asym_pause = 0;
1421 if (SPEED_1000 == phydev->speed)
1422 ctl |= BMCR_SPEED1000;
1423 else if (SPEED_100 == phydev->speed)
1424 ctl |= BMCR_SPEED100;
1426 if (DUPLEX_FULL == phydev->duplex)
1427 ctl |= BMCR_FULLDPLX;
1429 return phy_modify(phydev, MII_BMCR,
1430 ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
1432 EXPORT_SYMBOL(genphy_setup_forced);
1435 * genphy_restart_aneg - Enable and Restart Autonegotiation
1436 * @phydev: target phy_device struct
1438 int genphy_restart_aneg(struct phy_device *phydev)
1440 /* Don't isolate the PHY if we're negotiating */
1441 return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
1442 BMCR_ANENABLE | BMCR_ANRESTART);
1444 EXPORT_SYMBOL(genphy_restart_aneg);
1447 * genphy_config_aneg - restart auto-negotiation or write BMCR
1448 * @phydev: target phy_device struct
1450 * Description: If auto-negotiation is enabled, we configure the
1451 * advertising, and then restart auto-negotiation. If it is not
1452 * enabled, then we write the BMCR.
1454 int genphy_config_aneg(struct phy_device *phydev)
1458 changed = genphy_config_eee_advert(phydev);
1460 if (AUTONEG_ENABLE != phydev->autoneg)
1461 return genphy_setup_forced(phydev);
1463 err = genphy_config_advert(phydev);
1464 if (err < 0) /* error */
1470 /* Advertisement hasn't changed, but maybe aneg was never on to
1471 * begin with? Or maybe phy was isolated?
1473 int ctl = phy_read(phydev, MII_BMCR);
1478 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
1479 changed = 1; /* do restart aneg */
1482 /* Only restart aneg if we are advertising something different
1483 * than we were before.
1486 return genphy_restart_aneg(phydev);
1490 EXPORT_SYMBOL(genphy_config_aneg);
1493 * genphy_aneg_done - return auto-negotiation status
1494 * @phydev: target phy_device struct
1496 * Description: Reads the status register and returns 0 either if
1497 * auto-negotiation is incomplete, or if there was an error.
1498 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
1500 int genphy_aneg_done(struct phy_device *phydev)
1502 int retval = phy_read(phydev, MII_BMSR);
1504 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1506 EXPORT_SYMBOL(genphy_aneg_done);
1509 * genphy_update_link - update link status in @phydev
1510 * @phydev: target phy_device struct
1512 * Description: Update the value in phydev->link to reflect the
1513 * current link value. In order to do this, we need to read
1514 * the status register twice, keeping the second value.
1516 int genphy_update_link(struct phy_device *phydev)
1520 /* The link state is latched low so that momentary link
1521 * drops can be detected. Do not double-read the status
1522 * in polling mode to detect such short link drops.
1524 if (!phy_polling_mode(phydev)) {
1525 status = phy_read(phydev, MII_BMSR);
1530 /* Read link and autonegotiation status */
1531 status = phy_read(phydev, MII_BMSR);
1535 if ((status & BMSR_LSTATUS) == 0)
1542 EXPORT_SYMBOL(genphy_update_link);
1545 * genphy_read_status - check the link status and update current link state
1546 * @phydev: target phy_device struct
1548 * Description: Check the link, then figure out the current state
1549 * by comparing what we advertise with what the link partner
1550 * advertises. Start by checking the gigabit possibilities,
1551 * then move on to 10/100.
1553 int genphy_read_status(struct phy_device *phydev)
1560 int common_adv_gb = 0;
1562 /* Update the link, but return if there was an error */
1563 err = genphy_update_link(phydev);
1567 phydev->lp_advertising = 0;
1569 if (AUTONEG_ENABLE == phydev->autoneg) {
1570 if (phydev->supported & (SUPPORTED_1000baseT_Half
1571 | SUPPORTED_1000baseT_Full)) {
1572 lpagb = phy_read(phydev, MII_STAT1000);
1576 adv = phy_read(phydev, MII_CTRL1000);
1580 if (lpagb & LPA_1000MSFAIL) {
1581 if (adv & CTL1000_ENABLE_MASTER)
1582 phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
1584 phydev_err(phydev, "Master/Slave resolution failed\n");
1588 phydev->lp_advertising =
1589 mii_stat1000_to_ethtool_lpa_t(lpagb);
1590 common_adv_gb = lpagb & adv << 2;
1593 lpa = phy_read(phydev, MII_LPA);
1597 phydev->lp_advertising |= mii_lpa_to_ethtool_lpa_t(lpa);
1599 adv = phy_read(phydev, MII_ADVERTISE);
1603 common_adv = lpa & adv;
1605 phydev->speed = SPEED_10;
1606 phydev->duplex = DUPLEX_HALF;
1608 phydev->asym_pause = 0;
1610 if (common_adv_gb & (LPA_1000FULL | LPA_1000HALF)) {
1611 phydev->speed = SPEED_1000;
1613 if (common_adv_gb & LPA_1000FULL)
1614 phydev->duplex = DUPLEX_FULL;
1615 } else if (common_adv & (LPA_100FULL | LPA_100HALF)) {
1616 phydev->speed = SPEED_100;
1618 if (common_adv & LPA_100FULL)
1619 phydev->duplex = DUPLEX_FULL;
1621 if (common_adv & LPA_10FULL)
1622 phydev->duplex = DUPLEX_FULL;
1624 if (phydev->duplex == DUPLEX_FULL) {
1625 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
1626 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
1629 int bmcr = phy_read(phydev, MII_BMCR);
1634 if (bmcr & BMCR_FULLDPLX)
1635 phydev->duplex = DUPLEX_FULL;
1637 phydev->duplex = DUPLEX_HALF;
1639 if (bmcr & BMCR_SPEED1000)
1640 phydev->speed = SPEED_1000;
1641 else if (bmcr & BMCR_SPEED100)
1642 phydev->speed = SPEED_100;
1644 phydev->speed = SPEED_10;
1647 phydev->asym_pause = 0;
1652 EXPORT_SYMBOL(genphy_read_status);
1655 * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
1656 * @phydev: target phy_device struct
1658 * Description: Perform a software PHY reset using the standard
1659 * BMCR_RESET bit and poll for the reset bit to be cleared.
1661 * Returns: 0 on success, < 0 on failure
1663 int genphy_soft_reset(struct phy_device *phydev)
1667 ret = phy_set_bits(phydev, MII_BMCR, BMCR_RESET);
1671 return phy_poll_reset(phydev);
1673 EXPORT_SYMBOL(genphy_soft_reset);
1675 int genphy_config_init(struct phy_device *phydev)
1680 features = (SUPPORTED_TP | SUPPORTED_MII
1681 | SUPPORTED_AUI | SUPPORTED_FIBRE |
1682 SUPPORTED_BNC | SUPPORTED_Pause | SUPPORTED_Asym_Pause);
1684 /* Do we support autonegotiation? */
1685 val = phy_read(phydev, MII_BMSR);
1689 if (val & BMSR_ANEGCAPABLE)
1690 features |= SUPPORTED_Autoneg;
1692 if (val & BMSR_100FULL)
1693 features |= SUPPORTED_100baseT_Full;
1694 if (val & BMSR_100HALF)
1695 features |= SUPPORTED_100baseT_Half;
1696 if (val & BMSR_10FULL)
1697 features |= SUPPORTED_10baseT_Full;
1698 if (val & BMSR_10HALF)
1699 features |= SUPPORTED_10baseT_Half;
1701 if (val & BMSR_ESTATEN) {
1702 val = phy_read(phydev, MII_ESTATUS);
1706 if (val & ESTATUS_1000_TFULL)
1707 features |= SUPPORTED_1000baseT_Full;
1708 if (val & ESTATUS_1000_THALF)
1709 features |= SUPPORTED_1000baseT_Half;
1712 phydev->supported &= features;
1713 phydev->advertising &= features;
1717 EXPORT_SYMBOL(genphy_config_init);
1719 /* This is used for the phy device which doesn't support the MMD extended
1720 * register access, but it does have side effect when we are trying to access
1721 * the MMD register via indirect method.
1723 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum)
1727 EXPORT_SYMBOL(genphy_read_mmd_unsupported);
1729 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
1730 u16 regnum, u16 val)
1734 EXPORT_SYMBOL(genphy_write_mmd_unsupported);
1736 int genphy_suspend(struct phy_device *phydev)
1738 return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN);
1740 EXPORT_SYMBOL(genphy_suspend);
1742 int genphy_resume(struct phy_device *phydev)
1744 return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN);
1746 EXPORT_SYMBOL(genphy_resume);
1748 int genphy_loopback(struct phy_device *phydev, bool enable)
1750 return phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK,
1751 enable ? BMCR_LOOPBACK : 0);
1753 EXPORT_SYMBOL(genphy_loopback);
1755 static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
1757 switch (max_speed) {
1759 phydev->supported &= ~PHY_100BT_FEATURES;
1762 phydev->supported &= ~PHY_1000BT_FEATURES;
1773 int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
1777 err = __set_phy_supported(phydev, max_speed);
1781 phydev->advertising = phydev->supported;
1785 EXPORT_SYMBOL(phy_set_max_speed);
1787 static void of_set_phy_supported(struct phy_device *phydev)
1789 struct device_node *node = phydev->mdio.dev.of_node;
1792 if (!IS_ENABLED(CONFIG_OF_MDIO))
1798 if (!of_property_read_u32(node, "max-speed", &max_speed))
1799 __set_phy_supported(phydev, max_speed);
1802 static void of_set_phy_eee_broken(struct phy_device *phydev)
1804 struct device_node *node = phydev->mdio.dev.of_node;
1807 if (!IS_ENABLED(CONFIG_OF_MDIO))
1813 if (of_property_read_bool(node, "eee-broken-100tx"))
1814 broken |= MDIO_EEE_100TX;
1815 if (of_property_read_bool(node, "eee-broken-1000t"))
1816 broken |= MDIO_EEE_1000T;
1817 if (of_property_read_bool(node, "eee-broken-10gt"))
1818 broken |= MDIO_EEE_10GT;
1819 if (of_property_read_bool(node, "eee-broken-1000kx"))
1820 broken |= MDIO_EEE_1000KX;
1821 if (of_property_read_bool(node, "eee-broken-10gkx4"))
1822 broken |= MDIO_EEE_10GKX4;
1823 if (of_property_read_bool(node, "eee-broken-10gkr"))
1824 broken |= MDIO_EEE_10GKR;
1826 phydev->eee_broken_modes = broken;
1830 * phy_probe - probe and init a PHY device
1831 * @dev: device to probe and init
1833 * Description: Take care of setting up the phy_device structure,
1834 * set the state to READY (the driver's init function should
1835 * set it to STARTING if needed).
1837 static int phy_probe(struct device *dev)
1839 struct phy_device *phydev = to_phy_device(dev);
1840 struct device_driver *drv = phydev->mdio.dev.driver;
1841 struct phy_driver *phydrv = to_phy_driver(drv);
1844 phydev->drv = phydrv;
1846 /* Disable the interrupt if the PHY doesn't support it
1847 * but the interrupt is still a valid one
1849 if (!(phydrv->flags & PHY_HAS_INTERRUPT) &&
1850 phy_interrupt_is_valid(phydev))
1851 phydev->irq = PHY_POLL;
1853 if (phydrv->flags & PHY_IS_INTERNAL)
1854 phydev->is_internal = true;
1856 mutex_lock(&phydev->lock);
1858 /* Start out supporting everything. Eventually,
1859 * a controller will attach, and may modify one
1860 * or both of these values
1862 phydev->supported = phydrv->features;
1863 of_set_phy_supported(phydev);
1864 phydev->advertising = phydev->supported;
1866 /* Get the EEE modes we want to prohibit. We will ask
1867 * the PHY stop advertising these mode later on
1869 of_set_phy_eee_broken(phydev);
1871 /* The Pause Frame bits indicate that the PHY can support passing
1872 * pause frames. During autonegotiation, the PHYs will determine if
1873 * they should allow pause frames to pass. The MAC driver should then
1874 * use that result to determine whether to enable flow control via
1877 * Normally, PHY drivers should not set the Pause bits, and instead
1878 * allow phylib to do that. However, there may be some situations
1879 * (e.g. hardware erratum) where the driver wants to set only one
1882 if (phydrv->features & (SUPPORTED_Pause | SUPPORTED_Asym_Pause)) {
1883 phydev->supported &= ~(SUPPORTED_Pause | SUPPORTED_Asym_Pause);
1884 phydev->supported |= phydrv->features &
1885 (SUPPORTED_Pause | SUPPORTED_Asym_Pause);
1887 phydev->supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
1890 /* Set the state to READY by default */
1891 phydev->state = PHY_READY;
1893 if (phydev->drv->probe) {
1894 /* Deassert the reset signal */
1895 phy_device_reset(phydev, 0);
1897 err = phydev->drv->probe(phydev);
1899 /* Assert the reset signal */
1900 phy_device_reset(phydev, 1);
1904 mutex_unlock(&phydev->lock);
1909 static int phy_remove(struct device *dev)
1911 struct phy_device *phydev = to_phy_device(dev);
1913 cancel_delayed_work_sync(&phydev->state_queue);
1915 mutex_lock(&phydev->lock);
1916 phydev->state = PHY_DOWN;
1917 mutex_unlock(&phydev->lock);
1919 if (phydev->drv && phydev->drv->remove) {
1920 phydev->drv->remove(phydev);
1922 /* Assert the reset signal */
1923 phy_device_reset(phydev, 1);
1931 * phy_driver_register - register a phy_driver with the PHY layer
1932 * @new_driver: new phy_driver to register
1933 * @owner: module owning this PHY
1935 int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
1939 new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
1940 new_driver->mdiodrv.driver.name = new_driver->name;
1941 new_driver->mdiodrv.driver.bus = &mdio_bus_type;
1942 new_driver->mdiodrv.driver.probe = phy_probe;
1943 new_driver->mdiodrv.driver.remove = phy_remove;
1944 new_driver->mdiodrv.driver.owner = owner;
1946 /* The following works around an issue where the PHY driver doesn't bind
1947 * to the device, resulting in the genphy driver being used instead of
1948 * the dedicated driver. The root cause of the issue isn't known yet
1949 * and seems to be in the base driver core. Once this is fixed we may
1950 * remove this workaround.
1952 new_driver->mdiodrv.driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
1954 retval = driver_register(&new_driver->mdiodrv.driver);
1956 pr_err("%s: Error %d in registering driver\n",
1957 new_driver->name, retval);
1962 pr_debug("%s: Registered new driver\n", new_driver->name);
1966 EXPORT_SYMBOL(phy_driver_register);
1968 int phy_drivers_register(struct phy_driver *new_driver, int n,
1969 struct module *owner)
1973 for (i = 0; i < n; i++) {
1974 ret = phy_driver_register(new_driver + i, owner);
1977 phy_driver_unregister(new_driver + i);
1983 EXPORT_SYMBOL(phy_drivers_register);
1985 void phy_driver_unregister(struct phy_driver *drv)
1987 driver_unregister(&drv->mdiodrv.driver);
1989 EXPORT_SYMBOL(phy_driver_unregister);
1991 void phy_drivers_unregister(struct phy_driver *drv, int n)
1995 for (i = 0; i < n; i++)
1996 phy_driver_unregister(drv + i);
1998 EXPORT_SYMBOL(phy_drivers_unregister);
2000 static struct phy_driver genphy_driver = {
2001 .phy_id = 0xffffffff,
2002 .phy_id_mask = 0xffffffff,
2003 .name = "Generic PHY",
2004 .soft_reset = genphy_no_soft_reset,
2005 .config_init = genphy_config_init,
2006 .features = PHY_GBIT_FEATURES | SUPPORTED_MII |
2007 SUPPORTED_AUI | SUPPORTED_FIBRE |
2009 .aneg_done = genphy_aneg_done,
2010 .suspend = genphy_suspend,
2011 .resume = genphy_resume,
2012 .set_loopback = genphy_loopback,
2015 static int __init phy_init(void)
2019 rc = mdio_bus_init();
2023 rc = phy_driver_register(&genphy_10g_driver, THIS_MODULE);
2027 rc = phy_driver_register(&genphy_driver, THIS_MODULE);
2029 phy_driver_unregister(&genphy_10g_driver);
2037 static void __exit phy_exit(void)
2039 phy_driver_unregister(&genphy_10g_driver);
2040 phy_driver_unregister(&genphy_driver);
2044 subsys_initcall(phy_init);
2045 module_exit(phy_exit);