1 /****************************************************************************
2 * Driver for Solarflare 802.3an compliant PHY
3 * Copyright 2007 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 #include <linux/delay.h>
11 #include <linux/seq_file.h>
16 #include "falcon_hwdefs.h"
20 /* We expect these MMDs to be in the package */
21 /* AN not here as mdio_check_mmds() requires STAT2 support */
22 #define TENXPRESS_REQUIRED_DEVS (MDIO_MMDREG_DEVS0_PMAPMD | \
23 MDIO_MMDREG_DEVS0_PCS | \
24 MDIO_MMDREG_DEVS0_PHYXS)
26 #define TENXPRESS_LOOPBACKS ((1 << LOOPBACK_PHYXS) | \
27 (1 << LOOPBACK_PCS) | \
28 (1 << LOOPBACK_PMAPMD) | \
29 (1 << LOOPBACK_NETWORK))
31 /* We complain if we fail to see the link partner as 10G capable this many
32 * times in a row (must be > 1 as sampling the autoneg. registers is racy)
34 #define MAX_BAD_LP_TRIES (5)
36 /* Extended control register */
37 #define PMA_PMD_XCONTROL_REG 0xc000
38 #define PMA_PMD_LNPGA_POWERDOWN_LBN 8
39 #define PMA_PMD_LNPGA_POWERDOWN_WIDTH 1
41 /* extended status register */
42 #define PMA_PMD_XSTATUS_REG 0xc001
43 #define PMA_PMD_XSTAT_FLP_LBN (12)
45 /* LED control register */
46 #define PMA_PMD_LED_CTRL_REG (0xc007)
47 #define PMA_PMA_LED_ACTIVITY_LBN (3)
49 /* LED function override register */
50 #define PMA_PMD_LED_OVERR_REG (0xc009)
51 /* Bit positions for different LEDs (there are more but not wired on SFE4001)*/
52 #define PMA_PMD_LED_LINK_LBN (0)
53 #define PMA_PMD_LED_SPEED_LBN (2)
54 #define PMA_PMD_LED_TX_LBN (4)
55 #define PMA_PMD_LED_RX_LBN (6)
56 /* Override settings */
57 #define PMA_PMD_LED_AUTO (0) /* H/W control */
58 #define PMA_PMD_LED_ON (1)
59 #define PMA_PMD_LED_OFF (2)
60 #define PMA_PMD_LED_FLASH (3)
61 /* All LEDs under hardware control */
62 #define PMA_PMD_LED_FULL_AUTO (0)
63 /* Green and Amber under hardware control, Red off */
64 #define PMA_PMD_LED_DEFAULT (PMA_PMD_LED_OFF << PMA_PMD_LED_RX_LBN)
67 /* Special Software reset register */
68 #define PMA_PMD_EXT_CTRL_REG 49152
69 #define PMA_PMD_EXT_SSR_LBN 15
71 /* Misc register defines */
72 #define PCS_CLOCK_CTRL_REG 0xd801
73 #define PLL312_RST_N_LBN 2
75 #define PCS_SOFT_RST2_REG 0xd806
76 #define SERDES_RST_N_LBN 13
77 #define XGXS_RST_N_LBN 12
79 #define PCS_TEST_SELECT_REG 0xd807 /* PRM 10.5.8 */
80 #define CLK312_EN_LBN 3
83 #define PHYXS_TEST1 (49162)
84 #define LOOPBACK_NEAR_LBN (8)
85 #define LOOPBACK_NEAR_WIDTH (1)
87 /* Boot status register */
88 #define PCS_BOOT_STATUS_REG (0xd000)
89 #define PCS_BOOT_FATAL_ERR_LBN (0)
90 #define PCS_BOOT_PROGRESS_LBN (1)
91 #define PCS_BOOT_PROGRESS_WIDTH (2)
92 #define PCS_BOOT_COMPLETE_LBN (3)
93 #define PCS_BOOT_MAX_DELAY (100)
94 #define PCS_BOOT_POLL_DELAY (10)
96 /* Time to wait between powering down the LNPGA and turning off the power
98 #define LNPGA_PDOWN_WAIT (HZ / 5)
100 static int crc_error_reset_threshold = 100;
101 module_param(crc_error_reset_threshold, int, 0644);
102 MODULE_PARM_DESC(crc_error_reset_threshold,
103 "Max number of CRC errors before XAUI reset");
105 struct tenxpress_phy_data {
106 enum efx_loopback_mode loopback_mode;
107 atomic_t bad_crc_count;
108 enum efx_phy_mode phy_mode;
112 void tenxpress_crc_err(struct efx_nic *efx)
114 struct tenxpress_phy_data *phy_data = efx->phy_data;
115 if (phy_data != NULL)
116 atomic_inc(&phy_data->bad_crc_count);
119 /* Check that the C166 has booted successfully */
120 static int tenxpress_phy_check(struct efx_nic *efx)
122 int phy_id = efx->mii.phy_id;
123 int count = PCS_BOOT_MAX_DELAY / PCS_BOOT_POLL_DELAY;
126 /* Wait for the boot to complete (or not) */
128 boot_stat = mdio_clause45_read(efx, phy_id,
130 PCS_BOOT_STATUS_REG);
131 if (boot_stat & (1 << PCS_BOOT_COMPLETE_LBN))
134 udelay(PCS_BOOT_POLL_DELAY);
138 EFX_ERR(efx, "%s: PHY boot timed out. Last status "
140 (boot_stat >> PCS_BOOT_PROGRESS_LBN) &
141 ((1 << PCS_BOOT_PROGRESS_WIDTH) - 1));
148 static int tenxpress_init(struct efx_nic *efx)
152 /* Turn on the clock */
153 reg = (1 << CLK312_EN_LBN);
154 mdio_clause45_write(efx, efx->mii.phy_id,
155 MDIO_MMD_PCS, PCS_TEST_SELECT_REG, reg);
157 rc = tenxpress_phy_check(efx);
161 /* Set the LEDs up as: Green = Link, Amber = Link/Act, Red = Off */
162 reg = mdio_clause45_read(efx, efx->mii.phy_id,
163 MDIO_MMD_PMAPMD, PMA_PMD_LED_CTRL_REG);
164 reg |= (1 << PMA_PMA_LED_ACTIVITY_LBN);
165 mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
166 PMA_PMD_LED_CTRL_REG, reg);
168 reg = PMA_PMD_LED_DEFAULT;
169 mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
170 PMA_PMD_LED_OVERR_REG, reg);
175 static int tenxpress_phy_init(struct efx_nic *efx)
177 struct tenxpress_phy_data *phy_data;
180 phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
183 efx->phy_data = phy_data;
184 phy_data->phy_mode = efx->phy_mode;
186 rc = mdio_clause45_wait_reset_mmds(efx,
187 TENXPRESS_REQUIRED_DEVS);
191 rc = mdio_clause45_check_mmds(efx, TENXPRESS_REQUIRED_DEVS, 0);
195 rc = tenxpress_init(efx);
199 schedule_timeout_uninterruptible(HZ / 5); /* 200ms */
201 /* Let XGXS and SerDes out of reset and resets 10XPress */
202 falcon_reset_xaui(efx);
207 kfree(efx->phy_data);
208 efx->phy_data = NULL;
212 static int tenxpress_special_reset(struct efx_nic *efx)
216 /* The XGMAC clock is driven from the SFC7101/SFT9001 312MHz clock, so
217 * a special software reset can glitch the XGMAC sufficiently for stats
218 * requests to fail. Since we don't ofen special_reset, just lock. */
219 spin_lock(&efx->stats_lock);
222 reg = mdio_clause45_read(efx, efx->mii.phy_id,
223 MDIO_MMD_PMAPMD, PMA_PMD_EXT_CTRL_REG);
224 reg |= (1 << PMA_PMD_EXT_SSR_LBN);
225 mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
226 PMA_PMD_EXT_CTRL_REG, reg);
230 /* Wait for the blocks to come out of reset */
231 rc = mdio_clause45_wait_reset_mmds(efx,
232 TENXPRESS_REQUIRED_DEVS);
236 /* Try and reconfigure the device */
237 rc = tenxpress_init(efx);
242 spin_unlock(&efx->stats_lock);
246 static void tenxpress_set_bad_lp(struct efx_nic *efx, bool bad_lp)
248 struct tenxpress_phy_data *pd = efx->phy_data;
251 /* Nothing to do if all is well and was previously so. */
252 if (!(bad_lp || pd->bad_lp_tries))
255 reg = mdio_clause45_read(efx, efx->mii.phy_id,
256 MDIO_MMD_PMAPMD, PMA_PMD_LED_OVERR_REG);
261 pd->bad_lp_tries = 0;
263 if (pd->bad_lp_tries == MAX_BAD_LP_TRIES) {
264 pd->bad_lp_tries = 0; /* Restart count */
265 reg &= ~(PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN);
266 reg |= (PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN);
267 EFX_ERR(efx, "This NIC appears to be plugged into"
268 " a port that is not 10GBASE-T capable.\n"
269 " This PHY is 10GBASE-T ONLY, so no link can"
270 " be established.\n");
272 reg |= (PMA_PMD_LED_OFF << PMA_PMD_LED_RX_LBN);
274 mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
275 PMA_PMD_LED_OVERR_REG, reg);
278 /* Check link status and return a boolean OK value. If the link is NOT
279 * OK we have a quick rummage round to see if we appear to be plugged
280 * into a non-10GBT port and if so warn the user that they won't get
281 * link any time soon as we are 10GBT only, unless caller specified
282 * not to do this check (it isn't useful in loopback) */
283 static bool tenxpress_link_ok(struct efx_nic *efx, bool check_lp)
285 bool ok = mdio_clause45_links_ok(efx, TENXPRESS_REQUIRED_DEVS);
288 tenxpress_set_bad_lp(efx, false);
289 } else if (check_lp) {
290 /* Are we plugged into the wrong sort of link? */
292 int phy_id = efx->mii.phy_id;
293 int an_stat = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN,
295 int xphy_stat = mdio_clause45_read(efx, phy_id,
297 PMA_PMD_XSTATUS_REG);
298 /* Are we plugged into anything that sends FLPs? If
299 * not we can't distinguish between not being plugged
300 * in and being plugged into a non-AN antique. The FLP
301 * bit has the advantage of not clearing when autoneg
303 if (!(xphy_stat & (1 << PMA_PMD_XSTAT_FLP_LBN))) {
304 tenxpress_set_bad_lp(efx, false);
308 /* If it can do 10GBT it must be XNP capable */
309 bad_lp = !(an_stat & (1 << MDIO_AN_STATUS_XNP_LBN));
310 if (!bad_lp && (an_stat & (1 << MDIO_AN_STATUS_PAGE_LBN))) {
311 bad_lp = !(mdio_clause45_read(efx, phy_id,
312 MDIO_MMD_AN, MDIO_AN_10GBT_STATUS) &
313 (1 << MDIO_AN_10GBT_STATUS_LP_10G_LBN));
315 tenxpress_set_bad_lp(efx, bad_lp);
320 static void tenxpress_phyxs_loopback(struct efx_nic *efx)
322 int phy_id = efx->mii.phy_id;
325 ctrl1 = ctrl2 = mdio_clause45_read(efx, phy_id, MDIO_MMD_PHYXS,
327 if (efx->loopback_mode == LOOPBACK_PHYXS)
328 ctrl2 |= (1 << LOOPBACK_NEAR_LBN);
330 ctrl2 &= ~(1 << LOOPBACK_NEAR_LBN);
332 mdio_clause45_write(efx, phy_id, MDIO_MMD_PHYXS,
336 static void tenxpress_phy_reconfigure(struct efx_nic *efx)
338 struct tenxpress_phy_data *phy_data = efx->phy_data;
339 bool loop_change = LOOPBACK_OUT_OF(phy_data, efx,
340 TENXPRESS_LOOPBACKS);
342 if (efx->phy_mode & PHY_MODE_SPECIAL) {
343 phy_data->phy_mode = efx->phy_mode;
347 /* When coming out of transmit disable, coming out of low power
348 * mode, or moving out of any PHY internal loopback mode,
349 * perform a special software reset */
350 if ((efx->phy_mode == PHY_MODE_NORMAL &&
351 phy_data->phy_mode != PHY_MODE_NORMAL) ||
353 tenxpress_special_reset(efx);
354 falcon_reset_xaui(efx);
357 mdio_clause45_transmit_disable(efx);
358 mdio_clause45_phy_reconfigure(efx);
359 tenxpress_phyxs_loopback(efx);
361 phy_data->loopback_mode = efx->loopback_mode;
362 phy_data->phy_mode = efx->phy_mode;
363 efx->link_up = tenxpress_link_ok(efx, false);
364 efx->link_speed = 10000;
368 static void tenxpress_phy_clear_interrupt(struct efx_nic *efx)
370 /* Nothing done here - LASI interrupts aren't reliable so poll */
374 /* Poll PHY for interrupt */
375 static int tenxpress_phy_check_hw(struct efx_nic *efx)
377 struct tenxpress_phy_data *phy_data = efx->phy_data;
381 link_ok = tenxpress_link_ok(efx, true);
383 if (link_ok != efx->link_up)
384 falcon_xmac_sim_phy_event(efx);
386 if (phy_data->phy_mode != PHY_MODE_NORMAL)
389 if (atomic_read(&phy_data->bad_crc_count) > crc_error_reset_threshold) {
390 EFX_ERR(efx, "Resetting XAUI due to too many CRC errors\n");
391 falcon_reset_xaui(efx);
392 atomic_set(&phy_data->bad_crc_count, 0);
395 rc = efx->board_info.monitor(efx);
397 EFX_ERR(efx, "Board sensor %s; shutting down PHY\n",
398 (rc == -ERANGE) ? "reported fault" : "failed");
399 if (efx->phy_mode & PHY_MODE_OFF) {
400 /* Assume that board has shut PHY off */
401 phy_data->phy_mode = PHY_MODE_OFF;
403 efx->phy_mode |= PHY_MODE_LOW_POWER;
404 mdio_clause45_set_mmds_lpower(efx, true,
406 phy_data->phy_mode |= PHY_MODE_LOW_POWER;
413 static void tenxpress_phy_fini(struct efx_nic *efx)
417 /* Power down the LNPGA */
418 reg = (1 << PMA_PMD_LNPGA_POWERDOWN_LBN);
419 mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
420 PMA_PMD_XCONTROL_REG, reg);
422 /* Waiting here ensures that the board fini, which can turn off the
423 * power to the PHY, won't get run until the LNPGA powerdown has been
424 * given long enough to complete. */
425 schedule_timeout_uninterruptible(LNPGA_PDOWN_WAIT); /* 200 ms */
427 kfree(efx->phy_data);
428 efx->phy_data = NULL;
432 /* Set the RX and TX LEDs and Link LED flashing. The other LEDs
433 * (which probably aren't wired anyway) are left in AUTO mode */
434 void tenxpress_phy_blink(struct efx_nic *efx, bool blink)
439 reg = (PMA_PMD_LED_FLASH << PMA_PMD_LED_TX_LBN) |
440 (PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN) |
441 (PMA_PMD_LED_FLASH << PMA_PMD_LED_LINK_LBN);
443 reg = PMA_PMD_LED_DEFAULT;
445 mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
446 PMA_PMD_LED_OVERR_REG, reg);
449 static int tenxpress_phy_test(struct efx_nic *efx)
451 /* BIST is automatically run after a special software reset */
452 return tenxpress_special_reset(efx);
455 struct efx_phy_operations falcon_tenxpress_phy_ops = {
456 .init = tenxpress_phy_init,
457 .reconfigure = tenxpress_phy_reconfigure,
458 .check_hw = tenxpress_phy_check_hw,
459 .fini = tenxpress_phy_fini,
460 .clear_interrupt = tenxpress_phy_clear_interrupt,
461 .test = tenxpress_phy_test,
462 .mmds = TENXPRESS_REQUIRED_DEVS,
463 .loopbacks = TENXPRESS_LOOPBACKS,