18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright(c) 2007 - 2018 Intel Corporation. */
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <linux/if_ether.h>
58c2ecf20Sopenharmony_ci#include <linux/delay.h>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include "e1000_mac.h"
88c2ecf20Sopenharmony_ci#include "e1000_phy.h"
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_cistatic s32  igb_phy_setup_autoneg(struct e1000_hw *hw);
118c2ecf20Sopenharmony_cistatic void igb_phy_force_speed_duplex_setup(struct e1000_hw *hw,
128c2ecf20Sopenharmony_ci					     u16 *phy_ctrl);
138c2ecf20Sopenharmony_cistatic s32  igb_wait_autoneg(struct e1000_hw *hw);
148c2ecf20Sopenharmony_cistatic s32  igb_set_master_slave_mode(struct e1000_hw *hw);
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/* Cable length tables */
178c2ecf20Sopenharmony_cistatic const u16 e1000_m88_cable_length_table[] = {
188c2ecf20Sopenharmony_ci	0, 50, 80, 110, 140, 140, E1000_CABLE_LENGTH_UNDEFINED };
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic const u16 e1000_igp_2_cable_length_table[] = {
218c2ecf20Sopenharmony_ci	0, 0, 0, 0, 0, 0, 0, 0, 3, 5, 8, 11, 13, 16, 18, 21,
228c2ecf20Sopenharmony_ci	0, 0, 0, 3, 6, 10, 13, 16, 19, 23, 26, 29, 32, 35, 38, 41,
238c2ecf20Sopenharmony_ci	6, 10, 14, 18, 22, 26, 30, 33, 37, 41, 44, 48, 51, 54, 58, 61,
248c2ecf20Sopenharmony_ci	21, 26, 31, 35, 40, 44, 49, 53, 57, 61, 65, 68, 72, 75, 79, 82,
258c2ecf20Sopenharmony_ci	40, 45, 51, 56, 61, 66, 70, 75, 79, 83, 87, 91, 94, 98, 101, 104,
268c2ecf20Sopenharmony_ci	60, 66, 72, 77, 82, 87, 92, 96, 100, 104, 108, 111, 114, 117, 119, 121,
278c2ecf20Sopenharmony_ci	83, 89, 95, 100, 105, 109, 113, 116, 119, 122, 124,
288c2ecf20Sopenharmony_ci	104, 109, 114, 118, 121, 124};
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/**
318c2ecf20Sopenharmony_ci *  igb_check_reset_block - Check if PHY reset is blocked
328c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci *  Read the PHY management control register and check whether a PHY reset
358c2ecf20Sopenharmony_ci *  is blocked.  If a reset is not blocked return 0, otherwise
368c2ecf20Sopenharmony_ci *  return E1000_BLK_PHY_RESET (12).
378c2ecf20Sopenharmony_ci **/
388c2ecf20Sopenharmony_cis32 igb_check_reset_block(struct e1000_hw *hw)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	u32 manc;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	manc = rd32(E1000_MANC);
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	return (manc & E1000_MANC_BLK_PHY_RST_ON_IDE) ? E1000_BLK_PHY_RESET : 0;
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/**
488c2ecf20Sopenharmony_ci *  igb_get_phy_id - Retrieve the PHY ID and revision
498c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci *  Reads the PHY registers and stores the PHY ID and possibly the PHY
528c2ecf20Sopenharmony_ci *  revision in the hardware structure.
538c2ecf20Sopenharmony_ci **/
548c2ecf20Sopenharmony_cis32 igb_get_phy_id(struct e1000_hw *hw)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
578c2ecf20Sopenharmony_ci	s32 ret_val = 0;
588c2ecf20Sopenharmony_ci	u16 phy_id;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	/* ensure PHY page selection to fix misconfigured i210 */
618c2ecf20Sopenharmony_ci	if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211))
628c2ecf20Sopenharmony_ci		phy->ops.write_reg(hw, I347AT4_PAGE_SELECT, 0);
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, PHY_ID1, &phy_id);
658c2ecf20Sopenharmony_ci	if (ret_val)
668c2ecf20Sopenharmony_ci		goto out;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	phy->id = (u32)(phy_id << 16);
698c2ecf20Sopenharmony_ci	udelay(20);
708c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, PHY_ID2, &phy_id);
718c2ecf20Sopenharmony_ci	if (ret_val)
728c2ecf20Sopenharmony_ci		goto out;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	phy->id |= (u32)(phy_id & PHY_REVISION_MASK);
758c2ecf20Sopenharmony_ci	phy->revision = (u32)(phy_id & ~PHY_REVISION_MASK);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ciout:
788c2ecf20Sopenharmony_ci	return ret_val;
798c2ecf20Sopenharmony_ci}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci/**
828c2ecf20Sopenharmony_ci *  igb_phy_reset_dsp - Reset PHY DSP
838c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
848c2ecf20Sopenharmony_ci *
858c2ecf20Sopenharmony_ci *  Reset the digital signal processor.
868c2ecf20Sopenharmony_ci **/
878c2ecf20Sopenharmony_cistatic s32 igb_phy_reset_dsp(struct e1000_hw *hw)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	s32 ret_val = 0;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	if (!(hw->phy.ops.write_reg))
928c2ecf20Sopenharmony_ci		goto out;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	ret_val = hw->phy.ops.write_reg(hw, M88E1000_PHY_GEN_CONTROL, 0xC1);
958c2ecf20Sopenharmony_ci	if (ret_val)
968c2ecf20Sopenharmony_ci		goto out;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	ret_val = hw->phy.ops.write_reg(hw, M88E1000_PHY_GEN_CONTROL, 0);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ciout:
1018c2ecf20Sopenharmony_ci	return ret_val;
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci/**
1058c2ecf20Sopenharmony_ci *  igb_read_phy_reg_mdic - Read MDI control register
1068c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
1078c2ecf20Sopenharmony_ci *  @offset: register offset to be read
1088c2ecf20Sopenharmony_ci *  @data: pointer to the read data
1098c2ecf20Sopenharmony_ci *
1108c2ecf20Sopenharmony_ci *  Reads the MDI control register in the PHY at offset and stores the
1118c2ecf20Sopenharmony_ci *  information read to data.
1128c2ecf20Sopenharmony_ci **/
1138c2ecf20Sopenharmony_cis32 igb_read_phy_reg_mdic(struct e1000_hw *hw, u32 offset, u16 *data)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
1168c2ecf20Sopenharmony_ci	u32 i, mdic = 0;
1178c2ecf20Sopenharmony_ci	s32 ret_val = 0;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	if (offset > MAX_PHY_REG_ADDRESS) {
1208c2ecf20Sopenharmony_ci		hw_dbg("PHY Address %d is out of range\n", offset);
1218c2ecf20Sopenharmony_ci		ret_val = -E1000_ERR_PARAM;
1228c2ecf20Sopenharmony_ci		goto out;
1238c2ecf20Sopenharmony_ci	}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	/* Set up Op-code, Phy Address, and register offset in the MDI
1268c2ecf20Sopenharmony_ci	 * Control register.  The MAC will take care of interfacing with the
1278c2ecf20Sopenharmony_ci	 * PHY to retrieve the desired data.
1288c2ecf20Sopenharmony_ci	 */
1298c2ecf20Sopenharmony_ci	mdic = ((offset << E1000_MDIC_REG_SHIFT) |
1308c2ecf20Sopenharmony_ci		(phy->addr << E1000_MDIC_PHY_SHIFT) |
1318c2ecf20Sopenharmony_ci		(E1000_MDIC_OP_READ));
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	wr32(E1000_MDIC, mdic);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	/* Poll the ready bit to see if the MDI read completed
1368c2ecf20Sopenharmony_ci	 * Increasing the time out as testing showed failures with
1378c2ecf20Sopenharmony_ci	 * the lower time out
1388c2ecf20Sopenharmony_ci	 */
1398c2ecf20Sopenharmony_ci	for (i = 0; i < (E1000_GEN_POLL_TIMEOUT * 3); i++) {
1408c2ecf20Sopenharmony_ci		udelay(50);
1418c2ecf20Sopenharmony_ci		mdic = rd32(E1000_MDIC);
1428c2ecf20Sopenharmony_ci		if (mdic & E1000_MDIC_READY)
1438c2ecf20Sopenharmony_ci			break;
1448c2ecf20Sopenharmony_ci	}
1458c2ecf20Sopenharmony_ci	if (!(mdic & E1000_MDIC_READY)) {
1468c2ecf20Sopenharmony_ci		hw_dbg("MDI Read did not complete\n");
1478c2ecf20Sopenharmony_ci		ret_val = -E1000_ERR_PHY;
1488c2ecf20Sopenharmony_ci		goto out;
1498c2ecf20Sopenharmony_ci	}
1508c2ecf20Sopenharmony_ci	if (mdic & E1000_MDIC_ERROR) {
1518c2ecf20Sopenharmony_ci		hw_dbg("MDI Error\n");
1528c2ecf20Sopenharmony_ci		ret_val = -E1000_ERR_PHY;
1538c2ecf20Sopenharmony_ci		goto out;
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci	*data = (u16) mdic;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ciout:
1588c2ecf20Sopenharmony_ci	return ret_val;
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci/**
1628c2ecf20Sopenharmony_ci *  igb_write_phy_reg_mdic - Write MDI control register
1638c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
1648c2ecf20Sopenharmony_ci *  @offset: register offset to write to
1658c2ecf20Sopenharmony_ci *  @data: data to write to register at offset
1668c2ecf20Sopenharmony_ci *
1678c2ecf20Sopenharmony_ci *  Writes data to MDI control register in the PHY at offset.
1688c2ecf20Sopenharmony_ci **/
1698c2ecf20Sopenharmony_cis32 igb_write_phy_reg_mdic(struct e1000_hw *hw, u32 offset, u16 data)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
1728c2ecf20Sopenharmony_ci	u32 i, mdic = 0;
1738c2ecf20Sopenharmony_ci	s32 ret_val = 0;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	if (offset > MAX_PHY_REG_ADDRESS) {
1768c2ecf20Sopenharmony_ci		hw_dbg("PHY Address %d is out of range\n", offset);
1778c2ecf20Sopenharmony_ci		ret_val = -E1000_ERR_PARAM;
1788c2ecf20Sopenharmony_ci		goto out;
1798c2ecf20Sopenharmony_ci	}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	/* Set up Op-code, Phy Address, and register offset in the MDI
1828c2ecf20Sopenharmony_ci	 * Control register.  The MAC will take care of interfacing with the
1838c2ecf20Sopenharmony_ci	 * PHY to retrieve the desired data.
1848c2ecf20Sopenharmony_ci	 */
1858c2ecf20Sopenharmony_ci	mdic = (((u32)data) |
1868c2ecf20Sopenharmony_ci		(offset << E1000_MDIC_REG_SHIFT) |
1878c2ecf20Sopenharmony_ci		(phy->addr << E1000_MDIC_PHY_SHIFT) |
1888c2ecf20Sopenharmony_ci		(E1000_MDIC_OP_WRITE));
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	wr32(E1000_MDIC, mdic);
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	/* Poll the ready bit to see if the MDI read completed
1938c2ecf20Sopenharmony_ci	 * Increasing the time out as testing showed failures with
1948c2ecf20Sopenharmony_ci	 * the lower time out
1958c2ecf20Sopenharmony_ci	 */
1968c2ecf20Sopenharmony_ci	for (i = 0; i < (E1000_GEN_POLL_TIMEOUT * 3); i++) {
1978c2ecf20Sopenharmony_ci		udelay(50);
1988c2ecf20Sopenharmony_ci		mdic = rd32(E1000_MDIC);
1998c2ecf20Sopenharmony_ci		if (mdic & E1000_MDIC_READY)
2008c2ecf20Sopenharmony_ci			break;
2018c2ecf20Sopenharmony_ci	}
2028c2ecf20Sopenharmony_ci	if (!(mdic & E1000_MDIC_READY)) {
2038c2ecf20Sopenharmony_ci		hw_dbg("MDI Write did not complete\n");
2048c2ecf20Sopenharmony_ci		ret_val = -E1000_ERR_PHY;
2058c2ecf20Sopenharmony_ci		goto out;
2068c2ecf20Sopenharmony_ci	}
2078c2ecf20Sopenharmony_ci	if (mdic & E1000_MDIC_ERROR) {
2088c2ecf20Sopenharmony_ci		hw_dbg("MDI Error\n");
2098c2ecf20Sopenharmony_ci		ret_val = -E1000_ERR_PHY;
2108c2ecf20Sopenharmony_ci		goto out;
2118c2ecf20Sopenharmony_ci	}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ciout:
2148c2ecf20Sopenharmony_ci	return ret_val;
2158c2ecf20Sopenharmony_ci}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci/**
2188c2ecf20Sopenharmony_ci *  igb_read_phy_reg_i2c - Read PHY register using i2c
2198c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
2208c2ecf20Sopenharmony_ci *  @offset: register offset to be read
2218c2ecf20Sopenharmony_ci *  @data: pointer to the read data
2228c2ecf20Sopenharmony_ci *
2238c2ecf20Sopenharmony_ci *  Reads the PHY register at offset using the i2c interface and stores the
2248c2ecf20Sopenharmony_ci *  retrieved information in data.
2258c2ecf20Sopenharmony_ci **/
2268c2ecf20Sopenharmony_cis32 igb_read_phy_reg_i2c(struct e1000_hw *hw, u32 offset, u16 *data)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
2298c2ecf20Sopenharmony_ci	u32 i, i2ccmd = 0;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	/* Set up Op-code, Phy Address, and register address in the I2CCMD
2328c2ecf20Sopenharmony_ci	 * register.  The MAC will take care of interfacing with the
2338c2ecf20Sopenharmony_ci	 * PHY to retrieve the desired data.
2348c2ecf20Sopenharmony_ci	 */
2358c2ecf20Sopenharmony_ci	i2ccmd = ((offset << E1000_I2CCMD_REG_ADDR_SHIFT) |
2368c2ecf20Sopenharmony_ci		  (phy->addr << E1000_I2CCMD_PHY_ADDR_SHIFT) |
2378c2ecf20Sopenharmony_ci		  (E1000_I2CCMD_OPCODE_READ));
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	wr32(E1000_I2CCMD, i2ccmd);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	/* Poll the ready bit to see if the I2C read completed */
2428c2ecf20Sopenharmony_ci	for (i = 0; i < E1000_I2CCMD_PHY_TIMEOUT; i++) {
2438c2ecf20Sopenharmony_ci		udelay(50);
2448c2ecf20Sopenharmony_ci		i2ccmd = rd32(E1000_I2CCMD);
2458c2ecf20Sopenharmony_ci		if (i2ccmd & E1000_I2CCMD_READY)
2468c2ecf20Sopenharmony_ci			break;
2478c2ecf20Sopenharmony_ci	}
2488c2ecf20Sopenharmony_ci	if (!(i2ccmd & E1000_I2CCMD_READY)) {
2498c2ecf20Sopenharmony_ci		hw_dbg("I2CCMD Read did not complete\n");
2508c2ecf20Sopenharmony_ci		return -E1000_ERR_PHY;
2518c2ecf20Sopenharmony_ci	}
2528c2ecf20Sopenharmony_ci	if (i2ccmd & E1000_I2CCMD_ERROR) {
2538c2ecf20Sopenharmony_ci		hw_dbg("I2CCMD Error bit set\n");
2548c2ecf20Sopenharmony_ci		return -E1000_ERR_PHY;
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	/* Need to byte-swap the 16-bit value. */
2588c2ecf20Sopenharmony_ci	*data = ((i2ccmd >> 8) & 0x00FF) | ((i2ccmd << 8) & 0xFF00);
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	return 0;
2618c2ecf20Sopenharmony_ci}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci/**
2648c2ecf20Sopenharmony_ci *  igb_write_phy_reg_i2c - Write PHY register using i2c
2658c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
2668c2ecf20Sopenharmony_ci *  @offset: register offset to write to
2678c2ecf20Sopenharmony_ci *  @data: data to write at register offset
2688c2ecf20Sopenharmony_ci *
2698c2ecf20Sopenharmony_ci *  Writes the data to PHY register at the offset using the i2c interface.
2708c2ecf20Sopenharmony_ci **/
2718c2ecf20Sopenharmony_cis32 igb_write_phy_reg_i2c(struct e1000_hw *hw, u32 offset, u16 data)
2728c2ecf20Sopenharmony_ci{
2738c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
2748c2ecf20Sopenharmony_ci	u32 i, i2ccmd = 0;
2758c2ecf20Sopenharmony_ci	u16 phy_data_swapped;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	/* Prevent overwriting SFP I2C EEPROM which is at A0 address.*/
2788c2ecf20Sopenharmony_ci	if ((hw->phy.addr == 0) || (hw->phy.addr > 7)) {
2798c2ecf20Sopenharmony_ci		hw_dbg("PHY I2C Address %d is out of range.\n",
2808c2ecf20Sopenharmony_ci			  hw->phy.addr);
2818c2ecf20Sopenharmony_ci		return -E1000_ERR_CONFIG;
2828c2ecf20Sopenharmony_ci	}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	/* Swap the data bytes for the I2C interface */
2858c2ecf20Sopenharmony_ci	phy_data_swapped = ((data >> 8) & 0x00FF) | ((data << 8) & 0xFF00);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	/* Set up Op-code, Phy Address, and register address in the I2CCMD
2888c2ecf20Sopenharmony_ci	 * register.  The MAC will take care of interfacing with the
2898c2ecf20Sopenharmony_ci	 * PHY to retrieve the desired data.
2908c2ecf20Sopenharmony_ci	 */
2918c2ecf20Sopenharmony_ci	i2ccmd = ((offset << E1000_I2CCMD_REG_ADDR_SHIFT) |
2928c2ecf20Sopenharmony_ci		  (phy->addr << E1000_I2CCMD_PHY_ADDR_SHIFT) |
2938c2ecf20Sopenharmony_ci		  E1000_I2CCMD_OPCODE_WRITE |
2948c2ecf20Sopenharmony_ci		  phy_data_swapped);
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	wr32(E1000_I2CCMD, i2ccmd);
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	/* Poll the ready bit to see if the I2C read completed */
2998c2ecf20Sopenharmony_ci	for (i = 0; i < E1000_I2CCMD_PHY_TIMEOUT; i++) {
3008c2ecf20Sopenharmony_ci		udelay(50);
3018c2ecf20Sopenharmony_ci		i2ccmd = rd32(E1000_I2CCMD);
3028c2ecf20Sopenharmony_ci		if (i2ccmd & E1000_I2CCMD_READY)
3038c2ecf20Sopenharmony_ci			break;
3048c2ecf20Sopenharmony_ci	}
3058c2ecf20Sopenharmony_ci	if (!(i2ccmd & E1000_I2CCMD_READY)) {
3068c2ecf20Sopenharmony_ci		hw_dbg("I2CCMD Write did not complete\n");
3078c2ecf20Sopenharmony_ci		return -E1000_ERR_PHY;
3088c2ecf20Sopenharmony_ci	}
3098c2ecf20Sopenharmony_ci	if (i2ccmd & E1000_I2CCMD_ERROR) {
3108c2ecf20Sopenharmony_ci		hw_dbg("I2CCMD Error bit set\n");
3118c2ecf20Sopenharmony_ci		return -E1000_ERR_PHY;
3128c2ecf20Sopenharmony_ci	}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	return 0;
3158c2ecf20Sopenharmony_ci}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci/**
3188c2ecf20Sopenharmony_ci *  igb_read_sfp_data_byte - Reads SFP module data.
3198c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
3208c2ecf20Sopenharmony_ci *  @offset: byte location offset to be read
3218c2ecf20Sopenharmony_ci *  @data: read data buffer pointer
3228c2ecf20Sopenharmony_ci *
3238c2ecf20Sopenharmony_ci *  Reads one byte from SFP module data stored
3248c2ecf20Sopenharmony_ci *  in SFP resided EEPROM memory or SFP diagnostic area.
3258c2ecf20Sopenharmony_ci *  Function should be called with
3268c2ecf20Sopenharmony_ci *  E1000_I2CCMD_SFP_DATA_ADDR(<byte offset>) for SFP module database access
3278c2ecf20Sopenharmony_ci *  E1000_I2CCMD_SFP_DIAG_ADDR(<byte offset>) for SFP diagnostics parameters
3288c2ecf20Sopenharmony_ci *  access
3298c2ecf20Sopenharmony_ci **/
3308c2ecf20Sopenharmony_cis32 igb_read_sfp_data_byte(struct e1000_hw *hw, u16 offset, u8 *data)
3318c2ecf20Sopenharmony_ci{
3328c2ecf20Sopenharmony_ci	u32 i = 0;
3338c2ecf20Sopenharmony_ci	u32 i2ccmd = 0;
3348c2ecf20Sopenharmony_ci	u32 data_local = 0;
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	if (offset > E1000_I2CCMD_SFP_DIAG_ADDR(255)) {
3378c2ecf20Sopenharmony_ci		hw_dbg("I2CCMD command address exceeds upper limit\n");
3388c2ecf20Sopenharmony_ci		return -E1000_ERR_PHY;
3398c2ecf20Sopenharmony_ci	}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	/* Set up Op-code, EEPROM Address,in the I2CCMD
3428c2ecf20Sopenharmony_ci	 * register. The MAC will take care of interfacing with the
3438c2ecf20Sopenharmony_ci	 * EEPROM to retrieve the desired data.
3448c2ecf20Sopenharmony_ci	 */
3458c2ecf20Sopenharmony_ci	i2ccmd = ((offset << E1000_I2CCMD_REG_ADDR_SHIFT) |
3468c2ecf20Sopenharmony_ci		  E1000_I2CCMD_OPCODE_READ);
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	wr32(E1000_I2CCMD, i2ccmd);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	/* Poll the ready bit to see if the I2C read completed */
3518c2ecf20Sopenharmony_ci	for (i = 0; i < E1000_I2CCMD_PHY_TIMEOUT; i++) {
3528c2ecf20Sopenharmony_ci		udelay(50);
3538c2ecf20Sopenharmony_ci		data_local = rd32(E1000_I2CCMD);
3548c2ecf20Sopenharmony_ci		if (data_local & E1000_I2CCMD_READY)
3558c2ecf20Sopenharmony_ci			break;
3568c2ecf20Sopenharmony_ci	}
3578c2ecf20Sopenharmony_ci	if (!(data_local & E1000_I2CCMD_READY)) {
3588c2ecf20Sopenharmony_ci		hw_dbg("I2CCMD Read did not complete\n");
3598c2ecf20Sopenharmony_ci		return -E1000_ERR_PHY;
3608c2ecf20Sopenharmony_ci	}
3618c2ecf20Sopenharmony_ci	if (data_local & E1000_I2CCMD_ERROR) {
3628c2ecf20Sopenharmony_ci		hw_dbg("I2CCMD Error bit set\n");
3638c2ecf20Sopenharmony_ci		return -E1000_ERR_PHY;
3648c2ecf20Sopenharmony_ci	}
3658c2ecf20Sopenharmony_ci	*data = (u8) data_local & 0xFF;
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	return 0;
3688c2ecf20Sopenharmony_ci}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci/**
3718c2ecf20Sopenharmony_ci *  igb_read_phy_reg_igp - Read igp PHY register
3728c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
3738c2ecf20Sopenharmony_ci *  @offset: register offset to be read
3748c2ecf20Sopenharmony_ci *  @data: pointer to the read data
3758c2ecf20Sopenharmony_ci *
3768c2ecf20Sopenharmony_ci *  Acquires semaphore, if necessary, then reads the PHY register at offset
3778c2ecf20Sopenharmony_ci *  and storing the retrieved information in data.  Release any acquired
3788c2ecf20Sopenharmony_ci *  semaphores before exiting.
3798c2ecf20Sopenharmony_ci **/
3808c2ecf20Sopenharmony_cis32 igb_read_phy_reg_igp(struct e1000_hw *hw, u32 offset, u16 *data)
3818c2ecf20Sopenharmony_ci{
3828c2ecf20Sopenharmony_ci	s32 ret_val = 0;
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	if (!(hw->phy.ops.acquire))
3858c2ecf20Sopenharmony_ci		goto out;
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	ret_val = hw->phy.ops.acquire(hw);
3888c2ecf20Sopenharmony_ci	if (ret_val)
3898c2ecf20Sopenharmony_ci		goto out;
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	if (offset > MAX_PHY_MULTI_PAGE_REG) {
3928c2ecf20Sopenharmony_ci		ret_val = igb_write_phy_reg_mdic(hw,
3938c2ecf20Sopenharmony_ci						 IGP01E1000_PHY_PAGE_SELECT,
3948c2ecf20Sopenharmony_ci						 (u16)offset);
3958c2ecf20Sopenharmony_ci		if (ret_val) {
3968c2ecf20Sopenharmony_ci			hw->phy.ops.release(hw);
3978c2ecf20Sopenharmony_ci			goto out;
3988c2ecf20Sopenharmony_ci		}
3998c2ecf20Sopenharmony_ci	}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	ret_val = igb_read_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & offset,
4028c2ecf20Sopenharmony_ci					data);
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	hw->phy.ops.release(hw);
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ciout:
4078c2ecf20Sopenharmony_ci	return ret_val;
4088c2ecf20Sopenharmony_ci}
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci/**
4118c2ecf20Sopenharmony_ci *  igb_write_phy_reg_igp - Write igp PHY register
4128c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
4138c2ecf20Sopenharmony_ci *  @offset: register offset to write to
4148c2ecf20Sopenharmony_ci *  @data: data to write at register offset
4158c2ecf20Sopenharmony_ci *
4168c2ecf20Sopenharmony_ci *  Acquires semaphore, if necessary, then writes the data to PHY register
4178c2ecf20Sopenharmony_ci *  at the offset.  Release any acquired semaphores before exiting.
4188c2ecf20Sopenharmony_ci **/
4198c2ecf20Sopenharmony_cis32 igb_write_phy_reg_igp(struct e1000_hw *hw, u32 offset, u16 data)
4208c2ecf20Sopenharmony_ci{
4218c2ecf20Sopenharmony_ci	s32 ret_val = 0;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	if (!(hw->phy.ops.acquire))
4248c2ecf20Sopenharmony_ci		goto out;
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	ret_val = hw->phy.ops.acquire(hw);
4278c2ecf20Sopenharmony_ci	if (ret_val)
4288c2ecf20Sopenharmony_ci		goto out;
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	if (offset > MAX_PHY_MULTI_PAGE_REG) {
4318c2ecf20Sopenharmony_ci		ret_val = igb_write_phy_reg_mdic(hw,
4328c2ecf20Sopenharmony_ci						 IGP01E1000_PHY_PAGE_SELECT,
4338c2ecf20Sopenharmony_ci						 (u16)offset);
4348c2ecf20Sopenharmony_ci		if (ret_val) {
4358c2ecf20Sopenharmony_ci			hw->phy.ops.release(hw);
4368c2ecf20Sopenharmony_ci			goto out;
4378c2ecf20Sopenharmony_ci		}
4388c2ecf20Sopenharmony_ci	}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	ret_val = igb_write_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & offset,
4418c2ecf20Sopenharmony_ci					 data);
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	hw->phy.ops.release(hw);
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ciout:
4468c2ecf20Sopenharmony_ci	return ret_val;
4478c2ecf20Sopenharmony_ci}
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci/**
4508c2ecf20Sopenharmony_ci *  igb_copper_link_setup_82580 - Setup 82580 PHY for copper link
4518c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
4528c2ecf20Sopenharmony_ci *
4538c2ecf20Sopenharmony_ci *  Sets up Carrier-sense on Transmit and downshift values.
4548c2ecf20Sopenharmony_ci **/
4558c2ecf20Sopenharmony_cis32 igb_copper_link_setup_82580(struct e1000_hw *hw)
4568c2ecf20Sopenharmony_ci{
4578c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
4588c2ecf20Sopenharmony_ci	s32 ret_val;
4598c2ecf20Sopenharmony_ci	u16 phy_data;
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci	if (phy->reset_disable) {
4628c2ecf20Sopenharmony_ci		ret_val = 0;
4638c2ecf20Sopenharmony_ci		goto out;
4648c2ecf20Sopenharmony_ci	}
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	if (phy->type == e1000_phy_82580) {
4678c2ecf20Sopenharmony_ci		ret_val = hw->phy.ops.reset(hw);
4688c2ecf20Sopenharmony_ci		if (ret_val) {
4698c2ecf20Sopenharmony_ci			hw_dbg("Error resetting the PHY.\n");
4708c2ecf20Sopenharmony_ci			goto out;
4718c2ecf20Sopenharmony_ci		}
4728c2ecf20Sopenharmony_ci	}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	/* Enable CRS on TX. This must be set for half-duplex operation. */
4758c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, I82580_CFG_REG, &phy_data);
4768c2ecf20Sopenharmony_ci	if (ret_val)
4778c2ecf20Sopenharmony_ci		goto out;
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	phy_data |= I82580_CFG_ASSERT_CRS_ON_TX;
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	/* Enable downshift */
4828c2ecf20Sopenharmony_ci	phy_data |= I82580_CFG_ENABLE_DOWNSHIFT;
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, I82580_CFG_REG, phy_data);
4858c2ecf20Sopenharmony_ci	if (ret_val)
4868c2ecf20Sopenharmony_ci		goto out;
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	/* Set MDI/MDIX mode */
4898c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, I82580_PHY_CTRL_2, &phy_data);
4908c2ecf20Sopenharmony_ci	if (ret_val)
4918c2ecf20Sopenharmony_ci		goto out;
4928c2ecf20Sopenharmony_ci	phy_data &= ~I82580_PHY_CTRL2_MDIX_CFG_MASK;
4938c2ecf20Sopenharmony_ci	/* Options:
4948c2ecf20Sopenharmony_ci	 *   0 - Auto (default)
4958c2ecf20Sopenharmony_ci	 *   1 - MDI mode
4968c2ecf20Sopenharmony_ci	 *   2 - MDI-X mode
4978c2ecf20Sopenharmony_ci	 */
4988c2ecf20Sopenharmony_ci	switch (hw->phy.mdix) {
4998c2ecf20Sopenharmony_ci	case 1:
5008c2ecf20Sopenharmony_ci		break;
5018c2ecf20Sopenharmony_ci	case 2:
5028c2ecf20Sopenharmony_ci		phy_data |= I82580_PHY_CTRL2_MANUAL_MDIX;
5038c2ecf20Sopenharmony_ci		break;
5048c2ecf20Sopenharmony_ci	case 0:
5058c2ecf20Sopenharmony_ci	default:
5068c2ecf20Sopenharmony_ci		phy_data |= I82580_PHY_CTRL2_AUTO_MDI_MDIX;
5078c2ecf20Sopenharmony_ci		break;
5088c2ecf20Sopenharmony_ci	}
5098c2ecf20Sopenharmony_ci	ret_val = hw->phy.ops.write_reg(hw, I82580_PHY_CTRL_2, phy_data);
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ciout:
5128c2ecf20Sopenharmony_ci	return ret_val;
5138c2ecf20Sopenharmony_ci}
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci/**
5168c2ecf20Sopenharmony_ci *  igb_copper_link_setup_m88 - Setup m88 PHY's for copper link
5178c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
5188c2ecf20Sopenharmony_ci *
5198c2ecf20Sopenharmony_ci *  Sets up MDI/MDI-X and polarity for m88 PHY's.  If necessary, transmit clock
5208c2ecf20Sopenharmony_ci *  and downshift values are set also.
5218c2ecf20Sopenharmony_ci **/
5228c2ecf20Sopenharmony_cis32 igb_copper_link_setup_m88(struct e1000_hw *hw)
5238c2ecf20Sopenharmony_ci{
5248c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
5258c2ecf20Sopenharmony_ci	s32 ret_val;
5268c2ecf20Sopenharmony_ci	u16 phy_data;
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci	if (phy->reset_disable) {
5298c2ecf20Sopenharmony_ci		ret_val = 0;
5308c2ecf20Sopenharmony_ci		goto out;
5318c2ecf20Sopenharmony_ci	}
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	/* Enable CRS on TX. This must be set for half-duplex operation. */
5348c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
5358c2ecf20Sopenharmony_ci	if (ret_val)
5368c2ecf20Sopenharmony_ci		goto out;
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	/* Options:
5418c2ecf20Sopenharmony_ci	 *   MDI/MDI-X = 0 (default)
5428c2ecf20Sopenharmony_ci	 *   0 - Auto for all speeds
5438c2ecf20Sopenharmony_ci	 *   1 - MDI mode
5448c2ecf20Sopenharmony_ci	 *   2 - MDI-X mode
5458c2ecf20Sopenharmony_ci	 *   3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
5468c2ecf20Sopenharmony_ci	 */
5478c2ecf20Sopenharmony_ci	phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	switch (phy->mdix) {
5508c2ecf20Sopenharmony_ci	case 1:
5518c2ecf20Sopenharmony_ci		phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
5528c2ecf20Sopenharmony_ci		break;
5538c2ecf20Sopenharmony_ci	case 2:
5548c2ecf20Sopenharmony_ci		phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
5558c2ecf20Sopenharmony_ci		break;
5568c2ecf20Sopenharmony_ci	case 3:
5578c2ecf20Sopenharmony_ci		phy_data |= M88E1000_PSCR_AUTO_X_1000T;
5588c2ecf20Sopenharmony_ci		break;
5598c2ecf20Sopenharmony_ci	case 0:
5608c2ecf20Sopenharmony_ci	default:
5618c2ecf20Sopenharmony_ci		phy_data |= M88E1000_PSCR_AUTO_X_MODE;
5628c2ecf20Sopenharmony_ci		break;
5638c2ecf20Sopenharmony_ci	}
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	/* Options:
5668c2ecf20Sopenharmony_ci	 *   disable_polarity_correction = 0 (default)
5678c2ecf20Sopenharmony_ci	 *       Automatic Correction for Reversed Cable Polarity
5688c2ecf20Sopenharmony_ci	 *   0 - Disabled
5698c2ecf20Sopenharmony_ci	 *   1 - Enabled
5708c2ecf20Sopenharmony_ci	 */
5718c2ecf20Sopenharmony_ci	phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
5728c2ecf20Sopenharmony_ci	if (phy->disable_polarity_correction == 1)
5738c2ecf20Sopenharmony_ci		phy_data |= M88E1000_PSCR_POLARITY_REVERSAL;
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
5768c2ecf20Sopenharmony_ci	if (ret_val)
5778c2ecf20Sopenharmony_ci		goto out;
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	if (phy->revision < E1000_REVISION_4) {
5808c2ecf20Sopenharmony_ci		/* Force TX_CLK in the Extended PHY Specific Control Register
5818c2ecf20Sopenharmony_ci		 * to 25MHz clock.
5828c2ecf20Sopenharmony_ci		 */
5838c2ecf20Sopenharmony_ci		ret_val = phy->ops.read_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL,
5848c2ecf20Sopenharmony_ci					    &phy_data);
5858c2ecf20Sopenharmony_ci		if (ret_val)
5868c2ecf20Sopenharmony_ci			goto out;
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci		phy_data |= M88E1000_EPSCR_TX_CLK_25;
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci		if ((phy->revision == E1000_REVISION_2) &&
5918c2ecf20Sopenharmony_ci		    (phy->id == M88E1111_I_PHY_ID)) {
5928c2ecf20Sopenharmony_ci			/* 82573L PHY - set the downshift counter to 5x. */
5938c2ecf20Sopenharmony_ci			phy_data &= ~M88EC018_EPSCR_DOWNSHIFT_COUNTER_MASK;
5948c2ecf20Sopenharmony_ci			phy_data |= M88EC018_EPSCR_DOWNSHIFT_COUNTER_5X;
5958c2ecf20Sopenharmony_ci		} else {
5968c2ecf20Sopenharmony_ci			/* Configure Master and Slave downshift values */
5978c2ecf20Sopenharmony_ci			phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK |
5988c2ecf20Sopenharmony_ci				      M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
5998c2ecf20Sopenharmony_ci			phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X |
6008c2ecf20Sopenharmony_ci				     M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
6018c2ecf20Sopenharmony_ci		}
6028c2ecf20Sopenharmony_ci		ret_val = phy->ops.write_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL,
6038c2ecf20Sopenharmony_ci					     phy_data);
6048c2ecf20Sopenharmony_ci		if (ret_val)
6058c2ecf20Sopenharmony_ci			goto out;
6068c2ecf20Sopenharmony_ci	}
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	/* Commit the changes. */
6098c2ecf20Sopenharmony_ci	ret_val = igb_phy_sw_reset(hw);
6108c2ecf20Sopenharmony_ci	if (ret_val) {
6118c2ecf20Sopenharmony_ci		hw_dbg("Error committing the PHY changes\n");
6128c2ecf20Sopenharmony_ci		goto out;
6138c2ecf20Sopenharmony_ci	}
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ciout:
6168c2ecf20Sopenharmony_ci	return ret_val;
6178c2ecf20Sopenharmony_ci}
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci/**
6208c2ecf20Sopenharmony_ci *  igb_copper_link_setup_m88_gen2 - Setup m88 PHY's for copper link
6218c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
6228c2ecf20Sopenharmony_ci *
6238c2ecf20Sopenharmony_ci *  Sets up MDI/MDI-X and polarity for i347-AT4, m88e1322 and m88e1112 PHY's.
6248c2ecf20Sopenharmony_ci *  Also enables and sets the downshift parameters.
6258c2ecf20Sopenharmony_ci **/
6268c2ecf20Sopenharmony_cis32 igb_copper_link_setup_m88_gen2(struct e1000_hw *hw)
6278c2ecf20Sopenharmony_ci{
6288c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
6298c2ecf20Sopenharmony_ci	s32 ret_val;
6308c2ecf20Sopenharmony_ci	u16 phy_data;
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	if (phy->reset_disable)
6338c2ecf20Sopenharmony_ci		return 0;
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	/* Enable CRS on Tx. This must be set for half-duplex operation. */
6368c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
6378c2ecf20Sopenharmony_ci	if (ret_val)
6388c2ecf20Sopenharmony_ci		return ret_val;
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	/* Options:
6418c2ecf20Sopenharmony_ci	 *   MDI/MDI-X = 0 (default)
6428c2ecf20Sopenharmony_ci	 *   0 - Auto for all speeds
6438c2ecf20Sopenharmony_ci	 *   1 - MDI mode
6448c2ecf20Sopenharmony_ci	 *   2 - MDI-X mode
6458c2ecf20Sopenharmony_ci	 *   3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
6468c2ecf20Sopenharmony_ci	 */
6478c2ecf20Sopenharmony_ci	phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	switch (phy->mdix) {
6508c2ecf20Sopenharmony_ci	case 1:
6518c2ecf20Sopenharmony_ci		phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
6528c2ecf20Sopenharmony_ci		break;
6538c2ecf20Sopenharmony_ci	case 2:
6548c2ecf20Sopenharmony_ci		phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
6558c2ecf20Sopenharmony_ci		break;
6568c2ecf20Sopenharmony_ci	case 3:
6578c2ecf20Sopenharmony_ci		/* M88E1112 does not support this mode) */
6588c2ecf20Sopenharmony_ci		if (phy->id != M88E1112_E_PHY_ID) {
6598c2ecf20Sopenharmony_ci			phy_data |= M88E1000_PSCR_AUTO_X_1000T;
6608c2ecf20Sopenharmony_ci			break;
6618c2ecf20Sopenharmony_ci		}
6628c2ecf20Sopenharmony_ci		fallthrough;
6638c2ecf20Sopenharmony_ci	case 0:
6648c2ecf20Sopenharmony_ci	default:
6658c2ecf20Sopenharmony_ci		phy_data |= M88E1000_PSCR_AUTO_X_MODE;
6668c2ecf20Sopenharmony_ci		break;
6678c2ecf20Sopenharmony_ci	}
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	/* Options:
6708c2ecf20Sopenharmony_ci	 *   disable_polarity_correction = 0 (default)
6718c2ecf20Sopenharmony_ci	 *       Automatic Correction for Reversed Cable Polarity
6728c2ecf20Sopenharmony_ci	 *   0 - Disabled
6738c2ecf20Sopenharmony_ci	 *   1 - Enabled
6748c2ecf20Sopenharmony_ci	 */
6758c2ecf20Sopenharmony_ci	phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
6768c2ecf20Sopenharmony_ci	if (phy->disable_polarity_correction == 1)
6778c2ecf20Sopenharmony_ci		phy_data |= M88E1000_PSCR_POLARITY_REVERSAL;
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	/* Enable downshift and setting it to X6 */
6808c2ecf20Sopenharmony_ci	if (phy->id == M88E1543_E_PHY_ID) {
6818c2ecf20Sopenharmony_ci		phy_data &= ~I347AT4_PSCR_DOWNSHIFT_ENABLE;
6828c2ecf20Sopenharmony_ci		ret_val =
6838c2ecf20Sopenharmony_ci		    phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
6848c2ecf20Sopenharmony_ci		if (ret_val)
6858c2ecf20Sopenharmony_ci			return ret_val;
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci		ret_val = igb_phy_sw_reset(hw);
6888c2ecf20Sopenharmony_ci		if (ret_val) {
6898c2ecf20Sopenharmony_ci			hw_dbg("Error committing the PHY changes\n");
6908c2ecf20Sopenharmony_ci			return ret_val;
6918c2ecf20Sopenharmony_ci		}
6928c2ecf20Sopenharmony_ci	}
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	phy_data &= ~I347AT4_PSCR_DOWNSHIFT_MASK;
6958c2ecf20Sopenharmony_ci	phy_data |= I347AT4_PSCR_DOWNSHIFT_6X;
6968c2ecf20Sopenharmony_ci	phy_data |= I347AT4_PSCR_DOWNSHIFT_ENABLE;
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
6998c2ecf20Sopenharmony_ci	if (ret_val)
7008c2ecf20Sopenharmony_ci		return ret_val;
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	/* Commit the changes. */
7038c2ecf20Sopenharmony_ci	ret_val = igb_phy_sw_reset(hw);
7048c2ecf20Sopenharmony_ci	if (ret_val) {
7058c2ecf20Sopenharmony_ci		hw_dbg("Error committing the PHY changes\n");
7068c2ecf20Sopenharmony_ci		return ret_val;
7078c2ecf20Sopenharmony_ci	}
7088c2ecf20Sopenharmony_ci	ret_val = igb_set_master_slave_mode(hw);
7098c2ecf20Sopenharmony_ci	if (ret_val)
7108c2ecf20Sopenharmony_ci		return ret_val;
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	return 0;
7138c2ecf20Sopenharmony_ci}
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci/**
7168c2ecf20Sopenharmony_ci *  igb_copper_link_setup_igp - Setup igp PHY's for copper link
7178c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
7188c2ecf20Sopenharmony_ci *
7198c2ecf20Sopenharmony_ci *  Sets up LPLU, MDI/MDI-X, polarity, Smartspeed and Master/Slave config for
7208c2ecf20Sopenharmony_ci *  igp PHY's.
7218c2ecf20Sopenharmony_ci **/
7228c2ecf20Sopenharmony_cis32 igb_copper_link_setup_igp(struct e1000_hw *hw)
7238c2ecf20Sopenharmony_ci{
7248c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
7258c2ecf20Sopenharmony_ci	s32 ret_val;
7268c2ecf20Sopenharmony_ci	u16 data;
7278c2ecf20Sopenharmony_ci
7288c2ecf20Sopenharmony_ci	if (phy->reset_disable) {
7298c2ecf20Sopenharmony_ci		ret_val = 0;
7308c2ecf20Sopenharmony_ci		goto out;
7318c2ecf20Sopenharmony_ci	}
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	ret_val = phy->ops.reset(hw);
7348c2ecf20Sopenharmony_ci	if (ret_val) {
7358c2ecf20Sopenharmony_ci		hw_dbg("Error resetting the PHY.\n");
7368c2ecf20Sopenharmony_ci		goto out;
7378c2ecf20Sopenharmony_ci	}
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	/* Wait 100ms for MAC to configure PHY from NVM settings, to avoid
7408c2ecf20Sopenharmony_ci	 * timeout issues when LFS is enabled.
7418c2ecf20Sopenharmony_ci	 */
7428c2ecf20Sopenharmony_ci	msleep(100);
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	/* The NVM settings will configure LPLU in D3 for
7458c2ecf20Sopenharmony_ci	 * non-IGP1 PHYs.
7468c2ecf20Sopenharmony_ci	 */
7478c2ecf20Sopenharmony_ci	if (phy->type == e1000_phy_igp) {
7488c2ecf20Sopenharmony_ci		/* disable lplu d3 during driver init */
7498c2ecf20Sopenharmony_ci		if (phy->ops.set_d3_lplu_state)
7508c2ecf20Sopenharmony_ci			ret_val = phy->ops.set_d3_lplu_state(hw, false);
7518c2ecf20Sopenharmony_ci		if (ret_val) {
7528c2ecf20Sopenharmony_ci			hw_dbg("Error Disabling LPLU D3\n");
7538c2ecf20Sopenharmony_ci			goto out;
7548c2ecf20Sopenharmony_ci		}
7558c2ecf20Sopenharmony_ci	}
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	/* disable lplu d0 during driver init */
7588c2ecf20Sopenharmony_ci	ret_val = phy->ops.set_d0_lplu_state(hw, false);
7598c2ecf20Sopenharmony_ci	if (ret_val) {
7608c2ecf20Sopenharmony_ci		hw_dbg("Error Disabling LPLU D0\n");
7618c2ecf20Sopenharmony_ci		goto out;
7628c2ecf20Sopenharmony_ci	}
7638c2ecf20Sopenharmony_ci	/* Configure mdi-mdix settings */
7648c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_CTRL, &data);
7658c2ecf20Sopenharmony_ci	if (ret_val)
7668c2ecf20Sopenharmony_ci		goto out;
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	data &= ~IGP01E1000_PSCR_AUTO_MDIX;
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci	switch (phy->mdix) {
7718c2ecf20Sopenharmony_ci	case 1:
7728c2ecf20Sopenharmony_ci		data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
7738c2ecf20Sopenharmony_ci		break;
7748c2ecf20Sopenharmony_ci	case 2:
7758c2ecf20Sopenharmony_ci		data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
7768c2ecf20Sopenharmony_ci		break;
7778c2ecf20Sopenharmony_ci	case 0:
7788c2ecf20Sopenharmony_ci	default:
7798c2ecf20Sopenharmony_ci		data |= IGP01E1000_PSCR_AUTO_MDIX;
7808c2ecf20Sopenharmony_ci		break;
7818c2ecf20Sopenharmony_ci	}
7828c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, IGP01E1000_PHY_PORT_CTRL, data);
7838c2ecf20Sopenharmony_ci	if (ret_val)
7848c2ecf20Sopenharmony_ci		goto out;
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	/* set auto-master slave resolution settings */
7878c2ecf20Sopenharmony_ci	if (hw->mac.autoneg) {
7888c2ecf20Sopenharmony_ci		/* when autonegotiation advertisement is only 1000Mbps then we
7898c2ecf20Sopenharmony_ci		 * should disable SmartSpeed and enable Auto MasterSlave
7908c2ecf20Sopenharmony_ci		 * resolution as hardware default.
7918c2ecf20Sopenharmony_ci		 */
7928c2ecf20Sopenharmony_ci		if (phy->autoneg_advertised == ADVERTISE_1000_FULL) {
7938c2ecf20Sopenharmony_ci			/* Disable SmartSpeed */
7948c2ecf20Sopenharmony_ci			ret_val = phy->ops.read_reg(hw,
7958c2ecf20Sopenharmony_ci						    IGP01E1000_PHY_PORT_CONFIG,
7968c2ecf20Sopenharmony_ci						    &data);
7978c2ecf20Sopenharmony_ci			if (ret_val)
7988c2ecf20Sopenharmony_ci				goto out;
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci			data &= ~IGP01E1000_PSCFR_SMART_SPEED;
8018c2ecf20Sopenharmony_ci			ret_val = phy->ops.write_reg(hw,
8028c2ecf20Sopenharmony_ci						     IGP01E1000_PHY_PORT_CONFIG,
8038c2ecf20Sopenharmony_ci						     data);
8048c2ecf20Sopenharmony_ci			if (ret_val)
8058c2ecf20Sopenharmony_ci				goto out;
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci			/* Set auto Master/Slave resolution process */
8088c2ecf20Sopenharmony_ci			ret_val = phy->ops.read_reg(hw, PHY_1000T_CTRL, &data);
8098c2ecf20Sopenharmony_ci			if (ret_val)
8108c2ecf20Sopenharmony_ci				goto out;
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci			data &= ~CR_1000T_MS_ENABLE;
8138c2ecf20Sopenharmony_ci			ret_val = phy->ops.write_reg(hw, PHY_1000T_CTRL, data);
8148c2ecf20Sopenharmony_ci			if (ret_val)
8158c2ecf20Sopenharmony_ci				goto out;
8168c2ecf20Sopenharmony_ci		}
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci		ret_val = phy->ops.read_reg(hw, PHY_1000T_CTRL, &data);
8198c2ecf20Sopenharmony_ci		if (ret_val)
8208c2ecf20Sopenharmony_ci			goto out;
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci		/* load defaults for future use */
8238c2ecf20Sopenharmony_ci		phy->original_ms_type = (data & CR_1000T_MS_ENABLE) ?
8248c2ecf20Sopenharmony_ci			((data & CR_1000T_MS_VALUE) ?
8258c2ecf20Sopenharmony_ci			e1000_ms_force_master :
8268c2ecf20Sopenharmony_ci			e1000_ms_force_slave) :
8278c2ecf20Sopenharmony_ci			e1000_ms_auto;
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci		switch (phy->ms_type) {
8308c2ecf20Sopenharmony_ci		case e1000_ms_force_master:
8318c2ecf20Sopenharmony_ci			data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
8328c2ecf20Sopenharmony_ci			break;
8338c2ecf20Sopenharmony_ci		case e1000_ms_force_slave:
8348c2ecf20Sopenharmony_ci			data |= CR_1000T_MS_ENABLE;
8358c2ecf20Sopenharmony_ci			data &= ~(CR_1000T_MS_VALUE);
8368c2ecf20Sopenharmony_ci			break;
8378c2ecf20Sopenharmony_ci		case e1000_ms_auto:
8388c2ecf20Sopenharmony_ci			data &= ~CR_1000T_MS_ENABLE;
8398c2ecf20Sopenharmony_ci		default:
8408c2ecf20Sopenharmony_ci			break;
8418c2ecf20Sopenharmony_ci		}
8428c2ecf20Sopenharmony_ci		ret_val = phy->ops.write_reg(hw, PHY_1000T_CTRL, data);
8438c2ecf20Sopenharmony_ci		if (ret_val)
8448c2ecf20Sopenharmony_ci			goto out;
8458c2ecf20Sopenharmony_ci	}
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ciout:
8488c2ecf20Sopenharmony_ci	return ret_val;
8498c2ecf20Sopenharmony_ci}
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci/**
8528c2ecf20Sopenharmony_ci *  igb_copper_link_autoneg - Setup/Enable autoneg for copper link
8538c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
8548c2ecf20Sopenharmony_ci *
8558c2ecf20Sopenharmony_ci *  Performs initial bounds checking on autoneg advertisement parameter, then
8568c2ecf20Sopenharmony_ci *  configure to advertise the full capability.  Setup the PHY to autoneg
8578c2ecf20Sopenharmony_ci *  and restart the negotiation process between the link partner.  If
8588c2ecf20Sopenharmony_ci *  autoneg_wait_to_complete, then wait for autoneg to complete before exiting.
8598c2ecf20Sopenharmony_ci **/
8608c2ecf20Sopenharmony_cistatic s32 igb_copper_link_autoneg(struct e1000_hw *hw)
8618c2ecf20Sopenharmony_ci{
8628c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
8638c2ecf20Sopenharmony_ci	s32 ret_val;
8648c2ecf20Sopenharmony_ci	u16 phy_ctrl;
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ci	/* Perform some bounds checking on the autoneg advertisement
8678c2ecf20Sopenharmony_ci	 * parameter.
8688c2ecf20Sopenharmony_ci	 */
8698c2ecf20Sopenharmony_ci	phy->autoneg_advertised &= phy->autoneg_mask;
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci	/* If autoneg_advertised is zero, we assume it was not defaulted
8728c2ecf20Sopenharmony_ci	 * by the calling code so we set to advertise full capability.
8738c2ecf20Sopenharmony_ci	 */
8748c2ecf20Sopenharmony_ci	if (phy->autoneg_advertised == 0)
8758c2ecf20Sopenharmony_ci		phy->autoneg_advertised = phy->autoneg_mask;
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci	hw_dbg("Reconfiguring auto-neg advertisement params\n");
8788c2ecf20Sopenharmony_ci	ret_val = igb_phy_setup_autoneg(hw);
8798c2ecf20Sopenharmony_ci	if (ret_val) {
8808c2ecf20Sopenharmony_ci		hw_dbg("Error Setting up Auto-Negotiation\n");
8818c2ecf20Sopenharmony_ci		goto out;
8828c2ecf20Sopenharmony_ci	}
8838c2ecf20Sopenharmony_ci	hw_dbg("Restarting Auto-Neg\n");
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	/* Restart auto-negotiation by setting the Auto Neg Enable bit and
8868c2ecf20Sopenharmony_ci	 * the Auto Neg Restart bit in the PHY control register.
8878c2ecf20Sopenharmony_ci	 */
8888c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_ctrl);
8898c2ecf20Sopenharmony_ci	if (ret_val)
8908c2ecf20Sopenharmony_ci		goto out;
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	phy_ctrl |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
8938c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_ctrl);
8948c2ecf20Sopenharmony_ci	if (ret_val)
8958c2ecf20Sopenharmony_ci		goto out;
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	/* Does the user want to wait for Auto-Neg to complete here, or
8988c2ecf20Sopenharmony_ci	 * check at a later time (for example, callback routine).
8998c2ecf20Sopenharmony_ci	 */
9008c2ecf20Sopenharmony_ci	if (phy->autoneg_wait_to_complete) {
9018c2ecf20Sopenharmony_ci		ret_val = igb_wait_autoneg(hw);
9028c2ecf20Sopenharmony_ci		if (ret_val) {
9038c2ecf20Sopenharmony_ci			hw_dbg("Error while waiting for autoneg to complete\n");
9048c2ecf20Sopenharmony_ci			goto out;
9058c2ecf20Sopenharmony_ci		}
9068c2ecf20Sopenharmony_ci	}
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_ci	hw->mac.get_link_status = true;
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ciout:
9118c2ecf20Sopenharmony_ci	return ret_val;
9128c2ecf20Sopenharmony_ci}
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ci/**
9158c2ecf20Sopenharmony_ci *  igb_phy_setup_autoneg - Configure PHY for auto-negotiation
9168c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
9178c2ecf20Sopenharmony_ci *
9188c2ecf20Sopenharmony_ci *  Reads the MII auto-neg advertisement register and/or the 1000T control
9198c2ecf20Sopenharmony_ci *  register and if the PHY is already setup for auto-negotiation, then
9208c2ecf20Sopenharmony_ci *  return successful.  Otherwise, setup advertisement and flow control to
9218c2ecf20Sopenharmony_ci *  the appropriate values for the wanted auto-negotiation.
9228c2ecf20Sopenharmony_ci **/
9238c2ecf20Sopenharmony_cistatic s32 igb_phy_setup_autoneg(struct e1000_hw *hw)
9248c2ecf20Sopenharmony_ci{
9258c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
9268c2ecf20Sopenharmony_ci	s32 ret_val;
9278c2ecf20Sopenharmony_ci	u16 mii_autoneg_adv_reg;
9288c2ecf20Sopenharmony_ci	u16 mii_1000t_ctrl_reg = 0;
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_ci	phy->autoneg_advertised &= phy->autoneg_mask;
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci	/* Read the MII Auto-Neg Advertisement Register (Address 4). */
9338c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
9348c2ecf20Sopenharmony_ci	if (ret_val)
9358c2ecf20Sopenharmony_ci		goto out;
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci	if (phy->autoneg_mask & ADVERTISE_1000_FULL) {
9388c2ecf20Sopenharmony_ci		/* Read the MII 1000Base-T Control Register (Address 9). */
9398c2ecf20Sopenharmony_ci		ret_val = phy->ops.read_reg(hw, PHY_1000T_CTRL,
9408c2ecf20Sopenharmony_ci					    &mii_1000t_ctrl_reg);
9418c2ecf20Sopenharmony_ci		if (ret_val)
9428c2ecf20Sopenharmony_ci			goto out;
9438c2ecf20Sopenharmony_ci	}
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci	/* Need to parse both autoneg_advertised and fc and set up
9468c2ecf20Sopenharmony_ci	 * the appropriate PHY registers.  First we will parse for
9478c2ecf20Sopenharmony_ci	 * autoneg_advertised software override.  Since we can advertise
9488c2ecf20Sopenharmony_ci	 * a plethora of combinations, we need to check each bit
9498c2ecf20Sopenharmony_ci	 * individually.
9508c2ecf20Sopenharmony_ci	 */
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci	/* First we clear all the 10/100 mb speed bits in the Auto-Neg
9538c2ecf20Sopenharmony_ci	 * Advertisement Register (Address 4) and the 1000 mb speed bits in
9548c2ecf20Sopenharmony_ci	 * the  1000Base-T Control Register (Address 9).
9558c2ecf20Sopenharmony_ci	 */
9568c2ecf20Sopenharmony_ci	mii_autoneg_adv_reg &= ~(NWAY_AR_100TX_FD_CAPS |
9578c2ecf20Sopenharmony_ci				 NWAY_AR_100TX_HD_CAPS |
9588c2ecf20Sopenharmony_ci				 NWAY_AR_10T_FD_CAPS   |
9598c2ecf20Sopenharmony_ci				 NWAY_AR_10T_HD_CAPS);
9608c2ecf20Sopenharmony_ci	mii_1000t_ctrl_reg &= ~(CR_1000T_HD_CAPS | CR_1000T_FD_CAPS);
9618c2ecf20Sopenharmony_ci
9628c2ecf20Sopenharmony_ci	hw_dbg("autoneg_advertised %x\n", phy->autoneg_advertised);
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	/* Do we want to advertise 10 Mb Half Duplex? */
9658c2ecf20Sopenharmony_ci	if (phy->autoneg_advertised & ADVERTISE_10_HALF) {
9668c2ecf20Sopenharmony_ci		hw_dbg("Advertise 10mb Half duplex\n");
9678c2ecf20Sopenharmony_ci		mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
9688c2ecf20Sopenharmony_ci	}
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci	/* Do we want to advertise 10 Mb Full Duplex? */
9718c2ecf20Sopenharmony_ci	if (phy->autoneg_advertised & ADVERTISE_10_FULL) {
9728c2ecf20Sopenharmony_ci		hw_dbg("Advertise 10mb Full duplex\n");
9738c2ecf20Sopenharmony_ci		mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
9748c2ecf20Sopenharmony_ci	}
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_ci	/* Do we want to advertise 100 Mb Half Duplex? */
9778c2ecf20Sopenharmony_ci	if (phy->autoneg_advertised & ADVERTISE_100_HALF) {
9788c2ecf20Sopenharmony_ci		hw_dbg("Advertise 100mb Half duplex\n");
9798c2ecf20Sopenharmony_ci		mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
9808c2ecf20Sopenharmony_ci	}
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_ci	/* Do we want to advertise 100 Mb Full Duplex? */
9838c2ecf20Sopenharmony_ci	if (phy->autoneg_advertised & ADVERTISE_100_FULL) {
9848c2ecf20Sopenharmony_ci		hw_dbg("Advertise 100mb Full duplex\n");
9858c2ecf20Sopenharmony_ci		mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
9868c2ecf20Sopenharmony_ci	}
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci	/* We do not allow the Phy to advertise 1000 Mb Half Duplex */
9898c2ecf20Sopenharmony_ci	if (phy->autoneg_advertised & ADVERTISE_1000_HALF)
9908c2ecf20Sopenharmony_ci		hw_dbg("Advertise 1000mb Half duplex request denied!\n");
9918c2ecf20Sopenharmony_ci
9928c2ecf20Sopenharmony_ci	/* Do we want to advertise 1000 Mb Full Duplex? */
9938c2ecf20Sopenharmony_ci	if (phy->autoneg_advertised & ADVERTISE_1000_FULL) {
9948c2ecf20Sopenharmony_ci		hw_dbg("Advertise 1000mb Full duplex\n");
9958c2ecf20Sopenharmony_ci		mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
9968c2ecf20Sopenharmony_ci	}
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci	/* Check for a software override of the flow control settings, and
9998c2ecf20Sopenharmony_ci	 * setup the PHY advertisement registers accordingly.  If
10008c2ecf20Sopenharmony_ci	 * auto-negotiation is enabled, then software will have to set the
10018c2ecf20Sopenharmony_ci	 * "PAUSE" bits to the correct value in the Auto-Negotiation
10028c2ecf20Sopenharmony_ci	 * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-
10038c2ecf20Sopenharmony_ci	 * negotiation.
10048c2ecf20Sopenharmony_ci	 *
10058c2ecf20Sopenharmony_ci	 * The possible values of the "fc" parameter are:
10068c2ecf20Sopenharmony_ci	 *      0:  Flow control is completely disabled
10078c2ecf20Sopenharmony_ci	 *      1:  Rx flow control is enabled (we can receive pause frames
10088c2ecf20Sopenharmony_ci	 *          but not send pause frames).
10098c2ecf20Sopenharmony_ci	 *      2:  Tx flow control is enabled (we can send pause frames
10108c2ecf20Sopenharmony_ci	 *          but we do not support receiving pause frames).
10118c2ecf20Sopenharmony_ci	 *      3:  Both Rx and TX flow control (symmetric) are enabled.
10128c2ecf20Sopenharmony_ci	 *  other:  No software override.  The flow control configuration
10138c2ecf20Sopenharmony_ci	 *          in the EEPROM is used.
10148c2ecf20Sopenharmony_ci	 */
10158c2ecf20Sopenharmony_ci	switch (hw->fc.current_mode) {
10168c2ecf20Sopenharmony_ci	case e1000_fc_none:
10178c2ecf20Sopenharmony_ci		/* Flow control (RX & TX) is completely disabled by a
10188c2ecf20Sopenharmony_ci		 * software over-ride.
10198c2ecf20Sopenharmony_ci		 */
10208c2ecf20Sopenharmony_ci		mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
10218c2ecf20Sopenharmony_ci		break;
10228c2ecf20Sopenharmony_ci	case e1000_fc_rx_pause:
10238c2ecf20Sopenharmony_ci		/* RX Flow control is enabled, and TX Flow control is
10248c2ecf20Sopenharmony_ci		 * disabled, by a software over-ride.
10258c2ecf20Sopenharmony_ci		 *
10268c2ecf20Sopenharmony_ci		 * Since there really isn't a way to advertise that we are
10278c2ecf20Sopenharmony_ci		 * capable of RX Pause ONLY, we will advertise that we
10288c2ecf20Sopenharmony_ci		 * support both symmetric and asymmetric RX PAUSE.  Later
10298c2ecf20Sopenharmony_ci		 * (in e1000_config_fc_after_link_up) we will disable the
10308c2ecf20Sopenharmony_ci		 * hw's ability to send PAUSE frames.
10318c2ecf20Sopenharmony_ci		 */
10328c2ecf20Sopenharmony_ci		mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
10338c2ecf20Sopenharmony_ci		break;
10348c2ecf20Sopenharmony_ci	case e1000_fc_tx_pause:
10358c2ecf20Sopenharmony_ci		/* TX Flow control is enabled, and RX Flow control is
10368c2ecf20Sopenharmony_ci		 * disabled, by a software over-ride.
10378c2ecf20Sopenharmony_ci		 */
10388c2ecf20Sopenharmony_ci		mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
10398c2ecf20Sopenharmony_ci		mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
10408c2ecf20Sopenharmony_ci		break;
10418c2ecf20Sopenharmony_ci	case e1000_fc_full:
10428c2ecf20Sopenharmony_ci		/* Flow control (both RX and TX) is enabled by a software
10438c2ecf20Sopenharmony_ci		 * over-ride.
10448c2ecf20Sopenharmony_ci		 */
10458c2ecf20Sopenharmony_ci		mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
10468c2ecf20Sopenharmony_ci		break;
10478c2ecf20Sopenharmony_ci	default:
10488c2ecf20Sopenharmony_ci		hw_dbg("Flow control param set incorrectly\n");
10498c2ecf20Sopenharmony_ci		ret_val = -E1000_ERR_CONFIG;
10508c2ecf20Sopenharmony_ci		goto out;
10518c2ecf20Sopenharmony_ci	}
10528c2ecf20Sopenharmony_ci
10538c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
10548c2ecf20Sopenharmony_ci	if (ret_val)
10558c2ecf20Sopenharmony_ci		goto out;
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci	hw_dbg("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_ci	if (phy->autoneg_mask & ADVERTISE_1000_FULL) {
10608c2ecf20Sopenharmony_ci		ret_val = phy->ops.write_reg(hw,
10618c2ecf20Sopenharmony_ci					     PHY_1000T_CTRL,
10628c2ecf20Sopenharmony_ci					     mii_1000t_ctrl_reg);
10638c2ecf20Sopenharmony_ci		if (ret_val)
10648c2ecf20Sopenharmony_ci			goto out;
10658c2ecf20Sopenharmony_ci	}
10668c2ecf20Sopenharmony_ci
10678c2ecf20Sopenharmony_ciout:
10688c2ecf20Sopenharmony_ci	return ret_val;
10698c2ecf20Sopenharmony_ci}
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_ci/**
10728c2ecf20Sopenharmony_ci *  igb_setup_copper_link - Configure copper link settings
10738c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
10748c2ecf20Sopenharmony_ci *
10758c2ecf20Sopenharmony_ci *  Calls the appropriate function to configure the link for auto-neg or forced
10768c2ecf20Sopenharmony_ci *  speed and duplex.  Then we check for link, once link is established calls
10778c2ecf20Sopenharmony_ci *  to configure collision distance and flow control are called.  If link is
10788c2ecf20Sopenharmony_ci *  not established, we return -E1000_ERR_PHY (-2).
10798c2ecf20Sopenharmony_ci **/
10808c2ecf20Sopenharmony_cis32 igb_setup_copper_link(struct e1000_hw *hw)
10818c2ecf20Sopenharmony_ci{
10828c2ecf20Sopenharmony_ci	s32 ret_val;
10838c2ecf20Sopenharmony_ci	bool link;
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_ci	if (hw->mac.autoneg) {
10868c2ecf20Sopenharmony_ci		/* Setup autoneg and flow control advertisement and perform
10878c2ecf20Sopenharmony_ci		 * autonegotiation.
10888c2ecf20Sopenharmony_ci		 */
10898c2ecf20Sopenharmony_ci		ret_val = igb_copper_link_autoneg(hw);
10908c2ecf20Sopenharmony_ci		if (ret_val)
10918c2ecf20Sopenharmony_ci			goto out;
10928c2ecf20Sopenharmony_ci	} else {
10938c2ecf20Sopenharmony_ci		/* PHY will be set to 10H, 10F, 100H or 100F
10948c2ecf20Sopenharmony_ci		 * depending on user settings.
10958c2ecf20Sopenharmony_ci		 */
10968c2ecf20Sopenharmony_ci		hw_dbg("Forcing Speed and Duplex\n");
10978c2ecf20Sopenharmony_ci		ret_val = hw->phy.ops.force_speed_duplex(hw);
10988c2ecf20Sopenharmony_ci		if (ret_val) {
10998c2ecf20Sopenharmony_ci			hw_dbg("Error Forcing Speed and Duplex\n");
11008c2ecf20Sopenharmony_ci			goto out;
11018c2ecf20Sopenharmony_ci		}
11028c2ecf20Sopenharmony_ci	}
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_ci	/* Check link status. Wait up to 100 microseconds for link to become
11058c2ecf20Sopenharmony_ci	 * valid.
11068c2ecf20Sopenharmony_ci	 */
11078c2ecf20Sopenharmony_ci	ret_val = igb_phy_has_link(hw, COPPER_LINK_UP_LIMIT, 10, &link);
11088c2ecf20Sopenharmony_ci	if (ret_val)
11098c2ecf20Sopenharmony_ci		goto out;
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci	if (link) {
11128c2ecf20Sopenharmony_ci		hw_dbg("Valid link established!!!\n");
11138c2ecf20Sopenharmony_ci		igb_config_collision_dist(hw);
11148c2ecf20Sopenharmony_ci		ret_val = igb_config_fc_after_link_up(hw);
11158c2ecf20Sopenharmony_ci	} else {
11168c2ecf20Sopenharmony_ci		hw_dbg("Unable to establish link!!!\n");
11178c2ecf20Sopenharmony_ci	}
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_ciout:
11208c2ecf20Sopenharmony_ci	return ret_val;
11218c2ecf20Sopenharmony_ci}
11228c2ecf20Sopenharmony_ci
11238c2ecf20Sopenharmony_ci/**
11248c2ecf20Sopenharmony_ci *  igb_phy_force_speed_duplex_igp - Force speed/duplex for igp PHY
11258c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
11268c2ecf20Sopenharmony_ci *
11278c2ecf20Sopenharmony_ci *  Calls the PHY setup function to force speed and duplex.  Clears the
11288c2ecf20Sopenharmony_ci *  auto-crossover to force MDI manually.  Waits for link and returns
11298c2ecf20Sopenharmony_ci *  successful if link up is successful, else -E1000_ERR_PHY (-2).
11308c2ecf20Sopenharmony_ci **/
11318c2ecf20Sopenharmony_cis32 igb_phy_force_speed_duplex_igp(struct e1000_hw *hw)
11328c2ecf20Sopenharmony_ci{
11338c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
11348c2ecf20Sopenharmony_ci	s32 ret_val;
11358c2ecf20Sopenharmony_ci	u16 phy_data;
11368c2ecf20Sopenharmony_ci	bool link;
11378c2ecf20Sopenharmony_ci
11388c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_data);
11398c2ecf20Sopenharmony_ci	if (ret_val)
11408c2ecf20Sopenharmony_ci		goto out;
11418c2ecf20Sopenharmony_ci
11428c2ecf20Sopenharmony_ci	igb_phy_force_speed_duplex_setup(hw, &phy_data);
11438c2ecf20Sopenharmony_ci
11448c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_data);
11458c2ecf20Sopenharmony_ci	if (ret_val)
11468c2ecf20Sopenharmony_ci		goto out;
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_ci	/* Clear Auto-Crossover to force MDI manually.  IGP requires MDI
11498c2ecf20Sopenharmony_ci	 * forced whenever speed and duplex are forced.
11508c2ecf20Sopenharmony_ci	 */
11518c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
11528c2ecf20Sopenharmony_ci	if (ret_val)
11538c2ecf20Sopenharmony_ci		goto out;
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_ci	phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
11568c2ecf20Sopenharmony_ci	phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
11578c2ecf20Sopenharmony_ci
11588c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
11598c2ecf20Sopenharmony_ci	if (ret_val)
11608c2ecf20Sopenharmony_ci		goto out;
11618c2ecf20Sopenharmony_ci
11628c2ecf20Sopenharmony_ci	hw_dbg("IGP PSCR: %X\n", phy_data);
11638c2ecf20Sopenharmony_ci
11648c2ecf20Sopenharmony_ci	udelay(1);
11658c2ecf20Sopenharmony_ci
11668c2ecf20Sopenharmony_ci	if (phy->autoneg_wait_to_complete) {
11678c2ecf20Sopenharmony_ci		hw_dbg("Waiting for forced speed/duplex link on IGP phy.\n");
11688c2ecf20Sopenharmony_ci
11698c2ecf20Sopenharmony_ci		ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT, 10000, &link);
11708c2ecf20Sopenharmony_ci		if (ret_val)
11718c2ecf20Sopenharmony_ci			goto out;
11728c2ecf20Sopenharmony_ci
11738c2ecf20Sopenharmony_ci		if (!link)
11748c2ecf20Sopenharmony_ci			hw_dbg("Link taking longer than expected.\n");
11758c2ecf20Sopenharmony_ci
11768c2ecf20Sopenharmony_ci		/* Try once more */
11778c2ecf20Sopenharmony_ci		ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT, 10000, &link);
11788c2ecf20Sopenharmony_ci		if (ret_val)
11798c2ecf20Sopenharmony_ci			goto out;
11808c2ecf20Sopenharmony_ci	}
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_ciout:
11838c2ecf20Sopenharmony_ci	return ret_val;
11848c2ecf20Sopenharmony_ci}
11858c2ecf20Sopenharmony_ci
11868c2ecf20Sopenharmony_ci/**
11878c2ecf20Sopenharmony_ci *  igb_phy_force_speed_duplex_m88 - Force speed/duplex for m88 PHY
11888c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
11898c2ecf20Sopenharmony_ci *
11908c2ecf20Sopenharmony_ci *  Calls the PHY setup function to force speed and duplex.  Clears the
11918c2ecf20Sopenharmony_ci *  auto-crossover to force MDI manually.  Resets the PHY to commit the
11928c2ecf20Sopenharmony_ci *  changes.  If time expires while waiting for link up, we reset the DSP.
11938c2ecf20Sopenharmony_ci *  After reset, TX_CLK and CRS on TX must be set.  Return successful upon
11948c2ecf20Sopenharmony_ci *  successful completion, else return corresponding error code.
11958c2ecf20Sopenharmony_ci **/
11968c2ecf20Sopenharmony_cis32 igb_phy_force_speed_duplex_m88(struct e1000_hw *hw)
11978c2ecf20Sopenharmony_ci{
11988c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
11998c2ecf20Sopenharmony_ci	s32 ret_val;
12008c2ecf20Sopenharmony_ci	u16 phy_data;
12018c2ecf20Sopenharmony_ci	bool link;
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_ci	/* I210 and I211 devices support Auto-Crossover in forced operation. */
12048c2ecf20Sopenharmony_ci	if (phy->type != e1000_phy_i210) {
12058c2ecf20Sopenharmony_ci		/* Clear Auto-Crossover to force MDI manually.  M88E1000
12068c2ecf20Sopenharmony_ci		 * requires MDI forced whenever speed and duplex are forced.
12078c2ecf20Sopenharmony_ci		 */
12088c2ecf20Sopenharmony_ci		ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL,
12098c2ecf20Sopenharmony_ci					    &phy_data);
12108c2ecf20Sopenharmony_ci		if (ret_val)
12118c2ecf20Sopenharmony_ci			goto out;
12128c2ecf20Sopenharmony_ci
12138c2ecf20Sopenharmony_ci		phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
12148c2ecf20Sopenharmony_ci		ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL,
12158c2ecf20Sopenharmony_ci					     phy_data);
12168c2ecf20Sopenharmony_ci		if (ret_val)
12178c2ecf20Sopenharmony_ci			goto out;
12188c2ecf20Sopenharmony_ci
12198c2ecf20Sopenharmony_ci		hw_dbg("M88E1000 PSCR: %X\n", phy_data);
12208c2ecf20Sopenharmony_ci	}
12218c2ecf20Sopenharmony_ci
12228c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_data);
12238c2ecf20Sopenharmony_ci	if (ret_val)
12248c2ecf20Sopenharmony_ci		goto out;
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_ci	igb_phy_force_speed_duplex_setup(hw, &phy_data);
12278c2ecf20Sopenharmony_ci
12288c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_data);
12298c2ecf20Sopenharmony_ci	if (ret_val)
12308c2ecf20Sopenharmony_ci		goto out;
12318c2ecf20Sopenharmony_ci
12328c2ecf20Sopenharmony_ci	/* Reset the phy to commit changes. */
12338c2ecf20Sopenharmony_ci	ret_val = igb_phy_sw_reset(hw);
12348c2ecf20Sopenharmony_ci	if (ret_val)
12358c2ecf20Sopenharmony_ci		goto out;
12368c2ecf20Sopenharmony_ci
12378c2ecf20Sopenharmony_ci	if (phy->autoneg_wait_to_complete) {
12388c2ecf20Sopenharmony_ci		hw_dbg("Waiting for forced speed/duplex link on M88 phy.\n");
12398c2ecf20Sopenharmony_ci
12408c2ecf20Sopenharmony_ci		ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT, 100000, &link);
12418c2ecf20Sopenharmony_ci		if (ret_val)
12428c2ecf20Sopenharmony_ci			goto out;
12438c2ecf20Sopenharmony_ci
12448c2ecf20Sopenharmony_ci		if (!link) {
12458c2ecf20Sopenharmony_ci			bool reset_dsp = true;
12468c2ecf20Sopenharmony_ci
12478c2ecf20Sopenharmony_ci			switch (hw->phy.id) {
12488c2ecf20Sopenharmony_ci			case I347AT4_E_PHY_ID:
12498c2ecf20Sopenharmony_ci			case M88E1112_E_PHY_ID:
12508c2ecf20Sopenharmony_ci			case M88E1543_E_PHY_ID:
12518c2ecf20Sopenharmony_ci			case M88E1512_E_PHY_ID:
12528c2ecf20Sopenharmony_ci			case I210_I_PHY_ID:
12538c2ecf20Sopenharmony_ci				reset_dsp = false;
12548c2ecf20Sopenharmony_ci				break;
12558c2ecf20Sopenharmony_ci			default:
12568c2ecf20Sopenharmony_ci				if (hw->phy.type != e1000_phy_m88)
12578c2ecf20Sopenharmony_ci					reset_dsp = false;
12588c2ecf20Sopenharmony_ci				break;
12598c2ecf20Sopenharmony_ci			}
12608c2ecf20Sopenharmony_ci			if (!reset_dsp) {
12618c2ecf20Sopenharmony_ci				hw_dbg("Link taking longer than expected.\n");
12628c2ecf20Sopenharmony_ci			} else {
12638c2ecf20Sopenharmony_ci				/* We didn't get link.
12648c2ecf20Sopenharmony_ci				 * Reset the DSP and cross our fingers.
12658c2ecf20Sopenharmony_ci				 */
12668c2ecf20Sopenharmony_ci				ret_val = phy->ops.write_reg(hw,
12678c2ecf20Sopenharmony_ci						M88E1000_PHY_PAGE_SELECT,
12688c2ecf20Sopenharmony_ci						0x001d);
12698c2ecf20Sopenharmony_ci				if (ret_val)
12708c2ecf20Sopenharmony_ci					goto out;
12718c2ecf20Sopenharmony_ci				ret_val = igb_phy_reset_dsp(hw);
12728c2ecf20Sopenharmony_ci				if (ret_val)
12738c2ecf20Sopenharmony_ci					goto out;
12748c2ecf20Sopenharmony_ci			}
12758c2ecf20Sopenharmony_ci		}
12768c2ecf20Sopenharmony_ci
12778c2ecf20Sopenharmony_ci		/* Try once more */
12788c2ecf20Sopenharmony_ci		ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT,
12798c2ecf20Sopenharmony_ci					   100000, &link);
12808c2ecf20Sopenharmony_ci		if (ret_val)
12818c2ecf20Sopenharmony_ci			goto out;
12828c2ecf20Sopenharmony_ci	}
12838c2ecf20Sopenharmony_ci
12848c2ecf20Sopenharmony_ci	if (hw->phy.type != e1000_phy_m88 ||
12858c2ecf20Sopenharmony_ci	    hw->phy.id == I347AT4_E_PHY_ID ||
12868c2ecf20Sopenharmony_ci	    hw->phy.id == M88E1112_E_PHY_ID ||
12878c2ecf20Sopenharmony_ci	    hw->phy.id == M88E1543_E_PHY_ID ||
12888c2ecf20Sopenharmony_ci	    hw->phy.id == M88E1512_E_PHY_ID ||
12898c2ecf20Sopenharmony_ci	    hw->phy.id == I210_I_PHY_ID)
12908c2ecf20Sopenharmony_ci		goto out;
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
12938c2ecf20Sopenharmony_ci	if (ret_val)
12948c2ecf20Sopenharmony_ci		goto out;
12958c2ecf20Sopenharmony_ci
12968c2ecf20Sopenharmony_ci	/* Resetting the phy means we need to re-force TX_CLK in the
12978c2ecf20Sopenharmony_ci	 * Extended PHY Specific Control Register to 25MHz clock from
12988c2ecf20Sopenharmony_ci	 * the reset value of 2.5MHz.
12998c2ecf20Sopenharmony_ci	 */
13008c2ecf20Sopenharmony_ci	phy_data |= M88E1000_EPSCR_TX_CLK_25;
13018c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
13028c2ecf20Sopenharmony_ci	if (ret_val)
13038c2ecf20Sopenharmony_ci		goto out;
13048c2ecf20Sopenharmony_ci
13058c2ecf20Sopenharmony_ci	/* In addition, we must re-enable CRS on Tx for both half and full
13068c2ecf20Sopenharmony_ci	 * duplex.
13078c2ecf20Sopenharmony_ci	 */
13088c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
13098c2ecf20Sopenharmony_ci	if (ret_val)
13108c2ecf20Sopenharmony_ci		goto out;
13118c2ecf20Sopenharmony_ci
13128c2ecf20Sopenharmony_ci	phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
13138c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
13148c2ecf20Sopenharmony_ci
13158c2ecf20Sopenharmony_ciout:
13168c2ecf20Sopenharmony_ci	return ret_val;
13178c2ecf20Sopenharmony_ci}
13188c2ecf20Sopenharmony_ci
13198c2ecf20Sopenharmony_ci/**
13208c2ecf20Sopenharmony_ci *  igb_phy_force_speed_duplex_setup - Configure forced PHY speed/duplex
13218c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
13228c2ecf20Sopenharmony_ci *  @phy_ctrl: pointer to current value of PHY_CONTROL
13238c2ecf20Sopenharmony_ci *
13248c2ecf20Sopenharmony_ci *  Forces speed and duplex on the PHY by doing the following: disable flow
13258c2ecf20Sopenharmony_ci *  control, force speed/duplex on the MAC, disable auto speed detection,
13268c2ecf20Sopenharmony_ci *  disable auto-negotiation, configure duplex, configure speed, configure
13278c2ecf20Sopenharmony_ci *  the collision distance, write configuration to CTRL register.  The
13288c2ecf20Sopenharmony_ci *  caller must write to the PHY_CONTROL register for these settings to
13298c2ecf20Sopenharmony_ci *  take affect.
13308c2ecf20Sopenharmony_ci **/
13318c2ecf20Sopenharmony_cistatic void igb_phy_force_speed_duplex_setup(struct e1000_hw *hw,
13328c2ecf20Sopenharmony_ci					     u16 *phy_ctrl)
13338c2ecf20Sopenharmony_ci{
13348c2ecf20Sopenharmony_ci	struct e1000_mac_info *mac = &hw->mac;
13358c2ecf20Sopenharmony_ci	u32 ctrl;
13368c2ecf20Sopenharmony_ci
13378c2ecf20Sopenharmony_ci	/* Turn off flow control when forcing speed/duplex */
13388c2ecf20Sopenharmony_ci	hw->fc.current_mode = e1000_fc_none;
13398c2ecf20Sopenharmony_ci
13408c2ecf20Sopenharmony_ci	/* Force speed/duplex on the mac */
13418c2ecf20Sopenharmony_ci	ctrl = rd32(E1000_CTRL);
13428c2ecf20Sopenharmony_ci	ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
13438c2ecf20Sopenharmony_ci	ctrl &= ~E1000_CTRL_SPD_SEL;
13448c2ecf20Sopenharmony_ci
13458c2ecf20Sopenharmony_ci	/* Disable Auto Speed Detection */
13468c2ecf20Sopenharmony_ci	ctrl &= ~E1000_CTRL_ASDE;
13478c2ecf20Sopenharmony_ci
13488c2ecf20Sopenharmony_ci	/* Disable autoneg on the phy */
13498c2ecf20Sopenharmony_ci	*phy_ctrl &= ~MII_CR_AUTO_NEG_EN;
13508c2ecf20Sopenharmony_ci
13518c2ecf20Sopenharmony_ci	/* Forcing Full or Half Duplex? */
13528c2ecf20Sopenharmony_ci	if (mac->forced_speed_duplex & E1000_ALL_HALF_DUPLEX) {
13538c2ecf20Sopenharmony_ci		ctrl &= ~E1000_CTRL_FD;
13548c2ecf20Sopenharmony_ci		*phy_ctrl &= ~MII_CR_FULL_DUPLEX;
13558c2ecf20Sopenharmony_ci		hw_dbg("Half Duplex\n");
13568c2ecf20Sopenharmony_ci	} else {
13578c2ecf20Sopenharmony_ci		ctrl |= E1000_CTRL_FD;
13588c2ecf20Sopenharmony_ci		*phy_ctrl |= MII_CR_FULL_DUPLEX;
13598c2ecf20Sopenharmony_ci		hw_dbg("Full Duplex\n");
13608c2ecf20Sopenharmony_ci	}
13618c2ecf20Sopenharmony_ci
13628c2ecf20Sopenharmony_ci	/* Forcing 10mb or 100mb? */
13638c2ecf20Sopenharmony_ci	if (mac->forced_speed_duplex & E1000_ALL_100_SPEED) {
13648c2ecf20Sopenharmony_ci		ctrl |= E1000_CTRL_SPD_100;
13658c2ecf20Sopenharmony_ci		*phy_ctrl |= MII_CR_SPEED_100;
13668c2ecf20Sopenharmony_ci		*phy_ctrl &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_10);
13678c2ecf20Sopenharmony_ci		hw_dbg("Forcing 100mb\n");
13688c2ecf20Sopenharmony_ci	} else {
13698c2ecf20Sopenharmony_ci		ctrl &= ~(E1000_CTRL_SPD_1000 | E1000_CTRL_SPD_100);
13708c2ecf20Sopenharmony_ci		*phy_ctrl |= MII_CR_SPEED_10;
13718c2ecf20Sopenharmony_ci		*phy_ctrl &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_100);
13728c2ecf20Sopenharmony_ci		hw_dbg("Forcing 10mb\n");
13738c2ecf20Sopenharmony_ci	}
13748c2ecf20Sopenharmony_ci
13758c2ecf20Sopenharmony_ci	igb_config_collision_dist(hw);
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_ci	wr32(E1000_CTRL, ctrl);
13788c2ecf20Sopenharmony_ci}
13798c2ecf20Sopenharmony_ci
13808c2ecf20Sopenharmony_ci/**
13818c2ecf20Sopenharmony_ci *  igb_set_d3_lplu_state - Sets low power link up state for D3
13828c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
13838c2ecf20Sopenharmony_ci *  @active: boolean used to enable/disable lplu
13848c2ecf20Sopenharmony_ci *
13858c2ecf20Sopenharmony_ci *  Success returns 0, Failure returns 1
13868c2ecf20Sopenharmony_ci *
13878c2ecf20Sopenharmony_ci *  The low power link up (lplu) state is set to the power management level D3
13888c2ecf20Sopenharmony_ci *  and SmartSpeed is disabled when active is true, else clear lplu for D3
13898c2ecf20Sopenharmony_ci *  and enable Smartspeed.  LPLU and Smartspeed are mutually exclusive.  LPLU
13908c2ecf20Sopenharmony_ci *  is used during Dx states where the power conservation is most important.
13918c2ecf20Sopenharmony_ci *  During driver activity, SmartSpeed should be enabled so performance is
13928c2ecf20Sopenharmony_ci *  maintained.
13938c2ecf20Sopenharmony_ci **/
13948c2ecf20Sopenharmony_cis32 igb_set_d3_lplu_state(struct e1000_hw *hw, bool active)
13958c2ecf20Sopenharmony_ci{
13968c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
13978c2ecf20Sopenharmony_ci	s32 ret_val = 0;
13988c2ecf20Sopenharmony_ci	u16 data;
13998c2ecf20Sopenharmony_ci
14008c2ecf20Sopenharmony_ci	if (!(hw->phy.ops.read_reg))
14018c2ecf20Sopenharmony_ci		goto out;
14028c2ecf20Sopenharmony_ci
14038c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, IGP02E1000_PHY_POWER_MGMT, &data);
14048c2ecf20Sopenharmony_ci	if (ret_val)
14058c2ecf20Sopenharmony_ci		goto out;
14068c2ecf20Sopenharmony_ci
14078c2ecf20Sopenharmony_ci	if (!active) {
14088c2ecf20Sopenharmony_ci		data &= ~IGP02E1000_PM_D3_LPLU;
14098c2ecf20Sopenharmony_ci		ret_val = phy->ops.write_reg(hw, IGP02E1000_PHY_POWER_MGMT,
14108c2ecf20Sopenharmony_ci					     data);
14118c2ecf20Sopenharmony_ci		if (ret_val)
14128c2ecf20Sopenharmony_ci			goto out;
14138c2ecf20Sopenharmony_ci		/* LPLU and SmartSpeed are mutually exclusive.  LPLU is used
14148c2ecf20Sopenharmony_ci		 * during Dx states where the power conservation is most
14158c2ecf20Sopenharmony_ci		 * important.  During driver activity we should enable
14168c2ecf20Sopenharmony_ci		 * SmartSpeed, so performance is maintained.
14178c2ecf20Sopenharmony_ci		 */
14188c2ecf20Sopenharmony_ci		if (phy->smart_speed == e1000_smart_speed_on) {
14198c2ecf20Sopenharmony_ci			ret_val = phy->ops.read_reg(hw,
14208c2ecf20Sopenharmony_ci						    IGP01E1000_PHY_PORT_CONFIG,
14218c2ecf20Sopenharmony_ci						    &data);
14228c2ecf20Sopenharmony_ci			if (ret_val)
14238c2ecf20Sopenharmony_ci				goto out;
14248c2ecf20Sopenharmony_ci
14258c2ecf20Sopenharmony_ci			data |= IGP01E1000_PSCFR_SMART_SPEED;
14268c2ecf20Sopenharmony_ci			ret_val = phy->ops.write_reg(hw,
14278c2ecf20Sopenharmony_ci						     IGP01E1000_PHY_PORT_CONFIG,
14288c2ecf20Sopenharmony_ci						     data);
14298c2ecf20Sopenharmony_ci			if (ret_val)
14308c2ecf20Sopenharmony_ci				goto out;
14318c2ecf20Sopenharmony_ci		} else if (phy->smart_speed == e1000_smart_speed_off) {
14328c2ecf20Sopenharmony_ci			ret_val = phy->ops.read_reg(hw,
14338c2ecf20Sopenharmony_ci						     IGP01E1000_PHY_PORT_CONFIG,
14348c2ecf20Sopenharmony_ci						     &data);
14358c2ecf20Sopenharmony_ci			if (ret_val)
14368c2ecf20Sopenharmony_ci				goto out;
14378c2ecf20Sopenharmony_ci
14388c2ecf20Sopenharmony_ci			data &= ~IGP01E1000_PSCFR_SMART_SPEED;
14398c2ecf20Sopenharmony_ci			ret_val = phy->ops.write_reg(hw,
14408c2ecf20Sopenharmony_ci						     IGP01E1000_PHY_PORT_CONFIG,
14418c2ecf20Sopenharmony_ci						     data);
14428c2ecf20Sopenharmony_ci			if (ret_val)
14438c2ecf20Sopenharmony_ci				goto out;
14448c2ecf20Sopenharmony_ci		}
14458c2ecf20Sopenharmony_ci	} else if ((phy->autoneg_advertised == E1000_ALL_SPEED_DUPLEX) ||
14468c2ecf20Sopenharmony_ci		   (phy->autoneg_advertised == E1000_ALL_NOT_GIG) ||
14478c2ecf20Sopenharmony_ci		   (phy->autoneg_advertised == E1000_ALL_10_SPEED)) {
14488c2ecf20Sopenharmony_ci		data |= IGP02E1000_PM_D3_LPLU;
14498c2ecf20Sopenharmony_ci		ret_val = phy->ops.write_reg(hw, IGP02E1000_PHY_POWER_MGMT,
14508c2ecf20Sopenharmony_ci					      data);
14518c2ecf20Sopenharmony_ci		if (ret_val)
14528c2ecf20Sopenharmony_ci			goto out;
14538c2ecf20Sopenharmony_ci
14548c2ecf20Sopenharmony_ci		/* When LPLU is enabled, we should disable SmartSpeed */
14558c2ecf20Sopenharmony_ci		ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
14568c2ecf20Sopenharmony_ci					    &data);
14578c2ecf20Sopenharmony_ci		if (ret_val)
14588c2ecf20Sopenharmony_ci			goto out;
14598c2ecf20Sopenharmony_ci
14608c2ecf20Sopenharmony_ci		data &= ~IGP01E1000_PSCFR_SMART_SPEED;
14618c2ecf20Sopenharmony_ci		ret_val = phy->ops.write_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
14628c2ecf20Sopenharmony_ci					     data);
14638c2ecf20Sopenharmony_ci	}
14648c2ecf20Sopenharmony_ci
14658c2ecf20Sopenharmony_ciout:
14668c2ecf20Sopenharmony_ci	return ret_val;
14678c2ecf20Sopenharmony_ci}
14688c2ecf20Sopenharmony_ci
14698c2ecf20Sopenharmony_ci/**
14708c2ecf20Sopenharmony_ci *  igb_check_downshift - Checks whether a downshift in speed occurred
14718c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
14728c2ecf20Sopenharmony_ci *
14738c2ecf20Sopenharmony_ci *  Success returns 0, Failure returns 1
14748c2ecf20Sopenharmony_ci *
14758c2ecf20Sopenharmony_ci *  A downshift is detected by querying the PHY link health.
14768c2ecf20Sopenharmony_ci **/
14778c2ecf20Sopenharmony_cis32 igb_check_downshift(struct e1000_hw *hw)
14788c2ecf20Sopenharmony_ci{
14798c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
14808c2ecf20Sopenharmony_ci	s32 ret_val;
14818c2ecf20Sopenharmony_ci	u16 phy_data, offset, mask;
14828c2ecf20Sopenharmony_ci
14838c2ecf20Sopenharmony_ci	switch (phy->type) {
14848c2ecf20Sopenharmony_ci	case e1000_phy_i210:
14858c2ecf20Sopenharmony_ci	case e1000_phy_m88:
14868c2ecf20Sopenharmony_ci	case e1000_phy_gg82563:
14878c2ecf20Sopenharmony_ci		offset	= M88E1000_PHY_SPEC_STATUS;
14888c2ecf20Sopenharmony_ci		mask	= M88E1000_PSSR_DOWNSHIFT;
14898c2ecf20Sopenharmony_ci		break;
14908c2ecf20Sopenharmony_ci	case e1000_phy_igp_2:
14918c2ecf20Sopenharmony_ci	case e1000_phy_igp:
14928c2ecf20Sopenharmony_ci	case e1000_phy_igp_3:
14938c2ecf20Sopenharmony_ci		offset	= IGP01E1000_PHY_LINK_HEALTH;
14948c2ecf20Sopenharmony_ci		mask	= IGP01E1000_PLHR_SS_DOWNGRADE;
14958c2ecf20Sopenharmony_ci		break;
14968c2ecf20Sopenharmony_ci	default:
14978c2ecf20Sopenharmony_ci		/* speed downshift not supported */
14988c2ecf20Sopenharmony_ci		phy->speed_downgraded = false;
14998c2ecf20Sopenharmony_ci		ret_val = 0;
15008c2ecf20Sopenharmony_ci		goto out;
15018c2ecf20Sopenharmony_ci	}
15028c2ecf20Sopenharmony_ci
15038c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, offset, &phy_data);
15048c2ecf20Sopenharmony_ci
15058c2ecf20Sopenharmony_ci	if (!ret_val)
15068c2ecf20Sopenharmony_ci		phy->speed_downgraded = (phy_data & mask) ? true : false;
15078c2ecf20Sopenharmony_ci
15088c2ecf20Sopenharmony_ciout:
15098c2ecf20Sopenharmony_ci	return ret_val;
15108c2ecf20Sopenharmony_ci}
15118c2ecf20Sopenharmony_ci
15128c2ecf20Sopenharmony_ci/**
15138c2ecf20Sopenharmony_ci *  igb_check_polarity_m88 - Checks the polarity.
15148c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
15158c2ecf20Sopenharmony_ci *
15168c2ecf20Sopenharmony_ci *  Success returns 0, Failure returns -E1000_ERR_PHY (-2)
15178c2ecf20Sopenharmony_ci *
15188c2ecf20Sopenharmony_ci *  Polarity is determined based on the PHY specific status register.
15198c2ecf20Sopenharmony_ci **/
15208c2ecf20Sopenharmony_cis32 igb_check_polarity_m88(struct e1000_hw *hw)
15218c2ecf20Sopenharmony_ci{
15228c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
15238c2ecf20Sopenharmony_ci	s32 ret_val;
15248c2ecf20Sopenharmony_ci	u16 data;
15258c2ecf20Sopenharmony_ci
15268c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_STATUS, &data);
15278c2ecf20Sopenharmony_ci
15288c2ecf20Sopenharmony_ci	if (!ret_val)
15298c2ecf20Sopenharmony_ci		phy->cable_polarity = (data & M88E1000_PSSR_REV_POLARITY)
15308c2ecf20Sopenharmony_ci				      ? e1000_rev_polarity_reversed
15318c2ecf20Sopenharmony_ci				      : e1000_rev_polarity_normal;
15328c2ecf20Sopenharmony_ci
15338c2ecf20Sopenharmony_ci	return ret_val;
15348c2ecf20Sopenharmony_ci}
15358c2ecf20Sopenharmony_ci
15368c2ecf20Sopenharmony_ci/**
15378c2ecf20Sopenharmony_ci *  igb_check_polarity_igp - Checks the polarity.
15388c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
15398c2ecf20Sopenharmony_ci *
15408c2ecf20Sopenharmony_ci *  Success returns 0, Failure returns -E1000_ERR_PHY (-2)
15418c2ecf20Sopenharmony_ci *
15428c2ecf20Sopenharmony_ci *  Polarity is determined based on the PHY port status register, and the
15438c2ecf20Sopenharmony_ci *  current speed (since there is no polarity at 100Mbps).
15448c2ecf20Sopenharmony_ci **/
15458c2ecf20Sopenharmony_cistatic s32 igb_check_polarity_igp(struct e1000_hw *hw)
15468c2ecf20Sopenharmony_ci{
15478c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
15488c2ecf20Sopenharmony_ci	s32 ret_val;
15498c2ecf20Sopenharmony_ci	u16 data, offset, mask;
15508c2ecf20Sopenharmony_ci
15518c2ecf20Sopenharmony_ci	/* Polarity is determined based on the speed of
15528c2ecf20Sopenharmony_ci	 * our connection.
15538c2ecf20Sopenharmony_ci	 */
15548c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_STATUS, &data);
15558c2ecf20Sopenharmony_ci	if (ret_val)
15568c2ecf20Sopenharmony_ci		goto out;
15578c2ecf20Sopenharmony_ci
15588c2ecf20Sopenharmony_ci	if ((data & IGP01E1000_PSSR_SPEED_MASK) ==
15598c2ecf20Sopenharmony_ci	    IGP01E1000_PSSR_SPEED_1000MBPS) {
15608c2ecf20Sopenharmony_ci		offset	= IGP01E1000_PHY_PCS_INIT_REG;
15618c2ecf20Sopenharmony_ci		mask	= IGP01E1000_PHY_POLARITY_MASK;
15628c2ecf20Sopenharmony_ci	} else {
15638c2ecf20Sopenharmony_ci		/* This really only applies to 10Mbps since
15648c2ecf20Sopenharmony_ci		 * there is no polarity for 100Mbps (always 0).
15658c2ecf20Sopenharmony_ci		 */
15668c2ecf20Sopenharmony_ci		offset	= IGP01E1000_PHY_PORT_STATUS;
15678c2ecf20Sopenharmony_ci		mask	= IGP01E1000_PSSR_POLARITY_REVERSED;
15688c2ecf20Sopenharmony_ci	}
15698c2ecf20Sopenharmony_ci
15708c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, offset, &data);
15718c2ecf20Sopenharmony_ci
15728c2ecf20Sopenharmony_ci	if (!ret_val)
15738c2ecf20Sopenharmony_ci		phy->cable_polarity = (data & mask)
15748c2ecf20Sopenharmony_ci				      ? e1000_rev_polarity_reversed
15758c2ecf20Sopenharmony_ci				      : e1000_rev_polarity_normal;
15768c2ecf20Sopenharmony_ci
15778c2ecf20Sopenharmony_ciout:
15788c2ecf20Sopenharmony_ci	return ret_val;
15798c2ecf20Sopenharmony_ci}
15808c2ecf20Sopenharmony_ci
15818c2ecf20Sopenharmony_ci/**
15828c2ecf20Sopenharmony_ci *  igb_wait_autoneg - Wait for auto-neg completion
15838c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
15848c2ecf20Sopenharmony_ci *
15858c2ecf20Sopenharmony_ci *  Waits for auto-negotiation to complete or for the auto-negotiation time
15868c2ecf20Sopenharmony_ci *  limit to expire, which ever happens first.
15878c2ecf20Sopenharmony_ci **/
15888c2ecf20Sopenharmony_cistatic s32 igb_wait_autoneg(struct e1000_hw *hw)
15898c2ecf20Sopenharmony_ci{
15908c2ecf20Sopenharmony_ci	s32 ret_val = 0;
15918c2ecf20Sopenharmony_ci	u16 i, phy_status;
15928c2ecf20Sopenharmony_ci
15938c2ecf20Sopenharmony_ci	/* Break after autoneg completes or PHY_AUTO_NEG_LIMIT expires. */
15948c2ecf20Sopenharmony_ci	for (i = PHY_AUTO_NEG_LIMIT; i > 0; i--) {
15958c2ecf20Sopenharmony_ci		ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
15968c2ecf20Sopenharmony_ci		if (ret_val)
15978c2ecf20Sopenharmony_ci			break;
15988c2ecf20Sopenharmony_ci		ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
15998c2ecf20Sopenharmony_ci		if (ret_val)
16008c2ecf20Sopenharmony_ci			break;
16018c2ecf20Sopenharmony_ci		if (phy_status & MII_SR_AUTONEG_COMPLETE)
16028c2ecf20Sopenharmony_ci			break;
16038c2ecf20Sopenharmony_ci		msleep(100);
16048c2ecf20Sopenharmony_ci	}
16058c2ecf20Sopenharmony_ci
16068c2ecf20Sopenharmony_ci	/* PHY_AUTO_NEG_TIME expiration doesn't guarantee auto-negotiation
16078c2ecf20Sopenharmony_ci	 * has completed.
16088c2ecf20Sopenharmony_ci	 */
16098c2ecf20Sopenharmony_ci	return ret_val;
16108c2ecf20Sopenharmony_ci}
16118c2ecf20Sopenharmony_ci
16128c2ecf20Sopenharmony_ci/**
16138c2ecf20Sopenharmony_ci *  igb_phy_has_link - Polls PHY for link
16148c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
16158c2ecf20Sopenharmony_ci *  @iterations: number of times to poll for link
16168c2ecf20Sopenharmony_ci *  @usec_interval: delay between polling attempts
16178c2ecf20Sopenharmony_ci *  @success: pointer to whether polling was successful or not
16188c2ecf20Sopenharmony_ci *
16198c2ecf20Sopenharmony_ci *  Polls the PHY status register for link, 'iterations' number of times.
16208c2ecf20Sopenharmony_ci **/
16218c2ecf20Sopenharmony_cis32 igb_phy_has_link(struct e1000_hw *hw, u32 iterations,
16228c2ecf20Sopenharmony_ci		     u32 usec_interval, bool *success)
16238c2ecf20Sopenharmony_ci{
16248c2ecf20Sopenharmony_ci	s32 ret_val = 0;
16258c2ecf20Sopenharmony_ci	u16 i, phy_status;
16268c2ecf20Sopenharmony_ci
16278c2ecf20Sopenharmony_ci	for (i = 0; i < iterations; i++) {
16288c2ecf20Sopenharmony_ci		/* Some PHYs require the PHY_STATUS register to be read
16298c2ecf20Sopenharmony_ci		 * twice due to the link bit being sticky.  No harm doing
16308c2ecf20Sopenharmony_ci		 * it across the board.
16318c2ecf20Sopenharmony_ci		 */
16328c2ecf20Sopenharmony_ci		ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
16338c2ecf20Sopenharmony_ci		if (ret_val && usec_interval > 0) {
16348c2ecf20Sopenharmony_ci			/* If the first read fails, another entity may have
16358c2ecf20Sopenharmony_ci			 * ownership of the resources, wait and try again to
16368c2ecf20Sopenharmony_ci			 * see if they have relinquished the resources yet.
16378c2ecf20Sopenharmony_ci			 */
16388c2ecf20Sopenharmony_ci			if (usec_interval >= 1000)
16398c2ecf20Sopenharmony_ci				mdelay(usec_interval/1000);
16408c2ecf20Sopenharmony_ci			else
16418c2ecf20Sopenharmony_ci				udelay(usec_interval);
16428c2ecf20Sopenharmony_ci		}
16438c2ecf20Sopenharmony_ci		ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
16448c2ecf20Sopenharmony_ci		if (ret_val)
16458c2ecf20Sopenharmony_ci			break;
16468c2ecf20Sopenharmony_ci		if (phy_status & MII_SR_LINK_STATUS)
16478c2ecf20Sopenharmony_ci			break;
16488c2ecf20Sopenharmony_ci		if (usec_interval >= 1000)
16498c2ecf20Sopenharmony_ci			mdelay(usec_interval/1000);
16508c2ecf20Sopenharmony_ci		else
16518c2ecf20Sopenharmony_ci			udelay(usec_interval);
16528c2ecf20Sopenharmony_ci	}
16538c2ecf20Sopenharmony_ci
16548c2ecf20Sopenharmony_ci	*success = (i < iterations) ? true : false;
16558c2ecf20Sopenharmony_ci
16568c2ecf20Sopenharmony_ci	return ret_val;
16578c2ecf20Sopenharmony_ci}
16588c2ecf20Sopenharmony_ci
16598c2ecf20Sopenharmony_ci/**
16608c2ecf20Sopenharmony_ci *  igb_get_cable_length_m88 - Determine cable length for m88 PHY
16618c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
16628c2ecf20Sopenharmony_ci *
16638c2ecf20Sopenharmony_ci *  Reads the PHY specific status register to retrieve the cable length
16648c2ecf20Sopenharmony_ci *  information.  The cable length is determined by averaging the minimum and
16658c2ecf20Sopenharmony_ci *  maximum values to get the "average" cable length.  The m88 PHY has four
16668c2ecf20Sopenharmony_ci *  possible cable length values, which are:
16678c2ecf20Sopenharmony_ci *	Register Value		Cable Length
16688c2ecf20Sopenharmony_ci *	0			< 50 meters
16698c2ecf20Sopenharmony_ci *	1			50 - 80 meters
16708c2ecf20Sopenharmony_ci *	2			80 - 110 meters
16718c2ecf20Sopenharmony_ci *	3			110 - 140 meters
16728c2ecf20Sopenharmony_ci *	4			> 140 meters
16738c2ecf20Sopenharmony_ci **/
16748c2ecf20Sopenharmony_cis32 igb_get_cable_length_m88(struct e1000_hw *hw)
16758c2ecf20Sopenharmony_ci{
16768c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
16778c2ecf20Sopenharmony_ci	s32 ret_val;
16788c2ecf20Sopenharmony_ci	u16 phy_data, index;
16798c2ecf20Sopenharmony_ci
16808c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
16818c2ecf20Sopenharmony_ci	if (ret_val)
16828c2ecf20Sopenharmony_ci		goto out;
16838c2ecf20Sopenharmony_ci
16848c2ecf20Sopenharmony_ci	index = (phy_data & M88E1000_PSSR_CABLE_LENGTH) >>
16858c2ecf20Sopenharmony_ci		M88E1000_PSSR_CABLE_LENGTH_SHIFT;
16868c2ecf20Sopenharmony_ci	if (index >= ARRAY_SIZE(e1000_m88_cable_length_table) - 1) {
16878c2ecf20Sopenharmony_ci		ret_val = -E1000_ERR_PHY;
16888c2ecf20Sopenharmony_ci		goto out;
16898c2ecf20Sopenharmony_ci	}
16908c2ecf20Sopenharmony_ci
16918c2ecf20Sopenharmony_ci	phy->min_cable_length = e1000_m88_cable_length_table[index];
16928c2ecf20Sopenharmony_ci	phy->max_cable_length = e1000_m88_cable_length_table[index + 1];
16938c2ecf20Sopenharmony_ci
16948c2ecf20Sopenharmony_ci	phy->cable_length = (phy->min_cable_length + phy->max_cable_length) / 2;
16958c2ecf20Sopenharmony_ci
16968c2ecf20Sopenharmony_ciout:
16978c2ecf20Sopenharmony_ci	return ret_val;
16988c2ecf20Sopenharmony_ci}
16998c2ecf20Sopenharmony_ci
17008c2ecf20Sopenharmony_cis32 igb_get_cable_length_m88_gen2(struct e1000_hw *hw)
17018c2ecf20Sopenharmony_ci{
17028c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
17038c2ecf20Sopenharmony_ci	s32 ret_val;
17048c2ecf20Sopenharmony_ci	u16 phy_data, phy_data2, index, default_page, is_cm;
17058c2ecf20Sopenharmony_ci	int len_tot = 0;
17068c2ecf20Sopenharmony_ci	u16 len_min;
17078c2ecf20Sopenharmony_ci	u16 len_max;
17088c2ecf20Sopenharmony_ci
17098c2ecf20Sopenharmony_ci	switch (hw->phy.id) {
17108c2ecf20Sopenharmony_ci	case M88E1543_E_PHY_ID:
17118c2ecf20Sopenharmony_ci	case M88E1512_E_PHY_ID:
17128c2ecf20Sopenharmony_ci	case I347AT4_E_PHY_ID:
17138c2ecf20Sopenharmony_ci	case I210_I_PHY_ID:
17148c2ecf20Sopenharmony_ci		/* Remember the original page select and set it to 7 */
17158c2ecf20Sopenharmony_ci		ret_val = phy->ops.read_reg(hw, I347AT4_PAGE_SELECT,
17168c2ecf20Sopenharmony_ci					    &default_page);
17178c2ecf20Sopenharmony_ci		if (ret_val)
17188c2ecf20Sopenharmony_ci			goto out;
17198c2ecf20Sopenharmony_ci
17208c2ecf20Sopenharmony_ci		ret_val = phy->ops.write_reg(hw, I347AT4_PAGE_SELECT, 0x07);
17218c2ecf20Sopenharmony_ci		if (ret_val)
17228c2ecf20Sopenharmony_ci			goto out;
17238c2ecf20Sopenharmony_ci
17248c2ecf20Sopenharmony_ci		/* Check if the unit of cable length is meters or cm */
17258c2ecf20Sopenharmony_ci		ret_val = phy->ops.read_reg(hw, I347AT4_PCDC, &phy_data2);
17268c2ecf20Sopenharmony_ci		if (ret_val)
17278c2ecf20Sopenharmony_ci			goto out;
17288c2ecf20Sopenharmony_ci
17298c2ecf20Sopenharmony_ci		is_cm = !(phy_data2 & I347AT4_PCDC_CABLE_LENGTH_UNIT);
17308c2ecf20Sopenharmony_ci
17318c2ecf20Sopenharmony_ci		/* Get cable length from Pair 0 length Regs */
17328c2ecf20Sopenharmony_ci		ret_val = phy->ops.read_reg(hw, I347AT4_PCDL0, &phy_data);
17338c2ecf20Sopenharmony_ci		if (ret_val)
17348c2ecf20Sopenharmony_ci			goto out;
17358c2ecf20Sopenharmony_ci
17368c2ecf20Sopenharmony_ci		phy->pair_length[0] = phy_data / (is_cm ? 100 : 1);
17378c2ecf20Sopenharmony_ci		len_tot = phy->pair_length[0];
17388c2ecf20Sopenharmony_ci		len_min = phy->pair_length[0];
17398c2ecf20Sopenharmony_ci		len_max = phy->pair_length[0];
17408c2ecf20Sopenharmony_ci
17418c2ecf20Sopenharmony_ci		/* Get cable length from Pair 1 length Regs */
17428c2ecf20Sopenharmony_ci		ret_val = phy->ops.read_reg(hw, I347AT4_PCDL1, &phy_data);
17438c2ecf20Sopenharmony_ci		if (ret_val)
17448c2ecf20Sopenharmony_ci			goto out;
17458c2ecf20Sopenharmony_ci
17468c2ecf20Sopenharmony_ci		phy->pair_length[1] = phy_data / (is_cm ? 100 : 1);
17478c2ecf20Sopenharmony_ci		len_tot += phy->pair_length[1];
17488c2ecf20Sopenharmony_ci		len_min = min(len_min, phy->pair_length[1]);
17498c2ecf20Sopenharmony_ci		len_max = max(len_max, phy->pair_length[1]);
17508c2ecf20Sopenharmony_ci
17518c2ecf20Sopenharmony_ci		/* Get cable length from Pair 2 length Regs */
17528c2ecf20Sopenharmony_ci		ret_val = phy->ops.read_reg(hw, I347AT4_PCDL2, &phy_data);
17538c2ecf20Sopenharmony_ci		if (ret_val)
17548c2ecf20Sopenharmony_ci			goto out;
17558c2ecf20Sopenharmony_ci
17568c2ecf20Sopenharmony_ci		phy->pair_length[2] = phy_data / (is_cm ? 100 : 1);
17578c2ecf20Sopenharmony_ci		len_tot += phy->pair_length[2];
17588c2ecf20Sopenharmony_ci		len_min = min(len_min, phy->pair_length[2]);
17598c2ecf20Sopenharmony_ci		len_max = max(len_max, phy->pair_length[2]);
17608c2ecf20Sopenharmony_ci
17618c2ecf20Sopenharmony_ci		/* Get cable length from Pair 3 length Regs */
17628c2ecf20Sopenharmony_ci		ret_val = phy->ops.read_reg(hw, I347AT4_PCDL3, &phy_data);
17638c2ecf20Sopenharmony_ci		if (ret_val)
17648c2ecf20Sopenharmony_ci			goto out;
17658c2ecf20Sopenharmony_ci
17668c2ecf20Sopenharmony_ci		phy->pair_length[3] = phy_data / (is_cm ? 100 : 1);
17678c2ecf20Sopenharmony_ci		len_tot += phy->pair_length[3];
17688c2ecf20Sopenharmony_ci		len_min = min(len_min, phy->pair_length[3]);
17698c2ecf20Sopenharmony_ci		len_max = max(len_max, phy->pair_length[3]);
17708c2ecf20Sopenharmony_ci
17718c2ecf20Sopenharmony_ci		/* Populate the phy structure with cable length in meters */
17728c2ecf20Sopenharmony_ci		phy->min_cable_length = len_min;
17738c2ecf20Sopenharmony_ci		phy->max_cable_length = len_max;
17748c2ecf20Sopenharmony_ci		phy->cable_length = len_tot / 4;
17758c2ecf20Sopenharmony_ci
17768c2ecf20Sopenharmony_ci		/* Reset the page selec to its original value */
17778c2ecf20Sopenharmony_ci		ret_val = phy->ops.write_reg(hw, I347AT4_PAGE_SELECT,
17788c2ecf20Sopenharmony_ci					     default_page);
17798c2ecf20Sopenharmony_ci		if (ret_val)
17808c2ecf20Sopenharmony_ci			goto out;
17818c2ecf20Sopenharmony_ci		break;
17828c2ecf20Sopenharmony_ci	case M88E1112_E_PHY_ID:
17838c2ecf20Sopenharmony_ci		/* Remember the original page select and set it to 5 */
17848c2ecf20Sopenharmony_ci		ret_val = phy->ops.read_reg(hw, I347AT4_PAGE_SELECT,
17858c2ecf20Sopenharmony_ci					    &default_page);
17868c2ecf20Sopenharmony_ci		if (ret_val)
17878c2ecf20Sopenharmony_ci			goto out;
17888c2ecf20Sopenharmony_ci
17898c2ecf20Sopenharmony_ci		ret_val = phy->ops.write_reg(hw, I347AT4_PAGE_SELECT, 0x05);
17908c2ecf20Sopenharmony_ci		if (ret_val)
17918c2ecf20Sopenharmony_ci			goto out;
17928c2ecf20Sopenharmony_ci
17938c2ecf20Sopenharmony_ci		ret_val = phy->ops.read_reg(hw, M88E1112_VCT_DSP_DISTANCE,
17948c2ecf20Sopenharmony_ci					    &phy_data);
17958c2ecf20Sopenharmony_ci		if (ret_val)
17968c2ecf20Sopenharmony_ci			goto out;
17978c2ecf20Sopenharmony_ci
17988c2ecf20Sopenharmony_ci		index = (phy_data & M88E1000_PSSR_CABLE_LENGTH) >>
17998c2ecf20Sopenharmony_ci			M88E1000_PSSR_CABLE_LENGTH_SHIFT;
18008c2ecf20Sopenharmony_ci		if (index >= ARRAY_SIZE(e1000_m88_cable_length_table) - 1) {
18018c2ecf20Sopenharmony_ci			ret_val = -E1000_ERR_PHY;
18028c2ecf20Sopenharmony_ci			goto out;
18038c2ecf20Sopenharmony_ci		}
18048c2ecf20Sopenharmony_ci
18058c2ecf20Sopenharmony_ci		phy->min_cable_length = e1000_m88_cable_length_table[index];
18068c2ecf20Sopenharmony_ci		phy->max_cable_length = e1000_m88_cable_length_table[index + 1];
18078c2ecf20Sopenharmony_ci
18088c2ecf20Sopenharmony_ci		phy->cable_length = (phy->min_cable_length +
18098c2ecf20Sopenharmony_ci				     phy->max_cable_length) / 2;
18108c2ecf20Sopenharmony_ci
18118c2ecf20Sopenharmony_ci		/* Reset the page select to its original value */
18128c2ecf20Sopenharmony_ci		ret_val = phy->ops.write_reg(hw, I347AT4_PAGE_SELECT,
18138c2ecf20Sopenharmony_ci					     default_page);
18148c2ecf20Sopenharmony_ci		if (ret_val)
18158c2ecf20Sopenharmony_ci			goto out;
18168c2ecf20Sopenharmony_ci
18178c2ecf20Sopenharmony_ci		break;
18188c2ecf20Sopenharmony_ci	default:
18198c2ecf20Sopenharmony_ci		ret_val = -E1000_ERR_PHY;
18208c2ecf20Sopenharmony_ci		goto out;
18218c2ecf20Sopenharmony_ci	}
18228c2ecf20Sopenharmony_ci
18238c2ecf20Sopenharmony_ciout:
18248c2ecf20Sopenharmony_ci	return ret_val;
18258c2ecf20Sopenharmony_ci}
18268c2ecf20Sopenharmony_ci
18278c2ecf20Sopenharmony_ci/**
18288c2ecf20Sopenharmony_ci *  igb_get_cable_length_igp_2 - Determine cable length for igp2 PHY
18298c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
18308c2ecf20Sopenharmony_ci *
18318c2ecf20Sopenharmony_ci *  The automatic gain control (agc) normalizes the amplitude of the
18328c2ecf20Sopenharmony_ci *  received signal, adjusting for the attenuation produced by the
18338c2ecf20Sopenharmony_ci *  cable.  By reading the AGC registers, which represent the
18348c2ecf20Sopenharmony_ci *  combination of coarse and fine gain value, the value can be put
18358c2ecf20Sopenharmony_ci *  into a lookup table to obtain the approximate cable length
18368c2ecf20Sopenharmony_ci *  for each channel.
18378c2ecf20Sopenharmony_ci **/
18388c2ecf20Sopenharmony_cis32 igb_get_cable_length_igp_2(struct e1000_hw *hw)
18398c2ecf20Sopenharmony_ci{
18408c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
18418c2ecf20Sopenharmony_ci	s32 ret_val = 0;
18428c2ecf20Sopenharmony_ci	u16 phy_data, i, agc_value = 0;
18438c2ecf20Sopenharmony_ci	u16 cur_agc_index, max_agc_index = 0;
18448c2ecf20Sopenharmony_ci	u16 min_agc_index = ARRAY_SIZE(e1000_igp_2_cable_length_table) - 1;
18458c2ecf20Sopenharmony_ci	static const u16 agc_reg_array[IGP02E1000_PHY_CHANNEL_NUM] = {
18468c2ecf20Sopenharmony_ci		IGP02E1000_PHY_AGC_A,
18478c2ecf20Sopenharmony_ci		IGP02E1000_PHY_AGC_B,
18488c2ecf20Sopenharmony_ci		IGP02E1000_PHY_AGC_C,
18498c2ecf20Sopenharmony_ci		IGP02E1000_PHY_AGC_D
18508c2ecf20Sopenharmony_ci	};
18518c2ecf20Sopenharmony_ci
18528c2ecf20Sopenharmony_ci	/* Read the AGC registers for all channels */
18538c2ecf20Sopenharmony_ci	for (i = 0; i < IGP02E1000_PHY_CHANNEL_NUM; i++) {
18548c2ecf20Sopenharmony_ci		ret_val = phy->ops.read_reg(hw, agc_reg_array[i], &phy_data);
18558c2ecf20Sopenharmony_ci		if (ret_val)
18568c2ecf20Sopenharmony_ci			goto out;
18578c2ecf20Sopenharmony_ci
18588c2ecf20Sopenharmony_ci		/* Getting bits 15:9, which represent the combination of
18598c2ecf20Sopenharmony_ci		 * coarse and fine gain values.  The result is a number
18608c2ecf20Sopenharmony_ci		 * that can be put into the lookup table to obtain the
18618c2ecf20Sopenharmony_ci		 * approximate cable length.
18628c2ecf20Sopenharmony_ci		 */
18638c2ecf20Sopenharmony_ci		cur_agc_index = (phy_data >> IGP02E1000_AGC_LENGTH_SHIFT) &
18648c2ecf20Sopenharmony_ci				IGP02E1000_AGC_LENGTH_MASK;
18658c2ecf20Sopenharmony_ci
18668c2ecf20Sopenharmony_ci		/* Array index bound check. */
18678c2ecf20Sopenharmony_ci		if ((cur_agc_index >= ARRAY_SIZE(e1000_igp_2_cable_length_table)) ||
18688c2ecf20Sopenharmony_ci		    (cur_agc_index == 0)) {
18698c2ecf20Sopenharmony_ci			ret_val = -E1000_ERR_PHY;
18708c2ecf20Sopenharmony_ci			goto out;
18718c2ecf20Sopenharmony_ci		}
18728c2ecf20Sopenharmony_ci
18738c2ecf20Sopenharmony_ci		/* Remove min & max AGC values from calculation. */
18748c2ecf20Sopenharmony_ci		if (e1000_igp_2_cable_length_table[min_agc_index] >
18758c2ecf20Sopenharmony_ci		    e1000_igp_2_cable_length_table[cur_agc_index])
18768c2ecf20Sopenharmony_ci			min_agc_index = cur_agc_index;
18778c2ecf20Sopenharmony_ci		if (e1000_igp_2_cable_length_table[max_agc_index] <
18788c2ecf20Sopenharmony_ci		    e1000_igp_2_cable_length_table[cur_agc_index])
18798c2ecf20Sopenharmony_ci			max_agc_index = cur_agc_index;
18808c2ecf20Sopenharmony_ci
18818c2ecf20Sopenharmony_ci		agc_value += e1000_igp_2_cable_length_table[cur_agc_index];
18828c2ecf20Sopenharmony_ci	}
18838c2ecf20Sopenharmony_ci
18848c2ecf20Sopenharmony_ci	agc_value -= (e1000_igp_2_cable_length_table[min_agc_index] +
18858c2ecf20Sopenharmony_ci		      e1000_igp_2_cable_length_table[max_agc_index]);
18868c2ecf20Sopenharmony_ci	agc_value /= (IGP02E1000_PHY_CHANNEL_NUM - 2);
18878c2ecf20Sopenharmony_ci
18888c2ecf20Sopenharmony_ci	/* Calculate cable length with the error range of +/- 10 meters. */
18898c2ecf20Sopenharmony_ci	phy->min_cable_length = ((agc_value - IGP02E1000_AGC_RANGE) > 0) ?
18908c2ecf20Sopenharmony_ci				 (agc_value - IGP02E1000_AGC_RANGE) : 0;
18918c2ecf20Sopenharmony_ci	phy->max_cable_length = agc_value + IGP02E1000_AGC_RANGE;
18928c2ecf20Sopenharmony_ci
18938c2ecf20Sopenharmony_ci	phy->cable_length = (phy->min_cable_length + phy->max_cable_length) / 2;
18948c2ecf20Sopenharmony_ci
18958c2ecf20Sopenharmony_ciout:
18968c2ecf20Sopenharmony_ci	return ret_val;
18978c2ecf20Sopenharmony_ci}
18988c2ecf20Sopenharmony_ci
18998c2ecf20Sopenharmony_ci/**
19008c2ecf20Sopenharmony_ci *  igb_get_phy_info_m88 - Retrieve PHY information
19018c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
19028c2ecf20Sopenharmony_ci *
19038c2ecf20Sopenharmony_ci *  Valid for only copper links.  Read the PHY status register (sticky read)
19048c2ecf20Sopenharmony_ci *  to verify that link is up.  Read the PHY special control register to
19058c2ecf20Sopenharmony_ci *  determine the polarity and 10base-T extended distance.  Read the PHY
19068c2ecf20Sopenharmony_ci *  special status register to determine MDI/MDIx and current speed.  If
19078c2ecf20Sopenharmony_ci *  speed is 1000, then determine cable length, local and remote receiver.
19088c2ecf20Sopenharmony_ci **/
19098c2ecf20Sopenharmony_cis32 igb_get_phy_info_m88(struct e1000_hw *hw)
19108c2ecf20Sopenharmony_ci{
19118c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
19128c2ecf20Sopenharmony_ci	s32  ret_val;
19138c2ecf20Sopenharmony_ci	u16 phy_data;
19148c2ecf20Sopenharmony_ci	bool link;
19158c2ecf20Sopenharmony_ci
19168c2ecf20Sopenharmony_ci	if (phy->media_type != e1000_media_type_copper) {
19178c2ecf20Sopenharmony_ci		hw_dbg("Phy info is only valid for copper media\n");
19188c2ecf20Sopenharmony_ci		ret_val = -E1000_ERR_CONFIG;
19198c2ecf20Sopenharmony_ci		goto out;
19208c2ecf20Sopenharmony_ci	}
19218c2ecf20Sopenharmony_ci
19228c2ecf20Sopenharmony_ci	ret_val = igb_phy_has_link(hw, 1, 0, &link);
19238c2ecf20Sopenharmony_ci	if (ret_val)
19248c2ecf20Sopenharmony_ci		goto out;
19258c2ecf20Sopenharmony_ci
19268c2ecf20Sopenharmony_ci	if (!link) {
19278c2ecf20Sopenharmony_ci		hw_dbg("Phy info is only valid if link is up\n");
19288c2ecf20Sopenharmony_ci		ret_val = -E1000_ERR_CONFIG;
19298c2ecf20Sopenharmony_ci		goto out;
19308c2ecf20Sopenharmony_ci	}
19318c2ecf20Sopenharmony_ci
19328c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
19338c2ecf20Sopenharmony_ci	if (ret_val)
19348c2ecf20Sopenharmony_ci		goto out;
19358c2ecf20Sopenharmony_ci
19368c2ecf20Sopenharmony_ci	phy->polarity_correction = (phy_data & M88E1000_PSCR_POLARITY_REVERSAL)
19378c2ecf20Sopenharmony_ci				   ? true : false;
19388c2ecf20Sopenharmony_ci
19398c2ecf20Sopenharmony_ci	ret_val = igb_check_polarity_m88(hw);
19408c2ecf20Sopenharmony_ci	if (ret_val)
19418c2ecf20Sopenharmony_ci		goto out;
19428c2ecf20Sopenharmony_ci
19438c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
19448c2ecf20Sopenharmony_ci	if (ret_val)
19458c2ecf20Sopenharmony_ci		goto out;
19468c2ecf20Sopenharmony_ci
19478c2ecf20Sopenharmony_ci	phy->is_mdix = (phy_data & M88E1000_PSSR_MDIX) ? true : false;
19488c2ecf20Sopenharmony_ci
19498c2ecf20Sopenharmony_ci	if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS) {
19508c2ecf20Sopenharmony_ci		ret_val = phy->ops.get_cable_length(hw);
19518c2ecf20Sopenharmony_ci		if (ret_val)
19528c2ecf20Sopenharmony_ci			goto out;
19538c2ecf20Sopenharmony_ci
19548c2ecf20Sopenharmony_ci		ret_val = phy->ops.read_reg(hw, PHY_1000T_STATUS, &phy_data);
19558c2ecf20Sopenharmony_ci		if (ret_val)
19568c2ecf20Sopenharmony_ci			goto out;
19578c2ecf20Sopenharmony_ci
19588c2ecf20Sopenharmony_ci		phy->local_rx = (phy_data & SR_1000T_LOCAL_RX_STATUS)
19598c2ecf20Sopenharmony_ci				? e1000_1000t_rx_status_ok
19608c2ecf20Sopenharmony_ci				: e1000_1000t_rx_status_not_ok;
19618c2ecf20Sopenharmony_ci
19628c2ecf20Sopenharmony_ci		phy->remote_rx = (phy_data & SR_1000T_REMOTE_RX_STATUS)
19638c2ecf20Sopenharmony_ci				 ? e1000_1000t_rx_status_ok
19648c2ecf20Sopenharmony_ci				 : e1000_1000t_rx_status_not_ok;
19658c2ecf20Sopenharmony_ci	} else {
19668c2ecf20Sopenharmony_ci		/* Set values to "undefined" */
19678c2ecf20Sopenharmony_ci		phy->cable_length = E1000_CABLE_LENGTH_UNDEFINED;
19688c2ecf20Sopenharmony_ci		phy->local_rx = e1000_1000t_rx_status_undefined;
19698c2ecf20Sopenharmony_ci		phy->remote_rx = e1000_1000t_rx_status_undefined;
19708c2ecf20Sopenharmony_ci	}
19718c2ecf20Sopenharmony_ci
19728c2ecf20Sopenharmony_ciout:
19738c2ecf20Sopenharmony_ci	return ret_val;
19748c2ecf20Sopenharmony_ci}
19758c2ecf20Sopenharmony_ci
19768c2ecf20Sopenharmony_ci/**
19778c2ecf20Sopenharmony_ci *  igb_get_phy_info_igp - Retrieve igp PHY information
19788c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
19798c2ecf20Sopenharmony_ci *
19808c2ecf20Sopenharmony_ci *  Read PHY status to determine if link is up.  If link is up, then
19818c2ecf20Sopenharmony_ci *  set/determine 10base-T extended distance and polarity correction.  Read
19828c2ecf20Sopenharmony_ci *  PHY port status to determine MDI/MDIx and speed.  Based on the speed,
19838c2ecf20Sopenharmony_ci *  determine on the cable length, local and remote receiver.
19848c2ecf20Sopenharmony_ci **/
19858c2ecf20Sopenharmony_cis32 igb_get_phy_info_igp(struct e1000_hw *hw)
19868c2ecf20Sopenharmony_ci{
19878c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
19888c2ecf20Sopenharmony_ci	s32 ret_val;
19898c2ecf20Sopenharmony_ci	u16 data;
19908c2ecf20Sopenharmony_ci	bool link;
19918c2ecf20Sopenharmony_ci
19928c2ecf20Sopenharmony_ci	ret_val = igb_phy_has_link(hw, 1, 0, &link);
19938c2ecf20Sopenharmony_ci	if (ret_val)
19948c2ecf20Sopenharmony_ci		goto out;
19958c2ecf20Sopenharmony_ci
19968c2ecf20Sopenharmony_ci	if (!link) {
19978c2ecf20Sopenharmony_ci		hw_dbg("Phy info is only valid if link is up\n");
19988c2ecf20Sopenharmony_ci		ret_val = -E1000_ERR_CONFIG;
19998c2ecf20Sopenharmony_ci		goto out;
20008c2ecf20Sopenharmony_ci	}
20018c2ecf20Sopenharmony_ci
20028c2ecf20Sopenharmony_ci	phy->polarity_correction = true;
20038c2ecf20Sopenharmony_ci
20048c2ecf20Sopenharmony_ci	ret_val = igb_check_polarity_igp(hw);
20058c2ecf20Sopenharmony_ci	if (ret_val)
20068c2ecf20Sopenharmony_ci		goto out;
20078c2ecf20Sopenharmony_ci
20088c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_STATUS, &data);
20098c2ecf20Sopenharmony_ci	if (ret_val)
20108c2ecf20Sopenharmony_ci		goto out;
20118c2ecf20Sopenharmony_ci
20128c2ecf20Sopenharmony_ci	phy->is_mdix = (data & IGP01E1000_PSSR_MDIX) ? true : false;
20138c2ecf20Sopenharmony_ci
20148c2ecf20Sopenharmony_ci	if ((data & IGP01E1000_PSSR_SPEED_MASK) ==
20158c2ecf20Sopenharmony_ci	    IGP01E1000_PSSR_SPEED_1000MBPS) {
20168c2ecf20Sopenharmony_ci		ret_val = phy->ops.get_cable_length(hw);
20178c2ecf20Sopenharmony_ci		if (ret_val)
20188c2ecf20Sopenharmony_ci			goto out;
20198c2ecf20Sopenharmony_ci
20208c2ecf20Sopenharmony_ci		ret_val = phy->ops.read_reg(hw, PHY_1000T_STATUS, &data);
20218c2ecf20Sopenharmony_ci		if (ret_val)
20228c2ecf20Sopenharmony_ci			goto out;
20238c2ecf20Sopenharmony_ci
20248c2ecf20Sopenharmony_ci		phy->local_rx = (data & SR_1000T_LOCAL_RX_STATUS)
20258c2ecf20Sopenharmony_ci				? e1000_1000t_rx_status_ok
20268c2ecf20Sopenharmony_ci				: e1000_1000t_rx_status_not_ok;
20278c2ecf20Sopenharmony_ci
20288c2ecf20Sopenharmony_ci		phy->remote_rx = (data & SR_1000T_REMOTE_RX_STATUS)
20298c2ecf20Sopenharmony_ci				 ? e1000_1000t_rx_status_ok
20308c2ecf20Sopenharmony_ci				 : e1000_1000t_rx_status_not_ok;
20318c2ecf20Sopenharmony_ci	} else {
20328c2ecf20Sopenharmony_ci		phy->cable_length = E1000_CABLE_LENGTH_UNDEFINED;
20338c2ecf20Sopenharmony_ci		phy->local_rx = e1000_1000t_rx_status_undefined;
20348c2ecf20Sopenharmony_ci		phy->remote_rx = e1000_1000t_rx_status_undefined;
20358c2ecf20Sopenharmony_ci	}
20368c2ecf20Sopenharmony_ci
20378c2ecf20Sopenharmony_ciout:
20388c2ecf20Sopenharmony_ci	return ret_val;
20398c2ecf20Sopenharmony_ci}
20408c2ecf20Sopenharmony_ci
20418c2ecf20Sopenharmony_ci/**
20428c2ecf20Sopenharmony_ci *  igb_phy_sw_reset - PHY software reset
20438c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
20448c2ecf20Sopenharmony_ci *
20458c2ecf20Sopenharmony_ci *  Does a software reset of the PHY by reading the PHY control register and
20468c2ecf20Sopenharmony_ci *  setting/write the control register reset bit to the PHY.
20478c2ecf20Sopenharmony_ci **/
20488c2ecf20Sopenharmony_cis32 igb_phy_sw_reset(struct e1000_hw *hw)
20498c2ecf20Sopenharmony_ci{
20508c2ecf20Sopenharmony_ci	s32 ret_val = 0;
20518c2ecf20Sopenharmony_ci	u16 phy_ctrl;
20528c2ecf20Sopenharmony_ci
20538c2ecf20Sopenharmony_ci	if (!(hw->phy.ops.read_reg))
20548c2ecf20Sopenharmony_ci		goto out;
20558c2ecf20Sopenharmony_ci
20568c2ecf20Sopenharmony_ci	ret_val = hw->phy.ops.read_reg(hw, PHY_CONTROL, &phy_ctrl);
20578c2ecf20Sopenharmony_ci	if (ret_val)
20588c2ecf20Sopenharmony_ci		goto out;
20598c2ecf20Sopenharmony_ci
20608c2ecf20Sopenharmony_ci	phy_ctrl |= MII_CR_RESET;
20618c2ecf20Sopenharmony_ci	ret_val = hw->phy.ops.write_reg(hw, PHY_CONTROL, phy_ctrl);
20628c2ecf20Sopenharmony_ci	if (ret_val)
20638c2ecf20Sopenharmony_ci		goto out;
20648c2ecf20Sopenharmony_ci
20658c2ecf20Sopenharmony_ci	udelay(1);
20668c2ecf20Sopenharmony_ci
20678c2ecf20Sopenharmony_ciout:
20688c2ecf20Sopenharmony_ci	return ret_val;
20698c2ecf20Sopenharmony_ci}
20708c2ecf20Sopenharmony_ci
20718c2ecf20Sopenharmony_ci/**
20728c2ecf20Sopenharmony_ci *  igb_phy_hw_reset - PHY hardware reset
20738c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
20748c2ecf20Sopenharmony_ci *
20758c2ecf20Sopenharmony_ci *  Verify the reset block is not blocking us from resetting.  Acquire
20768c2ecf20Sopenharmony_ci *  semaphore (if necessary) and read/set/write the device control reset
20778c2ecf20Sopenharmony_ci *  bit in the PHY.  Wait the appropriate delay time for the device to
20788c2ecf20Sopenharmony_ci *  reset and release the semaphore (if necessary).
20798c2ecf20Sopenharmony_ci **/
20808c2ecf20Sopenharmony_cis32 igb_phy_hw_reset(struct e1000_hw *hw)
20818c2ecf20Sopenharmony_ci{
20828c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
20838c2ecf20Sopenharmony_ci	s32  ret_val;
20848c2ecf20Sopenharmony_ci	u32 ctrl;
20858c2ecf20Sopenharmony_ci
20868c2ecf20Sopenharmony_ci	ret_val = igb_check_reset_block(hw);
20878c2ecf20Sopenharmony_ci	if (ret_val) {
20888c2ecf20Sopenharmony_ci		ret_val = 0;
20898c2ecf20Sopenharmony_ci		goto out;
20908c2ecf20Sopenharmony_ci	}
20918c2ecf20Sopenharmony_ci
20928c2ecf20Sopenharmony_ci	ret_val = phy->ops.acquire(hw);
20938c2ecf20Sopenharmony_ci	if (ret_val)
20948c2ecf20Sopenharmony_ci		goto out;
20958c2ecf20Sopenharmony_ci
20968c2ecf20Sopenharmony_ci	ctrl = rd32(E1000_CTRL);
20978c2ecf20Sopenharmony_ci	wr32(E1000_CTRL, ctrl | E1000_CTRL_PHY_RST);
20988c2ecf20Sopenharmony_ci	wrfl();
20998c2ecf20Sopenharmony_ci
21008c2ecf20Sopenharmony_ci	udelay(phy->reset_delay_us);
21018c2ecf20Sopenharmony_ci
21028c2ecf20Sopenharmony_ci	wr32(E1000_CTRL, ctrl);
21038c2ecf20Sopenharmony_ci	wrfl();
21048c2ecf20Sopenharmony_ci
21058c2ecf20Sopenharmony_ci	udelay(150);
21068c2ecf20Sopenharmony_ci
21078c2ecf20Sopenharmony_ci	phy->ops.release(hw);
21088c2ecf20Sopenharmony_ci
21098c2ecf20Sopenharmony_ci	ret_val = phy->ops.get_cfg_done(hw);
21108c2ecf20Sopenharmony_ci
21118c2ecf20Sopenharmony_ciout:
21128c2ecf20Sopenharmony_ci	return ret_val;
21138c2ecf20Sopenharmony_ci}
21148c2ecf20Sopenharmony_ci
21158c2ecf20Sopenharmony_ci/**
21168c2ecf20Sopenharmony_ci *  igb_phy_init_script_igp3 - Inits the IGP3 PHY
21178c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
21188c2ecf20Sopenharmony_ci *
21198c2ecf20Sopenharmony_ci *  Initializes a Intel Gigabit PHY3 when an EEPROM is not present.
21208c2ecf20Sopenharmony_ci **/
21218c2ecf20Sopenharmony_cis32 igb_phy_init_script_igp3(struct e1000_hw *hw)
21228c2ecf20Sopenharmony_ci{
21238c2ecf20Sopenharmony_ci	hw_dbg("Running IGP 3 PHY init script\n");
21248c2ecf20Sopenharmony_ci
21258c2ecf20Sopenharmony_ci	/* PHY init IGP 3 */
21268c2ecf20Sopenharmony_ci	/* Enable rise/fall, 10-mode work in class-A */
21278c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x2F5B, 0x9018);
21288c2ecf20Sopenharmony_ci	/* Remove all caps from Replica path filter */
21298c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x2F52, 0x0000);
21308c2ecf20Sopenharmony_ci	/* Bias trimming for ADC, AFE and Driver (Default) */
21318c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x2FB1, 0x8B24);
21328c2ecf20Sopenharmony_ci	/* Increase Hybrid poly bias */
21338c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x2FB2, 0xF8F0);
21348c2ecf20Sopenharmony_ci	/* Add 4% to TX amplitude in Giga mode */
21358c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x2010, 0x10B0);
21368c2ecf20Sopenharmony_ci	/* Disable trimming (TTT) */
21378c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x2011, 0x0000);
21388c2ecf20Sopenharmony_ci	/* Poly DC correction to 94.6% + 2% for all channels */
21398c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x20DD, 0x249A);
21408c2ecf20Sopenharmony_ci	/* ABS DC correction to 95.9% */
21418c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x20DE, 0x00D3);
21428c2ecf20Sopenharmony_ci	/* BG temp curve trim */
21438c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x28B4, 0x04CE);
21448c2ecf20Sopenharmony_ci	/* Increasing ADC OPAMP stage 1 currents to max */
21458c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x2F70, 0x29E4);
21468c2ecf20Sopenharmony_ci	/* Force 1000 ( required for enabling PHY regs configuration) */
21478c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x0000, 0x0140);
21488c2ecf20Sopenharmony_ci	/* Set upd_freq to 6 */
21498c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x1F30, 0x1606);
21508c2ecf20Sopenharmony_ci	/* Disable NPDFE */
21518c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x1F31, 0xB814);
21528c2ecf20Sopenharmony_ci	/* Disable adaptive fixed FFE (Default) */
21538c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x1F35, 0x002A);
21548c2ecf20Sopenharmony_ci	/* Enable FFE hysteresis */
21558c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x1F3E, 0x0067);
21568c2ecf20Sopenharmony_ci	/* Fixed FFE for short cable lengths */
21578c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x1F54, 0x0065);
21588c2ecf20Sopenharmony_ci	/* Fixed FFE for medium cable lengths */
21598c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x1F55, 0x002A);
21608c2ecf20Sopenharmony_ci	/* Fixed FFE for long cable lengths */
21618c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x1F56, 0x002A);
21628c2ecf20Sopenharmony_ci	/* Enable Adaptive Clip Threshold */
21638c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x1F72, 0x3FB0);
21648c2ecf20Sopenharmony_ci	/* AHT reset limit to 1 */
21658c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x1F76, 0xC0FF);
21668c2ecf20Sopenharmony_ci	/* Set AHT master delay to 127 msec */
21678c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x1F77, 0x1DEC);
21688c2ecf20Sopenharmony_ci	/* Set scan bits for AHT */
21698c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x1F78, 0xF9EF);
21708c2ecf20Sopenharmony_ci	/* Set AHT Preset bits */
21718c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x1F79, 0x0210);
21728c2ecf20Sopenharmony_ci	/* Change integ_factor of channel A to 3 */
21738c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x1895, 0x0003);
21748c2ecf20Sopenharmony_ci	/* Change prop_factor of channels BCD to 8 */
21758c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x1796, 0x0008);
21768c2ecf20Sopenharmony_ci	/* Change cg_icount + enable integbp for channels BCD */
21778c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x1798, 0xD008);
21788c2ecf20Sopenharmony_ci	/* Change cg_icount + enable integbp + change prop_factor_master
21798c2ecf20Sopenharmony_ci	 * to 8 for channel A
21808c2ecf20Sopenharmony_ci	 */
21818c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x1898, 0xD918);
21828c2ecf20Sopenharmony_ci	/* Disable AHT in Slave mode on channel A */
21838c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x187A, 0x0800);
21848c2ecf20Sopenharmony_ci	/* Enable LPLU and disable AN to 1000 in non-D0a states,
21858c2ecf20Sopenharmony_ci	 * Enable SPD+B2B
21868c2ecf20Sopenharmony_ci	 */
21878c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x0019, 0x008D);
21888c2ecf20Sopenharmony_ci	/* Enable restart AN on an1000_dis change */
21898c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x001B, 0x2080);
21908c2ecf20Sopenharmony_ci	/* Enable wh_fifo read clock in 10/100 modes */
21918c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x0014, 0x0045);
21928c2ecf20Sopenharmony_ci	/* Restart AN, Speed selection is 1000 */
21938c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, 0x0000, 0x1340);
21948c2ecf20Sopenharmony_ci
21958c2ecf20Sopenharmony_ci	return 0;
21968c2ecf20Sopenharmony_ci}
21978c2ecf20Sopenharmony_ci
21988c2ecf20Sopenharmony_ci/**
21998c2ecf20Sopenharmony_ci *  igb_initialize_M88E1512_phy - Initialize M88E1512 PHY
22008c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
22018c2ecf20Sopenharmony_ci *
22028c2ecf20Sopenharmony_ci *  Initialize Marvel 1512 to work correctly with Avoton.
22038c2ecf20Sopenharmony_ci **/
22048c2ecf20Sopenharmony_cis32 igb_initialize_M88E1512_phy(struct e1000_hw *hw)
22058c2ecf20Sopenharmony_ci{
22068c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
22078c2ecf20Sopenharmony_ci	s32 ret_val = 0;
22088c2ecf20Sopenharmony_ci
22098c2ecf20Sopenharmony_ci	/* Switch to PHY page 0xFF. */
22108c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x00FF);
22118c2ecf20Sopenharmony_ci	if (ret_val)
22128c2ecf20Sopenharmony_ci		goto out;
22138c2ecf20Sopenharmony_ci
22148c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0x214B);
22158c2ecf20Sopenharmony_ci	if (ret_val)
22168c2ecf20Sopenharmony_ci		goto out;
22178c2ecf20Sopenharmony_ci
22188c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2144);
22198c2ecf20Sopenharmony_ci	if (ret_val)
22208c2ecf20Sopenharmony_ci		goto out;
22218c2ecf20Sopenharmony_ci
22228c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0x0C28);
22238c2ecf20Sopenharmony_ci	if (ret_val)
22248c2ecf20Sopenharmony_ci		goto out;
22258c2ecf20Sopenharmony_ci
22268c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2146);
22278c2ecf20Sopenharmony_ci	if (ret_val)
22288c2ecf20Sopenharmony_ci		goto out;
22298c2ecf20Sopenharmony_ci
22308c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0xB233);
22318c2ecf20Sopenharmony_ci	if (ret_val)
22328c2ecf20Sopenharmony_ci		goto out;
22338c2ecf20Sopenharmony_ci
22348c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x214D);
22358c2ecf20Sopenharmony_ci	if (ret_val)
22368c2ecf20Sopenharmony_ci		goto out;
22378c2ecf20Sopenharmony_ci
22388c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0xCC0C);
22398c2ecf20Sopenharmony_ci	if (ret_val)
22408c2ecf20Sopenharmony_ci		goto out;
22418c2ecf20Sopenharmony_ci
22428c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2159);
22438c2ecf20Sopenharmony_ci	if (ret_val)
22448c2ecf20Sopenharmony_ci		goto out;
22458c2ecf20Sopenharmony_ci
22468c2ecf20Sopenharmony_ci	/* Switch to PHY page 0xFB. */
22478c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x00FB);
22488c2ecf20Sopenharmony_ci	if (ret_val)
22498c2ecf20Sopenharmony_ci		goto out;
22508c2ecf20Sopenharmony_ci
22518c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_3, 0x000D);
22528c2ecf20Sopenharmony_ci	if (ret_val)
22538c2ecf20Sopenharmony_ci		goto out;
22548c2ecf20Sopenharmony_ci
22558c2ecf20Sopenharmony_ci	/* Switch to PHY page 0x12. */
22568c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x12);
22578c2ecf20Sopenharmony_ci	if (ret_val)
22588c2ecf20Sopenharmony_ci		goto out;
22598c2ecf20Sopenharmony_ci
22608c2ecf20Sopenharmony_ci	/* Change mode to SGMII-to-Copper */
22618c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_MODE, 0x8001);
22628c2ecf20Sopenharmony_ci	if (ret_val)
22638c2ecf20Sopenharmony_ci		goto out;
22648c2ecf20Sopenharmony_ci
22658c2ecf20Sopenharmony_ci	/* Return the PHY to page 0. */
22668c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0);
22678c2ecf20Sopenharmony_ci	if (ret_val)
22688c2ecf20Sopenharmony_ci		goto out;
22698c2ecf20Sopenharmony_ci
22708c2ecf20Sopenharmony_ci	ret_val = igb_phy_sw_reset(hw);
22718c2ecf20Sopenharmony_ci	if (ret_val) {
22728c2ecf20Sopenharmony_ci		hw_dbg("Error committing the PHY changes\n");
22738c2ecf20Sopenharmony_ci		return ret_val;
22748c2ecf20Sopenharmony_ci	}
22758c2ecf20Sopenharmony_ci
22768c2ecf20Sopenharmony_ci	/* msec_delay(1000); */
22778c2ecf20Sopenharmony_ci	usleep_range(1000, 2000);
22788c2ecf20Sopenharmony_ciout:
22798c2ecf20Sopenharmony_ci	return ret_val;
22808c2ecf20Sopenharmony_ci}
22818c2ecf20Sopenharmony_ci
22828c2ecf20Sopenharmony_ci/**
22838c2ecf20Sopenharmony_ci *  igb_initialize_M88E1543_phy - Initialize M88E1512 PHY
22848c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
22858c2ecf20Sopenharmony_ci *
22868c2ecf20Sopenharmony_ci *  Initialize Marvell 1543 to work correctly with Avoton.
22878c2ecf20Sopenharmony_ci **/
22888c2ecf20Sopenharmony_cis32 igb_initialize_M88E1543_phy(struct e1000_hw *hw)
22898c2ecf20Sopenharmony_ci{
22908c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
22918c2ecf20Sopenharmony_ci	s32 ret_val = 0;
22928c2ecf20Sopenharmony_ci
22938c2ecf20Sopenharmony_ci	/* Switch to PHY page 0xFF. */
22948c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x00FF);
22958c2ecf20Sopenharmony_ci	if (ret_val)
22968c2ecf20Sopenharmony_ci		goto out;
22978c2ecf20Sopenharmony_ci
22988c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0x214B);
22998c2ecf20Sopenharmony_ci	if (ret_val)
23008c2ecf20Sopenharmony_ci		goto out;
23018c2ecf20Sopenharmony_ci
23028c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2144);
23038c2ecf20Sopenharmony_ci	if (ret_val)
23048c2ecf20Sopenharmony_ci		goto out;
23058c2ecf20Sopenharmony_ci
23068c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0x0C28);
23078c2ecf20Sopenharmony_ci	if (ret_val)
23088c2ecf20Sopenharmony_ci		goto out;
23098c2ecf20Sopenharmony_ci
23108c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2146);
23118c2ecf20Sopenharmony_ci	if (ret_val)
23128c2ecf20Sopenharmony_ci		goto out;
23138c2ecf20Sopenharmony_ci
23148c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0xB233);
23158c2ecf20Sopenharmony_ci	if (ret_val)
23168c2ecf20Sopenharmony_ci		goto out;
23178c2ecf20Sopenharmony_ci
23188c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x214D);
23198c2ecf20Sopenharmony_ci	if (ret_val)
23208c2ecf20Sopenharmony_ci		goto out;
23218c2ecf20Sopenharmony_ci
23228c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0xDC0C);
23238c2ecf20Sopenharmony_ci	if (ret_val)
23248c2ecf20Sopenharmony_ci		goto out;
23258c2ecf20Sopenharmony_ci
23268c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2159);
23278c2ecf20Sopenharmony_ci	if (ret_val)
23288c2ecf20Sopenharmony_ci		goto out;
23298c2ecf20Sopenharmony_ci
23308c2ecf20Sopenharmony_ci	/* Switch to PHY page 0xFB. */
23318c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x00FB);
23328c2ecf20Sopenharmony_ci	if (ret_val)
23338c2ecf20Sopenharmony_ci		goto out;
23348c2ecf20Sopenharmony_ci
23358c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_3, 0x0C0D);
23368c2ecf20Sopenharmony_ci	if (ret_val)
23378c2ecf20Sopenharmony_ci		goto out;
23388c2ecf20Sopenharmony_ci
23398c2ecf20Sopenharmony_ci	/* Switch to PHY page 0x12. */
23408c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x12);
23418c2ecf20Sopenharmony_ci	if (ret_val)
23428c2ecf20Sopenharmony_ci		goto out;
23438c2ecf20Sopenharmony_ci
23448c2ecf20Sopenharmony_ci	/* Change mode to SGMII-to-Copper */
23458c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_MODE, 0x8001);
23468c2ecf20Sopenharmony_ci	if (ret_val)
23478c2ecf20Sopenharmony_ci		goto out;
23488c2ecf20Sopenharmony_ci
23498c2ecf20Sopenharmony_ci	/* Switch to PHY page 1. */
23508c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x1);
23518c2ecf20Sopenharmony_ci	if (ret_val)
23528c2ecf20Sopenharmony_ci		goto out;
23538c2ecf20Sopenharmony_ci
23548c2ecf20Sopenharmony_ci	/* Change mode to 1000BASE-X/SGMII and autoneg enable */
23558c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1543_FIBER_CTRL, 0x9140);
23568c2ecf20Sopenharmony_ci	if (ret_val)
23578c2ecf20Sopenharmony_ci		goto out;
23588c2ecf20Sopenharmony_ci
23598c2ecf20Sopenharmony_ci	/* Return the PHY to page 0. */
23608c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0);
23618c2ecf20Sopenharmony_ci	if (ret_val)
23628c2ecf20Sopenharmony_ci		goto out;
23638c2ecf20Sopenharmony_ci
23648c2ecf20Sopenharmony_ci	ret_val = igb_phy_sw_reset(hw);
23658c2ecf20Sopenharmony_ci	if (ret_val) {
23668c2ecf20Sopenharmony_ci		hw_dbg("Error committing the PHY changes\n");
23678c2ecf20Sopenharmony_ci		return ret_val;
23688c2ecf20Sopenharmony_ci	}
23698c2ecf20Sopenharmony_ci
23708c2ecf20Sopenharmony_ci	/* msec_delay(1000); */
23718c2ecf20Sopenharmony_ci	usleep_range(1000, 2000);
23728c2ecf20Sopenharmony_ciout:
23738c2ecf20Sopenharmony_ci	return ret_val;
23748c2ecf20Sopenharmony_ci}
23758c2ecf20Sopenharmony_ci
23768c2ecf20Sopenharmony_ci/**
23778c2ecf20Sopenharmony_ci * igb_power_up_phy_copper - Restore copper link in case of PHY power down
23788c2ecf20Sopenharmony_ci * @hw: pointer to the HW structure
23798c2ecf20Sopenharmony_ci *
23808c2ecf20Sopenharmony_ci * In the case of a PHY power down to save power, or to turn off link during a
23818c2ecf20Sopenharmony_ci * driver unload, restore the link to previous settings.
23828c2ecf20Sopenharmony_ci **/
23838c2ecf20Sopenharmony_civoid igb_power_up_phy_copper(struct e1000_hw *hw)
23848c2ecf20Sopenharmony_ci{
23858c2ecf20Sopenharmony_ci	u16 mii_reg = 0;
23868c2ecf20Sopenharmony_ci
23878c2ecf20Sopenharmony_ci	/* The PHY will retain its settings across a power down/up cycle */
23888c2ecf20Sopenharmony_ci	hw->phy.ops.read_reg(hw, PHY_CONTROL, &mii_reg);
23898c2ecf20Sopenharmony_ci	mii_reg &= ~MII_CR_POWER_DOWN;
23908c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, PHY_CONTROL, mii_reg);
23918c2ecf20Sopenharmony_ci}
23928c2ecf20Sopenharmony_ci
23938c2ecf20Sopenharmony_ci/**
23948c2ecf20Sopenharmony_ci * igb_power_down_phy_copper - Power down copper PHY
23958c2ecf20Sopenharmony_ci * @hw: pointer to the HW structure
23968c2ecf20Sopenharmony_ci *
23978c2ecf20Sopenharmony_ci * Power down PHY to save power when interface is down and wake on lan
23988c2ecf20Sopenharmony_ci * is not enabled.
23998c2ecf20Sopenharmony_ci **/
24008c2ecf20Sopenharmony_civoid igb_power_down_phy_copper(struct e1000_hw *hw)
24018c2ecf20Sopenharmony_ci{
24028c2ecf20Sopenharmony_ci	u16 mii_reg = 0;
24038c2ecf20Sopenharmony_ci
24048c2ecf20Sopenharmony_ci	/* The PHY will retain its settings across a power down/up cycle */
24058c2ecf20Sopenharmony_ci	hw->phy.ops.read_reg(hw, PHY_CONTROL, &mii_reg);
24068c2ecf20Sopenharmony_ci	mii_reg |= MII_CR_POWER_DOWN;
24078c2ecf20Sopenharmony_ci	hw->phy.ops.write_reg(hw, PHY_CONTROL, mii_reg);
24088c2ecf20Sopenharmony_ci	usleep_range(1000, 2000);
24098c2ecf20Sopenharmony_ci}
24108c2ecf20Sopenharmony_ci
24118c2ecf20Sopenharmony_ci/**
24128c2ecf20Sopenharmony_ci *  igb_check_polarity_82580 - Checks the polarity.
24138c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
24148c2ecf20Sopenharmony_ci *
24158c2ecf20Sopenharmony_ci *  Success returns 0, Failure returns -E1000_ERR_PHY (-2)
24168c2ecf20Sopenharmony_ci *
24178c2ecf20Sopenharmony_ci *  Polarity is determined based on the PHY specific status register.
24188c2ecf20Sopenharmony_ci **/
24198c2ecf20Sopenharmony_cistatic s32 igb_check_polarity_82580(struct e1000_hw *hw)
24208c2ecf20Sopenharmony_ci{
24218c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
24228c2ecf20Sopenharmony_ci	s32 ret_val;
24238c2ecf20Sopenharmony_ci	u16 data;
24248c2ecf20Sopenharmony_ci
24258c2ecf20Sopenharmony_ci
24268c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, I82580_PHY_STATUS_2, &data);
24278c2ecf20Sopenharmony_ci
24288c2ecf20Sopenharmony_ci	if (!ret_val)
24298c2ecf20Sopenharmony_ci		phy->cable_polarity = (data & I82580_PHY_STATUS2_REV_POLARITY)
24308c2ecf20Sopenharmony_ci				      ? e1000_rev_polarity_reversed
24318c2ecf20Sopenharmony_ci				      : e1000_rev_polarity_normal;
24328c2ecf20Sopenharmony_ci
24338c2ecf20Sopenharmony_ci	return ret_val;
24348c2ecf20Sopenharmony_ci}
24358c2ecf20Sopenharmony_ci
24368c2ecf20Sopenharmony_ci/**
24378c2ecf20Sopenharmony_ci *  igb_phy_force_speed_duplex_82580 - Force speed/duplex for I82580 PHY
24388c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
24398c2ecf20Sopenharmony_ci *
24408c2ecf20Sopenharmony_ci *  Calls the PHY setup function to force speed and duplex.  Clears the
24418c2ecf20Sopenharmony_ci *  auto-crossover to force MDI manually.  Waits for link and returns
24428c2ecf20Sopenharmony_ci *  successful if link up is successful, else -E1000_ERR_PHY (-2).
24438c2ecf20Sopenharmony_ci **/
24448c2ecf20Sopenharmony_cis32 igb_phy_force_speed_duplex_82580(struct e1000_hw *hw)
24458c2ecf20Sopenharmony_ci{
24468c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
24478c2ecf20Sopenharmony_ci	s32 ret_val;
24488c2ecf20Sopenharmony_ci	u16 phy_data;
24498c2ecf20Sopenharmony_ci	bool link;
24508c2ecf20Sopenharmony_ci
24518c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_data);
24528c2ecf20Sopenharmony_ci	if (ret_val)
24538c2ecf20Sopenharmony_ci		goto out;
24548c2ecf20Sopenharmony_ci
24558c2ecf20Sopenharmony_ci	igb_phy_force_speed_duplex_setup(hw, &phy_data);
24568c2ecf20Sopenharmony_ci
24578c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_data);
24588c2ecf20Sopenharmony_ci	if (ret_val)
24598c2ecf20Sopenharmony_ci		goto out;
24608c2ecf20Sopenharmony_ci
24618c2ecf20Sopenharmony_ci	/* Clear Auto-Crossover to force MDI manually.  82580 requires MDI
24628c2ecf20Sopenharmony_ci	 * forced whenever speed and duplex are forced.
24638c2ecf20Sopenharmony_ci	 */
24648c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, I82580_PHY_CTRL_2, &phy_data);
24658c2ecf20Sopenharmony_ci	if (ret_val)
24668c2ecf20Sopenharmony_ci		goto out;
24678c2ecf20Sopenharmony_ci
24688c2ecf20Sopenharmony_ci	phy_data &= ~I82580_PHY_CTRL2_MDIX_CFG_MASK;
24698c2ecf20Sopenharmony_ci
24708c2ecf20Sopenharmony_ci	ret_val = phy->ops.write_reg(hw, I82580_PHY_CTRL_2, phy_data);
24718c2ecf20Sopenharmony_ci	if (ret_val)
24728c2ecf20Sopenharmony_ci		goto out;
24738c2ecf20Sopenharmony_ci
24748c2ecf20Sopenharmony_ci	hw_dbg("I82580_PHY_CTRL_2: %X\n", phy_data);
24758c2ecf20Sopenharmony_ci
24768c2ecf20Sopenharmony_ci	udelay(1);
24778c2ecf20Sopenharmony_ci
24788c2ecf20Sopenharmony_ci	if (phy->autoneg_wait_to_complete) {
24798c2ecf20Sopenharmony_ci		hw_dbg("Waiting for forced speed/duplex link on 82580 phy\n");
24808c2ecf20Sopenharmony_ci
24818c2ecf20Sopenharmony_ci		ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT, 100000, &link);
24828c2ecf20Sopenharmony_ci		if (ret_val)
24838c2ecf20Sopenharmony_ci			goto out;
24848c2ecf20Sopenharmony_ci
24858c2ecf20Sopenharmony_ci		if (!link)
24868c2ecf20Sopenharmony_ci			hw_dbg("Link taking longer than expected.\n");
24878c2ecf20Sopenharmony_ci
24888c2ecf20Sopenharmony_ci		/* Try once more */
24898c2ecf20Sopenharmony_ci		ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT, 100000, &link);
24908c2ecf20Sopenharmony_ci		if (ret_val)
24918c2ecf20Sopenharmony_ci			goto out;
24928c2ecf20Sopenharmony_ci	}
24938c2ecf20Sopenharmony_ci
24948c2ecf20Sopenharmony_ciout:
24958c2ecf20Sopenharmony_ci	return ret_val;
24968c2ecf20Sopenharmony_ci}
24978c2ecf20Sopenharmony_ci
24988c2ecf20Sopenharmony_ci/**
24998c2ecf20Sopenharmony_ci *  igb_get_phy_info_82580 - Retrieve I82580 PHY information
25008c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
25018c2ecf20Sopenharmony_ci *
25028c2ecf20Sopenharmony_ci *  Read PHY status to determine if link is up.  If link is up, then
25038c2ecf20Sopenharmony_ci *  set/determine 10base-T extended distance and polarity correction.  Read
25048c2ecf20Sopenharmony_ci *  PHY port status to determine MDI/MDIx and speed.  Based on the speed,
25058c2ecf20Sopenharmony_ci *  determine on the cable length, local and remote receiver.
25068c2ecf20Sopenharmony_ci **/
25078c2ecf20Sopenharmony_cis32 igb_get_phy_info_82580(struct e1000_hw *hw)
25088c2ecf20Sopenharmony_ci{
25098c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
25108c2ecf20Sopenharmony_ci	s32 ret_val;
25118c2ecf20Sopenharmony_ci	u16 data;
25128c2ecf20Sopenharmony_ci	bool link;
25138c2ecf20Sopenharmony_ci
25148c2ecf20Sopenharmony_ci	ret_val = igb_phy_has_link(hw, 1, 0, &link);
25158c2ecf20Sopenharmony_ci	if (ret_val)
25168c2ecf20Sopenharmony_ci		goto out;
25178c2ecf20Sopenharmony_ci
25188c2ecf20Sopenharmony_ci	if (!link) {
25198c2ecf20Sopenharmony_ci		hw_dbg("Phy info is only valid if link is up\n");
25208c2ecf20Sopenharmony_ci		ret_val = -E1000_ERR_CONFIG;
25218c2ecf20Sopenharmony_ci		goto out;
25228c2ecf20Sopenharmony_ci	}
25238c2ecf20Sopenharmony_ci
25248c2ecf20Sopenharmony_ci	phy->polarity_correction = true;
25258c2ecf20Sopenharmony_ci
25268c2ecf20Sopenharmony_ci	ret_val = igb_check_polarity_82580(hw);
25278c2ecf20Sopenharmony_ci	if (ret_val)
25288c2ecf20Sopenharmony_ci		goto out;
25298c2ecf20Sopenharmony_ci
25308c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, I82580_PHY_STATUS_2, &data);
25318c2ecf20Sopenharmony_ci	if (ret_val)
25328c2ecf20Sopenharmony_ci		goto out;
25338c2ecf20Sopenharmony_ci
25348c2ecf20Sopenharmony_ci	phy->is_mdix = (data & I82580_PHY_STATUS2_MDIX) ? true : false;
25358c2ecf20Sopenharmony_ci
25368c2ecf20Sopenharmony_ci	if ((data & I82580_PHY_STATUS2_SPEED_MASK) ==
25378c2ecf20Sopenharmony_ci	    I82580_PHY_STATUS2_SPEED_1000MBPS) {
25388c2ecf20Sopenharmony_ci		ret_val = hw->phy.ops.get_cable_length(hw);
25398c2ecf20Sopenharmony_ci		if (ret_val)
25408c2ecf20Sopenharmony_ci			goto out;
25418c2ecf20Sopenharmony_ci
25428c2ecf20Sopenharmony_ci		ret_val = phy->ops.read_reg(hw, PHY_1000T_STATUS, &data);
25438c2ecf20Sopenharmony_ci		if (ret_val)
25448c2ecf20Sopenharmony_ci			goto out;
25458c2ecf20Sopenharmony_ci
25468c2ecf20Sopenharmony_ci		phy->local_rx = (data & SR_1000T_LOCAL_RX_STATUS)
25478c2ecf20Sopenharmony_ci				? e1000_1000t_rx_status_ok
25488c2ecf20Sopenharmony_ci				: e1000_1000t_rx_status_not_ok;
25498c2ecf20Sopenharmony_ci
25508c2ecf20Sopenharmony_ci		phy->remote_rx = (data & SR_1000T_REMOTE_RX_STATUS)
25518c2ecf20Sopenharmony_ci				 ? e1000_1000t_rx_status_ok
25528c2ecf20Sopenharmony_ci				 : e1000_1000t_rx_status_not_ok;
25538c2ecf20Sopenharmony_ci	} else {
25548c2ecf20Sopenharmony_ci		phy->cable_length = E1000_CABLE_LENGTH_UNDEFINED;
25558c2ecf20Sopenharmony_ci		phy->local_rx = e1000_1000t_rx_status_undefined;
25568c2ecf20Sopenharmony_ci		phy->remote_rx = e1000_1000t_rx_status_undefined;
25578c2ecf20Sopenharmony_ci	}
25588c2ecf20Sopenharmony_ci
25598c2ecf20Sopenharmony_ciout:
25608c2ecf20Sopenharmony_ci	return ret_val;
25618c2ecf20Sopenharmony_ci}
25628c2ecf20Sopenharmony_ci
25638c2ecf20Sopenharmony_ci/**
25648c2ecf20Sopenharmony_ci *  igb_get_cable_length_82580 - Determine cable length for 82580 PHY
25658c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
25668c2ecf20Sopenharmony_ci *
25678c2ecf20Sopenharmony_ci * Reads the diagnostic status register and verifies result is valid before
25688c2ecf20Sopenharmony_ci * placing it in the phy_cable_length field.
25698c2ecf20Sopenharmony_ci **/
25708c2ecf20Sopenharmony_cis32 igb_get_cable_length_82580(struct e1000_hw *hw)
25718c2ecf20Sopenharmony_ci{
25728c2ecf20Sopenharmony_ci	struct e1000_phy_info *phy = &hw->phy;
25738c2ecf20Sopenharmony_ci	s32 ret_val;
25748c2ecf20Sopenharmony_ci	u16 phy_data, length;
25758c2ecf20Sopenharmony_ci
25768c2ecf20Sopenharmony_ci	ret_val = phy->ops.read_reg(hw, I82580_PHY_DIAG_STATUS, &phy_data);
25778c2ecf20Sopenharmony_ci	if (ret_val)
25788c2ecf20Sopenharmony_ci		goto out;
25798c2ecf20Sopenharmony_ci
25808c2ecf20Sopenharmony_ci	length = (phy_data & I82580_DSTATUS_CABLE_LENGTH) >>
25818c2ecf20Sopenharmony_ci		 I82580_DSTATUS_CABLE_LENGTH_SHIFT;
25828c2ecf20Sopenharmony_ci
25838c2ecf20Sopenharmony_ci	if (length == E1000_CABLE_LENGTH_UNDEFINED)
25848c2ecf20Sopenharmony_ci		ret_val = -E1000_ERR_PHY;
25858c2ecf20Sopenharmony_ci
25868c2ecf20Sopenharmony_ci	phy->cable_length = length;
25878c2ecf20Sopenharmony_ci
25888c2ecf20Sopenharmony_ciout:
25898c2ecf20Sopenharmony_ci	return ret_val;
25908c2ecf20Sopenharmony_ci}
25918c2ecf20Sopenharmony_ci
25928c2ecf20Sopenharmony_ci/**
25938c2ecf20Sopenharmony_ci *  igb_set_master_slave_mode - Setup PHY for Master/slave mode
25948c2ecf20Sopenharmony_ci *  @hw: pointer to the HW structure
25958c2ecf20Sopenharmony_ci *
25968c2ecf20Sopenharmony_ci *  Sets up Master/slave mode
25978c2ecf20Sopenharmony_ci **/
25988c2ecf20Sopenharmony_cistatic s32 igb_set_master_slave_mode(struct e1000_hw *hw)
25998c2ecf20Sopenharmony_ci{
26008c2ecf20Sopenharmony_ci	s32 ret_val;
26018c2ecf20Sopenharmony_ci	u16 phy_data;
26028c2ecf20Sopenharmony_ci
26038c2ecf20Sopenharmony_ci	/* Resolve Master/Slave mode */
26048c2ecf20Sopenharmony_ci	ret_val = hw->phy.ops.read_reg(hw, PHY_1000T_CTRL, &phy_data);
26058c2ecf20Sopenharmony_ci	if (ret_val)
26068c2ecf20Sopenharmony_ci		return ret_val;
26078c2ecf20Sopenharmony_ci
26088c2ecf20Sopenharmony_ci	/* load defaults for future use */
26098c2ecf20Sopenharmony_ci	hw->phy.original_ms_type = (phy_data & CR_1000T_MS_ENABLE) ?
26108c2ecf20Sopenharmony_ci				   ((phy_data & CR_1000T_MS_VALUE) ?
26118c2ecf20Sopenharmony_ci				    e1000_ms_force_master :
26128c2ecf20Sopenharmony_ci				    e1000_ms_force_slave) : e1000_ms_auto;
26138c2ecf20Sopenharmony_ci
26148c2ecf20Sopenharmony_ci	switch (hw->phy.ms_type) {
26158c2ecf20Sopenharmony_ci	case e1000_ms_force_master:
26168c2ecf20Sopenharmony_ci		phy_data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
26178c2ecf20Sopenharmony_ci		break;
26188c2ecf20Sopenharmony_ci	case e1000_ms_force_slave:
26198c2ecf20Sopenharmony_ci		phy_data |= CR_1000T_MS_ENABLE;
26208c2ecf20Sopenharmony_ci		phy_data &= ~(CR_1000T_MS_VALUE);
26218c2ecf20Sopenharmony_ci		break;
26228c2ecf20Sopenharmony_ci	case e1000_ms_auto:
26238c2ecf20Sopenharmony_ci		phy_data &= ~CR_1000T_MS_ENABLE;
26248c2ecf20Sopenharmony_ci		fallthrough;
26258c2ecf20Sopenharmony_ci	default:
26268c2ecf20Sopenharmony_ci		break;
26278c2ecf20Sopenharmony_ci	}
26288c2ecf20Sopenharmony_ci
26298c2ecf20Sopenharmony_ci	return hw->phy.ops.write_reg(hw, PHY_1000T_CTRL, phy_data);
26308c2ecf20Sopenharmony_ci}
2631