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