]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/sfc/tenxpress.c
8412dbe1e8fb954eda7286d9f2cc21bf25f59127
[linux-2.6-omap-h63xx.git] / drivers / net / sfc / tenxpress.c
1 /****************************************************************************
2  * Driver for Solarflare 802.3an compliant PHY
3  * Copyright 2007 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 #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"
20
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)
26
27 #define TENXPRESS_LOOPBACKS ((1 << LOOPBACK_PHYXS) |    \
28                              (1 << LOOPBACK_PCS) |      \
29                              (1 << LOOPBACK_PMAPMD) |   \
30                              (1 << LOOPBACK_NETWORK))
31
32 /* We complain if we fail to see the link partner as 10G capable this many
33  * times in a row (must be > 1 as sampling the autoneg. registers is racy)
34  */
35 #define MAX_BAD_LP_TRIES        (5)
36
37 /* Extended control register */
38 #define PMA_PMD_XCONTROL_REG 0xc000
39 #define PMA_PMD_LNPGA_POWERDOWN_LBN 8
40 #define PMA_PMD_LNPGA_POWERDOWN_WIDTH 1
41
42 /* extended status register */
43 #define PMA_PMD_XSTATUS_REG 0xc001
44 #define PMA_PMD_XSTAT_FLP_LBN   (12)
45
46 /* LED control register */
47 #define PMA_PMD_LED_CTRL_REG    (0xc007)
48 #define PMA_PMA_LED_ACTIVITY_LBN        (3)
49
50 /* LED function override register */
51 #define PMA_PMD_LED_OVERR_REG   (0xc009)
52 /* Bit positions for different LEDs (there are more but not wired on SFE4001)*/
53 #define PMA_PMD_LED_LINK_LBN    (0)
54 #define PMA_PMD_LED_SPEED_LBN   (2)
55 #define PMA_PMD_LED_TX_LBN      (4)
56 #define PMA_PMD_LED_RX_LBN      (6)
57 /* Override settings */
58 #define PMA_PMD_LED_AUTO        (0)     /* H/W control */
59 #define PMA_PMD_LED_ON          (1)
60 #define PMA_PMD_LED_OFF         (2)
61 #define PMA_PMD_LED_FLASH       (3)
62 /* All LEDs under hardware control */
63 #define PMA_PMD_LED_FULL_AUTO   (0)
64 /* Green and Amber under hardware control, Red off */
65 #define PMA_PMD_LED_DEFAULT     (PMA_PMD_LED_OFF << PMA_PMD_LED_RX_LBN)
66
67
68 /* Special Software reset register */
69 #define PMA_PMD_EXT_CTRL_REG 49152
70 #define PMA_PMD_EXT_SSR_LBN 15
71
72 /* Misc register defines */
73 #define PCS_CLOCK_CTRL_REG 0xd801
74 #define PLL312_RST_N_LBN 2
75
76 #define PCS_SOFT_RST2_REG 0xd806
77 #define SERDES_RST_N_LBN 13
78 #define XGXS_RST_N_LBN 12
79
80 #define PCS_TEST_SELECT_REG 0xd807      /* PRM 10.5.8 */
81 #define CLK312_EN_LBN 3
82
83 /* PHYXS registers */
84 #define PHYXS_TEST1         (49162)
85 #define LOOPBACK_NEAR_LBN   (8)
86 #define LOOPBACK_NEAR_WIDTH (1)
87
88 /* Boot status register */
89 #define PCS_BOOT_STATUS_REG     (0xd000)
90 #define PCS_BOOT_FATAL_ERR_LBN  (0)
91 #define PCS_BOOT_PROGRESS_LBN   (1)
92 #define PCS_BOOT_PROGRESS_WIDTH (2)
93 #define PCS_BOOT_COMPLETE_LBN   (3)
94 #define PCS_BOOT_MAX_DELAY      (100)
95 #define PCS_BOOT_POLL_DELAY     (10)
96
97 /* Time to wait between powering down the LNPGA and turning off the power
98  * rails */
99 #define LNPGA_PDOWN_WAIT        (HZ / 5)
100
101 static int crc_error_reset_threshold = 100;
102 module_param(crc_error_reset_threshold, int, 0644);
103 MODULE_PARM_DESC(crc_error_reset_threshold,
104                  "Max number of CRC errors before XAUI reset");
105
106 struct tenxpress_phy_data {
107         enum efx_loopback_mode loopback_mode;
108         atomic_t bad_crc_count;
109         enum efx_phy_mode phy_mode;
110         int bad_lp_tries;
111 };
112
113 void tenxpress_crc_err(struct efx_nic *efx)
114 {
115         struct tenxpress_phy_data *phy_data = efx->phy_data;
116         if (phy_data != NULL)
117                 atomic_inc(&phy_data->bad_crc_count);
118 }
119
120 /* Check that the C166 has booted successfully */
121 static int tenxpress_phy_check(struct efx_nic *efx)
122 {
123         int phy_id = efx->mii.phy_id;
124         int count = PCS_BOOT_MAX_DELAY / PCS_BOOT_POLL_DELAY;
125         int boot_stat;
126
127         /* Wait for the boot to complete (or not) */
128         while (count) {
129                 boot_stat = mdio_clause45_read(efx, phy_id,
130                                                MDIO_MMD_PCS,
131                                                PCS_BOOT_STATUS_REG);
132                 if (boot_stat & (1 << PCS_BOOT_COMPLETE_LBN))
133                         break;
134                 count--;
135                 udelay(PCS_BOOT_POLL_DELAY);
136         }
137
138         if (!count) {
139                 EFX_ERR(efx, "%s: PHY boot timed out. Last status "
140                         "%x\n", __func__,
141                         (boot_stat >> PCS_BOOT_PROGRESS_LBN) &
142                         ((1 << PCS_BOOT_PROGRESS_WIDTH) - 1));
143                 return -ETIMEDOUT;
144         }
145
146         return 0;
147 }
148
149 static void tenxpress_reset_xaui(struct efx_nic *efx);
150
151 static int tenxpress_init(struct efx_nic *efx)
152 {
153         int rc, reg;
154
155         /* Turn on the clock  */
156         reg = (1 << CLK312_EN_LBN);
157         mdio_clause45_write(efx, efx->mii.phy_id,
158                             MDIO_MMD_PCS, PCS_TEST_SELECT_REG, reg);
159
160         rc = tenxpress_phy_check(efx);
161         if (rc < 0)
162                 return rc;
163
164         /* Set the LEDs up as: Green = Link, Amber = Link/Act, Red = Off */
165         reg = mdio_clause45_read(efx, efx->mii.phy_id,
166                                  MDIO_MMD_PMAPMD, PMA_PMD_LED_CTRL_REG);
167         reg |= (1 << PMA_PMA_LED_ACTIVITY_LBN);
168         mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
169                             PMA_PMD_LED_CTRL_REG, reg);
170
171         reg = PMA_PMD_LED_DEFAULT;
172         mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
173                             PMA_PMD_LED_OVERR_REG, reg);
174
175         return rc;
176 }
177
178 static int tenxpress_phy_init(struct efx_nic *efx)
179 {
180         struct tenxpress_phy_data *phy_data;
181         int rc = 0;
182
183         phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
184         if (!phy_data)
185                 return -ENOMEM;
186         efx->phy_data = phy_data;
187         phy_data->phy_mode = efx->phy_mode;
188
189         rc = mdio_clause45_wait_reset_mmds(efx,
190                                            TENXPRESS_REQUIRED_DEVS);
191         if (rc < 0)
192                 goto fail;
193
194         rc = mdio_clause45_check_mmds(efx, TENXPRESS_REQUIRED_DEVS, 0);
195         if (rc < 0)
196                 goto fail;
197
198         rc = tenxpress_init(efx);
199         if (rc < 0)
200                 goto fail;
201
202         schedule_timeout_uninterruptible(HZ / 5); /* 200ms */
203
204         /* Let XGXS and SerDes out of reset and resets 10XPress */
205         falcon_reset_xaui(efx);
206
207         return 0;
208
209  fail:
210         kfree(efx->phy_data);
211         efx->phy_data = NULL;
212         return rc;
213 }
214
215 static int tenxpress_special_reset(struct efx_nic *efx)
216 {
217         int rc, reg;
218
219         EFX_TRACE(efx, "%s\n", __func__);
220
221         /* Initiate reset */
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);
227
228         msleep(200);
229
230         /* Wait for the blocks to come out of reset */
231         rc = mdio_clause45_wait_reset_mmds(efx,
232                                            TENXPRESS_REQUIRED_DEVS);
233         if (rc < 0)
234                 return rc;
235
236         /* Try and reconfigure the device */
237         rc = tenxpress_init(efx);
238         if (rc < 0)
239                 return rc;
240
241         return 0;
242 }
243
244 static void tenxpress_set_bad_lp(struct efx_nic *efx, bool bad_lp)
245 {
246         struct tenxpress_phy_data *pd = efx->phy_data;
247         int reg;
248
249         /* Nothing to do if all is well and was previously so. */
250         if (!(bad_lp || pd->bad_lp_tries))
251                 return;
252
253         reg = mdio_clause45_read(efx, efx->mii.phy_id,
254                                  MDIO_MMD_PMAPMD, PMA_PMD_LED_OVERR_REG);
255
256         if (bad_lp)
257                 pd->bad_lp_tries++;
258         else
259                 pd->bad_lp_tries = 0;
260
261         if (pd->bad_lp_tries == MAX_BAD_LP_TRIES) {
262                 pd->bad_lp_tries = 0;   /* Restart count */
263                 reg &= ~(PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN);
264                 reg |= (PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN);
265                 EFX_ERR(efx, "This NIC appears to be plugged into"
266                         " a port that is not 10GBASE-T capable.\n"
267                         " This PHY is 10GBASE-T ONLY, so no link can"
268                         " be established.\n");
269         } else {
270                 reg |= (PMA_PMD_LED_OFF << PMA_PMD_LED_RX_LBN);
271         }
272         mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
273                             PMA_PMD_LED_OVERR_REG, reg);
274 }
275
276 /* Check link status and return a boolean OK value. If the link is NOT
277  * OK we have a quick rummage round to see if we appear to be plugged
278  * into a non-10GBT port and if so warn the user that they won't get
279  * link any time soon as we are 10GBT only, unless caller specified
280  * not to do this check (it isn't useful in loopback) */
281 static bool tenxpress_link_ok(struct efx_nic *efx, bool check_lp)
282 {
283         bool ok = mdio_clause45_links_ok(efx, TENXPRESS_REQUIRED_DEVS);
284
285         if (ok) {
286                 tenxpress_set_bad_lp(efx, false);
287         } else if (check_lp) {
288                 /* Are we plugged into the wrong sort of link? */
289                 bool bad_lp = false;
290                 int phy_id = efx->mii.phy_id;
291                 int an_stat = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN,
292                                                  MDIO_AN_STATUS);
293                 int xphy_stat = mdio_clause45_read(efx, phy_id,
294                                                    MDIO_MMD_PMAPMD,
295                                                    PMA_PMD_XSTATUS_REG);
296                 /* Are we plugged into anything that sends FLPs? If
297                  * not we can't distinguish between not being plugged
298                  * in and being plugged into a non-AN antique. The FLP
299                  * bit has the advantage of not clearing when autoneg
300                  * restarts. */
301                 if (!(xphy_stat & (1 << PMA_PMD_XSTAT_FLP_LBN))) {
302                         tenxpress_set_bad_lp(efx, false);
303                         return ok;
304                 }
305
306                 /* If it can do 10GBT it must be XNP capable */
307                 bad_lp = !(an_stat & (1 << MDIO_AN_STATUS_XNP_LBN));
308                 if (!bad_lp && (an_stat & (1 << MDIO_AN_STATUS_PAGE_LBN))) {
309                         bad_lp = !(mdio_clause45_read(efx, phy_id,
310                                         MDIO_MMD_AN, MDIO_AN_10GBT_STATUS) &
311                                         (1 << MDIO_AN_10GBT_STATUS_LP_10G_LBN));
312                 }
313                 tenxpress_set_bad_lp(efx, bad_lp);
314         }
315         return ok;
316 }
317
318 static void tenxpress_phyxs_loopback(struct efx_nic *efx)
319 {
320         int phy_id = efx->mii.phy_id;
321         int ctrl1, ctrl2;
322
323         ctrl1 = ctrl2 = mdio_clause45_read(efx, phy_id, MDIO_MMD_PHYXS,
324                                            PHYXS_TEST1);
325         if (efx->loopback_mode == LOOPBACK_PHYXS)
326                 ctrl2 |= (1 << LOOPBACK_NEAR_LBN);
327         else
328                 ctrl2 &= ~(1 << LOOPBACK_NEAR_LBN);
329         if (ctrl1 != ctrl2)
330                 mdio_clause45_write(efx, phy_id, MDIO_MMD_PHYXS,
331                                     PHYXS_TEST1, ctrl2);
332 }
333
334 static void tenxpress_phy_reconfigure(struct efx_nic *efx)
335 {
336         struct tenxpress_phy_data *phy_data = efx->phy_data;
337         bool loop_change = LOOPBACK_OUT_OF(phy_data, efx,
338                                            TENXPRESS_LOOPBACKS);
339
340         if (efx->phy_mode & PHY_MODE_SPECIAL) {
341                 phy_data->phy_mode = efx->phy_mode;
342                 return;
343         }
344
345         /* When coming out of transmit disable, coming out of low power
346          * mode, or moving out of any PHY internal loopback mode,
347          * perform a special software reset */
348         if ((efx->phy_mode == PHY_MODE_NORMAL &&
349              phy_data->phy_mode != PHY_MODE_NORMAL) ||
350             loop_change) {
351                 tenxpress_special_reset(efx);
352                 falcon_reset_xaui(efx);
353         }
354
355         mdio_clause45_transmit_disable(efx);
356         mdio_clause45_phy_reconfigure(efx);
357         tenxpress_phyxs_loopback(efx);
358
359         phy_data->loopback_mode = efx->loopback_mode;
360         phy_data->phy_mode = efx->phy_mode;
361         efx->link_up = tenxpress_link_ok(efx, false);
362         efx->link_options = GM_LPA_10000FULL;
363 }
364
365 static void tenxpress_phy_clear_interrupt(struct efx_nic *efx)
366 {
367         /* Nothing done here - LASI interrupts aren't reliable so poll  */
368 }
369
370
371 /* Poll PHY for interrupt */
372 static int tenxpress_phy_check_hw(struct efx_nic *efx)
373 {
374         struct tenxpress_phy_data *phy_data = efx->phy_data;
375         bool link_ok;
376
377         link_ok = (phy_data->phy_mode == PHY_MODE_NORMAL &&
378                    tenxpress_link_ok(efx, true));
379
380         if (link_ok != efx->link_up)
381                 falcon_xmac_sim_phy_event(efx);
382
383         if (phy_data->phy_mode != PHY_MODE_NORMAL)
384                 return 0;
385
386         if (atomic_read(&phy_data->bad_crc_count) > crc_error_reset_threshold) {
387                 EFX_ERR(efx, "Resetting XAUI due to too many CRC errors\n");
388                 falcon_reset_xaui(efx);
389                 atomic_set(&phy_data->bad_crc_count, 0);
390         }
391
392         return 0;
393 }
394
395 static void tenxpress_phy_fini(struct efx_nic *efx)
396 {
397         int reg;
398
399         /* Power down the LNPGA */
400         reg = (1 << PMA_PMD_LNPGA_POWERDOWN_LBN);
401         mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
402                             PMA_PMD_XCONTROL_REG, reg);
403
404         /* Waiting here ensures that the board fini, which can turn off the
405          * power to the PHY, won't get run until the LNPGA powerdown has been
406          * given long enough to complete. */
407         schedule_timeout_uninterruptible(LNPGA_PDOWN_WAIT); /* 200 ms */
408
409         kfree(efx->phy_data);
410         efx->phy_data = NULL;
411 }
412
413
414 /* Set the RX and TX LEDs and Link LED flashing. The other LEDs
415  * (which probably aren't wired anyway) are left in AUTO mode */
416 void tenxpress_phy_blink(struct efx_nic *efx, bool blink)
417 {
418         int reg;
419
420         if (blink)
421                 reg = (PMA_PMD_LED_FLASH << PMA_PMD_LED_TX_LBN) |
422                         (PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN) |
423                         (PMA_PMD_LED_FLASH << PMA_PMD_LED_LINK_LBN);
424         else
425                 reg = PMA_PMD_LED_DEFAULT;
426
427         mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
428                             PMA_PMD_LED_OVERR_REG, reg);
429 }
430
431 static void tenxpress_reset_xaui(struct efx_nic *efx)
432 {
433         int phy = efx->mii.phy_id;
434         int clk_ctrl, test_select, soft_rst2;
435
436         /* Real work is done on clock_ctrl other resets are thought to be
437          * optional but make the reset more reliable
438          */
439
440         /* Read */
441         clk_ctrl = mdio_clause45_read(efx, phy, MDIO_MMD_PCS,
442                                       PCS_CLOCK_CTRL_REG);
443         test_select = mdio_clause45_read(efx, phy, MDIO_MMD_PCS,
444                                          PCS_TEST_SELECT_REG);
445         soft_rst2 = mdio_clause45_read(efx, phy, MDIO_MMD_PCS,
446                                        PCS_SOFT_RST2_REG);
447
448         /* Put in reset */
449         test_select &= ~(1 << CLK312_EN_LBN);
450         mdio_clause45_write(efx, phy, MDIO_MMD_PCS,
451                             PCS_TEST_SELECT_REG, test_select);
452
453         soft_rst2 &= ~((1 << XGXS_RST_N_LBN) | (1 << SERDES_RST_N_LBN));
454         mdio_clause45_write(efx, phy, MDIO_MMD_PCS,
455                             PCS_SOFT_RST2_REG, soft_rst2);
456
457         clk_ctrl &= ~(1 << PLL312_RST_N_LBN);
458         mdio_clause45_write(efx, phy, MDIO_MMD_PCS,
459                             PCS_CLOCK_CTRL_REG, clk_ctrl);
460         udelay(10);
461
462         /* Remove reset */
463         clk_ctrl |= (1 << PLL312_RST_N_LBN);
464         mdio_clause45_write(efx, phy, MDIO_MMD_PCS,
465                             PCS_CLOCK_CTRL_REG, clk_ctrl);
466         udelay(10);
467
468         soft_rst2 |= ((1 << XGXS_RST_N_LBN) | (1 << SERDES_RST_N_LBN));
469         mdio_clause45_write(efx, phy, MDIO_MMD_PCS,
470                             PCS_SOFT_RST2_REG, soft_rst2);
471         udelay(10);
472
473         test_select |= (1 << CLK312_EN_LBN);
474         mdio_clause45_write(efx, phy, MDIO_MMD_PCS,
475                             PCS_TEST_SELECT_REG, test_select);
476         udelay(10);
477 }
478
479 static int tenxpress_phy_test(struct efx_nic *efx)
480 {
481         /* BIST is automatically run after a special software reset */
482         return tenxpress_special_reset(efx);
483 }
484
485 struct efx_phy_operations falcon_tenxpress_phy_ops = {
486         .init             = tenxpress_phy_init,
487         .reconfigure      = tenxpress_phy_reconfigure,
488         .check_hw         = tenxpress_phy_check_hw,
489         .fini             = tenxpress_phy_fini,
490         .clear_interrupt  = tenxpress_phy_clear_interrupt,
491         .reset_xaui       = tenxpress_reset_xaui,
492         .test             = tenxpress_phy_test,
493         .mmds             = TENXPRESS_REQUIRED_DEVS,
494         .loopbacks        = TENXPRESS_LOOPBACKS,
495 };