at v5.5 11 kB view raw
1/* SPDX-License-Identifier: GPL-2.0-only */ 2/* 3 * linux/mdio.h: definitions for MDIO (clause 45) transceivers 4 * Copyright 2006-2009 Solarflare Communications Inc. 5 */ 6#ifndef __LINUX_MDIO_H__ 7#define __LINUX_MDIO_H__ 8 9#include <uapi/linux/mdio.h> 10#include <linux/mod_devicetable.h> 11 12struct gpio_desc; 13struct mii_bus; 14 15/* Multiple levels of nesting are possible. However typically this is 16 * limited to nested DSA like layer, a MUX layer, and the normal 17 * user. Instead of trying to handle the general case, just define 18 * these cases. 19 */ 20enum mdio_mutex_lock_class { 21 MDIO_MUTEX_NORMAL, 22 MDIO_MUTEX_MUX, 23 MDIO_MUTEX_NESTED, 24}; 25 26struct mdio_device { 27 struct device dev; 28 29 struct mii_bus *bus; 30 char modalias[MDIO_NAME_SIZE]; 31 32 int (*bus_match)(struct device *dev, struct device_driver *drv); 33 void (*device_free)(struct mdio_device *mdiodev); 34 void (*device_remove)(struct mdio_device *mdiodev); 35 36 /* Bus address of the MDIO device (0-31) */ 37 int addr; 38 int flags; 39 struct gpio_desc *reset_gpio; 40 struct reset_control *reset_ctrl; 41 unsigned int reset_assert_delay; 42 unsigned int reset_deassert_delay; 43}; 44#define to_mdio_device(d) container_of(d, struct mdio_device, dev) 45 46/* struct mdio_driver_common: Common to all MDIO drivers */ 47struct mdio_driver_common { 48 struct device_driver driver; 49 int flags; 50}; 51#define MDIO_DEVICE_FLAG_PHY 1 52#define to_mdio_common_driver(d) \ 53 container_of(d, struct mdio_driver_common, driver) 54 55/* struct mdio_driver: Generic MDIO driver */ 56struct mdio_driver { 57 struct mdio_driver_common mdiodrv; 58 59 /* 60 * Called during discovery. Used to set 61 * up device-specific structures, if any 62 */ 63 int (*probe)(struct mdio_device *mdiodev); 64 65 /* Clears up any memory if needed */ 66 void (*remove)(struct mdio_device *mdiodev); 67}; 68#define to_mdio_driver(d) \ 69 container_of(to_mdio_common_driver(d), struct mdio_driver, mdiodrv) 70 71/* device driver data */ 72static inline void mdiodev_set_drvdata(struct mdio_device *mdio, void *data) 73{ 74 dev_set_drvdata(&mdio->dev, data); 75} 76 77static inline void *mdiodev_get_drvdata(struct mdio_device *mdio) 78{ 79 return dev_get_drvdata(&mdio->dev); 80} 81 82void mdio_device_free(struct mdio_device *mdiodev); 83struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr); 84int mdio_device_register(struct mdio_device *mdiodev); 85void mdio_device_remove(struct mdio_device *mdiodev); 86void mdio_device_reset(struct mdio_device *mdiodev, int value); 87int mdio_driver_register(struct mdio_driver *drv); 88void mdio_driver_unregister(struct mdio_driver *drv); 89int mdio_device_bus_match(struct device *dev, struct device_driver *drv); 90 91static inline bool mdio_phy_id_is_c45(int phy_id) 92{ 93 return (phy_id & MDIO_PHY_ID_C45) && !(phy_id & ~MDIO_PHY_ID_C45_MASK); 94} 95 96static inline __u16 mdio_phy_id_prtad(int phy_id) 97{ 98 return (phy_id & MDIO_PHY_ID_PRTAD) >> 5; 99} 100 101static inline __u16 mdio_phy_id_devad(int phy_id) 102{ 103 return phy_id & MDIO_PHY_ID_DEVAD; 104} 105 106/** 107 * struct mdio_if_info - Ethernet controller MDIO interface 108 * @prtad: PRTAD of the PHY (%MDIO_PRTAD_NONE if not present/unknown) 109 * @mmds: Mask of MMDs expected to be present in the PHY. This must be 110 * non-zero unless @prtad = %MDIO_PRTAD_NONE. 111 * @mode_support: MDIO modes supported. If %MDIO_SUPPORTS_C22 is set then 112 * MII register access will be passed through with @devad = 113 * %MDIO_DEVAD_NONE. If %MDIO_EMULATE_C22 is set then access to 114 * commonly used clause 22 registers will be translated into 115 * clause 45 registers. 116 * @dev: Net device structure 117 * @mdio_read: Register read function; returns value or negative error code 118 * @mdio_write: Register write function; returns 0 or negative error code 119 */ 120struct mdio_if_info { 121 int prtad; 122 u32 mmds; 123 unsigned mode_support; 124 125 struct net_device *dev; 126 int (*mdio_read)(struct net_device *dev, int prtad, int devad, 127 u16 addr); 128 int (*mdio_write)(struct net_device *dev, int prtad, int devad, 129 u16 addr, u16 val); 130}; 131 132#define MDIO_PRTAD_NONE (-1) 133#define MDIO_DEVAD_NONE (-1) 134#define MDIO_SUPPORTS_C22 1 135#define MDIO_SUPPORTS_C45 2 136#define MDIO_EMULATE_C22 4 137 138struct ethtool_cmd; 139struct ethtool_pauseparam; 140extern int mdio45_probe(struct mdio_if_info *mdio, int prtad); 141extern int mdio_set_flag(const struct mdio_if_info *mdio, 142 int prtad, int devad, u16 addr, int mask, 143 bool sense); 144extern int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmds); 145extern int mdio45_nway_restart(const struct mdio_if_info *mdio); 146extern void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio, 147 struct ethtool_cmd *ecmd, 148 u32 npage_adv, u32 npage_lpa); 149extern void 150mdio45_ethtool_ksettings_get_npage(const struct mdio_if_info *mdio, 151 struct ethtool_link_ksettings *cmd, 152 u32 npage_adv, u32 npage_lpa); 153 154/** 155 * mdio45_ethtool_gset - get settings for ETHTOOL_GSET 156 * @mdio: MDIO interface 157 * @ecmd: Ethtool request structure 158 * 159 * Since the CSRs for auto-negotiation using next pages are not fully 160 * standardised, this function does not attempt to decode them. Use 161 * mdio45_ethtool_gset_npage() to specify advertisement bits from next 162 * pages. 163 */ 164static inline void mdio45_ethtool_gset(const struct mdio_if_info *mdio, 165 struct ethtool_cmd *ecmd) 166{ 167 mdio45_ethtool_gset_npage(mdio, ecmd, 0, 0); 168} 169 170/** 171 * mdio45_ethtool_ksettings_get - get settings for ETHTOOL_GLINKSETTINGS 172 * @mdio: MDIO interface 173 * @cmd: Ethtool request structure 174 * 175 * Since the CSRs for auto-negotiation using next pages are not fully 176 * standardised, this function does not attempt to decode them. Use 177 * mdio45_ethtool_ksettings_get_npage() to specify advertisement bits 178 * from next pages. 179 */ 180static inline void 181mdio45_ethtool_ksettings_get(const struct mdio_if_info *mdio, 182 struct ethtool_link_ksettings *cmd) 183{ 184 mdio45_ethtool_ksettings_get_npage(mdio, cmd, 0, 0); 185} 186 187extern int mdio_mii_ioctl(const struct mdio_if_info *mdio, 188 struct mii_ioctl_data *mii_data, int cmd); 189 190/** 191 * mmd_eee_cap_to_ethtool_sup_t 192 * @eee_cap: value of the MMD EEE Capability register 193 * 194 * A small helper function that translates MMD EEE Capability (3.20) bits 195 * to ethtool supported settings. 196 */ 197static inline u32 mmd_eee_cap_to_ethtool_sup_t(u16 eee_cap) 198{ 199 u32 supported = 0; 200 201 if (eee_cap & MDIO_EEE_100TX) 202 supported |= SUPPORTED_100baseT_Full; 203 if (eee_cap & MDIO_EEE_1000T) 204 supported |= SUPPORTED_1000baseT_Full; 205 if (eee_cap & MDIO_EEE_10GT) 206 supported |= SUPPORTED_10000baseT_Full; 207 if (eee_cap & MDIO_EEE_1000KX) 208 supported |= SUPPORTED_1000baseKX_Full; 209 if (eee_cap & MDIO_EEE_10GKX4) 210 supported |= SUPPORTED_10000baseKX4_Full; 211 if (eee_cap & MDIO_EEE_10GKR) 212 supported |= SUPPORTED_10000baseKR_Full; 213 214 return supported; 215} 216 217/** 218 * mmd_eee_adv_to_ethtool_adv_t 219 * @eee_adv: value of the MMD EEE Advertisement/Link Partner Ability registers 220 * 221 * A small helper function that translates the MMD EEE Advertisment (7.60) 222 * and MMD EEE Link Partner Ability (7.61) bits to ethtool advertisement 223 * settings. 224 */ 225static inline u32 mmd_eee_adv_to_ethtool_adv_t(u16 eee_adv) 226{ 227 u32 adv = 0; 228 229 if (eee_adv & MDIO_EEE_100TX) 230 adv |= ADVERTISED_100baseT_Full; 231 if (eee_adv & MDIO_EEE_1000T) 232 adv |= ADVERTISED_1000baseT_Full; 233 if (eee_adv & MDIO_EEE_10GT) 234 adv |= ADVERTISED_10000baseT_Full; 235 if (eee_adv & MDIO_EEE_1000KX) 236 adv |= ADVERTISED_1000baseKX_Full; 237 if (eee_adv & MDIO_EEE_10GKX4) 238 adv |= ADVERTISED_10000baseKX4_Full; 239 if (eee_adv & MDIO_EEE_10GKR) 240 adv |= ADVERTISED_10000baseKR_Full; 241 242 return adv; 243} 244 245/** 246 * ethtool_adv_to_mmd_eee_adv_t 247 * @adv: the ethtool advertisement settings 248 * 249 * A small helper function that translates ethtool advertisement settings 250 * to EEE advertisements for the MMD EEE Advertisement (7.60) and 251 * MMD EEE Link Partner Ability (7.61) registers. 252 */ 253static inline u16 ethtool_adv_to_mmd_eee_adv_t(u32 adv) 254{ 255 u16 reg = 0; 256 257 if (adv & ADVERTISED_100baseT_Full) 258 reg |= MDIO_EEE_100TX; 259 if (adv & ADVERTISED_1000baseT_Full) 260 reg |= MDIO_EEE_1000T; 261 if (adv & ADVERTISED_10000baseT_Full) 262 reg |= MDIO_EEE_10GT; 263 if (adv & ADVERTISED_1000baseKX_Full) 264 reg |= MDIO_EEE_1000KX; 265 if (adv & ADVERTISED_10000baseKX4_Full) 266 reg |= MDIO_EEE_10GKX4; 267 if (adv & ADVERTISED_10000baseKR_Full) 268 reg |= MDIO_EEE_10GKR; 269 270 return reg; 271} 272 273/** 274 * linkmode_adv_to_mii_10gbt_adv_t 275 * @advertising: the linkmode advertisement settings 276 * 277 * A small helper function that translates linkmode advertisement 278 * settings to phy autonegotiation advertisements for the C45 279 * 10GBASE-T AN CONTROL (7.32) register. 280 */ 281static inline u32 linkmode_adv_to_mii_10gbt_adv_t(unsigned long *advertising) 282{ 283 u32 result = 0; 284 285 if (linkmode_test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, 286 advertising)) 287 result |= MDIO_AN_10GBT_CTRL_ADV2_5G; 288 if (linkmode_test_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, 289 advertising)) 290 result |= MDIO_AN_10GBT_CTRL_ADV5G; 291 if (linkmode_test_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, 292 advertising)) 293 result |= MDIO_AN_10GBT_CTRL_ADV10G; 294 295 return result; 296} 297 298/** 299 * mii_10gbt_stat_mod_linkmode_lpa_t 300 * @advertising: target the linkmode advertisement settings 301 * @adv: value of the C45 10GBASE-T AN STATUS register 302 * 303 * A small helper function that translates C45 10GBASE-T AN STATUS register bits 304 * to linkmode advertisement settings. Other bits in advertising aren't changed. 305 */ 306static inline void mii_10gbt_stat_mod_linkmode_lpa_t(unsigned long *advertising, 307 u32 lpa) 308{ 309 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, 310 advertising, lpa & MDIO_AN_10GBT_STAT_LP2_5G); 311 linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, 312 advertising, lpa & MDIO_AN_10GBT_STAT_LP5G); 313 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, 314 advertising, lpa & MDIO_AN_10GBT_STAT_LP10G); 315} 316 317int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum); 318int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val); 319 320int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum); 321int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum); 322int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val); 323int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val); 324 325int mdiobus_register_device(struct mdio_device *mdiodev); 326int mdiobus_unregister_device(struct mdio_device *mdiodev); 327bool mdiobus_is_registered_device(struct mii_bus *bus, int addr); 328struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr); 329 330/** 331 * mdio_module_driver() - Helper macro for registering mdio drivers 332 * 333 * Helper macro for MDIO drivers which do not do anything special in module 334 * init/exit. Each module may only use this macro once, and calling it 335 * replaces module_init() and module_exit(). 336 */ 337#define mdio_module_driver(_mdio_driver) \ 338static int __init mdio_module_init(void) \ 339{ \ 340 return mdio_driver_register(&_mdio_driver); \ 341} \ 342module_init(mdio_module_init); \ 343static void __exit mdio_module_exit(void) \ 344{ \ 345 mdio_driver_unregister(&_mdio_driver); \ 346} \ 347module_exit(mdio_module_exit) 348 349#endif /* __LINUX_MDIO_H__ */