1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Framework and drivers for configuring and reading different PHYs
4 * Based on code in sungem_phy.c and (long-removed) gianfar_phy.c
8 * Copyright (c) 2004 Freescale Semiconductor, Inc.
14 #include <linux/compiler.h>
15 #include <linux/spinlock.h>
16 #include <linux/ethtool.h>
17 #include <linux/linkmode.h>
18 #include <linux/mdio.h>
19 #include <linux/mii.h>
20 #include <linux/mii_timestamper.h>
21 #include <linux/module.h>
22 #include <linux/timer.h>
23 #include <linux/workqueue.h>
24 #include <linux/mod_devicetable.h>
26 #include <linux/atomic.h>
28 #define PHY_DEFAULT_FEATURES (SUPPORTED_Autoneg | \
32 #define PHY_10BT_FEATURES (SUPPORTED_10baseT_Half | \
33 SUPPORTED_10baseT_Full)
35 #define PHY_100BT_FEATURES (SUPPORTED_100baseT_Half | \
36 SUPPORTED_100baseT_Full)
38 #define PHY_1000BT_FEATURES (SUPPORTED_1000baseT_Half | \
39 SUPPORTED_1000baseT_Full)
41 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;
42 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;
43 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;
44 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;
45 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init;
46 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;
47 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_fec_features) __ro_after_init;
48 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_full_features) __ro_after_init;
50 #define PHY_BASIC_FEATURES ((unsigned long *)&phy_basic_features)
51 #define PHY_BASIC_T1_FEATURES ((unsigned long *)&phy_basic_t1_features)
52 #define PHY_GBIT_FEATURES ((unsigned long *)&phy_gbit_features)
53 #define PHY_GBIT_FIBRE_FEATURES ((unsigned long *)&phy_gbit_fibre_features)
54 #define PHY_GBIT_ALL_PORTS_FEATURES ((unsigned long *)&phy_gbit_all_ports_features)
55 #define PHY_10GBIT_FEATURES ((unsigned long *)&phy_10gbit_features)
56 #define PHY_10GBIT_FEC_FEATURES ((unsigned long *)&phy_10gbit_fec_features)
57 #define PHY_10GBIT_FULL_FEATURES ((unsigned long *)&phy_10gbit_full_features)
59 extern const int phy_basic_ports_array[3];
60 extern const int phy_fibre_port_array[1];
61 extern const int phy_all_ports_features_array[7];
62 extern const int phy_10_100_features_array[4];
63 extern const int phy_basic_t1_features_array[2];
64 extern const int phy_gbit_features_array[2];
65 extern const int phy_10gbit_features_array[1];
68 * Set phydev->irq to PHY_POLL if interrupts are not supported,
69 * or not desired for this PHY. Set to PHY_IGNORE_INTERRUPT if
70 * the attached driver handles the interrupt
73 #define PHY_IGNORE_INTERRUPT -2
75 #define PHY_IS_INTERNAL 0x00000001
76 #define PHY_RST_AFTER_CLK_EN 0x00000002
77 #define MDIO_DEVICE_IS_PHY 0x80000000
79 /* Interface Mode definitions */
81 PHY_INTERFACE_MODE_NA,
82 PHY_INTERFACE_MODE_INTERNAL,
83 PHY_INTERFACE_MODE_MII,
84 PHY_INTERFACE_MODE_GMII,
85 PHY_INTERFACE_MODE_SGMII,
86 PHY_INTERFACE_MODE_TBI,
87 PHY_INTERFACE_MODE_REVMII,
88 PHY_INTERFACE_MODE_RMII,
89 PHY_INTERFACE_MODE_RGMII,
90 PHY_INTERFACE_MODE_RGMII_ID,
91 PHY_INTERFACE_MODE_RGMII_RXID,
92 PHY_INTERFACE_MODE_RGMII_TXID,
93 PHY_INTERFACE_MODE_RTBI,
94 PHY_INTERFACE_MODE_SMII,
95 PHY_INTERFACE_MODE_XGMII,
96 PHY_INTERFACE_MODE_MOCA,
97 PHY_INTERFACE_MODE_QSGMII,
98 PHY_INTERFACE_MODE_TRGMII,
99 PHY_INTERFACE_MODE_1000BASEX,
100 PHY_INTERFACE_MODE_2500BASEX,
101 PHY_INTERFACE_MODE_RXAUI,
102 PHY_INTERFACE_MODE_XAUI,
103 /* 10GBASE-R, XFI, SFI - single lane 10G Serdes */
104 PHY_INTERFACE_MODE_10GBASER,
105 PHY_INTERFACE_MODE_USXGMII,
106 /* 10GBASE-KR - with Clause 73 AN */
107 PHY_INTERFACE_MODE_10GKR,
108 PHY_INTERFACE_MODE_MAX,
112 * phy_supported_speeds - return all speeds currently supported by a phy device
113 * @phy: The phy device to return supported speeds of.
114 * @speeds: buffer to store supported speeds in.
115 * @size: size of speeds buffer.
117 * Description: Returns the number of supported speeds, and fills
118 * the speeds buffer with the supported speeds. If speeds buffer is
119 * too small to contain all currently supported speeds, will return as
120 * many speeds as can fit.
122 unsigned int phy_supported_speeds(struct phy_device *phy,
123 unsigned int *speeds,
127 * phy_modes - map phy_interface_t enum to device tree binding of phy-mode
128 * @interface: enum phy_interface_t value
130 * Description: maps 'enum phy_interface_t' defined in this file
131 * into the device tree binding of 'phy-mode', so that Ethernet
132 * device driver can get phy interface from device tree.
134 static inline const char *phy_modes(phy_interface_t interface)
137 case PHY_INTERFACE_MODE_NA:
139 case PHY_INTERFACE_MODE_INTERNAL:
141 case PHY_INTERFACE_MODE_MII:
143 case PHY_INTERFACE_MODE_GMII:
145 case PHY_INTERFACE_MODE_SGMII:
147 case PHY_INTERFACE_MODE_TBI:
149 case PHY_INTERFACE_MODE_REVMII:
151 case PHY_INTERFACE_MODE_RMII:
153 case PHY_INTERFACE_MODE_RGMII:
155 case PHY_INTERFACE_MODE_RGMII_ID:
157 case PHY_INTERFACE_MODE_RGMII_RXID:
159 case PHY_INTERFACE_MODE_RGMII_TXID:
161 case PHY_INTERFACE_MODE_RTBI:
163 case PHY_INTERFACE_MODE_SMII:
165 case PHY_INTERFACE_MODE_XGMII:
167 case PHY_INTERFACE_MODE_MOCA:
169 case PHY_INTERFACE_MODE_QSGMII:
171 case PHY_INTERFACE_MODE_TRGMII:
173 case PHY_INTERFACE_MODE_1000BASEX:
175 case PHY_INTERFACE_MODE_2500BASEX:
177 case PHY_INTERFACE_MODE_RXAUI:
179 case PHY_INTERFACE_MODE_XAUI:
181 case PHY_INTERFACE_MODE_10GBASER:
183 case PHY_INTERFACE_MODE_USXGMII:
185 case PHY_INTERFACE_MODE_10GKR:
193 #define PHY_INIT_TIMEOUT 100000
194 #define PHY_FORCE_TIMEOUT 10
196 #define PHY_MAX_ADDR 32
198 /* Used when trying to connect to a specific phy (mii bus id:phy device id) */
199 #define PHY_ID_FMT "%s:%02x"
201 #define MII_BUS_ID_SIZE 61
203 /* Or MII_ADDR_C45 into regnum for read/write on mii_bus to enable the 21 bit
204 IEEE 802.3ae clause 45 addressing mode used by 10GIGE phy chips. */
205 #define MII_ADDR_C45 (1<<30)
206 #define MII_DEVADDR_C45_SHIFT 16
207 #define MII_REGADDR_C45_MASK GENMASK(15, 0)
212 struct sfp_upstream_ops;
216 * The Bus class for PHYs. Devices which provide access to
217 * PHYs should register using this structure
220 struct module *owner;
222 char id[MII_BUS_ID_SIZE];
224 int (*read)(struct mii_bus *bus, int addr, int regnum);
225 int (*write)(struct mii_bus *bus, int addr, int regnum, u16 val);
226 int (*reset)(struct mii_bus *bus);
229 * A lock to ensure that only one thing can read/write
230 * the MDIO bus at a time
232 struct mutex mdio_lock;
234 struct device *parent;
236 MDIOBUS_ALLOCATED = 1,
238 MDIOBUS_UNREGISTERED,
243 /* list of all PHYs on bus */
244 struct mdio_device *mdio_map[PHY_MAX_ADDR];
246 /* PHY addresses to be ignored when probing */
249 /* PHY addresses to ignore the TA/read failure */
250 u32 phy_ignore_ta_mask;
253 * An array of interrupts, each PHY's interrupt at the index
254 * matching its address
256 int irq[PHY_MAX_ADDR];
258 /* GPIO reset pulse width in microseconds */
260 /* RESET GPIO descriptor pointer */
261 struct gpio_desc *reset_gpiod;
263 #define to_mii_bus(d) container_of(d, struct mii_bus, dev)
265 struct mii_bus *mdiobus_alloc_size(size_t);
266 static inline struct mii_bus *mdiobus_alloc(void)
268 return mdiobus_alloc_size(0);
271 int __mdiobus_register(struct mii_bus *bus, struct module *owner);
272 #define mdiobus_register(bus) __mdiobus_register(bus, THIS_MODULE)
273 void mdiobus_unregister(struct mii_bus *bus);
274 void mdiobus_free(struct mii_bus *bus);
275 struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv);
276 static inline struct mii_bus *devm_mdiobus_alloc(struct device *dev)
278 return devm_mdiobus_alloc_size(dev, 0);
281 void devm_mdiobus_free(struct device *dev, struct mii_bus *bus);
282 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr);
284 #define PHY_INTERRUPT_DISABLED false
285 #define PHY_INTERRUPT_ENABLED true
287 /* PHY state machine states:
289 * DOWN: PHY device and driver are not ready for anything. probe
290 * should be called if and only if the PHY is in this state,
291 * given that the PHY device exists.
292 * - PHY driver probe function will set the state to READY
294 * READY: PHY is ready to send and receive packets, but the
295 * controller is not. By default, PHYs which do not implement
296 * probe will be set to this state by phy_probe().
297 * - start will set the state to UP
299 * UP: The PHY and attached device are ready to do work.
300 * Interrupts should be started here.
301 * - timer moves to NOLINK or RUNNING
303 * NOLINK: PHY is up, but not currently plugged in.
304 * - irq or timer will set RUNNING if link comes back
305 * - phy_stop moves to HALTED
307 * RUNNING: PHY is currently up, running, and possibly sending
308 * and/or receiving packets
309 * - irq or timer will set NOLINK if link goes down
310 * - phy_stop moves to HALTED
312 * HALTED: PHY is up, but no polling or interrupts are done. Or
313 * PHY is in an error state.
314 * - phy_start moves to UP
326 * struct phy_c45_device_ids - 802.3-c45 Device Identifiers
327 * @devices_in_package: Bit vector of devices present.
328 * @device_ids: The device identifer for each present device.
330 struct phy_c45_device_ids {
331 u32 devices_in_package;
335 /* phy_device: An instance of a PHY
337 * drv: Pointer to the driver for this PHY instance
338 * phy_id: UID for this device found during discovery
339 * c45_ids: 802.3-c45 Device Identifers if is_c45.
340 * is_c45: Set to true if this phy uses clause 45 addressing.
341 * is_internal: Set to true if this phy is internal to a MAC.
342 * is_pseudo_fixed_link: Set to true if this phy is an Ethernet switch, etc.
343 * is_gigabit_capable: Set to true if PHY supports 1000Mbps
344 * has_fixups: Set to true if this phy has fixups/quirks.
345 * suspended: Set to true if this phy has been suspended successfully.
346 * sysfs_links: Internal boolean tracking sysfs symbolic links setup/removal.
347 * loopback_enabled: Set true if this phy has been loopbacked successfully.
348 * state: state of the PHY for management purposes
349 * dev_flags: Device-specific flags used by the PHY driver.
350 * irq: IRQ number of the PHY's interrupt (-1 if none)
351 * phy_timer: The timer for handling the state machine
352 * sfp_bus_attached: flag indicating whether the SFP bus has been attached
353 * sfp_bus: SFP bus attached to this PHY's fiber port
354 * attached_dev: The attached enet driver's device instance ptr
355 * adjust_link: Callback for the enet controller to respond to
356 * changes in the link state.
358 * speed, duplex, pause, supported, advertising, lp_advertising,
359 * and autoneg are used like in mii_if_info
361 * interrupts currently only supports enabled or disabled,
362 * but could be changed in the future to support enabling
363 * and disabling specific interrupts
365 * Contains some infrastructure for polling and interrupt
366 * handling, as well as handling shifts in PHY hardware state
369 struct mdio_device mdio;
371 /* Information about the PHY type */
372 /* And management functions */
373 struct phy_driver *drv;
377 struct phy_c45_device_ids c45_ids;
379 unsigned is_internal:1;
380 unsigned is_pseudo_fixed_link:1;
381 unsigned is_gigabit_capable:1;
382 unsigned has_fixups:1;
383 unsigned suspended:1;
384 unsigned sysfs_links:1;
385 unsigned loopback_enabled:1;
388 /* The most recently read link state */
390 unsigned autoneg_complete:1;
392 /* Interrupts are enabled */
393 unsigned interrupts:1;
395 enum phy_state state;
399 phy_interface_t interface;
402 * forced speed & duplex (no autoneg)
403 * partner speed & duplex & pause (autoneg)
410 /* Union of PHY and Attached devices' supported link modes */
411 /* See ethtool.h for more info */
412 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
413 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
414 __ETHTOOL_DECLARE_LINK_MODE_MASK(lp_advertising);
415 /* used with phy_speed_down */
416 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_old);
418 /* Energy efficient ethernet modes which should be prohibited */
419 u32 eee_broken_modes;
421 #ifdef CONFIG_LED_TRIGGER_PHY
422 struct phy_led_trigger *phy_led_triggers;
423 unsigned int phy_num_led_triggers;
424 struct phy_led_trigger *last_triggered;
426 struct phy_led_trigger *led_link_trigger;
430 * Interrupt number for this PHY
431 * -1 means no interrupt
435 /* private data pointer */
436 /* For use by PHYs to maintain extra state */
439 /* Interrupt and Polling infrastructure */
440 struct delayed_work state_queue;
444 /* This may be modified under the rtnl lock */
445 bool sfp_bus_attached;
446 struct sfp_bus *sfp_bus;
447 struct phylink *phylink;
448 struct net_device *attached_dev;
449 struct mii_timestamper *mii_ts;
454 void (*phy_link_change)(struct phy_device *, bool up, bool do_carrier);
455 void (*adjust_link)(struct net_device *dev);
457 #define to_phy_device(d) container_of(to_mdio_device(d), \
458 struct phy_device, mdio)
460 /* struct phy_driver: Driver structure for a particular PHY type
462 * driver_data: static driver data
463 * phy_id: The result of reading the UID registers of this PHY
464 * type, and ANDing them with the phy_id_mask. This driver
465 * only works for PHYs with IDs which match this field
466 * name: The friendly name of this PHY type
467 * phy_id_mask: Defines the important bits of the phy_id
468 * features: A mandatory list of features (speed, duplex, etc)
469 * supported by this PHY
470 * flags: A bitfield defining certain other features this PHY
471 * supports (like interrupts)
473 * All functions are optional. If config_aneg or read_status
474 * are not implemented, the phy core uses the genphy versions.
475 * Note that none of these functions should be called from
476 * interrupt time. The goal is for the bus read/write functions
477 * to be able to block when the bus transaction is happening,
478 * and be freed up by an interrupt (The MPC85xx has this ability,
479 * though it is not currently supported in the driver).
482 struct mdio_driver_common mdiodrv;
486 const unsigned long * const features;
488 const void *driver_data;
491 * Called to issue a PHY software reset
493 int (*soft_reset)(struct phy_device *phydev);
496 * Called to initialize the PHY,
497 * including after a reset
499 int (*config_init)(struct phy_device *phydev);
502 * Called during discovery. Used to set
503 * up device-specific structures, if any
505 int (*probe)(struct phy_device *phydev);
508 * Probe the hardware to determine what abilities it has.
509 * Should only set phydev->supported.
511 int (*get_features)(struct phy_device *phydev);
513 /* PHY Power Management */
514 int (*suspend)(struct phy_device *phydev);
515 int (*resume)(struct phy_device *phydev);
518 * Configures the advertisement and resets
519 * autonegotiation if phydev->autoneg is on,
520 * forces the speed to the current settings in phydev
521 * if phydev->autoneg is off
523 int (*config_aneg)(struct phy_device *phydev);
525 /* Determines the auto negotiation result */
526 int (*aneg_done)(struct phy_device *phydev);
528 /* Determines the negotiated speed and duplex */
529 int (*read_status)(struct phy_device *phydev);
531 /* Clears any pending interrupts */
532 int (*ack_interrupt)(struct phy_device *phydev);
534 /* Enables or disables interrupts */
535 int (*config_intr)(struct phy_device *phydev);
538 * Checks if the PHY generated an interrupt.
539 * For multi-PHY devices with shared PHY interrupt pin
541 int (*did_interrupt)(struct phy_device *phydev);
543 /* Override default interrupt handling */
544 int (*handle_interrupt)(struct phy_device *phydev);
546 /* Clears up any memory if needed */
547 void (*remove)(struct phy_device *phydev);
549 /* Returns true if this is a suitable driver for the given
550 * phydev. If NULL, matching is based on phy_id and
553 int (*match_phy_device)(struct phy_device *phydev);
555 /* Some devices (e.g. qnap TS-119P II) require PHY register changes to
556 * enable Wake on LAN, so set_wol is provided to be called in the
557 * ethernet driver's set_wol function. */
558 int (*set_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
560 /* See set_wol, but for checking whether Wake on LAN is enabled. */
561 void (*get_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
564 * Called to inform a PHY device driver when the core is about to
565 * change the link state. This callback is supposed to be used as
566 * fixup hook for drivers that need to take action when the link
567 * state changes. Drivers are by no means allowed to mess with the
568 * PHY device structure in their implementations.
570 void (*link_change_notify)(struct phy_device *dev);
573 * Phy specific driver override for reading a MMD register.
574 * This function is optional for PHY specific drivers. When
575 * not provided, the default MMD read function will be used
576 * by phy_read_mmd(), which will use either a direct read for
577 * Clause 45 PHYs or an indirect read for Clause 22 PHYs.
578 * devnum is the MMD device number within the PHY device,
579 * regnum is the register within the selected MMD device.
581 int (*read_mmd)(struct phy_device *dev, int devnum, u16 regnum);
584 * Phy specific driver override for writing a MMD register.
585 * This function is optional for PHY specific drivers. When
586 * not provided, the default MMD write function will be used
587 * by phy_write_mmd(), which will use either a direct write for
588 * Clause 45 PHYs, or an indirect write for Clause 22 PHYs.
589 * devnum is the MMD device number within the PHY device,
590 * regnum is the register within the selected MMD device.
591 * val is the value to be written.
593 int (*write_mmd)(struct phy_device *dev, int devnum, u16 regnum,
596 int (*read_page)(struct phy_device *dev);
597 int (*write_page)(struct phy_device *dev, int page);
599 /* Get the size and type of the eeprom contained within a plug-in
601 int (*module_info)(struct phy_device *dev,
602 struct ethtool_modinfo *modinfo);
604 /* Get the eeprom information from the plug-in module */
605 int (*module_eeprom)(struct phy_device *dev,
606 struct ethtool_eeprom *ee, u8 *data);
608 /* Get statistics from the phy using ethtool */
609 int (*get_sset_count)(struct phy_device *dev);
610 void (*get_strings)(struct phy_device *dev, u8 *data);
611 void (*get_stats)(struct phy_device *dev,
612 struct ethtool_stats *stats, u64 *data);
614 /* Get and Set PHY tunables */
615 int (*get_tunable)(struct phy_device *dev,
616 struct ethtool_tunable *tuna, void *data);
617 int (*set_tunable)(struct phy_device *dev,
618 struct ethtool_tunable *tuna,
620 int (*set_loopback)(struct phy_device *dev, bool enable);
622 #define to_phy_driver(d) container_of(to_mdio_common_driver(d), \
623 struct phy_driver, mdiodrv)
625 #define PHY_ANY_ID "MATCH ANY PHY"
626 #define PHY_ANY_UID 0xffffffff
628 #define PHY_ID_MATCH_EXACT(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 0)
629 #define PHY_ID_MATCH_MODEL(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 4)
630 #define PHY_ID_MATCH_VENDOR(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 10)
632 /* A Structure for boards to register fixups with the PHY Lib */
634 struct list_head list;
635 char bus_id[MII_BUS_ID_SIZE + 3];
638 int (*run)(struct phy_device *phydev);
641 const char *phy_speed_to_str(int speed);
642 const char *phy_duplex_to_str(unsigned int duplex);
644 /* A structure for mapping a particular speed and duplex
645 * combination to a particular SUPPORTED and ADVERTISED value
653 const struct phy_setting *
654 phy_lookup_setting(int speed, int duplex, const unsigned long *mask,
656 size_t phy_speeds(unsigned int *speeds, size_t size,
657 unsigned long *mask);
658 void of_set_phy_supported(struct phy_device *phydev);
659 void of_set_phy_eee_broken(struct phy_device *phydev);
660 int phy_speed_down_core(struct phy_device *phydev);
663 * phy_is_started - Convenience function to check whether PHY is started
664 * @phydev: The phy_device struct
666 static inline bool phy_is_started(struct phy_device *phydev)
668 return phydev->state >= PHY_UP;
671 void phy_resolve_aneg_pause(struct phy_device *phydev);
672 void phy_resolve_aneg_linkmode(struct phy_device *phydev);
675 * phy_read - Convenience function for reading a given PHY register
676 * @phydev: the phy_device struct
677 * @regnum: register number to read
679 * NOTE: MUST NOT be called from interrupt context,
680 * because the bus read/write functions may wait for an interrupt
681 * to conclude the operation.
683 static inline int phy_read(struct phy_device *phydev, u32 regnum)
685 return mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
689 * __phy_read - convenience function for reading a given PHY register
690 * @phydev: the phy_device struct
691 * @regnum: register number to read
693 * The caller must have taken the MDIO bus lock.
695 static inline int __phy_read(struct phy_device *phydev, u32 regnum)
697 return __mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
701 * phy_write - Convenience function for writing a given PHY register
702 * @phydev: the phy_device struct
703 * @regnum: register number to write
704 * @val: value to write to @regnum
706 * NOTE: MUST NOT be called from interrupt context,
707 * because the bus read/write functions may wait for an interrupt
708 * to conclude the operation.
710 static inline int phy_write(struct phy_device *phydev, u32 regnum, u16 val)
712 return mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum, val);
716 * __phy_write - Convenience function for writing a given PHY register
717 * @phydev: the phy_device struct
718 * @regnum: register number to write
719 * @val: value to write to @regnum
721 * The caller must have taken the MDIO bus lock.
723 static inline int __phy_write(struct phy_device *phydev, u32 regnum, u16 val)
725 return __mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum,
730 * phy_read_mmd - Convenience function for reading a register
731 * from an MMD on a given PHY.
732 * @phydev: The phy_device struct
733 * @devad: The MMD to read from
734 * @regnum: The register on the MMD to read
736 * Same rules as for phy_read();
738 int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
741 * __phy_read_mmd - Convenience function for reading a register
742 * from an MMD on a given PHY.
743 * @phydev: The phy_device struct
744 * @devad: The MMD to read from
745 * @regnum: The register on the MMD to read
747 * Same rules as for __phy_read();
749 int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
752 * phy_write_mmd - Convenience function for writing a register
753 * on an MMD on a given PHY.
754 * @phydev: The phy_device struct
755 * @devad: The MMD to write to
756 * @regnum: The register on the MMD to read
757 * @val: value to write to @regnum
759 * Same rules as for phy_write();
761 int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
764 * __phy_write_mmd - Convenience function for writing a register
765 * on an MMD on a given PHY.
766 * @phydev: The phy_device struct
767 * @devad: The MMD to write to
768 * @regnum: The register on the MMD to read
769 * @val: value to write to @regnum
771 * Same rules as for __phy_write();
773 int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
775 int __phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
777 int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
779 int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set);
780 int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set);
782 int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
784 int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
786 int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
788 int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
792 * __phy_set_bits - Convenience function for setting bits in a PHY register
793 * @phydev: the phy_device struct
794 * @regnum: register number to write
797 * The caller must have taken the MDIO bus lock.
799 static inline int __phy_set_bits(struct phy_device *phydev, u32 regnum, u16 val)
801 return __phy_modify(phydev, regnum, 0, val);
805 * __phy_clear_bits - Convenience function for clearing bits in a PHY register
806 * @phydev: the phy_device struct
807 * @regnum: register number to write
808 * @val: bits to clear
810 * The caller must have taken the MDIO bus lock.
812 static inline int __phy_clear_bits(struct phy_device *phydev, u32 regnum,
815 return __phy_modify(phydev, regnum, val, 0);
819 * phy_set_bits - Convenience function for setting bits in a PHY register
820 * @phydev: the phy_device struct
821 * @regnum: register number to write
824 static inline int phy_set_bits(struct phy_device *phydev, u32 regnum, u16 val)
826 return phy_modify(phydev, regnum, 0, val);
830 * phy_clear_bits - Convenience function for clearing bits in a PHY register
831 * @phydev: the phy_device struct
832 * @regnum: register number to write
833 * @val: bits to clear
835 static inline int phy_clear_bits(struct phy_device *phydev, u32 regnum, u16 val)
837 return phy_modify(phydev, regnum, val, 0);
841 * __phy_set_bits_mmd - Convenience function for setting bits in a register
843 * @phydev: the phy_device struct
844 * @devad: the MMD containing register to modify
845 * @regnum: register number to modify
848 * The caller must have taken the MDIO bus lock.
850 static inline int __phy_set_bits_mmd(struct phy_device *phydev, int devad,
853 return __phy_modify_mmd(phydev, devad, regnum, 0, val);
857 * __phy_clear_bits_mmd - Convenience function for clearing bits in a register
859 * @phydev: the phy_device struct
860 * @devad: the MMD containing register to modify
861 * @regnum: register number to modify
862 * @val: bits to clear
864 * The caller must have taken the MDIO bus lock.
866 static inline int __phy_clear_bits_mmd(struct phy_device *phydev, int devad,
869 return __phy_modify_mmd(phydev, devad, regnum, val, 0);
873 * phy_set_bits_mmd - Convenience function for setting bits in a register
875 * @phydev: the phy_device struct
876 * @devad: the MMD containing register to modify
877 * @regnum: register number to modify
880 static inline int phy_set_bits_mmd(struct phy_device *phydev, int devad,
883 return phy_modify_mmd(phydev, devad, regnum, 0, val);
887 * phy_clear_bits_mmd - Convenience function for clearing bits in a register
889 * @phydev: the phy_device struct
890 * @devad: the MMD containing register to modify
891 * @regnum: register number to modify
892 * @val: bits to clear
894 static inline int phy_clear_bits_mmd(struct phy_device *phydev, int devad,
897 return phy_modify_mmd(phydev, devad, regnum, val, 0);
901 * phy_interrupt_is_valid - Convenience function for testing a given PHY irq
902 * @phydev: the phy_device struct
904 * NOTE: must be kept in sync with addition/removal of PHY_POLL and
905 * PHY_IGNORE_INTERRUPT
907 static inline bool phy_interrupt_is_valid(struct phy_device *phydev)
909 return phydev->irq != PHY_POLL && phydev->irq != PHY_IGNORE_INTERRUPT;
913 * phy_polling_mode - Convenience function for testing whether polling is
914 * used to detect PHY status changes
915 * @phydev: the phy_device struct
917 static inline bool phy_polling_mode(struct phy_device *phydev)
919 return phydev->irq == PHY_POLL;
923 * phy_has_hwtstamp - Tests whether a PHY time stamp configuration.
924 * @phydev: the phy_device struct
926 static inline bool phy_has_hwtstamp(struct phy_device *phydev)
928 return phydev && phydev->mii_ts && phydev->mii_ts->hwtstamp;
932 * phy_has_rxtstamp - Tests whether a PHY supports receive time stamping.
933 * @phydev: the phy_device struct
935 static inline bool phy_has_rxtstamp(struct phy_device *phydev)
937 return phydev && phydev->mii_ts && phydev->mii_ts->rxtstamp;
941 * phy_has_tsinfo - Tests whether a PHY reports time stamping and/or
942 * PTP hardware clock capabilities.
943 * @phydev: the phy_device struct
945 static inline bool phy_has_tsinfo(struct phy_device *phydev)
947 return phydev && phydev->mii_ts && phydev->mii_ts->ts_info;
951 * phy_has_txtstamp - Tests whether a PHY supports transmit time stamping.
952 * @phydev: the phy_device struct
954 static inline bool phy_has_txtstamp(struct phy_device *phydev)
956 return phydev && phydev->mii_ts && phydev->mii_ts->txtstamp;
959 static inline int phy_hwtstamp(struct phy_device *phydev, struct ifreq *ifr)
961 return phydev->mii_ts->hwtstamp(phydev->mii_ts, ifr);
964 static inline bool phy_rxtstamp(struct phy_device *phydev, struct sk_buff *skb,
967 return phydev->mii_ts->rxtstamp(phydev->mii_ts, skb, type);
970 static inline int phy_ts_info(struct phy_device *phydev,
971 struct ethtool_ts_info *tsinfo)
973 return phydev->mii_ts->ts_info(phydev->mii_ts, tsinfo);
976 static inline void phy_txtstamp(struct phy_device *phydev, struct sk_buff *skb,
979 phydev->mii_ts->txtstamp(phydev->mii_ts, skb, type);
983 * phy_is_internal - Convenience function for testing if a PHY is internal
984 * @phydev: the phy_device struct
986 static inline bool phy_is_internal(struct phy_device *phydev)
988 return phydev->is_internal;
992 * phy_interface_mode_is_rgmii - Convenience function for testing if a
993 * PHY interface mode is RGMII (all variants)
994 * @mode: the phy_interface_t enum
996 static inline bool phy_interface_mode_is_rgmii(phy_interface_t mode)
998 return mode >= PHY_INTERFACE_MODE_RGMII &&
999 mode <= PHY_INTERFACE_MODE_RGMII_TXID;
1003 * phy_interface_mode_is_8023z() - does the phy interface mode use 802.3z
1005 * @mode: one of &enum phy_interface_t
1007 * Returns true if the phy interface mode uses the 16-bit negotiation
1008 * word as defined in 802.3z. (See 802.3-2015 37.2.1 Config_Reg encoding)
1010 static inline bool phy_interface_mode_is_8023z(phy_interface_t mode)
1012 return mode == PHY_INTERFACE_MODE_1000BASEX ||
1013 mode == PHY_INTERFACE_MODE_2500BASEX;
1017 * phy_interface_is_rgmii - Convenience function for testing if a PHY interface
1018 * is RGMII (all variants)
1019 * @phydev: the phy_device struct
1021 static inline bool phy_interface_is_rgmii(struct phy_device *phydev)
1023 return phy_interface_mode_is_rgmii(phydev->interface);
1027 * phy_is_pseudo_fixed_link - Convenience function for testing if this
1028 * PHY is the CPU port facing side of an Ethernet switch, or similar.
1029 * @phydev: the phy_device struct
1031 static inline bool phy_is_pseudo_fixed_link(struct phy_device *phydev)
1033 return phydev->is_pseudo_fixed_link;
1036 int phy_save_page(struct phy_device *phydev);
1037 int phy_select_page(struct phy_device *phydev, int page);
1038 int phy_restore_page(struct phy_device *phydev, int oldpage, int ret);
1039 int phy_read_paged(struct phy_device *phydev, int page, u32 regnum);
1040 int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val);
1041 int phy_modify_paged_changed(struct phy_device *phydev, int page, u32 regnum,
1043 int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
1046 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
1048 struct phy_c45_device_ids *c45_ids);
1049 #if IS_ENABLED(CONFIG_PHYLIB)
1050 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45);
1051 int phy_device_register(struct phy_device *phy);
1052 void phy_device_free(struct phy_device *phydev);
1055 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
1060 static inline int phy_device_register(struct phy_device *phy)
1065 static inline void phy_device_free(struct phy_device *phydev) { }
1066 #endif /* CONFIG_PHYLIB */
1067 void phy_device_remove(struct phy_device *phydev);
1068 int phy_init_hw(struct phy_device *phydev);
1069 int phy_suspend(struct phy_device *phydev);
1070 int phy_resume(struct phy_device *phydev);
1071 int __phy_resume(struct phy_device *phydev);
1072 int phy_loopback(struct phy_device *phydev, bool enable);
1073 void phy_sfp_attach(void *upstream, struct sfp_bus *bus);
1074 void phy_sfp_detach(void *upstream, struct sfp_bus *bus);
1075 int phy_sfp_probe(struct phy_device *phydev,
1076 const struct sfp_upstream_ops *ops);
1077 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1078 phy_interface_t interface);
1079 struct phy_device *phy_find_first(struct mii_bus *bus);
1080 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
1081 u32 flags, phy_interface_t interface);
1082 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
1083 void (*handler)(struct net_device *),
1084 phy_interface_t interface);
1085 struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
1086 void (*handler)(struct net_device *),
1087 phy_interface_t interface);
1088 void phy_disconnect(struct phy_device *phydev);
1089 void phy_detach(struct phy_device *phydev);
1090 void phy_start(struct phy_device *phydev);
1091 void phy_stop(struct phy_device *phydev);
1092 int phy_start_aneg(struct phy_device *phydev);
1093 int phy_aneg_done(struct phy_device *phydev);
1094 int phy_speed_down(struct phy_device *phydev, bool sync);
1095 int phy_speed_up(struct phy_device *phydev);
1097 int phy_restart_aneg(struct phy_device *phydev);
1098 int phy_reset_after_clk_enable(struct phy_device *phydev);
1100 static inline void phy_device_reset(struct phy_device *phydev, int value)
1102 mdio_device_reset(&phydev->mdio, value);
1105 #define phydev_err(_phydev, format, args...) \
1106 dev_err(&_phydev->mdio.dev, format, ##args)
1108 #define phydev_info(_phydev, format, args...) \
1109 dev_info(&_phydev->mdio.dev, format, ##args)
1111 #define phydev_warn(_phydev, format, args...) \
1112 dev_warn(&_phydev->mdio.dev, format, ##args)
1114 #define phydev_dbg(_phydev, format, args...) \
1115 dev_dbg(&_phydev->mdio.dev, format, ##args)
1117 static inline const char *phydev_name(const struct phy_device *phydev)
1119 return dev_name(&phydev->mdio.dev);
1122 static inline void phy_lock_mdio_bus(struct phy_device *phydev)
1124 mutex_lock(&phydev->mdio.bus->mdio_lock);
1127 static inline void phy_unlock_mdio_bus(struct phy_device *phydev)
1129 mutex_unlock(&phydev->mdio.bus->mdio_lock);
1132 void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
1134 char *phy_attached_info_irq(struct phy_device *phydev)
1136 void phy_attached_info(struct phy_device *phydev);
1139 int genphy_read_abilities(struct phy_device *phydev);
1140 int genphy_setup_forced(struct phy_device *phydev);
1141 int genphy_restart_aneg(struct phy_device *phydev);
1142 int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart);
1143 int genphy_config_eee_advert(struct phy_device *phydev);
1144 int __genphy_config_aneg(struct phy_device *phydev, bool changed);
1145 int genphy_aneg_done(struct phy_device *phydev);
1146 int genphy_update_link(struct phy_device *phydev);
1147 int genphy_read_lpa(struct phy_device *phydev);
1148 int genphy_read_status_fixed(struct phy_device *phydev);
1149 int genphy_read_status(struct phy_device *phydev);
1150 int genphy_suspend(struct phy_device *phydev);
1151 int genphy_resume(struct phy_device *phydev);
1152 int genphy_loopback(struct phy_device *phydev, bool enable);
1153 int genphy_soft_reset(struct phy_device *phydev);
1155 static inline int genphy_config_aneg(struct phy_device *phydev)
1157 return __genphy_config_aneg(phydev, false);
1160 static inline int genphy_no_soft_reset(struct phy_device *phydev)
1164 static inline int genphy_no_ack_interrupt(struct phy_device *phydev)
1168 static inline int genphy_no_config_intr(struct phy_device *phydev)
1172 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad,
1174 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
1175 u16 regnum, u16 val);
1178 int genphy_c37_config_aneg(struct phy_device *phydev);
1179 int genphy_c37_read_status(struct phy_device *phydev);
1182 int genphy_c45_restart_aneg(struct phy_device *phydev);
1183 int genphy_c45_check_and_restart_aneg(struct phy_device *phydev, bool restart);
1184 int genphy_c45_aneg_done(struct phy_device *phydev);
1185 int genphy_c45_read_link(struct phy_device *phydev);
1186 int genphy_c45_read_lpa(struct phy_device *phydev);
1187 int genphy_c45_read_pma(struct phy_device *phydev);
1188 int genphy_c45_pma_setup_forced(struct phy_device *phydev);
1189 int genphy_c45_an_config_aneg(struct phy_device *phydev);
1190 int genphy_c45_an_disable_aneg(struct phy_device *phydev);
1191 int genphy_c45_read_mdix(struct phy_device *phydev);
1192 int genphy_c45_pma_read_abilities(struct phy_device *phydev);
1193 int genphy_c45_read_status(struct phy_device *phydev);
1194 int genphy_c45_config_aneg(struct phy_device *phydev);
1196 /* The gen10g_* functions are the old Clause 45 stub */
1197 int gen10g_config_aneg(struct phy_device *phydev);
1199 static inline int phy_read_status(struct phy_device *phydev)
1204 if (phydev->drv->read_status)
1205 return phydev->drv->read_status(phydev);
1207 return genphy_read_status(phydev);
1210 void phy_driver_unregister(struct phy_driver *drv);
1211 void phy_drivers_unregister(struct phy_driver *drv, int n);
1212 int phy_driver_register(struct phy_driver *new_driver, struct module *owner);
1213 int phy_drivers_register(struct phy_driver *new_driver, int n,
1214 struct module *owner);
1215 void phy_state_machine(struct work_struct *work);
1216 void phy_queue_state_machine(struct phy_device *phydev, unsigned long jiffies);
1217 void phy_mac_interrupt(struct phy_device *phydev);
1218 void phy_start_machine(struct phy_device *phydev);
1219 void phy_stop_machine(struct phy_device *phydev);
1220 void phy_ethtool_ksettings_get(struct phy_device *phydev,
1221 struct ethtool_link_ksettings *cmd);
1222 int phy_ethtool_ksettings_set(struct phy_device *phydev,
1223 const struct ethtool_link_ksettings *cmd);
1224 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd);
1225 void phy_request_interrupt(struct phy_device *phydev);
1226 void phy_free_interrupt(struct phy_device *phydev);
1227 void phy_print_status(struct phy_device *phydev);
1228 int phy_set_max_speed(struct phy_device *phydev, u32 max_speed);
1229 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode);
1230 void phy_advertise_supported(struct phy_device *phydev);
1231 void phy_support_sym_pause(struct phy_device *phydev);
1232 void phy_support_asym_pause(struct phy_device *phydev);
1233 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
1235 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx);
1236 bool phy_validate_pause(struct phy_device *phydev,
1237 struct ethtool_pauseparam *pp);
1239 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
1240 int (*run)(struct phy_device *));
1241 int phy_register_fixup_for_id(const char *bus_id,
1242 int (*run)(struct phy_device *));
1243 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
1244 int (*run)(struct phy_device *));
1246 int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask);
1247 int phy_unregister_fixup_for_id(const char *bus_id);
1248 int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask);
1250 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable);
1251 int phy_get_eee_err(struct phy_device *phydev);
1252 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data);
1253 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data);
1254 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol);
1255 void phy_ethtool_get_wol(struct phy_device *phydev,
1256 struct ethtool_wolinfo *wol);
1257 int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1258 struct ethtool_link_ksettings *cmd);
1259 int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1260 const struct ethtool_link_ksettings *cmd);
1261 int phy_ethtool_nway_reset(struct net_device *ndev);
1263 #if IS_ENABLED(CONFIG_PHYLIB)
1264 int __init mdio_bus_init(void);
1265 void mdio_bus_exit(void);
1268 /* Inline function for use within net/core/ethtool.c (built-in) */
1269 static inline int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data)
1274 mutex_lock(&phydev->lock);
1275 phydev->drv->get_strings(phydev, data);
1276 mutex_unlock(&phydev->lock);
1281 static inline int phy_ethtool_get_sset_count(struct phy_device *phydev)
1288 if (phydev->drv->get_sset_count &&
1289 phydev->drv->get_strings &&
1290 phydev->drv->get_stats) {
1291 mutex_lock(&phydev->lock);
1292 ret = phydev->drv->get_sset_count(phydev);
1293 mutex_unlock(&phydev->lock);
1301 static inline int phy_ethtool_get_stats(struct phy_device *phydev,
1302 struct ethtool_stats *stats, u64 *data)
1307 mutex_lock(&phydev->lock);
1308 phydev->drv->get_stats(phydev, stats, data);
1309 mutex_unlock(&phydev->lock);
1314 extern struct bus_type mdio_bus_type;
1316 struct mdio_board_info {
1318 char modalias[MDIO_NAME_SIZE];
1320 const void *platform_data;
1323 #if IS_ENABLED(CONFIG_MDIO_DEVICE)
1324 int mdiobus_register_board_info(const struct mdio_board_info *info,
1327 static inline int mdiobus_register_board_info(const struct mdio_board_info *i,
1336 * module_phy_driver() - Helper macro for registering PHY drivers
1337 * @__phy_drivers: array of PHY drivers to register
1339 * Helper macro for PHY drivers which do not do anything special in module
1340 * init/exit. Each module may only use this macro once, and calling it
1341 * replaces module_init() and module_exit().
1343 #define phy_module_driver(__phy_drivers, __count) \
1344 static int __init phy_module_init(void) \
1346 return phy_drivers_register(__phy_drivers, __count, THIS_MODULE); \
1348 module_init(phy_module_init); \
1349 static void __exit phy_module_exit(void) \
1351 phy_drivers_unregister(__phy_drivers, __count); \
1353 module_exit(phy_module_exit)
1355 #define module_phy_driver(__phy_drivers) \
1356 phy_module_driver(__phy_drivers, ARRAY_SIZE(__phy_drivers))
1358 bool phy_driver_is_genphy(struct phy_device *phydev);
1359 bool phy_driver_is_genphy_10g(struct phy_device *phydev);
1361 #endif /* __PHY_H */