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/bitmap.h>
33 #include <linux/phy.h>
34 #include <linux/phy_led_triggers.h>
35 #include <linux/mdio.h>
37 #include <linux/uaccess.h>
42 MODULE_DESCRIPTION("PHY library");
43 MODULE_AUTHOR("Andy Fleming");
44 MODULE_LICENSE("GPL");
46 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;
47 EXPORT_SYMBOL_GPL(phy_basic_features);
49 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;
50 EXPORT_SYMBOL_GPL(phy_basic_t1_features);
52 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;
53 EXPORT_SYMBOL_GPL(phy_gbit_features);
55 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;
56 EXPORT_SYMBOL_GPL(phy_gbit_fibre_features);
58 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init;
59 EXPORT_SYMBOL_GPL(phy_gbit_all_ports_features);
61 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;
62 EXPORT_SYMBOL_GPL(phy_10gbit_features);
64 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_fec_features) __ro_after_init;
65 EXPORT_SYMBOL_GPL(phy_10gbit_fec_features);
67 static const int phy_basic_ports_array[] = {
68 ETHTOOL_LINK_MODE_Autoneg_BIT,
69 ETHTOOL_LINK_MODE_TP_BIT,
70 ETHTOOL_LINK_MODE_MII_BIT,
72 EXPORT_SYMBOL_GPL(phy_basic_ports_array);
74 static const int phy_fibre_port_array[] = {
75 ETHTOOL_LINK_MODE_FIBRE_BIT,
77 EXPORT_SYMBOL_GPL(phy_fibre_port_array);
79 static const int phy_all_ports_features_array[] = {
80 ETHTOOL_LINK_MODE_Autoneg_BIT,
81 ETHTOOL_LINK_MODE_TP_BIT,
82 ETHTOOL_LINK_MODE_MII_BIT,
83 ETHTOOL_LINK_MODE_FIBRE_BIT,
84 ETHTOOL_LINK_MODE_AUI_BIT,
85 ETHTOOL_LINK_MODE_BNC_BIT,
86 ETHTOOL_LINK_MODE_Backplane_BIT,
88 EXPORT_SYMBOL_GPL(phy_all_ports_features_array);
90 const int phy_10_100_features_array[4] = {
91 ETHTOOL_LINK_MODE_10baseT_Half_BIT,
92 ETHTOOL_LINK_MODE_10baseT_Full_BIT,
93 ETHTOOL_LINK_MODE_100baseT_Half_BIT,
94 ETHTOOL_LINK_MODE_100baseT_Full_BIT,
96 EXPORT_SYMBOL_GPL(phy_10_100_features_array);
98 const int phy_basic_t1_features_array[2] = {
99 ETHTOOL_LINK_MODE_TP_BIT,
100 ETHTOOL_LINK_MODE_100baseT_Full_BIT,
102 EXPORT_SYMBOL_GPL(phy_basic_t1_features_array);
104 const int phy_gbit_features_array[2] = {
105 ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
106 ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
108 EXPORT_SYMBOL_GPL(phy_gbit_features_array);
110 const int phy_10gbit_features_array[1] = {
111 ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
113 EXPORT_SYMBOL_GPL(phy_10gbit_features_array);
115 const int phy_10gbit_fec_features_array[1] = {
116 ETHTOOL_LINK_MODE_10000baseR_FEC_BIT,
118 EXPORT_SYMBOL_GPL(phy_10gbit_fec_features_array);
120 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_full_features) __ro_after_init;
121 EXPORT_SYMBOL_GPL(phy_10gbit_full_features);
123 static const int phy_10gbit_full_features_array[] = {
124 ETHTOOL_LINK_MODE_10baseT_Full_BIT,
125 ETHTOOL_LINK_MODE_100baseT_Full_BIT,
126 ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
127 ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
130 static void features_init(void)
132 /* 10/100 half/full*/
133 linkmode_set_bit_array(phy_basic_ports_array,
134 ARRAY_SIZE(phy_basic_ports_array),
136 linkmode_set_bit_array(phy_10_100_features_array,
137 ARRAY_SIZE(phy_10_100_features_array),
141 linkmode_set_bit_array(phy_basic_t1_features_array,
142 ARRAY_SIZE(phy_basic_t1_features_array),
143 phy_basic_t1_features);
145 /* 10/100 half/full + 1000 half/full */
146 linkmode_set_bit_array(phy_basic_ports_array,
147 ARRAY_SIZE(phy_basic_ports_array),
149 linkmode_set_bit_array(phy_10_100_features_array,
150 ARRAY_SIZE(phy_10_100_features_array),
152 linkmode_set_bit_array(phy_gbit_features_array,
153 ARRAY_SIZE(phy_gbit_features_array),
156 /* 10/100 half/full + 1000 half/full + fibre*/
157 linkmode_set_bit_array(phy_basic_ports_array,
158 ARRAY_SIZE(phy_basic_ports_array),
159 phy_gbit_fibre_features);
160 linkmode_set_bit_array(phy_10_100_features_array,
161 ARRAY_SIZE(phy_10_100_features_array),
162 phy_gbit_fibre_features);
163 linkmode_set_bit_array(phy_gbit_features_array,
164 ARRAY_SIZE(phy_gbit_features_array),
165 phy_gbit_fibre_features);
166 linkmode_set_bit_array(phy_fibre_port_array,
167 ARRAY_SIZE(phy_fibre_port_array),
168 phy_gbit_fibre_features);
170 /* 10/100 half/full + 1000 half/full + TP/MII/FIBRE/AUI/BNC/Backplane*/
171 linkmode_set_bit_array(phy_all_ports_features_array,
172 ARRAY_SIZE(phy_all_ports_features_array),
173 phy_gbit_all_ports_features);
174 linkmode_set_bit_array(phy_10_100_features_array,
175 ARRAY_SIZE(phy_10_100_features_array),
176 phy_gbit_all_ports_features);
177 linkmode_set_bit_array(phy_gbit_features_array,
178 ARRAY_SIZE(phy_gbit_features_array),
179 phy_gbit_all_ports_features);
181 /* 10/100 half/full + 1000 half/full + 10G full*/
182 linkmode_set_bit_array(phy_all_ports_features_array,
183 ARRAY_SIZE(phy_all_ports_features_array),
184 phy_10gbit_features);
185 linkmode_set_bit_array(phy_10_100_features_array,
186 ARRAY_SIZE(phy_10_100_features_array),
187 phy_10gbit_features);
188 linkmode_set_bit_array(phy_gbit_features_array,
189 ARRAY_SIZE(phy_gbit_features_array),
190 phy_10gbit_features);
191 linkmode_set_bit_array(phy_10gbit_features_array,
192 ARRAY_SIZE(phy_10gbit_features_array),
193 phy_10gbit_features);
195 /* 10/100/1000/10G full */
196 linkmode_set_bit_array(phy_all_ports_features_array,
197 ARRAY_SIZE(phy_all_ports_features_array),
198 phy_10gbit_full_features);
199 linkmode_set_bit_array(phy_10gbit_full_features_array,
200 ARRAY_SIZE(phy_10gbit_full_features_array),
201 phy_10gbit_full_features);
203 linkmode_set_bit_array(phy_10gbit_fec_features_array,
204 ARRAY_SIZE(phy_10gbit_fec_features_array),
205 phy_10gbit_fec_features);
208 void phy_device_free(struct phy_device *phydev)
210 put_device(&phydev->mdio.dev);
212 EXPORT_SYMBOL(phy_device_free);
214 static void phy_mdio_device_free(struct mdio_device *mdiodev)
216 struct phy_device *phydev;
218 phydev = container_of(mdiodev, struct phy_device, mdio);
219 phy_device_free(phydev);
222 static void phy_device_release(struct device *dev)
224 kfree(to_phy_device(dev));
227 static void phy_mdio_device_remove(struct mdio_device *mdiodev)
229 struct phy_device *phydev;
231 phydev = container_of(mdiodev, struct phy_device, mdio);
232 phy_device_remove(phydev);
235 static struct phy_driver genphy_driver;
236 extern struct phy_driver genphy_10g_driver;
238 static LIST_HEAD(phy_fixup_list);
239 static DEFINE_MUTEX(phy_fixup_lock);
242 static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
244 struct device_driver *drv = phydev->mdio.dev.driver;
245 struct phy_driver *phydrv = to_phy_driver(drv);
246 struct net_device *netdev = phydev->attached_dev;
248 if (!drv || !phydrv->suspend)
251 /* PHY not attached? May suspend if the PHY has not already been
252 * suspended as part of a prior call to phy_disconnect() ->
253 * phy_detach() -> phy_suspend() because the parent netdev might be the
254 * MDIO bus driver and clock gated at this point.
257 return !phydev->suspended;
259 if (netdev->wol_enabled)
262 /* As long as not all affected network drivers support the
263 * wol_enabled flag, let's check for hints that WoL is enabled.
264 * Don't suspend PHY if the attached netdev parent may wake up.
265 * The parent may point to a PCI device, as in tg3 driver.
267 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
270 /* Also don't suspend PHY if the netdev itself may wakeup. This
271 * is the case for devices w/o underlaying pwr. mgmt. aware bus,
274 if (device_may_wakeup(&netdev->dev))
280 static int mdio_bus_phy_suspend(struct device *dev)
282 struct phy_device *phydev = to_phy_device(dev);
284 /* We must stop the state machine manually, otherwise it stops out of
285 * control, possibly with the phydev->lock held. Upon resume, netdev
286 * may call phy routines that try to grab the same lock, and that may
287 * lead to a deadlock.
289 if (phydev->attached_dev && phydev->adjust_link)
290 phy_stop_machine(phydev);
292 if (!mdio_bus_phy_may_suspend(phydev))
295 return phy_suspend(phydev);
298 static int mdio_bus_phy_resume(struct device *dev)
300 struct phy_device *phydev = to_phy_device(dev);
303 if (!mdio_bus_phy_may_suspend(phydev))
306 ret = phy_resume(phydev);
311 if (phydev->attached_dev && phydev->adjust_link)
312 phy_start_machine(phydev);
317 static int mdio_bus_phy_restore(struct device *dev)
319 struct phy_device *phydev = to_phy_device(dev);
320 struct net_device *netdev = phydev->attached_dev;
326 ret = phy_init_hw(phydev);
330 if (phydev->attached_dev && phydev->adjust_link)
331 phy_start_machine(phydev);
336 static const struct dev_pm_ops mdio_bus_phy_pm_ops = {
337 .suspend = mdio_bus_phy_suspend,
338 .resume = mdio_bus_phy_resume,
339 .freeze = mdio_bus_phy_suspend,
340 .thaw = mdio_bus_phy_resume,
341 .restore = mdio_bus_phy_restore,
344 #define MDIO_BUS_PHY_PM_OPS (&mdio_bus_phy_pm_ops)
348 #define MDIO_BUS_PHY_PM_OPS NULL
350 #endif /* CONFIG_PM */
353 * phy_register_fixup - creates a new phy_fixup and adds it to the list
354 * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID)
355 * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
356 * It can also be PHY_ANY_UID
357 * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
359 * @run: The actual code to be run when a matching PHY is found
361 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
362 int (*run)(struct phy_device *))
364 struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
369 strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
370 fixup->phy_uid = phy_uid;
371 fixup->phy_uid_mask = phy_uid_mask;
374 mutex_lock(&phy_fixup_lock);
375 list_add_tail(&fixup->list, &phy_fixup_list);
376 mutex_unlock(&phy_fixup_lock);
380 EXPORT_SYMBOL(phy_register_fixup);
382 /* Registers a fixup to be run on any PHY with the UID in phy_uid */
383 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
384 int (*run)(struct phy_device *))
386 return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
388 EXPORT_SYMBOL(phy_register_fixup_for_uid);
390 /* Registers a fixup to be run on the PHY with id string bus_id */
391 int phy_register_fixup_for_id(const char *bus_id,
392 int (*run)(struct phy_device *))
394 return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
396 EXPORT_SYMBOL(phy_register_fixup_for_id);
399 * phy_unregister_fixup - remove a phy_fixup from the list
400 * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list
401 * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list
402 * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison
404 int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask)
406 struct list_head *pos, *n;
407 struct phy_fixup *fixup;
412 mutex_lock(&phy_fixup_lock);
413 list_for_each_safe(pos, n, &phy_fixup_list) {
414 fixup = list_entry(pos, struct phy_fixup, list);
416 if ((!strcmp(fixup->bus_id, bus_id)) &&
417 ((fixup->phy_uid & phy_uid_mask) ==
418 (phy_uid & phy_uid_mask))) {
419 list_del(&fixup->list);
425 mutex_unlock(&phy_fixup_lock);
429 EXPORT_SYMBOL(phy_unregister_fixup);
431 /* Unregisters a fixup of any PHY with the UID in phy_uid */
432 int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask)
434 return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask);
436 EXPORT_SYMBOL(phy_unregister_fixup_for_uid);
438 /* Unregisters a fixup of the PHY with id string bus_id */
439 int phy_unregister_fixup_for_id(const char *bus_id)
441 return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff);
443 EXPORT_SYMBOL(phy_unregister_fixup_for_id);
445 /* Returns 1 if fixup matches phydev in bus_id and phy_uid.
446 * Fixups can be set to match any in one or more fields.
448 static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
450 if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
451 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
454 if ((fixup->phy_uid & fixup->phy_uid_mask) !=
455 (phydev->phy_id & fixup->phy_uid_mask))
456 if (fixup->phy_uid != PHY_ANY_UID)
462 /* Runs any matching fixups for this phydev */
463 static int phy_scan_fixups(struct phy_device *phydev)
465 struct phy_fixup *fixup;
467 mutex_lock(&phy_fixup_lock);
468 list_for_each_entry(fixup, &phy_fixup_list, list) {
469 if (phy_needs_fixup(phydev, fixup)) {
470 int err = fixup->run(phydev);
473 mutex_unlock(&phy_fixup_lock);
476 phydev->has_fixups = true;
479 mutex_unlock(&phy_fixup_lock);
484 static int phy_bus_match(struct device *dev, struct device_driver *drv)
486 struct phy_device *phydev = to_phy_device(dev);
487 struct phy_driver *phydrv = to_phy_driver(drv);
488 const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
491 if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
494 if (phydrv->match_phy_device)
495 return phydrv->match_phy_device(phydev);
497 if (phydev->is_c45) {
498 for (i = 1; i < num_ids; i++) {
499 if (!(phydev->c45_ids.devices_in_package & (1 << i)))
502 if ((phydrv->phy_id & phydrv->phy_id_mask) ==
503 (phydev->c45_ids.device_ids[i] &
504 phydrv->phy_id_mask))
509 return (phydrv->phy_id & phydrv->phy_id_mask) ==
510 (phydev->phy_id & phydrv->phy_id_mask);
515 phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
517 struct phy_device *phydev = to_phy_device(dev);
519 return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
521 static DEVICE_ATTR_RO(phy_id);
524 phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
526 struct phy_device *phydev = to_phy_device(dev);
527 const char *mode = NULL;
529 if (phy_is_internal(phydev))
532 mode = phy_modes(phydev->interface);
534 return sprintf(buf, "%s\n", mode);
536 static DEVICE_ATTR_RO(phy_interface);
539 phy_has_fixups_show(struct device *dev, struct device_attribute *attr,
542 struct phy_device *phydev = to_phy_device(dev);
544 return sprintf(buf, "%d\n", phydev->has_fixups);
546 static DEVICE_ATTR_RO(phy_has_fixups);
548 static struct attribute *phy_dev_attrs[] = {
549 &dev_attr_phy_id.attr,
550 &dev_attr_phy_interface.attr,
551 &dev_attr_phy_has_fixups.attr,
554 ATTRIBUTE_GROUPS(phy_dev);
556 static const struct device_type mdio_bus_phy_type = {
558 .groups = phy_dev_groups,
559 .release = phy_device_release,
560 .pm = MDIO_BUS_PHY_PM_OPS,
563 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
565 struct phy_c45_device_ids *c45_ids)
567 struct phy_device *dev;
568 struct mdio_device *mdiodev;
570 /* We allocate the device, and initialize the default values */
571 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
573 return ERR_PTR(-ENOMEM);
575 mdiodev = &dev->mdio;
576 mdiodev->dev.parent = &bus->dev;
577 mdiodev->dev.bus = &mdio_bus_type;
578 mdiodev->dev.type = &mdio_bus_phy_type;
580 mdiodev->bus_match = phy_bus_match;
581 mdiodev->addr = addr;
582 mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
583 mdiodev->device_free = phy_mdio_device_free;
584 mdiodev->device_remove = phy_mdio_device_remove;
591 dev->interface = PHY_INTERFACE_MODE_GMII;
593 dev->autoneg = AUTONEG_ENABLE;
595 dev->is_c45 = is_c45;
596 dev->phy_id = phy_id;
598 dev->c45_ids = *c45_ids;
599 dev->irq = bus->irq[addr];
600 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
602 dev->state = PHY_DOWN;
604 mutex_init(&dev->lock);
605 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
607 /* Request the appropriate module unconditionally; don't
608 * bother trying to do so only if it isn't already loaded,
609 * because that gets complicated. A hotplug event would have
610 * done an unconditional modprobe anyway.
611 * We don't do normal hotplug because it won't work for MDIO
612 * -- because it relies on the device staying around for long
613 * enough for the driver to get loaded. With MDIO, the NIC
614 * driver will get bored and give up as soon as it finds that
615 * there's no driver _already_ loaded.
617 if (is_c45 && c45_ids) {
618 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
621 for (i = 1; i < num_ids; i++) {
622 if (!(c45_ids->devices_in_package & (1 << i)))
625 request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT,
626 MDIO_ID_ARGS(c45_ids->device_ids[i]));
629 request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT,
630 MDIO_ID_ARGS(phy_id));
633 device_initialize(&mdiodev->dev);
637 EXPORT_SYMBOL(phy_device_create);
639 /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
640 * @bus: the target MII bus
641 * @addr: PHY address on the MII bus
642 * @dev_addr: MMD address in the PHY.
643 * @devices_in_package: where to store the devices in package information.
645 * Description: reads devices in package registers of a MMD at @dev_addr
646 * from PHY at @addr on @bus.
648 * Returns: 0 on success, -EIO on failure.
650 static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
651 u32 *devices_in_package)
653 int phy_reg, reg_addr;
655 reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS2;
656 phy_reg = mdiobus_read(bus, addr, reg_addr);
659 *devices_in_package = (phy_reg & 0xffff) << 16;
661 reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS1;
662 phy_reg = mdiobus_read(bus, addr, reg_addr);
665 *devices_in_package |= (phy_reg & 0xffff);
671 * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
672 * @bus: the target MII bus
673 * @addr: PHY address on the MII bus
674 * @phy_id: where to store the ID retrieved.
675 * @c45_ids: where to store the c45 ID information.
677 * If the PHY devices-in-package appears to be valid, it and the
678 * corresponding identifiers are stored in @c45_ids, zero is stored
679 * in @phy_id. Otherwise 0xffffffff is stored in @phy_id. Returns
683 static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,
684 struct phy_c45_device_ids *c45_ids) {
687 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
688 u32 *devs = &c45_ids->devices_in_package;
690 /* Find first non-zero Devices In package. Device zero is reserved
691 * for 802.3 c45 complied PHYs, so don't probe it at first.
693 for (i = 1; i < num_ids && *devs == 0; i++) {
694 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, devs);
698 if ((*devs & 0x1fffffff) == 0x1fffffff) {
699 /* If mostly Fs, there is no device there,
700 * then let's continue to probe more, as some
701 * 10G PHYs have zero Devices In package,
702 * e.g. Cortina CS4315/CS4340 PHY.
704 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, devs);
707 /* no device there, let's get out of here */
708 if ((*devs & 0x1fffffff) == 0x1fffffff) {
709 *phy_id = 0xffffffff;
717 /* Now probe Device Identifiers for each device present. */
718 for (i = 1; i < num_ids; i++) {
719 if (!(c45_ids->devices_in_package & (1 << i)))
722 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1;
723 phy_reg = mdiobus_read(bus, addr, reg_addr);
726 c45_ids->device_ids[i] = (phy_reg & 0xffff) << 16;
728 reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2;
729 phy_reg = mdiobus_read(bus, addr, reg_addr);
732 c45_ids->device_ids[i] |= (phy_reg & 0xffff);
739 * get_phy_id - reads the specified addr for its ID.
740 * @bus: the target MII bus
741 * @addr: PHY address on the MII bus
742 * @phy_id: where to store the ID retrieved.
743 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
744 * @c45_ids: where to store the c45 ID information.
746 * Description: In the case of a 802.3-c22 PHY, reads the ID registers
747 * of the PHY at @addr on the @bus, stores it in @phy_id and returns
750 * In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and
751 * its return value is in turn returned.
754 static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
755 bool is_c45, struct phy_c45_device_ids *c45_ids)
760 return get_phy_c45_ids(bus, addr, phy_id, c45_ids);
762 /* Grab the bits from PHYIR1, and put them in the upper half */
763 phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
765 /* if there is no device, return without an error so scanning
766 * the bus works properly
768 if (phy_reg == -EIO || phy_reg == -ENODEV) {
769 *phy_id = 0xffffffff;
776 *phy_id = (phy_reg & 0xffff) << 16;
778 /* Grab the bits from PHYIR2, and put them in the lower half */
779 phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
783 *phy_id |= (phy_reg & 0xffff);
789 * get_phy_device - reads the specified PHY device and returns its @phy_device
791 * @bus: the target MII bus
792 * @addr: PHY address on the MII bus
793 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
795 * Description: Reads the ID registers of the PHY at @addr on the
796 * @bus, then allocates and returns the phy_device to represent it.
798 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
800 struct phy_c45_device_ids c45_ids = {0};
804 r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids);
808 /* If the phy_id is mostly Fs, there is no device there */
809 if ((phy_id & 0x1fffffff) == 0x1fffffff)
810 return ERR_PTR(-ENODEV);
812 return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
814 EXPORT_SYMBOL(get_phy_device);
817 * phy_device_register - Register the phy device on the MDIO bus
818 * @phydev: phy_device structure to be added to the MDIO bus
820 int phy_device_register(struct phy_device *phydev)
824 err = mdiobus_register_device(&phydev->mdio);
828 /* Deassert the reset signal */
829 phy_device_reset(phydev, 0);
831 /* Run all of the fixups for this PHY */
832 err = phy_scan_fixups(phydev);
834 pr_err("PHY %d failed to initialize\n", phydev->mdio.addr);
838 err = device_add(&phydev->mdio.dev);
840 pr_err("PHY %d failed to add\n", phydev->mdio.addr);
847 /* Assert the reset signal */
848 phy_device_reset(phydev, 1);
850 mdiobus_unregister_device(&phydev->mdio);
853 EXPORT_SYMBOL(phy_device_register);
856 * phy_device_remove - Remove a previously registered phy device from the MDIO bus
857 * @phydev: phy_device structure to remove
859 * This doesn't free the phy_device itself, it merely reverses the effects
860 * of phy_device_register(). Use phy_device_free() to free the device
861 * after calling this function.
863 void phy_device_remove(struct phy_device *phydev)
865 device_del(&phydev->mdio.dev);
867 /* Assert the reset signal */
868 phy_device_reset(phydev, 1);
870 mdiobus_unregister_device(&phydev->mdio);
872 EXPORT_SYMBOL(phy_device_remove);
875 * phy_find_first - finds the first PHY device on the bus
876 * @bus: the target MII bus
878 struct phy_device *phy_find_first(struct mii_bus *bus)
880 struct phy_device *phydev;
883 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
884 phydev = mdiobus_get_phy(bus, addr);
890 EXPORT_SYMBOL(phy_find_first);
892 static void phy_link_change(struct phy_device *phydev, bool up, bool do_carrier)
894 struct net_device *netdev = phydev->attached_dev;
898 netif_carrier_on(netdev);
900 netif_carrier_off(netdev);
902 phydev->adjust_link(netdev);
906 * phy_prepare_link - prepares the PHY layer to monitor link status
907 * @phydev: target phy_device struct
908 * @handler: callback function for link status change notifications
910 * Description: Tells the PHY infrastructure to handle the
911 * gory details on monitoring link status (whether through
912 * polling or an interrupt), and to call back to the
913 * connected device driver when the link status changes.
914 * If you want to monitor your own link state, don't call
917 static void phy_prepare_link(struct phy_device *phydev,
918 void (*handler)(struct net_device *))
920 phydev->adjust_link = handler;
924 * phy_connect_direct - connect an ethernet device to a specific phy_device
925 * @dev: the network device to connect
926 * @phydev: the pointer to the phy device
927 * @handler: callback function for state change notifications
928 * @interface: PHY device's interface
930 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
931 void (*handler)(struct net_device *),
932 phy_interface_t interface)
936 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
940 phy_prepare_link(phydev, handler);
941 phy_start_machine(phydev);
943 phy_start_interrupts(phydev);
947 EXPORT_SYMBOL(phy_connect_direct);
950 * phy_connect - connect an ethernet device to a PHY device
951 * @dev: the network device to connect
952 * @bus_id: the id string of the PHY device to connect
953 * @handler: callback function for state change notifications
954 * @interface: PHY device's interface
956 * Description: Convenience function for connecting ethernet
957 * devices to PHY devices. The default behavior is for
958 * the PHY infrastructure to handle everything, and only notify
959 * the connected driver when the link status changes. If you
960 * don't want, or can't use the provided functionality, you may
961 * choose to call only the subset of functions which provide
962 * the desired functionality.
964 struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
965 void (*handler)(struct net_device *),
966 phy_interface_t interface)
968 struct phy_device *phydev;
972 /* Search the list of PHY devices on the mdio bus for the
973 * PHY with the requested name
975 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
977 pr_err("PHY %s not found\n", bus_id);
978 return ERR_PTR(-ENODEV);
980 phydev = to_phy_device(d);
982 rc = phy_connect_direct(dev, phydev, handler, interface);
989 EXPORT_SYMBOL(phy_connect);
992 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
994 * @phydev: target phy_device struct
996 void phy_disconnect(struct phy_device *phydev)
999 phy_stop_interrupts(phydev);
1001 phy_stop_machine(phydev);
1003 phydev->adjust_link = NULL;
1007 EXPORT_SYMBOL(phy_disconnect);
1010 * phy_poll_reset - Safely wait until a PHY reset has properly completed
1011 * @phydev: The PHY device to poll
1013 * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
1014 * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR
1015 * register must be polled until the BMCR_RESET bit clears.
1017 * Furthermore, any attempts to write to PHY registers may have no effect
1018 * or even generate MDIO bus errors until this is complete.
1020 * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
1021 * standard and do not fully reset after the BMCR_RESET bit is set, and may
1022 * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an
1023 * effort to support such broken PHYs, this function is separate from the
1024 * standard phy_init_hw() which will zero all the other bits in the BMCR
1025 * and reapply all driver-specific and board-specific fixups.
1027 static int phy_poll_reset(struct phy_device *phydev)
1029 /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
1030 unsigned int retries = 12;
1035 ret = phy_read(phydev, MII_BMCR);
1038 } while (ret & BMCR_RESET && --retries);
1039 if (ret & BMCR_RESET)
1042 /* Some chips (smsc911x) may still need up to another 1ms after the
1043 * BMCR_RESET bit is cleared before they are usable.
1049 int phy_init_hw(struct phy_device *phydev)
1053 /* Deassert the reset signal */
1054 phy_device_reset(phydev, 0);
1056 if (!phydev->drv || !phydev->drv->config_init)
1059 if (phydev->drv->soft_reset)
1060 ret = phydev->drv->soft_reset(phydev);
1065 ret = phy_scan_fixups(phydev);
1069 return phydev->drv->config_init(phydev);
1071 EXPORT_SYMBOL(phy_init_hw);
1073 void phy_attached_info(struct phy_device *phydev)
1075 phy_attached_print(phydev, NULL);
1077 EXPORT_SYMBOL(phy_attached_info);
1079 #define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%s)"
1080 void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
1082 const char *drv_name = phydev->drv ? phydev->drv->name : "unbound";
1086 switch(phydev->irq) {
1090 case PHY_IGNORE_INTERRUPT:
1094 snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
1101 phydev_info(phydev, ATTACHED_FMT "\n",
1102 drv_name, phydev_name(phydev),
1107 phydev_info(phydev, ATTACHED_FMT,
1108 drv_name, phydev_name(phydev),
1116 EXPORT_SYMBOL(phy_attached_print);
1119 * phy_attach_direct - attach a network device to a given PHY device pointer
1120 * @dev: network device to attach
1121 * @phydev: Pointer to phy_device to attach
1122 * @flags: PHY device's dev_flags
1123 * @interface: PHY device's interface
1125 * Description: Called by drivers to attach to a particular PHY
1126 * device. The phy_device is found, and properly hooked up
1127 * to the phy_driver. If no driver is attached, then a
1128 * generic driver is used. The phy_device is given a ptr to
1129 * the attaching device, and given a callback for link status
1130 * change. The phy_device is returned to the attaching driver.
1131 * This function takes a reference on the phy device.
1133 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
1134 u32 flags, phy_interface_t interface)
1136 struct module *ndev_owner = dev->dev.parent->driver->owner;
1137 struct mii_bus *bus = phydev->mdio.bus;
1138 struct device *d = &phydev->mdio.dev;
1139 bool using_genphy = false;
1142 /* For Ethernet device drivers that register their own MDIO bus, we
1143 * will have bus->owner match ndev_mod, so we do not want to increment
1144 * our own module->refcnt here, otherwise we would not be able to
1147 if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
1148 dev_err(&dev->dev, "failed to get the bus module\n");
1154 /* Assume that if there is no driver, that it doesn't
1155 * exist, and we should use the genphy driver.
1159 d->driver = &genphy_10g_driver.mdiodrv.driver;
1161 d->driver = &genphy_driver.mdiodrv.driver;
1163 using_genphy = true;
1166 if (!try_module_get(d->driver->owner)) {
1167 dev_err(&dev->dev, "failed to get the device driver module\n");
1169 goto error_put_device;
1173 err = d->driver->probe(d);
1175 err = device_bind_driver(d);
1178 goto error_module_put;
1181 if (phydev->attached_dev) {
1182 dev_err(&dev->dev, "PHY already attached\n");
1187 phydev->phy_link_change = phy_link_change;
1188 phydev->attached_dev = dev;
1189 dev->phydev = phydev;
1191 /* Some Ethernet drivers try to connect to a PHY device before
1192 * calling register_netdevice() -> netdev_register_kobject() and
1193 * does the dev->dev.kobj initialization. Here we only check for
1194 * success which indicates that the network device kobject is
1195 * ready. Once we do that we still need to keep track of whether
1196 * links were successfully set up or not for phy_detach() to
1197 * remove them accordingly.
1199 phydev->sysfs_links = false;
1201 err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
1204 err = sysfs_create_link_nowarn(&dev->dev.kobj,
1205 &phydev->mdio.dev.kobj,
1208 dev_err(&dev->dev, "could not add device link to %s err %d\n",
1209 kobject_name(&phydev->mdio.dev.kobj),
1211 /* non-fatal - some net drivers can use one netdevice
1212 * with more then one phy
1216 phydev->sysfs_links = true;
1219 phydev->dev_flags = flags;
1221 phydev->interface = interface;
1223 phydev->state = PHY_READY;
1225 /* Initial carrier state is off as the phy is about to be
1228 netif_carrier_off(phydev->attached_dev);
1230 /* Do initial configuration here, now that
1231 * we have certain key parameters
1232 * (dev_flags and interface)
1234 err = phy_init_hw(phydev);
1239 phy_led_triggers_register(phydev);
1244 /* phy_detach() does all of the cleanup below */
1249 module_put(d->driver->owner);
1252 if (ndev_owner != bus->owner)
1253 module_put(bus->owner);
1256 EXPORT_SYMBOL(phy_attach_direct);
1259 * phy_attach - attach a network device to a particular PHY device
1260 * @dev: network device to attach
1261 * @bus_id: Bus ID of PHY device to attach
1262 * @interface: PHY device's interface
1264 * Description: Same as phy_attach_direct() except that a PHY bus_id
1265 * string is passed instead of a pointer to a struct phy_device.
1267 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1268 phy_interface_t interface)
1270 struct bus_type *bus = &mdio_bus_type;
1271 struct phy_device *phydev;
1275 /* Search the list of PHY devices on the mdio bus for the
1276 * PHY with the requested name
1278 d = bus_find_device_by_name(bus, NULL, bus_id);
1280 pr_err("PHY %s not found\n", bus_id);
1281 return ERR_PTR(-ENODEV);
1283 phydev = to_phy_device(d);
1285 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
1292 EXPORT_SYMBOL(phy_attach);
1295 * phy_detach - detach a PHY device from its network device
1296 * @phydev: target phy_device struct
1298 * This detaches the phy device from its network device and the phy
1299 * driver, and drops the reference count taken in phy_attach_direct().
1301 void phy_detach(struct phy_device *phydev)
1303 struct net_device *dev = phydev->attached_dev;
1304 struct module *ndev_owner = dev->dev.parent->driver->owner;
1305 struct mii_bus *bus;
1307 if (phydev->sysfs_links) {
1308 sysfs_remove_link(&dev->dev.kobj, "phydev");
1309 sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
1311 phy_suspend(phydev);
1312 phydev->attached_dev->phydev = NULL;
1313 phydev->attached_dev = NULL;
1314 phydev->phylink = NULL;
1316 phy_led_triggers_unregister(phydev);
1318 module_put(phydev->mdio.dev.driver->owner);
1320 /* If the device had no specific driver before (i.e. - it
1321 * was using the generic driver), we unbind the device
1322 * from the generic driver so that there's a chance a
1323 * real driver could be loaded
1325 if (phydev->mdio.dev.driver == &genphy_10g_driver.mdiodrv.driver ||
1326 phydev->mdio.dev.driver == &genphy_driver.mdiodrv.driver)
1327 device_release_driver(&phydev->mdio.dev);
1330 * The phydev might go away on the put_device() below, so avoid
1331 * a use-after-free bug by reading the underlying bus first.
1333 bus = phydev->mdio.bus;
1335 put_device(&phydev->mdio.dev);
1336 if (ndev_owner != bus->owner)
1337 module_put(bus->owner);
1339 /* Assert the reset signal */
1340 phy_device_reset(phydev, 1);
1342 EXPORT_SYMBOL(phy_detach);
1344 int phy_suspend(struct phy_device *phydev)
1346 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1347 struct net_device *netdev = phydev->attached_dev;
1348 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1351 /* If the device has WOL enabled, we cannot suspend the PHY */
1352 phy_ethtool_get_wol(phydev, &wol);
1353 if (wol.wolopts || (netdev && netdev->wol_enabled))
1356 if (phydev->drv && phydrv->suspend)
1357 ret = phydrv->suspend(phydev);
1362 phydev->suspended = true;
1366 EXPORT_SYMBOL(phy_suspend);
1368 int __phy_resume(struct phy_device *phydev)
1370 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1373 WARN_ON(!mutex_is_locked(&phydev->lock));
1375 if (phydev->drv && phydrv->resume)
1376 ret = phydrv->resume(phydev);
1381 phydev->suspended = false;
1385 EXPORT_SYMBOL(__phy_resume);
1387 int phy_resume(struct phy_device *phydev)
1391 mutex_lock(&phydev->lock);
1392 ret = __phy_resume(phydev);
1393 mutex_unlock(&phydev->lock);
1397 EXPORT_SYMBOL(phy_resume);
1399 int phy_loopback(struct phy_device *phydev, bool enable)
1401 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1404 mutex_lock(&phydev->lock);
1406 if (enable && phydev->loopback_enabled) {
1411 if (!enable && !phydev->loopback_enabled) {
1416 if (phydev->drv && phydrv->set_loopback)
1417 ret = phydrv->set_loopback(phydev, enable);
1424 phydev->loopback_enabled = enable;
1427 mutex_unlock(&phydev->lock);
1430 EXPORT_SYMBOL(phy_loopback);
1433 * phy_reset_after_clk_enable - perform a PHY reset if needed
1434 * @phydev: target phy_device struct
1436 * Description: Some PHYs are known to need a reset after their refclk was
1437 * enabled. This function evaluates the flags and perform the reset if it's
1438 * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy
1441 int phy_reset_after_clk_enable(struct phy_device *phydev)
1443 if (!phydev || !phydev->drv)
1446 if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) {
1447 phy_device_reset(phydev, 1);
1448 phy_device_reset(phydev, 0);
1454 EXPORT_SYMBOL(phy_reset_after_clk_enable);
1456 /* Generic PHY support and helper functions */
1459 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
1460 * @phydev: target phy_device struct
1462 * Description: Writes MII_ADVERTISE with the appropriate values,
1463 * after sanitizing the values to make sure we only advertise
1464 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
1465 * hasn't changed, and > 0 if it has changed.
1467 static int genphy_config_advert(struct phy_device *phydev)
1470 int oldadv, adv, bmsr;
1471 int err, changed = 0;
1473 /* Only allow advertising what this PHY supports */
1474 linkmode_and(phydev->advertising, phydev->advertising,
1476 if (!ethtool_convert_link_mode_to_legacy_u32(&advertise,
1477 phydev->advertising))
1478 phydev_warn(phydev, "PHY advertising (%*pb) more modes than genphy supports, some modes not advertised.\n",
1479 __ETHTOOL_LINK_MODE_MASK_NBITS,
1480 phydev->advertising);
1482 /* Setup standard advertisement */
1483 adv = phy_read(phydev, MII_ADVERTISE);
1488 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
1489 ADVERTISE_PAUSE_ASYM);
1490 adv |= ethtool_adv_to_mii_adv_t(advertise);
1492 if (adv != oldadv) {
1493 err = phy_write(phydev, MII_ADVERTISE, adv);
1500 bmsr = phy_read(phydev, MII_BMSR);
1504 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
1505 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
1508 if (!(bmsr & BMSR_ESTATEN))
1511 /* Configure gigabit if it's supported */
1512 adv = phy_read(phydev, MII_CTRL1000);
1517 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1519 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
1520 phydev->supported) ||
1521 linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1523 adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
1528 err = phy_write(phydev, MII_CTRL1000, adv);
1536 * genphy_config_eee_advert - disable unwanted eee mode advertisement
1537 * @phydev: target phy_device struct
1539 * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy
1540 * efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't
1541 * changed, and 1 if it has changed.
1543 static int genphy_config_eee_advert(struct phy_device *phydev)
1545 int broken = phydev->eee_broken_modes;
1548 /* Nothing to disable */
1552 /* If the following call fails, we assume that EEE is not
1553 * supported by the phy. If we read 0, EEE is not advertised
1554 * In both case, we don't need to continue
1556 adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1563 /* Advertising remains unchanged with the broken mask */
1567 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
1573 * genphy_setup_forced - configures/forces speed/duplex from @phydev
1574 * @phydev: target phy_device struct
1576 * Description: Configures MII_BMCR to force speed/duplex
1577 * to the values in phydev. Assumes that the values are valid.
1578 * Please see phy_sanitize_settings().
1580 int genphy_setup_forced(struct phy_device *phydev)
1585 phydev->asym_pause = 0;
1587 if (SPEED_1000 == phydev->speed)
1588 ctl |= BMCR_SPEED1000;
1589 else if (SPEED_100 == phydev->speed)
1590 ctl |= BMCR_SPEED100;
1592 if (DUPLEX_FULL == phydev->duplex)
1593 ctl |= BMCR_FULLDPLX;
1595 return phy_modify(phydev, MII_BMCR,
1596 ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
1598 EXPORT_SYMBOL(genphy_setup_forced);
1601 * genphy_restart_aneg - Enable and Restart Autonegotiation
1602 * @phydev: target phy_device struct
1604 int genphy_restart_aneg(struct phy_device *phydev)
1606 /* Don't isolate the PHY if we're negotiating */
1607 return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
1608 BMCR_ANENABLE | BMCR_ANRESTART);
1610 EXPORT_SYMBOL(genphy_restart_aneg);
1613 * genphy_config_aneg - restart auto-negotiation or write BMCR
1614 * @phydev: target phy_device struct
1616 * Description: If auto-negotiation is enabled, we configure the
1617 * advertising, and then restart auto-negotiation. If it is not
1618 * enabled, then we write the BMCR.
1620 int genphy_config_aneg(struct phy_device *phydev)
1624 changed = genphy_config_eee_advert(phydev);
1626 if (AUTONEG_ENABLE != phydev->autoneg)
1627 return genphy_setup_forced(phydev);
1629 err = genphy_config_advert(phydev);
1630 if (err < 0) /* error */
1636 /* Advertisement hasn't changed, but maybe aneg was never on to
1637 * begin with? Or maybe phy was isolated?
1639 int ctl = phy_read(phydev, MII_BMCR);
1644 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
1645 changed = 1; /* do restart aneg */
1648 /* Only restart aneg if we are advertising something different
1649 * than we were before.
1652 return genphy_restart_aneg(phydev);
1656 EXPORT_SYMBOL(genphy_config_aneg);
1659 * genphy_aneg_done - return auto-negotiation status
1660 * @phydev: target phy_device struct
1662 * Description: Reads the status register and returns 0 either if
1663 * auto-negotiation is incomplete, or if there was an error.
1664 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
1666 int genphy_aneg_done(struct phy_device *phydev)
1668 int retval = phy_read(phydev, MII_BMSR);
1670 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1672 EXPORT_SYMBOL(genphy_aneg_done);
1675 * genphy_update_link - update link status in @phydev
1676 * @phydev: target phy_device struct
1678 * Description: Update the value in phydev->link to reflect the
1679 * current link value. In order to do this, we need to read
1680 * the status register twice, keeping the second value.
1682 int genphy_update_link(struct phy_device *phydev)
1686 /* Do a fake read */
1687 status = phy_read(phydev, MII_BMSR);
1691 /* Read link and autonegotiation status */
1692 status = phy_read(phydev, MII_BMSR);
1696 if ((status & BMSR_LSTATUS) == 0)
1703 EXPORT_SYMBOL(genphy_update_link);
1706 * genphy_read_status - check the link status and update current link state
1707 * @phydev: target phy_device struct
1709 * Description: Check the link, then figure out the current state
1710 * by comparing what we advertise with what the link partner
1711 * advertises. Start by checking the gigabit possibilities,
1712 * then move on to 10/100.
1714 int genphy_read_status(struct phy_device *phydev)
1721 int common_adv_gb = 0;
1723 /* Update the link, but return if there was an error */
1724 err = genphy_update_link(phydev);
1728 linkmode_zero(phydev->lp_advertising);
1730 if (AUTONEG_ENABLE == phydev->autoneg) {
1731 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
1732 phydev->supported) ||
1733 linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1734 phydev->supported)) {
1735 lpagb = phy_read(phydev, MII_STAT1000);
1739 adv = phy_read(phydev, MII_CTRL1000);
1743 if (lpagb & LPA_1000MSFAIL) {
1744 if (adv & CTL1000_ENABLE_MASTER)
1745 phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
1747 phydev_err(phydev, "Master/Slave resolution failed\n");
1751 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
1753 common_adv_gb = lpagb & adv << 2;
1756 lpa = phy_read(phydev, MII_LPA);
1760 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa);
1762 adv = phy_read(phydev, MII_ADVERTISE);
1766 common_adv = lpa & adv;
1768 phydev->speed = SPEED_10;
1769 phydev->duplex = DUPLEX_HALF;
1771 phydev->asym_pause = 0;
1773 if (common_adv_gb & (LPA_1000FULL | LPA_1000HALF)) {
1774 phydev->speed = SPEED_1000;
1776 if (common_adv_gb & LPA_1000FULL)
1777 phydev->duplex = DUPLEX_FULL;
1778 } else if (common_adv & (LPA_100FULL | LPA_100HALF)) {
1779 phydev->speed = SPEED_100;
1781 if (common_adv & LPA_100FULL)
1782 phydev->duplex = DUPLEX_FULL;
1784 if (common_adv & LPA_10FULL)
1785 phydev->duplex = DUPLEX_FULL;
1787 if (phydev->duplex == DUPLEX_FULL) {
1788 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
1789 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
1792 int bmcr = phy_read(phydev, MII_BMCR);
1797 if (bmcr & BMCR_FULLDPLX)
1798 phydev->duplex = DUPLEX_FULL;
1800 phydev->duplex = DUPLEX_HALF;
1802 if (bmcr & BMCR_SPEED1000)
1803 phydev->speed = SPEED_1000;
1804 else if (bmcr & BMCR_SPEED100)
1805 phydev->speed = SPEED_100;
1807 phydev->speed = SPEED_10;
1810 phydev->asym_pause = 0;
1815 EXPORT_SYMBOL(genphy_read_status);
1818 * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
1819 * @phydev: target phy_device struct
1821 * Description: Perform a software PHY reset using the standard
1822 * BMCR_RESET bit and poll for the reset bit to be cleared.
1824 * Returns: 0 on success, < 0 on failure
1826 int genphy_soft_reset(struct phy_device *phydev)
1830 ret = phy_write(phydev, MII_BMCR, BMCR_RESET);
1834 return phy_poll_reset(phydev);
1836 EXPORT_SYMBOL(genphy_soft_reset);
1838 int genphy_config_init(struct phy_device *phydev)
1841 __ETHTOOL_DECLARE_LINK_MODE_MASK(features) = { 0, };
1843 linkmode_set_bit_array(phy_basic_ports_array,
1844 ARRAY_SIZE(phy_basic_ports_array),
1846 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, features);
1847 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, features);
1849 /* Do we support autonegotiation? */
1850 val = phy_read(phydev, MII_BMSR);
1854 if (val & BMSR_ANEGCAPABLE)
1855 linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, features);
1857 if (val & BMSR_100FULL)
1858 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, features);
1859 if (val & BMSR_100HALF)
1860 linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, features);
1861 if (val & BMSR_10FULL)
1862 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, features);
1863 if (val & BMSR_10HALF)
1864 linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, features);
1866 if (val & BMSR_ESTATEN) {
1867 val = phy_read(phydev, MII_ESTATUS);
1871 if (val & ESTATUS_1000_TFULL)
1872 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1874 if (val & ESTATUS_1000_THALF)
1875 linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
1879 linkmode_and(phydev->supported, phydev->supported, features);
1880 linkmode_and(phydev->advertising, phydev->advertising, features);
1884 EXPORT_SYMBOL(genphy_config_init);
1886 /* This is used for the phy device which doesn't support the MMD extended
1887 * register access, but it does have side effect when we are trying to access
1888 * the MMD register via indirect method.
1890 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum)
1894 EXPORT_SYMBOL(genphy_read_mmd_unsupported);
1896 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
1897 u16 regnum, u16 val)
1901 EXPORT_SYMBOL(genphy_write_mmd_unsupported);
1903 int genphy_suspend(struct phy_device *phydev)
1905 return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN);
1907 EXPORT_SYMBOL(genphy_suspend);
1909 int genphy_resume(struct phy_device *phydev)
1911 return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN);
1913 EXPORT_SYMBOL(genphy_resume);
1915 int genphy_loopback(struct phy_device *phydev, bool enable)
1917 return phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK,
1918 enable ? BMCR_LOOPBACK : 0);
1920 EXPORT_SYMBOL(genphy_loopback);
1922 static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
1924 switch (max_speed) {
1926 linkmode_clear_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
1928 linkmode_clear_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
1932 linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
1934 linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1946 int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
1950 err = __set_phy_supported(phydev, max_speed);
1954 linkmode_copy(phydev->advertising, phydev->supported);
1958 EXPORT_SYMBOL(phy_set_max_speed);
1961 * phy_remove_link_mode - Remove a supported link mode
1962 * @phydev: phy_device structure to remove link mode from
1963 * @link_mode: Link mode to be removed
1965 * Description: Some MACs don't support all link modes which the PHY
1966 * does. e.g. a 1G MAC often does not support 1000Half. Add a helper
1967 * to remove a link mode.
1969 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode)
1971 linkmode_clear_bit(link_mode, phydev->supported);
1972 linkmode_copy(phydev->advertising, phydev->supported);
1974 EXPORT_SYMBOL(phy_remove_link_mode);
1977 * phy_support_sym_pause - Enable support of symmetrical pause
1978 * @phydev: target phy_device struct
1980 * Description: Called by the MAC to indicate is supports symmetrical
1981 * Pause, but not asym pause.
1983 void phy_support_sym_pause(struct phy_device *phydev)
1985 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
1986 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
1987 linkmode_copy(phydev->advertising, phydev->supported);
1989 EXPORT_SYMBOL(phy_support_sym_pause);
1992 * phy_support_asym_pause - Enable support of asym pause
1993 * @phydev: target phy_device struct
1995 * Description: Called by the MAC to indicate is supports Asym Pause.
1997 void phy_support_asym_pause(struct phy_device *phydev)
1999 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
2000 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
2001 linkmode_copy(phydev->advertising, phydev->supported);
2003 EXPORT_SYMBOL(phy_support_asym_pause);
2006 * phy_set_sym_pause - Configure symmetric Pause
2007 * @phydev: target phy_device struct
2008 * @rx: Receiver Pause is supported
2009 * @tx: Transmit Pause is supported
2010 * @autoneg: Auto neg should be used
2012 * Description: Configure advertised Pause support depending on if
2013 * receiver pause and pause auto neg is supported. Generally called
2014 * from the set_pauseparam .ndo.
2016 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
2019 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
2021 if (rx && tx && autoneg)
2022 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2025 linkmode_copy(phydev->advertising, phydev->supported);
2027 EXPORT_SYMBOL(phy_set_sym_pause);
2030 * phy_set_asym_pause - Configure Pause and Asym Pause
2031 * @phydev: target phy_device struct
2032 * @rx: Receiver Pause is supported
2033 * @tx: Transmit Pause is supported
2035 * Description: Configure advertised Pause support depending on if
2036 * transmit and receiver pause is supported. If there has been a
2037 * change in adverting, trigger a new autoneg. Generally called from
2038 * the set_pauseparam .ndo.
2040 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx)
2042 __ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv);
2044 linkmode_copy(oldadv, phydev->advertising);
2046 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2047 phydev->advertising);
2048 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2049 phydev->advertising);
2052 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2053 phydev->advertising);
2054 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2055 phydev->advertising);
2059 linkmode_change_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2060 phydev->advertising);
2062 if (!linkmode_equal(oldadv, phydev->advertising) &&
2064 phy_start_aneg(phydev);
2066 EXPORT_SYMBOL(phy_set_asym_pause);
2069 * phy_validate_pause - Test if the PHY/MAC support the pause configuration
2070 * @phydev: phy_device struct
2071 * @pp: requested pause configuration
2073 * Description: Test if the PHY/MAC combination supports the Pause
2074 * configuration the user is requesting. Returns True if it is
2075 * supported, false otherwise.
2077 bool phy_validate_pause(struct phy_device *phydev,
2078 struct ethtool_pauseparam *pp)
2080 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2081 phydev->supported) ||
2082 (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2083 phydev->supported) &&
2084 pp->rx_pause != pp->tx_pause))
2088 EXPORT_SYMBOL(phy_validate_pause);
2090 static void of_set_phy_supported(struct phy_device *phydev)
2092 struct device_node *node = phydev->mdio.dev.of_node;
2095 if (!IS_ENABLED(CONFIG_OF_MDIO))
2101 if (!of_property_read_u32(node, "max-speed", &max_speed))
2102 __set_phy_supported(phydev, max_speed);
2105 static void of_set_phy_eee_broken(struct phy_device *phydev)
2107 struct device_node *node = phydev->mdio.dev.of_node;
2110 if (!IS_ENABLED(CONFIG_OF_MDIO))
2116 if (of_property_read_bool(node, "eee-broken-100tx"))
2117 broken |= MDIO_EEE_100TX;
2118 if (of_property_read_bool(node, "eee-broken-1000t"))
2119 broken |= MDIO_EEE_1000T;
2120 if (of_property_read_bool(node, "eee-broken-10gt"))
2121 broken |= MDIO_EEE_10GT;
2122 if (of_property_read_bool(node, "eee-broken-1000kx"))
2123 broken |= MDIO_EEE_1000KX;
2124 if (of_property_read_bool(node, "eee-broken-10gkx4"))
2125 broken |= MDIO_EEE_10GKX4;
2126 if (of_property_read_bool(node, "eee-broken-10gkr"))
2127 broken |= MDIO_EEE_10GKR;
2129 phydev->eee_broken_modes = broken;
2132 static bool phy_drv_supports_irq(struct phy_driver *phydrv)
2134 return phydrv->config_intr && phydrv->ack_interrupt;
2138 * phy_probe - probe and init a PHY device
2139 * @dev: device to probe and init
2141 * Description: Take care of setting up the phy_device structure,
2142 * set the state to READY (the driver's init function should
2143 * set it to STARTING if needed).
2145 static int phy_probe(struct device *dev)
2147 struct phy_device *phydev = to_phy_device(dev);
2148 struct device_driver *drv = phydev->mdio.dev.driver;
2149 struct phy_driver *phydrv = to_phy_driver(drv);
2152 phydev->drv = phydrv;
2154 /* Disable the interrupt if the PHY doesn't support it
2155 * but the interrupt is still a valid one
2157 if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev))
2158 phydev->irq = PHY_POLL;
2160 if (phydrv->flags & PHY_IS_INTERNAL)
2161 phydev->is_internal = true;
2163 mutex_lock(&phydev->lock);
2165 /* Start out supporting everything. Eventually,
2166 * a controller will attach, and may modify one
2167 * or both of these values
2169 linkmode_copy(phydev->supported, phydrv->features);
2170 of_set_phy_supported(phydev);
2171 linkmode_copy(phydev->advertising, phydev->supported);
2173 /* Get the EEE modes we want to prohibit. We will ask
2174 * the PHY stop advertising these mode later on
2176 of_set_phy_eee_broken(phydev);
2178 /* The Pause Frame bits indicate that the PHY can support passing
2179 * pause frames. During autonegotiation, the PHYs will determine if
2180 * they should allow pause frames to pass. The MAC driver should then
2181 * use that result to determine whether to enable flow control via
2184 * Normally, PHY drivers should not set the Pause bits, and instead
2185 * allow phylib to do that. However, there may be some situations
2186 * (e.g. hardware erratum) where the driver wants to set only one
2189 if (test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydrv->features) ||
2190 test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydrv->features)) {
2191 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2193 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2195 if (test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydrv->features))
2196 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2198 if (test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2200 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2203 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2205 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2209 /* Set the state to READY by default */
2210 phydev->state = PHY_READY;
2212 if (phydev->drv->probe) {
2213 /* Deassert the reset signal */
2214 phy_device_reset(phydev, 0);
2216 err = phydev->drv->probe(phydev);
2218 /* Assert the reset signal */
2219 phy_device_reset(phydev, 1);
2223 mutex_unlock(&phydev->lock);
2228 static int phy_remove(struct device *dev)
2230 struct phy_device *phydev = to_phy_device(dev);
2232 cancel_delayed_work_sync(&phydev->state_queue);
2234 mutex_lock(&phydev->lock);
2235 phydev->state = PHY_DOWN;
2236 mutex_unlock(&phydev->lock);
2238 if (phydev->drv && phydev->drv->remove) {
2239 phydev->drv->remove(phydev);
2241 /* Assert the reset signal */
2242 phy_device_reset(phydev, 1);
2250 * phy_driver_register - register a phy_driver with the PHY layer
2251 * @new_driver: new phy_driver to register
2252 * @owner: module owning this PHY
2254 int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
2258 if (WARN_ON(!new_driver->features)) {
2259 pr_err("%s: Driver features are missing\n", new_driver->name);
2263 new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
2264 new_driver->mdiodrv.driver.name = new_driver->name;
2265 new_driver->mdiodrv.driver.bus = &mdio_bus_type;
2266 new_driver->mdiodrv.driver.probe = phy_probe;
2267 new_driver->mdiodrv.driver.remove = phy_remove;
2268 new_driver->mdiodrv.driver.owner = owner;
2270 /* The following works around an issue where the PHY driver doesn't bind
2271 * to the device, resulting in the genphy driver being used instead of
2272 * the dedicated driver. The root cause of the issue isn't known yet
2273 * and seems to be in the base driver core. Once this is fixed we may
2274 * remove this workaround.
2276 new_driver->mdiodrv.driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
2278 retval = driver_register(&new_driver->mdiodrv.driver);
2280 pr_err("%s: Error %d in registering driver\n",
2281 new_driver->name, retval);
2286 pr_debug("%s: Registered new driver\n", new_driver->name);
2290 EXPORT_SYMBOL(phy_driver_register);
2292 int phy_drivers_register(struct phy_driver *new_driver, int n,
2293 struct module *owner)
2297 for (i = 0; i < n; i++) {
2298 ret = phy_driver_register(new_driver + i, owner);
2301 phy_driver_unregister(new_driver + i);
2307 EXPORT_SYMBOL(phy_drivers_register);
2309 void phy_driver_unregister(struct phy_driver *drv)
2311 driver_unregister(&drv->mdiodrv.driver);
2313 EXPORT_SYMBOL(phy_driver_unregister);
2315 void phy_drivers_unregister(struct phy_driver *drv, int n)
2319 for (i = 0; i < n; i++)
2320 phy_driver_unregister(drv + i);
2322 EXPORT_SYMBOL(phy_drivers_unregister);
2324 static struct phy_driver genphy_driver = {
2325 .phy_id = 0xffffffff,
2326 .phy_id_mask = 0xffffffff,
2327 .name = "Generic PHY",
2328 .soft_reset = genphy_no_soft_reset,
2329 .config_init = genphy_config_init,
2330 .features = PHY_GBIT_ALL_PORTS_FEATURES,
2331 .aneg_done = genphy_aneg_done,
2332 .suspend = genphy_suspend,
2333 .resume = genphy_resume,
2334 .set_loopback = genphy_loopback,
2337 static int __init phy_init(void)
2341 rc = mdio_bus_init();
2347 rc = phy_driver_register(&genphy_10g_driver, THIS_MODULE);
2351 rc = phy_driver_register(&genphy_driver, THIS_MODULE);
2353 phy_driver_unregister(&genphy_10g_driver);
2361 static void __exit phy_exit(void)
2363 phy_driver_unregister(&genphy_10g_driver);
2364 phy_driver_unregister(&genphy_driver);
2368 subsys_initcall(phy_init);
2369 module_exit(phy_exit);