xref: /OK3568_Linux_fs/kernel/drivers/net/ethernet/intel/igb/e1000_phy.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /* Copyright(c) 2007 - 2018 Intel Corporation. */
3*4882a593Smuzhiyun 
4*4882a593Smuzhiyun #include <linux/if_ether.h>
5*4882a593Smuzhiyun #include <linux/delay.h>
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include "e1000_mac.h"
8*4882a593Smuzhiyun #include "e1000_phy.h"
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun static s32  igb_phy_setup_autoneg(struct e1000_hw *hw);
11*4882a593Smuzhiyun static void igb_phy_force_speed_duplex_setup(struct e1000_hw *hw,
12*4882a593Smuzhiyun 					     u16 *phy_ctrl);
13*4882a593Smuzhiyun static s32  igb_wait_autoneg(struct e1000_hw *hw);
14*4882a593Smuzhiyun static s32  igb_set_master_slave_mode(struct e1000_hw *hw);
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun /* Cable length tables */
17*4882a593Smuzhiyun static const u16 e1000_m88_cable_length_table[] = {
18*4882a593Smuzhiyun 	0, 50, 80, 110, 140, 140, E1000_CABLE_LENGTH_UNDEFINED };
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun static const u16 e1000_igp_2_cable_length_table[] = {
21*4882a593Smuzhiyun 	0, 0, 0, 0, 0, 0, 0, 0, 3, 5, 8, 11, 13, 16, 18, 21,
22*4882a593Smuzhiyun 	0, 0, 0, 3, 6, 10, 13, 16, 19, 23, 26, 29, 32, 35, 38, 41,
23*4882a593Smuzhiyun 	6, 10, 14, 18, 22, 26, 30, 33, 37, 41, 44, 48, 51, 54, 58, 61,
24*4882a593Smuzhiyun 	21, 26, 31, 35, 40, 44, 49, 53, 57, 61, 65, 68, 72, 75, 79, 82,
25*4882a593Smuzhiyun 	40, 45, 51, 56, 61, 66, 70, 75, 79, 83, 87, 91, 94, 98, 101, 104,
26*4882a593Smuzhiyun 	60, 66, 72, 77, 82, 87, 92, 96, 100, 104, 108, 111, 114, 117, 119, 121,
27*4882a593Smuzhiyun 	83, 89, 95, 100, 105, 109, 113, 116, 119, 122, 124,
28*4882a593Smuzhiyun 	104, 109, 114, 118, 121, 124};
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun /**
31*4882a593Smuzhiyun  *  igb_check_reset_block - Check if PHY reset is blocked
32*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  *  Read the PHY management control register and check whether a PHY reset
35*4882a593Smuzhiyun  *  is blocked.  If a reset is not blocked return 0, otherwise
36*4882a593Smuzhiyun  *  return E1000_BLK_PHY_RESET (12).
37*4882a593Smuzhiyun  **/
igb_check_reset_block(struct e1000_hw * hw)38*4882a593Smuzhiyun s32 igb_check_reset_block(struct e1000_hw *hw)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	u32 manc;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	manc = rd32(E1000_MANC);
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	return (manc & E1000_MANC_BLK_PHY_RST_ON_IDE) ? E1000_BLK_PHY_RESET : 0;
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun /**
48*4882a593Smuzhiyun  *  igb_get_phy_id - Retrieve the PHY ID and revision
49*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
50*4882a593Smuzhiyun  *
51*4882a593Smuzhiyun  *  Reads the PHY registers and stores the PHY ID and possibly the PHY
52*4882a593Smuzhiyun  *  revision in the hardware structure.
53*4882a593Smuzhiyun  **/
igb_get_phy_id(struct e1000_hw * hw)54*4882a593Smuzhiyun s32 igb_get_phy_id(struct e1000_hw *hw)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
57*4882a593Smuzhiyun 	s32 ret_val = 0;
58*4882a593Smuzhiyun 	u16 phy_id;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	/* ensure PHY page selection to fix misconfigured i210 */
61*4882a593Smuzhiyun 	if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211))
62*4882a593Smuzhiyun 		phy->ops.write_reg(hw, I347AT4_PAGE_SELECT, 0);
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, PHY_ID1, &phy_id);
65*4882a593Smuzhiyun 	if (ret_val)
66*4882a593Smuzhiyun 		goto out;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	phy->id = (u32)(phy_id << 16);
69*4882a593Smuzhiyun 	udelay(20);
70*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, PHY_ID2, &phy_id);
71*4882a593Smuzhiyun 	if (ret_val)
72*4882a593Smuzhiyun 		goto out;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	phy->id |= (u32)(phy_id & PHY_REVISION_MASK);
75*4882a593Smuzhiyun 	phy->revision = (u32)(phy_id & ~PHY_REVISION_MASK);
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun out:
78*4882a593Smuzhiyun 	return ret_val;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun /**
82*4882a593Smuzhiyun  *  igb_phy_reset_dsp - Reset PHY DSP
83*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
84*4882a593Smuzhiyun  *
85*4882a593Smuzhiyun  *  Reset the digital signal processor.
86*4882a593Smuzhiyun  **/
igb_phy_reset_dsp(struct e1000_hw * hw)87*4882a593Smuzhiyun static s32 igb_phy_reset_dsp(struct e1000_hw *hw)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun 	s32 ret_val = 0;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	if (!(hw->phy.ops.write_reg))
92*4882a593Smuzhiyun 		goto out;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	ret_val = hw->phy.ops.write_reg(hw, M88E1000_PHY_GEN_CONTROL, 0xC1);
95*4882a593Smuzhiyun 	if (ret_val)
96*4882a593Smuzhiyun 		goto out;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	ret_val = hw->phy.ops.write_reg(hw, M88E1000_PHY_GEN_CONTROL, 0);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun out:
101*4882a593Smuzhiyun 	return ret_val;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun /**
105*4882a593Smuzhiyun  *  igb_read_phy_reg_mdic - Read MDI control register
106*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
107*4882a593Smuzhiyun  *  @offset: register offset to be read
108*4882a593Smuzhiyun  *  @data: pointer to the read data
109*4882a593Smuzhiyun  *
110*4882a593Smuzhiyun  *  Reads the MDI control register in the PHY at offset and stores the
111*4882a593Smuzhiyun  *  information read to data.
112*4882a593Smuzhiyun  **/
igb_read_phy_reg_mdic(struct e1000_hw * hw,u32 offset,u16 * data)113*4882a593Smuzhiyun s32 igb_read_phy_reg_mdic(struct e1000_hw *hw, u32 offset, u16 *data)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
116*4882a593Smuzhiyun 	u32 i, mdic = 0;
117*4882a593Smuzhiyun 	s32 ret_val = 0;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	if (offset > MAX_PHY_REG_ADDRESS) {
120*4882a593Smuzhiyun 		hw_dbg("PHY Address %d is out of range\n", offset);
121*4882a593Smuzhiyun 		ret_val = -E1000_ERR_PARAM;
122*4882a593Smuzhiyun 		goto out;
123*4882a593Smuzhiyun 	}
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	/* Set up Op-code, Phy Address, and register offset in the MDI
126*4882a593Smuzhiyun 	 * Control register.  The MAC will take care of interfacing with the
127*4882a593Smuzhiyun 	 * PHY to retrieve the desired data.
128*4882a593Smuzhiyun 	 */
129*4882a593Smuzhiyun 	mdic = ((offset << E1000_MDIC_REG_SHIFT) |
130*4882a593Smuzhiyun 		(phy->addr << E1000_MDIC_PHY_SHIFT) |
131*4882a593Smuzhiyun 		(E1000_MDIC_OP_READ));
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	wr32(E1000_MDIC, mdic);
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	/* Poll the ready bit to see if the MDI read completed
136*4882a593Smuzhiyun 	 * Increasing the time out as testing showed failures with
137*4882a593Smuzhiyun 	 * the lower time out
138*4882a593Smuzhiyun 	 */
139*4882a593Smuzhiyun 	for (i = 0; i < (E1000_GEN_POLL_TIMEOUT * 3); i++) {
140*4882a593Smuzhiyun 		udelay(50);
141*4882a593Smuzhiyun 		mdic = rd32(E1000_MDIC);
142*4882a593Smuzhiyun 		if (mdic & E1000_MDIC_READY)
143*4882a593Smuzhiyun 			break;
144*4882a593Smuzhiyun 	}
145*4882a593Smuzhiyun 	if (!(mdic & E1000_MDIC_READY)) {
146*4882a593Smuzhiyun 		hw_dbg("MDI Read did not complete\n");
147*4882a593Smuzhiyun 		ret_val = -E1000_ERR_PHY;
148*4882a593Smuzhiyun 		goto out;
149*4882a593Smuzhiyun 	}
150*4882a593Smuzhiyun 	if (mdic & E1000_MDIC_ERROR) {
151*4882a593Smuzhiyun 		hw_dbg("MDI Error\n");
152*4882a593Smuzhiyun 		ret_val = -E1000_ERR_PHY;
153*4882a593Smuzhiyun 		goto out;
154*4882a593Smuzhiyun 	}
155*4882a593Smuzhiyun 	*data = (u16) mdic;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun out:
158*4882a593Smuzhiyun 	return ret_val;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun /**
162*4882a593Smuzhiyun  *  igb_write_phy_reg_mdic - Write MDI control register
163*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
164*4882a593Smuzhiyun  *  @offset: register offset to write to
165*4882a593Smuzhiyun  *  @data: data to write to register at offset
166*4882a593Smuzhiyun  *
167*4882a593Smuzhiyun  *  Writes data to MDI control register in the PHY at offset.
168*4882a593Smuzhiyun  **/
igb_write_phy_reg_mdic(struct e1000_hw * hw,u32 offset,u16 data)169*4882a593Smuzhiyun s32 igb_write_phy_reg_mdic(struct e1000_hw *hw, u32 offset, u16 data)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
172*4882a593Smuzhiyun 	u32 i, mdic = 0;
173*4882a593Smuzhiyun 	s32 ret_val = 0;
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	if (offset > MAX_PHY_REG_ADDRESS) {
176*4882a593Smuzhiyun 		hw_dbg("PHY Address %d is out of range\n", offset);
177*4882a593Smuzhiyun 		ret_val = -E1000_ERR_PARAM;
178*4882a593Smuzhiyun 		goto out;
179*4882a593Smuzhiyun 	}
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	/* Set up Op-code, Phy Address, and register offset in the MDI
182*4882a593Smuzhiyun 	 * Control register.  The MAC will take care of interfacing with the
183*4882a593Smuzhiyun 	 * PHY to retrieve the desired data.
184*4882a593Smuzhiyun 	 */
185*4882a593Smuzhiyun 	mdic = (((u32)data) |
186*4882a593Smuzhiyun 		(offset << E1000_MDIC_REG_SHIFT) |
187*4882a593Smuzhiyun 		(phy->addr << E1000_MDIC_PHY_SHIFT) |
188*4882a593Smuzhiyun 		(E1000_MDIC_OP_WRITE));
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	wr32(E1000_MDIC, mdic);
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	/* Poll the ready bit to see if the MDI read completed
193*4882a593Smuzhiyun 	 * Increasing the time out as testing showed failures with
194*4882a593Smuzhiyun 	 * the lower time out
195*4882a593Smuzhiyun 	 */
196*4882a593Smuzhiyun 	for (i = 0; i < (E1000_GEN_POLL_TIMEOUT * 3); i++) {
197*4882a593Smuzhiyun 		udelay(50);
198*4882a593Smuzhiyun 		mdic = rd32(E1000_MDIC);
199*4882a593Smuzhiyun 		if (mdic & E1000_MDIC_READY)
200*4882a593Smuzhiyun 			break;
201*4882a593Smuzhiyun 	}
202*4882a593Smuzhiyun 	if (!(mdic & E1000_MDIC_READY)) {
203*4882a593Smuzhiyun 		hw_dbg("MDI Write did not complete\n");
204*4882a593Smuzhiyun 		ret_val = -E1000_ERR_PHY;
205*4882a593Smuzhiyun 		goto out;
206*4882a593Smuzhiyun 	}
207*4882a593Smuzhiyun 	if (mdic & E1000_MDIC_ERROR) {
208*4882a593Smuzhiyun 		hw_dbg("MDI Error\n");
209*4882a593Smuzhiyun 		ret_val = -E1000_ERR_PHY;
210*4882a593Smuzhiyun 		goto out;
211*4882a593Smuzhiyun 	}
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun out:
214*4882a593Smuzhiyun 	return ret_val;
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun /**
218*4882a593Smuzhiyun  *  igb_read_phy_reg_i2c - Read PHY register using i2c
219*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
220*4882a593Smuzhiyun  *  @offset: register offset to be read
221*4882a593Smuzhiyun  *  @data: pointer to the read data
222*4882a593Smuzhiyun  *
223*4882a593Smuzhiyun  *  Reads the PHY register at offset using the i2c interface and stores the
224*4882a593Smuzhiyun  *  retrieved information in data.
225*4882a593Smuzhiyun  **/
igb_read_phy_reg_i2c(struct e1000_hw * hw,u32 offset,u16 * data)226*4882a593Smuzhiyun s32 igb_read_phy_reg_i2c(struct e1000_hw *hw, u32 offset, u16 *data)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
229*4882a593Smuzhiyun 	u32 i, i2ccmd = 0;
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	/* Set up Op-code, Phy Address, and register address in the I2CCMD
232*4882a593Smuzhiyun 	 * register.  The MAC will take care of interfacing with the
233*4882a593Smuzhiyun 	 * PHY to retrieve the desired data.
234*4882a593Smuzhiyun 	 */
235*4882a593Smuzhiyun 	i2ccmd = ((offset << E1000_I2CCMD_REG_ADDR_SHIFT) |
236*4882a593Smuzhiyun 		  (phy->addr << E1000_I2CCMD_PHY_ADDR_SHIFT) |
237*4882a593Smuzhiyun 		  (E1000_I2CCMD_OPCODE_READ));
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	wr32(E1000_I2CCMD, i2ccmd);
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	/* Poll the ready bit to see if the I2C read completed */
242*4882a593Smuzhiyun 	for (i = 0; i < E1000_I2CCMD_PHY_TIMEOUT; i++) {
243*4882a593Smuzhiyun 		udelay(50);
244*4882a593Smuzhiyun 		i2ccmd = rd32(E1000_I2CCMD);
245*4882a593Smuzhiyun 		if (i2ccmd & E1000_I2CCMD_READY)
246*4882a593Smuzhiyun 			break;
247*4882a593Smuzhiyun 	}
248*4882a593Smuzhiyun 	if (!(i2ccmd & E1000_I2CCMD_READY)) {
249*4882a593Smuzhiyun 		hw_dbg("I2CCMD Read did not complete\n");
250*4882a593Smuzhiyun 		return -E1000_ERR_PHY;
251*4882a593Smuzhiyun 	}
252*4882a593Smuzhiyun 	if (i2ccmd & E1000_I2CCMD_ERROR) {
253*4882a593Smuzhiyun 		hw_dbg("I2CCMD Error bit set\n");
254*4882a593Smuzhiyun 		return -E1000_ERR_PHY;
255*4882a593Smuzhiyun 	}
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	/* Need to byte-swap the 16-bit value. */
258*4882a593Smuzhiyun 	*data = ((i2ccmd >> 8) & 0x00FF) | ((i2ccmd << 8) & 0xFF00);
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	return 0;
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun /**
264*4882a593Smuzhiyun  *  igb_write_phy_reg_i2c - Write PHY register using i2c
265*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
266*4882a593Smuzhiyun  *  @offset: register offset to write to
267*4882a593Smuzhiyun  *  @data: data to write at register offset
268*4882a593Smuzhiyun  *
269*4882a593Smuzhiyun  *  Writes the data to PHY register at the offset using the i2c interface.
270*4882a593Smuzhiyun  **/
igb_write_phy_reg_i2c(struct e1000_hw * hw,u32 offset,u16 data)271*4882a593Smuzhiyun s32 igb_write_phy_reg_i2c(struct e1000_hw *hw, u32 offset, u16 data)
272*4882a593Smuzhiyun {
273*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
274*4882a593Smuzhiyun 	u32 i, i2ccmd = 0;
275*4882a593Smuzhiyun 	u16 phy_data_swapped;
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	/* Prevent overwriting SFP I2C EEPROM which is at A0 address.*/
278*4882a593Smuzhiyun 	if ((hw->phy.addr == 0) || (hw->phy.addr > 7)) {
279*4882a593Smuzhiyun 		hw_dbg("PHY I2C Address %d is out of range.\n",
280*4882a593Smuzhiyun 			  hw->phy.addr);
281*4882a593Smuzhiyun 		return -E1000_ERR_CONFIG;
282*4882a593Smuzhiyun 	}
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	/* Swap the data bytes for the I2C interface */
285*4882a593Smuzhiyun 	phy_data_swapped = ((data >> 8) & 0x00FF) | ((data << 8) & 0xFF00);
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	/* Set up Op-code, Phy Address, and register address in the I2CCMD
288*4882a593Smuzhiyun 	 * register.  The MAC will take care of interfacing with the
289*4882a593Smuzhiyun 	 * PHY to retrieve the desired data.
290*4882a593Smuzhiyun 	 */
291*4882a593Smuzhiyun 	i2ccmd = ((offset << E1000_I2CCMD_REG_ADDR_SHIFT) |
292*4882a593Smuzhiyun 		  (phy->addr << E1000_I2CCMD_PHY_ADDR_SHIFT) |
293*4882a593Smuzhiyun 		  E1000_I2CCMD_OPCODE_WRITE |
294*4882a593Smuzhiyun 		  phy_data_swapped);
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	wr32(E1000_I2CCMD, i2ccmd);
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	/* Poll the ready bit to see if the I2C read completed */
299*4882a593Smuzhiyun 	for (i = 0; i < E1000_I2CCMD_PHY_TIMEOUT; i++) {
300*4882a593Smuzhiyun 		udelay(50);
301*4882a593Smuzhiyun 		i2ccmd = rd32(E1000_I2CCMD);
302*4882a593Smuzhiyun 		if (i2ccmd & E1000_I2CCMD_READY)
303*4882a593Smuzhiyun 			break;
304*4882a593Smuzhiyun 	}
305*4882a593Smuzhiyun 	if (!(i2ccmd & E1000_I2CCMD_READY)) {
306*4882a593Smuzhiyun 		hw_dbg("I2CCMD Write did not complete\n");
307*4882a593Smuzhiyun 		return -E1000_ERR_PHY;
308*4882a593Smuzhiyun 	}
309*4882a593Smuzhiyun 	if (i2ccmd & E1000_I2CCMD_ERROR) {
310*4882a593Smuzhiyun 		hw_dbg("I2CCMD Error bit set\n");
311*4882a593Smuzhiyun 		return -E1000_ERR_PHY;
312*4882a593Smuzhiyun 	}
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	return 0;
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun /**
318*4882a593Smuzhiyun  *  igb_read_sfp_data_byte - Reads SFP module data.
319*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
320*4882a593Smuzhiyun  *  @offset: byte location offset to be read
321*4882a593Smuzhiyun  *  @data: read data buffer pointer
322*4882a593Smuzhiyun  *
323*4882a593Smuzhiyun  *  Reads one byte from SFP module data stored
324*4882a593Smuzhiyun  *  in SFP resided EEPROM memory or SFP diagnostic area.
325*4882a593Smuzhiyun  *  Function should be called with
326*4882a593Smuzhiyun  *  E1000_I2CCMD_SFP_DATA_ADDR(<byte offset>) for SFP module database access
327*4882a593Smuzhiyun  *  E1000_I2CCMD_SFP_DIAG_ADDR(<byte offset>) for SFP diagnostics parameters
328*4882a593Smuzhiyun  *  access
329*4882a593Smuzhiyun  **/
igb_read_sfp_data_byte(struct e1000_hw * hw,u16 offset,u8 * data)330*4882a593Smuzhiyun s32 igb_read_sfp_data_byte(struct e1000_hw *hw, u16 offset, u8 *data)
331*4882a593Smuzhiyun {
332*4882a593Smuzhiyun 	u32 i = 0;
333*4882a593Smuzhiyun 	u32 i2ccmd = 0;
334*4882a593Smuzhiyun 	u32 data_local = 0;
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	if (offset > E1000_I2CCMD_SFP_DIAG_ADDR(255)) {
337*4882a593Smuzhiyun 		hw_dbg("I2CCMD command address exceeds upper limit\n");
338*4882a593Smuzhiyun 		return -E1000_ERR_PHY;
339*4882a593Smuzhiyun 	}
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	/* Set up Op-code, EEPROM Address,in the I2CCMD
342*4882a593Smuzhiyun 	 * register. The MAC will take care of interfacing with the
343*4882a593Smuzhiyun 	 * EEPROM to retrieve the desired data.
344*4882a593Smuzhiyun 	 */
345*4882a593Smuzhiyun 	i2ccmd = ((offset << E1000_I2CCMD_REG_ADDR_SHIFT) |
346*4882a593Smuzhiyun 		  E1000_I2CCMD_OPCODE_READ);
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	wr32(E1000_I2CCMD, i2ccmd);
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	/* Poll the ready bit to see if the I2C read completed */
351*4882a593Smuzhiyun 	for (i = 0; i < E1000_I2CCMD_PHY_TIMEOUT; i++) {
352*4882a593Smuzhiyun 		udelay(50);
353*4882a593Smuzhiyun 		data_local = rd32(E1000_I2CCMD);
354*4882a593Smuzhiyun 		if (data_local & E1000_I2CCMD_READY)
355*4882a593Smuzhiyun 			break;
356*4882a593Smuzhiyun 	}
357*4882a593Smuzhiyun 	if (!(data_local & E1000_I2CCMD_READY)) {
358*4882a593Smuzhiyun 		hw_dbg("I2CCMD Read did not complete\n");
359*4882a593Smuzhiyun 		return -E1000_ERR_PHY;
360*4882a593Smuzhiyun 	}
361*4882a593Smuzhiyun 	if (data_local & E1000_I2CCMD_ERROR) {
362*4882a593Smuzhiyun 		hw_dbg("I2CCMD Error bit set\n");
363*4882a593Smuzhiyun 		return -E1000_ERR_PHY;
364*4882a593Smuzhiyun 	}
365*4882a593Smuzhiyun 	*data = (u8) data_local & 0xFF;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	return 0;
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun /**
371*4882a593Smuzhiyun  *  igb_read_phy_reg_igp - Read igp PHY register
372*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
373*4882a593Smuzhiyun  *  @offset: register offset to be read
374*4882a593Smuzhiyun  *  @data: pointer to the read data
375*4882a593Smuzhiyun  *
376*4882a593Smuzhiyun  *  Acquires semaphore, if necessary, then reads the PHY register at offset
377*4882a593Smuzhiyun  *  and storing the retrieved information in data.  Release any acquired
378*4882a593Smuzhiyun  *  semaphores before exiting.
379*4882a593Smuzhiyun  **/
igb_read_phy_reg_igp(struct e1000_hw * hw,u32 offset,u16 * data)380*4882a593Smuzhiyun s32 igb_read_phy_reg_igp(struct e1000_hw *hw, u32 offset, u16 *data)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun 	s32 ret_val = 0;
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	if (!(hw->phy.ops.acquire))
385*4882a593Smuzhiyun 		goto out;
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	ret_val = hw->phy.ops.acquire(hw);
388*4882a593Smuzhiyun 	if (ret_val)
389*4882a593Smuzhiyun 		goto out;
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	if (offset > MAX_PHY_MULTI_PAGE_REG) {
392*4882a593Smuzhiyun 		ret_val = igb_write_phy_reg_mdic(hw,
393*4882a593Smuzhiyun 						 IGP01E1000_PHY_PAGE_SELECT,
394*4882a593Smuzhiyun 						 (u16)offset);
395*4882a593Smuzhiyun 		if (ret_val) {
396*4882a593Smuzhiyun 			hw->phy.ops.release(hw);
397*4882a593Smuzhiyun 			goto out;
398*4882a593Smuzhiyun 		}
399*4882a593Smuzhiyun 	}
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	ret_val = igb_read_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & offset,
402*4882a593Smuzhiyun 					data);
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	hw->phy.ops.release(hw);
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun out:
407*4882a593Smuzhiyun 	return ret_val;
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun /**
411*4882a593Smuzhiyun  *  igb_write_phy_reg_igp - Write igp PHY register
412*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
413*4882a593Smuzhiyun  *  @offset: register offset to write to
414*4882a593Smuzhiyun  *  @data: data to write at register offset
415*4882a593Smuzhiyun  *
416*4882a593Smuzhiyun  *  Acquires semaphore, if necessary, then writes the data to PHY register
417*4882a593Smuzhiyun  *  at the offset.  Release any acquired semaphores before exiting.
418*4882a593Smuzhiyun  **/
igb_write_phy_reg_igp(struct e1000_hw * hw,u32 offset,u16 data)419*4882a593Smuzhiyun s32 igb_write_phy_reg_igp(struct e1000_hw *hw, u32 offset, u16 data)
420*4882a593Smuzhiyun {
421*4882a593Smuzhiyun 	s32 ret_val = 0;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	if (!(hw->phy.ops.acquire))
424*4882a593Smuzhiyun 		goto out;
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	ret_val = hw->phy.ops.acquire(hw);
427*4882a593Smuzhiyun 	if (ret_val)
428*4882a593Smuzhiyun 		goto out;
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	if (offset > MAX_PHY_MULTI_PAGE_REG) {
431*4882a593Smuzhiyun 		ret_val = igb_write_phy_reg_mdic(hw,
432*4882a593Smuzhiyun 						 IGP01E1000_PHY_PAGE_SELECT,
433*4882a593Smuzhiyun 						 (u16)offset);
434*4882a593Smuzhiyun 		if (ret_val) {
435*4882a593Smuzhiyun 			hw->phy.ops.release(hw);
436*4882a593Smuzhiyun 			goto out;
437*4882a593Smuzhiyun 		}
438*4882a593Smuzhiyun 	}
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	ret_val = igb_write_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & offset,
441*4882a593Smuzhiyun 					 data);
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun 	hw->phy.ops.release(hw);
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun out:
446*4882a593Smuzhiyun 	return ret_val;
447*4882a593Smuzhiyun }
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun /**
450*4882a593Smuzhiyun  *  igb_copper_link_setup_82580 - Setup 82580 PHY for copper link
451*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
452*4882a593Smuzhiyun  *
453*4882a593Smuzhiyun  *  Sets up Carrier-sense on Transmit and downshift values.
454*4882a593Smuzhiyun  **/
igb_copper_link_setup_82580(struct e1000_hw * hw)455*4882a593Smuzhiyun s32 igb_copper_link_setup_82580(struct e1000_hw *hw)
456*4882a593Smuzhiyun {
457*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
458*4882a593Smuzhiyun 	s32 ret_val;
459*4882a593Smuzhiyun 	u16 phy_data;
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	if (phy->reset_disable) {
462*4882a593Smuzhiyun 		ret_val = 0;
463*4882a593Smuzhiyun 		goto out;
464*4882a593Smuzhiyun 	}
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	if (phy->type == e1000_phy_82580) {
467*4882a593Smuzhiyun 		ret_val = hw->phy.ops.reset(hw);
468*4882a593Smuzhiyun 		if (ret_val) {
469*4882a593Smuzhiyun 			hw_dbg("Error resetting the PHY.\n");
470*4882a593Smuzhiyun 			goto out;
471*4882a593Smuzhiyun 		}
472*4882a593Smuzhiyun 	}
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 	/* Enable CRS on TX. This must be set for half-duplex operation. */
475*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, I82580_CFG_REG, &phy_data);
476*4882a593Smuzhiyun 	if (ret_val)
477*4882a593Smuzhiyun 		goto out;
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	phy_data |= I82580_CFG_ASSERT_CRS_ON_TX;
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun 	/* Enable downshift */
482*4882a593Smuzhiyun 	phy_data |= I82580_CFG_ENABLE_DOWNSHIFT;
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, I82580_CFG_REG, phy_data);
485*4882a593Smuzhiyun 	if (ret_val)
486*4882a593Smuzhiyun 		goto out;
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 	/* Set MDI/MDIX mode */
489*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, I82580_PHY_CTRL_2, &phy_data);
490*4882a593Smuzhiyun 	if (ret_val)
491*4882a593Smuzhiyun 		goto out;
492*4882a593Smuzhiyun 	phy_data &= ~I82580_PHY_CTRL2_MDIX_CFG_MASK;
493*4882a593Smuzhiyun 	/* Options:
494*4882a593Smuzhiyun 	 *   0 - Auto (default)
495*4882a593Smuzhiyun 	 *   1 - MDI mode
496*4882a593Smuzhiyun 	 *   2 - MDI-X mode
497*4882a593Smuzhiyun 	 */
498*4882a593Smuzhiyun 	switch (hw->phy.mdix) {
499*4882a593Smuzhiyun 	case 1:
500*4882a593Smuzhiyun 		break;
501*4882a593Smuzhiyun 	case 2:
502*4882a593Smuzhiyun 		phy_data |= I82580_PHY_CTRL2_MANUAL_MDIX;
503*4882a593Smuzhiyun 		break;
504*4882a593Smuzhiyun 	case 0:
505*4882a593Smuzhiyun 	default:
506*4882a593Smuzhiyun 		phy_data |= I82580_PHY_CTRL2_AUTO_MDI_MDIX;
507*4882a593Smuzhiyun 		break;
508*4882a593Smuzhiyun 	}
509*4882a593Smuzhiyun 	ret_val = hw->phy.ops.write_reg(hw, I82580_PHY_CTRL_2, phy_data);
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun out:
512*4882a593Smuzhiyun 	return ret_val;
513*4882a593Smuzhiyun }
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun /**
516*4882a593Smuzhiyun  *  igb_copper_link_setup_m88 - Setup m88 PHY's for copper link
517*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
518*4882a593Smuzhiyun  *
519*4882a593Smuzhiyun  *  Sets up MDI/MDI-X and polarity for m88 PHY's.  If necessary, transmit clock
520*4882a593Smuzhiyun  *  and downshift values are set also.
521*4882a593Smuzhiyun  **/
igb_copper_link_setup_m88(struct e1000_hw * hw)522*4882a593Smuzhiyun s32 igb_copper_link_setup_m88(struct e1000_hw *hw)
523*4882a593Smuzhiyun {
524*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
525*4882a593Smuzhiyun 	s32 ret_val;
526*4882a593Smuzhiyun 	u16 phy_data;
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	if (phy->reset_disable) {
529*4882a593Smuzhiyun 		ret_val = 0;
530*4882a593Smuzhiyun 		goto out;
531*4882a593Smuzhiyun 	}
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 	/* Enable CRS on TX. This must be set for half-duplex operation. */
534*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
535*4882a593Smuzhiyun 	if (ret_val)
536*4882a593Smuzhiyun 		goto out;
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	/* Options:
541*4882a593Smuzhiyun 	 *   MDI/MDI-X = 0 (default)
542*4882a593Smuzhiyun 	 *   0 - Auto for all speeds
543*4882a593Smuzhiyun 	 *   1 - MDI mode
544*4882a593Smuzhiyun 	 *   2 - MDI-X mode
545*4882a593Smuzhiyun 	 *   3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
546*4882a593Smuzhiyun 	 */
547*4882a593Smuzhiyun 	phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 	switch (phy->mdix) {
550*4882a593Smuzhiyun 	case 1:
551*4882a593Smuzhiyun 		phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
552*4882a593Smuzhiyun 		break;
553*4882a593Smuzhiyun 	case 2:
554*4882a593Smuzhiyun 		phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
555*4882a593Smuzhiyun 		break;
556*4882a593Smuzhiyun 	case 3:
557*4882a593Smuzhiyun 		phy_data |= M88E1000_PSCR_AUTO_X_1000T;
558*4882a593Smuzhiyun 		break;
559*4882a593Smuzhiyun 	case 0:
560*4882a593Smuzhiyun 	default:
561*4882a593Smuzhiyun 		phy_data |= M88E1000_PSCR_AUTO_X_MODE;
562*4882a593Smuzhiyun 		break;
563*4882a593Smuzhiyun 	}
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	/* Options:
566*4882a593Smuzhiyun 	 *   disable_polarity_correction = 0 (default)
567*4882a593Smuzhiyun 	 *       Automatic Correction for Reversed Cable Polarity
568*4882a593Smuzhiyun 	 *   0 - Disabled
569*4882a593Smuzhiyun 	 *   1 - Enabled
570*4882a593Smuzhiyun 	 */
571*4882a593Smuzhiyun 	phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
572*4882a593Smuzhiyun 	if (phy->disable_polarity_correction == 1)
573*4882a593Smuzhiyun 		phy_data |= M88E1000_PSCR_POLARITY_REVERSAL;
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
576*4882a593Smuzhiyun 	if (ret_val)
577*4882a593Smuzhiyun 		goto out;
578*4882a593Smuzhiyun 
579*4882a593Smuzhiyun 	if (phy->revision < E1000_REVISION_4) {
580*4882a593Smuzhiyun 		/* Force TX_CLK in the Extended PHY Specific Control Register
581*4882a593Smuzhiyun 		 * to 25MHz clock.
582*4882a593Smuzhiyun 		 */
583*4882a593Smuzhiyun 		ret_val = phy->ops.read_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL,
584*4882a593Smuzhiyun 					    &phy_data);
585*4882a593Smuzhiyun 		if (ret_val)
586*4882a593Smuzhiyun 			goto out;
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 		phy_data |= M88E1000_EPSCR_TX_CLK_25;
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 		if ((phy->revision == E1000_REVISION_2) &&
591*4882a593Smuzhiyun 		    (phy->id == M88E1111_I_PHY_ID)) {
592*4882a593Smuzhiyun 			/* 82573L PHY - set the downshift counter to 5x. */
593*4882a593Smuzhiyun 			phy_data &= ~M88EC018_EPSCR_DOWNSHIFT_COUNTER_MASK;
594*4882a593Smuzhiyun 			phy_data |= M88EC018_EPSCR_DOWNSHIFT_COUNTER_5X;
595*4882a593Smuzhiyun 		} else {
596*4882a593Smuzhiyun 			/* Configure Master and Slave downshift values */
597*4882a593Smuzhiyun 			phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK |
598*4882a593Smuzhiyun 				      M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
599*4882a593Smuzhiyun 			phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X |
600*4882a593Smuzhiyun 				     M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
601*4882a593Smuzhiyun 		}
602*4882a593Smuzhiyun 		ret_val = phy->ops.write_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL,
603*4882a593Smuzhiyun 					     phy_data);
604*4882a593Smuzhiyun 		if (ret_val)
605*4882a593Smuzhiyun 			goto out;
606*4882a593Smuzhiyun 	}
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	/* Commit the changes. */
609*4882a593Smuzhiyun 	ret_val = igb_phy_sw_reset(hw);
610*4882a593Smuzhiyun 	if (ret_val) {
611*4882a593Smuzhiyun 		hw_dbg("Error committing the PHY changes\n");
612*4882a593Smuzhiyun 		goto out;
613*4882a593Smuzhiyun 	}
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun out:
616*4882a593Smuzhiyun 	return ret_val;
617*4882a593Smuzhiyun }
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun /**
620*4882a593Smuzhiyun  *  igb_copper_link_setup_m88_gen2 - Setup m88 PHY's for copper link
621*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
622*4882a593Smuzhiyun  *
623*4882a593Smuzhiyun  *  Sets up MDI/MDI-X and polarity for i347-AT4, m88e1322 and m88e1112 PHY's.
624*4882a593Smuzhiyun  *  Also enables and sets the downshift parameters.
625*4882a593Smuzhiyun  **/
igb_copper_link_setup_m88_gen2(struct e1000_hw * hw)626*4882a593Smuzhiyun s32 igb_copper_link_setup_m88_gen2(struct e1000_hw *hw)
627*4882a593Smuzhiyun {
628*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
629*4882a593Smuzhiyun 	s32 ret_val;
630*4882a593Smuzhiyun 	u16 phy_data;
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 	if (phy->reset_disable)
633*4882a593Smuzhiyun 		return 0;
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun 	/* Enable CRS on Tx. This must be set for half-duplex operation. */
636*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
637*4882a593Smuzhiyun 	if (ret_val)
638*4882a593Smuzhiyun 		return ret_val;
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun 	/* Options:
641*4882a593Smuzhiyun 	 *   MDI/MDI-X = 0 (default)
642*4882a593Smuzhiyun 	 *   0 - Auto for all speeds
643*4882a593Smuzhiyun 	 *   1 - MDI mode
644*4882a593Smuzhiyun 	 *   2 - MDI-X mode
645*4882a593Smuzhiyun 	 *   3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
646*4882a593Smuzhiyun 	 */
647*4882a593Smuzhiyun 	phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun 	switch (phy->mdix) {
650*4882a593Smuzhiyun 	case 1:
651*4882a593Smuzhiyun 		phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
652*4882a593Smuzhiyun 		break;
653*4882a593Smuzhiyun 	case 2:
654*4882a593Smuzhiyun 		phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
655*4882a593Smuzhiyun 		break;
656*4882a593Smuzhiyun 	case 3:
657*4882a593Smuzhiyun 		/* M88E1112 does not support this mode) */
658*4882a593Smuzhiyun 		if (phy->id != M88E1112_E_PHY_ID) {
659*4882a593Smuzhiyun 			phy_data |= M88E1000_PSCR_AUTO_X_1000T;
660*4882a593Smuzhiyun 			break;
661*4882a593Smuzhiyun 		}
662*4882a593Smuzhiyun 		fallthrough;
663*4882a593Smuzhiyun 	case 0:
664*4882a593Smuzhiyun 	default:
665*4882a593Smuzhiyun 		phy_data |= M88E1000_PSCR_AUTO_X_MODE;
666*4882a593Smuzhiyun 		break;
667*4882a593Smuzhiyun 	}
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 	/* Options:
670*4882a593Smuzhiyun 	 *   disable_polarity_correction = 0 (default)
671*4882a593Smuzhiyun 	 *       Automatic Correction for Reversed Cable Polarity
672*4882a593Smuzhiyun 	 *   0 - Disabled
673*4882a593Smuzhiyun 	 *   1 - Enabled
674*4882a593Smuzhiyun 	 */
675*4882a593Smuzhiyun 	phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
676*4882a593Smuzhiyun 	if (phy->disable_polarity_correction == 1)
677*4882a593Smuzhiyun 		phy_data |= M88E1000_PSCR_POLARITY_REVERSAL;
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun 	/* Enable downshift and setting it to X6 */
680*4882a593Smuzhiyun 	if (phy->id == M88E1543_E_PHY_ID) {
681*4882a593Smuzhiyun 		phy_data &= ~I347AT4_PSCR_DOWNSHIFT_ENABLE;
682*4882a593Smuzhiyun 		ret_val =
683*4882a593Smuzhiyun 		    phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
684*4882a593Smuzhiyun 		if (ret_val)
685*4882a593Smuzhiyun 			return ret_val;
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 		ret_val = igb_phy_sw_reset(hw);
688*4882a593Smuzhiyun 		if (ret_val) {
689*4882a593Smuzhiyun 			hw_dbg("Error committing the PHY changes\n");
690*4882a593Smuzhiyun 			return ret_val;
691*4882a593Smuzhiyun 		}
692*4882a593Smuzhiyun 	}
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun 	phy_data &= ~I347AT4_PSCR_DOWNSHIFT_MASK;
695*4882a593Smuzhiyun 	phy_data |= I347AT4_PSCR_DOWNSHIFT_6X;
696*4882a593Smuzhiyun 	phy_data |= I347AT4_PSCR_DOWNSHIFT_ENABLE;
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
699*4882a593Smuzhiyun 	if (ret_val)
700*4882a593Smuzhiyun 		return ret_val;
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	/* Commit the changes. */
703*4882a593Smuzhiyun 	ret_val = igb_phy_sw_reset(hw);
704*4882a593Smuzhiyun 	if (ret_val) {
705*4882a593Smuzhiyun 		hw_dbg("Error committing the PHY changes\n");
706*4882a593Smuzhiyun 		return ret_val;
707*4882a593Smuzhiyun 	}
708*4882a593Smuzhiyun 	ret_val = igb_set_master_slave_mode(hw);
709*4882a593Smuzhiyun 	if (ret_val)
710*4882a593Smuzhiyun 		return ret_val;
711*4882a593Smuzhiyun 
712*4882a593Smuzhiyun 	return 0;
713*4882a593Smuzhiyun }
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun /**
716*4882a593Smuzhiyun  *  igb_copper_link_setup_igp - Setup igp PHY's for copper link
717*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
718*4882a593Smuzhiyun  *
719*4882a593Smuzhiyun  *  Sets up LPLU, MDI/MDI-X, polarity, Smartspeed and Master/Slave config for
720*4882a593Smuzhiyun  *  igp PHY's.
721*4882a593Smuzhiyun  **/
igb_copper_link_setup_igp(struct e1000_hw * hw)722*4882a593Smuzhiyun s32 igb_copper_link_setup_igp(struct e1000_hw *hw)
723*4882a593Smuzhiyun {
724*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
725*4882a593Smuzhiyun 	s32 ret_val;
726*4882a593Smuzhiyun 	u16 data;
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 	if (phy->reset_disable) {
729*4882a593Smuzhiyun 		ret_val = 0;
730*4882a593Smuzhiyun 		goto out;
731*4882a593Smuzhiyun 	}
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun 	ret_val = phy->ops.reset(hw);
734*4882a593Smuzhiyun 	if (ret_val) {
735*4882a593Smuzhiyun 		hw_dbg("Error resetting the PHY.\n");
736*4882a593Smuzhiyun 		goto out;
737*4882a593Smuzhiyun 	}
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	/* Wait 100ms for MAC to configure PHY from NVM settings, to avoid
740*4882a593Smuzhiyun 	 * timeout issues when LFS is enabled.
741*4882a593Smuzhiyun 	 */
742*4882a593Smuzhiyun 	msleep(100);
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 	/* The NVM settings will configure LPLU in D3 for
745*4882a593Smuzhiyun 	 * non-IGP1 PHYs.
746*4882a593Smuzhiyun 	 */
747*4882a593Smuzhiyun 	if (phy->type == e1000_phy_igp) {
748*4882a593Smuzhiyun 		/* disable lplu d3 during driver init */
749*4882a593Smuzhiyun 		if (phy->ops.set_d3_lplu_state)
750*4882a593Smuzhiyun 			ret_val = phy->ops.set_d3_lplu_state(hw, false);
751*4882a593Smuzhiyun 		if (ret_val) {
752*4882a593Smuzhiyun 			hw_dbg("Error Disabling LPLU D3\n");
753*4882a593Smuzhiyun 			goto out;
754*4882a593Smuzhiyun 		}
755*4882a593Smuzhiyun 	}
756*4882a593Smuzhiyun 
757*4882a593Smuzhiyun 	/* disable lplu d0 during driver init */
758*4882a593Smuzhiyun 	ret_val = phy->ops.set_d0_lplu_state(hw, false);
759*4882a593Smuzhiyun 	if (ret_val) {
760*4882a593Smuzhiyun 		hw_dbg("Error Disabling LPLU D0\n");
761*4882a593Smuzhiyun 		goto out;
762*4882a593Smuzhiyun 	}
763*4882a593Smuzhiyun 	/* Configure mdi-mdix settings */
764*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_CTRL, &data);
765*4882a593Smuzhiyun 	if (ret_val)
766*4882a593Smuzhiyun 		goto out;
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 	data &= ~IGP01E1000_PSCR_AUTO_MDIX;
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 	switch (phy->mdix) {
771*4882a593Smuzhiyun 	case 1:
772*4882a593Smuzhiyun 		data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
773*4882a593Smuzhiyun 		break;
774*4882a593Smuzhiyun 	case 2:
775*4882a593Smuzhiyun 		data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
776*4882a593Smuzhiyun 		break;
777*4882a593Smuzhiyun 	case 0:
778*4882a593Smuzhiyun 	default:
779*4882a593Smuzhiyun 		data |= IGP01E1000_PSCR_AUTO_MDIX;
780*4882a593Smuzhiyun 		break;
781*4882a593Smuzhiyun 	}
782*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, IGP01E1000_PHY_PORT_CTRL, data);
783*4882a593Smuzhiyun 	if (ret_val)
784*4882a593Smuzhiyun 		goto out;
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun 	/* set auto-master slave resolution settings */
787*4882a593Smuzhiyun 	if (hw->mac.autoneg) {
788*4882a593Smuzhiyun 		/* when autonegotiation advertisement is only 1000Mbps then we
789*4882a593Smuzhiyun 		 * should disable SmartSpeed and enable Auto MasterSlave
790*4882a593Smuzhiyun 		 * resolution as hardware default.
791*4882a593Smuzhiyun 		 */
792*4882a593Smuzhiyun 		if (phy->autoneg_advertised == ADVERTISE_1000_FULL) {
793*4882a593Smuzhiyun 			/* Disable SmartSpeed */
794*4882a593Smuzhiyun 			ret_val = phy->ops.read_reg(hw,
795*4882a593Smuzhiyun 						    IGP01E1000_PHY_PORT_CONFIG,
796*4882a593Smuzhiyun 						    &data);
797*4882a593Smuzhiyun 			if (ret_val)
798*4882a593Smuzhiyun 				goto out;
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun 			data &= ~IGP01E1000_PSCFR_SMART_SPEED;
801*4882a593Smuzhiyun 			ret_val = phy->ops.write_reg(hw,
802*4882a593Smuzhiyun 						     IGP01E1000_PHY_PORT_CONFIG,
803*4882a593Smuzhiyun 						     data);
804*4882a593Smuzhiyun 			if (ret_val)
805*4882a593Smuzhiyun 				goto out;
806*4882a593Smuzhiyun 
807*4882a593Smuzhiyun 			/* Set auto Master/Slave resolution process */
808*4882a593Smuzhiyun 			ret_val = phy->ops.read_reg(hw, PHY_1000T_CTRL, &data);
809*4882a593Smuzhiyun 			if (ret_val)
810*4882a593Smuzhiyun 				goto out;
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 			data &= ~CR_1000T_MS_ENABLE;
813*4882a593Smuzhiyun 			ret_val = phy->ops.write_reg(hw, PHY_1000T_CTRL, data);
814*4882a593Smuzhiyun 			if (ret_val)
815*4882a593Smuzhiyun 				goto out;
816*4882a593Smuzhiyun 		}
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun 		ret_val = phy->ops.read_reg(hw, PHY_1000T_CTRL, &data);
819*4882a593Smuzhiyun 		if (ret_val)
820*4882a593Smuzhiyun 			goto out;
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 		/* load defaults for future use */
823*4882a593Smuzhiyun 		phy->original_ms_type = (data & CR_1000T_MS_ENABLE) ?
824*4882a593Smuzhiyun 			((data & CR_1000T_MS_VALUE) ?
825*4882a593Smuzhiyun 			e1000_ms_force_master :
826*4882a593Smuzhiyun 			e1000_ms_force_slave) :
827*4882a593Smuzhiyun 			e1000_ms_auto;
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun 		switch (phy->ms_type) {
830*4882a593Smuzhiyun 		case e1000_ms_force_master:
831*4882a593Smuzhiyun 			data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
832*4882a593Smuzhiyun 			break;
833*4882a593Smuzhiyun 		case e1000_ms_force_slave:
834*4882a593Smuzhiyun 			data |= CR_1000T_MS_ENABLE;
835*4882a593Smuzhiyun 			data &= ~(CR_1000T_MS_VALUE);
836*4882a593Smuzhiyun 			break;
837*4882a593Smuzhiyun 		case e1000_ms_auto:
838*4882a593Smuzhiyun 			data &= ~CR_1000T_MS_ENABLE;
839*4882a593Smuzhiyun 		default:
840*4882a593Smuzhiyun 			break;
841*4882a593Smuzhiyun 		}
842*4882a593Smuzhiyun 		ret_val = phy->ops.write_reg(hw, PHY_1000T_CTRL, data);
843*4882a593Smuzhiyun 		if (ret_val)
844*4882a593Smuzhiyun 			goto out;
845*4882a593Smuzhiyun 	}
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun out:
848*4882a593Smuzhiyun 	return ret_val;
849*4882a593Smuzhiyun }
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun /**
852*4882a593Smuzhiyun  *  igb_copper_link_autoneg - Setup/Enable autoneg for copper link
853*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
854*4882a593Smuzhiyun  *
855*4882a593Smuzhiyun  *  Performs initial bounds checking on autoneg advertisement parameter, then
856*4882a593Smuzhiyun  *  configure to advertise the full capability.  Setup the PHY to autoneg
857*4882a593Smuzhiyun  *  and restart the negotiation process between the link partner.  If
858*4882a593Smuzhiyun  *  autoneg_wait_to_complete, then wait for autoneg to complete before exiting.
859*4882a593Smuzhiyun  **/
igb_copper_link_autoneg(struct e1000_hw * hw)860*4882a593Smuzhiyun static s32 igb_copper_link_autoneg(struct e1000_hw *hw)
861*4882a593Smuzhiyun {
862*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
863*4882a593Smuzhiyun 	s32 ret_val;
864*4882a593Smuzhiyun 	u16 phy_ctrl;
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	/* Perform some bounds checking on the autoneg advertisement
867*4882a593Smuzhiyun 	 * parameter.
868*4882a593Smuzhiyun 	 */
869*4882a593Smuzhiyun 	phy->autoneg_advertised &= phy->autoneg_mask;
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 	/* If autoneg_advertised is zero, we assume it was not defaulted
872*4882a593Smuzhiyun 	 * by the calling code so we set to advertise full capability.
873*4882a593Smuzhiyun 	 */
874*4882a593Smuzhiyun 	if (phy->autoneg_advertised == 0)
875*4882a593Smuzhiyun 		phy->autoneg_advertised = phy->autoneg_mask;
876*4882a593Smuzhiyun 
877*4882a593Smuzhiyun 	hw_dbg("Reconfiguring auto-neg advertisement params\n");
878*4882a593Smuzhiyun 	ret_val = igb_phy_setup_autoneg(hw);
879*4882a593Smuzhiyun 	if (ret_val) {
880*4882a593Smuzhiyun 		hw_dbg("Error Setting up Auto-Negotiation\n");
881*4882a593Smuzhiyun 		goto out;
882*4882a593Smuzhiyun 	}
883*4882a593Smuzhiyun 	hw_dbg("Restarting Auto-Neg\n");
884*4882a593Smuzhiyun 
885*4882a593Smuzhiyun 	/* Restart auto-negotiation by setting the Auto Neg Enable bit and
886*4882a593Smuzhiyun 	 * the Auto Neg Restart bit in the PHY control register.
887*4882a593Smuzhiyun 	 */
888*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_ctrl);
889*4882a593Smuzhiyun 	if (ret_val)
890*4882a593Smuzhiyun 		goto out;
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun 	phy_ctrl |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
893*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_ctrl);
894*4882a593Smuzhiyun 	if (ret_val)
895*4882a593Smuzhiyun 		goto out;
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 	/* Does the user want to wait for Auto-Neg to complete here, or
898*4882a593Smuzhiyun 	 * check at a later time (for example, callback routine).
899*4882a593Smuzhiyun 	 */
900*4882a593Smuzhiyun 	if (phy->autoneg_wait_to_complete) {
901*4882a593Smuzhiyun 		ret_val = igb_wait_autoneg(hw);
902*4882a593Smuzhiyun 		if (ret_val) {
903*4882a593Smuzhiyun 			hw_dbg("Error while waiting for autoneg to complete\n");
904*4882a593Smuzhiyun 			goto out;
905*4882a593Smuzhiyun 		}
906*4882a593Smuzhiyun 	}
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 	hw->mac.get_link_status = true;
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun out:
911*4882a593Smuzhiyun 	return ret_val;
912*4882a593Smuzhiyun }
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun /**
915*4882a593Smuzhiyun  *  igb_phy_setup_autoneg - Configure PHY for auto-negotiation
916*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
917*4882a593Smuzhiyun  *
918*4882a593Smuzhiyun  *  Reads the MII auto-neg advertisement register and/or the 1000T control
919*4882a593Smuzhiyun  *  register and if the PHY is already setup for auto-negotiation, then
920*4882a593Smuzhiyun  *  return successful.  Otherwise, setup advertisement and flow control to
921*4882a593Smuzhiyun  *  the appropriate values for the wanted auto-negotiation.
922*4882a593Smuzhiyun  **/
igb_phy_setup_autoneg(struct e1000_hw * hw)923*4882a593Smuzhiyun static s32 igb_phy_setup_autoneg(struct e1000_hw *hw)
924*4882a593Smuzhiyun {
925*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
926*4882a593Smuzhiyun 	s32 ret_val;
927*4882a593Smuzhiyun 	u16 mii_autoneg_adv_reg;
928*4882a593Smuzhiyun 	u16 mii_1000t_ctrl_reg = 0;
929*4882a593Smuzhiyun 
930*4882a593Smuzhiyun 	phy->autoneg_advertised &= phy->autoneg_mask;
931*4882a593Smuzhiyun 
932*4882a593Smuzhiyun 	/* Read the MII Auto-Neg Advertisement Register (Address 4). */
933*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
934*4882a593Smuzhiyun 	if (ret_val)
935*4882a593Smuzhiyun 		goto out;
936*4882a593Smuzhiyun 
937*4882a593Smuzhiyun 	if (phy->autoneg_mask & ADVERTISE_1000_FULL) {
938*4882a593Smuzhiyun 		/* Read the MII 1000Base-T Control Register (Address 9). */
939*4882a593Smuzhiyun 		ret_val = phy->ops.read_reg(hw, PHY_1000T_CTRL,
940*4882a593Smuzhiyun 					    &mii_1000t_ctrl_reg);
941*4882a593Smuzhiyun 		if (ret_val)
942*4882a593Smuzhiyun 			goto out;
943*4882a593Smuzhiyun 	}
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun 	/* Need to parse both autoneg_advertised and fc and set up
946*4882a593Smuzhiyun 	 * the appropriate PHY registers.  First we will parse for
947*4882a593Smuzhiyun 	 * autoneg_advertised software override.  Since we can advertise
948*4882a593Smuzhiyun 	 * a plethora of combinations, we need to check each bit
949*4882a593Smuzhiyun 	 * individually.
950*4882a593Smuzhiyun 	 */
951*4882a593Smuzhiyun 
952*4882a593Smuzhiyun 	/* First we clear all the 10/100 mb speed bits in the Auto-Neg
953*4882a593Smuzhiyun 	 * Advertisement Register (Address 4) and the 1000 mb speed bits in
954*4882a593Smuzhiyun 	 * the  1000Base-T Control Register (Address 9).
955*4882a593Smuzhiyun 	 */
956*4882a593Smuzhiyun 	mii_autoneg_adv_reg &= ~(NWAY_AR_100TX_FD_CAPS |
957*4882a593Smuzhiyun 				 NWAY_AR_100TX_HD_CAPS |
958*4882a593Smuzhiyun 				 NWAY_AR_10T_FD_CAPS   |
959*4882a593Smuzhiyun 				 NWAY_AR_10T_HD_CAPS);
960*4882a593Smuzhiyun 	mii_1000t_ctrl_reg &= ~(CR_1000T_HD_CAPS | CR_1000T_FD_CAPS);
961*4882a593Smuzhiyun 
962*4882a593Smuzhiyun 	hw_dbg("autoneg_advertised %x\n", phy->autoneg_advertised);
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun 	/* Do we want to advertise 10 Mb Half Duplex? */
965*4882a593Smuzhiyun 	if (phy->autoneg_advertised & ADVERTISE_10_HALF) {
966*4882a593Smuzhiyun 		hw_dbg("Advertise 10mb Half duplex\n");
967*4882a593Smuzhiyun 		mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
968*4882a593Smuzhiyun 	}
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun 	/* Do we want to advertise 10 Mb Full Duplex? */
971*4882a593Smuzhiyun 	if (phy->autoneg_advertised & ADVERTISE_10_FULL) {
972*4882a593Smuzhiyun 		hw_dbg("Advertise 10mb Full duplex\n");
973*4882a593Smuzhiyun 		mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
974*4882a593Smuzhiyun 	}
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun 	/* Do we want to advertise 100 Mb Half Duplex? */
977*4882a593Smuzhiyun 	if (phy->autoneg_advertised & ADVERTISE_100_HALF) {
978*4882a593Smuzhiyun 		hw_dbg("Advertise 100mb Half duplex\n");
979*4882a593Smuzhiyun 		mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
980*4882a593Smuzhiyun 	}
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun 	/* Do we want to advertise 100 Mb Full Duplex? */
983*4882a593Smuzhiyun 	if (phy->autoneg_advertised & ADVERTISE_100_FULL) {
984*4882a593Smuzhiyun 		hw_dbg("Advertise 100mb Full duplex\n");
985*4882a593Smuzhiyun 		mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
986*4882a593Smuzhiyun 	}
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun 	/* We do not allow the Phy to advertise 1000 Mb Half Duplex */
989*4882a593Smuzhiyun 	if (phy->autoneg_advertised & ADVERTISE_1000_HALF)
990*4882a593Smuzhiyun 		hw_dbg("Advertise 1000mb Half duplex request denied!\n");
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun 	/* Do we want to advertise 1000 Mb Full Duplex? */
993*4882a593Smuzhiyun 	if (phy->autoneg_advertised & ADVERTISE_1000_FULL) {
994*4882a593Smuzhiyun 		hw_dbg("Advertise 1000mb Full duplex\n");
995*4882a593Smuzhiyun 		mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
996*4882a593Smuzhiyun 	}
997*4882a593Smuzhiyun 
998*4882a593Smuzhiyun 	/* Check for a software override of the flow control settings, and
999*4882a593Smuzhiyun 	 * setup the PHY advertisement registers accordingly.  If
1000*4882a593Smuzhiyun 	 * auto-negotiation is enabled, then software will have to set the
1001*4882a593Smuzhiyun 	 * "PAUSE" bits to the correct value in the Auto-Negotiation
1002*4882a593Smuzhiyun 	 * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-
1003*4882a593Smuzhiyun 	 * negotiation.
1004*4882a593Smuzhiyun 	 *
1005*4882a593Smuzhiyun 	 * The possible values of the "fc" parameter are:
1006*4882a593Smuzhiyun 	 *      0:  Flow control is completely disabled
1007*4882a593Smuzhiyun 	 *      1:  Rx flow control is enabled (we can receive pause frames
1008*4882a593Smuzhiyun 	 *          but not send pause frames).
1009*4882a593Smuzhiyun 	 *      2:  Tx flow control is enabled (we can send pause frames
1010*4882a593Smuzhiyun 	 *          but we do not support receiving pause frames).
1011*4882a593Smuzhiyun 	 *      3:  Both Rx and TX flow control (symmetric) are enabled.
1012*4882a593Smuzhiyun 	 *  other:  No software override.  The flow control configuration
1013*4882a593Smuzhiyun 	 *          in the EEPROM is used.
1014*4882a593Smuzhiyun 	 */
1015*4882a593Smuzhiyun 	switch (hw->fc.current_mode) {
1016*4882a593Smuzhiyun 	case e1000_fc_none:
1017*4882a593Smuzhiyun 		/* Flow control (RX & TX) is completely disabled by a
1018*4882a593Smuzhiyun 		 * software over-ride.
1019*4882a593Smuzhiyun 		 */
1020*4882a593Smuzhiyun 		mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1021*4882a593Smuzhiyun 		break;
1022*4882a593Smuzhiyun 	case e1000_fc_rx_pause:
1023*4882a593Smuzhiyun 		/* RX Flow control is enabled, and TX Flow control is
1024*4882a593Smuzhiyun 		 * disabled, by a software over-ride.
1025*4882a593Smuzhiyun 		 *
1026*4882a593Smuzhiyun 		 * Since there really isn't a way to advertise that we are
1027*4882a593Smuzhiyun 		 * capable of RX Pause ONLY, we will advertise that we
1028*4882a593Smuzhiyun 		 * support both symmetric and asymmetric RX PAUSE.  Later
1029*4882a593Smuzhiyun 		 * (in e1000_config_fc_after_link_up) we will disable the
1030*4882a593Smuzhiyun 		 * hw's ability to send PAUSE frames.
1031*4882a593Smuzhiyun 		 */
1032*4882a593Smuzhiyun 		mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1033*4882a593Smuzhiyun 		break;
1034*4882a593Smuzhiyun 	case e1000_fc_tx_pause:
1035*4882a593Smuzhiyun 		/* TX Flow control is enabled, and RX Flow control is
1036*4882a593Smuzhiyun 		 * disabled, by a software over-ride.
1037*4882a593Smuzhiyun 		 */
1038*4882a593Smuzhiyun 		mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
1039*4882a593Smuzhiyun 		mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
1040*4882a593Smuzhiyun 		break;
1041*4882a593Smuzhiyun 	case e1000_fc_full:
1042*4882a593Smuzhiyun 		/* Flow control (both RX and TX) is enabled by a software
1043*4882a593Smuzhiyun 		 * over-ride.
1044*4882a593Smuzhiyun 		 */
1045*4882a593Smuzhiyun 		mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1046*4882a593Smuzhiyun 		break;
1047*4882a593Smuzhiyun 	default:
1048*4882a593Smuzhiyun 		hw_dbg("Flow control param set incorrectly\n");
1049*4882a593Smuzhiyun 		ret_val = -E1000_ERR_CONFIG;
1050*4882a593Smuzhiyun 		goto out;
1051*4882a593Smuzhiyun 	}
1052*4882a593Smuzhiyun 
1053*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
1054*4882a593Smuzhiyun 	if (ret_val)
1055*4882a593Smuzhiyun 		goto out;
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun 	hw_dbg("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
1058*4882a593Smuzhiyun 
1059*4882a593Smuzhiyun 	if (phy->autoneg_mask & ADVERTISE_1000_FULL) {
1060*4882a593Smuzhiyun 		ret_val = phy->ops.write_reg(hw,
1061*4882a593Smuzhiyun 					     PHY_1000T_CTRL,
1062*4882a593Smuzhiyun 					     mii_1000t_ctrl_reg);
1063*4882a593Smuzhiyun 		if (ret_val)
1064*4882a593Smuzhiyun 			goto out;
1065*4882a593Smuzhiyun 	}
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun out:
1068*4882a593Smuzhiyun 	return ret_val;
1069*4882a593Smuzhiyun }
1070*4882a593Smuzhiyun 
1071*4882a593Smuzhiyun /**
1072*4882a593Smuzhiyun  *  igb_setup_copper_link - Configure copper link settings
1073*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1074*4882a593Smuzhiyun  *
1075*4882a593Smuzhiyun  *  Calls the appropriate function to configure the link for auto-neg or forced
1076*4882a593Smuzhiyun  *  speed and duplex.  Then we check for link, once link is established calls
1077*4882a593Smuzhiyun  *  to configure collision distance and flow control are called.  If link is
1078*4882a593Smuzhiyun  *  not established, we return -E1000_ERR_PHY (-2).
1079*4882a593Smuzhiyun  **/
igb_setup_copper_link(struct e1000_hw * hw)1080*4882a593Smuzhiyun s32 igb_setup_copper_link(struct e1000_hw *hw)
1081*4882a593Smuzhiyun {
1082*4882a593Smuzhiyun 	s32 ret_val;
1083*4882a593Smuzhiyun 	bool link;
1084*4882a593Smuzhiyun 
1085*4882a593Smuzhiyun 	if (hw->mac.autoneg) {
1086*4882a593Smuzhiyun 		/* Setup autoneg and flow control advertisement and perform
1087*4882a593Smuzhiyun 		 * autonegotiation.
1088*4882a593Smuzhiyun 		 */
1089*4882a593Smuzhiyun 		ret_val = igb_copper_link_autoneg(hw);
1090*4882a593Smuzhiyun 		if (ret_val)
1091*4882a593Smuzhiyun 			goto out;
1092*4882a593Smuzhiyun 	} else {
1093*4882a593Smuzhiyun 		/* PHY will be set to 10H, 10F, 100H or 100F
1094*4882a593Smuzhiyun 		 * depending on user settings.
1095*4882a593Smuzhiyun 		 */
1096*4882a593Smuzhiyun 		hw_dbg("Forcing Speed and Duplex\n");
1097*4882a593Smuzhiyun 		ret_val = hw->phy.ops.force_speed_duplex(hw);
1098*4882a593Smuzhiyun 		if (ret_val) {
1099*4882a593Smuzhiyun 			hw_dbg("Error Forcing Speed and Duplex\n");
1100*4882a593Smuzhiyun 			goto out;
1101*4882a593Smuzhiyun 		}
1102*4882a593Smuzhiyun 	}
1103*4882a593Smuzhiyun 
1104*4882a593Smuzhiyun 	/* Check link status. Wait up to 100 microseconds for link to become
1105*4882a593Smuzhiyun 	 * valid.
1106*4882a593Smuzhiyun 	 */
1107*4882a593Smuzhiyun 	ret_val = igb_phy_has_link(hw, COPPER_LINK_UP_LIMIT, 10, &link);
1108*4882a593Smuzhiyun 	if (ret_val)
1109*4882a593Smuzhiyun 		goto out;
1110*4882a593Smuzhiyun 
1111*4882a593Smuzhiyun 	if (link) {
1112*4882a593Smuzhiyun 		hw_dbg("Valid link established!!!\n");
1113*4882a593Smuzhiyun 		igb_config_collision_dist(hw);
1114*4882a593Smuzhiyun 		ret_val = igb_config_fc_after_link_up(hw);
1115*4882a593Smuzhiyun 	} else {
1116*4882a593Smuzhiyun 		hw_dbg("Unable to establish link!!!\n");
1117*4882a593Smuzhiyun 	}
1118*4882a593Smuzhiyun 
1119*4882a593Smuzhiyun out:
1120*4882a593Smuzhiyun 	return ret_val;
1121*4882a593Smuzhiyun }
1122*4882a593Smuzhiyun 
1123*4882a593Smuzhiyun /**
1124*4882a593Smuzhiyun  *  igb_phy_force_speed_duplex_igp - Force speed/duplex for igp PHY
1125*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1126*4882a593Smuzhiyun  *
1127*4882a593Smuzhiyun  *  Calls the PHY setup function to force speed and duplex.  Clears the
1128*4882a593Smuzhiyun  *  auto-crossover to force MDI manually.  Waits for link and returns
1129*4882a593Smuzhiyun  *  successful if link up is successful, else -E1000_ERR_PHY (-2).
1130*4882a593Smuzhiyun  **/
igb_phy_force_speed_duplex_igp(struct e1000_hw * hw)1131*4882a593Smuzhiyun s32 igb_phy_force_speed_duplex_igp(struct e1000_hw *hw)
1132*4882a593Smuzhiyun {
1133*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1134*4882a593Smuzhiyun 	s32 ret_val;
1135*4882a593Smuzhiyun 	u16 phy_data;
1136*4882a593Smuzhiyun 	bool link;
1137*4882a593Smuzhiyun 
1138*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_data);
1139*4882a593Smuzhiyun 	if (ret_val)
1140*4882a593Smuzhiyun 		goto out;
1141*4882a593Smuzhiyun 
1142*4882a593Smuzhiyun 	igb_phy_force_speed_duplex_setup(hw, &phy_data);
1143*4882a593Smuzhiyun 
1144*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_data);
1145*4882a593Smuzhiyun 	if (ret_val)
1146*4882a593Smuzhiyun 		goto out;
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun 	/* Clear Auto-Crossover to force MDI manually.  IGP requires MDI
1149*4882a593Smuzhiyun 	 * forced whenever speed and duplex are forced.
1150*4882a593Smuzhiyun 	 */
1151*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
1152*4882a593Smuzhiyun 	if (ret_val)
1153*4882a593Smuzhiyun 		goto out;
1154*4882a593Smuzhiyun 
1155*4882a593Smuzhiyun 	phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
1156*4882a593Smuzhiyun 	phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
1157*4882a593Smuzhiyun 
1158*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
1159*4882a593Smuzhiyun 	if (ret_val)
1160*4882a593Smuzhiyun 		goto out;
1161*4882a593Smuzhiyun 
1162*4882a593Smuzhiyun 	hw_dbg("IGP PSCR: %X\n", phy_data);
1163*4882a593Smuzhiyun 
1164*4882a593Smuzhiyun 	udelay(1);
1165*4882a593Smuzhiyun 
1166*4882a593Smuzhiyun 	if (phy->autoneg_wait_to_complete) {
1167*4882a593Smuzhiyun 		hw_dbg("Waiting for forced speed/duplex link on IGP phy.\n");
1168*4882a593Smuzhiyun 
1169*4882a593Smuzhiyun 		ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT, 10000, &link);
1170*4882a593Smuzhiyun 		if (ret_val)
1171*4882a593Smuzhiyun 			goto out;
1172*4882a593Smuzhiyun 
1173*4882a593Smuzhiyun 		if (!link)
1174*4882a593Smuzhiyun 			hw_dbg("Link taking longer than expected.\n");
1175*4882a593Smuzhiyun 
1176*4882a593Smuzhiyun 		/* Try once more */
1177*4882a593Smuzhiyun 		ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT, 10000, &link);
1178*4882a593Smuzhiyun 		if (ret_val)
1179*4882a593Smuzhiyun 			goto out;
1180*4882a593Smuzhiyun 	}
1181*4882a593Smuzhiyun 
1182*4882a593Smuzhiyun out:
1183*4882a593Smuzhiyun 	return ret_val;
1184*4882a593Smuzhiyun }
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun /**
1187*4882a593Smuzhiyun  *  igb_phy_force_speed_duplex_m88 - Force speed/duplex for m88 PHY
1188*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1189*4882a593Smuzhiyun  *
1190*4882a593Smuzhiyun  *  Calls the PHY setup function to force speed and duplex.  Clears the
1191*4882a593Smuzhiyun  *  auto-crossover to force MDI manually.  Resets the PHY to commit the
1192*4882a593Smuzhiyun  *  changes.  If time expires while waiting for link up, we reset the DSP.
1193*4882a593Smuzhiyun  *  After reset, TX_CLK and CRS on TX must be set.  Return successful upon
1194*4882a593Smuzhiyun  *  successful completion, else return corresponding error code.
1195*4882a593Smuzhiyun  **/
igb_phy_force_speed_duplex_m88(struct e1000_hw * hw)1196*4882a593Smuzhiyun s32 igb_phy_force_speed_duplex_m88(struct e1000_hw *hw)
1197*4882a593Smuzhiyun {
1198*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1199*4882a593Smuzhiyun 	s32 ret_val;
1200*4882a593Smuzhiyun 	u16 phy_data;
1201*4882a593Smuzhiyun 	bool link;
1202*4882a593Smuzhiyun 
1203*4882a593Smuzhiyun 	/* I210 and I211 devices support Auto-Crossover in forced operation. */
1204*4882a593Smuzhiyun 	if (phy->type != e1000_phy_i210) {
1205*4882a593Smuzhiyun 		/* Clear Auto-Crossover to force MDI manually.  M88E1000
1206*4882a593Smuzhiyun 		 * requires MDI forced whenever speed and duplex are forced.
1207*4882a593Smuzhiyun 		 */
1208*4882a593Smuzhiyun 		ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL,
1209*4882a593Smuzhiyun 					    &phy_data);
1210*4882a593Smuzhiyun 		if (ret_val)
1211*4882a593Smuzhiyun 			goto out;
1212*4882a593Smuzhiyun 
1213*4882a593Smuzhiyun 		phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
1214*4882a593Smuzhiyun 		ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL,
1215*4882a593Smuzhiyun 					     phy_data);
1216*4882a593Smuzhiyun 		if (ret_val)
1217*4882a593Smuzhiyun 			goto out;
1218*4882a593Smuzhiyun 
1219*4882a593Smuzhiyun 		hw_dbg("M88E1000 PSCR: %X\n", phy_data);
1220*4882a593Smuzhiyun 	}
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_data);
1223*4882a593Smuzhiyun 	if (ret_val)
1224*4882a593Smuzhiyun 		goto out;
1225*4882a593Smuzhiyun 
1226*4882a593Smuzhiyun 	igb_phy_force_speed_duplex_setup(hw, &phy_data);
1227*4882a593Smuzhiyun 
1228*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_data);
1229*4882a593Smuzhiyun 	if (ret_val)
1230*4882a593Smuzhiyun 		goto out;
1231*4882a593Smuzhiyun 
1232*4882a593Smuzhiyun 	/* Reset the phy to commit changes. */
1233*4882a593Smuzhiyun 	ret_val = igb_phy_sw_reset(hw);
1234*4882a593Smuzhiyun 	if (ret_val)
1235*4882a593Smuzhiyun 		goto out;
1236*4882a593Smuzhiyun 
1237*4882a593Smuzhiyun 	if (phy->autoneg_wait_to_complete) {
1238*4882a593Smuzhiyun 		hw_dbg("Waiting for forced speed/duplex link on M88 phy.\n");
1239*4882a593Smuzhiyun 
1240*4882a593Smuzhiyun 		ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT, 100000, &link);
1241*4882a593Smuzhiyun 		if (ret_val)
1242*4882a593Smuzhiyun 			goto out;
1243*4882a593Smuzhiyun 
1244*4882a593Smuzhiyun 		if (!link) {
1245*4882a593Smuzhiyun 			bool reset_dsp = true;
1246*4882a593Smuzhiyun 
1247*4882a593Smuzhiyun 			switch (hw->phy.id) {
1248*4882a593Smuzhiyun 			case I347AT4_E_PHY_ID:
1249*4882a593Smuzhiyun 			case M88E1112_E_PHY_ID:
1250*4882a593Smuzhiyun 			case M88E1543_E_PHY_ID:
1251*4882a593Smuzhiyun 			case M88E1512_E_PHY_ID:
1252*4882a593Smuzhiyun 			case I210_I_PHY_ID:
1253*4882a593Smuzhiyun 				reset_dsp = false;
1254*4882a593Smuzhiyun 				break;
1255*4882a593Smuzhiyun 			default:
1256*4882a593Smuzhiyun 				if (hw->phy.type != e1000_phy_m88)
1257*4882a593Smuzhiyun 					reset_dsp = false;
1258*4882a593Smuzhiyun 				break;
1259*4882a593Smuzhiyun 			}
1260*4882a593Smuzhiyun 			if (!reset_dsp) {
1261*4882a593Smuzhiyun 				hw_dbg("Link taking longer than expected.\n");
1262*4882a593Smuzhiyun 			} else {
1263*4882a593Smuzhiyun 				/* We didn't get link.
1264*4882a593Smuzhiyun 				 * Reset the DSP and cross our fingers.
1265*4882a593Smuzhiyun 				 */
1266*4882a593Smuzhiyun 				ret_val = phy->ops.write_reg(hw,
1267*4882a593Smuzhiyun 						M88E1000_PHY_PAGE_SELECT,
1268*4882a593Smuzhiyun 						0x001d);
1269*4882a593Smuzhiyun 				if (ret_val)
1270*4882a593Smuzhiyun 					goto out;
1271*4882a593Smuzhiyun 				ret_val = igb_phy_reset_dsp(hw);
1272*4882a593Smuzhiyun 				if (ret_val)
1273*4882a593Smuzhiyun 					goto out;
1274*4882a593Smuzhiyun 			}
1275*4882a593Smuzhiyun 		}
1276*4882a593Smuzhiyun 
1277*4882a593Smuzhiyun 		/* Try once more */
1278*4882a593Smuzhiyun 		ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT,
1279*4882a593Smuzhiyun 					   100000, &link);
1280*4882a593Smuzhiyun 		if (ret_val)
1281*4882a593Smuzhiyun 			goto out;
1282*4882a593Smuzhiyun 	}
1283*4882a593Smuzhiyun 
1284*4882a593Smuzhiyun 	if (hw->phy.type != e1000_phy_m88 ||
1285*4882a593Smuzhiyun 	    hw->phy.id == I347AT4_E_PHY_ID ||
1286*4882a593Smuzhiyun 	    hw->phy.id == M88E1112_E_PHY_ID ||
1287*4882a593Smuzhiyun 	    hw->phy.id == M88E1543_E_PHY_ID ||
1288*4882a593Smuzhiyun 	    hw->phy.id == M88E1512_E_PHY_ID ||
1289*4882a593Smuzhiyun 	    hw->phy.id == I210_I_PHY_ID)
1290*4882a593Smuzhiyun 		goto out;
1291*4882a593Smuzhiyun 
1292*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
1293*4882a593Smuzhiyun 	if (ret_val)
1294*4882a593Smuzhiyun 		goto out;
1295*4882a593Smuzhiyun 
1296*4882a593Smuzhiyun 	/* Resetting the phy means we need to re-force TX_CLK in the
1297*4882a593Smuzhiyun 	 * Extended PHY Specific Control Register to 25MHz clock from
1298*4882a593Smuzhiyun 	 * the reset value of 2.5MHz.
1299*4882a593Smuzhiyun 	 */
1300*4882a593Smuzhiyun 	phy_data |= M88E1000_EPSCR_TX_CLK_25;
1301*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
1302*4882a593Smuzhiyun 	if (ret_val)
1303*4882a593Smuzhiyun 		goto out;
1304*4882a593Smuzhiyun 
1305*4882a593Smuzhiyun 	/* In addition, we must re-enable CRS on Tx for both half and full
1306*4882a593Smuzhiyun 	 * duplex.
1307*4882a593Smuzhiyun 	 */
1308*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1309*4882a593Smuzhiyun 	if (ret_val)
1310*4882a593Smuzhiyun 		goto out;
1311*4882a593Smuzhiyun 
1312*4882a593Smuzhiyun 	phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
1313*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1314*4882a593Smuzhiyun 
1315*4882a593Smuzhiyun out:
1316*4882a593Smuzhiyun 	return ret_val;
1317*4882a593Smuzhiyun }
1318*4882a593Smuzhiyun 
1319*4882a593Smuzhiyun /**
1320*4882a593Smuzhiyun  *  igb_phy_force_speed_duplex_setup - Configure forced PHY speed/duplex
1321*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1322*4882a593Smuzhiyun  *  @phy_ctrl: pointer to current value of PHY_CONTROL
1323*4882a593Smuzhiyun  *
1324*4882a593Smuzhiyun  *  Forces speed and duplex on the PHY by doing the following: disable flow
1325*4882a593Smuzhiyun  *  control, force speed/duplex on the MAC, disable auto speed detection,
1326*4882a593Smuzhiyun  *  disable auto-negotiation, configure duplex, configure speed, configure
1327*4882a593Smuzhiyun  *  the collision distance, write configuration to CTRL register.  The
1328*4882a593Smuzhiyun  *  caller must write to the PHY_CONTROL register for these settings to
1329*4882a593Smuzhiyun  *  take affect.
1330*4882a593Smuzhiyun  **/
igb_phy_force_speed_duplex_setup(struct e1000_hw * hw,u16 * phy_ctrl)1331*4882a593Smuzhiyun static void igb_phy_force_speed_duplex_setup(struct e1000_hw *hw,
1332*4882a593Smuzhiyun 					     u16 *phy_ctrl)
1333*4882a593Smuzhiyun {
1334*4882a593Smuzhiyun 	struct e1000_mac_info *mac = &hw->mac;
1335*4882a593Smuzhiyun 	u32 ctrl;
1336*4882a593Smuzhiyun 
1337*4882a593Smuzhiyun 	/* Turn off flow control when forcing speed/duplex */
1338*4882a593Smuzhiyun 	hw->fc.current_mode = e1000_fc_none;
1339*4882a593Smuzhiyun 
1340*4882a593Smuzhiyun 	/* Force speed/duplex on the mac */
1341*4882a593Smuzhiyun 	ctrl = rd32(E1000_CTRL);
1342*4882a593Smuzhiyun 	ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
1343*4882a593Smuzhiyun 	ctrl &= ~E1000_CTRL_SPD_SEL;
1344*4882a593Smuzhiyun 
1345*4882a593Smuzhiyun 	/* Disable Auto Speed Detection */
1346*4882a593Smuzhiyun 	ctrl &= ~E1000_CTRL_ASDE;
1347*4882a593Smuzhiyun 
1348*4882a593Smuzhiyun 	/* Disable autoneg on the phy */
1349*4882a593Smuzhiyun 	*phy_ctrl &= ~MII_CR_AUTO_NEG_EN;
1350*4882a593Smuzhiyun 
1351*4882a593Smuzhiyun 	/* Forcing Full or Half Duplex? */
1352*4882a593Smuzhiyun 	if (mac->forced_speed_duplex & E1000_ALL_HALF_DUPLEX) {
1353*4882a593Smuzhiyun 		ctrl &= ~E1000_CTRL_FD;
1354*4882a593Smuzhiyun 		*phy_ctrl &= ~MII_CR_FULL_DUPLEX;
1355*4882a593Smuzhiyun 		hw_dbg("Half Duplex\n");
1356*4882a593Smuzhiyun 	} else {
1357*4882a593Smuzhiyun 		ctrl |= E1000_CTRL_FD;
1358*4882a593Smuzhiyun 		*phy_ctrl |= MII_CR_FULL_DUPLEX;
1359*4882a593Smuzhiyun 		hw_dbg("Full Duplex\n");
1360*4882a593Smuzhiyun 	}
1361*4882a593Smuzhiyun 
1362*4882a593Smuzhiyun 	/* Forcing 10mb or 100mb? */
1363*4882a593Smuzhiyun 	if (mac->forced_speed_duplex & E1000_ALL_100_SPEED) {
1364*4882a593Smuzhiyun 		ctrl |= E1000_CTRL_SPD_100;
1365*4882a593Smuzhiyun 		*phy_ctrl |= MII_CR_SPEED_100;
1366*4882a593Smuzhiyun 		*phy_ctrl &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_10);
1367*4882a593Smuzhiyun 		hw_dbg("Forcing 100mb\n");
1368*4882a593Smuzhiyun 	} else {
1369*4882a593Smuzhiyun 		ctrl &= ~(E1000_CTRL_SPD_1000 | E1000_CTRL_SPD_100);
1370*4882a593Smuzhiyun 		*phy_ctrl |= MII_CR_SPEED_10;
1371*4882a593Smuzhiyun 		*phy_ctrl &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_100);
1372*4882a593Smuzhiyun 		hw_dbg("Forcing 10mb\n");
1373*4882a593Smuzhiyun 	}
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun 	igb_config_collision_dist(hw);
1376*4882a593Smuzhiyun 
1377*4882a593Smuzhiyun 	wr32(E1000_CTRL, ctrl);
1378*4882a593Smuzhiyun }
1379*4882a593Smuzhiyun 
1380*4882a593Smuzhiyun /**
1381*4882a593Smuzhiyun  *  igb_set_d3_lplu_state - Sets low power link up state for D3
1382*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1383*4882a593Smuzhiyun  *  @active: boolean used to enable/disable lplu
1384*4882a593Smuzhiyun  *
1385*4882a593Smuzhiyun  *  Success returns 0, Failure returns 1
1386*4882a593Smuzhiyun  *
1387*4882a593Smuzhiyun  *  The low power link up (lplu) state is set to the power management level D3
1388*4882a593Smuzhiyun  *  and SmartSpeed is disabled when active is true, else clear lplu for D3
1389*4882a593Smuzhiyun  *  and enable Smartspeed.  LPLU and Smartspeed are mutually exclusive.  LPLU
1390*4882a593Smuzhiyun  *  is used during Dx states where the power conservation is most important.
1391*4882a593Smuzhiyun  *  During driver activity, SmartSpeed should be enabled so performance is
1392*4882a593Smuzhiyun  *  maintained.
1393*4882a593Smuzhiyun  **/
igb_set_d3_lplu_state(struct e1000_hw * hw,bool active)1394*4882a593Smuzhiyun s32 igb_set_d3_lplu_state(struct e1000_hw *hw, bool active)
1395*4882a593Smuzhiyun {
1396*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1397*4882a593Smuzhiyun 	s32 ret_val = 0;
1398*4882a593Smuzhiyun 	u16 data;
1399*4882a593Smuzhiyun 
1400*4882a593Smuzhiyun 	if (!(hw->phy.ops.read_reg))
1401*4882a593Smuzhiyun 		goto out;
1402*4882a593Smuzhiyun 
1403*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, IGP02E1000_PHY_POWER_MGMT, &data);
1404*4882a593Smuzhiyun 	if (ret_val)
1405*4882a593Smuzhiyun 		goto out;
1406*4882a593Smuzhiyun 
1407*4882a593Smuzhiyun 	if (!active) {
1408*4882a593Smuzhiyun 		data &= ~IGP02E1000_PM_D3_LPLU;
1409*4882a593Smuzhiyun 		ret_val = phy->ops.write_reg(hw, IGP02E1000_PHY_POWER_MGMT,
1410*4882a593Smuzhiyun 					     data);
1411*4882a593Smuzhiyun 		if (ret_val)
1412*4882a593Smuzhiyun 			goto out;
1413*4882a593Smuzhiyun 		/* LPLU and SmartSpeed are mutually exclusive.  LPLU is used
1414*4882a593Smuzhiyun 		 * during Dx states where the power conservation is most
1415*4882a593Smuzhiyun 		 * important.  During driver activity we should enable
1416*4882a593Smuzhiyun 		 * SmartSpeed, so performance is maintained.
1417*4882a593Smuzhiyun 		 */
1418*4882a593Smuzhiyun 		if (phy->smart_speed == e1000_smart_speed_on) {
1419*4882a593Smuzhiyun 			ret_val = phy->ops.read_reg(hw,
1420*4882a593Smuzhiyun 						    IGP01E1000_PHY_PORT_CONFIG,
1421*4882a593Smuzhiyun 						    &data);
1422*4882a593Smuzhiyun 			if (ret_val)
1423*4882a593Smuzhiyun 				goto out;
1424*4882a593Smuzhiyun 
1425*4882a593Smuzhiyun 			data |= IGP01E1000_PSCFR_SMART_SPEED;
1426*4882a593Smuzhiyun 			ret_val = phy->ops.write_reg(hw,
1427*4882a593Smuzhiyun 						     IGP01E1000_PHY_PORT_CONFIG,
1428*4882a593Smuzhiyun 						     data);
1429*4882a593Smuzhiyun 			if (ret_val)
1430*4882a593Smuzhiyun 				goto out;
1431*4882a593Smuzhiyun 		} else if (phy->smart_speed == e1000_smart_speed_off) {
1432*4882a593Smuzhiyun 			ret_val = phy->ops.read_reg(hw,
1433*4882a593Smuzhiyun 						     IGP01E1000_PHY_PORT_CONFIG,
1434*4882a593Smuzhiyun 						     &data);
1435*4882a593Smuzhiyun 			if (ret_val)
1436*4882a593Smuzhiyun 				goto out;
1437*4882a593Smuzhiyun 
1438*4882a593Smuzhiyun 			data &= ~IGP01E1000_PSCFR_SMART_SPEED;
1439*4882a593Smuzhiyun 			ret_val = phy->ops.write_reg(hw,
1440*4882a593Smuzhiyun 						     IGP01E1000_PHY_PORT_CONFIG,
1441*4882a593Smuzhiyun 						     data);
1442*4882a593Smuzhiyun 			if (ret_val)
1443*4882a593Smuzhiyun 				goto out;
1444*4882a593Smuzhiyun 		}
1445*4882a593Smuzhiyun 	} else if ((phy->autoneg_advertised == E1000_ALL_SPEED_DUPLEX) ||
1446*4882a593Smuzhiyun 		   (phy->autoneg_advertised == E1000_ALL_NOT_GIG) ||
1447*4882a593Smuzhiyun 		   (phy->autoneg_advertised == E1000_ALL_10_SPEED)) {
1448*4882a593Smuzhiyun 		data |= IGP02E1000_PM_D3_LPLU;
1449*4882a593Smuzhiyun 		ret_val = phy->ops.write_reg(hw, IGP02E1000_PHY_POWER_MGMT,
1450*4882a593Smuzhiyun 					      data);
1451*4882a593Smuzhiyun 		if (ret_val)
1452*4882a593Smuzhiyun 			goto out;
1453*4882a593Smuzhiyun 
1454*4882a593Smuzhiyun 		/* When LPLU is enabled, we should disable SmartSpeed */
1455*4882a593Smuzhiyun 		ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
1456*4882a593Smuzhiyun 					    &data);
1457*4882a593Smuzhiyun 		if (ret_val)
1458*4882a593Smuzhiyun 			goto out;
1459*4882a593Smuzhiyun 
1460*4882a593Smuzhiyun 		data &= ~IGP01E1000_PSCFR_SMART_SPEED;
1461*4882a593Smuzhiyun 		ret_val = phy->ops.write_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
1462*4882a593Smuzhiyun 					     data);
1463*4882a593Smuzhiyun 	}
1464*4882a593Smuzhiyun 
1465*4882a593Smuzhiyun out:
1466*4882a593Smuzhiyun 	return ret_val;
1467*4882a593Smuzhiyun }
1468*4882a593Smuzhiyun 
1469*4882a593Smuzhiyun /**
1470*4882a593Smuzhiyun  *  igb_check_downshift - Checks whether a downshift in speed occurred
1471*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1472*4882a593Smuzhiyun  *
1473*4882a593Smuzhiyun  *  Success returns 0, Failure returns 1
1474*4882a593Smuzhiyun  *
1475*4882a593Smuzhiyun  *  A downshift is detected by querying the PHY link health.
1476*4882a593Smuzhiyun  **/
igb_check_downshift(struct e1000_hw * hw)1477*4882a593Smuzhiyun s32 igb_check_downshift(struct e1000_hw *hw)
1478*4882a593Smuzhiyun {
1479*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1480*4882a593Smuzhiyun 	s32 ret_val;
1481*4882a593Smuzhiyun 	u16 phy_data, offset, mask;
1482*4882a593Smuzhiyun 
1483*4882a593Smuzhiyun 	switch (phy->type) {
1484*4882a593Smuzhiyun 	case e1000_phy_i210:
1485*4882a593Smuzhiyun 	case e1000_phy_m88:
1486*4882a593Smuzhiyun 	case e1000_phy_gg82563:
1487*4882a593Smuzhiyun 		offset	= M88E1000_PHY_SPEC_STATUS;
1488*4882a593Smuzhiyun 		mask	= M88E1000_PSSR_DOWNSHIFT;
1489*4882a593Smuzhiyun 		break;
1490*4882a593Smuzhiyun 	case e1000_phy_igp_2:
1491*4882a593Smuzhiyun 	case e1000_phy_igp:
1492*4882a593Smuzhiyun 	case e1000_phy_igp_3:
1493*4882a593Smuzhiyun 		offset	= IGP01E1000_PHY_LINK_HEALTH;
1494*4882a593Smuzhiyun 		mask	= IGP01E1000_PLHR_SS_DOWNGRADE;
1495*4882a593Smuzhiyun 		break;
1496*4882a593Smuzhiyun 	default:
1497*4882a593Smuzhiyun 		/* speed downshift not supported */
1498*4882a593Smuzhiyun 		phy->speed_downgraded = false;
1499*4882a593Smuzhiyun 		ret_val = 0;
1500*4882a593Smuzhiyun 		goto out;
1501*4882a593Smuzhiyun 	}
1502*4882a593Smuzhiyun 
1503*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, offset, &phy_data);
1504*4882a593Smuzhiyun 
1505*4882a593Smuzhiyun 	if (!ret_val)
1506*4882a593Smuzhiyun 		phy->speed_downgraded = (phy_data & mask) ? true : false;
1507*4882a593Smuzhiyun 
1508*4882a593Smuzhiyun out:
1509*4882a593Smuzhiyun 	return ret_val;
1510*4882a593Smuzhiyun }
1511*4882a593Smuzhiyun 
1512*4882a593Smuzhiyun /**
1513*4882a593Smuzhiyun  *  igb_check_polarity_m88 - Checks the polarity.
1514*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1515*4882a593Smuzhiyun  *
1516*4882a593Smuzhiyun  *  Success returns 0, Failure returns -E1000_ERR_PHY (-2)
1517*4882a593Smuzhiyun  *
1518*4882a593Smuzhiyun  *  Polarity is determined based on the PHY specific status register.
1519*4882a593Smuzhiyun  **/
igb_check_polarity_m88(struct e1000_hw * hw)1520*4882a593Smuzhiyun s32 igb_check_polarity_m88(struct e1000_hw *hw)
1521*4882a593Smuzhiyun {
1522*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1523*4882a593Smuzhiyun 	s32 ret_val;
1524*4882a593Smuzhiyun 	u16 data;
1525*4882a593Smuzhiyun 
1526*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_STATUS, &data);
1527*4882a593Smuzhiyun 
1528*4882a593Smuzhiyun 	if (!ret_val)
1529*4882a593Smuzhiyun 		phy->cable_polarity = (data & M88E1000_PSSR_REV_POLARITY)
1530*4882a593Smuzhiyun 				      ? e1000_rev_polarity_reversed
1531*4882a593Smuzhiyun 				      : e1000_rev_polarity_normal;
1532*4882a593Smuzhiyun 
1533*4882a593Smuzhiyun 	return ret_val;
1534*4882a593Smuzhiyun }
1535*4882a593Smuzhiyun 
1536*4882a593Smuzhiyun /**
1537*4882a593Smuzhiyun  *  igb_check_polarity_igp - Checks the polarity.
1538*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1539*4882a593Smuzhiyun  *
1540*4882a593Smuzhiyun  *  Success returns 0, Failure returns -E1000_ERR_PHY (-2)
1541*4882a593Smuzhiyun  *
1542*4882a593Smuzhiyun  *  Polarity is determined based on the PHY port status register, and the
1543*4882a593Smuzhiyun  *  current speed (since there is no polarity at 100Mbps).
1544*4882a593Smuzhiyun  **/
igb_check_polarity_igp(struct e1000_hw * hw)1545*4882a593Smuzhiyun static s32 igb_check_polarity_igp(struct e1000_hw *hw)
1546*4882a593Smuzhiyun {
1547*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1548*4882a593Smuzhiyun 	s32 ret_val;
1549*4882a593Smuzhiyun 	u16 data, offset, mask;
1550*4882a593Smuzhiyun 
1551*4882a593Smuzhiyun 	/* Polarity is determined based on the speed of
1552*4882a593Smuzhiyun 	 * our connection.
1553*4882a593Smuzhiyun 	 */
1554*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_STATUS, &data);
1555*4882a593Smuzhiyun 	if (ret_val)
1556*4882a593Smuzhiyun 		goto out;
1557*4882a593Smuzhiyun 
1558*4882a593Smuzhiyun 	if ((data & IGP01E1000_PSSR_SPEED_MASK) ==
1559*4882a593Smuzhiyun 	    IGP01E1000_PSSR_SPEED_1000MBPS) {
1560*4882a593Smuzhiyun 		offset	= IGP01E1000_PHY_PCS_INIT_REG;
1561*4882a593Smuzhiyun 		mask	= IGP01E1000_PHY_POLARITY_MASK;
1562*4882a593Smuzhiyun 	} else {
1563*4882a593Smuzhiyun 		/* This really only applies to 10Mbps since
1564*4882a593Smuzhiyun 		 * there is no polarity for 100Mbps (always 0).
1565*4882a593Smuzhiyun 		 */
1566*4882a593Smuzhiyun 		offset	= IGP01E1000_PHY_PORT_STATUS;
1567*4882a593Smuzhiyun 		mask	= IGP01E1000_PSSR_POLARITY_REVERSED;
1568*4882a593Smuzhiyun 	}
1569*4882a593Smuzhiyun 
1570*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, offset, &data);
1571*4882a593Smuzhiyun 
1572*4882a593Smuzhiyun 	if (!ret_val)
1573*4882a593Smuzhiyun 		phy->cable_polarity = (data & mask)
1574*4882a593Smuzhiyun 				      ? e1000_rev_polarity_reversed
1575*4882a593Smuzhiyun 				      : e1000_rev_polarity_normal;
1576*4882a593Smuzhiyun 
1577*4882a593Smuzhiyun out:
1578*4882a593Smuzhiyun 	return ret_val;
1579*4882a593Smuzhiyun }
1580*4882a593Smuzhiyun 
1581*4882a593Smuzhiyun /**
1582*4882a593Smuzhiyun  *  igb_wait_autoneg - Wait for auto-neg completion
1583*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1584*4882a593Smuzhiyun  *
1585*4882a593Smuzhiyun  *  Waits for auto-negotiation to complete or for the auto-negotiation time
1586*4882a593Smuzhiyun  *  limit to expire, which ever happens first.
1587*4882a593Smuzhiyun  **/
igb_wait_autoneg(struct e1000_hw * hw)1588*4882a593Smuzhiyun static s32 igb_wait_autoneg(struct e1000_hw *hw)
1589*4882a593Smuzhiyun {
1590*4882a593Smuzhiyun 	s32 ret_val = 0;
1591*4882a593Smuzhiyun 	u16 i, phy_status;
1592*4882a593Smuzhiyun 
1593*4882a593Smuzhiyun 	/* Break after autoneg completes or PHY_AUTO_NEG_LIMIT expires. */
1594*4882a593Smuzhiyun 	for (i = PHY_AUTO_NEG_LIMIT; i > 0; i--) {
1595*4882a593Smuzhiyun 		ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
1596*4882a593Smuzhiyun 		if (ret_val)
1597*4882a593Smuzhiyun 			break;
1598*4882a593Smuzhiyun 		ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
1599*4882a593Smuzhiyun 		if (ret_val)
1600*4882a593Smuzhiyun 			break;
1601*4882a593Smuzhiyun 		if (phy_status & MII_SR_AUTONEG_COMPLETE)
1602*4882a593Smuzhiyun 			break;
1603*4882a593Smuzhiyun 		msleep(100);
1604*4882a593Smuzhiyun 	}
1605*4882a593Smuzhiyun 
1606*4882a593Smuzhiyun 	/* PHY_AUTO_NEG_TIME expiration doesn't guarantee auto-negotiation
1607*4882a593Smuzhiyun 	 * has completed.
1608*4882a593Smuzhiyun 	 */
1609*4882a593Smuzhiyun 	return ret_val;
1610*4882a593Smuzhiyun }
1611*4882a593Smuzhiyun 
1612*4882a593Smuzhiyun /**
1613*4882a593Smuzhiyun  *  igb_phy_has_link - Polls PHY for link
1614*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1615*4882a593Smuzhiyun  *  @iterations: number of times to poll for link
1616*4882a593Smuzhiyun  *  @usec_interval: delay between polling attempts
1617*4882a593Smuzhiyun  *  @success: pointer to whether polling was successful or not
1618*4882a593Smuzhiyun  *
1619*4882a593Smuzhiyun  *  Polls the PHY status register for link, 'iterations' number of times.
1620*4882a593Smuzhiyun  **/
igb_phy_has_link(struct e1000_hw * hw,u32 iterations,u32 usec_interval,bool * success)1621*4882a593Smuzhiyun s32 igb_phy_has_link(struct e1000_hw *hw, u32 iterations,
1622*4882a593Smuzhiyun 		     u32 usec_interval, bool *success)
1623*4882a593Smuzhiyun {
1624*4882a593Smuzhiyun 	s32 ret_val = 0;
1625*4882a593Smuzhiyun 	u16 i, phy_status;
1626*4882a593Smuzhiyun 
1627*4882a593Smuzhiyun 	for (i = 0; i < iterations; i++) {
1628*4882a593Smuzhiyun 		/* Some PHYs require the PHY_STATUS register to be read
1629*4882a593Smuzhiyun 		 * twice due to the link bit being sticky.  No harm doing
1630*4882a593Smuzhiyun 		 * it across the board.
1631*4882a593Smuzhiyun 		 */
1632*4882a593Smuzhiyun 		ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
1633*4882a593Smuzhiyun 		if (ret_val && usec_interval > 0) {
1634*4882a593Smuzhiyun 			/* If the first read fails, another entity may have
1635*4882a593Smuzhiyun 			 * ownership of the resources, wait and try again to
1636*4882a593Smuzhiyun 			 * see if they have relinquished the resources yet.
1637*4882a593Smuzhiyun 			 */
1638*4882a593Smuzhiyun 			if (usec_interval >= 1000)
1639*4882a593Smuzhiyun 				mdelay(usec_interval/1000);
1640*4882a593Smuzhiyun 			else
1641*4882a593Smuzhiyun 				udelay(usec_interval);
1642*4882a593Smuzhiyun 		}
1643*4882a593Smuzhiyun 		ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
1644*4882a593Smuzhiyun 		if (ret_val)
1645*4882a593Smuzhiyun 			break;
1646*4882a593Smuzhiyun 		if (phy_status & MII_SR_LINK_STATUS)
1647*4882a593Smuzhiyun 			break;
1648*4882a593Smuzhiyun 		if (usec_interval >= 1000)
1649*4882a593Smuzhiyun 			mdelay(usec_interval/1000);
1650*4882a593Smuzhiyun 		else
1651*4882a593Smuzhiyun 			udelay(usec_interval);
1652*4882a593Smuzhiyun 	}
1653*4882a593Smuzhiyun 
1654*4882a593Smuzhiyun 	*success = (i < iterations) ? true : false;
1655*4882a593Smuzhiyun 
1656*4882a593Smuzhiyun 	return ret_val;
1657*4882a593Smuzhiyun }
1658*4882a593Smuzhiyun 
1659*4882a593Smuzhiyun /**
1660*4882a593Smuzhiyun  *  igb_get_cable_length_m88 - Determine cable length for m88 PHY
1661*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1662*4882a593Smuzhiyun  *
1663*4882a593Smuzhiyun  *  Reads the PHY specific status register to retrieve the cable length
1664*4882a593Smuzhiyun  *  information.  The cable length is determined by averaging the minimum and
1665*4882a593Smuzhiyun  *  maximum values to get the "average" cable length.  The m88 PHY has four
1666*4882a593Smuzhiyun  *  possible cable length values, which are:
1667*4882a593Smuzhiyun  *	Register Value		Cable Length
1668*4882a593Smuzhiyun  *	0			< 50 meters
1669*4882a593Smuzhiyun  *	1			50 - 80 meters
1670*4882a593Smuzhiyun  *	2			80 - 110 meters
1671*4882a593Smuzhiyun  *	3			110 - 140 meters
1672*4882a593Smuzhiyun  *	4			> 140 meters
1673*4882a593Smuzhiyun  **/
igb_get_cable_length_m88(struct e1000_hw * hw)1674*4882a593Smuzhiyun s32 igb_get_cable_length_m88(struct e1000_hw *hw)
1675*4882a593Smuzhiyun {
1676*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1677*4882a593Smuzhiyun 	s32 ret_val;
1678*4882a593Smuzhiyun 	u16 phy_data, index;
1679*4882a593Smuzhiyun 
1680*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
1681*4882a593Smuzhiyun 	if (ret_val)
1682*4882a593Smuzhiyun 		goto out;
1683*4882a593Smuzhiyun 
1684*4882a593Smuzhiyun 	index = (phy_data & M88E1000_PSSR_CABLE_LENGTH) >>
1685*4882a593Smuzhiyun 		M88E1000_PSSR_CABLE_LENGTH_SHIFT;
1686*4882a593Smuzhiyun 	if (index >= ARRAY_SIZE(e1000_m88_cable_length_table) - 1) {
1687*4882a593Smuzhiyun 		ret_val = -E1000_ERR_PHY;
1688*4882a593Smuzhiyun 		goto out;
1689*4882a593Smuzhiyun 	}
1690*4882a593Smuzhiyun 
1691*4882a593Smuzhiyun 	phy->min_cable_length = e1000_m88_cable_length_table[index];
1692*4882a593Smuzhiyun 	phy->max_cable_length = e1000_m88_cable_length_table[index + 1];
1693*4882a593Smuzhiyun 
1694*4882a593Smuzhiyun 	phy->cable_length = (phy->min_cable_length + phy->max_cable_length) / 2;
1695*4882a593Smuzhiyun 
1696*4882a593Smuzhiyun out:
1697*4882a593Smuzhiyun 	return ret_val;
1698*4882a593Smuzhiyun }
1699*4882a593Smuzhiyun 
igb_get_cable_length_m88_gen2(struct e1000_hw * hw)1700*4882a593Smuzhiyun s32 igb_get_cable_length_m88_gen2(struct e1000_hw *hw)
1701*4882a593Smuzhiyun {
1702*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1703*4882a593Smuzhiyun 	s32 ret_val;
1704*4882a593Smuzhiyun 	u16 phy_data, phy_data2, index, default_page, is_cm;
1705*4882a593Smuzhiyun 	int len_tot = 0;
1706*4882a593Smuzhiyun 	u16 len_min;
1707*4882a593Smuzhiyun 	u16 len_max;
1708*4882a593Smuzhiyun 
1709*4882a593Smuzhiyun 	switch (hw->phy.id) {
1710*4882a593Smuzhiyun 	case M88E1543_E_PHY_ID:
1711*4882a593Smuzhiyun 	case M88E1512_E_PHY_ID:
1712*4882a593Smuzhiyun 	case I347AT4_E_PHY_ID:
1713*4882a593Smuzhiyun 	case I210_I_PHY_ID:
1714*4882a593Smuzhiyun 		/* Remember the original page select and set it to 7 */
1715*4882a593Smuzhiyun 		ret_val = phy->ops.read_reg(hw, I347AT4_PAGE_SELECT,
1716*4882a593Smuzhiyun 					    &default_page);
1717*4882a593Smuzhiyun 		if (ret_val)
1718*4882a593Smuzhiyun 			goto out;
1719*4882a593Smuzhiyun 
1720*4882a593Smuzhiyun 		ret_val = phy->ops.write_reg(hw, I347AT4_PAGE_SELECT, 0x07);
1721*4882a593Smuzhiyun 		if (ret_val)
1722*4882a593Smuzhiyun 			goto out;
1723*4882a593Smuzhiyun 
1724*4882a593Smuzhiyun 		/* Check if the unit of cable length is meters or cm */
1725*4882a593Smuzhiyun 		ret_val = phy->ops.read_reg(hw, I347AT4_PCDC, &phy_data2);
1726*4882a593Smuzhiyun 		if (ret_val)
1727*4882a593Smuzhiyun 			goto out;
1728*4882a593Smuzhiyun 
1729*4882a593Smuzhiyun 		is_cm = !(phy_data2 & I347AT4_PCDC_CABLE_LENGTH_UNIT);
1730*4882a593Smuzhiyun 
1731*4882a593Smuzhiyun 		/* Get cable length from Pair 0 length Regs */
1732*4882a593Smuzhiyun 		ret_val = phy->ops.read_reg(hw, I347AT4_PCDL0, &phy_data);
1733*4882a593Smuzhiyun 		if (ret_val)
1734*4882a593Smuzhiyun 			goto out;
1735*4882a593Smuzhiyun 
1736*4882a593Smuzhiyun 		phy->pair_length[0] = phy_data / (is_cm ? 100 : 1);
1737*4882a593Smuzhiyun 		len_tot = phy->pair_length[0];
1738*4882a593Smuzhiyun 		len_min = phy->pair_length[0];
1739*4882a593Smuzhiyun 		len_max = phy->pair_length[0];
1740*4882a593Smuzhiyun 
1741*4882a593Smuzhiyun 		/* Get cable length from Pair 1 length Regs */
1742*4882a593Smuzhiyun 		ret_val = phy->ops.read_reg(hw, I347AT4_PCDL1, &phy_data);
1743*4882a593Smuzhiyun 		if (ret_val)
1744*4882a593Smuzhiyun 			goto out;
1745*4882a593Smuzhiyun 
1746*4882a593Smuzhiyun 		phy->pair_length[1] = phy_data / (is_cm ? 100 : 1);
1747*4882a593Smuzhiyun 		len_tot += phy->pair_length[1];
1748*4882a593Smuzhiyun 		len_min = min(len_min, phy->pair_length[1]);
1749*4882a593Smuzhiyun 		len_max = max(len_max, phy->pair_length[1]);
1750*4882a593Smuzhiyun 
1751*4882a593Smuzhiyun 		/* Get cable length from Pair 2 length Regs */
1752*4882a593Smuzhiyun 		ret_val = phy->ops.read_reg(hw, I347AT4_PCDL2, &phy_data);
1753*4882a593Smuzhiyun 		if (ret_val)
1754*4882a593Smuzhiyun 			goto out;
1755*4882a593Smuzhiyun 
1756*4882a593Smuzhiyun 		phy->pair_length[2] = phy_data / (is_cm ? 100 : 1);
1757*4882a593Smuzhiyun 		len_tot += phy->pair_length[2];
1758*4882a593Smuzhiyun 		len_min = min(len_min, phy->pair_length[2]);
1759*4882a593Smuzhiyun 		len_max = max(len_max, phy->pair_length[2]);
1760*4882a593Smuzhiyun 
1761*4882a593Smuzhiyun 		/* Get cable length from Pair 3 length Regs */
1762*4882a593Smuzhiyun 		ret_val = phy->ops.read_reg(hw, I347AT4_PCDL3, &phy_data);
1763*4882a593Smuzhiyun 		if (ret_val)
1764*4882a593Smuzhiyun 			goto out;
1765*4882a593Smuzhiyun 
1766*4882a593Smuzhiyun 		phy->pair_length[3] = phy_data / (is_cm ? 100 : 1);
1767*4882a593Smuzhiyun 		len_tot += phy->pair_length[3];
1768*4882a593Smuzhiyun 		len_min = min(len_min, phy->pair_length[3]);
1769*4882a593Smuzhiyun 		len_max = max(len_max, phy->pair_length[3]);
1770*4882a593Smuzhiyun 
1771*4882a593Smuzhiyun 		/* Populate the phy structure with cable length in meters */
1772*4882a593Smuzhiyun 		phy->min_cable_length = len_min;
1773*4882a593Smuzhiyun 		phy->max_cable_length = len_max;
1774*4882a593Smuzhiyun 		phy->cable_length = len_tot / 4;
1775*4882a593Smuzhiyun 
1776*4882a593Smuzhiyun 		/* Reset the page selec to its original value */
1777*4882a593Smuzhiyun 		ret_val = phy->ops.write_reg(hw, I347AT4_PAGE_SELECT,
1778*4882a593Smuzhiyun 					     default_page);
1779*4882a593Smuzhiyun 		if (ret_val)
1780*4882a593Smuzhiyun 			goto out;
1781*4882a593Smuzhiyun 		break;
1782*4882a593Smuzhiyun 	case M88E1112_E_PHY_ID:
1783*4882a593Smuzhiyun 		/* Remember the original page select and set it to 5 */
1784*4882a593Smuzhiyun 		ret_val = phy->ops.read_reg(hw, I347AT4_PAGE_SELECT,
1785*4882a593Smuzhiyun 					    &default_page);
1786*4882a593Smuzhiyun 		if (ret_val)
1787*4882a593Smuzhiyun 			goto out;
1788*4882a593Smuzhiyun 
1789*4882a593Smuzhiyun 		ret_val = phy->ops.write_reg(hw, I347AT4_PAGE_SELECT, 0x05);
1790*4882a593Smuzhiyun 		if (ret_val)
1791*4882a593Smuzhiyun 			goto out;
1792*4882a593Smuzhiyun 
1793*4882a593Smuzhiyun 		ret_val = phy->ops.read_reg(hw, M88E1112_VCT_DSP_DISTANCE,
1794*4882a593Smuzhiyun 					    &phy_data);
1795*4882a593Smuzhiyun 		if (ret_val)
1796*4882a593Smuzhiyun 			goto out;
1797*4882a593Smuzhiyun 
1798*4882a593Smuzhiyun 		index = (phy_data & M88E1000_PSSR_CABLE_LENGTH) >>
1799*4882a593Smuzhiyun 			M88E1000_PSSR_CABLE_LENGTH_SHIFT;
1800*4882a593Smuzhiyun 		if (index >= ARRAY_SIZE(e1000_m88_cable_length_table) - 1) {
1801*4882a593Smuzhiyun 			ret_val = -E1000_ERR_PHY;
1802*4882a593Smuzhiyun 			goto out;
1803*4882a593Smuzhiyun 		}
1804*4882a593Smuzhiyun 
1805*4882a593Smuzhiyun 		phy->min_cable_length = e1000_m88_cable_length_table[index];
1806*4882a593Smuzhiyun 		phy->max_cable_length = e1000_m88_cable_length_table[index + 1];
1807*4882a593Smuzhiyun 
1808*4882a593Smuzhiyun 		phy->cable_length = (phy->min_cable_length +
1809*4882a593Smuzhiyun 				     phy->max_cable_length) / 2;
1810*4882a593Smuzhiyun 
1811*4882a593Smuzhiyun 		/* Reset the page select to its original value */
1812*4882a593Smuzhiyun 		ret_val = phy->ops.write_reg(hw, I347AT4_PAGE_SELECT,
1813*4882a593Smuzhiyun 					     default_page);
1814*4882a593Smuzhiyun 		if (ret_val)
1815*4882a593Smuzhiyun 			goto out;
1816*4882a593Smuzhiyun 
1817*4882a593Smuzhiyun 		break;
1818*4882a593Smuzhiyun 	default:
1819*4882a593Smuzhiyun 		ret_val = -E1000_ERR_PHY;
1820*4882a593Smuzhiyun 		goto out;
1821*4882a593Smuzhiyun 	}
1822*4882a593Smuzhiyun 
1823*4882a593Smuzhiyun out:
1824*4882a593Smuzhiyun 	return ret_val;
1825*4882a593Smuzhiyun }
1826*4882a593Smuzhiyun 
1827*4882a593Smuzhiyun /**
1828*4882a593Smuzhiyun  *  igb_get_cable_length_igp_2 - Determine cable length for igp2 PHY
1829*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1830*4882a593Smuzhiyun  *
1831*4882a593Smuzhiyun  *  The automatic gain control (agc) normalizes the amplitude of the
1832*4882a593Smuzhiyun  *  received signal, adjusting for the attenuation produced by the
1833*4882a593Smuzhiyun  *  cable.  By reading the AGC registers, which represent the
1834*4882a593Smuzhiyun  *  combination of coarse and fine gain value, the value can be put
1835*4882a593Smuzhiyun  *  into a lookup table to obtain the approximate cable length
1836*4882a593Smuzhiyun  *  for each channel.
1837*4882a593Smuzhiyun  **/
igb_get_cable_length_igp_2(struct e1000_hw * hw)1838*4882a593Smuzhiyun s32 igb_get_cable_length_igp_2(struct e1000_hw *hw)
1839*4882a593Smuzhiyun {
1840*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1841*4882a593Smuzhiyun 	s32 ret_val = 0;
1842*4882a593Smuzhiyun 	u16 phy_data, i, agc_value = 0;
1843*4882a593Smuzhiyun 	u16 cur_agc_index, max_agc_index = 0;
1844*4882a593Smuzhiyun 	u16 min_agc_index = ARRAY_SIZE(e1000_igp_2_cable_length_table) - 1;
1845*4882a593Smuzhiyun 	static const u16 agc_reg_array[IGP02E1000_PHY_CHANNEL_NUM] = {
1846*4882a593Smuzhiyun 		IGP02E1000_PHY_AGC_A,
1847*4882a593Smuzhiyun 		IGP02E1000_PHY_AGC_B,
1848*4882a593Smuzhiyun 		IGP02E1000_PHY_AGC_C,
1849*4882a593Smuzhiyun 		IGP02E1000_PHY_AGC_D
1850*4882a593Smuzhiyun 	};
1851*4882a593Smuzhiyun 
1852*4882a593Smuzhiyun 	/* Read the AGC registers for all channels */
1853*4882a593Smuzhiyun 	for (i = 0; i < IGP02E1000_PHY_CHANNEL_NUM; i++) {
1854*4882a593Smuzhiyun 		ret_val = phy->ops.read_reg(hw, agc_reg_array[i], &phy_data);
1855*4882a593Smuzhiyun 		if (ret_val)
1856*4882a593Smuzhiyun 			goto out;
1857*4882a593Smuzhiyun 
1858*4882a593Smuzhiyun 		/* Getting bits 15:9, which represent the combination of
1859*4882a593Smuzhiyun 		 * coarse and fine gain values.  The result is a number
1860*4882a593Smuzhiyun 		 * that can be put into the lookup table to obtain the
1861*4882a593Smuzhiyun 		 * approximate cable length.
1862*4882a593Smuzhiyun 		 */
1863*4882a593Smuzhiyun 		cur_agc_index = (phy_data >> IGP02E1000_AGC_LENGTH_SHIFT) &
1864*4882a593Smuzhiyun 				IGP02E1000_AGC_LENGTH_MASK;
1865*4882a593Smuzhiyun 
1866*4882a593Smuzhiyun 		/* Array index bound check. */
1867*4882a593Smuzhiyun 		if ((cur_agc_index >= ARRAY_SIZE(e1000_igp_2_cable_length_table)) ||
1868*4882a593Smuzhiyun 		    (cur_agc_index == 0)) {
1869*4882a593Smuzhiyun 			ret_val = -E1000_ERR_PHY;
1870*4882a593Smuzhiyun 			goto out;
1871*4882a593Smuzhiyun 		}
1872*4882a593Smuzhiyun 
1873*4882a593Smuzhiyun 		/* Remove min & max AGC values from calculation. */
1874*4882a593Smuzhiyun 		if (e1000_igp_2_cable_length_table[min_agc_index] >
1875*4882a593Smuzhiyun 		    e1000_igp_2_cable_length_table[cur_agc_index])
1876*4882a593Smuzhiyun 			min_agc_index = cur_agc_index;
1877*4882a593Smuzhiyun 		if (e1000_igp_2_cable_length_table[max_agc_index] <
1878*4882a593Smuzhiyun 		    e1000_igp_2_cable_length_table[cur_agc_index])
1879*4882a593Smuzhiyun 			max_agc_index = cur_agc_index;
1880*4882a593Smuzhiyun 
1881*4882a593Smuzhiyun 		agc_value += e1000_igp_2_cable_length_table[cur_agc_index];
1882*4882a593Smuzhiyun 	}
1883*4882a593Smuzhiyun 
1884*4882a593Smuzhiyun 	agc_value -= (e1000_igp_2_cable_length_table[min_agc_index] +
1885*4882a593Smuzhiyun 		      e1000_igp_2_cable_length_table[max_agc_index]);
1886*4882a593Smuzhiyun 	agc_value /= (IGP02E1000_PHY_CHANNEL_NUM - 2);
1887*4882a593Smuzhiyun 
1888*4882a593Smuzhiyun 	/* Calculate cable length with the error range of +/- 10 meters. */
1889*4882a593Smuzhiyun 	phy->min_cable_length = ((agc_value - IGP02E1000_AGC_RANGE) > 0) ?
1890*4882a593Smuzhiyun 				 (agc_value - IGP02E1000_AGC_RANGE) : 0;
1891*4882a593Smuzhiyun 	phy->max_cable_length = agc_value + IGP02E1000_AGC_RANGE;
1892*4882a593Smuzhiyun 
1893*4882a593Smuzhiyun 	phy->cable_length = (phy->min_cable_length + phy->max_cable_length) / 2;
1894*4882a593Smuzhiyun 
1895*4882a593Smuzhiyun out:
1896*4882a593Smuzhiyun 	return ret_val;
1897*4882a593Smuzhiyun }
1898*4882a593Smuzhiyun 
1899*4882a593Smuzhiyun /**
1900*4882a593Smuzhiyun  *  igb_get_phy_info_m88 - Retrieve PHY information
1901*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1902*4882a593Smuzhiyun  *
1903*4882a593Smuzhiyun  *  Valid for only copper links.  Read the PHY status register (sticky read)
1904*4882a593Smuzhiyun  *  to verify that link is up.  Read the PHY special control register to
1905*4882a593Smuzhiyun  *  determine the polarity and 10base-T extended distance.  Read the PHY
1906*4882a593Smuzhiyun  *  special status register to determine MDI/MDIx and current speed.  If
1907*4882a593Smuzhiyun  *  speed is 1000, then determine cable length, local and remote receiver.
1908*4882a593Smuzhiyun  **/
igb_get_phy_info_m88(struct e1000_hw * hw)1909*4882a593Smuzhiyun s32 igb_get_phy_info_m88(struct e1000_hw *hw)
1910*4882a593Smuzhiyun {
1911*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1912*4882a593Smuzhiyun 	s32  ret_val;
1913*4882a593Smuzhiyun 	u16 phy_data;
1914*4882a593Smuzhiyun 	bool link;
1915*4882a593Smuzhiyun 
1916*4882a593Smuzhiyun 	if (phy->media_type != e1000_media_type_copper) {
1917*4882a593Smuzhiyun 		hw_dbg("Phy info is only valid for copper media\n");
1918*4882a593Smuzhiyun 		ret_val = -E1000_ERR_CONFIG;
1919*4882a593Smuzhiyun 		goto out;
1920*4882a593Smuzhiyun 	}
1921*4882a593Smuzhiyun 
1922*4882a593Smuzhiyun 	ret_val = igb_phy_has_link(hw, 1, 0, &link);
1923*4882a593Smuzhiyun 	if (ret_val)
1924*4882a593Smuzhiyun 		goto out;
1925*4882a593Smuzhiyun 
1926*4882a593Smuzhiyun 	if (!link) {
1927*4882a593Smuzhiyun 		hw_dbg("Phy info is only valid if link is up\n");
1928*4882a593Smuzhiyun 		ret_val = -E1000_ERR_CONFIG;
1929*4882a593Smuzhiyun 		goto out;
1930*4882a593Smuzhiyun 	}
1931*4882a593Smuzhiyun 
1932*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1933*4882a593Smuzhiyun 	if (ret_val)
1934*4882a593Smuzhiyun 		goto out;
1935*4882a593Smuzhiyun 
1936*4882a593Smuzhiyun 	phy->polarity_correction = (phy_data & M88E1000_PSCR_POLARITY_REVERSAL)
1937*4882a593Smuzhiyun 				   ? true : false;
1938*4882a593Smuzhiyun 
1939*4882a593Smuzhiyun 	ret_val = igb_check_polarity_m88(hw);
1940*4882a593Smuzhiyun 	if (ret_val)
1941*4882a593Smuzhiyun 		goto out;
1942*4882a593Smuzhiyun 
1943*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
1944*4882a593Smuzhiyun 	if (ret_val)
1945*4882a593Smuzhiyun 		goto out;
1946*4882a593Smuzhiyun 
1947*4882a593Smuzhiyun 	phy->is_mdix = (phy_data & M88E1000_PSSR_MDIX) ? true : false;
1948*4882a593Smuzhiyun 
1949*4882a593Smuzhiyun 	if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS) {
1950*4882a593Smuzhiyun 		ret_val = phy->ops.get_cable_length(hw);
1951*4882a593Smuzhiyun 		if (ret_val)
1952*4882a593Smuzhiyun 			goto out;
1953*4882a593Smuzhiyun 
1954*4882a593Smuzhiyun 		ret_val = phy->ops.read_reg(hw, PHY_1000T_STATUS, &phy_data);
1955*4882a593Smuzhiyun 		if (ret_val)
1956*4882a593Smuzhiyun 			goto out;
1957*4882a593Smuzhiyun 
1958*4882a593Smuzhiyun 		phy->local_rx = (phy_data & SR_1000T_LOCAL_RX_STATUS)
1959*4882a593Smuzhiyun 				? e1000_1000t_rx_status_ok
1960*4882a593Smuzhiyun 				: e1000_1000t_rx_status_not_ok;
1961*4882a593Smuzhiyun 
1962*4882a593Smuzhiyun 		phy->remote_rx = (phy_data & SR_1000T_REMOTE_RX_STATUS)
1963*4882a593Smuzhiyun 				 ? e1000_1000t_rx_status_ok
1964*4882a593Smuzhiyun 				 : e1000_1000t_rx_status_not_ok;
1965*4882a593Smuzhiyun 	} else {
1966*4882a593Smuzhiyun 		/* Set values to "undefined" */
1967*4882a593Smuzhiyun 		phy->cable_length = E1000_CABLE_LENGTH_UNDEFINED;
1968*4882a593Smuzhiyun 		phy->local_rx = e1000_1000t_rx_status_undefined;
1969*4882a593Smuzhiyun 		phy->remote_rx = e1000_1000t_rx_status_undefined;
1970*4882a593Smuzhiyun 	}
1971*4882a593Smuzhiyun 
1972*4882a593Smuzhiyun out:
1973*4882a593Smuzhiyun 	return ret_val;
1974*4882a593Smuzhiyun }
1975*4882a593Smuzhiyun 
1976*4882a593Smuzhiyun /**
1977*4882a593Smuzhiyun  *  igb_get_phy_info_igp - Retrieve igp PHY information
1978*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1979*4882a593Smuzhiyun  *
1980*4882a593Smuzhiyun  *  Read PHY status to determine if link is up.  If link is up, then
1981*4882a593Smuzhiyun  *  set/determine 10base-T extended distance and polarity correction.  Read
1982*4882a593Smuzhiyun  *  PHY port status to determine MDI/MDIx and speed.  Based on the speed,
1983*4882a593Smuzhiyun  *  determine on the cable length, local and remote receiver.
1984*4882a593Smuzhiyun  **/
igb_get_phy_info_igp(struct e1000_hw * hw)1985*4882a593Smuzhiyun s32 igb_get_phy_info_igp(struct e1000_hw *hw)
1986*4882a593Smuzhiyun {
1987*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1988*4882a593Smuzhiyun 	s32 ret_val;
1989*4882a593Smuzhiyun 	u16 data;
1990*4882a593Smuzhiyun 	bool link;
1991*4882a593Smuzhiyun 
1992*4882a593Smuzhiyun 	ret_val = igb_phy_has_link(hw, 1, 0, &link);
1993*4882a593Smuzhiyun 	if (ret_val)
1994*4882a593Smuzhiyun 		goto out;
1995*4882a593Smuzhiyun 
1996*4882a593Smuzhiyun 	if (!link) {
1997*4882a593Smuzhiyun 		hw_dbg("Phy info is only valid if link is up\n");
1998*4882a593Smuzhiyun 		ret_val = -E1000_ERR_CONFIG;
1999*4882a593Smuzhiyun 		goto out;
2000*4882a593Smuzhiyun 	}
2001*4882a593Smuzhiyun 
2002*4882a593Smuzhiyun 	phy->polarity_correction = true;
2003*4882a593Smuzhiyun 
2004*4882a593Smuzhiyun 	ret_val = igb_check_polarity_igp(hw);
2005*4882a593Smuzhiyun 	if (ret_val)
2006*4882a593Smuzhiyun 		goto out;
2007*4882a593Smuzhiyun 
2008*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_STATUS, &data);
2009*4882a593Smuzhiyun 	if (ret_val)
2010*4882a593Smuzhiyun 		goto out;
2011*4882a593Smuzhiyun 
2012*4882a593Smuzhiyun 	phy->is_mdix = (data & IGP01E1000_PSSR_MDIX) ? true : false;
2013*4882a593Smuzhiyun 
2014*4882a593Smuzhiyun 	if ((data & IGP01E1000_PSSR_SPEED_MASK) ==
2015*4882a593Smuzhiyun 	    IGP01E1000_PSSR_SPEED_1000MBPS) {
2016*4882a593Smuzhiyun 		ret_val = phy->ops.get_cable_length(hw);
2017*4882a593Smuzhiyun 		if (ret_val)
2018*4882a593Smuzhiyun 			goto out;
2019*4882a593Smuzhiyun 
2020*4882a593Smuzhiyun 		ret_val = phy->ops.read_reg(hw, PHY_1000T_STATUS, &data);
2021*4882a593Smuzhiyun 		if (ret_val)
2022*4882a593Smuzhiyun 			goto out;
2023*4882a593Smuzhiyun 
2024*4882a593Smuzhiyun 		phy->local_rx = (data & SR_1000T_LOCAL_RX_STATUS)
2025*4882a593Smuzhiyun 				? e1000_1000t_rx_status_ok
2026*4882a593Smuzhiyun 				: e1000_1000t_rx_status_not_ok;
2027*4882a593Smuzhiyun 
2028*4882a593Smuzhiyun 		phy->remote_rx = (data & SR_1000T_REMOTE_RX_STATUS)
2029*4882a593Smuzhiyun 				 ? e1000_1000t_rx_status_ok
2030*4882a593Smuzhiyun 				 : e1000_1000t_rx_status_not_ok;
2031*4882a593Smuzhiyun 	} else {
2032*4882a593Smuzhiyun 		phy->cable_length = E1000_CABLE_LENGTH_UNDEFINED;
2033*4882a593Smuzhiyun 		phy->local_rx = e1000_1000t_rx_status_undefined;
2034*4882a593Smuzhiyun 		phy->remote_rx = e1000_1000t_rx_status_undefined;
2035*4882a593Smuzhiyun 	}
2036*4882a593Smuzhiyun 
2037*4882a593Smuzhiyun out:
2038*4882a593Smuzhiyun 	return ret_val;
2039*4882a593Smuzhiyun }
2040*4882a593Smuzhiyun 
2041*4882a593Smuzhiyun /**
2042*4882a593Smuzhiyun  *  igb_phy_sw_reset - PHY software reset
2043*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2044*4882a593Smuzhiyun  *
2045*4882a593Smuzhiyun  *  Does a software reset of the PHY by reading the PHY control register and
2046*4882a593Smuzhiyun  *  setting/write the control register reset bit to the PHY.
2047*4882a593Smuzhiyun  **/
igb_phy_sw_reset(struct e1000_hw * hw)2048*4882a593Smuzhiyun s32 igb_phy_sw_reset(struct e1000_hw *hw)
2049*4882a593Smuzhiyun {
2050*4882a593Smuzhiyun 	s32 ret_val = 0;
2051*4882a593Smuzhiyun 	u16 phy_ctrl;
2052*4882a593Smuzhiyun 
2053*4882a593Smuzhiyun 	if (!(hw->phy.ops.read_reg))
2054*4882a593Smuzhiyun 		goto out;
2055*4882a593Smuzhiyun 
2056*4882a593Smuzhiyun 	ret_val = hw->phy.ops.read_reg(hw, PHY_CONTROL, &phy_ctrl);
2057*4882a593Smuzhiyun 	if (ret_val)
2058*4882a593Smuzhiyun 		goto out;
2059*4882a593Smuzhiyun 
2060*4882a593Smuzhiyun 	phy_ctrl |= MII_CR_RESET;
2061*4882a593Smuzhiyun 	ret_val = hw->phy.ops.write_reg(hw, PHY_CONTROL, phy_ctrl);
2062*4882a593Smuzhiyun 	if (ret_val)
2063*4882a593Smuzhiyun 		goto out;
2064*4882a593Smuzhiyun 
2065*4882a593Smuzhiyun 	udelay(1);
2066*4882a593Smuzhiyun 
2067*4882a593Smuzhiyun out:
2068*4882a593Smuzhiyun 	return ret_val;
2069*4882a593Smuzhiyun }
2070*4882a593Smuzhiyun 
2071*4882a593Smuzhiyun /**
2072*4882a593Smuzhiyun  *  igb_phy_hw_reset - PHY hardware reset
2073*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2074*4882a593Smuzhiyun  *
2075*4882a593Smuzhiyun  *  Verify the reset block is not blocking us from resetting.  Acquire
2076*4882a593Smuzhiyun  *  semaphore (if necessary) and read/set/write the device control reset
2077*4882a593Smuzhiyun  *  bit in the PHY.  Wait the appropriate delay time for the device to
2078*4882a593Smuzhiyun  *  reset and release the semaphore (if necessary).
2079*4882a593Smuzhiyun  **/
igb_phy_hw_reset(struct e1000_hw * hw)2080*4882a593Smuzhiyun s32 igb_phy_hw_reset(struct e1000_hw *hw)
2081*4882a593Smuzhiyun {
2082*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
2083*4882a593Smuzhiyun 	s32  ret_val;
2084*4882a593Smuzhiyun 	u32 ctrl;
2085*4882a593Smuzhiyun 
2086*4882a593Smuzhiyun 	ret_val = igb_check_reset_block(hw);
2087*4882a593Smuzhiyun 	if (ret_val) {
2088*4882a593Smuzhiyun 		ret_val = 0;
2089*4882a593Smuzhiyun 		goto out;
2090*4882a593Smuzhiyun 	}
2091*4882a593Smuzhiyun 
2092*4882a593Smuzhiyun 	ret_val = phy->ops.acquire(hw);
2093*4882a593Smuzhiyun 	if (ret_val)
2094*4882a593Smuzhiyun 		goto out;
2095*4882a593Smuzhiyun 
2096*4882a593Smuzhiyun 	ctrl = rd32(E1000_CTRL);
2097*4882a593Smuzhiyun 	wr32(E1000_CTRL, ctrl | E1000_CTRL_PHY_RST);
2098*4882a593Smuzhiyun 	wrfl();
2099*4882a593Smuzhiyun 
2100*4882a593Smuzhiyun 	udelay(phy->reset_delay_us);
2101*4882a593Smuzhiyun 
2102*4882a593Smuzhiyun 	wr32(E1000_CTRL, ctrl);
2103*4882a593Smuzhiyun 	wrfl();
2104*4882a593Smuzhiyun 
2105*4882a593Smuzhiyun 	udelay(150);
2106*4882a593Smuzhiyun 
2107*4882a593Smuzhiyun 	phy->ops.release(hw);
2108*4882a593Smuzhiyun 
2109*4882a593Smuzhiyun 	ret_val = phy->ops.get_cfg_done(hw);
2110*4882a593Smuzhiyun 
2111*4882a593Smuzhiyun out:
2112*4882a593Smuzhiyun 	return ret_val;
2113*4882a593Smuzhiyun }
2114*4882a593Smuzhiyun 
2115*4882a593Smuzhiyun /**
2116*4882a593Smuzhiyun  *  igb_phy_init_script_igp3 - Inits the IGP3 PHY
2117*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2118*4882a593Smuzhiyun  *
2119*4882a593Smuzhiyun  *  Initializes a Intel Gigabit PHY3 when an EEPROM is not present.
2120*4882a593Smuzhiyun  **/
igb_phy_init_script_igp3(struct e1000_hw * hw)2121*4882a593Smuzhiyun s32 igb_phy_init_script_igp3(struct e1000_hw *hw)
2122*4882a593Smuzhiyun {
2123*4882a593Smuzhiyun 	hw_dbg("Running IGP 3 PHY init script\n");
2124*4882a593Smuzhiyun 
2125*4882a593Smuzhiyun 	/* PHY init IGP 3 */
2126*4882a593Smuzhiyun 	/* Enable rise/fall, 10-mode work in class-A */
2127*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x2F5B, 0x9018);
2128*4882a593Smuzhiyun 	/* Remove all caps from Replica path filter */
2129*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x2F52, 0x0000);
2130*4882a593Smuzhiyun 	/* Bias trimming for ADC, AFE and Driver (Default) */
2131*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x2FB1, 0x8B24);
2132*4882a593Smuzhiyun 	/* Increase Hybrid poly bias */
2133*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x2FB2, 0xF8F0);
2134*4882a593Smuzhiyun 	/* Add 4% to TX amplitude in Giga mode */
2135*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x2010, 0x10B0);
2136*4882a593Smuzhiyun 	/* Disable trimming (TTT) */
2137*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x2011, 0x0000);
2138*4882a593Smuzhiyun 	/* Poly DC correction to 94.6% + 2% for all channels */
2139*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x20DD, 0x249A);
2140*4882a593Smuzhiyun 	/* ABS DC correction to 95.9% */
2141*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x20DE, 0x00D3);
2142*4882a593Smuzhiyun 	/* BG temp curve trim */
2143*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x28B4, 0x04CE);
2144*4882a593Smuzhiyun 	/* Increasing ADC OPAMP stage 1 currents to max */
2145*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x2F70, 0x29E4);
2146*4882a593Smuzhiyun 	/* Force 1000 ( required for enabling PHY regs configuration) */
2147*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x0000, 0x0140);
2148*4882a593Smuzhiyun 	/* Set upd_freq to 6 */
2149*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x1F30, 0x1606);
2150*4882a593Smuzhiyun 	/* Disable NPDFE */
2151*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x1F31, 0xB814);
2152*4882a593Smuzhiyun 	/* Disable adaptive fixed FFE (Default) */
2153*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x1F35, 0x002A);
2154*4882a593Smuzhiyun 	/* Enable FFE hysteresis */
2155*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x1F3E, 0x0067);
2156*4882a593Smuzhiyun 	/* Fixed FFE for short cable lengths */
2157*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x1F54, 0x0065);
2158*4882a593Smuzhiyun 	/* Fixed FFE for medium cable lengths */
2159*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x1F55, 0x002A);
2160*4882a593Smuzhiyun 	/* Fixed FFE for long cable lengths */
2161*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x1F56, 0x002A);
2162*4882a593Smuzhiyun 	/* Enable Adaptive Clip Threshold */
2163*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x1F72, 0x3FB0);
2164*4882a593Smuzhiyun 	/* AHT reset limit to 1 */
2165*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x1F76, 0xC0FF);
2166*4882a593Smuzhiyun 	/* Set AHT master delay to 127 msec */
2167*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x1F77, 0x1DEC);
2168*4882a593Smuzhiyun 	/* Set scan bits for AHT */
2169*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x1F78, 0xF9EF);
2170*4882a593Smuzhiyun 	/* Set AHT Preset bits */
2171*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x1F79, 0x0210);
2172*4882a593Smuzhiyun 	/* Change integ_factor of channel A to 3 */
2173*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x1895, 0x0003);
2174*4882a593Smuzhiyun 	/* Change prop_factor of channels BCD to 8 */
2175*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x1796, 0x0008);
2176*4882a593Smuzhiyun 	/* Change cg_icount + enable integbp for channels BCD */
2177*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x1798, 0xD008);
2178*4882a593Smuzhiyun 	/* Change cg_icount + enable integbp + change prop_factor_master
2179*4882a593Smuzhiyun 	 * to 8 for channel A
2180*4882a593Smuzhiyun 	 */
2181*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x1898, 0xD918);
2182*4882a593Smuzhiyun 	/* Disable AHT in Slave mode on channel A */
2183*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x187A, 0x0800);
2184*4882a593Smuzhiyun 	/* Enable LPLU and disable AN to 1000 in non-D0a states,
2185*4882a593Smuzhiyun 	 * Enable SPD+B2B
2186*4882a593Smuzhiyun 	 */
2187*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x0019, 0x008D);
2188*4882a593Smuzhiyun 	/* Enable restart AN on an1000_dis change */
2189*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x001B, 0x2080);
2190*4882a593Smuzhiyun 	/* Enable wh_fifo read clock in 10/100 modes */
2191*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x0014, 0x0045);
2192*4882a593Smuzhiyun 	/* Restart AN, Speed selection is 1000 */
2193*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, 0x0000, 0x1340);
2194*4882a593Smuzhiyun 
2195*4882a593Smuzhiyun 	return 0;
2196*4882a593Smuzhiyun }
2197*4882a593Smuzhiyun 
2198*4882a593Smuzhiyun /**
2199*4882a593Smuzhiyun  *  igb_initialize_M88E1512_phy - Initialize M88E1512 PHY
2200*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2201*4882a593Smuzhiyun  *
2202*4882a593Smuzhiyun  *  Initialize Marvel 1512 to work correctly with Avoton.
2203*4882a593Smuzhiyun  **/
igb_initialize_M88E1512_phy(struct e1000_hw * hw)2204*4882a593Smuzhiyun s32 igb_initialize_M88E1512_phy(struct e1000_hw *hw)
2205*4882a593Smuzhiyun {
2206*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
2207*4882a593Smuzhiyun 	s32 ret_val = 0;
2208*4882a593Smuzhiyun 
2209*4882a593Smuzhiyun 	/* Switch to PHY page 0xFF. */
2210*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x00FF);
2211*4882a593Smuzhiyun 	if (ret_val)
2212*4882a593Smuzhiyun 		goto out;
2213*4882a593Smuzhiyun 
2214*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0x214B);
2215*4882a593Smuzhiyun 	if (ret_val)
2216*4882a593Smuzhiyun 		goto out;
2217*4882a593Smuzhiyun 
2218*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2144);
2219*4882a593Smuzhiyun 	if (ret_val)
2220*4882a593Smuzhiyun 		goto out;
2221*4882a593Smuzhiyun 
2222*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0x0C28);
2223*4882a593Smuzhiyun 	if (ret_val)
2224*4882a593Smuzhiyun 		goto out;
2225*4882a593Smuzhiyun 
2226*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2146);
2227*4882a593Smuzhiyun 	if (ret_val)
2228*4882a593Smuzhiyun 		goto out;
2229*4882a593Smuzhiyun 
2230*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0xB233);
2231*4882a593Smuzhiyun 	if (ret_val)
2232*4882a593Smuzhiyun 		goto out;
2233*4882a593Smuzhiyun 
2234*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x214D);
2235*4882a593Smuzhiyun 	if (ret_val)
2236*4882a593Smuzhiyun 		goto out;
2237*4882a593Smuzhiyun 
2238*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0xCC0C);
2239*4882a593Smuzhiyun 	if (ret_val)
2240*4882a593Smuzhiyun 		goto out;
2241*4882a593Smuzhiyun 
2242*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2159);
2243*4882a593Smuzhiyun 	if (ret_val)
2244*4882a593Smuzhiyun 		goto out;
2245*4882a593Smuzhiyun 
2246*4882a593Smuzhiyun 	/* Switch to PHY page 0xFB. */
2247*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x00FB);
2248*4882a593Smuzhiyun 	if (ret_val)
2249*4882a593Smuzhiyun 		goto out;
2250*4882a593Smuzhiyun 
2251*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_3, 0x000D);
2252*4882a593Smuzhiyun 	if (ret_val)
2253*4882a593Smuzhiyun 		goto out;
2254*4882a593Smuzhiyun 
2255*4882a593Smuzhiyun 	/* Switch to PHY page 0x12. */
2256*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x12);
2257*4882a593Smuzhiyun 	if (ret_val)
2258*4882a593Smuzhiyun 		goto out;
2259*4882a593Smuzhiyun 
2260*4882a593Smuzhiyun 	/* Change mode to SGMII-to-Copper */
2261*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_MODE, 0x8001);
2262*4882a593Smuzhiyun 	if (ret_val)
2263*4882a593Smuzhiyun 		goto out;
2264*4882a593Smuzhiyun 
2265*4882a593Smuzhiyun 	/* Return the PHY to page 0. */
2266*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0);
2267*4882a593Smuzhiyun 	if (ret_val)
2268*4882a593Smuzhiyun 		goto out;
2269*4882a593Smuzhiyun 
2270*4882a593Smuzhiyun 	ret_val = igb_phy_sw_reset(hw);
2271*4882a593Smuzhiyun 	if (ret_val) {
2272*4882a593Smuzhiyun 		hw_dbg("Error committing the PHY changes\n");
2273*4882a593Smuzhiyun 		return ret_val;
2274*4882a593Smuzhiyun 	}
2275*4882a593Smuzhiyun 
2276*4882a593Smuzhiyun 	/* msec_delay(1000); */
2277*4882a593Smuzhiyun 	usleep_range(1000, 2000);
2278*4882a593Smuzhiyun out:
2279*4882a593Smuzhiyun 	return ret_val;
2280*4882a593Smuzhiyun }
2281*4882a593Smuzhiyun 
2282*4882a593Smuzhiyun /**
2283*4882a593Smuzhiyun  *  igb_initialize_M88E1543_phy - Initialize M88E1512 PHY
2284*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2285*4882a593Smuzhiyun  *
2286*4882a593Smuzhiyun  *  Initialize Marvell 1543 to work correctly with Avoton.
2287*4882a593Smuzhiyun  **/
igb_initialize_M88E1543_phy(struct e1000_hw * hw)2288*4882a593Smuzhiyun s32 igb_initialize_M88E1543_phy(struct e1000_hw *hw)
2289*4882a593Smuzhiyun {
2290*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
2291*4882a593Smuzhiyun 	s32 ret_val = 0;
2292*4882a593Smuzhiyun 
2293*4882a593Smuzhiyun 	/* Switch to PHY page 0xFF. */
2294*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x00FF);
2295*4882a593Smuzhiyun 	if (ret_val)
2296*4882a593Smuzhiyun 		goto out;
2297*4882a593Smuzhiyun 
2298*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0x214B);
2299*4882a593Smuzhiyun 	if (ret_val)
2300*4882a593Smuzhiyun 		goto out;
2301*4882a593Smuzhiyun 
2302*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2144);
2303*4882a593Smuzhiyun 	if (ret_val)
2304*4882a593Smuzhiyun 		goto out;
2305*4882a593Smuzhiyun 
2306*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0x0C28);
2307*4882a593Smuzhiyun 	if (ret_val)
2308*4882a593Smuzhiyun 		goto out;
2309*4882a593Smuzhiyun 
2310*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2146);
2311*4882a593Smuzhiyun 	if (ret_val)
2312*4882a593Smuzhiyun 		goto out;
2313*4882a593Smuzhiyun 
2314*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0xB233);
2315*4882a593Smuzhiyun 	if (ret_val)
2316*4882a593Smuzhiyun 		goto out;
2317*4882a593Smuzhiyun 
2318*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x214D);
2319*4882a593Smuzhiyun 	if (ret_val)
2320*4882a593Smuzhiyun 		goto out;
2321*4882a593Smuzhiyun 
2322*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_2, 0xDC0C);
2323*4882a593Smuzhiyun 	if (ret_val)
2324*4882a593Smuzhiyun 		goto out;
2325*4882a593Smuzhiyun 
2326*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_1, 0x2159);
2327*4882a593Smuzhiyun 	if (ret_val)
2328*4882a593Smuzhiyun 		goto out;
2329*4882a593Smuzhiyun 
2330*4882a593Smuzhiyun 	/* Switch to PHY page 0xFB. */
2331*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x00FB);
2332*4882a593Smuzhiyun 	if (ret_val)
2333*4882a593Smuzhiyun 		goto out;
2334*4882a593Smuzhiyun 
2335*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_CFG_REG_3, 0x0C0D);
2336*4882a593Smuzhiyun 	if (ret_val)
2337*4882a593Smuzhiyun 		goto out;
2338*4882a593Smuzhiyun 
2339*4882a593Smuzhiyun 	/* Switch to PHY page 0x12. */
2340*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x12);
2341*4882a593Smuzhiyun 	if (ret_val)
2342*4882a593Smuzhiyun 		goto out;
2343*4882a593Smuzhiyun 
2344*4882a593Smuzhiyun 	/* Change mode to SGMII-to-Copper */
2345*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1512_MODE, 0x8001);
2346*4882a593Smuzhiyun 	if (ret_val)
2347*4882a593Smuzhiyun 		goto out;
2348*4882a593Smuzhiyun 
2349*4882a593Smuzhiyun 	/* Switch to PHY page 1. */
2350*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0x1);
2351*4882a593Smuzhiyun 	if (ret_val)
2352*4882a593Smuzhiyun 		goto out;
2353*4882a593Smuzhiyun 
2354*4882a593Smuzhiyun 	/* Change mode to 1000BASE-X/SGMII and autoneg enable */
2355*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1543_FIBER_CTRL, 0x9140);
2356*4882a593Smuzhiyun 	if (ret_val)
2357*4882a593Smuzhiyun 		goto out;
2358*4882a593Smuzhiyun 
2359*4882a593Smuzhiyun 	/* Return the PHY to page 0. */
2360*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, E1000_M88E1543_PAGE_ADDR, 0);
2361*4882a593Smuzhiyun 	if (ret_val)
2362*4882a593Smuzhiyun 		goto out;
2363*4882a593Smuzhiyun 
2364*4882a593Smuzhiyun 	ret_val = igb_phy_sw_reset(hw);
2365*4882a593Smuzhiyun 	if (ret_val) {
2366*4882a593Smuzhiyun 		hw_dbg("Error committing the PHY changes\n");
2367*4882a593Smuzhiyun 		return ret_val;
2368*4882a593Smuzhiyun 	}
2369*4882a593Smuzhiyun 
2370*4882a593Smuzhiyun 	/* msec_delay(1000); */
2371*4882a593Smuzhiyun 	usleep_range(1000, 2000);
2372*4882a593Smuzhiyun out:
2373*4882a593Smuzhiyun 	return ret_val;
2374*4882a593Smuzhiyun }
2375*4882a593Smuzhiyun 
2376*4882a593Smuzhiyun /**
2377*4882a593Smuzhiyun  * igb_power_up_phy_copper - Restore copper link in case of PHY power down
2378*4882a593Smuzhiyun  * @hw: pointer to the HW structure
2379*4882a593Smuzhiyun  *
2380*4882a593Smuzhiyun  * In the case of a PHY power down to save power, or to turn off link during a
2381*4882a593Smuzhiyun  * driver unload, restore the link to previous settings.
2382*4882a593Smuzhiyun  **/
igb_power_up_phy_copper(struct e1000_hw * hw)2383*4882a593Smuzhiyun void igb_power_up_phy_copper(struct e1000_hw *hw)
2384*4882a593Smuzhiyun {
2385*4882a593Smuzhiyun 	u16 mii_reg = 0;
2386*4882a593Smuzhiyun 
2387*4882a593Smuzhiyun 	/* The PHY will retain its settings across a power down/up cycle */
2388*4882a593Smuzhiyun 	hw->phy.ops.read_reg(hw, PHY_CONTROL, &mii_reg);
2389*4882a593Smuzhiyun 	mii_reg &= ~MII_CR_POWER_DOWN;
2390*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, PHY_CONTROL, mii_reg);
2391*4882a593Smuzhiyun }
2392*4882a593Smuzhiyun 
2393*4882a593Smuzhiyun /**
2394*4882a593Smuzhiyun  * igb_power_down_phy_copper - Power down copper PHY
2395*4882a593Smuzhiyun  * @hw: pointer to the HW structure
2396*4882a593Smuzhiyun  *
2397*4882a593Smuzhiyun  * Power down PHY to save power when interface is down and wake on lan
2398*4882a593Smuzhiyun  * is not enabled.
2399*4882a593Smuzhiyun  **/
igb_power_down_phy_copper(struct e1000_hw * hw)2400*4882a593Smuzhiyun void igb_power_down_phy_copper(struct e1000_hw *hw)
2401*4882a593Smuzhiyun {
2402*4882a593Smuzhiyun 	u16 mii_reg = 0;
2403*4882a593Smuzhiyun 
2404*4882a593Smuzhiyun 	/* The PHY will retain its settings across a power down/up cycle */
2405*4882a593Smuzhiyun 	hw->phy.ops.read_reg(hw, PHY_CONTROL, &mii_reg);
2406*4882a593Smuzhiyun 	mii_reg |= MII_CR_POWER_DOWN;
2407*4882a593Smuzhiyun 	hw->phy.ops.write_reg(hw, PHY_CONTROL, mii_reg);
2408*4882a593Smuzhiyun 	usleep_range(1000, 2000);
2409*4882a593Smuzhiyun }
2410*4882a593Smuzhiyun 
2411*4882a593Smuzhiyun /**
2412*4882a593Smuzhiyun  *  igb_check_polarity_82580 - Checks the polarity.
2413*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2414*4882a593Smuzhiyun  *
2415*4882a593Smuzhiyun  *  Success returns 0, Failure returns -E1000_ERR_PHY (-2)
2416*4882a593Smuzhiyun  *
2417*4882a593Smuzhiyun  *  Polarity is determined based on the PHY specific status register.
2418*4882a593Smuzhiyun  **/
igb_check_polarity_82580(struct e1000_hw * hw)2419*4882a593Smuzhiyun static s32 igb_check_polarity_82580(struct e1000_hw *hw)
2420*4882a593Smuzhiyun {
2421*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
2422*4882a593Smuzhiyun 	s32 ret_val;
2423*4882a593Smuzhiyun 	u16 data;
2424*4882a593Smuzhiyun 
2425*4882a593Smuzhiyun 
2426*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, I82580_PHY_STATUS_2, &data);
2427*4882a593Smuzhiyun 
2428*4882a593Smuzhiyun 	if (!ret_val)
2429*4882a593Smuzhiyun 		phy->cable_polarity = (data & I82580_PHY_STATUS2_REV_POLARITY)
2430*4882a593Smuzhiyun 				      ? e1000_rev_polarity_reversed
2431*4882a593Smuzhiyun 				      : e1000_rev_polarity_normal;
2432*4882a593Smuzhiyun 
2433*4882a593Smuzhiyun 	return ret_val;
2434*4882a593Smuzhiyun }
2435*4882a593Smuzhiyun 
2436*4882a593Smuzhiyun /**
2437*4882a593Smuzhiyun  *  igb_phy_force_speed_duplex_82580 - Force speed/duplex for I82580 PHY
2438*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2439*4882a593Smuzhiyun  *
2440*4882a593Smuzhiyun  *  Calls the PHY setup function to force speed and duplex.  Clears the
2441*4882a593Smuzhiyun  *  auto-crossover to force MDI manually.  Waits for link and returns
2442*4882a593Smuzhiyun  *  successful if link up is successful, else -E1000_ERR_PHY (-2).
2443*4882a593Smuzhiyun  **/
igb_phy_force_speed_duplex_82580(struct e1000_hw * hw)2444*4882a593Smuzhiyun s32 igb_phy_force_speed_duplex_82580(struct e1000_hw *hw)
2445*4882a593Smuzhiyun {
2446*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
2447*4882a593Smuzhiyun 	s32 ret_val;
2448*4882a593Smuzhiyun 	u16 phy_data;
2449*4882a593Smuzhiyun 	bool link;
2450*4882a593Smuzhiyun 
2451*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_data);
2452*4882a593Smuzhiyun 	if (ret_val)
2453*4882a593Smuzhiyun 		goto out;
2454*4882a593Smuzhiyun 
2455*4882a593Smuzhiyun 	igb_phy_force_speed_duplex_setup(hw, &phy_data);
2456*4882a593Smuzhiyun 
2457*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_data);
2458*4882a593Smuzhiyun 	if (ret_val)
2459*4882a593Smuzhiyun 		goto out;
2460*4882a593Smuzhiyun 
2461*4882a593Smuzhiyun 	/* Clear Auto-Crossover to force MDI manually.  82580 requires MDI
2462*4882a593Smuzhiyun 	 * forced whenever speed and duplex are forced.
2463*4882a593Smuzhiyun 	 */
2464*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, I82580_PHY_CTRL_2, &phy_data);
2465*4882a593Smuzhiyun 	if (ret_val)
2466*4882a593Smuzhiyun 		goto out;
2467*4882a593Smuzhiyun 
2468*4882a593Smuzhiyun 	phy_data &= ~I82580_PHY_CTRL2_MDIX_CFG_MASK;
2469*4882a593Smuzhiyun 
2470*4882a593Smuzhiyun 	ret_val = phy->ops.write_reg(hw, I82580_PHY_CTRL_2, phy_data);
2471*4882a593Smuzhiyun 	if (ret_val)
2472*4882a593Smuzhiyun 		goto out;
2473*4882a593Smuzhiyun 
2474*4882a593Smuzhiyun 	hw_dbg("I82580_PHY_CTRL_2: %X\n", phy_data);
2475*4882a593Smuzhiyun 
2476*4882a593Smuzhiyun 	udelay(1);
2477*4882a593Smuzhiyun 
2478*4882a593Smuzhiyun 	if (phy->autoneg_wait_to_complete) {
2479*4882a593Smuzhiyun 		hw_dbg("Waiting for forced speed/duplex link on 82580 phy\n");
2480*4882a593Smuzhiyun 
2481*4882a593Smuzhiyun 		ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT, 100000, &link);
2482*4882a593Smuzhiyun 		if (ret_val)
2483*4882a593Smuzhiyun 			goto out;
2484*4882a593Smuzhiyun 
2485*4882a593Smuzhiyun 		if (!link)
2486*4882a593Smuzhiyun 			hw_dbg("Link taking longer than expected.\n");
2487*4882a593Smuzhiyun 
2488*4882a593Smuzhiyun 		/* Try once more */
2489*4882a593Smuzhiyun 		ret_val = igb_phy_has_link(hw, PHY_FORCE_LIMIT, 100000, &link);
2490*4882a593Smuzhiyun 		if (ret_val)
2491*4882a593Smuzhiyun 			goto out;
2492*4882a593Smuzhiyun 	}
2493*4882a593Smuzhiyun 
2494*4882a593Smuzhiyun out:
2495*4882a593Smuzhiyun 	return ret_val;
2496*4882a593Smuzhiyun }
2497*4882a593Smuzhiyun 
2498*4882a593Smuzhiyun /**
2499*4882a593Smuzhiyun  *  igb_get_phy_info_82580 - Retrieve I82580 PHY information
2500*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2501*4882a593Smuzhiyun  *
2502*4882a593Smuzhiyun  *  Read PHY status to determine if link is up.  If link is up, then
2503*4882a593Smuzhiyun  *  set/determine 10base-T extended distance and polarity correction.  Read
2504*4882a593Smuzhiyun  *  PHY port status to determine MDI/MDIx and speed.  Based on the speed,
2505*4882a593Smuzhiyun  *  determine on the cable length, local and remote receiver.
2506*4882a593Smuzhiyun  **/
igb_get_phy_info_82580(struct e1000_hw * hw)2507*4882a593Smuzhiyun s32 igb_get_phy_info_82580(struct e1000_hw *hw)
2508*4882a593Smuzhiyun {
2509*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
2510*4882a593Smuzhiyun 	s32 ret_val;
2511*4882a593Smuzhiyun 	u16 data;
2512*4882a593Smuzhiyun 	bool link;
2513*4882a593Smuzhiyun 
2514*4882a593Smuzhiyun 	ret_val = igb_phy_has_link(hw, 1, 0, &link);
2515*4882a593Smuzhiyun 	if (ret_val)
2516*4882a593Smuzhiyun 		goto out;
2517*4882a593Smuzhiyun 
2518*4882a593Smuzhiyun 	if (!link) {
2519*4882a593Smuzhiyun 		hw_dbg("Phy info is only valid if link is up\n");
2520*4882a593Smuzhiyun 		ret_val = -E1000_ERR_CONFIG;
2521*4882a593Smuzhiyun 		goto out;
2522*4882a593Smuzhiyun 	}
2523*4882a593Smuzhiyun 
2524*4882a593Smuzhiyun 	phy->polarity_correction = true;
2525*4882a593Smuzhiyun 
2526*4882a593Smuzhiyun 	ret_val = igb_check_polarity_82580(hw);
2527*4882a593Smuzhiyun 	if (ret_val)
2528*4882a593Smuzhiyun 		goto out;
2529*4882a593Smuzhiyun 
2530*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, I82580_PHY_STATUS_2, &data);
2531*4882a593Smuzhiyun 	if (ret_val)
2532*4882a593Smuzhiyun 		goto out;
2533*4882a593Smuzhiyun 
2534*4882a593Smuzhiyun 	phy->is_mdix = (data & I82580_PHY_STATUS2_MDIX) ? true : false;
2535*4882a593Smuzhiyun 
2536*4882a593Smuzhiyun 	if ((data & I82580_PHY_STATUS2_SPEED_MASK) ==
2537*4882a593Smuzhiyun 	    I82580_PHY_STATUS2_SPEED_1000MBPS) {
2538*4882a593Smuzhiyun 		ret_val = hw->phy.ops.get_cable_length(hw);
2539*4882a593Smuzhiyun 		if (ret_val)
2540*4882a593Smuzhiyun 			goto out;
2541*4882a593Smuzhiyun 
2542*4882a593Smuzhiyun 		ret_val = phy->ops.read_reg(hw, PHY_1000T_STATUS, &data);
2543*4882a593Smuzhiyun 		if (ret_val)
2544*4882a593Smuzhiyun 			goto out;
2545*4882a593Smuzhiyun 
2546*4882a593Smuzhiyun 		phy->local_rx = (data & SR_1000T_LOCAL_RX_STATUS)
2547*4882a593Smuzhiyun 				? e1000_1000t_rx_status_ok
2548*4882a593Smuzhiyun 				: e1000_1000t_rx_status_not_ok;
2549*4882a593Smuzhiyun 
2550*4882a593Smuzhiyun 		phy->remote_rx = (data & SR_1000T_REMOTE_RX_STATUS)
2551*4882a593Smuzhiyun 				 ? e1000_1000t_rx_status_ok
2552*4882a593Smuzhiyun 				 : e1000_1000t_rx_status_not_ok;
2553*4882a593Smuzhiyun 	} else {
2554*4882a593Smuzhiyun 		phy->cable_length = E1000_CABLE_LENGTH_UNDEFINED;
2555*4882a593Smuzhiyun 		phy->local_rx = e1000_1000t_rx_status_undefined;
2556*4882a593Smuzhiyun 		phy->remote_rx = e1000_1000t_rx_status_undefined;
2557*4882a593Smuzhiyun 	}
2558*4882a593Smuzhiyun 
2559*4882a593Smuzhiyun out:
2560*4882a593Smuzhiyun 	return ret_val;
2561*4882a593Smuzhiyun }
2562*4882a593Smuzhiyun 
2563*4882a593Smuzhiyun /**
2564*4882a593Smuzhiyun  *  igb_get_cable_length_82580 - Determine cable length for 82580 PHY
2565*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2566*4882a593Smuzhiyun  *
2567*4882a593Smuzhiyun  * Reads the diagnostic status register and verifies result is valid before
2568*4882a593Smuzhiyun  * placing it in the phy_cable_length field.
2569*4882a593Smuzhiyun  **/
igb_get_cable_length_82580(struct e1000_hw * hw)2570*4882a593Smuzhiyun s32 igb_get_cable_length_82580(struct e1000_hw *hw)
2571*4882a593Smuzhiyun {
2572*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
2573*4882a593Smuzhiyun 	s32 ret_val;
2574*4882a593Smuzhiyun 	u16 phy_data, length;
2575*4882a593Smuzhiyun 
2576*4882a593Smuzhiyun 	ret_val = phy->ops.read_reg(hw, I82580_PHY_DIAG_STATUS, &phy_data);
2577*4882a593Smuzhiyun 	if (ret_val)
2578*4882a593Smuzhiyun 		goto out;
2579*4882a593Smuzhiyun 
2580*4882a593Smuzhiyun 	length = (phy_data & I82580_DSTATUS_CABLE_LENGTH) >>
2581*4882a593Smuzhiyun 		 I82580_DSTATUS_CABLE_LENGTH_SHIFT;
2582*4882a593Smuzhiyun 
2583*4882a593Smuzhiyun 	if (length == E1000_CABLE_LENGTH_UNDEFINED)
2584*4882a593Smuzhiyun 		ret_val = -E1000_ERR_PHY;
2585*4882a593Smuzhiyun 
2586*4882a593Smuzhiyun 	phy->cable_length = length;
2587*4882a593Smuzhiyun 
2588*4882a593Smuzhiyun out:
2589*4882a593Smuzhiyun 	return ret_val;
2590*4882a593Smuzhiyun }
2591*4882a593Smuzhiyun 
2592*4882a593Smuzhiyun /**
2593*4882a593Smuzhiyun  *  igb_set_master_slave_mode - Setup PHY for Master/slave mode
2594*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2595*4882a593Smuzhiyun  *
2596*4882a593Smuzhiyun  *  Sets up Master/slave mode
2597*4882a593Smuzhiyun  **/
igb_set_master_slave_mode(struct e1000_hw * hw)2598*4882a593Smuzhiyun static s32 igb_set_master_slave_mode(struct e1000_hw *hw)
2599*4882a593Smuzhiyun {
2600*4882a593Smuzhiyun 	s32 ret_val;
2601*4882a593Smuzhiyun 	u16 phy_data;
2602*4882a593Smuzhiyun 
2603*4882a593Smuzhiyun 	/* Resolve Master/Slave mode */
2604*4882a593Smuzhiyun 	ret_val = hw->phy.ops.read_reg(hw, PHY_1000T_CTRL, &phy_data);
2605*4882a593Smuzhiyun 	if (ret_val)
2606*4882a593Smuzhiyun 		return ret_val;
2607*4882a593Smuzhiyun 
2608*4882a593Smuzhiyun 	/* load defaults for future use */
2609*4882a593Smuzhiyun 	hw->phy.original_ms_type = (phy_data & CR_1000T_MS_ENABLE) ?
2610*4882a593Smuzhiyun 				   ((phy_data & CR_1000T_MS_VALUE) ?
2611*4882a593Smuzhiyun 				    e1000_ms_force_master :
2612*4882a593Smuzhiyun 				    e1000_ms_force_slave) : e1000_ms_auto;
2613*4882a593Smuzhiyun 
2614*4882a593Smuzhiyun 	switch (hw->phy.ms_type) {
2615*4882a593Smuzhiyun 	case e1000_ms_force_master:
2616*4882a593Smuzhiyun 		phy_data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
2617*4882a593Smuzhiyun 		break;
2618*4882a593Smuzhiyun 	case e1000_ms_force_slave:
2619*4882a593Smuzhiyun 		phy_data |= CR_1000T_MS_ENABLE;
2620*4882a593Smuzhiyun 		phy_data &= ~(CR_1000T_MS_VALUE);
2621*4882a593Smuzhiyun 		break;
2622*4882a593Smuzhiyun 	case e1000_ms_auto:
2623*4882a593Smuzhiyun 		phy_data &= ~CR_1000T_MS_ENABLE;
2624*4882a593Smuzhiyun 		fallthrough;
2625*4882a593Smuzhiyun 	default:
2626*4882a593Smuzhiyun 		break;
2627*4882a593Smuzhiyun 	}
2628*4882a593Smuzhiyun 
2629*4882a593Smuzhiyun 	return hw->phy.ops.write_reg(hw, PHY_1000T_CTRL, phy_data);
2630*4882a593Smuzhiyun }
2631