blob: 92d4e55ffe6756378eaa8cf48eb2f6dbe08f65e2 [file] [log] [blame]
Ben Hutchings52c94df2009-04-29 08:04:14 +00001/*
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 */
Ben Hutchings52c94df2009-04-29 08:04:14 +00009#ifndef __LINUX_MDIO_H__
10#define __LINUX_MDIO_H__
11
David Howells607ca462012-10-13 10:46:48 +010012#include <uapi/linux/mdio.h>
Florian Fainelli648ea012017-02-04 13:02:44 -080013#include <linux/mod_devicetable.h>
Ben Hutchings52c94df2009-04-29 08:04:14 +000014
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +010015struct gpio_desc;
Andrew Lunnbac83c62016-01-06 20:11:07 +010016struct mii_bus;
Ben Hutchings9c717752012-02-29 14:23:27 +000017
Andrew Lunn9a6f2b02016-04-11 21:40:05 +020018/* 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 */
23enum mdio_mutex_lock_class {
24 MDIO_MUTEX_NORMAL,
25 MDIO_MUTEX_MUX,
26 MDIO_MUTEX_NESTED,
27};
28
Andrew Lunne5a03bf2016-01-06 20:11:16 +010029struct mdio_device {
30 struct device dev;
Andrew Lunna9049e02016-01-06 20:11:26 +010031
Andrew Lunnbc879222016-01-06 20:11:21 +010032 const struct dev_pm_ops *pm_ops;
Andrew Lunne5a03bf2016-01-06 20:11:16 +010033 struct mii_bus *bus;
Florian Fainelli648ea012017-02-04 13:02:44 -080034 char modalias[MDIO_NAME_SIZE];
Andrew Lunn711fdba2016-01-06 20:11:27 +010035
Andrew Lunne76a4952016-01-06 20:11:23 +010036 int (*bus_match)(struct device *dev, struct device_driver *drv);
Andrew Lunn711fdba2016-01-06 20:11:27 +010037 void (*device_free)(struct mdio_device *mdiodev);
38 void (*device_remove)(struct mdio_device *mdiodev);
39
Andrew Lunne5a03bf2016-01-06 20:11:16 +010040 /* Bus address of the MDIO device (0-31) */
41 int addr;
Andrew Lunn7f854422016-01-06 20:11:18 +010042 int flags;
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +010043 struct gpio_desc *reset;
Andrew Lunne5a03bf2016-01-06 20:11:16 +010044};
45#define to_mdio_device(d) container_of(d, struct mdio_device, dev)
46
Andrew Lunna9049e02016-01-06 20:11:26 +010047/* struct mdio_driver_common: Common to all MDIO drivers */
48struct mdio_driver_common {
49 struct device_driver driver;
50 int flags;
51};
Andrew Lunn7f854422016-01-06 20:11:18 +010052#define MDIO_DEVICE_FLAG_PHY 1
Andrew Lunna9049e02016-01-06 20:11:26 +010053#define to_mdio_common_driver(d) \
54 container_of(d, struct mdio_driver_common, driver)
55
56/* struct mdio_driver: Generic MDIO driver */
57struct mdio_driver {
58 struct mdio_driver_common mdiodrv;
59
60 /*
61 * Called during discovery. Used to set
62 * up device-specific structures, if any
63 */
64 int (*probe)(struct mdio_device *mdiodev);
65
66 /* Clears up any memory if needed */
67 void (*remove)(struct mdio_device *mdiodev);
68};
69#define to_mdio_driver(d) \
70 container_of(to_mdio_common_driver(d), struct mdio_driver, mdiodrv)
71
72void mdio_device_free(struct mdio_device *mdiodev);
73struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr);
74int mdio_device_register(struct mdio_device *mdiodev);
75void mdio_device_remove(struct mdio_device *mdiodev);
Sergei Shtylyovbafbdd52017-12-04 13:35:05 +010076void mdio_device_reset(struct mdio_device *mdiodev, int value);
Andrew Lunna9049e02016-01-06 20:11:26 +010077int mdio_driver_register(struct mdio_driver *drv);
78void mdio_driver_unregister(struct mdio_driver *drv);
Florian Fainelli648ea012017-02-04 13:02:44 -080079int mdio_device_bus_match(struct device *dev, struct device_driver *drv);
Andrew Lunn7f854422016-01-06 20:11:18 +010080
Ben Hutchings52c94df2009-04-29 08:04:14 +000081static inline bool mdio_phy_id_is_c45(int phy_id)
82{
83 return (phy_id & MDIO_PHY_ID_C45) && !(phy_id & ~MDIO_PHY_ID_C45_MASK);
84}
85
86static inline __u16 mdio_phy_id_prtad(int phy_id)
87{
88 return (phy_id & MDIO_PHY_ID_PRTAD) >> 5;
89}
90
91static inline __u16 mdio_phy_id_devad(int phy_id)
92{
93 return phy_id & MDIO_PHY_ID_DEVAD;
94}
95
Ben Hutchings1b1c2e92009-04-29 08:04:46 +000096/**
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
109 */
110struct mdio_if_info {
111 int prtad;
Ben Hutchings23428e62009-08-18 20:13:03 -0700112 u32 mmds;
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000113 unsigned mode_support;
114
115 struct net_device *dev;
116 int (*mdio_read)(struct net_device *dev, int prtad, int devad,
117 u16 addr);
118 int (*mdio_write)(struct net_device *dev, int prtad, int devad,
119 u16 addr, u16 val);
120};
121
122#define MDIO_PRTAD_NONE (-1)
123#define MDIO_DEVAD_NONE (-1)
Ben Hutchings9c717752012-02-29 14:23:27 +0000124#define MDIO_SUPPORTS_C22 1
125#define MDIO_SUPPORTS_C45 2
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000126#define MDIO_EMULATE_C22 4
127
128struct ethtool_cmd;
129struct ethtool_pauseparam;
130extern int mdio45_probe(struct mdio_if_info *mdio, int prtad);
131extern int mdio_set_flag(const struct mdio_if_info *mdio,
132 int prtad, int devad, u16 addr, int mask,
133 bool sense);
134extern int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmds);
135extern int mdio45_nway_restart(const struct mdio_if_info *mdio);
136extern void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio,
137 struct ethtool_cmd *ecmd,
138 u32 npage_adv, u32 npage_lpa);
Philippe Reynes8e4881a2017-01-01 19:02:45 +0100139extern void
140mdio45_ethtool_ksettings_get_npage(const struct mdio_if_info *mdio,
141 struct ethtool_link_ksettings *cmd,
142 u32 npage_adv, u32 npage_lpa);
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000143
144/**
145 * mdio45_ethtool_gset - get settings for ETHTOOL_GSET
146 * @mdio: MDIO interface
147 * @ecmd: Ethtool request structure
148 *
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
152 * pages.
153 */
154static inline void mdio45_ethtool_gset(const struct mdio_if_info *mdio,
155 struct ethtool_cmd *ecmd)
156{
157 mdio45_ethtool_gset_npage(mdio, ecmd, 0, 0);
158}
159
Philippe Reynes8e4881a2017-01-01 19:02:45 +0100160/**
161 * mdio45_ethtool_ksettings_get - get settings for ETHTOOL_GLINKSETTINGS
162 * @mdio: MDIO interface
163 * @cmd: Ethtool request structure
164 *
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
168 * from next pages.
169 */
170static inline void
171mdio45_ethtool_ksettings_get(const struct mdio_if_info *mdio,
172 struct ethtool_link_ksettings *cmd)
173{
174 mdio45_ethtool_ksettings_get_npage(mdio, cmd, 0, 0);
175}
176
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000177extern int mdio_mii_ioctl(const struct mdio_if_info *mdio,
178 struct mii_ioctl_data *mii_data, int cmd);
179
Allan, Bruce Wb32607d2012-08-20 04:55:29 +0000180/**
181 * mmd_eee_cap_to_ethtool_sup_t
182 * @eee_cap: value of the MMD EEE Capability register
183 *
184 * A small helper function that translates MMD EEE Capability (3.20) bits
185 * to ethtool supported settings.
186 */
187static inline u32 mmd_eee_cap_to_ethtool_sup_t(u16 eee_cap)
188{
189 u32 supported = 0;
190
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;
203
204 return supported;
205}
206
207/**
208 * mmd_eee_adv_to_ethtool_adv_t
209 * @eee_adv: value of the MMD EEE Advertisement/Link Partner Ability registers
210 *
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
213 * settings.
214 */
215static inline u32 mmd_eee_adv_to_ethtool_adv_t(u16 eee_adv)
216{
217 u32 adv = 0;
218
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;
231
232 return adv;
233}
234
235/**
236 * ethtool_adv_to_mmd_eee_adv_t
237 * @adv: the ethtool advertisement settings
238 *
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.
242 */
243static inline u16 ethtool_adv_to_mmd_eee_adv_t(u32 adv)
244{
245 u16 reg = 0;
246
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;
259
260 return reg;
261}
262
Andrew Lunnbac83c62016-01-06 20:11:07 +0100263int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
264int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum);
265int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
266int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val);
267
Andrew Lunn7f854422016-01-06 20:11:18 +0100268int mdiobus_register_device(struct mdio_device *mdiodev);
269int mdiobus_unregister_device(struct mdio_device *mdiodev);
270bool mdiobus_is_registered_device(struct mii_bus *bus, int addr);
271struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr);
272
Andrew Lunna9049e02016-01-06 20:11:26 +0100273/**
Florian Fainellib70f43a2017-01-22 21:17:32 -0800274 * mdio_module_driver() - Helper macro for registering mdio drivers
Andrew Lunna9049e02016-01-06 20:11:26 +0100275 *
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().
279 */
280#define mdio_module_driver(_mdio_driver) \
281static int __init mdio_module_init(void) \
282{ \
283 return mdio_driver_register(&_mdio_driver); \
284} \
285module_init(mdio_module_init); \
286static void __exit mdio_module_exit(void) \
287{ \
288 mdio_driver_unregister(&_mdio_driver); \
289} \
290module_exit(mdio_module_exit)
291
Ben Hutchings52c94df2009-04-29 08:04:14 +0000292#endif /* __LINUX_MDIO_H__ */