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