blob: 16fbb11d92aca1cf74369058ab373cf60555302b [file] [log] [blame]
Ben Hutchings1b1c2e92009-04-29 08:04:46 +00001/*
2 * mdio.c: Generic support for MDIO-compatible 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
10#include <linux/kernel.h>
11#include <linux/capability.h>
12#include <linux/errno.h>
13#include <linux/ethtool.h>
14#include <linux/mdio.h>
15#include <linux/module.h>
16
Nicolas Reinecke30767632009-06-25 02:55:31 +000017MODULE_DESCRIPTION("Generic support for MDIO-compatible transceivers");
18MODULE_AUTHOR("Copyright 2006-2009 Solarflare Communications Inc.");
19MODULE_LICENSE("GPL");
20
Ben Hutchings1b1c2e92009-04-29 08:04:46 +000021/**
22 * mdio45_probe - probe for an MDIO (clause 45) device
23 * @mdio: MDIO interface
24 * @prtad: Expected PHY address
25 *
26 * This sets @prtad and @mmds in the MDIO interface if successful.
27 * Returns 0 on success, negative on error.
28 */
29int mdio45_probe(struct mdio_if_info *mdio, int prtad)
30{
31 int mmd, stat2, devs1, devs2;
32
33 /* Assume PHY must have at least one of PMA/PMD, WIS, PCS, PHY
34 * XS or DTE XS; give up if none is present. */
35 for (mmd = 1; mmd <= 5; mmd++) {
36 /* Is this MMD present? */
37 stat2 = mdio->mdio_read(mdio->dev, prtad, mmd, MDIO_STAT2);
38 if (stat2 < 0 ||
39 (stat2 & MDIO_STAT2_DEVPRST) != MDIO_STAT2_DEVPRST_VAL)
40 continue;
41
42 /* It should tell us about all the other MMDs */
43 devs1 = mdio->mdio_read(mdio->dev, prtad, mmd, MDIO_DEVS1);
44 devs2 = mdio->mdio_read(mdio->dev, prtad, mmd, MDIO_DEVS2);
45 if (devs1 < 0 || devs2 < 0)
46 continue;
47
48 mdio->prtad = prtad;
49 mdio->mmds = devs1 | (devs2 << 16);
50 return 0;
51 }
52
53 return -ENODEV;
54}
55EXPORT_SYMBOL(mdio45_probe);
56
57/**
58 * mdio_set_flag - set or clear flag in an MDIO register
59 * @mdio: MDIO interface
60 * @prtad: PHY address
61 * @devad: MMD address
62 * @addr: Register address
63 * @mask: Mask for flag (single bit set)
64 * @sense: New value of flag
65 *
66 * This debounces changes: it does not write the register if the flag
67 * already has the proper value. Returns 0 on success, negative on error.
68 */
69int mdio_set_flag(const struct mdio_if_info *mdio,
70 int prtad, int devad, u16 addr, int mask,
71 bool sense)
72{
73 int old_val = mdio->mdio_read(mdio->dev, prtad, devad, addr);
74 int new_val;
75
76 if (old_val < 0)
77 return old_val;
78 if (sense)
79 new_val = old_val | mask;
80 else
81 new_val = old_val & ~mask;
82 if (old_val == new_val)
83 return 0;
84 return mdio->mdio_write(mdio->dev, prtad, devad, addr, new_val);
85}
86EXPORT_SYMBOL(mdio_set_flag);
87
88/**
89 * mdio_link_ok - is link status up/OK
90 * @mdio: MDIO interface
91 * @mmd_mask: Mask for MMDs to check
92 *
93 * Returns 1 if the PHY reports link status up/OK, 0 otherwise.
94 * @mmd_mask is normally @mdio->mmds, but if loopback is enabled
95 * the MMDs being bypassed should be excluded from the mask.
96 */
97int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmd_mask)
98{
99 int devad, reg;
100
101 if (!mmd_mask) {
102 /* Use absence of XGMII faults in lieu of link state */
103 reg = mdio->mdio_read(mdio->dev, mdio->prtad,
104 MDIO_MMD_PHYXS, MDIO_STAT2);
105 return reg >= 0 && !(reg & MDIO_STAT2_RXFAULT);
106 }
107
108 for (devad = 0; mmd_mask; devad++) {
109 if (mmd_mask & (1 << devad)) {
110 mmd_mask &= ~(1 << devad);
111
Ben Hutchings771046d2009-08-26 08:16:58 +0000112 /* Reset the latched status and fault flags */
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000113 mdio->mdio_read(mdio->dev, mdio->prtad,
114 devad, MDIO_STAT1);
Ben Hutchings771046d2009-08-26 08:16:58 +0000115 if (devad == MDIO_MMD_PMAPMD || devad == MDIO_MMD_PCS ||
116 devad == MDIO_MMD_PHYXS || devad == MDIO_MMD_DTEXS)
117 mdio->mdio_read(mdio->dev, mdio->prtad,
118 devad, MDIO_STAT2);
119
120 /* Check the current status and fault flags */
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000121 reg = mdio->mdio_read(mdio->dev, mdio->prtad,
122 devad, MDIO_STAT1);
Ben Hutchings771046d2009-08-26 08:16:58 +0000123 if (reg < 0 ||
124 (reg & (MDIO_STAT1_FAULT | MDIO_STAT1_LSTATUS)) !=
125 MDIO_STAT1_LSTATUS)
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000126 return false;
127 }
128 }
129
130 return true;
131}
132EXPORT_SYMBOL(mdio45_links_ok);
133
134/**
135 * mdio45_nway_restart - restart auto-negotiation for this interface
136 * @mdio: MDIO interface
137 *
138 * Returns 0 on success, negative on error.
139 */
140int mdio45_nway_restart(const struct mdio_if_info *mdio)
141{
142 if (!(mdio->mmds & MDIO_DEVS_AN))
143 return -EOPNOTSUPP;
144
145 mdio_set_flag(mdio, mdio->prtad, MDIO_MMD_AN, MDIO_CTRL1,
146 MDIO_AN_CTRL1_RESTART, true);
147 return 0;
148}
149EXPORT_SYMBOL(mdio45_nway_restart);
150
151static u32 mdio45_get_an(const struct mdio_if_info *mdio, u16 addr)
152{
153 u32 result = 0;
154 int reg;
155
156 reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_AN, addr);
157 if (reg & ADVERTISE_10HALF)
158 result |= ADVERTISED_10baseT_Half;
159 if (reg & ADVERTISE_10FULL)
160 result |= ADVERTISED_10baseT_Full;
161 if (reg & ADVERTISE_100HALF)
162 result |= ADVERTISED_100baseT_Half;
163 if (reg & ADVERTISE_100FULL)
164 result |= ADVERTISED_100baseT_Full;
Ben Hutchings27fbc7d2009-10-12 09:26:37 +0000165 if (reg & ADVERTISE_PAUSE_CAP)
166 result |= ADVERTISED_Pause;
167 if (reg & ADVERTISE_PAUSE_ASYM)
168 result |= ADVERTISED_Asym_Pause;
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000169 return result;
170}
171
172/**
173 * mdio45_ethtool_gset_npage - get settings for ETHTOOL_GSET
174 * @mdio: MDIO interface
175 * @ecmd: Ethtool request structure
176 * @npage_adv: Modes currently advertised on next pages
177 * @npage_lpa: Modes advertised by link partner on next pages
178 *
David Decotigny8ae6dac2011-04-27 18:32:38 +0000179 * The @ecmd parameter is expected to have been cleared before calling
180 * mdio45_ethtool_gset_npage().
181 *
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000182 * Since the CSRs for auto-negotiation using next pages are not fully
183 * standardised, this function does not attempt to decode them. The
184 * caller must pass them in.
185 */
186void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio,
187 struct ethtool_cmd *ecmd,
188 u32 npage_adv, u32 npage_lpa)
189{
190 int reg;
David Decotigny70739492011-04-27 18:32:40 +0000191 u32 speed;
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000192
193 ecmd->transceiver = XCVR_INTERNAL;
194 ecmd->phy_address = mdio->prtad;
Ben Hutchings0c09c1a2009-04-29 08:21:53 +0000195 ecmd->mdio_support =
196 mdio->mode_support & (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22);
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000197
198 reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
199 MDIO_CTRL2);
200 switch (reg & MDIO_PMA_CTRL2_TYPE) {
201 case MDIO_PMA_CTRL2_10GBT:
202 case MDIO_PMA_CTRL2_1000BT:
203 case MDIO_PMA_CTRL2_100BTX:
204 case MDIO_PMA_CTRL2_10BT:
205 ecmd->port = PORT_TP;
206 ecmd->supported = SUPPORTED_TP;
207 reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
208 MDIO_SPEED);
209 if (reg & MDIO_SPEED_10G)
210 ecmd->supported |= SUPPORTED_10000baseT_Full;
211 if (reg & MDIO_PMA_SPEED_1000)
212 ecmd->supported |= (SUPPORTED_1000baseT_Full |
213 SUPPORTED_1000baseT_Half);
214 if (reg & MDIO_PMA_SPEED_100)
215 ecmd->supported |= (SUPPORTED_100baseT_Full |
216 SUPPORTED_100baseT_Half);
217 if (reg & MDIO_PMA_SPEED_10)
218 ecmd->supported |= (SUPPORTED_10baseT_Full |
219 SUPPORTED_10baseT_Half);
220 ecmd->advertising = ADVERTISED_TP;
221 break;
222
223 case MDIO_PMA_CTRL2_10GBCX4:
Ben Hutchings894b19a2009-04-29 08:25:57 +0000224 ecmd->port = PORT_OTHER;
225 ecmd->supported = 0;
226 ecmd->advertising = 0;
227 break;
228
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000229 case MDIO_PMA_CTRL2_10GBKX4:
230 case MDIO_PMA_CTRL2_10GBKR:
231 case MDIO_PMA_CTRL2_1000BKX:
232 ecmd->port = PORT_OTHER;
Ben Hutchings894b19a2009-04-29 08:25:57 +0000233 ecmd->supported = SUPPORTED_Backplane;
234 reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
235 MDIO_PMA_EXTABLE);
236 if (reg & MDIO_PMA_EXTABLE_10GBKX4)
237 ecmd->supported |= SUPPORTED_10000baseKX4_Full;
238 if (reg & MDIO_PMA_EXTABLE_10GBKR)
239 ecmd->supported |= SUPPORTED_10000baseKR_Full;
240 if (reg & MDIO_PMA_EXTABLE_1000BKX)
241 ecmd->supported |= SUPPORTED_1000baseKX_Full;
242 reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
243 MDIO_PMA_10GBR_FECABLE);
244 if (reg & MDIO_PMA_10GBR_FECABLE_ABLE)
245 ecmd->supported |= SUPPORTED_10000baseR_FEC;
246 ecmd->advertising = ADVERTISED_Backplane;
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000247 break;
248
249 /* All the other defined modes are flavours of optical */
250 default:
251 ecmd->port = PORT_FIBRE;
252 ecmd->supported = SUPPORTED_FIBRE;
253 ecmd->advertising = ADVERTISED_FIBRE;
254 break;
255 }
256
257 if (mdio->mmds & MDIO_DEVS_AN) {
258 ecmd->supported |= SUPPORTED_Autoneg;
259 reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_AN,
260 MDIO_CTRL1);
261 if (reg & MDIO_AN_CTRL1_ENABLE) {
262 ecmd->autoneg = AUTONEG_ENABLE;
263 ecmd->advertising |=
264 ADVERTISED_Autoneg |
265 mdio45_get_an(mdio, MDIO_AN_ADVERTISE) |
266 npage_adv;
267 } else {
268 ecmd->autoneg = AUTONEG_DISABLE;
269 }
270 } else {
271 ecmd->autoneg = AUTONEG_DISABLE;
272 }
273
274 if (ecmd->autoneg) {
275 u32 modes = 0;
Ben Hutchings0c09c1a2009-04-29 08:21:53 +0000276 int an_stat = mdio->mdio_read(mdio->dev, mdio->prtad,
277 MDIO_MMD_AN, MDIO_STAT1);
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000278
279 /* If AN is complete and successful, report best common
280 * mode, otherwise report best advertised mode. */
Ben Hutchings0c09c1a2009-04-29 08:21:53 +0000281 if (an_stat & MDIO_AN_STAT1_COMPLETE) {
282 ecmd->lp_advertising =
283 mdio45_get_an(mdio, MDIO_AN_LPA) | npage_lpa;
284 if (an_stat & MDIO_AN_STAT1_LPABLE)
285 ecmd->lp_advertising |= ADVERTISED_Autoneg;
286 modes = ecmd->advertising & ecmd->lp_advertising;
287 }
288 if ((modes & ~ADVERTISED_Autoneg) == 0)
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000289 modes = ecmd->advertising;
290
Ben Hutchings894b19a2009-04-29 08:25:57 +0000291 if (modes & (ADVERTISED_10000baseT_Full |
292 ADVERTISED_10000baseKX4_Full |
293 ADVERTISED_10000baseKR_Full)) {
David Decotigny70739492011-04-27 18:32:40 +0000294 speed = SPEED_10000;
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000295 ecmd->duplex = DUPLEX_FULL;
296 } else if (modes & (ADVERTISED_1000baseT_Full |
Ben Hutchings894b19a2009-04-29 08:25:57 +0000297 ADVERTISED_1000baseT_Half |
298 ADVERTISED_1000baseKX_Full)) {
David Decotigny70739492011-04-27 18:32:40 +0000299 speed = SPEED_1000;
Ben Hutchings894b19a2009-04-29 08:25:57 +0000300 ecmd->duplex = !(modes & ADVERTISED_1000baseT_Half);
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000301 } else if (modes & (ADVERTISED_100baseT_Full |
302 ADVERTISED_100baseT_Half)) {
David Decotigny70739492011-04-27 18:32:40 +0000303 speed = SPEED_100;
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000304 ecmd->duplex = !!(modes & ADVERTISED_100baseT_Full);
305 } else {
David Decotigny70739492011-04-27 18:32:40 +0000306 speed = SPEED_10;
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000307 ecmd->duplex = !!(modes & ADVERTISED_10baseT_Full);
308 }
309 } else {
310 /* Report forced settings */
311 reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
312 MDIO_CTRL1);
David Decotigny70739492011-04-27 18:32:40 +0000313 speed = (((reg & MDIO_PMA_CTRL1_SPEED1000) ? 100 : 1)
314 * ((reg & MDIO_PMA_CTRL1_SPEED100) ? 100 : 10));
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000315 ecmd->duplex = (reg & MDIO_CTRL1_FULLDPLX ||
David Decotigny70739492011-04-27 18:32:40 +0000316 speed == SPEED_10000);
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000317 }
Ben Hutchingsd005ba62009-06-10 05:28:04 +0000318
David Decotigny70739492011-04-27 18:32:40 +0000319 ethtool_cmd_speed_set(ecmd, speed);
320
Ben Hutchingsd005ba62009-06-10 05:28:04 +0000321 /* 10GBASE-T MDI/MDI-X */
David Decotigny70739492011-04-27 18:32:40 +0000322 if (ecmd->port == PORT_TP
323 && (ethtool_cmd_speed(ecmd) == SPEED_10000)) {
Ben Hutchingsd005ba62009-06-10 05:28:04 +0000324 switch (mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
325 MDIO_PMA_10GBT_SWAPPOL)) {
326 case MDIO_PMA_10GBT_SWAPPOL_ABNX | MDIO_PMA_10GBT_SWAPPOL_CDNX:
327 ecmd->eth_tp_mdix = ETH_TP_MDI;
328 break;
329 case 0:
330 ecmd->eth_tp_mdix = ETH_TP_MDI_X;
331 break;
332 default:
333 /* It's complicated... */
334 ecmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
335 break;
336 }
337 }
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000338}
339EXPORT_SYMBOL(mdio45_ethtool_gset_npage);
340
341/**
Ben Hutchingsaf2a3ea2009-04-29 08:19:36 +0000342 * mdio45_ethtool_spauseparam_an - set auto-negotiated pause parameters
343 * @mdio: MDIO interface
344 * @ecmd: Ethtool request structure
345 *
346 * This function assumes that the PHY has an auto-negotiation MMD. It
347 * will enable and disable advertising of flow control as appropriate.
348 */
349void mdio45_ethtool_spauseparam_an(const struct mdio_if_info *mdio,
350 const struct ethtool_pauseparam *ecmd)
351{
352 int adv, old_adv;
353
354 WARN_ON(!(mdio->mmds & MDIO_DEVS_AN));
355
356 old_adv = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_AN,
357 MDIO_AN_ADVERTISE);
Ben Hutchings767f4a72009-10-12 09:26:17 +0000358 adv = ((old_adv & ~(ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM)) |
359 mii_advertise_flowctrl((ecmd->rx_pause ? FLOW_CTRL_RX : 0) |
360 (ecmd->tx_pause ? FLOW_CTRL_TX : 0)));
Ben Hutchingsaf2a3ea2009-04-29 08:19:36 +0000361 if (adv != old_adv) {
362 mdio->mdio_write(mdio->dev, mdio->prtad, MDIO_MMD_AN,
363 MDIO_AN_ADVERTISE, adv);
364 mdio45_nway_restart(mdio);
365 }
366}
367EXPORT_SYMBOL(mdio45_ethtool_spauseparam_an);
368
369/**
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000370 * mdio_mii_ioctl - MII ioctl interface for MDIO (clause 22 or 45) PHYs
371 * @mdio: MDIO interface
372 * @mii_data: MII ioctl data structure
373 * @cmd: MII ioctl command
374 *
375 * Returns 0 on success, negative on error.
376 */
377int mdio_mii_ioctl(const struct mdio_if_info *mdio,
378 struct mii_ioctl_data *mii_data, int cmd)
379{
380 int prtad, devad;
381 u16 addr = mii_data->reg_num;
382
383 /* Validate/convert cmd to one of SIOC{G,S}MIIREG */
384 switch (cmd) {
385 case SIOCGMIIPHY:
386 if (mdio->prtad == MDIO_PRTAD_NONE)
387 return -EOPNOTSUPP;
388 mii_data->phy_id = mdio->prtad;
389 cmd = SIOCGMIIREG;
390 break;
391 case SIOCGMIIREG:
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000392 case SIOCSMIIREG:
Ben Hutchings1b1c2e92009-04-29 08:04:46 +0000393 break;
394 default:
395 return -EOPNOTSUPP;
396 }
397
398 /* Validate/convert phy_id */
399 if ((mdio->mode_support & MDIO_SUPPORTS_C45) &&
400 mdio_phy_id_is_c45(mii_data->phy_id)) {
401 prtad = mdio_phy_id_prtad(mii_data->phy_id);
402 devad = mdio_phy_id_devad(mii_data->phy_id);
403 } else if ((mdio->mode_support & MDIO_SUPPORTS_C22) &&
404 mii_data->phy_id < 0x20) {
405 prtad = mii_data->phy_id;
406 devad = MDIO_DEVAD_NONE;
407 addr &= 0x1f;
408 } else if ((mdio->mode_support & MDIO_EMULATE_C22) &&
409 mdio->prtad != MDIO_PRTAD_NONE &&
410 mii_data->phy_id == mdio->prtad) {
411 /* Remap commonly-used MII registers. */
412 prtad = mdio->prtad;
413 switch (addr) {
414 case MII_BMCR:
415 case MII_BMSR:
416 case MII_PHYSID1:
417 case MII_PHYSID2:
418 devad = __ffs(mdio->mmds);
419 break;
420 case MII_ADVERTISE:
421 case MII_LPA:
422 if (!(mdio->mmds & MDIO_DEVS_AN))
423 return -EINVAL;
424 devad = MDIO_MMD_AN;
425 if (addr == MII_ADVERTISE)
426 addr = MDIO_AN_ADVERTISE;
427 else
428 addr = MDIO_AN_LPA;
429 break;
430 default:
431 return -EINVAL;
432 }
433 } else {
434 return -EINVAL;
435 }
436
437 if (cmd == SIOCGMIIREG) {
438 int rc = mdio->mdio_read(mdio->dev, prtad, devad, addr);
439 if (rc < 0)
440 return rc;
441 mii_data->val_out = rc;
442 return 0;
443 } else {
444 return mdio->mdio_write(mdio->dev, prtad, devad, addr,
445 mii_data->val_in);
446 }
447}
448EXPORT_SYMBOL(mdio_mii_ioctl);