blob: 1567ab538e273a4af2ba6d0d67e08448fdec6775 [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
Ben Hutchings177dfcd2008-12-12 21:50:08 -08002 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2007-2008 Solarflare Communications Inc.
Ben Hutchings8ceee662008-04-27 12:55:59 +01004 *
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#include <linux/delay.h>
11#include <linux/seq_file.h>
12#include "efx.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010013#include "mdio_10g.h"
14#include "falcon.h"
15#include "phy.h"
16#include "falcon_hwdefs.h"
17#include "boards.h"
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -080018#include "workarounds.h"
19#include "selftest.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010020
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -080021/* We expect these MMDs to be in the package. SFT9001 also has a
22 * clause 22 extension MMD, but since it doesn't have all the generic
23 * MMD registers it is pointless to include it here.
24 */
Ben Hutchings27dd2ca2008-12-12 21:44:14 -080025#define TENXPRESS_REQUIRED_DEVS (MDIO_MMDREG_DEVS_PMAPMD | \
26 MDIO_MMDREG_DEVS_PCS | \
Ben Hutchings04cc8ca2008-12-12 21:50:46 -080027 MDIO_MMDREG_DEVS_PHYXS | \
28 MDIO_MMDREG_DEVS_AN)
Ben Hutchings8ceee662008-04-27 12:55:59 +010029
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -080030#define SFX7101_LOOPBACKS ((1 << LOOPBACK_PHYXS) | \
31 (1 << LOOPBACK_PCS) | \
32 (1 << LOOPBACK_PMAPMD) | \
33 (1 << LOOPBACK_NETWORK))
34
35#define SFT9001_LOOPBACKS ((1 << LOOPBACK_GPHY) | \
36 (1 << LOOPBACK_PHYXS) | \
37 (1 << LOOPBACK_PCS) | \
38 (1 << LOOPBACK_PMAPMD) | \
39 (1 << LOOPBACK_NETWORK))
Ben Hutchings3273c2e2008-05-07 13:36:19 +010040
Ben Hutchings8ceee662008-04-27 12:55:59 +010041/* We complain if we fail to see the link partner as 10G capable this many
42 * times in a row (must be > 1 as sampling the autoneg. registers is racy)
43 */
44#define MAX_BAD_LP_TRIES (5)
45
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -080046/* LASI Control */
47#define PMA_PMD_LASI_CTRL 36866
48#define PMA_PMD_LASI_STATUS 36869
49#define PMA_PMD_LS_ALARM_LBN 0
50#define PMA_PMD_LS_ALARM_WIDTH 1
51#define PMA_PMD_TX_ALARM_LBN 1
52#define PMA_PMD_TX_ALARM_WIDTH 1
53#define PMA_PMD_RX_ALARM_LBN 2
54#define PMA_PMD_RX_ALARM_WIDTH 1
55#define PMA_PMD_AN_ALARM_LBN 3
56#define PMA_PMD_AN_ALARM_WIDTH 1
57
Ben Hutchings8ceee662008-04-27 12:55:59 +010058/* Extended control register */
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -080059#define PMA_PMD_XCONTROL_REG 49152
60#define PMA_PMD_EXT_GMII_EN_LBN 1
61#define PMA_PMD_EXT_GMII_EN_WIDTH 1
62#define PMA_PMD_EXT_CLK_OUT_LBN 2
63#define PMA_PMD_EXT_CLK_OUT_WIDTH 1
64#define PMA_PMD_LNPGA_POWERDOWN_LBN 8 /* SFX7101 only */
65#define PMA_PMD_LNPGA_POWERDOWN_WIDTH 1
66#define PMA_PMD_EXT_CLK312_LBN 8 /* SFT9001 only */
67#define PMA_PMD_EXT_CLK312_WIDTH 1
68#define PMA_PMD_EXT_LPOWER_LBN 12
69#define PMA_PMD_EXT_LPOWER_WIDTH 1
70#define PMA_PMD_EXT_SSR_LBN 15
71#define PMA_PMD_EXT_SSR_WIDTH 1
Ben Hutchings8ceee662008-04-27 12:55:59 +010072
73/* extended status register */
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -080074#define PMA_PMD_XSTATUS_REG 49153
Ben Hutchings8ceee662008-04-27 12:55:59 +010075#define PMA_PMD_XSTAT_FLP_LBN (12)
76
77/* LED control register */
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -080078#define PMA_PMD_LED_CTRL_REG 49159
Ben Hutchings8ceee662008-04-27 12:55:59 +010079#define PMA_PMA_LED_ACTIVITY_LBN (3)
80
81/* LED function override register */
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -080082#define PMA_PMD_LED_OVERR_REG 49161
Ben Hutchings8ceee662008-04-27 12:55:59 +010083/* Bit positions for different LEDs (there are more but not wired on SFE4001)*/
84#define PMA_PMD_LED_LINK_LBN (0)
85#define PMA_PMD_LED_SPEED_LBN (2)
86#define PMA_PMD_LED_TX_LBN (4)
87#define PMA_PMD_LED_RX_LBN (6)
88/* Override settings */
89#define PMA_PMD_LED_AUTO (0) /* H/W control */
90#define PMA_PMD_LED_ON (1)
91#define PMA_PMD_LED_OFF (2)
92#define PMA_PMD_LED_FLASH (3)
Ben Hutchings04cc8ca2008-12-12 21:50:46 -080093#define PMA_PMD_LED_MASK 3
Ben Hutchings8ceee662008-04-27 12:55:59 +010094/* All LEDs under hardware control */
95#define PMA_PMD_LED_FULL_AUTO (0)
96/* Green and Amber under hardware control, Red off */
97#define PMA_PMD_LED_DEFAULT (PMA_PMD_LED_OFF << PMA_PMD_LED_RX_LBN)
98
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -080099#define PMA_PMD_SPEED_ENABLE_REG 49192
100#define PMA_PMD_100TX_ADV_LBN 1
101#define PMA_PMD_100TX_ADV_WIDTH 1
102#define PMA_PMD_1000T_ADV_LBN 2
103#define PMA_PMD_1000T_ADV_WIDTH 1
104#define PMA_PMD_10000T_ADV_LBN 3
105#define PMA_PMD_10000T_ADV_WIDTH 1
106#define PMA_PMD_SPEED_LBN 4
107#define PMA_PMD_SPEED_WIDTH 4
Ben Hutchings8ceee662008-04-27 12:55:59 +0100108
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800109/* Serdes control registers - SFT9001 only */
110#define PMA_PMD_CSERDES_CTRL_REG 64258
111/* Set the 156.25 MHz output to 312.5 MHz to drive Falcon's XMAC */
112#define PMA_PMD_CSERDES_DEFAULT 0x000f
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100113
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800114/* Misc register defines - SFX7101 only */
115#define PCS_CLOCK_CTRL_REG 55297
Ben Hutchings8ceee662008-04-27 12:55:59 +0100116#define PLL312_RST_N_LBN 2
117
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800118#define PCS_SOFT_RST2_REG 55302
Ben Hutchings8ceee662008-04-27 12:55:59 +0100119#define SERDES_RST_N_LBN 13
120#define XGXS_RST_N_LBN 12
121
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800122#define PCS_TEST_SELECT_REG 55303 /* PRM 10.5.8 */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100123#define CLK312_EN_LBN 3
124
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100125/* PHYXS registers */
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800126#define PHYXS_XCONTROL_REG 49152
127#define PHYXS_RESET_LBN 15
128#define PHYXS_RESET_WIDTH 1
129
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100130#define PHYXS_TEST1 (49162)
131#define LOOPBACK_NEAR_LBN (8)
132#define LOOPBACK_NEAR_WIDTH (1)
133
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800134#define PCS_10GBASET_STAT1 32
135#define PCS_10GBASET_BLKLK_LBN 0
136#define PCS_10GBASET_BLKLK_WIDTH 1
137
Ben Hutchings8ceee662008-04-27 12:55:59 +0100138/* Boot status register */
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800139#define PCS_BOOT_STATUS_REG 53248
Ben Hutchings8ceee662008-04-27 12:55:59 +0100140#define PCS_BOOT_FATAL_ERR_LBN (0)
141#define PCS_BOOT_PROGRESS_LBN (1)
142#define PCS_BOOT_PROGRESS_WIDTH (2)
143#define PCS_BOOT_COMPLETE_LBN (3)
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800144
Ben Hutchings8ceee662008-04-27 12:55:59 +0100145#define PCS_BOOT_MAX_DELAY (100)
146#define PCS_BOOT_POLL_DELAY (10)
147
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800148/* 100M/1G PHY registers */
149#define GPHY_XCONTROL_REG 49152
150#define GPHY_ISOLATE_LBN 10
151#define GPHY_ISOLATE_WIDTH 1
152#define GPHY_DUPLEX_LBN 8
153#define GPHY_DUPLEX_WIDTH 1
154#define GPHY_LOOPBACK_NEAR_LBN 14
155#define GPHY_LOOPBACK_NEAR_WIDTH 1
156
157#define C22EXT_STATUS_REG 49153
158#define C22EXT_STATUS_LINK_LBN 2
159#define C22EXT_STATUS_LINK_WIDTH 1
160
161#define C22EXT_MSTSLV_REG 49162
162#define C22EXT_MSTSLV_1000_HD_LBN 10
163#define C22EXT_MSTSLV_1000_HD_WIDTH 1
164#define C22EXT_MSTSLV_1000_FD_LBN 11
165#define C22EXT_MSTSLV_1000_FD_WIDTH 1
166
Ben Hutchings8ceee662008-04-27 12:55:59 +0100167/* Time to wait between powering down the LNPGA and turning off the power
168 * rails */
169#define LNPGA_PDOWN_WAIT (HZ / 5)
170
171static int crc_error_reset_threshold = 100;
172module_param(crc_error_reset_threshold, int, 0644);
173MODULE_PARM_DESC(crc_error_reset_threshold,
174 "Max number of CRC errors before XAUI reset");
175
176struct tenxpress_phy_data {
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100177 enum efx_loopback_mode loopback_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100178 atomic_t bad_crc_count;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +0100179 enum efx_phy_mode phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100180 int bad_lp_tries;
181};
182
Ben Hutchings8ceee662008-04-27 12:55:59 +0100183void tenxpress_crc_err(struct efx_nic *efx)
184{
185 struct tenxpress_phy_data *phy_data = efx->phy_data;
186 if (phy_data != NULL)
187 atomic_inc(&phy_data->bad_crc_count);
188}
189
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800190static ssize_t show_phy_short_reach(struct device *dev,
191 struct device_attribute *attr, char *buf)
192{
193 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
194 int reg;
195
196 reg = mdio_clause45_read(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
197 MDIO_PMAPMD_10GBT_TXPWR);
198 return sprintf(buf, "%d\n",
199 !!(reg & (1 << MDIO_PMAPMD_10GBT_TXPWR_SHORT_LBN)));
200}
201
202static ssize_t set_phy_short_reach(struct device *dev,
203 struct device_attribute *attr,
204 const char *buf, size_t count)
205{
206 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
207
208 rtnl_lock();
209 mdio_clause45_set_flag(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
210 MDIO_PMAPMD_10GBT_TXPWR,
211 MDIO_PMAPMD_10GBT_TXPWR_SHORT_LBN,
212 count != 0 && *buf != '0');
213 efx_reconfigure_port(efx);
214 rtnl_unlock();
215
216 return count;
217}
218
219static DEVICE_ATTR(phy_short_reach, 0644, show_phy_short_reach,
220 set_phy_short_reach);
221
Ben Hutchings8ceee662008-04-27 12:55:59 +0100222/* Check that the C166 has booted successfully */
223static int tenxpress_phy_check(struct efx_nic *efx)
224{
225 int phy_id = efx->mii.phy_id;
226 int count = PCS_BOOT_MAX_DELAY / PCS_BOOT_POLL_DELAY;
227 int boot_stat;
228
229 /* Wait for the boot to complete (or not) */
230 while (count) {
231 boot_stat = mdio_clause45_read(efx, phy_id,
232 MDIO_MMD_PCS,
233 PCS_BOOT_STATUS_REG);
234 if (boot_stat & (1 << PCS_BOOT_COMPLETE_LBN))
235 break;
236 count--;
237 udelay(PCS_BOOT_POLL_DELAY);
238 }
239
240 if (!count) {
241 EFX_ERR(efx, "%s: PHY boot timed out. Last status "
242 "%x\n", __func__,
243 (boot_stat >> PCS_BOOT_PROGRESS_LBN) &
244 ((1 << PCS_BOOT_PROGRESS_WIDTH) - 1));
245 return -ETIMEDOUT;
246 }
247
248 return 0;
249}
250
Ben Hutchings8ceee662008-04-27 12:55:59 +0100251static int tenxpress_init(struct efx_nic *efx)
252{
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800253 int phy_id = efx->mii.phy_id;
254 int reg;
255 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100256
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800257 if (efx->phy_type == PHY_TYPE_SFX7101) {
258 /* Enable 312.5 MHz clock */
259 mdio_clause45_write(efx, phy_id,
260 MDIO_MMD_PCS, PCS_TEST_SELECT_REG,
261 1 << CLK312_EN_LBN);
262 } else {
263 /* Enable 312.5 MHz clock and GMII */
264 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
265 PMA_PMD_XCONTROL_REG);
266 reg |= ((1 << PMA_PMD_EXT_GMII_EN_LBN) |
267 (1 << PMA_PMD_EXT_CLK_OUT_LBN) |
268 (1 << PMA_PMD_EXT_CLK312_LBN));
269 mdio_clause45_write(efx, phy_id, MDIO_MMD_PMAPMD,
270 PMA_PMD_XCONTROL_REG, reg);
271 mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_C22EXT,
272 GPHY_XCONTROL_REG, GPHY_ISOLATE_LBN,
273 false);
274 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100275
276 rc = tenxpress_phy_check(efx);
277 if (rc < 0)
278 return rc;
279
280 /* Set the LEDs up as: Green = Link, Amber = Link/Act, Red = Off */
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800281 if (efx->phy_type == PHY_TYPE_SFX7101) {
282 mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_PMAPMD,
283 PMA_PMD_LED_CTRL_REG,
284 PMA_PMA_LED_ACTIVITY_LBN,
285 true);
286 mdio_clause45_write(efx, phy_id, MDIO_MMD_PMAPMD,
287 PMA_PMD_LED_OVERR_REG, PMA_PMD_LED_DEFAULT);
288 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100289
290 return rc;
291}
292
293static int tenxpress_phy_init(struct efx_nic *efx)
294{
295 struct tenxpress_phy_data *phy_data;
296 int rc = 0;
297
298 phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
Ben Hutchings9b7bfc42008-05-16 21:20:20 +0100299 if (!phy_data)
300 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100301 efx->phy_data = phy_data;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +0100302 phy_data->phy_mode = efx->phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100303
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800304 if (!(efx->phy_mode & PHY_MODE_SPECIAL)) {
305 if (efx->phy_type == PHY_TYPE_SFT9001A) {
306 int reg;
307 reg = mdio_clause45_read(efx, efx->mii.phy_id,
308 MDIO_MMD_PMAPMD,
309 PMA_PMD_XCONTROL_REG);
310 reg |= (1 << PMA_PMD_EXT_SSR_LBN);
311 mdio_clause45_write(efx, efx->mii.phy_id,
312 MDIO_MMD_PMAPMD,
313 PMA_PMD_XCONTROL_REG, reg);
314 mdelay(200);
315 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100316
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800317 rc = mdio_clause45_wait_reset_mmds(efx,
318 TENXPRESS_REQUIRED_DEVS);
319 if (rc < 0)
320 goto fail;
321
322 rc = mdio_clause45_check_mmds(efx, TENXPRESS_REQUIRED_DEVS, 0);
323 if (rc < 0)
324 goto fail;
325 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100326
327 rc = tenxpress_init(efx);
328 if (rc < 0)
329 goto fail;
330
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800331 if (efx->phy_type == PHY_TYPE_SFT9001B) {
332 rc = device_create_file(&efx->pci_dev->dev,
333 &dev_attr_phy_short_reach);
334 if (rc)
335 goto fail;
336 }
337
Ben Hutchings8ceee662008-04-27 12:55:59 +0100338 schedule_timeout_uninterruptible(HZ / 5); /* 200ms */
339
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800340 /* Let XGXS and SerDes out of reset */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100341 falcon_reset_xaui(efx);
342
343 return 0;
344
345 fail:
346 kfree(efx->phy_data);
347 efx->phy_data = NULL;
348 return rc;
349}
350
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800351/* Perform a "special software reset" on the PHY. The caller is
352 * responsible for saving and restoring the PHY hardware registers
353 * properly, and masking/unmasking LASI */
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100354static int tenxpress_special_reset(struct efx_nic *efx)
355{
356 int rc, reg;
357
Ben Hutchingsc8fcc492008-09-01 12:49:25 +0100358 /* The XGMAC clock is driven from the SFC7101/SFT9001 312MHz clock, so
359 * a special software reset can glitch the XGMAC sufficiently for stats
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800360 * requests to fail. Since we don't often special_reset, just lock. */
Ben Hutchingsc8fcc492008-09-01 12:49:25 +0100361 spin_lock(&efx->stats_lock);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100362
363 /* Initiate reset */
364 reg = mdio_clause45_read(efx, efx->mii.phy_id,
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800365 MDIO_MMD_PMAPMD, PMA_PMD_XCONTROL_REG);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100366 reg |= (1 << PMA_PMD_EXT_SSR_LBN);
367 mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800368 PMA_PMD_XCONTROL_REG, reg);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100369
Ben Hutchingsc8fcc492008-09-01 12:49:25 +0100370 mdelay(200);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100371
372 /* Wait for the blocks to come out of reset */
373 rc = mdio_clause45_wait_reset_mmds(efx,
374 TENXPRESS_REQUIRED_DEVS);
375 if (rc < 0)
Ben Hutchingsc8fcc492008-09-01 12:49:25 +0100376 goto unlock;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100377
378 /* Try and reconfigure the device */
379 rc = tenxpress_init(efx);
380 if (rc < 0)
Ben Hutchingsc8fcc492008-09-01 12:49:25 +0100381 goto unlock;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100382
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800383 /* Wait for the XGXS state machine to churn */
384 mdelay(10);
Ben Hutchingsc8fcc492008-09-01 12:49:25 +0100385unlock:
386 spin_unlock(&efx->stats_lock);
387 return rc;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100388}
389
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800390static void sfx7101_check_bad_lp(struct efx_nic *efx, bool link_ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100391{
392 struct tenxpress_phy_data *pd = efx->phy_data;
Ben Hutchings04cc8ca2008-12-12 21:50:46 -0800393 int phy_id = efx->mii.phy_id;
394 bool bad_lp;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100395 int reg;
396
Ben Hutchings04cc8ca2008-12-12 21:50:46 -0800397 if (link_ok) {
398 bad_lp = false;
399 } else {
400 /* Check that AN has started but not completed. */
401 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN,
402 MDIO_AN_STATUS);
403 if (!(reg & (1 << MDIO_AN_STATUS_LP_AN_CAP_LBN)))
404 return; /* LP status is unknown */
405 bad_lp = !(reg & (1 << MDIO_AN_STATUS_AN_DONE_LBN));
406 if (bad_lp)
407 pd->bad_lp_tries++;
408 }
409
Ben Hutchings8ceee662008-04-27 12:55:59 +0100410 /* Nothing to do if all is well and was previously so. */
Ben Hutchings04cc8ca2008-12-12 21:50:46 -0800411 if (!pd->bad_lp_tries)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100412 return;
413
Ben Hutchings04cc8ca2008-12-12 21:50:46 -0800414 /* Use the RX (red) LED as an error indicator once we've seen AN
415 * failure several times in a row, and also log a message. */
416 if (!bad_lp || pd->bad_lp_tries == MAX_BAD_LP_TRIES) {
417 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
418 PMA_PMD_LED_OVERR_REG);
419 reg &= ~(PMA_PMD_LED_MASK << PMA_PMD_LED_RX_LBN);
420 if (!bad_lp) {
421 reg |= PMA_PMD_LED_OFF << PMA_PMD_LED_RX_LBN;
422 } else {
423 reg |= PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN;
424 EFX_ERR(efx, "appears to be plugged into a port"
425 " that is not 10GBASE-T capable. The PHY"
426 " supports 10GBASE-T ONLY, so no link can"
427 " be established\n");
428 }
429 mdio_clause45_write(efx, phy_id, MDIO_MMD_PMAPMD,
430 PMA_PMD_LED_OVERR_REG, reg);
431 pd->bad_lp_tries = bad_lp;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100432 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100433}
434
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800435static bool sfx7101_link_ok(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100436{
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800437 return mdio_clause45_links_ok(efx,
438 MDIO_MMDREG_DEVS_PMAPMD |
439 MDIO_MMDREG_DEVS_PCS |
440 MDIO_MMDREG_DEVS_PHYXS);
441}
442
443static bool sft9001_link_ok(struct efx_nic *efx, struct ethtool_cmd *ecmd)
444{
445 int phy_id = efx->mii.phy_id;
446 u32 reg;
447
Ben Hutchingscaa8d8b2008-12-26 13:46:12 -0800448 if (efx_phy_mode_disabled(efx->phy_mode))
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800449 return false;
Ben Hutchingscaa8d8b2008-12-26 13:46:12 -0800450 else if (efx->loopback_mode == LOOPBACK_GPHY)
451 return true;
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800452 else if (efx->loopback_mode)
Ben Hutchings04cc8ca2008-12-12 21:50:46 -0800453 return mdio_clause45_links_ok(efx,
454 MDIO_MMDREG_DEVS_PMAPMD |
Ben Hutchings04cc8ca2008-12-12 21:50:46 -0800455 MDIO_MMDREG_DEVS_PHYXS);
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800456
457 /* We must use the same definition of link state as LASI,
458 * otherwise we can miss a link state transition
459 */
460 if (ecmd->speed == 10000) {
461 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PCS,
462 PCS_10GBASET_STAT1);
463 return reg & (1 << PCS_10GBASET_BLKLK_LBN);
464 } else {
465 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_C22EXT,
466 C22EXT_STATUS_REG);
467 return reg & (1 << C22EXT_STATUS_LINK_LBN);
468 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100469}
470
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800471static void tenxpress_ext_loopback(struct efx_nic *efx)
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100472{
473 int phy_id = efx->mii.phy_id;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100474
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800475 mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_PHYXS,
476 PHYXS_TEST1, LOOPBACK_NEAR_LBN,
477 efx->loopback_mode == LOOPBACK_PHYXS);
478 if (efx->phy_type != PHY_TYPE_SFX7101)
479 mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_C22EXT,
480 GPHY_XCONTROL_REG,
481 GPHY_LOOPBACK_NEAR_LBN,
482 efx->loopback_mode == LOOPBACK_GPHY);
483}
484
485static void tenxpress_low_power(struct efx_nic *efx)
486{
487 int phy_id = efx->mii.phy_id;
488
489 if (efx->phy_type == PHY_TYPE_SFX7101)
490 mdio_clause45_set_mmds_lpower(
491 efx, !!(efx->phy_mode & PHY_MODE_LOW_POWER),
492 TENXPRESS_REQUIRED_DEVS);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100493 else
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800494 mdio_clause45_set_flag(
495 efx, phy_id, MDIO_MMD_PMAPMD,
496 PMA_PMD_XCONTROL_REG, PMA_PMD_EXT_LPOWER_LBN,
497 !!(efx->phy_mode & PHY_MODE_LOW_POWER));
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100498}
499
Ben Hutchings8ceee662008-04-27 12:55:59 +0100500static void tenxpress_phy_reconfigure(struct efx_nic *efx)
501{
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100502 struct tenxpress_phy_data *phy_data = efx->phy_data;
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800503 struct ethtool_cmd ecmd;
504 bool phy_mode_change, loop_reset, loop_toggle, loopback;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100505
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800506 if (efx->phy_mode & (PHY_MODE_OFF | PHY_MODE_SPECIAL)) {
Ben Hutchingsf8b87c12008-09-01 12:48:17 +0100507 phy_data->phy_mode = efx->phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100508 return;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +0100509 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100510
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800511 tenxpress_low_power(efx);
512
513 phy_mode_change = (efx->phy_mode == PHY_MODE_NORMAL &&
514 phy_data->phy_mode != PHY_MODE_NORMAL);
515 loopback = LOOPBACK_MASK(efx) & efx->phy_op->loopbacks;
516 loop_toggle = LOOPBACK_CHANGED(phy_data, efx, efx->phy_op->loopbacks);
517 loop_reset = (LOOPBACK_OUT_OF(phy_data, efx, efx->phy_op->loopbacks) ||
518 LOOPBACK_CHANGED(phy_data, efx, 1 << LOOPBACK_GPHY));
519
520 if (loop_reset || loop_toggle || loopback || phy_mode_change) {
521 int rc;
522
523 efx->phy_op->get_settings(efx, &ecmd);
524
525 if (loop_reset || phy_mode_change) {
526 tenxpress_special_reset(efx);
527
528 /* Reset XAUI if we were in 10G, and are staying
529 * in 10G. If we're moving into and out of 10G
530 * then xaui will be reset anyway */
531 if (EFX_IS10G(efx))
532 falcon_reset_xaui(efx);
533 }
534
535 if (efx->phy_type != PHY_TYPE_SFX7101) {
536 /* Only change autoneg once, on coming out or
537 * going into loopback */
538 if (loop_toggle)
539 ecmd.autoneg = !loopback;
540 if (loopback) {
541 ecmd.duplex = DUPLEX_FULL;
542 if (efx->loopback_mode == LOOPBACK_GPHY)
543 ecmd.speed = SPEED_1000;
544 else
545 ecmd.speed = SPEED_10000;
546 }
547 }
548
549 rc = efx->phy_op->set_settings(efx, &ecmd);
550 WARN_ON(rc);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100551 }
552
553 mdio_clause45_transmit_disable(efx);
554 mdio_clause45_phy_reconfigure(efx);
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800555 tenxpress_ext_loopback(efx);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100556
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100557 phy_data->loopback_mode = efx->loopback_mode;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +0100558 phy_data->phy_mode = efx->phy_mode;
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800559
560 if (efx->phy_type == PHY_TYPE_SFX7101) {
561 efx->link_speed = 10000;
562 efx->link_fd = true;
563 efx->link_up = sfx7101_link_ok(efx);
564 } else {
565 efx->phy_op->get_settings(efx, &ecmd);
566 efx->link_speed = ecmd.speed;
567 efx->link_fd = ecmd.duplex == DUPLEX_FULL;
568 efx->link_up = sft9001_link_ok(efx, &ecmd);
569 }
Ben Hutchings04cc8ca2008-12-12 21:50:46 -0800570 efx->link_fc = mdio_clause45_get_pause(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100571}
572
Ben Hutchings8ceee662008-04-27 12:55:59 +0100573/* Poll PHY for interrupt */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800574static void tenxpress_phy_poll(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100575{
576 struct tenxpress_phy_data *phy_data = efx->phy_data;
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800577 bool change = false, link_ok;
578 unsigned link_fc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100579
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800580 if (efx->phy_type == PHY_TYPE_SFX7101) {
581 link_ok = sfx7101_link_ok(efx);
582 if (link_ok != efx->link_up) {
583 change = true;
584 } else {
585 link_fc = mdio_clause45_get_pause(efx);
586 if (link_fc != efx->link_fc)
587 change = true;
588 }
589 sfx7101_check_bad_lp(efx, link_ok);
Ben Hutchingscaa8d8b2008-12-26 13:46:12 -0800590 } else if (efx->loopback_mode) {
591 bool link_ok = sft9001_link_ok(efx, NULL);
592 if (link_ok != efx->link_up)
593 change = true;
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800594 } else {
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800595 u32 status = mdio_clause45_read(efx, efx->mii.phy_id,
596 MDIO_MMD_PMAPMD,
597 PMA_PMD_LASI_STATUS);
598 if (status & (1 << PMA_PMD_LS_ALARM_LBN))
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800599 change = true;
600 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100601
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800602 if (change)
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800603 falcon_sim_phy_event(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100604
Ben Hutchingsf8b87c12008-09-01 12:48:17 +0100605 if (phy_data->phy_mode != PHY_MODE_NORMAL)
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800606 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100607
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800608 if (EFX_WORKAROUND_10750(efx) &&
609 atomic_read(&phy_data->bad_crc_count) > crc_error_reset_threshold) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100610 EFX_ERR(efx, "Resetting XAUI due to too many CRC errors\n");
611 falcon_reset_xaui(efx);
612 atomic_set(&phy_data->bad_crc_count, 0);
613 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100614}
615
616static void tenxpress_phy_fini(struct efx_nic *efx)
617{
618 int reg;
619
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800620 if (efx->phy_type == PHY_TYPE_SFT9001B) {
621 device_remove_file(&efx->pci_dev->dev,
622 &dev_attr_phy_short_reach);
623 } else {
624 /* Power down the LNPGA */
625 reg = (1 << PMA_PMD_LNPGA_POWERDOWN_LBN);
626 mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
627 PMA_PMD_XCONTROL_REG, reg);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100628
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800629 /* Waiting here ensures that the board fini, which can turn
630 * off the power to the PHY, won't get run until the LNPGA
631 * powerdown has been given long enough to complete. */
632 schedule_timeout_uninterruptible(LNPGA_PDOWN_WAIT); /* 200 ms */
633 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100634
635 kfree(efx->phy_data);
636 efx->phy_data = NULL;
637}
638
639
640/* Set the RX and TX LEDs and Link LED flashing. The other LEDs
641 * (which probably aren't wired anyway) are left in AUTO mode */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100642void tenxpress_phy_blink(struct efx_nic *efx, bool blink)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100643{
644 int reg;
645
646 if (blink)
647 reg = (PMA_PMD_LED_FLASH << PMA_PMD_LED_TX_LBN) |
648 (PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN) |
649 (PMA_PMD_LED_FLASH << PMA_PMD_LED_LINK_LBN);
650 else
651 reg = PMA_PMD_LED_DEFAULT;
652
653 mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
654 PMA_PMD_LED_OVERR_REG, reg);
655}
656
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100657static int tenxpress_phy_test(struct efx_nic *efx)
658{
659 /* BIST is automatically run after a special software reset */
660 return tenxpress_special_reset(efx);
661}
662
Ben Hutchings04cc8ca2008-12-12 21:50:46 -0800663static u32 tenxpress_get_xnp_lpa(struct efx_nic *efx)
664{
665 int phy = efx->mii.phy_id;
666 u32 lpa = 0;
667 int reg;
668
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800669 if (efx->phy_type != PHY_TYPE_SFX7101) {
670 reg = mdio_clause45_read(efx, phy, MDIO_MMD_C22EXT,
671 C22EXT_MSTSLV_REG);
672 if (reg & (1 << C22EXT_MSTSLV_1000_HD_LBN))
673 lpa |= ADVERTISED_1000baseT_Half;
674 if (reg & (1 << C22EXT_MSTSLV_1000_FD_LBN))
675 lpa |= ADVERTISED_1000baseT_Full;
676 }
Ben Hutchings04cc8ca2008-12-12 21:50:46 -0800677 reg = mdio_clause45_read(efx, phy, MDIO_MMD_AN, MDIO_AN_10GBT_STATUS);
678 if (reg & (1 << MDIO_AN_10GBT_STATUS_LP_10G_LBN))
679 lpa |= ADVERTISED_10000baseT_Full;
680 return lpa;
681}
682
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800683static void sfx7101_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
Ben Hutchings04cc8ca2008-12-12 21:50:46 -0800684{
685 mdio_clause45_get_settings_ext(efx, ecmd, ADVERTISED_10000baseT_Full,
686 tenxpress_get_xnp_lpa(efx));
687 ecmd->supported |= SUPPORTED_10000baseT_Full;
688 ecmd->advertising |= ADVERTISED_10000baseT_Full;
689}
690
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800691static void sft9001_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
692{
693 int phy_id = efx->mii.phy_id;
694 u32 xnp_adv = 0;
695 int reg;
696
697 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
698 PMA_PMD_SPEED_ENABLE_REG);
699 if (EFX_WORKAROUND_13204(efx) && (reg & (1 << PMA_PMD_100TX_ADV_LBN)))
700 xnp_adv |= ADVERTISED_100baseT_Full;
701 if (reg & (1 << PMA_PMD_1000T_ADV_LBN))
702 xnp_adv |= ADVERTISED_1000baseT_Full;
703 if (reg & (1 << PMA_PMD_10000T_ADV_LBN))
704 xnp_adv |= ADVERTISED_10000baseT_Full;
705
706 mdio_clause45_get_settings_ext(efx, ecmd, xnp_adv,
707 tenxpress_get_xnp_lpa(efx));
708
709 ecmd->supported |= (SUPPORTED_100baseT_Half |
710 SUPPORTED_100baseT_Full |
711 SUPPORTED_1000baseT_Full);
712
713 /* Use the vendor defined C22ext register for duplex settings */
714 if (ecmd->speed != SPEED_10000 && !ecmd->autoneg) {
715 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_C22EXT,
716 GPHY_XCONTROL_REG);
717 ecmd->duplex = (reg & (1 << GPHY_DUPLEX_LBN) ?
718 DUPLEX_FULL : DUPLEX_HALF);
719 }
720}
721
722static int sft9001_set_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
723{
724 int phy_id = efx->mii.phy_id;
725 int rc;
726
727 rc = mdio_clause45_set_settings(efx, ecmd);
728 if (rc)
729 return rc;
730
731 if (ecmd->speed != SPEED_10000 && !ecmd->autoneg)
732 mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_C22EXT,
733 GPHY_XCONTROL_REG, GPHY_DUPLEX_LBN,
734 ecmd->duplex == DUPLEX_FULL);
735
736 return rc;
737}
738
739static bool sft9001_set_xnp_advertise(struct efx_nic *efx, u32 advertising)
740{
741 int phy = efx->mii.phy_id;
742 int reg = mdio_clause45_read(efx, phy, MDIO_MMD_PMAPMD,
743 PMA_PMD_SPEED_ENABLE_REG);
744 bool enabled;
745
746 reg &= ~((1 << 2) | (1 << 3));
747 if (EFX_WORKAROUND_13204(efx) &&
748 (advertising & ADVERTISED_100baseT_Full))
749 reg |= 1 << PMA_PMD_100TX_ADV_LBN;
750 if (advertising & ADVERTISED_1000baseT_Full)
751 reg |= 1 << PMA_PMD_1000T_ADV_LBN;
752 if (advertising & ADVERTISED_10000baseT_Full)
753 reg |= 1 << PMA_PMD_10000T_ADV_LBN;
754 mdio_clause45_write(efx, phy, MDIO_MMD_PMAPMD,
755 PMA_PMD_SPEED_ENABLE_REG, reg);
756
757 enabled = (advertising &
758 (ADVERTISED_1000baseT_Half |
759 ADVERTISED_1000baseT_Full |
760 ADVERTISED_10000baseT_Full));
761 if (EFX_WORKAROUND_13204(efx))
762 enabled |= (advertising & ADVERTISED_100baseT_Full);
763 return enabled;
764}
765
766struct efx_phy_operations falcon_sfx7101_phy_ops = {
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800767 .macs = EFX_XMAC,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100768 .init = tenxpress_phy_init,
769 .reconfigure = tenxpress_phy_reconfigure,
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800770 .poll = tenxpress_phy_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100771 .fini = tenxpress_phy_fini,
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800772 .clear_interrupt = efx_port_dummy_op_void,
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100773 .test = tenxpress_phy_test,
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800774 .get_settings = sfx7101_get_settings,
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800775 .set_settings = mdio_clause45_set_settings,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100776 .mmds = TENXPRESS_REQUIRED_DEVS,
Ben Hutchingse6fa2eb2008-12-12 22:00:17 -0800777 .loopbacks = SFX7101_LOOPBACKS,
778};
779
780struct efx_phy_operations falcon_sft9001_phy_ops = {
781 .macs = EFX_GMAC | EFX_XMAC,
782 .init = tenxpress_phy_init,
783 .reconfigure = tenxpress_phy_reconfigure,
784 .poll = tenxpress_phy_poll,
785 .fini = tenxpress_phy_fini,
786 .clear_interrupt = efx_port_dummy_op_void,
787 .test = tenxpress_phy_test,
788 .get_settings = sft9001_get_settings,
789 .set_settings = sft9001_set_settings,
790 .set_xnp_advertise = sft9001_set_xnp_advertise,
791 .mmds = TENXPRESS_REQUIRED_DEVS,
792 .loopbacks = SFT9001_LOOPBACKS,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100793};