xfp_phy.c 6.3 KB
Newer Older
1 2 3 4 5 6 7 8 9
/****************************************************************************
 * Driver for Solarflare Solarstorm network controllers and boards
 * Copyright 2006-2008 Solarflare Communications Inc.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation, incorporated herein by reference.
 */
/*
B
Ben Hutchings 已提交
10 11
 * Driver for SFP+ and XFP optical PHYs plus some support specific to the
 * AMCC QT20xx adapters; see www.amcc.com for details
12 13 14 15 16 17 18
 */

#include <linux/timer.h>
#include <linux/delay.h>
#include "efx.h"
#include "mdio_10g.h"
#include "phy.h"
19
#include "falcon.h"
20

21 22 23
#define XFP_REQUIRED_DEVS (MDIO_DEVS_PCS |	\
			   MDIO_DEVS_PMAPMD |	\
			   MDIO_DEVS_PHYXS)
24

25 26 27 28
#define XFP_LOOPBACKS ((1 << LOOPBACK_PCS) |		\
		       (1 << LOOPBACK_PMAPMD) |		\
		       (1 << LOOPBACK_NETWORK))

29 30 31 32
/****************************************************************************/
/* Quake-specific MDIO registers */
#define MDIO_QUAKE_LED0_REG	(0xD006)

B
Ben Hutchings 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/* QT2025C only */
#define PCS_FW_HEARTBEAT_REG	0xd7ee
#define PCS_FW_HEARTB_LBN	0
#define PCS_FW_HEARTB_WIDTH	8
#define PCS_UC8051_STATUS_REG	0xd7fd
#define PCS_UC_STATUS_LBN	0
#define PCS_UC_STATUS_WIDTH	8
#define PCS_UC_STATUS_FW_SAVE	0x20
#define PMA_PMD_FTX_CTRL2_REG	0xc309
#define PMA_PMD_FTX_STATIC_LBN	13
#define PMA_PMD_VEND1_REG	0xc001
#define PMA_PMD_VEND1_LBTXD_LBN	15
#define PCS_VEND1_REG	   	0xc000
#define PCS_VEND1_LBTXD_LBN	5

48 49 50
void xfp_set_led(struct efx_nic *p, int led, int mode)
{
	int addr = MDIO_QUAKE_LED0_REG + led;
51
	efx_mdio_write(p, MDIO_MMD_PMAPMD, addr, mode);
52 53
}

54
struct xfp_phy_data {
55
	enum efx_phy_mode phy_mode;
56 57
};

58 59 60
#define XFP_MAX_RESET_TIME 500
#define XFP_RESET_WAIT 10

B
Ben Hutchings 已提交
61 62 63 64 65 66 67 68
static int qt2025c_wait_reset(struct efx_nic *efx)
{
	unsigned long timeout = jiffies + 10 * HZ;
	int reg, old_counter = 0;

	/* Wait for firmware heartbeat to start */
	for (;;) {
		int counter;
69
		reg = efx_mdio_read(efx, MDIO_MMD_PCS, PCS_FW_HEARTBEAT_REG);
B
Ben Hutchings 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
		if (reg < 0)
			return reg;
		counter = ((reg >> PCS_FW_HEARTB_LBN) &
			    ((1 << PCS_FW_HEARTB_WIDTH) - 1));
		if (old_counter == 0)
			old_counter = counter;
		else if (counter != old_counter)
			break;
		if (time_after(jiffies, timeout))
			return -ETIMEDOUT;
		msleep(10);
	}

	/* Wait for firmware status to look good */
	for (;;) {
85
		reg = efx_mdio_read(efx, MDIO_MMD_PCS, PCS_UC8051_STATUS_REG);
B
Ben Hutchings 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
		if (reg < 0)
			return reg;
		if ((reg &
		     ((1 << PCS_UC_STATUS_WIDTH) - 1) << PCS_UC_STATUS_LBN) >=
		    PCS_UC_STATUS_FW_SAVE)
			break;
		if (time_after(jiffies, timeout))
			return -ETIMEDOUT;
		msleep(100);
	}

	return 0;
}

/* Reset the PHYXS MMD. This is documented (for the Quake PHYs) as doing
101 102 103 104 105 106
 * a complete soft reset.
 */
static int xfp_reset_phy(struct efx_nic *efx)
{
	int rc;

107 108 109
	rc = efx_mdio_reset_mmd(efx, MDIO_MMD_PHYXS,
				XFP_MAX_RESET_TIME / XFP_RESET_WAIT,
				XFP_RESET_WAIT);
110 111 112
	if (rc < 0)
		goto fail;

B
Ben Hutchings 已提交
113 114 115 116 117 118
	if (efx->phy_type == PHY_TYPE_QT2025C) {
		rc = qt2025c_wait_reset(efx);
		if (rc < 0)
			goto fail;
	}

119 120 121 122 123
	/* Wait 250ms for the PHY to complete bootup */
	msleep(250);

	/* Check that all the MMDs we expect are present and responding. We
	 * expect faults on some if the link is down, but not on the PHY XS */
124
	rc = efx_mdio_check_mmds(efx, XFP_REQUIRED_DEVS, MDIO_DEVS_PHYXS);
125 126 127 128 129 130 131 132
	if (rc < 0)
		goto fail;

	efx->board_info.init_leds(efx);

	return rc;

 fail:
133
	EFX_ERR(efx, "PHY reset timed out\n");
134 135 136 137 138
	return rc;
}

