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"
19 int mdio_clause45_reset_mmd(struct efx_nic *port, int mmd,
20 int spins, int spintime)
23 int phy_id = port->mii.phy_id;
25 /* Catch callers passing values in the wrong units (or just silly) */
26 EFX_BUG_ON_PARANOID(spins * spintime >= 5000);
28 mdio_clause45_write(port, phy_id, mmd, MDIO_MMDREG_CTRL1,
29 (1 << MDIO_MMDREG_CTRL1_RESET_LBN));
30 /* Wait for the reset bit to clear. */
33 ctrl = mdio_clause45_read(port, phy_id, mmd, MDIO_MMDREG_CTRL1);
36 } while (spins && (ctrl & (1 << MDIO_MMDREG_CTRL1_RESET_LBN)));
38 return spins ? spins : -ETIMEDOUT;
41 static int mdio_clause45_check_mmd(struct efx_nic *efx, int mmd,
45 int phy_id = efx->mii.phy_id;
47 if (LOOPBACK_INTERNAL(efx))
50 /* Read MMD STATUS2 to check it is responding. */
51 status = mdio_clause45_read(efx, phy_id, mmd, MDIO_MMDREG_STAT2);
52 if (((status >> MDIO_MMDREG_STAT2_PRESENT_LBN) &
53 ((1 << MDIO_MMDREG_STAT2_PRESENT_WIDTH) - 1)) !=
54 MDIO_MMDREG_STAT2_PRESENT_VAL) {
55 EFX_ERR(efx, "PHY MMD %d not responding.\n", mmd);
59 /* Read MMD STATUS 1 to check for fault. */
60 status = mdio_clause45_read(efx, phy_id, mmd, MDIO_MMDREG_STAT1);
61 if ((status & (1 << MDIO_MMDREG_STAT1_FAULT_LBN)) != 0) {
63 EFX_ERR(efx, "PHY MMD %d reporting fatal"
64 " fault: status %x\n", mmd, status);
67 EFX_LOG(efx, "PHY MMD %d reporting status"
68 " %x (expected)\n", mmd, status);
74 /* This ought to be ridiculous overkill. We expect it to fail rarely */
75 #define MDIO45_RESET_TIME 1000 /* ms */
76 #define MDIO45_RESET_ITERS 100
78 int mdio_clause45_wait_reset_mmds(struct efx_nic *efx,
79 unsigned int mmd_mask)
81 const int spintime = MDIO45_RESET_TIME / MDIO45_RESET_ITERS;
82 int tries = MDIO45_RESET_ITERS;
93 stat = mdio_clause45_read(efx,
98 EFX_ERR(efx, "failed to read status of"
102 if (stat & (1 << MDIO_MMDREG_CTRL1_RESET_LBN))
103 in_reset |= (1 << mmd);
114 EFX_ERR(efx, "not all MMDs came out of reset in time."
115 " MMDs still in reset: %x\n", in_reset);
121 int mdio_clause45_check_mmds(struct efx_nic *efx,
122 unsigned int mmd_mask, unsigned int fatal_mask)
125 int mmd = 0, probe_mmd;
127 /* Historically we have probed the PHYXS to find out what devices are
128 * present,but that doesn't work so well if the PHYXS isn't expected
129 * to exist, if so just find the first item in the list supplied. */
130 probe_mmd = (mmd_mask & MDIO_MMDREG_DEVS_PHYXS) ? MDIO_MMD_PHYXS :
132 devices = (mdio_clause45_read(efx, efx->mii.phy_id,
133 probe_mmd, MDIO_MMDREG_DEVS0) |
134 mdio_clause45_read(efx, efx->mii.phy_id,
135 probe_mmd, MDIO_MMDREG_DEVS1) << 16);
137 /* Check all the expected MMDs are present */
139 EFX_ERR(efx, "failed to read devices present\n");
142 if ((devices & mmd_mask) != mmd_mask) {
143 EFX_ERR(efx, "required MMDs not present: got %x, "
144 "wanted %x\n", devices, mmd_mask);
147 EFX_TRACE(efx, "Devices present: %x\n", devices);
149 /* Check all required MMDs are responding and happy. */
152 int fault_fatal = fatal_mask & 1;
153 if (mdio_clause45_check_mmd(efx, mmd, fault_fatal))
156 mmd_mask = mmd_mask >> 1;
157 fatal_mask = fatal_mask >> 1;
164 bool mdio_clause45_links_ok(struct efx_nic *efx, unsigned int mmd_mask)
166 int phy_id = efx->mii.phy_id;
171 /* If the port is in loopback, then we should only consider a subset
173 if (LOOPBACK_INTERNAL(efx))
175 else if (efx->loopback_mode == LOOPBACK_NETWORK)
177 else if (efx_phy_mode_disabled(efx->phy_mode))
179 else if (efx->loopback_mode == LOOPBACK_PHYXS)
180 mmd_mask &= ~(MDIO_MMDREG_DEVS_PHYXS |
181 MDIO_MMDREG_DEVS_PCS |
182 MDIO_MMDREG_DEVS_PMAPMD);
183 else if (efx->loopback_mode == LOOPBACK_PCS)
184 mmd_mask &= ~(MDIO_MMDREG_DEVS_PCS |
185 MDIO_MMDREG_DEVS_PMAPMD);
186 else if (efx->loopback_mode == LOOPBACK_PMAPMD)
187 mmd_mask &= ~MDIO_MMDREG_DEVS_PMAPMD;
191 /* Double reads because link state is latched, and a
192 * read moves the current state into the register */
193 status = mdio_clause45_read(efx, phy_id,
194 mmd, MDIO_MMDREG_STAT1);
195 status = mdio_clause45_read(efx, phy_id,
196 mmd, MDIO_MMDREG_STAT1);
198 ok = ok && (status & (1 << MDIO_MMDREG_STAT1_LINK_LBN));
200 mmd_mask = (mmd_mask >> 1);
206 void mdio_clause45_transmit_disable(struct efx_nic *efx)
208 int phy_id = efx->mii.phy_id;
211 ctrl1 = ctrl2 = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
213 if (efx->phy_mode & PHY_MODE_TX_DISABLED)
214 ctrl2 |= (1 << MDIO_MMDREG_TXDIS_GLOBAL_LBN);
216 ctrl1 &= ~(1 << MDIO_MMDREG_TXDIS_GLOBAL_LBN);
218 mdio_clause45_write(efx, phy_id, MDIO_MMD_PMAPMD,
219 MDIO_MMDREG_TXDIS, ctrl2);
222 void mdio_clause45_phy_reconfigure(struct efx_nic *efx)
224 int phy_id = efx->mii.phy_id;
227 /* Handle (with debouncing) PMA/PMD loopback */
228 ctrl1 = ctrl2 = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
231 if (efx->loopback_mode == LOOPBACK_PMAPMD)
232 ctrl2 |= (1 << MDIO_PMAPMD_CTRL1_LBACK_LBN);
234 ctrl2 &= ~(1 << MDIO_PMAPMD_CTRL1_LBACK_LBN);
237 mdio_clause45_write(efx, phy_id, MDIO_MMD_PMAPMD,
238 MDIO_MMDREG_CTRL1, ctrl2);
240 /* Handle (with debouncing) PCS loopback */
241 ctrl1 = ctrl2 = mdio_clause45_read(efx, phy_id, MDIO_MMD_PCS,
243 if (efx->loopback_mode == LOOPBACK_PCS)
244 ctrl2 |= (1 << MDIO_MMDREG_CTRL1_LBACK_LBN);
246 ctrl2 &= ~(1 << MDIO_MMDREG_CTRL1_LBACK_LBN);
249 mdio_clause45_write(efx, phy_id, MDIO_MMD_PCS,
250 MDIO_MMDREG_CTRL1, ctrl2);
252 /* Handle (with debouncing) PHYXS network loopback */
253 ctrl1 = ctrl2 = mdio_clause45_read(efx, phy_id, MDIO_MMD_PHYXS,
255 if (efx->loopback_mode == LOOPBACK_NETWORK)
256 ctrl2 |= (1 << MDIO_MMDREG_CTRL1_LBACK_LBN);
258 ctrl2 &= ~(1 << MDIO_MMDREG_CTRL1_LBACK_LBN);
261 mdio_clause45_write(efx, phy_id, MDIO_MMD_PHYXS,
262 MDIO_MMDREG_CTRL1, ctrl2);
265 static void mdio_clause45_set_mmd_lpower(struct efx_nic *efx,
268 int phy = efx->mii.phy_id;
269 int stat = mdio_clause45_read(efx, phy, mmd, MDIO_MMDREG_STAT1);
272 EFX_TRACE(efx, "Setting low power mode for MMD %d to %d\n",
275 if (stat & (1 << MDIO_MMDREG_STAT1_LPABLE_LBN)) {
276 ctrl1 = ctrl2 = mdio_clause45_read(efx, phy,
277 mmd, MDIO_MMDREG_CTRL1);
279 ctrl2 |= (1 << MDIO_MMDREG_CTRL1_LPOWER_LBN);
281 ctrl2 &= ~(1 << MDIO_MMDREG_CTRL1_LPOWER_LBN);
283 mdio_clause45_write(efx, phy, mmd,
284 MDIO_MMDREG_CTRL1, ctrl2);
288 void mdio_clause45_set_mmds_lpower(struct efx_nic *efx,
289 int low_power, unsigned int mmd_mask)
294 mdio_clause45_set_mmd_lpower(efx, low_power, mmd);
295 mmd_mask = (mmd_mask >> 1);
301 * mdio_clause45_get_settings - Read (some of) the PHY settings over MDIO.
303 * @ecmd: Buffer for settings
305 * On return the 'port', 'speed', 'supported' and 'advertising' fields of
306 * ecmd have been filled out based on the PMA type.
308 void mdio_clause45_get_settings(struct efx_nic *efx,
309 struct ethtool_cmd *ecmd)
313 /* If no PMA is present we are presumably talking something XAUI-ish
314 * like CX4. Which we report as FIBRE (see below) */
315 if ((efx->phy_op->mmds & DEV_PRESENT_BIT(MDIO_MMD_PMAPMD)) == 0) {
316 ecmd->speed = SPEED_10000;
317 ecmd->port = PORT_FIBRE;
318 ecmd->supported = SUPPORTED_FIBRE;
319 ecmd->advertising = ADVERTISED_FIBRE;
323 pma_type = mdio_clause45_read(efx, efx->mii.phy_id,
324 MDIO_MMD_PMAPMD, MDIO_MMDREG_CTRL2);
325 pma_type &= MDIO_PMAPMD_CTRL2_TYPE_MASK;
328 /* We represent CX4 as fibre in the absence of anything
330 case MDIO_PMAPMD_CTRL2_10G_CX4:
331 ecmd->speed = SPEED_10000;
332 ecmd->port = PORT_FIBRE;
333 ecmd->supported = SUPPORTED_FIBRE;
334 ecmd->advertising = ADVERTISED_FIBRE;
337 case MDIO_PMAPMD_CTRL2_10G_BT:
338 ecmd->speed = SPEED_10000;
339 ecmd->port = PORT_TP;
340 ecmd->supported = SUPPORTED_TP | SUPPORTED_10000baseT_Full;
341 ecmd->advertising = (ADVERTISED_FIBRE
342 | ADVERTISED_10000baseT_Full);
344 case MDIO_PMAPMD_CTRL2_1G_BT:
345 ecmd->speed = SPEED_1000;
346 ecmd->port = PORT_TP;
347 ecmd->supported = SUPPORTED_TP | SUPPORTED_1000baseT_Full;
348 ecmd->advertising = (ADVERTISED_FIBRE
349 | ADVERTISED_1000baseT_Full);
351 case MDIO_PMAPMD_CTRL2_100_BT:
352 ecmd->speed = SPEED_100;
353 ecmd->port = PORT_TP;
354 ecmd->supported = SUPPORTED_TP | SUPPORTED_100baseT_Full;
355 ecmd->advertising = (ADVERTISED_FIBRE
356 | ADVERTISED_100baseT_Full);
358 case MDIO_PMAPMD_CTRL2_10_BT:
359 ecmd->speed = SPEED_10;
360 ecmd->port = PORT_TP;
361 ecmd->supported = SUPPORTED_TP | SUPPORTED_10baseT_Full;
362 ecmd->advertising = ADVERTISED_FIBRE | ADVERTISED_10baseT_Full;
364 /* All the other defined modes are flavours of
367 ecmd->speed = SPEED_10000;
368 ecmd->port = PORT_FIBRE;
369 ecmd->supported = SUPPORTED_FIBRE;
370 ecmd->advertising = ADVERTISED_FIBRE;
376 * mdio_clause45_set_settings - Set (some of) the PHY settings over MDIO.
378 * @ecmd: New settings
380 * Currently this just enforces that we are _not_ changing the
381 * 'port', 'speed', 'supported' or 'advertising' settings as these
382 * cannot be changed on any currently supported PHY.
384 int mdio_clause45_set_settings(struct efx_nic *efx,
385 struct ethtool_cmd *ecmd)
387 struct ethtool_cmd tmpcmd;
388 mdio_clause45_get_settings(efx, &tmpcmd);
389 /* None of the current PHYs support more than one mode
390 * of operation (and only 10GBT ever will), so keep things
392 if ((ecmd->speed == tmpcmd.speed) && (ecmd->port == tmpcmd.port) &&
393 (ecmd->supported == tmpcmd.supported) &&
394 (ecmd->advertising == tmpcmd.advertising))