0690359e55a55ebf9016d54f32e84c4d99e8b7a6
[cascardo/linux.git] / include / linux / mdio.h
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 struct mii_bus;
15
16 struct mdio_device {
17         struct device dev;
18         const struct dev_pm_ops *pm_ops;
19         struct mii_bus *bus;
20         int (*bus_match)(struct device *dev, struct device_driver *drv);
21         /* Bus address of the MDIO device (0-31) */
22         int addr;
23         int flags;
24 };
25 #define to_mdio_device(d) container_of(d, struct mdio_device, dev)
26
27 #define MDIO_DEVICE_FLAG_PHY            1
28
29 static inline bool mdio_phy_id_is_c45(int phy_id)
30 {
31         return (phy_id & MDIO_PHY_ID_C45) && !(phy_id & ~MDIO_PHY_ID_C45_MASK);
32 }
33
34 static inline __u16 mdio_phy_id_prtad(int phy_id)
35 {
36         return (phy_id & MDIO_PHY_ID_PRTAD) >> 5;
37 }
38
39 static inline __u16 mdio_phy_id_devad(int phy_id)
40 {
41         return phy_id & MDIO_PHY_ID_DEVAD;
42 }
43
44 /**
45  * struct mdio_if_info - Ethernet controller MDIO interface
46  * @prtad: PRTAD of the PHY (%MDIO_PRTAD_NONE if not present/unknown)
47  * @mmds: Mask of MMDs expected to be present in the PHY.  This must be
48  *      non-zero unless @prtad = %MDIO_PRTAD_NONE.
49  * @mode_support: MDIO modes supported.  If %MDIO_SUPPORTS_C22 is set then
50  *      MII register access will be passed through with @devad =
51  *      %MDIO_DEVAD_NONE.  If %MDIO_EMULATE_C22 is set then access to
52  *      commonly used clause 22 registers will be translated into
53  *      clause 45 registers.
54  * @dev: Net device structure
55  * @mdio_read: Register read function; returns value or negative error code
56  * @mdio_write: Register write function; returns 0 or negative error code
57  */
58 struct mdio_if_info {
59         int prtad;
60         u32 mmds;
61         unsigned mode_support;
62
63         struct net_device *dev;
64         int (*mdio_read)(struct net_device *dev, int prtad, int devad,
65                          u16 addr);
66         int (*mdio_write)(struct net_device *dev, int prtad, int devad,
67                           u16 addr, u16 val);
68 };
69
70 #define MDIO_PRTAD_NONE                 (-1)
71 #define MDIO_DEVAD_NONE                 (-1)
72 #define MDIO_SUPPORTS_C22               1
73 #define MDIO_SUPPORTS_C45               2
74 #define MDIO_EMULATE_C22                4
75
76 struct ethtool_cmd;
77 struct ethtool_pauseparam;
78 extern int mdio45_probe(struct mdio_if_info *mdio, int prtad);
79 extern int mdio_set_flag(const struct mdio_if_info *mdio,
80                          int prtad, int devad, u16 addr, int mask,
81                          bool sense);
82 extern int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmds);
83 extern int mdio45_nway_restart(const struct mdio_if_info *mdio);
84 extern void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio,
85                                       struct ethtool_cmd *ecmd,
86                                       u32 npage_adv, u32 npage_lpa);
87
88 /**
89  * mdio45_ethtool_gset - get settings for ETHTOOL_GSET
90  * @mdio: MDIO interface
91  * @ecmd: Ethtool request structure
92  *
93  * Since the CSRs for auto-negotiation using next pages are not fully
94  * standardised, this function does not attempt to decode them.  Use
95  * mdio45_ethtool_gset_npage() to specify advertisement bits from next
96  * pages.
97  */
98 static inline void mdio45_ethtool_gset(const struct mdio_if_info *mdio,
99                                        struct ethtool_cmd *ecmd)
100 {
101         mdio45_ethtool_gset_npage(mdio, ecmd, 0, 0);
102 }
103
104 extern int mdio_mii_ioctl(const struct mdio_if_info *mdio,
105                           struct mii_ioctl_data *mii_data, int cmd);
106
107 /**
108  * mmd_eee_cap_to_ethtool_sup_t
109  * @eee_cap: value of the MMD EEE Capability register
110  *
111  * A small helper function that translates MMD EEE Capability (3.20) bits
112  * to ethtool supported settings.
113  */
114 static inline u32 mmd_eee_cap_to_ethtool_sup_t(u16 eee_cap)
115 {
116         u32 supported = 0;
117
118         if (eee_cap & MDIO_EEE_100TX)
119                 supported |= SUPPORTED_100baseT_Full;
120         if (eee_cap & MDIO_EEE_1000T)
121                 supported |= SUPPORTED_1000baseT_Full;
122         if (eee_cap & MDIO_EEE_10GT)
123                 supported |= SUPPORTED_10000baseT_Full;
124         if (eee_cap & MDIO_EEE_1000KX)
125                 supported |= SUPPORTED_1000baseKX_Full;
126         if (eee_cap & MDIO_EEE_10GKX4)
127                 supported |= SUPPORTED_10000baseKX4_Full;
128         if (eee_cap & MDIO_EEE_10GKR)
129                 supported |= SUPPORTED_10000baseKR_Full;
130
131         return supported;
132 }
133
134 /**
135  * mmd_eee_adv_to_ethtool_adv_t
136  * @eee_adv: value of the MMD EEE Advertisement/Link Partner Ability registers
137  *
138  * A small helper function that translates the MMD EEE Advertisment (7.60)
139  * and MMD EEE Link Partner Ability (7.61) bits to ethtool advertisement
140  * settings.
141  */
142 static inline u32 mmd_eee_adv_to_ethtool_adv_t(u16 eee_adv)
143 {
144         u32 adv = 0;
145
146         if (eee_adv & MDIO_EEE_100TX)
147                 adv |= ADVERTISED_100baseT_Full;
148         if (eee_adv & MDIO_EEE_1000T)
149                 adv |= ADVERTISED_1000baseT_Full;
150         if (eee_adv & MDIO_EEE_10GT)
151                 adv |= ADVERTISED_10000baseT_Full;
152         if (eee_adv & MDIO_EEE_1000KX)
153                 adv |= ADVERTISED_1000baseKX_Full;
154         if (eee_adv & MDIO_EEE_10GKX4)
155                 adv |= ADVERTISED_10000baseKX4_Full;
156         if (eee_adv & MDIO_EEE_10GKR)
157                 adv |= ADVERTISED_10000baseKR_Full;
158
159         return adv;
160 }
161
162 /**
163  * ethtool_adv_to_mmd_eee_adv_t
164  * @adv: the ethtool advertisement settings
165  *
166  * A small helper function that translates ethtool advertisement settings
167  * to EEE advertisements for the MMD EEE Advertisement (7.60) and
168  * MMD EEE Link Partner Ability (7.61) registers.
169  */
170 static inline u16 ethtool_adv_to_mmd_eee_adv_t(u32 adv)
171 {
172         u16 reg = 0;
173
174         if (adv & ADVERTISED_100baseT_Full)
175                 reg |= MDIO_EEE_100TX;
176         if (adv & ADVERTISED_1000baseT_Full)
177                 reg |= MDIO_EEE_1000T;
178         if (adv & ADVERTISED_10000baseT_Full)
179                 reg |= MDIO_EEE_10GT;
180         if (adv & ADVERTISED_1000baseKX_Full)
181                 reg |= MDIO_EEE_1000KX;
182         if (adv & ADVERTISED_10000baseKX4_Full)
183                 reg |= MDIO_EEE_10GKX4;
184         if (adv & ADVERTISED_10000baseKR_Full)
185                 reg |= MDIO_EEE_10GKR;
186
187         return reg;
188 }
189
190 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
191 int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum);
192 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
193 int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val);
194
195 int mdiobus_register_device(struct mdio_device *mdiodev);
196 int mdiobus_unregister_device(struct mdio_device *mdiodev);
197 bool mdiobus_is_registered_device(struct mii_bus *bus, int addr);
198 struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr);
199
200 #endif /* __LINUX_MDIO_H__ */