2 * linux/mdio.h: definitions for MDIO (clause 45) transceivers
3 * Copyright 2006-2009 Solarflare Communications Inc.
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.
9 #ifndef __LINUX_MDIO_H__
10 #define __LINUX_MDIO_H__
12 #include <uapi/linux/mdio.h>
16 /* Multiple levels of nesting are possible. However typically this is
17 * limited to nested DSA like layer, a MUX layer, and the normal
18 * user. Instead of trying to handle the general case, just define
21 enum mdio_mutex_lock_class {
30 const struct dev_pm_ops *pm_ops;
33 int (*bus_match)(struct device *dev, struct device_driver *drv);
34 void (*device_free)(struct mdio_device *mdiodev);
35 void (*device_remove)(struct mdio_device *mdiodev);
37 /* Bus address of the MDIO device (0-31) */
41 #define to_mdio_device(d) container_of(d, struct mdio_device, dev)
43 /* struct mdio_driver_common: Common to all MDIO drivers */
44 struct mdio_driver_common {
45 struct device_driver driver;
48 #define MDIO_DEVICE_FLAG_PHY 1
49 #define to_mdio_common_driver(d) \
50 container_of(d, struct mdio_driver_common, driver)
52 /* struct mdio_driver: Generic MDIO driver */
54 struct mdio_driver_common mdiodrv;
57 * Called during discovery. Used to set
58 * up device-specific structures, if any
60 int (*probe)(struct mdio_device *mdiodev);
62 /* Clears up any memory if needed */
63 void (*remove)(struct mdio_device *mdiodev);
65 #define to_mdio_driver(d) \
66 container_of(to_mdio_common_driver(d), struct mdio_driver, mdiodrv)
68 void mdio_device_free(struct mdio_device *mdiodev);
69 struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr);
70 int mdio_device_register(struct mdio_device *mdiodev);
71 void mdio_device_remove(struct mdio_device *mdiodev);
72 int mdio_driver_register(struct mdio_driver *drv);
73 void mdio_driver_unregister(struct mdio_driver *drv);
75 static inline bool mdio_phy_id_is_c45(int phy_id)
77 return (phy_id & MDIO_PHY_ID_C45) && !(phy_id & ~MDIO_PHY_ID_C45_MASK);
80 static inline __u16 mdio_phy_id_prtad(int phy_id)
82 return (phy_id & MDIO_PHY_ID_PRTAD) >> 5;
85 static inline __u16 mdio_phy_id_devad(int phy_id)
87 return phy_id & MDIO_PHY_ID_DEVAD;
91 * struct mdio_if_info - Ethernet controller MDIO interface
92 * @prtad: PRTAD of the PHY (%MDIO_PRTAD_NONE if not present/unknown)
93 * @mmds: Mask of MMDs expected to be present in the PHY. This must be
94 * non-zero unless @prtad = %MDIO_PRTAD_NONE.
95 * @mode_support: MDIO modes supported. If %MDIO_SUPPORTS_C22 is set then
96 * MII register access will be passed through with @devad =
97 * %MDIO_DEVAD_NONE. If %MDIO_EMULATE_C22 is set then access to
98 * commonly used clause 22 registers will be translated into
99 * clause 45 registers.
100 * @dev: Net device structure
101 * @mdio_read: Register read function; returns value or negative error code
102 * @mdio_write: Register write function; returns 0 or negative error code
104 struct mdio_if_info {
107 unsigned mode_support;
109 struct net_device *dev;
110 int (*mdio_read)(struct net_device *dev, int prtad, int devad,
112 int (*mdio_write)(struct net_device *dev, int prtad, int devad,
116 #define MDIO_PRTAD_NONE (-1)
117 #define MDIO_DEVAD_NONE (-1)
118 #define MDIO_SUPPORTS_C22 1
119 #define MDIO_SUPPORTS_C45 2
120 #define MDIO_EMULATE_C22 4
123 struct ethtool_pauseparam;
124 extern int mdio45_probe(struct mdio_if_info *mdio, int prtad);
125 extern int mdio_set_flag(const struct mdio_if_info *mdio,
126 int prtad, int devad, u16 addr, int mask,
128 extern int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmds);
129 extern int mdio45_nway_restart(const struct mdio_if_info *mdio);
130 extern void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio,
131 struct ethtool_cmd *ecmd,
132 u32 npage_adv, u32 npage_lpa);
134 mdio45_ethtool_ksettings_get_npage(const struct mdio_if_info *mdio,
135 struct ethtool_link_ksettings *cmd,
136 u32 npage_adv, u32 npage_lpa);
139 * mdio45_ethtool_gset - get settings for ETHTOOL_GSET
140 * @mdio: MDIO interface
141 * @ecmd: Ethtool request structure
143 * Since the CSRs for auto-negotiation using next pages are not fully
144 * standardised, this function does not attempt to decode them. Use
145 * mdio45_ethtool_gset_npage() to specify advertisement bits from next
148 static inline void mdio45_ethtool_gset(const struct mdio_if_info *mdio,
149 struct ethtool_cmd *ecmd)
151 mdio45_ethtool_gset_npage(mdio, ecmd, 0, 0);
155 * mdio45_ethtool_ksettings_get - get settings for ETHTOOL_GLINKSETTINGS
156 * @mdio: MDIO interface
157 * @cmd: Ethtool request structure
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_ksettings_get_npage() to specify advertisement bits
165 mdio45_ethtool_ksettings_get(const struct mdio_if_info *mdio,
166 struct ethtool_link_ksettings *cmd)
168 mdio45_ethtool_ksettings_get_npage(mdio, cmd, 0, 0);
171 extern int mdio_mii_ioctl(const struct mdio_if_info *mdio,
172 struct mii_ioctl_data *mii_data, int cmd);
175 * mmd_eee_cap_to_ethtool_sup_t
176 * @eee_cap: value of the MMD EEE Capability register
178 * A small helper function that translates MMD EEE Capability (3.20) bits
179 * to ethtool supported settings.
181 static inline u32 mmd_eee_cap_to_ethtool_sup_t(u16 eee_cap)
185 if (eee_cap & MDIO_EEE_100TX)
186 supported |= SUPPORTED_100baseT_Full;
187 if (eee_cap & MDIO_EEE_1000T)
188 supported |= SUPPORTED_1000baseT_Full;
189 if (eee_cap & MDIO_EEE_10GT)
190 supported |= SUPPORTED_10000baseT_Full;
191 if (eee_cap & MDIO_EEE_1000KX)
192 supported |= SUPPORTED_1000baseKX_Full;
193 if (eee_cap & MDIO_EEE_10GKX4)
194 supported |= SUPPORTED_10000baseKX4_Full;
195 if (eee_cap & MDIO_EEE_10GKR)
196 supported |= SUPPORTED_10000baseKR_Full;
202 * mmd_eee_adv_to_ethtool_adv_t
203 * @eee_adv: value of the MMD EEE Advertisement/Link Partner Ability registers
205 * A small helper function that translates the MMD EEE Advertisment (7.60)
206 * and MMD EEE Link Partner Ability (7.61) bits to ethtool advertisement
209 static inline u32 mmd_eee_adv_to_ethtool_adv_t(u16 eee_adv)
213 if (eee_adv & MDIO_EEE_100TX)
214 adv |= ADVERTISED_100baseT_Full;
215 if (eee_adv & MDIO_EEE_1000T)
216 adv |= ADVERTISED_1000baseT_Full;
217 if (eee_adv & MDIO_EEE_10GT)
218 adv |= ADVERTISED_10000baseT_Full;
219 if (eee_adv & MDIO_EEE_1000KX)
220 adv |= ADVERTISED_1000baseKX_Full;
221 if (eee_adv & MDIO_EEE_10GKX4)
222 adv |= ADVERTISED_10000baseKX4_Full;
223 if (eee_adv & MDIO_EEE_10GKR)
224 adv |= ADVERTISED_10000baseKR_Full;
230 * ethtool_adv_to_mmd_eee_adv_t
231 * @adv: the ethtool advertisement settings
233 * A small helper function that translates ethtool advertisement settings
234 * to EEE advertisements for the MMD EEE Advertisement (7.60) and
235 * MMD EEE Link Partner Ability (7.61) registers.
237 static inline u16 ethtool_adv_to_mmd_eee_adv_t(u32 adv)
241 if (adv & ADVERTISED_100baseT_Full)
242 reg |= MDIO_EEE_100TX;
243 if (adv & ADVERTISED_1000baseT_Full)
244 reg |= MDIO_EEE_1000T;
245 if (adv & ADVERTISED_10000baseT_Full)
246 reg |= MDIO_EEE_10GT;
247 if (adv & ADVERTISED_1000baseKX_Full)
248 reg |= MDIO_EEE_1000KX;
249 if (adv & ADVERTISED_10000baseKX4_Full)
250 reg |= MDIO_EEE_10GKX4;
251 if (adv & ADVERTISED_10000baseKR_Full)
252 reg |= MDIO_EEE_10GKR;
257 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
258 int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum);
259 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
260 int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val);
262 int mdiobus_register_device(struct mdio_device *mdiodev);
263 int mdiobus_unregister_device(struct mdio_device *mdiodev);
264 bool mdiobus_is_registered_device(struct mii_bus *bus, int addr);
265 struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr);
268 * module_mdio_driver() - Helper macro for registering mdio drivers
270 * Helper macro for MDIO drivers which do not do anything special in module
271 * init/exit. Each module may only use this macro once, and calling it
272 * replaces module_init() and module_exit().
274 #define mdio_module_driver(_mdio_driver) \
275 static int __init mdio_module_init(void) \
277 return mdio_driver_register(&_mdio_driver); \
279 module_init(mdio_module_init); \
280 static void __exit mdio_module_exit(void) \
282 mdio_driver_unregister(&_mdio_driver); \
284 module_exit(mdio_module_exit)
286 #endif /* __LINUX_MDIO_H__ */