1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * linux/mdio.h: definitions for MDIO (clause 45) transceivers
4 * Copyright 2006-2009 Solarflare Communications Inc.
6 #ifndef __LINUX_MDIO_H__
7 #define __LINUX_MDIO_H__
9 #include <uapi/linux/mdio.h>
10 #include <linux/bitfield.h>
11 #include <linux/mod_devicetable.h>
17 /* Multiple levels of nesting are possible. However typically this is
18 * limited to nested DSA like layer, a MUX layer, and the normal
19 * user. Instead of trying to handle the general case, just define
22 enum mdio_mutex_lock_class {
32 char modalias[MDIO_NAME_SIZE];
34 int (*bus_match)(struct device *dev, struct device_driver *drv);
35 void (*device_free)(struct mdio_device *mdiodev);
36 void (*device_remove)(struct mdio_device *mdiodev);
38 /* Bus address of the MDIO device (0-31) */
41 struct gpio_desc *reset_gpio;
42 struct reset_control *reset_ctrl;
43 unsigned int reset_assert_delay;
44 unsigned int reset_deassert_delay;
47 static inline struct mdio_device *to_mdio_device(const struct device *dev)
49 return container_of(dev, struct mdio_device, dev);
52 /* struct mdio_driver_common: Common to all MDIO drivers */
53 struct mdio_driver_common {
54 struct device_driver driver;
57 #define MDIO_DEVICE_FLAG_PHY 1
59 static inline struct mdio_driver_common *
60 to_mdio_common_driver(const struct device_driver *driver)
62 return container_of(driver, struct mdio_driver_common, driver);
65 /* struct mdio_driver: Generic MDIO driver */
67 struct mdio_driver_common mdiodrv;
70 * Called during discovery. Used to set
71 * up device-specific structures, if any
73 int (*probe)(struct mdio_device *mdiodev);
75 /* Clears up any memory if needed */
76 void (*remove)(struct mdio_device *mdiodev);
78 /* Quiesces the device on system shutdown, turns off interrupts etc */
79 void (*shutdown)(struct mdio_device *mdiodev);
82 static inline struct mdio_driver *
83 to_mdio_driver(const struct device_driver *driver)
85 return container_of(to_mdio_common_driver(driver), struct mdio_driver,
89 /* device driver data */
90 static inline void mdiodev_set_drvdata(struct mdio_device *mdio, void *data)
92 dev_set_drvdata(&mdio->dev, data);
95 static inline void *mdiodev_get_drvdata(struct mdio_device *mdio)
97 return dev_get_drvdata(&mdio->dev);
100 void mdio_device_free(struct mdio_device *mdiodev);
101 struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr);
102 int mdio_device_register(struct mdio_device *mdiodev);
103 void mdio_device_remove(struct mdio_device *mdiodev);
104 void mdio_device_reset(struct mdio_device *mdiodev, int value);
105 int mdio_driver_register(struct mdio_driver *drv);
106 void mdio_driver_unregister(struct mdio_driver *drv);
107 int mdio_device_bus_match(struct device *dev, struct device_driver *drv);
109 static inline bool mdio_phy_id_is_c45(int phy_id)
111 return (phy_id & MDIO_PHY_ID_C45) && !(phy_id & ~MDIO_PHY_ID_C45_MASK);
114 static inline __u16 mdio_phy_id_prtad(int phy_id)
116 return (phy_id & MDIO_PHY_ID_PRTAD) >> 5;
119 static inline __u16 mdio_phy_id_devad(int phy_id)
121 return phy_id & MDIO_PHY_ID_DEVAD;
125 * struct mdio_if_info - Ethernet controller MDIO interface
126 * @prtad: PRTAD of the PHY (%MDIO_PRTAD_NONE if not present/unknown)
127 * @mmds: Mask of MMDs expected to be present in the PHY. This must be
128 * non-zero unless @prtad = %MDIO_PRTAD_NONE.
129 * @mode_support: MDIO modes supported. If %MDIO_SUPPORTS_C22 is set then
130 * MII register access will be passed through with @devad =
131 * %MDIO_DEVAD_NONE. If %MDIO_EMULATE_C22 is set then access to
132 * commonly used clause 22 registers will be translated into
133 * clause 45 registers.
134 * @dev: Net device structure
135 * @mdio_read: Register read function; returns value or negative error code
136 * @mdio_write: Register write function; returns 0 or negative error code
138 struct mdio_if_info {
141 unsigned mode_support;
143 struct net_device *dev;
144 int (*mdio_read)(struct net_device *dev, int prtad, int devad,
146 int (*mdio_write)(struct net_device *dev, int prtad, int devad,
150 #define MDIO_PRTAD_NONE (-1)
151 #define MDIO_DEVAD_NONE (-1)
152 #define MDIO_SUPPORTS_C22 1
153 #define MDIO_SUPPORTS_C45 2
154 #define MDIO_EMULATE_C22 4
157 struct ethtool_pauseparam;
158 extern int mdio45_probe(struct mdio_if_info *mdio, int prtad);
159 extern int mdio_set_flag(const struct mdio_if_info *mdio,
160 int prtad, int devad, u16 addr, int mask,
162 extern int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmds);
163 extern int mdio45_nway_restart(const struct mdio_if_info *mdio);
164 extern void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio,
165 struct ethtool_cmd *ecmd,
166 u32 npage_adv, u32 npage_lpa);
168 mdio45_ethtool_ksettings_get_npage(const struct mdio_if_info *mdio,
169 struct ethtool_link_ksettings *cmd,
170 u32 npage_adv, u32 npage_lpa);
173 * mdio45_ethtool_gset - get settings for ETHTOOL_GSET
174 * @mdio: MDIO interface
175 * @ecmd: Ethtool request structure
177 * Since the CSRs for auto-negotiation using next pages are not fully
178 * standardised, this function does not attempt to decode them. Use
179 * mdio45_ethtool_gset_npage() to specify advertisement bits from next
182 static inline void mdio45_ethtool_gset(const struct mdio_if_info *mdio,
183 struct ethtool_cmd *ecmd)
185 mdio45_ethtool_gset_npage(mdio, ecmd, 0, 0);
189 * mdio45_ethtool_ksettings_get - get settings for ETHTOOL_GLINKSETTINGS
190 * @mdio: MDIO interface
191 * @cmd: Ethtool request structure
193 * Since the CSRs for auto-negotiation using next pages are not fully
194 * standardised, this function does not attempt to decode them. Use
195 * mdio45_ethtool_ksettings_get_npage() to specify advertisement bits
199 mdio45_ethtool_ksettings_get(const struct mdio_if_info *mdio,
200 struct ethtool_link_ksettings *cmd)
202 mdio45_ethtool_ksettings_get_npage(mdio, cmd, 0, 0);
205 extern int mdio_mii_ioctl(const struct mdio_if_info *mdio,
206 struct mii_ioctl_data *mii_data, int cmd);
209 * mmd_eee_cap_to_ethtool_sup_t
210 * @eee_cap: value of the MMD EEE Capability register
212 * A small helper function that translates MMD EEE Capability (3.20) bits
213 * to ethtool supported settings.
215 static inline u32 mmd_eee_cap_to_ethtool_sup_t(u16 eee_cap)
219 if (eee_cap & MDIO_EEE_100TX)
220 supported |= SUPPORTED_100baseT_Full;
221 if (eee_cap & MDIO_EEE_1000T)
222 supported |= SUPPORTED_1000baseT_Full;
223 if (eee_cap & MDIO_EEE_10GT)
224 supported |= SUPPORTED_10000baseT_Full;
225 if (eee_cap & MDIO_EEE_1000KX)
226 supported |= SUPPORTED_1000baseKX_Full;
227 if (eee_cap & MDIO_EEE_10GKX4)
228 supported |= SUPPORTED_10000baseKX4_Full;
229 if (eee_cap & MDIO_EEE_10GKR)
230 supported |= SUPPORTED_10000baseKR_Full;
236 * mmd_eee_adv_to_ethtool_adv_t
237 * @eee_adv: value of the MMD EEE Advertisement/Link Partner Ability registers
239 * A small helper function that translates the MMD EEE Advertisment (7.60)
240 * and MMD EEE Link Partner Ability (7.61) bits to ethtool advertisement
243 static inline u32 mmd_eee_adv_to_ethtool_adv_t(u16 eee_adv)
247 if (eee_adv & MDIO_EEE_100TX)
248 adv |= ADVERTISED_100baseT_Full;
249 if (eee_adv & MDIO_EEE_1000T)
250 adv |= ADVERTISED_1000baseT_Full;
251 if (eee_adv & MDIO_EEE_10GT)
252 adv |= ADVERTISED_10000baseT_Full;
253 if (eee_adv & MDIO_EEE_1000KX)
254 adv |= ADVERTISED_1000baseKX_Full;
255 if (eee_adv & MDIO_EEE_10GKX4)
256 adv |= ADVERTISED_10000baseKX4_Full;
257 if (eee_adv & MDIO_EEE_10GKR)
258 adv |= ADVERTISED_10000baseKR_Full;
264 * ethtool_adv_to_mmd_eee_adv_t
265 * @adv: the ethtool advertisement settings
267 * A small helper function that translates ethtool advertisement settings
268 * to EEE advertisements for the MMD EEE Advertisement (7.60) and
269 * MMD EEE Link Partner Ability (7.61) registers.
271 static inline u16 ethtool_adv_to_mmd_eee_adv_t(u32 adv)
275 if (adv & ADVERTISED_100baseT_Full)
276 reg |= MDIO_EEE_100TX;
277 if (adv & ADVERTISED_1000baseT_Full)
278 reg |= MDIO_EEE_1000T;
279 if (adv & ADVERTISED_10000baseT_Full)
280 reg |= MDIO_EEE_10GT;
281 if (adv & ADVERTISED_1000baseKX_Full)
282 reg |= MDIO_EEE_1000KX;
283 if (adv & ADVERTISED_10000baseKX4_Full)
284 reg |= MDIO_EEE_10GKX4;
285 if (adv & ADVERTISED_10000baseKR_Full)
286 reg |= MDIO_EEE_10GKR;
292 * linkmode_adv_to_mii_10gbt_adv_t
293 * @advertising: the linkmode advertisement settings
295 * A small helper function that translates linkmode advertisement
296 * settings to phy autonegotiation advertisements for the C45
297 * 10GBASE-T AN CONTROL (7.32) register.
299 static inline u32 linkmode_adv_to_mii_10gbt_adv_t(unsigned long *advertising)
303 if (linkmode_test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
305 result |= MDIO_AN_10GBT_CTRL_ADV2_5G;
306 if (linkmode_test_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
308 result |= MDIO_AN_10GBT_CTRL_ADV5G;
309 if (linkmode_test_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
311 result |= MDIO_AN_10GBT_CTRL_ADV10G;
317 * mii_10gbt_stat_mod_linkmode_lpa_t
318 * @advertising: target the linkmode advertisement settings
319 * @lpa: value of the C45 10GBASE-T AN STATUS register
321 * A small helper function that translates C45 10GBASE-T AN STATUS register bits
322 * to linkmode advertisement settings. Other bits in advertising aren't changed.
324 static inline void mii_10gbt_stat_mod_linkmode_lpa_t(unsigned long *advertising,
327 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
328 advertising, lpa & MDIO_AN_10GBT_STAT_LP2_5G);
329 linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
330 advertising, lpa & MDIO_AN_10GBT_STAT_LP5G);
331 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
332 advertising, lpa & MDIO_AN_10GBT_STAT_LP10G);
336 * mii_t1_adv_l_mod_linkmode_t
337 * @advertising: target the linkmode advertisement settings
338 * @lpa: value of the BASE-T1 Autonegotiation Advertisement [15:0] Register
340 * A small helper function that translates BASE-T1 Autonegotiation
341 * Advertisement [15:0] Register bits to linkmode advertisement settings.
342 * Other bits in advertising aren't changed.
344 static inline void mii_t1_adv_l_mod_linkmode_t(unsigned long *advertising, u32 lpa)
346 linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, advertising,
347 lpa & MDIO_AN_T1_ADV_L_PAUSE_CAP);
348 linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, advertising,
349 lpa & MDIO_AN_T1_ADV_L_PAUSE_ASYM);
353 * mii_t1_adv_m_mod_linkmode_t
354 * @advertising: target the linkmode advertisement settings
355 * @lpa: value of the BASE-T1 Autonegotiation Advertisement [31:16] Register
357 * A small helper function that translates BASE-T1 Autonegotiation
358 * Advertisement [31:16] Register bits to linkmode advertisement settings.
359 * Other bits in advertising aren't changed.
361 static inline void mii_t1_adv_m_mod_linkmode_t(unsigned long *advertising, u32 lpa)
363 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT1L_Full_BIT,
364 advertising, lpa & MDIO_AN_T1_ADV_M_B10L);
368 * linkmode_adv_to_mii_t1_adv_l_t
369 * @advertising: the linkmode advertisement settings
371 * A small helper function that translates linkmode advertisement
372 * settings to phy autonegotiation advertisements for the
373 * BASE-T1 Autonegotiation Advertisement [15:0] Register.
375 static inline u32 linkmode_adv_to_mii_t1_adv_l_t(unsigned long *advertising)
379 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, advertising))
380 result |= MDIO_AN_T1_ADV_L_PAUSE_CAP;
381 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, advertising))
382 result |= MDIO_AN_T1_ADV_L_PAUSE_ASYM;
388 * linkmode_adv_to_mii_t1_adv_m_t
389 * @advertising: the linkmode advertisement settings
391 * A small helper function that translates linkmode advertisement
392 * settings to phy autonegotiation advertisements for the
393 * BASE-T1 Autonegotiation Advertisement [31:16] Register.
395 static inline u32 linkmode_adv_to_mii_t1_adv_m_t(unsigned long *advertising)
399 if (linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT1L_Full_BIT, advertising))
400 result |= MDIO_AN_T1_ADV_M_B10L;
406 * mii_eee_cap1_mod_linkmode_t()
407 * @adv: target the linkmode advertisement settings
408 * @val: register value
410 * A function that translates value of following registers to the linkmode:
411 * IEEE 802.3-2018 45.2.3.10 "EEE control and capability 1" register (3.20)
412 * IEEE 802.3-2018 45.2.7.13 "EEE advertisement 1" register (7.60)
413 * IEEE 802.3-2018 45.2.7.14 "EEE "link partner ability 1 register (7.61)
415 static inline void mii_eee_cap1_mod_linkmode_t(unsigned long *adv, u32 val)
417 linkmode_mod_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
418 adv, val & MDIO_EEE_100TX);
419 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
420 adv, val & MDIO_EEE_1000T);
421 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
422 adv, val & MDIO_EEE_10GT);
423 linkmode_mod_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
424 adv, val & MDIO_EEE_1000KX);
425 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
426 adv, val & MDIO_EEE_10GKX4);
427 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
428 adv, val & MDIO_EEE_10GKR);
432 * linkmode_to_mii_eee_cap1_t()
433 * @adv: the linkmode advertisement settings
435 * A function that translates linkmode to value for IEEE 802.3-2018 45.2.7.13
436 * "EEE advertisement 1" register (7.60)
438 static inline u32 linkmode_to_mii_eee_cap1_t(unsigned long *adv)
442 if (linkmode_test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, adv))
443 result |= MDIO_EEE_100TX;
444 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, adv))
445 result |= MDIO_EEE_1000T;
446 if (linkmode_test_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, adv))
447 result |= MDIO_EEE_10GT;
448 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, adv))
449 result |= MDIO_EEE_1000KX;
450 if (linkmode_test_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, adv))
451 result |= MDIO_EEE_10GKX4;
452 if (linkmode_test_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, adv))
453 result |= MDIO_EEE_10GKR;
459 * mii_10base_t1_adv_mod_linkmode_t()
460 * @adv: linkmode advertisement settings
461 * @val: register value
463 * A function that translates IEEE 802.3cg-2019 45.2.7.26 "10BASE-T1 AN status"
464 * register (7.527) value to the linkmode.
466 static inline void mii_10base_t1_adv_mod_linkmode_t(unsigned long *adv, u16 val)
468 linkmode_mod_bit(ETHTOOL_LINK_MODE_10baseT1L_Full_BIT,
469 adv, val & MDIO_AN_10BT1_AN_CTRL_ADV_EEE_T1L);
473 * linkmode_adv_to_mii_10base_t1_t()
474 * @adv: linkmode advertisement settings
476 * A function that translates the linkmode to IEEE 802.3cg-2019 45.2.7.25
477 * "10BASE-T1 AN control" register (7.526) value.
479 static inline u32 linkmode_adv_to_mii_10base_t1_t(unsigned long *adv)
483 if (linkmode_test_bit(ETHTOOL_LINK_MODE_10baseT1L_Full_BIT, adv))
484 result |= MDIO_AN_10BT1_AN_CTRL_ADV_EEE_T1L;
489 int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
490 int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
491 int __mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
494 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
495 int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum);
496 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
497 int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val);
498 int mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask,
500 int mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
502 int __mdiobus_c45_read(struct mii_bus *bus, int addr, int devad, u32 regnum);
503 int mdiobus_c45_read(struct mii_bus *bus, int addr, int devad, u32 regnum);
504 int mdiobus_c45_read_nested(struct mii_bus *bus, int addr, int devad,
506 int __mdiobus_c45_write(struct mii_bus *bus, int addr, int devad, u32 regnum,
508 int mdiobus_c45_write(struct mii_bus *bus, int addr, int devad, u32 regnum,
510 int mdiobus_c45_write_nested(struct mii_bus *bus, int addr, int devad,
511 u32 regnum, u16 val);
512 int mdiobus_c45_modify(struct mii_bus *bus, int addr, int devad, u32 regnum,
515 int mdiobus_c45_modify_changed(struct mii_bus *bus, int addr, int devad,
516 u32 regnum, u16 mask, u16 set);
518 static inline int mdiodev_read(struct mdio_device *mdiodev, u32 regnum)
520 return mdiobus_read(mdiodev->bus, mdiodev->addr, regnum);
523 static inline int mdiodev_write(struct mdio_device *mdiodev, u32 regnum,
526 return mdiobus_write(mdiodev->bus, mdiodev->addr, regnum, val);
529 static inline int mdiodev_modify(struct mdio_device *mdiodev, u32 regnum,
532 return mdiobus_modify(mdiodev->bus, mdiodev->addr, regnum, mask, set);
535 static inline int mdiodev_modify_changed(struct mdio_device *mdiodev,
536 u32 regnum, u16 mask, u16 set)
538 return mdiobus_modify_changed(mdiodev->bus, mdiodev->addr, regnum,
542 static inline int mdiodev_c45_modify(struct mdio_device *mdiodev, int devad,
543 u32 regnum, u16 mask, u16 set)
545 return mdiobus_c45_modify(mdiodev->bus, mdiodev->addr, devad, regnum,
549 static inline int mdiodev_c45_modify_changed(struct mdio_device *mdiodev,
550 int devad, u32 regnum, u16 mask,
553 return mdiobus_c45_modify_changed(mdiodev->bus, mdiodev->addr, devad,
557 static inline int mdiodev_c45_read(struct mdio_device *mdiodev, int devad,
560 return mdiobus_c45_read(mdiodev->bus, mdiodev->addr, devad, regnum);
563 static inline int mdiodev_c45_write(struct mdio_device *mdiodev, u32 devad,
566 return mdiobus_c45_write(mdiodev->bus, mdiodev->addr, devad, regnum,
570 int mdiobus_register_device(struct mdio_device *mdiodev);
571 int mdiobus_unregister_device(struct mdio_device *mdiodev);
572 bool mdiobus_is_registered_device(struct mii_bus *bus, int addr);
573 struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr);
576 * mdio_module_driver() - Helper macro for registering mdio drivers
577 * @_mdio_driver: driver to register
579 * Helper macro for MDIO drivers which do not do anything special in module
580 * init/exit. Each module may only use this macro once, and calling it
581 * replaces module_init() and module_exit().
583 #define mdio_module_driver(_mdio_driver) \
584 static int __init mdio_module_init(void) \
586 return mdio_driver_register(&_mdio_driver); \
588 module_init(mdio_module_init); \
589 static void __exit mdio_module_exit(void) \
591 mdio_driver_unregister(&_mdio_driver); \
593 module_exit(mdio_module_exit)
595 #endif /* __LINUX_MDIO_H__ */