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