Lines Matching +full:0 +full:- +full:mdio

1 // SPDX-License-Identifier: GPL-2.0-only
3 * mdio.c: Generic support for MDIO-compatible transceivers
4 * Copyright 2006-2009 Solarflare Communications Inc.
11 #include <linux/mdio.h>
14 MODULE_DESCRIPTION("Generic support for MDIO-compatible transceivers");
15 MODULE_AUTHOR("Copyright 2006-2009 Solarflare Communications Inc.");
19 * mdio45_probe - probe for an MDIO (clause 45) device
20 * @mdio: MDIO interface
23 * This sets @prtad and @mmds in the MDIO interface if successful.
24 * Returns 0 on success, negative on error.
26 int mdio45_probe(struct mdio_if_info *mdio, int prtad) in mdio45_probe() argument
34 stat2 = mdio->mdio_read(mdio->dev, prtad, mmd, MDIO_STAT2); in mdio45_probe()
35 if (stat2 < 0 || in mdio45_probe()
40 devs1 = mdio->mdio_read(mdio->dev, prtad, mmd, MDIO_DEVS1); in mdio45_probe()
41 devs2 = mdio->mdio_read(mdio->dev, prtad, mmd, MDIO_DEVS2); in mdio45_probe()
42 if (devs1 < 0 || devs2 < 0) in mdio45_probe()
45 mdio->prtad = prtad; in mdio45_probe()
46 mdio->mmds = devs1 | (devs2 << 16); in mdio45_probe()
47 return 0; in mdio45_probe()
50 return -ENODEV; in mdio45_probe()
55 * mdio_set_flag - set or clear flag in an MDIO register
56 * @mdio: MDIO interface
64 * already has the proper value. Returns 0 on success, negative on error.
66 int mdio_set_flag(const struct mdio_if_info *mdio, in mdio_set_flag() argument
70 int old_val = mdio->mdio_read(mdio->dev, prtad, devad, addr); in mdio_set_flag()
73 if (old_val < 0) in mdio_set_flag()
80 return 0; in mdio_set_flag()
81 return mdio->mdio_write(mdio->dev, prtad, devad, addr, new_val); in mdio_set_flag()
86 * mdio45_links_ok - is link status up/OK
87 * @mdio: MDIO interface
90 * Returns 1 if the PHY reports link status up/OK, 0 otherwise.
91 * @mmd_mask is normally @mdio->mmds, but if loopback is enabled
94 int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmd_mask) in mdio45_links_ok() argument
100 reg = mdio->mdio_read(mdio->dev, mdio->prtad, in mdio45_links_ok()
102 return reg >= 0 && !(reg & MDIO_STAT2_RXFAULT); in mdio45_links_ok()
105 for (devad = 0; mmd_mask; devad++) { in mdio45_links_ok()
110 mdio->mdio_read(mdio->dev, mdio->prtad, in mdio45_links_ok()
114 mdio->mdio_read(mdio->dev, mdio->prtad, in mdio45_links_ok()
118 reg = mdio->mdio_read(mdio->dev, mdio->prtad, in mdio45_links_ok()
120 if (reg < 0 || in mdio45_links_ok()
132 * mdio45_nway_restart - restart auto-negotiation for this interface
133 * @mdio: MDIO interface
135 * Returns 0 on success, negative on error.
137 int mdio45_nway_restart(const struct mdio_if_info *mdio) in mdio45_nway_restart() argument
139 if (!(mdio->mmds & MDIO_DEVS_AN)) in mdio45_nway_restart()
140 return -EOPNOTSUPP; in mdio45_nway_restart()
142 mdio_set_flag(mdio, mdio->prtad, MDIO_MMD_AN, MDIO_CTRL1, in mdio45_nway_restart()
144 return 0; in mdio45_nway_restart()
148 static u32 mdio45_get_an(const struct mdio_if_info *mdio, u16 addr) in mdio45_get_an() argument
150 u32 result = 0; in mdio45_get_an()
153 reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_AN, addr); in mdio45_get_an()
170 * mdio45_ethtool_gset_npage - get settings for ETHTOOL_GSET
171 * @mdio: MDIO interface
179 * Since the CSRs for auto-negotiation using next pages are not fully
183 void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio, in mdio45_ethtool_gset_npage() argument
193 ecmd->transceiver = XCVR_INTERNAL; in mdio45_ethtool_gset_npage()
194 ecmd->phy_address = mdio->prtad; in mdio45_ethtool_gset_npage()
195 ecmd->mdio_support = in mdio45_ethtool_gset_npage()
196 mdio->mode_support & (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22); in mdio45_ethtool_gset_npage()
198 reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD, in mdio45_ethtool_gset_npage()
205 ecmd->port = PORT_TP; in mdio45_ethtool_gset_npage()
206 ecmd->supported = SUPPORTED_TP; in mdio45_ethtool_gset_npage()
207 reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD, in mdio45_ethtool_gset_npage()
210 ecmd->supported |= SUPPORTED_10000baseT_Full; in mdio45_ethtool_gset_npage()
212 ecmd->supported |= (SUPPORTED_1000baseT_Full | in mdio45_ethtool_gset_npage()
215 ecmd->supported |= (SUPPORTED_100baseT_Full | in mdio45_ethtool_gset_npage()
218 ecmd->supported |= (SUPPORTED_10baseT_Full | in mdio45_ethtool_gset_npage()
220 ecmd->advertising = ADVERTISED_TP; in mdio45_ethtool_gset_npage()
224 ecmd->port = PORT_OTHER; in mdio45_ethtool_gset_npage()
225 ecmd->supported = 0; in mdio45_ethtool_gset_npage()
226 ecmd->advertising = 0; in mdio45_ethtool_gset_npage()
232 ecmd->port = PORT_OTHER; in mdio45_ethtool_gset_npage()
233 ecmd->supported = SUPPORTED_Backplane; in mdio45_ethtool_gset_npage()
234 reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD, in mdio45_ethtool_gset_npage()
237 ecmd->supported |= SUPPORTED_10000baseKX4_Full; in mdio45_ethtool_gset_npage()
239 ecmd->supported |= SUPPORTED_10000baseKR_Full; in mdio45_ethtool_gset_npage()
241 ecmd->supported |= SUPPORTED_1000baseKX_Full; in mdio45_ethtool_gset_npage()
242 reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD, in mdio45_ethtool_gset_npage()
245 ecmd->supported |= SUPPORTED_10000baseR_FEC; in mdio45_ethtool_gset_npage()
246 ecmd->advertising = ADVERTISED_Backplane; in mdio45_ethtool_gset_npage()
251 ecmd->port = PORT_FIBRE; in mdio45_ethtool_gset_npage()
252 ecmd->supported = SUPPORTED_FIBRE; in mdio45_ethtool_gset_npage()
253 ecmd->advertising = ADVERTISED_FIBRE; in mdio45_ethtool_gset_npage()
257 if (mdio->mmds & MDIO_DEVS_AN) { in mdio45_ethtool_gset_npage()
258 ecmd->supported |= SUPPORTED_Autoneg; in mdio45_ethtool_gset_npage()
259 reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_AN, in mdio45_ethtool_gset_npage()
262 ecmd->autoneg = AUTONEG_ENABLE; in mdio45_ethtool_gset_npage()
263 ecmd->advertising |= in mdio45_ethtool_gset_npage()
265 mdio45_get_an(mdio, MDIO_AN_ADVERTISE) | in mdio45_ethtool_gset_npage()
268 ecmd->autoneg = AUTONEG_DISABLE; in mdio45_ethtool_gset_npage()
271 ecmd->autoneg = AUTONEG_DISABLE; in mdio45_ethtool_gset_npage()
274 if (ecmd->autoneg) { in mdio45_ethtool_gset_npage()
275 u32 modes = 0; in mdio45_ethtool_gset_npage()
276 int an_stat = mdio->mdio_read(mdio->dev, mdio->prtad, in mdio45_ethtool_gset_npage()
282 ecmd->lp_advertising = in mdio45_ethtool_gset_npage()
283 mdio45_get_an(mdio, MDIO_AN_LPA) | npage_lpa; in mdio45_ethtool_gset_npage()
285 ecmd->lp_advertising |= ADVERTISED_Autoneg; in mdio45_ethtool_gset_npage()
286 modes = ecmd->advertising & ecmd->lp_advertising; in mdio45_ethtool_gset_npage()
288 if ((modes & ~ADVERTISED_Autoneg) == 0) in mdio45_ethtool_gset_npage()
289 modes = ecmd->advertising; in mdio45_ethtool_gset_npage()
295 ecmd->duplex = DUPLEX_FULL; in mdio45_ethtool_gset_npage()
300 ecmd->duplex = !(modes & ADVERTISED_1000baseT_Half); in mdio45_ethtool_gset_npage()
304 ecmd->duplex = !!(modes & ADVERTISED_100baseT_Full); in mdio45_ethtool_gset_npage()
307 ecmd->duplex = !!(modes & ADVERTISED_10baseT_Full); in mdio45_ethtool_gset_npage()
311 reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD, in mdio45_ethtool_gset_npage()
315 ecmd->duplex = (reg & MDIO_CTRL1_FULLDPLX || in mdio45_ethtool_gset_npage()
321 /* 10GBASE-T MDI/MDI-X */ in mdio45_ethtool_gset_npage()
322 if (ecmd->port == PORT_TP in mdio45_ethtool_gset_npage()
324 switch (mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD, in mdio45_ethtool_gset_npage()
327 ecmd->eth_tp_mdix = ETH_TP_MDI; in mdio45_ethtool_gset_npage()
329 case 0: in mdio45_ethtool_gset_npage()
330 ecmd->eth_tp_mdix = ETH_TP_MDI_X; in mdio45_ethtool_gset_npage()
334 ecmd->eth_tp_mdix = ETH_TP_MDI_INVALID; in mdio45_ethtool_gset_npage()
342 * mdio45_ethtool_ksettings_get_npage - get settings for ETHTOOL_GLINKSETTINGS
343 * @mdio: MDIO interface
351 * Since the CSRs for auto-negotiation using next pages are not fully
355 void mdio45_ethtool_ksettings_get_npage(const struct mdio_if_info *mdio, in mdio45_ethtool_ksettings_get_npage() argument
360 u32 speed, supported = 0, advertising = 0, lp_advertising = 0; in mdio45_ethtool_ksettings_get_npage()
365 cmd->base.phy_address = mdio->prtad; in mdio45_ethtool_ksettings_get_npage()
366 cmd->base.mdio_support = in mdio45_ethtool_ksettings_get_npage()
367 mdio->mode_support & (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22); in mdio45_ethtool_ksettings_get_npage()
369 reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD, in mdio45_ethtool_ksettings_get_npage()
376 cmd->base.port = PORT_TP; in mdio45_ethtool_ksettings_get_npage()
378 reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD, in mdio45_ethtool_ksettings_get_npage()
395 cmd->base.port = PORT_OTHER; in mdio45_ethtool_ksettings_get_npage()
396 supported = 0; in mdio45_ethtool_ksettings_get_npage()
397 advertising = 0; in mdio45_ethtool_ksettings_get_npage()
403 cmd->base.port = PORT_OTHER; in mdio45_ethtool_ksettings_get_npage()
405 reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD, in mdio45_ethtool_ksettings_get_npage()
413 reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD, in mdio45_ethtool_ksettings_get_npage()
422 cmd->base.port = PORT_FIBRE; in mdio45_ethtool_ksettings_get_npage()
428 if (mdio->mmds & MDIO_DEVS_AN) { in mdio45_ethtool_ksettings_get_npage()
430 reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_AN, in mdio45_ethtool_ksettings_get_npage()
433 cmd->base.autoneg = AUTONEG_ENABLE; in mdio45_ethtool_ksettings_get_npage()
436 mdio45_get_an(mdio, MDIO_AN_ADVERTISE) | in mdio45_ethtool_ksettings_get_npage()
439 cmd->base.autoneg = AUTONEG_DISABLE; in mdio45_ethtool_ksettings_get_npage()
442 cmd->base.autoneg = AUTONEG_DISABLE; in mdio45_ethtool_ksettings_get_npage()
445 if (cmd->base.autoneg) { in mdio45_ethtool_ksettings_get_npage()
446 u32 modes = 0; in mdio45_ethtool_ksettings_get_npage()
447 int an_stat = mdio->mdio_read(mdio->dev, mdio->prtad, in mdio45_ethtool_ksettings_get_npage()
455 mdio45_get_an(mdio, MDIO_AN_LPA) | npage_lpa; in mdio45_ethtool_ksettings_get_npage()
460 if ((modes & ~ADVERTISED_Autoneg) == 0) in mdio45_ethtool_ksettings_get_npage()
467 cmd->base.duplex = DUPLEX_FULL; in mdio45_ethtool_ksettings_get_npage()
472 cmd->base.duplex = !(modes & ADVERTISED_1000baseT_Half); in mdio45_ethtool_ksettings_get_npage()
476 cmd->base.duplex = !!(modes & ADVERTISED_100baseT_Full); in mdio45_ethtool_ksettings_get_npage()
479 cmd->base.duplex = !!(modes & ADVERTISED_10baseT_Full); in mdio45_ethtool_ksettings_get_npage()
483 reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD, in mdio45_ethtool_ksettings_get_npage()
487 cmd->base.duplex = (reg & MDIO_CTRL1_FULLDPLX || in mdio45_ethtool_ksettings_get_npage()
491 cmd->base.speed = speed; in mdio45_ethtool_ksettings_get_npage()
493 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported, in mdio45_ethtool_ksettings_get_npage()
495 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising, in mdio45_ethtool_ksettings_get_npage()
497 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising, in mdio45_ethtool_ksettings_get_npage()
500 /* 10GBASE-T MDI/MDI-X */ in mdio45_ethtool_ksettings_get_npage()
501 if (cmd->base.port == PORT_TP && (cmd->base.speed == SPEED_10000)) { in mdio45_ethtool_ksettings_get_npage()
502 switch (mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD, in mdio45_ethtool_ksettings_get_npage()
505 cmd->base.eth_tp_mdix = ETH_TP_MDI; in mdio45_ethtool_ksettings_get_npage()
507 case 0: in mdio45_ethtool_ksettings_get_npage()
508 cmd->base.eth_tp_mdix = ETH_TP_MDI_X; in mdio45_ethtool_ksettings_get_npage()
512 cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID; in mdio45_ethtool_ksettings_get_npage()
520 * mdio_mii_ioctl - MII ioctl interface for MDIO (clause 22 or 45) PHYs
521 * @mdio: MDIO interface
525 * Returns 0 on success, negative on error.
527 int mdio_mii_ioctl(const struct mdio_if_info *mdio, in mdio_mii_ioctl() argument
531 u16 addr = mii_data->reg_num; in mdio_mii_ioctl()
536 if (mdio->prtad == MDIO_PRTAD_NONE) in mdio_mii_ioctl()
537 return -EOPNOTSUPP; in mdio_mii_ioctl()
538 mii_data->phy_id = mdio->prtad; in mdio_mii_ioctl()
545 return -EOPNOTSUPP; in mdio_mii_ioctl()
549 if ((mdio->mode_support & MDIO_SUPPORTS_C45) && in mdio_mii_ioctl()
550 mdio_phy_id_is_c45(mii_data->phy_id)) { in mdio_mii_ioctl()
551 prtad = mdio_phy_id_prtad(mii_data->phy_id); in mdio_mii_ioctl()
552 devad = mdio_phy_id_devad(mii_data->phy_id); in mdio_mii_ioctl()
553 } else if ((mdio->mode_support & MDIO_SUPPORTS_C22) && in mdio_mii_ioctl()
554 mii_data->phy_id < 0x20) { in mdio_mii_ioctl()
555 prtad = mii_data->phy_id; in mdio_mii_ioctl()
557 addr &= 0x1f; in mdio_mii_ioctl()
558 } else if ((mdio->mode_support & MDIO_EMULATE_C22) && in mdio_mii_ioctl()
559 mdio->prtad != MDIO_PRTAD_NONE && in mdio_mii_ioctl()
560 mii_data->phy_id == mdio->prtad) { in mdio_mii_ioctl()
561 /* Remap commonly-used MII registers. */ in mdio_mii_ioctl()
562 prtad = mdio->prtad; in mdio_mii_ioctl()
568 devad = __ffs(mdio->mmds); in mdio_mii_ioctl()
572 if (!(mdio->mmds & MDIO_DEVS_AN)) in mdio_mii_ioctl()
573 return -EINVAL; in mdio_mii_ioctl()
581 return -EINVAL; in mdio_mii_ioctl()
584 return -EINVAL; in mdio_mii_ioctl()
588 int rc = mdio->mdio_read(mdio->dev, prtad, devad, addr); in mdio_mii_ioctl()
589 if (rc < 0) in mdio_mii_ioctl()
591 mii_data->val_out = rc; in mdio_mii_ioctl()
592 return 0; in mdio_mii_ioctl()
594 return mdio->mdio_write(mdio->dev, prtad, devad, addr, in mdio_mii_ioctl()
595 mii_data->val_in); in mdio_mii_ioctl()