18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/****************************************************************************
38c2ecf20Sopenharmony_ci * Driver for Solarflare network controllers and boards
48c2ecf20Sopenharmony_ci * Copyright 2006-2012 Solarflare Communications Inc.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci/*
78c2ecf20Sopenharmony_ci * Driver for AMCC QT202x SFP+ and XFP adapters; see www.amcc.com for details
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/slab.h>
118c2ecf20Sopenharmony_ci#include <linux/timer.h>
128c2ecf20Sopenharmony_ci#include <linux/delay.h>
138c2ecf20Sopenharmony_ci#include "efx.h"
148c2ecf20Sopenharmony_ci#include "mdio_10g.h"
158c2ecf20Sopenharmony_ci#include "phy.h"
168c2ecf20Sopenharmony_ci#include "nic.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define QT202X_REQUIRED_DEVS (MDIO_DEVS_PCS |		\
198c2ecf20Sopenharmony_ci			      MDIO_DEVS_PMAPMD |	\
208c2ecf20Sopenharmony_ci			      MDIO_DEVS_PHYXS)
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define QT202X_LOOPBACKS ((1 << LOOPBACK_PCS) |		\
238c2ecf20Sopenharmony_ci			  (1 << LOOPBACK_PMAPMD) |	\
248c2ecf20Sopenharmony_ci			  (1 << LOOPBACK_PHYXS_WS))
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/****************************************************************************/
278c2ecf20Sopenharmony_ci/* Quake-specific MDIO registers */
288c2ecf20Sopenharmony_ci#define MDIO_QUAKE_LED0_REG	(0xD006)
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/* QT2025C only */
318c2ecf20Sopenharmony_ci#define PCS_FW_HEARTBEAT_REG	0xd7ee
328c2ecf20Sopenharmony_ci#define PCS_FW_HEARTB_LBN	0
338c2ecf20Sopenharmony_ci#define PCS_FW_HEARTB_WIDTH	8
348c2ecf20Sopenharmony_ci#define PCS_FW_PRODUCT_CODE_1	0xd7f0
358c2ecf20Sopenharmony_ci#define PCS_FW_VERSION_1	0xd7f3
368c2ecf20Sopenharmony_ci#define PCS_FW_BUILD_1		0xd7f6
378c2ecf20Sopenharmony_ci#define PCS_UC8051_STATUS_REG	0xd7fd
388c2ecf20Sopenharmony_ci#define PCS_UC_STATUS_LBN	0
398c2ecf20Sopenharmony_ci#define PCS_UC_STATUS_WIDTH	8
408c2ecf20Sopenharmony_ci#define PCS_UC_STATUS_FW_SAVE	0x20
418c2ecf20Sopenharmony_ci#define PMA_PMD_MODE_REG	0xc301
428c2ecf20Sopenharmony_ci#define PMA_PMD_RXIN_SEL_LBN	6
438c2ecf20Sopenharmony_ci#define PMA_PMD_FTX_CTRL2_REG	0xc309
448c2ecf20Sopenharmony_ci#define PMA_PMD_FTX_STATIC_LBN	13
458c2ecf20Sopenharmony_ci#define PMA_PMD_VEND1_REG	0xc001
468c2ecf20Sopenharmony_ci#define PMA_PMD_VEND1_LBTXD_LBN	15
478c2ecf20Sopenharmony_ci#define PCS_VEND1_REG		0xc000
488c2ecf20Sopenharmony_ci#define PCS_VEND1_LBTXD_LBN	5
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_civoid falcon_qt202x_set_led(struct ef4_nic *p, int led, int mode)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	int addr = MDIO_QUAKE_LED0_REG + led;
538c2ecf20Sopenharmony_ci	ef4_mdio_write(p, MDIO_MMD_PMAPMD, addr, mode);
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistruct qt202x_phy_data {
578c2ecf20Sopenharmony_ci	enum ef4_phy_mode phy_mode;
588c2ecf20Sopenharmony_ci	bool bug17190_in_bad_state;
598c2ecf20Sopenharmony_ci	unsigned long bug17190_timer;
608c2ecf20Sopenharmony_ci	u32 firmware_ver;
618c2ecf20Sopenharmony_ci};
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#define QT2022C2_MAX_RESET_TIME 500
648c2ecf20Sopenharmony_ci#define QT2022C2_RESET_WAIT 10
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#define QT2025C_MAX_HEARTB_TIME (5 * HZ)
678c2ecf20Sopenharmony_ci#define QT2025C_HEARTB_WAIT 100
688c2ecf20Sopenharmony_ci#define QT2025C_MAX_FWSTART_TIME (25 * HZ / 10)
698c2ecf20Sopenharmony_ci#define QT2025C_FWSTART_WAIT 100
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci#define BUG17190_INTERVAL (2 * HZ)
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic int qt2025c_wait_heartbeat(struct ef4_nic *efx)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	unsigned long timeout = jiffies + QT2025C_MAX_HEARTB_TIME;
768c2ecf20Sopenharmony_ci	int reg, old_counter = 0;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	/* Wait for firmware heartbeat to start */
798c2ecf20Sopenharmony_ci	for (;;) {
808c2ecf20Sopenharmony_ci		int counter;
818c2ecf20Sopenharmony_ci		reg = ef4_mdio_read(efx, MDIO_MMD_PCS, PCS_FW_HEARTBEAT_REG);
828c2ecf20Sopenharmony_ci		if (reg < 0)
838c2ecf20Sopenharmony_ci			return reg;
848c2ecf20Sopenharmony_ci		counter = ((reg >> PCS_FW_HEARTB_LBN) &
858c2ecf20Sopenharmony_ci			    ((1 << PCS_FW_HEARTB_WIDTH) - 1));
868c2ecf20Sopenharmony_ci		if (old_counter == 0)
878c2ecf20Sopenharmony_ci			old_counter = counter;
888c2ecf20Sopenharmony_ci		else if (counter != old_counter)
898c2ecf20Sopenharmony_ci			break;
908c2ecf20Sopenharmony_ci		if (time_after(jiffies, timeout)) {
918c2ecf20Sopenharmony_ci			/* Some cables have EEPROMs that conflict with the
928c2ecf20Sopenharmony_ci			 * PHY's on-board EEPROM so it cannot load firmware */
938c2ecf20Sopenharmony_ci			netif_err(efx, hw, efx->net_dev,
948c2ecf20Sopenharmony_ci				  "If an SFP+ direct attach cable is"
958c2ecf20Sopenharmony_ci				  " connected, please check that it complies"
968c2ecf20Sopenharmony_ci				  " with the SFP+ specification\n");
978c2ecf20Sopenharmony_ci			return -ETIMEDOUT;
988c2ecf20Sopenharmony_ci		}
998c2ecf20Sopenharmony_ci		msleep(QT2025C_HEARTB_WAIT);
1008c2ecf20Sopenharmony_ci	}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	return 0;
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cistatic int qt2025c_wait_fw_status_good(struct ef4_nic *efx)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	unsigned long timeout = jiffies + QT2025C_MAX_FWSTART_TIME;
1088c2ecf20Sopenharmony_ci	int reg;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	/* Wait for firmware status to look good */
1118c2ecf20Sopenharmony_ci	for (;;) {
1128c2ecf20Sopenharmony_ci		reg = ef4_mdio_read(efx, MDIO_MMD_PCS, PCS_UC8051_STATUS_REG);
1138c2ecf20Sopenharmony_ci		if (reg < 0)
1148c2ecf20Sopenharmony_ci			return reg;
1158c2ecf20Sopenharmony_ci		if ((reg &
1168c2ecf20Sopenharmony_ci		     ((1 << PCS_UC_STATUS_WIDTH) - 1) << PCS_UC_STATUS_LBN) >=
1178c2ecf20Sopenharmony_ci		    PCS_UC_STATUS_FW_SAVE)
1188c2ecf20Sopenharmony_ci			break;
1198c2ecf20Sopenharmony_ci		if (time_after(jiffies, timeout))
1208c2ecf20Sopenharmony_ci			return -ETIMEDOUT;
1218c2ecf20Sopenharmony_ci		msleep(QT2025C_FWSTART_WAIT);
1228c2ecf20Sopenharmony_ci	}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	return 0;
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cistatic void qt2025c_restart_firmware(struct ef4_nic *efx)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	/* Restart microcontroller execution of firmware from RAM */
1308c2ecf20Sopenharmony_ci	ef4_mdio_write(efx, 3, 0xe854, 0x00c0);
1318c2ecf20Sopenharmony_ci	ef4_mdio_write(efx, 3, 0xe854, 0x0040);
1328c2ecf20Sopenharmony_ci	msleep(50);
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic int qt2025c_wait_reset(struct ef4_nic *efx)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	int rc;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	rc = qt2025c_wait_heartbeat(efx);
1408c2ecf20Sopenharmony_ci	if (rc != 0)
1418c2ecf20Sopenharmony_ci		return rc;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	rc = qt2025c_wait_fw_status_good(efx);
1448c2ecf20Sopenharmony_ci	if (rc == -ETIMEDOUT) {
1458c2ecf20Sopenharmony_ci		/* Bug 17689: occasionally heartbeat starts but firmware status
1468c2ecf20Sopenharmony_ci		 * code never progresses beyond 0x00.  Try again, once, after
1478c2ecf20Sopenharmony_ci		 * restarting execution of the firmware image. */
1488c2ecf20Sopenharmony_ci		netif_dbg(efx, hw, efx->net_dev,
1498c2ecf20Sopenharmony_ci			  "bashing QT2025C microcontroller\n");
1508c2ecf20Sopenharmony_ci		qt2025c_restart_firmware(efx);
1518c2ecf20Sopenharmony_ci		rc = qt2025c_wait_heartbeat(efx);
1528c2ecf20Sopenharmony_ci		if (rc != 0)
1538c2ecf20Sopenharmony_ci			return rc;
1548c2ecf20Sopenharmony_ci		rc = qt2025c_wait_fw_status_good(efx);
1558c2ecf20Sopenharmony_ci	}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	return rc;
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic void qt2025c_firmware_id(struct ef4_nic *efx)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	struct qt202x_phy_data *phy_data = efx->phy_data;
1638c2ecf20Sopenharmony_ci	u8 firmware_id[9];
1648c2ecf20Sopenharmony_ci	size_t i;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	for (i = 0; i < sizeof(firmware_id); i++)
1678c2ecf20Sopenharmony_ci		firmware_id[i] = ef4_mdio_read(efx, MDIO_MMD_PCS,
1688c2ecf20Sopenharmony_ci					       PCS_FW_PRODUCT_CODE_1 + i);
1698c2ecf20Sopenharmony_ci	netif_info(efx, probe, efx->net_dev,
1708c2ecf20Sopenharmony_ci		   "QT2025C firmware %xr%d v%d.%d.%d.%d [20%02d-%02d-%02d]\n",
1718c2ecf20Sopenharmony_ci		   (firmware_id[0] << 8) | firmware_id[1], firmware_id[2],
1728c2ecf20Sopenharmony_ci		   firmware_id[3] >> 4, firmware_id[3] & 0xf,
1738c2ecf20Sopenharmony_ci		   firmware_id[4], firmware_id[5],
1748c2ecf20Sopenharmony_ci		   firmware_id[6], firmware_id[7], firmware_id[8]);
1758c2ecf20Sopenharmony_ci	phy_data->firmware_ver = ((firmware_id[3] & 0xf0) << 20) |
1768c2ecf20Sopenharmony_ci				 ((firmware_id[3] & 0x0f) << 16) |
1778c2ecf20Sopenharmony_ci				 (firmware_id[4] << 8) | firmware_id[5];
1788c2ecf20Sopenharmony_ci}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_cistatic void qt2025c_bug17190_workaround(struct ef4_nic *efx)
1818c2ecf20Sopenharmony_ci{
1828c2ecf20Sopenharmony_ci	struct qt202x_phy_data *phy_data = efx->phy_data;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	/* The PHY can get stuck in a state where it reports PHY_XS and PMA/PMD
1858c2ecf20Sopenharmony_ci	 * layers up, but PCS down (no block_lock).  If we notice this state
1868c2ecf20Sopenharmony_ci	 * persisting for a couple of seconds, we switch PMA/PMD loopback
1878c2ecf20Sopenharmony_ci	 * briefly on and then off again, which is normally sufficient to
1888c2ecf20Sopenharmony_ci	 * recover it.
1898c2ecf20Sopenharmony_ci	 */
1908c2ecf20Sopenharmony_ci	if (efx->link_state.up ||
1918c2ecf20Sopenharmony_ci	    !ef4_mdio_links_ok(efx, MDIO_DEVS_PMAPMD | MDIO_DEVS_PHYXS)) {
1928c2ecf20Sopenharmony_ci		phy_data->bug17190_in_bad_state = false;
1938c2ecf20Sopenharmony_ci		return;
1948c2ecf20Sopenharmony_ci	}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	if (!phy_data->bug17190_in_bad_state) {
1978c2ecf20Sopenharmony_ci		phy_data->bug17190_in_bad_state = true;
1988c2ecf20Sopenharmony_ci		phy_data->bug17190_timer = jiffies + BUG17190_INTERVAL;
1998c2ecf20Sopenharmony_ci		return;
2008c2ecf20Sopenharmony_ci	}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	if (time_after_eq(jiffies, phy_data->bug17190_timer)) {
2038c2ecf20Sopenharmony_ci		netif_dbg(efx, hw, efx->net_dev, "bashing QT2025C PMA/PMD\n");
2048c2ecf20Sopenharmony_ci		ef4_mdio_set_flag(efx, MDIO_MMD_PMAPMD, MDIO_CTRL1,
2058c2ecf20Sopenharmony_ci				  MDIO_PMA_CTRL1_LOOPBACK, true);
2068c2ecf20Sopenharmony_ci		msleep(100);
2078c2ecf20Sopenharmony_ci		ef4_mdio_set_flag(efx, MDIO_MMD_PMAPMD, MDIO_CTRL1,
2088c2ecf20Sopenharmony_ci				  MDIO_PMA_CTRL1_LOOPBACK, false);
2098c2ecf20Sopenharmony_ci		phy_data->bug17190_timer = jiffies + BUG17190_INTERVAL;
2108c2ecf20Sopenharmony_ci	}
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic int qt2025c_select_phy_mode(struct ef4_nic *efx)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	struct qt202x_phy_data *phy_data = efx->phy_data;
2168c2ecf20Sopenharmony_ci	struct falcon_board *board = falcon_board(efx);
2178c2ecf20Sopenharmony_ci	int reg, rc, i;
2188c2ecf20Sopenharmony_ci	uint16_t phy_op_mode;
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	/* Only 2.0.1.0+ PHY firmware supports the more optimal SFP+
2218c2ecf20Sopenharmony_ci	 * Self-Configure mode.  Don't attempt any switching if we encounter
2228c2ecf20Sopenharmony_ci	 * older firmware. */
2238c2ecf20Sopenharmony_ci	if (phy_data->firmware_ver < 0x02000100)
2248c2ecf20Sopenharmony_ci		return 0;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	/* In general we will get optimal behaviour in "SFP+ Self-Configure"
2278c2ecf20Sopenharmony_ci	 * mode; however, that powers down most of the PHY when no module is
2288c2ecf20Sopenharmony_ci	 * present, so we must use a different mode (any fixed mode will do)
2298c2ecf20Sopenharmony_ci	 * to be sure that loopbacks will work. */
2308c2ecf20Sopenharmony_ci	phy_op_mode = (efx->loopback_mode == LOOPBACK_NONE) ? 0x0038 : 0x0020;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	/* Only change mode if really necessary */
2338c2ecf20Sopenharmony_ci	reg = ef4_mdio_read(efx, 1, 0xc319);
2348c2ecf20Sopenharmony_ci	if ((reg & 0x0038) == phy_op_mode)
2358c2ecf20Sopenharmony_ci		return 0;
2368c2ecf20Sopenharmony_ci	netif_dbg(efx, hw, efx->net_dev, "Switching PHY to mode 0x%04x\n",
2378c2ecf20Sopenharmony_ci		  phy_op_mode);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	/* This sequence replicates the register writes configured in the boot
2408c2ecf20Sopenharmony_ci	 * EEPROM (including the differences between board revisions), except
2418c2ecf20Sopenharmony_ci	 * that the operating mode is changed, and the PHY is prevented from
2428c2ecf20Sopenharmony_ci	 * unnecessarily reloading the main firmware image again. */
2438c2ecf20Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xc300, 0x0000);
2448c2ecf20Sopenharmony_ci	/* (Note: this portion of the boot EEPROM sequence, which bit-bashes 9
2458c2ecf20Sopenharmony_ci	 * STOPs onto the firmware/module I2C bus to reset it, varies across
2468c2ecf20Sopenharmony_ci	 * board revisions, as the bus is connected to different GPIO/LED
2478c2ecf20Sopenharmony_ci	 * outputs on the PHY.) */
2488c2ecf20Sopenharmony_ci	if (board->major == 0 && board->minor < 2) {
2498c2ecf20Sopenharmony_ci		ef4_mdio_write(efx, 1, 0xc303, 0x4498);
2508c2ecf20Sopenharmony_ci		for (i = 0; i < 9; i++) {
2518c2ecf20Sopenharmony_ci			ef4_mdio_write(efx, 1, 0xc303, 0x4488);
2528c2ecf20Sopenharmony_ci			ef4_mdio_write(efx, 1, 0xc303, 0x4480);
2538c2ecf20Sopenharmony_ci			ef4_mdio_write(efx, 1, 0xc303, 0x4490);
2548c2ecf20Sopenharmony_ci			ef4_mdio_write(efx, 1, 0xc303, 0x4498);
2558c2ecf20Sopenharmony_ci		}
2568c2ecf20Sopenharmony_ci	} else {
2578c2ecf20Sopenharmony_ci		ef4_mdio_write(efx, 1, 0xc303, 0x0920);
2588c2ecf20Sopenharmony_ci		ef4_mdio_write(efx, 1, 0xd008, 0x0004);
2598c2ecf20Sopenharmony_ci		for (i = 0; i < 9; i++) {
2608c2ecf20Sopenharmony_ci			ef4_mdio_write(efx, 1, 0xc303, 0x0900);
2618c2ecf20Sopenharmony_ci			ef4_mdio_write(efx, 1, 0xd008, 0x0005);
2628c2ecf20Sopenharmony_ci			ef4_mdio_write(efx, 1, 0xc303, 0x0920);
2638c2ecf20Sopenharmony_ci			ef4_mdio_write(efx, 1, 0xd008, 0x0004);
2648c2ecf20Sopenharmony_ci		}
2658c2ecf20Sopenharmony_ci		ef4_mdio_write(efx, 1, 0xc303, 0x4900);
2668c2ecf20Sopenharmony_ci	}
2678c2ecf20Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xc303, 0x4900);
2688c2ecf20Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xc302, 0x0004);
2698c2ecf20Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xc316, 0x0013);
2708c2ecf20Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xc318, 0x0054);
2718c2ecf20Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xc319, phy_op_mode);
2728c2ecf20Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xc31a, 0x0098);
2738c2ecf20Sopenharmony_ci	ef4_mdio_write(efx, 3, 0x0026, 0x0e00);
2748c2ecf20Sopenharmony_ci	ef4_mdio_write(efx, 3, 0x0027, 0x0013);
2758c2ecf20Sopenharmony_ci	ef4_mdio_write(efx, 3, 0x0028, 0xa528);
2768c2ecf20Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xd006, 0x000a);
2778c2ecf20Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xd007, 0x0009);
2788c2ecf20Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xd008, 0x0004);
2798c2ecf20Sopenharmony_ci	/* This additional write is not present in the boot EEPROM.  It
2808c2ecf20Sopenharmony_ci	 * prevents the PHY's internal boot ROM doing another pointless (and
2818c2ecf20Sopenharmony_ci	 * slow) reload of the firmware image (the microcontroller's code
2828c2ecf20Sopenharmony_ci	 * memory is not affected by the microcontroller reset). */
2838c2ecf20Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xc317, 0x00ff);
2848c2ecf20Sopenharmony_ci	/* PMA/PMD loopback sets RXIN to inverse polarity and the firmware
2858c2ecf20Sopenharmony_ci	 * restart doesn't reset it. We need to do that ourselves. */
2868c2ecf20Sopenharmony_ci	ef4_mdio_set_flag(efx, 1, PMA_PMD_MODE_REG,
2878c2ecf20Sopenharmony_ci			  1 << PMA_PMD_RXIN_SEL_LBN, false);
2888c2ecf20Sopenharmony_ci	ef4_mdio_write(efx, 1, 0xc300, 0x0002);
2898c2ecf20Sopenharmony_ci	msleep(20);
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	/* Restart microcontroller execution of firmware from RAM */
2928c2ecf20Sopenharmony_ci	qt2025c_restart_firmware(efx);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	/* Wait for the microcontroller to be ready again */
2958c2ecf20Sopenharmony_ci	rc = qt2025c_wait_reset(efx);
2968c2ecf20Sopenharmony_ci	if (rc < 0) {
2978c2ecf20Sopenharmony_ci		netif_err(efx, hw, efx->net_dev,
2988c2ecf20Sopenharmony_ci			  "PHY microcontroller reset during mode switch "
2998c2ecf20Sopenharmony_ci			  "timed out\n");
3008c2ecf20Sopenharmony_ci		return rc;
3018c2ecf20Sopenharmony_ci	}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	return 0;
3048c2ecf20Sopenharmony_ci}
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_cistatic int qt202x_reset_phy(struct ef4_nic *efx)
3078c2ecf20Sopenharmony_ci{
3088c2ecf20Sopenharmony_ci	int rc;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	if (efx->phy_type == PHY_TYPE_QT2025C) {
3118c2ecf20Sopenharmony_ci		/* Wait for the reset triggered by falcon_reset_hw()
3128c2ecf20Sopenharmony_ci		 * to complete */
3138c2ecf20Sopenharmony_ci		rc = qt2025c_wait_reset(efx);
3148c2ecf20Sopenharmony_ci		if (rc < 0)
3158c2ecf20Sopenharmony_ci			goto fail;
3168c2ecf20Sopenharmony_ci	} else {
3178c2ecf20Sopenharmony_ci		/* Reset the PHYXS MMD. This is documented as doing
3188c2ecf20Sopenharmony_ci		 * a complete soft reset. */
3198c2ecf20Sopenharmony_ci		rc = ef4_mdio_reset_mmd(efx, MDIO_MMD_PHYXS,
3208c2ecf20Sopenharmony_ci					QT2022C2_MAX_RESET_TIME /
3218c2ecf20Sopenharmony_ci					QT2022C2_RESET_WAIT,
3228c2ecf20Sopenharmony_ci					QT2022C2_RESET_WAIT);
3238c2ecf20Sopenharmony_ci		if (rc < 0)
3248c2ecf20Sopenharmony_ci			goto fail;
3258c2ecf20Sopenharmony_ci	}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	/* Wait 250ms for the PHY to complete bootup */
3288c2ecf20Sopenharmony_ci	msleep(250);
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	falcon_board(efx)->type->init_phy(efx);
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	return 0;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci fail:
3358c2ecf20Sopenharmony_ci	netif_err(efx, hw, efx->net_dev, "PHY reset timed out\n");
3368c2ecf20Sopenharmony_ci	return rc;
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_cistatic int qt202x_phy_probe(struct ef4_nic *efx)
3408c2ecf20Sopenharmony_ci{
3418c2ecf20Sopenharmony_ci	struct qt202x_phy_data *phy_data;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	phy_data = kzalloc(sizeof(struct qt202x_phy_data), GFP_KERNEL);
3448c2ecf20Sopenharmony_ci	if (!phy_data)
3458c2ecf20Sopenharmony_ci		return -ENOMEM;
3468c2ecf20Sopenharmony_ci	efx->phy_data = phy_data;
3478c2ecf20Sopenharmony_ci	phy_data->phy_mode = efx->phy_mode;
3488c2ecf20Sopenharmony_ci	phy_data->bug17190_in_bad_state = false;
3498c2ecf20Sopenharmony_ci	phy_data->bug17190_timer = 0;
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	efx->mdio.mmds = QT202X_REQUIRED_DEVS;
3528c2ecf20Sopenharmony_ci	efx->mdio.mode_support = MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
3538c2ecf20Sopenharmony_ci	efx->loopback_modes = QT202X_LOOPBACKS | FALCON_XMAC_LOOPBACKS;
3548c2ecf20Sopenharmony_ci	return 0;
3558c2ecf20Sopenharmony_ci}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_cistatic int qt202x_phy_init(struct ef4_nic *efx)
3588c2ecf20Sopenharmony_ci{
3598c2ecf20Sopenharmony_ci	u32 devid;
3608c2ecf20Sopenharmony_ci	int rc;
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	rc = qt202x_reset_phy(efx);
3638c2ecf20Sopenharmony_ci	if (rc) {
3648c2ecf20Sopenharmony_ci		netif_err(efx, probe, efx->net_dev, "PHY init failed\n");
3658c2ecf20Sopenharmony_ci		return rc;
3668c2ecf20Sopenharmony_ci	}
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	devid = ef4_mdio_read_id(efx, MDIO_MMD_PHYXS);
3698c2ecf20Sopenharmony_ci	netif_info(efx, probe, efx->net_dev,
3708c2ecf20Sopenharmony_ci		   "PHY ID reg %x (OUI %06x model %02x revision %x)\n",
3718c2ecf20Sopenharmony_ci		   devid, ef4_mdio_id_oui(devid), ef4_mdio_id_model(devid),
3728c2ecf20Sopenharmony_ci		   ef4_mdio_id_rev(devid));
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	if (efx->phy_type == PHY_TYPE_QT2025C)
3758c2ecf20Sopenharmony_ci		qt2025c_firmware_id(efx);
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	return 0;
3788c2ecf20Sopenharmony_ci}
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_cistatic int qt202x_link_ok(struct ef4_nic *efx)
3818c2ecf20Sopenharmony_ci{
3828c2ecf20Sopenharmony_ci	return ef4_mdio_links_ok(efx, QT202X_REQUIRED_DEVS);
3838c2ecf20Sopenharmony_ci}
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_cistatic bool qt202x_phy_poll(struct ef4_nic *efx)
3868c2ecf20Sopenharmony_ci{
3878c2ecf20Sopenharmony_ci	bool was_up = efx->link_state.up;
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	efx->link_state.up = qt202x_link_ok(efx);
3908c2ecf20Sopenharmony_ci	efx->link_state.speed = 10000;
3918c2ecf20Sopenharmony_ci	efx->link_state.fd = true;
3928c2ecf20Sopenharmony_ci	efx->link_state.fc = efx->wanted_fc;
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	if (efx->phy_type == PHY_TYPE_QT2025C)
3958c2ecf20Sopenharmony_ci		qt2025c_bug17190_workaround(efx);
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	return efx->link_state.up != was_up;
3988c2ecf20Sopenharmony_ci}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_cistatic int qt202x_phy_reconfigure(struct ef4_nic *efx)
4018c2ecf20Sopenharmony_ci{
4028c2ecf20Sopenharmony_ci	struct qt202x_phy_data *phy_data = efx->phy_data;
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	if (efx->phy_type == PHY_TYPE_QT2025C) {
4058c2ecf20Sopenharmony_ci		int rc = qt2025c_select_phy_mode(efx);
4068c2ecf20Sopenharmony_ci		if (rc)
4078c2ecf20Sopenharmony_ci			return rc;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci		/* There are several different register bits which can
4108c2ecf20Sopenharmony_ci		 * disable TX (and save power) on direct-attach cables
4118c2ecf20Sopenharmony_ci		 * or optical transceivers, varying somewhat between
4128c2ecf20Sopenharmony_ci		 * firmware versions.  Only 'static mode' appears to
4138c2ecf20Sopenharmony_ci		 * cover everything. */
4148c2ecf20Sopenharmony_ci		mdio_set_flag(
4158c2ecf20Sopenharmony_ci			&efx->mdio, efx->mdio.prtad, MDIO_MMD_PMAPMD,
4168c2ecf20Sopenharmony_ci			PMA_PMD_FTX_CTRL2_REG, 1 << PMA_PMD_FTX_STATIC_LBN,
4178c2ecf20Sopenharmony_ci			efx->phy_mode & PHY_MODE_TX_DISABLED ||
4188c2ecf20Sopenharmony_ci			efx->phy_mode & PHY_MODE_LOW_POWER ||
4198c2ecf20Sopenharmony_ci			efx->loopback_mode == LOOPBACK_PCS ||
4208c2ecf20Sopenharmony_ci			efx->loopback_mode == LOOPBACK_PMAPMD);
4218c2ecf20Sopenharmony_ci	} else {
4228c2ecf20Sopenharmony_ci		/* Reset the PHY when moving from tx off to tx on */
4238c2ecf20Sopenharmony_ci		if (!(efx->phy_mode & PHY_MODE_TX_DISABLED) &&
4248c2ecf20Sopenharmony_ci		    (phy_data->phy_mode & PHY_MODE_TX_DISABLED))
4258c2ecf20Sopenharmony_ci			qt202x_reset_phy(efx);
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci		ef4_mdio_transmit_disable(efx);
4288c2ecf20Sopenharmony_ci	}
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	ef4_mdio_phy_reconfigure(efx);
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	phy_data->phy_mode = efx->phy_mode;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	return 0;
4358c2ecf20Sopenharmony_ci}
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_cistatic void qt202x_phy_get_link_ksettings(struct ef4_nic *efx,
4388c2ecf20Sopenharmony_ci					  struct ethtool_link_ksettings *cmd)
4398c2ecf20Sopenharmony_ci{
4408c2ecf20Sopenharmony_ci	mdio45_ethtool_ksettings_get(&efx->mdio, cmd);
4418c2ecf20Sopenharmony_ci}
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_cistatic void qt202x_phy_remove(struct ef4_nic *efx)
4448c2ecf20Sopenharmony_ci{
4458c2ecf20Sopenharmony_ci	/* Free the context block */
4468c2ecf20Sopenharmony_ci	kfree(efx->phy_data);
4478c2ecf20Sopenharmony_ci	efx->phy_data = NULL;
4488c2ecf20Sopenharmony_ci}
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_cistatic int qt202x_phy_get_module_info(struct ef4_nic *efx,
4518c2ecf20Sopenharmony_ci				      struct ethtool_modinfo *modinfo)
4528c2ecf20Sopenharmony_ci{
4538c2ecf20Sopenharmony_ci	modinfo->type = ETH_MODULE_SFF_8079;
4548c2ecf20Sopenharmony_ci	modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
4558c2ecf20Sopenharmony_ci	return 0;
4568c2ecf20Sopenharmony_ci}
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_cistatic int qt202x_phy_get_module_eeprom(struct ef4_nic *efx,
4598c2ecf20Sopenharmony_ci					struct ethtool_eeprom *ee, u8 *data)
4608c2ecf20Sopenharmony_ci{
4618c2ecf20Sopenharmony_ci	int mmd, reg_base, rc, i;
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	if (efx->phy_type == PHY_TYPE_QT2025C) {
4648c2ecf20Sopenharmony_ci		mmd = MDIO_MMD_PCS;
4658c2ecf20Sopenharmony_ci		reg_base = 0xd000;
4668c2ecf20Sopenharmony_ci	} else {
4678c2ecf20Sopenharmony_ci		mmd = MDIO_MMD_PMAPMD;
4688c2ecf20Sopenharmony_ci		reg_base = 0x8007;
4698c2ecf20Sopenharmony_ci	}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	for (i = 0; i < ee->len; i++) {
4728c2ecf20Sopenharmony_ci		rc = ef4_mdio_read(efx, mmd, reg_base + ee->offset + i);
4738c2ecf20Sopenharmony_ci		if (rc < 0)
4748c2ecf20Sopenharmony_ci			return rc;
4758c2ecf20Sopenharmony_ci		data[i] = rc;
4768c2ecf20Sopenharmony_ci	}
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	return 0;
4798c2ecf20Sopenharmony_ci}
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ciconst struct ef4_phy_operations falcon_qt202x_phy_ops = {
4828c2ecf20Sopenharmony_ci	.probe		 = qt202x_phy_probe,
4838c2ecf20Sopenharmony_ci	.init		 = qt202x_phy_init,
4848c2ecf20Sopenharmony_ci	.reconfigure	 = qt202x_phy_reconfigure,
4858c2ecf20Sopenharmony_ci	.poll		 = qt202x_phy_poll,
4868c2ecf20Sopenharmony_ci	.fini		 = ef4_port_dummy_op_void,
4878c2ecf20Sopenharmony_ci	.remove		 = qt202x_phy_remove,
4888c2ecf20Sopenharmony_ci	.get_link_ksettings = qt202x_phy_get_link_ksettings,
4898c2ecf20Sopenharmony_ci	.set_link_ksettings = ef4_mdio_set_link_ksettings,
4908c2ecf20Sopenharmony_ci	.test_alive	 = ef4_mdio_test_alive,
4918c2ecf20Sopenharmony_ci	.get_module_eeprom = qt202x_phy_get_module_eeprom,
4928c2ecf20Sopenharmony_ci	.get_module_info = qt202x_phy_get_module_info,
4938c2ecf20Sopenharmony_ci};
494