162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/****************************************************************************
362306a36Sopenharmony_ci * Driver for Solarflare network controllers and boards
462306a36Sopenharmony_ci * Copyright 2006-2012 Solarflare Communications Inc.
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci/*
762306a36Sopenharmony_ci * Driver for AMCC QT202x SFP+ and XFP adapters; see www.amcc.com for details
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/slab.h>
1162306a36Sopenharmony_ci#include <linux/timer.h>
1262306a36Sopenharmony_ci#include <linux/delay.h>
1362306a36Sopenharmony_ci#include "efx.h"
1462306a36Sopenharmony_ci#include "mdio_10g.h"
1562306a36Sopenharmony_ci#include "phy.h"
1662306a36Sopenharmony_ci#include "nic.h"
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci#define QT202X_REQUIRED_DEVS (MDIO_DEVS_PCS |		\
1962306a36Sopenharmony_ci			      MDIO_DEVS_PMAPMD |	\
2062306a36Sopenharmony_ci			      MDIO_DEVS_PHYXS)
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#define QT202X_LOOPBACKS ((1 << LOOPBACK_PCS) |		\
2362306a36Sopenharmony_ci			  (1 << LOOPBACK_PMAPMD) |	\
2462306a36Sopenharmony_ci			  (1 << LOOPBACK_PHYXS_WS))
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci/****************************************************************************/
2762306a36Sopenharmony_ci/* Quake-specific MDIO registers */
2862306a36Sopenharmony_ci#define MDIO_QUAKE_LED0_REG	(0xD006)
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci/* QT2025C only */
3162306a36Sopenharmony_ci#define PCS_FW_HEARTBEAT_REG	0xd7ee
3262306a36Sopenharmony_ci#define PCS_FW_HEARTB_LBN	0
3362306a36Sopenharmony_ci#define PCS_FW_HEARTB_WIDTH	8
3462306a36Sopenharmony_ci#define PCS_FW_PRODUCT_CODE_1	0xd7f0
3562306a36Sopenharmony_ci#define PCS_FW_VERSION_1	0xd7f3
3662306a36Sopenharmony_ci#define PCS_FW_BUILD_1		0xd7f6
3762306a36Sopenharmony_ci#define PCS_UC8051_STATUS_REG	0xd7fd
3862306a36Sopenharmony_ci#define PCS_UC_STATUS_LBN	0
3962306a36Sopenharmony_ci#define PCS_UC_STATUS_WIDTH	8
4062306a36Sopenharmony_ci#define PCS_UC_STATUS_FW_SAVE	0x20
4162306a36Sopenharmony_ci#define PMA_PMD_MODE_REG	0xc301
4262306a36Sopenharmony_ci#define PMA_PMD_RXIN_SEL_LBN	6
4362306a36Sopenharmony_ci#define PMA_PMD_FTX_CTRL2_REG	0xc309
4462306a36Sopenharmony_ci#define PMA_PMD_FTX_STATIC_LBN	13
4562306a36Sopenharmony_ci#define PMA_PMD_VEND1_REG	0xc001
4662306a36Sopenharmony_ci#define PMA_PMD_VEND1_LBTXD_LBN	15
4762306a36Sopenharmony_ci#define PCS_VEND1_REG		0xc000
4862306a36Sopenharmony_ci#define PCS_VEND1_LBTXD_LBN	5
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_civoid falcon_qt202x_set_led(struct ef4_nic *p, int led, int mode)
5162306a36Sopenharmony_ci{
5262306a36Sopenharmony_ci	int addr = MDIO_QUAKE_LED0_REG + led;
5362306a36Sopenharmony_ci	ef4_mdio_write(p, MDIO_MMD_PMAPMD, addr, mode);
5462306a36Sopenharmony_ci}
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_cistruct qt202x_phy_data {
5762306a36Sopenharmony_ci	enum ef4_phy_mode phy_mode;
5862306a36Sopenharmony_ci	bool bug17190_in_bad_state;
5962306a36Sopenharmony_ci	unsigned long bug17190_timer;
6062306a36Sopenharmony_ci	u32 firmware_ver;
6162306a36Sopenharmony_ci};
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci#define QT2022C2_MAX_RESET_TIME 500
6462306a36Sopenharmony_ci#define QT2022C2_RESET_WAIT 10
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci#define QT2025C_MAX_HEARTB_TIME (5 * HZ)
6762306a36Sopenharmony_ci#define QT2025C_HEARTB_WAIT 100
6862306a36Sopenharmony_ci#define QT2025C_MAX_FWSTART_TIME (25 * HZ / 10)
6962306a36Sopenharmony_ci#define QT2025C_FWSTART_WAIT 100
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci#define BUG17190_INTERVAL (2 * HZ)
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_cistatic int qt2025c_wait_heartbeat(struct ef4_nic *efx)
7462306a36Sopenharmony_ci{
7562306a36Sopenharmony_ci	unsigned long timeout = jiffies + QT2025C_MAX_HEARTB_TIME;
7662306a36Sopenharmony_ci	int reg, old_counter = 0;
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci	/* Wait for firmware heartbeat to start */
7962306a36Sopenharmony_ci	for (;;) {
8062306a36Sopenharmony_ci		int counter;
8162306a36Sopenharmony_ci		reg = ef4_mdio_read(efx, MDIO_MMD_PCS, PCS_FW_HEARTBEAT_REG);
8262306a36Sopenharmony_ci		if (reg < 0)
8362306a36Sopenharmony_ci			return reg;
8462306a36Sopenharmony_ci		counter = ((reg >> PCS_FW_HEARTB_LBN) &
8562306a36Sopenharmony_ci			    ((1 << PCS_FW_HEARTB_WIDTH) - 1));
8662306a36Sopenharmony_ci		if (old_counter == 0)
8762306a36Sopenharmony_ci			old_counter = counter;
8862306a36Sopenharmony_ci		else if (counter != old_counter)
8962306a36Sopenharmony_ci			break;
9062306a36Sopenharmony_ci		if (time_after(jiffies, timeout)) {
9162306a36Sopenharmony_ci			/* Some cables have EEPROMs that conflict with the
9262306a36Sopenharmony_ci			 * PHY's on-board EEPROM so it cannot load firmware */
9362306a36Sopenharmony_ci			netif_err(efx, hw, efx->net_dev,
9462306a36Sopenharmony_ci				  "If an SFP+ direct attach cable is"
9562306a36Sopenharmony_ci				  " connected, please check that it complies"
9662306a36Sopenharmony_ci				  " with the SFP+ specification\n");
9762306a36Sopenharmony_ci			return -ETIMEDOUT;
9862306a36Sopenharmony_ci		}
9962306a36Sopenharmony_ci		msleep(QT2025C_HEARTB_WAIT);
10062306a36Sopenharmony_ci	}
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci	return 0;
10362306a36Sopenharmony_ci}
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_cistatic int qt2025c_wait_fw_status_good(struct ef4_nic *efx)
10662306a36Sopenharmony_ci{
10762306a36Sopenharmony_ci	unsigned long timeout = jiffies + QT2025C_MAX_FWSTART_TIME;
10862306a36Sopenharmony_ci	int reg;
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	/* Wait for firmware status to look good */
11162306a36Sopenharmony_ci	for (;;) {
11262306a36Sopenharmony_ci		reg = ef4_mdio_read(efx, MDIO_MMD_PCS, PCS_UC8051_STATUS_REG);
11362306a36Sopenharmony_ci		if (reg < 0)
11462306a36Sopenharmony_ci			return reg;
11562306a36Sopenharmony_ci		if ((reg &
11662306a36Sopenharmony_ci		     ((1 << PCS_UC_STATUS_WIDTH) - 1) << PCS_UC_STATUS_LBN) >=
11762306a36Sopenharmony_ci		    PCS_UC_STATUS_FW_SAVE)
11862306a36Sopenharmony_ci			break;
11962306a36Sopenharmony_ci		if (time_after(jiffies, timeout))
12062306a36Sopenharmony_ci			return -ETIMEDOUT;
12162306a36Sopenharmony_ci		msleep(QT2025C_FWSTART_WAIT);
12262306a36Sopenharmony_ci	}
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	return 0;
12562306a36Sopenharmony_ci}
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_cistatic void qt2025c_restart_firmware(struct ef4_nic *efx)
12862306a36Sopenharmony_ci{
12962306a36Sopenharmony_ci	/* Restart microcontroller execution of firmware from RAM */
13062306a36Sopenharmony_ci	ef4_mdio_write(efx, 3, 0xe854, 0x00c0);
13162306a36Sopenharmony_ci	ef4_mdio_write(efx, 3, 0xe854, 0x0040);
13262306a36Sopenharmony_ci	msleep(50);
13362306a36Sopenharmony_ci}
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_cistatic int qt2025c_wait_reset(struct ef4_nic *efx)
13662306a36Sopenharmony_ci{
13762306a36Sopenharmony_ci	int rc;
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	rc = qt2025c_wait_heartbeat(efx);
14062306a36Sopenharmony_ci	if (rc != 0)
14162306a36Sopenharmony_ci		return rc;
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	rc = qt2025c_wait_fw_status_good(efx);
14462306a36Sopenharmony_ci	if (rc == -ETIMEDOUT) {
14562306a36Sopenharmony_ci		/* Bug 17689: occasionally heartbeat starts but firmware status
14662306a36Sopenharmony_ci		 * code never progresses beyond 0x00.  Try again, once, after
14762306a36Sopenharmony_ci		 * restarting execution of the firmware image. */
14862306a36Sopenharmony_ci		netif_dbg(efx, hw, efx->net_dev,
14962306a36Sopenharmony_ci			  "bashing QT2025C microcontroller\n");
15062306a36Sopenharmony_ci		qt2025c_restart_firmware(efx);
15162306a36Sopenharmony_ci		rc = qt2025c_wait_heartbeat(efx);
15262306a36Sopenharmony_ci		if (rc != 0)
15362306a36Sopenharmony_ci			return rc;
15462306a36Sopenharmony_ci		rc = qt2025c_wait_fw_status_good(efx);
15562306a36Sopenharmony_ci	}
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci	return rc;
15862306a36Sopenharmony_ci}
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_cistatic void qt2025c_firmware_id(struct ef4_nic *efx)
16162306a36Sopenharmony_ci{
16262306a36Sopenharmony_ci	struct qt202x_phy_data *phy_data = efx->phy_data;
16362306a36Sopenharmony_ci	u8 firmware_id[9];
16462306a36Sopenharmony_ci	size_t i;
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci	for (i = 0; i < sizeof(firmware_id); i++)
16762306a36Sopenharmony_ci		firmware_id[i] = ef4_mdio_read(efx, MDIO_MMD_PCS,
16862306a36Sopenharmony_ci					       PCS_FW_PRODUCT_CODE_1 + i);
16962306a36Sopenharmony_ci	netif_info(efx, probe, efx->net_dev,
17062306a36Sopenharmony_ci		   "QT2025C firmware %xr%d v%d.%d.%d.%d [20%02d-%02d-%02d]\n",
17162306a36Sopenharmony_ci		   (firmware_id[0] << 8) | firmware_id[1], firmware_id[2],
17262306a36Sopenharmony_ci		   firmware_id[3] >> 4, firmware_id[3] & 0xf,
17362306a36Sopenharmony_ci		   firmware_id[4], firmware_id[5],
17462306a36Sopenharmony_ci		   firmware_id[6], firmware_id[7], firmware_id[8]);
17562306a36Sopenharmony_ci	phy_data->firmware_ver = ((firmware_id[3] & 0xf0) << 20) |
17662306a36Sopenharmony_ci				 ((firmware_id[3] & 0x0f) << 16) |
17762306a36Sopenharmony_ci				 (firmware_id[4] << 8) | firmware_id[5];
17862306a36Sopenharmony_ci}
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_cistatic void qt2025c_bug17190_workaround(struct ef4_nic *efx)
18162306a36Sopenharmony_ci{
18262306a36Sopenharmony_ci	struct qt202x_phy_data *phy_data = efx->phy_data;
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci	/* The PHY can get stuck in a state where it reports PHY_XS and PMA/PMD
18562306a36Sopenharmony_ci	 * layers up, but PCS down (no block_lock).  If we notice this state
18662306a36Sopenharmony_ci	 * persisting for a couple of seconds, we switch PMA/PMD loopback
18762306a36Sopenharmony_ci	 * briefly on and then off again, which is normally sufficient to
18862306a36Sopenharmony_ci	 * recover it.
18962306a36Sopenharmony_ci	 */
19062306a36Sopenharmony_ci	if (efx->link_state.up ||
19162306a36Sopenharmony_ci	    !ef4_mdio_links_ok(efx, MDIO_DEVS_PMAPMD | MDIO_DEVS_PHYXS)) {
19262306a36Sopenharmony_ci		phy_data->bug17190_in_bad_state = false;
19362306a36Sopenharmony_ci		return;
19462306a36Sopenharmony_ci	}
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci	if (!phy_data->bug17190_in_bad_state) {
19762306a36Sopenharmony_ci		phy_data->bug17190_in_bad_state = true;
19862306a36Sopenharmony_ci		phy_data->bug17190_timer = jiffies + BUG17190_INTERVAL;
19962306a36Sopenharmony_ci		return;
20062306a36Sopenharmony_ci	}
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci	if (time_after_eq(jiffies, phy_data->bug17190_timer)) {
20362306a36Sopenharmony_ci		netif_dbg(efx, hw, efx->net_dev, "bashing QT2025C PMA/PMD\n");
20462306a36Sopenharmony_ci		ef4_mdio_set_flag(efx, MDIO_MMD_PMAPMD, MDIO_CTRL1,
20562306a36Sopenharmony_ci				  MDIO_PMA_CTRL1_LOOPBACK, true);
20662306a36Sopenharmony_ci		msleep(100);
20762306a36Sopenharmony_ci		ef4_mdio_set_flag(efx, MDIO_MMD_PMAPMD, MDIO_CTRL1,
20862306a36Sopenharmony_ci				  MDIO_PMA_CTRL1_LOOPBACK, false);
20962306a36Sopenharmony_ci		phy_data->bug17190_timer = jiffies + BUG17190_INTERVAL;
21062306a36Sopenharmony_ci	}
21162306a36Sopenharmony_ci}
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_cistatic int qt2025c_select_phy_mode(struct ef4_nic *efx)
21462306a36Sopenharmony_ci{
21562306a36Sopenharmony_ci	struct qt202x_phy_data *phy_data = efx->phy_data;
21662306a36Sopenharmony_ci	struct falcon_board *board = falcon_board(efx);
21762306a36Sopenharmony_ci	int reg, rc, i;
21862306a36Sopenharmony_ci	uint16_t phy_op_mode;
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci	/* Only 2.0.1.0+ PHY firmware supports the more optimal SFP+
22162306a36Sopenharmony_ci	 * Self-Configure mode.  Don't attempt any switching if we encounter
22262306a36Sopenharmony_ci	 * older firmware. */
22362306a36Sopenharmony_ci	if (phy_data->firmware_ver < 0x02000100)
22462306a36Sopenharmony_ci		return 0;
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci	/* In general we will get optimal behaviour in "SFP+ Self-Configure"
22762306a36Sopenharmony_ci	 * mode; however, that powers down most of the PHY when no module is
22862306a36Sopenharmony_ci	 * present, so we must use a different mode (any fixed mode will do)
22962306a36Sopenharmony_ci	 * to be sure that loopbacks will work. */
23062306a36Sopenharmony_ci	phy_op_mode = (efx->loopback_mode == LOOPBACK_NONE) ? 0x0038 : 0x0020;
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_ci	/* Only change mode if really necessary */
23362306a36Sopenharmony_ci	reg = ef4_mdio_read(efx, 1, 0xc319);
23462306a36Sopenharmony_ci	if ((reg & 0x0038) == phy_op_mode)
23562306a36Sopenharmony_ci		return 0;
23662306a36Sopenharmony_ci	netif_dbg(efx, hw, efx->net_dev, "Switching PHY to mode 0x%04x\n",
23762306a36Sopenharmony_ci		  phy_op_mode);
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci	/* This sequence replicates the register writes configured in the boot
24062306a36Sopenharmony_ci	 * EEPROM (including the differences between board revisions), except
24162306a36Sopenharmony_ci	 * that the operating mode is changed, and the PHY is prevented from
24262306a36Sopenharmony_ci	 * unnecessarily reloading the main firmware image again. */
24362306a36Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xc300, 0x0000);
24462306a36Sopenharmony_ci	/* (Note: this portion of the boot EEPROM sequence, which bit-bashes 9
24562306a36Sopenharmony_ci	 * STOPs onto the firmware/module I2C bus to reset it, varies across
24662306a36Sopenharmony_ci	 * board revisions, as the bus is connected to different GPIO/LED
24762306a36Sopenharmony_ci	 * outputs on the PHY.) */
24862306a36Sopenharmony_ci	if (board->major == 0 && board->minor < 2) {
24962306a36Sopenharmony_ci		ef4_mdio_write(efx, 1, 0xc303, 0x4498);
25062306a36Sopenharmony_ci		for (i = 0; i < 9; i++) {
25162306a36Sopenharmony_ci			ef4_mdio_write(efx, 1, 0xc303, 0x4488);
25262306a36Sopenharmony_ci			ef4_mdio_write(efx, 1, 0xc303, 0x4480);
25362306a36Sopenharmony_ci			ef4_mdio_write(efx, 1, 0xc303, 0x4490);
25462306a36Sopenharmony_ci			ef4_mdio_write(efx, 1, 0xc303, 0x4498);
25562306a36Sopenharmony_ci		}
25662306a36Sopenharmony_ci	} else {
25762306a36Sopenharmony_ci		ef4_mdio_write(efx, 1, 0xc303, 0x0920);
25862306a36Sopenharmony_ci		ef4_mdio_write(efx, 1, 0xd008, 0x0004);
25962306a36Sopenharmony_ci		for (i = 0; i < 9; i++) {
26062306a36Sopenharmony_ci			ef4_mdio_write(efx, 1, 0xc303, 0x0900);
26162306a36Sopenharmony_ci			ef4_mdio_write(efx, 1, 0xd008, 0x0005);
26262306a36Sopenharmony_ci			ef4_mdio_write(efx, 1, 0xc303, 0x0920);
26362306a36Sopenharmony_ci			ef4_mdio_write(efx, 1, 0xd008, 0x0004);
26462306a36Sopenharmony_ci		}
26562306a36Sopenharmony_ci		ef4_mdio_write(efx, 1, 0xc303, 0x4900);
26662306a36Sopenharmony_ci	}
26762306a36Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xc303, 0x4900);
26862306a36Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xc302, 0x0004);
26962306a36Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xc316, 0x0013);
27062306a36Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xc318, 0x0054);
27162306a36Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xc319, phy_op_mode);
27262306a36Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xc31a, 0x0098);
27362306a36Sopenharmony_ci	ef4_mdio_write(efx, 3, 0x0026, 0x0e00);
27462306a36Sopenharmony_ci	ef4_mdio_write(efx, 3, 0x0027, 0x0013);
27562306a36Sopenharmony_ci	ef4_mdio_write(efx, 3, 0x0028, 0xa528);
27662306a36Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xd006, 0x000a);
27762306a36Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xd007, 0x0009);
27862306a36Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xd008, 0x0004);
27962306a36Sopenharmony_ci	/* This additional write is not present in the boot EEPROM.  It
28062306a36Sopenharmony_ci	 * prevents the PHY's internal boot ROM doing another pointless (and
28162306a36Sopenharmony_ci	 * slow) reload of the firmware image (the microcontroller's code
28262306a36Sopenharmony_ci	 * memory is not affected by the microcontroller reset). */
28362306a36Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xc317, 0x00ff);
28462306a36Sopenharmony_ci	/* PMA/PMD loopback sets RXIN to inverse polarity and the firmware
28562306a36Sopenharmony_ci	 * restart doesn't reset it. We need to do that ourselves. */
28662306a36Sopenharmony_ci	ef4_mdio_set_flag(efx, 1, PMA_PMD_MODE_REG,
28762306a36Sopenharmony_ci			  1 << PMA_PMD_RXIN_SEL_LBN, false);
28862306a36Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xc300, 0x0002);
28962306a36Sopenharmony_ci	msleep(20);
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ci	/* Restart microcontroller execution of firmware from RAM */
29262306a36Sopenharmony_ci	qt2025c_restart_firmware(efx);
29362306a36Sopenharmony_ci
29462306a36Sopenharmony_ci	/* Wait for the microcontroller to be ready again */
29562306a36Sopenharmony_ci	rc = qt2025c_wait_reset(efx);
29662306a36Sopenharmony_ci	if (rc < 0) {
29762306a36Sopenharmony_ci		netif_err(efx, hw, efx->net_dev,
29862306a36Sopenharmony_ci			  "PHY microcontroller reset during mode switch "
29962306a36Sopenharmony_ci			  "timed out\n");
30062306a36Sopenharmony_ci		return rc;
30162306a36Sopenharmony_ci	}
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci	return 0;
30462306a36Sopenharmony_ci}
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_cistatic int qt202x_reset_phy(struct ef4_nic *efx)
30762306a36Sopenharmony_ci{
30862306a36Sopenharmony_ci	int rc;
30962306a36Sopenharmony_ci
31062306a36Sopenharmony_ci	if (efx->phy_type == PHY_TYPE_QT2025C) {
31162306a36Sopenharmony_ci		/* Wait for the reset triggered by falcon_reset_hw()
31262306a36Sopenharmony_ci		 * to complete */
31362306a36Sopenharmony_ci		rc = qt2025c_wait_reset(efx);
31462306a36Sopenharmony_ci		if (rc < 0)
31562306a36Sopenharmony_ci			goto fail;
31662306a36Sopenharmony_ci	} else {
31762306a36Sopenharmony_ci		/* Reset the PHYXS MMD. This is documented as doing
31862306a36Sopenharmony_ci		 * a complete soft reset. */
31962306a36Sopenharmony_ci		rc = ef4_mdio_reset_mmd(efx, MDIO_MMD_PHYXS,
32062306a36Sopenharmony_ci					QT2022C2_MAX_RESET_TIME /
32162306a36Sopenharmony_ci					QT2022C2_RESET_WAIT,
32262306a36Sopenharmony_ci					QT2022C2_RESET_WAIT);
32362306a36Sopenharmony_ci		if (rc < 0)
32462306a36Sopenharmony_ci			goto fail;
32562306a36Sopenharmony_ci	}
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ci	/* Wait 250ms for the PHY to complete bootup */
32862306a36Sopenharmony_ci	msleep(250);
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_ci	falcon_board(efx)->type->init_phy(efx);
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci	return 0;
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci fail:
33562306a36Sopenharmony_ci	netif_err(efx, hw, efx->net_dev, "PHY reset timed out\n");
33662306a36Sopenharmony_ci	return rc;
33762306a36Sopenharmony_ci}
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_cistatic int qt202x_phy_probe(struct ef4_nic *efx)
34062306a36Sopenharmony_ci{
34162306a36Sopenharmony_ci	struct qt202x_phy_data *phy_data;
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_ci	phy_data = kzalloc(sizeof(struct qt202x_phy_data), GFP_KERNEL);
34462306a36Sopenharmony_ci	if (!phy_data)
34562306a36Sopenharmony_ci		return -ENOMEM;
34662306a36Sopenharmony_ci	efx->phy_data = phy_data;
34762306a36Sopenharmony_ci	phy_data->phy_mode = efx->phy_mode;
34862306a36Sopenharmony_ci	phy_data->bug17190_in_bad_state = false;
34962306a36Sopenharmony_ci	phy_data->bug17190_timer = 0;
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_ci	efx->mdio.mmds = QT202X_REQUIRED_DEVS;
35262306a36Sopenharmony_ci	efx->mdio.mode_support = MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
35362306a36Sopenharmony_ci	efx->loopback_modes = QT202X_LOOPBACKS | FALCON_XMAC_LOOPBACKS;
35462306a36Sopenharmony_ci	return 0;
35562306a36Sopenharmony_ci}
35662306a36Sopenharmony_ci
35762306a36Sopenharmony_cistatic int qt202x_phy_init(struct ef4_nic *efx)
35862306a36Sopenharmony_ci{
35962306a36Sopenharmony_ci	u32 devid;
36062306a36Sopenharmony_ci	int rc;
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ci	rc = qt202x_reset_phy(efx);
36362306a36Sopenharmony_ci	if (rc) {
36462306a36Sopenharmony_ci		netif_err(efx, probe, efx->net_dev, "PHY init failed\n");
36562306a36Sopenharmony_ci		return rc;
36662306a36Sopenharmony_ci	}
36762306a36Sopenharmony_ci
36862306a36Sopenharmony_ci	devid = ef4_mdio_read_id(efx, MDIO_MMD_PHYXS);
36962306a36Sopenharmony_ci	netif_info(efx, probe, efx->net_dev,
37062306a36Sopenharmony_ci		   "PHY ID reg %x (OUI %06x model %02x revision %x)\n",
37162306a36Sopenharmony_ci		   devid, ef4_mdio_id_oui(devid), ef4_mdio_id_model(devid),
37262306a36Sopenharmony_ci		   ef4_mdio_id_rev(devid));
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ci	if (efx->phy_type == PHY_TYPE_QT2025C)
37562306a36Sopenharmony_ci		qt2025c_firmware_id(efx);
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_ci	return 0;
37862306a36Sopenharmony_ci}
37962306a36Sopenharmony_ci
38062306a36Sopenharmony_cistatic int qt202x_link_ok(struct ef4_nic *efx)
38162306a36Sopenharmony_ci{
38262306a36Sopenharmony_ci	return ef4_mdio_links_ok(efx, QT202X_REQUIRED_DEVS);
38362306a36Sopenharmony_ci}
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_cistatic bool qt202x_phy_poll(struct ef4_nic *efx)
38662306a36Sopenharmony_ci{
38762306a36Sopenharmony_ci	bool was_up = efx->link_state.up;
38862306a36Sopenharmony_ci
38962306a36Sopenharmony_ci	efx->link_state.up = qt202x_link_ok(efx);
39062306a36Sopenharmony_ci	efx->link_state.speed = 10000;
39162306a36Sopenharmony_ci	efx->link_state.fd = true;
39262306a36Sopenharmony_ci	efx->link_state.fc = efx->wanted_fc;
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci	if (efx->phy_type == PHY_TYPE_QT2025C)
39562306a36Sopenharmony_ci		qt2025c_bug17190_workaround(efx);
39662306a36Sopenharmony_ci
39762306a36Sopenharmony_ci	return efx->link_state.up != was_up;
39862306a36Sopenharmony_ci}
39962306a36Sopenharmony_ci
40062306a36Sopenharmony_cistatic int qt202x_phy_reconfigure(struct ef4_nic *efx)
40162306a36Sopenharmony_ci{
40262306a36Sopenharmony_ci	struct qt202x_phy_data *phy_data = efx->phy_data;
40362306a36Sopenharmony_ci
40462306a36Sopenharmony_ci	if (efx->phy_type == PHY_TYPE_QT2025C) {
40562306a36Sopenharmony_ci		int rc = qt2025c_select_phy_mode(efx);
40662306a36Sopenharmony_ci		if (rc)
40762306a36Sopenharmony_ci			return rc;
40862306a36Sopenharmony_ci
40962306a36Sopenharmony_ci		/* There are several different register bits which can
41062306a36Sopenharmony_ci		 * disable TX (and save power) on direct-attach cables
41162306a36Sopenharmony_ci		 * or optical transceivers, varying somewhat between
41262306a36Sopenharmony_ci		 * firmware versions.  Only 'static mode' appears to
41362306a36Sopenharmony_ci		 * cover everything. */
41462306a36Sopenharmony_ci		mdio_set_flag(
41562306a36Sopenharmony_ci			&efx->mdio, efx->mdio.prtad, MDIO_MMD_PMAPMD,
41662306a36Sopenharmony_ci			PMA_PMD_FTX_CTRL2_REG, 1 << PMA_PMD_FTX_STATIC_LBN,
41762306a36Sopenharmony_ci			efx->phy_mode & PHY_MODE_TX_DISABLED ||
41862306a36Sopenharmony_ci			efx->phy_mode & PHY_MODE_LOW_POWER ||
41962306a36Sopenharmony_ci			efx->loopback_mode == LOOPBACK_PCS ||
42062306a36Sopenharmony_ci			efx->loopback_mode == LOOPBACK_PMAPMD);
42162306a36Sopenharmony_ci	} else {
42262306a36Sopenharmony_ci		/* Reset the PHY when moving from tx off to tx on */
42362306a36Sopenharmony_ci		if (!(efx->phy_mode & PHY_MODE_TX_DISABLED) &&
42462306a36Sopenharmony_ci		    (phy_data->phy_mode & PHY_MODE_TX_DISABLED))
42562306a36Sopenharmony_ci			qt202x_reset_phy(efx);
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ci		ef4_mdio_transmit_disable(efx);
42862306a36Sopenharmony_ci	}
42962306a36Sopenharmony_ci
43062306a36Sopenharmony_ci	ef4_mdio_phy_reconfigure(efx);
43162306a36Sopenharmony_ci
43262306a36Sopenharmony_ci	phy_data->phy_mode = efx->phy_mode;
43362306a36Sopenharmony_ci
43462306a36Sopenharmony_ci	return 0;
43562306a36Sopenharmony_ci}
43662306a36Sopenharmony_ci
43762306a36Sopenharmony_cistatic void qt202x_phy_get_link_ksettings(struct ef4_nic *efx,
43862306a36Sopenharmony_ci					  struct ethtool_link_ksettings *cmd)
43962306a36Sopenharmony_ci{
44062306a36Sopenharmony_ci	mdio45_ethtool_ksettings_get(&efx->mdio, cmd);
44162306a36Sopenharmony_ci}
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_cistatic void qt202x_phy_remove(struct ef4_nic *efx)
44462306a36Sopenharmony_ci{
44562306a36Sopenharmony_ci	/* Free the context block */
44662306a36Sopenharmony_ci	kfree(efx->phy_data);
44762306a36Sopenharmony_ci	efx->phy_data = NULL;
44862306a36Sopenharmony_ci}
44962306a36Sopenharmony_ci
45062306a36Sopenharmony_cistatic int qt202x_phy_get_module_info(struct ef4_nic *efx,
45162306a36Sopenharmony_ci				      struct ethtool_modinfo *modinfo)
45262306a36Sopenharmony_ci{
45362306a36Sopenharmony_ci	modinfo->type = ETH_MODULE_SFF_8079;
45462306a36Sopenharmony_ci	modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
45562306a36Sopenharmony_ci	return 0;
45662306a36Sopenharmony_ci}
45762306a36Sopenharmony_ci
45862306a36Sopenharmony_cistatic int qt202x_phy_get_module_eeprom(struct ef4_nic *efx,
45962306a36Sopenharmony_ci					struct ethtool_eeprom *ee, u8 *data)
46062306a36Sopenharmony_ci{
46162306a36Sopenharmony_ci	int mmd, reg_base, rc, i;
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_ci	if (efx->phy_type == PHY_TYPE_QT2025C) {
46462306a36Sopenharmony_ci		mmd = MDIO_MMD_PCS;
46562306a36Sopenharmony_ci		reg_base = 0xd000;
46662306a36Sopenharmony_ci	} else {
46762306a36Sopenharmony_ci		mmd = MDIO_MMD_PMAPMD;
46862306a36Sopenharmony_ci		reg_base = 0x8007;
46962306a36Sopenharmony_ci	}
47062306a36Sopenharmony_ci
47162306a36Sopenharmony_ci	for (i = 0; i < ee->len; i++) {
47262306a36Sopenharmony_ci		rc = ef4_mdio_read(efx, mmd, reg_base + ee->offset + i);
47362306a36Sopenharmony_ci		if (rc < 0)
47462306a36Sopenharmony_ci			return rc;
47562306a36Sopenharmony_ci		data[i] = rc;
47662306a36Sopenharmony_ci	}
47762306a36Sopenharmony_ci
47862306a36Sopenharmony_ci	return 0;
47962306a36Sopenharmony_ci}
48062306a36Sopenharmony_ci
48162306a36Sopenharmony_ciconst struct ef4_phy_operations falcon_qt202x_phy_ops = {
48262306a36Sopenharmony_ci	.probe		 = qt202x_phy_probe,
48362306a36Sopenharmony_ci	.init		 = qt202x_phy_init,
48462306a36Sopenharmony_ci	.reconfigure	 = qt202x_phy_reconfigure,
48562306a36Sopenharmony_ci	.poll		 = qt202x_phy_poll,
48662306a36Sopenharmony_ci	.fini		 = ef4_port_dummy_op_void,
48762306a36Sopenharmony_ci	.remove		 = qt202x_phy_remove,
48862306a36Sopenharmony_ci	.get_link_ksettings = qt202x_phy_get_link_ksettings,
48962306a36Sopenharmony_ci	.set_link_ksettings = ef4_mdio_set_link_ksettings,
49062306a36Sopenharmony_ci	.test_alive	 = ef4_mdio_test_alive,
49162306a36Sopenharmony_ci	.get_module_eeprom = qt202x_phy_get_module_eeprom,
49262306a36Sopenharmony_ci	.get_module_info = qt202x_phy_get_module_info,
49362306a36Sopenharmony_ci};
494