blob: aad2dca197728c2ca8871d040c12efcbf7415a28 [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2006-2008 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/*
Ben Hutchingsd2d2c372009-02-27 13:07:33 +000010 * Driver for SFP+ and XFP optical PHYs plus some support specific to the
11 * AMCC QT20xx adapters; see www.amcc.com for details
Ben Hutchings8ceee662008-04-27 12:55:59 +010012 */
13
14#include <linux/timer.h>
15#include <linux/delay.h>
16#include "efx.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010017#include "mdio_10g.h"
18#include "xenpack.h"
19#include "phy.h"
Ben Hutchings177dfcd2008-12-12 21:50:08 -080020#include "falcon.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010021
Ben Hutchings68e7f452009-04-29 08:05:08 +000022#define XFP_REQUIRED_DEVS (MDIO_DEVS_PCS | \
23 MDIO_DEVS_PMAPMD | \
24 MDIO_DEVS_PHYXS)
Ben Hutchings8ceee662008-04-27 12:55:59 +010025
Ben Hutchings3273c2e2008-05-07 13:36:19 +010026#define XFP_LOOPBACKS ((1 << LOOPBACK_PCS) | \
27 (1 << LOOPBACK_PMAPMD) | \
28 (1 << LOOPBACK_NETWORK))
29
Ben Hutchings8ceee662008-04-27 12:55:59 +010030/****************************************************************************/
31/* Quake-specific MDIO registers */
32#define MDIO_QUAKE_LED0_REG (0xD006)
33
Ben Hutchingsd2d2c372009-02-27 13:07:33 +000034/* QT2025C only */
35#define PCS_FW_HEARTBEAT_REG 0xd7ee
36#define PCS_FW_HEARTB_LBN 0
37#define PCS_FW_HEARTB_WIDTH 8
38#define PCS_UC8051_STATUS_REG 0xd7fd
39#define PCS_UC_STATUS_LBN 0
40#define PCS_UC_STATUS_WIDTH 8
41#define PCS_UC_STATUS_FW_SAVE 0x20
42#define PMA_PMD_FTX_CTRL2_REG 0xc309
43#define PMA_PMD_FTX_STATIC_LBN 13
44#define PMA_PMD_VEND1_REG 0xc001
45#define PMA_PMD_VEND1_LBTXD_LBN 15
46#define PCS_VEND1_REG 0xc000
47#define PCS_VEND1_LBTXD_LBN 5
48
Ben Hutchings8ceee662008-04-27 12:55:59 +010049void xfp_set_led(struct efx_nic *p, int led, int mode)
50{
51 int addr = MDIO_QUAKE_LED0_REG + led;
Ben Hutchings68e7f452009-04-29 08:05:08 +000052 efx_mdio_write(p, MDIO_MMD_PMAPMD, addr, mode);
Ben Hutchings8ceee662008-04-27 12:55:59 +010053}
54
Ben Hutchings3273c2e2008-05-07 13:36:19 +010055struct xfp_phy_data {
Ben Hutchingsf8b87c12008-09-01 12:48:17 +010056 enum efx_phy_mode phy_mode;
Ben Hutchings3273c2e2008-05-07 13:36:19 +010057};
58
Ben Hutchings8ceee662008-04-27 12:55:59 +010059#define XFP_MAX_RESET_TIME 500
60#define XFP_RESET_WAIT 10
61
Ben Hutchingsd2d2c372009-02-27 13:07:33 +000062static int qt2025c_wait_reset(struct efx_nic *efx)
63{
64 unsigned long timeout = jiffies + 10 * HZ;
Ben Hutchingsd2d2c372009-02-27 13:07:33 +000065 int reg, old_counter = 0;
66
67 /* Wait for firmware heartbeat to start */
68 for (;;) {
69 int counter;
Ben Hutchings68e7f452009-04-29 08:05:08 +000070 reg = efx_mdio_read(efx, MDIO_MMD_PCS, PCS_FW_HEARTBEAT_REG);
Ben Hutchingsd2d2c372009-02-27 13:07:33 +000071 if (reg < 0)
72 return reg;
73 counter = ((reg >> PCS_FW_HEARTB_LBN) &
74 ((1 << PCS_FW_HEARTB_WIDTH) - 1));
75 if (old_counter == 0)
76 old_counter = counter;
77 else if (counter != old_counter)
78 break;
79 if (time_after(jiffies, timeout))
80 return -ETIMEDOUT;
81 msleep(10);
82 }
83
84 /* Wait for firmware status to look good */
85 for (;;) {
Ben Hutchings68e7f452009-04-29 08:05:08 +000086 reg = efx_mdio_read(efx, MDIO_MMD_PCS, PCS_UC8051_STATUS_REG);
Ben Hutchingsd2d2c372009-02-27 13:07:33 +000087 if (reg < 0)
88 return reg;
89 if ((reg &
90 ((1 << PCS_UC_STATUS_WIDTH) - 1) << PCS_UC_STATUS_LBN) >=
91 PCS_UC_STATUS_FW_SAVE)
92 break;
93 if (time_after(jiffies, timeout))
94 return -ETIMEDOUT;
95 msleep(100);
96 }
97
98 return 0;
99}
100
101/* Reset the PHYXS MMD. This is documented (for the Quake PHYs) as doing
Ben Hutchings8ceee662008-04-27 12:55:59 +0100102 * a complete soft reset.
103 */
104static int xfp_reset_phy(struct efx_nic *efx)
105{
106 int rc;
107
Ben Hutchings68e7f452009-04-29 08:05:08 +0000108 rc = efx_mdio_reset_mmd(efx, MDIO_MMD_PHYXS,
109 XFP_MAX_RESET_TIME / XFP_RESET_WAIT,
110 XFP_RESET_WAIT);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100111 if (rc < 0)
112 goto fail;
113
Ben Hutchingsd2d2c372009-02-27 13:07:33 +0000114 if (efx->phy_type == PHY_TYPE_QT2025C) {
115 rc = qt2025c_wait_reset(efx);
116 if (rc < 0)
117 goto fail;
118 }
119
Ben Hutchings8ceee662008-04-27 12:55:59 +0100120 /* Wait 250ms for the PHY to complete bootup */
121 msleep(250);
122
123 /* Check that all the MMDs we expect are present and responding. We
124 * expect faults on some if the link is down, but not on the PHY XS */
Ben Hutchings68e7f452009-04-29 08:05:08 +0000125 rc = efx_mdio_check_mmds(efx, XFP_REQUIRED_DEVS, MDIO_DEVS_PHYXS);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100126 if (rc < 0)
127 goto fail;
128
129 efx->board_info.init_leds(efx);
130
131 return rc;
132
133 fail:
Ben Hutchingsf794fd42009-02-27 13:06:58 +0000134 EFX_ERR(efx, "PHY reset timed out\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100135 return rc;
136}
137
138static int xfp_phy_init(struct efx_nic *efx)
139{
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100140 struct xfp_phy_data *phy_data;
Ben Hutchings68e7f452009-04-29 08:05:08 +0000141 u32 devid = efx_mdio_read_id(efx, MDIO_MMD_PHYXS);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100142 int rc;
143
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100144 phy_data = kzalloc(sizeof(struct xfp_phy_data), GFP_KERNEL);
Ben Hutchings9b7bfc42008-05-16 21:20:20 +0100145 if (!phy_data)
146 return -ENOMEM;
Ben Hutchingsd3208b52008-05-16 21:20:00 +0100147 efx->phy_data = phy_data;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100148
Ben Hutchings3f39a5e2009-02-27 13:07:15 +0000149 EFX_INFO(efx, "PHY ID reg %x (OUI %06x model %02x revision %x)\n",
Ben Hutchings68e7f452009-04-29 08:05:08 +0000150 devid, efx_mdio_id_oui(devid), efx_mdio_id_model(devid),
151 efx_mdio_id_rev(devid));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100152
Ben Hutchingsf8b87c12008-09-01 12:48:17 +0100153 phy_data->phy_mode = efx->phy_mode;
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100154
Ben Hutchings8ceee662008-04-27 12:55:59 +0100155 rc = xfp_reset_phy(efx);
156
Ben Hutchingsf794fd42009-02-27 13:06:58 +0000157 EFX_INFO(efx, "PHY init %s.\n",
Ben Hutchings8ceee662008-04-27 12:55:59 +0100158 rc ? "failed" : "successful");
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100159 if (rc < 0)
160 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100161
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100162 return 0;
163
164 fail:
165 kfree(efx->phy_data);
166 efx->phy_data = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100167 return rc;
168}
169
170static void xfp_phy_clear_interrupt(struct efx_nic *efx)
171{
172 xenpack_clear_lasi_irqs(efx);
173}
174
175static int xfp_link_ok(struct efx_nic *efx)
176{
Ben Hutchings68e7f452009-04-29 08:05:08 +0000177 return efx_mdio_links_ok(efx, XFP_REQUIRED_DEVS);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100178}
179
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800180static void xfp_phy_poll(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100181{
Ben Hutchings8ceee662008-04-27 12:55:59 +0100182 int link_up = xfp_link_ok(efx);
183 /* Simulate a PHY event if link state has changed */
184 if (link_up != efx->link_up)
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800185 falcon_sim_phy_event(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100186}
187
188static void xfp_phy_reconfigure(struct efx_nic *efx)
189{
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100190 struct xfp_phy_data *phy_data = efx->phy_data;
191
Ben Hutchingsd2d2c372009-02-27 13:07:33 +0000192 if (efx->phy_type == PHY_TYPE_QT2025C) {
193 /* There are several different register bits which can
194 * disable TX (and save power) on direct-attach cables
195 * or optical transceivers, varying somewhat between
196 * firmware versions. Only 'static mode' appears to
197 * cover everything. */
Ben Hutchings68e7f452009-04-29 08:05:08 +0000198 mdio_set_flag(
199 &efx->mdio, efx->mdio.prtad, MDIO_MMD_PMAPMD,
200 PMA_PMD_FTX_CTRL2_REG, 1 << PMA_PMD_FTX_STATIC_LBN,
Ben Hutchingsd2d2c372009-02-27 13:07:33 +0000201 efx->phy_mode & PHY_MODE_TX_DISABLED ||
202 efx->phy_mode & PHY_MODE_LOW_POWER ||
203 efx->loopback_mode == LOOPBACK_PCS ||
204 efx->loopback_mode == LOOPBACK_PMAPMD);
205 } else {
206 /* Reset the PHY when moving from tx off to tx on */
207 if (!(efx->phy_mode & PHY_MODE_TX_DISABLED) &&
208 (phy_data->phy_mode & PHY_MODE_TX_DISABLED))
209 xfp_reset_phy(efx);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100210
Ben Hutchings68e7f452009-04-29 08:05:08 +0000211 efx_mdio_transmit_disable(efx);
Ben Hutchingsd2d2c372009-02-27 13:07:33 +0000212 }
213
Ben Hutchings68e7f452009-04-29 08:05:08 +0000214 efx_mdio_phy_reconfigure(efx);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100215
Ben Hutchingsf8b87c12008-09-01 12:48:17 +0100216 phy_data->phy_mode = efx->phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100217 efx->link_up = xfp_link_ok(efx);
Ben Hutchingsf31a45d2008-12-12 21:43:33 -0800218 efx->link_speed = 10000;
219 efx->link_fd = true;
Ben Hutchings04cc8ca2008-12-12 21:50:46 -0800220 efx->link_fc = efx->wanted_fc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100221}
222
Ben Hutchings68e7f452009-04-29 08:05:08 +0000223static void xfp_phy_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
224{
225 mdio45_ethtool_gset(&efx->mdio, ecmd);
226}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100227
228static void xfp_phy_fini(struct efx_nic *efx)
229{
230 /* Clobber the LED if it was blinking */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100231 efx->board_info.blink(efx, false);
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100232
233 /* Free the context block */
234 kfree(efx->phy_data);
235 efx->phy_data = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100236}
237
238struct efx_phy_operations falcon_xfp_phy_ops = {
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800239 .macs = EFX_XMAC,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100240 .init = xfp_phy_init,
241 .reconfigure = xfp_phy_reconfigure,
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800242 .poll = xfp_phy_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100243 .fini = xfp_phy_fini,
244 .clear_interrupt = xfp_phy_clear_interrupt,
Ben Hutchings68e7f452009-04-29 08:05:08 +0000245 .get_settings = xfp_phy_get_settings,
246 .set_settings = efx_mdio_set_settings,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100247 .mmds = XFP_REQUIRED_DEVS,
Ben Hutchings3273c2e2008-05-07 13:36:19 +0100248 .loopbacks = XFP_LOOPBACKS,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100249};