e1000: mark unused parameters
[dpdk.git] / lib / librte_pmd_e1000 / e1000 / e1000_phy.c
1 /*******************************************************************************
2
3 Copyright (c) 2001-2012, Intel Corporation
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8
9  1. Redistributions of source code must retain the above copyright notice,
10     this list of conditions and the following disclaimer.
11
12  2. Redistributions in binary form must reproduce the above copyright
13     notice, this list of conditions and the following disclaimer in the
14     documentation and/or other materials provided with the distribution.
15
16  3. Neither the name of the Intel Corporation nor the names of its
17     contributors may be used to endorse or promote products derived from
18     this software without specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
31
32 ***************************************************************************/
33
34 #include "e1000_api.h"
35
36 STATIC s32 e1000_access_phy_wakeup_reg_bm(struct e1000_hw *hw, u32 offset,
37                                           u16 *data, bool read, bool page_set);
38 STATIC u32 e1000_get_phy_addr_for_hv_page(u32 page);
39 STATIC s32 e1000_access_phy_debug_regs_hv(struct e1000_hw *hw, u32 offset,
40                                           u16 *data, bool read);
41
42 /* Cable length tables */
43 static const u16 e1000_m88_cable_length_table[] = {
44         0, 50, 80, 110, 140, 140, E1000_CABLE_LENGTH_UNDEFINED };
45 #define M88E1000_CABLE_LENGTH_TABLE_SIZE \
46                 (sizeof(e1000_m88_cable_length_table) / \
47                  sizeof(e1000_m88_cable_length_table[0]))
48
49 static const u16 e1000_igp_2_cable_length_table[] = {
50         0, 0, 0, 0, 0, 0, 0, 0, 3, 5, 8, 11, 13, 16, 18, 21, 0, 0, 0, 3,
51         6, 10, 13, 16, 19, 23, 26, 29, 32, 35, 38, 41, 6, 10, 14, 18, 22,
52         26, 30, 33, 37, 41, 44, 48, 51, 54, 58, 61, 21, 26, 31, 35, 40,
53         44, 49, 53, 57, 61, 65, 68, 72, 75, 79, 82, 40, 45, 51, 56, 61,
54         66, 70, 75, 79, 83, 87, 91, 94, 98, 101, 104, 60, 66, 72, 77, 82,
55         87, 92, 96, 100, 104, 108, 111, 114, 117, 119, 121, 83, 89, 95,
56         100, 105, 109, 113, 116, 119, 122, 124, 104, 109, 114, 118, 121,
57         124};
58 #define IGP02E1000_CABLE_LENGTH_TABLE_SIZE \
59                 (sizeof(e1000_igp_2_cable_length_table) / \
60                  sizeof(e1000_igp_2_cable_length_table[0]))
61
62 /**
63  *  e1000_init_phy_ops_generic - Initialize PHY function pointers
64  *  @hw: pointer to the HW structure
65  *
66  *  Setups up the function pointers to no-op functions
67  **/
68 void e1000_init_phy_ops_generic(struct e1000_hw *hw)
69 {
70         struct e1000_phy_info *phy = &hw->phy;
71         DEBUGFUNC("e1000_init_phy_ops_generic");
72
73         /* Initialize function pointers */
74         phy->ops.init_params = e1000_null_ops_generic;
75         phy->ops.acquire = e1000_null_ops_generic;
76         phy->ops.check_polarity = e1000_null_ops_generic;
77         phy->ops.check_reset_block = e1000_null_ops_generic;
78         phy->ops.commit = e1000_null_ops_generic;
79         phy->ops.force_speed_duplex = e1000_null_ops_generic;
80         phy->ops.get_cfg_done = e1000_null_ops_generic;
81         phy->ops.get_cable_length = e1000_null_ops_generic;
82         phy->ops.get_info = e1000_null_ops_generic;
83         phy->ops.set_page = e1000_null_set_page;
84         phy->ops.read_reg = e1000_null_read_reg;
85         phy->ops.read_reg_locked = e1000_null_read_reg;
86         phy->ops.read_reg_page = e1000_null_read_reg;
87         phy->ops.release = e1000_null_phy_generic;
88         phy->ops.reset = e1000_null_ops_generic;
89         phy->ops.set_d0_lplu_state = e1000_null_lplu_state;
90         phy->ops.set_d3_lplu_state = e1000_null_lplu_state;
91         phy->ops.write_reg = e1000_null_write_reg;
92         phy->ops.write_reg_locked = e1000_null_write_reg;
93         phy->ops.write_reg_page = e1000_null_write_reg;
94         phy->ops.power_up = e1000_null_phy_generic;
95         phy->ops.power_down = e1000_null_phy_generic;
96         phy->ops.read_i2c_byte = e1000_read_i2c_byte_null;
97         phy->ops.write_i2c_byte = e1000_write_i2c_byte_null;
98         phy->ops.cfg_on_link_up = e1000_null_ops_generic;
99 }
100
101 /**
102  *  e1000_null_set_page - No-op function, return 0
103  *  @hw: pointer to the HW structure
104  **/
105 s32 e1000_null_set_page(struct e1000_hw E1000_UNUSEDARG *hw,
106                         u16 E1000_UNUSEDARG data)
107 {
108         DEBUGFUNC("e1000_null_set_page");
109         UNREFERENCED_2PARAMETER(hw, data);
110         return E1000_SUCCESS;
111 }
112
113 /**
114  *  e1000_null_read_reg - No-op function, return 0
115  *  @hw: pointer to the HW structure
116  **/
117 s32 e1000_null_read_reg(struct e1000_hw E1000_UNUSEDARG *hw,
118                         u32 E1000_UNUSEDARG offset, u16 E1000_UNUSEDARG *data)
119 {
120         DEBUGFUNC("e1000_null_read_reg");
121         UNREFERENCED_3PARAMETER(hw, offset, data);
122         return E1000_SUCCESS;
123 }
124
125 /**
126  *  e1000_null_phy_generic - No-op function, return void
127  *  @hw: pointer to the HW structure
128  **/
129 void e1000_null_phy_generic(struct e1000_hw E1000_UNUSEDARG *hw)
130 {
131         DEBUGFUNC("e1000_null_phy_generic");
132         UNREFERENCED_1PARAMETER(hw);
133         return;
134 }
135
136 /**
137  *  e1000_null_lplu_state - No-op function, return 0
138  *  @hw: pointer to the HW structure
139  **/
140 s32 e1000_null_lplu_state(struct e1000_hw E1000_UNUSEDARG *hw,
141                           bool E1000_UNUSEDARG active)
142 {
143         DEBUGFUNC("e1000_null_lplu_state");
144         UNREFERENCED_2PARAMETER(hw, active);
145         return E1000_SUCCESS;
146 }
147
148 /**
149  *  e1000_null_write_reg - No-op function, return 0
150  *  @hw: pointer to the HW structure
151  **/
152 s32 e1000_null_write_reg(struct e1000_hw E1000_UNUSEDARG *hw,
153                          u32 E1000_UNUSEDARG offset, u16 E1000_UNUSEDARG data)
154 {
155         DEBUGFUNC("e1000_null_write_reg");
156         UNREFERENCED_3PARAMETER(hw, offset, data);
157         return E1000_SUCCESS;
158 }
159
160 /**
161  *  e1000_read_i2c_byte_null - No-op function, return 0
162  *  @hw: pointer to hardware structure
163  *  @byte_offset: byte offset to write
164  *  @dev_addr: device address
165  *  @data: data value read
166  *
167  **/
168 s32 e1000_read_i2c_byte_null(struct e1000_hw E1000_UNUSEDARG *hw,
169                              u8 E1000_UNUSEDARG byte_offset,
170                              u8 E1000_UNUSEDARG dev_addr,
171                              u8 E1000_UNUSEDARG *data)
172 {
173         DEBUGFUNC("e1000_read_i2c_byte_null");
174         UNREFERENCED_4PARAMETER(hw, byte_offset, dev_addr, data);
175         return E1000_SUCCESS;
176 }
177
178 /**
179  *  e1000_write_i2c_byte_null - No-op function, return 0
180  *  @hw: pointer to hardware structure
181  *  @byte_offset: byte offset to write
182  *  @dev_addr: device address
183  *  @data: data value to write
184  *
185  **/
186 s32 e1000_write_i2c_byte_null(struct e1000_hw E1000_UNUSEDARG *hw,
187                               u8 E1000_UNUSEDARG byte_offset,
188                               u8 E1000_UNUSEDARG dev_addr,
189                               u8 E1000_UNUSEDARG data)
190 {
191         DEBUGFUNC("e1000_write_i2c_byte_null");
192         UNREFERENCED_4PARAMETER(hw, byte_offset, dev_addr, data);
193         return E1000_SUCCESS;
194 }
195
196 /**
197  *  e1000_check_reset_block_generic - Check if PHY reset is blocked
198  *  @hw: pointer to the HW structure
199  *
200  *  Read the PHY management control register and check whether a PHY reset
201  *  is blocked.  If a reset is not blocked return E1000_SUCCESS, otherwise
202  *  return E1000_BLK_PHY_RESET (12).
203  **/
204 s32 e1000_check_reset_block_generic(struct e1000_hw *hw)
205 {
206         u32 manc;
207
208         DEBUGFUNC("e1000_check_reset_block");
209
210         manc = E1000_READ_REG(hw, E1000_MANC);
211
212         return (manc & E1000_MANC_BLK_PHY_RST_ON_IDE) ?
213                E1000_BLK_PHY_RESET : E1000_SUCCESS;
214 }
215
216 /**
217  *  e1000_get_phy_id - Retrieve the PHY ID and revision
218  *  @hw: pointer to the HW structure
219  *
220  *  Reads the PHY registers and stores the PHY ID and possibly the PHY
221  *  revision in the hardware structure.
222  **/
223 s32 e1000_get_phy_id(struct e1000_hw *hw)
224 {
225         struct e1000_phy_info *phy = &hw->phy;
226         s32 ret_val = E1000_SUCCESS;
227         u16 phy_id;
228         u16 retry_count = 0;
229
230         DEBUGFUNC("e1000_get_phy_id");
231
232         if (!phy->ops.read_reg)
233                 return E1000_SUCCESS;
234
235         while (retry_count < 2) {
236                 ret_val = phy->ops.read_reg(hw, PHY_ID1, &phy_id);
237                 if (ret_val)
238                         return ret_val;
239
240                 phy->id = (u32)(phy_id << 16);
241                 usec_delay(20);
242                 ret_val = phy->ops.read_reg(hw, PHY_ID2, &phy_id);
243                 if (ret_val)
244                         return ret_val;
245
246                 phy->id |= (u32)(phy_id & PHY_REVISION_MASK);
247                 phy->revision = (u32)(phy_id & ~PHY_REVISION_MASK);
248
249                 if (phy->id != 0 && phy->id != PHY_REVISION_MASK)
250                         return E1000_SUCCESS;
251
252                 retry_count++;
253         }
254
255         return E1000_SUCCESS;
256 }
257
258 /**
259  *  e1000_phy_reset_dsp_generic - Reset PHY DSP
260  *  @hw: pointer to the HW structure
261  *
262  *  Reset the digital signal processor.
263  **/
264 s32 e1000_phy_reset_dsp_generic(struct e1000_hw *hw)
265 {
266         s32 ret_val;
267
268         DEBUGFUNC("e1000_phy_reset_dsp_generic");
269
270         if (!hw->phy.ops.write_reg)
271                 return E1000_SUCCESS;
272
273         ret_val = hw->phy.ops.write_reg(hw, M88E1000_PHY_GEN_CONTROL, 0xC1);
274         if (ret_val)
275                 return ret_val;
276
277         return hw->phy.ops.write_reg(hw, M88E1000_PHY_GEN_CONTROL, 0);
278 }
279
280 /**
281  *  e1000_read_phy_reg_mdic - Read MDI control register
282  *  @hw: pointer to the HW structure
283  *  @offset: register offset to be read
284  *  @data: pointer to the read data
285  *
286  *  Reads the MDI control register in the PHY at offset and stores the
287  *  information read to data.
288  **/
289 s32 e1000_read_phy_reg_mdic(struct e1000_hw *hw, u32 offset, u16 *data)
290 {
291         struct e1000_phy_info *phy = &hw->phy;
292         u32 i, mdic = 0;
293
294         DEBUGFUNC("e1000_read_phy_reg_mdic");
295
296         if (offset > MAX_PHY_REG_ADDRESS) {
297                 DEBUGOUT1("PHY Address %d is out of range\n", offset);
298                 return -E1000_ERR_PARAM;
299         }
300
301         /* Set up Op-code, Phy Address, and register offset in the MDI
302          * Control register.  The MAC will take care of interfacing with the
303          * PHY to retrieve the desired data.
304          */
305         mdic = ((offset << E1000_MDIC_REG_SHIFT) |
306                 (phy->addr << E1000_MDIC_PHY_SHIFT) |
307                 (E1000_MDIC_OP_READ));
308
309         E1000_WRITE_REG(hw, E1000_MDIC, mdic);
310
311         /* Poll the ready bit to see if the MDI read completed
312          * Increasing the time out as testing showed failures with
313          * the lower time out
314          */
315         for (i = 0; i < (E1000_GEN_POLL_TIMEOUT * 3); i++) {
316                 usec_delay(50);
317                 mdic = E1000_READ_REG(hw, E1000_MDIC);
318                 if (mdic & E1000_MDIC_READY)
319                         break;
320         }
321         if (!(mdic & E1000_MDIC_READY)) {
322                 DEBUGOUT("MDI Read did not complete\n");
323                 return -E1000_ERR_PHY;
324         }
325         if (mdic & E1000_MDIC_ERROR) {
326                 DEBUGOUT("MDI Error\n");
327                 return -E1000_ERR_PHY;
328         }
329         *data = (u16) mdic;
330
331         /* Allow some time after each MDIC transaction to avoid
332          * reading duplicate data in the next MDIC transaction.
333          */
334         if (hw->mac.type == e1000_pch2lan)
335                 usec_delay(100);
336
337         return E1000_SUCCESS;
338 }
339
340 /**
341  *  e1000_write_phy_reg_mdic - Write MDI control register
342  *  @hw: pointer to the HW structure
343  *  @offset: register offset to write to
344  *  @data: data to write to register at offset
345  *
346  *  Writes data to MDI control register in the PHY at offset.
347  **/
348 s32 e1000_write_phy_reg_mdic(struct e1000_hw *hw, u32 offset, u16 data)
349 {
350         struct e1000_phy_info *phy = &hw->phy;
351         u32 i, mdic = 0;
352
353         DEBUGFUNC("e1000_write_phy_reg_mdic");
354
355         if (offset > MAX_PHY_REG_ADDRESS) {
356                 DEBUGOUT1("PHY Address %d is out of range\n", offset);
357                 return -E1000_ERR_PARAM;
358         }
359
360         /* Set up Op-code, Phy Address, and register offset in the MDI
361          * Control register.  The MAC will take care of interfacing with the
362          * PHY to retrieve the desired data.
363          */
364         mdic = (((u32)data) |
365                 (offset << E1000_MDIC_REG_SHIFT) |
366                 (phy->addr << E1000_MDIC_PHY_SHIFT) |
367                 (E1000_MDIC_OP_WRITE));
368
369         E1000_WRITE_REG(hw, E1000_MDIC, mdic);
370
371         /* Poll the ready bit to see if the MDI read completed
372          * Increasing the time out as testing showed failures with
373          * the lower time out
374          */
375         for (i = 0; i < (E1000_GEN_POLL_TIMEOUT * 3); i++) {
376                 usec_delay(50);
377                 mdic = E1000_READ_REG(hw, E1000_MDIC);
378                 if (mdic & E1000_MDIC_READY)
379                         break;
380         }
381         if (!(mdic & E1000_MDIC_READY)) {
382                 DEBUGOUT("MDI Write did not complete\n");
383                 return -E1000_ERR_PHY;
384         }
385         if (mdic & E1000_MDIC_ERROR) {
386                 DEBUGOUT("MDI Error\n");
387                 return -E1000_ERR_PHY;
388         }
389
390         /* Allow some time after each MDIC transaction to avoid
391          * reading duplicate data in the next MDIC transaction.
392          */
393         if (hw->mac.type == e1000_pch2lan)
394                 usec_delay(100);
395
396         return E1000_SUCCESS;
397 }
398
399 /**
400  *  e1000_read_phy_reg_i2c - Read PHY register using i2c
401  *  @hw: pointer to the HW structure
402  *  @offset: register offset to be read
403  *  @data: pointer to the read data
404  *
405  *  Reads the PHY register at offset using the i2c interface and stores the
406  *  retrieved information in data.
407  **/
408 s32 e1000_read_phy_reg_i2c(struct e1000_hw *hw, u32 offset, u16 *data)
409 {
410         struct e1000_phy_info *phy = &hw->phy;
411         u32 i, i2ccmd = 0;
412
413         DEBUGFUNC("e1000_read_phy_reg_i2c");
414
415         /* Set up Op-code, Phy Address, and register address in the I2CCMD
416          * register.  The MAC will take care of interfacing with the
417          * PHY to retrieve the desired data.
418          */
419         i2ccmd = ((offset << E1000_I2CCMD_REG_ADDR_SHIFT) |
420                   (phy->addr << E1000_I2CCMD_PHY_ADDR_SHIFT) |
421                   (E1000_I2CCMD_OPCODE_READ));
422
423         E1000_WRITE_REG(hw, E1000_I2CCMD, i2ccmd);
424
425         /* Poll the ready bit to see if the I2C read completed */
426         for (i = 0; i < E1000_I2CCMD_PHY_TIMEOUT; i++) {
427                 usec_delay(50);
428                 i2ccmd = E1000_READ_REG(hw, E1000_I2CCMD);
429                 if (i2ccmd & E1000_I2CCMD_READY)
430                         break;
431         }
432         if (!(i2ccmd & E1000_I2CCMD_READY)) {
433                 DEBUGOUT("I2CCMD Read did not complete\n");
434                 return -E1000_ERR_PHY;
435         }
436         if (i2ccmd & E1000_I2CCMD_ERROR) {
437                 DEBUGOUT("I2CCMD Error bit set\n");
438                 return -E1000_ERR_PHY;
439         }
440
441         /* Need to byte-swap the 16-bit value. */
442         *data = ((i2ccmd >> 8) & 0x00FF) | ((i2ccmd << 8) & 0xFF00);
443
444         return E1000_SUCCESS;
445 }
446
447 /**
448  *  e1000_write_phy_reg_i2c - Write PHY register using i2c
449  *  @hw: pointer to the HW structure
450  *  @offset: register offset to write to
451  *  @data: data to write at register offset
452  *
453  *  Writes the data to PHY register at the offset using the i2c interface.
454  **/
455 s32 e1000_write_phy_reg_i2c(struct e1000_hw *hw, u32 offset, u16 data)
456 {
457         struct e1000_phy_info *phy = &hw->phy;
458         u32 i, i2ccmd = 0;
459         u16 phy_data_swapped;
460
461         DEBUGFUNC("e1000_write_phy_reg_i2c");
462
463         /* Prevent overwritting SFP I2C EEPROM which is at A0 address.*/
464         if ((hw->phy.addr == 0) || (hw->phy.addr > 7)) {
465                 DEBUGOUT1("PHY I2C Address %d is out of range.\n",
466                           hw->phy.addr);
467                 return -E1000_ERR_CONFIG;
468         }
469
470         /* Swap the data bytes for the I2C interface */
471         phy_data_swapped = ((data >> 8) & 0x00FF) | ((data << 8) & 0xFF00);
472
473         /* Set up Op-code, Phy Address, and register address in the I2CCMD
474          * register.  The MAC will take care of interfacing with the
475          * PHY to retrieve the desired data.
476          */
477         i2ccmd = ((offset << E1000_I2CCMD_REG_ADDR_SHIFT) |
478                   (phy->addr << E1000_I2CCMD_PHY_ADDR_SHIFT) |
479                   E1000_I2CCMD_OPCODE_WRITE |
480                   phy_data_swapped);
481
482         E1000_WRITE_REG(hw, E1000_I2CCMD, i2ccmd);
483
484         /* Poll the ready bit to see if the I2C read completed */
485         for (i = 0; i < E1000_I2CCMD_PHY_TIMEOUT; i++) {
486                 usec_delay(50);
487                 i2ccmd = E1000_READ_REG(hw, E1000_I2CCMD);
488                 if (i2ccmd & E1000_I2CCMD_READY)
489                         break;
490         }
491         if (!(i2ccmd & E1000_I2CCMD_READY)) {
492                 DEBUGOUT("I2CCMD Write did not complete\n");
493                 return -E1000_ERR_PHY;
494         }
495         if (i2ccmd & E1000_I2CCMD_ERROR) {
496                 DEBUGOUT("I2CCMD Error bit set\n");
497                 return -E1000_ERR_PHY;
498         }
499
500         return E1000_SUCCESS;
501 }
502
503 /**
504  *  e1000_read_sfp_data_byte - Reads SFP module data.
505  *  @hw: pointer to the HW structure
506  *  @offset: byte location offset to be read
507  *  @data: read data buffer pointer
508  *
509  *  Reads one byte from SFP module data stored
510  *  in SFP resided EEPROM memory or SFP diagnostic area.
511  *  Function should be called with
512  *  E1000_I2CCMD_SFP_DATA_ADDR(<byte offset>) for SFP module database access
513  *  E1000_I2CCMD_SFP_DIAG_ADDR(<byte offset>) for SFP diagnostics parameters
514  *  access
515  **/
516 s32 e1000_read_sfp_data_byte(struct e1000_hw *hw, u16 offset, u8 *data)
517 {
518         u32 i = 0;
519         u32 i2ccmd = 0;
520         u32 data_local = 0;
521
522         DEBUGFUNC("e1000_read_sfp_data_byte");
523
524         if (offset > E1000_I2CCMD_SFP_DIAG_ADDR(255)) {
525                 DEBUGOUT("I2CCMD command address exceeds upper limit\n");
526                 return -E1000_ERR_PHY;
527         }
528
529         /* Set up Op-code, EEPROM Address,in the I2CCMD
530          * register. The MAC will take care of interfacing with the
531          * EEPROM to retrieve the desired data.
532          */
533         i2ccmd = ((offset << E1000_I2CCMD_REG_ADDR_SHIFT) |
534                   E1000_I2CCMD_OPCODE_READ);
535
536         E1000_WRITE_REG(hw, E1000_I2CCMD, i2ccmd);
537
538         /* Poll the ready bit to see if the I2C read completed */
539         for (i = 0; i < E1000_I2CCMD_PHY_TIMEOUT; i++) {
540                 usec_delay(50);
541                 data_local = E1000_READ_REG(hw, E1000_I2CCMD);
542                 if (data_local & E1000_I2CCMD_READY)
543                         break;
544         }
545         if (!(data_local & E1000_I2CCMD_READY)) {
546                 DEBUGOUT("I2CCMD Read did not complete\n");
547                 return -E1000_ERR_PHY;
548         }
549         if (data_local & E1000_I2CCMD_ERROR) {
550                 DEBUGOUT("I2CCMD Error bit set\n");
551                 return -E1000_ERR_PHY;
552         }
553         *data = (u8) data_local & 0xFF;
554
555         return E1000_SUCCESS;
556 }
557
558 /**
559  *  e1000_write_sfp_data_byte - Writes SFP module data.
560  *  @hw: pointer to the HW structure
561  *  @offset: byte location offset to write to
562  *  @data: data to write
563  *
564  *  Writes one byte to SFP module data stored
565  *  in SFP resided EEPROM memory or SFP diagnostic area.
566  *  Function should be called with
567  *  E1000_I2CCMD_SFP_DATA_ADDR(<byte offset>) for SFP module database access
568  *  E1000_I2CCMD_SFP_DIAG_ADDR(<byte offset>) for SFP diagnostics parameters
569  *  access
570  **/
571 s32 e1000_write_sfp_data_byte(struct e1000_hw *hw, u16 offset, u8 data)
572 {
573         u32 i = 0;
574         u32 i2ccmd = 0;
575         u32 data_local = 0;
576
577         DEBUGFUNC("e1000_write_sfp_data_byte");
578
579         if (offset > E1000_I2CCMD_SFP_DIAG_ADDR(255)) {
580                 DEBUGOUT("I2CCMD command address exceeds upper limit\n");
581                 return -E1000_ERR_PHY;
582         }
583         /* The programming interface is 16 bits wide
584          * so we need to read the whole word first
585          * then update appropriate byte lane and write
586          * the updated word back.
587          */
588         /* Set up Op-code, EEPROM Address,in the I2CCMD
589          * register. The MAC will take care of interfacing
590          * with an EEPROM to write the data given.
591          */
592         i2ccmd = ((offset << E1000_I2CCMD_REG_ADDR_SHIFT) |
593                   E1000_I2CCMD_OPCODE_READ);
594         /* Set a command to read single word */
595         E1000_WRITE_REG(hw, E1000_I2CCMD, i2ccmd);
596         for (i = 0; i < E1000_I2CCMD_PHY_TIMEOUT; i++) {
597                 usec_delay(50);
598                 /* Poll the ready bit to see if lastly
599                  * launched I2C operation completed
600                  */
601                 i2ccmd = E1000_READ_REG(hw, E1000_I2CCMD);
602                 if (i2ccmd & E1000_I2CCMD_READY) {
603                         /* Check if this is READ or WRITE phase */
604                         if ((i2ccmd & E1000_I2CCMD_OPCODE_READ) ==
605                             E1000_I2CCMD_OPCODE_READ) {
606                                 /* Write the selected byte
607                                  * lane and update whole word
608                                  */
609                                 data_local = i2ccmd & 0xFF00;
610                                 data_local |= data;
611                                 i2ccmd = ((offset <<
612                                         E1000_I2CCMD_REG_ADDR_SHIFT) |
613                                         E1000_I2CCMD_OPCODE_WRITE | data_local);
614                                 E1000_WRITE_REG(hw, E1000_I2CCMD, i2ccmd);
615                         } else {
616                                 break;
617                         }
618                 }
619         }
620         if (!(i2ccmd & E1000_I2CCMD_READY)) {
621                 DEBUGOUT("I2CCMD Write did not complete\n");
622                 return -E1000_ERR_PHY;
623         }
624         if (i2ccmd & E1000_I2CCMD_ERROR) {
625                 DEBUGOUT("I2CCMD Error bit set\n");
626                 return -E1000_ERR_PHY;
627         }
628         return E1000_SUCCESS;
629 }
630
631 /**
632  *  e1000_read_phy_reg_m88 - Read m88 PHY register
633  *  @hw: pointer to the HW structure
634  *  @offset: register offset to be read
635  *  @data: pointer to the read data
636  *
637  *  Acquires semaphore, if necessary, then reads the PHY register at offset
638  *  and storing the retrieved information in data.  Release any acquired
639  *  semaphores before exiting.
640  **/
641 s32 e1000_read_phy_reg_m88(struct e1000_hw *hw, u32 offset, u16 *data)
642 {
643         s32 ret_val;
644
645         DEBUGFUNC("e1000_read_phy_reg_m88");
646
647         if (!hw->phy.ops.acquire)
648                 return E1000_SUCCESS;
649
650         ret_val = hw->phy.ops.acquire(hw);
651         if (ret_val)
652                 return ret_val;
653
654         ret_val = e1000_read_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & offset,
655                                           data);
656
657         hw->phy.ops.release(hw);
658
659         return ret_val;
660 }
661
662 /**
663  *  e1000_write_phy_reg_m88 - Write m88 PHY register
664  *  @hw: pointer to the HW structure
665  *  @offset: register offset to write to
666  *  @data: data to write at register offset
667  *
668  *  Acquires semaphore, if necessary, then writes the data to PHY register
669  *  at the offset.  Release any acquired semaphores before exiting.
670  **/
671 s32 e1000_write_phy_reg_m88(struct e1000_hw *hw, u32 offset, u16 data)
672 {
673         s32 ret_val;
674
675         DEBUGFUNC("e1000_write_phy_reg_m88");
676
677         if (!hw->phy.ops.acquire)
678                 return E1000_SUCCESS;
679
680         ret_val = hw->phy.ops.acquire(hw);
681         if (ret_val)
682                 return ret_val;
683
684         ret_val = e1000_write_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & offset,
685                                            data);
686
687         hw->phy.ops.release(hw);
688
689         return ret_val;
690 }
691
692 /**
693  *  e1000_set_page_igp - Set page as on IGP-like PHY(s)
694  *  @hw: pointer to the HW structure
695  *  @page: page to set (shifted left when necessary)
696  *
697  *  Sets PHY page required for PHY register access.  Assumes semaphore is
698  *  already acquired.  Note, this function sets phy.addr to 1 so the caller
699  *  must set it appropriately (if necessary) after this function returns.
700  **/
701 s32 e1000_set_page_igp(struct e1000_hw *hw, u16 page)
702 {
703         DEBUGFUNC("e1000_set_page_igp");
704
705         DEBUGOUT1("Setting page 0x%x\n", page);
706
707         hw->phy.addr = 1;
708
709         return e1000_write_phy_reg_mdic(hw, IGP01E1000_PHY_PAGE_SELECT, page);
710 }
711
712 /**
713  *  __e1000_read_phy_reg_igp - Read igp PHY register
714  *  @hw: pointer to the HW structure
715  *  @offset: register offset to be read
716  *  @data: pointer to the read data
717  *  @locked: semaphore has already been acquired or not
718  *
719  *  Acquires semaphore, if necessary, then reads the PHY register at offset
720  *  and stores the retrieved information in data.  Release any acquired
721  *  semaphores before exiting.
722  **/
723 static s32 __e1000_read_phy_reg_igp(struct e1000_hw *hw, u32 offset, u16 *data,
724                                     bool locked)
725 {
726         s32 ret_val = E1000_SUCCESS;
727
728         DEBUGFUNC("__e1000_read_phy_reg_igp");
729
730         if (!locked) {
731                 if (!hw->phy.ops.acquire)
732                         return E1000_SUCCESS;
733
734                 ret_val = hw->phy.ops.acquire(hw);
735                 if (ret_val)
736                         return ret_val;
737         }
738
739         if (offset > MAX_PHY_MULTI_PAGE_REG)
740                 ret_val = e1000_write_phy_reg_mdic(hw,
741                                                    IGP01E1000_PHY_PAGE_SELECT,
742                                                    (u16)offset);
743         if (!ret_val)
744                 ret_val = e1000_read_phy_reg_mdic(hw,
745                                                   MAX_PHY_REG_ADDRESS & offset,
746                                                   data);
747         if (!locked)
748                 hw->phy.ops.release(hw);
749
750         return ret_val;
751 }
752
753 /**
754  *  e1000_read_phy_reg_igp - Read igp PHY register
755  *  @hw: pointer to the HW structure
756  *  @offset: register offset to be read
757  *  @data: pointer to the read data
758  *
759  *  Acquires semaphore then reads the PHY register at offset and stores the
760  *  retrieved information in data.
761  *  Release the acquired semaphore before exiting.
762  **/
763 s32 e1000_read_phy_reg_igp(struct e1000_hw *hw, u32 offset, u16 *data)
764 {
765         return __e1000_read_phy_reg_igp(hw, offset, data, false);
766 }
767
768 /**
769  *  e1000_read_phy_reg_igp_locked - Read igp PHY register
770  *  @hw: pointer to the HW structure
771  *  @offset: register offset to be read
772  *  @data: pointer to the read data
773  *
774  *  Reads the PHY register at offset and stores the retrieved information
775  *  in data.  Assumes semaphore already acquired.
776  **/
777 s32 e1000_read_phy_reg_igp_locked(struct e1000_hw *hw, u32 offset, u16 *data)
778 {
779         return __e1000_read_phy_reg_igp(hw, offset, data, true);
780 }
781
782 /**
783  *  e1000_write_phy_reg_igp - Write igp PHY register
784  *  @hw: pointer to the HW structure
785  *  @offset: register offset to write to
786  *  @data: data to write at register offset
787  *  @locked: semaphore has already been acquired or not
788  *
789  *  Acquires semaphore, if necessary, then writes the data to PHY register
790  *  at the offset.  Release any acquired semaphores before exiting.
791  **/
792 static s32 __e1000_write_phy_reg_igp(struct e1000_hw *hw, u32 offset, u16 data,
793                                      bool locked)
794 {
795         s32 ret_val = E1000_SUCCESS;
796
797         DEBUGFUNC("e1000_write_phy_reg_igp");
798
799         if (!locked) {
800                 if (!hw->phy.ops.acquire)
801                         return E1000_SUCCESS;
802
803                 ret_val = hw->phy.ops.acquire(hw);
804                 if (ret_val)
805                         return ret_val;
806         }
807
808         if (offset > MAX_PHY_MULTI_PAGE_REG)
809                 ret_val = e1000_write_phy_reg_mdic(hw,
810                                                    IGP01E1000_PHY_PAGE_SELECT,
811                                                    (u16)offset);
812         if (!ret_val)
813                 ret_val = e1000_write_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS &
814                                                        offset,
815                                                    data);
816         if (!locked)
817                 hw->phy.ops.release(hw);
818
819         return ret_val;
820 }
821
822 /**
823  *  e1000_write_phy_reg_igp - Write igp PHY register
824  *  @hw: pointer to the HW structure
825  *  @offset: register offset to write to
826  *  @data: data to write at register offset
827  *
828  *  Acquires semaphore then writes the data to PHY register
829  *  at the offset.  Release any acquired semaphores before exiting.
830  **/
831 s32 e1000_write_phy_reg_igp(struct e1000_hw *hw, u32 offset, u16 data)
832 {
833         return __e1000_write_phy_reg_igp(hw, offset, data, false);
834 }
835
836 /**
837  *  e1000_write_phy_reg_igp_locked - Write igp PHY register
838  *  @hw: pointer to the HW structure
839  *  @offset: register offset to write to
840  *  @data: data to write at register offset
841  *
842  *  Writes the data to PHY register at the offset.
843  *  Assumes semaphore already acquired.
844  **/
845 s32 e1000_write_phy_reg_igp_locked(struct e1000_hw *hw, u32 offset, u16 data)
846 {
847         return __e1000_write_phy_reg_igp(hw, offset, data, true);
848 }
849
850 /**
851  *  __e1000_read_kmrn_reg - Read kumeran register
852  *  @hw: pointer to the HW structure
853  *  @offset: register offset to be read
854  *  @data: pointer to the read data
855  *  @locked: semaphore has already been acquired or not
856  *
857  *  Acquires semaphore, if necessary.  Then reads the PHY register at offset
858  *  using the kumeran interface.  The information retrieved is stored in data.
859  *  Release any acquired semaphores before exiting.
860  **/
861 static s32 __e1000_read_kmrn_reg(struct e1000_hw *hw, u32 offset, u16 *data,
862                                  bool locked)
863 {
864         u32 kmrnctrlsta;
865
866         DEBUGFUNC("__e1000_read_kmrn_reg");
867
868         if (!locked) {
869                 s32 ret_val = E1000_SUCCESS;
870
871                 if (!hw->phy.ops.acquire)
872                         return E1000_SUCCESS;
873
874                 ret_val = hw->phy.ops.acquire(hw);
875                 if (ret_val)
876                         return ret_val;
877         }
878
879         kmrnctrlsta = ((offset << E1000_KMRNCTRLSTA_OFFSET_SHIFT) &
880                        E1000_KMRNCTRLSTA_OFFSET) | E1000_KMRNCTRLSTA_REN;
881         E1000_WRITE_REG(hw, E1000_KMRNCTRLSTA, kmrnctrlsta);
882         E1000_WRITE_FLUSH(hw);
883
884         usec_delay(2);
885
886         kmrnctrlsta = E1000_READ_REG(hw, E1000_KMRNCTRLSTA);
887         *data = (u16)kmrnctrlsta;
888
889         if (!locked)
890                 hw->phy.ops.release(hw);
891
892         return E1000_SUCCESS;
893 }
894
895 /**
896  *  e1000_read_kmrn_reg_generic -  Read kumeran register
897  *  @hw: pointer to the HW structure
898  *  @offset: register offset to be read
899  *  @data: pointer to the read data
900  *
901  *  Acquires semaphore then reads the PHY register at offset using the
902  *  kumeran interface.  The information retrieved is stored in data.
903  *  Release the acquired semaphore before exiting.
904  **/
905 s32 e1000_read_kmrn_reg_generic(struct e1000_hw *hw, u32 offset, u16 *data)
906 {
907         return __e1000_read_kmrn_reg(hw, offset, data, false);
908 }
909
910 /**
911  *  e1000_read_kmrn_reg_locked -  Read kumeran register
912  *  @hw: pointer to the HW structure
913  *  @offset: register offset to be read
914  *  @data: pointer to the read data
915  *
916  *  Reads the PHY register at offset using the kumeran interface.  The
917  *  information retrieved is stored in data.
918  *  Assumes semaphore already acquired.
919  **/
920 s32 e1000_read_kmrn_reg_locked(struct e1000_hw *hw, u32 offset, u16 *data)
921 {
922         return __e1000_read_kmrn_reg(hw, offset, data, true);
923 }
924
925 /**
926  *  __e1000_write_kmrn_reg - Write kumeran register
927  *  @hw: pointer to the HW structure
928  *  @offset: register offset to write to
929  *  @data: data to write at register offset
930  *  @locked: semaphore has already been acquired or not
931  *
932  *  Acquires semaphore, if necessary.  Then write the data to PHY register
933  *  at the offset using the kumeran interface.  Release any acquired semaphores
934  *  before exiting.
935  **/
936 static s32 __e1000_write_kmrn_reg(struct e1000_hw *hw, u32 offset, u16 data,
937                                   bool locked)
938 {
939         u32 kmrnctrlsta;
940
941         DEBUGFUNC("e1000_write_kmrn_reg_generic");
942
943         if (!locked) {
944                 s32 ret_val = E1000_SUCCESS;
945
946                 if (!hw->phy.ops.acquire)
947                         return E1000_SUCCESS;
948
949                 ret_val = hw->phy.ops.acquire(hw);
950                 if (ret_val)
951                         return ret_val;
952         }
953
954         kmrnctrlsta = ((offset << E1000_KMRNCTRLSTA_OFFSET_SHIFT) &
955                        E1000_KMRNCTRLSTA_OFFSET) | data;
956         E1000_WRITE_REG(hw, E1000_KMRNCTRLSTA, kmrnctrlsta);
957         E1000_WRITE_FLUSH(hw);
958
959         usec_delay(2);
960
961         if (!locked)
962                 hw->phy.ops.release(hw);
963
964         return E1000_SUCCESS;
965 }
966
967 /**
968  *  e1000_write_kmrn_reg_generic -  Write kumeran register
969  *  @hw: pointer to the HW structure
970  *  @offset: register offset to write to
971  *  @data: data to write at register offset
972  *
973  *  Acquires semaphore then writes the data to the PHY register at the offset
974  *  using the kumeran interface.  Release the acquired semaphore before exiting.
975  **/
976 s32 e1000_write_kmrn_reg_generic(struct e1000_hw *hw, u32 offset, u16 data)
977 {
978         return __e1000_write_kmrn_reg(hw, offset, data, false);
979 }
980
981 /**
982  *  e1000_write_kmrn_reg_locked -  Write kumeran register
983  *  @hw: pointer to the HW structure
984  *  @offset: register offset to write to
985  *  @data: data to write at register offset
986  *
987  *  Write the data to PHY register at the offset using the kumeran interface.
988  *  Assumes semaphore already acquired.
989  **/
990 s32 e1000_write_kmrn_reg_locked(struct e1000_hw *hw, u32 offset, u16 data)
991 {
992         return __e1000_write_kmrn_reg(hw, offset, data, true);
993 }
994
995 /**
996  *  e1000_set_master_slave_mode - Setup PHY for Master/slave mode
997  *  @hw: pointer to the HW structure
998  *
999  *  Sets up Master/slave mode
1000  **/
1001 STATIC s32 e1000_set_master_slave_mode(struct e1000_hw *hw)
1002 {
1003         s32 ret_val;
1004         u16 phy_data;
1005
1006         /* Resolve Master/Slave mode */
1007         ret_val = hw->phy.ops.read_reg(hw, PHY_1000T_CTRL, &phy_data);
1008         if (ret_val)
1009                 return ret_val;
1010
1011         /* load defaults for future use */
1012         hw->phy.original_ms_type = (phy_data & CR_1000T_MS_ENABLE) ?
1013                                    ((phy_data & CR_1000T_MS_VALUE) ?
1014                                     e1000_ms_force_master :
1015                                     e1000_ms_force_slave) : e1000_ms_auto;
1016
1017         switch (hw->phy.ms_type) {
1018         case e1000_ms_force_master:
1019                 phy_data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
1020                 break;
1021         case e1000_ms_force_slave:
1022                 phy_data |= CR_1000T_MS_ENABLE;
1023                 phy_data &= ~(CR_1000T_MS_VALUE);
1024                 break;
1025         case e1000_ms_auto:
1026                 phy_data &= ~CR_1000T_MS_ENABLE;
1027                 /* fall-through */
1028         default:
1029                 break;
1030         }
1031
1032         return hw->phy.ops.write_reg(hw, PHY_1000T_CTRL, phy_data);
1033 }
1034
1035 /**
1036  *  e1000_copper_link_setup_82577 - Setup 82577 PHY for copper link
1037  *  @hw: pointer to the HW structure
1038  *
1039  *  Sets up Carrier-sense on Transmit and downshift values.
1040  **/
1041 s32 e1000_copper_link_setup_82577(struct e1000_hw *hw)
1042 {
1043         s32 ret_val;
1044         u16 phy_data;
1045
1046         DEBUGFUNC("e1000_copper_link_setup_82577");
1047
1048         if (hw->phy.type == e1000_phy_82580) {
1049                 ret_val = hw->phy.ops.reset(hw);
1050                 if (ret_val) {
1051                         DEBUGOUT("Error resetting the PHY.\n");
1052                         return ret_val;
1053                 }
1054         }
1055
1056         /* Enable CRS on Tx. This must be set for half-duplex operation. */
1057         ret_val = hw->phy.ops.read_reg(hw, I82577_CFG_REG, &phy_data);
1058         if (ret_val)
1059                 return ret_val;
1060
1061         phy_data |= I82577_CFG_ASSERT_CRS_ON_TX;
1062
1063         /* Enable downshift */
1064         phy_data |= I82577_CFG_ENABLE_DOWNSHIFT;
1065
1066         ret_val = hw->phy.ops.write_reg(hw, I82577_CFG_REG, phy_data);
1067         if (ret_val)
1068                 return ret_val;
1069
1070         /* Set MDI/MDIX mode */
1071         ret_val = hw->phy.ops.read_reg(hw, I82577_PHY_CTRL_2, &phy_data);
1072         if (ret_val)
1073                 return ret_val;
1074         phy_data &= ~I82577_PHY_CTRL2_MDIX_CFG_MASK;
1075         /* Options:
1076          *   0 - Auto (default)
1077          *   1 - MDI mode
1078          *   2 - MDI-X mode
1079          */
1080         switch (hw->phy.mdix) {
1081         case 1:
1082                 break;
1083         case 2:
1084                 phy_data |= I82577_PHY_CTRL2_MANUAL_MDIX;
1085                 break;
1086         case 0:
1087         default:
1088                 phy_data |= I82577_PHY_CTRL2_AUTO_MDI_MDIX;
1089                 break;
1090         }
1091         ret_val = hw->phy.ops.write_reg(hw, I82577_PHY_CTRL_2, phy_data);
1092         if (ret_val)
1093                 return ret_val;
1094
1095         return e1000_set_master_slave_mode(hw);
1096 }
1097
1098 /**
1099  *  e1000_copper_link_setup_m88 - Setup m88 PHY's for copper link
1100  *  @hw: pointer to the HW structure
1101  *
1102  *  Sets up MDI/MDI-X and polarity for m88 PHY's.  If necessary, transmit clock
1103  *  and downshift values are set also.
1104  **/
1105 s32 e1000_copper_link_setup_m88(struct e1000_hw *hw)
1106 {
1107         struct e1000_phy_info *phy = &hw->phy;
1108         s32 ret_val;
1109         u16 phy_data;
1110
1111         DEBUGFUNC("e1000_copper_link_setup_m88");
1112
1113
1114         /* Enable CRS on Tx. This must be set for half-duplex operation. */
1115         ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1116         if (ret_val)
1117                 return ret_val;
1118
1119         /* For BM PHY this bit is downshift enable */
1120         if (phy->type != e1000_phy_bm)
1121                 phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
1122
1123         /* Options:
1124          *   MDI/MDI-X = 0 (default)
1125          *   0 - Auto for all speeds
1126          *   1 - MDI mode
1127          *   2 - MDI-X mode
1128          *   3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
1129          */
1130         phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
1131
1132         switch (phy->mdix) {
1133         case 1:
1134                 phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
1135                 break;
1136         case 2:
1137                 phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
1138                 break;
1139         case 3:
1140                 phy_data |= M88E1000_PSCR_AUTO_X_1000T;
1141                 break;
1142         case 0:
1143         default:
1144                 phy_data |= M88E1000_PSCR_AUTO_X_MODE;
1145                 break;
1146         }
1147
1148         /* Options:
1149          *   disable_polarity_correction = 0 (default)
1150          *       Automatic Correction for Reversed Cable Polarity
1151          *   0 - Disabled
1152          *   1 - Enabled
1153          */
1154         phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
1155         if (phy->disable_polarity_correction)
1156                 phy_data |= M88E1000_PSCR_POLARITY_REVERSAL;
1157
1158         /* Enable downshift on BM (disabled by default) */
1159         if (phy->type == e1000_phy_bm) {
1160                 /* For 82574/82583, first disable then enable downshift */
1161                 if (phy->id == BME1000_E_PHY_ID_R2) {
1162                         phy_data &= ~BME1000_PSCR_ENABLE_DOWNSHIFT;
1163                         ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL,
1164                                                      phy_data);
1165                         if (ret_val)
1166                                 return ret_val;
1167                         /* Commit the changes. */
1168                         ret_val = phy->ops.commit(hw);
1169                         if (ret_val) {
1170                                 DEBUGOUT("Error committing the PHY changes\n");
1171                                 return ret_val;
1172                         }
1173                 }
1174
1175                 phy_data |= BME1000_PSCR_ENABLE_DOWNSHIFT;
1176         }
1177
1178         ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1179         if (ret_val)
1180                 return ret_val;
1181
1182         if ((phy->type == e1000_phy_m88) &&
1183             (phy->revision < E1000_REVISION_4) &&
1184             (phy->id != BME1000_E_PHY_ID_R2)) {
1185                 /* Force TX_CLK in the Extended PHY Specific Control Register
1186                  * to 25MHz clock.
1187                  */
1188                 ret_val = phy->ops.read_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL,
1189                                             &phy_data);
1190                 if (ret_val)
1191                         return ret_val;
1192
1193                 phy_data |= M88E1000_EPSCR_TX_CLK_25;
1194
1195                 if ((phy->revision == E1000_REVISION_2) &&
1196                     (phy->id == M88E1111_I_PHY_ID)) {
1197                         /* 82573L PHY - set the downshift counter to 5x. */
1198                         phy_data &= ~M88EC018_EPSCR_DOWNSHIFT_COUNTER_MASK;
1199                         phy_data |= M88EC018_EPSCR_DOWNSHIFT_COUNTER_5X;
1200                 } else {
1201                         /* Configure Master and Slave downshift values */
1202                         phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK |
1203                                      M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
1204                         phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X |
1205                                      M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
1206                 }
1207                 ret_val = phy->ops.write_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL,
1208                                              phy_data);
1209                 if (ret_val)
1210                         return ret_val;
1211         }
1212
1213         if ((phy->type == e1000_phy_bm) && (phy->id == BME1000_E_PHY_ID_R2)) {
1214                 /* Set PHY page 0, register 29 to 0x0003 */
1215                 ret_val = phy->ops.write_reg(hw, 29, 0x0003);
1216                 if (ret_val)
1217                         return ret_val;
1218
1219                 /* Set PHY page 0, register 30 to 0x0000 */
1220                 ret_val = phy->ops.write_reg(hw, 30, 0x0000);
1221                 if (ret_val)
1222                         return ret_val;
1223         }
1224
1225         /* Commit the changes. */
1226         ret_val = phy->ops.commit(hw);
1227         if (ret_val) {
1228                 DEBUGOUT("Error committing the PHY changes\n");
1229                 return ret_val;
1230         }
1231
1232         if (phy->type == e1000_phy_82578) {
1233                 ret_val = phy->ops.read_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL,
1234                                             &phy_data);
1235                 if (ret_val)
1236                         return ret_val;
1237
1238                 /* 82578 PHY - set the downshift count to 1x. */
1239                 phy_data |= I82578_EPSCR_DOWNSHIFT_ENABLE;
1240                 phy_data &= ~I82578_EPSCR_DOWNSHIFT_COUNTER_MASK;
1241                 ret_val = phy->ops.write_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL,
1242                                              phy_data);
1243                 if (ret_val)
1244                         return ret_val;
1245         }
1246
1247         if (phy->type == e1000_phy_i210) {
1248                 ret_val = e1000_set_master_slave_mode(hw);
1249                 if (ret_val)
1250                         return ret_val;
1251         }
1252
1253         return E1000_SUCCESS;
1254 }
1255
1256 /**
1257  *  e1000_copper_link_setup_m88_gen2 - Setup m88 PHY's for copper link
1258  *  @hw: pointer to the HW structure
1259  *
1260  *  Sets up MDI/MDI-X and polarity for i347-AT4, m88e1322 and m88e1112 PHY's.
1261  *  Also enables and sets the downshift parameters.
1262  **/
1263 s32 e1000_copper_link_setup_m88_gen2(struct e1000_hw *hw)
1264 {
1265         struct e1000_phy_info *phy = &hw->phy;
1266         s32 ret_val;
1267         u16 phy_data;
1268
1269         DEBUGFUNC("e1000_copper_link_setup_m88_gen2");
1270
1271
1272         /* Enable CRS on Tx. This must be set for half-duplex operation. */
1273         ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1274         if (ret_val)
1275                 return ret_val;
1276
1277         /* Options:
1278          *   MDI/MDI-X = 0 (default)
1279          *   0 - Auto for all speeds
1280          *   1 - MDI mode
1281          *   2 - MDI-X mode
1282          *   3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
1283          */
1284         phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
1285
1286         switch (phy->mdix) {
1287         case 1:
1288                 phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
1289                 break;
1290         case 2:
1291                 phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
1292                 break;
1293         case 3:
1294                 /* M88E1112 does not support this mode) */
1295                 if (phy->id != M88E1112_E_PHY_ID) {
1296                         phy_data |= M88E1000_PSCR_AUTO_X_1000T;
1297                         break;
1298                 }
1299         case 0:
1300         default:
1301                 phy_data |= M88E1000_PSCR_AUTO_X_MODE;
1302                 break;
1303         }
1304
1305         /* Options:
1306          *   disable_polarity_correction = 0 (default)
1307          *       Automatic Correction for Reversed Cable Polarity
1308          *   0 - Disabled
1309          *   1 - Enabled
1310          */
1311         phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
1312         if (phy->disable_polarity_correction)
1313                 phy_data |= M88E1000_PSCR_POLARITY_REVERSAL;
1314
1315         /* Enable downshift and setting it to X6 */
1316         phy_data &= ~I347AT4_PSCR_DOWNSHIFT_MASK;
1317         phy_data |= I347AT4_PSCR_DOWNSHIFT_6X;
1318         phy_data |= I347AT4_PSCR_DOWNSHIFT_ENABLE;
1319
1320         ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1321         if (ret_val)
1322                 return ret_val;
1323
1324         /* Commit the changes. */
1325         ret_val = phy->ops.commit(hw);
1326         if (ret_val) {
1327                 DEBUGOUT("Error committing the PHY changes\n");
1328                 return ret_val;
1329         }
1330
1331         return E1000_SUCCESS;
1332 }
1333
1334 /**
1335  *  e1000_copper_link_setup_igp - Setup igp PHY's for copper link
1336  *  @hw: pointer to the HW structure
1337  *
1338  *  Sets up LPLU, MDI/MDI-X, polarity, Smartspeed and Master/Slave config for
1339  *  igp PHY's.
1340  **/
1341 s32 e1000_copper_link_setup_igp(struct e1000_hw *hw)
1342 {
1343         struct e1000_phy_info *phy = &hw->phy;
1344         s32 ret_val;
1345         u16 data;
1346
1347         DEBUGFUNC("e1000_copper_link_setup_igp");
1348
1349
1350         ret_val = hw->phy.ops.reset(hw);
1351         if (ret_val) {
1352                 DEBUGOUT("Error resetting the PHY.\n");
1353                 return ret_val;
1354         }
1355
1356         /* Wait 100ms for MAC to configure PHY from NVM settings, to avoid
1357          * timeout issues when LFS is enabled.
1358          */
1359         msec_delay(100);
1360
1361         /* The NVM settings will configure LPLU in D3 for
1362          * non-IGP1 PHYs.
1363          */
1364         if (phy->type == e1000_phy_igp) {
1365                 /* disable lplu d3 during driver init */
1366                 ret_val = hw->phy.ops.set_d3_lplu_state(hw, false);
1367                 if (ret_val) {
1368                         DEBUGOUT("Error Disabling LPLU D3\n");
1369                         return ret_val;
1370                 }
1371         }
1372
1373         /* disable lplu d0 during driver init */
1374         if (hw->phy.ops.set_d0_lplu_state) {
1375                 ret_val = hw->phy.ops.set_d0_lplu_state(hw, false);
1376                 if (ret_val) {
1377                         DEBUGOUT("Error Disabling LPLU D0\n");
1378                         return ret_val;
1379                 }
1380         }
1381         /* Configure mdi-mdix settings */
1382         ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_CTRL, &data);
1383         if (ret_val)
1384                 return ret_val;
1385
1386         data &= ~IGP01E1000_PSCR_AUTO_MDIX;
1387
1388         switch (phy->mdix) {
1389         case 1:
1390                 data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
1391                 break;
1392         case 2:
1393                 data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
1394                 break;
1395         case 0:
1396         default:
1397                 data |= IGP01E1000_PSCR_AUTO_MDIX;
1398                 break;
1399         }
1400         ret_val = phy->ops.write_reg(hw, IGP01E1000_PHY_PORT_CTRL, data);
1401         if (ret_val)
1402                 return ret_val;
1403
1404         /* set auto-master slave resolution settings */
1405         if (hw->mac.autoneg) {
1406                 /* when autonegotiation advertisement is only 1000Mbps then we
1407                  * should disable SmartSpeed and enable Auto MasterSlave
1408                  * resolution as hardware default.
1409                  */
1410                 if (phy->autoneg_advertised == ADVERTISE_1000_FULL) {
1411                         /* Disable SmartSpeed */
1412                         ret_val = phy->ops.read_reg(hw,
1413                                                     IGP01E1000_PHY_PORT_CONFIG,
1414                                                     &data);
1415                         if (ret_val)
1416                                 return ret_val;
1417
1418                         data &= ~IGP01E1000_PSCFR_SMART_SPEED;
1419                         ret_val = phy->ops.write_reg(hw,
1420                                                      IGP01E1000_PHY_PORT_CONFIG,
1421                                                      data);
1422                         if (ret_val)
1423                                 return ret_val;
1424
1425                         /* Set auto Master/Slave resolution process */
1426                         ret_val = phy->ops.read_reg(hw, PHY_1000T_CTRL, &data);
1427                         if (ret_val)
1428                                 return ret_val;
1429
1430                         data &= ~CR_1000T_MS_ENABLE;
1431                         ret_val = phy->ops.write_reg(hw, PHY_1000T_CTRL, data);
1432                         if (ret_val)
1433                                 return ret_val;
1434                 }
1435
1436                 ret_val = e1000_set_master_slave_mode(hw);
1437         }
1438
1439         return ret_val;
1440 }
1441
1442 /**
1443  *  e1000_phy_setup_autoneg - Configure PHY for auto-negotiation
1444  *  @hw: pointer to the HW structure
1445  *
1446  *  Reads the MII auto-neg advertisement register and/or the 1000T control
1447  *  register and if the PHY is already setup for auto-negotiation, then
1448  *  return successful.  Otherwise, setup advertisement and flow control to
1449  *  the appropriate values for the wanted auto-negotiation.
1450  **/
1451 s32 e1000_phy_setup_autoneg(struct e1000_hw *hw)
1452 {
1453         struct e1000_phy_info *phy = &hw->phy;
1454         s32 ret_val;
1455         u16 mii_autoneg_adv_reg;
1456         u16 mii_1000t_ctrl_reg = 0;
1457
1458         DEBUGFUNC("e1000_phy_setup_autoneg");
1459
1460         phy->autoneg_advertised &= phy->autoneg_mask;
1461
1462         /* Read the MII Auto-Neg Advertisement Register (Address 4). */
1463         ret_val = phy->ops.read_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
1464         if (ret_val)
1465                 return ret_val;
1466
1467         if (phy->autoneg_mask & ADVERTISE_1000_FULL) {
1468                 /* Read the MII 1000Base-T Control Register (Address 9). */
1469                 ret_val = phy->ops.read_reg(hw, PHY_1000T_CTRL,
1470                                             &mii_1000t_ctrl_reg);
1471                 if (ret_val)
1472                         return ret_val;
1473         }
1474
1475         /* Need to parse both autoneg_advertised and fc and set up
1476          * the appropriate PHY registers.  First we will parse for
1477          * autoneg_advertised software override.  Since we can advertise
1478          * a plethora of combinations, we need to check each bit
1479          * individually.
1480          */
1481
1482         /* First we clear all the 10/100 mb speed bits in the Auto-Neg
1483          * Advertisement Register (Address 4) and the 1000 mb speed bits in
1484          * the  1000Base-T Control Register (Address 9).
1485          */
1486         mii_autoneg_adv_reg &= ~(NWAY_AR_100TX_FD_CAPS |
1487                                  NWAY_AR_100TX_HD_CAPS |
1488                                  NWAY_AR_10T_FD_CAPS   |
1489                                  NWAY_AR_10T_HD_CAPS);
1490         mii_1000t_ctrl_reg &= ~(CR_1000T_HD_CAPS | CR_1000T_FD_CAPS);
1491
1492         DEBUGOUT1("autoneg_advertised %x\n", phy->autoneg_advertised);
1493
1494         /* Do we want to advertise 10 Mb Half Duplex? */
1495         if (phy->autoneg_advertised & ADVERTISE_10_HALF) {
1496                 DEBUGOUT("Advertise 10mb Half duplex\n");
1497                 mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
1498         }
1499
1500         /* Do we want to advertise 10 Mb Full Duplex? */
1501         if (phy->autoneg_advertised & ADVERTISE_10_FULL) {
1502                 DEBUGOUT("Advertise 10mb Full duplex\n");
1503                 mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
1504         }
1505
1506         /* Do we want to advertise 100 Mb Half Duplex? */
1507         if (phy->autoneg_advertised & ADVERTISE_100_HALF) {
1508                 DEBUGOUT("Advertise 100mb Half duplex\n");
1509                 mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
1510         }
1511
1512         /* Do we want to advertise 100 Mb Full Duplex? */
1513         if (phy->autoneg_advertised & ADVERTISE_100_FULL) {
1514                 DEBUGOUT("Advertise 100mb Full duplex\n");
1515                 mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
1516         }
1517
1518         /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
1519         if (phy->autoneg_advertised & ADVERTISE_1000_HALF)
1520                 DEBUGOUT("Advertise 1000mb Half duplex request denied!\n");
1521
1522         /* Do we want to advertise 1000 Mb Full Duplex? */
1523         if (phy->autoneg_advertised & ADVERTISE_1000_FULL) {
1524                 DEBUGOUT("Advertise 1000mb Full duplex\n");
1525                 mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
1526         }
1527
1528         /* Check for a software override of the flow control settings, and
1529          * setup the PHY advertisement registers accordingly.  If
1530          * auto-negotiation is enabled, then software will have to set the
1531          * "PAUSE" bits to the correct value in the Auto-Negotiation
1532          * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-
1533          * negotiation.
1534          *
1535          * The possible values of the "fc" parameter are:
1536          *      0:  Flow control is completely disabled
1537          *      1:  Rx flow control is enabled (we can receive pause frames
1538          *          but not send pause frames).
1539          *      2:  Tx flow control is enabled (we can send pause frames
1540          *          but we do not support receiving pause frames).
1541          *      3:  Both Rx and Tx flow control (symmetric) are enabled.
1542          *  other:  No software override.  The flow control configuration
1543          *          in the EEPROM is used.
1544          */
1545         switch (hw->fc.current_mode) {
1546         case e1000_fc_none:
1547                 /* Flow control (Rx & Tx) is completely disabled by a
1548                  * software over-ride.
1549                  */
1550                 mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1551                 break;
1552         case e1000_fc_rx_pause:
1553                 /* Rx Flow control is enabled, and Tx Flow control is
1554                  * disabled, by a software over-ride.
1555                  *
1556                  * Since there really isn't a way to advertise that we are
1557                  * capable of Rx Pause ONLY, we will advertise that we
1558                  * support both symmetric and asymmetric Rx PAUSE.  Later
1559                  * (in e1000_config_fc_after_link_up) we will disable the
1560                  * hw's ability to send PAUSE frames.
1561                  */
1562                 mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1563                 break;
1564         case e1000_fc_tx_pause:
1565                 /* Tx Flow control is enabled, and Rx Flow control is
1566                  * disabled, by a software over-ride.
1567                  */
1568                 mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
1569                 mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
1570                 break;
1571         case e1000_fc_full:
1572                 /* Flow control (both Rx and Tx) is enabled by a software
1573                  * over-ride.
1574                  */
1575                 mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1576                 break;
1577         default:
1578                 DEBUGOUT("Flow control param set incorrectly\n");
1579                 return -E1000_ERR_CONFIG;
1580         }
1581
1582         ret_val = phy->ops.write_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
1583         if (ret_val)
1584                 return ret_val;
1585
1586         DEBUGOUT1("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
1587
1588         if (phy->autoneg_mask & ADVERTISE_1000_FULL)
1589                 ret_val = phy->ops.write_reg(hw, PHY_1000T_CTRL,
1590                                              mii_1000t_ctrl_reg);
1591
1592         return ret_val;
1593 }
1594
1595 /**
1596  *  e1000_copper_link_autoneg - Setup/Enable autoneg for copper link
1597  *  @hw: pointer to the HW structure
1598  *
1599  *  Performs initial bounds checking on autoneg advertisement parameter, then
1600  *  configure to advertise the full capability.  Setup the PHY to autoneg
1601  *  and restart the negotiation process between the link partner.  If
1602  *  autoneg_wait_to_complete, then wait for autoneg to complete before exiting.
1603  **/
1604 s32 e1000_copper_link_autoneg(struct e1000_hw *hw)
1605 {
1606         struct e1000_phy_info *phy = &hw->phy;
1607         s32 ret_val;
1608         u16 phy_ctrl;
1609
1610         DEBUGFUNC("e1000_copper_link_autoneg");
1611
1612         /* Perform some bounds checking on the autoneg advertisement
1613          * parameter.
1614          */
1615         phy->autoneg_advertised &= phy->autoneg_mask;
1616
1617         /* If autoneg_advertised is zero, we assume it was not defaulted
1618          * by the calling code so we set to advertise full capability.
1619          */
1620         if (!phy->autoneg_advertised)
1621                 phy->autoneg_advertised = phy->autoneg_mask;
1622
1623         DEBUGOUT("Reconfiguring auto-neg advertisement params\n");
1624         ret_val = e1000_phy_setup_autoneg(hw);
1625         if (ret_val) {
1626                 DEBUGOUT("Error Setting up Auto-Negotiation\n");
1627                 return ret_val;
1628         }
1629         DEBUGOUT("Restarting Auto-Neg\n");
1630
1631         /* Restart auto-negotiation by setting the Auto Neg Enable bit and
1632          * the Auto Neg Restart bit in the PHY control register.
1633          */
1634         ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_ctrl);
1635         if (ret_val)
1636                 return ret_val;
1637
1638         phy_ctrl |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
1639         ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_ctrl);
1640         if (ret_val)
1641                 return ret_val;
1642
1643         /* Does the user want to wait for Auto-Neg to complete here, or
1644          * check at a later time (for example, callback routine).
1645          */
1646         if (phy->autoneg_wait_to_complete) {
1647                 ret_val = hw->mac.ops.wait_autoneg(hw);
1648                 if (ret_val) {
1649                         DEBUGOUT("Error while waiting for autoneg to complete\n");
1650                         return ret_val;
1651                 }
1652         }
1653
1654         hw->mac.get_link_status = true;
1655
1656         return ret_val;
1657 }
1658
1659 /**
1660  *  e1000_setup_copper_link_generic - Configure copper link settings
1661  *  @hw: pointer to the HW structure
1662  *
1663  *  Calls the appropriate function to configure the link for auto-neg or forced
1664  *  speed and duplex.  Then we check for link, once link is established calls
1665  *  to configure collision distance and flow control are called.  If link is
1666  *  not established, we return -E1000_ERR_PHY (-2).
1667  **/
1668 s32 e1000_setup_copper_link_generic(struct e1000_hw *hw)
1669 {
1670         s32 ret_val;
1671         bool link;
1672
1673         DEBUGFUNC("e1000_setup_copper_link_generic");
1674
1675         if (hw->mac.autoneg) {
1676                 /* Setup autoneg and flow control advertisement and perform
1677                  * autonegotiation.
1678                  */
1679                 ret_val = e1000_copper_link_autoneg(hw);
1680                 if (ret_val)
1681                         return ret_val;
1682         } else {
1683                 /* PHY will be set to 10H, 10F, 100H or 100F
1684                  * depending on user settings.
1685                  */
1686                 DEBUGOUT("Forcing Speed and Duplex\n");
1687                 ret_val = hw->phy.ops.force_speed_duplex(hw);
1688                 if (ret_val) {
1689                         DEBUGOUT("Error Forcing Speed and Duplex\n");
1690                         return ret_val;
1691                 }
1692         }
1693
1694         /* Check link status. Wait up to 100 microseconds for link to become
1695          * valid.
1696          */
1697         ret_val = e1000_phy_has_link_generic(hw, COPPER_LINK_UP_LIMIT, 10,
1698                                              &link);
1699         if (ret_val)
1700                 return ret_val;
1701
1702         if (link) {
1703                 DEBUGOUT("Valid link established!!!\n");
1704                 hw->mac.ops.config_collision_dist(hw);
1705                 ret_val = e1000_config_fc_after_link_up_generic(hw);
1706         } else {
1707                 DEBUGOUT("Unable to establish link!!!\n");
1708         }
1709
1710         return ret_val;
1711 }
1712
1713 /**
1714  *  e1000_phy_force_speed_duplex_igp - Force speed/duplex for igp PHY
1715  *  @hw: pointer to the HW structure
1716  *
1717  *  Calls the PHY setup function to force speed and duplex.  Clears the
1718  *  auto-crossover to force MDI manually.  Waits for link and returns
1719  *  successful if link up is successful, else -E1000_ERR_PHY (-2).
1720  **/
1721 s32 e1000_phy_force_speed_duplex_igp(struct e1000_hw *hw)
1722 {
1723         struct e1000_phy_info *phy = &hw->phy;
1724         s32 ret_val;
1725         u16 phy_data;
1726         bool link;
1727
1728         DEBUGFUNC("e1000_phy_force_speed_duplex_igp");
1729
1730         ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_data);
1731         if (ret_val)
1732                 return ret_val;
1733
1734         e1000_phy_force_speed_duplex_setup(hw, &phy_data);
1735
1736         ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_data);
1737         if (ret_val)
1738                 return ret_val;
1739
1740         /* Clear Auto-Crossover to force MDI manually.  IGP requires MDI
1741          * forced whenever speed and duplex are forced.
1742          */
1743         ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
1744         if (ret_val)
1745                 return ret_val;
1746
1747         phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
1748         phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
1749
1750         ret_val = phy->ops.write_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
1751         if (ret_val)
1752                 return ret_val;
1753
1754         DEBUGOUT1("IGP PSCR: %X\n", phy_data);
1755
1756         usec_delay(1);
1757
1758         if (phy->autoneg_wait_to_complete) {
1759                 DEBUGOUT("Waiting for forced speed/duplex link on IGP phy.\n");
1760
1761                 ret_val = e1000_phy_has_link_generic(hw, PHY_FORCE_LIMIT,
1762                                                      100000, &link);
1763                 if (ret_val)
1764                         return ret_val;
1765
1766                 if (!link)
1767                         DEBUGOUT("Link taking longer than expected.\n");
1768
1769                 /* Try once more */
1770                 ret_val = e1000_phy_has_link_generic(hw, PHY_FORCE_LIMIT,
1771                                                      100000, &link);
1772         }
1773
1774         return ret_val;
1775 }
1776
1777 /**
1778  *  e1000_phy_force_speed_duplex_m88 - Force speed/duplex for m88 PHY
1779  *  @hw: pointer to the HW structure
1780  *
1781  *  Calls the PHY setup function to force speed and duplex.  Clears the
1782  *  auto-crossover to force MDI manually.  Resets the PHY to commit the
1783  *  changes.  If time expires while waiting for link up, we reset the DSP.
1784  *  After reset, TX_CLK and CRS on Tx must be set.  Return successful upon
1785  *  successful completion, else return corresponding error code.
1786  **/
1787 s32 e1000_phy_force_speed_duplex_m88(struct e1000_hw *hw)
1788 {
1789         struct e1000_phy_info *phy = &hw->phy;
1790         s32 ret_val;
1791         u16 phy_data;
1792         bool link;
1793
1794         DEBUGFUNC("e1000_phy_force_speed_duplex_m88");
1795
1796         /* I210 and I211 devices support Auto-Crossover in forced operation. */
1797         if (phy->type != e1000_phy_i210) {
1798                 /* Clear Auto-Crossover to force MDI manually.  M88E1000
1799                  * requires MDI forced whenever speed and duplex are forced.
1800                  */
1801                 ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL,
1802                                             &phy_data);
1803                 if (ret_val)
1804                         return ret_val;
1805
1806                 phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
1807                 ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL,
1808                                              phy_data);
1809                 if (ret_val)
1810                         return ret_val;
1811         }
1812
1813         DEBUGOUT1("M88E1000 PSCR: %X\n", phy_data);
1814
1815         ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_data);
1816         if (ret_val)
1817                 return ret_val;
1818
1819         e1000_phy_force_speed_duplex_setup(hw, &phy_data);
1820
1821         ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_data);
1822         if (ret_val)
1823                 return ret_val;
1824
1825         /* Reset the phy to commit changes. */
1826         ret_val = hw->phy.ops.commit(hw);
1827         if (ret_val)
1828                 return ret_val;
1829
1830         if (phy->autoneg_wait_to_complete) {
1831                 DEBUGOUT("Waiting for forced speed/duplex link on M88 phy.\n");
1832
1833                 ret_val = e1000_phy_has_link_generic(hw, PHY_FORCE_LIMIT,
1834                                                      100000, &link);
1835                 if (ret_val)
1836                         return ret_val;
1837
1838                 if (!link) {
1839                         bool reset_dsp = true;
1840
1841                         switch (hw->phy.id) {
1842                         case I347AT4_E_PHY_ID:
1843                         case M88E1340M_E_PHY_ID:
1844                         case M88E1112_E_PHY_ID:
1845                         case I210_I_PHY_ID:
1846                                 reset_dsp = false;
1847                                 break;
1848                         default:
1849                                 if (hw->phy.type != e1000_phy_m88)
1850                                         reset_dsp = false;
1851                                 break;
1852                         }
1853
1854                         if (!reset_dsp) {
1855                                 DEBUGOUT("Link taking longer than expected.\n");
1856                         } else {
1857                                 /* We didn't get link.
1858                                  * Reset the DSP and cross our fingers.
1859                                  */
1860                                 ret_val = phy->ops.write_reg(hw,
1861                                                 M88E1000_PHY_PAGE_SELECT,
1862                                                 0x001d);
1863                                 if (ret_val)
1864                                         return ret_val;
1865                                 ret_val = e1000_phy_reset_dsp_generic(hw);
1866                                 if (ret_val)
1867                                         return ret_val;
1868                         }
1869                 }
1870
1871                 /* Try once more */
1872                 ret_val = e1000_phy_has_link_generic(hw, PHY_FORCE_LIMIT,
1873                                                      100000, &link);
1874                 if (ret_val)
1875                         return ret_val;
1876         }
1877
1878         if (hw->phy.type != e1000_phy_m88)
1879                 return E1000_SUCCESS;
1880
1881         if (hw->phy.id == I347AT4_E_PHY_ID ||
1882                 hw->phy.id == M88E1340M_E_PHY_ID ||
1883                 hw->phy.id == M88E1112_E_PHY_ID)
1884                 return E1000_SUCCESS;
1885         if (hw->phy.id == I210_I_PHY_ID)
1886                 return E1000_SUCCESS;
1887         ret_val = phy->ops.read_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
1888         if (ret_val)
1889                 return ret_val;
1890
1891         /* Resetting the phy means we need to re-force TX_CLK in the
1892          * Extended PHY Specific Control Register to 25MHz clock from
1893          * the reset value of 2.5MHz.
1894          */
1895         phy_data |= M88E1000_EPSCR_TX_CLK_25;
1896         ret_val = phy->ops.write_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
1897         if (ret_val)
1898                 return ret_val;
1899
1900         /* In addition, we must re-enable CRS on Tx for both half and full
1901          * duplex.
1902          */
1903         ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1904         if (ret_val)
1905                 return ret_val;
1906
1907         phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
1908         ret_val = phy->ops.write_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1909
1910         return ret_val;
1911 }
1912
1913 /**
1914  *  e1000_phy_force_speed_duplex_ife - Force PHY speed & duplex
1915  *  @hw: pointer to the HW structure
1916  *
1917  *  Forces the speed and duplex settings of the PHY.
1918  *  This is a function pointer entry point only called by
1919  *  PHY setup routines.
1920  **/
1921 s32 e1000_phy_force_speed_duplex_ife(struct e1000_hw *hw)
1922 {
1923         struct e1000_phy_info *phy = &hw->phy;
1924         s32 ret_val;
1925         u16 data;
1926         bool link;
1927
1928         DEBUGFUNC("e1000_phy_force_speed_duplex_ife");
1929
1930         ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &data);
1931         if (ret_val)
1932                 return ret_val;
1933
1934         e1000_phy_force_speed_duplex_setup(hw, &data);
1935
1936         ret_val = phy->ops.write_reg(hw, PHY_CONTROL, data);
1937         if (ret_val)
1938                 return ret_val;
1939
1940         /* Disable MDI-X support for 10/100 */
1941         ret_val = phy->ops.read_reg(hw, IFE_PHY_MDIX_CONTROL, &data);
1942         if (ret_val)
1943                 return ret_val;
1944
1945         data &= ~IFE_PMC_AUTO_MDIX;
1946         data &= ~IFE_PMC_FORCE_MDIX;
1947
1948         ret_val = phy->ops.write_reg(hw, IFE_PHY_MDIX_CONTROL, data);
1949         if (ret_val)
1950                 return ret_val;
1951
1952         DEBUGOUT1("IFE PMC: %X\n", data);
1953
1954         usec_delay(1);
1955
1956         if (phy->autoneg_wait_to_complete) {
1957                 DEBUGOUT("Waiting for forced speed/duplex link on IFE phy.\n");
1958
1959                 ret_val = e1000_phy_has_link_generic(hw, PHY_FORCE_LIMIT,
1960                                                      100000, &link);
1961                 if (ret_val)
1962                         return ret_val;
1963
1964                 if (!link)
1965                         DEBUGOUT("Link taking longer than expected.\n");
1966
1967                 /* Try once more */
1968                 ret_val = e1000_phy_has_link_generic(hw, PHY_FORCE_LIMIT,
1969                                                      100000, &link);
1970                 if (ret_val)
1971                         return ret_val;
1972         }
1973
1974         return E1000_SUCCESS;
1975 }
1976
1977 /**
1978  *  e1000_phy_force_speed_duplex_setup - Configure forced PHY speed/duplex
1979  *  @hw: pointer to the HW structure
1980  *  @phy_ctrl: pointer to current value of PHY_CONTROL
1981  *
1982  *  Forces speed and duplex on the PHY by doing the following: disable flow
1983  *  control, force speed/duplex on the MAC, disable auto speed detection,
1984  *  disable auto-negotiation, configure duplex, configure speed, configure
1985  *  the collision distance, write configuration to CTRL register.  The
1986  *  caller must write to the PHY_CONTROL register for these settings to
1987  *  take affect.
1988  **/
1989 void e1000_phy_force_speed_duplex_setup(struct e1000_hw *hw, u16 *phy_ctrl)
1990 {
1991         struct e1000_mac_info *mac = &hw->mac;
1992         u32 ctrl;
1993
1994         DEBUGFUNC("e1000_phy_force_speed_duplex_setup");
1995
1996         /* Turn off flow control when forcing speed/duplex */
1997         hw->fc.current_mode = e1000_fc_none;
1998
1999         /* Force speed/duplex on the mac */
2000         ctrl = E1000_READ_REG(hw, E1000_CTRL);
2001         ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
2002         ctrl &= ~E1000_CTRL_SPD_SEL;
2003
2004         /* Disable Auto Speed Detection */
2005         ctrl &= ~E1000_CTRL_ASDE;
2006
2007         /* Disable autoneg on the phy */
2008         *phy_ctrl &= ~MII_CR_AUTO_NEG_EN;
2009
2010         /* Forcing Full or Half Duplex? */
2011         if (mac->forced_speed_duplex & E1000_ALL_HALF_DUPLEX) {
2012                 ctrl &= ~E1000_CTRL_FD;
2013                 *phy_ctrl &= ~MII_CR_FULL_DUPLEX;
2014                 DEBUGOUT("Half Duplex\n");
2015         } else {
2016                 ctrl |= E1000_CTRL_FD;
2017                 *phy_ctrl |= MII_CR_FULL_DUPLEX;
2018                 DEBUGOUT("Full Duplex\n");
2019         }
2020
2021         /* Forcing 10mb or 100mb? */
2022         if (mac->forced_speed_duplex & E1000_ALL_100_SPEED) {
2023                 ctrl |= E1000_CTRL_SPD_100;
2024                 *phy_ctrl |= MII_CR_SPEED_100;
2025                 *phy_ctrl &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_10);
2026                 DEBUGOUT("Forcing 100mb\n");
2027         } else {
2028                 ctrl &= ~(E1000_CTRL_SPD_1000 | E1000_CTRL_SPD_100);
2029                 *phy_ctrl |= MII_CR_SPEED_10;
2030                 *phy_ctrl &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_100);
2031                 DEBUGOUT("Forcing 10mb\n");
2032         }
2033
2034         hw->mac.ops.config_collision_dist(hw);
2035
2036         E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
2037 }
2038
2039 /**
2040  *  e1000_set_d3_lplu_state_generic - Sets low power link up state for D3
2041  *  @hw: pointer to the HW structure
2042  *  @active: boolean used to enable/disable lplu
2043  *
2044  *  Success returns 0, Failure returns 1
2045  *
2046  *  The low power link up (lplu) state is set to the power management level D3
2047  *  and SmartSpeed is disabled when active is true, else clear lplu for D3
2048  *  and enable Smartspeed.  LPLU and Smartspeed are mutually exclusive.  LPLU
2049  *  is used during Dx states where the power conservation is most important.
2050  *  During driver activity, SmartSpeed should be enabled so performance is
2051  *  maintained.
2052  **/
2053 s32 e1000_set_d3_lplu_state_generic(struct e1000_hw *hw, bool active)
2054 {
2055         struct e1000_phy_info *phy = &hw->phy;
2056         s32 ret_val;
2057         u16 data;
2058
2059         DEBUGFUNC("e1000_set_d3_lplu_state_generic");
2060
2061         if (!hw->phy.ops.read_reg)
2062                 return E1000_SUCCESS;
2063
2064         ret_val = phy->ops.read_reg(hw, IGP02E1000_PHY_POWER_MGMT, &data);
2065         if (ret_val)
2066                 return ret_val;
2067
2068         if (!active) {
2069                 data &= ~IGP02E1000_PM_D3_LPLU;
2070                 ret_val = phy->ops.write_reg(hw, IGP02E1000_PHY_POWER_MGMT,
2071                                              data);
2072                 if (ret_val)
2073                         return ret_val;
2074                 /* LPLU and SmartSpeed are mutually exclusive.  LPLU is used
2075                  * during Dx states where the power conservation is most
2076                  * important.  During driver activity we should enable
2077                  * SmartSpeed, so performance is maintained.
2078                  */
2079                 if (phy->smart_speed == e1000_smart_speed_on) {
2080                         ret_val = phy->ops.read_reg(hw,
2081                                                     IGP01E1000_PHY_PORT_CONFIG,
2082                                                     &data);
2083                         if (ret_val)
2084                                 return ret_val;
2085
2086                         data |= IGP01E1000_PSCFR_SMART_SPEED;
2087                         ret_val = phy->ops.write_reg(hw,
2088                                                      IGP01E1000_PHY_PORT_CONFIG,
2089                                                      data);
2090                         if (ret_val)
2091                                 return ret_val;
2092                 } else if (phy->smart_speed == e1000_smart_speed_off) {
2093                         ret_val = phy->ops.read_reg(hw,
2094                                                     IGP01E1000_PHY_PORT_CONFIG,
2095                                                     &data);
2096                         if (ret_val)
2097                                 return ret_val;
2098
2099                         data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2100                         ret_val = phy->ops.write_reg(hw,
2101                                                      IGP01E1000_PHY_PORT_CONFIG,
2102                                                      data);
2103                         if (ret_val)
2104                                 return ret_val;
2105                 }
2106         } else if ((phy->autoneg_advertised == E1000_ALL_SPEED_DUPLEX) ||
2107                    (phy->autoneg_advertised == E1000_ALL_NOT_GIG) ||
2108                    (phy->autoneg_advertised == E1000_ALL_10_SPEED)) {
2109                 data |= IGP02E1000_PM_D3_LPLU;
2110                 ret_val = phy->ops.write_reg(hw, IGP02E1000_PHY_POWER_MGMT,
2111                                              data);
2112                 if (ret_val)
2113                         return ret_val;
2114
2115                 /* When LPLU is enabled, we should disable SmartSpeed */
2116                 ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
2117                                             &data);
2118                 if (ret_val)
2119                         return ret_val;
2120
2121                 data &= ~IGP01E1000_PSCFR_SMART_SPEED;
2122                 ret_val = phy->ops.write_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
2123                                              data);
2124         }
2125
2126         return ret_val;
2127 }
2128
2129 /**
2130  *  e1000_check_downshift_generic - Checks whether a downshift in speed occurred
2131  *  @hw: pointer to the HW structure
2132  *
2133  *  Success returns 0, Failure returns 1
2134  *
2135  *  A downshift is detected by querying the PHY link health.
2136  **/
2137 s32 e1000_check_downshift_generic(struct e1000_hw *hw)
2138 {
2139         struct e1000_phy_info *phy = &hw->phy;
2140         s32 ret_val;
2141         u16 phy_data, offset, mask;
2142
2143         DEBUGFUNC("e1000_check_downshift_generic");
2144
2145         switch (phy->type) {
2146         case e1000_phy_i210:
2147         case e1000_phy_m88:
2148         case e1000_phy_gg82563:
2149         case e1000_phy_bm:
2150         case e1000_phy_82578:
2151                 offset = M88E1000_PHY_SPEC_STATUS;
2152                 mask = M88E1000_PSSR_DOWNSHIFT;
2153                 break;
2154         case e1000_phy_igp:
2155         case e1000_phy_igp_2:
2156         case e1000_phy_igp_3:
2157                 offset = IGP01E1000_PHY_LINK_HEALTH;
2158                 mask = IGP01E1000_PLHR_SS_DOWNGRADE;
2159                 break;
2160         default:
2161                 /* speed downshift not supported */
2162                 phy->speed_downgraded = false;
2163                 return E1000_SUCCESS;
2164         }
2165
2166         ret_val = phy->ops.read_reg(hw, offset, &phy_data);
2167
2168         if (!ret_val)
2169                 phy->speed_downgraded = !!(phy_data & mask);
2170
2171         return ret_val;
2172 }
2173
2174 /**
2175  *  e1000_check_polarity_m88 - Checks the polarity.
2176  *  @hw: pointer to the HW structure
2177  *
2178  *  Success returns 0, Failure returns -E1000_ERR_PHY (-2)
2179  *
2180  *  Polarity is determined based on the PHY specific status register.
2181  **/
2182 s32 e1000_check_polarity_m88(struct e1000_hw *hw)
2183 {
2184         struct e1000_phy_info *phy = &hw->phy;
2185         s32 ret_val;
2186         u16 data;
2187
2188         DEBUGFUNC("e1000_check_polarity_m88");
2189
2190         ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_STATUS, &data);
2191
2192         if (!ret_val)
2193                 phy->cable_polarity = ((data & M88E1000_PSSR_REV_POLARITY)
2194                                        ? e1000_rev_polarity_reversed
2195                                        : e1000_rev_polarity_normal);
2196
2197         return ret_val;
2198 }
2199
2200 /**
2201  *  e1000_check_polarity_igp - Checks the polarity.
2202  *  @hw: pointer to the HW structure
2203  *
2204  *  Success returns 0, Failure returns -E1000_ERR_PHY (-2)
2205  *
2206  *  Polarity is determined based on the PHY port status register, and the
2207  *  current speed (since there is no polarity at 100Mbps).
2208  **/
2209 s32 e1000_check_polarity_igp(struct e1000_hw *hw)
2210 {
2211         struct e1000_phy_info *phy = &hw->phy;
2212         s32 ret_val;
2213         u16 data, offset, mask;
2214
2215         DEBUGFUNC("e1000_check_polarity_igp");
2216
2217         /* Polarity is determined based on the speed of
2218          * our connection.
2219          */
2220         ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_STATUS, &data);
2221         if (ret_val)
2222                 return ret_val;
2223
2224         if ((data & IGP01E1000_PSSR_SPEED_MASK) ==
2225             IGP01E1000_PSSR_SPEED_1000MBPS) {
2226                 offset = IGP01E1000_PHY_PCS_INIT_REG;
2227                 mask = IGP01E1000_PHY_POLARITY_MASK;
2228         } else {
2229                 /* This really only applies to 10Mbps since
2230                  * there is no polarity for 100Mbps (always 0).
2231                  */
2232                 offset = IGP01E1000_PHY_PORT_STATUS;
2233                 mask = IGP01E1000_PSSR_POLARITY_REVERSED;
2234         }
2235
2236         ret_val = phy->ops.read_reg(hw, offset, &data);
2237
2238         if (!ret_val)
2239                 phy->cable_polarity = ((data & mask)
2240                                        ? e1000_rev_polarity_reversed
2241                                        : e1000_rev_polarity_normal);
2242
2243         return ret_val;
2244 }
2245
2246 /**
2247  *  e1000_check_polarity_ife - Check cable polarity for IFE PHY
2248  *  @hw: pointer to the HW structure
2249  *
2250  *  Polarity is determined on the polarity reversal feature being enabled.
2251  **/
2252 s32 e1000_check_polarity_ife(struct e1000_hw *hw)
2253 {
2254         struct e1000_phy_info *phy = &hw->phy;
2255         s32 ret_val;
2256         u16 phy_data, offset, mask;
2257
2258         DEBUGFUNC("e1000_check_polarity_ife");
2259
2260         /* Polarity is determined based on the reversal feature being enabled.
2261          */
2262         if (phy->polarity_correction) {
2263                 offset = IFE_PHY_EXTENDED_STATUS_CONTROL;
2264                 mask = IFE_PESC_POLARITY_REVERSED;
2265         } else {
2266                 offset = IFE_PHY_SPECIAL_CONTROL;
2267                 mask = IFE_PSC_FORCE_POLARITY;
2268         }
2269
2270         ret_val = phy->ops.read_reg(hw, offset, &phy_data);
2271
2272         if (!ret_val)
2273                 phy->cable_polarity = ((phy_data & mask)
2274                                        ? e1000_rev_polarity_reversed
2275                                        : e1000_rev_polarity_normal);
2276
2277         return ret_val;
2278 }
2279
2280 /**
2281  *  e1000_wait_autoneg_generic - Wait for auto-neg completion
2282  *  @hw: pointer to the HW structure
2283  *
2284  *  Waits for auto-negotiation to complete or for the auto-negotiation time
2285  *  limit to expire, which ever happens first.
2286  **/
2287 s32 e1000_wait_autoneg_generic(struct e1000_hw *hw)
2288 {
2289         s32 ret_val = E1000_SUCCESS;
2290         u16 i, phy_status;
2291
2292         DEBUGFUNC("e1000_wait_autoneg_generic");
2293
2294         if (!hw->phy.ops.read_reg)
2295                 return E1000_SUCCESS;
2296
2297         /* Break after autoneg completes or PHY_AUTO_NEG_LIMIT expires. */
2298         for (i = PHY_AUTO_NEG_LIMIT; i > 0; i--) {
2299                 ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
2300                 if (ret_val)
2301                         break;
2302                 ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
2303                 if (ret_val)
2304                         break;
2305                 if (phy_status & MII_SR_AUTONEG_COMPLETE)
2306                         break;
2307                 msec_delay(100);
2308         }
2309
2310         /* PHY_AUTO_NEG_TIME expiration doesn't guarantee auto-negotiation
2311          * has completed.
2312          */
2313         return ret_val;
2314 }
2315
2316 /**
2317  *  e1000_phy_has_link_generic - Polls PHY for link
2318  *  @hw: pointer to the HW structure
2319  *  @iterations: number of times to poll for link
2320  *  @usec_interval: delay between polling attempts
2321  *  @success: pointer to whether polling was successful or not
2322  *
2323  *  Polls the PHY status register for link, 'iterations' number of times.
2324  **/
2325 s32 e1000_phy_has_link_generic(struct e1000_hw *hw, u32 iterations,
2326                                u32 usec_interval, bool *success)
2327 {
2328         s32 ret_val = E1000_SUCCESS;
2329         u16 i, phy_status;
2330
2331         DEBUGFUNC("e1000_phy_has_link_generic");
2332
2333         if (!hw->phy.ops.read_reg)
2334                 return E1000_SUCCESS;
2335
2336         for (i = 0; i < iterations; i++) {
2337                 /* Some PHYs require the PHY_STATUS register to be read
2338                  * twice due to the link bit being sticky.  No harm doing
2339                  * it across the board.
2340                  */
2341                 ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
2342                 if (ret_val)
2343                         /* If the first read fails, another entity may have
2344                          * ownership of the resources, wait and try again to
2345                          * see if they have relinquished the resources yet.
2346                          */
2347                         usec_delay(usec_interval);
2348                 ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &phy_status);
2349                 if (ret_val)
2350                         break;
2351                 if (phy_status & MII_SR_LINK_STATUS)
2352                         break;
2353                 if (usec_interval >= 1000)
2354                         msec_delay_irq(usec_interval/1000);
2355                 else
2356                         usec_delay(usec_interval);
2357         }
2358
2359         *success = (i < iterations);
2360
2361         return ret_val;
2362 }
2363
2364 /**
2365  *  e1000_get_cable_length_m88 - Determine cable length for m88 PHY
2366  *  @hw: pointer to the HW structure
2367  *
2368  *  Reads the PHY specific status register to retrieve the cable length
2369  *  information.  The cable length is determined by averaging the minimum and
2370  *  maximum values to get the "average" cable length.  The m88 PHY has four
2371  *  possible cable length values, which are:
2372  *      Register Value          Cable Length
2373  *      0                       < 50 meters
2374  *      1                       50 - 80 meters
2375  *      2                       80 - 110 meters
2376  *      3                       110 - 140 meters
2377  *      4                       > 140 meters
2378  **/
2379 s32 e1000_get_cable_length_m88(struct e1000_hw *hw)
2380 {
2381         struct e1000_phy_info *phy = &hw->phy;
2382         s32 ret_val;
2383         u16 phy_data, index;
2384
2385         DEBUGFUNC("e1000_get_cable_length_m88");
2386
2387         ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
2388         if (ret_val)
2389                 return ret_val;
2390
2391         index = ((phy_data & M88E1000_PSSR_CABLE_LENGTH) >>
2392                  M88E1000_PSSR_CABLE_LENGTH_SHIFT);
2393
2394         if (index >= M88E1000_CABLE_LENGTH_TABLE_SIZE - 1)
2395                 return -E1000_ERR_PHY;
2396
2397         phy->min_cable_length = e1000_m88_cable_length_table[index];
2398         phy->max_cable_length = e1000_m88_cable_length_table[index + 1];
2399
2400         phy->cable_length = (phy->min_cable_length + phy->max_cable_length) / 2;
2401
2402         return E1000_SUCCESS;
2403 }
2404
2405 s32 e1000_get_cable_length_m88_gen2(struct e1000_hw *hw)
2406 {
2407         struct e1000_phy_info *phy = &hw->phy;
2408         s32 ret_val;
2409         u16 phy_data, phy_data2, index, default_page, is_cm;
2410
2411         DEBUGFUNC("e1000_get_cable_length_m88_gen2");
2412
2413         switch (hw->phy.id) {
2414         case I210_I_PHY_ID:
2415                 /* Get cable length from PHY Cable Diagnostics Control Reg */
2416                 ret_val = phy->ops.read_reg(hw, (0x7 << GS40G_PAGE_SHIFT) +
2417                                             (I347AT4_PCDL + phy->addr),
2418                                             &phy_data);
2419                 if (ret_val)
2420                         return ret_val;
2421
2422                 /* Check if the unit of cable length is meters or cm */
2423                 ret_val = phy->ops.read_reg(hw, (0x7 << GS40G_PAGE_SHIFT) +
2424                                             I347AT4_PCDC, &phy_data2);
2425                 if (ret_val)
2426                         return ret_val;
2427
2428                 is_cm = !(phy_data2 & I347AT4_PCDC_CABLE_LENGTH_UNIT);
2429
2430                 /* Populate the phy structure with cable length in meters */
2431                 phy->min_cable_length = phy_data / (is_cm ? 100 : 1);
2432                 phy->max_cable_length = phy_data / (is_cm ? 100 : 1);
2433                 phy->cable_length = phy_data / (is_cm ? 100 : 1);
2434                 break;
2435         case M88E1340M_E_PHY_ID:
2436         case I347AT4_E_PHY_ID:
2437                 /* Remember the original page select and set it to 7 */
2438                 ret_val = phy->ops.read_reg(hw, I347AT4_PAGE_SELECT,
2439                                             &default_page);
2440                 if (ret_val)
2441                         return ret_val;
2442
2443                 ret_val = phy->ops.write_reg(hw, I347AT4_PAGE_SELECT, 0x07);
2444                 if (ret_val)
2445                         return ret_val;
2446
2447                 /* Get cable length from PHY Cable Diagnostics Control Reg */
2448                 ret_val = phy->ops.read_reg(hw, (I347AT4_PCDL + phy->addr),
2449                                             &phy_data);
2450                 if (ret_val)
2451                         return ret_val;
2452
2453                 /* Check if the unit of cable length is meters or cm */
2454                 ret_val = phy->ops.read_reg(hw, I347AT4_PCDC, &phy_data2);
2455                 if (ret_val)
2456                         return ret_val;
2457
2458                 is_cm = !(phy_data2 & I347AT4_PCDC_CABLE_LENGTH_UNIT);
2459
2460                 /* Populate the phy structure with cable length in meters */
2461                 phy->min_cable_length = phy_data / (is_cm ? 100 : 1);
2462                 phy->max_cable_length = phy_data / (is_cm ? 100 : 1);
2463                 phy->cable_length = phy_data / (is_cm ? 100 : 1);
2464
2465                 /* Reset the page select to its original value */
2466                 ret_val = phy->ops.write_reg(hw, I347AT4_PAGE_SELECT,
2467                                              default_page);
2468                 if (ret_val)
2469                         return ret_val;
2470                 break;
2471
2472         case M88E1112_E_PHY_ID:
2473                 /* Remember the original page select and set it to 5 */
2474                 ret_val = phy->ops.read_reg(hw, I347AT4_PAGE_SELECT,
2475                                             &default_page);
2476                 if (ret_val)
2477                         return ret_val;
2478
2479                 ret_val = phy->ops.write_reg(hw, I347AT4_PAGE_SELECT, 0x05);
2480                 if (ret_val)
2481                         return ret_val;
2482
2483                 ret_val = phy->ops.read_reg(hw, M88E1112_VCT_DSP_DISTANCE,
2484                                             &phy_data);
2485                 if (ret_val)
2486                         return ret_val;
2487
2488                 index = (phy_data & M88E1000_PSSR_CABLE_LENGTH) >>
2489                         M88E1000_PSSR_CABLE_LENGTH_SHIFT;
2490
2491                 if (index >= M88E1000_CABLE_LENGTH_TABLE_SIZE - 1)
2492                         return -E1000_ERR_PHY;
2493
2494                 phy->min_cable_length = e1000_m88_cable_length_table[index];
2495                 phy->max_cable_length = e1000_m88_cable_length_table[index + 1];
2496
2497                 phy->cable_length = (phy->min_cable_length +
2498                                      phy->max_cable_length) / 2;
2499
2500                 /* Reset the page select to its original value */
2501                 ret_val = phy->ops.write_reg(hw, I347AT4_PAGE_SELECT,
2502                                              default_page);
2503                 if (ret_val)
2504                         return ret_val;
2505
2506                 break;
2507         default:
2508                 return -E1000_ERR_PHY;
2509         }
2510
2511         return ret_val;
2512 }
2513
2514 /**
2515  *  e1000_get_cable_length_igp_2 - Determine cable length for igp2 PHY
2516  *  @hw: pointer to the HW structure
2517  *
2518  *  The automatic gain control (agc) normalizes the amplitude of the
2519  *  received signal, adjusting for the attenuation produced by the
2520  *  cable.  By reading the AGC registers, which represent the
2521  *  combination of coarse and fine gain value, the value can be put
2522  *  into a lookup table to obtain the approximate cable length
2523  *  for each channel.
2524  **/
2525 s32 e1000_get_cable_length_igp_2(struct e1000_hw *hw)
2526 {
2527         struct e1000_phy_info *phy = &hw->phy;
2528         s32 ret_val;
2529         u16 phy_data, i, agc_value = 0;
2530         u16 cur_agc_index, max_agc_index = 0;
2531         u16 min_agc_index = IGP02E1000_CABLE_LENGTH_TABLE_SIZE - 1;
2532         static const u16 agc_reg_array[IGP02E1000_PHY_CHANNEL_NUM] = {
2533                 IGP02E1000_PHY_AGC_A,
2534                 IGP02E1000_PHY_AGC_B,
2535                 IGP02E1000_PHY_AGC_C,
2536                 IGP02E1000_PHY_AGC_D
2537         };
2538
2539         DEBUGFUNC("e1000_get_cable_length_igp_2");
2540
2541         /* Read the AGC registers for all channels */
2542         for (i = 0; i < IGP02E1000_PHY_CHANNEL_NUM; i++) {
2543                 ret_val = phy->ops.read_reg(hw, agc_reg_array[i], &phy_data);
2544                 if (ret_val)
2545                         return ret_val;
2546
2547                 /* Getting bits 15:9, which represent the combination of
2548                  * coarse and fine gain values.  The result is a number
2549                  * that can be put into the lookup table to obtain the
2550                  * approximate cable length.
2551                  */
2552                 cur_agc_index = ((phy_data >> IGP02E1000_AGC_LENGTH_SHIFT) &
2553                                  IGP02E1000_AGC_LENGTH_MASK);
2554
2555                 /* Array index bound check. */
2556                 if ((cur_agc_index >= IGP02E1000_CABLE_LENGTH_TABLE_SIZE) ||
2557                     (cur_agc_index == 0))
2558                         return -E1000_ERR_PHY;
2559
2560                 /* Remove min & max AGC values from calculation. */
2561                 if (e1000_igp_2_cable_length_table[min_agc_index] >
2562                     e1000_igp_2_cable_length_table[cur_agc_index])
2563                         min_agc_index = cur_agc_index;
2564                 if (e1000_igp_2_cable_length_table[max_agc_index] <
2565                     e1000_igp_2_cable_length_table[cur_agc_index])
2566                         max_agc_index = cur_agc_index;
2567
2568                 agc_value += e1000_igp_2_cable_length_table[cur_agc_index];
2569         }
2570
2571         agc_value -= (e1000_igp_2_cable_length_table[min_agc_index] +
2572                       e1000_igp_2_cable_length_table[max_agc_index]);
2573         agc_value /= (IGP02E1000_PHY_CHANNEL_NUM - 2);
2574
2575         /* Calculate cable length with the error range of +/- 10 meters. */
2576         phy->min_cable_length = (((agc_value - IGP02E1000_AGC_RANGE) > 0) ?
2577                                  (agc_value - IGP02E1000_AGC_RANGE) : 0);
2578         phy->max_cable_length = agc_value + IGP02E1000_AGC_RANGE;
2579
2580         phy->cable_length = (phy->min_cable_length + phy->max_cable_length) / 2;
2581
2582         return E1000_SUCCESS;
2583 }
2584
2585 /**
2586  *  e1000_get_phy_info_m88 - Retrieve PHY information
2587  *  @hw: pointer to the HW structure
2588  *
2589  *  Valid for only copper links.  Read the PHY status register (sticky read)
2590  *  to verify that link is up.  Read the PHY special control register to
2591  *  determine the polarity and 10base-T extended distance.  Read the PHY
2592  *  special status register to determine MDI/MDIx and current speed.  If
2593  *  speed is 1000, then determine cable length, local and remote receiver.
2594  **/
2595 s32 e1000_get_phy_info_m88(struct e1000_hw *hw)
2596 {
2597         struct e1000_phy_info *phy = &hw->phy;
2598         s32  ret_val;
2599         u16 phy_data;
2600         bool link;
2601
2602         DEBUGFUNC("e1000_get_phy_info_m88");
2603
2604         if (phy->media_type != e1000_media_type_copper) {
2605                 DEBUGOUT("Phy info is only valid for copper media\n");
2606                 return -E1000_ERR_CONFIG;
2607         }
2608
2609         ret_val = e1000_phy_has_link_generic(hw, 1, 0, &link);
2610         if (ret_val)
2611                 return ret_val;
2612
2613         if (!link) {
2614                 DEBUGOUT("Phy info is only valid if link is up\n");
2615                 return -E1000_ERR_CONFIG;
2616         }
2617
2618         ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
2619         if (ret_val)
2620                 return ret_val;
2621
2622         phy->polarity_correction = !!(phy_data &
2623                                       M88E1000_PSCR_POLARITY_REVERSAL);
2624
2625         ret_val = e1000_check_polarity_m88(hw);
2626         if (ret_val)
2627                 return ret_val;
2628
2629         ret_val = phy->ops.read_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
2630         if (ret_val)
2631                 return ret_val;
2632
2633         phy->is_mdix = !!(phy_data & M88E1000_PSSR_MDIX);
2634
2635         if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS) {
2636                 ret_val = hw->phy.ops.get_cable_length(hw);
2637                 if (ret_val)
2638                         return ret_val;
2639
2640                 ret_val = phy->ops.read_reg(hw, PHY_1000T_STATUS, &phy_data);
2641                 if (ret_val)
2642                         return ret_val;
2643
2644                 phy->local_rx = (phy_data & SR_1000T_LOCAL_RX_STATUS)
2645                                 ? e1000_1000t_rx_status_ok
2646                                 : e1000_1000t_rx_status_not_ok;
2647
2648                 phy->remote_rx = (phy_data & SR_1000T_REMOTE_RX_STATUS)
2649                                  ? e1000_1000t_rx_status_ok
2650                                  : e1000_1000t_rx_status_not_ok;
2651         } else {
2652                 /* Set values to "undefined" */
2653                 phy->cable_length = E1000_CABLE_LENGTH_UNDEFINED;
2654                 phy->local_rx = e1000_1000t_rx_status_undefined;
2655                 phy->remote_rx = e1000_1000t_rx_status_undefined;
2656         }
2657
2658         return ret_val;
2659 }
2660
2661 /**
2662  *  e1000_get_phy_info_igp - Retrieve igp PHY information
2663  *  @hw: pointer to the HW structure
2664  *
2665  *  Read PHY status to determine if link is up.  If link is up, then
2666  *  set/determine 10base-T extended distance and polarity correction.  Read
2667  *  PHY port status to determine MDI/MDIx and speed.  Based on the speed,
2668  *  determine on the cable length, local and remote receiver.
2669  **/
2670 s32 e1000_get_phy_info_igp(struct e1000_hw *hw)
2671 {
2672         struct e1000_phy_info *phy = &hw->phy;
2673         s32 ret_val;
2674         u16 data;
2675         bool link;
2676
2677         DEBUGFUNC("e1000_get_phy_info_igp");
2678
2679         ret_val = e1000_phy_has_link_generic(hw, 1, 0, &link);
2680         if (ret_val)
2681                 return ret_val;
2682
2683         if (!link) {
2684                 DEBUGOUT("Phy info is only valid if link is up\n");
2685                 return -E1000_ERR_CONFIG;
2686         }
2687
2688         phy->polarity_correction = true;
2689
2690         ret_val = e1000_check_polarity_igp(hw);
2691         if (ret_val)
2692                 return ret_val;
2693
2694         ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_STATUS, &data);
2695         if (ret_val)
2696                 return ret_val;
2697
2698         phy->is_mdix = !!(data & IGP01E1000_PSSR_MDIX);
2699
2700         if ((data & IGP01E1000_PSSR_SPEED_MASK) ==
2701             IGP01E1000_PSSR_SPEED_1000MBPS) {
2702                 ret_val = phy->ops.get_cable_length(hw);
2703                 if (ret_val)
2704                         return ret_val;
2705
2706                 ret_val = phy->ops.read_reg(hw, PHY_1000T_STATUS, &data);
2707                 if (ret_val)
2708                         return ret_val;
2709
2710                 phy->local_rx = (data & SR_1000T_LOCAL_RX_STATUS)
2711                                 ? e1000_1000t_rx_status_ok
2712                                 : e1000_1000t_rx_status_not_ok;
2713
2714                 phy->remote_rx = (data & SR_1000T_REMOTE_RX_STATUS)
2715                                  ? e1000_1000t_rx_status_ok
2716                                  : e1000_1000t_rx_status_not_ok;
2717         } else {
2718                 phy->cable_length = E1000_CABLE_LENGTH_UNDEFINED;
2719                 phy->local_rx = e1000_1000t_rx_status_undefined;
2720                 phy->remote_rx = e1000_1000t_rx_status_undefined;
2721         }
2722
2723         return ret_val;
2724 }
2725
2726 /**
2727  *  e1000_get_phy_info_ife - Retrieves various IFE PHY states
2728  *  @hw: pointer to the HW structure
2729  *
2730  *  Populates "phy" structure with various feature states.
2731  **/
2732 s32 e1000_get_phy_info_ife(struct e1000_hw *hw)
2733 {
2734         struct e1000_phy_info *phy = &hw->phy;
2735         s32 ret_val;
2736         u16 data;
2737         bool link;
2738
2739         DEBUGFUNC("e1000_get_phy_info_ife");
2740
2741         ret_val = e1000_phy_has_link_generic(hw, 1, 0, &link);
2742         if (ret_val)
2743                 return ret_val;
2744
2745         if (!link) {
2746                 DEBUGOUT("Phy info is only valid if link is up\n");
2747                 return -E1000_ERR_CONFIG;
2748         }
2749
2750         ret_val = phy->ops.read_reg(hw, IFE_PHY_SPECIAL_CONTROL, &data);
2751         if (ret_val)
2752                 return ret_val;
2753         phy->polarity_correction = !(data & IFE_PSC_AUTO_POLARITY_DISABLE);
2754
2755         if (phy->polarity_correction) {
2756                 ret_val = e1000_check_polarity_ife(hw);
2757                 if (ret_val)
2758                         return ret_val;
2759         } else {
2760                 /* Polarity is forced */
2761                 phy->cable_polarity = ((data & IFE_PSC_FORCE_POLARITY)
2762                                        ? e1000_rev_polarity_reversed
2763                                        : e1000_rev_polarity_normal);
2764         }
2765
2766         ret_val = phy->ops.read_reg(hw, IFE_PHY_MDIX_CONTROL, &data);
2767         if (ret_val)
2768                 return ret_val;
2769
2770         phy->is_mdix = !!(data & IFE_PMC_MDIX_STATUS);
2771
2772         /* The following parameters are undefined for 10/100 operation. */
2773         phy->cable_length = E1000_CABLE_LENGTH_UNDEFINED;
2774         phy->local_rx = e1000_1000t_rx_status_undefined;
2775         phy->remote_rx = e1000_1000t_rx_status_undefined;
2776
2777         return E1000_SUCCESS;
2778 }
2779
2780 /**
2781  *  e1000_phy_sw_reset_generic - PHY software reset
2782  *  @hw: pointer to the HW structure
2783  *
2784  *  Does a software reset of the PHY by reading the PHY control register and
2785  *  setting/write the control register reset bit to the PHY.
2786  **/
2787 s32 e1000_phy_sw_reset_generic(struct e1000_hw *hw)
2788 {
2789         s32 ret_val;
2790         u16 phy_ctrl;
2791
2792         DEBUGFUNC("e1000_phy_sw_reset_generic");
2793
2794         if (!hw->phy.ops.read_reg)
2795                 return E1000_SUCCESS;
2796
2797         ret_val = hw->phy.ops.read_reg(hw, PHY_CONTROL, &phy_ctrl);
2798         if (ret_val)
2799                 return ret_val;
2800
2801         phy_ctrl |= MII_CR_RESET;
2802         ret_val = hw->phy.ops.write_reg(hw, PHY_CONTROL, phy_ctrl);
2803         if (ret_val)
2804                 return ret_val;
2805
2806         usec_delay(1);
2807
2808         return ret_val;
2809 }
2810
2811 /**
2812  *  e1000_phy_hw_reset_generic - PHY hardware reset
2813  *  @hw: pointer to the HW structure
2814  *
2815  *  Verify the reset block is not blocking us from resetting.  Acquire
2816  *  semaphore (if necessary) and read/set/write the device control reset
2817  *  bit in the PHY.  Wait the appropriate delay time for the device to
2818  *  reset and release the semaphore (if necessary).
2819  **/
2820 s32 e1000_phy_hw_reset_generic(struct e1000_hw *hw)
2821 {
2822         struct e1000_phy_info *phy = &hw->phy;
2823         s32 ret_val;
2824         u32 ctrl;
2825
2826         DEBUGFUNC("e1000_phy_hw_reset_generic");
2827
2828         if (phy->ops.check_reset_block) {
2829                 ret_val = phy->ops.check_reset_block(hw);
2830                 if (ret_val)
2831                         return E1000_SUCCESS;
2832         }
2833
2834         ret_val = phy->ops.acquire(hw);
2835         if (ret_val)
2836                 return ret_val;
2837
2838         ctrl = E1000_READ_REG(hw, E1000_CTRL);
2839         E1000_WRITE_REG(hw, E1000_CTRL, ctrl | E1000_CTRL_PHY_RST);
2840         E1000_WRITE_FLUSH(hw);
2841
2842         usec_delay(phy->reset_delay_us);
2843
2844         E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
2845         E1000_WRITE_FLUSH(hw);
2846
2847         usec_delay(150);
2848
2849         phy->ops.release(hw);
2850
2851         return phy->ops.get_cfg_done(hw);
2852 }
2853
2854 /**
2855  *  e1000_get_cfg_done_generic - Generic configuration done
2856  *  @hw: pointer to the HW structure
2857  *
2858  *  Generic function to wait 10 milli-seconds for configuration to complete
2859  *  and return success.
2860  **/
2861 s32 e1000_get_cfg_done_generic(struct e1000_hw E1000_UNUSEDARG *hw)
2862 {
2863         DEBUGFUNC("e1000_get_cfg_done_generic");
2864         UNREFERENCED_1PARAMETER(hw);
2865
2866         msec_delay_irq(10);
2867
2868         return E1000_SUCCESS;
2869 }
2870
2871 /**
2872  *  e1000_phy_init_script_igp3 - Inits the IGP3 PHY
2873  *  @hw: pointer to the HW structure
2874  *
2875  *  Initializes a Intel Gigabit PHY3 when an EEPROM is not present.
2876  **/
2877 s32 e1000_phy_init_script_igp3(struct e1000_hw *hw)
2878 {
2879         DEBUGOUT("Running IGP 3 PHY init script\n");
2880
2881         /* PHY init IGP 3 */
2882         /* Enable rise/fall, 10-mode work in class-A */
2883         hw->phy.ops.write_reg(hw, 0x2F5B, 0x9018);
2884         /* Remove all caps from Replica path filter */
2885         hw->phy.ops.write_reg(hw, 0x2F52, 0x0000);
2886         /* Bias trimming for ADC, AFE and Driver (Default) */
2887         hw->phy.ops.write_reg(hw, 0x2FB1, 0x8B24);
2888         /* Increase Hybrid poly bias */
2889         hw->phy.ops.write_reg(hw, 0x2FB2, 0xF8F0);
2890         /* Add 4% to Tx amplitude in Gig mode */
2891         hw->phy.ops.write_reg(hw, 0x2010, 0x10B0);
2892         /* Disable trimming (TTT) */
2893         hw->phy.ops.write_reg(hw, 0x2011, 0x0000);
2894         /* Poly DC correction to 94.6% + 2% for all channels */
2895         hw->phy.ops.write_reg(hw, 0x20DD, 0x249A);
2896         /* ABS DC correction to 95.9% */
2897         hw->phy.ops.write_reg(hw, 0x20DE, 0x00D3);
2898         /* BG temp curve trim */
2899         hw->phy.ops.write_reg(hw, 0x28B4, 0x04CE);
2900         /* Increasing ADC OPAMP stage 1 currents to max */
2901         hw->phy.ops.write_reg(hw, 0x2F70, 0x29E4);
2902         /* Force 1000 ( required for enabling PHY regs configuration) */
2903         hw->phy.ops.write_reg(hw, 0x0000, 0x0140);
2904         /* Set upd_freq to 6 */
2905         hw->phy.ops.write_reg(hw, 0x1F30, 0x1606);
2906         /* Disable NPDFE */
2907         hw->phy.ops.write_reg(hw, 0x1F31, 0xB814);
2908         /* Disable adaptive fixed FFE (Default) */
2909         hw->phy.ops.write_reg(hw, 0x1F35, 0x002A);
2910         /* Enable FFE hysteresis */
2911         hw->phy.ops.write_reg(hw, 0x1F3E, 0x0067);
2912         /* Fixed FFE for short cable lengths */
2913         hw->phy.ops.write_reg(hw, 0x1F54, 0x0065);
2914         /* Fixed FFE for medium cable lengths */
2915         hw->phy.ops.write_reg(hw, 0x1F55, 0x002A);
2916         /* Fixed FFE for long cable lengths */
2917         hw->phy.ops.write_reg(hw, 0x1F56, 0x002A);
2918         /* Enable Adaptive Clip Threshold */
2919         hw->phy.ops.write_reg(hw, 0x1F72, 0x3FB0);
2920         /* AHT reset limit to 1 */
2921         hw->phy.ops.write_reg(hw, 0x1F76, 0xC0FF);
2922         /* Set AHT master delay to 127 msec */
2923         hw->phy.ops.write_reg(hw, 0x1F77, 0x1DEC);
2924         /* Set scan bits for AHT */
2925         hw->phy.ops.write_reg(hw, 0x1F78, 0xF9EF);
2926         /* Set AHT Preset bits */
2927         hw->phy.ops.write_reg(hw, 0x1F79, 0x0210);
2928         /* Change integ_factor of channel A to 3 */
2929         hw->phy.ops.write_reg(hw, 0x1895, 0x0003);
2930         /* Change prop_factor of channels BCD to 8 */
2931         hw->phy.ops.write_reg(hw, 0x1796, 0x0008);
2932         /* Change cg_icount + enable integbp for channels BCD */
2933         hw->phy.ops.write_reg(hw, 0x1798, 0xD008);
2934         /* Change cg_icount + enable integbp + change prop_factor_master
2935          * to 8 for channel A
2936          */
2937         hw->phy.ops.write_reg(hw, 0x1898, 0xD918);
2938         /* Disable AHT in Slave mode on channel A */
2939         hw->phy.ops.write_reg(hw, 0x187A, 0x0800);
2940         /* Enable LPLU and disable AN to 1000 in non-D0a states,
2941          * Enable SPD+B2B
2942          */
2943         hw->phy.ops.write_reg(hw, 0x0019, 0x008D);
2944         /* Enable restart AN on an1000_dis change */
2945         hw->phy.ops.write_reg(hw, 0x001B, 0x2080);
2946         /* Enable wh_fifo read clock in 10/100 modes */
2947         hw->phy.ops.write_reg(hw, 0x0014, 0x0045);
2948         /* Restart AN, Speed selection is 1000 */
2949         hw->phy.ops.write_reg(hw, 0x0000, 0x1340);
2950
2951         return E1000_SUCCESS;
2952 }
2953
2954 /**
2955  *  e1000_get_phy_type_from_id - Get PHY type from id
2956  *  @phy_id: phy_id read from the phy
2957  *
2958  *  Returns the phy type from the id.
2959  **/
2960 enum e1000_phy_type e1000_get_phy_type_from_id(u32 phy_id)
2961 {
2962         enum e1000_phy_type phy_type = e1000_phy_unknown;
2963
2964         switch (phy_id) {
2965         case M88E1000_I_PHY_ID:
2966         case M88E1000_E_PHY_ID:
2967         case M88E1111_I_PHY_ID:
2968         case M88E1011_I_PHY_ID:
2969         case I347AT4_E_PHY_ID:
2970         case M88E1112_E_PHY_ID:
2971         case M88E1340M_E_PHY_ID:
2972                 phy_type = e1000_phy_m88;
2973                 break;
2974         case IGP01E1000_I_PHY_ID: /* IGP 1 & 2 share this */
2975                 phy_type = e1000_phy_igp_2;
2976                 break;
2977         case GG82563_E_PHY_ID:
2978                 phy_type = e1000_phy_gg82563;
2979                 break;
2980         case IGP03E1000_E_PHY_ID:
2981                 phy_type = e1000_phy_igp_3;
2982                 break;
2983         case IFE_E_PHY_ID:
2984         case IFE_PLUS_E_PHY_ID:
2985         case IFE_C_E_PHY_ID:
2986                 phy_type = e1000_phy_ife;
2987                 break;
2988         case BME1000_E_PHY_ID:
2989         case BME1000_E_PHY_ID_R2:
2990                 phy_type = e1000_phy_bm;
2991                 break;
2992         case I82578_E_PHY_ID:
2993                 phy_type = e1000_phy_82578;
2994                 break;
2995         case I82577_E_PHY_ID:
2996                 phy_type = e1000_phy_82577;
2997                 break;
2998         case I82579_E_PHY_ID:
2999                 phy_type = e1000_phy_82579;
3000                 break;
3001         case I217_E_PHY_ID:
3002                 phy_type = e1000_phy_i217;
3003                 break;
3004         case I82580_I_PHY_ID:
3005                 phy_type = e1000_phy_82580;
3006                 break;
3007         case I210_I_PHY_ID:
3008                 phy_type = e1000_phy_i210;
3009                 break;
3010         default:
3011                 phy_type = e1000_phy_unknown;
3012                 break;
3013         }
3014         return phy_type;
3015 }
3016
3017 /**
3018  *  e1000_determine_phy_address - Determines PHY address.
3019  *  @hw: pointer to the HW structure
3020  *
3021  *  This uses a trial and error method to loop through possible PHY
3022  *  addresses. It tests each by reading the PHY ID registers and
3023  *  checking for a match.
3024  **/
3025 s32 e1000_determine_phy_address(struct e1000_hw *hw)
3026 {
3027         u32 phy_addr = 0;
3028         u32 i;
3029         enum e1000_phy_type phy_type = e1000_phy_unknown;
3030
3031         hw->phy.id = phy_type;
3032
3033         for (phy_addr = 0; phy_addr < E1000_MAX_PHY_ADDR; phy_addr++) {
3034                 hw->phy.addr = phy_addr;
3035                 i = 0;
3036
3037                 do {
3038                         e1000_get_phy_id(hw);
3039                         phy_type = e1000_get_phy_type_from_id(hw->phy.id);
3040
3041                         /* If phy_type is valid, break - we found our
3042                          * PHY address
3043                          */
3044                         if (phy_type != e1000_phy_unknown)
3045                                 return E1000_SUCCESS;
3046
3047                         msec_delay(1);
3048                         i++;
3049                 } while (i < 10);
3050         }
3051
3052         return -E1000_ERR_PHY_TYPE;
3053 }
3054
3055 /**
3056  *  e1000_get_phy_addr_for_bm_page - Retrieve PHY page address
3057  *  @page: page to access
3058  *
3059  *  Returns the phy address for the page requested.
3060  **/
3061 static u32 e1000_get_phy_addr_for_bm_page(u32 page, u32 reg)
3062 {
3063         u32 phy_addr = 2;
3064
3065         if ((page >= 768) || (page == 0 && reg == 25) || (reg == 31))
3066                 phy_addr = 1;
3067
3068         return phy_addr;
3069 }
3070
3071 /**
3072  *  e1000_write_phy_reg_bm - Write BM PHY register
3073  *  @hw: pointer to the HW structure
3074  *  @offset: register offset to write to
3075  *  @data: data to write at register offset
3076  *
3077  *  Acquires semaphore, if necessary, then writes the data to PHY register
3078  *  at the offset.  Release any acquired semaphores before exiting.
3079  **/
3080 s32 e1000_write_phy_reg_bm(struct e1000_hw *hw, u32 offset, u16 data)
3081 {
3082         s32 ret_val;
3083         u32 page = offset >> IGP_PAGE_SHIFT;
3084
3085         DEBUGFUNC("e1000_write_phy_reg_bm");
3086
3087         ret_val = hw->phy.ops.acquire(hw);
3088         if (ret_val)
3089                 return ret_val;
3090
3091         /* Page 800 works differently than the rest so it has its own func */
3092         if (page == BM_WUC_PAGE) {
3093                 ret_val = e1000_access_phy_wakeup_reg_bm(hw, offset, &data,
3094                                                          false, false);
3095                 goto release;
3096         }
3097
3098         hw->phy.addr = e1000_get_phy_addr_for_bm_page(page, offset);
3099
3100         if (offset > MAX_PHY_MULTI_PAGE_REG) {
3101                 u32 page_shift, page_select;
3102
3103                 /* Page select is register 31 for phy address 1 and 22 for
3104                  * phy address 2 and 3. Page select is shifted only for
3105                  * phy address 1.
3106                  */
3107                 if (hw->phy.addr == 1) {
3108                         page_shift = IGP_PAGE_SHIFT;
3109                         page_select = IGP01E1000_PHY_PAGE_SELECT;
3110                 } else {
3111                         page_shift = 0;
3112                         page_select = BM_PHY_PAGE_SELECT;
3113                 }
3114
3115                 /* Page is shifted left, PHY expects (page x 32) */
3116                 ret_val = e1000_write_phy_reg_mdic(hw, page_select,
3117                                                    (page << page_shift));
3118                 if (ret_val)
3119                         goto release;
3120         }
3121
3122         ret_val = e1000_write_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & offset,
3123                                            data);
3124
3125 release:
3126         hw->phy.ops.release(hw);
3127         return ret_val;
3128 }
3129
3130 /**
3131  *  e1000_read_phy_reg_bm - Read BM PHY register
3132  *  @hw: pointer to the HW structure
3133  *  @offset: register offset to be read
3134  *  @data: pointer to the read data
3135  *
3136  *  Acquires semaphore, if necessary, then reads the PHY register at offset
3137  *  and storing the retrieved information in data.  Release any acquired
3138  *  semaphores before exiting.
3139  **/
3140 s32 e1000_read_phy_reg_bm(struct e1000_hw *hw, u32 offset, u16 *data)
3141 {
3142         s32 ret_val;
3143         u32 page = offset >> IGP_PAGE_SHIFT;
3144
3145         DEBUGFUNC("e1000_read_phy_reg_bm");
3146
3147         ret_val = hw->phy.ops.acquire(hw);
3148         if (ret_val)
3149                 return ret_val;
3150
3151         /* Page 800 works differently than the rest so it has its own func */
3152         if (page == BM_WUC_PAGE) {
3153                 ret_val = e1000_access_phy_wakeup_reg_bm(hw, offset, data,
3154                                                          true, false);
3155                 goto release;
3156         }
3157
3158         hw->phy.addr = e1000_get_phy_addr_for_bm_page(page, offset);
3159
3160         if (offset > MAX_PHY_MULTI_PAGE_REG) {
3161                 u32 page_shift, page_select;
3162
3163                 /* Page select is register 31 for phy address 1 and 22 for
3164                  * phy address 2 and 3. Page select is shifted only for
3165                  * phy address 1.
3166                  */
3167                 if (hw->phy.addr == 1) {
3168                         page_shift = IGP_PAGE_SHIFT;
3169                         page_select = IGP01E1000_PHY_PAGE_SELECT;
3170                 } else {
3171                         page_shift = 0;
3172                         page_select = BM_PHY_PAGE_SELECT;
3173                 }
3174
3175                 /* Page is shifted left, PHY expects (page x 32) */
3176                 ret_val = e1000_write_phy_reg_mdic(hw, page_select,
3177                                                    (page << page_shift));
3178                 if (ret_val)
3179                         goto release;
3180         }
3181
3182         ret_val = e1000_read_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & offset,
3183                                           data);
3184 release:
3185         hw->phy.ops.release(hw);
3186         return ret_val;
3187 }
3188
3189 /**
3190  *  e1000_read_phy_reg_bm2 - Read BM PHY register
3191  *  @hw: pointer to the HW structure
3192  *  @offset: register offset to be read
3193  *  @data: pointer to the read data
3194  *
3195  *  Acquires semaphore, if necessary, then reads the PHY register at offset
3196  *  and storing the retrieved information in data.  Release any acquired
3197  *  semaphores before exiting.
3198  **/
3199 s32 e1000_read_phy_reg_bm2(struct e1000_hw *hw, u32 offset, u16 *data)
3200 {
3201         s32 ret_val;
3202         u16 page = (u16)(offset >> IGP_PAGE_SHIFT);
3203
3204         DEBUGFUNC("e1000_read_phy_reg_bm2");
3205
3206         ret_val = hw->phy.ops.acquire(hw);
3207         if (ret_val)
3208                 return ret_val;
3209
3210         /* Page 800 works differently than the rest so it has its own func */
3211         if (page == BM_WUC_PAGE) {
3212                 ret_val = e1000_access_phy_wakeup_reg_bm(hw, offset, data,
3213                                                          true, false);
3214                 goto release;
3215         }
3216
3217         hw->phy.addr = 1;
3218
3219         if (offset > MAX_PHY_MULTI_PAGE_REG) {
3220                 /* Page is shifted left, PHY expects (page x 32) */
3221                 ret_val = e1000_write_phy_reg_mdic(hw, BM_PHY_PAGE_SELECT,
3222                                                    page);
3223
3224                 if (ret_val)
3225                         goto release;
3226         }
3227
3228         ret_val = e1000_read_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & offset,
3229                                           data);
3230 release:
3231         hw->phy.ops.release(hw);
3232         return ret_val;
3233 }
3234
3235 /**
3236  *  e1000_write_phy_reg_bm2 - Write BM PHY register
3237  *  @hw: pointer to the HW structure
3238  *  @offset: register offset to write to
3239  *  @data: data to write at register offset
3240  *
3241  *  Acquires semaphore, if necessary, then writes the data to PHY register
3242  *  at the offset.  Release any acquired semaphores before exiting.
3243  **/
3244 s32 e1000_write_phy_reg_bm2(struct e1000_hw *hw, u32 offset, u16 data)
3245 {
3246         s32 ret_val;
3247         u16 page = (u16)(offset >> IGP_PAGE_SHIFT);
3248
3249         DEBUGFUNC("e1000_write_phy_reg_bm2");
3250
3251         ret_val = hw->phy.ops.acquire(hw);
3252         if (ret_val)
3253                 return ret_val;
3254
3255         /* Page 800 works differently than the rest so it has its own func */
3256         if (page == BM_WUC_PAGE) {
3257                 ret_val = e1000_access_phy_wakeup_reg_bm(hw, offset, &data,
3258                                                          false, false);
3259                 goto release;
3260         }
3261
3262         hw->phy.addr = 1;
3263
3264         if (offset > MAX_PHY_MULTI_PAGE_REG) {
3265                 /* Page is shifted left, PHY expects (page x 32) */
3266                 ret_val = e1000_write_phy_reg_mdic(hw, BM_PHY_PAGE_SELECT,
3267                                                    page);
3268
3269                 if (ret_val)
3270                         goto release;
3271         }
3272
3273         ret_val = e1000_write_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & offset,
3274                                            data);
3275
3276 release:
3277         hw->phy.ops.release(hw);
3278         return ret_val;
3279 }
3280
3281 /**
3282  *  e1000_enable_phy_wakeup_reg_access_bm - enable access to BM wakeup registers
3283  *  @hw: pointer to the HW structure
3284  *  @phy_reg: pointer to store original contents of BM_WUC_ENABLE_REG
3285  *
3286  *  Assumes semaphore already acquired and phy_reg points to a valid memory
3287  *  address to store contents of the BM_WUC_ENABLE_REG register.
3288  **/
3289 s32 e1000_enable_phy_wakeup_reg_access_bm(struct e1000_hw *hw, u16 *phy_reg)
3290 {
3291         s32 ret_val;
3292         u16 temp;
3293
3294         DEBUGFUNC("e1000_enable_phy_wakeup_reg_access_bm");
3295
3296         if (!phy_reg)
3297                 return -E1000_ERR_PARAM;
3298
3299         /* All page select, port ctrl and wakeup registers use phy address 1 */
3300         hw->phy.addr = 1;
3301
3302         /* Select Port Control Registers page */
3303         ret_val = e1000_set_page_igp(hw, (BM_PORT_CTRL_PAGE << IGP_PAGE_SHIFT));
3304         if (ret_val) {
3305                 DEBUGOUT("Could not set Port Control page\n");
3306                 return ret_val;
3307         }
3308
3309         ret_val = e1000_read_phy_reg_mdic(hw, BM_WUC_ENABLE_REG, phy_reg);
3310         if (ret_val) {
3311                 DEBUGOUT2("Could not read PHY register %d.%d\n",
3312                           BM_PORT_CTRL_PAGE, BM_WUC_ENABLE_REG);
3313                 return ret_val;
3314         }
3315
3316         /* Enable both PHY wakeup mode and Wakeup register page writes.
3317          * Prevent a power state change by disabling ME and Host PHY wakeup.
3318          */
3319         temp = *phy_reg;
3320         temp |= BM_WUC_ENABLE_BIT;
3321         temp &= ~(BM_WUC_ME_WU_BIT | BM_WUC_HOST_WU_BIT);
3322
3323         ret_val = e1000_write_phy_reg_mdic(hw, BM_WUC_ENABLE_REG, temp);
3324         if (ret_val) {
3325                 DEBUGOUT2("Could not write PHY register %d.%d\n",
3326                           BM_PORT_CTRL_PAGE, BM_WUC_ENABLE_REG);
3327                 return ret_val;
3328         }
3329
3330         /* Select Host Wakeup Registers page - caller now able to write
3331          * registers on the Wakeup registers page
3332          */
3333         return e1000_set_page_igp(hw, (BM_WUC_PAGE << IGP_PAGE_SHIFT));
3334 }
3335
3336 /**
3337  *  e1000_disable_phy_wakeup_reg_access_bm - disable access to BM wakeup regs
3338  *  @hw: pointer to the HW structure
3339  *  @phy_reg: pointer to original contents of BM_WUC_ENABLE_REG
3340  *
3341  *  Restore BM_WUC_ENABLE_REG to its original value.
3342  *
3343  *  Assumes semaphore already acquired and *phy_reg is the contents of the
3344  *  BM_WUC_ENABLE_REG before register(s) on BM_WUC_PAGE were accessed by
3345  *  caller.
3346  **/
3347 s32 e1000_disable_phy_wakeup_reg_access_bm(struct e1000_hw *hw, u16 *phy_reg)
3348 {
3349         s32 ret_val;
3350
3351         DEBUGFUNC("e1000_disable_phy_wakeup_reg_access_bm");
3352
3353         if (!phy_reg)
3354                 return -E1000_ERR_PARAM;
3355
3356         /* Select Port Control Registers page */
3357         ret_val = e1000_set_page_igp(hw, (BM_PORT_CTRL_PAGE << IGP_PAGE_SHIFT));
3358         if (ret_val) {
3359                 DEBUGOUT("Could not set Port Control page\n");
3360                 return ret_val;
3361         }
3362
3363         /* Restore 769.17 to its original value */
3364         ret_val = e1000_write_phy_reg_mdic(hw, BM_WUC_ENABLE_REG, *phy_reg);
3365         if (ret_val)
3366                 DEBUGOUT2("Could not restore PHY register %d.%d\n",
3367                           BM_PORT_CTRL_PAGE, BM_WUC_ENABLE_REG);
3368
3369         return ret_val;
3370 }
3371
3372 /**
3373  *  e1000_access_phy_wakeup_reg_bm - Read/write BM PHY wakeup register
3374  *  @hw: pointer to the HW structure
3375  *  @offset: register offset to be read or written
3376  *  @data: pointer to the data to read or write
3377  *  @read: determines if operation is read or write
3378  *  @page_set: BM_WUC_PAGE already set and access enabled
3379  *
3380  *  Read the PHY register at offset and store the retrieved information in
3381  *  data, or write data to PHY register at offset.  Note the procedure to
3382  *  access the PHY wakeup registers is different than reading the other PHY
3383  *  registers. It works as such:
3384  *  1) Set 769.17.2 (page 769, register 17, bit 2) = 1
3385  *  2) Set page to 800 for host (801 if we were manageability)
3386  *  3) Write the address using the address opcode (0x11)
3387  *  4) Read or write the data using the data opcode (0x12)
3388  *  5) Restore 769.17.2 to its original value
3389  *
3390  *  Steps 1 and 2 are done by e1000_enable_phy_wakeup_reg_access_bm() and
3391  *  step 5 is done by e1000_disable_phy_wakeup_reg_access_bm().
3392  *
3393  *  Assumes semaphore is already acquired.  When page_set==true, assumes
3394  *  the PHY page is set to BM_WUC_PAGE (i.e. a function in the call stack
3395  *  is responsible for calls to e1000_[enable|disable]_phy_wakeup_reg_bm()).
3396  **/
3397 STATIC s32 e1000_access_phy_wakeup_reg_bm(struct e1000_hw *hw, u32 offset,
3398                                           u16 *data, bool read, bool page_set)
3399 {
3400         s32 ret_val;
3401         u16 reg = BM_PHY_REG_NUM(offset);
3402         u16 page = BM_PHY_REG_PAGE(offset);
3403         u16 phy_reg = 0;
3404
3405         DEBUGFUNC("e1000_access_phy_wakeup_reg_bm");
3406
3407         /* Gig must be disabled for MDIO accesses to Host Wakeup reg page */
3408         if ((hw->mac.type == e1000_pchlan) &&
3409            (!(E1000_READ_REG(hw, E1000_PHY_CTRL) & E1000_PHY_CTRL_GBE_DISABLE)))
3410                 DEBUGOUT1("Attempting to access page %d while gig enabled.\n",
3411                           page);
3412
3413         if (!page_set) {
3414                 /* Enable access to PHY wakeup registers */
3415                 ret_val = e1000_enable_phy_wakeup_reg_access_bm(hw, &phy_reg);
3416                 if (ret_val) {
3417                         DEBUGOUT("Could not enable PHY wakeup reg access\n");
3418                         return ret_val;
3419                 }
3420         }
3421
3422         DEBUGOUT2("Accessing PHY page %d reg 0x%x\n", page, reg);
3423
3424         /* Write the Wakeup register page offset value using opcode 0x11 */
3425         ret_val = e1000_write_phy_reg_mdic(hw, BM_WUC_ADDRESS_OPCODE, reg);
3426         if (ret_val) {
3427                 DEBUGOUT1("Could not write address opcode to page %d\n", page);
3428                 return ret_val;
3429         }
3430
3431         if (read) {
3432                 /* Read the Wakeup register page value using opcode 0x12 */
3433                 ret_val = e1000_read_phy_reg_mdic(hw, BM_WUC_DATA_OPCODE,
3434                                                   data);
3435         } else {
3436                 /* Write the Wakeup register page value using opcode 0x12 */
3437                 ret_val = e1000_write_phy_reg_mdic(hw, BM_WUC_DATA_OPCODE,
3438                                                    *data);
3439         }
3440
3441         if (ret_val) {
3442                 DEBUGOUT2("Could not access PHY reg %d.%d\n", page, reg);
3443                 return ret_val;
3444         }
3445
3446         if (!page_set)
3447                 ret_val = e1000_disable_phy_wakeup_reg_access_bm(hw, &phy_reg);
3448
3449         return ret_val;
3450 }
3451
3452 /**
3453  * e1000_power_up_phy_copper - Restore copper link in case of PHY power down
3454  * @hw: pointer to the HW structure
3455  *
3456  * In the case of a PHY power down to save power, or to turn off link during a
3457  * driver unload, or wake on lan is not enabled, restore the link to previous
3458  * settings.
3459  **/
3460 void e1000_power_up_phy_copper(struct e1000_hw *hw)
3461 {
3462         u16 mii_reg = 0;
3463         u16 power_reg = 0;
3464
3465         /* The PHY will retain its settings across a power down/up cycle */
3466         hw->phy.ops.read_reg(hw, PHY_CONTROL, &mii_reg);
3467         mii_reg &= ~MII_CR_POWER_DOWN;
3468         if (hw->phy.type == e1000_phy_i210) {
3469                 hw->phy.ops.read_reg(hw, GS40G_COPPER_SPEC, &power_reg);
3470                 power_reg &= ~GS40G_CS_POWER_DOWN;
3471                 hw->phy.ops.write_reg(hw, GS40G_COPPER_SPEC, power_reg);
3472         }
3473         hw->phy.ops.write_reg(hw, PHY_CONTROL, mii_reg);
3474 }
3475
3476 /**
3477  * e1000_power_down_phy_copper - Restore copper link in case of PHY power down
3478  * @hw: pointer to the HW structure
3479  *
3480  * In the case of a PHY power down to save power, or to turn off link during a
3481  * driver unload, or wake on lan is not enabled, restore the link to previous
3482  * settings.
3483  **/
3484 void e1000_power_down_phy_copper(struct e1000_hw *hw)
3485 {
3486         u16 mii_reg = 0;
3487         u16 power_reg = 0;
3488
3489         /* The PHY will retain its settings across a power down/up cycle */
3490         hw->phy.ops.read_reg(hw, PHY_CONTROL, &mii_reg);
3491         mii_reg |= MII_CR_POWER_DOWN;
3492         /* i210 Phy requires an additional bit for power up/down */
3493         if (hw->phy.type == e1000_phy_i210) {
3494                 hw->phy.ops.read_reg(hw, GS40G_COPPER_SPEC, &power_reg);
3495                 power_reg |= GS40G_CS_POWER_DOWN;
3496                 hw->phy.ops.write_reg(hw, GS40G_COPPER_SPEC, power_reg);
3497         }
3498         hw->phy.ops.write_reg(hw, PHY_CONTROL, mii_reg);
3499         msec_delay(1);
3500 }
3501
3502 /**
3503  *  __e1000_read_phy_reg_hv -  Read HV PHY register
3504  *  @hw: pointer to the HW structure
3505  *  @offset: register offset to be read
3506  *  @data: pointer to the read data
3507  *  @locked: semaphore has already been acquired or not
3508  *
3509  *  Acquires semaphore, if necessary, then reads the PHY register at offset
3510  *  and stores the retrieved information in data.  Release any acquired
3511  *  semaphore before exiting.
3512  **/
3513 static s32 __e1000_read_phy_reg_hv(struct e1000_hw *hw, u32 offset, u16 *data,
3514                                    bool locked, bool page_set)
3515 {
3516         s32 ret_val;
3517         u16 page = BM_PHY_REG_PAGE(offset);
3518         u16 reg = BM_PHY_REG_NUM(offset);
3519         u32 phy_addr = hw->phy.addr = e1000_get_phy_addr_for_hv_page(page);
3520
3521         DEBUGFUNC("__e1000_read_phy_reg_hv");
3522
3523         if (!locked) {
3524                 ret_val = hw->phy.ops.acquire(hw);
3525                 if (ret_val)
3526                         return ret_val;
3527         }
3528
3529         /* Page 800 works differently than the rest so it has its own func */
3530         if (page == BM_WUC_PAGE) {
3531                 ret_val = e1000_access_phy_wakeup_reg_bm(hw, offset, data,
3532                                                          true, page_set);
3533                 goto out;
3534         }
3535
3536         if (page > 0 && page < HV_INTC_FC_PAGE_START) {
3537                 ret_val = e1000_access_phy_debug_regs_hv(hw, offset,
3538                                                          data, true);
3539                 goto out;
3540         }
3541
3542         if (!page_set) {
3543                 if (page == HV_INTC_FC_PAGE_START)
3544                         page = 0;
3545
3546                 if (reg > MAX_PHY_MULTI_PAGE_REG) {
3547                         /* Page is shifted left, PHY expects (page x 32) */
3548                         ret_val = e1000_set_page_igp(hw,
3549                                                      (page << IGP_PAGE_SHIFT));
3550
3551                         hw->phy.addr = phy_addr;
3552
3553                         if (ret_val)
3554                                 goto out;
3555                 }
3556         }
3557
3558         DEBUGOUT3("reading PHY page %d (or 0x%x shifted) reg 0x%x\n", page,
3559                   page << IGP_PAGE_SHIFT, reg);
3560
3561         ret_val = e1000_read_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & reg,
3562                                           data);
3563 out:
3564         if (!locked)
3565                 hw->phy.ops.release(hw);
3566
3567         return ret_val;
3568 }
3569
3570 /**
3571  *  e1000_read_phy_reg_hv -  Read HV PHY register
3572  *  @hw: pointer to the HW structure
3573  *  @offset: register offset to be read
3574  *  @data: pointer to the read data
3575  *
3576  *  Acquires semaphore then reads the PHY register at offset and stores
3577  *  the retrieved information in data.  Release the acquired semaphore
3578  *  before exiting.
3579  **/
3580 s32 e1000_read_phy_reg_hv(struct e1000_hw *hw, u32 offset, u16 *data)
3581 {
3582         return __e1000_read_phy_reg_hv(hw, offset, data, false, false);
3583 }
3584
3585 /**
3586  *  e1000_read_phy_reg_hv_locked -  Read HV PHY register
3587  *  @hw: pointer to the HW structure
3588  *  @offset: register offset to be read
3589  *  @data: pointer to the read data
3590  *
3591  *  Reads the PHY register at offset and stores the retrieved information
3592  *  in data.  Assumes semaphore already acquired.
3593  **/
3594 s32 e1000_read_phy_reg_hv_locked(struct e1000_hw *hw, u32 offset, u16 *data)
3595 {
3596         return __e1000_read_phy_reg_hv(hw, offset, data, true, false);
3597 }
3598
3599 /**
3600  *  e1000_read_phy_reg_page_hv - Read HV PHY register
3601  *  @hw: pointer to the HW structure
3602  *  @offset: register offset to write to
3603  *  @data: data to write at register offset
3604  *
3605  *  Reads the PHY register at offset and stores the retrieved information
3606  *  in data.  Assumes semaphore already acquired and page already set.
3607  **/
3608 s32 e1000_read_phy_reg_page_hv(struct e1000_hw *hw, u32 offset, u16 *data)
3609 {
3610         return __e1000_read_phy_reg_hv(hw, offset, data, true, true);
3611 }
3612
3613 /**
3614  *  __e1000_write_phy_reg_hv - Write HV PHY register
3615  *  @hw: pointer to the HW structure
3616  *  @offset: register offset to write to
3617  *  @data: data to write at register offset
3618  *  @locked: semaphore has already been acquired or not
3619  *
3620  *  Acquires semaphore, if necessary, then writes the data to PHY register
3621  *  at the offset.  Release any acquired semaphores before exiting.
3622  **/
3623 static s32 __e1000_write_phy_reg_hv(struct e1000_hw *hw, u32 offset, u16 data,
3624                                     bool locked, bool page_set)
3625 {
3626         s32 ret_val;
3627         u16 page = BM_PHY_REG_PAGE(offset);
3628         u16 reg = BM_PHY_REG_NUM(offset);
3629         u32 phy_addr = hw->phy.addr = e1000_get_phy_addr_for_hv_page(page);
3630
3631         DEBUGFUNC("__e1000_write_phy_reg_hv");
3632
3633         if (!locked) {
3634                 ret_val = hw->phy.ops.acquire(hw);
3635                 if (ret_val)
3636                         return ret_val;
3637         }
3638
3639         /* Page 800 works differently than the rest so it has its own func */
3640         if (page == BM_WUC_PAGE) {
3641                 ret_val = e1000_access_phy_wakeup_reg_bm(hw, offset, &data,
3642                                                          false, page_set);
3643                 goto out;
3644         }
3645
3646         if (page > 0 && page < HV_INTC_FC_PAGE_START) {
3647                 ret_val = e1000_access_phy_debug_regs_hv(hw, offset,
3648                                                          &data, false);
3649                 goto out;
3650         }
3651
3652         if (!page_set) {
3653                 if (page == HV_INTC_FC_PAGE_START)
3654                         page = 0;
3655
3656                 /* Workaround MDIO accesses being disabled after entering IEEE
3657                  * Power Down (when bit 11 of the PHY Control register is set)
3658                  */
3659                 if ((hw->phy.type == e1000_phy_82578) &&
3660                     (hw->phy.revision >= 1) &&
3661                     (hw->phy.addr == 2) &&
3662                     !(MAX_PHY_REG_ADDRESS & reg) &&
3663                     (data & (1 << 11))) {
3664                         u16 data2 = 0x7EFF;
3665                         ret_val = e1000_access_phy_debug_regs_hv(hw,
3666                                                                  (1 << 6) | 0x3,
3667                                                                  &data2, false);
3668                         if (ret_val)
3669                                 goto out;
3670                 }
3671
3672                 if (reg > MAX_PHY_MULTI_PAGE_REG) {
3673                         /* Page is shifted left, PHY expects (page x 32) */
3674                         ret_val = e1000_set_page_igp(hw,
3675                                                      (page << IGP_PAGE_SHIFT));
3676
3677                         hw->phy.addr = phy_addr;
3678
3679                         if (ret_val)
3680                                 goto out;
3681                 }
3682         }
3683
3684         DEBUGOUT3("writing PHY page %d (or 0x%x shifted) reg 0x%x\n", page,
3685                   page << IGP_PAGE_SHIFT, reg);
3686
3687         ret_val = e1000_write_phy_reg_mdic(hw, MAX_PHY_REG_ADDRESS & reg,
3688                                            data);
3689
3690 out:
3691         if (!locked)
3692                 hw->phy.ops.release(hw);
3693
3694         return ret_val;
3695 }
3696
3697 /**
3698  *  e1000_write_phy_reg_hv - Write HV PHY register
3699  *  @hw: pointer to the HW structure
3700  *  @offset: register offset to write to
3701  *  @data: data to write at register offset
3702  *
3703  *  Acquires semaphore then writes the data to PHY register at the offset.
3704  *  Release the acquired semaphores before exiting.
3705  **/
3706 s32 e1000_write_phy_reg_hv(struct e1000_hw *hw, u32 offset, u16 data)
3707 {
3708         return __e1000_write_phy_reg_hv(hw, offset, data, false, false);
3709 }
3710
3711 /**
3712  *  e1000_write_phy_reg_hv_locked - Write HV PHY register
3713  *  @hw: pointer to the HW structure
3714  *  @offset: register offset to write to
3715  *  @data: data to write at register offset
3716  *
3717  *  Writes the data to PHY register at the offset.  Assumes semaphore
3718  *  already acquired.
3719  **/
3720 s32 e1000_write_phy_reg_hv_locked(struct e1000_hw *hw, u32 offset, u16 data)
3721 {
3722         return __e1000_write_phy_reg_hv(hw, offset, data, true, false);
3723 }
3724
3725 /**
3726  *  e1000_write_phy_reg_page_hv - Write HV PHY register
3727  *  @hw: pointer to the HW structure
3728  *  @offset: register offset to write to
3729  *  @data: data to write at register offset
3730  *
3731  *  Writes the data to PHY register at the offset.  Assumes semaphore
3732  *  already acquired and page already set.
3733  **/
3734 s32 e1000_write_phy_reg_page_hv(struct e1000_hw *hw, u32 offset, u16 data)
3735 {
3736         return __e1000_write_phy_reg_hv(hw, offset, data, true, true);
3737 }
3738
3739 /**
3740  *  e1000_get_phy_addr_for_hv_page - Get PHY adrress based on page
3741  *  @page: page to be accessed
3742  **/
3743 STATIC u32 e1000_get_phy_addr_for_hv_page(u32 page)
3744 {
3745         u32 phy_addr = 2;
3746
3747         if (page >= HV_INTC_FC_PAGE_START)
3748                 phy_addr = 1;
3749
3750         return phy_addr;
3751 }
3752
3753 /**
3754  *  e1000_access_phy_debug_regs_hv - Read HV PHY vendor specific high registers
3755  *  @hw: pointer to the HW structure
3756  *  @offset: register offset to be read or written
3757  *  @data: pointer to the data to be read or written
3758  *  @read: determines if operation is read or write
3759  *
3760  *  Reads the PHY register at offset and stores the retreived information
3761  *  in data.  Assumes semaphore already acquired.  Note that the procedure
3762  *  to access these regs uses the address port and data port to read/write.
3763  *  These accesses done with PHY address 2 and without using pages.
3764  **/
3765 STATIC s32 e1000_access_phy_debug_regs_hv(struct e1000_hw *hw, u32 offset,
3766                                           u16 *data, bool read)
3767 {
3768         s32 ret_val;
3769         u32 addr_reg;
3770         u32 data_reg;
3771
3772         DEBUGFUNC("e1000_access_phy_debug_regs_hv");
3773
3774         /* This takes care of the difference with desktop vs mobile phy */
3775         addr_reg = ((hw->phy.type == e1000_phy_82578) ?
3776                     I82578_ADDR_REG : I82577_ADDR_REG);
3777         data_reg = addr_reg + 1;
3778
3779         /* All operations in this function are phy address 2 */
3780         hw->phy.addr = 2;
3781
3782         /* masking with 0x3F to remove the page from offset */
3783         ret_val = e1000_write_phy_reg_mdic(hw, addr_reg, (u16)offset & 0x3F);
3784         if (ret_val) {
3785                 DEBUGOUT("Could not write the Address Offset port register\n");
3786                 return ret_val;
3787         }
3788
3789         /* Read or write the data value next */
3790         if (read)
3791                 ret_val = e1000_read_phy_reg_mdic(hw, data_reg, data);
3792         else
3793                 ret_val = e1000_write_phy_reg_mdic(hw, data_reg, *data);
3794
3795         if (ret_val)
3796                 DEBUGOUT("Could not access the Data port register\n");
3797
3798         return ret_val;
3799 }
3800
3801 /**
3802  *  e1000_link_stall_workaround_hv - Si workaround
3803  *  @hw: pointer to the HW structure
3804  *
3805  *  This function works around a Si bug where the link partner can get
3806  *  a link up indication before the PHY does.  If small packets are sent
3807  *  by the link partner they can be placed in the packet buffer without
3808  *  being properly accounted for by the PHY and will stall preventing
3809  *  further packets from being received.  The workaround is to clear the
3810  *  packet buffer after the PHY detects link up.
3811  **/
3812 s32 e1000_link_stall_workaround_hv(struct e1000_hw *hw)
3813 {
3814         s32 ret_val = E1000_SUCCESS;
3815         u16 data;
3816
3817         DEBUGFUNC("e1000_link_stall_workaround_hv");
3818
3819         if (hw->phy.type != e1000_phy_82578)
3820                 return E1000_SUCCESS;
3821
3822         /* Do not apply workaround if in PHY loopback bit 14 set */
3823         hw->phy.ops.read_reg(hw, PHY_CONTROL, &data);
3824         if (data & PHY_CONTROL_LB)
3825                 return E1000_SUCCESS;
3826
3827         /* check if link is up and at 1Gbps */
3828         ret_val = hw->phy.ops.read_reg(hw, BM_CS_STATUS, &data);
3829         if (ret_val)
3830                 return ret_val;
3831
3832         data &= (BM_CS_STATUS_LINK_UP | BM_CS_STATUS_RESOLVED |
3833                  BM_CS_STATUS_SPEED_MASK);
3834
3835         if (data != (BM_CS_STATUS_LINK_UP | BM_CS_STATUS_RESOLVED |
3836                      BM_CS_STATUS_SPEED_1000))
3837                 return E1000_SUCCESS;
3838
3839         msec_delay(200);
3840
3841         /* flush the packets in the fifo buffer */
3842         ret_val = hw->phy.ops.write_reg(hw, HV_MUX_DATA_CTRL,
3843                                         (HV_MUX_DATA_CTRL_GEN_TO_MAC |
3844                                          HV_MUX_DATA_CTRL_FORCE_SPEED));
3845         if (ret_val)
3846                 return ret_val;
3847
3848         return hw->phy.ops.write_reg(hw, HV_MUX_DATA_CTRL,
3849                                      HV_MUX_DATA_CTRL_GEN_TO_MAC);
3850 }
3851
3852 /**
3853  *  e1000_check_polarity_82577 - Checks the polarity.
3854  *  @hw: pointer to the HW structure
3855  *
3856  *  Success returns 0, Failure returns -E1000_ERR_PHY (-2)
3857  *
3858  *  Polarity is determined based on the PHY specific status register.
3859  **/
3860 s32 e1000_check_polarity_82577(struct e1000_hw *hw)
3861 {
3862         struct e1000_phy_info *phy = &hw->phy;
3863         s32 ret_val;
3864         u16 data;
3865
3866         DEBUGFUNC("e1000_check_polarity_82577");
3867
3868         ret_val = phy->ops.read_reg(hw, I82577_PHY_STATUS_2, &data);
3869
3870         if (!ret_val)
3871                 phy->cable_polarity = ((data & I82577_PHY_STATUS2_REV_POLARITY)
3872                                        ? e1000_rev_polarity_reversed
3873                                        : e1000_rev_polarity_normal);
3874
3875         return ret_val;
3876 }
3877
3878 /**
3879  *  e1000_phy_force_speed_duplex_82577 - Force speed/duplex for I82577 PHY
3880  *  @hw: pointer to the HW structure
3881  *
3882  *  Calls the PHY setup function to force speed and duplex.
3883  **/
3884 s32 e1000_phy_force_speed_duplex_82577(struct e1000_hw *hw)
3885 {
3886         struct e1000_phy_info *phy = &hw->phy;
3887         s32 ret_val;
3888         u16 phy_data;
3889         bool link;
3890
3891         DEBUGFUNC("e1000_phy_force_speed_duplex_82577");
3892
3893         ret_val = phy->ops.read_reg(hw, PHY_CONTROL, &phy_data);
3894         if (ret_val)
3895                 return ret_val;
3896
3897         e1000_phy_force_speed_duplex_setup(hw, &phy_data);
3898
3899         ret_val = phy->ops.write_reg(hw, PHY_CONTROL, phy_data);
3900         if (ret_val)
3901                 return ret_val;
3902
3903         usec_delay(1);
3904
3905         if (phy->autoneg_wait_to_complete) {
3906                 DEBUGOUT("Waiting for forced speed/duplex link on 82577 phy\n");
3907
3908                 ret_val = e1000_phy_has_link_generic(hw, PHY_FORCE_LIMIT,
3909                                                      100000, &link);
3910                 if (ret_val)
3911                         return ret_val;
3912
3913                 if (!link)
3914                         DEBUGOUT("Link taking longer than expected.\n");
3915
3916                 /* Try once more */
3917                 ret_val = e1000_phy_has_link_generic(hw, PHY_FORCE_LIMIT,
3918                                                      100000, &link);
3919         }
3920
3921         return ret_val;
3922 }
3923
3924 /**
3925  *  e1000_get_phy_info_82577 - Retrieve I82577 PHY information
3926  *  @hw: pointer to the HW structure
3927  *
3928  *  Read PHY status to determine if link is up.  If link is up, then
3929  *  set/determine 10base-T extended distance and polarity correction.  Read
3930  *  PHY port status to determine MDI/MDIx and speed.  Based on the speed,
3931  *  determine on the cable length, local and remote receiver.
3932  **/
3933 s32 e1000_get_phy_info_82577(struct e1000_hw *hw)
3934 {
3935         struct e1000_phy_info *phy = &hw->phy;
3936         s32 ret_val;
3937         u16 data;
3938         bool link;
3939
3940         DEBUGFUNC("e1000_get_phy_info_82577");
3941
3942         ret_val = e1000_phy_has_link_generic(hw, 1, 0, &link);
3943         if (ret_val)
3944                 return ret_val;
3945
3946         if (!link) {
3947                 DEBUGOUT("Phy info is only valid if link is up\n");
3948                 return -E1000_ERR_CONFIG;
3949         }
3950
3951         phy->polarity_correction = true;
3952
3953         ret_val = e1000_check_polarity_82577(hw);
3954         if (ret_val)
3955                 return ret_val;
3956
3957         ret_val = phy->ops.read_reg(hw, I82577_PHY_STATUS_2, &data);
3958         if (ret_val)
3959                 return ret_val;
3960
3961         phy->is_mdix = !!(data & I82577_PHY_STATUS2_MDIX);
3962
3963         if ((data & I82577_PHY_STATUS2_SPEED_MASK) ==
3964             I82577_PHY_STATUS2_SPEED_1000MBPS) {
3965                 ret_val = hw->phy.ops.get_cable_length(hw);
3966                 if (ret_val)
3967                         return ret_val;
3968
3969                 ret_val = phy->ops.read_reg(hw, PHY_1000T_STATUS, &data);
3970                 if (ret_val)
3971                         return ret_val;
3972
3973                 phy->local_rx = (data & SR_1000T_LOCAL_RX_STATUS)
3974                                 ? e1000_1000t_rx_status_ok
3975                                 : e1000_1000t_rx_status_not_ok;
3976
3977                 phy->remote_rx = (data & SR_1000T_REMOTE_RX_STATUS)
3978                                  ? e1000_1000t_rx_status_ok
3979                                  : e1000_1000t_rx_status_not_ok;
3980         } else {
3981                 phy->cable_length = E1000_CABLE_LENGTH_UNDEFINED;
3982                 phy->local_rx = e1000_1000t_rx_status_undefined;
3983                 phy->remote_rx = e1000_1000t_rx_status_undefined;
3984         }
3985
3986         return E1000_SUCCESS;
3987 }
3988
3989 /**
3990  *  e1000_get_cable_length_82577 - Determine cable length for 82577 PHY
3991  *  @hw: pointer to the HW structure
3992  *
3993  * Reads the diagnostic status register and verifies result is valid before
3994  * placing it in the phy_cable_length field.
3995  **/
3996 s32 e1000_get_cable_length_82577(struct e1000_hw *hw)
3997 {
3998         struct e1000_phy_info *phy = &hw->phy;
3999         s32 ret_val;
4000         u16 phy_data, length;
4001
4002         DEBUGFUNC("e1000_get_cable_length_82577");
4003
4004         ret_val = phy->ops.read_reg(hw, I82577_PHY_DIAG_STATUS, &phy_data);
4005         if (ret_val)
4006                 return ret_val;
4007
4008         length = ((phy_data & I82577_DSTATUS_CABLE_LENGTH) >>
4009                   I82577_DSTATUS_CABLE_LENGTH_SHIFT);
4010
4011         if (length == E1000_CABLE_LENGTH_UNDEFINED)
4012                 ret_val = -E1000_ERR_PHY;
4013
4014         phy->cable_length = length;
4015
4016         return E1000_SUCCESS;
4017 }
4018
4019 /**
4020  *  e1000_write_phy_reg_gs40g - Write GS40G  PHY register
4021  *  @hw: pointer to the HW structure
4022  *  @offset: register offset to write to
4023  *  @data: data to write at register offset
4024  *
4025  *  Acquires semaphore, if necessary, then writes the data to PHY register
4026  *  at the offset.  Release any acquired semaphores before exiting.
4027  **/
4028 s32 e1000_write_phy_reg_gs40g(struct e1000_hw *hw, u32 offset, u16 data)
4029 {
4030         s32 ret_val;
4031         u16 page = offset >> GS40G_PAGE_SHIFT;
4032
4033         DEBUGFUNC("e1000_write_phy_reg_gs40g");
4034
4035         offset = offset & GS40G_OFFSET_MASK;
4036         ret_val = hw->phy.ops.acquire(hw);
4037         if (ret_val)
4038                 return ret_val;
4039
4040         ret_val = e1000_write_phy_reg_mdic(hw, GS40G_PAGE_SELECT, page);
4041         if (ret_val)
4042                 goto release;
4043         ret_val = e1000_write_phy_reg_mdic(hw, offset, data);
4044
4045 release:
4046         hw->phy.ops.release(hw);
4047         return ret_val;
4048 }
4049
4050 /**
4051  *  e1000_read_phy_reg_gs40g - Read GS40G  PHY register
4052  *  @hw: pointer to the HW structure
4053  *  @offset: lower half is register offset to read to
4054  *     upper half is page to use.
4055  *  @data: data to read at register offset
4056  *
4057  *  Acquires semaphore, if necessary, then reads the data in the PHY register
4058  *  at the offset.  Release any acquired semaphores before exiting.
4059  **/
4060 s32 e1000_read_phy_reg_gs40g(struct e1000_hw *hw, u32 offset, u16 *data)
4061 {
4062         s32 ret_val;
4063         u16 page = offset >> GS40G_PAGE_SHIFT;
4064
4065         DEBUGFUNC("e1000_read_phy_reg_gs40g");
4066
4067         offset = offset & GS40G_OFFSET_MASK;
4068         ret_val = hw->phy.ops.acquire(hw);
4069         if (ret_val)
4070                 return ret_val;
4071
4072         ret_val = e1000_write_phy_reg_mdic(hw, GS40G_PAGE_SELECT, page);
4073         if (ret_val)
4074                 goto release;
4075         ret_val = e1000_read_phy_reg_mdic(hw, offset, data);
4076
4077 release:
4078         hw->phy.ops.release(hw);
4079         return ret_val;
4080 }
4081