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