sfc: Use generic MDIO functions and definitions
[linux-2.6/mini2440.git] / drivers / net / sfc / tenxpress.c
blob403f7d70c223133e07b70ccb1c5f448b715a739c
1 /****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2007-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.
8 */
10 #include <linux/delay.h>
11 #include <linux/rtnetlink.h>
12 #include <linux/seq_file.h>
13 #include "efx.h"
14 #include "mdio_10g.h"
15 #include "falcon.h"
16 #include "phy.h"
17 #include "falcon_hwdefs.h"
18 #include "boards.h"
19 #include "workarounds.h"
20 #include "selftest.h"
22 /* We expect these MMDs to be in the package. SFT9001 also has a
23 * clause 22 extension MMD, but since it doesn't have all the generic
24 * MMD registers it is pointless to include it here.
26 #define TENXPRESS_REQUIRED_DEVS (MDIO_DEVS_PMAPMD | \
27 MDIO_DEVS_PCS | \
28 MDIO_DEVS_PHYXS | \
29 MDIO_DEVS_AN)
31 #define SFX7101_LOOPBACKS ((1 << LOOPBACK_PHYXS) | \
32 (1 << LOOPBACK_PCS) | \
33 (1 << LOOPBACK_PMAPMD) | \
34 (1 << LOOPBACK_NETWORK))
36 #define SFT9001_LOOPBACKS ((1 << LOOPBACK_GPHY) | \
37 (1 << LOOPBACK_PHYXS) | \
38 (1 << LOOPBACK_PCS) | \
39 (1 << LOOPBACK_PMAPMD) | \
40 (1 << LOOPBACK_NETWORK))
42 /* We complain if we fail to see the link partner as 10G capable this many
43 * times in a row (must be > 1 as sampling the autoneg. registers is racy)
45 #define MAX_BAD_LP_TRIES (5)
47 /* LASI Control */
48 #define PMA_PMD_LASI_CTRL 36866
49 #define PMA_PMD_LASI_STATUS 36869
50 #define PMA_PMD_LS_ALARM_LBN 0
51 #define PMA_PMD_LS_ALARM_WIDTH 1
52 #define PMA_PMD_TX_ALARM_LBN 1
53 #define PMA_PMD_TX_ALARM_WIDTH 1
54 #define PMA_PMD_RX_ALARM_LBN 2
55 #define PMA_PMD_RX_ALARM_WIDTH 1
56 #define PMA_PMD_AN_ALARM_LBN 3
57 #define PMA_PMD_AN_ALARM_WIDTH 1
59 /* Extended control register */
60 #define PMA_PMD_XCONTROL_REG 49152
61 #define PMA_PMD_EXT_GMII_EN_LBN 1
62 #define PMA_PMD_EXT_GMII_EN_WIDTH 1
63 #define PMA_PMD_EXT_CLK_OUT_LBN 2
64 #define PMA_PMD_EXT_CLK_OUT_WIDTH 1
65 #define PMA_PMD_LNPGA_POWERDOWN_LBN 8 /* SFX7101 only */
66 #define PMA_PMD_LNPGA_POWERDOWN_WIDTH 1
67 #define PMA_PMD_EXT_CLK312_LBN 8 /* SFT9001 only */
68 #define PMA_PMD_EXT_CLK312_WIDTH 1
69 #define PMA_PMD_EXT_LPOWER_LBN 12
70 #define PMA_PMD_EXT_LPOWER_WIDTH 1
71 #define PMA_PMD_EXT_ROBUST_LBN 14
72 #define PMA_PMD_EXT_ROBUST_WIDTH 1
73 #define PMA_PMD_EXT_SSR_LBN 15
74 #define PMA_PMD_EXT_SSR_WIDTH 1
76 /* extended status register */
77 #define PMA_PMD_XSTATUS_REG 49153
78 #define PMA_PMD_XSTAT_FLP_LBN (12)
80 /* LED control register */
81 #define PMA_PMD_LED_CTRL_REG 49159
82 #define PMA_PMA_LED_ACTIVITY_LBN (3)
84 /* LED function override register */
85 #define PMA_PMD_LED_OVERR_REG 49161
86 /* Bit positions for different LEDs (there are more but not wired on SFE4001)*/
87 #define PMA_PMD_LED_LINK_LBN (0)
88 #define PMA_PMD_LED_SPEED_LBN (2)
89 #define PMA_PMD_LED_TX_LBN (4)
90 #define PMA_PMD_LED_RX_LBN (6)
91 /* Override settings */
92 #define PMA_PMD_LED_AUTO (0) /* H/W control */
93 #define PMA_PMD_LED_ON (1)
94 #define PMA_PMD_LED_OFF (2)
95 #define PMA_PMD_LED_FLASH (3)
96 #define PMA_PMD_LED_MASK 3
97 /* All LEDs under hardware control */
98 #define PMA_PMD_LED_FULL_AUTO (0)
99 /* Green and Amber under hardware control, Red off */
100 #define PMA_PMD_LED_DEFAULT (PMA_PMD_LED_OFF << PMA_PMD_LED_RX_LBN)
102 #define PMA_PMD_SPEED_ENABLE_REG 49192
103 #define PMA_PMD_100TX_ADV_LBN 1
104 #define PMA_PMD_100TX_ADV_WIDTH 1
105 #define PMA_PMD_1000T_ADV_LBN 2
106 #define PMA_PMD_1000T_ADV_WIDTH 1
107 #define PMA_PMD_10000T_ADV_LBN 3
108 #define PMA_PMD_10000T_ADV_WIDTH 1
109 #define PMA_PMD_SPEED_LBN 4
110 #define PMA_PMD_SPEED_WIDTH 4
112 /* Cable diagnostics - SFT9001 only */
113 #define PMA_PMD_CDIAG_CTRL_REG 49213
114 #define CDIAG_CTRL_IMMED_LBN 15
115 #define CDIAG_CTRL_BRK_LINK_LBN 12
116 #define CDIAG_CTRL_IN_PROG_LBN 11
117 #define CDIAG_CTRL_LEN_UNIT_LBN 10
118 #define CDIAG_CTRL_LEN_METRES 1
119 #define PMA_PMD_CDIAG_RES_REG 49174
120 #define CDIAG_RES_A_LBN 12
121 #define CDIAG_RES_B_LBN 8
122 #define CDIAG_RES_C_LBN 4
123 #define CDIAG_RES_D_LBN 0
124 #define CDIAG_RES_WIDTH 4
125 #define CDIAG_RES_OPEN 2
126 #define CDIAG_RES_OK 1
127 #define CDIAG_RES_INVALID 0
128 /* Set of 4 registers for pairs A-D */
129 #define PMA_PMD_CDIAG_LEN_REG 49175
131 /* Serdes control registers - SFT9001 only */
132 #define PMA_PMD_CSERDES_CTRL_REG 64258
133 /* Set the 156.25 MHz output to 312.5 MHz to drive Falcon's XMAC */
134 #define PMA_PMD_CSERDES_DEFAULT 0x000f
136 /* Misc register defines - SFX7101 only */
137 #define PCS_CLOCK_CTRL_REG 55297
138 #define PLL312_RST_N_LBN 2
140 #define PCS_SOFT_RST2_REG 55302
141 #define SERDES_RST_N_LBN 13
142 #define XGXS_RST_N_LBN 12
144 #define PCS_TEST_SELECT_REG 55303 /* PRM 10.5.8 */
145 #define CLK312_EN_LBN 3
147 /* PHYXS registers */
148 #define PHYXS_XCONTROL_REG 49152
149 #define PHYXS_RESET_LBN 15
150 #define PHYXS_RESET_WIDTH 1
152 #define PHYXS_TEST1 (49162)
153 #define LOOPBACK_NEAR_LBN (8)
154 #define LOOPBACK_NEAR_WIDTH (1)
156 /* Boot status register */
157 #define PCS_BOOT_STATUS_REG 53248
158 #define PCS_BOOT_FATAL_ERROR_LBN 0
159 #define PCS_BOOT_PROGRESS_LBN 1
160 #define PCS_BOOT_PROGRESS_WIDTH 2
161 #define PCS_BOOT_PROGRESS_INIT 0
162 #define PCS_BOOT_PROGRESS_WAIT_MDIO 1
163 #define PCS_BOOT_PROGRESS_CHECKSUM 2
164 #define PCS_BOOT_PROGRESS_JUMP 3
165 #define PCS_BOOT_DOWNLOAD_WAIT_LBN 3
166 #define PCS_BOOT_CODE_STARTED_LBN 4
168 /* 100M/1G PHY registers */
169 #define GPHY_XCONTROL_REG 49152
170 #define GPHY_ISOLATE_LBN 10
171 #define GPHY_ISOLATE_WIDTH 1
172 #define GPHY_DUPLEX_LBN 8
173 #define GPHY_DUPLEX_WIDTH 1
174 #define GPHY_LOOPBACK_NEAR_LBN 14
175 #define GPHY_LOOPBACK_NEAR_WIDTH 1
177 #define C22EXT_STATUS_REG 49153
178 #define C22EXT_STATUS_LINK_LBN 2
179 #define C22EXT_STATUS_LINK_WIDTH 1
181 #define C22EXT_MSTSLV_CTRL 49161
182 #define C22EXT_MSTSLV_CTRL_ADV_1000_HD_LBN 8
183 #define C22EXT_MSTSLV_CTRL_ADV_1000_FD_LBN 9
185 #define C22EXT_MSTSLV_STATUS 49162
186 #define C22EXT_MSTSLV_STATUS_LP_1000_HD_LBN 10
187 #define C22EXT_MSTSLV_STATUS_LP_1000_FD_LBN 11
189 /* Time to wait between powering down the LNPGA and turning off the power
190 * rails */
191 #define LNPGA_PDOWN_WAIT (HZ / 5)
193 struct tenxpress_phy_data {
194 enum efx_loopback_mode loopback_mode;
195 enum efx_phy_mode phy_mode;
196 int bad_lp_tries;
199 static ssize_t show_phy_short_reach(struct device *dev,
200 struct device_attribute *attr, char *buf)
202 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
203 int reg;
205 reg = efx_mdio_read(efx, MDIO_MMD_PMAPMD, MDIO_PMA_10GBT_TXPWR);
206 return sprintf(buf, "%d\n", !!(reg & MDIO_PMA_10GBT_TXPWR_SHORT));
209 static ssize_t set_phy_short_reach(struct device *dev,
210 struct device_attribute *attr,
211 const char *buf, size_t count)
213 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
215 rtnl_lock();
216 efx_mdio_set_flag(efx, MDIO_MMD_PMAPMD, MDIO_PMA_10GBT_TXPWR,
217 MDIO_PMA_10GBT_TXPWR_SHORT,
218 count != 0 && *buf != '0');
219 efx_reconfigure_port(efx);
220 rtnl_unlock();
222 return count;
225 static DEVICE_ATTR(phy_short_reach, 0644, show_phy_short_reach,
226 set_phy_short_reach);
228 int sft9001_wait_boot(struct efx_nic *efx)
230 unsigned long timeout = jiffies + HZ + 1;
231 int boot_stat;
233 for (;;) {
234 boot_stat = efx_mdio_read(efx, MDIO_MMD_PCS,
235 PCS_BOOT_STATUS_REG);
236 if (boot_stat >= 0) {
237 EFX_LOG(efx, "PHY boot status = %#x\n", boot_stat);
238 switch (boot_stat &
239 ((1 << PCS_BOOT_FATAL_ERROR_LBN) |
240 (3 << PCS_BOOT_PROGRESS_LBN) |
241 (1 << PCS_BOOT_DOWNLOAD_WAIT_LBN) |
242 (1 << PCS_BOOT_CODE_STARTED_LBN))) {
243 case ((1 << PCS_BOOT_FATAL_ERROR_LBN) |
244 (PCS_BOOT_PROGRESS_CHECKSUM <<
245 PCS_BOOT_PROGRESS_LBN)):
246 case ((1 << PCS_BOOT_FATAL_ERROR_LBN) |
247 (PCS_BOOT_PROGRESS_INIT <<
248 PCS_BOOT_PROGRESS_LBN) |
249 (1 << PCS_BOOT_DOWNLOAD_WAIT_LBN)):
250 return -EINVAL;
251 case ((PCS_BOOT_PROGRESS_WAIT_MDIO <<
252 PCS_BOOT_PROGRESS_LBN) |
253 (1 << PCS_BOOT_DOWNLOAD_WAIT_LBN)):
254 return (efx->phy_mode & PHY_MODE_SPECIAL) ?
255 0 : -EIO;
256 case ((PCS_BOOT_PROGRESS_JUMP <<
257 PCS_BOOT_PROGRESS_LBN) |
258 (1 << PCS_BOOT_CODE_STARTED_LBN)):
259 case ((PCS_BOOT_PROGRESS_JUMP <<
260 PCS_BOOT_PROGRESS_LBN) |
261 (1 << PCS_BOOT_DOWNLOAD_WAIT_LBN) |
262 (1 << PCS_BOOT_CODE_STARTED_LBN)):
263 return (efx->phy_mode & PHY_MODE_SPECIAL) ?
264 -EIO : 0;
265 default:
266 if (boot_stat & (1 << PCS_BOOT_FATAL_ERROR_LBN))
267 return -EIO;
268 break;
272 if (time_after_eq(jiffies, timeout))
273 return -ETIMEDOUT;
275 msleep(50);
279 static int tenxpress_init(struct efx_nic *efx)
281 int reg;
283 if (efx->phy_type == PHY_TYPE_SFX7101) {
284 /* Enable 312.5 MHz clock */
285 efx_mdio_write(efx, MDIO_MMD_PCS, PCS_TEST_SELECT_REG,
286 1 << CLK312_EN_LBN);
287 } else {
288 /* Enable 312.5 MHz clock and GMII */
289 reg = efx_mdio_read(efx, MDIO_MMD_PMAPMD, PMA_PMD_XCONTROL_REG);
290 reg |= ((1 << PMA_PMD_EXT_GMII_EN_LBN) |
291 (1 << PMA_PMD_EXT_CLK_OUT_LBN) |
292 (1 << PMA_PMD_EXT_CLK312_LBN) |
293 (1 << PMA_PMD_EXT_ROBUST_LBN));
295 efx_mdio_write(efx, MDIO_MMD_PMAPMD, PMA_PMD_XCONTROL_REG, reg);
296 efx_mdio_set_flag(efx, MDIO_MMD_C22EXT,
297 GPHY_XCONTROL_REG, 1 << GPHY_ISOLATE_LBN,
298 false);
301 /* Set the LEDs up as: Green = Link, Amber = Link/Act, Red = Off */
302 if (efx->phy_type == PHY_TYPE_SFX7101) {
303 efx_mdio_set_flag(efx, MDIO_MMD_PMAPMD, PMA_PMD_LED_CTRL_REG,
304 1 << PMA_PMA_LED_ACTIVITY_LBN, true);
305 efx_mdio_write(efx, MDIO_MMD_PMAPMD, PMA_PMD_LED_OVERR_REG,
306 PMA_PMD_LED_DEFAULT);
309 return 0;
312 static int tenxpress_phy_init(struct efx_nic *efx)
314 struct tenxpress_phy_data *phy_data;
315 int rc = 0;
317 phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
318 if (!phy_data)
319 return -ENOMEM;
320 efx->phy_data = phy_data;
321 phy_data->phy_mode = efx->phy_mode;
323 if (!(efx->phy_mode & PHY_MODE_SPECIAL)) {
324 if (efx->phy_type == PHY_TYPE_SFT9001A) {
325 int reg;
326 reg = efx_mdio_read(efx, MDIO_MMD_PMAPMD,
327 PMA_PMD_XCONTROL_REG);
328 reg |= (1 << PMA_PMD_EXT_SSR_LBN);
329 efx_mdio_write(efx, MDIO_MMD_PMAPMD,
330 PMA_PMD_XCONTROL_REG, reg);
331 mdelay(200);
334 rc = efx_mdio_wait_reset_mmds(efx, TENXPRESS_REQUIRED_DEVS);
335 if (rc < 0)
336 goto fail;
338 rc = efx_mdio_check_mmds(efx, TENXPRESS_REQUIRED_DEVS, 0);
339 if (rc < 0)
340 goto fail;
343 rc = tenxpress_init(efx);
344 if (rc < 0)
345 goto fail;
346 efx_mdio_set_pause(efx);
348 if (efx->phy_type == PHY_TYPE_SFT9001B) {
349 rc = device_create_file(&efx->pci_dev->dev,
350 &dev_attr_phy_short_reach);
351 if (rc)
352 goto fail;
355 schedule_timeout_uninterruptible(HZ / 5); /* 200ms */
357 /* Let XGXS and SerDes out of reset */
358 falcon_reset_xaui(efx);
360 return 0;
362 fail:
363 kfree(efx->phy_data);
364 efx->phy_data = NULL;
365 return rc;
368 /* Perform a "special software reset" on the PHY. The caller is
369 * responsible for saving and restoring the PHY hardware registers
370 * properly, and masking/unmasking LASI */
371 static int tenxpress_special_reset(struct efx_nic *efx)
373 int rc, reg;
375 /* The XGMAC clock is driven from the SFC7101/SFT9001 312MHz clock, so
376 * a special software reset can glitch the XGMAC sufficiently for stats
377 * requests to fail. */
378 efx_stats_disable(efx);
380 /* Initiate reset */
381 reg = efx_mdio_read(efx, MDIO_MMD_PMAPMD, PMA_PMD_XCONTROL_REG);
382 reg |= (1 << PMA_PMD_EXT_SSR_LBN);
383 efx_mdio_write(efx, MDIO_MMD_PMAPMD, PMA_PMD_XCONTROL_REG, reg);
385 mdelay(200);
387 /* Wait for the blocks to come out of reset */
388 rc = efx_mdio_wait_reset_mmds(efx, TENXPRESS_REQUIRED_DEVS);
389 if (rc < 0)
390 goto out;
392 /* Try and reconfigure the device */
393 rc = tenxpress_init(efx);
394 if (rc < 0)
395 goto out;
397 /* Wait for the XGXS state machine to churn */
398 mdelay(10);
399 out:
400 efx_stats_enable(efx);
401 return rc;
404 static void sfx7101_check_bad_lp(struct efx_nic *efx, bool link_ok)
406 struct tenxpress_phy_data *pd = efx->phy_data;
407 bool bad_lp;
408 int reg;
410 if (link_ok) {
411 bad_lp = false;
412 } else {
413 /* Check that AN has started but not completed. */
414 reg = efx_mdio_read(efx, MDIO_MMD_AN, MDIO_STAT1);
415 if (!(reg & MDIO_AN_STAT1_LPABLE))
416 return; /* LP status is unknown */
417 bad_lp = !(reg & MDIO_AN_STAT1_COMPLETE);
418 if (bad_lp)
419 pd->bad_lp_tries++;
422 /* Nothing to do if all is well and was previously so. */
423 if (!pd->bad_lp_tries)
424 return;
426 /* Use the RX (red) LED as an error indicator once we've seen AN
427 * failure several times in a row, and also log a message. */
428 if (!bad_lp || pd->bad_lp_tries == MAX_BAD_LP_TRIES) {
429 reg = efx_mdio_read(efx, MDIO_MMD_PMAPMD,
430 PMA_PMD_LED_OVERR_REG);
431 reg &= ~(PMA_PMD_LED_MASK << PMA_PMD_LED_RX_LBN);
432 if (!bad_lp) {
433 reg |= PMA_PMD_LED_OFF << PMA_PMD_LED_RX_LBN;
434 } else {
435 reg |= PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN;
436 EFX_ERR(efx, "appears to be plugged into a port"
437 " that is not 10GBASE-T capable. The PHY"
438 " supports 10GBASE-T ONLY, so no link can"
439 " be established\n");
441 efx_mdio_write(efx, MDIO_MMD_PMAPMD,
442 PMA_PMD_LED_OVERR_REG, reg);
443 pd->bad_lp_tries = bad_lp;
447 static bool sfx7101_link_ok(struct efx_nic *efx)
449 return efx_mdio_links_ok(efx,
450 MDIO_DEVS_PMAPMD |
451 MDIO_DEVS_PCS |
452 MDIO_DEVS_PHYXS);
455 static bool sft9001_link_ok(struct efx_nic *efx, struct ethtool_cmd *ecmd)
457 u32 reg;
459 if (efx_phy_mode_disabled(efx->phy_mode))
460 return false;
461 else if (efx->loopback_mode == LOOPBACK_GPHY)
462 return true;
463 else if (efx->loopback_mode)
464 return efx_mdio_links_ok(efx,
465 MDIO_DEVS_PMAPMD |
466 MDIO_DEVS_PHYXS);
468 /* We must use the same definition of link state as LASI,
469 * otherwise we can miss a link state transition
471 if (ecmd->speed == 10000) {
472 reg = efx_mdio_read(efx, MDIO_MMD_PCS, MDIO_PCS_10GBRT_STAT1);
473 return reg & MDIO_PCS_10GBRT_STAT1_BLKLK;
474 } else {
475 reg = efx_mdio_read(efx, MDIO_MMD_C22EXT, C22EXT_STATUS_REG);
476 return reg & (1 << C22EXT_STATUS_LINK_LBN);
480 static void tenxpress_ext_loopback(struct efx_nic *efx)
482 efx_mdio_set_flag(efx, MDIO_MMD_PHYXS, PHYXS_TEST1,
483 1 << LOOPBACK_NEAR_LBN,
484 efx->loopback_mode == LOOPBACK_PHYXS);
485 if (efx->phy_type != PHY_TYPE_SFX7101)
486 efx_mdio_set_flag(efx, MDIO_MMD_C22EXT, GPHY_XCONTROL_REG,
487 1 << GPHY_LOOPBACK_NEAR_LBN,
488 efx->loopback_mode == LOOPBACK_GPHY);
491 static void tenxpress_low_power(struct efx_nic *efx)
493 if (efx->phy_type == PHY_TYPE_SFX7101)
494 efx_mdio_set_mmds_lpower(
495 efx, !!(efx->phy_mode & PHY_MODE_LOW_POWER),
496 TENXPRESS_REQUIRED_DEVS);
497 else
498 efx_mdio_set_flag(
499 efx, MDIO_MMD_PMAPMD, PMA_PMD_XCONTROL_REG,
500 1 << PMA_PMD_EXT_LPOWER_LBN,
501 !!(efx->phy_mode & PHY_MODE_LOW_POWER));
504 static void tenxpress_phy_reconfigure(struct efx_nic *efx)
506 struct tenxpress_phy_data *phy_data = efx->phy_data;
507 struct ethtool_cmd ecmd;
508 bool phy_mode_change, loop_reset;
510 if (efx->phy_mode & (PHY_MODE_OFF | PHY_MODE_SPECIAL)) {
511 phy_data->phy_mode = efx->phy_mode;
512 return;
515 tenxpress_low_power(efx);
517 phy_mode_change = (efx->phy_mode == PHY_MODE_NORMAL &&
518 phy_data->phy_mode != PHY_MODE_NORMAL);
519 loop_reset = (LOOPBACK_OUT_OF(phy_data, efx, efx->phy_op->loopbacks) ||
520 LOOPBACK_CHANGED(phy_data, efx, 1 << LOOPBACK_GPHY));
522 if (loop_reset || phy_mode_change) {
523 int rc;
525 efx->phy_op->get_settings(efx, &ecmd);
527 if (loop_reset || phy_mode_change) {
528 tenxpress_special_reset(efx);
530 /* Reset XAUI if we were in 10G, and are staying
531 * in 10G. If we're moving into and out of 10G
532 * then xaui will be reset anyway */
533 if (EFX_IS10G(efx))
534 falcon_reset_xaui(efx);
537 rc = efx->phy_op->set_settings(efx, &ecmd);
538 WARN_ON(rc);
541 efx_mdio_transmit_disable(efx);
542 efx_mdio_phy_reconfigure(efx);
543 tenxpress_ext_loopback(efx);
545 phy_data->loopback_mode = efx->loopback_mode;
546 phy_data->phy_mode = efx->phy_mode;
548 if (efx->phy_type == PHY_TYPE_SFX7101) {
549 efx->link_speed = 10000;
550 efx->link_fd = true;
551 efx->link_up = sfx7101_link_ok(efx);
552 } else {
553 efx->phy_op->get_settings(efx, &ecmd);
554 efx->link_speed = ecmd.speed;
555 efx->link_fd = ecmd.duplex == DUPLEX_FULL;
556 efx->link_up = sft9001_link_ok(efx, &ecmd);
558 efx->link_fc = efx_mdio_get_pause(efx);
561 /* Poll PHY for interrupt */
562 static void tenxpress_phy_poll(struct efx_nic *efx)
564 struct tenxpress_phy_data *phy_data = efx->phy_data;
565 bool change = false;
567 if (efx->phy_type == PHY_TYPE_SFX7101) {
568 bool link_ok = sfx7101_link_ok(efx);
569 if (link_ok != efx->link_up) {
570 change = true;
571 } else {
572 unsigned int link_fc = efx_mdio_get_pause(efx);
573 if (link_fc != efx->link_fc)
574 change = true;
576 sfx7101_check_bad_lp(efx, link_ok);
577 } else if (efx->loopback_mode) {
578 bool link_ok = sft9001_link_ok(efx, NULL);
579 if (link_ok != efx->link_up)
580 change = true;
581 } else {
582 int status = efx_mdio_read(efx, MDIO_MMD_PMAPMD,
583 PMA_PMD_LASI_STATUS);
584 if (status & (1 << PMA_PMD_LS_ALARM_LBN))
585 change = true;
588 if (change)
589 falcon_sim_phy_event(efx);
591 if (phy_data->phy_mode != PHY_MODE_NORMAL)
592 return;
595 static void tenxpress_phy_fini(struct efx_nic *efx)
597 int reg;
599 if (efx->phy_type == PHY_TYPE_SFT9001B)
600 device_remove_file(&efx->pci_dev->dev,
601 &dev_attr_phy_short_reach);
603 if (efx->phy_type == PHY_TYPE_SFX7101) {
604 /* Power down the LNPGA */
605 reg = (1 << PMA_PMD_LNPGA_POWERDOWN_LBN);
606 efx_mdio_write(efx, MDIO_MMD_PMAPMD, PMA_PMD_XCONTROL_REG, reg);
608 /* Waiting here ensures that the board fini, which can turn
609 * off the power to the PHY, won't get run until the LNPGA
610 * powerdown has been given long enough to complete. */
611 schedule_timeout_uninterruptible(LNPGA_PDOWN_WAIT); /* 200 ms */
614 kfree(efx->phy_data);
615 efx->phy_data = NULL;
619 /* Set the RX and TX LEDs and Link LED flashing. The other LEDs
620 * (which probably aren't wired anyway) are left in AUTO mode */
621 void tenxpress_phy_blink(struct efx_nic *efx, bool blink)
623 int reg;
625 if (blink)
626 reg = (PMA_PMD_LED_FLASH << PMA_PMD_LED_TX_LBN) |
627 (PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN) |
628 (PMA_PMD_LED_FLASH << PMA_PMD_LED_LINK_LBN);
629 else
630 reg = PMA_PMD_LED_DEFAULT;
632 efx_mdio_write(efx, MDIO_MMD_PMAPMD, PMA_PMD_LED_OVERR_REG, reg);
635 static const char *const sfx7101_test_names[] = {
636 "bist"
639 static int
640 sfx7101_run_tests(struct efx_nic *efx, int *results, unsigned flags)
642 int rc;
644 if (!(flags & ETH_TEST_FL_OFFLINE))
645 return 0;
647 /* BIST is automatically run after a special software reset */
648 rc = tenxpress_special_reset(efx);
649 results[0] = rc ? -1 : 1;
650 return rc;
653 static const char *const sft9001_test_names[] = {
654 "bist",
655 "cable.pairA.status",
656 "cable.pairB.status",
657 "cable.pairC.status",
658 "cable.pairD.status",
659 "cable.pairA.length",
660 "cable.pairB.length",
661 "cable.pairC.length",
662 "cable.pairD.length",
665 static int sft9001_run_tests(struct efx_nic *efx, int *results, unsigned flags)
667 struct ethtool_cmd ecmd;
668 int rc = 0, rc2, i, ctrl_reg, res_reg;
670 if (flags & ETH_TEST_FL_OFFLINE)
671 efx->phy_op->get_settings(efx, &ecmd);
673 /* Initialise cable diagnostic results to unknown failure */
674 for (i = 1; i < 9; ++i)
675 results[i] = -1;
677 /* Run cable diagnostics; wait up to 5 seconds for them to complete.
678 * A cable fault is not a self-test failure, but a timeout is. */
679 ctrl_reg = ((1 << CDIAG_CTRL_IMMED_LBN) |
680 (CDIAG_CTRL_LEN_METRES << CDIAG_CTRL_LEN_UNIT_LBN));
681 if (flags & ETH_TEST_FL_OFFLINE) {
682 /* Break the link in order to run full diagnostics. We
683 * must reset the PHY to resume normal service. */
684 ctrl_reg |= (1 << CDIAG_CTRL_BRK_LINK_LBN);
686 efx_mdio_write(efx, MDIO_MMD_PMAPMD, PMA_PMD_CDIAG_CTRL_REG,
687 ctrl_reg);
688 i = 0;
689 while (efx_mdio_read(efx, MDIO_MMD_PMAPMD, PMA_PMD_CDIAG_CTRL_REG) &
690 (1 << CDIAG_CTRL_IN_PROG_LBN)) {
691 if (++i == 50) {
692 rc = -ETIMEDOUT;
693 goto out;
695 msleep(100);
697 res_reg = efx_mdio_read(efx, MDIO_MMD_PMAPMD, PMA_PMD_CDIAG_RES_REG);
698 for (i = 0; i < 4; i++) {
699 int pair_res =
700 (res_reg >> (CDIAG_RES_A_LBN - i * CDIAG_RES_WIDTH))
701 & ((1 << CDIAG_RES_WIDTH) - 1);
702 int len_reg = efx_mdio_read(efx, MDIO_MMD_PMAPMD,
703 PMA_PMD_CDIAG_LEN_REG + i);
704 if (pair_res == CDIAG_RES_OK)
705 results[1 + i] = 1;
706 else if (pair_res == CDIAG_RES_INVALID)
707 results[1 + i] = -1;
708 else
709 results[1 + i] = -pair_res;
710 if (pair_res != CDIAG_RES_INVALID &&
711 pair_res != CDIAG_RES_OPEN &&
712 len_reg != 0xffff)
713 results[5 + i] = len_reg;
716 out:
717 if (flags & ETH_TEST_FL_OFFLINE) {
718 /* Reset, running the BIST and then resuming normal service. */
719 rc2 = tenxpress_special_reset(efx);
720 results[0] = rc2 ? -1 : 1;
721 if (!rc)
722 rc = rc2;
724 rc2 = efx->phy_op->set_settings(efx, &ecmd);
725 if (!rc)
726 rc = rc2;
729 return rc;
732 static void
733 tenxpress_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
735 u32 adv = 0, lpa = 0;
736 int reg;
738 if (efx->phy_type != PHY_TYPE_SFX7101) {
739 reg = efx_mdio_read(efx, MDIO_MMD_C22EXT, C22EXT_MSTSLV_CTRL);
740 if (reg & (1 << C22EXT_MSTSLV_CTRL_ADV_1000_FD_LBN))
741 adv |= ADVERTISED_1000baseT_Full;
742 reg = efx_mdio_read(efx, MDIO_MMD_C22EXT, C22EXT_MSTSLV_STATUS);
743 if (reg & (1 << C22EXT_MSTSLV_STATUS_LP_1000_HD_LBN))
744 lpa |= ADVERTISED_1000baseT_Half;
745 if (reg & (1 << C22EXT_MSTSLV_STATUS_LP_1000_FD_LBN))
746 lpa |= ADVERTISED_1000baseT_Full;
748 reg = efx_mdio_read(efx, MDIO_MMD_AN, MDIO_AN_10GBT_CTRL);
749 if (reg & MDIO_AN_10GBT_CTRL_ADV10G)
750 adv |= ADVERTISED_10000baseT_Full;
751 reg = efx_mdio_read(efx, MDIO_MMD_AN, MDIO_AN_10GBT_STAT);
752 if (reg & MDIO_AN_10GBT_STAT_LP10G)
753 lpa |= ADVERTISED_10000baseT_Full;
755 mdio45_ethtool_gset_npage(&efx->mdio, ecmd, adv, lpa);
757 if (efx->phy_type != PHY_TYPE_SFX7101)
758 ecmd->supported |= (SUPPORTED_100baseT_Full |
759 SUPPORTED_1000baseT_Full);
761 /* In loopback, the PHY automatically brings up the correct interface,
762 * but doesn't advertise the correct speed. So override it */
763 if (efx->loopback_mode == LOOPBACK_GPHY)
764 ecmd->speed = SPEED_1000;
765 else if (LOOPBACK_MASK(efx) & efx->phy_op->loopbacks)
766 ecmd->speed = SPEED_10000;
769 static int tenxpress_set_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
771 if (!ecmd->autoneg)
772 return -EINVAL;
774 return efx_mdio_set_settings(efx, ecmd);
777 static void sfx7101_set_npage_adv(struct efx_nic *efx, u32 advertising)
779 efx_mdio_set_flag(efx, MDIO_MMD_AN, MDIO_AN_10GBT_CTRL,
780 MDIO_AN_10GBT_CTRL_ADV10G,
781 advertising & ADVERTISED_10000baseT_Full);
784 static void sft9001_set_npage_adv(struct efx_nic *efx, u32 advertising)
786 efx_mdio_set_flag(efx, MDIO_MMD_C22EXT, C22EXT_MSTSLV_CTRL,
787 1 << C22EXT_MSTSLV_CTRL_ADV_1000_FD_LBN,
788 advertising & ADVERTISED_1000baseT_Full);
789 efx_mdio_set_flag(efx, MDIO_MMD_AN, MDIO_AN_10GBT_CTRL,
790 MDIO_AN_10GBT_CTRL_ADV10G,
791 advertising & ADVERTISED_10000baseT_Full);
794 struct efx_phy_operations falcon_sfx7101_phy_ops = {
795 .macs = EFX_XMAC,
796 .init = tenxpress_phy_init,
797 .reconfigure = tenxpress_phy_reconfigure,
798 .poll = tenxpress_phy_poll,
799 .fini = tenxpress_phy_fini,
800 .clear_interrupt = efx_port_dummy_op_void,
801 .get_settings = tenxpress_get_settings,
802 .set_settings = tenxpress_set_settings,
803 .set_npage_adv = sfx7101_set_npage_adv,
804 .num_tests = ARRAY_SIZE(sfx7101_test_names),
805 .test_names = sfx7101_test_names,
806 .run_tests = sfx7101_run_tests,
807 .mmds = TENXPRESS_REQUIRED_DEVS,
808 .loopbacks = SFX7101_LOOPBACKS,
811 struct efx_phy_operations falcon_sft9001_phy_ops = {
812 .macs = EFX_GMAC | EFX_XMAC,
813 .init = tenxpress_phy_init,
814 .reconfigure = tenxpress_phy_reconfigure,
815 .poll = tenxpress_phy_poll,
816 .fini = tenxpress_phy_fini,
817 .clear_interrupt = efx_port_dummy_op_void,
818 .get_settings = tenxpress_get_settings,
819 .set_settings = tenxpress_set_settings,
820 .set_npage_adv = sft9001_set_npage_adv,
821 .num_tests = ARRAY_SIZE(sft9001_test_names),
822 .test_names = sft9001_test_names,
823 .run_tests = sft9001_run_tests,
824 .mmds = TENXPRESS_REQUIRED_DEVS,
825 .loopbacks = SFT9001_LOOPBACKS,