1 /****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2006-2008 Solarflare Communications Inc.
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.
10 * Useful functions for working with MDIO clause 45 PHYs
12 #include <linux/types.h>
13 #include <linux/ethtool.h>
14 #include <linux/delay.h>
15 #include "net_driver.h"
18 #include "workarounds.h"
20 unsigned mdio_id_oui(u32 id)
25 /* The bits of the OUI are designated a..x, with a=0 and b variable.
26 * In the id register c is the MSB but the OUI is conventionally
27 * written as bytes h..a, p..i, x..q. Reorder the bits accordingly. */
28 for (i = 0; i < 22; ++i)
29 if (id & (1 << (i + 10)))
35 int mdio_clause45_reset_mmd(struct efx_nic *port, int mmd,
36 int spins, int spintime)
39 int phy_id = port->mii.phy_id;
41 /* Catch callers passing values in the wrong units (or just silly) */
42 EFX_BUG_ON_PARANOID(spins * spintime >= 5000);
44 mdio_clause45_write(port, phy_id, mmd, MDIO_MMDREG_CTRL1,
45 (1 << MDIO_MMDREG_CTRL1_RESET_LBN));
46 /* Wait for the reset bit to clear. */
49 ctrl = mdio_clause45_read(port, phy_id, mmd, MDIO_MMDREG_CTRL1);
52 } while (spins && (ctrl & (1 << MDIO_MMDREG_CTRL1_RESET_LBN)));
54 return spins ? spins : -ETIMEDOUT;
57 static int mdio_clause45_check_mmd(struct efx_nic *efx, int mmd,
61 int phy_id = efx->mii.phy_id;
63 if (LOOPBACK_INTERNAL(efx))
66 if (mmd != MDIO_MMD_AN) {
67 /* Read MMD STATUS2 to check it is responding. */
68 status = mdio_clause45_read(efx, phy_id, mmd,
70 if (((status >> MDIO_MMDREG_STAT2_PRESENT_LBN) &
71 ((1 << MDIO_MMDREG_STAT2_PRESENT_WIDTH) - 1)) !=
72 MDIO_MMDREG_STAT2_PRESENT_VAL) {
73 EFX_ERR(efx, "PHY MMD %d not responding.\n", mmd);
78 /* Read MMD STATUS 1 to check for fault. */
79 status = mdio_clause45_read(efx, phy_id, mmd, MDIO_MMDREG_STAT1);
80 if ((status & (1 << MDIO_MMDREG_STAT1_FAULT_LBN)) != 0) {
82 EFX_ERR(efx, "PHY MMD %d reporting fatal"
83 " fault: status %x\n", mmd, status);
86 EFX_LOG(efx, "PHY MMD %d reporting status"
87 " %x (expected)\n", mmd, status);
93 /* This ought to be ridiculous overkill. We expect it to fail rarely */
94 #define MDIO45_RESET_TIME 1000 /* ms */
95 #define MDIO45_RESET_ITERS 100
97 int mdio_clause45_wait_reset_mmds(struct efx_nic *efx,
98 unsigned int mmd_mask)
100 const int spintime = MDIO45_RESET_TIME / MDIO45_RESET_ITERS;
101 int tries = MDIO45_RESET_ITERS;
112 stat = mdio_clause45_read(efx,
117 EFX_ERR(efx, "failed to read status of"
121 if (stat & (1 << MDIO_MMDREG_CTRL1_RESET_LBN))
122 in_reset |= (1 << mmd);
133 EFX_ERR(efx, "not all MMDs came out of reset in time."
134 " MMDs still in reset: %x\n", in_reset);
140 int mdio_clause45_check_mmds(struct efx_nic *efx,
141 unsigned int mmd_mask, unsigned int fatal_mask)
143 int mmd = 0, probe_mmd, devs0, devs1;
146 /* Historically we have probed the PHYXS to find out what devices are
147 * present,but that doesn't work so well if the PHYXS isn't expected
148 * to exist, if so just find the first item in the list supplied. */
149 probe_mmd = (mmd_mask & MDIO_MMDREG_DEVS_PHYXS) ? MDIO_MMD_PHYXS :
152 /* Check all the expected MMDs are present */
153 devs0 = mdio_clause45_read(efx, efx->mii.phy_id,
154 probe_mmd, MDIO_MMDREG_DEVS0);
155 devs1 = mdio_clause45_read(efx, efx->mii.phy_id,
156 probe_mmd, MDIO_MMDREG_DEVS1);
157 if (devs0 < 0 || devs1 < 0) {
158 EFX_ERR(efx, "failed to read devices present\n");
161 devices = devs0 | (devs1 << 16);
162 if ((devices & mmd_mask) != mmd_mask) {
163 EFX_ERR(efx, "required MMDs not present: got %x, "
164 "wanted %x\n", devices, mmd_mask);
167 EFX_TRACE(efx, "Devices present: %x\n", devices);
169 /* Check all required MMDs are responding and happy. */
172 int fault_fatal = fatal_mask & 1;
173 if (mdio_clause45_check_mmd(efx, mmd, fault_fatal))
176 mmd_mask = mmd_mask >> 1;
177 fatal_mask = fatal_mask >> 1;
184 bool mdio_clause45_links_ok(struct efx_nic *efx, unsigned int mmd_mask)
186 int phy_id = efx->mii.phy_id;
191 /* If the port is in loopback, then we should only consider a subset
193 if (LOOPBACK_INTERNAL(efx))
195 else if (efx->loopback_mode == LOOPBACK_NETWORK)
197 else if (efx_phy_mode_disabled(efx->phy_mode))
199 else if (efx->loopback_mode == LOOPBACK_PHYXS)
200 mmd_mask &= ~(MDIO_MMDREG_DEVS_PHYXS |
201 MDIO_MMDREG_DEVS_PCS |
202 MDIO_MMDREG_DEVS_PMAPMD |
203 MDIO_MMDREG_DEVS_AN);
204 else if (efx->loopback_mode == LOOPBACK_PCS)
205 mmd_mask &= ~(MDIO_MMDREG_DEVS_PCS |
206 MDIO_MMDREG_DEVS_PMAPMD |
207 MDIO_MMDREG_DEVS_AN);
208 else if (efx->loopback_mode == LOOPBACK_PMAPMD)
209 mmd_mask &= ~(MDIO_MMDREG_DEVS_PMAPMD |
210 MDIO_MMDREG_DEVS_AN);
213 /* Use presence of XGMII faults in leui of link state */
214 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PHYXS,
216 return !(reg & (1 << MDIO_PHYXS_STATUS2_RX_FAULT_LBN));
221 /* Double reads because link state is latched, and a
222 * read moves the current state into the register */
223 reg = mdio_clause45_read(efx, phy_id,
224 mmd, MDIO_MMDREG_STAT1);
225 reg = mdio_clause45_read(efx, phy_id,
226 mmd, MDIO_MMDREG_STAT1);
227 ok = ok && (reg & (1 << MDIO_MMDREG_STAT1_LINK_LBN));
229 mmd_mask = (mmd_mask >> 1);
235 void mdio_clause45_transmit_disable(struct efx_nic *efx)
237 mdio_clause45_set_flag(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
238 MDIO_MMDREG_TXDIS, MDIO_MMDREG_TXDIS_GLOBAL_LBN,
239 efx->phy_mode & PHY_MODE_TX_DISABLED);
242 void mdio_clause45_phy_reconfigure(struct efx_nic *efx)
244 int phy_id = efx->mii.phy_id;
246 mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_PMAPMD,
247 MDIO_MMDREG_CTRL1, MDIO_PMAPMD_CTRL1_LBACK_LBN,
248 efx->loopback_mode == LOOPBACK_PMAPMD);
249 mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_PCS,
250 MDIO_MMDREG_CTRL1, MDIO_MMDREG_CTRL1_LBACK_LBN,
251 efx->loopback_mode == LOOPBACK_PCS);
252 mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_PHYXS,
253 MDIO_MMDREG_CTRL1, MDIO_MMDREG_CTRL1_LBACK_LBN,
254 efx->loopback_mode == LOOPBACK_NETWORK);
257 static void mdio_clause45_set_mmd_lpower(struct efx_nic *efx,
260 int phy = efx->mii.phy_id;
261 int stat = mdio_clause45_read(efx, phy, mmd, MDIO_MMDREG_STAT1);
263 EFX_TRACE(efx, "Setting low power mode for MMD %d to %d\n",
266 if (stat & (1 << MDIO_MMDREG_STAT1_LPABLE_LBN)) {
267 mdio_clause45_set_flag(efx, phy, mmd, MDIO_MMDREG_CTRL1,
268 MDIO_MMDREG_CTRL1_LPOWER_LBN, lpower);
272 void mdio_clause45_set_mmds_lpower(struct efx_nic *efx,
273 int low_power, unsigned int mmd_mask)
276 mmd_mask &= ~MDIO_MMDREG_DEVS_AN;
279 mdio_clause45_set_mmd_lpower(efx, low_power, mmd);
280 mmd_mask = (mmd_mask >> 1);
285 static u32 mdio_clause45_get_an(struct efx_nic *efx, u16 addr)
287 int phy_id = efx->mii.phy_id;
291 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN, addr);
292 if (reg & ADVERTISE_10HALF)
293 result |= ADVERTISED_10baseT_Half;
294 if (reg & ADVERTISE_10FULL)
295 result |= ADVERTISED_10baseT_Full;
296 if (reg & ADVERTISE_100HALF)
297 result |= ADVERTISED_100baseT_Half;
298 if (reg & ADVERTISE_100FULL)
299 result |= ADVERTISED_100baseT_Full;
304 * mdio_clause45_get_settings - Read (some of) the PHY settings over MDIO.
306 * @ecmd: Buffer for settings
308 * On return the 'port', 'speed', 'supported' and 'advertising' fields of
309 * ecmd have been filled out.
311 void mdio_clause45_get_settings(struct efx_nic *efx,
312 struct ethtool_cmd *ecmd)
314 mdio_clause45_get_settings_ext(efx, ecmd, 0, 0);
318 * mdio_clause45_get_settings_ext - Read (some of) the PHY settings over MDIO.
320 * @ecmd: Buffer for settings
321 * @xnp: Advertised Extended Next Page state
322 * @xnp_lpa: Link Partner's advertised XNP state
324 * On return the 'port', 'speed', 'supported' and 'advertising' fields of
325 * ecmd have been filled out.
327 void mdio_clause45_get_settings_ext(struct efx_nic *efx,
328 struct ethtool_cmd *ecmd,
329 u32 npage_adv, u32 npage_lpa)
331 int phy_id = efx->mii.phy_id;
334 ecmd->transceiver = XCVR_INTERNAL;
335 ecmd->phy_address = phy_id;
337 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
339 switch (reg & MDIO_PMAPMD_CTRL2_TYPE_MASK) {
340 case MDIO_PMAPMD_CTRL2_10G_BT:
341 case MDIO_PMAPMD_CTRL2_1G_BT:
342 case MDIO_PMAPMD_CTRL2_100_BT:
343 case MDIO_PMAPMD_CTRL2_10_BT:
344 ecmd->port = PORT_TP;
345 ecmd->supported = SUPPORTED_TP;
346 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
348 if (reg & (1 << MDIO_MMDREG_SPEED_10G_LBN))
349 ecmd->supported |= SUPPORTED_10000baseT_Full;
350 if (reg & (1 << MDIO_MMDREG_SPEED_1000M_LBN))
351 ecmd->supported |= (SUPPORTED_1000baseT_Full |
352 SUPPORTED_1000baseT_Half);
353 if (reg & (1 << MDIO_MMDREG_SPEED_100M_LBN))
354 ecmd->supported |= (SUPPORTED_100baseT_Full |
355 SUPPORTED_100baseT_Half);
356 if (reg & (1 << MDIO_MMDREG_SPEED_10M_LBN))
357 ecmd->supported |= (SUPPORTED_10baseT_Full |
358 SUPPORTED_10baseT_Half);
359 ecmd->advertising = ADVERTISED_TP;
362 /* We represent CX4 as fibre in the absence of anything better */
363 case MDIO_PMAPMD_CTRL2_10G_CX4:
364 /* All the other defined modes are flavours of optical */
366 ecmd->port = PORT_FIBRE;
367 ecmd->supported = SUPPORTED_FIBRE;
368 ecmd->advertising = ADVERTISED_FIBRE;
372 if (efx->phy_op->mmds & DEV_PRESENT_BIT(MDIO_MMD_AN)) {
373 ecmd->supported |= SUPPORTED_Autoneg;
374 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN,
376 if (reg & BMCR_ANENABLE) {
377 ecmd->autoneg = AUTONEG_ENABLE;
380 mdio_clause45_get_an(efx, MDIO_AN_ADVERTISE) |
383 ecmd->autoneg = AUTONEG_DISABLE;
385 ecmd->autoneg = AUTONEG_DISABLE;
388 /* If AN is complete, report best common mode,
389 * otherwise report best advertised mode. */
391 if (mdio_clause45_read(efx, phy_id, MDIO_MMD_AN,
393 (1 << MDIO_AN_STATUS_AN_DONE_LBN))
394 modes = (ecmd->advertising &
395 (mdio_clause45_get_an(efx, MDIO_AN_LPA) |
398 modes = ecmd->advertising;
400 if (modes & ADVERTISED_10000baseT_Full) {
401 ecmd->speed = SPEED_10000;
402 ecmd->duplex = DUPLEX_FULL;
403 } else if (modes & (ADVERTISED_1000baseT_Full |
404 ADVERTISED_1000baseT_Half)) {
405 ecmd->speed = SPEED_1000;
406 ecmd->duplex = !!(modes & ADVERTISED_1000baseT_Full);
407 } else if (modes & (ADVERTISED_100baseT_Full |
408 ADVERTISED_100baseT_Half)) {
409 ecmd->speed = SPEED_100;
410 ecmd->duplex = !!(modes & ADVERTISED_100baseT_Full);
412 ecmd->speed = SPEED_10;
413 ecmd->duplex = !!(modes & ADVERTISED_10baseT_Full);
416 /* Report forced settings */
417 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
419 ecmd->speed = (((reg & BMCR_SPEED1000) ? 100 : 1) *
420 ((reg & BMCR_SPEED100) ? 100 : 10));
421 ecmd->duplex = (reg & BMCR_FULLDPLX ||
422 ecmd->speed == SPEED_10000);
427 * mdio_clause45_set_settings - Set (some of) the PHY settings over MDIO.
429 * @ecmd: New settings
431 int mdio_clause45_set_settings(struct efx_nic *efx,
432 struct ethtool_cmd *ecmd)
434 int phy_id = efx->mii.phy_id;
435 struct ethtool_cmd prev;
439 efx->phy_op->get_settings(efx, &prev);
441 if (ecmd->advertising == prev.advertising &&
442 ecmd->speed == prev.speed &&
443 ecmd->duplex == prev.duplex &&
444 ecmd->port == prev.port &&
445 ecmd->autoneg == prev.autoneg)
448 /* We can only change these settings for -T PHYs */
449 if (prev.port != PORT_TP || ecmd->port != PORT_TP)
452 /* Check that PHY supports these settings */
454 required = SUPPORTED_Autoneg;
455 } else if (ecmd->duplex) {
456 switch (ecmd->speed) {
457 case SPEED_10: required = SUPPORTED_10baseT_Full; break;
458 case SPEED_100: required = SUPPORTED_100baseT_Full; break;
459 default: return -EINVAL;
462 switch (ecmd->speed) {
463 case SPEED_10: required = SUPPORTED_10baseT_Half; break;
464 case SPEED_100: required = SUPPORTED_100baseT_Half; break;
465 default: return -EINVAL;
468 required |= ecmd->advertising;
469 if (required & ~prev.supported)
473 bool xnp = (ecmd->advertising & ADVERTISED_10000baseT_Full
474 || EFX_WORKAROUND_13204(efx));
476 /* Set up the base page */
477 reg = ADVERTISE_CSMA;
478 if (ecmd->advertising & ADVERTISED_10baseT_Half)
479 reg |= ADVERTISE_10HALF;
480 if (ecmd->advertising & ADVERTISED_10baseT_Full)
481 reg |= ADVERTISE_10FULL;
482 if (ecmd->advertising & ADVERTISED_100baseT_Half)
483 reg |= ADVERTISE_100HALF;
484 if (ecmd->advertising & ADVERTISED_100baseT_Full)
485 reg |= ADVERTISE_100FULL;
487 reg |= ADVERTISE_RESV;
488 else if (ecmd->advertising & (ADVERTISED_1000baseT_Half |
489 ADVERTISED_1000baseT_Full))
490 reg |= ADVERTISE_NPAGE;
491 reg |= efx_fc_advertise(efx->wanted_fc);
492 mdio_clause45_write(efx, phy_id, MDIO_MMD_AN,
493 MDIO_AN_ADVERTISE, reg);
495 /* Set up the (extended) next page if necessary */
496 if (efx->phy_op->set_npage_adv)
497 efx->phy_op->set_npage_adv(efx, ecmd->advertising);
499 /* Enable and restart AN */
500 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN,
502 reg |= BMCR_ANENABLE;
503 if (!(EFX_WORKAROUND_15195(efx) &&
504 LOOPBACK_MASK(efx) & efx->phy_op->loopbacks))
505 reg |= BMCR_ANRESTART;
507 reg |= 1 << MDIO_AN_CTRL_XNP_LBN;
509 reg &= ~(1 << MDIO_AN_CTRL_XNP_LBN);
510 mdio_clause45_write(efx, phy_id, MDIO_MMD_AN,
511 MDIO_MMDREG_CTRL1, reg);
514 mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_AN,
516 __ffs(BMCR_ANENABLE), false);
518 /* Set the basic control bits */
519 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
521 reg &= ~(BMCR_SPEED1000 | BMCR_SPEED100 | BMCR_FULLDPLX |
523 if (ecmd->speed == SPEED_100)
524 reg |= BMCR_SPEED100;
526 reg |= BMCR_FULLDPLX;
527 mdio_clause45_write(efx, phy_id, MDIO_MMD_PMAPMD,
528 MDIO_MMDREG_CTRL1, reg);
534 void mdio_clause45_set_pause(struct efx_nic *efx)
536 int phy_id = efx->mii.phy_id;
539 if (efx->phy_op->mmds & DEV_PRESENT_BIT(MDIO_MMD_AN)) {
540 /* Set pause capability advertising */
541 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN,
543 reg &= ~(ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
544 reg |= efx_fc_advertise(efx->wanted_fc);
545 mdio_clause45_write(efx, phy_id, MDIO_MMD_AN,
546 MDIO_AN_ADVERTISE, reg);
548 /* Restart auto-negotiation */
549 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN,
551 if (reg & BMCR_ANENABLE) {
552 reg |= BMCR_ANRESTART;
553 mdio_clause45_write(efx, phy_id, MDIO_MMD_AN,
554 MDIO_MMDREG_CTRL1, reg);
559 enum efx_fc_type mdio_clause45_get_pause(struct efx_nic *efx)
561 int phy_id = efx->mii.phy_id;
564 if (!(efx->phy_op->mmds & DEV_PRESENT_BIT(MDIO_MMD_AN)))
565 return efx->wanted_fc;
566 lpa = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN, MDIO_AN_LPA);
567 return efx_fc_resolve(efx->wanted_fc, lpa);
570 void mdio_clause45_set_flag(struct efx_nic *efx, u8 prt, u8 dev,
571 u16 addr, int bit, bool sense)
573 int old_val = mdio_clause45_read(efx, prt, dev, addr);
577 new_val = old_val | (1 << bit);
579 new_val = old_val & ~(1 << bit);
580 if (old_val != new_val)
581 mdio_clause45_write(efx, prt, dev, addr, new_val);