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