]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - include/linux/mdio.h
Merge tag 'kernel.sys.v5.15' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner...
[mirror_ubuntu-jammy-kernel.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 51};
7eab14de
AL
52
53static inline struct mdio_device *to_mdio_device(const struct device *dev)
54{
55 return container_of(dev, struct mdio_device, dev);
56}
e5a03bfd 57
a9049e0c
AL
58/* struct mdio_driver_common: Common to all MDIO drivers */
59struct mdio_driver_common {
60 struct device_driver driver;
61 int flags;
62};
7f854420 63#define MDIO_DEVICE_FLAG_PHY 1
7eab14de
AL
64
65static inline struct mdio_driver_common *
66to_mdio_common_driver(const struct device_driver *driver)
67{
68 return container_of(driver, struct mdio_driver_common, driver);
69}
a9049e0c
AL
70
71/* struct mdio_driver: Generic MDIO driver */
72struct mdio_driver {
73 struct mdio_driver_common mdiodrv;
74
75 /*
76 * Called during discovery. Used to set
77 * up device-specific structures, if any
78 */
79 int (*probe)(struct mdio_device *mdiodev);
80
81 /* Clears up any memory if needed */
82 void (*remove)(struct mdio_device *mdiodev);
83};
7eab14de
AL
84
85static inline struct mdio_driver *
86to_mdio_driver(const struct device_driver *driver)
87{
88 return container_of(to_mdio_common_driver(driver), struct mdio_driver,
89 mdiodrv);
90}
a9049e0c 91
36b1a2fc
HK
92/* device driver data */
93static inline void mdiodev_set_drvdata(struct mdio_device *mdio, void *data)
94{
95 dev_set_drvdata(&mdio->dev, data);
96}
97
98static inline void *mdiodev_get_drvdata(struct mdio_device *mdio)
99{
100 return dev_get_drvdata(&mdio->dev);
101}
102
a9049e0c
AL
103void mdio_device_free(struct mdio_device *mdiodev);
104struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr);
105int mdio_device_register(struct mdio_device *mdiodev);
106void mdio_device_remove(struct mdio_device *mdiodev);
bafbdd52 107void mdio_device_reset(struct mdio_device *mdiodev, int value);
a9049e0c
AL
108int mdio_driver_register(struct mdio_driver *drv);
109void mdio_driver_unregister(struct mdio_driver *drv);
648ea013 110int mdio_device_bus_match(struct device *dev, struct device_driver *drv);
7f854420 111
52c94dfa
BH
112static inline bool mdio_phy_id_is_c45(int phy_id)
113{
114 return (phy_id & MDIO_PHY_ID_C45) && !(phy_id & ~MDIO_PHY_ID_C45_MASK);
115}
116
117static inline __u16 mdio_phy_id_prtad(int phy_id)
118{
119 return (phy_id & MDIO_PHY_ID_PRTAD) >> 5;
120}
121
122static inline __u16 mdio_phy_id_devad(int phy_id)
123{
124 return phy_id & MDIO_PHY_ID_DEVAD;
125}
126
1b1c2e95
BH
127/**
128 * struct mdio_if_info - Ethernet controller MDIO interface
129 * @prtad: PRTAD of the PHY (%MDIO_PRTAD_NONE if not present/unknown)
130 * @mmds: Mask of MMDs expected to be present in the PHY. This must be
131 * non-zero unless @prtad = %MDIO_PRTAD_NONE.
132 * @mode_support: MDIO modes supported. If %MDIO_SUPPORTS_C22 is set then
133 * MII register access will be passed through with @devad =
134 * %MDIO_DEVAD_NONE. If %MDIO_EMULATE_C22 is set then access to
135 * commonly used clause 22 registers will be translated into
136 * clause 45 registers.
137 * @dev: Net device structure
138 * @mdio_read: Register read function; returns value or negative error code
139 * @mdio_write: Register write function; returns 0 or negative error code
140 */
141struct mdio_if_info {
142 int prtad;
23428e6b 143 u32 mmds;
1b1c2e95
BH
144 unsigned mode_support;
145
146 struct net_device *dev;
147 int (*mdio_read)(struct net_device *dev, int prtad, int devad,
148 u16 addr);
149 int (*mdio_write)(struct net_device *dev, int prtad, int devad,
150 u16 addr, u16 val);
151};
152
153#define MDIO_PRTAD_NONE (-1)
154#define MDIO_DEVAD_NONE (-1)
9c717758
BH
155#define MDIO_SUPPORTS_C22 1
156#define MDIO_SUPPORTS_C45 2
1b1c2e95
BH
157#define MDIO_EMULATE_C22 4
158
159struct ethtool_cmd;
160struct ethtool_pauseparam;
161extern int mdio45_probe(struct mdio_if_info *mdio, int prtad);
162extern int mdio_set_flag(const struct mdio_if_info *mdio,
163 int prtad, int devad, u16 addr, int mask,
164 bool sense);
165extern int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmds);
166extern int mdio45_nway_restart(const struct mdio_if_info *mdio);
167extern void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio,
168 struct ethtool_cmd *ecmd,
169 u32 npage_adv, u32 npage_lpa);
8e4881aa
PR
170extern void
171mdio45_ethtool_ksettings_get_npage(const struct mdio_if_info *mdio,
172 struct ethtool_link_ksettings *cmd,
173 u32 npage_adv, u32 npage_lpa);
1b1c2e95
BH
174
175/**
176 * mdio45_ethtool_gset - get settings for ETHTOOL_GSET
177 * @mdio: MDIO interface
178 * @ecmd: Ethtool request structure
179 *
180 * Since the CSRs for auto-negotiation using next pages are not fully
181 * standardised, this function does not attempt to decode them. Use
182 * mdio45_ethtool_gset_npage() to specify advertisement bits from next
183 * pages.
184 */
185static inline void mdio45_ethtool_gset(const struct mdio_if_info *mdio,
186 struct ethtool_cmd *ecmd)
187{
188 mdio45_ethtool_gset_npage(mdio, ecmd, 0, 0);
189}
190
8e4881aa
PR
191/**
192 * mdio45_ethtool_ksettings_get - get settings for ETHTOOL_GLINKSETTINGS
193 * @mdio: MDIO interface
194 * @cmd: Ethtool request structure
195 *
196 * Since the CSRs for auto-negotiation using next pages are not fully
197 * standardised, this function does not attempt to decode them. Use
198 * mdio45_ethtool_ksettings_get_npage() to specify advertisement bits
199 * from next pages.
200 */
201static inline void
202mdio45_ethtool_ksettings_get(const struct mdio_if_info *mdio,
203 struct ethtool_link_ksettings *cmd)
204{
205 mdio45_ethtool_ksettings_get_npage(mdio, cmd, 0, 0);
206}
207
1b1c2e95
BH
208extern int mdio_mii_ioctl(const struct mdio_if_info *mdio,
209 struct mii_ioctl_data *mii_data, int cmd);
210
b32607dd
AB
211/**
212 * mmd_eee_cap_to_ethtool_sup_t
213 * @eee_cap: value of the MMD EEE Capability register
214 *
215 * A small helper function that translates MMD EEE Capability (3.20) bits
216 * to ethtool supported settings.
217 */
218static inline u32 mmd_eee_cap_to_ethtool_sup_t(u16 eee_cap)
219{
220 u32 supported = 0;
221
222 if (eee_cap & MDIO_EEE_100TX)
223 supported |= SUPPORTED_100baseT_Full;
224 if (eee_cap & MDIO_EEE_1000T)
225 supported |= SUPPORTED_1000baseT_Full;
226 if (eee_cap & MDIO_EEE_10GT)
227 supported |= SUPPORTED_10000baseT_Full;
228 if (eee_cap & MDIO_EEE_1000KX)
229 supported |= SUPPORTED_1000baseKX_Full;
230 if (eee_cap & MDIO_EEE_10GKX4)
231 supported |= SUPPORTED_10000baseKX4_Full;
232 if (eee_cap & MDIO_EEE_10GKR)
233 supported |= SUPPORTED_10000baseKR_Full;
234
235 return supported;
236}
237
238/**
239 * mmd_eee_adv_to_ethtool_adv_t
240 * @eee_adv: value of the MMD EEE Advertisement/Link Partner Ability registers
241 *
242 * A small helper function that translates the MMD EEE Advertisment (7.60)
243 * and MMD EEE Link Partner Ability (7.61) bits to ethtool advertisement
244 * settings.
245 */
246static inline u32 mmd_eee_adv_to_ethtool_adv_t(u16 eee_adv)
247{
248 u32 adv = 0;
249
250 if (eee_adv & MDIO_EEE_100TX)
251 adv |= ADVERTISED_100baseT_Full;
252 if (eee_adv & MDIO_EEE_1000T)
253 adv |= ADVERTISED_1000baseT_Full;
254 if (eee_adv & MDIO_EEE_10GT)
255 adv |= ADVERTISED_10000baseT_Full;
256 if (eee_adv & MDIO_EEE_1000KX)
257 adv |= ADVERTISED_1000baseKX_Full;
258 if (eee_adv & MDIO_EEE_10GKX4)
259 adv |= ADVERTISED_10000baseKX4_Full;
260 if (eee_adv & MDIO_EEE_10GKR)
261 adv |= ADVERTISED_10000baseKR_Full;
262
263 return adv;
264}
265
266/**
267 * ethtool_adv_to_mmd_eee_adv_t
268 * @adv: the ethtool advertisement settings
269 *
270 * A small helper function that translates ethtool advertisement settings
271 * to EEE advertisements for the MMD EEE Advertisement (7.60) and
272 * MMD EEE Link Partner Ability (7.61) registers.
273 */
274static inline u16 ethtool_adv_to_mmd_eee_adv_t(u32 adv)
275{
276 u16 reg = 0;
277
278 if (adv & ADVERTISED_100baseT_Full)
279 reg |= MDIO_EEE_100TX;
280 if (adv & ADVERTISED_1000baseT_Full)
281 reg |= MDIO_EEE_1000T;
282 if (adv & ADVERTISED_10000baseT_Full)
283 reg |= MDIO_EEE_10GT;
284 if (adv & ADVERTISED_1000baseKX_Full)
285 reg |= MDIO_EEE_1000KX;
286 if (adv & ADVERTISED_10000baseKX4_Full)
287 reg |= MDIO_EEE_10GKX4;
288 if (adv & ADVERTISED_10000baseKR_Full)
289 reg |= MDIO_EEE_10GKR;
290
291 return reg;
292}
293
744e458a
HK
294/**
295 * linkmode_adv_to_mii_10gbt_adv_t
296 * @advertising: the linkmode advertisement settings
297 *
298 * A small helper function that translates linkmode advertisement
299 * settings to phy autonegotiation advertisements for the C45
300 * 10GBASE-T AN CONTROL (7.32) register.
301 */
302static inline u32 linkmode_adv_to_mii_10gbt_adv_t(unsigned long *advertising)
303{
304 u32 result = 0;
305
306 if (linkmode_test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
307 advertising))
308 result |= MDIO_AN_10GBT_CTRL_ADV2_5G;
309 if (linkmode_test_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
310 advertising))
311 result |= MDIO_AN_10GBT_CTRL_ADV5G;
312 if (linkmode_test_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
313 advertising))
314 result |= MDIO_AN_10GBT_CTRL_ADV10G;
315
316 return result;
317}
318
9004a14c
HK
319/**
320 * mii_10gbt_stat_mod_linkmode_lpa_t
321 * @advertising: target the linkmode advertisement settings
39097ab6 322 * @lpa: value of the C45 10GBASE-T AN STATUS register
9004a14c
HK
323 *
324 * A small helper function that translates C45 10GBASE-T AN STATUS register bits
325 * to linkmode advertisement settings. Other bits in advertising aren't changed.
326 */
327static inline void mii_10gbt_stat_mod_linkmode_lpa_t(unsigned long *advertising,
328 u32 lpa)
329{
330 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
331 advertising, lpa & MDIO_AN_10GBT_STAT_LP2_5G);
332 linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
333 advertising, lpa & MDIO_AN_10GBT_STAT_LP5G);
334 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
335 advertising, lpa & MDIO_AN_10GBT_STAT_LP10G);
336}
337
34dc08e4
RK
338int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
339int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
6cc7cf81
RK
340int __mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
341 u16 mask, u16 set);
34dc08e4 342
bac83c65
AL
343int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
344int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum);
345int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
346int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val);
6cc7cf81
RK
347int mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask,
348 u16 set);
bac83c65 349
90ce665c
RK
350static inline u32 mdiobus_c45_addr(int devad, u16 regnum)
351{
352 return MII_ADDR_C45 | devad << MII_DEVADDR_C45_SHIFT | regnum;
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
361static inline int __mdiobus_c45_write(struct mii_bus *bus, int prtad, int devad,
362 u16 regnum, u16 val)
363{
364 return __mdiobus_write(bus, prtad, mdiobus_c45_addr(devad, regnum),
365 val);
366}
367
368static inline int mdiobus_c45_read(struct mii_bus *bus, int prtad, int devad,
369 u16 regnum)
370{
371 return mdiobus_read(bus, prtad, mdiobus_c45_addr(devad, regnum));
372}
373
2dab432c
IC
374static inline int mdiobus_c45_write(struct mii_bus *bus, int prtad, int devad,
375 u16 regnum, u16 val)
376{
377 return mdiobus_write(bus, prtad, mdiobus_c45_addr(devad, regnum), val);
378}
379
7f854420
AL
380int mdiobus_register_device(struct mdio_device *mdiodev);
381int mdiobus_unregister_device(struct mdio_device *mdiodev);
382bool mdiobus_is_registered_device(struct mii_bus *bus, int addr);
383struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr);
384
a9049e0c 385/**
b70f43a1 386 * mdio_module_driver() - Helper macro for registering mdio drivers
39097ab6 387 * @_mdio_driver: driver to register
a9049e0c
AL
388 *
389 * Helper macro for MDIO drivers which do not do anything special in module
390 * init/exit. Each module may only use this macro once, and calling it
391 * replaces module_init() and module_exit().
392 */
393#define mdio_module_driver(_mdio_driver) \
394static int __init mdio_module_init(void) \
395{ \
396 return mdio_driver_register(&_mdio_driver); \
397} \
398module_init(mdio_module_init); \
399static void __exit mdio_module_exit(void) \
400{ \
401 mdio_driver_unregister(&_mdio_driver); \
402} \
403module_exit(mdio_module_exit)
404
52c94dfa 405#endif /* __LINUX_MDIO_H__ */