1 // SPDX-License-Identifier: GPL-2.0+
2 /* Framework for finding and configuring PHYs.
3 * Also contains generic PHY driver
7 * Copyright (c) 2004 Freescale Semiconductor, Inc.
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/acpi.h>
13 #include <linux/bitmap.h>
14 #include <linux/delay.h>
15 #include <linux/errno.h>
16 #include <linux/etherdevice.h>
17 #include <linux/ethtool.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
21 #include <linux/kernel.h>
22 #include <linux/list.h>
23 #include <linux/mdio.h>
24 #include <linux/mii.h>
26 #include <linux/module.h>
28 #include <linux/netdevice.h>
29 #include <linux/phy.h>
30 #include <linux/phylib_stubs.h>
31 #include <linux/phy_led_triggers.h>
32 #include <linux/phy_link_topology.h>
33 #include <linux/pse-pd/pse.h>
34 #include <linux/property.h>
35 #include <linux/ptp_clock_kernel.h>
36 #include <linux/rtnetlink.h>
37 #include <linux/sfp.h>
38 #include <linux/skbuff.h>
39 #include <linux/slab.h>
40 #include <linux/string.h>
41 #include <linux/uaccess.h>
42 #include <linux/unistd.h>
44 #include "phylib-internal.h"
47 MODULE_DESCRIPTION("PHY library");
48 MODULE_AUTHOR("Andy Fleming");
49 MODULE_LICENSE("GPL");
51 #define PHY_ANY_ID "MATCH ANY PHY"
52 #define PHY_ANY_UID 0xffffffff
55 struct list_head list;
56 char bus_id[MII_BUS_ID_SIZE + 3];
59 int (*run)(struct phy_device *phydev);
62 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;
63 EXPORT_SYMBOL_GPL(phy_basic_features);
65 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;
66 EXPORT_SYMBOL_GPL(phy_basic_t1_features);
68 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1s_p2mp_features) __ro_after_init;
69 EXPORT_SYMBOL_GPL(phy_basic_t1s_p2mp_features);
71 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;
72 EXPORT_SYMBOL_GPL(phy_gbit_features);
74 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;
75 EXPORT_SYMBOL_GPL(phy_gbit_fibre_features);
77 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;
78 EXPORT_SYMBOL_GPL(phy_10gbit_features);
80 const int phy_basic_ports_array[3] = {
81 ETHTOOL_LINK_MODE_Autoneg_BIT,
82 ETHTOOL_LINK_MODE_TP_BIT,
83 ETHTOOL_LINK_MODE_MII_BIT,
85 EXPORT_SYMBOL_GPL(phy_basic_ports_array);
87 static const int phy_all_ports_features_array[7] = {
88 ETHTOOL_LINK_MODE_Autoneg_BIT,
89 ETHTOOL_LINK_MODE_TP_BIT,
90 ETHTOOL_LINK_MODE_MII_BIT,
91 ETHTOOL_LINK_MODE_FIBRE_BIT,
92 ETHTOOL_LINK_MODE_AUI_BIT,
93 ETHTOOL_LINK_MODE_BNC_BIT,
94 ETHTOOL_LINK_MODE_Backplane_BIT,
97 static const int phy_10_100_features_array[4] = {
98 ETHTOOL_LINK_MODE_10baseT_Half_BIT,
99 ETHTOOL_LINK_MODE_10baseT_Full_BIT,
100 ETHTOOL_LINK_MODE_100baseT_Half_BIT,
101 ETHTOOL_LINK_MODE_100baseT_Full_BIT,
104 static const int phy_basic_t1_features_array[3] = {
105 ETHTOOL_LINK_MODE_TP_BIT,
106 ETHTOOL_LINK_MODE_10baseT1L_Full_BIT,
107 ETHTOOL_LINK_MODE_100baseT1_Full_BIT,
110 static const int phy_basic_t1s_p2mp_features_array[2] = {
111 ETHTOOL_LINK_MODE_TP_BIT,
112 ETHTOOL_LINK_MODE_10baseT1S_P2MP_Half_BIT,
115 static const int phy_gbit_features_array[2] = {
116 ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
117 ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
120 static const int phy_eee_cap1_features_array[] = {
121 ETHTOOL_LINK_MODE_100baseT_Full_BIT,
122 ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
123 ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
124 ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
125 ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
126 ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
129 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_eee_cap1_features) __ro_after_init;
130 EXPORT_SYMBOL_GPL(phy_eee_cap1_features);
132 static const int phy_eee_cap2_features_array[] = {
133 ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
134 ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
137 __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_eee_cap2_features) __ro_after_init;
138 EXPORT_SYMBOL_GPL(phy_eee_cap2_features);
140 static void features_init(void)
142 /* 10/100 half/full*/
143 linkmode_set_bit_array(phy_basic_ports_array,
144 ARRAY_SIZE(phy_basic_ports_array),
146 linkmode_set_bit_array(phy_10_100_features_array,
147 ARRAY_SIZE(phy_10_100_features_array),
151 linkmode_set_bit_array(phy_basic_t1_features_array,
152 ARRAY_SIZE(phy_basic_t1_features_array),
153 phy_basic_t1_features);
155 /* 10 half, P2MP, TP */
156 linkmode_set_bit_array(phy_basic_t1s_p2mp_features_array,
157 ARRAY_SIZE(phy_basic_t1s_p2mp_features_array),
158 phy_basic_t1s_p2mp_features);
160 /* 10/100 half/full + 1000 half/full */
161 linkmode_set_bit_array(phy_basic_ports_array,
162 ARRAY_SIZE(phy_basic_ports_array),
164 linkmode_set_bit_array(phy_10_100_features_array,
165 ARRAY_SIZE(phy_10_100_features_array),
167 linkmode_set_bit_array(phy_gbit_features_array,
168 ARRAY_SIZE(phy_gbit_features_array),
171 /* 10/100 half/full + 1000 half/full + fibre*/
172 linkmode_set_bit_array(phy_basic_ports_array,
173 ARRAY_SIZE(phy_basic_ports_array),
174 phy_gbit_fibre_features);
175 linkmode_set_bit_array(phy_10_100_features_array,
176 ARRAY_SIZE(phy_10_100_features_array),
177 phy_gbit_fibre_features);
178 linkmode_set_bit_array(phy_gbit_features_array,
179 ARRAY_SIZE(phy_gbit_features_array),
180 phy_gbit_fibre_features);
181 linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, phy_gbit_fibre_features);
183 /* 10/100 half/full + 1000 half/full + 10G full*/
184 linkmode_set_bit_array(phy_all_ports_features_array,
185 ARRAY_SIZE(phy_all_ports_features_array),
186 phy_10gbit_features);
187 linkmode_set_bit_array(phy_10_100_features_array,
188 ARRAY_SIZE(phy_10_100_features_array),
189 phy_10gbit_features);
190 linkmode_set_bit_array(phy_gbit_features_array,
191 ARRAY_SIZE(phy_gbit_features_array),
192 phy_10gbit_features);
193 linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
194 phy_10gbit_features);
196 linkmode_set_bit_array(phy_eee_cap1_features_array,
197 ARRAY_SIZE(phy_eee_cap1_features_array),
198 phy_eee_cap1_features);
199 linkmode_set_bit_array(phy_eee_cap2_features_array,
200 ARRAY_SIZE(phy_eee_cap2_features_array),
201 phy_eee_cap2_features);
205 void phy_device_free(struct phy_device *phydev)
207 put_device(&phydev->mdio.dev);
209 EXPORT_SYMBOL(phy_device_free);
211 static void phy_mdio_device_free(struct mdio_device *mdiodev)
213 struct phy_device *phydev;
215 phydev = container_of(mdiodev, struct phy_device, mdio);
216 phy_device_free(phydev);
219 static void phy_device_release(struct device *dev)
221 fwnode_handle_put(dev->fwnode);
222 kfree(to_phy_device(dev));
225 static void phy_mdio_device_remove(struct mdio_device *mdiodev)
227 struct phy_device *phydev;
229 phydev = container_of(mdiodev, struct phy_device, mdio);
230 phy_device_remove(phydev);
233 static struct phy_driver genphy_driver;
235 static LIST_HEAD(phy_fixup_list);
236 static DEFINE_MUTEX(phy_fixup_lock);
238 static bool phy_drv_wol_enabled(struct phy_device *phydev)
240 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
242 phy_ethtool_get_wol(phydev, &wol);
244 return wol.wolopts != 0;
247 static void phy_link_change(struct phy_device *phydev, bool up)
249 struct net_device *netdev = phydev->attached_dev;
252 netif_carrier_on(netdev);
254 netif_carrier_off(netdev);
255 phydev->adjust_link(netdev);
256 if (phydev->mii_ts && phydev->mii_ts->link_state)
257 phydev->mii_ts->link_state(phydev->mii_ts, phydev);
261 * phy_uses_state_machine - test whether consumer driver uses PAL state machine
262 * @phydev: the target PHY device structure
264 * Ultimately, this aims to indirectly determine whether the PHY is attached
265 * to a consumer which uses the state machine by calling phy_start() and
268 * When the PHY driver consumer uses phylib, it must have previously called
269 * phy_connect_direct() or one of its derivatives, so that phy_prepare_link()
270 * has set up a hook for monitoring state changes.
272 * When the PHY driver is used by the MAC driver consumer through phylink (the
273 * only other provider of a phy_link_change() method), using the PHY state
274 * machine is not optional.
276 * Return: true if consumer calls phy_start() and phy_stop(), false otherwise.
278 static bool phy_uses_state_machine(struct phy_device *phydev)
280 if (phydev->phy_link_change == phy_link_change)
281 return phydev->attached_dev && phydev->adjust_link;
283 /* phydev->phy_link_change is implicitly phylink_phy_change() */
287 static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
289 struct device_driver *drv = phydev->mdio.dev.driver;
290 struct phy_driver *phydrv = to_phy_driver(drv);
291 struct net_device *netdev = phydev->attached_dev;
293 if (!drv || !phydrv->suspend)
296 /* If the PHY on the mido bus is not attached but has WOL enabled
297 * we cannot suspend the PHY.
299 if (!netdev && phy_drv_wol_enabled(phydev))
302 /* PHY not attached? May suspend if the PHY has not already been
303 * suspended as part of a prior call to phy_disconnect() ->
304 * phy_detach() -> phy_suspend() because the parent netdev might be the
305 * MDIO bus driver and clock gated at this point.
310 if (netdev->ethtool->wol_enabled)
313 /* As long as not all affected network drivers support the
314 * wol_enabled flag, let's check for hints that WoL is enabled.
315 * Don't suspend PHY if the attached netdev parent may wake up.
316 * The parent may point to a PCI device, as in tg3 driver.
318 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
321 /* Also don't suspend PHY if the netdev itself may wakeup. This
322 * is the case for devices w/o underlaying pwr. mgmt. aware bus,
325 if (device_may_wakeup(&netdev->dev))
329 return !phydev->suspended;
332 static __maybe_unused int mdio_bus_phy_suspend(struct device *dev)
334 struct phy_device *phydev = to_phy_device(dev);
336 if (phydev->mac_managed_pm)
339 /* Wakeup interrupts may occur during the system sleep transition when
340 * the PHY is inaccessible. Set flag to postpone handling until the PHY
341 * has resumed. Wait for concurrent interrupt handler to complete.
343 if (phy_interrupt_is_valid(phydev)) {
344 phydev->irq_suspended = 1;
345 synchronize_irq(phydev->irq);
348 /* We must stop the state machine manually, otherwise it stops out of
349 * control, possibly with the phydev->lock held. Upon resume, netdev
350 * may call phy routines that try to grab the same lock, and that may
351 * lead to a deadlock.
353 if (phy_uses_state_machine(phydev))
354 phy_stop_machine(phydev);
356 if (!mdio_bus_phy_may_suspend(phydev))
359 phydev->suspended_by_mdio_bus = 1;
361 return phy_suspend(phydev);
364 static __maybe_unused int mdio_bus_phy_resume(struct device *dev)
366 struct phy_device *phydev = to_phy_device(dev);
369 if (phydev->mac_managed_pm)
372 if (!phydev->suspended_by_mdio_bus)
375 phydev->suspended_by_mdio_bus = 0;
377 /* If we managed to get here with the PHY state machine in a state
378 * neither PHY_HALTED, PHY_READY nor PHY_UP, this is an indication
379 * that something went wrong and we should most likely be using
380 * MAC managed PM, but we are not.
382 WARN_ON(phydev->state != PHY_HALTED && phydev->state != PHY_READY &&
383 phydev->state != PHY_UP);
385 ret = phy_init_hw(phydev);
389 ret = phy_resume(phydev);
393 if (phy_interrupt_is_valid(phydev)) {
394 phydev->irq_suspended = 0;
395 synchronize_irq(phydev->irq);
397 /* Rerun interrupts which were postponed by phy_interrupt()
398 * because they occurred during the system sleep transition.
400 if (phydev->irq_rerun) {
401 phydev->irq_rerun = 0;
402 enable_irq(phydev->irq);
403 irq_wake_thread(phydev->irq, phydev);
407 if (phy_uses_state_machine(phydev))
408 phy_start_machine(phydev);
413 static SIMPLE_DEV_PM_OPS(mdio_bus_phy_pm_ops, mdio_bus_phy_suspend,
414 mdio_bus_phy_resume);
417 * phy_register_fixup - creates a new phy_fixup and adds it to the list
418 * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID)
419 * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
420 * It can also be PHY_ANY_UID
421 * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
423 * @run: The actual code to be run when a matching PHY is found
425 static int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
426 int (*run)(struct phy_device *))
428 struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
433 strscpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
434 fixup->phy_uid = phy_uid;
435 fixup->phy_uid_mask = phy_uid_mask;
438 mutex_lock(&phy_fixup_lock);
439 list_add_tail(&fixup->list, &phy_fixup_list);
440 mutex_unlock(&phy_fixup_lock);
445 /* Registers a fixup to be run on any PHY with the UID in phy_uid */
446 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
447 int (*run)(struct phy_device *))
449 return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
451 EXPORT_SYMBOL(phy_register_fixup_for_uid);
453 /* Registers a fixup to be run on the PHY with id string bus_id */
454 int phy_register_fixup_for_id(const char *bus_id,
455 int (*run)(struct phy_device *))
457 return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
459 EXPORT_SYMBOL(phy_register_fixup_for_id);
462 * phy_unregister_fixup - remove a phy_fixup from the list
463 * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list
464 * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list
465 * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison
467 int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask)
469 struct list_head *pos, *n;
470 struct phy_fixup *fixup;
475 mutex_lock(&phy_fixup_lock);
476 list_for_each_safe(pos, n, &phy_fixup_list) {
477 fixup = list_entry(pos, struct phy_fixup, list);
479 if ((!strcmp(fixup->bus_id, bus_id)) &&
480 phy_id_compare(fixup->phy_uid, phy_uid, phy_uid_mask)) {
481 list_del(&fixup->list);
487 mutex_unlock(&phy_fixup_lock);
491 EXPORT_SYMBOL(phy_unregister_fixup);
493 /* Unregisters a fixup of any PHY with the UID in phy_uid */
494 int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask)
496 return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask);
498 EXPORT_SYMBOL(phy_unregister_fixup_for_uid);
500 /* Unregisters a fixup of the PHY with id string bus_id */
501 int phy_unregister_fixup_for_id(const char *bus_id)
503 return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff);
505 EXPORT_SYMBOL(phy_unregister_fixup_for_id);
507 /* Returns 1 if fixup matches phydev in bus_id and phy_uid.
508 * Fixups can be set to match any in one or more fields.
510 static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
512 if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
513 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
516 if (!phy_id_compare(phydev->phy_id, fixup->phy_uid,
517 fixup->phy_uid_mask))
518 if (fixup->phy_uid != PHY_ANY_UID)
524 /* Runs any matching fixups for this phydev */
525 static int phy_scan_fixups(struct phy_device *phydev)
527 struct phy_fixup *fixup;
529 mutex_lock(&phy_fixup_lock);
530 list_for_each_entry(fixup, &phy_fixup_list, list) {
531 if (phy_needs_fixup(phydev, fixup)) {
532 int err = fixup->run(phydev);
535 mutex_unlock(&phy_fixup_lock);
538 phydev->has_fixups = true;
541 mutex_unlock(&phy_fixup_lock);
547 * genphy_match_phy_device - match a PHY device with a PHY driver
548 * @phydev: target phy_device struct
549 * @phydrv: target phy_driver struct
551 * Description: Checks whether the given PHY device matches the specified
552 * PHY driver. For Clause 45 PHYs, iterates over the available device
553 * identifiers and compares them against the driver's expected PHY ID,
554 * applying the provided mask. For Clause 22 PHYs, a direct ID comparison
557 * Return: 1 if the PHY device matches the driver, 0 otherwise.
559 int genphy_match_phy_device(struct phy_device *phydev,
560 const struct phy_driver *phydrv)
562 if (phydev->is_c45) {
563 const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
566 for (i = 1; i < num_ids; i++) {
567 if (phydev->c45_ids.device_ids[i] == 0xffffffff)
570 if (phy_id_compare(phydev->c45_ids.device_ids[i],
571 phydrv->phy_id, phydrv->phy_id_mask))
578 return phy_id_compare(phydev->phy_id, phydrv->phy_id,
579 phydrv->phy_id_mask);
581 EXPORT_SYMBOL_GPL(genphy_match_phy_device);
583 static int phy_bus_match(struct device *dev, const struct device_driver *drv)
585 struct phy_device *phydev = to_phy_device(dev);
586 const struct phy_driver *phydrv = to_phy_driver(drv);
588 if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
591 if (phydrv->match_phy_device)
592 return phydrv->match_phy_device(phydev, phydrv);
594 return genphy_match_phy_device(phydev, phydrv);
598 phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
600 struct phy_device *phydev = to_phy_device(dev);
602 return sysfs_emit(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
604 static DEVICE_ATTR_RO(phy_id);
607 phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
609 struct phy_device *phydev = to_phy_device(dev);
610 const char *mode = NULL;
612 if (phydev->is_internal)
615 mode = phy_modes(phydev->interface);
617 return sysfs_emit(buf, "%s\n", mode);
619 static DEVICE_ATTR_RO(phy_interface);
622 phy_has_fixups_show(struct device *dev, struct device_attribute *attr,
625 struct phy_device *phydev = to_phy_device(dev);
627 return sysfs_emit(buf, "%d\n", phydev->has_fixups);
629 static DEVICE_ATTR_RO(phy_has_fixups);
631 static ssize_t phy_dev_flags_show(struct device *dev,
632 struct device_attribute *attr,
635 struct phy_device *phydev = to_phy_device(dev);
637 return sysfs_emit(buf, "0x%08x\n", phydev->dev_flags);
639 static DEVICE_ATTR_RO(phy_dev_flags);
641 static struct attribute *phy_dev_attrs[] = {
642 &dev_attr_phy_id.attr,
643 &dev_attr_phy_interface.attr,
644 &dev_attr_phy_has_fixups.attr,
645 &dev_attr_phy_dev_flags.attr,
648 ATTRIBUTE_GROUPS(phy_dev);
650 static const struct device_type mdio_bus_phy_type = {
652 .groups = phy_dev_groups,
653 .release = phy_device_release,
654 .pm = pm_ptr(&mdio_bus_phy_pm_ops),
657 static int phy_request_driver_module(struct phy_device *dev, u32 phy_id)
661 ret = request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT,
662 MDIO_ID_ARGS(phy_id));
663 /* We only check for failures in executing the usermode binary,
664 * not whether a PHY driver module exists for the PHY ID.
665 * Accept -ENOENT because this may occur in case no initramfs exists,
666 * then modprobe isn't available.
668 if (IS_ENABLED(CONFIG_MODULES) && ret < 0 && ret != -ENOENT) {
669 phydev_err(dev, "error %d loading PHY driver module for ID 0x%08lx\n",
670 ret, (unsigned long)phy_id);
677 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
679 struct phy_c45_device_ids *c45_ids)
681 struct phy_device *dev;
682 struct mdio_device *mdiodev;
685 /* We allocate the device, and initialize the default values */
686 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
688 return ERR_PTR(-ENOMEM);
690 mdiodev = &dev->mdio;
691 mdiodev->dev.parent = &bus->dev;
692 mdiodev->dev.bus = &mdio_bus_type;
693 mdiodev->dev.type = &mdio_bus_phy_type;
695 mdiodev->bus_match = phy_bus_match;
696 mdiodev->addr = addr;
697 mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
698 mdiodev->device_free = phy_mdio_device_free;
699 mdiodev->device_remove = phy_mdio_device_remove;
700 mdiodev->reset_state = -1;
702 dev->speed = SPEED_UNKNOWN;
703 dev->duplex = DUPLEX_UNKNOWN;
708 dev->interface = PHY_INTERFACE_MODE_GMII;
710 dev->autoneg = AUTONEG_ENABLE;
712 dev->pma_extable = -ENODATA;
713 dev->is_c45 = is_c45;
714 dev->phy_id = phy_id;
716 dev->c45_ids = *c45_ids;
717 dev->irq = bus->irq[addr];
719 dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
720 device_initialize(&mdiodev->dev);
722 dev->state = PHY_DOWN;
723 INIT_LIST_HEAD(&dev->leds);
725 mutex_init(&dev->lock);
726 INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
728 /* Request the appropriate module unconditionally; don't
729 * bother trying to do so only if it isn't already loaded,
730 * because that gets complicated. A hotplug event would have
731 * done an unconditional modprobe anyway.
732 * We don't do normal hotplug because it won't work for MDIO
733 * -- because it relies on the device staying around for long
734 * enough for the driver to get loaded. With MDIO, the NIC
735 * driver will get bored and give up as soon as it finds that
736 * there's no driver _already_ loaded.
738 if (is_c45 && c45_ids) {
739 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
742 for (i = 1; i < num_ids; i++) {
743 if (c45_ids->device_ids[i] == 0xffffffff)
746 ret = phy_request_driver_module(dev,
747 c45_ids->device_ids[i]);
752 ret = phy_request_driver_module(dev, phy_id);
756 put_device(&mdiodev->dev);
762 EXPORT_SYMBOL(phy_device_create);
764 /* phy_c45_probe_present - checks to see if a MMD is present in the package
765 * @bus: the target MII bus
766 * @prtad: PHY package address on the MII bus
767 * @devad: PHY device (MMD) address
769 * Read the MDIO_STAT2 register, and check whether a device is responding
772 * Returns: negative error number on bus access error, zero if no device
773 * is responding, or positive if a device is present.
775 static int phy_c45_probe_present(struct mii_bus *bus, int prtad, int devad)
779 stat2 = mdiobus_c45_read(bus, prtad, devad, MDIO_STAT2);
783 return (stat2 & MDIO_STAT2_DEVPRST) == MDIO_STAT2_DEVPRST_VAL;
786 /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
787 * @bus: the target MII bus
788 * @addr: PHY address on the MII bus
789 * @dev_addr: MMD address in the PHY.
790 * @devices_in_package: where to store the devices in package information.
792 * Description: reads devices in package registers of a MMD at @dev_addr
793 * from PHY at @addr on @bus.
795 * Returns: 0 on success, -EIO on failure.
797 static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
798 u32 *devices_in_package)
802 phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS2);
805 *devices_in_package = phy_reg << 16;
807 phy_reg = mdiobus_c45_read(bus, addr, dev_addr, MDIO_DEVS1);
810 *devices_in_package |= phy_reg;
816 * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
817 * @bus: the target MII bus
818 * @addr: PHY address on the MII bus
819 * @c45_ids: where to store the c45 ID information.
821 * Read the PHY "devices in package". If this appears to be valid, read
822 * the PHY identifiers for each device. Return the "devices in package"
823 * and identifiers in @c45_ids.
825 * Returns zero on success, %-EIO on bus access error, or %-ENODEV if
826 * the "devices in package" is invalid or no device responds.
828 static int get_phy_c45_ids(struct mii_bus *bus, int addr,
829 struct phy_c45_device_ids *c45_ids)
831 const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
835 /* Find first non-zero Devices In package. Device zero is reserved
836 * for 802.3 c45 complied PHYs, so don't probe it at first.
838 for (i = 1; i < MDIO_MMD_NUM && (devs_in_pkg == 0 ||
839 (devs_in_pkg & 0x1fffffff) == 0x1fffffff); i++) {
840 if (i == MDIO_MMD_VEND1 || i == MDIO_MMD_VEND2) {
841 /* Check that there is a device present at this
842 * address before reading the devices-in-package
843 * register to avoid reading garbage from the PHY.
844 * Some PHYs (88x3310) vendor space is not IEEE802.3
847 ret = phy_c45_probe_present(bus, addr, i);
849 /* returning -ENODEV doesn't stop bus
852 return (phy_reg == -EIO ||
853 phy_reg == -ENODEV) ? -ENODEV : -EIO;
858 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, &devs_in_pkg);
863 if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff) {
864 /* If mostly Fs, there is no device there, then let's probe
865 * MMD 0, as some 10G PHYs have zero Devices In package,
866 * e.g. Cortina CS4315/CS4340 PHY.
868 phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, &devs_in_pkg);
872 /* no device there, let's get out of here */
873 if ((devs_in_pkg & 0x1fffffff) == 0x1fffffff)
877 /* Now probe Device Identifiers for each device present. */
878 for (i = 1; i < num_ids; i++) {
879 if (!(devs_in_pkg & (1 << i)))
882 if (i == MDIO_MMD_VEND1 || i == MDIO_MMD_VEND2) {
883 /* Probe the "Device Present" bits for the vendor MMDs
884 * to ignore these if they do not contain IEEE 802.3
887 ret = phy_c45_probe_present(bus, addr, i);
895 phy_reg = mdiobus_c45_read(bus, addr, i, MII_PHYSID1);
898 c45_ids->device_ids[i] = phy_reg << 16;
900 phy_reg = mdiobus_c45_read(bus, addr, i, MII_PHYSID2);
903 c45_ids->device_ids[i] |= phy_reg;
906 c45_ids->devices_in_package = devs_in_pkg;
907 /* Bit 0 doesn't represent a device, it indicates c22 regs presence */
908 c45_ids->mmds_present = devs_in_pkg & ~BIT(0);
914 * get_phy_c22_id - reads the specified addr for its clause 22 ID.
915 * @bus: the target MII bus
916 * @addr: PHY address on the MII bus
917 * @phy_id: where to store the ID retrieved.
919 * Read the 802.3 clause 22 PHY ID from the PHY at @addr on the @bus,
920 * placing it in @phy_id. Return zero on successful read and the ID is
921 * valid, %-EIO on bus access error, or %-ENODEV if no device responds
924 static int get_phy_c22_id(struct mii_bus *bus, int addr, u32 *phy_id)
928 /* Grab the bits from PHYIR1, and put them in the upper half */
929 phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
931 /* returning -ENODEV doesn't stop bus scanning */
932 return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
935 *phy_id = phy_reg << 16;
937 /* Grab the bits from PHYIR2, and put them in the lower half */
938 phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
940 /* returning -ENODEV doesn't stop bus scanning */
941 return (phy_reg == -EIO || phy_reg == -ENODEV) ? -ENODEV : -EIO;
946 /* If the phy_id is mostly Fs, there is no device there */
947 if ((*phy_id & 0x1fffffff) == 0x1fffffff)
953 /* Extract the phy ID from the compatible string of the form
954 * ethernet-phy-idAAAA.BBBB.
956 int fwnode_get_phy_id(struct fwnode_handle *fwnode, u32 *phy_id)
958 unsigned int upper, lower;
962 ret = fwnode_property_read_string(fwnode, "compatible", &cp);
966 if (sscanf(cp, "ethernet-phy-id%4x.%4x", &upper, &lower) != 2)
969 *phy_id = ((upper & GENMASK(15, 0)) << 16) | (lower & GENMASK(15, 0));
972 EXPORT_SYMBOL(fwnode_get_phy_id);
975 * get_phy_device - reads the specified PHY device and returns its @phy_device
977 * @bus: the target MII bus
978 * @addr: PHY address on the MII bus
979 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
981 * Probe for a PHY at @addr on @bus.
983 * When probing for a clause 22 PHY, then read the ID registers. If we find
984 * a valid ID, allocate and return a &struct phy_device.
986 * When probing for a clause 45 PHY, read the "devices in package" registers.
987 * If the "devices in package" appears valid, read the ID registers for each
988 * MMD, allocate and return a &struct phy_device.
990 * Returns an allocated &struct phy_device on success, %-ENODEV if there is
991 * no PHY present, or %-EIO on bus access error.
993 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
995 struct phy_c45_device_ids c45_ids;
999 c45_ids.devices_in_package = 0;
1000 c45_ids.mmds_present = 0;
1001 memset(c45_ids.device_ids, 0xff, sizeof(c45_ids.device_ids));
1004 r = get_phy_c45_ids(bus, addr, &c45_ids);
1006 r = get_phy_c22_id(bus, addr, &phy_id);
1011 /* PHY device such as the Marvell Alaska 88E2110 will return a PHY ID
1012 * of 0 when probed using get_phy_c22_id() with no error. Proceed to
1013 * probe with C45 to see if we're able to get a valid PHY ID in the C45
1014 * space, if successful, create the C45 PHY device.
1016 if (!is_c45 && phy_id == 0 && bus->read_c45) {
1017 r = get_phy_c45_ids(bus, addr, &c45_ids);
1019 return phy_device_create(bus, addr, phy_id,
1023 return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
1025 EXPORT_SYMBOL(get_phy_device);
1028 * phy_device_register - Register the phy device on the MDIO bus
1029 * @phydev: phy_device structure to be added to the MDIO bus
1031 int phy_device_register(struct phy_device *phydev)
1035 err = mdiobus_register_device(&phydev->mdio);
1039 /* Deassert the reset signal */
1040 phy_device_reset(phydev, 0);
1042 /* Run all of the fixups for this PHY */
1043 err = phy_scan_fixups(phydev);
1045 phydev_err(phydev, "failed to initialize\n");
1049 err = device_add(&phydev->mdio.dev);
1051 phydev_err(phydev, "failed to add\n");
1058 /* Assert the reset signal */
1059 phy_device_reset(phydev, 1);
1061 mdiobus_unregister_device(&phydev->mdio);
1064 EXPORT_SYMBOL(phy_device_register);
1067 * phy_device_remove - Remove a previously registered phy device from the MDIO bus
1068 * @phydev: phy_device structure to remove
1070 * This doesn't free the phy_device itself, it merely reverses the effects
1071 * of phy_device_register(). Use phy_device_free() to free the device
1072 * after calling this function.
1074 void phy_device_remove(struct phy_device *phydev)
1076 unregister_mii_timestamper(phydev->mii_ts);
1077 pse_control_put(phydev->psec);
1079 device_del(&phydev->mdio.dev);
1081 /* Assert the reset signal */
1082 phy_device_reset(phydev, 1);
1084 mdiobus_unregister_device(&phydev->mdio);
1086 EXPORT_SYMBOL(phy_device_remove);
1089 * phy_get_c45_ids - Read 802.3-c45 IDs for phy device.
1090 * @phydev: phy_device structure to read 802.3-c45 IDs
1092 * Returns zero on success, %-EIO on bus access error, or %-ENODEV if
1093 * the "devices in package" is invalid.
1095 int phy_get_c45_ids(struct phy_device *phydev)
1097 return get_phy_c45_ids(phydev->mdio.bus, phydev->mdio.addr,
1100 EXPORT_SYMBOL(phy_get_c45_ids);
1103 * phy_find_first - finds the first PHY device on the bus
1104 * @bus: the target MII bus
1106 struct phy_device *phy_find_first(struct mii_bus *bus)
1108 struct phy_device *phydev;
1111 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
1112 phydev = mdiobus_get_phy(bus, addr);
1118 EXPORT_SYMBOL(phy_find_first);
1121 * phy_prepare_link - prepares the PHY layer to monitor link status
1122 * @phydev: target phy_device struct
1123 * @handler: callback function for link status change notifications
1125 * Description: Tells the PHY infrastructure to handle the
1126 * gory details on monitoring link status (whether through
1127 * polling or an interrupt), and to call back to the
1128 * connected device driver when the link status changes.
1129 * If you want to monitor your own link state, don't call
1132 static void phy_prepare_link(struct phy_device *phydev,
1133 void (*handler)(struct net_device *))
1135 phydev->adjust_link = handler;
1139 * phy_connect_direct - connect an ethernet device to a specific phy_device
1140 * @dev: the network device to connect
1141 * @phydev: the pointer to the phy device
1142 * @handler: callback function for state change notifications
1143 * @interface: PHY device's interface
1145 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
1146 void (*handler)(struct net_device *),
1147 phy_interface_t interface)
1154 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
1158 phy_prepare_link(phydev, handler);
1159 if (phy_interrupt_is_valid(phydev))
1160 phy_request_interrupt(phydev);
1164 EXPORT_SYMBOL(phy_connect_direct);
1167 * phy_connect - connect an ethernet device to a PHY device
1168 * @dev: the network device to connect
1169 * @bus_id: the id string of the PHY device to connect
1170 * @handler: callback function for state change notifications
1171 * @interface: PHY device's interface
1173 * Description: Convenience function for connecting ethernet
1174 * devices to PHY devices. The default behavior is for
1175 * the PHY infrastructure to handle everything, and only notify
1176 * the connected driver when the link status changes. If you
1177 * don't want, or can't use the provided functionality, you may
1178 * choose to call only the subset of functions which provide
1179 * the desired functionality.
1181 struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
1182 void (*handler)(struct net_device *),
1183 phy_interface_t interface)
1185 struct phy_device *phydev;
1189 /* Search the list of PHY devices on the mdio bus for the
1190 * PHY with the requested name
1192 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
1194 pr_err("PHY %s not found\n", bus_id);
1195 return ERR_PTR(-ENODEV);
1197 phydev = to_phy_device(d);
1199 rc = phy_connect_direct(dev, phydev, handler, interface);
1206 EXPORT_SYMBOL(phy_connect);
1209 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
1211 * @phydev: target phy_device struct
1213 void phy_disconnect(struct phy_device *phydev)
1215 if (phy_is_started(phydev))
1218 if (phy_interrupt_is_valid(phydev))
1219 phy_free_interrupt(phydev);
1221 phydev->adjust_link = NULL;
1225 EXPORT_SYMBOL(phy_disconnect);
1228 * phy_poll_reset - Safely wait until a PHY reset has properly completed
1229 * @phydev: The PHY device to poll
1231 * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
1232 * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR
1233 * register must be polled until the BMCR_RESET bit clears.
1235 * Furthermore, any attempts to write to PHY registers may have no effect
1236 * or even generate MDIO bus errors until this is complete.
1238 * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
1239 * standard and do not fully reset after the BMCR_RESET bit is set, and may
1240 * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an
1241 * effort to support such broken PHYs, this function is separate from the
1242 * standard phy_init_hw() which will zero all the other bits in the BMCR
1243 * and reapply all driver-specific and board-specific fixups.
1245 static int phy_poll_reset(struct phy_device *phydev)
1247 /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
1250 ret = phy_read_poll_timeout(phydev, MII_BMCR, val, !(val & BMCR_RESET),
1251 50000, 600000, true);
1254 /* Some chips (smsc911x) may still need up to another 1ms after the
1255 * BMCR_RESET bit is cleared before they are usable.
1261 int phy_init_hw(struct phy_device *phydev)
1265 /* Deassert the reset signal */
1266 phy_device_reset(phydev, 0);
1271 if (phydev->drv->soft_reset) {
1272 ret = phydev->drv->soft_reset(phydev);
1276 /* see comment in genphy_soft_reset for an explanation */
1277 phydev->suspended = 0;
1280 ret = phy_scan_fixups(phydev);
1284 phy_interface_zero(phydev->possible_interfaces);
1286 if (phydev->drv->config_init) {
1287 ret = phydev->drv->config_init(phydev);
1292 if (phydev->drv->config_intr) {
1293 ret = phydev->drv->config_intr(phydev);
1300 EXPORT_SYMBOL(phy_init_hw);
1302 void phy_attached_info(struct phy_device *phydev)
1304 phy_attached_print(phydev, NULL);
1306 EXPORT_SYMBOL(phy_attached_info);
1308 #define ATTACHED_FMT "attached PHY driver %s(mii_bus:phy_addr=%s, irq=%s)"
1309 char *phy_attached_info_irq(struct phy_device *phydev)
1314 switch(phydev->irq) {
1318 case PHY_MAC_INTERRUPT:
1322 snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
1327 return kasprintf(GFP_KERNEL, "%s", irq_str);
1329 EXPORT_SYMBOL(phy_attached_info_irq);
1331 void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
1333 const char *unbound = phydev->drv ? "" : "[unbound] ";
1334 char *irq_str = phy_attached_info_irq(phydev);
1337 phydev_info(phydev, ATTACHED_FMT "\n", unbound,
1338 phydev_name(phydev), irq_str);
1342 phydev_info(phydev, ATTACHED_FMT, unbound,
1343 phydev_name(phydev), irq_str);
1351 EXPORT_SYMBOL(phy_attached_print);
1353 static void phy_sysfs_create_links(struct phy_device *phydev)
1355 struct net_device *dev = phydev->attached_dev;
1361 err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
1366 err = sysfs_create_link_nowarn(&dev->dev.kobj,
1367 &phydev->mdio.dev.kobj,
1370 dev_err(&dev->dev, "could not add device link to %s err %d\n",
1371 kobject_name(&phydev->mdio.dev.kobj),
1373 /* non-fatal - some net drivers can use one netdevice
1374 * with more then one phy
1378 phydev->sysfs_links = true;
1382 phy_standalone_show(struct device *dev, struct device_attribute *attr,
1385 struct phy_device *phydev = to_phy_device(dev);
1387 return sysfs_emit(buf, "%d\n", !phydev->attached_dev);
1389 static DEVICE_ATTR_RO(phy_standalone);
1392 * phy_sfp_connect_phy - Connect the SFP module's PHY to the upstream PHY
1393 * @upstream: pointer to the upstream phy device
1394 * @phy: pointer to the SFP module's phy device
1396 * This helper allows keeping track of PHY devices on the link. It adds the
1397 * SFP module's phy to the phy namespace of the upstream phy
1399 * Return: 0 on success, otherwise a negative error code.
1401 int phy_sfp_connect_phy(void *upstream, struct phy_device *phy)
1403 struct phy_device *phydev = upstream;
1404 struct net_device *dev = phydev->attached_dev;
1407 return phy_link_topo_add_phy(dev, phy, PHY_UPSTREAM_PHY, phydev);
1411 EXPORT_SYMBOL(phy_sfp_connect_phy);
1414 * phy_sfp_disconnect_phy - Disconnect the SFP module's PHY from the upstream PHY
1415 * @upstream: pointer to the upstream phy device
1416 * @phy: pointer to the SFP module's phy device
1418 * This helper allows keeping track of PHY devices on the link. It removes the
1419 * SFP module's phy to the phy namespace of the upstream phy. As the module phy
1420 * will be destroyed, re-inserting the same module will add a new phy with a
1423 void phy_sfp_disconnect_phy(void *upstream, struct phy_device *phy)
1425 struct phy_device *phydev = upstream;
1426 struct net_device *dev = phydev->attached_dev;
1429 phy_link_topo_del_phy(dev, phy);
1431 EXPORT_SYMBOL(phy_sfp_disconnect_phy);
1434 * phy_sfp_attach - attach the SFP bus to the PHY upstream network device
1435 * @upstream: pointer to the phy device
1436 * @bus: sfp bus representing cage being attached
1438 * This is used to fill in the sfp_upstream_ops .attach member.
1440 void phy_sfp_attach(void *upstream, struct sfp_bus *bus)
1442 struct phy_device *phydev = upstream;
1444 if (phydev->attached_dev)
1445 phydev->attached_dev->sfp_bus = bus;
1446 phydev->sfp_bus_attached = true;
1448 EXPORT_SYMBOL(phy_sfp_attach);
1451 * phy_sfp_detach - detach the SFP bus from the PHY upstream network device
1452 * @upstream: pointer to the phy device
1453 * @bus: sfp bus representing cage being attached
1455 * This is used to fill in the sfp_upstream_ops .detach member.
1457 void phy_sfp_detach(void *upstream, struct sfp_bus *bus)
1459 struct phy_device *phydev = upstream;
1461 if (phydev->attached_dev)
1462 phydev->attached_dev->sfp_bus = NULL;
1463 phydev->sfp_bus_attached = false;
1465 EXPORT_SYMBOL(phy_sfp_detach);
1468 * phy_sfp_probe - probe for a SFP cage attached to this PHY device
1469 * @phydev: Pointer to phy_device
1470 * @ops: SFP's upstream operations
1472 int phy_sfp_probe(struct phy_device *phydev,
1473 const struct sfp_upstream_ops *ops)
1475 struct sfp_bus *bus;
1478 if (phydev->mdio.dev.fwnode) {
1479 bus = sfp_bus_find_fwnode(phydev->mdio.dev.fwnode);
1481 return PTR_ERR(bus);
1483 phydev->sfp_bus = bus;
1485 ret = sfp_bus_add_upstream(bus, phydev, ops);
1490 EXPORT_SYMBOL(phy_sfp_probe);
1492 static bool phy_drv_supports_irq(const struct phy_driver *phydrv)
1494 return phydrv->config_intr && phydrv->handle_interrupt;
1498 * phy_attach_direct - attach a network device to a given PHY device pointer
1499 * @dev: network device to attach
1500 * @phydev: Pointer to phy_device to attach
1501 * @flags: PHY device's dev_flags
1502 * @interface: PHY device's interface
1504 * Description: Called by drivers to attach to a particular PHY
1505 * device. The phy_device is found, and properly hooked up
1506 * to the phy_driver. If no driver is attached, then a
1507 * generic driver is used. The phy_device is given a ptr to
1508 * the attaching device, and given a callback for link status
1509 * change. The phy_device is returned to the attaching driver.
1510 * This function takes a reference on the phy device.
1512 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
1513 u32 flags, phy_interface_t interface)
1515 struct mii_bus *bus = phydev->mdio.bus;
1516 struct device *d = &phydev->mdio.dev;
1517 struct module *ndev_owner = NULL;
1518 bool using_genphy = false;
1521 /* For Ethernet device drivers that register their own MDIO bus, we
1522 * will have bus->owner match ndev_mod, so we do not want to increment
1523 * our own module->refcnt here, otherwise we would not be able to
1527 ndev_owner = dev->dev.parent->driver->owner;
1528 if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
1529 phydev_err(phydev, "failed to get the bus module\n");
1535 /* Assume that if there is no driver, that it doesn't
1536 * exist, and we should use the genphy driver.
1540 d->driver = &genphy_c45_driver.mdiodrv.driver;
1542 d->driver = &genphy_driver.mdiodrv.driver;
1544 using_genphy = true;
1547 if (!try_module_get(d->driver->owner)) {
1548 phydev_err(phydev, "failed to get the device driver module\n");
1550 goto error_put_device;
1554 err = d->driver->probe(d);
1556 err = device_bind_driver(d);
1559 goto error_module_put;
1562 if (phydev->attached_dev) {
1563 dev_err(&dev->dev, "PHY already attached\n");
1568 phydev->phy_link_change = phy_link_change;
1570 phydev->attached_dev = dev;
1571 dev->phydev = phydev;
1573 if (phydev->sfp_bus_attached)
1574 dev->sfp_bus = phydev->sfp_bus;
1576 err = phy_link_topo_add_phy(dev, phydev, PHY_UPSTREAM_MAC, dev);
1581 /* Some Ethernet drivers try to connect to a PHY device before
1582 * calling register_netdevice() -> netdev_register_kobject() and
1583 * does the dev->dev.kobj initialization. Here we only check for
1584 * success which indicates that the network device kobject is
1585 * ready. Once we do that we still need to keep track of whether
1586 * links were successfully set up or not for phy_detach() to
1587 * remove them accordingly.
1589 phydev->sysfs_links = false;
1591 phy_sysfs_create_links(phydev);
1593 if (!phydev->attached_dev) {
1594 err = sysfs_create_file(&phydev->mdio.dev.kobj,
1595 &dev_attr_phy_standalone.attr);
1597 phydev_err(phydev, "error creating 'phy_standalone' sysfs entry\n");
1600 phydev->dev_flags |= flags;
1602 phydev->interface = interface;
1604 phydev->state = PHY_READY;
1606 phydev->interrupts = PHY_INTERRUPT_DISABLED;
1608 /* PHYs can request to use poll mode even though they have an
1609 * associated interrupt line. This could be the case if they
1610 * detect a broken interrupt handling.
1612 if (phydev->dev_flags & PHY_F_NO_IRQ)
1613 phydev->irq = PHY_POLL;
1615 if (!phy_drv_supports_irq(phydev->drv) && phy_interrupt_is_valid(phydev))
1616 phydev->irq = PHY_POLL;
1618 /* Port is set to PORT_TP by default and the actual PHY driver will set
1619 * it to different value depending on the PHY configuration. If we have
1620 * the generic PHY driver we can't figure it out, thus set the old
1621 * legacy PORT_MII value.
1624 phydev->port = PORT_MII;
1626 /* Initial carrier state is off as the phy is about to be
1630 netif_carrier_off(phydev->attached_dev);
1632 /* Do initial configuration here, now that
1633 * we have certain key parameters
1634 * (dev_flags and interface)
1636 err = phy_init_hw(phydev);
1641 if (!phydev->is_on_sfp_module)
1642 phy_led_triggers_register(phydev);
1645 * If the external phy used by current mac interface is managed by
1646 * another mac interface, so we should create a device link between
1647 * phy dev and mac dev.
1649 if (dev && phydev->mdio.bus->parent && dev->dev.parent != phydev->mdio.bus->parent)
1650 phydev->devlink = device_link_add(dev->dev.parent, &phydev->mdio.dev,
1651 DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS);
1656 /* phy_detach() does all of the cleanup below */
1661 module_put(d->driver->owner);
1665 if (ndev_owner != bus->owner)
1666 module_put(bus->owner);
1669 EXPORT_SYMBOL(phy_attach_direct);
1672 * phy_attach - attach a network device to a particular PHY device
1673 * @dev: network device to attach
1674 * @bus_id: Bus ID of PHY device to attach
1675 * @interface: PHY device's interface
1677 * Description: Same as phy_attach_direct() except that a PHY bus_id
1678 * string is passed instead of a pointer to a struct phy_device.
1680 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1681 phy_interface_t interface)
1683 struct phy_device *phydev;
1688 return ERR_PTR(-EINVAL);
1690 /* Search the list of PHY devices on the mdio bus for the
1691 * PHY with the requested name
1693 d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
1695 pr_err("PHY %s not found\n", bus_id);
1696 return ERR_PTR(-ENODEV);
1698 phydev = to_phy_device(d);
1700 rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
1707 EXPORT_SYMBOL(phy_attach);
1709 static bool phy_driver_is_genphy_kind(struct phy_device *phydev,
1710 struct device_driver *driver)
1712 struct device *d = &phydev->mdio.dev;
1719 ret = d->driver == driver;
1725 bool phy_driver_is_genphy(struct phy_device *phydev)
1727 return phy_driver_is_genphy_kind(phydev,
1728 &genphy_driver.mdiodrv.driver);
1730 EXPORT_SYMBOL_GPL(phy_driver_is_genphy);
1732 bool phy_driver_is_genphy_10g(struct phy_device *phydev)
1734 return phy_driver_is_genphy_kind(phydev,
1735 &genphy_c45_driver.mdiodrv.driver);
1737 EXPORT_SYMBOL_GPL(phy_driver_is_genphy_10g);
1740 * phy_detach - detach a PHY device from its network device
1741 * @phydev: target phy_device struct
1743 * This detaches the phy device from its network device and the phy
1744 * driver, and drops the reference count taken in phy_attach_direct().
1746 void phy_detach(struct phy_device *phydev)
1748 struct net_device *dev = phydev->attached_dev;
1749 struct module *ndev_owner = NULL;
1750 struct mii_bus *bus;
1752 if (phydev->devlink) {
1753 device_link_del(phydev->devlink);
1754 phydev->devlink = NULL;
1757 if (phydev->sysfs_links) {
1759 sysfs_remove_link(&dev->dev.kobj, "phydev");
1760 sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
1763 if (!phydev->attached_dev)
1764 sysfs_remove_file(&phydev->mdio.dev.kobj,
1765 &dev_attr_phy_standalone.attr);
1767 phy_suspend(phydev);
1769 struct hwtstamp_provider *hwprov;
1771 hwprov = rtnl_dereference(dev->hwprov);
1772 /* Disable timestamp if it is the one selected */
1773 if (hwprov && hwprov->phydev == phydev) {
1774 rcu_assign_pointer(dev->hwprov, NULL);
1775 kfree_rcu(hwprov, rcu_head);
1778 phydev->attached_dev->phydev = NULL;
1779 phydev->attached_dev = NULL;
1780 phy_link_topo_del_phy(dev, phydev);
1782 phydev->phylink = NULL;
1784 if (!phydev->is_on_sfp_module)
1785 phy_led_triggers_unregister(phydev);
1787 if (phydev->mdio.dev.driver)
1788 module_put(phydev->mdio.dev.driver->owner);
1790 /* If the device had no specific driver before (i.e. - it
1791 * was using the generic driver), we unbind the device
1792 * from the generic driver so that there's a chance a
1793 * real driver could be loaded
1795 if (phy_driver_is_genphy(phydev) ||
1796 phy_driver_is_genphy_10g(phydev))
1797 device_release_driver(&phydev->mdio.dev);
1799 /* Assert the reset signal */
1800 phy_device_reset(phydev, 1);
1803 * The phydev might go away on the put_device() below, so avoid
1804 * a use-after-free bug by reading the underlying bus first.
1806 bus = phydev->mdio.bus;
1808 put_device(&phydev->mdio.dev);
1810 ndev_owner = dev->dev.parent->driver->owner;
1811 if (ndev_owner != bus->owner)
1812 module_put(bus->owner);
1814 EXPORT_SYMBOL(phy_detach);
1816 int phy_suspend(struct phy_device *phydev)
1818 struct net_device *netdev = phydev->attached_dev;
1819 const struct phy_driver *phydrv = phydev->drv;
1822 if (phydev->suspended || !phydrv)
1825 phydev->wol_enabled = phy_drv_wol_enabled(phydev) ||
1826 (netdev && netdev->ethtool->wol_enabled);
1827 /* If the device has WOL enabled, we cannot suspend the PHY */
1828 if (phydev->wol_enabled && !(phydrv->flags & PHY_ALWAYS_CALL_SUSPEND))
1831 if (!phydrv->suspend)
1834 ret = phydrv->suspend(phydev);
1836 phydev->suspended = true;
1840 EXPORT_SYMBOL(phy_suspend);
1842 int __phy_resume(struct phy_device *phydev)
1844 const struct phy_driver *phydrv = phydev->drv;
1847 lockdep_assert_held(&phydev->lock);
1849 if (!phydrv || !phydrv->resume)
1852 ret = phydrv->resume(phydev);
1854 phydev->suspended = false;
1858 EXPORT_SYMBOL(__phy_resume);
1860 int phy_resume(struct phy_device *phydev)
1864 mutex_lock(&phydev->lock);
1865 ret = __phy_resume(phydev);
1866 mutex_unlock(&phydev->lock);
1870 EXPORT_SYMBOL(phy_resume);
1873 * phy_reset_after_clk_enable - perform a PHY reset if needed
1874 * @phydev: target phy_device struct
1876 * Description: Some PHYs are known to need a reset after their refclk was
1877 * enabled. This function evaluates the flags and perform the reset if it's
1878 * needed. Returns < 0 on error, 0 if the phy wasn't reset and 1 if the phy
1881 int phy_reset_after_clk_enable(struct phy_device *phydev)
1883 if (!phydev || !phydev->drv)
1886 if (phydev->drv->flags & PHY_RST_AFTER_CLK_EN) {
1887 phy_device_reset(phydev, 1);
1888 phy_device_reset(phydev, 0);
1894 EXPORT_SYMBOL(phy_reset_after_clk_enable);
1896 /* Generic PHY support and helper functions */
1899 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
1900 * @phydev: target phy_device struct
1901 * @advert: auto-negotiation parameters to advertise
1903 * Description: Writes MII_ADVERTISE with the appropriate values,
1904 * after sanitizing the values to make sure we only advertise
1905 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
1906 * hasn't changed, and > 0 if it has changed.
1908 static int genphy_config_advert(struct phy_device *phydev,
1909 const unsigned long *advert)
1911 int err, bmsr, changed = 0;
1914 adv = linkmode_adv_to_mii_adv_t(advert);
1916 /* Setup standard advertisement */
1917 err = phy_modify_changed(phydev, MII_ADVERTISE,
1918 ADVERTISE_ALL | ADVERTISE_100BASE4 |
1919 ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM,
1926 bmsr = phy_read(phydev, MII_BMSR);
1930 /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
1931 * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
1934 if (!(bmsr & BMSR_ESTATEN))
1937 adv = linkmode_adv_to_mii_ctrl1000_t(advert);
1939 err = phy_modify_changed(phydev, MII_CTRL1000,
1940 ADVERTISE_1000FULL | ADVERTISE_1000HALF,
1951 * genphy_c37_config_advert - sanitize and advertise auto-negotiation parameters
1952 * @phydev: target phy_device struct
1954 * Description: Writes MII_ADVERTISE with the appropriate values,
1955 * after sanitizing the values to make sure we only advertise
1956 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
1957 * hasn't changed, and > 0 if it has changed. This function is intended
1958 * for Clause 37 1000Base-X mode.
1960 static int genphy_c37_config_advert(struct phy_device *phydev)
1964 /* Only allow advertising what this PHY supports */
1965 linkmode_and(phydev->advertising, phydev->advertising,
1968 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
1969 phydev->advertising))
1970 adv |= ADVERTISE_1000XFULL;
1971 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
1972 phydev->advertising))
1973 adv |= ADVERTISE_1000XPAUSE;
1974 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
1975 phydev->advertising))
1976 adv |= ADVERTISE_1000XPSE_ASYM;
1978 return phy_modify_changed(phydev, MII_ADVERTISE,
1979 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1980 ADVERTISE_1000XHALF | ADVERTISE_1000XPSE_ASYM,
1985 * genphy_setup_forced - configures/forces speed/duplex from @phydev
1986 * @phydev: target phy_device struct
1988 * Description: Configures MII_BMCR to force speed/duplex
1989 * to the values in phydev. Assumes that the values are valid.
1990 * Please see phy_sanitize_settings().
1992 int genphy_setup_forced(struct phy_device *phydev)
1997 phydev->asym_pause = 0;
1999 ctl = mii_bmcr_encode_fixed(phydev->speed, phydev->duplex);
2001 return phy_modify(phydev, MII_BMCR,
2002 ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
2004 EXPORT_SYMBOL(genphy_setup_forced);
2006 static int genphy_setup_master_slave(struct phy_device *phydev)
2010 if (!phydev->is_gigabit_capable)
2013 switch (phydev->master_slave_set) {
2014 case MASTER_SLAVE_CFG_MASTER_PREFERRED:
2015 ctl |= CTL1000_PREFER_MASTER;
2017 case MASTER_SLAVE_CFG_SLAVE_PREFERRED:
2019 case MASTER_SLAVE_CFG_MASTER_FORCE:
2020 ctl |= CTL1000_AS_MASTER;
2022 case MASTER_SLAVE_CFG_SLAVE_FORCE:
2023 ctl |= CTL1000_ENABLE_MASTER;
2025 case MASTER_SLAVE_CFG_UNKNOWN:
2026 case MASTER_SLAVE_CFG_UNSUPPORTED:
2029 phydev_warn(phydev, "Unsupported Master/Slave mode\n");
2033 return phy_modify_changed(phydev, MII_CTRL1000,
2034 (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER |
2035 CTL1000_PREFER_MASTER), ctl);
2038 int genphy_read_master_slave(struct phy_device *phydev)
2043 phydev->master_slave_get = MASTER_SLAVE_CFG_UNKNOWN;
2044 phydev->master_slave_state = MASTER_SLAVE_STATE_UNKNOWN;
2046 val = phy_read(phydev, MII_CTRL1000);
2050 if (val & CTL1000_ENABLE_MASTER) {
2051 if (val & CTL1000_AS_MASTER)
2052 cfg = MASTER_SLAVE_CFG_MASTER_FORCE;
2054 cfg = MASTER_SLAVE_CFG_SLAVE_FORCE;
2056 if (val & CTL1000_PREFER_MASTER)
2057 cfg = MASTER_SLAVE_CFG_MASTER_PREFERRED;
2059 cfg = MASTER_SLAVE_CFG_SLAVE_PREFERRED;
2062 val = phy_read(phydev, MII_STAT1000);
2066 if (val & LPA_1000MSFAIL) {
2067 state = MASTER_SLAVE_STATE_ERR;
2068 } else if (phydev->link) {
2069 /* this bits are valid only for active link */
2070 if (val & LPA_1000MSRES)
2071 state = MASTER_SLAVE_STATE_MASTER;
2073 state = MASTER_SLAVE_STATE_SLAVE;
2075 state = MASTER_SLAVE_STATE_UNKNOWN;
2078 phydev->master_slave_get = cfg;
2079 phydev->master_slave_state = state;
2083 EXPORT_SYMBOL(genphy_read_master_slave);
2086 * genphy_restart_aneg - Enable and Restart Autonegotiation
2087 * @phydev: target phy_device struct
2089 int genphy_restart_aneg(struct phy_device *phydev)
2091 /* Don't isolate the PHY if we're negotiating */
2092 return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
2093 BMCR_ANENABLE | BMCR_ANRESTART);
2095 EXPORT_SYMBOL(genphy_restart_aneg);
2098 * genphy_check_and_restart_aneg - Enable and restart auto-negotiation
2099 * @phydev: target phy_device struct
2100 * @restart: whether aneg restart is requested
2102 * Check, and restart auto-negotiation if needed.
2104 int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart)
2109 /* Advertisement hasn't changed, but maybe aneg was never on to
2110 * begin with? Or maybe phy was isolated?
2112 ret = phy_read(phydev, MII_BMCR);
2116 if (!(ret & BMCR_ANENABLE) || (ret & BMCR_ISOLATE))
2121 return genphy_restart_aneg(phydev);
2125 EXPORT_SYMBOL(genphy_check_and_restart_aneg);
2128 * __genphy_config_aneg - restart auto-negotiation or write BMCR
2129 * @phydev: target phy_device struct
2130 * @changed: whether autoneg is requested
2132 * Description: If auto-negotiation is enabled, we configure the
2133 * advertising, and then restart auto-negotiation. If it is not
2134 * enabled, then we write the BMCR.
2136 int __genphy_config_aneg(struct phy_device *phydev, bool changed)
2138 __ETHTOOL_DECLARE_LINK_MODE_MASK(fixed_advert);
2139 const struct link_capabilities *c;
2140 unsigned long *advert;
2143 err = genphy_c45_an_config_eee_aneg(phydev);
2149 err = genphy_setup_master_slave(phydev);
2155 if (phydev->autoneg == AUTONEG_ENABLE) {
2156 /* Only allow advertising what this PHY supports */
2157 linkmode_and(phydev->advertising, phydev->advertising,
2159 advert = phydev->advertising;
2160 } else if (phydev->speed < SPEED_1000) {
2161 return genphy_setup_forced(phydev);
2163 linkmode_zero(fixed_advert);
2165 c = phy_caps_lookup(phydev->speed, phydev->duplex,
2166 phydev->supported, true);
2168 linkmode_and(fixed_advert, phydev->supported,
2171 advert = fixed_advert;
2174 err = genphy_config_advert(phydev, advert);
2175 if (err < 0) /* error */
2180 return genphy_check_and_restart_aneg(phydev, changed);
2182 EXPORT_SYMBOL(__genphy_config_aneg);
2185 * genphy_c37_config_aneg - restart auto-negotiation or write BMCR
2186 * @phydev: target phy_device struct
2188 * Description: If auto-negotiation is enabled, we configure the
2189 * advertising, and then restart auto-negotiation. If it is not
2190 * enabled, then we write the BMCR. This function is intended
2191 * for use with Clause 37 1000Base-X mode.
2193 int genphy_c37_config_aneg(struct phy_device *phydev)
2197 if (phydev->autoneg != AUTONEG_ENABLE)
2198 return genphy_setup_forced(phydev);
2200 err = phy_modify(phydev, MII_BMCR, BMCR_SPEED1000 | BMCR_SPEED100,
2205 changed = genphy_c37_config_advert(phydev);
2206 if (changed < 0) /* error */
2210 /* Advertisement hasn't changed, but maybe aneg was never on to
2211 * begin with? Or maybe phy was isolated?
2213 int ctl = phy_read(phydev, MII_BMCR);
2218 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
2219 changed = 1; /* do restart aneg */
2222 /* Only restart aneg if we are advertising something different
2223 * than we were before.
2226 return genphy_restart_aneg(phydev);
2230 EXPORT_SYMBOL(genphy_c37_config_aneg);
2233 * genphy_aneg_done - return auto-negotiation status
2234 * @phydev: target phy_device struct
2236 * Description: Reads the status register and returns 0 either if
2237 * auto-negotiation is incomplete, or if there was an error.
2238 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
2240 int genphy_aneg_done(struct phy_device *phydev)
2242 int retval = phy_read(phydev, MII_BMSR);
2244 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
2246 EXPORT_SYMBOL(genphy_aneg_done);
2249 * genphy_update_link - update link status in @phydev
2250 * @phydev: target phy_device struct
2252 * Description: Update the value in phydev->link to reflect the
2253 * current link value. In order to do this, we need to read
2254 * the status register twice, keeping the second value.
2256 int genphy_update_link(struct phy_device *phydev)
2258 int status = 0, bmcr;
2260 bmcr = phy_read(phydev, MII_BMCR);
2264 /* Autoneg is being started, therefore disregard BMSR value and
2265 * report link as down.
2267 if (bmcr & BMCR_ANRESTART)
2270 /* The link state is latched low so that momentary link
2271 * drops can be detected. Do not double-read the status
2272 * in polling mode to detect such short link drops except
2273 * the link was already down.
2275 if (!phy_polling_mode(phydev) || !phydev->link) {
2276 status = phy_read(phydev, MII_BMSR);
2279 else if (status & BMSR_LSTATUS)
2283 /* Read link and autonegotiation status */
2284 status = phy_read(phydev, MII_BMSR);
2288 phydev->link = status & BMSR_LSTATUS ? 1 : 0;
2289 phydev->autoneg_complete = status & BMSR_ANEGCOMPLETE ? 1 : 0;
2291 /* Consider the case that autoneg was started and "aneg complete"
2292 * bit has been reset, but "link up" bit not yet.
2294 if (phydev->autoneg == AUTONEG_ENABLE && !phydev->autoneg_complete)
2299 EXPORT_SYMBOL(genphy_update_link);
2301 int genphy_read_lpa(struct phy_device *phydev)
2305 if (phydev->autoneg == AUTONEG_ENABLE) {
2306 if (!phydev->autoneg_complete) {
2307 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
2309 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, 0);
2313 if (phydev->is_gigabit_capable) {
2314 lpagb = phy_read(phydev, MII_STAT1000);
2318 if (lpagb & LPA_1000MSFAIL) {
2319 int adv = phy_read(phydev, MII_CTRL1000);
2324 if (adv & CTL1000_ENABLE_MASTER)
2325 phydev_err(phydev, "Master/Slave resolution failed, maybe conflicting manual settings?\n");
2327 phydev_err(phydev, "Master/Slave resolution failed\n");
2331 mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising,
2335 lpa = phy_read(phydev, MII_LPA);
2339 mii_lpa_mod_linkmode_lpa_t(phydev->lp_advertising, lpa);
2341 linkmode_zero(phydev->lp_advertising);
2346 EXPORT_SYMBOL(genphy_read_lpa);
2349 * genphy_read_status_fixed - read the link parameters for !aneg mode
2350 * @phydev: target phy_device struct
2352 * Read the current duplex and speed state for a PHY operating with
2353 * autonegotiation disabled.
2355 int genphy_read_status_fixed(struct phy_device *phydev)
2357 int bmcr = phy_read(phydev, MII_BMCR);
2362 if (bmcr & BMCR_FULLDPLX)
2363 phydev->duplex = DUPLEX_FULL;
2365 phydev->duplex = DUPLEX_HALF;
2367 if (bmcr & BMCR_SPEED1000)
2368 phydev->speed = SPEED_1000;
2369 else if (bmcr & BMCR_SPEED100)
2370 phydev->speed = SPEED_100;
2372 phydev->speed = SPEED_10;
2376 EXPORT_SYMBOL(genphy_read_status_fixed);
2379 * genphy_read_status - check the link status and update current link state
2380 * @phydev: target phy_device struct
2382 * Description: Check the link, then figure out the current state
2383 * by comparing what we advertise with what the link partner
2384 * advertises. Start by checking the gigabit possibilities,
2385 * then move on to 10/100.
2387 int genphy_read_status(struct phy_device *phydev)
2389 int err, old_link = phydev->link;
2391 /* Update the link, but return if there was an error */
2392 err = genphy_update_link(phydev);
2396 /* why bother the PHY if nothing can have changed */
2397 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
2400 phydev->master_slave_get = MASTER_SLAVE_CFG_UNSUPPORTED;
2401 phydev->master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
2402 phydev->speed = SPEED_UNKNOWN;
2403 phydev->duplex = DUPLEX_UNKNOWN;
2405 phydev->asym_pause = 0;
2407 if (phydev->is_gigabit_capable) {
2408 err = genphy_read_master_slave(phydev);
2413 err = genphy_read_lpa(phydev);
2417 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2418 phy_resolve_aneg_linkmode(phydev);
2419 } else if (phydev->autoneg == AUTONEG_DISABLE) {
2420 err = genphy_read_status_fixed(phydev);
2427 EXPORT_SYMBOL(genphy_read_status);
2430 * genphy_c37_read_status - check the link status and update current link state
2431 * @phydev: target phy_device struct
2432 * @changed: pointer where to store if link changed
2434 * Description: Check the link, then figure out the current state
2435 * by comparing what we advertise with what the link partner
2436 * advertises. This function is for Clause 37 1000Base-X mode.
2438 * If link has changed, @changed is set to true, false otherwise.
2440 int genphy_c37_read_status(struct phy_device *phydev, bool *changed)
2442 int lpa, err, old_link = phydev->link;
2444 /* Update the link, but return if there was an error */
2445 err = genphy_update_link(phydev);
2449 /* why bother the PHY if nothing can have changed */
2450 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link) {
2455 /* Signal link has changed */
2457 phydev->duplex = DUPLEX_UNKNOWN;
2459 phydev->asym_pause = 0;
2461 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
2462 lpa = phy_read(phydev, MII_LPA);
2466 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
2467 phydev->lp_advertising, lpa & LPA_LPACK);
2468 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2469 phydev->lp_advertising, lpa & LPA_1000XFULL);
2470 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2471 phydev->lp_advertising, lpa & LPA_1000XPAUSE);
2472 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2473 phydev->lp_advertising,
2474 lpa & LPA_1000XPAUSE_ASYM);
2476 phy_resolve_aneg_linkmode(phydev);
2477 } else if (phydev->autoneg == AUTONEG_DISABLE) {
2478 int bmcr = phy_read(phydev, MII_BMCR);
2483 if (bmcr & BMCR_FULLDPLX)
2484 phydev->duplex = DUPLEX_FULL;
2486 phydev->duplex = DUPLEX_HALF;
2491 EXPORT_SYMBOL(genphy_c37_read_status);
2494 * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
2495 * @phydev: target phy_device struct
2497 * Description: Perform a software PHY reset using the standard
2498 * BMCR_RESET bit and poll for the reset bit to be cleared.
2500 * Returns: 0 on success, < 0 on failure
2502 int genphy_soft_reset(struct phy_device *phydev)
2504 u16 res = BMCR_RESET;
2507 if (phydev->autoneg == AUTONEG_ENABLE)
2508 res |= BMCR_ANRESTART;
2510 ret = phy_modify(phydev, MII_BMCR, BMCR_ISOLATE, res);
2514 /* Clause 22 states that setting bit BMCR_RESET sets control registers
2515 * to their default value. Therefore the POWER DOWN bit is supposed to
2516 * be cleared after soft reset.
2518 phydev->suspended = 0;
2520 ret = phy_poll_reset(phydev);
2524 /* BMCR may be reset to defaults */
2525 if (phydev->autoneg == AUTONEG_DISABLE)
2526 ret = genphy_setup_forced(phydev);
2530 EXPORT_SYMBOL(genphy_soft_reset);
2532 irqreturn_t genphy_handle_interrupt_no_ack(struct phy_device *phydev)
2534 /* It seems there are cases where the interrupts are handled by another
2535 * entity (ie an IRQ controller embedded inside the PHY) and do not
2536 * need any other interraction from phylib. In this case, just trigger
2537 * the state machine directly.
2539 phy_trigger_machine(phydev);
2543 EXPORT_SYMBOL(genphy_handle_interrupt_no_ack);
2546 * genphy_read_abilities - read PHY abilities from Clause 22 registers
2547 * @phydev: target phy_device struct
2549 * Description: Reads the PHY's abilities and populates
2550 * phydev->supported accordingly.
2552 * Returns: 0 on success, < 0 on failure
2554 int genphy_read_abilities(struct phy_device *phydev)
2558 linkmode_set_bit_array(phy_basic_ports_array,
2559 ARRAY_SIZE(phy_basic_ports_array),
2562 val = phy_read(phydev, MII_BMSR);
2566 linkmode_mod_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phydev->supported,
2567 val & BMSR_ANEGCAPABLE);
2569 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, phydev->supported,
2570 val & BMSR_100FULL);
2571 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT, phydev->supported,
2572 val & BMSR_100HALF);
2573 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, phydev->supported,
2575 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, phydev->supported,
2578 if (val & BMSR_ESTATEN) {
2579 val = phy_read(phydev, MII_ESTATUS);
2583 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
2584 phydev->supported, val & ESTATUS_1000_TFULL);
2585 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
2586 phydev->supported, val & ESTATUS_1000_THALF);
2587 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
2588 phydev->supported, val & ESTATUS_1000_XFULL);
2591 /* This is optional functionality. If not supported, we may get an error
2592 * which should be ignored.
2594 genphy_c45_read_eee_abilities(phydev);
2598 EXPORT_SYMBOL(genphy_read_abilities);
2600 /* This is used for the phy device which doesn't support the MMD extended
2601 * register access, but it does have side effect when we are trying to access
2602 * the MMD register via indirect method.
2604 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad, u16 regnum)
2608 EXPORT_SYMBOL(genphy_read_mmd_unsupported);
2610 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
2611 u16 regnum, u16 val)
2615 EXPORT_SYMBOL(genphy_write_mmd_unsupported);
2617 int genphy_suspend(struct phy_device *phydev)
2619 return phy_set_bits(phydev, MII_BMCR, BMCR_PDOWN);
2621 EXPORT_SYMBOL(genphy_suspend);
2623 int genphy_resume(struct phy_device *phydev)
2625 return phy_clear_bits(phydev, MII_BMCR, BMCR_PDOWN);
2627 EXPORT_SYMBOL(genphy_resume);
2629 int genphy_loopback(struct phy_device *phydev, bool enable, int speed)
2632 u16 ctl = BMCR_LOOPBACK;
2635 if (speed == SPEED_10 || speed == SPEED_100 ||
2636 speed == SPEED_1000)
2637 phydev->speed = speed;
2641 ctl |= mii_bmcr_encode_fixed(phydev->speed, phydev->duplex);
2643 phy_modify(phydev, MII_BMCR, ~0, ctl);
2645 ret = phy_read_poll_timeout(phydev, MII_BMSR, val,
2647 5000, 500000, true);
2651 phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK, 0);
2653 phy_config_aneg(phydev);
2658 EXPORT_SYMBOL(genphy_loopback);
2661 * phy_remove_link_mode - Remove a supported link mode
2662 * @phydev: phy_device structure to remove link mode from
2663 * @link_mode: Link mode to be removed
2665 * Description: Some MACs don't support all link modes which the PHY
2666 * does. e.g. a 1G MAC often does not support 1000Half. Add a helper
2667 * to remove a link mode.
2669 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode)
2671 linkmode_clear_bit(link_mode, phydev->supported);
2672 phy_advertise_supported(phydev);
2674 EXPORT_SYMBOL(phy_remove_link_mode);
2676 static void phy_copy_pause_bits(unsigned long *dst, unsigned long *src)
2678 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, dst,
2679 linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, src));
2680 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, dst,
2681 linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, src));
2685 * phy_advertise_supported - Advertise all supported modes
2686 * @phydev: target phy_device struct
2688 * Description: Called to advertise all supported modes, doesn't touch
2689 * pause mode advertising.
2691 void phy_advertise_supported(struct phy_device *phydev)
2693 __ETHTOOL_DECLARE_LINK_MODE_MASK(new);
2695 linkmode_copy(new, phydev->supported);
2696 phy_copy_pause_bits(new, phydev->advertising);
2697 linkmode_copy(phydev->advertising, new);
2699 EXPORT_SYMBOL(phy_advertise_supported);
2702 * phy_advertise_eee_all - Advertise all supported EEE modes
2703 * @phydev: target phy_device struct
2705 * Description: Per default phylib preserves the EEE advertising at the time of
2706 * phy probing, which might be a subset of the supported EEE modes. Use this
2707 * function when all supported EEE modes should be advertised. This does not
2708 * trigger auto-negotiation, so must be called before phy_start()/
2709 * phylink_start() which will start auto-negotiation.
2711 void phy_advertise_eee_all(struct phy_device *phydev)
2713 linkmode_copy(phydev->advertising_eee, phydev->supported_eee);
2715 EXPORT_SYMBOL_GPL(phy_advertise_eee_all);
2718 * phy_support_eee - Set initial EEE policy configuration
2719 * @phydev: Target phy_device struct
2721 * This function configures the initial policy for Energy Efficient Ethernet
2722 * (EEE) on the specified PHY device, influencing that EEE capabilities are
2723 * advertised before the link is established. It should be called during PHY
2724 * registration by the MAC driver and/or the PHY driver (for SmartEEE PHYs)
2725 * if MAC supports LPI or PHY is capable to compensate missing LPI functionality
2728 * The function sets default EEE policy parameters, including preparing the PHY
2729 * to advertise EEE capabilities based on hardware support.
2731 * It also sets the expected configuration for Low Power Idle (LPI) in the MAC
2732 * driver. If the PHY framework determines that both local and remote
2733 * advertisements support EEE, and the negotiated link mode is compatible with
2734 * EEE, it will set enable_tx_lpi = true. The MAC driver is expected to act on
2735 * this setting by enabling the LPI timer if enable_tx_lpi is set.
2737 void phy_support_eee(struct phy_device *phydev)
2739 linkmode_copy(phydev->advertising_eee, phydev->supported_eee);
2740 phydev->eee_cfg.tx_lpi_enabled = true;
2741 phydev->eee_cfg.eee_enabled = true;
2743 EXPORT_SYMBOL(phy_support_eee);
2746 * phy_disable_eee - Disable EEE for the PHY
2747 * @phydev: Target phy_device struct
2749 * This function is used by MAC drivers for MAC's which don't support EEE.
2750 * It disables EEE on the PHY layer.
2752 void phy_disable_eee(struct phy_device *phydev)
2754 linkmode_zero(phydev->advertising_eee);
2755 phydev->eee_cfg.tx_lpi_enabled = false;
2756 phydev->eee_cfg.eee_enabled = false;
2757 /* don't let userspace re-enable EEE advertisement */
2758 linkmode_fill(phydev->eee_disabled_modes);
2760 EXPORT_SYMBOL_GPL(phy_disable_eee);
2763 * phy_support_sym_pause - Enable support of symmetrical pause
2764 * @phydev: target phy_device struct
2766 * Description: Called by the MAC to indicate is supports symmetrical
2767 * Pause, but not asym pause.
2769 void phy_support_sym_pause(struct phy_device *phydev)
2771 linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported);
2772 phy_copy_pause_bits(phydev->advertising, phydev->supported);
2774 EXPORT_SYMBOL(phy_support_sym_pause);
2777 * phy_support_asym_pause - Enable support of asym pause
2778 * @phydev: target phy_device struct
2780 * Description: Called by the MAC to indicate is supports Asym Pause.
2782 void phy_support_asym_pause(struct phy_device *phydev)
2784 phy_copy_pause_bits(phydev->advertising, phydev->supported);
2786 EXPORT_SYMBOL(phy_support_asym_pause);
2789 * phy_set_sym_pause - Configure symmetric Pause
2790 * @phydev: target phy_device struct
2791 * @rx: Receiver Pause is supported
2792 * @tx: Transmit Pause is supported
2793 * @autoneg: Auto neg should be used
2795 * Description: Configure advertised Pause support depending on if
2796 * receiver pause and pause auto neg is supported. Generally called
2797 * from the set_pauseparam .ndo.
2799 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
2802 linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported);
2804 if (rx && tx && autoneg)
2805 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2808 linkmode_copy(phydev->advertising, phydev->supported);
2810 EXPORT_SYMBOL(phy_set_sym_pause);
2813 * phy_set_asym_pause - Configure Pause and Asym Pause
2814 * @phydev: target phy_device struct
2815 * @rx: Receiver Pause is supported
2816 * @tx: Transmit Pause is supported
2818 * Description: Configure advertised Pause support depending on if
2819 * transmit and receiver pause is supported. If there has been a
2820 * change in adverting, trigger a new autoneg. Generally called from
2821 * the set_pauseparam .ndo.
2823 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx)
2825 __ETHTOOL_DECLARE_LINK_MODE_MASK(oldadv);
2827 linkmode_copy(oldadv, phydev->advertising);
2828 linkmode_set_pause(phydev->advertising, tx, rx);
2830 if (!linkmode_equal(oldadv, phydev->advertising) &&
2832 phy_start_aneg(phydev);
2834 EXPORT_SYMBOL(phy_set_asym_pause);
2837 * phy_validate_pause - Test if the PHY/MAC support the pause configuration
2838 * @phydev: phy_device struct
2839 * @pp: requested pause configuration
2841 * Description: Test if the PHY/MAC combination supports the Pause
2842 * configuration the user is requesting. Returns True if it is
2843 * supported, false otherwise.
2845 bool phy_validate_pause(struct phy_device *phydev,
2846 struct ethtool_pauseparam *pp)
2848 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2849 phydev->supported) && pp->rx_pause)
2852 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2853 phydev->supported) &&
2854 pp->rx_pause != pp->tx_pause)
2859 EXPORT_SYMBOL(phy_validate_pause);
2862 * phy_get_pause - resolve negotiated pause modes
2863 * @phydev: phy_device struct
2864 * @tx_pause: pointer to bool to indicate whether transmit pause should be
2866 * @rx_pause: pointer to bool to indicate whether receive pause should be
2869 * Resolve and return the flow control modes according to the negotiation
2870 * result. This includes checking that we are operating in full duplex mode.
2871 * See linkmode_resolve_pause() for further details.
2873 void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause)
2875 if (phydev->duplex != DUPLEX_FULL) {
2881 return linkmode_resolve_pause(phydev->advertising,
2882 phydev->lp_advertising,
2883 tx_pause, rx_pause);
2885 EXPORT_SYMBOL(phy_get_pause);
2887 #if IS_ENABLED(CONFIG_OF_MDIO)
2888 static int phy_get_u32_property(struct device *dev, const char *name, u32 *val)
2890 return device_property_read_u32(dev, name, val);
2893 static int phy_get_u32_property(struct device *dev, const char *name, u32 *val)
2900 * phy_get_internal_delay - returns the index of the internal delay
2901 * @phydev: phy_device struct
2902 * @dev: pointer to the devices device struct
2903 * @delay_values: array of delays the PHY supports
2904 * @size: the size of the delay array
2905 * @is_rx: boolean to indicate to get the rx internal delay
2907 * Returns the index within the array of internal delay passed in.
2908 * If the device property is not present then the interface type is checked
2909 * if the interface defines use of internal delay then a 1 is returned otherwise
2911 * The array must be in ascending order. If PHY does not have an ascending order
2912 * array then size = 0 and the value of the delay property is returned.
2913 * Return -EINVAL if the delay is invalid or cannot be found.
2915 s32 phy_get_internal_delay(struct phy_device *phydev, struct device *dev,
2916 const int *delay_values, int size, bool is_rx)
2922 ret = phy_get_u32_property(dev, "rx-internal-delay-ps", &delay);
2923 if (ret < 0 && size == 0) {
2924 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
2925 phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
2932 ret = phy_get_u32_property(dev, "tx-internal-delay-ps", &delay);
2933 if (ret < 0 && size == 0) {
2934 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
2935 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
2948 if (delay < delay_values[0] || delay > delay_values[size - 1]) {
2949 phydev_err(phydev, "Delay %d is out of range\n", delay);
2953 if (delay == delay_values[0])
2956 for (i = 1; i < size; i++) {
2957 if (delay == delay_values[i])
2960 /* Find an approximate index by looking up the table */
2961 if (delay > delay_values[i - 1] &&
2962 delay < delay_values[i]) {
2963 if (delay - delay_values[i - 1] <
2964 delay_values[i] - delay)
2971 phydev_err(phydev, "error finding internal delay index for %d\n",
2976 EXPORT_SYMBOL(phy_get_internal_delay);
2979 * phy_get_tx_amplitude_gain - stores tx amplitude gain in @val
2980 * @phydev: phy_device struct
2981 * @dev: pointer to the devices device struct
2982 * @linkmode: linkmode for which the tx amplitude gain should be retrieved
2983 * @val: tx amplitude gain
2985 * Returns: 0 on success, < 0 on failure
2987 int phy_get_tx_amplitude_gain(struct phy_device *phydev, struct device *dev,
2988 enum ethtool_link_mode_bit_indices linkmode,
2992 case ETHTOOL_LINK_MODE_100baseT_Full_BIT:
2993 return phy_get_u32_property(dev,
2994 "tx-amplitude-100base-tx-percent",
3000 EXPORT_SYMBOL_GPL(phy_get_tx_amplitude_gain);
3003 * phy_get_mac_termination - stores MAC termination in @val
3004 * @phydev: phy_device struct
3005 * @dev: pointer to the devices device struct
3006 * @val: MAC termination
3008 * Returns: 0 on success, < 0 on failure
3010 int phy_get_mac_termination(struct phy_device *phydev, struct device *dev,
3013 return phy_get_u32_property(dev, "mac-termination-ohms", val);
3015 EXPORT_SYMBOL_GPL(phy_get_mac_termination);
3017 static int phy_led_set_brightness(struct led_classdev *led_cdev,
3018 enum led_brightness value)
3020 struct phy_led *phyled = to_phy_led(led_cdev);
3021 struct phy_device *phydev = phyled->phydev;
3024 mutex_lock(&phydev->lock);
3025 err = phydev->drv->led_brightness_set(phydev, phyled->index, value);
3026 mutex_unlock(&phydev->lock);
3031 static int phy_led_blink_set(struct led_classdev *led_cdev,
3032 unsigned long *delay_on,
3033 unsigned long *delay_off)
3035 struct phy_led *phyled = to_phy_led(led_cdev);
3036 struct phy_device *phydev = phyled->phydev;
3039 mutex_lock(&phydev->lock);
3040 err = phydev->drv->led_blink_set(phydev, phyled->index,
3041 delay_on, delay_off);
3042 mutex_unlock(&phydev->lock);
3047 static __maybe_unused struct device *
3048 phy_led_hw_control_get_device(struct led_classdev *led_cdev)
3050 struct phy_led *phyled = to_phy_led(led_cdev);
3051 struct phy_device *phydev = phyled->phydev;
3053 if (phydev->attached_dev)
3054 return &phydev->attached_dev->dev;
3058 static int __maybe_unused
3059 phy_led_hw_control_get(struct led_classdev *led_cdev,
3060 unsigned long *rules)
3062 struct phy_led *phyled = to_phy_led(led_cdev);
3063 struct phy_device *phydev = phyled->phydev;
3066 mutex_lock(&phydev->lock);
3067 err = phydev->drv->led_hw_control_get(phydev, phyled->index, rules);
3068 mutex_unlock(&phydev->lock);
3073 static int __maybe_unused
3074 phy_led_hw_control_set(struct led_classdev *led_cdev,
3075 unsigned long rules)
3077 struct phy_led *phyled = to_phy_led(led_cdev);
3078 struct phy_device *phydev = phyled->phydev;
3081 mutex_lock(&phydev->lock);
3082 err = phydev->drv->led_hw_control_set(phydev, phyled->index, rules);
3083 mutex_unlock(&phydev->lock);
3088 static __maybe_unused int phy_led_hw_is_supported(struct led_classdev *led_cdev,
3089 unsigned long rules)
3091 struct phy_led *phyled = to_phy_led(led_cdev);
3092 struct phy_device *phydev = phyled->phydev;
3095 mutex_lock(&phydev->lock);
3096 err = phydev->drv->led_hw_is_supported(phydev, phyled->index, rules);
3097 mutex_unlock(&phydev->lock);
3102 static void phy_leds_unregister(struct phy_device *phydev)
3104 struct phy_led *phyled, *tmp;
3106 list_for_each_entry_safe(phyled, tmp, &phydev->leds, list) {
3107 led_classdev_unregister(&phyled->led_cdev);
3108 list_del(&phyled->list);
3112 static int of_phy_led(struct phy_device *phydev,
3113 struct device_node *led)
3115 struct device *dev = &phydev->mdio.dev;
3116 struct led_init_data init_data = {};
3117 struct led_classdev *cdev;
3118 unsigned long modes = 0;
3119 struct phy_led *phyled;
3123 phyled = devm_kzalloc(dev, sizeof(*phyled), GFP_KERNEL);
3127 cdev = &phyled->led_cdev;
3128 phyled->phydev = phydev;
3130 err = of_property_read_u32(led, "reg", &index);
3136 if (of_property_read_bool(led, "active-high"))
3137 set_bit(PHY_LED_ACTIVE_HIGH, &modes);
3138 if (of_property_read_bool(led, "active-low"))
3139 set_bit(PHY_LED_ACTIVE_LOW, &modes);
3140 if (of_property_read_bool(led, "inactive-high-impedance"))
3141 set_bit(PHY_LED_INACTIVE_HIGH_IMPEDANCE, &modes);
3143 if (WARN_ON(modes & BIT(PHY_LED_ACTIVE_LOW) &&
3144 modes & BIT(PHY_LED_ACTIVE_HIGH)))
3148 /* Return error if asked to set polarity modes but not supported */
3149 if (!phydev->drv->led_polarity_set)
3152 err = phydev->drv->led_polarity_set(phydev, index, modes);
3157 phyled->index = index;
3158 if (phydev->drv->led_brightness_set)
3159 cdev->brightness_set_blocking = phy_led_set_brightness;
3160 if (phydev->drv->led_blink_set)
3161 cdev->blink_set = phy_led_blink_set;
3163 #ifdef CONFIG_LEDS_TRIGGERS
3164 if (phydev->drv->led_hw_is_supported &&
3165 phydev->drv->led_hw_control_set &&
3166 phydev->drv->led_hw_control_get) {
3167 cdev->hw_control_is_supported = phy_led_hw_is_supported;
3168 cdev->hw_control_set = phy_led_hw_control_set;
3169 cdev->hw_control_get = phy_led_hw_control_get;
3170 cdev->hw_control_trigger = "netdev";
3173 cdev->hw_control_get_device = phy_led_hw_control_get_device;
3175 cdev->max_brightness = 1;
3176 init_data.devicename = dev_name(&phydev->mdio.dev);
3177 init_data.fwnode = of_fwnode_handle(led);
3178 init_data.devname_mandatory = true;
3180 err = led_classdev_register_ext(dev, cdev, &init_data);
3184 list_add(&phyled->list, &phydev->leds);
3189 static int of_phy_leds(struct phy_device *phydev)
3191 struct device_node *node = phydev->mdio.dev.of_node;
3192 struct device_node *leds;
3195 if (!IS_ENABLED(CONFIG_OF_MDIO))
3201 leds = of_get_child_by_name(node, "leds");
3205 /* Check if the PHY driver have at least an OP to
3208 if (!(phydev->drv->led_brightness_set ||
3209 phydev->drv->led_blink_set ||
3210 phydev->drv->led_hw_control_set)) {
3211 phydev_dbg(phydev, "ignoring leds node defined with no PHY driver support\n");
3215 for_each_available_child_of_node_scoped(leds, led) {
3216 err = of_phy_led(phydev, led);
3219 phy_leds_unregister(phydev);
3230 * fwnode_mdio_find_device - Given a fwnode, find the mdio_device
3231 * @fwnode: pointer to the mdio_device's fwnode
3233 * If successful, returns a pointer to the mdio_device with the embedded
3234 * struct device refcount incremented by one, or NULL on failure.
3235 * The caller should call put_device() on the mdio_device after its use.
3237 struct mdio_device *fwnode_mdio_find_device(struct fwnode_handle *fwnode)
3244 d = bus_find_device_by_fwnode(&mdio_bus_type, fwnode);
3248 return to_mdio_device(d);
3250 EXPORT_SYMBOL(fwnode_mdio_find_device);
3253 * fwnode_phy_find_device - For provided phy_fwnode, find phy_device.
3255 * @phy_fwnode: Pointer to the phy's fwnode.
3257 * If successful, returns a pointer to the phy_device with the embedded
3258 * struct device refcount incremented by one, or NULL on failure.
3260 struct phy_device *fwnode_phy_find_device(struct fwnode_handle *phy_fwnode)
3262 struct mdio_device *mdiodev;
3264 mdiodev = fwnode_mdio_find_device(phy_fwnode);
3268 if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY)
3269 return to_phy_device(&mdiodev->dev);
3271 put_device(&mdiodev->dev);
3275 EXPORT_SYMBOL(fwnode_phy_find_device);
3278 * fwnode_get_phy_node - Get the phy_node using the named reference.
3279 * @fwnode: Pointer to fwnode from which phy_node has to be obtained.
3281 * Refer return conditions of fwnode_find_reference().
3282 * For ACPI, only "phy-handle" is supported. Legacy DT properties "phy"
3283 * and "phy-device" are not supported in ACPI. DT supports all the three
3284 * named references to the phy node.
3286 struct fwnode_handle *fwnode_get_phy_node(const struct fwnode_handle *fwnode)
3288 struct fwnode_handle *phy_node;
3290 /* Only phy-handle is used for ACPI */
3291 phy_node = fwnode_find_reference(fwnode, "phy-handle", 0);
3292 if (!IS_ERR(phy_node) || is_acpi_node(fwnode))
3294 phy_node = fwnode_find_reference(fwnode, "phy", 0);
3295 if (!IS_ERR(phy_node))
3297 return fwnode_find_reference(fwnode, "phy-device", 0);
3299 EXPORT_SYMBOL_GPL(fwnode_get_phy_node);
3302 * phy_probe - probe and init a PHY device
3303 * @dev: device to probe and init
3305 * Take care of setting up the phy_device structure, set the state to READY.
3307 static int phy_probe(struct device *dev)
3309 struct phy_device *phydev = to_phy_device(dev);
3310 struct device_driver *drv = phydev->mdio.dev.driver;
3311 struct phy_driver *phydrv = to_phy_driver(drv);
3314 phydev->drv = phydrv;
3316 /* Disable the interrupt if the PHY doesn't support it
3317 * but the interrupt is still a valid one
3319 if (!phy_drv_supports_irq(phydrv) && phy_interrupt_is_valid(phydev))
3320 phydev->irq = PHY_POLL;
3322 if (phydrv->flags & PHY_IS_INTERNAL)
3323 phydev->is_internal = true;
3325 /* Deassert the reset signal */
3326 phy_device_reset(phydev, 0);
3328 if (phydev->drv->probe) {
3329 err = phydev->drv->probe(phydev);
3334 phy_disable_interrupts(phydev);
3336 /* Start out supporting everything. Eventually,
3337 * a controller will attach, and may modify one
3338 * or both of these values
3340 if (phydrv->features) {
3341 linkmode_copy(phydev->supported, phydrv->features);
3342 genphy_c45_read_eee_abilities(phydev);
3344 else if (phydrv->get_features)
3345 err = phydrv->get_features(phydev);
3346 else if (phydev->is_c45)
3347 err = genphy_c45_pma_read_abilities(phydev);
3349 err = genphy_read_abilities(phydev);
3354 if (!linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
3356 phydev->autoneg = 0;
3358 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
3360 phydev->is_gigabit_capable = 1;
3361 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
3363 phydev->is_gigabit_capable = 1;
3365 of_set_phy_supported(phydev);
3366 phy_advertise_supported(phydev);
3368 /* Get PHY default EEE advertising modes and handle them as potentially
3369 * safe initial configuration.
3371 err = genphy_c45_read_eee_adv(phydev, phydev->advertising_eee);
3375 /* Get the EEE modes we want to prohibit. */
3376 of_set_phy_eee_broken(phydev);
3378 /* Some PHYs may advertise, by default, not support EEE modes. So,
3379 * we need to clean them. In addition remove all disabled EEE modes.
3381 linkmode_and(phydev->advertising_eee, phydev->supported_eee,
3382 phydev->advertising_eee);
3383 linkmode_andnot(phydev->advertising_eee, phydev->advertising_eee,
3384 phydev->eee_disabled_modes);
3386 /* There is no "enabled" flag. If PHY is advertising, assume it is
3389 phydev->eee_cfg.eee_enabled = !linkmode_empty(phydev->advertising_eee);
3391 /* Get master/slave strap overrides */
3392 of_set_phy_timing_role(phydev);
3394 /* The Pause Frame bits indicate that the PHY can support passing
3395 * pause frames. During autonegotiation, the PHYs will determine if
3396 * they should allow pause frames to pass. The MAC driver should then
3397 * use that result to determine whether to enable flow control via
3400 * Normally, PHY drivers should not set the Pause bits, and instead
3401 * allow phylib to do that. However, there may be some situations
3402 * (e.g. hardware erratum) where the driver wants to set only one
3405 if (!test_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->supported) &&
3406 !test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->supported)) {
3407 linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT,
3409 linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
3413 /* Set the state to READY by default */
3414 phydev->state = PHY_READY;
3416 /* Get the LEDs from the device tree, and instantiate standard
3419 if (IS_ENABLED(CONFIG_PHYLIB_LEDS))
3420 err = of_phy_leds(phydev);
3423 /* Re-assert the reset signal on error */
3425 phy_device_reset(phydev, 1);
3430 static int phy_remove(struct device *dev)
3432 struct phy_device *phydev = to_phy_device(dev);
3434 cancel_delayed_work_sync(&phydev->state_queue);
3436 if (IS_ENABLED(CONFIG_PHYLIB_LEDS))
3437 phy_leds_unregister(phydev);
3439 phydev->state = PHY_DOWN;
3441 sfp_bus_del_upstream(phydev->sfp_bus);
3442 phydev->sfp_bus = NULL;
3444 if (phydev->drv && phydev->drv->remove)
3445 phydev->drv->remove(phydev);
3447 /* Assert the reset signal */
3448 phy_device_reset(phydev, 1);
3456 * phy_driver_register - register a phy_driver with the PHY layer
3457 * @new_driver: new phy_driver to register
3458 * @owner: module owning this PHY
3460 int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
3464 /* Either the features are hard coded, or dynamically
3465 * determined. It cannot be both.
3467 if (WARN_ON(new_driver->features && new_driver->get_features)) {
3468 pr_err("%s: features and get_features must not both be set\n",
3473 /* PHYLIB device drivers must not match using a DT compatible table
3474 * as this bypasses our checks that the mdiodev that is being matched
3475 * is backed by a struct phy_device. If such a case happens, we will
3476 * make out-of-bounds accesses and lockup in phydev->lock.
3478 if (WARN(new_driver->mdiodrv.driver.of_match_table,
3479 "%s: driver must not provide a DT match table\n",
3483 new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
3484 new_driver->mdiodrv.driver.name = new_driver->name;
3485 new_driver->mdiodrv.driver.bus = &mdio_bus_type;
3486 new_driver->mdiodrv.driver.probe = phy_probe;
3487 new_driver->mdiodrv.driver.remove = phy_remove;
3488 new_driver->mdiodrv.driver.owner = owner;
3489 new_driver->mdiodrv.driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
3491 retval = driver_register(&new_driver->mdiodrv.driver);
3493 pr_err("%s: Error %d in registering driver\n",
3494 new_driver->name, retval);
3499 pr_debug("%s: Registered new driver\n", new_driver->name);
3503 EXPORT_SYMBOL(phy_driver_register);
3505 int phy_drivers_register(struct phy_driver *new_driver, int n,
3506 struct module *owner)
3510 for (i = 0; i < n; i++) {
3511 ret = phy_driver_register(new_driver + i, owner);
3514 phy_driver_unregister(new_driver + i);
3520 EXPORT_SYMBOL(phy_drivers_register);
3522 void phy_driver_unregister(struct phy_driver *drv)
3524 driver_unregister(&drv->mdiodrv.driver);
3526 EXPORT_SYMBOL(phy_driver_unregister);
3528 void phy_drivers_unregister(struct phy_driver *drv, int n)
3532 for (i = 0; i < n; i++)
3533 phy_driver_unregister(drv + i);
3535 EXPORT_SYMBOL(phy_drivers_unregister);
3537 static struct phy_driver genphy_driver = {
3538 .phy_id = 0xffffffff,
3539 .phy_id_mask = 0xffffffff,
3540 .name = "Generic PHY",
3541 .get_features = genphy_read_abilities,
3542 .suspend = genphy_suspend,
3543 .resume = genphy_resume,
3544 .set_loopback = genphy_loopback,
3547 static const struct ethtool_phy_ops phy_ethtool_phy_ops = {
3548 .get_sset_count = phy_ethtool_get_sset_count,
3549 .get_strings = phy_ethtool_get_strings,
3550 .get_stats = phy_ethtool_get_stats,
3551 .get_plca_cfg = phy_ethtool_get_plca_cfg,
3552 .set_plca_cfg = phy_ethtool_set_plca_cfg,
3553 .get_plca_status = phy_ethtool_get_plca_status,
3554 .start_cable_test = phy_start_cable_test,
3555 .start_cable_test_tdr = phy_start_cable_test_tdr,
3558 static const struct phylib_stubs __phylib_stubs = {
3559 .hwtstamp_get = __phy_hwtstamp_get,
3560 .hwtstamp_set = __phy_hwtstamp_set,
3561 .get_phy_stats = __phy_ethtool_get_phy_stats,
3562 .get_link_ext_stats = __phy_ethtool_get_link_ext_stats,
3565 static void phylib_register_stubs(void)
3567 phylib_stubs = &__phylib_stubs;
3570 static void phylib_unregister_stubs(void)
3572 phylib_stubs = NULL;
3575 static int __init phy_init(void)
3580 ethtool_set_ethtool_phy_ops(&phy_ethtool_phy_ops);
3581 phylib_register_stubs();
3584 rc = phy_caps_init();
3586 goto err_ethtool_phy_ops;
3590 rc = phy_driver_register(&genphy_c45_driver, THIS_MODULE);
3592 goto err_ethtool_phy_ops;
3594 rc = phy_driver_register(&genphy_driver, THIS_MODULE);
3601 phy_driver_unregister(&genphy_c45_driver);
3602 err_ethtool_phy_ops:
3604 phylib_unregister_stubs();
3605 ethtool_set_ethtool_phy_ops(NULL);
3611 static void __exit phy_exit(void)
3613 phy_driver_unregister(&genphy_c45_driver);
3614 phy_driver_unregister(&genphy_driver);
3616 phylib_unregister_stubs();
3617 ethtool_set_ethtool_phy_ops(NULL);
3621 subsys_initcall(phy_init);
3622 module_exit(phy_exit);