xref: /openbmc/linux/drivers/net/ethernet/sfc/falcon/qt202x_phy.c (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25a6681e2SEdward Cree /****************************************************************************
35a6681e2SEdward Cree  * Driver for Solarflare network controllers and boards
45a6681e2SEdward Cree  * Copyright 2006-2012 Solarflare Communications Inc.
55a6681e2SEdward Cree  */
65a6681e2SEdward Cree /*
75a6681e2SEdward Cree  * Driver for AMCC QT202x SFP+ and XFP adapters; see www.amcc.com for details
85a6681e2SEdward Cree  */
95a6681e2SEdward Cree 
105a6681e2SEdward Cree #include <linux/slab.h>
115a6681e2SEdward Cree #include <linux/timer.h>
125a6681e2SEdward Cree #include <linux/delay.h>
135a6681e2SEdward Cree #include "efx.h"
145a6681e2SEdward Cree #include "mdio_10g.h"
155a6681e2SEdward Cree #include "phy.h"
165a6681e2SEdward Cree #include "nic.h"
175a6681e2SEdward Cree 
185a6681e2SEdward Cree #define QT202X_REQUIRED_DEVS (MDIO_DEVS_PCS |		\
195a6681e2SEdward Cree 			      MDIO_DEVS_PMAPMD |	\
205a6681e2SEdward Cree 			      MDIO_DEVS_PHYXS)
215a6681e2SEdward Cree 
225a6681e2SEdward Cree #define QT202X_LOOPBACKS ((1 << LOOPBACK_PCS) |		\
235a6681e2SEdward Cree 			  (1 << LOOPBACK_PMAPMD) |	\
245a6681e2SEdward Cree 			  (1 << LOOPBACK_PHYXS_WS))
255a6681e2SEdward Cree 
265a6681e2SEdward Cree /****************************************************************************/
275a6681e2SEdward Cree /* Quake-specific MDIO registers */
285a6681e2SEdward Cree #define MDIO_QUAKE_LED0_REG	(0xD006)
295a6681e2SEdward Cree 
305a6681e2SEdward Cree /* QT2025C only */
315a6681e2SEdward Cree #define PCS_FW_HEARTBEAT_REG	0xd7ee
325a6681e2SEdward Cree #define PCS_FW_HEARTB_LBN	0
335a6681e2SEdward Cree #define PCS_FW_HEARTB_WIDTH	8
345a6681e2SEdward Cree #define PCS_FW_PRODUCT_CODE_1	0xd7f0
355a6681e2SEdward Cree #define PCS_FW_VERSION_1	0xd7f3
365a6681e2SEdward Cree #define PCS_FW_BUILD_1		0xd7f6
375a6681e2SEdward Cree #define PCS_UC8051_STATUS_REG	0xd7fd
385a6681e2SEdward Cree #define PCS_UC_STATUS_LBN	0
395a6681e2SEdward Cree #define PCS_UC_STATUS_WIDTH	8
405a6681e2SEdward Cree #define PCS_UC_STATUS_FW_SAVE	0x20
415a6681e2SEdward Cree #define PMA_PMD_MODE_REG	0xc301
425a6681e2SEdward Cree #define PMA_PMD_RXIN_SEL_LBN	6
435a6681e2SEdward Cree #define PMA_PMD_FTX_CTRL2_REG	0xc309
445a6681e2SEdward Cree #define PMA_PMD_FTX_STATIC_LBN	13
455a6681e2SEdward Cree #define PMA_PMD_VEND1_REG	0xc001
465a6681e2SEdward Cree #define PMA_PMD_VEND1_LBTXD_LBN	15
475a6681e2SEdward Cree #define PCS_VEND1_REG		0xc000
485a6681e2SEdward Cree #define PCS_VEND1_LBTXD_LBN	5
495a6681e2SEdward Cree 
falcon_qt202x_set_led(struct ef4_nic * p,int led,int mode)505a6681e2SEdward Cree void falcon_qt202x_set_led(struct ef4_nic *p, int led, int mode)
515a6681e2SEdward Cree {
525a6681e2SEdward Cree 	int addr = MDIO_QUAKE_LED0_REG + led;
535a6681e2SEdward Cree 	ef4_mdio_write(p, MDIO_MMD_PMAPMD, addr, mode);
545a6681e2SEdward Cree }
555a6681e2SEdward Cree 
565a6681e2SEdward Cree struct qt202x_phy_data {
575a6681e2SEdward Cree 	enum ef4_phy_mode phy_mode;
585a6681e2SEdward Cree 	bool bug17190_in_bad_state;
595a6681e2SEdward Cree 	unsigned long bug17190_timer;
605a6681e2SEdward Cree 	u32 firmware_ver;
615a6681e2SEdward Cree };
625a6681e2SEdward Cree 
635a6681e2SEdward Cree #define QT2022C2_MAX_RESET_TIME 500
645a6681e2SEdward Cree #define QT2022C2_RESET_WAIT 10
655a6681e2SEdward Cree 
665a6681e2SEdward Cree #define QT2025C_MAX_HEARTB_TIME (5 * HZ)
675a6681e2SEdward Cree #define QT2025C_HEARTB_WAIT 100
685a6681e2SEdward Cree #define QT2025C_MAX_FWSTART_TIME (25 * HZ / 10)
695a6681e2SEdward Cree #define QT2025C_FWSTART_WAIT 100
705a6681e2SEdward Cree 
715a6681e2SEdward Cree #define BUG17190_INTERVAL (2 * HZ)
725a6681e2SEdward Cree 
qt2025c_wait_heartbeat(struct ef4_nic * efx)735a6681e2SEdward Cree static int qt2025c_wait_heartbeat(struct ef4_nic *efx)
745a6681e2SEdward Cree {
755a6681e2SEdward Cree 	unsigned long timeout = jiffies + QT2025C_MAX_HEARTB_TIME;
765a6681e2SEdward Cree 	int reg, old_counter = 0;
775a6681e2SEdward Cree 
785a6681e2SEdward Cree 	/* Wait for firmware heartbeat to start */
795a6681e2SEdward Cree 	for (;;) {
805a6681e2SEdward Cree 		int counter;
815a6681e2SEdward Cree 		reg = ef4_mdio_read(efx, MDIO_MMD_PCS, PCS_FW_HEARTBEAT_REG);
825a6681e2SEdward Cree 		if (reg < 0)
835a6681e2SEdward Cree 			return reg;
845a6681e2SEdward Cree 		counter = ((reg >> PCS_FW_HEARTB_LBN) &
855a6681e2SEdward Cree 			    ((1 << PCS_FW_HEARTB_WIDTH) - 1));
865a6681e2SEdward Cree 		if (old_counter == 0)
875a6681e2SEdward Cree 			old_counter = counter;
885a6681e2SEdward Cree 		else if (counter != old_counter)
895a6681e2SEdward Cree 			break;
905a6681e2SEdward Cree 		if (time_after(jiffies, timeout)) {
915a6681e2SEdward Cree 			/* Some cables have EEPROMs that conflict with the
925a6681e2SEdward Cree 			 * PHY's on-board EEPROM so it cannot load firmware */
935a6681e2SEdward Cree 			netif_err(efx, hw, efx->net_dev,
945a6681e2SEdward Cree 				  "If an SFP+ direct attach cable is"
955a6681e2SEdward Cree 				  " connected, please check that it complies"
965a6681e2SEdward Cree 				  " with the SFP+ specification\n");
975a6681e2SEdward Cree 			return -ETIMEDOUT;
985a6681e2SEdward Cree 		}
995a6681e2SEdward Cree 		msleep(QT2025C_HEARTB_WAIT);
1005a6681e2SEdward Cree 	}
1015a6681e2SEdward Cree 
1025a6681e2SEdward Cree 	return 0;
1035a6681e2SEdward Cree }
1045a6681e2SEdward Cree 
qt2025c_wait_fw_status_good(struct ef4_nic * efx)1055a6681e2SEdward Cree static int qt2025c_wait_fw_status_good(struct ef4_nic *efx)
1065a6681e2SEdward Cree {
1075a6681e2SEdward Cree 	unsigned long timeout = jiffies + QT2025C_MAX_FWSTART_TIME;
1085a6681e2SEdward Cree 	int reg;
1095a6681e2SEdward Cree 
1105a6681e2SEdward Cree 	/* Wait for firmware status to look good */
1115a6681e2SEdward Cree 	for (;;) {
1125a6681e2SEdward Cree 		reg = ef4_mdio_read(efx, MDIO_MMD_PCS, PCS_UC8051_STATUS_REG);
1135a6681e2SEdward Cree 		if (reg < 0)
1145a6681e2SEdward Cree 			return reg;
1155a6681e2SEdward Cree 		if ((reg &
1165a6681e2SEdward Cree 		     ((1 << PCS_UC_STATUS_WIDTH) - 1) << PCS_UC_STATUS_LBN) >=
1175a6681e2SEdward Cree 		    PCS_UC_STATUS_FW_SAVE)
1185a6681e2SEdward Cree 			break;
1195a6681e2SEdward Cree 		if (time_after(jiffies, timeout))
1205a6681e2SEdward Cree 			return -ETIMEDOUT;
1215a6681e2SEdward Cree 		msleep(QT2025C_FWSTART_WAIT);
1225a6681e2SEdward Cree 	}
1235a6681e2SEdward Cree 
1245a6681e2SEdward Cree 	return 0;
1255a6681e2SEdward Cree }
1265a6681e2SEdward Cree 
qt2025c_restart_firmware(struct ef4_nic * efx)1275a6681e2SEdward Cree static void qt2025c_restart_firmware(struct ef4_nic *efx)
1285a6681e2SEdward Cree {
1295a6681e2SEdward Cree 	/* Restart microcontroller execution of firmware from RAM */
1305a6681e2SEdward Cree 	ef4_mdio_write(efx, 3, 0xe854, 0x00c0);
1315a6681e2SEdward Cree 	ef4_mdio_write(efx, 3, 0xe854, 0x0040);
1325a6681e2SEdward Cree 	msleep(50);
1335a6681e2SEdward Cree }
1345a6681e2SEdward Cree 
qt2025c_wait_reset(struct ef4_nic * efx)1355a6681e2SEdward Cree static int qt2025c_wait_reset(struct ef4_nic *efx)
1365a6681e2SEdward Cree {
1375a6681e2SEdward Cree 	int rc;
1385a6681e2SEdward Cree 
1395a6681e2SEdward Cree 	rc = qt2025c_wait_heartbeat(efx);
1405a6681e2SEdward Cree 	if (rc != 0)
1415a6681e2SEdward Cree 		return rc;
1425a6681e2SEdward Cree 
1435a6681e2SEdward Cree 	rc = qt2025c_wait_fw_status_good(efx);
1445a6681e2SEdward Cree 	if (rc == -ETIMEDOUT) {
1455a6681e2SEdward Cree 		/* Bug 17689: occasionally heartbeat starts but firmware status
1465a6681e2SEdward Cree 		 * code never progresses beyond 0x00.  Try again, once, after
1475a6681e2SEdward Cree 		 * restarting execution of the firmware image. */
1485a6681e2SEdward Cree 		netif_dbg(efx, hw, efx->net_dev,
1495a6681e2SEdward Cree 			  "bashing QT2025C microcontroller\n");
1505a6681e2SEdward Cree 		qt2025c_restart_firmware(efx);
1515a6681e2SEdward Cree 		rc = qt2025c_wait_heartbeat(efx);
1525a6681e2SEdward Cree 		if (rc != 0)
1535a6681e2SEdward Cree 			return rc;
1545a6681e2SEdward Cree 		rc = qt2025c_wait_fw_status_good(efx);
1555a6681e2SEdward Cree 	}
1565a6681e2SEdward Cree 
1575a6681e2SEdward Cree 	return rc;
1585a6681e2SEdward Cree }
1595a6681e2SEdward Cree 
qt2025c_firmware_id(struct ef4_nic * efx)1605a6681e2SEdward Cree static void qt2025c_firmware_id(struct ef4_nic *efx)
1615a6681e2SEdward Cree {
1625a6681e2SEdward Cree 	struct qt202x_phy_data *phy_data = efx->phy_data;
1635a6681e2SEdward Cree 	u8 firmware_id[9];
1645a6681e2SEdward Cree 	size_t i;
1655a6681e2SEdward Cree 
1665a6681e2SEdward Cree 	for (i = 0; i < sizeof(firmware_id); i++)
1675a6681e2SEdward Cree 		firmware_id[i] = ef4_mdio_read(efx, MDIO_MMD_PCS,
1685a6681e2SEdward Cree 					       PCS_FW_PRODUCT_CODE_1 + i);
1695a6681e2SEdward Cree 	netif_info(efx, probe, efx->net_dev,
1705a6681e2SEdward Cree 		   "QT2025C firmware %xr%d v%d.%d.%d.%d [20%02d-%02d-%02d]\n",
1715a6681e2SEdward Cree 		   (firmware_id[0] << 8) | firmware_id[1], firmware_id[2],
1725a6681e2SEdward Cree 		   firmware_id[3] >> 4, firmware_id[3] & 0xf,
1735a6681e2SEdward Cree 		   firmware_id[4], firmware_id[5],
1745a6681e2SEdward Cree 		   firmware_id[6], firmware_id[7], firmware_id[8]);
1755a6681e2SEdward Cree 	phy_data->firmware_ver = ((firmware_id[3] & 0xf0) << 20) |
1765a6681e2SEdward Cree 				 ((firmware_id[3] & 0x0f) << 16) |
1775a6681e2SEdward Cree 				 (firmware_id[4] << 8) | firmware_id[5];
1785a6681e2SEdward Cree }
1795a6681e2SEdward Cree 
qt2025c_bug17190_workaround(struct ef4_nic * efx)1805a6681e2SEdward Cree static void qt2025c_bug17190_workaround(struct ef4_nic *efx)
1815a6681e2SEdward Cree {
1825a6681e2SEdward Cree 	struct qt202x_phy_data *phy_data = efx->phy_data;
1835a6681e2SEdward Cree 
1845a6681e2SEdward Cree 	/* The PHY can get stuck in a state where it reports PHY_XS and PMA/PMD
1855a6681e2SEdward Cree 	 * layers up, but PCS down (no block_lock).  If we notice this state
1865a6681e2SEdward Cree 	 * persisting for a couple of seconds, we switch PMA/PMD loopback
1875a6681e2SEdward Cree 	 * briefly on and then off again, which is normally sufficient to
1885a6681e2SEdward Cree 	 * recover it.
1895a6681e2SEdward Cree 	 */
1905a6681e2SEdward Cree 	if (efx->link_state.up ||
1915a6681e2SEdward Cree 	    !ef4_mdio_links_ok(efx, MDIO_DEVS_PMAPMD | MDIO_DEVS_PHYXS)) {
1925a6681e2SEdward Cree 		phy_data->bug17190_in_bad_state = false;
1935a6681e2SEdward Cree 		return;
1945a6681e2SEdward Cree 	}
1955a6681e2SEdward Cree 
1965a6681e2SEdward Cree 	if (!phy_data->bug17190_in_bad_state) {
1975a6681e2SEdward Cree 		phy_data->bug17190_in_bad_state = true;
1985a6681e2SEdward Cree 		phy_data->bug17190_timer = jiffies + BUG17190_INTERVAL;
1995a6681e2SEdward Cree 		return;
2005a6681e2SEdward Cree 	}
2015a6681e2SEdward Cree 
2025a6681e2SEdward Cree 	if (time_after_eq(jiffies, phy_data->bug17190_timer)) {
2035a6681e2SEdward Cree 		netif_dbg(efx, hw, efx->net_dev, "bashing QT2025C PMA/PMD\n");
2045a6681e2SEdward Cree 		ef4_mdio_set_flag(efx, MDIO_MMD_PMAPMD, MDIO_CTRL1,
2055a6681e2SEdward Cree 				  MDIO_PMA_CTRL1_LOOPBACK, true);
2065a6681e2SEdward Cree 		msleep(100);
2075a6681e2SEdward Cree 		ef4_mdio_set_flag(efx, MDIO_MMD_PMAPMD, MDIO_CTRL1,
2085a6681e2SEdward Cree 				  MDIO_PMA_CTRL1_LOOPBACK, false);
2095a6681e2SEdward Cree 		phy_data->bug17190_timer = jiffies + BUG17190_INTERVAL;
2105a6681e2SEdward Cree 	}
2115a6681e2SEdward Cree }
2125a6681e2SEdward Cree 
qt2025c_select_phy_mode(struct ef4_nic * efx)2135a6681e2SEdward Cree static int qt2025c_select_phy_mode(struct ef4_nic *efx)
2145a6681e2SEdward Cree {
2155a6681e2SEdward Cree 	struct qt202x_phy_data *phy_data = efx->phy_data;
2165a6681e2SEdward Cree 	struct falcon_board *board = falcon_board(efx);
2175a6681e2SEdward Cree 	int reg, rc, i;
2185a6681e2SEdward Cree 	uint16_t phy_op_mode;
2195a6681e2SEdward Cree 
2205a6681e2SEdward Cree 	/* Only 2.0.1.0+ PHY firmware supports the more optimal SFP+
2215a6681e2SEdward Cree 	 * Self-Configure mode.  Don't attempt any switching if we encounter
2225a6681e2SEdward Cree 	 * older firmware. */
2235a6681e2SEdward Cree 	if (phy_data->firmware_ver < 0x02000100)
2245a6681e2SEdward Cree 		return 0;
2255a6681e2SEdward Cree 
2265a6681e2SEdward Cree 	/* In general we will get optimal behaviour in "SFP+ Self-Configure"
2275a6681e2SEdward Cree 	 * mode; however, that powers down most of the PHY when no module is
2285a6681e2SEdward Cree 	 * present, so we must use a different mode (any fixed mode will do)
2295a6681e2SEdward Cree 	 * to be sure that loopbacks will work. */
2305a6681e2SEdward Cree 	phy_op_mode = (efx->loopback_mode == LOOPBACK_NONE) ? 0x0038 : 0x0020;
2315a6681e2SEdward Cree 
2325a6681e2SEdward Cree 	/* Only change mode if really necessary */
2335a6681e2SEdward Cree 	reg = ef4_mdio_read(efx, 1, 0xc319);
2345a6681e2SEdward Cree 	if ((reg & 0x0038) == phy_op_mode)
2355a6681e2SEdward Cree 		return 0;
2365a6681e2SEdward Cree 	netif_dbg(efx, hw, efx->net_dev, "Switching PHY to mode 0x%04x\n",
2375a6681e2SEdward Cree 		  phy_op_mode);
2385a6681e2SEdward Cree 
2395a6681e2SEdward Cree 	/* This sequence replicates the register writes configured in the boot
2405a6681e2SEdward Cree 	 * EEPROM (including the differences between board revisions), except
2415a6681e2SEdward Cree 	 * that the operating mode is changed, and the PHY is prevented from
2425a6681e2SEdward Cree 	 * unnecessarily reloading the main firmware image again. */
2435a6681e2SEdward Cree 	ef4_mdio_write(efx, 1, 0xc300, 0x0000);
2445a6681e2SEdward Cree 	/* (Note: this portion of the boot EEPROM sequence, which bit-bashes 9
2455a6681e2SEdward Cree 	 * STOPs onto the firmware/module I2C bus to reset it, varies across
2465a6681e2SEdward Cree 	 * board revisions, as the bus is connected to different GPIO/LED
2475a6681e2SEdward Cree 	 * outputs on the PHY.) */
2485a6681e2SEdward Cree 	if (board->major == 0 && board->minor < 2) {
2495a6681e2SEdward Cree 		ef4_mdio_write(efx, 1, 0xc303, 0x4498);
2505a6681e2SEdward Cree 		for (i = 0; i < 9; i++) {
2515a6681e2SEdward Cree 			ef4_mdio_write(efx, 1, 0xc303, 0x4488);
2525a6681e2SEdward Cree 			ef4_mdio_write(efx, 1, 0xc303, 0x4480);
2535a6681e2SEdward Cree 			ef4_mdio_write(efx, 1, 0xc303, 0x4490);
2545a6681e2SEdward Cree 			ef4_mdio_write(efx, 1, 0xc303, 0x4498);
2555a6681e2SEdward Cree 		}
2565a6681e2SEdward Cree 	} else {
2575a6681e2SEdward Cree 		ef4_mdio_write(efx, 1, 0xc303, 0x0920);
2585a6681e2SEdward Cree 		ef4_mdio_write(efx, 1, 0xd008, 0x0004);
2595a6681e2SEdward Cree 		for (i = 0; i < 9; i++) {
2605a6681e2SEdward Cree 			ef4_mdio_write(efx, 1, 0xc303, 0x0900);
2615a6681e2SEdward Cree 			ef4_mdio_write(efx, 1, 0xd008, 0x0005);
2625a6681e2SEdward Cree 			ef4_mdio_write(efx, 1, 0xc303, 0x0920);
2635a6681e2SEdward Cree 			ef4_mdio_write(efx, 1, 0xd008, 0x0004);
2645a6681e2SEdward Cree 		}
2655a6681e2SEdward Cree 		ef4_mdio_write(efx, 1, 0xc303, 0x4900);
2665a6681e2SEdward Cree 	}
2675a6681e2SEdward Cree 	ef4_mdio_write(efx, 1, 0xc303, 0x4900);
2685a6681e2SEdward Cree 	ef4_mdio_write(efx, 1, 0xc302, 0x0004);
2695a6681e2SEdward Cree 	ef4_mdio_write(efx, 1, 0xc316, 0x0013);
2705a6681e2SEdward Cree 	ef4_mdio_write(efx, 1, 0xc318, 0x0054);
2715a6681e2SEdward Cree 	ef4_mdio_write(efx, 1, 0xc319, phy_op_mode);
2725a6681e2SEdward Cree 	ef4_mdio_write(efx, 1, 0xc31a, 0x0098);
2735a6681e2SEdward Cree 	ef4_mdio_write(efx, 3, 0x0026, 0x0e00);
2745a6681e2SEdward Cree 	ef4_mdio_write(efx, 3, 0x0027, 0x0013);
2755a6681e2SEdward Cree 	ef4_mdio_write(efx, 3, 0x0028, 0xa528);
2765a6681e2SEdward Cree 	ef4_mdio_write(efx, 1, 0xd006, 0x000a);
2775a6681e2SEdward Cree 	ef4_mdio_write(efx, 1, 0xd007, 0x0009);
2785a6681e2SEdward Cree 	ef4_mdio_write(efx, 1, 0xd008, 0x0004);
2795a6681e2SEdward Cree 	/* This additional write is not present in the boot EEPROM.  It
2805a6681e2SEdward Cree 	 * prevents the PHY's internal boot ROM doing another pointless (and
2815a6681e2SEdward Cree 	 * slow) reload of the firmware image (the microcontroller's code
2825a6681e2SEdward Cree 	 * memory is not affected by the microcontroller reset). */
2835a6681e2SEdward Cree 	ef4_mdio_write(efx, 1, 0xc317, 0x00ff);
2845a6681e2SEdward Cree 	/* PMA/PMD loopback sets RXIN to inverse polarity and the firmware
2855a6681e2SEdward Cree 	 * restart doesn't reset it. We need to do that ourselves. */
2865a6681e2SEdward Cree 	ef4_mdio_set_flag(efx, 1, PMA_PMD_MODE_REG,
2875a6681e2SEdward Cree 			  1 << PMA_PMD_RXIN_SEL_LBN, false);
2885a6681e2SEdward Cree 	ef4_mdio_write(efx, 1, 0xc300, 0x0002);
2895a6681e2SEdward Cree 	msleep(20);
2905a6681e2SEdward Cree 
2915a6681e2SEdward Cree 	/* Restart microcontroller execution of firmware from RAM */
2925a6681e2SEdward Cree 	qt2025c_restart_firmware(efx);
2935a6681e2SEdward Cree 
2945a6681e2SEdward Cree 	/* Wait for the microcontroller to be ready again */
2955a6681e2SEdward Cree 	rc = qt2025c_wait_reset(efx);
2965a6681e2SEdward Cree 	if (rc < 0) {
2975a6681e2SEdward Cree 		netif_err(efx, hw, efx->net_dev,
2985a6681e2SEdward Cree 			  "PHY microcontroller reset during mode switch "
2995a6681e2SEdward Cree 			  "timed out\n");
3005a6681e2SEdward Cree 		return rc;
3015a6681e2SEdward Cree 	}
3025a6681e2SEdward Cree 
3035a6681e2SEdward Cree 	return 0;
3045a6681e2SEdward Cree }
3055a6681e2SEdward Cree 
qt202x_reset_phy(struct ef4_nic * efx)3065a6681e2SEdward Cree static int qt202x_reset_phy(struct ef4_nic *efx)
3075a6681e2SEdward Cree {
3085a6681e2SEdward Cree 	int rc;
3095a6681e2SEdward Cree 
3105a6681e2SEdward Cree 	if (efx->phy_type == PHY_TYPE_QT2025C) {
3115a6681e2SEdward Cree 		/* Wait for the reset triggered by falcon_reset_hw()
3125a6681e2SEdward Cree 		 * to complete */
3135a6681e2SEdward Cree 		rc = qt2025c_wait_reset(efx);
3145a6681e2SEdward Cree 		if (rc < 0)
3155a6681e2SEdward Cree 			goto fail;
3165a6681e2SEdward Cree 	} else {
3175a6681e2SEdward Cree 		/* Reset the PHYXS MMD. This is documented as doing
3185a6681e2SEdward Cree 		 * a complete soft reset. */
3195a6681e2SEdward Cree 		rc = ef4_mdio_reset_mmd(efx, MDIO_MMD_PHYXS,
3205a6681e2SEdward Cree 					QT2022C2_MAX_RESET_TIME /
3215a6681e2SEdward Cree 					QT2022C2_RESET_WAIT,
3225a6681e2SEdward Cree 					QT2022C2_RESET_WAIT);
3235a6681e2SEdward Cree 		if (rc < 0)
3245a6681e2SEdward Cree 			goto fail;
3255a6681e2SEdward Cree 	}
3265a6681e2SEdward Cree 
3275a6681e2SEdward Cree 	/* Wait 250ms for the PHY to complete bootup */
3285a6681e2SEdward Cree 	msleep(250);
3295a6681e2SEdward Cree 
3305a6681e2SEdward Cree 	falcon_board(efx)->type->init_phy(efx);
3315a6681e2SEdward Cree 
3325a6681e2SEdward Cree 	return 0;
3335a6681e2SEdward Cree 
3345a6681e2SEdward Cree  fail:
3355a6681e2SEdward Cree 	netif_err(efx, hw, efx->net_dev, "PHY reset timed out\n");
3365a6681e2SEdward Cree 	return rc;
3375a6681e2SEdward Cree }
3385a6681e2SEdward Cree 
qt202x_phy_probe(struct ef4_nic * efx)3395a6681e2SEdward Cree static int qt202x_phy_probe(struct ef4_nic *efx)
3405a6681e2SEdward Cree {
3415a6681e2SEdward Cree 	struct qt202x_phy_data *phy_data;
3425a6681e2SEdward Cree 
3435a6681e2SEdward Cree 	phy_data = kzalloc(sizeof(struct qt202x_phy_data), GFP_KERNEL);
3445a6681e2SEdward Cree 	if (!phy_data)
3455a6681e2SEdward Cree 		return -ENOMEM;
3465a6681e2SEdward Cree 	efx->phy_data = phy_data;
3475a6681e2SEdward Cree 	phy_data->phy_mode = efx->phy_mode;
3485a6681e2SEdward Cree 	phy_data->bug17190_in_bad_state = false;
3495a6681e2SEdward Cree 	phy_data->bug17190_timer = 0;
3505a6681e2SEdward Cree 
3515a6681e2SEdward Cree 	efx->mdio.mmds = QT202X_REQUIRED_DEVS;
3525a6681e2SEdward Cree 	efx->mdio.mode_support = MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
3535a6681e2SEdward Cree 	efx->loopback_modes = QT202X_LOOPBACKS | FALCON_XMAC_LOOPBACKS;
3545a6681e2SEdward Cree 	return 0;
3555a6681e2SEdward Cree }
3565a6681e2SEdward Cree 
qt202x_phy_init(struct ef4_nic * efx)3575a6681e2SEdward Cree static int qt202x_phy_init(struct ef4_nic *efx)
3585a6681e2SEdward Cree {
3595a6681e2SEdward Cree 	u32 devid;
3605a6681e2SEdward Cree 	int rc;
3615a6681e2SEdward Cree 
3625a6681e2SEdward Cree 	rc = qt202x_reset_phy(efx);
3635a6681e2SEdward Cree 	if (rc) {
3645a6681e2SEdward Cree 		netif_err(efx, probe, efx->net_dev, "PHY init failed\n");
3655a6681e2SEdward Cree 		return rc;
3665a6681e2SEdward Cree 	}
3675a6681e2SEdward Cree 
3685a6681e2SEdward Cree 	devid = ef4_mdio_read_id(efx, MDIO_MMD_PHYXS);
3695a6681e2SEdward Cree 	netif_info(efx, probe, efx->net_dev,
3705a6681e2SEdward Cree 		   "PHY ID reg %x (OUI %06x model %02x revision %x)\n",
3715a6681e2SEdward Cree 		   devid, ef4_mdio_id_oui(devid), ef4_mdio_id_model(devid),
3725a6681e2SEdward Cree 		   ef4_mdio_id_rev(devid));
3735a6681e2SEdward Cree 
3745a6681e2SEdward Cree 	if (efx->phy_type == PHY_TYPE_QT2025C)
3755a6681e2SEdward Cree 		qt2025c_firmware_id(efx);
3765a6681e2SEdward Cree 
3775a6681e2SEdward Cree 	return 0;
3785a6681e2SEdward Cree }
3795a6681e2SEdward Cree 
qt202x_link_ok(struct ef4_nic * efx)3805a6681e2SEdward Cree static int qt202x_link_ok(struct ef4_nic *efx)
3815a6681e2SEdward Cree {
3825a6681e2SEdward Cree 	return ef4_mdio_links_ok(efx, QT202X_REQUIRED_DEVS);
3835a6681e2SEdward Cree }
3845a6681e2SEdward Cree 
qt202x_phy_poll(struct ef4_nic * efx)3855a6681e2SEdward Cree static bool qt202x_phy_poll(struct ef4_nic *efx)
3865a6681e2SEdward Cree {
3875a6681e2SEdward Cree 	bool was_up = efx->link_state.up;
3885a6681e2SEdward Cree 
3895a6681e2SEdward Cree 	efx->link_state.up = qt202x_link_ok(efx);
3905a6681e2SEdward Cree 	efx->link_state.speed = 10000;
3915a6681e2SEdward Cree 	efx->link_state.fd = true;
3925a6681e2SEdward Cree 	efx->link_state.fc = efx->wanted_fc;
3935a6681e2SEdward Cree 
3945a6681e2SEdward Cree 	if (efx->phy_type == PHY_TYPE_QT2025C)
3955a6681e2SEdward Cree 		qt2025c_bug17190_workaround(efx);
3965a6681e2SEdward Cree 
3975a6681e2SEdward Cree 	return efx->link_state.up != was_up;
3985a6681e2SEdward Cree }
3995a6681e2SEdward Cree 
qt202x_phy_reconfigure(struct ef4_nic * efx)4005a6681e2SEdward Cree static int qt202x_phy_reconfigure(struct ef4_nic *efx)
4015a6681e2SEdward Cree {
4025a6681e2SEdward Cree 	struct qt202x_phy_data *phy_data = efx->phy_data;
4035a6681e2SEdward Cree 
4045a6681e2SEdward Cree 	if (efx->phy_type == PHY_TYPE_QT2025C) {
4055a6681e2SEdward Cree 		int rc = qt2025c_select_phy_mode(efx);
4065a6681e2SEdward Cree 		if (rc)
4075a6681e2SEdward Cree 			return rc;
4085a6681e2SEdward Cree 
4095a6681e2SEdward Cree 		/* There are several different register bits which can
4105a6681e2SEdward Cree 		 * disable TX (and save power) on direct-attach cables
4115a6681e2SEdward Cree 		 * or optical transceivers, varying somewhat between
4125a6681e2SEdward Cree 		 * firmware versions.  Only 'static mode' appears to
4135a6681e2SEdward Cree 		 * cover everything. */
4145a6681e2SEdward Cree 		mdio_set_flag(
4155a6681e2SEdward Cree 			&efx->mdio, efx->mdio.prtad, MDIO_MMD_PMAPMD,
4165a6681e2SEdward Cree 			PMA_PMD_FTX_CTRL2_REG, 1 << PMA_PMD_FTX_STATIC_LBN,
4175a6681e2SEdward Cree 			efx->phy_mode & PHY_MODE_TX_DISABLED ||
4185a6681e2SEdward Cree 			efx->phy_mode & PHY_MODE_LOW_POWER ||
4195a6681e2SEdward Cree 			efx->loopback_mode == LOOPBACK_PCS ||
4205a6681e2SEdward Cree 			efx->loopback_mode == LOOPBACK_PMAPMD);
4215a6681e2SEdward Cree 	} else {
4225a6681e2SEdward Cree 		/* Reset the PHY when moving from tx off to tx on */
4235a6681e2SEdward Cree 		if (!(efx->phy_mode & PHY_MODE_TX_DISABLED) &&
4245a6681e2SEdward Cree 		    (phy_data->phy_mode & PHY_MODE_TX_DISABLED))
4255a6681e2SEdward Cree 			qt202x_reset_phy(efx);
4265a6681e2SEdward Cree 
4275a6681e2SEdward Cree 		ef4_mdio_transmit_disable(efx);
4285a6681e2SEdward Cree 	}
4295a6681e2SEdward Cree 
4305a6681e2SEdward Cree 	ef4_mdio_phy_reconfigure(efx);
4315a6681e2SEdward Cree 
4325a6681e2SEdward Cree 	phy_data->phy_mode = efx->phy_mode;
4335a6681e2SEdward Cree 
4345a6681e2SEdward Cree 	return 0;
4355a6681e2SEdward Cree }
4365a6681e2SEdward Cree 
qt202x_phy_get_link_ksettings(struct ef4_nic * efx,struct ethtool_link_ksettings * cmd)437e938ed15SPhilippe Reynes static void qt202x_phy_get_link_ksettings(struct ef4_nic *efx,
438e938ed15SPhilippe Reynes 					  struct ethtool_link_ksettings *cmd)
4395a6681e2SEdward Cree {
440e938ed15SPhilippe Reynes 	mdio45_ethtool_ksettings_get(&efx->mdio, cmd);
4415a6681e2SEdward Cree }
4425a6681e2SEdward Cree 
qt202x_phy_remove(struct ef4_nic * efx)4435a6681e2SEdward Cree static void qt202x_phy_remove(struct ef4_nic *efx)
4445a6681e2SEdward Cree {
4455a6681e2SEdward Cree 	/* Free the context block */
4465a6681e2SEdward Cree 	kfree(efx->phy_data);
4475a6681e2SEdward Cree 	efx->phy_data = NULL;
4485a6681e2SEdward Cree }
4495a6681e2SEdward Cree 
qt202x_phy_get_module_info(struct ef4_nic * efx,struct ethtool_modinfo * modinfo)4505a6681e2SEdward Cree static int qt202x_phy_get_module_info(struct ef4_nic *efx,
4515a6681e2SEdward Cree 				      struct ethtool_modinfo *modinfo)
4525a6681e2SEdward Cree {
4535a6681e2SEdward Cree 	modinfo->type = ETH_MODULE_SFF_8079;
4545a6681e2SEdward Cree 	modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
4555a6681e2SEdward Cree 	return 0;
4565a6681e2SEdward Cree }
4575a6681e2SEdward Cree 
qt202x_phy_get_module_eeprom(struct ef4_nic * efx,struct ethtool_eeprom * ee,u8 * data)4585a6681e2SEdward Cree static int qt202x_phy_get_module_eeprom(struct ef4_nic *efx,
4595a6681e2SEdward Cree 					struct ethtool_eeprom *ee, u8 *data)
4605a6681e2SEdward Cree {
4615a6681e2SEdward Cree 	int mmd, reg_base, rc, i;
4625a6681e2SEdward Cree 
4635a6681e2SEdward Cree 	if (efx->phy_type == PHY_TYPE_QT2025C) {
4645a6681e2SEdward Cree 		mmd = MDIO_MMD_PCS;
4655a6681e2SEdward Cree 		reg_base = 0xd000;
4665a6681e2SEdward Cree 	} else {
4675a6681e2SEdward Cree 		mmd = MDIO_MMD_PMAPMD;
4685a6681e2SEdward Cree 		reg_base = 0x8007;
4695a6681e2SEdward Cree 	}
4705a6681e2SEdward Cree 
4715a6681e2SEdward Cree 	for (i = 0; i < ee->len; i++) {
4725a6681e2SEdward Cree 		rc = ef4_mdio_read(efx, mmd, reg_base + ee->offset + i);
4735a6681e2SEdward Cree 		if (rc < 0)
4745a6681e2SEdward Cree 			return rc;
4755a6681e2SEdward Cree 		data[i] = rc;
4765a6681e2SEdward Cree 	}
4775a6681e2SEdward Cree 
4785a6681e2SEdward Cree 	return 0;
4795a6681e2SEdward Cree }
4805a6681e2SEdward Cree 
4815a6681e2SEdward Cree const struct ef4_phy_operations falcon_qt202x_phy_ops = {
4825a6681e2SEdward Cree 	.probe		 = qt202x_phy_probe,
4835a6681e2SEdward Cree 	.init		 = qt202x_phy_init,
4845a6681e2SEdward Cree 	.reconfigure	 = qt202x_phy_reconfigure,
4855a6681e2SEdward Cree 	.poll		 = qt202x_phy_poll,
4865a6681e2SEdward Cree 	.fini		 = ef4_port_dummy_op_void,
4875a6681e2SEdward Cree 	.remove		 = qt202x_phy_remove,
488e938ed15SPhilippe Reynes 	.get_link_ksettings = qt202x_phy_get_link_ksettings,
489e938ed15SPhilippe Reynes 	.set_link_ksettings = ef4_mdio_set_link_ksettings,
4905a6681e2SEdward Cree 	.test_alive	 = ef4_mdio_test_alive,
4915a6681e2SEdward Cree 	.get_module_eeprom = qt202x_phy_get_module_eeprom,
4925a6681e2SEdward Cree 	.get_module_info = qt202x_phy_get_module_info,
4935a6681e2SEdward Cree };
494