Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | * Driver for Solarflare Solarstorm network controllers and boards |
| 3 | * Copyright 2006-2008 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 | * Useful functions for working with MDIO clause 45 PHYs |
| 11 | */ |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/ethtool.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include "net_driver.h" |
| 16 | #include "mdio_10g.h" |
| 17 | #include "boards.h" |
| 18 | |
| 19 | int mdio_clause45_reset_mmd(struct efx_nic *port, int mmd, |
| 20 | int spins, int spintime) |
| 21 | { |
| 22 | u32 ctrl; |
| 23 | int phy_id = port->mii.phy_id; |
| 24 | |
| 25 | /* Catch callers passing values in the wrong units (or just silly) */ |
| 26 | EFX_BUG_ON_PARANOID(spins * spintime >= 5000); |
| 27 | |
| 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. */ |
| 31 | do { |
| 32 | msleep(spintime); |
| 33 | ctrl = mdio_clause45_read(port, phy_id, mmd, MDIO_MMDREG_CTRL1); |
| 34 | spins--; |
| 35 | |
| 36 | } while (spins && (ctrl & (1 << MDIO_MMDREG_CTRL1_RESET_LBN))); |
| 37 | |
| 38 | return spins ? spins : -ETIMEDOUT; |
| 39 | } |
| 40 | |
| 41 | static int mdio_clause45_check_mmd(struct efx_nic *efx, int mmd, |
| 42 | int fault_fatal) |
| 43 | { |
| 44 | int status; |
| 45 | int phy_id = efx->mii.phy_id; |
| 46 | |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 47 | if (LOOPBACK_INTERNAL(efx)) |
| 48 | return 0; |
| 49 | |
Ben Hutchings | 04cc8ca | 2008-12-12 21:50:46 -0800 | [diff] [blame] | 50 | if (mmd != MDIO_MMD_AN) { |
| 51 | /* Read MMD STATUS2 to check it is responding. */ |
| 52 | status = mdio_clause45_read(efx, phy_id, mmd, |
| 53 | MDIO_MMDREG_STAT2); |
| 54 | if (((status >> MDIO_MMDREG_STAT2_PRESENT_LBN) & |
| 55 | ((1 << MDIO_MMDREG_STAT2_PRESENT_WIDTH) - 1)) != |
| 56 | MDIO_MMDREG_STAT2_PRESENT_VAL) { |
| 57 | EFX_ERR(efx, "PHY MMD %d not responding.\n", mmd); |
| 58 | return -EIO; |
| 59 | } |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /* Read MMD STATUS 1 to check for fault. */ |
| 63 | status = mdio_clause45_read(efx, phy_id, mmd, MDIO_MMDREG_STAT1); |
| 64 | if ((status & (1 << MDIO_MMDREG_STAT1_FAULT_LBN)) != 0) { |
| 65 | if (fault_fatal) { |
| 66 | EFX_ERR(efx, "PHY MMD %d reporting fatal" |
| 67 | " fault: status %x\n", mmd, status); |
| 68 | return -EIO; |
| 69 | } else { |
| 70 | EFX_LOG(efx, "PHY MMD %d reporting status" |
| 71 | " %x (expected)\n", mmd, status); |
| 72 | } |
| 73 | } |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | /* This ought to be ridiculous overkill. We expect it to fail rarely */ |
| 78 | #define MDIO45_RESET_TIME 1000 /* ms */ |
| 79 | #define MDIO45_RESET_ITERS 100 |
| 80 | |
| 81 | int mdio_clause45_wait_reset_mmds(struct efx_nic *efx, |
| 82 | unsigned int mmd_mask) |
| 83 | { |
| 84 | const int spintime = MDIO45_RESET_TIME / MDIO45_RESET_ITERS; |
| 85 | int tries = MDIO45_RESET_ITERS; |
| 86 | int rc = 0; |
| 87 | int in_reset; |
| 88 | |
| 89 | while (tries) { |
| 90 | int mask = mmd_mask; |
| 91 | int mmd = 0; |
| 92 | int stat; |
| 93 | in_reset = 0; |
| 94 | while (mask) { |
| 95 | if (mask & 1) { |
| 96 | stat = mdio_clause45_read(efx, |
| 97 | efx->mii.phy_id, |
| 98 | mmd, |
| 99 | MDIO_MMDREG_CTRL1); |
| 100 | if (stat < 0) { |
| 101 | EFX_ERR(efx, "failed to read status of" |
| 102 | " MMD %d\n", mmd); |
| 103 | return -EIO; |
| 104 | } |
| 105 | if (stat & (1 << MDIO_MMDREG_CTRL1_RESET_LBN)) |
| 106 | in_reset |= (1 << mmd); |
| 107 | } |
| 108 | mask = mask >> 1; |
| 109 | mmd++; |
| 110 | } |
| 111 | if (!in_reset) |
| 112 | break; |
| 113 | tries--; |
| 114 | msleep(spintime); |
| 115 | } |
| 116 | if (in_reset != 0) { |
| 117 | EFX_ERR(efx, "not all MMDs came out of reset in time." |
| 118 | " MMDs still in reset: %x\n", in_reset); |
| 119 | rc = -ETIMEDOUT; |
| 120 | } |
| 121 | return rc; |
| 122 | } |
| 123 | |
| 124 | int mdio_clause45_check_mmds(struct efx_nic *efx, |
| 125 | unsigned int mmd_mask, unsigned int fatal_mask) |
| 126 | { |
Ben Hutchings | 27dd2ca | 2008-12-12 21:44:14 -0800 | [diff] [blame] | 127 | u32 devices; |
| 128 | int mmd = 0, probe_mmd; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 129 | |
| 130 | /* Historically we have probed the PHYXS to find out what devices are |
| 131 | * present,but that doesn't work so well if the PHYXS isn't expected |
| 132 | * to exist, if so just find the first item in the list supplied. */ |
Ben Hutchings | 27dd2ca | 2008-12-12 21:44:14 -0800 | [diff] [blame] | 133 | probe_mmd = (mmd_mask & MDIO_MMDREG_DEVS_PHYXS) ? MDIO_MMD_PHYXS : |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 134 | __ffs(mmd_mask); |
Ben Hutchings | 27dd2ca | 2008-12-12 21:44:14 -0800 | [diff] [blame] | 135 | devices = (mdio_clause45_read(efx, efx->mii.phy_id, |
| 136 | probe_mmd, MDIO_MMDREG_DEVS0) | |
| 137 | mdio_clause45_read(efx, efx->mii.phy_id, |
| 138 | probe_mmd, MDIO_MMDREG_DEVS1) << 16); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 139 | |
| 140 | /* Check all the expected MMDs are present */ |
| 141 | if (devices < 0) { |
| 142 | EFX_ERR(efx, "failed to read devices present\n"); |
| 143 | return -EIO; |
| 144 | } |
| 145 | if ((devices & mmd_mask) != mmd_mask) { |
| 146 | EFX_ERR(efx, "required MMDs not present: got %x, " |
| 147 | "wanted %x\n", devices, mmd_mask); |
| 148 | return -ENODEV; |
| 149 | } |
| 150 | EFX_TRACE(efx, "Devices present: %x\n", devices); |
| 151 | |
| 152 | /* Check all required MMDs are responding and happy. */ |
| 153 | while (mmd_mask) { |
| 154 | if (mmd_mask & 1) { |
| 155 | int fault_fatal = fatal_mask & 1; |
| 156 | if (mdio_clause45_check_mmd(efx, mmd, fault_fatal)) |
| 157 | return -EIO; |
| 158 | } |
| 159 | mmd_mask = mmd_mask >> 1; |
| 160 | fatal_mask = fatal_mask >> 1; |
| 161 | mmd++; |
| 162 | } |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
Ben Hutchings | dc8cfa5 | 2008-09-01 12:46:50 +0100 | [diff] [blame] | 167 | bool mdio_clause45_links_ok(struct efx_nic *efx, unsigned int mmd_mask) |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 168 | { |
| 169 | int phy_id = efx->mii.phy_id; |
| 170 | int status; |
Ben Hutchings | dc8cfa5 | 2008-09-01 12:46:50 +0100 | [diff] [blame] | 171 | bool ok = true; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 172 | int mmd = 0; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 173 | |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 174 | /* If the port is in loopback, then we should only consider a subset |
| 175 | * of mmd's */ |
| 176 | if (LOOPBACK_INTERNAL(efx)) |
Ben Hutchings | dc8cfa5 | 2008-09-01 12:46:50 +0100 | [diff] [blame] | 177 | return true; |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 178 | else if (efx->loopback_mode == LOOPBACK_NETWORK) |
Ben Hutchings | dc8cfa5 | 2008-09-01 12:46:50 +0100 | [diff] [blame] | 179 | return false; |
Ben Hutchings | f8b87c1 | 2008-09-01 12:48:17 +0100 | [diff] [blame] | 180 | else if (efx_phy_mode_disabled(efx->phy_mode)) |
| 181 | return false; |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 182 | else if (efx->loopback_mode == LOOPBACK_PHYXS) |
Ben Hutchings | 27dd2ca | 2008-12-12 21:44:14 -0800 | [diff] [blame] | 183 | mmd_mask &= ~(MDIO_MMDREG_DEVS_PHYXS | |
| 184 | MDIO_MMDREG_DEVS_PCS | |
Ben Hutchings | 04cc8ca | 2008-12-12 21:50:46 -0800 | [diff] [blame] | 185 | MDIO_MMDREG_DEVS_PMAPMD | |
| 186 | MDIO_MMDREG_DEVS_AN); |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 187 | else if (efx->loopback_mode == LOOPBACK_PCS) |
Ben Hutchings | 27dd2ca | 2008-12-12 21:44:14 -0800 | [diff] [blame] | 188 | mmd_mask &= ~(MDIO_MMDREG_DEVS_PCS | |
Ben Hutchings | 04cc8ca | 2008-12-12 21:50:46 -0800 | [diff] [blame] | 189 | MDIO_MMDREG_DEVS_PMAPMD | |
| 190 | MDIO_MMDREG_DEVS_AN); |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 191 | else if (efx->loopback_mode == LOOPBACK_PMAPMD) |
Ben Hutchings | 04cc8ca | 2008-12-12 21:50:46 -0800 | [diff] [blame] | 192 | mmd_mask &= ~(MDIO_MMDREG_DEVS_PMAPMD | |
| 193 | MDIO_MMDREG_DEVS_AN); |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 194 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 195 | while (mmd_mask) { |
| 196 | if (mmd_mask & 1) { |
| 197 | /* Double reads because link state is latched, and a |
| 198 | * read moves the current state into the register */ |
| 199 | status = mdio_clause45_read(efx, phy_id, |
| 200 | mmd, MDIO_MMDREG_STAT1); |
| 201 | status = mdio_clause45_read(efx, phy_id, |
| 202 | mmd, MDIO_MMDREG_STAT1); |
| 203 | |
Ben Hutchings | dc8cfa5 | 2008-09-01 12:46:50 +0100 | [diff] [blame] | 204 | ok = ok && (status & (1 << MDIO_MMDREG_STAT1_LINK_LBN)); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 205 | } |
| 206 | mmd_mask = (mmd_mask >> 1); |
| 207 | mmd++; |
| 208 | } |
| 209 | return ok; |
| 210 | } |
| 211 | |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 212 | void mdio_clause45_transmit_disable(struct efx_nic *efx) |
| 213 | { |
Ben Hutchings | 356eebb | 2008-12-12 21:48:57 -0800 | [diff] [blame] | 214 | mdio_clause45_set_flag(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD, |
| 215 | MDIO_MMDREG_TXDIS, MDIO_MMDREG_TXDIS_GLOBAL_LBN, |
| 216 | efx->phy_mode & PHY_MODE_TX_DISABLED); |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void mdio_clause45_phy_reconfigure(struct efx_nic *efx) |
| 220 | { |
| 221 | int phy_id = efx->mii.phy_id; |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 222 | |
Ben Hutchings | 356eebb | 2008-12-12 21:48:57 -0800 | [diff] [blame] | 223 | mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_PMAPMD, |
| 224 | MDIO_MMDREG_CTRL1, MDIO_PMAPMD_CTRL1_LBACK_LBN, |
| 225 | efx->loopback_mode == LOOPBACK_PMAPMD); |
| 226 | mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_PCS, |
| 227 | MDIO_MMDREG_CTRL1, MDIO_MMDREG_CTRL1_LBACK_LBN, |
| 228 | efx->loopback_mode == LOOPBACK_PCS); |
| 229 | mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_PHYXS, |
| 230 | MDIO_MMDREG_CTRL1, MDIO_MMDREG_CTRL1_LBACK_LBN, |
| 231 | efx->loopback_mode == LOOPBACK_NETWORK); |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 232 | } |
| 233 | |
Ben Hutchings | 3e133c4 | 2008-11-04 20:34:56 +0000 | [diff] [blame] | 234 | static void mdio_clause45_set_mmd_lpower(struct efx_nic *efx, |
| 235 | int lpower, int mmd) |
| 236 | { |
| 237 | int phy = efx->mii.phy_id; |
| 238 | int stat = mdio_clause45_read(efx, phy, mmd, MDIO_MMDREG_STAT1); |
Ben Hutchings | 3e133c4 | 2008-11-04 20:34:56 +0000 | [diff] [blame] | 239 | |
| 240 | EFX_TRACE(efx, "Setting low power mode for MMD %d to %d\n", |
| 241 | mmd, lpower); |
| 242 | |
| 243 | if (stat & (1 << MDIO_MMDREG_STAT1_LPABLE_LBN)) { |
Ben Hutchings | 356eebb | 2008-12-12 21:48:57 -0800 | [diff] [blame] | 244 | mdio_clause45_set_flag(efx, phy, mmd, MDIO_MMDREG_CTRL1, |
| 245 | MDIO_MMDREG_CTRL1_LPOWER_LBN, lpower); |
Ben Hutchings | 3e133c4 | 2008-11-04 20:34:56 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
| 249 | void mdio_clause45_set_mmds_lpower(struct efx_nic *efx, |
| 250 | int low_power, unsigned int mmd_mask) |
| 251 | { |
| 252 | int mmd = 0; |
Ben Hutchings | 04cc8ca | 2008-12-12 21:50:46 -0800 | [diff] [blame] | 253 | mmd_mask &= ~MDIO_MMDREG_DEVS_AN; |
Ben Hutchings | 3e133c4 | 2008-11-04 20:34:56 +0000 | [diff] [blame] | 254 | while (mmd_mask) { |
| 255 | if (mmd_mask & 1) |
| 256 | mdio_clause45_set_mmd_lpower(efx, low_power, mmd); |
| 257 | mmd_mask = (mmd_mask >> 1); |
| 258 | mmd++; |
| 259 | } |
| 260 | } |
| 261 | |
Ben Hutchings | 04cc8ca | 2008-12-12 21:50:46 -0800 | [diff] [blame] | 262 | static u32 mdio_clause45_get_an(struct efx_nic *efx, u16 addr, u32 xnp) |
| 263 | { |
| 264 | int phy_id = efx->mii.phy_id; |
| 265 | u32 result = 0; |
| 266 | int reg; |
| 267 | |
| 268 | reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN, addr); |
| 269 | if (reg & ADVERTISE_10HALF) |
| 270 | result |= ADVERTISED_10baseT_Half; |
| 271 | if (reg & ADVERTISE_10FULL) |
| 272 | result |= ADVERTISED_10baseT_Full; |
| 273 | if (reg & ADVERTISE_100HALF) |
| 274 | result |= ADVERTISED_100baseT_Half; |
| 275 | if (reg & ADVERTISE_100FULL) |
| 276 | result |= ADVERTISED_100baseT_Full; |
| 277 | if (reg & LPA_RESV) |
| 278 | result |= xnp; |
| 279 | |
| 280 | return result; |
| 281 | } |
| 282 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 283 | /** |
| 284 | * mdio_clause45_get_settings - Read (some of) the PHY settings over MDIO. |
| 285 | * @efx: Efx NIC |
| 286 | * @ecmd: Buffer for settings |
| 287 | * |
| 288 | * On return the 'port', 'speed', 'supported' and 'advertising' fields of |
Ben Hutchings | 04cc8ca | 2008-12-12 21:50:46 -0800 | [diff] [blame] | 289 | * ecmd have been filled out. |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 290 | */ |
| 291 | void mdio_clause45_get_settings(struct efx_nic *efx, |
| 292 | struct ethtool_cmd *ecmd) |
| 293 | { |
Ben Hutchings | 04cc8ca | 2008-12-12 21:50:46 -0800 | [diff] [blame] | 294 | mdio_clause45_get_settings_ext(efx, ecmd, 0, 0); |
| 295 | } |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 296 | |
Ben Hutchings | 04cc8ca | 2008-12-12 21:50:46 -0800 | [diff] [blame] | 297 | /** |
| 298 | * mdio_clause45_get_settings_ext - Read (some of) the PHY settings over MDIO. |
| 299 | * @efx: Efx NIC |
| 300 | * @ecmd: Buffer for settings |
| 301 | * @xnp: Advertised Extended Next Page state |
| 302 | * @xnp_lpa: Link Partner's advertised XNP state |
| 303 | * |
| 304 | * On return the 'port', 'speed', 'supported' and 'advertising' fields of |
| 305 | * ecmd have been filled out. |
| 306 | */ |
| 307 | void mdio_clause45_get_settings_ext(struct efx_nic *efx, |
| 308 | struct ethtool_cmd *ecmd, |
| 309 | u32 xnp, u32 xnp_lpa) |
| 310 | { |
| 311 | int phy_id = efx->mii.phy_id; |
| 312 | int reg; |
| 313 | |
| 314 | ecmd->transceiver = XCVR_INTERNAL; |
| 315 | ecmd->phy_address = phy_id; |
| 316 | |
| 317 | reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD, |
| 318 | MDIO_MMDREG_CTRL2); |
| 319 | switch (reg & MDIO_PMAPMD_CTRL2_TYPE_MASK) { |
| 320 | case MDIO_PMAPMD_CTRL2_10G_BT: |
| 321 | case MDIO_PMAPMD_CTRL2_1G_BT: |
| 322 | case MDIO_PMAPMD_CTRL2_100_BT: |
| 323 | case MDIO_PMAPMD_CTRL2_10_BT: |
| 324 | ecmd->port = PORT_TP; |
| 325 | ecmd->supported = SUPPORTED_TP; |
| 326 | reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD, |
| 327 | MDIO_MMDREG_SPEED); |
| 328 | if (reg & (1 << MDIO_MMDREG_SPEED_10G_LBN)) |
| 329 | ecmd->supported |= SUPPORTED_10000baseT_Full; |
| 330 | if (reg & (1 << MDIO_MMDREG_SPEED_1000M_LBN)) |
| 331 | ecmd->supported |= (SUPPORTED_1000baseT_Full | |
| 332 | SUPPORTED_1000baseT_Half); |
| 333 | if (reg & (1 << MDIO_MMDREG_SPEED_100M_LBN)) |
| 334 | ecmd->supported |= (SUPPORTED_100baseT_Full | |
| 335 | SUPPORTED_100baseT_Half); |
| 336 | if (reg & (1 << MDIO_MMDREG_SPEED_10M_LBN)) |
| 337 | ecmd->supported |= (SUPPORTED_10baseT_Full | |
| 338 | SUPPORTED_10baseT_Half); |
| 339 | ecmd->advertising = ADVERTISED_TP; |
| 340 | break; |
| 341 | |
| 342 | /* We represent CX4 as fibre in the absence of anything better */ |
| 343 | case MDIO_PMAPMD_CTRL2_10G_CX4: |
| 344 | /* All the other defined modes are flavours of optical */ |
| 345 | default: |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 346 | ecmd->port = PORT_FIBRE; |
| 347 | ecmd->supported = SUPPORTED_FIBRE; |
| 348 | ecmd->advertising = ADVERTISED_FIBRE; |
Ben Hutchings | 04cc8ca | 2008-12-12 21:50:46 -0800 | [diff] [blame] | 349 | break; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 350 | } |
| 351 | |
Ben Hutchings | 04cc8ca | 2008-12-12 21:50:46 -0800 | [diff] [blame] | 352 | if (efx->phy_op->mmds & DEV_PRESENT_BIT(MDIO_MMD_AN)) { |
| 353 | ecmd->supported |= SUPPORTED_Autoneg; |
| 354 | reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN, |
| 355 | MDIO_MMDREG_CTRL1); |
| 356 | if (reg & BMCR_ANENABLE) { |
| 357 | ecmd->autoneg = AUTONEG_ENABLE; |
| 358 | ecmd->advertising |= |
| 359 | ADVERTISED_Autoneg | |
| 360 | mdio_clause45_get_an(efx, |
| 361 | MDIO_AN_ADVERTISE, xnp); |
| 362 | } else |
| 363 | ecmd->autoneg = AUTONEG_DISABLE; |
| 364 | } else |
| 365 | ecmd->autoneg = AUTONEG_DISABLE; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 366 | |
Ben Hutchings | 04cc8ca | 2008-12-12 21:50:46 -0800 | [diff] [blame] | 367 | /* If AN is enabled and complete, report best common mode */ |
| 368 | if (ecmd->autoneg && |
| 369 | (mdio_clause45_read(efx, phy_id, MDIO_MMD_AN, MDIO_MMDREG_STAT1) & |
| 370 | (1 << MDIO_AN_STATUS_AN_DONE_LBN))) { |
| 371 | u32 common, lpa; |
| 372 | lpa = mdio_clause45_get_an(efx, MDIO_AN_LPA, xnp_lpa); |
| 373 | common = ecmd->advertising & lpa; |
| 374 | if (common & ADVERTISED_10000baseT_Full) { |
| 375 | ecmd->speed = SPEED_10000; |
| 376 | ecmd->duplex = DUPLEX_FULL; |
| 377 | } else if (common & (ADVERTISED_1000baseT_Full | |
| 378 | ADVERTISED_1000baseT_Half)) { |
| 379 | ecmd->speed = SPEED_1000; |
| 380 | ecmd->duplex = !!(common & ADVERTISED_1000baseT_Full); |
| 381 | } else if (common & (ADVERTISED_100baseT_Full | |
| 382 | ADVERTISED_100baseT_Half)) { |
| 383 | ecmd->speed = SPEED_100; |
| 384 | ecmd->duplex = !!(common & ADVERTISED_100baseT_Full); |
| 385 | } else { |
| 386 | ecmd->speed = SPEED_10; |
| 387 | ecmd->duplex = !!(common & ADVERTISED_10baseT_Full); |
| 388 | } |
| 389 | } else { |
| 390 | /* Report forced settings */ |
| 391 | reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD, |
| 392 | MDIO_MMDREG_CTRL1); |
| 393 | ecmd->speed = (((reg & BMCR_SPEED1000) ? 100 : 1) * |
| 394 | ((reg & BMCR_SPEED100) ? 100 : 10)); |
| 395 | ecmd->duplex = (reg & BMCR_FULLDPLX || |
| 396 | ecmd->speed == SPEED_10000); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * mdio_clause45_set_settings - Set (some of) the PHY settings over MDIO. |
| 402 | * @efx: Efx NIC |
| 403 | * @ecmd: New settings |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 404 | */ |
| 405 | int mdio_clause45_set_settings(struct efx_nic *efx, |
| 406 | struct ethtool_cmd *ecmd) |
| 407 | { |
Ben Hutchings | 04cc8ca | 2008-12-12 21:50:46 -0800 | [diff] [blame] | 408 | int phy_id = efx->mii.phy_id; |
| 409 | struct ethtool_cmd prev; |
| 410 | u32 required; |
| 411 | int ctrl1_bits, reg; |
| 412 | |
| 413 | efx->phy_op->get_settings(efx, &prev); |
| 414 | |
| 415 | if (ecmd->advertising == prev.advertising && |
| 416 | ecmd->speed == prev.speed && |
| 417 | ecmd->duplex == prev.duplex && |
| 418 | ecmd->port == prev.port && |
| 419 | ecmd->autoneg == prev.autoneg) |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 420 | return 0; |
Ben Hutchings | 04cc8ca | 2008-12-12 21:50:46 -0800 | [diff] [blame] | 421 | |
| 422 | /* We can only change these settings for -T PHYs */ |
| 423 | if (prev.port != PORT_TP || ecmd->port != PORT_TP) |
| 424 | return -EINVAL; |
| 425 | |
| 426 | /* Check that PHY supports these settings and work out the |
| 427 | * basic control bits */ |
| 428 | if (ecmd->duplex) { |
| 429 | switch (ecmd->speed) { |
| 430 | case SPEED_10: |
| 431 | ctrl1_bits = BMCR_FULLDPLX; |
| 432 | required = SUPPORTED_10baseT_Full; |
| 433 | break; |
| 434 | case SPEED_100: |
| 435 | ctrl1_bits = BMCR_SPEED100 | BMCR_FULLDPLX; |
| 436 | required = SUPPORTED_100baseT_Full; |
| 437 | break; |
| 438 | case SPEED_1000: |
| 439 | ctrl1_bits = BMCR_SPEED1000 | BMCR_FULLDPLX; |
| 440 | required = SUPPORTED_1000baseT_Full; |
| 441 | break; |
| 442 | case SPEED_10000: |
| 443 | ctrl1_bits = (BMCR_SPEED1000 | BMCR_SPEED100 | |
| 444 | BMCR_FULLDPLX); |
| 445 | required = SUPPORTED_10000baseT_Full; |
| 446 | break; |
| 447 | default: |
| 448 | return -EINVAL; |
| 449 | } |
| 450 | } else { |
| 451 | switch (ecmd->speed) { |
| 452 | case SPEED_10: |
| 453 | ctrl1_bits = 0; |
| 454 | required = SUPPORTED_10baseT_Half; |
| 455 | break; |
| 456 | case SPEED_100: |
| 457 | ctrl1_bits = BMCR_SPEED100; |
| 458 | required = SUPPORTED_100baseT_Half; |
| 459 | break; |
| 460 | case SPEED_1000: |
| 461 | ctrl1_bits = BMCR_SPEED1000; |
| 462 | required = SUPPORTED_1000baseT_Half; |
| 463 | break; |
| 464 | default: |
| 465 | return -EINVAL; |
| 466 | } |
| 467 | } |
| 468 | if (ecmd->autoneg) |
| 469 | required |= SUPPORTED_Autoneg; |
| 470 | required |= ecmd->advertising; |
| 471 | if (required & ~prev.supported) |
| 472 | return -EINVAL; |
| 473 | |
| 474 | /* Set the basic control bits */ |
| 475 | reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD, |
| 476 | MDIO_MMDREG_CTRL1); |
| 477 | reg &= ~(BMCR_SPEED1000 | BMCR_SPEED100 | BMCR_FULLDPLX | 0x003c); |
| 478 | reg |= ctrl1_bits; |
| 479 | mdio_clause45_write(efx, phy_id, MDIO_MMD_PMAPMD, MDIO_MMDREG_CTRL1, |
| 480 | reg); |
| 481 | |
| 482 | /* Set the AN registers */ |
| 483 | if (ecmd->autoneg != prev.autoneg || |
| 484 | ecmd->advertising != prev.advertising) { |
| 485 | bool xnp = false; |
| 486 | |
| 487 | if (efx->phy_op->set_xnp_advertise) |
| 488 | xnp = efx->phy_op->set_xnp_advertise(efx, |
| 489 | ecmd->advertising); |
| 490 | |
| 491 | if (ecmd->autoneg) { |
| 492 | reg = 0; |
| 493 | if (ecmd->advertising & ADVERTISED_10baseT_Half) |
| 494 | reg |= ADVERTISE_10HALF; |
| 495 | if (ecmd->advertising & ADVERTISED_10baseT_Full) |
| 496 | reg |= ADVERTISE_10FULL; |
| 497 | if (ecmd->advertising & ADVERTISED_100baseT_Half) |
| 498 | reg |= ADVERTISE_100HALF; |
| 499 | if (ecmd->advertising & ADVERTISED_100baseT_Full) |
| 500 | reg |= ADVERTISE_100FULL; |
| 501 | if (xnp) |
| 502 | reg |= ADVERTISE_RESV; |
| 503 | mdio_clause45_write(efx, phy_id, MDIO_MMD_AN, |
| 504 | MDIO_AN_ADVERTISE, reg); |
| 505 | } |
| 506 | |
| 507 | reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN, |
| 508 | MDIO_MMDREG_CTRL1); |
| 509 | if (ecmd->autoneg) |
| 510 | reg |= BMCR_ANENABLE | BMCR_ANRESTART; |
| 511 | else |
| 512 | reg &= ~BMCR_ANENABLE; |
| 513 | if (xnp) |
| 514 | reg |= 1 << MDIO_AN_CTRL_XNP_LBN; |
| 515 | else |
| 516 | reg &= ~(1 << MDIO_AN_CTRL_XNP_LBN); |
| 517 | mdio_clause45_write(efx, phy_id, MDIO_MMD_AN, |
| 518 | MDIO_MMDREG_CTRL1, reg); |
| 519 | } |
| 520 | |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | void mdio_clause45_set_pause(struct efx_nic *efx) |
| 525 | { |
| 526 | int phy_id = efx->mii.phy_id; |
| 527 | int reg; |
| 528 | |
| 529 | if (efx->phy_op->mmds & DEV_PRESENT_BIT(MDIO_MMD_AN)) { |
| 530 | /* Set pause capability advertising */ |
| 531 | reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN, |
| 532 | MDIO_AN_ADVERTISE); |
| 533 | reg &= ~(ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM); |
| 534 | reg |= efx_fc_advertise(efx->wanted_fc); |
| 535 | mdio_clause45_write(efx, phy_id, MDIO_MMD_AN, |
| 536 | MDIO_AN_ADVERTISE, reg); |
| 537 | |
| 538 | /* Restart auto-negotiation */ |
| 539 | reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN, |
| 540 | MDIO_MMDREG_CTRL1); |
| 541 | if (reg & BMCR_ANENABLE) { |
| 542 | reg |= BMCR_ANRESTART; |
| 543 | mdio_clause45_write(efx, phy_id, MDIO_MMD_AN, |
| 544 | MDIO_MMDREG_CTRL1, reg); |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | enum efx_fc_type mdio_clause45_get_pause(struct efx_nic *efx) |
| 550 | { |
| 551 | int phy_id = efx->mii.phy_id; |
| 552 | int lpa; |
| 553 | |
| 554 | if (!(efx->phy_op->mmds & DEV_PRESENT_BIT(MDIO_MMD_AN))) |
| 555 | return efx->wanted_fc; |
| 556 | lpa = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN, MDIO_AN_LPA); |
| 557 | return efx_fc_resolve(efx->wanted_fc, lpa); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 558 | } |
Ben Hutchings | 356eebb | 2008-12-12 21:48:57 -0800 | [diff] [blame] | 559 | |
| 560 | void mdio_clause45_set_flag(struct efx_nic *efx, u8 prt, u8 dev, |
| 561 | u16 addr, int bit, bool sense) |
| 562 | { |
| 563 | int old_val = mdio_clause45_read(efx, prt, dev, addr); |
| 564 | int new_val; |
| 565 | |
| 566 | if (sense) |
| 567 | new_val = old_val | (1 << bit); |
| 568 | else |
| 569 | new_val = old_val & ~(1 << bit); |
| 570 | if (old_val != new_val) |
| 571 | mdio_clause45_write(efx, prt, dev, addr, new_val); |
| 572 | } |