Merge branch 'topic/tasklet-convert' into for-linus
[linux-block.git] / include / linux / mdio.h
CommitLineData
d2912cb1 1/* SPDX-License-Identifier: GPL-2.0-only */
52c94dfa
BH
2/*
3 * linux/mdio.h: definitions for MDIO (clause 45) transceivers
4 * Copyright 2006-2009 Solarflare Communications Inc.
52c94dfa 5 */
52c94dfa
BH
6#ifndef __LINUX_MDIO_H__
7#define __LINUX_MDIO_H__
8
607ca46e 9#include <uapi/linux/mdio.h>
648ea013 10#include <linux/mod_devicetable.h>
52c94dfa 11
90ce665c
RK
12/* Or MII_ADDR_C45 into regnum for read/write on mii_bus to enable the 21 bit
13 * IEEE 802.3ae clause 45 addressing mode used by 10GIGE phy chips.
14 */
15#define MII_ADDR_C45 (1<<30)
16#define MII_DEVADDR_C45_SHIFT 16
17#define MII_REGADDR_C45_MASK GENMASK(15, 0)
18
bafbdd52 19struct gpio_desc;
bac83c65 20struct mii_bus;
33374098 21struct reset_control;
9c717758 22
9a6f2b01
AL
23/* Multiple levels of nesting are possible. However typically this is
24 * limited to nested DSA like layer, a MUX layer, and the normal
25 * user. Instead of trying to handle the general case, just define
26 * these cases.
27 */
28enum mdio_mutex_lock_class {
29 MDIO_MUTEX_NORMAL,
30 MDIO_MUTEX_MUX,
31 MDIO_MUTEX_NESTED,
32};
33
e5a03bfd
AL
34struct mdio_device {
35 struct device dev;
a9049e0c 36
e5a03bfd 37 struct mii_bus *bus;
648ea013 38 char modalias[MDIO_NAME_SIZE];
711fdba3 39
e76a4957 40 int (*bus_match)(struct device *dev, struct device_driver *drv);
711fdba3
AL
41 void (*device_free)(struct mdio_device *mdiodev);
42 void (*device_remove)(struct mdio_device *mdiodev);
43
e5a03bfd
AL
44 /* Bus address of the MDIO device (0-31) */
45 int addr;
7f854420 46 int flags;
6110ed2d 47 struct gpio_desc *reset_gpio;
71dd6c0d 48 struct reset_control *reset_ctrl;
04f629f7
RL
49 unsigned int reset_assert_delay;
50 unsigned int reset_deassert_delay;
e5a03bfd
AL
51};
52#define to_mdio_device(d) container_of(d, struct mdio_device, dev)
53
a9049e0c
AL
54/* struct mdio_driver_common: Common to all MDIO drivers */
55struct mdio_driver_common {
56 struct device_driver driver;
57 int flags;
58};
7f854420 59#define MDIO_DEVICE_FLAG_PHY 1
a9049e0c
AL
60#define to_mdio_common_driver(d) \
61 container_of(d, struct mdio_driver_common, driver)
62
63/* struct mdio_driver: Generic MDIO driver */
64struct mdio_driver {
65 struct mdio_driver_common mdiodrv;
66
67 /*
68 * Called during discovery. Used to set
69 * up device-specific structures, if any
70 */
71 int (*probe)(struct mdio_device *mdiodev);
72
73 /* Clears up any memory if needed */
74 void (*remove)(struct mdio_device *mdiodev);
75};
76#define to_mdio_driver(d) \
77 container_of(to_mdio_common_driver(d), struct mdio_driver, mdiodrv)
78
36b1a2fc
HK
79/* device driver data */
80static inline void mdiodev_set_drvdata(struct mdio_device *mdio, void *data)
81{
82 dev_set_drvdata(&mdio->dev, data);
83}
84
85static inline void *mdiodev_get_drvdata(struct mdio_device *mdio)
86{
87 return dev_get_drvdata(&mdio->dev);
88}
89
a9049e0c
AL
90void mdio_device_free(struct mdio_device *mdiodev);
91struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr);
92int mdio_device_register(struct mdio_device *mdiodev);
93void mdio_device_remove(struct mdio_device *mdiodev);
bafbdd52 94void mdio_device_reset(struct mdio_device *mdiodev, int value);
a9049e0c
AL
95int mdio_driver_register(struct mdio_driver *drv);
96void mdio_driver_unregister(struct mdio_driver *drv);
648ea013 97int mdio_device_bus_match(struct device *dev, struct device_driver *drv);
7f854420 98
52c94dfa
BH
99static inline bool mdio_phy_id_is_c45(int phy_id)
100{
101 return (phy_id & MDIO_PHY_ID_C45) && !(phy_id & ~MDIO_PHY_ID_C45_MASK);
102}
103
104static inline __u16 mdio_phy_id_prtad(int phy_id)
105{
106 return (phy_id & MDIO_PHY_ID_PRTAD) >> 5;
107}
108
109static inline __u16 mdio_phy_id_devad(int phy_id)
110{
111 return phy_id & MDIO_PHY_ID_DEVAD;
112}
113
1b1c2e95
BH
114/**
115 * struct mdio_if_info - Ethernet controller MDIO interface
116 * @prtad: PRTAD of the PHY (%MDIO_PRTAD_NONE if not present/unknown)
117 * @mmds: Mask of MMDs expected to be present in the PHY. This must be
118 * non-zero unless @prtad = %MDIO_PRTAD_NONE.
119 * @mode_support: MDIO modes supported. If %MDIO_SUPPORTS_C22 is set then
120 * MII register access will be passed through with @devad =
121 * %MDIO_DEVAD_NONE. If %MDIO_EMULATE_C22 is set then access to
122 * commonly used clause 22 registers will be translated into
123 * clause 45 registers.
124 * @dev: Net device structure
125 * @mdio_read: Register read function; returns value or negative error code
126 * @mdio_write: Register write function; returns 0 or negative error code
127 */
128struct mdio_if_info {
129 int prtad;
23428e6b 130 u32 mmds;
1b1c2e95
BH
131 unsigned mode_support;
132
133 struct net_device *dev;
134 int (*mdio_read)(struct net_device *dev, int prtad, int devad,
135 u16 addr);
136 int (*mdio_write)(struct net_device *dev, int prtad, int devad,
137 u16 addr, u16 val);
138};
139
140#define MDIO_PRTAD_NONE (-1)
141#define MDIO_DEVAD_NONE (-1)
9c717758
BH
142#define MDIO_SUPPORTS_C22 1
143#define MDIO_SUPPORTS_C45 2
1b1c2e95
BH
144#define MDIO_EMULATE_C22 4
145
146struct ethtool_cmd;
147struct ethtool_pauseparam;
148extern int mdio45_probe(struct mdio_if_info *mdio, int prtad);
149extern int mdio_set_flag(const struct mdio_if_info *mdio,
150 int prtad, int devad, u16 addr, int mask,
151 bool sense);
152extern int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmds);
153extern int mdio45_nway_restart(const struct mdio_if_info *mdio);
154extern void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio,
155 struct ethtool_cmd *ecmd,
156 u32 npage_adv, u32 npage_lpa);
8e4881aa
PR
157extern void
158mdio45_ethtool_ksettings_get_npage(const struct mdio_if_info *mdio,
159 struct ethtool_link_ksettings *cmd,
160 u32 npage_adv, u32 npage_lpa);
1b1c2e95
BH
161
162/**
163 * mdio45_ethtool_gset - get settings for ETHTOOL_GSET
164 * @mdio: MDIO interface
165 * @ecmd: 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_gset_npage() to specify advertisement bits from next
170 * pages.
171 */
172static inline void mdio45_ethtool_gset(const struct mdio_if_info *mdio,
173 struct ethtool_cmd *ecmd)
174{
175 mdio45_ethtool_gset_npage(mdio, ecmd, 0, 0);
176}
177
8e4881aa
PR
178/**
179 * mdio45_ethtool_ksettings_get - get settings for ETHTOOL_GLINKSETTINGS
180 * @mdio: MDIO interface
181 * @cmd: Ethtool request structure
182 *
183 * Since the CSRs for auto-negotiation using next pages are not fully
184 * standardised, this function does not attempt to decode them. Use
185 * mdio45_ethtool_ksettings_get_npage() to specify advertisement bits
186 * from next pages.
187 */
188static inline void
189mdio45_ethtool_ksettings_get(const struct mdio_if_info *mdio,
190 struct ethtool_link_ksettings *cmd)
191{
192 mdio45_ethtool_ksettings_get_npage(mdio, cmd, 0, 0);
193}
194
1b1c2e95
BH
195extern int mdio_mii_ioctl(const struct mdio_if_info *mdio,
196 struct mii_ioctl_data *mii_data, int cmd);
197
b32607dd
AB
198/**
199 * mmd_eee_cap_to_ethtool_sup_t
200 * @eee_cap: value of the MMD EEE Capability register
201 *
202 * A small helper function that translates MMD EEE Capability (3.20) bits
203 * to ethtool supported settings.
204 */
205static inline u32 mmd_eee_cap_to_ethtool_sup_t(u16 eee_cap)
206{
207 u32 supported = 0;
208
209 if (eee_cap & MDIO_EEE_100TX)
210 supported |= SUPPORTED_100baseT_Full;
211 if (eee_cap & MDIO_EEE_1000T)
212 supported |= SUPPORTED_1000baseT_Full;
213 if (eee_cap & MDIO_EEE_10GT)
214 supported |= SUPPORTED_10000baseT_Full;
215 if (eee_cap & MDIO_EEE_1000KX)
216 supported |= SUPPORTED_1000baseKX_Full;
217 if (eee_cap & MDIO_EEE_10GKX4)
218 supported |= SUPPORTED_10000baseKX4_Full;
219 if (eee_cap & MDIO_EEE_10GKR)
220 supported |= SUPPORTED_10000baseKR_Full;
221
222 return supported;
223}
224
225/**
226 * mmd_eee_adv_to_ethtool_adv_t
227 * @eee_adv: value of the MMD EEE Advertisement/Link Partner Ability registers
228 *
229 * A small helper function that translates the MMD EEE Advertisment (7.60)
230 * and MMD EEE Link Partner Ability (7.61) bits to ethtool advertisement
231 * settings.
232 */
233static inline u32 mmd_eee_adv_to_ethtool_adv_t(u16 eee_adv)
234{
235 u32 adv = 0;
236
237 if (eee_adv & MDIO_EEE_100TX)
238 adv |= ADVERTISED_100baseT_Full;
239 if (eee_adv & MDIO_EEE_1000T)
240 adv |= ADVERTISED_1000baseT_Full;
241 if (eee_adv & MDIO_EEE_10GT)
242 adv |= ADVERTISED_10000baseT_Full;
243 if (eee_adv & MDIO_EEE_1000KX)
244 adv |= ADVERTISED_1000baseKX_Full;
245 if (eee_adv & MDIO_EEE_10GKX4)
246 adv |= ADVERTISED_10000baseKX4_Full;
247 if (eee_adv & MDIO_EEE_10GKR)
248 adv |= ADVERTISED_10000baseKR_Full;
249
250 return adv;
251}
252
253/**
254 * ethtool_adv_to_mmd_eee_adv_t
255 * @adv: the ethtool advertisement settings
256 *
257 * A small helper function that translates ethtool advertisement settings
258 * to EEE advertisements for the MMD EEE Advertisement (7.60) and
259 * MMD EEE Link Partner Ability (7.61) registers.
260 */
261static inline u16 ethtool_adv_to_mmd_eee_adv_t(u32 adv)
262{
263 u16 reg = 0;
264
265 if (adv & ADVERTISED_100baseT_Full)
266 reg |= MDIO_EEE_100TX;
267 if (adv & ADVERTISED_1000baseT_Full)
268 reg |= MDIO_EEE_1000T;
269 if (adv & ADVERTISED_10000baseT_Full)
270 reg |= MDIO_EEE_10GT;
271 if (adv & ADVERTISED_1000baseKX_Full)
272 reg |= MDIO_EEE_1000KX;
273 if (adv & ADVERTISED_10000baseKX4_Full)
274 reg |= MDIO_EEE_10GKX4;
275 if (adv & ADVERTISED_10000baseKR_Full)
276 reg |= MDIO_EEE_10GKR;
277
278 return reg;
279}
280
744e458a
HK
281/**
282 * linkmode_adv_to_mii_10gbt_adv_t
283 * @advertising: the linkmode advertisement settings
284 *
285 * A small helper function that translates linkmode advertisement
286 * settings to phy autonegotiation advertisements for the C45
287 * 10GBASE-T AN CONTROL (7.32) register.
288 */
289static inline u32 linkmode_adv_to_mii_10gbt_adv_t(unsigned long *advertising)
290{
291 u32 result = 0;
292
293 if (linkmode_test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
294 advertising))
295 result |= MDIO_AN_10GBT_CTRL_ADV2_5G;
296 if (linkmode_test_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
297 advertising))
298 result |= MDIO_AN_10GBT_CTRL_ADV5G;
299 if (linkmode_test_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
300 advertising))
301 result |= MDIO_AN_10GBT_CTRL_ADV10G;
302
303 return result;
304}
305
9004a14c
HK
306/**
307 * mii_10gbt_stat_mod_linkmode_lpa_t
308 * @advertising: target the linkmode advertisement settings
309 * @adv: value of the C45 10GBASE-T AN STATUS register
310 *
311 * A small helper function that translates C45 10GBASE-T AN STATUS register bits
312 * to linkmode advertisement settings. Other bits in advertising aren't changed.
313 */
314static inline void mii_10gbt_stat_mod_linkmode_lpa_t(unsigned long *advertising,
315 u32 lpa)
316{
317 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
318 advertising, lpa & MDIO_AN_10GBT_STAT_LP2_5G);
319 linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
320 advertising, lpa & MDIO_AN_10GBT_STAT_LP5G);
321 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
322 advertising, lpa & MDIO_AN_10GBT_STAT_LP10G);
323}
324
34dc08e4
RK
325int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
326int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
6cc7cf81
RK
327int __mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
328 u16 mask, u16 set);
34dc08e4 329
bac83c65
AL
330int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
331int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum);
332int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
333int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val);
6cc7cf81
RK
334int mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask,
335 u16 set);
bac83c65 336
90ce665c
RK
337static inline u32 mdiobus_c45_addr(int devad, u16 regnum)
338{
339 return MII_ADDR_C45 | devad << MII_DEVADDR_C45_SHIFT | regnum;
340}
341
342static inline int __mdiobus_c45_read(struct mii_bus *bus, int prtad, int devad,
343 u16 regnum)
344{
345 return __mdiobus_read(bus, prtad, mdiobus_c45_addr(devad, regnum));
346}
347
348static inline int __mdiobus_c45_write(struct mii_bus *bus, int prtad, int devad,
349 u16 regnum, u16 val)
350{
351 return __mdiobus_write(bus, prtad, mdiobus_c45_addr(devad, regnum),
352 val);
353}
354
355static inline int mdiobus_c45_read(struct mii_bus *bus, int prtad, int devad,
356 u16 regnum)
357{
358 return mdiobus_read(bus, prtad, mdiobus_c45_addr(devad, regnum));
359}
360
7f854420
AL
361int mdiobus_register_device(struct mdio_device *mdiodev);
362int mdiobus_unregister_device(struct mdio_device *mdiodev);
363bool mdiobus_is_registered_device(struct mii_bus *bus, int addr);
364struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr);
365
a9049e0c 366/**
b70f43a1 367 * mdio_module_driver() - Helper macro for registering mdio drivers
a9049e0c
AL
368 *
369 * Helper macro for MDIO drivers which do not do anything special in module
370 * init/exit. Each module may only use this macro once, and calling it
371 * replaces module_init() and module_exit().
372 */
373#define mdio_module_driver(_mdio_driver) \
374static int __init mdio_module_init(void) \
375{ \
376 return mdio_driver_register(&_mdio_driver); \
377} \
378module_init(mdio_module_init); \
379static void __exit mdio_module_exit(void) \
380{ \
381 mdio_driver_unregister(&_mdio_driver); \
382} \
383module_exit(mdio_module_exit)
384
52c94dfa 385#endif /* __LINUX_MDIO_H__ */