Ben Hutchings | 52c94df | 2009-04-29 08:04:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * linux/mdio.h: definitions for MDIO (clause 45) transceivers |
| 3 | * Copyright 2006-2009 Solarflare Communications Inc. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published |
| 7 | * by the Free Software Foundation, incorporated herein by reference. |
| 8 | */ |
Ben Hutchings | 52c94df | 2009-04-29 08:04:14 +0000 | [diff] [blame] | 9 | #ifndef __LINUX_MDIO_H__ |
| 10 | #define __LINUX_MDIO_H__ |
| 11 | |
David Howells | 607ca46 | 2012-10-13 10:46:48 +0100 | [diff] [blame] | 12 | #include <uapi/linux/mdio.h> |
Ben Hutchings | 52c94df | 2009-04-29 08:04:14 +0000 | [diff] [blame] | 13 | |
Andrew Lunn | bac83c6 | 2016-01-06 20:11:07 +0100 | [diff] [blame] | 14 | struct mii_bus; |
Ben Hutchings | 9c71775 | 2012-02-29 14:23:27 +0000 | [diff] [blame] | 15 | |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 16 | struct mdio_device { |
| 17 | struct device dev; |
Andrew Lunn | a9049e0 | 2016-01-06 20:11:26 +0100 | [diff] [blame^] | 18 | |
Andrew Lunn | bc87922 | 2016-01-06 20:11:21 +0100 | [diff] [blame] | 19 | const struct dev_pm_ops *pm_ops; |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 20 | struct mii_bus *bus; |
Andrew Lunn | e76a495 | 2016-01-06 20:11:23 +0100 | [diff] [blame] | 21 | int (*bus_match)(struct device *dev, struct device_driver *drv); |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 22 | /* Bus address of the MDIO device (0-31) */ |
| 23 | int addr; |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 24 | int flags; |
Andrew Lunn | e5a03bf | 2016-01-06 20:11:16 +0100 | [diff] [blame] | 25 | }; |
| 26 | #define to_mdio_device(d) container_of(d, struct mdio_device, dev) |
| 27 | |
Andrew Lunn | a9049e0 | 2016-01-06 20:11:26 +0100 | [diff] [blame^] | 28 | /* struct mdio_driver_common: Common to all MDIO drivers */ |
| 29 | struct mdio_driver_common { |
| 30 | struct device_driver driver; |
| 31 | int flags; |
| 32 | }; |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 33 | #define MDIO_DEVICE_FLAG_PHY 1 |
Andrew Lunn | a9049e0 | 2016-01-06 20:11:26 +0100 | [diff] [blame^] | 34 | #define to_mdio_common_driver(d) \ |
| 35 | container_of(d, struct mdio_driver_common, driver) |
| 36 | |
| 37 | /* struct mdio_driver: Generic MDIO driver */ |
| 38 | struct mdio_driver { |
| 39 | struct mdio_driver_common mdiodrv; |
| 40 | |
| 41 | /* |
| 42 | * Called during discovery. Used to set |
| 43 | * up device-specific structures, if any |
| 44 | */ |
| 45 | int (*probe)(struct mdio_device *mdiodev); |
| 46 | |
| 47 | /* Clears up any memory if needed */ |
| 48 | void (*remove)(struct mdio_device *mdiodev); |
| 49 | }; |
| 50 | #define to_mdio_driver(d) \ |
| 51 | container_of(to_mdio_common_driver(d), struct mdio_driver, mdiodrv) |
| 52 | |
| 53 | void mdio_device_free(struct mdio_device *mdiodev); |
| 54 | struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr); |
| 55 | int mdio_device_register(struct mdio_device *mdiodev); |
| 56 | void mdio_device_remove(struct mdio_device *mdiodev); |
| 57 | int mdio_driver_register(struct mdio_driver *drv); |
| 58 | void mdio_driver_unregister(struct mdio_driver *drv); |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 59 | |
Ben Hutchings | 52c94df | 2009-04-29 08:04:14 +0000 | [diff] [blame] | 60 | static inline bool mdio_phy_id_is_c45(int phy_id) |
| 61 | { |
| 62 | return (phy_id & MDIO_PHY_ID_C45) && !(phy_id & ~MDIO_PHY_ID_C45_MASK); |
| 63 | } |
| 64 | |
| 65 | static inline __u16 mdio_phy_id_prtad(int phy_id) |
| 66 | { |
| 67 | return (phy_id & MDIO_PHY_ID_PRTAD) >> 5; |
| 68 | } |
| 69 | |
| 70 | static inline __u16 mdio_phy_id_devad(int phy_id) |
| 71 | { |
| 72 | return phy_id & MDIO_PHY_ID_DEVAD; |
| 73 | } |
| 74 | |
Ben Hutchings | 1b1c2e9 | 2009-04-29 08:04:46 +0000 | [diff] [blame] | 75 | /** |
| 76 | * struct mdio_if_info - Ethernet controller MDIO interface |
| 77 | * @prtad: PRTAD of the PHY (%MDIO_PRTAD_NONE if not present/unknown) |
| 78 | * @mmds: Mask of MMDs expected to be present in the PHY. This must be |
| 79 | * non-zero unless @prtad = %MDIO_PRTAD_NONE. |
| 80 | * @mode_support: MDIO modes supported. If %MDIO_SUPPORTS_C22 is set then |
| 81 | * MII register access will be passed through with @devad = |
| 82 | * %MDIO_DEVAD_NONE. If %MDIO_EMULATE_C22 is set then access to |
| 83 | * commonly used clause 22 registers will be translated into |
| 84 | * clause 45 registers. |
| 85 | * @dev: Net device structure |
| 86 | * @mdio_read: Register read function; returns value or negative error code |
| 87 | * @mdio_write: Register write function; returns 0 or negative error code |
| 88 | */ |
| 89 | struct mdio_if_info { |
| 90 | int prtad; |
Ben Hutchings | 23428e6 | 2009-08-18 20:13:03 -0700 | [diff] [blame] | 91 | u32 mmds; |
Ben Hutchings | 1b1c2e9 | 2009-04-29 08:04:46 +0000 | [diff] [blame] | 92 | unsigned mode_support; |
| 93 | |
| 94 | struct net_device *dev; |
| 95 | int (*mdio_read)(struct net_device *dev, int prtad, int devad, |
| 96 | u16 addr); |
| 97 | int (*mdio_write)(struct net_device *dev, int prtad, int devad, |
| 98 | u16 addr, u16 val); |
| 99 | }; |
| 100 | |
| 101 | #define MDIO_PRTAD_NONE (-1) |
| 102 | #define MDIO_DEVAD_NONE (-1) |
Ben Hutchings | 9c71775 | 2012-02-29 14:23:27 +0000 | [diff] [blame] | 103 | #define MDIO_SUPPORTS_C22 1 |
| 104 | #define MDIO_SUPPORTS_C45 2 |
Ben Hutchings | 1b1c2e9 | 2009-04-29 08:04:46 +0000 | [diff] [blame] | 105 | #define MDIO_EMULATE_C22 4 |
| 106 | |
| 107 | struct ethtool_cmd; |
| 108 | struct ethtool_pauseparam; |
| 109 | extern int mdio45_probe(struct mdio_if_info *mdio, int prtad); |
| 110 | extern int mdio_set_flag(const struct mdio_if_info *mdio, |
| 111 | int prtad, int devad, u16 addr, int mask, |
| 112 | bool sense); |
| 113 | extern int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmds); |
| 114 | extern int mdio45_nway_restart(const struct mdio_if_info *mdio); |
| 115 | extern void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio, |
| 116 | struct ethtool_cmd *ecmd, |
| 117 | u32 npage_adv, u32 npage_lpa); |
| 118 | |
| 119 | /** |
| 120 | * mdio45_ethtool_gset - get settings for ETHTOOL_GSET |
| 121 | * @mdio: MDIO interface |
| 122 | * @ecmd: Ethtool request structure |
| 123 | * |
| 124 | * Since the CSRs for auto-negotiation using next pages are not fully |
| 125 | * standardised, this function does not attempt to decode them. Use |
| 126 | * mdio45_ethtool_gset_npage() to specify advertisement bits from next |
| 127 | * pages. |
| 128 | */ |
| 129 | static inline void mdio45_ethtool_gset(const struct mdio_if_info *mdio, |
| 130 | struct ethtool_cmd *ecmd) |
| 131 | { |
| 132 | mdio45_ethtool_gset_npage(mdio, ecmd, 0, 0); |
| 133 | } |
| 134 | |
| 135 | extern int mdio_mii_ioctl(const struct mdio_if_info *mdio, |
| 136 | struct mii_ioctl_data *mii_data, int cmd); |
| 137 | |
Allan, Bruce W | b32607d | 2012-08-20 04:55:29 +0000 | [diff] [blame] | 138 | /** |
| 139 | * mmd_eee_cap_to_ethtool_sup_t |
| 140 | * @eee_cap: value of the MMD EEE Capability register |
| 141 | * |
| 142 | * A small helper function that translates MMD EEE Capability (3.20) bits |
| 143 | * to ethtool supported settings. |
| 144 | */ |
| 145 | static inline u32 mmd_eee_cap_to_ethtool_sup_t(u16 eee_cap) |
| 146 | { |
| 147 | u32 supported = 0; |
| 148 | |
| 149 | if (eee_cap & MDIO_EEE_100TX) |
| 150 | supported |= SUPPORTED_100baseT_Full; |
| 151 | if (eee_cap & MDIO_EEE_1000T) |
| 152 | supported |= SUPPORTED_1000baseT_Full; |
| 153 | if (eee_cap & MDIO_EEE_10GT) |
| 154 | supported |= SUPPORTED_10000baseT_Full; |
| 155 | if (eee_cap & MDIO_EEE_1000KX) |
| 156 | supported |= SUPPORTED_1000baseKX_Full; |
| 157 | if (eee_cap & MDIO_EEE_10GKX4) |
| 158 | supported |= SUPPORTED_10000baseKX4_Full; |
| 159 | if (eee_cap & MDIO_EEE_10GKR) |
| 160 | supported |= SUPPORTED_10000baseKR_Full; |
| 161 | |
| 162 | return supported; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * mmd_eee_adv_to_ethtool_adv_t |
| 167 | * @eee_adv: value of the MMD EEE Advertisement/Link Partner Ability registers |
| 168 | * |
| 169 | * A small helper function that translates the MMD EEE Advertisment (7.60) |
| 170 | * and MMD EEE Link Partner Ability (7.61) bits to ethtool advertisement |
| 171 | * settings. |
| 172 | */ |
| 173 | static inline u32 mmd_eee_adv_to_ethtool_adv_t(u16 eee_adv) |
| 174 | { |
| 175 | u32 adv = 0; |
| 176 | |
| 177 | if (eee_adv & MDIO_EEE_100TX) |
| 178 | adv |= ADVERTISED_100baseT_Full; |
| 179 | if (eee_adv & MDIO_EEE_1000T) |
| 180 | adv |= ADVERTISED_1000baseT_Full; |
| 181 | if (eee_adv & MDIO_EEE_10GT) |
| 182 | adv |= ADVERTISED_10000baseT_Full; |
| 183 | if (eee_adv & MDIO_EEE_1000KX) |
| 184 | adv |= ADVERTISED_1000baseKX_Full; |
| 185 | if (eee_adv & MDIO_EEE_10GKX4) |
| 186 | adv |= ADVERTISED_10000baseKX4_Full; |
| 187 | if (eee_adv & MDIO_EEE_10GKR) |
| 188 | adv |= ADVERTISED_10000baseKR_Full; |
| 189 | |
| 190 | return adv; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * ethtool_adv_to_mmd_eee_adv_t |
| 195 | * @adv: the ethtool advertisement settings |
| 196 | * |
| 197 | * A small helper function that translates ethtool advertisement settings |
| 198 | * to EEE advertisements for the MMD EEE Advertisement (7.60) and |
| 199 | * MMD EEE Link Partner Ability (7.61) registers. |
| 200 | */ |
| 201 | static inline u16 ethtool_adv_to_mmd_eee_adv_t(u32 adv) |
| 202 | { |
| 203 | u16 reg = 0; |
| 204 | |
| 205 | if (adv & ADVERTISED_100baseT_Full) |
| 206 | reg |= MDIO_EEE_100TX; |
| 207 | if (adv & ADVERTISED_1000baseT_Full) |
| 208 | reg |= MDIO_EEE_1000T; |
| 209 | if (adv & ADVERTISED_10000baseT_Full) |
| 210 | reg |= MDIO_EEE_10GT; |
| 211 | if (adv & ADVERTISED_1000baseKX_Full) |
| 212 | reg |= MDIO_EEE_1000KX; |
| 213 | if (adv & ADVERTISED_10000baseKX4_Full) |
| 214 | reg |= MDIO_EEE_10GKX4; |
| 215 | if (adv & ADVERTISED_10000baseKR_Full) |
| 216 | reg |= MDIO_EEE_10GKR; |
| 217 | |
| 218 | return reg; |
| 219 | } |
| 220 | |
Andrew Lunn | bac83c6 | 2016-01-06 20:11:07 +0100 | [diff] [blame] | 221 | int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum); |
| 222 | int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum); |
| 223 | int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val); |
| 224 | int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val); |
| 225 | |
Andrew Lunn | 7f85442 | 2016-01-06 20:11:18 +0100 | [diff] [blame] | 226 | int mdiobus_register_device(struct mdio_device *mdiodev); |
| 227 | int mdiobus_unregister_device(struct mdio_device *mdiodev); |
| 228 | bool mdiobus_is_registered_device(struct mii_bus *bus, int addr); |
| 229 | struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr); |
| 230 | |
Andrew Lunn | a9049e0 | 2016-01-06 20:11:26 +0100 | [diff] [blame^] | 231 | /** |
| 232 | * module_mdio_driver() - Helper macro for registering mdio drivers |
| 233 | * |
| 234 | * Helper macro for MDIO drivers which do not do anything special in module |
| 235 | * init/exit. Each module may only use this macro once, and calling it |
| 236 | * replaces module_init() and module_exit(). |
| 237 | */ |
| 238 | #define mdio_module_driver(_mdio_driver) \ |
| 239 | static int __init mdio_module_init(void) \ |
| 240 | { \ |
| 241 | return mdio_driver_register(&_mdio_driver); \ |
| 242 | } \ |
| 243 | module_init(mdio_module_init); \ |
| 244 | static void __exit mdio_module_exit(void) \ |
| 245 | { \ |
| 246 | mdio_driver_unregister(&_mdio_driver); \ |
| 247 | } \ |
| 248 | module_exit(mdio_module_exit) |
| 249 | |
Ben Hutchings | 52c94df | 2009-04-29 08:04:14 +0000 | [diff] [blame] | 250 | #endif /* __LINUX_MDIO_H__ */ |