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 12 struct gpio_desc; 13 struct 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 */ 20 enum mdio_mutex_lock_class { 21 MDIO_MUTEX_NORMAL, 22 MDIO_MUTEX_MUX, 23 MDIO_MUTEX_NESTED, 24 }; 25 26 struct 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 */ 47 struct 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 */ 56 struct 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 void mdio_device_free(struct mdio_device *mdiodev); 72 struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr); 73 int mdio_device_register(struct mdio_device *mdiodev); 74 void mdio_device_remove(struct mdio_device *mdiodev); 75 void mdio_device_reset(struct mdio_device *mdiodev, int value); 76 int mdio_driver_register(struct mdio_driver *drv); 77 void mdio_driver_unregister(struct mdio_driver *drv); 78 int mdio_device_bus_match(struct device *dev, struct device_driver *drv); 79 80 static inline bool mdio_phy_id_is_c45(int phy_id) 81 { 82 return (phy_id & MDIO_PHY_ID_C45) && !(phy_id & ~MDIO_PHY_ID_C45_MASK); 83 } 84 85 static inline __u16 mdio_phy_id_prtad(int phy_id) 86 { 87 return (phy_id & MDIO_PHY_ID_PRTAD) >> 5; 88 } 89 90 static inline __u16 mdio_phy_id_devad(int phy_id) 91 { 92 return phy_id & MDIO_PHY_ID_DEVAD; 93 } 94 95 /** 96 * struct mdio_if_info - Ethernet controller MDIO interface 97 * @prtad: PRTAD of the PHY (%MDIO_PRTAD_NONE if not present/unknown) 98 * @mmds: Mask of MMDs expected to be present in the PHY. This must be 99 * non-zero unless @prtad = %MDIO_PRTAD_NONE. 100 * @mode_support: MDIO modes supported. If %MDIO_SUPPORTS_C22 is set then 101 * MII register access will be passed through with @devad = 102 * %MDIO_DEVAD_NONE. If %MDIO_EMULATE_C22 is set then access to 103 * commonly used clause 22 registers will be translated into 104 * clause 45 registers. 105 * @dev: Net device structure 106 * @mdio_read: Register read function; returns value or negative error code 107 * @mdio_write: Register write function; returns 0 or negative error code 108 */ 109 struct mdio_if_info { 110 int prtad; 111 u32 mmds; 112 unsigned mode_support; 113 114 struct net_device *dev; 115 int (*mdio_read)(struct net_device *dev, int prtad, int devad, 116 u16 addr); 117 int (*mdio_write)(struct net_device *dev, int prtad, int devad, 118 u16 addr, u16 val); 119 }; 120 121 #define MDIO_PRTAD_NONE (-1) 122 #define MDIO_DEVAD_NONE (-1) 123 #define MDIO_SUPPORTS_C22 1 124 #define MDIO_SUPPORTS_C45 2 125 #define MDIO_EMULATE_C22 4 126 127 struct ethtool_cmd; 128 struct ethtool_pauseparam; 129 extern int mdio45_probe(struct mdio_if_info *mdio, int prtad); 130 extern int mdio_set_flag(const struct mdio_if_info *mdio, 131 int prtad, int devad, u16 addr, int mask, 132 bool sense); 133 extern int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmds); 134 extern int mdio45_nway_restart(const struct mdio_if_info *mdio); 135 extern void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio, 136 struct ethtool_cmd *ecmd, 137 u32 npage_adv, u32 npage_lpa); 138 extern void 139 mdio45_ethtool_ksettings_get_npage(const struct mdio_if_info *mdio, 140 struct ethtool_link_ksettings *cmd, 141 u32 npage_adv, u32 npage_lpa); 142 143 /** 144 * mdio45_ethtool_gset - get settings for ETHTOOL_GSET 145 * @mdio: MDIO interface 146 * @ecmd: Ethtool request structure 147 * 148 * Since the CSRs for auto-negotiation using next pages are not fully 149 * standardised, this function does not attempt to decode them. Use 150 * mdio45_ethtool_gset_npage() to specify advertisement bits from next 151 * pages. 152 */ 153 static inline void mdio45_ethtool_gset(const struct mdio_if_info *mdio, 154 struct ethtool_cmd *ecmd) 155 { 156 mdio45_ethtool_gset_npage(mdio, ecmd, 0, 0); 157 } 158 159 /** 160 * mdio45_ethtool_ksettings_get - get settings for ETHTOOL_GLINKSETTINGS 161 * @mdio: MDIO interface 162 * @cmd: Ethtool request structure 163 * 164 * Since the CSRs for auto-negotiation using next pages are not fully 165 * standardised, this function does not attempt to decode them. Use 166 * mdio45_ethtool_ksettings_get_npage() to specify advertisement bits 167 * from next pages. 168 */ 169 static inline void 170 mdio45_ethtool_ksettings_get(const struct mdio_if_info *mdio, 171 struct ethtool_link_ksettings *cmd) 172 { 173 mdio45_ethtool_ksettings_get_npage(mdio, cmd, 0, 0); 174 } 175 176 extern int mdio_mii_ioctl(const struct mdio_if_info *mdio, 177 struct mii_ioctl_data *mii_data, int cmd); 178 179 /** 180 * mmd_eee_cap_to_ethtool_sup_t 181 * @eee_cap: value of the MMD EEE Capability register 182 * 183 * A small helper function that translates MMD EEE Capability (3.20) bits 184 * to ethtool supported settings. 185 */ 186 static inline u32 mmd_eee_cap_to_ethtool_sup_t(u16 eee_cap) 187 { 188 u32 supported = 0; 189 190 if (eee_cap & MDIO_EEE_100TX) 191 supported |= SUPPORTED_100baseT_Full; 192 if (eee_cap & MDIO_EEE_1000T) 193 supported |= SUPPORTED_1000baseT_Full; 194 if (eee_cap & MDIO_EEE_10GT) 195 supported |= SUPPORTED_10000baseT_Full; 196 if (eee_cap & MDIO_EEE_1000KX) 197 supported |= SUPPORTED_1000baseKX_Full; 198 if (eee_cap & MDIO_EEE_10GKX4) 199 supported |= SUPPORTED_10000baseKX4_Full; 200 if (eee_cap & MDIO_EEE_10GKR) 201 supported |= SUPPORTED_10000baseKR_Full; 202 203 return supported; 204 } 205 206 /** 207 * mmd_eee_adv_to_ethtool_adv_t 208 * @eee_adv: value of the MMD EEE Advertisement/Link Partner Ability registers 209 * 210 * A small helper function that translates the MMD EEE Advertisment (7.60) 211 * and MMD EEE Link Partner Ability (7.61) bits to ethtool advertisement 212 * settings. 213 */ 214 static inline u32 mmd_eee_adv_to_ethtool_adv_t(u16 eee_adv) 215 { 216 u32 adv = 0; 217 218 if (eee_adv & MDIO_EEE_100TX) 219 adv |= ADVERTISED_100baseT_Full; 220 if (eee_adv & MDIO_EEE_1000T) 221 adv |= ADVERTISED_1000baseT_Full; 222 if (eee_adv & MDIO_EEE_10GT) 223 adv |= ADVERTISED_10000baseT_Full; 224 if (eee_adv & MDIO_EEE_1000KX) 225 adv |= ADVERTISED_1000baseKX_Full; 226 if (eee_adv & MDIO_EEE_10GKX4) 227 adv |= ADVERTISED_10000baseKX4_Full; 228 if (eee_adv & MDIO_EEE_10GKR) 229 adv |= ADVERTISED_10000baseKR_Full; 230 231 return adv; 232 } 233 234 /** 235 * ethtool_adv_to_mmd_eee_adv_t 236 * @adv: the ethtool advertisement settings 237 * 238 * A small helper function that translates ethtool advertisement settings 239 * to EEE advertisements for the MMD EEE Advertisement (7.60) and 240 * MMD EEE Link Partner Ability (7.61) registers. 241 */ 242 static inline u16 ethtool_adv_to_mmd_eee_adv_t(u32 adv) 243 { 244 u16 reg = 0; 245 246 if (adv & ADVERTISED_100baseT_Full) 247 reg |= MDIO_EEE_100TX; 248 if (adv & ADVERTISED_1000baseT_Full) 249 reg |= MDIO_EEE_1000T; 250 if (adv & ADVERTISED_10000baseT_Full) 251 reg |= MDIO_EEE_10GT; 252 if (adv & ADVERTISED_1000baseKX_Full) 253 reg |= MDIO_EEE_1000KX; 254 if (adv & ADVERTISED_10000baseKX4_Full) 255 reg |= MDIO_EEE_10GKX4; 256 if (adv & ADVERTISED_10000baseKR_Full) 257 reg |= MDIO_EEE_10GKR; 258 259 return reg; 260 } 261 262 /** 263 * linkmode_adv_to_mii_10gbt_adv_t 264 * @advertising: the linkmode advertisement settings 265 * 266 * A small helper function that translates linkmode advertisement 267 * settings to phy autonegotiation advertisements for the C45 268 * 10GBASE-T AN CONTROL (7.32) register. 269 */ 270 static inline u32 linkmode_adv_to_mii_10gbt_adv_t(unsigned long *advertising) 271 { 272 u32 result = 0; 273 274 if (linkmode_test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, 275 advertising)) 276 result |= MDIO_AN_10GBT_CTRL_ADV2_5G; 277 if (linkmode_test_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, 278 advertising)) 279 result |= MDIO_AN_10GBT_CTRL_ADV5G; 280 if (linkmode_test_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, 281 advertising)) 282 result |= MDIO_AN_10GBT_CTRL_ADV10G; 283 284 return result; 285 } 286 287 /** 288 * mii_10gbt_stat_mod_linkmode_lpa_t 289 * @advertising: target the linkmode advertisement settings 290 * @adv: value of the C45 10GBASE-T AN STATUS register 291 * 292 * A small helper function that translates C45 10GBASE-T AN STATUS register bits 293 * to linkmode advertisement settings. Other bits in advertising aren't changed. 294 */ 295 static inline void mii_10gbt_stat_mod_linkmode_lpa_t(unsigned long *advertising, 296 u32 lpa) 297 { 298 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT, 299 advertising, lpa & MDIO_AN_10GBT_STAT_LP2_5G); 300 linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT, 301 advertising, lpa & MDIO_AN_10GBT_STAT_LP5G); 302 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, 303 advertising, lpa & MDIO_AN_10GBT_STAT_LP10G); 304 } 305 306 int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum); 307 int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val); 308 309 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum); 310 int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum); 311 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val); 312 int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val); 313 314 int mdiobus_register_device(struct mdio_device *mdiodev); 315 int mdiobus_unregister_device(struct mdio_device *mdiodev); 316 bool mdiobus_is_registered_device(struct mii_bus *bus, int addr); 317 struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr); 318 319 /** 320 * mdio_module_driver() - Helper macro for registering mdio drivers 321 * 322 * Helper macro for MDIO drivers which do not do anything special in module 323 * init/exit. Each module may only use this macro once, and calling it 324 * replaces module_init() and module_exit(). 325 */ 326 #define mdio_module_driver(_mdio_driver) \ 327 static int __init mdio_module_init(void) \ 328 { \ 329 return mdio_driver_register(&_mdio_driver); \ 330 } \ 331 module_init(mdio_module_init); \ 332 static void __exit mdio_module_exit(void) \ 333 { \ 334 mdio_driver_unregister(&_mdio_driver); \ 335 } \ 336 module_exit(mdio_module_exit) 337 338 #endif /* __LINUX_MDIO_H__ */ 339