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>
13 /* Or MII_ADDR_C45 into regnum for read/write on mii_bus to enable the 21 bit
14 * IEEE 802.3ae clause 45 addressing mode used by 10GIGE phy chips.
16 #define MII_ADDR_C45 (1<<30)
17 #define MII_DEVADDR_C45_SHIFT 16
18 #define MII_DEVADDR_C45_MASK GENMASK(20, 16)
19 #define MII_REGADDR_C45_MASK GENMASK(15, 0)
25 /* Multiple levels of nesting are possible. However typically this is
26 * limited to nested DSA like layer, a MUX layer, and the normal
27 * user. Instead of trying to handle the general case, just define
30 enum mdio_mutex_lock_class {
40 char modalias[MDIO_NAME_SIZE];
42 int (*bus_match)(struct device *dev, struct device_driver *drv);
43 void (*device_free)(struct mdio_device *mdiodev);
44 void (*device_remove)(struct mdio_device *mdiodev);
46 /* Bus address of the MDIO device (0-31) */
49 struct gpio_desc *reset_gpio;
50 struct reset_control *reset_ctrl;
51 unsigned int reset_assert_delay;
52 unsigned int reset_deassert_delay;
55 static inline struct mdio_device *to_mdio_device(const struct device *dev)
57 return container_of(dev, struct mdio_device, dev);
60 /* struct mdio_driver_common: Common to all MDIO drivers */
61 struct mdio_driver_common {
62 struct device_driver driver;
65 #define MDIO_DEVICE_FLAG_PHY 1
67 static inline struct mdio_driver_common *
68 to_mdio_common_driver(const struct device_driver *driver)
70 return container_of(driver, struct mdio_driver_common, driver);
73 /* struct mdio_driver: Generic MDIO driver */
75 struct mdio_driver_common mdiodrv;
78 * Called during discovery. Used to set
79 * up device-specific structures, if any
81 int (*probe)(struct mdio_device *mdiodev);
83 /* Clears up any memory if needed */
84 void (*remove)(struct mdio_device *mdiodev);
86 /* Quiesces the device on system shutdown, turns off interrupts etc */
87 void (*shutdown)(struct mdio_device *mdiodev);
90 static inline struct mdio_driver *
91 to_mdio_driver(const struct device_driver *driver)
93 return container_of(to_mdio_common_driver(driver), struct mdio_driver,
97 /* device driver data */
98 static inline void mdiodev_set_drvdata(struct mdio_device *mdio, void *data)
100 dev_set_drvdata(&mdio->dev, data);
103 static inline void *mdiodev_get_drvdata(struct mdio_device *mdio)
105 return dev_get_drvdata(&mdio->dev);
108 void mdio_device_free(struct mdio_device *mdiodev);
109 struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr);
110 int mdio_device_register(struct mdio_device *mdiodev);
111 void mdio_device_remove(struct mdio_device *mdiodev);
112 void mdio_device_reset(struct mdio_device *mdiodev, int value);
113 int mdio_driver_register(struct mdio_driver *drv);
114 void mdio_driver_unregister(struct mdio_driver *drv);
115 int mdio_device_bus_match(struct device *dev, struct device_driver *drv);
117 static inline bool mdio_phy_id_is_c45(int phy_id)
119 return (phy_id & MDIO_PHY_ID_C45) && !(phy_id & ~MDIO_PHY_ID_C45_MASK);
122 static inline __u16 mdio_phy_id_prtad(int phy_id)
124 return (phy_id & MDIO_PHY_ID_PRTAD) >> 5;
127 static inline __u16 mdio_phy_id_devad(int phy_id)
129 return phy_id & MDIO_PHY_ID_DEVAD;
133 * struct mdio_if_info - Ethernet controller MDIO interface
134 * @prtad: PRTAD of the PHY (%MDIO_PRTAD_NONE if not present/unknown)
135 * @mmds: Mask of MMDs expected to be present in the PHY. This must be
136 * non-zero unless @prtad = %MDIO_PRTAD_NONE.
137 * @mode_support: MDIO modes supported. If %MDIO_SUPPORTS_C22 is set then
138 * MII register access will be passed through with @devad =
139 * %MDIO_DEVAD_NONE. If %MDIO_EMULATE_C22 is set then access to
140 * commonly used clause 22 registers will be translated into
141 * clause 45 registers.
142 * @dev: Net device structure
143 * @mdio_read: Register read function; returns value or negative error code
144 * @mdio_write: Register write function; returns 0 or negative error code
146 struct mdio_if_info {
149 unsigned mode_support;
151 struct net_device *dev;
152 int (*mdio_read)(struct net_device *dev, int prtad, int devad,
154 int (*mdio_write)(struct net_device *dev, int prtad, int devad,
158 #define MDIO_PRTAD_NONE (-1)
159 #define MDIO_DEVAD_NONE (-1)
160 #define MDIO_SUPPORTS_C22 1
161 #define MDIO_SUPPORTS_C45 2
162 #define MDIO_EMULATE_C22 4
165 struct ethtool_pauseparam;
166 extern int mdio45_probe(struct mdio_if_info *mdio, int prtad);
167 extern int mdio_set_flag(const struct mdio_if_info *mdio,
168 int prtad, int devad, u16 addr, int mask,
170 extern int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmds);
171 extern int mdio45_nway_restart(const struct mdio_if_info *mdio);
172 extern void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio,
173 struct ethtool_cmd *ecmd,
174 u32 npage_adv, u32 npage_lpa);
176 mdio45_ethtool_ksettings_get_npage(const struct mdio_if_info *mdio,
177 struct ethtool_link_ksettings *cmd,
178 u32 npage_adv, u32 npage_lpa);
181 * mdio45_ethtool_gset - get settings for ETHTOOL_GSET
182 * @mdio: MDIO interface
183 * @ecmd: Ethtool request structure
185 * Since the CSRs for auto-negotiation using next pages are not fully
186 * standardised, this function does not attempt to decode them. Use
187 * mdio45_ethtool_gset_npage() to specify advertisement bits from next
190 static inline void mdio45_ethtool_gset(const struct mdio_if_info *mdio,
191 struct ethtool_cmd *ecmd)
193 mdio45_ethtool_gset_npage(mdio, ecmd, 0, 0);
197 * mdio45_ethtool_ksettings_get - get settings for ETHTOOL_GLINKSETTINGS
198 * @mdio: MDIO interface
199 * @cmd: Ethtool request structure
201 * Since the CSRs for auto-negotiation using next pages are not fully
202 * standardised, this function does not attempt to decode them. Use
203 * mdio45_ethtool_ksettings_get_npage() to specify advertisement bits
207 mdio45_ethtool_ksettings_get(const struct mdio_if_info *mdio,
208 struct ethtool_link_ksettings *cmd)
210 mdio45_ethtool_ksettings_get_npage(mdio, cmd, 0, 0);
213 extern int mdio_mii_ioctl(const struct mdio_if_info *mdio,
214 struct mii_ioctl_data *mii_data, int cmd);
217 * mmd_eee_cap_to_ethtool_sup_t
218 * @eee_cap: value of the MMD EEE Capability register
220 * A small helper function that translates MMD EEE Capability (3.20) bits
221 * to ethtool supported settings.
223 static inline u32 mmd_eee_cap_to_ethtool_sup_t(u16 eee_cap)
227 if (eee_cap & MDIO_EEE_100TX)
228 supported |= SUPPORTED_100baseT_Full;
229 if (eee_cap & MDIO_EEE_1000T)
230 supported |= SUPPORTED_1000baseT_Full;
231 if (eee_cap & MDIO_EEE_10GT)
232 supported |= SUPPORTED_10000baseT_Full;
233 if (eee_cap & MDIO_EEE_1000KX)
234 supported |= SUPPORTED_1000baseKX_Full;
235 if (eee_cap & MDIO_EEE_10GKX4)
236 supported |= SUPPORTED_10000baseKX4_Full;
237 if (eee_cap & MDIO_EEE_10GKR)
238 supported |= SUPPORTED_10000baseKR_Full;
244 * mmd_eee_adv_to_ethtool_adv_t
245 * @eee_adv: value of the MMD EEE Advertisement/Link Partner Ability registers
247 * A small helper function that translates the MMD EEE Advertisment (7.60)
248 * and MMD EEE Link Partner Ability (7.61) bits to ethtool advertisement
251 static inline u32 mmd_eee_adv_to_ethtool_adv_t(u16 eee_adv)
255 if (eee_adv & MDIO_EEE_100TX)
256 adv |= ADVERTISED_100baseT_Full;
257 if (eee_adv & MDIO_EEE_1000T)
258 adv |= ADVERTISED_1000baseT_Full;
259 if (eee_adv & MDIO_EEE_10GT)
260 adv |= ADVERTISED_10000baseT_Full;
261 if (eee_adv & MDIO_EEE_1000KX)
262 adv |= ADVERTISED_1000baseKX_Full;
263 if (eee_adv & MDIO_EEE_10GKX4)
264 adv |= ADVERTISED_10000baseKX4_Full;
265 if (eee_adv & MDIO_EEE_10GKR)
266 adv |= ADVERTISED_10000baseKR_Full;
272 * ethtool_adv_to_mmd_eee_adv_t
273 * @adv: the ethtool advertisement settings
275 * A small helper function that translates ethtool advertisement settings
276 * to EEE advertisements for the MMD EEE Advertisement (7.60) and
277 * MMD EEE Link Partner Ability (7.61) registers.
279 static inline u16 ethtool_adv_to_mmd_eee_adv_t(u32 adv)
283 if (adv & ADVERTISED_100baseT_Full)
284 reg |= MDIO_EEE_100TX;
285 if (adv & ADVERTISED_1000baseT_Full)
286 reg |= MDIO_EEE_1000T;
287 if (adv & ADVERTISED_10000baseT_Full)
288 reg |= MDIO_EEE_10GT;
289 if (adv & ADVERTISED_1000baseKX_Full)
290 reg |= MDIO_EEE_1000KX;
291 if (adv & ADVERTISED_10000baseKX4_Full)
292 reg |= MDIO_EEE_10GKX4;
293 if (adv & ADVERTISED_10000baseKR_Full)
294 reg |= MDIO_EEE_10GKR;
300 * linkmode_adv_to_mii_10gbt_adv_t
301 * @advertising: the linkmode advertisement settings
303 * A small helper function that translates linkmode advertisement
304 * settings to phy autonegotiation advertisements for the C45
305 * 10GBASE-T AN CONTROL (7.32) register.
307 static inline u32 linkmode_adv_to_mii_10gbt_adv_t(unsigned long *advertising)
311 if (linkmode_test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
313 result |= MDIO_AN_10GBT_CTRL_ADV2_5G;
314 if (linkmode_test_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
316 result |= MDIO_AN_10GBT_CTRL_ADV5G;
317 if (linkmode_test_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
319 result |= MDIO_AN_10GBT_CTRL_ADV10G;
325 * mii_10gbt_stat_mod_linkmode_lpa_t
326 * @advertising: target the linkmode advertisement settings
327 * @lpa: value of the C45 10GBASE-T AN STATUS register
329 * A small helper function that translates C45 10GBASE-T AN STATUS register bits
330 * to linkmode advertisement settings. Other bits in advertising aren't changed.
332 static inline void mii_10gbt_stat_mod_linkmode_lpa_t(unsigned long *advertising,
335 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
336 advertising, lpa & MDIO_AN_10GBT_STAT_LP2_5G);
337 linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
338 advertising, lpa & MDIO_AN_10GBT_STAT_LP5G);
339 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
340 advertising, lpa & MDIO_AN_10GBT_STAT_LP10G);
343 int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
344 int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
345 int __mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
348 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
349 int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum);
350 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
351 int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val);
352 int mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask,
354 int mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
357 static inline int mdiodev_read(struct mdio_device *mdiodev, u32 regnum)
359 return mdiobus_read(mdiodev->bus, mdiodev->addr, regnum);
362 static inline int mdiodev_write(struct mdio_device *mdiodev, u32 regnum,
365 return mdiobus_write(mdiodev->bus, mdiodev->addr, regnum, val);
368 static inline int mdiodev_modify(struct mdio_device *mdiodev, u32 regnum,
371 return mdiobus_modify(mdiodev->bus, mdiodev->addr, regnum, mask, set);
374 static inline int mdiodev_modify_changed(struct mdio_device *mdiodev,
375 u32 regnum, u16 mask, u16 set)
377 return mdiobus_modify_changed(mdiodev->bus, mdiodev->addr, regnum,
381 static inline u32 mdiobus_c45_addr(int devad, u16 regnum)
383 return MII_ADDR_C45 | devad << MII_DEVADDR_C45_SHIFT | regnum;
386 static inline u16 mdiobus_c45_regad(u32 regnum)
388 return FIELD_GET(MII_REGADDR_C45_MASK, regnum);
391 static inline u16 mdiobus_c45_devad(u32 regnum)
393 return FIELD_GET(MII_DEVADDR_C45_MASK, regnum);
396 static inline int __mdiobus_c45_read(struct mii_bus *bus, int prtad, int devad,
399 return __mdiobus_read(bus, prtad, mdiobus_c45_addr(devad, regnum));
402 static inline int __mdiobus_c45_write(struct mii_bus *bus, int prtad, int devad,
405 return __mdiobus_write(bus, prtad, mdiobus_c45_addr(devad, regnum),
409 static inline int mdiobus_c45_read(struct mii_bus *bus, int prtad, int devad,
412 return mdiobus_read(bus, prtad, mdiobus_c45_addr(devad, regnum));
415 static inline int mdiobus_c45_write(struct mii_bus *bus, int prtad, int devad,
418 return mdiobus_write(bus, prtad, mdiobus_c45_addr(devad, regnum), val);
421 int mdiobus_register_device(struct mdio_device *mdiodev);
422 int mdiobus_unregister_device(struct mdio_device *mdiodev);
423 bool mdiobus_is_registered_device(struct mii_bus *bus, int addr);
424 struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr);
427 * mdio_module_driver() - Helper macro for registering mdio drivers
428 * @_mdio_driver: driver to register
430 * Helper macro for MDIO drivers which do not do anything special in module
431 * init/exit. Each module may only use this macro once, and calling it
432 * replaces module_init() and module_exit().
434 #define mdio_module_driver(_mdio_driver) \
435 static int __init mdio_module_init(void) \
437 return mdio_driver_register(&_mdio_driver); \
439 module_init(mdio_module_init); \
440 static void __exit mdio_module_exit(void) \
442 mdio_driver_unregister(&_mdio_driver); \
444 module_exit(mdio_module_exit)
446 #endif /* __LINUX_MDIO_H__ */