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>
13 #include <linux/mod_devicetable.h>
17 /* Multiple levels of nesting are possible. However typically this is
18 * limited to nested DSA like layer, a MUX layer, and the normal
19 * user. Instead of trying to handle the general case, just define
22 enum mdio_mutex_lock_class {
31 const struct dev_pm_ops *pm_ops;
33 char modalias[MDIO_NAME_SIZE];
35 int (*bus_match)(struct device *dev, struct device_driver *drv);
36 void (*device_free)(struct mdio_device *mdiodev);
37 void (*device_remove)(struct mdio_device *mdiodev);
39 /* Bus address of the MDIO device (0-31) */
43 #define to_mdio_device(d) container_of(d, struct mdio_device, dev)
45 /* struct mdio_driver_common: Common to all MDIO drivers */
46 struct mdio_driver_common {
47 struct device_driver driver;
50 #define MDIO_DEVICE_FLAG_PHY 1
51 #define to_mdio_common_driver(d) \
52 container_of(d, struct mdio_driver_common, driver)
54 /* struct mdio_driver: Generic MDIO driver */
56 struct mdio_driver_common mdiodrv;
59 * Called during discovery. Used to set
60 * up device-specific structures, if any
62 int (*probe)(struct mdio_device *mdiodev);
64 /* Clears up any memory if needed */
65 void (*remove)(struct mdio_device *mdiodev);
67 /* Quiesces the device on system shutdown, turns off interrupts etc */
68 void (*shutdown)(struct mdio_device *mdiodev);
70 #define to_mdio_driver(d) \
71 container_of(to_mdio_common_driver(d), struct mdio_driver, mdiodrv)
73 void mdio_device_free(struct mdio_device *mdiodev);
74 struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr);
75 int mdio_device_register(struct mdio_device *mdiodev);
76 void mdio_device_remove(struct mdio_device *mdiodev);
77 int mdio_driver_register(struct mdio_driver *drv);
78 void mdio_driver_unregister(struct mdio_driver *drv);
79 int mdio_device_bus_match(struct device *dev, struct device_driver *drv);
81 static inline bool mdio_phy_id_is_c45(int phy_id)
83 return (phy_id & MDIO_PHY_ID_C45) && !(phy_id & ~MDIO_PHY_ID_C45_MASK);
86 static inline __u16 mdio_phy_id_prtad(int phy_id)
88 return (phy_id & MDIO_PHY_ID_PRTAD) >> 5;
91 static inline __u16 mdio_phy_id_devad(int phy_id)
93 return phy_id & MDIO_PHY_ID_DEVAD;
97 * struct mdio_if_info - Ethernet controller MDIO interface
98 * @prtad: PRTAD of the PHY (%MDIO_PRTAD_NONE if not present/unknown)
99 * @mmds: Mask of MMDs expected to be present in the PHY. This must be
100 * non-zero unless @prtad = %MDIO_PRTAD_NONE.
101 * @mode_support: MDIO modes supported. If %MDIO_SUPPORTS_C22 is set then
102 * MII register access will be passed through with @devad =
103 * %MDIO_DEVAD_NONE. If %MDIO_EMULATE_C22 is set then access to
104 * commonly used clause 22 registers will be translated into
105 * clause 45 registers.
106 * @dev: Net device structure
107 * @mdio_read: Register read function; returns value or negative error code
108 * @mdio_write: Register write function; returns 0 or negative error code
110 struct mdio_if_info {
113 unsigned mode_support;
115 struct net_device *dev;
116 int (*mdio_read)(struct net_device *dev, int prtad, int devad,
118 int (*mdio_write)(struct net_device *dev, int prtad, int devad,
122 #define MDIO_PRTAD_NONE (-1)
123 #define MDIO_DEVAD_NONE (-1)
124 #define MDIO_SUPPORTS_C22 1
125 #define MDIO_SUPPORTS_C45 2
126 #define MDIO_EMULATE_C22 4
129 struct ethtool_pauseparam;
130 extern int mdio45_probe(struct mdio_if_info *mdio, int prtad);
131 extern int mdio_set_flag(const struct mdio_if_info *mdio,
132 int prtad, int devad, u16 addr, int mask,
134 extern int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmds);
135 extern int mdio45_nway_restart(const struct mdio_if_info *mdio);
136 extern void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio,
137 struct ethtool_cmd *ecmd,
138 u32 npage_adv, u32 npage_lpa);
140 mdio45_ethtool_ksettings_get_npage(const struct mdio_if_info *mdio,
141 struct ethtool_link_ksettings *cmd,
142 u32 npage_adv, u32 npage_lpa);
145 * mdio45_ethtool_gset - get settings for ETHTOOL_GSET
146 * @mdio: MDIO interface
147 * @ecmd: Ethtool request structure
149 * Since the CSRs for auto-negotiation using next pages are not fully
150 * standardised, this function does not attempt to decode them. Use
151 * mdio45_ethtool_gset_npage() to specify advertisement bits from next
154 static inline void mdio45_ethtool_gset(const struct mdio_if_info *mdio,
155 struct ethtool_cmd *ecmd)
157 mdio45_ethtool_gset_npage(mdio, ecmd, 0, 0);
161 * mdio45_ethtool_ksettings_get - get settings for ETHTOOL_GLINKSETTINGS
162 * @mdio: MDIO interface
163 * @cmd: Ethtool request structure
165 * Since the CSRs for auto-negotiation using next pages are not fully
166 * standardised, this function does not attempt to decode them. Use
167 * mdio45_ethtool_ksettings_get_npage() to specify advertisement bits
171 mdio45_ethtool_ksettings_get(const struct mdio_if_info *mdio,
172 struct ethtool_link_ksettings *cmd)
174 mdio45_ethtool_ksettings_get_npage(mdio, cmd, 0, 0);
177 extern int mdio_mii_ioctl(const struct mdio_if_info *mdio,
178 struct mii_ioctl_data *mii_data, int cmd);
181 * mmd_eee_cap_to_ethtool_sup_t
182 * @eee_cap: value of the MMD EEE Capability register
184 * A small helper function that translates MMD EEE Capability (3.20) bits
185 * to ethtool supported settings.
187 static inline u32 mmd_eee_cap_to_ethtool_sup_t(u16 eee_cap)
191 if (eee_cap & MDIO_EEE_100TX)
192 supported |= SUPPORTED_100baseT_Full;
193 if (eee_cap & MDIO_EEE_1000T)
194 supported |= SUPPORTED_1000baseT_Full;
195 if (eee_cap & MDIO_EEE_10GT)
196 supported |= SUPPORTED_10000baseT_Full;
197 if (eee_cap & MDIO_EEE_1000KX)
198 supported |= SUPPORTED_1000baseKX_Full;
199 if (eee_cap & MDIO_EEE_10GKX4)
200 supported |= SUPPORTED_10000baseKX4_Full;
201 if (eee_cap & MDIO_EEE_10GKR)
202 supported |= SUPPORTED_10000baseKR_Full;
208 * mmd_eee_adv_to_ethtool_adv_t
209 * @eee_adv: value of the MMD EEE Advertisement/Link Partner Ability registers
211 * A small helper function that translates the MMD EEE Advertisment (7.60)
212 * and MMD EEE Link Partner Ability (7.61) bits to ethtool advertisement
215 static inline u32 mmd_eee_adv_to_ethtool_adv_t(u16 eee_adv)
219 if (eee_adv & MDIO_EEE_100TX)
220 adv |= ADVERTISED_100baseT_Full;
221 if (eee_adv & MDIO_EEE_1000T)
222 adv |= ADVERTISED_1000baseT_Full;
223 if (eee_adv & MDIO_EEE_10GT)
224 adv |= ADVERTISED_10000baseT_Full;
225 if (eee_adv & MDIO_EEE_1000KX)
226 adv |= ADVERTISED_1000baseKX_Full;
227 if (eee_adv & MDIO_EEE_10GKX4)
228 adv |= ADVERTISED_10000baseKX4_Full;
229 if (eee_adv & MDIO_EEE_10GKR)
230 adv |= ADVERTISED_10000baseKR_Full;
236 * ethtool_adv_to_mmd_eee_adv_t
237 * @adv: the ethtool advertisement settings
239 * A small helper function that translates ethtool advertisement settings
240 * to EEE advertisements for the MMD EEE Advertisement (7.60) and
241 * MMD EEE Link Partner Ability (7.61) registers.
243 static inline u16 ethtool_adv_to_mmd_eee_adv_t(u32 adv)
247 if (adv & ADVERTISED_100baseT_Full)
248 reg |= MDIO_EEE_100TX;
249 if (adv & ADVERTISED_1000baseT_Full)
250 reg |= MDIO_EEE_1000T;
251 if (adv & ADVERTISED_10000baseT_Full)
252 reg |= MDIO_EEE_10GT;
253 if (adv & ADVERTISED_1000baseKX_Full)
254 reg |= MDIO_EEE_1000KX;
255 if (adv & ADVERTISED_10000baseKX4_Full)
256 reg |= MDIO_EEE_10GKX4;
257 if (adv & ADVERTISED_10000baseKR_Full)
258 reg |= MDIO_EEE_10GKR;
263 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
264 int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum);
265 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
266 int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val);
268 int mdiobus_register_device(struct mdio_device *mdiodev);
269 int mdiobus_unregister_device(struct mdio_device *mdiodev);
270 bool mdiobus_is_registered_device(struct mii_bus *bus, int addr);
271 struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr);
274 * mdio_module_driver() - Helper macro for registering mdio drivers
276 * Helper macro for MDIO drivers which do not do anything special in module
277 * init/exit. Each module may only use this macro once, and calling it
278 * replaces module_init() and module_exit().
280 #define mdio_module_driver(_mdio_driver) \
281 static int __init mdio_module_init(void) \
283 return mdio_driver_register(&_mdio_driver); \
285 module_init(mdio_module_init); \
286 static void __exit mdio_module_exit(void) \
288 mdio_driver_unregister(&_mdio_driver); \
290 module_exit(mdio_module_exit)
292 #endif /* __LINUX_MDIO_H__ */