[netdrvr] sfc: Add phy_flash_cfg module parameter and implementation
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / sfc / tenxpress.c
blobd8df031c711dd788507f69a0dcee176ed30027e0
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.
8 */
10 #include <linux/delay.h>
11 #include <linux/seq_file.h>
12 #include "efx.h"
13 #include "gmii.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 "mac.h"
21 /* We expect these MMDs to be in the package */
22 /* AN not here as mdio_check_mmds() requires STAT2 support */
23 #define TENXPRESS_REQUIRED_DEVS (MDIO_MMDREG_DEVS0_PMAPMD | \
24 MDIO_MMDREG_DEVS0_PCS | \
25 MDIO_MMDREG_DEVS0_PHYXS)
27 /* We complain if we fail to see the link partner as 10G capable this many
28 * times in a row (must be > 1 as sampling the autoneg. registers is racy)
30 #define MAX_BAD_LP_TRIES (5)
32 /* Extended control register */
33 #define PMA_PMD_XCONTROL_REG 0xc000
34 #define PMA_PMD_LNPGA_POWERDOWN_LBN 8
35 #define PMA_PMD_LNPGA_POWERDOWN_WIDTH 1
37 /* extended status register */
38 #define PMA_PMD_XSTATUS_REG 0xc001
39 #define PMA_PMD_XSTAT_FLP_LBN (12)
41 /* LED control register */
42 #define PMA_PMD_LED_CTRL_REG (0xc007)
43 #define PMA_PMA_LED_ACTIVITY_LBN (3)
45 /* LED function override register */
46 #define PMA_PMD_LED_OVERR_REG (0xc009)
47 /* Bit positions for different LEDs (there are more but not wired on SFE4001)*/
48 #define PMA_PMD_LED_LINK_LBN (0)
49 #define PMA_PMD_LED_SPEED_LBN (2)
50 #define PMA_PMD_LED_TX_LBN (4)
51 #define PMA_PMD_LED_RX_LBN (6)
52 /* Override settings */
53 #define PMA_PMD_LED_AUTO (0) /* H/W control */
54 #define PMA_PMD_LED_ON (1)
55 #define PMA_PMD_LED_OFF (2)
56 #define PMA_PMD_LED_FLASH (3)
57 /* All LEDs under hardware control */
58 #define PMA_PMD_LED_FULL_AUTO (0)
59 /* Green and Amber under hardware control, Red off */
60 #define PMA_PMD_LED_DEFAULT (PMA_PMD_LED_OFF << PMA_PMD_LED_RX_LBN)
63 /* Self test (BIST) control register */
64 #define PMA_PMD_BIST_CTRL_REG (0xc014)
65 #define PMA_PMD_BIST_BER_LBN (2) /* Run BER test */
66 #define PMA_PMD_BIST_CONT_LBN (1) /* Run continuous BIST until cleared */
67 #define PMA_PMD_BIST_SINGLE_LBN (0) /* Run 1 BIST iteration (self clears) */
68 /* Self test status register */
69 #define PMA_PMD_BIST_STAT_REG (0xc015)
70 #define PMA_PMD_BIST_ENX_LBN (3)
71 #define PMA_PMD_BIST_PMA_LBN (2)
72 #define PMA_PMD_BIST_RXD_LBN (1)
73 #define PMA_PMD_BIST_AFE_LBN (0)
75 #define BIST_MAX_DELAY (1000)
76 #define BIST_POLL_DELAY (10)
78 /* Misc register defines */
79 #define PCS_CLOCK_CTRL_REG 0xd801
80 #define PLL312_RST_N_LBN 2
82 #define PCS_SOFT_RST2_REG 0xd806
83 #define SERDES_RST_N_LBN 13
84 #define XGXS_RST_N_LBN 12
86 #define PCS_TEST_SELECT_REG 0xd807 /* PRM 10.5.8 */
87 #define CLK312_EN_LBN 3
89 /* Boot status register */
90 #define PCS_BOOT_STATUS_REG (0xd000)
91 #define PCS_BOOT_FATAL_ERR_LBN (0)
92 #define PCS_BOOT_PROGRESS_LBN (1)
93 #define PCS_BOOT_PROGRESS_WIDTH (2)
94 #define PCS_BOOT_COMPLETE_LBN (3)
95 #define PCS_BOOT_MAX_DELAY (100)
96 #define PCS_BOOT_POLL_DELAY (10)
98 /* Time to wait between powering down the LNPGA and turning off the power
99 * rails */
100 #define LNPGA_PDOWN_WAIT (HZ / 5)
102 static int crc_error_reset_threshold = 100;
103 module_param(crc_error_reset_threshold, int, 0644);
104 MODULE_PARM_DESC(crc_error_reset_threshold,
105 "Max number of CRC errors before XAUI reset");
107 struct tenxpress_phy_data {
108 enum tenxpress_state state;
109 atomic_t bad_crc_count;
110 int bad_lp_tries;
113 static int tenxpress_state_is(struct efx_nic *efx, int state)
115 struct tenxpress_phy_data *phy_data = efx->phy_data;
116 return (phy_data != NULL) && (state == phy_data->state);
119 void tenxpress_set_state(struct efx_nic *efx,
120 enum tenxpress_state state)
122 struct tenxpress_phy_data *phy_data = efx->phy_data;
123 if (phy_data != NULL)
124 phy_data->state = state;
127 void tenxpress_crc_err(struct efx_nic *efx)
129 struct tenxpress_phy_data *phy_data = efx->phy_data;
130 if (phy_data != NULL)
131 atomic_inc(&phy_data->bad_crc_count);
134 /* Check that the C166 has booted successfully */
135 static int tenxpress_phy_check(struct efx_nic *efx)
137 int phy_id = efx->mii.phy_id;
138 int count = PCS_BOOT_MAX_DELAY / PCS_BOOT_POLL_DELAY;
139 int boot_stat;
141 /* Wait for the boot to complete (or not) */
142 while (count) {
143 boot_stat = mdio_clause45_read(efx, phy_id,
144 MDIO_MMD_PCS,
145 PCS_BOOT_STATUS_REG);
146 if (boot_stat & (1 << PCS_BOOT_COMPLETE_LBN))
147 break;
148 count--;
149 udelay(PCS_BOOT_POLL_DELAY);
152 if (!count) {
153 EFX_ERR(efx, "%s: PHY boot timed out. Last status "
154 "%x\n", __func__,
155 (boot_stat >> PCS_BOOT_PROGRESS_LBN) &
156 ((1 << PCS_BOOT_PROGRESS_WIDTH) - 1));
157 return -ETIMEDOUT;
160 return 0;
163 static void tenxpress_reset_xaui(struct efx_nic *efx);
165 static int tenxpress_init(struct efx_nic *efx)
167 int rc, reg;
169 /* Turn on the clock */
170 reg = (1 << CLK312_EN_LBN);
171 mdio_clause45_write(efx, efx->mii.phy_id,
172 MDIO_MMD_PCS, PCS_TEST_SELECT_REG, reg);
174 rc = tenxpress_phy_check(efx);
175 if (rc < 0)
176 return rc;
178 /* Set the LEDs up as: Green = Link, Amber = Link/Act, Red = Off */
179 reg = mdio_clause45_read(efx, efx->mii.phy_id,
180 MDIO_MMD_PMAPMD, PMA_PMD_LED_CTRL_REG);
181 reg |= (1 << PMA_PMA_LED_ACTIVITY_LBN);
182 mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
183 PMA_PMD_LED_CTRL_REG, reg);
185 reg = PMA_PMD_LED_DEFAULT;
186 mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
187 PMA_PMD_LED_OVERR_REG, reg);
189 return rc;
192 static int tenxpress_phy_init(struct efx_nic *efx)
194 struct tenxpress_phy_data *phy_data;
195 int rc = 0;
197 phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
198 efx->phy_data = phy_data;
200 tenxpress_set_state(efx, TENXPRESS_STATUS_NORMAL);
202 if (!sfe4001_phy_flash_cfg) {
203 rc = mdio_clause45_wait_reset_mmds(efx,
204 TENXPRESS_REQUIRED_DEVS);
205 if (rc < 0)
206 goto fail;
209 rc = mdio_clause45_check_mmds(efx, TENXPRESS_REQUIRED_DEVS, 0);
210 if (rc < 0)
211 goto fail;
213 rc = tenxpress_init(efx);
214 if (rc < 0)
215 goto fail;
217 schedule_timeout_uninterruptible(HZ / 5); /* 200ms */
219 /* Let XGXS and SerDes out of reset and resets 10XPress */
220 falcon_reset_xaui(efx);
222 return 0;
224 fail:
225 kfree(efx->phy_data);
226 efx->phy_data = NULL;
227 return rc;
230 static void tenxpress_set_bad_lp(struct efx_nic *efx, int bad_lp)
232 struct tenxpress_phy_data *pd = efx->phy_data;
233 int reg;
235 /* Nothing to do if all is well and was previously so. */
236 if (!(bad_lp || pd->bad_lp_tries))
237 return;
239 reg = mdio_clause45_read(efx, efx->mii.phy_id,
240 MDIO_MMD_PMAPMD, PMA_PMD_LED_OVERR_REG);
242 if (bad_lp)
243 pd->bad_lp_tries++;
244 else
245 pd->bad_lp_tries = 0;
247 if (pd->bad_lp_tries == MAX_BAD_LP_TRIES) {
248 pd->bad_lp_tries = 0; /* Restart count */
249 reg &= ~(PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN);
250 reg |= (PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN);
251 EFX_ERR(efx, "This NIC appears to be plugged into"
252 " a port that is not 10GBASE-T capable.\n"
253 " This PHY is 10GBASE-T ONLY, so no link can"
254 " be established.\n");
255 } else {
256 reg |= (PMA_PMD_LED_OFF << PMA_PMD_LED_RX_LBN);
258 mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
259 PMA_PMD_LED_OVERR_REG, reg);
262 /* Check link status and return a boolean OK value. If the link is NOT
263 * OK we have a quick rummage round to see if we appear to be plugged
264 * into a non-10GBT port and if so warn the user that they won't get
265 * link any time soon as we are 10GBT only, unless caller specified
266 * not to do this check (it isn't useful in loopback) */
267 static int tenxpress_link_ok(struct efx_nic *efx, int check_lp)
269 int ok = mdio_clause45_links_ok(efx, TENXPRESS_REQUIRED_DEVS);
271 if (ok) {
272 tenxpress_set_bad_lp(efx, 0);
273 } else if (check_lp) {
274 /* Are we plugged into the wrong sort of link? */
275 int bad_lp = 0;
276 int phy_id = efx->mii.phy_id;
277 int an_stat = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN,
278 MDIO_AN_STATUS);
279 int xphy_stat = mdio_clause45_read(efx, phy_id,
280 MDIO_MMD_PMAPMD,
281 PMA_PMD_XSTATUS_REG);
282 /* Are we plugged into anything that sends FLPs? If
283 * not we can't distinguish between not being plugged
284 * in and being plugged into a non-AN antique. The FLP
285 * bit has the advantage of not clearing when autoneg
286 * restarts. */
287 if (!(xphy_stat & (1 << PMA_PMD_XSTAT_FLP_LBN))) {
288 tenxpress_set_bad_lp(efx, 0);
289 return ok;
292 /* If it can do 10GBT it must be XNP capable */
293 bad_lp = !(an_stat & (1 << MDIO_AN_STATUS_XNP_LBN));
294 if (!bad_lp && (an_stat & (1 << MDIO_AN_STATUS_PAGE_LBN))) {
295 bad_lp = !(mdio_clause45_read(efx, phy_id,
296 MDIO_MMD_AN, MDIO_AN_10GBT_STATUS) &
297 (1 << MDIO_AN_10GBT_STATUS_LP_10G_LBN));
299 tenxpress_set_bad_lp(efx, bad_lp);
301 return ok;
304 static void tenxpress_phy_reconfigure(struct efx_nic *efx)
306 if (!tenxpress_state_is(efx, TENXPRESS_STATUS_NORMAL))
307 return;
309 efx->link_up = tenxpress_link_ok(efx, 0);
310 efx->link_options = GM_LPA_10000FULL;
313 static void tenxpress_phy_clear_interrupt(struct efx_nic *efx)
315 /* Nothing done here - LASI interrupts aren't reliable so poll */
319 /* Poll PHY for interrupt */
320 static int tenxpress_phy_check_hw(struct efx_nic *efx)
322 struct tenxpress_phy_data *phy_data = efx->phy_data;
323 int phy_up = tenxpress_state_is(efx, TENXPRESS_STATUS_NORMAL);
324 int link_ok;
326 link_ok = phy_up && tenxpress_link_ok(efx, 1);
328 if (link_ok != efx->link_up)
329 falcon_xmac_sim_phy_event(efx);
331 /* Nothing to check if we've already shut down the PHY */
332 if (!phy_up)
333 return 0;
335 if (atomic_read(&phy_data->bad_crc_count) > crc_error_reset_threshold) {
336 EFX_ERR(efx, "Resetting XAUI due to too many CRC errors\n");
337 falcon_reset_xaui(efx);
338 atomic_set(&phy_data->bad_crc_count, 0);
341 return 0;
344 static void tenxpress_phy_fini(struct efx_nic *efx)
346 int reg;
348 /* Power down the LNPGA */
349 reg = (1 << PMA_PMD_LNPGA_POWERDOWN_LBN);
350 mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
351 PMA_PMD_XCONTROL_REG, reg);
353 /* Waiting here ensures that the board fini, which can turn off the
354 * power to the PHY, won't get run until the LNPGA powerdown has been
355 * given long enough to complete. */
356 schedule_timeout_uninterruptible(LNPGA_PDOWN_WAIT); /* 200 ms */
358 kfree(efx->phy_data);
359 efx->phy_data = NULL;
363 /* Set the RX and TX LEDs and Link LED flashing. The other LEDs
364 * (which probably aren't wired anyway) are left in AUTO mode */
365 void tenxpress_phy_blink(struct efx_nic *efx, int blink)
367 int reg;
369 if (blink)
370 reg = (PMA_PMD_LED_FLASH << PMA_PMD_LED_TX_LBN) |
371 (PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN) |
372 (PMA_PMD_LED_FLASH << PMA_PMD_LED_LINK_LBN);
373 else
374 reg = PMA_PMD_LED_DEFAULT;
376 mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
377 PMA_PMD_LED_OVERR_REG, reg);
380 static void tenxpress_reset_xaui(struct efx_nic *efx)
382 int phy = efx->mii.phy_id;
383 int clk_ctrl, test_select, soft_rst2;
385 /* Real work is done on clock_ctrl other resets are thought to be
386 * optional but make the reset more reliable
389 /* Read */
390 clk_ctrl = mdio_clause45_read(efx, phy, MDIO_MMD_PCS,
391 PCS_CLOCK_CTRL_REG);
392 test_select = mdio_clause45_read(efx, phy, MDIO_MMD_PCS,
393 PCS_TEST_SELECT_REG);
394 soft_rst2 = mdio_clause45_read(efx, phy, MDIO_MMD_PCS,
395 PCS_SOFT_RST2_REG);
397 /* Put in reset */
398 test_select &= ~(1 << CLK312_EN_LBN);
399 mdio_clause45_write(efx, phy, MDIO_MMD_PCS,
400 PCS_TEST_SELECT_REG, test_select);
402 soft_rst2 &= ~((1 << XGXS_RST_N_LBN) | (1 << SERDES_RST_N_LBN));
403 mdio_clause45_write(efx, phy, MDIO_MMD_PCS,
404 PCS_SOFT_RST2_REG, soft_rst2);
406 clk_ctrl &= ~(1 << PLL312_RST_N_LBN);
407 mdio_clause45_write(efx, phy, MDIO_MMD_PCS,
408 PCS_CLOCK_CTRL_REG, clk_ctrl);
409 udelay(10);
411 /* Remove reset */
412 clk_ctrl |= (1 << PLL312_RST_N_LBN);
413 mdio_clause45_write(efx, phy, MDIO_MMD_PCS,
414 PCS_CLOCK_CTRL_REG, clk_ctrl);
415 udelay(10);
417 soft_rst2 |= ((1 << XGXS_RST_N_LBN) | (1 << SERDES_RST_N_LBN));
418 mdio_clause45_write(efx, phy, MDIO_MMD_PCS,
419 PCS_SOFT_RST2_REG, soft_rst2);
420 udelay(10);
422 test_select |= (1 << CLK312_EN_LBN);
423 mdio_clause45_write(efx, phy, MDIO_MMD_PCS,
424 PCS_TEST_SELECT_REG, test_select);
425 udelay(10);
428 struct efx_phy_operations falcon_tenxpress_phy_ops = {
429 .init = tenxpress_phy_init,
430 .reconfigure = tenxpress_phy_reconfigure,
431 .check_hw = tenxpress_phy_check_hw,
432 .fini = tenxpress_phy_fini,
433 .clear_interrupt = tenxpress_phy_clear_interrupt,
434 .reset_xaui = tenxpress_reset_xaui,
435 .mmds = TENXPRESS_REQUIRED_DEVS,