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