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 int mdio_clause45_reset_mmd(struct efx_nic *port, int mmd,
21 int spins, int spintime)
24 int phy_id = port->mii.phy_id;
26 /* Catch callers passing values in the wrong units (or just silly) */
27 EFX_BUG_ON_PARANOID(spins * spintime >= 5000);
29 mdio_clause45_write(port, phy_id, mmd, MDIO_MMDREG_CTRL1,
30 (1 << MDIO_MMDREG_CTRL1_RESET_LBN));
31 /* Wait for the reset bit to clear. */
34 ctrl = mdio_clause45_read(port, phy_id, mmd, MDIO_MMDREG_CTRL1);
37 } while (spins && (ctrl & (1 << MDIO_MMDREG_CTRL1_RESET_LBN)));
39 return spins ? spins : -ETIMEDOUT;
42 static int mdio_clause45_check_mmd(struct efx_nic *efx, int mmd,
46 int phy_id = efx->mii.phy_id;
48 if (LOOPBACK_INTERNAL(efx))
51 if (mmd != MDIO_MMD_AN) {
52 /* Read MMD STATUS2 to check it is responding. */
53 status = mdio_clause45_read(efx, phy_id, mmd,
55 if (((status >> MDIO_MMDREG_STAT2_PRESENT_LBN) &
56 ((1 << MDIO_MMDREG_STAT2_PRESENT_WIDTH) - 1)) !=
57 MDIO_MMDREG_STAT2_PRESENT_VAL) {
58 EFX_ERR(efx, "PHY MMD %d not responding.\n", mmd);
63 /* Read MMD STATUS 1 to check for fault. */
64 status = mdio_clause45_read(efx, phy_id, mmd, MDIO_MMDREG_STAT1);
65 if ((status & (1 << MDIO_MMDREG_STAT1_FAULT_LBN)) != 0) {
67 EFX_ERR(efx, "PHY MMD %d reporting fatal"
68 " fault: status %x\n", mmd, status);
71 EFX_LOG(efx, "PHY MMD %d reporting status"
72 " %x (expected)\n", mmd, status);
78 /* This ought to be ridiculous overkill. We expect it to fail rarely */
79 #define MDIO45_RESET_TIME 1000 /* ms */
80 #define MDIO45_RESET_ITERS 100
82 int mdio_clause45_wait_reset_mmds(struct efx_nic *efx,
83 unsigned int mmd_mask)
85 const int spintime = MDIO45_RESET_TIME / MDIO45_RESET_ITERS;
86 int tries = MDIO45_RESET_ITERS;
97 stat = mdio_clause45_read(efx,
102 EFX_ERR(efx, "failed to read status of"
106 if (stat & (1 << MDIO_MMDREG_CTRL1_RESET_LBN))
107 in_reset |= (1 << mmd);
118 EFX_ERR(efx, "not all MMDs came out of reset in time."
119 " MMDs still in reset: %x\n", in_reset);
125 int mdio_clause45_check_mmds(struct efx_nic *efx,
126 unsigned int mmd_mask, unsigned int fatal_mask)
129 int mmd = 0, probe_mmd;
131 /* Historically we have probed the PHYXS to find out what devices are
132 * present,but that doesn't work so well if the PHYXS isn't expected
133 * to exist, if so just find the first item in the list supplied. */
134 probe_mmd = (mmd_mask & MDIO_MMDREG_DEVS_PHYXS) ? MDIO_MMD_PHYXS :
136 devices = (mdio_clause45_read(efx, efx->mii.phy_id,
137 probe_mmd, MDIO_MMDREG_DEVS0) |
138 mdio_clause45_read(efx, efx->mii.phy_id,
139 probe_mmd, MDIO_MMDREG_DEVS1) << 16);
141 /* Check all the expected MMDs are present */
143 EFX_ERR(efx, "failed to read devices present\n");
146 if ((devices & mmd_mask) != mmd_mask) {
147 EFX_ERR(efx, "required MMDs not present: got %x, "
148 "wanted %x\n", devices, mmd_mask);
151 EFX_TRACE(efx, "Devices present: %x\n", devices);
153 /* Check all required MMDs are responding and happy. */
156 int fault_fatal = fatal_mask & 1;
157 if (mdio_clause45_check_mmd(efx, mmd, fault_fatal))
160 mmd_mask = mmd_mask >> 1;
161 fatal_mask = fatal_mask >> 1;
168 bool mdio_clause45_links_ok(struct efx_nic *efx, unsigned int mmd_mask)
170 int phy_id = efx->mii.phy_id;
175 /* If the port is in loopback, then we should only consider a subset
177 if (LOOPBACK_INTERNAL(efx))
179 else if (efx->loopback_mode == LOOPBACK_NETWORK)
181 else if (efx_phy_mode_disabled(efx->phy_mode))
183 else if (efx->loopback_mode == LOOPBACK_PHYXS) {
184 mmd_mask &= ~(MDIO_MMDREG_DEVS_PHYXS |
185 MDIO_MMDREG_DEVS_PCS |
186 MDIO_MMDREG_DEVS_PMAPMD |
187 MDIO_MMDREG_DEVS_AN);
189 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PHYXS,
191 return !(reg & (1 << MDIO_PHYXS_STATUS2_RX_FAULT_LBN));
193 } else if (efx->loopback_mode == LOOPBACK_PCS)
194 mmd_mask &= ~(MDIO_MMDREG_DEVS_PCS |
195 MDIO_MMDREG_DEVS_PMAPMD |
196 MDIO_MMDREG_DEVS_AN);
197 else if (efx->loopback_mode == LOOPBACK_PMAPMD)
198 mmd_mask &= ~(MDIO_MMDREG_DEVS_PMAPMD |
199 MDIO_MMDREG_DEVS_AN);
203 /* Double reads because link state is latched, and a
204 * read moves the current state into the register */
205 reg = mdio_clause45_read(efx, phy_id,
206 mmd, MDIO_MMDREG_STAT1);
207 reg = mdio_clause45_read(efx, phy_id,
208 mmd, MDIO_MMDREG_STAT1);
209 ok = ok && (reg & (1 << MDIO_MMDREG_STAT1_LINK_LBN));
211 mmd_mask = (mmd_mask >> 1);
217 void mdio_clause45_transmit_disable(struct efx_nic *efx)
219 mdio_clause45_set_flag(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
220 MDIO_MMDREG_TXDIS, MDIO_MMDREG_TXDIS_GLOBAL_LBN,
221 efx->phy_mode & PHY_MODE_TX_DISABLED);
224 void mdio_clause45_phy_reconfigure(struct efx_nic *efx)
226 int phy_id = efx->mii.phy_id;
228 mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_PMAPMD,
229 MDIO_MMDREG_CTRL1, MDIO_PMAPMD_CTRL1_LBACK_LBN,
230 efx->loopback_mode == LOOPBACK_PMAPMD);
231 mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_PCS,
232 MDIO_MMDREG_CTRL1, MDIO_MMDREG_CTRL1_LBACK_LBN,
233 efx->loopback_mode == LOOPBACK_PCS);
234 mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_PHYXS,
235 MDIO_MMDREG_CTRL1, MDIO_MMDREG_CTRL1_LBACK_LBN,
236 efx->loopback_mode == LOOPBACK_NETWORK);
239 static void mdio_clause45_set_mmd_lpower(struct efx_nic *efx,
242 int phy = efx->mii.phy_id;
243 int stat = mdio_clause45_read(efx, phy, mmd, MDIO_MMDREG_STAT1);
245 EFX_TRACE(efx, "Setting low power mode for MMD %d to %d\n",
248 if (stat & (1 << MDIO_MMDREG_STAT1_LPABLE_LBN)) {
249 mdio_clause45_set_flag(efx, phy, mmd, MDIO_MMDREG_CTRL1,
250 MDIO_MMDREG_CTRL1_LPOWER_LBN, lpower);
254 void mdio_clause45_set_mmds_lpower(struct efx_nic *efx,
255 int low_power, unsigned int mmd_mask)
258 mmd_mask &= ~MDIO_MMDREG_DEVS_AN;
261 mdio_clause45_set_mmd_lpower(efx, low_power, mmd);
262 mmd_mask = (mmd_mask >> 1);
267 static u32 mdio_clause45_get_an(struct efx_nic *efx, u16 addr, u32 xnp)
269 int phy_id = efx->mii.phy_id;
273 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN, addr);
274 if (reg & ADVERTISE_10HALF)
275 result |= ADVERTISED_10baseT_Half;
276 if (reg & ADVERTISE_10FULL)
277 result |= ADVERTISED_10baseT_Full;
278 if (reg & ADVERTISE_100HALF)
279 result |= ADVERTISED_100baseT_Half;
280 if (reg & ADVERTISE_100FULL)
281 result |= ADVERTISED_100baseT_Full;
289 * mdio_clause45_get_settings - Read (some of) the PHY settings over MDIO.
291 * @ecmd: Buffer for settings
293 * On return the 'port', 'speed', 'supported' and 'advertising' fields of
294 * ecmd have been filled out.
296 void mdio_clause45_get_settings(struct efx_nic *efx,
297 struct ethtool_cmd *ecmd)
299 mdio_clause45_get_settings_ext(efx, ecmd, 0, 0);
303 * mdio_clause45_get_settings_ext - Read (some of) the PHY settings over MDIO.
305 * @ecmd: Buffer for settings
306 * @xnp: Advertised Extended Next Page state
307 * @xnp_lpa: Link Partner's advertised XNP state
309 * On return the 'port', 'speed', 'supported' and 'advertising' fields of
310 * ecmd have been filled out.
312 void mdio_clause45_get_settings_ext(struct efx_nic *efx,
313 struct ethtool_cmd *ecmd,
314 u32 xnp, u32 xnp_lpa)
316 int phy_id = efx->mii.phy_id;
319 ecmd->transceiver = XCVR_INTERNAL;
320 ecmd->phy_address = phy_id;
322 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
324 switch (reg & MDIO_PMAPMD_CTRL2_TYPE_MASK) {
325 case MDIO_PMAPMD_CTRL2_10G_BT:
326 case MDIO_PMAPMD_CTRL2_1G_BT:
327 case MDIO_PMAPMD_CTRL2_100_BT:
328 case MDIO_PMAPMD_CTRL2_10_BT:
329 ecmd->port = PORT_TP;
330 ecmd->supported = SUPPORTED_TP;
331 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
333 if (reg & (1 << MDIO_MMDREG_SPEED_10G_LBN))
334 ecmd->supported |= SUPPORTED_10000baseT_Full;
335 if (reg & (1 << MDIO_MMDREG_SPEED_1000M_LBN))
336 ecmd->supported |= (SUPPORTED_1000baseT_Full |
337 SUPPORTED_1000baseT_Half);
338 if (reg & (1 << MDIO_MMDREG_SPEED_100M_LBN))
339 ecmd->supported |= (SUPPORTED_100baseT_Full |
340 SUPPORTED_100baseT_Half);
341 if (reg & (1 << MDIO_MMDREG_SPEED_10M_LBN))
342 ecmd->supported |= (SUPPORTED_10baseT_Full |
343 SUPPORTED_10baseT_Half);
344 ecmd->advertising = ADVERTISED_TP;
347 /* We represent CX4 as fibre in the absence of anything better */
348 case MDIO_PMAPMD_CTRL2_10G_CX4:
349 /* All the other defined modes are flavours of optical */
351 ecmd->port = PORT_FIBRE;
352 ecmd->supported = SUPPORTED_FIBRE;
353 ecmd->advertising = ADVERTISED_FIBRE;
357 if (efx->phy_op->mmds & DEV_PRESENT_BIT(MDIO_MMD_AN)) {
358 ecmd->supported |= SUPPORTED_Autoneg;
359 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN,
361 if (reg & BMCR_ANENABLE) {
362 ecmd->autoneg = AUTONEG_ENABLE;
365 mdio_clause45_get_an(efx,
366 MDIO_AN_ADVERTISE, xnp);
368 ecmd->autoneg = AUTONEG_DISABLE;
370 ecmd->autoneg = AUTONEG_DISABLE;
373 /* If AN is complete, report best common mode,
374 * otherwise report best advertised mode. */
375 u32 common = ecmd->advertising;
376 if (mdio_clause45_read(efx, phy_id, MDIO_MMD_AN,
378 (1 << MDIO_AN_STATUS_AN_DONE_LBN)) {
379 common &= mdio_clause45_get_an(efx, MDIO_AN_LPA,
382 if (common & ADVERTISED_10000baseT_Full) {
383 ecmd->speed = SPEED_10000;
384 ecmd->duplex = DUPLEX_FULL;
385 } else if (common & (ADVERTISED_1000baseT_Full |
386 ADVERTISED_1000baseT_Half)) {
387 ecmd->speed = SPEED_1000;
388 ecmd->duplex = !!(common & ADVERTISED_1000baseT_Full);
389 } else if (common & (ADVERTISED_100baseT_Full |
390 ADVERTISED_100baseT_Half)) {
391 ecmd->speed = SPEED_100;
392 ecmd->duplex = !!(common & ADVERTISED_100baseT_Full);
394 ecmd->speed = SPEED_10;
395 ecmd->duplex = !!(common & ADVERTISED_10baseT_Full);
398 /* Report forced settings */
399 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
401 ecmd->speed = (((reg & BMCR_SPEED1000) ? 100 : 1) *
402 ((reg & BMCR_SPEED100) ? 100 : 10));
403 ecmd->duplex = (reg & BMCR_FULLDPLX ||
404 ecmd->speed == SPEED_10000);
409 * mdio_clause45_set_settings - Set (some of) the PHY settings over MDIO.
411 * @ecmd: New settings
413 int mdio_clause45_set_settings(struct efx_nic *efx,
414 struct ethtool_cmd *ecmd)
416 int phy_id = efx->mii.phy_id;
417 struct ethtool_cmd prev;
421 efx->phy_op->get_settings(efx, &prev);
423 if (ecmd->advertising == prev.advertising &&
424 ecmd->speed == prev.speed &&
425 ecmd->duplex == prev.duplex &&
426 ecmd->port == prev.port &&
427 ecmd->autoneg == prev.autoneg)
430 /* We can only change these settings for -T PHYs */
431 if (prev.port != PORT_TP || ecmd->port != PORT_TP)
434 /* Check that PHY supports these settings and work out the
435 * basic control bits */
437 switch (ecmd->speed) {
439 ctrl1_bits = BMCR_FULLDPLX;
440 required = SUPPORTED_10baseT_Full;
443 ctrl1_bits = BMCR_SPEED100 | BMCR_FULLDPLX;
444 required = SUPPORTED_100baseT_Full;
447 ctrl1_bits = BMCR_SPEED1000 | BMCR_FULLDPLX;
448 required = SUPPORTED_1000baseT_Full;
451 ctrl1_bits = (BMCR_SPEED1000 | BMCR_SPEED100 |
453 required = SUPPORTED_10000baseT_Full;
459 switch (ecmd->speed) {
462 required = SUPPORTED_10baseT_Half;
465 ctrl1_bits = BMCR_SPEED100;
466 required = SUPPORTED_100baseT_Half;
469 ctrl1_bits = BMCR_SPEED1000;
470 required = SUPPORTED_1000baseT_Half;
477 required |= SUPPORTED_Autoneg;
478 required |= ecmd->advertising;
479 if (required & ~prev.supported)
482 /* Set the basic control bits */
483 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
485 reg &= ~(BMCR_SPEED1000 | BMCR_SPEED100 | BMCR_FULLDPLX | 0x003c);
487 mdio_clause45_write(efx, phy_id, MDIO_MMD_PMAPMD, MDIO_MMDREG_CTRL1,
490 /* Set the AN registers */
491 if (ecmd->autoneg != prev.autoneg ||
492 ecmd->advertising != prev.advertising) {
495 if (efx->phy_op->set_xnp_advertise)
496 xnp = efx->phy_op->set_xnp_advertise(efx,
501 if (ecmd->advertising & ADVERTISED_10baseT_Half)
502 reg |= ADVERTISE_10HALF;
503 if (ecmd->advertising & ADVERTISED_10baseT_Full)
504 reg |= ADVERTISE_10FULL;
505 if (ecmd->advertising & ADVERTISED_100baseT_Half)
506 reg |= ADVERTISE_100HALF;
507 if (ecmd->advertising & ADVERTISED_100baseT_Full)
508 reg |= ADVERTISE_100FULL;
510 reg |= ADVERTISE_RESV;
511 mdio_clause45_write(efx, phy_id, MDIO_MMD_AN,
512 MDIO_AN_ADVERTISE, reg);
515 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN,
518 reg |= BMCR_ANENABLE | BMCR_ANRESTART;
520 reg &= ~BMCR_ANENABLE;
521 if (EFX_WORKAROUND_15195(efx)
522 && LOOPBACK_MASK(efx) & efx->phy_op->loopbacks)
523 reg &= ~BMCR_ANRESTART;
525 reg |= 1 << MDIO_AN_CTRL_XNP_LBN;
527 reg &= ~(1 << MDIO_AN_CTRL_XNP_LBN);
528 mdio_clause45_write(efx, phy_id, MDIO_MMD_AN,
529 MDIO_MMDREG_CTRL1, reg);
535 void mdio_clause45_set_pause(struct efx_nic *efx)
537 int phy_id = efx->mii.phy_id;
540 if (efx->phy_op->mmds & DEV_PRESENT_BIT(MDIO_MMD_AN)) {
541 /* Set pause capability advertising */
542 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN,
544 reg &= ~(ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
545 reg |= efx_fc_advertise(efx->wanted_fc);
546 mdio_clause45_write(efx, phy_id, MDIO_MMD_AN,
547 MDIO_AN_ADVERTISE, reg);
549 /* Restart auto-negotiation */
550 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN,
552 if (reg & BMCR_ANENABLE) {
553 reg |= BMCR_ANRESTART;
554 mdio_clause45_write(efx, phy_id, MDIO_MMD_AN,
555 MDIO_MMDREG_CTRL1, reg);
560 enum efx_fc_type mdio_clause45_get_pause(struct efx_nic *efx)
562 int phy_id = efx->mii.phy_id;
565 if (!(efx->phy_op->mmds & DEV_PRESENT_BIT(MDIO_MMD_AN)))
566 return efx->wanted_fc;
567 lpa = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN, MDIO_AN_LPA);
568 return efx_fc_resolve(efx->wanted_fc, lpa);
571 void mdio_clause45_set_flag(struct efx_nic *efx, u8 prt, u8 dev,
572 u16 addr, int bit, bool sense)
574 int old_val = mdio_clause45_read(efx, prt, dev, addr);
578 new_val = old_val | (1 << bit);
580 new_val = old_val & ~(1 << bit);
581 if (old_val != new_val)
582 mdio_clause45_write(efx, prt, dev, addr, new_val);