blob: de5fb4b333ce3df46872ad0023fd71e3ca45ad03 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0-only */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * linux/mdio.h: definitions for MDIO (clause 45) transceivers
4 * Copyright 2006-2009 Solarflare Communications Inc.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005 */
6#ifndef __LINUX_MDIO_H__
7#define __LINUX_MDIO_H__
8
9#include <uapi/linux/mdio.h>
10#include <linux/mod_devicetable.h>
11
Olivier Deprez157378f2022-04-04 15:47:50 +020012/* 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000019struct gpio_desc;
20struct mii_bus;
Olivier Deprez157378f2022-04-04 15:47:50 +020021struct reset_control;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000022
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
34struct mdio_device {
35 struct device dev;
36
37 struct mii_bus *bus;
38 char modalias[MDIO_NAME_SIZE];
39
40 int (*bus_match)(struct device *dev, struct device_driver *drv);
41 void (*device_free)(struct mdio_device *mdiodev);
42 void (*device_remove)(struct mdio_device *mdiodev);
43
44 /* Bus address of the MDIO device (0-31) */
45 int addr;
46 int flags;
David Brazdil0f672f62019-12-10 10:32:29 +000047 struct gpio_desc *reset_gpio;
48 struct reset_control *reset_ctrl;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000049 unsigned int reset_assert_delay;
50 unsigned int reset_deassert_delay;
51};
52#define to_mdio_device(d) container_of(d, struct mdio_device, dev)
53
54/* struct mdio_driver_common: Common to all MDIO drivers */
55struct mdio_driver_common {
56 struct device_driver driver;
57 int flags;
58};
59#define MDIO_DEVICE_FLAG_PHY 1
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);
Olivier Deprez157378f2022-04-04 15:47:50 +020075
76 /* Quiesces the device on system shutdown, turns off interrupts etc */
77 void (*shutdown)(struct mdio_device *mdiodev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000078};
79#define to_mdio_driver(d) \
80 container_of(to_mdio_common_driver(d), struct mdio_driver, mdiodrv)
81
David Brazdil0f672f62019-12-10 10:32:29 +000082/* device driver data */
83static inline void mdiodev_set_drvdata(struct mdio_device *mdio, void *data)
84{
85 dev_set_drvdata(&mdio->dev, data);
86}
87
88static inline void *mdiodev_get_drvdata(struct mdio_device *mdio)
89{
90 return dev_get_drvdata(&mdio->dev);
91}
92
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000093void mdio_device_free(struct mdio_device *mdiodev);
94struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr);
95int mdio_device_register(struct mdio_device *mdiodev);
96void mdio_device_remove(struct mdio_device *mdiodev);
97void mdio_device_reset(struct mdio_device *mdiodev, int value);
98int mdio_driver_register(struct mdio_driver *drv);
99void mdio_driver_unregister(struct mdio_driver *drv);
100int mdio_device_bus_match(struct device *dev, struct device_driver *drv);
101
102static inline bool mdio_phy_id_is_c45(int phy_id)
103{
104 return (phy_id & MDIO_PHY_ID_C45) && !(phy_id & ~MDIO_PHY_ID_C45_MASK);
105}
106
107static inline __u16 mdio_phy_id_prtad(int phy_id)
108{
109 return (phy_id & MDIO_PHY_ID_PRTAD) >> 5;
110}
111
112static inline __u16 mdio_phy_id_devad(int phy_id)
113{
114 return phy_id & MDIO_PHY_ID_DEVAD;
115}
116
117/**
118 * struct mdio_if_info - Ethernet controller MDIO interface
119 * @prtad: PRTAD of the PHY (%MDIO_PRTAD_NONE if not present/unknown)
120 * @mmds: Mask of MMDs expected to be present in the PHY. This must be
121 * non-zero unless @prtad = %MDIO_PRTAD_NONE.
122 * @mode_support: MDIO modes supported. If %MDIO_SUPPORTS_C22 is set then
123 * MII register access will be passed through with @devad =
124 * %MDIO_DEVAD_NONE. If %MDIO_EMULATE_C22 is set then access to
125 * commonly used clause 22 registers will be translated into
126 * clause 45 registers.
127 * @dev: Net device structure
128 * @mdio_read: Register read function; returns value or negative error code
129 * @mdio_write: Register write function; returns 0 or negative error code
130 */
131struct mdio_if_info {
132 int prtad;
133 u32 mmds;
134 unsigned mode_support;
135
136 struct net_device *dev;
137 int (*mdio_read)(struct net_device *dev, int prtad, int devad,
138 u16 addr);
139 int (*mdio_write)(struct net_device *dev, int prtad, int devad,
140 u16 addr, u16 val);
141};
142
143#define MDIO_PRTAD_NONE (-1)
144#define MDIO_DEVAD_NONE (-1)
145#define MDIO_SUPPORTS_C22 1
146#define MDIO_SUPPORTS_C45 2
147#define MDIO_EMULATE_C22 4
148
149struct ethtool_cmd;
150struct ethtool_pauseparam;
151extern int mdio45_probe(struct mdio_if_info *mdio, int prtad);
152extern int mdio_set_flag(const struct mdio_if_info *mdio,
153 int prtad, int devad, u16 addr, int mask,
154 bool sense);
155extern int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmds);
156extern int mdio45_nway_restart(const struct mdio_if_info *mdio);
157extern void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio,
158 struct ethtool_cmd *ecmd,
159 u32 npage_adv, u32 npage_lpa);
160extern void
161mdio45_ethtool_ksettings_get_npage(const struct mdio_if_info *mdio,
162 struct ethtool_link_ksettings *cmd,
163 u32 npage_adv, u32 npage_lpa);
164
165/**
166 * mdio45_ethtool_gset - get settings for ETHTOOL_GSET
167 * @mdio: MDIO interface
168 * @ecmd: Ethtool request structure
169 *
170 * Since the CSRs for auto-negotiation using next pages are not fully
171 * standardised, this function does not attempt to decode them. Use
172 * mdio45_ethtool_gset_npage() to specify advertisement bits from next
173 * pages.
174 */
175static inline void mdio45_ethtool_gset(const struct mdio_if_info *mdio,
176 struct ethtool_cmd *ecmd)
177{
178 mdio45_ethtool_gset_npage(mdio, ecmd, 0, 0);
179}
180
181/**
182 * mdio45_ethtool_ksettings_get - get settings for ETHTOOL_GLINKSETTINGS
183 * @mdio: MDIO interface
184 * @cmd: Ethtool request structure
185 *
186 * Since the CSRs for auto-negotiation using next pages are not fully
187 * standardised, this function does not attempt to decode them. Use
188 * mdio45_ethtool_ksettings_get_npage() to specify advertisement bits
189 * from next pages.
190 */
191static inline void
192mdio45_ethtool_ksettings_get(const struct mdio_if_info *mdio,
193 struct ethtool_link_ksettings *cmd)
194{
195 mdio45_ethtool_ksettings_get_npage(mdio, cmd, 0, 0);
196}
197
198extern int mdio_mii_ioctl(const struct mdio_if_info *mdio,
199 struct mii_ioctl_data *mii_data, int cmd);
200
201/**
202 * mmd_eee_cap_to_ethtool_sup_t
203 * @eee_cap: value of the MMD EEE Capability register
204 *
205 * A small helper function that translates MMD EEE Capability (3.20) bits
206 * to ethtool supported settings.
207 */
208static inline u32 mmd_eee_cap_to_ethtool_sup_t(u16 eee_cap)
209{
210 u32 supported = 0;
211
212 if (eee_cap & MDIO_EEE_100TX)
213 supported |= SUPPORTED_100baseT_Full;
214 if (eee_cap & MDIO_EEE_1000T)
215 supported |= SUPPORTED_1000baseT_Full;
216 if (eee_cap & MDIO_EEE_10GT)
217 supported |= SUPPORTED_10000baseT_Full;
218 if (eee_cap & MDIO_EEE_1000KX)
219 supported |= SUPPORTED_1000baseKX_Full;
220 if (eee_cap & MDIO_EEE_10GKX4)
221 supported |= SUPPORTED_10000baseKX4_Full;
222 if (eee_cap & MDIO_EEE_10GKR)
223 supported |= SUPPORTED_10000baseKR_Full;
224
225 return supported;
226}
227
228/**
229 * mmd_eee_adv_to_ethtool_adv_t
230 * @eee_adv: value of the MMD EEE Advertisement/Link Partner Ability registers
231 *
232 * A small helper function that translates the MMD EEE Advertisment (7.60)
233 * and MMD EEE Link Partner Ability (7.61) bits to ethtool advertisement
234 * settings.
235 */
236static inline u32 mmd_eee_adv_to_ethtool_adv_t(u16 eee_adv)
237{
238 u32 adv = 0;
239
240 if (eee_adv & MDIO_EEE_100TX)
241 adv |= ADVERTISED_100baseT_Full;
242 if (eee_adv & MDIO_EEE_1000T)
243 adv |= ADVERTISED_1000baseT_Full;
244 if (eee_adv & MDIO_EEE_10GT)
245 adv |= ADVERTISED_10000baseT_Full;
246 if (eee_adv & MDIO_EEE_1000KX)
247 adv |= ADVERTISED_1000baseKX_Full;
248 if (eee_adv & MDIO_EEE_10GKX4)
249 adv |= ADVERTISED_10000baseKX4_Full;
250 if (eee_adv & MDIO_EEE_10GKR)
251 adv |= ADVERTISED_10000baseKR_Full;
252
253 return adv;
254}
255
256/**
257 * ethtool_adv_to_mmd_eee_adv_t
258 * @adv: the ethtool advertisement settings
259 *
260 * A small helper function that translates ethtool advertisement settings
261 * to EEE advertisements for the MMD EEE Advertisement (7.60) and
262 * MMD EEE Link Partner Ability (7.61) registers.
263 */
264static inline u16 ethtool_adv_to_mmd_eee_adv_t(u32 adv)
265{
266 u16 reg = 0;
267
268 if (adv & ADVERTISED_100baseT_Full)
269 reg |= MDIO_EEE_100TX;
270 if (adv & ADVERTISED_1000baseT_Full)
271 reg |= MDIO_EEE_1000T;
272 if (adv & ADVERTISED_10000baseT_Full)
273 reg |= MDIO_EEE_10GT;
274 if (adv & ADVERTISED_1000baseKX_Full)
275 reg |= MDIO_EEE_1000KX;
276 if (adv & ADVERTISED_10000baseKX4_Full)
277 reg |= MDIO_EEE_10GKX4;
278 if (adv & ADVERTISED_10000baseKR_Full)
279 reg |= MDIO_EEE_10GKR;
280
281 return reg;
282}
283
David Brazdil0f672f62019-12-10 10:32:29 +0000284/**
285 * linkmode_adv_to_mii_10gbt_adv_t
286 * @advertising: the linkmode advertisement settings
287 *
288 * A small helper function that translates linkmode advertisement
289 * settings to phy autonegotiation advertisements for the C45
290 * 10GBASE-T AN CONTROL (7.32) register.
291 */
292static inline u32 linkmode_adv_to_mii_10gbt_adv_t(unsigned long *advertising)
293{
294 u32 result = 0;
295
296 if (linkmode_test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
297 advertising))
298 result |= MDIO_AN_10GBT_CTRL_ADV2_5G;
299 if (linkmode_test_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
300 advertising))
301 result |= MDIO_AN_10GBT_CTRL_ADV5G;
302 if (linkmode_test_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
303 advertising))
304 result |= MDIO_AN_10GBT_CTRL_ADV10G;
305
306 return result;
307}
308
309/**
310 * mii_10gbt_stat_mod_linkmode_lpa_t
311 * @advertising: target the linkmode advertisement settings
Olivier Deprez157378f2022-04-04 15:47:50 +0200312 * @lpa: value of the C45 10GBASE-T AN STATUS register
David Brazdil0f672f62019-12-10 10:32:29 +0000313 *
314 * A small helper function that translates C45 10GBASE-T AN STATUS register bits
315 * to linkmode advertisement settings. Other bits in advertising aren't changed.
316 */
317static inline void mii_10gbt_stat_mod_linkmode_lpa_t(unsigned long *advertising,
318 u32 lpa)
319{
320 linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
321 advertising, lpa & MDIO_AN_10GBT_STAT_LP2_5G);
322 linkmode_mod_bit(ETHTOOL_LINK_MODE_5000baseT_Full_BIT,
323 advertising, lpa & MDIO_AN_10GBT_STAT_LP5G);
324 linkmode_mod_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
325 advertising, lpa & MDIO_AN_10GBT_STAT_LP10G);
326}
327
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000328int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
329int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
Olivier Deprez157378f2022-04-04 15:47:50 +0200330int __mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
331 u16 mask, u16 set);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000332
333int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
334int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum);
335int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
336int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val);
Olivier Deprez157378f2022-04-04 15:47:50 +0200337int mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask,
338 u16 set);
339
340static inline u32 mdiobus_c45_addr(int devad, u16 regnum)
341{
342 return MII_ADDR_C45 | devad << MII_DEVADDR_C45_SHIFT | regnum;
343}
344
345static inline int __mdiobus_c45_read(struct mii_bus *bus, int prtad, int devad,
346 u16 regnum)
347{
348 return __mdiobus_read(bus, prtad, mdiobus_c45_addr(devad, regnum));
349}
350
351static inline int __mdiobus_c45_write(struct mii_bus *bus, int prtad, int devad,
352 u16 regnum, u16 val)
353{
354 return __mdiobus_write(bus, prtad, mdiobus_c45_addr(devad, regnum),
355 val);
356}
357
358static inline int mdiobus_c45_read(struct mii_bus *bus, int prtad, int devad,
359 u16 regnum)
360{
361 return mdiobus_read(bus, prtad, mdiobus_c45_addr(devad, regnum));
362}
363
364static inline int mdiobus_c45_write(struct mii_bus *bus, int prtad, int devad,
365 u16 regnum, u16 val)
366{
367 return mdiobus_write(bus, prtad, mdiobus_c45_addr(devad, regnum), val);
368}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000369
370int mdiobus_register_device(struct mdio_device *mdiodev);
371int mdiobus_unregister_device(struct mdio_device *mdiodev);
372bool mdiobus_is_registered_device(struct mii_bus *bus, int addr);
373struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr);
374
375/**
376 * mdio_module_driver() - Helper macro for registering mdio drivers
Olivier Deprez157378f2022-04-04 15:47:50 +0200377 * @_mdio_driver: driver to register
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000378 *
379 * Helper macro for MDIO drivers which do not do anything special in module
380 * init/exit. Each module may only use this macro once, and calling it
381 * replaces module_init() and module_exit().
382 */
383#define mdio_module_driver(_mdio_driver) \
384static int __init mdio_module_init(void) \
385{ \
386 return mdio_driver_register(&_mdio_driver); \
387} \
388module_init(mdio_module_init); \
389static void __exit mdio_module_exit(void) \
390{ \
391 mdio_driver_unregister(&_mdio_driver); \
392} \
393module_exit(mdio_module_exit)
394
395#endif /* __LINUX_MDIO_H__ */