static int xfp_phy_init(struct efx_nic *efx)
{
139
	struct xfp_phy_data *phy_data;
140
	u32 devid = efx_mdio_read_id(efx, MDIO_MMD_PHYXS);
141 142
	int rc;

143
	phy_data = kzalloc(sizeof(struct xfp_phy_data), GFP_KERNEL);
144 145
	if (!phy_data)
		return -ENOMEM;
146
	efx->phy_data = phy_data;
147

B
Ben Hutchings 已提交
148
	EFX_INFO(efx, "PHY ID reg %x (OUI %06x model %02x revision %x)\n",
149 150
		 devid, efx_mdio_id_oui(devid), efx_mdio_id_model(devid),
		 efx_mdio_id_rev(devid));
151

152
	phy_data->phy_mode = efx->phy_mode;
153

154 155
	rc = xfp_reset_phy(efx);

156
	EFX_INFO(efx, "PHY init %s.\n",
157
		 rc ? "failed" : "successful");
158 159
	if (rc < 0)
		goto fail;
160

161 162 163 164 165
	return 0;

 fail:
	kfree(efx->phy_data);
	efx->phy_data = NULL;
166 167 168 169 170
	return rc;
}

static void xfp_phy_clear_interrupt(struct efx_nic *efx)
{
171 172
	/* Read to clear link status alarm */
	efx_mdio_read(efx, MDIO_MMD_PMAPMD, MDIO_PMA_LASI_STAT);
173 174 175 176
}

static int xfp_link_ok(struct efx_nic *efx)
{
177
	return efx_mdio_links_ok(efx, XFP_REQUIRED_DEVS);
178 179
}

180
static void xfp_phy_poll(struct efx_nic *efx)
181 182 183 184
{
	int link_up = xfp_link_ok(efx);
	/* Simulate a PHY event if link state has changed */
	if (link_up != efx->link_up)
185
		falcon_sim_phy_event(efx);
186 187 188 189
}

static void xfp_phy_reconfigure(struct efx_nic *efx)
{
190 191
	struct xfp_phy_data *phy_data = efx->phy_data;

B
Ben Hutchings 已提交
192 193 194 195 196 197
	if (efx->phy_type == PHY_TYPE_QT2025C) {
		/* There are several different register bits which can
		 * disable TX (and save power) on direct-attach cables
		 * or optical transceivers, varying somewhat between
		 * firmware versions.  Only 'static mode' appears to
		 * cover everything. */
198 199 200
		mdio_set_flag(
			&efx->mdio, efx->mdio.prtad, MDIO_MMD_PMAPMD,
			PMA_PMD_FTX_CTRL2_REG, 1 << PMA_PMD_FTX_STATIC_LBN,
B
Ben Hutchings 已提交
201 202 203 204 205 206 207 208 209 210
			efx->phy_mode & PHY_MODE_TX_DISABLED ||
			efx->phy_mode & PHY_MODE_LOW_POWER ||
			efx->loopback_mode == LOOPBACK_PCS ||
			efx->loopback_mode == LOOPBACK_PMAPMD);
	} else {
		/* Reset the PHY when moving from tx off to tx on */
		if (!(efx->phy_mode & PHY_MODE_TX_DISABLED) &&
		    (phy_data->phy_mode & PHY_MODE_TX_DISABLED))
			xfp_reset_phy(efx);

211
		efx_mdio_transmit_disable(efx);
B
Ben Hutchings 已提交
212
	}
213

214
	efx_mdio_phy_reconfigure(efx);
215

216
	phy_data->phy_mode = efx->phy_mode;
217
	efx->link_up = xfp_link_ok(efx);
B
Ben Hutchings 已提交
218 219
	efx->link_speed = 10000;
	efx->link_fd = true;
B
Ben Hutchings 已提交
220
	efx->link_fc = efx->wanted_fc;
221 222
}

223 224 225 226
static void xfp_phy_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
{
	mdio45_ethtool_gset(&efx->mdio, ecmd);
}
227 228 229 230

static void xfp_phy_fini(struct efx_nic *efx)
{
	/* Clobber the LED if it was blinking */
231
	efx->board_info.blink(efx, false);
232 233 234 235

	/* Free the context block */
	kfree(efx->phy_data);
	efx->phy_data = NULL;
236 237 238
}

struct efx_phy_operations falcon_xfp_phy_ops = {
239
	.macs		 = EFX_XMAC,
240 241
	.init            = xfp_phy_init,
	.reconfigure     = xfp_phy_reconfigure,
242
	.poll            = xfp_phy_poll,
243 244
	.fini            = xfp_phy_fini,
	.clear_interrupt = xfp_phy_clear_interrupt,
245 246
	.get_settings    = xfp_phy_get_settings,
	.set_settings	 = efx_mdio_set_settings,
247
	.mmds            = XFP_REQUIRED_DEVS,
248
	.loopbacks       = XFP_LOOPBACKS,
249
};