Linux Audio

Check our new training course

Linux kernel drivers training

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