xref: /OK3568_Linux_fs/kernel/drivers/net/ethernet/intel/e1000e/phy.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /* Copyright(c) 1999 - 2018 Intel Corporation. */
3*4882a593Smuzhiyun 
4*4882a593Smuzhiyun #include "e1000.h"
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun static s32 e1000_wait_autoneg(struct e1000_hw *hw);
7*4882a593Smuzhiyun static s32 e1000_access_phy_wakeup_reg_bm(struct e1000_hw *hw, u32 offset,
8*4882a593Smuzhiyun 					  u16 *data, bool read, bool page_set);
9*4882a593Smuzhiyun static u32 e1000_get_phy_addr_for_hv_page(u32 page);
10*4882a593Smuzhiyun static s32 e1000_access_phy_debug_regs_hv(struct e1000_hw *hw, u32 offset,
11*4882a593Smuzhiyun 					  u16 *data, bool read);
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun /* Cable length tables */
14*4882a593Smuzhiyun static const u16 e1000_m88_cable_length_table[] = {
15*4882a593Smuzhiyun 	0, 50, 80, 110, 140, 140, E1000_CABLE_LENGTH_UNDEFINED
16*4882a593Smuzhiyun };
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #define M88E1000_CABLE_LENGTH_TABLE_SIZE \
19*4882a593Smuzhiyun 		ARRAY_SIZE(e1000_m88_cable_length_table)
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun static const u16 e1000_igp_2_cable_length_table[] = {
22*4882a593Smuzhiyun 	0, 0, 0, 0, 0, 0, 0, 0, 3, 5, 8, 11, 13, 16, 18, 21, 0, 0, 0, 3,
23*4882a593Smuzhiyun 	6, 10, 13, 16, 19, 23, 26, 29, 32, 35, 38, 41, 6, 10, 14, 18, 22,
24*4882a593Smuzhiyun 	26, 30, 33, 37, 41, 44, 48, 51, 54, 58, 61, 21, 26, 31, 35, 40,
25*4882a593Smuzhiyun 	44, 49, 53, 57, 61, 65, 68, 72, 75, 79, 82, 40, 45, 51, 56, 61,
26*4882a593Smuzhiyun 	66, 70, 75, 79, 83, 87, 91, 94, 98, 101, 104, 60, 66, 72, 77, 82,
27*4882a593Smuzhiyun 	87, 92, 96, 100, 104, 108, 111, 114, 117, 119, 121, 83, 89, 95,
28*4882a593Smuzhiyun 	100, 105, 109, 113, 116, 119, 122, 124, 104, 109, 114, 118, 121,
29*4882a593Smuzhiyun 	124
30*4882a593Smuzhiyun };
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #define IGP02E1000_CABLE_LENGTH_TABLE_SIZE \
33*4882a593Smuzhiyun 		ARRAY_SIZE(e1000_igp_2_cable_length_table)
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /**
36*4882a593Smuzhiyun  *  e1000e_check_reset_block_generic - Check if PHY reset is blocked
37*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
38*4882a593Smuzhiyun  *
39*4882a593Smuzhiyun  *  Read the PHY management control register and check whether a PHY reset
40*4882a593Smuzhiyun  *  is blocked.  If a reset is not blocked return 0, otherwise
41*4882a593Smuzhiyun  *  return E1000_BLK_PHY_RESET (12).
42*4882a593Smuzhiyun  **/
e1000e_check_reset_block_generic(struct e1000_hw * hw)43*4882a593Smuzhiyun s32 e1000e_check_reset_block_generic(struct e1000_hw *hw)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun 	u32 manc;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	manc = er32(MANC);
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	return (manc & E1000_MANC_BLK_PHY_RST_ON_IDE) ? E1000_BLK_PHY_RESET : 0;
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /**
53*4882a593Smuzhiyun  *  e1000e_get_phy_id - Retrieve the PHY ID and revision
54*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
55*4882a593Smuzhiyun  *
56*4882a593Smuzhiyun  *  Reads the PHY registers and stores the PHY ID and possibly the PHY
57*4882a593Smuzhiyun  *  revision in the hardware structure.
58*4882a593Smuzhiyun  **/
e1000e_get_phy_id(struct e1000_hw * hw)59*4882a593Smuzhiyun s32 e1000e_get_phy_id(struct e1000_hw *hw)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
62*4882a593Smuzhiyun 	s32 ret_val = 0;
63*4882a593Smuzhiyun 	u16 phy_id;
64*4882a593Smuzhiyun 	u16 retry_count = 0;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	if (!phy->ops.read_reg)
67*4882a593Smuzhiyun 		return 0;
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	while (retry_count < 2) {
70*4882a593Smuzhiyun 		ret_val = e1e_rphy(hw, MII_PHYSID1, &phy_id);
71*4882a593Smuzhiyun 		if (ret_val)
72*4882a593Smuzhiyun 			return ret_val;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 		phy->id = (u32)(phy_id << 16);
75*4882a593Smuzhiyun 		usleep_range(20, 40);
76*4882a593Smuzhiyun 		ret_val = e1e_rphy(hw, MII_PHYSID2, &phy_id);
77*4882a593Smuzhiyun 		if (ret_val)
78*4882a593Smuzhiyun 			return ret_val;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 		phy->id |= (u32)(phy_id & PHY_REVISION_MASK);
81*4882a593Smuzhiyun 		phy->revision = (u32)(phy_id & ~PHY_REVISION_MASK);
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 		if (phy->id != 0 && phy->id != PHY_REVISION_MASK)
84*4882a593Smuzhiyun 			return 0;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 		retry_count++;
87*4882a593Smuzhiyun 	}
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	return 0;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun /**
93*4882a593Smuzhiyun  *  e1000e_phy_reset_dsp - Reset PHY DSP
94*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
95*4882a593Smuzhiyun  *
96*4882a593Smuzhiyun  *  Reset the digital signal processor.
97*4882a593Smuzhiyun  **/
e1000e_phy_reset_dsp(struct e1000_hw * hw)98*4882a593Smuzhiyun s32 e1000e_phy_reset_dsp(struct e1000_hw *hw)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun 	s32 ret_val;
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	ret_val = e1e_wphy(hw, M88E1000_PHY_GEN_CONTROL, 0xC1);
103*4882a593Smuzhiyun 	if (ret_val)
104*4882a593Smuzhiyun 		return ret_val;
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	return e1e_wphy(hw, M88E1000_PHY_GEN_CONTROL, 0);
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun /**
110*4882a593Smuzhiyun  *  e1000e_read_phy_reg_mdic - Read MDI control register
111*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
112*4882a593Smuzhiyun  *  @offset: register offset to be read
113*4882a593Smuzhiyun  *  @data: pointer to the read data
114*4882a593Smuzhiyun  *
115*4882a593Smuzhiyun  *  Reads the MDI control register in the PHY at offset and stores the
116*4882a593Smuzhiyun  *  information read to data.
117*4882a593Smuzhiyun  **/
e1000e_read_phy_reg_mdic(struct e1000_hw * hw,u32 offset,u16 * data)118*4882a593Smuzhiyun s32 e1000e_read_phy_reg_mdic(struct e1000_hw *hw, u32 offset, u16 *data)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
121*4882a593Smuzhiyun 	u32 i, mdic = 0;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	if (offset > MAX_PHY_REG_ADDRESS) {
124*4882a593Smuzhiyun 		e_dbg("PHY Address %d is out of range\n", offset);
125*4882a593Smuzhiyun 		return -E1000_ERR_PARAM;
126*4882a593Smuzhiyun 	}
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	/* Set up Op-code, Phy Address, and register offset in the MDI
129*4882a593Smuzhiyun 	 * Control register.  The MAC will take care of interfacing with the
130*4882a593Smuzhiyun 	 * PHY to retrieve the desired data.
131*4882a593Smuzhiyun 	 */
132*4882a593Smuzhiyun 	mdic = ((offset << E1000_MDIC_REG_SHIFT) |
133*4882a593Smuzhiyun 		(phy->addr << E1000_MDIC_PHY_SHIFT) |
134*4882a593Smuzhiyun 		(E1000_MDIC_OP_READ));
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	ew32(MDIC, mdic);
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	/* Poll the ready bit to see if the MDI read completed
139*4882a593Smuzhiyun 	 * Increasing the time out as testing showed failures with
140*4882a593Smuzhiyun 	 * the lower time out
141*4882a593Smuzhiyun 	 */
142*4882a593Smuzhiyun 	for (i = 0; i < (E1000_GEN_POLL_TIMEOUT * 3); i++) {
143*4882a593Smuzhiyun 		udelay(50);
144*4882a593Smuzhiyun 		mdic = er32(MDIC);
145*4882a593Smuzhiyun 		if (mdic & E1000_MDIC_READY)
146*4882a593Smuzhiyun 			break;
147*4882a593Smuzhiyun 	}
148*4882a593Smuzhiyun 	if (!(mdic & E1000_MDIC_READY)) {
149*4882a593Smuzhiyun 		e_dbg("MDI Read did not complete\n");
150*4882a593Smuzhiyun 		return -E1000_ERR_PHY;
151*4882a593Smuzhiyun 	}
152*4882a593Smuzhiyun 	if (mdic & E1000_MDIC_ERROR) {
153*4882a593Smuzhiyun 		e_dbg("MDI Error\n");
154*4882a593Smuzhiyun 		return -E1000_ERR_PHY;
155*4882a593Smuzhiyun 	}
156*4882a593Smuzhiyun 	if (((mdic & E1000_MDIC_REG_MASK) >> E1000_MDIC_REG_SHIFT) != offset) {
157*4882a593Smuzhiyun 		e_dbg("MDI Read offset error - requested %d, returned %d\n",
158*4882a593Smuzhiyun 		      offset,
159*4882a593Smuzhiyun 		      (mdic & E1000_MDIC_REG_MASK) >> E1000_MDIC_REG_SHIFT);
160*4882a593Smuzhiyun 		return -E1000_ERR_PHY;
161*4882a593Smuzhiyun 	}
162*4882a593Smuzhiyun 	*data = (u16)mdic;
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	/* Allow some time after each MDIC transaction to avoid
165*4882a593Smuzhiyun 	 * reading duplicate data in the next MDIC transaction.
166*4882a593Smuzhiyun 	 */
167*4882a593Smuzhiyun 	if (hw->mac.type == e1000_pch2lan)
168*4882a593Smuzhiyun 		udelay(100);
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	return 0;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun /**
174*4882a593Smuzhiyun  *  e1000e_write_phy_reg_mdic - Write MDI control register
175*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
176*4882a593Smuzhiyun  *  @offset: register offset to write to
177*4882a593Smuzhiyun  *  @data: data to write to register at offset
178*4882a593Smuzhiyun  *
179*4882a593Smuzhiyun  *  Writes data to MDI control register in the PHY at offset.
180*4882a593Smuzhiyun  **/
e1000e_write_phy_reg_mdic(struct e1000_hw * hw,u32 offset,u16 data)181*4882a593Smuzhiyun s32 e1000e_write_phy_reg_mdic(struct e1000_hw *hw, u32 offset, u16 data)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
184*4882a593Smuzhiyun 	u32 i, mdic = 0;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	if (offset > MAX_PHY_REG_ADDRESS) {
187*4882a593Smuzhiyun 		e_dbg("PHY Address %d is out of range\n", offset);
188*4882a593Smuzhiyun 		return -E1000_ERR_PARAM;
189*4882a593Smuzhiyun 	}
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	/* Set up Op-code, Phy Address, and register offset in the MDI
192*4882a593Smuzhiyun 	 * Control register.  The MAC will take care of interfacing with the
193*4882a593Smuzhiyun 	 * PHY to retrieve the desired data.
194*4882a593Smuzhiyun 	 */
195*4882a593Smuzhiyun 	mdic = (((u32)data) |
196*4882a593Smuzhiyun 		(offset << E1000_MDIC_REG_SHIFT) |
197*4882a593Smuzhiyun 		(phy->addr << E1000_MDIC_PHY_SHIFT) |
198*4882a593Smuzhiyun 		(E1000_MDIC_OP_WRITE));
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	ew32(MDIC, mdic);
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	/* Poll the ready bit to see if the MDI read completed
203*4882a593Smuzhiyun 	 * Increasing the time out as testing showed failures with
204*4882a593Smuzhiyun 	 * the lower time out
205*4882a593Smuzhiyun 	 */
206*4882a593Smuzhiyun 	for (i = 0; i < (E1000_GEN_POLL_TIMEOUT * 3); i++) {
207*4882a593Smuzhiyun 		udelay(50);
208*4882a593Smuzhiyun 		mdic = er32(MDIC);
209*4882a593Smuzhiyun 		if (mdic & E1000_MDIC_READY)
210*4882a593Smuzhiyun 			break;
211*4882a593Smuzhiyun 	}
212*4882a593Smuzhiyun 	if (!(mdic & E1000_MDIC_READY)) {
213*4882a593Smuzhiyun 		e_dbg("MDI Write did not complete\n");
214*4882a593Smuzhiyun 		return -E1000_ERR_PHY;
215*4882a593Smuzhiyun 	}
216*4882a593Smuzhiyun 	if (mdic & E1000_MDIC_ERROR) {
217*4882a593Smuzhiyun 		e_dbg("MDI Error\n");
218*4882a593Smuzhiyun 		return -E1000_ERR_PHY;
219*4882a593Smuzhiyun 	}
220*4882a593Smuzhiyun 	if (((mdic & E1000_MDIC_REG_MASK) >> E1000_MDIC_REG_SHIFT) != offset) {
221*4882a593Smuzhiyun 		e_dbg("MDI Write offset error - requested %d, returned %d\n",
222*4882a593Smuzhiyun 		      offset,
223*4882a593Smuzhiyun 		      (mdic & E1000_MDIC_REG_MASK) >> E1000_MDIC_REG_SHIFT);
224*4882a593Smuzhiyun 		return -E1000_ERR_PHY;
225*4882a593Smuzhiyun 	}
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	/* Allow some time after each MDIC transaction to avoid
228*4882a593Smuzhiyun 	 * reading duplicate data in the next MDIC transaction.
229*4882a593Smuzhiyun 	 */
230*4882a593Smuzhiyun 	if (hw->mac.type == e1000_pch2lan)
231*4882a593Smuzhiyun 		udelay(100);
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	return 0;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun /**
237*4882a593Smuzhiyun  *  e1000e_read_phy_reg_m88 - Read m88 PHY register
238*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
239*4882a593Smuzhiyun  *  @offset: register offset to be read
240*4882a593Smuzhiyun  *  @data: pointer to the read data
241*4882a593Smuzhiyun  *
242*4882a593Smuzhiyun  *  Acquires semaphore, if necessary, then reads the PHY register at offset
243*4882a593Smuzhiyun  *  and storing the retrieved information in data.  Release any acquired
244*4882a593Smuzhiyun  *  semaphores before exiting.
245*4882a593Smuzhiyun  **/
e1000e_read_phy_reg_m88(struct e1000_hw * hw,u32 offset,u16 * data)246*4882a593Smuzhiyun s32 e1000e_read_phy_reg_m88(struct e1000_hw *hw, u32 offset, u16 *data)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun 	s32 ret_val;
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	ret_val = hw->phy.ops.acquire(hw);
251*4882a593Smuzhiyun 	if (ret_val)
252*4882a593Smuzhiyun 		return ret_val;
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	ret_val = e1000e_read_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & offset,
255*4882a593Smuzhiyun 					   data);
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	hw->phy.ops.release(hw);
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	return ret_val;
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun /**
263*4882a593Smuzhiyun  *  e1000e_write_phy_reg_m88 - Write m88 PHY register
264*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
265*4882a593Smuzhiyun  *  @offset: register offset to write to
266*4882a593Smuzhiyun  *  @data: data to write at register offset
267*4882a593Smuzhiyun  *
268*4882a593Smuzhiyun  *  Acquires semaphore, if necessary, then writes the data to PHY register
269*4882a593Smuzhiyun  *  at the offset.  Release any acquired semaphores before exiting.
270*4882a593Smuzhiyun  **/
e1000e_write_phy_reg_m88(struct e1000_hw * hw,u32 offset,u16 data)271*4882a593Smuzhiyun s32 e1000e_write_phy_reg_m88(struct e1000_hw *hw, u32 offset, u16 data)
272*4882a593Smuzhiyun {
273*4882a593Smuzhiyun 	s32 ret_val;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	ret_val = hw->phy.ops.acquire(hw);
276*4882a593Smuzhiyun 	if (ret_val)
277*4882a593Smuzhiyun 		return ret_val;
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	ret_val = e1000e_write_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & offset,
280*4882a593Smuzhiyun 					    data);
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	hw->phy.ops.release(hw);
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	return ret_val;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun /**
288*4882a593Smuzhiyun  *  e1000_set_page_igp - Set page as on IGP-like PHY(s)
289*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
290*4882a593Smuzhiyun  *  @page: page to set (shifted left when necessary)
291*4882a593Smuzhiyun  *
292*4882a593Smuzhiyun  *  Sets PHY page required for PHY register access.  Assumes semaphore is
293*4882a593Smuzhiyun  *  already acquired.  Note, this function sets phy.addr to 1 so the caller
294*4882a593Smuzhiyun  *  must set it appropriately (if necessary) after this function returns.
295*4882a593Smuzhiyun  **/
e1000_set_page_igp(struct e1000_hw * hw,u16 page)296*4882a593Smuzhiyun s32 e1000_set_page_igp(struct e1000_hw *hw, u16 page)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun 	e_dbg("Setting page 0x%x\n", page);
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	hw->phy.addr = 1;
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	return e1000e_write_phy_reg_mdic(hw, IGP01E1000_PHY_PAGE_SELECT, page);
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun /**
306*4882a593Smuzhiyun  *  __e1000e_read_phy_reg_igp - Read igp PHY register
307*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
308*4882a593Smuzhiyun  *  @offset: register offset to be read
309*4882a593Smuzhiyun  *  @data: pointer to the read data
310*4882a593Smuzhiyun  *  @locked: semaphore has already been acquired or not
311*4882a593Smuzhiyun  *
312*4882a593Smuzhiyun  *  Acquires semaphore, if necessary, then reads the PHY register at offset
313*4882a593Smuzhiyun  *  and stores the retrieved information in data.  Release any acquired
314*4882a593Smuzhiyun  *  semaphores before exiting.
315*4882a593Smuzhiyun  **/
__e1000e_read_phy_reg_igp(struct e1000_hw * hw,u32 offset,u16 * data,bool locked)316*4882a593Smuzhiyun static s32 __e1000e_read_phy_reg_igp(struct e1000_hw *hw, u32 offset, u16 *data,
317*4882a593Smuzhiyun 				     bool locked)
318*4882a593Smuzhiyun {
319*4882a593Smuzhiyun 	s32 ret_val = 0;
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	if (!locked) {
322*4882a593Smuzhiyun 		if (!hw->phy.ops.acquire)
323*4882a593Smuzhiyun 			return 0;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 		ret_val = hw->phy.ops.acquire(hw);
326*4882a593Smuzhiyun 		if (ret_val)
327*4882a593Smuzhiyun 			return ret_val;
328*4882a593Smuzhiyun 	}
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	if (offset > MAX_PHY_MULTI_PAGE_REG)
331*4882a593Smuzhiyun 		ret_val = e1000e_write_phy_reg_mdic(hw,
332*4882a593Smuzhiyun 						    IGP01E1000_PHY_PAGE_SELECT,
333*4882a593Smuzhiyun 						    (u16)offset);
334*4882a593Smuzhiyun 	if (!ret_val)
335*4882a593Smuzhiyun 		ret_val = e1000e_read_phy_reg_mdic(hw,
336*4882a593Smuzhiyun 						   MAX_PHY_REG_ADDRESS & offset,
337*4882a593Smuzhiyun 						   data);
338*4882a593Smuzhiyun 	if (!locked)
339*4882a593Smuzhiyun 		hw->phy.ops.release(hw);
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	return ret_val;
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun /**
345*4882a593Smuzhiyun  *  e1000e_read_phy_reg_igp - Read igp PHY register
346*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
347*4882a593Smuzhiyun  *  @offset: register offset to be read
348*4882a593Smuzhiyun  *  @data: pointer to the read data
349*4882a593Smuzhiyun  *
350*4882a593Smuzhiyun  *  Acquires semaphore then reads the PHY register at offset and stores the
351*4882a593Smuzhiyun  *  retrieved information in data.
352*4882a593Smuzhiyun  *  Release the acquired semaphore before exiting.
353*4882a593Smuzhiyun  **/
e1000e_read_phy_reg_igp(struct e1000_hw * hw,u32 offset,u16 * data)354*4882a593Smuzhiyun s32 e1000e_read_phy_reg_igp(struct e1000_hw *hw, u32 offset, u16 *data)
355*4882a593Smuzhiyun {
356*4882a593Smuzhiyun 	return __e1000e_read_phy_reg_igp(hw, offset, data, false);
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun /**
360*4882a593Smuzhiyun  *  e1000e_read_phy_reg_igp_locked - Read igp PHY register
361*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
362*4882a593Smuzhiyun  *  @offset: register offset to be read
363*4882a593Smuzhiyun  *  @data: pointer to the read data
364*4882a593Smuzhiyun  *
365*4882a593Smuzhiyun  *  Reads the PHY register at offset and stores the retrieved information
366*4882a593Smuzhiyun  *  in data.  Assumes semaphore already acquired.
367*4882a593Smuzhiyun  **/
e1000e_read_phy_reg_igp_locked(struct e1000_hw * hw,u32 offset,u16 * data)368*4882a593Smuzhiyun s32 e1000e_read_phy_reg_igp_locked(struct e1000_hw *hw, u32 offset, u16 *data)
369*4882a593Smuzhiyun {
370*4882a593Smuzhiyun 	return __e1000e_read_phy_reg_igp(hw, offset, data, true);
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun /**
374*4882a593Smuzhiyun  *  e1000e_write_phy_reg_igp - Write igp PHY register
375*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
376*4882a593Smuzhiyun  *  @offset: register offset to write to
377*4882a593Smuzhiyun  *  @data: data to write at register offset
378*4882a593Smuzhiyun  *  @locked: semaphore has already been acquired or not
379*4882a593Smuzhiyun  *
380*4882a593Smuzhiyun  *  Acquires semaphore, if necessary, then writes the data to PHY register
381*4882a593Smuzhiyun  *  at the offset.  Release any acquired semaphores before exiting.
382*4882a593Smuzhiyun  **/
__e1000e_write_phy_reg_igp(struct e1000_hw * hw,u32 offset,u16 data,bool locked)383*4882a593Smuzhiyun static s32 __e1000e_write_phy_reg_igp(struct e1000_hw *hw, u32 offset, u16 data,
384*4882a593Smuzhiyun 				      bool locked)
385*4882a593Smuzhiyun {
386*4882a593Smuzhiyun 	s32 ret_val = 0;
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	if (!locked) {
389*4882a593Smuzhiyun 		if (!hw->phy.ops.acquire)
390*4882a593Smuzhiyun 			return 0;
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 		ret_val = hw->phy.ops.acquire(hw);
393*4882a593Smuzhiyun 		if (ret_val)
394*4882a593Smuzhiyun 			return ret_val;
395*4882a593Smuzhiyun 	}
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	if (offset > MAX_PHY_MULTI_PAGE_REG)
398*4882a593Smuzhiyun 		ret_val = e1000e_write_phy_reg_mdic(hw,
399*4882a593Smuzhiyun 						    IGP01E1000_PHY_PAGE_SELECT,
400*4882a593Smuzhiyun 						    (u16)offset);
401*4882a593Smuzhiyun 	if (!ret_val)
402*4882a593Smuzhiyun 		ret_val = e1000e_write_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS &
403*4882a593Smuzhiyun 						    offset, data);
404*4882a593Smuzhiyun 	if (!locked)
405*4882a593Smuzhiyun 		hw->phy.ops.release(hw);
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	return ret_val;
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun /**
411*4882a593Smuzhiyun  *  e1000e_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 then writes the data to PHY register
417*4882a593Smuzhiyun  *  at the offset.  Release any acquired semaphores before exiting.
418*4882a593Smuzhiyun  **/
e1000e_write_phy_reg_igp(struct e1000_hw * hw,u32 offset,u16 data)419*4882a593Smuzhiyun s32 e1000e_write_phy_reg_igp(struct e1000_hw *hw, u32 offset, u16 data)
420*4882a593Smuzhiyun {
421*4882a593Smuzhiyun 	return __e1000e_write_phy_reg_igp(hw, offset, data, false);
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun /**
425*4882a593Smuzhiyun  *  e1000e_write_phy_reg_igp_locked - Write igp PHY register
426*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
427*4882a593Smuzhiyun  *  @offset: register offset to write to
428*4882a593Smuzhiyun  *  @data: data to write at register offset
429*4882a593Smuzhiyun  *
430*4882a593Smuzhiyun  *  Writes the data to PHY register at the offset.
431*4882a593Smuzhiyun  *  Assumes semaphore already acquired.
432*4882a593Smuzhiyun  **/
e1000e_write_phy_reg_igp_locked(struct e1000_hw * hw,u32 offset,u16 data)433*4882a593Smuzhiyun s32 e1000e_write_phy_reg_igp_locked(struct e1000_hw *hw, u32 offset, u16 data)
434*4882a593Smuzhiyun {
435*4882a593Smuzhiyun 	return __e1000e_write_phy_reg_igp(hw, offset, data, true);
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun /**
439*4882a593Smuzhiyun  *  __e1000_read_kmrn_reg - Read kumeran register
440*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
441*4882a593Smuzhiyun  *  @offset: register offset to be read
442*4882a593Smuzhiyun  *  @data: pointer to the read data
443*4882a593Smuzhiyun  *  @locked: semaphore has already been acquired or not
444*4882a593Smuzhiyun  *
445*4882a593Smuzhiyun  *  Acquires semaphore, if necessary.  Then reads the PHY register at offset
446*4882a593Smuzhiyun  *  using the kumeran interface.  The information retrieved is stored in data.
447*4882a593Smuzhiyun  *  Release any acquired semaphores before exiting.
448*4882a593Smuzhiyun  **/
__e1000_read_kmrn_reg(struct e1000_hw * hw,u32 offset,u16 * data,bool locked)449*4882a593Smuzhiyun static s32 __e1000_read_kmrn_reg(struct e1000_hw *hw, u32 offset, u16 *data,
450*4882a593Smuzhiyun 				 bool locked)
451*4882a593Smuzhiyun {
452*4882a593Smuzhiyun 	u32 kmrnctrlsta;
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	if (!locked) {
455*4882a593Smuzhiyun 		s32 ret_val = 0;
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 		if (!hw->phy.ops.acquire)
458*4882a593Smuzhiyun 			return 0;
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 		ret_val = hw->phy.ops.acquire(hw);
461*4882a593Smuzhiyun 		if (ret_val)
462*4882a593Smuzhiyun 			return ret_val;
463*4882a593Smuzhiyun 	}
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	kmrnctrlsta = ((offset << E1000_KMRNCTRLSTA_OFFSET_SHIFT) &
466*4882a593Smuzhiyun 		       E1000_KMRNCTRLSTA_OFFSET) | E1000_KMRNCTRLSTA_REN;
467*4882a593Smuzhiyun 	ew32(KMRNCTRLSTA, kmrnctrlsta);
468*4882a593Smuzhiyun 	e1e_flush();
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun 	udelay(2);
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	kmrnctrlsta = er32(KMRNCTRLSTA);
473*4882a593Smuzhiyun 	*data = (u16)kmrnctrlsta;
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 	if (!locked)
476*4882a593Smuzhiyun 		hw->phy.ops.release(hw);
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 	return 0;
479*4882a593Smuzhiyun }
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun /**
482*4882a593Smuzhiyun  *  e1000e_read_kmrn_reg -  Read kumeran register
483*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
484*4882a593Smuzhiyun  *  @offset: register offset to be read
485*4882a593Smuzhiyun  *  @data: pointer to the read data
486*4882a593Smuzhiyun  *
487*4882a593Smuzhiyun  *  Acquires semaphore then reads the PHY register at offset using the
488*4882a593Smuzhiyun  *  kumeran interface.  The information retrieved is stored in data.
489*4882a593Smuzhiyun  *  Release the acquired semaphore before exiting.
490*4882a593Smuzhiyun  **/
e1000e_read_kmrn_reg(struct e1000_hw * hw,u32 offset,u16 * data)491*4882a593Smuzhiyun s32 e1000e_read_kmrn_reg(struct e1000_hw *hw, u32 offset, u16 *data)
492*4882a593Smuzhiyun {
493*4882a593Smuzhiyun 	return __e1000_read_kmrn_reg(hw, offset, data, false);
494*4882a593Smuzhiyun }
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun /**
497*4882a593Smuzhiyun  *  e1000e_read_kmrn_reg_locked -  Read kumeran register
498*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
499*4882a593Smuzhiyun  *  @offset: register offset to be read
500*4882a593Smuzhiyun  *  @data: pointer to the read data
501*4882a593Smuzhiyun  *
502*4882a593Smuzhiyun  *  Reads the PHY register at offset using the kumeran interface.  The
503*4882a593Smuzhiyun  *  information retrieved is stored in data.
504*4882a593Smuzhiyun  *  Assumes semaphore already acquired.
505*4882a593Smuzhiyun  **/
e1000e_read_kmrn_reg_locked(struct e1000_hw * hw,u32 offset,u16 * data)506*4882a593Smuzhiyun s32 e1000e_read_kmrn_reg_locked(struct e1000_hw *hw, u32 offset, u16 *data)
507*4882a593Smuzhiyun {
508*4882a593Smuzhiyun 	return __e1000_read_kmrn_reg(hw, offset, data, true);
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun /**
512*4882a593Smuzhiyun  *  __e1000_write_kmrn_reg - Write kumeran register
513*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
514*4882a593Smuzhiyun  *  @offset: register offset to write to
515*4882a593Smuzhiyun  *  @data: data to write at register offset
516*4882a593Smuzhiyun  *  @locked: semaphore has already been acquired or not
517*4882a593Smuzhiyun  *
518*4882a593Smuzhiyun  *  Acquires semaphore, if necessary.  Then write the data to PHY register
519*4882a593Smuzhiyun  *  at the offset using the kumeran interface.  Release any acquired semaphores
520*4882a593Smuzhiyun  *  before exiting.
521*4882a593Smuzhiyun  **/
__e1000_write_kmrn_reg(struct e1000_hw * hw,u32 offset,u16 data,bool locked)522*4882a593Smuzhiyun static s32 __e1000_write_kmrn_reg(struct e1000_hw *hw, u32 offset, u16 data,
523*4882a593Smuzhiyun 				  bool locked)
524*4882a593Smuzhiyun {
525*4882a593Smuzhiyun 	u32 kmrnctrlsta;
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	if (!locked) {
528*4882a593Smuzhiyun 		s32 ret_val = 0;
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 		if (!hw->phy.ops.acquire)
531*4882a593Smuzhiyun 			return 0;
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 		ret_val = hw->phy.ops.acquire(hw);
534*4882a593Smuzhiyun 		if (ret_val)
535*4882a593Smuzhiyun 			return ret_val;
536*4882a593Smuzhiyun 	}
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	kmrnctrlsta = ((offset << E1000_KMRNCTRLSTA_OFFSET_SHIFT) &
539*4882a593Smuzhiyun 		       E1000_KMRNCTRLSTA_OFFSET) | data;
540*4882a593Smuzhiyun 	ew32(KMRNCTRLSTA, kmrnctrlsta);
541*4882a593Smuzhiyun 	e1e_flush();
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	udelay(2);
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	if (!locked)
546*4882a593Smuzhiyun 		hw->phy.ops.release(hw);
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 	return 0;
549*4882a593Smuzhiyun }
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun /**
552*4882a593Smuzhiyun  *  e1000e_write_kmrn_reg -  Write kumeran register
553*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
554*4882a593Smuzhiyun  *  @offset: register offset to write to
555*4882a593Smuzhiyun  *  @data: data to write at register offset
556*4882a593Smuzhiyun  *
557*4882a593Smuzhiyun  *  Acquires semaphore then writes the data to the PHY register at the offset
558*4882a593Smuzhiyun  *  using the kumeran interface.  Release the acquired semaphore before exiting.
559*4882a593Smuzhiyun  **/
e1000e_write_kmrn_reg(struct e1000_hw * hw,u32 offset,u16 data)560*4882a593Smuzhiyun s32 e1000e_write_kmrn_reg(struct e1000_hw *hw, u32 offset, u16 data)
561*4882a593Smuzhiyun {
562*4882a593Smuzhiyun 	return __e1000_write_kmrn_reg(hw, offset, data, false);
563*4882a593Smuzhiyun }
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun /**
566*4882a593Smuzhiyun  *  e1000e_write_kmrn_reg_locked -  Write kumeran register
567*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
568*4882a593Smuzhiyun  *  @offset: register offset to write to
569*4882a593Smuzhiyun  *  @data: data to write at register offset
570*4882a593Smuzhiyun  *
571*4882a593Smuzhiyun  *  Write the data to PHY register at the offset using the kumeran interface.
572*4882a593Smuzhiyun  *  Assumes semaphore already acquired.
573*4882a593Smuzhiyun  **/
e1000e_write_kmrn_reg_locked(struct e1000_hw * hw,u32 offset,u16 data)574*4882a593Smuzhiyun s32 e1000e_write_kmrn_reg_locked(struct e1000_hw *hw, u32 offset, u16 data)
575*4882a593Smuzhiyun {
576*4882a593Smuzhiyun 	return __e1000_write_kmrn_reg(hw, offset, data, true);
577*4882a593Smuzhiyun }
578*4882a593Smuzhiyun 
579*4882a593Smuzhiyun /**
580*4882a593Smuzhiyun  *  e1000_set_master_slave_mode - Setup PHY for Master/slave mode
581*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
582*4882a593Smuzhiyun  *
583*4882a593Smuzhiyun  *  Sets up Master/slave mode
584*4882a593Smuzhiyun  **/
e1000_set_master_slave_mode(struct e1000_hw * hw)585*4882a593Smuzhiyun static s32 e1000_set_master_slave_mode(struct e1000_hw *hw)
586*4882a593Smuzhiyun {
587*4882a593Smuzhiyun 	s32 ret_val;
588*4882a593Smuzhiyun 	u16 phy_data;
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 	/* Resolve Master/Slave mode */
591*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, MII_CTRL1000, &phy_data);
592*4882a593Smuzhiyun 	if (ret_val)
593*4882a593Smuzhiyun 		return ret_val;
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	/* load defaults for future use */
596*4882a593Smuzhiyun 	hw->phy.original_ms_type = (phy_data & CTL1000_ENABLE_MASTER) ?
597*4882a593Smuzhiyun 	    ((phy_data & CTL1000_AS_MASTER) ?
598*4882a593Smuzhiyun 	     e1000_ms_force_master : e1000_ms_force_slave) : e1000_ms_auto;
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	switch (hw->phy.ms_type) {
601*4882a593Smuzhiyun 	case e1000_ms_force_master:
602*4882a593Smuzhiyun 		phy_data |= (CTL1000_ENABLE_MASTER | CTL1000_AS_MASTER);
603*4882a593Smuzhiyun 		break;
604*4882a593Smuzhiyun 	case e1000_ms_force_slave:
605*4882a593Smuzhiyun 		phy_data |= CTL1000_ENABLE_MASTER;
606*4882a593Smuzhiyun 		phy_data &= ~(CTL1000_AS_MASTER);
607*4882a593Smuzhiyun 		break;
608*4882a593Smuzhiyun 	case e1000_ms_auto:
609*4882a593Smuzhiyun 		phy_data &= ~CTL1000_ENABLE_MASTER;
610*4882a593Smuzhiyun 		fallthrough;
611*4882a593Smuzhiyun 	default:
612*4882a593Smuzhiyun 		break;
613*4882a593Smuzhiyun 	}
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	return e1e_wphy(hw, MII_CTRL1000, phy_data);
616*4882a593Smuzhiyun }
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun /**
619*4882a593Smuzhiyun  *  e1000_copper_link_setup_82577 - Setup 82577 PHY for copper link
620*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
621*4882a593Smuzhiyun  *
622*4882a593Smuzhiyun  *  Sets up Carrier-sense on Transmit and downshift values.
623*4882a593Smuzhiyun  **/
e1000_copper_link_setup_82577(struct e1000_hw * hw)624*4882a593Smuzhiyun s32 e1000_copper_link_setup_82577(struct e1000_hw *hw)
625*4882a593Smuzhiyun {
626*4882a593Smuzhiyun 	s32 ret_val;
627*4882a593Smuzhiyun 	u16 phy_data;
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	/* Enable CRS on Tx. This must be set for half-duplex operation. */
630*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, I82577_CFG_REG, &phy_data);
631*4882a593Smuzhiyun 	if (ret_val)
632*4882a593Smuzhiyun 		return ret_val;
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun 	phy_data |= I82577_CFG_ASSERT_CRS_ON_TX;
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 	/* Enable downshift */
637*4882a593Smuzhiyun 	phy_data |= I82577_CFG_ENABLE_DOWNSHIFT;
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	ret_val = e1e_wphy(hw, I82577_CFG_REG, phy_data);
640*4882a593Smuzhiyun 	if (ret_val)
641*4882a593Smuzhiyun 		return ret_val;
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun 	/* Set MDI/MDIX mode */
644*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, I82577_PHY_CTRL_2, &phy_data);
645*4882a593Smuzhiyun 	if (ret_val)
646*4882a593Smuzhiyun 		return ret_val;
647*4882a593Smuzhiyun 	phy_data &= ~I82577_PHY_CTRL2_MDIX_CFG_MASK;
648*4882a593Smuzhiyun 	/* Options:
649*4882a593Smuzhiyun 	 *   0 - Auto (default)
650*4882a593Smuzhiyun 	 *   1 - MDI mode
651*4882a593Smuzhiyun 	 *   2 - MDI-X mode
652*4882a593Smuzhiyun 	 */
653*4882a593Smuzhiyun 	switch (hw->phy.mdix) {
654*4882a593Smuzhiyun 	case 1:
655*4882a593Smuzhiyun 		break;
656*4882a593Smuzhiyun 	case 2:
657*4882a593Smuzhiyun 		phy_data |= I82577_PHY_CTRL2_MANUAL_MDIX;
658*4882a593Smuzhiyun 		break;
659*4882a593Smuzhiyun 	case 0:
660*4882a593Smuzhiyun 	default:
661*4882a593Smuzhiyun 		phy_data |= I82577_PHY_CTRL2_AUTO_MDI_MDIX;
662*4882a593Smuzhiyun 		break;
663*4882a593Smuzhiyun 	}
664*4882a593Smuzhiyun 	ret_val = e1e_wphy(hw, I82577_PHY_CTRL_2, phy_data);
665*4882a593Smuzhiyun 	if (ret_val)
666*4882a593Smuzhiyun 		return ret_val;
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	return e1000_set_master_slave_mode(hw);
669*4882a593Smuzhiyun }
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun /**
672*4882a593Smuzhiyun  *  e1000e_copper_link_setup_m88 - Setup m88 PHY's for copper link
673*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
674*4882a593Smuzhiyun  *
675*4882a593Smuzhiyun  *  Sets up MDI/MDI-X and polarity for m88 PHY's.  If necessary, transmit clock
676*4882a593Smuzhiyun  *  and downshift values are set also.
677*4882a593Smuzhiyun  **/
e1000e_copper_link_setup_m88(struct e1000_hw * hw)678*4882a593Smuzhiyun s32 e1000e_copper_link_setup_m88(struct e1000_hw *hw)
679*4882a593Smuzhiyun {
680*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
681*4882a593Smuzhiyun 	s32 ret_val;
682*4882a593Smuzhiyun 	u16 phy_data;
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun 	/* Enable CRS on Tx. This must be set for half-duplex operation. */
685*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
686*4882a593Smuzhiyun 	if (ret_val)
687*4882a593Smuzhiyun 		return ret_val;
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 	/* For BM PHY this bit is downshift enable */
690*4882a593Smuzhiyun 	if (phy->type != e1000_phy_bm)
691*4882a593Smuzhiyun 		phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 	/* Options:
694*4882a593Smuzhiyun 	 *   MDI/MDI-X = 0 (default)
695*4882a593Smuzhiyun 	 *   0 - Auto for all speeds
696*4882a593Smuzhiyun 	 *   1 - MDI mode
697*4882a593Smuzhiyun 	 *   2 - MDI-X mode
698*4882a593Smuzhiyun 	 *   3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
699*4882a593Smuzhiyun 	 */
700*4882a593Smuzhiyun 	phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	switch (phy->mdix) {
703*4882a593Smuzhiyun 	case 1:
704*4882a593Smuzhiyun 		phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
705*4882a593Smuzhiyun 		break;
706*4882a593Smuzhiyun 	case 2:
707*4882a593Smuzhiyun 		phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
708*4882a593Smuzhiyun 		break;
709*4882a593Smuzhiyun 	case 3:
710*4882a593Smuzhiyun 		phy_data |= M88E1000_PSCR_AUTO_X_1000T;
711*4882a593Smuzhiyun 		break;
712*4882a593Smuzhiyun 	case 0:
713*4882a593Smuzhiyun 	default:
714*4882a593Smuzhiyun 		phy_data |= M88E1000_PSCR_AUTO_X_MODE;
715*4882a593Smuzhiyun 		break;
716*4882a593Smuzhiyun 	}
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun 	/* Options:
719*4882a593Smuzhiyun 	 *   disable_polarity_correction = 0 (default)
720*4882a593Smuzhiyun 	 *       Automatic Correction for Reversed Cable Polarity
721*4882a593Smuzhiyun 	 *   0 - Disabled
722*4882a593Smuzhiyun 	 *   1 - Enabled
723*4882a593Smuzhiyun 	 */
724*4882a593Smuzhiyun 	phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
725*4882a593Smuzhiyun 	if (phy->disable_polarity_correction)
726*4882a593Smuzhiyun 		phy_data |= M88E1000_PSCR_POLARITY_REVERSAL;
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 	/* Enable downshift on BM (disabled by default) */
729*4882a593Smuzhiyun 	if (phy->type == e1000_phy_bm) {
730*4882a593Smuzhiyun 		/* For 82574/82583, first disable then enable downshift */
731*4882a593Smuzhiyun 		if (phy->id == BME1000_E_PHY_ID_R2) {
732*4882a593Smuzhiyun 			phy_data &= ~BME1000_PSCR_ENABLE_DOWNSHIFT;
733*4882a593Smuzhiyun 			ret_val = e1e_wphy(hw, M88E1000_PHY_SPEC_CTRL,
734*4882a593Smuzhiyun 					   phy_data);
735*4882a593Smuzhiyun 			if (ret_val)
736*4882a593Smuzhiyun 				return ret_val;
737*4882a593Smuzhiyun 			/* Commit the changes. */
738*4882a593Smuzhiyun 			ret_val = phy->ops.commit(hw);
739*4882a593Smuzhiyun 			if (ret_val) {
740*4882a593Smuzhiyun 				e_dbg("Error committing the PHY changes\n");
741*4882a593Smuzhiyun 				return ret_val;
742*4882a593Smuzhiyun 			}
743*4882a593Smuzhiyun 		}
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 		phy_data |= BME1000_PSCR_ENABLE_DOWNSHIFT;
746*4882a593Smuzhiyun 	}
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 	ret_val = e1e_wphy(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
749*4882a593Smuzhiyun 	if (ret_val)
750*4882a593Smuzhiyun 		return ret_val;
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun 	if ((phy->type == e1000_phy_m88) &&
753*4882a593Smuzhiyun 	    (phy->revision < E1000_REVISION_4) &&
754*4882a593Smuzhiyun 	    (phy->id != BME1000_E_PHY_ID_R2)) {
755*4882a593Smuzhiyun 		/* Force TX_CLK in the Extended PHY Specific Control Register
756*4882a593Smuzhiyun 		 * to 25MHz clock.
757*4882a593Smuzhiyun 		 */
758*4882a593Smuzhiyun 		ret_val = e1e_rphy(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
759*4882a593Smuzhiyun 		if (ret_val)
760*4882a593Smuzhiyun 			return ret_val;
761*4882a593Smuzhiyun 
762*4882a593Smuzhiyun 		phy_data |= M88E1000_EPSCR_TX_CLK_25;
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 		if ((phy->revision == 2) && (phy->id == M88E1111_I_PHY_ID)) {
765*4882a593Smuzhiyun 			/* 82573L PHY - set the downshift counter to 5x. */
766*4882a593Smuzhiyun 			phy_data &= ~M88EC018_EPSCR_DOWNSHIFT_COUNTER_MASK;
767*4882a593Smuzhiyun 			phy_data |= M88EC018_EPSCR_DOWNSHIFT_COUNTER_5X;
768*4882a593Smuzhiyun 		} else {
769*4882a593Smuzhiyun 			/* Configure Master and Slave downshift values */
770*4882a593Smuzhiyun 			phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK |
771*4882a593Smuzhiyun 				      M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
772*4882a593Smuzhiyun 			phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X |
773*4882a593Smuzhiyun 				     M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
774*4882a593Smuzhiyun 		}
775*4882a593Smuzhiyun 		ret_val = e1e_wphy(hw, M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
776*4882a593Smuzhiyun 		if (ret_val)
777*4882a593Smuzhiyun 			return ret_val;
778*4882a593Smuzhiyun 	}
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun 	if ((phy->type == e1000_phy_bm) && (phy->id == BME1000_E_PHY_ID_R2)) {
781*4882a593Smuzhiyun 		/* Set PHY page 0, register 29 to 0x0003 */
782*4882a593Smuzhiyun 		ret_val = e1e_wphy(hw, 29, 0x0003);
783*4882a593Smuzhiyun 		if (ret_val)
784*4882a593Smuzhiyun 			return ret_val;
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun 		/* Set PHY page 0, register 30 to 0x0000 */
787*4882a593Smuzhiyun 		ret_val = e1e_wphy(hw, 30, 0x0000);
788*4882a593Smuzhiyun 		if (ret_val)
789*4882a593Smuzhiyun 			return ret_val;
790*4882a593Smuzhiyun 	}
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	/* Commit the changes. */
793*4882a593Smuzhiyun 	if (phy->ops.commit) {
794*4882a593Smuzhiyun 		ret_val = phy->ops.commit(hw);
795*4882a593Smuzhiyun 		if (ret_val) {
796*4882a593Smuzhiyun 			e_dbg("Error committing the PHY changes\n");
797*4882a593Smuzhiyun 			return ret_val;
798*4882a593Smuzhiyun 		}
799*4882a593Smuzhiyun 	}
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun 	if (phy->type == e1000_phy_82578) {
802*4882a593Smuzhiyun 		ret_val = e1e_rphy(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
803*4882a593Smuzhiyun 		if (ret_val)
804*4882a593Smuzhiyun 			return ret_val;
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun 		/* 82578 PHY - set the downshift count to 1x. */
807*4882a593Smuzhiyun 		phy_data |= I82578_EPSCR_DOWNSHIFT_ENABLE;
808*4882a593Smuzhiyun 		phy_data &= ~I82578_EPSCR_DOWNSHIFT_COUNTER_MASK;
809*4882a593Smuzhiyun 		ret_val = e1e_wphy(hw, M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
810*4882a593Smuzhiyun 		if (ret_val)
811*4882a593Smuzhiyun 			return ret_val;
812*4882a593Smuzhiyun 	}
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 	return 0;
815*4882a593Smuzhiyun }
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun /**
818*4882a593Smuzhiyun  *  e1000e_copper_link_setup_igp - Setup igp PHY's for copper link
819*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
820*4882a593Smuzhiyun  *
821*4882a593Smuzhiyun  *  Sets up LPLU, MDI/MDI-X, polarity, Smartspeed and Master/Slave config for
822*4882a593Smuzhiyun  *  igp PHY's.
823*4882a593Smuzhiyun  **/
e1000e_copper_link_setup_igp(struct e1000_hw * hw)824*4882a593Smuzhiyun s32 e1000e_copper_link_setup_igp(struct e1000_hw *hw)
825*4882a593Smuzhiyun {
826*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
827*4882a593Smuzhiyun 	s32 ret_val;
828*4882a593Smuzhiyun 	u16 data;
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 	ret_val = e1000_phy_hw_reset(hw);
831*4882a593Smuzhiyun 	if (ret_val) {
832*4882a593Smuzhiyun 		e_dbg("Error resetting the PHY.\n");
833*4882a593Smuzhiyun 		return ret_val;
834*4882a593Smuzhiyun 	}
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun 	/* Wait 100ms for MAC to configure PHY from NVM settings, to avoid
837*4882a593Smuzhiyun 	 * timeout issues when LFS is enabled.
838*4882a593Smuzhiyun 	 */
839*4882a593Smuzhiyun 	msleep(100);
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	/* disable lplu d0 during driver init */
842*4882a593Smuzhiyun 	if (hw->phy.ops.set_d0_lplu_state) {
843*4882a593Smuzhiyun 		ret_val = hw->phy.ops.set_d0_lplu_state(hw, false);
844*4882a593Smuzhiyun 		if (ret_val) {
845*4882a593Smuzhiyun 			e_dbg("Error Disabling LPLU D0\n");
846*4882a593Smuzhiyun 			return ret_val;
847*4882a593Smuzhiyun 		}
848*4882a593Smuzhiyun 	}
849*4882a593Smuzhiyun 	/* Configure mdi-mdix settings */
850*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, IGP01E1000_PHY_PORT_CTRL, &data);
851*4882a593Smuzhiyun 	if (ret_val)
852*4882a593Smuzhiyun 		return ret_val;
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun 	data &= ~IGP01E1000_PSCR_AUTO_MDIX;
855*4882a593Smuzhiyun 
856*4882a593Smuzhiyun 	switch (phy->mdix) {
857*4882a593Smuzhiyun 	case 1:
858*4882a593Smuzhiyun 		data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
859*4882a593Smuzhiyun 		break;
860*4882a593Smuzhiyun 	case 2:
861*4882a593Smuzhiyun 		data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
862*4882a593Smuzhiyun 		break;
863*4882a593Smuzhiyun 	case 0:
864*4882a593Smuzhiyun 	default:
865*4882a593Smuzhiyun 		data |= IGP01E1000_PSCR_AUTO_MDIX;
866*4882a593Smuzhiyun 		break;
867*4882a593Smuzhiyun 	}
868*4882a593Smuzhiyun 	ret_val = e1e_wphy(hw, IGP01E1000_PHY_PORT_CTRL, data);
869*4882a593Smuzhiyun 	if (ret_val)
870*4882a593Smuzhiyun 		return ret_val;
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun 	/* set auto-master slave resolution settings */
873*4882a593Smuzhiyun 	if (hw->mac.autoneg) {
874*4882a593Smuzhiyun 		/* when autonegotiation advertisement is only 1000Mbps then we
875*4882a593Smuzhiyun 		 * should disable SmartSpeed and enable Auto MasterSlave
876*4882a593Smuzhiyun 		 * resolution as hardware default.
877*4882a593Smuzhiyun 		 */
878*4882a593Smuzhiyun 		if (phy->autoneg_advertised == ADVERTISE_1000_FULL) {
879*4882a593Smuzhiyun 			/* Disable SmartSpeed */
880*4882a593Smuzhiyun 			ret_val = e1e_rphy(hw, IGP01E1000_PHY_PORT_CONFIG,
881*4882a593Smuzhiyun 					   &data);
882*4882a593Smuzhiyun 			if (ret_val)
883*4882a593Smuzhiyun 				return ret_val;
884*4882a593Smuzhiyun 
885*4882a593Smuzhiyun 			data &= ~IGP01E1000_PSCFR_SMART_SPEED;
886*4882a593Smuzhiyun 			ret_val = e1e_wphy(hw, IGP01E1000_PHY_PORT_CONFIG,
887*4882a593Smuzhiyun 					   data);
888*4882a593Smuzhiyun 			if (ret_val)
889*4882a593Smuzhiyun 				return ret_val;
890*4882a593Smuzhiyun 
891*4882a593Smuzhiyun 			/* Set auto Master/Slave resolution process */
892*4882a593Smuzhiyun 			ret_val = e1e_rphy(hw, MII_CTRL1000, &data);
893*4882a593Smuzhiyun 			if (ret_val)
894*4882a593Smuzhiyun 				return ret_val;
895*4882a593Smuzhiyun 
896*4882a593Smuzhiyun 			data &= ~CTL1000_ENABLE_MASTER;
897*4882a593Smuzhiyun 			ret_val = e1e_wphy(hw, MII_CTRL1000, data);
898*4882a593Smuzhiyun 			if (ret_val)
899*4882a593Smuzhiyun 				return ret_val;
900*4882a593Smuzhiyun 		}
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 		ret_val = e1000_set_master_slave_mode(hw);
903*4882a593Smuzhiyun 	}
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun 	return ret_val;
906*4882a593Smuzhiyun }
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun /**
909*4882a593Smuzhiyun  *  e1000_phy_setup_autoneg - Configure PHY for auto-negotiation
910*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
911*4882a593Smuzhiyun  *
912*4882a593Smuzhiyun  *  Reads the MII auto-neg advertisement register and/or the 1000T control
913*4882a593Smuzhiyun  *  register and if the PHY is already setup for auto-negotiation, then
914*4882a593Smuzhiyun  *  return successful.  Otherwise, setup advertisement and flow control to
915*4882a593Smuzhiyun  *  the appropriate values for the wanted auto-negotiation.
916*4882a593Smuzhiyun  **/
e1000_phy_setup_autoneg(struct e1000_hw * hw)917*4882a593Smuzhiyun static s32 e1000_phy_setup_autoneg(struct e1000_hw *hw)
918*4882a593Smuzhiyun {
919*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
920*4882a593Smuzhiyun 	s32 ret_val;
921*4882a593Smuzhiyun 	u16 mii_autoneg_adv_reg;
922*4882a593Smuzhiyun 	u16 mii_1000t_ctrl_reg = 0;
923*4882a593Smuzhiyun 
924*4882a593Smuzhiyun 	phy->autoneg_advertised &= phy->autoneg_mask;
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 	/* Read the MII Auto-Neg Advertisement Register (Address 4). */
927*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, MII_ADVERTISE, &mii_autoneg_adv_reg);
928*4882a593Smuzhiyun 	if (ret_val)
929*4882a593Smuzhiyun 		return ret_val;
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun 	if (phy->autoneg_mask & ADVERTISE_1000_FULL) {
932*4882a593Smuzhiyun 		/* Read the MII 1000Base-T Control Register (Address 9). */
933*4882a593Smuzhiyun 		ret_val = e1e_rphy(hw, MII_CTRL1000, &mii_1000t_ctrl_reg);
934*4882a593Smuzhiyun 		if (ret_val)
935*4882a593Smuzhiyun 			return ret_val;
936*4882a593Smuzhiyun 	}
937*4882a593Smuzhiyun 
938*4882a593Smuzhiyun 	/* Need to parse both autoneg_advertised and fc and set up
939*4882a593Smuzhiyun 	 * the appropriate PHY registers.  First we will parse for
940*4882a593Smuzhiyun 	 * autoneg_advertised software override.  Since we can advertise
941*4882a593Smuzhiyun 	 * a plethora of combinations, we need to check each bit
942*4882a593Smuzhiyun 	 * individually.
943*4882a593Smuzhiyun 	 */
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun 	/* First we clear all the 10/100 mb speed bits in the Auto-Neg
946*4882a593Smuzhiyun 	 * Advertisement Register (Address 4) and the 1000 mb speed bits in
947*4882a593Smuzhiyun 	 * the  1000Base-T Control Register (Address 9).
948*4882a593Smuzhiyun 	 */
949*4882a593Smuzhiyun 	mii_autoneg_adv_reg &= ~(ADVERTISE_100FULL |
950*4882a593Smuzhiyun 				 ADVERTISE_100HALF |
951*4882a593Smuzhiyun 				 ADVERTISE_10FULL | ADVERTISE_10HALF);
952*4882a593Smuzhiyun 	mii_1000t_ctrl_reg &= ~(ADVERTISE_1000HALF | ADVERTISE_1000FULL);
953*4882a593Smuzhiyun 
954*4882a593Smuzhiyun 	e_dbg("autoneg_advertised %x\n", phy->autoneg_advertised);
955*4882a593Smuzhiyun 
956*4882a593Smuzhiyun 	/* Do we want to advertise 10 Mb Half Duplex? */
957*4882a593Smuzhiyun 	if (phy->autoneg_advertised & ADVERTISE_10_HALF) {
958*4882a593Smuzhiyun 		e_dbg("Advertise 10mb Half duplex\n");
959*4882a593Smuzhiyun 		mii_autoneg_adv_reg |= ADVERTISE_10HALF;
960*4882a593Smuzhiyun 	}
961*4882a593Smuzhiyun 
962*4882a593Smuzhiyun 	/* Do we want to advertise 10 Mb Full Duplex? */
963*4882a593Smuzhiyun 	if (phy->autoneg_advertised & ADVERTISE_10_FULL) {
964*4882a593Smuzhiyun 		e_dbg("Advertise 10mb Full duplex\n");
965*4882a593Smuzhiyun 		mii_autoneg_adv_reg |= ADVERTISE_10FULL;
966*4882a593Smuzhiyun 	}
967*4882a593Smuzhiyun 
968*4882a593Smuzhiyun 	/* Do we want to advertise 100 Mb Half Duplex? */
969*4882a593Smuzhiyun 	if (phy->autoneg_advertised & ADVERTISE_100_HALF) {
970*4882a593Smuzhiyun 		e_dbg("Advertise 100mb Half duplex\n");
971*4882a593Smuzhiyun 		mii_autoneg_adv_reg |= ADVERTISE_100HALF;
972*4882a593Smuzhiyun 	}
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun 	/* Do we want to advertise 100 Mb Full Duplex? */
975*4882a593Smuzhiyun 	if (phy->autoneg_advertised & ADVERTISE_100_FULL) {
976*4882a593Smuzhiyun 		e_dbg("Advertise 100mb Full duplex\n");
977*4882a593Smuzhiyun 		mii_autoneg_adv_reg |= ADVERTISE_100FULL;
978*4882a593Smuzhiyun 	}
979*4882a593Smuzhiyun 
980*4882a593Smuzhiyun 	/* We do not allow the Phy to advertise 1000 Mb Half Duplex */
981*4882a593Smuzhiyun 	if (phy->autoneg_advertised & ADVERTISE_1000_HALF)
982*4882a593Smuzhiyun 		e_dbg("Advertise 1000mb Half duplex request denied!\n");
983*4882a593Smuzhiyun 
984*4882a593Smuzhiyun 	/* Do we want to advertise 1000 Mb Full Duplex? */
985*4882a593Smuzhiyun 	if (phy->autoneg_advertised & ADVERTISE_1000_FULL) {
986*4882a593Smuzhiyun 		e_dbg("Advertise 1000mb Full duplex\n");
987*4882a593Smuzhiyun 		mii_1000t_ctrl_reg |= ADVERTISE_1000FULL;
988*4882a593Smuzhiyun 	}
989*4882a593Smuzhiyun 
990*4882a593Smuzhiyun 	/* Check for a software override of the flow control settings, and
991*4882a593Smuzhiyun 	 * setup the PHY advertisement registers accordingly.  If
992*4882a593Smuzhiyun 	 * auto-negotiation is enabled, then software will have to set the
993*4882a593Smuzhiyun 	 * "PAUSE" bits to the correct value in the Auto-Negotiation
994*4882a593Smuzhiyun 	 * Advertisement Register (MII_ADVERTISE) and re-start auto-
995*4882a593Smuzhiyun 	 * negotiation.
996*4882a593Smuzhiyun 	 *
997*4882a593Smuzhiyun 	 * The possible values of the "fc" parameter are:
998*4882a593Smuzhiyun 	 *      0:  Flow control is completely disabled
999*4882a593Smuzhiyun 	 *      1:  Rx flow control is enabled (we can receive pause frames
1000*4882a593Smuzhiyun 	 *          but not send pause frames).
1001*4882a593Smuzhiyun 	 *      2:  Tx flow control is enabled (we can send pause frames
1002*4882a593Smuzhiyun 	 *          but we do not support receiving pause frames).
1003*4882a593Smuzhiyun 	 *      3:  Both Rx and Tx flow control (symmetric) are enabled.
1004*4882a593Smuzhiyun 	 *  other:  No software override.  The flow control configuration
1005*4882a593Smuzhiyun 	 *          in the EEPROM is used.
1006*4882a593Smuzhiyun 	 */
1007*4882a593Smuzhiyun 	switch (hw->fc.current_mode) {
1008*4882a593Smuzhiyun 	case e1000_fc_none:
1009*4882a593Smuzhiyun 		/* Flow control (Rx & Tx) is completely disabled by a
1010*4882a593Smuzhiyun 		 * software over-ride.
1011*4882a593Smuzhiyun 		 */
1012*4882a593Smuzhiyun 		mii_autoneg_adv_reg &=
1013*4882a593Smuzhiyun 		    ~(ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP);
1014*4882a593Smuzhiyun 		break;
1015*4882a593Smuzhiyun 	case e1000_fc_rx_pause:
1016*4882a593Smuzhiyun 		/* Rx Flow control is enabled, and Tx Flow control is
1017*4882a593Smuzhiyun 		 * disabled, by a software over-ride.
1018*4882a593Smuzhiyun 		 *
1019*4882a593Smuzhiyun 		 * Since there really isn't a way to advertise that we are
1020*4882a593Smuzhiyun 		 * capable of Rx Pause ONLY, we will advertise that we
1021*4882a593Smuzhiyun 		 * support both symmetric and asymmetric Rx PAUSE.  Later
1022*4882a593Smuzhiyun 		 * (in e1000e_config_fc_after_link_up) we will disable the
1023*4882a593Smuzhiyun 		 * hw's ability to send PAUSE frames.
1024*4882a593Smuzhiyun 		 */
1025*4882a593Smuzhiyun 		mii_autoneg_adv_reg |=
1026*4882a593Smuzhiyun 		    (ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP);
1027*4882a593Smuzhiyun 		break;
1028*4882a593Smuzhiyun 	case e1000_fc_tx_pause:
1029*4882a593Smuzhiyun 		/* Tx Flow control is enabled, and Rx Flow control is
1030*4882a593Smuzhiyun 		 * disabled, by a software over-ride.
1031*4882a593Smuzhiyun 		 */
1032*4882a593Smuzhiyun 		mii_autoneg_adv_reg |= ADVERTISE_PAUSE_ASYM;
1033*4882a593Smuzhiyun 		mii_autoneg_adv_reg &= ~ADVERTISE_PAUSE_CAP;
1034*4882a593Smuzhiyun 		break;
1035*4882a593Smuzhiyun 	case e1000_fc_full:
1036*4882a593Smuzhiyun 		/* Flow control (both Rx and Tx) is enabled by a software
1037*4882a593Smuzhiyun 		 * over-ride.
1038*4882a593Smuzhiyun 		 */
1039*4882a593Smuzhiyun 		mii_autoneg_adv_reg |=
1040*4882a593Smuzhiyun 		    (ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP);
1041*4882a593Smuzhiyun 		break;
1042*4882a593Smuzhiyun 	default:
1043*4882a593Smuzhiyun 		e_dbg("Flow control param set incorrectly\n");
1044*4882a593Smuzhiyun 		return -E1000_ERR_CONFIG;
1045*4882a593Smuzhiyun 	}
1046*4882a593Smuzhiyun 
1047*4882a593Smuzhiyun 	ret_val = e1e_wphy(hw, MII_ADVERTISE, mii_autoneg_adv_reg);
1048*4882a593Smuzhiyun 	if (ret_val)
1049*4882a593Smuzhiyun 		return ret_val;
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun 	e_dbg("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
1052*4882a593Smuzhiyun 
1053*4882a593Smuzhiyun 	if (phy->autoneg_mask & ADVERTISE_1000_FULL)
1054*4882a593Smuzhiyun 		ret_val = e1e_wphy(hw, MII_CTRL1000, mii_1000t_ctrl_reg);
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun 	return ret_val;
1057*4882a593Smuzhiyun }
1058*4882a593Smuzhiyun 
1059*4882a593Smuzhiyun /**
1060*4882a593Smuzhiyun  *  e1000_copper_link_autoneg - Setup/Enable autoneg for copper link
1061*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1062*4882a593Smuzhiyun  *
1063*4882a593Smuzhiyun  *  Performs initial bounds checking on autoneg advertisement parameter, then
1064*4882a593Smuzhiyun  *  configure to advertise the full capability.  Setup the PHY to autoneg
1065*4882a593Smuzhiyun  *  and restart the negotiation process between the link partner.  If
1066*4882a593Smuzhiyun  *  autoneg_wait_to_complete, then wait for autoneg to complete before exiting.
1067*4882a593Smuzhiyun  **/
e1000_copper_link_autoneg(struct e1000_hw * hw)1068*4882a593Smuzhiyun static s32 e1000_copper_link_autoneg(struct e1000_hw *hw)
1069*4882a593Smuzhiyun {
1070*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1071*4882a593Smuzhiyun 	s32 ret_val;
1072*4882a593Smuzhiyun 	u16 phy_ctrl;
1073*4882a593Smuzhiyun 
1074*4882a593Smuzhiyun 	/* Perform some bounds checking on the autoneg advertisement
1075*4882a593Smuzhiyun 	 * parameter.
1076*4882a593Smuzhiyun 	 */
1077*4882a593Smuzhiyun 	phy->autoneg_advertised &= phy->autoneg_mask;
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun 	/* If autoneg_advertised is zero, we assume it was not defaulted
1080*4882a593Smuzhiyun 	 * by the calling code so we set to advertise full capability.
1081*4882a593Smuzhiyun 	 */
1082*4882a593Smuzhiyun 	if (!phy->autoneg_advertised)
1083*4882a593Smuzhiyun 		phy->autoneg_advertised = phy->autoneg_mask;
1084*4882a593Smuzhiyun 
1085*4882a593Smuzhiyun 	e_dbg("Reconfiguring auto-neg advertisement params\n");
1086*4882a593Smuzhiyun 	ret_val = e1000_phy_setup_autoneg(hw);
1087*4882a593Smuzhiyun 	if (ret_val) {
1088*4882a593Smuzhiyun 		e_dbg("Error Setting up Auto-Negotiation\n");
1089*4882a593Smuzhiyun 		return ret_val;
1090*4882a593Smuzhiyun 	}
1091*4882a593Smuzhiyun 	e_dbg("Restarting Auto-Neg\n");
1092*4882a593Smuzhiyun 
1093*4882a593Smuzhiyun 	/* Restart auto-negotiation by setting the Auto Neg Enable bit and
1094*4882a593Smuzhiyun 	 * the Auto Neg Restart bit in the PHY control register.
1095*4882a593Smuzhiyun 	 */
1096*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, MII_BMCR, &phy_ctrl);
1097*4882a593Smuzhiyun 	if (ret_val)
1098*4882a593Smuzhiyun 		return ret_val;
1099*4882a593Smuzhiyun 
1100*4882a593Smuzhiyun 	phy_ctrl |= (BMCR_ANENABLE | BMCR_ANRESTART);
1101*4882a593Smuzhiyun 	ret_val = e1e_wphy(hw, MII_BMCR, phy_ctrl);
1102*4882a593Smuzhiyun 	if (ret_val)
1103*4882a593Smuzhiyun 		return ret_val;
1104*4882a593Smuzhiyun 
1105*4882a593Smuzhiyun 	/* Does the user want to wait for Auto-Neg to complete here, or
1106*4882a593Smuzhiyun 	 * check at a later time (for example, callback routine).
1107*4882a593Smuzhiyun 	 */
1108*4882a593Smuzhiyun 	if (phy->autoneg_wait_to_complete) {
1109*4882a593Smuzhiyun 		ret_val = e1000_wait_autoneg(hw);
1110*4882a593Smuzhiyun 		if (ret_val) {
1111*4882a593Smuzhiyun 			e_dbg("Error while waiting for autoneg to complete\n");
1112*4882a593Smuzhiyun 			return ret_val;
1113*4882a593Smuzhiyun 		}
1114*4882a593Smuzhiyun 	}
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun 	hw->mac.get_link_status = true;
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun 	return ret_val;
1119*4882a593Smuzhiyun }
1120*4882a593Smuzhiyun 
1121*4882a593Smuzhiyun /**
1122*4882a593Smuzhiyun  *  e1000e_setup_copper_link - Configure copper link settings
1123*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1124*4882a593Smuzhiyun  *
1125*4882a593Smuzhiyun  *  Calls the appropriate function to configure the link for auto-neg or forced
1126*4882a593Smuzhiyun  *  speed and duplex.  Then we check for link, once link is established calls
1127*4882a593Smuzhiyun  *  to configure collision distance and flow control are called.  If link is
1128*4882a593Smuzhiyun  *  not established, we return -E1000_ERR_PHY (-2).
1129*4882a593Smuzhiyun  **/
e1000e_setup_copper_link(struct e1000_hw * hw)1130*4882a593Smuzhiyun s32 e1000e_setup_copper_link(struct e1000_hw *hw)
1131*4882a593Smuzhiyun {
1132*4882a593Smuzhiyun 	s32 ret_val;
1133*4882a593Smuzhiyun 	bool link;
1134*4882a593Smuzhiyun 
1135*4882a593Smuzhiyun 	if (hw->mac.autoneg) {
1136*4882a593Smuzhiyun 		/* Setup autoneg and flow control advertisement and perform
1137*4882a593Smuzhiyun 		 * autonegotiation.
1138*4882a593Smuzhiyun 		 */
1139*4882a593Smuzhiyun 		ret_val = e1000_copper_link_autoneg(hw);
1140*4882a593Smuzhiyun 		if (ret_val)
1141*4882a593Smuzhiyun 			return ret_val;
1142*4882a593Smuzhiyun 	} else {
1143*4882a593Smuzhiyun 		/* PHY will be set to 10H, 10F, 100H or 100F
1144*4882a593Smuzhiyun 		 * depending on user settings.
1145*4882a593Smuzhiyun 		 */
1146*4882a593Smuzhiyun 		e_dbg("Forcing Speed and Duplex\n");
1147*4882a593Smuzhiyun 		ret_val = hw->phy.ops.force_speed_duplex(hw);
1148*4882a593Smuzhiyun 		if (ret_val) {
1149*4882a593Smuzhiyun 			e_dbg("Error Forcing Speed and Duplex\n");
1150*4882a593Smuzhiyun 			return ret_val;
1151*4882a593Smuzhiyun 		}
1152*4882a593Smuzhiyun 	}
1153*4882a593Smuzhiyun 
1154*4882a593Smuzhiyun 	/* Check link status. Wait up to 100 microseconds for link to become
1155*4882a593Smuzhiyun 	 * valid.
1156*4882a593Smuzhiyun 	 */
1157*4882a593Smuzhiyun 	ret_val = e1000e_phy_has_link_generic(hw, COPPER_LINK_UP_LIMIT, 10,
1158*4882a593Smuzhiyun 					      &link);
1159*4882a593Smuzhiyun 	if (ret_val)
1160*4882a593Smuzhiyun 		return ret_val;
1161*4882a593Smuzhiyun 
1162*4882a593Smuzhiyun 	if (link) {
1163*4882a593Smuzhiyun 		e_dbg("Valid link established!!!\n");
1164*4882a593Smuzhiyun 		hw->mac.ops.config_collision_dist(hw);
1165*4882a593Smuzhiyun 		ret_val = e1000e_config_fc_after_link_up(hw);
1166*4882a593Smuzhiyun 	} else {
1167*4882a593Smuzhiyun 		e_dbg("Unable to establish link!!!\n");
1168*4882a593Smuzhiyun 	}
1169*4882a593Smuzhiyun 
1170*4882a593Smuzhiyun 	return ret_val;
1171*4882a593Smuzhiyun }
1172*4882a593Smuzhiyun 
1173*4882a593Smuzhiyun /**
1174*4882a593Smuzhiyun  *  e1000e_phy_force_speed_duplex_igp - Force speed/duplex for igp PHY
1175*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1176*4882a593Smuzhiyun  *
1177*4882a593Smuzhiyun  *  Calls the PHY setup function to force speed and duplex.  Clears the
1178*4882a593Smuzhiyun  *  auto-crossover to force MDI manually.  Waits for link and returns
1179*4882a593Smuzhiyun  *  successful if link up is successful, else -E1000_ERR_PHY (-2).
1180*4882a593Smuzhiyun  **/
e1000e_phy_force_speed_duplex_igp(struct e1000_hw * hw)1181*4882a593Smuzhiyun s32 e1000e_phy_force_speed_duplex_igp(struct e1000_hw *hw)
1182*4882a593Smuzhiyun {
1183*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1184*4882a593Smuzhiyun 	s32 ret_val;
1185*4882a593Smuzhiyun 	u16 phy_data;
1186*4882a593Smuzhiyun 	bool link;
1187*4882a593Smuzhiyun 
1188*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, MII_BMCR, &phy_data);
1189*4882a593Smuzhiyun 	if (ret_val)
1190*4882a593Smuzhiyun 		return ret_val;
1191*4882a593Smuzhiyun 
1192*4882a593Smuzhiyun 	e1000e_phy_force_speed_duplex_setup(hw, &phy_data);
1193*4882a593Smuzhiyun 
1194*4882a593Smuzhiyun 	ret_val = e1e_wphy(hw, MII_BMCR, phy_data);
1195*4882a593Smuzhiyun 	if (ret_val)
1196*4882a593Smuzhiyun 		return ret_val;
1197*4882a593Smuzhiyun 
1198*4882a593Smuzhiyun 	/* Clear Auto-Crossover to force MDI manually.  IGP requires MDI
1199*4882a593Smuzhiyun 	 * forced whenever speed and duplex are forced.
1200*4882a593Smuzhiyun 	 */
1201*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
1202*4882a593Smuzhiyun 	if (ret_val)
1203*4882a593Smuzhiyun 		return ret_val;
1204*4882a593Smuzhiyun 
1205*4882a593Smuzhiyun 	phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
1206*4882a593Smuzhiyun 	phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
1207*4882a593Smuzhiyun 
1208*4882a593Smuzhiyun 	ret_val = e1e_wphy(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
1209*4882a593Smuzhiyun 	if (ret_val)
1210*4882a593Smuzhiyun 		return ret_val;
1211*4882a593Smuzhiyun 
1212*4882a593Smuzhiyun 	e_dbg("IGP PSCR: %X\n", phy_data);
1213*4882a593Smuzhiyun 
1214*4882a593Smuzhiyun 	udelay(1);
1215*4882a593Smuzhiyun 
1216*4882a593Smuzhiyun 	if (phy->autoneg_wait_to_complete) {
1217*4882a593Smuzhiyun 		e_dbg("Waiting for forced speed/duplex link on IGP phy.\n");
1218*4882a593Smuzhiyun 
1219*4882a593Smuzhiyun 		ret_val = e1000e_phy_has_link_generic(hw, PHY_FORCE_LIMIT,
1220*4882a593Smuzhiyun 						      100000, &link);
1221*4882a593Smuzhiyun 		if (ret_val)
1222*4882a593Smuzhiyun 			return ret_val;
1223*4882a593Smuzhiyun 
1224*4882a593Smuzhiyun 		if (!link)
1225*4882a593Smuzhiyun 			e_dbg("Link taking longer than expected.\n");
1226*4882a593Smuzhiyun 
1227*4882a593Smuzhiyun 		/* Try once more */
1228*4882a593Smuzhiyun 		ret_val = e1000e_phy_has_link_generic(hw, PHY_FORCE_LIMIT,
1229*4882a593Smuzhiyun 						      100000, &link);
1230*4882a593Smuzhiyun 	}
1231*4882a593Smuzhiyun 
1232*4882a593Smuzhiyun 	return ret_val;
1233*4882a593Smuzhiyun }
1234*4882a593Smuzhiyun 
1235*4882a593Smuzhiyun /**
1236*4882a593Smuzhiyun  *  e1000e_phy_force_speed_duplex_m88 - Force speed/duplex for m88 PHY
1237*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1238*4882a593Smuzhiyun  *
1239*4882a593Smuzhiyun  *  Calls the PHY setup function to force speed and duplex.  Clears the
1240*4882a593Smuzhiyun  *  auto-crossover to force MDI manually.  Resets the PHY to commit the
1241*4882a593Smuzhiyun  *  changes.  If time expires while waiting for link up, we reset the DSP.
1242*4882a593Smuzhiyun  *  After reset, TX_CLK and CRS on Tx must be set.  Return successful upon
1243*4882a593Smuzhiyun  *  successful completion, else return corresponding error code.
1244*4882a593Smuzhiyun  **/
e1000e_phy_force_speed_duplex_m88(struct e1000_hw * hw)1245*4882a593Smuzhiyun s32 e1000e_phy_force_speed_duplex_m88(struct e1000_hw *hw)
1246*4882a593Smuzhiyun {
1247*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1248*4882a593Smuzhiyun 	s32 ret_val;
1249*4882a593Smuzhiyun 	u16 phy_data;
1250*4882a593Smuzhiyun 	bool link;
1251*4882a593Smuzhiyun 
1252*4882a593Smuzhiyun 	/* Clear Auto-Crossover to force MDI manually.  M88E1000 requires MDI
1253*4882a593Smuzhiyun 	 * forced whenever speed and duplex are forced.
1254*4882a593Smuzhiyun 	 */
1255*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1256*4882a593Smuzhiyun 	if (ret_val)
1257*4882a593Smuzhiyun 		return ret_val;
1258*4882a593Smuzhiyun 
1259*4882a593Smuzhiyun 	phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
1260*4882a593Smuzhiyun 	ret_val = e1e_wphy(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1261*4882a593Smuzhiyun 	if (ret_val)
1262*4882a593Smuzhiyun 		return ret_val;
1263*4882a593Smuzhiyun 
1264*4882a593Smuzhiyun 	e_dbg("M88E1000 PSCR: %X\n", phy_data);
1265*4882a593Smuzhiyun 
1266*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, MII_BMCR, &phy_data);
1267*4882a593Smuzhiyun 	if (ret_val)
1268*4882a593Smuzhiyun 		return ret_val;
1269*4882a593Smuzhiyun 
1270*4882a593Smuzhiyun 	e1000e_phy_force_speed_duplex_setup(hw, &phy_data);
1271*4882a593Smuzhiyun 
1272*4882a593Smuzhiyun 	ret_val = e1e_wphy(hw, MII_BMCR, phy_data);
1273*4882a593Smuzhiyun 	if (ret_val)
1274*4882a593Smuzhiyun 		return ret_val;
1275*4882a593Smuzhiyun 
1276*4882a593Smuzhiyun 	/* Reset the phy to commit changes. */
1277*4882a593Smuzhiyun 	if (hw->phy.ops.commit) {
1278*4882a593Smuzhiyun 		ret_val = hw->phy.ops.commit(hw);
1279*4882a593Smuzhiyun 		if (ret_val)
1280*4882a593Smuzhiyun 			return ret_val;
1281*4882a593Smuzhiyun 	}
1282*4882a593Smuzhiyun 
1283*4882a593Smuzhiyun 	if (phy->autoneg_wait_to_complete) {
1284*4882a593Smuzhiyun 		e_dbg("Waiting for forced speed/duplex link on M88 phy.\n");
1285*4882a593Smuzhiyun 
1286*4882a593Smuzhiyun 		ret_val = e1000e_phy_has_link_generic(hw, PHY_FORCE_LIMIT,
1287*4882a593Smuzhiyun 						      100000, &link);
1288*4882a593Smuzhiyun 		if (ret_val)
1289*4882a593Smuzhiyun 			return ret_val;
1290*4882a593Smuzhiyun 
1291*4882a593Smuzhiyun 		if (!link) {
1292*4882a593Smuzhiyun 			if (hw->phy.type != e1000_phy_m88) {
1293*4882a593Smuzhiyun 				e_dbg("Link taking longer than expected.\n");
1294*4882a593Smuzhiyun 			} else {
1295*4882a593Smuzhiyun 				/* We didn't get link.
1296*4882a593Smuzhiyun 				 * Reset the DSP and cross our fingers.
1297*4882a593Smuzhiyun 				 */
1298*4882a593Smuzhiyun 				ret_val = e1e_wphy(hw, M88E1000_PHY_PAGE_SELECT,
1299*4882a593Smuzhiyun 						   0x001d);
1300*4882a593Smuzhiyun 				if (ret_val)
1301*4882a593Smuzhiyun 					return ret_val;
1302*4882a593Smuzhiyun 				ret_val = e1000e_phy_reset_dsp(hw);
1303*4882a593Smuzhiyun 				if (ret_val)
1304*4882a593Smuzhiyun 					return ret_val;
1305*4882a593Smuzhiyun 			}
1306*4882a593Smuzhiyun 		}
1307*4882a593Smuzhiyun 
1308*4882a593Smuzhiyun 		/* Try once more */
1309*4882a593Smuzhiyun 		ret_val = e1000e_phy_has_link_generic(hw, PHY_FORCE_LIMIT,
1310*4882a593Smuzhiyun 						      100000, &link);
1311*4882a593Smuzhiyun 		if (ret_val)
1312*4882a593Smuzhiyun 			return ret_val;
1313*4882a593Smuzhiyun 	}
1314*4882a593Smuzhiyun 
1315*4882a593Smuzhiyun 	if (hw->phy.type != e1000_phy_m88)
1316*4882a593Smuzhiyun 		return 0;
1317*4882a593Smuzhiyun 
1318*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
1319*4882a593Smuzhiyun 	if (ret_val)
1320*4882a593Smuzhiyun 		return ret_val;
1321*4882a593Smuzhiyun 
1322*4882a593Smuzhiyun 	/* Resetting the phy means we need to re-force TX_CLK in the
1323*4882a593Smuzhiyun 	 * Extended PHY Specific Control Register to 25MHz clock from
1324*4882a593Smuzhiyun 	 * the reset value of 2.5MHz.
1325*4882a593Smuzhiyun 	 */
1326*4882a593Smuzhiyun 	phy_data |= M88E1000_EPSCR_TX_CLK_25;
1327*4882a593Smuzhiyun 	ret_val = e1e_wphy(hw, M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
1328*4882a593Smuzhiyun 	if (ret_val)
1329*4882a593Smuzhiyun 		return ret_val;
1330*4882a593Smuzhiyun 
1331*4882a593Smuzhiyun 	/* In addition, we must re-enable CRS on Tx for both half and full
1332*4882a593Smuzhiyun 	 * duplex.
1333*4882a593Smuzhiyun 	 */
1334*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1335*4882a593Smuzhiyun 	if (ret_val)
1336*4882a593Smuzhiyun 		return ret_val;
1337*4882a593Smuzhiyun 
1338*4882a593Smuzhiyun 	phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
1339*4882a593Smuzhiyun 	ret_val = e1e_wphy(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1340*4882a593Smuzhiyun 
1341*4882a593Smuzhiyun 	return ret_val;
1342*4882a593Smuzhiyun }
1343*4882a593Smuzhiyun 
1344*4882a593Smuzhiyun /**
1345*4882a593Smuzhiyun  *  e1000_phy_force_speed_duplex_ife - Force PHY speed & duplex
1346*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1347*4882a593Smuzhiyun  *
1348*4882a593Smuzhiyun  *  Forces the speed and duplex settings of the PHY.
1349*4882a593Smuzhiyun  *  This is a function pointer entry point only called by
1350*4882a593Smuzhiyun  *  PHY setup routines.
1351*4882a593Smuzhiyun  **/
e1000_phy_force_speed_duplex_ife(struct e1000_hw * hw)1352*4882a593Smuzhiyun s32 e1000_phy_force_speed_duplex_ife(struct e1000_hw *hw)
1353*4882a593Smuzhiyun {
1354*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1355*4882a593Smuzhiyun 	s32 ret_val;
1356*4882a593Smuzhiyun 	u16 data;
1357*4882a593Smuzhiyun 	bool link;
1358*4882a593Smuzhiyun 
1359*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, MII_BMCR, &data);
1360*4882a593Smuzhiyun 	if (ret_val)
1361*4882a593Smuzhiyun 		return ret_val;
1362*4882a593Smuzhiyun 
1363*4882a593Smuzhiyun 	e1000e_phy_force_speed_duplex_setup(hw, &data);
1364*4882a593Smuzhiyun 
1365*4882a593Smuzhiyun 	ret_val = e1e_wphy(hw, MII_BMCR, data);
1366*4882a593Smuzhiyun 	if (ret_val)
1367*4882a593Smuzhiyun 		return ret_val;
1368*4882a593Smuzhiyun 
1369*4882a593Smuzhiyun 	/* Disable MDI-X support for 10/100 */
1370*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, IFE_PHY_MDIX_CONTROL, &data);
1371*4882a593Smuzhiyun 	if (ret_val)
1372*4882a593Smuzhiyun 		return ret_val;
1373*4882a593Smuzhiyun 
1374*4882a593Smuzhiyun 	data &= ~IFE_PMC_AUTO_MDIX;
1375*4882a593Smuzhiyun 	data &= ~IFE_PMC_FORCE_MDIX;
1376*4882a593Smuzhiyun 
1377*4882a593Smuzhiyun 	ret_val = e1e_wphy(hw, IFE_PHY_MDIX_CONTROL, data);
1378*4882a593Smuzhiyun 	if (ret_val)
1379*4882a593Smuzhiyun 		return ret_val;
1380*4882a593Smuzhiyun 
1381*4882a593Smuzhiyun 	e_dbg("IFE PMC: %X\n", data);
1382*4882a593Smuzhiyun 
1383*4882a593Smuzhiyun 	udelay(1);
1384*4882a593Smuzhiyun 
1385*4882a593Smuzhiyun 	if (phy->autoneg_wait_to_complete) {
1386*4882a593Smuzhiyun 		e_dbg("Waiting for forced speed/duplex link on IFE phy.\n");
1387*4882a593Smuzhiyun 
1388*4882a593Smuzhiyun 		ret_val = e1000e_phy_has_link_generic(hw, PHY_FORCE_LIMIT,
1389*4882a593Smuzhiyun 						      100000, &link);
1390*4882a593Smuzhiyun 		if (ret_val)
1391*4882a593Smuzhiyun 			return ret_val;
1392*4882a593Smuzhiyun 
1393*4882a593Smuzhiyun 		if (!link)
1394*4882a593Smuzhiyun 			e_dbg("Link taking longer than expected.\n");
1395*4882a593Smuzhiyun 
1396*4882a593Smuzhiyun 		/* Try once more */
1397*4882a593Smuzhiyun 		ret_val = e1000e_phy_has_link_generic(hw, PHY_FORCE_LIMIT,
1398*4882a593Smuzhiyun 						      100000, &link);
1399*4882a593Smuzhiyun 		if (ret_val)
1400*4882a593Smuzhiyun 			return ret_val;
1401*4882a593Smuzhiyun 	}
1402*4882a593Smuzhiyun 
1403*4882a593Smuzhiyun 	return 0;
1404*4882a593Smuzhiyun }
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun /**
1407*4882a593Smuzhiyun  *  e1000e_phy_force_speed_duplex_setup - Configure forced PHY speed/duplex
1408*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1409*4882a593Smuzhiyun  *  @phy_ctrl: pointer to current value of MII_BMCR
1410*4882a593Smuzhiyun  *
1411*4882a593Smuzhiyun  *  Forces speed and duplex on the PHY by doing the following: disable flow
1412*4882a593Smuzhiyun  *  control, force speed/duplex on the MAC, disable auto speed detection,
1413*4882a593Smuzhiyun  *  disable auto-negotiation, configure duplex, configure speed, configure
1414*4882a593Smuzhiyun  *  the collision distance, write configuration to CTRL register.  The
1415*4882a593Smuzhiyun  *  caller must write to the MII_BMCR register for these settings to
1416*4882a593Smuzhiyun  *  take affect.
1417*4882a593Smuzhiyun  **/
e1000e_phy_force_speed_duplex_setup(struct e1000_hw * hw,u16 * phy_ctrl)1418*4882a593Smuzhiyun void e1000e_phy_force_speed_duplex_setup(struct e1000_hw *hw, u16 *phy_ctrl)
1419*4882a593Smuzhiyun {
1420*4882a593Smuzhiyun 	struct e1000_mac_info *mac = &hw->mac;
1421*4882a593Smuzhiyun 	u32 ctrl;
1422*4882a593Smuzhiyun 
1423*4882a593Smuzhiyun 	/* Turn off flow control when forcing speed/duplex */
1424*4882a593Smuzhiyun 	hw->fc.current_mode = e1000_fc_none;
1425*4882a593Smuzhiyun 
1426*4882a593Smuzhiyun 	/* Force speed/duplex on the mac */
1427*4882a593Smuzhiyun 	ctrl = er32(CTRL);
1428*4882a593Smuzhiyun 	ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
1429*4882a593Smuzhiyun 	ctrl &= ~E1000_CTRL_SPD_SEL;
1430*4882a593Smuzhiyun 
1431*4882a593Smuzhiyun 	/* Disable Auto Speed Detection */
1432*4882a593Smuzhiyun 	ctrl &= ~E1000_CTRL_ASDE;
1433*4882a593Smuzhiyun 
1434*4882a593Smuzhiyun 	/* Disable autoneg on the phy */
1435*4882a593Smuzhiyun 	*phy_ctrl &= ~BMCR_ANENABLE;
1436*4882a593Smuzhiyun 
1437*4882a593Smuzhiyun 	/* Forcing Full or Half Duplex? */
1438*4882a593Smuzhiyun 	if (mac->forced_speed_duplex & E1000_ALL_HALF_DUPLEX) {
1439*4882a593Smuzhiyun 		ctrl &= ~E1000_CTRL_FD;
1440*4882a593Smuzhiyun 		*phy_ctrl &= ~BMCR_FULLDPLX;
1441*4882a593Smuzhiyun 		e_dbg("Half Duplex\n");
1442*4882a593Smuzhiyun 	} else {
1443*4882a593Smuzhiyun 		ctrl |= E1000_CTRL_FD;
1444*4882a593Smuzhiyun 		*phy_ctrl |= BMCR_FULLDPLX;
1445*4882a593Smuzhiyun 		e_dbg("Full Duplex\n");
1446*4882a593Smuzhiyun 	}
1447*4882a593Smuzhiyun 
1448*4882a593Smuzhiyun 	/* Forcing 10mb or 100mb? */
1449*4882a593Smuzhiyun 	if (mac->forced_speed_duplex & E1000_ALL_100_SPEED) {
1450*4882a593Smuzhiyun 		ctrl |= E1000_CTRL_SPD_100;
1451*4882a593Smuzhiyun 		*phy_ctrl |= BMCR_SPEED100;
1452*4882a593Smuzhiyun 		*phy_ctrl &= ~BMCR_SPEED1000;
1453*4882a593Smuzhiyun 		e_dbg("Forcing 100mb\n");
1454*4882a593Smuzhiyun 	} else {
1455*4882a593Smuzhiyun 		ctrl &= ~(E1000_CTRL_SPD_1000 | E1000_CTRL_SPD_100);
1456*4882a593Smuzhiyun 		*phy_ctrl &= ~(BMCR_SPEED1000 | BMCR_SPEED100);
1457*4882a593Smuzhiyun 		e_dbg("Forcing 10mb\n");
1458*4882a593Smuzhiyun 	}
1459*4882a593Smuzhiyun 
1460*4882a593Smuzhiyun 	hw->mac.ops.config_collision_dist(hw);
1461*4882a593Smuzhiyun 
1462*4882a593Smuzhiyun 	ew32(CTRL, ctrl);
1463*4882a593Smuzhiyun }
1464*4882a593Smuzhiyun 
1465*4882a593Smuzhiyun /**
1466*4882a593Smuzhiyun  *  e1000e_set_d3_lplu_state - Sets low power link up state for D3
1467*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1468*4882a593Smuzhiyun  *  @active: boolean used to enable/disable lplu
1469*4882a593Smuzhiyun  *
1470*4882a593Smuzhiyun  *  Success returns 0, Failure returns 1
1471*4882a593Smuzhiyun  *
1472*4882a593Smuzhiyun  *  The low power link up (lplu) state is set to the power management level D3
1473*4882a593Smuzhiyun  *  and SmartSpeed is disabled when active is true, else clear lplu for D3
1474*4882a593Smuzhiyun  *  and enable Smartspeed.  LPLU and Smartspeed are mutually exclusive.  LPLU
1475*4882a593Smuzhiyun  *  is used during Dx states where the power conservation is most important.
1476*4882a593Smuzhiyun  *  During driver activity, SmartSpeed should be enabled so performance is
1477*4882a593Smuzhiyun  *  maintained.
1478*4882a593Smuzhiyun  **/
e1000e_set_d3_lplu_state(struct e1000_hw * hw,bool active)1479*4882a593Smuzhiyun s32 e1000e_set_d3_lplu_state(struct e1000_hw *hw, bool active)
1480*4882a593Smuzhiyun {
1481*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1482*4882a593Smuzhiyun 	s32 ret_val;
1483*4882a593Smuzhiyun 	u16 data;
1484*4882a593Smuzhiyun 
1485*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, IGP02E1000_PHY_POWER_MGMT, &data);
1486*4882a593Smuzhiyun 	if (ret_val)
1487*4882a593Smuzhiyun 		return ret_val;
1488*4882a593Smuzhiyun 
1489*4882a593Smuzhiyun 	if (!active) {
1490*4882a593Smuzhiyun 		data &= ~IGP02E1000_PM_D3_LPLU;
1491*4882a593Smuzhiyun 		ret_val = e1e_wphy(hw, IGP02E1000_PHY_POWER_MGMT, data);
1492*4882a593Smuzhiyun 		if (ret_val)
1493*4882a593Smuzhiyun 			return ret_val;
1494*4882a593Smuzhiyun 		/* LPLU and SmartSpeed are mutually exclusive.  LPLU is used
1495*4882a593Smuzhiyun 		 * during Dx states where the power conservation is most
1496*4882a593Smuzhiyun 		 * important.  During driver activity we should enable
1497*4882a593Smuzhiyun 		 * SmartSpeed, so performance is maintained.
1498*4882a593Smuzhiyun 		 */
1499*4882a593Smuzhiyun 		if (phy->smart_speed == e1000_smart_speed_on) {
1500*4882a593Smuzhiyun 			ret_val = e1e_rphy(hw, IGP01E1000_PHY_PORT_CONFIG,
1501*4882a593Smuzhiyun 					   &data);
1502*4882a593Smuzhiyun 			if (ret_val)
1503*4882a593Smuzhiyun 				return ret_val;
1504*4882a593Smuzhiyun 
1505*4882a593Smuzhiyun 			data |= IGP01E1000_PSCFR_SMART_SPEED;
1506*4882a593Smuzhiyun 			ret_val = e1e_wphy(hw, IGP01E1000_PHY_PORT_CONFIG,
1507*4882a593Smuzhiyun 					   data);
1508*4882a593Smuzhiyun 			if (ret_val)
1509*4882a593Smuzhiyun 				return ret_val;
1510*4882a593Smuzhiyun 		} else if (phy->smart_speed == e1000_smart_speed_off) {
1511*4882a593Smuzhiyun 			ret_val = e1e_rphy(hw, IGP01E1000_PHY_PORT_CONFIG,
1512*4882a593Smuzhiyun 					   &data);
1513*4882a593Smuzhiyun 			if (ret_val)
1514*4882a593Smuzhiyun 				return ret_val;
1515*4882a593Smuzhiyun 
1516*4882a593Smuzhiyun 			data &= ~IGP01E1000_PSCFR_SMART_SPEED;
1517*4882a593Smuzhiyun 			ret_val = e1e_wphy(hw, IGP01E1000_PHY_PORT_CONFIG,
1518*4882a593Smuzhiyun 					   data);
1519*4882a593Smuzhiyun 			if (ret_val)
1520*4882a593Smuzhiyun 				return ret_val;
1521*4882a593Smuzhiyun 		}
1522*4882a593Smuzhiyun 	} else if ((phy->autoneg_advertised == E1000_ALL_SPEED_DUPLEX) ||
1523*4882a593Smuzhiyun 		   (phy->autoneg_advertised == E1000_ALL_NOT_GIG) ||
1524*4882a593Smuzhiyun 		   (phy->autoneg_advertised == E1000_ALL_10_SPEED)) {
1525*4882a593Smuzhiyun 		data |= IGP02E1000_PM_D3_LPLU;
1526*4882a593Smuzhiyun 		ret_val = e1e_wphy(hw, IGP02E1000_PHY_POWER_MGMT, data);
1527*4882a593Smuzhiyun 		if (ret_val)
1528*4882a593Smuzhiyun 			return ret_val;
1529*4882a593Smuzhiyun 
1530*4882a593Smuzhiyun 		/* When LPLU is enabled, we should disable SmartSpeed */
1531*4882a593Smuzhiyun 		ret_val = e1e_rphy(hw, IGP01E1000_PHY_PORT_CONFIG, &data);
1532*4882a593Smuzhiyun 		if (ret_val)
1533*4882a593Smuzhiyun 			return ret_val;
1534*4882a593Smuzhiyun 
1535*4882a593Smuzhiyun 		data &= ~IGP01E1000_PSCFR_SMART_SPEED;
1536*4882a593Smuzhiyun 		ret_val = e1e_wphy(hw, IGP01E1000_PHY_PORT_CONFIG, data);
1537*4882a593Smuzhiyun 	}
1538*4882a593Smuzhiyun 
1539*4882a593Smuzhiyun 	return ret_val;
1540*4882a593Smuzhiyun }
1541*4882a593Smuzhiyun 
1542*4882a593Smuzhiyun /**
1543*4882a593Smuzhiyun  *  e1000e_check_downshift - Checks whether a downshift in speed occurred
1544*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1545*4882a593Smuzhiyun  *
1546*4882a593Smuzhiyun  *  Success returns 0, Failure returns 1
1547*4882a593Smuzhiyun  *
1548*4882a593Smuzhiyun  *  A downshift is detected by querying the PHY link health.
1549*4882a593Smuzhiyun  **/
e1000e_check_downshift(struct e1000_hw * hw)1550*4882a593Smuzhiyun s32 e1000e_check_downshift(struct e1000_hw *hw)
1551*4882a593Smuzhiyun {
1552*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1553*4882a593Smuzhiyun 	s32 ret_val;
1554*4882a593Smuzhiyun 	u16 phy_data, offset, mask;
1555*4882a593Smuzhiyun 
1556*4882a593Smuzhiyun 	switch (phy->type) {
1557*4882a593Smuzhiyun 	case e1000_phy_m88:
1558*4882a593Smuzhiyun 	case e1000_phy_gg82563:
1559*4882a593Smuzhiyun 	case e1000_phy_bm:
1560*4882a593Smuzhiyun 	case e1000_phy_82578:
1561*4882a593Smuzhiyun 		offset = M88E1000_PHY_SPEC_STATUS;
1562*4882a593Smuzhiyun 		mask = M88E1000_PSSR_DOWNSHIFT;
1563*4882a593Smuzhiyun 		break;
1564*4882a593Smuzhiyun 	case e1000_phy_igp_2:
1565*4882a593Smuzhiyun 	case e1000_phy_igp_3:
1566*4882a593Smuzhiyun 		offset = IGP01E1000_PHY_LINK_HEALTH;
1567*4882a593Smuzhiyun 		mask = IGP01E1000_PLHR_SS_DOWNGRADE;
1568*4882a593Smuzhiyun 		break;
1569*4882a593Smuzhiyun 	default:
1570*4882a593Smuzhiyun 		/* speed downshift not supported */
1571*4882a593Smuzhiyun 		phy->speed_downgraded = false;
1572*4882a593Smuzhiyun 		return 0;
1573*4882a593Smuzhiyun 	}
1574*4882a593Smuzhiyun 
1575*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, offset, &phy_data);
1576*4882a593Smuzhiyun 
1577*4882a593Smuzhiyun 	if (!ret_val)
1578*4882a593Smuzhiyun 		phy->speed_downgraded = !!(phy_data & mask);
1579*4882a593Smuzhiyun 
1580*4882a593Smuzhiyun 	return ret_val;
1581*4882a593Smuzhiyun }
1582*4882a593Smuzhiyun 
1583*4882a593Smuzhiyun /**
1584*4882a593Smuzhiyun  *  e1000_check_polarity_m88 - Checks the polarity.
1585*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1586*4882a593Smuzhiyun  *
1587*4882a593Smuzhiyun  *  Success returns 0, Failure returns -E1000_ERR_PHY (-2)
1588*4882a593Smuzhiyun  *
1589*4882a593Smuzhiyun  *  Polarity is determined based on the PHY specific status register.
1590*4882a593Smuzhiyun  **/
e1000_check_polarity_m88(struct e1000_hw * hw)1591*4882a593Smuzhiyun s32 e1000_check_polarity_m88(struct e1000_hw *hw)
1592*4882a593Smuzhiyun {
1593*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1594*4882a593Smuzhiyun 	s32 ret_val;
1595*4882a593Smuzhiyun 	u16 data;
1596*4882a593Smuzhiyun 
1597*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, M88E1000_PHY_SPEC_STATUS, &data);
1598*4882a593Smuzhiyun 
1599*4882a593Smuzhiyun 	if (!ret_val)
1600*4882a593Smuzhiyun 		phy->cable_polarity = ((data & M88E1000_PSSR_REV_POLARITY)
1601*4882a593Smuzhiyun 				       ? e1000_rev_polarity_reversed
1602*4882a593Smuzhiyun 				       : e1000_rev_polarity_normal);
1603*4882a593Smuzhiyun 
1604*4882a593Smuzhiyun 	return ret_val;
1605*4882a593Smuzhiyun }
1606*4882a593Smuzhiyun 
1607*4882a593Smuzhiyun /**
1608*4882a593Smuzhiyun  *  e1000_check_polarity_igp - Checks the polarity.
1609*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1610*4882a593Smuzhiyun  *
1611*4882a593Smuzhiyun  *  Success returns 0, Failure returns -E1000_ERR_PHY (-2)
1612*4882a593Smuzhiyun  *
1613*4882a593Smuzhiyun  *  Polarity is determined based on the PHY port status register, and the
1614*4882a593Smuzhiyun  *  current speed (since there is no polarity at 100Mbps).
1615*4882a593Smuzhiyun  **/
e1000_check_polarity_igp(struct e1000_hw * hw)1616*4882a593Smuzhiyun s32 e1000_check_polarity_igp(struct e1000_hw *hw)
1617*4882a593Smuzhiyun {
1618*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1619*4882a593Smuzhiyun 	s32 ret_val;
1620*4882a593Smuzhiyun 	u16 data, offset, mask;
1621*4882a593Smuzhiyun 
1622*4882a593Smuzhiyun 	/* Polarity is determined based on the speed of
1623*4882a593Smuzhiyun 	 * our connection.
1624*4882a593Smuzhiyun 	 */
1625*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, IGP01E1000_PHY_PORT_STATUS, &data);
1626*4882a593Smuzhiyun 	if (ret_val)
1627*4882a593Smuzhiyun 		return ret_val;
1628*4882a593Smuzhiyun 
1629*4882a593Smuzhiyun 	if ((data & IGP01E1000_PSSR_SPEED_MASK) ==
1630*4882a593Smuzhiyun 	    IGP01E1000_PSSR_SPEED_1000MBPS) {
1631*4882a593Smuzhiyun 		offset = IGP01E1000_PHY_PCS_INIT_REG;
1632*4882a593Smuzhiyun 		mask = IGP01E1000_PHY_POLARITY_MASK;
1633*4882a593Smuzhiyun 	} else {
1634*4882a593Smuzhiyun 		/* This really only applies to 10Mbps since
1635*4882a593Smuzhiyun 		 * there is no polarity for 100Mbps (always 0).
1636*4882a593Smuzhiyun 		 */
1637*4882a593Smuzhiyun 		offset = IGP01E1000_PHY_PORT_STATUS;
1638*4882a593Smuzhiyun 		mask = IGP01E1000_PSSR_POLARITY_REVERSED;
1639*4882a593Smuzhiyun 	}
1640*4882a593Smuzhiyun 
1641*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, offset, &data);
1642*4882a593Smuzhiyun 
1643*4882a593Smuzhiyun 	if (!ret_val)
1644*4882a593Smuzhiyun 		phy->cable_polarity = ((data & mask)
1645*4882a593Smuzhiyun 				       ? e1000_rev_polarity_reversed
1646*4882a593Smuzhiyun 				       : e1000_rev_polarity_normal);
1647*4882a593Smuzhiyun 
1648*4882a593Smuzhiyun 	return ret_val;
1649*4882a593Smuzhiyun }
1650*4882a593Smuzhiyun 
1651*4882a593Smuzhiyun /**
1652*4882a593Smuzhiyun  *  e1000_check_polarity_ife - Check cable polarity for IFE PHY
1653*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1654*4882a593Smuzhiyun  *
1655*4882a593Smuzhiyun  *  Polarity is determined on the polarity reversal feature being enabled.
1656*4882a593Smuzhiyun  **/
e1000_check_polarity_ife(struct e1000_hw * hw)1657*4882a593Smuzhiyun s32 e1000_check_polarity_ife(struct e1000_hw *hw)
1658*4882a593Smuzhiyun {
1659*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1660*4882a593Smuzhiyun 	s32 ret_val;
1661*4882a593Smuzhiyun 	u16 phy_data, offset, mask;
1662*4882a593Smuzhiyun 
1663*4882a593Smuzhiyun 	/* Polarity is determined based on the reversal feature being enabled.
1664*4882a593Smuzhiyun 	 */
1665*4882a593Smuzhiyun 	if (phy->polarity_correction) {
1666*4882a593Smuzhiyun 		offset = IFE_PHY_EXTENDED_STATUS_CONTROL;
1667*4882a593Smuzhiyun 		mask = IFE_PESC_POLARITY_REVERSED;
1668*4882a593Smuzhiyun 	} else {
1669*4882a593Smuzhiyun 		offset = IFE_PHY_SPECIAL_CONTROL;
1670*4882a593Smuzhiyun 		mask = IFE_PSC_FORCE_POLARITY;
1671*4882a593Smuzhiyun 	}
1672*4882a593Smuzhiyun 
1673*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, offset, &phy_data);
1674*4882a593Smuzhiyun 
1675*4882a593Smuzhiyun 	if (!ret_val)
1676*4882a593Smuzhiyun 		phy->cable_polarity = ((phy_data & mask)
1677*4882a593Smuzhiyun 				       ? e1000_rev_polarity_reversed
1678*4882a593Smuzhiyun 				       : e1000_rev_polarity_normal);
1679*4882a593Smuzhiyun 
1680*4882a593Smuzhiyun 	return ret_val;
1681*4882a593Smuzhiyun }
1682*4882a593Smuzhiyun 
1683*4882a593Smuzhiyun /**
1684*4882a593Smuzhiyun  *  e1000_wait_autoneg - Wait for auto-neg completion
1685*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1686*4882a593Smuzhiyun  *
1687*4882a593Smuzhiyun  *  Waits for auto-negotiation to complete or for the auto-negotiation time
1688*4882a593Smuzhiyun  *  limit to expire, which ever happens first.
1689*4882a593Smuzhiyun  **/
e1000_wait_autoneg(struct e1000_hw * hw)1690*4882a593Smuzhiyun static s32 e1000_wait_autoneg(struct e1000_hw *hw)
1691*4882a593Smuzhiyun {
1692*4882a593Smuzhiyun 	s32 ret_val = 0;
1693*4882a593Smuzhiyun 	u16 i, phy_status;
1694*4882a593Smuzhiyun 
1695*4882a593Smuzhiyun 	/* Break after autoneg completes or PHY_AUTO_NEG_LIMIT expires. */
1696*4882a593Smuzhiyun 	for (i = PHY_AUTO_NEG_LIMIT; i > 0; i--) {
1697*4882a593Smuzhiyun 		ret_val = e1e_rphy(hw, MII_BMSR, &phy_status);
1698*4882a593Smuzhiyun 		if (ret_val)
1699*4882a593Smuzhiyun 			break;
1700*4882a593Smuzhiyun 		ret_val = e1e_rphy(hw, MII_BMSR, &phy_status);
1701*4882a593Smuzhiyun 		if (ret_val)
1702*4882a593Smuzhiyun 			break;
1703*4882a593Smuzhiyun 		if (phy_status & BMSR_ANEGCOMPLETE)
1704*4882a593Smuzhiyun 			break;
1705*4882a593Smuzhiyun 		msleep(100);
1706*4882a593Smuzhiyun 	}
1707*4882a593Smuzhiyun 
1708*4882a593Smuzhiyun 	/* PHY_AUTO_NEG_TIME expiration doesn't guarantee auto-negotiation
1709*4882a593Smuzhiyun 	 * has completed.
1710*4882a593Smuzhiyun 	 */
1711*4882a593Smuzhiyun 	return ret_val;
1712*4882a593Smuzhiyun }
1713*4882a593Smuzhiyun 
1714*4882a593Smuzhiyun /**
1715*4882a593Smuzhiyun  *  e1000e_phy_has_link_generic - Polls PHY for link
1716*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1717*4882a593Smuzhiyun  *  @iterations: number of times to poll for link
1718*4882a593Smuzhiyun  *  @usec_interval: delay between polling attempts
1719*4882a593Smuzhiyun  *  @success: pointer to whether polling was successful or not
1720*4882a593Smuzhiyun  *
1721*4882a593Smuzhiyun  *  Polls the PHY status register for link, 'iterations' number of times.
1722*4882a593Smuzhiyun  **/
e1000e_phy_has_link_generic(struct e1000_hw * hw,u32 iterations,u32 usec_interval,bool * success)1723*4882a593Smuzhiyun s32 e1000e_phy_has_link_generic(struct e1000_hw *hw, u32 iterations,
1724*4882a593Smuzhiyun 				u32 usec_interval, bool *success)
1725*4882a593Smuzhiyun {
1726*4882a593Smuzhiyun 	s32 ret_val = 0;
1727*4882a593Smuzhiyun 	u16 i, phy_status;
1728*4882a593Smuzhiyun 
1729*4882a593Smuzhiyun 	*success = false;
1730*4882a593Smuzhiyun 	for (i = 0; i < iterations; i++) {
1731*4882a593Smuzhiyun 		/* Some PHYs require the MII_BMSR register to be read
1732*4882a593Smuzhiyun 		 * twice due to the link bit being sticky.  No harm doing
1733*4882a593Smuzhiyun 		 * it across the board.
1734*4882a593Smuzhiyun 		 */
1735*4882a593Smuzhiyun 		ret_val = e1e_rphy(hw, MII_BMSR, &phy_status);
1736*4882a593Smuzhiyun 		if (ret_val) {
1737*4882a593Smuzhiyun 			/* If the first read fails, another entity may have
1738*4882a593Smuzhiyun 			 * ownership of the resources, wait and try again to
1739*4882a593Smuzhiyun 			 * see if they have relinquished the resources yet.
1740*4882a593Smuzhiyun 			 */
1741*4882a593Smuzhiyun 			if (usec_interval >= 1000)
1742*4882a593Smuzhiyun 				msleep(usec_interval / 1000);
1743*4882a593Smuzhiyun 			else
1744*4882a593Smuzhiyun 				udelay(usec_interval);
1745*4882a593Smuzhiyun 		}
1746*4882a593Smuzhiyun 		ret_val = e1e_rphy(hw, MII_BMSR, &phy_status);
1747*4882a593Smuzhiyun 		if (ret_val)
1748*4882a593Smuzhiyun 			break;
1749*4882a593Smuzhiyun 		if (phy_status & BMSR_LSTATUS) {
1750*4882a593Smuzhiyun 			*success = true;
1751*4882a593Smuzhiyun 			break;
1752*4882a593Smuzhiyun 		}
1753*4882a593Smuzhiyun 		if (usec_interval >= 1000)
1754*4882a593Smuzhiyun 			msleep(usec_interval / 1000);
1755*4882a593Smuzhiyun 		else
1756*4882a593Smuzhiyun 			udelay(usec_interval);
1757*4882a593Smuzhiyun 	}
1758*4882a593Smuzhiyun 
1759*4882a593Smuzhiyun 	return ret_val;
1760*4882a593Smuzhiyun }
1761*4882a593Smuzhiyun 
1762*4882a593Smuzhiyun /**
1763*4882a593Smuzhiyun  *  e1000e_get_cable_length_m88 - Determine cable length for m88 PHY
1764*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1765*4882a593Smuzhiyun  *
1766*4882a593Smuzhiyun  *  Reads the PHY specific status register to retrieve the cable length
1767*4882a593Smuzhiyun  *  information.  The cable length is determined by averaging the minimum and
1768*4882a593Smuzhiyun  *  maximum values to get the "average" cable length.  The m88 PHY has four
1769*4882a593Smuzhiyun  *  possible cable length values, which are:
1770*4882a593Smuzhiyun  *	Register Value		Cable Length
1771*4882a593Smuzhiyun  *	0			< 50 meters
1772*4882a593Smuzhiyun  *	1			50 - 80 meters
1773*4882a593Smuzhiyun  *	2			80 - 110 meters
1774*4882a593Smuzhiyun  *	3			110 - 140 meters
1775*4882a593Smuzhiyun  *	4			> 140 meters
1776*4882a593Smuzhiyun  **/
e1000e_get_cable_length_m88(struct e1000_hw * hw)1777*4882a593Smuzhiyun s32 e1000e_get_cable_length_m88(struct e1000_hw *hw)
1778*4882a593Smuzhiyun {
1779*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1780*4882a593Smuzhiyun 	s32 ret_val;
1781*4882a593Smuzhiyun 	u16 phy_data, index;
1782*4882a593Smuzhiyun 
1783*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
1784*4882a593Smuzhiyun 	if (ret_val)
1785*4882a593Smuzhiyun 		return ret_val;
1786*4882a593Smuzhiyun 
1787*4882a593Smuzhiyun 	index = ((phy_data & M88E1000_PSSR_CABLE_LENGTH) >>
1788*4882a593Smuzhiyun 		 M88E1000_PSSR_CABLE_LENGTH_SHIFT);
1789*4882a593Smuzhiyun 
1790*4882a593Smuzhiyun 	if (index >= M88E1000_CABLE_LENGTH_TABLE_SIZE - 1)
1791*4882a593Smuzhiyun 		return -E1000_ERR_PHY;
1792*4882a593Smuzhiyun 
1793*4882a593Smuzhiyun 	phy->min_cable_length = e1000_m88_cable_length_table[index];
1794*4882a593Smuzhiyun 	phy->max_cable_length = e1000_m88_cable_length_table[index + 1];
1795*4882a593Smuzhiyun 
1796*4882a593Smuzhiyun 	phy->cable_length = (phy->min_cable_length + phy->max_cable_length) / 2;
1797*4882a593Smuzhiyun 
1798*4882a593Smuzhiyun 	return 0;
1799*4882a593Smuzhiyun }
1800*4882a593Smuzhiyun 
1801*4882a593Smuzhiyun /**
1802*4882a593Smuzhiyun  *  e1000e_get_cable_length_igp_2 - Determine cable length for igp2 PHY
1803*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1804*4882a593Smuzhiyun  *
1805*4882a593Smuzhiyun  *  The automatic gain control (agc) normalizes the amplitude of the
1806*4882a593Smuzhiyun  *  received signal, adjusting for the attenuation produced by the
1807*4882a593Smuzhiyun  *  cable.  By reading the AGC registers, which represent the
1808*4882a593Smuzhiyun  *  combination of coarse and fine gain value, the value can be put
1809*4882a593Smuzhiyun  *  into a lookup table to obtain the approximate cable length
1810*4882a593Smuzhiyun  *  for each channel.
1811*4882a593Smuzhiyun  **/
e1000e_get_cable_length_igp_2(struct e1000_hw * hw)1812*4882a593Smuzhiyun s32 e1000e_get_cable_length_igp_2(struct e1000_hw *hw)
1813*4882a593Smuzhiyun {
1814*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1815*4882a593Smuzhiyun 	s32 ret_val;
1816*4882a593Smuzhiyun 	u16 phy_data, i, agc_value = 0;
1817*4882a593Smuzhiyun 	u16 cur_agc_index, max_agc_index = 0;
1818*4882a593Smuzhiyun 	u16 min_agc_index = IGP02E1000_CABLE_LENGTH_TABLE_SIZE - 1;
1819*4882a593Smuzhiyun 	static const u16 agc_reg_array[IGP02E1000_PHY_CHANNEL_NUM] = {
1820*4882a593Smuzhiyun 		IGP02E1000_PHY_AGC_A,
1821*4882a593Smuzhiyun 		IGP02E1000_PHY_AGC_B,
1822*4882a593Smuzhiyun 		IGP02E1000_PHY_AGC_C,
1823*4882a593Smuzhiyun 		IGP02E1000_PHY_AGC_D
1824*4882a593Smuzhiyun 	};
1825*4882a593Smuzhiyun 
1826*4882a593Smuzhiyun 	/* Read the AGC registers for all channels */
1827*4882a593Smuzhiyun 	for (i = 0; i < IGP02E1000_PHY_CHANNEL_NUM; i++) {
1828*4882a593Smuzhiyun 		ret_val = e1e_rphy(hw, agc_reg_array[i], &phy_data);
1829*4882a593Smuzhiyun 		if (ret_val)
1830*4882a593Smuzhiyun 			return ret_val;
1831*4882a593Smuzhiyun 
1832*4882a593Smuzhiyun 		/* Getting bits 15:9, which represent the combination of
1833*4882a593Smuzhiyun 		 * coarse and fine gain values.  The result is a number
1834*4882a593Smuzhiyun 		 * that can be put into the lookup table to obtain the
1835*4882a593Smuzhiyun 		 * approximate cable length.
1836*4882a593Smuzhiyun 		 */
1837*4882a593Smuzhiyun 		cur_agc_index = ((phy_data >> IGP02E1000_AGC_LENGTH_SHIFT) &
1838*4882a593Smuzhiyun 				 IGP02E1000_AGC_LENGTH_MASK);
1839*4882a593Smuzhiyun 
1840*4882a593Smuzhiyun 		/* Array index bound check. */
1841*4882a593Smuzhiyun 		if ((cur_agc_index >= IGP02E1000_CABLE_LENGTH_TABLE_SIZE) ||
1842*4882a593Smuzhiyun 		    (cur_agc_index == 0))
1843*4882a593Smuzhiyun 			return -E1000_ERR_PHY;
1844*4882a593Smuzhiyun 
1845*4882a593Smuzhiyun 		/* Remove min & max AGC values from calculation. */
1846*4882a593Smuzhiyun 		if (e1000_igp_2_cable_length_table[min_agc_index] >
1847*4882a593Smuzhiyun 		    e1000_igp_2_cable_length_table[cur_agc_index])
1848*4882a593Smuzhiyun 			min_agc_index = cur_agc_index;
1849*4882a593Smuzhiyun 		if (e1000_igp_2_cable_length_table[max_agc_index] <
1850*4882a593Smuzhiyun 		    e1000_igp_2_cable_length_table[cur_agc_index])
1851*4882a593Smuzhiyun 			max_agc_index = cur_agc_index;
1852*4882a593Smuzhiyun 
1853*4882a593Smuzhiyun 		agc_value += e1000_igp_2_cable_length_table[cur_agc_index];
1854*4882a593Smuzhiyun 	}
1855*4882a593Smuzhiyun 
1856*4882a593Smuzhiyun 	agc_value -= (e1000_igp_2_cable_length_table[min_agc_index] +
1857*4882a593Smuzhiyun 		      e1000_igp_2_cable_length_table[max_agc_index]);
1858*4882a593Smuzhiyun 	agc_value /= (IGP02E1000_PHY_CHANNEL_NUM - 2);
1859*4882a593Smuzhiyun 
1860*4882a593Smuzhiyun 	/* Calculate cable length with the error range of +/- 10 meters. */
1861*4882a593Smuzhiyun 	phy->min_cable_length = (((agc_value - IGP02E1000_AGC_RANGE) > 0) ?
1862*4882a593Smuzhiyun 				 (agc_value - IGP02E1000_AGC_RANGE) : 0);
1863*4882a593Smuzhiyun 	phy->max_cable_length = agc_value + IGP02E1000_AGC_RANGE;
1864*4882a593Smuzhiyun 
1865*4882a593Smuzhiyun 	phy->cable_length = (phy->min_cable_length + phy->max_cable_length) / 2;
1866*4882a593Smuzhiyun 
1867*4882a593Smuzhiyun 	return 0;
1868*4882a593Smuzhiyun }
1869*4882a593Smuzhiyun 
1870*4882a593Smuzhiyun /**
1871*4882a593Smuzhiyun  *  e1000e_get_phy_info_m88 - Retrieve PHY information
1872*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1873*4882a593Smuzhiyun  *
1874*4882a593Smuzhiyun  *  Valid for only copper links.  Read the PHY status register (sticky read)
1875*4882a593Smuzhiyun  *  to verify that link is up.  Read the PHY special control register to
1876*4882a593Smuzhiyun  *  determine the polarity and 10base-T extended distance.  Read the PHY
1877*4882a593Smuzhiyun  *  special status register to determine MDI/MDIx and current speed.  If
1878*4882a593Smuzhiyun  *  speed is 1000, then determine cable length, local and remote receiver.
1879*4882a593Smuzhiyun  **/
e1000e_get_phy_info_m88(struct e1000_hw * hw)1880*4882a593Smuzhiyun s32 e1000e_get_phy_info_m88(struct e1000_hw *hw)
1881*4882a593Smuzhiyun {
1882*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1883*4882a593Smuzhiyun 	s32 ret_val;
1884*4882a593Smuzhiyun 	u16 phy_data;
1885*4882a593Smuzhiyun 	bool link;
1886*4882a593Smuzhiyun 
1887*4882a593Smuzhiyun 	if (phy->media_type != e1000_media_type_copper) {
1888*4882a593Smuzhiyun 		e_dbg("Phy info is only valid for copper media\n");
1889*4882a593Smuzhiyun 		return -E1000_ERR_CONFIG;
1890*4882a593Smuzhiyun 	}
1891*4882a593Smuzhiyun 
1892*4882a593Smuzhiyun 	ret_val = e1000e_phy_has_link_generic(hw, 1, 0, &link);
1893*4882a593Smuzhiyun 	if (ret_val)
1894*4882a593Smuzhiyun 		return ret_val;
1895*4882a593Smuzhiyun 
1896*4882a593Smuzhiyun 	if (!link) {
1897*4882a593Smuzhiyun 		e_dbg("Phy info is only valid if link is up\n");
1898*4882a593Smuzhiyun 		return -E1000_ERR_CONFIG;
1899*4882a593Smuzhiyun 	}
1900*4882a593Smuzhiyun 
1901*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1902*4882a593Smuzhiyun 	if (ret_val)
1903*4882a593Smuzhiyun 		return ret_val;
1904*4882a593Smuzhiyun 
1905*4882a593Smuzhiyun 	phy->polarity_correction = !!(phy_data &
1906*4882a593Smuzhiyun 				      M88E1000_PSCR_POLARITY_REVERSAL);
1907*4882a593Smuzhiyun 
1908*4882a593Smuzhiyun 	ret_val = e1000_check_polarity_m88(hw);
1909*4882a593Smuzhiyun 	if (ret_val)
1910*4882a593Smuzhiyun 		return ret_val;
1911*4882a593Smuzhiyun 
1912*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
1913*4882a593Smuzhiyun 	if (ret_val)
1914*4882a593Smuzhiyun 		return ret_val;
1915*4882a593Smuzhiyun 
1916*4882a593Smuzhiyun 	phy->is_mdix = !!(phy_data & M88E1000_PSSR_MDIX);
1917*4882a593Smuzhiyun 
1918*4882a593Smuzhiyun 	if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS) {
1919*4882a593Smuzhiyun 		ret_val = hw->phy.ops.get_cable_length(hw);
1920*4882a593Smuzhiyun 		if (ret_val)
1921*4882a593Smuzhiyun 			return ret_val;
1922*4882a593Smuzhiyun 
1923*4882a593Smuzhiyun 		ret_val = e1e_rphy(hw, MII_STAT1000, &phy_data);
1924*4882a593Smuzhiyun 		if (ret_val)
1925*4882a593Smuzhiyun 			return ret_val;
1926*4882a593Smuzhiyun 
1927*4882a593Smuzhiyun 		phy->local_rx = (phy_data & LPA_1000LOCALRXOK)
1928*4882a593Smuzhiyun 		    ? e1000_1000t_rx_status_ok : e1000_1000t_rx_status_not_ok;
1929*4882a593Smuzhiyun 
1930*4882a593Smuzhiyun 		phy->remote_rx = (phy_data & LPA_1000REMRXOK)
1931*4882a593Smuzhiyun 		    ? e1000_1000t_rx_status_ok : e1000_1000t_rx_status_not_ok;
1932*4882a593Smuzhiyun 	} else {
1933*4882a593Smuzhiyun 		/* Set values to "undefined" */
1934*4882a593Smuzhiyun 		phy->cable_length = E1000_CABLE_LENGTH_UNDEFINED;
1935*4882a593Smuzhiyun 		phy->local_rx = e1000_1000t_rx_status_undefined;
1936*4882a593Smuzhiyun 		phy->remote_rx = e1000_1000t_rx_status_undefined;
1937*4882a593Smuzhiyun 	}
1938*4882a593Smuzhiyun 
1939*4882a593Smuzhiyun 	return ret_val;
1940*4882a593Smuzhiyun }
1941*4882a593Smuzhiyun 
1942*4882a593Smuzhiyun /**
1943*4882a593Smuzhiyun  *  e1000e_get_phy_info_igp - Retrieve igp PHY information
1944*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
1945*4882a593Smuzhiyun  *
1946*4882a593Smuzhiyun  *  Read PHY status to determine if link is up.  If link is up, then
1947*4882a593Smuzhiyun  *  set/determine 10base-T extended distance and polarity correction.  Read
1948*4882a593Smuzhiyun  *  PHY port status to determine MDI/MDIx and speed.  Based on the speed,
1949*4882a593Smuzhiyun  *  determine on the cable length, local and remote receiver.
1950*4882a593Smuzhiyun  **/
e1000e_get_phy_info_igp(struct e1000_hw * hw)1951*4882a593Smuzhiyun s32 e1000e_get_phy_info_igp(struct e1000_hw *hw)
1952*4882a593Smuzhiyun {
1953*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
1954*4882a593Smuzhiyun 	s32 ret_val;
1955*4882a593Smuzhiyun 	u16 data;
1956*4882a593Smuzhiyun 	bool link;
1957*4882a593Smuzhiyun 
1958*4882a593Smuzhiyun 	ret_val = e1000e_phy_has_link_generic(hw, 1, 0, &link);
1959*4882a593Smuzhiyun 	if (ret_val)
1960*4882a593Smuzhiyun 		return ret_val;
1961*4882a593Smuzhiyun 
1962*4882a593Smuzhiyun 	if (!link) {
1963*4882a593Smuzhiyun 		e_dbg("Phy info is only valid if link is up\n");
1964*4882a593Smuzhiyun 		return -E1000_ERR_CONFIG;
1965*4882a593Smuzhiyun 	}
1966*4882a593Smuzhiyun 
1967*4882a593Smuzhiyun 	phy->polarity_correction = true;
1968*4882a593Smuzhiyun 
1969*4882a593Smuzhiyun 	ret_val = e1000_check_polarity_igp(hw);
1970*4882a593Smuzhiyun 	if (ret_val)
1971*4882a593Smuzhiyun 		return ret_val;
1972*4882a593Smuzhiyun 
1973*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, IGP01E1000_PHY_PORT_STATUS, &data);
1974*4882a593Smuzhiyun 	if (ret_val)
1975*4882a593Smuzhiyun 		return ret_val;
1976*4882a593Smuzhiyun 
1977*4882a593Smuzhiyun 	phy->is_mdix = !!(data & IGP01E1000_PSSR_MDIX);
1978*4882a593Smuzhiyun 
1979*4882a593Smuzhiyun 	if ((data & IGP01E1000_PSSR_SPEED_MASK) ==
1980*4882a593Smuzhiyun 	    IGP01E1000_PSSR_SPEED_1000MBPS) {
1981*4882a593Smuzhiyun 		ret_val = phy->ops.get_cable_length(hw);
1982*4882a593Smuzhiyun 		if (ret_val)
1983*4882a593Smuzhiyun 			return ret_val;
1984*4882a593Smuzhiyun 
1985*4882a593Smuzhiyun 		ret_val = e1e_rphy(hw, MII_STAT1000, &data);
1986*4882a593Smuzhiyun 		if (ret_val)
1987*4882a593Smuzhiyun 			return ret_val;
1988*4882a593Smuzhiyun 
1989*4882a593Smuzhiyun 		phy->local_rx = (data & LPA_1000LOCALRXOK)
1990*4882a593Smuzhiyun 		    ? e1000_1000t_rx_status_ok : e1000_1000t_rx_status_not_ok;
1991*4882a593Smuzhiyun 
1992*4882a593Smuzhiyun 		phy->remote_rx = (data & LPA_1000REMRXOK)
1993*4882a593Smuzhiyun 		    ? e1000_1000t_rx_status_ok : e1000_1000t_rx_status_not_ok;
1994*4882a593Smuzhiyun 	} else {
1995*4882a593Smuzhiyun 		phy->cable_length = E1000_CABLE_LENGTH_UNDEFINED;
1996*4882a593Smuzhiyun 		phy->local_rx = e1000_1000t_rx_status_undefined;
1997*4882a593Smuzhiyun 		phy->remote_rx = e1000_1000t_rx_status_undefined;
1998*4882a593Smuzhiyun 	}
1999*4882a593Smuzhiyun 
2000*4882a593Smuzhiyun 	return ret_val;
2001*4882a593Smuzhiyun }
2002*4882a593Smuzhiyun 
2003*4882a593Smuzhiyun /**
2004*4882a593Smuzhiyun  *  e1000_get_phy_info_ife - Retrieves various IFE PHY states
2005*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2006*4882a593Smuzhiyun  *
2007*4882a593Smuzhiyun  *  Populates "phy" structure with various feature states.
2008*4882a593Smuzhiyun  **/
e1000_get_phy_info_ife(struct e1000_hw * hw)2009*4882a593Smuzhiyun s32 e1000_get_phy_info_ife(struct e1000_hw *hw)
2010*4882a593Smuzhiyun {
2011*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
2012*4882a593Smuzhiyun 	s32 ret_val;
2013*4882a593Smuzhiyun 	u16 data;
2014*4882a593Smuzhiyun 	bool link;
2015*4882a593Smuzhiyun 
2016*4882a593Smuzhiyun 	ret_val = e1000e_phy_has_link_generic(hw, 1, 0, &link);
2017*4882a593Smuzhiyun 	if (ret_val)
2018*4882a593Smuzhiyun 		return ret_val;
2019*4882a593Smuzhiyun 
2020*4882a593Smuzhiyun 	if (!link) {
2021*4882a593Smuzhiyun 		e_dbg("Phy info is only valid if link is up\n");
2022*4882a593Smuzhiyun 		return -E1000_ERR_CONFIG;
2023*4882a593Smuzhiyun 	}
2024*4882a593Smuzhiyun 
2025*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, IFE_PHY_SPECIAL_CONTROL, &data);
2026*4882a593Smuzhiyun 	if (ret_val)
2027*4882a593Smuzhiyun 		return ret_val;
2028*4882a593Smuzhiyun 	phy->polarity_correction = !(data & IFE_PSC_AUTO_POLARITY_DISABLE);
2029*4882a593Smuzhiyun 
2030*4882a593Smuzhiyun 	if (phy->polarity_correction) {
2031*4882a593Smuzhiyun 		ret_val = e1000_check_polarity_ife(hw);
2032*4882a593Smuzhiyun 		if (ret_val)
2033*4882a593Smuzhiyun 			return ret_val;
2034*4882a593Smuzhiyun 	} else {
2035*4882a593Smuzhiyun 		/* Polarity is forced */
2036*4882a593Smuzhiyun 		phy->cable_polarity = ((data & IFE_PSC_FORCE_POLARITY)
2037*4882a593Smuzhiyun 				       ? e1000_rev_polarity_reversed
2038*4882a593Smuzhiyun 				       : e1000_rev_polarity_normal);
2039*4882a593Smuzhiyun 	}
2040*4882a593Smuzhiyun 
2041*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, IFE_PHY_MDIX_CONTROL, &data);
2042*4882a593Smuzhiyun 	if (ret_val)
2043*4882a593Smuzhiyun 		return ret_val;
2044*4882a593Smuzhiyun 
2045*4882a593Smuzhiyun 	phy->is_mdix = !!(data & IFE_PMC_MDIX_STATUS);
2046*4882a593Smuzhiyun 
2047*4882a593Smuzhiyun 	/* The following parameters are undefined for 10/100 operation. */
2048*4882a593Smuzhiyun 	phy->cable_length = E1000_CABLE_LENGTH_UNDEFINED;
2049*4882a593Smuzhiyun 	phy->local_rx = e1000_1000t_rx_status_undefined;
2050*4882a593Smuzhiyun 	phy->remote_rx = e1000_1000t_rx_status_undefined;
2051*4882a593Smuzhiyun 
2052*4882a593Smuzhiyun 	return 0;
2053*4882a593Smuzhiyun }
2054*4882a593Smuzhiyun 
2055*4882a593Smuzhiyun /**
2056*4882a593Smuzhiyun  *  e1000e_phy_sw_reset - PHY software reset
2057*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2058*4882a593Smuzhiyun  *
2059*4882a593Smuzhiyun  *  Does a software reset of the PHY by reading the PHY control register and
2060*4882a593Smuzhiyun  *  setting/write the control register reset bit to the PHY.
2061*4882a593Smuzhiyun  **/
e1000e_phy_sw_reset(struct e1000_hw * hw)2062*4882a593Smuzhiyun s32 e1000e_phy_sw_reset(struct e1000_hw *hw)
2063*4882a593Smuzhiyun {
2064*4882a593Smuzhiyun 	s32 ret_val;
2065*4882a593Smuzhiyun 	u16 phy_ctrl;
2066*4882a593Smuzhiyun 
2067*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, MII_BMCR, &phy_ctrl);
2068*4882a593Smuzhiyun 	if (ret_val)
2069*4882a593Smuzhiyun 		return ret_val;
2070*4882a593Smuzhiyun 
2071*4882a593Smuzhiyun 	phy_ctrl |= BMCR_RESET;
2072*4882a593Smuzhiyun 	ret_val = e1e_wphy(hw, MII_BMCR, phy_ctrl);
2073*4882a593Smuzhiyun 	if (ret_val)
2074*4882a593Smuzhiyun 		return ret_val;
2075*4882a593Smuzhiyun 
2076*4882a593Smuzhiyun 	udelay(1);
2077*4882a593Smuzhiyun 
2078*4882a593Smuzhiyun 	return ret_val;
2079*4882a593Smuzhiyun }
2080*4882a593Smuzhiyun 
2081*4882a593Smuzhiyun /**
2082*4882a593Smuzhiyun  *  e1000e_phy_hw_reset_generic - PHY hardware reset
2083*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2084*4882a593Smuzhiyun  *
2085*4882a593Smuzhiyun  *  Verify the reset block is not blocking us from resetting.  Acquire
2086*4882a593Smuzhiyun  *  semaphore (if necessary) and read/set/write the device control reset
2087*4882a593Smuzhiyun  *  bit in the PHY.  Wait the appropriate delay time for the device to
2088*4882a593Smuzhiyun  *  reset and release the semaphore (if necessary).
2089*4882a593Smuzhiyun  **/
e1000e_phy_hw_reset_generic(struct e1000_hw * hw)2090*4882a593Smuzhiyun s32 e1000e_phy_hw_reset_generic(struct e1000_hw *hw)
2091*4882a593Smuzhiyun {
2092*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
2093*4882a593Smuzhiyun 	s32 ret_val;
2094*4882a593Smuzhiyun 	u32 ctrl;
2095*4882a593Smuzhiyun 
2096*4882a593Smuzhiyun 	if (phy->ops.check_reset_block) {
2097*4882a593Smuzhiyun 		ret_val = phy->ops.check_reset_block(hw);
2098*4882a593Smuzhiyun 		if (ret_val)
2099*4882a593Smuzhiyun 			return 0;
2100*4882a593Smuzhiyun 	}
2101*4882a593Smuzhiyun 
2102*4882a593Smuzhiyun 	ret_val = phy->ops.acquire(hw);
2103*4882a593Smuzhiyun 	if (ret_val)
2104*4882a593Smuzhiyun 		return ret_val;
2105*4882a593Smuzhiyun 
2106*4882a593Smuzhiyun 	ctrl = er32(CTRL);
2107*4882a593Smuzhiyun 	ew32(CTRL, ctrl | E1000_CTRL_PHY_RST);
2108*4882a593Smuzhiyun 	e1e_flush();
2109*4882a593Smuzhiyun 
2110*4882a593Smuzhiyun 	udelay(phy->reset_delay_us);
2111*4882a593Smuzhiyun 
2112*4882a593Smuzhiyun 	ew32(CTRL, ctrl);
2113*4882a593Smuzhiyun 	e1e_flush();
2114*4882a593Smuzhiyun 
2115*4882a593Smuzhiyun 	usleep_range(150, 300);
2116*4882a593Smuzhiyun 
2117*4882a593Smuzhiyun 	phy->ops.release(hw);
2118*4882a593Smuzhiyun 
2119*4882a593Smuzhiyun 	return phy->ops.get_cfg_done(hw);
2120*4882a593Smuzhiyun }
2121*4882a593Smuzhiyun 
2122*4882a593Smuzhiyun /**
2123*4882a593Smuzhiyun  *  e1000e_get_cfg_done_generic - Generic configuration done
2124*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2125*4882a593Smuzhiyun  *
2126*4882a593Smuzhiyun  *  Generic function to wait 10 milli-seconds for configuration to complete
2127*4882a593Smuzhiyun  *  and return success.
2128*4882a593Smuzhiyun  **/
e1000e_get_cfg_done_generic(struct e1000_hw __always_unused * hw)2129*4882a593Smuzhiyun s32 e1000e_get_cfg_done_generic(struct e1000_hw __always_unused *hw)
2130*4882a593Smuzhiyun {
2131*4882a593Smuzhiyun 	mdelay(10);
2132*4882a593Smuzhiyun 
2133*4882a593Smuzhiyun 	return 0;
2134*4882a593Smuzhiyun }
2135*4882a593Smuzhiyun 
2136*4882a593Smuzhiyun /**
2137*4882a593Smuzhiyun  *  e1000e_phy_init_script_igp3 - Inits the IGP3 PHY
2138*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2139*4882a593Smuzhiyun  *
2140*4882a593Smuzhiyun  *  Initializes a Intel Gigabit PHY3 when an EEPROM is not present.
2141*4882a593Smuzhiyun  **/
e1000e_phy_init_script_igp3(struct e1000_hw * hw)2142*4882a593Smuzhiyun s32 e1000e_phy_init_script_igp3(struct e1000_hw *hw)
2143*4882a593Smuzhiyun {
2144*4882a593Smuzhiyun 	e_dbg("Running IGP 3 PHY init script\n");
2145*4882a593Smuzhiyun 
2146*4882a593Smuzhiyun 	/* PHY init IGP 3 */
2147*4882a593Smuzhiyun 	/* Enable rise/fall, 10-mode work in class-A */
2148*4882a593Smuzhiyun 	e1e_wphy(hw, 0x2F5B, 0x9018);
2149*4882a593Smuzhiyun 	/* Remove all caps from Replica path filter */
2150*4882a593Smuzhiyun 	e1e_wphy(hw, 0x2F52, 0x0000);
2151*4882a593Smuzhiyun 	/* Bias trimming for ADC, AFE and Driver (Default) */
2152*4882a593Smuzhiyun 	e1e_wphy(hw, 0x2FB1, 0x8B24);
2153*4882a593Smuzhiyun 	/* Increase Hybrid poly bias */
2154*4882a593Smuzhiyun 	e1e_wphy(hw, 0x2FB2, 0xF8F0);
2155*4882a593Smuzhiyun 	/* Add 4% to Tx amplitude in Gig mode */
2156*4882a593Smuzhiyun 	e1e_wphy(hw, 0x2010, 0x10B0);
2157*4882a593Smuzhiyun 	/* Disable trimming (TTT) */
2158*4882a593Smuzhiyun 	e1e_wphy(hw, 0x2011, 0x0000);
2159*4882a593Smuzhiyun 	/* Poly DC correction to 94.6% + 2% for all channels */
2160*4882a593Smuzhiyun 	e1e_wphy(hw, 0x20DD, 0x249A);
2161*4882a593Smuzhiyun 	/* ABS DC correction to 95.9% */
2162*4882a593Smuzhiyun 	e1e_wphy(hw, 0x20DE, 0x00D3);
2163*4882a593Smuzhiyun 	/* BG temp curve trim */
2164*4882a593Smuzhiyun 	e1e_wphy(hw, 0x28B4, 0x04CE);
2165*4882a593Smuzhiyun 	/* Increasing ADC OPAMP stage 1 currents to max */
2166*4882a593Smuzhiyun 	e1e_wphy(hw, 0x2F70, 0x29E4);
2167*4882a593Smuzhiyun 	/* Force 1000 ( required for enabling PHY regs configuration) */
2168*4882a593Smuzhiyun 	e1e_wphy(hw, 0x0000, 0x0140);
2169*4882a593Smuzhiyun 	/* Set upd_freq to 6 */
2170*4882a593Smuzhiyun 	e1e_wphy(hw, 0x1F30, 0x1606);
2171*4882a593Smuzhiyun 	/* Disable NPDFE */
2172*4882a593Smuzhiyun 	e1e_wphy(hw, 0x1F31, 0xB814);
2173*4882a593Smuzhiyun 	/* Disable adaptive fixed FFE (Default) */
2174*4882a593Smuzhiyun 	e1e_wphy(hw, 0x1F35, 0x002A);
2175*4882a593Smuzhiyun 	/* Enable FFE hysteresis */
2176*4882a593Smuzhiyun 	e1e_wphy(hw, 0x1F3E, 0x0067);
2177*4882a593Smuzhiyun 	/* Fixed FFE for short cable lengths */
2178*4882a593Smuzhiyun 	e1e_wphy(hw, 0x1F54, 0x0065);
2179*4882a593Smuzhiyun 	/* Fixed FFE for medium cable lengths */
2180*4882a593Smuzhiyun 	e1e_wphy(hw, 0x1F55, 0x002A);
2181*4882a593Smuzhiyun 	/* Fixed FFE for long cable lengths */
2182*4882a593Smuzhiyun 	e1e_wphy(hw, 0x1F56, 0x002A);
2183*4882a593Smuzhiyun 	/* Enable Adaptive Clip Threshold */
2184*4882a593Smuzhiyun 	e1e_wphy(hw, 0x1F72, 0x3FB0);
2185*4882a593Smuzhiyun 	/* AHT reset limit to 1 */
2186*4882a593Smuzhiyun 	e1e_wphy(hw, 0x1F76, 0xC0FF);
2187*4882a593Smuzhiyun 	/* Set AHT master delay to 127 msec */
2188*4882a593Smuzhiyun 	e1e_wphy(hw, 0x1F77, 0x1DEC);
2189*4882a593Smuzhiyun 	/* Set scan bits for AHT */
2190*4882a593Smuzhiyun 	e1e_wphy(hw, 0x1F78, 0xF9EF);
2191*4882a593Smuzhiyun 	/* Set AHT Preset bits */
2192*4882a593Smuzhiyun 	e1e_wphy(hw, 0x1F79, 0x0210);
2193*4882a593Smuzhiyun 	/* Change integ_factor of channel A to 3 */
2194*4882a593Smuzhiyun 	e1e_wphy(hw, 0x1895, 0x0003);
2195*4882a593Smuzhiyun 	/* Change prop_factor of channels BCD to 8 */
2196*4882a593Smuzhiyun 	e1e_wphy(hw, 0x1796, 0x0008);
2197*4882a593Smuzhiyun 	/* Change cg_icount + enable integbp for channels BCD */
2198*4882a593Smuzhiyun 	e1e_wphy(hw, 0x1798, 0xD008);
2199*4882a593Smuzhiyun 	/* Change cg_icount + enable integbp + change prop_factor_master
2200*4882a593Smuzhiyun 	 * to 8 for channel A
2201*4882a593Smuzhiyun 	 */
2202*4882a593Smuzhiyun 	e1e_wphy(hw, 0x1898, 0xD918);
2203*4882a593Smuzhiyun 	/* Disable AHT in Slave mode on channel A */
2204*4882a593Smuzhiyun 	e1e_wphy(hw, 0x187A, 0x0800);
2205*4882a593Smuzhiyun 	/* Enable LPLU and disable AN to 1000 in non-D0a states,
2206*4882a593Smuzhiyun 	 * Enable SPD+B2B
2207*4882a593Smuzhiyun 	 */
2208*4882a593Smuzhiyun 	e1e_wphy(hw, 0x0019, 0x008D);
2209*4882a593Smuzhiyun 	/* Enable restart AN on an1000_dis change */
2210*4882a593Smuzhiyun 	e1e_wphy(hw, 0x001B, 0x2080);
2211*4882a593Smuzhiyun 	/* Enable wh_fifo read clock in 10/100 modes */
2212*4882a593Smuzhiyun 	e1e_wphy(hw, 0x0014, 0x0045);
2213*4882a593Smuzhiyun 	/* Restart AN, Speed selection is 1000 */
2214*4882a593Smuzhiyun 	e1e_wphy(hw, 0x0000, 0x1340);
2215*4882a593Smuzhiyun 
2216*4882a593Smuzhiyun 	return 0;
2217*4882a593Smuzhiyun }
2218*4882a593Smuzhiyun 
2219*4882a593Smuzhiyun /**
2220*4882a593Smuzhiyun  *  e1000e_get_phy_type_from_id - Get PHY type from id
2221*4882a593Smuzhiyun  *  @phy_id: phy_id read from the phy
2222*4882a593Smuzhiyun  *
2223*4882a593Smuzhiyun  *  Returns the phy type from the id.
2224*4882a593Smuzhiyun  **/
e1000e_get_phy_type_from_id(u32 phy_id)2225*4882a593Smuzhiyun enum e1000_phy_type e1000e_get_phy_type_from_id(u32 phy_id)
2226*4882a593Smuzhiyun {
2227*4882a593Smuzhiyun 	enum e1000_phy_type phy_type = e1000_phy_unknown;
2228*4882a593Smuzhiyun 
2229*4882a593Smuzhiyun 	switch (phy_id) {
2230*4882a593Smuzhiyun 	case M88E1000_I_PHY_ID:
2231*4882a593Smuzhiyun 	case M88E1000_E_PHY_ID:
2232*4882a593Smuzhiyun 	case M88E1111_I_PHY_ID:
2233*4882a593Smuzhiyun 	case M88E1011_I_PHY_ID:
2234*4882a593Smuzhiyun 		phy_type = e1000_phy_m88;
2235*4882a593Smuzhiyun 		break;
2236*4882a593Smuzhiyun 	case IGP01E1000_I_PHY_ID:	/* IGP 1 & 2 share this */
2237*4882a593Smuzhiyun 		phy_type = e1000_phy_igp_2;
2238*4882a593Smuzhiyun 		break;
2239*4882a593Smuzhiyun 	case GG82563_E_PHY_ID:
2240*4882a593Smuzhiyun 		phy_type = e1000_phy_gg82563;
2241*4882a593Smuzhiyun 		break;
2242*4882a593Smuzhiyun 	case IGP03E1000_E_PHY_ID:
2243*4882a593Smuzhiyun 		phy_type = e1000_phy_igp_3;
2244*4882a593Smuzhiyun 		break;
2245*4882a593Smuzhiyun 	case IFE_E_PHY_ID:
2246*4882a593Smuzhiyun 	case IFE_PLUS_E_PHY_ID:
2247*4882a593Smuzhiyun 	case IFE_C_E_PHY_ID:
2248*4882a593Smuzhiyun 		phy_type = e1000_phy_ife;
2249*4882a593Smuzhiyun 		break;
2250*4882a593Smuzhiyun 	case BME1000_E_PHY_ID:
2251*4882a593Smuzhiyun 	case BME1000_E_PHY_ID_R2:
2252*4882a593Smuzhiyun 		phy_type = e1000_phy_bm;
2253*4882a593Smuzhiyun 		break;
2254*4882a593Smuzhiyun 	case I82578_E_PHY_ID:
2255*4882a593Smuzhiyun 		phy_type = e1000_phy_82578;
2256*4882a593Smuzhiyun 		break;
2257*4882a593Smuzhiyun 	case I82577_E_PHY_ID:
2258*4882a593Smuzhiyun 		phy_type = e1000_phy_82577;
2259*4882a593Smuzhiyun 		break;
2260*4882a593Smuzhiyun 	case I82579_E_PHY_ID:
2261*4882a593Smuzhiyun 		phy_type = e1000_phy_82579;
2262*4882a593Smuzhiyun 		break;
2263*4882a593Smuzhiyun 	case I217_E_PHY_ID:
2264*4882a593Smuzhiyun 		phy_type = e1000_phy_i217;
2265*4882a593Smuzhiyun 		break;
2266*4882a593Smuzhiyun 	default:
2267*4882a593Smuzhiyun 		phy_type = e1000_phy_unknown;
2268*4882a593Smuzhiyun 		break;
2269*4882a593Smuzhiyun 	}
2270*4882a593Smuzhiyun 	return phy_type;
2271*4882a593Smuzhiyun }
2272*4882a593Smuzhiyun 
2273*4882a593Smuzhiyun /**
2274*4882a593Smuzhiyun  *  e1000e_determine_phy_address - Determines PHY address.
2275*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2276*4882a593Smuzhiyun  *
2277*4882a593Smuzhiyun  *  This uses a trial and error method to loop through possible PHY
2278*4882a593Smuzhiyun  *  addresses. It tests each by reading the PHY ID registers and
2279*4882a593Smuzhiyun  *  checking for a match.
2280*4882a593Smuzhiyun  **/
e1000e_determine_phy_address(struct e1000_hw * hw)2281*4882a593Smuzhiyun s32 e1000e_determine_phy_address(struct e1000_hw *hw)
2282*4882a593Smuzhiyun {
2283*4882a593Smuzhiyun 	u32 phy_addr = 0;
2284*4882a593Smuzhiyun 	u32 i;
2285*4882a593Smuzhiyun 	enum e1000_phy_type phy_type = e1000_phy_unknown;
2286*4882a593Smuzhiyun 
2287*4882a593Smuzhiyun 	hw->phy.id = phy_type;
2288*4882a593Smuzhiyun 
2289*4882a593Smuzhiyun 	for (phy_addr = 0; phy_addr < E1000_MAX_PHY_ADDR; phy_addr++) {
2290*4882a593Smuzhiyun 		hw->phy.addr = phy_addr;
2291*4882a593Smuzhiyun 		i = 0;
2292*4882a593Smuzhiyun 
2293*4882a593Smuzhiyun 		do {
2294*4882a593Smuzhiyun 			e1000e_get_phy_id(hw);
2295*4882a593Smuzhiyun 			phy_type = e1000e_get_phy_type_from_id(hw->phy.id);
2296*4882a593Smuzhiyun 
2297*4882a593Smuzhiyun 			/* If phy_type is valid, break - we found our
2298*4882a593Smuzhiyun 			 * PHY address
2299*4882a593Smuzhiyun 			 */
2300*4882a593Smuzhiyun 			if (phy_type != e1000_phy_unknown)
2301*4882a593Smuzhiyun 				return 0;
2302*4882a593Smuzhiyun 
2303*4882a593Smuzhiyun 			usleep_range(1000, 2000);
2304*4882a593Smuzhiyun 			i++;
2305*4882a593Smuzhiyun 		} while (i < 10);
2306*4882a593Smuzhiyun 	}
2307*4882a593Smuzhiyun 
2308*4882a593Smuzhiyun 	return -E1000_ERR_PHY_TYPE;
2309*4882a593Smuzhiyun }
2310*4882a593Smuzhiyun 
2311*4882a593Smuzhiyun /**
2312*4882a593Smuzhiyun  *  e1000_get_phy_addr_for_bm_page - Retrieve PHY page address
2313*4882a593Smuzhiyun  *  @page: page to access
2314*4882a593Smuzhiyun  *  @reg: register to check
2315*4882a593Smuzhiyun  *
2316*4882a593Smuzhiyun  *  Returns the phy address for the page requested.
2317*4882a593Smuzhiyun  **/
e1000_get_phy_addr_for_bm_page(u32 page,u32 reg)2318*4882a593Smuzhiyun static u32 e1000_get_phy_addr_for_bm_page(u32 page, u32 reg)
2319*4882a593Smuzhiyun {
2320*4882a593Smuzhiyun 	u32 phy_addr = 2;
2321*4882a593Smuzhiyun 
2322*4882a593Smuzhiyun 	if ((page >= 768) || (page == 0 && reg == 25) || (reg == 31))
2323*4882a593Smuzhiyun 		phy_addr = 1;
2324*4882a593Smuzhiyun 
2325*4882a593Smuzhiyun 	return phy_addr;
2326*4882a593Smuzhiyun }
2327*4882a593Smuzhiyun 
2328*4882a593Smuzhiyun /**
2329*4882a593Smuzhiyun  *  e1000e_write_phy_reg_bm - Write BM PHY register
2330*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2331*4882a593Smuzhiyun  *  @offset: register offset to write to
2332*4882a593Smuzhiyun  *  @data: data to write at register offset
2333*4882a593Smuzhiyun  *
2334*4882a593Smuzhiyun  *  Acquires semaphore, if necessary, then writes the data to PHY register
2335*4882a593Smuzhiyun  *  at the offset.  Release any acquired semaphores before exiting.
2336*4882a593Smuzhiyun  **/
e1000e_write_phy_reg_bm(struct e1000_hw * hw,u32 offset,u16 data)2337*4882a593Smuzhiyun s32 e1000e_write_phy_reg_bm(struct e1000_hw *hw, u32 offset, u16 data)
2338*4882a593Smuzhiyun {
2339*4882a593Smuzhiyun 	s32 ret_val;
2340*4882a593Smuzhiyun 	u32 page = offset >> IGP_PAGE_SHIFT;
2341*4882a593Smuzhiyun 
2342*4882a593Smuzhiyun 	ret_val = hw->phy.ops.acquire(hw);
2343*4882a593Smuzhiyun 	if (ret_val)
2344*4882a593Smuzhiyun 		return ret_val;
2345*4882a593Smuzhiyun 
2346*4882a593Smuzhiyun 	/* Page 800 works differently than the rest so it has its own func */
2347*4882a593Smuzhiyun 	if (page == BM_WUC_PAGE) {
2348*4882a593Smuzhiyun 		ret_val = e1000_access_phy_wakeup_reg_bm(hw, offset, &data,
2349*4882a593Smuzhiyun 							 false, false);
2350*4882a593Smuzhiyun 		goto release;
2351*4882a593Smuzhiyun 	}
2352*4882a593Smuzhiyun 
2353*4882a593Smuzhiyun 	hw->phy.addr = e1000_get_phy_addr_for_bm_page(page, offset);
2354*4882a593Smuzhiyun 
2355*4882a593Smuzhiyun 	if (offset > MAX_PHY_MULTI_PAGE_REG) {
2356*4882a593Smuzhiyun 		u32 page_shift, page_select;
2357*4882a593Smuzhiyun 
2358*4882a593Smuzhiyun 		/* Page select is register 31 for phy address 1 and 22 for
2359*4882a593Smuzhiyun 		 * phy address 2 and 3. Page select is shifted only for
2360*4882a593Smuzhiyun 		 * phy address 1.
2361*4882a593Smuzhiyun 		 */
2362*4882a593Smuzhiyun 		if (hw->phy.addr == 1) {
2363*4882a593Smuzhiyun 			page_shift = IGP_PAGE_SHIFT;
2364*4882a593Smuzhiyun 			page_select = IGP01E1000_PHY_PAGE_SELECT;
2365*4882a593Smuzhiyun 		} else {
2366*4882a593Smuzhiyun 			page_shift = 0;
2367*4882a593Smuzhiyun 			page_select = BM_PHY_PAGE_SELECT;
2368*4882a593Smuzhiyun 		}
2369*4882a593Smuzhiyun 
2370*4882a593Smuzhiyun 		/* Page is shifted left, PHY expects (page x 32) */
2371*4882a593Smuzhiyun 		ret_val = e1000e_write_phy_reg_mdic(hw, page_select,
2372*4882a593Smuzhiyun 						    (page << page_shift));
2373*4882a593Smuzhiyun 		if (ret_val)
2374*4882a593Smuzhiyun 			goto release;
2375*4882a593Smuzhiyun 	}
2376*4882a593Smuzhiyun 
2377*4882a593Smuzhiyun 	ret_val = e1000e_write_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & offset,
2378*4882a593Smuzhiyun 					    data);
2379*4882a593Smuzhiyun 
2380*4882a593Smuzhiyun release:
2381*4882a593Smuzhiyun 	hw->phy.ops.release(hw);
2382*4882a593Smuzhiyun 	return ret_val;
2383*4882a593Smuzhiyun }
2384*4882a593Smuzhiyun 
2385*4882a593Smuzhiyun /**
2386*4882a593Smuzhiyun  *  e1000e_read_phy_reg_bm - Read BM PHY register
2387*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2388*4882a593Smuzhiyun  *  @offset: register offset to be read
2389*4882a593Smuzhiyun  *  @data: pointer to the read data
2390*4882a593Smuzhiyun  *
2391*4882a593Smuzhiyun  *  Acquires semaphore, if necessary, then reads the PHY register at offset
2392*4882a593Smuzhiyun  *  and storing the retrieved information in data.  Release any acquired
2393*4882a593Smuzhiyun  *  semaphores before exiting.
2394*4882a593Smuzhiyun  **/
e1000e_read_phy_reg_bm(struct e1000_hw * hw,u32 offset,u16 * data)2395*4882a593Smuzhiyun s32 e1000e_read_phy_reg_bm(struct e1000_hw *hw, u32 offset, u16 *data)
2396*4882a593Smuzhiyun {
2397*4882a593Smuzhiyun 	s32 ret_val;
2398*4882a593Smuzhiyun 	u32 page = offset >> IGP_PAGE_SHIFT;
2399*4882a593Smuzhiyun 
2400*4882a593Smuzhiyun 	ret_val = hw->phy.ops.acquire(hw);
2401*4882a593Smuzhiyun 	if (ret_val)
2402*4882a593Smuzhiyun 		return ret_val;
2403*4882a593Smuzhiyun 
2404*4882a593Smuzhiyun 	/* Page 800 works differently than the rest so it has its own func */
2405*4882a593Smuzhiyun 	if (page == BM_WUC_PAGE) {
2406*4882a593Smuzhiyun 		ret_val = e1000_access_phy_wakeup_reg_bm(hw, offset, data,
2407*4882a593Smuzhiyun 							 true, false);
2408*4882a593Smuzhiyun 		goto release;
2409*4882a593Smuzhiyun 	}
2410*4882a593Smuzhiyun 
2411*4882a593Smuzhiyun 	hw->phy.addr = e1000_get_phy_addr_for_bm_page(page, offset);
2412*4882a593Smuzhiyun 
2413*4882a593Smuzhiyun 	if (offset > MAX_PHY_MULTI_PAGE_REG) {
2414*4882a593Smuzhiyun 		u32 page_shift, page_select;
2415*4882a593Smuzhiyun 
2416*4882a593Smuzhiyun 		/* Page select is register 31 for phy address 1 and 22 for
2417*4882a593Smuzhiyun 		 * phy address 2 and 3. Page select is shifted only for
2418*4882a593Smuzhiyun 		 * phy address 1.
2419*4882a593Smuzhiyun 		 */
2420*4882a593Smuzhiyun 		if (hw->phy.addr == 1) {
2421*4882a593Smuzhiyun 			page_shift = IGP_PAGE_SHIFT;
2422*4882a593Smuzhiyun 			page_select = IGP01E1000_PHY_PAGE_SELECT;
2423*4882a593Smuzhiyun 		} else {
2424*4882a593Smuzhiyun 			page_shift = 0;
2425*4882a593Smuzhiyun 			page_select = BM_PHY_PAGE_SELECT;
2426*4882a593Smuzhiyun 		}
2427*4882a593Smuzhiyun 
2428*4882a593Smuzhiyun 		/* Page is shifted left, PHY expects (page x 32) */
2429*4882a593Smuzhiyun 		ret_val = e1000e_write_phy_reg_mdic(hw, page_select,
2430*4882a593Smuzhiyun 						    (page << page_shift));
2431*4882a593Smuzhiyun 		if (ret_val)
2432*4882a593Smuzhiyun 			goto release;
2433*4882a593Smuzhiyun 	}
2434*4882a593Smuzhiyun 
2435*4882a593Smuzhiyun 	ret_val = e1000e_read_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & offset,
2436*4882a593Smuzhiyun 					   data);
2437*4882a593Smuzhiyun release:
2438*4882a593Smuzhiyun 	hw->phy.ops.release(hw);
2439*4882a593Smuzhiyun 	return ret_val;
2440*4882a593Smuzhiyun }
2441*4882a593Smuzhiyun 
2442*4882a593Smuzhiyun /**
2443*4882a593Smuzhiyun  *  e1000e_read_phy_reg_bm2 - Read BM PHY register
2444*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2445*4882a593Smuzhiyun  *  @offset: register offset to be read
2446*4882a593Smuzhiyun  *  @data: pointer to the read data
2447*4882a593Smuzhiyun  *
2448*4882a593Smuzhiyun  *  Acquires semaphore, if necessary, then reads the PHY register at offset
2449*4882a593Smuzhiyun  *  and storing the retrieved information in data.  Release any acquired
2450*4882a593Smuzhiyun  *  semaphores before exiting.
2451*4882a593Smuzhiyun  **/
e1000e_read_phy_reg_bm2(struct e1000_hw * hw,u32 offset,u16 * data)2452*4882a593Smuzhiyun s32 e1000e_read_phy_reg_bm2(struct e1000_hw *hw, u32 offset, u16 *data)
2453*4882a593Smuzhiyun {
2454*4882a593Smuzhiyun 	s32 ret_val;
2455*4882a593Smuzhiyun 	u16 page = (u16)(offset >> IGP_PAGE_SHIFT);
2456*4882a593Smuzhiyun 
2457*4882a593Smuzhiyun 	ret_val = hw->phy.ops.acquire(hw);
2458*4882a593Smuzhiyun 	if (ret_val)
2459*4882a593Smuzhiyun 		return ret_val;
2460*4882a593Smuzhiyun 
2461*4882a593Smuzhiyun 	/* Page 800 works differently than the rest so it has its own func */
2462*4882a593Smuzhiyun 	if (page == BM_WUC_PAGE) {
2463*4882a593Smuzhiyun 		ret_val = e1000_access_phy_wakeup_reg_bm(hw, offset, data,
2464*4882a593Smuzhiyun 							 true, false);
2465*4882a593Smuzhiyun 		goto release;
2466*4882a593Smuzhiyun 	}
2467*4882a593Smuzhiyun 
2468*4882a593Smuzhiyun 	hw->phy.addr = 1;
2469*4882a593Smuzhiyun 
2470*4882a593Smuzhiyun 	if (offset > MAX_PHY_MULTI_PAGE_REG) {
2471*4882a593Smuzhiyun 		/* Page is shifted left, PHY expects (page x 32) */
2472*4882a593Smuzhiyun 		ret_val = e1000e_write_phy_reg_mdic(hw, BM_PHY_PAGE_SELECT,
2473*4882a593Smuzhiyun 						    page);
2474*4882a593Smuzhiyun 
2475*4882a593Smuzhiyun 		if (ret_val)
2476*4882a593Smuzhiyun 			goto release;
2477*4882a593Smuzhiyun 	}
2478*4882a593Smuzhiyun 
2479*4882a593Smuzhiyun 	ret_val = e1000e_read_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & offset,
2480*4882a593Smuzhiyun 					   data);
2481*4882a593Smuzhiyun release:
2482*4882a593Smuzhiyun 	hw->phy.ops.release(hw);
2483*4882a593Smuzhiyun 	return ret_val;
2484*4882a593Smuzhiyun }
2485*4882a593Smuzhiyun 
2486*4882a593Smuzhiyun /**
2487*4882a593Smuzhiyun  *  e1000e_write_phy_reg_bm2 - Write BM PHY register
2488*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2489*4882a593Smuzhiyun  *  @offset: register offset to write to
2490*4882a593Smuzhiyun  *  @data: data to write at register offset
2491*4882a593Smuzhiyun  *
2492*4882a593Smuzhiyun  *  Acquires semaphore, if necessary, then writes the data to PHY register
2493*4882a593Smuzhiyun  *  at the offset.  Release any acquired semaphores before exiting.
2494*4882a593Smuzhiyun  **/
e1000e_write_phy_reg_bm2(struct e1000_hw * hw,u32 offset,u16 data)2495*4882a593Smuzhiyun s32 e1000e_write_phy_reg_bm2(struct e1000_hw *hw, u32 offset, u16 data)
2496*4882a593Smuzhiyun {
2497*4882a593Smuzhiyun 	s32 ret_val;
2498*4882a593Smuzhiyun 	u16 page = (u16)(offset >> IGP_PAGE_SHIFT);
2499*4882a593Smuzhiyun 
2500*4882a593Smuzhiyun 	ret_val = hw->phy.ops.acquire(hw);
2501*4882a593Smuzhiyun 	if (ret_val)
2502*4882a593Smuzhiyun 		return ret_val;
2503*4882a593Smuzhiyun 
2504*4882a593Smuzhiyun 	/* Page 800 works differently than the rest so it has its own func */
2505*4882a593Smuzhiyun 	if (page == BM_WUC_PAGE) {
2506*4882a593Smuzhiyun 		ret_val = e1000_access_phy_wakeup_reg_bm(hw, offset, &data,
2507*4882a593Smuzhiyun 							 false, false);
2508*4882a593Smuzhiyun 		goto release;
2509*4882a593Smuzhiyun 	}
2510*4882a593Smuzhiyun 
2511*4882a593Smuzhiyun 	hw->phy.addr = 1;
2512*4882a593Smuzhiyun 
2513*4882a593Smuzhiyun 	if (offset > MAX_PHY_MULTI_PAGE_REG) {
2514*4882a593Smuzhiyun 		/* Page is shifted left, PHY expects (page x 32) */
2515*4882a593Smuzhiyun 		ret_val = e1000e_write_phy_reg_mdic(hw, BM_PHY_PAGE_SELECT,
2516*4882a593Smuzhiyun 						    page);
2517*4882a593Smuzhiyun 
2518*4882a593Smuzhiyun 		if (ret_val)
2519*4882a593Smuzhiyun 			goto release;
2520*4882a593Smuzhiyun 	}
2521*4882a593Smuzhiyun 
2522*4882a593Smuzhiyun 	ret_val = e1000e_write_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & offset,
2523*4882a593Smuzhiyun 					    data);
2524*4882a593Smuzhiyun 
2525*4882a593Smuzhiyun release:
2526*4882a593Smuzhiyun 	hw->phy.ops.release(hw);
2527*4882a593Smuzhiyun 	return ret_val;
2528*4882a593Smuzhiyun }
2529*4882a593Smuzhiyun 
2530*4882a593Smuzhiyun /**
2531*4882a593Smuzhiyun  *  e1000_enable_phy_wakeup_reg_access_bm - enable access to BM wakeup registers
2532*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2533*4882a593Smuzhiyun  *  @phy_reg: pointer to store original contents of BM_WUC_ENABLE_REG
2534*4882a593Smuzhiyun  *
2535*4882a593Smuzhiyun  *  Assumes semaphore already acquired and phy_reg points to a valid memory
2536*4882a593Smuzhiyun  *  address to store contents of the BM_WUC_ENABLE_REG register.
2537*4882a593Smuzhiyun  **/
e1000_enable_phy_wakeup_reg_access_bm(struct e1000_hw * hw,u16 * phy_reg)2538*4882a593Smuzhiyun s32 e1000_enable_phy_wakeup_reg_access_bm(struct e1000_hw *hw, u16 *phy_reg)
2539*4882a593Smuzhiyun {
2540*4882a593Smuzhiyun 	s32 ret_val;
2541*4882a593Smuzhiyun 	u16 temp;
2542*4882a593Smuzhiyun 
2543*4882a593Smuzhiyun 	/* All page select, port ctrl and wakeup registers use phy address 1 */
2544*4882a593Smuzhiyun 	hw->phy.addr = 1;
2545*4882a593Smuzhiyun 
2546*4882a593Smuzhiyun 	/* Select Port Control Registers page */
2547*4882a593Smuzhiyun 	ret_val = e1000_set_page_igp(hw, (BM_PORT_CTRL_PAGE << IGP_PAGE_SHIFT));
2548*4882a593Smuzhiyun 	if (ret_val) {
2549*4882a593Smuzhiyun 		e_dbg("Could not set Port Control page\n");
2550*4882a593Smuzhiyun 		return ret_val;
2551*4882a593Smuzhiyun 	}
2552*4882a593Smuzhiyun 
2553*4882a593Smuzhiyun 	ret_val = e1000e_read_phy_reg_mdic(hw, BM_WUC_ENABLE_REG, phy_reg);
2554*4882a593Smuzhiyun 	if (ret_val) {
2555*4882a593Smuzhiyun 		e_dbg("Could not read PHY register %d.%d\n",
2556*4882a593Smuzhiyun 		      BM_PORT_CTRL_PAGE, BM_WUC_ENABLE_REG);
2557*4882a593Smuzhiyun 		return ret_val;
2558*4882a593Smuzhiyun 	}
2559*4882a593Smuzhiyun 
2560*4882a593Smuzhiyun 	/* Enable both PHY wakeup mode and Wakeup register page writes.
2561*4882a593Smuzhiyun 	 * Prevent a power state change by disabling ME and Host PHY wakeup.
2562*4882a593Smuzhiyun 	 */
2563*4882a593Smuzhiyun 	temp = *phy_reg;
2564*4882a593Smuzhiyun 	temp |= BM_WUC_ENABLE_BIT;
2565*4882a593Smuzhiyun 	temp &= ~(BM_WUC_ME_WU_BIT | BM_WUC_HOST_WU_BIT);
2566*4882a593Smuzhiyun 
2567*4882a593Smuzhiyun 	ret_val = e1000e_write_phy_reg_mdic(hw, BM_WUC_ENABLE_REG, temp);
2568*4882a593Smuzhiyun 	if (ret_val) {
2569*4882a593Smuzhiyun 		e_dbg("Could not write PHY register %d.%d\n",
2570*4882a593Smuzhiyun 		      BM_PORT_CTRL_PAGE, BM_WUC_ENABLE_REG);
2571*4882a593Smuzhiyun 		return ret_val;
2572*4882a593Smuzhiyun 	}
2573*4882a593Smuzhiyun 
2574*4882a593Smuzhiyun 	/* Select Host Wakeup Registers page - caller now able to write
2575*4882a593Smuzhiyun 	 * registers on the Wakeup registers page
2576*4882a593Smuzhiyun 	 */
2577*4882a593Smuzhiyun 	return e1000_set_page_igp(hw, (BM_WUC_PAGE << IGP_PAGE_SHIFT));
2578*4882a593Smuzhiyun }
2579*4882a593Smuzhiyun 
2580*4882a593Smuzhiyun /**
2581*4882a593Smuzhiyun  *  e1000_disable_phy_wakeup_reg_access_bm - disable access to BM wakeup regs
2582*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2583*4882a593Smuzhiyun  *  @phy_reg: pointer to original contents of BM_WUC_ENABLE_REG
2584*4882a593Smuzhiyun  *
2585*4882a593Smuzhiyun  *  Restore BM_WUC_ENABLE_REG to its original value.
2586*4882a593Smuzhiyun  *
2587*4882a593Smuzhiyun  *  Assumes semaphore already acquired and *phy_reg is the contents of the
2588*4882a593Smuzhiyun  *  BM_WUC_ENABLE_REG before register(s) on BM_WUC_PAGE were accessed by
2589*4882a593Smuzhiyun  *  caller.
2590*4882a593Smuzhiyun  **/
e1000_disable_phy_wakeup_reg_access_bm(struct e1000_hw * hw,u16 * phy_reg)2591*4882a593Smuzhiyun s32 e1000_disable_phy_wakeup_reg_access_bm(struct e1000_hw *hw, u16 *phy_reg)
2592*4882a593Smuzhiyun {
2593*4882a593Smuzhiyun 	s32 ret_val;
2594*4882a593Smuzhiyun 
2595*4882a593Smuzhiyun 	/* Select Port Control Registers page */
2596*4882a593Smuzhiyun 	ret_val = e1000_set_page_igp(hw, (BM_PORT_CTRL_PAGE << IGP_PAGE_SHIFT));
2597*4882a593Smuzhiyun 	if (ret_val) {
2598*4882a593Smuzhiyun 		e_dbg("Could not set Port Control page\n");
2599*4882a593Smuzhiyun 		return ret_val;
2600*4882a593Smuzhiyun 	}
2601*4882a593Smuzhiyun 
2602*4882a593Smuzhiyun 	/* Restore 769.17 to its original value */
2603*4882a593Smuzhiyun 	ret_val = e1000e_write_phy_reg_mdic(hw, BM_WUC_ENABLE_REG, *phy_reg);
2604*4882a593Smuzhiyun 	if (ret_val)
2605*4882a593Smuzhiyun 		e_dbg("Could not restore PHY register %d.%d\n",
2606*4882a593Smuzhiyun 		      BM_PORT_CTRL_PAGE, BM_WUC_ENABLE_REG);
2607*4882a593Smuzhiyun 
2608*4882a593Smuzhiyun 	return ret_val;
2609*4882a593Smuzhiyun }
2610*4882a593Smuzhiyun 
2611*4882a593Smuzhiyun /**
2612*4882a593Smuzhiyun  *  e1000_access_phy_wakeup_reg_bm - Read/write BM PHY wakeup register
2613*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2614*4882a593Smuzhiyun  *  @offset: register offset to be read or written
2615*4882a593Smuzhiyun  *  @data: pointer to the data to read or write
2616*4882a593Smuzhiyun  *  @read: determines if operation is read or write
2617*4882a593Smuzhiyun  *  @page_set: BM_WUC_PAGE already set and access enabled
2618*4882a593Smuzhiyun  *
2619*4882a593Smuzhiyun  *  Read the PHY register at offset and store the retrieved information in
2620*4882a593Smuzhiyun  *  data, or write data to PHY register at offset.  Note the procedure to
2621*4882a593Smuzhiyun  *  access the PHY wakeup registers is different than reading the other PHY
2622*4882a593Smuzhiyun  *  registers. It works as such:
2623*4882a593Smuzhiyun  *  1) Set 769.17.2 (page 769, register 17, bit 2) = 1
2624*4882a593Smuzhiyun  *  2) Set page to 800 for host (801 if we were manageability)
2625*4882a593Smuzhiyun  *  3) Write the address using the address opcode (0x11)
2626*4882a593Smuzhiyun  *  4) Read or write the data using the data opcode (0x12)
2627*4882a593Smuzhiyun  *  5) Restore 769.17.2 to its original value
2628*4882a593Smuzhiyun  *
2629*4882a593Smuzhiyun  *  Steps 1 and 2 are done by e1000_enable_phy_wakeup_reg_access_bm() and
2630*4882a593Smuzhiyun  *  step 5 is done by e1000_disable_phy_wakeup_reg_access_bm().
2631*4882a593Smuzhiyun  *
2632*4882a593Smuzhiyun  *  Assumes semaphore is already acquired.  When page_set==true, assumes
2633*4882a593Smuzhiyun  *  the PHY page is set to BM_WUC_PAGE (i.e. a function in the call stack
2634*4882a593Smuzhiyun  *  is responsible for calls to e1000_[enable|disable]_phy_wakeup_reg_bm()).
2635*4882a593Smuzhiyun  **/
e1000_access_phy_wakeup_reg_bm(struct e1000_hw * hw,u32 offset,u16 * data,bool read,bool page_set)2636*4882a593Smuzhiyun static s32 e1000_access_phy_wakeup_reg_bm(struct e1000_hw *hw, u32 offset,
2637*4882a593Smuzhiyun 					  u16 *data, bool read, bool page_set)
2638*4882a593Smuzhiyun {
2639*4882a593Smuzhiyun 	s32 ret_val;
2640*4882a593Smuzhiyun 	u16 reg = BM_PHY_REG_NUM(offset);
2641*4882a593Smuzhiyun 	u16 page = BM_PHY_REG_PAGE(offset);
2642*4882a593Smuzhiyun 	u16 phy_reg = 0;
2643*4882a593Smuzhiyun 
2644*4882a593Smuzhiyun 	/* Gig must be disabled for MDIO accesses to Host Wakeup reg page */
2645*4882a593Smuzhiyun 	if ((hw->mac.type == e1000_pchlan) &&
2646*4882a593Smuzhiyun 	    (!(er32(PHY_CTRL) & E1000_PHY_CTRL_GBE_DISABLE)))
2647*4882a593Smuzhiyun 		e_dbg("Attempting to access page %d while gig enabled.\n",
2648*4882a593Smuzhiyun 		      page);
2649*4882a593Smuzhiyun 
2650*4882a593Smuzhiyun 	if (!page_set) {
2651*4882a593Smuzhiyun 		/* Enable access to PHY wakeup registers */
2652*4882a593Smuzhiyun 		ret_val = e1000_enable_phy_wakeup_reg_access_bm(hw, &phy_reg);
2653*4882a593Smuzhiyun 		if (ret_val) {
2654*4882a593Smuzhiyun 			e_dbg("Could not enable PHY wakeup reg access\n");
2655*4882a593Smuzhiyun 			return ret_val;
2656*4882a593Smuzhiyun 		}
2657*4882a593Smuzhiyun 	}
2658*4882a593Smuzhiyun 
2659*4882a593Smuzhiyun 	e_dbg("Accessing PHY page %d reg 0x%x\n", page, reg);
2660*4882a593Smuzhiyun 
2661*4882a593Smuzhiyun 	/* Write the Wakeup register page offset value using opcode 0x11 */
2662*4882a593Smuzhiyun 	ret_val = e1000e_write_phy_reg_mdic(hw, BM_WUC_ADDRESS_OPCODE, reg);
2663*4882a593Smuzhiyun 	if (ret_val) {
2664*4882a593Smuzhiyun 		e_dbg("Could not write address opcode to page %d\n", page);
2665*4882a593Smuzhiyun 		return ret_val;
2666*4882a593Smuzhiyun 	}
2667*4882a593Smuzhiyun 
2668*4882a593Smuzhiyun 	if (read) {
2669*4882a593Smuzhiyun 		/* Read the Wakeup register page value using opcode 0x12 */
2670*4882a593Smuzhiyun 		ret_val = e1000e_read_phy_reg_mdic(hw, BM_WUC_DATA_OPCODE,
2671*4882a593Smuzhiyun 						   data);
2672*4882a593Smuzhiyun 	} else {
2673*4882a593Smuzhiyun 		/* Write the Wakeup register page value using opcode 0x12 */
2674*4882a593Smuzhiyun 		ret_val = e1000e_write_phy_reg_mdic(hw, BM_WUC_DATA_OPCODE,
2675*4882a593Smuzhiyun 						    *data);
2676*4882a593Smuzhiyun 	}
2677*4882a593Smuzhiyun 
2678*4882a593Smuzhiyun 	if (ret_val) {
2679*4882a593Smuzhiyun 		e_dbg("Could not access PHY reg %d.%d\n", page, reg);
2680*4882a593Smuzhiyun 		return ret_val;
2681*4882a593Smuzhiyun 	}
2682*4882a593Smuzhiyun 
2683*4882a593Smuzhiyun 	if (!page_set)
2684*4882a593Smuzhiyun 		ret_val = e1000_disable_phy_wakeup_reg_access_bm(hw, &phy_reg);
2685*4882a593Smuzhiyun 
2686*4882a593Smuzhiyun 	return ret_val;
2687*4882a593Smuzhiyun }
2688*4882a593Smuzhiyun 
2689*4882a593Smuzhiyun /**
2690*4882a593Smuzhiyun  * e1000_power_up_phy_copper - Restore copper link in case of PHY power down
2691*4882a593Smuzhiyun  * @hw: pointer to the HW structure
2692*4882a593Smuzhiyun  *
2693*4882a593Smuzhiyun  * In the case of a PHY power down to save power, or to turn off link during a
2694*4882a593Smuzhiyun  * driver unload, or wake on lan is not enabled, restore the link to previous
2695*4882a593Smuzhiyun  * settings.
2696*4882a593Smuzhiyun  **/
e1000_power_up_phy_copper(struct e1000_hw * hw)2697*4882a593Smuzhiyun void e1000_power_up_phy_copper(struct e1000_hw *hw)
2698*4882a593Smuzhiyun {
2699*4882a593Smuzhiyun 	u16 mii_reg = 0;
2700*4882a593Smuzhiyun 
2701*4882a593Smuzhiyun 	/* The PHY will retain its settings across a power down/up cycle */
2702*4882a593Smuzhiyun 	e1e_rphy(hw, MII_BMCR, &mii_reg);
2703*4882a593Smuzhiyun 	mii_reg &= ~BMCR_PDOWN;
2704*4882a593Smuzhiyun 	e1e_wphy(hw, MII_BMCR, mii_reg);
2705*4882a593Smuzhiyun }
2706*4882a593Smuzhiyun 
2707*4882a593Smuzhiyun /**
2708*4882a593Smuzhiyun  * e1000_power_down_phy_copper - Restore copper link in case of PHY power down
2709*4882a593Smuzhiyun  * @hw: pointer to the HW structure
2710*4882a593Smuzhiyun  *
2711*4882a593Smuzhiyun  * In the case of a PHY power down to save power, or to turn off link during a
2712*4882a593Smuzhiyun  * driver unload, or wake on lan is not enabled, restore the link to previous
2713*4882a593Smuzhiyun  * settings.
2714*4882a593Smuzhiyun  **/
e1000_power_down_phy_copper(struct e1000_hw * hw)2715*4882a593Smuzhiyun void e1000_power_down_phy_copper(struct e1000_hw *hw)
2716*4882a593Smuzhiyun {
2717*4882a593Smuzhiyun 	u16 mii_reg = 0;
2718*4882a593Smuzhiyun 
2719*4882a593Smuzhiyun 	/* The PHY will retain its settings across a power down/up cycle */
2720*4882a593Smuzhiyun 	e1e_rphy(hw, MII_BMCR, &mii_reg);
2721*4882a593Smuzhiyun 	mii_reg |= BMCR_PDOWN;
2722*4882a593Smuzhiyun 	e1e_wphy(hw, MII_BMCR, mii_reg);
2723*4882a593Smuzhiyun 	usleep_range(1000, 2000);
2724*4882a593Smuzhiyun }
2725*4882a593Smuzhiyun 
2726*4882a593Smuzhiyun /**
2727*4882a593Smuzhiyun  *  __e1000_read_phy_reg_hv -  Read HV PHY register
2728*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2729*4882a593Smuzhiyun  *  @offset: register offset to be read
2730*4882a593Smuzhiyun  *  @data: pointer to the read data
2731*4882a593Smuzhiyun  *  @locked: semaphore has already been acquired or not
2732*4882a593Smuzhiyun  *  @page_set: BM_WUC_PAGE already set and access enabled
2733*4882a593Smuzhiyun  *
2734*4882a593Smuzhiyun  *  Acquires semaphore, if necessary, then reads the PHY register at offset
2735*4882a593Smuzhiyun  *  and stores the retrieved information in data.  Release any acquired
2736*4882a593Smuzhiyun  *  semaphore before exiting.
2737*4882a593Smuzhiyun  **/
__e1000_read_phy_reg_hv(struct e1000_hw * hw,u32 offset,u16 * data,bool locked,bool page_set)2738*4882a593Smuzhiyun static s32 __e1000_read_phy_reg_hv(struct e1000_hw *hw, u32 offset, u16 *data,
2739*4882a593Smuzhiyun 				   bool locked, bool page_set)
2740*4882a593Smuzhiyun {
2741*4882a593Smuzhiyun 	s32 ret_val;
2742*4882a593Smuzhiyun 	u16 page = BM_PHY_REG_PAGE(offset);
2743*4882a593Smuzhiyun 	u16 reg = BM_PHY_REG_NUM(offset);
2744*4882a593Smuzhiyun 	u32 phy_addr = hw->phy.addr = e1000_get_phy_addr_for_hv_page(page);
2745*4882a593Smuzhiyun 
2746*4882a593Smuzhiyun 	if (!locked) {
2747*4882a593Smuzhiyun 		ret_val = hw->phy.ops.acquire(hw);
2748*4882a593Smuzhiyun 		if (ret_val)
2749*4882a593Smuzhiyun 			return ret_val;
2750*4882a593Smuzhiyun 	}
2751*4882a593Smuzhiyun 
2752*4882a593Smuzhiyun 	/* Page 800 works differently than the rest so it has its own func */
2753*4882a593Smuzhiyun 	if (page == BM_WUC_PAGE) {
2754*4882a593Smuzhiyun 		ret_val = e1000_access_phy_wakeup_reg_bm(hw, offset, data,
2755*4882a593Smuzhiyun 							 true, page_set);
2756*4882a593Smuzhiyun 		goto out;
2757*4882a593Smuzhiyun 	}
2758*4882a593Smuzhiyun 
2759*4882a593Smuzhiyun 	if (page > 0 && page < HV_INTC_FC_PAGE_START) {
2760*4882a593Smuzhiyun 		ret_val = e1000_access_phy_debug_regs_hv(hw, offset,
2761*4882a593Smuzhiyun 							 data, true);
2762*4882a593Smuzhiyun 		goto out;
2763*4882a593Smuzhiyun 	}
2764*4882a593Smuzhiyun 
2765*4882a593Smuzhiyun 	if (!page_set) {
2766*4882a593Smuzhiyun 		if (page == HV_INTC_FC_PAGE_START)
2767*4882a593Smuzhiyun 			page = 0;
2768*4882a593Smuzhiyun 
2769*4882a593Smuzhiyun 		if (reg > MAX_PHY_MULTI_PAGE_REG) {
2770*4882a593Smuzhiyun 			/* Page is shifted left, PHY expects (page x 32) */
2771*4882a593Smuzhiyun 			ret_val = e1000_set_page_igp(hw,
2772*4882a593Smuzhiyun 						     (page << IGP_PAGE_SHIFT));
2773*4882a593Smuzhiyun 
2774*4882a593Smuzhiyun 			hw->phy.addr = phy_addr;
2775*4882a593Smuzhiyun 
2776*4882a593Smuzhiyun 			if (ret_val)
2777*4882a593Smuzhiyun 				goto out;
2778*4882a593Smuzhiyun 		}
2779*4882a593Smuzhiyun 	}
2780*4882a593Smuzhiyun 
2781*4882a593Smuzhiyun 	e_dbg("reading PHY page %d (or 0x%x shifted) reg 0x%x\n", page,
2782*4882a593Smuzhiyun 	      page << IGP_PAGE_SHIFT, reg);
2783*4882a593Smuzhiyun 
2784*4882a593Smuzhiyun 	ret_val = e1000e_read_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & reg, data);
2785*4882a593Smuzhiyun out:
2786*4882a593Smuzhiyun 	if (!locked)
2787*4882a593Smuzhiyun 		hw->phy.ops.release(hw);
2788*4882a593Smuzhiyun 
2789*4882a593Smuzhiyun 	return ret_val;
2790*4882a593Smuzhiyun }
2791*4882a593Smuzhiyun 
2792*4882a593Smuzhiyun /**
2793*4882a593Smuzhiyun  *  e1000_read_phy_reg_hv -  Read HV PHY register
2794*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2795*4882a593Smuzhiyun  *  @offset: register offset to be read
2796*4882a593Smuzhiyun  *  @data: pointer to the read data
2797*4882a593Smuzhiyun  *
2798*4882a593Smuzhiyun  *  Acquires semaphore then reads the PHY register at offset and stores
2799*4882a593Smuzhiyun  *  the retrieved information in data.  Release the acquired semaphore
2800*4882a593Smuzhiyun  *  before exiting.
2801*4882a593Smuzhiyun  **/
e1000_read_phy_reg_hv(struct e1000_hw * hw,u32 offset,u16 * data)2802*4882a593Smuzhiyun s32 e1000_read_phy_reg_hv(struct e1000_hw *hw, u32 offset, u16 *data)
2803*4882a593Smuzhiyun {
2804*4882a593Smuzhiyun 	return __e1000_read_phy_reg_hv(hw, offset, data, false, false);
2805*4882a593Smuzhiyun }
2806*4882a593Smuzhiyun 
2807*4882a593Smuzhiyun /**
2808*4882a593Smuzhiyun  *  e1000_read_phy_reg_hv_locked -  Read HV PHY register
2809*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2810*4882a593Smuzhiyun  *  @offset: register offset to be read
2811*4882a593Smuzhiyun  *  @data: pointer to the read data
2812*4882a593Smuzhiyun  *
2813*4882a593Smuzhiyun  *  Reads the PHY register at offset and stores the retrieved information
2814*4882a593Smuzhiyun  *  in data.  Assumes semaphore already acquired.
2815*4882a593Smuzhiyun  **/
e1000_read_phy_reg_hv_locked(struct e1000_hw * hw,u32 offset,u16 * data)2816*4882a593Smuzhiyun s32 e1000_read_phy_reg_hv_locked(struct e1000_hw *hw, u32 offset, u16 *data)
2817*4882a593Smuzhiyun {
2818*4882a593Smuzhiyun 	return __e1000_read_phy_reg_hv(hw, offset, data, true, false);
2819*4882a593Smuzhiyun }
2820*4882a593Smuzhiyun 
2821*4882a593Smuzhiyun /**
2822*4882a593Smuzhiyun  *  e1000_read_phy_reg_page_hv - Read HV PHY register
2823*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2824*4882a593Smuzhiyun  *  @offset: register offset to write to
2825*4882a593Smuzhiyun  *  @data: data to write at register offset
2826*4882a593Smuzhiyun  *
2827*4882a593Smuzhiyun  *  Reads the PHY register at offset and stores the retrieved information
2828*4882a593Smuzhiyun  *  in data.  Assumes semaphore already acquired and page already set.
2829*4882a593Smuzhiyun  **/
e1000_read_phy_reg_page_hv(struct e1000_hw * hw,u32 offset,u16 * data)2830*4882a593Smuzhiyun s32 e1000_read_phy_reg_page_hv(struct e1000_hw *hw, u32 offset, u16 *data)
2831*4882a593Smuzhiyun {
2832*4882a593Smuzhiyun 	return __e1000_read_phy_reg_hv(hw, offset, data, true, true);
2833*4882a593Smuzhiyun }
2834*4882a593Smuzhiyun 
2835*4882a593Smuzhiyun /**
2836*4882a593Smuzhiyun  *  __e1000_write_phy_reg_hv - Write HV PHY register
2837*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2838*4882a593Smuzhiyun  *  @offset: register offset to write to
2839*4882a593Smuzhiyun  *  @data: data to write at register offset
2840*4882a593Smuzhiyun  *  @locked: semaphore has already been acquired or not
2841*4882a593Smuzhiyun  *  @page_set: BM_WUC_PAGE already set and access enabled
2842*4882a593Smuzhiyun  *
2843*4882a593Smuzhiyun  *  Acquires semaphore, if necessary, then writes the data to PHY register
2844*4882a593Smuzhiyun  *  at the offset.  Release any acquired semaphores before exiting.
2845*4882a593Smuzhiyun  **/
__e1000_write_phy_reg_hv(struct e1000_hw * hw,u32 offset,u16 data,bool locked,bool page_set)2846*4882a593Smuzhiyun static s32 __e1000_write_phy_reg_hv(struct e1000_hw *hw, u32 offset, u16 data,
2847*4882a593Smuzhiyun 				    bool locked, bool page_set)
2848*4882a593Smuzhiyun {
2849*4882a593Smuzhiyun 	s32 ret_val;
2850*4882a593Smuzhiyun 	u16 page = BM_PHY_REG_PAGE(offset);
2851*4882a593Smuzhiyun 	u16 reg = BM_PHY_REG_NUM(offset);
2852*4882a593Smuzhiyun 	u32 phy_addr = hw->phy.addr = e1000_get_phy_addr_for_hv_page(page);
2853*4882a593Smuzhiyun 
2854*4882a593Smuzhiyun 	if (!locked) {
2855*4882a593Smuzhiyun 		ret_val = hw->phy.ops.acquire(hw);
2856*4882a593Smuzhiyun 		if (ret_val)
2857*4882a593Smuzhiyun 			return ret_val;
2858*4882a593Smuzhiyun 	}
2859*4882a593Smuzhiyun 
2860*4882a593Smuzhiyun 	/* Page 800 works differently than the rest so it has its own func */
2861*4882a593Smuzhiyun 	if (page == BM_WUC_PAGE) {
2862*4882a593Smuzhiyun 		ret_val = e1000_access_phy_wakeup_reg_bm(hw, offset, &data,
2863*4882a593Smuzhiyun 							 false, page_set);
2864*4882a593Smuzhiyun 		goto out;
2865*4882a593Smuzhiyun 	}
2866*4882a593Smuzhiyun 
2867*4882a593Smuzhiyun 	if (page > 0 && page < HV_INTC_FC_PAGE_START) {
2868*4882a593Smuzhiyun 		ret_val = e1000_access_phy_debug_regs_hv(hw, offset,
2869*4882a593Smuzhiyun 							 &data, false);
2870*4882a593Smuzhiyun 		goto out;
2871*4882a593Smuzhiyun 	}
2872*4882a593Smuzhiyun 
2873*4882a593Smuzhiyun 	if (!page_set) {
2874*4882a593Smuzhiyun 		if (page == HV_INTC_FC_PAGE_START)
2875*4882a593Smuzhiyun 			page = 0;
2876*4882a593Smuzhiyun 
2877*4882a593Smuzhiyun 		/* Workaround MDIO accesses being disabled after entering IEEE
2878*4882a593Smuzhiyun 		 * Power Down (when bit 11 of the PHY Control register is set)
2879*4882a593Smuzhiyun 		 */
2880*4882a593Smuzhiyun 		if ((hw->phy.type == e1000_phy_82578) &&
2881*4882a593Smuzhiyun 		    (hw->phy.revision >= 1) &&
2882*4882a593Smuzhiyun 		    (hw->phy.addr == 2) &&
2883*4882a593Smuzhiyun 		    !(MAX_PHY_REG_ADDRESS & reg) && (data & BIT(11))) {
2884*4882a593Smuzhiyun 			u16 data2 = 0x7EFF;
2885*4882a593Smuzhiyun 
2886*4882a593Smuzhiyun 			ret_val = e1000_access_phy_debug_regs_hv(hw,
2887*4882a593Smuzhiyun 								 BIT(6) | 0x3,
2888*4882a593Smuzhiyun 								 &data2, false);
2889*4882a593Smuzhiyun 			if (ret_val)
2890*4882a593Smuzhiyun 				goto out;
2891*4882a593Smuzhiyun 		}
2892*4882a593Smuzhiyun 
2893*4882a593Smuzhiyun 		if (reg > MAX_PHY_MULTI_PAGE_REG) {
2894*4882a593Smuzhiyun 			/* Page is shifted left, PHY expects (page x 32) */
2895*4882a593Smuzhiyun 			ret_val = e1000_set_page_igp(hw,
2896*4882a593Smuzhiyun 						     (page << IGP_PAGE_SHIFT));
2897*4882a593Smuzhiyun 
2898*4882a593Smuzhiyun 			hw->phy.addr = phy_addr;
2899*4882a593Smuzhiyun 
2900*4882a593Smuzhiyun 			if (ret_val)
2901*4882a593Smuzhiyun 				goto out;
2902*4882a593Smuzhiyun 		}
2903*4882a593Smuzhiyun 	}
2904*4882a593Smuzhiyun 
2905*4882a593Smuzhiyun 	e_dbg("writing PHY page %d (or 0x%x shifted) reg 0x%x\n", page,
2906*4882a593Smuzhiyun 	      page << IGP_PAGE_SHIFT, reg);
2907*4882a593Smuzhiyun 
2908*4882a593Smuzhiyun 	ret_val = e1000e_write_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & reg,
2909*4882a593Smuzhiyun 					    data);
2910*4882a593Smuzhiyun 
2911*4882a593Smuzhiyun out:
2912*4882a593Smuzhiyun 	if (!locked)
2913*4882a593Smuzhiyun 		hw->phy.ops.release(hw);
2914*4882a593Smuzhiyun 
2915*4882a593Smuzhiyun 	return ret_val;
2916*4882a593Smuzhiyun }
2917*4882a593Smuzhiyun 
2918*4882a593Smuzhiyun /**
2919*4882a593Smuzhiyun  *  e1000_write_phy_reg_hv - Write HV PHY register
2920*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2921*4882a593Smuzhiyun  *  @offset: register offset to write to
2922*4882a593Smuzhiyun  *  @data: data to write at register offset
2923*4882a593Smuzhiyun  *
2924*4882a593Smuzhiyun  *  Acquires semaphore then writes the data to PHY register at the offset.
2925*4882a593Smuzhiyun  *  Release the acquired semaphores before exiting.
2926*4882a593Smuzhiyun  **/
e1000_write_phy_reg_hv(struct e1000_hw * hw,u32 offset,u16 data)2927*4882a593Smuzhiyun s32 e1000_write_phy_reg_hv(struct e1000_hw *hw, u32 offset, u16 data)
2928*4882a593Smuzhiyun {
2929*4882a593Smuzhiyun 	return __e1000_write_phy_reg_hv(hw, offset, data, false, false);
2930*4882a593Smuzhiyun }
2931*4882a593Smuzhiyun 
2932*4882a593Smuzhiyun /**
2933*4882a593Smuzhiyun  *  e1000_write_phy_reg_hv_locked - Write HV PHY register
2934*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2935*4882a593Smuzhiyun  *  @offset: register offset to write to
2936*4882a593Smuzhiyun  *  @data: data to write at register offset
2937*4882a593Smuzhiyun  *
2938*4882a593Smuzhiyun  *  Writes the data to PHY register at the offset.  Assumes semaphore
2939*4882a593Smuzhiyun  *  already acquired.
2940*4882a593Smuzhiyun  **/
e1000_write_phy_reg_hv_locked(struct e1000_hw * hw,u32 offset,u16 data)2941*4882a593Smuzhiyun s32 e1000_write_phy_reg_hv_locked(struct e1000_hw *hw, u32 offset, u16 data)
2942*4882a593Smuzhiyun {
2943*4882a593Smuzhiyun 	return __e1000_write_phy_reg_hv(hw, offset, data, true, false);
2944*4882a593Smuzhiyun }
2945*4882a593Smuzhiyun 
2946*4882a593Smuzhiyun /**
2947*4882a593Smuzhiyun  *  e1000_write_phy_reg_page_hv - Write HV PHY register
2948*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2949*4882a593Smuzhiyun  *  @offset: register offset to write to
2950*4882a593Smuzhiyun  *  @data: data to write at register offset
2951*4882a593Smuzhiyun  *
2952*4882a593Smuzhiyun  *  Writes the data to PHY register at the offset.  Assumes semaphore
2953*4882a593Smuzhiyun  *  already acquired and page already set.
2954*4882a593Smuzhiyun  **/
e1000_write_phy_reg_page_hv(struct e1000_hw * hw,u32 offset,u16 data)2955*4882a593Smuzhiyun s32 e1000_write_phy_reg_page_hv(struct e1000_hw *hw, u32 offset, u16 data)
2956*4882a593Smuzhiyun {
2957*4882a593Smuzhiyun 	return __e1000_write_phy_reg_hv(hw, offset, data, true, true);
2958*4882a593Smuzhiyun }
2959*4882a593Smuzhiyun 
2960*4882a593Smuzhiyun /**
2961*4882a593Smuzhiyun  *  e1000_get_phy_addr_for_hv_page - Get PHY address based on page
2962*4882a593Smuzhiyun  *  @page: page to be accessed
2963*4882a593Smuzhiyun  **/
e1000_get_phy_addr_for_hv_page(u32 page)2964*4882a593Smuzhiyun static u32 e1000_get_phy_addr_for_hv_page(u32 page)
2965*4882a593Smuzhiyun {
2966*4882a593Smuzhiyun 	u32 phy_addr = 2;
2967*4882a593Smuzhiyun 
2968*4882a593Smuzhiyun 	if (page >= HV_INTC_FC_PAGE_START)
2969*4882a593Smuzhiyun 		phy_addr = 1;
2970*4882a593Smuzhiyun 
2971*4882a593Smuzhiyun 	return phy_addr;
2972*4882a593Smuzhiyun }
2973*4882a593Smuzhiyun 
2974*4882a593Smuzhiyun /**
2975*4882a593Smuzhiyun  *  e1000_access_phy_debug_regs_hv - Read HV PHY vendor specific high registers
2976*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
2977*4882a593Smuzhiyun  *  @offset: register offset to be read or written
2978*4882a593Smuzhiyun  *  @data: pointer to the data to be read or written
2979*4882a593Smuzhiyun  *  @read: determines if operation is read or write
2980*4882a593Smuzhiyun  *
2981*4882a593Smuzhiyun  *  Reads the PHY register at offset and stores the retreived information
2982*4882a593Smuzhiyun  *  in data.  Assumes semaphore already acquired.  Note that the procedure
2983*4882a593Smuzhiyun  *  to access these regs uses the address port and data port to read/write.
2984*4882a593Smuzhiyun  *  These accesses done with PHY address 2 and without using pages.
2985*4882a593Smuzhiyun  **/
e1000_access_phy_debug_regs_hv(struct e1000_hw * hw,u32 offset,u16 * data,bool read)2986*4882a593Smuzhiyun static s32 e1000_access_phy_debug_regs_hv(struct e1000_hw *hw, u32 offset,
2987*4882a593Smuzhiyun 					  u16 *data, bool read)
2988*4882a593Smuzhiyun {
2989*4882a593Smuzhiyun 	s32 ret_val;
2990*4882a593Smuzhiyun 	u32 addr_reg;
2991*4882a593Smuzhiyun 	u32 data_reg;
2992*4882a593Smuzhiyun 
2993*4882a593Smuzhiyun 	/* This takes care of the difference with desktop vs mobile phy */
2994*4882a593Smuzhiyun 	addr_reg = ((hw->phy.type == e1000_phy_82578) ?
2995*4882a593Smuzhiyun 		    I82578_ADDR_REG : I82577_ADDR_REG);
2996*4882a593Smuzhiyun 	data_reg = addr_reg + 1;
2997*4882a593Smuzhiyun 
2998*4882a593Smuzhiyun 	/* All operations in this function are phy address 2 */
2999*4882a593Smuzhiyun 	hw->phy.addr = 2;
3000*4882a593Smuzhiyun 
3001*4882a593Smuzhiyun 	/* masking with 0x3F to remove the page from offset */
3002*4882a593Smuzhiyun 	ret_val = e1000e_write_phy_reg_mdic(hw, addr_reg, (u16)offset & 0x3F);
3003*4882a593Smuzhiyun 	if (ret_val) {
3004*4882a593Smuzhiyun 		e_dbg("Could not write the Address Offset port register\n");
3005*4882a593Smuzhiyun 		return ret_val;
3006*4882a593Smuzhiyun 	}
3007*4882a593Smuzhiyun 
3008*4882a593Smuzhiyun 	/* Read or write the data value next */
3009*4882a593Smuzhiyun 	if (read)
3010*4882a593Smuzhiyun 		ret_val = e1000e_read_phy_reg_mdic(hw, data_reg, data);
3011*4882a593Smuzhiyun 	else
3012*4882a593Smuzhiyun 		ret_val = e1000e_write_phy_reg_mdic(hw, data_reg, *data);
3013*4882a593Smuzhiyun 
3014*4882a593Smuzhiyun 	if (ret_val)
3015*4882a593Smuzhiyun 		e_dbg("Could not access the Data port register\n");
3016*4882a593Smuzhiyun 
3017*4882a593Smuzhiyun 	return ret_val;
3018*4882a593Smuzhiyun }
3019*4882a593Smuzhiyun 
3020*4882a593Smuzhiyun /**
3021*4882a593Smuzhiyun  *  e1000_link_stall_workaround_hv - Si workaround
3022*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
3023*4882a593Smuzhiyun  *
3024*4882a593Smuzhiyun  *  This function works around a Si bug where the link partner can get
3025*4882a593Smuzhiyun  *  a link up indication before the PHY does.  If small packets are sent
3026*4882a593Smuzhiyun  *  by the link partner they can be placed in the packet buffer without
3027*4882a593Smuzhiyun  *  being properly accounted for by the PHY and will stall preventing
3028*4882a593Smuzhiyun  *  further packets from being received.  The workaround is to clear the
3029*4882a593Smuzhiyun  *  packet buffer after the PHY detects link up.
3030*4882a593Smuzhiyun  **/
e1000_link_stall_workaround_hv(struct e1000_hw * hw)3031*4882a593Smuzhiyun s32 e1000_link_stall_workaround_hv(struct e1000_hw *hw)
3032*4882a593Smuzhiyun {
3033*4882a593Smuzhiyun 	s32 ret_val = 0;
3034*4882a593Smuzhiyun 	u16 data;
3035*4882a593Smuzhiyun 
3036*4882a593Smuzhiyun 	if (hw->phy.type != e1000_phy_82578)
3037*4882a593Smuzhiyun 		return 0;
3038*4882a593Smuzhiyun 
3039*4882a593Smuzhiyun 	/* Do not apply workaround if in PHY loopback bit 14 set */
3040*4882a593Smuzhiyun 	e1e_rphy(hw, MII_BMCR, &data);
3041*4882a593Smuzhiyun 	if (data & BMCR_LOOPBACK)
3042*4882a593Smuzhiyun 		return 0;
3043*4882a593Smuzhiyun 
3044*4882a593Smuzhiyun 	/* check if link is up and at 1Gbps */
3045*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, BM_CS_STATUS, &data);
3046*4882a593Smuzhiyun 	if (ret_val)
3047*4882a593Smuzhiyun 		return ret_val;
3048*4882a593Smuzhiyun 
3049*4882a593Smuzhiyun 	data &= (BM_CS_STATUS_LINK_UP | BM_CS_STATUS_RESOLVED |
3050*4882a593Smuzhiyun 		 BM_CS_STATUS_SPEED_MASK);
3051*4882a593Smuzhiyun 
3052*4882a593Smuzhiyun 	if (data != (BM_CS_STATUS_LINK_UP | BM_CS_STATUS_RESOLVED |
3053*4882a593Smuzhiyun 		     BM_CS_STATUS_SPEED_1000))
3054*4882a593Smuzhiyun 		return 0;
3055*4882a593Smuzhiyun 
3056*4882a593Smuzhiyun 	msleep(200);
3057*4882a593Smuzhiyun 
3058*4882a593Smuzhiyun 	/* flush the packets in the fifo buffer */
3059*4882a593Smuzhiyun 	ret_val = e1e_wphy(hw, HV_MUX_DATA_CTRL,
3060*4882a593Smuzhiyun 			   (HV_MUX_DATA_CTRL_GEN_TO_MAC |
3061*4882a593Smuzhiyun 			    HV_MUX_DATA_CTRL_FORCE_SPEED));
3062*4882a593Smuzhiyun 	if (ret_val)
3063*4882a593Smuzhiyun 		return ret_val;
3064*4882a593Smuzhiyun 
3065*4882a593Smuzhiyun 	return e1e_wphy(hw, HV_MUX_DATA_CTRL, HV_MUX_DATA_CTRL_GEN_TO_MAC);
3066*4882a593Smuzhiyun }
3067*4882a593Smuzhiyun 
3068*4882a593Smuzhiyun /**
3069*4882a593Smuzhiyun  *  e1000_check_polarity_82577 - Checks the polarity.
3070*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
3071*4882a593Smuzhiyun  *
3072*4882a593Smuzhiyun  *  Success returns 0, Failure returns -E1000_ERR_PHY (-2)
3073*4882a593Smuzhiyun  *
3074*4882a593Smuzhiyun  *  Polarity is determined based on the PHY specific status register.
3075*4882a593Smuzhiyun  **/
e1000_check_polarity_82577(struct e1000_hw * hw)3076*4882a593Smuzhiyun s32 e1000_check_polarity_82577(struct e1000_hw *hw)
3077*4882a593Smuzhiyun {
3078*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
3079*4882a593Smuzhiyun 	s32 ret_val;
3080*4882a593Smuzhiyun 	u16 data;
3081*4882a593Smuzhiyun 
3082*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, I82577_PHY_STATUS_2, &data);
3083*4882a593Smuzhiyun 
3084*4882a593Smuzhiyun 	if (!ret_val)
3085*4882a593Smuzhiyun 		phy->cable_polarity = ((data & I82577_PHY_STATUS2_REV_POLARITY)
3086*4882a593Smuzhiyun 				       ? e1000_rev_polarity_reversed
3087*4882a593Smuzhiyun 				       : e1000_rev_polarity_normal);
3088*4882a593Smuzhiyun 
3089*4882a593Smuzhiyun 	return ret_val;
3090*4882a593Smuzhiyun }
3091*4882a593Smuzhiyun 
3092*4882a593Smuzhiyun /**
3093*4882a593Smuzhiyun  *  e1000_phy_force_speed_duplex_82577 - Force speed/duplex for I82577 PHY
3094*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
3095*4882a593Smuzhiyun  *
3096*4882a593Smuzhiyun  *  Calls the PHY setup function to force speed and duplex.
3097*4882a593Smuzhiyun  **/
e1000_phy_force_speed_duplex_82577(struct e1000_hw * hw)3098*4882a593Smuzhiyun s32 e1000_phy_force_speed_duplex_82577(struct e1000_hw *hw)
3099*4882a593Smuzhiyun {
3100*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
3101*4882a593Smuzhiyun 	s32 ret_val;
3102*4882a593Smuzhiyun 	u16 phy_data;
3103*4882a593Smuzhiyun 	bool link;
3104*4882a593Smuzhiyun 
3105*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, MII_BMCR, &phy_data);
3106*4882a593Smuzhiyun 	if (ret_val)
3107*4882a593Smuzhiyun 		return ret_val;
3108*4882a593Smuzhiyun 
3109*4882a593Smuzhiyun 	e1000e_phy_force_speed_duplex_setup(hw, &phy_data);
3110*4882a593Smuzhiyun 
3111*4882a593Smuzhiyun 	ret_val = e1e_wphy(hw, MII_BMCR, phy_data);
3112*4882a593Smuzhiyun 	if (ret_val)
3113*4882a593Smuzhiyun 		return ret_val;
3114*4882a593Smuzhiyun 
3115*4882a593Smuzhiyun 	udelay(1);
3116*4882a593Smuzhiyun 
3117*4882a593Smuzhiyun 	if (phy->autoneg_wait_to_complete) {
3118*4882a593Smuzhiyun 		e_dbg("Waiting for forced speed/duplex link on 82577 phy\n");
3119*4882a593Smuzhiyun 
3120*4882a593Smuzhiyun 		ret_val = e1000e_phy_has_link_generic(hw, PHY_FORCE_LIMIT,
3121*4882a593Smuzhiyun 						      100000, &link);
3122*4882a593Smuzhiyun 		if (ret_val)
3123*4882a593Smuzhiyun 			return ret_val;
3124*4882a593Smuzhiyun 
3125*4882a593Smuzhiyun 		if (!link)
3126*4882a593Smuzhiyun 			e_dbg("Link taking longer than expected.\n");
3127*4882a593Smuzhiyun 
3128*4882a593Smuzhiyun 		/* Try once more */
3129*4882a593Smuzhiyun 		ret_val = e1000e_phy_has_link_generic(hw, PHY_FORCE_LIMIT,
3130*4882a593Smuzhiyun 						      100000, &link);
3131*4882a593Smuzhiyun 	}
3132*4882a593Smuzhiyun 
3133*4882a593Smuzhiyun 	return ret_val;
3134*4882a593Smuzhiyun }
3135*4882a593Smuzhiyun 
3136*4882a593Smuzhiyun /**
3137*4882a593Smuzhiyun  *  e1000_get_phy_info_82577 - Retrieve I82577 PHY information
3138*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
3139*4882a593Smuzhiyun  *
3140*4882a593Smuzhiyun  *  Read PHY status to determine if link is up.  If link is up, then
3141*4882a593Smuzhiyun  *  set/determine 10base-T extended distance and polarity correction.  Read
3142*4882a593Smuzhiyun  *  PHY port status to determine MDI/MDIx and speed.  Based on the speed,
3143*4882a593Smuzhiyun  *  determine on the cable length, local and remote receiver.
3144*4882a593Smuzhiyun  **/
e1000_get_phy_info_82577(struct e1000_hw * hw)3145*4882a593Smuzhiyun s32 e1000_get_phy_info_82577(struct e1000_hw *hw)
3146*4882a593Smuzhiyun {
3147*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
3148*4882a593Smuzhiyun 	s32 ret_val;
3149*4882a593Smuzhiyun 	u16 data;
3150*4882a593Smuzhiyun 	bool link;
3151*4882a593Smuzhiyun 
3152*4882a593Smuzhiyun 	ret_val = e1000e_phy_has_link_generic(hw, 1, 0, &link);
3153*4882a593Smuzhiyun 	if (ret_val)
3154*4882a593Smuzhiyun 		return ret_val;
3155*4882a593Smuzhiyun 
3156*4882a593Smuzhiyun 	if (!link) {
3157*4882a593Smuzhiyun 		e_dbg("Phy info is only valid if link is up\n");
3158*4882a593Smuzhiyun 		return -E1000_ERR_CONFIG;
3159*4882a593Smuzhiyun 	}
3160*4882a593Smuzhiyun 
3161*4882a593Smuzhiyun 	phy->polarity_correction = true;
3162*4882a593Smuzhiyun 
3163*4882a593Smuzhiyun 	ret_val = e1000_check_polarity_82577(hw);
3164*4882a593Smuzhiyun 	if (ret_val)
3165*4882a593Smuzhiyun 		return ret_val;
3166*4882a593Smuzhiyun 
3167*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, I82577_PHY_STATUS_2, &data);
3168*4882a593Smuzhiyun 	if (ret_val)
3169*4882a593Smuzhiyun 		return ret_val;
3170*4882a593Smuzhiyun 
3171*4882a593Smuzhiyun 	phy->is_mdix = !!(data & I82577_PHY_STATUS2_MDIX);
3172*4882a593Smuzhiyun 
3173*4882a593Smuzhiyun 	if ((data & I82577_PHY_STATUS2_SPEED_MASK) ==
3174*4882a593Smuzhiyun 	    I82577_PHY_STATUS2_SPEED_1000MBPS) {
3175*4882a593Smuzhiyun 		ret_val = hw->phy.ops.get_cable_length(hw);
3176*4882a593Smuzhiyun 		if (ret_val)
3177*4882a593Smuzhiyun 			return ret_val;
3178*4882a593Smuzhiyun 
3179*4882a593Smuzhiyun 		ret_val = e1e_rphy(hw, MII_STAT1000, &data);
3180*4882a593Smuzhiyun 		if (ret_val)
3181*4882a593Smuzhiyun 			return ret_val;
3182*4882a593Smuzhiyun 
3183*4882a593Smuzhiyun 		phy->local_rx = (data & LPA_1000LOCALRXOK)
3184*4882a593Smuzhiyun 		    ? e1000_1000t_rx_status_ok : e1000_1000t_rx_status_not_ok;
3185*4882a593Smuzhiyun 
3186*4882a593Smuzhiyun 		phy->remote_rx = (data & LPA_1000REMRXOK)
3187*4882a593Smuzhiyun 		    ? e1000_1000t_rx_status_ok : e1000_1000t_rx_status_not_ok;
3188*4882a593Smuzhiyun 	} else {
3189*4882a593Smuzhiyun 		phy->cable_length = E1000_CABLE_LENGTH_UNDEFINED;
3190*4882a593Smuzhiyun 		phy->local_rx = e1000_1000t_rx_status_undefined;
3191*4882a593Smuzhiyun 		phy->remote_rx = e1000_1000t_rx_status_undefined;
3192*4882a593Smuzhiyun 	}
3193*4882a593Smuzhiyun 
3194*4882a593Smuzhiyun 	return 0;
3195*4882a593Smuzhiyun }
3196*4882a593Smuzhiyun 
3197*4882a593Smuzhiyun /**
3198*4882a593Smuzhiyun  *  e1000_get_cable_length_82577 - Determine cable length for 82577 PHY
3199*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
3200*4882a593Smuzhiyun  *
3201*4882a593Smuzhiyun  * Reads the diagnostic status register and verifies result is valid before
3202*4882a593Smuzhiyun  * placing it in the phy_cable_length field.
3203*4882a593Smuzhiyun  **/
e1000_get_cable_length_82577(struct e1000_hw * hw)3204*4882a593Smuzhiyun s32 e1000_get_cable_length_82577(struct e1000_hw *hw)
3205*4882a593Smuzhiyun {
3206*4882a593Smuzhiyun 	struct e1000_phy_info *phy = &hw->phy;
3207*4882a593Smuzhiyun 	s32 ret_val;
3208*4882a593Smuzhiyun 	u16 phy_data, length;
3209*4882a593Smuzhiyun 
3210*4882a593Smuzhiyun 	ret_val = e1e_rphy(hw, I82577_PHY_DIAG_STATUS, &phy_data);
3211*4882a593Smuzhiyun 	if (ret_val)
3212*4882a593Smuzhiyun 		return ret_val;
3213*4882a593Smuzhiyun 
3214*4882a593Smuzhiyun 	length = ((phy_data & I82577_DSTATUS_CABLE_LENGTH) >>
3215*4882a593Smuzhiyun 		  I82577_DSTATUS_CABLE_LENGTH_SHIFT);
3216*4882a593Smuzhiyun 
3217*4882a593Smuzhiyun 	if (length == E1000_CABLE_LENGTH_UNDEFINED)
3218*4882a593Smuzhiyun 		return -E1000_ERR_PHY;
3219*4882a593Smuzhiyun 
3220*4882a593Smuzhiyun 	phy->cable_length = length;
3221*4882a593Smuzhiyun 
3222*4882a593Smuzhiyun 	return 0;
3223*4882a593Smuzhiyun }
3224